Blame


1 3baa2617 2022-02-16 op /*
2 3baa2617 2022-02-16 op * Copyright (c) 2022 Omar Polo <op@openbsd.org>
3 3baa2617 2022-02-16 op *
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.
7 3baa2617 2022-02-16 op *
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.
15 3baa2617 2022-02-16 op */
16 3baa2617 2022-02-16 op
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>
22 3baa2617 2022-02-16 op
23 3baa2617 2022-02-16 op #include <errno.h>
24 3baa2617 2022-02-16 op #include <event.h>
25 3baa2617 2022-02-16 op #include <limits.h>
26 3baa2617 2022-02-16 op #include <sndio.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>
34 3baa2617 2022-02-16 op
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"
39 3baa2617 2022-02-16 op
40 3baa2617 2022-02-16 op static struct imsgbuf *ibuf;
41 3baa2617 2022-02-16 op
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 3baa2617 2022-02-16 op
45 3baa2617 2022-02-16 op struct ctl_command ctl_commands[] = {
46 3baa2617 2022-02-16 op { "play", PLAY, ctl_noarg, "" },
47 3baa2617 2022-02-16 op { "pause", PAUSE, ctl_noarg, "" },
48 3baa2617 2022-02-16 op { "toggle", TOGGLE, ctl_noarg, "" },
49 3baa2617 2022-02-16 op { "stop", STOP, ctl_noarg, "" },
50 3baa2617 2022-02-16 op { "restart", RESTART, ctl_noarg, "" },
51 3baa2617 2022-02-16 op { "add", ADD, ctl_add, "files...", 1 },
52 3baa2617 2022-02-16 op { "flush", FLUSH, ctl_noarg, "" },
53 d980494c 2022-02-16 op { "show", SHOW, ctl_noarg, "" },
54 bb3f279f 2022-02-16 op { "status", STATUS, ctl_noarg, "" },
55 3baa2617 2022-02-16 op { NULL },
56 3baa2617 2022-02-16 op };
57 3baa2617 2022-02-16 op
58 3baa2617 2022-02-16 op __dead void
59 3baa2617 2022-02-16 op usage(void)
60 3baa2617 2022-02-16 op {
61 3baa2617 2022-02-16 op fprintf(stderr, "usage: %s [-dv] [-s socket]\n", getprogname());
62 3baa2617 2022-02-16 op fprintf(stderr, "%s version %s\n", getprogname(), AMUSED_VERSION);
63 3baa2617 2022-02-16 op exit(1);
64 3baa2617 2022-02-16 op }
65 3baa2617 2022-02-16 op
66 3baa2617 2022-02-16 op static __dead void
67 3baa2617 2022-02-16 op ctl_usage(struct ctl_command *ctl)
68 3baa2617 2022-02-16 op {
69 3baa2617 2022-02-16 op fprintf(stderr, "usage: %s [-v] [-s socket] %s %s\n", getprogname(),
70 3baa2617 2022-02-16 op ctl->name, ctl->usage);
71 3baa2617 2022-02-16 op exit(1);
72 3baa2617 2022-02-16 op }
73 3baa2617 2022-02-16 op
74 3baa2617 2022-02-16 op static int
75 3baa2617 2022-02-16 op parse(int argc, char **argv)
76 3baa2617 2022-02-16 op {
77 3baa2617 2022-02-16 op struct ctl_command *ctl = NULL;
78 3baa2617 2022-02-16 op struct parse_result res;
79 3baa2617 2022-02-16 op int i, status;
80 3baa2617 2022-02-16 op
81 3baa2617 2022-02-16 op memset(&res, 0, sizeof(res));
82 3baa2617 2022-02-16 op
83 3baa2617 2022-02-16 op for (i = 0; ctl_commands[i].name != NULL; ++i) {
84 3baa2617 2022-02-16 op if (strncmp(ctl_commands[i].name, argv[0], strlen(argv[0]))
85 3baa2617 2022-02-16 op == 0) {
86 3baa2617 2022-02-16 op if (ctl != NULL) {
87 3baa2617 2022-02-16 op fprintf(stderr,
88 3baa2617 2022-02-16 op "ambiguous argument: %s\n", argv[0]);
89 3baa2617 2022-02-16 op usage();
90 3baa2617 2022-02-16 op }
91 3baa2617 2022-02-16 op ctl = &ctl_commands[i];
92 3baa2617 2022-02-16 op }
93 3baa2617 2022-02-16 op }
94 3baa2617 2022-02-16 op
95 3baa2617 2022-02-16 op if (ctl == NULL) {
96 3baa2617 2022-02-16 op fprintf(stderr, "unknown argument: %s\n", argv[0]);
97 3baa2617 2022-02-16 op usage();
98 3baa2617 2022-02-16 op }
99 3baa2617 2022-02-16 op
100 3baa2617 2022-02-16 op res.action = ctl->action;
101 3baa2617 2022-02-16 op res.ctl = ctl;
102 3baa2617 2022-02-16 op
103 3baa2617 2022-02-16 op if (!ctl->has_pledge) {
104 3baa2617 2022-02-16 op /* pledge(2) default if command doesn't have its own */
105 3baa2617 2022-02-16 op if (pledge("stdio", NULL) == -1)
106 3baa2617 2022-02-16 op fatal("pledge");
107 3baa2617 2022-02-16 op }
108 3baa2617 2022-02-16 op
109 3baa2617 2022-02-16 op status = ctl->main(&res, argc, argv);
110 3baa2617 2022-02-16 op close(ibuf->fd);
111 3baa2617 2022-02-16 op free(ibuf);
112 3baa2617 2022-02-16 op return status;
113 3baa2617 2022-02-16 op }
114 3baa2617 2022-02-16 op
115 3baa2617 2022-02-16 op static int
116 3baa2617 2022-02-16 op enqueue_tracks(char **files)
117 3baa2617 2022-02-16 op {
118 3baa2617 2022-02-16 op char res[PATH_MAX];
119 3baa2617 2022-02-16 op int enq = 0;
120 3baa2617 2022-02-16 op
121 3baa2617 2022-02-16 op for (; *files != NULL; ++files) {
122 3baa2617 2022-02-16 op memset(&res, 0, sizeof(res));
123 3baa2617 2022-02-16 op if (realpath(*files, res) == NULL) {
124 3baa2617 2022-02-16 op log_warn("realpath %s", *files);
125 3baa2617 2022-02-16 op continue;
126 3baa2617 2022-02-16 op }
127 3baa2617 2022-02-16 op
128 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
129 3baa2617 2022-02-16 op res, sizeof(res));
130 3baa2617 2022-02-16 op enq++;
131 3baa2617 2022-02-16 op }
132 3baa2617 2022-02-16 op
133 3baa2617 2022-02-16 op return enq == 0;
134 3baa2617 2022-02-16 op }
135 3baa2617 2022-02-16 op
136 8ff9231f 2022-02-16 op static void
137 8ff9231f 2022-02-16 op print_error_message(const char *prfx, struct imsg *imsg)
138 8ff9231f 2022-02-16 op {
139 8ff9231f 2022-02-16 op size_t datalen;
140 8ff9231f 2022-02-16 op char *msg;
141 8ff9231f 2022-02-16 op
142 8ff9231f 2022-02-16 op datalen = IMSG_DATA_SIZE(*imsg);
143 8ff9231f 2022-02-16 op if ((msg = calloc(1, datalen)) == NULL)
144 8ff9231f 2022-02-16 op fatal("calloc %zu", datalen);
145 8ff9231f 2022-02-16 op memcpy(msg, imsg->data, datalen);
146 8ff9231f 2022-02-16 op if (datalen == 0 || msg[datalen-1] != '\0')
147 8ff9231f 2022-02-16 op fatalx("malformed error message");
148 8ff9231f 2022-02-16 op
149 8ff9231f 2022-02-16 op log_warnx("%s: %s", prfx, msg);
150 8ff9231f 2022-02-16 op free(msg);
151 8ff9231f 2022-02-16 op }
152 8ff9231f 2022-02-16 op
153 3baa2617 2022-02-16 op static int
154 8ff9231f 2022-02-16 op show_add(struct imsg *imsg, int *ret, char ***files)
155 8ff9231f 2022-02-16 op {
156 8ff9231f 2022-02-16 op if (**files == NULL) {
157 8ff9231f 2022-02-16 op log_warnx("received more replies than file sent");
158 8ff9231f 2022-02-16 op *ret = 1;
159 8ff9231f 2022-02-16 op return 1;
160 8ff9231f 2022-02-16 op }
161 8ff9231f 2022-02-16 op
162 8ff9231f 2022-02-16 op if (imsg->hdr.type == IMSG_CTL_ERR)
163 8ff9231f 2022-02-16 op print_error_message(**files, imsg);
164 8ff9231f 2022-02-16 op else if (imsg->hdr.type == IMSG_CTL_ADD)
165 8ff9231f 2022-02-16 op log_debug("enqueued %s", **files);
166 8ff9231f 2022-02-16 op else
167 8ff9231f 2022-02-16 op fatalx("got invalid message %d", imsg->hdr.type);
168 8ff9231f 2022-02-16 op
169 8ff9231f 2022-02-16 op (*files)++;
170 8ff9231f 2022-02-16 op return (**files) == NULL;
171 8ff9231f 2022-02-16 op }
172 8ff9231f 2022-02-16 op
173 8ff9231f 2022-02-16 op static int
174 3baa2617 2022-02-16 op show_complete(struct imsg *imsg, int *ret)
175 3baa2617 2022-02-16 op {
176 3baa2617 2022-02-16 op size_t datalen;
177 3baa2617 2022-02-16 op char path[PATH_MAX];
178 3baa2617 2022-02-16 op
179 8ff9231f 2022-02-16 op if (imsg->hdr.type == IMSG_CTL_ERR) {
180 8ff9231f 2022-02-16 op print_error_message("show failed", imsg);
181 8ff9231f 2022-02-16 op *ret = 1;
182 8ff9231f 2022-02-16 op return 1;
183 8ff9231f 2022-02-16 op }
184 8ff9231f 2022-02-16 op
185 3baa2617 2022-02-16 op datalen = IMSG_DATA_SIZE(*imsg);
186 3baa2617 2022-02-16 op if (datalen == 0)
187 3baa2617 2022-02-16 op return 1;
188 3baa2617 2022-02-16 op
189 3baa2617 2022-02-16 op if (datalen != sizeof(path))
190 3baa2617 2022-02-16 op fatalx("%s: data size mismatch", __func__);
191 3baa2617 2022-02-16 op memcpy(path, imsg->data, sizeof(path));
192 3baa2617 2022-02-16 op if (path[datalen-1] != '\0')
193 3baa2617 2022-02-16 op fatalx("%s: data corrupted?", __func__);
194 3baa2617 2022-02-16 op
195 3baa2617 2022-02-16 op printf("%s\n", path);
196 3baa2617 2022-02-16 op return 0;
197 bb3f279f 2022-02-16 op }
198 bb3f279f 2022-02-16 op
199 bb3f279f 2022-02-16 op static int
200 bb3f279f 2022-02-16 op show_status(struct imsg *imsg, int *ret)
201 bb3f279f 2022-02-16 op {
202 bb3f279f 2022-02-16 op struct player_status s;
203 bb3f279f 2022-02-16 op size_t datalen;
204 bb3f279f 2022-02-16 op
205 bb3f279f 2022-02-16 op if (imsg->hdr.type == IMSG_CTL_ERR) {
206 bb3f279f 2022-02-16 op print_error_message("show failed", imsg);
207 bb3f279f 2022-02-16 op *ret = 1;
208 bb3f279f 2022-02-16 op return 1;
209 bb3f279f 2022-02-16 op }
210 bb3f279f 2022-02-16 op
211 bb3f279f 2022-02-16 op if (imsg->hdr.type != IMSG_CTL_STATUS)
212 bb3f279f 2022-02-16 op fatalx("%s: got wrong reply", __func__);
213 bb3f279f 2022-02-16 op
214 bb3f279f 2022-02-16 op datalen = IMSG_DATA_SIZE(*imsg);
215 bb3f279f 2022-02-16 op if (datalen != sizeof(s))
216 bb3f279f 2022-02-16 op fatalx("%s: data size mismatch", __func__);
217 bb3f279f 2022-02-16 op memcpy(&s, imsg->data, sizeof(s));
218 bb3f279f 2022-02-16 op if (s.path[sizeof(s.path)-1] != '\0')
219 bb3f279f 2022-02-16 op fatalx("%s: data corrupted?", __func__);
220 bb3f279f 2022-02-16 op
221 bb3f279f 2022-02-16 op switch (s.status) {
222 bb3f279f 2022-02-16 op case STATE_STOPPED:
223 bb3f279f 2022-02-16 op printf("stopped ");
224 bb3f279f 2022-02-16 op break;
225 bb3f279f 2022-02-16 op case STATE_PLAYING:
226 bb3f279f 2022-02-16 op printf("playing ");
227 bb3f279f 2022-02-16 op break;
228 bb3f279f 2022-02-16 op case STATE_PAUSED:
229 bb3f279f 2022-02-16 op printf("paused ");
230 bb3f279f 2022-02-16 op break;
231 bb3f279f 2022-02-16 op default:
232 bb3f279f 2022-02-16 op printf("unknown ");
233 bb3f279f 2022-02-16 op break;
234 bb3f279f 2022-02-16 op }
235 bb3f279f 2022-02-16 op
236 bb3f279f 2022-02-16 op printf("%s\n", s.path);
237 bb3f279f 2022-02-16 op return 1;
238 3baa2617 2022-02-16 op }
239 3baa2617 2022-02-16 op
240 3baa2617 2022-02-16 op static int
241 3baa2617 2022-02-16 op ctlaction(struct parse_result *res)
242 3baa2617 2022-02-16 op {
243 3baa2617 2022-02-16 op struct imsg imsg;
244 3baa2617 2022-02-16 op ssize_t n;
245 3baa2617 2022-02-16 op int ret = 0, done = 1;
246 8ff9231f 2022-02-16 op char **files;
247 3baa2617 2022-02-16 op
248 3baa2617 2022-02-16 op switch (res->action) {
249 3baa2617 2022-02-16 op case PLAY:
250 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_PLAY, 0, 0, -1, NULL, 0);
251 3baa2617 2022-02-16 op break;
252 3baa2617 2022-02-16 op case PAUSE:
253 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_PAUSE, 0, 0, -1, NULL, 0);
254 3baa2617 2022-02-16 op break;
255 3baa2617 2022-02-16 op case TOGGLE:
256 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_TOGGLE_PLAY, 0, 0, -1, NULL, 0);
257 3baa2617 2022-02-16 op break;
258 3baa2617 2022-02-16 op case STOP:
259 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_STOP, 0, 0, -1, NULL, 0);
260 3baa2617 2022-02-16 op break;
261 3baa2617 2022-02-16 op case RESTART:
262 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_RESTART, 0, 0, -1, NULL, 0);
263 3baa2617 2022-02-16 op break;
264 3baa2617 2022-02-16 op case ADD:
265 8ff9231f 2022-02-16 op done = 0;
266 8ff9231f 2022-02-16 op files = res->files;
267 3baa2617 2022-02-16 op ret = enqueue_tracks(res->files);
268 3baa2617 2022-02-16 op break;
269 3baa2617 2022-02-16 op case FLUSH:
270 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_FLUSH, 0, 0, -1, NULL, 0);
271 3baa2617 2022-02-16 op break;
272 3baa2617 2022-02-16 op case SHOW:
273 3baa2617 2022-02-16 op done = 0;
274 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_SHOW, 0, 0, -1, NULL, 0);
275 3baa2617 2022-02-16 op break;
276 bb3f279f 2022-02-16 op case STATUS:
277 bb3f279f 2022-02-16 op done = 0;
278 bb3f279f 2022-02-16 op imsg_compose(ibuf, IMSG_CTL_STATUS, 0, 0, -1, NULL, 0);
279 bb3f279f 2022-02-16 op break;
280 3baa2617 2022-02-16 op case NONE:
281 3baa2617 2022-02-16 op /* action not expected */
282 3baa2617 2022-02-16 op fatalx("invalid action %u", res->action);
283 3baa2617 2022-02-16 op break;
284 3baa2617 2022-02-16 op }
285 3baa2617 2022-02-16 op
286 3baa2617 2022-02-16 op if (ret != 0)
287 3baa2617 2022-02-16 op goto end;
288 3baa2617 2022-02-16 op
289 3baa2617 2022-02-16 op imsg_flush(ibuf);
290 3baa2617 2022-02-16 op
291 3baa2617 2022-02-16 op while (!done) {
292 3baa2617 2022-02-16 op if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
293 3baa2617 2022-02-16 op fatalx("imsg_read error");
294 3baa2617 2022-02-16 op if (n == 0)
295 3baa2617 2022-02-16 op fatalx("pipe closed");
296 3baa2617 2022-02-16 op
297 3baa2617 2022-02-16 op while (!done) {
298 3baa2617 2022-02-16 op if ((n = imsg_get(ibuf, &imsg)) == -1)
299 3baa2617 2022-02-16 op fatalx("imsg_get error");
300 3baa2617 2022-02-16 op if (n == 0)
301 3baa2617 2022-02-16 op break;
302 3baa2617 2022-02-16 op
303 3baa2617 2022-02-16 op switch (res->action) {
304 8ff9231f 2022-02-16 op case ADD:
305 8ff9231f 2022-02-16 op done = show_add(&imsg, &ret, &files);
306 8ff9231f 2022-02-16 op break;
307 3baa2617 2022-02-16 op case SHOW:
308 3baa2617 2022-02-16 op done = show_complete(&imsg, &ret);
309 3baa2617 2022-02-16 op break;
310 bb3f279f 2022-02-16 op case STATUS:
311 bb3f279f 2022-02-16 op done = show_status(&imsg, &ret);
312 bb3f279f 2022-02-16 op break;
313 3baa2617 2022-02-16 op default:
314 3baa2617 2022-02-16 op done = 1;
315 3baa2617 2022-02-16 op break;
316 3baa2617 2022-02-16 op }
317 3baa2617 2022-02-16 op
318 3baa2617 2022-02-16 op imsg_free(&imsg);
319 3baa2617 2022-02-16 op }
320 3baa2617 2022-02-16 op }
321 3baa2617 2022-02-16 op
322 3baa2617 2022-02-16 op end:
323 3baa2617 2022-02-16 op return ret;
324 3baa2617 2022-02-16 op }
325 3baa2617 2022-02-16 op
326 3baa2617 2022-02-16 op int
327 3baa2617 2022-02-16 op ctl_noarg(struct parse_result *res, int argc, char **argv)
328 3baa2617 2022-02-16 op {
329 3baa2617 2022-02-16 op if (argc != 1)
330 3baa2617 2022-02-16 op ctl_usage(res->ctl);
331 3baa2617 2022-02-16 op return ctlaction(res);
332 3baa2617 2022-02-16 op }
333 3baa2617 2022-02-16 op
334 3baa2617 2022-02-16 op int
335 3baa2617 2022-02-16 op ctl_add(struct parse_result *res, int argc, char **argv)
336 3baa2617 2022-02-16 op {
337 3baa2617 2022-02-16 op if (argc < 2)
338 3baa2617 2022-02-16 op ctl_usage(res->ctl);
339 3baa2617 2022-02-16 op res->files = argv+1;
340 3baa2617 2022-02-16 op return ctlaction(res);
341 3baa2617 2022-02-16 op }
342 3baa2617 2022-02-16 op
343 fea541a8 2022-02-16 op static int
344 fea541a8 2022-02-16 op sockconn(void)
345 3baa2617 2022-02-16 op {
346 fea541a8 2022-02-16 op struct sockaddr_un sun;
347 fea541a8 2022-02-16 op int sock, saved_errno;
348 3baa2617 2022-02-16 op
349 fea541a8 2022-02-16 op if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
350 3baa2617 2022-02-16 op fatal("socket");
351 3baa2617 2022-02-16 op
352 3baa2617 2022-02-16 op memset(&sun, 0, sizeof(sun));
353 3baa2617 2022-02-16 op sun.sun_family = AF_UNIX;
354 3baa2617 2022-02-16 op strlcpy(sun.sun_path, csock, sizeof(sun.sun_path));
355 fea541a8 2022-02-16 op if (connect(sock, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
356 fea541a8 2022-02-16 op saved_errno = errno;
357 fea541a8 2022-02-16 op close(sock);
358 fea541a8 2022-02-16 op errno = saved_errno;
359 fea541a8 2022-02-16 op return -1;
360 fea541a8 2022-02-16 op }
361 3baa2617 2022-02-16 op
362 fea541a8 2022-02-16 op return sock;
363 fea541a8 2022-02-16 op }
364 fea541a8 2022-02-16 op
365 fea541a8 2022-02-16 op __dead void
366 fea541a8 2022-02-16 op ctl(int argc, char **argv)
367 fea541a8 2022-02-16 op {
368 fea541a8 2022-02-16 op int ctl_sock, i = 0;
369 3baa2617 2022-02-16 op
370 fea541a8 2022-02-16 op log_init(1, LOG_DAEMON);
371 fea541a8 2022-02-16 op log_setverbose(verbose);
372 fea541a8 2022-02-16 op
373 fea541a8 2022-02-16 op do {
374 fea541a8 2022-02-16 op struct timespec ts = { 0, 50000000 }; /* 0.05 seconds */
375 fea541a8 2022-02-16 op
376 fea541a8 2022-02-16 op if ((ctl_sock = sockconn()) != -1)
377 fea541a8 2022-02-16 op break;
378 fea541a8 2022-02-16 op if (errno != ENOENT && errno != ECONNREFUSED)
379 fea541a8 2022-02-16 op fatal("connect %s", csock);
380 fea541a8 2022-02-16 op
381 fea541a8 2022-02-16 op if (i == 0)
382 fea541a8 2022-02-16 op spawn_daemon();
383 fea541a8 2022-02-16 op
384 fea541a8 2022-02-16 op nanosleep(&ts, NULL);
385 fea541a8 2022-02-16 op } while (++i < 20);
386 fea541a8 2022-02-16 op
387 fea541a8 2022-02-16 op if (ctl_sock == -1)
388 fea541a8 2022-02-16 op fatalx("failed to connect to the daemon");
389 fea541a8 2022-02-16 op
390 3baa2617 2022-02-16 op ibuf = xmalloc(sizeof(*ibuf));
391 3baa2617 2022-02-16 op imsg_init(ibuf, ctl_sock);
392 3baa2617 2022-02-16 op
393 3baa2617 2022-02-16 op optreset = 1;
394 3baa2617 2022-02-16 op optind = 1;
395 3baa2617 2022-02-16 op
396 3baa2617 2022-02-16 op exit(parse(argc, argv));
397 3baa2617 2022-02-16 op }