run-command: expose is_executable function
[git] / run-command.c
1 #include "cache.h"
2 #include "run-command.h"
3 #include "exec_cmd.h"
4 #include "sigchain.h"
5 #include "argv-array.h"
6 #include "thread-utils.h"
7 #include "strbuf.h"
8
9 void child_process_init(struct child_process *child)
10 {
11         memset(child, 0, sizeof(*child));
12         argv_array_init(&child->args);
13         argv_array_init(&child->env_array);
14 }
15
16 void child_process_clear(struct child_process *child)
17 {
18         argv_array_clear(&child->args);
19         argv_array_clear(&child->env_array);
20 }
21
22 struct child_to_clean {
23         pid_t pid;
24         struct child_process *process;
25         struct child_to_clean *next;
26 };
27 static struct child_to_clean *children_to_clean;
28 static int installed_child_cleanup_handler;
29
30 static void cleanup_children(int sig, int in_signal)
31 {
32         struct child_to_clean *children_to_wait_for = NULL;
33
34         while (children_to_clean) {
35                 struct child_to_clean *p = children_to_clean;
36                 children_to_clean = p->next;
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
49                 kill(p->pid, sig);
50
51                 if (p->process && p->process->wait_after_clean) {
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
67                 if (!in_signal)
68                         free(p);
69         }
70 }
71
72 static void cleanup_children_on_signal(int sig)
73 {
74         cleanup_children(sig, 1);
75         sigchain_pop(sig);
76         raise(sig);
77 }
78
79 static void cleanup_children_on_exit(void)
80 {
81         cleanup_children(SIGTERM, 0);
82 }
83
84 static void mark_child_for_cleanup(pid_t pid, struct child_process *process)
85 {
86         struct child_to_clean *p = xmalloc(sizeof(*p));
87         p->pid = pid;
88         p->process = process;
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
99 static void clear_child_for_cleanup(pid_t pid)
100 {
101         struct child_to_clean **pp;
102
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);
109                         return;
110                 }
111         }
112 }
113
114 static inline void close_pair(int fd[2])
115 {
116         close(fd[0]);
117         close(fd[1]);
118 }
119
120 int is_executable(const char *name)
121 {
122         struct stat st;
123
124         if (stat(name, &st) || /* stat, not lstat */
125             !S_ISREG(st.st_mode))
126                 return 0;
127
128 #if defined(GIT_WINDOWS_NATIVE)
129         /*
130          * On Windows there is no executable bit. The file extension
131          * indicates whether it can be run as an executable, and Git
132          * has special-handling to detect scripts and launch them
133          * through the indicated script interpreter. We test for the
134          * file extension first because virus scanners may make
135          * it quite expensive to open many files.
136          */
137         if (ends_with(name, ".exe"))
138                 return S_IXUSR;
139
140 {
141         /*
142          * Now that we know it does not have an executable extension,
143          * peek into the file instead.
144          */
145         char buf[3] = { 0 };
146         int n;
147         int fd = open(name, O_RDONLY);
148         st.st_mode &= ~S_IXUSR;
149         if (fd >= 0) {
150                 n = read(fd, buf, 2);
151                 if (n == 2)
152                         /* look for a she-bang */
153                         if (!strcmp(buf, "#!"))
154                                 st.st_mode |= S_IXUSR;
155                 close(fd);
156         }
157 }
158 #endif
159         return st.st_mode & S_IXUSR;
160 }
161
162 static char *locate_in_PATH(const char *file)
163 {
164         const char *p = getenv("PATH");
165         struct strbuf buf = STRBUF_INIT;
166
167         if (!p || !*p)
168                 return NULL;
169
170         while (1) {
171                 const char *end = strchrnul(p, ':');
172
173                 strbuf_reset(&buf);
174
175                 /* POSIX specifies an empty entry as the current directory. */
176                 if (end != p) {
177                         strbuf_add(&buf, p, end - p);
178                         strbuf_addch(&buf, '/');
179                 }
180                 strbuf_addstr(&buf, file);
181
182                 if (!access(buf.buf, F_OK))
183                         return strbuf_detach(&buf, NULL);
184
185                 if (!*end)
186                         break;
187                 p = end + 1;
188         }
189
190         strbuf_release(&buf);
191         return NULL;
192 }
193
194 static int exists_in_PATH(const char *file)
195 {
196         char *r = locate_in_PATH(file);
197         free(r);
198         return r != NULL;
199 }
200
201 int sane_execvp(const char *file, char * const argv[])
202 {
203         if (!execvp(file, argv))
204                 return 0; /* cannot happen ;-) */
205
206         /*
207          * When a command can't be found because one of the directories
208          * listed in $PATH is unsearchable, execvp reports EACCES, but
209          * careful usability testing (read: analysis of occasional bug
210          * reports) reveals that "No such file or directory" is more
211          * intuitive.
212          *
213          * We avoid commands with "/", because execvp will not do $PATH
214          * lookups in that case.
215          *
216          * The reassignment of EACCES to errno looks like a no-op below,
217          * but we need to protect against exists_in_PATH overwriting errno.
218          */
219         if (errno == EACCES && !strchr(file, '/'))
220                 errno = exists_in_PATH(file) ? EACCES : ENOENT;
221         else if (errno == ENOTDIR && !strchr(file, '/'))
222                 errno = ENOENT;
223         return -1;
224 }
225
226 static const char **prepare_shell_cmd(struct argv_array *out, const char **argv)
227 {
228         if (!argv[0])
229                 die("BUG: shell command is empty");
230
231         if (strcspn(argv[0], "|&;<>()$`\\\"' \t\n*?[#~=%") != strlen(argv[0])) {
232 #ifndef GIT_WINDOWS_NATIVE
233                 argv_array_push(out, SHELL_PATH);
234 #else
235                 argv_array_push(out, "sh");
236 #endif
237                 argv_array_push(out, "-c");
238
239                 /*
240                  * If we have no extra arguments, we do not even need to
241                  * bother with the "$@" magic.
242                  */
243                 if (!argv[1])
244                         argv_array_push(out, argv[0]);
245                 else
246                         argv_array_pushf(out, "%s \"$@\"", argv[0]);
247         }
248
249         argv_array_pushv(out, argv);
250         return out->argv;
251 }
252
253 #ifndef GIT_WINDOWS_NATIVE
254 static int child_notifier = -1;
255
256 enum child_errcode {
257         CHILD_ERR_CHDIR,
258         CHILD_ERR_DUP2,
259         CHILD_ERR_CLOSE,
260         CHILD_ERR_SIGPROCMASK,
261         CHILD_ERR_ENOENT,
262         CHILD_ERR_SILENT,
263         CHILD_ERR_ERRNO
264 };
265
266 struct child_err {
267         enum child_errcode err;
268         int syserr; /* errno */
269 };
270
271 static void child_die(enum child_errcode err)
272 {
273         struct child_err buf;
274
275         buf.err = err;
276         buf.syserr = errno;
277
278         /* write(2) on buf smaller than PIPE_BUF (min 512) is atomic: */
279         xwrite(child_notifier, &buf, sizeof(buf));
280         _exit(1);
281 }
282
283 static void child_dup2(int fd, int to)
284 {
285         if (dup2(fd, to) < 0)
286                 child_die(CHILD_ERR_DUP2);
287 }
288
289 static void child_close(int fd)
290 {
291         if (close(fd))
292                 child_die(CHILD_ERR_CLOSE);
293 }
294
295 static void child_close_pair(int fd[2])
296 {
297         child_close(fd[0]);
298         child_close(fd[1]);
299 }
300
301 /*
302  * parent will make it look like the child spewed a fatal error and died
303  * this is needed to prevent changes to t0061.
304  */
305 static void fake_fatal(const char *err, va_list params)
306 {
307         vreportf("fatal: ", err, params);
308 }
309
310 static void child_error_fn(const char *err, va_list params)
311 {
312         const char msg[] = "error() should not be called in child\n";
313         xwrite(2, msg, sizeof(msg) - 1);
314 }
315
316 static void child_warn_fn(const char *err, va_list params)
317 {
318         const char msg[] = "warn() should not be called in child\n";
319         xwrite(2, msg, sizeof(msg) - 1);
320 }
321
322 static void NORETURN child_die_fn(const char *err, va_list params)
323 {
324         const char msg[] = "die() should not be called in child\n";
325         xwrite(2, msg, sizeof(msg) - 1);
326         _exit(2);
327 }
328
329 /* this runs in the parent process */
330 static void child_err_spew(struct child_process *cmd, struct child_err *cerr)
331 {
332         static void (*old_errfn)(const char *err, va_list params);
333
334         old_errfn = get_error_routine();
335         set_error_routine(fake_fatal);
336         errno = cerr->syserr;
337
338         switch (cerr->err) {
339         case CHILD_ERR_CHDIR:
340                 error_errno("exec '%s': cd to '%s' failed",
341                             cmd->argv[0], cmd->dir);
342                 break;
343         case CHILD_ERR_DUP2:
344                 error_errno("dup2() in child failed");
345                 break;
346         case CHILD_ERR_CLOSE:
347                 error_errno("close() in child failed");
348                 break;
349         case CHILD_ERR_SIGPROCMASK:
350                 error_errno("sigprocmask failed restoring signals");
351                 break;
352         case CHILD_ERR_ENOENT:
353                 error_errno("cannot run %s", cmd->argv[0]);
354                 break;
355         case CHILD_ERR_SILENT:
356                 break;
357         case CHILD_ERR_ERRNO:
358                 error_errno("cannot exec '%s'", cmd->argv[0]);
359                 break;
360         }
361         set_error_routine(old_errfn);
362 }
363
364 static void prepare_cmd(struct argv_array *out, const struct child_process *cmd)
365 {
366         if (!cmd->argv[0])
367                 die("BUG: command is empty");
368
369         /*
370          * Add SHELL_PATH so in the event exec fails with ENOEXEC we can
371          * attempt to interpret the command with 'sh'.
372          */
373         argv_array_push(out, SHELL_PATH);
374
375         if (cmd->git_cmd) {
376                 argv_array_push(out, "git");
377                 argv_array_pushv(out, cmd->argv);
378         } else if (cmd->use_shell) {
379                 prepare_shell_cmd(out, cmd->argv);
380         } else {
381                 argv_array_pushv(out, cmd->argv);
382         }
383
384         /*
385          * If there are no '/' characters in the command then perform a path
386          * lookup and use the resolved path as the command to exec.  If there
387          * are no '/' characters or if the command wasn't found in the path,
388          * have exec attempt to invoke the command directly.
389          */
390         if (!strchr(out->argv[1], '/')) {
391                 char *program = locate_in_PATH(out->argv[1]);
392                 if (program) {
393                         free((char *)out->argv[1]);
394                         out->argv[1] = program;
395                 }
396         }
397 }
398
399 static char **prep_childenv(const char *const *deltaenv)
400 {
401         extern char **environ;
402         char **childenv;
403         struct string_list env = STRING_LIST_INIT_DUP;
404         struct strbuf key = STRBUF_INIT;
405         const char *const *p;
406         int i;
407
408         /* Construct a sorted string list consisting of the current environ */
409         for (p = (const char *const *) environ; p && *p; p++) {
410                 const char *equals = strchr(*p, '=');
411
412                 if (equals) {
413                         strbuf_reset(&key);
414                         strbuf_add(&key, *p, equals - *p);
415                         string_list_append(&env, key.buf)->util = (void *) *p;
416                 } else {
417                         string_list_append(&env, *p)->util = (void *) *p;
418                 }
419         }
420         string_list_sort(&env);
421
422         /* Merge in 'deltaenv' with the current environ */
423         for (p = deltaenv; p && *p; p++) {
424                 const char *equals = strchr(*p, '=');
425
426                 if (equals) {
427                         /* ('key=value'), insert or replace entry */
428                         strbuf_reset(&key);
429                         strbuf_add(&key, *p, equals - *p);
430                         string_list_insert(&env, key.buf)->util = (void *) *p;
431                 } else {
432                         /* otherwise ('key') remove existing entry */
433                         string_list_remove(&env, *p, 0);
434                 }
435         }
436
437         /* Create an array of 'char *' to be used as the childenv */
438         childenv = xmalloc((env.nr + 1) * sizeof(char *));
439         for (i = 0; i < env.nr; i++)
440                 childenv[i] = env.items[i].util;
441         childenv[env.nr] = NULL;
442
443         string_list_clear(&env, 0);
444         strbuf_release(&key);
445         return childenv;
446 }
447
448 struct atfork_state {
449 #ifndef NO_PTHREADS
450         int cs;
451 #endif
452         sigset_t old;
453 };
454
455 #ifndef NO_PTHREADS
456 static void bug_die(int err, const char *msg)
457 {
458         if (err) {
459                 errno = err;
460                 die_errno("BUG: %s", msg);
461         }
462 }
463 #endif
464
465 static void atfork_prepare(struct atfork_state *as)
466 {
467         sigset_t all;
468
469         if (sigfillset(&all))
470                 die_errno("sigfillset");
471 #ifdef NO_PTHREADS
472         if (sigprocmask(SIG_SETMASK, &all, &as->old))
473                 die_errno("sigprocmask");
474 #else
475         bug_die(pthread_sigmask(SIG_SETMASK, &all, &as->old),
476                 "blocking all signals");
477         bug_die(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &as->cs),
478                 "disabling cancellation");
479 #endif
480 }
481
482 static void atfork_parent(struct atfork_state *as)
483 {
484 #ifdef NO_PTHREADS
485         if (sigprocmask(SIG_SETMASK, &as->old, NULL))
486                 die_errno("sigprocmask");
487 #else
488         bug_die(pthread_setcancelstate(as->cs, NULL),
489                 "re-enabling cancellation");
490         bug_die(pthread_sigmask(SIG_SETMASK, &as->old, NULL),
491                 "restoring signal mask");
492 #endif
493 }
494 #endif /* GIT_WINDOWS_NATIVE */
495
496 static inline void set_cloexec(int fd)
497 {
498         int flags = fcntl(fd, F_GETFD);
499         if (flags >= 0)
500                 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
501 }
502
503 static int wait_or_whine(pid_t pid, const char *argv0, int in_signal)
504 {
505         int status, code = -1;
506         pid_t waiting;
507         int failed_errno = 0;
508
509         while ((waiting = waitpid(pid, &status, 0)) < 0 && errno == EINTR)
510                 ;       /* nothing */
511         if (in_signal)
512                 return 0;
513
514         if (waiting < 0) {
515                 failed_errno = errno;
516                 error_errno("waitpid for %s failed", argv0);
517         } else if (waiting != pid) {
518                 error("waitpid is confused (%s)", argv0);
519         } else if (WIFSIGNALED(status)) {
520                 code = WTERMSIG(status);
521                 if (code != SIGINT && code != SIGQUIT && code != SIGPIPE)
522                         error("%s died of signal %d", argv0, code);
523                 /*
524                  * This return value is chosen so that code & 0xff
525                  * mimics the exit code that a POSIX shell would report for
526                  * a program that died from this signal.
527                  */
528                 code += 128;
529         } else if (WIFEXITED(status)) {
530                 code = WEXITSTATUS(status);
531         } else {
532                 error("waitpid is confused (%s)", argv0);
533         }
534
535         clear_child_for_cleanup(pid);
536
537         errno = failed_errno;
538         return code;
539 }
540
541 int start_command(struct child_process *cmd)
542 {
543         int need_in, need_out, need_err;
544         int fdin[2], fdout[2], fderr[2];
545         int failed_errno;
546         char *str;
547
548         if (!cmd->argv)
549                 cmd->argv = cmd->args.argv;
550         if (!cmd->env)
551                 cmd->env = cmd->env_array.argv;
552
553         /*
554          * In case of errors we must keep the promise to close FDs
555          * that have been passed in via ->in and ->out.
556          */
557
558         need_in = !cmd->no_stdin && cmd->in < 0;
559         if (need_in) {
560                 if (pipe(fdin) < 0) {
561                         failed_errno = errno;
562                         if (cmd->out > 0)
563                                 close(cmd->out);
564                         str = "standard input";
565                         goto fail_pipe;
566                 }
567                 cmd->in = fdin[1];
568         }
569
570         need_out = !cmd->no_stdout
571                 && !cmd->stdout_to_stderr
572                 && cmd->out < 0;
573         if (need_out) {
574                 if (pipe(fdout) < 0) {
575                         failed_errno = errno;
576                         if (need_in)
577                                 close_pair(fdin);
578                         else if (cmd->in)
579                                 close(cmd->in);
580                         str = "standard output";
581                         goto fail_pipe;
582                 }
583                 cmd->out = fdout[0];
584         }
585
586         need_err = !cmd->no_stderr && cmd->err < 0;
587         if (need_err) {
588                 if (pipe(fderr) < 0) {
589                         failed_errno = errno;
590                         if (need_in)
591                                 close_pair(fdin);
592                         else if (cmd->in)
593                                 close(cmd->in);
594                         if (need_out)
595                                 close_pair(fdout);
596                         else if (cmd->out)
597                                 close(cmd->out);
598                         str = "standard error";
599 fail_pipe:
600                         error("cannot create %s pipe for %s: %s",
601                                 str, cmd->argv[0], strerror(failed_errno));
602                         child_process_clear(cmd);
603                         errno = failed_errno;
604                         return -1;
605                 }
606                 cmd->err = fderr[0];
607         }
608
609         trace_argv_printf(cmd->argv, "trace: run_command:");
610         fflush(NULL);
611
612 #ifndef GIT_WINDOWS_NATIVE
613 {
614         int notify_pipe[2];
615         int null_fd = -1;
616         char **childenv;
617         struct argv_array argv = ARGV_ARRAY_INIT;
618         struct child_err cerr;
619         struct atfork_state as;
620
621         if (pipe(notify_pipe))
622                 notify_pipe[0] = notify_pipe[1] = -1;
623
624         if (cmd->no_stdin || cmd->no_stdout || cmd->no_stderr) {
625                 null_fd = open("/dev/null", O_RDWR | O_CLOEXEC);
626                 if (null_fd < 0)
627                         die_errno(_("open /dev/null failed"));
628                 set_cloexec(null_fd);
629         }
630
631         prepare_cmd(&argv, cmd);
632         childenv = prep_childenv(cmd->env);
633         atfork_prepare(&as);
634
635         /*
636          * NOTE: In order to prevent deadlocking when using threads special
637          * care should be taken with the function calls made in between the
638          * fork() and exec() calls.  No calls should be made to functions which
639          * require acquiring a lock (e.g. malloc) as the lock could have been
640          * held by another thread at the time of forking, causing the lock to
641          * never be released in the child process.  This means only
642          * Async-Signal-Safe functions are permitted in the child.
643          */
644         cmd->pid = fork();
645         failed_errno = errno;
646         if (!cmd->pid) {
647                 int sig;
648                 /*
649                  * Ensure the default die/error/warn routines do not get
650                  * called, they can take stdio locks and malloc.
651                  */
652                 set_die_routine(child_die_fn);
653                 set_error_routine(child_error_fn);
654                 set_warn_routine(child_warn_fn);
655
656                 close(notify_pipe[0]);
657                 set_cloexec(notify_pipe[1]);
658                 child_notifier = notify_pipe[1];
659
660                 if (cmd->no_stdin)
661                         child_dup2(null_fd, 0);
662                 else if (need_in) {
663                         child_dup2(fdin[0], 0);
664                         child_close_pair(fdin);
665                 } else if (cmd->in) {
666                         child_dup2(cmd->in, 0);
667                         child_close(cmd->in);
668                 }
669
670                 if (cmd->no_stderr)
671                         child_dup2(null_fd, 2);
672                 else if (need_err) {
673                         child_dup2(fderr[1], 2);
674                         child_close_pair(fderr);
675                 } else if (cmd->err > 1) {
676                         child_dup2(cmd->err, 2);
677                         child_close(cmd->err);
678                 }
679
680                 if (cmd->no_stdout)
681                         child_dup2(null_fd, 1);
682                 else if (cmd->stdout_to_stderr)
683                         child_dup2(2, 1);
684                 else if (need_out) {
685                         child_dup2(fdout[1], 1);
686                         child_close_pair(fdout);
687                 } else if (cmd->out > 1) {
688                         child_dup2(cmd->out, 1);
689                         child_close(cmd->out);
690                 }
691
692                 if (cmd->dir && chdir(cmd->dir))
693                         child_die(CHILD_ERR_CHDIR);
694
695                 /*
696                  * restore default signal handlers here, in case
697                  * we catch a signal right before execve below
698                  */
699                 for (sig = 1; sig < NSIG; sig++) {
700                         /* ignored signals get reset to SIG_DFL on execve */
701                         if (signal(sig, SIG_DFL) == SIG_IGN)
702                                 signal(sig, SIG_IGN);
703                 }
704
705                 if (sigprocmask(SIG_SETMASK, &as.old, NULL) != 0)
706                         child_die(CHILD_ERR_SIGPROCMASK);
707
708                 /*
709                  * Attempt to exec using the command and arguments starting at
710                  * argv.argv[1].  argv.argv[0] contains SHELL_PATH which will
711                  * be used in the event exec failed with ENOEXEC at which point
712                  * we will try to interpret the command using 'sh'.
713                  */
714                 execve(argv.argv[1], (char *const *) argv.argv + 1,
715                        (char *const *) childenv);
716                 if (errno == ENOEXEC)
717                         execve(argv.argv[0], (char *const *) argv.argv,
718                                (char *const *) childenv);
719
720                 if (errno == ENOENT) {
721                         if (cmd->silent_exec_failure)
722                                 child_die(CHILD_ERR_SILENT);
723                         child_die(CHILD_ERR_ENOENT);
724                 } else {
725                         child_die(CHILD_ERR_ERRNO);
726                 }
727         }
728         atfork_parent(&as);
729         if (cmd->pid < 0)
730                 error_errno("cannot fork() for %s", cmd->argv[0]);
731         else if (cmd->clean_on_exit)
732                 mark_child_for_cleanup(cmd->pid, cmd);
733
734         /*
735          * Wait for child's exec. If the exec succeeds (or if fork()
736          * failed), EOF is seen immediately by the parent. Otherwise, the
737          * child process sends a child_err struct.
738          * Note that use of this infrastructure is completely advisory,
739          * therefore, we keep error checks minimal.
740          */
741         close(notify_pipe[1]);
742         if (xread(notify_pipe[0], &cerr, sizeof(cerr)) == sizeof(cerr)) {
743                 /*
744                  * At this point we know that fork() succeeded, but exec()
745                  * failed. Errors have been reported to our stderr.
746                  */
747                 wait_or_whine(cmd->pid, cmd->argv[0], 0);
748                 child_err_spew(cmd, &cerr);
749                 failed_errno = errno;
750                 cmd->pid = -1;
751         }
752         close(notify_pipe[0]);
753
754         if (null_fd >= 0)
755                 close(null_fd);
756         argv_array_clear(&argv);
757         free(childenv);
758 }
759 #else
760 {
761         int fhin = 0, fhout = 1, fherr = 2;
762         const char **sargv = cmd->argv;
763         struct argv_array nargv = ARGV_ARRAY_INIT;
764
765         if (cmd->no_stdin)
766                 fhin = open("/dev/null", O_RDWR);
767         else if (need_in)
768                 fhin = dup(fdin[0]);
769         else if (cmd->in)
770                 fhin = dup(cmd->in);
771
772         if (cmd->no_stderr)
773                 fherr = open("/dev/null", O_RDWR);
774         else if (need_err)
775                 fherr = dup(fderr[1]);
776         else if (cmd->err > 2)
777                 fherr = dup(cmd->err);
778
779         if (cmd->no_stdout)
780                 fhout = open("/dev/null", O_RDWR);
781         else if (cmd->stdout_to_stderr)
782                 fhout = dup(fherr);
783         else if (need_out)
784                 fhout = dup(fdout[1]);
785         else if (cmd->out > 1)
786                 fhout = dup(cmd->out);
787
788         if (cmd->git_cmd)
789                 cmd->argv = prepare_git_cmd(&nargv, cmd->argv);
790         else if (cmd->use_shell)
791                 cmd->argv = prepare_shell_cmd(&nargv, cmd->argv);
792
793         cmd->pid = mingw_spawnvpe(cmd->argv[0], cmd->argv, (char**) cmd->env,
794                         cmd->dir, fhin, fhout, fherr);
795         failed_errno = errno;
796         if (cmd->pid < 0 && (!cmd->silent_exec_failure || errno != ENOENT))
797                 error_errno("cannot spawn %s", cmd->argv[0]);
798         if (cmd->clean_on_exit && cmd->pid >= 0)
799                 mark_child_for_cleanup(cmd->pid, cmd);
800
801         argv_array_clear(&nargv);
802         cmd->argv = sargv;
803         if (fhin != 0)
804                 close(fhin);
805         if (fhout != 1)
806                 close(fhout);
807         if (fherr != 2)
808                 close(fherr);
809 }
810 #endif
811
812         if (cmd->pid < 0) {
813                 if (need_in)
814                         close_pair(fdin);
815                 else if (cmd->in)
816                         close(cmd->in);
817                 if (need_out)
818                         close_pair(fdout);
819                 else if (cmd->out)
820                         close(cmd->out);
821                 if (need_err)
822                         close_pair(fderr);
823                 else if (cmd->err)
824                         close(cmd->err);
825                 child_process_clear(cmd);
826                 errno = failed_errno;
827                 return -1;
828         }
829
830         if (need_in)
831                 close(fdin[0]);
832         else if (cmd->in)
833                 close(cmd->in);
834
835         if (need_out)
836                 close(fdout[1]);
837         else if (cmd->out)
838                 close(cmd->out);
839
840         if (need_err)
841                 close(fderr[1]);
842         else if (cmd->err)
843                 close(cmd->err);
844
845         return 0;
846 }
847
848 int finish_command(struct child_process *cmd)
849 {
850         int ret = wait_or_whine(cmd->pid, cmd->argv[0], 0);
851         child_process_clear(cmd);
852         return ret;
853 }
854
855 int finish_command_in_signal(struct child_process *cmd)
856 {
857         return wait_or_whine(cmd->pid, cmd->argv[0], 1);
858 }
859
860
861 int run_command(struct child_process *cmd)
862 {
863         int code;
864
865         if (cmd->out < 0 || cmd->err < 0)
866                 die("BUG: run_command with a pipe can cause deadlock");
867
868         code = start_command(cmd);
869         if (code)
870                 return code;
871         return finish_command(cmd);
872 }
873
874 int run_command_v_opt(const char **argv, int opt)
875 {
876         return run_command_v_opt_cd_env(argv, opt, NULL, NULL);
877 }
878
879 int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env)
880 {
881         struct child_process cmd = CHILD_PROCESS_INIT;
882         cmd.argv = argv;
883         cmd.no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
884         cmd.git_cmd = opt & RUN_GIT_CMD ? 1 : 0;
885         cmd.stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
886         cmd.silent_exec_failure = opt & RUN_SILENT_EXEC_FAILURE ? 1 : 0;
887         cmd.use_shell = opt & RUN_USING_SHELL ? 1 : 0;
888         cmd.clean_on_exit = opt & RUN_CLEAN_ON_EXIT ? 1 : 0;
889         cmd.dir = dir;
890         cmd.env = env;
891         return run_command(&cmd);
892 }
893
894 #ifndef NO_PTHREADS
895 static pthread_t main_thread;
896 static int main_thread_set;
897 static pthread_key_t async_key;
898 static pthread_key_t async_die_counter;
899
900 static void *run_thread(void *data)
901 {
902         struct async *async = data;
903         intptr_t ret;
904
905         if (async->isolate_sigpipe) {
906                 sigset_t mask;
907                 sigemptyset(&mask);
908                 sigaddset(&mask, SIGPIPE);
909                 if (pthread_sigmask(SIG_BLOCK, &mask, NULL) < 0) {
910                         ret = error("unable to block SIGPIPE in async thread");
911                         return (void *)ret;
912                 }
913         }
914
915         pthread_setspecific(async_key, async);
916         ret = async->proc(async->proc_in, async->proc_out, async->data);
917         return (void *)ret;
918 }
919
920 static NORETURN void die_async(const char *err, va_list params)
921 {
922         vreportf("fatal: ", err, params);
923
924         if (in_async()) {
925                 struct async *async = pthread_getspecific(async_key);
926                 if (async->proc_in >= 0)
927                         close(async->proc_in);
928                 if (async->proc_out >= 0)
929                         close(async->proc_out);
930                 pthread_exit((void *)128);
931         }
932
933         exit(128);
934 }
935
936 static int async_die_is_recursing(void)
937 {
938         void *ret = pthread_getspecific(async_die_counter);
939         pthread_setspecific(async_die_counter, (void *)1);
940         return ret != NULL;
941 }
942
943 int in_async(void)
944 {
945         if (!main_thread_set)
946                 return 0; /* no asyncs started yet */
947         return !pthread_equal(main_thread, pthread_self());
948 }
949
950 static void NORETURN async_exit(int code)
951 {
952         pthread_exit((void *)(intptr_t)code);
953 }
954
955 #else
956
957 static struct {
958         void (**handlers)(void);
959         size_t nr;
960         size_t alloc;
961 } git_atexit_hdlrs;
962
963 static int git_atexit_installed;
964
965 static void git_atexit_dispatch(void)
966 {
967         size_t i;
968
969         for (i=git_atexit_hdlrs.nr ; i ; i--)
970                 git_atexit_hdlrs.handlers[i-1]();
971 }
972
973 static void git_atexit_clear(void)
974 {
975         free(git_atexit_hdlrs.handlers);
976         memset(&git_atexit_hdlrs, 0, sizeof(git_atexit_hdlrs));
977         git_atexit_installed = 0;
978 }
979
980 #undef atexit
981 int git_atexit(void (*handler)(void))
982 {
983         ALLOC_GROW(git_atexit_hdlrs.handlers, git_atexit_hdlrs.nr + 1, git_atexit_hdlrs.alloc);
984         git_atexit_hdlrs.handlers[git_atexit_hdlrs.nr++] = handler;
985         if (!git_atexit_installed) {
986                 if (atexit(&git_atexit_dispatch))
987                         return -1;
988                 git_atexit_installed = 1;
989         }
990         return 0;
991 }
992 #define atexit git_atexit
993
994 static int process_is_async;
995 int in_async(void)
996 {
997         return process_is_async;
998 }
999
1000 static void NORETURN async_exit(int code)
1001 {
1002         exit(code);
1003 }
1004
1005 #endif
1006
1007 void check_pipe(int err)
1008 {
1009         if (err == EPIPE) {
1010                 if (in_async())
1011                         async_exit(141);
1012
1013                 signal(SIGPIPE, SIG_DFL);
1014                 raise(SIGPIPE);
1015                 /* Should never happen, but just in case... */
1016                 exit(141);
1017         }
1018 }
1019
1020 int start_async(struct async *async)
1021 {
1022         int need_in, need_out;
1023         int fdin[2], fdout[2];
1024         int proc_in, proc_out;
1025
1026         need_in = async->in < 0;
1027         if (need_in) {
1028                 if (pipe(fdin) < 0) {
1029                         if (async->out > 0)
1030                                 close(async->out);
1031                         return error_errno("cannot create pipe");
1032                 }
1033                 async->in = fdin[1];
1034         }
1035
1036         need_out = async->out < 0;
1037         if (need_out) {
1038                 if (pipe(fdout) < 0) {
1039                         if (need_in)
1040                                 close_pair(fdin);
1041                         else if (async->in)
1042                                 close(async->in);
1043                         return error_errno("cannot create pipe");
1044                 }
1045                 async->out = fdout[0];
1046         }
1047
1048         if (need_in)
1049                 proc_in = fdin[0];
1050         else if (async->in)
1051                 proc_in = async->in;
1052         else
1053                 proc_in = -1;
1054
1055         if (need_out)
1056                 proc_out = fdout[1];
1057         else if (async->out)
1058                 proc_out = async->out;
1059         else
1060                 proc_out = -1;
1061
1062 #ifdef NO_PTHREADS
1063         /* Flush stdio before fork() to avoid cloning buffers */
1064         fflush(NULL);
1065
1066         async->pid = fork();
1067         if (async->pid < 0) {
1068                 error_errno("fork (async) failed");
1069                 goto error;
1070         }
1071         if (!async->pid) {
1072                 if (need_in)
1073                         close(fdin[1]);
1074                 if (need_out)
1075                         close(fdout[0]);
1076                 git_atexit_clear();
1077                 process_is_async = 1;
1078                 exit(!!async->proc(proc_in, proc_out, async->data));
1079         }
1080
1081         mark_child_for_cleanup(async->pid, NULL);
1082
1083         if (need_in)
1084                 close(fdin[0]);
1085         else if (async->in)
1086                 close(async->in);
1087
1088         if (need_out)
1089                 close(fdout[1]);
1090         else if (async->out)
1091                 close(async->out);
1092 #else
1093         if (!main_thread_set) {
1094                 /*
1095                  * We assume that the first time that start_async is called
1096                  * it is from the main thread.
1097                  */
1098                 main_thread_set = 1;
1099                 main_thread = pthread_self();
1100                 pthread_key_create(&async_key, NULL);
1101                 pthread_key_create(&async_die_counter, NULL);
1102                 set_die_routine(die_async);
1103                 set_die_is_recursing_routine(async_die_is_recursing);
1104         }
1105
1106         if (proc_in >= 0)
1107                 set_cloexec(proc_in);
1108         if (proc_out >= 0)
1109                 set_cloexec(proc_out);
1110         async->proc_in = proc_in;
1111         async->proc_out = proc_out;
1112         {
1113                 int err = pthread_create(&async->tid, NULL, run_thread, async);
1114                 if (err) {
1115                         error_errno("cannot create thread");
1116                         goto error;
1117                 }
1118         }
1119 #endif
1120         return 0;
1121
1122 error:
1123         if (need_in)
1124                 close_pair(fdin);
1125         else if (async->in)
1126                 close(async->in);
1127
1128         if (need_out)
1129                 close_pair(fdout);
1130         else if (async->out)
1131                 close(async->out);
1132         return -1;
1133 }
1134
1135 int finish_async(struct async *async)
1136 {
1137 #ifdef NO_PTHREADS
1138         return wait_or_whine(async->pid, "child process", 0);
1139 #else
1140         void *ret = (void *)(intptr_t)(-1);
1141
1142         if (pthread_join(async->tid, &ret))
1143                 error("pthread_join failed");
1144         return (int)(intptr_t)ret;
1145 #endif
1146 }
1147
1148 const char *find_hook(const char *name)
1149 {
1150         static struct strbuf path = STRBUF_INIT;
1151
1152         strbuf_reset(&path);
1153         strbuf_git_path(&path, "hooks/%s", name);
1154         if (access(path.buf, X_OK) < 0) {
1155 #ifdef STRIP_EXTENSION
1156                 strbuf_addstr(&path, STRIP_EXTENSION);
1157                 if (access(path.buf, X_OK) >= 0)
1158                         return path.buf;
1159 #endif
1160                 return NULL;
1161         }
1162         return path.buf;
1163 }
1164
1165 int run_hook_ve(const char *const *env, const char *name, va_list args)
1166 {
1167         struct child_process hook = CHILD_PROCESS_INIT;
1168         const char *p;
1169
1170         p = find_hook(name);
1171         if (!p)
1172                 return 0;
1173
1174         argv_array_push(&hook.args, p);
1175         while ((p = va_arg(args, const char *)))
1176                 argv_array_push(&hook.args, p);
1177         hook.env = env;
1178         hook.no_stdin = 1;
1179         hook.stdout_to_stderr = 1;
1180
1181         return run_command(&hook);
1182 }
1183
1184 int run_hook_le(const char *const *env, const char *name, ...)
1185 {
1186         va_list args;
1187         int ret;
1188
1189         va_start(args, name);
1190         ret = run_hook_ve(env, name, args);
1191         va_end(args);
1192
1193         return ret;
1194 }
1195
1196 struct io_pump {
1197         /* initialized by caller */
1198         int fd;
1199         int type; /* POLLOUT or POLLIN */
1200         union {
1201                 struct {
1202                         const char *buf;
1203                         size_t len;
1204                 } out;
1205                 struct {
1206                         struct strbuf *buf;
1207                         size_t hint;
1208                 } in;
1209         } u;
1210
1211         /* returned by pump_io */
1212         int error; /* 0 for success, otherwise errno */
1213
1214         /* internal use */
1215         struct pollfd *pfd;
1216 };
1217
1218 static int pump_io_round(struct io_pump *slots, int nr, struct pollfd *pfd)
1219 {
1220         int pollsize = 0;
1221         int i;
1222
1223         for (i = 0; i < nr; i++) {
1224                 struct io_pump *io = &slots[i];
1225                 if (io->fd < 0)
1226                         continue;
1227                 pfd[pollsize].fd = io->fd;
1228                 pfd[pollsize].events = io->type;
1229                 io->pfd = &pfd[pollsize++];
1230         }
1231
1232         if (!pollsize)
1233                 return 0;
1234
1235         if (poll(pfd, pollsize, -1) < 0) {
1236                 if (errno == EINTR)
1237                         return 1;
1238                 die_errno("poll failed");
1239         }
1240
1241         for (i = 0; i < nr; i++) {
1242                 struct io_pump *io = &slots[i];
1243
1244                 if (io->fd < 0)
1245                         continue;
1246
1247                 if (!(io->pfd->revents & (POLLOUT|POLLIN|POLLHUP|POLLERR|POLLNVAL)))
1248                         continue;
1249
1250                 if (io->type == POLLOUT) {
1251                         ssize_t len = xwrite(io->fd,
1252                                              io->u.out.buf, io->u.out.len);
1253                         if (len < 0) {
1254                                 io->error = errno;
1255                                 close(io->fd);
1256                                 io->fd = -1;
1257                         } else {
1258                                 io->u.out.buf += len;
1259                                 io->u.out.len -= len;
1260                                 if (!io->u.out.len) {
1261                                         close(io->fd);
1262                                         io->fd = -1;
1263                                 }
1264                         }
1265                 }
1266
1267                 if (io->type == POLLIN) {
1268                         ssize_t len = strbuf_read_once(io->u.in.buf,
1269                                                        io->fd, io->u.in.hint);
1270                         if (len < 0)
1271                                 io->error = errno;
1272                         if (len <= 0) {
1273                                 close(io->fd);
1274                                 io->fd = -1;
1275                         }
1276                 }
1277         }
1278
1279         return 1;
1280 }
1281
1282 static int pump_io(struct io_pump *slots, int nr)
1283 {
1284         struct pollfd *pfd;
1285         int i;
1286
1287         for (i = 0; i < nr; i++)
1288                 slots[i].error = 0;
1289
1290         ALLOC_ARRAY(pfd, nr);
1291         while (pump_io_round(slots, nr, pfd))
1292                 ; /* nothing */
1293         free(pfd);
1294
1295         /* There may be multiple errno values, so just pick the first. */
1296         for (i = 0; i < nr; i++) {
1297                 if (slots[i].error) {
1298                         errno = slots[i].error;
1299                         return -1;
1300                 }
1301         }
1302         return 0;
1303 }
1304
1305
1306 int pipe_command(struct child_process *cmd,
1307                  const char *in, size_t in_len,
1308                  struct strbuf *out, size_t out_hint,
1309                  struct strbuf *err, size_t err_hint)
1310 {
1311         struct io_pump io[3];
1312         int nr = 0;
1313
1314         if (in)
1315                 cmd->in = -1;
1316         if (out)
1317                 cmd->out = -1;
1318         if (err)
1319                 cmd->err = -1;
1320
1321         if (start_command(cmd) < 0)
1322                 return -1;
1323
1324         if (in) {
1325                 io[nr].fd = cmd->in;
1326                 io[nr].type = POLLOUT;
1327                 io[nr].u.out.buf = in;
1328                 io[nr].u.out.len = in_len;
1329                 nr++;
1330         }
1331         if (out) {
1332                 io[nr].fd = cmd->out;
1333                 io[nr].type = POLLIN;
1334                 io[nr].u.in.buf = out;
1335                 io[nr].u.in.hint = out_hint;
1336                 nr++;
1337         }
1338         if (err) {
1339                 io[nr].fd = cmd->err;
1340                 io[nr].type = POLLIN;
1341                 io[nr].u.in.buf = err;
1342                 io[nr].u.in.hint = err_hint;
1343                 nr++;
1344         }
1345
1346         if (pump_io(io, nr) < 0) {
1347                 finish_command(cmd); /* throw away exit code */
1348                 return -1;
1349         }
1350
1351         return finish_command(cmd);
1352 }
1353
1354 enum child_state {
1355         GIT_CP_FREE,
1356         GIT_CP_WORKING,
1357         GIT_CP_WAIT_CLEANUP,
1358 };
1359
1360 struct parallel_processes {
1361         void *data;
1362
1363         int max_processes;
1364         int nr_processes;
1365
1366         get_next_task_fn get_next_task;
1367         start_failure_fn start_failure;
1368         task_finished_fn task_finished;
1369
1370         struct {
1371                 enum child_state state;
1372                 struct child_process process;
1373                 struct strbuf err;
1374                 void *data;
1375         } *children;
1376         /*
1377          * The struct pollfd is logically part of *children,
1378          * but the system call expects it as its own array.
1379          */
1380         struct pollfd *pfd;
1381
1382         unsigned shutdown : 1;
1383
1384         int output_owner;
1385         struct strbuf buffered_output; /* of finished children */
1386 };
1387
1388 static int default_start_failure(struct strbuf *out,
1389                                  void *pp_cb,
1390                                  void *pp_task_cb)
1391 {
1392         return 0;
1393 }
1394
1395 static int default_task_finished(int result,
1396                                  struct strbuf *out,
1397                                  void *pp_cb,
1398                                  void *pp_task_cb)
1399 {
1400         return 0;
1401 }
1402
1403 static void kill_children(struct parallel_processes *pp, int signo)
1404 {
1405         int i, n = pp->max_processes;
1406
1407         for (i = 0; i < n; i++)
1408                 if (pp->children[i].state == GIT_CP_WORKING)
1409                         kill(pp->children[i].process.pid, signo);
1410 }
1411
1412 static struct parallel_processes *pp_for_signal;
1413
1414 static void handle_children_on_signal(int signo)
1415 {
1416         kill_children(pp_for_signal, signo);
1417         sigchain_pop(signo);
1418         raise(signo);
1419 }
1420
1421 static void pp_init(struct parallel_processes *pp,
1422                     int n,
1423                     get_next_task_fn get_next_task,
1424                     start_failure_fn start_failure,
1425                     task_finished_fn task_finished,
1426                     void *data)
1427 {
1428         int i;
1429
1430         if (n < 1)
1431                 n = online_cpus();
1432
1433         pp->max_processes = n;
1434
1435         trace_printf("run_processes_parallel: preparing to run up to %d tasks", n);
1436
1437         pp->data = data;
1438         if (!get_next_task)
1439                 die("BUG: you need to specify a get_next_task function");
1440         pp->get_next_task = get_next_task;
1441
1442         pp->start_failure = start_failure ? start_failure : default_start_failure;
1443         pp->task_finished = task_finished ? task_finished : default_task_finished;
1444
1445         pp->nr_processes = 0;
1446         pp->output_owner = 0;
1447         pp->shutdown = 0;
1448         pp->children = xcalloc(n, sizeof(*pp->children));
1449         pp->pfd = xcalloc(n, sizeof(*pp->pfd));
1450         strbuf_init(&pp->buffered_output, 0);
1451
1452         for (i = 0; i < n; i++) {
1453                 strbuf_init(&pp->children[i].err, 0);
1454                 child_process_init(&pp->children[i].process);
1455                 pp->pfd[i].events = POLLIN | POLLHUP;
1456                 pp->pfd[i].fd = -1;
1457         }
1458
1459         pp_for_signal = pp;
1460         sigchain_push_common(handle_children_on_signal);
1461 }
1462
1463 static void pp_cleanup(struct parallel_processes *pp)
1464 {
1465         int i;
1466
1467         trace_printf("run_processes_parallel: done");
1468         for (i = 0; i < pp->max_processes; i++) {
1469                 strbuf_release(&pp->children[i].err);
1470                 child_process_clear(&pp->children[i].process);
1471         }
1472
1473         free(pp->children);
1474         free(pp->pfd);
1475
1476         /*
1477          * When get_next_task added messages to the buffer in its last
1478          * iteration, the buffered output is non empty.
1479          */
1480         strbuf_write(&pp->buffered_output, stderr);
1481         strbuf_release(&pp->buffered_output);
1482
1483         sigchain_pop_common();
1484 }
1485
1486 /* returns
1487  *  0 if a new task was started.
1488  *  1 if no new jobs was started (get_next_task ran out of work, non critical
1489  *    problem with starting a new command)
1490  * <0 no new job was started, user wishes to shutdown early. Use negative code
1491  *    to signal the children.
1492  */
1493 static int pp_start_one(struct parallel_processes *pp)
1494 {
1495         int i, code;
1496
1497         for (i = 0; i < pp->max_processes; i++)
1498                 if (pp->children[i].state == GIT_CP_FREE)
1499                         break;
1500         if (i == pp->max_processes)
1501                 die("BUG: bookkeeping is hard");
1502
1503         code = pp->get_next_task(&pp->children[i].process,
1504                                  &pp->children[i].err,
1505                                  pp->data,
1506                                  &pp->children[i].data);
1507         if (!code) {
1508                 strbuf_addbuf(&pp->buffered_output, &pp->children[i].err);
1509                 strbuf_reset(&pp->children[i].err);
1510                 return 1;
1511         }
1512         pp->children[i].process.err = -1;
1513         pp->children[i].process.stdout_to_stderr = 1;
1514         pp->children[i].process.no_stdin = 1;
1515
1516         if (start_command(&pp->children[i].process)) {
1517                 code = pp->start_failure(&pp->children[i].err,
1518                                          pp->data,
1519                                          &pp->children[i].data);
1520                 strbuf_addbuf(&pp->buffered_output, &pp->children[i].err);
1521                 strbuf_reset(&pp->children[i].err);
1522                 if (code)
1523                         pp->shutdown = 1;
1524                 return code;
1525         }
1526
1527         pp->nr_processes++;
1528         pp->children[i].state = GIT_CP_WORKING;
1529         pp->pfd[i].fd = pp->children[i].process.err;
1530         return 0;
1531 }
1532
1533 static void pp_buffer_stderr(struct parallel_processes *pp, int output_timeout)
1534 {
1535         int i;
1536
1537         while ((i = poll(pp->pfd, pp->max_processes, output_timeout)) < 0) {
1538                 if (errno == EINTR)
1539                         continue;
1540                 pp_cleanup(pp);
1541                 die_errno("poll");
1542         }
1543
1544         /* Buffer output from all pipes. */
1545         for (i = 0; i < pp->max_processes; i++) {
1546                 if (pp->children[i].state == GIT_CP_WORKING &&
1547                     pp->pfd[i].revents & (POLLIN | POLLHUP)) {
1548                         int n = strbuf_read_once(&pp->children[i].err,
1549                                                  pp->children[i].process.err, 0);
1550                         if (n == 0) {
1551                                 close(pp->children[i].process.err);
1552                                 pp->children[i].state = GIT_CP_WAIT_CLEANUP;
1553                         } else if (n < 0)
1554                                 if (errno != EAGAIN)
1555                                         die_errno("read");
1556                 }
1557         }
1558 }
1559
1560 static void pp_output(struct parallel_processes *pp)
1561 {
1562         int i = pp->output_owner;
1563         if (pp->children[i].state == GIT_CP_WORKING &&
1564             pp->children[i].err.len) {
1565                 strbuf_write(&pp->children[i].err, stderr);
1566                 strbuf_reset(&pp->children[i].err);
1567         }
1568 }
1569
1570 static int pp_collect_finished(struct parallel_processes *pp)
1571 {
1572         int i, code;
1573         int n = pp->max_processes;
1574         int result = 0;
1575
1576         while (pp->nr_processes > 0) {
1577                 for (i = 0; i < pp->max_processes; i++)
1578                         if (pp->children[i].state == GIT_CP_WAIT_CLEANUP)
1579                                 break;
1580                 if (i == pp->max_processes)
1581                         break;
1582
1583                 code = finish_command(&pp->children[i].process);
1584
1585                 code = pp->task_finished(code,
1586                                          &pp->children[i].err, pp->data,
1587                                          &pp->children[i].data);
1588
1589                 if (code)
1590                         result = code;
1591                 if (code < 0)
1592                         break;
1593
1594                 pp->nr_processes--;
1595                 pp->children[i].state = GIT_CP_FREE;
1596                 pp->pfd[i].fd = -1;
1597                 child_process_init(&pp->children[i].process);
1598
1599                 if (i != pp->output_owner) {
1600                         strbuf_addbuf(&pp->buffered_output, &pp->children[i].err);
1601                         strbuf_reset(&pp->children[i].err);
1602                 } else {
1603                         strbuf_write(&pp->children[i].err, stderr);
1604                         strbuf_reset(&pp->children[i].err);
1605
1606                         /* Output all other finished child processes */
1607                         strbuf_write(&pp->buffered_output, stderr);
1608                         strbuf_reset(&pp->buffered_output);
1609
1610                         /*
1611                          * Pick next process to output live.
1612                          * NEEDSWORK:
1613                          * For now we pick it randomly by doing a round
1614                          * robin. Later we may want to pick the one with
1615                          * the most output or the longest or shortest
1616                          * running process time.
1617                          */
1618                         for (i = 0; i < n; i++)
1619                                 if (pp->children[(pp->output_owner + i) % n].state == GIT_CP_WORKING)
1620                                         break;
1621                         pp->output_owner = (pp->output_owner + i) % n;
1622                 }
1623         }
1624         return result;
1625 }
1626
1627 int run_processes_parallel(int n,
1628                            get_next_task_fn get_next_task,
1629                            start_failure_fn start_failure,
1630                            task_finished_fn task_finished,
1631                            void *pp_cb)
1632 {
1633         int i, code;
1634         int output_timeout = 100;
1635         int spawn_cap = 4;
1636         struct parallel_processes pp;
1637
1638         pp_init(&pp, n, get_next_task, start_failure, task_finished, pp_cb);
1639         while (1) {
1640                 for (i = 0;
1641                     i < spawn_cap && !pp.shutdown &&
1642                     pp.nr_processes < pp.max_processes;
1643                     i++) {
1644                         code = pp_start_one(&pp);
1645                         if (!code)
1646                                 continue;
1647                         if (code < 0) {
1648                                 pp.shutdown = 1;
1649                                 kill_children(&pp, -code);
1650                         }
1651                         break;
1652                 }
1653                 if (!pp.nr_processes)
1654                         break;
1655                 pp_buffer_stderr(&pp, output_timeout);
1656                 pp_output(&pp);
1657                 code = pp_collect_finished(&pp);
1658                 if (code) {
1659                         pp.shutdown = 1;
1660                         if (code < 0)
1661                                 kill_children(&pp, -code);
1662                 }
1663         }
1664
1665         pp_cleanup(&pp);
1666         return 0;
1667 }