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 = current_song) != 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;
142 if (event & EV_READ) {
143 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
144 fatal("imsg_read error");
145 if (n == 0) /* Connection closed */
146 shut = 1;
148 if (event & EV_WRITE) {
149 if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
150 fatal("msgbuf_write");
151 if (n == 0) /* Connection closed */
152 shut = 1;
155 for (;;) {
156 if ((n = imsg_get(ibuf, &imsg)) == -1)
157 fatal("imsg_get");
158 if (n == 0) /* No more messages. */
159 break;
161 switch (imsg.hdr.type) {
162 case IMSG_ERR:
163 playlist_dropcurrent();
164 main_playlist_advance();
165 break;
166 case IMSG_EOF:
167 if (repeat_one && current_song != NULL)
168 if (main_play_song(current_song))
169 break;
170 main_playlist_advance();
171 break;
173 default:
174 log_debug("%s: error handling imsg %d", __func__,
175 imsg.hdr.type);
176 break;
178 imsg_free(&imsg);
181 if (!shut)
182 imsg_event_add(iev);
183 else {
184 /* This pipe is dead. Remove its event handler. */
185 event_del(&iev->ev);
186 event_loopexit(NULL);
190 static pid_t
191 start_child(enum amused_process proc, int fd)
193 const char *argv[6];
194 int argc = 0;
195 pid_t pid;
197 switch (pid = fork()) {
198 case -1:
199 fatal("cannot fork");
200 case 0:
201 break;
202 default:
203 close(fd);
204 return pid;
207 if (fd != 3) {
208 if (fd != -1 && dup2(fd, 3) == -1)
209 fatal("cannot setup imsg fd");
210 } else if (fcntl(F_SETFD, 0) == -1)
211 fatal("cannot setup imsg fd");
213 argv[argc++] = argv0;
215 switch (proc) {
216 case PROC_MAIN:
217 argv[argc++] = "-s";
218 argv[argc++] = csock;
219 break;
220 case PROC_PLAYER:
221 argv[argc++] = "-Tp";
222 break;
225 if (debug)
226 argv[argc++] = "-d";
227 if (verbose)
228 argv[argc++] = "-v";
229 argv[argc++] = NULL;
231 /* obnoxious casts */
232 execvp(argv0, (char *const *)argv);
233 fatal("execvp %s", argv0);
236 /* daemon main routine */
237 static __dead int
238 amused_main(void)
240 int pipe_main2player[2];
241 int control_fd;
243 log_init(debug, LOG_DAEMON);
244 log_setverbose(verbose);
245 log_procinit("main");
247 if (!debug)
248 daemon(1, 0);
250 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
251 PF_UNSPEC, pipe_main2player) == -1)
252 fatal("socketpair");
254 player_pid = start_child(PROC_PLAYER, pipe_main2player[1]);
256 event_init();
258 signal_set(&ev_sigint, SIGINT, main_sig_handler, NULL);
259 signal_set(&ev_sigterm, SIGTERM, main_sig_handler, NULL);
260 signal_set(&ev_siginfo, SIGINFO, main_sig_handler, NULL);
262 signal_add(&ev_sigint, NULL);
263 signal_add(&ev_sigterm, NULL);
264 signal_add(&ev_siginfo, NULL);
266 signal(SIGHUP, SIG_IGN);
267 signal(SIGCHLD, SIG_IGN);
268 signal(SIGPIPE, SIG_IGN);
270 iev_player = xmalloc(sizeof(*iev_player));
271 imsg_init(&iev_player->ibuf, pipe_main2player[0]);
272 iev_player->handler = main_dispatch_player;
273 iev_player->events = EV_READ;
274 event_set(&iev_player->ev, iev_player->ibuf.fd, iev_player->events,
275 iev_player->handler, iev_player);
276 event_add(&iev_player->ev, NULL);
278 if ((control_fd = control_init(csock)) == -1)
279 fatal("control socket setup failed %s", csock);
280 control_listen(control_fd);
282 if (pledge("stdio rpath unix sendfd", NULL) == -1)
283 fatal("pledge");
285 log_info("startup");
286 event_dispatch();
287 main_shutdown();
290 int
291 main(int argc, char **argv)
293 int ch, proc = PROC_MAIN;
295 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized */
296 log_setverbose(1);
298 argv0 = argv[0];
299 if (argv0 == NULL)
300 argv0 = "amused";
302 while ((ch = getopt(argc, argv, "ds:T:v")) != -1) {
303 switch (ch) {
304 case 'd':
305 debug = 1;
306 break;
307 case 's':
308 free(csock);
309 csock = xstrdup(optarg);
310 break;
311 case 'T':
312 switch (*optarg) {
313 case 'p':
314 proc = PROC_PLAYER;
315 break;
316 default:
317 usage();
319 break;
320 case 'v':
321 verbose++;
322 break;
323 default:
324 usage();
327 argv += optind;
328 argc -= optind;
330 if (proc == PROC_PLAYER)
331 exit(player(debug, verbose));
333 if (csock == NULL)
334 xasprintf(&csock, "/tmp/amused-%d", getuid());
336 if (argc == 0)
337 amused_main();
338 else
339 ctl(argc, argv);
340 return 0;
343 void
344 spawn_daemon(void)
346 debug = 0;
347 start_child(PROC_MAIN, -1);
350 void
351 imsg_event_add(struct imsgev *iev)
353 iev->events = EV_READ;
354 if (iev->ibuf.w.queued)
355 iev->events |= EV_WRITE;
357 event_del(&iev->ev);
358 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
359 event_add(&iev->ev, NULL);
362 int
363 imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
364 pid_t pid, int fd, const void *data, uint16_t datalen)
366 int ret;
368 if ((ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data,
369 datalen)) != -1)
370 imsg_event_add(iev);
372 return ret;
375 int
376 main_send_player(uint16_t type, int fd, const void *data, uint16_t datalen)
378 return imsg_compose_event(iev_player, type, 0, 0, fd, data, datalen);
381 int
382 main_play_song(const char *song)
384 char path[PATH_MAX] = { 0 };
385 int fd;
387 strlcpy(path, song, sizeof(path));
388 if ((fd = open(path, O_RDONLY)) == -1) {
389 log_warn("open %s", path);
390 return 0;
393 play_state = STATE_PLAYING;
394 imsg_compose_event(iev_player, IMSG_PLAY, 0, 0, fd,
395 path, sizeof(path));
396 return 1;
399 void
400 main_playlist_jump(struct imsgev *iev, struct imsg *imsg)
402 size_t datalen;
403 char arg[PATH_MAX];
404 const char *song;
406 datalen = IMSG_DATA_SIZE(*imsg);
407 if (datalen != sizeof(arg)) {
408 main_senderr(iev, "wrong size");
409 return;
412 memcpy(arg, imsg->data, sizeof(arg));
413 if (arg[sizeof(arg)-1] != '\0') {
414 main_senderr(iev, "data corrupted");
415 return;
418 song = playlist_jump(arg);
419 if (song == NULL) {
420 main_senderr(iev, "not found");
421 return;
424 main_send_player(IMSG_STOP, -1, NULL, 0);
425 if (!main_play_song(song)) {
426 main_senderr(iev, "can't play");
427 playlist_dropcurrent();
428 main_playlist_advance();
429 return;
432 main_send_status(iev);
435 void
436 main_playlist_resume(void)
438 const char *song;
440 if ((song = current_song) == NULL)
441 song = playlist_advance();
443 for (; song != NULL; song = playlist_advance()) {
444 if (main_play_song(song))
445 return;
447 playlist_dropcurrent();
451 void
452 main_playlist_advance(void)
454 const char *song;
456 for (;;) {
457 song = playlist_advance();
458 if (song == NULL)
459 return;
461 if (main_play_song(song))
462 break;
464 playlist_dropcurrent();
468 void
469 main_playlist_previous(void)
471 const char *song;
473 for (;;) {
474 song = playlist_previous();
475 if (song == NULL)
476 return;
478 if (main_play_song(song))
479 break;
481 playlist_dropcurrent();
485 void
486 main_restart_track(void)
488 const char *song;
490 song = current_song;
491 if (song == NULL)
492 return;
494 if (main_play_song(song))
495 return;
497 playlist_dropcurrent();
498 main_playlist_advance();
501 void
502 main_senderr(struct imsgev *iev, const char *msg)
504 imsg_compose_event(iev, IMSG_CTL_ERR, 0, 0, -1,
505 msg, strlen(msg)+1);
508 void
509 main_enqueue(int tx, struct playlist *px, struct imsgev *iev,
510 struct imsg *imsg)
512 size_t datalen;
513 char path[PATH_MAX] = { 0 };
514 const char *err = NULL;
516 datalen = IMSG_DATA_SIZE(*imsg);
517 if (datalen != sizeof(path)) {
518 err = "data size mismatch";
519 goto err;
522 memcpy(path, imsg->data, sizeof(path));
523 if (path[datalen-1] != '\0') {
524 err = "malformed data";
525 goto err;
528 if (tx)
529 playlist_push(px, path);
530 else
531 playlist_enqueue(path);
532 imsg_compose_event(iev, IMSG_CTL_ADD, 0, 0, -1, path, sizeof(path));
533 return;
534 err:
535 imsg_compose_event(iev, IMSG_CTL_ERR, 0, 0, -1, err, strlen(err)+1);
538 void
539 main_send_playlist(struct imsgev *iev)
541 struct player_status s;
542 size_t i;
544 for (i = 0; i < playlist.len; ++i) {
545 memset(&s, 0, sizeof(s));
546 strlcpy(s.path, playlist.songs[i], sizeof(s.path));
547 s.status = play_off == i ? STATE_PLAYING : STATE_STOPPED;
548 imsg_compose_event(iev, IMSG_CTL_SHOW, 0, 0, -1, &s,
549 sizeof(s));
552 imsg_compose_event(iev, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
555 void
556 main_send_status(struct imsgev *iev)
558 struct player_status s;
560 memset(&s, 0, sizeof(s));
562 if (current_song != NULL)
563 strlcpy(s.path, current_song, sizeof(s.path));
564 s.status = play_state;
565 s.rp.repeat_all = repeat_all;
566 s.rp.repeat_one = repeat_one;
568 imsg_compose_event(iev, IMSG_CTL_STATUS, 0, 0, -1, &s, sizeof(s));