2 * Copyright (c) 2022, 2023 Omar Polo <op@omarpolo.com>
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>
39 static struct imsgbuf *imsgbuf;
42 static int ctl_noarg(struct parse_result *, int, char **);
43 static int ctl_add(struct parse_result *, int, char **);
44 static int ctl_show(struct parse_result *, int, char **);
45 static int ctl_load(struct parse_result *, int, char **);
46 static int ctl_jump(struct parse_result *, int, char **);
47 static int ctl_repeat(struct parse_result *, int, char **);
48 static int ctl_consume(struct parse_result *, int, char **);
49 static int ctl_monitor(struct parse_result *, int, char **);
50 static int ctl_seek(struct parse_result *, int, char **);
51 static int ctl_status(struct parse_result *, int, char **);
53 struct ctl_command ctl_commands[] = {
54 { "add", ADD, ctl_add, "files..."},
55 { "consume", MODE, ctl_consume, "one|all"},
56 { "flush", FLUSH, ctl_noarg, ""},
57 { "jump", JUMP, ctl_jump, "pattern"},
58 { "load", LOAD, ctl_load, "[file]"},
59 { "monitor", MONITOR, ctl_monitor, "[events]"},
60 { "next", NEXT, ctl_noarg, ""},
61 { "pause", PAUSE, ctl_noarg, ""},
62 { "play", PLAY, ctl_noarg, ""},
63 { "prev", PREV, ctl_noarg, ""},
64 { "repeat", MODE, ctl_repeat, "one|all on|off"},
65 { "restart", RESTART, ctl_noarg, ""},
66 { "seek", SEEK, ctl_seek, "[+-]time[%]"},
67 { "show", SHOW, ctl_show, "[-p]"},
68 { "status", STATUS, ctl_status, "[-f fmt]"},
69 { "stop", STOP, ctl_noarg, ""},
70 { "toggle", TOGGLE, ctl_noarg, ""},
71 { NULL, 0, NULL, NULL},
77 fprintf(stderr, "usage: %s [-dv] [-s socket]\n", getprogname());
82 ctl_usage(struct ctl_command *ctl)
84 fprintf(stderr, "usage: %s [-v] [-s socket] %s %s\n", getprogname(),
85 ctl->name, ctl->usage);
89 /* based on canonpath from kern_pledge.c */
91 canonpath(const char *input, char *buf, size_t bufsize)
94 char *q, path[PATH_MAX];
97 if (input[0] != '/') {
98 r = snprintf(path, sizeof(path), "%s/%s", cwd, input);
99 if (r < 0 || (size_t)r >= sizeof(path)) {
100 errno = ENAMETOOLONG;
108 while (*p && (q - buf < bufsize)) {
109 if (p[0] == '/' && (p[1] == '/' || p[1] == '\0')) {
112 } else if (p[0] == '/' && p[1] == '.' &&
113 (p[2] == '/' || p[2] == '\0')) {
116 } else if (p[0] == '/' && p[1] == '.' && p[2] == '.' &&
117 (p[3] == '/' || p[3] == '\0')) {
119 if (q != buf) /* "/../" at start of buf */
127 if ((*p == '\0') && (q - buf < bufsize)) {
131 errno = ENAMETOOLONG;
137 parse(struct parse_result *res, int argc, char **argv)
139 struct ctl_command *ctl = NULL;
143 if ((argv0 = argv[0]) == NULL)
146 for (i = 0; ctl_commands[i].name != NULL; ++i) {
147 if (strncmp(ctl_commands[i].name, argv0, strlen(argv0))
151 "ambiguous argument: %s\n", argv0);
154 ctl = &ctl_commands[i];
159 fprintf(stderr, "unknown argument: %s\n", argv[0]);
163 res->action = ctl->action;
166 status = ctl->main(res, argc, argv);
173 load_files(struct parse_result *res, int *ret)
178 size_t linesize = 0, i = 0;
179 ssize_t linelen, curr = -1;
181 while ((linelen = getline(&line, &linesize, res->fp)) != -1) {
184 line[linelen-1] = '\0';
186 if (!strncmp(file, "> ", 2)) {
189 } else if (!strncmp(file, " ", 2))
192 memset(path, 0, sizeof(path));
193 if (canonpath(file, path, sizeof(path)) == -1) {
194 log_warn("canonpath %s", file);
199 imsg_compose(imsgbuf, IMSG_CTL_ADD, 0, 0, -1,
209 imsg_compose(imsgbuf, IMSG_CTL_COMMIT, 0, 0, -1,
210 &curr, sizeof(curr));
216 imsg_strerror(struct imsg *imsg)
222 if (imsg_get_ibuf(imsg, &ibuf) == -1 ||
223 (datalen = ibuf_size(&ibuf)) == 0 ||
224 (msg = ibuf_data(&ibuf)) == NULL ||
225 msg[datalen - 1] != '\0')
226 fatalx("malformed error message");
249 case IMSG_CTL_COMMIT:
261 print_time(const char *label, int64_t seconds, const char *suffx)
268 hours = seconds / 3600;
269 seconds -= hours * 3600;
271 minutes = seconds / 60;
272 seconds -= minutes * 60;
276 printf("%02d:", hours);
277 printf("%02d:%02lld%s", minutes, (long long)seconds, suffx);
281 print_status(struct player_status *ps, const char *spec)
285 char *dup, *tmp, *tok;
287 if (ps->status == STATE_STOPPED)
289 else if (ps->status == STATE_PLAYING)
291 else if (ps->status == STATE_PAUSED)
296 percent = 100.0 * (double)ps->position / (double)ps->duration;
298 tmp = dup = xstrdup(spec);
299 while ((tok = strsep(&tmp, ",")) != NULL) {
303 if (!strcmp(tok, "path")) {
305 } else if (!strcmp(tok, "mode:oneline")) {
306 printf("repeat one:%s ",
307 ps->mode.repeat_one ? "on" : "off");
308 printf("all:%s ", ps->mode.repeat_all ? "on" : "off");
309 printf("consume:%s\n", ps->mode.consume ? "on" : "off");
310 } else if (!strcmp(tok, "mode")) {
311 printf("repeat all %s\n",
312 ps->mode.repeat_all ? "on" : "off");
313 printf("repeat one %s\n",
314 ps->mode.repeat_one ? "on" : "off");
315 printf("consume %s\n",
316 ps->mode.consume ? "on" : "off");
317 } else if (!strcmp(tok, "status")) {
318 printf("%s %s\n", status, ps->path);
319 } else if (!strcmp(tok, "time:oneline")) {
320 print_time("time ", ps->position, " / ");
321 print_time("", ps->duration, "\n");
322 } else if (!strcmp(tok, "time:percentage")) {
323 printf("position %.2f%%\n", percent);
324 } else if (!strcmp(tok, "time:raw")) {
325 printf("position %lld\n", (long long)ps->position);
326 printf("duration %lld\n", (long long)ps->duration);
327 } else if (!strcmp(tok, "time")) {
328 print_time("position ", ps->position, "\n");
329 print_time("duration ", ps->duration, "\n");
337 print_monitor_event(struct player_event *ev)
341 printf("%s repeat one:%s all:%s consume:%s\n",
342 event_name(ev->event),
343 ev->mode.repeat_one ? "on" : "off",
344 ev->mode.repeat_all ? "on" : "off",
345 ev->mode.consume ? "on" : "off");
348 printf("%s %lld %lld\n", event_name(ev->event),
349 (long long)ev->position, (long long)ev->duration);
352 puts(event_name(ev->event));
360 ctlaction(struct parse_result *res)
364 struct player_status ps;
365 struct player_event ev;
367 int i, type, ret = 0, done = 1;
369 if (pledge("stdio", NULL) == -1)
372 switch (res->action) {
374 imsg_compose(imsgbuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
376 imsg_compose(imsgbuf, IMSG_CTL_STATUS, 0, 0, -1,
382 imsg_compose(imsgbuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
385 imsg_compose(imsgbuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
387 imsg_compose(imsgbuf, IMSG_CTL_STATUS, 0, 0, -1,
393 imsg_compose(imsgbuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
397 for (i = 0; res->files[i] != NULL; ++i) {
398 memset(path, 0, sizeof(path));
399 if (canonpath(res->files[i], path, sizeof(path))
401 log_warn("canonpath %s", res->files[i]);
405 imsg_compose(imsgbuf, IMSG_CTL_ADD, 0, 0, -1,
411 imsg_compose(imsgbuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
415 imsg_compose(imsgbuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
419 imsg_compose(imsgbuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
422 imsg_compose(imsgbuf, IMSG_CTL_NEXT, 0, 0, -1, NULL, 0);
424 imsg_compose(imsgbuf, IMSG_CTL_STATUS, 0, 0, -1,
430 imsg_compose(imsgbuf, IMSG_CTL_PREV, 0, 0, -1, NULL, 0);
432 imsg_compose(imsgbuf, IMSG_CTL_STATUS, 0, 0, -1,
439 imsg_compose(imsgbuf, IMSG_CTL_BEGIN, 0, 0, -1, NULL, 0);
443 memset(path, 0, sizeof(path));
444 strlcpy(path, res->files[0], sizeof(path));
445 imsg_compose(imsgbuf, IMSG_CTL_JUMP, 0, 0, -1,
450 imsg_compose(imsgbuf, IMSG_CTL_MODE, 0, 0, -1,
451 &res->mode, sizeof(res->mode));
452 imsg_compose(imsgbuf, IMSG_CTL_STATUS, 0, 0, -1,
453 &res->mode, sizeof(res->mode));
454 res->status_format = "mode:oneline";
456 res->status_format = "mode";
460 imsg_compose(imsgbuf, IMSG_CTL_MONITOR, 0, 0, -1,
464 memset(&res->seek, 0, sizeof(res->seek));
467 imsg_compose(imsgbuf, IMSG_CTL_SEEK, 0, 0, -1, &res->seek,
471 /* action not expected */
472 fatalx("invalid action %u", res->action);
483 if ((n = imsg_read(imsgbuf)) == -1 && errno != EAGAIN)
484 fatalx("imsg_read error");
486 fatalx("pipe closed");
489 if ((n = imsg_get(imsgbuf, &imsg)) == -1)
490 fatalx("imsg_get error");
494 type = imsg_get_type(&imsg);
495 if (type == IMSG_CTL_ERR) {
496 log_warnx("%s: %s", res->ctl->name,
497 imsg_strerror(&imsg));
503 switch (res->action) {
505 if (res->files[i] == NULL)
506 fatalx("received more replies than "
509 if (type == IMSG_CTL_ADD)
510 log_debug("enqueued %s", res->files[i]);
512 fatalx("invalid message %d", type);
514 done = res->files[i] == NULL;
517 if (imsg_get_len(&imsg) == 0) {
521 if (imsg_get_data(&imsg, &ps, sizeof(ps))
523 fatalx("data size mismatch");
524 if (ps.path[sizeof(ps.path) - 1] != '\0')
525 fatalx("received corrupted data");
528 if (ps.status == STATE_PLAYING)
541 if (type != IMSG_CTL_STATUS)
542 fatalx("invalid message %d", type);
544 if (imsg_get_data(&imsg, &ps, sizeof(ps))
546 fatalx("data size mismatch");
547 if (ps.path[sizeof(ps.path) - 1] != '\0')
548 fatalx("received corrupted data");
550 print_status(&ps, res->status_format);
554 if (type == IMSG_CTL_ADD)
556 if (type == IMSG_CTL_COMMIT) {
561 if (type != IMSG_CTL_BEGIN)
562 fatalx("invalid message %d", type);
564 load_files(res, &ret);
567 if (type != IMSG_CTL_MONITOR)
568 fatalx("invalid message %d", type);
570 if (imsg_get_data(&imsg, &ev, sizeof(ev))
572 fatalx("data size mismatch");
574 if (ev.event < 0 || ev.event > IMSG__LAST)
575 fatalx("received corrupted data");
577 if (!res->monitor[ev.event])
580 print_monitor_event(&ev);
596 ctl_noarg(struct parse_result *res, int argc, char **argv)
600 while ((ch = getopt(argc, argv, "")) != -1)
608 return ctlaction(res);
612 ctl_add(struct parse_result *res, int argc, char **argv)
616 while ((ch = getopt(argc, argv, "")) != -1)
625 return ctlaction(res);
629 ctl_show(struct parse_result *res, int argc, char **argv)
633 while ((ch = getopt(argc, argv, "p")) != -1) {
643 return ctlaction(res);
647 ctl_load(struct parse_result *res, int argc, char **argv)
651 while ((ch = getopt(argc, argv, "")) != -1)
661 if ((res->fp = fopen(argv[0], "r")) == NULL)
662 fatal("can't open %s", argv[0]);
665 return ctlaction(res);
669 ctl_jump(struct parse_result *res, int argc, char **argv)
673 while ((ch = getopt(argc, argv, "")) != -1)
682 return ctlaction(res);
686 parse_mode(struct parse_result *res, const char *v)
690 if (!strcmp(v, "on"))
692 if (!strcmp(v, "off"))
698 ctl_repeat(struct parse_result *res, int argc, char **argv)
702 while ((ch = getopt(argc, argv, "")) != -1)
707 if (argc != 1 && argc != 2)
710 if (!strcmp(argv[0], "one"))
711 res->mode.repeat_one = parse_mode(res, argv[1]);
712 else if (!strcmp(argv[0], "all"))
713 res->mode.repeat_all = parse_mode(res, argv[1]);
717 return ctlaction(res);
721 ctl_consume(struct parse_result *res, int argc, char **argv)
725 while ((ch = getopt(argc, argv, "")) != -1)
733 res->mode.consume = parse_mode(res, argv[0]);
734 return ctlaction(res);
738 ctl_monitor(struct parse_result *res, int argc, char **argv)
742 char *dup, *tmp, *tok;
744 while ((ch = getopt(argc, argv, "")) != -1)
752 events = "play,pause,stop,next,prev,jump,mode,add,load,seek";
756 tmp = dup = xstrdup(events);
757 while ((tok = strsep(&tmp, ",")) != NULL) {
762 if (!strcmp(tok, "play"))
763 res->monitor[IMSG_CTL_PLAY] = 1;
764 else if (!strcmp(tok, "pause"))
765 res->monitor[IMSG_CTL_PAUSE] = 1;
766 else if (!strcmp(tok, "stop"))
767 res->monitor[IMSG_CTL_STOP] = 1;
768 else if (!strcmp(tok, "next"))
769 res->monitor[IMSG_CTL_NEXT] = 1;
770 else if (!strcmp(tok, "prev"))
771 res->monitor[IMSG_CTL_PREV] = 1;
772 else if (!strcmp(tok, "jump"))
773 res->monitor[IMSG_CTL_JUMP] = 1;
774 else if (!strcmp(tok, "mode"))
775 res->monitor[IMSG_CTL_MODE] = 1;
776 else if (!strcmp(tok, "add"))
777 res->monitor[IMSG_CTL_ADD] = 1;
778 else if (!strcmp(tok, "load"))
779 res->monitor[IMSG_CTL_COMMIT] = 1;
780 else if (!strcmp(tok, "seek"))
781 res->monitor[IMSG_CTL_SEEK] = 1;
783 log_warnx("unknown event \"%s\"", tok);
791 return ctlaction(res);
795 ctl_seek(struct parse_result *res, int argc, char **argv)
799 int hours = 0, minutes = 0, seconds = 0;
803 /* skip the command name */
808 if (argc > 0 && !strcmp(*argv, "--")) {
817 if (*n == '-' || *n == '+')
818 res->seek.relative = 1;
824 seconds = strtol(n, &ep, 10);
826 (*ep != '\0' && *ep != ':' && *ep != '%') ||
827 (*ep == '%' && ep[1] != '\0'))
828 fatalx("invalid offset: %s", argv[0]);
829 if (*ep == '\0' || *ep == '%') {
830 res->seek.percent = *ep == '%';
836 seconds = strtol(n, &ep, 10);
837 if (n[0] == '\0' || (*ep != '\0' && *ep != ':'))
838 fatalx("invalid offset: %s", argv[0]);
845 seconds = strtol(n, &ep, 10);
846 if (n[0] == '\0' || *ep != '\0')
847 fatalx("invalid offset: %s", argv[0]);
850 res->seek.offset = sign * (hours * 3600 + minutes * 60 + seconds);
851 return ctlaction(res);
855 ctl_status(struct parse_result *res, int argc, char **argv)
859 while ((ch = getopt(argc, argv, "f:")) != -1) {
862 res->status_format = optarg;
874 return ctlaction(res);
878 ctl_get_lock(const char *lockfile)
882 if ((lockfd = open(lockfile, O_WRONLY|O_CREAT, 0600)) == -1) {
883 log_debug("open failed: %s", strerror(errno));
887 if (flock(lockfd, LOCK_EX|LOCK_NB) == -1) {
888 log_debug("flock failed: %s", strerror(errno));
891 while (flock(lockfd, LOCK_EX) == -1 && errno == EINTR)
896 log_debug("flock succeeded");
904 struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
905 struct sockaddr_un sa;
907 int fd, lockfd = -1, locked = 0, spawned = 0;
909 char *lockfile = NULL;
911 memset(&sa, 0, sizeof(sa));
912 sa.sun_family = AF_UNIX;
913 size = strlcpy(sa.sun_path, csock, sizeof(sa.sun_path));
914 if (size >= sizeof(sa.sun_path)) {
915 errno = ENAMETOOLONG;
920 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
923 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
924 log_debug("connection failed: %s", strerror(errno));
925 if (errno != ECONNREFUSED && errno != ENOENT)
927 if (attempt++ == 100)
932 xasprintf(&lockfile, "%s.lock", csock);
933 if ((lockfd = ctl_get_lock(lockfile)) < 0) {
934 log_debug("didn't get the lock (%d)", lockfd);
944 * Always retry at least once, even if we got
945 * the lock, because another client could have
946 * taken the lock, started the server and released
947 * the lock between our connect() and flock()
954 log_debug("spawning the daemon");
959 nanosleep(&ts, NULL);
963 if (locked && lockfd >= 0) {
980 ctl(int argc, char **argv)
982 struct parse_result res;
986 memset(&res, 0, sizeof(res));
987 if ((fmt = getenv("AMUSED_STATUS_FORMAT")) == NULL)
988 fmt = "status,time:oneline,mode:oneline";
989 res.status_format = fmt;
991 res.mode.consume = MODE_UNDEF;
992 res.mode.repeat_all = MODE_UNDEF;
993 res.mode.repeat_one = MODE_UNDEF;
995 log_init(1, LOG_DAEMON);
996 log_setverbose(verbose);
998 if (getcwd(cwd, sizeof(cwd)) == NULL)
1001 if ((ctl_sock = ctl_connect()) == -1)
1002 fatal("can't connect");
1004 imsgbuf = xmalloc(sizeof(*imsgbuf));
1005 imsg_init(imsgbuf, ctl_sock);
1010 /* we'll drop rpath too later in ctlaction */
1011 if (pledge("stdio rpath", NULL) == -1)
1014 exit(parse(&res, argc, argv));