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 if (canonpath(file, path, sizeof(path)) == -1) {
207 log_warnx("canonpath %s", file);
208 continue;
211 i++;
212 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
213 path, sizeof(path));
216 free(line);
217 if (ferror(f))
218 fatal("getline");
219 fclose(f);
221 if (i == 0) {
222 *ret = 1;
223 return 1;
226 imsg_compose(ibuf, IMSG_CTL_COMMIT, 0, 0, -1,
227 &curr, sizeof(curr));
228 imsg_flush(ibuf);
229 return 0;
232 static const char *
233 imsg_strerror(struct imsg *imsg)
235 size_t datalen;
236 const char *msg;
238 datalen = IMSG_DATA_SIZE(*imsg);
239 msg = imsg->data;
240 if (datalen == 0 || msg[datalen-1] != '\0')
241 fatalx("malformed error message");
243 return msg;
246 static const char *
247 imsg_name(int type)
249 switch (type) {
250 case IMSG_CTL_PLAY:
251 return "play";
252 case IMSG_CTL_TOGGLE_PLAY:
253 return "toggle";
254 case IMSG_CTL_PAUSE:
255 return "pause";
256 case IMSG_CTL_STOP:
257 return "stop";
258 case IMSG_CTL_RESTART:
259 return "restart";
260 case IMSG_CTL_FLUSH:
261 return "flush";
262 case IMSG_CTL_NEXT:
263 return "next";
264 case IMSG_CTL_PREV:
265 return "prev";
266 case IMSG_CTL_JUMP:
267 return "jump";
268 case IMSG_CTL_REPEAT:
269 return "repeat";
270 case IMSG_CTL_ADD:
271 return "add";
272 case IMSG_CTL_COMMIT:
273 return "load";
274 default:
275 return "unknown";
279 static int
280 ctlaction(struct parse_result *res)
282 char path[PATH_MAX];
283 struct imsg imsg;
284 struct player_status ps;
285 size_t datalen;
286 ssize_t n;
287 int i, type, ret = 0, done = 1;
289 switch (res->action) {
290 case PLAY:
291 imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
292 if (verbose) {
293 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
294 NULL, 0);
295 done = 0;
297 break;
298 case PAUSE:
299 imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
300 break;
301 case TOGGLE:
302 imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
303 if (verbose) {
304 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
305 NULL, 0);
306 done = 0;
308 break;
309 case STOP:
310 imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
311 break;
312 case RESTART:
313 imsg_compose(ibuf, IMSG_CTL_RESTART, 0, 0, -1, NULL, 0);
314 if (verbose) {
315 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
316 NULL, 0);
317 done = 0;
319 break;
320 case ADD:
321 done = 0;
322 for (i = 0; res->files[i] != NULL; ++i) {
323 if (canonpath(res->files[i], path, sizeof(path))
324 == -1) {
325 log_warn("canonpath %s", res->files[i]);
326 continue;
329 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
330 path, sizeof(path));
332 ret = i == 0;
333 break;
334 case FLUSH:
335 imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
336 break;
337 case SHOW:
338 done = 0;
339 imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
340 break;
341 case STATUS:
342 done = 0;
343 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
344 break;
345 case NEXT:
346 imsg_compose(ibuf, IMSG_CTL_NEXT, 0, 0, -1, NULL, 0);
347 if (verbose) {
348 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
349 NULL, 0);
350 done = 0;
352 break;
353 case PREV:
354 imsg_compose(ibuf, IMSG_CTL_PREV, 0, 0, -1, NULL, 0);
355 if (verbose) {
356 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
357 NULL, 0);
358 done = 0;
360 break;
361 case LOAD:
362 done = 0;
363 imsg_compose(ibuf, IMSG_CTL_BEGIN, 0, 0, -1, NULL, 0);
364 break;
365 case JUMP:
366 done = 0;
367 memset(path, 0, sizeof(path));
368 strlcpy(path, res->file, sizeof(path));
369 imsg_compose(ibuf, IMSG_CTL_JUMP, 0, 0, -1,
370 path, sizeof(path));
371 break;
372 case REPEAT:
373 imsg_compose(ibuf, IMSG_CTL_REPEAT, 0, 0, -1,
374 &res->rep, sizeof(res->rep));
375 break;
376 case MONITOR:
377 done = 0;
378 imsg_compose(ibuf, IMSG_CTL_MONITOR, 0, 0, -1,
379 NULL, 0);
380 break;
381 case NONE:
382 /* action not expected */
383 fatalx("invalid action %u", res->action);
384 break;
387 if (ret != 0)
388 goto end;
390 imsg_flush(ibuf);
392 i = 0;
393 while (!done) {
394 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
395 fatalx("imsg_read error");
396 if (n == 0)
397 fatalx("pipe closed");
399 while (!done) {
400 if ((n = imsg_get(ibuf, &imsg)) == -1)
401 fatalx("imsg_get error");
402 if (n == 0)
403 break;
405 if (imsg.hdr.type == IMSG_CTL_ERR) {
406 log_warnx("%s: %s", res->ctl->name,
407 imsg_strerror(&imsg));
408 ret = 1;
409 done = 1;
410 break;
413 datalen = IMSG_DATA_SIZE(imsg);
415 switch (res->action) {
416 case ADD:
417 if (res->files[i] == NULL)
418 fatalx("received more replies than "
419 "files enqueued.");
421 if (imsg.hdr.type == IMSG_CTL_ADD)
422 log_debug("enqueued %s", res->files[i]);
423 else
424 fatalx("invalid message %d",
425 imsg.hdr.type);
426 i++;
427 done = res->files[i] == NULL;
428 break;
429 case SHOW:
430 if (datalen == 0) {
431 done = 1;
432 break;
434 if (datalen != sizeof(ps))
435 fatalx("data size mismatch");
436 memcpy(&ps, imsg.data, sizeof(ps));
437 if (ps.path[sizeof(ps.path) - 1] != '\0')
438 fatalx("received corrupted data");
439 if (res->pretty) {
440 char c = ' ';
441 if (ps.status == STATE_PLAYING)
442 c = '>';
443 printf("%c ", c);
445 puts(ps.path);
446 break;
447 case PLAY:
448 case TOGGLE:
449 case RESTART:
450 case STATUS:
451 case NEXT:
452 case PREV:
453 case JUMP:
454 if (imsg.hdr.type != IMSG_CTL_STATUS)
455 fatalx("invalid message %d",
456 imsg.hdr.type);
458 if (datalen != sizeof(ps))
459 fatalx("data size mismatch");
460 memcpy(&ps, imsg.data, sizeof(ps));
461 if (ps.path[sizeof(ps.path) - 1] != '\0')
462 fatalx("received corrupted data");
464 if (ps.status == STATE_STOPPED)
465 printf("stopped ");
466 else if (ps.status == STATE_PLAYING)
467 printf("playing ");
468 else if (ps.status == STATE_PAUSED)
469 printf("paused ");
470 else
471 printf("unknown ");
473 puts(ps.path);
474 printf("repat one %s\nrepeat all %s\n",
475 ps.rp.repeat_one ? "on" : "off",
476 ps.rp.repeat_all ? "on" : "off");
478 done = 1;
479 break;
480 case LOAD:
481 if (imsg.hdr.type == IMSG_CTL_ADD)
482 break;
483 if (imsg.hdr.type == IMSG_CTL_COMMIT) {
484 done = 1;
485 break;
488 if (imsg.hdr.type != IMSG_CTL_BEGIN)
489 fatalx("invalid message %d",
490 imsg.hdr.type);
492 load_files(res, &ret);
493 break;
494 case MONITOR:
495 if (imsg.hdr.type != IMSG_CTL_MONITOR)
496 fatalx("invalid message %d",
497 imsg.hdr.type);
499 if (datalen != sizeof(type))
500 fatalx("data size mismatch");
502 memcpy(&type, imsg.data, sizeof(type));
503 if (type < 0 || type > IMSG__LAST)
504 fatalx("received corrupted data");
506 if (!res->monitor[type])
507 break;
509 puts(imsg_name(type));
510 fflush(stdout);
511 break;
512 default:
513 done = 1;
514 break;
517 imsg_free(&imsg);
521 end:
522 return ret;
525 int
526 ctl_noarg(struct parse_result *res, int argc, char **argv)
528 int ch;
530 while ((ch = getopt(argc, argv, "")) != -1)
531 ctl_usage(res->ctl);
532 argc -= optind;
533 argv += optind;
535 if (argc > 0)
536 ctl_usage(res->ctl);
538 return ctlaction(res);
541 int
542 ctl_add(struct parse_result *res, int argc, char **argv)
544 int ch;
546 while ((ch = getopt(argc, argv, "")) != -1)
547 ctl_usage(res->ctl);
548 argc -= optind;
549 argv += optind;
551 if (argc == 0)
552 ctl_usage(res->ctl);
553 res->files = argv;
555 return ctlaction(res);
558 int
559 ctl_show(struct parse_result *res, int argc, char **argv)
561 int ch;
563 while ((ch = getopt(argc, argv, "p")) != -1) {
564 switch (ch) {
565 case 'p':
566 res->pretty = 1;
567 break;
568 default:
569 ctl_usage(res->ctl);
573 return ctlaction(res);
576 int
577 ctl_load(struct parse_result *res, int argc, char **argv)
579 int ch;
581 while ((ch = getopt(argc, argv, "")) != -1)
582 ctl_usage(res->ctl);
583 argc -= optind;
584 argv += optind;
586 if (argc == 0)
587 res->file = NULL;
588 else if (argc == 1)
589 res->file = argv[0];
590 else
591 ctl_usage(res->ctl);
593 if (pledge("stdio rpath", NULL) == -1)
594 fatal("pledge");
596 return ctlaction(res);
599 int
600 ctl_jump(struct parse_result *res, int argc, char **argv)
602 int ch;
604 while ((ch = getopt(argc, argv, "")) != -1)
605 ctl_usage(res->ctl);
606 argc -= optind;
607 argv += optind;
609 if (argc != 1)
610 ctl_usage(res->ctl);
612 res->file = argv[0];
613 return ctlaction(res);
616 int
617 ctl_repeat(struct parse_result *res, int argc, char **argv)
619 int ch, b;
621 while ((ch = getopt(argc, argv, "")) != -1)
622 ctl_usage(res->ctl);
623 argc -= optind;
624 argv += optind;
626 if (argc != 2)
627 ctl_usage(res->ctl);
629 if (!strcmp(argv[1], "on"))
630 b = 1;
631 else if (!strcmp(argv[1], "off"))
632 b = 0;
633 else
634 ctl_usage(res->ctl);
636 res->rep.repeat_one = -1;
637 res->rep.repeat_all = -1;
638 if (!strcmp(argv[0], "one"))
639 res->rep.repeat_one = b;
640 else if (!strcmp(argv[0], "all"))
641 res->rep.repeat_all = b;
642 else
643 ctl_usage(res->ctl);
645 return ctlaction(res);
648 int
649 ctl_monitor(struct parse_result *res, int argc, char **argv)
651 int ch;
652 const char *events;
653 char *dup, *tmp, *tok;
655 while ((ch = getopt(argc, argv, "")) != -1)
656 ctl_usage(res->ctl);
657 argc -= optind;
658 argv += optind;
660 if (argc > 1)
661 ctl_usage(res->ctl);
663 if (argc == 1)
664 events = *argv;
665 else
666 events = "play,toggle,pause,stop,restart,flush,next,prev,"
667 "jump,repeat,add,load";
669 tmp = dup = xstrdup(events);
670 while ((tok = strsep(&tmp, ",")) != NULL) {
671 if (*tok == '\0')
672 continue;
674 if (!strcmp(tok, "play"))
675 res->monitor[IMSG_CTL_PLAY] = 1;
676 else if (!strcmp(tok, "toggle"))
677 res->monitor[IMSG_CTL_TOGGLE_PLAY] = 1;
678 else if (!strcmp(tok, "pause"))
679 res->monitor[IMSG_CTL_PAUSE] = 1;
680 else if (!strcmp(tok, "stop"))
681 res->monitor[IMSG_CTL_STOP] = 1;
682 else if (!strcmp(tok, "restart"))
683 res->monitor[IMSG_CTL_RESTART] = 1;
684 else if (!strcmp(tok, "flush"))
685 res->monitor[IMSG_CTL_FLUSH] = 1;
686 else if (!strcmp(tok, "next"))
687 res->monitor[IMSG_CTL_NEXT] = 1;
688 else if (!strcmp(tok, "prev"))
689 res->monitor[IMSG_CTL_PREV] = 1;
690 else if (!strcmp(tok, "jump"))
691 res->monitor[IMSG_CTL_JUMP] = 1;
692 else if (!strcmp(tok, "repeat"))
693 res->monitor[IMSG_CTL_REPEAT] = 1;
694 else if (!strcmp(tok, "add"))
695 res->monitor[IMSG_CTL_ADD] = 1;
696 else if (!strcmp(tok, "load"))
697 res->monitor[IMSG_CTL_COMMIT] = 1;
698 else
699 fatalx("unknown event \"%s\"", tok);
702 free(dup);
703 return ctlaction(res);
706 static int
707 ctl_get_lock(const char *lockfile)
709 int lockfd;
711 if ((lockfd = open(lockfile, O_WRONLY|O_CREAT, 0600)) == -1) {
712 log_debug("open failed: %s", strerror(errno));
713 return -1;
716 if (flock(lockfd, LOCK_EX|LOCK_NB) == -1) {
717 log_debug("flock failed: %s", strerror(errno));
718 if (errno != EAGAIN)
719 return -1;
720 while (flock(lockfd, LOCK_EX) == -1 && errno == EINTR)
721 /* nop */;
722 close(lockfd);
723 return -2;
725 log_debug("flock succeeded");
727 return lockfd;
730 static int
731 ctl_connect(void)
733 struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
734 struct sockaddr_un sa;
735 size_t size;
736 int fd, lockfd = -1, locked = 0, spawned = 0;
737 char *lockfile = NULL;
739 memset(&sa, 0, sizeof(sa));
740 sa.sun_family = AF_UNIX;
741 size = strlcpy(sa.sun_path, csock, sizeof(sa.sun_path));
742 if (size >= sizeof(sa.sun_path)) {
743 errno = ENAMETOOLONG;
744 return -1;
747 retry:
748 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
749 return -1;
751 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
752 log_debug("connection failed: %s", strerror(errno));
753 if (errno != ECONNREFUSED && errno != ENOENT)
754 goto failed;
755 close(fd);
757 if (!locked) {
758 xasprintf(&lockfile, "%s.lock", csock);
759 if ((lockfd = ctl_get_lock(lockfile)) < 0) {
760 log_debug("didn't get the lock (%d)", lockfd);
762 free(lockfile);
763 lockfile = NULL;
765 if (lockfd == -1)
766 goto retry;
769 /*
770 * Always retry at least once, even if we got
771 * the lock, because another client could have
772 * taken the lock, started the server and released
773 * the lock between our connect() and flock()
774 */
775 locked = 1;
776 goto retry;
779 if (!spawned) {
780 log_debug("spawning the daemon");
781 spawn_daemon();
782 spawned = 1;
785 nanosleep(&ts, NULL);
786 goto retry;
789 if (locked && lockfd >= 0) {
790 unlink(lockfile);
791 free(lockfile);
792 close(lockfd);
794 return fd;
796 failed:
797 if (locked) {
798 free(lockfile);
799 close(lockfd);
801 close(fd);
802 return -1;
805 __dead void
806 ctl(int argc, char **argv)
808 int ctl_sock;
810 log_init(1, LOG_DAEMON);
811 log_setverbose(verbose);
813 if (getcwd(cwd, sizeof(cwd)) == NULL)
814 fatal("getcwd");
816 if ((ctl_sock = ctl_connect()) == -1)
817 fatal("can't connect");
819 if (ctl_sock == -1)
820 fatalx("failed to connect to the daemon");
822 ibuf = xmalloc(sizeof(*ibuf));
823 imsg_init(ibuf, ctl_sock);
825 optreset = 1;
826 optind = 1;
828 exit(parse(argc, argv));