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 main_playlist_advance();
140 if (play_state == STATE_PLAYING)
141 control_notify(NULL, IMSG_CTL_NEXT);
143 control_notify(NULL, IMSG_CTL_STOP);
147 log_debug("%s: error handling imsg %d", __func__,
157 /* This pipe is dead. Remove its event handler. */
159 event_loopexit(NULL);
164 start_child(enum amused_process proc, int fd)
170 switch (pid = fork()) {
172 fatal("cannot fork");
181 if (fd != -1 && dup2(fd, 3) == -1)
182 fatal("cannot setup imsg fd");
183 } else if (fcntl(F_SETFD, 0) == -1)
184 fatal("cannot setup imsg fd");
186 argv[argc++] = argv0;
191 argv[argc++] = csock;
194 argv[argc++] = "-Tp";
204 /* obnoxious casts */
205 execvp(argv0, (char *const *)argv);
206 fatal("execvp %s", argv0);
209 /* daemon main routine */
213 int pipe_main2player[2];
216 log_init(debug, LOG_DAEMON);
217 log_setverbose(verbose);
218 log_procinit("main");
223 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
224 PF_UNSPEC, pipe_main2player) == -1)
227 player_pid = start_child(PROC_PLAYER, pipe_main2player[1]);
231 signal_set(&ev_sigint, SIGINT, main_sig_handler, NULL);
232 signal_set(&ev_sigterm, SIGTERM, main_sig_handler, NULL);
234 signal_add(&ev_sigint, NULL);
235 signal_add(&ev_sigterm, NULL);
237 signal(SIGHUP, SIG_IGN);
238 signal(SIGCHLD, SIG_IGN);
239 signal(SIGPIPE, SIG_IGN);
241 iev_player = xmalloc(sizeof(*iev_player));
242 imsg_init(&iev_player->ibuf, pipe_main2player[0]);
243 iev_player->handler = main_dispatch_player;
244 iev_player->events = EV_READ;
245 event_set(&iev_player->ev, iev_player->ibuf.fd, iev_player->events,
246 iev_player->handler, iev_player);
247 event_add(&iev_player->ev, NULL);
249 if ((control_fd = control_init(csock)) == -1)
250 fatal("control socket setup failed %s", csock);
251 control_listen(control_fd);
253 if (pledge("stdio rpath unix sendfd", NULL) == -1)
262 main(int argc, char **argv)
264 int ch, proc = PROC_MAIN;
266 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized */
273 while ((ch = getopt(argc, argv, "ds:T:v")) != -1) {
280 csock = xstrdup(optarg);
301 if (proc == PROC_PLAYER)
302 exit(player(debug, verbose));
305 xasprintf(&csock, "/tmp/amused-%d", getuid());
318 start_child(PROC_MAIN, -1);
322 imsg_event_add(struct imsgev *iev)
324 iev->events = EV_READ;
325 if (iev->ibuf.w.queued)
326 iev->events |= EV_WRITE;
329 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
330 event_add(&iev->ev, NULL);
334 imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
335 pid_t pid, int fd, const void *data, uint16_t datalen)
339 if ((ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data,
347 main_send_player(uint16_t type, int fd, const void *data, uint16_t datalen)
349 return imsg_compose_event(iev_player, type, 0, 0, fd, data, datalen);
353 main_play_song(const char *song)
355 char path[PATH_MAX] = { 0 };
358 strlcpy(path, song, sizeof(path));
359 if ((fd = open(path, O_RDONLY)) == -1) {
360 log_warn("open %s", path);
364 play_state = STATE_PLAYING;
365 imsg_compose_event(iev_player, IMSG_PLAY, 0, 0, fd,
371 main_playlist_jump(struct imsgev *iev, struct imsg *imsg)
377 datalen = IMSG_DATA_SIZE(*imsg);
378 if (datalen != sizeof(arg)) {
379 main_senderr(iev, "wrong size");
383 memcpy(arg, imsg->data, sizeof(arg));
384 if (arg[sizeof(arg)-1] != '\0') {
385 main_senderr(iev, "data corrupted");
389 song = playlist_jump(arg);
391 main_senderr(iev, "not found");
395 main_send_player(IMSG_STOP, -1, NULL, 0);
396 if (!main_play_song(song)) {
397 main_senderr(iev, "can't play");
398 playlist_dropcurrent();
399 main_playlist_advance();
403 main_send_status(iev);
407 main_playlist_resume(void)
411 if ((song = current_song) == NULL)
412 song = playlist_advance();
414 for (; song != NULL; song = playlist_advance()) {
415 if (main_play_song(song))
418 playlist_dropcurrent();
423 main_playlist_advance(void)
428 song = playlist_advance();
432 if (main_play_song(song))
435 playlist_dropcurrent();
440 main_playlist_previous(void)
445 song = playlist_previous();
449 if (main_play_song(song))
452 playlist_dropcurrent();
457 main_restart_track(void)
465 if (main_play_song(song))
468 playlist_dropcurrent();
469 main_playlist_advance();
473 main_senderr(struct imsgev *iev, const char *msg)
475 imsg_compose_event(iev, IMSG_CTL_ERR, 0, 0, -1,
480 main_enqueue(int tx, struct playlist *px, struct imsgev *iev,
484 char path[PATH_MAX] = { 0 };
485 const char *err = NULL;
487 datalen = IMSG_DATA_SIZE(*imsg);
488 if (datalen != sizeof(path)) {
489 err = "data size mismatch";
493 memcpy(path, imsg->data, sizeof(path));
494 if (path[datalen-1] != '\0') {
495 err = "malformed data";
500 playlist_push(px, path);
502 playlist_enqueue(path);
503 imsg_compose_event(iev, IMSG_CTL_ADD, 0, 0, -1, path, sizeof(path));
506 imsg_compose_event(iev, IMSG_CTL_ERR, 0, 0, -1, err, strlen(err)+1);
510 main_send_playlist(struct imsgev *iev)
512 struct player_status s;
515 for (i = 0; i < playlist.len; ++i) {
516 memset(&s, 0, sizeof(s));
517 strlcpy(s.path, playlist.songs[i], sizeof(s.path));
518 s.status = play_off == i ? STATE_PLAYING : STATE_STOPPED;
519 imsg_compose_event(iev, IMSG_CTL_SHOW, 0, 0, -1, &s,
523 imsg_compose_event(iev, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
527 main_send_status(struct imsgev *iev)
529 struct player_status s;
531 memset(&s, 0, sizeof(s));
533 if (current_song != NULL)
534 strlcpy(s.path, current_song, sizeof(s.path));
535 s.status = play_state;
536 s.rp.repeat_all = repeat_all;
537 s.rp.repeat_one = repeat_one;
539 imsg_compose_event(iev, IMSG_CTL_STATUS, 0, 0, -1, &s, sizeof(s));