2 3baa2617 2022-02-16 op * Copyright (c) 2022 Omar Polo <op@openbsd.org>
4 3baa2617 2022-02-16 op * Permission to use, copy, modify, and distribute this software for any
5 3baa2617 2022-02-16 op * purpose with or without fee is hereby granted, provided that the above
6 3baa2617 2022-02-16 op * copyright notice and this permission notice appear in all copies.
8 3baa2617 2022-02-16 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 3baa2617 2022-02-16 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 3baa2617 2022-02-16 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 3baa2617 2022-02-16 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 3baa2617 2022-02-16 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 3baa2617 2022-02-16 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 3baa2617 2022-02-16 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 3baa2617 2022-02-16 op #include <sys/types.h>
18 3baa2617 2022-02-16 op #include <sys/queue.h>
19 3baa2617 2022-02-16 op #include <sys/socket.h>
20 dfe2ad96 2022-02-24 op #include <sys/stat.h>
21 3baa2617 2022-02-16 op #include <sys/uio.h>
22 3baa2617 2022-02-16 op #include <sys/wait.h>
24 3baa2617 2022-02-16 op #include <event.h>
25 3baa2617 2022-02-16 op #include <errno.h>
26 3baa2617 2022-02-16 op #include <fcntl.h>
27 3baa2617 2022-02-16 op #include <limits.h>
28 3baa2617 2022-02-16 op #include <sndio.h>
29 3baa2617 2022-02-16 op #include <signal.h>
30 3baa2617 2022-02-16 op #include <stdio.h>
31 3baa2617 2022-02-16 op #include <stdlib.h>
32 3baa2617 2022-02-16 op #include <stdint.h>
33 3baa2617 2022-02-16 op #include <string.h>
34 3baa2617 2022-02-16 op #include <syslog.h>
35 3baa2617 2022-02-16 op #include <unistd.h>
36 3baa2617 2022-02-16 op #include <imsg.h>
38 3baa2617 2022-02-16 op #include "amused.h"
39 3baa2617 2022-02-16 op #include "control.h"
40 3baa2617 2022-02-16 op #include "log.h"
41 3baa2617 2022-02-16 op #include "playlist.h"
42 3baa2617 2022-02-16 op #include "xmalloc.h"
44 3baa2617 2022-02-16 op struct sio_hdl *hdl;
45 3baa2617 2022-02-16 op char *csock = NULL;
48 3baa2617 2022-02-16 op struct imsgev *iev_player;
50 3baa2617 2022-02-16 op const char *argv0;
51 3baa2617 2022-02-16 op pid_t player_pid;
52 3baa2617 2022-02-16 op struct event ev_sigint;
53 3baa2617 2022-02-16 op struct event ev_sigterm;
55 3baa2617 2022-02-16 op enum amused_process {
61 3baa2617 2022-02-16 op main_shutdown(void)
66 3baa2617 2022-02-16 op /* close pipes. */
67 3baa2617 2022-02-16 op msgbuf_clear(&iev_player->ibuf.w);
68 3baa2617 2022-02-16 op close(iev_player->ibuf.fd);
69 3baa2617 2022-02-16 op free(iev_player);
71 3baa2617 2022-02-16 op log_debug("waiting for children to terminate");
73 3baa2617 2022-02-16 op pid = wait(&status);
74 3baa2617 2022-02-16 op if (pid == -1) {
75 3baa2617 2022-02-16 op if (errno != EINTR && errno != ECHILD)
76 3baa2617 2022-02-16 op fatal("wait");
77 3baa2617 2022-02-16 op } else if (WIFSIGNALED(status))
78 3baa2617 2022-02-16 op log_warnx("player terminated; signal %d",
79 3baa2617 2022-02-16 op WTERMSIG(status));
80 3baa2617 2022-02-16 op } while (pid != -1 || (pid == -1 && errno == EINTR));
82 3baa2617 2022-02-16 op log_info("terminating");
87 3baa2617 2022-02-16 op main_sig_handler(int sig, short event, void *arg)
90 3baa2617 2022-02-16 op * Normal signal handler rules don't apply because libevent
91 3baa2617 2022-02-16 op * decouples for us.
94 3baa2617 2022-02-16 op switch (sig) {
97 3baa2617 2022-02-16 op main_shutdown();
100 3baa2617 2022-02-16 op fatalx("unexpected signal %d", sig);
105 3baa2617 2022-02-16 op main_dispatch_player(int sig, short event, void *d)
107 3baa2617 2022-02-16 op struct imsgev *iev = d;
108 3baa2617 2022-02-16 op struct imsgbuf *ibuf = &iev->ibuf;
109 3baa2617 2022-02-16 op struct imsg imsg;
111 3baa2617 2022-02-16 op int shut = 0;
113 3baa2617 2022-02-16 op if (event & EV_READ) {
114 3baa2617 2022-02-16 op if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
115 3baa2617 2022-02-16 op fatal("imsg_read error");
116 3baa2617 2022-02-16 op if (n == 0) /* Connection closed */
119 3baa2617 2022-02-16 op if (event & EV_WRITE) {
120 3baa2617 2022-02-16 op if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
121 3baa2617 2022-02-16 op fatal("msgbuf_write");
122 3baa2617 2022-02-16 op if (n == 0) /* Connection closed */
127 3baa2617 2022-02-16 op if ((n = imsg_get(ibuf, &imsg)) == -1)
128 3baa2617 2022-02-16 op fatal("imsg_get");
129 3baa2617 2022-02-16 op if (n == 0) /* No more messages. */
132 3baa2617 2022-02-16 op switch (imsg.hdr.type) {
133 3baa2617 2022-02-16 op case IMSG_ERR:
134 13b83883 2022-02-16 op playlist_dropcurrent();
135 601093db 2022-02-22 op /* fallthrough */
136 aaccc123 2022-02-16 op case IMSG_EOF:
137 0be6c0b1 2022-02-22 op if (repeat_one && current_song != NULL) {
138 f3bd773b 2022-02-19 op if (main_play_song(current_song))
140 0be6c0b1 2022-02-22 op playlist_dropcurrent();
142 aaccc123 2022-02-16 op main_playlist_advance();
143 87f575c3 2022-02-21 op if (play_state == STATE_PLAYING)
144 87f575c3 2022-02-21 op control_notify(NULL, IMSG_CTL_NEXT);
146 87f575c3 2022-02-21 op control_notify(NULL, IMSG_CTL_STOP);
149 3baa2617 2022-02-16 op log_debug("%s: error handling imsg %d", __func__,
150 3baa2617 2022-02-16 op imsg.hdr.type);
153 3baa2617 2022-02-16 op imsg_free(&imsg);
157 3baa2617 2022-02-16 op imsg_event_add(iev);
159 3baa2617 2022-02-16 op /* This pipe is dead. Remove its event handler. */
160 3baa2617 2022-02-16 op event_del(&iev->ev);
161 3baa2617 2022-02-16 op event_loopexit(NULL);
166 3baa2617 2022-02-16 op start_child(enum amused_process proc, int fd)
168 1d673950 2022-03-03 op const char *argv[7];
169 3baa2617 2022-02-16 op int argc = 0;
172 1d673950 2022-03-03 op if (fd == -1 && debug)
175 3baa2617 2022-02-16 op switch (pid = fork()) {
177 3baa2617 2022-02-16 op fatal("cannot fork");
185 3baa2617 2022-02-16 op if (fd != 3) {
186 fea541a8 2022-02-16 op if (fd != -1 && dup2(fd, 3) == -1)
187 3baa2617 2022-02-16 op fatal("cannot setup imsg fd");
188 3baa2617 2022-02-16 op } else if (fcntl(F_SETFD, 0) == -1)
189 3baa2617 2022-02-16 op fatal("cannot setup imsg fd");
192 3baa2617 2022-02-16 op argv[argc++] = argv0;
194 3baa2617 2022-02-16 op switch (proc) {
195 3baa2617 2022-02-16 op case PROC_MAIN:
196 4c86871a 2022-02-17 op argv[argc++] = "-s";
197 4c86871a 2022-02-17 op argv[argc++] = csock;
198 1d673950 2022-03-03 op argv[argc++] = "-Tm";
200 3baa2617 2022-02-16 op case PROC_PLAYER:
201 3baa2617 2022-02-16 op argv[argc++] = "-Tp";
206 3baa2617 2022-02-16 op argv[argc++] = "-d";
208 3baa2617 2022-02-16 op argv[argc++] = "-v";
209 3baa2617 2022-02-16 op argv[argc++] = NULL;
211 3baa2617 2022-02-16 op /* obnoxious casts */
212 3baa2617 2022-02-16 op execvp(argv0, (char *const *)argv);
213 3baa2617 2022-02-16 op fatal("execvp %s", argv0);
216 3baa2617 2022-02-16 op /* daemon main routine */
217 3baa2617 2022-02-16 op static __dead int
218 3baa2617 2022-02-16 op amused_main(void)
220 3baa2617 2022-02-16 op int pipe_main2player[2];
221 3baa2617 2022-02-16 op int control_fd;
223 3baa2617 2022-02-16 op log_init(debug, LOG_DAEMON);
224 3baa2617 2022-02-16 op log_setverbose(verbose);
225 3baa2617 2022-02-16 op log_procinit("main");
228 3baa2617 2022-02-16 op daemon(1, 0);
230 3baa2617 2022-02-16 op if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
231 3baa2617 2022-02-16 op PF_UNSPEC, pipe_main2player) == -1)
232 3baa2617 2022-02-16 op fatal("socketpair");
234 3baa2617 2022-02-16 op player_pid = start_child(PROC_PLAYER, pipe_main2player[1]);
236 3baa2617 2022-02-16 op event_init();
238 3baa2617 2022-02-16 op signal_set(&ev_sigint, SIGINT, main_sig_handler, NULL);
239 3baa2617 2022-02-16 op signal_set(&ev_sigterm, SIGTERM, main_sig_handler, NULL);
241 3baa2617 2022-02-16 op signal_add(&ev_sigint, NULL);
242 3baa2617 2022-02-16 op signal_add(&ev_sigterm, NULL);
244 3baa2617 2022-02-16 op signal(SIGHUP, SIG_IGN);
245 3baa2617 2022-02-16 op signal(SIGCHLD, SIG_IGN);
246 3baa2617 2022-02-16 op signal(SIGPIPE, SIG_IGN);
248 3baa2617 2022-02-16 op iev_player = xmalloc(sizeof(*iev_player));
249 3baa2617 2022-02-16 op imsg_init(&iev_player->ibuf, pipe_main2player[0]);
250 3baa2617 2022-02-16 op iev_player->handler = main_dispatch_player;
251 3baa2617 2022-02-16 op iev_player->events = EV_READ;
252 3baa2617 2022-02-16 op event_set(&iev_player->ev, iev_player->ibuf.fd, iev_player->events,
253 3baa2617 2022-02-16 op iev_player->handler, iev_player);
254 3baa2617 2022-02-16 op event_add(&iev_player->ev, NULL);
256 3baa2617 2022-02-16 op if ((control_fd = control_init(csock)) == -1)
257 3baa2617 2022-02-16 op fatal("control socket setup failed %s", csock);
258 3baa2617 2022-02-16 op control_listen(control_fd);
260 3baa2617 2022-02-16 op if (pledge("stdio rpath unix sendfd", NULL) == -1)
261 3baa2617 2022-02-16 op fatal("pledge");
263 3baa2617 2022-02-16 op log_info("startup");
264 3baa2617 2022-02-16 op event_dispatch();
265 3baa2617 2022-02-16 op main_shutdown();
269 3baa2617 2022-02-16 op main(int argc, char **argv)
271 1d673950 2022-03-03 op int ch, proc = -1;
273 3baa2617 2022-02-16 op log_init(1, LOG_DAEMON); /* Log to stderr until daemonized */
274 3baa2617 2022-02-16 op log_setverbose(1);
276 3baa2617 2022-02-16 op argv0 = argv[0];
277 3baa2617 2022-02-16 op if (argv0 == NULL)
278 3baa2617 2022-02-16 op argv0 = "amused";
280 d307e5a2 2022-02-19 op while ((ch = getopt(argc, argv, "ds:T:v")) != -1) {
281 3baa2617 2022-02-16 op switch (ch) {
287 3baa2617 2022-02-16 op csock = xstrdup(optarg);
290 3baa2617 2022-02-16 op switch (*optarg) {
292 1d673950 2022-03-03 op proc = PROC_MAIN;
295 3baa2617 2022-02-16 op proc = PROC_PLAYER;
308 3baa2617 2022-02-16 op argv += optind;
309 3baa2617 2022-02-16 op argc -= optind;
311 1d673950 2022-03-03 op if (proc == PROC_MAIN)
312 1d673950 2022-03-03 op amused_main();
313 3baa2617 2022-02-16 op if (proc == PROC_PLAYER)
314 3baa2617 2022-02-16 op exit(player(debug, verbose));
316 3baa2617 2022-02-16 op if (csock == NULL)
317 3baa2617 2022-02-16 op xasprintf(&csock, "/tmp/amused-%d", getuid());
319 1d673950 2022-03-03 op if (argc > 0)
322 1d673950 2022-03-03 op ctl(argc, argv);
326 fea541a8 2022-02-16 op spawn_daemon(void)
328 fea541a8 2022-02-16 op start_child(PROC_MAIN, -1);
332 3baa2617 2022-02-16 op imsg_event_add(struct imsgev *iev)
334 3baa2617 2022-02-16 op iev->events = EV_READ;
335 3baa2617 2022-02-16 op if (iev->ibuf.w.queued)
336 3baa2617 2022-02-16 op iev->events |= EV_WRITE;
338 3baa2617 2022-02-16 op event_del(&iev->ev);
339 3baa2617 2022-02-16 op event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
340 3baa2617 2022-02-16 op event_add(&iev->ev, NULL);
344 3baa2617 2022-02-16 op imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
345 3baa2617 2022-02-16 op pid_t pid, int fd, const void *data, uint16_t datalen)
349 3baa2617 2022-02-16 op if ((ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data,
350 3baa2617 2022-02-16 op datalen)) != -1)
351 3baa2617 2022-02-16 op imsg_event_add(iev);
357 06961b72 2022-02-16 op main_send_player(uint16_t type, int fd, const void *data, uint16_t datalen)
359 06961b72 2022-02-16 op return imsg_compose_event(iev_player, type, 0, 0, fd, data, datalen);
363 3baa2617 2022-02-16 op main_play_song(const char *song)
365 dfe2ad96 2022-02-24 op struct stat sb;
366 3baa2617 2022-02-16 op char path[PATH_MAX] = { 0 };
369 3baa2617 2022-02-16 op strlcpy(path, song, sizeof(path));
370 3baa2617 2022-02-16 op if ((fd = open(path, O_RDONLY)) == -1) {
371 3baa2617 2022-02-16 op log_warn("open %s", path);
375 dfe2ad96 2022-02-24 op if (fstat(fd, &sb) == -1) {
376 dfe2ad96 2022-02-24 op log_warn("failed to stat %s", path);
381 dfe2ad96 2022-02-24 op if (S_ISDIR(sb.st_mode)) {
382 dfe2ad96 2022-02-24 op log_info("skipping a directory: %s", path);
387 aaccc123 2022-02-16 op play_state = STATE_PLAYING;
388 3baa2617 2022-02-16 op imsg_compose_event(iev_player, IMSG_PLAY, 0, 0, fd,
389 3baa2617 2022-02-16 op path, sizeof(path));
394 a913de21 2022-02-17 op main_playlist_jump(struct imsgev *iev, struct imsg *imsg)
396 a913de21 2022-02-17 op size_t datalen;
397 a913de21 2022-02-17 op char arg[PATH_MAX];
398 a913de21 2022-02-17 op const char *song;
400 a913de21 2022-02-17 op datalen = IMSG_DATA_SIZE(*imsg);
401 a913de21 2022-02-17 op if (datalen != sizeof(arg)) {
402 a913de21 2022-02-17 op main_senderr(iev, "wrong size");
406 a913de21 2022-02-17 op memcpy(arg, imsg->data, sizeof(arg));
407 a913de21 2022-02-17 op if (arg[sizeof(arg)-1] != '\0') {
408 a913de21 2022-02-17 op main_senderr(iev, "data corrupted");
412 a913de21 2022-02-17 op song = playlist_jump(arg);
413 a913de21 2022-02-17 op if (song == NULL) {
414 a913de21 2022-02-17 op main_senderr(iev, "not found");
418 a913de21 2022-02-17 op main_send_player(IMSG_STOP, -1, NULL, 0);
419 a913de21 2022-02-17 op if (!main_play_song(song)) {
420 a913de21 2022-02-17 op main_senderr(iev, "can't play");
421 a913de21 2022-02-17 op playlist_dropcurrent();
422 a913de21 2022-02-17 op main_playlist_advance();
426 a913de21 2022-02-17 op main_send_status(iev);
430 2f589330 2022-02-17 op main_playlist_resume(void)
432 2f589330 2022-02-17 op const char *song;
434 74c987d5 2022-02-19 op if ((song = current_song) == NULL)
435 2f589330 2022-02-17 op song = playlist_advance();
437 c8777fe4 2022-02-17 op for (; song != NULL; song = playlist_advance()) {
438 2f589330 2022-02-17 op if (main_play_song(song))
441 2f589330 2022-02-17 op playlist_dropcurrent();
446 aaccc123 2022-02-16 op main_playlist_advance(void)
448 aaccc123 2022-02-16 op const char *song;
451 aaccc123 2022-02-16 op song = playlist_advance();
452 aaccc123 2022-02-16 op if (song == NULL)
455 aaccc123 2022-02-16 op if (main_play_song(song))
458 13b83883 2022-02-16 op playlist_dropcurrent();
463 af27e631 2022-02-17 op main_playlist_previous(void)
465 af27e631 2022-02-17 op const char *song;
468 af27e631 2022-02-17 op song = playlist_previous();
469 af27e631 2022-02-17 op if (song == NULL)
472 af27e631 2022-02-17 op if (main_play_song(song))
475 af27e631 2022-02-17 op playlist_dropcurrent();
480 aaccc123 2022-02-16 op main_restart_track(void)
482 aaccc123 2022-02-16 op const char *song;
484 74c987d5 2022-02-19 op song = current_song;
485 aaccc123 2022-02-16 op if (song == NULL)
488 aaccc123 2022-02-16 op if (main_play_song(song))
491 13b83883 2022-02-16 op playlist_dropcurrent();
492 aaccc123 2022-02-16 op main_playlist_advance();
496 19d6b480 2022-02-17 op main_senderr(struct imsgev *iev, const char *msg)
498 19d6b480 2022-02-17 op imsg_compose_event(iev, IMSG_CTL_ERR, 0, 0, -1,
499 19d6b480 2022-02-17 op msg, strlen(msg)+1);
503 19d6b480 2022-02-17 op main_enqueue(int tx, struct playlist *px, struct imsgev *iev,
504 19d6b480 2022-02-17 op struct imsg *imsg)
506 3baa2617 2022-02-16 op size_t datalen;
507 3baa2617 2022-02-16 op char path[PATH_MAX] = { 0 };
508 3baa2617 2022-02-16 op const char *err = NULL;
510 3baa2617 2022-02-16 op datalen = IMSG_DATA_SIZE(*imsg);
511 3baa2617 2022-02-16 op if (datalen != sizeof(path)) {
512 3baa2617 2022-02-16 op err = "data size mismatch";
516 3baa2617 2022-02-16 op memcpy(path, imsg->data, sizeof(path));
517 3baa2617 2022-02-16 op if (path[datalen-1] != '\0') {
518 3baa2617 2022-02-16 op err = "malformed data";
523 19d6b480 2022-02-17 op playlist_push(px, path);
525 19d6b480 2022-02-17 op playlist_enqueue(path);
526 3baa2617 2022-02-16 op imsg_compose_event(iev, IMSG_CTL_ADD, 0, 0, -1, path, sizeof(path));
529 0f5568cb 2022-03-02 op main_senderr(iev, err);
533 d980494c 2022-02-16 op main_send_playlist(struct imsgev *iev)
535 63dd8e70 2022-02-17 op struct player_status s;
538 d980494c 2022-02-16 op for (i = 0; i < playlist.len; ++i) {
539 63dd8e70 2022-02-17 op memset(&s, 0, sizeof(s));
540 63dd8e70 2022-02-17 op strlcpy(s.path, playlist.songs[i], sizeof(s.path));
541 63dd8e70 2022-02-17 op s.status = play_off == i ? STATE_PLAYING : STATE_STOPPED;
542 63dd8e70 2022-02-17 op imsg_compose_event(iev, IMSG_CTL_SHOW, 0, 0, -1, &s,
546 d980494c 2022-02-16 op imsg_compose_event(iev, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
550 bb3f279f 2022-02-16 op main_send_status(struct imsgev *iev)
552 bb3f279f 2022-02-16 op struct player_status s;
554 bb3f279f 2022-02-16 op memset(&s, 0, sizeof(s));
556 74c987d5 2022-02-19 op if (current_song != NULL)
557 74c987d5 2022-02-19 op strlcpy(s.path, current_song, sizeof(s.path));
558 bb3f279f 2022-02-16 op s.status = play_state;
559 44398779 2022-02-19 op s.rp.repeat_all = repeat_all;
560 44398779 2022-02-19 op s.rp.repeat_one = repeat_one;
562 bb3f279f 2022-02-16 op imsg_compose_event(iev, IMSG_CTL_STATUS, 0, 0, -1, &s, sizeof(s));