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