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;
112 if (event & EV_READ) {
113 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
114 fatal("imsg_read error");
115 if (n == 0) /* Connection closed */
116 shut = 1;
118 if (event & EV_WRITE) {
119 if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
120 fatal("msgbuf_write");
121 if (n == 0) /* Connection closed */
122 shut = 1;
125 for (;;) {
126 if ((n = imsg_get(ibuf, &imsg)) == -1)
127 fatal("imsg_get");
128 if (n == 0) /* No more messages. */
129 break;
131 switch (imsg.hdr.type) {
132 case IMSG_ERR:
133 playlist_dropcurrent();
134 /* fallthrough */
135 case IMSG_EOF:
136 if (repeat_one && current_song != NULL) {
137 if (main_play_song(current_song))
138 break;
139 playlist_dropcurrent();
141 main_playlist_advance();
142 if (play_state == STATE_PLAYING)
143 control_notify(NULL, IMSG_CTL_NEXT);
144 else
145 control_notify(NULL, IMSG_CTL_STOP);
146 break;
147 default:
148 log_debug("%s: error handling imsg %d", __func__,
149 imsg.hdr.type);
150 break;
152 imsg_free(&imsg);
155 if (!shut)
156 imsg_event_add(iev);
157 else {
158 /* This pipe is dead. Remove its event handler. */
159 event_del(&iev->ev);
160 event_loopexit(NULL);
164 static pid_t
165 start_child(enum amused_process proc, int fd)
167 const char *argv[6];
168 int argc = 0;
169 pid_t pid;
171 switch (pid = fork()) {
172 case -1:
173 fatal("cannot fork");
174 case 0:
175 break;
176 default:
177 close(fd);
178 return pid;
181 if (fd != 3) {
182 if (fd != -1 && dup2(fd, 3) == -1)
183 fatal("cannot setup imsg fd");
184 } else if (fcntl(F_SETFD, 0) == -1)
185 fatal("cannot setup imsg fd");
187 argv[argc++] = argv0;
189 switch (proc) {
190 case PROC_MAIN:
191 argv[argc++] = "-s";
192 argv[argc++] = csock;
193 break;
194 case PROC_PLAYER:
195 argv[argc++] = "-Tp";
196 break;
199 if (debug)
200 argv[argc++] = "-d";
201 if (verbose)
202 argv[argc++] = "-v";
203 argv[argc++] = NULL;
205 /* obnoxious casts */
206 execvp(argv0, (char *const *)argv);
207 fatal("execvp %s", argv0);
210 /* daemon main routine */
211 static __dead int
212 amused_main(void)
214 int pipe_main2player[2];
215 int control_fd;
217 log_init(debug, LOG_DAEMON);
218 log_setverbose(verbose);
219 log_procinit("main");
221 if (!debug)
222 daemon(1, 0);
224 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
225 PF_UNSPEC, pipe_main2player) == -1)
226 fatal("socketpair");
228 player_pid = start_child(PROC_PLAYER, pipe_main2player[1]);
230 event_init();
232 signal_set(&ev_sigint, SIGINT, main_sig_handler, NULL);
233 signal_set(&ev_sigterm, SIGTERM, main_sig_handler, NULL);
235 signal_add(&ev_sigint, NULL);
236 signal_add(&ev_sigterm, NULL);
238 signal(SIGHUP, SIG_IGN);
239 signal(SIGCHLD, SIG_IGN);
240 signal(SIGPIPE, SIG_IGN);
242 iev_player = xmalloc(sizeof(*iev_player));
243 imsg_init(&iev_player->ibuf, pipe_main2player[0]);
244 iev_player->handler = main_dispatch_player;
245 iev_player->events = EV_READ;
246 event_set(&iev_player->ev, iev_player->ibuf.fd, iev_player->events,
247 iev_player->handler, iev_player);
248 event_add(&iev_player->ev, NULL);
250 if ((control_fd = control_init(csock)) == -1)
251 fatal("control socket setup failed %s", csock);
252 control_listen(control_fd);
254 if (pledge("stdio rpath unix sendfd", NULL) == -1)
255 fatal("pledge");
257 log_info("startup");
258 event_dispatch();
259 main_shutdown();
262 int
263 main(int argc, char **argv)
265 int ch, proc = PROC_MAIN;
267 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized */
268 log_setverbose(1);
270 argv0 = argv[0];
271 if (argv0 == NULL)
272 argv0 = "amused";
274 while ((ch = getopt(argc, argv, "ds:T:v")) != -1) {
275 switch (ch) {
276 case 'd':
277 debug = 1;
278 break;
279 case 's':
280 free(csock);
281 csock = xstrdup(optarg);
282 break;
283 case 'T':
284 switch (*optarg) {
285 case 'p':
286 proc = PROC_PLAYER;
287 break;
288 default:
289 usage();
291 break;
292 case 'v':
293 verbose++;
294 break;
295 default:
296 usage();
299 argv += optind;
300 argc -= optind;
302 if (proc == PROC_PLAYER)
303 exit(player(debug, verbose));
305 if (csock == NULL)
306 xasprintf(&csock, "/tmp/amused-%d", getuid());
308 if (argc == 0)
309 amused_main();
310 else
311 ctl(argc, argv);
312 return 0;
315 void
316 spawn_daemon(void)
318 debug = 0;
319 start_child(PROC_MAIN, -1);
322 void
323 imsg_event_add(struct imsgev *iev)
325 iev->events = EV_READ;
326 if (iev->ibuf.w.queued)
327 iev->events |= EV_WRITE;
329 event_del(&iev->ev);
330 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
331 event_add(&iev->ev, NULL);
334 int
335 imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
336 pid_t pid, int fd, const void *data, uint16_t datalen)
338 int ret;
340 if ((ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data,
341 datalen)) != -1)
342 imsg_event_add(iev);
344 return ret;
347 int
348 main_send_player(uint16_t type, int fd, const void *data, uint16_t datalen)
350 return imsg_compose_event(iev_player, type, 0, 0, fd, data, datalen);
353 int
354 main_play_song(const char *song)
356 char path[PATH_MAX] = { 0 };
357 int fd;
359 strlcpy(path, song, sizeof(path));
360 if ((fd = open(path, O_RDONLY)) == -1) {
361 log_warn("open %s", path);
362 return 0;
365 play_state = STATE_PLAYING;
366 imsg_compose_event(iev_player, IMSG_PLAY, 0, 0, fd,
367 path, sizeof(path));
368 return 1;
371 void
372 main_playlist_jump(struct imsgev *iev, struct imsg *imsg)
374 size_t datalen;
375 char arg[PATH_MAX];
376 const char *song;
378 datalen = IMSG_DATA_SIZE(*imsg);
379 if (datalen != sizeof(arg)) {
380 main_senderr(iev, "wrong size");
381 return;
384 memcpy(arg, imsg->data, sizeof(arg));
385 if (arg[sizeof(arg)-1] != '\0') {
386 main_senderr(iev, "data corrupted");
387 return;
390 song = playlist_jump(arg);
391 if (song == NULL) {
392 main_senderr(iev, "not found");
393 return;
396 main_send_player(IMSG_STOP, -1, NULL, 0);
397 if (!main_play_song(song)) {
398 main_senderr(iev, "can't play");
399 playlist_dropcurrent();
400 main_playlist_advance();
401 return;
404 main_send_status(iev);
407 void
408 main_playlist_resume(void)
410 const char *song;
412 if ((song = current_song) == NULL)
413 song = playlist_advance();
415 for (; song != NULL; song = playlist_advance()) {
416 if (main_play_song(song))
417 return;
419 playlist_dropcurrent();
423 void
424 main_playlist_advance(void)
426 const char *song;
428 for (;;) {
429 song = playlist_advance();
430 if (song == NULL)
431 return;
433 if (main_play_song(song))
434 break;
436 playlist_dropcurrent();
440 void
441 main_playlist_previous(void)
443 const char *song;
445 for (;;) {
446 song = playlist_previous();
447 if (song == NULL)
448 return;
450 if (main_play_song(song))
451 break;
453 playlist_dropcurrent();
457 void
458 main_restart_track(void)
460 const char *song;
462 song = current_song;
463 if (song == NULL)
464 return;
466 if (main_play_song(song))
467 return;
469 playlist_dropcurrent();
470 main_playlist_advance();
473 void
474 main_senderr(struct imsgev *iev, const char *msg)
476 imsg_compose_event(iev, IMSG_CTL_ERR, 0, 0, -1,
477 msg, strlen(msg)+1);
480 void
481 main_enqueue(int tx, struct playlist *px, struct imsgev *iev,
482 struct imsg *imsg)
484 size_t datalen;
485 char path[PATH_MAX] = { 0 };
486 const char *err = NULL;
488 datalen = IMSG_DATA_SIZE(*imsg);
489 if (datalen != sizeof(path)) {
490 err = "data size mismatch";
491 goto err;
494 memcpy(path, imsg->data, sizeof(path));
495 if (path[datalen-1] != '\0') {
496 err = "malformed data";
497 goto err;
500 if (tx)
501 playlist_push(px, path);
502 else
503 playlist_enqueue(path);
504 imsg_compose_event(iev, IMSG_CTL_ADD, 0, 0, -1, path, sizeof(path));
505 return;
506 err:
507 imsg_compose_event(iev, IMSG_CTL_ERR, 0, 0, -1, err, strlen(err)+1);
510 void
511 main_send_playlist(struct imsgev *iev)
513 struct player_status s;
514 size_t i;
516 for (i = 0; i < playlist.len; ++i) {
517 memset(&s, 0, sizeof(s));
518 strlcpy(s.path, playlist.songs[i], sizeof(s.path));
519 s.status = play_off == i ? STATE_PLAYING : STATE_STOPPED;
520 imsg_compose_event(iev, IMSG_CTL_SHOW, 0, 0, -1, &s,
521 sizeof(s));
524 imsg_compose_event(iev, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
527 void
528 main_send_status(struct imsgev *iev)
530 struct player_status s;
532 memset(&s, 0, sizeof(s));
534 if (current_song != NULL)
535 strlcpy(s.path, current_song, sizeof(s.path));
536 s.status = play_state;
537 s.rp.repeat_all = repeat_all;
538 s.rp.repeat_one = repeat_one;
540 imsg_compose_event(iev, IMSG_CTL_STATUS, 0, 0, -1, &s, sizeof(s));