2 * Copyright (c) 2022 Omar Polo <op@openbsd.org>
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];
95 if (input[0] != '/') {
96 if (snprintf(path, sizeof(path), "%s/%s", cwd, input)
106 while (*p && (q - buf < bufsize)) {
107 if (p[0] == '/' && (p[1] == '/' || p[1] == '\0')) {
110 } else if (p[0] == '/' && p[1] == '.' &&
111 (p[2] == '/' || p[2] == '\0')) {
114 } else if (p[0] == '/' && p[1] == '.' && p[2] == '.' &&
115 (p[3] == '/' || p[3] == '\0')) {
117 if (q != buf) /* "/../" at start of buf */
125 if ((*p == '\0') && (q - buf < bufsize)) {
129 errno = ENAMETOOLONG;
135 parse(struct parse_result *res, int argc, char **argv)
137 struct ctl_command *ctl = NULL;
141 if ((argv0 = argv[0]) == NULL)
144 for (i = 0; ctl_commands[i].name != NULL; ++i) {
145 if (strncmp(ctl_commands[i].name, argv0, strlen(argv0))
149 "ambiguous argument: %s\n", argv0);
152 ctl = &ctl_commands[i];
157 fprintf(stderr, "unknown argument: %s\n", argv[0]);
161 res->action = ctl->action;
164 status = ctl->main(res, argc, argv);
171 load_files(struct parse_result *res, int *ret)
176 size_t linesize = 0, i = 0;
177 ssize_t linelen, curr = -1;
179 while ((linelen = getline(&line, &linesize, res->fp)) != -1) {
182 line[linelen-1] = '\0';
184 if (!strncmp(file, "> ", 2)) {
187 } else if (!strncmp(file, " ", 2))
190 memset(path, 0, sizeof(path));
191 if (canonpath(file, path, sizeof(path)) == -1) {
192 log_warn("canonpath %s", file);
197 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
207 imsg_compose(ibuf, IMSG_CTL_COMMIT, 0, 0, -1,
208 &curr, sizeof(curr));
214 imsg_strerror(struct imsg *imsg)
219 datalen = IMSG_DATA_SIZE(*imsg);
221 if (datalen == 0 || msg[datalen-1] != '\0')
222 fatalx("malformed error message");
245 case IMSG_CTL_COMMIT:
257 print_time(const char *label, int64_t seconds, const char *suffx)
264 hours = seconds / 3600;
265 seconds -= hours * 3600;
267 minutes = seconds / 60;
268 seconds -= minutes * 60;
272 printf("%02d:", hours);
273 printf("%02d:%02lld%s", minutes, (long long)seconds, suffx);
277 print_status(struct player_status *ps, const char *spec)
281 char *dup, *tmp, *tok;
283 if (ps->status == STATE_STOPPED)
285 else if (ps->status == STATE_PLAYING)
287 else if (ps->status == STATE_PAUSED)
292 percent = 100.0 * (double)ps->position / (double)ps->duration;
294 tmp = dup = xstrdup(spec);
295 while ((tok = strsep(&tmp, ",")) != NULL) {
299 if (!strcmp(tok, "path")) {
301 } else if (!strcmp(tok, "mode:oneline")) {
302 printf("repeat one:%s ",
303 ps->mode.repeat_one ? "on" : "off");
304 printf("all:%s ", ps->mode.repeat_all ? "on" : "off");
305 printf("consume:%s\n", ps->mode.consume ? "on" : "off");
306 } else if (!strcmp(tok, "mode")) {
307 printf("repeat all %s\n",
308 ps->mode.repeat_all ? "on" : "off");
309 printf("repeat one %s\n",
310 ps->mode.repeat_one ? "on" : "off");
311 printf("consume %s\n",
312 ps->mode.consume ? "on" : "off");
313 } else if (!strcmp(tok, "status")) {
314 printf("%s %s\n", status, ps->path);
315 } else if (!strcmp(tok, "time:oneline")) {
316 print_time("time ", ps->position, " / ");
317 print_time("", ps->duration, "\n");
318 } else if (!strcmp(tok, "time:percentage")) {
319 printf("position %.2f%%\n", percent);
320 } else if (!strcmp(tok, "time:raw")) {
321 printf("position %lld\n", (long long)ps->position);
322 printf("duration %lld\n", (long long)ps->duration);
323 } else if (!strcmp(tok, "time")) {
324 print_time("position ", ps->position, "\n");
325 print_time("duration ", ps->duration, "\n");
333 ctlaction(struct parse_result *res)
337 struct player_status ps;
340 int i, type, ret = 0, done = 1;
342 if (pledge("stdio", NULL) == -1)
345 switch (res->action) {
347 imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
349 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
355 imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
358 imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
360 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
366 imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
370 for (i = 0; res->files[i] != NULL; ++i) {
371 memset(path, 0, sizeof(path));
372 if (canonpath(res->files[i], path, sizeof(path))
374 log_warn("canonpath %s", res->files[i]);
378 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
384 imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
388 imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
392 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
395 imsg_compose(ibuf, IMSG_CTL_NEXT, 0, 0, -1, NULL, 0);
397 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
403 imsg_compose(ibuf, IMSG_CTL_PREV, 0, 0, -1, NULL, 0);
405 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
412 imsg_compose(ibuf, IMSG_CTL_BEGIN, 0, 0, -1, NULL, 0);
416 memset(path, 0, sizeof(path));
417 strlcpy(path, res->files[0], sizeof(path));
418 imsg_compose(ibuf, IMSG_CTL_JUMP, 0, 0, -1,
423 imsg_compose(ibuf, IMSG_CTL_MODE, 0, 0, -1,
424 &res->mode, sizeof(res->mode));
425 res->status_format = "mode:oneline";
427 res->status_format = "mode";
431 imsg_compose(ibuf, IMSG_CTL_MONITOR, 0, 0, -1,
435 memset(&res->seek, 0, sizeof(res->seek));
438 imsg_compose(ibuf, IMSG_CTL_SEEK, 0, 0, -1, &res->seek,
442 /* action not expected */
443 fatalx("invalid action %u", res->action);
454 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
455 fatalx("imsg_read error");
457 fatalx("pipe closed");
460 if ((n = imsg_get(ibuf, &imsg)) == -1)
461 fatalx("imsg_get error");
465 if (imsg.hdr.type == IMSG_CTL_ERR) {
466 log_warnx("%s: %s", res->ctl->name,
467 imsg_strerror(&imsg));
473 datalen = IMSG_DATA_SIZE(imsg);
475 switch (res->action) {
477 if (res->files[i] == NULL)
478 fatalx("received more replies than "
481 if (imsg.hdr.type == IMSG_CTL_ADD)
482 log_debug("enqueued %s", res->files[i]);
484 fatalx("invalid message %d",
487 done = res->files[i] == NULL;
494 if (datalen != sizeof(ps))
495 fatalx("data size mismatch");
496 memcpy(&ps, imsg.data, sizeof(ps));
497 if (ps.path[sizeof(ps.path) - 1] != '\0')
498 fatalx("received corrupted data");
501 if (ps.status == STATE_PLAYING)
514 if (imsg.hdr.type != IMSG_CTL_STATUS)
515 fatalx("invalid message %d",
518 if (datalen != sizeof(ps))
519 fatalx("data size mismatch");
520 memcpy(&ps, imsg.data, sizeof(ps));
521 if (ps.path[sizeof(ps.path) - 1] != '\0')
522 fatalx("received corrupted data");
524 print_status(&ps, res->status_format);
528 if (imsg.hdr.type == IMSG_CTL_ADD)
530 if (imsg.hdr.type == IMSG_CTL_COMMIT) {
535 if (imsg.hdr.type != IMSG_CTL_BEGIN)
536 fatalx("invalid message %d",
539 load_files(res, &ret);
542 if (imsg.hdr.type != IMSG_CTL_MONITOR)
543 fatalx("invalid message %d",
546 if (datalen != sizeof(type))
547 fatalx("data size mismatch");
549 memcpy(&type, imsg.data, sizeof(type));
550 if (type < 0 || type > IMSG__LAST)
551 fatalx("received corrupted data");
553 if (!res->monitor[type])
556 puts(event_name(type));
573 ctl_noarg(struct parse_result *res, int argc, char **argv)
577 while ((ch = getopt(argc, argv, "")) != -1)
585 return ctlaction(res);
589 ctl_add(struct parse_result *res, int argc, char **argv)
593 while ((ch = getopt(argc, argv, "")) != -1)
602 return ctlaction(res);
606 ctl_show(struct parse_result *res, int argc, char **argv)
610 while ((ch = getopt(argc, argv, "p")) != -1) {
620 return ctlaction(res);
624 ctl_load(struct parse_result *res, int argc, char **argv)
628 while ((ch = getopt(argc, argv, "")) != -1)
638 if ((res->fp = fopen(argv[0], "r")) == NULL)
639 fatal("can't open %s", argv[0]);
642 return ctlaction(res);
646 ctl_jump(struct parse_result *res, int argc, char **argv)
650 while ((ch = getopt(argc, argv, "")) != -1)
659 return ctlaction(res);
663 parse_mode(struct parse_result *res, const char *v)
667 if (!strcmp(v, "on"))
669 if (!strcmp(v, "off"))
675 ctl_repeat(struct parse_result *res, int argc, char **argv)
679 while ((ch = getopt(argc, argv, "")) != -1)
684 if (argc != 1 && argc != 2)
687 if (!strcmp(argv[0], "one"))
688 res->mode.repeat_one = parse_mode(res, argv[1]);
689 else if (!strcmp(argv[0], "all"))
690 res->mode.repeat_all = parse_mode(res, argv[1]);
694 return ctlaction(res);
698 ctl_consume(struct parse_result *res, int argc, char **argv)
702 while ((ch = getopt(argc, argv, "")) != -1)
710 res->mode.consume = parse_mode(res, argv[0]);
711 return ctlaction(res);
715 ctl_monitor(struct parse_result *res, int argc, char **argv)
719 char *dup, *tmp, *tok;
721 while ((ch = getopt(argc, argv, "")) != -1)
729 events = "play,pause,stop,next,prev,jump,mode,add,load,seek";
733 tmp = dup = xstrdup(events);
734 while ((tok = strsep(&tmp, ",")) != NULL) {
739 if (!strcmp(tok, "play"))
740 res->monitor[IMSG_CTL_PLAY] = 1;
741 else if (!strcmp(tok, "pause"))
742 res->monitor[IMSG_CTL_PAUSE] = 1;
743 else if (!strcmp(tok, "stop"))
744 res->monitor[IMSG_CTL_STOP] = 1;
745 else if (!strcmp(tok, "next"))
746 res->monitor[IMSG_CTL_NEXT] = 1;
747 else if (!strcmp(tok, "prev"))
748 res->monitor[IMSG_CTL_PREV] = 1;
749 else if (!strcmp(tok, "jump"))
750 res->monitor[IMSG_CTL_JUMP] = 1;
751 else if (!strcmp(tok, "mode"))
752 res->monitor[IMSG_CTL_MODE] = 1;
753 else if (!strcmp(tok, "add"))
754 res->monitor[IMSG_CTL_ADD] = 1;
755 else if (!strcmp(tok, "load"))
756 res->monitor[IMSG_CTL_COMMIT] = 1;
757 else if (!strcmp(tok, "seek"))
758 res->monitor[IMSG_CTL_SEEK] = 1;
760 log_warnx("unknown event \"%s\"", tok);
768 return ctlaction(res);
772 ctl_seek(struct parse_result *res, int argc, char **argv)
776 int hours = 0, minutes = 0, seconds = 0;
780 /* skip the command name */
785 if (argc > 0 && !strcmp(*argv, "--")) {
794 if (*n == '-' || *n == '+')
795 res->seek.relative = 1;
801 seconds = strtol(n, &ep, 10);
803 (*ep != '\0' && *ep != ':' && *ep != '%') ||
804 (*ep == '%' && ep[1] != '\0'))
805 fatalx("invalid offset: %s", argv[0]);
806 if (*ep == '\0' || *ep == '%') {
807 res->seek.percent = *ep == '%';
813 seconds = strtol(n, &ep, 10);
814 if (n[0] == '\0' || (*ep != '\0' && *ep != ':'))
815 fatalx("invalid offset: %s", argv[0]);
822 seconds = strtol(n, &ep, 10);
823 if (n[0] == '\0' || *ep != '\0')
824 fatalx("invalid offset: %s", argv[0]);
827 res->seek.offset = sign * (hours * 3600 + minutes * 60 + seconds);
828 return ctlaction(res);
832 ctl_status(struct parse_result *res, int argc, char **argv)
836 while ((ch = getopt(argc, argv, "f:")) != -1) {
839 res->status_format = optarg;
851 return ctlaction(res);
855 ctl_get_lock(const char *lockfile)
859 if ((lockfd = open(lockfile, O_WRONLY|O_CREAT, 0600)) == -1) {
860 log_debug("open failed: %s", strerror(errno));
864 if (flock(lockfd, LOCK_EX|LOCK_NB) == -1) {
865 log_debug("flock failed: %s", strerror(errno));
868 while (flock(lockfd, LOCK_EX) == -1 && errno == EINTR)
873 log_debug("flock succeeded");
881 struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
882 struct sockaddr_un sa;
884 int fd, lockfd = -1, locked = 0, spawned = 0;
886 char *lockfile = NULL;
888 memset(&sa, 0, sizeof(sa));
889 sa.sun_family = AF_UNIX;
890 size = strlcpy(sa.sun_path, csock, sizeof(sa.sun_path));
891 if (size >= sizeof(sa.sun_path)) {
892 errno = ENAMETOOLONG;
897 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
900 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
901 log_debug("connection failed: %s", strerror(errno));
902 if (errno != ECONNREFUSED && errno != ENOENT)
904 if (attempt++ == 100)
909 xasprintf(&lockfile, "%s.lock", csock);
910 if ((lockfd = ctl_get_lock(lockfile)) < 0) {
911 log_debug("didn't get the lock (%d)", lockfd);
921 * Always retry at least once, even if we got
922 * the lock, because another client could have
923 * taken the lock, started the server and released
924 * the lock between our connect() and flock()
931 log_debug("spawning the daemon");
936 nanosleep(&ts, NULL);
940 if (locked && lockfd >= 0) {
957 ctl(int argc, char **argv)
959 struct parse_result res;
963 memset(&res, 0, sizeof(res));
964 if ((fmt = getenv("AMUSED_STATUS_FORMAT")) == NULL)
965 fmt = "status,time:oneline,mode:oneline";
966 res.status_format = fmt;
968 res.mode.consume = MODE_UNDEF;
969 res.mode.repeat_all = MODE_UNDEF;
970 res.mode.repeat_one = MODE_UNDEF;
972 log_init(1, LOG_DAEMON);
973 log_setverbose(verbose);
975 if (getcwd(cwd, sizeof(cwd)) == NULL)
978 if ((ctl_sock = ctl_connect()) == -1)
979 fatal("can't connect");
982 fatalx("failed to connect to the daemon");
984 ibuf = xmalloc(sizeof(*ibuf));
985 imsg_init(ibuf, ctl_sock);
990 /* we'll drop rpath too later in ctlaction */
991 if (pledge("stdio rpath", NULL) == -1)
994 exit(parse(&res, argc, argv));