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;
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 switch (imsg.hdr.type) {
135 song = playlist_advance();
138 /* XXX: watch out for failures! */
139 main_play_song(song);
142 log_debug("%s: error handling imsg %d", __func__,
152 /* This pipe is dead. Remove its event handler. */
154 event_loopexit(NULL);
159 start_child(enum amused_process proc, int fd)
165 switch (pid = fork()) {
167 fatal("cannot fork");
176 if (dup2(fd, 3) == -1)
177 fatal("cannot setup imsg fd");
178 } else if (fcntl(F_SETFD, 0) == -1)
179 fatal("cannot setup imsg fd");
181 argv[argc++] = argv0;
184 fatal("can not start main process");
186 argv[argc++] = "-Tp";
196 /* obnoxious casts */
197 execvp(argv0, (char *const *)argv);
198 fatal("execvp %s", argv0);
201 /* daemon main routine */
205 int pipe_main2player[2];
208 log_init(debug, LOG_DAEMON);
209 log_setverbose(verbose);
210 log_procinit("main");
215 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
216 PF_UNSPEC, pipe_main2player) == -1)
219 player_pid = start_child(PROC_PLAYER, pipe_main2player[1]);
223 signal_set(&ev_sigint, SIGINT, main_sig_handler, NULL);
224 signal_set(&ev_sigterm, SIGTERM, main_sig_handler, NULL);
226 signal_add(&ev_sigint, NULL);
227 signal_add(&ev_sigterm, NULL);
229 signal(SIGHUP, SIG_IGN);
230 signal(SIGCHLD, SIG_IGN);
231 signal(SIGPIPE, SIG_IGN);
233 iev_player = xmalloc(sizeof(*iev_player));
234 imsg_init(&iev_player->ibuf, pipe_main2player[0]);
235 iev_player->handler = main_dispatch_player;
236 iev_player->events = EV_READ;
237 event_set(&iev_player->ev, iev_player->ibuf.fd, iev_player->events,
238 iev_player->handler, iev_player);
239 event_add(&iev_player->ev, NULL);
241 if ((control_fd = control_init(csock)) == -1)
242 fatal("control socket setup failed %s", csock);
243 control_listen(control_fd);
245 if (pledge("stdio rpath unix sendfd", NULL) == -1)
254 main(int argc, char **argv)
256 int ch, proc = PROC_MAIN;
258 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized */
265 while ((ch = getopt(argc, argv, "ds:T:vV")) != -1) {
272 csock = xstrdup(optarg);
287 printf("%s version %s\n", getprogname(),
297 if (proc == PROC_PLAYER)
298 exit(player(debug, verbose));
301 xasprintf(&csock, "/tmp/amused-%d", getuid());
311 imsg_event_add(struct imsgev *iev)
313 iev->events = EV_READ;
314 if (iev->ibuf.w.queued)
315 iev->events |= EV_WRITE;
318 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
319 event_add(&iev->ev, NULL);
323 imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
324 pid_t pid, int fd, const void *data, uint16_t datalen)
328 if ((ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data,
336 main_play_song(const char *song)
338 char path[PATH_MAX] = { 0 };
341 strlcpy(path, song, sizeof(path));
342 if ((fd = open(path, O_RDONLY)) == -1) {
344 log_warn("open %s", path);
347 fatal("open %s", path);
351 imsg_compose_event(iev_player, IMSG_PLAY, 0, 0, fd,
357 main_enqueue(struct imsgev *iev, struct imsg *imsg)
360 char path[PATH_MAX] = { 0 };
361 const char *err = NULL;
363 datalen = IMSG_DATA_SIZE(*imsg);
364 if (datalen != sizeof(path)) {
365 err = "data size mismatch";
369 memcpy(path, imsg->data, sizeof(path));
370 if (path[datalen-1] != '\0') {
371 err = "malformed data";
375 playlist_enqueue(path);
376 imsg_compose_event(iev, IMSG_CTL_ADD, 0, 0, -1, path, sizeof(path));
379 imsg_compose_event(iev, IMSG_CTL_ERR, 0, 0, -1, err, strlen(err)+1);