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();
164 main_playlist_advance();
165 if (play_state == STATE_PLAYING)
166 control_notify(IMSG_CTL_NEXT);
168 control_notify(IMSG_CTL_STOP);
171 if (repeat_one && main_play_song(current_song))
173 else if (repeat_one || consume)
174 playlist_dropcurrent();
175 main_playlist_advance();
176 if (play_state == STATE_PLAYING)
177 control_notify(IMSG_CTL_NEXT);
179 control_notify(IMSG_CTL_STOP);
182 log_debug("%s: error handling imsg %d", __func__,
192 /* This pipe is dead. Remove its event handler. */
194 event_loopexit(NULL);
199 start_child(enum amused_process proc, int fd)
205 if (fd == -1 && debug)
208 switch (pid = fork()) {
210 fatal("cannot fork");
219 if (fd != -1 && dup2(fd, 3) == -1)
220 fatal("cannot setup imsg fd");
221 } else if (fcntl(F_SETFD, 0) == -1)
222 fatal("cannot setup imsg fd");
225 argv[argc++] = argv0;
230 argv[argc++] = csock;
231 argv[argc++] = "-Tm";
234 argv[argc++] = "-Tp";
244 /* obnoxious casts */
245 execvp(argv0, (char *const *)argv);
246 fatal("execvp %s", argv0);
249 /* daemon main routine */
253 int pipe_main2player[2];
256 log_init(debug, LOG_DAEMON);
257 log_setverbose(verbose);
258 log_procinit("main");
263 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
264 PF_UNSPEC, pipe_main2player) == -1)
267 player_pid = start_child(PROC_PLAYER, pipe_main2player[1]);
271 signal_set(&ev_sigint, SIGINT, main_sig_handler, NULL);
272 signal_set(&ev_sigterm, SIGTERM, main_sig_handler, NULL);
274 signal_add(&ev_sigint, NULL);
275 signal_add(&ev_sigterm, NULL);
277 signal(SIGHUP, SIG_IGN);
278 signal(SIGCHLD, SIG_IGN);
279 signal(SIGPIPE, SIG_IGN);
281 iev_player = xmalloc(sizeof(*iev_player));
282 imsg_init(&iev_player->ibuf, pipe_main2player[0]);
283 iev_player->handler = main_dispatch_player;
284 iev_player->events = EV_READ;
285 event_set(&iev_player->ev, iev_player->ibuf.fd, iev_player->events,
286 iev_player->handler, iev_player);
287 event_add(&iev_player->ev, NULL);
289 if ((control_fd = control_init(csock)) == -1)
290 fatal("control socket setup failed %s", csock);
291 control_listen(control_fd);
293 if (pledge("stdio rpath unix sendfd", NULL) == -1)
302 main(int argc, char **argv)
306 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized */
313 while ((ch = getopt(argc, argv, "ds:T:v")) != -1) {
320 csock = xstrdup(optarg);
344 if (proc == PROC_MAIN)
346 if (proc == PROC_PLAYER)
347 exit(player(debug, verbose));
350 xasprintf(&csock, "/tmp/amused-%d", getuid());
361 start_child(PROC_MAIN, -1);
365 imsg_event_add(struct imsgev *iev)
367 iev->events = EV_READ;
368 if (iev->ibuf.w.queued)
369 iev->events |= EV_WRITE;
372 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
373 event_add(&iev->ev, NULL);
377 imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
378 pid_t pid, int fd, const void *data, uint16_t datalen)
382 if ((ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data,
390 main_send_player(uint16_t type, int fd, const void *data, size_t len)
392 return imsg_compose_event(iev_player, type, 0, 0, fd, data, len);
396 main_play_song(const char *path)
401 if ((fd = open(path, O_RDONLY)) == -1) {
402 log_warn("open %s", path);
406 if (fstat(fd, &sb) == -1) {
407 log_warn("failed to stat %s", path);
412 if (!S_ISREG(sb.st_mode)) {
413 log_info("skipping non-regular file: %s", path);
418 play_state = STATE_PLAYING;
419 main_send_player(IMSG_PLAY, fd, NULL, 0);
424 main_playlist_jump(struct imsgev *iev, struct imsg *imsg)
430 datalen = IMSG_DATA_SIZE(*imsg);
431 if (datalen != sizeof(arg)) {
432 main_senderr(iev, "wrong size");
436 memcpy(arg, imsg->data, sizeof(arg));
437 if (arg[sizeof(arg)-1] != '\0') {
438 main_senderr(iev, "data corrupted");
442 song = playlist_jump(arg);
444 main_senderr(iev, "not found");
448 control_notify(IMSG_CTL_JUMP);
450 main_send_player(IMSG_STOP, -1, NULL, 0);
451 if (!main_play_song(song)) {
452 main_senderr(iev, "can't play");
453 playlist_dropcurrent();
454 main_playlist_advance();
458 main_send_status(iev);
462 main_playlist_resume(void)
466 if ((song = current_song) == NULL)
467 song = playlist_advance();
469 for (; song != NULL; song = playlist_advance()) {
470 if (main_play_song(song))
473 playlist_dropcurrent();
478 main_playlist_advance(void)
483 song = playlist_advance();
487 if (main_play_song(song))
490 playlist_dropcurrent();
495 main_playlist_previous(void)
500 song = playlist_previous();
504 if (main_play_song(song))
507 playlist_dropcurrent();
512 main_senderr(struct imsgev *iev, const char *msg)
514 imsg_compose_event(iev, IMSG_CTL_ERR, 0, 0, -1,
519 main_enqueue(int tx, struct playlist *px, struct imsgev *iev,
524 const char *err = NULL;
526 datalen = IMSG_DATA_SIZE(*imsg);
527 if (datalen != sizeof(path)) {
528 err = "data size mismatch";
532 memcpy(path, imsg->data, sizeof(path));
533 if (path[datalen-1] != '\0') {
534 err = "malformed data";
539 playlist_push(px, path);
541 playlist_enqueue(path);
542 imsg_compose_event(iev, IMSG_CTL_ADD, 0, 0, -1, path, sizeof(path));
545 main_senderr(iev, err);
549 main_send_playlist(struct imsgev *iev)
551 struct player_status s;
554 for (i = 0; i < playlist.len; ++i) {
555 memset(&s, 0, sizeof(s));
556 strlcpy(s.path, playlist.songs[i], sizeof(s.path));
557 s.status = play_off == i ? STATE_PLAYING : STATE_STOPPED;
558 imsg_compose_event(iev, IMSG_CTL_SHOW, 0, 0, -1, &s,
562 imsg_compose_event(iev, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
566 main_send_status(struct imsgev *iev)
568 struct player_status s;
570 memset(&s, 0, sizeof(s));
572 if (current_song != NULL)
573 strlcpy(s.path, current_song, sizeof(s.path));
574 s.status = play_state;
575 s.position = current_position;
576 s.duration = current_duration;
577 s.mode.repeat_all = repeat_all;
578 s.mode.repeat_one = repeat_one;
579 s.mode.consume = consume;
581 imsg_compose_event(iev, IMSG_CTL_STATUS, 0, 0, -1, &s, sizeof(s));
585 main_seek(struct player_seek *s)
587 switch (play_state) {
589 main_playlist_resume();
594 play_state = STATE_PLAYING;
599 main_send_player(IMSG_CTL_SEEK, -1, s, sizeof(*s));