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>
47 struct imsgev *iev_player;
51 struct event ev_sigint;
52 struct event ev_sigterm;
66 msgbuf_clear(&iev_player->ibuf.w);
67 close(iev_player->ibuf.fd);
70 log_debug("waiting for children to terminate");
74 if (errno != EINTR && errno != ECHILD)
76 } else if (WIFSIGNALED(status))
77 log_warnx("player terminated; signal %d",
79 } while (pid != -1 || (pid == -1 && errno == EINTR));
81 log_info("terminating");
86 main_sig_handler(int sig, short event, void *arg)
89 * Normal signal handler rules don't apply because libevent
99 fatalx("unexpected signal %d", sig);
104 main_dispatch_player(int sig, short event, void *d)
106 struct imsgev *iev = d;
107 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 switch (imsg.hdr.type) {
133 playlist_dropcurrent();
136 if (repeat_one && current_song != NULL) {
137 if (main_play_song(current_song))
139 playlist_dropcurrent();
141 main_playlist_advance();
142 if (play_state == STATE_PLAYING)
143 control_notify(NULL, IMSG_CTL_NEXT);
145 control_notify(NULL, IMSG_CTL_STOP);
148 log_debug("%s: error handling imsg %d", __func__,
158 /* This pipe is dead. Remove its event handler. */
160 event_loopexit(NULL);
165 start_child(enum amused_process proc, int fd)
171 switch (pid = fork()) {
173 fatal("cannot fork");
182 if (fd != -1 && dup2(fd, 3) == -1)
183 fatal("cannot setup imsg fd");
184 } else if (fcntl(F_SETFD, 0) == -1)
185 fatal("cannot setup imsg fd");
187 argv[argc++] = argv0;
192 argv[argc++] = csock;
195 argv[argc++] = "-Tp";
205 /* obnoxious casts */
206 execvp(argv0, (char *const *)argv);
207 fatal("execvp %s", argv0);
210 /* daemon main routine */
214 int pipe_main2player[2];
217 log_init(debug, LOG_DAEMON);
218 log_setverbose(verbose);
219 log_procinit("main");
224 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
225 PF_UNSPEC, pipe_main2player) == -1)
228 player_pid = start_child(PROC_PLAYER, pipe_main2player[1]);
232 signal_set(&ev_sigint, SIGINT, main_sig_handler, NULL);
233 signal_set(&ev_sigterm, SIGTERM, main_sig_handler, NULL);
235 signal_add(&ev_sigint, NULL);
236 signal_add(&ev_sigterm, NULL);
238 signal(SIGHUP, SIG_IGN);
239 signal(SIGCHLD, SIG_IGN);
240 signal(SIGPIPE, SIG_IGN);
242 iev_player = xmalloc(sizeof(*iev_player));
243 imsg_init(&iev_player->ibuf, pipe_main2player[0]);
244 iev_player->handler = main_dispatch_player;
245 iev_player->events = EV_READ;
246 event_set(&iev_player->ev, iev_player->ibuf.fd, iev_player->events,
247 iev_player->handler, iev_player);
248 event_add(&iev_player->ev, NULL);
250 if ((control_fd = control_init(csock)) == -1)
251 fatal("control socket setup failed %s", csock);
252 control_listen(control_fd);
254 if (pledge("stdio rpath unix sendfd", NULL) == -1)
263 main(int argc, char **argv)
265 int ch, proc = PROC_MAIN;
267 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized */
274 while ((ch = getopt(argc, argv, "ds:T:v")) != -1) {
281 csock = xstrdup(optarg);
302 if (proc == PROC_PLAYER)
303 exit(player(debug, verbose));
306 xasprintf(&csock, "/tmp/amused-%d", getuid());
319 start_child(PROC_MAIN, -1);
323 imsg_event_add(struct imsgev *iev)
325 iev->events = EV_READ;
326 if (iev->ibuf.w.queued)
327 iev->events |= EV_WRITE;
330 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
331 event_add(&iev->ev, NULL);
335 imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
336 pid_t pid, int fd, const void *data, uint16_t datalen)
340 if ((ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data,
348 main_send_player(uint16_t type, int fd, const void *data, uint16_t datalen)
350 return imsg_compose_event(iev_player, type, 0, 0, fd, data, datalen);
354 main_play_song(const char *song)
356 char path[PATH_MAX] = { 0 };
359 strlcpy(path, song, sizeof(path));
360 if ((fd = open(path, O_RDONLY)) == -1) {
361 log_warn("open %s", path);
365 play_state = STATE_PLAYING;
366 imsg_compose_event(iev_player, IMSG_PLAY, 0, 0, fd,
372 main_playlist_jump(struct imsgev *iev, struct imsg *imsg)
378 datalen = IMSG_DATA_SIZE(*imsg);
379 if (datalen != sizeof(arg)) {
380 main_senderr(iev, "wrong size");
384 memcpy(arg, imsg->data, sizeof(arg));
385 if (arg[sizeof(arg)-1] != '\0') {
386 main_senderr(iev, "data corrupted");
390 song = playlist_jump(arg);
392 main_senderr(iev, "not found");
396 main_send_player(IMSG_STOP, -1, NULL, 0);
397 if (!main_play_song(song)) {
398 main_senderr(iev, "can't play");
399 playlist_dropcurrent();
400 main_playlist_advance();
404 main_send_status(iev);
408 main_playlist_resume(void)
412 if ((song = current_song) == NULL)
413 song = playlist_advance();
415 for (; song != NULL; song = playlist_advance()) {
416 if (main_play_song(song))
419 playlist_dropcurrent();
424 main_playlist_advance(void)
429 song = playlist_advance();
433 if (main_play_song(song))
436 playlist_dropcurrent();
441 main_playlist_previous(void)
446 song = playlist_previous();
450 if (main_play_song(song))
453 playlist_dropcurrent();
458 main_restart_track(void)
466 if (main_play_song(song))
469 playlist_dropcurrent();
470 main_playlist_advance();
474 main_senderr(struct imsgev *iev, const char *msg)
476 imsg_compose_event(iev, IMSG_CTL_ERR, 0, 0, -1,
481 main_enqueue(int tx, struct playlist *px, struct imsgev *iev,
485 char path[PATH_MAX] = { 0 };
486 const char *err = NULL;
488 datalen = IMSG_DATA_SIZE(*imsg);
489 if (datalen != sizeof(path)) {
490 err = "data size mismatch";
494 memcpy(path, imsg->data, sizeof(path));
495 if (path[datalen-1] != '\0') {
496 err = "malformed data";
501 playlist_push(px, path);
503 playlist_enqueue(path);
504 imsg_compose_event(iev, IMSG_CTL_ADD, 0, 0, -1, path, sizeof(path));
507 imsg_compose_event(iev, IMSG_CTL_ERR, 0, 0, -1, err, strlen(err)+1);
511 main_send_playlist(struct imsgev *iev)
513 struct player_status s;
516 for (i = 0; i < playlist.len; ++i) {
517 memset(&s, 0, sizeof(s));
518 strlcpy(s.path, playlist.songs[i], sizeof(s.path));
519 s.status = play_off == i ? STATE_PLAYING : STATE_STOPPED;
520 imsg_compose_event(iev, IMSG_CTL_SHOW, 0, 0, -1, &s,
524 imsg_compose_event(iev, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
528 main_send_status(struct imsgev *iev)
530 struct player_status s;
532 memset(&s, 0, sizeof(s));
534 if (current_song != NULL)
535 strlcpy(s.path, current_song, sizeof(s.path));
536 s.status = play_state;
537 s.rp.repeat_all = repeat_all;
538 s.rp.repeat_one = repeat_one;
540 imsg_compose_event(iev, IMSG_CTL_STATUS, 0, 0, -1, &s, sizeof(s));