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 (file[0] == '>' && file[1] == ' ') {
311 file += 2;
312 curr = i;
314 if (file[0] == ' ' && file[1] == ' ')
315 file += 2;
316 if (file[0] == '.' && file[1] == '/')
317 file += 2;
319 if (*file == '/')
320 n = strlcpy(path, file, sizeof(path));
321 else
322 n = snprintf(path, sizeof(path), "%s/%s", cwd, file);
324 if (n >= sizeof(path)) {
325 log_warnx("path too long: %s", file);
326 continue;
329 i++;
330 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
331 path, sizeof(path));
334 free(line);
335 if (ferror(f))
336 fatal("getline");
337 fclose(f);
339 if (i == 0) {
340 *ret = 1;
341 return 1;
344 imsg_compose(ibuf, IMSG_CTL_COMMIT, 0, 0, -1,
345 &curr, sizeof(curr));
346 imsg_flush(ibuf);
347 return 0;
350 static int
351 show_monitor(struct imsg *imsg, int *ret)
353 int type;
355 if (imsg->hdr.type != IMSG_CTL_MONITOR) {
356 log_warnx("wrong message type received: %d",
357 imsg->hdr.type);
358 *ret = 1;
359 return 1;
362 if (IMSG_DATA_SIZE(*imsg) != sizeof(type)) {
363 log_warnx("size mismatch");
364 *ret = 1;
365 return 1;
368 memcpy(&type, imsg->data, sizeof(type));
369 switch (type) {
370 case IMSG_CTL_PLAY:
371 puts("play");
372 break;
373 case IMSG_CTL_TOGGLE_PLAY:
374 puts("toggle");
375 break;
376 case IMSG_CTL_PAUSE:
377 puts("pause");
378 break;
379 case IMSG_CTL_STOP:
380 puts("stop");
381 break;
382 case IMSG_CTL_RESTART:
383 puts("restart");
384 break;
385 case IMSG_CTL_FLUSH:
386 puts("flush");
387 break;
388 case IMSG_CTL_NEXT:
389 puts("next");
390 break;
391 case IMSG_CTL_PREV:
392 puts("prev");
393 break;
394 case IMSG_CTL_JUMP:
395 puts("jump");
396 break;
397 case IMSG_CTL_REPEAT:
398 puts("repeat");
399 break;
400 case IMSG_CTL_ADD:
401 puts("add");
402 break;
403 case IMSG_CTL_COMMIT:
404 puts("load");
405 break;
406 default:
407 puts("unknown");
408 break;
411 fflush(stdout);
412 return 0;
415 static int
416 ctlaction(struct parse_result *res)
418 struct imsg imsg;
419 ssize_t n;
420 int ret = 0, done = 1;
421 char **files;
423 switch (res->action) {
424 case PLAY:
425 imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
426 if (verbose) {
427 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
428 NULL, 0);
429 done = 0;
431 break;
432 case PAUSE:
433 imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
434 break;
435 case TOGGLE:
436 imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
437 if (verbose) {
438 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
439 NULL, 0);
440 done = 0;
442 break;
443 case STOP:
444 imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
445 break;
446 case RESTART:
447 imsg_compose(ibuf, IMSG_CTL_RESTART, 0, 0, -1, NULL, 0);
448 if (verbose) {
449 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
450 NULL, 0);
451 done = 0;
453 break;
454 case ADD:
455 done = 0;
456 files = res->files;
457 ret = enqueue_tracks(res->files);
458 break;
459 case FLUSH:
460 imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
461 break;
462 case SHOW:
463 done = 0;
464 imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
465 break;
466 case STATUS:
467 done = 0;
468 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
469 break;
470 case NEXT:
471 imsg_compose(ibuf, IMSG_CTL_NEXT, 0, 0, -1, NULL, 0);
472 if (verbose) {
473 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
474 NULL, 0);
475 done = 0;
477 break;
478 case PREV:
479 imsg_compose(ibuf, IMSG_CTL_PREV, 0, 0, -1, NULL, 0);
480 if (verbose) {
481 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
482 NULL, 0);
483 done = 0;
485 break;
486 case LOAD:
487 done = 0;
488 imsg_compose(ibuf, IMSG_CTL_BEGIN, 0, 0, -1, NULL, 0);
489 break;
490 case JUMP:
491 done = 0;
492 ret = jump_req(res->file);
493 break;
494 case REPEAT:
495 imsg_compose(ibuf, IMSG_CTL_REPEAT, 0, 0, -1,
496 &res->rep, sizeof(res->rep));
497 break;
498 case MONITOR:
499 done = 0;
500 imsg_compose(ibuf, IMSG_CTL_MONITOR, 0, 0, -1,
501 NULL, 0);
502 break;
503 case NONE:
504 /* action not expected */
505 fatalx("invalid action %u", res->action);
506 break;
509 if (ret != 0)
510 goto end;
512 imsg_flush(ibuf);
514 while (!done) {
515 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
516 fatalx("imsg_read error");
517 if (n == 0)
518 fatalx("pipe closed");
520 while (!done) {
521 if ((n = imsg_get(ibuf, &imsg)) == -1)
522 fatalx("imsg_get error");
523 if (n == 0)
524 break;
526 switch (res->action) {
527 case ADD:
528 done = show_add(&imsg, &ret, &files);
529 break;
530 case SHOW:
531 done = show_complete(res, &imsg, &ret);
532 break;
533 case PLAY:
534 case TOGGLE:
535 case RESTART:
536 case STATUS:
537 case NEXT:
538 case PREV:
539 case JUMP:
540 done = show_status(&imsg, &ret);
541 break;
542 case LOAD:
543 done = show_load(res, &imsg, &ret);
544 break;
545 case MONITOR:
546 done = show_monitor(&imsg, &ret);
547 break;
548 default:
549 done = 1;
550 break;
553 imsg_free(&imsg);
557 end:
558 return ret;
561 int
562 ctl_noarg(struct parse_result *res, int argc, char **argv)
564 int ch;
566 while ((ch = getopt(argc, argv, "")) != -1)
567 ctl_usage(res->ctl);
568 argc -= optind;
569 argv += optind;
571 if (argc > 0)
572 ctl_usage(res->ctl);
574 return ctlaction(res);
577 int
578 ctl_add(struct parse_result *res, int argc, char **argv)
580 int ch;
582 while ((ch = getopt(argc, argv, "")) != -1)
583 ctl_usage(res->ctl);
584 argc -= optind;
585 argv += optind;
587 if (argc == 0)
588 ctl_usage(res->ctl);
589 res->files = argv;
591 if (pledge("stdio rpath", NULL) == -1)
592 fatal("pledge");
594 return ctlaction(res);
597 int
598 ctl_show(struct parse_result *res, int argc, char **argv)
600 int ch;
602 while ((ch = getopt(argc, argv, "p")) != -1) {
603 switch (ch) {
604 case 'p':
605 res->pretty = 1;
606 break;
607 default:
608 ctl_usage(res->ctl);
612 return ctlaction(res);
615 int
616 ctl_load(struct parse_result *res, int argc, char **argv)
618 int ch;
620 while ((ch = getopt(argc, argv, "")) != -1)
621 ctl_usage(res->ctl);
622 argc -= optind;
623 argv += optind;
625 if (argc == 0)
626 res->file = NULL;
627 else if (argc == 1)
628 res->file = argv[0];
629 else
630 ctl_usage(res->ctl);
632 if (pledge("stdio rpath", NULL) == -1)
633 fatal("pledge");
635 return ctlaction(res);
638 int
639 ctl_jump(struct parse_result *res, int argc, char **argv)
641 int ch;
643 while ((ch = getopt(argc, argv, "")) != -1)
644 ctl_usage(res->ctl);
645 argc -= optind;
646 argv += optind;
648 if (argc != 1)
649 ctl_usage(res->ctl);
651 res->file = argv[0];
652 return ctlaction(res);
655 int
656 ctl_repeat(struct parse_result *res, int argc, char **argv)
658 int ch, b;
660 while ((ch = getopt(argc, argv, "")) != -1)
661 ctl_usage(res->ctl);
662 argc -= optind;
663 argv += optind;
665 if (argc != 2)
666 ctl_usage(res->ctl);
668 if (!strcmp(argv[1], "on"))
669 b = 1;
670 else if (!strcmp(argv[1], "off"))
671 b = 0;
672 else
673 ctl_usage(res->ctl);
675 res->rep.repeat_one = -1;
676 res->rep.repeat_all = -1;
677 if (!strcmp(argv[0], "one"))
678 res->rep.repeat_one = b;
679 else if (!strcmp(argv[0], "all"))
680 res->rep.repeat_all = b;
681 else
682 ctl_usage(res->ctl);
684 return ctlaction(res);
687 static int
688 ctl_get_lock(const char *lockfile)
690 int lockfd;
692 if ((lockfd = open(lockfile, O_WRONLY|O_CREAT, 0600)) == -1) {
693 log_debug("open failed: %s", strerror(errno));
694 return -1;
697 if (flock(lockfd, LOCK_EX|LOCK_NB) == -1) {
698 log_debug("flock failed: %s", strerror(errno));
699 if (errno != EAGAIN)
700 return -1;
701 while (flock(lockfd, LOCK_EX) == -1 && errno == EINTR)
702 /* nop */;
703 close(lockfd);
704 return -2;
706 log_debug("flock succeeded");
708 return lockfd;
711 static int
712 ctl_connect(void)
714 struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
715 struct sockaddr_un sa;
716 size_t size;
717 int fd, lockfd = -1, locked = 0, spawned = 0;
718 char *lockfile = NULL;
720 memset(&sa, 0, sizeof(sa));
721 sa.sun_family = AF_UNIX;
722 size = strlcpy(sa.sun_path, csock, sizeof(sa.sun_path));
723 if (size >= sizeof(sa.sun_path)) {
724 errno = ENAMETOOLONG;
725 return -1;
728 retry:
729 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
730 return -1;
732 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
733 log_debug("connection failed: %s", strerror(errno));
734 if (errno != ECONNREFUSED && errno != ENOENT)
735 goto failed;
736 close(fd);
738 if (!locked) {
739 xasprintf(&lockfile, "%s.lock", csock);
740 if ((lockfd = ctl_get_lock(lockfile)) < 0) {
741 log_debug("didn't get the lock (%d)", lockfd);
743 free(lockfile);
744 lockfile = NULL;
746 if (lockfd == -1)
747 goto retry;
750 /*
751 * Always retry at least once, even if we got
752 * the lock, because another client could have
753 * taken the lock, started the server and released
754 * the lock between our connect() and flock()
755 */
756 locked = 1;
757 goto retry;
760 if (!spawned) {
761 log_debug("spawning the daemon");
762 spawn_daemon();
763 spawned = 1;
766 nanosleep(&ts, NULL);
767 goto retry;
770 if (locked && lockfd >= 0) {
771 unlink(lockfile);
772 free(lockfile);
773 close(lockfd);
775 return fd;
777 failed:
778 if (locked) {
779 free(lockfile);
780 close(lockfd);
782 close(fd);
783 return -1;
786 __dead void
787 ctl(int argc, char **argv)
789 int ctl_sock;
791 log_init(1, LOG_DAEMON);
792 log_setverbose(verbose);
794 if ((ctl_sock = ctl_connect()) == -1)
795 fatal("can't connect");
797 if (ctl_sock == -1)
798 fatalx("failed to connect to the daemon");
800 ibuf = xmalloc(sizeof(*ibuf));
801 imsg_init(ibuf, ctl_sock);
803 optreset = 1;
804 optind = 1;
806 exit(parse(argc, argv));