run-command: expose is_executable function
[git/git.git] / run-command.c
CommitLineData
b1bf95bb
JW
1#include "cache.h"
2#include "run-command.h"
77cb17e9 3#include "exec_cmd.h"
afe19ff7 4#include "sigchain.h"
5d40a179 5#include "argv-array.h"
c553c72e
SB
6#include "thread-utils.h"
7#include "strbuf.h"
b1bf95bb 8
483bbd4e
RS
9void child_process_init(struct child_process *child)
10{
11 memset(child, 0, sizeof(*child));
12 argv_array_init(&child->args);
19a583dc 13 argv_array_init(&child->env_array);
483bbd4e
RS
14}
15
2d71608e
RS
16void child_process_clear(struct child_process *child)
17{
18 argv_array_clear(&child->args);
19 argv_array_clear(&child->env_array);
20}
21
afe19ff7
JK
22struct child_to_clean {
23 pid_t pid;
ac2fbaa6 24 struct child_process *process;
afe19ff7
JK
25 struct child_to_clean *next;
26};
27static struct child_to_clean *children_to_clean;
28static int installed_child_cleanup_handler;
29
507d7804 30static void cleanup_children(int sig, int in_signal)
afe19ff7 31{
46df6906
JK
32 struct child_to_clean *children_to_wait_for = NULL;
33
afe19ff7
JK
34 while (children_to_clean) {
35 struct child_to_clean *p = children_to_clean;
36 children_to_clean = p->next;
ac2fbaa6
LS
37
38 if (p->process && !in_signal) {
39 struct child_process *process = p->process;
40 if (process->clean_on_exit_handler) {
41 trace_printf(
42 "trace: run_command: running exit handler for pid %"
43 PRIuMAX, (uintmax_t)p->pid
44 );
45 process->clean_on_exit_handler(process);
46 }
47 }
48
afe19ff7 49 kill(p->pid, sig);
46df6906 50
7b91929b 51 if (p->process && p->process->wait_after_clean) {
46df6906
JK
52 p->next = children_to_wait_for;
53 children_to_wait_for = p;
54 } else {
55 if (!in_signal)
56 free(p);
57 }
58 }
59
60 while (children_to_wait_for) {
61 struct child_to_clean *p = children_to_wait_for;
62 children_to_wait_for = p->next;
63
64 while (waitpid(p->pid, NULL, 0) < 0 && errno == EINTR)
65 ; /* spin waiting for process exit or error */
66
507d7804
TI
67 if (!in_signal)
68 free(p);
afe19ff7
JK
69 }
70}
71
72static void cleanup_children_on_signal(int sig)
73{
507d7804 74 cleanup_children(sig, 1);
afe19ff7
JK
75 sigchain_pop(sig);
76 raise(sig);
77}
78
79static void cleanup_children_on_exit(void)
80{
507d7804 81 cleanup_children(SIGTERM, 0);
afe19ff7
JK
82}
83
ac2fbaa6 84static void mark_child_for_cleanup(pid_t pid, struct child_process *process)
afe19ff7
JK
85{
86 struct child_to_clean *p = xmalloc(sizeof(*p));
87 p->pid = pid;
ac2fbaa6 88 p->process = process;
afe19ff7
JK
89 p->next = children_to_clean;
90 children_to_clean = p;
91
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;
96 }
97}
98
99static void clear_child_for_cleanup(pid_t pid)
100{
bdee397d 101 struct child_to_clean **pp;
afe19ff7 102
bdee397d
DG
103 for (pp = &children_to_clean; *pp; pp = &(*pp)->next) {
104 struct child_to_clean *clean_me = *pp;
105
106 if (clean_me->pid == pid) {
107 *pp = clean_me->next;
108 free(clean_me);
afe19ff7
JK
109 return;
110 }
111 }
112}
113
9dc09c76
SP
114static inline void close_pair(int fd[2])
115{
116 close(fd[0]);
117 close(fd[1]);
118}
119
38124a40
BW
120int is_executable(const char *name)
121{
122 struct stat st;
123
124 if (stat(name, &st) || /* stat, not lstat */
125 !S_ISREG(st.st_mode))
126 return 0;
127
128#if defined(GIT_WINDOWS_NATIVE)
129 /*
130 * On Windows there is no executable bit. The file extension
131 * indicates whether it can be run as an executable, and Git
132 * has special-handling to detect scripts and launch them
133 * through the indicated script interpreter. We test for the
134 * file extension first because virus scanners may make
135 * it quite expensive to open many files.
136 */
137 if (ends_with(name, ".exe"))
138 return S_IXUSR;
139
140{
141 /*
142 * Now that we know it does not have an executable extension,
143 * peek into the file instead.
144 */
145 char buf[3] = { 0 };
146 int n;
147 int fd = open(name, O_RDONLY);
148 st.st_mode &= ~S_IXUSR;
149 if (fd >= 0) {
150 n = read(fd, buf, 2);
151 if (n == 2)
152 /* look for a she-bang */
153 if (!strcmp(buf, "#!"))
154 st.st_mode |= S_IXUSR;
155 close(fd);
156 }
157}
158#endif
159 return st.st_mode & S_IXUSR;
160}
161
38f865c2
JK
162static char *locate_in_PATH(const char *file)
163{
164 const char *p = getenv("PATH");
165 struct strbuf buf = STRBUF_INIT;
166
167 if (!p || !*p)
168 return NULL;
169
170 while (1) {
171 const char *end = strchrnul(p, ':');
172
173 strbuf_reset(&buf);
174
175 /* POSIX specifies an empty entry as the current directory. */
176 if (end != p) {
177 strbuf_add(&buf, p, end - p);
178 strbuf_addch(&buf, '/');
179 }
180 strbuf_addstr(&buf, file);
181
182 if (!access(buf.buf, F_OK))
183 return strbuf_detach(&buf, NULL);
184
185 if (!*end)
186 break;
187 p = end + 1;
188 }
189
190 strbuf_release(&buf);
191 return NULL;
192}
193
194static int exists_in_PATH(const char *file)
195{
196 char *r = locate_in_PATH(file);
197 free(r);
198 return r != NULL;
199}
200
201int sane_execvp(const char *file, char * const argv[])
202{
203 if (!execvp(file, argv))
204 return 0; /* cannot happen ;-) */
205
206 /*
207 * When a command can't be found because one of the directories
208 * listed in $PATH is unsearchable, execvp reports EACCES, but
209 * careful usability testing (read: analysis of occasional bug
210 * reports) reveals that "No such file or directory" is more
211 * intuitive.
212 *
213 * We avoid commands with "/", because execvp will not do $PATH
214 * lookups in that case.
215 *
216 * The reassignment of EACCES to errno looks like a no-op below,
217 * but we need to protect against exists_in_PATH overwriting errno.
218 */
219 if (errno == EACCES && !strchr(file, '/'))
220 errno = exists_in_PATH(file) ? EACCES : ENOENT;
a7855083
JH
221 else if (errno == ENOTDIR && !strchr(file, '/'))
222 errno = ENOENT;
38f865c2
JK
223 return -1;
224}
225
20574f55 226static const char **prepare_shell_cmd(struct argv_array *out, const char **argv)
8dba1e63 227{
20574f55 228 if (!argv[0])
8dba1e63
JK
229 die("BUG: shell command is empty");
230
f445644f 231 if (strcspn(argv[0], "|&;<>()$`\\\"' \t\n*?[#~=%") != strlen(argv[0])) {
380395d0 232#ifndef GIT_WINDOWS_NATIVE
20574f55 233 argv_array_push(out, SHELL_PATH);
77629754 234#else
20574f55 235 argv_array_push(out, "sh");
77629754 236#endif
20574f55 237 argv_array_push(out, "-c");
8dba1e63 238
20574f55
JK
239 /*
240 * If we have no extra arguments, we do not even need to
241 * bother with the "$@" magic.
242 */
243 if (!argv[1])
244 argv_array_push(out, argv[0]);
245 else
246 argv_array_pushf(out, "%s \"$@\"", argv[0]);
247 }
8dba1e63 248
20574f55
JK
249 argv_array_pushv(out, argv);
250 return out->argv;
8dba1e63
JK
251}
252
380395d0 253#ifndef GIT_WINDOWS_NATIVE
2b541bf8
JS
254static int child_notifier = -1;
255
79319b19
BW
256enum child_errcode {
257 CHILD_ERR_CHDIR,
53fa6753
BW
258 CHILD_ERR_DUP2,
259 CHILD_ERR_CLOSE,
45afb1ca 260 CHILD_ERR_SIGPROCMASK,
79319b19
BW
261 CHILD_ERR_ENOENT,
262 CHILD_ERR_SILENT,
263 CHILD_ERR_ERRNO
264};
265
266struct child_err {
267 enum child_errcode err;
268 int syserr; /* errno */
269};
270
271static void child_die(enum child_errcode err)
2b541bf8 272{
79319b19
BW
273 struct child_err buf;
274
275 buf.err = err;
276 buf.syserr = errno;
277
278 /* write(2) on buf smaller than PIPE_BUF (min 512) is atomic: */
279 xwrite(child_notifier, &buf, sizeof(buf));
280 _exit(1);
281}
282
53fa6753
BW
283static void child_dup2(int fd, int to)
284{
285 if (dup2(fd, to) < 0)
286 child_die(CHILD_ERR_DUP2);
287}
288
289static void child_close(int fd)
290{
291 if (close(fd))
292 child_die(CHILD_ERR_CLOSE);
293}
294
295static void child_close_pair(int fd[2])
296{
297 child_close(fd[0]);
298 child_close(fd[1]);
299}
300
79319b19
BW
301/*
302 * parent will make it look like the child spewed a fatal error and died
303 * this is needed to prevent changes to t0061.
304 */
305static void fake_fatal(const char *err, va_list params)
306{
307 vreportf("fatal: ", err, params);
308}
309
310static void child_error_fn(const char *err, va_list params)
311{
312 const char msg[] = "error() should not be called in child\n";
313 xwrite(2, msg, sizeof(msg) - 1);
314}
315
316static void child_warn_fn(const char *err, va_list params)
317{
318 const char msg[] = "warn() should not be called in child\n";
319 xwrite(2, msg, sizeof(msg) - 1);
320}
321
322static void NORETURN child_die_fn(const char *err, va_list params)
323{
324 const char msg[] = "die() should not be called in child\n";
325 xwrite(2, msg, sizeof(msg) - 1);
326 _exit(2);
327}
328
329/* this runs in the parent process */
330static void child_err_spew(struct child_process *cmd, struct child_err *cerr)
331{
332 static void (*old_errfn)(const char *err, va_list params);
333
334 old_errfn = get_error_routine();
335 set_error_routine(fake_fatal);
336 errno = cerr->syserr;
337
338 switch (cerr->err) {
339 case CHILD_ERR_CHDIR:
340 error_errno("exec '%s': cd to '%s' failed",
341 cmd->argv[0], cmd->dir);
342 break;
53fa6753
BW
343 case CHILD_ERR_DUP2:
344 error_errno("dup2() in child failed");
345 break;
346 case CHILD_ERR_CLOSE:
347 error_errno("close() in child failed");
348 break;
45afb1ca
EW
349 case CHILD_ERR_SIGPROCMASK:
350 error_errno("sigprocmask failed restoring signals");
351 break;
79319b19
BW
352 case CHILD_ERR_ENOENT:
353 error_errno("cannot run %s", cmd->argv[0]);
354 break;
355 case CHILD_ERR_SILENT:
356 break;
357 case CHILD_ERR_ERRNO:
358 error_errno("cannot exec '%s'", cmd->argv[0]);
359 break;
360 }
361 set_error_routine(old_errfn);
2b541bf8 362}
3967e25b
BW
363
364static void prepare_cmd(struct argv_array *out, const struct child_process *cmd)
365{
366 if (!cmd->argv[0])
367 die("BUG: command is empty");
368
e3a43446
BW
369 /*
370 * Add SHELL_PATH so in the event exec fails with ENOEXEC we can
371 * attempt to interpret the command with 'sh'.
372 */
373 argv_array_push(out, SHELL_PATH);
374
3967e25b
BW
375 if (cmd->git_cmd) {
376 argv_array_push(out, "git");
377 argv_array_pushv(out, cmd->argv);
378 } else if (cmd->use_shell) {
379 prepare_shell_cmd(out, cmd->argv);
380 } else {
381 argv_array_pushv(out, cmd->argv);
382 }
e3a43446
BW
383
384 /*
385 * If there are no '/' characters in the command then perform a path
386 * lookup and use the resolved path as the command to exec. If there
387 * are no '/' characters or if the command wasn't found in the path,
388 * have exec attempt to invoke the command directly.
389 */
390 if (!strchr(out->argv[1], '/')) {
391 char *program = locate_in_PATH(out->argv[1]);
392 if (program) {
393 free((char *)out->argv[1]);
394 out->argv[1] = program;
395 }
396 }
3967e25b 397}
ae25394b
BW
398
399static char **prep_childenv(const char *const *deltaenv)
400{
401 extern char **environ;
402 char **childenv;
403 struct string_list env = STRING_LIST_INIT_DUP;
404 struct strbuf key = STRBUF_INIT;
405 const char *const *p;
406 int i;
407
408 /* Construct a sorted string list consisting of the current environ */
409 for (p = (const char *const *) environ; p && *p; p++) {
410 const char *equals = strchr(*p, '=');
411
412 if (equals) {
413 strbuf_reset(&key);
414 strbuf_add(&key, *p, equals - *p);
415 string_list_append(&env, key.buf)->util = (void *) *p;
416 } else {
417 string_list_append(&env, *p)->util = (void *) *p;
418 }
419 }
420 string_list_sort(&env);
421
422 /* Merge in 'deltaenv' with the current environ */
423 for (p = deltaenv; p && *p; p++) {
424 const char *equals = strchr(*p, '=');
425
426 if (equals) {
427 /* ('key=value'), insert or replace entry */
428 strbuf_reset(&key);
429 strbuf_add(&key, *p, equals - *p);
430 string_list_insert(&env, key.buf)->util = (void *) *p;
431 } else {
432 /* otherwise ('key') remove existing entry */
433 string_list_remove(&env, *p, 0);
434 }
435 }
436
437 /* Create an array of 'char *' to be used as the childenv */
438 childenv = xmalloc((env.nr + 1) * sizeof(char *));
439 for (i = 0; i < env.nr; i++)
440 childenv[i] = env.items[i].util;
441 childenv[env.nr] = NULL;
442
443 string_list_clear(&env, 0);
444 strbuf_release(&key);
445 return childenv;
446}
45afb1ca
EW
447
448struct atfork_state {
449#ifndef NO_PTHREADS
450 int cs;
451#endif
452 sigset_t old;
453};
454
455#ifndef NO_PTHREADS
456static void bug_die(int err, const char *msg)
457{
458 if (err) {
459 errno = err;
460 die_errno("BUG: %s", msg);
461 }
462}
463#endif
464
465static void atfork_prepare(struct atfork_state *as)
466{
467 sigset_t all;
468
469 if (sigfillset(&all))
470 die_errno("sigfillset");
471#ifdef NO_PTHREADS
472 if (sigprocmask(SIG_SETMASK, &all, &as->old))
473 die_errno("sigprocmask");
474#else
475 bug_die(pthread_sigmask(SIG_SETMASK, &all, &as->old),
476 "blocking all signals");
477 bug_die(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &as->cs),
478 "disabling cancellation");
479#endif
480}
481
482static void atfork_parent(struct atfork_state *as)
483{
484#ifdef NO_PTHREADS
485 if (sigprocmask(SIG_SETMASK, &as->old, NULL))
486 die_errno("sigprocmask");
487#else
488 bug_die(pthread_setcancelstate(as->cs, NULL),
489 "re-enabling cancellation");
490 bug_die(pthread_sigmask(SIG_SETMASK, &as->old, NULL),
491 "restoring signal mask");
200a76b7 492#endif
45afb1ca
EW
493}
494#endif /* GIT_WINDOWS_NATIVE */
a5487ddf
JS
495
496static inline void set_cloexec(int fd)
497{
498 int flags = fcntl(fd, F_GETFD);
499 if (flags >= 0)
500 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
501}
a5487ddf 502
507d7804 503static int wait_or_whine(pid_t pid, const char *argv0, int in_signal)
ab0b41da
JS
504{
505 int status, code = -1;
506 pid_t waiting;
507 int failed_errno = 0;
508
509 while ((waiting = waitpid(pid, &status, 0)) < 0 && errno == EINTR)
510 ; /* nothing */
507d7804
TI
511 if (in_signal)
512 return 0;
ab0b41da
JS
513
514 if (waiting < 0) {
515 failed_errno = errno;
fbcb0e06 516 error_errno("waitpid for %s failed", argv0);
ab0b41da
JS
517 } else if (waiting != pid) {
518 error("waitpid is confused (%s)", argv0);
519 } else if (WIFSIGNALED(status)) {
520 code = WTERMSIG(status);
ac78663b 521 if (code != SIGINT && code != SIGQUIT && code != SIGPIPE)
a2767c5c 522 error("%s died of signal %d", argv0, code);
ab0b41da
JS
523 /*
524 * This return value is chosen so that code & 0xff
525 * mimics the exit code that a POSIX shell would report for
526 * a program that died from this signal.
527 */
709ca730 528 code += 128;
ab0b41da
JS
529 } else if (WIFEXITED(status)) {
530 code = WEXITSTATUS(status);
ab0b41da
JS
531 } else {
532 error("waitpid is confused (%s)", argv0);
533 }
afe19ff7
JK
534
535 clear_child_for_cleanup(pid);
536
ab0b41da
JS
537 errno = failed_errno;
538 return code;
539}
540
ebcb5d16 541int start_command(struct child_process *cmd)
b1bf95bb 542{
f3b33f1d
JS
543 int need_in, need_out, need_err;
544 int fdin[2], fdout[2], fderr[2];
25043d8a 545 int failed_errno;
939296c4 546 char *str;
4919bf03 547
c460c0ec
JK
548 if (!cmd->argv)
549 cmd->argv = cmd->args.argv;
19a583dc
RS
550 if (!cmd->env)
551 cmd->env = cmd->env_array.argv;
c460c0ec 552
c20181e3
JS
553 /*
554 * In case of errors we must keep the promise to close FDs
555 * that have been passed in via ->in and ->out.
556 */
557
e4507ae8 558 need_in = !cmd->no_stdin && cmd->in < 0;
4919bf03 559 if (need_in) {
c20181e3 560 if (pipe(fdin) < 0) {
0ac77ec3 561 failed_errno = errno;
c20181e3
JS
562 if (cmd->out > 0)
563 close(cmd->out);
939296c4 564 str = "standard input";
0ac77ec3 565 goto fail_pipe;
c20181e3 566 }
4919bf03 567 cmd->in = fdin[1];
4919bf03
SP
568 }
569
e4507ae8
SP
570 need_out = !cmd->no_stdout
571 && !cmd->stdout_to_stderr
572 && cmd->out < 0;
f4bba25b
SP
573 if (need_out) {
574 if (pipe(fdout) < 0) {
0ac77ec3 575 failed_errno = errno;
f4bba25b
SP
576 if (need_in)
577 close_pair(fdin);
c20181e3
JS
578 else if (cmd->in)
579 close(cmd->in);
939296c4 580 str = "standard output";
0ac77ec3 581 goto fail_pipe;
f4bba25b
SP
582 }
583 cmd->out = fdout[0];
f4bba25b
SP
584 }
585
b73a4397 586 need_err = !cmd->no_stderr && cmd->err < 0;
f3b33f1d
JS
587 if (need_err) {
588 if (pipe(fderr) < 0) {
0ac77ec3 589 failed_errno = errno;
f3b33f1d
JS
590 if (need_in)
591 close_pair(fdin);
c20181e3
JS
592 else if (cmd->in)
593 close(cmd->in);
f3b33f1d
JS
594 if (need_out)
595 close_pair(fdout);
c20181e3
JS
596 else if (cmd->out)
597 close(cmd->out);
939296c4 598 str = "standard error";
0ac77ec3 599fail_pipe:
939296c4
SB
600 error("cannot create %s pipe for %s: %s",
601 str, cmd->argv[0], strerror(failed_errno));
2d71608e 602 child_process_clear(cmd);
0ac77ec3
JS
603 errno = failed_errno;
604 return -1;
f3b33f1d
JS
605 }
606 cmd->err = fderr[0];
607 }
608
8852f5d7 609 trace_argv_printf(cmd->argv, "trace: run_command:");
13af8cbd 610 fflush(NULL);
8852f5d7 611
380395d0 612#ifndef GIT_WINDOWS_NATIVE
2b541bf8
JS
613{
614 int notify_pipe[2];
db015a28 615 int null_fd = -1;
ae25394b 616 char **childenv;
3967e25b 617 struct argv_array argv = ARGV_ARRAY_INIT;
79319b19 618 struct child_err cerr;
45afb1ca 619 struct atfork_state as;
3967e25b 620
2b541bf8
JS
621 if (pipe(notify_pipe))
622 notify_pipe[0] = notify_pipe[1] = -1;
623
db015a28
BW
624 if (cmd->no_stdin || cmd->no_stdout || cmd->no_stderr) {
625 null_fd = open("/dev/null", O_RDWR | O_CLOEXEC);
626 if (null_fd < 0)
627 die_errno(_("open /dev/null failed"));
628 set_cloexec(null_fd);
629 }
630
3967e25b 631 prepare_cmd(&argv, cmd);
ae25394b 632 childenv = prep_childenv(cmd->env);
45afb1ca 633 atfork_prepare(&as);
3967e25b 634
e503cd6e
BW
635 /*
636 * NOTE: In order to prevent deadlocking when using threads special
637 * care should be taken with the function calls made in between the
638 * fork() and exec() calls. No calls should be made to functions which
639 * require acquiring a lock (e.g. malloc) as the lock could have been
640 * held by another thread at the time of forking, causing the lock to
641 * never be released in the child process. This means only
642 * Async-Signal-Safe functions are permitted in the child.
643 */
ebcb5d16 644 cmd->pid = fork();
25043d8a 645 failed_errno = errno;
ebcb5d16 646 if (!cmd->pid) {
45afb1ca 647 int sig;
a5487ddf 648 /*
79319b19
BW
649 * Ensure the default die/error/warn routines do not get
650 * called, they can take stdio locks and malloc.
a5487ddf 651 */
79319b19
BW
652 set_die_routine(child_die_fn);
653 set_error_routine(child_error_fn);
654 set_warn_routine(child_warn_fn);
a5487ddf 655
2b541bf8
JS
656 close(notify_pipe[0]);
657 set_cloexec(notify_pipe[1]);
658 child_notifier = notify_pipe[1];
2b541bf8 659
e4507ae8 660 if (cmd->no_stdin)
53fa6753 661 child_dup2(null_fd, 0);
e4507ae8 662 else if (need_in) {
53fa6753
BW
663 child_dup2(fdin[0], 0);
664 child_close_pair(fdin);
4919bf03 665 } else if (cmd->in) {
53fa6753
BW
666 child_dup2(cmd->in, 0);
667 child_close(cmd->in);
95d3c4f5 668 }
4919bf03 669
ce2cf27a 670 if (cmd->no_stderr)
53fa6753 671 child_dup2(null_fd, 2);
ce2cf27a 672 else if (need_err) {
53fa6753
BW
673 child_dup2(fderr[1], 2);
674 child_close_pair(fderr);
4f41b611 675 } else if (cmd->err > 1) {
53fa6753
BW
676 child_dup2(cmd->err, 2);
677 child_close(cmd->err);
ce2cf27a
CC
678 }
679
e4507ae8 680 if (cmd->no_stdout)
53fa6753 681 child_dup2(null_fd, 1);
e4507ae8 682 else if (cmd->stdout_to_stderr)
53fa6753 683 child_dup2(2, 1);
f4bba25b 684 else if (need_out) {
53fa6753
BW
685 child_dup2(fdout[1], 1);
686 child_close_pair(fdout);
f4bba25b 687 } else if (cmd->out > 1) {
53fa6753
BW
688 child_dup2(cmd->out, 1);
689 child_close(cmd->out);
f4bba25b
SP
690 }
691
1568fea0 692 if (cmd->dir && chdir(cmd->dir))
79319b19 693 child_die(CHILD_ERR_CHDIR);
3967e25b 694
45afb1ca
EW
695 /*
696 * restore default signal handlers here, in case
697 * we catch a signal right before execve below
698 */
699 for (sig = 1; sig < NSIG; sig++) {
700 /* ignored signals get reset to SIG_DFL on execve */
701 if (signal(sig, SIG_DFL) == SIG_IGN)
702 signal(sig, SIG_IGN);
703 }
704
705 if (sigprocmask(SIG_SETMASK, &as.old, NULL) != 0)
706 child_die(CHILD_ERR_SIGPROCMASK);
707
e3a43446
BW
708 /*
709 * Attempt to exec using the command and arguments starting at
710 * argv.argv[1]. argv.argv[0] contains SHELL_PATH which will
711 * be used in the event exec failed with ENOEXEC at which point
712 * we will try to interpret the command using 'sh'.
713 */
ae25394b
BW
714 execve(argv.argv[1], (char *const *) argv.argv + 1,
715 (char *const *) childenv);
e3a43446 716 if (errno == ENOEXEC)
ae25394b
BW
717 execve(argv.argv[0], (char *const *) argv.argv,
718 (char *const *) childenv);
3967e25b 719
fc1b56f0 720 if (errno == ENOENT) {
79319b19
BW
721 if (cmd->silent_exec_failure)
722 child_die(CHILD_ERR_SILENT);
723 child_die(CHILD_ERR_ENOENT);
fc1b56f0 724 } else {
79319b19 725 child_die(CHILD_ERR_ERRNO);
fc1b56f0 726 }
b1bf95bb 727 }
45afb1ca 728 atfork_parent(&as);
0ac77ec3 729 if (cmd->pid < 0)
fbcb0e06 730 error_errno("cannot fork() for %s", cmd->argv[0]);
afe19ff7 731 else if (cmd->clean_on_exit)
ac2fbaa6 732 mark_child_for_cleanup(cmd->pid, cmd);
2b541bf8
JS
733
734 /*
3967e25b 735 * Wait for child's exec. If the exec succeeds (or if fork()
2b541bf8 736 * failed), EOF is seen immediately by the parent. Otherwise, the
79319b19 737 * child process sends a child_err struct.
2b541bf8
JS
738 * Note that use of this infrastructure is completely advisory,
739 * therefore, we keep error checks minimal.
740 */
741 close(notify_pipe[1]);
79319b19 742 if (xread(notify_pipe[0], &cerr, sizeof(cerr)) == sizeof(cerr)) {
2b541bf8 743 /*
3967e25b 744 * At this point we know that fork() succeeded, but exec()
2b541bf8
JS
745 * failed. Errors have been reported to our stderr.
746 */
507d7804 747 wait_or_whine(cmd->pid, cmd->argv[0], 0);
79319b19 748 child_err_spew(cmd, &cerr);
2b541bf8
JS
749 failed_errno = errno;
750 cmd->pid = -1;
751 }
752 close(notify_pipe[0]);
3967e25b 753
db015a28
BW
754 if (null_fd >= 0)
755 close(null_fd);
3967e25b 756 argv_array_clear(&argv);
ae25394b 757 free(childenv);
2b541bf8 758}
ba26f296 759#else
0d30ad71 760{
75301f90 761 int fhin = 0, fhout = 1, fherr = 2;
108ac313 762 const char **sargv = cmd->argv;
20574f55 763 struct argv_array nargv = ARGV_ARRAY_INIT;
ba26f296 764
75301f90
JS
765 if (cmd->no_stdin)
766 fhin = open("/dev/null", O_RDWR);
767 else if (need_in)
768 fhin = dup(fdin[0]);
769 else if (cmd->in)
770 fhin = dup(cmd->in);
771
772 if (cmd->no_stderr)
773 fherr = open("/dev/null", O_RDWR);
774 else if (need_err)
775 fherr = dup(fderr[1]);
76d44c8c
JH
776 else if (cmd->err > 2)
777 fherr = dup(cmd->err);
75301f90
JS
778
779 if (cmd->no_stdout)
780 fhout = open("/dev/null", O_RDWR);
781 else if (cmd->stdout_to_stderr)
782 fhout = dup(fherr);
783 else if (need_out)
784 fhout = dup(fdout[1]);
785 else if (cmd->out > 1)
786 fhout = dup(cmd->out);
ba26f296 787
5a50085c 788 if (cmd->git_cmd)
20574f55 789 cmd->argv = prepare_git_cmd(&nargv, cmd->argv);
5a50085c 790 else if (cmd->use_shell)
20574f55 791 cmd->argv = prepare_shell_cmd(&nargv, cmd->argv);
ba26f296 792
77734da2
KB
793 cmd->pid = mingw_spawnvpe(cmd->argv[0], cmd->argv, (char**) cmd->env,
794 cmd->dir, fhin, fhout, fherr);
0ac77ec3 795 failed_errno = errno;
c024beb5 796 if (cmd->pid < 0 && (!cmd->silent_exec_failure || errno != ENOENT))
fbcb0e06 797 error_errno("cannot spawn %s", cmd->argv[0]);
afe19ff7 798 if (cmd->clean_on_exit && cmd->pid >= 0)
ac2fbaa6 799 mark_child_for_cleanup(cmd->pid, cmd);
ba26f296 800
20574f55 801 argv_array_clear(&nargv);
108ac313 802 cmd->argv = sargv;
75301f90
JS
803 if (fhin != 0)
804 close(fhin);
805 if (fhout != 1)
806 close(fhout);
807 if (fherr != 2)
808 close(fherr);
0d30ad71 809}
ba26f296
JS
810#endif
811
812 if (cmd->pid < 0) {
813 if (need_in)
814 close_pair(fdin);
815 else if (cmd->in)
816 close(cmd->in);
817 if (need_out)
818 close_pair(fdout);
819 else if (cmd->out)
820 close(cmd->out);
821 if (need_err)
822 close_pair(fderr);
fc012c28
D
823 else if (cmd->err)
824 close(cmd->err);
2d71608e 825 child_process_clear(cmd);
0ac77ec3
JS
826 errno = failed_errno;
827 return -1;
ba26f296 828 }
4919bf03
SP
829
830 if (need_in)
831 close(fdin[0]);
832 else if (cmd->in)
833 close(cmd->in);
834
f4bba25b
SP
835 if (need_out)
836 close(fdout[1]);
c20181e3 837 else if (cmd->out)
f4bba25b
SP
838 close(cmd->out);
839
f3b33f1d
JS
840 if (need_err)
841 close(fderr[1]);
4f41b611
SP
842 else if (cmd->err)
843 close(cmd->err);
f3b33f1d 844
ebcb5d16
SP
845 return 0;
846}
847
2d22c208
JS
848int finish_command(struct child_process *cmd)
849{
507d7804 850 int ret = wait_or_whine(cmd->pid, cmd->argv[0], 0);
2d71608e 851 child_process_clear(cmd);
c460c0ec 852 return ret;
2d22c208
JS
853}
854
507d7804
TI
855int finish_command_in_signal(struct child_process *cmd)
856{
857 return wait_or_whine(cmd->pid, cmd->argv[0], 1);
858}
859
860
ebcb5d16
SP
861int run_command(struct child_process *cmd)
862{
c29b3962
JK
863 int code;
864
865 if (cmd->out < 0 || cmd->err < 0)
866 die("BUG: run_command with a pipe can cause deadlock");
867
868 code = start_command(cmd);
ebcb5d16
SP
869 if (code)
870 return code;
871 return finish_command(cmd);
872}
873
f1000898
SP
874int run_command_v_opt(const char **argv, int opt)
875{
41e9bad7 876 return run_command_v_opt_cd_env(argv, opt, NULL, NULL);
1568fea0
AR
877}
878
ee493148
AR
879int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env)
880{
1f87293d
RS
881 struct child_process cmd = CHILD_PROCESS_INIT;
882 cmd.argv = argv;
883 cmd.no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
884 cmd.git_cmd = opt & RUN_GIT_CMD ? 1 : 0;
885 cmd.stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
886 cmd.silent_exec_failure = opt & RUN_SILENT_EXEC_FAILURE ? 1 : 0;
887 cmd.use_shell = opt & RUN_USING_SHELL ? 1 : 0;
888 cmd.clean_on_exit = opt & RUN_CLEAN_ON_EXIT ? 1 : 0;
ee493148
AR
889 cmd.dir = dir;
890 cmd.env = env;
891 return run_command(&cmd);
892}
2d22c208 893
f6b60983 894#ifndef NO_PTHREADS
0ea1c89b
JS
895static pthread_t main_thread;
896static int main_thread_set;
897static pthread_key_t async_key;
1ece66bc 898static pthread_key_t async_die_counter;
0ea1c89b 899
200a76b7 900static void *run_thread(void *data)
618ebe9f
JS
901{
902 struct async *async = data;
f6b60983 903 intptr_t ret;
0ea1c89b 904
c792d7b6
JK
905 if (async->isolate_sigpipe) {
906 sigset_t mask;
907 sigemptyset(&mask);
908 sigaddset(&mask, SIGPIPE);
909 if (pthread_sigmask(SIG_BLOCK, &mask, NULL) < 0) {
910 ret = error("unable to block SIGPIPE in async thread");
911 return (void *)ret;
912 }
913 }
914
0ea1c89b 915 pthread_setspecific(async_key, async);
f6b60983 916 ret = async->proc(async->proc_in, async->proc_out, async->data);
200a76b7 917 return (void *)ret;
618ebe9f 918}
0ea1c89b
JS
919
920static NORETURN void die_async(const char *err, va_list params)
921{
922 vreportf("fatal: ", err, params);
923
661a8cf4 924 if (in_async()) {
0ea1c89b
JS
925 struct async *async = pthread_getspecific(async_key);
926 if (async->proc_in >= 0)
927 close(async->proc_in);
928 if (async->proc_out >= 0)
929 close(async->proc_out);
930 pthread_exit((void *)128);
931 }
932
933 exit(128);
618ebe9f 934}
1ece66bc
JK
935
936static int async_die_is_recursing(void)
937{
938 void *ret = pthread_getspecific(async_die_counter);
939 pthread_setspecific(async_die_counter, (void *)1);
940 return ret != NULL;
941}
942
661a8cf4
JK
943int in_async(void)
944{
945 if (!main_thread_set)
946 return 0; /* no asyncs started yet */
947 return !pthread_equal(main_thread, pthread_self());
948}
949
b992fe10 950static void NORETURN async_exit(int code)
9658846c
JK
951{
952 pthread_exit((void *)(intptr_t)code);
953}
954
0f4b6db3
EB
955#else
956
957static struct {
958 void (**handlers)(void);
959 size_t nr;
960 size_t alloc;
961} git_atexit_hdlrs;
962
963static int git_atexit_installed;
964
6066a7ea 965static void git_atexit_dispatch(void)
0f4b6db3
EB
966{
967 size_t i;
968
969 for (i=git_atexit_hdlrs.nr ; i ; i--)
970 git_atexit_hdlrs.handlers[i-1]();
971}
972
6066a7ea 973static void git_atexit_clear(void)
0f4b6db3
EB
974{
975 free(git_atexit_hdlrs.handlers);
976 memset(&git_atexit_hdlrs, 0, sizeof(git_atexit_hdlrs));
977 git_atexit_installed = 0;
978}
979
980#undef atexit
981int git_atexit(void (*handler)(void))
982{
983 ALLOC_GROW(git_atexit_hdlrs.handlers, git_atexit_hdlrs.nr + 1, git_atexit_hdlrs.alloc);
984 git_atexit_hdlrs.handlers[git_atexit_hdlrs.nr++] = handler;
985 if (!git_atexit_installed) {
986 if (atexit(&git_atexit_dispatch))
987 return -1;
988 git_atexit_installed = 1;
989 }
990 return 0;
991}
992#define atexit git_atexit
993
661a8cf4
JK
994static int process_is_async;
995int in_async(void)
996{
997 return process_is_async;
998}
999
b992fe10 1000static void NORETURN async_exit(int code)
9658846c
JK
1001{
1002 exit(code);
1003}
1004
618ebe9f
JS
1005#endif
1006
b992fe10
LS
1007void check_pipe(int err)
1008{
1009 if (err == EPIPE) {
1010 if (in_async())
1011 async_exit(141);
1012
1013 signal(SIGPIPE, SIG_DFL);
1014 raise(SIGPIPE);
1015 /* Should never happen, but just in case... */
1016 exit(141);
1017 }
1018}
1019
2d22c208
JS
1020int start_async(struct async *async)
1021{
ae6a5609
EFL
1022 int need_in, need_out;
1023 int fdin[2], fdout[2];
1024 int proc_in, proc_out;
2d22c208 1025
ae6a5609
EFL
1026 need_in = async->in < 0;
1027 if (need_in) {
1028 if (pipe(fdin) < 0) {
1029 if (async->out > 0)
1030 close(async->out);
fbcb0e06 1031 return error_errno("cannot create pipe");
ae6a5609
EFL
1032 }
1033 async->in = fdin[1];
1034 }
1035
1036 need_out = async->out < 0;
1037 if (need_out) {
1038 if (pipe(fdout) < 0) {
1039 if (need_in)
1040 close_pair(fdin);
1041 else if (async->in)
1042 close(async->in);
fbcb0e06 1043 return error_errno("cannot create pipe");
ae6a5609
EFL
1044 }
1045 async->out = fdout[0];
1046 }
1047
1048 if (need_in)
1049 proc_in = fdin[0];
1050 else if (async->in)
1051 proc_in = async->in;
1052 else
1053 proc_in = -1;
1054
1055 if (need_out)
1056 proc_out = fdout[1];
1057 else if (async->out)
1058 proc_out = async->out;
1059 else
1060 proc_out = -1;
2d22c208 1061
f6b60983 1062#ifdef NO_PTHREADS
2c3766f0
AM
1063 /* Flush stdio before fork() to avoid cloning buffers */
1064 fflush(NULL);
1065
2d22c208
JS
1066 async->pid = fork();
1067 if (async->pid < 0) {
fbcb0e06 1068 error_errno("fork (async) failed");
ae6a5609 1069 goto error;
2d22c208
JS
1070 }
1071 if (!async->pid) {
ae6a5609
EFL
1072 if (need_in)
1073 close(fdin[1]);
1074 if (need_out)
1075 close(fdout[0]);
0f4b6db3 1076 git_atexit_clear();
661a8cf4 1077 process_is_async = 1;
ae6a5609 1078 exit(!!async->proc(proc_in, proc_out, async->data));
2d22c208 1079 }
ae6a5609 1080
ac2fbaa6 1081 mark_child_for_cleanup(async->pid, NULL);
afe19ff7 1082
ae6a5609
EFL
1083 if (need_in)
1084 close(fdin[0]);
1085 else if (async->in)
1086 close(async->in);
1087
1088 if (need_out)
1089 close(fdout[1]);
1090 else if (async->out)
1091 close(async->out);
618ebe9f 1092#else
0ea1c89b
JS
1093 if (!main_thread_set) {
1094 /*
1095 * We assume that the first time that start_async is called
1096 * it is from the main thread.
1097 */
1098 main_thread_set = 1;
1099 main_thread = pthread_self();
1100 pthread_key_create(&async_key, NULL);
1ece66bc 1101 pthread_key_create(&async_die_counter, NULL);
0ea1c89b 1102 set_die_routine(die_async);
1ece66bc 1103 set_die_is_recursing_routine(async_die_is_recursing);
0ea1c89b
JS
1104 }
1105
200a76b7
JS
1106 if (proc_in >= 0)
1107 set_cloexec(proc_in);
1108 if (proc_out >= 0)
1109 set_cloexec(proc_out);
ae6a5609
EFL
1110 async->proc_in = proc_in;
1111 async->proc_out = proc_out;
200a76b7
JS
1112 {
1113 int err = pthread_create(&async->tid, NULL, run_thread, async);
1114 if (err) {
fbcb0e06 1115 error_errno("cannot create thread");
200a76b7
JS
1116 goto error;
1117 }
618ebe9f
JS
1118 }
1119#endif
2d22c208 1120 return 0;
ae6a5609
EFL
1121
1122error:
1123 if (need_in)
1124 close_pair(fdin);
1125 else if (async->in)
1126 close(async->in);
1127
1128 if (need_out)
1129 close_pair(fdout);
1130 else if (async->out)
1131 close(async->out);
1132 return -1;
2d22c208
JS
1133}
1134
1135int finish_async(struct async *async)
1136{
f6b60983 1137#ifdef NO_PTHREADS
507d7804 1138 return wait_or_whine(async->pid, "child process", 0);
618ebe9f 1139#else
200a76b7
JS
1140 void *ret = (void *)(intptr_t)(-1);
1141
1142 if (pthread_join(async->tid, &ret))
1143 error("pthread_join failed");
1144 return (int)(intptr_t)ret;
618ebe9f 1145#endif
2d22c208 1146}
ae98a008 1147
dcf69262 1148const char *find_hook(const char *name)
5a7da2dc 1149{
03f2c773 1150 static struct strbuf path = STRBUF_INIT;
5a7da2dc 1151
03f2c773 1152 strbuf_reset(&path);
9445b492 1153 strbuf_git_path(&path, "hooks/%s", name);
235be51f
JS
1154 if (access(path.buf, X_OK) < 0) {
1155#ifdef STRIP_EXTENSION
1156 strbuf_addstr(&path, STRIP_EXTENSION);
1157 if (access(path.buf, X_OK) >= 0)
1158 return path.buf;
1159#endif
03f2c773 1160 return NULL;
235be51f 1161 }
03f2c773 1162 return path.buf;
5a7da2dc
AS
1163}
1164
15048f8a 1165int run_hook_ve(const char *const *env, const char *name, va_list args)
ae98a008 1166{
d3180279 1167 struct child_process hook = CHILD_PROCESS_INIT;
15048f8a 1168 const char *p;
ae98a008 1169
5a7da2dc
AS
1170 p = find_hook(name);
1171 if (!p)
cf94ca8e
SB
1172 return 0;
1173
d1d09456
RS
1174 argv_array_push(&hook.args, p);
1175 while ((p = va_arg(args, const char *)))
1176 argv_array_push(&hook.args, p);
15048f8a 1177 hook.env = env;
ae98a008
SB
1178 hook.no_stdin = 1;
1179 hook.stdout_to_stderr = 1;
ae98a008 1180
d1d09456 1181 return run_command(&hook);
ae98a008 1182}
15048f8a
BP
1183
1184int run_hook_le(const char *const *env, const char *name, ...)
1185{
1186 va_list args;
1187 int ret;
1188
1189 va_start(args, name);
1190 ret = run_hook_ve(env, name, args);
1191 va_end(args);
1192
1193 return ret;
1194}
911ec99b 1195
96335bcf
JK
1196struct io_pump {
1197 /* initialized by caller */
1198 int fd;
1199 int type; /* POLLOUT or POLLIN */
1200 union {
1201 struct {
1202 const char *buf;
1203 size_t len;
1204 } out;
1205 struct {
1206 struct strbuf *buf;
1207 size_t hint;
1208 } in;
1209 } u;
1210
1211 /* returned by pump_io */
1212 int error; /* 0 for success, otherwise errno */
1213
1214 /* internal use */
1215 struct pollfd *pfd;
1216};
1217
1218static int pump_io_round(struct io_pump *slots, int nr, struct pollfd *pfd)
1219{
1220 int pollsize = 0;
1221 int i;
1222
1223 for (i = 0; i < nr; i++) {
1224 struct io_pump *io = &slots[i];
1225 if (io->fd < 0)
1226 continue;
1227 pfd[pollsize].fd = io->fd;
1228 pfd[pollsize].events = io->type;
1229 io->pfd = &pfd[pollsize++];
1230 }
1231
1232 if (!pollsize)
1233 return 0;
1234
1235 if (poll(pfd, pollsize, -1) < 0) {
1236 if (errno == EINTR)
1237 return 1;
1238 die_errno("poll failed");
1239 }
1240
1241 for (i = 0; i < nr; i++) {
1242 struct io_pump *io = &slots[i];
1243
1244 if (io->fd < 0)
1245 continue;
1246
1247 if (!(io->pfd->revents & (POLLOUT|POLLIN|POLLHUP|POLLERR|POLLNVAL)))
1248 continue;
1249
1250 if (io->type == POLLOUT) {
1251 ssize_t len = xwrite(io->fd,
1252 io->u.out.buf, io->u.out.len);
1253 if (len < 0) {
1254 io->error = errno;
1255 close(io->fd);
1256 io->fd = -1;
1257 } else {
1258 io->u.out.buf += len;
1259 io->u.out.len -= len;
1260 if (!io->u.out.len) {
1261 close(io->fd);
1262 io->fd = -1;
1263 }
1264 }
1265 }
1266
1267 if (io->type == POLLIN) {
1268 ssize_t len = strbuf_read_once(io->u.in.buf,
1269 io->fd, io->u.in.hint);
1270 if (len < 0)
1271 io->error = errno;
1272 if (len <= 0) {
1273 close(io->fd);
1274 io->fd = -1;
1275 }
1276 }
1277 }
1278
1279 return 1;
1280}
1281
1282static int pump_io(struct io_pump *slots, int nr)
911ec99b 1283{
96335bcf
JK
1284 struct pollfd *pfd;
1285 int i;
1286
1287 for (i = 0; i < nr; i++)
1288 slots[i].error = 0;
1289
1290 ALLOC_ARRAY(pfd, nr);
1291 while (pump_io_round(slots, nr, pfd))
1292 ; /* nothing */
1293 free(pfd);
1294
1295 /* There may be multiple errno values, so just pick the first. */
1296 for (i = 0; i < nr; i++) {
1297 if (slots[i].error) {
1298 errno = slots[i].error;
1299 return -1;
1300 }
1301 }
1302 return 0;
1303}
1304
1305
1306int pipe_command(struct child_process *cmd,
1307 const char *in, size_t in_len,
1308 struct strbuf *out, size_t out_hint,
1309 struct strbuf *err, size_t err_hint)
1310{
1311 struct io_pump io[3];
1312 int nr = 0;
1313
1314 if (in)
1315 cmd->in = -1;
1316 if (out)
1317 cmd->out = -1;
1318 if (err)
1319 cmd->err = -1;
1320
911ec99b
JK
1321 if (start_command(cmd) < 0)
1322 return -1;
1323
96335bcf
JK
1324 if (in) {
1325 io[nr].fd = cmd->in;
1326 io[nr].type = POLLOUT;
1327 io[nr].u.out.buf = in;
1328 io[nr].u.out.len = in_len;
1329 nr++;
1330 }
1331 if (out) {
1332 io[nr].fd = cmd->out;
1333 io[nr].type = POLLIN;
1334 io[nr].u.in.buf = out;
1335 io[nr].u.in.hint = out_hint;
1336 nr++;
1337 }
1338 if (err) {
1339 io[nr].fd = cmd->err;
1340 io[nr].type = POLLIN;
1341 io[nr].u.in.buf = err;
1342 io[nr].u.in.hint = err_hint;
1343 nr++;
1344 }
1345
1346 if (pump_io(io, nr) < 0) {
911ec99b
JK
1347 finish_command(cmd); /* throw away exit code */
1348 return -1;
1349 }
1350
911ec99b
JK
1351 return finish_command(cmd);
1352}
c553c72e
SB
1353
1354enum child_state {
1355 GIT_CP_FREE,
1356 GIT_CP_WORKING,
1357 GIT_CP_WAIT_CLEANUP,
1358};
1359
1360struct parallel_processes {
1361 void *data;
1362
1363 int max_processes;
1364 int nr_processes;
1365
1366 get_next_task_fn get_next_task;
1367 start_failure_fn start_failure;
1368 task_finished_fn task_finished;
1369
1370 struct {
1371 enum child_state state;
1372 struct child_process process;
1373 struct strbuf err;
1374 void *data;
1375 } *children;
1376 /*
1377 * The struct pollfd is logically part of *children,
1378 * but the system call expects it as its own array.
1379 */
1380 struct pollfd *pfd;
1381
1382 unsigned shutdown : 1;
1383
1384 int output_owner;
1385 struct strbuf buffered_output; /* of finished children */
1386};
1387
aa710494 1388static int default_start_failure(struct strbuf *out,
c553c72e
SB
1389 void *pp_cb,
1390 void *pp_task_cb)
1391{
c553c72e
SB
1392 return 0;
1393}
1394
1395static int default_task_finished(int result,
aa710494 1396 struct strbuf *out,
c553c72e
SB
1397 void *pp_cb,
1398 void *pp_task_cb)
1399{
c553c72e
SB
1400 return 0;
1401}
1402
1403static void kill_children(struct parallel_processes *pp, int signo)
1404{
1405 int i, n = pp->max_processes;
1406
1407 for (i = 0; i < n; i++)
1408 if (pp->children[i].state == GIT_CP_WORKING)
1409 kill(pp->children[i].process.pid, signo);
1410}
1411
1412static struct parallel_processes *pp_for_signal;
1413
1414static void handle_children_on_signal(int signo)
1415{
1416 kill_children(pp_for_signal, signo);
1417 sigchain_pop(signo);
1418 raise(signo);
1419}
1420
1421static void pp_init(struct parallel_processes *pp,
1422 int n,
1423 get_next_task_fn get_next_task,
1424 start_failure_fn start_failure,
1425 task_finished_fn task_finished,
1426 void *data)
1427{
1428 int i;
1429
1430 if (n < 1)
1431 n = online_cpus();
1432
1433 pp->max_processes = n;
1434
1435 trace_printf("run_processes_parallel: preparing to run up to %d tasks", n);
1436
1437 pp->data = data;
1438 if (!get_next_task)
1439 die("BUG: you need to specify a get_next_task function");
1440 pp->get_next_task = get_next_task;
1441
1442 pp->start_failure = start_failure ? start_failure : default_start_failure;
1443 pp->task_finished = task_finished ? task_finished : default_task_finished;
1444
1445 pp->nr_processes = 0;
1446 pp->output_owner = 0;
1447 pp->shutdown = 0;
1448 pp->children = xcalloc(n, sizeof(*pp->children));
1449 pp->pfd = xcalloc(n, sizeof(*pp->pfd));
1450 strbuf_init(&pp->buffered_output, 0);
1451
1452 for (i = 0; i < n; i++) {
1453 strbuf_init(&pp->children[i].err, 0);
1454 child_process_init(&pp->children[i].process);
1455 pp->pfd[i].events = POLLIN | POLLHUP;
1456 pp->pfd[i].fd = -1;
1457 }
1458
1459 pp_for_signal = pp;
1460 sigchain_push_common(handle_children_on_signal);
1461}
1462
1463static void pp_cleanup(struct parallel_processes *pp)
1464{
1465 int i;
1466
1467 trace_printf("run_processes_parallel: done");
1468 for (i = 0; i < pp->max_processes; i++) {
1469 strbuf_release(&pp->children[i].err);
1470 child_process_clear(&pp->children[i].process);
1471 }
1472
1473 free(pp->children);
1474 free(pp->pfd);
1475
1476 /*
1477 * When get_next_task added messages to the buffer in its last
1478 * iteration, the buffered output is non empty.
1479 */
2dac9b56 1480 strbuf_write(&pp->buffered_output, stderr);
c553c72e
SB
1481 strbuf_release(&pp->buffered_output);
1482
1483 sigchain_pop_common();
1484}
1485
1486/* returns
1487 * 0 if a new task was started.
1488 * 1 if no new jobs was started (get_next_task ran out of work, non critical
1489 * problem with starting a new command)
1490 * <0 no new job was started, user wishes to shutdown early. Use negative code
1491 * to signal the children.
1492 */
1493static int pp_start_one(struct parallel_processes *pp)
1494{
1495 int i, code;
1496
1497 for (i = 0; i < pp->max_processes; i++)
1498 if (pp->children[i].state == GIT_CP_FREE)
1499 break;
1500 if (i == pp->max_processes)
1501 die("BUG: bookkeeping is hard");
1502
1503 code = pp->get_next_task(&pp->children[i].process,
1504 &pp->children[i].err,
1505 pp->data,
1506 &pp->children[i].data);
1507 if (!code) {
1508 strbuf_addbuf(&pp->buffered_output, &pp->children[i].err);
1509 strbuf_reset(&pp->children[i].err);
1510 return 1;
1511 }
1512 pp->children[i].process.err = -1;
1513 pp->children[i].process.stdout_to_stderr = 1;
1514 pp->children[i].process.no_stdin = 1;
1515
1516 if (start_command(&pp->children[i].process)) {
2a73b3da 1517 code = pp->start_failure(&pp->children[i].err,
c553c72e
SB
1518 pp->data,
1519 &pp->children[i].data);
1520 strbuf_addbuf(&pp->buffered_output, &pp->children[i].err);
1521 strbuf_reset(&pp->children[i].err);
1522 if (code)
1523 pp->shutdown = 1;
1524 return code;
1525 }
1526
1527 pp->nr_processes++;
1528 pp->children[i].state = GIT_CP_WORKING;
1529 pp->pfd[i].fd = pp->children[i].process.err;
1530 return 0;
1531}
1532
1533static void pp_buffer_stderr(struct parallel_processes *pp, int output_timeout)
1534{
1535 int i;
1536
1537 while ((i = poll(pp->pfd, pp->max_processes, output_timeout)) < 0) {
1538 if (errno == EINTR)
1539 continue;
1540 pp_cleanup(pp);
1541 die_errno("poll");
1542 }
1543
1544 /* Buffer output from all pipes. */
1545 for (i = 0; i < pp->max_processes; i++) {
1546 if (pp->children[i].state == GIT_CP_WORKING &&
1547 pp->pfd[i].revents & (POLLIN | POLLHUP)) {
1548 int n = strbuf_read_once(&pp->children[i].err,
1549 pp->children[i].process.err, 0);
1550 if (n == 0) {
1551 close(pp->children[i].process.err);
1552 pp->children[i].state = GIT_CP_WAIT_CLEANUP;
1553 } else if (n < 0)
1554 if (errno != EAGAIN)
1555 die_errno("read");
1556 }
1557 }
1558}
1559
1560static void pp_output(struct parallel_processes *pp)
1561{
1562 int i = pp->output_owner;
1563 if (pp->children[i].state == GIT_CP_WORKING &&
1564 pp->children[i].err.len) {
2dac9b56 1565 strbuf_write(&pp->children[i].err, stderr);
c553c72e
SB
1566 strbuf_reset(&pp->children[i].err);
1567 }
1568}
1569
1570static int pp_collect_finished(struct parallel_processes *pp)
1571{
1572 int i, code;
1573 int n = pp->max_processes;
1574 int result = 0;
1575
1576 while (pp->nr_processes > 0) {
1577 for (i = 0; i < pp->max_processes; i++)
1578 if (pp->children[i].state == GIT_CP_WAIT_CLEANUP)
1579 break;
1580 if (i == pp->max_processes)
1581 break;
1582
1583 code = finish_command(&pp->children[i].process);
1584
2a73b3da 1585 code = pp->task_finished(code,
c553c72e
SB
1586 &pp->children[i].err, pp->data,
1587 &pp->children[i].data);
1588
1589 if (code)
1590 result = code;
1591 if (code < 0)
1592 break;
1593
1594 pp->nr_processes--;
1595 pp->children[i].state = GIT_CP_FREE;
1596 pp->pfd[i].fd = -1;
1597 child_process_init(&pp->children[i].process);
1598
1599 if (i != pp->output_owner) {
1600 strbuf_addbuf(&pp->buffered_output, &pp->children[i].err);
1601 strbuf_reset(&pp->children[i].err);
1602 } else {
2dac9b56 1603 strbuf_write(&pp->children[i].err, stderr);
c553c72e
SB
1604 strbuf_reset(&pp->children[i].err);
1605
1606 /* Output all other finished child processes */
2dac9b56 1607 strbuf_write(&pp->buffered_output, stderr);
c553c72e
SB
1608 strbuf_reset(&pp->buffered_output);
1609
1610 /*
1611 * Pick next process to output live.
1612 * NEEDSWORK:
1613 * For now we pick it randomly by doing a round
1614 * robin. Later we may want to pick the one with
1615 * the most output or the longest or shortest
1616 * running process time.
1617 */
1618 for (i = 0; i < n; i++)
1619 if (pp->children[(pp->output_owner + i) % n].state == GIT_CP_WORKING)
1620 break;
1621 pp->output_owner = (pp->output_owner + i) % n;
1622 }
1623 }
1624 return result;
1625}
1626
1627int run_processes_parallel(int n,
1628 get_next_task_fn get_next_task,
1629 start_failure_fn start_failure,
1630 task_finished_fn task_finished,
1631 void *pp_cb)
1632{
1633 int i, code;
1634 int output_timeout = 100;
1635 int spawn_cap = 4;
1636 struct parallel_processes pp;
1637
1638 pp_init(&pp, n, get_next_task, start_failure, task_finished, pp_cb);
1639 while (1) {
1640 for (i = 0;
1641 i < spawn_cap && !pp.shutdown &&
1642 pp.nr_processes < pp.max_processes;
1643 i++) {
1644 code = pp_start_one(&pp);
1645 if (!code)
1646 continue;
1647 if (code < 0) {
1648 pp.shutdown = 1;
1649 kill_children(&pp, -code);
1650 }
1651 break;
1652 }
1653 if (!pp.nr_processes)
1654 break;
1655 pp_buffer_stderr(&pp, output_timeout);
1656 pp_output(&pp);
1657 code = pp_collect_finished(&pp);
1658 if (code) {
1659 pp.shutdown = 1;
1660 if (code < 0)
1661 kill_children(&pp, -code);
1662 }
1663 }
1664
1665 pp_cleanup(&pp);
1666 return 0;
1667}