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 3baa2617 2022-02-16 op #include <sys/uio.h>
21 3baa2617 2022-02-16 op #include <sys/wait.h>
23 3baa2617 2022-02-16 op #include <event.h>
24 3baa2617 2022-02-16 op #include <errno.h>
25 3baa2617 2022-02-16 op #include <fcntl.h>
26 3baa2617 2022-02-16 op #include <limits.h>
27 3baa2617 2022-02-16 op #include <sndio.h>
28 3baa2617 2022-02-16 op #include <signal.h>
29 3baa2617 2022-02-16 op #include <stdio.h>
30 3baa2617 2022-02-16 op #include <stdlib.h>
31 3baa2617 2022-02-16 op #include <stdint.h>
32 3baa2617 2022-02-16 op #include <string.h>
33 3baa2617 2022-02-16 op #include <syslog.h>
34 3baa2617 2022-02-16 op #include <unistd.h>
35 3baa2617 2022-02-16 op #include <imsg.h>
37 3baa2617 2022-02-16 op #include "amused.h"
38 3baa2617 2022-02-16 op #include "control.h"
39 3baa2617 2022-02-16 op #include "log.h"
40 3baa2617 2022-02-16 op #include "playlist.h"
41 3baa2617 2022-02-16 op #include "xmalloc.h"
43 3baa2617 2022-02-16 op struct sio_hdl *hdl;
44 3baa2617 2022-02-16 op char *csock = NULL;
47 3baa2617 2022-02-16 op struct imsgev *iev_player;
49 3baa2617 2022-02-16 op const char *argv0;
50 3baa2617 2022-02-16 op pid_t player_pid;
51 3baa2617 2022-02-16 op struct event ev_sigint;
52 3baa2617 2022-02-16 op struct event ev_sigterm;
53 946944db 2022-02-16 op struct event ev_siginfo;
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 946944db 2022-02-16 op main_status(void)
89 946944db 2022-02-16 op const char *cur;
91 946944db 2022-02-16 op switch (play_state) {
92 946944db 2022-02-16 op case STATE_STOPPED:
93 946944db 2022-02-16 op log_info("status: stopped");
95 946944db 2022-02-16 op case STATE_PLAYING:
96 946944db 2022-02-16 op log_info("status: playing");
98 946944db 2022-02-16 op case STATE_PAUSED:
99 946944db 2022-02-16 op log_info("status: paused");
102 946944db 2022-02-16 op log_info("status: unknown");
106 946944db 2022-02-16 op if ((cur = playlist_current()) != NULL)
107 946944db 2022-02-16 op log_info("playing %s", cur);
109 946944db 2022-02-16 op log_info("not playing anything");
113 3baa2617 2022-02-16 op main_sig_handler(int sig, short event, void *arg)
116 3baa2617 2022-02-16 op * Normal signal handler rules don't apply because libevent
117 3baa2617 2022-02-16 op * decouples for us.
120 3baa2617 2022-02-16 op switch (sig) {
121 3baa2617 2022-02-16 op case SIGTERM:
123 3baa2617 2022-02-16 op main_shutdown();
125 946944db 2022-02-16 op case SIGINFO:
126 946944db 2022-02-16 op main_status();
129 3baa2617 2022-02-16 op fatalx("unexpected signal %d", sig);
134 3baa2617 2022-02-16 op main_dispatch_player(int sig, short event, void *d)
136 3baa2617 2022-02-16 op struct imsgev *iev = d;
137 3baa2617 2022-02-16 op struct imsgbuf *ibuf = &iev->ibuf;
138 3baa2617 2022-02-16 op struct imsg imsg;
140 3baa2617 2022-02-16 op int shut = 0;
142 3baa2617 2022-02-16 op if (event & EV_READ) {
143 3baa2617 2022-02-16 op if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
144 3baa2617 2022-02-16 op fatal("imsg_read error");
145 3baa2617 2022-02-16 op if (n == 0) /* Connection closed */
148 3baa2617 2022-02-16 op if (event & EV_WRITE) {
149 3baa2617 2022-02-16 op if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
150 3baa2617 2022-02-16 op fatal("msgbuf_write");
151 3baa2617 2022-02-16 op if (n == 0) /* Connection closed */
156 3baa2617 2022-02-16 op if ((n = imsg_get(ibuf, &imsg)) == -1)
157 3baa2617 2022-02-16 op fatal("imsg_get");
158 3baa2617 2022-02-16 op if (n == 0) /* No more messages. */
161 3baa2617 2022-02-16 op switch (imsg.hdr.type) {
162 3baa2617 2022-02-16 op case IMSG_ERR:
163 13b83883 2022-02-16 op playlist_dropcurrent();
164 13b83883 2022-02-16 op /* fallthrough */
165 aaccc123 2022-02-16 op case IMSG_EOF:
166 aaccc123 2022-02-16 op main_playlist_advance();
170 3baa2617 2022-02-16 op log_debug("%s: error handling imsg %d", __func__,
171 3baa2617 2022-02-16 op imsg.hdr.type);
174 3baa2617 2022-02-16 op imsg_free(&imsg);
178 3baa2617 2022-02-16 op imsg_event_add(iev);
180 3baa2617 2022-02-16 op /* This pipe is dead. Remove its event handler. */
181 3baa2617 2022-02-16 op event_del(&iev->ev);
182 3baa2617 2022-02-16 op event_loopexit(NULL);
187 3baa2617 2022-02-16 op start_child(enum amused_process proc, int fd)
189 4c86871a 2022-02-17 op const char *argv[6];
190 3baa2617 2022-02-16 op int argc = 0;
193 3baa2617 2022-02-16 op switch (pid = fork()) {
195 3baa2617 2022-02-16 op fatal("cannot fork");
203 3baa2617 2022-02-16 op if (fd != 3) {
204 fea541a8 2022-02-16 op if (fd != -1 && dup2(fd, 3) == -1)
205 3baa2617 2022-02-16 op fatal("cannot setup imsg fd");
206 3baa2617 2022-02-16 op } else if (fcntl(F_SETFD, 0) == -1)
207 3baa2617 2022-02-16 op fatal("cannot setup imsg fd");
209 3baa2617 2022-02-16 op argv[argc++] = argv0;
211 3baa2617 2022-02-16 op switch (proc) {
212 3baa2617 2022-02-16 op case PROC_MAIN:
213 4c86871a 2022-02-17 op argv[argc++] = "-s";
214 4c86871a 2022-02-17 op argv[argc++] = csock;
216 3baa2617 2022-02-16 op case PROC_PLAYER:
217 3baa2617 2022-02-16 op argv[argc++] = "-Tp";
222 3baa2617 2022-02-16 op argv[argc++] = "-d";
224 3baa2617 2022-02-16 op argv[argc++] = "-v";
225 3baa2617 2022-02-16 op argv[argc++] = NULL;
227 3baa2617 2022-02-16 op /* obnoxious casts */
228 3baa2617 2022-02-16 op execvp(argv0, (char *const *)argv);
229 3baa2617 2022-02-16 op fatal("execvp %s", argv0);
232 3baa2617 2022-02-16 op /* daemon main routine */
233 3baa2617 2022-02-16 op static __dead int
234 3baa2617 2022-02-16 op amused_main(void)
236 3baa2617 2022-02-16 op int pipe_main2player[2];
237 3baa2617 2022-02-16 op int control_fd;
239 3baa2617 2022-02-16 op log_init(debug, LOG_DAEMON);
240 3baa2617 2022-02-16 op log_setverbose(verbose);
241 3baa2617 2022-02-16 op log_procinit("main");
244 3baa2617 2022-02-16 op daemon(1, 0);
246 3baa2617 2022-02-16 op if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
247 3baa2617 2022-02-16 op PF_UNSPEC, pipe_main2player) == -1)
248 3baa2617 2022-02-16 op fatal("socketpair");
250 3baa2617 2022-02-16 op player_pid = start_child(PROC_PLAYER, pipe_main2player[1]);
252 3baa2617 2022-02-16 op event_init();
254 3baa2617 2022-02-16 op signal_set(&ev_sigint, SIGINT, main_sig_handler, NULL);
255 3baa2617 2022-02-16 op signal_set(&ev_sigterm, SIGTERM, main_sig_handler, NULL);
256 946944db 2022-02-16 op signal_set(&ev_siginfo, SIGINFO, main_sig_handler, NULL);
258 3baa2617 2022-02-16 op signal_add(&ev_sigint, NULL);
259 3baa2617 2022-02-16 op signal_add(&ev_sigterm, NULL);
260 946944db 2022-02-16 op signal_add(&ev_siginfo, NULL);
262 3baa2617 2022-02-16 op signal(SIGHUP, SIG_IGN);
263 3baa2617 2022-02-16 op signal(SIGCHLD, SIG_IGN);
264 3baa2617 2022-02-16 op signal(SIGPIPE, SIG_IGN);
266 3baa2617 2022-02-16 op iev_player = xmalloc(sizeof(*iev_player));
267 3baa2617 2022-02-16 op imsg_init(&iev_player->ibuf, pipe_main2player[0]);
268 3baa2617 2022-02-16 op iev_player->handler = main_dispatch_player;
269 3baa2617 2022-02-16 op iev_player->events = EV_READ;
270 3baa2617 2022-02-16 op event_set(&iev_player->ev, iev_player->ibuf.fd, iev_player->events,
271 3baa2617 2022-02-16 op iev_player->handler, iev_player);
272 3baa2617 2022-02-16 op event_add(&iev_player->ev, NULL);
274 3baa2617 2022-02-16 op if ((control_fd = control_init(csock)) == -1)
275 3baa2617 2022-02-16 op fatal("control socket setup failed %s", csock);
276 3baa2617 2022-02-16 op control_listen(control_fd);
278 3baa2617 2022-02-16 op if (pledge("stdio rpath unix sendfd", NULL) == -1)
279 3baa2617 2022-02-16 op fatal("pledge");
281 3baa2617 2022-02-16 op log_info("startup");
282 3baa2617 2022-02-16 op event_dispatch();
283 3baa2617 2022-02-16 op main_shutdown();
287 3baa2617 2022-02-16 op main(int argc, char **argv)
289 3baa2617 2022-02-16 op int ch, proc = PROC_MAIN;
291 3baa2617 2022-02-16 op log_init(1, LOG_DAEMON); /* Log to stderr until daemonized */
292 3baa2617 2022-02-16 op log_setverbose(1);
294 3baa2617 2022-02-16 op argv0 = argv[0];
295 3baa2617 2022-02-16 op if (argv0 == NULL)
296 3baa2617 2022-02-16 op argv0 = "amused";
298 3baa2617 2022-02-16 op while ((ch = getopt(argc, argv, "ds:T:vV")) != -1) {
299 3baa2617 2022-02-16 op switch (ch) {
305 3baa2617 2022-02-16 op csock = xstrdup(optarg);
308 3baa2617 2022-02-16 op switch (*optarg) {
310 3baa2617 2022-02-16 op proc = PROC_PLAYER;
320 3baa2617 2022-02-16 op printf("%s version %s\n", getprogname(),
321 3baa2617 2022-02-16 op AMUSED_VERSION);
327 3baa2617 2022-02-16 op argv += optind;
328 3baa2617 2022-02-16 op argc -= optind;
330 3baa2617 2022-02-16 op if (proc == PROC_PLAYER)
331 3baa2617 2022-02-16 op exit(player(debug, verbose));
333 3baa2617 2022-02-16 op if (csock == NULL)
334 3baa2617 2022-02-16 op xasprintf(&csock, "/tmp/amused-%d", getuid());
336 3baa2617 2022-02-16 op if (argc == 0)
337 3baa2617 2022-02-16 op amused_main();
339 3baa2617 2022-02-16 op ctl(argc, argv);
344 fea541a8 2022-02-16 op spawn_daemon(void)
347 fea541a8 2022-02-16 op start_child(PROC_MAIN, -1);
351 3baa2617 2022-02-16 op imsg_event_add(struct imsgev *iev)
353 3baa2617 2022-02-16 op iev->events = EV_READ;
354 3baa2617 2022-02-16 op if (iev->ibuf.w.queued)
355 3baa2617 2022-02-16 op iev->events |= EV_WRITE;
357 3baa2617 2022-02-16 op event_del(&iev->ev);
358 3baa2617 2022-02-16 op event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
359 3baa2617 2022-02-16 op event_add(&iev->ev, NULL);
363 3baa2617 2022-02-16 op imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
364 3baa2617 2022-02-16 op pid_t pid, int fd, const void *data, uint16_t datalen)
368 3baa2617 2022-02-16 op if ((ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data,
369 3baa2617 2022-02-16 op datalen)) != -1)
370 3baa2617 2022-02-16 op imsg_event_add(iev);
376 06961b72 2022-02-16 op main_send_player(uint16_t type, int fd, const void *data, uint16_t datalen)
378 06961b72 2022-02-16 op return imsg_compose_event(iev_player, type, 0, 0, fd, data, datalen);
382 3baa2617 2022-02-16 op main_play_song(const char *song)
384 3baa2617 2022-02-16 op char path[PATH_MAX] = { 0 };
387 3baa2617 2022-02-16 op strlcpy(path, song, sizeof(path));
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 aaccc123 2022-02-16 op play_state = STATE_PLAYING;
394 3baa2617 2022-02-16 op imsg_compose_event(iev_player, IMSG_PLAY, 0, 0, fd,
395 3baa2617 2022-02-16 op path, sizeof(path));
400 2f589330 2022-02-17 op main_playlist_resume(void)
402 2f589330 2022-02-17 op const char *song;
404 2f589330 2022-02-17 op if ((song = playlist_current()) == NULL)
405 2f589330 2022-02-17 op song = playlist_advance();
407 c8777fe4 2022-02-17 op for (; song != NULL; song = playlist_advance()) {
408 2f589330 2022-02-17 op if (main_play_song(song))
411 2f589330 2022-02-17 op playlist_dropcurrent();
416 aaccc123 2022-02-16 op main_playlist_advance(void)
418 aaccc123 2022-02-16 op const char *song;
421 aaccc123 2022-02-16 op song = playlist_advance();
422 aaccc123 2022-02-16 op if (song == NULL)
425 aaccc123 2022-02-16 op if (main_play_song(song))
428 13b83883 2022-02-16 op playlist_dropcurrent();
433 af27e631 2022-02-17 op main_playlist_previous(void)
435 af27e631 2022-02-17 op const char *song;
438 af27e631 2022-02-17 op song = playlist_previous();
439 af27e631 2022-02-17 op if (song == NULL)
442 af27e631 2022-02-17 op if (main_play_song(song))
445 af27e631 2022-02-17 op playlist_dropcurrent();
450 aaccc123 2022-02-16 op main_restart_track(void)
452 aaccc123 2022-02-16 op const char *song;
454 aaccc123 2022-02-16 op song = playlist_current();
455 aaccc123 2022-02-16 op if (song == NULL)
458 aaccc123 2022-02-16 op if (main_play_song(song))
461 13b83883 2022-02-16 op playlist_dropcurrent();
462 aaccc123 2022-02-16 op main_playlist_advance();
466 3baa2617 2022-02-16 op main_enqueue(struct imsgev *iev, struct imsg *imsg)
468 3baa2617 2022-02-16 op size_t datalen;
469 3baa2617 2022-02-16 op char path[PATH_MAX] = { 0 };
470 3baa2617 2022-02-16 op const char *err = NULL;
472 3baa2617 2022-02-16 op datalen = IMSG_DATA_SIZE(*imsg);
473 3baa2617 2022-02-16 op if (datalen != sizeof(path)) {
474 3baa2617 2022-02-16 op err = "data size mismatch";
478 3baa2617 2022-02-16 op memcpy(path, imsg->data, sizeof(path));
479 3baa2617 2022-02-16 op if (path[datalen-1] != '\0') {
480 3baa2617 2022-02-16 op err = "malformed data";
484 3baa2617 2022-02-16 op playlist_enqueue(path);
485 3baa2617 2022-02-16 op imsg_compose_event(iev, IMSG_CTL_ADD, 0, 0, -1, path, sizeof(path));
488 3baa2617 2022-02-16 op imsg_compose_event(iev, IMSG_CTL_ERR, 0, 0, -1, err, strlen(err)+1);
492 d980494c 2022-02-16 op main_send_playlist(struct imsgev *iev)
494 63dd8e70 2022-02-17 op struct player_status s;
497 d980494c 2022-02-16 op for (i = 0; i < playlist.len; ++i) {
498 63dd8e70 2022-02-17 op memset(&s, 0, sizeof(s));
499 63dd8e70 2022-02-17 op strlcpy(s.path, playlist.songs[i], sizeof(s.path));
500 63dd8e70 2022-02-17 op s.status = play_off == i ? STATE_PLAYING : STATE_STOPPED;
501 63dd8e70 2022-02-17 op imsg_compose_event(iev, IMSG_CTL_SHOW, 0, 0, -1, &s,
505 d980494c 2022-02-16 op imsg_compose_event(iev, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
509 bb3f279f 2022-02-16 op main_send_status(struct imsgev *iev)
511 bb3f279f 2022-02-16 op struct player_status s;
512 bb3f279f 2022-02-16 op const char *song;
514 bb3f279f 2022-02-16 op memset(&s, 0, sizeof(s));
516 bb3f279f 2022-02-16 op song = playlist_current();
517 bb3f279f 2022-02-16 op if (song != NULL)
518 bb3f279f 2022-02-16 op strlcpy(s.path, song, sizeof(s.path));
519 bb3f279f 2022-02-16 op s.status = play_state;
521 bb3f279f 2022-02-16 op imsg_compose_event(iev, IMSG_CTL_STATUS, 0, 0, -1, &s, sizeof(s));