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_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;
40 static void cleanup_children_on_signal(int sig)
42 cleanup_children(sig, 1);
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));
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) {
81 static inline void close_pair(int fd[2])
87 #ifndef GIT_WINDOWS_NATIVE
88 static inline void dup_devnull(int to)
90 int fd = open("/dev/null", O_RDWR);
92 die_errno(_("open /dev/null failed"));
94 die_errno(_("dup2(%d,%d) failed"), fd, to);
99 static char *locate_in_PATH(const char *file)
101 const char *p = getenv("PATH");
102 struct strbuf buf = STRBUF_INIT;
108 const char *end = strchrnul(p, ':');
112 /* POSIX specifies an empty entry as the current directory. */
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);
127 strbuf_release(&buf);
131 static int exists_in_PATH(const char *file)
133 char *r = locate_in_PATH(file);
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
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, '/'))
163 static const char **prepare_shell_cmd(struct argv_array *out, const char **argv)
166 die("BUG: shell command is empty");
168 if (strcspn(argv[0], "|&;<>()$`\\\"' \t\n*?[#~=%") != strlen(argv[0])) {
169 #ifndef GIT_WINDOWS_NATIVE
170 argv_array_push(out, SHELL_PATH);
172 argv_array_push(out, "sh");
174 argv_array_push(out, "-c");
177 * If we have no extra arguments, we do not even need to
178 * bother with the "$@" magic.
181 argv_array_push(out, argv[0]);
183 argv_array_pushf(out, "%s \"$@\"", argv[0]);
186 argv_array_pushv(out, argv);
190 #ifndef GIT_WINDOWS_NATIVE
191 static int execv_shell_cmd(const char **argv)
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);
202 #ifndef GIT_WINDOWS_NATIVE
203 static int child_notifier = -1;
205 static void notify_parent(void)
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.
212 xwrite(child_notifier, "", 1);
216 static inline void set_cloexec(int fd)
218 int flags = fcntl(fd, F_GETFD);
220 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
223 static int wait_or_whine(pid_t pid, const char *argv0, int in_signal)
225 int status, code = -1;
227 int failed_errno = 0;
229 while ((waiting = waitpid(pid, &status, 0)) < 0 && errno == EINTR)
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);
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.
249 } else if (WIFEXITED(status)) {
250 code = WEXITSTATUS(status);
252 * Convert special exit code when execvp failed.
256 failed_errno = ENOENT;
259 error("waitpid is confused (%s)", argv0);
262 clear_child_for_cleanup(pid);
264 errno = failed_errno;
268 int start_command(struct child_process *cmd)
270 int need_in, need_out, need_err;
271 int fdin[2], fdout[2], fderr[2];
276 cmd->argv = cmd->args.argv;
278 cmd->env = cmd->env_array.argv;
281 * In case of errors we must keep the promise to close FDs
282 * that have been passed in via ->in and ->out.
285 need_in = !cmd->no_stdin && cmd->in < 0;
287 if (pipe(fdin) < 0) {
288 failed_errno = errno;
291 str = "standard input";
297 need_out = !cmd->no_stdout
298 && !cmd->stdout_to_stderr
301 if (pipe(fdout) < 0) {
302 failed_errno = errno;
307 str = "standard output";
313 need_err = !cmd->no_stderr && cmd->err < 0;
315 if (pipe(fderr) < 0) {
316 failed_errno = errno;
325 str = "standard error";
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;
336 trace_argv_printf(cmd->argv, "trace: run_command:");
339 #ifndef GIT_WINDOWS_NATIVE
342 if (pipe(notify_pipe))
343 notify_pipe[0] = notify_pipe[1] = -1;
346 failed_errno = errno;
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.
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"));
359 close(notify_pipe[0]);
360 set_cloexec(notify_pipe[1]);
361 child_notifier = notify_pipe[1];
362 atexit(notify_parent);
369 } else if (cmd->in) {
379 } else if (cmd->err > 1) {
386 else if (cmd->stdout_to_stderr)
391 } else if (cmd->out > 1) {
396 if (cmd->dir && chdir(cmd->dir))
397 die_errno("exec '%s': cd to '%s' failed", cmd->argv[0],
400 for (; *cmd->env; cmd->env++) {
401 if (strchr(*cmd->env, '='))
402 putenv((char *)*cmd->env);
408 execv_git_cmd(cmd->argv);
409 else if (cmd->use_shell)
410 execv_shell_cmd(cmd->argv);
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],
419 die_errno("cannot exec '%s'", cmd->argv[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);
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.
434 close(notify_pipe[1]);
435 if (read(notify_pipe[0], ¬ify_pipe[1], 1) == 1) {
437 * At this point we know that fork() succeeded, but execvp()
438 * failed. Errors have been reported to our stderr.
440 wait_or_whine(cmd->pid, cmd->argv[0], 0);
441 failed_errno = errno;
444 close(notify_pipe[0]);
448 int fhin = 0, fhout = 1, fherr = 2;
449 const char **sargv = cmd->argv;
450 struct argv_array nargv = ARGV_ARRAY_INIT;
453 fhin = open("/dev/null", O_RDWR);
460 fherr = open("/dev/null", O_RDWR);
462 fherr = dup(fderr[1]);
463 else if (cmd->err > 2)
464 fherr = dup(cmd->err);
467 fhout = open("/dev/null", O_RDWR);
468 else if (cmd->stdout_to_stderr)
471 fhout = dup(fdout[1]);
472 else if (cmd->out > 1)
473 fhout = dup(cmd->out);
476 cmd->argv = prepare_git_cmd(&nargv, cmd->argv);
477 else if (cmd->use_shell)
478 cmd->argv = prepare_shell_cmd(&nargv, cmd->argv);
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);
488 argv_array_clear(&nargv);
512 child_process_clear(cmd);
513 errno = failed_errno;
535 int finish_command(struct child_process *cmd)
537 int ret = wait_or_whine(cmd->pid, cmd->argv[0], 0);
538 child_process_clear(cmd);
542 int finish_command_in_signal(struct child_process *cmd)
544 return wait_or_whine(cmd->pid, cmd->argv[0], 1);
548 int run_command(struct child_process *cmd)
552 if (cmd->out < 0 || cmd->err < 0)
553 die("BUG: run_command with a pipe can cause deadlock");
555 code = start_command(cmd);
558 return finish_command(cmd);
561 int run_command_v_opt(const char **argv, int opt)
563 return run_command_v_opt_cd_env(argv, opt, NULL, NULL);
566 int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env)
568 struct child_process cmd = CHILD_PROCESS_INIT;
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;
578 return run_command(&cmd);
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;
587 static void *run_thread(void *data)
589 struct async *async = data;
592 if (async->isolate_sigpipe) {
595 sigaddset(&mask, SIGPIPE);
596 if (pthread_sigmask(SIG_BLOCK, &mask, NULL) < 0) {
597 ret = error("unable to block SIGPIPE in async thread");
602 pthread_setspecific(async_key, async);
603 ret = async->proc(async->proc_in, async->proc_out, async->data);
607 static NORETURN void die_async(const char *err, va_list params)
609 vreportf("fatal: ", err, params);
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);
623 static int async_die_is_recursing(void)
625 void *ret = pthread_getspecific(async_die_counter);
626 pthread_setspecific(async_die_counter, (void *)1);
632 if (!main_thread_set)
633 return 0; /* no asyncs started yet */
634 return !pthread_equal(main_thread, pthread_self());
637 static void NORETURN async_exit(int code)
639 pthread_exit((void *)(intptr_t)code);
645 void (**handlers)(void);
650 static int git_atexit_installed;
652 static void git_atexit_dispatch(void)
656 for (i=git_atexit_hdlrs.nr ; i ; i--)
657 git_atexit_hdlrs.handlers[i-1]();
660 static void git_atexit_clear(void)
662 free(git_atexit_hdlrs.handlers);
663 memset(&git_atexit_hdlrs, 0, sizeof(git_atexit_hdlrs));
664 git_atexit_installed = 0;
668 int git_atexit(void (*handler)(void))
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))
675 git_atexit_installed = 1;
679 #define atexit git_atexit
681 static int process_is_async;
684 return process_is_async;
687 static void NORETURN async_exit(int code)
694 void check_pipe(int err)
700 signal(SIGPIPE, SIG_DFL);
702 /* Should never happen, but just in case... */
707 int start_async(struct async *async)
709 int need_in, need_out;
710 int fdin[2], fdout[2];
711 int proc_in, proc_out;
713 need_in = async->in < 0;
715 if (pipe(fdin) < 0) {
718 return error_errno("cannot create pipe");
723 need_out = async->out < 0;
725 if (pipe(fdout) < 0) {
730 return error_errno("cannot create pipe");
732 async->out = fdout[0];
745 proc_out = async->out;
750 /* Flush stdio before fork() to avoid cloning buffers */
754 if (async->pid < 0) {
755 error_errno("fork (async) failed");
764 process_is_async = 1;
765 exit(!!async->proc(proc_in, proc_out, async->data));
768 mark_child_for_cleanup(async->pid);
780 if (!main_thread_set) {
782 * We assume that the first time that start_async is called
783 * it is from the main thread.
786 main_thread = pthread_self();
787 pthread_key_create(&async_key, NULL);
788 pthread_key_create(&async_die_counter, NULL);
789 set_die_routine(die_async);
790 set_die_is_recursing_routine(async_die_is_recursing);
794 set_cloexec(proc_in);
796 set_cloexec(proc_out);
797 async->proc_in = proc_in;
798 async->proc_out = proc_out;
800 int err = pthread_create(&async->tid, NULL, run_thread, async);
802 error_errno("cannot create thread");
822 int finish_async(struct async *async)
825 return wait_or_whine(async->pid, "child process", 0);
827 void *ret = (void *)(intptr_t)(-1);
829 if (pthread_join(async->tid, &ret))
830 error("pthread_join failed");
831 return (int)(intptr_t)ret;
835 const char *find_hook(const char *name)
837 static struct strbuf path = STRBUF_INIT;
840 strbuf_git_path(&path, "hooks/%s", name);
841 if (access(path.buf, X_OK) < 0)
846 int run_hook_ve(const char *const *env, const char *name, va_list args)
848 struct child_process hook = CHILD_PROCESS_INIT;
855 argv_array_push(&hook.args, p);
856 while ((p = va_arg(args, const char *)))
857 argv_array_push(&hook.args, p);
860 hook.stdout_to_stderr = 1;
862 return run_command(&hook);
865 int run_hook_le(const char *const *env, const char *name, ...)
870 va_start(args, name);
871 ret = run_hook_ve(env, name, args);
878 /* initialized by caller */
880 int type; /* POLLOUT or POLLIN */
892 /* returned by pump_io */
893 int error; /* 0 for success, otherwise errno */
899 static int pump_io_round(struct io_pump *slots, int nr, struct pollfd *pfd)
904 for (i = 0; i < nr; i++) {
905 struct io_pump *io = &slots[i];
908 pfd[pollsize].fd = io->fd;
909 pfd[pollsize].events = io->type;
910 io->pfd = &pfd[pollsize++];
916 if (poll(pfd, pollsize, -1) < 0) {
919 die_errno("poll failed");
922 for (i = 0; i < nr; i++) {
923 struct io_pump *io = &slots[i];
928 if (!(io->pfd->revents & (POLLOUT|POLLIN|POLLHUP|POLLERR|POLLNVAL)))
931 if (io->type == POLLOUT) {
932 ssize_t len = xwrite(io->fd,
933 io->u.out.buf, io->u.out.len);
939 io->u.out.buf += len;
940 io->u.out.len -= len;
941 if (!io->u.out.len) {
948 if (io->type == POLLIN) {
949 ssize_t len = strbuf_read_once(io->u.in.buf,
950 io->fd, io->u.in.hint);
963 static int pump_io(struct io_pump *slots, int nr)
968 for (i = 0; i < nr; i++)
971 ALLOC_ARRAY(pfd, nr);
972 while (pump_io_round(slots, nr, pfd))
976 /* There may be multiple errno values, so just pick the first. */
977 for (i = 0; i < nr; i++) {
978 if (slots[i].error) {
979 errno = slots[i].error;
987 int pipe_command(struct child_process *cmd,
988 const char *in, size_t in_len,
989 struct strbuf *out, size_t out_hint,
990 struct strbuf *err, size_t err_hint)
992 struct io_pump io[3];
1002 if (start_command(cmd) < 0)
1006 io[nr].fd = cmd->in;
1007 io[nr].type = POLLOUT;
1008 io[nr].u.out.buf = in;
1009 io[nr].u.out.len = in_len;
1013 io[nr].fd = cmd->out;
1014 io[nr].type = POLLIN;
1015 io[nr].u.in.buf = out;
1016 io[nr].u.in.hint = out_hint;
1020 io[nr].fd = cmd->err;
1021 io[nr].type = POLLIN;
1022 io[nr].u.in.buf = err;
1023 io[nr].u.in.hint = err_hint;
1027 if (pump_io(io, nr) < 0) {
1028 finish_command(cmd); /* throw away exit code */
1032 return finish_command(cmd);
1038 GIT_CP_WAIT_CLEANUP,
1041 struct parallel_processes {
1047 get_next_task_fn get_next_task;
1048 start_failure_fn start_failure;
1049 task_finished_fn task_finished;
1052 enum child_state state;
1053 struct child_process process;
1058 * The struct pollfd is logically part of *children,
1059 * but the system call expects it as its own array.
1063 unsigned shutdown : 1;
1066 struct strbuf buffered_output; /* of finished children */
1069 static int default_start_failure(struct strbuf *out,
1076 static int default_task_finished(int result,
1084 static void kill_children(struct parallel_processes *pp, int signo)
1086 int i, n = pp->max_processes;
1088 for (i = 0; i < n; i++)
1089 if (pp->children[i].state == GIT_CP_WORKING)
1090 kill(pp->children[i].process.pid, signo);
1093 static struct parallel_processes *pp_for_signal;
1095 static void handle_children_on_signal(int signo)
1097 kill_children(pp_for_signal, signo);
1098 sigchain_pop(signo);
1102 static void pp_init(struct parallel_processes *pp,
1104 get_next_task_fn get_next_task,
1105 start_failure_fn start_failure,
1106 task_finished_fn task_finished,
1114 pp->max_processes = n;
1116 trace_printf("run_processes_parallel: preparing to run up to %d tasks", n);
1120 die("BUG: you need to specify a get_next_task function");
1121 pp->get_next_task = get_next_task;
1123 pp->start_failure = start_failure ? start_failure : default_start_failure;
1124 pp->task_finished = task_finished ? task_finished : default_task_finished;
1126 pp->nr_processes = 0;
1127 pp->output_owner = 0;
1129 pp->children = xcalloc(n, sizeof(*pp->children));
1130 pp->pfd = xcalloc(n, sizeof(*pp->pfd));
1131 strbuf_init(&pp->buffered_output, 0);
1133 for (i = 0; i < n; i++) {
1134 strbuf_init(&pp->children[i].err, 0);
1135 child_process_init(&pp->children[i].process);
1136 pp->pfd[i].events = POLLIN | POLLHUP;
1141 sigchain_push_common(handle_children_on_signal);
1144 static void pp_cleanup(struct parallel_processes *pp)
1148 trace_printf("run_processes_parallel: done");
1149 for (i = 0; i < pp->max_processes; i++) {
1150 strbuf_release(&pp->children[i].err);
1151 child_process_clear(&pp->children[i].process);
1158 * When get_next_task added messages to the buffer in its last
1159 * iteration, the buffered output is non empty.
1161 strbuf_write(&pp->buffered_output, stderr);
1162 strbuf_release(&pp->buffered_output);
1164 sigchain_pop_common();
1168 * 0 if a new task was started.
1169 * 1 if no new jobs was started (get_next_task ran out of work, non critical
1170 * problem with starting a new command)
1171 * <0 no new job was started, user wishes to shutdown early. Use negative code
1172 * to signal the children.
1174 static int pp_start_one(struct parallel_processes *pp)
1178 for (i = 0; i < pp->max_processes; i++)
1179 if (pp->children[i].state == GIT_CP_FREE)
1181 if (i == pp->max_processes)
1182 die("BUG: bookkeeping is hard");
1184 code = pp->get_next_task(&pp->children[i].process,
1185 &pp->children[i].err,
1187 &pp->children[i].data);
1189 strbuf_addbuf(&pp->buffered_output, &pp->children[i].err);
1190 strbuf_reset(&pp->children[i].err);
1193 pp->children[i].process.err = -1;
1194 pp->children[i].process.stdout_to_stderr = 1;
1195 pp->children[i].process.no_stdin = 1;
1197 if (start_command(&pp->children[i].process)) {
1198 code = pp->start_failure(&pp->children[i].err,
1200 &pp->children[i].data);
1201 strbuf_addbuf(&pp->buffered_output, &pp->children[i].err);
1202 strbuf_reset(&pp->children[i].err);
1209 pp->children[i].state = GIT_CP_WORKING;
1210 pp->pfd[i].fd = pp->children[i].process.err;
1214 static void pp_buffer_stderr(struct parallel_processes *pp, int output_timeout)
1218 while ((i = poll(pp->pfd, pp->max_processes, output_timeout)) < 0) {
1225 /* Buffer output from all pipes. */
1226 for (i = 0; i < pp->max_processes; i++) {
1227 if (pp->children[i].state == GIT_CP_WORKING &&
1228 pp->pfd[i].revents & (POLLIN | POLLHUP)) {
1229 int n = strbuf_read_once(&pp->children[i].err,
1230 pp->children[i].process.err, 0);
1232 close(pp->children[i].process.err);
1233 pp->children[i].state = GIT_CP_WAIT_CLEANUP;
1235 if (errno != EAGAIN)
1241 static void pp_output(struct parallel_processes *pp)
1243 int i = pp->output_owner;
1244 if (pp->children[i].state == GIT_CP_WORKING &&
1245 pp->children[i].err.len) {
1246 strbuf_write(&pp->children[i].err, stderr);
1247 strbuf_reset(&pp->children[i].err);
1251 static int pp_collect_finished(struct parallel_processes *pp)
1254 int n = pp->max_processes;
1257 while (pp->nr_processes > 0) {
1258 for (i = 0; i < pp->max_processes; i++)
1259 if (pp->children[i].state == GIT_CP_WAIT_CLEANUP)
1261 if (i == pp->max_processes)
1264 code = finish_command(&pp->children[i].process);
1266 code = pp->task_finished(code,
1267 &pp->children[i].err, pp->data,
1268 &pp->children[i].data);
1276 pp->children[i].state = GIT_CP_FREE;
1278 child_process_init(&pp->children[i].process);
1280 if (i != pp->output_owner) {
1281 strbuf_addbuf(&pp->buffered_output, &pp->children[i].err);
1282 strbuf_reset(&pp->children[i].err);
1284 strbuf_write(&pp->children[i].err, stderr);
1285 strbuf_reset(&pp->children[i].err);
1287 /* Output all other finished child processes */
1288 strbuf_write(&pp->buffered_output, stderr);
1289 strbuf_reset(&pp->buffered_output);
1292 * Pick next process to output live.
1294 * For now we pick it randomly by doing a round
1295 * robin. Later we may want to pick the one with
1296 * the most output or the longest or shortest
1297 * running process time.
1299 for (i = 0; i < n; i++)
1300 if (pp->children[(pp->output_owner + i) % n].state == GIT_CP_WORKING)
1302 pp->output_owner = (pp->output_owner + i) % n;
1308 int run_processes_parallel(int n,
1309 get_next_task_fn get_next_task,
1310 start_failure_fn start_failure,
1311 task_finished_fn task_finished,
1315 int output_timeout = 100;
1317 struct parallel_processes pp;
1319 pp_init(&pp, n, get_next_task, start_failure, task_finished, pp_cb);
1322 i < spawn_cap && !pp.shutdown &&
1323 pp.nr_processes < pp.max_processes;
1325 code = pp_start_one(&pp);
1330 kill_children(&pp, -code);
1334 if (!pp.nr_processes)
1336 pp_buffer_stderr(&pp, output_timeout);
1338 code = pp_collect_finished(&pp);
1342 kill_children(&pp, -code);