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