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 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) {
42 "trace: run_command: running exit handler for pid %"
43 PRIuMAX, (uintmax_t)p->pid
45 process->clean_on_exit_handler(process);
51 if (p->process && p->process->wait_after_clean) {
52 p->next = children_to_wait_for;
53 children_to_wait_for = 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 */
72 static void cleanup_children_on_signal(int sig)
74 cleanup_children(sig, 1);
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));
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;
114 static inline void close_pair(int fd[2])
120 #ifndef GIT_WINDOWS_NATIVE
121 static inline void dup_devnull(int to)
123 int fd = open("/dev/null", O_RDWR);
125 die_errno(_("open /dev/null failed"));
126 if (dup2(fd, to) < 0)
127 die_errno(_("dup2(%d,%d) failed"), fd, to);
132 static char *locate_in_PATH(const char *file)
134 const char *p = getenv("PATH");
135 struct strbuf buf = STRBUF_INIT;
141 const char *end = strchrnul(p, ':');
145 /* POSIX specifies an empty entry as the current directory. */
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);
160 strbuf_release(&buf);
164 static int exists_in_PATH(const char *file)
166 char *r = locate_in_PATH(file);
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
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, '/'))
196 static const char **prepare_shell_cmd(struct argv_array *out, const char **argv)
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);
205 argv_array_push(out, "sh");
207 argv_array_push(out, "-c");
210 * If we have no extra arguments, we do not even need to
211 * bother with the "$@" magic.
214 argv_array_push(out, argv[0]);
216 argv_array_pushf(out, "%s \"$@\"", argv[0]);
219 argv_array_pushv(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)
239 die("BUG: command is empty");
242 argv_array_push(out, "git");
243 argv_array_pushv(out, cmd->argv);
244 } else if (cmd->use_shell) {
245 prepare_shell_cmd(out, cmd->argv);
247 argv_array_pushv(out, cmd->argv);
252 static inline void set_cloexec(int fd)
254 int flags = fcntl(fd, F_GETFD);
256 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
259 static int wait_or_whine(pid_t pid, const char *argv0, int in_signal)
261 int status, code = -1;
263 int failed_errno = 0;
265 while ((waiting = waitpid(pid, &status, 0)) < 0 && errno == EINTR)
271 failed_errno = errno;
272 error_errno("waitpid for %s failed", argv0);
273 } else if (waiting != pid) {
274 error("waitpid is confused (%s)", argv0);
275 } else if (WIFSIGNALED(status)) {
276 code = WTERMSIG(status);
277 if (code != SIGINT && code != SIGQUIT && code != SIGPIPE)
278 error("%s died of signal %d", argv0, code);
280 * This return value is chosen so that code & 0xff
281 * mimics the exit code that a POSIX shell would report for
282 * a program that died from this signal.
285 } else if (WIFEXITED(status)) {
286 code = WEXITSTATUS(status);
288 * Convert special exit code when execvp failed.
292 failed_errno = ENOENT;
295 error("waitpid is confused (%s)", argv0);
298 clear_child_for_cleanup(pid);
300 errno = failed_errno;
304 int start_command(struct child_process *cmd)
306 int need_in, need_out, need_err;
307 int fdin[2], fdout[2], fderr[2];
312 cmd->argv = cmd->args.argv;
314 cmd->env = cmd->env_array.argv;
317 * In case of errors we must keep the promise to close FDs
318 * that have been passed in via ->in and ->out.
321 need_in = !cmd->no_stdin && cmd->in < 0;
323 if (pipe(fdin) < 0) {
324 failed_errno = errno;
327 str = "standard input";
333 need_out = !cmd->no_stdout
334 && !cmd->stdout_to_stderr
337 if (pipe(fdout) < 0) {
338 failed_errno = errno;
343 str = "standard output";
349 need_err = !cmd->no_stderr && cmd->err < 0;
351 if (pipe(fderr) < 0) {
352 failed_errno = errno;
361 str = "standard error";
363 error("cannot create %s pipe for %s: %s",
364 str, cmd->argv[0], strerror(failed_errno));
365 child_process_clear(cmd);
366 errno = failed_errno;
372 trace_argv_printf(cmd->argv, "trace: run_command:");
375 #ifndef GIT_WINDOWS_NATIVE
378 struct argv_array argv = ARGV_ARRAY_INIT;
380 if (pipe(notify_pipe))
381 notify_pipe[0] = notify_pipe[1] = -1;
383 prepare_cmd(&argv, cmd);
386 failed_errno = errno;
389 * Redirect the channel to write syscall error messages to
390 * before redirecting the process's stderr so that all die()
391 * in subsequent call paths use the parent's stderr.
393 if (cmd->no_stderr || need_err) {
394 int child_err = dup(2);
395 set_cloexec(child_err);
396 set_error_handle(fdopen(child_err, "w"));
399 close(notify_pipe[0]);
400 set_cloexec(notify_pipe[1]);
401 child_notifier = notify_pipe[1];
402 atexit(notify_parent);
409 } else if (cmd->in) {
419 } else if (cmd->err > 1) {
426 else if (cmd->stdout_to_stderr)
431 } else if (cmd->out > 1) {
436 if (cmd->dir && chdir(cmd->dir))
437 die_errno("exec '%s': cd to '%s' failed", cmd->argv[0],
440 for (; *cmd->env; cmd->env++) {
441 if (strchr(*cmd->env, '='))
442 putenv((char *)*cmd->env);
448 sane_execvp(argv.argv[0], (char *const *) argv.argv);
450 if (errno == ENOENT) {
451 if (!cmd->silent_exec_failure)
452 error("cannot run %s: %s", cmd->argv[0],
456 die_errno("cannot exec '%s'", cmd->argv[0]);
460 error_errno("cannot fork() for %s", cmd->argv[0]);
461 else if (cmd->clean_on_exit)
462 mark_child_for_cleanup(cmd->pid, cmd);
465 * Wait for child's exec. If the exec succeeds (or if fork()
466 * failed), EOF is seen immediately by the parent. Otherwise, the
467 * child process sends a single byte.
468 * Note that use of this infrastructure is completely advisory,
469 * therefore, we keep error checks minimal.
471 close(notify_pipe[1]);
472 if (read(notify_pipe[0], ¬ify_pipe[1], 1) == 1) {
474 * At this point we know that fork() succeeded, but exec()
475 * failed. Errors have been reported to our stderr.
477 wait_or_whine(cmd->pid, cmd->argv[0], 0);
478 failed_errno = errno;
481 close(notify_pipe[0]);
483 argv_array_clear(&argv);
487 int fhin = 0, fhout = 1, fherr = 2;
488 const char **sargv = cmd->argv;
489 struct argv_array nargv = ARGV_ARRAY_INIT;
492 fhin = open("/dev/null", O_RDWR);
499 fherr = open("/dev/null", O_RDWR);
501 fherr = dup(fderr[1]);
502 else if (cmd->err > 2)
503 fherr = dup(cmd->err);
506 fhout = open("/dev/null", O_RDWR);
507 else if (cmd->stdout_to_stderr)
510 fhout = dup(fdout[1]);
511 else if (cmd->out > 1)
512 fhout = dup(cmd->out);
515 cmd->argv = prepare_git_cmd(&nargv, cmd->argv);
516 else if (cmd->use_shell)
517 cmd->argv = prepare_shell_cmd(&nargv, cmd->argv);
519 cmd->pid = mingw_spawnvpe(cmd->argv[0], cmd->argv, (char**) cmd->env,
520 cmd->dir, fhin, fhout, fherr);
521 failed_errno = errno;
522 if (cmd->pid < 0 && (!cmd->silent_exec_failure || errno != ENOENT))
523 error_errno("cannot spawn %s", cmd->argv[0]);
524 if (cmd->clean_on_exit && cmd->pid >= 0)
525 mark_child_for_cleanup(cmd->pid, cmd);
527 argv_array_clear(&nargv);
551 child_process_clear(cmd);
552 errno = failed_errno;
574 int finish_command(struct child_process *cmd)
576 int ret = wait_or_whine(cmd->pid, cmd->argv[0], 0);
577 child_process_clear(cmd);
581 int finish_command_in_signal(struct child_process *cmd)
583 return wait_or_whine(cmd->pid, cmd->argv[0], 1);
587 int run_command(struct child_process *cmd)
591 if (cmd->out < 0 || cmd->err < 0)
592 die("BUG: run_command with a pipe can cause deadlock");
594 code = start_command(cmd);
597 return finish_command(cmd);
600 int run_command_v_opt(const char **argv, int opt)
602 return run_command_v_opt_cd_env(argv, opt, NULL, NULL);
605 int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env)
607 struct child_process cmd = CHILD_PROCESS_INIT;
609 cmd.no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
610 cmd.git_cmd = opt & RUN_GIT_CMD ? 1 : 0;
611 cmd.stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
612 cmd.silent_exec_failure = opt & RUN_SILENT_EXEC_FAILURE ? 1 : 0;
613 cmd.use_shell = opt & RUN_USING_SHELL ? 1 : 0;
614 cmd.clean_on_exit = opt & RUN_CLEAN_ON_EXIT ? 1 : 0;
617 return run_command(&cmd);
621 static pthread_t main_thread;
622 static int main_thread_set;
623 static pthread_key_t async_key;
624 static pthread_key_t async_die_counter;
626 static void *run_thread(void *data)
628 struct async *async = data;
631 if (async->isolate_sigpipe) {
634 sigaddset(&mask, SIGPIPE);
635 if (pthread_sigmask(SIG_BLOCK, &mask, NULL) < 0) {
636 ret = error("unable to block SIGPIPE in async thread");
641 pthread_setspecific(async_key, async);
642 ret = async->proc(async->proc_in, async->proc_out, async->data);
646 static NORETURN void die_async(const char *err, va_list params)
648 vreportf("fatal: ", err, params);
651 struct async *async = pthread_getspecific(async_key);
652 if (async->proc_in >= 0)
653 close(async->proc_in);
654 if (async->proc_out >= 0)
655 close(async->proc_out);
656 pthread_exit((void *)128);
662 static int async_die_is_recursing(void)
664 void *ret = pthread_getspecific(async_die_counter);
665 pthread_setspecific(async_die_counter, (void *)1);
671 if (!main_thread_set)
672 return 0; /* no asyncs started yet */
673 return !pthread_equal(main_thread, pthread_self());
676 static void NORETURN async_exit(int code)
678 pthread_exit((void *)(intptr_t)code);
684 void (**handlers)(void);
689 static int git_atexit_installed;
691 static void git_atexit_dispatch(void)
695 for (i=git_atexit_hdlrs.nr ; i ; i--)
696 git_atexit_hdlrs.handlers[i-1]();
699 static void git_atexit_clear(void)
701 free(git_atexit_hdlrs.handlers);
702 memset(&git_atexit_hdlrs, 0, sizeof(git_atexit_hdlrs));
703 git_atexit_installed = 0;
707 int git_atexit(void (*handler)(void))
709 ALLOC_GROW(git_atexit_hdlrs.handlers, git_atexit_hdlrs.nr + 1, git_atexit_hdlrs.alloc);
710 git_atexit_hdlrs.handlers[git_atexit_hdlrs.nr++] = handler;
711 if (!git_atexit_installed) {
712 if (atexit(&git_atexit_dispatch))
714 git_atexit_installed = 1;
718 #define atexit git_atexit
720 static int process_is_async;
723 return process_is_async;
726 static void NORETURN async_exit(int code)
733 void check_pipe(int err)
739 signal(SIGPIPE, SIG_DFL);
741 /* Should never happen, but just in case... */
746 int start_async(struct async *async)
748 int need_in, need_out;
749 int fdin[2], fdout[2];
750 int proc_in, proc_out;
752 need_in = async->in < 0;
754 if (pipe(fdin) < 0) {
757 return error_errno("cannot create pipe");
762 need_out = async->out < 0;
764 if (pipe(fdout) < 0) {
769 return error_errno("cannot create pipe");
771 async->out = fdout[0];
784 proc_out = async->out;
789 /* Flush stdio before fork() to avoid cloning buffers */
793 if (async->pid < 0) {
794 error_errno("fork (async) failed");
803 process_is_async = 1;
804 exit(!!async->proc(proc_in, proc_out, async->data));
807 mark_child_for_cleanup(async->pid, NULL);
819 if (!main_thread_set) {
821 * We assume that the first time that start_async is called
822 * it is from the main thread.
825 main_thread = pthread_self();
826 pthread_key_create(&async_key, NULL);
827 pthread_key_create(&async_die_counter, NULL);
828 set_die_routine(die_async);
829 set_die_is_recursing_routine(async_die_is_recursing);
833 set_cloexec(proc_in);
835 set_cloexec(proc_out);
836 async->proc_in = proc_in;
837 async->proc_out = proc_out;
839 int err = pthread_create(&async->tid, NULL, run_thread, async);
841 error_errno("cannot create thread");
861 int finish_async(struct async *async)
864 return wait_or_whine(async->pid, "child process", 0);
866 void *ret = (void *)(intptr_t)(-1);
868 if (pthread_join(async->tid, &ret))
869 error("pthread_join failed");
870 return (int)(intptr_t)ret;
874 const char *find_hook(const char *name)
876 static struct strbuf path = STRBUF_INIT;
879 strbuf_git_path(&path, "hooks/%s", name);
880 if (access(path.buf, X_OK) < 0) {
881 #ifdef STRIP_EXTENSION
882 strbuf_addstr(&path, STRIP_EXTENSION);
883 if (access(path.buf, X_OK) >= 0)
891 int run_hook_ve(const char *const *env, const char *name, va_list args)
893 struct child_process hook = CHILD_PROCESS_INIT;
900 argv_array_push(&hook.args, p);
901 while ((p = va_arg(args, const char *)))
902 argv_array_push(&hook.args, p);
905 hook.stdout_to_stderr = 1;
907 return run_command(&hook);
910 int run_hook_le(const char *const *env, const char *name, ...)
915 va_start(args, name);
916 ret = run_hook_ve(env, name, args);
923 /* initialized by caller */
925 int type; /* POLLOUT or POLLIN */
937 /* returned by pump_io */
938 int error; /* 0 for success, otherwise errno */
944 static int pump_io_round(struct io_pump *slots, int nr, struct pollfd *pfd)
949 for (i = 0; i < nr; i++) {
950 struct io_pump *io = &slots[i];
953 pfd[pollsize].fd = io->fd;
954 pfd[pollsize].events = io->type;
955 io->pfd = &pfd[pollsize++];
961 if (poll(pfd, pollsize, -1) < 0) {
964 die_errno("poll failed");
967 for (i = 0; i < nr; i++) {
968 struct io_pump *io = &slots[i];
973 if (!(io->pfd->revents & (POLLOUT|POLLIN|POLLHUP|POLLERR|POLLNVAL)))
976 if (io->type == POLLOUT) {
977 ssize_t len = xwrite(io->fd,
978 io->u.out.buf, io->u.out.len);
984 io->u.out.buf += len;
985 io->u.out.len -= len;
986 if (!io->u.out.len) {
993 if (io->type == POLLIN) {
994 ssize_t len = strbuf_read_once(io->u.in.buf,
995 io->fd, io->u.in.hint);
1008 static int pump_io(struct io_pump *slots, int nr)
1013 for (i = 0; i < nr; i++)
1016 ALLOC_ARRAY(pfd, nr);
1017 while (pump_io_round(slots, nr, pfd))
1021 /* There may be multiple errno values, so just pick the first. */
1022 for (i = 0; i < nr; i++) {
1023 if (slots[i].error) {
1024 errno = slots[i].error;
1032 int pipe_command(struct child_process *cmd,
1033 const char *in, size_t in_len,
1034 struct strbuf *out, size_t out_hint,
1035 struct strbuf *err, size_t err_hint)
1037 struct io_pump io[3];
1047 if (start_command(cmd) < 0)
1051 io[nr].fd = cmd->in;
1052 io[nr].type = POLLOUT;
1053 io[nr].u.out.buf = in;
1054 io[nr].u.out.len = in_len;
1058 io[nr].fd = cmd->out;
1059 io[nr].type = POLLIN;
1060 io[nr].u.in.buf = out;
1061 io[nr].u.in.hint = out_hint;
1065 io[nr].fd = cmd->err;
1066 io[nr].type = POLLIN;
1067 io[nr].u.in.buf = err;
1068 io[nr].u.in.hint = err_hint;
1072 if (pump_io(io, nr) < 0) {
1073 finish_command(cmd); /* throw away exit code */
1077 return finish_command(cmd);
1083 GIT_CP_WAIT_CLEANUP,
1086 struct parallel_processes {
1092 get_next_task_fn get_next_task;
1093 start_failure_fn start_failure;
1094 task_finished_fn task_finished;
1097 enum child_state state;
1098 struct child_process process;
1103 * The struct pollfd is logically part of *children,
1104 * but the system call expects it as its own array.
1108 unsigned shutdown : 1;
1111 struct strbuf buffered_output; /* of finished children */
1114 static int default_start_failure(struct strbuf *out,
1121 static int default_task_finished(int result,
1129 static void kill_children(struct parallel_processes *pp, int signo)
1131 int i, n = pp->max_processes;
1133 for (i = 0; i < n; i++)
1134 if (pp->children[i].state == GIT_CP_WORKING)
1135 kill(pp->children[i].process.pid, signo);
1138 static struct parallel_processes *pp_for_signal;
1140 static void handle_children_on_signal(int signo)
1142 kill_children(pp_for_signal, signo);
1143 sigchain_pop(signo);
1147 static void pp_init(struct parallel_processes *pp,
1149 get_next_task_fn get_next_task,
1150 start_failure_fn start_failure,
1151 task_finished_fn task_finished,
1159 pp->max_processes = n;
1161 trace_printf("run_processes_parallel: preparing to run up to %d tasks", n);
1165 die("BUG: you need to specify a get_next_task function");
1166 pp->get_next_task = get_next_task;
1168 pp->start_failure = start_failure ? start_failure : default_start_failure;
1169 pp->task_finished = task_finished ? task_finished : default_task_finished;
1171 pp->nr_processes = 0;
1172 pp->output_owner = 0;
1174 pp->children = xcalloc(n, sizeof(*pp->children));
1175 pp->pfd = xcalloc(n, sizeof(*pp->pfd));
1176 strbuf_init(&pp->buffered_output, 0);
1178 for (i = 0; i < n; i++) {
1179 strbuf_init(&pp->children[i].err, 0);
1180 child_process_init(&pp->children[i].process);
1181 pp->pfd[i].events = POLLIN | POLLHUP;
1186 sigchain_push_common(handle_children_on_signal);
1189 static void pp_cleanup(struct parallel_processes *pp)
1193 trace_printf("run_processes_parallel: done");
1194 for (i = 0; i < pp->max_processes; i++) {
1195 strbuf_release(&pp->children[i].err);
1196 child_process_clear(&pp->children[i].process);
1203 * When get_next_task added messages to the buffer in its last
1204 * iteration, the buffered output is non empty.
1206 strbuf_write(&pp->buffered_output, stderr);
1207 strbuf_release(&pp->buffered_output);
1209 sigchain_pop_common();
1213 * 0 if a new task was started.
1214 * 1 if no new jobs was started (get_next_task ran out of work, non critical
1215 * problem with starting a new command)
1216 * <0 no new job was started, user wishes to shutdown early. Use negative code
1217 * to signal the children.
1219 static int pp_start_one(struct parallel_processes *pp)
1223 for (i = 0; i < pp->max_processes; i++)
1224 if (pp->children[i].state == GIT_CP_FREE)
1226 if (i == pp->max_processes)
1227 die("BUG: bookkeeping is hard");
1229 code = pp->get_next_task(&pp->children[i].process,
1230 &pp->children[i].err,
1232 &pp->children[i].data);
1234 strbuf_addbuf(&pp->buffered_output, &pp->children[i].err);
1235 strbuf_reset(&pp->children[i].err);
1238 pp->children[i].process.err = -1;
1239 pp->children[i].process.stdout_to_stderr = 1;
1240 pp->children[i].process.no_stdin = 1;
1242 if (start_command(&pp->children[i].process)) {
1243 code = pp->start_failure(&pp->children[i].err,
1245 &pp->children[i].data);
1246 strbuf_addbuf(&pp->buffered_output, &pp->children[i].err);
1247 strbuf_reset(&pp->children[i].err);
1254 pp->children[i].state = GIT_CP_WORKING;
1255 pp->pfd[i].fd = pp->children[i].process.err;
1259 static void pp_buffer_stderr(struct parallel_processes *pp, int output_timeout)
1263 while ((i = poll(pp->pfd, pp->max_processes, output_timeout)) < 0) {
1270 /* Buffer output from all pipes. */
1271 for (i = 0; i < pp->max_processes; i++) {
1272 if (pp->children[i].state == GIT_CP_WORKING &&
1273 pp->pfd[i].revents & (POLLIN | POLLHUP)) {
1274 int n = strbuf_read_once(&pp->children[i].err,
1275 pp->children[i].process.err, 0);
1277 close(pp->children[i].process.err);
1278 pp->children[i].state = GIT_CP_WAIT_CLEANUP;
1280 if (errno != EAGAIN)
1286 static void pp_output(struct parallel_processes *pp)
1288 int i = pp->output_owner;
1289 if (pp->children[i].state == GIT_CP_WORKING &&
1290 pp->children[i].err.len) {
1291 strbuf_write(&pp->children[i].err, stderr);
1292 strbuf_reset(&pp->children[i].err);
1296 static int pp_collect_finished(struct parallel_processes *pp)
1299 int n = pp->max_processes;
1302 while (pp->nr_processes > 0) {
1303 for (i = 0; i < pp->max_processes; i++)
1304 if (pp->children[i].state == GIT_CP_WAIT_CLEANUP)
1306 if (i == pp->max_processes)
1309 code = finish_command(&pp->children[i].process);
1311 code = pp->task_finished(code,
1312 &pp->children[i].err, pp->data,
1313 &pp->children[i].data);
1321 pp->children[i].state = GIT_CP_FREE;
1323 child_process_init(&pp->children[i].process);
1325 if (i != pp->output_owner) {
1326 strbuf_addbuf(&pp->buffered_output, &pp->children[i].err);
1327 strbuf_reset(&pp->children[i].err);
1329 strbuf_write(&pp->children[i].err, stderr);
1330 strbuf_reset(&pp->children[i].err);
1332 /* Output all other finished child processes */
1333 strbuf_write(&pp->buffered_output, stderr);
1334 strbuf_reset(&pp->buffered_output);
1337 * Pick next process to output live.
1339 * For now we pick it randomly by doing a round
1340 * robin. Later we may want to pick the one with
1341 * the most output or the longest or shortest
1342 * running process time.
1344 for (i = 0; i < n; i++)
1345 if (pp->children[(pp->output_owner + i) % n].state == GIT_CP_WORKING)
1347 pp->output_owner = (pp->output_owner + i) % n;
1353 int run_processes_parallel(int n,
1354 get_next_task_fn get_next_task,
1355 start_failure_fn start_failure,
1356 task_finished_fn task_finished,
1360 int output_timeout = 100;
1362 struct parallel_processes pp;
1364 pp_init(&pp, n, get_next_task, start_failure, task_finished, pp_cb);
1367 i < spawn_cap && !pp.shutdown &&
1368 pp.nr_processes < pp.max_processes;
1370 code = pp_start_one(&pp);
1375 kill_children(&pp, -code);
1379 if (!pp.nr_processes)
1381 pp_buffer_stderr(&pp, output_timeout);
1383 code = pp_collect_finished(&pp);
1387 kill_children(&pp, -code);