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 **);
48 int ctl_monitor(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_monitor, "[events]" },
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], cwd[PATH_MAX];
277 size_t linesize = 0, i = 0, n;
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 if (getcwd(cwd, sizeof(cwd)) == NULL)
304 fatal("getcwd");
306 while ((linelen = getline(&line, &linesize, f)) != -1) {
307 if (linelen == 0)
308 continue;
309 line[linelen-1] = '\0';
310 file = line;
311 if (!strncmp(file, "> ", 2)) {
312 file += 2;
313 curr = i;
314 } else if (!strncmp(file, " ", 2))
315 file += 2;
317 if (!strncmp(file, "./", 2))
318 file += 2;
320 memset(path, 0, sizeof(path));
321 if (*file == '/')
322 n = strlcpy(path, file, sizeof(path));
323 else
324 n = snprintf(path, sizeof(path), "%s/%s", cwd, file);
326 if (n >= sizeof(path)) {
327 log_warnx("path too long: %s", file);
328 continue;
331 i++;
332 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
333 path, sizeof(path));
336 free(line);
337 if (ferror(f))
338 fatal("getline");
339 fclose(f);
341 if (i == 0) {
342 *ret = 1;
343 return 1;
346 imsg_compose(ibuf, IMSG_CTL_COMMIT, 0, 0, -1,
347 &curr, sizeof(curr));
348 imsg_flush(ibuf);
349 return 0;
352 static int
353 show_monitor(struct parse_result *res, struct imsg *imsg, int *ret)
355 int type;
357 if (imsg->hdr.type != IMSG_CTL_MONITOR) {
358 log_warnx("wrong message type received: %d",
359 imsg->hdr.type);
360 *ret = 1;
361 return 1;
364 if (IMSG_DATA_SIZE(*imsg) != sizeof(type)) {
365 log_warnx("size mismatch");
366 *ret = 1;
367 return 1;
370 memcpy(&type, imsg->data, sizeof(type));
371 if (type < 0 || type > IMSG__LAST) {
372 log_warnx("wrong monitor type received");
373 *ret = 1;
374 return 1;
377 if (!res->monitor[type])
378 return 0;
380 switch (type) {
381 case IMSG_CTL_PLAY:
382 puts("play");
383 break;
384 case IMSG_CTL_TOGGLE_PLAY:
385 puts("toggle");
386 break;
387 case IMSG_CTL_PAUSE:
388 puts("pause");
389 break;
390 case IMSG_CTL_STOP:
391 puts("stop");
392 break;
393 case IMSG_CTL_RESTART:
394 puts("restart");
395 break;
396 case IMSG_CTL_FLUSH:
397 puts("flush");
398 break;
399 case IMSG_CTL_NEXT:
400 puts("next");
401 break;
402 case IMSG_CTL_PREV:
403 puts("prev");
404 break;
405 case IMSG_CTL_JUMP:
406 puts("jump");
407 break;
408 case IMSG_CTL_REPEAT:
409 puts("repeat");
410 break;
411 case IMSG_CTL_ADD:
412 puts("add");
413 break;
414 case IMSG_CTL_COMMIT:
415 puts("load");
416 break;
417 default:
418 puts("unknown");
419 break;
422 fflush(stdout);
423 return 0;
426 static int
427 ctlaction(struct parse_result *res)
429 struct imsg imsg;
430 ssize_t n;
431 int ret = 0, done = 1;
432 char **files;
434 switch (res->action) {
435 case PLAY:
436 imsg_compose(ibuf, IMSG_CTL_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 PAUSE:
444 imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
445 break;
446 case TOGGLE:
447 imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 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 STOP:
455 imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
456 break;
457 case RESTART:
458 imsg_compose(ibuf, IMSG_CTL_RESTART, 0, 0, -1, NULL, 0);
459 if (verbose) {
460 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
461 NULL, 0);
462 done = 0;
464 break;
465 case ADD:
466 done = 0;
467 files = res->files;
468 ret = enqueue_tracks(res->files);
469 break;
470 case FLUSH:
471 imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
472 break;
473 case SHOW:
474 done = 0;
475 imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
476 break;
477 case STATUS:
478 done = 0;
479 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
480 break;
481 case NEXT:
482 imsg_compose(ibuf, IMSG_CTL_NEXT, 0, 0, -1, NULL, 0);
483 if (verbose) {
484 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
485 NULL, 0);
486 done = 0;
488 break;
489 case PREV:
490 imsg_compose(ibuf, IMSG_CTL_PREV, 0, 0, -1, NULL, 0);
491 if (verbose) {
492 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
493 NULL, 0);
494 done = 0;
496 break;
497 case LOAD:
498 done = 0;
499 imsg_compose(ibuf, IMSG_CTL_BEGIN, 0, 0, -1, NULL, 0);
500 break;
501 case JUMP:
502 done = 0;
503 ret = jump_req(res->file);
504 break;
505 case REPEAT:
506 imsg_compose(ibuf, IMSG_CTL_REPEAT, 0, 0, -1,
507 &res->rep, sizeof(res->rep));
508 break;
509 case MONITOR:
510 done = 0;
511 imsg_compose(ibuf, IMSG_CTL_MONITOR, 0, 0, -1,
512 NULL, 0);
513 break;
514 case NONE:
515 /* action not expected */
516 fatalx("invalid action %u", res->action);
517 break;
520 if (ret != 0)
521 goto end;
523 imsg_flush(ibuf);
525 while (!done) {
526 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
527 fatalx("imsg_read error");
528 if (n == 0)
529 fatalx("pipe closed");
531 while (!done) {
532 if ((n = imsg_get(ibuf, &imsg)) == -1)
533 fatalx("imsg_get error");
534 if (n == 0)
535 break;
537 switch (res->action) {
538 case ADD:
539 done = show_add(&imsg, &ret, &files);
540 break;
541 case SHOW:
542 done = show_complete(res, &imsg, &ret);
543 break;
544 case PLAY:
545 case TOGGLE:
546 case RESTART:
547 case STATUS:
548 case NEXT:
549 case PREV:
550 case JUMP:
551 done = show_status(&imsg, &ret);
552 break;
553 case LOAD:
554 done = show_load(res, &imsg, &ret);
555 break;
556 case MONITOR:
557 done = show_monitor(res, &imsg, &ret);
558 break;
559 default:
560 done = 1;
561 break;
564 imsg_free(&imsg);
568 end:
569 return ret;
572 int
573 ctl_noarg(struct parse_result *res, int argc, char **argv)
575 int ch;
577 while ((ch = getopt(argc, argv, "")) != -1)
578 ctl_usage(res->ctl);
579 argc -= optind;
580 argv += optind;
582 if (argc > 0)
583 ctl_usage(res->ctl);
585 return ctlaction(res);
588 int
589 ctl_add(struct parse_result *res, int argc, char **argv)
591 int ch;
593 while ((ch = getopt(argc, argv, "")) != -1)
594 ctl_usage(res->ctl);
595 argc -= optind;
596 argv += optind;
598 if (argc == 0)
599 ctl_usage(res->ctl);
600 res->files = argv;
602 if (pledge("stdio rpath", NULL) == -1)
603 fatal("pledge");
605 return ctlaction(res);
608 int
609 ctl_show(struct parse_result *res, int argc, char **argv)
611 int ch;
613 while ((ch = getopt(argc, argv, "p")) != -1) {
614 switch (ch) {
615 case 'p':
616 res->pretty = 1;
617 break;
618 default:
619 ctl_usage(res->ctl);
623 return ctlaction(res);
626 int
627 ctl_load(struct parse_result *res, int argc, char **argv)
629 int ch;
631 while ((ch = getopt(argc, argv, "")) != -1)
632 ctl_usage(res->ctl);
633 argc -= optind;
634 argv += optind;
636 if (argc == 0)
637 res->file = NULL;
638 else if (argc == 1)
639 res->file = argv[0];
640 else
641 ctl_usage(res->ctl);
643 if (pledge("stdio rpath", NULL) == -1)
644 fatal("pledge");
646 return ctlaction(res);
649 int
650 ctl_jump(struct parse_result *res, int argc, char **argv)
652 int ch;
654 while ((ch = getopt(argc, argv, "")) != -1)
655 ctl_usage(res->ctl);
656 argc -= optind;
657 argv += optind;
659 if (argc != 1)
660 ctl_usage(res->ctl);
662 res->file = argv[0];
663 return ctlaction(res);
666 int
667 ctl_repeat(struct parse_result *res, int argc, char **argv)
669 int ch, b;
671 while ((ch = getopt(argc, argv, "")) != -1)
672 ctl_usage(res->ctl);
673 argc -= optind;
674 argv += optind;
676 if (argc != 2)
677 ctl_usage(res->ctl);
679 if (!strcmp(argv[1], "on"))
680 b = 1;
681 else if (!strcmp(argv[1], "off"))
682 b = 0;
683 else
684 ctl_usage(res->ctl);
686 res->rep.repeat_one = -1;
687 res->rep.repeat_all = -1;
688 if (!strcmp(argv[0], "one"))
689 res->rep.repeat_one = b;
690 else if (!strcmp(argv[0], "all"))
691 res->rep.repeat_all = b;
692 else
693 ctl_usage(res->ctl);
695 return ctlaction(res);
698 int
699 ctl_monitor(struct parse_result *res, int argc, char **argv)
701 int ch;
702 const char *events;
703 char *dup, *tmp, *tok;
705 while ((ch = getopt(argc, argv, "")) != -1)
706 ctl_usage(res->ctl);
707 argc -= optind;
708 argv += optind;
710 if (argc > 1)
711 ctl_usage(res->ctl);
713 if (argc == 1)
714 events = *argv;
715 else
716 events = "play,toggle,pause,stop,restart,flush,next,prev,"
717 "jump,repeat,add,load";
719 tmp = dup = xstrdup(events);
720 while ((tok = strsep(&tmp, ",")) != NULL) {
721 if (*tok == '\0')
722 continue;
724 if (!strcmp(tok, "play"))
725 res->monitor[IMSG_CTL_PLAY] = 1;
726 else if (!strcmp(tok, "toggle"))
727 res->monitor[IMSG_CTL_TOGGLE_PLAY] = 1;
728 else if (!strcmp(tok, "pause"))
729 res->monitor[IMSG_CTL_PAUSE] = 1;
730 else if (!strcmp(tok, "stop"))
731 res->monitor[IMSG_CTL_STOP] = 1;
732 else if (!strcmp(tok, "restart"))
733 res->monitor[IMSG_CTL_RESTART] = 1;
734 else if (!strcmp(tok, "flush"))
735 res->monitor[IMSG_CTL_FLUSH] = 1;
736 else if (!strcmp(tok, "next"))
737 res->monitor[IMSG_CTL_NEXT] = 1;
738 else if (!strcmp(tok, "prev"))
739 res->monitor[IMSG_CTL_PREV] = 1;
740 else if (!strcmp(tok, "jump"))
741 res->monitor[IMSG_CTL_JUMP] = 1;
742 else if (!strcmp(tok, "repeat"))
743 res->monitor[IMSG_CTL_REPEAT] = 1;
744 else if (!strcmp(tok, "add"))
745 res->monitor[IMSG_CTL_ADD] = 1;
746 else if (!strcmp(tok, "load"))
747 res->monitor[IMSG_CTL_COMMIT] = 1;
748 else
749 fatalx("unknown event \"%s\"", tok);
752 free(dup);
753 return ctlaction(res);
756 static int
757 ctl_get_lock(const char *lockfile)
759 int lockfd;
761 if ((lockfd = open(lockfile, O_WRONLY|O_CREAT, 0600)) == -1) {
762 log_debug("open failed: %s", strerror(errno));
763 return -1;
766 if (flock(lockfd, LOCK_EX|LOCK_NB) == -1) {
767 log_debug("flock failed: %s", strerror(errno));
768 if (errno != EAGAIN)
769 return -1;
770 while (flock(lockfd, LOCK_EX) == -1 && errno == EINTR)
771 /* nop */;
772 close(lockfd);
773 return -2;
775 log_debug("flock succeeded");
777 return lockfd;
780 static int
781 ctl_connect(void)
783 struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
784 struct sockaddr_un sa;
785 size_t size;
786 int fd, lockfd = -1, locked = 0, spawned = 0;
787 char *lockfile = NULL;
789 memset(&sa, 0, sizeof(sa));
790 sa.sun_family = AF_UNIX;
791 size = strlcpy(sa.sun_path, csock, sizeof(sa.sun_path));
792 if (size >= sizeof(sa.sun_path)) {
793 errno = ENAMETOOLONG;
794 return -1;
797 retry:
798 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
799 return -1;
801 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
802 log_debug("connection failed: %s", strerror(errno));
803 if (errno != ECONNREFUSED && errno != ENOENT)
804 goto failed;
805 close(fd);
807 if (!locked) {
808 xasprintf(&lockfile, "%s.lock", csock);
809 if ((lockfd = ctl_get_lock(lockfile)) < 0) {
810 log_debug("didn't get the lock (%d)", lockfd);
812 free(lockfile);
813 lockfile = NULL;
815 if (lockfd == -1)
816 goto retry;
819 /*
820 * Always retry at least once, even if we got
821 * the lock, because another client could have
822 * taken the lock, started the server and released
823 * the lock between our connect() and flock()
824 */
825 locked = 1;
826 goto retry;
829 if (!spawned) {
830 log_debug("spawning the daemon");
831 spawn_daemon();
832 spawned = 1;
835 nanosleep(&ts, NULL);
836 goto retry;
839 if (locked && lockfd >= 0) {
840 unlink(lockfile);
841 free(lockfile);
842 close(lockfd);
844 return fd;
846 failed:
847 if (locked) {
848 free(lockfile);
849 close(lockfd);
851 close(fd);
852 return -1;
855 __dead void
856 ctl(int argc, char **argv)
858 int ctl_sock;
860 log_init(1, LOG_DAEMON);
861 log_setverbose(verbose);
863 if ((ctl_sock = ctl_connect()) == -1)
864 fatal("can't connect");
866 if (ctl_sock == -1)
867 fatalx("failed to connect to the daemon");
869 ibuf = xmalloc(sizeof(*ibuf));
870 imsg_init(ibuf, ctl_sock);
872 optreset = 1;
873 optind = 1;
875 exit(parse(argc, argv));