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