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_RESTART:
259 return "restart";
260 case IMSG_CTL_FLUSH:
261 return "flush";
262 case IMSG_CTL_NEXT:
263 return "next";
264 case IMSG_CTL_PREV:
265 return "prev";
266 case IMSG_CTL_JUMP:
267 return "jump";
268 case IMSG_CTL_REPEAT:
269 return "repeat";
270 case IMSG_CTL_ADD:
271 return "add";
272 case IMSG_CTL_COMMIT:
273 return "load";
274 default:
275 return "unknown";
279 static void
280 print_time(const char *label, int64_t seconds)
282 int hours, minutes;
284 if (seconds < 0)
285 seconds = 0;
287 hours = seconds / 3600;
288 seconds -= hours * 3600;
290 minutes = seconds / 60;
291 seconds -= minutes * 60;
293 printf("%s ", label);
294 if (hours != 0)
295 printf("%02d:", hours);
296 printf("%02d:%02lld\n", minutes, (long long)seconds);
299 static int
300 ctlaction(struct parse_result *res)
302 char path[PATH_MAX];
303 struct imsg imsg;
304 struct player_status ps;
305 size_t datalen;
306 ssize_t n;
307 int i, type, ret = 0, done = 1;
309 switch (res->action) {
310 case PLAY:
311 imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
312 if (verbose) {
313 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
314 NULL, 0);
315 done = 0;
317 break;
318 case PAUSE:
319 imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
320 break;
321 case TOGGLE:
322 imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
323 if (verbose) {
324 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
325 NULL, 0);
326 done = 0;
328 break;
329 case STOP:
330 imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
331 break;
332 case RESTART:
333 imsg_compose(ibuf, IMSG_CTL_RESTART, 0, 0, -1, NULL, 0);
334 if (verbose) {
335 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
336 NULL, 0);
337 done = 0;
339 break;
340 case ADD:
341 done = 0;
342 for (i = 0; res->files[i] != NULL; ++i) {
343 memset(path, 0, sizeof(path));
344 if (canonpath(res->files[i], path, sizeof(path))
345 == -1) {
346 log_warn("canonpath %s", res->files[i]);
347 continue;
350 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
351 path, sizeof(path));
353 ret = i == 0;
354 break;
355 case FLUSH:
356 imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
357 break;
358 case SHOW:
359 done = 0;
360 imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
361 break;
362 case STATUS:
363 done = 0;
364 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
365 break;
366 case NEXT:
367 imsg_compose(ibuf, IMSG_CTL_NEXT, 0, 0, -1, NULL, 0);
368 if (verbose) {
369 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
370 NULL, 0);
371 done = 0;
373 break;
374 case PREV:
375 imsg_compose(ibuf, IMSG_CTL_PREV, 0, 0, -1, NULL, 0);
376 if (verbose) {
377 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
378 NULL, 0);
379 done = 0;
381 break;
382 case LOAD:
383 done = 0;
384 imsg_compose(ibuf, IMSG_CTL_BEGIN, 0, 0, -1, NULL, 0);
385 break;
386 case JUMP:
387 done = 0;
388 memset(path, 0, sizeof(path));
389 strlcpy(path, res->file, sizeof(path));
390 imsg_compose(ibuf, IMSG_CTL_JUMP, 0, 0, -1,
391 path, sizeof(path));
392 break;
393 case REPEAT:
394 imsg_compose(ibuf, IMSG_CTL_REPEAT, 0, 0, -1,
395 &res->rep, sizeof(res->rep));
396 break;
397 case MONITOR:
398 done = 0;
399 imsg_compose(ibuf, IMSG_CTL_MONITOR, 0, 0, -1,
400 NULL, 0);
401 break;
402 case SEEK:
403 imsg_compose(ibuf, IMSG_CTL_SEEK, 0, 0, -1, &res->seek,
404 sizeof(res->seek));
405 break;
406 case NONE:
407 /* action not expected */
408 fatalx("invalid action %u", res->action);
409 break;
412 if (ret != 0)
413 goto end;
415 imsg_flush(ibuf);
417 i = 0;
418 while (!done) {
419 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
420 fatalx("imsg_read error");
421 if (n == 0)
422 fatalx("pipe closed");
424 while (!done) {
425 if ((n = imsg_get(ibuf, &imsg)) == -1)
426 fatalx("imsg_get error");
427 if (n == 0)
428 break;
430 if (imsg.hdr.type == IMSG_CTL_ERR) {
431 log_warnx("%s: %s", res->ctl->name,
432 imsg_strerror(&imsg));
433 ret = 1;
434 done = 1;
435 break;
438 datalen = IMSG_DATA_SIZE(imsg);
440 switch (res->action) {
441 case ADD:
442 if (res->files[i] == NULL)
443 fatalx("received more replies than "
444 "files enqueued.");
446 if (imsg.hdr.type == IMSG_CTL_ADD)
447 log_debug("enqueued %s", res->files[i]);
448 else
449 fatalx("invalid message %d",
450 imsg.hdr.type);
451 i++;
452 done = res->files[i] == NULL;
453 break;
454 case SHOW:
455 if (datalen == 0) {
456 done = 1;
457 break;
459 if (datalen != sizeof(ps))
460 fatalx("data size mismatch");
461 memcpy(&ps, imsg.data, sizeof(ps));
462 if (ps.path[sizeof(ps.path) - 1] != '\0')
463 fatalx("received corrupted data");
464 if (res->pretty) {
465 char c = ' ';
466 if (ps.status == STATE_PLAYING)
467 c = '>';
468 printf("%c ", c);
470 puts(ps.path);
471 break;
472 case PLAY:
473 case TOGGLE:
474 case RESTART:
475 case STATUS:
476 case NEXT:
477 case PREV:
478 case JUMP:
479 if (imsg.hdr.type != IMSG_CTL_STATUS)
480 fatalx("invalid message %d",
481 imsg.hdr.type);
483 if (datalen != sizeof(ps))
484 fatalx("data size mismatch");
485 memcpy(&ps, imsg.data, sizeof(ps));
486 if (ps.path[sizeof(ps.path) - 1] != '\0')
487 fatalx("received corrupted data");
489 if (ps.status == STATE_STOPPED)
490 printf("stopped ");
491 else if (ps.status == STATE_PLAYING)
492 printf("playing ");
493 else if (ps.status == STATE_PAUSED)
494 printf("paused ");
495 else
496 printf("unknown ");
498 puts(ps.path);
500 print_time("position", ps.position);
501 print_time("duration", ps.duration);
503 printf("repeat one %s\nrepeat all %s\n",
504 ps.rp.repeat_one ? "on" : "off",
505 ps.rp.repeat_all ? "on" : "off");
507 done = 1;
508 break;
509 case LOAD:
510 if (imsg.hdr.type == IMSG_CTL_ADD)
511 break;
512 if (imsg.hdr.type == IMSG_CTL_COMMIT) {
513 done = 1;
514 break;
517 if (imsg.hdr.type != IMSG_CTL_BEGIN)
518 fatalx("invalid message %d",
519 imsg.hdr.type);
521 load_files(res, &ret);
522 break;
523 case MONITOR:
524 if (imsg.hdr.type != IMSG_CTL_MONITOR)
525 fatalx("invalid message %d",
526 imsg.hdr.type);
528 if (datalen != sizeof(type))
529 fatalx("data size mismatch");
531 memcpy(&type, imsg.data, sizeof(type));
532 if (type < 0 || type > IMSG__LAST)
533 fatalx("received corrupted data");
535 if (!res->monitor[type])
536 break;
538 puts(imsg_name(type));
539 fflush(stdout);
540 break;
541 default:
542 done = 1;
543 break;
546 imsg_free(&imsg);
550 end:
551 return ret;
554 static int
555 ctl_noarg(struct parse_result *res, int argc, char **argv)
557 int ch;
559 while ((ch = getopt(argc, argv, "")) != -1)
560 ctl_usage(res->ctl);
561 argc -= optind;
562 argv += optind;
564 if (argc > 0)
565 ctl_usage(res->ctl);
567 return ctlaction(res);
570 static int
571 ctl_add(struct parse_result *res, int argc, char **argv)
573 int ch;
575 while ((ch = getopt(argc, argv, "")) != -1)
576 ctl_usage(res->ctl);
577 argc -= optind;
578 argv += optind;
580 if (argc == 0)
581 ctl_usage(res->ctl);
582 res->files = argv;
584 return ctlaction(res);
587 static int
588 ctl_show(struct parse_result *res, int argc, char **argv)
590 int ch;
592 while ((ch = getopt(argc, argv, "p")) != -1) {
593 switch (ch) {
594 case 'p':
595 res->pretty = 1;
596 break;
597 default:
598 ctl_usage(res->ctl);
602 return ctlaction(res);
605 static int
606 ctl_load(struct parse_result *res, int argc, char **argv)
608 int ch;
610 while ((ch = getopt(argc, argv, "")) != -1)
611 ctl_usage(res->ctl);
612 argc -= optind;
613 argv += optind;
615 if (argc == 0)
616 res->file = NULL;
617 else if (argc == 1)
618 res->file = argv[0];
619 else
620 ctl_usage(res->ctl);
622 if (pledge("stdio rpath", NULL) == -1)
623 fatal("pledge");
625 return ctlaction(res);
628 static int
629 ctl_jump(struct parse_result *res, int argc, char **argv)
631 int ch;
633 while ((ch = getopt(argc, argv, "")) != -1)
634 ctl_usage(res->ctl);
635 argc -= optind;
636 argv += optind;
638 if (argc != 1)
639 ctl_usage(res->ctl);
641 res->file = argv[0];
642 return ctlaction(res);
645 static int
646 ctl_repeat(struct parse_result *res, int argc, char **argv)
648 int ch, b;
650 while ((ch = getopt(argc, argv, "")) != -1)
651 ctl_usage(res->ctl);
652 argc -= optind;
653 argv += optind;
655 if (argc != 2)
656 ctl_usage(res->ctl);
658 if (!strcmp(argv[1], "on"))
659 b = 1;
660 else if (!strcmp(argv[1], "off"))
661 b = 0;
662 else
663 ctl_usage(res->ctl);
665 res->rep.repeat_one = -1;
666 res->rep.repeat_all = -1;
667 if (!strcmp(argv[0], "one"))
668 res->rep.repeat_one = b;
669 else if (!strcmp(argv[0], "all"))
670 res->rep.repeat_all = b;
671 else
672 ctl_usage(res->ctl);
674 return ctlaction(res);
677 static int
678 ctl_monitor(struct parse_result *res, int argc, char **argv)
680 int ch;
681 const char *events;
682 char *dup, *tmp, *tok;
684 while ((ch = getopt(argc, argv, "")) != -1)
685 ctl_usage(res->ctl);
686 argc -= optind;
687 argv += optind;
689 if (argc > 1)
690 ctl_usage(res->ctl);
692 if (argc == 1)
693 events = *argv;
694 else
695 events = "play,toggle,pause,stop,restart,flush,next,prev,"
696 "jump,repeat,add,load";
698 tmp = dup = xstrdup(events);
699 while ((tok = strsep(&tmp, ",")) != NULL) {
700 if (*tok == '\0')
701 continue;
703 if (!strcmp(tok, "play"))
704 res->monitor[IMSG_CTL_PLAY] = 1;
705 else if (!strcmp(tok, "toggle"))
706 res->monitor[IMSG_CTL_TOGGLE_PLAY] = 1;
707 else if (!strcmp(tok, "pause"))
708 res->monitor[IMSG_CTL_PAUSE] = 1;
709 else if (!strcmp(tok, "stop"))
710 res->monitor[IMSG_CTL_STOP] = 1;
711 else if (!strcmp(tok, "restart"))
712 res->monitor[IMSG_CTL_RESTART] = 1;
713 else if (!strcmp(tok, "flush"))
714 res->monitor[IMSG_CTL_FLUSH] = 1;
715 else if (!strcmp(tok, "next"))
716 res->monitor[IMSG_CTL_NEXT] = 1;
717 else if (!strcmp(tok, "prev"))
718 res->monitor[IMSG_CTL_PREV] = 1;
719 else if (!strcmp(tok, "jump"))
720 res->monitor[IMSG_CTL_JUMP] = 1;
721 else if (!strcmp(tok, "repeat"))
722 res->monitor[IMSG_CTL_REPEAT] = 1;
723 else if (!strcmp(tok, "add"))
724 res->monitor[IMSG_CTL_ADD] = 1;
725 else if (!strcmp(tok, "load"))
726 res->monitor[IMSG_CTL_COMMIT] = 1;
727 else
728 fatalx("unknown event \"%s\"", tok);
731 free(dup);
732 return ctlaction(res);
735 static int
736 ctl_seek(struct parse_result *res, int argc, char **argv)
738 const char *n, *errstr;
740 if (argc > 0) {
741 /* skip the command name */
742 argc--;
743 argv++;
746 if (argc > 0 && !strcmp(*argv, "--")) {
747 argc--;
748 argv++;
751 if (argc != 1)
752 ctl_usage(res->ctl);
754 n = *argv;
755 if (*n == '-' || *n == '+')
756 res->seek.relative = 1;
758 res->seek.offset = strtonum(n, INT64_MIN, INT64_MAX, &errstr);
759 if (errstr != NULL)
760 fatalx("offset is %s: %s", errstr, n);
762 return ctlaction(res);
765 static int
766 ctl_get_lock(const char *lockfile)
768 int lockfd;
770 if ((lockfd = open(lockfile, O_WRONLY|O_CREAT, 0600)) == -1) {
771 log_debug("open failed: %s", strerror(errno));
772 return -1;
775 if (flock(lockfd, LOCK_EX|LOCK_NB) == -1) {
776 log_debug("flock failed: %s", strerror(errno));
777 if (errno != EAGAIN)
778 return -1;
779 while (flock(lockfd, LOCK_EX) == -1 && errno == EINTR)
780 /* nop */;
781 close(lockfd);
782 return -2;
784 log_debug("flock succeeded");
786 return lockfd;
789 static int
790 ctl_connect(void)
792 struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
793 struct sockaddr_un sa;
794 size_t size;
795 int fd, lockfd = -1, locked = 0, spawned = 0;
796 int attempt = 0;
797 char *lockfile = NULL;
799 memset(&sa, 0, sizeof(sa));
800 sa.sun_family = AF_UNIX;
801 size = strlcpy(sa.sun_path, csock, sizeof(sa.sun_path));
802 if (size >= sizeof(sa.sun_path)) {
803 errno = ENAMETOOLONG;
804 return -1;
807 retry:
808 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
809 return -1;
811 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
812 log_debug("connection failed: %s", strerror(errno));
813 if (errno != ECONNREFUSED && errno != ENOENT)
814 goto failed;
815 if (attempt++ == 100)
816 goto failed;
817 close(fd);
819 if (!locked) {
820 xasprintf(&lockfile, "%s.lock", csock);
821 if ((lockfd = ctl_get_lock(lockfile)) < 0) {
822 log_debug("didn't get the lock (%d)", lockfd);
824 free(lockfile);
825 lockfile = NULL;
827 if (lockfd == -1)
828 goto retry;
831 /*
832 * Always retry at least once, even if we got
833 * the lock, because another client could have
834 * taken the lock, started the server and released
835 * the lock between our connect() and flock()
836 */
837 locked = 1;
838 goto retry;
841 if (!spawned) {
842 log_debug("spawning the daemon");
843 spawn_daemon();
844 spawned = 1;
847 nanosleep(&ts, NULL);
848 goto retry;
851 if (locked && lockfd >= 0) {
852 unlink(lockfile);
853 free(lockfile);
854 close(lockfd);
856 return fd;
858 failed:
859 if (locked) {
860 free(lockfile);
861 close(lockfd);
863 close(fd);
864 return -1;
867 __dead void
868 ctl(int argc, char **argv)
870 int ctl_sock;
872 log_init(1, LOG_DAEMON);
873 log_setverbose(verbose);
875 if (getcwd(cwd, sizeof(cwd)) == NULL)
876 fatal("getcwd");
878 if ((ctl_sock = ctl_connect()) == -1)
879 fatal("can't connect");
881 if (ctl_sock == -1)
882 fatalx("failed to connect to the daemon");
884 ibuf = xmalloc(sizeof(*ibuf));
885 imsg_init(ibuf, ctl_sock);
887 optreset = 1;
888 optind = 1;
890 exit(parse(argc, argv));