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 switch (pid = fork()) {
174 fatal("cannot fork");
183 if (fd != -1 && dup2(fd, 3) == -1)
184 fatal("cannot setup imsg fd");
185 } else if (fcntl(F_SETFD, 0) == -1)
186 fatal("cannot setup imsg fd");
188 argv[argc++] = argv0;
193 argv[argc++] = csock;
196 argv[argc++] = "-Tp";
206 /* obnoxious casts */
207 execvp(argv0, (char *const *)argv);
208 fatal("execvp %s", argv0);
211 /* daemon main routine */
215 int pipe_main2player[2];
218 log_init(debug, LOG_DAEMON);
219 log_setverbose(verbose);
220 log_procinit("main");
225 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
226 PF_UNSPEC, pipe_main2player) == -1)
229 player_pid = start_child(PROC_PLAYER, pipe_main2player[1]);
233 signal_set(&ev_sigint, SIGINT, main_sig_handler, NULL);
234 signal_set(&ev_sigterm, SIGTERM, main_sig_handler, NULL);
236 signal_add(&ev_sigint, NULL);
237 signal_add(&ev_sigterm, NULL);
239 signal(SIGHUP, SIG_IGN);
240 signal(SIGCHLD, SIG_IGN);
241 signal(SIGPIPE, SIG_IGN);
243 iev_player = xmalloc(sizeof(*iev_player));
244 imsg_init(&iev_player->ibuf, pipe_main2player[0]);
245 iev_player->handler = main_dispatch_player;
246 iev_player->events = EV_READ;
247 event_set(&iev_player->ev, iev_player->ibuf.fd, iev_player->events,
248 iev_player->handler, iev_player);
249 event_add(&iev_player->ev, NULL);
251 if ((control_fd = control_init(csock)) == -1)
252 fatal("control socket setup failed %s", csock);
253 control_listen(control_fd);
255 if (pledge("stdio rpath unix sendfd", NULL) == -1)
264 main(int argc, char **argv)
266 int ch, proc = PROC_MAIN;
268 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized */
275 while ((ch = getopt(argc, argv, "ds:T:v")) != -1) {
282 csock = xstrdup(optarg);
303 if (proc == PROC_PLAYER)
304 exit(player(debug, verbose));
307 xasprintf(&csock, "/tmp/amused-%d", getuid());
320 start_child(PROC_MAIN, -1);
324 imsg_event_add(struct imsgev *iev)
326 iev->events = EV_READ;
327 if (iev->ibuf.w.queued)
328 iev->events |= EV_WRITE;
331 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
332 event_add(&iev->ev, NULL);
336 imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
337 pid_t pid, int fd, const void *data, uint16_t datalen)
341 if ((ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data,
349 main_send_player(uint16_t type, int fd, const void *data, uint16_t datalen)
351 return imsg_compose_event(iev_player, type, 0, 0, fd, data, datalen);
355 main_play_song(const char *song)
358 char path[PATH_MAX] = { 0 };
361 strlcpy(path, song, sizeof(path));
362 if ((fd = open(path, O_RDONLY)) == -1) {
363 log_warn("open %s", path);
367 if (fstat(fd, &sb) == -1) {
368 log_warn("failed to stat %s", path);
373 if (S_ISDIR(sb.st_mode)) {
374 log_info("skipping a directory: %s", path);
379 play_state = STATE_PLAYING;
380 imsg_compose_event(iev_player, IMSG_PLAY, 0, 0, fd,
386 main_playlist_jump(struct imsgev *iev, struct imsg *imsg)
392 datalen = IMSG_DATA_SIZE(*imsg);
393 if (datalen != sizeof(arg)) {
394 main_senderr(iev, "wrong size");
398 memcpy(arg, imsg->data, sizeof(arg));
399 if (arg[sizeof(arg)-1] != '\0') {
400 main_senderr(iev, "data corrupted");
404 song = playlist_jump(arg);
406 main_senderr(iev, "not found");
410 main_send_player(IMSG_STOP, -1, NULL, 0);
411 if (!main_play_song(song)) {
412 main_senderr(iev, "can't play");
413 playlist_dropcurrent();
414 main_playlist_advance();
418 main_send_status(iev);
422 main_playlist_resume(void)
426 if ((song = current_song) == NULL)
427 song = playlist_advance();
429 for (; song != NULL; song = playlist_advance()) {
430 if (main_play_song(song))
433 playlist_dropcurrent();
438 main_playlist_advance(void)
443 song = playlist_advance();
447 if (main_play_song(song))
450 playlist_dropcurrent();
455 main_playlist_previous(void)
460 song = playlist_previous();
464 if (main_play_song(song))
467 playlist_dropcurrent();
472 main_restart_track(void)
480 if (main_play_song(song))
483 playlist_dropcurrent();
484 main_playlist_advance();
488 main_senderr(struct imsgev *iev, const char *msg)
490 imsg_compose_event(iev, IMSG_CTL_ERR, 0, 0, -1,
495 main_enqueue(int tx, struct playlist *px, struct imsgev *iev,
499 char path[PATH_MAX] = { 0 };
500 const char *err = NULL;
502 datalen = IMSG_DATA_SIZE(*imsg);
503 if (datalen != sizeof(path)) {
504 err = "data size mismatch";
508 memcpy(path, imsg->data, sizeof(path));
509 if (path[datalen-1] != '\0') {
510 err = "malformed data";
515 playlist_push(px, path);
517 playlist_enqueue(path);
518 imsg_compose_event(iev, IMSG_CTL_ADD, 0, 0, -1, path, sizeof(path));
521 main_senderr(iev, err);
525 main_send_playlist(struct imsgev *iev)
527 struct player_status s;
530 for (i = 0; i < playlist.len; ++i) {
531 memset(&s, 0, sizeof(s));
532 strlcpy(s.path, playlist.songs[i], sizeof(s.path));
533 s.status = play_off == i ? STATE_PLAYING : STATE_STOPPED;
534 imsg_compose_event(iev, IMSG_CTL_SHOW, 0, 0, -1, &s,
538 imsg_compose_event(iev, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
542 main_send_status(struct imsgev *iev)
544 struct player_status s;
546 memset(&s, 0, sizeof(s));
548 if (current_song != NULL)
549 strlcpy(s.path, current_song, sizeof(s.path));
550 s.status = play_state;
551 s.rp.repeat_all = repeat_all;
552 s.rp.repeat_one = repeat_one;
554 imsg_compose_event(iev, IMSG_CTL_STATUS, 0, 0, -1, &s, sizeof(s));