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_monitor(struct parse_result *, int, char **);
48 static int ctl_seek(struct parse_result *, int, char **);
50 struct ctl_command ctl_commands[] = {
51 { "add", ADD, ctl_add, "files...", 0 },
52 { "flush", FLUSH, ctl_noarg, "", 0 },
53 { "jump", JUMP, ctl_jump, "pattern", 0 },
54 { "load", LOAD, ctl_load, "[file]", 1 },
55 { "monitor", MONITOR, ctl_monitor, "[events]", 0 },
56 { "next", NEXT, ctl_noarg, "", 0 },
57 { "pause", PAUSE, ctl_noarg, "", 0 },
58 { "play", PLAY, ctl_noarg, "", 0 },
59 { "prev", PREV, ctl_noarg, "", 0 },
60 { "repeat", REPEAT, ctl_repeat, "one|all on|off", 0 },
61 { "restart", RESTART, ctl_noarg, "", 0 },
62 { "seek", SEEK, ctl_seek, "[+-]time[%]", 0 },
63 { "show", SHOW, ctl_show, "[-p]", 0 },
64 { "status", STATUS, ctl_noarg, "", 0 },
65 { "stop", STOP, ctl_noarg, "", 0 },
66 { "toggle", TOGGLE, ctl_noarg, "", 0 },
67 { NULL, 0, NULL, NULL, 0 },
73 fprintf(stderr, "usage: %s [-dv] [-s socket]\n", getprogname());
78 ctl_usage(struct ctl_command *ctl)
80 fprintf(stderr, "usage: %s [-v] [-s socket] %s %s\n", getprogname(),
81 ctl->name, ctl->usage);
85 /* based on canonpath from kern_pledge.c */
87 canonpath(const char *input, char *buf, size_t bufsize)
90 char *q, path[PATH_MAX];
92 if (input[0] != '/') {
93 if (snprintf(path, sizeof(path), "%s/%s", cwd, input)
103 while (*p && (q - buf < bufsize)) {
104 if (p[0] == '/' && (p[1] == '/' || p[1] == '\0')) {
107 } else if (p[0] == '/' && p[1] == '.' &&
108 (p[2] == '/' || p[2] == '\0')) {
111 } else if (p[0] == '/' && p[1] == '.' && p[2] == '.' &&
112 (p[3] == '/' || p[3] == '\0')) {
114 if (q != buf) /* "/../" at start of buf */
122 if ((*p == '\0') && (q - buf < bufsize)) {
126 errno = ENAMETOOLONG;
132 parse(int argc, char **argv)
134 struct ctl_command *ctl = NULL;
135 struct parse_result res;
139 memset(&res, 0, sizeof(res));
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 if (!ctl->has_pledge) {
165 /* pledge(2) default if command doesn't have its own */
166 if (pledge("stdio", NULL) == -1)
170 status = ctl->main(&res, argc, argv);
177 load_files(struct parse_result *res, int *ret)
183 size_t linesize = 0, i = 0;
184 ssize_t linelen, curr = -1;
186 if (res->file == NULL)
188 else if ((f = fopen(res->file, "r")) == NULL) {
189 log_warn("can't open %s", res->file);
194 while ((linelen = getline(&line, &linesize, f)) != -1) {
197 line[linelen-1] = '\0';
199 if (!strncmp(file, "> ", 2)) {
202 } else if (!strncmp(file, " ", 2))
205 memset(path, 0, sizeof(path));
206 if (canonpath(file, path, sizeof(path)) == -1) {
207 log_warn("canonpath %s", file);
212 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
226 imsg_compose(ibuf, IMSG_CTL_COMMIT, 0, 0, -1,
227 &curr, sizeof(curr));
233 imsg_strerror(struct imsg *imsg)
238 datalen = IMSG_DATA_SIZE(*imsg);
240 if (datalen == 0 || msg[datalen-1] != '\0')
241 fatalx("malformed error message");
252 case IMSG_CTL_TOGGLE_PLAY:
266 case IMSG_CTL_REPEAT:
270 case IMSG_CTL_COMMIT:
278 print_time(const char *label, int64_t seconds)
285 hours = seconds / 3600;
286 seconds -= hours * 3600;
288 minutes = seconds / 60;
289 seconds -= minutes * 60;
291 printf("%s ", label);
293 printf("%02d:", hours);
294 printf("%02d:%02lld\n", minutes, (long long)seconds);
298 ctlaction(struct parse_result *res)
302 struct player_status ps;
305 int i, type, ret = 0, done = 1;
307 switch (res->action) {
309 imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
311 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
317 imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
320 imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
322 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
328 imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
332 for (i = 0; res->files[i] != NULL; ++i) {
333 memset(path, 0, sizeof(path));
334 if (canonpath(res->files[i], path, sizeof(path))
336 log_warn("canonpath %s", res->files[i]);
340 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
346 imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
350 imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
354 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
357 imsg_compose(ibuf, IMSG_CTL_NEXT, 0, 0, -1, NULL, 0);
359 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
365 imsg_compose(ibuf, IMSG_CTL_PREV, 0, 0, -1, NULL, 0);
367 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
374 imsg_compose(ibuf, IMSG_CTL_BEGIN, 0, 0, -1, NULL, 0);
378 memset(path, 0, sizeof(path));
379 strlcpy(path, res->file, sizeof(path));
380 imsg_compose(ibuf, IMSG_CTL_JUMP, 0, 0, -1,
384 imsg_compose(ibuf, IMSG_CTL_REPEAT, 0, 0, -1,
385 &res->rep, sizeof(res->rep));
389 imsg_compose(ibuf, IMSG_CTL_MONITOR, 0, 0, -1,
393 memset(&res->seek, 0, sizeof(res->seek));
396 imsg_compose(ibuf, IMSG_CTL_SEEK, 0, 0, -1, &res->seek,
400 /* action not expected */
401 fatalx("invalid action %u", res->action);
412 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
413 fatalx("imsg_read error");
415 fatalx("pipe closed");
418 if ((n = imsg_get(ibuf, &imsg)) == -1)
419 fatalx("imsg_get error");
423 if (imsg.hdr.type == IMSG_CTL_ERR) {
424 log_warnx("%s: %s", res->ctl->name,
425 imsg_strerror(&imsg));
431 datalen = IMSG_DATA_SIZE(imsg);
433 switch (res->action) {
435 if (res->files[i] == NULL)
436 fatalx("received more replies than "
439 if (imsg.hdr.type == IMSG_CTL_ADD)
440 log_debug("enqueued %s", res->files[i]);
442 fatalx("invalid message %d",
445 done = res->files[i] == NULL;
452 if (datalen != sizeof(ps))
453 fatalx("data size mismatch");
454 memcpy(&ps, imsg.data, sizeof(ps));
455 if (ps.path[sizeof(ps.path) - 1] != '\0')
456 fatalx("received corrupted data");
459 if (ps.status == STATE_PLAYING)
472 if (imsg.hdr.type != IMSG_CTL_STATUS)
473 fatalx("invalid message %d",
476 if (datalen != sizeof(ps))
477 fatalx("data size mismatch");
478 memcpy(&ps, imsg.data, sizeof(ps));
479 if (ps.path[sizeof(ps.path) - 1] != '\0')
480 fatalx("received corrupted data");
482 if (ps.status == STATE_STOPPED)
484 else if (ps.status == STATE_PLAYING)
486 else if (ps.status == STATE_PAUSED)
493 print_time("position", ps.position);
494 print_time("duration", ps.duration);
496 printf("repeat one %s\nrepeat all %s\n",
497 ps.rp.repeat_one ? "on" : "off",
498 ps.rp.repeat_all ? "on" : "off");
503 if (imsg.hdr.type == IMSG_CTL_ADD)
505 if (imsg.hdr.type == IMSG_CTL_COMMIT) {
510 if (imsg.hdr.type != IMSG_CTL_BEGIN)
511 fatalx("invalid message %d",
514 load_files(res, &ret);
517 if (imsg.hdr.type != IMSG_CTL_MONITOR)
518 fatalx("invalid message %d",
521 if (datalen != sizeof(type))
522 fatalx("data size mismatch");
524 memcpy(&type, imsg.data, sizeof(type));
525 if (type < 0 || type > IMSG__LAST)
526 fatalx("received corrupted data");
528 if (!res->monitor[type])
531 puts(imsg_name(type));
548 ctl_noarg(struct parse_result *res, int argc, char **argv)
552 while ((ch = getopt(argc, argv, "")) != -1)
560 return ctlaction(res);
564 ctl_add(struct parse_result *res, int argc, char **argv)
568 while ((ch = getopt(argc, argv, "")) != -1)
577 return ctlaction(res);
581 ctl_show(struct parse_result *res, int argc, char **argv)
585 while ((ch = getopt(argc, argv, "p")) != -1) {
595 return ctlaction(res);
599 ctl_load(struct parse_result *res, int argc, char **argv)
603 while ((ch = getopt(argc, argv, "")) != -1)
615 if (pledge("stdio rpath", NULL) == -1)
618 return ctlaction(res);
622 ctl_jump(struct parse_result *res, int argc, char **argv)
626 while ((ch = getopt(argc, argv, "")) != -1)
635 return ctlaction(res);
639 ctl_repeat(struct parse_result *res, int argc, char **argv)
643 while ((ch = getopt(argc, argv, "")) != -1)
651 if (!strcmp(argv[1], "on"))
653 else if (!strcmp(argv[1], "off"))
658 res->rep.repeat_one = -1;
659 res->rep.repeat_all = -1;
660 if (!strcmp(argv[0], "one"))
661 res->rep.repeat_one = b;
662 else if (!strcmp(argv[0], "all"))
663 res->rep.repeat_all = b;
667 return ctlaction(res);
671 ctl_monitor(struct parse_result *res, int argc, char **argv)
675 char *dup, *tmp, *tok;
677 while ((ch = getopt(argc, argv, "")) != -1)
688 events = "play,toggle,pause,stop,restart,flush,next,prev,"
689 "jump,repeat,add,load";
691 tmp = dup = xstrdup(events);
692 while ((tok = strsep(&tmp, ",")) != NULL) {
696 if (!strcmp(tok, "play"))
697 res->monitor[IMSG_CTL_PLAY] = 1;
698 else if (!strcmp(tok, "toggle"))
699 res->monitor[IMSG_CTL_TOGGLE_PLAY] = 1;
700 else if (!strcmp(tok, "pause"))
701 res->monitor[IMSG_CTL_PAUSE] = 1;
702 else if (!strcmp(tok, "stop"))
703 res->monitor[IMSG_CTL_STOP] = 1;
704 else if (!strcmp(tok, "restart")) /* compat */
705 res->monitor[IMSG_CTL_SEEK] = 1;
706 else if (!strcmp(tok, "flush"))
707 res->monitor[IMSG_CTL_FLUSH] = 1;
708 else if (!strcmp(tok, "next"))
709 res->monitor[IMSG_CTL_NEXT] = 1;
710 else if (!strcmp(tok, "prev"))
711 res->monitor[IMSG_CTL_PREV] = 1;
712 else if (!strcmp(tok, "jump"))
713 res->monitor[IMSG_CTL_JUMP] = 1;
714 else if (!strcmp(tok, "repeat"))
715 res->monitor[IMSG_CTL_REPEAT] = 1;
716 else if (!strcmp(tok, "add"))
717 res->monitor[IMSG_CTL_ADD] = 1;
718 else if (!strcmp(tok, "load"))
719 res->monitor[IMSG_CTL_COMMIT] = 1;
720 else if (!strcmp(tok, "seek"))
721 res->monitor[IMSG_CTL_SEEK] = 1;
723 fatalx("unknown event \"%s\"", tok);
727 return ctlaction(res);
731 ctl_seek(struct parse_result *res, int argc, char **argv)
735 int hours = 0, minutes = 0, seconds = 0;
739 /* skip the command name */
744 if (argc > 0 && !strcmp(*argv, "--")) {
753 if (*n == '-' || *n == '+')
754 res->seek.relative = 1;
760 seconds = strtol(n, &ep, 10);
762 (*ep != '\0' && *ep != ':' && *ep != '%') ||
763 (*ep == '%' && ep[1] != '\0'))
764 fatalx("invalid offset: %s", argv[0]);
765 if (*ep == '\0' || *ep == '%') {
766 res->seek.percent = *ep == '%';
772 seconds = strtol(n, &ep, 10);
773 if (n[0] == '\0' || (*ep != '\0' && *ep != ':'))
774 fatalx("invalid offset: %s", argv[0]);
781 seconds = strtol(n, &ep, 10);
782 if (n[0] == '\0' || *ep != '\0')
783 fatalx("invalid offset: %s", argv[0]);
786 res->seek.offset = sign * (hours * 3600 + minutes * 60 + seconds);
787 return ctlaction(res);
791 ctl_get_lock(const char *lockfile)
795 if ((lockfd = open(lockfile, O_WRONLY|O_CREAT, 0600)) == -1) {
796 log_debug("open failed: %s", strerror(errno));
800 if (flock(lockfd, LOCK_EX|LOCK_NB) == -1) {
801 log_debug("flock failed: %s", strerror(errno));
804 while (flock(lockfd, LOCK_EX) == -1 && errno == EINTR)
809 log_debug("flock succeeded");
817 struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
818 struct sockaddr_un sa;
820 int fd, lockfd = -1, locked = 0, spawned = 0;
822 char *lockfile = NULL;
824 memset(&sa, 0, sizeof(sa));
825 sa.sun_family = AF_UNIX;
826 size = strlcpy(sa.sun_path, csock, sizeof(sa.sun_path));
827 if (size >= sizeof(sa.sun_path)) {
828 errno = ENAMETOOLONG;
833 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
836 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
837 log_debug("connection failed: %s", strerror(errno));
838 if (errno != ECONNREFUSED && errno != ENOENT)
840 if (attempt++ == 100)
845 xasprintf(&lockfile, "%s.lock", csock);
846 if ((lockfd = ctl_get_lock(lockfile)) < 0) {
847 log_debug("didn't get the lock (%d)", lockfd);
857 * Always retry at least once, even if we got
858 * the lock, because another client could have
859 * taken the lock, started the server and released
860 * the lock between our connect() and flock()
867 log_debug("spawning the daemon");
872 nanosleep(&ts, NULL);
876 if (locked && lockfd >= 0) {
893 ctl(int argc, char **argv)
897 log_init(1, LOG_DAEMON);
898 log_setverbose(verbose);
900 if (getcwd(cwd, sizeof(cwd)) == NULL)
903 if ((ctl_sock = ctl_connect()) == -1)
904 fatal("can't connect");
907 fatalx("failed to connect to the daemon");
909 ibuf = xmalloc(sizeof(*ibuf));
910 imsg_init(ibuf, ctl_sock);
915 exit(parse(argc, argv));