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:
258 case IMSG_CTL_RESTART:
268 case IMSG_CTL_REPEAT:
272 case IMSG_CTL_COMMIT:
280 print_time(const char *label, int64_t seconds)
287 hours = seconds / 3600;
288 seconds -= hours * 3600;
290 minutes = seconds / 60;
291 seconds -= minutes * 60;
293 printf("%s ", label);
295 printf("%02d:", hours);
296 printf("%02d:%02lld\n", minutes, (long long)seconds);
300 ctlaction(struct parse_result *res)
304 struct player_status ps;
307 int i, type, ret = 0, done = 1;
309 switch (res->action) {
311 imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
313 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
319 imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
322 imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
324 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
330 imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
333 imsg_compose(ibuf, IMSG_CTL_RESTART, 0, 0, -1, NULL, 0);
335 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
342 for (i = 0; res->files[i] != NULL; ++i) {
343 memset(path, 0, sizeof(path));
344 if (canonpath(res->files[i], path, sizeof(path))
346 log_warn("canonpath %s", res->files[i]);
350 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
356 imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
360 imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
364 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
367 imsg_compose(ibuf, IMSG_CTL_NEXT, 0, 0, -1, NULL, 0);
369 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
375 imsg_compose(ibuf, IMSG_CTL_PREV, 0, 0, -1, NULL, 0);
377 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
384 imsg_compose(ibuf, IMSG_CTL_BEGIN, 0, 0, -1, NULL, 0);
388 memset(path, 0, sizeof(path));
389 strlcpy(path, res->file, sizeof(path));
390 imsg_compose(ibuf, IMSG_CTL_JUMP, 0, 0, -1,
394 imsg_compose(ibuf, IMSG_CTL_REPEAT, 0, 0, -1,
395 &res->rep, sizeof(res->rep));
399 imsg_compose(ibuf, IMSG_CTL_MONITOR, 0, 0, -1,
403 imsg_compose(ibuf, IMSG_CTL_SEEK, 0, 0, -1, &res->seek,
407 /* action not expected */
408 fatalx("invalid action %u", res->action);
419 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
420 fatalx("imsg_read error");
422 fatalx("pipe closed");
425 if ((n = imsg_get(ibuf, &imsg)) == -1)
426 fatalx("imsg_get error");
430 if (imsg.hdr.type == IMSG_CTL_ERR) {
431 log_warnx("%s: %s", res->ctl->name,
432 imsg_strerror(&imsg));
438 datalen = IMSG_DATA_SIZE(imsg);
440 switch (res->action) {
442 if (res->files[i] == NULL)
443 fatalx("received more replies than "
446 if (imsg.hdr.type == IMSG_CTL_ADD)
447 log_debug("enqueued %s", res->files[i]);
449 fatalx("invalid message %d",
452 done = res->files[i] == NULL;
459 if (datalen != sizeof(ps))
460 fatalx("data size mismatch");
461 memcpy(&ps, imsg.data, sizeof(ps));
462 if (ps.path[sizeof(ps.path) - 1] != '\0')
463 fatalx("received corrupted data");
466 if (ps.status == STATE_PLAYING)
479 if (imsg.hdr.type != IMSG_CTL_STATUS)
480 fatalx("invalid message %d",
483 if (datalen != sizeof(ps))
484 fatalx("data size mismatch");
485 memcpy(&ps, imsg.data, sizeof(ps));
486 if (ps.path[sizeof(ps.path) - 1] != '\0')
487 fatalx("received corrupted data");
489 if (ps.status == STATE_STOPPED)
491 else if (ps.status == STATE_PLAYING)
493 else if (ps.status == STATE_PAUSED)
500 print_time("position", ps.position);
501 print_time("duration", ps.duration);
503 printf("repeat one %s\nrepeat all %s\n",
504 ps.rp.repeat_one ? "on" : "off",
505 ps.rp.repeat_all ? "on" : "off");
510 if (imsg.hdr.type == IMSG_CTL_ADD)
512 if (imsg.hdr.type == IMSG_CTL_COMMIT) {
517 if (imsg.hdr.type != IMSG_CTL_BEGIN)
518 fatalx("invalid message %d",
521 load_files(res, &ret);
524 if (imsg.hdr.type != IMSG_CTL_MONITOR)
525 fatalx("invalid message %d",
528 if (datalen != sizeof(type))
529 fatalx("data size mismatch");
531 memcpy(&type, imsg.data, sizeof(type));
532 if (type < 0 || type > IMSG__LAST)
533 fatalx("received corrupted data");
535 if (!res->monitor[type])
538 puts(imsg_name(type));
555 ctl_noarg(struct parse_result *res, int argc, char **argv)
559 while ((ch = getopt(argc, argv, "")) != -1)
567 return ctlaction(res);
571 ctl_add(struct parse_result *res, int argc, char **argv)
575 while ((ch = getopt(argc, argv, "")) != -1)
584 return ctlaction(res);
588 ctl_show(struct parse_result *res, int argc, char **argv)
592 while ((ch = getopt(argc, argv, "p")) != -1) {
602 return ctlaction(res);
606 ctl_load(struct parse_result *res, int argc, char **argv)
610 while ((ch = getopt(argc, argv, "")) != -1)
622 if (pledge("stdio rpath", NULL) == -1)
625 return ctlaction(res);
629 ctl_jump(struct parse_result *res, int argc, char **argv)
633 while ((ch = getopt(argc, argv, "")) != -1)
642 return ctlaction(res);
646 ctl_repeat(struct parse_result *res, int argc, char **argv)
650 while ((ch = getopt(argc, argv, "")) != -1)
658 if (!strcmp(argv[1], "on"))
660 else if (!strcmp(argv[1], "off"))
665 res->rep.repeat_one = -1;
666 res->rep.repeat_all = -1;
667 if (!strcmp(argv[0], "one"))
668 res->rep.repeat_one = b;
669 else if (!strcmp(argv[0], "all"))
670 res->rep.repeat_all = b;
674 return ctlaction(res);
678 ctl_monitor(struct parse_result *res, int argc, char **argv)
682 char *dup, *tmp, *tok;
684 while ((ch = getopt(argc, argv, "")) != -1)
695 events = "play,toggle,pause,stop,restart,flush,next,prev,"
696 "jump,repeat,add,load";
698 tmp = dup = xstrdup(events);
699 while ((tok = strsep(&tmp, ",")) != NULL) {
703 if (!strcmp(tok, "play"))
704 res->monitor[IMSG_CTL_PLAY] = 1;
705 else if (!strcmp(tok, "toggle"))
706 res->monitor[IMSG_CTL_TOGGLE_PLAY] = 1;
707 else if (!strcmp(tok, "pause"))
708 res->monitor[IMSG_CTL_PAUSE] = 1;
709 else if (!strcmp(tok, "stop"))
710 res->monitor[IMSG_CTL_STOP] = 1;
711 else if (!strcmp(tok, "restart"))
712 res->monitor[IMSG_CTL_RESTART] = 1;
713 else if (!strcmp(tok, "flush"))
714 res->monitor[IMSG_CTL_FLUSH] = 1;
715 else if (!strcmp(tok, "next"))
716 res->monitor[IMSG_CTL_NEXT] = 1;
717 else if (!strcmp(tok, "prev"))
718 res->monitor[IMSG_CTL_PREV] = 1;
719 else if (!strcmp(tok, "jump"))
720 res->monitor[IMSG_CTL_JUMP] = 1;
721 else if (!strcmp(tok, "repeat"))
722 res->monitor[IMSG_CTL_REPEAT] = 1;
723 else if (!strcmp(tok, "add"))
724 res->monitor[IMSG_CTL_ADD] = 1;
725 else if (!strcmp(tok, "load"))
726 res->monitor[IMSG_CTL_COMMIT] = 1;
728 fatalx("unknown event \"%s\"", tok);
732 return ctlaction(res);
736 ctl_seek(struct parse_result *res, int argc, char **argv)
738 const char *n, *errstr;
741 /* skip the command name */
746 if (argc > 0 && !strcmp(*argv, "--")) {
755 if (*n == '-' || *n == '+')
756 res->seek.relative = 1;
758 res->seek.offset = strtonum(n, INT64_MIN, INT64_MAX, &errstr);
760 fatalx("offset is %s: %s", errstr, n);
762 return ctlaction(res);
766 ctl_get_lock(const char *lockfile)
770 if ((lockfd = open(lockfile, O_WRONLY|O_CREAT, 0600)) == -1) {
771 log_debug("open failed: %s", strerror(errno));
775 if (flock(lockfd, LOCK_EX|LOCK_NB) == -1) {
776 log_debug("flock failed: %s", strerror(errno));
779 while (flock(lockfd, LOCK_EX) == -1 && errno == EINTR)
784 log_debug("flock succeeded");
792 struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
793 struct sockaddr_un sa;
795 int fd, lockfd = -1, locked = 0, spawned = 0;
797 char *lockfile = NULL;
799 memset(&sa, 0, sizeof(sa));
800 sa.sun_family = AF_UNIX;
801 size = strlcpy(sa.sun_path, csock, sizeof(sa.sun_path));
802 if (size >= sizeof(sa.sun_path)) {
803 errno = ENAMETOOLONG;
808 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
811 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
812 log_debug("connection failed: %s", strerror(errno));
813 if (errno != ECONNREFUSED && errno != ENOENT)
815 if (attempt++ == 100)
820 xasprintf(&lockfile, "%s.lock", csock);
821 if ((lockfd = ctl_get_lock(lockfile)) < 0) {
822 log_debug("didn't get the lock (%d)", lockfd);
832 * Always retry at least once, even if we got
833 * the lock, because another client could have
834 * taken the lock, started the server and released
835 * the lock between our connect() and flock()
842 log_debug("spawning the daemon");
847 nanosleep(&ts, NULL);
851 if (locked && lockfd >= 0) {
868 ctl(int argc, char **argv)
872 log_init(1, LOG_DAEMON);
873 log_setverbose(verbose);
875 if (getcwd(cwd, sizeof(cwd)) == NULL)
878 if ((ctl_sock = ctl_connect()) == -1)
879 fatal("can't connect");
882 fatalx("failed to connect to the daemon");
884 ibuf = xmalloc(sizeof(*ibuf));
885 imsg_init(ibuf, ctl_sock);
890 exit(parse(argc, argv));