run-command: don't die in child when duping /dev/null
[git/git.git] / run-command.c
CommitLineData
b1bf95bb
JW
1#include "cache.h"
2#include "run-command.h"
77cb17e9 3#include "exec_cmd.h"
afe19ff7 4#include "sigchain.h"
5d40a179 5#include "argv-array.h"
c553c72e
SB
6#include "thread-utils.h"
7#include "strbuf.h"
b1bf95bb 8
483bbd4e
RS
9void child_process_init(struct child_process *child)
10{
11 memset(child, 0, sizeof(*child));
12 argv_array_init(&child->args);
19a583dc 13 argv_array_init(&child->env_array);
483bbd4e
RS
14}
15
2d71608e
RS
16void child_process_clear(struct child_process *child)
17{
18 argv_array_clear(&child->args);
19 argv_array_clear(&child->env_array);
20}
21
afe19ff7
JK
22struct child_to_clean {
23 pid_t pid;
ac2fbaa6 24 struct child_process *process;
afe19ff7
JK
25 struct child_to_clean *next;
26};
27static struct child_to_clean *children_to_clean;
28static int installed_child_cleanup_handler;
29
507d7804 30static void cleanup_children(int sig, int in_signal)
afe19ff7 31{
46df6906
JK
32 struct child_to_clean *children_to_wait_for = NULL;
33
afe19ff7
JK
34 while (children_to_clean) {
35 struct child_to_clean *p = children_to_clean;
36 children_to_clean = p->next;
ac2fbaa6
LS
37
38 if (p->process && !in_signal) {
39 struct child_process *process = p->process;
40 if (process->clean_on_exit_handler) {
41 trace_printf(
42 "trace: run_command: running exit handler for pid %"
43 PRIuMAX, (uintmax_t)p->pid
44 );
45 process->clean_on_exit_handler(process);
46 }
47 }
48
afe19ff7 49 kill(p->pid, sig);
46df6906 50
7b91929b 51 if (p->process && p->process->wait_after_clean) {
46df6906
JK
52 p->next = children_to_wait_for;
53 children_to_wait_for = p;
54 } else {
55 if (!in_signal)
56 free(p);
57 }
58 }
59
60 while (children_to_wait_for) {
61 struct child_to_clean *p = children_to_wait_for;
62 children_to_wait_for = p->next;
63
64 while (waitpid(p->pid, NULL, 0) < 0 && errno == EINTR)
65 ; /* spin waiting for process exit or error */
66
507d7804
TI
67 if (!in_signal)
68 free(p);
afe19ff7
JK
69 }
70}
71
72static void cleanup_children_on_signal(int sig)
73{
507d7804 74 cleanup_children(sig, 1);
afe19ff7
JK
75 sigchain_pop(sig);
76 raise(sig);
77}
78
79static void cleanup_children_on_exit(void)
80{
507d7804 81 cleanup_children(SIGTERM, 0);
afe19ff7
JK
82}
83
ac2fbaa6 84static void mark_child_for_cleanup(pid_t pid, struct child_process *process)
afe19ff7
JK
85{
86 struct child_to_clean *p = xmalloc(sizeof(*p));
87 p->pid = pid;
ac2fbaa6 88 p->process = process;
afe19ff7
JK
89 p->next = children_to_clean;
90 children_to_clean = p;
91
92 if (!installed_child_cleanup_handler) {
93 atexit(cleanup_children_on_exit);
94 sigchain_push_common(cleanup_children_on_signal);
95 installed_child_cleanup_handler = 1;
96 }
97}
98
99static void clear_child_for_cleanup(pid_t pid)
100{
bdee397d 101 struct child_to_clean **pp;
afe19ff7 102
bdee397d
DG
103 for (pp = &children_to_clean; *pp; pp = &(*pp)->next) {
104 struct child_to_clean *clean_me = *pp;
105
106 if (clean_me->pid == pid) {
107 *pp = clean_me->next;
108 free(clean_me);
afe19ff7
JK
109 return;
110 }
111 }
112}
113
9dc09c76
SP
114static inline void close_pair(int fd[2])
115{
116 close(fd[0]);
117 close(fd[1]);
118}
119
38f865c2
JK
120static char *locate_in_PATH(const char *file)
121{
122 const char *p = getenv("PATH");
123 struct strbuf buf = STRBUF_INIT;
124
125 if (!p || !*p)
126 return NULL;
127
128 while (1) {
129 const char *end = strchrnul(p, ':');
130
131 strbuf_reset(&buf);
132
133 /* POSIX specifies an empty entry as the current directory. */
134 if (end != p) {
135 strbuf_add(&buf, p, end - p);
136 strbuf_addch(&buf, '/');
137 }
138 strbuf_addstr(&buf, file);
139
140 if (!access(buf.buf, F_OK))
141 return strbuf_detach(&buf, NULL);
142
143 if (!*end)
144 break;
145 p = end + 1;
146 }
147
148 strbuf_release(&buf);
149 return NULL;
150}
151
152static int exists_in_PATH(const char *file)
153{
154 char *r = locate_in_PATH(file);
155 free(r);
156 return r != NULL;
157}
158
159int sane_execvp(const char *file, char * const argv[])
160{
161 if (!execvp(file, argv))
162 return 0; /* cannot happen ;-) */
163
164 /*
165 * When a command can't be found because one of the directories
166 * listed in $PATH is unsearchable, execvp reports EACCES, but
167 * careful usability testing (read: analysis of occasional bug
168 * reports) reveals that "No such file or directory" is more
169 * intuitive.
170 *
171 * We avoid commands with "/", because execvp will not do $PATH
172 * lookups in that case.
173 *
174 * The reassignment of EACCES to errno looks like a no-op below,
175 * but we need to protect against exists_in_PATH overwriting errno.
176 */
177 if (errno == EACCES && !strchr(file, '/'))
178 errno = exists_in_PATH(file) ? EACCES : ENOENT;
a7855083
JH
179 else if (errno == ENOTDIR && !strchr(file, '/'))
180 errno = ENOENT;
38f865c2
JK
181 return -1;
182}
183
20574f55 184static const char **prepare_shell_cmd(struct argv_array *out, const char **argv)
8dba1e63 185{
20574f55 186 if (!argv[0])
8dba1e63
JK
187 die("BUG: shell command is empty");
188
f445644f 189 if (strcspn(argv[0], "|&;<>()$`\\\"' \t\n*?[#~=%") != strlen(argv[0])) {
380395d0 190#ifndef GIT_WINDOWS_NATIVE
20574f55 191 argv_array_push(out, SHELL_PATH);
77629754 192#else
20574f55 193 argv_array_push(out, "sh");
77629754 194#endif
20574f55 195 argv_array_push(out, "-c");
8dba1e63 196
20574f55
JK
197 /*
198 * If we have no extra arguments, we do not even need to
199 * bother with the "$@" magic.
200 */
201 if (!argv[1])
202 argv_array_push(out, argv[0]);
203 else
204 argv_array_pushf(out, "%s \"$@\"", argv[0]);
205 }
8dba1e63 206
20574f55
JK
207 argv_array_pushv(out, argv);
208 return out->argv;
8dba1e63
JK
209}
210
380395d0 211#ifndef GIT_WINDOWS_NATIVE
2b541bf8
JS
212static int child_notifier = -1;
213
214static void notify_parent(void)
215{
a111eb78
JN
216 /*
217 * execvp failed. If possible, we'd like to let start_command
218 * know, so failures like ENOENT can be handled right away; but
219 * otherwise, finish_command will still report the error.
220 */
221 xwrite(child_notifier, "", 1);
2b541bf8 222}
3967e25b
BW
223
224static void prepare_cmd(struct argv_array *out, const struct child_process *cmd)
225{
226 if (!cmd->argv[0])
227 die("BUG: command is empty");
228
e3a43446
BW
229 /*
230 * Add SHELL_PATH so in the event exec fails with ENOEXEC we can
231 * attempt to interpret the command with 'sh'.
232 */
233 argv_array_push(out, SHELL_PATH);
234
3967e25b
BW
235 if (cmd->git_cmd) {
236 argv_array_push(out, "git");
237 argv_array_pushv(out, cmd->argv);
238 } else if (cmd->use_shell) {
239 prepare_shell_cmd(out, cmd->argv);
240 } else {
241 argv_array_pushv(out, cmd->argv);
242 }
e3a43446
BW
243
244 /*
245 * If there are no '/' characters in the command then perform a path
246 * lookup and use the resolved path as the command to exec. If there
247 * are no '/' characters or if the command wasn't found in the path,
248 * have exec attempt to invoke the command directly.
249 */
250 if (!strchr(out->argv[1], '/')) {
251 char *program = locate_in_PATH(out->argv[1]);
252 if (program) {
253 free((char *)out->argv[1]);
254 out->argv[1] = program;
255 }
256 }
3967e25b 257}
ae25394b
BW
258
259static char **prep_childenv(const char *const *deltaenv)
260{
261 extern char **environ;
262 char **childenv;
263 struct string_list env = STRING_LIST_INIT_DUP;
264 struct strbuf key = STRBUF_INIT;
265 const char *const *p;
266 int i;
267
268 /* Construct a sorted string list consisting of the current environ */
269 for (p = (const char *const *) environ; p && *p; p++) {
270 const char *equals = strchr(*p, '=');
271
272 if (equals) {
273 strbuf_reset(&key);
274 strbuf_add(&key, *p, equals - *p);
275 string_list_append(&env, key.buf)->util = (void *) *p;
276 } else {
277 string_list_append(&env, *p)->util = (void *) *p;
278 }
279 }
280 string_list_sort(&env);
281
282 /* Merge in 'deltaenv' with the current environ */
283 for (p = deltaenv; p && *p; p++) {
284 const char *equals = strchr(*p, '=');
285
286 if (equals) {
287 /* ('key=value'), insert or replace entry */
288 strbuf_reset(&key);
289 strbuf_add(&key, *p, equals - *p);
290 string_list_insert(&env, key.buf)->util = (void *) *p;
291 } else {
292 /* otherwise ('key') remove existing entry */
293 string_list_remove(&env, *p, 0);
294 }
295 }
296
297 /* Create an array of 'char *' to be used as the childenv */
298 childenv = xmalloc((env.nr + 1) * sizeof(char *));
299 for (i = 0; i < env.nr; i++)
300 childenv[i] = env.items[i].util;
301 childenv[env.nr] = NULL;
302
303 string_list_clear(&env, 0);
304 strbuf_release(&key);
305 return childenv;
306}
200a76b7 307#endif
a5487ddf
JS
308
309static inline void set_cloexec(int fd)
310{
311 int flags = fcntl(fd, F_GETFD);
312 if (flags >= 0)
313 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
314}
a5487ddf 315
507d7804 316static int wait_or_whine(pid_t pid, const char *argv0, int in_signal)
ab0b41da
JS
317{
318 int status, code = -1;
319 pid_t waiting;
320 int failed_errno = 0;
321
322 while ((waiting = waitpid(pid, &status, 0)) < 0 && errno == EINTR)
323 ; /* nothing */
507d7804
TI
324 if (in_signal)
325 return 0;
ab0b41da
JS
326
327 if (waiting < 0) {
328 failed_errno = errno;
fbcb0e06 329 error_errno("waitpid for %s failed", argv0);
ab0b41da
JS
330 } else if (waiting != pid) {
331 error("waitpid is confused (%s)", argv0);
332 } else if (WIFSIGNALED(status)) {
333 code = WTERMSIG(status);
ac78663b 334 if (code != SIGINT && code != SIGQUIT && code != SIGPIPE)
a2767c5c 335 error("%s died of signal %d", argv0, code);
ab0b41da
JS
336 /*
337 * This return value is chosen so that code & 0xff
338 * mimics the exit code that a POSIX shell would report for
339 * a program that died from this signal.
340 */
709ca730 341 code += 128;
ab0b41da
JS
342 } else if (WIFEXITED(status)) {
343 code = WEXITSTATUS(status);
344 /*
345 * Convert special exit code when execvp failed.
346 */
347 if (code == 127) {
348 code = -1;
349 failed_errno = ENOENT;
ab0b41da
JS
350 }
351 } else {
352 error("waitpid is confused (%s)", argv0);
353 }
afe19ff7
JK
354
355 clear_child_for_cleanup(pid);
356
ab0b41da
JS
357 errno = failed_errno;
358 return code;
359}
360
ebcb5d16 361int start_command(struct child_process *cmd)
b1bf95bb 362{
f3b33f1d
JS
363 int need_in, need_out, need_err;
364 int fdin[2], fdout[2], fderr[2];
25043d8a 365 int failed_errno;
939296c4 366 char *str;
4919bf03 367
c460c0ec
JK
368 if (!cmd->argv)
369 cmd->argv = cmd->args.argv;
19a583dc
RS
370 if (!cmd->env)
371 cmd->env = cmd->env_array.argv;
c460c0ec 372
c20181e3
JS
373 /*
374 * In case of errors we must keep the promise to close FDs
375 * that have been passed in via ->in and ->out.
376 */
377
e4507ae8 378 need_in = !cmd->no_stdin && cmd->in < 0;
4919bf03 379 if (need_in) {
c20181e3 380 if (pipe(fdin) < 0) {
0ac77ec3 381 failed_errno = errno;
c20181e3
JS
382 if (cmd->out > 0)
383 close(cmd->out);
939296c4 384 str = "standard input";
0ac77ec3 385 goto fail_pipe;
c20181e3 386 }
4919bf03 387 cmd->in = fdin[1];
4919bf03
SP
388 }
389
e4507ae8
SP
390 need_out = !cmd->no_stdout
391 && !cmd->stdout_to_stderr
392 && cmd->out < 0;
f4bba25b
SP
393 if (need_out) {
394 if (pipe(fdout) < 0) {
0ac77ec3 395 failed_errno = errno;
f4bba25b
SP
396 if (need_in)
397 close_pair(fdin);
c20181e3
JS
398 else if (cmd->in)
399 close(cmd->in);
939296c4 400 str = "standard output";
0ac77ec3 401 goto fail_pipe;
f4bba25b
SP
402 }
403 cmd->out = fdout[0];
f4bba25b
SP
404 }
405
b73a4397 406 need_err = !cmd->no_stderr && cmd->err < 0;
f3b33f1d
JS
407 if (need_err) {
408 if (pipe(fderr) < 0) {
0ac77ec3 409 failed_errno = errno;
f3b33f1d
JS
410 if (need_in)
411 close_pair(fdin);
c20181e3
JS
412 else if (cmd->in)
413 close(cmd->in);
f3b33f1d
JS
414 if (need_out)
415 close_pair(fdout);
c20181e3
JS
416 else if (cmd->out)
417 close(cmd->out);
939296c4 418 str = "standard error";
0ac77ec3 419fail_pipe:
939296c4
SB
420 error("cannot create %s pipe for %s: %s",
421 str, cmd->argv[0], strerror(failed_errno));
2d71608e 422 child_process_clear(cmd);
0ac77ec3
JS
423 errno = failed_errno;
424 return -1;
f3b33f1d
JS
425 }
426 cmd->err = fderr[0];
427 }
428
8852f5d7 429 trace_argv_printf(cmd->argv, "trace: run_command:");
13af8cbd 430 fflush(NULL);
8852f5d7 431
380395d0 432#ifndef GIT_WINDOWS_NATIVE
2b541bf8
JS
433{
434 int notify_pipe[2];
db015a28 435 int null_fd = -1;
ae25394b 436 char **childenv;
3967e25b
BW
437 struct argv_array argv = ARGV_ARRAY_INIT;
438
2b541bf8
JS
439 if (pipe(notify_pipe))
440 notify_pipe[0] = notify_pipe[1] = -1;
441
db015a28
BW
442 if (cmd->no_stdin || cmd->no_stdout || cmd->no_stderr) {
443 null_fd = open("/dev/null", O_RDWR | O_CLOEXEC);
444 if (null_fd < 0)
445 die_errno(_("open /dev/null failed"));
446 set_cloexec(null_fd);
447 }
448
3967e25b 449 prepare_cmd(&argv, cmd);
ae25394b 450 childenv = prep_childenv(cmd->env);
3967e25b 451
ebcb5d16 452 cmd->pid = fork();
25043d8a 453 failed_errno = errno;
ebcb5d16 454 if (!cmd->pid) {
a5487ddf
JS
455 /*
456 * Redirect the channel to write syscall error messages to
457 * before redirecting the process's stderr so that all die()
458 * in subsequent call paths use the parent's stderr.
459 */
460 if (cmd->no_stderr || need_err) {
3b331e92 461 int child_err = dup(2);
a5487ddf 462 set_cloexec(child_err);
3b331e92 463 set_error_handle(fdopen(child_err, "w"));
a5487ddf 464 }
a5487ddf 465
2b541bf8
JS
466 close(notify_pipe[0]);
467 set_cloexec(notify_pipe[1]);
468 child_notifier = notify_pipe[1];
469 atexit(notify_parent);
470
e4507ae8 471 if (cmd->no_stdin)
db015a28 472 dup2(null_fd, 0);
e4507ae8 473 else if (need_in) {
4919bf03 474 dup2(fdin[0], 0);
9dc09c76 475 close_pair(fdin);
4919bf03
SP
476 } else if (cmd->in) {
477 dup2(cmd->in, 0);
478 close(cmd->in);
95d3c4f5 479 }
4919bf03 480
ce2cf27a 481 if (cmd->no_stderr)
db015a28 482 dup2(null_fd, 2);
ce2cf27a
CC
483 else if (need_err) {
484 dup2(fderr[1], 2);
485 close_pair(fderr);
4f41b611
SP
486 } else if (cmd->err > 1) {
487 dup2(cmd->err, 2);
488 close(cmd->err);
ce2cf27a
CC
489 }
490
e4507ae8 491 if (cmd->no_stdout)
db015a28 492 dup2(null_fd, 1);
e4507ae8 493 else if (cmd->stdout_to_stderr)
cd83c74c 494 dup2(2, 1);
f4bba25b
SP
495 else if (need_out) {
496 dup2(fdout[1], 1);
497 close_pair(fdout);
498 } else if (cmd->out > 1) {
499 dup2(cmd->out, 1);
500 close(cmd->out);
501 }
502
1568fea0 503 if (cmd->dir && chdir(cmd->dir))
d824cbba
TR
504 die_errno("exec '%s': cd to '%s' failed", cmd->argv[0],
505 cmd->dir);
3967e25b 506
e3a43446
BW
507 /*
508 * Attempt to exec using the command and arguments starting at
509 * argv.argv[1]. argv.argv[0] contains SHELL_PATH which will
510 * be used in the event exec failed with ENOEXEC at which point
511 * we will try to interpret the command using 'sh'.
512 */
ae25394b
BW
513 execve(argv.argv[1], (char *const *) argv.argv + 1,
514 (char *const *) childenv);
e3a43446 515 if (errno == ENOEXEC)
ae25394b
BW
516 execve(argv.argv[0], (char *const *) argv.argv,
517 (char *const *) childenv);
3967e25b 518
fc1b56f0
CB
519 if (errno == ENOENT) {
520 if (!cmd->silent_exec_failure)
521 error("cannot run %s: %s", cmd->argv[0],
522 strerror(ENOENT));
a5487ddf 523 exit(127);
fc1b56f0 524 } else {
a5487ddf 525 die_errno("cannot exec '%s'", cmd->argv[0]);
fc1b56f0 526 }
b1bf95bb 527 }
0ac77ec3 528 if (cmd->pid < 0)
fbcb0e06 529 error_errno("cannot fork() for %s", cmd->argv[0]);
afe19ff7 530 else if (cmd->clean_on_exit)
ac2fbaa6 531 mark_child_for_cleanup(cmd->pid, cmd);
2b541bf8
JS
532
533 /*
3967e25b 534 * Wait for child's exec. If the exec succeeds (or if fork()
2b541bf8
JS
535 * failed), EOF is seen immediately by the parent. Otherwise, the
536 * child process sends a single byte.
537 * Note that use of this infrastructure is completely advisory,
538 * therefore, we keep error checks minimal.
539 */
540 close(notify_pipe[1]);
541 if (read(notify_pipe[0], &notify_pipe[1], 1) == 1) {
542 /*
3967e25b 543 * At this point we know that fork() succeeded, but exec()
2b541bf8
JS
544 * failed. Errors have been reported to our stderr.
545 */
507d7804 546 wait_or_whine(cmd->pid, cmd->argv[0], 0);
2b541bf8
JS
547 failed_errno = errno;
548 cmd->pid = -1;
549 }
550 close(notify_pipe[0]);
3967e25b 551
db015a28
BW
552 if (null_fd >= 0)
553 close(null_fd);
3967e25b 554 argv_array_clear(&argv);
ae25394b 555 free(childenv);
2b541bf8 556}
ba26f296 557#else
0d30ad71 558{
75301f90 559 int fhin = 0, fhout = 1, fherr = 2;
108ac313 560 const char **sargv = cmd->argv;
20574f55 561 struct argv_array nargv = ARGV_ARRAY_INIT;
ba26f296 562
75301f90
JS
563 if (cmd->no_stdin)
564 fhin = open("/dev/null", O_RDWR);
565 else if (need_in)
566 fhin = dup(fdin[0]);
567 else if (cmd->in)
568 fhin = dup(cmd->in);
569
570 if (cmd->no_stderr)
571 fherr = open("/dev/null", O_RDWR);
572 else if (need_err)
573 fherr = dup(fderr[1]);
76d44c8c
JH
574 else if (cmd->err > 2)
575 fherr = dup(cmd->err);
75301f90
JS
576
577 if (cmd->no_stdout)
578 fhout = open("/dev/null", O_RDWR);
579 else if (cmd->stdout_to_stderr)
580 fhout = dup(fherr);
581 else if (need_out)
582 fhout = dup(fdout[1]);
583 else if (cmd->out > 1)
584 fhout = dup(cmd->out);
ba26f296 585
5a50085c 586 if (cmd->git_cmd)
20574f55 587 cmd->argv = prepare_git_cmd(&nargv, cmd->argv);
5a50085c 588 else if (cmd->use_shell)
20574f55 589 cmd->argv = prepare_shell_cmd(&nargv, cmd->argv);
ba26f296 590
77734da2
KB
591 cmd->pid = mingw_spawnvpe(cmd->argv[0], cmd->argv, (char**) cmd->env,
592 cmd->dir, fhin, fhout, fherr);
0ac77ec3 593 failed_errno = errno;
c024beb5 594 if (cmd->pid < 0 && (!cmd->silent_exec_failure || errno != ENOENT))
fbcb0e06 595 error_errno("cannot spawn %s", cmd->argv[0]);
afe19ff7 596 if (cmd->clean_on_exit && cmd->pid >= 0)
ac2fbaa6 597 mark_child_for_cleanup(cmd->pid, cmd);
ba26f296 598
20574f55 599 argv_array_clear(&nargv);
108ac313 600 cmd->argv = sargv;
75301f90
JS
601 if (fhin != 0)
602 close(fhin);
603 if (fhout != 1)
604 close(fhout);
605 if (fherr != 2)
606 close(fherr);
0d30ad71 607}
ba26f296
JS
608#endif
609
610 if (cmd->pid < 0) {
611 if (need_in)
612 close_pair(fdin);
613 else if (cmd->in)
614 close(cmd->in);
615 if (need_out)
616 close_pair(fdout);
617 else if (cmd->out)
618 close(cmd->out);
619 if (need_err)
620 close_pair(fderr);
fc012c28
D
621 else if (cmd->err)
622 close(cmd->err);
2d71608e 623 child_process_clear(cmd);
0ac77ec3
JS
624 errno = failed_errno;
625 return -1;
ba26f296 626 }
4919bf03
SP
627
628 if (need_in)
629 close(fdin[0]);
630 else if (cmd->in)
631 close(cmd->in);
632
f4bba25b
SP
633 if (need_out)
634 close(fdout[1]);
c20181e3 635 else if (cmd->out)
f4bba25b
SP
636 close(cmd->out);
637
f3b33f1d
JS
638 if (need_err)
639 close(fderr[1]);
4f41b611
SP
640 else if (cmd->err)
641 close(cmd->err);
f3b33f1d 642
ebcb5d16
SP
643 return 0;
644}
645
2d22c208
JS
646int finish_command(struct child_process *cmd)
647{
507d7804 648 int ret = wait_or_whine(cmd->pid, cmd->argv[0], 0);
2d71608e 649 child_process_clear(cmd);
c460c0ec 650 return ret;
2d22c208
JS
651}
652
507d7804
TI
653int finish_command_in_signal(struct child_process *cmd)
654{
655 return wait_or_whine(cmd->pid, cmd->argv[0], 1);
656}
657
658
ebcb5d16
SP
659int run_command(struct child_process *cmd)
660{
c29b3962
JK
661 int code;
662
663 if (cmd->out < 0 || cmd->err < 0)
664 die("BUG: run_command with a pipe can cause deadlock");
665
666 code = start_command(cmd);
ebcb5d16
SP
667 if (code)
668 return code;
669 return finish_command(cmd);
670}
671
f1000898
SP
672int run_command_v_opt(const char **argv, int opt)
673{
41e9bad7 674 return run_command_v_opt_cd_env(argv, opt, NULL, NULL);
1568fea0
AR
675}
676
ee493148
AR
677int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env)
678{
1f87293d
RS
679 struct child_process cmd = CHILD_PROCESS_INIT;
680 cmd.argv = argv;
681 cmd.no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
682 cmd.git_cmd = opt & RUN_GIT_CMD ? 1 : 0;
683 cmd.stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
684 cmd.silent_exec_failure = opt & RUN_SILENT_EXEC_FAILURE ? 1 : 0;
685 cmd.use_shell = opt & RUN_USING_SHELL ? 1 : 0;
686 cmd.clean_on_exit = opt & RUN_CLEAN_ON_EXIT ? 1 : 0;
ee493148
AR
687 cmd.dir = dir;
688 cmd.env = env;
689 return run_command(&cmd);
690}
2d22c208 691
f6b60983 692#ifndef NO_PTHREADS
0ea1c89b
JS
693static pthread_t main_thread;
694static int main_thread_set;
695static pthread_key_t async_key;
1ece66bc 696static pthread_key_t async_die_counter;
0ea1c89b 697
200a76b7 698static void *run_thread(void *data)
618ebe9f
JS
699{
700 struct async *async = data;
f6b60983 701 intptr_t ret;
0ea1c89b 702
c792d7b6
JK
703 if (async->isolate_sigpipe) {
704 sigset_t mask;
705 sigemptyset(&mask);
706 sigaddset(&mask, SIGPIPE);
707 if (pthread_sigmask(SIG_BLOCK, &mask, NULL) < 0) {
708 ret = error("unable to block SIGPIPE in async thread");
709 return (void *)ret;
710 }
711 }
712
0ea1c89b 713 pthread_setspecific(async_key, async);
f6b60983 714 ret = async->proc(async->proc_in, async->proc_out, async->data);
200a76b7 715 return (void *)ret;
618ebe9f 716}
0ea1c89b
JS
717
718static NORETURN void die_async(const char *err, va_list params)
719{
720 vreportf("fatal: ", err, params);
721
661a8cf4 722 if (in_async()) {
0ea1c89b
JS
723 struct async *async = pthread_getspecific(async_key);
724 if (async->proc_in >= 0)
725 close(async->proc_in);
726 if (async->proc_out >= 0)
727 close(async->proc_out);
728 pthread_exit((void *)128);
729 }
730
731 exit(128);
618ebe9f 732}
1ece66bc
JK
733
734static int async_die_is_recursing(void)
735{
736 void *ret = pthread_getspecific(async_die_counter);
737 pthread_setspecific(async_die_counter, (void *)1);
738 return ret != NULL;
739}
740
661a8cf4
JK
741int in_async(void)
742{
743 if (!main_thread_set)
744 return 0; /* no asyncs started yet */
745 return !pthread_equal(main_thread, pthread_self());
746}
747
b992fe10 748static void NORETURN async_exit(int code)
9658846c
JK
749{
750 pthread_exit((void *)(intptr_t)code);
751}
752
0f4b6db3
EB
753#else
754
755static struct {
756 void (**handlers)(void);
757 size_t nr;
758 size_t alloc;
759} git_atexit_hdlrs;
760
761static int git_atexit_installed;
762
6066a7ea 763static void git_atexit_dispatch(void)
0f4b6db3
EB
764{
765 size_t i;
766
767 for (i=git_atexit_hdlrs.nr ; i ; i--)
768 git_atexit_hdlrs.handlers[i-1]();
769}
770
6066a7ea 771static void git_atexit_clear(void)
0f4b6db3
EB
772{
773 free(git_atexit_hdlrs.handlers);
774 memset(&git_atexit_hdlrs, 0, sizeof(git_atexit_hdlrs));
775 git_atexit_installed = 0;
776}
777
778#undef atexit
779int git_atexit(void (*handler)(void))
780{
781 ALLOC_GROW(git_atexit_hdlrs.handlers, git_atexit_hdlrs.nr + 1, git_atexit_hdlrs.alloc);
782 git_atexit_hdlrs.handlers[git_atexit_hdlrs.nr++] = handler;
783 if (!git_atexit_installed) {
784 if (atexit(&git_atexit_dispatch))
785 return -1;
786 git_atexit_installed = 1;
787 }
788 return 0;
789}
790#define atexit git_atexit
791
661a8cf4
JK
792static int process_is_async;
793int in_async(void)
794{
795 return process_is_async;
796}
797
b992fe10 798static void NORETURN async_exit(int code)
9658846c
JK
799{
800 exit(code);
801}
802
618ebe9f
JS
803#endif
804
b992fe10
LS
805void check_pipe(int err)
806{
807 if (err == EPIPE) {
808 if (in_async())
809 async_exit(141);
810
811 signal(SIGPIPE, SIG_DFL);
812 raise(SIGPIPE);
813 /* Should never happen, but just in case... */
814 exit(141);
815 }
816}
817
2d22c208
JS
818int start_async(struct async *async)
819{
ae6a5609
EFL
820 int need_in, need_out;
821 int fdin[2], fdout[2];
822 int proc_in, proc_out;
2d22c208 823
ae6a5609
EFL
824 need_in = async->in < 0;
825 if (need_in) {
826 if (pipe(fdin) < 0) {
827 if (async->out > 0)
828 close(async->out);
fbcb0e06 829 return error_errno("cannot create pipe");
ae6a5609
EFL
830 }
831 async->in = fdin[1];
832 }
833
834 need_out = async->out < 0;
835 if (need_out) {
836 if (pipe(fdout) < 0) {
837 if (need_in)
838 close_pair(fdin);
839 else if (async->in)
840 close(async->in);
fbcb0e06 841 return error_errno("cannot create pipe");
ae6a5609
EFL
842 }
843 async->out = fdout[0];
844 }
845
846 if (need_in)
847 proc_in = fdin[0];
848 else if (async->in)
849 proc_in = async->in;
850 else
851 proc_in = -1;
852
853 if (need_out)
854 proc_out = fdout[1];
855 else if (async->out)
856 proc_out = async->out;
857 else
858 proc_out = -1;
2d22c208 859
f6b60983 860#ifdef NO_PTHREADS
2c3766f0
AM
861 /* Flush stdio before fork() to avoid cloning buffers */
862 fflush(NULL);
863
2d22c208
JS
864 async->pid = fork();
865 if (async->pid < 0) {
fbcb0e06 866 error_errno("fork (async) failed");
ae6a5609 867 goto error;
2d22c208
JS
868 }
869 if (!async->pid) {
ae6a5609
EFL
870 if (need_in)
871 close(fdin[1]);
872 if (need_out)
873 close(fdout[0]);
0f4b6db3 874 git_atexit_clear();
661a8cf4 875 process_is_async = 1;
ae6a5609 876 exit(!!async->proc(proc_in, proc_out, async->data));
2d22c208 877 }
ae6a5609 878
ac2fbaa6 879 mark_child_for_cleanup(async->pid, NULL);
afe19ff7 880
ae6a5609
EFL
881 if (need_in)
882 close(fdin[0]);
883 else if (async->in)
884 close(async->in);
885
886 if (need_out)
887 close(fdout[1]);
888 else if (async->out)
889 close(async->out);
618ebe9f 890#else
0ea1c89b
JS
891 if (!main_thread_set) {
892 /*
893 * We assume that the first time that start_async is called
894 * it is from the main thread.
895 */
896 main_thread_set = 1;
897 main_thread = pthread_self();
898 pthread_key_create(&async_key, NULL);
1ece66bc 899 pthread_key_create(&async_die_counter, NULL);
0ea1c89b 900 set_die_routine(die_async);
1ece66bc 901 set_die_is_recursing_routine(async_die_is_recursing);
0ea1c89b
JS
902 }
903
200a76b7
JS
904 if (proc_in >= 0)
905 set_cloexec(proc_in);
906 if (proc_out >= 0)
907 set_cloexec(proc_out);
ae6a5609
EFL
908 async->proc_in = proc_in;
909 async->proc_out = proc_out;
200a76b7
JS
910 {
911 int err = pthread_create(&async->tid, NULL, run_thread, async);
912 if (err) {
fbcb0e06 913 error_errno("cannot create thread");
200a76b7
JS
914 goto error;
915 }
618ebe9f
JS
916 }
917#endif
2d22c208 918 return 0;
ae6a5609
EFL
919
920error:
921 if (need_in)
922 close_pair(fdin);
923 else if (async->in)
924 close(async->in);
925
926 if (need_out)
927 close_pair(fdout);
928 else if (async->out)
929 close(async->out);
930 return -1;
2d22c208
JS
931}
932
933int finish_async(struct async *async)
934{
f6b60983 935#ifdef NO_PTHREADS
507d7804 936 return wait_or_whine(async->pid, "child process", 0);
618ebe9f 937#else
200a76b7
JS
938 void *ret = (void *)(intptr_t)(-1);
939
940 if (pthread_join(async->tid, &ret))
941 error("pthread_join failed");
942 return (int)(intptr_t)ret;
618ebe9f 943#endif
2d22c208 944}
ae98a008 945
dcf69262 946const char *find_hook(const char *name)
5a7da2dc 947{
03f2c773 948 static struct strbuf path = STRBUF_INIT;
5a7da2dc 949
03f2c773 950 strbuf_reset(&path);
9445b492 951 strbuf_git_path(&path, "hooks/%s", name);
235be51f
JS
952 if (access(path.buf, X_OK) < 0) {
953#ifdef STRIP_EXTENSION
954 strbuf_addstr(&path, STRIP_EXTENSION);
955 if (access(path.buf, X_OK) >= 0)
956 return path.buf;
957#endif
03f2c773 958 return NULL;
235be51f 959 }
03f2c773 960 return path.buf;
5a7da2dc
AS
961}
962
15048f8a 963int run_hook_ve(const char *const *env, const char *name, va_list args)
ae98a008 964{
d3180279 965 struct child_process hook = CHILD_PROCESS_INIT;
15048f8a 966 const char *p;
ae98a008 967
5a7da2dc
AS
968 p = find_hook(name);
969 if (!p)
cf94ca8e
SB
970 return 0;
971
d1d09456
RS
972 argv_array_push(&hook.args, p);
973 while ((p = va_arg(args, const char *)))
974 argv_array_push(&hook.args, p);
15048f8a 975 hook.env = env;
ae98a008
SB
976 hook.no_stdin = 1;
977 hook.stdout_to_stderr = 1;
ae98a008 978
d1d09456 979 return run_command(&hook);
ae98a008 980}
15048f8a
BP
981
982int run_hook_le(const char *const *env, const char *name, ...)
983{
984 va_list args;
985 int ret;
986
987 va_start(args, name);
988 ret = run_hook_ve(env, name, args);
989 va_end(args);
990
991 return ret;
992}
911ec99b 993
96335bcf
JK
994struct io_pump {
995 /* initialized by caller */
996 int fd;
997 int type; /* POLLOUT or POLLIN */
998 union {
999 struct {
1000 const char *buf;
1001 size_t len;
1002 } out;
1003 struct {
1004 struct strbuf *buf;
1005 size_t hint;
1006 } in;
1007 } u;
1008
1009 /* returned by pump_io */
1010 int error; /* 0 for success, otherwise errno */
1011
1012 /* internal use */
1013 struct pollfd *pfd;
1014};
1015
1016static int pump_io_round(struct io_pump *slots, int nr, struct pollfd *pfd)
1017{
1018 int pollsize = 0;
1019 int i;
1020
1021 for (i = 0; i < nr; i++) {
1022 struct io_pump *io = &slots[i];
1023 if (io->fd < 0)
1024 continue;
1025 pfd[pollsize].fd = io->fd;
1026 pfd[pollsize].events = io->type;
1027 io->pfd = &pfd[pollsize++];
1028 }
1029
1030 if (!pollsize)
1031 return 0;
1032
1033 if (poll(pfd, pollsize, -1) < 0) {
1034 if (errno == EINTR)
1035 return 1;
1036 die_errno("poll failed");
1037 }
1038
1039 for (i = 0; i < nr; i++) {
1040 struct io_pump *io = &slots[i];
1041
1042 if (io->fd < 0)
1043 continue;
1044
1045 if (!(io->pfd->revents & (POLLOUT|POLLIN|POLLHUP|POLLERR|POLLNVAL)))
1046 continue;
1047
1048 if (io->type == POLLOUT) {
1049 ssize_t len = xwrite(io->fd,
1050 io->u.out.buf, io->u.out.len);
1051 if (len < 0) {
1052 io->error = errno;
1053 close(io->fd);
1054 io->fd = -1;
1055 } else {
1056 io->u.out.buf += len;
1057 io->u.out.len -= len;
1058 if (!io->u.out.len) {
1059 close(io->fd);
1060 io->fd = -1;
1061 }
1062 }
1063 }
1064
1065 if (io->type == POLLIN) {
1066 ssize_t len = strbuf_read_once(io->u.in.buf,
1067 io->fd, io->u.in.hint);
1068 if (len < 0)
1069 io->error = errno;
1070 if (len <= 0) {
1071 close(io->fd);
1072 io->fd = -1;
1073 }
1074 }
1075 }
1076
1077 return 1;
1078}
1079
1080static int pump_io(struct io_pump *slots, int nr)
911ec99b 1081{
96335bcf
JK
1082 struct pollfd *pfd;
1083 int i;
1084
1085 for (i = 0; i < nr; i++)
1086 slots[i].error = 0;
1087
1088 ALLOC_ARRAY(pfd, nr);
1089 while (pump_io_round(slots, nr, pfd))
1090 ; /* nothing */
1091 free(pfd);
1092
1093 /* There may be multiple errno values, so just pick the first. */
1094 for (i = 0; i < nr; i++) {
1095 if (slots[i].error) {
1096 errno = slots[i].error;
1097 return -1;
1098 }
1099 }
1100 return 0;
1101}
1102
1103
1104int pipe_command(struct child_process *cmd,
1105 const char *in, size_t in_len,
1106 struct strbuf *out, size_t out_hint,
1107 struct strbuf *err, size_t err_hint)
1108{
1109 struct io_pump io[3];
1110 int nr = 0;
1111
1112 if (in)
1113 cmd->in = -1;
1114 if (out)
1115 cmd->out = -1;
1116 if (err)
1117 cmd->err = -1;
1118
911ec99b
JK
1119 if (start_command(cmd) < 0)
1120 return -1;
1121
96335bcf
JK
1122 if (in) {
1123 io[nr].fd = cmd->in;
1124 io[nr].type = POLLOUT;
1125 io[nr].u.out.buf = in;
1126 io[nr].u.out.len = in_len;
1127 nr++;
1128 }
1129 if (out) {
1130 io[nr].fd = cmd->out;
1131 io[nr].type = POLLIN;
1132 io[nr].u.in.buf = out;
1133 io[nr].u.in.hint = out_hint;
1134 nr++;
1135 }
1136 if (err) {
1137 io[nr].fd = cmd->err;
1138 io[nr].type = POLLIN;
1139 io[nr].u.in.buf = err;
1140 io[nr].u.in.hint = err_hint;
1141 nr++;
1142 }
1143
1144 if (pump_io(io, nr) < 0) {
911ec99b
JK
1145 finish_command(cmd); /* throw away exit code */
1146 return -1;
1147 }
1148
911ec99b
JK
1149 return finish_command(cmd);
1150}
c553c72e
SB
1151
1152enum child_state {
1153 GIT_CP_FREE,
1154 GIT_CP_WORKING,
1155 GIT_CP_WAIT_CLEANUP,
1156};
1157
1158struct parallel_processes {
1159 void *data;
1160
1161 int max_processes;
1162 int nr_processes;
1163
1164 get_next_task_fn get_next_task;
1165 start_failure_fn start_failure;
1166 task_finished_fn task_finished;
1167
1168 struct {
1169 enum child_state state;
1170 struct child_process process;
1171 struct strbuf err;
1172 void *data;
1173 } *children;
1174 /*
1175 * The struct pollfd is logically part of *children,
1176 * but the system call expects it as its own array.
1177 */
1178 struct pollfd *pfd;
1179
1180 unsigned shutdown : 1;
1181
1182 int output_owner;
1183 struct strbuf buffered_output; /* of finished children */
1184};
1185
aa710494 1186static int default_start_failure(struct strbuf *out,
c553c72e
SB
1187 void *pp_cb,
1188 void *pp_task_cb)
1189{
c553c72e
SB
1190 return 0;
1191}
1192
1193static int default_task_finished(int result,
aa710494 1194 struct strbuf *out,
c553c72e
SB
1195 void *pp_cb,
1196 void *pp_task_cb)
1197{
c553c72e
SB
1198 return 0;
1199}
1200
1201static void kill_children(struct parallel_processes *pp, int signo)
1202{
1203 int i, n = pp->max_processes;
1204
1205 for (i = 0; i < n; i++)
1206 if (pp->children[i].state == GIT_CP_WORKING)
1207 kill(pp->children[i].process.pid, signo);
1208}
1209
1210static struct parallel_processes *pp_for_signal;
1211
1212static void handle_children_on_signal(int signo)
1213{
1214 kill_children(pp_for_signal, signo);
1215 sigchain_pop(signo);
1216 raise(signo);
1217}
1218
1219static void pp_init(struct parallel_processes *pp,
1220 int n,
1221 get_next_task_fn get_next_task,
1222 start_failure_fn start_failure,
1223 task_finished_fn task_finished,
1224 void *data)
1225{
1226 int i;
1227
1228 if (n < 1)
1229 n = online_cpus();
1230
1231 pp->max_processes = n;
1232
1233 trace_printf("run_processes_parallel: preparing to run up to %d tasks", n);
1234
1235 pp->data = data;
1236 if (!get_next_task)
1237 die("BUG: you need to specify a get_next_task function");
1238 pp->get_next_task = get_next_task;
1239
1240 pp->start_failure = start_failure ? start_failure : default_start_failure;
1241 pp->task_finished = task_finished ? task_finished : default_task_finished;
1242
1243 pp->nr_processes = 0;
1244 pp->output_owner = 0;
1245 pp->shutdown = 0;
1246 pp->children = xcalloc(n, sizeof(*pp->children));
1247 pp->pfd = xcalloc(n, sizeof(*pp->pfd));
1248 strbuf_init(&pp->buffered_output, 0);
1249
1250 for (i = 0; i < n; i++) {
1251 strbuf_init(&pp->children[i].err, 0);
1252 child_process_init(&pp->children[i].process);
1253 pp->pfd[i].events = POLLIN | POLLHUP;
1254 pp->pfd[i].fd = -1;
1255 }
1256
1257 pp_for_signal = pp;
1258 sigchain_push_common(handle_children_on_signal);
1259}
1260
1261static void pp_cleanup(struct parallel_processes *pp)
1262{
1263 int i;
1264
1265 trace_printf("run_processes_parallel: done");
1266 for (i = 0; i < pp->max_processes; i++) {
1267 strbuf_release(&pp->children[i].err);
1268 child_process_clear(&pp->children[i].process);
1269 }
1270
1271 free(pp->children);
1272 free(pp->pfd);
1273
1274 /*
1275 * When get_next_task added messages to the buffer in its last
1276 * iteration, the buffered output is non empty.
1277 */
2dac9b56 1278 strbuf_write(&pp->buffered_output, stderr);
c553c72e
SB
1279 strbuf_release(&pp->buffered_output);
1280
1281 sigchain_pop_common();
1282}
1283
1284/* returns
1285 * 0 if a new task was started.
1286 * 1 if no new jobs was started (get_next_task ran out of work, non critical
1287 * problem with starting a new command)
1288 * <0 no new job was started, user wishes to shutdown early. Use negative code
1289 * to signal the children.
1290 */
1291static int pp_start_one(struct parallel_processes *pp)
1292{
1293 int i, code;
1294
1295 for (i = 0; i < pp->max_processes; i++)
1296 if (pp->children[i].state == GIT_CP_FREE)
1297 break;
1298 if (i == pp->max_processes)
1299 die("BUG: bookkeeping is hard");
1300
1301 code = pp->get_next_task(&pp->children[i].process,
1302 &pp->children[i].err,
1303 pp->data,
1304 &pp->children[i].data);
1305 if (!code) {
1306 strbuf_addbuf(&pp->buffered_output, &pp->children[i].err);
1307 strbuf_reset(&pp->children[i].err);
1308 return 1;
1309 }
1310 pp->children[i].process.err = -1;
1311 pp->children[i].process.stdout_to_stderr = 1;
1312 pp->children[i].process.no_stdin = 1;
1313
1314 if (start_command(&pp->children[i].process)) {
2a73b3da 1315 code = pp->start_failure(&pp->children[i].err,
c553c72e
SB
1316 pp->data,
1317 &pp->children[i].data);
1318 strbuf_addbuf(&pp->buffered_output, &pp->children[i].err);
1319 strbuf_reset(&pp->children[i].err);
1320 if (code)
1321 pp->shutdown = 1;
1322 return code;
1323 }
1324
1325 pp->nr_processes++;
1326 pp->children[i].state = GIT_CP_WORKING;
1327 pp->pfd[i].fd = pp->children[i].process.err;
1328 return 0;
1329}
1330
1331static void pp_buffer_stderr(struct parallel_processes *pp, int output_timeout)
1332{
1333 int i;
1334
1335 while ((i = poll(pp->pfd, pp->max_processes, output_timeout)) < 0) {
1336 if (errno == EINTR)
1337 continue;
1338 pp_cleanup(pp);
1339 die_errno("poll");
1340 }
1341
1342 /* Buffer output from all pipes. */
1343 for (i = 0; i < pp->max_processes; i++) {
1344 if (pp->children[i].state == GIT_CP_WORKING &&
1345 pp->pfd[i].revents & (POLLIN | POLLHUP)) {
1346 int n = strbuf_read_once(&pp->children[i].err,
1347 pp->children[i].process.err, 0);
1348 if (n == 0) {
1349 close(pp->children[i].process.err);
1350 pp->children[i].state = GIT_CP_WAIT_CLEANUP;
1351 } else if (n < 0)
1352 if (errno != EAGAIN)
1353 die_errno("read");
1354 }
1355 }
1356}
1357
1358static void pp_output(struct parallel_processes *pp)
1359{
1360 int i = pp->output_owner;
1361 if (pp->children[i].state == GIT_CP_WORKING &&
1362 pp->children[i].err.len) {
2dac9b56 1363 strbuf_write(&pp->children[i].err, stderr);
c553c72e
SB
1364 strbuf_reset(&pp->children[i].err);
1365 }
1366}
1367
1368static int pp_collect_finished(struct parallel_processes *pp)
1369{
1370 int i, code;
1371 int n = pp->max_processes;
1372 int result = 0;
1373
1374 while (pp->nr_processes > 0) {
1375 for (i = 0; i < pp->max_processes; i++)
1376 if (pp->children[i].state == GIT_CP_WAIT_CLEANUP)
1377 break;
1378 if (i == pp->max_processes)
1379 break;
1380
1381 code = finish_command(&pp->children[i].process);
1382
2a73b3da 1383 code = pp->task_finished(code,
c553c72e
SB
1384 &pp->children[i].err, pp->data,
1385 &pp->children[i].data);
1386
1387 if (code)
1388 result = code;
1389 if (code < 0)
1390 break;
1391
1392 pp->nr_processes--;
1393 pp->children[i].state = GIT_CP_FREE;
1394 pp->pfd[i].fd = -1;
1395 child_process_init(&pp->children[i].process);
1396
1397 if (i != pp->output_owner) {
1398 strbuf_addbuf(&pp->buffered_output, &pp->children[i].err);
1399 strbuf_reset(&pp->children[i].err);
1400 } else {
2dac9b56 1401 strbuf_write(&pp->children[i].err, stderr);
c553c72e
SB
1402 strbuf_reset(&pp->children[i].err);
1403
1404 /* Output all other finished child processes */
2dac9b56 1405 strbuf_write(&pp->buffered_output, stderr);
c553c72e
SB
1406 strbuf_reset(&pp->buffered_output);
1407
1408 /*
1409 * Pick next process to output live.
1410 * NEEDSWORK:
1411 * For now we pick it randomly by doing a round
1412 * robin. Later we may want to pick the one with
1413 * the most output or the longest or shortest
1414 * running process time.
1415 */
1416 for (i = 0; i < n; i++)
1417 if (pp->children[(pp->output_owner + i) % n].state == GIT_CP_WORKING)
1418 break;
1419 pp->output_owner = (pp->output_owner + i) % n;
1420 }
1421 }
1422 return result;
1423}
1424
1425int run_processes_parallel(int n,
1426 get_next_task_fn get_next_task,
1427 start_failure_fn start_failure,
1428 task_finished_fn task_finished,
1429 void *pp_cb)
1430{
1431 int i, code;
1432 int output_timeout = 100;
1433 int spawn_cap = 4;
1434 struct parallel_processes pp;
1435
1436 pp_init(&pp, n, get_next_task, start_failure, task_finished, pp_cb);
1437 while (1) {
1438 for (i = 0;
1439 i < spawn_cap && !pp.shutdown &&
1440 pp.nr_processes < pp.max_processes;
1441 i++) {
1442 code = pp_start_one(&pp);
1443 if (!code)
1444 continue;
1445 if (code < 0) {
1446 pp.shutdown = 1;
1447 kill_children(&pp, -code);
1448 }
1449 break;
1450 }
1451 if (!pp.nr_processes)
1452 break;
1453 pp_buffer_stderr(&pp, output_timeout);
1454 pp_output(&pp);
1455 code = pp_collect_finished(&pp);
1456 if (code) {
1457 pp.shutdown = 1;
1458 if (code < 0)
1459 kill_children(&pp, -code);
1460 }
1461 }
1462
1463 pp_cleanup(&pp);
1464 return 0;
1465}