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.
18 #include <sys/types.h>
19 #include <sys/socket.h>
20 #include <sys/queue.h>
41 static struct imsgbuf *ibuf;
44 static int ctl_noarg(struct parse_result *, int, char **);
45 static int ctl_add(struct parse_result *, int, char **);
46 static int ctl_show(struct parse_result *, int, char **);
47 static int ctl_load(struct parse_result *, int, char **);
48 static int ctl_jump(struct parse_result *, int, char **);
49 static int ctl_repeat(struct parse_result *, int, char **);
50 static int ctl_monitor(struct parse_result *, int, char **);
51 static int ctl_seek(struct parse_result *, int, char **);
53 struct ctl_command ctl_commands[] = {
54 { "add", ADD, ctl_add, "files...", 0 },
55 { "flush", FLUSH, ctl_noarg, "", 0 },
56 { "jump", JUMP, ctl_jump, "pattern", 0 },
57 { "load", LOAD, ctl_load, "[file]", 1 },
58 { "monitor", MONITOR, ctl_monitor, "[events]", 0 },
59 { "next", NEXT, ctl_noarg, "", 0 },
60 { "pause", PAUSE, ctl_noarg, "", 0 },
61 { "play", PLAY, ctl_noarg, "", 0 },
62 { "prev", PREV, ctl_noarg, "", 0 },
63 { "repeat", REPEAT, ctl_repeat, "one|all on|off", 0 },
64 { "restart", RESTART, ctl_noarg, "", 0 },
65 { "seek", SEEK, ctl_seek, "[+-]time", 0 },
66 { "show", SHOW, ctl_show, "[-p]", 0 },
67 { "status", STATUS, ctl_noarg, "", 0 },
68 { "stop", STOP, ctl_noarg, "", 0 },
69 { "toggle", TOGGLE, ctl_noarg, "", 0 },
70 { NULL, 0, NULL, NULL, 0 },
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(int argc, char **argv)
137 struct ctl_command *ctl = NULL;
138 struct parse_result res;
142 memset(&res, 0, sizeof(res));
144 if ((argv0 = argv[0]) == NULL)
147 for (i = 0; ctl_commands[i].name != NULL; ++i) {
148 if (strncmp(ctl_commands[i].name, argv0, strlen(argv0))
152 "ambiguous argument: %s\n", argv0);
155 ctl = &ctl_commands[i];
160 fprintf(stderr, "unknown argument: %s\n", argv[0]);
164 res.action = ctl->action;
167 if (!ctl->has_pledge) {
168 /* pledge(2) default if command doesn't have its own */
169 if (pledge("stdio", NULL) == -1)
173 status = ctl->main(&res, argc, argv);
180 load_files(struct parse_result *res, int *ret)
186 size_t linesize = 0, i = 0;
187 ssize_t linelen, curr = -1;
189 if (res->file == NULL)
191 else if ((f = fopen(res->file, "r")) == NULL) {
192 log_warn("can't open %s", res->file);
197 while ((linelen = getline(&line, &linesize, f)) != -1) {
200 line[linelen-1] = '\0';
202 if (!strncmp(file, "> ", 2)) {
205 } else if (!strncmp(file, " ", 2))
208 memset(path, 0, sizeof(path));
209 if (canonpath(file, path, sizeof(path)) == -1) {
210 log_warn("canonpath %s", file);
215 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
229 imsg_compose(ibuf, IMSG_CTL_COMMIT, 0, 0, -1,
230 &curr, sizeof(curr));
236 imsg_strerror(struct imsg *imsg)
241 datalen = IMSG_DATA_SIZE(*imsg);
243 if (datalen == 0 || msg[datalen-1] != '\0')
244 fatalx("malformed error message");
255 case IMSG_CTL_TOGGLE_PLAY:
261 case IMSG_CTL_RESTART:
271 case IMSG_CTL_REPEAT:
275 case IMSG_CTL_COMMIT:
283 print_time(const char *label, int64_t seconds)
290 hours = seconds / 3600;
291 seconds -= hours * 3600;
293 minutes = seconds / 60;
294 seconds -= minutes * 60;
296 printf("%s ", label);
298 printf("%02d:", hours);
299 printf("%02d:%02lld\n", minutes, (long long)seconds);
303 ctlaction(struct parse_result *res)
307 struct player_status ps;
310 int i, type, ret = 0, done = 1;
312 switch (res->action) {
314 imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
316 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
322 imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
325 imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
327 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
333 imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
336 imsg_compose(ibuf, IMSG_CTL_RESTART, 0, 0, -1, NULL, 0);
338 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
345 for (i = 0; res->files[i] != NULL; ++i) {
346 memset(path, 0, sizeof(path));
347 if (canonpath(res->files[i], path, sizeof(path))
349 log_warn("canonpath %s", res->files[i]);
353 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
359 imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
363 imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
367 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
370 imsg_compose(ibuf, IMSG_CTL_NEXT, 0, 0, -1, NULL, 0);
372 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
378 imsg_compose(ibuf, IMSG_CTL_PREV, 0, 0, -1, NULL, 0);
380 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
387 imsg_compose(ibuf, IMSG_CTL_BEGIN, 0, 0, -1, NULL, 0);
391 memset(path, 0, sizeof(path));
392 strlcpy(path, res->file, sizeof(path));
393 imsg_compose(ibuf, IMSG_CTL_JUMP, 0, 0, -1,
397 imsg_compose(ibuf, IMSG_CTL_REPEAT, 0, 0, -1,
398 &res->rep, sizeof(res->rep));
402 imsg_compose(ibuf, IMSG_CTL_MONITOR, 0, 0, -1,
406 imsg_compose(ibuf, IMSG_CTL_SEEK, 0, 0, -1, &res->seek,
410 /* action not expected */
411 fatalx("invalid action %u", res->action);
422 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
423 fatalx("imsg_read error");
425 fatalx("pipe closed");
428 if ((n = imsg_get(ibuf, &imsg)) == -1)
429 fatalx("imsg_get error");
433 if (imsg.hdr.type == IMSG_CTL_ERR) {
434 log_warnx("%s: %s", res->ctl->name,
435 imsg_strerror(&imsg));
441 datalen = IMSG_DATA_SIZE(imsg);
443 switch (res->action) {
445 if (res->files[i] == NULL)
446 fatalx("received more replies than "
449 if (imsg.hdr.type == IMSG_CTL_ADD)
450 log_debug("enqueued %s", res->files[i]);
452 fatalx("invalid message %d",
455 done = res->files[i] == NULL;
462 if (datalen != sizeof(ps))
463 fatalx("data size mismatch");
464 memcpy(&ps, imsg.data, sizeof(ps));
465 if (ps.path[sizeof(ps.path) - 1] != '\0')
466 fatalx("received corrupted data");
469 if (ps.status == STATE_PLAYING)
482 if (imsg.hdr.type != IMSG_CTL_STATUS)
483 fatalx("invalid message %d",
486 if (datalen != sizeof(ps))
487 fatalx("data size mismatch");
488 memcpy(&ps, imsg.data, sizeof(ps));
489 if (ps.path[sizeof(ps.path) - 1] != '\0')
490 fatalx("received corrupted data");
492 if (ps.status == STATE_STOPPED)
494 else if (ps.status == STATE_PLAYING)
496 else if (ps.status == STATE_PAUSED)
503 print_time("position", ps.position);
504 print_time("duration", ps.duration);
506 printf("repeat one %s\nrepeat all %s\n",
507 ps.rp.repeat_one ? "on" : "off",
508 ps.rp.repeat_all ? "on" : "off");
513 if (imsg.hdr.type == IMSG_CTL_ADD)
515 if (imsg.hdr.type == IMSG_CTL_COMMIT) {
520 if (imsg.hdr.type != IMSG_CTL_BEGIN)
521 fatalx("invalid message %d",
524 load_files(res, &ret);
527 if (imsg.hdr.type != IMSG_CTL_MONITOR)
528 fatalx("invalid message %d",
531 if (datalen != sizeof(type))
532 fatalx("data size mismatch");
534 memcpy(&type, imsg.data, sizeof(type));
535 if (type < 0 || type > IMSG__LAST)
536 fatalx("received corrupted data");
538 if (!res->monitor[type])
541 puts(imsg_name(type));
558 ctl_noarg(struct parse_result *res, int argc, char **argv)
562 while ((ch = getopt(argc, argv, "")) != -1)
570 return ctlaction(res);
574 ctl_add(struct parse_result *res, int argc, char **argv)
578 while ((ch = getopt(argc, argv, "")) != -1)
587 return ctlaction(res);
591 ctl_show(struct parse_result *res, int argc, char **argv)
595 while ((ch = getopt(argc, argv, "p")) != -1) {
605 return ctlaction(res);
609 ctl_load(struct parse_result *res, int argc, char **argv)
613 while ((ch = getopt(argc, argv, "")) != -1)
625 if (pledge("stdio rpath", NULL) == -1)
628 return ctlaction(res);
632 ctl_jump(struct parse_result *res, int argc, char **argv)
636 while ((ch = getopt(argc, argv, "")) != -1)
645 return ctlaction(res);
649 ctl_repeat(struct parse_result *res, int argc, char **argv)
653 while ((ch = getopt(argc, argv, "")) != -1)
661 if (!strcmp(argv[1], "on"))
663 else if (!strcmp(argv[1], "off"))
668 res->rep.repeat_one = -1;
669 res->rep.repeat_all = -1;
670 if (!strcmp(argv[0], "one"))
671 res->rep.repeat_one = b;
672 else if (!strcmp(argv[0], "all"))
673 res->rep.repeat_all = b;
677 return ctlaction(res);
681 ctl_monitor(struct parse_result *res, int argc, char **argv)
685 char *dup, *tmp, *tok;
687 while ((ch = getopt(argc, argv, "")) != -1)
698 events = "play,toggle,pause,stop,restart,flush,next,prev,"
699 "jump,repeat,add,load";
701 tmp = dup = xstrdup(events);
702 while ((tok = strsep(&tmp, ",")) != NULL) {
706 if (!strcmp(tok, "play"))
707 res->monitor[IMSG_CTL_PLAY] = 1;
708 else if (!strcmp(tok, "toggle"))
709 res->monitor[IMSG_CTL_TOGGLE_PLAY] = 1;
710 else if (!strcmp(tok, "pause"))
711 res->monitor[IMSG_CTL_PAUSE] = 1;
712 else if (!strcmp(tok, "stop"))
713 res->monitor[IMSG_CTL_STOP] = 1;
714 else if (!strcmp(tok, "restart"))
715 res->monitor[IMSG_CTL_RESTART] = 1;
716 else if (!strcmp(tok, "flush"))
717 res->monitor[IMSG_CTL_FLUSH] = 1;
718 else if (!strcmp(tok, "next"))
719 res->monitor[IMSG_CTL_NEXT] = 1;
720 else if (!strcmp(tok, "prev"))
721 res->monitor[IMSG_CTL_PREV] = 1;
722 else if (!strcmp(tok, "jump"))
723 res->monitor[IMSG_CTL_JUMP] = 1;
724 else if (!strcmp(tok, "repeat"))
725 res->monitor[IMSG_CTL_REPEAT] = 1;
726 else if (!strcmp(tok, "add"))
727 res->monitor[IMSG_CTL_ADD] = 1;
728 else if (!strcmp(tok, "load"))
729 res->monitor[IMSG_CTL_COMMIT] = 1;
731 fatalx("unknown event \"%s\"", tok);
735 return ctlaction(res);
739 ctl_seek(struct parse_result *res, int argc, char **argv)
741 const char *n, *errstr;
744 /* skip the command name */
749 if (argc > 0 && !strcmp(*argv, "--")) {
758 if (*n == '-' || *n == '+')
759 res->seek.relative = 1;
761 res->seek.offset = strtonum(n, INT64_MIN, INT64_MAX, &errstr);
763 fatalx("offset is %s: %s", errstr, n);
765 return ctlaction(res);
769 ctl_get_lock(const char *lockfile)
773 if ((lockfd = open(lockfile, O_WRONLY|O_CREAT, 0600)) == -1) {
774 log_debug("open failed: %s", strerror(errno));
778 if (flock(lockfd, LOCK_EX|LOCK_NB) == -1) {
779 log_debug("flock failed: %s", strerror(errno));
782 while (flock(lockfd, LOCK_EX) == -1 && errno == EINTR)
787 log_debug("flock succeeded");
795 struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
796 struct sockaddr_un sa;
798 int fd, lockfd = -1, locked = 0, spawned = 0;
800 char *lockfile = NULL;
802 memset(&sa, 0, sizeof(sa));
803 sa.sun_family = AF_UNIX;
804 size = strlcpy(sa.sun_path, csock, sizeof(sa.sun_path));
805 if (size >= sizeof(sa.sun_path)) {
806 errno = ENAMETOOLONG;
811 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
814 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
815 log_debug("connection failed: %s", strerror(errno));
816 if (errno != ECONNREFUSED && errno != ENOENT)
818 if (attempt++ == 100)
823 xasprintf(&lockfile, "%s.lock", csock);
824 if ((lockfd = ctl_get_lock(lockfile)) < 0) {
825 log_debug("didn't get the lock (%d)", lockfd);
835 * Always retry at least once, even if we got
836 * the lock, because another client could have
837 * taken the lock, started the server and released
838 * the lock between our connect() and flock()
845 log_debug("spawning the daemon");
850 nanosleep(&ts, NULL);
854 if (locked && lockfd >= 0) {
871 ctl(int argc, char **argv)
875 log_init(1, LOG_DAEMON);
876 log_setverbose(verbose);
878 if (getcwd(cwd, sizeof(cwd)) == NULL)
881 if ((ctl_sock = ctl_connect()) == -1)
882 fatal("can't connect");
885 fatalx("failed to connect to the daemon");
887 ibuf = xmalloc(sizeof(*ibuf));
888 imsg_init(ibuf, ctl_sock);
893 exit(parse(argc, argv));