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.
19 #include <sys/socket.h>
43 struct imsgev *iev_player;
47 struct event ev_sigint;
48 struct event ev_sigterm;
62 msgbuf_clear(&iev_player->ibuf.w);
63 close(iev_player->ibuf.fd);
66 log_debug("waiting for children to terminate");
70 if (errno != EINTR && errno != ECHILD)
72 } else if (WIFSIGNALED(status))
73 log_warnx("player terminated; signal %d",
75 } while (pid != -1 || (pid == -1 && errno == EINTR));
77 log_info("terminating");
82 main_sig_handler(int sig, short event, void *arg)
85 * Normal signal handler rules don't apply because libevent
95 fatalx("unexpected signal %d", sig);
100 main_dispatch_player(int sig, short event, void *d)
103 struct imsgev *iev = d;
104 struct imsgbuf *ibuf = &iev->ibuf;
110 if (event & EV_READ) {
111 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
112 fatal("imsg_read error");
113 if (n == 0) /* Connection closed */
116 if (event & EV_WRITE) {
117 if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
118 fatal("msgbuf_write");
119 if (n == 0) /* Connection closed */
124 if ((n = imsg_get(ibuf, &imsg)) == -1)
126 if (n == 0) /* No more messages. */
129 datalen = IMSG_DATA_SIZE(imsg);
130 switch (imsg.hdr.type) {
132 if (datalen != sizeof(current_position))
133 fatalx("IMSG_POS: got wrong size (%zu vs %zu)",
134 datalen, sizeof(current_position));
135 memcpy(¤t_position, imsg.data,
136 sizeof(current_position));
137 if (current_position < 0)
138 current_position = -1;
141 if (datalen != sizeof(current_duration))
142 fatalx("IMSG_LEN: got wrong size (%zu vs %zu)",
143 datalen, sizeof(current_duration));
144 memcpy(¤t_duration, imsg.data,
145 sizeof(current_duration));
146 if (current_duration < 0)
147 current_duration = -1;
151 errstr = "unknown error";
154 errstr[datalen-1] = '\0';
156 log_warnx("%s; skipping %s", errstr, current_song);
157 playlist_dropcurrent();
160 if (repeat_one && current_song != NULL) {
161 if (main_play_song(current_song))
163 playlist_dropcurrent();
165 main_playlist_advance();
166 if (play_state == STATE_PLAYING)
167 control_notify(IMSG_CTL_NEXT);
169 control_notify(IMSG_CTL_STOP);
172 log_debug("%s: error handling imsg %d", __func__,
182 /* This pipe is dead. Remove its event handler. */
184 event_loopexit(NULL);
189 start_child(enum amused_process proc, int fd)
195 if (fd == -1 && debug)
198 switch (pid = fork()) {
200 fatal("cannot fork");
209 if (fd != -1 && dup2(fd, 3) == -1)
210 fatal("cannot setup imsg fd");
211 } else if (fcntl(F_SETFD, 0) == -1)
212 fatal("cannot setup imsg fd");
215 argv[argc++] = argv0;
220 argv[argc++] = csock;
221 argv[argc++] = "-Tm";
224 argv[argc++] = "-Tp";
234 /* obnoxious casts */
235 execvp(argv0, (char *const *)argv);
236 fatal("execvp %s", argv0);
239 /* daemon main routine */
243 int pipe_main2player[2];
246 log_init(debug, LOG_DAEMON);
247 log_setverbose(verbose);
248 log_procinit("main");
253 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
254 PF_UNSPEC, pipe_main2player) == -1)
257 player_pid = start_child(PROC_PLAYER, pipe_main2player[1]);
261 signal_set(&ev_sigint, SIGINT, main_sig_handler, NULL);
262 signal_set(&ev_sigterm, SIGTERM, main_sig_handler, NULL);
264 signal_add(&ev_sigint, NULL);
265 signal_add(&ev_sigterm, NULL);
267 signal(SIGHUP, SIG_IGN);
268 signal(SIGCHLD, SIG_IGN);
269 signal(SIGPIPE, SIG_IGN);
271 iev_player = xmalloc(sizeof(*iev_player));
272 imsg_init(&iev_player->ibuf, pipe_main2player[0]);
273 iev_player->handler = main_dispatch_player;
274 iev_player->events = EV_READ;
275 event_set(&iev_player->ev, iev_player->ibuf.fd, iev_player->events,
276 iev_player->handler, iev_player);
277 event_add(&iev_player->ev, NULL);
279 if ((control_fd = control_init(csock)) == -1)
280 fatal("control socket setup failed %s", csock);
281 control_listen(control_fd);
283 if (pledge("stdio rpath unix sendfd", NULL) == -1)
292 main(int argc, char **argv)
296 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized */
303 while ((ch = getopt(argc, argv, "ds:T:v")) != -1) {
310 csock = xstrdup(optarg);
334 if (proc == PROC_MAIN)
336 if (proc == PROC_PLAYER)
337 exit(player(debug, verbose));
340 xasprintf(&csock, "/tmp/amused-%d", getuid());
351 start_child(PROC_MAIN, -1);
355 imsg_event_add(struct imsgev *iev)
357 iev->events = EV_READ;
358 if (iev->ibuf.w.queued)
359 iev->events |= EV_WRITE;
362 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
363 event_add(&iev->ev, NULL);
367 imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
368 pid_t pid, int fd, const void *data, uint16_t datalen)
372 if ((ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data,
380 main_send_player(uint16_t type, int fd, const void *data, size_t len)
382 return imsg_compose_event(iev_player, type, 0, 0, fd, data, len);
386 main_play_song(const char *path)
391 if ((fd = open(path, O_RDONLY)) == -1) {
392 log_warn("open %s", path);
396 if (fstat(fd, &sb) == -1) {
397 log_warn("failed to stat %s", path);
402 if (!S_ISREG(sb.st_mode)) {
403 log_info("skipping non-regular file: %s", path);
408 play_state = STATE_PLAYING;
409 main_send_player(IMSG_PLAY, fd, NULL, 0);
414 main_playlist_jump(struct imsgev *iev, struct imsg *imsg)
420 datalen = IMSG_DATA_SIZE(*imsg);
421 if (datalen != sizeof(arg)) {
422 main_senderr(iev, "wrong size");
426 memcpy(arg, imsg->data, sizeof(arg));
427 if (arg[sizeof(arg)-1] != '\0') {
428 main_senderr(iev, "data corrupted");
432 song = playlist_jump(arg);
434 main_senderr(iev, "not found");
438 main_send_player(IMSG_STOP, -1, NULL, 0);
439 if (!main_play_song(song)) {
440 main_senderr(iev, "can't play");
441 playlist_dropcurrent();
442 main_playlist_advance();
446 main_send_status(iev);
450 main_playlist_resume(void)
454 if ((song = current_song) == NULL)
455 song = playlist_advance();
457 for (; song != NULL; song = playlist_advance()) {
458 if (main_play_song(song))
461 playlist_dropcurrent();
466 main_playlist_advance(void)
471 song = playlist_advance();
475 if (main_play_song(song))
478 playlist_dropcurrent();
483 main_playlist_previous(void)
488 song = playlist_previous();
492 if (main_play_song(song))
495 playlist_dropcurrent();
500 main_senderr(struct imsgev *iev, const char *msg)
502 imsg_compose_event(iev, IMSG_CTL_ERR, 0, 0, -1,
507 main_enqueue(int tx, struct playlist *px, struct imsgev *iev,
512 const char *err = NULL;
514 datalen = IMSG_DATA_SIZE(*imsg);
515 if (datalen != sizeof(path)) {
516 err = "data size mismatch";
520 memcpy(path, imsg->data, sizeof(path));
521 if (path[datalen-1] != '\0') {
522 err = "malformed data";
527 playlist_push(px, path);
529 playlist_enqueue(path);
530 imsg_compose_event(iev, IMSG_CTL_ADD, 0, 0, -1, path, sizeof(path));
533 main_senderr(iev, err);
537 main_send_playlist(struct imsgev *iev)
539 struct player_status s;
542 for (i = 0; i < playlist.len; ++i) {
543 memset(&s, 0, sizeof(s));
544 strlcpy(s.path, playlist.songs[i], sizeof(s.path));
545 s.status = play_off == i ? STATE_PLAYING : STATE_STOPPED;
546 imsg_compose_event(iev, IMSG_CTL_SHOW, 0, 0, -1, &s,
550 imsg_compose_event(iev, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
554 main_send_status(struct imsgev *iev)
556 struct player_status s;
558 memset(&s, 0, sizeof(s));
560 if (current_song != NULL)
561 strlcpy(s.path, current_song, sizeof(s.path));
562 s.status = play_state;
563 s.position = current_position;
564 s.duration = current_duration;
565 s.rp.repeat_all = repeat_all;
566 s.rp.repeat_one = repeat_one;
568 imsg_compose_event(iev, IMSG_CTL_STATUS, 0, 0, -1, &s, sizeof(s));