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