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];
276 size_t linesize = 0, i = 0;
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 while ((linelen = getline(&line, &linesize, f)) != -1) {
303 if (linelen == 0)
304 continue;
305 line[linelen-1] = '\0';
306 file = line;
307 if (file[0] == '>' && file[1] == ' ') {
308 file += 2;
309 curr = i;
311 if (file[0] == ' ' && file[1] == ' ')
312 file += 2;
314 memset(&path, 0, sizeof(path));
315 if (realpath(file, path) == NULL) {
316 log_warn("realpath %s", file);
317 continue;
320 i++;
321 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
322 path, sizeof(path));
325 free(line);
326 if (ferror(f))
327 fatal("getline");
328 fclose(f);
330 if (i == 0) {
331 *ret = 1;
332 return 1;
335 imsg_compose(ibuf, IMSG_CTL_COMMIT, 0, 0, -1,
336 &curr, sizeof(curr));
337 imsg_flush(ibuf);
338 return 0;
341 static int
342 show_monitor(struct imsg *imsg, int *ret)
344 int type;
346 if (imsg->hdr.type != IMSG_CTL_MONITOR) {
347 log_warnx("wrong message type received: %d",
348 imsg->hdr.type);
349 *ret = 1;
350 return 1;
353 if (IMSG_DATA_SIZE(*imsg) != sizeof(type)) {
354 log_warnx("size mismatch");
355 *ret = 1;
356 return 1;
359 memcpy(&type, imsg->data, sizeof(type));
360 switch (type) {
361 case IMSG_CTL_PLAY:
362 puts("play");
363 break;
364 case IMSG_CTL_TOGGLE_PLAY:
365 puts("toggle");
366 break;
367 case IMSG_CTL_PAUSE:
368 puts("pause");
369 break;
370 case IMSG_CTL_STOP:
371 puts("stop");
372 break;
373 case IMSG_CTL_RESTART:
374 puts("restart");
375 break;
376 case IMSG_CTL_FLUSH:
377 puts("flush");
378 break;
379 case IMSG_CTL_NEXT:
380 puts("next");
381 break;
382 case IMSG_CTL_PREV:
383 puts("prev");
384 break;
385 case IMSG_CTL_JUMP:
386 puts("jump");
387 break;
388 case IMSG_CTL_REPEAT:
389 puts("repeat");
390 break;
391 case IMSG_CTL_ADD:
392 puts("add");
393 break;
394 case IMSG_CTL_COMMIT:
395 puts("load");
396 break;
397 default:
398 puts("unknown");
399 break;
402 fflush(stdout);
403 return 0;
406 static int
407 ctlaction(struct parse_result *res)
409 struct imsg imsg;
410 ssize_t n;
411 int ret = 0, done = 1;
412 char **files;
414 switch (res->action) {
415 case PLAY:
416 imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
417 if (verbose) {
418 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
419 NULL, 0);
420 done = 0;
422 break;
423 case PAUSE:
424 imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
425 break;
426 case TOGGLE:
427 imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
428 if (verbose) {
429 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
430 NULL, 0);
431 done = 0;
433 break;
434 case STOP:
435 imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
436 break;
437 case RESTART:
438 imsg_compose(ibuf, IMSG_CTL_RESTART, 0, 0, -1, NULL, 0);
439 if (verbose) {
440 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
441 NULL, 0);
442 done = 0;
444 break;
445 case ADD:
446 done = 0;
447 files = res->files;
448 ret = enqueue_tracks(res->files);
449 break;
450 case FLUSH:
451 imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
452 break;
453 case SHOW:
454 done = 0;
455 imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
456 break;
457 case STATUS:
458 done = 0;
459 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
460 break;
461 case NEXT:
462 imsg_compose(ibuf, IMSG_CTL_NEXT, 0, 0, -1, NULL, 0);
463 if (verbose) {
464 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
465 NULL, 0);
466 done = 0;
468 break;
469 case PREV:
470 imsg_compose(ibuf, IMSG_CTL_PREV, 0, 0, -1, NULL, 0);
471 if (verbose) {
472 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
473 NULL, 0);
474 done = 0;
476 break;
477 case LOAD:
478 done = 0;
479 imsg_compose(ibuf, IMSG_CTL_BEGIN, 0, 0, -1, NULL, 0);
480 break;
481 case JUMP:
482 done = 0;
483 ret = jump_req(res->file);
484 break;
485 case REPEAT:
486 imsg_compose(ibuf, IMSG_CTL_REPEAT, 0, 0, -1,
487 &res->rep, sizeof(res->rep));
488 break;
489 case MONITOR:
490 done = 0;
491 imsg_compose(ibuf, IMSG_CTL_MONITOR, 0, 0, -1,
492 NULL, 0);
493 break;
494 case NONE:
495 /* action not expected */
496 fatalx("invalid action %u", res->action);
497 break;
500 if (ret != 0)
501 goto end;
503 imsg_flush(ibuf);
505 while (!done) {
506 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
507 fatalx("imsg_read error");
508 if (n == 0)
509 fatalx("pipe closed");
511 while (!done) {
512 if ((n = imsg_get(ibuf, &imsg)) == -1)
513 fatalx("imsg_get error");
514 if (n == 0)
515 break;
517 switch (res->action) {
518 case ADD:
519 done = show_add(&imsg, &ret, &files);
520 break;
521 case SHOW:
522 done = show_complete(res, &imsg, &ret);
523 break;
524 case PLAY:
525 case TOGGLE:
526 case RESTART:
527 case STATUS:
528 case NEXT:
529 case PREV:
530 case JUMP:
531 done = show_status(&imsg, &ret);
532 break;
533 case LOAD:
534 done = show_load(res, &imsg, &ret);
535 break;
536 case MONITOR:
537 done = show_monitor(&imsg, &ret);
538 break;
539 default:
540 done = 1;
541 break;
544 imsg_free(&imsg);
548 end:
549 return ret;
552 int
553 ctl_noarg(struct parse_result *res, int argc, char **argv)
555 int ch;
557 while ((ch = getopt(argc, argv, "")) != -1)
558 ctl_usage(res->ctl);
559 argc -= optind;
560 argv += optind;
562 if (argc > 0)
563 ctl_usage(res->ctl);
565 return ctlaction(res);
568 int
569 ctl_add(struct parse_result *res, int argc, char **argv)
571 int ch;
573 while ((ch = getopt(argc, argv, "")) != -1)
574 ctl_usage(res->ctl);
575 argc -= optind;
576 argv += optind;
578 if (argc == 0)
579 ctl_usage(res->ctl);
580 res->files = argv;
582 if (pledge("stdio rpath", NULL) == -1)
583 fatal("pledge");
585 return ctlaction(res);
588 int
589 ctl_show(struct parse_result *res, int argc, char **argv)
591 int ch;
593 while ((ch = getopt(argc, argv, "p")) != -1) {
594 switch (ch) {
595 case 'p':
596 res->pretty = 1;
597 break;
598 default:
599 ctl_usage(res->ctl);
603 return ctlaction(res);
606 int
607 ctl_load(struct parse_result *res, int argc, char **argv)
609 int ch;
611 while ((ch = getopt(argc, argv, "")) != -1)
612 ctl_usage(res->ctl);
613 argc -= optind;
614 argv += optind;
616 if (argc == 0)
617 res->file = NULL;
618 else if (argc == 1)
619 res->file = argv[0];
620 else
621 ctl_usage(res->ctl);
623 if (pledge("stdio rpath", NULL) == -1)
624 fatal("pledge");
626 return ctlaction(res);
629 int
630 ctl_jump(struct parse_result *res, int argc, char **argv)
632 int ch;
634 while ((ch = getopt(argc, argv, "")) != -1)
635 ctl_usage(res->ctl);
636 argc -= optind;
637 argv += optind;
639 if (argc != 1)
640 ctl_usage(res->ctl);
642 res->file = argv[0];
643 return ctlaction(res);
646 int
647 ctl_repeat(struct parse_result *res, int argc, char **argv)
649 int ch, b;
651 while ((ch = getopt(argc, argv, "")) != -1)
652 ctl_usage(res->ctl);
653 argc -= optind;
654 argv += optind;
656 if (argc != 2)
657 ctl_usage(res->ctl);
659 if (!strcmp(argv[1], "on"))
660 b = 1;
661 else if (!strcmp(argv[1], "off"))
662 b = 0;
663 else
664 ctl_usage(res->ctl);
666 res->rep.repeat_one = -1;
667 res->rep.repeat_all = -1;
668 if (!strcmp(argv[0], "one"))
669 res->rep.repeat_one = b;
670 else if (!strcmp(argv[0], "all"))
671 res->rep.repeat_all = b;
672 else
673 ctl_usage(res->ctl);
675 return ctlaction(res);
678 static int
679 ctl_get_lock(const char *lockfile)
681 int lockfd;
683 if ((lockfd = open(lockfile, O_WRONLY|O_CREAT, 0600)) == -1) {
684 log_debug("open failed: %s", strerror(errno));
685 return -1;
688 if (flock(lockfd, LOCK_EX|LOCK_NB) == -1) {
689 log_debug("flock failed: %s", strerror(errno));
690 if (errno != EAGAIN)
691 return -1;
692 while (flock(lockfd, LOCK_EX) == -1 && errno == EINTR)
693 /* nop */;
694 close(lockfd);
695 return -2;
697 log_debug("flock succeeded");
699 return lockfd;
702 static int
703 ctl_connect(void)
705 struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
706 struct sockaddr_un sa;
707 size_t size;
708 int fd, lockfd = -1, locked = 0, spawned = 0;
709 char *lockfile = NULL;
711 memset(&sa, 0, sizeof(sa));
712 sa.sun_family = AF_UNIX;
713 size = strlcpy(sa.sun_path, csock, sizeof(sa.sun_path));
714 if (size >= sizeof(sa.sun_path)) {
715 errno = ENAMETOOLONG;
716 return -1;
719 retry:
720 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
721 return -1;
723 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
724 log_debug("connection failed: %s", strerror(errno));
725 if (errno != ECONNREFUSED && errno != ENOENT)
726 goto failed;
727 close(fd);
729 if (!locked) {
730 xasprintf(&lockfile, "%s.lock", csock);
731 if ((lockfd = ctl_get_lock(lockfile)) < 0) {
732 log_debug("didn't get the lock (%d)", lockfd);
734 free(lockfile);
735 lockfile = NULL;
737 if (lockfd == -1)
738 goto retry;
741 /*
742 * Always retry at least once, even if we got
743 * the lock, because another client could have
744 * taken the lock, started the server and released
745 * the lock between our connect() and flock()
746 */
747 locked = 1;
748 goto retry;
751 if (!spawned) {
752 log_debug("spawning the daemon");
753 spawn_daemon();
754 spawned = 1;
757 nanosleep(&ts, NULL);
758 goto retry;
761 if (locked && lockfd >= 0) {
762 unlink(lockfile);
763 free(lockfile);
764 close(lockfd);
766 return fd;
768 failed:
769 if (locked) {
770 free(lockfile);
771 close(lockfd);
773 close(fd);
774 return -1;
777 __dead void
778 ctl(int argc, char **argv)
780 int ctl_sock;
782 log_init(1, LOG_DAEMON);
783 log_setverbose(verbose);
785 if ((ctl_sock = ctl_connect()) == -1)
786 fatal("can't connect");
788 if (ctl_sock == -1)
789 fatalx("failed to connect to the daemon");
791 ibuf = xmalloc(sizeof(*ibuf));
792 imsg_init(ibuf, ctl_sock);
794 optreset = 1;
795 optind = 1;
797 exit(parse(argc, argv));