2 * Copyright (c) 2022 Omar Polo <op@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/socket.h>
46 struct imsgev *iev_player;
50 struct event ev_sigint;
51 struct event ev_sigterm;
65 msgbuf_clear(&iev_player->ibuf.w);
66 close(iev_player->ibuf.fd);
69 log_debug("waiting for children to terminate");
73 if (errno != EINTR && errno != ECHILD)
75 } else if (WIFSIGNALED(status))
76 log_warnx("player terminated; signal %d",
78 } while (pid != -1 || (pid == -1 && errno == EINTR));
80 log_info("terminating");
85 main_sig_handler(int sig, short event, void *arg)
88 * Normal signal handler rules don't apply because libevent
98 fatalx("unexpected signal %d", sig);
103 main_dispatch_player(int sig, short event, void *d)
105 struct imsgev *iev = d;
106 struct imsgbuf *ibuf = &iev->ibuf;
111 if (event & EV_READ) {
112 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
113 fatal("imsg_read error");
114 if (n == 0) /* Connection closed */
117 if (event & EV_WRITE) {
118 if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
119 fatal("msgbuf_write");
120 if (n == 0) /* Connection closed */
125 if ((n = imsg_get(ibuf, &imsg)) == -1)
127 if (n == 0) /* No more messages. */
130 switch (imsg.hdr.type) {
132 playlist_dropcurrent();
135 if (repeat_one && current_song != NULL) {
136 if (main_play_song(current_song))
138 playlist_dropcurrent();
140 main_playlist_advance();
141 if (play_state == STATE_PLAYING)
142 control_notify(NULL, IMSG_CTL_NEXT);
144 control_notify(NULL, IMSG_CTL_STOP);
147 log_debug("%s: error handling imsg %d", __func__,
157 /* This pipe is dead. Remove its event handler. */
159 event_loopexit(NULL);
164 start_child(enum amused_process proc, int fd)
170 if (fd == -1 && debug)
173 switch (pid = fork()) {
175 fatal("cannot fork");
184 if (fd != -1 && dup2(fd, 3) == -1)
185 fatal("cannot setup imsg fd");
186 } else if (fcntl(F_SETFD, 0) == -1)
187 fatal("cannot setup imsg fd");
190 argv[argc++] = argv0;
195 argv[argc++] = csock;
196 argv[argc++] = "-Tm";
199 argv[argc++] = "-Tp";
209 /* obnoxious casts */
210 execvp(argv0, (char *const *)argv);
211 fatal("execvp %s", argv0);
214 /* daemon main routine */
218 int pipe_main2player[2];
221 log_init(debug, LOG_DAEMON);
222 log_setverbose(verbose);
223 log_procinit("main");
228 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
229 PF_UNSPEC, pipe_main2player) == -1)
232 player_pid = start_child(PROC_PLAYER, pipe_main2player[1]);
236 signal_set(&ev_sigint, SIGINT, main_sig_handler, NULL);
237 signal_set(&ev_sigterm, SIGTERM, main_sig_handler, NULL);
239 signal_add(&ev_sigint, NULL);
240 signal_add(&ev_sigterm, NULL);
242 signal(SIGHUP, SIG_IGN);
243 signal(SIGCHLD, SIG_IGN);
244 signal(SIGPIPE, SIG_IGN);
246 iev_player = xmalloc(sizeof(*iev_player));
247 imsg_init(&iev_player->ibuf, pipe_main2player[0]);
248 iev_player->handler = main_dispatch_player;
249 iev_player->events = EV_READ;
250 event_set(&iev_player->ev, iev_player->ibuf.fd, iev_player->events,
251 iev_player->handler, iev_player);
252 event_add(&iev_player->ev, NULL);
254 if ((control_fd = control_init(csock)) == -1)
255 fatal("control socket setup failed %s", csock);
256 control_listen(control_fd);
258 if (pledge("stdio rpath unix sendfd", NULL) == -1)
267 main(int argc, char **argv)
271 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized */
278 while ((ch = getopt(argc, argv, "ds:T:v")) != -1) {
285 csock = xstrdup(optarg);
309 if (proc == PROC_MAIN)
311 if (proc == PROC_PLAYER)
312 exit(player(debug, verbose));
315 xasprintf(&csock, "/tmp/amused-%d", getuid());
326 start_child(PROC_MAIN, -1);
330 imsg_event_add(struct imsgev *iev)
332 iev->events = EV_READ;
333 if (iev->ibuf.w.queued)
334 iev->events |= EV_WRITE;
337 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
338 event_add(&iev->ev, NULL);
342 imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
343 pid_t pid, int fd, const void *data, uint16_t datalen)
347 if ((ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data,
355 main_send_player(uint16_t type, int fd, const void *data, uint16_t datalen)
357 return imsg_compose_event(iev_player, type, 0, 0, fd, data, datalen);
361 main_play_song(const char *song)
364 char path[PATH_MAX] = { 0 };
367 strlcpy(path, song, sizeof(path));
368 if ((fd = open(path, O_RDONLY)) == -1) {
369 log_warn("open %s", path);
373 if (fstat(fd, &sb) == -1) {
374 log_warn("failed to stat %s", path);
379 if (S_ISDIR(sb.st_mode)) {
380 log_info("skipping a directory: %s", path);
385 play_state = STATE_PLAYING;
386 imsg_compose_event(iev_player, IMSG_PLAY, 0, 0, fd,
392 main_playlist_jump(struct imsgev *iev, struct imsg *imsg)
398 datalen = IMSG_DATA_SIZE(*imsg);
399 if (datalen != sizeof(arg)) {
400 main_senderr(iev, "wrong size");
404 memcpy(arg, imsg->data, sizeof(arg));
405 if (arg[sizeof(arg)-1] != '\0') {
406 main_senderr(iev, "data corrupted");
410 song = playlist_jump(arg);
412 main_senderr(iev, "not found");
416 main_send_player(IMSG_STOP, -1, NULL, 0);
417 if (!main_play_song(song)) {
418 main_senderr(iev, "can't play");
419 playlist_dropcurrent();
420 main_playlist_advance();
424 main_send_status(iev);
428 main_playlist_resume(void)
432 if ((song = current_song) == NULL)
433 song = playlist_advance();
435 for (; song != NULL; song = playlist_advance()) {
436 if (main_play_song(song))
439 playlist_dropcurrent();
444 main_playlist_advance(void)
449 song = playlist_advance();
453 if (main_play_song(song))
456 playlist_dropcurrent();
461 main_playlist_previous(void)
466 song = playlist_previous();
470 if (main_play_song(song))
473 playlist_dropcurrent();
478 main_restart_track(void)
486 if (main_play_song(song))
489 playlist_dropcurrent();
490 main_playlist_advance();
494 main_senderr(struct imsgev *iev, const char *msg)
496 imsg_compose_event(iev, IMSG_CTL_ERR, 0, 0, -1,
501 main_enqueue(int tx, struct playlist *px, struct imsgev *iev,
505 char path[PATH_MAX] = { 0 };
506 const char *err = NULL;
508 datalen = IMSG_DATA_SIZE(*imsg);
509 if (datalen != sizeof(path)) {
510 err = "data size mismatch";
514 memcpy(path, imsg->data, sizeof(path));
515 if (path[datalen-1] != '\0') {
516 err = "malformed data";
521 playlist_push(px, path);
523 playlist_enqueue(path);
524 imsg_compose_event(iev, IMSG_CTL_ADD, 0, 0, -1, path, sizeof(path));
527 main_senderr(iev, err);
531 main_send_playlist(struct imsgev *iev)
533 struct player_status s;
536 for (i = 0; i < playlist.len; ++i) {
537 memset(&s, 0, sizeof(s));
538 strlcpy(s.path, playlist.songs[i], sizeof(s.path));
539 s.status = play_off == i ? STATE_PLAYING : STATE_STOPPED;
540 imsg_compose_event(iev, IMSG_CTL_SHOW, 0, 0, -1, &s,
544 imsg_compose_event(iev, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
548 main_send_status(struct imsgev *iev)
550 struct player_status s;
552 memset(&s, 0, sizeof(s));
554 if (current_song != NULL)
555 strlcpy(s.path, current_song, sizeof(s.path));
556 s.status = play_state;
557 s.rp.repeat_all = repeat_all;
558 s.rp.repeat_one = repeat_one;
560 imsg_compose_event(iev, IMSG_CTL_STATUS, 0, 0, -1, &s, sizeof(s));