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 execv_shell_cmd(const char **argv
)
226 struct argv_array nargv
= ARGV_ARRAY_INIT
;
227 prepare_shell_cmd(&nargv
, argv
);
228 trace_argv_printf(nargv
.argv
, "trace: exec:");
229 sane_execvp(nargv
.argv
[0], (char **)nargv
.argv
);
230 argv_array_clear(&nargv
);
235 #ifndef GIT_WINDOWS_NATIVE
236 static int child_notifier
= -1;
238 static void notify_parent(void)
241 * execvp failed. If possible, we'd like to let start_command
242 * know, so failures like ENOENT can be handled right away; but
243 * otherwise, finish_command will still report the error.
245 xwrite(child_notifier
, "", 1);
249 static inline void set_cloexec(int fd
)
251 int flags
= fcntl(fd
, F_GETFD
);
253 fcntl(fd
, F_SETFD
, flags
| FD_CLOEXEC
);
256 static int wait_or_whine(pid_t pid
, const char *argv0
, int in_signal
)
258 int status
, code
= -1;
260 int failed_errno
= 0;
262 while ((waiting
= waitpid(pid
, &status
, 0)) < 0 && errno
== EINTR
)
268 failed_errno
= errno
;
269 error_errno("waitpid for %s failed", argv0
);
270 } else if (waiting
!= pid
) {
271 error("waitpid is confused (%s)", argv0
);
272 } else if (WIFSIGNALED(status
)) {
273 code
= WTERMSIG(status
);
274 if (code
!= SIGINT
&& code
!= SIGQUIT
&& code
!= SIGPIPE
)
275 error("%s died of signal %d", argv0
, code
);
277 * This return value is chosen so that code & 0xff
278 * mimics the exit code that a POSIX shell would report for
279 * a program that died from this signal.
282 } else if (WIFEXITED(status
)) {
283 code
= WEXITSTATUS(status
);
285 * Convert special exit code when execvp failed.
289 failed_errno
= ENOENT
;
292 error("waitpid is confused (%s)", argv0
);
295 clear_child_for_cleanup(pid
);
297 errno
= failed_errno
;
301 int start_command(struct child_process
*cmd
)
303 int need_in
, need_out
, need_err
;
304 int fdin
[2], fdout
[2], fderr
[2];
309 cmd
->argv
= cmd
->args
.argv
;
311 cmd
->env
= cmd
->env_array
.argv
;
314 * In case of errors we must keep the promise to close FDs
315 * that have been passed in via ->in and ->out.
318 need_in
= !cmd
->no_stdin
&& cmd
->in
< 0;
320 if (pipe(fdin
) < 0) {
321 failed_errno
= errno
;
324 str
= "standard input";
330 need_out
= !cmd
->no_stdout
331 && !cmd
->stdout_to_stderr
334 if (pipe(fdout
) < 0) {
335 failed_errno
= errno
;
340 str
= "standard output";
346 need_err
= !cmd
->no_stderr
&& cmd
->err
< 0;
348 if (pipe(fderr
) < 0) {
349 failed_errno
= errno
;
358 str
= "standard error";
360 error("cannot create %s pipe for %s: %s",
361 str
, cmd
->argv
[0], strerror(failed_errno
));
362 child_process_clear(cmd
);
363 errno
= failed_errno
;
369 trace_argv_printf(cmd
->argv
, "trace: run_command:");
372 #ifndef GIT_WINDOWS_NATIVE
375 if (pipe(notify_pipe
))
376 notify_pipe
[0] = notify_pipe
[1] = -1;
379 failed_errno
= errno
;
382 * Redirect the channel to write syscall error messages to
383 * before redirecting the process's stderr so that all die()
384 * in subsequent call paths use the parent's stderr.
386 if (cmd
->no_stderr
|| need_err
) {
387 int child_err
= dup(2);
388 set_cloexec(child_err
);
389 set_error_handle(fdopen(child_err
, "w"));
392 close(notify_pipe
[0]);
393 set_cloexec(notify_pipe
[1]);
394 child_notifier
= notify_pipe
[1];
395 atexit(notify_parent
);
402 } else if (cmd
->in
) {
412 } else if (cmd
->err
> 1) {
419 else if (cmd
->stdout_to_stderr
)
424 } else if (cmd
->out
> 1) {
429 if (cmd
->dir
&& chdir(cmd
->dir
))
430 die_errno("exec '%s': cd to '%s' failed", cmd
->argv
[0],
433 for (; *cmd
->env
; cmd
->env
++) {
434 if (strchr(*cmd
->env
, '='))
435 putenv((char *)*cmd
->env
);
441 execv_git_cmd(cmd
->argv
);
442 else if (cmd
->use_shell
)
443 execv_shell_cmd(cmd
->argv
);
445 sane_execvp(cmd
->argv
[0], (char *const*) cmd
->argv
);
446 if (errno
== ENOENT
) {
447 if (!cmd
->silent_exec_failure
)
448 error("cannot run %s: %s", cmd
->argv
[0],
452 die_errno("cannot exec '%s'", cmd
->argv
[0]);
456 error_errno("cannot fork() for %s", cmd
->argv
[0]);
457 else if (cmd
->clean_on_exit
)
458 mark_child_for_cleanup(cmd
->pid
, cmd
);
461 * Wait for child's execvp. If the execvp succeeds (or if fork()
462 * failed), EOF is seen immediately by the parent. Otherwise, the
463 * child process sends a single byte.
464 * Note that use of this infrastructure is completely advisory,
465 * therefore, we keep error checks minimal.
467 close(notify_pipe
[1]);
468 if (read(notify_pipe
[0], ¬ify_pipe
[1], 1) == 1) {
470 * At this point we know that fork() succeeded, but execvp()
471 * failed. Errors have been reported to our stderr.
473 wait_or_whine(cmd
->pid
, cmd
->argv
[0], 0);
474 failed_errno
= errno
;
477 close(notify_pipe
[0]);
481 int fhin
= 0, fhout
= 1, fherr
= 2;
482 const char **sargv
= cmd
->argv
;
483 struct argv_array nargv
= ARGV_ARRAY_INIT
;
486 fhin
= open("/dev/null", O_RDWR
);
493 fherr
= open("/dev/null", O_RDWR
);
495 fherr
= dup(fderr
[1]);
496 else if (cmd
->err
> 2)
497 fherr
= dup(cmd
->err
);
500 fhout
= open("/dev/null", O_RDWR
);
501 else if (cmd
->stdout_to_stderr
)
504 fhout
= dup(fdout
[1]);
505 else if (cmd
->out
> 1)
506 fhout
= dup(cmd
->out
);
509 cmd
->argv
= prepare_git_cmd(&nargv
, cmd
->argv
);
510 else if (cmd
->use_shell
)
511 cmd
->argv
= prepare_shell_cmd(&nargv
, cmd
->argv
);
513 cmd
->pid
= mingw_spawnvpe(cmd
->argv
[0], cmd
->argv
, (char**) cmd
->env
,
514 cmd
->dir
, fhin
, fhout
, fherr
);
515 failed_errno
= errno
;
516 if (cmd
->pid
< 0 && (!cmd
->silent_exec_failure
|| errno
!= ENOENT
))
517 error_errno("cannot spawn %s", cmd
->argv
[0]);
518 if (cmd
->clean_on_exit
&& cmd
->pid
>= 0)
519 mark_child_for_cleanup(cmd
->pid
, cmd
);
521 argv_array_clear(&nargv
);
545 child_process_clear(cmd
);
546 errno
= failed_errno
;
568 int finish_command(struct child_process
*cmd
)
570 int ret
= wait_or_whine(cmd
->pid
, cmd
->argv
[0], 0);
571 child_process_clear(cmd
);
575 int finish_command_in_signal(struct child_process
*cmd
)
577 return wait_or_whine(cmd
->pid
, cmd
->argv
[0], 1);
581 int run_command(struct child_process
*cmd
)
585 if (cmd
->out
< 0 || cmd
->err
< 0)
586 die("BUG: run_command with a pipe can cause deadlock");
588 code
= start_command(cmd
);
591 return finish_command(cmd
);
594 int run_command_v_opt(const char **argv
, int opt
)
596 return run_command_v_opt_cd_env(argv
, opt
, NULL
, NULL
);
599 int run_command_v_opt_cd_env(const char **argv
, int opt
, const char *dir
, const char *const *env
)
601 struct child_process cmd
= CHILD_PROCESS_INIT
;
603 cmd
.no_stdin
= opt
& RUN_COMMAND_NO_STDIN
? 1 : 0;
604 cmd
.git_cmd
= opt
& RUN_GIT_CMD
? 1 : 0;
605 cmd
.stdout_to_stderr
= opt
& RUN_COMMAND_STDOUT_TO_STDERR
? 1 : 0;
606 cmd
.silent_exec_failure
= opt
& RUN_SILENT_EXEC_FAILURE
? 1 : 0;
607 cmd
.use_shell
= opt
& RUN_USING_SHELL
? 1 : 0;
608 cmd
.clean_on_exit
= opt
& RUN_CLEAN_ON_EXIT
? 1 : 0;
611 return run_command(&cmd
);
615 static pthread_t main_thread
;
616 static int main_thread_set
;
617 static pthread_key_t async_key
;
618 static pthread_key_t async_die_counter
;
620 static void *run_thread(void *data
)
622 struct async
*async
= data
;
625 if (async
->isolate_sigpipe
) {
628 sigaddset(&mask
, SIGPIPE
);
629 if (pthread_sigmask(SIG_BLOCK
, &mask
, NULL
) < 0) {
630 ret
= error("unable to block SIGPIPE in async thread");
635 pthread_setspecific(async_key
, async
);
636 ret
= async
->proc(async
->proc_in
, async
->proc_out
, async
->data
);
640 static NORETURN
void die_async(const char *err
, va_list params
)
642 vreportf("fatal: ", err
, params
);
645 struct async
*async
= pthread_getspecific(async_key
);
646 if (async
->proc_in
>= 0)
647 close(async
->proc_in
);
648 if (async
->proc_out
>= 0)
649 close(async
->proc_out
);
650 pthread_exit((void *)128);
656 static int async_die_is_recursing(void)
658 void *ret
= pthread_getspecific(async_die_counter
);
659 pthread_setspecific(async_die_counter
, (void *)1);
665 if (!main_thread_set
)
666 return 0; /* no asyncs started yet */
667 return !pthread_equal(main_thread
, pthread_self());
670 static void NORETURN
async_exit(int code
)
672 pthread_exit((void *)(intptr_t)code
);
678 void (**handlers
)(void);
683 static int git_atexit_installed
;
685 static void git_atexit_dispatch(void)
689 for (i
=git_atexit_hdlrs
.nr
; i
; i
--)
690 git_atexit_hdlrs
.handlers
[i
-1]();
693 static void git_atexit_clear(void)
695 free(git_atexit_hdlrs
.handlers
);
696 memset(&git_atexit_hdlrs
, 0, sizeof(git_atexit_hdlrs
));
697 git_atexit_installed
= 0;
701 int git_atexit(void (*handler
)(void))
703 ALLOC_GROW(git_atexit_hdlrs
.handlers
, git_atexit_hdlrs
.nr
+ 1, git_atexit_hdlrs
.alloc
);
704 git_atexit_hdlrs
.handlers
[git_atexit_hdlrs
.nr
++] = handler
;
705 if (!git_atexit_installed
) {
706 if (atexit(&git_atexit_dispatch
))
708 git_atexit_installed
= 1;
712 #define atexit git_atexit
714 static int process_is_async
;
717 return process_is_async
;
720 static void NORETURN
async_exit(int code
)
727 void check_pipe(int err
)
733 signal(SIGPIPE
, SIG_DFL
);
735 /* Should never happen, but just in case... */
740 int start_async(struct async
*async
)
742 int need_in
, need_out
;
743 int fdin
[2], fdout
[2];
744 int proc_in
, proc_out
;
746 need_in
= async
->in
< 0;
748 if (pipe(fdin
) < 0) {
751 return error_errno("cannot create pipe");
756 need_out
= async
->out
< 0;
758 if (pipe(fdout
) < 0) {
763 return error_errno("cannot create pipe");
765 async
->out
= fdout
[0];
778 proc_out
= async
->out
;
783 /* Flush stdio before fork() to avoid cloning buffers */
787 if (async
->pid
< 0) {
788 error_errno("fork (async) failed");
797 process_is_async
= 1;
798 exit(!!async
->proc(proc_in
, proc_out
, async
->data
));
801 mark_child_for_cleanup(async
->pid
, NULL
);
813 if (!main_thread_set
) {
815 * We assume that the first time that start_async is called
816 * it is from the main thread.
819 main_thread
= pthread_self();
820 pthread_key_create(&async_key
, NULL
);
821 pthread_key_create(&async_die_counter
, NULL
);
822 set_die_routine(die_async
);
823 set_die_is_recursing_routine(async_die_is_recursing
);
827 set_cloexec(proc_in
);
829 set_cloexec(proc_out
);
830 async
->proc_in
= proc_in
;
831 async
->proc_out
= proc_out
;
833 int err
= pthread_create(&async
->tid
, NULL
, run_thread
, async
);
835 error_errno("cannot create thread");
855 int finish_async(struct async
*async
)
858 return wait_or_whine(async
->pid
, "child process", 0);
860 void *ret
= (void *)(intptr_t)(-1);
862 if (pthread_join(async
->tid
, &ret
))
863 error("pthread_join failed");
864 return (int)(intptr_t)ret
;
868 const char *find_hook(const char *name
)
870 static struct strbuf path
= STRBUF_INIT
;
873 strbuf_git_path(&path
, "hooks/%s", name
);
874 if (access(path
.buf
, X_OK
) < 0) {
875 #ifdef STRIP_EXTENSION
876 strbuf_addstr(&path
, STRIP_EXTENSION
);
877 if (access(path
.buf
, X_OK
) >= 0)
885 int run_hook_ve(const char *const *env
, const char *name
, va_list args
)
887 struct child_process hook
= CHILD_PROCESS_INIT
;
894 argv_array_push(&hook
.args
, p
);
895 while ((p
= va_arg(args
, const char *)))
896 argv_array_push(&hook
.args
, p
);
899 hook
.stdout_to_stderr
= 1;
901 return run_command(&hook
);
904 int run_hook_le(const char *const *env
, const char *name
, ...)
909 va_start(args
, name
);
910 ret
= run_hook_ve(env
, name
, args
);
917 /* initialized by caller */
919 int type
; /* POLLOUT or POLLIN */
931 /* returned by pump_io */
932 int error
; /* 0 for success, otherwise errno */
938 static int pump_io_round(struct io_pump
*slots
, int nr
, struct pollfd
*pfd
)
943 for (i
= 0; i
< nr
; i
++) {
944 struct io_pump
*io
= &slots
[i
];
947 pfd
[pollsize
].fd
= io
->fd
;
948 pfd
[pollsize
].events
= io
->type
;
949 io
->pfd
= &pfd
[pollsize
++];
955 if (poll(pfd
, pollsize
, -1) < 0) {
958 die_errno("poll failed");
961 for (i
= 0; i
< nr
; i
++) {
962 struct io_pump
*io
= &slots
[i
];
967 if (!(io
->pfd
->revents
& (POLLOUT
|POLLIN
|POLLHUP
|POLLERR
|POLLNVAL
)))
970 if (io
->type
== POLLOUT
) {
971 ssize_t len
= xwrite(io
->fd
,
972 io
->u
.out
.buf
, io
->u
.out
.len
);
978 io
->u
.out
.buf
+= len
;
979 io
->u
.out
.len
-= len
;
980 if (!io
->u
.out
.len
) {
987 if (io
->type
== POLLIN
) {
988 ssize_t len
= strbuf_read_once(io
->u
.in
.buf
,
989 io
->fd
, io
->u
.in
.hint
);
1002 static int pump_io(struct io_pump
*slots
, int nr
)
1007 for (i
= 0; i
< nr
; i
++)
1010 ALLOC_ARRAY(pfd
, nr
);
1011 while (pump_io_round(slots
, nr
, pfd
))
1015 /* There may be multiple errno values, so just pick the first. */
1016 for (i
= 0; i
< nr
; i
++) {
1017 if (slots
[i
].error
) {
1018 errno
= slots
[i
].error
;
1026 int pipe_command(struct child_process
*cmd
,
1027 const char *in
, size_t in_len
,
1028 struct strbuf
*out
, size_t out_hint
,
1029 struct strbuf
*err
, size_t err_hint
)
1031 struct io_pump io
[3];
1041 if (start_command(cmd
) < 0)
1045 io
[nr
].fd
= cmd
->in
;
1046 io
[nr
].type
= POLLOUT
;
1047 io
[nr
].u
.out
.buf
= in
;
1048 io
[nr
].u
.out
.len
= in_len
;
1052 io
[nr
].fd
= cmd
->out
;
1053 io
[nr
].type
= POLLIN
;
1054 io
[nr
].u
.in
.buf
= out
;
1055 io
[nr
].u
.in
.hint
= out_hint
;
1059 io
[nr
].fd
= cmd
->err
;
1060 io
[nr
].type
= POLLIN
;
1061 io
[nr
].u
.in
.buf
= err
;
1062 io
[nr
].u
.in
.hint
= err_hint
;
1066 if (pump_io(io
, nr
) < 0) {
1067 finish_command(cmd
); /* throw away exit code */
1071 return finish_command(cmd
);
1077 GIT_CP_WAIT_CLEANUP
,
1080 struct parallel_processes
{
1086 get_next_task_fn get_next_task
;
1087 start_failure_fn start_failure
;
1088 task_finished_fn task_finished
;
1091 enum child_state state
;
1092 struct child_process process
;
1097 * The struct pollfd is logically part of *children,
1098 * but the system call expects it as its own array.
1102 unsigned shutdown
: 1;
1105 struct strbuf buffered_output
; /* of finished children */
1108 static int default_start_failure(struct strbuf
*out
,
1115 static int default_task_finished(int result
,
1123 static void kill_children(struct parallel_processes
*pp
, int signo
)
1125 int i
, n
= pp
->max_processes
;
1127 for (i
= 0; i
< n
; i
++)
1128 if (pp
->children
[i
].state
== GIT_CP_WORKING
)
1129 kill(pp
->children
[i
].process
.pid
, signo
);
1132 static struct parallel_processes
*pp_for_signal
;
1134 static void handle_children_on_signal(int signo
)
1136 kill_children(pp_for_signal
, signo
);
1137 sigchain_pop(signo
);
1141 static void pp_init(struct parallel_processes
*pp
,
1143 get_next_task_fn get_next_task
,
1144 start_failure_fn start_failure
,
1145 task_finished_fn task_finished
,
1153 pp
->max_processes
= n
;
1155 trace_printf("run_processes_parallel: preparing to run up to %d tasks", n
);
1159 die("BUG: you need to specify a get_next_task function");
1160 pp
->get_next_task
= get_next_task
;
1162 pp
->start_failure
= start_failure
? start_failure
: default_start_failure
;
1163 pp
->task_finished
= task_finished
? task_finished
: default_task_finished
;
1165 pp
->nr_processes
= 0;
1166 pp
->output_owner
= 0;
1168 pp
->children
= xcalloc(n
, sizeof(*pp
->children
));
1169 pp
->pfd
= xcalloc(n
, sizeof(*pp
->pfd
));
1170 strbuf_init(&pp
->buffered_output
, 0);
1172 for (i
= 0; i
< n
; i
++) {
1173 strbuf_init(&pp
->children
[i
].err
, 0);
1174 child_process_init(&pp
->children
[i
].process
);
1175 pp
->pfd
[i
].events
= POLLIN
| POLLHUP
;
1180 sigchain_push_common(handle_children_on_signal
);
1183 static void pp_cleanup(struct parallel_processes
*pp
)
1187 trace_printf("run_processes_parallel: done");
1188 for (i
= 0; i
< pp
->max_processes
; i
++) {
1189 strbuf_release(&pp
->children
[i
].err
);
1190 child_process_clear(&pp
->children
[i
].process
);
1197 * When get_next_task added messages to the buffer in its last
1198 * iteration, the buffered output is non empty.
1200 strbuf_write(&pp
->buffered_output
, stderr
);
1201 strbuf_release(&pp
->buffered_output
);
1203 sigchain_pop_common();
1207 * 0 if a new task was started.
1208 * 1 if no new jobs was started (get_next_task ran out of work, non critical
1209 * problem with starting a new command)
1210 * <0 no new job was started, user wishes to shutdown early. Use negative code
1211 * to signal the children.
1213 static int pp_start_one(struct parallel_processes
*pp
)
1217 for (i
= 0; i
< pp
->max_processes
; i
++)
1218 if (pp
->children
[i
].state
== GIT_CP_FREE
)
1220 if (i
== pp
->max_processes
)
1221 die("BUG: bookkeeping is hard");
1223 code
= pp
->get_next_task(&pp
->children
[i
].process
,
1224 &pp
->children
[i
].err
,
1226 &pp
->children
[i
].data
);
1228 strbuf_addbuf(&pp
->buffered_output
, &pp
->children
[i
].err
);
1229 strbuf_reset(&pp
->children
[i
].err
);
1232 pp
->children
[i
].process
.err
= -1;
1233 pp
->children
[i
].process
.stdout_to_stderr
= 1;
1234 pp
->children
[i
].process
.no_stdin
= 1;
1236 if (start_command(&pp
->children
[i
].process
)) {
1237 code
= pp
->start_failure(&pp
->children
[i
].err
,
1239 &pp
->children
[i
].data
);
1240 strbuf_addbuf(&pp
->buffered_output
, &pp
->children
[i
].err
);
1241 strbuf_reset(&pp
->children
[i
].err
);
1248 pp
->children
[i
].state
= GIT_CP_WORKING
;
1249 pp
->pfd
[i
].fd
= pp
->children
[i
].process
.err
;
1253 static void pp_buffer_stderr(struct parallel_processes
*pp
, int output_timeout
)
1257 while ((i
= poll(pp
->pfd
, pp
->max_processes
, output_timeout
)) < 0) {
1264 /* Buffer output from all pipes. */
1265 for (i
= 0; i
< pp
->max_processes
; i
++) {
1266 if (pp
->children
[i
].state
== GIT_CP_WORKING
&&
1267 pp
->pfd
[i
].revents
& (POLLIN
| POLLHUP
)) {
1268 int n
= strbuf_read_once(&pp
->children
[i
].err
,
1269 pp
->children
[i
].process
.err
, 0);
1271 close(pp
->children
[i
].process
.err
);
1272 pp
->children
[i
].state
= GIT_CP_WAIT_CLEANUP
;
1274 if (errno
!= EAGAIN
)
1280 static void pp_output(struct parallel_processes
*pp
)
1282 int i
= pp
->output_owner
;
1283 if (pp
->children
[i
].state
== GIT_CP_WORKING
&&
1284 pp
->children
[i
].err
.len
) {
1285 strbuf_write(&pp
->children
[i
].err
, stderr
);
1286 strbuf_reset(&pp
->children
[i
].err
);
1290 static int pp_collect_finished(struct parallel_processes
*pp
)
1293 int n
= pp
->max_processes
;
1296 while (pp
->nr_processes
> 0) {
1297 for (i
= 0; i
< pp
->max_processes
; i
++)
1298 if (pp
->children
[i
].state
== GIT_CP_WAIT_CLEANUP
)
1300 if (i
== pp
->max_processes
)
1303 code
= finish_command(&pp
->children
[i
].process
);
1305 code
= pp
->task_finished(code
,
1306 &pp
->children
[i
].err
, pp
->data
,
1307 &pp
->children
[i
].data
);
1315 pp
->children
[i
].state
= GIT_CP_FREE
;
1317 child_process_init(&pp
->children
[i
].process
);
1319 if (i
!= pp
->output_owner
) {
1320 strbuf_addbuf(&pp
->buffered_output
, &pp
->children
[i
].err
);
1321 strbuf_reset(&pp
->children
[i
].err
);
1323 strbuf_write(&pp
->children
[i
].err
, stderr
);
1324 strbuf_reset(&pp
->children
[i
].err
);
1326 /* Output all other finished child processes */
1327 strbuf_write(&pp
->buffered_output
, stderr
);
1328 strbuf_reset(&pp
->buffered_output
);
1331 * Pick next process to output live.
1333 * For now we pick it randomly by doing a round
1334 * robin. Later we may want to pick the one with
1335 * the most output or the longest or shortest
1336 * running process time.
1338 for (i
= 0; i
< n
; i
++)
1339 if (pp
->children
[(pp
->output_owner
+ i
) % n
].state
== GIT_CP_WORKING
)
1341 pp
->output_owner
= (pp
->output_owner
+ i
) % n
;
1347 int run_processes_parallel(int n
,
1348 get_next_task_fn get_next_task
,
1349 start_failure_fn start_failure
,
1350 task_finished_fn task_finished
,
1354 int output_timeout
= 100;
1356 struct parallel_processes pp
;
1358 pp_init(&pp
, n
, get_next_task
, start_failure
, task_finished
, pp_cb
);
1361 i
< spawn_cap
&& !pp
.shutdown
&&
1362 pp
.nr_processes
< pp
.max_processes
;
1364 code
= pp_start_one(&pp
);
1369 kill_children(&pp
, -code
);
1373 if (!pp
.nr_processes
)
1375 pp_buffer_stderr(&pp
, output_timeout
);
1377 code
= pp_collect_finished(&pp
);
1381 kill_children(&pp
, -code
);