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