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