Line | Count | Source |
1 | | #define DISABLE_SIGN_COMPARE_WARNINGS |
2 | | |
3 | | #include "git-compat-util.h" |
4 | | #include "run-command.h" |
5 | | #include "environment.h" |
6 | | #include "exec-cmd.h" |
7 | | #include "gettext.h" |
8 | | #include "sigchain.h" |
9 | | #include "strvec.h" |
10 | | #include "symlinks.h" |
11 | | #include "thread-utils.h" |
12 | | #include "strbuf.h" |
13 | | #include "string-list.h" |
14 | | #include "trace.h" |
15 | | #include "trace2.h" |
16 | | #include "quote.h" |
17 | | #include "config.h" |
18 | | #include "packfile.h" |
19 | | #include "compat/nonblock.h" |
20 | | |
21 | | void child_process_init(struct child_process *child) |
22 | 0 | { |
23 | 0 | struct child_process blank = CHILD_PROCESS_INIT; |
24 | 0 | memcpy(child, &blank, sizeof(*child)); |
25 | 0 | } |
26 | | |
27 | | void child_process_clear(struct child_process *child) |
28 | 0 | { |
29 | 0 | strvec_clear(&child->args); |
30 | 0 | strvec_clear(&child->env); |
31 | 0 | } |
32 | | |
33 | | struct child_to_clean { |
34 | | pid_t pid; |
35 | | struct child_process *process; |
36 | | struct child_to_clean *next; |
37 | | }; |
38 | | static struct child_to_clean *children_to_clean; |
39 | | static int installed_child_cleanup_handler; |
40 | | |
41 | | static void cleanup_children(int sig, int in_signal) |
42 | 0 | { |
43 | 0 | struct child_to_clean *children_to_wait_for = NULL; |
44 | |
|
45 | 0 | while (children_to_clean) { |
46 | 0 | struct child_to_clean *p = children_to_clean; |
47 | 0 | children_to_clean = p->next; |
48 | |
|
49 | 0 | if (p->process && !in_signal) { |
50 | 0 | struct child_process *process = p->process; |
51 | 0 | if (process->clean_on_exit_handler) { |
52 | 0 | trace_printf( |
53 | 0 | "trace: run_command: running exit handler for pid %" |
54 | 0 | PRIuMAX, (uintmax_t)p->pid |
55 | 0 | ); |
56 | 0 | process->clean_on_exit_handler(process); |
57 | 0 | } |
58 | 0 | } |
59 | |
|
60 | 0 | kill(p->pid, sig); |
61 | |
|
62 | 0 | if (p->process && p->process->wait_after_clean) { |
63 | 0 | p->next = children_to_wait_for; |
64 | 0 | children_to_wait_for = p; |
65 | 0 | } else { |
66 | 0 | if (!in_signal) |
67 | 0 | free(p); |
68 | 0 | } |
69 | 0 | } |
70 | |
|
71 | 0 | while (children_to_wait_for) { |
72 | 0 | struct child_to_clean *p = children_to_wait_for; |
73 | 0 | children_to_wait_for = p->next; |
74 | |
|
75 | 0 | while (waitpid(p->pid, NULL, 0) < 0 && errno == EINTR) |
76 | 0 | ; /* spin waiting for process exit or error */ |
77 | |
|
78 | 0 | if (!in_signal) |
79 | 0 | free(p); |
80 | 0 | } |
81 | 0 | } |
82 | | |
83 | | static void cleanup_children_on_signal(int sig) |
84 | 0 | { |
85 | 0 | cleanup_children(sig, 1); |
86 | 0 | sigchain_pop(sig); |
87 | 0 | raise(sig); |
88 | 0 | } |
89 | | |
90 | | static void cleanup_children_on_exit(void) |
91 | 0 | { |
92 | 0 | cleanup_children(SIGTERM, 0); |
93 | 0 | } |
94 | | |
95 | | static void mark_child_for_cleanup(pid_t pid, struct child_process *process) |
96 | 0 | { |
97 | 0 | struct child_to_clean *p = xmalloc(sizeof(*p)); |
98 | 0 | p->pid = pid; |
99 | 0 | p->process = process; |
100 | 0 | p->next = children_to_clean; |
101 | 0 | children_to_clean = p; |
102 | |
|
103 | 0 | if (!installed_child_cleanup_handler) { |
104 | 0 | atexit(cleanup_children_on_exit); |
105 | 0 | sigchain_push_common(cleanup_children_on_signal); |
106 | 0 | installed_child_cleanup_handler = 1; |
107 | 0 | } |
108 | 0 | } |
109 | | |
110 | | static void clear_child_for_cleanup(pid_t pid) |
111 | 0 | { |
112 | 0 | struct child_to_clean **pp; |
113 | |
|
114 | 0 | for (pp = &children_to_clean; *pp; pp = &(*pp)->next) { |
115 | 0 | struct child_to_clean *clean_me = *pp; |
116 | |
|
117 | 0 | if (clean_me->pid == pid) { |
118 | 0 | *pp = clean_me->next; |
119 | 0 | free(clean_me); |
120 | 0 | return; |
121 | 0 | } |
122 | 0 | } |
123 | 0 | } |
124 | | |
125 | | static inline void close_pair(int fd[2]) |
126 | 0 | { |
127 | 0 | close(fd[0]); |
128 | 0 | close(fd[1]); |
129 | 0 | } |
130 | | |
131 | | int is_executable(const char *name) |
132 | 0 | { |
133 | 0 | struct stat st; |
134 | |
|
135 | 0 | if (stat(name, &st) || /* stat, not lstat */ |
136 | 0 | !S_ISREG(st.st_mode)) |
137 | 0 | return 0; |
138 | | |
139 | | #if defined(GIT_WINDOWS_NATIVE) |
140 | | /* |
141 | | * On Windows there is no executable bit. The file extension |
142 | | * indicates whether it can be run as an executable, and Git |
143 | | * has special-handling to detect scripts and launch them |
144 | | * through the indicated script interpreter. We test for the |
145 | | * file extension first because virus scanners may make |
146 | | * it quite expensive to open many files. |
147 | | */ |
148 | | if (ends_with(name, ".exe")) |
149 | | return S_IXUSR; |
150 | | |
151 | | { |
152 | | /* |
153 | | * Now that we know it does not have an executable extension, |
154 | | * peek into the file instead. |
155 | | */ |
156 | | char buf[3] = { 0 }; |
157 | | int n; |
158 | | int fd = open(name, O_RDONLY); |
159 | | st.st_mode &= ~S_IXUSR; |
160 | | if (fd >= 0) { |
161 | | n = read(fd, buf, 2); |
162 | | if (n == 2) |
163 | | /* look for a she-bang */ |
164 | | if (!strcmp(buf, "#!")) |
165 | | st.st_mode |= S_IXUSR; |
166 | | close(fd); |
167 | | } |
168 | | } |
169 | | #endif |
170 | 0 | return st.st_mode & S_IXUSR; |
171 | 0 | } |
172 | | |
173 | | #ifndef locate_in_PATH |
174 | | /* |
175 | | * Search $PATH for a command. This emulates the path search that |
176 | | * execvp would perform, without actually executing the command so it |
177 | | * can be used before fork() to prepare to run a command using |
178 | | * execve() or after execvp() to diagnose why it failed. |
179 | | * |
180 | | * The caller should ensure that file contains no directory |
181 | | * separators. |
182 | | * |
183 | | * Returns the path to the command, as found in $PATH or NULL if the |
184 | | * command could not be found. The caller inherits ownership of the memory |
185 | | * used to store the resultant path. |
186 | | * |
187 | | * This should not be used on Windows, where the $PATH search rules |
188 | | * are more complicated (e.g., a search for "foo" should find |
189 | | * "foo.exe"). |
190 | | */ |
191 | | static char *locate_in_PATH(const char *file) |
192 | 0 | { |
193 | 0 | const char *p = getenv("PATH"); |
194 | 0 | struct strbuf buf = STRBUF_INIT; |
195 | |
|
196 | 0 | if (!p || !*p) |
197 | 0 | return NULL; |
198 | | |
199 | 0 | while (1) { |
200 | 0 | const char *end = strchrnul(p, ':'); |
201 | |
|
202 | 0 | strbuf_reset(&buf); |
203 | | |
204 | | /* POSIX specifies an empty entry as the current directory. */ |
205 | 0 | if (end != p) { |
206 | 0 | strbuf_add(&buf, p, end - p); |
207 | 0 | strbuf_addch(&buf, '/'); |
208 | 0 | } |
209 | 0 | strbuf_addstr(&buf, file); |
210 | |
|
211 | 0 | if (is_executable(buf.buf)) |
212 | 0 | return strbuf_detach(&buf, NULL); |
213 | | |
214 | 0 | if (!*end) |
215 | 0 | break; |
216 | 0 | p = end + 1; |
217 | 0 | } |
218 | | |
219 | 0 | strbuf_release(&buf); |
220 | 0 | return NULL; |
221 | 0 | } |
222 | | #endif |
223 | | |
224 | | int exists_in_PATH(const char *command) |
225 | 0 | { |
226 | 0 | char *r = locate_in_PATH(command); |
227 | 0 | int found = r != NULL; |
228 | 0 | free(r); |
229 | 0 | return found; |
230 | 0 | } |
231 | | |
232 | | int sane_execvp(const char *file, char * const argv[]) |
233 | 0 | { |
234 | 0 | #ifndef GIT_WINDOWS_NATIVE |
235 | | /* |
236 | | * execvp() doesn't return, so we all we can do is tell trace2 |
237 | | * what we are about to do and let it leave a hint in the log |
238 | | * (unless of course the execvp() fails). |
239 | | * |
240 | | * we skip this for Windows because the compat layer already |
241 | | * has to emulate the execvp() call anyway. |
242 | | */ |
243 | 0 | int exec_id = trace2_exec(file, (const char **)argv); |
244 | 0 | #endif |
245 | |
|
246 | 0 | if (!execvp(file, argv)) |
247 | 0 | return 0; /* cannot happen ;-) */ |
248 | | |
249 | 0 | #ifndef GIT_WINDOWS_NATIVE |
250 | 0 | { |
251 | 0 | int ec = errno; |
252 | 0 | trace2_exec_result(exec_id, ec); |
253 | 0 | errno = ec; |
254 | 0 | } |
255 | 0 | #endif |
256 | | |
257 | | /* |
258 | | * When a command can't be found because one of the directories |
259 | | * listed in $PATH is unsearchable, execvp reports EACCES, but |
260 | | * careful usability testing (read: analysis of occasional bug |
261 | | * reports) reveals that "No such file or directory" is more |
262 | | * intuitive. |
263 | | * |
264 | | * We avoid commands with "/", because execvp will not do $PATH |
265 | | * lookups in that case. |
266 | | * |
267 | | * The reassignment of EACCES to errno looks like a no-op below, |
268 | | * but we need to protect against exists_in_PATH overwriting errno. |
269 | | */ |
270 | 0 | if (errno == EACCES && !strchr(file, '/')) |
271 | 0 | errno = exists_in_PATH(file) ? EACCES : ENOENT; |
272 | 0 | else if (errno == ENOTDIR && !strchr(file, '/')) |
273 | 0 | errno = ENOENT; |
274 | 0 | return -1; |
275 | 0 | } |
276 | | |
277 | | char *git_shell_path(void) |
278 | 0 | { |
279 | 0 | #ifndef GIT_WINDOWS_NATIVE |
280 | 0 | return xstrdup(SHELL_PATH); |
281 | | #else |
282 | | char *p = locate_in_PATH("sh"); |
283 | | convert_slashes(p); |
284 | | return p; |
285 | | #endif |
286 | 0 | } |
287 | | |
288 | | static const char **prepare_shell_cmd(struct strvec *out, const char **argv) |
289 | 0 | { |
290 | 0 | if (!argv[0]) |
291 | 0 | BUG("shell command is empty"); |
292 | | |
293 | 0 | if (strcspn(argv[0], "|&;<>()$`\\\"' \t\n*?[#~=%") != strlen(argv[0])) { |
294 | 0 | strvec_push_nodup(out, git_shell_path()); |
295 | 0 | strvec_push(out, "-c"); |
296 | | |
297 | | /* |
298 | | * If we have no extra arguments, we do not even need to |
299 | | * bother with the "$@" magic. |
300 | | */ |
301 | 0 | if (!argv[1]) |
302 | 0 | strvec_push(out, argv[0]); |
303 | 0 | else |
304 | 0 | strvec_pushf(out, "%s \"$@\"", argv[0]); |
305 | 0 | } |
306 | |
|
307 | 0 | strvec_pushv(out, argv); |
308 | 0 | return out->v; |
309 | 0 | } |
310 | | |
311 | | #ifndef GIT_WINDOWS_NATIVE |
312 | | static int child_notifier = -1; |
313 | | |
314 | | enum child_errcode { |
315 | | CHILD_ERR_CHDIR, |
316 | | CHILD_ERR_DUP2, |
317 | | CHILD_ERR_CLOSE, |
318 | | CHILD_ERR_SIGPROCMASK, |
319 | | CHILD_ERR_SILENT, |
320 | | CHILD_ERR_ERRNO |
321 | | }; |
322 | | |
323 | | struct child_err { |
324 | | enum child_errcode err; |
325 | | int syserr; /* errno */ |
326 | | }; |
327 | | |
328 | | static void child_die(enum child_errcode err) |
329 | 0 | { |
330 | 0 | struct child_err buf; |
331 | |
|
332 | 0 | buf.err = err; |
333 | 0 | buf.syserr = errno; |
334 | | |
335 | | /* write(2) on buf smaller than PIPE_BUF (min 512) is atomic: */ |
336 | 0 | xwrite(child_notifier, &buf, sizeof(buf)); |
337 | 0 | _exit(1); |
338 | 0 | } |
339 | | |
340 | | static void child_dup2(int fd, int to) |
341 | 0 | { |
342 | 0 | if (dup2(fd, to) < 0) |
343 | 0 | child_die(CHILD_ERR_DUP2); |
344 | 0 | } |
345 | | |
346 | | static void child_close(int fd) |
347 | 0 | { |
348 | 0 | if (close(fd)) |
349 | 0 | child_die(CHILD_ERR_CLOSE); |
350 | 0 | } |
351 | | |
352 | | static void child_close_pair(int fd[2]) |
353 | 0 | { |
354 | 0 | child_close(fd[0]); |
355 | 0 | child_close(fd[1]); |
356 | 0 | } |
357 | | |
358 | | static void child_error_fn(const char *err UNUSED, va_list params UNUSED) |
359 | 0 | { |
360 | 0 | const char msg[] = "error() should not be called in child\n"; |
361 | 0 | xwrite(2, msg, sizeof(msg) - 1); |
362 | 0 | } |
363 | | |
364 | | static void child_warn_fn(const char *err UNUSED, va_list params UNUSED) |
365 | 0 | { |
366 | 0 | const char msg[] = "warn() should not be called in child\n"; |
367 | 0 | xwrite(2, msg, sizeof(msg) - 1); |
368 | 0 | } |
369 | | |
370 | | static void NORETURN child_die_fn(const char *err UNUSED, va_list params UNUSED) |
371 | 0 | { |
372 | 0 | const char msg[] = "die() should not be called in child\n"; |
373 | 0 | xwrite(2, msg, sizeof(msg) - 1); |
374 | 0 | _exit(2); |
375 | 0 | } |
376 | | |
377 | | /* this runs in the parent process */ |
378 | | static void child_err_spew(struct child_process *cmd, struct child_err *cerr) |
379 | 0 | { |
380 | 0 | static void (*old_errfn)(const char *err, va_list params); |
381 | 0 | report_fn die_message_routine = get_die_message_routine(); |
382 | |
|
383 | 0 | old_errfn = get_error_routine(); |
384 | 0 | set_error_routine(die_message_routine); |
385 | 0 | errno = cerr->syserr; |
386 | |
|
387 | 0 | switch (cerr->err) { |
388 | 0 | case CHILD_ERR_CHDIR: |
389 | 0 | error_errno("exec '%s': cd to '%s' failed", |
390 | 0 | cmd->args.v[0], cmd->dir); |
391 | 0 | break; |
392 | 0 | case CHILD_ERR_DUP2: |
393 | 0 | error_errno("dup2() in child failed"); |
394 | 0 | break; |
395 | 0 | case CHILD_ERR_CLOSE: |
396 | 0 | error_errno("close() in child failed"); |
397 | 0 | break; |
398 | 0 | case CHILD_ERR_SIGPROCMASK: |
399 | 0 | error_errno("sigprocmask failed restoring signals"); |
400 | 0 | break; |
401 | 0 | case CHILD_ERR_SILENT: |
402 | 0 | break; |
403 | 0 | case CHILD_ERR_ERRNO: |
404 | 0 | error_errno("cannot exec '%s'", cmd->args.v[0]); |
405 | 0 | break; |
406 | 0 | } |
407 | 0 | set_error_routine(old_errfn); |
408 | 0 | } |
409 | | |
410 | | static int prepare_cmd(struct strvec *out, const struct child_process *cmd) |
411 | 0 | { |
412 | 0 | if (!cmd->args.v[0]) |
413 | 0 | BUG("command is empty"); |
414 | | |
415 | | /* |
416 | | * Add SHELL_PATH so in the event exec fails with ENOEXEC we can |
417 | | * attempt to interpret the command with 'sh'. |
418 | | */ |
419 | 0 | strvec_push(out, SHELL_PATH); |
420 | |
|
421 | 0 | if (cmd->git_cmd) { |
422 | 0 | prepare_git_cmd(out, cmd->args.v); |
423 | 0 | } else if (cmd->use_shell) { |
424 | 0 | prepare_shell_cmd(out, cmd->args.v); |
425 | 0 | } else { |
426 | 0 | strvec_pushv(out, cmd->args.v); |
427 | 0 | } |
428 | | |
429 | | /* |
430 | | * If there are no dir separator characters in the command then perform |
431 | | * a path lookup and use the resolved path as the command to exec. If |
432 | | * there are dir separator characters, we have exec attempt to invoke |
433 | | * the command directly. |
434 | | */ |
435 | 0 | if (!has_dir_sep(out->v[1])) { |
436 | 0 | char *program = locate_in_PATH(out->v[1]); |
437 | 0 | if (program) { |
438 | 0 | free((char *)out->v[1]); |
439 | 0 | out->v[1] = program; |
440 | 0 | } else { |
441 | 0 | strvec_clear(out); |
442 | 0 | errno = ENOENT; |
443 | 0 | return -1; |
444 | 0 | } |
445 | 0 | } |
446 | | |
447 | 0 | return 0; |
448 | 0 | } |
449 | | |
450 | | static char **prep_childenv(const char *const *deltaenv) |
451 | 0 | { |
452 | 0 | extern char **environ; |
453 | 0 | char **childenv; |
454 | 0 | struct string_list env = STRING_LIST_INIT_DUP; |
455 | 0 | struct strbuf key = STRBUF_INIT; |
456 | 0 | const char *const *p; |
457 | 0 | int i; |
458 | | |
459 | | /* Construct a sorted string list consisting of the current environ */ |
460 | 0 | for (p = (const char *const *) environ; p && *p; p++) { |
461 | 0 | const char *equals = strchr(*p, '='); |
462 | |
|
463 | 0 | if (equals) { |
464 | 0 | strbuf_reset(&key); |
465 | 0 | strbuf_add(&key, *p, equals - *p); |
466 | 0 | string_list_append(&env, key.buf)->util = (void *) *p; |
467 | 0 | } else { |
468 | 0 | string_list_append(&env, *p)->util = (void *) *p; |
469 | 0 | } |
470 | 0 | } |
471 | 0 | string_list_sort(&env); |
472 | | |
473 | | /* Merge in 'deltaenv' with the current environ */ |
474 | 0 | for (p = deltaenv; p && *p; p++) { |
475 | 0 | const char *equals = strchr(*p, '='); |
476 | |
|
477 | 0 | if (equals) { |
478 | | /* ('key=value'), insert or replace entry */ |
479 | 0 | strbuf_reset(&key); |
480 | 0 | strbuf_add(&key, *p, equals - *p); |
481 | 0 | string_list_insert(&env, key.buf)->util = (void *) *p; |
482 | 0 | } else { |
483 | | /* otherwise ('key') remove existing entry */ |
484 | 0 | string_list_remove(&env, *p, 0); |
485 | 0 | } |
486 | 0 | } |
487 | | |
488 | | /* Create an array of 'char *' to be used as the childenv */ |
489 | 0 | ALLOC_ARRAY(childenv, env.nr + 1); |
490 | 0 | for (i = 0; i < env.nr; i++) |
491 | 0 | childenv[i] = env.items[i].util; |
492 | 0 | childenv[env.nr] = NULL; |
493 | |
|
494 | 0 | string_list_clear(&env, 0); |
495 | 0 | strbuf_release(&key); |
496 | 0 | return childenv; |
497 | 0 | } |
498 | | |
499 | | struct atfork_state { |
500 | | #ifndef NO_PTHREADS |
501 | | int cs; |
502 | | #endif |
503 | | sigset_t old; |
504 | | }; |
505 | | |
506 | | #define CHECK_BUG(err, msg) \ |
507 | 0 | do { \ |
508 | 0 | int e = (err); \ |
509 | 0 | if (e) \ |
510 | 0 | BUG("%s: %s", msg, strerror(e)); \ |
511 | 0 | } while(0) |
512 | | |
513 | | static void atfork_prepare(struct atfork_state *as) |
514 | 0 | { |
515 | 0 | sigset_t all; |
516 | | |
517 | | /* |
518 | | * POSIX says sigfillset() can fail, but an overly clever |
519 | | * compiler can see through the header files and decide |
520 | | * it cannot fail on a particular platform it is compiling for, |
521 | | * triggering -Wunreachable-code false positive. |
522 | | */ |
523 | 0 | if (NOT_CONSTANT(sigfillset(&all))) |
524 | 0 | die_errno("sigfillset"); |
525 | | #ifdef NO_PTHREADS |
526 | | if (sigprocmask(SIG_SETMASK, &all, &as->old)) |
527 | | die_errno("sigprocmask"); |
528 | | #else |
529 | 0 | CHECK_BUG(pthread_sigmask(SIG_SETMASK, &all, &as->old), |
530 | 0 | "blocking all signals"); |
531 | 0 | CHECK_BUG(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &as->cs), |
532 | 0 | "disabling cancellation"); |
533 | 0 | #endif |
534 | 0 | } |
535 | | |
536 | | static void atfork_parent(struct atfork_state *as) |
537 | 0 | { |
538 | | #ifdef NO_PTHREADS |
539 | | if (sigprocmask(SIG_SETMASK, &as->old, NULL)) |
540 | | die_errno("sigprocmask"); |
541 | | #else |
542 | 0 | CHECK_BUG(pthread_setcancelstate(as->cs, NULL), |
543 | 0 | "re-enabling cancellation"); |
544 | 0 | CHECK_BUG(pthread_sigmask(SIG_SETMASK, &as->old, NULL), |
545 | 0 | "restoring signal mask"); |
546 | 0 | #endif |
547 | 0 | } |
548 | | #endif /* GIT_WINDOWS_NATIVE */ |
549 | | |
550 | | static inline void set_cloexec(int fd) |
551 | 0 | { |
552 | 0 | int flags = fcntl(fd, F_GETFD); |
553 | 0 | if (flags >= 0) |
554 | 0 | fcntl(fd, F_SETFD, flags | FD_CLOEXEC); |
555 | 0 | } |
556 | | |
557 | | static int wait_or_whine(pid_t pid, const char *argv0, int in_signal) |
558 | 0 | { |
559 | 0 | int status, code = -1; |
560 | 0 | pid_t waiting; |
561 | 0 | int failed_errno = 0; |
562 | |
|
563 | 0 | while ((waiting = waitpid(pid, &status, 0)) < 0 && errno == EINTR) |
564 | 0 | ; /* nothing */ |
565 | |
|
566 | 0 | if (waiting < 0) { |
567 | 0 | failed_errno = errno; |
568 | 0 | if (!in_signal) |
569 | 0 | error_errno("waitpid for %s failed", argv0); |
570 | 0 | } else if (waiting != pid) { |
571 | 0 | if (!in_signal) |
572 | 0 | error("waitpid is confused (%s)", argv0); |
573 | 0 | } else if (WIFSIGNALED(status)) { |
574 | 0 | code = WTERMSIG(status); |
575 | 0 | if (!in_signal && code != SIGINT && code != SIGQUIT && code != SIGPIPE) |
576 | 0 | error("%s died of signal %d", argv0, code); |
577 | | /* |
578 | | * This return value is chosen so that code & 0xff |
579 | | * mimics the exit code that a POSIX shell would report for |
580 | | * a program that died from this signal. |
581 | | */ |
582 | 0 | code += 128; |
583 | 0 | } else if (WIFEXITED(status)) { |
584 | 0 | code = WEXITSTATUS(status); |
585 | 0 | } else { |
586 | 0 | if (!in_signal) |
587 | 0 | error("waitpid is confused (%s)", argv0); |
588 | 0 | } |
589 | |
|
590 | 0 | if (!in_signal) |
591 | 0 | clear_child_for_cleanup(pid); |
592 | |
|
593 | 0 | errno = failed_errno; |
594 | 0 | return code; |
595 | 0 | } |
596 | | |
597 | | static void trace_add_env(struct strbuf *dst, const char *const *deltaenv) |
598 | 0 | { |
599 | 0 | struct string_list envs = STRING_LIST_INIT_DUP; |
600 | 0 | const char *const *e; |
601 | 0 | int i; |
602 | 0 | int printed_unset = 0; |
603 | | |
604 | | /* Last one wins, see run-command.c:prep_childenv() for context */ |
605 | 0 | for (e = deltaenv; e && *e; e++) { |
606 | 0 | struct strbuf key = STRBUF_INIT; |
607 | 0 | char *equals = strchr(*e, '='); |
608 | |
|
609 | 0 | if (equals) { |
610 | 0 | strbuf_add(&key, *e, equals - *e); |
611 | 0 | string_list_insert(&envs, key.buf)->util = equals + 1; |
612 | 0 | } else { |
613 | 0 | string_list_insert(&envs, *e)->util = NULL; |
614 | 0 | } |
615 | 0 | strbuf_release(&key); |
616 | 0 | } |
617 | | |
618 | | /* "unset X Y...;" */ |
619 | 0 | for (i = 0; i < envs.nr; i++) { |
620 | 0 | const char *var = envs.items[i].string; |
621 | 0 | const char *val = envs.items[i].util; |
622 | |
|
623 | 0 | if (val || !getenv(var)) |
624 | 0 | continue; |
625 | | |
626 | 0 | if (!printed_unset) { |
627 | 0 | strbuf_addstr(dst, " unset"); |
628 | 0 | printed_unset = 1; |
629 | 0 | } |
630 | 0 | strbuf_addf(dst, " %s", var); |
631 | 0 | } |
632 | 0 | if (printed_unset) |
633 | 0 | strbuf_addch(dst, ';'); |
634 | | |
635 | | /* ... followed by "A=B C=D ..." */ |
636 | 0 | for (i = 0; i < envs.nr; i++) { |
637 | 0 | const char *var = envs.items[i].string; |
638 | 0 | const char *val = envs.items[i].util; |
639 | 0 | const char *oldval; |
640 | |
|
641 | 0 | if (!val) |
642 | 0 | continue; |
643 | | |
644 | 0 | oldval = getenv(var); |
645 | 0 | if (oldval && !strcmp(val, oldval)) |
646 | 0 | continue; |
647 | | |
648 | 0 | strbuf_addf(dst, " %s=", var); |
649 | 0 | sq_quote_buf_pretty(dst, val); |
650 | 0 | } |
651 | 0 | string_list_clear(&envs, 0); |
652 | 0 | } |
653 | | |
654 | | static void trace_run_command(const struct child_process *cp) |
655 | 0 | { |
656 | 0 | struct strbuf buf = STRBUF_INIT; |
657 | |
|
658 | 0 | if (!trace_want(&trace_default_key)) |
659 | 0 | return; |
660 | | |
661 | 0 | strbuf_addstr(&buf, "trace: run_command:"); |
662 | 0 | if (cp->dir) { |
663 | 0 | strbuf_addstr(&buf, " cd "); |
664 | 0 | sq_quote_buf_pretty(&buf, cp->dir); |
665 | 0 | strbuf_addch(&buf, ';'); |
666 | 0 | } |
667 | 0 | trace_add_env(&buf, cp->env.v); |
668 | 0 | if (cp->git_cmd) |
669 | 0 | strbuf_addstr(&buf, " git"); |
670 | 0 | sq_quote_argv_pretty(&buf, cp->args.v); |
671 | |
|
672 | 0 | trace_printf("%s", buf.buf); |
673 | 0 | strbuf_release(&buf); |
674 | 0 | } |
675 | | |
676 | | int start_command(struct child_process *cmd) |
677 | 0 | { |
678 | 0 | int need_in, need_out, need_err; |
679 | 0 | int fdin[2], fdout[2], fderr[2]; |
680 | 0 | int failed_errno; |
681 | 0 | const char *str; |
682 | | |
683 | | /* |
684 | | * In case of errors we must keep the promise to close FDs |
685 | | * that have been passed in via ->in and ->out. |
686 | | */ |
687 | |
|
688 | 0 | need_in = !cmd->no_stdin && cmd->in < 0; |
689 | 0 | if (need_in) { |
690 | 0 | if (pipe(fdin) < 0) { |
691 | 0 | failed_errno = errno; |
692 | 0 | if (cmd->out > 0) |
693 | 0 | close(cmd->out); |
694 | 0 | str = "standard input"; |
695 | 0 | goto fail_pipe; |
696 | 0 | } |
697 | 0 | cmd->in = fdin[1]; |
698 | 0 | } |
699 | | |
700 | 0 | need_out = !cmd->no_stdout |
701 | 0 | && !cmd->stdout_to_stderr |
702 | 0 | && cmd->out < 0; |
703 | 0 | if (need_out) { |
704 | 0 | if (pipe(fdout) < 0) { |
705 | 0 | failed_errno = errno; |
706 | 0 | if (need_in) |
707 | 0 | close_pair(fdin); |
708 | 0 | else if (cmd->in) |
709 | 0 | close(cmd->in); |
710 | 0 | str = "standard output"; |
711 | 0 | goto fail_pipe; |
712 | 0 | } |
713 | 0 | cmd->out = fdout[0]; |
714 | 0 | } |
715 | | |
716 | 0 | need_err = !cmd->no_stderr && cmd->err < 0; |
717 | 0 | if (need_err) { |
718 | 0 | if (pipe(fderr) < 0) { |
719 | 0 | failed_errno = errno; |
720 | 0 | if (need_in) |
721 | 0 | close_pair(fdin); |
722 | 0 | else if (cmd->in) |
723 | 0 | close(cmd->in); |
724 | 0 | if (need_out) |
725 | 0 | close_pair(fdout); |
726 | 0 | else if (cmd->out) |
727 | 0 | close(cmd->out); |
728 | 0 | str = "standard error"; |
729 | 0 | fail_pipe: |
730 | 0 | error("cannot create %s pipe for %s: %s", |
731 | 0 | str, cmd->args.v[0], strerror(failed_errno)); |
732 | 0 | child_process_clear(cmd); |
733 | 0 | errno = failed_errno; |
734 | 0 | return -1; |
735 | 0 | } |
736 | 0 | cmd->err = fderr[0]; |
737 | 0 | } |
738 | | |
739 | 0 | trace2_child_start(cmd); |
740 | 0 | trace_run_command(cmd); |
741 | |
|
742 | 0 | fflush(NULL); |
743 | |
|
744 | 0 | if (cmd->odb_to_close) |
745 | 0 | odb_close(cmd->odb_to_close); |
746 | |
|
747 | 0 | #ifndef GIT_WINDOWS_NATIVE |
748 | 0 | { |
749 | 0 | int notify_pipe[2]; |
750 | 0 | int null_fd = -1; |
751 | 0 | char **childenv; |
752 | 0 | struct strvec argv = STRVEC_INIT; |
753 | 0 | struct child_err cerr; |
754 | 0 | struct atfork_state as; |
755 | |
|
756 | 0 | if (prepare_cmd(&argv, cmd) < 0) { |
757 | 0 | failed_errno = errno; |
758 | 0 | cmd->pid = -1; |
759 | 0 | if (!cmd->silent_exec_failure) |
760 | 0 | error_errno("cannot run %s", cmd->args.v[0]); |
761 | 0 | goto end_of_spawn; |
762 | 0 | } |
763 | | |
764 | 0 | trace_argv_printf(&argv.v[1], "trace: start_command:"); |
765 | |
|
766 | 0 | if (pipe(notify_pipe)) |
767 | 0 | notify_pipe[0] = notify_pipe[1] = -1; |
768 | |
|
769 | 0 | if (cmd->no_stdin || cmd->no_stdout || cmd->no_stderr) { |
770 | 0 | null_fd = xopen("/dev/null", O_RDWR | O_CLOEXEC); |
771 | 0 | set_cloexec(null_fd); |
772 | 0 | } |
773 | |
|
774 | 0 | childenv = prep_childenv(cmd->env.v); |
775 | 0 | atfork_prepare(&as); |
776 | | |
777 | | /* |
778 | | * NOTE: In order to prevent deadlocking when using threads special |
779 | | * care should be taken with the function calls made in between the |
780 | | * fork() and exec() calls. No calls should be made to functions which |
781 | | * require acquiring a lock (e.g. malloc) as the lock could have been |
782 | | * held by another thread at the time of forking, causing the lock to |
783 | | * never be released in the child process. This means only |
784 | | * Async-Signal-Safe functions are permitted in the child. |
785 | | */ |
786 | 0 | cmd->pid = fork(); |
787 | 0 | failed_errno = errno; |
788 | 0 | if (!cmd->pid) { |
789 | 0 | int sig; |
790 | | /* |
791 | | * Ensure the default die/error/warn routines do not get |
792 | | * called, they can take stdio locks and malloc. |
793 | | */ |
794 | 0 | set_die_routine(child_die_fn); |
795 | 0 | set_error_routine(child_error_fn); |
796 | 0 | set_warn_routine(child_warn_fn); |
797 | |
|
798 | 0 | close(notify_pipe[0]); |
799 | 0 | set_cloexec(notify_pipe[1]); |
800 | 0 | child_notifier = notify_pipe[1]; |
801 | |
|
802 | 0 | if (cmd->no_stdin) |
803 | 0 | child_dup2(null_fd, 0); |
804 | 0 | else if (need_in) { |
805 | 0 | child_dup2(fdin[0], 0); |
806 | 0 | child_close_pair(fdin); |
807 | 0 | } else if (cmd->in) { |
808 | 0 | child_dup2(cmd->in, 0); |
809 | 0 | child_close(cmd->in); |
810 | 0 | } |
811 | |
|
812 | 0 | if (cmd->no_stderr) |
813 | 0 | child_dup2(null_fd, 2); |
814 | 0 | else if (need_err) { |
815 | 0 | child_dup2(fderr[1], 2); |
816 | 0 | child_close_pair(fderr); |
817 | 0 | } else if (cmd->err > 1) { |
818 | 0 | child_dup2(cmd->err, 2); |
819 | 0 | child_close(cmd->err); |
820 | 0 | } |
821 | |
|
822 | 0 | if (cmd->no_stdout) |
823 | 0 | child_dup2(null_fd, 1); |
824 | 0 | else if (cmd->stdout_to_stderr) |
825 | 0 | child_dup2(2, 1); |
826 | 0 | else if (need_out) { |
827 | 0 | child_dup2(fdout[1], 1); |
828 | 0 | child_close_pair(fdout); |
829 | 0 | } else if (cmd->out > 1) { |
830 | 0 | child_dup2(cmd->out, 1); |
831 | 0 | child_close(cmd->out); |
832 | 0 | } |
833 | |
|
834 | 0 | if (cmd->dir && chdir(cmd->dir)) |
835 | 0 | child_die(CHILD_ERR_CHDIR); |
836 | | |
837 | | /* |
838 | | * restore default signal handlers here, in case |
839 | | * we catch a signal right before execve below |
840 | | */ |
841 | 0 | for (sig = 1; sig < NSIG; sig++) { |
842 | | /* ignored signals get reset to SIG_DFL on execve */ |
843 | 0 | if (signal(sig, SIG_DFL) == SIG_IGN) |
844 | 0 | signal(sig, SIG_IGN); |
845 | 0 | } |
846 | |
|
847 | 0 | if (sigprocmask(SIG_SETMASK, &as.old, NULL) != 0) |
848 | 0 | child_die(CHILD_ERR_SIGPROCMASK); |
849 | | |
850 | | /* |
851 | | * Attempt to exec using the command and arguments starting at |
852 | | * argv.argv[1]. argv.argv[0] contains SHELL_PATH which will |
853 | | * be used in the event exec failed with ENOEXEC at which point |
854 | | * we will try to interpret the command using 'sh'. |
855 | | */ |
856 | 0 | execve(argv.v[1], (char *const *) argv.v + 1, |
857 | 0 | (char *const *) childenv); |
858 | 0 | if (errno == ENOEXEC) |
859 | 0 | execve(argv.v[0], (char *const *) argv.v, |
860 | 0 | (char *const *) childenv); |
861 | |
|
862 | 0 | if (cmd->silent_exec_failure && errno == ENOENT) |
863 | 0 | child_die(CHILD_ERR_SILENT); |
864 | 0 | child_die(CHILD_ERR_ERRNO); |
865 | 0 | } |
866 | 0 | atfork_parent(&as); |
867 | 0 | if (cmd->pid < 0) |
868 | 0 | error_errno("cannot fork() for %s", cmd->args.v[0]); |
869 | 0 | else if (cmd->clean_on_exit) |
870 | 0 | mark_child_for_cleanup(cmd->pid, cmd); |
871 | | |
872 | | /* |
873 | | * Wait for child's exec. If the exec succeeds (or if fork() |
874 | | * failed), EOF is seen immediately by the parent. Otherwise, the |
875 | | * child process sends a child_err struct. |
876 | | * Note that use of this infrastructure is completely advisory, |
877 | | * therefore, we keep error checks minimal. |
878 | | */ |
879 | 0 | close(notify_pipe[1]); |
880 | 0 | if (xread(notify_pipe[0], &cerr, sizeof(cerr)) == sizeof(cerr)) { |
881 | | /* |
882 | | * At this point we know that fork() succeeded, but exec() |
883 | | * failed. Errors have been reported to our stderr. |
884 | | */ |
885 | 0 | wait_or_whine(cmd->pid, cmd->args.v[0], 0); |
886 | 0 | child_err_spew(cmd, &cerr); |
887 | 0 | failed_errno = errno; |
888 | 0 | cmd->pid = -1; |
889 | 0 | } |
890 | 0 | close(notify_pipe[0]); |
891 | |
|
892 | 0 | if (null_fd >= 0) |
893 | 0 | close(null_fd); |
894 | 0 | strvec_clear(&argv); |
895 | 0 | free(childenv); |
896 | 0 | } |
897 | 0 | end_of_spawn: |
898 | |
|
899 | | #else |
900 | | { |
901 | | int fhin = 0, fhout = 1, fherr = 2; |
902 | | const char **sargv = cmd->args.v; |
903 | | struct strvec nargv = STRVEC_INIT; |
904 | | |
905 | | if (cmd->no_stdin) |
906 | | fhin = open("/dev/null", O_RDWR); |
907 | | else if (need_in) |
908 | | fhin = dup(fdin[0]); |
909 | | else if (cmd->in) |
910 | | fhin = dup(cmd->in); |
911 | | |
912 | | if (cmd->no_stderr) |
913 | | fherr = open("/dev/null", O_RDWR); |
914 | | else if (need_err) |
915 | | fherr = dup(fderr[1]); |
916 | | else if (cmd->err > 2) |
917 | | fherr = dup(cmd->err); |
918 | | |
919 | | if (cmd->no_stdout) |
920 | | fhout = open("/dev/null", O_RDWR); |
921 | | else if (cmd->stdout_to_stderr) |
922 | | fhout = dup(fherr); |
923 | | else if (need_out) |
924 | | fhout = dup(fdout[1]); |
925 | | else if (cmd->out > 1) |
926 | | fhout = dup(cmd->out); |
927 | | |
928 | | if (cmd->git_cmd) |
929 | | cmd->args.v = prepare_git_cmd(&nargv, sargv); |
930 | | else if (cmd->use_shell) |
931 | | cmd->args.v = prepare_shell_cmd(&nargv, sargv); |
932 | | |
933 | | trace_argv_printf(cmd->args.v, "trace: start_command:"); |
934 | | cmd->pid = mingw_spawnvpe(cmd->args.v[0], cmd->args.v, |
935 | | (char**) cmd->env.v, |
936 | | cmd->dir, fhin, fhout, fherr); |
937 | | failed_errno = errno; |
938 | | if (cmd->pid < 0 && (!cmd->silent_exec_failure || errno != ENOENT)) |
939 | | error_errno("cannot spawn %s", cmd->args.v[0]); |
940 | | if (cmd->clean_on_exit && cmd->pid >= 0) |
941 | | mark_child_for_cleanup(cmd->pid, cmd); |
942 | | |
943 | | strvec_clear(&nargv); |
944 | | cmd->args.v = sargv; |
945 | | if (fhin != 0) |
946 | | close(fhin); |
947 | | if (fhout != 1) |
948 | | close(fhout); |
949 | | if (fherr != 2) |
950 | | close(fherr); |
951 | | } |
952 | | #endif |
953 | |
|
954 | 0 | if (cmd->pid < 0) { |
955 | 0 | trace2_child_exit(cmd, -1); |
956 | |
|
957 | 0 | if (need_in) |
958 | 0 | close_pair(fdin); |
959 | 0 | else if (cmd->in) |
960 | 0 | close(cmd->in); |
961 | 0 | if (need_out) |
962 | 0 | close_pair(fdout); |
963 | 0 | else if (cmd->out) |
964 | 0 | close(cmd->out); |
965 | 0 | if (need_err) |
966 | 0 | close_pair(fderr); |
967 | 0 | else if (cmd->err) |
968 | 0 | close(cmd->err); |
969 | 0 | child_process_clear(cmd); |
970 | 0 | errno = failed_errno; |
971 | 0 | return -1; |
972 | 0 | } |
973 | | |
974 | 0 | if (need_in) |
975 | 0 | close(fdin[0]); |
976 | 0 | else if (cmd->in) |
977 | 0 | close(cmd->in); |
978 | |
|
979 | 0 | if (need_out) |
980 | 0 | close(fdout[1]); |
981 | 0 | else if (cmd->out) |
982 | 0 | close(cmd->out); |
983 | |
|
984 | 0 | if (need_err) |
985 | 0 | close(fderr[1]); |
986 | 0 | else if (cmd->err) |
987 | 0 | close(cmd->err); |
988 | |
|
989 | 0 | return 0; |
990 | 0 | } |
991 | | |
992 | | int finish_command(struct child_process *cmd) |
993 | 0 | { |
994 | 0 | int ret = wait_or_whine(cmd->pid, cmd->args.v[0], 0); |
995 | 0 | trace2_child_exit(cmd, ret); |
996 | 0 | child_process_clear(cmd); |
997 | 0 | invalidate_lstat_cache(); |
998 | 0 | return ret; |
999 | 0 | } |
1000 | | |
1001 | | int finish_command_in_signal(struct child_process *cmd) |
1002 | 0 | { |
1003 | 0 | int ret = wait_or_whine(cmd->pid, cmd->args.v[0], 1); |
1004 | 0 | if (ret != -1) |
1005 | 0 | trace2_child_exit(cmd, ret); |
1006 | 0 | return ret; |
1007 | 0 | } |
1008 | | |
1009 | | |
1010 | | int run_command(struct child_process *cmd) |
1011 | 0 | { |
1012 | 0 | int code; |
1013 | |
|
1014 | 0 | if (cmd->out < 0 || cmd->err < 0) |
1015 | 0 | BUG("run_command with a pipe can cause deadlock"); |
1016 | | |
1017 | 0 | code = start_command(cmd); |
1018 | 0 | if (code) |
1019 | 0 | return code; |
1020 | 0 | return finish_command(cmd); |
1021 | 0 | } |
1022 | | |
1023 | | #ifndef NO_PTHREADS |
1024 | | static pthread_t main_thread; |
1025 | | static int main_thread_set; |
1026 | | static pthread_key_t async_key; |
1027 | | static pthread_key_t async_die_counter; |
1028 | | |
1029 | | static void *run_thread(void *data) |
1030 | 0 | { |
1031 | 0 | struct async *async = data; |
1032 | 0 | intptr_t ret; |
1033 | |
|
1034 | 0 | if (async->isolate_sigpipe) { |
1035 | 0 | sigset_t mask; |
1036 | 0 | sigemptyset(&mask); |
1037 | 0 | sigaddset(&mask, SIGPIPE); |
1038 | 0 | if (pthread_sigmask(SIG_BLOCK, &mask, NULL)) { |
1039 | 0 | ret = error("unable to block SIGPIPE in async thread"); |
1040 | 0 | return (void *)ret; |
1041 | 0 | } |
1042 | 0 | } |
1043 | | |
1044 | 0 | pthread_setspecific(async_key, async); |
1045 | 0 | ret = async->proc(async->proc_in, async->proc_out, async->data); |
1046 | 0 | return (void *)ret; |
1047 | 0 | } |
1048 | | |
1049 | | static NORETURN void die_async(const char *err, va_list params) |
1050 | 0 | { |
1051 | 0 | report_fn die_message_fn = get_die_message_routine(); |
1052 | |
|
1053 | 0 | die_message_fn(err, params); |
1054 | |
|
1055 | 0 | if (in_async()) { |
1056 | 0 | struct async *async = pthread_getspecific(async_key); |
1057 | 0 | if (async->proc_in >= 0) |
1058 | 0 | close(async->proc_in); |
1059 | 0 | if (async->proc_out >= 0) |
1060 | 0 | close(async->proc_out); |
1061 | 0 | pthread_exit((void *)128); |
1062 | 0 | } |
1063 | | |
1064 | 0 | exit(128); |
1065 | 0 | } |
1066 | | |
1067 | | static int async_die_is_recursing(void) |
1068 | 0 | { |
1069 | 0 | void *ret = pthread_getspecific(async_die_counter); |
1070 | 0 | pthread_setspecific(async_die_counter, &async_die_counter); /* set to any non-NULL valid pointer */ |
1071 | 0 | return ret != NULL; |
1072 | 0 | } |
1073 | | |
1074 | | int in_async(void) |
1075 | 0 | { |
1076 | 0 | if (!main_thread_set) |
1077 | 0 | return 0; /* no asyncs started yet */ |
1078 | 0 | return !pthread_equal(main_thread, pthread_self()); |
1079 | 0 | } |
1080 | | |
1081 | | static void NORETURN async_exit(int code) |
1082 | 0 | { |
1083 | 0 | pthread_exit((void *)(intptr_t)code); |
1084 | 0 | } |
1085 | | |
1086 | | #else |
1087 | | |
1088 | | static struct { |
1089 | | void (**handlers)(void); |
1090 | | size_t nr; |
1091 | | size_t alloc; |
1092 | | } git_atexit_hdlrs; |
1093 | | |
1094 | | static int git_atexit_installed; |
1095 | | |
1096 | | static void git_atexit_dispatch(void) |
1097 | | { |
1098 | | size_t i; |
1099 | | |
1100 | | for (i=git_atexit_hdlrs.nr ; i ; i--) |
1101 | | git_atexit_hdlrs.handlers[i-1](); |
1102 | | } |
1103 | | |
1104 | | static void git_atexit_clear(void) |
1105 | | { |
1106 | | free(git_atexit_hdlrs.handlers); |
1107 | | memset(&git_atexit_hdlrs, 0, sizeof(git_atexit_hdlrs)); |
1108 | | git_atexit_installed = 0; |
1109 | | } |
1110 | | |
1111 | | #undef atexit |
1112 | | int git_atexit(void (*handler)(void)) |
1113 | | { |
1114 | | ALLOC_GROW(git_atexit_hdlrs.handlers, git_atexit_hdlrs.nr + 1, git_atexit_hdlrs.alloc); |
1115 | | git_atexit_hdlrs.handlers[git_atexit_hdlrs.nr++] = handler; |
1116 | | if (!git_atexit_installed) { |
1117 | | if (atexit(&git_atexit_dispatch)) |
1118 | | return -1; |
1119 | | git_atexit_installed = 1; |
1120 | | } |
1121 | | return 0; |
1122 | | } |
1123 | | #define atexit git_atexit |
1124 | | |
1125 | | static int process_is_async; |
1126 | | int in_async(void) |
1127 | | { |
1128 | | return process_is_async; |
1129 | | } |
1130 | | |
1131 | | static void NORETURN async_exit(int code) |
1132 | | { |
1133 | | exit(code); |
1134 | | } |
1135 | | |
1136 | | #endif |
1137 | | |
1138 | | void check_pipe(int err) |
1139 | 0 | { |
1140 | 0 | if (err == EPIPE) { |
1141 | 0 | if (in_async()) |
1142 | 0 | async_exit(141); |
1143 | | |
1144 | 0 | signal(SIGPIPE, SIG_DFL); |
1145 | 0 | raise(SIGPIPE); |
1146 | | /* Should never happen, but just in case... */ |
1147 | 0 | exit(141); |
1148 | 0 | } |
1149 | 0 | } |
1150 | | |
1151 | | int start_async(struct async *async) |
1152 | 0 | { |
1153 | 0 | int need_in, need_out; |
1154 | 0 | int fdin[2], fdout[2]; |
1155 | 0 | int proc_in, proc_out; |
1156 | |
|
1157 | 0 | need_in = async->in < 0; |
1158 | 0 | if (need_in) { |
1159 | 0 | if (pipe(fdin) < 0) { |
1160 | 0 | if (async->out > 0) |
1161 | 0 | close(async->out); |
1162 | 0 | return error_errno("cannot create pipe"); |
1163 | 0 | } |
1164 | 0 | async->in = fdin[1]; |
1165 | 0 | } |
1166 | | |
1167 | 0 | need_out = async->out < 0; |
1168 | 0 | if (need_out) { |
1169 | 0 | if (pipe(fdout) < 0) { |
1170 | 0 | if (need_in) |
1171 | 0 | close_pair(fdin); |
1172 | 0 | else if (async->in) |
1173 | 0 | close(async->in); |
1174 | 0 | return error_errno("cannot create pipe"); |
1175 | 0 | } |
1176 | 0 | async->out = fdout[0]; |
1177 | 0 | } |
1178 | | |
1179 | 0 | if (need_in) |
1180 | 0 | proc_in = fdin[0]; |
1181 | 0 | else if (async->in) |
1182 | 0 | proc_in = async->in; |
1183 | 0 | else |
1184 | 0 | proc_in = -1; |
1185 | |
|
1186 | 0 | if (need_out) |
1187 | 0 | proc_out = fdout[1]; |
1188 | 0 | else if (async->out) |
1189 | 0 | proc_out = async->out; |
1190 | 0 | else |
1191 | 0 | proc_out = -1; |
1192 | |
|
1193 | | #ifdef NO_PTHREADS |
1194 | | /* Flush stdio before fork() to avoid cloning buffers */ |
1195 | | fflush(NULL); |
1196 | | |
1197 | | async->pid = fork(); |
1198 | | if (async->pid < 0) { |
1199 | | error_errno("fork (async) failed"); |
1200 | | goto error; |
1201 | | } |
1202 | | if (!async->pid) { |
1203 | | if (need_in) |
1204 | | close(fdin[1]); |
1205 | | if (need_out) |
1206 | | close(fdout[0]); |
1207 | | git_atexit_clear(); |
1208 | | process_is_async = 1; |
1209 | | exit(!!async->proc(proc_in, proc_out, async->data)); |
1210 | | } |
1211 | | |
1212 | | mark_child_for_cleanup(async->pid, NULL); |
1213 | | |
1214 | | if (need_in) |
1215 | | close(fdin[0]); |
1216 | | else if (async->in) |
1217 | | close(async->in); |
1218 | | |
1219 | | if (need_out) |
1220 | | close(fdout[1]); |
1221 | | else if (async->out) |
1222 | | close(async->out); |
1223 | | #else |
1224 | 0 | if (!main_thread_set) { |
1225 | | /* |
1226 | | * We assume that the first time that start_async is called |
1227 | | * it is from the main thread. |
1228 | | */ |
1229 | 0 | main_thread_set = 1; |
1230 | 0 | main_thread = pthread_self(); |
1231 | 0 | pthread_key_create(&async_key, NULL); |
1232 | 0 | pthread_key_create(&async_die_counter, NULL); |
1233 | 0 | set_die_routine(die_async); |
1234 | 0 | set_die_is_recursing_routine(async_die_is_recursing); |
1235 | 0 | } |
1236 | |
|
1237 | 0 | if (proc_in >= 0) |
1238 | 0 | set_cloexec(proc_in); |
1239 | 0 | if (proc_out >= 0) |
1240 | 0 | set_cloexec(proc_out); |
1241 | 0 | async->proc_in = proc_in; |
1242 | 0 | async->proc_out = proc_out; |
1243 | 0 | { |
1244 | 0 | int err = pthread_create(&async->tid, NULL, run_thread, async); |
1245 | 0 | if (err) { |
1246 | 0 | error(_("cannot create async thread: %s"), strerror(err)); |
1247 | 0 | goto error; |
1248 | 0 | } |
1249 | 0 | } |
1250 | 0 | #endif |
1251 | 0 | return 0; |
1252 | | |
1253 | 0 | error: |
1254 | 0 | if (need_in) |
1255 | 0 | close_pair(fdin); |
1256 | 0 | else if (async->in) |
1257 | 0 | close(async->in); |
1258 | |
|
1259 | 0 | if (need_out) |
1260 | 0 | close_pair(fdout); |
1261 | 0 | else if (async->out) |
1262 | 0 | close(async->out); |
1263 | 0 | return -1; |
1264 | 0 | } |
1265 | | |
1266 | | int finish_async(struct async *async) |
1267 | 0 | { |
1268 | | #ifdef NO_PTHREADS |
1269 | | int ret = wait_or_whine(async->pid, "child process", 0); |
1270 | | |
1271 | | invalidate_lstat_cache(); |
1272 | | |
1273 | | return ret; |
1274 | | #else |
1275 | 0 | void *ret = (void *)(intptr_t)(-1); |
1276 | |
|
1277 | 0 | if (pthread_join(async->tid, &ret)) |
1278 | 0 | error("pthread_join failed"); |
1279 | 0 | invalidate_lstat_cache(); |
1280 | 0 | return (int)(intptr_t)ret; |
1281 | |
|
1282 | 0 | #endif |
1283 | 0 | } |
1284 | | |
1285 | | int async_with_fork(void) |
1286 | 0 | { |
1287 | | #ifdef NO_PTHREADS |
1288 | | return 1; |
1289 | | #else |
1290 | 0 | return 0; |
1291 | 0 | #endif |
1292 | 0 | } |
1293 | | |
1294 | | struct io_pump { |
1295 | | /* initialized by caller */ |
1296 | | int fd; |
1297 | | int type; /* POLLOUT or POLLIN */ |
1298 | | union { |
1299 | | struct { |
1300 | | const char *buf; |
1301 | | size_t len; |
1302 | | } out; |
1303 | | struct { |
1304 | | struct strbuf *buf; |
1305 | | size_t hint; |
1306 | | } in; |
1307 | | } u; |
1308 | | |
1309 | | /* returned by pump_io */ |
1310 | | int error; /* 0 for success, otherwise errno */ |
1311 | | |
1312 | | /* internal use */ |
1313 | | struct pollfd *pfd; |
1314 | | }; |
1315 | | |
1316 | | static int pump_io_round(struct io_pump *slots, int nr, struct pollfd *pfd) |
1317 | 0 | { |
1318 | 0 | int pollsize = 0; |
1319 | 0 | int i; |
1320 | |
|
1321 | 0 | for (i = 0; i < nr; i++) { |
1322 | 0 | struct io_pump *io = &slots[i]; |
1323 | 0 | if (io->fd < 0) |
1324 | 0 | continue; |
1325 | 0 | pfd[pollsize].fd = io->fd; |
1326 | 0 | pfd[pollsize].events = io->type; |
1327 | 0 | io->pfd = &pfd[pollsize++]; |
1328 | 0 | } |
1329 | |
|
1330 | 0 | if (!pollsize) |
1331 | 0 | return 0; |
1332 | | |
1333 | 0 | if (poll(pfd, pollsize, -1) < 0) { |
1334 | 0 | if (errno == EINTR) |
1335 | 0 | return 1; |
1336 | 0 | die_errno("poll failed"); |
1337 | 0 | } |
1338 | | |
1339 | 0 | for (i = 0; i < nr; i++) { |
1340 | 0 | struct io_pump *io = &slots[i]; |
1341 | |
|
1342 | 0 | if (io->fd < 0) |
1343 | 0 | continue; |
1344 | | |
1345 | 0 | if (!(io->pfd->revents & (POLLOUT|POLLIN|POLLHUP|POLLERR|POLLNVAL))) |
1346 | 0 | continue; |
1347 | | |
1348 | 0 | if (io->type == POLLOUT) { |
1349 | 0 | ssize_t len; |
1350 | | |
1351 | | /* |
1352 | | * Don't use xwrite() here. It loops forever on EAGAIN, |
1353 | | * and we're in our own poll() loop here. |
1354 | | * |
1355 | | * Note that we lose xwrite()'s handling of MAX_IO_SIZE |
1356 | | * and EINTR, so we have to implement those ourselves. |
1357 | | */ |
1358 | 0 | len = write(io->fd, io->u.out.buf, |
1359 | 0 | io->u.out.len <= MAX_IO_SIZE ? |
1360 | 0 | io->u.out.len : MAX_IO_SIZE); |
1361 | 0 | if (len < 0) { |
1362 | 0 | if (errno != EINTR && errno != EAGAIN && |
1363 | 0 | errno != ENOSPC) { |
1364 | 0 | io->error = errno; |
1365 | 0 | close(io->fd); |
1366 | 0 | io->fd = -1; |
1367 | 0 | } |
1368 | 0 | } else { |
1369 | 0 | io->u.out.buf += len; |
1370 | 0 | io->u.out.len -= len; |
1371 | 0 | if (!io->u.out.len) { |
1372 | 0 | close(io->fd); |
1373 | 0 | io->fd = -1; |
1374 | 0 | } |
1375 | 0 | } |
1376 | 0 | } |
1377 | |
|
1378 | 0 | if (io->type == POLLIN) { |
1379 | 0 | ssize_t len = strbuf_read_once(io->u.in.buf, |
1380 | 0 | io->fd, io->u.in.hint); |
1381 | 0 | if (len < 0) |
1382 | 0 | io->error = errno; |
1383 | 0 | if (len <= 0) { |
1384 | 0 | close(io->fd); |
1385 | 0 | io->fd = -1; |
1386 | 0 | } |
1387 | 0 | } |
1388 | 0 | } |
1389 | |
|
1390 | 0 | return 1; |
1391 | 0 | } |
1392 | | |
1393 | | static int pump_io(struct io_pump *slots, int nr) |
1394 | 0 | { |
1395 | 0 | struct pollfd *pfd; |
1396 | 0 | int i; |
1397 | |
|
1398 | 0 | for (i = 0; i < nr; i++) |
1399 | 0 | slots[i].error = 0; |
1400 | |
|
1401 | 0 | ALLOC_ARRAY(pfd, nr); |
1402 | 0 | while (pump_io_round(slots, nr, pfd)) |
1403 | 0 | ; /* nothing */ |
1404 | 0 | free(pfd); |
1405 | | |
1406 | | /* There may be multiple errno values, so just pick the first. */ |
1407 | 0 | for (i = 0; i < nr; i++) { |
1408 | 0 | if (slots[i].error) { |
1409 | 0 | errno = slots[i].error; |
1410 | 0 | return -1; |
1411 | 0 | } |
1412 | 0 | } |
1413 | 0 | return 0; |
1414 | 0 | } |
1415 | | |
1416 | | |
1417 | | int pipe_command(struct child_process *cmd, |
1418 | | const char *in, size_t in_len, |
1419 | | struct strbuf *out, size_t out_hint, |
1420 | | struct strbuf *err, size_t err_hint) |
1421 | 0 | { |
1422 | 0 | struct io_pump io[3]; |
1423 | 0 | int nr = 0; |
1424 | |
|
1425 | 0 | if (in) |
1426 | 0 | cmd->in = -1; |
1427 | 0 | if (out) |
1428 | 0 | cmd->out = -1; |
1429 | 0 | if (err) |
1430 | 0 | cmd->err = -1; |
1431 | |
|
1432 | 0 | if (start_command(cmd) < 0) |
1433 | 0 | return -1; |
1434 | | |
1435 | 0 | if (in) { |
1436 | 0 | if (enable_pipe_nonblock(cmd->in) < 0) { |
1437 | 0 | error_errno("unable to make pipe non-blocking"); |
1438 | 0 | close(cmd->in); |
1439 | 0 | if (out) |
1440 | 0 | close(cmd->out); |
1441 | 0 | if (err) |
1442 | 0 | close(cmd->err); |
1443 | 0 | return -1; |
1444 | 0 | } |
1445 | 0 | io[nr].fd = cmd->in; |
1446 | 0 | io[nr].type = POLLOUT; |
1447 | 0 | io[nr].u.out.buf = in; |
1448 | 0 | io[nr].u.out.len = in_len; |
1449 | 0 | nr++; |
1450 | 0 | } |
1451 | 0 | if (out) { |
1452 | 0 | io[nr].fd = cmd->out; |
1453 | 0 | io[nr].type = POLLIN; |
1454 | 0 | io[nr].u.in.buf = out; |
1455 | 0 | io[nr].u.in.hint = out_hint; |
1456 | 0 | nr++; |
1457 | 0 | } |
1458 | 0 | if (err) { |
1459 | 0 | io[nr].fd = cmd->err; |
1460 | 0 | io[nr].type = POLLIN; |
1461 | 0 | io[nr].u.in.buf = err; |
1462 | 0 | io[nr].u.in.hint = err_hint; |
1463 | 0 | nr++; |
1464 | 0 | } |
1465 | |
|
1466 | 0 | if (pump_io(io, nr) < 0) { |
1467 | 0 | finish_command(cmd); /* throw away exit code */ |
1468 | 0 | return -1; |
1469 | 0 | } |
1470 | | |
1471 | 0 | return finish_command(cmd); |
1472 | 0 | } |
1473 | | |
1474 | | enum child_state { |
1475 | | GIT_CP_FREE, |
1476 | | GIT_CP_WORKING, |
1477 | | GIT_CP_WAIT_CLEANUP, |
1478 | | }; |
1479 | | |
1480 | | struct parallel_child { |
1481 | | enum child_state state; |
1482 | | struct child_process process; |
1483 | | struct strbuf err; |
1484 | | void *data; |
1485 | | }; |
1486 | | |
1487 | | static int child_is_working(const struct parallel_child *pp_child) |
1488 | 0 | { |
1489 | 0 | return pp_child->state == GIT_CP_WORKING; |
1490 | 0 | } |
1491 | | |
1492 | | static int child_is_ready_for_cleanup(const struct parallel_child *pp_child) |
1493 | 0 | { |
1494 | 0 | return child_is_working(pp_child) && !pp_child->process.in; |
1495 | 0 | } |
1496 | | |
1497 | | static int child_is_receiving_input(const struct parallel_child *pp_child) |
1498 | 0 | { |
1499 | 0 | return child_is_working(pp_child) && pp_child->process.in > 0; |
1500 | 0 | } |
1501 | | static int child_is_sending_output(const struct parallel_child *pp_child) |
1502 | 0 | { |
1503 | | /* |
1504 | | * all pp children which buffer output through run_command via ungroup=0 |
1505 | | * redirect stdout to stderr, so we just need to check process.err. |
1506 | | */ |
1507 | 0 | return child_is_working(pp_child) && pp_child->process.err > 0; |
1508 | 0 | } |
1509 | | |
1510 | | struct parallel_processes { |
1511 | | size_t nr_processes; |
1512 | | |
1513 | | struct parallel_child *children; |
1514 | | /* |
1515 | | * The struct pollfd is logically part of *children, |
1516 | | * but the system call expects it as its own array. |
1517 | | */ |
1518 | | struct pollfd *pfd; |
1519 | | |
1520 | | unsigned shutdown : 1; |
1521 | | |
1522 | | size_t output_owner; |
1523 | | struct strbuf buffered_output; /* of finished children */ |
1524 | | }; |
1525 | | |
1526 | | struct parallel_processes_for_signal { |
1527 | | const struct run_process_parallel_opts *opts; |
1528 | | const struct parallel_processes *pp; |
1529 | | }; |
1530 | | |
1531 | | static void kill_children(const struct parallel_processes *pp, |
1532 | | const struct run_process_parallel_opts *opts, |
1533 | | int signo) |
1534 | 0 | { |
1535 | 0 | for (size_t i = 0; i < opts->processes; i++) |
1536 | 0 | if (child_is_working(&pp->children[i])) |
1537 | 0 | kill(pp->children[i].process.pid, signo); |
1538 | 0 | } |
1539 | | |
1540 | | static void kill_children_signal(const struct parallel_processes_for_signal *pp_sig, |
1541 | | int signo) |
1542 | 0 | { |
1543 | 0 | kill_children(pp_sig->pp, pp_sig->opts, signo); |
1544 | 0 | } |
1545 | | |
1546 | | static struct parallel_processes_for_signal *pp_for_signal; |
1547 | | |
1548 | | static void handle_children_on_signal(int signo) |
1549 | 0 | { |
1550 | 0 | kill_children_signal(pp_for_signal, signo); |
1551 | 0 | sigchain_pop(signo); |
1552 | 0 | raise(signo); |
1553 | 0 | } |
1554 | | |
1555 | | static void pp_init(struct parallel_processes *pp, |
1556 | | const struct run_process_parallel_opts *opts, |
1557 | | struct parallel_processes_for_signal *pp_sig) |
1558 | 0 | { |
1559 | 0 | const size_t n = opts->processes; |
1560 | |
|
1561 | 0 | if (!n) |
1562 | 0 | BUG("you must provide a non-zero number of processes!"); |
1563 | | |
1564 | 0 | trace_printf("run_processes_parallel: preparing to run up to %"PRIuMAX" tasks", |
1565 | 0 | (uintmax_t)n); |
1566 | |
|
1567 | 0 | if (!opts->get_next_task) |
1568 | 0 | BUG("you need to specify a get_next_task function"); |
1569 | | |
1570 | 0 | CALLOC_ARRAY(pp->children, n); |
1571 | 0 | if (!opts->ungroup) |
1572 | 0 | CALLOC_ARRAY(pp->pfd, n * 2); |
1573 | |
|
1574 | 0 | for (size_t i = 0; i < n; i++) { |
1575 | 0 | strbuf_init(&pp->children[i].err, 0); |
1576 | 0 | child_process_init(&pp->children[i].process); |
1577 | 0 | if (pp->pfd) { |
1578 | 0 | pp->pfd[i].events = POLLIN | POLLHUP; |
1579 | 0 | pp->pfd[i].fd = -1; |
1580 | 0 | } |
1581 | 0 | } |
1582 | |
|
1583 | 0 | pp_sig->pp = pp; |
1584 | 0 | pp_sig->opts = opts; |
1585 | 0 | pp_for_signal = pp_sig; |
1586 | 0 | sigchain_push_common(handle_children_on_signal); |
1587 | 0 | } |
1588 | | |
1589 | | static void pp_cleanup(struct parallel_processes *pp, |
1590 | | const struct run_process_parallel_opts *opts) |
1591 | 0 | { |
1592 | 0 | trace_printf("run_processes_parallel: done"); |
1593 | 0 | for (size_t i = 0; i < opts->processes; i++) { |
1594 | 0 | strbuf_release(&pp->children[i].err); |
1595 | 0 | child_process_clear(&pp->children[i].process); |
1596 | 0 | } |
1597 | |
|
1598 | 0 | free(pp->children); |
1599 | 0 | free(pp->pfd); |
1600 | | |
1601 | | /* |
1602 | | * When get_next_task added messages to the buffer in its last |
1603 | | * iteration, the buffered output is non empty. |
1604 | | */ |
1605 | 0 | strbuf_write(&pp->buffered_output, stderr); |
1606 | 0 | strbuf_release(&pp->buffered_output); |
1607 | |
|
1608 | 0 | sigchain_pop_common(); |
1609 | 0 | } |
1610 | | |
1611 | | /* returns |
1612 | | * 0 if a new task was started. |
1613 | | * 1 if no new jobs was started (get_next_task ran out of work, non critical |
1614 | | * problem with starting a new command) |
1615 | | * <0 no new job was started, user wishes to shutdown early. Use negative code |
1616 | | * to signal the children. |
1617 | | */ |
1618 | | static int pp_start_one(struct parallel_processes *pp, |
1619 | | const struct run_process_parallel_opts *opts) |
1620 | 0 | { |
1621 | 0 | size_t i; |
1622 | 0 | int code; |
1623 | |
|
1624 | 0 | for (i = 0; i < opts->processes; i++) |
1625 | 0 | if (pp->children[i].state == GIT_CP_FREE) |
1626 | 0 | break; |
1627 | 0 | if (i == opts->processes) |
1628 | 0 | BUG("bookkeeping is hard"); |
1629 | | |
1630 | | /* |
1631 | | * By default, do not inherit stdin from the parent process - otherwise, |
1632 | | * all children would share stdin! Users may overwrite this to provide |
1633 | | * something to the child's stdin by having their 'get_next_task' |
1634 | | * callback assign 0 to .no_stdin and an appropriate integer to .in. |
1635 | | */ |
1636 | 0 | pp->children[i].process.no_stdin = 1; |
1637 | |
|
1638 | 0 | code = opts->get_next_task(&pp->children[i].process, |
1639 | 0 | opts->ungroup ? NULL : &pp->children[i].err, |
1640 | 0 | opts->data, |
1641 | 0 | &pp->children[i].data); |
1642 | 0 | if (!code) { |
1643 | 0 | if (!opts->ungroup) { |
1644 | 0 | strbuf_addbuf(&pp->buffered_output, &pp->children[i].err); |
1645 | 0 | strbuf_reset(&pp->children[i].err); |
1646 | 0 | } |
1647 | 0 | return 1; |
1648 | 0 | } |
1649 | 0 | if (!opts->ungroup) { |
1650 | 0 | pp->children[i].process.err = -1; |
1651 | 0 | pp->children[i].process.stdout_to_stderr = 1; |
1652 | 0 | } |
1653 | |
|
1654 | 0 | if (start_command(&pp->children[i].process)) { |
1655 | 0 | if (opts->start_failure) |
1656 | 0 | code = opts->start_failure(opts->ungroup ? NULL : |
1657 | 0 | &pp->children[i].err, |
1658 | 0 | opts->data, |
1659 | 0 | pp->children[i].data); |
1660 | 0 | else |
1661 | 0 | code = 0; |
1662 | |
|
1663 | 0 | if (!opts->ungroup) { |
1664 | 0 | strbuf_addbuf(&pp->buffered_output, &pp->children[i].err); |
1665 | 0 | strbuf_reset(&pp->children[i].err); |
1666 | 0 | } |
1667 | 0 | if (code) |
1668 | 0 | pp->shutdown = 1; |
1669 | 0 | return code; |
1670 | 0 | } |
1671 | | |
1672 | 0 | pp->nr_processes++; |
1673 | 0 | pp->children[i].state = GIT_CP_WORKING; |
1674 | 0 | if (pp->pfd) |
1675 | 0 | pp->pfd[i].fd = pp->children[i].process.err; |
1676 | 0 | return 0; |
1677 | 0 | } |
1678 | | |
1679 | | static void pp_buffer_stdin(struct parallel_processes *pp, |
1680 | | const struct run_process_parallel_opts *opts) |
1681 | 0 | { |
1682 | | /* Buffer stdin for each pipe. */ |
1683 | 0 | for (size_t i = 0; i < opts->processes; i++) { |
1684 | 0 | struct child_process *proc = &pp->children[i].process; |
1685 | 0 | int ret; |
1686 | |
|
1687 | 0 | if (!child_is_receiving_input(&pp->children[i])) |
1688 | 0 | continue; |
1689 | | |
1690 | | /* |
1691 | | * child input is provided via path_to_stdin when the feed_pipe cb is |
1692 | | * missing, so we just signal an EOF. |
1693 | | */ |
1694 | 0 | if (!opts->feed_pipe) { |
1695 | 0 | close(proc->in); |
1696 | 0 | proc->in = 0; |
1697 | 0 | continue; |
1698 | 0 | } |
1699 | | |
1700 | | /** |
1701 | | * Feed the pipe: |
1702 | | * ret < 0 means error |
1703 | | * ret == 0 means there is more data to be fed |
1704 | | * ret > 0 means feeding finished |
1705 | | */ |
1706 | 0 | ret = opts->feed_pipe(proc->in, opts->data, pp->children[i].data); |
1707 | 0 | if (ret < 0) |
1708 | 0 | die_errno("feed_pipe"); |
1709 | | |
1710 | 0 | if (ret) { |
1711 | 0 | close(proc->in); |
1712 | 0 | proc->in = 0; |
1713 | 0 | } |
1714 | 0 | } |
1715 | 0 | } |
1716 | | |
1717 | | static void pp_buffer_io(struct parallel_processes *pp, |
1718 | | const struct run_process_parallel_opts *opts, |
1719 | | int timeout) |
1720 | 0 | { |
1721 | | /* for each potential child slot, prepare two pollfd entries */ |
1722 | 0 | for (size_t i = 0; i < opts->processes; i++) { |
1723 | 0 | if (child_is_sending_output(&pp->children[i])) { |
1724 | 0 | pp->pfd[2*i].fd = pp->children[i].process.err; |
1725 | 0 | pp->pfd[2*i].events = POLLIN | POLLHUP; |
1726 | 0 | } else { |
1727 | 0 | pp->pfd[2*i].fd = -1; |
1728 | 0 | } |
1729 | |
|
1730 | 0 | if (child_is_receiving_input(&pp->children[i])) { |
1731 | 0 | pp->pfd[2*i+1].fd = pp->children[i].process.in; |
1732 | 0 | pp->pfd[2*i+1].events = POLLOUT; |
1733 | 0 | } else { |
1734 | 0 | pp->pfd[2*i+1].fd = -1; |
1735 | 0 | } |
1736 | 0 | } |
1737 | |
|
1738 | 0 | while (poll(pp->pfd, opts->processes * 2, timeout) < 0) { |
1739 | 0 | if (errno == EINTR) |
1740 | 0 | continue; |
1741 | 0 | pp_cleanup(pp, opts); |
1742 | 0 | die_errno("poll"); |
1743 | 0 | } |
1744 | | |
1745 | 0 | for (size_t i = 0; i < opts->processes; i++) { |
1746 | | /* Handle input feeding (stdin) */ |
1747 | 0 | if (pp->pfd[2*i+1].revents & (POLLOUT | POLLHUP | POLLERR)) { |
1748 | 0 | if (opts->feed_pipe) { |
1749 | 0 | int ret = opts->feed_pipe(pp->children[i].process.in, |
1750 | 0 | opts->data, |
1751 | 0 | pp->children[i].data); |
1752 | 0 | if (ret < 0) |
1753 | 0 | die_errno("feed_pipe"); |
1754 | 0 | if (ret) { |
1755 | | /* done feeding */ |
1756 | 0 | close(pp->children[i].process.in); |
1757 | 0 | pp->children[i].process.in = 0; |
1758 | 0 | } |
1759 | 0 | } else { |
1760 | | /* |
1761 | | * No feed_pipe means there is nothing to do, so |
1762 | | * close the fd. Child input can be fed by other |
1763 | | * methods, such as opts->path_to_stdin which |
1764 | | * slurps a file via dup2, so clean up here. |
1765 | | */ |
1766 | 0 | close(pp->children[i].process.in); |
1767 | 0 | pp->children[i].process.in = 0; |
1768 | 0 | } |
1769 | 0 | } |
1770 | | |
1771 | | /* Handle output reading (stderr) */ |
1772 | 0 | if (child_is_working(&pp->children[i]) && |
1773 | 0 | pp->pfd[2*i].revents & (POLLIN | POLLHUP)) { |
1774 | 0 | int n = strbuf_read_once(&pp->children[i].err, |
1775 | 0 | pp->children[i].process.err, 0); |
1776 | 0 | if (n == 0) { |
1777 | 0 | close(pp->children[i].process.err); |
1778 | 0 | pp->children[i].state = GIT_CP_WAIT_CLEANUP; |
1779 | 0 | } else if (n < 0) |
1780 | 0 | if (errno != EAGAIN) |
1781 | 0 | die_errno("read"); |
1782 | 0 | } |
1783 | 0 | } |
1784 | 0 | } |
1785 | | |
1786 | | static void pp_output(const struct parallel_processes *pp) |
1787 | 0 | { |
1788 | 0 | size_t i = pp->output_owner; |
1789 | |
|
1790 | 0 | if (child_is_working(&pp->children[i]) && |
1791 | 0 | pp->children[i].err.len) { |
1792 | 0 | strbuf_write(&pp->children[i].err, stderr); |
1793 | 0 | strbuf_reset(&pp->children[i].err); |
1794 | 0 | } |
1795 | 0 | } |
1796 | | |
1797 | | static int pp_collect_finished(struct parallel_processes *pp, |
1798 | | const struct run_process_parallel_opts *opts) |
1799 | 0 | { |
1800 | 0 | int code; |
1801 | 0 | size_t i; |
1802 | 0 | int result = 0; |
1803 | |
|
1804 | 0 | while (pp->nr_processes > 0) { |
1805 | 0 | for (i = 0; i < opts->processes; i++) |
1806 | 0 | if (pp->children[i].state == GIT_CP_WAIT_CLEANUP) |
1807 | 0 | break; |
1808 | 0 | if (i == opts->processes) |
1809 | 0 | break; |
1810 | | |
1811 | 0 | code = finish_command(&pp->children[i].process); |
1812 | |
|
1813 | 0 | if (opts->task_finished) |
1814 | 0 | code = opts->task_finished(code, opts->ungroup ? NULL : |
1815 | 0 | &pp->children[i].err, opts->data, |
1816 | 0 | pp->children[i].data); |
1817 | 0 | else |
1818 | 0 | code = 0; |
1819 | |
|
1820 | 0 | if (code) |
1821 | 0 | result = code; |
1822 | 0 | if (code < 0) |
1823 | 0 | break; |
1824 | | |
1825 | 0 | pp->nr_processes--; |
1826 | 0 | pp->children[i].state = GIT_CP_FREE; |
1827 | 0 | if (pp->pfd) |
1828 | 0 | pp->pfd[i].fd = -1; |
1829 | 0 | pp->children[i].process.in = 0; |
1830 | 0 | child_process_init(&pp->children[i].process); |
1831 | |
|
1832 | 0 | if (opts->ungroup) { |
1833 | 0 | ; /* no strbuf_*() work to do here */ |
1834 | 0 | } else if (i != pp->output_owner) { |
1835 | 0 | strbuf_addbuf(&pp->buffered_output, &pp->children[i].err); |
1836 | 0 | strbuf_reset(&pp->children[i].err); |
1837 | 0 | } else { |
1838 | 0 | const size_t n = opts->processes; |
1839 | |
|
1840 | 0 | strbuf_write(&pp->children[i].err, stderr); |
1841 | 0 | strbuf_reset(&pp->children[i].err); |
1842 | | |
1843 | | /* Output all other finished child processes */ |
1844 | 0 | strbuf_write(&pp->buffered_output, stderr); |
1845 | 0 | strbuf_reset(&pp->buffered_output); |
1846 | | |
1847 | | /* |
1848 | | * Pick next process to output live. |
1849 | | * NEEDSWORK: |
1850 | | * For now we pick it randomly by doing a round |
1851 | | * robin. Later we may want to pick the one with |
1852 | | * the most output or the longest or shortest |
1853 | | * running process time. |
1854 | | */ |
1855 | 0 | for (i = 0; i < n; i++) |
1856 | 0 | if (child_is_working(&pp->children[(pp->output_owner + i) % n])) |
1857 | 0 | break; |
1858 | 0 | pp->output_owner = (pp->output_owner + i) % n; |
1859 | 0 | } |
1860 | 0 | } |
1861 | 0 | return result; |
1862 | 0 | } |
1863 | | |
1864 | | static void pp_handle_child_IO(struct parallel_processes *pp, |
1865 | | const struct run_process_parallel_opts *opts, |
1866 | | int timeout) |
1867 | 0 | { |
1868 | 0 | if (opts->ungroup) { |
1869 | 0 | pp_buffer_stdin(pp, opts); |
1870 | 0 | for (size_t i = 0; i < opts->processes; i++) |
1871 | 0 | if (child_is_ready_for_cleanup(&pp->children[i])) |
1872 | 0 | pp->children[i].state = GIT_CP_WAIT_CLEANUP; |
1873 | 0 | } else { |
1874 | 0 | pp_buffer_io(pp, opts, timeout); |
1875 | 0 | pp_output(pp); |
1876 | 0 | } |
1877 | 0 | } |
1878 | | |
1879 | | void run_processes_parallel(const struct run_process_parallel_opts *opts) |
1880 | 0 | { |
1881 | 0 | int i, code; |
1882 | 0 | int timeout = 100; |
1883 | 0 | int spawn_cap = 4; |
1884 | 0 | struct parallel_processes_for_signal pp_sig; |
1885 | 0 | struct parallel_processes pp = { |
1886 | 0 | .buffered_output = STRBUF_INIT, |
1887 | 0 | }; |
1888 | | /* options */ |
1889 | 0 | const char *tr2_category = opts->tr2_category; |
1890 | 0 | const char *tr2_label = opts->tr2_label; |
1891 | 0 | const int do_trace2 = tr2_category && tr2_label; |
1892 | |
|
1893 | 0 | if (do_trace2) |
1894 | 0 | trace2_region_enter_printf(tr2_category, tr2_label, NULL, |
1895 | 0 | "max:%"PRIuMAX, |
1896 | 0 | (uintmax_t)opts->processes); |
1897 | | |
1898 | | /* |
1899 | | * Child tasks might receive input via stdin, terminating early (or not), so |
1900 | | * ignore the default SIGPIPE which gets handled by each feed_pipe_fn which |
1901 | | * actually writes the data to children stdin fds. |
1902 | | */ |
1903 | 0 | sigchain_push(SIGPIPE, SIG_IGN); |
1904 | |
|
1905 | 0 | pp_init(&pp, opts, &pp_sig); |
1906 | 0 | while (1) { |
1907 | 0 | for (i = 0; |
1908 | 0 | i < spawn_cap && !pp.shutdown && |
1909 | 0 | pp.nr_processes < opts->processes; |
1910 | 0 | i++) { |
1911 | 0 | code = pp_start_one(&pp, opts); |
1912 | 0 | if (!code) |
1913 | 0 | continue; |
1914 | 0 | if (code < 0) { |
1915 | 0 | pp.shutdown = 1; |
1916 | 0 | kill_children(&pp, opts, -code); |
1917 | 0 | } |
1918 | 0 | break; |
1919 | 0 | } |
1920 | 0 | if (!pp.nr_processes) |
1921 | 0 | break; |
1922 | 0 | pp_handle_child_IO(&pp, opts, timeout); |
1923 | 0 | code = pp_collect_finished(&pp, opts); |
1924 | 0 | if (code) { |
1925 | 0 | pp.shutdown = 1; |
1926 | 0 | if (code < 0) |
1927 | 0 | kill_children(&pp, opts,-code); |
1928 | 0 | } |
1929 | 0 | } |
1930 | |
|
1931 | 0 | pp_cleanup(&pp, opts); |
1932 | |
|
1933 | 0 | sigchain_pop(SIGPIPE); |
1934 | |
|
1935 | 0 | if (do_trace2) |
1936 | 0 | trace2_region_leave(tr2_category, tr2_label, NULL); |
1937 | 0 | } |
1938 | | |
1939 | | int prepare_auto_maintenance(struct repository *r, int quiet, |
1940 | | struct child_process *maint) |
1941 | 0 | { |
1942 | 0 | int enabled, auto_detach; |
1943 | |
|
1944 | 0 | if (!repo_config_get_bool(r, "maintenance.auto", &enabled) && |
1945 | 0 | !enabled) |
1946 | 0 | return 0; |
1947 | | |
1948 | | /* |
1949 | | * When `maintenance.autoDetach` isn't set, then we fall back to |
1950 | | * honoring `gc.autoDetach`. This is somewhat weird, but required to |
1951 | | * retain behaviour from when we used to run git-gc(1) here. |
1952 | | */ |
1953 | 0 | if (repo_config_get_bool(r, "maintenance.autodetach", &auto_detach) && |
1954 | 0 | repo_config_get_bool(r, "gc.autodetach", &auto_detach)) |
1955 | 0 | auto_detach = git_env_bool("GIT_TEST_MAINT_AUTO_DETACH", true); |
1956 | |
|
1957 | 0 | maint->git_cmd = 1; |
1958 | 0 | maint->odb_to_close = r->objects; |
1959 | 0 | strvec_pushl(&maint->args, "maintenance", "run", "--auto", NULL); |
1960 | 0 | strvec_push(&maint->args, quiet ? "--quiet" : "--no-quiet"); |
1961 | 0 | strvec_push(&maint->args, auto_detach ? "--detach" : "--no-detach"); |
1962 | |
|
1963 | 0 | return 1; |
1964 | 0 | } |
1965 | | |
1966 | | int run_auto_maintenance(struct repository *r, int quiet) |
1967 | 0 | { |
1968 | 0 | struct child_process maint = CHILD_PROCESS_INIT; |
1969 | 0 | if (!prepare_auto_maintenance(r, quiet, &maint)) |
1970 | 0 | return 0; |
1971 | 0 | return run_command(&maint); |
1972 | 0 | } |
1973 | | |
1974 | | void sanitize_repo_env(struct strvec *env) |
1975 | 0 | { |
1976 | 0 | const char * const *var; |
1977 | |
|
1978 | 0 | for (var = local_repo_env; *var; var++) { |
1979 | 0 | if (strcmp(*var, CONFIG_DATA_ENVIRONMENT) && |
1980 | 0 | strcmp(*var, CONFIG_COUNT_ENVIRONMENT)) |
1981 | 0 | strvec_push(env, *var); |
1982 | 0 | } |
1983 | 0 | } |
1984 | | |
1985 | | void prepare_other_repo_env(struct strvec *env, const char *new_git_dir) |
1986 | 0 | { |
1987 | 0 | sanitize_repo_env(env); |
1988 | 0 | strvec_pushf(env, "%s=%s", GIT_DIR_ENVIRONMENT, new_git_dir); |
1989 | 0 | } |
1990 | | |
1991 | | enum start_bg_result start_bg_command(struct child_process *cmd, |
1992 | | start_bg_wait_cb *wait_cb, |
1993 | | void *cb_data, |
1994 | | unsigned int timeout_sec) |
1995 | 0 | { |
1996 | 0 | enum start_bg_result sbgr = SBGR_ERROR; |
1997 | 0 | int ret; |
1998 | 0 | int wait_status; |
1999 | 0 | pid_t pid_seen; |
2000 | 0 | time_t time_limit; |
2001 | | |
2002 | | /* |
2003 | | * We do not allow clean-on-exit because the child process |
2004 | | * should persist in the background and possibly/probably |
2005 | | * after this process exits. So we don't want to kill the |
2006 | | * child during our atexit routine. |
2007 | | */ |
2008 | 0 | if (cmd->clean_on_exit) |
2009 | 0 | BUG("start_bg_command() does not allow non-zero clean_on_exit"); |
2010 | | |
2011 | 0 | if (!cmd->trace2_child_class) |
2012 | 0 | cmd->trace2_child_class = "background"; |
2013 | |
|
2014 | 0 | ret = start_command(cmd); |
2015 | 0 | if (ret) { |
2016 | | /* |
2017 | | * We assume that if `start_command()` fails, we |
2018 | | * either get a complete `trace2_child_start() / |
2019 | | * trace2_child_exit()` pair or it fails before the |
2020 | | * `trace2_child_start()` is emitted, so we do not |
2021 | | * need to worry about it here. |
2022 | | * |
2023 | | * We also assume that `start_command()` does not add |
2024 | | * us to the cleanup list. And that it calls |
2025 | | * `child_process_clear()`. |
2026 | | */ |
2027 | 0 | sbgr = SBGR_ERROR; |
2028 | 0 | goto done; |
2029 | 0 | } |
2030 | | |
2031 | 0 | time(&time_limit); |
2032 | 0 | time_limit += timeout_sec; |
2033 | |
|
2034 | 0 | wait: |
2035 | 0 | pid_seen = waitpid(cmd->pid, &wait_status, WNOHANG); |
2036 | |
|
2037 | 0 | if (!pid_seen) { |
2038 | | /* |
2039 | | * The child is currently running. Ask the callback |
2040 | | * if the child is ready to do work or whether we |
2041 | | * should keep waiting for it to boot up. |
2042 | | */ |
2043 | 0 | ret = (*wait_cb)(cmd, cb_data); |
2044 | 0 | if (!ret) { |
2045 | | /* |
2046 | | * The child is running and "ready". |
2047 | | */ |
2048 | 0 | trace2_child_ready(cmd, "ready"); |
2049 | 0 | sbgr = SBGR_READY; |
2050 | 0 | goto done; |
2051 | 0 | } else if (ret > 0) { |
2052 | | /* |
2053 | | * The callback said to give it more time to boot up |
2054 | | * (subject to our timeout limit). |
2055 | | */ |
2056 | 0 | time_t now; |
2057 | |
|
2058 | 0 | time(&now); |
2059 | 0 | if (now < time_limit) |
2060 | 0 | goto wait; |
2061 | | |
2062 | | /* |
2063 | | * Our timeout has expired. We don't try to |
2064 | | * kill the child, but rather let it continue |
2065 | | * (hopefully) trying to startup. |
2066 | | */ |
2067 | 0 | trace2_child_ready(cmd, "timeout"); |
2068 | 0 | sbgr = SBGR_TIMEOUT; |
2069 | 0 | goto done; |
2070 | 0 | } else { |
2071 | | /* |
2072 | | * The cb gave up on this child. It is still running, |
2073 | | * but our cb got an error trying to probe it. |
2074 | | */ |
2075 | 0 | trace2_child_ready(cmd, "error"); |
2076 | 0 | sbgr = SBGR_CB_ERROR; |
2077 | 0 | goto done; |
2078 | 0 | } |
2079 | 0 | } |
2080 | | |
2081 | 0 | else if (pid_seen == cmd->pid) { |
2082 | 0 | int child_code = -1; |
2083 | | |
2084 | | /* |
2085 | | * The child started, but exited or was terminated |
2086 | | * before becoming "ready". |
2087 | | * |
2088 | | * We try to match the behavior of `wait_or_whine()` |
2089 | | * WRT the handling of WIFSIGNALED() and WIFEXITED() |
2090 | | * and convert the child's status to a return code for |
2091 | | * tracing purposes and emit the `trace2_child_exit()` |
2092 | | * event. |
2093 | | * |
2094 | | * We do not want the wait_or_whine() error message |
2095 | | * because we will be called by client-side library |
2096 | | * routines. |
2097 | | */ |
2098 | 0 | if (WIFEXITED(wait_status)) |
2099 | 0 | child_code = WEXITSTATUS(wait_status); |
2100 | 0 | else if (WIFSIGNALED(wait_status)) |
2101 | 0 | child_code = WTERMSIG(wait_status) + 128; |
2102 | 0 | trace2_child_exit(cmd, child_code); |
2103 | |
|
2104 | 0 | sbgr = SBGR_DIED; |
2105 | 0 | goto done; |
2106 | 0 | } |
2107 | | |
2108 | 0 | else if (pid_seen < 0 && errno == EINTR) |
2109 | 0 | goto wait; |
2110 | | |
2111 | 0 | trace2_child_exit(cmd, -1); |
2112 | 0 | sbgr = SBGR_ERROR; |
2113 | |
|
2114 | 0 | done: |
2115 | 0 | child_process_clear(cmd); |
2116 | 0 | invalidate_lstat_cache(); |
2117 | 0 | return sbgr; |
2118 | 0 | } |