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>
48 struct imsgev *iev_player;
52 struct event ev_sigint;
53 struct event ev_sigterm;
67 msgbuf_clear(&iev_player->ibuf.w);
68 close(iev_player->ibuf.fd);
71 log_debug("waiting for children to terminate");
75 if (errno != EINTR && errno != ECHILD)
77 } else if (WIFSIGNALED(status))
78 log_warnx("player terminated; signal %d",
80 } while (pid != -1 || (pid == -1 && errno == EINTR));
82 log_info("terminating");
87 main_sig_handler(int sig, short event, void *arg)
90 * Normal signal handler rules don't apply because libevent
100 fatalx("unexpected signal %d", sig);
105 main_dispatch_player(int sig, short event, void *d)
107 struct imsgev *iev = d;
108 struct imsgbuf *ibuf = &iev->ibuf;
113 if (event & EV_READ) {
114 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
115 fatal("imsg_read error");
116 if (n == 0) /* Connection closed */
119 if (event & EV_WRITE) {
120 if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
121 fatal("msgbuf_write");
122 if (n == 0) /* Connection closed */
127 if ((n = imsg_get(ibuf, &imsg)) == -1)
129 if (n == 0) /* No more messages. */
132 switch (imsg.hdr.type) {
134 playlist_dropcurrent();
137 if (repeat_one && current_song != NULL) {
138 if (main_play_song(current_song))
140 playlist_dropcurrent();
142 main_playlist_advance();
143 if (play_state == STATE_PLAYING)
144 control_notify(NULL, IMSG_CTL_NEXT);
146 control_notify(NULL, IMSG_CTL_STOP);
149 log_debug("%s: error handling imsg %d", __func__,
159 /* This pipe is dead. Remove its event handler. */
161 event_loopexit(NULL);
166 start_child(enum amused_process proc, int fd)
172 if (fd == -1 && debug)
175 switch (pid = fork()) {
177 fatal("cannot fork");
186 if (fd != -1 && dup2(fd, 3) == -1)
187 fatal("cannot setup imsg fd");
188 } else if (fcntl(F_SETFD, 0) == -1)
189 fatal("cannot setup imsg fd");
192 argv[argc++] = argv0;
197 argv[argc++] = csock;
198 argv[argc++] = "-Tm";
201 argv[argc++] = "-Tp";
211 /* obnoxious casts */
212 execvp(argv0, (char *const *)argv);
213 fatal("execvp %s", argv0);
216 /* daemon main routine */
220 int pipe_main2player[2];
223 log_init(debug, LOG_DAEMON);
224 log_setverbose(verbose);
225 log_procinit("main");
230 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
231 PF_UNSPEC, pipe_main2player) == -1)
234 player_pid = start_child(PROC_PLAYER, pipe_main2player[1]);
238 signal_set(&ev_sigint, SIGINT, main_sig_handler, NULL);
239 signal_set(&ev_sigterm, SIGTERM, main_sig_handler, NULL);
241 signal_add(&ev_sigint, NULL);
242 signal_add(&ev_sigterm, NULL);
244 signal(SIGHUP, SIG_IGN);
245 signal(SIGCHLD, SIG_IGN);
246 signal(SIGPIPE, SIG_IGN);
248 iev_player = xmalloc(sizeof(*iev_player));
249 imsg_init(&iev_player->ibuf, pipe_main2player[0]);
250 iev_player->handler = main_dispatch_player;
251 iev_player->events = EV_READ;
252 event_set(&iev_player->ev, iev_player->ibuf.fd, iev_player->events,
253 iev_player->handler, iev_player);
254 event_add(&iev_player->ev, NULL);
256 if ((control_fd = control_init(csock)) == -1)
257 fatal("control socket setup failed %s", csock);
258 control_listen(control_fd);
260 if (pledge("stdio rpath unix sendfd", NULL) == -1)
269 main(int argc, char **argv)
273 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized */
280 while ((ch = getopt(argc, argv, "ds:T:v")) != -1) {
287 csock = xstrdup(optarg);
311 if (proc == PROC_MAIN)
313 if (proc == PROC_PLAYER)
314 exit(player(debug, verbose));
317 xasprintf(&csock, "/tmp/amused-%d", getuid());
328 start_child(PROC_MAIN, -1);
332 imsg_event_add(struct imsgev *iev)
334 iev->events = EV_READ;
335 if (iev->ibuf.w.queued)
336 iev->events |= EV_WRITE;
339 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
340 event_add(&iev->ev, NULL);
344 imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
345 pid_t pid, int fd, const void *data, uint16_t datalen)
349 if ((ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data,
357 main_send_player(uint16_t type, int fd, const void *data, uint16_t datalen)
359 return imsg_compose_event(iev_player, type, 0, 0, fd, data, datalen);
363 main_play_song(const char *song)
366 char path[PATH_MAX] = { 0 };
369 strlcpy(path, song, sizeof(path));
370 if ((fd = open(path, O_RDONLY)) == -1) {
371 log_warn("open %s", path);
375 if (fstat(fd, &sb) == -1) {
376 log_warn("failed to stat %s", path);
381 if (S_ISDIR(sb.st_mode)) {
382 log_info("skipping a directory: %s", path);
387 play_state = STATE_PLAYING;
388 imsg_compose_event(iev_player, IMSG_PLAY, 0, 0, fd,
394 main_playlist_jump(struct imsgev *iev, struct imsg *imsg)
400 datalen = IMSG_DATA_SIZE(*imsg);
401 if (datalen != sizeof(arg)) {
402 main_senderr(iev, "wrong size");
406 memcpy(arg, imsg->data, sizeof(arg));
407 if (arg[sizeof(arg)-1] != '\0') {
408 main_senderr(iev, "data corrupted");
412 song = playlist_jump(arg);
414 main_senderr(iev, "not found");
418 main_send_player(IMSG_STOP, -1, NULL, 0);
419 if (!main_play_song(song)) {
420 main_senderr(iev, "can't play");
421 playlist_dropcurrent();
422 main_playlist_advance();
426 main_send_status(iev);
430 main_playlist_resume(void)
434 if ((song = current_song) == NULL)
435 song = playlist_advance();
437 for (; song != NULL; song = playlist_advance()) {
438 if (main_play_song(song))
441 playlist_dropcurrent();
446 main_playlist_advance(void)
451 song = playlist_advance();
455 if (main_play_song(song))
458 playlist_dropcurrent();
463 main_playlist_previous(void)
468 song = playlist_previous();
472 if (main_play_song(song))
475 playlist_dropcurrent();
480 main_restart_track(void)
488 if (main_play_song(song))
491 playlist_dropcurrent();
492 main_playlist_advance();
496 main_senderr(struct imsgev *iev, const char *msg)
498 imsg_compose_event(iev, IMSG_CTL_ERR, 0, 0, -1,
503 main_enqueue(int tx, struct playlist *px, struct imsgev *iev,
507 char path[PATH_MAX] = { 0 };
508 const char *err = NULL;
510 datalen = IMSG_DATA_SIZE(*imsg);
511 if (datalen != sizeof(path)) {
512 err = "data size mismatch";
516 memcpy(path, imsg->data, sizeof(path));
517 if (path[datalen-1] != '\0') {
518 err = "malformed data";
523 playlist_push(px, path);
525 playlist_enqueue(path);
526 imsg_compose_event(iev, IMSG_CTL_ADD, 0, 0, -1, path, sizeof(path));
529 main_senderr(iev, err);
533 main_send_playlist(struct imsgev *iev)
535 struct player_status s;
538 for (i = 0; i < playlist.len; ++i) {
539 memset(&s, 0, sizeof(s));
540 strlcpy(s.path, playlist.songs[i], sizeof(s.path));
541 s.status = play_off == i ? STATE_PLAYING : STATE_STOPPED;
542 imsg_compose_event(iev, IMSG_CTL_SHOW, 0, 0, -1, &s,
546 imsg_compose_event(iev, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
550 main_send_status(struct imsgev *iev)
552 struct player_status s;
554 memset(&s, 0, sizeof(s));
556 if (current_song != NULL)
557 strlcpy(s.path, current_song, sizeof(s.path));
558 s.status = play_state;
559 s.rp.repeat_all = repeat_all;
560 s.rp.repeat_one = repeat_one;
562 imsg_compose_event(iev, IMSG_CTL_STATUS, 0, 0, -1, &s, sizeof(s));