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