Blob


1 /*
2 * Copyright (c) 2022 Omar Polo <op@openbsd.org>
3 * Copyright (c) 2015 Theo de Raadt <deraadt@openbsd.org>
4 *
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.
8 *
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.
16 */
18 #include <sys/types.h>
19 #include <sys/socket.h>
20 #include <sys/queue.h>
21 #include <sys/uio.h>
22 #include <sys/un.h>
24 #include <errno.h>
25 #include <event.h>
26 #include <fcntl.h>
27 #include <limits.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <stdint.h>
31 #include <string.h>
32 #include <syslog.h>
33 #include <unistd.h>
34 #include <imsg.h>
36 #include "amused.h"
37 #include "log.h"
38 #include "playlist.h"
39 #include "xmalloc.h"
41 static struct imsgbuf *ibuf;
42 char cwd[PATH_MAX];
44 int ctl_noarg(struct parse_result *, int, char **);
45 int ctl_add(struct parse_result *, int, char **);
46 int ctl_show(struct parse_result *, int, char **);
47 int ctl_load(struct parse_result *, int, char **);
48 int ctl_jump(struct parse_result *, int, char **);
49 int ctl_repeat(struct parse_result *, int, char **);
50 int ctl_monitor(struct parse_result *, int, char **);
52 struct ctl_command ctl_commands[] = {
53 { "play", PLAY, ctl_noarg, "" },
54 { "pause", PAUSE, ctl_noarg, "" },
55 { "toggle", TOGGLE, ctl_noarg, "" },
56 { "stop", STOP, ctl_noarg, "" },
57 { "restart", RESTART, ctl_noarg, "" },
58 { "add", ADD, ctl_add, "files..." },
59 { "flush", FLUSH, ctl_noarg, "" },
60 { "show", SHOW, ctl_show, "[-p]" },
61 { "status", STATUS, ctl_noarg, "" },
62 { "next", NEXT, ctl_noarg, "" },
63 { "prev", PREV, ctl_noarg, "" },
64 { "load", LOAD, ctl_load, "[file]", 1 },
65 { "jump", JUMP, ctl_jump, "pattern" },
66 { "repeat", REPEAT, ctl_repeat, "one|all on|off" },
67 { "monitor", MONITOR, ctl_monitor, "[events]" },
68 { NULL },
69 };
71 __dead void
72 usage(void)
73 {
74 fprintf(stderr, "usage: %s [-dv] [-s socket]\n", getprogname());
75 exit(1);
76 }
78 static __dead void
79 ctl_usage(struct ctl_command *ctl)
80 {
81 fprintf(stderr, "usage: %s [-v] [-s socket] %s %s\n", getprogname(),
82 ctl->name, ctl->usage);
83 exit(1);
84 }
86 /* based on canonpath from kern_pledge.c */
87 static int
88 canonpath(const char *input, char *buf, size_t bufsize)
89 {
90 const char *p;
91 char *q, path[PATH_MAX];
93 if (input[0] != '/') {
94 if (snprintf(path, sizeof(path), "%s/%s", cwd, input)
95 >= sizeof(path)) {
96 errno = ENAMETOOLONG;
97 return -1;
98 }
99 input = path;
102 p = input;
103 q = buf;
104 while (*p && (q - buf < bufsize)) {
105 if (p[0] == '/' && (p[1] == '/' || p[1] == '\0')) {
106 p += 1;
108 } else if (p[0] == '/' && p[1] == '.' &&
109 (p[2] == '/' || p[2] == '\0')) {
110 p += 2;
112 } else if (p[0] == '/' && p[1] == '.' && p[2] == '.' &&
113 (p[3] == '/' || p[3] == '\0')) {
114 p += 3;
115 if (q != buf) /* "/../" at start of buf */
116 while (*--q != '/')
117 continue;
119 } else {
120 *q++ = *p++;
123 if ((*p == '\0') && (q - buf < bufsize)) {
124 *q = 0;
125 return 0;
126 } else {
127 errno = ENAMETOOLONG;
128 return -1;
132 static int
133 parse(int argc, char **argv)
135 struct ctl_command *ctl = NULL;
136 struct parse_result res;
137 const char *argv0;
138 int i, status;
140 memset(&res, 0, sizeof(res));
142 if ((argv0 = argv[0]) == NULL)
143 argv0 = "status";
145 for (i = 0; ctl_commands[i].name != NULL; ++i) {
146 if (strncmp(ctl_commands[i].name, argv0, strlen(argv0))
147 == 0) {
148 if (ctl != NULL) {
149 fprintf(stderr,
150 "ambiguous argument: %s\n", argv0);
151 usage();
153 ctl = &ctl_commands[i];
157 if (ctl == NULL) {
158 fprintf(stderr, "unknown argument: %s\n", argv[0]);
159 usage();
162 res.action = ctl->action;
163 res.ctl = ctl;
165 if (!ctl->has_pledge) {
166 /* pledge(2) default if command doesn't have its own */
167 if (pledge("stdio", NULL) == -1)
168 fatal("pledge");
171 status = ctl->main(&res, argc, argv);
172 close(ibuf->fd);
173 free(ibuf);
174 return status;
177 static int
178 load_files(struct parse_result *res, int *ret)
180 FILE *f;
181 const char *file;
182 char *line = NULL;
183 char path[PATH_MAX];
184 size_t linesize = 0, i = 0;
185 ssize_t linelen, curr = -1;
187 if (res->file == NULL)
188 f = stdin;
189 else if ((f = fopen(res->file, "r")) == NULL) {
190 log_warn("can't open %s", res->file);
191 *ret = 1;
192 return 1;
195 while ((linelen = getline(&line, &linesize, f)) != -1) {
196 if (linelen == 0)
197 continue;
198 line[linelen-1] = '\0';
199 file = line;
200 if (!strncmp(file, "> ", 2)) {
201 file += 2;
202 curr = i;
203 } else if (!strncmp(file, " ", 2))
204 file += 2;
206 memset(path, 0, sizeof(path));
207 if (canonpath(file, path, sizeof(path)) == -1) {
208 log_warnx("canonpath %s", file);
209 continue;
212 i++;
213 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
214 path, sizeof(path));
217 free(line);
218 if (ferror(f))
219 fatal("getline");
220 fclose(f);
222 if (i == 0) {
223 *ret = 1;
224 return 1;
227 imsg_compose(ibuf, IMSG_CTL_COMMIT, 0, 0, -1,
228 &curr, sizeof(curr));
229 imsg_flush(ibuf);
230 return 0;
233 static const char *
234 imsg_strerror(struct imsg *imsg)
236 size_t datalen;
237 const char *msg;
239 datalen = IMSG_DATA_SIZE(*imsg);
240 msg = imsg->data;
241 if (datalen == 0 || msg[datalen-1] != '\0')
242 fatalx("malformed error message");
244 return msg;
247 static const char *
248 imsg_name(int type)
250 switch (type) {
251 case IMSG_CTL_PLAY:
252 return "play";
253 case IMSG_CTL_TOGGLE_PLAY:
254 return "toggle";
255 case IMSG_CTL_PAUSE:
256 return "pause";
257 case IMSG_CTL_STOP:
258 return "stop";
259 case IMSG_CTL_RESTART:
260 return "restart";
261 case IMSG_CTL_FLUSH:
262 return "flush";
263 case IMSG_CTL_NEXT:
264 return "next";
265 case IMSG_CTL_PREV:
266 return "prev";
267 case IMSG_CTL_JUMP:
268 return "jump";
269 case IMSG_CTL_REPEAT:
270 return "repeat";
271 case IMSG_CTL_ADD:
272 return "add";
273 case IMSG_CTL_COMMIT:
274 return "load";
275 default:
276 return "unknown";
280 static int
281 ctlaction(struct parse_result *res)
283 char path[PATH_MAX];
284 struct imsg imsg;
285 struct player_status ps;
286 size_t datalen;
287 ssize_t n;
288 int i, type, ret = 0, done = 1;
290 switch (res->action) {
291 case PLAY:
292 imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
293 if (verbose) {
294 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
295 NULL, 0);
296 done = 0;
298 break;
299 case PAUSE:
300 imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
301 break;
302 case TOGGLE:
303 imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
304 if (verbose) {
305 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
306 NULL, 0);
307 done = 0;
309 break;
310 case STOP:
311 imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
312 break;
313 case RESTART:
314 imsg_compose(ibuf, IMSG_CTL_RESTART, 0, 0, -1, NULL, 0);
315 if (verbose) {
316 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
317 NULL, 0);
318 done = 0;
320 break;
321 case ADD:
322 done = 0;
323 for (i = 0; res->files[i] != NULL; ++i) {
324 memset(path, 0, sizeof(path));
325 if (canonpath(res->files[i], path, sizeof(path))
326 == -1) {
327 log_warn("canonpath %s", res->files[i]);
328 continue;
331 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
332 path, sizeof(path));
334 ret = i == 0;
335 break;
336 case FLUSH:
337 imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
338 break;
339 case SHOW:
340 done = 0;
341 imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
342 break;
343 case STATUS:
344 done = 0;
345 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
346 break;
347 case NEXT:
348 imsg_compose(ibuf, IMSG_CTL_NEXT, 0, 0, -1, NULL, 0);
349 if (verbose) {
350 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
351 NULL, 0);
352 done = 0;
354 break;
355 case PREV:
356 imsg_compose(ibuf, IMSG_CTL_PREV, 0, 0, -1, NULL, 0);
357 if (verbose) {
358 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
359 NULL, 0);
360 done = 0;
362 break;
363 case LOAD:
364 done = 0;
365 imsg_compose(ibuf, IMSG_CTL_BEGIN, 0, 0, -1, NULL, 0);
366 break;
367 case JUMP:
368 done = 0;
369 memset(path, 0, sizeof(path));
370 strlcpy(path, res->file, sizeof(path));
371 imsg_compose(ibuf, IMSG_CTL_JUMP, 0, 0, -1,
372 path, sizeof(path));
373 break;
374 case REPEAT:
375 imsg_compose(ibuf, IMSG_CTL_REPEAT, 0, 0, -1,
376 &res->rep, sizeof(res->rep));
377 break;
378 case MONITOR:
379 done = 0;
380 imsg_compose(ibuf, IMSG_CTL_MONITOR, 0, 0, -1,
381 NULL, 0);
382 break;
383 case NONE:
384 /* action not expected */
385 fatalx("invalid action %u", res->action);
386 break;
389 if (ret != 0)
390 goto end;
392 imsg_flush(ibuf);
394 i = 0;
395 while (!done) {
396 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
397 fatalx("imsg_read error");
398 if (n == 0)
399 fatalx("pipe closed");
401 while (!done) {
402 if ((n = imsg_get(ibuf, &imsg)) == -1)
403 fatalx("imsg_get error");
404 if (n == 0)
405 break;
407 if (imsg.hdr.type == IMSG_CTL_ERR) {
408 log_warnx("%s: %s", res->ctl->name,
409 imsg_strerror(&imsg));
410 ret = 1;
411 done = 1;
412 break;
415 datalen = IMSG_DATA_SIZE(imsg);
417 switch (res->action) {
418 case ADD:
419 if (res->files[i] == NULL)
420 fatalx("received more replies than "
421 "files enqueued.");
423 if (imsg.hdr.type == IMSG_CTL_ADD)
424 log_debug("enqueued %s", res->files[i]);
425 else
426 fatalx("invalid message %d",
427 imsg.hdr.type);
428 i++;
429 done = res->files[i] == NULL;
430 break;
431 case SHOW:
432 if (datalen == 0) {
433 done = 1;
434 break;
436 if (datalen != sizeof(ps))
437 fatalx("data size mismatch");
438 memcpy(&ps, imsg.data, sizeof(ps));
439 if (ps.path[sizeof(ps.path) - 1] != '\0')
440 fatalx("received corrupted data");
441 if (res->pretty) {
442 char c = ' ';
443 if (ps.status == STATE_PLAYING)
444 c = '>';
445 printf("%c ", c);
447 puts(ps.path);
448 break;
449 case PLAY:
450 case TOGGLE:
451 case RESTART:
452 case STATUS:
453 case NEXT:
454 case PREV:
455 case JUMP:
456 if (imsg.hdr.type != IMSG_CTL_STATUS)
457 fatalx("invalid message %d",
458 imsg.hdr.type);
460 if (datalen != sizeof(ps))
461 fatalx("data size mismatch");
462 memcpy(&ps, imsg.data, sizeof(ps));
463 if (ps.path[sizeof(ps.path) - 1] != '\0')
464 fatalx("received corrupted data");
466 if (ps.status == STATE_STOPPED)
467 printf("stopped ");
468 else if (ps.status == STATE_PLAYING)
469 printf("playing ");
470 else if (ps.status == STATE_PAUSED)
471 printf("paused ");
472 else
473 printf("unknown ");
475 puts(ps.path);
476 printf("repat one %s\nrepeat all %s\n",
477 ps.rp.repeat_one ? "on" : "off",
478 ps.rp.repeat_all ? "on" : "off");
480 done = 1;
481 break;
482 case LOAD:
483 if (imsg.hdr.type == IMSG_CTL_ADD)
484 break;
485 if (imsg.hdr.type == IMSG_CTL_COMMIT) {
486 done = 1;
487 break;
490 if (imsg.hdr.type != IMSG_CTL_BEGIN)
491 fatalx("invalid message %d",
492 imsg.hdr.type);
494 load_files(res, &ret);
495 break;
496 case MONITOR:
497 if (imsg.hdr.type != IMSG_CTL_MONITOR)
498 fatalx("invalid message %d",
499 imsg.hdr.type);
501 if (datalen != sizeof(type))
502 fatalx("data size mismatch");
504 memcpy(&type, imsg.data, sizeof(type));
505 if (type < 0 || type > IMSG__LAST)
506 fatalx("received corrupted data");
508 if (!res->monitor[type])
509 break;
511 puts(imsg_name(type));
512 fflush(stdout);
513 break;
514 default:
515 done = 1;
516 break;
519 imsg_free(&imsg);
523 end:
524 return ret;
527 int
528 ctl_noarg(struct parse_result *res, int argc, char **argv)
530 int ch;
532 while ((ch = getopt(argc, argv, "")) != -1)
533 ctl_usage(res->ctl);
534 argc -= optind;
535 argv += optind;
537 if (argc > 0)
538 ctl_usage(res->ctl);
540 return ctlaction(res);
543 int
544 ctl_add(struct parse_result *res, int argc, char **argv)
546 int ch;
548 while ((ch = getopt(argc, argv, "")) != -1)
549 ctl_usage(res->ctl);
550 argc -= optind;
551 argv += optind;
553 if (argc == 0)
554 ctl_usage(res->ctl);
555 res->files = argv;
557 return ctlaction(res);
560 int
561 ctl_show(struct parse_result *res, int argc, char **argv)
563 int ch;
565 while ((ch = getopt(argc, argv, "p")) != -1) {
566 switch (ch) {
567 case 'p':
568 res->pretty = 1;
569 break;
570 default:
571 ctl_usage(res->ctl);
575 return ctlaction(res);
578 int
579 ctl_load(struct parse_result *res, int argc, char **argv)
581 int ch;
583 while ((ch = getopt(argc, argv, "")) != -1)
584 ctl_usage(res->ctl);
585 argc -= optind;
586 argv += optind;
588 if (argc == 0)
589 res->file = NULL;
590 else if (argc == 1)
591 res->file = argv[0];
592 else
593 ctl_usage(res->ctl);
595 if (pledge("stdio rpath", NULL) == -1)
596 fatal("pledge");
598 return ctlaction(res);
601 int
602 ctl_jump(struct parse_result *res, int argc, char **argv)
604 int ch;
606 while ((ch = getopt(argc, argv, "")) != -1)
607 ctl_usage(res->ctl);
608 argc -= optind;
609 argv += optind;
611 if (argc != 1)
612 ctl_usage(res->ctl);
614 res->file = argv[0];
615 return ctlaction(res);
618 int
619 ctl_repeat(struct parse_result *res, int argc, char **argv)
621 int ch, b;
623 while ((ch = getopt(argc, argv, "")) != -1)
624 ctl_usage(res->ctl);
625 argc -= optind;
626 argv += optind;
628 if (argc != 2)
629 ctl_usage(res->ctl);
631 if (!strcmp(argv[1], "on"))
632 b = 1;
633 else if (!strcmp(argv[1], "off"))
634 b = 0;
635 else
636 ctl_usage(res->ctl);
638 res->rep.repeat_one = -1;
639 res->rep.repeat_all = -1;
640 if (!strcmp(argv[0], "one"))
641 res->rep.repeat_one = b;
642 else if (!strcmp(argv[0], "all"))
643 res->rep.repeat_all = b;
644 else
645 ctl_usage(res->ctl);
647 return ctlaction(res);
650 int
651 ctl_monitor(struct parse_result *res, int argc, char **argv)
653 int ch;
654 const char *events;
655 char *dup, *tmp, *tok;
657 while ((ch = getopt(argc, argv, "")) != -1)
658 ctl_usage(res->ctl);
659 argc -= optind;
660 argv += optind;
662 if (argc > 1)
663 ctl_usage(res->ctl);
665 if (argc == 1)
666 events = *argv;
667 else
668 events = "play,toggle,pause,stop,restart,flush,next,prev,"
669 "jump,repeat,add,load";
671 tmp = dup = xstrdup(events);
672 while ((tok = strsep(&tmp, ",")) != NULL) {
673 if (*tok == '\0')
674 continue;
676 if (!strcmp(tok, "play"))
677 res->monitor[IMSG_CTL_PLAY] = 1;
678 else if (!strcmp(tok, "toggle"))
679 res->monitor[IMSG_CTL_TOGGLE_PLAY] = 1;
680 else if (!strcmp(tok, "pause"))
681 res->monitor[IMSG_CTL_PAUSE] = 1;
682 else if (!strcmp(tok, "stop"))
683 res->monitor[IMSG_CTL_STOP] = 1;
684 else if (!strcmp(tok, "restart"))
685 res->monitor[IMSG_CTL_RESTART] = 1;
686 else if (!strcmp(tok, "flush"))
687 res->monitor[IMSG_CTL_FLUSH] = 1;
688 else if (!strcmp(tok, "next"))
689 res->monitor[IMSG_CTL_NEXT] = 1;
690 else if (!strcmp(tok, "prev"))
691 res->monitor[IMSG_CTL_PREV] = 1;
692 else if (!strcmp(tok, "jump"))
693 res->monitor[IMSG_CTL_JUMP] = 1;
694 else if (!strcmp(tok, "repeat"))
695 res->monitor[IMSG_CTL_REPEAT] = 1;
696 else if (!strcmp(tok, "add"))
697 res->monitor[IMSG_CTL_ADD] = 1;
698 else if (!strcmp(tok, "load"))
699 res->monitor[IMSG_CTL_COMMIT] = 1;
700 else
701 fatalx("unknown event \"%s\"", tok);
704 free(dup);
705 return ctlaction(res);
708 static int
709 ctl_get_lock(const char *lockfile)
711 int lockfd;
713 if ((lockfd = open(lockfile, O_WRONLY|O_CREAT, 0600)) == -1) {
714 log_debug("open failed: %s", strerror(errno));
715 return -1;
718 if (flock(lockfd, LOCK_EX|LOCK_NB) == -1) {
719 log_debug("flock failed: %s", strerror(errno));
720 if (errno != EAGAIN)
721 return -1;
722 while (flock(lockfd, LOCK_EX) == -1 && errno == EINTR)
723 /* nop */;
724 close(lockfd);
725 return -2;
727 log_debug("flock succeeded");
729 return lockfd;
732 static int
733 ctl_connect(void)
735 struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
736 struct sockaddr_un sa;
737 size_t size;
738 int fd, lockfd = -1, locked = 0, spawned = 0;
739 char *lockfile = NULL;
741 memset(&sa, 0, sizeof(sa));
742 sa.sun_family = AF_UNIX;
743 size = strlcpy(sa.sun_path, csock, sizeof(sa.sun_path));
744 if (size >= sizeof(sa.sun_path)) {
745 errno = ENAMETOOLONG;
746 return -1;
749 retry:
750 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
751 return -1;
753 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
754 log_debug("connection failed: %s", strerror(errno));
755 if (errno != ECONNREFUSED && errno != ENOENT)
756 goto failed;
757 close(fd);
759 if (!locked) {
760 xasprintf(&lockfile, "%s.lock", csock);
761 if ((lockfd = ctl_get_lock(lockfile)) < 0) {
762 log_debug("didn't get the lock (%d)", lockfd);
764 free(lockfile);
765 lockfile = NULL;
767 if (lockfd == -1)
768 goto retry;
771 /*
772 * Always retry at least once, even if we got
773 * the lock, because another client could have
774 * taken the lock, started the server and released
775 * the lock between our connect() and flock()
776 */
777 locked = 1;
778 goto retry;
781 if (!spawned) {
782 log_debug("spawning the daemon");
783 spawn_daemon();
784 spawned = 1;
787 nanosleep(&ts, NULL);
788 goto retry;
791 if (locked && lockfd >= 0) {
792 unlink(lockfile);
793 free(lockfile);
794 close(lockfd);
796 return fd;
798 failed:
799 if (locked) {
800 free(lockfile);
801 close(lockfd);
803 close(fd);
804 return -1;
807 __dead void
808 ctl(int argc, char **argv)
810 int ctl_sock;
812 log_init(1, LOG_DAEMON);
813 log_setverbose(verbose);
815 if (getcwd(cwd, sizeof(cwd)) == NULL)
816 fatal("getcwd");
818 if ((ctl_sock = ctl_connect()) == -1)
819 fatal("can't connect");
821 if (ctl_sock == -1)
822 fatalx("failed to connect to the daemon");
824 ibuf = xmalloc(sizeof(*ibuf));
825 imsg_init(ibuf, ctl_sock);
827 optreset = 1;
828 optind = 1;
830 exit(parse(argc, argv));