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>
41 static struct imsgbuf *ibuf;
43 int ctl_noarg(struct parse_result *, int, char **);
44 int ctl_add(struct parse_result *, int, char **);
45 int ctl_show(struct parse_result *, int, char **);
46 int ctl_load(struct parse_result *, int, char **);
47 int ctl_jump(struct parse_result *, int, char **);
48 int ctl_repeat(struct parse_result *, int, char **);
50 struct ctl_command ctl_commands[] = {
51 { "play", PLAY, ctl_noarg, "" },
52 { "pause", PAUSE, ctl_noarg, "" },
53 { "toggle", TOGGLE, ctl_noarg, "" },
54 { "stop", STOP, ctl_noarg, "" },
55 { "restart", RESTART, ctl_noarg, "" },
56 { "add", ADD, ctl_add, "files...", 1 },
57 { "flush", FLUSH, ctl_noarg, "" },
58 { "show", SHOW, ctl_show, "[-p]" },
59 { "status", STATUS, ctl_noarg, "" },
60 { "next", NEXT, ctl_noarg, "" },
61 { "prev", PREV, ctl_noarg, "" },
62 { "load", LOAD, ctl_load, "[file]", 1 },
63 { "jump", JUMP, ctl_jump, "pattern" },
64 { "repeat", REPEAT, ctl_repeat, "one|all on|off" },
65 { "monitor", MONITOR, ctl_noarg, "" },
72 fprintf(stderr, "usage: %s [-dv] [-s socket]\n", getprogname());
77 ctl_usage(struct ctl_command *ctl)
79 fprintf(stderr, "usage: %s [-v] [-s socket] %s %s\n", getprogname(),
80 ctl->name, ctl->usage);
85 parse(int argc, char **argv)
87 struct ctl_command *ctl = NULL;
88 struct parse_result res;
92 memset(&res, 0, sizeof(res));
94 if ((argv0 = argv[0]) == NULL)
97 for (i = 0; ctl_commands[i].name != NULL; ++i) {
98 if (strncmp(ctl_commands[i].name, argv0, strlen(argv0))
102 "ambiguous argument: %s\n", argv0);
105 ctl = &ctl_commands[i];
110 fprintf(stderr, "unknown argument: %s\n", argv[0]);
114 res.action = ctl->action;
117 if (!ctl->has_pledge) {
118 /* pledge(2) default if command doesn't have its own */
119 if (pledge("stdio", NULL) == -1)
123 status = ctl->main(&res, argc, argv);
130 enqueue_tracks(char **files)
135 for (; *files != NULL; ++files) {
136 memset(&res, 0, sizeof(res));
137 if (realpath(*files, res) == NULL) {
138 log_warn("realpath %s", *files);
142 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
151 jump_req(const char *arg)
155 memset(path, 0, sizeof(path));
156 strlcpy(path, arg, sizeof(path));
157 imsg_compose(ibuf, IMSG_CTL_JUMP, 0, 0, -1, path, sizeof(path));
162 print_error_message(const char *prfx, struct imsg *imsg)
167 datalen = IMSG_DATA_SIZE(*imsg);
168 if ((msg = calloc(1, datalen)) == NULL)
169 fatal("calloc %zu", datalen);
170 memcpy(msg, imsg->data, datalen);
171 if (datalen == 0 || msg[datalen-1] != '\0')
172 fatalx("malformed error message");
174 log_warnx("%s: %s", prfx, msg);
179 show_add(struct imsg *imsg, int *ret, char ***files)
181 if (**files == NULL) {
182 log_warnx("received more replies than file sent");
187 if (imsg->hdr.type == IMSG_CTL_ERR)
188 print_error_message(**files, imsg);
189 else if (imsg->hdr.type == IMSG_CTL_ADD)
190 log_debug("enqueued %s", **files);
192 fatalx("got invalid message %d", imsg->hdr.type);
195 return (**files) == NULL;
199 show_complete(struct parse_result *res, struct imsg *imsg, int *ret)
201 struct player_status s;
204 if (imsg->hdr.type == IMSG_CTL_ERR) {
205 print_error_message("show failed", imsg);
210 datalen = IMSG_DATA_SIZE(*imsg);
214 if (datalen != sizeof(s))
215 fatalx("%s: data size mismatch", __func__);
216 memcpy(&s, imsg->data, sizeof(s));
217 if (s.path[sizeof(s.path)-1] != '\0')
218 fatalx("%s: data corrupted?", __func__);
221 printf("%c ", s.status == STATE_PLAYING ? '>' : ' ');
222 printf("%s\n", s.path);
227 show_status(struct imsg *imsg, int *ret)
229 struct player_status s;
232 if (imsg->hdr.type == IMSG_CTL_ERR) {
233 print_error_message("show failed", imsg);
238 if (imsg->hdr.type != IMSG_CTL_STATUS)
239 fatalx("%s: got wrong reply", __func__);
241 datalen = IMSG_DATA_SIZE(*imsg);
242 if (datalen != sizeof(s))
243 fatalx("%s: data size mismatch", __func__);
244 memcpy(&s, imsg->data, sizeof(s));
245 if (s.path[sizeof(s.path)-1] != '\0')
246 fatalx("%s: data corrupted?", __func__);
263 printf("%s\n", s.path);
264 printf("repeat one %s -- repeat all %s\n",
265 s.rp.repeat_one ? "on" : "off",
266 s.rp.repeat_all ? "on" : "off");
271 show_load(struct parse_result *res, struct imsg *imsg, int *ret)
277 size_t linesize = 0, i = 0;
278 ssize_t linelen, curr = -1;
280 if (imsg->hdr.type == IMSG_CTL_ERR) {
281 print_error_message("load failed", imsg);
286 if (imsg->hdr.type == IMSG_CTL_ADD)
289 if (imsg->hdr.type == IMSG_CTL_COMMIT)
292 if (imsg->hdr.type != IMSG_CTL_BEGIN)
293 fatalx("got unexpected message %d", imsg->hdr.type);
295 if (res->file == NULL)
297 else if ((f = fopen(res->file, "r")) == NULL) {
298 log_warn("can't open %s", res->file);
303 while ((linelen = getline(&line, &linesize, f)) != -1) {
306 line[linelen-1] = '\0';
308 if (file[0] == '>' && file[1] == ' ') {
312 if (file[0] == ' ' && file[1] == ' ')
315 memset(&path, 0, sizeof(path));
316 if (realpath(file, path) == NULL) {
317 log_warn("realpath %s", file);
322 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
336 imsg_compose(ibuf, IMSG_CTL_COMMIT, 0, 0, -1,
337 &curr, sizeof(curr));
343 show_monitor(struct imsg *imsg, int *ret)
347 if (imsg->hdr.type != IMSG_CTL_MONITOR) {
348 log_warnx("wrong message type received: %d",
354 if (IMSG_DATA_SIZE(*imsg) != sizeof(type)) {
355 log_warnx("size mismatch");
360 memcpy(&type, imsg->data, sizeof(type));
365 case IMSG_CTL_TOGGLE_PLAY:
374 case IMSG_CTL_RESTART:
389 case IMSG_CTL_REPEAT:
395 case IMSG_CTL_COMMIT:
408 ctlaction(struct parse_result *res)
412 int ret = 0, done = 1;
415 switch (res->action) {
417 imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
419 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
425 imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
428 imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
430 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
436 imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
439 imsg_compose(ibuf, IMSG_CTL_RESTART, 0, 0, -1, NULL, 0);
441 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
449 ret = enqueue_tracks(res->files);
452 imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
456 imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
460 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
463 imsg_compose(ibuf, IMSG_CTL_NEXT, 0, 0, -1, NULL, 0);
465 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
471 imsg_compose(ibuf, IMSG_CTL_PREV, 0, 0, -1, NULL, 0);
473 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
480 imsg_compose(ibuf, IMSG_CTL_BEGIN, 0, 0, -1, NULL, 0);
484 ret = jump_req(res->file);
487 imsg_compose(ibuf, IMSG_CTL_REPEAT, 0, 0, -1,
488 &res->rep, sizeof(res->rep));
492 imsg_compose(ibuf, IMSG_CTL_MONITOR, 0, 0, -1,
496 /* action not expected */
497 fatalx("invalid action %u", res->action);
507 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
508 fatalx("imsg_read error");
510 fatalx("pipe closed");
513 if ((n = imsg_get(ibuf, &imsg)) == -1)
514 fatalx("imsg_get error");
518 switch (res->action) {
520 done = show_add(&imsg, &ret, &files);
523 done = show_complete(res, &imsg, &ret);
532 done = show_status(&imsg, &ret);
535 done = show_load(res, &imsg, &ret);
538 done = show_monitor(&imsg, &ret);
554 ctl_noarg(struct parse_result *res, int argc, char **argv)
558 return ctlaction(res);
562 ctl_add(struct parse_result *res, int argc, char **argv)
568 if (pledge("stdio rpath", NULL) == -1)
571 return ctlaction(res);
575 ctl_show(struct parse_result *res, int argc, char **argv)
579 while ((ch = getopt(argc, argv, "p")) != -1) {
589 return ctlaction(res);
593 ctl_load(struct parse_result *res, int argc, char **argv)
602 if (pledge("stdio rpath", NULL) == -1)
605 return ctlaction(res);
609 ctl_jump(struct parse_result *res, int argc, char **argv)
613 while ((ch = getopt(argc, argv, "")) != -1)
622 return ctlaction(res);
626 ctl_repeat(struct parse_result *res, int argc, char **argv)
630 while ((ch = getopt(argc, argv, "")) != -1)
638 if (!strcmp(argv[1], "on"))
640 else if (!strcmp(argv[1], "off"))
645 res->rep.repeat_one = -1;
646 res->rep.repeat_all = -1;
647 if (!strcmp(argv[0], "one"))
648 res->rep.repeat_one = b;
649 else if (!strcmp(argv[0], "all"))
650 res->rep.repeat_all = b;
654 return ctlaction(res);
658 ctl_get_lock(const char *lockfile)
662 if ((lockfd = open(lockfile, O_WRONLY|O_CREAT, 0600)) == -1) {
663 log_debug("open failed: %s", strerror(errno));
667 if (flock(lockfd, LOCK_EX|LOCK_NB) == -1) {
668 log_debug("flock failed: %s", strerror(errno));
671 while (flock(lockfd, LOCK_EX) == -1 && errno == EINTR)
676 log_debug("flock succeeded");
684 struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
685 struct sockaddr_un sa;
687 int fd, lockfd = -1, locked = 0, spawned = 0;
688 char *lockfile = NULL;
690 memset(&sa, 0, sizeof(sa));
691 sa.sun_family = AF_UNIX;
692 size = strlcpy(sa.sun_path, csock, sizeof(sa.sun_path));
693 if (size >= sizeof(sa.sun_path)) {
694 errno = ENAMETOOLONG;
699 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
702 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
703 log_debug("connection failed: %s", strerror(errno));
704 if (errno != ECONNREFUSED && errno != ENOENT)
709 xasprintf(&lockfile, "%s.lock", csock);
710 if ((lockfd = ctl_get_lock(lockfile)) < 0) {
711 log_debug("didn't get the lock (%d)", lockfd);
721 * Always retry at least once, even if we got
722 * the lock, because another client could have
723 * taken the lock, started the server and released
724 * the lock between our connect() and flock()
731 log_debug("spawning the daemon");
736 nanosleep(&ts, NULL);
740 if (locked && lockfd >= 0) {
757 ctl(int argc, char **argv)
761 log_init(1, LOG_DAEMON);
762 log_setverbose(verbose);
764 if ((ctl_sock = ctl_connect()) == -1)
765 fatal("can't connect");
768 fatalx("failed to connect to the daemon");
770 ibuf = xmalloc(sizeof(*ibuf));
771 imsg_init(ibuf, ctl_sock);
776 exit(parse(argc, argv));