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..."},
53 { "flush", FLUSH, ctl_noarg, ""},
54 { "jump", JUMP, ctl_jump, "pattern"},
55 { "load", LOAD, ctl_load, "[file]"},
56 { "monitor", MONITOR, ctl_monitor, "[events]"},
57 { "next", NEXT, ctl_noarg, ""},
58 { "pause", PAUSE, ctl_noarg, ""},
59 { "play", PLAY, ctl_noarg, ""},
60 { "prev", PREV, ctl_noarg, ""},
61 { "repeat", REPEAT, ctl_repeat, "one|all on|off"},
62 { "restart", RESTART, ctl_noarg, ""},
63 { "seek", SEEK, ctl_seek, "[+-]time[%]"},
64 { "show", SHOW, ctl_show, "[-p]"},
65 { "status", STATUS, ctl_status, "[-f fmt]"},
66 { "stop", STOP, ctl_noarg, ""},
67 { "toggle", TOGGLE, ctl_noarg, ""},
68 { NULL, 0, NULL, NULL},
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 status = ctl->main(res, argc, argv);
169 load_files(struct parse_result *res, int *ret)
174 size_t linesize = 0, i = 0;
175 ssize_t linelen, curr = -1;
177 while ((linelen = getline(&line, &linesize, res->fp)) != -1) {
180 line[linelen-1] = '\0';
182 if (!strncmp(file, "> ", 2)) {
185 } else if (!strncmp(file, " ", 2))
188 memset(path, 0, sizeof(path));
189 if (canonpath(file, path, sizeof(path)) == -1) {
190 log_warn("canonpath %s", file);
195 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
210 imsg_compose(ibuf, IMSG_CTL_COMMIT, 0, 0, -1,
211 &curr, sizeof(curr));
217 imsg_strerror(struct imsg *imsg)
222 datalen = IMSG_DATA_SIZE(*imsg);
224 if (datalen == 0 || msg[datalen-1] != '\0')
225 fatalx("malformed error message");
248 case IMSG_CTL_COMMIT:
258 print_time(const char *label, int64_t seconds, const char *suffx)
265 hours = seconds / 3600;
266 seconds -= hours * 3600;
268 minutes = seconds / 60;
269 seconds -= minutes * 60;
273 printf("%02d:", hours);
274 printf("%02d:%02lld%s", minutes, (long long)seconds, suffx);
278 print_status(struct player_status *ps, const char *spec)
282 char *dup, *tmp, *tok;
284 if (ps->status == STATE_STOPPED)
286 else if (ps->status == STATE_PLAYING)
288 else if (ps->status == STATE_PAUSED)
293 percent = 100.0 * (double)ps->position / (double)ps->duration;
295 tmp = dup = xstrdup(spec);
296 while ((tok = strsep(&tmp, ",")) != NULL) {
300 if (!strcmp(tok, "path")) {
302 } else if (!strcmp(tok, "repeat:oneline")) {
303 printf("repeat one:%s ",
304 ps->rp.repeat_one ? "on" : "off");
305 printf("all:%s\n", ps->rp.repeat_all ? "on" : "off");
306 } else if (!strcmp(tok, "repeat")) {
307 printf("repeat one %s\n",
308 ps->rp.repeat_one ? "on" : "off");
309 printf("repeat all %s\n",
310 ps->rp.repeat_all ? "on" : "off");
311 } else if (!strcmp(tok, "status")) {
312 printf("%s %s\n", status, ps->path);
313 } else if (!strcmp(tok, "time:oneline")) {
314 print_time("time ", ps->position, " / ");
315 print_time("", ps->duration, "\n");
316 } else if (!strcmp(tok, "time:percentage")) {
317 printf("position %.2f%%\n", percent);
318 } else if (!strcmp(tok, "time:raw")) {
319 printf("position %lld\n", (long long)ps->position);
320 printf("duration %lld\n", (long long)ps->duration);
321 } else if (!strcmp(tok, "time")) {
322 print_time("position ", ps->position, "\n");
323 print_time("duration ", ps->duration, "\n");
331 ctlaction(struct parse_result *res)
335 struct player_status ps;
338 int i, type, ret = 0, done = 1;
340 if (pledge("stdio", NULL) == -1)
343 switch (res->action) {
345 imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
347 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
353 imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
356 imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
358 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
364 imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
368 for (i = 0; res->files[i] != NULL; ++i) {
369 memset(path, 0, sizeof(path));
370 if (canonpath(res->files[i], path, sizeof(path))
372 log_warn("canonpath %s", res->files[i]);
376 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
382 imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
386 imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
390 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
393 imsg_compose(ibuf, IMSG_CTL_NEXT, 0, 0, -1, NULL, 0);
395 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
401 imsg_compose(ibuf, IMSG_CTL_PREV, 0, 0, -1, NULL, 0);
403 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
410 imsg_compose(ibuf, IMSG_CTL_BEGIN, 0, 0, -1, NULL, 0);
414 memset(path, 0, sizeof(path));
415 strlcpy(path, res->files[0], sizeof(path));
416 imsg_compose(ibuf, IMSG_CTL_JUMP, 0, 0, -1,
420 imsg_compose(ibuf, IMSG_CTL_REPEAT, 0, 0, -1,
421 &res->rep, sizeof(res->rep));
425 imsg_compose(ibuf, IMSG_CTL_MONITOR, 0, 0, -1,
429 memset(&res->seek, 0, sizeof(res->seek));
432 imsg_compose(ibuf, IMSG_CTL_SEEK, 0, 0, -1, &res->seek,
436 /* action not expected */
437 fatalx("invalid action %u", res->action);
448 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
449 fatalx("imsg_read error");
451 fatalx("pipe closed");
454 if ((n = imsg_get(ibuf, &imsg)) == -1)
455 fatalx("imsg_get error");
459 if (imsg.hdr.type == IMSG_CTL_ERR) {
460 log_warnx("%s: %s", res->ctl->name,
461 imsg_strerror(&imsg));
467 datalen = IMSG_DATA_SIZE(imsg);
469 switch (res->action) {
471 if (res->files[i] == NULL)
472 fatalx("received more replies than "
475 if (imsg.hdr.type == IMSG_CTL_ADD)
476 log_debug("enqueued %s", res->files[i]);
478 fatalx("invalid message %d",
481 done = res->files[i] == NULL;
488 if (datalen != sizeof(ps))
489 fatalx("data size mismatch");
490 memcpy(&ps, imsg.data, sizeof(ps));
491 if (ps.path[sizeof(ps.path) - 1] != '\0')
492 fatalx("received corrupted data");
495 if (ps.status == STATE_PLAYING)
507 if (imsg.hdr.type != IMSG_CTL_STATUS)
508 fatalx("invalid message %d",
511 if (datalen != sizeof(ps))
512 fatalx("data size mismatch");
513 memcpy(&ps, imsg.data, sizeof(ps));
514 if (ps.path[sizeof(ps.path) - 1] != '\0')
515 fatalx("received corrupted data");
517 print_status(&ps, res->status_format);
521 if (imsg.hdr.type == IMSG_CTL_ADD)
523 if (imsg.hdr.type == IMSG_CTL_COMMIT) {
528 if (imsg.hdr.type != IMSG_CTL_BEGIN)
529 fatalx("invalid message %d",
532 load_files(res, &ret);
535 if (imsg.hdr.type != IMSG_CTL_MONITOR)
536 fatalx("invalid message %d",
539 if (datalen != sizeof(type))
540 fatalx("data size mismatch");
542 memcpy(&type, imsg.data, sizeof(type));
543 if (type < 0 || type > IMSG__LAST)
544 fatalx("received corrupted data");
546 if (!res->monitor[type])
549 puts(event_name(type));
566 ctl_noarg(struct parse_result *res, int argc, char **argv)
570 while ((ch = getopt(argc, argv, "")) != -1)
578 return ctlaction(res);
582 ctl_add(struct parse_result *res, int argc, char **argv)
586 while ((ch = getopt(argc, argv, "")) != -1)
595 return ctlaction(res);
599 ctl_show(struct parse_result *res, int argc, char **argv)
603 while ((ch = getopt(argc, argv, "p")) != -1) {
613 return ctlaction(res);
617 ctl_load(struct parse_result *res, int argc, char **argv)
621 while ((ch = getopt(argc, argv, "")) != -1)
631 if ((res->fp = fopen(argv[0], "r")) == NULL)
632 fatal("can't open %s", argv[0]);
635 return ctlaction(res);
639 ctl_jump(struct parse_result *res, int argc, char **argv)
643 while ((ch = getopt(argc, argv, "")) != -1)
652 return ctlaction(res);
656 ctl_repeat(struct parse_result *res, int argc, char **argv)
660 while ((ch = getopt(argc, argv, "")) != -1)
668 if (!strcmp(argv[1], "on"))
670 else if (!strcmp(argv[1], "off"))
675 res->rep.repeat_one = -1;
676 res->rep.repeat_all = -1;
677 if (!strcmp(argv[0], "one"))
678 res->rep.repeat_one = b;
679 else if (!strcmp(argv[0], "all"))
680 res->rep.repeat_all = b;
684 return ctlaction(res);
688 ctl_monitor(struct parse_result *res, int argc, char **argv)
692 char *dup, *tmp, *tok;
694 while ((ch = getopt(argc, argv, "")) != -1)
702 events = "play,pause,stop,next,prev,jump,repeat,add,load,seek";
706 tmp = dup = xstrdup(events);
707 while ((tok = strsep(&tmp, ",")) != NULL) {
712 if (!strcmp(tok, "play"))
713 res->monitor[IMSG_CTL_PLAY] = 1;
714 else if (!strcmp(tok, "pause"))
715 res->monitor[IMSG_CTL_PAUSE] = 1;
716 else if (!strcmp(tok, "stop"))
717 res->monitor[IMSG_CTL_STOP] = 1;
718 else if (!strcmp(tok, "next"))
719 res->monitor[IMSG_CTL_NEXT] = 1;
720 else if (!strcmp(tok, "prev"))
721 res->monitor[IMSG_CTL_PREV] = 1;
722 else if (!strcmp(tok, "jump"))
723 res->monitor[IMSG_CTL_JUMP] = 1;
724 else if (!strcmp(tok, "repeat"))
725 res->monitor[IMSG_CTL_REPEAT] = 1;
726 else if (!strcmp(tok, "add"))
727 res->monitor[IMSG_CTL_ADD] = 1;
728 else if (!strcmp(tok, "load"))
729 res->monitor[IMSG_CTL_COMMIT] = 1;
730 else if (!strcmp(tok, "seek"))
731 res->monitor[IMSG_CTL_SEEK] = 1;
733 log_warnx("unknown event \"%s\"", tok);
741 return ctlaction(res);
745 ctl_seek(struct parse_result *res, int argc, char **argv)
749 int hours = 0, minutes = 0, seconds = 0;
753 /* skip the command name */
758 if (argc > 0 && !strcmp(*argv, "--")) {
767 if (*n == '-' || *n == '+')
768 res->seek.relative = 1;
774 seconds = strtol(n, &ep, 10);
776 (*ep != '\0' && *ep != ':' && *ep != '%') ||
777 (*ep == '%' && ep[1] != '\0'))
778 fatalx("invalid offset: %s", argv[0]);
779 if (*ep == '\0' || *ep == '%') {
780 res->seek.percent = *ep == '%';
786 seconds = strtol(n, &ep, 10);
787 if (n[0] == '\0' || (*ep != '\0' && *ep != ':'))
788 fatalx("invalid offset: %s", argv[0]);
795 seconds = strtol(n, &ep, 10);
796 if (n[0] == '\0' || *ep != '\0')
797 fatalx("invalid offset: %s", argv[0]);
800 res->seek.offset = sign * (hours * 3600 + minutes * 60 + seconds);
801 return ctlaction(res);
805 ctl_status(struct parse_result *res, int argc, char **argv)
809 while ((ch = getopt(argc, argv, "f:")) != -1) {
812 res->status_format = optarg;
824 return ctlaction(res);
828 ctl_get_lock(const char *lockfile)
832 if ((lockfd = open(lockfile, O_WRONLY|O_CREAT, 0600)) == -1) {
833 log_debug("open failed: %s", strerror(errno));
837 if (flock(lockfd, LOCK_EX|LOCK_NB) == -1) {
838 log_debug("flock failed: %s", strerror(errno));
841 while (flock(lockfd, LOCK_EX) == -1 && errno == EINTR)
846 log_debug("flock succeeded");
854 struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
855 struct sockaddr_un sa;
857 int fd, lockfd = -1, locked = 0, spawned = 0;
859 char *lockfile = NULL;
861 memset(&sa, 0, sizeof(sa));
862 sa.sun_family = AF_UNIX;
863 size = strlcpy(sa.sun_path, csock, sizeof(sa.sun_path));
864 if (size >= sizeof(sa.sun_path)) {
865 errno = ENAMETOOLONG;
870 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
873 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
874 log_debug("connection failed: %s", strerror(errno));
875 if (errno != ECONNREFUSED && errno != ENOENT)
877 if (attempt++ == 100)
882 xasprintf(&lockfile, "%s.lock", csock);
883 if ((lockfd = ctl_get_lock(lockfile)) < 0) {
884 log_debug("didn't get the lock (%d)", lockfd);
894 * Always retry at least once, even if we got
895 * the lock, because another client could have
896 * taken the lock, started the server and released
897 * the lock between our connect() and flock()
904 log_debug("spawning the daemon");
909 nanosleep(&ts, NULL);
913 if (locked && lockfd >= 0) {
930 ctl(int argc, char **argv)
932 struct parse_result res;
936 memset(&res, 0, sizeof(res));
937 if ((fmt = getenv("AMUSED_STATUS_FORMAT")) == NULL)
938 fmt = "status,time,repeat";
939 res.status_format = fmt;
941 log_init(1, LOG_DAEMON);
942 log_setverbose(verbose);
944 if (getcwd(cwd, sizeof(cwd)) == NULL)
947 if ((ctl_sock = ctl_connect()) == -1)
948 fatal("can't connect");
951 fatalx("failed to connect to the daemon");
953 ibuf = xmalloc(sizeof(*ibuf));
954 imsg_init(ibuf, ctl_sock);
959 /* we'll drop rpath too later in ctlaction */
960 if (pledge("stdio rpath", NULL) == -1)
963 exit(parse(&res, argc, argv));