2 #include "run-command.h"
5 #include "argv-array.h"
6 #include "thread-utils.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 {
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 while (children_to_clean) {
33 struct child_to_clean *p = children_to_clean;
34 children_to_clean = p->next;
36 if (p->process && !in_signal) {
37 struct child_process *process = p->process;
38 if (process->clean_on_exit_handler) {
40 "trace: run_command: running exit handler for pid %"
41 PRIuMAX, (uintmax_t)p->pid
43 process->clean_on_exit_handler(process);
53 static void cleanup_children_on_signal(int sig)
55 cleanup_children(sig, 1);
60 static void cleanup_children_on_exit(void)
62 cleanup_children(SIGTERM, 0);
65 static void mark_child_for_cleanup(pid_t pid, struct child_process *process)
67 struct child_to_clean *p = xmalloc(sizeof(*p));
70 p->next = children_to_clean;
71 children_to_clean = p;
73 if (!installed_child_cleanup_handler) {
74 atexit(cleanup_children_on_exit);
75 sigchain_push_common(cleanup_children_on_signal);
76 installed_child_cleanup_handler = 1;
80 static void clear_child_for_cleanup(pid_t pid)
82 struct child_to_clean **pp;
84 for (pp = &children_to_clean; *pp; pp = &(*pp)->next) {
85 struct child_to_clean *clean_me = *pp;
87 if (clean_me->pid == pid) {
95 static inline void close_pair(int fd[2])
101 #ifndef GIT_WINDOWS_NATIVE
102 static inline void dup_devnull(int to)
104 int fd = open("/dev/null", O_RDWR);
106 die_errno(_("open /dev/null failed"));
107 if (dup2(fd, to) < 0)
108 die_errno(_("dup2(%d,%d) failed"), fd, to);
113 static char *locate_in_PATH(const char *file)
115 const char *p = getenv("PATH");
116 struct strbuf buf = STRBUF_INIT;
122 const char *end = strchrnul(p, ':');
126 /* POSIX specifies an empty entry as the current directory. */
128 strbuf_add(&buf, p, end - p);
129 strbuf_addch(&buf, '/');
131 strbuf_addstr(&buf, file);
133 if (!access(buf.buf, F_OK))
134 return strbuf_detach(&buf, NULL);
141 strbuf_release(&buf);
145 static int exists_in_PATH(const char *file)
147 char *r = locate_in_PATH(file);
152 int sane_execvp(const char *file, char * const argv[])
154 if (!execvp(file, argv))
155 return 0; /* cannot happen ;-) */
158 * When a command can't be found because one of the directories
159 * listed in $PATH is unsearchable, execvp reports EACCES, but
160 * careful usability testing (read: analysis of occasional bug
161 * reports) reveals that "No such file or directory" is more
164 * We avoid commands with "/", because execvp will not do $PATH
165 * lookups in that case.
167 * The reassignment of EACCES to errno looks like a no-op below,
168 * but we need to protect against exists_in_PATH overwriting errno.
170 if (errno == EACCES && !strchr(file, '/'))
171 errno = exists_in_PATH(file) ? EACCES : ENOENT;
172 else if (errno == ENOTDIR && !strchr(file, '/'))
177 static const char **prepare_shell_cmd(struct argv_array *out, const char **argv)
180 die("BUG: shell command is empty");
182 if (strcspn(argv[0], "|&;<>()$`\\\"' \t\n*?[#~=%") != strlen(argv[0])) {
183 #ifndef GIT_WINDOWS_NATIVE
184 argv_array_push(out, SHELL_PATH);
186 argv_array_push(out, "sh");
188 argv_array_push(out, "-c");
191 * If we have no extra arguments, we do not even need to
192 * bother with the "$@" magic.
195 argv_array_push(out, argv[0]);
197 argv_array_pushf(out, "%s \"$@\"", argv[0]);
200 argv_array_pushv(out, argv);
204 #ifndef GIT_WINDOWS_NATIVE
205 static int execv_shell_cmd(const char **argv)
207 struct argv_array nargv = ARGV_ARRAY_INIT;
208 prepare_shell_cmd(&nargv, argv);
209 trace_argv_printf(nargv.argv, "trace: exec:");
210 sane_execvp(nargv.argv[0], (char **)nargv.argv);
211 argv_array_clear(&nargv);
216 #ifndef GIT_WINDOWS_NATIVE
217 static int child_notifier = -1;
219 static void notify_parent(void)
222 * execvp failed. If possible, we'd like to let start_command
223 * know, so failures like ENOENT can be handled right away; but
224 * otherwise, finish_command will still report the error.
226 xwrite(child_notifier, "", 1);
230 static inline void set_cloexec(int fd)
232 int flags = fcntl(fd, F_GETFD);
234 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
237 static int wait_or_whine(pid_t pid, const char *argv0, int in_signal)
239 int status, code = -1;
241 int failed_errno = 0;
243 while ((waiting = waitpid(pid, &status, 0)) < 0 && errno == EINTR)
249 failed_errno = errno;
250 error_errno("waitpid for %s failed", argv0);
251 } else if (waiting != pid) {
252 error("waitpid is confused (%s)", argv0);
253 } else if (WIFSIGNALED(status)) {
254 code = WTERMSIG(status);
255 if (code != SIGINT && code != SIGQUIT && code != SIGPIPE)
256 error("%s died of signal %d", argv0, code);
258 * This return value is chosen so that code & 0xff
259 * mimics the exit code that a POSIX shell would report for
260 * a program that died from this signal.
263 } else if (WIFEXITED(status)) {
264 code = WEXITSTATUS(status);
266 * Convert special exit code when execvp failed.
270 failed_errno = ENOENT;
273 error("waitpid is confused (%s)", argv0);
276 clear_child_for_cleanup(pid);
278 errno = failed_errno;
282 int start_command(struct child_process *cmd)
284 int need_in, need_out, need_err;
285 int fdin[2], fdout[2], fderr[2];
290 cmd->argv = cmd->args.argv;
292 cmd->env = cmd->env_array.argv;
295 * In case of errors we must keep the promise to close FDs
296 * that have been passed in via ->in and ->out.
299 need_in = !cmd->no_stdin && cmd->in < 0;
301 if (pipe(fdin) < 0) {
302 failed_errno = errno;
305 str = "standard input";
311 need_out = !cmd->no_stdout
312 && !cmd->stdout_to_stderr
315 if (pipe(fdout) < 0) {
316 failed_errno = errno;
321 str = "standard output";
327 need_err = !cmd->no_stderr && cmd->err < 0;
329 if (pipe(fderr) < 0) {
330 failed_errno = errno;
339 str = "standard error";
341 error("cannot create %s pipe for %s: %s",
342 str, cmd->argv[0], strerror(failed_errno));
343 child_process_clear(cmd);
344 errno = failed_errno;
350 trace_argv_printf(cmd->argv, "trace: run_command:");
353 #ifndef GIT_WINDOWS_NATIVE
356 if (pipe(notify_pipe))
357 notify_pipe[0] = notify_pipe[1] = -1;
360 failed_errno = errno;
363 * Redirect the channel to write syscall error messages to
364 * before redirecting the process's stderr so that all die()
365 * in subsequent call paths use the parent's stderr.
367 if (cmd->no_stderr || need_err) {
368 int child_err = dup(2);
369 set_cloexec(child_err);
370 set_error_handle(fdopen(child_err, "w"));
373 close(notify_pipe[0]);
374 set_cloexec(notify_pipe[1]);
375 child_notifier = notify_pipe[1];
376 atexit(notify_parent);
383 } else if (cmd->in) {
393 } else if (cmd->err > 1) {
400 else if (cmd->stdout_to_stderr)
405 } else if (cmd->out > 1) {
410 if (cmd->dir && chdir(cmd->dir))
411 die_errno("exec '%s': cd to '%s' failed", cmd->argv[0],
414 for (; *cmd->env; cmd->env++) {
415 if (strchr(*cmd->env, '='))
416 putenv((char *)*cmd->env);
422 execv_git_cmd(cmd->argv);
423 else if (cmd->use_shell)
424 execv_shell_cmd(cmd->argv);
426 sane_execvp(cmd->argv[0], (char *const*) cmd->argv);
427 if (errno == ENOENT) {
428 if (!cmd->silent_exec_failure)
429 error("cannot run %s: %s", cmd->argv[0],
433 die_errno("cannot exec '%s'", cmd->argv[0]);
437 error_errno("cannot fork() for %s", cmd->argv[0]);
438 else if (cmd->clean_on_exit)
439 mark_child_for_cleanup(cmd->pid, cmd);
442 * Wait for child's execvp. If the execvp succeeds (or if fork()
443 * failed), EOF is seen immediately by the parent. Otherwise, the
444 * child process sends a single byte.
445 * Note that use of this infrastructure is completely advisory,
446 * therefore, we keep error checks minimal.
448 close(notify_pipe[1]);
449 if (read(notify_pipe[0], ¬ify_pipe[1], 1) == 1) {
451 * At this point we know that fork() succeeded, but execvp()
452 * failed. Errors have been reported to our stderr.
454 wait_or_whine(cmd->pid, cmd->argv[0], 0);
455 failed_errno = errno;
458 close(notify_pipe[0]);
462 int fhin = 0, fhout = 1, fherr = 2;
463 const char **sargv = cmd->argv;
464 struct argv_array nargv = ARGV_ARRAY_INIT;
467 fhin = open("/dev/null", O_RDWR);
474 fherr = open("/dev/null", O_RDWR);
476 fherr = dup(fderr[1]);
477 else if (cmd->err > 2)
478 fherr = dup(cmd->err);
481 fhout = open("/dev/null", O_RDWR);
482 else if (cmd->stdout_to_stderr)
485 fhout = dup(fdout[1]);
486 else if (cmd->out > 1)
487 fhout = dup(cmd->out);
490 cmd->argv = prepare_git_cmd(&nargv, cmd->argv);
491 else if (cmd->use_shell)
492 cmd->argv = prepare_shell_cmd(&nargv, cmd->argv);
494 cmd->pid = mingw_spawnvpe(cmd->argv[0], cmd->argv, (char**) cmd->env,
495 cmd->dir, fhin, fhout, fherr);
496 failed_errno = errno;
497 if (cmd->pid < 0 && (!cmd->silent_exec_failure || errno != ENOENT))
498 error_errno("cannot spawn %s", cmd->argv[0]);
499 if (cmd->clean_on_exit && cmd->pid >= 0)
500 mark_child_for_cleanup(cmd->pid, cmd);
502 argv_array_clear(&nargv);
526 child_process_clear(cmd);
527 errno = failed_errno;
549 int finish_command(struct child_process *cmd)
551 int ret = wait_or_whine(cmd->pid, cmd->argv[0], 0);
552 child_process_clear(cmd);
556 int finish_command_in_signal(struct child_process *cmd)
558 return wait_or_whine(cmd->pid, cmd->argv[0], 1);
562 int run_command(struct child_process *cmd)
566 if (cmd->out < 0 || cmd->err < 0)
567 die("BUG: run_command with a pipe can cause deadlock");
569 code = start_command(cmd);
572 return finish_command(cmd);
575 int run_command_v_opt(const char **argv, int opt)
577 return run_command_v_opt_cd_env(argv, opt, NULL, NULL);
580 int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env)
582 struct child_process cmd = CHILD_PROCESS_INIT;
584 cmd.no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
585 cmd.git_cmd = opt & RUN_GIT_CMD ? 1 : 0;
586 cmd.stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
587 cmd.silent_exec_failure = opt & RUN_SILENT_EXEC_FAILURE ? 1 : 0;
588 cmd.use_shell = opt & RUN_USING_SHELL ? 1 : 0;
589 cmd.clean_on_exit = opt & RUN_CLEAN_ON_EXIT ? 1 : 0;
592 return run_command(&cmd);
596 static pthread_t main_thread;
597 static int main_thread_set;
598 static pthread_key_t async_key;
599 static pthread_key_t async_die_counter;
601 static void *run_thread(void *data)
603 struct async *async = data;
606 if (async->isolate_sigpipe) {
609 sigaddset(&mask, SIGPIPE);
610 if (pthread_sigmask(SIG_BLOCK, &mask, NULL) < 0) {
611 ret = error("unable to block SIGPIPE in async thread");
616 pthread_setspecific(async_key, async);
617 ret = async->proc(async->proc_in, async->proc_out, async->data);
621 static NORETURN void die_async(const char *err, va_list params)
623 vreportf("fatal: ", err, params);
626 struct async *async = pthread_getspecific(async_key);
627 if (async->proc_in >= 0)
628 close(async->proc_in);
629 if (async->proc_out >= 0)
630 close(async->proc_out);
631 pthread_exit((void *)128);
637 static int async_die_is_recursing(void)
639 void *ret = pthread_getspecific(async_die_counter);
640 pthread_setspecific(async_die_counter, (void *)1);
646 if (!main_thread_set)
647 return 0; /* no asyncs started yet */
648 return !pthread_equal(main_thread, pthread_self());
651 static void NORETURN async_exit(int code)
653 pthread_exit((void *)(intptr_t)code);
659 void (**handlers)(void);
664 static int git_atexit_installed;
666 static void git_atexit_dispatch(void)
670 for (i=git_atexit_hdlrs.nr ; i ; i--)
671 git_atexit_hdlrs.handlers[i-1]();
674 static void git_atexit_clear(void)
676 free(git_atexit_hdlrs.handlers);
677 memset(&git_atexit_hdlrs, 0, sizeof(git_atexit_hdlrs));
678 git_atexit_installed = 0;
682 int git_atexit(void (*handler)(void))
684 ALLOC_GROW(git_atexit_hdlrs.handlers, git_atexit_hdlrs.nr + 1, git_atexit_hdlrs.alloc);
685 git_atexit_hdlrs.handlers[git_atexit_hdlrs.nr++] = handler;
686 if (!git_atexit_installed) {
687 if (atexit(&git_atexit_dispatch))
689 git_atexit_installed = 1;
693 #define atexit git_atexit
695 static int process_is_async;
698 return process_is_async;
701 static void NORETURN async_exit(int code)
708 void check_pipe(int err)
714 signal(SIGPIPE, SIG_DFL);
716 /* Should never happen, but just in case... */
721 int start_async(struct async *async)
723 int need_in, need_out;
724 int fdin[2], fdout[2];
725 int proc_in, proc_out;
727 need_in = async->in < 0;
729 if (pipe(fdin) < 0) {
732 return error_errno("cannot create pipe");
737 need_out = async->out < 0;
739 if (pipe(fdout) < 0) {
744 return error_errno("cannot create pipe");
746 async->out = fdout[0];
759 proc_out = async->out;
764 /* Flush stdio before fork() to avoid cloning buffers */
768 if (async->pid < 0) {
769 error_errno("fork (async) failed");
778 process_is_async = 1;
779 exit(!!async->proc(proc_in, proc_out, async->data));
782 mark_child_for_cleanup(async->pid, NULL);
794 if (!main_thread_set) {
796 * We assume that the first time that start_async is called
797 * it is from the main thread.
800 main_thread = pthread_self();
801 pthread_key_create(&async_key, NULL);
802 pthread_key_create(&async_die_counter, NULL);
803 set_die_routine(die_async);
804 set_die_is_recursing_routine(async_die_is_recursing);
808 set_cloexec(proc_in);
810 set_cloexec(proc_out);
811 async->proc_in = proc_in;
812 async->proc_out = proc_out;
814 int err = pthread_create(&async->tid, NULL, run_thread, async);
816 error_errno("cannot create thread");
836 int finish_async(struct async *async)
839 return wait_or_whine(async->pid, "child process", 0);
841 void *ret = (void *)(intptr_t)(-1);
843 if (pthread_join(async->tid, &ret))
844 error("pthread_join failed");
845 return (int)(intptr_t)ret;
849 const char *find_hook(const char *name)
851 static struct strbuf path = STRBUF_INIT;
854 strbuf_git_path(&path, "hooks/%s", name);
855 if (access(path.buf, X_OK) < 0) {
856 #ifdef STRIP_EXTENSION
857 strbuf_addstr(&path, STRIP_EXTENSION);
858 if (access(path.buf, X_OK) >= 0)
866 int run_hook_ve(const char *const *env, const char *name, va_list args)
868 struct child_process hook = CHILD_PROCESS_INIT;
875 argv_array_push(&hook.args, p);
876 while ((p = va_arg(args, const char *)))
877 argv_array_push(&hook.args, p);
880 hook.stdout_to_stderr = 1;
882 return run_command(&hook);
885 int run_hook_le(const char *const *env, const char *name, ...)
890 va_start(args, name);
891 ret = run_hook_ve(env, name, args);
898 /* initialized by caller */
900 int type; /* POLLOUT or POLLIN */
912 /* returned by pump_io */
913 int error; /* 0 for success, otherwise errno */
919 static int pump_io_round(struct io_pump *slots, int nr, struct pollfd *pfd)
924 for (i = 0; i < nr; i++) {
925 struct io_pump *io = &slots[i];
928 pfd[pollsize].fd = io->fd;
929 pfd[pollsize].events = io->type;
930 io->pfd = &pfd[pollsize++];
936 if (poll(pfd, pollsize, -1) < 0) {
939 die_errno("poll failed");
942 for (i = 0; i < nr; i++) {
943 struct io_pump *io = &slots[i];
948 if (!(io->pfd->revents & (POLLOUT|POLLIN|POLLHUP|POLLERR|POLLNVAL)))
951 if (io->type == POLLOUT) {
952 ssize_t len = xwrite(io->fd,
953 io->u.out.buf, io->u.out.len);
959 io->u.out.buf += len;
960 io->u.out.len -= len;
961 if (!io->u.out.len) {
968 if (io->type == POLLIN) {
969 ssize_t len = strbuf_read_once(io->u.in.buf,
970 io->fd, io->u.in.hint);
983 static int pump_io(struct io_pump *slots, int nr)
988 for (i = 0; i < nr; i++)
991 ALLOC_ARRAY(pfd, nr);
992 while (pump_io_round(slots, nr, pfd))
996 /* There may be multiple errno values, so just pick the first. */
997 for (i = 0; i < nr; i++) {
998 if (slots[i].error) {
999 errno = slots[i].error;
1007 int pipe_command(struct child_process *cmd,
1008 const char *in, size_t in_len,
1009 struct strbuf *out, size_t out_hint,
1010 struct strbuf *err, size_t err_hint)
1012 struct io_pump io[3];
1022 if (start_command(cmd) < 0)
1026 io[nr].fd = cmd->in;
1027 io[nr].type = POLLOUT;
1028 io[nr].u.out.buf = in;
1029 io[nr].u.out.len = in_len;
1033 io[nr].fd = cmd->out;
1034 io[nr].type = POLLIN;
1035 io[nr].u.in.buf = out;
1036 io[nr].u.in.hint = out_hint;
1040 io[nr].fd = cmd->err;
1041 io[nr].type = POLLIN;
1042 io[nr].u.in.buf = err;
1043 io[nr].u.in.hint = err_hint;
1047 if (pump_io(io, nr) < 0) {
1048 finish_command(cmd); /* throw away exit code */
1052 return finish_command(cmd);
1058 GIT_CP_WAIT_CLEANUP,
1061 struct parallel_processes {
1067 get_next_task_fn get_next_task;
1068 start_failure_fn start_failure;
1069 task_finished_fn task_finished;
1072 enum child_state state;
1073 struct child_process process;
1078 * The struct pollfd is logically part of *children,
1079 * but the system call expects it as its own array.
1083 unsigned shutdown : 1;
1086 struct strbuf buffered_output; /* of finished children */
1089 static int default_start_failure(struct strbuf *out,
1096 static int default_task_finished(int result,
1104 static void kill_children(struct parallel_processes *pp, int signo)
1106 int i, n = pp->max_processes;
1108 for (i = 0; i < n; i++)
1109 if (pp->children[i].state == GIT_CP_WORKING)
1110 kill(pp->children[i].process.pid, signo);
1113 static struct parallel_processes *pp_for_signal;
1115 static void handle_children_on_signal(int signo)
1117 kill_children(pp_for_signal, signo);
1118 sigchain_pop(signo);
1122 static void pp_init(struct parallel_processes *pp,
1124 get_next_task_fn get_next_task,
1125 start_failure_fn start_failure,
1126 task_finished_fn task_finished,
1134 pp->max_processes = n;
1136 trace_printf("run_processes_parallel: preparing to run up to %d tasks", n);
1140 die("BUG: you need to specify a get_next_task function");
1141 pp->get_next_task = get_next_task;
1143 pp->start_failure = start_failure ? start_failure : default_start_failure;
1144 pp->task_finished = task_finished ? task_finished : default_task_finished;
1146 pp->nr_processes = 0;
1147 pp->output_owner = 0;
1149 pp->children = xcalloc(n, sizeof(*pp->children));
1150 pp->pfd = xcalloc(n, sizeof(*pp->pfd));
1151 strbuf_init(&pp->buffered_output, 0);
1153 for (i = 0; i < n; i++) {
1154 strbuf_init(&pp->children[i].err, 0);
1155 child_process_init(&pp->children[i].process);
1156 pp->pfd[i].events = POLLIN | POLLHUP;
1161 sigchain_push_common(handle_children_on_signal);
1164 static void pp_cleanup(struct parallel_processes *pp)
1168 trace_printf("run_processes_parallel: done");
1169 for (i = 0; i < pp->max_processes; i++) {
1170 strbuf_release(&pp->children[i].err);
1171 child_process_clear(&pp->children[i].process);
1178 * When get_next_task added messages to the buffer in its last
1179 * iteration, the buffered output is non empty.
1181 strbuf_write(&pp->buffered_output, stderr);
1182 strbuf_release(&pp->buffered_output);
1184 sigchain_pop_common();
1188 * 0 if a new task was started.
1189 * 1 if no new jobs was started (get_next_task ran out of work, non critical
1190 * problem with starting a new command)
1191 * <0 no new job was started, user wishes to shutdown early. Use negative code
1192 * to signal the children.
1194 static int pp_start_one(struct parallel_processes *pp)
1198 for (i = 0; i < pp->max_processes; i++)
1199 if (pp->children[i].state == GIT_CP_FREE)
1201 if (i == pp->max_processes)
1202 die("BUG: bookkeeping is hard");
1204 code = pp->get_next_task(&pp->children[i].process,
1205 &pp->children[i].err,
1207 &pp->children[i].data);
1209 strbuf_addbuf(&pp->buffered_output, &pp->children[i].err);
1210 strbuf_reset(&pp->children[i].err);
1213 pp->children[i].process.err = -1;
1214 pp->children[i].process.stdout_to_stderr = 1;
1215 pp->children[i].process.no_stdin = 1;
1217 if (start_command(&pp->children[i].process)) {
1218 code = pp->start_failure(&pp->children[i].err,
1220 &pp->children[i].data);
1221 strbuf_addbuf(&pp->buffered_output, &pp->children[i].err);
1222 strbuf_reset(&pp->children[i].err);
1229 pp->children[i].state = GIT_CP_WORKING;
1230 pp->pfd[i].fd = pp->children[i].process.err;
1234 static void pp_buffer_stderr(struct parallel_processes *pp, int output_timeout)
1238 while ((i = poll(pp->pfd, pp->max_processes, output_timeout)) < 0) {
1245 /* Buffer output from all pipes. */
1246 for (i = 0; i < pp->max_processes; i++) {
1247 if (pp->children[i].state == GIT_CP_WORKING &&
1248 pp->pfd[i].revents & (POLLIN | POLLHUP)) {
1249 int n = strbuf_read_once(&pp->children[i].err,
1250 pp->children[i].process.err, 0);
1252 close(pp->children[i].process.err);
1253 pp->children[i].state = GIT_CP_WAIT_CLEANUP;
1255 if (errno != EAGAIN)
1261 static void pp_output(struct parallel_processes *pp)
1263 int i = pp->output_owner;
1264 if (pp->children[i].state == GIT_CP_WORKING &&
1265 pp->children[i].err.len) {
1266 strbuf_write(&pp->children[i].err, stderr);
1267 strbuf_reset(&pp->children[i].err);
1271 static int pp_collect_finished(struct parallel_processes *pp)
1274 int n = pp->max_processes;
1277 while (pp->nr_processes > 0) {
1278 for (i = 0; i < pp->max_processes; i++)
1279 if (pp->children[i].state == GIT_CP_WAIT_CLEANUP)
1281 if (i == pp->max_processes)
1284 code = finish_command(&pp->children[i].process);
1286 code = pp->task_finished(code,
1287 &pp->children[i].err, pp->data,
1288 &pp->children[i].data);
1296 pp->children[i].state = GIT_CP_FREE;
1298 child_process_init(&pp->children[i].process);
1300 if (i != pp->output_owner) {
1301 strbuf_addbuf(&pp->buffered_output, &pp->children[i].err);
1302 strbuf_reset(&pp->children[i].err);
1304 strbuf_write(&pp->children[i].err, stderr);
1305 strbuf_reset(&pp->children[i].err);
1307 /* Output all other finished child processes */
1308 strbuf_write(&pp->buffered_output, stderr);
1309 strbuf_reset(&pp->buffered_output);
1312 * Pick next process to output live.
1314 * For now we pick it randomly by doing a round
1315 * robin. Later we may want to pick the one with
1316 * the most output or the longest or shortest
1317 * running process time.
1319 for (i = 0; i < n; i++)
1320 if (pp->children[(pp->output_owner + i) % n].state == GIT_CP_WORKING)
1322 pp->output_owner = (pp->output_owner + i) % n;
1328 int run_processes_parallel(int n,
1329 get_next_task_fn get_next_task,
1330 start_failure_fn start_failure,
1331 task_finished_fn task_finished,
1335 int output_timeout = 100;
1337 struct parallel_processes pp;
1339 pp_init(&pp, n, get_next_task, start_failure, task_finished, pp_cb);
1342 i < spawn_cap && !pp.shutdown &&
1343 pp.nr_processes < pp.max_processes;
1345 code = pp_start_one(&pp);
1350 kill_children(&pp, -code);
1354 if (!pp.nr_processes)
1356 pp_buffer_stderr(&pp, output_timeout);
1358 code = pp_collect_finished(&pp);
1362 kill_children(&pp, -code);