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;
53 struct event ev_siginfo;
55 enum amused_process {
56 PROC_MAIN,
57 PROC_PLAYER,
58 };
60 __dead void
61 main_shutdown(void)
62 {
63 pid_t pid;
64 int status;
66 /* close pipes. */
67 msgbuf_clear(&iev_player->ibuf.w);
68 close(iev_player->ibuf.fd);
69 free(iev_player);
71 log_debug("waiting for children to terminate");
72 do {
73 pid = wait(&status);
74 if (pid == -1) {
75 if (errno != EINTR && errno != ECHILD)
76 fatal("wait");
77 } else if (WIFSIGNALED(status))
78 log_warnx("player terminated; signal %d",
79 WTERMSIG(status));
80 } while (pid != -1 || (pid == -1 && errno == EINTR));
82 log_info("terminating");
83 exit(0);
84 }
86 static void
87 main_status(void)
88 {
89 const char *cur;
91 switch (play_state) {
92 case STATE_STOPPED:
93 log_info("status: stopped");
94 break;
95 case STATE_PLAYING:
96 log_info("status: playing");
97 break;
98 case STATE_PAUSED:
99 log_info("status: paused");
100 break;
101 default:
102 log_info("status: unknown");
103 break;
106 if ((cur = playlist_current()) != NULL)
107 log_info("playing %s", cur);
108 else
109 log_info("not playing anything");
112 static void
113 main_sig_handler(int sig, short event, void *arg)
115 /*
116 * Normal signal handler rules don't apply because libevent
117 * decouples for us.
118 */
120 switch (sig) {
121 case SIGTERM:
122 case SIGINT:
123 main_shutdown();
124 break;
125 case SIGINFO:
126 main_status();
127 break;
128 default:
129 fatalx("unexpected signal %d", sig);
133 static void
134 main_dispatch_player(int sig, short event, void *d)
136 struct imsgev *iev = d;
137 struct imsgbuf *ibuf = &iev->ibuf;
138 struct imsg imsg;
139 ssize_t n;
140 int shut = 0;
142 if (event & EV_READ) {
143 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
144 fatal("imsg_read error");
145 if (n == 0) /* Connection closed */
146 shut = 1;
148 if (event & EV_WRITE) {
149 if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
150 fatal("msgbuf_write");
151 if (n == 0) /* Connection closed */
152 shut = 1;
155 for (;;) {
156 if ((n = imsg_get(ibuf, &imsg)) == -1)
157 fatal("imsg_get");
158 if (n == 0) /* No more messages. */
159 break;
161 switch (imsg.hdr.type) {
162 case IMSG_ERR:
163 /* TODO: remove current track from the playlist */
164 case IMSG_EOF:
165 main_playlist_advance();
166 break;
167 default:
168 log_debug("%s: error handling imsg %d", __func__,
169 imsg.hdr.type);
170 break;
172 imsg_free(&imsg);
175 if (!shut)
176 imsg_event_add(iev);
177 else {
178 /* This pipe is dead. Remove its event handler. */
179 event_del(&iev->ev);
180 event_loopexit(NULL);
184 static pid_t
185 start_child(enum amused_process proc, int fd)
187 const char *argv[5];
188 int argc = 0;
189 pid_t pid;
191 switch (pid = fork()) {
192 case -1:
193 fatal("cannot fork");
194 case 0:
195 break;
196 default:
197 close(fd);
198 return pid;
201 if (fd != 3) {
202 if (dup2(fd, 3) == -1)
203 fatal("cannot setup imsg fd");
204 } else if (fcntl(F_SETFD, 0) == -1)
205 fatal("cannot setup imsg fd");
207 argv[argc++] = argv0;
208 switch (proc) {
209 case PROC_MAIN:
210 fatal("can not start main process");
211 case PROC_PLAYER:
212 argv[argc++] = "-Tp";
213 break;
216 if (debug)
217 argv[argc++] = "-d";
218 if (verbose)
219 argv[argc++] = "-v";
220 argv[argc++] = NULL;
222 /* obnoxious casts */
223 execvp(argv0, (char *const *)argv);
224 fatal("execvp %s", argv0);
227 /* daemon main routine */
228 static __dead int
229 amused_main(void)
231 int pipe_main2player[2];
232 int control_fd;
234 log_init(debug, LOG_DAEMON);
235 log_setverbose(verbose);
236 log_procinit("main");
238 if (!debug)
239 daemon(1, 0);
241 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
242 PF_UNSPEC, pipe_main2player) == -1)
243 fatal("socketpair");
245 player_pid = start_child(PROC_PLAYER, pipe_main2player[1]);
247 event_init();
249 signal_set(&ev_sigint, SIGINT, main_sig_handler, NULL);
250 signal_set(&ev_sigterm, SIGTERM, main_sig_handler, NULL);
251 signal_set(&ev_siginfo, SIGINFO, main_sig_handler, NULL);
253 signal_add(&ev_sigint, NULL);
254 signal_add(&ev_sigterm, NULL);
255 signal_add(&ev_siginfo, NULL);
257 signal(SIGHUP, SIG_IGN);
258 signal(SIGCHLD, SIG_IGN);
259 signal(SIGPIPE, SIG_IGN);
261 iev_player = xmalloc(sizeof(*iev_player));
262 imsg_init(&iev_player->ibuf, pipe_main2player[0]);
263 iev_player->handler = main_dispatch_player;
264 iev_player->events = EV_READ;
265 event_set(&iev_player->ev, iev_player->ibuf.fd, iev_player->events,
266 iev_player->handler, iev_player);
267 event_add(&iev_player->ev, NULL);
269 if ((control_fd = control_init(csock)) == -1)
270 fatal("control socket setup failed %s", csock);
271 control_listen(control_fd);
273 if (pledge("stdio rpath unix sendfd", NULL) == -1)
274 fatal("pledge");
276 log_info("startup");
277 event_dispatch();
278 main_shutdown();
281 int
282 main(int argc, char **argv)
284 int ch, proc = PROC_MAIN;
286 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized */
287 log_setverbose(1);
289 argv0 = argv[0];
290 if (argv0 == NULL)
291 argv0 = "amused";
293 while ((ch = getopt(argc, argv, "ds:T:vV")) != -1) {
294 switch (ch) {
295 case 'd':
296 debug = 1;
297 break;
298 case 's':
299 free(csock);
300 csock = xstrdup(optarg);
301 break;
302 case 'T':
303 switch (*optarg) {
304 case 'p':
305 proc = PROC_PLAYER;
306 break;
307 default:
308 usage();
310 break;
311 case 'v':
312 verbose++;
313 break;
314 case 'V':
315 printf("%s version %s\n", getprogname(),
316 AMUSED_VERSION);
317 exit(0);
318 default:
319 usage();
322 argv += optind;
323 argc -= optind;
325 if (proc == PROC_PLAYER)
326 exit(player(debug, verbose));
328 if (csock == NULL)
329 xasprintf(&csock, "/tmp/amused-%d", getuid());
331 if (argc == 0)
332 amused_main();
333 else
334 ctl(argc, argv);
335 return 0;
338 void
339 imsg_event_add(struct imsgev *iev)
341 iev->events = EV_READ;
342 if (iev->ibuf.w.queued)
343 iev->events |= EV_WRITE;
345 event_del(&iev->ev);
346 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
347 event_add(&iev->ev, NULL);
350 int
351 imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
352 pid_t pid, int fd, const void *data, uint16_t datalen)
354 int ret;
356 if ((ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data,
357 datalen)) != -1)
358 imsg_event_add(iev);
360 return ret;
363 int
364 main_send_player(uint16_t type, int fd, const void *data, uint16_t datalen)
366 return imsg_compose_event(iev_player, type, 0, 0, fd, data, datalen);
369 int
370 main_play_song(const char *song)
372 char path[PATH_MAX] = { 0 };
373 int fd;
375 strlcpy(path, song, sizeof(path));
376 if ((fd = open(path, O_RDONLY)) == -1) {
377 log_warn("open %s", path);
378 return -1;
381 play_state = STATE_PLAYING;
382 imsg_compose_event(iev_player, IMSG_PLAY, 0, 0, fd,
383 path, sizeof(path));
384 return 0;
387 void
388 main_playlist_advance(void)
390 const char *song;
392 for (;;) {
393 song = playlist_advance();
394 if (song == NULL)
395 return;
397 if (main_play_song(song))
398 break;
400 /* TODO: remove the song from the playlist */
404 void
405 main_restart_track(void)
407 const char *song;
409 song = playlist_current();
410 if (song == NULL)
411 return;
413 if (main_play_song(song))
414 return;
416 /* TODO: remove the song from the playlist */
417 main_playlist_advance();
420 void
421 main_enqueue(struct imsgev *iev, struct imsg *imsg)
423 size_t datalen;
424 char path[PATH_MAX] = { 0 };
425 const char *err = NULL;
427 datalen = IMSG_DATA_SIZE(*imsg);
428 if (datalen != sizeof(path)) {
429 err = "data size mismatch";
430 goto err;
433 memcpy(path, imsg->data, sizeof(path));
434 if (path[datalen-1] != '\0') {
435 err = "malformed data";
436 goto err;
439 playlist_enqueue(path);
440 imsg_compose_event(iev, IMSG_CTL_ADD, 0, 0, -1, path, sizeof(path));
441 return;
442 err:
443 imsg_compose_event(iev, IMSG_CTL_ERR, 0, 0, -1, err, strlen(err)+1);