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.
19 #include <sys/socket.h>
43 struct imsgev *iev_player;
47 struct event ev_sigint;
48 struct event ev_sigterm;
50 static int notify_seek;
64 msgbuf_clear(&iev_player->ibuf.w);
65 close(iev_player->ibuf.fd);
68 log_debug("waiting for children to terminate");
72 if (errno != EINTR && errno != ECHILD)
74 } else if (WIFSIGNALED(status))
75 log_warnx("player terminated; signal %d",
77 } while (pid != -1 || (pid == -1 && errno == EINTR));
79 log_info("terminating");
84 main_sig_handler(int sig, short event, void *arg)
87 * Normal signal handler rules don't apply because libevent
97 fatalx("unexpected signal %d", sig);
102 main_dispatch_player(int sig, short event, void *d)
105 struct imsgev *iev = d;
106 struct imsgbuf *ibuf = &iev->ibuf;
112 if (event & EV_READ) {
113 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
114 fatal("imsg_read error");
115 if (n == 0) /* Connection closed */
118 if (event & EV_WRITE) {
119 if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
120 fatal("msgbuf_write");
121 if (n == 0) /* Connection closed */
126 if ((n = imsg_get(ibuf, &imsg)) == -1)
128 if (n == 0) /* No more messages. */
131 datalen = IMSG_DATA_SIZE(imsg);
132 switch (imsg.hdr.type) {
134 if (datalen != sizeof(current_position))
135 fatalx("IMSG_POS: got wrong size (%zu vs %zu)",
136 datalen, sizeof(current_position));
137 memcpy(¤t_position, imsg.data,
138 sizeof(current_position));
139 if (current_position < 0)
140 current_position = -1;
143 control_notify(IMSG_CTL_SEEK);
147 if (datalen != sizeof(current_duration))
148 fatalx("IMSG_LEN: got wrong size (%zu vs %zu)",
149 datalen, sizeof(current_duration));
150 memcpy(¤t_duration, imsg.data,
151 sizeof(current_duration));
152 if (current_duration < 0)
153 current_duration = -1;
157 errstr = "unknown error";
160 errstr[datalen-1] = '\0';
162 log_warnx("%s; skipping %s", errstr, current_song);
163 playlist_dropcurrent();
166 if (repeat_one && current_song != NULL) {
167 if (main_play_song(current_song))
169 playlist_dropcurrent();
171 main_playlist_advance();
172 if (play_state == STATE_PLAYING)
173 control_notify(IMSG_CTL_NEXT);
175 control_notify(IMSG_CTL_STOP);
178 log_debug("%s: error handling imsg %d", __func__,
188 /* This pipe is dead. Remove its event handler. */
190 event_loopexit(NULL);
195 start_child(enum amused_process proc, int fd)
201 if (fd == -1 && debug)
204 switch (pid = fork()) {
206 fatal("cannot fork");
215 if (fd != -1 && dup2(fd, 3) == -1)
216 fatal("cannot setup imsg fd");
217 } else if (fcntl(F_SETFD, 0) == -1)
218 fatal("cannot setup imsg fd");
221 argv[argc++] = argv0;
226 argv[argc++] = csock;
227 argv[argc++] = "-Tm";
230 argv[argc++] = "-Tp";
240 /* obnoxious casts */
241 execvp(argv0, (char *const *)argv);
242 fatal("execvp %s", argv0);
245 /* daemon main routine */
249 int pipe_main2player[2];
252 log_init(debug, LOG_DAEMON);
253 log_setverbose(verbose);
254 log_procinit("main");
259 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
260 PF_UNSPEC, pipe_main2player) == -1)
263 player_pid = start_child(PROC_PLAYER, pipe_main2player[1]);
267 signal_set(&ev_sigint, SIGINT, main_sig_handler, NULL);
268 signal_set(&ev_sigterm, SIGTERM, main_sig_handler, NULL);
270 signal_add(&ev_sigint, NULL);
271 signal_add(&ev_sigterm, NULL);
273 signal(SIGHUP, SIG_IGN);
274 signal(SIGCHLD, SIG_IGN);
275 signal(SIGPIPE, SIG_IGN);
277 iev_player = xmalloc(sizeof(*iev_player));
278 imsg_init(&iev_player->ibuf, pipe_main2player[0]);
279 iev_player->handler = main_dispatch_player;
280 iev_player->events = EV_READ;
281 event_set(&iev_player->ev, iev_player->ibuf.fd, iev_player->events,
282 iev_player->handler, iev_player);
283 event_add(&iev_player->ev, NULL);
285 if ((control_fd = control_init(csock)) == -1)
286 fatal("control socket setup failed %s", csock);
287 control_listen(control_fd);
289 if (pledge("stdio rpath unix sendfd", NULL) == -1)
298 main(int argc, char **argv)
302 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized */
309 while ((ch = getopt(argc, argv, "ds:T:v")) != -1) {
316 csock = xstrdup(optarg);
340 if (proc == PROC_MAIN)
342 if (proc == PROC_PLAYER)
343 exit(player(debug, verbose));
346 xasprintf(&csock, "/tmp/amused-%d", getuid());
357 start_child(PROC_MAIN, -1);
361 imsg_event_add(struct imsgev *iev)
363 iev->events = EV_READ;
364 if (iev->ibuf.w.queued)
365 iev->events |= EV_WRITE;
368 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
369 event_add(&iev->ev, NULL);
373 imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
374 pid_t pid, int fd, const void *data, uint16_t datalen)
378 if ((ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data,
386 main_send_player(uint16_t type, int fd, const void *data, size_t len)
388 return imsg_compose_event(iev_player, type, 0, 0, fd, data, len);
392 main_play_song(const char *path)
397 if ((fd = open(path, O_RDONLY)) == -1) {
398 log_warn("open %s", path);
402 if (fstat(fd, &sb) == -1) {
403 log_warn("failed to stat %s", path);
408 if (!S_ISREG(sb.st_mode)) {
409 log_info("skipping non-regular file: %s", path);
414 play_state = STATE_PLAYING;
415 main_send_player(IMSG_PLAY, fd, NULL, 0);
420 main_playlist_jump(struct imsgev *iev, struct imsg *imsg)
426 datalen = IMSG_DATA_SIZE(*imsg);
427 if (datalen != sizeof(arg)) {
428 main_senderr(iev, "wrong size");
432 memcpy(arg, imsg->data, sizeof(arg));
433 if (arg[sizeof(arg)-1] != '\0') {
434 main_senderr(iev, "data corrupted");
438 song = playlist_jump(arg);
440 main_senderr(iev, "not found");
444 control_notify(IMSG_CTL_JUMP);
446 main_send_player(IMSG_STOP, -1, NULL, 0);
447 if (!main_play_song(song)) {
448 main_senderr(iev, "can't play");
449 playlist_dropcurrent();
450 main_playlist_advance();
454 main_send_status(iev);
458 main_playlist_resume(void)
462 if ((song = current_song) == NULL)
463 song = playlist_advance();
465 for (; song != NULL; song = playlist_advance()) {
466 if (main_play_song(song))
469 playlist_dropcurrent();
474 main_playlist_advance(void)
479 song = playlist_advance();
483 if (main_play_song(song))
486 playlist_dropcurrent();
491 main_playlist_previous(void)
496 song = playlist_previous();
500 if (main_play_song(song))
503 playlist_dropcurrent();
508 main_senderr(struct imsgev *iev, const char *msg)
510 imsg_compose_event(iev, IMSG_CTL_ERR, 0, 0, -1,
515 main_enqueue(int tx, struct playlist *px, struct imsgev *iev,
520 const char *err = NULL;
522 datalen = IMSG_DATA_SIZE(*imsg);
523 if (datalen != sizeof(path)) {
524 err = "data size mismatch";
528 memcpy(path, imsg->data, sizeof(path));
529 if (path[datalen-1] != '\0') {
530 err = "malformed data";
535 playlist_push(px, path);
537 playlist_enqueue(path);
538 imsg_compose_event(iev, IMSG_CTL_ADD, 0, 0, -1, path, sizeof(path));
541 main_senderr(iev, err);
545 main_send_playlist(struct imsgev *iev)
547 struct player_status s;
550 for (i = 0; i < playlist.len; ++i) {
551 memset(&s, 0, sizeof(s));
552 strlcpy(s.path, playlist.songs[i], sizeof(s.path));
553 s.status = play_off == i ? STATE_PLAYING : STATE_STOPPED;
554 imsg_compose_event(iev, IMSG_CTL_SHOW, 0, 0, -1, &s,
558 imsg_compose_event(iev, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
562 main_send_status(struct imsgev *iev)
564 struct player_status s;
566 memset(&s, 0, sizeof(s));
568 if (current_song != NULL)
569 strlcpy(s.path, current_song, sizeof(s.path));
570 s.status = play_state;
571 s.position = current_position;
572 s.duration = current_duration;
573 s.rp.repeat_all = repeat_all;
574 s.rp.repeat_one = repeat_one;
576 imsg_compose_event(iev, IMSG_CTL_STATUS, 0, 0, -1, &s, sizeof(s));
580 main_seek(struct player_seek *s)
582 switch (play_state) {
584 main_playlist_resume();
589 play_state = STATE_PLAYING;
594 main_send_player(IMSG_CTL_SEEK, -1, s, sizeof(*s));