Line | Branch | Exec | Source |
---|---|---|---|
1 | #include <errno.h> | ||
2 | #include <poll.h> | ||
3 | #include <stddef.h> | ||
4 | #include <stdio.h> | ||
5 | #include <string.h> | ||
6 | #include <unistd.h> | ||
7 | #include "spawn.h" | ||
8 | #include "command/error.h" | ||
9 | #include "regexp.h" | ||
10 | #include "util/debug.h" | ||
11 | #include "util/fd.h" | ||
12 | #include "util/fork-exec.h" | ||
13 | #include "util/str-util.h" | ||
14 | #include "util/strtonum.h" | ||
15 | #include "util/xmalloc.h" | ||
16 | #include "util/xreadwrite.h" | ||
17 | #include "util/xstdio.h" | ||
18 | |||
19 | ✗ | static void handle_error_msg(const Compiler *c, MessageList *msgs, char *str) | |
20 | { | ||
21 | ✗ | if (str[0] == '\0' || str[0] == '\n') { | |
22 | return; | ||
23 | } | ||
24 | |||
25 | ✗ | size_t str_len = str_replace_byte(str, '\t', ' '); | |
26 | ✗ | if (str[str_len - 1] == '\n') { | |
27 | ✗ | str[--str_len] = '\0'; | |
28 | } | ||
29 | |||
30 | ✗ | for (size_t i = 0, n = c->error_formats.count; i < n; i++) { | |
31 | ✗ | const ErrorFormat *p = c->error_formats.ptrs[i]; | |
32 | ✗ | regmatch_t m[ERRORFMT_CAPTURE_MAX]; | |
33 | ✗ | if (!regexp_exec(&p->re, str, str_len, ARRAYLEN(m), m, 0)) { | |
34 | ✗ | continue; | |
35 | } | ||
36 | ✗ | if (p->ignore) { | |
37 | ✗ | return; | |
38 | } | ||
39 | |||
40 | ✗ | int mi = (int)p->capture_index[ERRFMT_MESSAGE]; | |
41 | ✗ | if (m[mi].rm_so < 0) { | |
42 | ✗ | mi = 0; | |
43 | } | ||
44 | |||
45 | ✗ | Message *msg = new_message(str + m[mi].rm_so, m[mi].rm_eo - m[mi].rm_so); | |
46 | ✗ | msg->loc = new_file_location(NULL, 0, 0, 0); | |
47 | |||
48 | ✗ | int fi = (int)p->capture_index[ERRFMT_FILE]; | |
49 | ✗ | if (fi >= 0 && m[fi].rm_so >= 0) { | |
50 | ✗ | msg->loc->filename = xstrslice(str, m[fi].rm_so, m[fi].rm_eo); | |
51 | |||
52 | ✗ | unsigned long *const ptrs[] = { | |
53 | ✗ | [ERRFMT_LINE] = &msg->loc->line, | |
54 | ✗ | [ERRFMT_COLUMN] = &msg->loc->column, | |
55 | }; | ||
56 | |||
57 | ✗ | static_assert(ARRAYLEN(ptrs) == 3); | |
58 | ✗ | static_assert(ERRFMT_LINE == 1); | |
59 | ✗ | static_assert(ERRFMT_COLUMN == 2); | |
60 | |||
61 | ✗ | for (size_t j = ERRFMT_LINE; j < ARRAYLEN(ptrs); j++) { | |
62 | ✗ | int ci = (int)p->capture_index[j]; | |
63 | ✗ | if (ci >= 0 && m[ci].rm_so >= 0) { | |
64 | ✗ | size_t len = m[ci].rm_eo - m[ci].rm_so; | |
65 | ✗ | unsigned long val; | |
66 | ✗ | if (len == buf_parse_ulong(str + m[ci].rm_so, len, &val)) { | |
67 | ✗ | *ptrs[j] = val; | |
68 | } | ||
69 | } | ||
70 | } | ||
71 | } | ||
72 | |||
73 | ✗ | add_message(msgs, msg); | |
74 | ✗ | return; | |
75 | } | ||
76 | |||
77 | ✗ | add_message(msgs, new_message(str, str_len)); | |
78 | } | ||
79 | |||
80 | ✗ | static void read_errors(const Compiler *c, MessageList *msgs, int fd, bool quiet) | |
81 | { | ||
82 | ✗ | FILE *f = fdopen(fd, "r"); | |
83 | ✗ | if (unlikely(!f)) { | |
84 | ✗ | return; | |
85 | } | ||
86 | char line[4096]; | ||
87 | ✗ | while (xfgets(line, sizeof(line), f)) { | |
88 | ✗ | if (!quiet) { | |
89 | ✗ | xfputs(line, stderr); | |
90 | } | ||
91 | ✗ | handle_error_msg(c, msgs, line); | |
92 | } | ||
93 | ✗ | fclose(f); | |
94 | } | ||
95 | |||
96 | 11 | static void handle_piped_data(int f[3], SpawnContext *ctx) | |
97 | { | ||
98 | 11 | BUG_ON(f[0] < 0 && f[1] < 0 && f[2] < 0); | |
99 | 11 | BUG_ON(f[0] >= 0 && f[0] <= 2); | |
100 | 11 | BUG_ON(f[1] >= 0 && f[1] <= 2); | |
101 | 11 | BUG_ON(f[2] >= 0 && f[2] <= 2); | |
102 | |||
103 |
2/2✓ Branch 0 (12→13) taken 5 times.
✓ Branch 1 (12→16) taken 6 times.
|
11 | if (ctx->input.length == 0) { |
104 | 5 | xclose(f[0]); | |
105 | 5 | f[0] = -1; | |
106 |
4/4✓ Branch 0 (14→15) taken 2 times.
✓ Branch 1 (14→16) taken 3 times.
✓ Branch 2 (15→16) taken 1 times.
✓ Branch 3 (15→63) taken 1 times.
|
5 | if (f[1] < 0 && f[2] < 0) { |
107 | return; | ||
108 | } | ||
109 | } | ||
110 | |||
111 | 10 | struct pollfd fds[] = { | |
112 | 10 | {.fd = f[0], .events = POLLOUT}, | |
113 | {.fd = f[1], .events = POLLIN}, | ||
114 | {.fd = f[2], .events = POLLIN}, | ||
115 | }; | ||
116 | |||
117 | 10 | size_t wlen = 0; | |
118 | 25 | while (1) { | |
119 |
1/2✗ Branch 0 (19→20) not taken.
✓ Branch 1 (19→38) taken 25 times.
|
25 | if (unlikely(poll(fds, ARRAYLEN(fds), -1) < 0)) { |
120 | ✗ | if (errno == EINTR) { | |
121 | ✗ | continue; | |
122 | } | ||
123 | ✗ | error_msg_errno(ctx->ebuf, "poll"); | |
124 | ✗ | return; | |
125 | } | ||
126 | |||
127 |
2/2✓ Branch 0 (38→24) taken 50 times.
✓ Branch 1 (38→39) taken 25 times.
|
75 | for (size_t i = 0; i < ARRAYLEN(ctx->outputs); i++) { |
128 | 50 | struct pollfd *pfd = fds + i + 1; | |
129 |
2/2✓ Branch 0 (24→25) taken 11 times.
✓ Branch 1 (24→37) taken 39 times.
|
50 | if (pfd->revents & POLLIN) { |
130 | 11 | String *output = &ctx->outputs[i]; | |
131 | 11 | char *buf = string_reserve_space(output, 4096); | |
132 | 11 | ssize_t rc = xread(pfd->fd, buf, output->alloc - output->len); | |
133 |
1/2✗ Branch 0 (27→28) not taken.
✓ Branch 1 (27→30) taken 11 times.
|
11 | if (unlikely(rc < 0)) { |
134 | ✗ | error_msg_errno(ctx->ebuf, "read"); | |
135 | ✗ | return; | |
136 | } | ||
137 |
1/2✗ Branch 0 (30→31) not taken.
✓ Branch 1 (30→36) taken 11 times.
|
11 | if (rc == 0) { // EOF |
138 | ✗ | if (xclose(pfd->fd)) { | |
139 | ✗ | error_msg_errno(ctx->ebuf, "close"); | |
140 | ✗ | return; | |
141 | } | ||
142 | ✗ | pfd->fd = -1; | |
143 | ✗ | continue; | |
144 | } | ||
145 | 11 | output->len += rc; | |
146 | } | ||
147 | } | ||
148 | |||
149 |
2/2✓ Branch 0 (39→40) taken 4 times.
✓ Branch 1 (39→50) taken 21 times.
|
25 | if (fds[0].revents & POLLOUT) { |
150 | 4 | ssize_t rc = xwrite(fds[0].fd, ctx->input.data + wlen, ctx->input.length - wlen); | |
151 |
1/2✗ Branch 0 (41→42) not taken.
✓ Branch 1 (41→44) taken 4 times.
|
4 | if (unlikely(rc < 0)) { |
152 | ✗ | error_msg_errno(ctx->ebuf, "write"); | |
153 | ✗ | return; | |
154 | } | ||
155 | 4 | wlen += (size_t) rc; | |
156 |
1/2✓ Branch 0 (44→45) taken 4 times.
✗ Branch 1 (44→50) not taken.
|
4 | if (wlen == ctx->input.length) { |
157 |
1/2✗ Branch 0 (46→47) not taken.
✓ Branch 1 (46→49) taken 4 times.
|
4 | if (xclose(fds[0].fd)) { |
158 | ✗ | error_msg_errno(ctx->ebuf, "close"); | |
159 | ✗ | return; | |
160 | } | ||
161 | 4 | fds[0].fd = -1; | |
162 | } | ||
163 | } | ||
164 | |||
165 | 25 | size_t active_fds = ARRAYLEN(fds); | |
166 |
2/2✓ Branch 0 (61→51) taken 75 times.
✓ Branch 1 (61→62) taken 25 times.
|
100 | for (size_t i = 0; i < ARRAYLEN(fds); i++) { |
167 | 75 | int rev = fds[i].revents; | |
168 |
3/4✓ Branch 0 (51→52) taken 39 times.
✓ Branch 1 (51→53) taken 36 times.
✗ Branch 2 (52→53) not taken.
✓ Branch 3 (52→54) taken 39 times.
|
75 | if (fds[i].fd < 0 || rev & POLLNVAL) { |
169 | 36 | fds[i].fd = -1; | |
170 | 36 | active_fds--; | |
171 | 36 | continue; | |
172 | } | ||
173 |
3/4✓ Branch 0 (54→55) taken 39 times.
✗ Branch 1 (54→56) not taken.
✓ Branch 2 (55→56) taken 16 times.
✓ Branch 3 (55→60) taken 23 times.
|
39 | if (rev & POLLERR || (rev & (POLLHUP | POLLIN)) == POLLHUP) { |
174 |
1/2✗ Branch 0 (57→58) not taken.
✓ Branch 1 (57→59) taken 16 times.
|
16 | if (xclose(fds[i].fd)) { |
175 | ✗ | error_msg_errno(ctx->ebuf, "close"); | |
176 | } | ||
177 | 16 | fds[i].fd = -1; | |
178 | 16 | active_fds--; | |
179 | } | ||
180 | } | ||
181 |
2/2✓ Branch 0 (62→17) taken 15 times.
✓ Branch 1 (62→63) taken 10 times.
|
25 | if (active_fds == 0) { |
182 | return; | ||
183 | } | ||
184 | } | ||
185 | } | ||
186 | |||
187 | 36 | static int open_dev_null(ErrorBuffer *ebuf, int flags) | |
188 | { | ||
189 | 36 | int fd = xopen("/dev/null", flags | O_CLOEXEC, 0); | |
190 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 36 times.
|
36 | if (unlikely(fd < 0)) { |
191 | ✗ | error_msg_errno(ebuf, "Error opening /dev/null"); | |
192 | } | ||
193 | |||
194 | // Prevented by init_std_fds() and relied upon by fork_exec() | ||
195 | 36 | BUG_ON(fd <= STDERR_FILENO); | |
196 | |||
197 | 36 | return fd; | |
198 | } | ||
199 | |||
200 | 21 | static bool open_pipe(ErrorBuffer *ebuf, int fds[2]) | |
201 | { | ||
202 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 21 times.
|
21 | if (unlikely(xpipe2(fds, O_CLOEXEC) != 0)) { |
203 | ✗ | return error_msg_errno(ebuf, "xpipe2"); | |
204 | } | ||
205 | |||
206 | // Prevented by init_std_fds() and relied upon by fork_exec() | ||
207 | 21 | BUG_ON(fds[0] <= STDERR_FILENO); | |
208 | 21 | BUG_ON(fds[1] <= STDERR_FILENO); | |
209 | |||
210 | return true; | ||
211 | } | ||
212 | |||
213 | ✗ | static int handle_child_error(ErrorBuffer *ebuf, pid_t pid) | |
214 | { | ||
215 | ✗ | int ret = wait_child(pid); | |
216 | ✗ | if (ret < 0) { | |
217 | ✗ | error_msg_errno(ebuf, "waitpid"); | |
218 | ✗ | } else if (ret >= 256) { | |
219 | ✗ | int sig = ret >> 8; | |
220 | ✗ | const char *str = strsignal(sig); | |
221 | ✗ | error_msg(ebuf, "Child received signal %d (%s)", sig, str ? str : "??"); | |
222 | ✗ | } else if (ret) { | |
223 | ✗ | error_msg(ebuf, "Child returned %d", ret); | |
224 | } | ||
225 | ✗ | return ret; | |
226 | } | ||
227 | |||
228 | 1 | static void exec_error(SpawnContext *ctx) | |
229 | { | ||
230 | 1 | error_msg(ctx->ebuf, "Unable to exec '%s': %s", ctx->argv[0], strerror(errno)); | |
231 | 1 | } | |
232 | |||
233 | ✗ | bool spawn_compiler(SpawnContext *ctx, const Compiler *c, MessageList *msgs, bool read_stdout) | |
234 | { | ||
235 | ✗ | BUG_ON(!ctx->argv); | |
236 | ✗ | BUG_ON(!ctx->argv[0]); | |
237 | ✗ | BUG_ON(!ctx->ebuf); | |
238 | |||
239 | ✗ | int fd[3]; | |
240 | ✗ | fd[0] = open_dev_null(ctx->ebuf, O_RDONLY); | |
241 | ✗ | if (fd[0] < 0) { | |
242 | return false; | ||
243 | } | ||
244 | |||
245 | ✗ | int dev_null = open_dev_null(ctx->ebuf, O_WRONLY); | |
246 | ✗ | if (dev_null < 0) { | |
247 | ✗ | xclose(fd[0]); | |
248 | ✗ | return false; | |
249 | } | ||
250 | |||
251 | ✗ | int p[2]; | |
252 | ✗ | if (!open_pipe(ctx->ebuf, p)) { | |
253 | ✗ | xclose(dev_null); | |
254 | ✗ | xclose(fd[0]); | |
255 | ✗ | return false; | |
256 | } | ||
257 | |||
258 | ✗ | bool quiet = ctx->quiet; | |
259 | ✗ | if (read_stdout) { | |
260 | ✗ | fd[1] = p[1]; | |
261 | ✗ | fd[2] = quiet ? dev_null : 2; | |
262 | } else { | ||
263 | ✗ | fd[1] = quiet ? dev_null : 1; | |
264 | ✗ | fd[2] = p[1]; | |
265 | } | ||
266 | |||
267 | ✗ | pid_t pid = fork_exec(ctx->argv, fd, ctx->lines, ctx->columns, quiet); | |
268 | ✗ | if (pid == -1) { | |
269 | ✗ | exec_error(ctx); | |
270 | ✗ | xclose(p[1]); | |
271 | } else { | ||
272 | // Must close write end of the pipe before read_errors() or | ||
273 | // the read end never gets EOF! | ||
274 | ✗ | xclose(p[1]); | |
275 | ✗ | read_errors(c, msgs, p[0], quiet); | |
276 | ✗ | handle_child_error(ctx->ebuf, pid); | |
277 | } | ||
278 | |||
279 | ✗ | xclose(p[0]); | |
280 | ✗ | xclose(dev_null); | |
281 | ✗ | xclose(fd[0]); | |
282 | ✗ | return (pid != -1); | |
283 | } | ||
284 | |||
285 | // Close fd only if valid (positive) and not stdin/stdout/stderr | ||
286 | 234 | static int safe_xclose(int fd) | |
287 | { | ||
288 |
2/2✓ Branch 0 (2→3) taken 77 times.
✓ Branch 1 (2→4) taken 157 times.
|
234 | return (fd > STDERR_FILENO) ? xclose(fd) : 0; |
289 | } | ||
290 | |||
291 | 86 | static void safe_xclose_all(int fds[], size_t nr_fds) | |
292 | { | ||
293 |
2/2✓ Branch 0 (5→3) taken 234 times.
✓ Branch 1 (5→6) taken 86 times.
|
320 | for (size_t i = 0; i < nr_fds; i++) { |
294 | 234 | safe_xclose(fds[i]); | |
295 | 234 | fds[i] = -1; | |
296 | } | ||
297 | 86 | } | |
298 | |||
299 | 24 | UNITTEST { | |
300 | 24 | int fds[] = {-2, -3, -4}; | |
301 | 24 | safe_xclose_all(fds, 2); | |
302 | 24 | BUG_ON(fds[0] != -1); | |
303 | 24 | BUG_ON(fds[1] != -1); | |
304 | 24 | BUG_ON(fds[2] != -4); | |
305 | 24 | safe_xclose_all(fds, 3); | |
306 | 24 | BUG_ON(fds[2] != -1); | |
307 | 24 | } | |
308 | |||
309 | 19 | int spawn(SpawnContext *ctx) | |
310 | { | ||
311 | 19 | BUG_ON(!ctx->argv); | |
312 | 19 | BUG_ON(!ctx->argv[0]); | |
313 | 19 | BUG_ON(!ctx->ebuf); | |
314 | |||
315 | 19 | int child_fds[3] = {-1, -1, -1}; | |
316 | 19 | int parent_fds[3] = {-1, -1, -1}; | |
317 | 19 | bool quiet = ctx->quiet; | |
318 | 19 | size_t nr_pipes = 0; | |
319 | |||
320 |
2/2✓ Branch 0 (32→9) taken 57 times.
✓ Branch 1 (32→33) taken 19 times.
|
76 | for (size_t i = 0; i < ARRAYLEN(child_fds); i++) { |
321 |
3/4✓ Branch 0 (9→10) taken 30 times.
✓ Branch 1 (9→12) taken 6 times.
✓ Branch 2 (9→15) taken 21 times.
✗ Branch 3 (9→30) not taken.
|
57 | switch (ctx->actions[i]) { |
322 | 30 | case SPAWN_TTY: | |
323 |
1/2✗ Branch 0 (10→11) not taken.
✓ Branch 1 (10→12) taken 30 times.
|
30 | if (!quiet) { |
324 | ✗ | child_fds[i] = i; | |
325 | ✗ | break; | |
326 | } | ||
327 | // Fallthrough | ||
328 | case SPAWN_NULL: | ||
329 | 36 | child_fds[i] = open_dev_null(ctx->ebuf, O_RDWR); | |
330 |
1/2✗ Branch 0 (13→14) not taken.
✓ Branch 1 (13→31) taken 36 times.
|
36 | if (child_fds[i] < 0) { |
331 | ✗ | goto error; | |
332 | } | ||
333 | break; | ||
334 | 21 | case SPAWN_PIPE: { | |
335 | 21 | int p[2]; | |
336 |
1/2✗ Branch 0 (16→17) not taken.
✓ Branch 1 (16→18) taken 21 times.
|
21 | if (!open_pipe(ctx->ebuf, p)) { |
337 | ✗ | goto error; | |
338 | } | ||
339 |
2/2✓ Branch 0 (18→19) taken 16 times.
✓ Branch 1 (18→20) taken 5 times.
|
21 | child_fds[i] = i ? p[1] : p[0]; |
340 |
2/2✓ Branch 0 (21→22) taken 16 times.
✓ Branch 1 (21→23) taken 5 times.
|
21 | parent_fds[i] = i ? p[0] : p[1]; |
341 |
1/2✗ Branch 0 (25→26) not taken.
✓ Branch 1 (25→28) taken 21 times.
|
21 | if (!fd_set_nonblock(parent_fds[i], true)) { |
342 | ✗ | error_msg_errno(ctx->ebuf, "fcntl"); | |
343 | ✗ | goto error; | |
344 | } | ||
345 | 21 | nr_pipes++; | |
346 | 21 | break; | |
347 | } | ||
348 | ✗ | default: | |
349 | − | BUG("unhandled action type"); | |
350 | goto error; | ||
351 | } | ||
352 | } | ||
353 | |||
354 | 19 | pid_t pid = fork_exec(ctx->argv, child_fds, ctx->lines, ctx->columns, quiet); | |
355 |
2/2✓ Branch 0 (34→35) taken 1 times.
✓ Branch 1 (34→37) taken 18 times.
|
19 | if (pid == -1) { |
356 | 1 | exec_error(ctx); | |
357 | 1 | goto error; | |
358 | } | ||
359 | |||
360 | 18 | safe_xclose_all(child_fds, ARRAYLEN(child_fds)); | |
361 |
2/2✓ Branch 0 (38→39) taken 11 times.
✓ Branch 1 (38→40) taken 7 times.
|
18 | if (nr_pipes > 0) { |
362 | 11 | handle_piped_data(parent_fds, ctx); | |
363 | } | ||
364 | |||
365 | 18 | safe_xclose_all(parent_fds, ARRAYLEN(parent_fds)); | |
366 | 18 | int err = wait_child(pid); | |
367 |
1/2✗ Branch 0 (42→43) not taken.
✓ Branch 1 (42→47) taken 18 times.
|
18 | if (err < 0) { |
368 | ✗ | error_msg_errno(ctx->ebuf, "waitpid"); | |
369 | } | ||
370 | |||
371 | return err; | ||
372 | |||
373 | 1 | error: | |
374 | 1 | safe_xclose_all(child_fds, ARRAYLEN(child_fds)); | |
375 | 1 | safe_xclose_all(parent_fds, ARRAYLEN(parent_fds)); | |
376 | 1 | return -1; | |
377 | } | ||
378 |