2 * Copyright (c) 2022 Omar Polo <op@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/types.h>
18 #include <sys/socket.h>
19 #include <sys/queue.h>
40 static struct imsgbuf *ibuf;
42 int ctl_noarg(struct parse_result *, int, char **);
43 int ctl_add(struct parse_result *, int, char **);
44 int ctl_show(struct parse_result *, int, char **);
45 int ctl_load(struct parse_result *, int, char **);
46 int ctl_jump(struct parse_result *, int, char **);
47 int ctl_repeat(struct parse_result *, int, char **);
48 int ctl_monitor(struct parse_result *, int, char **);
50 struct ctl_command ctl_commands[] = {
51 { "play", PLAY, ctl_noarg, "" },
52 { "pause", PAUSE, ctl_noarg, "" },
53 { "toggle", TOGGLE, ctl_noarg, "" },
54 { "stop", STOP, ctl_noarg, "" },
55 { "restart", RESTART, ctl_noarg, "" },
56 { "add", ADD, ctl_add, "files...", 1 },
57 { "flush", FLUSH, ctl_noarg, "" },
58 { "show", SHOW, ctl_show, "[-p]" },
59 { "status", STATUS, ctl_noarg, "" },
60 { "next", NEXT, ctl_noarg, "" },
61 { "prev", PREV, ctl_noarg, "" },
62 { "load", LOAD, ctl_load, "[file]", 1 },
63 { "jump", JUMP, ctl_jump, "pattern" },
64 { "repeat", REPEAT, ctl_repeat, "one|all on|off" },
65 { "monitor", MONITOR, ctl_monitor, "[events]" },
72 fprintf(stderr, "usage: %s [-dv] [-s socket]\n", getprogname());
77 ctl_usage(struct ctl_command *ctl)
79 fprintf(stderr, "usage: %s [-v] [-s socket] %s %s\n", getprogname(),
80 ctl->name, ctl->usage);
85 parse(int argc, char **argv)
87 struct ctl_command *ctl = NULL;
88 struct parse_result res;
92 memset(&res, 0, sizeof(res));
94 if ((argv0 = argv[0]) == NULL)
97 for (i = 0; ctl_commands[i].name != NULL; ++i) {
98 if (strncmp(ctl_commands[i].name, argv0, strlen(argv0))
102 "ambiguous argument: %s\n", argv0);
105 ctl = &ctl_commands[i];
110 fprintf(stderr, "unknown argument: %s\n", argv[0]);
114 res.action = ctl->action;
117 if (!ctl->has_pledge) {
118 /* pledge(2) default if command doesn't have its own */
119 if (pledge("stdio", NULL) == -1)
123 status = ctl->main(&res, argc, argv);
130 load_files(struct parse_result *res, int *ret)
135 char path[PATH_MAX], cwd[PATH_MAX];
136 size_t linesize = 0, i = 0, n;
137 ssize_t linelen, curr = -1;
139 if (res->file == NULL)
141 else if ((f = fopen(res->file, "r")) == NULL) {
142 log_warn("can't open %s", res->file);
147 if (getcwd(cwd, sizeof(cwd)) == NULL)
150 while ((linelen = getline(&line, &linesize, f)) != -1) {
153 line[linelen-1] = '\0';
155 if (!strncmp(file, "> ", 2)) {
158 } else if (!strncmp(file, " ", 2))
161 if (!strncmp(file, "./", 2))
164 memset(path, 0, sizeof(path));
166 n = strlcpy(path, file, sizeof(path));
168 n = snprintf(path, sizeof(path), "%s/%s", cwd, file);
170 if (n >= sizeof(path)) {
171 log_warnx("path too long: %s", file);
176 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
190 imsg_compose(ibuf, IMSG_CTL_COMMIT, 0, 0, -1,
191 &curr, sizeof(curr));
197 imsg_strerror(struct imsg *imsg)
202 datalen = IMSG_DATA_SIZE(*imsg);
204 if (datalen == 0 || msg[datalen-1] != '\0')
205 fatalx("malformed error message");
216 case IMSG_CTL_TOGGLE_PLAY:
222 case IMSG_CTL_RESTART:
232 case IMSG_CTL_REPEAT:
236 case IMSG_CTL_COMMIT:
244 ctlaction(struct parse_result *res)
248 struct player_status ps;
251 int i, type, ret = 0, done = 1;
253 switch (res->action) {
255 imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
257 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
263 imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
266 imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
268 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
274 imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
277 imsg_compose(ibuf, IMSG_CTL_RESTART, 0, 0, -1, NULL, 0);
279 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
286 for (i = 0; res->files[i] != NULL; ++i) {
287 memset(&path, 0, sizeof(path));
288 if (realpath(res->files[i], path) == NULL) {
289 log_warn("realpath %s", res->files[i]);
293 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
299 imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
303 imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
307 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
310 imsg_compose(ibuf, IMSG_CTL_NEXT, 0, 0, -1, NULL, 0);
312 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
318 imsg_compose(ibuf, IMSG_CTL_PREV, 0, 0, -1, NULL, 0);
320 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
327 imsg_compose(ibuf, IMSG_CTL_BEGIN, 0, 0, -1, NULL, 0);
331 memset(path, 0, sizeof(path));
332 strlcpy(path, res->file, sizeof(path));
333 imsg_compose(ibuf, IMSG_CTL_JUMP, 0, 0, -1,
337 imsg_compose(ibuf, IMSG_CTL_REPEAT, 0, 0, -1,
338 &res->rep, sizeof(res->rep));
342 imsg_compose(ibuf, IMSG_CTL_MONITOR, 0, 0, -1,
346 /* action not expected */
347 fatalx("invalid action %u", res->action);
358 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
359 fatalx("imsg_read error");
361 fatalx("pipe closed");
364 if ((n = imsg_get(ibuf, &imsg)) == -1)
365 fatalx("imsg_get error");
369 if (imsg.hdr.type == IMSG_CTL_ERR) {
370 log_warnx("%s: %s", res->ctl->name,
371 imsg_strerror(&imsg));
377 datalen = IMSG_DATA_SIZE(imsg);
379 switch (res->action) {
381 if (res->files[i] == NULL)
382 fatalx("received more replies than "
385 if (imsg.hdr.type == IMSG_CTL_ADD)
386 log_debug("enqueued %s", res->files[i]);
388 fatalx("invalid message %d",
391 done = res->files[i] == NULL;
398 if (datalen != sizeof(ps))
399 fatalx("data size mismatch");
400 memcpy(&ps, imsg.data, sizeof(ps));
401 if (ps.path[sizeof(ps.path) - 1] != '\0')
402 fatalx("received corrupted data");
405 if (ps.status == STATE_PLAYING)
418 if (imsg.hdr.type != IMSG_CTL_STATUS)
419 fatalx("invalid message %d",
422 if (datalen != sizeof(ps))
423 fatalx("data size mismatch");
424 memcpy(&ps, imsg.data, sizeof(ps));
425 if (ps.path[sizeof(ps.path) - 1] != '\0')
426 fatalx("received corrupted data");
428 if (ps.status == STATE_STOPPED)
430 else if (ps.status == STATE_PLAYING)
432 else if (ps.status == STATE_PAUSED)
438 printf("repat one %s\nrepeat all %s\n",
439 ps.rp.repeat_one ? "on" : "off",
440 ps.rp.repeat_all ? "on" : "off");
445 if (imsg.hdr.type == IMSG_CTL_ADD)
447 if (imsg.hdr.type == IMSG_CTL_COMMIT) {
452 if (imsg.hdr.type != IMSG_CTL_BEGIN)
453 fatalx("invalid message %d",
456 load_files(res, &ret);
459 if (imsg.hdr.type != IMSG_CTL_MONITOR)
460 fatalx("invalid message %d",
463 if (datalen != sizeof(type))
464 fatalx("data size mismatch");
466 memcpy(&type, imsg.data, sizeof(type));
467 if (type < 0 || type > IMSG__LAST)
468 fatalx("received corrupted data");
470 if (!res->monitor[type])
473 puts(imsg_name(type));
490 ctl_noarg(struct parse_result *res, int argc, char **argv)
494 while ((ch = getopt(argc, argv, "")) != -1)
502 return ctlaction(res);
506 ctl_add(struct parse_result *res, int argc, char **argv)
510 while ((ch = getopt(argc, argv, "")) != -1)
519 if (pledge("stdio rpath", NULL) == -1)
522 return ctlaction(res);
526 ctl_show(struct parse_result *res, int argc, char **argv)
530 while ((ch = getopt(argc, argv, "p")) != -1) {
540 return ctlaction(res);
544 ctl_load(struct parse_result *res, int argc, char **argv)
548 while ((ch = getopt(argc, argv, "")) != -1)
560 if (pledge("stdio rpath", NULL) == -1)
563 return ctlaction(res);
567 ctl_jump(struct parse_result *res, int argc, char **argv)
571 while ((ch = getopt(argc, argv, "")) != -1)
580 return ctlaction(res);
584 ctl_repeat(struct parse_result *res, int argc, char **argv)
588 while ((ch = getopt(argc, argv, "")) != -1)
596 if (!strcmp(argv[1], "on"))
598 else if (!strcmp(argv[1], "off"))
603 res->rep.repeat_one = -1;
604 res->rep.repeat_all = -1;
605 if (!strcmp(argv[0], "one"))
606 res->rep.repeat_one = b;
607 else if (!strcmp(argv[0], "all"))
608 res->rep.repeat_all = b;
612 return ctlaction(res);
616 ctl_monitor(struct parse_result *res, int argc, char **argv)
620 char *dup, *tmp, *tok;
622 while ((ch = getopt(argc, argv, "")) != -1)
633 events = "play,toggle,pause,stop,restart,flush,next,prev,"
634 "jump,repeat,add,load";
636 tmp = dup = xstrdup(events);
637 while ((tok = strsep(&tmp, ",")) != NULL) {
641 if (!strcmp(tok, "play"))
642 res->monitor[IMSG_CTL_PLAY] = 1;
643 else if (!strcmp(tok, "toggle"))
644 res->monitor[IMSG_CTL_TOGGLE_PLAY] = 1;
645 else if (!strcmp(tok, "pause"))
646 res->monitor[IMSG_CTL_PAUSE] = 1;
647 else if (!strcmp(tok, "stop"))
648 res->monitor[IMSG_CTL_STOP] = 1;
649 else if (!strcmp(tok, "restart"))
650 res->monitor[IMSG_CTL_RESTART] = 1;
651 else if (!strcmp(tok, "flush"))
652 res->monitor[IMSG_CTL_FLUSH] = 1;
653 else if (!strcmp(tok, "next"))
654 res->monitor[IMSG_CTL_NEXT] = 1;
655 else if (!strcmp(tok, "prev"))
656 res->monitor[IMSG_CTL_PREV] = 1;
657 else if (!strcmp(tok, "jump"))
658 res->monitor[IMSG_CTL_JUMP] = 1;
659 else if (!strcmp(tok, "repeat"))
660 res->monitor[IMSG_CTL_REPEAT] = 1;
661 else if (!strcmp(tok, "add"))
662 res->monitor[IMSG_CTL_ADD] = 1;
663 else if (!strcmp(tok, "load"))
664 res->monitor[IMSG_CTL_COMMIT] = 1;
666 fatalx("unknown event \"%s\"", tok);
670 return ctlaction(res);
674 ctl_get_lock(const char *lockfile)
678 if ((lockfd = open(lockfile, O_WRONLY|O_CREAT, 0600)) == -1) {
679 log_debug("open failed: %s", strerror(errno));
683 if (flock(lockfd, LOCK_EX|LOCK_NB) == -1) {
684 log_debug("flock failed: %s", strerror(errno));
687 while (flock(lockfd, LOCK_EX) == -1 && errno == EINTR)
692 log_debug("flock succeeded");
700 struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
701 struct sockaddr_un sa;
703 int fd, lockfd = -1, locked = 0, spawned = 0;
704 char *lockfile = NULL;
706 memset(&sa, 0, sizeof(sa));
707 sa.sun_family = AF_UNIX;
708 size = strlcpy(sa.sun_path, csock, sizeof(sa.sun_path));
709 if (size >= sizeof(sa.sun_path)) {
710 errno = ENAMETOOLONG;
715 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
718 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
719 log_debug("connection failed: %s", strerror(errno));
720 if (errno != ECONNREFUSED && errno != ENOENT)
725 xasprintf(&lockfile, "%s.lock", csock);
726 if ((lockfd = ctl_get_lock(lockfile)) < 0) {
727 log_debug("didn't get the lock (%d)", lockfd);
737 * Always retry at least once, even if we got
738 * the lock, because another client could have
739 * taken the lock, started the server and released
740 * the lock between our connect() and flock()
747 log_debug("spawning the daemon");
752 nanosleep(&ts, NULL);
756 if (locked && lockfd >= 0) {
773 ctl(int argc, char **argv)
777 log_init(1, LOG_DAEMON);
778 log_setverbose(verbose);
780 if ((ctl_sock = ctl_connect()) == -1)
781 fatal("can't connect");
784 fatalx("failed to connect to the daemon");
786 ibuf = xmalloc(sizeof(*ibuf));
787 imsg_init(ibuf, ctl_sock);
792 exit(parse(argc, argv));