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;
148 default:
149 log_debug("%s: error handling imsg %d", __func__,
150 imsg.hdr.type);
151 break;
153 imsg_free(&imsg);
156 if (!shut)
157 imsg_event_add(iev);
158 else {
159 /* This pipe is dead. Remove its event handler. */
160 event_del(&iev->ev);
161 event_loopexit(NULL);
165 static pid_t
166 start_child(enum amused_process proc, int fd)
168 const char *argv[6];
169 int argc = 0;
170 pid_t pid;
172 switch (pid = fork()) {
173 case -1:
174 fatal("cannot fork");
175 case 0:
176 break;
177 default:
178 close(fd);
179 return pid;
182 if (fd != 3) {
183 if (fd != -1 && dup2(fd, 3) == -1)
184 fatal("cannot setup imsg fd");
185 } else if (fcntl(F_SETFD, 0) == -1)
186 fatal("cannot setup imsg fd");
188 argv[argc++] = argv0;
190 switch (proc) {
191 case PROC_MAIN:
192 argv[argc++] = "-s";
193 argv[argc++] = csock;
194 break;
195 case PROC_PLAYER:
196 argv[argc++] = "-Tp";
197 break;
200 if (debug)
201 argv[argc++] = "-d";
202 if (verbose)
203 argv[argc++] = "-v";
204 argv[argc++] = NULL;
206 /* obnoxious casts */
207 execvp(argv0, (char *const *)argv);
208 fatal("execvp %s", argv0);
211 /* daemon main routine */
212 static __dead int
213 amused_main(void)
215 int pipe_main2player[2];
216 int control_fd;
218 log_init(debug, LOG_DAEMON);
219 log_setverbose(verbose);
220 log_procinit("main");
222 if (!debug)
223 daemon(1, 0);
225 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
226 PF_UNSPEC, pipe_main2player) == -1)
227 fatal("socketpair");
229 player_pid = start_child(PROC_PLAYER, pipe_main2player[1]);
231 event_init();
233 signal_set(&ev_sigint, SIGINT, main_sig_handler, NULL);
234 signal_set(&ev_sigterm, SIGTERM, main_sig_handler, NULL);
236 signal_add(&ev_sigint, NULL);
237 signal_add(&ev_sigterm, NULL);
239 signal(SIGHUP, SIG_IGN);
240 signal(SIGCHLD, SIG_IGN);
241 signal(SIGPIPE, SIG_IGN);
243 iev_player = xmalloc(sizeof(*iev_player));
244 imsg_init(&iev_player->ibuf, pipe_main2player[0]);
245 iev_player->handler = main_dispatch_player;
246 iev_player->events = EV_READ;
247 event_set(&iev_player->ev, iev_player->ibuf.fd, iev_player->events,
248 iev_player->handler, iev_player);
249 event_add(&iev_player->ev, NULL);
251 if ((control_fd = control_init(csock)) == -1)
252 fatal("control socket setup failed %s", csock);
253 control_listen(control_fd);
255 if (pledge("stdio rpath unix sendfd", NULL) == -1)
256 fatal("pledge");
258 log_info("startup");
259 event_dispatch();
260 main_shutdown();
263 int
264 main(int argc, char **argv)
266 int ch, proc = PROC_MAIN;
268 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized */
269 log_setverbose(1);
271 argv0 = argv[0];
272 if (argv0 == NULL)
273 argv0 = "amused";
275 while ((ch = getopt(argc, argv, "ds:T:v")) != -1) {
276 switch (ch) {
277 case 'd':
278 debug = 1;
279 break;
280 case 's':
281 free(csock);
282 csock = xstrdup(optarg);
283 break;
284 case 'T':
285 switch (*optarg) {
286 case 'p':
287 proc = PROC_PLAYER;
288 break;
289 default:
290 usage();
292 break;
293 case 'v':
294 verbose++;
295 break;
296 default:
297 usage();
300 argv += optind;
301 argc -= optind;
303 if (proc == PROC_PLAYER)
304 exit(player(debug, verbose));
306 if (csock == NULL)
307 xasprintf(&csock, "/tmp/amused-%d", getuid());
309 if (argc == 0)
310 amused_main();
311 else
312 ctl(argc, argv);
313 return 0;
316 void
317 spawn_daemon(void)
319 debug = 0;
320 start_child(PROC_MAIN, -1);
323 void
324 imsg_event_add(struct imsgev *iev)
326 iev->events = EV_READ;
327 if (iev->ibuf.w.queued)
328 iev->events |= EV_WRITE;
330 event_del(&iev->ev);
331 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
332 event_add(&iev->ev, NULL);
335 int
336 imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
337 pid_t pid, int fd, const void *data, uint16_t datalen)
339 int ret;
341 if ((ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data,
342 datalen)) != -1)
343 imsg_event_add(iev);
345 return ret;
348 int
349 main_send_player(uint16_t type, int fd, const void *data, uint16_t datalen)
351 return imsg_compose_event(iev_player, type, 0, 0, fd, data, datalen);
354 int
355 main_play_song(const char *song)
357 char path[PATH_MAX] = { 0 };
358 int fd;
360 strlcpy(path, song, sizeof(path));
361 if ((fd = open(path, O_RDONLY)) == -1) {
362 log_warn("open %s", path);
363 return 0;
366 play_state = STATE_PLAYING;
367 imsg_compose_event(iev_player, IMSG_PLAY, 0, 0, fd,
368 path, sizeof(path));
369 return 1;
372 void
373 main_playlist_jump(struct imsgev *iev, struct imsg *imsg)
375 size_t datalen;
376 char arg[PATH_MAX];
377 const char *song;
379 datalen = IMSG_DATA_SIZE(*imsg);
380 if (datalen != sizeof(arg)) {
381 main_senderr(iev, "wrong size");
382 return;
385 memcpy(arg, imsg->data, sizeof(arg));
386 if (arg[sizeof(arg)-1] != '\0') {
387 main_senderr(iev, "data corrupted");
388 return;
391 song = playlist_jump(arg);
392 if (song == NULL) {
393 main_senderr(iev, "not found");
394 return;
397 main_send_player(IMSG_STOP, -1, NULL, 0);
398 if (!main_play_song(song)) {
399 main_senderr(iev, "can't play");
400 playlist_dropcurrent();
401 main_playlist_advance();
402 return;
405 main_send_status(iev);
408 void
409 main_playlist_resume(void)
411 const char *song;
413 if ((song = current_song) == NULL)
414 song = playlist_advance();
416 for (; song != NULL; song = playlist_advance()) {
417 if (main_play_song(song))
418 return;
420 playlist_dropcurrent();
424 void
425 main_playlist_advance(void)
427 const char *song;
429 for (;;) {
430 song = playlist_advance();
431 if (song == NULL)
432 return;
434 if (main_play_song(song))
435 break;
437 playlist_dropcurrent();
441 void
442 main_playlist_previous(void)
444 const char *song;
446 for (;;) {
447 song = playlist_previous();
448 if (song == NULL)
449 return;
451 if (main_play_song(song))
452 break;
454 playlist_dropcurrent();
458 void
459 main_restart_track(void)
461 const char *song;
463 song = current_song;
464 if (song == NULL)
465 return;
467 if (main_play_song(song))
468 return;
470 playlist_dropcurrent();
471 main_playlist_advance();
474 void
475 main_senderr(struct imsgev *iev, const char *msg)
477 imsg_compose_event(iev, IMSG_CTL_ERR, 0, 0, -1,
478 msg, strlen(msg)+1);
481 void
482 main_enqueue(int tx, struct playlist *px, struct imsgev *iev,
483 struct imsg *imsg)
485 size_t datalen;
486 char path[PATH_MAX] = { 0 };
487 const char *err = NULL;
489 datalen = IMSG_DATA_SIZE(*imsg);
490 if (datalen != sizeof(path)) {
491 err = "data size mismatch";
492 goto err;
495 memcpy(path, imsg->data, sizeof(path));
496 if (path[datalen-1] != '\0') {
497 err = "malformed data";
498 goto err;
501 if (tx)
502 playlist_push(px, path);
503 else
504 playlist_enqueue(path);
505 imsg_compose_event(iev, IMSG_CTL_ADD, 0, 0, -1, path, sizeof(path));
506 return;
507 err:
508 imsg_compose_event(iev, IMSG_CTL_ERR, 0, 0, -1, err, strlen(err)+1);
511 void
512 main_send_playlist(struct imsgev *iev)
514 struct player_status s;
515 size_t i;
517 for (i = 0; i < playlist.len; ++i) {
518 memset(&s, 0, sizeof(s));
519 strlcpy(s.path, playlist.songs[i], sizeof(s.path));
520 s.status = play_off == i ? STATE_PLAYING : STATE_STOPPED;
521 imsg_compose_event(iev, IMSG_CTL_SHOW, 0, 0, -1, &s,
522 sizeof(s));
525 imsg_compose_event(iev, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
528 void
529 main_send_status(struct imsgev *iev)
531 struct player_status s;
533 memset(&s, 0, sizeof(s));
535 if (current_song != NULL)
536 strlcpy(s.path, current_song, sizeof(s.path));
537 s.status = play_state;
538 s.rp.repeat_all = repeat_all;
539 s.rp.repeat_one = repeat_one;
541 imsg_compose_event(iev, IMSG_CTL_STATUS, 0, 0, -1, &s, sizeof(s));