2 * Copyright (c) 2022 Omar Polo <op@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/types.h>
18 #include <sys/socket.h>
19 #include <sys/queue.h>
40 static struct imsgbuf *ibuf;
42 int ctl_noarg(struct parse_result *, int, char **);
43 int ctl_add(struct parse_result *, int, char **);
44 int ctl_show(struct parse_result *, int, char **);
45 int ctl_load(struct parse_result *, int, char **);
46 int ctl_jump(struct parse_result *, int, char **);
47 int ctl_repeat(struct parse_result *, int, char **);
49 struct ctl_command ctl_commands[] = {
50 { "play", PLAY, ctl_noarg, "" },
51 { "pause", PAUSE, ctl_noarg, "" },
52 { "toggle", TOGGLE, ctl_noarg, "" },
53 { "stop", STOP, ctl_noarg, "" },
54 { "restart", RESTART, ctl_noarg, "" },
55 { "add", ADD, ctl_add, "files...", 1 },
56 { "flush", FLUSH, ctl_noarg, "" },
57 { "show", SHOW, ctl_show, "[-p]" },
58 { "status", STATUS, ctl_noarg, "" },
59 { "next", NEXT, ctl_noarg, "" },
60 { "prev", PREV, ctl_noarg, "" },
61 { "load", LOAD, ctl_load, "[file]", 1 },
62 { "jump", JUMP, ctl_jump, "pattern" },
63 { "repeat", REPEAT, ctl_repeat, "one|all on|off" },
64 { "monitor", MONITOR, ctl_noarg, "" },
71 fprintf(stderr, "usage: %s [-dv] [-s socket]\n", getprogname());
76 ctl_usage(struct ctl_command *ctl)
78 fprintf(stderr, "usage: %s [-v] [-s socket] %s %s\n", getprogname(),
79 ctl->name, ctl->usage);
84 parse(int argc, char **argv)
86 struct ctl_command *ctl = NULL;
87 struct parse_result res;
91 memset(&res, 0, sizeof(res));
93 if ((argv0 = argv[0]) == NULL)
96 for (i = 0; ctl_commands[i].name != NULL; ++i) {
97 if (strncmp(ctl_commands[i].name, argv0, strlen(argv0))
101 "ambiguous argument: %s\n", argv0);
104 ctl = &ctl_commands[i];
109 fprintf(stderr, "unknown argument: %s\n", argv[0]);
113 res.action = ctl->action;
116 if (!ctl->has_pledge) {
117 /* pledge(2) default if command doesn't have its own */
118 if (pledge("stdio", NULL) == -1)
122 status = ctl->main(&res, argc, argv);
129 enqueue_tracks(char **files)
134 for (; *files != NULL; ++files) {
135 memset(&res, 0, sizeof(res));
136 if (realpath(*files, res) == NULL) {
137 log_warn("realpath %s", *files);
141 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
150 jump_req(const char *arg)
154 memset(path, 0, sizeof(path));
155 strlcpy(path, arg, sizeof(path));
156 imsg_compose(ibuf, IMSG_CTL_JUMP, 0, 0, -1, path, sizeof(path));
161 print_error_message(const char *prfx, struct imsg *imsg)
166 datalen = IMSG_DATA_SIZE(*imsg);
167 if ((msg = calloc(1, datalen)) == NULL)
168 fatal("calloc %zu", datalen);
169 memcpy(msg, imsg->data, datalen);
170 if (datalen == 0 || msg[datalen-1] != '\0')
171 fatalx("malformed error message");
173 log_warnx("%s: %s", prfx, msg);
178 show_add(struct imsg *imsg, int *ret, char ***files)
180 if (**files == NULL) {
181 log_warnx("received more replies than file sent");
186 if (imsg->hdr.type == IMSG_CTL_ERR)
187 print_error_message(**files, imsg);
188 else if (imsg->hdr.type == IMSG_CTL_ADD)
189 log_debug("enqueued %s", **files);
191 fatalx("got invalid message %d", imsg->hdr.type);
194 return (**files) == NULL;
198 show_complete(struct parse_result *res, struct imsg *imsg, int *ret)
200 struct player_status s;
203 if (imsg->hdr.type == IMSG_CTL_ERR) {
204 print_error_message("show failed", imsg);
209 datalen = IMSG_DATA_SIZE(*imsg);
213 if (datalen != sizeof(s))
214 fatalx("%s: data size mismatch", __func__);
215 memcpy(&s, imsg->data, sizeof(s));
216 if (s.path[sizeof(s.path)-1] != '\0')
217 fatalx("%s: data corrupted?", __func__);
220 printf("%c ", s.status == STATE_PLAYING ? '>' : ' ');
221 printf("%s\n", s.path);
226 show_status(struct imsg *imsg, int *ret)
228 struct player_status s;
231 if (imsg->hdr.type == IMSG_CTL_ERR) {
232 print_error_message("show failed", imsg);
237 if (imsg->hdr.type != IMSG_CTL_STATUS)
238 fatalx("%s: got wrong reply", __func__);
240 datalen = IMSG_DATA_SIZE(*imsg);
241 if (datalen != sizeof(s))
242 fatalx("%s: data size mismatch", __func__);
243 memcpy(&s, imsg->data, sizeof(s));
244 if (s.path[sizeof(s.path)-1] != '\0')
245 fatalx("%s: data corrupted?", __func__);
262 printf("%s\n", s.path);
263 printf("repeat one %s\nrepeat all %s\n",
264 s.rp.repeat_one ? "on" : "off",
265 s.rp.repeat_all ? "on" : "off");
270 show_load(struct parse_result *res, struct imsg *imsg, int *ret)
275 char path[PATH_MAX], cwd[PATH_MAX];
276 size_t linesize = 0, i = 0, n;
277 ssize_t linelen, curr = -1;
279 if (imsg->hdr.type == IMSG_CTL_ERR) {
280 print_error_message("load failed", imsg);
285 if (imsg->hdr.type == IMSG_CTL_ADD)
288 if (imsg->hdr.type == IMSG_CTL_COMMIT)
291 if (imsg->hdr.type != IMSG_CTL_BEGIN)
292 fatalx("got unexpected message %d", imsg->hdr.type);
294 if (res->file == NULL)
296 else if ((f = fopen(res->file, "r")) == NULL) {
297 log_warn("can't open %s", res->file);
302 if (getcwd(cwd, sizeof(cwd)) == NULL)
305 while ((linelen = getline(&line, &linesize, f)) != -1) {
308 line[linelen-1] = '\0';
310 if (file[0] == '>' && file[1] == ' ') {
314 if (file[0] == ' ' && file[1] == ' ')
316 if (file[0] == '.' && file[1] == '/')
320 n = strlcpy(path, file, sizeof(path));
322 n = snprintf(path, sizeof(path), "%s/%s", cwd, file);
324 if (n >= sizeof(path)) {
325 log_warnx("path too long: %s", file);
330 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
344 imsg_compose(ibuf, IMSG_CTL_COMMIT, 0, 0, -1,
345 &curr, sizeof(curr));
351 show_monitor(struct imsg *imsg, int *ret)
355 if (imsg->hdr.type != IMSG_CTL_MONITOR) {
356 log_warnx("wrong message type received: %d",
362 if (IMSG_DATA_SIZE(*imsg) != sizeof(type)) {
363 log_warnx("size mismatch");
368 memcpy(&type, imsg->data, sizeof(type));
373 case IMSG_CTL_TOGGLE_PLAY:
382 case IMSG_CTL_RESTART:
397 case IMSG_CTL_REPEAT:
403 case IMSG_CTL_COMMIT:
416 ctlaction(struct parse_result *res)
420 int ret = 0, done = 1;
423 switch (res->action) {
425 imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
427 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
433 imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
436 imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
438 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
444 imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
447 imsg_compose(ibuf, IMSG_CTL_RESTART, 0, 0, -1, NULL, 0);
449 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
457 ret = enqueue_tracks(res->files);
460 imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
464 imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
468 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
471 imsg_compose(ibuf, IMSG_CTL_NEXT, 0, 0, -1, NULL, 0);
473 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
479 imsg_compose(ibuf, IMSG_CTL_PREV, 0, 0, -1, NULL, 0);
481 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
488 imsg_compose(ibuf, IMSG_CTL_BEGIN, 0, 0, -1, NULL, 0);
492 ret = jump_req(res->file);
495 imsg_compose(ibuf, IMSG_CTL_REPEAT, 0, 0, -1,
496 &res->rep, sizeof(res->rep));
500 imsg_compose(ibuf, IMSG_CTL_MONITOR, 0, 0, -1,
504 /* action not expected */
505 fatalx("invalid action %u", res->action);
515 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
516 fatalx("imsg_read error");
518 fatalx("pipe closed");
521 if ((n = imsg_get(ibuf, &imsg)) == -1)
522 fatalx("imsg_get error");
526 switch (res->action) {
528 done = show_add(&imsg, &ret, &files);
531 done = show_complete(res, &imsg, &ret);
540 done = show_status(&imsg, &ret);
543 done = show_load(res, &imsg, &ret);
546 done = show_monitor(&imsg, &ret);
562 ctl_noarg(struct parse_result *res, int argc, char **argv)
566 while ((ch = getopt(argc, argv, "")) != -1)
574 return ctlaction(res);
578 ctl_add(struct parse_result *res, int argc, char **argv)
582 while ((ch = getopt(argc, argv, "")) != -1)
591 if (pledge("stdio rpath", NULL) == -1)
594 return ctlaction(res);
598 ctl_show(struct parse_result *res, int argc, char **argv)
602 while ((ch = getopt(argc, argv, "p")) != -1) {
612 return ctlaction(res);
616 ctl_load(struct parse_result *res, int argc, char **argv)
620 while ((ch = getopt(argc, argv, "")) != -1)
632 if (pledge("stdio rpath", NULL) == -1)
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_get_lock(const char *lockfile)
692 if ((lockfd = open(lockfile, O_WRONLY|O_CREAT, 0600)) == -1) {
693 log_debug("open failed: %s", strerror(errno));
697 if (flock(lockfd, LOCK_EX|LOCK_NB) == -1) {
698 log_debug("flock failed: %s", strerror(errno));
701 while (flock(lockfd, LOCK_EX) == -1 && errno == EINTR)
706 log_debug("flock succeeded");
714 struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
715 struct sockaddr_un sa;
717 int fd, lockfd = -1, locked = 0, spawned = 0;
718 char *lockfile = NULL;
720 memset(&sa, 0, sizeof(sa));
721 sa.sun_family = AF_UNIX;
722 size = strlcpy(sa.sun_path, csock, sizeof(sa.sun_path));
723 if (size >= sizeof(sa.sun_path)) {
724 errno = ENAMETOOLONG;
729 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
732 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
733 log_debug("connection failed: %s", strerror(errno));
734 if (errno != ECONNREFUSED && errno != ENOENT)
739 xasprintf(&lockfile, "%s.lock", csock);
740 if ((lockfd = ctl_get_lock(lockfile)) < 0) {
741 log_debug("didn't get the lock (%d)", lockfd);
751 * Always retry at least once, even if we got
752 * the lock, because another client could have
753 * taken the lock, started the server and released
754 * the lock between our connect() and flock()
761 log_debug("spawning the daemon");
766 nanosleep(&ts, NULL);
770 if (locked && lockfd >= 0) {
787 ctl(int argc, char **argv)
791 log_init(1, LOG_DAEMON);
792 log_setverbose(verbose);
794 if ((ctl_sock = ctl_connect()) == -1)
795 fatal("can't connect");
798 fatalx("failed to connect to the daemon");
800 ibuf = xmalloc(sizeof(*ibuf));
801 imsg_init(ibuf, ctl_sock);
806 exit(parse(argc, argv));