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 071167ed 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_FLUSH:
259 fad0fb69 2022-05-28 op return "flush";
260 6b47a39f 2022-02-21 op case IMSG_CTL_NEXT:
261 fad0fb69 2022-05-28 op return "next";
262 6b47a39f 2022-02-21 op case IMSG_CTL_PREV:
263 fad0fb69 2022-05-28 op return "prev";
264 6b47a39f 2022-02-21 op case IMSG_CTL_JUMP:
265 fad0fb69 2022-05-28 op return "jump";
266 6b47a39f 2022-02-21 op case IMSG_CTL_REPEAT:
267 fad0fb69 2022-05-28 op return "repeat";
268 6b47a39f 2022-02-21 op case IMSG_CTL_ADD:
269 fad0fb69 2022-05-28 op return "add";
270 6b47a39f 2022-02-21 op case IMSG_CTL_COMMIT:
271 fad0fb69 2022-05-28 op return "load";
273 fad0fb69 2022-05-28 op return "unknown";
278 bdc8c4e0 2022-07-08 op print_time(const char *label, int64_t seconds)
280 bdc8c4e0 2022-07-08 op int hours, minutes;
282 bdc8c4e0 2022-07-08 op if (seconds < 0)
285 bdc8c4e0 2022-07-08 op hours = seconds / 3600;
286 bdc8c4e0 2022-07-08 op seconds -= hours * 3600;
288 bdc8c4e0 2022-07-08 op minutes = seconds / 60;
289 bdc8c4e0 2022-07-08 op seconds -= minutes * 60;
291 bdc8c4e0 2022-07-08 op printf("%s ", label);
292 bdc8c4e0 2022-07-08 op if (hours != 0)
293 bdc8c4e0 2022-07-08 op printf("%02d:", hours);
294 bdc8c4e0 2022-07-08 op printf("%02d:%02lld\n", minutes, (long long)seconds);
298 3baa2617 2022-02-16 op ctlaction(struct parse_result *res)
300 fad0fb69 2022-05-28 op char path[PATH_MAX];
301 3baa2617 2022-02-16 op struct imsg imsg;
302 fad0fb69 2022-05-28 op struct player_status ps;
303 fad0fb69 2022-05-28 op size_t datalen;
305 fad0fb69 2022-05-28 op int i, type, ret = 0, done = 1;
307 3baa2617 2022-02-16 op switch (res->action) {
309 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
310 00f500ff 2022-02-19 op if (verbose) {
311 00f500ff 2022-02-19 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
317 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
320 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
321 00f500ff 2022-02-19 op if (verbose) {
322 00f500ff 2022-02-19 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
328 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
332 d903ec9a 2022-05-29 op for (i = 0; res->files[i] != NULL; ++i) {
333 18223c28 2022-06-13 op memset(path, 0, sizeof(path));
334 ef593b43 2022-06-09 op if (canonpath(res->files[i], path, sizeof(path))
336 ef593b43 2022-06-09 op log_warn("canonpath %s", res->files[i]);
340 fad0fb69 2022-05-28 op imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
341 fad0fb69 2022-05-28 op path, sizeof(path));
343 fad0fb69 2022-05-28 op ret = i == 0;
346 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
350 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
354 bb3f279f 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
357 af27e631 2022-02-17 op imsg_compose(ibuf, IMSG_CTL_NEXT, 0, 0, -1, NULL, 0);
358 00f500ff 2022-02-19 op if (verbose) {
359 00f500ff 2022-02-19 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
365 af27e631 2022-02-17 op imsg_compose(ibuf, IMSG_CTL_PREV, 0, 0, -1, NULL, 0);
366 00f500ff 2022-02-19 op if (verbose) {
367 00f500ff 2022-02-19 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
374 7a427ecd 2022-02-17 op imsg_compose(ibuf, IMSG_CTL_BEGIN, 0, 0, -1, NULL, 0);
378 fad0fb69 2022-05-28 op memset(path, 0, sizeof(path));
379 fad0fb69 2022-05-28 op strlcpy(path, res->file, sizeof(path));
380 fad0fb69 2022-05-28 op imsg_compose(ibuf, IMSG_CTL_JUMP, 0, 0, -1,
381 fad0fb69 2022-05-28 op path, sizeof(path));
384 310ef57c 2022-02-19 op imsg_compose(ibuf, IMSG_CTL_REPEAT, 0, 0, -1,
385 310ef57c 2022-02-19 op &res->rep, sizeof(res->rep));
387 6b47a39f 2022-02-21 op case MONITOR:
389 6b47a39f 2022-02-21 op imsg_compose(ibuf, IMSG_CTL_MONITOR, 0, 0, -1,
392 8d087670 2022-07-09 op case RESTART:
393 8d087670 2022-07-09 op memset(&res->seek, 0, sizeof(res->seek));
394 8d087670 2022-07-09 op /* fallthrough */
396 e5d4e9f7 2022-07-09 op imsg_compose(ibuf, IMSG_CTL_SEEK, 0, 0, -1, &res->seek,
397 e5d4e9f7 2022-07-09 op sizeof(res->seek));
400 3baa2617 2022-02-16 op /* action not expected */
401 3baa2617 2022-02-16 op fatalx("invalid action %u", res->action);
405 3baa2617 2022-02-16 op if (ret != 0)
408 3baa2617 2022-02-16 op imsg_flush(ibuf);
411 3baa2617 2022-02-16 op while (!done) {
412 3baa2617 2022-02-16 op if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
413 3baa2617 2022-02-16 op fatalx("imsg_read error");
415 3baa2617 2022-02-16 op fatalx("pipe closed");
417 3baa2617 2022-02-16 op while (!done) {
418 3baa2617 2022-02-16 op if ((n = imsg_get(ibuf, &imsg)) == -1)
419 3baa2617 2022-02-16 op fatalx("imsg_get error");
423 fad0fb69 2022-05-28 op if (imsg.hdr.type == IMSG_CTL_ERR) {
424 fad0fb69 2022-05-28 op log_warnx("%s: %s", res->ctl->name,
425 fad0fb69 2022-05-28 op imsg_strerror(&imsg));
431 fad0fb69 2022-05-28 op datalen = IMSG_DATA_SIZE(imsg);
433 3baa2617 2022-02-16 op switch (res->action) {
435 d903ec9a 2022-05-29 op if (res->files[i] == NULL)
436 fad0fb69 2022-05-28 op fatalx("received more replies than "
437 fad0fb69 2022-05-28 op "files enqueued.");
439 fad0fb69 2022-05-28 op if (imsg.hdr.type == IMSG_CTL_ADD)
440 d903ec9a 2022-05-29 op log_debug("enqueued %s", res->files[i]);
442 fad0fb69 2022-05-28 op fatalx("invalid message %d",
443 fad0fb69 2022-05-28 op imsg.hdr.type);
445 d903ec9a 2022-05-29 op done = res->files[i] == NULL;
448 fad0fb69 2022-05-28 op if (datalen == 0) {
452 fad0fb69 2022-05-28 op if (datalen != sizeof(ps))
453 fad0fb69 2022-05-28 op fatalx("data size mismatch");
454 fad0fb69 2022-05-28 op memcpy(&ps, imsg.data, sizeof(ps));
455 fad0fb69 2022-05-28 op if (ps.path[sizeof(ps.path) - 1] != '\0')
456 fad0fb69 2022-05-28 op fatalx("received corrupted data");
457 fad0fb69 2022-05-28 op if (res->pretty) {
458 fad0fb69 2022-05-28 op char c = ' ';
459 fad0fb69 2022-05-28 op if (ps.status == STATE_PLAYING)
461 fad0fb69 2022-05-28 op printf("%c ", c);
463 fad0fb69 2022-05-28 op puts(ps.path);
467 146307bd 2022-02-17 op case RESTART:
472 fad0fb69 2022-05-28 op if (imsg.hdr.type != IMSG_CTL_STATUS)
473 fad0fb69 2022-05-28 op fatalx("invalid message %d",
474 fad0fb69 2022-05-28 op imsg.hdr.type);
476 fad0fb69 2022-05-28 op if (datalen != sizeof(ps))
477 fad0fb69 2022-05-28 op fatalx("data size mismatch");
478 fad0fb69 2022-05-28 op memcpy(&ps, imsg.data, sizeof(ps));
479 fad0fb69 2022-05-28 op if (ps.path[sizeof(ps.path) - 1] != '\0')
480 fad0fb69 2022-05-28 op fatalx("received corrupted data");
482 fad0fb69 2022-05-28 op if (ps.status == STATE_STOPPED)
483 fad0fb69 2022-05-28 op printf("stopped ");
484 fad0fb69 2022-05-28 op else if (ps.status == STATE_PLAYING)
485 fad0fb69 2022-05-28 op printf("playing ");
486 fad0fb69 2022-05-28 op else if (ps.status == STATE_PAUSED)
487 fad0fb69 2022-05-28 op printf("paused ");
489 fad0fb69 2022-05-28 op printf("unknown ");
491 fad0fb69 2022-05-28 op puts(ps.path);
493 bdc8c4e0 2022-07-08 op print_time("position", ps.position);
494 bdc8c4e0 2022-07-08 op print_time("duration", ps.duration);
496 dc12484b 2022-07-08 op printf("repeat one %s\nrepeat all %s\n",
497 fad0fb69 2022-05-28 op ps.rp.repeat_one ? "on" : "off",
498 fad0fb69 2022-05-28 op ps.rp.repeat_all ? "on" : "off");
503 fad0fb69 2022-05-28 op if (imsg.hdr.type == IMSG_CTL_ADD)
505 fad0fb69 2022-05-28 op if (imsg.hdr.type == IMSG_CTL_COMMIT) {
510 fad0fb69 2022-05-28 op if (imsg.hdr.type != IMSG_CTL_BEGIN)
511 fad0fb69 2022-05-28 op fatalx("invalid message %d",
512 fad0fb69 2022-05-28 op imsg.hdr.type);
514 fad0fb69 2022-05-28 op load_files(res, &ret);
516 6b47a39f 2022-02-21 op case MONITOR:
517 fad0fb69 2022-05-28 op if (imsg.hdr.type != IMSG_CTL_MONITOR)
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 if (datalen != sizeof(type))
522 fad0fb69 2022-05-28 op fatalx("data size mismatch");
524 fad0fb69 2022-05-28 op memcpy(&type, imsg.data, sizeof(type));
525 fad0fb69 2022-05-28 op if (type < 0 || type > IMSG__LAST)
526 fad0fb69 2022-05-28 op fatalx("received corrupted data");
528 fad0fb69 2022-05-28 op if (!res->monitor[type])
531 fad0fb69 2022-05-28 op puts(imsg_name(type));
532 fad0fb69 2022-05-28 op fflush(stdout);
539 3baa2617 2022-02-16 op imsg_free(&imsg);
548 3baa2617 2022-02-16 op ctl_noarg(struct parse_result *res, int argc, char **argv)
552 7e29fc4a 2022-03-03 op while ((ch = getopt(argc, argv, "")) != -1)
553 7e29fc4a 2022-03-03 op ctl_usage(res->ctl);
554 7e29fc4a 2022-03-03 op argc -= optind;
555 7e29fc4a 2022-03-03 op argv += optind;
557 acaf7eb2 2022-03-05 op if (argc > 0)
558 3baa2617 2022-02-16 op ctl_usage(res->ctl);
560 3baa2617 2022-02-16 op return ctlaction(res);
564 3baa2617 2022-02-16 op ctl_add(struct parse_result *res, int argc, char **argv)
568 7e29fc4a 2022-03-03 op while ((ch = getopt(argc, argv, "")) != -1)
569 3baa2617 2022-02-16 op ctl_usage(res->ctl);
570 7e29fc4a 2022-03-03 op argc -= optind;
571 7e29fc4a 2022-03-03 op argv += optind;
573 7e29fc4a 2022-03-03 op if (argc == 0)
574 7e29fc4a 2022-03-03 op ctl_usage(res->ctl);
575 7e29fc4a 2022-03-03 op res->files = argv;
577 3baa2617 2022-02-16 op return ctlaction(res);
581 8e916714 2022-02-17 op ctl_show(struct parse_result *res, int argc, char **argv)
585 8e916714 2022-02-17 op while ((ch = getopt(argc, argv, "p")) != -1) {
586 8e916714 2022-02-17 op switch (ch) {
588 8e916714 2022-02-17 op res->pretty = 1;
591 8e916714 2022-02-17 op ctl_usage(res->ctl);
595 8e916714 2022-02-17 op return ctlaction(res);
599 14be015f 2022-02-17 op ctl_load(struct parse_result *res, int argc, char **argv)
603 7e29fc4a 2022-03-03 op while ((ch = getopt(argc, argv, "")) != -1)
604 7e29fc4a 2022-03-03 op ctl_usage(res->ctl);
605 7e29fc4a 2022-03-03 op argc -= optind;
606 7e29fc4a 2022-03-03 op argv += optind;
608 7e29fc4a 2022-03-03 op if (argc == 0)
609 ec1fb0c7 2022-02-17 op res->file = NULL;
610 7e29fc4a 2022-03-03 op else if (argc == 1)
611 7e29fc4a 2022-03-03 op res->file = argv[0];
613 14be015f 2022-02-17 op ctl_usage(res->ctl);
615 14be015f 2022-02-17 op if (pledge("stdio rpath", NULL) == -1)
616 14be015f 2022-02-17 op fatal("pledge");
618 7a427ecd 2022-02-17 op return ctlaction(res);
622 a913de21 2022-02-17 op ctl_jump(struct parse_result *res, int argc, char **argv)
626 a913de21 2022-02-17 op while ((ch = getopt(argc, argv, "")) != -1)
627 a913de21 2022-02-17 op ctl_usage(res->ctl);
628 a913de21 2022-02-17 op argc -= optind;
629 a913de21 2022-02-17 op argv += optind;
631 a913de21 2022-02-17 op if (argc != 1)
632 a913de21 2022-02-17 op ctl_usage(res->ctl);
634 a913de21 2022-02-17 op res->file = argv[0];
635 310ef57c 2022-02-19 op return ctlaction(res);
639 310ef57c 2022-02-19 op ctl_repeat(struct parse_result *res, int argc, char **argv)
643 310ef57c 2022-02-19 op while ((ch = getopt(argc, argv, "")) != -1)
644 310ef57c 2022-02-19 op ctl_usage(res->ctl);
645 310ef57c 2022-02-19 op argc -= optind;
646 310ef57c 2022-02-19 op argv += optind;
648 310ef57c 2022-02-19 op if (argc != 2)
649 310ef57c 2022-02-19 op ctl_usage(res->ctl);
651 310ef57c 2022-02-19 op if (!strcmp(argv[1], "on"))
653 310ef57c 2022-02-19 op else if (!strcmp(argv[1], "off"))
656 310ef57c 2022-02-19 op ctl_usage(res->ctl);
658 310ef57c 2022-02-19 op res->rep.repeat_one = -1;
659 310ef57c 2022-02-19 op res->rep.repeat_all = -1;
660 310ef57c 2022-02-19 op if (!strcmp(argv[0], "one"))
661 310ef57c 2022-02-19 op res->rep.repeat_one = b;
662 310ef57c 2022-02-19 op else if (!strcmp(argv[0], "all"))
663 310ef57c 2022-02-19 op res->rep.repeat_all = b;
665 310ef57c 2022-02-19 op ctl_usage(res->ctl);
667 90122a37 2022-05-14 op return ctlaction(res);
671 90122a37 2022-05-14 op ctl_monitor(struct parse_result *res, int argc, char **argv)
674 90122a37 2022-05-14 op const char *events;
675 90122a37 2022-05-14 op char *dup, *tmp, *tok;
677 90122a37 2022-05-14 op while ((ch = getopt(argc, argv, "")) != -1)
678 90122a37 2022-05-14 op ctl_usage(res->ctl);
679 90122a37 2022-05-14 op argc -= optind;
680 90122a37 2022-05-14 op argv += optind;
682 90122a37 2022-05-14 op if (argc > 1)
683 90122a37 2022-05-14 op ctl_usage(res->ctl);
685 90122a37 2022-05-14 op if (argc == 1)
686 90122a37 2022-05-14 op events = *argv;
688 90122a37 2022-05-14 op events = "play,toggle,pause,stop,restart,flush,next,prev,"
689 90122a37 2022-05-14 op "jump,repeat,add,load";
691 90122a37 2022-05-14 op tmp = dup = xstrdup(events);
692 90122a37 2022-05-14 op while ((tok = strsep(&tmp, ",")) != NULL) {
693 90122a37 2022-05-14 op if (*tok == '\0')
696 90122a37 2022-05-14 op if (!strcmp(tok, "play"))
697 90122a37 2022-05-14 op res->monitor[IMSG_CTL_PLAY] = 1;
698 90122a37 2022-05-14 op else if (!strcmp(tok, "toggle"))
699 90122a37 2022-05-14 op res->monitor[IMSG_CTL_TOGGLE_PLAY] = 1;
700 90122a37 2022-05-14 op else if (!strcmp(tok, "pause"))
701 90122a37 2022-05-14 op res->monitor[IMSG_CTL_PAUSE] = 1;
702 90122a37 2022-05-14 op else if (!strcmp(tok, "stop"))
703 90122a37 2022-05-14 op res->monitor[IMSG_CTL_STOP] = 1;
704 8d087670 2022-07-09 op else if (!strcmp(tok, "restart")) /* compat */
705 8d087670 2022-07-09 op res->monitor[IMSG_CTL_SEEK] = 1;
706 90122a37 2022-05-14 op else if (!strcmp(tok, "flush"))
707 90122a37 2022-05-14 op res->monitor[IMSG_CTL_FLUSH] = 1;
708 90122a37 2022-05-14 op else if (!strcmp(tok, "next"))
709 90122a37 2022-05-14 op res->monitor[IMSG_CTL_NEXT] = 1;
710 90122a37 2022-05-14 op else if (!strcmp(tok, "prev"))
711 90122a37 2022-05-14 op res->monitor[IMSG_CTL_PREV] = 1;
712 90122a37 2022-05-14 op else if (!strcmp(tok, "jump"))
713 90122a37 2022-05-14 op res->monitor[IMSG_CTL_JUMP] = 1;
714 90122a37 2022-05-14 op else if (!strcmp(tok, "repeat"))
715 90122a37 2022-05-14 op res->monitor[IMSG_CTL_REPEAT] = 1;
716 90122a37 2022-05-14 op else if (!strcmp(tok, "add"))
717 90122a37 2022-05-14 op res->monitor[IMSG_CTL_ADD] = 1;
718 90122a37 2022-05-14 op else if (!strcmp(tok, "load"))
719 90122a37 2022-05-14 op res->monitor[IMSG_CTL_COMMIT] = 1;
720 88519c7b 2022-07-09 op else if (!strcmp(tok, "seek"))
721 88519c7b 2022-07-09 op res->monitor[IMSG_CTL_SEEK] = 1;
723 90122a37 2022-05-14 op fatalx("unknown event \"%s\"", tok);
727 e5d4e9f7 2022-07-09 op return ctlaction(res);
731 e5d4e9f7 2022-07-09 op ctl_seek(struct parse_result *res, int argc, char **argv)
733 071167ed 2022-07-09 op const char *n;
735 071167ed 2022-07-09 op int hours = 0, minutes = 0, seconds = 0;
736 071167ed 2022-07-09 op int sign = 1;
738 e5d4e9f7 2022-07-09 op if (argc > 0) {
739 e5d4e9f7 2022-07-09 op /* skip the command name */
744 e5d4e9f7 2022-07-09 op if (argc > 0 && !strcmp(*argv, "--")) {
749 e5d4e9f7 2022-07-09 op if (argc != 1)
750 e5d4e9f7 2022-07-09 op ctl_usage(res->ctl);
753 e5d4e9f7 2022-07-09 op if (*n == '-' || *n == '+')
754 e5d4e9f7 2022-07-09 op res->seek.relative = 1;
755 071167ed 2022-07-09 op if (*n == '-') {
760 071167ed 2022-07-09 op seconds = strtol(n, &ep, 10);
761 071167ed 2022-07-09 op if (n[0] == '\0' ||
762 071167ed 2022-07-09 op (*ep != '\0' && *ep != ':' && *ep != '%') ||
763 071167ed 2022-07-09 op (*ep == '%' && ep[1] != '\0'))
764 071167ed 2022-07-09 op fatalx("invalid offset: %s", argv[0]);
765 071167ed 2022-07-09 op if (*ep == '\0' || *ep == '%') {
766 071167ed 2022-07-09 op res->seek.percent = *ep == '%';
771 071167ed 2022-07-09 op minutes = seconds;
772 071167ed 2022-07-09 op seconds = strtol(n, &ep, 10);
773 071167ed 2022-07-09 op if (n[0] == '\0' || (*ep != '\0' && *ep != ':'))
774 071167ed 2022-07-09 op fatalx("invalid offset: %s", argv[0]);
775 071167ed 2022-07-09 op if (*ep == '\0')
779 071167ed 2022-07-09 op hours = minutes;
780 071167ed 2022-07-09 op minutes = seconds;
781 071167ed 2022-07-09 op seconds = strtol(n, &ep, 10);
782 071167ed 2022-07-09 op if (n[0] == '\0' || *ep != '\0')
783 071167ed 2022-07-09 op fatalx("invalid offset: %s", argv[0]);
786 071167ed 2022-07-09 op res->seek.offset = sign * (hours * 3600 + minutes * 60 + seconds);
787 a913de21 2022-02-17 op return ctlaction(res);
791 1d673950 2022-03-03 op ctl_get_lock(const char *lockfile)
795 1d673950 2022-03-03 op if ((lockfd = open(lockfile, O_WRONLY|O_CREAT, 0600)) == -1) {
796 1d673950 2022-03-03 op log_debug("open failed: %s", strerror(errno));
800 1d673950 2022-03-03 op if (flock(lockfd, LOCK_EX|LOCK_NB) == -1) {
801 1d673950 2022-03-03 op log_debug("flock failed: %s", strerror(errno));
802 1d673950 2022-03-03 op if (errno != EAGAIN)
804 1d673950 2022-03-03 op while (flock(lockfd, LOCK_EX) == -1 && errno == EINTR)
806 1d673950 2022-03-03 op close(lockfd);
809 1d673950 2022-03-03 op log_debug("flock succeeded");
811 1d673950 2022-03-03 op return lockfd;
815 1d673950 2022-03-03 op ctl_connect(void)
817 1d673950 2022-03-03 op struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
818 1d673950 2022-03-03 op struct sockaddr_un sa;
820 1d673950 2022-03-03 op int fd, lockfd = -1, locked = 0, spawned = 0;
821 1b77ad48 2022-07-08 op int attempt = 0;
822 1d673950 2022-03-03 op char *lockfile = NULL;
824 1d673950 2022-03-03 op memset(&sa, 0, sizeof(sa));
825 1d673950 2022-03-03 op sa.sun_family = AF_UNIX;
826 1d673950 2022-03-03 op size = strlcpy(sa.sun_path, csock, sizeof(sa.sun_path));
827 1d673950 2022-03-03 op if (size >= sizeof(sa.sun_path)) {
828 1d673950 2022-03-03 op errno = ENAMETOOLONG;
833 1d673950 2022-03-03 op if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
836 1d673950 2022-03-03 op if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
837 1d673950 2022-03-03 op log_debug("connection failed: %s", strerror(errno));
838 1d673950 2022-03-03 op if (errno != ECONNREFUSED && errno != ENOENT)
840 1b77ad48 2022-07-08 op if (attempt++ == 100)
844 1d673950 2022-03-03 op if (!locked) {
845 1d673950 2022-03-03 op xasprintf(&lockfile, "%s.lock", csock);
846 1d673950 2022-03-03 op if ((lockfd = ctl_get_lock(lockfile)) < 0) {
847 1d673950 2022-03-03 op log_debug("didn't get the lock (%d)", lockfd);
849 1d673950 2022-03-03 op free(lockfile);
850 1d673950 2022-03-03 op lockfile = NULL;
852 1d673950 2022-03-03 op if (lockfd == -1)
857 1d673950 2022-03-03 op * Always retry at least once, even if we got
858 1d673950 2022-03-03 op * the lock, because another client could have
859 1d673950 2022-03-03 op * taken the lock, started the server and released
860 1d673950 2022-03-03 op * the lock between our connect() and flock()
866 1d673950 2022-03-03 op if (!spawned) {
867 1d673950 2022-03-03 op log_debug("spawning the daemon");
868 1d673950 2022-03-03 op spawn_daemon();
872 1d673950 2022-03-03 op nanosleep(&ts, NULL);
876 1d673950 2022-03-03 op if (locked && lockfd >= 0) {
877 1d673950 2022-03-03 op unlink(lockfile);
878 1d673950 2022-03-03 op free(lockfile);
879 1d673950 2022-03-03 op close(lockfd);
884 1d673950 2022-03-03 op if (locked) {
885 1d673950 2022-03-03 op free(lockfile);
886 1d673950 2022-03-03 op close(lockfd);
893 fea541a8 2022-02-16 op ctl(int argc, char **argv)
895 1d673950 2022-03-03 op int ctl_sock;
897 fea541a8 2022-02-16 op log_init(1, LOG_DAEMON);
898 fea541a8 2022-02-16 op log_setverbose(verbose);
900 96233476 2022-06-09 op if (getcwd(cwd, sizeof(cwd)) == NULL)
901 96233476 2022-06-09 op fatal("getcwd");
903 1d673950 2022-03-03 op if ((ctl_sock = ctl_connect()) == -1)
904 1d673950 2022-03-03 op fatal("can't connect");
906 fea541a8 2022-02-16 op if (ctl_sock == -1)
907 fea541a8 2022-02-16 op fatalx("failed to connect to the daemon");
909 3baa2617 2022-02-16 op ibuf = xmalloc(sizeof(*ibuf));
910 3baa2617 2022-02-16 op imsg_init(ibuf, ctl_sock);
912 3baa2617 2022-02-16 op optreset = 1;
915 3baa2617 2022-02-16 op exit(parse(argc, argv));