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();
134 main_playlist_advance();
135 if (play_state == STATE_PLAYING)
136 control_notify(NULL, IMSG_CTL_NEXT);
138 control_notify(NULL, IMSG_CTL_STOP);
141 if (repeat_one && current_song != NULL)
142 if (main_play_song(current_song))
144 main_playlist_advance();
145 if (play_state == STATE_PLAYING)
146 control_notify(NULL, IMSG_CTL_NEXT);
148 control_notify(NULL, IMSG_CTL_STOP);
152 log_debug("%s: error handling imsg %d", __func__,
162 /* This pipe is dead. Remove its event handler. */
164 event_loopexit(NULL);
169 start_child(enum amused_process proc, int fd)
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");
191 argv[argc++] = argv0;
196 argv[argc++] = csock;
199 argv[argc++] = "-Tp";
209 /* obnoxious casts */
210 execvp(argv0, (char *const *)argv);
211 fatal("execvp %s", argv0);
214 /* daemon main routine */
218 int pipe_main2player[2];
221 log_init(debug, LOG_DAEMON);
222 log_setverbose(verbose);
223 log_procinit("main");
228 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
229 PF_UNSPEC, pipe_main2player) == -1)
232 player_pid = start_child(PROC_PLAYER, pipe_main2player[1]);
236 signal_set(&ev_sigint, SIGINT, main_sig_handler, NULL);
237 signal_set(&ev_sigterm, SIGTERM, main_sig_handler, NULL);
239 signal_add(&ev_sigint, NULL);
240 signal_add(&ev_sigterm, NULL);
242 signal(SIGHUP, SIG_IGN);
243 signal(SIGCHLD, SIG_IGN);
244 signal(SIGPIPE, SIG_IGN);
246 iev_player = xmalloc(sizeof(*iev_player));
247 imsg_init(&iev_player->ibuf, pipe_main2player[0]);
248 iev_player->handler = main_dispatch_player;
249 iev_player->events = EV_READ;
250 event_set(&iev_player->ev, iev_player->ibuf.fd, iev_player->events,
251 iev_player->handler, iev_player);
252 event_add(&iev_player->ev, NULL);
254 if ((control_fd = control_init(csock)) == -1)
255 fatal("control socket setup failed %s", csock);
256 control_listen(control_fd);
258 if (pledge("stdio rpath unix sendfd", NULL) == -1)
267 main(int argc, char **argv)
269 int ch, proc = PROC_MAIN;
271 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized */
278 while ((ch = getopt(argc, argv, "ds:T:v")) != -1) {
285 csock = xstrdup(optarg);
306 if (proc == PROC_PLAYER)
307 exit(player(debug, verbose));
310 xasprintf(&csock, "/tmp/amused-%d", getuid());
323 start_child(PROC_MAIN, -1);
327 imsg_event_add(struct imsgev *iev)
329 iev->events = EV_READ;
330 if (iev->ibuf.w.queued)
331 iev->events |= EV_WRITE;
334 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
335 event_add(&iev->ev, NULL);
339 imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
340 pid_t pid, int fd, const void *data, uint16_t datalen)
344 if ((ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data,
352 main_send_player(uint16_t type, int fd, const void *data, uint16_t datalen)
354 return imsg_compose_event(iev_player, type, 0, 0, fd, data, datalen);
358 main_play_song(const char *song)
360 char path[PATH_MAX] = { 0 };
363 strlcpy(path, song, sizeof(path));
364 if ((fd = open(path, O_RDONLY)) == -1) {
365 log_warn("open %s", path);
369 play_state = STATE_PLAYING;
370 imsg_compose_event(iev_player, IMSG_PLAY, 0, 0, fd,
376 main_playlist_jump(struct imsgev *iev, struct imsg *imsg)
382 datalen = IMSG_DATA_SIZE(*imsg);
383 if (datalen != sizeof(arg)) {
384 main_senderr(iev, "wrong size");
388 memcpy(arg, imsg->data, sizeof(arg));
389 if (arg[sizeof(arg)-1] != '\0') {
390 main_senderr(iev, "data corrupted");
394 song = playlist_jump(arg);
396 main_senderr(iev, "not found");
400 main_send_player(IMSG_STOP, -1, NULL, 0);
401 if (!main_play_song(song)) {
402 main_senderr(iev, "can't play");
403 playlist_dropcurrent();
404 main_playlist_advance();
408 main_send_status(iev);
412 main_playlist_resume(void)
416 if ((song = current_song) == NULL)
417 song = playlist_advance();
419 for (; song != NULL; song = playlist_advance()) {
420 if (main_play_song(song))
423 playlist_dropcurrent();
428 main_playlist_advance(void)
433 song = playlist_advance();
437 if (main_play_song(song))
440 playlist_dropcurrent();
445 main_playlist_previous(void)
450 song = playlist_previous();
454 if (main_play_song(song))
457 playlist_dropcurrent();
462 main_restart_track(void)
470 if (main_play_song(song))
473 playlist_dropcurrent();
474 main_playlist_advance();
478 main_senderr(struct imsgev *iev, const char *msg)
480 imsg_compose_event(iev, IMSG_CTL_ERR, 0, 0, -1,
485 main_enqueue(int tx, struct playlist *px, struct imsgev *iev,
489 char path[PATH_MAX] = { 0 };
490 const char *err = NULL;
492 datalen = IMSG_DATA_SIZE(*imsg);
493 if (datalen != sizeof(path)) {
494 err = "data size mismatch";
498 memcpy(path, imsg->data, sizeof(path));
499 if (path[datalen-1] != '\0') {
500 err = "malformed data";
505 playlist_push(px, path);
507 playlist_enqueue(path);
508 imsg_compose_event(iev, IMSG_CTL_ADD, 0, 0, -1, path, sizeof(path));
511 imsg_compose_event(iev, IMSG_CTL_ERR, 0, 0, -1, err, strlen(err)+1);
515 main_send_playlist(struct imsgev *iev)
517 struct player_status s;
520 for (i = 0; i < playlist.len; ++i) {
521 memset(&s, 0, sizeof(s));
522 strlcpy(s.path, playlist.songs[i], sizeof(s.path));
523 s.status = play_off == i ? STATE_PLAYING : STATE_STOPPED;
524 imsg_compose_event(iev, IMSG_CTL_SHOW, 0, 0, -1, &s,
528 imsg_compose_event(iev, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
532 main_send_status(struct imsgev *iev)
534 struct player_status s;
536 memset(&s, 0, sizeof(s));
538 if (current_song != NULL)
539 strlcpy(s.path, current_song, sizeof(s.path));
540 s.status = play_state;
541 s.rp.repeat_all = repeat_all;
542 s.rp.repeat_one = repeat_one;
544 imsg_compose_event(iev, IMSG_CTL_STATUS, 0, 0, -1, &s, sizeof(s));