Blob


1 /*
2 * Copyright (c) 2022 Omar Polo <op@openbsd.org>
3 *
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.
7 *
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.
15 */
17 #include <sys/types.h>
18 #include <sys/socket.h>
19 #include <sys/queue.h>
20 #include <sys/uio.h>
21 #include <sys/un.h>
23 #include <errno.h>
24 #include <event.h>
25 #include <fcntl.h>
26 #include <limits.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <stdint.h>
30 #include <string.h>
31 #include <syslog.h>
32 #include <unistd.h>
33 #include <imsg.h>
35 #include "amused.h"
36 #include "log.h"
37 #include "playlist.h"
38 #include "xmalloc.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, "" },
65 { NULL },
66 };
68 __dead void
69 usage(void)
70 {
71 fprintf(stderr, "usage: %s [-dv] [-s socket]\n", getprogname());
72 exit(1);
73 }
75 static __dead void
76 ctl_usage(struct ctl_command *ctl)
77 {
78 fprintf(stderr, "usage: %s [-v] [-s socket] %s %s\n", getprogname(),
79 ctl->name, ctl->usage);
80 exit(1);
81 }
83 static int
84 parse(int argc, char **argv)
85 {
86 struct ctl_command *ctl = NULL;
87 struct parse_result res;
88 const char *argv0;
89 int i, status;
91 memset(&res, 0, sizeof(res));
93 if ((argv0 = argv[0]) == NULL)
94 argv0 = "status";
96 for (i = 0; ctl_commands[i].name != NULL; ++i) {
97 if (strncmp(ctl_commands[i].name, argv0, strlen(argv0))
98 == 0) {
99 if (ctl != NULL) {
100 fprintf(stderr,
101 "ambiguous argument: %s\n", argv0);
102 usage();
104 ctl = &ctl_commands[i];
108 if (ctl == NULL) {
109 fprintf(stderr, "unknown argument: %s\n", argv[0]);
110 usage();
113 res.action = ctl->action;
114 res.ctl = ctl;
116 if (!ctl->has_pledge) {
117 /* pledge(2) default if command doesn't have its own */
118 if (pledge("stdio", NULL) == -1)
119 fatal("pledge");
122 status = ctl->main(&res, argc, argv);
123 close(ibuf->fd);
124 free(ibuf);
125 return status;
128 static int
129 enqueue_tracks(char **files)
131 char res[PATH_MAX];
132 int enq = 0;
134 for (; *files != NULL; ++files) {
135 memset(&res, 0, sizeof(res));
136 if (realpath(*files, res) == NULL) {
137 log_warn("realpath %s", *files);
138 continue;
141 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
142 res, sizeof(res));
143 enq++;
146 return enq == 0;
149 static int
150 jump_req(const char *arg)
152 char path[PATH_MAX];
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));
157 return 0;
160 static void
161 print_error_message(const char *prfx, struct imsg *imsg)
163 size_t datalen;
164 char *msg;
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);
174 free(msg);
177 static int
178 show_add(struct imsg *imsg, int *ret, char ***files)
180 if (**files == NULL) {
181 log_warnx("received more replies than file sent");
182 *ret = 1;
183 return 1;
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);
190 else
191 fatalx("got invalid message %d", imsg->hdr.type);
193 (*files)++;
194 return (**files) == NULL;
197 static int
198 show_complete(struct parse_result *res, struct imsg *imsg, int *ret)
200 struct player_status s;
201 size_t datalen;
203 if (imsg->hdr.type == IMSG_CTL_ERR) {
204 print_error_message("show failed", imsg);
205 *ret = 1;
206 return 1;
209 datalen = IMSG_DATA_SIZE(*imsg);
210 if (datalen == 0)
211 return 1;
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__);
219 if (res->pretty)
220 printf("%c ", s.status == STATE_PLAYING ? '>' : ' ');
221 printf("%s\n", s.path);
222 return 0;
225 static int
226 show_status(struct imsg *imsg, int *ret)
228 struct player_status s;
229 size_t datalen;
231 if (imsg->hdr.type == IMSG_CTL_ERR) {
232 print_error_message("show failed", imsg);
233 *ret = 1;
234 return 1;
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__);
247 switch (s.status) {
248 case STATE_STOPPED:
249 printf("stopped ");
250 break;
251 case STATE_PLAYING:
252 printf("playing ");
253 break;
254 case STATE_PAUSED:
255 printf("paused ");
256 break;
257 default:
258 printf("unknown ");
259 break;
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");
266 return 1;
269 static int
270 show_load(struct parse_result *res, struct imsg *imsg, int *ret)
272 FILE *f;
273 const char *file;
274 char *line = NULL;
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);
281 *ret = 1;
282 return 1;
285 if (imsg->hdr.type == IMSG_CTL_ADD)
286 return 0;
288 if (imsg->hdr.type == IMSG_CTL_COMMIT)
289 return 1;
291 if (imsg->hdr.type != IMSG_CTL_BEGIN)
292 fatalx("got unexpected message %d", imsg->hdr.type);
294 if (res->file == NULL)
295 f = stdin;
296 else if ((f = fopen(res->file, "r")) == NULL) {
297 log_warn("can't open %s", res->file);
298 *ret = 1;
299 return 1;
302 if (getcwd(cwd, sizeof(cwd)) == NULL)
303 fatal("getcwd");
305 while ((linelen = getline(&line, &linesize, f)) != -1) {
306 if (linelen == 0)
307 continue;
308 line[linelen-1] = '\0';
309 file = line;
310 if (!strncmp(file, "> ", 2)) {
311 file += 2;
312 curr = i;
313 } else if (!strncmp(file, " ", 2))
314 file += 2;
316 if (!strncmp(file, "./", 2))
317 file += 2;
319 memset(path, 0, sizeof(path));
320 if (*file == '/')
321 n = strlcpy(path, file, sizeof(path));
322 else
323 n = snprintf(path, sizeof(path), "%s/%s", cwd, file);
325 if (n >= sizeof(path)) {
326 log_warnx("path too long: %s", file);
327 continue;
330 i++;
331 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
332 path, sizeof(path));
335 free(line);
336 if (ferror(f))
337 fatal("getline");
338 fclose(f);
340 if (i == 0) {
341 *ret = 1;
342 return 1;
345 imsg_compose(ibuf, IMSG_CTL_COMMIT, 0, 0, -1,
346 &curr, sizeof(curr));
347 imsg_flush(ibuf);
348 return 0;
351 static int
352 show_monitor(struct imsg *imsg, int *ret)
354 int type;
356 if (imsg->hdr.type != IMSG_CTL_MONITOR) {
357 log_warnx("wrong message type received: %d",
358 imsg->hdr.type);
359 *ret = 1;
360 return 1;
363 if (IMSG_DATA_SIZE(*imsg) != sizeof(type)) {
364 log_warnx("size mismatch");
365 *ret = 1;
366 return 1;
369 memcpy(&type, imsg->data, sizeof(type));
370 switch (type) {
371 case IMSG_CTL_PLAY:
372 puts("play");
373 break;
374 case IMSG_CTL_TOGGLE_PLAY:
375 puts("toggle");
376 break;
377 case IMSG_CTL_PAUSE:
378 puts("pause");
379 break;
380 case IMSG_CTL_STOP:
381 puts("stop");
382 break;
383 case IMSG_CTL_RESTART:
384 puts("restart");
385 break;
386 case IMSG_CTL_FLUSH:
387 puts("flush");
388 break;
389 case IMSG_CTL_NEXT:
390 puts("next");
391 break;
392 case IMSG_CTL_PREV:
393 puts("prev");
394 break;
395 case IMSG_CTL_JUMP:
396 puts("jump");
397 break;
398 case IMSG_CTL_REPEAT:
399 puts("repeat");
400 break;
401 case IMSG_CTL_ADD:
402 puts("add");
403 break;
404 case IMSG_CTL_COMMIT:
405 puts("load");
406 break;
407 default:
408 puts("unknown");
409 break;
412 fflush(stdout);
413 return 0;
416 static int
417 ctlaction(struct parse_result *res)
419 struct imsg imsg;
420 ssize_t n;
421 int ret = 0, done = 1;
422 char **files;
424 switch (res->action) {
425 case PLAY:
426 imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
427 if (verbose) {
428 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
429 NULL, 0);
430 done = 0;
432 break;
433 case PAUSE:
434 imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
435 break;
436 case TOGGLE:
437 imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
438 if (verbose) {
439 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
440 NULL, 0);
441 done = 0;
443 break;
444 case STOP:
445 imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
446 break;
447 case RESTART:
448 imsg_compose(ibuf, IMSG_CTL_RESTART, 0, 0, -1, NULL, 0);
449 if (verbose) {
450 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
451 NULL, 0);
452 done = 0;
454 break;
455 case ADD:
456 done = 0;
457 files = res->files;
458 ret = enqueue_tracks(res->files);
459 break;
460 case FLUSH:
461 imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
462 break;
463 case SHOW:
464 done = 0;
465 imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
466 break;
467 case STATUS:
468 done = 0;
469 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
470 break;
471 case NEXT:
472 imsg_compose(ibuf, IMSG_CTL_NEXT, 0, 0, -1, NULL, 0);
473 if (verbose) {
474 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
475 NULL, 0);
476 done = 0;
478 break;
479 case PREV:
480 imsg_compose(ibuf, IMSG_CTL_PREV, 0, 0, -1, NULL, 0);
481 if (verbose) {
482 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
483 NULL, 0);
484 done = 0;
486 break;
487 case LOAD:
488 done = 0;
489 imsg_compose(ibuf, IMSG_CTL_BEGIN, 0, 0, -1, NULL, 0);
490 break;
491 case JUMP:
492 done = 0;
493 ret = jump_req(res->file);
494 break;
495 case REPEAT:
496 imsg_compose(ibuf, IMSG_CTL_REPEAT, 0, 0, -1,
497 &res->rep, sizeof(res->rep));
498 break;
499 case MONITOR:
500 done = 0;
501 imsg_compose(ibuf, IMSG_CTL_MONITOR, 0, 0, -1,
502 NULL, 0);
503 break;
504 case NONE:
505 /* action not expected */
506 fatalx("invalid action %u", res->action);
507 break;
510 if (ret != 0)
511 goto end;
513 imsg_flush(ibuf);
515 while (!done) {
516 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
517 fatalx("imsg_read error");
518 if (n == 0)
519 fatalx("pipe closed");
521 while (!done) {
522 if ((n = imsg_get(ibuf, &imsg)) == -1)
523 fatalx("imsg_get error");
524 if (n == 0)
525 break;
527 switch (res->action) {
528 case ADD:
529 done = show_add(&imsg, &ret, &files);
530 break;
531 case SHOW:
532 done = show_complete(res, &imsg, &ret);
533 break;
534 case PLAY:
535 case TOGGLE:
536 case RESTART:
537 case STATUS:
538 case NEXT:
539 case PREV:
540 case JUMP:
541 done = show_status(&imsg, &ret);
542 break;
543 case LOAD:
544 done = show_load(res, &imsg, &ret);
545 break;
546 case MONITOR:
547 done = show_monitor(&imsg, &ret);
548 break;
549 default:
550 done = 1;
551 break;
554 imsg_free(&imsg);
558 end:
559 return ret;
562 int
563 ctl_noarg(struct parse_result *res, int argc, char **argv)
565 int ch;
567 while ((ch = getopt(argc, argv, "")) != -1)
568 ctl_usage(res->ctl);
569 argc -= optind;
570 argv += optind;
572 if (argc > 0)
573 ctl_usage(res->ctl);
575 return ctlaction(res);
578 int
579 ctl_add(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 ctl_usage(res->ctl);
590 res->files = argv;
592 if (pledge("stdio rpath", NULL) == -1)
593 fatal("pledge");
595 return ctlaction(res);
598 int
599 ctl_show(struct parse_result *res, int argc, char **argv)
601 int ch;
603 while ((ch = getopt(argc, argv, "p")) != -1) {
604 switch (ch) {
605 case 'p':
606 res->pretty = 1;
607 break;
608 default:
609 ctl_usage(res->ctl);
613 return ctlaction(res);
616 int
617 ctl_load(struct parse_result *res, int argc, char **argv)
619 int ch;
621 while ((ch = getopt(argc, argv, "")) != -1)
622 ctl_usage(res->ctl);
623 argc -= optind;
624 argv += optind;
626 if (argc == 0)
627 res->file = NULL;
628 else if (argc == 1)
629 res->file = argv[0];
630 else
631 ctl_usage(res->ctl);
633 if (pledge("stdio rpath", NULL) == -1)
634 fatal("pledge");
636 return ctlaction(res);
639 int
640 ctl_jump(struct parse_result *res, int argc, char **argv)
642 int ch;
644 while ((ch = getopt(argc, argv, "")) != -1)
645 ctl_usage(res->ctl);
646 argc -= optind;
647 argv += optind;
649 if (argc != 1)
650 ctl_usage(res->ctl);
652 res->file = argv[0];
653 return ctlaction(res);
656 int
657 ctl_repeat(struct parse_result *res, int argc, char **argv)
659 int ch, b;
661 while ((ch = getopt(argc, argv, "")) != -1)
662 ctl_usage(res->ctl);
663 argc -= optind;
664 argv += optind;
666 if (argc != 2)
667 ctl_usage(res->ctl);
669 if (!strcmp(argv[1], "on"))
670 b = 1;
671 else if (!strcmp(argv[1], "off"))
672 b = 0;
673 else
674 ctl_usage(res->ctl);
676 res->rep.repeat_one = -1;
677 res->rep.repeat_all = -1;
678 if (!strcmp(argv[0], "one"))
679 res->rep.repeat_one = b;
680 else if (!strcmp(argv[0], "all"))
681 res->rep.repeat_all = b;
682 else
683 ctl_usage(res->ctl);
685 return ctlaction(res);
688 static int
689 ctl_get_lock(const char *lockfile)
691 int lockfd;
693 if ((lockfd = open(lockfile, O_WRONLY|O_CREAT, 0600)) == -1) {
694 log_debug("open failed: %s", strerror(errno));
695 return -1;
698 if (flock(lockfd, LOCK_EX|LOCK_NB) == -1) {
699 log_debug("flock failed: %s", strerror(errno));
700 if (errno != EAGAIN)
701 return -1;
702 while (flock(lockfd, LOCK_EX) == -1 && errno == EINTR)
703 /* nop */;
704 close(lockfd);
705 return -2;
707 log_debug("flock succeeded");
709 return lockfd;
712 static int
713 ctl_connect(void)
715 struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
716 struct sockaddr_un sa;
717 size_t size;
718 int fd, lockfd = -1, locked = 0, spawned = 0;
719 char *lockfile = NULL;
721 memset(&sa, 0, sizeof(sa));
722 sa.sun_family = AF_UNIX;
723 size = strlcpy(sa.sun_path, csock, sizeof(sa.sun_path));
724 if (size >= sizeof(sa.sun_path)) {
725 errno = ENAMETOOLONG;
726 return -1;
729 retry:
730 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
731 return -1;
733 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
734 log_debug("connection failed: %s", strerror(errno));
735 if (errno != ECONNREFUSED && errno != ENOENT)
736 goto failed;
737 close(fd);
739 if (!locked) {
740 xasprintf(&lockfile, "%s.lock", csock);
741 if ((lockfd = ctl_get_lock(lockfile)) < 0) {
742 log_debug("didn't get the lock (%d)", lockfd);
744 free(lockfile);
745 lockfile = NULL;
747 if (lockfd == -1)
748 goto retry;
751 /*
752 * Always retry at least once, even if we got
753 * the lock, because another client could have
754 * taken the lock, started the server and released
755 * the lock between our connect() and flock()
756 */
757 locked = 1;
758 goto retry;
761 if (!spawned) {
762 log_debug("spawning the daemon");
763 spawn_daemon();
764 spawned = 1;
767 nanosleep(&ts, NULL);
768 goto retry;
771 if (locked && lockfd >= 0) {
772 unlink(lockfile);
773 free(lockfile);
774 close(lockfd);
776 return fd;
778 failed:
779 if (locked) {
780 free(lockfile);
781 close(lockfd);
783 close(fd);
784 return -1;
787 __dead void
788 ctl(int argc, char **argv)
790 int ctl_sock;
792 log_init(1, LOG_DAEMON);
793 log_setverbose(verbose);
795 if ((ctl_sock = ctl_connect()) == -1)
796 fatal("can't connect");
798 if (ctl_sock == -1)
799 fatalx("failed to connect to the daemon");
801 ibuf = xmalloc(sizeof(*ibuf));
802 imsg_init(ibuf, ctl_sock);
804 optreset = 1;
805 optind = 1;
807 exit(parse(argc, argv));