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(struct parse_result *res, int argc, char **argv)
135 struct ctl_command *ctl = NULL;
139 if ((argv0 = argv[0]) == NULL)
142 for (i = 0; ctl_commands[i].name != NULL; ++i) {
143 if (strncmp(ctl_commands[i].name, argv0, strlen(argv0))
147 "ambiguous argument: %s\n", argv0);
150 ctl = &ctl_commands[i];
155 fprintf(stderr, "unknown argument: %s\n", argv[0]);
159 res->action = ctl->action;
162 if (!ctl->has_pledge) {
163 /* pledge(2) default if command doesn't have its own */
164 if (pledge("stdio", NULL) == -1)
168 status = ctl->main(res, argc, argv);
175 load_files(struct parse_result *res, int *ret)
181 size_t linesize = 0, i = 0;
182 ssize_t linelen, curr = -1;
184 if (res->file == NULL)
186 else if ((f = fopen(res->file, "r")) == NULL) {
187 log_warn("can't open %s", res->file);
192 while ((linelen = getline(&line, &linesize, f)) != -1) {
195 line[linelen-1] = '\0';
197 if (!strncmp(file, "> ", 2)) {
200 } else if (!strncmp(file, " ", 2))
203 memset(path, 0, sizeof(path));
204 if (canonpath(file, path, sizeof(path)) == -1) {
205 log_warn("canonpath %s", file);
210 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
224 imsg_compose(ibuf, IMSG_CTL_COMMIT, 0, 0, -1,
225 &curr, sizeof(curr));
231 imsg_strerror(struct imsg *imsg)
236 datalen = IMSG_DATA_SIZE(*imsg);
238 if (datalen == 0 || msg[datalen-1] != '\0')
239 fatalx("malformed error message");
262 case IMSG_CTL_COMMIT:
272 print_time(const char *label, int64_t seconds, const char *suffx)
279 hours = seconds / 3600;
280 seconds -= hours * 3600;
282 minutes = seconds / 60;
283 seconds -= minutes * 60;
287 printf("%02d:", hours);
288 printf("%02d:%02lld%s", minutes, (long long)seconds, suffx);
292 print_status(struct player_status *ps, const char *spec)
296 char *dup, *tmp, *tok;
298 if (ps->status == STATE_STOPPED)
300 else if (ps->status == STATE_PLAYING)
302 else if (ps->status == STATE_PAUSED)
307 percent = 100.0 * (double)ps->position / (double)ps->duration;
309 tmp = dup = xstrdup(spec);
310 while ((tok = strsep(&tmp, ",")) != NULL) {
314 if (!strcmp(tok, "path")) {
316 } else if (!strcmp(tok, "repeat:oneline")) {
317 printf("repeat one:%s ",
318 ps->rp.repeat_one ? "on" : "off");
319 printf("all:%s\n", ps->rp.repeat_all ? "on" : "off");
320 } else if (!strcmp(tok, "repeat")) {
321 printf("repeat one %s\n",
322 ps->rp.repeat_one ? "on" : "off");
323 printf("repeat all %s\n",
324 ps->rp.repeat_all ? "on" : "off");
325 } else if (!strcmp(tok, "status")) {
326 printf("%s %s\n", status, ps->path);
327 } else if (!strcmp(tok, "time:oneline")) {
328 print_time("time ", ps->position, " / ");
329 print_time("", ps->duration, "\n");
330 } else if (!strcmp(tok, "time:percentage")) {
331 printf("position %.2f%%\n", percent);
332 } else if (!strcmp(tok, "time:raw")) {
333 printf("position %lld\n", (long long)ps->position);
334 printf("duration %lld\n", (long long)ps->duration);
335 } else if (!strcmp(tok, "time")) {
336 print_time("position ", ps->position, "\n");
337 print_time("duration ", ps->duration, "\n");
345 ctlaction(struct parse_result *res)
349 struct player_status ps;
352 int i, type, ret = 0, done = 1;
354 switch (res->action) {
356 imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
358 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
364 imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
367 imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
369 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
375 imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
379 for (i = 0; res->files[i] != NULL; ++i) {
380 memset(path, 0, sizeof(path));
381 if (canonpath(res->files[i], path, sizeof(path))
383 log_warn("canonpath %s", res->files[i]);
387 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
393 imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
397 imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
401 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
404 imsg_compose(ibuf, IMSG_CTL_NEXT, 0, 0, -1, NULL, 0);
406 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
412 imsg_compose(ibuf, IMSG_CTL_PREV, 0, 0, -1, NULL, 0);
414 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
421 imsg_compose(ibuf, IMSG_CTL_BEGIN, 0, 0, -1, NULL, 0);
425 memset(path, 0, sizeof(path));
426 strlcpy(path, res->file, sizeof(path));
427 imsg_compose(ibuf, IMSG_CTL_JUMP, 0, 0, -1,
431 imsg_compose(ibuf, IMSG_CTL_REPEAT, 0, 0, -1,
432 &res->rep, sizeof(res->rep));
436 imsg_compose(ibuf, IMSG_CTL_MONITOR, 0, 0, -1,
440 memset(&res->seek, 0, sizeof(res->seek));
443 imsg_compose(ibuf, IMSG_CTL_SEEK, 0, 0, -1, &res->seek,
447 /* action not expected */
448 fatalx("invalid action %u", res->action);
459 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
460 fatalx("imsg_read error");
462 fatalx("pipe closed");
465 if ((n = imsg_get(ibuf, &imsg)) == -1)
466 fatalx("imsg_get error");
470 if (imsg.hdr.type == IMSG_CTL_ERR) {
471 log_warnx("%s: %s", res->ctl->name,
472 imsg_strerror(&imsg));
478 datalen = IMSG_DATA_SIZE(imsg);
480 switch (res->action) {
482 if (res->files[i] == NULL)
483 fatalx("received more replies than "
486 if (imsg.hdr.type == IMSG_CTL_ADD)
487 log_debug("enqueued %s", res->files[i]);
489 fatalx("invalid message %d",
492 done = res->files[i] == NULL;
499 if (datalen != sizeof(ps))
500 fatalx("data size mismatch");
501 memcpy(&ps, imsg.data, sizeof(ps));
502 if (ps.path[sizeof(ps.path) - 1] != '\0')
503 fatalx("received corrupted data");
506 if (ps.status == STATE_PLAYING)
519 if (imsg.hdr.type != IMSG_CTL_STATUS)
520 fatalx("invalid message %d",
523 if (datalen != sizeof(ps))
524 fatalx("data size mismatch");
525 memcpy(&ps, imsg.data, sizeof(ps));
526 if (ps.path[sizeof(ps.path) - 1] != '\0')
527 fatalx("received corrupted data");
529 print_status(&ps, res->status_format);
533 if (imsg.hdr.type == IMSG_CTL_ADD)
535 if (imsg.hdr.type == IMSG_CTL_COMMIT) {
540 if (imsg.hdr.type != IMSG_CTL_BEGIN)
541 fatalx("invalid message %d",
544 load_files(res, &ret);
547 if (imsg.hdr.type != IMSG_CTL_MONITOR)
548 fatalx("invalid message %d",
551 if (datalen != sizeof(type))
552 fatalx("data size mismatch");
554 memcpy(&type, imsg.data, sizeof(type));
555 if (type < 0 || type > IMSG__LAST)
556 fatalx("received corrupted data");
558 if (!res->monitor[type])
561 puts(event_name(type));
578 ctl_noarg(struct parse_result *res, int argc, char **argv)
582 while ((ch = getopt(argc, argv, "")) != -1)
590 return ctlaction(res);
594 ctl_add(struct parse_result *res, int argc, char **argv)
598 while ((ch = getopt(argc, argv, "")) != -1)
607 return ctlaction(res);
611 ctl_show(struct parse_result *res, int argc, char **argv)
615 while ((ch = getopt(argc, argv, "p")) != -1) {
625 return ctlaction(res);
629 ctl_load(struct parse_result *res, int argc, char **argv)
633 while ((ch = getopt(argc, argv, "")) != -1)
645 if (pledge("stdio rpath", NULL) == -1)
648 return ctlaction(res);
652 ctl_jump(struct parse_result *res, int argc, char **argv)
656 while ((ch = getopt(argc, argv, "")) != -1)
665 return ctlaction(res);
669 ctl_repeat(struct parse_result *res, int argc, char **argv)
673 while ((ch = getopt(argc, argv, "")) != -1)
681 if (!strcmp(argv[1], "on"))
683 else if (!strcmp(argv[1], "off"))
688 res->rep.repeat_one = -1;
689 res->rep.repeat_all = -1;
690 if (!strcmp(argv[0], "one"))
691 res->rep.repeat_one = b;
692 else if (!strcmp(argv[0], "all"))
693 res->rep.repeat_all = b;
697 return ctlaction(res);
701 ctl_monitor(struct parse_result *res, int argc, char **argv)
705 char *dup, *tmp, *tok;
707 while ((ch = getopt(argc, argv, "")) != -1)
715 events = "play,pause,stop,next,prev,jump,repeat,add,load,seek";
719 tmp = dup = xstrdup(events);
720 while ((tok = strsep(&tmp, ",")) != NULL) {
725 if (!strcmp(tok, "play"))
726 res->monitor[IMSG_CTL_PLAY] = 1;
727 else if (!strcmp(tok, "pause"))
728 res->monitor[IMSG_CTL_PAUSE] = 1;
729 else if (!strcmp(tok, "stop"))
730 res->monitor[IMSG_CTL_STOP] = 1;
731 else if (!strcmp(tok, "next"))
732 res->monitor[IMSG_CTL_NEXT] = 1;
733 else if (!strcmp(tok, "prev"))
734 res->monitor[IMSG_CTL_PREV] = 1;
735 else if (!strcmp(tok, "jump"))
736 res->monitor[IMSG_CTL_JUMP] = 1;
737 else if (!strcmp(tok, "repeat"))
738 res->monitor[IMSG_CTL_REPEAT] = 1;
739 else if (!strcmp(tok, "add"))
740 res->monitor[IMSG_CTL_ADD] = 1;
741 else if (!strcmp(tok, "load"))
742 res->monitor[IMSG_CTL_COMMIT] = 1;
743 else if (!strcmp(tok, "seek"))
744 res->monitor[IMSG_CTL_SEEK] = 1;
746 log_warnx("unknown event \"%s\"", tok);
754 return ctlaction(res);
758 ctl_seek(struct parse_result *res, int argc, char **argv)
762 int hours = 0, minutes = 0, seconds = 0;
766 /* skip the command name */
771 if (argc > 0 && !strcmp(*argv, "--")) {
780 if (*n == '-' || *n == '+')
781 res->seek.relative = 1;
787 seconds = strtol(n, &ep, 10);
789 (*ep != '\0' && *ep != ':' && *ep != '%') ||
790 (*ep == '%' && ep[1] != '\0'))
791 fatalx("invalid offset: %s", argv[0]);
792 if (*ep == '\0' || *ep == '%') {
793 res->seek.percent = *ep == '%';
799 seconds = strtol(n, &ep, 10);
800 if (n[0] == '\0' || (*ep != '\0' && *ep != ':'))
801 fatalx("invalid offset: %s", argv[0]);
808 seconds = strtol(n, &ep, 10);
809 if (n[0] == '\0' || *ep != '\0')
810 fatalx("invalid offset: %s", argv[0]);
813 res->seek.offset = sign * (hours * 3600 + minutes * 60 + seconds);
814 return ctlaction(res);
818 ctl_status(struct parse_result *res, int argc, char **argv)
822 while ((ch = getopt(argc, argv, "f:")) != -1) {
825 res->status_format = optarg;
837 return ctlaction(res);
841 ctl_get_lock(const char *lockfile)
845 if ((lockfd = open(lockfile, O_WRONLY|O_CREAT, 0600)) == -1) {
846 log_debug("open failed: %s", strerror(errno));
850 if (flock(lockfd, LOCK_EX|LOCK_NB) == -1) {
851 log_debug("flock failed: %s", strerror(errno));
854 while (flock(lockfd, LOCK_EX) == -1 && errno == EINTR)
859 log_debug("flock succeeded");
867 struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
868 struct sockaddr_un sa;
870 int fd, lockfd = -1, locked = 0, spawned = 0;
872 char *lockfile = NULL;
874 memset(&sa, 0, sizeof(sa));
875 sa.sun_family = AF_UNIX;
876 size = strlcpy(sa.sun_path, csock, sizeof(sa.sun_path));
877 if (size >= sizeof(sa.sun_path)) {
878 errno = ENAMETOOLONG;
883 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
886 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
887 log_debug("connection failed: %s", strerror(errno));
888 if (errno != ECONNREFUSED && errno != ENOENT)
890 if (attempt++ == 100)
895 xasprintf(&lockfile, "%s.lock", csock);
896 if ((lockfd = ctl_get_lock(lockfile)) < 0) {
897 log_debug("didn't get the lock (%d)", lockfd);
907 * Always retry at least once, even if we got
908 * the lock, because another client could have
909 * taken the lock, started the server and released
910 * the lock between our connect() and flock()
917 log_debug("spawning the daemon");
922 nanosleep(&ts, NULL);
926 if (locked && lockfd >= 0) {
943 ctl(int argc, char **argv)
945 struct parse_result res;
949 memset(&res, 0, sizeof(res));
950 if ((fmt = getenv("AMUSED_STATUS_FORMAT")) == NULL)
951 fmt = "status,time,repeat";
952 res.status_format = fmt;
954 log_init(1, LOG_DAEMON);
955 log_setverbose(verbose);
957 if (getcwd(cwd, sizeof(cwd)) == NULL)
960 if ((ctl_sock = ctl_connect()) == -1)
961 fatal("can't connect");
964 fatalx("failed to connect to the daemon");
966 ibuf = xmalloc(sizeof(*ibuf));
967 imsg_init(ibuf, ctl_sock);
972 exit(parse(&res, argc, argv));