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");
250 case IMSG_CTL_TOGGLE_PLAY:
264 case IMSG_CTL_REPEAT:
268 case IMSG_CTL_COMMIT:
276 print_time(const char *label, int64_t seconds, const char *suffx)
283 hours = seconds / 3600;
284 seconds -= hours * 3600;
286 minutes = seconds / 60;
287 seconds -= minutes * 60;
291 printf("%02d:", hours);
292 printf("%02d:%02lld%s", minutes, (long long)seconds, suffx);
296 print_status(struct player_status *ps, const char *spec)
300 char *dup, *tmp, *tok;
302 if (ps->status == STATE_STOPPED)
304 else if (ps->status == STATE_PLAYING)
306 else if (ps->status == STATE_PAUSED)
311 percent = 100.0 * (double)ps->position / (double)ps->duration;
313 tmp = dup = xstrdup(spec);
314 while ((tok = strsep(&tmp, ",")) != NULL) {
318 if (!strcmp(tok, "path")) {
320 } else if (!strcmp(tok, "repeat:oneline")) {
321 printf("repeat one:%s ",
322 ps->rp.repeat_one ? "on" : "off");
323 printf("all:%s\n", ps->rp.repeat_all ? "on" : "off");
324 } else if (!strcmp(tok, "repeat")) {
325 printf("repeat one %s\n",
326 ps->rp.repeat_one ? "on" : "off");
327 printf("repeat all %s\n",
328 ps->rp.repeat_all ? "on" : "off");
329 } else if (!strcmp(tok, "status")) {
330 printf("%s %s\n", status, ps->path);
331 } else if (!strcmp(tok, "time:oneline")) {
332 print_time("time ", ps->position, " / ");
333 print_time("", ps->duration, "\n");
334 } else if (!strcmp(tok, "time:percentage")) {
335 printf("position %.2f%%\n", percent);
336 } else if (!strcmp(tok, "time:raw")) {
337 printf("position %lld\n", (long long)ps->position);
338 printf("duration %lld\n", (long long)ps->duration);
339 } else if (!strcmp(tok, "time")) {
340 print_time("position ", ps->position, "\n");
341 print_time("duration ", ps->duration, "\n");
349 ctlaction(struct parse_result *res)
353 struct player_status ps;
356 int i, type, ret = 0, done = 1;
358 switch (res->action) {
360 imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
362 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
368 imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
371 imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
373 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
379 imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
383 for (i = 0; res->files[i] != NULL; ++i) {
384 memset(path, 0, sizeof(path));
385 if (canonpath(res->files[i], path, sizeof(path))
387 log_warn("canonpath %s", res->files[i]);
391 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
397 imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
401 imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
405 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
408 imsg_compose(ibuf, IMSG_CTL_NEXT, 0, 0, -1, NULL, 0);
410 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
416 imsg_compose(ibuf, IMSG_CTL_PREV, 0, 0, -1, NULL, 0);
418 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
425 imsg_compose(ibuf, IMSG_CTL_BEGIN, 0, 0, -1, NULL, 0);
429 memset(path, 0, sizeof(path));
430 strlcpy(path, res->file, sizeof(path));
431 imsg_compose(ibuf, IMSG_CTL_JUMP, 0, 0, -1,
435 imsg_compose(ibuf, IMSG_CTL_REPEAT, 0, 0, -1,
436 &res->rep, sizeof(res->rep));
440 imsg_compose(ibuf, IMSG_CTL_MONITOR, 0, 0, -1,
444 memset(&res->seek, 0, sizeof(res->seek));
447 imsg_compose(ibuf, IMSG_CTL_SEEK, 0, 0, -1, &res->seek,
451 /* action not expected */
452 fatalx("invalid action %u", res->action);
463 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
464 fatalx("imsg_read error");
466 fatalx("pipe closed");
469 if ((n = imsg_get(ibuf, &imsg)) == -1)
470 fatalx("imsg_get error");
474 if (imsg.hdr.type == IMSG_CTL_ERR) {
475 log_warnx("%s: %s", res->ctl->name,
476 imsg_strerror(&imsg));
482 datalen = IMSG_DATA_SIZE(imsg);
484 switch (res->action) {
486 if (res->files[i] == NULL)
487 fatalx("received more replies than "
490 if (imsg.hdr.type == IMSG_CTL_ADD)
491 log_debug("enqueued %s", res->files[i]);
493 fatalx("invalid message %d",
496 done = res->files[i] == NULL;
503 if (datalen != sizeof(ps))
504 fatalx("data size mismatch");
505 memcpy(&ps, imsg.data, sizeof(ps));
506 if (ps.path[sizeof(ps.path) - 1] != '\0')
507 fatalx("received corrupted data");
510 if (ps.status == STATE_PLAYING)
523 if (imsg.hdr.type != IMSG_CTL_STATUS)
524 fatalx("invalid message %d",
527 if (datalen != sizeof(ps))
528 fatalx("data size mismatch");
529 memcpy(&ps, imsg.data, sizeof(ps));
530 if (ps.path[sizeof(ps.path) - 1] != '\0')
531 fatalx("received corrupted data");
533 print_status(&ps, res->status_format);
537 if (imsg.hdr.type == IMSG_CTL_ADD)
539 if (imsg.hdr.type == IMSG_CTL_COMMIT) {
544 if (imsg.hdr.type != IMSG_CTL_BEGIN)
545 fatalx("invalid message %d",
548 load_files(res, &ret);
551 if (imsg.hdr.type != IMSG_CTL_MONITOR)
552 fatalx("invalid message %d",
555 if (datalen != sizeof(type))
556 fatalx("data size mismatch");
558 memcpy(&type, imsg.data, sizeof(type));
559 if (type < 0 || type > IMSG__LAST)
560 fatalx("received corrupted data");
562 if (!res->monitor[type])
565 puts(imsg_name(type));
582 ctl_noarg(struct parse_result *res, int argc, char **argv)
586 while ((ch = getopt(argc, argv, "")) != -1)
594 return ctlaction(res);
598 ctl_add(struct parse_result *res, int argc, char **argv)
602 while ((ch = getopt(argc, argv, "")) != -1)
611 return ctlaction(res);
615 ctl_show(struct parse_result *res, int argc, char **argv)
619 while ((ch = getopt(argc, argv, "p")) != -1) {
629 return ctlaction(res);
633 ctl_load(struct parse_result *res, int argc, char **argv)
637 while ((ch = getopt(argc, argv, "")) != -1)
649 if (pledge("stdio rpath", NULL) == -1)
652 return ctlaction(res);
656 ctl_jump(struct parse_result *res, int argc, char **argv)
660 while ((ch = getopt(argc, argv, "")) != -1)
669 return ctlaction(res);
673 ctl_repeat(struct parse_result *res, int argc, char **argv)
677 while ((ch = getopt(argc, argv, "")) != -1)
685 if (!strcmp(argv[1], "on"))
687 else if (!strcmp(argv[1], "off"))
692 res->rep.repeat_one = -1;
693 res->rep.repeat_all = -1;
694 if (!strcmp(argv[0], "one"))
695 res->rep.repeat_one = b;
696 else if (!strcmp(argv[0], "all"))
697 res->rep.repeat_all = b;
701 return ctlaction(res);
705 ctl_monitor(struct parse_result *res, int argc, char **argv)
709 char *dup, *tmp, *tok;
711 while ((ch = getopt(argc, argv, "")) != -1)
722 events = "play,toggle,pause,stop,restart,flush,next,prev,"
723 "jump,repeat,add,load";
725 tmp = dup = xstrdup(events);
726 while ((tok = strsep(&tmp, ",")) != NULL) {
730 if (!strcmp(tok, "play"))
731 res->monitor[IMSG_CTL_PLAY] = 1;
732 else if (!strcmp(tok, "toggle"))
733 res->monitor[IMSG_CTL_TOGGLE_PLAY] = 1;
734 else if (!strcmp(tok, "pause"))
735 res->monitor[IMSG_CTL_PAUSE] = 1;
736 else if (!strcmp(tok, "stop"))
737 res->monitor[IMSG_CTL_STOP] = 1;
738 else if (!strcmp(tok, "restart")) /* compat */
739 res->monitor[IMSG_CTL_SEEK] = 1;
740 else if (!strcmp(tok, "flush"))
741 res->monitor[IMSG_CTL_FLUSH] = 1;
742 else if (!strcmp(tok, "next"))
743 res->monitor[IMSG_CTL_NEXT] = 1;
744 else if (!strcmp(tok, "prev"))
745 res->monitor[IMSG_CTL_PREV] = 1;
746 else if (!strcmp(tok, "jump"))
747 res->monitor[IMSG_CTL_JUMP] = 1;
748 else if (!strcmp(tok, "repeat"))
749 res->monitor[IMSG_CTL_REPEAT] = 1;
750 else if (!strcmp(tok, "add"))
751 res->monitor[IMSG_CTL_ADD] = 1;
752 else if (!strcmp(tok, "load"))
753 res->monitor[IMSG_CTL_COMMIT] = 1;
754 else if (!strcmp(tok, "seek"))
755 res->monitor[IMSG_CTL_SEEK] = 1;
757 fatalx("unknown event \"%s\"", tok);
761 return ctlaction(res);
765 ctl_seek(struct parse_result *res, int argc, char **argv)
769 int hours = 0, minutes = 0, seconds = 0;
773 /* skip the command name */
778 if (argc > 0 && !strcmp(*argv, "--")) {
787 if (*n == '-' || *n == '+')
788 res->seek.relative = 1;
794 seconds = strtol(n, &ep, 10);
796 (*ep != '\0' && *ep != ':' && *ep != '%') ||
797 (*ep == '%' && ep[1] != '\0'))
798 fatalx("invalid offset: %s", argv[0]);
799 if (*ep == '\0' || *ep == '%') {
800 res->seek.percent = *ep == '%';
806 seconds = strtol(n, &ep, 10);
807 if (n[0] == '\0' || (*ep != '\0' && *ep != ':'))
808 fatalx("invalid offset: %s", argv[0]);
815 seconds = strtol(n, &ep, 10);
816 if (n[0] == '\0' || *ep != '\0')
817 fatalx("invalid offset: %s", argv[0]);
820 res->seek.offset = sign * (hours * 3600 + minutes * 60 + seconds);
821 return ctlaction(res);
825 ctl_status(struct parse_result *res, int argc, char **argv)
829 while ((ch = getopt(argc, argv, "f:")) != -1) {
832 res->status_format = optarg;
844 return ctlaction(res);
848 ctl_get_lock(const char *lockfile)
852 if ((lockfd = open(lockfile, O_WRONLY|O_CREAT, 0600)) == -1) {
853 log_debug("open failed: %s", strerror(errno));
857 if (flock(lockfd, LOCK_EX|LOCK_NB) == -1) {
858 log_debug("flock failed: %s", strerror(errno));
861 while (flock(lockfd, LOCK_EX) == -1 && errno == EINTR)
866 log_debug("flock succeeded");
874 struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
875 struct sockaddr_un sa;
877 int fd, lockfd = -1, locked = 0, spawned = 0;
879 char *lockfile = NULL;
881 memset(&sa, 0, sizeof(sa));
882 sa.sun_family = AF_UNIX;
883 size = strlcpy(sa.sun_path, csock, sizeof(sa.sun_path));
884 if (size >= sizeof(sa.sun_path)) {
885 errno = ENAMETOOLONG;
890 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
893 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
894 log_debug("connection failed: %s", strerror(errno));
895 if (errno != ECONNREFUSED && errno != ENOENT)
897 if (attempt++ == 100)
902 xasprintf(&lockfile, "%s.lock", csock);
903 if ((lockfd = ctl_get_lock(lockfile)) < 0) {
904 log_debug("didn't get the lock (%d)", lockfd);
914 * Always retry at least once, even if we got
915 * the lock, because another client could have
916 * taken the lock, started the server and released
917 * the lock between our connect() and flock()
924 log_debug("spawning the daemon");
929 nanosleep(&ts, NULL);
933 if (locked && lockfd >= 0) {
950 ctl(int argc, char **argv)
952 struct parse_result res;
956 memset(&res, 0, sizeof(res));
957 if ((fmt = getenv("AMUSED_STATUS_FORMAT")) == NULL)
958 fmt = "status,time,repeat";
959 res.status_format = fmt;
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(&res, argc, argv));