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