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 if (play_state == STATE_PLAYING)
166 control_notify(NULL, IMSG_CTL_NEXT);
167 else
168 control_notify(NULL, IMSG_CTL_STOP);
169 break;
170 case IMSG_EOF:
171 if (repeat_one && current_song != NULL)
172 if (main_play_song(current_song))
173 break;
174 main_playlist_advance();
175 if (play_state == STATE_PLAYING)
176 control_notify(NULL, IMSG_CTL_NEXT);
177 else
178 control_notify(NULL, IMSG_CTL_STOP);
179 break;
181 default:
182 log_debug("%s: error handling imsg %d", __func__,
183 imsg.hdr.type);
184 break;
186 imsg_free(&imsg);
189 if (!shut)
190 imsg_event_add(iev);
191 else {
192 /* This pipe is dead. Remove its event handler. */
193 event_del(&iev->ev);
194 event_loopexit(NULL);
198 static pid_t
199 start_child(enum amused_process proc, int fd)
201 const char *argv[6];
202 int argc = 0;
203 pid_t pid;
205 switch (pid = fork()) {
206 case -1:
207 fatal("cannot fork");
208 case 0:
209 break;
210 default:
211 close(fd);
212 return pid;
215 if (fd != 3) {
216 if (fd != -1 && dup2(fd, 3) == -1)
217 fatal("cannot setup imsg fd");
218 } else if (fcntl(F_SETFD, 0) == -1)
219 fatal("cannot setup imsg fd");
221 argv[argc++] = argv0;
223 switch (proc) {
224 case PROC_MAIN:
225 argv[argc++] = "-s";
226 argv[argc++] = csock;
227 break;
228 case PROC_PLAYER:
229 argv[argc++] = "-Tp";
230 break;
233 if (debug)
234 argv[argc++] = "-d";
235 if (verbose)
236 argv[argc++] = "-v";
237 argv[argc++] = NULL;
239 /* obnoxious casts */
240 execvp(argv0, (char *const *)argv);
241 fatal("execvp %s", argv0);
244 /* daemon main routine */
245 static __dead int
246 amused_main(void)
248 int pipe_main2player[2];
249 int control_fd;
251 log_init(debug, LOG_DAEMON);
252 log_setverbose(verbose);
253 log_procinit("main");
255 if (!debug)
256 daemon(1, 0);
258 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
259 PF_UNSPEC, pipe_main2player) == -1)
260 fatal("socketpair");
262 player_pid = start_child(PROC_PLAYER, pipe_main2player[1]);
264 event_init();
266 signal_set(&ev_sigint, SIGINT, main_sig_handler, NULL);
267 signal_set(&ev_sigterm, SIGTERM, main_sig_handler, NULL);
268 signal_set(&ev_siginfo, SIGINFO, main_sig_handler, NULL);
270 signal_add(&ev_sigint, NULL);
271 signal_add(&ev_sigterm, NULL);
272 signal_add(&ev_siginfo, NULL);
274 signal(SIGHUP, SIG_IGN);
275 signal(SIGCHLD, SIG_IGN);
276 signal(SIGPIPE, SIG_IGN);
278 iev_player = xmalloc(sizeof(*iev_player));
279 imsg_init(&iev_player->ibuf, pipe_main2player[0]);
280 iev_player->handler = main_dispatch_player;
281 iev_player->events = EV_READ;
282 event_set(&iev_player->ev, iev_player->ibuf.fd, iev_player->events,
283 iev_player->handler, iev_player);
284 event_add(&iev_player->ev, NULL);
286 if ((control_fd = control_init(csock)) == -1)
287 fatal("control socket setup failed %s", csock);
288 control_listen(control_fd);
290 if (pledge("stdio rpath unix sendfd", NULL) == -1)
291 fatal("pledge");
293 log_info("startup");
294 event_dispatch();
295 main_shutdown();
298 int
299 main(int argc, char **argv)
301 int ch, proc = PROC_MAIN;
303 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized */
304 log_setverbose(1);
306 argv0 = argv[0];
307 if (argv0 == NULL)
308 argv0 = "amused";
310 while ((ch = getopt(argc, argv, "ds:T:v")) != -1) {
311 switch (ch) {
312 case 'd':
313 debug = 1;
314 break;
315 case 's':
316 free(csock);
317 csock = xstrdup(optarg);
318 break;
319 case 'T':
320 switch (*optarg) {
321 case 'p':
322 proc = PROC_PLAYER;
323 break;
324 default:
325 usage();
327 break;
328 case 'v':
329 verbose++;
330 break;
331 default:
332 usage();
335 argv += optind;
336 argc -= optind;
338 if (proc == PROC_PLAYER)
339 exit(player(debug, verbose));
341 if (csock == NULL)
342 xasprintf(&csock, "/tmp/amused-%d", getuid());
344 if (argc == 0)
345 amused_main();
346 else
347 ctl(argc, argv);
348 return 0;
351 void
352 spawn_daemon(void)
354 debug = 0;
355 start_child(PROC_MAIN, -1);
358 void
359 imsg_event_add(struct imsgev *iev)
361 iev->events = EV_READ;
362 if (iev->ibuf.w.queued)
363 iev->events |= EV_WRITE;
365 event_del(&iev->ev);
366 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
367 event_add(&iev->ev, NULL);
370 int
371 imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
372 pid_t pid, int fd, const void *data, uint16_t datalen)
374 int ret;
376 if ((ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data,
377 datalen)) != -1)
378 imsg_event_add(iev);
380 return ret;
383 int
384 main_send_player(uint16_t type, int fd, const void *data, uint16_t datalen)
386 return imsg_compose_event(iev_player, type, 0, 0, fd, data, datalen);
389 int
390 main_play_song(const char *song)
392 char path[PATH_MAX] = { 0 };
393 int fd;
395 strlcpy(path, song, sizeof(path));
396 if ((fd = open(path, O_RDONLY)) == -1) {
397 log_warn("open %s", path);
398 return 0;
401 play_state = STATE_PLAYING;
402 imsg_compose_event(iev_player, IMSG_PLAY, 0, 0, fd,
403 path, sizeof(path));
404 return 1;
407 void
408 main_playlist_jump(struct imsgev *iev, struct imsg *imsg)
410 size_t datalen;
411 char arg[PATH_MAX];
412 const char *song;
414 datalen = IMSG_DATA_SIZE(*imsg);
415 if (datalen != sizeof(arg)) {
416 main_senderr(iev, "wrong size");
417 return;
420 memcpy(arg, imsg->data, sizeof(arg));
421 if (arg[sizeof(arg)-1] != '\0') {
422 main_senderr(iev, "data corrupted");
423 return;
426 song = playlist_jump(arg);
427 if (song == NULL) {
428 main_senderr(iev, "not found");
429 return;
432 main_send_player(IMSG_STOP, -1, NULL, 0);
433 if (!main_play_song(song)) {
434 main_senderr(iev, "can't play");
435 playlist_dropcurrent();
436 main_playlist_advance();
437 return;
440 main_send_status(iev);
443 void
444 main_playlist_resume(void)
446 const char *song;
448 if ((song = current_song) == NULL)
449 song = playlist_advance();
451 for (; song != NULL; song = playlist_advance()) {
452 if (main_play_song(song))
453 return;
455 playlist_dropcurrent();
459 void
460 main_playlist_advance(void)
462 const char *song;
464 for (;;) {
465 song = playlist_advance();
466 if (song == NULL)
467 return;
469 if (main_play_song(song))
470 break;
472 playlist_dropcurrent();
476 void
477 main_playlist_previous(void)
479 const char *song;
481 for (;;) {
482 song = playlist_previous();
483 if (song == NULL)
484 return;
486 if (main_play_song(song))
487 break;
489 playlist_dropcurrent();
493 void
494 main_restart_track(void)
496 const char *song;
498 song = current_song;
499 if (song == NULL)
500 return;
502 if (main_play_song(song))
503 return;
505 playlist_dropcurrent();
506 main_playlist_advance();
509 void
510 main_senderr(struct imsgev *iev, const char *msg)
512 imsg_compose_event(iev, IMSG_CTL_ERR, 0, 0, -1,
513 msg, strlen(msg)+1);
516 void
517 main_enqueue(int tx, struct playlist *px, struct imsgev *iev,
518 struct imsg *imsg)
520 size_t datalen;
521 char path[PATH_MAX] = { 0 };
522 const char *err = NULL;
524 datalen = IMSG_DATA_SIZE(*imsg);
525 if (datalen != sizeof(path)) {
526 err = "data size mismatch";
527 goto err;
530 memcpy(path, imsg->data, sizeof(path));
531 if (path[datalen-1] != '\0') {
532 err = "malformed data";
533 goto err;
536 if (tx)
537 playlist_push(px, path);
538 else
539 playlist_enqueue(path);
540 imsg_compose_event(iev, IMSG_CTL_ADD, 0, 0, -1, path, sizeof(path));
541 return;
542 err:
543 imsg_compose_event(iev, IMSG_CTL_ERR, 0, 0, -1, err, strlen(err)+1);
546 void
547 main_send_playlist(struct imsgev *iev)
549 struct player_status s;
550 size_t i;
552 for (i = 0; i < playlist.len; ++i) {
553 memset(&s, 0, sizeof(s));
554 strlcpy(s.path, playlist.songs[i], sizeof(s.path));
555 s.status = play_off == i ? STATE_PLAYING : STATE_STOPPED;
556 imsg_compose_event(iev, IMSG_CTL_SHOW, 0, 0, -1, &s,
557 sizeof(s));
560 imsg_compose_event(iev, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
563 void
564 main_send_status(struct imsgev *iev)
566 struct player_status s;
568 memset(&s, 0, sizeof(s));
570 if (current_song != NULL)
571 strlcpy(s.path, current_song, sizeof(s.path));
572 s.status = play_state;
573 s.rp.repeat_all = repeat_all;
574 s.rp.repeat_one = repeat_one;
576 imsg_compose_event(iev, IMSG_CTL_STATUS, 0, 0, -1, &s, sizeof(s));