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_warnx("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 ctlaction(struct parse_result *res)
285 struct player_status ps;
288 int i, type, ret = 0, done = 1;
290 switch (res->action) {
292 imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
294 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
300 imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
303 imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
305 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
311 imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
314 imsg_compose(ibuf, IMSG_CTL_RESTART, 0, 0, -1, NULL, 0);
316 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
323 for (i = 0; res->files[i] != NULL; ++i) {
324 memset(path, 0, sizeof(path));
325 if (canonpath(res->files[i], path, sizeof(path))
327 log_warn("canonpath %s", res->files[i]);
331 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
337 imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
341 imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
345 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
348 imsg_compose(ibuf, IMSG_CTL_NEXT, 0, 0, -1, NULL, 0);
350 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
356 imsg_compose(ibuf, IMSG_CTL_PREV, 0, 0, -1, NULL, 0);
358 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
365 imsg_compose(ibuf, IMSG_CTL_BEGIN, 0, 0, -1, NULL, 0);
369 memset(path, 0, sizeof(path));
370 strlcpy(path, res->file, sizeof(path));
371 imsg_compose(ibuf, IMSG_CTL_JUMP, 0, 0, -1,
375 imsg_compose(ibuf, IMSG_CTL_REPEAT, 0, 0, -1,
376 &res->rep, sizeof(res->rep));
380 imsg_compose(ibuf, IMSG_CTL_MONITOR, 0, 0, -1,
384 /* action not expected */
385 fatalx("invalid action %u", res->action);
396 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
397 fatalx("imsg_read error");
399 fatalx("pipe closed");
402 if ((n = imsg_get(ibuf, &imsg)) == -1)
403 fatalx("imsg_get error");
407 if (imsg.hdr.type == IMSG_CTL_ERR) {
408 log_warnx("%s: %s", res->ctl->name,
409 imsg_strerror(&imsg));
415 datalen = IMSG_DATA_SIZE(imsg);
417 switch (res->action) {
419 if (res->files[i] == NULL)
420 fatalx("received more replies than "
423 if (imsg.hdr.type == IMSG_CTL_ADD)
424 log_debug("enqueued %s", res->files[i]);
426 fatalx("invalid message %d",
429 done = res->files[i] == NULL;
436 if (datalen != sizeof(ps))
437 fatalx("data size mismatch");
438 memcpy(&ps, imsg.data, sizeof(ps));
439 if (ps.path[sizeof(ps.path) - 1] != '\0')
440 fatalx("received corrupted data");
443 if (ps.status == STATE_PLAYING)
456 if (imsg.hdr.type != IMSG_CTL_STATUS)
457 fatalx("invalid message %d",
460 if (datalen != sizeof(ps))
461 fatalx("data size mismatch");
462 memcpy(&ps, imsg.data, sizeof(ps));
463 if (ps.path[sizeof(ps.path) - 1] != '\0')
464 fatalx("received corrupted data");
466 if (ps.status == STATE_STOPPED)
468 else if (ps.status == STATE_PLAYING)
470 else if (ps.status == STATE_PAUSED)
476 printf("repat one %s\nrepeat all %s\n",
477 ps.rp.repeat_one ? "on" : "off",
478 ps.rp.repeat_all ? "on" : "off");
483 if (imsg.hdr.type == IMSG_CTL_ADD)
485 if (imsg.hdr.type == IMSG_CTL_COMMIT) {
490 if (imsg.hdr.type != IMSG_CTL_BEGIN)
491 fatalx("invalid message %d",
494 load_files(res, &ret);
497 if (imsg.hdr.type != IMSG_CTL_MONITOR)
498 fatalx("invalid message %d",
501 if (datalen != sizeof(type))
502 fatalx("data size mismatch");
504 memcpy(&type, imsg.data, sizeof(type));
505 if (type < 0 || type > IMSG__LAST)
506 fatalx("received corrupted data");
508 if (!res->monitor[type])
511 puts(imsg_name(type));
528 ctl_noarg(struct parse_result *res, int argc, char **argv)
532 while ((ch = getopt(argc, argv, "")) != -1)
540 return ctlaction(res);
544 ctl_add(struct parse_result *res, int argc, char **argv)
548 while ((ch = getopt(argc, argv, "")) != -1)
557 return ctlaction(res);
561 ctl_show(struct parse_result *res, int argc, char **argv)
565 while ((ch = getopt(argc, argv, "p")) != -1) {
575 return ctlaction(res);
579 ctl_load(struct parse_result *res, int argc, char **argv)
583 while ((ch = getopt(argc, argv, "")) != -1)
595 if (pledge("stdio rpath", NULL) == -1)
598 return ctlaction(res);
602 ctl_jump(struct parse_result *res, int argc, char **argv)
606 while ((ch = getopt(argc, argv, "")) != -1)
615 return ctlaction(res);
619 ctl_repeat(struct parse_result *res, int argc, char **argv)
623 while ((ch = getopt(argc, argv, "")) != -1)
631 if (!strcmp(argv[1], "on"))
633 else if (!strcmp(argv[1], "off"))
638 res->rep.repeat_one = -1;
639 res->rep.repeat_all = -1;
640 if (!strcmp(argv[0], "one"))
641 res->rep.repeat_one = b;
642 else if (!strcmp(argv[0], "all"))
643 res->rep.repeat_all = b;
647 return ctlaction(res);
651 ctl_monitor(struct parse_result *res, int argc, char **argv)
655 char *dup, *tmp, *tok;
657 while ((ch = getopt(argc, argv, "")) != -1)
668 events = "play,toggle,pause,stop,restart,flush,next,prev,"
669 "jump,repeat,add,load";
671 tmp = dup = xstrdup(events);
672 while ((tok = strsep(&tmp, ",")) != NULL) {
676 if (!strcmp(tok, "play"))
677 res->monitor[IMSG_CTL_PLAY] = 1;
678 else if (!strcmp(tok, "toggle"))
679 res->monitor[IMSG_CTL_TOGGLE_PLAY] = 1;
680 else if (!strcmp(tok, "pause"))
681 res->monitor[IMSG_CTL_PAUSE] = 1;
682 else if (!strcmp(tok, "stop"))
683 res->monitor[IMSG_CTL_STOP] = 1;
684 else if (!strcmp(tok, "restart"))
685 res->monitor[IMSG_CTL_RESTART] = 1;
686 else if (!strcmp(tok, "flush"))
687 res->monitor[IMSG_CTL_FLUSH] = 1;
688 else if (!strcmp(tok, "next"))
689 res->monitor[IMSG_CTL_NEXT] = 1;
690 else if (!strcmp(tok, "prev"))
691 res->monitor[IMSG_CTL_PREV] = 1;
692 else if (!strcmp(tok, "jump"))
693 res->monitor[IMSG_CTL_JUMP] = 1;
694 else if (!strcmp(tok, "repeat"))
695 res->monitor[IMSG_CTL_REPEAT] = 1;
696 else if (!strcmp(tok, "add"))
697 res->monitor[IMSG_CTL_ADD] = 1;
698 else if (!strcmp(tok, "load"))
699 res->monitor[IMSG_CTL_COMMIT] = 1;
701 fatalx("unknown event \"%s\"", tok);
705 return ctlaction(res);
709 ctl_get_lock(const char *lockfile)
713 if ((lockfd = open(lockfile, O_WRONLY|O_CREAT, 0600)) == -1) {
714 log_debug("open failed: %s", strerror(errno));
718 if (flock(lockfd, LOCK_EX|LOCK_NB) == -1) {
719 log_debug("flock failed: %s", strerror(errno));
722 while (flock(lockfd, LOCK_EX) == -1 && errno == EINTR)
727 log_debug("flock succeeded");
735 struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
736 struct sockaddr_un sa;
738 int fd, lockfd = -1, locked = 0, spawned = 0;
739 char *lockfile = NULL;
741 memset(&sa, 0, sizeof(sa));
742 sa.sun_family = AF_UNIX;
743 size = strlcpy(sa.sun_path, csock, sizeof(sa.sun_path));
744 if (size >= sizeof(sa.sun_path)) {
745 errno = ENAMETOOLONG;
750 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
753 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
754 log_debug("connection failed: %s", strerror(errno));
755 if (errno != ECONNREFUSED && errno != ENOENT)
760 xasprintf(&lockfile, "%s.lock", csock);
761 if ((lockfd = ctl_get_lock(lockfile)) < 0) {
762 log_debug("didn't get the lock (%d)", lockfd);
772 * Always retry at least once, even if we got
773 * the lock, because another client could have
774 * taken the lock, started the server and released
775 * the lock between our connect() and flock()
782 log_debug("spawning the daemon");
787 nanosleep(&ts, NULL);
791 if (locked && lockfd >= 0) {
808 ctl(int argc, char **argv)
812 log_init(1, LOG_DAEMON);
813 log_setverbose(verbose);
815 if (getcwd(cwd, sizeof(cwd)) == NULL)
818 if ((ctl_sock = ctl_connect()) == -1)
819 fatal("can't connect");
822 fatalx("failed to connect to the daemon");
824 ibuf = xmalloc(sizeof(*ibuf));
825 imsg_init(ibuf, ctl_sock);
830 exit(parse(argc, argv));