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 main_playlist_advance();
135 if (play_state == STATE_PLAYING)
136 control_notify(NULL, IMSG_CTL_NEXT);
137 else
138 control_notify(NULL, IMSG_CTL_STOP);
139 break;
140 case IMSG_EOF:
141 if (repeat_one && current_song != NULL)
142 if (main_play_song(current_song))
143 break;
144 main_playlist_advance();
145 if (play_state == STATE_PLAYING)
146 control_notify(NULL, IMSG_CTL_NEXT);
147 else
148 control_notify(NULL, IMSG_CTL_STOP);
149 break;
151 default:
152 log_debug("%s: error handling imsg %d", __func__,
153 imsg.hdr.type);
154 break;
156 imsg_free(&imsg);
159 if (!shut)
160 imsg_event_add(iev);
161 else {
162 /* This pipe is dead. Remove its event handler. */
163 event_del(&iev->ev);
164 event_loopexit(NULL);
168 static pid_t
169 start_child(enum amused_process proc, int fd)
171 const char *argv[6];
172 int argc = 0;
173 pid_t pid;
175 switch (pid = fork()) {
176 case -1:
177 fatal("cannot fork");
178 case 0:
179 break;
180 default:
181 close(fd);
182 return pid;
185 if (fd != 3) {
186 if (fd != -1 && dup2(fd, 3) == -1)
187 fatal("cannot setup imsg fd");
188 } else if (fcntl(F_SETFD, 0) == -1)
189 fatal("cannot setup imsg fd");
191 argv[argc++] = argv0;
193 switch (proc) {
194 case PROC_MAIN:
195 argv[argc++] = "-s";
196 argv[argc++] = csock;
197 break;
198 case PROC_PLAYER:
199 argv[argc++] = "-Tp";
200 break;
203 if (debug)
204 argv[argc++] = "-d";
205 if (verbose)
206 argv[argc++] = "-v";
207 argv[argc++] = NULL;
209 /* obnoxious casts */
210 execvp(argv0, (char *const *)argv);
211 fatal("execvp %s", argv0);
214 /* daemon main routine */
215 static __dead int
216 amused_main(void)
218 int pipe_main2player[2];
219 int control_fd;
221 log_init(debug, LOG_DAEMON);
222 log_setverbose(verbose);
223 log_procinit("main");
225 if (!debug)
226 daemon(1, 0);
228 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
229 PF_UNSPEC, pipe_main2player) == -1)
230 fatal("socketpair");
232 player_pid = start_child(PROC_PLAYER, pipe_main2player[1]);
234 event_init();
236 signal_set(&ev_sigint, SIGINT, main_sig_handler, NULL);
237 signal_set(&ev_sigterm, SIGTERM, main_sig_handler, NULL);
239 signal_add(&ev_sigint, NULL);
240 signal_add(&ev_sigterm, NULL);
242 signal(SIGHUP, SIG_IGN);
243 signal(SIGCHLD, SIG_IGN);
244 signal(SIGPIPE, SIG_IGN);
246 iev_player = xmalloc(sizeof(*iev_player));
247 imsg_init(&iev_player->ibuf, pipe_main2player[0]);
248 iev_player->handler = main_dispatch_player;
249 iev_player->events = EV_READ;
250 event_set(&iev_player->ev, iev_player->ibuf.fd, iev_player->events,
251 iev_player->handler, iev_player);
252 event_add(&iev_player->ev, NULL);
254 if ((control_fd = control_init(csock)) == -1)
255 fatal("control socket setup failed %s", csock);
256 control_listen(control_fd);
258 if (pledge("stdio rpath unix sendfd", NULL) == -1)
259 fatal("pledge");
261 log_info("startup");
262 event_dispatch();
263 main_shutdown();
266 int
267 main(int argc, char **argv)
269 int ch, proc = PROC_MAIN;
271 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized */
272 log_setverbose(1);
274 argv0 = argv[0];
275 if (argv0 == NULL)
276 argv0 = "amused";
278 while ((ch = getopt(argc, argv, "ds:T:v")) != -1) {
279 switch (ch) {
280 case 'd':
281 debug = 1;
282 break;
283 case 's':
284 free(csock);
285 csock = xstrdup(optarg);
286 break;
287 case 'T':
288 switch (*optarg) {
289 case 'p':
290 proc = PROC_PLAYER;
291 break;
292 default:
293 usage();
295 break;
296 case 'v':
297 verbose++;
298 break;
299 default:
300 usage();
303 argv += optind;
304 argc -= optind;
306 if (proc == PROC_PLAYER)
307 exit(player(debug, verbose));
309 if (csock == NULL)
310 xasprintf(&csock, "/tmp/amused-%d", getuid());
312 if (argc == 0)
313 amused_main();
314 else
315 ctl(argc, argv);
316 return 0;
319 void
320 spawn_daemon(void)
322 debug = 0;
323 start_child(PROC_MAIN, -1);
326 void
327 imsg_event_add(struct imsgev *iev)
329 iev->events = EV_READ;
330 if (iev->ibuf.w.queued)
331 iev->events |= EV_WRITE;
333 event_del(&iev->ev);
334 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
335 event_add(&iev->ev, NULL);
338 int
339 imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
340 pid_t pid, int fd, const void *data, uint16_t datalen)
342 int ret;
344 if ((ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data,
345 datalen)) != -1)
346 imsg_event_add(iev);
348 return ret;
351 int
352 main_send_player(uint16_t type, int fd, const void *data, uint16_t datalen)
354 return imsg_compose_event(iev_player, type, 0, 0, fd, data, datalen);
357 int
358 main_play_song(const char *song)
360 char path[PATH_MAX] = { 0 };
361 int fd;
363 strlcpy(path, song, sizeof(path));
364 if ((fd = open(path, O_RDONLY)) == -1) {
365 log_warn("open %s", path);
366 return 0;
369 play_state = STATE_PLAYING;
370 imsg_compose_event(iev_player, IMSG_PLAY, 0, 0, fd,
371 path, sizeof(path));
372 return 1;
375 void
376 main_playlist_jump(struct imsgev *iev, struct imsg *imsg)
378 size_t datalen;
379 char arg[PATH_MAX];
380 const char *song;
382 datalen = IMSG_DATA_SIZE(*imsg);
383 if (datalen != sizeof(arg)) {
384 main_senderr(iev, "wrong size");
385 return;
388 memcpy(arg, imsg->data, sizeof(arg));
389 if (arg[sizeof(arg)-1] != '\0') {
390 main_senderr(iev, "data corrupted");
391 return;
394 song = playlist_jump(arg);
395 if (song == NULL) {
396 main_senderr(iev, "not found");
397 return;
400 main_send_player(IMSG_STOP, -1, NULL, 0);
401 if (!main_play_song(song)) {
402 main_senderr(iev, "can't play");
403 playlist_dropcurrent();
404 main_playlist_advance();
405 return;
408 main_send_status(iev);
411 void
412 main_playlist_resume(void)
414 const char *song;
416 if ((song = current_song) == NULL)
417 song = playlist_advance();
419 for (; song != NULL; song = playlist_advance()) {
420 if (main_play_song(song))
421 return;
423 playlist_dropcurrent();
427 void
428 main_playlist_advance(void)
430 const char *song;
432 for (;;) {
433 song = playlist_advance();
434 if (song == NULL)
435 return;
437 if (main_play_song(song))
438 break;
440 playlist_dropcurrent();
444 void
445 main_playlist_previous(void)
447 const char *song;
449 for (;;) {
450 song = playlist_previous();
451 if (song == NULL)
452 return;
454 if (main_play_song(song))
455 break;
457 playlist_dropcurrent();
461 void
462 main_restart_track(void)
464 const char *song;
466 song = current_song;
467 if (song == NULL)
468 return;
470 if (main_play_song(song))
471 return;
473 playlist_dropcurrent();
474 main_playlist_advance();
477 void
478 main_senderr(struct imsgev *iev, const char *msg)
480 imsg_compose_event(iev, IMSG_CTL_ERR, 0, 0, -1,
481 msg, strlen(msg)+1);
484 void
485 main_enqueue(int tx, struct playlist *px, struct imsgev *iev,
486 struct imsg *imsg)
488 size_t datalen;
489 char path[PATH_MAX] = { 0 };
490 const char *err = NULL;
492 datalen = IMSG_DATA_SIZE(*imsg);
493 if (datalen != sizeof(path)) {
494 err = "data size mismatch";
495 goto err;
498 memcpy(path, imsg->data, sizeof(path));
499 if (path[datalen-1] != '\0') {
500 err = "malformed data";
501 goto err;
504 if (tx)
505 playlist_push(px, path);
506 else
507 playlist_enqueue(path);
508 imsg_compose_event(iev, IMSG_CTL_ADD, 0, 0, -1, path, sizeof(path));
509 return;
510 err:
511 imsg_compose_event(iev, IMSG_CTL_ERR, 0, 0, -1, err, strlen(err)+1);
514 void
515 main_send_playlist(struct imsgev *iev)
517 struct player_status s;
518 size_t i;
520 for (i = 0; i < playlist.len; ++i) {
521 memset(&s, 0, sizeof(s));
522 strlcpy(s.path, playlist.songs[i], sizeof(s.path));
523 s.status = play_off == i ? STATE_PLAYING : STATE_STOPPED;
524 imsg_compose_event(iev, IMSG_CTL_SHOW, 0, 0, -1, &s,
525 sizeof(s));
528 imsg_compose_event(iev, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
531 void
532 main_send_status(struct imsgev *iev)
534 struct player_status s;
536 memset(&s, 0, sizeof(s));
538 if (current_song != NULL)
539 strlcpy(s.path, current_song, sizeof(s.path));
540 s.status = play_state;
541 s.rp.repeat_all = repeat_all;
542 s.rp.repeat_one = repeat_one;
544 imsg_compose_event(iev, IMSG_CTL_STATUS, 0, 0, -1, &s, sizeof(s));