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/socket.h>
19 3baa2617 2022-02-16 op #include <sys/queue.h>
20 3baa2617 2022-02-16 op #include <sys/uio.h>
21 3baa2617 2022-02-16 op #include <sys/un.h>
23 3baa2617 2022-02-16 op #include <errno.h>
24 3baa2617 2022-02-16 op #include <event.h>
25 1d673950 2022-03-03 op #include <fcntl.h>
26 3baa2617 2022-02-16 op #include <limits.h>
27 3baa2617 2022-02-16 op #include <stdio.h>
28 3baa2617 2022-02-16 op #include <stdlib.h>
29 3baa2617 2022-02-16 op #include <stdint.h>
30 3baa2617 2022-02-16 op #include <string.h>
31 3baa2617 2022-02-16 op #include <syslog.h>
32 3baa2617 2022-02-16 op #include <unistd.h>
33 3baa2617 2022-02-16 op #include <imsg.h>
35 3baa2617 2022-02-16 op #include "amused.h"
36 3baa2617 2022-02-16 op #include "log.h"
37 bb3f279f 2022-02-16 op #include "playlist.h"
38 3baa2617 2022-02-16 op #include "xmalloc.h"
40 3baa2617 2022-02-16 op static struct imsgbuf *ibuf;
42 3baa2617 2022-02-16 op int ctl_noarg(struct parse_result *, int, char **);
43 3baa2617 2022-02-16 op int ctl_add(struct parse_result *, int, char **);
44 8e916714 2022-02-17 op int ctl_show(struct parse_result *, int, char **);
45 14be015f 2022-02-17 op int ctl_load(struct parse_result *, int, char **);
46 a913de21 2022-02-17 op int ctl_jump(struct parse_result *, int, char **);
47 310ef57c 2022-02-19 op int ctl_repeat(struct parse_result *, int, char **);
48 90122a37 2022-05-14 op int ctl_monitor(struct parse_result *, int, char **);
50 3baa2617 2022-02-16 op struct ctl_command ctl_commands[] = {
51 3baa2617 2022-02-16 op { "play", PLAY, ctl_noarg, "" },
52 3baa2617 2022-02-16 op { "pause", PAUSE, ctl_noarg, "" },
53 3baa2617 2022-02-16 op { "toggle", TOGGLE, ctl_noarg, "" },
54 3baa2617 2022-02-16 op { "stop", STOP, ctl_noarg, "" },
55 3baa2617 2022-02-16 op { "restart", RESTART, ctl_noarg, "" },
56 3baa2617 2022-02-16 op { "add", ADD, ctl_add, "files...", 1 },
57 3baa2617 2022-02-16 op { "flush", FLUSH, ctl_noarg, "" },
58 714abf92 2022-02-19 op { "show", SHOW, ctl_show, "[-p]" },
59 bb3f279f 2022-02-16 op { "status", STATUS, ctl_noarg, "" },
60 af27e631 2022-02-17 op { "next", NEXT, ctl_noarg, "" },
61 af27e631 2022-02-17 op { "prev", PREV, ctl_noarg, "" },
62 14be015f 2022-02-17 op { "load", LOAD, ctl_load, "[file]", 1 },
63 a913de21 2022-02-17 op { "jump", JUMP, ctl_jump, "pattern" },
64 310ef57c 2022-02-19 op { "repeat", REPEAT, ctl_repeat, "one|all on|off" },
65 90122a37 2022-05-14 op { "monitor", MONITOR, ctl_monitor, "[events]" },
72 3baa2617 2022-02-16 op fprintf(stderr, "usage: %s [-dv] [-s socket]\n", getprogname());
76 3baa2617 2022-02-16 op static __dead void
77 3baa2617 2022-02-16 op ctl_usage(struct ctl_command *ctl)
79 3baa2617 2022-02-16 op fprintf(stderr, "usage: %s [-v] [-s socket] %s %s\n", getprogname(),
80 3baa2617 2022-02-16 op ctl->name, ctl->usage);
85 3baa2617 2022-02-16 op parse(int argc, char **argv)
87 3baa2617 2022-02-16 op struct ctl_command *ctl = NULL;
88 3baa2617 2022-02-16 op struct parse_result res;
89 1d673950 2022-03-03 op const char *argv0;
90 3baa2617 2022-02-16 op int i, status;
92 3baa2617 2022-02-16 op memset(&res, 0, sizeof(res));
94 1d673950 2022-03-03 op if ((argv0 = argv[0]) == NULL)
95 1d673950 2022-03-03 op argv0 = "status";
97 3baa2617 2022-02-16 op for (i = 0; ctl_commands[i].name != NULL; ++i) {
98 1d673950 2022-03-03 op if (strncmp(ctl_commands[i].name, argv0, strlen(argv0))
100 3baa2617 2022-02-16 op if (ctl != NULL) {
101 3baa2617 2022-02-16 op fprintf(stderr,
102 1d673950 2022-03-03 op "ambiguous argument: %s\n", argv0);
105 3baa2617 2022-02-16 op ctl = &ctl_commands[i];
109 3baa2617 2022-02-16 op if (ctl == NULL) {
110 3baa2617 2022-02-16 op fprintf(stderr, "unknown argument: %s\n", argv[0]);
114 3baa2617 2022-02-16 op res.action = ctl->action;
115 3baa2617 2022-02-16 op res.ctl = ctl;
117 3baa2617 2022-02-16 op if (!ctl->has_pledge) {
118 3baa2617 2022-02-16 op /* pledge(2) default if command doesn't have its own */
119 3baa2617 2022-02-16 op if (pledge("stdio", NULL) == -1)
120 3baa2617 2022-02-16 op fatal("pledge");
123 3baa2617 2022-02-16 op status = ctl->main(&res, argc, argv);
124 3baa2617 2022-02-16 op close(ibuf->fd);
126 3baa2617 2022-02-16 op return status;
130 fad0fb69 2022-05-28 op load_files(struct parse_result *res, int *ret)
133 7a427ecd 2022-02-17 op const char *file;
134 7a427ecd 2022-02-17 op char *line = NULL;
135 c00c1428 2022-03-25 op char path[PATH_MAX], cwd[PATH_MAX];
136 c00c1428 2022-03-25 op size_t linesize = 0, i = 0, n;
137 3af93963 2022-03-02 op ssize_t linelen, curr = -1;
139 ec1fb0c7 2022-02-17 op if (res->file == NULL)
141 ec1fb0c7 2022-02-17 op else if ((f = fopen(res->file, "r")) == NULL) {
142 ec1fb0c7 2022-02-17 op log_warn("can't open %s", res->file);
147 c00c1428 2022-03-25 op if (getcwd(cwd, sizeof(cwd)) == NULL)
148 c00c1428 2022-03-25 op fatal("getcwd");
150 ec1fb0c7 2022-02-17 op while ((linelen = getline(&line, &linesize, f)) != -1) {
151 7a427ecd 2022-02-17 op if (linelen == 0)
153 7a427ecd 2022-02-17 op line[linelen-1] = '\0';
155 1dd3b054 2022-03-25 op if (!strncmp(file, "> ", 2)) {
158 1dd3b054 2022-03-25 op } else if (!strncmp(file, " ", 2))
161 1dd3b054 2022-03-25 op if (!strncmp(file, "./", 2))
164 d5120106 2022-03-26 op memset(path, 0, sizeof(path));
165 c00c1428 2022-03-25 op if (*file == '/')
166 c00c1428 2022-03-25 op n = strlcpy(path, file, sizeof(path));
168 c00c1428 2022-03-25 op n = snprintf(path, sizeof(path), "%s/%s", cwd, file);
170 c00c1428 2022-03-25 op if (n >= sizeof(path)) {
171 c00c1428 2022-03-25 op log_warnx("path too long: %s", file);
176 7a427ecd 2022-02-17 op imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
177 7a427ecd 2022-02-17 op path, sizeof(path));
181 ec1fb0c7 2022-02-17 op if (ferror(f))
182 7a427ecd 2022-02-17 op fatal("getline");
185 3af93963 2022-03-02 op if (i == 0) {
190 7a427ecd 2022-02-17 op imsg_compose(ibuf, IMSG_CTL_COMMIT, 0, 0, -1,
191 3af93963 2022-03-02 op &curr, sizeof(curr));
192 7a427ecd 2022-02-17 op imsg_flush(ibuf);
196 fad0fb69 2022-05-28 op static const char *
197 fad0fb69 2022-05-28 op imsg_strerror(struct imsg *imsg)
199 fad0fb69 2022-05-28 op size_t datalen;
200 fad0fb69 2022-05-28 op const char *msg;
202 fad0fb69 2022-05-28 op datalen = IMSG_DATA_SIZE(*imsg);
203 fad0fb69 2022-05-28 op msg = imsg->data;
204 fad0fb69 2022-05-28 op if (datalen == 0 || msg[datalen-1] != '\0')
205 fad0fb69 2022-05-28 op fatalx("malformed error message");
210 fad0fb69 2022-05-28 op static const char *
211 fad0fb69 2022-05-28 op imsg_name(int type)
213 6b47a39f 2022-02-21 op switch (type) {
214 6b47a39f 2022-02-21 op case IMSG_CTL_PLAY:
215 fad0fb69 2022-05-28 op return "play";
216 6b47a39f 2022-02-21 op case IMSG_CTL_TOGGLE_PLAY:
217 fad0fb69 2022-05-28 op return "toggle";
218 6b47a39f 2022-02-21 op case IMSG_CTL_PAUSE:
219 fad0fb69 2022-05-28 op return "pause";
220 6b47a39f 2022-02-21 op case IMSG_CTL_STOP:
221 fad0fb69 2022-05-28 op return "stop";
222 6b47a39f 2022-02-21 op case IMSG_CTL_RESTART:
223 fad0fb69 2022-05-28 op return "restart";
224 6b47a39f 2022-02-21 op case IMSG_CTL_FLUSH:
225 fad0fb69 2022-05-28 op return "flush";
226 6b47a39f 2022-02-21 op case IMSG_CTL_NEXT:
227 fad0fb69 2022-05-28 op return "next";
228 6b47a39f 2022-02-21 op case IMSG_CTL_PREV:
229 fad0fb69 2022-05-28 op return "prev";
230 6b47a39f 2022-02-21 op case IMSG_CTL_JUMP:
231 fad0fb69 2022-05-28 op return "jump";
232 6b47a39f 2022-02-21 op case IMSG_CTL_REPEAT:
233 fad0fb69 2022-05-28 op return "repeat";
234 6b47a39f 2022-02-21 op case IMSG_CTL_ADD:
235 fad0fb69 2022-05-28 op return "add";
236 6b47a39f 2022-02-21 op case IMSG_CTL_COMMIT:
237 fad0fb69 2022-05-28 op return "load";
239 fad0fb69 2022-05-28 op return "unknown";
244 3baa2617 2022-02-16 op ctlaction(struct parse_result *res)
246 fad0fb69 2022-05-28 op char path[PATH_MAX];
247 3baa2617 2022-02-16 op struct imsg imsg;
248 fad0fb69 2022-05-28 op struct player_status ps;
249 fad0fb69 2022-05-28 op size_t datalen;
251 fad0fb69 2022-05-28 op int i, type, ret = 0, done = 1;
253 3baa2617 2022-02-16 op switch (res->action) {
255 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
256 00f500ff 2022-02-19 op if (verbose) {
257 00f500ff 2022-02-19 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
263 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
266 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
267 00f500ff 2022-02-19 op if (verbose) {
268 00f500ff 2022-02-19 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
274 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
276 3baa2617 2022-02-16 op case RESTART:
277 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_RESTART, 0, 0, -1, NULL, 0);
278 00f500ff 2022-02-19 op if (verbose) {
279 00f500ff 2022-02-19 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
286 d903ec9a 2022-05-29 op for (i = 0; res->files[i] != NULL; ++i) {
287 fad0fb69 2022-05-28 op memset(&path, 0, sizeof(path));
288 d903ec9a 2022-05-29 op if (realpath(res->files[i], path) == NULL) {
289 d903ec9a 2022-05-29 op log_warn("realpath %s", res->files[i]);
293 fad0fb69 2022-05-28 op imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
294 fad0fb69 2022-05-28 op path, sizeof(path));
296 fad0fb69 2022-05-28 op ret = i == 0;
299 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
303 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
307 bb3f279f 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
310 af27e631 2022-02-17 op imsg_compose(ibuf, IMSG_CTL_NEXT, 0, 0, -1, NULL, 0);
311 00f500ff 2022-02-19 op if (verbose) {
312 00f500ff 2022-02-19 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
318 af27e631 2022-02-17 op imsg_compose(ibuf, IMSG_CTL_PREV, 0, 0, -1, NULL, 0);
319 00f500ff 2022-02-19 op if (verbose) {
320 00f500ff 2022-02-19 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
327 7a427ecd 2022-02-17 op imsg_compose(ibuf, IMSG_CTL_BEGIN, 0, 0, -1, NULL, 0);
331 fad0fb69 2022-05-28 op memset(path, 0, sizeof(path));
332 fad0fb69 2022-05-28 op strlcpy(path, res->file, sizeof(path));
333 fad0fb69 2022-05-28 op imsg_compose(ibuf, IMSG_CTL_JUMP, 0, 0, -1,
334 fad0fb69 2022-05-28 op path, sizeof(path));
337 310ef57c 2022-02-19 op imsg_compose(ibuf, IMSG_CTL_REPEAT, 0, 0, -1,
338 310ef57c 2022-02-19 op &res->rep, sizeof(res->rep));
340 6b47a39f 2022-02-21 op case MONITOR:
342 6b47a39f 2022-02-21 op imsg_compose(ibuf, IMSG_CTL_MONITOR, 0, 0, -1,
346 3baa2617 2022-02-16 op /* action not expected */
347 3baa2617 2022-02-16 op fatalx("invalid action %u", res->action);
351 3baa2617 2022-02-16 op if (ret != 0)
354 3baa2617 2022-02-16 op imsg_flush(ibuf);
357 3baa2617 2022-02-16 op while (!done) {
358 3baa2617 2022-02-16 op if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
359 3baa2617 2022-02-16 op fatalx("imsg_read error");
361 3baa2617 2022-02-16 op fatalx("pipe closed");
363 3baa2617 2022-02-16 op while (!done) {
364 3baa2617 2022-02-16 op if ((n = imsg_get(ibuf, &imsg)) == -1)
365 3baa2617 2022-02-16 op fatalx("imsg_get error");
369 fad0fb69 2022-05-28 op if (imsg.hdr.type == IMSG_CTL_ERR) {
370 fad0fb69 2022-05-28 op log_warnx("%s: %s", res->ctl->name,
371 fad0fb69 2022-05-28 op imsg_strerror(&imsg));
377 fad0fb69 2022-05-28 op datalen = IMSG_DATA_SIZE(imsg);
379 3baa2617 2022-02-16 op switch (res->action) {
381 d903ec9a 2022-05-29 op if (res->files[i] == NULL)
382 fad0fb69 2022-05-28 op fatalx("received more replies than "
383 fad0fb69 2022-05-28 op "files enqueued.");
385 fad0fb69 2022-05-28 op if (imsg.hdr.type == IMSG_CTL_ADD)
386 d903ec9a 2022-05-29 op log_debug("enqueued %s", res->files[i]);
388 fad0fb69 2022-05-28 op fatalx("invalid message %d",
389 fad0fb69 2022-05-28 op imsg.hdr.type);
391 d903ec9a 2022-05-29 op done = res->files[i] == NULL;
394 fad0fb69 2022-05-28 op if (datalen == 0) {
398 fad0fb69 2022-05-28 op if (datalen != sizeof(ps))
399 fad0fb69 2022-05-28 op fatalx("data size mismatch");
400 fad0fb69 2022-05-28 op memcpy(&ps, imsg.data, sizeof(ps));
401 fad0fb69 2022-05-28 op if (ps.path[sizeof(ps.path) - 1] != '\0')
402 fad0fb69 2022-05-28 op fatalx("received corrupted data");
403 fad0fb69 2022-05-28 op if (res->pretty) {
404 fad0fb69 2022-05-28 op char c = ' ';
405 fad0fb69 2022-05-28 op if (ps.status == STATE_PLAYING)
407 fad0fb69 2022-05-28 op printf("%c ", c);
409 fad0fb69 2022-05-28 op puts(ps.path);
413 146307bd 2022-02-17 op case RESTART:
418 fad0fb69 2022-05-28 op if (imsg.hdr.type != IMSG_CTL_STATUS)
419 fad0fb69 2022-05-28 op fatalx("invalid message %d",
420 fad0fb69 2022-05-28 op imsg.hdr.type);
422 fad0fb69 2022-05-28 op if (datalen != sizeof(ps))
423 fad0fb69 2022-05-28 op fatalx("data size mismatch");
424 fad0fb69 2022-05-28 op memcpy(&ps, imsg.data, sizeof(ps));
425 fad0fb69 2022-05-28 op if (ps.path[sizeof(ps.path) - 1] != '\0')
426 fad0fb69 2022-05-28 op fatalx("received corrupted data");
428 fad0fb69 2022-05-28 op if (ps.status == STATE_STOPPED)
429 fad0fb69 2022-05-28 op printf("stopped ");
430 fad0fb69 2022-05-28 op else if (ps.status == STATE_PLAYING)
431 fad0fb69 2022-05-28 op printf("playing ");
432 fad0fb69 2022-05-28 op else if (ps.status == STATE_PAUSED)
433 fad0fb69 2022-05-28 op printf("paused ");
435 fad0fb69 2022-05-28 op printf("unknown ");
437 fad0fb69 2022-05-28 op puts(ps.path);
438 fad0fb69 2022-05-28 op printf("repat one %s\nrepeat all %s\n",
439 fad0fb69 2022-05-28 op ps.rp.repeat_one ? "on" : "off",
440 fad0fb69 2022-05-28 op ps.rp.repeat_all ? "on" : "off");
445 fad0fb69 2022-05-28 op if (imsg.hdr.type == IMSG_CTL_ADD)
447 fad0fb69 2022-05-28 op if (imsg.hdr.type == IMSG_CTL_COMMIT) {
452 fad0fb69 2022-05-28 op if (imsg.hdr.type != IMSG_CTL_BEGIN)
453 fad0fb69 2022-05-28 op fatalx("invalid message %d",
454 fad0fb69 2022-05-28 op imsg.hdr.type);
456 fad0fb69 2022-05-28 op load_files(res, &ret);
458 6b47a39f 2022-02-21 op case MONITOR:
459 fad0fb69 2022-05-28 op if (imsg.hdr.type != IMSG_CTL_MONITOR)
460 fad0fb69 2022-05-28 op fatalx("invalid message %d",
461 fad0fb69 2022-05-28 op imsg.hdr.type);
463 fad0fb69 2022-05-28 op if (datalen != sizeof(type))
464 fad0fb69 2022-05-28 op fatalx("data size mismatch");
466 fad0fb69 2022-05-28 op memcpy(&type, imsg.data, sizeof(type));
467 fad0fb69 2022-05-28 op if (type < 0 || type > IMSG__LAST)
468 fad0fb69 2022-05-28 op fatalx("received corrupted data");
470 fad0fb69 2022-05-28 op if (!res->monitor[type])
473 fad0fb69 2022-05-28 op puts(imsg_name(type));
474 fad0fb69 2022-05-28 op fflush(stdout);
481 3baa2617 2022-02-16 op imsg_free(&imsg);
490 3baa2617 2022-02-16 op ctl_noarg(struct parse_result *res, int argc, char **argv)
494 7e29fc4a 2022-03-03 op while ((ch = getopt(argc, argv, "")) != -1)
495 7e29fc4a 2022-03-03 op ctl_usage(res->ctl);
496 7e29fc4a 2022-03-03 op argc -= optind;
497 7e29fc4a 2022-03-03 op argv += optind;
499 acaf7eb2 2022-03-05 op if (argc > 0)
500 3baa2617 2022-02-16 op ctl_usage(res->ctl);
502 3baa2617 2022-02-16 op return ctlaction(res);
506 3baa2617 2022-02-16 op ctl_add(struct parse_result *res, int argc, char **argv)
510 7e29fc4a 2022-03-03 op while ((ch = getopt(argc, argv, "")) != -1)
511 3baa2617 2022-02-16 op ctl_usage(res->ctl);
512 7e29fc4a 2022-03-03 op argc -= optind;
513 7e29fc4a 2022-03-03 op argv += optind;
515 7e29fc4a 2022-03-03 op if (argc == 0)
516 7e29fc4a 2022-03-03 op ctl_usage(res->ctl);
517 7e29fc4a 2022-03-03 op res->files = argv;
519 14be015f 2022-02-17 op if (pledge("stdio rpath", NULL) == -1)
520 14be015f 2022-02-17 op fatal("pledge");
522 3baa2617 2022-02-16 op return ctlaction(res);
526 8e916714 2022-02-17 op ctl_show(struct parse_result *res, int argc, char **argv)
530 8e916714 2022-02-17 op while ((ch = getopt(argc, argv, "p")) != -1) {
531 8e916714 2022-02-17 op switch (ch) {
533 8e916714 2022-02-17 op res->pretty = 1;
536 8e916714 2022-02-17 op ctl_usage(res->ctl);
540 8e916714 2022-02-17 op return ctlaction(res);
544 14be015f 2022-02-17 op ctl_load(struct parse_result *res, int argc, char **argv)
548 7e29fc4a 2022-03-03 op while ((ch = getopt(argc, argv, "")) != -1)
549 7e29fc4a 2022-03-03 op ctl_usage(res->ctl);
550 7e29fc4a 2022-03-03 op argc -= optind;
551 7e29fc4a 2022-03-03 op argv += optind;
553 7e29fc4a 2022-03-03 op if (argc == 0)
554 ec1fb0c7 2022-02-17 op res->file = NULL;
555 7e29fc4a 2022-03-03 op else if (argc == 1)
556 7e29fc4a 2022-03-03 op res->file = argv[0];
558 14be015f 2022-02-17 op ctl_usage(res->ctl);
560 14be015f 2022-02-17 op if (pledge("stdio rpath", NULL) == -1)
561 14be015f 2022-02-17 op fatal("pledge");
563 7a427ecd 2022-02-17 op return ctlaction(res);
567 a913de21 2022-02-17 op ctl_jump(struct parse_result *res, int argc, char **argv)
571 a913de21 2022-02-17 op while ((ch = getopt(argc, argv, "")) != -1)
572 a913de21 2022-02-17 op ctl_usage(res->ctl);
573 a913de21 2022-02-17 op argc -= optind;
574 a913de21 2022-02-17 op argv += optind;
576 a913de21 2022-02-17 op if (argc != 1)
577 a913de21 2022-02-17 op ctl_usage(res->ctl);
579 a913de21 2022-02-17 op res->file = argv[0];
580 310ef57c 2022-02-19 op return ctlaction(res);
584 310ef57c 2022-02-19 op ctl_repeat(struct parse_result *res, int argc, char **argv)
588 310ef57c 2022-02-19 op while ((ch = getopt(argc, argv, "")) != -1)
589 310ef57c 2022-02-19 op ctl_usage(res->ctl);
590 310ef57c 2022-02-19 op argc -= optind;
591 310ef57c 2022-02-19 op argv += optind;
593 310ef57c 2022-02-19 op if (argc != 2)
594 310ef57c 2022-02-19 op ctl_usage(res->ctl);
596 310ef57c 2022-02-19 op if (!strcmp(argv[1], "on"))
598 310ef57c 2022-02-19 op else if (!strcmp(argv[1], "off"))
601 310ef57c 2022-02-19 op ctl_usage(res->ctl);
603 310ef57c 2022-02-19 op res->rep.repeat_one = -1;
604 310ef57c 2022-02-19 op res->rep.repeat_all = -1;
605 310ef57c 2022-02-19 op if (!strcmp(argv[0], "one"))
606 310ef57c 2022-02-19 op res->rep.repeat_one = b;
607 310ef57c 2022-02-19 op else if (!strcmp(argv[0], "all"))
608 310ef57c 2022-02-19 op res->rep.repeat_all = b;
610 310ef57c 2022-02-19 op ctl_usage(res->ctl);
612 90122a37 2022-05-14 op return ctlaction(res);
616 90122a37 2022-05-14 op ctl_monitor(struct parse_result *res, int argc, char **argv)
619 90122a37 2022-05-14 op const char *events;
620 90122a37 2022-05-14 op char *dup, *tmp, *tok;
622 90122a37 2022-05-14 op while ((ch = getopt(argc, argv, "")) != -1)
623 90122a37 2022-05-14 op ctl_usage(res->ctl);
624 90122a37 2022-05-14 op argc -= optind;
625 90122a37 2022-05-14 op argv += optind;
627 90122a37 2022-05-14 op if (argc > 1)
628 90122a37 2022-05-14 op ctl_usage(res->ctl);
630 90122a37 2022-05-14 op if (argc == 1)
631 90122a37 2022-05-14 op events = *argv;
633 90122a37 2022-05-14 op events = "play,toggle,pause,stop,restart,flush,next,prev,"
634 90122a37 2022-05-14 op "jump,repeat,add,load";
636 90122a37 2022-05-14 op tmp = dup = xstrdup(events);
637 90122a37 2022-05-14 op while ((tok = strsep(&tmp, ",")) != NULL) {
638 90122a37 2022-05-14 op if (*tok == '\0')
641 90122a37 2022-05-14 op if (!strcmp(tok, "play"))
642 90122a37 2022-05-14 op res->monitor[IMSG_CTL_PLAY] = 1;
643 90122a37 2022-05-14 op else if (!strcmp(tok, "toggle"))
644 90122a37 2022-05-14 op res->monitor[IMSG_CTL_TOGGLE_PLAY] = 1;
645 90122a37 2022-05-14 op else if (!strcmp(tok, "pause"))
646 90122a37 2022-05-14 op res->monitor[IMSG_CTL_PAUSE] = 1;
647 90122a37 2022-05-14 op else if (!strcmp(tok, "stop"))
648 90122a37 2022-05-14 op res->monitor[IMSG_CTL_STOP] = 1;
649 90122a37 2022-05-14 op else if (!strcmp(tok, "restart"))
650 90122a37 2022-05-14 op res->monitor[IMSG_CTL_RESTART] = 1;
651 90122a37 2022-05-14 op else if (!strcmp(tok, "flush"))
652 90122a37 2022-05-14 op res->monitor[IMSG_CTL_FLUSH] = 1;
653 90122a37 2022-05-14 op else if (!strcmp(tok, "next"))
654 90122a37 2022-05-14 op res->monitor[IMSG_CTL_NEXT] = 1;
655 90122a37 2022-05-14 op else if (!strcmp(tok, "prev"))
656 90122a37 2022-05-14 op res->monitor[IMSG_CTL_PREV] = 1;
657 90122a37 2022-05-14 op else if (!strcmp(tok, "jump"))
658 90122a37 2022-05-14 op res->monitor[IMSG_CTL_JUMP] = 1;
659 90122a37 2022-05-14 op else if (!strcmp(tok, "repeat"))
660 90122a37 2022-05-14 op res->monitor[IMSG_CTL_REPEAT] = 1;
661 90122a37 2022-05-14 op else if (!strcmp(tok, "add"))
662 90122a37 2022-05-14 op res->monitor[IMSG_CTL_ADD] = 1;
663 90122a37 2022-05-14 op else if (!strcmp(tok, "load"))
664 90122a37 2022-05-14 op res->monitor[IMSG_CTL_COMMIT] = 1;
666 90122a37 2022-05-14 op fatalx("unknown event \"%s\"", tok);
670 a913de21 2022-02-17 op return ctlaction(res);
674 1d673950 2022-03-03 op ctl_get_lock(const char *lockfile)
678 1d673950 2022-03-03 op if ((lockfd = open(lockfile, O_WRONLY|O_CREAT, 0600)) == -1) {
679 1d673950 2022-03-03 op log_debug("open failed: %s", strerror(errno));
683 1d673950 2022-03-03 op if (flock(lockfd, LOCK_EX|LOCK_NB) == -1) {
684 1d673950 2022-03-03 op log_debug("flock failed: %s", strerror(errno));
685 1d673950 2022-03-03 op if (errno != EAGAIN)
687 1d673950 2022-03-03 op while (flock(lockfd, LOCK_EX) == -1 && errno == EINTR)
689 1d673950 2022-03-03 op close(lockfd);
692 1d673950 2022-03-03 op log_debug("flock succeeded");
694 1d673950 2022-03-03 op return lockfd;
698 1d673950 2022-03-03 op ctl_connect(void)
700 1d673950 2022-03-03 op struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
701 1d673950 2022-03-03 op struct sockaddr_un sa;
703 1d673950 2022-03-03 op int fd, lockfd = -1, locked = 0, spawned = 0;
704 1d673950 2022-03-03 op char *lockfile = NULL;
706 1d673950 2022-03-03 op memset(&sa, 0, sizeof(sa));
707 1d673950 2022-03-03 op sa.sun_family = AF_UNIX;
708 1d673950 2022-03-03 op size = strlcpy(sa.sun_path, csock, sizeof(sa.sun_path));
709 1d673950 2022-03-03 op if (size >= sizeof(sa.sun_path)) {
710 1d673950 2022-03-03 op errno = ENAMETOOLONG;
715 1d673950 2022-03-03 op if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
718 1d673950 2022-03-03 op if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
719 1d673950 2022-03-03 op log_debug("connection failed: %s", strerror(errno));
720 1d673950 2022-03-03 op if (errno != ECONNREFUSED && errno != ENOENT)
724 1d673950 2022-03-03 op if (!locked) {
725 1d673950 2022-03-03 op xasprintf(&lockfile, "%s.lock", csock);
726 1d673950 2022-03-03 op if ((lockfd = ctl_get_lock(lockfile)) < 0) {
727 1d673950 2022-03-03 op log_debug("didn't get the lock (%d)", lockfd);
729 1d673950 2022-03-03 op free(lockfile);
730 1d673950 2022-03-03 op lockfile = NULL;
732 1d673950 2022-03-03 op if (lockfd == -1)
737 1d673950 2022-03-03 op * Always retry at least once, even if we got
738 1d673950 2022-03-03 op * the lock, because another client could have
739 1d673950 2022-03-03 op * taken the lock, started the server and released
740 1d673950 2022-03-03 op * the lock between our connect() and flock()
746 1d673950 2022-03-03 op if (!spawned) {
747 1d673950 2022-03-03 op log_debug("spawning the daemon");
748 1d673950 2022-03-03 op spawn_daemon();
752 1d673950 2022-03-03 op nanosleep(&ts, NULL);
756 1d673950 2022-03-03 op if (locked && lockfd >= 0) {
757 1d673950 2022-03-03 op unlink(lockfile);
758 1d673950 2022-03-03 op free(lockfile);
759 1d673950 2022-03-03 op close(lockfd);
764 1d673950 2022-03-03 op if (locked) {
765 1d673950 2022-03-03 op free(lockfile);
766 1d673950 2022-03-03 op close(lockfd);
773 fea541a8 2022-02-16 op ctl(int argc, char **argv)
775 1d673950 2022-03-03 op int ctl_sock;
777 fea541a8 2022-02-16 op log_init(1, LOG_DAEMON);
778 fea541a8 2022-02-16 op log_setverbose(verbose);
780 1d673950 2022-03-03 op if ((ctl_sock = ctl_connect()) == -1)
781 1d673950 2022-03-03 op fatal("can't connect");
783 fea541a8 2022-02-16 op if (ctl_sock == -1)
784 fea541a8 2022-02-16 op fatalx("failed to connect to the daemon");
786 3baa2617 2022-02-16 op ibuf = xmalloc(sizeof(*ibuf));
787 3baa2617 2022-02-16 op imsg_init(ibuf, ctl_sock);
789 3baa2617 2022-02-16 op optreset = 1;
792 3baa2617 2022-02-16 op exit(parse(argc, argv));