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;
53 struct event ev_siginfo;
67 msgbuf_clear(&iev_player->ibuf.w);
68 close(iev_player->ibuf.fd);
71 log_debug("waiting for children to terminate");
75 if (errno != EINTR && errno != ECHILD)
77 } else if (WIFSIGNALED(status))
78 log_warnx("player terminated; signal %d",
80 } while (pid != -1 || (pid == -1 && errno == EINTR));
82 log_info("terminating");
93 log_info("status: stopped");
96 log_info("status: playing");
99 log_info("status: paused");
102 log_info("status: unknown");
106 if ((cur = playlist_current()) != NULL)
107 log_info("playing %s", cur);
109 log_info("not playing anything");
113 main_sig_handler(int sig, short event, void *arg)
116 * Normal signal handler rules don't apply because libevent
129 fatalx("unexpected signal %d", sig);
134 main_dispatch_player(int sig, short event, void *d)
136 struct imsgev *iev = d;
137 struct imsgbuf *ibuf = &iev->ibuf;
142 if (event & EV_READ) {
143 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
144 fatal("imsg_read error");
145 if (n == 0) /* Connection closed */
148 if (event & EV_WRITE) {
149 if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
150 fatal("msgbuf_write");
151 if (n == 0) /* Connection closed */
156 if ((n = imsg_get(ibuf, &imsg)) == -1)
158 if (n == 0) /* No more messages. */
161 switch (imsg.hdr.type) {
163 playlist_dropcurrent();
166 main_playlist_advance();
170 log_debug("%s: error handling imsg %d", __func__,
180 /* This pipe is dead. Remove its event handler. */
182 event_loopexit(NULL);
187 start_child(enum amused_process proc, int fd)
193 switch (pid = fork()) {
195 fatal("cannot fork");
204 if (fd != -1 && dup2(fd, 3) == -1)
205 fatal("cannot setup imsg fd");
206 } else if (fcntl(F_SETFD, 0) == -1)
207 fatal("cannot setup imsg fd");
209 argv[argc++] = argv0;
214 argv[argc++] = csock;
217 argv[argc++] = "-Tp";
227 /* obnoxious casts */
228 execvp(argv0, (char *const *)argv);
229 fatal("execvp %s", argv0);
232 /* daemon main routine */
236 int pipe_main2player[2];
239 log_init(debug, LOG_DAEMON);
240 log_setverbose(verbose);
241 log_procinit("main");
246 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
247 PF_UNSPEC, pipe_main2player) == -1)
250 player_pid = start_child(PROC_PLAYER, pipe_main2player[1]);
254 signal_set(&ev_sigint, SIGINT, main_sig_handler, NULL);
255 signal_set(&ev_sigterm, SIGTERM, main_sig_handler, NULL);
256 signal_set(&ev_siginfo, SIGINFO, main_sig_handler, NULL);
258 signal_add(&ev_sigint, NULL);
259 signal_add(&ev_sigterm, NULL);
260 signal_add(&ev_siginfo, NULL);
262 signal(SIGHUP, SIG_IGN);
263 signal(SIGCHLD, SIG_IGN);
264 signal(SIGPIPE, SIG_IGN);
266 iev_player = xmalloc(sizeof(*iev_player));
267 imsg_init(&iev_player->ibuf, pipe_main2player[0]);
268 iev_player->handler = main_dispatch_player;
269 iev_player->events = EV_READ;
270 event_set(&iev_player->ev, iev_player->ibuf.fd, iev_player->events,
271 iev_player->handler, iev_player);
272 event_add(&iev_player->ev, NULL);
274 if ((control_fd = control_init(csock)) == -1)
275 fatal("control socket setup failed %s", csock);
276 control_listen(control_fd);
278 if (pledge("stdio rpath unix sendfd", NULL) == -1)
287 main(int argc, char **argv)
289 int ch, proc = PROC_MAIN;
291 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized */
298 while ((ch = getopt(argc, argv, "ds:T:vV")) != -1) {
305 csock = xstrdup(optarg);
320 printf("%s version %s\n", getprogname(),
330 if (proc == PROC_PLAYER)
331 exit(player(debug, verbose));
334 xasprintf(&csock, "/tmp/amused-%d", getuid());
347 start_child(PROC_MAIN, -1);
351 imsg_event_add(struct imsgev *iev)
353 iev->events = EV_READ;
354 if (iev->ibuf.w.queued)
355 iev->events |= EV_WRITE;
358 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
359 event_add(&iev->ev, NULL);
363 imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
364 pid_t pid, int fd, const void *data, uint16_t datalen)
368 if ((ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data,
376 main_send_player(uint16_t type, int fd, const void *data, uint16_t datalen)
378 return imsg_compose_event(iev_player, type, 0, 0, fd, data, datalen);
382 main_play_song(const char *song)
384 char path[PATH_MAX] = { 0 };
387 strlcpy(path, song, sizeof(path));
388 if ((fd = open(path, O_RDONLY)) == -1) {
389 log_warn("open %s", path);
393 play_state = STATE_PLAYING;
394 imsg_compose_event(iev_player, IMSG_PLAY, 0, 0, fd,
400 main_playlist_jump(struct imsgev *iev, struct imsg *imsg)
406 datalen = IMSG_DATA_SIZE(*imsg);
407 if (datalen != sizeof(arg)) {
408 main_senderr(iev, "wrong size");
412 memcpy(arg, imsg->data, sizeof(arg));
413 if (arg[sizeof(arg)-1] != '\0') {
414 main_senderr(iev, "data corrupted");
418 song = playlist_jump(arg);
420 main_senderr(iev, "not found");
424 main_send_player(IMSG_STOP, -1, NULL, 0);
425 if (!main_play_song(song)) {
426 main_senderr(iev, "can't play");
427 playlist_dropcurrent();
428 main_playlist_advance();
432 main_send_status(iev);
436 main_playlist_resume(void)
440 if ((song = playlist_current()) == NULL)
441 song = playlist_advance();
443 for (; song != NULL; song = playlist_advance()) {
444 if (main_play_song(song))
447 playlist_dropcurrent();
452 main_playlist_advance(void)
457 song = playlist_advance();
461 if (main_play_song(song))
464 playlist_dropcurrent();
469 main_playlist_previous(void)
474 song = playlist_previous();
478 if (main_play_song(song))
481 playlist_dropcurrent();
486 main_restart_track(void)
490 song = playlist_current();
494 if (main_play_song(song))
497 playlist_dropcurrent();
498 main_playlist_advance();
502 main_senderr(struct imsgev *iev, const char *msg)
504 imsg_compose_event(iev, IMSG_CTL_ERR, 0, 0, -1,
509 main_enqueue(int tx, struct playlist *px, struct imsgev *iev,
513 char path[PATH_MAX] = { 0 };
514 const char *err = NULL;
516 datalen = IMSG_DATA_SIZE(*imsg);
517 if (datalen != sizeof(path)) {
518 err = "data size mismatch";
522 memcpy(path, imsg->data, sizeof(path));
523 if (path[datalen-1] != '\0') {
524 err = "malformed data";
529 playlist_push(px, path);
531 playlist_enqueue(path);
532 imsg_compose_event(iev, IMSG_CTL_ADD, 0, 0, -1, path, sizeof(path));
535 imsg_compose_event(iev, IMSG_CTL_ERR, 0, 0, -1, err, strlen(err)+1);
539 main_send_playlist(struct imsgev *iev)
541 struct player_status s;
544 for (i = 0; i < playlist.len; ++i) {
545 memset(&s, 0, sizeof(s));
546 strlcpy(s.path, playlist.songs[i], sizeof(s.path));
547 s.status = play_off == i ? STATE_PLAYING : STATE_STOPPED;
548 imsg_compose_event(iev, IMSG_CTL_SHOW, 0, 0, -1, &s,
552 imsg_compose_event(iev, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
556 main_send_status(struct imsgev *iev)
558 struct player_status s;
561 memset(&s, 0, sizeof(s));
563 song = playlist_current();
565 strlcpy(s.path, song, sizeof(s.path));
566 s.status = play_state;
568 imsg_compose_event(iev, IMSG_CTL_STATUS, 0, 0, -1, &s, sizeof(s));