Blob


1 /*
2 * Copyright (c) 2022 Omar Polo <op@openbsd.org>
3 *
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.
7 *
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.
15 */
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/socket.h>
20 #include <sys/uio.h>
21 #include <sys/wait.h>
23 #include <event.h>
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <limits.h>
27 #include <sndio.h>
28 #include <signal.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <stdint.h>
32 #include <string.h>
33 #include <syslog.h>
34 #include <unistd.h>
35 #include <imsg.h>
37 #include "amused.h"
38 #include "control.h"
39 #include "log.h"
40 #include "playlist.h"
41 #include "xmalloc.h"
43 struct sio_hdl *hdl;
44 char *csock = NULL;
45 int debug;
46 int verbose;
47 struct imsgev *iev_player;
49 const char *argv0;
50 pid_t player_pid;
51 struct event ev_sigint;
52 struct event ev_sigterm;
54 enum amused_process {
55 PROC_MAIN,
56 PROC_PLAYER,
57 };
59 __dead void
60 main_shutdown(void)
61 {
62 pid_t pid;
63 int status;
65 /* close pipes. */
66 msgbuf_clear(&iev_player->ibuf.w);
67 close(iev_player->ibuf.fd);
68 free(iev_player);
70 log_debug("waiting for children to terminate");
71 do {
72 pid = wait(&status);
73 if (pid == -1) {
74 if (errno != EINTR && errno != ECHILD)
75 fatal("wait");
76 } else if (WIFSIGNALED(status))
77 log_warnx("player terminated; signal %d",
78 WTERMSIG(status));
79 } while (pid != -1 || (pid == -1 && errno == EINTR));
81 log_info("terminating");
82 exit(0);
83 }
85 static void
86 main_sig_handler(int sig, short event, void *arg)
87 {
88 /*
89 * Normal signal handler rules don't apply because libevent
90 * decouples for us.
91 */
93 switch (sig) {
94 case SIGTERM:
95 case SIGINT:
96 main_shutdown();
97 break;
98 default:
99 fatalx("unexpected signal %d", sig);
103 static void
104 main_dispatch_player(int sig, short event, void *d)
106 struct imsgev *iev = d;
107 struct imsgbuf *ibuf = &iev->ibuf;
108 struct imsg imsg;
109 ssize_t n;
110 int shut = 0;
111 const char *song;
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 */
117 shut = 1;
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 */
123 shut = 1;
126 for (;;) {
127 if ((n = imsg_get(ibuf, &imsg)) == -1)
128 fatal("imsg_get");
129 if (n == 0) /* No more messages. */
130 break;
132 switch (imsg.hdr.type) {
133 case IMSG_EOF:
134 case IMSG_ERR:
135 song = playlist_advance();
136 if (song == NULL)
137 break;
138 /* XXX: watch out for failures! */
139 main_play_song(song);
140 break;
141 default:
142 log_debug("%s: error handling imsg %d", __func__,
143 imsg.hdr.type);
144 break;
146 imsg_free(&imsg);
149 if (!shut)
150 imsg_event_add(iev);
151 else {
152 /* This pipe is dead. Remove its event handler. */
153 event_del(&iev->ev);
154 event_loopexit(NULL);
158 static pid_t
159 start_child(enum amused_process proc, int fd)
161 const char *argv[5];
162 int argc = 0;
163 pid_t pid;
165 switch (pid = fork()) {
166 case -1:
167 fatal("cannot fork");
168 case 0:
169 break;
170 default:
171 close(fd);
172 return pid;
175 if (fd != 3) {
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;
182 switch (proc) {
183 case PROC_MAIN:
184 fatal("can not start main process");
185 case PROC_PLAYER:
186 argv[argc++] = "-Tp";
187 break;
190 if (debug)
191 argv[argc++] = "-d";
192 if (verbose)
193 argv[argc++] = "-v";
194 argv[argc++] = NULL;
196 /* obnoxious casts */
197 execvp(argv0, (char *const *)argv);
198 fatal("execvp %s", argv0);
201 /* daemon main routine */
202 static __dead int
203 amused_main(void)
205 int pipe_main2player[2];
206 int control_fd;
208 log_init(debug, LOG_DAEMON);
209 log_setverbose(verbose);
210 log_procinit("main");
212 if (!debug)
213 daemon(1, 0);
215 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
216 PF_UNSPEC, pipe_main2player) == -1)
217 fatal("socketpair");
219 player_pid = start_child(PROC_PLAYER, pipe_main2player[1]);
221 event_init();
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)
246 fatal("pledge");
248 log_info("startup");
249 event_dispatch();
250 main_shutdown();
253 int
254 main(int argc, char **argv)
256 int ch, proc = PROC_MAIN;
258 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized */
259 log_setverbose(1);
261 argv0 = argv[0];
262 if (argv0 == NULL)
263 argv0 = "amused";
265 while ((ch = getopt(argc, argv, "ds:T:vV")) != -1) {
266 switch (ch) {
267 case 'd':
268 debug = 1;
269 break;
270 case 's':
271 free(csock);
272 csock = xstrdup(optarg);
273 break;
274 case 'T':
275 switch (*optarg) {
276 case 'p':
277 proc = PROC_PLAYER;
278 break;
279 default:
280 usage();
282 break;
283 case 'v':
284 verbose++;
285 break;
286 case 'V':
287 printf("%s version %s\n", getprogname(),
288 AMUSED_VERSION);
289 exit(0);
290 default:
291 usage();
294 argv += optind;
295 argc -= optind;
297 if (proc == PROC_PLAYER)
298 exit(player(debug, verbose));
300 if (csock == NULL)
301 xasprintf(&csock, "/tmp/amused-%d", getuid());
303 if (argc == 0)
304 amused_main();
305 else
306 ctl(argc, argv);
307 return 0;
310 void
311 imsg_event_add(struct imsgev *iev)
313 iev->events = EV_READ;
314 if (iev->ibuf.w.queued)
315 iev->events |= EV_WRITE;
317 event_del(&iev->ev);
318 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
319 event_add(&iev->ev, NULL);
322 int
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)
326 int ret;
328 if ((ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data,
329 datalen)) != -1)
330 imsg_event_add(iev);
332 return ret;
335 int
336 main_play_song(const char *song)
338 char path[PATH_MAX] = { 0 };
339 int fd;
341 strlcpy(path, song, sizeof(path));
342 if ((fd = open(path, O_RDONLY)) == -1) {
343 #if todo
344 log_warn("open %s", path);
345 return -1;
346 #else
347 fatal("open %s", path);
348 #endif
351 imsg_compose_event(iev_player, IMSG_PLAY, 0, 0, fd,
352 path, sizeof(path));
353 return 0;
356 void
357 main_enqueue(struct imsgev *iev, struct imsg *imsg)
359 size_t datalen;
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";
366 goto err;
369 memcpy(path, imsg->data, sizeof(path));
370 if (path[datalen-1] != '\0') {
371 err = "malformed data";
372 goto err;
375 playlist_enqueue(path);
376 imsg_compose_event(iev, IMSG_CTL_ADD, 0, 0, -1, path, sizeof(path));
377 return;
378 err:
379 imsg_compose_event(iev, IMSG_CTL_ERR, 0, 0, -1, err, strlen(err)+1);