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 <sndio.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;
43 int ctl_noarg(struct parse_result *, int, char **);
44 int ctl_add(struct parse_result *, int, char **);
45 int ctl_show(struct parse_result *, int, char **);
46 int ctl_load(struct parse_result *, int, char **);
47 int ctl_jump(struct parse_result *, int, char **);
48 int ctl_repeat(struct parse_result *, int, char **);
50 struct ctl_command ctl_commands[] = {
51 { "play", PLAY, ctl_noarg, "" },
52 { "pause", PAUSE, ctl_noarg, "" },
53 { "toggle", TOGGLE, ctl_noarg, "" },
54 { "stop", STOP, ctl_noarg, "" },
55 { "restart", RESTART, ctl_noarg, "" },
56 { "add", ADD, ctl_add, "files...", 1 },
57 { "flush", FLUSH, ctl_noarg, "" },
58 { "show", SHOW, ctl_show, "[-p]" },
59 { "status", STATUS, ctl_noarg, "" },
60 { "next", NEXT, ctl_noarg, "" },
61 { "prev", PREV, ctl_noarg, "" },
62 { "load", LOAD, ctl_load, "[file]", 1 },
63 { "jump", JUMP, ctl_jump, "pattern" },
64 { "repeat", REPEAT, ctl_repeat, "one|all on|off" },
65 { "monitor", MONITOR, ctl_noarg, "" },
66 { NULL },
67 };
69 __dead void
70 usage(void)
71 {
72 fprintf(stderr, "usage: %s [-dv] [-s socket]\n", getprogname());
73 exit(1);
74 }
76 static __dead void
77 ctl_usage(struct ctl_command *ctl)
78 {
79 fprintf(stderr, "usage: %s [-v] [-s socket] %s %s\n", getprogname(),
80 ctl->name, ctl->usage);
81 exit(1);
82 }
84 static int
85 parse(int argc, char **argv)
86 {
87 struct ctl_command *ctl = NULL;
88 struct parse_result res;
89 const char *argv0;
90 int i, status;
92 memset(&res, 0, sizeof(res));
94 if ((argv0 = argv[0]) == NULL)
95 argv0 = "status";
97 for (i = 0; ctl_commands[i].name != NULL; ++i) {
98 if (strncmp(ctl_commands[i].name, argv0, strlen(argv0))
99 == 0) {
100 if (ctl != NULL) {
101 fprintf(stderr,
102 "ambiguous argument: %s\n", argv0);
103 usage();
105 ctl = &ctl_commands[i];
109 if (ctl == NULL) {
110 fprintf(stderr, "unknown argument: %s\n", argv[0]);
111 usage();
114 res.action = ctl->action;
115 res.ctl = ctl;
117 if (!ctl->has_pledge) {
118 /* pledge(2) default if command doesn't have its own */
119 if (pledge("stdio", NULL) == -1)
120 fatal("pledge");
123 status = ctl->main(&res, argc, argv);
124 close(ibuf->fd);
125 free(ibuf);
126 return status;
129 static int
130 enqueue_tracks(char **files)
132 char res[PATH_MAX];
133 int enq = 0;
135 for (; *files != NULL; ++files) {
136 memset(&res, 0, sizeof(res));
137 if (realpath(*files, res) == NULL) {
138 log_warn("realpath %s", *files);
139 continue;
142 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
143 res, sizeof(res));
144 enq++;
147 return enq == 0;
150 static int
151 jump_req(const char *arg)
153 char path[PATH_MAX];
155 memset(path, 0, sizeof(path));
156 strlcpy(path, arg, sizeof(path));
157 imsg_compose(ibuf, IMSG_CTL_JUMP, 0, 0, -1, path, sizeof(path));
158 return 0;
161 static void
162 print_error_message(const char *prfx, struct imsg *imsg)
164 size_t datalen;
165 char *msg;
167 datalen = IMSG_DATA_SIZE(*imsg);
168 if ((msg = calloc(1, datalen)) == NULL)
169 fatal("calloc %zu", datalen);
170 memcpy(msg, imsg->data, datalen);
171 if (datalen == 0 || msg[datalen-1] != '\0')
172 fatalx("malformed error message");
174 log_warnx("%s: %s", prfx, msg);
175 free(msg);
178 static int
179 show_add(struct imsg *imsg, int *ret, char ***files)
181 if (**files == NULL) {
182 log_warnx("received more replies than file sent");
183 *ret = 1;
184 return 1;
187 if (imsg->hdr.type == IMSG_CTL_ERR)
188 print_error_message(**files, imsg);
189 else if (imsg->hdr.type == IMSG_CTL_ADD)
190 log_debug("enqueued %s", **files);
191 else
192 fatalx("got invalid message %d", imsg->hdr.type);
194 (*files)++;
195 return (**files) == NULL;
198 static int
199 show_complete(struct parse_result *res, struct imsg *imsg, int *ret)
201 struct player_status s;
202 size_t datalen;
204 if (imsg->hdr.type == IMSG_CTL_ERR) {
205 print_error_message("show failed", imsg);
206 *ret = 1;
207 return 1;
210 datalen = IMSG_DATA_SIZE(*imsg);
211 if (datalen == 0)
212 return 1;
214 if (datalen != sizeof(s))
215 fatalx("%s: data size mismatch", __func__);
216 memcpy(&s, imsg->data, sizeof(s));
217 if (s.path[sizeof(s.path)-1] != '\0')
218 fatalx("%s: data corrupted?", __func__);
220 if (res->pretty)
221 printf("%c ", s.status == STATE_PLAYING ? '>' : ' ');
222 printf("%s\n", s.path);
223 return 0;
226 static int
227 show_status(struct imsg *imsg, int *ret)
229 struct player_status s;
230 size_t datalen;
232 if (imsg->hdr.type == IMSG_CTL_ERR) {
233 print_error_message("show failed", imsg);
234 *ret = 1;
235 return 1;
238 if (imsg->hdr.type != IMSG_CTL_STATUS)
239 fatalx("%s: got wrong reply", __func__);
241 datalen = IMSG_DATA_SIZE(*imsg);
242 if (datalen != sizeof(s))
243 fatalx("%s: data size mismatch", __func__);
244 memcpy(&s, imsg->data, sizeof(s));
245 if (s.path[sizeof(s.path)-1] != '\0')
246 fatalx("%s: data corrupted?", __func__);
248 switch (s.status) {
249 case STATE_STOPPED:
250 printf("stopped ");
251 break;
252 case STATE_PLAYING:
253 printf("playing ");
254 break;
255 case STATE_PAUSED:
256 printf("paused ");
257 break;
258 default:
259 printf("unknown ");
260 break;
263 printf("%s\n", s.path);
264 printf("repeat one %s\nrepeat all %s\n",
265 s.rp.repeat_one ? "on" : "off",
266 s.rp.repeat_all ? "on" : "off");
267 return 1;
270 static int
271 show_load(struct parse_result *res, struct imsg *imsg, int *ret)
273 FILE *f;
274 const char *file;
275 char *line = NULL;
276 char path[PATH_MAX];
277 size_t linesize = 0, i = 0;
278 ssize_t linelen, curr = -1;
280 if (imsg->hdr.type == IMSG_CTL_ERR) {
281 print_error_message("load failed", imsg);
282 *ret = 1;
283 return 1;
286 if (imsg->hdr.type == IMSG_CTL_ADD)
287 return 0;
289 if (imsg->hdr.type == IMSG_CTL_COMMIT)
290 return 1;
292 if (imsg->hdr.type != IMSG_CTL_BEGIN)
293 fatalx("got unexpected message %d", imsg->hdr.type);
295 if (res->file == NULL)
296 f = stdin;
297 else if ((f = fopen(res->file, "r")) == NULL) {
298 log_warn("can't open %s", res->file);
299 *ret = 1;
300 return 1;
303 while ((linelen = getline(&line, &linesize, f)) != -1) {
304 if (linelen == 0)
305 continue;
306 line[linelen-1] = '\0';
307 file = line;
308 if (file[0] == '>' && file[1] == ' ') {
309 file += 2;
310 curr = i;
312 if (file[0] == ' ' && file[1] == ' ')
313 file += 2;
315 memset(&path, 0, sizeof(path));
316 if (realpath(file, path) == NULL) {
317 log_warn("realpath %s", file);
318 continue;
321 i++;
322 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
323 path, sizeof(path));
326 free(line);
327 if (ferror(f))
328 fatal("getline");
329 fclose(f);
331 if (i == 0) {
332 *ret = 1;
333 return 1;
336 imsg_compose(ibuf, IMSG_CTL_COMMIT, 0, 0, -1,
337 &curr, sizeof(curr));
338 imsg_flush(ibuf);
339 return 0;
342 static int
343 show_monitor(struct imsg *imsg, int *ret)
345 int type;
347 if (imsg->hdr.type != IMSG_CTL_MONITOR) {
348 log_warnx("wrong message type received: %d",
349 imsg->hdr.type);
350 *ret = 1;
351 return 1;
354 if (IMSG_DATA_SIZE(*imsg) != sizeof(type)) {
355 log_warnx("size mismatch");
356 *ret = 1;
357 return 1;
360 memcpy(&type, imsg->data, sizeof(type));
361 switch (type) {
362 case IMSG_CTL_PLAY:
363 puts("play");
364 break;
365 case IMSG_CTL_TOGGLE_PLAY:
366 puts("toggle");
367 break;
368 case IMSG_CTL_PAUSE:
369 puts("pause");
370 break;
371 case IMSG_CTL_STOP:
372 puts("stop");
373 break;
374 case IMSG_CTL_RESTART:
375 puts("restart");
376 break;
377 case IMSG_CTL_FLUSH:
378 puts("flush");
379 break;
380 case IMSG_CTL_NEXT:
381 puts("next");
382 break;
383 case IMSG_CTL_PREV:
384 puts("prev");
385 break;
386 case IMSG_CTL_JUMP:
387 puts("jump");
388 break;
389 case IMSG_CTL_REPEAT:
390 puts("repeat");
391 break;
392 case IMSG_CTL_ADD:
393 puts("add");
394 break;
395 case IMSG_CTL_COMMIT:
396 puts("load");
397 break;
398 default:
399 puts("unknown");
400 break;
403 fflush(stdout);
404 return 0;
407 static int
408 ctlaction(struct parse_result *res)
410 struct imsg imsg;
411 ssize_t n;
412 int ret = 0, done = 1;
413 char **files;
415 switch (res->action) {
416 case PLAY:
417 imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
418 if (verbose) {
419 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
420 NULL, 0);
421 done = 0;
423 break;
424 case PAUSE:
425 imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
426 break;
427 case TOGGLE:
428 imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
429 if (verbose) {
430 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
431 NULL, 0);
432 done = 0;
434 break;
435 case STOP:
436 imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
437 break;
438 case RESTART:
439 imsg_compose(ibuf, IMSG_CTL_RESTART, 0, 0, -1, NULL, 0);
440 if (verbose) {
441 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
442 NULL, 0);
443 done = 0;
445 break;
446 case ADD:
447 done = 0;
448 files = res->files;
449 ret = enqueue_tracks(res->files);
450 break;
451 case FLUSH:
452 imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
453 break;
454 case SHOW:
455 done = 0;
456 imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
457 break;
458 case STATUS:
459 done = 0;
460 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
461 break;
462 case NEXT:
463 imsg_compose(ibuf, IMSG_CTL_NEXT, 0, 0, -1, NULL, 0);
464 if (verbose) {
465 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
466 NULL, 0);
467 done = 0;
469 break;
470 case PREV:
471 imsg_compose(ibuf, IMSG_CTL_PREV, 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 LOAD:
479 done = 0;
480 imsg_compose(ibuf, IMSG_CTL_BEGIN, 0, 0, -1, NULL, 0);
481 break;
482 case JUMP:
483 done = 0;
484 ret = jump_req(res->file);
485 break;
486 case REPEAT:
487 imsg_compose(ibuf, IMSG_CTL_REPEAT, 0, 0, -1,
488 &res->rep, sizeof(res->rep));
489 break;
490 case MONITOR:
491 done = 0;
492 imsg_compose(ibuf, IMSG_CTL_MONITOR, 0, 0, -1,
493 NULL, 0);
494 break;
495 case NONE:
496 /* action not expected */
497 fatalx("invalid action %u", res->action);
498 break;
501 if (ret != 0)
502 goto end;
504 imsg_flush(ibuf);
506 while (!done) {
507 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
508 fatalx("imsg_read error");
509 if (n == 0)
510 fatalx("pipe closed");
512 while (!done) {
513 if ((n = imsg_get(ibuf, &imsg)) == -1)
514 fatalx("imsg_get error");
515 if (n == 0)
516 break;
518 switch (res->action) {
519 case ADD:
520 done = show_add(&imsg, &ret, &files);
521 break;
522 case SHOW:
523 done = show_complete(res, &imsg, &ret);
524 break;
525 case PLAY:
526 case TOGGLE:
527 case RESTART:
528 case STATUS:
529 case NEXT:
530 case PREV:
531 case JUMP:
532 done = show_status(&imsg, &ret);
533 break;
534 case LOAD:
535 done = show_load(res, &imsg, &ret);
536 break;
537 case MONITOR:
538 done = show_monitor(&imsg, &ret);
539 break;
540 default:
541 done = 1;
542 break;
545 imsg_free(&imsg);
549 end:
550 return ret;
553 int
554 ctl_noarg(struct parse_result *res, int argc, char **argv)
556 int ch;
558 while ((ch = getopt(argc, argv, "")) != -1)
559 ctl_usage(res->ctl);
560 argc -= optind;
561 argv += optind;
563 if (argc > 0)
564 ctl_usage(res->ctl);
566 return ctlaction(res);
569 int
570 ctl_add(struct parse_result *res, int argc, char **argv)
572 int ch;
574 while ((ch = getopt(argc, argv, "")) != -1)
575 ctl_usage(res->ctl);
576 argc -= optind;
577 argv += optind;
579 if (argc == 0)
580 ctl_usage(res->ctl);
581 res->files = argv;
583 if (pledge("stdio rpath", NULL) == -1)
584 fatal("pledge");
586 return ctlaction(res);
589 int
590 ctl_show(struct parse_result *res, int argc, char **argv)
592 int ch;
594 while ((ch = getopt(argc, argv, "p")) != -1) {
595 switch (ch) {
596 case 'p':
597 res->pretty = 1;
598 break;
599 default:
600 ctl_usage(res->ctl);
604 return ctlaction(res);
607 int
608 ctl_load(struct parse_result *res, int argc, char **argv)
610 int ch;
612 while ((ch = getopt(argc, argv, "")) != -1)
613 ctl_usage(res->ctl);
614 argc -= optind;
615 argv += optind;
617 if (argc == 0)
618 res->file = NULL;
619 else if (argc == 1)
620 res->file = argv[0];
621 else
622 ctl_usage(res->ctl);
624 if (pledge("stdio rpath", NULL) == -1)
625 fatal("pledge");
627 return ctlaction(res);
630 int
631 ctl_jump(struct parse_result *res, int argc, char **argv)
633 int ch;
635 while ((ch = getopt(argc, argv, "")) != -1)
636 ctl_usage(res->ctl);
637 argc -= optind;
638 argv += optind;
640 if (argc != 1)
641 ctl_usage(res->ctl);
643 res->file = argv[0];
644 return ctlaction(res);
647 int
648 ctl_repeat(struct parse_result *res, int argc, char **argv)
650 int ch, b;
652 while ((ch = getopt(argc, argv, "")) != -1)
653 ctl_usage(res->ctl);
654 argc -= optind;
655 argv += optind;
657 if (argc != 2)
658 ctl_usage(res->ctl);
660 if (!strcmp(argv[1], "on"))
661 b = 1;
662 else if (!strcmp(argv[1], "off"))
663 b = 0;
664 else
665 ctl_usage(res->ctl);
667 res->rep.repeat_one = -1;
668 res->rep.repeat_all = -1;
669 if (!strcmp(argv[0], "one"))
670 res->rep.repeat_one = b;
671 else if (!strcmp(argv[0], "all"))
672 res->rep.repeat_all = b;
673 else
674 ctl_usage(res->ctl);
676 return ctlaction(res);
679 static int
680 ctl_get_lock(const char *lockfile)
682 int lockfd;
684 if ((lockfd = open(lockfile, O_WRONLY|O_CREAT, 0600)) == -1) {
685 log_debug("open failed: %s", strerror(errno));
686 return -1;
689 if (flock(lockfd, LOCK_EX|LOCK_NB) == -1) {
690 log_debug("flock failed: %s", strerror(errno));
691 if (errno != EAGAIN)
692 return -1;
693 while (flock(lockfd, LOCK_EX) == -1 && errno == EINTR)
694 /* nop */;
695 close(lockfd);
696 return -2;
698 log_debug("flock succeeded");
700 return lockfd;
703 static int
704 ctl_connect(void)
706 struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
707 struct sockaddr_un sa;
708 size_t size;
709 int fd, lockfd = -1, locked = 0, spawned = 0;
710 char *lockfile = NULL;
712 memset(&sa, 0, sizeof(sa));
713 sa.sun_family = AF_UNIX;
714 size = strlcpy(sa.sun_path, csock, sizeof(sa.sun_path));
715 if (size >= sizeof(sa.sun_path)) {
716 errno = ENAMETOOLONG;
717 return -1;
720 retry:
721 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
722 return -1;
724 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
725 log_debug("connection failed: %s", strerror(errno));
726 if (errno != ECONNREFUSED && errno != ENOENT)
727 goto failed;
728 close(fd);
730 if (!locked) {
731 xasprintf(&lockfile, "%s.lock", csock);
732 if ((lockfd = ctl_get_lock(lockfile)) < 0) {
733 log_debug("didn't get the lock (%d)", lockfd);
735 free(lockfile);
736 lockfile = NULL;
738 if (lockfd == -1)
739 goto retry;
742 /*
743 * Always retry at least once, even if we got
744 * the lock, because another client could have
745 * taken the lock, started the server and released
746 * the lock between our connect() and flock()
747 */
748 locked = 1;
749 goto retry;
752 if (!spawned) {
753 log_debug("spawning the daemon");
754 spawn_daemon();
755 spawned = 1;
758 nanosleep(&ts, NULL);
759 goto retry;
762 if (locked && lockfd >= 0) {
763 unlink(lockfile);
764 free(lockfile);
765 close(lockfd);
767 return fd;
769 failed:
770 if (locked) {
771 free(lockfile);
772 close(lockfd);
774 close(fd);
775 return -1;
778 __dead void
779 ctl(int argc, char **argv)
781 int ctl_sock;
783 log_init(1, LOG_DAEMON);
784 log_setverbose(verbose);
786 if ((ctl_sock = ctl_connect()) == -1)
787 fatal("can't connect");
789 if (ctl_sock == -1)
790 fatalx("failed to connect to the daemon");
792 ibuf = xmalloc(sizeof(*ibuf));
793 imsg_init(ibuf, ctl_sock);
795 optreset = 1;
796 optind = 1;
798 exit(parse(argc, argv));