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 f36fd90a 2022-07-09 op #include "config.h"
20 3baa2617 2022-02-16 op #include <sys/socket.h>
21 3baa2617 2022-02-16 op #include <sys/un.h>
23 3baa2617 2022-02-16 op #include <errno.h>
24 1d673950 2022-03-03 op #include <fcntl.h>
25 3baa2617 2022-02-16 op #include <limits.h>
26 3baa2617 2022-02-16 op #include <stdio.h>
27 3baa2617 2022-02-16 op #include <stdlib.h>
28 3baa2617 2022-02-16 op #include <stdint.h>
29 3baa2617 2022-02-16 op #include <string.h>
30 3baa2617 2022-02-16 op #include <syslog.h>
31 3baa2617 2022-02-16 op #include <unistd.h>
33 3baa2617 2022-02-16 op #include "amused.h"
34 3baa2617 2022-02-16 op #include "log.h"
35 bb3f279f 2022-02-16 op #include "playlist.h"
36 3baa2617 2022-02-16 op #include "xmalloc.h"
38 96233476 2022-06-09 op static struct imsgbuf *ibuf;
39 96233476 2022-06-09 op char cwd[PATH_MAX];
41 2ddcfa40 2022-07-08 op static int ctl_noarg(struct parse_result *, int, char **);
42 2ddcfa40 2022-07-08 op static int ctl_add(struct parse_result *, int, char **);
43 2ddcfa40 2022-07-08 op static int ctl_show(struct parse_result *, int, char **);
44 2ddcfa40 2022-07-08 op static int ctl_load(struct parse_result *, int, char **);
45 2ddcfa40 2022-07-08 op static int ctl_jump(struct parse_result *, int, char **);
46 2ddcfa40 2022-07-08 op static int ctl_repeat(struct parse_result *, int, char **);
47 2ddcfa40 2022-07-08 op static int ctl_monitor(struct parse_result *, int, char **);
48 e5d4e9f7 2022-07-09 op static int ctl_seek(struct parse_result *, int, char **);
50 3baa2617 2022-02-16 op struct ctl_command ctl_commands[] = {
51 d488e5a5 2022-07-08 op { "add", ADD, ctl_add, "files...", 0 },
52 d488e5a5 2022-07-08 op { "flush", FLUSH, ctl_noarg, "", 0 },
53 d488e5a5 2022-07-08 op { "jump", JUMP, ctl_jump, "pattern", 0 },
54 6a128dce 2022-07-08 op { "load", LOAD, ctl_load, "[file]", 1 },
55 d488e5a5 2022-07-08 op { "monitor", MONITOR, ctl_monitor, "[events]", 0 },
56 6a128dce 2022-07-08 op { "next", NEXT, ctl_noarg, "", 0 },
57 6a128dce 2022-07-08 op { "pause", PAUSE, ctl_noarg, "", 0 },
58 6a128dce 2022-07-08 op { "play", PLAY, ctl_noarg, "", 0 },
59 6a128dce 2022-07-08 op { "prev", PREV, ctl_noarg, "", 0 },
60 6a128dce 2022-07-08 op { "repeat", REPEAT, ctl_repeat, "one|all on|off", 0 },
61 6a128dce 2022-07-08 op { "restart", RESTART, ctl_noarg, "", 0 },
62 e5d4e9f7 2022-07-09 op { "seek", SEEK, ctl_seek, "[+-]time", 0 },
63 6a128dce 2022-07-08 op { "show", SHOW, ctl_show, "[-p]", 0 },
64 6a128dce 2022-07-08 op { "status", STATUS, ctl_noarg, "", 0 },
65 6a128dce 2022-07-08 op { "stop", STOP, ctl_noarg, "", 0 },
66 6a128dce 2022-07-08 op { "toggle", TOGGLE, ctl_noarg, "", 0 },
67 d488e5a5 2022-07-08 op { NULL, 0, NULL, NULL, 0 },
73 3baa2617 2022-02-16 op fprintf(stderr, "usage: %s [-dv] [-s socket]\n", getprogname());
77 3baa2617 2022-02-16 op static __dead void
78 3baa2617 2022-02-16 op ctl_usage(struct ctl_command *ctl)
80 3baa2617 2022-02-16 op fprintf(stderr, "usage: %s [-v] [-s socket] %s %s\n", getprogname(),
81 3baa2617 2022-02-16 op ctl->name, ctl->usage);
85 96233476 2022-06-09 op /* based on canonpath from kern_pledge.c */
87 96233476 2022-06-09 op canonpath(const char *input, char *buf, size_t bufsize)
89 96233476 2022-06-09 op const char *p;
90 96233476 2022-06-09 op char *q, path[PATH_MAX];
92 96233476 2022-06-09 op if (input[0] != '/') {
93 96233476 2022-06-09 op if (snprintf(path, sizeof(path), "%s/%s", cwd, input)
94 96233476 2022-06-09 op >= sizeof(path)) {
95 96233476 2022-06-09 op errno = ENAMETOOLONG;
103 96233476 2022-06-09 op while (*p && (q - buf < bufsize)) {
104 96233476 2022-06-09 op if (p[0] == '/' && (p[1] == '/' || p[1] == '\0')) {
107 96233476 2022-06-09 op } else if (p[0] == '/' && p[1] == '.' &&
108 96233476 2022-06-09 op (p[2] == '/' || p[2] == '\0')) {
111 96233476 2022-06-09 op } else if (p[0] == '/' && p[1] == '.' && p[2] == '.' &&
112 96233476 2022-06-09 op (p[3] == '/' || p[3] == '\0')) {
114 96233476 2022-06-09 op if (q != buf) /* "/../" at start of buf */
115 96233476 2022-06-09 op while (*--q != '/')
122 96233476 2022-06-09 op if ((*p == '\0') && (q - buf < bufsize)) {
126 96233476 2022-06-09 op errno = ENAMETOOLONG;
132 3baa2617 2022-02-16 op parse(int argc, char **argv)
134 3baa2617 2022-02-16 op struct ctl_command *ctl = NULL;
135 3baa2617 2022-02-16 op struct parse_result res;
136 1d673950 2022-03-03 op const char *argv0;
137 3baa2617 2022-02-16 op int i, status;
139 3baa2617 2022-02-16 op memset(&res, 0, sizeof(res));
141 1d673950 2022-03-03 op if ((argv0 = argv[0]) == NULL)
142 1d673950 2022-03-03 op argv0 = "status";
144 3baa2617 2022-02-16 op for (i = 0; ctl_commands[i].name != NULL; ++i) {
145 1d673950 2022-03-03 op if (strncmp(ctl_commands[i].name, argv0, strlen(argv0))
147 3baa2617 2022-02-16 op if (ctl != NULL) {
148 3baa2617 2022-02-16 op fprintf(stderr,
149 1d673950 2022-03-03 op "ambiguous argument: %s\n", argv0);
152 3baa2617 2022-02-16 op ctl = &ctl_commands[i];
156 3baa2617 2022-02-16 op if (ctl == NULL) {
157 3baa2617 2022-02-16 op fprintf(stderr, "unknown argument: %s\n", argv[0]);
161 3baa2617 2022-02-16 op res.action = ctl->action;
162 3baa2617 2022-02-16 op res.ctl = ctl;
164 3baa2617 2022-02-16 op if (!ctl->has_pledge) {
165 3baa2617 2022-02-16 op /* pledge(2) default if command doesn't have its own */
166 3baa2617 2022-02-16 op if (pledge("stdio", NULL) == -1)
167 3baa2617 2022-02-16 op fatal("pledge");
170 3baa2617 2022-02-16 op status = ctl->main(&res, argc, argv);
171 3baa2617 2022-02-16 op close(ibuf->fd);
173 3baa2617 2022-02-16 op return status;
177 fad0fb69 2022-05-28 op load_files(struct parse_result *res, int *ret)
180 7a427ecd 2022-02-17 op const char *file;
181 7a427ecd 2022-02-17 op char *line = NULL;
182 96233476 2022-06-09 op char path[PATH_MAX];
183 96233476 2022-06-09 op size_t linesize = 0, i = 0;
184 3af93963 2022-03-02 op ssize_t linelen, curr = -1;
186 ec1fb0c7 2022-02-17 op if (res->file == NULL)
188 ec1fb0c7 2022-02-17 op else if ((f = fopen(res->file, "r")) == NULL) {
189 ec1fb0c7 2022-02-17 op log_warn("can't open %s", res->file);
194 ec1fb0c7 2022-02-17 op while ((linelen = getline(&line, &linesize, f)) != -1) {
195 7a427ecd 2022-02-17 op if (linelen == 0)
197 7a427ecd 2022-02-17 op line[linelen-1] = '\0';
199 1dd3b054 2022-03-25 op if (!strncmp(file, "> ", 2)) {
202 1dd3b054 2022-03-25 op } else if (!strncmp(file, " ", 2))
205 18223c28 2022-06-13 op memset(path, 0, sizeof(path));
206 96233476 2022-06-09 op if (canonpath(file, path, sizeof(path)) == -1) {
207 f1beb69a 2022-06-13 op log_warn("canonpath %s", file);
212 7a427ecd 2022-02-17 op imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
213 7a427ecd 2022-02-17 op path, sizeof(path));
217 ec1fb0c7 2022-02-17 op if (ferror(f))
218 7a427ecd 2022-02-17 op fatal("getline");
221 3af93963 2022-03-02 op if (i == 0) {
226 7a427ecd 2022-02-17 op imsg_compose(ibuf, IMSG_CTL_COMMIT, 0, 0, -1,
227 3af93963 2022-03-02 op &curr, sizeof(curr));
228 7a427ecd 2022-02-17 op imsg_flush(ibuf);
232 fad0fb69 2022-05-28 op static const char *
233 fad0fb69 2022-05-28 op imsg_strerror(struct imsg *imsg)
235 fad0fb69 2022-05-28 op size_t datalen;
236 fad0fb69 2022-05-28 op const char *msg;
238 fad0fb69 2022-05-28 op datalen = IMSG_DATA_SIZE(*imsg);
239 fad0fb69 2022-05-28 op msg = imsg->data;
240 fad0fb69 2022-05-28 op if (datalen == 0 || msg[datalen-1] != '\0')
241 fad0fb69 2022-05-28 op fatalx("malformed error message");
246 fad0fb69 2022-05-28 op static const char *
247 fad0fb69 2022-05-28 op imsg_name(int type)
249 6b47a39f 2022-02-21 op switch (type) {
250 6b47a39f 2022-02-21 op case IMSG_CTL_PLAY:
251 fad0fb69 2022-05-28 op return "play";
252 6b47a39f 2022-02-21 op case IMSG_CTL_TOGGLE_PLAY:
253 fad0fb69 2022-05-28 op return "toggle";
254 6b47a39f 2022-02-21 op case IMSG_CTL_PAUSE:
255 fad0fb69 2022-05-28 op return "pause";
256 6b47a39f 2022-02-21 op case IMSG_CTL_STOP:
257 fad0fb69 2022-05-28 op return "stop";
258 6b47a39f 2022-02-21 op case IMSG_CTL_RESTART:
259 fad0fb69 2022-05-28 op return "restart";
260 6b47a39f 2022-02-21 op case IMSG_CTL_FLUSH:
261 fad0fb69 2022-05-28 op return "flush";
262 6b47a39f 2022-02-21 op case IMSG_CTL_NEXT:
263 fad0fb69 2022-05-28 op return "next";
264 6b47a39f 2022-02-21 op case IMSG_CTL_PREV:
265 fad0fb69 2022-05-28 op return "prev";
266 6b47a39f 2022-02-21 op case IMSG_CTL_JUMP:
267 fad0fb69 2022-05-28 op return "jump";
268 6b47a39f 2022-02-21 op case IMSG_CTL_REPEAT:
269 fad0fb69 2022-05-28 op return "repeat";
270 6b47a39f 2022-02-21 op case IMSG_CTL_ADD:
271 fad0fb69 2022-05-28 op return "add";
272 6b47a39f 2022-02-21 op case IMSG_CTL_COMMIT:
273 fad0fb69 2022-05-28 op return "load";
275 fad0fb69 2022-05-28 op return "unknown";
280 bdc8c4e0 2022-07-08 op print_time(const char *label, int64_t seconds)
282 bdc8c4e0 2022-07-08 op int hours, minutes;
284 bdc8c4e0 2022-07-08 op if (seconds < 0)
287 bdc8c4e0 2022-07-08 op hours = seconds / 3600;
288 bdc8c4e0 2022-07-08 op seconds -= hours * 3600;
290 bdc8c4e0 2022-07-08 op minutes = seconds / 60;
291 bdc8c4e0 2022-07-08 op seconds -= minutes * 60;
293 bdc8c4e0 2022-07-08 op printf("%s ", label);
294 bdc8c4e0 2022-07-08 op if (hours != 0)
295 bdc8c4e0 2022-07-08 op printf("%02d:", hours);
296 bdc8c4e0 2022-07-08 op printf("%02d:%02lld\n", minutes, (long long)seconds);
300 3baa2617 2022-02-16 op ctlaction(struct parse_result *res)
302 fad0fb69 2022-05-28 op char path[PATH_MAX];
303 3baa2617 2022-02-16 op struct imsg imsg;
304 fad0fb69 2022-05-28 op struct player_status ps;
305 fad0fb69 2022-05-28 op size_t datalen;
307 fad0fb69 2022-05-28 op int i, type, ret = 0, done = 1;
309 3baa2617 2022-02-16 op switch (res->action) {
311 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
312 00f500ff 2022-02-19 op if (verbose) {
313 00f500ff 2022-02-19 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
319 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
322 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
323 00f500ff 2022-02-19 op if (verbose) {
324 00f500ff 2022-02-19 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
330 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
332 3baa2617 2022-02-16 op case RESTART:
333 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_RESTART, 0, 0, -1, NULL, 0);
334 00f500ff 2022-02-19 op if (verbose) {
335 00f500ff 2022-02-19 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
342 d903ec9a 2022-05-29 op for (i = 0; res->files[i] != NULL; ++i) {
343 18223c28 2022-06-13 op memset(path, 0, sizeof(path));
344 ef593b43 2022-06-09 op if (canonpath(res->files[i], path, sizeof(path))
346 ef593b43 2022-06-09 op log_warn("canonpath %s", res->files[i]);
350 fad0fb69 2022-05-28 op imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
351 fad0fb69 2022-05-28 op path, sizeof(path));
353 fad0fb69 2022-05-28 op ret = i == 0;
356 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
360 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
364 bb3f279f 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
367 af27e631 2022-02-17 op imsg_compose(ibuf, IMSG_CTL_NEXT, 0, 0, -1, NULL, 0);
368 00f500ff 2022-02-19 op if (verbose) {
369 00f500ff 2022-02-19 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
375 af27e631 2022-02-17 op imsg_compose(ibuf, IMSG_CTL_PREV, 0, 0, -1, NULL, 0);
376 00f500ff 2022-02-19 op if (verbose) {
377 00f500ff 2022-02-19 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
384 7a427ecd 2022-02-17 op imsg_compose(ibuf, IMSG_CTL_BEGIN, 0, 0, -1, NULL, 0);
388 fad0fb69 2022-05-28 op memset(path, 0, sizeof(path));
389 fad0fb69 2022-05-28 op strlcpy(path, res->file, sizeof(path));
390 fad0fb69 2022-05-28 op imsg_compose(ibuf, IMSG_CTL_JUMP, 0, 0, -1,
391 fad0fb69 2022-05-28 op path, sizeof(path));
394 310ef57c 2022-02-19 op imsg_compose(ibuf, IMSG_CTL_REPEAT, 0, 0, -1,
395 310ef57c 2022-02-19 op &res->rep, sizeof(res->rep));
397 6b47a39f 2022-02-21 op case MONITOR:
399 6b47a39f 2022-02-21 op imsg_compose(ibuf, IMSG_CTL_MONITOR, 0, 0, -1,
403 e5d4e9f7 2022-07-09 op imsg_compose(ibuf, IMSG_CTL_SEEK, 0, 0, -1, &res->seek,
404 e5d4e9f7 2022-07-09 op sizeof(res->seek));
407 3baa2617 2022-02-16 op /* action not expected */
408 3baa2617 2022-02-16 op fatalx("invalid action %u", res->action);
412 3baa2617 2022-02-16 op if (ret != 0)
415 3baa2617 2022-02-16 op imsg_flush(ibuf);
418 3baa2617 2022-02-16 op while (!done) {
419 3baa2617 2022-02-16 op if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
420 3baa2617 2022-02-16 op fatalx("imsg_read error");
422 3baa2617 2022-02-16 op fatalx("pipe closed");
424 3baa2617 2022-02-16 op while (!done) {
425 3baa2617 2022-02-16 op if ((n = imsg_get(ibuf, &imsg)) == -1)
426 3baa2617 2022-02-16 op fatalx("imsg_get error");
430 fad0fb69 2022-05-28 op if (imsg.hdr.type == IMSG_CTL_ERR) {
431 fad0fb69 2022-05-28 op log_warnx("%s: %s", res->ctl->name,
432 fad0fb69 2022-05-28 op imsg_strerror(&imsg));
438 fad0fb69 2022-05-28 op datalen = IMSG_DATA_SIZE(imsg);
440 3baa2617 2022-02-16 op switch (res->action) {
442 d903ec9a 2022-05-29 op if (res->files[i] == NULL)
443 fad0fb69 2022-05-28 op fatalx("received more replies than "
444 fad0fb69 2022-05-28 op "files enqueued.");
446 fad0fb69 2022-05-28 op if (imsg.hdr.type == IMSG_CTL_ADD)
447 d903ec9a 2022-05-29 op log_debug("enqueued %s", res->files[i]);
449 fad0fb69 2022-05-28 op fatalx("invalid message %d",
450 fad0fb69 2022-05-28 op imsg.hdr.type);
452 d903ec9a 2022-05-29 op done = res->files[i] == NULL;
455 fad0fb69 2022-05-28 op if (datalen == 0) {
459 fad0fb69 2022-05-28 op if (datalen != sizeof(ps))
460 fad0fb69 2022-05-28 op fatalx("data size mismatch");
461 fad0fb69 2022-05-28 op memcpy(&ps, imsg.data, sizeof(ps));
462 fad0fb69 2022-05-28 op if (ps.path[sizeof(ps.path) - 1] != '\0')
463 fad0fb69 2022-05-28 op fatalx("received corrupted data");
464 fad0fb69 2022-05-28 op if (res->pretty) {
465 fad0fb69 2022-05-28 op char c = ' ';
466 fad0fb69 2022-05-28 op if (ps.status == STATE_PLAYING)
468 fad0fb69 2022-05-28 op printf("%c ", c);
470 fad0fb69 2022-05-28 op puts(ps.path);
474 146307bd 2022-02-17 op case RESTART:
479 fad0fb69 2022-05-28 op if (imsg.hdr.type != IMSG_CTL_STATUS)
480 fad0fb69 2022-05-28 op fatalx("invalid message %d",
481 fad0fb69 2022-05-28 op imsg.hdr.type);
483 fad0fb69 2022-05-28 op if (datalen != sizeof(ps))
484 fad0fb69 2022-05-28 op fatalx("data size mismatch");
485 fad0fb69 2022-05-28 op memcpy(&ps, imsg.data, sizeof(ps));
486 fad0fb69 2022-05-28 op if (ps.path[sizeof(ps.path) - 1] != '\0')
487 fad0fb69 2022-05-28 op fatalx("received corrupted data");
489 fad0fb69 2022-05-28 op if (ps.status == STATE_STOPPED)
490 fad0fb69 2022-05-28 op printf("stopped ");
491 fad0fb69 2022-05-28 op else if (ps.status == STATE_PLAYING)
492 fad0fb69 2022-05-28 op printf("playing ");
493 fad0fb69 2022-05-28 op else if (ps.status == STATE_PAUSED)
494 fad0fb69 2022-05-28 op printf("paused ");
496 fad0fb69 2022-05-28 op printf("unknown ");
498 fad0fb69 2022-05-28 op puts(ps.path);
500 bdc8c4e0 2022-07-08 op print_time("position", ps.position);
501 bdc8c4e0 2022-07-08 op print_time("duration", ps.duration);
503 dc12484b 2022-07-08 op printf("repeat one %s\nrepeat all %s\n",
504 fad0fb69 2022-05-28 op ps.rp.repeat_one ? "on" : "off",
505 fad0fb69 2022-05-28 op ps.rp.repeat_all ? "on" : "off");
510 fad0fb69 2022-05-28 op if (imsg.hdr.type == IMSG_CTL_ADD)
512 fad0fb69 2022-05-28 op if (imsg.hdr.type == IMSG_CTL_COMMIT) {
517 fad0fb69 2022-05-28 op if (imsg.hdr.type != IMSG_CTL_BEGIN)
518 fad0fb69 2022-05-28 op fatalx("invalid message %d",
519 fad0fb69 2022-05-28 op imsg.hdr.type);
521 fad0fb69 2022-05-28 op load_files(res, &ret);
523 6b47a39f 2022-02-21 op case MONITOR:
524 fad0fb69 2022-05-28 op if (imsg.hdr.type != IMSG_CTL_MONITOR)
525 fad0fb69 2022-05-28 op fatalx("invalid message %d",
526 fad0fb69 2022-05-28 op imsg.hdr.type);
528 fad0fb69 2022-05-28 op if (datalen != sizeof(type))
529 fad0fb69 2022-05-28 op fatalx("data size mismatch");
531 fad0fb69 2022-05-28 op memcpy(&type, imsg.data, sizeof(type));
532 fad0fb69 2022-05-28 op if (type < 0 || type > IMSG__LAST)
533 fad0fb69 2022-05-28 op fatalx("received corrupted data");
535 fad0fb69 2022-05-28 op if (!res->monitor[type])
538 fad0fb69 2022-05-28 op puts(imsg_name(type));
539 fad0fb69 2022-05-28 op fflush(stdout);
546 3baa2617 2022-02-16 op imsg_free(&imsg);
555 3baa2617 2022-02-16 op ctl_noarg(struct parse_result *res, int argc, char **argv)
559 7e29fc4a 2022-03-03 op while ((ch = getopt(argc, argv, "")) != -1)
560 7e29fc4a 2022-03-03 op ctl_usage(res->ctl);
561 7e29fc4a 2022-03-03 op argc -= optind;
562 7e29fc4a 2022-03-03 op argv += optind;
564 acaf7eb2 2022-03-05 op if (argc > 0)
565 3baa2617 2022-02-16 op ctl_usage(res->ctl);
567 3baa2617 2022-02-16 op return ctlaction(res);
571 3baa2617 2022-02-16 op ctl_add(struct parse_result *res, int argc, char **argv)
575 7e29fc4a 2022-03-03 op while ((ch = getopt(argc, argv, "")) != -1)
576 3baa2617 2022-02-16 op ctl_usage(res->ctl);
577 7e29fc4a 2022-03-03 op argc -= optind;
578 7e29fc4a 2022-03-03 op argv += optind;
580 7e29fc4a 2022-03-03 op if (argc == 0)
581 7e29fc4a 2022-03-03 op ctl_usage(res->ctl);
582 7e29fc4a 2022-03-03 op res->files = argv;
584 3baa2617 2022-02-16 op return ctlaction(res);
588 8e916714 2022-02-17 op ctl_show(struct parse_result *res, int argc, char **argv)
592 8e916714 2022-02-17 op while ((ch = getopt(argc, argv, "p")) != -1) {
593 8e916714 2022-02-17 op switch (ch) {
595 8e916714 2022-02-17 op res->pretty = 1;
598 8e916714 2022-02-17 op ctl_usage(res->ctl);
602 8e916714 2022-02-17 op return ctlaction(res);
606 14be015f 2022-02-17 op ctl_load(struct parse_result *res, int argc, char **argv)
610 7e29fc4a 2022-03-03 op while ((ch = getopt(argc, argv, "")) != -1)
611 7e29fc4a 2022-03-03 op ctl_usage(res->ctl);
612 7e29fc4a 2022-03-03 op argc -= optind;
613 7e29fc4a 2022-03-03 op argv += optind;
615 7e29fc4a 2022-03-03 op if (argc == 0)
616 ec1fb0c7 2022-02-17 op res->file = NULL;
617 7e29fc4a 2022-03-03 op else if (argc == 1)
618 7e29fc4a 2022-03-03 op res->file = argv[0];
620 14be015f 2022-02-17 op ctl_usage(res->ctl);
622 14be015f 2022-02-17 op if (pledge("stdio rpath", NULL) == -1)
623 14be015f 2022-02-17 op fatal("pledge");
625 7a427ecd 2022-02-17 op return ctlaction(res);
629 a913de21 2022-02-17 op ctl_jump(struct parse_result *res, int argc, char **argv)
633 a913de21 2022-02-17 op while ((ch = getopt(argc, argv, "")) != -1)
634 a913de21 2022-02-17 op ctl_usage(res->ctl);
635 a913de21 2022-02-17 op argc -= optind;
636 a913de21 2022-02-17 op argv += optind;
638 a913de21 2022-02-17 op if (argc != 1)
639 a913de21 2022-02-17 op ctl_usage(res->ctl);
641 a913de21 2022-02-17 op res->file = argv[0];
642 310ef57c 2022-02-19 op return ctlaction(res);
646 310ef57c 2022-02-19 op ctl_repeat(struct parse_result *res, int argc, char **argv)
650 310ef57c 2022-02-19 op while ((ch = getopt(argc, argv, "")) != -1)
651 310ef57c 2022-02-19 op ctl_usage(res->ctl);
652 310ef57c 2022-02-19 op argc -= optind;
653 310ef57c 2022-02-19 op argv += optind;
655 310ef57c 2022-02-19 op if (argc != 2)
656 310ef57c 2022-02-19 op ctl_usage(res->ctl);
658 310ef57c 2022-02-19 op if (!strcmp(argv[1], "on"))
660 310ef57c 2022-02-19 op else if (!strcmp(argv[1], "off"))
663 310ef57c 2022-02-19 op ctl_usage(res->ctl);
665 310ef57c 2022-02-19 op res->rep.repeat_one = -1;
666 310ef57c 2022-02-19 op res->rep.repeat_all = -1;
667 310ef57c 2022-02-19 op if (!strcmp(argv[0], "one"))
668 310ef57c 2022-02-19 op res->rep.repeat_one = b;
669 310ef57c 2022-02-19 op else if (!strcmp(argv[0], "all"))
670 310ef57c 2022-02-19 op res->rep.repeat_all = b;
672 310ef57c 2022-02-19 op ctl_usage(res->ctl);
674 90122a37 2022-05-14 op return ctlaction(res);
678 90122a37 2022-05-14 op ctl_monitor(struct parse_result *res, int argc, char **argv)
681 90122a37 2022-05-14 op const char *events;
682 90122a37 2022-05-14 op char *dup, *tmp, *tok;
684 90122a37 2022-05-14 op while ((ch = getopt(argc, argv, "")) != -1)
685 90122a37 2022-05-14 op ctl_usage(res->ctl);
686 90122a37 2022-05-14 op argc -= optind;
687 90122a37 2022-05-14 op argv += optind;
689 90122a37 2022-05-14 op if (argc > 1)
690 90122a37 2022-05-14 op ctl_usage(res->ctl);
692 90122a37 2022-05-14 op if (argc == 1)
693 90122a37 2022-05-14 op events = *argv;
695 90122a37 2022-05-14 op events = "play,toggle,pause,stop,restart,flush,next,prev,"
696 90122a37 2022-05-14 op "jump,repeat,add,load";
698 90122a37 2022-05-14 op tmp = dup = xstrdup(events);
699 90122a37 2022-05-14 op while ((tok = strsep(&tmp, ",")) != NULL) {
700 90122a37 2022-05-14 op if (*tok == '\0')
703 90122a37 2022-05-14 op if (!strcmp(tok, "play"))
704 90122a37 2022-05-14 op res->monitor[IMSG_CTL_PLAY] = 1;
705 90122a37 2022-05-14 op else if (!strcmp(tok, "toggle"))
706 90122a37 2022-05-14 op res->monitor[IMSG_CTL_TOGGLE_PLAY] = 1;
707 90122a37 2022-05-14 op else if (!strcmp(tok, "pause"))
708 90122a37 2022-05-14 op res->monitor[IMSG_CTL_PAUSE] = 1;
709 90122a37 2022-05-14 op else if (!strcmp(tok, "stop"))
710 90122a37 2022-05-14 op res->monitor[IMSG_CTL_STOP] = 1;
711 90122a37 2022-05-14 op else if (!strcmp(tok, "restart"))
712 90122a37 2022-05-14 op res->monitor[IMSG_CTL_RESTART] = 1;
713 90122a37 2022-05-14 op else if (!strcmp(tok, "flush"))
714 90122a37 2022-05-14 op res->monitor[IMSG_CTL_FLUSH] = 1;
715 90122a37 2022-05-14 op else if (!strcmp(tok, "next"))
716 90122a37 2022-05-14 op res->monitor[IMSG_CTL_NEXT] = 1;
717 90122a37 2022-05-14 op else if (!strcmp(tok, "prev"))
718 90122a37 2022-05-14 op res->monitor[IMSG_CTL_PREV] = 1;
719 90122a37 2022-05-14 op else if (!strcmp(tok, "jump"))
720 90122a37 2022-05-14 op res->monitor[IMSG_CTL_JUMP] = 1;
721 90122a37 2022-05-14 op else if (!strcmp(tok, "repeat"))
722 90122a37 2022-05-14 op res->monitor[IMSG_CTL_REPEAT] = 1;
723 90122a37 2022-05-14 op else if (!strcmp(tok, "add"))
724 90122a37 2022-05-14 op res->monitor[IMSG_CTL_ADD] = 1;
725 90122a37 2022-05-14 op else if (!strcmp(tok, "load"))
726 90122a37 2022-05-14 op res->monitor[IMSG_CTL_COMMIT] = 1;
728 90122a37 2022-05-14 op fatalx("unknown event \"%s\"", tok);
732 e5d4e9f7 2022-07-09 op return ctlaction(res);
736 e5d4e9f7 2022-07-09 op ctl_seek(struct parse_result *res, int argc, char **argv)
738 e5d4e9f7 2022-07-09 op const char *n, *errstr;
740 e5d4e9f7 2022-07-09 op if (argc > 0) {
741 e5d4e9f7 2022-07-09 op /* skip the command name */
746 e5d4e9f7 2022-07-09 op if (argc > 0 && !strcmp(*argv, "--")) {
751 e5d4e9f7 2022-07-09 op if (argc != 1)
752 e5d4e9f7 2022-07-09 op ctl_usage(res->ctl);
755 e5d4e9f7 2022-07-09 op if (*n == '-' || *n == '+')
756 e5d4e9f7 2022-07-09 op res->seek.relative = 1;
758 e5d4e9f7 2022-07-09 op res->seek.offset = strtonum(n, INT64_MIN, INT64_MAX, &errstr);
759 e5d4e9f7 2022-07-09 op if (errstr != NULL)
760 e5d4e9f7 2022-07-09 op fatalx("offset is %s: %s", errstr, n);
762 a913de21 2022-02-17 op return ctlaction(res);
766 1d673950 2022-03-03 op ctl_get_lock(const char *lockfile)
770 1d673950 2022-03-03 op if ((lockfd = open(lockfile, O_WRONLY|O_CREAT, 0600)) == -1) {
771 1d673950 2022-03-03 op log_debug("open failed: %s", strerror(errno));
775 1d673950 2022-03-03 op if (flock(lockfd, LOCK_EX|LOCK_NB) == -1) {
776 1d673950 2022-03-03 op log_debug("flock failed: %s", strerror(errno));
777 1d673950 2022-03-03 op if (errno != EAGAIN)
779 1d673950 2022-03-03 op while (flock(lockfd, LOCK_EX) == -1 && errno == EINTR)
781 1d673950 2022-03-03 op close(lockfd);
784 1d673950 2022-03-03 op log_debug("flock succeeded");
786 1d673950 2022-03-03 op return lockfd;
790 1d673950 2022-03-03 op ctl_connect(void)
792 1d673950 2022-03-03 op struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
793 1d673950 2022-03-03 op struct sockaddr_un sa;
795 1d673950 2022-03-03 op int fd, lockfd = -1, locked = 0, spawned = 0;
796 1b77ad48 2022-07-08 op int attempt = 0;
797 1d673950 2022-03-03 op char *lockfile = NULL;
799 1d673950 2022-03-03 op memset(&sa, 0, sizeof(sa));
800 1d673950 2022-03-03 op sa.sun_family = AF_UNIX;
801 1d673950 2022-03-03 op size = strlcpy(sa.sun_path, csock, sizeof(sa.sun_path));
802 1d673950 2022-03-03 op if (size >= sizeof(sa.sun_path)) {
803 1d673950 2022-03-03 op errno = ENAMETOOLONG;
808 1d673950 2022-03-03 op if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
811 1d673950 2022-03-03 op if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
812 1d673950 2022-03-03 op log_debug("connection failed: %s", strerror(errno));
813 1d673950 2022-03-03 op if (errno != ECONNREFUSED && errno != ENOENT)
815 1b77ad48 2022-07-08 op if (attempt++ == 100)
819 1d673950 2022-03-03 op if (!locked) {
820 1d673950 2022-03-03 op xasprintf(&lockfile, "%s.lock", csock);
821 1d673950 2022-03-03 op if ((lockfd = ctl_get_lock(lockfile)) < 0) {
822 1d673950 2022-03-03 op log_debug("didn't get the lock (%d)", lockfd);
824 1d673950 2022-03-03 op free(lockfile);
825 1d673950 2022-03-03 op lockfile = NULL;
827 1d673950 2022-03-03 op if (lockfd == -1)
832 1d673950 2022-03-03 op * Always retry at least once, even if we got
833 1d673950 2022-03-03 op * the lock, because another client could have
834 1d673950 2022-03-03 op * taken the lock, started the server and released
835 1d673950 2022-03-03 op * the lock between our connect() and flock()
841 1d673950 2022-03-03 op if (!spawned) {
842 1d673950 2022-03-03 op log_debug("spawning the daemon");
843 1d673950 2022-03-03 op spawn_daemon();
847 1d673950 2022-03-03 op nanosleep(&ts, NULL);
851 1d673950 2022-03-03 op if (locked && lockfd >= 0) {
852 1d673950 2022-03-03 op unlink(lockfile);
853 1d673950 2022-03-03 op free(lockfile);
854 1d673950 2022-03-03 op close(lockfd);
859 1d673950 2022-03-03 op if (locked) {
860 1d673950 2022-03-03 op free(lockfile);
861 1d673950 2022-03-03 op close(lockfd);
868 fea541a8 2022-02-16 op ctl(int argc, char **argv)
870 1d673950 2022-03-03 op int ctl_sock;
872 fea541a8 2022-02-16 op log_init(1, LOG_DAEMON);
873 fea541a8 2022-02-16 op log_setverbose(verbose);
875 96233476 2022-06-09 op if (getcwd(cwd, sizeof(cwd)) == NULL)
876 96233476 2022-06-09 op fatal("getcwd");
878 1d673950 2022-03-03 op if ((ctl_sock = ctl_connect()) == -1)
879 1d673950 2022-03-03 op fatal("can't connect");
881 fea541a8 2022-02-16 op if (ctl_sock == -1)
882 fea541a8 2022-02-16 op fatalx("failed to connect to the daemon");
884 3baa2617 2022-02-16 op ibuf = xmalloc(sizeof(*ibuf));
885 3baa2617 2022-02-16 op imsg_init(ibuf, ctl_sock);
887 3baa2617 2022-02-16 op optreset = 1;
890 3baa2617 2022-02-16 op exit(parse(argc, argv));