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>
39 static struct imsgbuf *ibuf;
42 static int ctl_noarg(struct parse_result *, int, char **);
43 static int ctl_add(struct parse_result *, int, char **);
44 static int ctl_show(struct parse_result *, int, char **);
45 static int ctl_load(struct parse_result *, int, char **);
46 static int ctl_jump(struct parse_result *, int, char **);
47 static int ctl_repeat(struct parse_result *, int, char **);
48 static int ctl_consume(struct parse_result *, int, char **);
49 static int ctl_monitor(struct parse_result *, int, char **);
50 static int ctl_seek(struct parse_result *, int, char **);
51 static int ctl_status(struct parse_result *, int, char **);
53 struct ctl_command ctl_commands[] = {
54 { "add", ADD, ctl_add, "files..."},
55 { "consume", MODE, ctl_consume, "one|all"},
56 { "flush", FLUSH, ctl_noarg, ""},
57 { "jump", JUMP, ctl_jump, "pattern"},
58 { "load", LOAD, ctl_load, "[file]"},
59 { "monitor", MONITOR, ctl_monitor, "[events]"},
60 { "next", NEXT, ctl_noarg, ""},
61 { "pause", PAUSE, ctl_noarg, ""},
62 { "play", PLAY, ctl_noarg, ""},
63 { "prev", PREV, ctl_noarg, ""},
64 { "repeat", MODE, ctl_repeat, "one|all on|off"},
65 { "restart", RESTART, ctl_noarg, ""},
66 { "seek", SEEK, ctl_seek, "[+-]time[%]"},
67 { "show", SHOW, ctl_show, "[-p]"},
68 { "status", STATUS, ctl_status, "[-f fmt]"},
69 { "stop", STOP, ctl_noarg, ""},
70 { "toggle", TOGGLE, ctl_noarg, ""},
71 { NULL, 0, NULL, NULL},
77 fprintf(stderr, "usage: %s [-dv] [-s socket]\n", getprogname());
82 ctl_usage(struct ctl_command *ctl)
84 fprintf(stderr, "usage: %s [-v] [-s socket] %s %s\n", getprogname(),
85 ctl->name, ctl->usage);
89 /* based on canonpath from kern_pledge.c */
91 canonpath(const char *input, char *buf, size_t bufsize)
94 char *q, path[PATH_MAX];
97 if (input[0] != '/') {
98 r = snprintf(path, sizeof(path), "%s/%s", cwd, input);
99 if (r < 0 || (size_t)r >= sizeof(path)) {
100 errno = ENAMETOOLONG;
108 while (*p && (q - buf < bufsize)) {
109 if (p[0] == '/' && (p[1] == '/' || p[1] == '\0')) {
112 } else if (p[0] == '/' && p[1] == '.' &&
113 (p[2] == '/' || p[2] == '\0')) {
116 } else if (p[0] == '/' && p[1] == '.' && p[2] == '.' &&
117 (p[3] == '/' || p[3] == '\0')) {
119 if (q != buf) /* "/../" at start of buf */
127 if ((*p == '\0') && (q - buf < bufsize)) {
131 errno = ENAMETOOLONG;
137 parse(struct parse_result *res, int argc, char **argv)
139 struct ctl_command *ctl = NULL;
143 if ((argv0 = argv[0]) == NULL)
146 for (i = 0; ctl_commands[i].name != NULL; ++i) {
147 if (strncmp(ctl_commands[i].name, argv0, strlen(argv0))
151 "ambiguous argument: %s\n", argv0);
154 ctl = &ctl_commands[i];
159 fprintf(stderr, "unknown argument: %s\n", argv[0]);
163 res->action = ctl->action;
166 status = ctl->main(res, argc, argv);
173 load_files(struct parse_result *res, int *ret)
178 size_t linesize = 0, i = 0;
179 ssize_t linelen, curr = -1;
181 while ((linelen = getline(&line, &linesize, res->fp)) != -1) {
184 line[linelen-1] = '\0';
186 if (!strncmp(file, "> ", 2)) {
189 } else if (!strncmp(file, " ", 2))
192 memset(path, 0, sizeof(path));
193 if (canonpath(file, path, sizeof(path)) == -1) {
194 log_warn("canonpath %s", file);
199 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
209 imsg_compose(ibuf, IMSG_CTL_COMMIT, 0, 0, -1,
210 &curr, sizeof(curr));
216 imsg_strerror(struct imsg *imsg)
221 datalen = IMSG_DATA_SIZE(*imsg);
223 if (datalen == 0 || msg[datalen-1] != '\0')
224 fatalx("malformed error message");
247 case IMSG_CTL_COMMIT:
259 print_time(const char *label, int64_t seconds, const char *suffx)
266 hours = seconds / 3600;
267 seconds -= hours * 3600;
269 minutes = seconds / 60;
270 seconds -= minutes * 60;
274 printf("%02d:", hours);
275 printf("%02d:%02lld%s", minutes, (long long)seconds, suffx);
279 print_status(struct player_status *ps, const char *spec)
283 char *dup, *tmp, *tok;
285 if (ps->status == STATE_STOPPED)
287 else if (ps->status == STATE_PLAYING)
289 else if (ps->status == STATE_PAUSED)
294 percent = 100.0 * (double)ps->position / (double)ps->duration;
296 tmp = dup = xstrdup(spec);
297 while ((tok = strsep(&tmp, ",")) != NULL) {
301 if (!strcmp(tok, "path")) {
303 } else if (!strcmp(tok, "mode:oneline")) {
304 printf("repeat one:%s ",
305 ps->mode.repeat_one ? "on" : "off");
306 printf("all:%s ", ps->mode.repeat_all ? "on" : "off");
307 printf("consume:%s\n", ps->mode.consume ? "on" : "off");
308 } else if (!strcmp(tok, "mode")) {
309 printf("repeat all %s\n",
310 ps->mode.repeat_all ? "on" : "off");
311 printf("repeat one %s\n",
312 ps->mode.repeat_one ? "on" : "off");
313 printf("consume %s\n",
314 ps->mode.consume ? "on" : "off");
315 } else if (!strcmp(tok, "status")) {
316 printf("%s %s\n", status, ps->path);
317 } else if (!strcmp(tok, "time:oneline")) {
318 print_time("time ", ps->position, " / ");
319 print_time("", ps->duration, "\n");
320 } else if (!strcmp(tok, "time:percentage")) {
321 printf("position %.2f%%\n", percent);
322 } else if (!strcmp(tok, "time:raw")) {
323 printf("position %lld\n", (long long)ps->position);
324 printf("duration %lld\n", (long long)ps->duration);
325 } else if (!strcmp(tok, "time")) {
326 print_time("position ", ps->position, "\n");
327 print_time("duration ", ps->duration, "\n");
335 print_monitor_event(struct player_event *ev)
339 printf("%s repeat one:%s all:%s consume:%s\n",
340 event_name(ev->event),
341 ev->mode.repeat_one ? "on" : "off",
342 ev->mode.repeat_all ? "on" : "off",
343 ev->mode.consume ? "on" : "off");
346 printf("%s %lld %lld\n", event_name(ev->event),
347 (long long)ev->position, (long long)ev->duration);
350 puts(event_name(ev->event));
358 ctlaction(struct parse_result *res)
362 struct player_status ps;
363 struct player_event ev;
366 int i, ret = 0, done = 1;
368 if (pledge("stdio", NULL) == -1)
371 switch (res->action) {
373 imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
375 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
381 imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
384 imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
386 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
392 imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
396 for (i = 0; res->files[i] != NULL; ++i) {
397 memset(path, 0, sizeof(path));
398 if (canonpath(res->files[i], path, sizeof(path))
400 log_warn("canonpath %s", res->files[i]);
404 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
410 imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
414 imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
418 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
421 imsg_compose(ibuf, IMSG_CTL_NEXT, 0, 0, -1, NULL, 0);
423 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
429 imsg_compose(ibuf, IMSG_CTL_PREV, 0, 0, -1, NULL, 0);
431 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
438 imsg_compose(ibuf, IMSG_CTL_BEGIN, 0, 0, -1, NULL, 0);
442 memset(path, 0, sizeof(path));
443 strlcpy(path, res->files[0], sizeof(path));
444 imsg_compose(ibuf, IMSG_CTL_JUMP, 0, 0, -1,
449 imsg_compose(ibuf, IMSG_CTL_MODE, 0, 0, -1,
450 &res->mode, sizeof(res->mode));
451 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
452 &res->mode, sizeof(res->mode));
453 res->status_format = "mode:oneline";
455 res->status_format = "mode";
459 imsg_compose(ibuf, IMSG_CTL_MONITOR, 0, 0, -1,
463 memset(&res->seek, 0, sizeof(res->seek));
466 imsg_compose(ibuf, IMSG_CTL_SEEK, 0, 0, -1, &res->seek,
470 /* action not expected */
471 fatalx("invalid action %u", res->action);
482 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
483 fatalx("imsg_read error");
485 fatalx("pipe closed");
488 if ((n = imsg_get(ibuf, &imsg)) == -1)
489 fatalx("imsg_get error");
493 if (imsg.hdr.type == IMSG_CTL_ERR) {
494 log_warnx("%s: %s", res->ctl->name,
495 imsg_strerror(&imsg));
501 datalen = IMSG_DATA_SIZE(imsg);
503 switch (res->action) {
505 if (res->files[i] == NULL)
506 fatalx("received more replies than "
509 if (imsg.hdr.type == IMSG_CTL_ADD)
510 log_debug("enqueued %s", res->files[i]);
512 fatalx("invalid message %d",
515 done = res->files[i] == NULL;
522 if (datalen != sizeof(ps))
523 fatalx("data size mismatch");
524 memcpy(&ps, imsg.data, sizeof(ps));
525 if (ps.path[sizeof(ps.path) - 1] != '\0')
526 fatalx("received corrupted data");
529 if (ps.status == STATE_PLAYING)
542 if (imsg.hdr.type != IMSG_CTL_STATUS)
543 fatalx("invalid message %d",
546 if (datalen != sizeof(ps))
547 fatalx("data size mismatch");
548 memcpy(&ps, imsg.data, sizeof(ps));
549 if (ps.path[sizeof(ps.path) - 1] != '\0')
550 fatalx("received corrupted data");
552 print_status(&ps, res->status_format);
556 if (imsg.hdr.type == IMSG_CTL_ADD)
558 if (imsg.hdr.type == IMSG_CTL_COMMIT) {
563 if (imsg.hdr.type != IMSG_CTL_BEGIN)
564 fatalx("invalid message %d",
567 load_files(res, &ret);
570 if (imsg.hdr.type != IMSG_CTL_MONITOR)
571 fatalx("invalid message %d",
574 if (datalen != sizeof(ev))
575 fatalx("data size mismatch");
577 memcpy(&ev, imsg.data, sizeof(ev));
578 if (ev.event < 0 || ev.event > IMSG__LAST)
579 fatalx("received corrupted data");
581 if (!res->monitor[ev.event])
584 print_monitor_event(&ev);
600 ctl_noarg(struct parse_result *res, int argc, char **argv)
604 while ((ch = getopt(argc, argv, "")) != -1)
612 return ctlaction(res);
616 ctl_add(struct parse_result *res, int argc, char **argv)
620 while ((ch = getopt(argc, argv, "")) != -1)
629 return ctlaction(res);
633 ctl_show(struct parse_result *res, int argc, char **argv)
637 while ((ch = getopt(argc, argv, "p")) != -1) {
647 return ctlaction(res);
651 ctl_load(struct parse_result *res, int argc, char **argv)
655 while ((ch = getopt(argc, argv, "")) != -1)
665 if ((res->fp = fopen(argv[0], "r")) == NULL)
666 fatal("can't open %s", argv[0]);
669 return ctlaction(res);
673 ctl_jump(struct parse_result *res, int argc, char **argv)
677 while ((ch = getopt(argc, argv, "")) != -1)
686 return ctlaction(res);
690 parse_mode(struct parse_result *res, const char *v)
694 if (!strcmp(v, "on"))
696 if (!strcmp(v, "off"))
702 ctl_repeat(struct parse_result *res, int argc, char **argv)
706 while ((ch = getopt(argc, argv, "")) != -1)
711 if (argc != 1 && argc != 2)
714 if (!strcmp(argv[0], "one"))
715 res->mode.repeat_one = parse_mode(res, argv[1]);
716 else if (!strcmp(argv[0], "all"))
717 res->mode.repeat_all = parse_mode(res, argv[1]);
721 return ctlaction(res);
725 ctl_consume(struct parse_result *res, int argc, char **argv)
729 while ((ch = getopt(argc, argv, "")) != -1)
737 res->mode.consume = parse_mode(res, argv[0]);
738 return ctlaction(res);
742 ctl_monitor(struct parse_result *res, int argc, char **argv)
746 char *dup, *tmp, *tok;
748 while ((ch = getopt(argc, argv, "")) != -1)
756 events = "play,pause,stop,next,prev,jump,mode,add,load,seek";
760 tmp = dup = xstrdup(events);
761 while ((tok = strsep(&tmp, ",")) != NULL) {
766 if (!strcmp(tok, "play"))
767 res->monitor[IMSG_CTL_PLAY] = 1;
768 else if (!strcmp(tok, "pause"))
769 res->monitor[IMSG_CTL_PAUSE] = 1;
770 else if (!strcmp(tok, "stop"))
771 res->monitor[IMSG_CTL_STOP] = 1;
772 else if (!strcmp(tok, "next"))
773 res->monitor[IMSG_CTL_NEXT] = 1;
774 else if (!strcmp(tok, "prev"))
775 res->monitor[IMSG_CTL_PREV] = 1;
776 else if (!strcmp(tok, "jump"))
777 res->monitor[IMSG_CTL_JUMP] = 1;
778 else if (!strcmp(tok, "mode"))
779 res->monitor[IMSG_CTL_MODE] = 1;
780 else if (!strcmp(tok, "add"))
781 res->monitor[IMSG_CTL_ADD] = 1;
782 else if (!strcmp(tok, "load"))
783 res->monitor[IMSG_CTL_COMMIT] = 1;
784 else if (!strcmp(tok, "seek"))
785 res->monitor[IMSG_CTL_SEEK] = 1;
787 log_warnx("unknown event \"%s\"", tok);
795 return ctlaction(res);
799 ctl_seek(struct parse_result *res, int argc, char **argv)
803 int hours = 0, minutes = 0, seconds = 0;
807 /* skip the command name */
812 if (argc > 0 && !strcmp(*argv, "--")) {
821 if (*n == '-' || *n == '+')
822 res->seek.relative = 1;
828 seconds = strtol(n, &ep, 10);
830 (*ep != '\0' && *ep != ':' && *ep != '%') ||
831 (*ep == '%' && ep[1] != '\0'))
832 fatalx("invalid offset: %s", argv[0]);
833 if (*ep == '\0' || *ep == '%') {
834 res->seek.percent = *ep == '%';
840 seconds = strtol(n, &ep, 10);
841 if (n[0] == '\0' || (*ep != '\0' && *ep != ':'))
842 fatalx("invalid offset: %s", argv[0]);
849 seconds = strtol(n, &ep, 10);
850 if (n[0] == '\0' || *ep != '\0')
851 fatalx("invalid offset: %s", argv[0]);
854 res->seek.offset = sign * (hours * 3600 + minutes * 60 + seconds);
855 return ctlaction(res);
859 ctl_status(struct parse_result *res, int argc, char **argv)
863 while ((ch = getopt(argc, argv, "f:")) != -1) {
866 res->status_format = optarg;
878 return ctlaction(res);
882 ctl_get_lock(const char *lockfile)
886 if ((lockfd = open(lockfile, O_WRONLY|O_CREAT, 0600)) == -1) {
887 log_debug("open failed: %s", strerror(errno));
891 if (flock(lockfd, LOCK_EX|LOCK_NB) == -1) {
892 log_debug("flock failed: %s", strerror(errno));
895 while (flock(lockfd, LOCK_EX) == -1 && errno == EINTR)
900 log_debug("flock succeeded");
908 struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
909 struct sockaddr_un sa;
911 int fd, lockfd = -1, locked = 0, spawned = 0;
913 char *lockfile = NULL;
915 memset(&sa, 0, sizeof(sa));
916 sa.sun_family = AF_UNIX;
917 size = strlcpy(sa.sun_path, csock, sizeof(sa.sun_path));
918 if (size >= sizeof(sa.sun_path)) {
919 errno = ENAMETOOLONG;
924 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
927 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
928 log_debug("connection failed: %s", strerror(errno));
929 if (errno != ECONNREFUSED && errno != ENOENT)
931 if (attempt++ == 100)
936 xasprintf(&lockfile, "%s.lock", csock);
937 if ((lockfd = ctl_get_lock(lockfile)) < 0) {
938 log_debug("didn't get the lock (%d)", lockfd);
948 * Always retry at least once, even if we got
949 * the lock, because another client could have
950 * taken the lock, started the server and released
951 * the lock between our connect() and flock()
958 log_debug("spawning the daemon");
963 nanosleep(&ts, NULL);
967 if (locked && lockfd >= 0) {
984 ctl(int argc, char **argv)
986 struct parse_result res;
990 memset(&res, 0, sizeof(res));
991 if ((fmt = getenv("AMUSED_STATUS_FORMAT")) == NULL)
992 fmt = "status,time:oneline,mode:oneline";
993 res.status_format = fmt;
995 res.mode.consume = MODE_UNDEF;
996 res.mode.repeat_all = MODE_UNDEF;
997 res.mode.repeat_one = MODE_UNDEF;
999 log_init(1, LOG_DAEMON);
1000 log_setverbose(verbose);
1002 if (getcwd(cwd, sizeof(cwd)) == NULL)
1005 if ((ctl_sock = ctl_connect()) == -1)
1006 fatal("can't connect");
1008 ibuf = xmalloc(sizeof(*ibuf));
1009 imsg_init(ibuf, ctl_sock);
1014 /* we'll drop rpath too later in ctlaction */
1015 if (pledge("stdio rpath", NULL) == -1)
1018 exit(parse(&res, argc, argv));