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