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 -- repeat 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 if (argc != 1)
557 ctl_usage(res->ctl);
558 return ctlaction(res);
561 int
562 ctl_add(struct parse_result *res, int argc, char **argv)
564 if (argc < 2)
565 ctl_usage(res->ctl);
566 res->files = argv+1;
568 if (pledge("stdio rpath", NULL) == -1)
569 fatal("pledge");
571 return ctlaction(res);
574 int
575 ctl_show(struct parse_result *res, int argc, char **argv)
577 int ch;
579 while ((ch = getopt(argc, argv, "p")) != -1) {
580 switch (ch) {
581 case 'p':
582 res->pretty = 1;
583 break;
584 default:
585 ctl_usage(res->ctl);
589 return ctlaction(res);
592 int
593 ctl_load(struct parse_result *res, int argc, char **argv)
595 if (argc < 2)
596 res->file = NULL;
597 else if (argc == 2)
598 res->file = argv[1];
599 else
600 ctl_usage(res->ctl);
602 if (pledge("stdio rpath", NULL) == -1)
603 fatal("pledge");
605 return ctlaction(res);
608 int
609 ctl_jump(struct parse_result *res, int argc, char **argv)
611 int ch;
613 while ((ch = getopt(argc, argv, "")) != -1)
614 ctl_usage(res->ctl);
615 argc -= optind;
616 argv += optind;
618 if (argc != 1)
619 ctl_usage(res->ctl);
621 res->file = argv[0];
622 return ctlaction(res);
625 int
626 ctl_repeat(struct parse_result *res, int argc, char **argv)
628 int ch, b;
630 while ((ch = getopt(argc, argv, "")) != -1)
631 ctl_usage(res->ctl);
632 argc -= optind;
633 argv += optind;
635 if (argc != 2)
636 ctl_usage(res->ctl);
638 if (!strcmp(argv[1], "on"))
639 b = 1;
640 else if (!strcmp(argv[1], "off"))
641 b = 0;
642 else
643 ctl_usage(res->ctl);
645 res->rep.repeat_one = -1;
646 res->rep.repeat_all = -1;
647 if (!strcmp(argv[0], "one"))
648 res->rep.repeat_one = b;
649 else if (!strcmp(argv[0], "all"))
650 res->rep.repeat_all = b;
651 else
652 ctl_usage(res->ctl);
654 return ctlaction(res);
657 static int
658 ctl_get_lock(const char *lockfile)
660 int lockfd;
662 if ((lockfd = open(lockfile, O_WRONLY|O_CREAT, 0600)) == -1) {
663 log_debug("open failed: %s", strerror(errno));
664 return -1;
667 if (flock(lockfd, LOCK_EX|LOCK_NB) == -1) {
668 log_debug("flock failed: %s", strerror(errno));
669 if (errno != EAGAIN)
670 return -1;
671 while (flock(lockfd, LOCK_EX) == -1 && errno == EINTR)
672 /* nop */;
673 close(lockfd);
674 return -2;
676 log_debug("flock succeeded");
678 return lockfd;
681 static int
682 ctl_connect(void)
684 struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
685 struct sockaddr_un sa;
686 size_t size;
687 int fd, lockfd = -1, locked = 0, spawned = 0;
688 char *lockfile = NULL;
690 memset(&sa, 0, sizeof(sa));
691 sa.sun_family = AF_UNIX;
692 size = strlcpy(sa.sun_path, csock, sizeof(sa.sun_path));
693 if (size >= sizeof(sa.sun_path)) {
694 errno = ENAMETOOLONG;
695 return -1;
698 retry:
699 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
700 return -1;
702 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
703 log_debug("connection failed: %s", strerror(errno));
704 if (errno != ECONNREFUSED && errno != ENOENT)
705 goto failed;
706 close(fd);
708 if (!locked) {
709 xasprintf(&lockfile, "%s.lock", csock);
710 if ((lockfd = ctl_get_lock(lockfile)) < 0) {
711 log_debug("didn't get the lock (%d)", lockfd);
713 free(lockfile);
714 lockfile = NULL;
716 if (lockfd == -1)
717 goto retry;
720 /*
721 * Always retry at least once, even if we got
722 * the lock, because another client could have
723 * taken the lock, started the server and released
724 * the lock between our connect() and flock()
725 */
726 locked = 1;
727 goto retry;
730 if (!spawned) {
731 log_debug("spawning the daemon");
732 spawn_daemon();
733 spawned = 1;
736 nanosleep(&ts, NULL);
737 goto retry;
740 if (locked && lockfd >= 0) {
741 unlink(lockfile);
742 free(lockfile);
743 close(lockfd);
745 return fd;
747 failed:
748 if (locked) {
749 free(lockfile);
750 close(lockfd);
752 close(fd);
753 return -1;
756 __dead void
757 ctl(int argc, char **argv)
759 int ctl_sock;
761 log_init(1, LOG_DAEMON);
762 log_setverbose(verbose);
764 if ((ctl_sock = ctl_connect()) == -1)
765 fatal("can't connect");
767 if (ctl_sock == -1)
768 fatalx("failed to connect to the daemon");
770 ibuf = xmalloc(sizeof(*ibuf));
771 imsg_init(ibuf, ctl_sock);
773 optreset = 1;
774 optind = 1;
776 exit(parse(argc, argv));