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 **);
49 68e4f29e 2022-07-10 op static int ctl_status(struct parse_result *, int, char **);
51 3baa2617 2022-02-16 op struct ctl_command ctl_commands[] = {
52 d488e5a5 2022-07-08 op { "add", ADD, ctl_add, "files...", 0 },
53 d488e5a5 2022-07-08 op { "flush", FLUSH, ctl_noarg, "", 0 },
54 d488e5a5 2022-07-08 op { "jump", JUMP, ctl_jump, "pattern", 0 },
55 6a128dce 2022-07-08 op { "load", LOAD, ctl_load, "[file]", 1 },
56 d488e5a5 2022-07-08 op { "monitor", MONITOR, ctl_monitor, "[events]", 0 },
57 6a128dce 2022-07-08 op { "next", NEXT, ctl_noarg, "", 0 },
58 6a128dce 2022-07-08 op { "pause", PAUSE, ctl_noarg, "", 0 },
59 6a128dce 2022-07-08 op { "play", PLAY, ctl_noarg, "", 0 },
60 6a128dce 2022-07-08 op { "prev", PREV, ctl_noarg, "", 0 },
61 6a128dce 2022-07-08 op { "repeat", REPEAT, ctl_repeat, "one|all on|off", 0 },
62 6a128dce 2022-07-08 op { "restart", RESTART, ctl_noarg, "", 0 },
63 071167ed 2022-07-09 op { "seek", SEEK, ctl_seek, "[+-]time[%]", 0 },
64 6a128dce 2022-07-08 op { "show", SHOW, ctl_show, "[-p]", 0 },
65 68e4f29e 2022-07-10 op { "status", STATUS, ctl_status, "[-f fmt]", 0 },
66 6a128dce 2022-07-08 op { "stop", STOP, ctl_noarg, "", 0 },
67 6a128dce 2022-07-08 op { "toggle", TOGGLE, ctl_noarg, "", 0 },
68 d488e5a5 2022-07-08 op { NULL, 0, NULL, NULL, 0 },
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 18223c28 2022-06-13 op memset(path, 0, sizeof(path));
207 96233476 2022-06-09 op if (canonpath(file, path, sizeof(path)) == -1) {
208 f1beb69a 2022-06-13 op log_warn("canonpath %s", file);
213 7a427ecd 2022-02-17 op imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
214 7a427ecd 2022-02-17 op path, sizeof(path));
218 ec1fb0c7 2022-02-17 op if (ferror(f))
219 7a427ecd 2022-02-17 op fatal("getline");
222 3af93963 2022-03-02 op if (i == 0) {
227 7a427ecd 2022-02-17 op imsg_compose(ibuf, IMSG_CTL_COMMIT, 0, 0, -1,
228 3af93963 2022-03-02 op &curr, sizeof(curr));
229 7a427ecd 2022-02-17 op imsg_flush(ibuf);
233 fad0fb69 2022-05-28 op static const char *
234 fad0fb69 2022-05-28 op imsg_strerror(struct imsg *imsg)
236 fad0fb69 2022-05-28 op size_t datalen;
237 fad0fb69 2022-05-28 op const char *msg;
239 fad0fb69 2022-05-28 op datalen = IMSG_DATA_SIZE(*imsg);
240 fad0fb69 2022-05-28 op msg = imsg->data;
241 fad0fb69 2022-05-28 op if (datalen == 0 || msg[datalen-1] != '\0')
242 fad0fb69 2022-05-28 op fatalx("malformed error message");
247 fad0fb69 2022-05-28 op static const char *
248 fad0fb69 2022-05-28 op imsg_name(int type)
250 6b47a39f 2022-02-21 op switch (type) {
251 6b47a39f 2022-02-21 op case IMSG_CTL_PLAY:
252 fad0fb69 2022-05-28 op return "play";
253 6b47a39f 2022-02-21 op case IMSG_CTL_TOGGLE_PLAY:
254 fad0fb69 2022-05-28 op return "toggle";
255 6b47a39f 2022-02-21 op case IMSG_CTL_PAUSE:
256 fad0fb69 2022-05-28 op return "pause";
257 6b47a39f 2022-02-21 op case IMSG_CTL_STOP:
258 fad0fb69 2022-05-28 op return "stop";
259 6b47a39f 2022-02-21 op case IMSG_CTL_FLUSH:
260 fad0fb69 2022-05-28 op return "flush";
261 6b47a39f 2022-02-21 op case IMSG_CTL_NEXT:
262 fad0fb69 2022-05-28 op return "next";
263 6b47a39f 2022-02-21 op case IMSG_CTL_PREV:
264 fad0fb69 2022-05-28 op return "prev";
265 6b47a39f 2022-02-21 op case IMSG_CTL_JUMP:
266 fad0fb69 2022-05-28 op return "jump";
267 6b47a39f 2022-02-21 op case IMSG_CTL_REPEAT:
268 fad0fb69 2022-05-28 op return "repeat";
269 6b47a39f 2022-02-21 op case IMSG_CTL_ADD:
270 fad0fb69 2022-05-28 op return "add";
271 6b47a39f 2022-02-21 op case IMSG_CTL_COMMIT:
272 fad0fb69 2022-05-28 op return "load";
274 fad0fb69 2022-05-28 op return "unknown";
279 68e4f29e 2022-07-10 op print_time(const char *label, int64_t seconds, const char *suffx)
281 bdc8c4e0 2022-07-08 op int hours, minutes;
283 bdc8c4e0 2022-07-08 op if (seconds < 0)
286 bdc8c4e0 2022-07-08 op hours = seconds / 3600;
287 bdc8c4e0 2022-07-08 op seconds -= hours * 3600;
289 bdc8c4e0 2022-07-08 op minutes = seconds / 60;
290 bdc8c4e0 2022-07-08 op seconds -= minutes * 60;
292 68e4f29e 2022-07-10 op printf("%s", label);
293 bdc8c4e0 2022-07-08 op if (hours != 0)
294 bdc8c4e0 2022-07-08 op printf("%02d:", hours);
295 68e4f29e 2022-07-10 op printf("%02d:%02lld%s", minutes, (long long)seconds, suffx);
299 68e4f29e 2022-07-10 op print_status(struct player_status *ps, const char *spec)
301 68e4f29e 2022-07-10 op const char *status;
302 68e4f29e 2022-07-10 op double percent;
303 68e4f29e 2022-07-10 op char *dup, *tmp, *tok;
305 68e4f29e 2022-07-10 op if (ps->status == STATE_STOPPED)
306 68e4f29e 2022-07-10 op status = "stopped";
307 68e4f29e 2022-07-10 op else if (ps->status == STATE_PLAYING)
308 68e4f29e 2022-07-10 op status = "playing";
309 68e4f29e 2022-07-10 op else if (ps->status == STATE_PAUSED)
310 68e4f29e 2022-07-10 op status = "paused";
312 68e4f29e 2022-07-10 op status = "unknown";
314 68e4f29e 2022-07-10 op percent = 100.0 * (double)ps->position / (double)ps->duration;
316 68e4f29e 2022-07-10 op tmp = dup = xstrdup(spec);
317 68e4f29e 2022-07-10 op while ((tok = strsep(&tmp, ",")) != NULL) {
318 68e4f29e 2022-07-10 op if (*tok == '\0')
321 68e4f29e 2022-07-10 op if (!strcmp(tok, "path")) {
322 68e4f29e 2022-07-10 op puts(ps->path);
323 68e4f29e 2022-07-10 op } else if (!strcmp(tok, "repeat:oneline")) {
324 68e4f29e 2022-07-10 op printf("repeat one:%s ",
325 68e4f29e 2022-07-10 op ps->rp.repeat_one ? "on" : "off");
326 68e4f29e 2022-07-10 op printf("all:%s\n", ps->rp.repeat_all ? "on" : "off");
327 68e4f29e 2022-07-10 op } else if (!strcmp(tok, "repeat")) {
328 68e4f29e 2022-07-10 op printf("repeat one %s\n",
329 68e4f29e 2022-07-10 op ps->rp.repeat_one ? "on" : "off");
330 68e4f29e 2022-07-10 op printf("repeat all %s\n",
331 68e4f29e 2022-07-10 op ps->rp.repeat_all ? "on" : "off");
332 68e4f29e 2022-07-10 op } else if (!strcmp(tok, "status")) {
333 68e4f29e 2022-07-10 op printf("%s %s\n", status, ps->path);
334 68e4f29e 2022-07-10 op } else if (!strcmp(tok, "time:oneline")) {
335 68e4f29e 2022-07-10 op print_time("time ", ps->position, " / ");
336 68e4f29e 2022-07-10 op print_time("", ps->duration, "\n");
337 68e4f29e 2022-07-10 op } else if (!strcmp(tok, "time:percentage")) {
338 68e4f29e 2022-07-10 op printf("position %.2f%%\n", percent);
339 68e4f29e 2022-07-10 op } else if (!strcmp(tok, "time:raw")) {
340 68e4f29e 2022-07-10 op printf("position %lld\n", (long long)ps->position);
341 68e4f29e 2022-07-10 op printf("duration %lld\n", (long long)ps->duration);
342 68e4f29e 2022-07-10 op } else if (!strcmp(tok, "time")) {
343 68e4f29e 2022-07-10 op print_time("position ", ps->position, "\n");
344 68e4f29e 2022-07-10 op print_time("duration ", ps->duration, "\n");
352 3baa2617 2022-02-16 op ctlaction(struct parse_result *res)
354 fad0fb69 2022-05-28 op char path[PATH_MAX];
355 3baa2617 2022-02-16 op struct imsg imsg;
356 fad0fb69 2022-05-28 op struct player_status ps;
357 fad0fb69 2022-05-28 op size_t datalen;
359 fad0fb69 2022-05-28 op int i, type, ret = 0, done = 1;
361 3baa2617 2022-02-16 op switch (res->action) {
363 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
364 00f500ff 2022-02-19 op if (verbose) {
365 00f500ff 2022-02-19 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
371 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
374 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
375 00f500ff 2022-02-19 op if (verbose) {
376 00f500ff 2022-02-19 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
382 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
386 d903ec9a 2022-05-29 op for (i = 0; res->files[i] != NULL; ++i) {
387 18223c28 2022-06-13 op memset(path, 0, sizeof(path));
388 ef593b43 2022-06-09 op if (canonpath(res->files[i], path, sizeof(path))
390 ef593b43 2022-06-09 op log_warn("canonpath %s", res->files[i]);
394 fad0fb69 2022-05-28 op imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
395 fad0fb69 2022-05-28 op path, sizeof(path));
397 fad0fb69 2022-05-28 op ret = i == 0;
400 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
404 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
408 bb3f279f 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
411 af27e631 2022-02-17 op imsg_compose(ibuf, IMSG_CTL_NEXT, 0, 0, -1, NULL, 0);
412 00f500ff 2022-02-19 op if (verbose) {
413 00f500ff 2022-02-19 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
419 af27e631 2022-02-17 op imsg_compose(ibuf, IMSG_CTL_PREV, 0, 0, -1, NULL, 0);
420 00f500ff 2022-02-19 op if (verbose) {
421 00f500ff 2022-02-19 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
428 7a427ecd 2022-02-17 op imsg_compose(ibuf, IMSG_CTL_BEGIN, 0, 0, -1, NULL, 0);
432 fad0fb69 2022-05-28 op memset(path, 0, sizeof(path));
433 fad0fb69 2022-05-28 op strlcpy(path, res->file, sizeof(path));
434 fad0fb69 2022-05-28 op imsg_compose(ibuf, IMSG_CTL_JUMP, 0, 0, -1,
435 fad0fb69 2022-05-28 op path, sizeof(path));
438 310ef57c 2022-02-19 op imsg_compose(ibuf, IMSG_CTL_REPEAT, 0, 0, -1,
439 310ef57c 2022-02-19 op &res->rep, sizeof(res->rep));
441 6b47a39f 2022-02-21 op case MONITOR:
443 6b47a39f 2022-02-21 op imsg_compose(ibuf, IMSG_CTL_MONITOR, 0, 0, -1,
446 8d087670 2022-07-09 op case RESTART:
447 8d087670 2022-07-09 op memset(&res->seek, 0, sizeof(res->seek));
448 8d087670 2022-07-09 op /* fallthrough */
450 e5d4e9f7 2022-07-09 op imsg_compose(ibuf, IMSG_CTL_SEEK, 0, 0, -1, &res->seek,
451 e5d4e9f7 2022-07-09 op sizeof(res->seek));
454 3baa2617 2022-02-16 op /* action not expected */
455 3baa2617 2022-02-16 op fatalx("invalid action %u", res->action);
459 3baa2617 2022-02-16 op if (ret != 0)
462 3baa2617 2022-02-16 op imsg_flush(ibuf);
465 3baa2617 2022-02-16 op while (!done) {
466 3baa2617 2022-02-16 op if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
467 3baa2617 2022-02-16 op fatalx("imsg_read error");
469 3baa2617 2022-02-16 op fatalx("pipe closed");
471 3baa2617 2022-02-16 op while (!done) {
472 3baa2617 2022-02-16 op if ((n = imsg_get(ibuf, &imsg)) == -1)
473 3baa2617 2022-02-16 op fatalx("imsg_get error");
477 fad0fb69 2022-05-28 op if (imsg.hdr.type == IMSG_CTL_ERR) {
478 fad0fb69 2022-05-28 op log_warnx("%s: %s", res->ctl->name,
479 fad0fb69 2022-05-28 op imsg_strerror(&imsg));
485 fad0fb69 2022-05-28 op datalen = IMSG_DATA_SIZE(imsg);
487 3baa2617 2022-02-16 op switch (res->action) {
489 d903ec9a 2022-05-29 op if (res->files[i] == NULL)
490 fad0fb69 2022-05-28 op fatalx("received more replies than "
491 fad0fb69 2022-05-28 op "files enqueued.");
493 fad0fb69 2022-05-28 op if (imsg.hdr.type == IMSG_CTL_ADD)
494 d903ec9a 2022-05-29 op log_debug("enqueued %s", res->files[i]);
496 fad0fb69 2022-05-28 op fatalx("invalid message %d",
497 fad0fb69 2022-05-28 op imsg.hdr.type);
499 d903ec9a 2022-05-29 op done = res->files[i] == NULL;
502 fad0fb69 2022-05-28 op if (datalen == 0) {
506 fad0fb69 2022-05-28 op if (datalen != sizeof(ps))
507 fad0fb69 2022-05-28 op fatalx("data size mismatch");
508 fad0fb69 2022-05-28 op memcpy(&ps, imsg.data, sizeof(ps));
509 fad0fb69 2022-05-28 op if (ps.path[sizeof(ps.path) - 1] != '\0')
510 fad0fb69 2022-05-28 op fatalx("received corrupted data");
511 fad0fb69 2022-05-28 op if (res->pretty) {
512 fad0fb69 2022-05-28 op char c = ' ';
513 fad0fb69 2022-05-28 op if (ps.status == STATE_PLAYING)
515 fad0fb69 2022-05-28 op printf("%c ", c);
517 fad0fb69 2022-05-28 op puts(ps.path);
521 146307bd 2022-02-17 op case RESTART:
526 fad0fb69 2022-05-28 op if (imsg.hdr.type != IMSG_CTL_STATUS)
527 fad0fb69 2022-05-28 op fatalx("invalid message %d",
528 fad0fb69 2022-05-28 op imsg.hdr.type);
530 fad0fb69 2022-05-28 op if (datalen != sizeof(ps))
531 fad0fb69 2022-05-28 op fatalx("data size mismatch");
532 fad0fb69 2022-05-28 op memcpy(&ps, imsg.data, sizeof(ps));
533 fad0fb69 2022-05-28 op if (ps.path[sizeof(ps.path) - 1] != '\0')
534 fad0fb69 2022-05-28 op fatalx("received corrupted data");
536 68e4f29e 2022-07-10 op print_status(&ps, res->status_format);
540 fad0fb69 2022-05-28 op if (imsg.hdr.type == IMSG_CTL_ADD)
542 fad0fb69 2022-05-28 op if (imsg.hdr.type == IMSG_CTL_COMMIT) {
547 fad0fb69 2022-05-28 op if (imsg.hdr.type != IMSG_CTL_BEGIN)
548 fad0fb69 2022-05-28 op fatalx("invalid message %d",
549 fad0fb69 2022-05-28 op imsg.hdr.type);
551 fad0fb69 2022-05-28 op load_files(res, &ret);
553 6b47a39f 2022-02-21 op case MONITOR:
554 fad0fb69 2022-05-28 op if (imsg.hdr.type != IMSG_CTL_MONITOR)
555 fad0fb69 2022-05-28 op fatalx("invalid message %d",
556 fad0fb69 2022-05-28 op imsg.hdr.type);
558 fad0fb69 2022-05-28 op if (datalen != sizeof(type))
559 fad0fb69 2022-05-28 op fatalx("data size mismatch");
561 fad0fb69 2022-05-28 op memcpy(&type, imsg.data, sizeof(type));
562 fad0fb69 2022-05-28 op if (type < 0 || type > IMSG__LAST)
563 fad0fb69 2022-05-28 op fatalx("received corrupted data");
565 fad0fb69 2022-05-28 op if (!res->monitor[type])
568 fad0fb69 2022-05-28 op puts(imsg_name(type));
569 fad0fb69 2022-05-28 op fflush(stdout);
576 3baa2617 2022-02-16 op imsg_free(&imsg);
585 3baa2617 2022-02-16 op ctl_noarg(struct parse_result *res, int argc, char **argv)
589 7e29fc4a 2022-03-03 op while ((ch = getopt(argc, argv, "")) != -1)
590 7e29fc4a 2022-03-03 op ctl_usage(res->ctl);
591 7e29fc4a 2022-03-03 op argc -= optind;
592 7e29fc4a 2022-03-03 op argv += optind;
594 acaf7eb2 2022-03-05 op if (argc > 0)
595 3baa2617 2022-02-16 op ctl_usage(res->ctl);
597 3baa2617 2022-02-16 op return ctlaction(res);
601 3baa2617 2022-02-16 op ctl_add(struct parse_result *res, int argc, char **argv)
605 7e29fc4a 2022-03-03 op while ((ch = getopt(argc, argv, "")) != -1)
606 3baa2617 2022-02-16 op ctl_usage(res->ctl);
607 7e29fc4a 2022-03-03 op argc -= optind;
608 7e29fc4a 2022-03-03 op argv += optind;
610 7e29fc4a 2022-03-03 op if (argc == 0)
611 7e29fc4a 2022-03-03 op ctl_usage(res->ctl);
612 7e29fc4a 2022-03-03 op res->files = argv;
614 3baa2617 2022-02-16 op return ctlaction(res);
618 8e916714 2022-02-17 op ctl_show(struct parse_result *res, int argc, char **argv)
622 8e916714 2022-02-17 op while ((ch = getopt(argc, argv, "p")) != -1) {
623 8e916714 2022-02-17 op switch (ch) {
625 8e916714 2022-02-17 op res->pretty = 1;
628 8e916714 2022-02-17 op ctl_usage(res->ctl);
632 8e916714 2022-02-17 op return ctlaction(res);
636 14be015f 2022-02-17 op ctl_load(struct parse_result *res, int argc, char **argv)
640 7e29fc4a 2022-03-03 op while ((ch = getopt(argc, argv, "")) != -1)
641 7e29fc4a 2022-03-03 op ctl_usage(res->ctl);
642 7e29fc4a 2022-03-03 op argc -= optind;
643 7e29fc4a 2022-03-03 op argv += optind;
645 7e29fc4a 2022-03-03 op if (argc == 0)
646 ec1fb0c7 2022-02-17 op res->file = NULL;
647 7e29fc4a 2022-03-03 op else if (argc == 1)
648 7e29fc4a 2022-03-03 op res->file = argv[0];
650 14be015f 2022-02-17 op ctl_usage(res->ctl);
652 14be015f 2022-02-17 op if (pledge("stdio rpath", NULL) == -1)
653 14be015f 2022-02-17 op fatal("pledge");
655 7a427ecd 2022-02-17 op return ctlaction(res);
659 a913de21 2022-02-17 op ctl_jump(struct parse_result *res, int argc, char **argv)
663 a913de21 2022-02-17 op while ((ch = getopt(argc, argv, "")) != -1)
664 a913de21 2022-02-17 op ctl_usage(res->ctl);
665 a913de21 2022-02-17 op argc -= optind;
666 a913de21 2022-02-17 op argv += optind;
668 a913de21 2022-02-17 op if (argc != 1)
669 a913de21 2022-02-17 op ctl_usage(res->ctl);
671 a913de21 2022-02-17 op res->file = argv[0];
672 310ef57c 2022-02-19 op return ctlaction(res);
676 310ef57c 2022-02-19 op ctl_repeat(struct parse_result *res, int argc, char **argv)
680 310ef57c 2022-02-19 op while ((ch = getopt(argc, argv, "")) != -1)
681 310ef57c 2022-02-19 op ctl_usage(res->ctl);
682 310ef57c 2022-02-19 op argc -= optind;
683 310ef57c 2022-02-19 op argv += optind;
685 310ef57c 2022-02-19 op if (argc != 2)
686 310ef57c 2022-02-19 op ctl_usage(res->ctl);
688 310ef57c 2022-02-19 op if (!strcmp(argv[1], "on"))
690 310ef57c 2022-02-19 op else if (!strcmp(argv[1], "off"))
693 310ef57c 2022-02-19 op ctl_usage(res->ctl);
695 310ef57c 2022-02-19 op res->rep.repeat_one = -1;
696 310ef57c 2022-02-19 op res->rep.repeat_all = -1;
697 310ef57c 2022-02-19 op if (!strcmp(argv[0], "one"))
698 310ef57c 2022-02-19 op res->rep.repeat_one = b;
699 310ef57c 2022-02-19 op else if (!strcmp(argv[0], "all"))
700 310ef57c 2022-02-19 op res->rep.repeat_all = b;
702 310ef57c 2022-02-19 op ctl_usage(res->ctl);
704 90122a37 2022-05-14 op return ctlaction(res);
708 90122a37 2022-05-14 op ctl_monitor(struct parse_result *res, int argc, char **argv)
711 90122a37 2022-05-14 op const char *events;
712 90122a37 2022-05-14 op char *dup, *tmp, *tok;
714 90122a37 2022-05-14 op while ((ch = getopt(argc, argv, "")) != -1)
715 90122a37 2022-05-14 op ctl_usage(res->ctl);
716 90122a37 2022-05-14 op argc -= optind;
717 90122a37 2022-05-14 op argv += optind;
719 90122a37 2022-05-14 op if (argc > 1)
720 90122a37 2022-05-14 op ctl_usage(res->ctl);
722 90122a37 2022-05-14 op if (argc == 1)
723 90122a37 2022-05-14 op events = *argv;
725 90122a37 2022-05-14 op events = "play,toggle,pause,stop,restart,flush,next,prev,"
726 90122a37 2022-05-14 op "jump,repeat,add,load";
728 90122a37 2022-05-14 op tmp = dup = xstrdup(events);
729 90122a37 2022-05-14 op while ((tok = strsep(&tmp, ",")) != NULL) {
730 90122a37 2022-05-14 op if (*tok == '\0')
733 90122a37 2022-05-14 op if (!strcmp(tok, "play"))
734 90122a37 2022-05-14 op res->monitor[IMSG_CTL_PLAY] = 1;
735 90122a37 2022-05-14 op else if (!strcmp(tok, "toggle"))
736 90122a37 2022-05-14 op res->monitor[IMSG_CTL_TOGGLE_PLAY] = 1;
737 90122a37 2022-05-14 op else if (!strcmp(tok, "pause"))
738 90122a37 2022-05-14 op res->monitor[IMSG_CTL_PAUSE] = 1;
739 90122a37 2022-05-14 op else if (!strcmp(tok, "stop"))
740 90122a37 2022-05-14 op res->monitor[IMSG_CTL_STOP] = 1;
741 8d087670 2022-07-09 op else if (!strcmp(tok, "restart")) /* compat */
742 8d087670 2022-07-09 op res->monitor[IMSG_CTL_SEEK] = 1;
743 90122a37 2022-05-14 op else if (!strcmp(tok, "flush"))
744 90122a37 2022-05-14 op res->monitor[IMSG_CTL_FLUSH] = 1;
745 90122a37 2022-05-14 op else if (!strcmp(tok, "next"))
746 90122a37 2022-05-14 op res->monitor[IMSG_CTL_NEXT] = 1;
747 90122a37 2022-05-14 op else if (!strcmp(tok, "prev"))
748 90122a37 2022-05-14 op res->monitor[IMSG_CTL_PREV] = 1;
749 90122a37 2022-05-14 op else if (!strcmp(tok, "jump"))
750 90122a37 2022-05-14 op res->monitor[IMSG_CTL_JUMP] = 1;
751 90122a37 2022-05-14 op else if (!strcmp(tok, "repeat"))
752 90122a37 2022-05-14 op res->monitor[IMSG_CTL_REPEAT] = 1;
753 90122a37 2022-05-14 op else if (!strcmp(tok, "add"))
754 90122a37 2022-05-14 op res->monitor[IMSG_CTL_ADD] = 1;
755 90122a37 2022-05-14 op else if (!strcmp(tok, "load"))
756 90122a37 2022-05-14 op res->monitor[IMSG_CTL_COMMIT] = 1;
757 88519c7b 2022-07-09 op else if (!strcmp(tok, "seek"))
758 88519c7b 2022-07-09 op res->monitor[IMSG_CTL_SEEK] = 1;
760 90122a37 2022-05-14 op fatalx("unknown event \"%s\"", tok);
764 e5d4e9f7 2022-07-09 op return ctlaction(res);
768 e5d4e9f7 2022-07-09 op ctl_seek(struct parse_result *res, int argc, char **argv)
770 071167ed 2022-07-09 op const char *n;
772 071167ed 2022-07-09 op int hours = 0, minutes = 0, seconds = 0;
773 071167ed 2022-07-09 op int sign = 1;
775 e5d4e9f7 2022-07-09 op if (argc > 0) {
776 e5d4e9f7 2022-07-09 op /* skip the command name */
781 e5d4e9f7 2022-07-09 op if (argc > 0 && !strcmp(*argv, "--")) {
786 e5d4e9f7 2022-07-09 op if (argc != 1)
787 e5d4e9f7 2022-07-09 op ctl_usage(res->ctl);
790 e5d4e9f7 2022-07-09 op if (*n == '-' || *n == '+')
791 e5d4e9f7 2022-07-09 op res->seek.relative = 1;
792 071167ed 2022-07-09 op if (*n == '-') {
797 071167ed 2022-07-09 op seconds = strtol(n, &ep, 10);
798 071167ed 2022-07-09 op if (n[0] == '\0' ||
799 071167ed 2022-07-09 op (*ep != '\0' && *ep != ':' && *ep != '%') ||
800 071167ed 2022-07-09 op (*ep == '%' && ep[1] != '\0'))
801 071167ed 2022-07-09 op fatalx("invalid offset: %s", argv[0]);
802 071167ed 2022-07-09 op if (*ep == '\0' || *ep == '%') {
803 071167ed 2022-07-09 op res->seek.percent = *ep == '%';
808 071167ed 2022-07-09 op minutes = seconds;
809 071167ed 2022-07-09 op seconds = strtol(n, &ep, 10);
810 071167ed 2022-07-09 op if (n[0] == '\0' || (*ep != '\0' && *ep != ':'))
811 071167ed 2022-07-09 op fatalx("invalid offset: %s", argv[0]);
812 071167ed 2022-07-09 op if (*ep == '\0')
816 071167ed 2022-07-09 op hours = minutes;
817 071167ed 2022-07-09 op minutes = seconds;
818 071167ed 2022-07-09 op seconds = strtol(n, &ep, 10);
819 071167ed 2022-07-09 op if (n[0] == '\0' || *ep != '\0')
820 071167ed 2022-07-09 op fatalx("invalid offset: %s", argv[0]);
823 071167ed 2022-07-09 op res->seek.offset = sign * (hours * 3600 + minutes * 60 + seconds);
824 a913de21 2022-02-17 op return ctlaction(res);
828 68e4f29e 2022-07-10 op ctl_status(struct parse_result *res, int argc, char **argv)
830 68e4f29e 2022-07-10 op const char *fmt = NULL;
833 68e4f29e 2022-07-10 op while ((ch = getopt(argc, argv, "f:")) != -1) {
834 68e4f29e 2022-07-10 op switch (ch) {
836 68e4f29e 2022-07-10 op fmt = optarg;
839 68e4f29e 2022-07-10 op ctl_usage(res->ctl);
842 68e4f29e 2022-07-10 op argc -= optind;
843 68e4f29e 2022-07-10 op argv += optind;
845 68e4f29e 2022-07-10 op if (argc > 0)
846 68e4f29e 2022-07-10 op ctl_usage(res->ctl);
848 68e4f29e 2022-07-10 op if (fmt == NULL && (fmt = getenv("AMUSED_STATUS_FORMAT")) == NULL)
849 68e4f29e 2022-07-10 op fmt = "status,time,repeat";
850 68e4f29e 2022-07-10 op res->status_format = fmt;
851 68e4f29e 2022-07-10 op return ctlaction(res);
855 1d673950 2022-03-03 op ctl_get_lock(const char *lockfile)
859 1d673950 2022-03-03 op if ((lockfd = open(lockfile, O_WRONLY|O_CREAT, 0600)) == -1) {
860 1d673950 2022-03-03 op log_debug("open failed: %s", strerror(errno));
864 1d673950 2022-03-03 op if (flock(lockfd, LOCK_EX|LOCK_NB) == -1) {
865 1d673950 2022-03-03 op log_debug("flock failed: %s", strerror(errno));
866 1d673950 2022-03-03 op if (errno != EAGAIN)
868 1d673950 2022-03-03 op while (flock(lockfd, LOCK_EX) == -1 && errno == EINTR)
870 1d673950 2022-03-03 op close(lockfd);
873 1d673950 2022-03-03 op log_debug("flock succeeded");
875 1d673950 2022-03-03 op return lockfd;
879 1d673950 2022-03-03 op ctl_connect(void)
881 1d673950 2022-03-03 op struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
882 1d673950 2022-03-03 op struct sockaddr_un sa;
884 1d673950 2022-03-03 op int fd, lockfd = -1, locked = 0, spawned = 0;
885 1b77ad48 2022-07-08 op int attempt = 0;
886 1d673950 2022-03-03 op char *lockfile = NULL;
888 1d673950 2022-03-03 op memset(&sa, 0, sizeof(sa));
889 1d673950 2022-03-03 op sa.sun_family = AF_UNIX;
890 1d673950 2022-03-03 op size = strlcpy(sa.sun_path, csock, sizeof(sa.sun_path));
891 1d673950 2022-03-03 op if (size >= sizeof(sa.sun_path)) {
892 1d673950 2022-03-03 op errno = ENAMETOOLONG;
897 1d673950 2022-03-03 op if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
900 1d673950 2022-03-03 op if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
901 1d673950 2022-03-03 op log_debug("connection failed: %s", strerror(errno));
902 1d673950 2022-03-03 op if (errno != ECONNREFUSED && errno != ENOENT)
904 1b77ad48 2022-07-08 op if (attempt++ == 100)
908 1d673950 2022-03-03 op if (!locked) {
909 1d673950 2022-03-03 op xasprintf(&lockfile, "%s.lock", csock);
910 1d673950 2022-03-03 op if ((lockfd = ctl_get_lock(lockfile)) < 0) {
911 1d673950 2022-03-03 op log_debug("didn't get the lock (%d)", lockfd);
913 1d673950 2022-03-03 op free(lockfile);
914 1d673950 2022-03-03 op lockfile = NULL;
916 1d673950 2022-03-03 op if (lockfd == -1)
921 1d673950 2022-03-03 op * Always retry at least once, even if we got
922 1d673950 2022-03-03 op * the lock, because another client could have
923 1d673950 2022-03-03 op * taken the lock, started the server and released
924 1d673950 2022-03-03 op * the lock between our connect() and flock()
930 1d673950 2022-03-03 op if (!spawned) {
931 1d673950 2022-03-03 op log_debug("spawning the daemon");
932 1d673950 2022-03-03 op spawn_daemon();
936 1d673950 2022-03-03 op nanosleep(&ts, NULL);
940 1d673950 2022-03-03 op if (locked && lockfd >= 0) {
941 1d673950 2022-03-03 op unlink(lockfile);
942 1d673950 2022-03-03 op free(lockfile);
943 1d673950 2022-03-03 op close(lockfd);
948 1d673950 2022-03-03 op if (locked) {
949 1d673950 2022-03-03 op free(lockfile);
950 1d673950 2022-03-03 op close(lockfd);
957 fea541a8 2022-02-16 op ctl(int argc, char **argv)
959 1d673950 2022-03-03 op int ctl_sock;
961 fea541a8 2022-02-16 op log_init(1, LOG_DAEMON);
962 fea541a8 2022-02-16 op log_setverbose(verbose);
964 96233476 2022-06-09 op if (getcwd(cwd, sizeof(cwd)) == NULL)
965 96233476 2022-06-09 op fatal("getcwd");
967 1d673950 2022-03-03 op if ((ctl_sock = ctl_connect()) == -1)
968 1d673950 2022-03-03 op fatal("can't connect");
970 fea541a8 2022-02-16 op if (ctl_sock == -1)
971 fea541a8 2022-02-16 op fatalx("failed to connect to the daemon");
973 3baa2617 2022-02-16 op ibuf = xmalloc(sizeof(*ibuf));
974 3baa2617 2022-02-16 op imsg_init(ibuf, ctl_sock);
976 3baa2617 2022-02-16 op optreset = 1;
979 3baa2617 2022-02-16 op exit(parse(argc, argv));