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