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...", 1 },
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 if (canonpath(file, path, sizeof(path)) == -1) {
207 log_warnx("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 ctlaction(struct parse_result *res)
284 struct player_status ps;
287 int i, type, ret = 0, done = 1;
289 switch (res->action) {
291 imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
293 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
299 imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
302 imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
304 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
310 imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
313 imsg_compose(ibuf, IMSG_CTL_RESTART, 0, 0, -1, NULL, 0);
315 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
322 for (i = 0; res->files[i] != NULL; ++i) {
323 memset(&path, 0, sizeof(path));
324 if (realpath(res->files[i], path) == NULL) {
325 log_warn("realpath %s", res->files[i]);
329 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
335 imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
339 imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
343 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
346 imsg_compose(ibuf, IMSG_CTL_NEXT, 0, 0, -1, NULL, 0);
348 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
354 imsg_compose(ibuf, IMSG_CTL_PREV, 0, 0, -1, NULL, 0);
356 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
363 imsg_compose(ibuf, IMSG_CTL_BEGIN, 0, 0, -1, NULL, 0);
367 memset(path, 0, sizeof(path));
368 strlcpy(path, res->file, sizeof(path));
369 imsg_compose(ibuf, IMSG_CTL_JUMP, 0, 0, -1,
373 imsg_compose(ibuf, IMSG_CTL_REPEAT, 0, 0, -1,
374 &res->rep, sizeof(res->rep));
378 imsg_compose(ibuf, IMSG_CTL_MONITOR, 0, 0, -1,
382 /* action not expected */
383 fatalx("invalid action %u", res->action);
394 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
395 fatalx("imsg_read error");
397 fatalx("pipe closed");
400 if ((n = imsg_get(ibuf, &imsg)) == -1)
401 fatalx("imsg_get error");
405 if (imsg.hdr.type == IMSG_CTL_ERR) {
406 log_warnx("%s: %s", res->ctl->name,
407 imsg_strerror(&imsg));
413 datalen = IMSG_DATA_SIZE(imsg);
415 switch (res->action) {
417 if (res->files[i] == NULL)
418 fatalx("received more replies than "
421 if (imsg.hdr.type == IMSG_CTL_ADD)
422 log_debug("enqueued %s", res->files[i]);
424 fatalx("invalid message %d",
427 done = res->files[i] == NULL;
434 if (datalen != sizeof(ps))
435 fatalx("data size mismatch");
436 memcpy(&ps, imsg.data, sizeof(ps));
437 if (ps.path[sizeof(ps.path) - 1] != '\0')
438 fatalx("received corrupted data");
441 if (ps.status == STATE_PLAYING)
454 if (imsg.hdr.type != IMSG_CTL_STATUS)
455 fatalx("invalid message %d",
458 if (datalen != sizeof(ps))
459 fatalx("data size mismatch");
460 memcpy(&ps, imsg.data, sizeof(ps));
461 if (ps.path[sizeof(ps.path) - 1] != '\0')
462 fatalx("received corrupted data");
464 if (ps.status == STATE_STOPPED)
466 else if (ps.status == STATE_PLAYING)
468 else if (ps.status == STATE_PAUSED)
474 printf("repat one %s\nrepeat all %s\n",
475 ps.rp.repeat_one ? "on" : "off",
476 ps.rp.repeat_all ? "on" : "off");
481 if (imsg.hdr.type == IMSG_CTL_ADD)
483 if (imsg.hdr.type == IMSG_CTL_COMMIT) {
488 if (imsg.hdr.type != IMSG_CTL_BEGIN)
489 fatalx("invalid message %d",
492 load_files(res, &ret);
495 if (imsg.hdr.type != IMSG_CTL_MONITOR)
496 fatalx("invalid message %d",
499 if (datalen != sizeof(type))
500 fatalx("data size mismatch");
502 memcpy(&type, imsg.data, sizeof(type));
503 if (type < 0 || type > IMSG__LAST)
504 fatalx("received corrupted data");
506 if (!res->monitor[type])
509 puts(imsg_name(type));
526 ctl_noarg(struct parse_result *res, int argc, char **argv)
530 while ((ch = getopt(argc, argv, "")) != -1)
538 return ctlaction(res);
542 ctl_add(struct parse_result *res, int argc, char **argv)
546 while ((ch = getopt(argc, argv, "")) != -1)
555 if (pledge("stdio rpath", NULL) == -1)
558 return ctlaction(res);
562 ctl_show(struct parse_result *res, int argc, char **argv)
566 while ((ch = getopt(argc, argv, "p")) != -1) {
576 return ctlaction(res);
580 ctl_load(struct parse_result *res, int argc, char **argv)
584 while ((ch = getopt(argc, argv, "")) != -1)
596 if (pledge("stdio rpath", NULL) == -1)
599 return ctlaction(res);
603 ctl_jump(struct parse_result *res, int argc, char **argv)
607 while ((ch = getopt(argc, argv, "")) != -1)
616 return ctlaction(res);
620 ctl_repeat(struct parse_result *res, int argc, char **argv)
624 while ((ch = getopt(argc, argv, "")) != -1)
632 if (!strcmp(argv[1], "on"))
634 else if (!strcmp(argv[1], "off"))
639 res->rep.repeat_one = -1;
640 res->rep.repeat_all = -1;
641 if (!strcmp(argv[0], "one"))
642 res->rep.repeat_one = b;
643 else if (!strcmp(argv[0], "all"))
644 res->rep.repeat_all = b;
648 return ctlaction(res);
652 ctl_monitor(struct parse_result *res, int argc, char **argv)
656 char *dup, *tmp, *tok;
658 while ((ch = getopt(argc, argv, "")) != -1)
669 events = "play,toggle,pause,stop,restart,flush,next,prev,"
670 "jump,repeat,add,load";
672 tmp = dup = xstrdup(events);
673 while ((tok = strsep(&tmp, ",")) != NULL) {
677 if (!strcmp(tok, "play"))
678 res->monitor[IMSG_CTL_PLAY] = 1;
679 else if (!strcmp(tok, "toggle"))
680 res->monitor[IMSG_CTL_TOGGLE_PLAY] = 1;
681 else if (!strcmp(tok, "pause"))
682 res->monitor[IMSG_CTL_PAUSE] = 1;
683 else if (!strcmp(tok, "stop"))
684 res->monitor[IMSG_CTL_STOP] = 1;
685 else if (!strcmp(tok, "restart"))
686 res->monitor[IMSG_CTL_RESTART] = 1;
687 else if (!strcmp(tok, "flush"))
688 res->monitor[IMSG_CTL_FLUSH] = 1;
689 else if (!strcmp(tok, "next"))
690 res->monitor[IMSG_CTL_NEXT] = 1;
691 else if (!strcmp(tok, "prev"))
692 res->monitor[IMSG_CTL_PREV] = 1;
693 else if (!strcmp(tok, "jump"))
694 res->monitor[IMSG_CTL_JUMP] = 1;
695 else if (!strcmp(tok, "repeat"))
696 res->monitor[IMSG_CTL_REPEAT] = 1;
697 else if (!strcmp(tok, "add"))
698 res->monitor[IMSG_CTL_ADD] = 1;
699 else if (!strcmp(tok, "load"))
700 res->monitor[IMSG_CTL_COMMIT] = 1;
702 fatalx("unknown event \"%s\"", tok);
706 return ctlaction(res);
710 ctl_get_lock(const char *lockfile)
714 if ((lockfd = open(lockfile, O_WRONLY|O_CREAT, 0600)) == -1) {
715 log_debug("open failed: %s", strerror(errno));
719 if (flock(lockfd, LOCK_EX|LOCK_NB) == -1) {
720 log_debug("flock failed: %s", strerror(errno));
723 while (flock(lockfd, LOCK_EX) == -1 && errno == EINTR)
728 log_debug("flock succeeded");
736 struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
737 struct sockaddr_un sa;
739 int fd, lockfd = -1, locked = 0, spawned = 0;
740 char *lockfile = NULL;
742 memset(&sa, 0, sizeof(sa));
743 sa.sun_family = AF_UNIX;
744 size = strlcpy(sa.sun_path, csock, sizeof(sa.sun_path));
745 if (size >= sizeof(sa.sun_path)) {
746 errno = ENAMETOOLONG;
751 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
754 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
755 log_debug("connection failed: %s", strerror(errno));
756 if (errno != ECONNREFUSED && errno != ENOENT)
761 xasprintf(&lockfile, "%s.lock", csock);
762 if ((lockfd = ctl_get_lock(lockfile)) < 0) {
763 log_debug("didn't get the lock (%d)", lockfd);
773 * Always retry at least once, even if we got
774 * the lock, because another client could have
775 * taken the lock, started the server and released
776 * the lock between our connect() and flock()
783 log_debug("spawning the daemon");
788 nanosleep(&ts, NULL);
792 if (locked && lockfd >= 0) {
809 ctl(int argc, char **argv)
813 log_init(1, LOG_DAEMON);
814 log_setverbose(verbose);
816 if (getcwd(cwd, sizeof(cwd)) == NULL)
819 if ((ctl_sock = ctl_connect()) == -1)
820 fatal("can't connect");
823 fatalx("failed to connect to the daemon");
825 ibuf = xmalloc(sizeof(*ibuf));
826 imsg_init(ibuf, ctl_sock);
831 exit(parse(argc, argv));