2 * Copyright (c) 2022, 2023 Omar Polo <op@omarpolo.com>
3 * Copyright (c) 2015 Theo de Raadt <deraadt@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include <sys/socket.h>
38 static struct imsgbuf *ibuf;
41 static int ctl_noarg(struct parse_result *, int, char **);
42 static int ctl_add(struct parse_result *, int, char **);
43 static int ctl_show(struct parse_result *, int, char **);
44 static int ctl_load(struct parse_result *, int, char **);
45 static int ctl_jump(struct parse_result *, int, char **);
46 static int ctl_repeat(struct parse_result *, int, char **);
47 static int ctl_consume(struct parse_result *, int, char **);
48 static int ctl_monitor(struct parse_result *, int, char **);
49 static int ctl_seek(struct parse_result *, int, char **);
50 static int ctl_status(struct parse_result *, int, char **);
52 struct ctl_command ctl_commands[] = {
53 { "add", ADD, ctl_add, "files..."},
54 { "consume", MODE, ctl_consume, "one|all"},
55 { "flush", FLUSH, ctl_noarg, ""},
56 { "jump", JUMP, ctl_jump, "pattern"},
57 { "load", LOAD, ctl_load, "[file]"},
58 { "monitor", MONITOR, ctl_monitor, "[events]"},
59 { "next", NEXT, ctl_noarg, ""},
60 { "pause", PAUSE, ctl_noarg, ""},
61 { "play", PLAY, ctl_noarg, ""},
62 { "prev", PREV, ctl_noarg, ""},
63 { "repeat", MODE, ctl_repeat, "one|all on|off"},
64 { "restart", RESTART, ctl_noarg, ""},
65 { "seek", SEEK, ctl_seek, "[+-]time[%]"},
66 { "show", SHOW, ctl_show, "[-p]"},
67 { "status", STATUS, ctl_status, "[-f fmt]"},
68 { "stop", STOP, ctl_noarg, ""},
69 { "toggle", TOGGLE, ctl_noarg, ""},
70 { NULL, 0, NULL, NULL},
76 fprintf(stderr, "usage: %s [-dv] [-s socket]\n", getprogname());
81 ctl_usage(struct ctl_command *ctl)
83 fprintf(stderr, "usage: %s [-v] [-s socket] %s %s\n", getprogname(),
84 ctl->name, ctl->usage);
88 /* based on canonpath from kern_pledge.c */
90 canonpath(const char *input, char *buf, size_t bufsize)
93 char *q, path[PATH_MAX];
96 if (input[0] != '/') {
97 r = snprintf(path, sizeof(path), "%s/%s", cwd, input);
98 if (r < 0 || (size_t)r >= sizeof(path)) {
107 while (*p && (q - buf < bufsize)) {
108 if (p[0] == '/' && (p[1] == '/' || p[1] == '\0')) {
111 } else if (p[0] == '/' && p[1] == '.' &&
112 (p[2] == '/' || p[2] == '\0')) {
115 } else if (p[0] == '/' && p[1] == '.' && p[2] == '.' &&
116 (p[3] == '/' || p[3] == '\0')) {
118 if (q != buf) /* "/../" at start of buf */
126 if ((*p == '\0') && (q - buf < bufsize)) {
130 errno = ENAMETOOLONG;
136 parse(struct parse_result *res, int argc, char **argv)
138 struct ctl_command *ctl = NULL;
142 if ((argv0 = argv[0]) == NULL)
145 for (i = 0; ctl_commands[i].name != NULL; ++i) {
146 if (strncmp(ctl_commands[i].name, argv0, strlen(argv0))
150 "ambiguous argument: %s\n", argv0);
153 ctl = &ctl_commands[i];
158 fprintf(stderr, "unknown argument: %s\n", argv[0]);
162 res->action = ctl->action;
165 status = ctl->main(res, argc, argv);
172 load_files(struct parse_result *res, int *ret)
177 size_t linesize = 0, i = 0;
178 ssize_t linelen, curr = -1;
180 while ((linelen = getline(&line, &linesize, res->fp)) != -1) {
183 line[linelen-1] = '\0';
185 if (!strncmp(file, "> ", 2)) {
188 } else if (!strncmp(file, " ", 2))
191 memset(path, 0, sizeof(path));
192 if (canonpath(file, path, sizeof(path)) == -1) {
193 log_warn("canonpath %s", file);
198 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
208 imsg_compose(ibuf, IMSG_CTL_COMMIT, 0, 0, -1,
209 &curr, sizeof(curr));
215 imsg_strerror(struct imsg *imsg)
220 datalen = IMSG_DATA_SIZE(*imsg);
222 if (datalen == 0 || msg[datalen-1] != '\0')
223 fatalx("malformed error message");
246 case IMSG_CTL_COMMIT:
258 print_time(const char *label, int64_t seconds, const char *suffx)
265 hours = seconds / 3600;
266 seconds -= hours * 3600;
268 minutes = seconds / 60;
269 seconds -= minutes * 60;
273 printf("%02d:", hours);
274 printf("%02d:%02lld%s", minutes, (long long)seconds, suffx);
278 print_status(struct player_status *ps, const char *spec)
282 char *dup, *tmp, *tok;
284 if (ps->status == STATE_STOPPED)
286 else if (ps->status == STATE_PLAYING)
288 else if (ps->status == STATE_PAUSED)
293 percent = 100.0 * (double)ps->position / (double)ps->duration;
295 tmp = dup = xstrdup(spec);
296 while ((tok = strsep(&tmp, ",")) != NULL) {
300 if (!strcmp(tok, "path")) {
302 } else if (!strcmp(tok, "mode:oneline")) {
303 printf("repeat one:%s ",
304 ps->mode.repeat_one ? "on" : "off");
305 printf("all:%s ", ps->mode.repeat_all ? "on" : "off");
306 printf("consume:%s\n", ps->mode.consume ? "on" : "off");
307 } else if (!strcmp(tok, "mode")) {
308 printf("repeat all %s\n",
309 ps->mode.repeat_all ? "on" : "off");
310 printf("repeat one %s\n",
311 ps->mode.repeat_one ? "on" : "off");
312 printf("consume %s\n",
313 ps->mode.consume ? "on" : "off");
314 } else if (!strcmp(tok, "status")) {
315 printf("%s %s\n", status, ps->path);
316 } else if (!strcmp(tok, "time:oneline")) {
317 print_time("time ", ps->position, " / ");
318 print_time("", ps->duration, "\n");
319 } else if (!strcmp(tok, "time:percentage")) {
320 printf("position %.2f%%\n", percent);
321 } else if (!strcmp(tok, "time:raw")) {
322 printf("position %lld\n", (long long)ps->position);
323 printf("duration %lld\n", (long long)ps->duration);
324 } else if (!strcmp(tok, "time")) {
325 print_time("position ", ps->position, "\n");
326 print_time("duration ", ps->duration, "\n");
334 print_monitor_event(struct player_event *ev)
338 printf("%s repeat one:%s all:%s consume:%s\n",
339 event_name(ev->event),
340 ev->mode.repeat_one ? "on" : "off",
341 ev->mode.repeat_all ? "on" : "off",
342 ev->mode.consume ? "on" : "off");
345 printf("%s %lld %lld\n", event_name(ev->event),
346 (long long)ev->position, (long long)ev->duration);
349 puts(event_name(ev->event));
357 ctlaction(struct parse_result *res)
361 struct player_status ps;
362 struct player_event ev;
365 int i, ret = 0, done = 1;
367 if (pledge("stdio", NULL) == -1)
370 switch (res->action) {
372 imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
374 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
380 imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
383 imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
385 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
391 imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
395 for (i = 0; res->files[i] != NULL; ++i) {
396 memset(path, 0, sizeof(path));
397 if (canonpath(res->files[i], path, sizeof(path))
399 log_warn("canonpath %s", res->files[i]);
403 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
409 imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
413 imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
417 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
420 imsg_compose(ibuf, IMSG_CTL_NEXT, 0, 0, -1, NULL, 0);
422 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
428 imsg_compose(ibuf, IMSG_CTL_PREV, 0, 0, -1, NULL, 0);
430 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
437 imsg_compose(ibuf, IMSG_CTL_BEGIN, 0, 0, -1, NULL, 0);
441 memset(path, 0, sizeof(path));
442 strlcpy(path, res->files[0], sizeof(path));
443 imsg_compose(ibuf, IMSG_CTL_JUMP, 0, 0, -1,
448 imsg_compose(ibuf, IMSG_CTL_MODE, 0, 0, -1,
449 &res->mode, sizeof(res->mode));
450 res->status_format = "mode:oneline";
452 res->status_format = "mode";
456 imsg_compose(ibuf, IMSG_CTL_MONITOR, 0, 0, -1,
460 memset(&res->seek, 0, sizeof(res->seek));
463 imsg_compose(ibuf, IMSG_CTL_SEEK, 0, 0, -1, &res->seek,
467 /* action not expected */
468 fatalx("invalid action %u", res->action);
479 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
480 fatalx("imsg_read error");
482 fatalx("pipe closed");
485 if ((n = imsg_get(ibuf, &imsg)) == -1)
486 fatalx("imsg_get error");
490 if (imsg.hdr.type == IMSG_CTL_ERR) {
491 log_warnx("%s: %s", res->ctl->name,
492 imsg_strerror(&imsg));
498 datalen = IMSG_DATA_SIZE(imsg);
500 switch (res->action) {
502 if (res->files[i] == NULL)
503 fatalx("received more replies than "
506 if (imsg.hdr.type == IMSG_CTL_ADD)
507 log_debug("enqueued %s", res->files[i]);
509 fatalx("invalid message %d",
512 done = res->files[i] == NULL;
519 if (datalen != sizeof(ps))
520 fatalx("data size mismatch");
521 memcpy(&ps, imsg.data, sizeof(ps));
522 if (ps.path[sizeof(ps.path) - 1] != '\0')
523 fatalx("received corrupted data");
526 if (ps.status == STATE_PLAYING)
539 if (imsg.hdr.type != IMSG_CTL_STATUS)
540 fatalx("invalid message %d",
543 if (datalen != sizeof(ps))
544 fatalx("data size mismatch");
545 memcpy(&ps, imsg.data, sizeof(ps));
546 if (ps.path[sizeof(ps.path) - 1] != '\0')
547 fatalx("received corrupted data");
549 print_status(&ps, res->status_format);
553 if (imsg.hdr.type == IMSG_CTL_ADD)
555 if (imsg.hdr.type == IMSG_CTL_COMMIT) {
560 if (imsg.hdr.type != IMSG_CTL_BEGIN)
561 fatalx("invalid message %d",
564 load_files(res, &ret);
567 if (imsg.hdr.type != IMSG_CTL_MONITOR)
568 fatalx("invalid message %d",
571 if (datalen != sizeof(ev))
572 fatalx("data size mismatch");
574 memcpy(&ev, imsg.data, sizeof(ev));
575 if (ev.event < 0 || ev.event > IMSG__LAST)
576 fatalx("received corrupted data");
578 if (!res->monitor[ev.event])
581 print_monitor_event(&ev);
597 ctl_noarg(struct parse_result *res, int argc, char **argv)
601 while ((ch = getopt(argc, argv, "")) != -1)
609 return ctlaction(res);
613 ctl_add(struct parse_result *res, int argc, char **argv)
617 while ((ch = getopt(argc, argv, "")) != -1)
626 return ctlaction(res);
630 ctl_show(struct parse_result *res, int argc, char **argv)
634 while ((ch = getopt(argc, argv, "p")) != -1) {
644 return ctlaction(res);
648 ctl_load(struct parse_result *res, int argc, char **argv)
652 while ((ch = getopt(argc, argv, "")) != -1)
662 if ((res->fp = fopen(argv[0], "r")) == NULL)
663 fatal("can't open %s", argv[0]);
666 return ctlaction(res);
670 ctl_jump(struct parse_result *res, int argc, char **argv)
674 while ((ch = getopt(argc, argv, "")) != -1)
683 return ctlaction(res);
687 parse_mode(struct parse_result *res, const char *v)
691 if (!strcmp(v, "on"))
693 if (!strcmp(v, "off"))
699 ctl_repeat(struct parse_result *res, int argc, char **argv)
703 while ((ch = getopt(argc, argv, "")) != -1)
708 if (argc != 1 && argc != 2)
711 if (!strcmp(argv[0], "one"))
712 res->mode.repeat_one = parse_mode(res, argv[1]);
713 else if (!strcmp(argv[0], "all"))
714 res->mode.repeat_all = parse_mode(res, argv[1]);
718 return ctlaction(res);
722 ctl_consume(struct parse_result *res, int argc, char **argv)
726 while ((ch = getopt(argc, argv, "")) != -1)
734 res->mode.consume = parse_mode(res, argv[0]);
735 return ctlaction(res);
739 ctl_monitor(struct parse_result *res, int argc, char **argv)
743 char *dup, *tmp, *tok;
745 while ((ch = getopt(argc, argv, "")) != -1)
753 events = "play,pause,stop,next,prev,jump,mode,add,load,seek";
757 tmp = dup = xstrdup(events);
758 while ((tok = strsep(&tmp, ",")) != NULL) {
763 if (!strcmp(tok, "play"))
764 res->monitor[IMSG_CTL_PLAY] = 1;
765 else if (!strcmp(tok, "pause"))
766 res->monitor[IMSG_CTL_PAUSE] = 1;
767 else if (!strcmp(tok, "stop"))
768 res->monitor[IMSG_CTL_STOP] = 1;
769 else if (!strcmp(tok, "next"))
770 res->monitor[IMSG_CTL_NEXT] = 1;
771 else if (!strcmp(tok, "prev"))
772 res->monitor[IMSG_CTL_PREV] = 1;
773 else if (!strcmp(tok, "jump"))
774 res->monitor[IMSG_CTL_JUMP] = 1;
775 else if (!strcmp(tok, "mode"))
776 res->monitor[IMSG_CTL_MODE] = 1;
777 else if (!strcmp(tok, "add"))
778 res->monitor[IMSG_CTL_ADD] = 1;
779 else if (!strcmp(tok, "load"))
780 res->monitor[IMSG_CTL_COMMIT] = 1;
781 else if (!strcmp(tok, "seek"))
782 res->monitor[IMSG_CTL_SEEK] = 1;
784 log_warnx("unknown event \"%s\"", tok);
792 return ctlaction(res);
796 ctl_seek(struct parse_result *res, int argc, char **argv)
800 int hours = 0, minutes = 0, seconds = 0;
804 /* skip the command name */
809 if (argc > 0 && !strcmp(*argv, "--")) {
818 if (*n == '-' || *n == '+')
819 res->seek.relative = 1;
825 seconds = strtol(n, &ep, 10);
827 (*ep != '\0' && *ep != ':' && *ep != '%') ||
828 (*ep == '%' && ep[1] != '\0'))
829 fatalx("invalid offset: %s", argv[0]);
830 if (*ep == '\0' || *ep == '%') {
831 res->seek.percent = *ep == '%';
837 seconds = strtol(n, &ep, 10);
838 if (n[0] == '\0' || (*ep != '\0' && *ep != ':'))
839 fatalx("invalid offset: %s", argv[0]);
846 seconds = strtol(n, &ep, 10);
847 if (n[0] == '\0' || *ep != '\0')
848 fatalx("invalid offset: %s", argv[0]);
851 res->seek.offset = sign * (hours * 3600 + minutes * 60 + seconds);
852 return ctlaction(res);
856 ctl_status(struct parse_result *res, int argc, char **argv)
860 while ((ch = getopt(argc, argv, "f:")) != -1) {
863 res->status_format = optarg;
875 return ctlaction(res);
879 ctl_get_lock(const char *lockfile)
883 if ((lockfd = open(lockfile, O_WRONLY|O_CREAT, 0600)) == -1) {
884 log_debug("open failed: %s", strerror(errno));
888 if (flock(lockfd, LOCK_EX|LOCK_NB) == -1) {
889 log_debug("flock failed: %s", strerror(errno));
892 while (flock(lockfd, LOCK_EX) == -1 && errno == EINTR)
897 log_debug("flock succeeded");
905 struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
906 struct sockaddr_un sa;
908 int fd, lockfd = -1, locked = 0, spawned = 0;
910 char *lockfile = NULL;
912 memset(&sa, 0, sizeof(sa));
913 sa.sun_family = AF_UNIX;
914 size = strlcpy(sa.sun_path, csock, sizeof(sa.sun_path));
915 if (size >= sizeof(sa.sun_path)) {
916 errno = ENAMETOOLONG;
921 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
924 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
925 log_debug("connection failed: %s", strerror(errno));
926 if (errno != ECONNREFUSED && errno != ENOENT)
928 if (attempt++ == 100)
933 xasprintf(&lockfile, "%s.lock", csock);
934 if ((lockfd = ctl_get_lock(lockfile)) < 0) {
935 log_debug("didn't get the lock (%d)", lockfd);
945 * Always retry at least once, even if we got
946 * the lock, because another client could have
947 * taken the lock, started the server and released
948 * the lock between our connect() and flock()
955 log_debug("spawning the daemon");
960 nanosleep(&ts, NULL);
964 if (locked && lockfd >= 0) {
981 ctl(int argc, char **argv)
983 struct parse_result res;
987 memset(&res, 0, sizeof(res));
988 if ((fmt = getenv("AMUSED_STATUS_FORMAT")) == NULL)
989 fmt = "status,time:oneline,mode:oneline";
990 res.status_format = fmt;
992 res.mode.consume = MODE_UNDEF;
993 res.mode.repeat_all = MODE_UNDEF;
994 res.mode.repeat_one = MODE_UNDEF;
996 log_init(1, LOG_DAEMON);
997 log_setverbose(verbose);
999 if (getcwd(cwd, sizeof(cwd)) == NULL)
1002 if ((ctl_sock = ctl_connect()) == -1)
1003 fatal("can't connect");
1005 ibuf = xmalloc(sizeof(*ibuf));
1006 imsg_init(ibuf, ctl_sock);
1011 /* we'll drop rpath too later in ctlaction */
1012 if (pledge("stdio rpath", NULL) == -1)
1015 exit(parse(&res, argc, argv));