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 int ctl_noarg(struct parse_result *, int, char **);
45 int ctl_add(struct parse_result *, int, char **);
46 int ctl_show(struct parse_result *, int, char **);
47 int ctl_load(struct parse_result *, int, char **);
48 int ctl_jump(struct parse_result *, int, char **);
49 int ctl_repeat(struct parse_result *, int, char **);
50 int ctl_monitor(struct parse_result *, int, char **);
52 struct ctl_command ctl_commands[] = {
53 { "play", PLAY, ctl_noarg, "" },
54 { "pause", PAUSE, ctl_noarg, "" },
55 { "toggle", TOGGLE, ctl_noarg, "" },
56 { "stop", STOP, ctl_noarg, "" },
57 { "restart", RESTART, ctl_noarg, "" },
58 { "add", ADD, ctl_add, "files..." },
59 { "flush", FLUSH, ctl_noarg, "" },
60 { "show", SHOW, ctl_show, "[-p]" },
61 { "status", STATUS, ctl_noarg, "" },
62 { "next", NEXT, ctl_noarg, "" },
63 { "prev", PREV, ctl_noarg, "" },
64 { "load", LOAD, ctl_load, "[file]", 1 },
65 { "jump", JUMP, ctl_jump, "pattern" },
66 { "repeat", REPEAT, ctl_repeat, "one|all on|off" },
67 { "monitor", MONITOR, ctl_monitor, "[events]" },
74 fprintf(stderr, "usage: %s [-dv] [-s socket]\n", getprogname());
79 ctl_usage(struct ctl_command *ctl)
81 fprintf(stderr, "usage: %s [-v] [-s socket] %s %s\n", getprogname(),
82 ctl->name, ctl->usage);
86 /* based on canonpath from kern_pledge.c */
88 canonpath(const char *input, char *buf, size_t bufsize)
91 char *q, path[PATH_MAX];
93 if (input[0] != '/') {
94 if (snprintf(path, sizeof(path), "%s/%s", cwd, input)
104 while (*p && (q - buf < bufsize)) {
105 if (p[0] == '/' && (p[1] == '/' || p[1] == '\0')) {
108 } else if (p[0] == '/' && p[1] == '.' &&
109 (p[2] == '/' || p[2] == '\0')) {
112 } else if (p[0] == '/' && p[1] == '.' && p[2] == '.' &&
113 (p[3] == '/' || p[3] == '\0')) {
115 if (q != buf) /* "/../" at start of buf */
123 if ((*p == '\0') && (q - buf < bufsize)) {
127 errno = ENAMETOOLONG;
133 parse(int argc, char **argv)
135 struct ctl_command *ctl = NULL;
136 struct parse_result res;
140 memset(&res, 0, sizeof(res));
142 if ((argv0 = argv[0]) == NULL)
145 for (i = 0; ctl_commands[i].name != NULL; ++i) {
146 if (strncmp(ctl_commands[i].name, argv0, strlen(argv0))
150 "ambiguous argument: %s\n", argv0);
153 ctl = &ctl_commands[i];
158 fprintf(stderr, "unknown argument: %s\n", argv[0]);
162 res.action = ctl->action;
165 if (!ctl->has_pledge) {
166 /* pledge(2) default if command doesn't have its own */
167 if (pledge("stdio", NULL) == -1)
171 status = ctl->main(&res, argc, argv);
178 load_files(struct parse_result *res, int *ret)
184 size_t linesize = 0, i = 0;
185 ssize_t linelen, curr = -1;
187 if (res->file == NULL)
189 else if ((f = fopen(res->file, "r")) == NULL) {
190 log_warn("can't open %s", res->file);
195 while ((linelen = getline(&line, &linesize, f)) != -1) {
198 line[linelen-1] = '\0';
200 if (!strncmp(file, "> ", 2)) {
203 } else if (!strncmp(file, " ", 2))
206 memset(path, 0, sizeof(path));
207 if (canonpath(file, path, sizeof(path)) == -1) {
208 log_warn("canonpath %s", file);
213 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
227 imsg_compose(ibuf, IMSG_CTL_COMMIT, 0, 0, -1,
228 &curr, sizeof(curr));
234 imsg_strerror(struct imsg *imsg)
239 datalen = IMSG_DATA_SIZE(*imsg);
241 if (datalen == 0 || msg[datalen-1] != '\0')
242 fatalx("malformed error message");
253 case IMSG_CTL_TOGGLE_PLAY:
259 case IMSG_CTL_RESTART:
269 case IMSG_CTL_REPEAT:
273 case IMSG_CTL_COMMIT:
281 print_time(const char *label, int64_t seconds)
288 hours = seconds / 3600;
289 seconds -= hours * 3600;
291 minutes = seconds / 60;
292 seconds -= minutes * 60;
294 printf("%s ", label);
296 printf("%02d:", hours);
297 printf("%02d:%02lld\n", minutes, (long long)seconds);
301 ctlaction(struct parse_result *res)
305 struct player_status ps;
308 int i, type, ret = 0, done = 1;
310 switch (res->action) {
312 imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
314 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
320 imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
323 imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
325 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
331 imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
334 imsg_compose(ibuf, IMSG_CTL_RESTART, 0, 0, -1, NULL, 0);
336 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
343 for (i = 0; res->files[i] != NULL; ++i) {
344 memset(path, 0, sizeof(path));
345 if (canonpath(res->files[i], path, sizeof(path))
347 log_warn("canonpath %s", res->files[i]);
351 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
357 imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
361 imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
365 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
368 imsg_compose(ibuf, IMSG_CTL_NEXT, 0, 0, -1, NULL, 0);
370 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
376 imsg_compose(ibuf, IMSG_CTL_PREV, 0, 0, -1, NULL, 0);
378 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
385 imsg_compose(ibuf, IMSG_CTL_BEGIN, 0, 0, -1, NULL, 0);
389 memset(path, 0, sizeof(path));
390 strlcpy(path, res->file, sizeof(path));
391 imsg_compose(ibuf, IMSG_CTL_JUMP, 0, 0, -1,
395 imsg_compose(ibuf, IMSG_CTL_REPEAT, 0, 0, -1,
396 &res->rep, sizeof(res->rep));
400 imsg_compose(ibuf, IMSG_CTL_MONITOR, 0, 0, -1,
404 /* action not expected */
405 fatalx("invalid action %u", res->action);
416 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
417 fatalx("imsg_read error");
419 fatalx("pipe closed");
422 if ((n = imsg_get(ibuf, &imsg)) == -1)
423 fatalx("imsg_get error");
427 if (imsg.hdr.type == IMSG_CTL_ERR) {
428 log_warnx("%s: %s", res->ctl->name,
429 imsg_strerror(&imsg));
435 datalen = IMSG_DATA_SIZE(imsg);
437 switch (res->action) {
439 if (res->files[i] == NULL)
440 fatalx("received more replies than "
443 if (imsg.hdr.type == IMSG_CTL_ADD)
444 log_debug("enqueued %s", res->files[i]);
446 fatalx("invalid message %d",
449 done = res->files[i] == NULL;
456 if (datalen != sizeof(ps))
457 fatalx("data size mismatch");
458 memcpy(&ps, imsg.data, sizeof(ps));
459 if (ps.path[sizeof(ps.path) - 1] != '\0')
460 fatalx("received corrupted data");
463 if (ps.status == STATE_PLAYING)
476 if (imsg.hdr.type != IMSG_CTL_STATUS)
477 fatalx("invalid message %d",
480 if (datalen != sizeof(ps))
481 fatalx("data size mismatch");
482 memcpy(&ps, imsg.data, sizeof(ps));
483 if (ps.path[sizeof(ps.path) - 1] != '\0')
484 fatalx("received corrupted data");
486 if (ps.status == STATE_STOPPED)
488 else if (ps.status == STATE_PLAYING)
490 else if (ps.status == STATE_PAUSED)
497 print_time("position", ps.position);
498 print_time("duration", ps.duration);
500 printf("repeat one %s\nrepeat all %s\n",
501 ps.rp.repeat_one ? "on" : "off",
502 ps.rp.repeat_all ? "on" : "off");
507 if (imsg.hdr.type == IMSG_CTL_ADD)
509 if (imsg.hdr.type == IMSG_CTL_COMMIT) {
514 if (imsg.hdr.type != IMSG_CTL_BEGIN)
515 fatalx("invalid message %d",
518 load_files(res, &ret);
521 if (imsg.hdr.type != IMSG_CTL_MONITOR)
522 fatalx("invalid message %d",
525 if (datalen != sizeof(type))
526 fatalx("data size mismatch");
528 memcpy(&type, imsg.data, sizeof(type));
529 if (type < 0 || type > IMSG__LAST)
530 fatalx("received corrupted data");
532 if (!res->monitor[type])
535 puts(imsg_name(type));
552 ctl_noarg(struct parse_result *res, int argc, char **argv)
556 while ((ch = getopt(argc, argv, "")) != -1)
564 return ctlaction(res);
568 ctl_add(struct parse_result *res, int argc, char **argv)
572 while ((ch = getopt(argc, argv, "")) != -1)
581 return ctlaction(res);
585 ctl_show(struct parse_result *res, int argc, char **argv)
589 while ((ch = getopt(argc, argv, "p")) != -1) {
599 return ctlaction(res);
603 ctl_load(struct parse_result *res, int argc, char **argv)
607 while ((ch = getopt(argc, argv, "")) != -1)
619 if (pledge("stdio rpath", NULL) == -1)
622 return ctlaction(res);
626 ctl_jump(struct parse_result *res, int argc, char **argv)
630 while ((ch = getopt(argc, argv, "")) != -1)
639 return ctlaction(res);
643 ctl_repeat(struct parse_result *res, int argc, char **argv)
647 while ((ch = getopt(argc, argv, "")) != -1)
655 if (!strcmp(argv[1], "on"))
657 else if (!strcmp(argv[1], "off"))
662 res->rep.repeat_one = -1;
663 res->rep.repeat_all = -1;
664 if (!strcmp(argv[0], "one"))
665 res->rep.repeat_one = b;
666 else if (!strcmp(argv[0], "all"))
667 res->rep.repeat_all = b;
671 return ctlaction(res);
675 ctl_monitor(struct parse_result *res, int argc, char **argv)
679 char *dup, *tmp, *tok;
681 while ((ch = getopt(argc, argv, "")) != -1)
692 events = "play,toggle,pause,stop,restart,flush,next,prev,"
693 "jump,repeat,add,load";
695 tmp = dup = xstrdup(events);
696 while ((tok = strsep(&tmp, ",")) != NULL) {
700 if (!strcmp(tok, "play"))
701 res->monitor[IMSG_CTL_PLAY] = 1;
702 else if (!strcmp(tok, "toggle"))
703 res->monitor[IMSG_CTL_TOGGLE_PLAY] = 1;
704 else if (!strcmp(tok, "pause"))
705 res->monitor[IMSG_CTL_PAUSE] = 1;
706 else if (!strcmp(tok, "stop"))
707 res->monitor[IMSG_CTL_STOP] = 1;
708 else if (!strcmp(tok, "restart"))
709 res->monitor[IMSG_CTL_RESTART] = 1;
710 else if (!strcmp(tok, "flush"))
711 res->monitor[IMSG_CTL_FLUSH] = 1;
712 else if (!strcmp(tok, "next"))
713 res->monitor[IMSG_CTL_NEXT] = 1;
714 else if (!strcmp(tok, "prev"))
715 res->monitor[IMSG_CTL_PREV] = 1;
716 else if (!strcmp(tok, "jump"))
717 res->monitor[IMSG_CTL_JUMP] = 1;
718 else if (!strcmp(tok, "repeat"))
719 res->monitor[IMSG_CTL_REPEAT] = 1;
720 else if (!strcmp(tok, "add"))
721 res->monitor[IMSG_CTL_ADD] = 1;
722 else if (!strcmp(tok, "load"))
723 res->monitor[IMSG_CTL_COMMIT] = 1;
725 fatalx("unknown event \"%s\"", tok);
729 return ctlaction(res);
733 ctl_get_lock(const char *lockfile)
737 if ((lockfd = open(lockfile, O_WRONLY|O_CREAT, 0600)) == -1) {
738 log_debug("open failed: %s", strerror(errno));
742 if (flock(lockfd, LOCK_EX|LOCK_NB) == -1) {
743 log_debug("flock failed: %s", strerror(errno));
746 while (flock(lockfd, LOCK_EX) == -1 && errno == EINTR)
751 log_debug("flock succeeded");
759 struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
760 struct sockaddr_un sa;
762 int fd, lockfd = -1, locked = 0, spawned = 0;
764 char *lockfile = NULL;
766 memset(&sa, 0, sizeof(sa));
767 sa.sun_family = AF_UNIX;
768 size = strlcpy(sa.sun_path, csock, sizeof(sa.sun_path));
769 if (size >= sizeof(sa.sun_path)) {
770 errno = ENAMETOOLONG;
775 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
778 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
779 log_debug("connection failed: %s", strerror(errno));
780 if (errno != ECONNREFUSED && errno != ENOENT)
782 if (attempt++ == 100)
787 xasprintf(&lockfile, "%s.lock", csock);
788 if ((lockfd = ctl_get_lock(lockfile)) < 0) {
789 log_debug("didn't get the lock (%d)", lockfd);
799 * Always retry at least once, even if we got
800 * the lock, because another client could have
801 * taken the lock, started the server and released
802 * the lock between our connect() and flock()
809 log_debug("spawning the daemon");
814 nanosleep(&ts, NULL);
818 if (locked && lockfd >= 0) {
835 ctl(int argc, char **argv)
839 log_init(1, LOG_DAEMON);
840 log_setverbose(verbose);
842 if (getcwd(cwd, sizeof(cwd)) == NULL)
845 if ((ctl_sock = ctl_connect()) == -1)
846 fatal("can't connect");
849 fatalx("failed to connect to the daemon");
851 ibuf = xmalloc(sizeof(*ibuf));
852 imsg_init(ibuf, ctl_sock);
857 exit(parse(argc, argv));