Merge branch 'jk/filter-branch-no-index'
[git/gitweb.git] / run-command.c
blobcdf01845790849f863aef4195a68e12fa19ca543
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_to_clean *next;
26 static struct child_to_clean *children_to_clean;
27 static int installed_child_cleanup_handler;
29 static void cleanup_children(int sig, int in_signal)
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);
40 static void cleanup_children_on_signal(int sig)
42 cleanup_children(sig, 1);
43 sigchain_pop(sig);
44 raise(sig);
47 static void cleanup_children_on_exit(void)
49 cleanup_children(SIGTERM, 0);
52 static void mark_child_for_cleanup(pid_t pid)
54 struct child_to_clean *p = xmalloc(sizeof(*p));
55 p->pid = pid;
56 p->next = children_to_clean;
57 children_to_clean = p;
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;
66 static void clear_child_for_cleanup(pid_t pid)
68 struct child_to_clean **pp;
70 for (pp = &children_to_clean; *pp; pp = &(*pp)->next) {
71 struct child_to_clean *clean_me = *pp;
73 if (clean_me->pid == pid) {
74 *pp = clean_me->next;
75 free(clean_me);
76 return;
81 static inline void close_pair(int fd[2])
83 close(fd[0]);
84 close(fd[1]);
87 #ifndef GIT_WINDOWS_NATIVE
88 static inline void dup_devnull(int to)
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);
97 #endif
99 static char *locate_in_PATH(const char *file)
101 const char *p = getenv("PATH");
102 struct strbuf buf = STRBUF_INIT;
104 if (!p || !*p)
105 return NULL;
107 while (1) {
108 const char *end = strchrnul(p, ':');
110 strbuf_reset(&buf);
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, '/');
117 strbuf_addstr(&buf, file);
119 if (!access(buf.buf, F_OK))
120 return strbuf_detach(&buf, NULL);
122 if (!*end)
123 break;
124 p = end + 1;
127 strbuf_release(&buf);
128 return NULL;
131 static int exists_in_PATH(const char *file)
133 char *r = locate_in_PATH(file);
134 free(r);
135 return r != NULL;
138 int sane_execvp(const char *file, char * const argv[])
140 if (!execvp(file, argv))
141 return 0; /* cannot happen ;-) */
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.
150 * We avoid commands with "/", because execvp will not do $PATH
151 * lookups in that case.
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.
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;
163 static const char **prepare_shell_cmd(const char **argv)
165 int argc, nargc = 0;
166 const char **nargv;
168 for (argc = 0; argv[argc]; argc++)
169 ; /* just counting */
170 /* +1 for NULL, +3 for "sh -c" plus extra $0 */
171 nargv = xmalloc(sizeof(*nargv) * (argc + 1 + 3));
173 if (argc < 1)
174 die("BUG: shell command is empty");
176 if (strcspn(argv[0], "|&;<>()$`\\\"' \t\n*?[#~=%") != strlen(argv[0])) {
177 #ifndef GIT_WINDOWS_NATIVE
178 nargv[nargc++] = SHELL_PATH;
179 #else
180 nargv[nargc++] = "sh";
181 #endif
182 nargv[nargc++] = "-c";
184 if (argc < 2)
185 nargv[nargc++] = argv[0];
186 else {
187 struct strbuf arg0 = STRBUF_INIT;
188 strbuf_addf(&arg0, "%s \"$@\"", argv[0]);
189 nargv[nargc++] = strbuf_detach(&arg0, NULL);
193 for (argc = 0; argv[argc]; argc++)
194 nargv[nargc++] = argv[argc];
195 nargv[nargc] = NULL;
197 return nargv;
200 #ifndef GIT_WINDOWS_NATIVE
201 static int execv_shell_cmd(const char **argv)
203 const char **nargv = prepare_shell_cmd(argv);
204 trace_argv_printf(nargv, "trace: exec:");
205 sane_execvp(nargv[0], (char **)nargv);
206 free(nargv);
207 return -1;
209 #endif
211 #ifndef GIT_WINDOWS_NATIVE
212 static int child_notifier = -1;
214 static void notify_parent(void)
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.
221 xwrite(child_notifier, "", 1);
223 #endif
225 static inline void set_cloexec(int fd)
227 int flags = fcntl(fd, F_GETFD);
228 if (flags >= 0)
229 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
232 static int wait_or_whine(pid_t pid, const char *argv0, int in_signal)
234 int status, code = -1;
235 pid_t waiting;
236 int failed_errno = 0;
238 while ((waiting = waitpid(pid, &status, 0)) < 0 && errno == EINTR)
239 ; /* nothing */
240 if (in_signal)
241 return 0;
243 if (waiting < 0) {
244 failed_errno = errno;
245 error("waitpid for %s failed: %s", argv0, strerror(errno));
246 } else if (waiting != pid) {
247 error("waitpid is confused (%s)", argv0);
248 } else if (WIFSIGNALED(status)) {
249 code = WTERMSIG(status);
250 if (code != SIGINT && code != SIGQUIT && code != SIGPIPE)
251 error("%s died of signal %d", argv0, code);
253 * This return value is chosen so that code & 0xff
254 * mimics the exit code that a POSIX shell would report for
255 * a program that died from this signal.
257 code += 128;
258 } else if (WIFEXITED(status)) {
259 code = WEXITSTATUS(status);
261 * Convert special exit code when execvp failed.
263 if (code == 127) {
264 code = -1;
265 failed_errno = ENOENT;
267 } else {
268 error("waitpid is confused (%s)", argv0);
271 clear_child_for_cleanup(pid);
273 errno = failed_errno;
274 return code;
277 int start_command(struct child_process *cmd)
279 int need_in, need_out, need_err;
280 int fdin[2], fdout[2], fderr[2];
281 int failed_errno;
282 char *str;
284 if (!cmd->argv)
285 cmd->argv = cmd->args.argv;
286 if (!cmd->env)
287 cmd->env = cmd->env_array.argv;
290 * In case of errors we must keep the promise to close FDs
291 * that have been passed in via ->in and ->out.
294 need_in = !cmd->no_stdin && cmd->in < 0;
295 if (need_in) {
296 if (pipe(fdin) < 0) {
297 failed_errno = errno;
298 if (cmd->out > 0)
299 close(cmd->out);
300 str = "standard input";
301 goto fail_pipe;
303 cmd->in = fdin[1];
306 need_out = !cmd->no_stdout
307 && !cmd->stdout_to_stderr
308 && cmd->out < 0;
309 if (need_out) {
310 if (pipe(fdout) < 0) {
311 failed_errno = errno;
312 if (need_in)
313 close_pair(fdin);
314 else if (cmd->in)
315 close(cmd->in);
316 str = "standard output";
317 goto fail_pipe;
319 cmd->out = fdout[0];
322 need_err = !cmd->no_stderr && cmd->err < 0;
323 if (need_err) {
324 if (pipe(fderr) < 0) {
325 failed_errno = errno;
326 if (need_in)
327 close_pair(fdin);
328 else if (cmd->in)
329 close(cmd->in);
330 if (need_out)
331 close_pair(fdout);
332 else if (cmd->out)
333 close(cmd->out);
334 str = "standard error";
335 fail_pipe:
336 error("cannot create %s pipe for %s: %s",
337 str, cmd->argv[0], strerror(failed_errno));
338 child_process_clear(cmd);
339 errno = failed_errno;
340 return -1;
342 cmd->err = fderr[0];
345 trace_argv_printf(cmd->argv, "trace: run_command:");
346 fflush(NULL);
348 #ifndef GIT_WINDOWS_NATIVE
350 int notify_pipe[2];
351 if (pipe(notify_pipe))
352 notify_pipe[0] = notify_pipe[1] = -1;
354 cmd->pid = fork();
355 failed_errno = errno;
356 if (!cmd->pid) {
358 * Redirect the channel to write syscall error messages to
359 * before redirecting the process's stderr so that all die()
360 * in subsequent call paths use the parent's stderr.
362 if (cmd->no_stderr || need_err) {
363 int child_err = dup(2);
364 set_cloexec(child_err);
365 set_error_handle(fdopen(child_err, "w"));
368 close(notify_pipe[0]);
369 set_cloexec(notify_pipe[1]);
370 child_notifier = notify_pipe[1];
371 atexit(notify_parent);
373 if (cmd->no_stdin)
374 dup_devnull(0);
375 else if (need_in) {
376 dup2(fdin[0], 0);
377 close_pair(fdin);
378 } else if (cmd->in) {
379 dup2(cmd->in, 0);
380 close(cmd->in);
383 if (cmd->no_stderr)
384 dup_devnull(2);
385 else if (need_err) {
386 dup2(fderr[1], 2);
387 close_pair(fderr);
388 } else if (cmd->err > 1) {
389 dup2(cmd->err, 2);
390 close(cmd->err);
393 if (cmd->no_stdout)
394 dup_devnull(1);
395 else if (cmd->stdout_to_stderr)
396 dup2(2, 1);
397 else if (need_out) {
398 dup2(fdout[1], 1);
399 close_pair(fdout);
400 } else if (cmd->out > 1) {
401 dup2(cmd->out, 1);
402 close(cmd->out);
405 if (cmd->dir && chdir(cmd->dir))
406 die_errno("exec '%s': cd to '%s' failed", cmd->argv[0],
407 cmd->dir);
408 if (cmd->env) {
409 for (; *cmd->env; cmd->env++) {
410 if (strchr(*cmd->env, '='))
411 putenv((char *)*cmd->env);
412 else
413 unsetenv(*cmd->env);
416 if (cmd->git_cmd)
417 execv_git_cmd(cmd->argv);
418 else if (cmd->use_shell)
419 execv_shell_cmd(cmd->argv);
420 else
421 sane_execvp(cmd->argv[0], (char *const*) cmd->argv);
422 if (errno == ENOENT) {
423 if (!cmd->silent_exec_failure)
424 error("cannot run %s: %s", cmd->argv[0],
425 strerror(ENOENT));
426 exit(127);
427 } else {
428 die_errno("cannot exec '%s'", cmd->argv[0]);
431 if (cmd->pid < 0)
432 error("cannot fork() for %s: %s", cmd->argv[0],
433 strerror(errno));
434 else if (cmd->clean_on_exit)
435 mark_child_for_cleanup(cmd->pid);
438 * Wait for child's execvp. If the execvp succeeds (or if fork()
439 * failed), EOF is seen immediately by the parent. Otherwise, the
440 * child process sends a single byte.
441 * Note that use of this infrastructure is completely advisory,
442 * therefore, we keep error checks minimal.
444 close(notify_pipe[1]);
445 if (read(notify_pipe[0], &notify_pipe[1], 1) == 1) {
447 * At this point we know that fork() succeeded, but execvp()
448 * failed. Errors have been reported to our stderr.
450 wait_or_whine(cmd->pid, cmd->argv[0], 0);
451 failed_errno = errno;
452 cmd->pid = -1;
454 close(notify_pipe[0]);
456 #else
458 int fhin = 0, fhout = 1, fherr = 2;
459 const char **sargv = cmd->argv;
461 if (cmd->no_stdin)
462 fhin = open("/dev/null", O_RDWR);
463 else if (need_in)
464 fhin = dup(fdin[0]);
465 else if (cmd->in)
466 fhin = dup(cmd->in);
468 if (cmd->no_stderr)
469 fherr = open("/dev/null", O_RDWR);
470 else if (need_err)
471 fherr = dup(fderr[1]);
472 else if (cmd->err > 2)
473 fherr = dup(cmd->err);
475 if (cmd->no_stdout)
476 fhout = open("/dev/null", O_RDWR);
477 else if (cmd->stdout_to_stderr)
478 fhout = dup(fherr);
479 else if (need_out)
480 fhout = dup(fdout[1]);
481 else if (cmd->out > 1)
482 fhout = dup(cmd->out);
484 if (cmd->git_cmd)
485 cmd->argv = prepare_git_cmd(cmd->argv);
486 else if (cmd->use_shell)
487 cmd->argv = prepare_shell_cmd(cmd->argv);
489 cmd->pid = mingw_spawnvpe(cmd->argv[0], cmd->argv, (char**) cmd->env,
490 cmd->dir, fhin, fhout, fherr);
491 failed_errno = errno;
492 if (cmd->pid < 0 && (!cmd->silent_exec_failure || errno != ENOENT))
493 error("cannot spawn %s: %s", cmd->argv[0], strerror(errno));
494 if (cmd->clean_on_exit && cmd->pid >= 0)
495 mark_child_for_cleanup(cmd->pid);
497 if (cmd->git_cmd)
498 free(cmd->argv);
500 cmd->argv = sargv;
501 if (fhin != 0)
502 close(fhin);
503 if (fhout != 1)
504 close(fhout);
505 if (fherr != 2)
506 close(fherr);
508 #endif
510 if (cmd->pid < 0) {
511 if (need_in)
512 close_pair(fdin);
513 else if (cmd->in)
514 close(cmd->in);
515 if (need_out)
516 close_pair(fdout);
517 else if (cmd->out)
518 close(cmd->out);
519 if (need_err)
520 close_pair(fderr);
521 else if (cmd->err)
522 close(cmd->err);
523 child_process_clear(cmd);
524 errno = failed_errno;
525 return -1;
528 if (need_in)
529 close(fdin[0]);
530 else if (cmd->in)
531 close(cmd->in);
533 if (need_out)
534 close(fdout[1]);
535 else if (cmd->out)
536 close(cmd->out);
538 if (need_err)
539 close(fderr[1]);
540 else if (cmd->err)
541 close(cmd->err);
543 return 0;
546 int finish_command(struct child_process *cmd)
548 int ret = wait_or_whine(cmd->pid, cmd->argv[0], 0);
549 child_process_clear(cmd);
550 return ret;
553 int finish_command_in_signal(struct child_process *cmd)
555 return wait_or_whine(cmd->pid, cmd->argv[0], 1);
559 int run_command(struct child_process *cmd)
561 int code;
563 if (cmd->out < 0 || cmd->err < 0)
564 die("BUG: run_command with a pipe can cause deadlock");
566 code = start_command(cmd);
567 if (code)
568 return code;
569 return finish_command(cmd);
572 int run_command_v_opt(const char **argv, int opt)
574 return run_command_v_opt_cd_env(argv, opt, NULL, NULL);
577 int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env)
579 struct child_process cmd = CHILD_PROCESS_INIT;
580 cmd.argv = argv;
581 cmd.no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
582 cmd.git_cmd = opt & RUN_GIT_CMD ? 1 : 0;
583 cmd.stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
584 cmd.silent_exec_failure = opt & RUN_SILENT_EXEC_FAILURE ? 1 : 0;
585 cmd.use_shell = opt & RUN_USING_SHELL ? 1 : 0;
586 cmd.clean_on_exit = opt & RUN_CLEAN_ON_EXIT ? 1 : 0;
587 cmd.dir = dir;
588 cmd.env = env;
589 return run_command(&cmd);
592 #ifndef NO_PTHREADS
593 static pthread_t main_thread;
594 static int main_thread_set;
595 static pthread_key_t async_key;
596 static pthread_key_t async_die_counter;
598 static void *run_thread(void *data)
600 struct async *async = data;
601 intptr_t ret;
603 pthread_setspecific(async_key, async);
604 ret = async->proc(async->proc_in, async->proc_out, async->data);
605 return (void *)ret;
608 static NORETURN void die_async(const char *err, va_list params)
610 vreportf("fatal: ", err, params);
612 if (in_async()) {
613 struct async *async = pthread_getspecific(async_key);
614 if (async->proc_in >= 0)
615 close(async->proc_in);
616 if (async->proc_out >= 0)
617 close(async->proc_out);
618 pthread_exit((void *)128);
621 exit(128);
624 static int async_die_is_recursing(void)
626 void *ret = pthread_getspecific(async_die_counter);
627 pthread_setspecific(async_die_counter, (void *)1);
628 return ret != NULL;
631 int in_async(void)
633 if (!main_thread_set)
634 return 0; /* no asyncs started yet */
635 return !pthread_equal(main_thread, pthread_self());
638 #else
640 static struct {
641 void (**handlers)(void);
642 size_t nr;
643 size_t alloc;
644 } git_atexit_hdlrs;
646 static int git_atexit_installed;
648 static void git_atexit_dispatch(void)
650 size_t i;
652 for (i=git_atexit_hdlrs.nr ; i ; i--)
653 git_atexit_hdlrs.handlers[i-1]();
656 static void git_atexit_clear(void)
658 free(git_atexit_hdlrs.handlers);
659 memset(&git_atexit_hdlrs, 0, sizeof(git_atexit_hdlrs));
660 git_atexit_installed = 0;
663 #undef atexit
664 int git_atexit(void (*handler)(void))
666 ALLOC_GROW(git_atexit_hdlrs.handlers, git_atexit_hdlrs.nr + 1, git_atexit_hdlrs.alloc);
667 git_atexit_hdlrs.handlers[git_atexit_hdlrs.nr++] = handler;
668 if (!git_atexit_installed) {
669 if (atexit(&git_atexit_dispatch))
670 return -1;
671 git_atexit_installed = 1;
673 return 0;
675 #define atexit git_atexit
677 static int process_is_async;
678 int in_async(void)
680 return process_is_async;
683 #endif
685 int start_async(struct async *async)
687 int need_in, need_out;
688 int fdin[2], fdout[2];
689 int proc_in, proc_out;
691 need_in = async->in < 0;
692 if (need_in) {
693 if (pipe(fdin) < 0) {
694 if (async->out > 0)
695 close(async->out);
696 return error("cannot create pipe: %s", strerror(errno));
698 async->in = fdin[1];
701 need_out = async->out < 0;
702 if (need_out) {
703 if (pipe(fdout) < 0) {
704 if (need_in)
705 close_pair(fdin);
706 else if (async->in)
707 close(async->in);
708 return error("cannot create pipe: %s", strerror(errno));
710 async->out = fdout[0];
713 if (need_in)
714 proc_in = fdin[0];
715 else if (async->in)
716 proc_in = async->in;
717 else
718 proc_in = -1;
720 if (need_out)
721 proc_out = fdout[1];
722 else if (async->out)
723 proc_out = async->out;
724 else
725 proc_out = -1;
727 #ifdef NO_PTHREADS
728 /* Flush stdio before fork() to avoid cloning buffers */
729 fflush(NULL);
731 async->pid = fork();
732 if (async->pid < 0) {
733 error("fork (async) failed: %s", strerror(errno));
734 goto error;
736 if (!async->pid) {
737 if (need_in)
738 close(fdin[1]);
739 if (need_out)
740 close(fdout[0]);
741 git_atexit_clear();
742 process_is_async = 1;
743 exit(!!async->proc(proc_in, proc_out, async->data));
746 mark_child_for_cleanup(async->pid);
748 if (need_in)
749 close(fdin[0]);
750 else if (async->in)
751 close(async->in);
753 if (need_out)
754 close(fdout[1]);
755 else if (async->out)
756 close(async->out);
757 #else
758 if (!main_thread_set) {
760 * We assume that the first time that start_async is called
761 * it is from the main thread.
763 main_thread_set = 1;
764 main_thread = pthread_self();
765 pthread_key_create(&async_key, NULL);
766 pthread_key_create(&async_die_counter, NULL);
767 set_die_routine(die_async);
768 set_die_is_recursing_routine(async_die_is_recursing);
771 if (proc_in >= 0)
772 set_cloexec(proc_in);
773 if (proc_out >= 0)
774 set_cloexec(proc_out);
775 async->proc_in = proc_in;
776 async->proc_out = proc_out;
778 int err = pthread_create(&async->tid, NULL, run_thread, async);
779 if (err) {
780 error("cannot create thread: %s", strerror(err));
781 goto error;
784 #endif
785 return 0;
787 error:
788 if (need_in)
789 close_pair(fdin);
790 else if (async->in)
791 close(async->in);
793 if (need_out)
794 close_pair(fdout);
795 else if (async->out)
796 close(async->out);
797 return -1;
800 int finish_async(struct async *async)
802 #ifdef NO_PTHREADS
803 return wait_or_whine(async->pid, "child process", 0);
804 #else
805 void *ret = (void *)(intptr_t)(-1);
807 if (pthread_join(async->tid, &ret))
808 error("pthread_join failed");
809 return (int)(intptr_t)ret;
810 #endif
813 const char *find_hook(const char *name)
815 static struct strbuf path = STRBUF_INIT;
817 strbuf_reset(&path);
818 strbuf_git_path(&path, "hooks/%s", name);
819 if (access(path.buf, X_OK) < 0)
820 return NULL;
821 return path.buf;
824 int run_hook_ve(const char *const *env, const char *name, va_list args)
826 struct child_process hook = CHILD_PROCESS_INIT;
827 const char *p;
829 p = find_hook(name);
830 if (!p)
831 return 0;
833 argv_array_push(&hook.args, p);
834 while ((p = va_arg(args, const char *)))
835 argv_array_push(&hook.args, p);
836 hook.env = env;
837 hook.no_stdin = 1;
838 hook.stdout_to_stderr = 1;
840 return run_command(&hook);
843 int run_hook_le(const char *const *env, const char *name, ...)
845 va_list args;
846 int ret;
848 va_start(args, name);
849 ret = run_hook_ve(env, name, args);
850 va_end(args);
852 return ret;
855 int capture_command(struct child_process *cmd, struct strbuf *buf, size_t hint)
857 cmd->out = -1;
858 if (start_command(cmd) < 0)
859 return -1;
861 if (strbuf_read(buf, cmd->out, hint) < 0) {
862 close(cmd->out);
863 finish_command(cmd); /* throw away exit code */
864 return -1;
867 close(cmd->out);
868 return finish_command(cmd);
871 enum child_state {
872 GIT_CP_FREE,
873 GIT_CP_WORKING,
874 GIT_CP_WAIT_CLEANUP,
877 struct parallel_processes {
878 void *data;
880 int max_processes;
881 int nr_processes;
883 get_next_task_fn get_next_task;
884 start_failure_fn start_failure;
885 task_finished_fn task_finished;
887 struct {
888 enum child_state state;
889 struct child_process process;
890 struct strbuf err;
891 void *data;
892 } *children;
894 * The struct pollfd is logically part of *children,
895 * but the system call expects it as its own array.
897 struct pollfd *pfd;
899 unsigned shutdown : 1;
901 int output_owner;
902 struct strbuf buffered_output; /* of finished children */
905 static int default_start_failure(struct child_process *cp,
906 struct strbuf *err,
907 void *pp_cb,
908 void *pp_task_cb)
910 int i;
912 strbuf_addstr(err, "Starting a child failed:");
913 for (i = 0; cp->argv[i]; i++)
914 strbuf_addf(err, " %s", cp->argv[i]);
916 return 0;
919 static int default_task_finished(int result,
920 struct child_process *cp,
921 struct strbuf *err,
922 void *pp_cb,
923 void *pp_task_cb)
925 int i;
927 if (!result)
928 return 0;
930 strbuf_addf(err, "A child failed with return code %d:", result);
931 for (i = 0; cp->argv[i]; i++)
932 strbuf_addf(err, " %s", cp->argv[i]);
934 return 0;
937 static void kill_children(struct parallel_processes *pp, int signo)
939 int i, n = pp->max_processes;
941 for (i = 0; i < n; i++)
942 if (pp->children[i].state == GIT_CP_WORKING)
943 kill(pp->children[i].process.pid, signo);
946 static struct parallel_processes *pp_for_signal;
948 static void handle_children_on_signal(int signo)
950 kill_children(pp_for_signal, signo);
951 sigchain_pop(signo);
952 raise(signo);
955 static void pp_init(struct parallel_processes *pp,
956 int n,
957 get_next_task_fn get_next_task,
958 start_failure_fn start_failure,
959 task_finished_fn task_finished,
960 void *data)
962 int i;
964 if (n < 1)
965 n = online_cpus();
967 pp->max_processes = n;
969 trace_printf("run_processes_parallel: preparing to run up to %d tasks", n);
971 pp->data = data;
972 if (!get_next_task)
973 die("BUG: you need to specify a get_next_task function");
974 pp->get_next_task = get_next_task;
976 pp->start_failure = start_failure ? start_failure : default_start_failure;
977 pp->task_finished = task_finished ? task_finished : default_task_finished;
979 pp->nr_processes = 0;
980 pp->output_owner = 0;
981 pp->shutdown = 0;
982 pp->children = xcalloc(n, sizeof(*pp->children));
983 pp->pfd = xcalloc(n, sizeof(*pp->pfd));
984 strbuf_init(&pp->buffered_output, 0);
986 for (i = 0; i < n; i++) {
987 strbuf_init(&pp->children[i].err, 0);
988 child_process_init(&pp->children[i].process);
989 pp->pfd[i].events = POLLIN | POLLHUP;
990 pp->pfd[i].fd = -1;
993 pp_for_signal = pp;
994 sigchain_push_common(handle_children_on_signal);
997 static void pp_cleanup(struct parallel_processes *pp)
999 int i;
1001 trace_printf("run_processes_parallel: done");
1002 for (i = 0; i < pp->max_processes; i++) {
1003 strbuf_release(&pp->children[i].err);
1004 child_process_clear(&pp->children[i].process);
1007 free(pp->children);
1008 free(pp->pfd);
1011 * When get_next_task added messages to the buffer in its last
1012 * iteration, the buffered output is non empty.
1014 fputs(pp->buffered_output.buf, stderr);
1015 strbuf_release(&pp->buffered_output);
1017 sigchain_pop_common();
1020 /* returns
1021 * 0 if a new task was started.
1022 * 1 if no new jobs was started (get_next_task ran out of work, non critical
1023 * problem with starting a new command)
1024 * <0 no new job was started, user wishes to shutdown early. Use negative code
1025 * to signal the children.
1027 static int pp_start_one(struct parallel_processes *pp)
1029 int i, code;
1031 for (i = 0; i < pp->max_processes; i++)
1032 if (pp->children[i].state == GIT_CP_FREE)
1033 break;
1034 if (i == pp->max_processes)
1035 die("BUG: bookkeeping is hard");
1037 code = pp->get_next_task(&pp->children[i].process,
1038 &pp->children[i].err,
1039 pp->data,
1040 &pp->children[i].data);
1041 if (!code) {
1042 strbuf_addbuf(&pp->buffered_output, &pp->children[i].err);
1043 strbuf_reset(&pp->children[i].err);
1044 return 1;
1046 pp->children[i].process.err = -1;
1047 pp->children[i].process.stdout_to_stderr = 1;
1048 pp->children[i].process.no_stdin = 1;
1050 if (start_command(&pp->children[i].process)) {
1051 code = pp->start_failure(&pp->children[i].process,
1052 &pp->children[i].err,
1053 pp->data,
1054 &pp->children[i].data);
1055 strbuf_addbuf(&pp->buffered_output, &pp->children[i].err);
1056 strbuf_reset(&pp->children[i].err);
1057 if (code)
1058 pp->shutdown = 1;
1059 return code;
1062 pp->nr_processes++;
1063 pp->children[i].state = GIT_CP_WORKING;
1064 pp->pfd[i].fd = pp->children[i].process.err;
1065 return 0;
1068 static void pp_buffer_stderr(struct parallel_processes *pp, int output_timeout)
1070 int i;
1072 while ((i = poll(pp->pfd, pp->max_processes, output_timeout)) < 0) {
1073 if (errno == EINTR)
1074 continue;
1075 pp_cleanup(pp);
1076 die_errno("poll");
1079 /* Buffer output from all pipes. */
1080 for (i = 0; i < pp->max_processes; i++) {
1081 if (pp->children[i].state == GIT_CP_WORKING &&
1082 pp->pfd[i].revents & (POLLIN | POLLHUP)) {
1083 int n = strbuf_read_once(&pp->children[i].err,
1084 pp->children[i].process.err, 0);
1085 if (n == 0) {
1086 close(pp->children[i].process.err);
1087 pp->children[i].state = GIT_CP_WAIT_CLEANUP;
1088 } else if (n < 0)
1089 if (errno != EAGAIN)
1090 die_errno("read");
1095 static void pp_output(struct parallel_processes *pp)
1097 int i = pp->output_owner;
1098 if (pp->children[i].state == GIT_CP_WORKING &&
1099 pp->children[i].err.len) {
1100 fputs(pp->children[i].err.buf, stderr);
1101 strbuf_reset(&pp->children[i].err);
1105 static int pp_collect_finished(struct parallel_processes *pp)
1107 int i, code;
1108 int n = pp->max_processes;
1109 int result = 0;
1111 while (pp->nr_processes > 0) {
1112 for (i = 0; i < pp->max_processes; i++)
1113 if (pp->children[i].state == GIT_CP_WAIT_CLEANUP)
1114 break;
1115 if (i == pp->max_processes)
1116 break;
1118 code = finish_command(&pp->children[i].process);
1120 code = pp->task_finished(code, &pp->children[i].process,
1121 &pp->children[i].err, pp->data,
1122 &pp->children[i].data);
1124 if (code)
1125 result = code;
1126 if (code < 0)
1127 break;
1129 pp->nr_processes--;
1130 pp->children[i].state = GIT_CP_FREE;
1131 pp->pfd[i].fd = -1;
1132 child_process_init(&pp->children[i].process);
1134 if (i != pp->output_owner) {
1135 strbuf_addbuf(&pp->buffered_output, &pp->children[i].err);
1136 strbuf_reset(&pp->children[i].err);
1137 } else {
1138 fputs(pp->children[i].err.buf, stderr);
1139 strbuf_reset(&pp->children[i].err);
1141 /* Output all other finished child processes */
1142 fputs(pp->buffered_output.buf, stderr);
1143 strbuf_reset(&pp->buffered_output);
1146 * Pick next process to output live.
1147 * NEEDSWORK:
1148 * For now we pick it randomly by doing a round
1149 * robin. Later we may want to pick the one with
1150 * the most output or the longest or shortest
1151 * running process time.
1153 for (i = 0; i < n; i++)
1154 if (pp->children[(pp->output_owner + i) % n].state == GIT_CP_WORKING)
1155 break;
1156 pp->output_owner = (pp->output_owner + i) % n;
1159 return result;
1162 int run_processes_parallel(int n,
1163 get_next_task_fn get_next_task,
1164 start_failure_fn start_failure,
1165 task_finished_fn task_finished,
1166 void *pp_cb)
1168 int i, code;
1169 int output_timeout = 100;
1170 int spawn_cap = 4;
1171 struct parallel_processes pp;
1173 pp_init(&pp, n, get_next_task, start_failure, task_finished, pp_cb);
1174 while (1) {
1175 for (i = 0;
1176 i < spawn_cap && !pp.shutdown &&
1177 pp.nr_processes < pp.max_processes;
1178 i++) {
1179 code = pp_start_one(&pp);
1180 if (!code)
1181 continue;
1182 if (code < 0) {
1183 pp.shutdown = 1;
1184 kill_children(&pp, -code);
1186 break;
1188 if (!pp.nr_processes)
1189 break;
1190 pp_buffer_stderr(&pp, output_timeout);
1191 pp_output(&pp);
1192 code = pp_collect_finished(&pp);
1193 if (code) {
1194 pp.shutdown = 1;
1195 if (code < 0)
1196 kill_children(&pp, -code);
1200 pp_cleanup(&pp);
1201 return 0;