Blame


1 3baa2617 2022-02-16 op /*
2 3baa2617 2022-02-16 op * Copyright (c) 2022 Omar Polo <op@openbsd.org>
3 3baa2617 2022-02-16 op *
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.
7 3baa2617 2022-02-16 op *
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.
15 3baa2617 2022-02-16 op */
16 3baa2617 2022-02-16 op
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>
22 3baa2617 2022-02-16 op
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>
36 3baa2617 2022-02-16 op
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"
42 3baa2617 2022-02-16 op
43 3baa2617 2022-02-16 op struct sio_hdl *hdl;
44 3baa2617 2022-02-16 op char *csock = NULL;
45 3baa2617 2022-02-16 op int debug;
46 3baa2617 2022-02-16 op int verbose;
47 3baa2617 2022-02-16 op struct imsgev *iev_player;
48 3baa2617 2022-02-16 op
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 3baa2617 2022-02-16 op
54 3baa2617 2022-02-16 op enum amused_process {
55 3baa2617 2022-02-16 op PROC_MAIN,
56 3baa2617 2022-02-16 op PROC_PLAYER,
57 3baa2617 2022-02-16 op };
58 3baa2617 2022-02-16 op
59 3baa2617 2022-02-16 op __dead void
60 3baa2617 2022-02-16 op main_shutdown(void)
61 3baa2617 2022-02-16 op {
62 3baa2617 2022-02-16 op pid_t pid;
63 3baa2617 2022-02-16 op int status;
64 3baa2617 2022-02-16 op
65 3baa2617 2022-02-16 op /* close pipes. */
66 3baa2617 2022-02-16 op msgbuf_clear(&iev_player->ibuf.w);
67 3baa2617 2022-02-16 op close(iev_player->ibuf.fd);
68 3baa2617 2022-02-16 op free(iev_player);
69 3baa2617 2022-02-16 op
70 3baa2617 2022-02-16 op log_debug("waiting for children to terminate");
71 3baa2617 2022-02-16 op do {
72 3baa2617 2022-02-16 op pid = wait(&status);
73 3baa2617 2022-02-16 op if (pid == -1) {
74 3baa2617 2022-02-16 op if (errno != EINTR && errno != ECHILD)
75 3baa2617 2022-02-16 op fatal("wait");
76 3baa2617 2022-02-16 op } else if (WIFSIGNALED(status))
77 3baa2617 2022-02-16 op log_warnx("player terminated; signal %d",
78 3baa2617 2022-02-16 op WTERMSIG(status));
79 3baa2617 2022-02-16 op } while (pid != -1 || (pid == -1 && errno == EINTR));
80 3baa2617 2022-02-16 op
81 3baa2617 2022-02-16 op log_info("terminating");
82 3baa2617 2022-02-16 op exit(0);
83 3baa2617 2022-02-16 op }
84 3baa2617 2022-02-16 op
85 3baa2617 2022-02-16 op static void
86 3baa2617 2022-02-16 op main_sig_handler(int sig, short event, void *arg)
87 3baa2617 2022-02-16 op {
88 3baa2617 2022-02-16 op /*
89 3baa2617 2022-02-16 op * Normal signal handler rules don't apply because libevent
90 3baa2617 2022-02-16 op * decouples for us.
91 3baa2617 2022-02-16 op */
92 3baa2617 2022-02-16 op
93 3baa2617 2022-02-16 op switch (sig) {
94 3baa2617 2022-02-16 op case SIGTERM:
95 3baa2617 2022-02-16 op case SIGINT:
96 3baa2617 2022-02-16 op main_shutdown();
97 3baa2617 2022-02-16 op break;
98 3baa2617 2022-02-16 op default:
99 3baa2617 2022-02-16 op fatalx("unexpected signal %d", sig);
100 3baa2617 2022-02-16 op }
101 3baa2617 2022-02-16 op }
102 3baa2617 2022-02-16 op
103 3baa2617 2022-02-16 op static void
104 3baa2617 2022-02-16 op main_dispatch_player(int sig, short event, void *d)
105 3baa2617 2022-02-16 op {
106 3baa2617 2022-02-16 op struct imsgev *iev = d;
107 3baa2617 2022-02-16 op struct imsgbuf *ibuf = &iev->ibuf;
108 3baa2617 2022-02-16 op struct imsg imsg;
109 3baa2617 2022-02-16 op ssize_t n;
110 3baa2617 2022-02-16 op int shut = 0;
111 3baa2617 2022-02-16 op const char *song;
112 3baa2617 2022-02-16 op
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 */
117 3baa2617 2022-02-16 op shut = 1;
118 3baa2617 2022-02-16 op }
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 */
123 3baa2617 2022-02-16 op shut = 1;
124 3baa2617 2022-02-16 op }
125 3baa2617 2022-02-16 op
126 3baa2617 2022-02-16 op for (;;) {
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. */
130 3baa2617 2022-02-16 op break;
131 3baa2617 2022-02-16 op
132 3baa2617 2022-02-16 op switch (imsg.hdr.type) {
133 3baa2617 2022-02-16 op case IMSG_EOF:
134 3baa2617 2022-02-16 op case IMSG_ERR:
135 3baa2617 2022-02-16 op song = playlist_advance();
136 3baa2617 2022-02-16 op if (song == NULL)
137 3baa2617 2022-02-16 op break;
138 3baa2617 2022-02-16 op /* XXX: watch out for failures! */
139 3baa2617 2022-02-16 op main_play_song(song);
140 3baa2617 2022-02-16 op break;
141 3baa2617 2022-02-16 op default:
142 3baa2617 2022-02-16 op log_debug("%s: error handling imsg %d", __func__,
143 3baa2617 2022-02-16 op imsg.hdr.type);
144 3baa2617 2022-02-16 op break;
145 3baa2617 2022-02-16 op }
146 3baa2617 2022-02-16 op imsg_free(&imsg);
147 3baa2617 2022-02-16 op }
148 3baa2617 2022-02-16 op
149 3baa2617 2022-02-16 op if (!shut)
150 3baa2617 2022-02-16 op imsg_event_add(iev);
151 3baa2617 2022-02-16 op else {
152 3baa2617 2022-02-16 op /* This pipe is dead. Remove its event handler. */
153 3baa2617 2022-02-16 op event_del(&iev->ev);
154 3baa2617 2022-02-16 op event_loopexit(NULL);
155 3baa2617 2022-02-16 op }
156 3baa2617 2022-02-16 op }
157 3baa2617 2022-02-16 op
158 3baa2617 2022-02-16 op static pid_t
159 3baa2617 2022-02-16 op start_child(enum amused_process proc, int fd)
160 3baa2617 2022-02-16 op {
161 3baa2617 2022-02-16 op const char *argv[5];
162 3baa2617 2022-02-16 op int argc = 0;
163 3baa2617 2022-02-16 op pid_t pid;
164 3baa2617 2022-02-16 op
165 3baa2617 2022-02-16 op switch (pid = fork()) {
166 3baa2617 2022-02-16 op case -1:
167 3baa2617 2022-02-16 op fatal("cannot fork");
168 3baa2617 2022-02-16 op case 0:
169 3baa2617 2022-02-16 op break;
170 3baa2617 2022-02-16 op default:
171 3baa2617 2022-02-16 op close(fd);
172 3baa2617 2022-02-16 op return pid;
173 3baa2617 2022-02-16 op }
174 3baa2617 2022-02-16 op
175 3baa2617 2022-02-16 op if (fd != 3) {
176 3baa2617 2022-02-16 op if (dup2(fd, 3) == -1)
177 3baa2617 2022-02-16 op fatal("cannot setup imsg fd");
178 3baa2617 2022-02-16 op } else if (fcntl(F_SETFD, 0) == -1)
179 3baa2617 2022-02-16 op fatal("cannot setup imsg fd");
180 3baa2617 2022-02-16 op
181 3baa2617 2022-02-16 op argv[argc++] = argv0;
182 3baa2617 2022-02-16 op switch (proc) {
183 3baa2617 2022-02-16 op case PROC_MAIN:
184 3baa2617 2022-02-16 op fatal("can not start main process");
185 3baa2617 2022-02-16 op case PROC_PLAYER:
186 3baa2617 2022-02-16 op argv[argc++] = "-Tp";
187 3baa2617 2022-02-16 op break;
188 3baa2617 2022-02-16 op }
189 3baa2617 2022-02-16 op
190 3baa2617 2022-02-16 op if (debug)
191 3baa2617 2022-02-16 op argv[argc++] = "-d";
192 3baa2617 2022-02-16 op if (verbose)
193 3baa2617 2022-02-16 op argv[argc++] = "-v";
194 3baa2617 2022-02-16 op argv[argc++] = NULL;
195 3baa2617 2022-02-16 op
196 3baa2617 2022-02-16 op /* obnoxious casts */
197 3baa2617 2022-02-16 op execvp(argv0, (char *const *)argv);
198 3baa2617 2022-02-16 op fatal("execvp %s", argv0);
199 3baa2617 2022-02-16 op }
200 3baa2617 2022-02-16 op
201 3baa2617 2022-02-16 op /* daemon main routine */
202 3baa2617 2022-02-16 op static __dead int
203 3baa2617 2022-02-16 op amused_main(void)
204 3baa2617 2022-02-16 op {
205 3baa2617 2022-02-16 op int pipe_main2player[2];
206 3baa2617 2022-02-16 op int control_fd;
207 3baa2617 2022-02-16 op
208 3baa2617 2022-02-16 op log_init(debug, LOG_DAEMON);
209 3baa2617 2022-02-16 op log_setverbose(verbose);
210 3baa2617 2022-02-16 op log_procinit("main");
211 3baa2617 2022-02-16 op
212 3baa2617 2022-02-16 op if (!debug)
213 3baa2617 2022-02-16 op daemon(1, 0);
214 3baa2617 2022-02-16 op
215 3baa2617 2022-02-16 op if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
216 3baa2617 2022-02-16 op PF_UNSPEC, pipe_main2player) == -1)
217 3baa2617 2022-02-16 op fatal("socketpair");
218 3baa2617 2022-02-16 op
219 3baa2617 2022-02-16 op player_pid = start_child(PROC_PLAYER, pipe_main2player[1]);
220 3baa2617 2022-02-16 op
221 3baa2617 2022-02-16 op event_init();
222 3baa2617 2022-02-16 op
223 3baa2617 2022-02-16 op signal_set(&ev_sigint, SIGINT, main_sig_handler, NULL);
224 3baa2617 2022-02-16 op signal_set(&ev_sigterm, SIGTERM, main_sig_handler, NULL);
225 3baa2617 2022-02-16 op
226 3baa2617 2022-02-16 op signal_add(&ev_sigint, NULL);
227 3baa2617 2022-02-16 op signal_add(&ev_sigterm, NULL);
228 3baa2617 2022-02-16 op
229 3baa2617 2022-02-16 op signal(SIGHUP, SIG_IGN);
230 3baa2617 2022-02-16 op signal(SIGCHLD, SIG_IGN);
231 3baa2617 2022-02-16 op signal(SIGPIPE, SIG_IGN);
232 3baa2617 2022-02-16 op
233 3baa2617 2022-02-16 op iev_player = xmalloc(sizeof(*iev_player));
234 3baa2617 2022-02-16 op imsg_init(&iev_player->ibuf, pipe_main2player[0]);
235 3baa2617 2022-02-16 op iev_player->handler = main_dispatch_player;
236 3baa2617 2022-02-16 op iev_player->events = EV_READ;
237 3baa2617 2022-02-16 op event_set(&iev_player->ev, iev_player->ibuf.fd, iev_player->events,
238 3baa2617 2022-02-16 op iev_player->handler, iev_player);
239 3baa2617 2022-02-16 op event_add(&iev_player->ev, NULL);
240 3baa2617 2022-02-16 op
241 3baa2617 2022-02-16 op if ((control_fd = control_init(csock)) == -1)
242 3baa2617 2022-02-16 op fatal("control socket setup failed %s", csock);
243 3baa2617 2022-02-16 op control_listen(control_fd);
244 3baa2617 2022-02-16 op
245 3baa2617 2022-02-16 op if (pledge("stdio rpath unix sendfd", NULL) == -1)
246 3baa2617 2022-02-16 op fatal("pledge");
247 3baa2617 2022-02-16 op
248 3baa2617 2022-02-16 op log_info("startup");
249 3baa2617 2022-02-16 op event_dispatch();
250 3baa2617 2022-02-16 op main_shutdown();
251 3baa2617 2022-02-16 op }
252 3baa2617 2022-02-16 op
253 3baa2617 2022-02-16 op int
254 3baa2617 2022-02-16 op main(int argc, char **argv)
255 3baa2617 2022-02-16 op {
256 3baa2617 2022-02-16 op int ch, proc = PROC_MAIN;
257 3baa2617 2022-02-16 op
258 3baa2617 2022-02-16 op log_init(1, LOG_DAEMON); /* Log to stderr until daemonized */
259 3baa2617 2022-02-16 op log_setverbose(1);
260 3baa2617 2022-02-16 op
261 3baa2617 2022-02-16 op argv0 = argv[0];
262 3baa2617 2022-02-16 op if (argv0 == NULL)
263 3baa2617 2022-02-16 op argv0 = "amused";
264 3baa2617 2022-02-16 op
265 3baa2617 2022-02-16 op while ((ch = getopt(argc, argv, "ds:T:vV")) != -1) {
266 3baa2617 2022-02-16 op switch (ch) {
267 3baa2617 2022-02-16 op case 'd':
268 3baa2617 2022-02-16 op debug = 1;
269 3baa2617 2022-02-16 op break;
270 3baa2617 2022-02-16 op case 's':
271 3baa2617 2022-02-16 op free(csock);
272 3baa2617 2022-02-16 op csock = xstrdup(optarg);
273 3baa2617 2022-02-16 op break;
274 3baa2617 2022-02-16 op case 'T':
275 3baa2617 2022-02-16 op switch (*optarg) {
276 3baa2617 2022-02-16 op case 'p':
277 3baa2617 2022-02-16 op proc = PROC_PLAYER;
278 3baa2617 2022-02-16 op break;
279 3baa2617 2022-02-16 op default:
280 3baa2617 2022-02-16 op usage();
281 3baa2617 2022-02-16 op }
282 3baa2617 2022-02-16 op break;
283 3baa2617 2022-02-16 op case 'v':
284 3baa2617 2022-02-16 op verbose++;
285 3baa2617 2022-02-16 op break;
286 3baa2617 2022-02-16 op case 'V':
287 3baa2617 2022-02-16 op printf("%s version %s\n", getprogname(),
288 3baa2617 2022-02-16 op AMUSED_VERSION);
289 3baa2617 2022-02-16 op exit(0);
290 3baa2617 2022-02-16 op default:
291 3baa2617 2022-02-16 op usage();
292 3baa2617 2022-02-16 op }
293 3baa2617 2022-02-16 op }
294 3baa2617 2022-02-16 op argv += optind;
295 3baa2617 2022-02-16 op argc -= optind;
296 3baa2617 2022-02-16 op
297 3baa2617 2022-02-16 op if (proc == PROC_PLAYER)
298 3baa2617 2022-02-16 op exit(player(debug, verbose));
299 3baa2617 2022-02-16 op
300 3baa2617 2022-02-16 op if (csock == NULL)
301 3baa2617 2022-02-16 op xasprintf(&csock, "/tmp/amused-%d", getuid());
302 3baa2617 2022-02-16 op
303 3baa2617 2022-02-16 op if (argc == 0)
304 3baa2617 2022-02-16 op amused_main();
305 3baa2617 2022-02-16 op else
306 3baa2617 2022-02-16 op ctl(argc, argv);
307 3baa2617 2022-02-16 op return 0;
308 3baa2617 2022-02-16 op }
309 3baa2617 2022-02-16 op
310 3baa2617 2022-02-16 op void
311 3baa2617 2022-02-16 op imsg_event_add(struct imsgev *iev)
312 3baa2617 2022-02-16 op {
313 3baa2617 2022-02-16 op iev->events = EV_READ;
314 3baa2617 2022-02-16 op if (iev->ibuf.w.queued)
315 3baa2617 2022-02-16 op iev->events |= EV_WRITE;
316 3baa2617 2022-02-16 op
317 3baa2617 2022-02-16 op event_del(&iev->ev);
318 3baa2617 2022-02-16 op event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
319 3baa2617 2022-02-16 op event_add(&iev->ev, NULL);
320 3baa2617 2022-02-16 op }
321 3baa2617 2022-02-16 op
322 3baa2617 2022-02-16 op int
323 3baa2617 2022-02-16 op imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
324 3baa2617 2022-02-16 op pid_t pid, int fd, const void *data, uint16_t datalen)
325 3baa2617 2022-02-16 op {
326 3baa2617 2022-02-16 op int ret;
327 3baa2617 2022-02-16 op
328 3baa2617 2022-02-16 op if ((ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data,
329 3baa2617 2022-02-16 op datalen)) != -1)
330 3baa2617 2022-02-16 op imsg_event_add(iev);
331 3baa2617 2022-02-16 op
332 3baa2617 2022-02-16 op return ret;
333 3baa2617 2022-02-16 op }
334 3baa2617 2022-02-16 op
335 3baa2617 2022-02-16 op int
336 3baa2617 2022-02-16 op main_play_song(const char *song)
337 3baa2617 2022-02-16 op {
338 3baa2617 2022-02-16 op char path[PATH_MAX] = { 0 };
339 3baa2617 2022-02-16 op int fd;
340 3baa2617 2022-02-16 op
341 3baa2617 2022-02-16 op strlcpy(path, song, sizeof(path));
342 3baa2617 2022-02-16 op if ((fd = open(path, O_RDONLY)) == -1) {
343 3baa2617 2022-02-16 op #if todo
344 3baa2617 2022-02-16 op log_warn("open %s", path);
345 3baa2617 2022-02-16 op return -1;
346 3baa2617 2022-02-16 op #else
347 3baa2617 2022-02-16 op fatal("open %s", path);
348 3baa2617 2022-02-16 op #endif
349 3baa2617 2022-02-16 op }
350 3baa2617 2022-02-16 op
351 3baa2617 2022-02-16 op imsg_compose_event(iev_player, IMSG_PLAY, 0, 0, fd,
352 3baa2617 2022-02-16 op path, sizeof(path));
353 3baa2617 2022-02-16 op return 0;
354 3baa2617 2022-02-16 op }
355 3baa2617 2022-02-16 op
356 3baa2617 2022-02-16 op void
357 3baa2617 2022-02-16 op main_enqueue(struct imsgev *iev, struct imsg *imsg)
358 3baa2617 2022-02-16 op {
359 3baa2617 2022-02-16 op size_t datalen;
360 3baa2617 2022-02-16 op char path[PATH_MAX] = { 0 };
361 3baa2617 2022-02-16 op const char *err = NULL;
362 3baa2617 2022-02-16 op
363 3baa2617 2022-02-16 op datalen = IMSG_DATA_SIZE(*imsg);
364 3baa2617 2022-02-16 op if (datalen != sizeof(path)) {
365 3baa2617 2022-02-16 op err = "data size mismatch";
366 3baa2617 2022-02-16 op goto err;
367 3baa2617 2022-02-16 op }
368 3baa2617 2022-02-16 op
369 3baa2617 2022-02-16 op memcpy(path, imsg->data, sizeof(path));
370 3baa2617 2022-02-16 op if (path[datalen-1] != '\0') {
371 3baa2617 2022-02-16 op err = "malformed data";
372 3baa2617 2022-02-16 op goto err;
373 3baa2617 2022-02-16 op }
374 3baa2617 2022-02-16 op
375 3baa2617 2022-02-16 op playlist_enqueue(path);
376 3baa2617 2022-02-16 op imsg_compose_event(iev, IMSG_CTL_ADD, 0, 0, -1, path, sizeof(path));
377 3baa2617 2022-02-16 op return;
378 3baa2617 2022-02-16 op err:
379 3baa2617 2022-02-16 op imsg_compose_event(iev, IMSG_CTL_ERR, 0, 0, -1, err, strlen(err)+1);
380 3baa2617 2022-02-16 op }