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;
62 msgbuf_clear(&iev_player->ibuf.w);
63 close(iev_player->ibuf.fd);
66 log_debug("waiting for children to terminate");
70 if (errno != EINTR && errno != ECHILD)
72 } else if (WIFSIGNALED(status))
73 log_warnx("player terminated; signal %d",
75 } while (pid != -1 || (pid == -1 && errno == EINTR));
77 log_info("terminating");
82 main_sig_handler(int sig, short event, void *arg)
85 * Normal signal handler rules don't apply because libevent
95 fatalx("unexpected signal %d", sig);
100 main_dispatch_player(int sig, short event, void *d)
103 struct imsgev *iev = d;
104 struct imsgbuf *ibuf = &iev->ibuf;
110 if (event & EV_READ) {
111 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
112 fatal("imsg_read error");
113 if (n == 0) /* Connection closed */
116 if (event & EV_WRITE) {
117 if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
118 fatal("msgbuf_write");
119 if (n == 0) /* Connection closed */
124 if ((n = imsg_get(ibuf, &imsg)) == -1)
126 if (n == 0) /* No more messages. */
129 datalen = IMSG_DATA_SIZE(imsg);
130 switch (imsg.hdr.type) {
132 if (datalen != sizeof(current_position))
133 fatalx("IMSG_POS: got wrong size (%zu vs %zu)",
134 datalen, sizeof(current_position));
135 memcpy(¤t_position, imsg.data,
136 sizeof(current_position));
137 if (current_position < 0)
138 current_position = -1;
139 control_notify(IMSG_CTL_SEEK);
142 if (datalen != sizeof(current_duration))
143 fatalx("IMSG_LEN: got wrong size (%zu vs %zu)",
144 datalen, sizeof(current_duration));
145 memcpy(¤t_duration, imsg.data,
146 sizeof(current_duration));
147 if (current_duration < 0)
148 current_duration = -1;
152 errstr = "unknown error";
155 errstr[datalen-1] = '\0';
157 log_warnx("%s; skipping %s", errstr, current_song);
158 playlist_dropcurrent();
159 main_playlist_advance();
160 if (play_state == STATE_PLAYING)
161 control_notify(IMSG_CTL_NEXT);
163 control_notify(IMSG_CTL_STOP);
166 if (repeat_one && main_play_song(current_song))
168 else if (repeat_one || consume)
169 playlist_dropcurrent();
170 main_playlist_advance();
171 if (play_state == STATE_PLAYING)
172 control_notify(IMSG_CTL_NEXT);
174 control_notify(IMSG_CTL_STOP);
177 log_debug("%s: error handling imsg %d", __func__,
187 /* This pipe is dead. Remove its event handler. */
189 event_loopexit(NULL);
194 start_child(enum amused_process proc, int fd)
200 if (fd == -1 && debug)
203 switch (pid = fork()) {
205 fatal("cannot fork");
214 if (fd != -1 && dup2(fd, 3) == -1)
215 fatal("cannot setup imsg fd");
216 } else if (fcntl(F_SETFD, 0) == -1)
217 fatal("cannot setup imsg fd");
220 argv[argc++] = argv0;
225 argv[argc++] = csock;
226 argv[argc++] = "-Tm";
229 argv[argc++] = "-Tp";
239 /* obnoxious casts */
240 execvp(argv0, (char *const *)argv);
241 fatal("execvp %s", argv0);
244 /* daemon main routine */
248 int pipe_main2player[2];
251 log_init(debug, LOG_DAEMON);
252 log_setverbose(verbose);
253 log_procinit("main");
258 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
259 PF_UNSPEC, pipe_main2player) == -1)
262 player_pid = start_child(PROC_PLAYER, pipe_main2player[1]);
266 signal_set(&ev_sigint, SIGINT, main_sig_handler, NULL);
267 signal_set(&ev_sigterm, SIGTERM, main_sig_handler, NULL);
269 signal_add(&ev_sigint, NULL);
270 signal_add(&ev_sigterm, NULL);
272 signal(SIGHUP, SIG_IGN);
273 signal(SIGCHLD, SIG_IGN);
274 signal(SIGPIPE, SIG_IGN);
276 iev_player = xmalloc(sizeof(*iev_player));
277 imsg_init(&iev_player->ibuf, pipe_main2player[0]);
278 iev_player->handler = main_dispatch_player;
279 iev_player->events = EV_READ;
280 event_set(&iev_player->ev, iev_player->ibuf.fd, iev_player->events,
281 iev_player->handler, iev_player);
282 event_add(&iev_player->ev, NULL);
284 if ((control_fd = control_init(csock)) == -1)
285 fatal("control socket setup failed %s", csock);
286 control_listen(control_fd);
288 if (pledge("stdio rpath unix sendfd", NULL) == -1)
297 main(int argc, char **argv)
301 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized */
308 while ((ch = getopt(argc, argv, "ds:T:v")) != -1) {
315 csock = xstrdup(optarg);
339 if (proc == PROC_MAIN)
341 if (proc == PROC_PLAYER)
342 exit(player(debug, verbose));
345 xasprintf(&csock, "/tmp/amused-%d", getuid());
356 start_child(PROC_MAIN, -1);
360 imsg_event_add(struct imsgev *iev)
362 iev->events = EV_READ;
363 if (iev->ibuf.w.queued)
364 iev->events |= EV_WRITE;
367 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
368 event_add(&iev->ev, NULL);
372 imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
373 pid_t pid, int fd, const void *data, uint16_t datalen)
377 if ((ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data,
385 main_send_player(uint16_t type, int fd, const void *data, size_t len)
387 return imsg_compose_event(iev_player, type, 0, 0, fd, data, len);
391 main_play_song(const char *path)
396 if ((fd = open(path, O_RDONLY)) == -1) {
397 log_warn("open %s", path);
401 if (fstat(fd, &sb) == -1) {
402 log_warn("failed to stat %s", path);
407 if (!S_ISREG(sb.st_mode)) {
408 log_info("skipping non-regular file: %s", path);
413 play_state = STATE_PLAYING;
414 main_send_player(IMSG_PLAY, fd, NULL, 0);
419 main_playlist_jump(struct imsgev *iev, struct imsg *imsg)
425 datalen = IMSG_DATA_SIZE(*imsg);
426 if (datalen != sizeof(arg)) {
427 main_senderr(iev, "wrong size");
431 memcpy(arg, imsg->data, sizeof(arg));
432 if (arg[sizeof(arg)-1] != '\0') {
433 main_senderr(iev, "data corrupted");
437 song = playlist_jump(arg);
439 main_senderr(iev, "not found");
443 control_notify(IMSG_CTL_JUMP);
445 main_send_player(IMSG_STOP, -1, NULL, 0);
446 if (!main_play_song(song)) {
447 main_senderr(iev, "can't play");
448 playlist_dropcurrent();
449 main_playlist_advance();
453 main_send_status(iev);
457 main_playlist_resume(void)
461 if ((song = current_song) == NULL)
462 song = playlist_advance();
464 for (; song != NULL; song = playlist_advance()) {
465 if (main_play_song(song))
468 playlist_dropcurrent();
473 main_playlist_advance(void)
478 song = playlist_advance();
482 if (main_play_song(song))
485 playlist_dropcurrent();
490 main_playlist_previous(void)
495 song = playlist_previous();
499 if (main_play_song(song))
502 playlist_dropcurrent();
507 main_senderr(struct imsgev *iev, const char *msg)
509 imsg_compose_event(iev, IMSG_CTL_ERR, 0, 0, -1,
514 main_enqueue(int tx, struct playlist *px, struct imsgev *iev,
519 const char *err = NULL;
521 datalen = IMSG_DATA_SIZE(*imsg);
522 if (datalen != sizeof(path)) {
523 err = "data size mismatch";
527 memcpy(path, imsg->data, sizeof(path));
528 if (path[datalen-1] != '\0') {
529 err = "malformed data";
534 playlist_push(px, path);
536 playlist_enqueue(path);
537 imsg_compose_event(iev, IMSG_CTL_ADD, 0, 0, -1, path, sizeof(path));
540 main_senderr(iev, err);
544 main_send_playlist(struct imsgev *iev)
546 struct player_status s;
549 for (i = 0; i < playlist.len; ++i) {
550 memset(&s, 0, sizeof(s));
551 strlcpy(s.path, playlist.songs[i], sizeof(s.path));
552 s.status = play_off == i ? STATE_PLAYING : STATE_STOPPED;
553 imsg_compose_event(iev, IMSG_CTL_SHOW, 0, 0, -1, &s,
557 imsg_compose_event(iev, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
561 main_send_status(struct imsgev *iev)
563 struct player_status s;
565 memset(&s, 0, sizeof(s));
567 if (current_song != NULL)
568 strlcpy(s.path, current_song, sizeof(s.path));
569 s.status = play_state;
570 s.position = current_position;
571 s.duration = current_duration;
572 s.mode.repeat_all = repeat_all;
573 s.mode.repeat_one = repeat_one;
574 s.mode.consume = consume;
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;
593 main_send_player(IMSG_CTL_SEEK, -1, s, sizeof(*s));