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)
276 size_t linesize = 0, i = 0;
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 while ((linelen = getline(&line, &linesize, f)) != -1) {
305 line[linelen-1] = '\0';
307 if (file[0] == '>' && file[1] == ' ') {
311 if (file[0] == ' ' && file[1] == ' ')
314 memset(&path, 0, sizeof(path));
315 if (realpath(file, path) == NULL) {
316 log_warn("realpath %s", file);
321 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
335 imsg_compose(ibuf, IMSG_CTL_COMMIT, 0, 0, -1,
336 &curr, sizeof(curr));
342 show_monitor(struct imsg *imsg, int *ret)
346 if (imsg->hdr.type != IMSG_CTL_MONITOR) {
347 log_warnx("wrong message type received: %d",
353 if (IMSG_DATA_SIZE(*imsg) != sizeof(type)) {
354 log_warnx("size mismatch");
359 memcpy(&type, imsg->data, sizeof(type));
364 case IMSG_CTL_TOGGLE_PLAY:
373 case IMSG_CTL_RESTART:
388 case IMSG_CTL_REPEAT:
394 case IMSG_CTL_COMMIT:
407 ctlaction(struct parse_result *res)
411 int ret = 0, done = 1;
414 switch (res->action) {
416 imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
418 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
424 imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
427 imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
429 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
435 imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
438 imsg_compose(ibuf, IMSG_CTL_RESTART, 0, 0, -1, NULL, 0);
440 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
448 ret = enqueue_tracks(res->files);
451 imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
455 imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
459 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
462 imsg_compose(ibuf, IMSG_CTL_NEXT, 0, 0, -1, NULL, 0);
464 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
470 imsg_compose(ibuf, IMSG_CTL_PREV, 0, 0, -1, NULL, 0);
472 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
479 imsg_compose(ibuf, IMSG_CTL_BEGIN, 0, 0, -1, NULL, 0);
483 ret = jump_req(res->file);
486 imsg_compose(ibuf, IMSG_CTL_REPEAT, 0, 0, -1,
487 &res->rep, sizeof(res->rep));
491 imsg_compose(ibuf, IMSG_CTL_MONITOR, 0, 0, -1,
495 /* action not expected */
496 fatalx("invalid action %u", res->action);
506 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
507 fatalx("imsg_read error");
509 fatalx("pipe closed");
512 if ((n = imsg_get(ibuf, &imsg)) == -1)
513 fatalx("imsg_get error");
517 switch (res->action) {
519 done = show_add(&imsg, &ret, &files);
522 done = show_complete(res, &imsg, &ret);
531 done = show_status(&imsg, &ret);
534 done = show_load(res, &imsg, &ret);
537 done = show_monitor(&imsg, &ret);
553 ctl_noarg(struct parse_result *res, int argc, char **argv)
557 while ((ch = getopt(argc, argv, "")) != -1)
565 return ctlaction(res);
569 ctl_add(struct parse_result *res, int argc, char **argv)
573 while ((ch = getopt(argc, argv, "")) != -1)
582 if (pledge("stdio rpath", NULL) == -1)
585 return ctlaction(res);
589 ctl_show(struct parse_result *res, int argc, char **argv)
593 while ((ch = getopt(argc, argv, "p")) != -1) {
603 return ctlaction(res);
607 ctl_load(struct parse_result *res, int argc, char **argv)
611 while ((ch = getopt(argc, argv, "")) != -1)
623 if (pledge("stdio rpath", NULL) == -1)
626 return ctlaction(res);
630 ctl_jump(struct parse_result *res, int argc, char **argv)
634 while ((ch = getopt(argc, argv, "")) != -1)
643 return ctlaction(res);
647 ctl_repeat(struct parse_result *res, int argc, char **argv)
651 while ((ch = getopt(argc, argv, "")) != -1)
659 if (!strcmp(argv[1], "on"))
661 else if (!strcmp(argv[1], "off"))
666 res->rep.repeat_one = -1;
667 res->rep.repeat_all = -1;
668 if (!strcmp(argv[0], "one"))
669 res->rep.repeat_one = b;
670 else if (!strcmp(argv[0], "all"))
671 res->rep.repeat_all = b;
675 return ctlaction(res);
679 ctl_get_lock(const char *lockfile)
683 if ((lockfd = open(lockfile, O_WRONLY|O_CREAT, 0600)) == -1) {
684 log_debug("open failed: %s", strerror(errno));
688 if (flock(lockfd, LOCK_EX|LOCK_NB) == -1) {
689 log_debug("flock failed: %s", strerror(errno));
692 while (flock(lockfd, LOCK_EX) == -1 && errno == EINTR)
697 log_debug("flock succeeded");
705 struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
706 struct sockaddr_un sa;
708 int fd, lockfd = -1, locked = 0, spawned = 0;
709 char *lockfile = NULL;
711 memset(&sa, 0, sizeof(sa));
712 sa.sun_family = AF_UNIX;
713 size = strlcpy(sa.sun_path, csock, sizeof(sa.sun_path));
714 if (size >= sizeof(sa.sun_path)) {
715 errno = ENAMETOOLONG;
720 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
723 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
724 log_debug("connection failed: %s", strerror(errno));
725 if (errno != ECONNREFUSED && errno != ENOENT)
730 xasprintf(&lockfile, "%s.lock", csock);
731 if ((lockfd = ctl_get_lock(lockfile)) < 0) {
732 log_debug("didn't get the lock (%d)", lockfd);
742 * Always retry at least once, even if we got
743 * the lock, because another client could have
744 * taken the lock, started the server and released
745 * the lock between our connect() and flock()
752 log_debug("spawning the daemon");
757 nanosleep(&ts, NULL);
761 if (locked && lockfd >= 0) {
778 ctl(int argc, char **argv)
782 log_init(1, LOG_DAEMON);
783 log_setverbose(verbose);
785 if ((ctl_sock = ctl_connect()) == -1)
786 fatal("can't connect");
789 fatalx("failed to connect to the daemon");
791 ibuf = xmalloc(sizeof(*ibuf));
792 imsg_init(ibuf, ctl_sock);
797 exit(parse(argc, argv));