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 <sys/types.h>
19 #include <sys/socket.h>
20 #include <sys/queue.h>
21 #include <sys/uio.h>
22 #include <sys/un.h>
24 #include <errno.h>
25 #include <event.h>
26 #include <fcntl.h>
27 #include <limits.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;
42 char cwd[PATH_MAX];
44 static int ctl_noarg(struct parse_result *, int, char **);
45 static int ctl_add(struct parse_result *, int, char **);
46 static int ctl_show(struct parse_result *, int, char **);
47 static int ctl_load(struct parse_result *, int, char **);
48 static int ctl_jump(struct parse_result *, int, char **);
49 static int ctl_repeat(struct parse_result *, int, char **);
50 static int ctl_monitor(struct parse_result *, int, char **);
51 static int ctl_seek(struct parse_result *, int, char **);
53 struct ctl_command ctl_commands[] = {
54 { "add", ADD, ctl_add, "files...", 0 },
55 { "flush", FLUSH, ctl_noarg, "", 0 },
56 { "jump", JUMP, ctl_jump, "pattern", 0 },
57 { "load", LOAD, ctl_load, "[file]", 1 },
58 { "monitor", MONITOR, ctl_monitor, "[events]", 0 },
59 { "next", NEXT, ctl_noarg, "", 0 },
60 { "pause", PAUSE, ctl_noarg, "", 0 },
61 { "play", PLAY, ctl_noarg, "", 0 },
62 { "prev", PREV, ctl_noarg, "", 0 },
63 { "repeat", REPEAT, ctl_repeat, "one|all on|off", 0 },
64 { "restart", RESTART, ctl_noarg, "", 0 },
65 { "seek", SEEK, ctl_seek, "[+-]time", 0 },
66 { "show", SHOW, ctl_show, "[-p]", 0 },
67 { "status", STATUS, ctl_noarg, "", 0 },
68 { "stop", STOP, ctl_noarg, "", 0 },
69 { "toggle", TOGGLE, ctl_noarg, "", 0 },
70 { NULL, 0, NULL, NULL, 0 },
71 };
73 __dead void
74 usage(void)
75 {
76 fprintf(stderr, "usage: %s [-dv] [-s socket]\n", getprogname());
77 exit(1);
78 }
80 static __dead void
81 ctl_usage(struct ctl_command *ctl)
82 {
83 fprintf(stderr, "usage: %s [-v] [-s socket] %s %s\n", getprogname(),
84 ctl->name, ctl->usage);
85 exit(1);
86 }
88 /* based on canonpath from kern_pledge.c */
89 static int
90 canonpath(const char *input, char *buf, size_t bufsize)
91 {
92 const char *p;
93 char *q, path[PATH_MAX];
95 if (input[0] != '/') {
96 if (snprintf(path, sizeof(path), "%s/%s", cwd, input)
97 >= sizeof(path)) {
98 errno = ENAMETOOLONG;
99 return -1;
101 input = path;
104 p = input;
105 q = buf;
106 while (*p && (q - buf < bufsize)) {
107 if (p[0] == '/' && (p[1] == '/' || p[1] == '\0')) {
108 p += 1;
110 } else if (p[0] == '/' && p[1] == '.' &&
111 (p[2] == '/' || p[2] == '\0')) {
112 p += 2;
114 } else if (p[0] == '/' && p[1] == '.' && p[2] == '.' &&
115 (p[3] == '/' || p[3] == '\0')) {
116 p += 3;
117 if (q != buf) /* "/../" at start of buf */
118 while (*--q != '/')
119 continue;
121 } else {
122 *q++ = *p++;
125 if ((*p == '\0') && (q - buf < bufsize)) {
126 *q = 0;
127 return 0;
128 } else {
129 errno = ENAMETOOLONG;
130 return -1;
134 static int
135 parse(int argc, char **argv)
137 struct ctl_command *ctl = NULL;
138 struct parse_result res;
139 const char *argv0;
140 int i, status;
142 memset(&res, 0, sizeof(res));
144 if ((argv0 = argv[0]) == NULL)
145 argv0 = "status";
147 for (i = 0; ctl_commands[i].name != NULL; ++i) {
148 if (strncmp(ctl_commands[i].name, argv0, strlen(argv0))
149 == 0) {
150 if (ctl != NULL) {
151 fprintf(stderr,
152 "ambiguous argument: %s\n", argv0);
153 usage();
155 ctl = &ctl_commands[i];
159 if (ctl == NULL) {
160 fprintf(stderr, "unknown argument: %s\n", argv[0]);
161 usage();
164 res.action = ctl->action;
165 res.ctl = ctl;
167 if (!ctl->has_pledge) {
168 /* pledge(2) default if command doesn't have its own */
169 if (pledge("stdio", NULL) == -1)
170 fatal("pledge");
173 status = ctl->main(&res, argc, argv);
174 close(ibuf->fd);
175 free(ibuf);
176 return status;
179 static int
180 load_files(struct parse_result *res, int *ret)
182 FILE *f;
183 const char *file;
184 char *line = NULL;
185 char path[PATH_MAX];
186 size_t linesize = 0, i = 0;
187 ssize_t linelen, curr = -1;
189 if (res->file == NULL)
190 f = stdin;
191 else if ((f = fopen(res->file, "r")) == NULL) {
192 log_warn("can't open %s", res->file);
193 *ret = 1;
194 return 1;
197 while ((linelen = getline(&line, &linesize, f)) != -1) {
198 if (linelen == 0)
199 continue;
200 line[linelen-1] = '\0';
201 file = line;
202 if (!strncmp(file, "> ", 2)) {
203 file += 2;
204 curr = i;
205 } else if (!strncmp(file, " ", 2))
206 file += 2;
208 memset(path, 0, sizeof(path));
209 if (canonpath(file, path, sizeof(path)) == -1) {
210 log_warn("canonpath %s", file);
211 continue;
214 i++;
215 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
216 path, sizeof(path));
219 free(line);
220 if (ferror(f))
221 fatal("getline");
222 fclose(f);
224 if (i == 0) {
225 *ret = 1;
226 return 1;
229 imsg_compose(ibuf, IMSG_CTL_COMMIT, 0, 0, -1,
230 &curr, sizeof(curr));
231 imsg_flush(ibuf);
232 return 0;
235 static const char *
236 imsg_strerror(struct imsg *imsg)
238 size_t datalen;
239 const char *msg;
241 datalen = IMSG_DATA_SIZE(*imsg);
242 msg = imsg->data;
243 if (datalen == 0 || msg[datalen-1] != '\0')
244 fatalx("malformed error message");
246 return msg;
249 static const char *
250 imsg_name(int type)
252 switch (type) {
253 case IMSG_CTL_PLAY:
254 return "play";
255 case IMSG_CTL_TOGGLE_PLAY:
256 return "toggle";
257 case IMSG_CTL_PAUSE:
258 return "pause";
259 case IMSG_CTL_STOP:
260 return "stop";
261 case IMSG_CTL_RESTART:
262 return "restart";
263 case IMSG_CTL_FLUSH:
264 return "flush";
265 case IMSG_CTL_NEXT:
266 return "next";
267 case IMSG_CTL_PREV:
268 return "prev";
269 case IMSG_CTL_JUMP:
270 return "jump";
271 case IMSG_CTL_REPEAT:
272 return "repeat";
273 case IMSG_CTL_ADD:
274 return "add";
275 case IMSG_CTL_COMMIT:
276 return "load";
277 default:
278 return "unknown";
282 static void
283 print_time(const char *label, int64_t seconds)
285 int hours, minutes;
287 if (seconds < 0)
288 seconds = 0;
290 hours = seconds / 3600;
291 seconds -= hours * 3600;
293 minutes = seconds / 60;
294 seconds -= minutes * 60;
296 printf("%s ", label);
297 if (hours != 0)
298 printf("%02d:", hours);
299 printf("%02d:%02lld\n", minutes, (long long)seconds);
302 static int
303 ctlaction(struct parse_result *res)
305 char path[PATH_MAX];
306 struct imsg imsg;
307 struct player_status ps;
308 size_t datalen;
309 ssize_t n;
310 int i, type, ret = 0, done = 1;
312 switch (res->action) {
313 case PLAY:
314 imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
315 if (verbose) {
316 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
317 NULL, 0);
318 done = 0;
320 break;
321 case PAUSE:
322 imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
323 break;
324 case TOGGLE:
325 imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
326 if (verbose) {
327 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
328 NULL, 0);
329 done = 0;
331 break;
332 case STOP:
333 imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
334 break;
335 case RESTART:
336 imsg_compose(ibuf, IMSG_CTL_RESTART, 0, 0, -1, NULL, 0);
337 if (verbose) {
338 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
339 NULL, 0);
340 done = 0;
342 break;
343 case ADD:
344 done = 0;
345 for (i = 0; res->files[i] != NULL; ++i) {
346 memset(path, 0, sizeof(path));
347 if (canonpath(res->files[i], path, sizeof(path))
348 == -1) {
349 log_warn("canonpath %s", res->files[i]);
350 continue;
353 imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
354 path, sizeof(path));
356 ret = i == 0;
357 break;
358 case FLUSH:
359 imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
360 break;
361 case SHOW:
362 done = 0;
363 imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
364 break;
365 case STATUS:
366 done = 0;
367 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
368 break;
369 case NEXT:
370 imsg_compose(ibuf, IMSG_CTL_NEXT, 0, 0, -1, NULL, 0);
371 if (verbose) {
372 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
373 NULL, 0);
374 done = 0;
376 break;
377 case PREV:
378 imsg_compose(ibuf, IMSG_CTL_PREV, 0, 0, -1, NULL, 0);
379 if (verbose) {
380 imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
381 NULL, 0);
382 done = 0;
384 break;
385 case LOAD:
386 done = 0;
387 imsg_compose(ibuf, IMSG_CTL_BEGIN, 0, 0, -1, NULL, 0);
388 break;
389 case JUMP:
390 done = 0;
391 memset(path, 0, sizeof(path));
392 strlcpy(path, res->file, sizeof(path));
393 imsg_compose(ibuf, IMSG_CTL_JUMP, 0, 0, -1,
394 path, sizeof(path));
395 break;
396 case REPEAT:
397 imsg_compose(ibuf, IMSG_CTL_REPEAT, 0, 0, -1,
398 &res->rep, sizeof(res->rep));
399 break;
400 case MONITOR:
401 done = 0;
402 imsg_compose(ibuf, IMSG_CTL_MONITOR, 0, 0, -1,
403 NULL, 0);
404 break;
405 case SEEK:
406 imsg_compose(ibuf, IMSG_CTL_SEEK, 0, 0, -1, &res->seek,
407 sizeof(res->seek));
408 break;
409 case NONE:
410 /* action not expected */
411 fatalx("invalid action %u", res->action);
412 break;
415 if (ret != 0)
416 goto end;
418 imsg_flush(ibuf);
420 i = 0;
421 while (!done) {
422 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
423 fatalx("imsg_read error");
424 if (n == 0)
425 fatalx("pipe closed");
427 while (!done) {
428 if ((n = imsg_get(ibuf, &imsg)) == -1)
429 fatalx("imsg_get error");
430 if (n == 0)
431 break;
433 if (imsg.hdr.type == IMSG_CTL_ERR) {
434 log_warnx("%s: %s", res->ctl->name,
435 imsg_strerror(&imsg));
436 ret = 1;
437 done = 1;
438 break;
441 datalen = IMSG_DATA_SIZE(imsg);
443 switch (res->action) {
444 case ADD:
445 if (res->files[i] == NULL)
446 fatalx("received more replies than "
447 "files enqueued.");
449 if (imsg.hdr.type == IMSG_CTL_ADD)
450 log_debug("enqueued %s", res->files[i]);
451 else
452 fatalx("invalid message %d",
453 imsg.hdr.type);
454 i++;
455 done = res->files[i] == NULL;
456 break;
457 case SHOW:
458 if (datalen == 0) {
459 done = 1;
460 break;
462 if (datalen != sizeof(ps))
463 fatalx("data size mismatch");
464 memcpy(&ps, imsg.data, sizeof(ps));
465 if (ps.path[sizeof(ps.path) - 1] != '\0')
466 fatalx("received corrupted data");
467 if (res->pretty) {
468 char c = ' ';
469 if (ps.status == STATE_PLAYING)
470 c = '>';
471 printf("%c ", c);
473 puts(ps.path);
474 break;
475 case PLAY:
476 case TOGGLE:
477 case RESTART:
478 case STATUS:
479 case NEXT:
480 case PREV:
481 case JUMP:
482 if (imsg.hdr.type != IMSG_CTL_STATUS)
483 fatalx("invalid message %d",
484 imsg.hdr.type);
486 if (datalen != sizeof(ps))
487 fatalx("data size mismatch");
488 memcpy(&ps, imsg.data, sizeof(ps));
489 if (ps.path[sizeof(ps.path) - 1] != '\0')
490 fatalx("received corrupted data");
492 if (ps.status == STATE_STOPPED)
493 printf("stopped ");
494 else if (ps.status == STATE_PLAYING)
495 printf("playing ");
496 else if (ps.status == STATE_PAUSED)
497 printf("paused ");
498 else
499 printf("unknown ");
501 puts(ps.path);
503 print_time("position", ps.position);
504 print_time("duration", ps.duration);
506 printf("repeat one %s\nrepeat all %s\n",
507 ps.rp.repeat_one ? "on" : "off",
508 ps.rp.repeat_all ? "on" : "off");
510 done = 1;
511 break;
512 case LOAD:
513 if (imsg.hdr.type == IMSG_CTL_ADD)
514 break;
515 if (imsg.hdr.type == IMSG_CTL_COMMIT) {
516 done = 1;
517 break;
520 if (imsg.hdr.type != IMSG_CTL_BEGIN)
521 fatalx("invalid message %d",
522 imsg.hdr.type);
524 load_files(res, &ret);
525 break;
526 case MONITOR:
527 if (imsg.hdr.type != IMSG_CTL_MONITOR)
528 fatalx("invalid message %d",
529 imsg.hdr.type);
531 if (datalen != sizeof(type))
532 fatalx("data size mismatch");
534 memcpy(&type, imsg.data, sizeof(type));
535 if (type < 0 || type > IMSG__LAST)
536 fatalx("received corrupted data");
538 if (!res->monitor[type])
539 break;
541 puts(imsg_name(type));
542 fflush(stdout);
543 break;
544 default:
545 done = 1;
546 break;
549 imsg_free(&imsg);
553 end:
554 return ret;
557 static int
558 ctl_noarg(struct parse_result *res, int argc, char **argv)
560 int ch;
562 while ((ch = getopt(argc, argv, "")) != -1)
563 ctl_usage(res->ctl);
564 argc -= optind;
565 argv += optind;
567 if (argc > 0)
568 ctl_usage(res->ctl);
570 return ctlaction(res);
573 static int
574 ctl_add(struct parse_result *res, int argc, char **argv)
576 int ch;
578 while ((ch = getopt(argc, argv, "")) != -1)
579 ctl_usage(res->ctl);
580 argc -= optind;
581 argv += optind;
583 if (argc == 0)
584 ctl_usage(res->ctl);
585 res->files = argv;
587 return ctlaction(res);
590 static int
591 ctl_show(struct parse_result *res, int argc, char **argv)
593 int ch;
595 while ((ch = getopt(argc, argv, "p")) != -1) {
596 switch (ch) {
597 case 'p':
598 res->pretty = 1;
599 break;
600 default:
601 ctl_usage(res->ctl);
605 return ctlaction(res);
608 static int
609 ctl_load(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 == 0)
619 res->file = NULL;
620 else if (argc == 1)
621 res->file = argv[0];
622 else
623 ctl_usage(res->ctl);
625 if (pledge("stdio rpath", NULL) == -1)
626 fatal("pledge");
628 return ctlaction(res);
631 static int
632 ctl_jump(struct parse_result *res, int argc, char **argv)
634 int ch;
636 while ((ch = getopt(argc, argv, "")) != -1)
637 ctl_usage(res->ctl);
638 argc -= optind;
639 argv += optind;
641 if (argc != 1)
642 ctl_usage(res->ctl);
644 res->file = argv[0];
645 return ctlaction(res);
648 static int
649 ctl_repeat(struct parse_result *res, int argc, char **argv)
651 int ch, b;
653 while ((ch = getopt(argc, argv, "")) != -1)
654 ctl_usage(res->ctl);
655 argc -= optind;
656 argv += optind;
658 if (argc != 2)
659 ctl_usage(res->ctl);
661 if (!strcmp(argv[1], "on"))
662 b = 1;
663 else if (!strcmp(argv[1], "off"))
664 b = 0;
665 else
666 ctl_usage(res->ctl);
668 res->rep.repeat_one = -1;
669 res->rep.repeat_all = -1;
670 if (!strcmp(argv[0], "one"))
671 res->rep.repeat_one = b;
672 else if (!strcmp(argv[0], "all"))
673 res->rep.repeat_all = b;
674 else
675 ctl_usage(res->ctl);
677 return ctlaction(res);
680 static int
681 ctl_monitor(struct parse_result *res, int argc, char **argv)
683 int ch;
684 const char *events;
685 char *dup, *tmp, *tok;
687 while ((ch = getopt(argc, argv, "")) != -1)
688 ctl_usage(res->ctl);
689 argc -= optind;
690 argv += optind;
692 if (argc > 1)
693 ctl_usage(res->ctl);
695 if (argc == 1)
696 events = *argv;
697 else
698 events = "play,toggle,pause,stop,restart,flush,next,prev,"
699 "jump,repeat,add,load";
701 tmp = dup = xstrdup(events);
702 while ((tok = strsep(&tmp, ",")) != NULL) {
703 if (*tok == '\0')
704 continue;
706 if (!strcmp(tok, "play"))
707 res->monitor[IMSG_CTL_PLAY] = 1;
708 else if (!strcmp(tok, "toggle"))
709 res->monitor[IMSG_CTL_TOGGLE_PLAY] = 1;
710 else if (!strcmp(tok, "pause"))
711 res->monitor[IMSG_CTL_PAUSE] = 1;
712 else if (!strcmp(tok, "stop"))
713 res->monitor[IMSG_CTL_STOP] = 1;
714 else if (!strcmp(tok, "restart"))
715 res->monitor[IMSG_CTL_RESTART] = 1;
716 else if (!strcmp(tok, "flush"))
717 res->monitor[IMSG_CTL_FLUSH] = 1;
718 else if (!strcmp(tok, "next"))
719 res->monitor[IMSG_CTL_NEXT] = 1;
720 else if (!strcmp(tok, "prev"))
721 res->monitor[IMSG_CTL_PREV] = 1;
722 else if (!strcmp(tok, "jump"))
723 res->monitor[IMSG_CTL_JUMP] = 1;
724 else if (!strcmp(tok, "repeat"))
725 res->monitor[IMSG_CTL_REPEAT] = 1;
726 else if (!strcmp(tok, "add"))
727 res->monitor[IMSG_CTL_ADD] = 1;
728 else if (!strcmp(tok, "load"))
729 res->monitor[IMSG_CTL_COMMIT] = 1;
730 else
731 fatalx("unknown event \"%s\"", tok);
734 free(dup);
735 return ctlaction(res);
738 static int
739 ctl_seek(struct parse_result *res, int argc, char **argv)
741 const char *n, *errstr;
743 if (argc > 0) {
744 /* skip the command name */
745 argc--;
746 argv++;
749 if (argc > 0 && !strcmp(*argv, "--")) {
750 argc--;
751 argv++;
754 if (argc != 1)
755 ctl_usage(res->ctl);
757 n = *argv;
758 if (*n == '-' || *n == '+')
759 res->seek.relative = 1;
761 res->seek.offset = strtonum(n, INT64_MIN, INT64_MAX, &errstr);
762 if (errstr != NULL)
763 fatalx("offset is %s: %s", errstr, n);
765 return ctlaction(res);
768 static int
769 ctl_get_lock(const char *lockfile)
771 int lockfd;
773 if ((lockfd = open(lockfile, O_WRONLY|O_CREAT, 0600)) == -1) {
774 log_debug("open failed: %s", strerror(errno));
775 return -1;
778 if (flock(lockfd, LOCK_EX|LOCK_NB) == -1) {
779 log_debug("flock failed: %s", strerror(errno));
780 if (errno != EAGAIN)
781 return -1;
782 while (flock(lockfd, LOCK_EX) == -1 && errno == EINTR)
783 /* nop */;
784 close(lockfd);
785 return -2;
787 log_debug("flock succeeded");
789 return lockfd;
792 static int
793 ctl_connect(void)
795 struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
796 struct sockaddr_un sa;
797 size_t size;
798 int fd, lockfd = -1, locked = 0, spawned = 0;
799 int attempt = 0;
800 char *lockfile = NULL;
802 memset(&sa, 0, sizeof(sa));
803 sa.sun_family = AF_UNIX;
804 size = strlcpy(sa.sun_path, csock, sizeof(sa.sun_path));
805 if (size >= sizeof(sa.sun_path)) {
806 errno = ENAMETOOLONG;
807 return -1;
810 retry:
811 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
812 return -1;
814 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
815 log_debug("connection failed: %s", strerror(errno));
816 if (errno != ECONNREFUSED && errno != ENOENT)
817 goto failed;
818 if (attempt++ == 100)
819 goto failed;
820 close(fd);
822 if (!locked) {
823 xasprintf(&lockfile, "%s.lock", csock);
824 if ((lockfd = ctl_get_lock(lockfile)) < 0) {
825 log_debug("didn't get the lock (%d)", lockfd);
827 free(lockfile);
828 lockfile = NULL;
830 if (lockfd == -1)
831 goto retry;
834 /*
835 * Always retry at least once, even if we got
836 * the lock, because another client could have
837 * taken the lock, started the server and released
838 * the lock between our connect() and flock()
839 */
840 locked = 1;
841 goto retry;
844 if (!spawned) {
845 log_debug("spawning the daemon");
846 spawn_daemon();
847 spawned = 1;
850 nanosleep(&ts, NULL);
851 goto retry;
854 if (locked && lockfd >= 0) {
855 unlink(lockfile);
856 free(lockfile);
857 close(lockfd);
859 return fd;
861 failed:
862 if (locked) {
863 free(lockfile);
864 close(lockfd);
866 close(fd);
867 return -1;
870 __dead void
871 ctl(int argc, char **argv)
873 int ctl_sock;
875 log_init(1, LOG_DAEMON);
876 log_setverbose(verbose);
878 if (getcwd(cwd, sizeof(cwd)) == NULL)
879 fatal("getcwd");
881 if ((ctl_sock = ctl_connect()) == -1)
882 fatal("can't connect");
884 if (ctl_sock == -1)
885 fatalx("failed to connect to the daemon");
887 ibuf = xmalloc(sizeof(*ibuf));
888 imsg_init(ibuf, ctl_sock);
890 optreset = 1;
891 optind = 1;
893 exit(parse(argc, argv));