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>
46 struct imsgev *iev_player;
50 struct event ev_sigint;
51 struct event ev_sigterm;
65 msgbuf_clear(&iev_player->ibuf.w);
66 close(iev_player->ibuf.fd);
69 log_debug("waiting for children to terminate");
73 if (errno != EINTR && errno != ECHILD)
75 } else if (WIFSIGNALED(status))
76 log_warnx("player terminated; signal %d",
78 } while (pid != -1 || (pid == -1 && errno == EINTR));
80 log_info("terminating");
85 main_sig_handler(int sig, short event, void *arg)
88 * Normal signal handler rules don't apply because libevent
98 fatalx("unexpected signal %d", sig);
103 main_dispatch_player(int sig, short event, void *d)
106 struct imsgev *iev = d;
107 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 datalen = IMSG_DATA_SIZE(imsg);
133 switch (imsg.hdr.type) {
135 if (datalen != sizeof(current_position))
136 fatalx("IMSG_POS: got wrong size (%zu vs %zu)",
137 datalen, sizeof(current_position));
138 memcpy(¤t_position, imsg.data,
139 sizeof(current_position));
140 if (current_position < 0)
141 current_position = -1;
144 if (datalen != sizeof(current_duration))
145 fatalx("IMSG_LEN: got wrong size (%zu vs %zu)",
146 datalen, sizeof(current_duration));
147 memcpy(¤t_duration, imsg.data,
148 sizeof(current_duration));
149 if (current_duration < 0)
150 current_duration = -1;
154 errstr = "unknown error";
157 errstr[datalen-1] = '\0';
159 log_warnx("%s; skipping %s", errstr, current_song);
160 playlist_dropcurrent();
163 if (repeat_one && current_song != NULL) {
164 if (main_play_song(current_song))
166 playlist_dropcurrent();
168 main_playlist_advance();
169 if (play_state == STATE_PLAYING)
170 control_notify(NULL, IMSG_CTL_NEXT);
172 control_notify(NULL, IMSG_CTL_STOP);
175 log_debug("%s: error handling imsg %d", __func__,
185 /* This pipe is dead. Remove its event handler. */
187 event_loopexit(NULL);
192 start_child(enum amused_process proc, int fd)
198 if (fd == -1 && debug)
201 switch (pid = fork()) {
203 fatal("cannot fork");
212 if (fd != -1 && dup2(fd, 3) == -1)
213 fatal("cannot setup imsg fd");
214 } else if (fcntl(F_SETFD, 0) == -1)
215 fatal("cannot setup imsg fd");
218 argv[argc++] = argv0;
223 argv[argc++] = csock;
224 argv[argc++] = "-Tm";
227 argv[argc++] = "-Tp";
237 /* obnoxious casts */
238 execvp(argv0, (char *const *)argv);
239 fatal("execvp %s", argv0);
242 /* daemon main routine */
246 int pipe_main2player[2];
249 log_init(debug, LOG_DAEMON);
250 log_setverbose(verbose);
251 log_procinit("main");
256 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
257 PF_UNSPEC, pipe_main2player) == -1)
260 player_pid = start_child(PROC_PLAYER, pipe_main2player[1]);
264 signal_set(&ev_sigint, SIGINT, main_sig_handler, NULL);
265 signal_set(&ev_sigterm, SIGTERM, main_sig_handler, NULL);
267 signal_add(&ev_sigint, NULL);
268 signal_add(&ev_sigterm, NULL);
270 signal(SIGHUP, SIG_IGN);
271 signal(SIGCHLD, SIG_IGN);
272 signal(SIGPIPE, SIG_IGN);
274 iev_player = xmalloc(sizeof(*iev_player));
275 imsg_init(&iev_player->ibuf, pipe_main2player[0]);
276 iev_player->handler = main_dispatch_player;
277 iev_player->events = EV_READ;
278 event_set(&iev_player->ev, iev_player->ibuf.fd, iev_player->events,
279 iev_player->handler, iev_player);
280 event_add(&iev_player->ev, NULL);
282 if ((control_fd = control_init(csock)) == -1)
283 fatal("control socket setup failed %s", csock);
284 control_listen(control_fd);
286 if (pledge("stdio rpath unix sendfd", NULL) == -1)
295 main(int argc, char **argv)
299 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized */
306 while ((ch = getopt(argc, argv, "ds:T:v")) != -1) {
313 csock = xstrdup(optarg);
337 if (proc == PROC_MAIN)
339 if (proc == PROC_PLAYER)
340 exit(player(debug, verbose));
343 xasprintf(&csock, "/tmp/amused-%d", getuid());
354 start_child(PROC_MAIN, -1);
358 imsg_event_add(struct imsgev *iev)
360 iev->events = EV_READ;
361 if (iev->ibuf.w.queued)
362 iev->events |= EV_WRITE;
365 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
366 event_add(&iev->ev, NULL);
370 imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
371 pid_t pid, int fd, const void *data, uint16_t datalen)
375 if ((ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data,
383 main_send_player(uint16_t type, int fd)
385 return imsg_compose_event(iev_player, type, 0, 0, fd, NULL, 0);
389 main_play_song(const char *path)
394 if ((fd = open(path, O_RDONLY)) == -1) {
395 log_warn("open %s", path);
399 if (fstat(fd, &sb) == -1) {
400 log_warn("failed to stat %s", path);
405 if (!S_ISREG(sb.st_mode)) {
406 log_info("skipping non-regular file: %s", path);
411 play_state = STATE_PLAYING;
412 main_send_player(IMSG_PLAY, fd);
417 main_playlist_jump(struct imsgev *iev, struct imsg *imsg)
423 datalen = IMSG_DATA_SIZE(*imsg);
424 if (datalen != sizeof(arg)) {
425 main_senderr(iev, "wrong size");
429 memcpy(arg, imsg->data, sizeof(arg));
430 if (arg[sizeof(arg)-1] != '\0') {
431 main_senderr(iev, "data corrupted");
435 song = playlist_jump(arg);
437 main_senderr(iev, "not found");
441 main_send_player(IMSG_STOP, -1);
442 if (!main_play_song(song)) {
443 main_senderr(iev, "can't play");
444 playlist_dropcurrent();
445 main_playlist_advance();
449 main_send_status(iev);
453 main_playlist_resume(void)
457 if ((song = current_song) == NULL)
458 song = playlist_advance();
460 for (; song != NULL; song = playlist_advance()) {
461 if (main_play_song(song))
464 playlist_dropcurrent();
469 main_playlist_advance(void)
474 song = playlist_advance();
478 if (main_play_song(song))
481 playlist_dropcurrent();
486 main_playlist_previous(void)
491 song = playlist_previous();
495 if (main_play_song(song))
498 playlist_dropcurrent();
503 main_restart_track(void)
505 if (current_song == NULL)
508 if (main_play_song(current_song))
511 playlist_dropcurrent();
512 main_playlist_advance();
516 main_senderr(struct imsgev *iev, const char *msg)
518 imsg_compose_event(iev, IMSG_CTL_ERR, 0, 0, -1,
523 main_enqueue(int tx, struct playlist *px, struct imsgev *iev,
528 const char *err = NULL;
530 datalen = IMSG_DATA_SIZE(*imsg);
531 if (datalen != sizeof(path)) {
532 err = "data size mismatch";
536 memcpy(path, imsg->data, sizeof(path));
537 if (path[datalen-1] != '\0') {
538 err = "malformed data";
543 playlist_push(px, path);
545 playlist_enqueue(path);
546 imsg_compose_event(iev, IMSG_CTL_ADD, 0, 0, -1, path, sizeof(path));
549 main_senderr(iev, err);
553 main_send_playlist(struct imsgev *iev)
555 struct player_status s;
558 for (i = 0; i < playlist.len; ++i) {
559 memset(&s, 0, sizeof(s));
560 strlcpy(s.path, playlist.songs[i], sizeof(s.path));
561 s.status = play_off == i ? STATE_PLAYING : STATE_STOPPED;
562 imsg_compose_event(iev, IMSG_CTL_SHOW, 0, 0, -1, &s,
566 imsg_compose_event(iev, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
570 main_send_status(struct imsgev *iev)
572 struct player_status s;
574 memset(&s, 0, sizeof(s));
576 if (current_song != NULL)
577 strlcpy(s.path, current_song, sizeof(s.path));
578 s.status = play_state;
579 s.position = current_position;
580 s.duration = current_duration;
581 s.rp.repeat_all = repeat_all;
582 s.rp.repeat_one = repeat_one;
584 imsg_compose_event(iev, IMSG_CTL_STATUS, 0, 0, -1, &s, sizeof(s));