2 3baa2617 2022-02-16 op * Copyright (c) 2022 Omar Polo <op@openbsd.org>
4 3baa2617 2022-02-16 op * Permission to use, copy, modify, and distribute this software for any
5 3baa2617 2022-02-16 op * purpose with or without fee is hereby granted, provided that the above
6 3baa2617 2022-02-16 op * copyright notice and this permission notice appear in all copies.
8 3baa2617 2022-02-16 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 3baa2617 2022-02-16 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 3baa2617 2022-02-16 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 3baa2617 2022-02-16 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 3baa2617 2022-02-16 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 3baa2617 2022-02-16 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 3baa2617 2022-02-16 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 3baa2617 2022-02-16 op #include <sys/types.h>
18 3baa2617 2022-02-16 op #include <sys/socket.h>
19 3baa2617 2022-02-16 op #include <sys/queue.h>
20 3baa2617 2022-02-16 op #include <sys/uio.h>
21 3baa2617 2022-02-16 op #include <sys/un.h>
23 3baa2617 2022-02-16 op #include <errno.h>
24 3baa2617 2022-02-16 op #include <event.h>
25 1d673950 2022-03-03 op #include <fcntl.h>
26 3baa2617 2022-02-16 op #include <limits.h>
27 3baa2617 2022-02-16 op #include <stdio.h>
28 3baa2617 2022-02-16 op #include <stdlib.h>
29 3baa2617 2022-02-16 op #include <stdint.h>
30 3baa2617 2022-02-16 op #include <string.h>
31 3baa2617 2022-02-16 op #include <syslog.h>
32 3baa2617 2022-02-16 op #include <unistd.h>
33 3baa2617 2022-02-16 op #include <imsg.h>
35 3baa2617 2022-02-16 op #include "amused.h"
36 3baa2617 2022-02-16 op #include "log.h"
37 bb3f279f 2022-02-16 op #include "playlist.h"
38 3baa2617 2022-02-16 op #include "xmalloc.h"
40 3baa2617 2022-02-16 op static struct imsgbuf *ibuf;
42 3baa2617 2022-02-16 op int ctl_noarg(struct parse_result *, int, char **);
43 3baa2617 2022-02-16 op int ctl_add(struct parse_result *, int, char **);
44 8e916714 2022-02-17 op int ctl_show(struct parse_result *, int, char **);
45 14be015f 2022-02-17 op int ctl_load(struct parse_result *, int, char **);
46 a913de21 2022-02-17 op int ctl_jump(struct parse_result *, int, char **);
47 310ef57c 2022-02-19 op int ctl_repeat(struct parse_result *, int, char **);
49 3baa2617 2022-02-16 op struct ctl_command ctl_commands[] = {
50 3baa2617 2022-02-16 op { "play", PLAY, ctl_noarg, "" },
51 3baa2617 2022-02-16 op { "pause", PAUSE, ctl_noarg, "" },
52 3baa2617 2022-02-16 op { "toggle", TOGGLE, ctl_noarg, "" },
53 3baa2617 2022-02-16 op { "stop", STOP, ctl_noarg, "" },
54 3baa2617 2022-02-16 op { "restart", RESTART, ctl_noarg, "" },
55 3baa2617 2022-02-16 op { "add", ADD, ctl_add, "files...", 1 },
56 3baa2617 2022-02-16 op { "flush", FLUSH, ctl_noarg, "" },
57 714abf92 2022-02-19 op { "show", SHOW, ctl_show, "[-p]" },
58 bb3f279f 2022-02-16 op { "status", STATUS, ctl_noarg, "" },
59 af27e631 2022-02-17 op { "next", NEXT, ctl_noarg, "" },
60 af27e631 2022-02-17 op { "prev", PREV, ctl_noarg, "" },
61 14be015f 2022-02-17 op { "load", LOAD, ctl_load, "[file]", 1 },
62 a913de21 2022-02-17 op { "jump", JUMP, ctl_jump, "pattern" },
63 310ef57c 2022-02-19 op { "repeat", REPEAT, ctl_repeat, "one|all on|off" },
64 6b47a39f 2022-02-21 op { "monitor", MONITOR, ctl_noarg, "" },
71 3baa2617 2022-02-16 op fprintf(stderr, "usage: %s [-dv] [-s socket]\n", getprogname());
75 3baa2617 2022-02-16 op static __dead void
76 3baa2617 2022-02-16 op ctl_usage(struct ctl_command *ctl)
78 3baa2617 2022-02-16 op fprintf(stderr, "usage: %s [-v] [-s socket] %s %s\n", getprogname(),
79 3baa2617 2022-02-16 op ctl->name, ctl->usage);
84 3baa2617 2022-02-16 op parse(int argc, char **argv)
86 3baa2617 2022-02-16 op struct ctl_command *ctl = NULL;
87 3baa2617 2022-02-16 op struct parse_result res;
88 1d673950 2022-03-03 op const char *argv0;
89 3baa2617 2022-02-16 op int i, status;
91 3baa2617 2022-02-16 op memset(&res, 0, sizeof(res));
93 1d673950 2022-03-03 op if ((argv0 = argv[0]) == NULL)
94 1d673950 2022-03-03 op argv0 = "status";
96 3baa2617 2022-02-16 op for (i = 0; ctl_commands[i].name != NULL; ++i) {
97 1d673950 2022-03-03 op if (strncmp(ctl_commands[i].name, argv0, strlen(argv0))
99 3baa2617 2022-02-16 op if (ctl != NULL) {
100 3baa2617 2022-02-16 op fprintf(stderr,
101 1d673950 2022-03-03 op "ambiguous argument: %s\n", argv0);
104 3baa2617 2022-02-16 op ctl = &ctl_commands[i];
108 3baa2617 2022-02-16 op if (ctl == NULL) {
109 3baa2617 2022-02-16 op fprintf(stderr, "unknown argument: %s\n", argv[0]);
113 3baa2617 2022-02-16 op res.action = ctl->action;
114 3baa2617 2022-02-16 op res.ctl = ctl;
116 3baa2617 2022-02-16 op if (!ctl->has_pledge) {
117 3baa2617 2022-02-16 op /* pledge(2) default if command doesn't have its own */
118 3baa2617 2022-02-16 op if (pledge("stdio", NULL) == -1)
119 3baa2617 2022-02-16 op fatal("pledge");
122 3baa2617 2022-02-16 op status = ctl->main(&res, argc, argv);
123 3baa2617 2022-02-16 op close(ibuf->fd);
125 3baa2617 2022-02-16 op return status;
129 3baa2617 2022-02-16 op enqueue_tracks(char **files)
131 3baa2617 2022-02-16 op char res[PATH_MAX];
134 3baa2617 2022-02-16 op for (; *files != NULL; ++files) {
135 3baa2617 2022-02-16 op memset(&res, 0, sizeof(res));
136 3baa2617 2022-02-16 op if (realpath(*files, res) == NULL) {
137 3baa2617 2022-02-16 op log_warn("realpath %s", *files);
141 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
142 3baa2617 2022-02-16 op res, sizeof(res));
146 3baa2617 2022-02-16 op return enq == 0;
150 a913de21 2022-02-17 op jump_req(const char *arg)
152 a913de21 2022-02-17 op char path[PATH_MAX];
154 a913de21 2022-02-17 op memset(path, 0, sizeof(path));
155 a913de21 2022-02-17 op strlcpy(path, arg, sizeof(path));
156 a913de21 2022-02-17 op imsg_compose(ibuf, IMSG_CTL_JUMP, 0, 0, -1, path, sizeof(path));
161 8ff9231f 2022-02-16 op print_error_message(const char *prfx, struct imsg *imsg)
163 8ff9231f 2022-02-16 op size_t datalen;
166 8ff9231f 2022-02-16 op datalen = IMSG_DATA_SIZE(*imsg);
167 8ff9231f 2022-02-16 op if ((msg = calloc(1, datalen)) == NULL)
168 8ff9231f 2022-02-16 op fatal("calloc %zu", datalen);
169 8ff9231f 2022-02-16 op memcpy(msg, imsg->data, datalen);
170 8ff9231f 2022-02-16 op if (datalen == 0 || msg[datalen-1] != '\0')
171 8ff9231f 2022-02-16 op fatalx("malformed error message");
173 8ff9231f 2022-02-16 op log_warnx("%s: %s", prfx, msg);
178 8ff9231f 2022-02-16 op show_add(struct imsg *imsg, int *ret, char ***files)
180 8ff9231f 2022-02-16 op if (**files == NULL) {
181 8ff9231f 2022-02-16 op log_warnx("received more replies than file sent");
186 8ff9231f 2022-02-16 op if (imsg->hdr.type == IMSG_CTL_ERR)
187 8ff9231f 2022-02-16 op print_error_message(**files, imsg);
188 8ff9231f 2022-02-16 op else if (imsg->hdr.type == IMSG_CTL_ADD)
189 8ff9231f 2022-02-16 op log_debug("enqueued %s", **files);
191 8ff9231f 2022-02-16 op fatalx("got invalid message %d", imsg->hdr.type);
194 8ff9231f 2022-02-16 op return (**files) == NULL;
198 8e916714 2022-02-17 op show_complete(struct parse_result *res, struct imsg *imsg, int *ret)
200 63dd8e70 2022-02-17 op struct player_status s;
201 3baa2617 2022-02-16 op size_t datalen;
203 8ff9231f 2022-02-16 op if (imsg->hdr.type == IMSG_CTL_ERR) {
204 8ff9231f 2022-02-16 op print_error_message("show failed", imsg);
209 3baa2617 2022-02-16 op datalen = IMSG_DATA_SIZE(*imsg);
210 3baa2617 2022-02-16 op if (datalen == 0)
213 63dd8e70 2022-02-17 op if (datalen != sizeof(s))
214 3baa2617 2022-02-16 op fatalx("%s: data size mismatch", __func__);
215 63dd8e70 2022-02-17 op memcpy(&s, imsg->data, sizeof(s));
216 63dd8e70 2022-02-17 op if (s.path[sizeof(s.path)-1] != '\0')
217 3baa2617 2022-02-16 op fatalx("%s: data corrupted?", __func__);
219 8e916714 2022-02-17 op if (res->pretty)
220 8e916714 2022-02-17 op printf("%c ", s.status == STATE_PLAYING ? '>' : ' ');
221 8e916714 2022-02-17 op printf("%s\n", s.path);
226 bb3f279f 2022-02-16 op show_status(struct imsg *imsg, int *ret)
228 bb3f279f 2022-02-16 op struct player_status s;
229 bb3f279f 2022-02-16 op size_t datalen;
231 bb3f279f 2022-02-16 op if (imsg->hdr.type == IMSG_CTL_ERR) {
232 bb3f279f 2022-02-16 op print_error_message("show failed", imsg);
237 bb3f279f 2022-02-16 op if (imsg->hdr.type != IMSG_CTL_STATUS)
238 bb3f279f 2022-02-16 op fatalx("%s: got wrong reply", __func__);
240 bb3f279f 2022-02-16 op datalen = IMSG_DATA_SIZE(*imsg);
241 bb3f279f 2022-02-16 op if (datalen != sizeof(s))
242 bb3f279f 2022-02-16 op fatalx("%s: data size mismatch", __func__);
243 bb3f279f 2022-02-16 op memcpy(&s, imsg->data, sizeof(s));
244 bb3f279f 2022-02-16 op if (s.path[sizeof(s.path)-1] != '\0')
245 bb3f279f 2022-02-16 op fatalx("%s: data corrupted?", __func__);
247 bb3f279f 2022-02-16 op switch (s.status) {
248 bb3f279f 2022-02-16 op case STATE_STOPPED:
249 bb3f279f 2022-02-16 op printf("stopped ");
251 bb3f279f 2022-02-16 op case STATE_PLAYING:
252 bb3f279f 2022-02-16 op printf("playing ");
254 bb3f279f 2022-02-16 op case STATE_PAUSED:
255 bb3f279f 2022-02-16 op printf("paused ");
258 bb3f279f 2022-02-16 op printf("unknown ");
262 bb3f279f 2022-02-16 op printf("%s\n", s.path);
263 f44b3c4e 2022-03-03 op printf("repeat one %s\nrepeat all %s\n",
264 44398779 2022-02-19 op s.rp.repeat_one ? "on" : "off",
265 44398779 2022-02-19 op s.rp.repeat_all ? "on" : "off");
270 7a427ecd 2022-02-17 op show_load(struct parse_result *res, struct imsg *imsg, int *ret)
273 7a427ecd 2022-02-17 op const char *file;
274 7a427ecd 2022-02-17 op char *line = NULL;
275 7a427ecd 2022-02-17 op char path[PATH_MAX];
276 3af93963 2022-03-02 op size_t linesize = 0, i = 0;
277 3af93963 2022-03-02 op ssize_t linelen, curr = -1;
279 7a427ecd 2022-02-17 op if (imsg->hdr.type == IMSG_CTL_ERR) {
280 7a427ecd 2022-02-17 op print_error_message("load failed", imsg);
285 7a427ecd 2022-02-17 op if (imsg->hdr.type == IMSG_CTL_ADD)
288 7a427ecd 2022-02-17 op if (imsg->hdr.type == IMSG_CTL_COMMIT)
291 7a427ecd 2022-02-17 op if (imsg->hdr.type != IMSG_CTL_BEGIN)
292 7a427ecd 2022-02-17 op fatalx("got unexpected message %d", imsg->hdr.type);
294 ec1fb0c7 2022-02-17 op if (res->file == NULL)
296 ec1fb0c7 2022-02-17 op else if ((f = fopen(res->file, "r")) == NULL) {
297 ec1fb0c7 2022-02-17 op log_warn("can't open %s", res->file);
302 ec1fb0c7 2022-02-17 op while ((linelen = getline(&line, &linesize, f)) != -1) {
303 7a427ecd 2022-02-17 op if (linelen == 0)
305 7a427ecd 2022-02-17 op line[linelen-1] = '\0';
307 3af93963 2022-03-02 op if (file[0] == '>' && file[1] == ' ') {
311 7a427ecd 2022-02-17 op if (file[0] == ' ' && file[1] == ' ')
314 7a427ecd 2022-02-17 op memset(&path, 0, sizeof(path));
315 7a427ecd 2022-02-17 op if (realpath(file, path) == NULL) {
316 7a427ecd 2022-02-17 op log_warn("realpath %s", file);
321 7a427ecd 2022-02-17 op imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
322 7a427ecd 2022-02-17 op path, sizeof(path));
326 ec1fb0c7 2022-02-17 op if (ferror(f))
327 7a427ecd 2022-02-17 op fatal("getline");
330 3af93963 2022-03-02 op if (i == 0) {
335 7a427ecd 2022-02-17 op imsg_compose(ibuf, IMSG_CTL_COMMIT, 0, 0, -1,
336 3af93963 2022-03-02 op &curr, sizeof(curr));
337 7a427ecd 2022-02-17 op imsg_flush(ibuf);
342 6b47a39f 2022-02-21 op show_monitor(struct imsg *imsg, int *ret)
346 6b47a39f 2022-02-21 op if (imsg->hdr.type != IMSG_CTL_MONITOR) {
347 6b47a39f 2022-02-21 op log_warnx("wrong message type received: %d",
348 6b47a39f 2022-02-21 op imsg->hdr.type);
353 6b47a39f 2022-02-21 op if (IMSG_DATA_SIZE(*imsg) != sizeof(type)) {
354 6b47a39f 2022-02-21 op log_warnx("size mismatch");
359 6b47a39f 2022-02-21 op memcpy(&type, imsg->data, sizeof(type));
360 6b47a39f 2022-02-21 op switch (type) {
361 6b47a39f 2022-02-21 op case IMSG_CTL_PLAY:
362 6b47a39f 2022-02-21 op puts("play");
364 6b47a39f 2022-02-21 op case IMSG_CTL_TOGGLE_PLAY:
365 6b47a39f 2022-02-21 op puts("toggle");
367 6b47a39f 2022-02-21 op case IMSG_CTL_PAUSE:
368 6b47a39f 2022-02-21 op puts("pause");
370 6b47a39f 2022-02-21 op case IMSG_CTL_STOP:
371 6b47a39f 2022-02-21 op puts("stop");
373 6b47a39f 2022-02-21 op case IMSG_CTL_RESTART:
374 6b47a39f 2022-02-21 op puts("restart");
376 6b47a39f 2022-02-21 op case IMSG_CTL_FLUSH:
377 6b47a39f 2022-02-21 op puts("flush");
379 6b47a39f 2022-02-21 op case IMSG_CTL_NEXT:
380 6b47a39f 2022-02-21 op puts("next");
382 6b47a39f 2022-02-21 op case IMSG_CTL_PREV:
383 6b47a39f 2022-02-21 op puts("prev");
385 6b47a39f 2022-02-21 op case IMSG_CTL_JUMP:
386 6b47a39f 2022-02-21 op puts("jump");
388 6b47a39f 2022-02-21 op case IMSG_CTL_REPEAT:
389 6b47a39f 2022-02-21 op puts("repeat");
391 6b47a39f 2022-02-21 op case IMSG_CTL_ADD:
394 6b47a39f 2022-02-21 op case IMSG_CTL_COMMIT:
395 6b47a39f 2022-02-21 op puts("load");
398 6b47a39f 2022-02-21 op puts("unknown");
402 8b51ccee 2022-02-22 op fflush(stdout);
407 3baa2617 2022-02-16 op ctlaction(struct parse_result *res)
409 3baa2617 2022-02-16 op struct imsg imsg;
411 3baa2617 2022-02-16 op int ret = 0, done = 1;
412 8ff9231f 2022-02-16 op char **files;
414 3baa2617 2022-02-16 op switch (res->action) {
416 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
417 00f500ff 2022-02-19 op if (verbose) {
418 00f500ff 2022-02-19 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
424 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
427 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
428 00f500ff 2022-02-19 op if (verbose) {
429 00f500ff 2022-02-19 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
435 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
437 3baa2617 2022-02-16 op case RESTART:
438 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_RESTART, 0, 0, -1, NULL, 0);
439 00f500ff 2022-02-19 op if (verbose) {
440 00f500ff 2022-02-19 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
447 8ff9231f 2022-02-16 op files = res->files;
448 3baa2617 2022-02-16 op ret = enqueue_tracks(res->files);
451 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
455 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
459 bb3f279f 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
462 af27e631 2022-02-17 op imsg_compose(ibuf, IMSG_CTL_NEXT, 0, 0, -1, NULL, 0);
463 00f500ff 2022-02-19 op if (verbose) {
464 00f500ff 2022-02-19 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
470 af27e631 2022-02-17 op imsg_compose(ibuf, IMSG_CTL_PREV, 0, 0, -1, NULL, 0);
471 00f500ff 2022-02-19 op if (verbose) {
472 00f500ff 2022-02-19 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1,
479 7a427ecd 2022-02-17 op imsg_compose(ibuf, IMSG_CTL_BEGIN, 0, 0, -1, NULL, 0);
483 a913de21 2022-02-17 op ret = jump_req(res->file);
486 310ef57c 2022-02-19 op imsg_compose(ibuf, IMSG_CTL_REPEAT, 0, 0, -1,
487 310ef57c 2022-02-19 op &res->rep, sizeof(res->rep));
489 6b47a39f 2022-02-21 op case MONITOR:
491 6b47a39f 2022-02-21 op imsg_compose(ibuf, IMSG_CTL_MONITOR, 0, 0, -1,
495 3baa2617 2022-02-16 op /* action not expected */
496 3baa2617 2022-02-16 op fatalx("invalid action %u", res->action);
500 3baa2617 2022-02-16 op if (ret != 0)
503 3baa2617 2022-02-16 op imsg_flush(ibuf);
505 3baa2617 2022-02-16 op while (!done) {
506 3baa2617 2022-02-16 op if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
507 3baa2617 2022-02-16 op fatalx("imsg_read error");
509 3baa2617 2022-02-16 op fatalx("pipe closed");
511 3baa2617 2022-02-16 op while (!done) {
512 3baa2617 2022-02-16 op if ((n = imsg_get(ibuf, &imsg)) == -1)
513 3baa2617 2022-02-16 op fatalx("imsg_get error");
517 3baa2617 2022-02-16 op switch (res->action) {
519 8ff9231f 2022-02-16 op done = show_add(&imsg, &ret, &files);
522 8e916714 2022-02-17 op done = show_complete(res, &imsg, &ret);
526 146307bd 2022-02-17 op case RESTART:
531 bb3f279f 2022-02-16 op done = show_status(&imsg, &ret);
534 7a427ecd 2022-02-17 op done = show_load(res, &imsg, &ret);
536 6b47a39f 2022-02-21 op case MONITOR:
537 6b47a39f 2022-02-21 op done = show_monitor(&imsg, &ret);
544 3baa2617 2022-02-16 op imsg_free(&imsg);
553 3baa2617 2022-02-16 op ctl_noarg(struct parse_result *res, int argc, char **argv)
557 7e29fc4a 2022-03-03 op while ((ch = getopt(argc, argv, "")) != -1)
558 7e29fc4a 2022-03-03 op ctl_usage(res->ctl);
559 7e29fc4a 2022-03-03 op argc -= optind;
560 7e29fc4a 2022-03-03 op argv += optind;
562 acaf7eb2 2022-03-05 op if (argc > 0)
563 3baa2617 2022-02-16 op ctl_usage(res->ctl);
565 3baa2617 2022-02-16 op return ctlaction(res);
569 3baa2617 2022-02-16 op ctl_add(struct parse_result *res, int argc, char **argv)
573 7e29fc4a 2022-03-03 op while ((ch = getopt(argc, argv, "")) != -1)
574 3baa2617 2022-02-16 op ctl_usage(res->ctl);
575 7e29fc4a 2022-03-03 op argc -= optind;
576 7e29fc4a 2022-03-03 op argv += optind;
578 7e29fc4a 2022-03-03 op if (argc == 0)
579 7e29fc4a 2022-03-03 op ctl_usage(res->ctl);
580 7e29fc4a 2022-03-03 op res->files = argv;
582 14be015f 2022-02-17 op if (pledge("stdio rpath", NULL) == -1)
583 14be015f 2022-02-17 op fatal("pledge");
585 3baa2617 2022-02-16 op return ctlaction(res);
589 8e916714 2022-02-17 op ctl_show(struct parse_result *res, int argc, char **argv)
593 8e916714 2022-02-17 op while ((ch = getopt(argc, argv, "p")) != -1) {
594 8e916714 2022-02-17 op switch (ch) {
596 8e916714 2022-02-17 op res->pretty = 1;
599 8e916714 2022-02-17 op ctl_usage(res->ctl);
603 8e916714 2022-02-17 op return ctlaction(res);
607 14be015f 2022-02-17 op ctl_load(struct parse_result *res, int argc, char **argv)
611 7e29fc4a 2022-03-03 op while ((ch = getopt(argc, argv, "")) != -1)
612 7e29fc4a 2022-03-03 op ctl_usage(res->ctl);
613 7e29fc4a 2022-03-03 op argc -= optind;
614 7e29fc4a 2022-03-03 op argv += optind;
616 7e29fc4a 2022-03-03 op if (argc == 0)
617 ec1fb0c7 2022-02-17 op res->file = NULL;
618 7e29fc4a 2022-03-03 op else if (argc == 1)
619 7e29fc4a 2022-03-03 op res->file = argv[0];
621 14be015f 2022-02-17 op ctl_usage(res->ctl);
623 14be015f 2022-02-17 op if (pledge("stdio rpath", NULL) == -1)
624 14be015f 2022-02-17 op fatal("pledge");
626 7a427ecd 2022-02-17 op return ctlaction(res);
630 a913de21 2022-02-17 op ctl_jump(struct parse_result *res, int argc, char **argv)
634 a913de21 2022-02-17 op while ((ch = getopt(argc, argv, "")) != -1)
635 a913de21 2022-02-17 op ctl_usage(res->ctl);
636 a913de21 2022-02-17 op argc -= optind;
637 a913de21 2022-02-17 op argv += optind;
639 a913de21 2022-02-17 op if (argc != 1)
640 a913de21 2022-02-17 op ctl_usage(res->ctl);
642 a913de21 2022-02-17 op res->file = argv[0];
643 310ef57c 2022-02-19 op return ctlaction(res);
647 310ef57c 2022-02-19 op ctl_repeat(struct parse_result *res, int argc, char **argv)
651 310ef57c 2022-02-19 op while ((ch = getopt(argc, argv, "")) != -1)
652 310ef57c 2022-02-19 op ctl_usage(res->ctl);
653 310ef57c 2022-02-19 op argc -= optind;
654 310ef57c 2022-02-19 op argv += optind;
656 310ef57c 2022-02-19 op if (argc != 2)
657 310ef57c 2022-02-19 op ctl_usage(res->ctl);
659 310ef57c 2022-02-19 op if (!strcmp(argv[1], "on"))
661 310ef57c 2022-02-19 op else if (!strcmp(argv[1], "off"))
664 310ef57c 2022-02-19 op ctl_usage(res->ctl);
666 310ef57c 2022-02-19 op res->rep.repeat_one = -1;
667 310ef57c 2022-02-19 op res->rep.repeat_all = -1;
668 310ef57c 2022-02-19 op if (!strcmp(argv[0], "one"))
669 310ef57c 2022-02-19 op res->rep.repeat_one = b;
670 310ef57c 2022-02-19 op else if (!strcmp(argv[0], "all"))
671 310ef57c 2022-02-19 op res->rep.repeat_all = b;
673 310ef57c 2022-02-19 op ctl_usage(res->ctl);
675 a913de21 2022-02-17 op return ctlaction(res);
679 1d673950 2022-03-03 op ctl_get_lock(const char *lockfile)
683 1d673950 2022-03-03 op if ((lockfd = open(lockfile, O_WRONLY|O_CREAT, 0600)) == -1) {
684 1d673950 2022-03-03 op log_debug("open failed: %s", strerror(errno));
688 1d673950 2022-03-03 op if (flock(lockfd, LOCK_EX|LOCK_NB) == -1) {
689 1d673950 2022-03-03 op log_debug("flock failed: %s", strerror(errno));
690 1d673950 2022-03-03 op if (errno != EAGAIN)
692 1d673950 2022-03-03 op while (flock(lockfd, LOCK_EX) == -1 && errno == EINTR)
694 1d673950 2022-03-03 op close(lockfd);
697 1d673950 2022-03-03 op log_debug("flock succeeded");
699 1d673950 2022-03-03 op return lockfd;
703 1d673950 2022-03-03 op ctl_connect(void)
705 1d673950 2022-03-03 op struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
706 1d673950 2022-03-03 op struct sockaddr_un sa;
708 1d673950 2022-03-03 op int fd, lockfd = -1, locked = 0, spawned = 0;
709 1d673950 2022-03-03 op char *lockfile = NULL;
711 1d673950 2022-03-03 op memset(&sa, 0, sizeof(sa));
712 1d673950 2022-03-03 op sa.sun_family = AF_UNIX;
713 1d673950 2022-03-03 op size = strlcpy(sa.sun_path, csock, sizeof(sa.sun_path));
714 1d673950 2022-03-03 op if (size >= sizeof(sa.sun_path)) {
715 1d673950 2022-03-03 op errno = ENAMETOOLONG;
720 1d673950 2022-03-03 op if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
723 1d673950 2022-03-03 op if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
724 1d673950 2022-03-03 op log_debug("connection failed: %s", strerror(errno));
725 1d673950 2022-03-03 op if (errno != ECONNREFUSED && errno != ENOENT)
729 1d673950 2022-03-03 op if (!locked) {
730 1d673950 2022-03-03 op xasprintf(&lockfile, "%s.lock", csock);
731 1d673950 2022-03-03 op if ((lockfd = ctl_get_lock(lockfile)) < 0) {
732 1d673950 2022-03-03 op log_debug("didn't get the lock (%d)", lockfd);
734 1d673950 2022-03-03 op free(lockfile);
735 1d673950 2022-03-03 op lockfile = NULL;
737 1d673950 2022-03-03 op if (lockfd == -1)
742 1d673950 2022-03-03 op * Always retry at least once, even if we got
743 1d673950 2022-03-03 op * the lock, because another client could have
744 1d673950 2022-03-03 op * taken the lock, started the server and released
745 1d673950 2022-03-03 op * the lock between our connect() and flock()
751 1d673950 2022-03-03 op if (!spawned) {
752 1d673950 2022-03-03 op log_debug("spawning the daemon");
753 1d673950 2022-03-03 op spawn_daemon();
757 1d673950 2022-03-03 op nanosleep(&ts, NULL);
761 1d673950 2022-03-03 op if (locked && lockfd >= 0) {
762 1d673950 2022-03-03 op unlink(lockfile);
763 1d673950 2022-03-03 op free(lockfile);
764 1d673950 2022-03-03 op close(lockfd);
769 1d673950 2022-03-03 op if (locked) {
770 1d673950 2022-03-03 op free(lockfile);
771 1d673950 2022-03-03 op close(lockfd);
778 fea541a8 2022-02-16 op ctl(int argc, char **argv)
780 1d673950 2022-03-03 op int ctl_sock;
782 fea541a8 2022-02-16 op log_init(1, LOG_DAEMON);
783 fea541a8 2022-02-16 op log_setverbose(verbose);
785 1d673950 2022-03-03 op if ((ctl_sock = ctl_connect()) == -1)
786 1d673950 2022-03-03 op fatal("can't connect");
788 fea541a8 2022-02-16 op if (ctl_sock == -1)
789 fea541a8 2022-02-16 op fatalx("failed to connect to the daemon");
791 3baa2617 2022-02-16 op ibuf = xmalloc(sizeof(*ibuf));
792 3baa2617 2022-02-16 op imsg_init(ibuf, ctl_sock);
794 3baa2617 2022-02-16 op optreset = 1;
797 3baa2617 2022-02-16 op exit(parse(argc, argv));