Blob


1 /*
2 * Copyright (c) 2022 Omar Polo <op@openbsd.org>
3 * Copyright (c) 2015 Theo de Raadt <deraadt@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include "config.h"
20 #include <sys/socket.h>
21 #include <sys/un.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <limits.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <stdint.h>
29 #include <string.h>
30 #include <syslog.h>
31 #include <unistd.h>
33 #include "amused.h"
34 #include "log.h"
35 #include "playlist.h"
36 #include "xmalloc.h"
38 static struct imsgbuf *ibuf;
39 char cwd[PATH_MAX];
41 static int ctl_noarg(struct parse_result *, int, char **);
42 static int ctl_add(struct parse_result *, int, char **);
43 static int ctl_show(struct parse_result *, int, char **);
44 static int ctl_load(struct parse_result *, int, char **);
45 static int ctl_jump(struct parse_result *, int, char **);
46 static int ctl_repeat(struct parse_result *, int, char **);
47 static int ctl_monitor(struct parse_result *, int, char **);
48 static int ctl_seek(struct parse_result *, int, char **);
50 struct ctl_command ctl_commands[] = {
51 { "add", ADD, ctl_add, "files...", 0 },
52 { "flush", FLUSH, ctl_noarg, "", 0 },
53 { "jump", JUMP, ctl_jump, "pattern", 0 },
54 { "load", LOAD, ctl_load, "[file]", 1 },
55 { "monitor", MONITOR, ctl_monitor, "[events]", 0 },
56 { "next", NEXT, ctl_noarg, "", 0 },
57 { "pause", PAUSE, ctl_noarg, "", 0 },
58 { "play", PLAY, ctl_noarg, "", 0 },
59 { "prev", PREV, ctl_noarg, "", 0 },
60 { "repeat", REPEAT, ctl_repeat, "one|all on|off", 0 },
61 { "restart", RESTART, ctl_noarg, "", 0 },
62 { "seek", SEEK, ctl_seek, "[+-]time[%]", 0 },
63 { "show", SHOW, ctl_show, "[-p]", 0 },
64 { "status", STATUS, ctl_noarg, "", 0 },
65 { "stop", STOP, ctl_noarg, "", 0 },
66 { "toggle", TOGGLE, ctl_noarg, "", 0 },
67 { NULL, 0, NULL, NULL, 0 },
68 };
70 __dead void
71 usage(void)
72 {
73 fprintf(stderr, "usage: %s [-dv] [-s socket]\n", getprogname());
74 exit(1);
75 }
77 static __dead void
78 ctl_usage(struct ctl_command *ctl)
79 {
80 fprintf(stderr, "usage: %s [-v] [-s socket] %s %s\n", getprogname(),
81 ctl->name, ctl->usage);
82 exit(1);
83 }
85 /* based on canonpath from kern_pledge.c */
86 static int
87 canonpath(const char *input, char *buf, size_t bufsize)
88 {
89 const char *p;
90 char *q, path[PATH_MAX];
92 if (input[0] != '/') {
93 if (snprintf(path, sizeof(path), "%s/%s", cwd, input)
94 >= sizeof(path)) {
95 errno = ENAMETOOLONG;
96 return -1;
97 }
98 input = path;
99 }
101 p = input;
102 q = buf;
103 while (*p && (q - buf < bufsize)) {
104 if (p[0] == '/' && (p[1] == '/' || p[1] == '\0')) {
105 p += 1;
107 } else if (p[0] == '/' && p[1] == '.' &&
108 (p[2] == '/' || p[2] == '\0')) {
109 p += 2;
111 } else if (p[0] == '/' && p[1] == '.' && p[2] == '.' &&
112 (p[3] == '/' || p[3] == '\0')) {
113 p += 3;
114 if (q != buf) /* "/../" at start of buf */
115 while (*--q != '/')
116 continue;
118 } else {
119 *q++ = *p++;
122 if ((*p == '\0') && (q - buf < bufsize)) {
123 *q = 0;
124 return 0;
125 } else {
126 errno = ENAMETOOLONG;
127 return -1;
131 static int
132 parse(int argc, char **argv)
134 struct ctl_command *ctl = NULL;
135 struct parse_result res;
136 const char *argv0;
137 int i, status;
139 memset(&res, 0, sizeof(res));
141 if ((argv0 = argv[0]) == NULL)
142 argv0 = "status";
144 for (i = 0; ctl_commands[i].name != NULL; ++i) {
145 if (strncmp(ctl_commands[i].name, argv0, strlen(argv0))
146 == 0) {
147 if (ctl != NULL) {
148 fprintf(stderr,
149 "ambiguous argument: %s\n", argv0);
150 usage();
152 ctl = &ctl_commands[i];
156 if (ctl == NULL) {
157 fprintf(stderr, "unknown argument: %s\n", argv[0]);
158 usage();
161 res.action = ctl->action;
162 res.ctl = ctl;
164 if (!ctl->has_pledge) {
165 /* pledge(2) default if command doesn't have its own */
166 if (pledge("stdio", NULL) == -1)
167 fatal("pledge");
170 status = ctl->main(&res, argc, argv);
171 close(ibuf->fd);
172 free(ibuf);
173 return status;
176 static int
177 load_files(struct parse_result *res, int *ret)
179 FILE *f;
180 const char *file;
181 char *line = NULL;
182 char path[PATH_MAX];
183 size_t linesize = 0, i = 0;
184 ssize_t linelen, curr = -1;
186 if (res->file == NULL)
187 f = stdin;
188 else if ((f = fopen(res->file, "r")) == NULL) {
189 log_warn("can't open %s", res->file);
190 *ret = 1;
191 return 1;
194 while ((linelen = getline(&line, &linesize, f)) != -1) {
195 if (linelen == 0)
196 continue;
197 line[linelen-1] = '\0';
198 file = line;
199 if (!strncmp(file, "> ", 2)) {
200 file += 2;
201 curr = i;
202 } else if (!strncmp(file, " ", 2))
203 file += 2;
205 memset(path, 0, sizeof(path));
206 if (canonpath(file, path, sizeof(path)) == -1) {
207 log_warn("canonpath %s", file);
208 continue;
211 i++;
212 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
213 path, sizeof(path));
216 free(line);
217 if (ferror(f))
218 fatal("getline");
219 fclose(f);
221 if (i == 0) {
222 *ret = 1;
223 return 1;
226 imsg_compose(ibuf, IMSG_CTL_COMMIT, 0, 0, -1,
227 &curr, sizeof(curr));
228 imsg_flush(ibuf);
229 return 0;
232 static const char *
233 imsg_strerror(struct imsg *imsg)
235 size_t datalen;
236 const char *msg;
238 datalen = IMSG_DATA_SIZE(*imsg);
239 msg = imsg->data;
240 if (datalen == 0 || msg[datalen-1] != '\0')
241 fatalx("malformed error message");
243 return msg;
246 static const char *
247 imsg_name(int type)
249 switch (type) {
250 case IMSG_CTL_PLAY:
251 return "play";
252 case IMSG_CTL_TOGGLE_PLAY:
253 return "toggle";
254 case IMSG_CTL_PAUSE:
255 return "pause";
256 case IMSG_CTL_STOP:
257 return "stop";
258 case IMSG_CTL_FLUSH:
259 return "flush";
260 case IMSG_CTL_NEXT:
261 return "next";
262 case IMSG_CTL_PREV:
263 return "prev";
264 case IMSG_CTL_JUMP:
265 return "jump";
266 case IMSG_CTL_REPEAT:
267 return "repeat";
268 case IMSG_CTL_ADD:
269 return "add";
270 case IMSG_CTL_COMMIT:
271 return "load";
272 default:
273 return "unknown";
277 static void
278 print_time(const char *label, int64_t seconds)
280 int hours, minutes;
282 if (seconds < 0)
283 seconds = 0;
285 hours = seconds / 3600;
286 seconds -= hours * 3600;
288 minutes = seconds / 60;
289 seconds -= minutes * 60;
291 printf("%s ", label);
292 if (hours != 0)
293 printf("%02d:", hours);
294 printf("%02d:%02lld\n", minutes, (long long)seconds);
297 static int
298 ctlaction(struct parse_result *res)
300 char path[PATH_MAX];
301 struct imsg imsg;
302 struct player_status ps;
303 size_t datalen;
304 ssize_t n;
305 int i, type, ret = 0, done = 1;
307 switch (res->action) {
308 case PLAY:
309 imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
310 if (verbose) {
311 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
312 NULL, 0);
313 done = 0;
315 break;
316 case PAUSE:
317 imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
318 break;
319 case TOGGLE:
320 imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
321 if (verbose) {
322 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
323 NULL, 0);
324 done = 0;
326 break;
327 case STOP:
328 imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
329 break;
330 case ADD:
331 done = 0;
332 for (i = 0; res->files[i] != NULL; ++i) {
333 memset(path, 0, sizeof(path));
334 if (canonpath(res->files[i], path, sizeof(path))
335 == -1) {
336 log_warn("canonpath %s", res->files[i]);
337 continue;
340 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
341 path, sizeof(path));
343 ret = i == 0;
344 break;
345 case FLUSH:
346 imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
347 break;
348 case SHOW:
349 done = 0;
350 imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
351 break;
352 case STATUS:
353 done = 0;
354 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
355 break;
356 case NEXT:
357 imsg_compose(ibuf, IMSG_CTL_NEXT, 0, 0, -1, NULL, 0);
358 if (verbose) {
359 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
360 NULL, 0);
361 done = 0;
363 break;
364 case PREV:
365 imsg_compose(ibuf, IMSG_CTL_PREV, 0, 0, -1, NULL, 0);
366 if (verbose) {
367 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
368 NULL, 0);
369 done = 0;
371 break;
372 case LOAD:
373 done = 0;
374 imsg_compose(ibuf, IMSG_CTL_BEGIN, 0, 0, -1, NULL, 0);
375 break;
376 case JUMP:
377 done = 0;
378 memset(path, 0, sizeof(path));
379 strlcpy(path, res->file, sizeof(path));
380 imsg_compose(ibuf, IMSG_CTL_JUMP, 0, 0, -1,
381 path, sizeof(path));
382 break;
383 case REPEAT:
384 imsg_compose(ibuf, IMSG_CTL_REPEAT, 0, 0, -1,
385 &res->rep, sizeof(res->rep));
386 break;
387 case MONITOR:
388 done = 0;
389 imsg_compose(ibuf, IMSG_CTL_MONITOR, 0, 0, -1,
390 NULL, 0);
391 break;
392 case RESTART:
393 memset(&res->seek, 0, sizeof(res->seek));
394 /* fallthrough */
395 case SEEK:
396 imsg_compose(ibuf, IMSG_CTL_SEEK, 0, 0, -1, &res->seek,
397 sizeof(res->seek));
398 break;
399 case NONE:
400 /* action not expected */
401 fatalx("invalid action %u", res->action);
402 break;
405 if (ret != 0)
406 goto end;
408 imsg_flush(ibuf);
410 i = 0;
411 while (!done) {
412 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
413 fatalx("imsg_read error");
414 if (n == 0)
415 fatalx("pipe closed");
417 while (!done) {
418 if ((n = imsg_get(ibuf, &imsg)) == -1)
419 fatalx("imsg_get error");
420 if (n == 0)
421 break;
423 if (imsg.hdr.type == IMSG_CTL_ERR) {
424 log_warnx("%s: %s", res->ctl->name,
425 imsg_strerror(&imsg));
426 ret = 1;
427 done = 1;
428 break;
431 datalen = IMSG_DATA_SIZE(imsg);
433 switch (res->action) {
434 case ADD:
435 if (res->files[i] == NULL)
436 fatalx("received more replies than "
437 "files enqueued.");
439 if (imsg.hdr.type == IMSG_CTL_ADD)
440 log_debug("enqueued %s", res->files[i]);
441 else
442 fatalx("invalid message %d",
443 imsg.hdr.type);
444 i++;
445 done = res->files[i] == NULL;
446 break;
447 case SHOW:
448 if (datalen == 0) {
449 done = 1;
450 break;
452 if (datalen != sizeof(ps))
453 fatalx("data size mismatch");
454 memcpy(&ps, imsg.data, sizeof(ps));
455 if (ps.path[sizeof(ps.path) - 1] != '\0')
456 fatalx("received corrupted data");
457 if (res->pretty) {
458 char c = ' ';
459 if (ps.status == STATE_PLAYING)
460 c = '>';
461 printf("%c ", c);
463 puts(ps.path);
464 break;
465 case PLAY:
466 case TOGGLE:
467 case RESTART:
468 case STATUS:
469 case NEXT:
470 case PREV:
471 case JUMP:
472 if (imsg.hdr.type != IMSG_CTL_STATUS)
473 fatalx("invalid message %d",
474 imsg.hdr.type);
476 if (datalen != sizeof(ps))
477 fatalx("data size mismatch");
478 memcpy(&ps, imsg.data, sizeof(ps));
479 if (ps.path[sizeof(ps.path) - 1] != '\0')
480 fatalx("received corrupted data");
482 if (ps.status == STATE_STOPPED)
483 printf("stopped ");
484 else if (ps.status == STATE_PLAYING)
485 printf("playing ");
486 else if (ps.status == STATE_PAUSED)
487 printf("paused ");
488 else
489 printf("unknown ");
491 puts(ps.path);
493 print_time("position", ps.position);
494 print_time("duration", ps.duration);
496 printf("repeat one %s\nrepeat all %s\n",
497 ps.rp.repeat_one ? "on" : "off",
498 ps.rp.repeat_all ? "on" : "off");
500 done = 1;
501 break;
502 case LOAD:
503 if (imsg.hdr.type == IMSG_CTL_ADD)
504 break;
505 if (imsg.hdr.type == IMSG_CTL_COMMIT) {
506 done = 1;
507 break;
510 if (imsg.hdr.type != IMSG_CTL_BEGIN)
511 fatalx("invalid message %d",
512 imsg.hdr.type);
514 load_files(res, &ret);
515 break;
516 case MONITOR:
517 if (imsg.hdr.type != IMSG_CTL_MONITOR)
518 fatalx("invalid message %d",
519 imsg.hdr.type);
521 if (datalen != sizeof(type))
522 fatalx("data size mismatch");
524 memcpy(&type, imsg.data, sizeof(type));
525 if (type < 0 || type > IMSG__LAST)
526 fatalx("received corrupted data");
528 if (!res->monitor[type])
529 break;
531 puts(imsg_name(type));
532 fflush(stdout);
533 break;
534 default:
535 done = 1;
536 break;
539 imsg_free(&imsg);
543 end:
544 return ret;
547 static int
548 ctl_noarg(struct parse_result *res, int argc, char **argv)
550 int ch;
552 while ((ch = getopt(argc, argv, "")) != -1)
553 ctl_usage(res->ctl);
554 argc -= optind;
555 argv += optind;
557 if (argc > 0)
558 ctl_usage(res->ctl);
560 return ctlaction(res);
563 static int
564 ctl_add(struct parse_result *res, int argc, char **argv)
566 int ch;
568 while ((ch = getopt(argc, argv, "")) != -1)
569 ctl_usage(res->ctl);
570 argc -= optind;
571 argv += optind;
573 if (argc == 0)
574 ctl_usage(res->ctl);
575 res->files = argv;
577 return ctlaction(res);
580 static int
581 ctl_show(struct parse_result *res, int argc, char **argv)
583 int ch;
585 while ((ch = getopt(argc, argv, "p")) != -1) {
586 switch (ch) {
587 case 'p':
588 res->pretty = 1;
589 break;
590 default:
591 ctl_usage(res->ctl);
595 return ctlaction(res);
598 static int
599 ctl_load(struct parse_result *res, int argc, char **argv)
601 int ch;
603 while ((ch = getopt(argc, argv, "")) != -1)
604 ctl_usage(res->ctl);
605 argc -= optind;
606 argv += optind;
608 if (argc == 0)
609 res->file = NULL;
610 else if (argc == 1)
611 res->file = argv[0];
612 else
613 ctl_usage(res->ctl);
615 if (pledge("stdio rpath", NULL) == -1)
616 fatal("pledge");
618 return ctlaction(res);
621 static int
622 ctl_jump(struct parse_result *res, int argc, char **argv)
624 int ch;
626 while ((ch = getopt(argc, argv, "")) != -1)
627 ctl_usage(res->ctl);
628 argc -= optind;
629 argv += optind;
631 if (argc != 1)
632 ctl_usage(res->ctl);
634 res->file = argv[0];
635 return ctlaction(res);
638 static int
639 ctl_repeat(struct parse_result *res, int argc, char **argv)
641 int ch, b;
643 while ((ch = getopt(argc, argv, "")) != -1)
644 ctl_usage(res->ctl);
645 argc -= optind;
646 argv += optind;
648 if (argc != 2)
649 ctl_usage(res->ctl);
651 if (!strcmp(argv[1], "on"))
652 b = 1;
653 else if (!strcmp(argv[1], "off"))
654 b = 0;
655 else
656 ctl_usage(res->ctl);
658 res->rep.repeat_one = -1;
659 res->rep.repeat_all = -1;
660 if (!strcmp(argv[0], "one"))
661 res->rep.repeat_one = b;
662 else if (!strcmp(argv[0], "all"))
663 res->rep.repeat_all = b;
664 else
665 ctl_usage(res->ctl);
667 return ctlaction(res);
670 static int
671 ctl_monitor(struct parse_result *res, int argc, char **argv)
673 int ch;
674 const char *events;
675 char *dup, *tmp, *tok;
677 while ((ch = getopt(argc, argv, "")) != -1)
678 ctl_usage(res->ctl);
679 argc -= optind;
680 argv += optind;
682 if (argc > 1)
683 ctl_usage(res->ctl);
685 if (argc == 1)
686 events = *argv;
687 else
688 events = "play,toggle,pause,stop,restart,flush,next,prev,"
689 "jump,repeat,add,load";
691 tmp = dup = xstrdup(events);
692 while ((tok = strsep(&tmp, ",")) != NULL) {
693 if (*tok == '\0')
694 continue;
696 if (!strcmp(tok, "play"))
697 res->monitor[IMSG_CTL_PLAY] = 1;
698 else if (!strcmp(tok, "toggle"))
699 res->monitor[IMSG_CTL_TOGGLE_PLAY] = 1;
700 else if (!strcmp(tok, "pause"))
701 res->monitor[IMSG_CTL_PAUSE] = 1;
702 else if (!strcmp(tok, "stop"))
703 res->monitor[IMSG_CTL_STOP] = 1;
704 else if (!strcmp(tok, "restart")) /* compat */
705 res->monitor[IMSG_CTL_SEEK] = 1;
706 else if (!strcmp(tok, "flush"))
707 res->monitor[IMSG_CTL_FLUSH] = 1;
708 else if (!strcmp(tok, "next"))
709 res->monitor[IMSG_CTL_NEXT] = 1;
710 else if (!strcmp(tok, "prev"))
711 res->monitor[IMSG_CTL_PREV] = 1;
712 else if (!strcmp(tok, "jump"))
713 res->monitor[IMSG_CTL_JUMP] = 1;
714 else if (!strcmp(tok, "repeat"))
715 res->monitor[IMSG_CTL_REPEAT] = 1;
716 else if (!strcmp(tok, "add"))
717 res->monitor[IMSG_CTL_ADD] = 1;
718 else if (!strcmp(tok, "load"))
719 res->monitor[IMSG_CTL_COMMIT] = 1;
720 else if (!strcmp(tok, "seek"))
721 res->monitor[IMSG_CTL_SEEK] = 1;
722 else
723 fatalx("unknown event \"%s\"", tok);
726 free(dup);
727 return ctlaction(res);
730 static int
731 ctl_seek(struct parse_result *res, int argc, char **argv)
733 const char *n;
734 char *ep;
735 int hours = 0, minutes = 0, seconds = 0;
736 int sign = 1;
738 if (argc > 0) {
739 /* skip the command name */
740 argc--;
741 argv++;
744 if (argc > 0 && !strcmp(*argv, "--")) {
745 argc--;
746 argv++;
749 if (argc != 1)
750 ctl_usage(res->ctl);
752 n = *argv;
753 if (*n == '-' || *n == '+')
754 res->seek.relative = 1;
755 if (*n == '-') {
756 n++;
757 sign = -1;
760 seconds = strtol(n, &ep, 10);
761 if (n[0] == '\0' ||
762 (*ep != '\0' && *ep != ':' && *ep != '%') ||
763 (*ep == '%' && ep[1] != '\0'))
764 fatalx("invalid offset: %s", argv[0]);
765 if (*ep == '\0' || *ep == '%') {
766 res->seek.percent = *ep == '%';
767 goto done;
770 n = ++ep;
771 minutes = seconds;
772 seconds = strtol(n, &ep, 10);
773 if (n[0] == '\0' || (*ep != '\0' && *ep != ':'))
774 fatalx("invalid offset: %s", argv[0]);
775 if (*ep == '\0')
776 goto done;
778 n = ++ep;
779 hours = minutes;
780 minutes = seconds;
781 seconds = strtol(n, &ep, 10);
782 if (n[0] == '\0' || *ep != '\0')
783 fatalx("invalid offset: %s", argv[0]);
785 done:
786 res->seek.offset = sign * (hours * 3600 + minutes * 60 + seconds);
787 return ctlaction(res);
790 static int
791 ctl_get_lock(const char *lockfile)
793 int lockfd;
795 if ((lockfd = open(lockfile, O_WRONLY|O_CREAT, 0600)) == -1) {
796 log_debug("open failed: %s", strerror(errno));
797 return -1;
800 if (flock(lockfd, LOCK_EX|LOCK_NB) == -1) {
801 log_debug("flock failed: %s", strerror(errno));
802 if (errno != EAGAIN)
803 return -1;
804 while (flock(lockfd, LOCK_EX) == -1 && errno == EINTR)
805 /* nop */;
806 close(lockfd);
807 return -2;
809 log_debug("flock succeeded");
811 return lockfd;
814 static int
815 ctl_connect(void)
817 struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
818 struct sockaddr_un sa;
819 size_t size;
820 int fd, lockfd = -1, locked = 0, spawned = 0;
821 int attempt = 0;
822 char *lockfile = NULL;
824 memset(&sa, 0, sizeof(sa));
825 sa.sun_family = AF_UNIX;
826 size = strlcpy(sa.sun_path, csock, sizeof(sa.sun_path));
827 if (size >= sizeof(sa.sun_path)) {
828 errno = ENAMETOOLONG;
829 return -1;
832 retry:
833 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
834 return -1;
836 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
837 log_debug("connection failed: %s", strerror(errno));
838 if (errno != ECONNREFUSED && errno != ENOENT)
839 goto failed;
840 if (attempt++ == 100)
841 goto failed;
842 close(fd);
844 if (!locked) {
845 xasprintf(&lockfile, "%s.lock", csock);
846 if ((lockfd = ctl_get_lock(lockfile)) < 0) {
847 log_debug("didn't get the lock (%d)", lockfd);
849 free(lockfile);
850 lockfile = NULL;
852 if (lockfd == -1)
853 goto retry;
856 /*
857 * Always retry at least once, even if we got
858 * the lock, because another client could have
859 * taken the lock, started the server and released
860 * the lock between our connect() and flock()
861 */
862 locked = 1;
863 goto retry;
866 if (!spawned) {
867 log_debug("spawning the daemon");
868 spawn_daemon();
869 spawned = 1;
872 nanosleep(&ts, NULL);
873 goto retry;
876 if (locked && lockfd >= 0) {
877 unlink(lockfile);
878 free(lockfile);
879 close(lockfd);
881 return fd;
883 failed:
884 if (locked) {
885 free(lockfile);
886 close(lockfd);
888 close(fd);
889 return -1;
892 __dead void
893 ctl(int argc, char **argv)
895 int ctl_sock;
897 log_init(1, LOG_DAEMON);
898 log_setverbose(verbose);
900 if (getcwd(cwd, sizeof(cwd)) == NULL)
901 fatal("getcwd");
903 if ((ctl_sock = ctl_connect()) == -1)
904 fatal("can't connect");
906 if (ctl_sock == -1)
907 fatalx("failed to connect to the daemon");
909 ibuf = xmalloc(sizeof(*ibuf));
910 imsg_init(ibuf, ctl_sock);
912 optreset = 1;
913 optind = 1;
915 exit(parse(argc, argv));