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 main_playlist_advance();
140 if (play_state == STATE_PLAYING)
141 control_notify(NULL, IMSG_CTL_NEXT);
142 else
143 control_notify(NULL, IMSG_CTL_STOP);
144 break;
146 default:
147 log_debug("%s: error handling imsg %d", __func__,
148 imsg.hdr.type);
149 break;
151 imsg_free(&imsg);
154 if (!shut)
155 imsg_event_add(iev);
156 else {
157 /* This pipe is dead. Remove its event handler. */
158 event_del(&iev->ev);
159 event_loopexit(NULL);
163 static pid_t
164 start_child(enum amused_process proc, int fd)
166 const char *argv[6];
167 int argc = 0;
168 pid_t pid;
170 switch (pid = fork()) {
171 case -1:
172 fatal("cannot fork");
173 case 0:
174 break;
175 default:
176 close(fd);
177 return pid;
180 if (fd != 3) {
181 if (fd != -1 && dup2(fd, 3) == -1)
182 fatal("cannot setup imsg fd");
183 } else if (fcntl(F_SETFD, 0) == -1)
184 fatal("cannot setup imsg fd");
186 argv[argc++] = argv0;
188 switch (proc) {
189 case PROC_MAIN:
190 argv[argc++] = "-s";
191 argv[argc++] = csock;
192 break;
193 case PROC_PLAYER:
194 argv[argc++] = "-Tp";
195 break;
198 if (debug)
199 argv[argc++] = "-d";
200 if (verbose)
201 argv[argc++] = "-v";
202 argv[argc++] = NULL;
204 /* obnoxious casts */
205 execvp(argv0, (char *const *)argv);
206 fatal("execvp %s", argv0);
209 /* daemon main routine */
210 static __dead int
211 amused_main(void)
213 int pipe_main2player[2];
214 int control_fd;
216 log_init(debug, LOG_DAEMON);
217 log_setverbose(verbose);
218 log_procinit("main");
220 if (!debug)
221 daemon(1, 0);
223 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
224 PF_UNSPEC, pipe_main2player) == -1)
225 fatal("socketpair");
227 player_pid = start_child(PROC_PLAYER, pipe_main2player[1]);
229 event_init();
231 signal_set(&ev_sigint, SIGINT, main_sig_handler, NULL);
232 signal_set(&ev_sigterm, SIGTERM, main_sig_handler, NULL);
234 signal_add(&ev_sigint, NULL);
235 signal_add(&ev_sigterm, NULL);
237 signal(SIGHUP, SIG_IGN);
238 signal(SIGCHLD, SIG_IGN);
239 signal(SIGPIPE, SIG_IGN);
241 iev_player = xmalloc(sizeof(*iev_player));
242 imsg_init(&iev_player->ibuf, pipe_main2player[0]);
243 iev_player->handler = main_dispatch_player;
244 iev_player->events = EV_READ;
245 event_set(&iev_player->ev, iev_player->ibuf.fd, iev_player->events,
246 iev_player->handler, iev_player);
247 event_add(&iev_player->ev, NULL);
249 if ((control_fd = control_init(csock)) == -1)
250 fatal("control socket setup failed %s", csock);
251 control_listen(control_fd);
253 if (pledge("stdio rpath unix sendfd", NULL) == -1)
254 fatal("pledge");
256 log_info("startup");
257 event_dispatch();
258 main_shutdown();
261 int
262 main(int argc, char **argv)
264 int ch, proc = PROC_MAIN;
266 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized */
267 log_setverbose(1);
269 argv0 = argv[0];
270 if (argv0 == NULL)
271 argv0 = "amused";
273 while ((ch = getopt(argc, argv, "ds:T:v")) != -1) {
274 switch (ch) {
275 case 'd':
276 debug = 1;
277 break;
278 case 's':
279 free(csock);
280 csock = xstrdup(optarg);
281 break;
282 case 'T':
283 switch (*optarg) {
284 case 'p':
285 proc = PROC_PLAYER;
286 break;
287 default:
288 usage();
290 break;
291 case 'v':
292 verbose++;
293 break;
294 default:
295 usage();
298 argv += optind;
299 argc -= optind;
301 if (proc == PROC_PLAYER)
302 exit(player(debug, verbose));
304 if (csock == NULL)
305 xasprintf(&csock, "/tmp/amused-%d", getuid());
307 if (argc == 0)
308 amused_main();
309 else
310 ctl(argc, argv);
311 return 0;
314 void
315 spawn_daemon(void)
317 debug = 0;
318 start_child(PROC_MAIN, -1);
321 void
322 imsg_event_add(struct imsgev *iev)
324 iev->events = EV_READ;
325 if (iev->ibuf.w.queued)
326 iev->events |= EV_WRITE;
328 event_del(&iev->ev);
329 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
330 event_add(&iev->ev, NULL);
333 int
334 imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
335 pid_t pid, int fd, const void *data, uint16_t datalen)
337 int ret;
339 if ((ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data,
340 datalen)) != -1)
341 imsg_event_add(iev);
343 return ret;
346 int
347 main_send_player(uint16_t type, int fd, const void *data, uint16_t datalen)
349 return imsg_compose_event(iev_player, type, 0, 0, fd, data, datalen);
352 int
353 main_play_song(const char *song)
355 char path[PATH_MAX] = { 0 };
356 int fd;
358 strlcpy(path, song, sizeof(path));
359 if ((fd = open(path, O_RDONLY)) == -1) {
360 log_warn("open %s", path);
361 return 0;
364 play_state = STATE_PLAYING;
365 imsg_compose_event(iev_player, IMSG_PLAY, 0, 0, fd,
366 path, sizeof(path));
367 return 1;
370 void
371 main_playlist_jump(struct imsgev *iev, struct imsg *imsg)
373 size_t datalen;
374 char arg[PATH_MAX];
375 const char *song;
377 datalen = IMSG_DATA_SIZE(*imsg);
378 if (datalen != sizeof(arg)) {
379 main_senderr(iev, "wrong size");
380 return;
383 memcpy(arg, imsg->data, sizeof(arg));
384 if (arg[sizeof(arg)-1] != '\0') {
385 main_senderr(iev, "data corrupted");
386 return;
389 song = playlist_jump(arg);
390 if (song == NULL) {
391 main_senderr(iev, "not found");
392 return;
395 main_send_player(IMSG_STOP, -1, NULL, 0);
396 if (!main_play_song(song)) {
397 main_senderr(iev, "can't play");
398 playlist_dropcurrent();
399 main_playlist_advance();
400 return;
403 main_send_status(iev);
406 void
407 main_playlist_resume(void)
409 const char *song;
411 if ((song = current_song) == NULL)
412 song = playlist_advance();
414 for (; song != NULL; song = playlist_advance()) {
415 if (main_play_song(song))
416 return;
418 playlist_dropcurrent();
422 void
423 main_playlist_advance(void)
425 const char *song;
427 for (;;) {
428 song = playlist_advance();
429 if (song == NULL)
430 return;
432 if (main_play_song(song))
433 break;
435 playlist_dropcurrent();
439 void
440 main_playlist_previous(void)
442 const char *song;
444 for (;;) {
445 song = playlist_previous();
446 if (song == NULL)
447 return;
449 if (main_play_song(song))
450 break;
452 playlist_dropcurrent();
456 void
457 main_restart_track(void)
459 const char *song;
461 song = current_song;
462 if (song == NULL)
463 return;
465 if (main_play_song(song))
466 return;
468 playlist_dropcurrent();
469 main_playlist_advance();
472 void
473 main_senderr(struct imsgev *iev, const char *msg)
475 imsg_compose_event(iev, IMSG_CTL_ERR, 0, 0, -1,
476 msg, strlen(msg)+1);
479 void
480 main_enqueue(int tx, struct playlist *px, struct imsgev *iev,
481 struct imsg *imsg)
483 size_t datalen;
484 char path[PATH_MAX] = { 0 };
485 const char *err = NULL;
487 datalen = IMSG_DATA_SIZE(*imsg);
488 if (datalen != sizeof(path)) {
489 err = "data size mismatch";
490 goto err;
493 memcpy(path, imsg->data, sizeof(path));
494 if (path[datalen-1] != '\0') {
495 err = "malformed data";
496 goto err;
499 if (tx)
500 playlist_push(px, path);
501 else
502 playlist_enqueue(path);
503 imsg_compose_event(iev, IMSG_CTL_ADD, 0, 0, -1, path, sizeof(path));
504 return;
505 err:
506 imsg_compose_event(iev, IMSG_CTL_ERR, 0, 0, -1, err, strlen(err)+1);
509 void
510 main_send_playlist(struct imsgev *iev)
512 struct player_status s;
513 size_t i;
515 for (i = 0; i < playlist.len; ++i) {
516 memset(&s, 0, sizeof(s));
517 strlcpy(s.path, playlist.songs[i], sizeof(s.path));
518 s.status = play_off == i ? STATE_PLAYING : STATE_STOPPED;
519 imsg_compose_event(iev, IMSG_CTL_SHOW, 0, 0, -1, &s,
520 sizeof(s));
523 imsg_compose_event(iev, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
526 void
527 main_send_status(struct imsgev *iev)
529 struct player_status s;
531 memset(&s, 0, sizeof(s));
533 if (current_song != NULL)
534 strlcpy(s.path, current_song, sizeof(s.path));
535 s.status = play_state;
536 s.rp.repeat_all = repeat_all;
537 s.rp.repeat_one = repeat_one;
539 imsg_compose_event(iev, IMSG_CTL_STATUS, 0, 0, -1, &s, sizeof(s));