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 **);
49 static int ctl_status(struct parse_result *, int, char **);
51 struct ctl_command ctl_commands[] = {
52 { "add", ADD, ctl_add, "files...", 0 },
53 { "flush", FLUSH, ctl_noarg, "", 0 },
54 { "jump", JUMP, ctl_jump, "pattern", 0 },
55 { "load", LOAD, ctl_load, "[file]", 1 },
56 { "monitor", MONITOR, ctl_monitor, "[events]", 0 },
57 { "next", NEXT, ctl_noarg, "", 0 },
58 { "pause", PAUSE, ctl_noarg, "", 0 },
59 { "play", PLAY, ctl_noarg, "", 0 },
60 { "prev", PREV, ctl_noarg, "", 0 },
61 { "repeat", REPEAT, ctl_repeat, "one|all on|off", 0 },
62 { "restart", RESTART, ctl_noarg, "", 0 },
63 { "seek", SEEK, ctl_seek, "[+-]time[%]", 0 },
64 { "show", SHOW, ctl_show, "[-p]", 0 },
65 { "status", STATUS, ctl_status, "[-f fmt]", 0 },
66 { "stop", STOP, ctl_noarg, "", 0 },
67 { "toggle", TOGGLE, ctl_noarg, "", 0 },
68 { NULL, 0, NULL, NULL, 0 },
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:
267 case IMSG_CTL_REPEAT:
271 case IMSG_CTL_COMMIT:
279 print_time(const char *label, int64_t seconds, const char *suffx)
286 hours = seconds / 3600;
287 seconds -= hours * 3600;
289 minutes = seconds / 60;
290 seconds -= minutes * 60;
294 printf("%02d:", hours);
295 printf("%02d:%02lld%s", minutes, (long long)seconds, suffx);
299 print_status(struct player_status *ps, const char *spec)
303 char *dup, *tmp, *tok;
305 if (ps->status == STATE_STOPPED)
307 else if (ps->status == STATE_PLAYING)
309 else if (ps->status == STATE_PAUSED)
314 percent = 100.0 * (double)ps->position / (double)ps->duration;
316 tmp = dup = xstrdup(spec);
317 while ((tok = strsep(&tmp, ",")) != NULL) {
321 if (!strcmp(tok, "path")) {
323 } else if (!strcmp(tok, "repeat:oneline")) {
324 printf("repeat one:%s ",
325 ps->rp.repeat_one ? "on" : "off");
326 printf("all:%s\n", ps->rp.repeat_all ? "on" : "off");
327 } else if (!strcmp(tok, "repeat")) {
328 printf("repeat one %s\n",
329 ps->rp.repeat_one ? "on" : "off");
330 printf("repeat all %s\n",
331 ps->rp.repeat_all ? "on" : "off");
332 } else if (!strcmp(tok, "status")) {
333 printf("%s %s\n", status, ps->path);
334 } else if (!strcmp(tok, "time:oneline")) {
335 print_time("time ", ps->position, " / ");
336 print_time("", ps->duration, "\n");
337 } else if (!strcmp(tok, "time:percentage")) {
338 printf("position %.2f%%\n", percent);
339 } else if (!strcmp(tok, "time:raw")) {
340 printf("position %lld\n", (long long)ps->position);
341 printf("duration %lld\n", (long long)ps->duration);
342 } else if (!strcmp(tok, "time")) {
343 print_time("position ", ps->position, "\n");
344 print_time("duration ", ps->duration, "\n");
352 ctlaction(struct parse_result *res)
356 struct player_status ps;
359 int i, type, ret = 0, done = 1;
361 switch (res->action) {
363 imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
365 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
371 imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
374 imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
376 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
382 imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
386 for (i = 0; res->files[i] != NULL; ++i) {
387 memset(path, 0, sizeof(path));
388 if (canonpath(res->files[i], path, sizeof(path))
390 log_warn("canonpath %s", res->files[i]);
394 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
400 imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
404 imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
408 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
411 imsg_compose(ibuf, IMSG_CTL_NEXT, 0, 0, -1, NULL, 0);
413 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
419 imsg_compose(ibuf, IMSG_CTL_PREV, 0, 0, -1, NULL, 0);
421 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
428 imsg_compose(ibuf, IMSG_CTL_BEGIN, 0, 0, -1, NULL, 0);
432 memset(path, 0, sizeof(path));
433 strlcpy(path, res->file, sizeof(path));
434 imsg_compose(ibuf, IMSG_CTL_JUMP, 0, 0, -1,
438 imsg_compose(ibuf, IMSG_CTL_REPEAT, 0, 0, -1,
439 &res->rep, sizeof(res->rep));
443 imsg_compose(ibuf, IMSG_CTL_MONITOR, 0, 0, -1,
447 memset(&res->seek, 0, sizeof(res->seek));
450 imsg_compose(ibuf, IMSG_CTL_SEEK, 0, 0, -1, &res->seek,
454 /* action not expected */
455 fatalx("invalid action %u", res->action);
466 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
467 fatalx("imsg_read error");
469 fatalx("pipe closed");
472 if ((n = imsg_get(ibuf, &imsg)) == -1)
473 fatalx("imsg_get error");
477 if (imsg.hdr.type == IMSG_CTL_ERR) {
478 log_warnx("%s: %s", res->ctl->name,
479 imsg_strerror(&imsg));
485 datalen = IMSG_DATA_SIZE(imsg);
487 switch (res->action) {
489 if (res->files[i] == NULL)
490 fatalx("received more replies than "
493 if (imsg.hdr.type == IMSG_CTL_ADD)
494 log_debug("enqueued %s", res->files[i]);
496 fatalx("invalid message %d",
499 done = res->files[i] == NULL;
506 if (datalen != sizeof(ps))
507 fatalx("data size mismatch");
508 memcpy(&ps, imsg.data, sizeof(ps));
509 if (ps.path[sizeof(ps.path) - 1] != '\0')
510 fatalx("received corrupted data");
513 if (ps.status == STATE_PLAYING)
526 if (imsg.hdr.type != IMSG_CTL_STATUS)
527 fatalx("invalid message %d",
530 if (datalen != sizeof(ps))
531 fatalx("data size mismatch");
532 memcpy(&ps, imsg.data, sizeof(ps));
533 if (ps.path[sizeof(ps.path) - 1] != '\0')
534 fatalx("received corrupted data");
536 print_status(&ps, res->status_format);
540 if (imsg.hdr.type == IMSG_CTL_ADD)
542 if (imsg.hdr.type == IMSG_CTL_COMMIT) {
547 if (imsg.hdr.type != IMSG_CTL_BEGIN)
548 fatalx("invalid message %d",
551 load_files(res, &ret);
554 if (imsg.hdr.type != IMSG_CTL_MONITOR)
555 fatalx("invalid message %d",
558 if (datalen != sizeof(type))
559 fatalx("data size mismatch");
561 memcpy(&type, imsg.data, sizeof(type));
562 if (type < 0 || type > IMSG__LAST)
563 fatalx("received corrupted data");
565 if (!res->monitor[type])
568 puts(imsg_name(type));
585 ctl_noarg(struct parse_result *res, int argc, char **argv)
589 while ((ch = getopt(argc, argv, "")) != -1)
597 return ctlaction(res);
601 ctl_add(struct parse_result *res, int argc, char **argv)
605 while ((ch = getopt(argc, argv, "")) != -1)
614 return ctlaction(res);
618 ctl_show(struct parse_result *res, int argc, char **argv)
622 while ((ch = getopt(argc, argv, "p")) != -1) {
632 return ctlaction(res);
636 ctl_load(struct parse_result *res, int argc, char **argv)
640 while ((ch = getopt(argc, argv, "")) != -1)
652 if (pledge("stdio rpath", NULL) == -1)
655 return ctlaction(res);
659 ctl_jump(struct parse_result *res, int argc, char **argv)
663 while ((ch = getopt(argc, argv, "")) != -1)
672 return ctlaction(res);
676 ctl_repeat(struct parse_result *res, int argc, char **argv)
680 while ((ch = getopt(argc, argv, "")) != -1)
688 if (!strcmp(argv[1], "on"))
690 else if (!strcmp(argv[1], "off"))
695 res->rep.repeat_one = -1;
696 res->rep.repeat_all = -1;
697 if (!strcmp(argv[0], "one"))
698 res->rep.repeat_one = b;
699 else if (!strcmp(argv[0], "all"))
700 res->rep.repeat_all = b;
704 return ctlaction(res);
708 ctl_monitor(struct parse_result *res, int argc, char **argv)
712 char *dup, *tmp, *tok;
714 while ((ch = getopt(argc, argv, "")) != -1)
725 events = "play,toggle,pause,stop,restart,flush,next,prev,"
726 "jump,repeat,add,load";
728 tmp = dup = xstrdup(events);
729 while ((tok = strsep(&tmp, ",")) != NULL) {
733 if (!strcmp(tok, "play"))
734 res->monitor[IMSG_CTL_PLAY] = 1;
735 else if (!strcmp(tok, "toggle"))
736 res->monitor[IMSG_CTL_TOGGLE_PLAY] = 1;
737 else if (!strcmp(tok, "pause"))
738 res->monitor[IMSG_CTL_PAUSE] = 1;
739 else if (!strcmp(tok, "stop"))
740 res->monitor[IMSG_CTL_STOP] = 1;
741 else if (!strcmp(tok, "restart")) /* compat */
742 res->monitor[IMSG_CTL_SEEK] = 1;
743 else if (!strcmp(tok, "flush"))
744 res->monitor[IMSG_CTL_FLUSH] = 1;
745 else if (!strcmp(tok, "next"))
746 res->monitor[IMSG_CTL_NEXT] = 1;
747 else if (!strcmp(tok, "prev"))
748 res->monitor[IMSG_CTL_PREV] = 1;
749 else if (!strcmp(tok, "jump"))
750 res->monitor[IMSG_CTL_JUMP] = 1;
751 else if (!strcmp(tok, "repeat"))
752 res->monitor[IMSG_CTL_REPEAT] = 1;
753 else if (!strcmp(tok, "add"))
754 res->monitor[IMSG_CTL_ADD] = 1;
755 else if (!strcmp(tok, "load"))
756 res->monitor[IMSG_CTL_COMMIT] = 1;
757 else if (!strcmp(tok, "seek"))
758 res->monitor[IMSG_CTL_SEEK] = 1;
760 fatalx("unknown event \"%s\"", tok);
764 return ctlaction(res);
768 ctl_seek(struct parse_result *res, int argc, char **argv)
772 int hours = 0, minutes = 0, seconds = 0;
776 /* skip the command name */
781 if (argc > 0 && !strcmp(*argv, "--")) {
790 if (*n == '-' || *n == '+')
791 res->seek.relative = 1;
797 seconds = strtol(n, &ep, 10);
799 (*ep != '\0' && *ep != ':' && *ep != '%') ||
800 (*ep == '%' && ep[1] != '\0'))
801 fatalx("invalid offset: %s", argv[0]);
802 if (*ep == '\0' || *ep == '%') {
803 res->seek.percent = *ep == '%';
809 seconds = strtol(n, &ep, 10);
810 if (n[0] == '\0' || (*ep != '\0' && *ep != ':'))
811 fatalx("invalid offset: %s", argv[0]);
818 seconds = strtol(n, &ep, 10);
819 if (n[0] == '\0' || *ep != '\0')
820 fatalx("invalid offset: %s", argv[0]);
823 res->seek.offset = sign * (hours * 3600 + minutes * 60 + seconds);
824 return ctlaction(res);
828 ctl_status(struct parse_result *res, int argc, char **argv)
830 const char *fmt = NULL;
833 while ((ch = getopt(argc, argv, "f:")) != -1) {
848 if (fmt == NULL && (fmt = getenv("AMUSED_STATUS_FORMAT")) == NULL)
849 fmt = "status,time,repeat";
850 res->status_format = fmt;
851 return ctlaction(res);
855 ctl_get_lock(const char *lockfile)
859 if ((lockfd = open(lockfile, O_WRONLY|O_CREAT, 0600)) == -1) {
860 log_debug("open failed: %s", strerror(errno));
864 if (flock(lockfd, LOCK_EX|LOCK_NB) == -1) {
865 log_debug("flock failed: %s", strerror(errno));
868 while (flock(lockfd, LOCK_EX) == -1 && errno == EINTR)
873 log_debug("flock succeeded");
881 struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
882 struct sockaddr_un sa;
884 int fd, lockfd = -1, locked = 0, spawned = 0;
886 char *lockfile = NULL;
888 memset(&sa, 0, sizeof(sa));
889 sa.sun_family = AF_UNIX;
890 size = strlcpy(sa.sun_path, csock, sizeof(sa.sun_path));
891 if (size >= sizeof(sa.sun_path)) {
892 errno = ENAMETOOLONG;
897 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
900 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
901 log_debug("connection failed: %s", strerror(errno));
902 if (errno != ECONNREFUSED && errno != ENOENT)
904 if (attempt++ == 100)
909 xasprintf(&lockfile, "%s.lock", csock);
910 if ((lockfd = ctl_get_lock(lockfile)) < 0) {
911 log_debug("didn't get the lock (%d)", lockfd);
921 * Always retry at least once, even if we got
922 * the lock, because another client could have
923 * taken the lock, started the server and released
924 * the lock between our connect() and flock()
931 log_debug("spawning the daemon");
936 nanosleep(&ts, NULL);
940 if (locked && lockfd >= 0) {
957 ctl(int argc, char **argv)
961 log_init(1, LOG_DAEMON);
962 log_setverbose(verbose);
964 if (getcwd(cwd, sizeof(cwd)) == NULL)
967 if ((ctl_sock = ctl_connect()) == -1)
968 fatal("can't connect");
971 fatalx("failed to connect to the daemon");
973 ibuf = xmalloc(sizeof(*ibuf));
974 imsg_init(ibuf, ctl_sock);
979 exit(parse(argc, argv));