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;
53 struct event ev_siginfo;
55 enum amused_process {
56 PROC_MAIN,
57 PROC_PLAYER,
58 };
60 __dead void
61 main_shutdown(void)
62 {
63 pid_t pid;
64 int status;
66 /* close pipes. */
67 msgbuf_clear(&iev_player->ibuf.w);
68 close(iev_player->ibuf.fd);
69 free(iev_player);
71 log_debug("waiting for children to terminate");
72 do {
73 pid = wait(&status);
74 if (pid == -1) {
75 if (errno != EINTR && errno != ECHILD)
76 fatal("wait");
77 } else if (WIFSIGNALED(status))
78 log_warnx("player terminated; signal %d",
79 WTERMSIG(status));
80 } while (pid != -1 || (pid == -1 && errno == EINTR));
82 log_info("terminating");
83 exit(0);
84 }
86 static void
87 main_status(void)
88 {
89 const char *cur;
91 switch (play_state) {
92 case STATE_STOPPED:
93 log_info("status: stopped");
94 break;
95 case STATE_PLAYING:
96 log_info("status: playing");
97 break;
98 case STATE_PAUSED:
99 log_info("status: paused");
100 break;
101 default:
102 log_info("status: unknown");
103 break;
106 if ((cur = playlist_current()) != NULL)
107 log_info("playing %s", cur);
108 else
109 log_info("not playing anything");
112 static void
113 main_sig_handler(int sig, short event, void *arg)
115 /*
116 * Normal signal handler rules don't apply because libevent
117 * decouples for us.
118 */
120 switch (sig) {
121 case SIGTERM:
122 case SIGINT:
123 main_shutdown();
124 break;
125 case SIGINFO:
126 main_status();
127 break;
128 default:
129 fatalx("unexpected signal %d", sig);
133 static void
134 main_dispatch_player(int sig, short event, void *d)
136 struct imsgev *iev = d;
137 struct imsgbuf *ibuf = &iev->ibuf;
138 struct imsg imsg;
139 ssize_t n;
140 int shut = 0;
141 const char *song;
143 if (event & EV_READ) {
144 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
145 fatal("imsg_read error");
146 if (n == 0) /* Connection closed */
147 shut = 1;
149 if (event & EV_WRITE) {
150 if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
151 fatal("msgbuf_write");
152 if (n == 0) /* Connection closed */
153 shut = 1;
156 for (;;) {
157 if ((n = imsg_get(ibuf, &imsg)) == -1)
158 fatal("imsg_get");
159 if (n == 0) /* No more messages. */
160 break;
162 switch (imsg.hdr.type) {
163 case IMSG_EOF:
164 case IMSG_ERR:
165 song = playlist_advance();
166 if (song == NULL)
167 break;
168 /* XXX: watch out for failures! */
169 main_play_song(song);
170 break;
171 default:
172 log_debug("%s: error handling imsg %d", __func__,
173 imsg.hdr.type);
174 break;
176 imsg_free(&imsg);
179 if (!shut)
180 imsg_event_add(iev);
181 else {
182 /* This pipe is dead. Remove its event handler. */
183 event_del(&iev->ev);
184 event_loopexit(NULL);
188 static pid_t
189 start_child(enum amused_process proc, int fd)
191 const char *argv[5];
192 int argc = 0;
193 pid_t pid;
195 switch (pid = fork()) {
196 case -1:
197 fatal("cannot fork");
198 case 0:
199 break;
200 default:
201 close(fd);
202 return pid;
205 if (fd != 3) {
206 if (dup2(fd, 3) == -1)
207 fatal("cannot setup imsg fd");
208 } else if (fcntl(F_SETFD, 0) == -1)
209 fatal("cannot setup imsg fd");
211 argv[argc++] = argv0;
212 switch (proc) {
213 case PROC_MAIN:
214 fatal("can not start main process");
215 case PROC_PLAYER:
216 argv[argc++] = "-Tp";
217 break;
220 if (debug)
221 argv[argc++] = "-d";
222 if (verbose)
223 argv[argc++] = "-v";
224 argv[argc++] = NULL;
226 /* obnoxious casts */
227 execvp(argv0, (char *const *)argv);
228 fatal("execvp %s", argv0);
231 /* daemon main routine */
232 static __dead int
233 amused_main(void)
235 int pipe_main2player[2];
236 int control_fd;
238 log_init(debug, LOG_DAEMON);
239 log_setverbose(verbose);
240 log_procinit("main");
242 if (!debug)
243 daemon(1, 0);
245 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
246 PF_UNSPEC, pipe_main2player) == -1)
247 fatal("socketpair");
249 player_pid = start_child(PROC_PLAYER, pipe_main2player[1]);
251 event_init();
253 signal_set(&ev_sigint, SIGINT, main_sig_handler, NULL);
254 signal_set(&ev_sigterm, SIGTERM, main_sig_handler, NULL);
255 signal_set(&ev_siginfo, SIGINFO, main_sig_handler, NULL);
257 signal_add(&ev_sigint, NULL);
258 signal_add(&ev_sigterm, NULL);
259 signal_add(&ev_siginfo, NULL);
261 signal(SIGHUP, SIG_IGN);
262 signal(SIGCHLD, SIG_IGN);
263 signal(SIGPIPE, SIG_IGN);
265 iev_player = xmalloc(sizeof(*iev_player));
266 imsg_init(&iev_player->ibuf, pipe_main2player[0]);
267 iev_player->handler = main_dispatch_player;
268 iev_player->events = EV_READ;
269 event_set(&iev_player->ev, iev_player->ibuf.fd, iev_player->events,
270 iev_player->handler, iev_player);
271 event_add(&iev_player->ev, NULL);
273 if ((control_fd = control_init(csock)) == -1)
274 fatal("control socket setup failed %s", csock);
275 control_listen(control_fd);
277 if (pledge("stdio rpath unix sendfd", NULL) == -1)
278 fatal("pledge");
280 log_info("startup");
281 event_dispatch();
282 main_shutdown();
285 int
286 main(int argc, char **argv)
288 int ch, proc = PROC_MAIN;
290 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized */
291 log_setverbose(1);
293 argv0 = argv[0];
294 if (argv0 == NULL)
295 argv0 = "amused";
297 while ((ch = getopt(argc, argv, "ds:T:vV")) != -1) {
298 switch (ch) {
299 case 'd':
300 debug = 1;
301 break;
302 case 's':
303 free(csock);
304 csock = xstrdup(optarg);
305 break;
306 case 'T':
307 switch (*optarg) {
308 case 'p':
309 proc = PROC_PLAYER;
310 break;
311 default:
312 usage();
314 break;
315 case 'v':
316 verbose++;
317 break;
318 case 'V':
319 printf("%s version %s\n", getprogname(),
320 AMUSED_VERSION);
321 exit(0);
322 default:
323 usage();
326 argv += optind;
327 argc -= optind;
329 if (proc == PROC_PLAYER)
330 exit(player(debug, verbose));
332 if (csock == NULL)
333 xasprintf(&csock, "/tmp/amused-%d", getuid());
335 if (argc == 0)
336 amused_main();
337 else
338 ctl(argc, argv);
339 return 0;
342 void
343 imsg_event_add(struct imsgev *iev)
345 iev->events = EV_READ;
346 if (iev->ibuf.w.queued)
347 iev->events |= EV_WRITE;
349 event_del(&iev->ev);
350 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
351 event_add(&iev->ev, NULL);
354 int
355 imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
356 pid_t pid, int fd, const void *data, uint16_t datalen)
358 int ret;
360 if ((ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data,
361 datalen)) != -1)
362 imsg_event_add(iev);
364 return ret;
367 int
368 main_play_song(const char *song)
370 char path[PATH_MAX] = { 0 };
371 int fd;
373 strlcpy(path, song, sizeof(path));
374 if ((fd = open(path, O_RDONLY)) == -1) {
375 #if todo
376 log_warn("open %s", path);
377 return -1;
378 #else
379 fatal("open %s", path);
380 #endif
383 imsg_compose_event(iev_player, IMSG_PLAY, 0, 0, fd,
384 path, sizeof(path));
385 return 0;
388 void
389 main_enqueue(struct imsgev *iev, struct imsg *imsg)
391 size_t datalen;
392 char path[PATH_MAX] = { 0 };
393 const char *err = NULL;
395 datalen = IMSG_DATA_SIZE(*imsg);
396 if (datalen != sizeof(path)) {
397 err = "data size mismatch";
398 goto err;
401 memcpy(path, imsg->data, sizeof(path));
402 if (path[datalen-1] != '\0') {
403 err = "malformed data";
404 goto err;
407 playlist_enqueue(path);
408 imsg_compose_event(iev, IMSG_CTL_ADD, 0, 0, -1, path, sizeof(path));
409 return;
410 err:
411 imsg_compose_event(iev, IMSG_CTL_ERR, 0, 0, -1, err, strlen(err)+1);