run-command: prepare child environment before forking
[git/debian.git] / run-command.c
blob15e2e74a7eb270085fd256d50ef0e05f20d682a1
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"
9 void child_process_init(struct child_process *child)
11 memset(child, 0, sizeof(*child));
12 argv_array_init(&child->args);
13 argv_array_init(&child->env_array);
16 void child_process_clear(struct child_process *child)
18 argv_array_clear(&child->args);
19 argv_array_clear(&child->env_array);
22 struct child_to_clean {
23 pid_t pid;
24 struct child_process *process;
25 struct child_to_clean *next;
27 static struct child_to_clean *children_to_clean;
28 static int installed_child_cleanup_handler;
30 static void cleanup_children(int sig, int in_signal)
32 struct child_to_clean *children_to_wait_for = NULL;
34 while (children_to_clean) {
35 struct child_to_clean *p = children_to_clean;
36 children_to_clean = p->next;
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
45 process->clean_on_exit_handler(process);
49 kill(p->pid, sig);
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);
60 while (children_to_wait_for) {
61 struct child_to_clean *p = children_to_wait_for;
62 children_to_wait_for = p->next;
64 while (waitpid(p->pid, NULL, 0) < 0 && errno == EINTR)
65 ; /* spin waiting for process exit or error */
67 if (!in_signal)
68 free(p);
72 static void cleanup_children_on_signal(int sig)
74 cleanup_children(sig, 1);
75 sigchain_pop(sig);
76 raise(sig);
79 static void cleanup_children_on_exit(void)
81 cleanup_children(SIGTERM, 0);
84 static void mark_child_for_cleanup(pid_t pid, struct child_process *process)
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;
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;
99 static void clear_child_for_cleanup(pid_t pid)
101 struct child_to_clean **pp;
103 for (pp = &children_to_clean; *pp; pp = &(*pp)->next) {
104 struct child_to_clean *clean_me = *pp;
106 if (clean_me->pid == pid) {
107 *pp = clean_me->next;
108 free(clean_me);
109 return;
114 static inline void close_pair(int fd[2])
116 close(fd[0]);
117 close(fd[1]);
120 #ifndef GIT_WINDOWS_NATIVE
121 static inline void dup_devnull(int to)
123 int fd = open("/dev/null", O_RDWR);
124 if (fd < 0)
125 die_errno(_("open /dev/null failed"));
126 if (dup2(fd, to) < 0)
127 die_errno(_("dup2(%d,%d) failed"), fd, to);
128 close(fd);
130 #endif
132 static char *locate_in_PATH(const char *file)
134 const char *p = getenv("PATH");
135 struct strbuf buf = STRBUF_INIT;
137 if (!p || !*p)
138 return NULL;
140 while (1) {
141 const char *end = strchrnul(p, ':');
143 strbuf_reset(&buf);
145 /* POSIX specifies an empty entry as the current directory. */
146 if (end != p) {
147 strbuf_add(&buf, p, end - p);
148 strbuf_addch(&buf, '/');
150 strbuf_addstr(&buf, file);
152 if (!access(buf.buf, F_OK))
153 return strbuf_detach(&buf, NULL);
155 if (!*end)
156 break;
157 p = end + 1;
160 strbuf_release(&buf);
161 return NULL;
164 static int exists_in_PATH(const char *file)
166 char *r = locate_in_PATH(file);
167 free(r);
168 return r != NULL;
171 int sane_execvp(const char *file, char * const argv[])
173 if (!execvp(file, argv))
174 return 0; /* cannot happen ;-) */
177 * When a command can't be found because one of the directories
178 * listed in $PATH is unsearchable, execvp reports EACCES, but
179 * careful usability testing (read: analysis of occasional bug
180 * reports) reveals that "No such file or directory" is more
181 * intuitive.
183 * We avoid commands with "/", because execvp will not do $PATH
184 * lookups in that case.
186 * The reassignment of EACCES to errno looks like a no-op below,
187 * but we need to protect against exists_in_PATH overwriting errno.
189 if (errno == EACCES && !strchr(file, '/'))
190 errno = exists_in_PATH(file) ? EACCES : ENOENT;
191 else if (errno == ENOTDIR && !strchr(file, '/'))
192 errno = ENOENT;
193 return -1;
196 static const char **prepare_shell_cmd(struct argv_array *out, const char **argv)
198 if (!argv[0])
199 die("BUG: shell command is empty");
201 if (strcspn(argv[0], "|&;<>()$`\\\"' \t\n*?[#~=%") != strlen(argv[0])) {
202 #ifndef GIT_WINDOWS_NATIVE
203 argv_array_push(out, SHELL_PATH);
204 #else
205 argv_array_push(out, "sh");
206 #endif
207 argv_array_push(out, "-c");
210 * If we have no extra arguments, we do not even need to
211 * bother with the "$@" magic.
213 if (!argv[1])
214 argv_array_push(out, argv[0]);
215 else
216 argv_array_pushf(out, "%s \"$@\"", argv[0]);
219 argv_array_pushv(out, argv);
220 return out->argv;
223 #ifndef GIT_WINDOWS_NATIVE
224 static int child_notifier = -1;
226 static void notify_parent(void)
229 * execvp failed. If possible, we'd like to let start_command
230 * know, so failures like ENOENT can be handled right away; but
231 * otherwise, finish_command will still report the error.
233 xwrite(child_notifier, "", 1);
236 static void prepare_cmd(struct argv_array *out, const struct child_process *cmd)
238 if (!cmd->argv[0])
239 die("BUG: command is empty");
242 * Add SHELL_PATH so in the event exec fails with ENOEXEC we can
243 * attempt to interpret the command with 'sh'.
245 argv_array_push(out, SHELL_PATH);
247 if (cmd->git_cmd) {
248 argv_array_push(out, "git");
249 argv_array_pushv(out, cmd->argv);
250 } else if (cmd->use_shell) {
251 prepare_shell_cmd(out, cmd->argv);
252 } else {
253 argv_array_pushv(out, cmd->argv);
257 * If there are no '/' characters in the command then perform a path
258 * lookup and use the resolved path as the command to exec. If there
259 * are no '/' characters or if the command wasn't found in the path,
260 * have exec attempt to invoke the command directly.
262 if (!strchr(out->argv[1], '/')) {
263 char *program = locate_in_PATH(out->argv[1]);
264 if (program) {
265 free((char *)out->argv[1]);
266 out->argv[1] = program;
271 static char **prep_childenv(const char *const *deltaenv)
273 extern char **environ;
274 char **childenv;
275 struct string_list env = STRING_LIST_INIT_DUP;
276 struct strbuf key = STRBUF_INIT;
277 const char *const *p;
278 int i;
280 /* Construct a sorted string list consisting of the current environ */
281 for (p = (const char *const *) environ; p && *p; p++) {
282 const char *equals = strchr(*p, '=');
284 if (equals) {
285 strbuf_reset(&key);
286 strbuf_add(&key, *p, equals - *p);
287 string_list_append(&env, key.buf)->util = (void *) *p;
288 } else {
289 string_list_append(&env, *p)->util = (void *) *p;
292 string_list_sort(&env);
294 /* Merge in 'deltaenv' with the current environ */
295 for (p = deltaenv; p && *p; p++) {
296 const char *equals = strchr(*p, '=');
298 if (equals) {
299 /* ('key=value'), insert or replace entry */
300 strbuf_reset(&key);
301 strbuf_add(&key, *p, equals - *p);
302 string_list_insert(&env, key.buf)->util = (void *) *p;
303 } else {
304 /* otherwise ('key') remove existing entry */
305 string_list_remove(&env, *p, 0);
309 /* Create an array of 'char *' to be used as the childenv */
310 childenv = xmalloc((env.nr + 1) * sizeof(char *));
311 for (i = 0; i < env.nr; i++)
312 childenv[i] = env.items[i].util;
313 childenv[env.nr] = NULL;
315 string_list_clear(&env, 0);
316 strbuf_release(&key);
317 return childenv;
319 #endif
321 static inline void set_cloexec(int fd)
323 int flags = fcntl(fd, F_GETFD);
324 if (flags >= 0)
325 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
328 static int wait_or_whine(pid_t pid, const char *argv0, int in_signal)
330 int status, code = -1;
331 pid_t waiting;
332 int failed_errno = 0;
334 while ((waiting = waitpid(pid, &status, 0)) < 0 && errno == EINTR)
335 ; /* nothing */
336 if (in_signal)
337 return 0;
339 if (waiting < 0) {
340 failed_errno = errno;
341 error_errno("waitpid for %s failed", argv0);
342 } else if (waiting != pid) {
343 error("waitpid is confused (%s)", argv0);
344 } else if (WIFSIGNALED(status)) {
345 code = WTERMSIG(status);
346 if (code != SIGINT && code != SIGQUIT && code != SIGPIPE)
347 error("%s died of signal %d", argv0, code);
349 * This return value is chosen so that code & 0xff
350 * mimics the exit code that a POSIX shell would report for
351 * a program that died from this signal.
353 code += 128;
354 } else if (WIFEXITED(status)) {
355 code = WEXITSTATUS(status);
357 * Convert special exit code when execvp failed.
359 if (code == 127) {
360 code = -1;
361 failed_errno = ENOENT;
363 } else {
364 error("waitpid is confused (%s)", argv0);
367 clear_child_for_cleanup(pid);
369 errno = failed_errno;
370 return code;
373 int start_command(struct child_process *cmd)
375 int need_in, need_out, need_err;
376 int fdin[2], fdout[2], fderr[2];
377 int failed_errno;
378 char *str;
380 if (!cmd->argv)
381 cmd->argv = cmd->args.argv;
382 if (!cmd->env)
383 cmd->env = cmd->env_array.argv;
386 * In case of errors we must keep the promise to close FDs
387 * that have been passed in via ->in and ->out.
390 need_in = !cmd->no_stdin && cmd->in < 0;
391 if (need_in) {
392 if (pipe(fdin) < 0) {
393 failed_errno = errno;
394 if (cmd->out > 0)
395 close(cmd->out);
396 str = "standard input";
397 goto fail_pipe;
399 cmd->in = fdin[1];
402 need_out = !cmd->no_stdout
403 && !cmd->stdout_to_stderr
404 && cmd->out < 0;
405 if (need_out) {
406 if (pipe(fdout) < 0) {
407 failed_errno = errno;
408 if (need_in)
409 close_pair(fdin);
410 else if (cmd->in)
411 close(cmd->in);
412 str = "standard output";
413 goto fail_pipe;
415 cmd->out = fdout[0];
418 need_err = !cmd->no_stderr && cmd->err < 0;
419 if (need_err) {
420 if (pipe(fderr) < 0) {
421 failed_errno = errno;
422 if (need_in)
423 close_pair(fdin);
424 else if (cmd->in)
425 close(cmd->in);
426 if (need_out)
427 close_pair(fdout);
428 else if (cmd->out)
429 close(cmd->out);
430 str = "standard error";
431 fail_pipe:
432 error("cannot create %s pipe for %s: %s",
433 str, cmd->argv[0], strerror(failed_errno));
434 child_process_clear(cmd);
435 errno = failed_errno;
436 return -1;
438 cmd->err = fderr[0];
441 trace_argv_printf(cmd->argv, "trace: run_command:");
442 fflush(NULL);
444 #ifndef GIT_WINDOWS_NATIVE
446 int notify_pipe[2];
447 char **childenv;
448 struct argv_array argv = ARGV_ARRAY_INIT;
450 if (pipe(notify_pipe))
451 notify_pipe[0] = notify_pipe[1] = -1;
453 prepare_cmd(&argv, cmd);
454 childenv = prep_childenv(cmd->env);
456 cmd->pid = fork();
457 failed_errno = errno;
458 if (!cmd->pid) {
460 * Redirect the channel to write syscall error messages to
461 * before redirecting the process's stderr so that all die()
462 * in subsequent call paths use the parent's stderr.
464 if (cmd->no_stderr || need_err) {
465 int child_err = dup(2);
466 set_cloexec(child_err);
467 set_error_handle(fdopen(child_err, "w"));
470 close(notify_pipe[0]);
471 set_cloexec(notify_pipe[1]);
472 child_notifier = notify_pipe[1];
473 atexit(notify_parent);
475 if (cmd->no_stdin)
476 dup_devnull(0);
477 else if (need_in) {
478 dup2(fdin[0], 0);
479 close_pair(fdin);
480 } else if (cmd->in) {
481 dup2(cmd->in, 0);
482 close(cmd->in);
485 if (cmd->no_stderr)
486 dup_devnull(2);
487 else if (need_err) {
488 dup2(fderr[1], 2);
489 close_pair(fderr);
490 } else if (cmd->err > 1) {
491 dup2(cmd->err, 2);
492 close(cmd->err);
495 if (cmd->no_stdout)
496 dup_devnull(1);
497 else if (cmd->stdout_to_stderr)
498 dup2(2, 1);
499 else if (need_out) {
500 dup2(fdout[1], 1);
501 close_pair(fdout);
502 } else if (cmd->out > 1) {
503 dup2(cmd->out, 1);
504 close(cmd->out);
507 if (cmd->dir && chdir(cmd->dir))
508 die_errno("exec '%s': cd to '%s' failed", cmd->argv[0],
509 cmd->dir);
512 * Attempt to exec using the command and arguments starting at
513 * argv.argv[1]. argv.argv[0] contains SHELL_PATH which will
514 * be used in the event exec failed with ENOEXEC at which point
515 * we will try to interpret the command using 'sh'.
517 execve(argv.argv[1], (char *const *) argv.argv + 1,
518 (char *const *) childenv);
519 if (errno == ENOEXEC)
520 execve(argv.argv[0], (char *const *) argv.argv,
521 (char *const *) childenv);
523 if (errno == ENOENT) {
524 if (!cmd->silent_exec_failure)
525 error("cannot run %s: %s", cmd->argv[0],
526 strerror(ENOENT));
527 exit(127);
528 } else {
529 die_errno("cannot exec '%s'", cmd->argv[0]);
532 if (cmd->pid < 0)
533 error_errno("cannot fork() for %s", cmd->argv[0]);
534 else if (cmd->clean_on_exit)
535 mark_child_for_cleanup(cmd->pid, cmd);
538 * Wait for child's exec. If the exec succeeds (or if fork()
539 * failed), EOF is seen immediately by the parent. Otherwise, the
540 * child process sends a single byte.
541 * Note that use of this infrastructure is completely advisory,
542 * therefore, we keep error checks minimal.
544 close(notify_pipe[1]);
545 if (read(notify_pipe[0], &notify_pipe[1], 1) == 1) {
547 * At this point we know that fork() succeeded, but exec()
548 * failed. Errors have been reported to our stderr.
550 wait_or_whine(cmd->pid, cmd->argv[0], 0);
551 failed_errno = errno;
552 cmd->pid = -1;
554 close(notify_pipe[0]);
556 argv_array_clear(&argv);
557 free(childenv);
559 #else
561 int fhin = 0, fhout = 1, fherr = 2;
562 const char **sargv = cmd->argv;
563 struct argv_array nargv = ARGV_ARRAY_INIT;
565 if (cmd->no_stdin)
566 fhin = open("/dev/null", O_RDWR);
567 else if (need_in)
568 fhin = dup(fdin[0]);
569 else if (cmd->in)
570 fhin = dup(cmd->in);
572 if (cmd->no_stderr)
573 fherr = open("/dev/null", O_RDWR);
574 else if (need_err)
575 fherr = dup(fderr[1]);
576 else if (cmd->err > 2)
577 fherr = dup(cmd->err);
579 if (cmd->no_stdout)
580 fhout = open("/dev/null", O_RDWR);
581 else if (cmd->stdout_to_stderr)
582 fhout = dup(fherr);
583 else if (need_out)
584 fhout = dup(fdout[1]);
585 else if (cmd->out > 1)
586 fhout = dup(cmd->out);
588 if (cmd->git_cmd)
589 cmd->argv = prepare_git_cmd(&nargv, cmd->argv);
590 else if (cmd->use_shell)
591 cmd->argv = prepare_shell_cmd(&nargv, cmd->argv);
593 cmd->pid = mingw_spawnvpe(cmd->argv[0], cmd->argv, (char**) cmd->env,
594 cmd->dir, fhin, fhout, fherr);
595 failed_errno = errno;
596 if (cmd->pid < 0 && (!cmd->silent_exec_failure || errno != ENOENT))
597 error_errno("cannot spawn %s", cmd->argv[0]);
598 if (cmd->clean_on_exit && cmd->pid >= 0)
599 mark_child_for_cleanup(cmd->pid, cmd);
601 argv_array_clear(&nargv);
602 cmd->argv = sargv;
603 if (fhin != 0)
604 close(fhin);
605 if (fhout != 1)
606 close(fhout);
607 if (fherr != 2)
608 close(fherr);
610 #endif
612 if (cmd->pid < 0) {
613 if (need_in)
614 close_pair(fdin);
615 else if (cmd->in)
616 close(cmd->in);
617 if (need_out)
618 close_pair(fdout);
619 else if (cmd->out)
620 close(cmd->out);
621 if (need_err)
622 close_pair(fderr);
623 else if (cmd->err)
624 close(cmd->err);
625 child_process_clear(cmd);
626 errno = failed_errno;
627 return -1;
630 if (need_in)
631 close(fdin[0]);
632 else if (cmd->in)
633 close(cmd->in);
635 if (need_out)
636 close(fdout[1]);
637 else if (cmd->out)
638 close(cmd->out);
640 if (need_err)
641 close(fderr[1]);
642 else if (cmd->err)
643 close(cmd->err);
645 return 0;
648 int finish_command(struct child_process *cmd)
650 int ret = wait_or_whine(cmd->pid, cmd->argv[0], 0);
651 child_process_clear(cmd);
652 return ret;
655 int finish_command_in_signal(struct child_process *cmd)
657 return wait_or_whine(cmd->pid, cmd->argv[0], 1);
661 int run_command(struct child_process *cmd)
663 int code;
665 if (cmd->out < 0 || cmd->err < 0)
666 die("BUG: run_command with a pipe can cause deadlock");
668 code = start_command(cmd);
669 if (code)
670 return code;
671 return finish_command(cmd);
674 int run_command_v_opt(const char **argv, int opt)
676 return run_command_v_opt_cd_env(argv, opt, NULL, NULL);
679 int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env)
681 struct child_process cmd = CHILD_PROCESS_INIT;
682 cmd.argv = argv;
683 cmd.no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
684 cmd.git_cmd = opt & RUN_GIT_CMD ? 1 : 0;
685 cmd.stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
686 cmd.silent_exec_failure = opt & RUN_SILENT_EXEC_FAILURE ? 1 : 0;
687 cmd.use_shell = opt & RUN_USING_SHELL ? 1 : 0;
688 cmd.clean_on_exit = opt & RUN_CLEAN_ON_EXIT ? 1 : 0;
689 cmd.dir = dir;
690 cmd.env = env;
691 return run_command(&cmd);
694 #ifndef NO_PTHREADS
695 static pthread_t main_thread;
696 static int main_thread_set;
697 static pthread_key_t async_key;
698 static pthread_key_t async_die_counter;
700 static void *run_thread(void *data)
702 struct async *async = data;
703 intptr_t ret;
705 if (async->isolate_sigpipe) {
706 sigset_t mask;
707 sigemptyset(&mask);
708 sigaddset(&mask, SIGPIPE);
709 if (pthread_sigmask(SIG_BLOCK, &mask, NULL) < 0) {
710 ret = error("unable to block SIGPIPE in async thread");
711 return (void *)ret;
715 pthread_setspecific(async_key, async);
716 ret = async->proc(async->proc_in, async->proc_out, async->data);
717 return (void *)ret;
720 static NORETURN void die_async(const char *err, va_list params)
722 vreportf("fatal: ", err, params);
724 if (in_async()) {
725 struct async *async = pthread_getspecific(async_key);
726 if (async->proc_in >= 0)
727 close(async->proc_in);
728 if (async->proc_out >= 0)
729 close(async->proc_out);
730 pthread_exit((void *)128);
733 exit(128);
736 static int async_die_is_recursing(void)
738 void *ret = pthread_getspecific(async_die_counter);
739 pthread_setspecific(async_die_counter, (void *)1);
740 return ret != NULL;
743 int in_async(void)
745 if (!main_thread_set)
746 return 0; /* no asyncs started yet */
747 return !pthread_equal(main_thread, pthread_self());
750 static void NORETURN async_exit(int code)
752 pthread_exit((void *)(intptr_t)code);
755 #else
757 static struct {
758 void (**handlers)(void);
759 size_t nr;
760 size_t alloc;
761 } git_atexit_hdlrs;
763 static int git_atexit_installed;
765 static void git_atexit_dispatch(void)
767 size_t i;
769 for (i=git_atexit_hdlrs.nr ; i ; i--)
770 git_atexit_hdlrs.handlers[i-1]();
773 static void git_atexit_clear(void)
775 free(git_atexit_hdlrs.handlers);
776 memset(&git_atexit_hdlrs, 0, sizeof(git_atexit_hdlrs));
777 git_atexit_installed = 0;
780 #undef atexit
781 int git_atexit(void (*handler)(void))
783 ALLOC_GROW(git_atexit_hdlrs.handlers, git_atexit_hdlrs.nr + 1, git_atexit_hdlrs.alloc);
784 git_atexit_hdlrs.handlers[git_atexit_hdlrs.nr++] = handler;
785 if (!git_atexit_installed) {
786 if (atexit(&git_atexit_dispatch))
787 return -1;
788 git_atexit_installed = 1;
790 return 0;
792 #define atexit git_atexit
794 static int process_is_async;
795 int in_async(void)
797 return process_is_async;
800 static void NORETURN async_exit(int code)
802 exit(code);
805 #endif
807 void check_pipe(int err)
809 if (err == EPIPE) {
810 if (in_async())
811 async_exit(141);
813 signal(SIGPIPE, SIG_DFL);
814 raise(SIGPIPE);
815 /* Should never happen, but just in case... */
816 exit(141);
820 int start_async(struct async *async)
822 int need_in, need_out;
823 int fdin[2], fdout[2];
824 int proc_in, proc_out;
826 need_in = async->in < 0;
827 if (need_in) {
828 if (pipe(fdin) < 0) {
829 if (async->out > 0)
830 close(async->out);
831 return error_errno("cannot create pipe");
833 async->in = fdin[1];
836 need_out = async->out < 0;
837 if (need_out) {
838 if (pipe(fdout) < 0) {
839 if (need_in)
840 close_pair(fdin);
841 else if (async->in)
842 close(async->in);
843 return error_errno("cannot create pipe");
845 async->out = fdout[0];
848 if (need_in)
849 proc_in = fdin[0];
850 else if (async->in)
851 proc_in = async->in;
852 else
853 proc_in = -1;
855 if (need_out)
856 proc_out = fdout[1];
857 else if (async->out)
858 proc_out = async->out;
859 else
860 proc_out = -1;
862 #ifdef NO_PTHREADS
863 /* Flush stdio before fork() to avoid cloning buffers */
864 fflush(NULL);
866 async->pid = fork();
867 if (async->pid < 0) {
868 error_errno("fork (async) failed");
869 goto error;
871 if (!async->pid) {
872 if (need_in)
873 close(fdin[1]);
874 if (need_out)
875 close(fdout[0]);
876 git_atexit_clear();
877 process_is_async = 1;
878 exit(!!async->proc(proc_in, proc_out, async->data));
881 mark_child_for_cleanup(async->pid, NULL);
883 if (need_in)
884 close(fdin[0]);
885 else if (async->in)
886 close(async->in);
888 if (need_out)
889 close(fdout[1]);
890 else if (async->out)
891 close(async->out);
892 #else
893 if (!main_thread_set) {
895 * We assume that the first time that start_async is called
896 * it is from the main thread.
898 main_thread_set = 1;
899 main_thread = pthread_self();
900 pthread_key_create(&async_key, NULL);
901 pthread_key_create(&async_die_counter, NULL);
902 set_die_routine(die_async);
903 set_die_is_recursing_routine(async_die_is_recursing);
906 if (proc_in >= 0)
907 set_cloexec(proc_in);
908 if (proc_out >= 0)
909 set_cloexec(proc_out);
910 async->proc_in = proc_in;
911 async->proc_out = proc_out;
913 int err = pthread_create(&async->tid, NULL, run_thread, async);
914 if (err) {
915 error_errno("cannot create thread");
916 goto error;
919 #endif
920 return 0;
922 error:
923 if (need_in)
924 close_pair(fdin);
925 else if (async->in)
926 close(async->in);
928 if (need_out)
929 close_pair(fdout);
930 else if (async->out)
931 close(async->out);
932 return -1;
935 int finish_async(struct async *async)
937 #ifdef NO_PTHREADS
938 return wait_or_whine(async->pid, "child process", 0);
939 #else
940 void *ret = (void *)(intptr_t)(-1);
942 if (pthread_join(async->tid, &ret))
943 error("pthread_join failed");
944 return (int)(intptr_t)ret;
945 #endif
948 const char *find_hook(const char *name)
950 static struct strbuf path = STRBUF_INIT;
952 strbuf_reset(&path);
953 strbuf_git_path(&path, "hooks/%s", name);
954 if (access(path.buf, X_OK) < 0) {
955 #ifdef STRIP_EXTENSION
956 strbuf_addstr(&path, STRIP_EXTENSION);
957 if (access(path.buf, X_OK) >= 0)
958 return path.buf;
959 #endif
960 return NULL;
962 return path.buf;
965 int run_hook_ve(const char *const *env, const char *name, va_list args)
967 struct child_process hook = CHILD_PROCESS_INIT;
968 const char *p;
970 p = find_hook(name);
971 if (!p)
972 return 0;
974 argv_array_push(&hook.args, p);
975 while ((p = va_arg(args, const char *)))
976 argv_array_push(&hook.args, p);
977 hook.env = env;
978 hook.no_stdin = 1;
979 hook.stdout_to_stderr = 1;
981 return run_command(&hook);
984 int run_hook_le(const char *const *env, const char *name, ...)
986 va_list args;
987 int ret;
989 va_start(args, name);
990 ret = run_hook_ve(env, name, args);
991 va_end(args);
993 return ret;
996 struct io_pump {
997 /* initialized by caller */
998 int fd;
999 int type; /* POLLOUT or POLLIN */
1000 union {
1001 struct {
1002 const char *buf;
1003 size_t len;
1004 } out;
1005 struct {
1006 struct strbuf *buf;
1007 size_t hint;
1008 } in;
1009 } u;
1011 /* returned by pump_io */
1012 int error; /* 0 for success, otherwise errno */
1014 /* internal use */
1015 struct pollfd *pfd;
1018 static int pump_io_round(struct io_pump *slots, int nr, struct pollfd *pfd)
1020 int pollsize = 0;
1021 int i;
1023 for (i = 0; i < nr; i++) {
1024 struct io_pump *io = &slots[i];
1025 if (io->fd < 0)
1026 continue;
1027 pfd[pollsize].fd = io->fd;
1028 pfd[pollsize].events = io->type;
1029 io->pfd = &pfd[pollsize++];
1032 if (!pollsize)
1033 return 0;
1035 if (poll(pfd, pollsize, -1) < 0) {
1036 if (errno == EINTR)
1037 return 1;
1038 die_errno("poll failed");
1041 for (i = 0; i < nr; i++) {
1042 struct io_pump *io = &slots[i];
1044 if (io->fd < 0)
1045 continue;
1047 if (!(io->pfd->revents & (POLLOUT|POLLIN|POLLHUP|POLLERR|POLLNVAL)))
1048 continue;
1050 if (io->type == POLLOUT) {
1051 ssize_t len = xwrite(io->fd,
1052 io->u.out.buf, io->u.out.len);
1053 if (len < 0) {
1054 io->error = errno;
1055 close(io->fd);
1056 io->fd = -1;
1057 } else {
1058 io->u.out.buf += len;
1059 io->u.out.len -= len;
1060 if (!io->u.out.len) {
1061 close(io->fd);
1062 io->fd = -1;
1067 if (io->type == POLLIN) {
1068 ssize_t len = strbuf_read_once(io->u.in.buf,
1069 io->fd, io->u.in.hint);
1070 if (len < 0)
1071 io->error = errno;
1072 if (len <= 0) {
1073 close(io->fd);
1074 io->fd = -1;
1079 return 1;
1082 static int pump_io(struct io_pump *slots, int nr)
1084 struct pollfd *pfd;
1085 int i;
1087 for (i = 0; i < nr; i++)
1088 slots[i].error = 0;
1090 ALLOC_ARRAY(pfd, nr);
1091 while (pump_io_round(slots, nr, pfd))
1092 ; /* nothing */
1093 free(pfd);
1095 /* There may be multiple errno values, so just pick the first. */
1096 for (i = 0; i < nr; i++) {
1097 if (slots[i].error) {
1098 errno = slots[i].error;
1099 return -1;
1102 return 0;
1106 int pipe_command(struct child_process *cmd,
1107 const char *in, size_t in_len,
1108 struct strbuf *out, size_t out_hint,
1109 struct strbuf *err, size_t err_hint)
1111 struct io_pump io[3];
1112 int nr = 0;
1114 if (in)
1115 cmd->in = -1;
1116 if (out)
1117 cmd->out = -1;
1118 if (err)
1119 cmd->err = -1;
1121 if (start_command(cmd) < 0)
1122 return -1;
1124 if (in) {
1125 io[nr].fd = cmd->in;
1126 io[nr].type = POLLOUT;
1127 io[nr].u.out.buf = in;
1128 io[nr].u.out.len = in_len;
1129 nr++;
1131 if (out) {
1132 io[nr].fd = cmd->out;
1133 io[nr].type = POLLIN;
1134 io[nr].u.in.buf = out;
1135 io[nr].u.in.hint = out_hint;
1136 nr++;
1138 if (err) {
1139 io[nr].fd = cmd->err;
1140 io[nr].type = POLLIN;
1141 io[nr].u.in.buf = err;
1142 io[nr].u.in.hint = err_hint;
1143 nr++;
1146 if (pump_io(io, nr) < 0) {
1147 finish_command(cmd); /* throw away exit code */
1148 return -1;
1151 return finish_command(cmd);
1154 enum child_state {
1155 GIT_CP_FREE,
1156 GIT_CP_WORKING,
1157 GIT_CP_WAIT_CLEANUP,
1160 struct parallel_processes {
1161 void *data;
1163 int max_processes;
1164 int nr_processes;
1166 get_next_task_fn get_next_task;
1167 start_failure_fn start_failure;
1168 task_finished_fn task_finished;
1170 struct {
1171 enum child_state state;
1172 struct child_process process;
1173 struct strbuf err;
1174 void *data;
1175 } *children;
1177 * The struct pollfd is logically part of *children,
1178 * but the system call expects it as its own array.
1180 struct pollfd *pfd;
1182 unsigned shutdown : 1;
1184 int output_owner;
1185 struct strbuf buffered_output; /* of finished children */
1188 static int default_start_failure(struct strbuf *out,
1189 void *pp_cb,
1190 void *pp_task_cb)
1192 return 0;
1195 static int default_task_finished(int result,
1196 struct strbuf *out,
1197 void *pp_cb,
1198 void *pp_task_cb)
1200 return 0;
1203 static void kill_children(struct parallel_processes *pp, int signo)
1205 int i, n = pp->max_processes;
1207 for (i = 0; i < n; i++)
1208 if (pp->children[i].state == GIT_CP_WORKING)
1209 kill(pp->children[i].process.pid, signo);
1212 static struct parallel_processes *pp_for_signal;
1214 static void handle_children_on_signal(int signo)
1216 kill_children(pp_for_signal, signo);
1217 sigchain_pop(signo);
1218 raise(signo);
1221 static void pp_init(struct parallel_processes *pp,
1222 int n,
1223 get_next_task_fn get_next_task,
1224 start_failure_fn start_failure,
1225 task_finished_fn task_finished,
1226 void *data)
1228 int i;
1230 if (n < 1)
1231 n = online_cpus();
1233 pp->max_processes = n;
1235 trace_printf("run_processes_parallel: preparing to run up to %d tasks", n);
1237 pp->data = data;
1238 if (!get_next_task)
1239 die("BUG: you need to specify a get_next_task function");
1240 pp->get_next_task = get_next_task;
1242 pp->start_failure = start_failure ? start_failure : default_start_failure;
1243 pp->task_finished = task_finished ? task_finished : default_task_finished;
1245 pp->nr_processes = 0;
1246 pp->output_owner = 0;
1247 pp->shutdown = 0;
1248 pp->children = xcalloc(n, sizeof(*pp->children));
1249 pp->pfd = xcalloc(n, sizeof(*pp->pfd));
1250 strbuf_init(&pp->buffered_output, 0);
1252 for (i = 0; i < n; i++) {
1253 strbuf_init(&pp->children[i].err, 0);
1254 child_process_init(&pp->children[i].process);
1255 pp->pfd[i].events = POLLIN | POLLHUP;
1256 pp->pfd[i].fd = -1;
1259 pp_for_signal = pp;
1260 sigchain_push_common(handle_children_on_signal);
1263 static void pp_cleanup(struct parallel_processes *pp)
1265 int i;
1267 trace_printf("run_processes_parallel: done");
1268 for (i = 0; i < pp->max_processes; i++) {
1269 strbuf_release(&pp->children[i].err);
1270 child_process_clear(&pp->children[i].process);
1273 free(pp->children);
1274 free(pp->pfd);
1277 * When get_next_task added messages to the buffer in its last
1278 * iteration, the buffered output is non empty.
1280 strbuf_write(&pp->buffered_output, stderr);
1281 strbuf_release(&pp->buffered_output);
1283 sigchain_pop_common();
1286 /* returns
1287 * 0 if a new task was started.
1288 * 1 if no new jobs was started (get_next_task ran out of work, non critical
1289 * problem with starting a new command)
1290 * <0 no new job was started, user wishes to shutdown early. Use negative code
1291 * to signal the children.
1293 static int pp_start_one(struct parallel_processes *pp)
1295 int i, code;
1297 for (i = 0; i < pp->max_processes; i++)
1298 if (pp->children[i].state == GIT_CP_FREE)
1299 break;
1300 if (i == pp->max_processes)
1301 die("BUG: bookkeeping is hard");
1303 code = pp->get_next_task(&pp->children[i].process,
1304 &pp->children[i].err,
1305 pp->data,
1306 &pp->children[i].data);
1307 if (!code) {
1308 strbuf_addbuf(&pp->buffered_output, &pp->children[i].err);
1309 strbuf_reset(&pp->children[i].err);
1310 return 1;
1312 pp->children[i].process.err = -1;
1313 pp->children[i].process.stdout_to_stderr = 1;
1314 pp->children[i].process.no_stdin = 1;
1316 if (start_command(&pp->children[i].process)) {
1317 code = pp->start_failure(&pp->children[i].err,
1318 pp->data,
1319 &pp->children[i].data);
1320 strbuf_addbuf(&pp->buffered_output, &pp->children[i].err);
1321 strbuf_reset(&pp->children[i].err);
1322 if (code)
1323 pp->shutdown = 1;
1324 return code;
1327 pp->nr_processes++;
1328 pp->children[i].state = GIT_CP_WORKING;
1329 pp->pfd[i].fd = pp->children[i].process.err;
1330 return 0;
1333 static void pp_buffer_stderr(struct parallel_processes *pp, int output_timeout)
1335 int i;
1337 while ((i = poll(pp->pfd, pp->max_processes, output_timeout)) < 0) {
1338 if (errno == EINTR)
1339 continue;
1340 pp_cleanup(pp);
1341 die_errno("poll");
1344 /* Buffer output from all pipes. */
1345 for (i = 0; i < pp->max_processes; i++) {
1346 if (pp->children[i].state == GIT_CP_WORKING &&
1347 pp->pfd[i].revents & (POLLIN | POLLHUP)) {
1348 int n = strbuf_read_once(&pp->children[i].err,
1349 pp->children[i].process.err, 0);
1350 if (n == 0) {
1351 close(pp->children[i].process.err);
1352 pp->children[i].state = GIT_CP_WAIT_CLEANUP;
1353 } else if (n < 0)
1354 if (errno != EAGAIN)
1355 die_errno("read");
1360 static void pp_output(struct parallel_processes *pp)
1362 int i = pp->output_owner;
1363 if (pp->children[i].state == GIT_CP_WORKING &&
1364 pp->children[i].err.len) {
1365 strbuf_write(&pp->children[i].err, stderr);
1366 strbuf_reset(&pp->children[i].err);
1370 static int pp_collect_finished(struct parallel_processes *pp)
1372 int i, code;
1373 int n = pp->max_processes;
1374 int result = 0;
1376 while (pp->nr_processes > 0) {
1377 for (i = 0; i < pp->max_processes; i++)
1378 if (pp->children[i].state == GIT_CP_WAIT_CLEANUP)
1379 break;
1380 if (i == pp->max_processes)
1381 break;
1383 code = finish_command(&pp->children[i].process);
1385 code = pp->task_finished(code,
1386 &pp->children[i].err, pp->data,
1387 &pp->children[i].data);
1389 if (code)
1390 result = code;
1391 if (code < 0)
1392 break;
1394 pp->nr_processes--;
1395 pp->children[i].state = GIT_CP_FREE;
1396 pp->pfd[i].fd = -1;
1397 child_process_init(&pp->children[i].process);
1399 if (i != pp->output_owner) {
1400 strbuf_addbuf(&pp->buffered_output, &pp->children[i].err);
1401 strbuf_reset(&pp->children[i].err);
1402 } else {
1403 strbuf_write(&pp->children[i].err, stderr);
1404 strbuf_reset(&pp->children[i].err);
1406 /* Output all other finished child processes */
1407 strbuf_write(&pp->buffered_output, stderr);
1408 strbuf_reset(&pp->buffered_output);
1411 * Pick next process to output live.
1412 * NEEDSWORK:
1413 * For now we pick it randomly by doing a round
1414 * robin. Later we may want to pick the one with
1415 * the most output or the longest or shortest
1416 * running process time.
1418 for (i = 0; i < n; i++)
1419 if (pp->children[(pp->output_owner + i) % n].state == GIT_CP_WORKING)
1420 break;
1421 pp->output_owner = (pp->output_owner + i) % n;
1424 return result;
1427 int run_processes_parallel(int n,
1428 get_next_task_fn get_next_task,
1429 start_failure_fn start_failure,
1430 task_finished_fn task_finished,
1431 void *pp_cb)
1433 int i, code;
1434 int output_timeout = 100;
1435 int spawn_cap = 4;
1436 struct parallel_processes pp;
1438 pp_init(&pp, n, get_next_task, start_failure, task_finished, pp_cb);
1439 while (1) {
1440 for (i = 0;
1441 i < spawn_cap && !pp.shutdown &&
1442 pp.nr_processes < pp.max_processes;
1443 i++) {
1444 code = pp_start_one(&pp);
1445 if (!code)
1446 continue;
1447 if (code < 0) {
1448 pp.shutdown = 1;
1449 kill_children(&pp, -code);
1451 break;
1453 if (!pp.nr_processes)
1454 break;
1455 pp_buffer_stderr(&pp, output_timeout);
1456 pp_output(&pp);
1457 code = pp_collect_finished(&pp);
1458 if (code) {
1459 pp.shutdown = 1;
1460 if (code < 0)
1461 kill_children(&pp, -code);
1465 pp_cleanup(&pp);
1466 return 0;