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 3baa2617 2022-02-16 op int ctl_noarg(struct parse_result *, int, char **);
45 3baa2617 2022-02-16 op int ctl_add(struct parse_result *, int, char **);
46 8e916714 2022-02-17 op int ctl_show(struct parse_result *, int, char **);
47 14be015f 2022-02-17 op int ctl_load(struct parse_result *, int, char **);
48 a913de21 2022-02-17 op int ctl_jump(struct parse_result *, int, char **);
49 310ef57c 2022-02-19 op int ctl_repeat(struct parse_result *, int, char **);
50 90122a37 2022-05-14 op int ctl_monitor(struct parse_result *, int, char **);
52 3baa2617 2022-02-16 op struct ctl_command ctl_commands[] = {
53 3baa2617 2022-02-16 op { "play", PLAY, ctl_noarg, "" },
54 3baa2617 2022-02-16 op { "pause", PAUSE, ctl_noarg, "" },
55 3baa2617 2022-02-16 op { "toggle", TOGGLE, ctl_noarg, "" },
56 3baa2617 2022-02-16 op { "stop", STOP, ctl_noarg, "" },
57 3baa2617 2022-02-16 op { "restart", RESTART, ctl_noarg, "" },
58 a830518b 2022-06-09 op { "add", ADD, ctl_add, "files..." },
59 3baa2617 2022-02-16 op { "flush", FLUSH, ctl_noarg, "" },
60 714abf92 2022-02-19 op { "show", SHOW, ctl_show, "[-p]" },
61 bb3f279f 2022-02-16 op { "status", STATUS, ctl_noarg, "" },
62 af27e631 2022-02-17 op { "next", NEXT, ctl_noarg, "" },
63 af27e631 2022-02-17 op { "prev", PREV, ctl_noarg, "" },
64 14be015f 2022-02-17 op { "load", LOAD, ctl_load, "[file]", 1 },
65 a913de21 2022-02-17 op { "jump", JUMP, ctl_jump, "pattern" },
66 310ef57c 2022-02-19 op { "repeat", REPEAT, ctl_repeat, "one|all on|off" },
67 90122a37 2022-05-14 op { "monitor", MONITOR, ctl_monitor, "[events]" },
74 3baa2617 2022-02-16 op fprintf(stderr, "usage: %s [-dv] [-s socket]\n", getprogname());
78 3baa2617 2022-02-16 op static __dead void
79 3baa2617 2022-02-16 op ctl_usage(struct ctl_command *ctl)
81 3baa2617 2022-02-16 op fprintf(stderr, "usage: %s [-v] [-s socket] %s %s\n", getprogname(),
82 3baa2617 2022-02-16 op ctl->name, ctl->usage);
86 96233476 2022-06-09 op /* based on canonpath from kern_pledge.c */
88 96233476 2022-06-09 op canonpath(const char *input, char *buf, size_t bufsize)
90 96233476 2022-06-09 op const char *p;
91 96233476 2022-06-09 op char *q, path[PATH_MAX];
93 96233476 2022-06-09 op if (input[0] != '/') {
94 96233476 2022-06-09 op if (snprintf(path, sizeof(path), "%s/%s", cwd, input)
95 96233476 2022-06-09 op >= sizeof(path)) {
96 96233476 2022-06-09 op errno = ENAMETOOLONG;
104 96233476 2022-06-09 op while (*p && (q - buf < bufsize)) {
105 96233476 2022-06-09 op if (p[0] == '/' && (p[1] == '/' || p[1] == '\0')) {
108 96233476 2022-06-09 op } else if (p[0] == '/' && p[1] == '.' &&
109 96233476 2022-06-09 op (p[2] == '/' || p[2] == '\0')) {
112 96233476 2022-06-09 op } else if (p[0] == '/' && p[1] == '.' && p[2] == '.' &&
113 96233476 2022-06-09 op (p[3] == '/' || p[3] == '\0')) {
115 96233476 2022-06-09 op if (q != buf) /* "/../" at start of buf */
116 96233476 2022-06-09 op while (*--q != '/')
123 96233476 2022-06-09 op if ((*p == '\0') && (q - buf < bufsize)) {
127 96233476 2022-06-09 op errno = ENAMETOOLONG;
133 3baa2617 2022-02-16 op parse(int argc, char **argv)
135 3baa2617 2022-02-16 op struct ctl_command *ctl = NULL;
136 3baa2617 2022-02-16 op struct parse_result res;
137 1d673950 2022-03-03 op const char *argv0;
138 3baa2617 2022-02-16 op int i, status;
140 3baa2617 2022-02-16 op memset(&res, 0, sizeof(res));
142 1d673950 2022-03-03 op if ((argv0 = argv[0]) == NULL)
143 1d673950 2022-03-03 op argv0 = "status";
145 3baa2617 2022-02-16 op for (i = 0; ctl_commands[i].name != NULL; ++i) {
146 1d673950 2022-03-03 op if (strncmp(ctl_commands[i].name, argv0, strlen(argv0))
148 3baa2617 2022-02-16 op if (ctl != NULL) {
149 3baa2617 2022-02-16 op fprintf(stderr,
150 1d673950 2022-03-03 op "ambiguous argument: %s\n", argv0);
153 3baa2617 2022-02-16 op ctl = &ctl_commands[i];
157 3baa2617 2022-02-16 op if (ctl == NULL) {
158 3baa2617 2022-02-16 op fprintf(stderr, "unknown argument: %s\n", argv[0]);
162 3baa2617 2022-02-16 op res.action = ctl->action;
163 3baa2617 2022-02-16 op res.ctl = ctl;
165 3baa2617 2022-02-16 op if (!ctl->has_pledge) {
166 3baa2617 2022-02-16 op /* pledge(2) default if command doesn't have its own */
167 3baa2617 2022-02-16 op if (pledge("stdio", NULL) == -1)
168 3baa2617 2022-02-16 op fatal("pledge");
171 3baa2617 2022-02-16 op status = ctl->main(&res, argc, argv);
172 3baa2617 2022-02-16 op close(ibuf->fd);
174 3baa2617 2022-02-16 op return status;
178 fad0fb69 2022-05-28 op load_files(struct parse_result *res, int *ret)
181 7a427ecd 2022-02-17 op const char *file;
182 7a427ecd 2022-02-17 op char *line = NULL;
183 96233476 2022-06-09 op char path[PATH_MAX];
184 96233476 2022-06-09 op size_t linesize = 0, i = 0;
185 3af93963 2022-03-02 op ssize_t linelen, curr = -1;
187 ec1fb0c7 2022-02-17 op if (res->file == NULL)
189 ec1fb0c7 2022-02-17 op else if ((f = fopen(res->file, "r")) == NULL) {
190 ec1fb0c7 2022-02-17 op log_warn("can't open %s", res->file);
195 ec1fb0c7 2022-02-17 op while ((linelen = getline(&line, &linesize, f)) != -1) {
196 7a427ecd 2022-02-17 op if (linelen == 0)
198 7a427ecd 2022-02-17 op line[linelen-1] = '\0';
200 1dd3b054 2022-03-25 op if (!strncmp(file, "> ", 2)) {
203 1dd3b054 2022-03-25 op } else if (!strncmp(file, " ", 2))
206 96233476 2022-06-09 op if (canonpath(file, path, sizeof(path)) == -1) {
207 96233476 2022-06-09 op log_warnx("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 3baa2617 2022-02-16 op ctlaction(struct parse_result *res)
282 fad0fb69 2022-05-28 op char path[PATH_MAX];
283 3baa2617 2022-02-16 op struct imsg imsg;
284 fad0fb69 2022-05-28 op struct player_status ps;
285 fad0fb69 2022-05-28 op size_t datalen;
287 fad0fb69 2022-05-28 op int i, type, ret = 0, done = 1;
289 3baa2617 2022-02-16 op switch (res->action) {
291 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
292 00f500ff 2022-02-19 op if (verbose) {
293 00f500ff 2022-02-19 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
299 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
302 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
303 00f500ff 2022-02-19 op if (verbose) {
304 00f500ff 2022-02-19 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
310 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
312 3baa2617 2022-02-16 op case RESTART:
313 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_RESTART, 0, 0, -1, NULL, 0);
314 00f500ff 2022-02-19 op if (verbose) {
315 00f500ff 2022-02-19 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
322 d903ec9a 2022-05-29 op for (i = 0; res->files[i] != NULL; ++i) {
323 ef593b43 2022-06-09 op if (canonpath(res->files[i], path, sizeof(path))
325 ef593b43 2022-06-09 op log_warn("canonpath %s", res->files[i]);
329 fad0fb69 2022-05-28 op imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
330 fad0fb69 2022-05-28 op path, sizeof(path));
332 fad0fb69 2022-05-28 op ret = i == 0;
335 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
339 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
343 bb3f279f 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
346 af27e631 2022-02-17 op imsg_compose(ibuf, IMSG_CTL_NEXT, 0, 0, -1, NULL, 0);
347 00f500ff 2022-02-19 op if (verbose) {
348 00f500ff 2022-02-19 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
354 af27e631 2022-02-17 op imsg_compose(ibuf, IMSG_CTL_PREV, 0, 0, -1, NULL, 0);
355 00f500ff 2022-02-19 op if (verbose) {
356 00f500ff 2022-02-19 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
363 7a427ecd 2022-02-17 op imsg_compose(ibuf, IMSG_CTL_BEGIN, 0, 0, -1, NULL, 0);
367 fad0fb69 2022-05-28 op memset(path, 0, sizeof(path));
368 fad0fb69 2022-05-28 op strlcpy(path, res->file, sizeof(path));
369 fad0fb69 2022-05-28 op imsg_compose(ibuf, IMSG_CTL_JUMP, 0, 0, -1,
370 fad0fb69 2022-05-28 op path, sizeof(path));
373 310ef57c 2022-02-19 op imsg_compose(ibuf, IMSG_CTL_REPEAT, 0, 0, -1,
374 310ef57c 2022-02-19 op &res->rep, sizeof(res->rep));
376 6b47a39f 2022-02-21 op case MONITOR:
378 6b47a39f 2022-02-21 op imsg_compose(ibuf, IMSG_CTL_MONITOR, 0, 0, -1,
382 3baa2617 2022-02-16 op /* action not expected */
383 3baa2617 2022-02-16 op fatalx("invalid action %u", res->action);
387 3baa2617 2022-02-16 op if (ret != 0)
390 3baa2617 2022-02-16 op imsg_flush(ibuf);
393 3baa2617 2022-02-16 op while (!done) {
394 3baa2617 2022-02-16 op if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
395 3baa2617 2022-02-16 op fatalx("imsg_read error");
397 3baa2617 2022-02-16 op fatalx("pipe closed");
399 3baa2617 2022-02-16 op while (!done) {
400 3baa2617 2022-02-16 op if ((n = imsg_get(ibuf, &imsg)) == -1)
401 3baa2617 2022-02-16 op fatalx("imsg_get error");
405 fad0fb69 2022-05-28 op if (imsg.hdr.type == IMSG_CTL_ERR) {
406 fad0fb69 2022-05-28 op log_warnx("%s: %s", res->ctl->name,
407 fad0fb69 2022-05-28 op imsg_strerror(&imsg));
413 fad0fb69 2022-05-28 op datalen = IMSG_DATA_SIZE(imsg);
415 3baa2617 2022-02-16 op switch (res->action) {
417 d903ec9a 2022-05-29 op if (res->files[i] == NULL)
418 fad0fb69 2022-05-28 op fatalx("received more replies than "
419 fad0fb69 2022-05-28 op "files enqueued.");
421 fad0fb69 2022-05-28 op if (imsg.hdr.type == IMSG_CTL_ADD)
422 d903ec9a 2022-05-29 op log_debug("enqueued %s", res->files[i]);
424 fad0fb69 2022-05-28 op fatalx("invalid message %d",
425 fad0fb69 2022-05-28 op imsg.hdr.type);
427 d903ec9a 2022-05-29 op done = res->files[i] == NULL;
430 fad0fb69 2022-05-28 op if (datalen == 0) {
434 fad0fb69 2022-05-28 op if (datalen != sizeof(ps))
435 fad0fb69 2022-05-28 op fatalx("data size mismatch");
436 fad0fb69 2022-05-28 op memcpy(&ps, imsg.data, sizeof(ps));
437 fad0fb69 2022-05-28 op if (ps.path[sizeof(ps.path) - 1] != '\0')
438 fad0fb69 2022-05-28 op fatalx("received corrupted data");
439 fad0fb69 2022-05-28 op if (res->pretty) {
440 fad0fb69 2022-05-28 op char c = ' ';
441 fad0fb69 2022-05-28 op if (ps.status == STATE_PLAYING)
443 fad0fb69 2022-05-28 op printf("%c ", c);
445 fad0fb69 2022-05-28 op puts(ps.path);
449 146307bd 2022-02-17 op case RESTART:
454 fad0fb69 2022-05-28 op if (imsg.hdr.type != IMSG_CTL_STATUS)
455 fad0fb69 2022-05-28 op fatalx("invalid message %d",
456 fad0fb69 2022-05-28 op imsg.hdr.type);
458 fad0fb69 2022-05-28 op if (datalen != sizeof(ps))
459 fad0fb69 2022-05-28 op fatalx("data size mismatch");
460 fad0fb69 2022-05-28 op memcpy(&ps, imsg.data, sizeof(ps));
461 fad0fb69 2022-05-28 op if (ps.path[sizeof(ps.path) - 1] != '\0')
462 fad0fb69 2022-05-28 op fatalx("received corrupted data");
464 fad0fb69 2022-05-28 op if (ps.status == STATE_STOPPED)
465 fad0fb69 2022-05-28 op printf("stopped ");
466 fad0fb69 2022-05-28 op else if (ps.status == STATE_PLAYING)
467 fad0fb69 2022-05-28 op printf("playing ");
468 fad0fb69 2022-05-28 op else if (ps.status == STATE_PAUSED)
469 fad0fb69 2022-05-28 op printf("paused ");
471 fad0fb69 2022-05-28 op printf("unknown ");
473 fad0fb69 2022-05-28 op puts(ps.path);
474 fad0fb69 2022-05-28 op printf("repat one %s\nrepeat all %s\n",
475 fad0fb69 2022-05-28 op ps.rp.repeat_one ? "on" : "off",
476 fad0fb69 2022-05-28 op ps.rp.repeat_all ? "on" : "off");
481 fad0fb69 2022-05-28 op if (imsg.hdr.type == IMSG_CTL_ADD)
483 fad0fb69 2022-05-28 op if (imsg.hdr.type == IMSG_CTL_COMMIT) {
488 fad0fb69 2022-05-28 op if (imsg.hdr.type != IMSG_CTL_BEGIN)
489 fad0fb69 2022-05-28 op fatalx("invalid message %d",
490 fad0fb69 2022-05-28 op imsg.hdr.type);
492 fad0fb69 2022-05-28 op load_files(res, &ret);
494 6b47a39f 2022-02-21 op case MONITOR:
495 fad0fb69 2022-05-28 op if (imsg.hdr.type != IMSG_CTL_MONITOR)
496 fad0fb69 2022-05-28 op fatalx("invalid message %d",
497 fad0fb69 2022-05-28 op imsg.hdr.type);
499 fad0fb69 2022-05-28 op if (datalen != sizeof(type))
500 fad0fb69 2022-05-28 op fatalx("data size mismatch");
502 fad0fb69 2022-05-28 op memcpy(&type, imsg.data, sizeof(type));
503 fad0fb69 2022-05-28 op if (type < 0 || type > IMSG__LAST)
504 fad0fb69 2022-05-28 op fatalx("received corrupted data");
506 fad0fb69 2022-05-28 op if (!res->monitor[type])
509 fad0fb69 2022-05-28 op puts(imsg_name(type));
510 fad0fb69 2022-05-28 op fflush(stdout);
517 3baa2617 2022-02-16 op imsg_free(&imsg);
526 3baa2617 2022-02-16 op ctl_noarg(struct parse_result *res, int argc, char **argv)
530 7e29fc4a 2022-03-03 op while ((ch = getopt(argc, argv, "")) != -1)
531 7e29fc4a 2022-03-03 op ctl_usage(res->ctl);
532 7e29fc4a 2022-03-03 op argc -= optind;
533 7e29fc4a 2022-03-03 op argv += optind;
535 acaf7eb2 2022-03-05 op if (argc > 0)
536 3baa2617 2022-02-16 op ctl_usage(res->ctl);
538 3baa2617 2022-02-16 op return ctlaction(res);
542 3baa2617 2022-02-16 op ctl_add(struct parse_result *res, int argc, char **argv)
546 7e29fc4a 2022-03-03 op while ((ch = getopt(argc, argv, "")) != -1)
547 3baa2617 2022-02-16 op ctl_usage(res->ctl);
548 7e29fc4a 2022-03-03 op argc -= optind;
549 7e29fc4a 2022-03-03 op argv += optind;
551 7e29fc4a 2022-03-03 op if (argc == 0)
552 7e29fc4a 2022-03-03 op ctl_usage(res->ctl);
553 7e29fc4a 2022-03-03 op res->files = argv;
555 3baa2617 2022-02-16 op return ctlaction(res);
559 8e916714 2022-02-17 op ctl_show(struct parse_result *res, int argc, char **argv)
563 8e916714 2022-02-17 op while ((ch = getopt(argc, argv, "p")) != -1) {
564 8e916714 2022-02-17 op switch (ch) {
566 8e916714 2022-02-17 op res->pretty = 1;
569 8e916714 2022-02-17 op ctl_usage(res->ctl);
573 8e916714 2022-02-17 op return ctlaction(res);
577 14be015f 2022-02-17 op ctl_load(struct parse_result *res, int argc, char **argv)
581 7e29fc4a 2022-03-03 op while ((ch = getopt(argc, argv, "")) != -1)
582 7e29fc4a 2022-03-03 op ctl_usage(res->ctl);
583 7e29fc4a 2022-03-03 op argc -= optind;
584 7e29fc4a 2022-03-03 op argv += optind;
586 7e29fc4a 2022-03-03 op if (argc == 0)
587 ec1fb0c7 2022-02-17 op res->file = NULL;
588 7e29fc4a 2022-03-03 op else if (argc == 1)
589 7e29fc4a 2022-03-03 op res->file = argv[0];
591 14be015f 2022-02-17 op ctl_usage(res->ctl);
593 14be015f 2022-02-17 op if (pledge("stdio rpath", NULL) == -1)
594 14be015f 2022-02-17 op fatal("pledge");
596 7a427ecd 2022-02-17 op return ctlaction(res);
600 a913de21 2022-02-17 op ctl_jump(struct parse_result *res, int argc, char **argv)
604 a913de21 2022-02-17 op while ((ch = getopt(argc, argv, "")) != -1)
605 a913de21 2022-02-17 op ctl_usage(res->ctl);
606 a913de21 2022-02-17 op argc -= optind;
607 a913de21 2022-02-17 op argv += optind;
609 a913de21 2022-02-17 op if (argc != 1)
610 a913de21 2022-02-17 op ctl_usage(res->ctl);
612 a913de21 2022-02-17 op res->file = argv[0];
613 310ef57c 2022-02-19 op return ctlaction(res);
617 310ef57c 2022-02-19 op ctl_repeat(struct parse_result *res, int argc, char **argv)
621 310ef57c 2022-02-19 op while ((ch = getopt(argc, argv, "")) != -1)
622 310ef57c 2022-02-19 op ctl_usage(res->ctl);
623 310ef57c 2022-02-19 op argc -= optind;
624 310ef57c 2022-02-19 op argv += optind;
626 310ef57c 2022-02-19 op if (argc != 2)
627 310ef57c 2022-02-19 op ctl_usage(res->ctl);
629 310ef57c 2022-02-19 op if (!strcmp(argv[1], "on"))
631 310ef57c 2022-02-19 op else if (!strcmp(argv[1], "off"))
634 310ef57c 2022-02-19 op ctl_usage(res->ctl);
636 310ef57c 2022-02-19 op res->rep.repeat_one = -1;
637 310ef57c 2022-02-19 op res->rep.repeat_all = -1;
638 310ef57c 2022-02-19 op if (!strcmp(argv[0], "one"))
639 310ef57c 2022-02-19 op res->rep.repeat_one = b;
640 310ef57c 2022-02-19 op else if (!strcmp(argv[0], "all"))
641 310ef57c 2022-02-19 op res->rep.repeat_all = b;
643 310ef57c 2022-02-19 op ctl_usage(res->ctl);
645 90122a37 2022-05-14 op return ctlaction(res);
649 90122a37 2022-05-14 op ctl_monitor(struct parse_result *res, int argc, char **argv)
652 90122a37 2022-05-14 op const char *events;
653 90122a37 2022-05-14 op char *dup, *tmp, *tok;
655 90122a37 2022-05-14 op while ((ch = getopt(argc, argv, "")) != -1)
656 90122a37 2022-05-14 op ctl_usage(res->ctl);
657 90122a37 2022-05-14 op argc -= optind;
658 90122a37 2022-05-14 op argv += optind;
660 90122a37 2022-05-14 op if (argc > 1)
661 90122a37 2022-05-14 op ctl_usage(res->ctl);
663 90122a37 2022-05-14 op if (argc == 1)
664 90122a37 2022-05-14 op events = *argv;
666 90122a37 2022-05-14 op events = "play,toggle,pause,stop,restart,flush,next,prev,"
667 90122a37 2022-05-14 op "jump,repeat,add,load";
669 90122a37 2022-05-14 op tmp = dup = xstrdup(events);
670 90122a37 2022-05-14 op while ((tok = strsep(&tmp, ",")) != NULL) {
671 90122a37 2022-05-14 op if (*tok == '\0')
674 90122a37 2022-05-14 op if (!strcmp(tok, "play"))
675 90122a37 2022-05-14 op res->monitor[IMSG_CTL_PLAY] = 1;
676 90122a37 2022-05-14 op else if (!strcmp(tok, "toggle"))
677 90122a37 2022-05-14 op res->monitor[IMSG_CTL_TOGGLE_PLAY] = 1;
678 90122a37 2022-05-14 op else if (!strcmp(tok, "pause"))
679 90122a37 2022-05-14 op res->monitor[IMSG_CTL_PAUSE] = 1;
680 90122a37 2022-05-14 op else if (!strcmp(tok, "stop"))
681 90122a37 2022-05-14 op res->monitor[IMSG_CTL_STOP] = 1;
682 90122a37 2022-05-14 op else if (!strcmp(tok, "restart"))
683 90122a37 2022-05-14 op res->monitor[IMSG_CTL_RESTART] = 1;
684 90122a37 2022-05-14 op else if (!strcmp(tok, "flush"))
685 90122a37 2022-05-14 op res->monitor[IMSG_CTL_FLUSH] = 1;
686 90122a37 2022-05-14 op else if (!strcmp(tok, "next"))
687 90122a37 2022-05-14 op res->monitor[IMSG_CTL_NEXT] = 1;
688 90122a37 2022-05-14 op else if (!strcmp(tok, "prev"))
689 90122a37 2022-05-14 op res->monitor[IMSG_CTL_PREV] = 1;
690 90122a37 2022-05-14 op else if (!strcmp(tok, "jump"))
691 90122a37 2022-05-14 op res->monitor[IMSG_CTL_JUMP] = 1;
692 90122a37 2022-05-14 op else if (!strcmp(tok, "repeat"))
693 90122a37 2022-05-14 op res->monitor[IMSG_CTL_REPEAT] = 1;
694 90122a37 2022-05-14 op else if (!strcmp(tok, "add"))
695 90122a37 2022-05-14 op res->monitor[IMSG_CTL_ADD] = 1;
696 90122a37 2022-05-14 op else if (!strcmp(tok, "load"))
697 90122a37 2022-05-14 op res->monitor[IMSG_CTL_COMMIT] = 1;
699 90122a37 2022-05-14 op fatalx("unknown event \"%s\"", tok);
703 a913de21 2022-02-17 op return ctlaction(res);
707 1d673950 2022-03-03 op ctl_get_lock(const char *lockfile)
711 1d673950 2022-03-03 op if ((lockfd = open(lockfile, O_WRONLY|O_CREAT, 0600)) == -1) {
712 1d673950 2022-03-03 op log_debug("open failed: %s", strerror(errno));
716 1d673950 2022-03-03 op if (flock(lockfd, LOCK_EX|LOCK_NB) == -1) {
717 1d673950 2022-03-03 op log_debug("flock failed: %s", strerror(errno));
718 1d673950 2022-03-03 op if (errno != EAGAIN)
720 1d673950 2022-03-03 op while (flock(lockfd, LOCK_EX) == -1 && errno == EINTR)
722 1d673950 2022-03-03 op close(lockfd);
725 1d673950 2022-03-03 op log_debug("flock succeeded");
727 1d673950 2022-03-03 op return lockfd;
731 1d673950 2022-03-03 op ctl_connect(void)
733 1d673950 2022-03-03 op struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
734 1d673950 2022-03-03 op struct sockaddr_un sa;
736 1d673950 2022-03-03 op int fd, lockfd = -1, locked = 0, spawned = 0;
737 1d673950 2022-03-03 op char *lockfile = NULL;
739 1d673950 2022-03-03 op memset(&sa, 0, sizeof(sa));
740 1d673950 2022-03-03 op sa.sun_family = AF_UNIX;
741 1d673950 2022-03-03 op size = strlcpy(sa.sun_path, csock, sizeof(sa.sun_path));
742 1d673950 2022-03-03 op if (size >= sizeof(sa.sun_path)) {
743 1d673950 2022-03-03 op errno = ENAMETOOLONG;
748 1d673950 2022-03-03 op if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
751 1d673950 2022-03-03 op if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
752 1d673950 2022-03-03 op log_debug("connection failed: %s", strerror(errno));
753 1d673950 2022-03-03 op if (errno != ECONNREFUSED && errno != ENOENT)
757 1d673950 2022-03-03 op if (!locked) {
758 1d673950 2022-03-03 op xasprintf(&lockfile, "%s.lock", csock);
759 1d673950 2022-03-03 op if ((lockfd = ctl_get_lock(lockfile)) < 0) {
760 1d673950 2022-03-03 op log_debug("didn't get the lock (%d)", lockfd);
762 1d673950 2022-03-03 op free(lockfile);
763 1d673950 2022-03-03 op lockfile = NULL;
765 1d673950 2022-03-03 op if (lockfd == -1)
770 1d673950 2022-03-03 op * Always retry at least once, even if we got
771 1d673950 2022-03-03 op * the lock, because another client could have
772 1d673950 2022-03-03 op * taken the lock, started the server and released
773 1d673950 2022-03-03 op * the lock between our connect() and flock()
779 1d673950 2022-03-03 op if (!spawned) {
780 1d673950 2022-03-03 op log_debug("spawning the daemon");
781 1d673950 2022-03-03 op spawn_daemon();
785 1d673950 2022-03-03 op nanosleep(&ts, NULL);
789 1d673950 2022-03-03 op if (locked && lockfd >= 0) {
790 1d673950 2022-03-03 op unlink(lockfile);
791 1d673950 2022-03-03 op free(lockfile);
792 1d673950 2022-03-03 op close(lockfd);
797 1d673950 2022-03-03 op if (locked) {
798 1d673950 2022-03-03 op free(lockfile);
799 1d673950 2022-03-03 op close(lockfd);
806 fea541a8 2022-02-16 op ctl(int argc, char **argv)
808 1d673950 2022-03-03 op int ctl_sock;
810 fea541a8 2022-02-16 op log_init(1, LOG_DAEMON);
811 fea541a8 2022-02-16 op log_setverbose(verbose);
813 96233476 2022-06-09 op if (getcwd(cwd, sizeof(cwd)) == NULL)
814 96233476 2022-06-09 op fatal("getcwd");
816 1d673950 2022-03-03 op if ((ctl_sock = ctl_connect()) == -1)
817 1d673950 2022-03-03 op fatal("can't connect");
819 fea541a8 2022-02-16 op if (ctl_sock == -1)
820 fea541a8 2022-02-16 op fatalx("failed to connect to the daemon");
822 3baa2617 2022-02-16 op ibuf = xmalloc(sizeof(*ibuf));
823 3baa2617 2022-02-16 op imsg_init(ibuf, ctl_sock);
825 3baa2617 2022-02-16 op optreset = 1;
828 3baa2617 2022-02-16 op exit(parse(argc, argv));