2 3baa2617 2022-02-16 op * Copyright (c) 2022 Omar Polo <op@openbsd.org>
3 96233476 2022-06-09 op * Copyright (c) 2015 Theo de Raadt <deraadt@openbsd.org>
5 3baa2617 2022-02-16 op * Permission to use, copy, modify, and distribute this software for any
6 3baa2617 2022-02-16 op * purpose with or without fee is hereby granted, provided that the above
7 3baa2617 2022-02-16 op * copyright notice and this permission notice appear in all copies.
9 3baa2617 2022-02-16 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 3baa2617 2022-02-16 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 3baa2617 2022-02-16 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 3baa2617 2022-02-16 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 3baa2617 2022-02-16 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 3baa2617 2022-02-16 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 3baa2617 2022-02-16 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 3baa2617 2022-02-16 op #include <sys/types.h>
19 3baa2617 2022-02-16 op #include <sys/socket.h>
20 3baa2617 2022-02-16 op #include <sys/queue.h>
21 3baa2617 2022-02-16 op #include <sys/uio.h>
22 3baa2617 2022-02-16 op #include <sys/un.h>
24 3baa2617 2022-02-16 op #include <errno.h>
25 3baa2617 2022-02-16 op #include <event.h>
26 1d673950 2022-03-03 op #include <fcntl.h>
27 3baa2617 2022-02-16 op #include <limits.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>
34 3baa2617 2022-02-16 op #include <imsg.h>
36 3baa2617 2022-02-16 op #include "amused.h"
37 3baa2617 2022-02-16 op #include "log.h"
38 bb3f279f 2022-02-16 op #include "playlist.h"
39 3baa2617 2022-02-16 op #include "xmalloc.h"
41 96233476 2022-06-09 op static struct imsgbuf *ibuf;
42 96233476 2022-06-09 op char cwd[PATH_MAX];
44 2ddcfa40 2022-07-08 op static int ctl_noarg(struct parse_result *, int, char **);
45 2ddcfa40 2022-07-08 op static int ctl_add(struct parse_result *, int, char **);
46 2ddcfa40 2022-07-08 op static int ctl_show(struct parse_result *, int, char **);
47 2ddcfa40 2022-07-08 op static int ctl_load(struct parse_result *, int, char **);
48 2ddcfa40 2022-07-08 op static int ctl_jump(struct parse_result *, int, char **);
49 2ddcfa40 2022-07-08 op static int ctl_repeat(struct parse_result *, int, char **);
50 2ddcfa40 2022-07-08 op static int ctl_monitor(struct parse_result *, int, char **);
51 e5d4e9f7 2022-07-09 op static int ctl_seek(struct parse_result *, int, char **);
53 3baa2617 2022-02-16 op struct ctl_command ctl_commands[] = {
54 d488e5a5 2022-07-08 op { "add", ADD, ctl_add, "files...", 0 },
55 d488e5a5 2022-07-08 op { "flush", FLUSH, ctl_noarg, "", 0 },
56 d488e5a5 2022-07-08 op { "jump", JUMP, ctl_jump, "pattern", 0 },
57 6a128dce 2022-07-08 op { "load", LOAD, ctl_load, "[file]", 1 },
58 d488e5a5 2022-07-08 op { "monitor", MONITOR, ctl_monitor, "[events]", 0 },
59 6a128dce 2022-07-08 op { "next", NEXT, ctl_noarg, "", 0 },
60 6a128dce 2022-07-08 op { "pause", PAUSE, ctl_noarg, "", 0 },
61 6a128dce 2022-07-08 op { "play", PLAY, ctl_noarg, "", 0 },
62 6a128dce 2022-07-08 op { "prev", PREV, ctl_noarg, "", 0 },
63 6a128dce 2022-07-08 op { "repeat", REPEAT, ctl_repeat, "one|all on|off", 0 },
64 6a128dce 2022-07-08 op { "restart", RESTART, ctl_noarg, "", 0 },
65 e5d4e9f7 2022-07-09 op { "seek", SEEK, ctl_seek, "[+-]time", 0 },
66 6a128dce 2022-07-08 op { "show", SHOW, ctl_show, "[-p]", 0 },
67 6a128dce 2022-07-08 op { "status", STATUS, ctl_noarg, "", 0 },
68 6a128dce 2022-07-08 op { "stop", STOP, ctl_noarg, "", 0 },
69 6a128dce 2022-07-08 op { "toggle", TOGGLE, ctl_noarg, "", 0 },
70 d488e5a5 2022-07-08 op { NULL, 0, NULL, NULL, 0 },
76 3baa2617 2022-02-16 op fprintf(stderr, "usage: %s [-dv] [-s socket]\n", getprogname());
80 3baa2617 2022-02-16 op static __dead void
81 3baa2617 2022-02-16 op ctl_usage(struct ctl_command *ctl)
83 3baa2617 2022-02-16 op fprintf(stderr, "usage: %s [-v] [-s socket] %s %s\n", getprogname(),
84 3baa2617 2022-02-16 op ctl->name, ctl->usage);
88 96233476 2022-06-09 op /* based on canonpath from kern_pledge.c */
90 96233476 2022-06-09 op canonpath(const char *input, char *buf, size_t bufsize)
92 96233476 2022-06-09 op const char *p;
93 96233476 2022-06-09 op char *q, path[PATH_MAX];
95 96233476 2022-06-09 op if (input[0] != '/') {
96 96233476 2022-06-09 op if (snprintf(path, sizeof(path), "%s/%s", cwd, input)
97 96233476 2022-06-09 op >= sizeof(path)) {
98 96233476 2022-06-09 op errno = ENAMETOOLONG;
101 96233476 2022-06-09 op input = path;
106 96233476 2022-06-09 op while (*p && (q - buf < bufsize)) {
107 96233476 2022-06-09 op if (p[0] == '/' && (p[1] == '/' || p[1] == '\0')) {
110 96233476 2022-06-09 op } else if (p[0] == '/' && p[1] == '.' &&
111 96233476 2022-06-09 op (p[2] == '/' || p[2] == '\0')) {
114 96233476 2022-06-09 op } else if (p[0] == '/' && p[1] == '.' && p[2] == '.' &&
115 96233476 2022-06-09 op (p[3] == '/' || p[3] == '\0')) {
117 96233476 2022-06-09 op if (q != buf) /* "/../" at start of buf */
118 96233476 2022-06-09 op while (*--q != '/')
125 96233476 2022-06-09 op if ((*p == '\0') && (q - buf < bufsize)) {
129 96233476 2022-06-09 op errno = ENAMETOOLONG;
135 3baa2617 2022-02-16 op parse(int argc, char **argv)
137 3baa2617 2022-02-16 op struct ctl_command *ctl = NULL;
138 3baa2617 2022-02-16 op struct parse_result res;
139 1d673950 2022-03-03 op const char *argv0;
140 3baa2617 2022-02-16 op int i, status;
142 3baa2617 2022-02-16 op memset(&res, 0, sizeof(res));
144 1d673950 2022-03-03 op if ((argv0 = argv[0]) == NULL)
145 1d673950 2022-03-03 op argv0 = "status";
147 3baa2617 2022-02-16 op for (i = 0; ctl_commands[i].name != NULL; ++i) {
148 1d673950 2022-03-03 op if (strncmp(ctl_commands[i].name, argv0, strlen(argv0))
150 3baa2617 2022-02-16 op if (ctl != NULL) {
151 3baa2617 2022-02-16 op fprintf(stderr,
152 1d673950 2022-03-03 op "ambiguous argument: %s\n", argv0);
155 3baa2617 2022-02-16 op ctl = &ctl_commands[i];
159 3baa2617 2022-02-16 op if (ctl == NULL) {
160 3baa2617 2022-02-16 op fprintf(stderr, "unknown argument: %s\n", argv[0]);
164 3baa2617 2022-02-16 op res.action = ctl->action;
165 3baa2617 2022-02-16 op res.ctl = ctl;
167 3baa2617 2022-02-16 op if (!ctl->has_pledge) {
168 3baa2617 2022-02-16 op /* pledge(2) default if command doesn't have its own */
169 3baa2617 2022-02-16 op if (pledge("stdio", NULL) == -1)
170 3baa2617 2022-02-16 op fatal("pledge");
173 3baa2617 2022-02-16 op status = ctl->main(&res, argc, argv);
174 3baa2617 2022-02-16 op close(ibuf->fd);
176 3baa2617 2022-02-16 op return status;
180 fad0fb69 2022-05-28 op load_files(struct parse_result *res, int *ret)
183 7a427ecd 2022-02-17 op const char *file;
184 7a427ecd 2022-02-17 op char *line = NULL;
185 96233476 2022-06-09 op char path[PATH_MAX];
186 96233476 2022-06-09 op size_t linesize = 0, i = 0;
187 3af93963 2022-03-02 op ssize_t linelen, curr = -1;
189 ec1fb0c7 2022-02-17 op if (res->file == NULL)
191 ec1fb0c7 2022-02-17 op else if ((f = fopen(res->file, "r")) == NULL) {
192 ec1fb0c7 2022-02-17 op log_warn("can't open %s", res->file);
197 ec1fb0c7 2022-02-17 op while ((linelen = getline(&line, &linesize, f)) != -1) {
198 7a427ecd 2022-02-17 op if (linelen == 0)
200 7a427ecd 2022-02-17 op line[linelen-1] = '\0';
202 1dd3b054 2022-03-25 op if (!strncmp(file, "> ", 2)) {
205 1dd3b054 2022-03-25 op } else if (!strncmp(file, " ", 2))
208 18223c28 2022-06-13 op memset(path, 0, sizeof(path));
209 96233476 2022-06-09 op if (canonpath(file, path, sizeof(path)) == -1) {
210 f1beb69a 2022-06-13 op log_warn("canonpath %s", file);
215 7a427ecd 2022-02-17 op imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
216 7a427ecd 2022-02-17 op path, sizeof(path));
220 ec1fb0c7 2022-02-17 op if (ferror(f))
221 7a427ecd 2022-02-17 op fatal("getline");
224 3af93963 2022-03-02 op if (i == 0) {
229 7a427ecd 2022-02-17 op imsg_compose(ibuf, IMSG_CTL_COMMIT, 0, 0, -1,
230 3af93963 2022-03-02 op &curr, sizeof(curr));
231 7a427ecd 2022-02-17 op imsg_flush(ibuf);
235 fad0fb69 2022-05-28 op static const char *
236 fad0fb69 2022-05-28 op imsg_strerror(struct imsg *imsg)
238 fad0fb69 2022-05-28 op size_t datalen;
239 fad0fb69 2022-05-28 op const char *msg;
241 fad0fb69 2022-05-28 op datalen = IMSG_DATA_SIZE(*imsg);
242 fad0fb69 2022-05-28 op msg = imsg->data;
243 fad0fb69 2022-05-28 op if (datalen == 0 || msg[datalen-1] != '\0')
244 fad0fb69 2022-05-28 op fatalx("malformed error message");
249 fad0fb69 2022-05-28 op static const char *
250 fad0fb69 2022-05-28 op imsg_name(int type)
252 6b47a39f 2022-02-21 op switch (type) {
253 6b47a39f 2022-02-21 op case IMSG_CTL_PLAY:
254 fad0fb69 2022-05-28 op return "play";
255 6b47a39f 2022-02-21 op case IMSG_CTL_TOGGLE_PLAY:
256 fad0fb69 2022-05-28 op return "toggle";
257 6b47a39f 2022-02-21 op case IMSG_CTL_PAUSE:
258 fad0fb69 2022-05-28 op return "pause";
259 6b47a39f 2022-02-21 op case IMSG_CTL_STOP:
260 fad0fb69 2022-05-28 op return "stop";
261 6b47a39f 2022-02-21 op case IMSG_CTL_RESTART:
262 fad0fb69 2022-05-28 op return "restart";
263 6b47a39f 2022-02-21 op case IMSG_CTL_FLUSH:
264 fad0fb69 2022-05-28 op return "flush";
265 6b47a39f 2022-02-21 op case IMSG_CTL_NEXT:
266 fad0fb69 2022-05-28 op return "next";
267 6b47a39f 2022-02-21 op case IMSG_CTL_PREV:
268 fad0fb69 2022-05-28 op return "prev";
269 6b47a39f 2022-02-21 op case IMSG_CTL_JUMP:
270 fad0fb69 2022-05-28 op return "jump";
271 6b47a39f 2022-02-21 op case IMSG_CTL_REPEAT:
272 fad0fb69 2022-05-28 op return "repeat";
273 6b47a39f 2022-02-21 op case IMSG_CTL_ADD:
274 fad0fb69 2022-05-28 op return "add";
275 6b47a39f 2022-02-21 op case IMSG_CTL_COMMIT:
276 fad0fb69 2022-05-28 op return "load";
278 fad0fb69 2022-05-28 op return "unknown";
283 bdc8c4e0 2022-07-08 op print_time(const char *label, int64_t seconds)
285 bdc8c4e0 2022-07-08 op int hours, minutes;
287 bdc8c4e0 2022-07-08 op if (seconds < 0)
290 bdc8c4e0 2022-07-08 op hours = seconds / 3600;
291 bdc8c4e0 2022-07-08 op seconds -= hours * 3600;
293 bdc8c4e0 2022-07-08 op minutes = seconds / 60;
294 bdc8c4e0 2022-07-08 op seconds -= minutes * 60;
296 bdc8c4e0 2022-07-08 op printf("%s ", label);
297 bdc8c4e0 2022-07-08 op if (hours != 0)
298 bdc8c4e0 2022-07-08 op printf("%02d:", hours);
299 bdc8c4e0 2022-07-08 op printf("%02d:%02lld\n", minutes, (long long)seconds);
303 3baa2617 2022-02-16 op ctlaction(struct parse_result *res)
305 fad0fb69 2022-05-28 op char path[PATH_MAX];
306 3baa2617 2022-02-16 op struct imsg imsg;
307 fad0fb69 2022-05-28 op struct player_status ps;
308 fad0fb69 2022-05-28 op size_t datalen;
310 fad0fb69 2022-05-28 op int i, type, ret = 0, done = 1;
312 3baa2617 2022-02-16 op switch (res->action) {
314 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
315 00f500ff 2022-02-19 op if (verbose) {
316 00f500ff 2022-02-19 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
322 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
325 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
326 00f500ff 2022-02-19 op if (verbose) {
327 00f500ff 2022-02-19 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
333 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
335 3baa2617 2022-02-16 op case RESTART:
336 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_RESTART, 0, 0, -1, NULL, 0);
337 00f500ff 2022-02-19 op if (verbose) {
338 00f500ff 2022-02-19 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
345 d903ec9a 2022-05-29 op for (i = 0; res->files[i] != NULL; ++i) {
346 18223c28 2022-06-13 op memset(path, 0, sizeof(path));
347 ef593b43 2022-06-09 op if (canonpath(res->files[i], path, sizeof(path))
349 ef593b43 2022-06-09 op log_warn("canonpath %s", res->files[i]);
353 fad0fb69 2022-05-28 op imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
354 fad0fb69 2022-05-28 op path, sizeof(path));
356 fad0fb69 2022-05-28 op ret = i == 0;
359 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
363 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
367 bb3f279f 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
370 af27e631 2022-02-17 op imsg_compose(ibuf, IMSG_CTL_NEXT, 0, 0, -1, NULL, 0);
371 00f500ff 2022-02-19 op if (verbose) {
372 00f500ff 2022-02-19 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
378 af27e631 2022-02-17 op imsg_compose(ibuf, IMSG_CTL_PREV, 0, 0, -1, NULL, 0);
379 00f500ff 2022-02-19 op if (verbose) {
380 00f500ff 2022-02-19 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
387 7a427ecd 2022-02-17 op imsg_compose(ibuf, IMSG_CTL_BEGIN, 0, 0, -1, NULL, 0);
391 fad0fb69 2022-05-28 op memset(path, 0, sizeof(path));
392 fad0fb69 2022-05-28 op strlcpy(path, res->file, sizeof(path));
393 fad0fb69 2022-05-28 op imsg_compose(ibuf, IMSG_CTL_JUMP, 0, 0, -1,
394 fad0fb69 2022-05-28 op path, sizeof(path));
397 310ef57c 2022-02-19 op imsg_compose(ibuf, IMSG_CTL_REPEAT, 0, 0, -1,
398 310ef57c 2022-02-19 op &res->rep, sizeof(res->rep));
400 6b47a39f 2022-02-21 op case MONITOR:
402 6b47a39f 2022-02-21 op imsg_compose(ibuf, IMSG_CTL_MONITOR, 0, 0, -1,
406 e5d4e9f7 2022-07-09 op imsg_compose(ibuf, IMSG_CTL_SEEK, 0, 0, -1, &res->seek,
407 e5d4e9f7 2022-07-09 op sizeof(res->seek));
410 3baa2617 2022-02-16 op /* action not expected */
411 3baa2617 2022-02-16 op fatalx("invalid action %u", res->action);
415 3baa2617 2022-02-16 op if (ret != 0)
418 3baa2617 2022-02-16 op imsg_flush(ibuf);
421 3baa2617 2022-02-16 op while (!done) {
422 3baa2617 2022-02-16 op if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
423 3baa2617 2022-02-16 op fatalx("imsg_read error");
425 3baa2617 2022-02-16 op fatalx("pipe closed");
427 3baa2617 2022-02-16 op while (!done) {
428 3baa2617 2022-02-16 op if ((n = imsg_get(ibuf, &imsg)) == -1)
429 3baa2617 2022-02-16 op fatalx("imsg_get error");
433 fad0fb69 2022-05-28 op if (imsg.hdr.type == IMSG_CTL_ERR) {
434 fad0fb69 2022-05-28 op log_warnx("%s: %s", res->ctl->name,
435 fad0fb69 2022-05-28 op imsg_strerror(&imsg));
441 fad0fb69 2022-05-28 op datalen = IMSG_DATA_SIZE(imsg);
443 3baa2617 2022-02-16 op switch (res->action) {
445 d903ec9a 2022-05-29 op if (res->files[i] == NULL)
446 fad0fb69 2022-05-28 op fatalx("received more replies than "
447 fad0fb69 2022-05-28 op "files enqueued.");
449 fad0fb69 2022-05-28 op if (imsg.hdr.type == IMSG_CTL_ADD)
450 d903ec9a 2022-05-29 op log_debug("enqueued %s", res->files[i]);
452 fad0fb69 2022-05-28 op fatalx("invalid message %d",
453 fad0fb69 2022-05-28 op imsg.hdr.type);
455 d903ec9a 2022-05-29 op done = res->files[i] == NULL;
458 fad0fb69 2022-05-28 op if (datalen == 0) {
462 fad0fb69 2022-05-28 op if (datalen != sizeof(ps))
463 fad0fb69 2022-05-28 op fatalx("data size mismatch");
464 fad0fb69 2022-05-28 op memcpy(&ps, imsg.data, sizeof(ps));
465 fad0fb69 2022-05-28 op if (ps.path[sizeof(ps.path) - 1] != '\0')
466 fad0fb69 2022-05-28 op fatalx("received corrupted data");
467 fad0fb69 2022-05-28 op if (res->pretty) {
468 fad0fb69 2022-05-28 op char c = ' ';
469 fad0fb69 2022-05-28 op if (ps.status == STATE_PLAYING)
471 fad0fb69 2022-05-28 op printf("%c ", c);
473 fad0fb69 2022-05-28 op puts(ps.path);
477 146307bd 2022-02-17 op case RESTART:
482 fad0fb69 2022-05-28 op if (imsg.hdr.type != IMSG_CTL_STATUS)
483 fad0fb69 2022-05-28 op fatalx("invalid message %d",
484 fad0fb69 2022-05-28 op imsg.hdr.type);
486 fad0fb69 2022-05-28 op if (datalen != sizeof(ps))
487 fad0fb69 2022-05-28 op fatalx("data size mismatch");
488 fad0fb69 2022-05-28 op memcpy(&ps, imsg.data, sizeof(ps));
489 fad0fb69 2022-05-28 op if (ps.path[sizeof(ps.path) - 1] != '\0')
490 fad0fb69 2022-05-28 op fatalx("received corrupted data");
492 fad0fb69 2022-05-28 op if (ps.status == STATE_STOPPED)
493 fad0fb69 2022-05-28 op printf("stopped ");
494 fad0fb69 2022-05-28 op else if (ps.status == STATE_PLAYING)
495 fad0fb69 2022-05-28 op printf("playing ");
496 fad0fb69 2022-05-28 op else if (ps.status == STATE_PAUSED)
497 fad0fb69 2022-05-28 op printf("paused ");
499 fad0fb69 2022-05-28 op printf("unknown ");
501 fad0fb69 2022-05-28 op puts(ps.path);
503 bdc8c4e0 2022-07-08 op print_time("position", ps.position);
504 bdc8c4e0 2022-07-08 op print_time("duration", ps.duration);
506 dc12484b 2022-07-08 op printf("repeat one %s\nrepeat all %s\n",
507 fad0fb69 2022-05-28 op ps.rp.repeat_one ? "on" : "off",
508 fad0fb69 2022-05-28 op ps.rp.repeat_all ? "on" : "off");
513 fad0fb69 2022-05-28 op if (imsg.hdr.type == IMSG_CTL_ADD)
515 fad0fb69 2022-05-28 op if (imsg.hdr.type == IMSG_CTL_COMMIT) {
520 fad0fb69 2022-05-28 op if (imsg.hdr.type != IMSG_CTL_BEGIN)
521 fad0fb69 2022-05-28 op fatalx("invalid message %d",
522 fad0fb69 2022-05-28 op imsg.hdr.type);
524 fad0fb69 2022-05-28 op load_files(res, &ret);
526 6b47a39f 2022-02-21 op case MONITOR:
527 fad0fb69 2022-05-28 op if (imsg.hdr.type != IMSG_CTL_MONITOR)
528 fad0fb69 2022-05-28 op fatalx("invalid message %d",
529 fad0fb69 2022-05-28 op imsg.hdr.type);
531 fad0fb69 2022-05-28 op if (datalen != sizeof(type))
532 fad0fb69 2022-05-28 op fatalx("data size mismatch");
534 fad0fb69 2022-05-28 op memcpy(&type, imsg.data, sizeof(type));
535 fad0fb69 2022-05-28 op if (type < 0 || type > IMSG__LAST)
536 fad0fb69 2022-05-28 op fatalx("received corrupted data");
538 fad0fb69 2022-05-28 op if (!res->monitor[type])
541 fad0fb69 2022-05-28 op puts(imsg_name(type));
542 fad0fb69 2022-05-28 op fflush(stdout);
549 3baa2617 2022-02-16 op imsg_free(&imsg);
558 3baa2617 2022-02-16 op ctl_noarg(struct parse_result *res, int argc, char **argv)
562 7e29fc4a 2022-03-03 op while ((ch = getopt(argc, argv, "")) != -1)
563 7e29fc4a 2022-03-03 op ctl_usage(res->ctl);
564 7e29fc4a 2022-03-03 op argc -= optind;
565 7e29fc4a 2022-03-03 op argv += optind;
567 acaf7eb2 2022-03-05 op if (argc > 0)
568 3baa2617 2022-02-16 op ctl_usage(res->ctl);
570 3baa2617 2022-02-16 op return ctlaction(res);
574 3baa2617 2022-02-16 op ctl_add(struct parse_result *res, int argc, char **argv)
578 7e29fc4a 2022-03-03 op while ((ch = getopt(argc, argv, "")) != -1)
579 3baa2617 2022-02-16 op ctl_usage(res->ctl);
580 7e29fc4a 2022-03-03 op argc -= optind;
581 7e29fc4a 2022-03-03 op argv += optind;
583 7e29fc4a 2022-03-03 op if (argc == 0)
584 7e29fc4a 2022-03-03 op ctl_usage(res->ctl);
585 7e29fc4a 2022-03-03 op res->files = argv;
587 3baa2617 2022-02-16 op return ctlaction(res);
591 8e916714 2022-02-17 op ctl_show(struct parse_result *res, int argc, char **argv)
595 8e916714 2022-02-17 op while ((ch = getopt(argc, argv, "p")) != -1) {
596 8e916714 2022-02-17 op switch (ch) {
598 8e916714 2022-02-17 op res->pretty = 1;
601 8e916714 2022-02-17 op ctl_usage(res->ctl);
605 8e916714 2022-02-17 op return ctlaction(res);
609 14be015f 2022-02-17 op ctl_load(struct parse_result *res, int argc, char **argv)
613 7e29fc4a 2022-03-03 op while ((ch = getopt(argc, argv, "")) != -1)
614 7e29fc4a 2022-03-03 op ctl_usage(res->ctl);
615 7e29fc4a 2022-03-03 op argc -= optind;
616 7e29fc4a 2022-03-03 op argv += optind;
618 7e29fc4a 2022-03-03 op if (argc == 0)
619 ec1fb0c7 2022-02-17 op res->file = NULL;
620 7e29fc4a 2022-03-03 op else if (argc == 1)
621 7e29fc4a 2022-03-03 op res->file = argv[0];
623 14be015f 2022-02-17 op ctl_usage(res->ctl);
625 14be015f 2022-02-17 op if (pledge("stdio rpath", NULL) == -1)
626 14be015f 2022-02-17 op fatal("pledge");
628 7a427ecd 2022-02-17 op return ctlaction(res);
632 a913de21 2022-02-17 op ctl_jump(struct parse_result *res, int argc, char **argv)
636 a913de21 2022-02-17 op while ((ch = getopt(argc, argv, "")) != -1)
637 a913de21 2022-02-17 op ctl_usage(res->ctl);
638 a913de21 2022-02-17 op argc -= optind;
639 a913de21 2022-02-17 op argv += optind;
641 a913de21 2022-02-17 op if (argc != 1)
642 a913de21 2022-02-17 op ctl_usage(res->ctl);
644 a913de21 2022-02-17 op res->file = argv[0];
645 310ef57c 2022-02-19 op return ctlaction(res);
649 310ef57c 2022-02-19 op ctl_repeat(struct parse_result *res, int argc, char **argv)
653 310ef57c 2022-02-19 op while ((ch = getopt(argc, argv, "")) != -1)
654 310ef57c 2022-02-19 op ctl_usage(res->ctl);
655 310ef57c 2022-02-19 op argc -= optind;
656 310ef57c 2022-02-19 op argv += optind;
658 310ef57c 2022-02-19 op if (argc != 2)
659 310ef57c 2022-02-19 op ctl_usage(res->ctl);
661 310ef57c 2022-02-19 op if (!strcmp(argv[1], "on"))
663 310ef57c 2022-02-19 op else if (!strcmp(argv[1], "off"))
666 310ef57c 2022-02-19 op ctl_usage(res->ctl);
668 310ef57c 2022-02-19 op res->rep.repeat_one = -1;
669 310ef57c 2022-02-19 op res->rep.repeat_all = -1;
670 310ef57c 2022-02-19 op if (!strcmp(argv[0], "one"))
671 310ef57c 2022-02-19 op res->rep.repeat_one = b;
672 310ef57c 2022-02-19 op else if (!strcmp(argv[0], "all"))
673 310ef57c 2022-02-19 op res->rep.repeat_all = b;
675 310ef57c 2022-02-19 op ctl_usage(res->ctl);
677 90122a37 2022-05-14 op return ctlaction(res);
681 90122a37 2022-05-14 op ctl_monitor(struct parse_result *res, int argc, char **argv)
684 90122a37 2022-05-14 op const char *events;
685 90122a37 2022-05-14 op char *dup, *tmp, *tok;
687 90122a37 2022-05-14 op while ((ch = getopt(argc, argv, "")) != -1)
688 90122a37 2022-05-14 op ctl_usage(res->ctl);
689 90122a37 2022-05-14 op argc -= optind;
690 90122a37 2022-05-14 op argv += optind;
692 90122a37 2022-05-14 op if (argc > 1)
693 90122a37 2022-05-14 op ctl_usage(res->ctl);
695 90122a37 2022-05-14 op if (argc == 1)
696 90122a37 2022-05-14 op events = *argv;
698 90122a37 2022-05-14 op events = "play,toggle,pause,stop,restart,flush,next,prev,"
699 90122a37 2022-05-14 op "jump,repeat,add,load";
701 90122a37 2022-05-14 op tmp = dup = xstrdup(events);
702 90122a37 2022-05-14 op while ((tok = strsep(&tmp, ",")) != NULL) {
703 90122a37 2022-05-14 op if (*tok == '\0')
706 90122a37 2022-05-14 op if (!strcmp(tok, "play"))
707 90122a37 2022-05-14 op res->monitor[IMSG_CTL_PLAY] = 1;
708 90122a37 2022-05-14 op else if (!strcmp(tok, "toggle"))
709 90122a37 2022-05-14 op res->monitor[IMSG_CTL_TOGGLE_PLAY] = 1;
710 90122a37 2022-05-14 op else if (!strcmp(tok, "pause"))
711 90122a37 2022-05-14 op res->monitor[IMSG_CTL_PAUSE] = 1;
712 90122a37 2022-05-14 op else if (!strcmp(tok, "stop"))
713 90122a37 2022-05-14 op res->monitor[IMSG_CTL_STOP] = 1;
714 90122a37 2022-05-14 op else if (!strcmp(tok, "restart"))
715 90122a37 2022-05-14 op res->monitor[IMSG_CTL_RESTART] = 1;
716 90122a37 2022-05-14 op else if (!strcmp(tok, "flush"))
717 90122a37 2022-05-14 op res->monitor[IMSG_CTL_FLUSH] = 1;
718 90122a37 2022-05-14 op else if (!strcmp(tok, "next"))
719 90122a37 2022-05-14 op res->monitor[IMSG_CTL_NEXT] = 1;
720 90122a37 2022-05-14 op else if (!strcmp(tok, "prev"))
721 90122a37 2022-05-14 op res->monitor[IMSG_CTL_PREV] = 1;
722 90122a37 2022-05-14 op else if (!strcmp(tok, "jump"))
723 90122a37 2022-05-14 op res->monitor[IMSG_CTL_JUMP] = 1;
724 90122a37 2022-05-14 op else if (!strcmp(tok, "repeat"))
725 90122a37 2022-05-14 op res->monitor[IMSG_CTL_REPEAT] = 1;
726 90122a37 2022-05-14 op else if (!strcmp(tok, "add"))
727 90122a37 2022-05-14 op res->monitor[IMSG_CTL_ADD] = 1;
728 90122a37 2022-05-14 op else if (!strcmp(tok, "load"))
729 90122a37 2022-05-14 op res->monitor[IMSG_CTL_COMMIT] = 1;
731 90122a37 2022-05-14 op fatalx("unknown event \"%s\"", tok);
735 e5d4e9f7 2022-07-09 op return ctlaction(res);
739 e5d4e9f7 2022-07-09 op ctl_seek(struct parse_result *res, int argc, char **argv)
741 e5d4e9f7 2022-07-09 op const char *n, *errstr;
743 e5d4e9f7 2022-07-09 op if (argc > 0) {
744 e5d4e9f7 2022-07-09 op /* skip the command name */
749 e5d4e9f7 2022-07-09 op if (argc > 0 && !strcmp(*argv, "--")) {
754 e5d4e9f7 2022-07-09 op if (argc != 1)
755 e5d4e9f7 2022-07-09 op ctl_usage(res->ctl);
758 e5d4e9f7 2022-07-09 op if (*n == '-' || *n == '+')
759 e5d4e9f7 2022-07-09 op res->seek.relative = 1;
761 e5d4e9f7 2022-07-09 op res->seek.offset = strtonum(n, INT64_MIN, INT64_MAX, &errstr);
762 e5d4e9f7 2022-07-09 op if (errstr != NULL)
763 e5d4e9f7 2022-07-09 op fatalx("offset is %s: %s", errstr, n);
765 a913de21 2022-02-17 op return ctlaction(res);
769 1d673950 2022-03-03 op ctl_get_lock(const char *lockfile)
773 1d673950 2022-03-03 op if ((lockfd = open(lockfile, O_WRONLY|O_CREAT, 0600)) == -1) {
774 1d673950 2022-03-03 op log_debug("open failed: %s", strerror(errno));
778 1d673950 2022-03-03 op if (flock(lockfd, LOCK_EX|LOCK_NB) == -1) {
779 1d673950 2022-03-03 op log_debug("flock failed: %s", strerror(errno));
780 1d673950 2022-03-03 op if (errno != EAGAIN)
782 1d673950 2022-03-03 op while (flock(lockfd, LOCK_EX) == -1 && errno == EINTR)
784 1d673950 2022-03-03 op close(lockfd);
787 1d673950 2022-03-03 op log_debug("flock succeeded");
789 1d673950 2022-03-03 op return lockfd;
793 1d673950 2022-03-03 op ctl_connect(void)
795 1d673950 2022-03-03 op struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
796 1d673950 2022-03-03 op struct sockaddr_un sa;
798 1d673950 2022-03-03 op int fd, lockfd = -1, locked = 0, spawned = 0;
799 1b77ad48 2022-07-08 op int attempt = 0;
800 1d673950 2022-03-03 op char *lockfile = NULL;
802 1d673950 2022-03-03 op memset(&sa, 0, sizeof(sa));
803 1d673950 2022-03-03 op sa.sun_family = AF_UNIX;
804 1d673950 2022-03-03 op size = strlcpy(sa.sun_path, csock, sizeof(sa.sun_path));
805 1d673950 2022-03-03 op if (size >= sizeof(sa.sun_path)) {
806 1d673950 2022-03-03 op errno = ENAMETOOLONG;
811 1d673950 2022-03-03 op if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
814 1d673950 2022-03-03 op if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
815 1d673950 2022-03-03 op log_debug("connection failed: %s", strerror(errno));
816 1d673950 2022-03-03 op if (errno != ECONNREFUSED && errno != ENOENT)
818 1b77ad48 2022-07-08 op if (attempt++ == 100)
822 1d673950 2022-03-03 op if (!locked) {
823 1d673950 2022-03-03 op xasprintf(&lockfile, "%s.lock", csock);
824 1d673950 2022-03-03 op if ((lockfd = ctl_get_lock(lockfile)) < 0) {
825 1d673950 2022-03-03 op log_debug("didn't get the lock (%d)", lockfd);
827 1d673950 2022-03-03 op free(lockfile);
828 1d673950 2022-03-03 op lockfile = NULL;
830 1d673950 2022-03-03 op if (lockfd == -1)
835 1d673950 2022-03-03 op * Always retry at least once, even if we got
836 1d673950 2022-03-03 op * the lock, because another client could have
837 1d673950 2022-03-03 op * taken the lock, started the server and released
838 1d673950 2022-03-03 op * the lock between our connect() and flock()
844 1d673950 2022-03-03 op if (!spawned) {
845 1d673950 2022-03-03 op log_debug("spawning the daemon");
846 1d673950 2022-03-03 op spawn_daemon();
850 1d673950 2022-03-03 op nanosleep(&ts, NULL);
854 1d673950 2022-03-03 op if (locked && lockfd >= 0) {
855 1d673950 2022-03-03 op unlink(lockfile);
856 1d673950 2022-03-03 op free(lockfile);
857 1d673950 2022-03-03 op close(lockfd);
862 1d673950 2022-03-03 op if (locked) {
863 1d673950 2022-03-03 op free(lockfile);
864 1d673950 2022-03-03 op close(lockfd);
871 fea541a8 2022-02-16 op ctl(int argc, char **argv)
873 1d673950 2022-03-03 op int ctl_sock;
875 fea541a8 2022-02-16 op log_init(1, LOG_DAEMON);
876 fea541a8 2022-02-16 op log_setverbose(verbose);
878 96233476 2022-06-09 op if (getcwd(cwd, sizeof(cwd)) == NULL)
879 96233476 2022-06-09 op fatal("getcwd");
881 1d673950 2022-03-03 op if ((ctl_sock = ctl_connect()) == -1)
882 1d673950 2022-03-03 op fatal("can't connect");
884 fea541a8 2022-02-16 op if (ctl_sock == -1)
885 fea541a8 2022-02-16 op fatalx("failed to connect to the daemon");
887 3baa2617 2022-02-16 op ibuf = xmalloc(sizeof(*ibuf));
888 3baa2617 2022-02-16 op imsg_init(ibuf, ctl_sock);
890 3baa2617 2022-02-16 op optreset = 1;
893 3baa2617 2022-02-16 op exit(parse(argc, argv));