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:vV")) != -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 case 'V':
324 printf("%s version %s\n", getprogname(),
325 AMUSED_VERSION);
326 exit(0);
327 default:
328 usage();
331 argv += optind;
332 argc -= optind;
334 if (proc == PROC_PLAYER)
335 exit(player(debug, verbose));
337 if (csock == NULL)
338 xasprintf(&csock, "/tmp/amused-%d", getuid());
340 if (argc == 0)
341 amused_main();
342 else
343 ctl(argc, argv);
344 return 0;
347 void
348 spawn_daemon(void)
350 debug = 0;
351 start_child(PROC_MAIN, -1);
354 void
355 imsg_event_add(struct imsgev *iev)
357 iev->events = EV_READ;
358 if (iev->ibuf.w.queued)
359 iev->events |= EV_WRITE;
361 event_del(&iev->ev);
362 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
363 event_add(&iev->ev, NULL);
366 int
367 imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
368 pid_t pid, int fd, const void *data, uint16_t datalen)
370 int ret;
372 if ((ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data,
373 datalen)) != -1)
374 imsg_event_add(iev);
376 return ret;
379 int
380 main_send_player(uint16_t type, int fd, const void *data, uint16_t datalen)
382 return imsg_compose_event(iev_player, type, 0, 0, fd, data, datalen);
385 int
386 main_play_song(const char *song)
388 char path[PATH_MAX] = { 0 };
389 int fd;
391 strlcpy(path, song, sizeof(path));
392 if ((fd = open(path, O_RDONLY)) == -1) {
393 log_warn("open %s", path);
394 return 0;
397 play_state = STATE_PLAYING;
398 imsg_compose_event(iev_player, IMSG_PLAY, 0, 0, fd,
399 path, sizeof(path));
400 return 1;
403 void
404 main_playlist_jump(struct imsgev *iev, struct imsg *imsg)
406 size_t datalen;
407 char arg[PATH_MAX];
408 const char *song;
410 datalen = IMSG_DATA_SIZE(*imsg);
411 if (datalen != sizeof(arg)) {
412 main_senderr(iev, "wrong size");
413 return;
416 memcpy(arg, imsg->data, sizeof(arg));
417 if (arg[sizeof(arg)-1] != '\0') {
418 main_senderr(iev, "data corrupted");
419 return;
422 song = playlist_jump(arg);
423 if (song == NULL) {
424 main_senderr(iev, "not found");
425 return;
428 main_send_player(IMSG_STOP, -1, NULL, 0);
429 if (!main_play_song(song)) {
430 main_senderr(iev, "can't play");
431 playlist_dropcurrent();
432 main_playlist_advance();
433 return;
436 main_send_status(iev);
439 void
440 main_playlist_resume(void)
442 const char *song;
444 if ((song = current_song) == NULL)
445 song = playlist_advance();
447 for (; song != NULL; song = playlist_advance()) {
448 if (main_play_song(song))
449 return;
451 playlist_dropcurrent();
455 void
456 main_playlist_advance(void)
458 const char *song;
460 for (;;) {
461 song = playlist_advance();
462 if (song == NULL)
463 return;
465 if (main_play_song(song))
466 break;
468 playlist_dropcurrent();
472 void
473 main_playlist_previous(void)
475 const char *song;
477 for (;;) {
478 song = playlist_previous();
479 if (song == NULL)
480 return;
482 if (main_play_song(song))
483 break;
485 playlist_dropcurrent();
489 void
490 main_restart_track(void)
492 const char *song;
494 song = current_song;
495 if (song == NULL)
496 return;
498 if (main_play_song(song))
499 return;
501 playlist_dropcurrent();
502 main_playlist_advance();
505 void
506 main_senderr(struct imsgev *iev, const char *msg)
508 imsg_compose_event(iev, IMSG_CTL_ERR, 0, 0, -1,
509 msg, strlen(msg)+1);
512 void
513 main_enqueue(int tx, struct playlist *px, struct imsgev *iev,
514 struct imsg *imsg)
516 size_t datalen;
517 char path[PATH_MAX] = { 0 };
518 const char *err = NULL;
520 datalen = IMSG_DATA_SIZE(*imsg);
521 if (datalen != sizeof(path)) {
522 err = "data size mismatch";
523 goto err;
526 memcpy(path, imsg->data, sizeof(path));
527 if (path[datalen-1] != '\0') {
528 err = "malformed data";
529 goto err;
532 if (tx)
533 playlist_push(px, path);
534 else
535 playlist_enqueue(path);
536 imsg_compose_event(iev, IMSG_CTL_ADD, 0, 0, -1, path, sizeof(path));
537 return;
538 err:
539 imsg_compose_event(iev, IMSG_CTL_ERR, 0, 0, -1, err, strlen(err)+1);
542 void
543 main_send_playlist(struct imsgev *iev)
545 struct player_status s;
546 size_t i;
548 for (i = 0; i < playlist.len; ++i) {
549 memset(&s, 0, sizeof(s));
550 strlcpy(s.path, playlist.songs[i], sizeof(s.path));
551 s.status = play_off == i ? STATE_PLAYING : STATE_STOPPED;
552 imsg_compose_event(iev, IMSG_CTL_SHOW, 0, 0, -1, &s,
553 sizeof(s));
556 imsg_compose_event(iev, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
559 void
560 main_send_status(struct imsgev *iev)
562 struct player_status s;
564 memset(&s, 0, sizeof(s));
566 if (current_song != NULL)
567 strlcpy(s.path, current_song, sizeof(s.path));
568 s.status = play_state;
570 imsg_compose_event(iev, IMSG_CTL_STATUS, 0, 0, -1, &s, sizeof(s));