2 e3317c86 2023-05-02 op * Copyright (c) 2022 Omar Polo <op@omarpolo.com>
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 f36fd90a 2022-07-09 op #include "config.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/wait.h>
23 3baa2617 2022-02-16 op #include <errno.h>
24 3baa2617 2022-02-16 op #include <fcntl.h>
25 3baa2617 2022-02-16 op #include <limits.h>
26 0a53bd62 2023-08-16 op #include <poll.h>
27 3baa2617 2022-02-16 op #include <signal.h>
28 3baa2617 2022-02-16 op #include <stdio.h>
29 3baa2617 2022-02-16 op #include <stdlib.h>
30 3baa2617 2022-02-16 op #include <stdint.h>
31 3baa2617 2022-02-16 op #include <string.h>
32 3baa2617 2022-02-16 op #include <syslog.h>
33 3baa2617 2022-02-16 op #include <unistd.h>
35 3baa2617 2022-02-16 op #include "amused.h"
36 3baa2617 2022-02-16 op #include "control.h"
37 0a53bd62 2023-08-16 op #include "ev.h"
38 3baa2617 2022-02-16 op #include "log.h"
39 3baa2617 2022-02-16 op #include "playlist.h"
40 3baa2617 2022-02-16 op #include "xmalloc.h"
42 3baa2617 2022-02-16 op char *csock = NULL;
45 3baa2617 2022-02-16 op struct imsgev *iev_player;
47 3baa2617 2022-02-16 op const char *argv0;
48 3baa2617 2022-02-16 op pid_t player_pid;
50 3baa2617 2022-02-16 op enum amused_process {
55 ae335651 2022-07-08 op static __dead void
56 3baa2617 2022-02-16 op main_shutdown(void)
61 3baa2617 2022-02-16 op /* close pipes. */
62 3baa2617 2022-02-16 op msgbuf_clear(&iev_player->ibuf.w);
63 3baa2617 2022-02-16 op close(iev_player->ibuf.fd);
64 3baa2617 2022-02-16 op free(iev_player);
66 3baa2617 2022-02-16 op log_debug("waiting for children to terminate");
68 3baa2617 2022-02-16 op pid = wait(&status);
69 3baa2617 2022-02-16 op if (pid == -1) {
70 3baa2617 2022-02-16 op if (errno != EINTR && errno != ECHILD)
71 3baa2617 2022-02-16 op fatal("wait");
72 3baa2617 2022-02-16 op } else if (WIFSIGNALED(status))
73 3baa2617 2022-02-16 op log_warnx("player terminated; signal %d",
74 3baa2617 2022-02-16 op WTERMSIG(status));
75 3baa2617 2022-02-16 op } while (pid != -1 || (pid == -1 && errno == EINTR));
77 3baa2617 2022-02-16 op log_info("terminating");
82 0a53bd62 2023-08-16 op main_sig_handler(int sig, int event, void *arg)
85 0a53bd62 2023-08-16 op * Normal signal handler rules don't apply because ev.c
86 3baa2617 2022-02-16 op * decouples for us.
89 3baa2617 2022-02-16 op switch (sig) {
92 3baa2617 2022-02-16 op main_shutdown();
95 3baa2617 2022-02-16 op fatalx("unexpected signal %d", sig);
100 0a53bd62 2023-08-16 op main_dispatch_player(int sig, int event, void *d)
102 17ef54d6 2022-06-22 op char *errstr;
103 3baa2617 2022-02-16 op struct imsgev *iev = d;
104 3baa2617 2022-02-16 op struct imsgbuf *ibuf = &iev->ibuf;
105 3baa2617 2022-02-16 op struct imsg imsg;
106 17ef54d6 2022-06-22 op size_t datalen;
108 3baa2617 2022-02-16 op int shut = 0;
110 0a53bd62 2023-08-16 op if (event & POLLIN) {
111 3baa2617 2022-02-16 op if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
112 3baa2617 2022-02-16 op fatal("imsg_read error");
113 3baa2617 2022-02-16 op if (n == 0) /* Connection closed */
116 0a53bd62 2023-08-16 op if (event & POLLOUT) {
117 3baa2617 2022-02-16 op if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
118 3baa2617 2022-02-16 op fatal("msgbuf_write");
119 3baa2617 2022-02-16 op if (n == 0) /* Connection closed */
124 3baa2617 2022-02-16 op if ((n = imsg_get(ibuf, &imsg)) == -1)
125 3baa2617 2022-02-16 op fatal("imsg_get");
126 3baa2617 2022-02-16 op if (n == 0) /* No more messages. */
129 17ef54d6 2022-06-22 op datalen = IMSG_DATA_SIZE(imsg);
130 3baa2617 2022-02-16 op switch (imsg.hdr.type) {
131 ff06024f 2022-07-08 op case IMSG_POS:
132 ff06024f 2022-07-08 op if (datalen != sizeof(current_position))
133 ff06024f 2022-07-08 op fatalx("IMSG_POS: got wrong size (%zu vs %zu)",
134 ff06024f 2022-07-08 op datalen, sizeof(current_position));
135 ff06024f 2022-07-08 op memcpy(¤t_position, imsg.data,
136 ff06024f 2022-07-08 op sizeof(current_position));
137 ff06024f 2022-07-08 op if (current_position < 0)
138 ff06024f 2022-07-08 op current_position = -1;
139 93cde5d1 2023-01-09 op control_notify(IMSG_CTL_SEEK);
141 ff06024f 2022-07-08 op case IMSG_LEN:
142 ff06024f 2022-07-08 op if (datalen != sizeof(current_duration))
143 ff06024f 2022-07-08 op fatalx("IMSG_LEN: got wrong size (%zu vs %zu)",
144 ff06024f 2022-07-08 op datalen, sizeof(current_duration));
145 ff06024f 2022-07-08 op memcpy(¤t_duration, imsg.data,
146 ff06024f 2022-07-08 op sizeof(current_duration));
147 ff06024f 2022-07-08 op if (current_duration < 0)
148 ff06024f 2022-07-08 op current_duration = -1;
150 3baa2617 2022-02-16 op case IMSG_ERR:
151 17ef54d6 2022-06-22 op if (datalen == 0)
152 17ef54d6 2022-06-22 op errstr = "unknown error";
154 17ef54d6 2022-06-22 op errstr = imsg.data;
155 17ef54d6 2022-06-22 op errstr[datalen-1] = '\0';
157 17ef54d6 2022-06-22 op log_warnx("%s; skipping %s", errstr, current_song);
158 13b83883 2022-02-16 op playlist_dropcurrent();
159 9fb94242 2022-07-13 op main_playlist_advance();
160 9fb94242 2022-07-13 op if (play_state == STATE_PLAYING)
161 9fb94242 2022-07-13 op control_notify(IMSG_CTL_NEXT);
163 9fb94242 2022-07-13 op control_notify(IMSG_CTL_STOP);
165 aaccc123 2022-02-16 op case IMSG_EOF:
166 9fb94242 2022-07-13 op if (repeat_one && main_play_song(current_song))
168 9fb94242 2022-07-13 op else if (repeat_one || consume)
169 0be6c0b1 2022-02-22 op playlist_dropcurrent();
170 aaccc123 2022-02-16 op main_playlist_advance();
171 87f575c3 2022-02-21 op if (play_state == STATE_PLAYING)
172 949b5c2e 2022-07-12 op control_notify(IMSG_CTL_NEXT);
174 949b5c2e 2022-07-12 op control_notify(IMSG_CTL_STOP);
177 3baa2617 2022-02-16 op log_debug("%s: error handling imsg %d", __func__,
178 3baa2617 2022-02-16 op imsg.hdr.type);
181 3baa2617 2022-02-16 op imsg_free(&imsg);
187 3baa2617 2022-02-16 op imsg_event_add(iev);
191 3baa2617 2022-02-16 op start_child(enum amused_process proc, int fd)
193 1d673950 2022-03-03 op const char *argv[7];
194 3baa2617 2022-02-16 op int argc = 0;
197 1d673950 2022-03-03 op if (fd == -1 && debug)
200 3baa2617 2022-02-16 op switch (pid = fork()) {
202 3baa2617 2022-02-16 op fatal("cannot fork");
210 3baa2617 2022-02-16 op if (fd != 3) {
211 fea541a8 2022-02-16 op if (fd != -1 && dup2(fd, 3) == -1)
212 3baa2617 2022-02-16 op fatal("cannot setup imsg fd");
213 3baa2617 2022-02-16 op } else if (fcntl(F_SETFD, 0) == -1)
214 3baa2617 2022-02-16 op fatal("cannot setup imsg fd");
217 3baa2617 2022-02-16 op argv[argc++] = argv0;
219 3baa2617 2022-02-16 op switch (proc) {
220 3baa2617 2022-02-16 op case PROC_MAIN:
221 4c86871a 2022-02-17 op argv[argc++] = "-s";
222 4c86871a 2022-02-17 op argv[argc++] = csock;
223 1d673950 2022-03-03 op argv[argc++] = "-Tm";
225 3baa2617 2022-02-16 op case PROC_PLAYER:
226 3baa2617 2022-02-16 op argv[argc++] = "-Tp";
231 3baa2617 2022-02-16 op argv[argc++] = "-d";
233 3baa2617 2022-02-16 op argv[argc++] = "-v";
234 3baa2617 2022-02-16 op argv[argc++] = NULL;
236 3baa2617 2022-02-16 op /* obnoxious casts */
237 3baa2617 2022-02-16 op execvp(argv0, (char *const *)argv);
238 3baa2617 2022-02-16 op fatal("execvp %s", argv0);
241 3baa2617 2022-02-16 op /* daemon main routine */
242 6a1b899f 2022-03-10 op static __dead void
243 3baa2617 2022-02-16 op amused_main(void)
245 3baa2617 2022-02-16 op int pipe_main2player[2];
246 3baa2617 2022-02-16 op int control_fd;
248 3baa2617 2022-02-16 op log_init(debug, LOG_DAEMON);
249 3baa2617 2022-02-16 op log_setverbose(verbose);
250 3baa2617 2022-02-16 op log_procinit("main");
253 3baa2617 2022-02-16 op daemon(1, 0);
255 3baa2617 2022-02-16 op if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
256 3baa2617 2022-02-16 op PF_UNSPEC, pipe_main2player) == -1)
257 3baa2617 2022-02-16 op fatal("socketpair");
259 3baa2617 2022-02-16 op player_pid = start_child(PROC_PLAYER, pipe_main2player[1]);
261 0a53bd62 2023-08-16 op if (ev_init() == -1)
262 0a53bd62 2023-08-16 op fatal("ev_init");
264 3baa2617 2022-02-16 op signal(SIGHUP, SIG_IGN);
265 3baa2617 2022-02-16 op signal(SIGCHLD, SIG_IGN);
266 3baa2617 2022-02-16 op signal(SIGPIPE, SIG_IGN);
268 0a53bd62 2023-08-16 op ev_signal(SIGINT, main_sig_handler, NULL);
269 0a53bd62 2023-08-16 op ev_signal(SIGTERM, main_sig_handler, NULL);
271 3baa2617 2022-02-16 op iev_player = xmalloc(sizeof(*iev_player));
272 3baa2617 2022-02-16 op imsg_init(&iev_player->ibuf, pipe_main2player[0]);
273 3baa2617 2022-02-16 op iev_player->handler = main_dispatch_player;
274 0a53bd62 2023-08-16 op iev_player->events = POLLIN;
275 0a53bd62 2023-08-16 op ev_add(iev_player->ibuf.fd, iev_player->events,
276 3baa2617 2022-02-16 op iev_player->handler, iev_player);
278 3baa2617 2022-02-16 op if ((control_fd = control_init(csock)) == -1)
279 3baa2617 2022-02-16 op fatal("control socket setup failed %s", csock);
280 3baa2617 2022-02-16 op control_listen(control_fd);
282 3baa2617 2022-02-16 op if (pledge("stdio rpath unix sendfd", NULL) == -1)
283 3baa2617 2022-02-16 op fatal("pledge");
285 3baa2617 2022-02-16 op log_info("startup");
287 3baa2617 2022-02-16 op main_shutdown();
291 3baa2617 2022-02-16 op main(int argc, char **argv)
293 1d673950 2022-03-03 op int ch, proc = -1;
295 3baa2617 2022-02-16 op log_init(1, LOG_DAEMON); /* Log to stderr until daemonized */
296 3baa2617 2022-02-16 op log_setverbose(1);
298 3baa2617 2022-02-16 op argv0 = argv[0];
299 3baa2617 2022-02-16 op if (argv0 == NULL)
300 3baa2617 2022-02-16 op argv0 = "amused";
302 d307e5a2 2022-02-19 op while ((ch = getopt(argc, argv, "ds:T:v")) != -1) {
303 3baa2617 2022-02-16 op switch (ch) {
309 3baa2617 2022-02-16 op csock = xstrdup(optarg);
312 3baa2617 2022-02-16 op switch (*optarg) {
314 1d673950 2022-03-03 op proc = PROC_MAIN;
317 3baa2617 2022-02-16 op proc = PROC_PLAYER;
330 3baa2617 2022-02-16 op argv += optind;
331 3baa2617 2022-02-16 op argc -= optind;
333 1d673950 2022-03-03 op if (proc == PROC_MAIN)
334 1d673950 2022-03-03 op amused_main();
335 3baa2617 2022-02-16 op if (proc == PROC_PLAYER)
336 3baa2617 2022-02-16 op exit(player(debug, verbose));
338 3baa2617 2022-02-16 op if (csock == NULL)
339 3baa2617 2022-02-16 op xasprintf(&csock, "/tmp/amused-%d", getuid());
341 1d673950 2022-03-03 op if (argc > 0)
344 1d673950 2022-03-03 op ctl(argc, argv);
348 fea541a8 2022-02-16 op spawn_daemon(void)
350 fea541a8 2022-02-16 op start_child(PROC_MAIN, -1);
354 3baa2617 2022-02-16 op imsg_event_add(struct imsgev *iev)
356 0a53bd62 2023-08-16 op iev->events = POLLIN;
357 3baa2617 2022-02-16 op if (iev->ibuf.w.queued)
358 0a53bd62 2023-08-16 op iev->events |= POLLOUT;
360 0a53bd62 2023-08-16 op ev_add(iev->ibuf.fd, iev->events, iev->handler, iev);
364 3baa2617 2022-02-16 op imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
365 3baa2617 2022-02-16 op pid_t pid, int fd, const void *data, uint16_t datalen)
369 3baa2617 2022-02-16 op if ((ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data,
370 3baa2617 2022-02-16 op datalen)) != -1)
371 3baa2617 2022-02-16 op imsg_event_add(iev);
377 791d3db3 2022-07-09 op main_send_player(uint16_t type, int fd, const void *data, size_t len)
379 791d3db3 2022-07-09 op return imsg_compose_event(iev_player, type, 0, 0, fd, data, len);
383 a975dca9 2022-05-10 op main_play_song(const char *path)
385 dfe2ad96 2022-02-24 op struct stat sb;
388 3baa2617 2022-02-16 op if ((fd = open(path, O_RDONLY)) == -1) {
389 3baa2617 2022-02-16 op log_warn("open %s", path);
393 dfe2ad96 2022-02-24 op if (fstat(fd, &sb) == -1) {
394 dfe2ad96 2022-02-24 op log_warn("failed to stat %s", path);
399 58b2f322 2022-06-11 op if (!S_ISREG(sb.st_mode)) {
400 58b2f322 2022-06-11 op log_info("skipping non-regular file: %s", path);
405 aaccc123 2022-02-16 op play_state = STATE_PLAYING;
406 791d3db3 2022-07-09 op main_send_player(IMSG_PLAY, fd, NULL, 0);
411 a913de21 2022-02-17 op main_playlist_jump(struct imsgev *iev, struct imsg *imsg)
413 a913de21 2022-02-17 op size_t datalen;
414 a913de21 2022-02-17 op char arg[PATH_MAX];
415 a913de21 2022-02-17 op const char *song;
417 a913de21 2022-02-17 op datalen = IMSG_DATA_SIZE(*imsg);
418 a913de21 2022-02-17 op if (datalen != sizeof(arg)) {
419 a913de21 2022-02-17 op main_senderr(iev, "wrong size");
423 a913de21 2022-02-17 op memcpy(arg, imsg->data, sizeof(arg));
424 a913de21 2022-02-17 op if (arg[sizeof(arg)-1] != '\0') {
425 a913de21 2022-02-17 op main_senderr(iev, "data corrupted");
429 a913de21 2022-02-17 op song = playlist_jump(arg);
430 a913de21 2022-02-17 op if (song == NULL) {
431 a913de21 2022-02-17 op main_senderr(iev, "not found");
435 15aecb89 2022-07-12 op control_notify(IMSG_CTL_JUMP);
437 791d3db3 2022-07-09 op main_send_player(IMSG_STOP, -1, NULL, 0);
438 a913de21 2022-02-17 op if (!main_play_song(song)) {
439 a913de21 2022-02-17 op main_senderr(iev, "can't play");
440 a913de21 2022-02-17 op playlist_dropcurrent();
441 a913de21 2022-02-17 op main_playlist_advance();
445 a913de21 2022-02-17 op main_send_status(iev);
449 2f589330 2022-02-17 op main_playlist_resume(void)
451 2f589330 2022-02-17 op const char *song;
453 74c987d5 2022-02-19 op if ((song = current_song) == NULL)
454 2f589330 2022-02-17 op song = playlist_advance();
456 c8777fe4 2022-02-17 op for (; song != NULL; song = playlist_advance()) {
457 2f589330 2022-02-17 op if (main_play_song(song))
460 2f589330 2022-02-17 op playlist_dropcurrent();
465 aaccc123 2022-02-16 op main_playlist_advance(void)
467 aaccc123 2022-02-16 op const char *song;
470 aaccc123 2022-02-16 op song = playlist_advance();
471 aaccc123 2022-02-16 op if (song == NULL)
474 aaccc123 2022-02-16 op if (main_play_song(song))
477 13b83883 2022-02-16 op playlist_dropcurrent();
482 af27e631 2022-02-17 op main_playlist_previous(void)
484 af27e631 2022-02-17 op const char *song;
487 af27e631 2022-02-17 op song = playlist_previous();
488 af27e631 2022-02-17 op if (song == NULL)
491 af27e631 2022-02-17 op if (main_play_song(song))
494 af27e631 2022-02-17 op playlist_dropcurrent();
499 19d6b480 2022-02-17 op main_senderr(struct imsgev *iev, const char *msg)
501 19d6b480 2022-02-17 op imsg_compose_event(iev, IMSG_CTL_ERR, 0, 0, -1,
502 19d6b480 2022-02-17 op msg, strlen(msg)+1);
506 19d6b480 2022-02-17 op main_enqueue(int tx, struct playlist *px, struct imsgev *iev,
507 19d6b480 2022-02-17 op struct imsg *imsg)
509 3baa2617 2022-02-16 op size_t datalen;
510 e06ad444 2022-06-09 op char path[PATH_MAX];
511 3baa2617 2022-02-16 op const char *err = NULL;
513 3baa2617 2022-02-16 op datalen = IMSG_DATA_SIZE(*imsg);
514 3baa2617 2022-02-16 op if (datalen != sizeof(path)) {
515 3baa2617 2022-02-16 op err = "data size mismatch";
519 3baa2617 2022-02-16 op memcpy(path, imsg->data, sizeof(path));
520 3baa2617 2022-02-16 op if (path[datalen-1] != '\0') {
521 3baa2617 2022-02-16 op err = "malformed data";
526 19d6b480 2022-02-17 op playlist_push(px, path);
528 19d6b480 2022-02-17 op playlist_enqueue(path);
529 3baa2617 2022-02-16 op imsg_compose_event(iev, IMSG_CTL_ADD, 0, 0, -1, path, sizeof(path));
532 0f5568cb 2022-03-02 op main_senderr(iev, err);
536 d980494c 2022-02-16 op main_send_playlist(struct imsgev *iev)
538 63dd8e70 2022-02-17 op struct player_status s;
541 d980494c 2022-02-16 op for (i = 0; i < playlist.len; ++i) {
542 63dd8e70 2022-02-17 op memset(&s, 0, sizeof(s));
543 63dd8e70 2022-02-17 op strlcpy(s.path, playlist.songs[i], sizeof(s.path));
544 63dd8e70 2022-02-17 op s.status = play_off == i ? STATE_PLAYING : STATE_STOPPED;
545 63dd8e70 2022-02-17 op imsg_compose_event(iev, IMSG_CTL_SHOW, 0, 0, -1, &s,
549 d980494c 2022-02-16 op imsg_compose_event(iev, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
553 bb3f279f 2022-02-16 op main_send_status(struct imsgev *iev)
555 bb3f279f 2022-02-16 op struct player_status s;
557 bb3f279f 2022-02-16 op memset(&s, 0, sizeof(s));
559 74c987d5 2022-02-19 op if (current_song != NULL)
560 74c987d5 2022-02-19 op strlcpy(s.path, current_song, sizeof(s.path));
561 bb3f279f 2022-02-16 op s.status = play_state;
562 ff06024f 2022-07-08 op s.position = current_position;
563 ff06024f 2022-07-08 op s.duration = current_duration;
564 9fb94242 2022-07-13 op s.mode.repeat_all = repeat_all;
565 9fb94242 2022-07-13 op s.mode.repeat_one = repeat_one;
566 9fb94242 2022-07-13 op s.mode.consume = consume;
568 bb3f279f 2022-02-16 op imsg_compose_event(iev, IMSG_CTL_STATUS, 0, 0, -1, &s, sizeof(s));
572 15aecb89 2022-07-12 op main_seek(struct player_seek *s)
574 15aecb89 2022-07-12 op switch (play_state) {
575 15aecb89 2022-07-12 op case STATE_STOPPED:
576 15aecb89 2022-07-12 op main_playlist_resume();
578 15aecb89 2022-07-12 op case STATE_PLAYING:
580 15aecb89 2022-07-12 op case STATE_PAUSED:
581 15aecb89 2022-07-12 op play_state = STATE_PLAYING;
585 15aecb89 2022-07-12 op main_send_player(IMSG_CTL_SEEK, -1, s, sizeof(*s));