]> git.ipfire.org Git - thirdparty/git.git/blame - run-command.c
The fifth batch
[thirdparty/git.git] / run-command.c
CommitLineData
e7da9385 1#define USE_THE_REPOSITORY_VARIABLE
41f43b82 2#define DISABLE_SIGN_COMPARE_WARNINGS
e7da9385 3
64c85595 4#include "git-compat-util.h"
b1bf95bb 5#include "run-command.h"
32a8f510 6#include "environment.h"
d807c4a0 7#include "exec-cmd.h"
f394e093 8#include "gettext.h"
afe19ff7 9#include "sigchain.h"
dbbcd44f 10#include "strvec.h"
cb2a5135 11#include "symlinks.h"
c553c72e
SB
12#include "thread-utils.h"
13#include "strbuf.h"
f805a00a 14#include "string-list.h"
74ea5c95
EN
15#include "trace.h"
16#include "trace2.h"
e73dd786 17#include "quote.h"
1942d483 18#include "config.h"
28d04e1e 19#include "packfile.h"
716c1f64 20#include "compat/nonblock.h"
b1bf95bb 21
483bbd4e
RS
22void child_process_init(struct child_process *child)
23{
5726a6b4
ÆAB
24 struct child_process blank = CHILD_PROCESS_INIT;
25 memcpy(child, &blank, sizeof(*child));
483bbd4e
RS
26}
27
2d71608e
RS
28void child_process_clear(struct child_process *child)
29{
c972bf4c 30 strvec_clear(&child->args);
29fda24d 31 strvec_clear(&child->env);
2d71608e
RS
32}
33
afe19ff7
JK
34struct child_to_clean {
35 pid_t pid;
ac2fbaa6 36 struct child_process *process;
afe19ff7
JK
37 struct child_to_clean *next;
38};
39static struct child_to_clean *children_to_clean;
40static int installed_child_cleanup_handler;
41
507d7804 42static void cleanup_children(int sig, int in_signal)
afe19ff7 43{
46df6906
JK
44 struct child_to_clean *children_to_wait_for = NULL;
45
afe19ff7
JK
46 while (children_to_clean) {
47 struct child_to_clean *p = children_to_clean;
48 children_to_clean = p->next;
ac2fbaa6
LS
49
50 if (p->process && !in_signal) {
51 struct child_process *process = p->process;
52 if (process->clean_on_exit_handler) {
53 trace_printf(
54 "trace: run_command: running exit handler for pid %"
55 PRIuMAX, (uintmax_t)p->pid
56 );
57 process->clean_on_exit_handler(process);
58 }
59 }
60
afe19ff7 61 kill(p->pid, sig);
46df6906 62
7b91929b 63 if (p->process && p->process->wait_after_clean) {
46df6906
JK
64 p->next = children_to_wait_for;
65 children_to_wait_for = p;
66 } else {
67 if (!in_signal)
68 free(p);
69 }
70 }
71
72 while (children_to_wait_for) {
73 struct child_to_clean *p = children_to_wait_for;
74 children_to_wait_for = p->next;
75
76 while (waitpid(p->pid, NULL, 0) < 0 && errno == EINTR)
77 ; /* spin waiting for process exit or error */
78
507d7804
TI
79 if (!in_signal)
80 free(p);
afe19ff7
JK
81 }
82}
83
84static void cleanup_children_on_signal(int sig)
85{
507d7804 86 cleanup_children(sig, 1);
afe19ff7
JK
87 sigchain_pop(sig);
88 raise(sig);
89}
90
91static void cleanup_children_on_exit(void)
92{
507d7804 93 cleanup_children(SIGTERM, 0);
afe19ff7
JK
94}
95
ac2fbaa6 96static void mark_child_for_cleanup(pid_t pid, struct child_process *process)
afe19ff7
JK
97{
98 struct child_to_clean *p = xmalloc(sizeof(*p));
99 p->pid = pid;
ac2fbaa6 100 p->process = process;
afe19ff7
JK
101 p->next = children_to_clean;
102 children_to_clean = p;
103
104 if (!installed_child_cleanup_handler) {
105 atexit(cleanup_children_on_exit);
106 sigchain_push_common(cleanup_children_on_signal);
107 installed_child_cleanup_handler = 1;
108 }
109}
110
111static void clear_child_for_cleanup(pid_t pid)
112{
bdee397d 113 struct child_to_clean **pp;
afe19ff7 114
bdee397d
DG
115 for (pp = &children_to_clean; *pp; pp = &(*pp)->next) {
116 struct child_to_clean *clean_me = *pp;
117
118 if (clean_me->pid == pid) {
119 *pp = clean_me->next;
120 free(clean_me);
afe19ff7
JK
121 return;
122 }
123 }
124}
125
9dc09c76
SP
126static inline void close_pair(int fd[2])
127{
128 close(fd[0]);
129 close(fd[1]);
130}
131
38124a40
BW
132int is_executable(const char *name)
133{
134 struct stat st;
135
136 if (stat(name, &st) || /* stat, not lstat */
137 !S_ISREG(st.st_mode))
138 return 0;
139
140#if defined(GIT_WINDOWS_NATIVE)
141 /*
142 * On Windows there is no executable bit. The file extension
143 * indicates whether it can be run as an executable, and Git
144 * has special-handling to detect scripts and launch them
145 * through the indicated script interpreter. We test for the
146 * file extension first because virus scanners may make
147 * it quite expensive to open many files.
148 */
149 if (ends_with(name, ".exe"))
150 return S_IXUSR;
151
152{
153 /*
154 * Now that we know it does not have an executable extension,
155 * peek into the file instead.
156 */
157 char buf[3] = { 0 };
158 int n;
159 int fd = open(name, O_RDONLY);
160 st.st_mode &= ~S_IXUSR;
161 if (fd >= 0) {
162 n = read(fd, buf, 2);
163 if (n == 2)
164 /* look for a she-bang */
165 if (!strcmp(buf, "#!"))
166 st.st_mode |= S_IXUSR;
167 close(fd);
168 }
169}
170#endif
171 return st.st_mode & S_IXUSR;
172}
173
bb532b53 174#ifndef locate_in_PATH
94028310
BW
175/*
176 * Search $PATH for a command. This emulates the path search that
177 * execvp would perform, without actually executing the command so it
178 * can be used before fork() to prepare to run a command using
179 * execve() or after execvp() to diagnose why it failed.
180 *
181 * The caller should ensure that file contains no directory
182 * separators.
183 *
184 * Returns the path to the command, as found in $PATH or NULL if the
185 * command could not be found. The caller inherits ownership of the memory
186 * used to store the resultant path.
187 *
188 * This should not be used on Windows, where the $PATH search rules
189 * are more complicated (e.g., a search for "foo" should find
190 * "foo.exe").
191 */
38f865c2
JK
192static char *locate_in_PATH(const char *file)
193{
194 const char *p = getenv("PATH");
195 struct strbuf buf = STRBUF_INIT;
196
197 if (!p || !*p)
198 return NULL;
199
200 while (1) {
201 const char *end = strchrnul(p, ':');
202
203 strbuf_reset(&buf);
204
205 /* POSIX specifies an empty entry as the current directory. */
206 if (end != p) {
207 strbuf_add(&buf, p, end - p);
208 strbuf_addch(&buf, '/');
209 }
210 strbuf_addstr(&buf, file);
211
94028310 212 if (is_executable(buf.buf))
38f865c2
JK
213 return strbuf_detach(&buf, NULL);
214
215 if (!*end)
216 break;
217 p = end + 1;
218 }
219
220 strbuf_release(&buf);
221 return NULL;
222}
bb532b53 223#endif
38f865c2 224
3f36e6f3 225int exists_in_PATH(const char *command)
38f865c2 226{
3f36e6f3 227 char *r = locate_in_PATH(command);
63ab08fb 228 int found = r != NULL;
38f865c2 229 free(r);
63ab08fb 230 return found;
38f865c2
JK
231}
232
233int sane_execvp(const char *file, char * const argv[])
234{
ee4512ed
JH
235#ifndef GIT_WINDOWS_NATIVE
236 /*
237 * execvp() doesn't return, so we all we can do is tell trace2
238 * what we are about to do and let it leave a hint in the log
239 * (unless of course the execvp() fails).
240 *
241 * we skip this for Windows because the compat layer already
242 * has to emulate the execvp() call anyway.
243 */
244 int exec_id = trace2_exec(file, (const char **)argv);
245#endif
246
38f865c2
JK
247 if (!execvp(file, argv))
248 return 0; /* cannot happen ;-) */
249
ee4512ed
JH
250#ifndef GIT_WINDOWS_NATIVE
251 {
252 int ec = errno;
253 trace2_exec_result(exec_id, ec);
254 errno = ec;
255 }
256#endif
257
38f865c2
JK
258 /*
259 * When a command can't be found because one of the directories
260 * listed in $PATH is unsearchable, execvp reports EACCES, but
261 * careful usability testing (read: analysis of occasional bug
262 * reports) reveals that "No such file or directory" is more
263 * intuitive.
264 *
265 * We avoid commands with "/", because execvp will not do $PATH
266 * lookups in that case.
267 *
268 * The reassignment of EACCES to errno looks like a no-op below,
269 * but we need to protect against exists_in_PATH overwriting errno.
270 */
271 if (errno == EACCES && !strchr(file, '/'))
272 errno = exists_in_PATH(file) ? EACCES : ENOENT;
a7855083
JH
273 else if (errno == ENOTDIR && !strchr(file, '/'))
274 errno = ENOENT;
38f865c2
JK
275 return -1;
276}
277
877da5e2 278char *git_shell_path(void)
0593c1ea
JS
279{
280#ifndef GIT_WINDOWS_NATIVE
92fe7c7d 281 return xstrdup(SHELL_PATH);
0593c1ea 282#else
92fe7c7d
JS
283 char *p = locate_in_PATH("sh");
284 convert_slashes(p);
285 return p;
0593c1ea
JS
286#endif
287}
288
c972bf4c 289static const char **prepare_shell_cmd(struct strvec *out, const char **argv)
8dba1e63 290{
20574f55 291 if (!argv[0])
033abf97 292 BUG("shell command is empty");
8dba1e63 293
f445644f 294 if (strcspn(argv[0], "|&;<>()$`\\\"' \t\n*?[#~=%") != strlen(argv[0])) {
92fe7c7d 295 strvec_push_nodup(out, git_shell_path());
c972bf4c 296 strvec_push(out, "-c");
8dba1e63 297
20574f55
JK
298 /*
299 * If we have no extra arguments, we do not even need to
300 * bother with the "$@" magic.
301 */
302 if (!argv[1])
c972bf4c 303 strvec_push(out, argv[0]);
20574f55 304 else
c972bf4c 305 strvec_pushf(out, "%s \"$@\"", argv[0]);
20574f55 306 }
8dba1e63 307
c972bf4c 308 strvec_pushv(out, argv);
d70a9eb6 309 return out->v;
8dba1e63
JK
310}
311
380395d0 312#ifndef GIT_WINDOWS_NATIVE
2b541bf8
JS
313static int child_notifier = -1;
314
79319b19
BW
315enum child_errcode {
316 CHILD_ERR_CHDIR,
53fa6753
BW
317 CHILD_ERR_DUP2,
318 CHILD_ERR_CLOSE,
45afb1ca 319 CHILD_ERR_SIGPROCMASK,
79319b19
BW
320 CHILD_ERR_SILENT,
321 CHILD_ERR_ERRNO
322};
323
324struct child_err {
325 enum child_errcode err;
326 int syserr; /* errno */
327};
328
329static void child_die(enum child_errcode err)
2b541bf8 330{
79319b19
BW
331 struct child_err buf;
332
333 buf.err = err;
334 buf.syserr = errno;
335
336 /* write(2) on buf smaller than PIPE_BUF (min 512) is atomic: */
337 xwrite(child_notifier, &buf, sizeof(buf));
338 _exit(1);
339}
340
53fa6753
BW
341static void child_dup2(int fd, int to)
342{
343 if (dup2(fd, to) < 0)
344 child_die(CHILD_ERR_DUP2);
345}
346
347static void child_close(int fd)
348{
349 if (close(fd))
350 child_die(CHILD_ERR_CLOSE);
351}
352
353static void child_close_pair(int fd[2])
354{
355 child_close(fd[0]);
356 child_close(fd[1]);
357}
358
ce41759e 359static void child_error_fn(const char *err UNUSED, va_list params UNUSED)
79319b19
BW
360{
361 const char msg[] = "error() should not be called in child\n";
362 xwrite(2, msg, sizeof(msg) - 1);
363}
364
ce41759e 365static void child_warn_fn(const char *err UNUSED, va_list params UNUSED)
79319b19
BW
366{
367 const char msg[] = "warn() should not be called in child\n";
368 xwrite(2, msg, sizeof(msg) - 1);
369}
370
ce41759e 371static void NORETURN child_die_fn(const char *err UNUSED, va_list params UNUSED)
79319b19
BW
372{
373 const char msg[] = "die() should not be called in child\n";
374 xwrite(2, msg, sizeof(msg) - 1);
375 _exit(2);
376}
377
378/* this runs in the parent process */
379static void child_err_spew(struct child_process *cmd, struct child_err *cerr)
380{
381 static void (*old_errfn)(const char *err, va_list params);
e081a7c3 382 report_fn die_message_routine = get_die_message_routine();
79319b19
BW
383
384 old_errfn = get_error_routine();
e081a7c3 385 set_error_routine(die_message_routine);
79319b19
BW
386 errno = cerr->syserr;
387
388 switch (cerr->err) {
389 case CHILD_ERR_CHDIR:
390 error_errno("exec '%s': cd to '%s' failed",
d3b21597 391 cmd->args.v[0], cmd->dir);
79319b19 392 break;
53fa6753
BW
393 case CHILD_ERR_DUP2:
394 error_errno("dup2() in child failed");
395 break;
396 case CHILD_ERR_CLOSE:
397 error_errno("close() in child failed");
398 break;
45afb1ca
EW
399 case CHILD_ERR_SIGPROCMASK:
400 error_errno("sigprocmask failed restoring signals");
401 break;
79319b19
BW
402 case CHILD_ERR_SILENT:
403 break;
404 case CHILD_ERR_ERRNO:
d3b21597 405 error_errno("cannot exec '%s'", cmd->args.v[0]);
79319b19
BW
406 break;
407 }
408 set_error_routine(old_errfn);
2b541bf8 409}
3967e25b 410
c972bf4c 411static int prepare_cmd(struct strvec *out, const struct child_process *cmd)
3967e25b 412{
d3b21597 413 if (!cmd->args.v[0])
033abf97 414 BUG("command is empty");
3967e25b 415
e3a43446
BW
416 /*
417 * Add SHELL_PATH so in the event exec fails with ENOEXEC we can
418 * attempt to interpret the command with 'sh'.
419 */
c972bf4c 420 strvec_push(out, SHELL_PATH);
e3a43446 421
3967e25b 422 if (cmd->git_cmd) {
d3b21597 423 prepare_git_cmd(out, cmd->args.v);
3967e25b 424 } else if (cmd->use_shell) {
d3b21597 425 prepare_shell_cmd(out, cmd->args.v);
3967e25b 426 } else {
d3b21597 427 strvec_pushv(out, cmd->args.v);
3967e25b 428 }
e3a43446
BW
429
430 /*
05ac8582
AK
431 * If there are no dir separator characters in the command then perform
432 * a path lookup and use the resolved path as the command to exec. If
433 * there are dir separator characters, we have exec attempt to invoke
434 * the command directly.
e3a43446 435 */
d70a9eb6
JK
436 if (!has_dir_sep(out->v[1])) {
437 char *program = locate_in_PATH(out->v[1]);
e3a43446 438 if (program) {
d70a9eb6
JK
439 free((char *)out->v[1]);
440 out->v[1] = program;
321fd823 441 } else {
c972bf4c 442 strvec_clear(out);
321fd823
JK
443 errno = ENOENT;
444 return -1;
e3a43446
BW
445 }
446 }
321fd823
JK
447
448 return 0;
3967e25b 449}
ae25394b
BW
450
451static char **prep_childenv(const char *const *deltaenv)
452{
453 extern char **environ;
454 char **childenv;
455 struct string_list env = STRING_LIST_INIT_DUP;
456 struct strbuf key = STRBUF_INIT;
457 const char *const *p;
458 int i;
459
460 /* Construct a sorted string list consisting of the current environ */
461 for (p = (const char *const *) environ; p && *p; p++) {
462 const char *equals = strchr(*p, '=');
463
464 if (equals) {
465 strbuf_reset(&key);
466 strbuf_add(&key, *p, equals - *p);
467 string_list_append(&env, key.buf)->util = (void *) *p;
468 } else {
469 string_list_append(&env, *p)->util = (void *) *p;
470 }
471 }
472 string_list_sort(&env);
473
474 /* Merge in 'deltaenv' with the current environ */
475 for (p = deltaenv; p && *p; p++) {
476 const char *equals = strchr(*p, '=');
477
478 if (equals) {
479 /* ('key=value'), insert or replace entry */
480 strbuf_reset(&key);
481 strbuf_add(&key, *p, equals - *p);
482 string_list_insert(&env, key.buf)->util = (void *) *p;
483 } else {
484 /* otherwise ('key') remove existing entry */
485 string_list_remove(&env, *p, 0);
486 }
487 }
488
489 /* Create an array of 'char *' to be used as the childenv */
0e187d75 490 ALLOC_ARRAY(childenv, env.nr + 1);
ae25394b
BW
491 for (i = 0; i < env.nr; i++)
492 childenv[i] = env.items[i].util;
493 childenv[env.nr] = NULL;
494
495 string_list_clear(&env, 0);
496 strbuf_release(&key);
497 return childenv;
498}
45afb1ca
EW
499
500struct atfork_state {
501#ifndef NO_PTHREADS
502 int cs;
503#endif
504 sigset_t old;
505};
506
dde74d73
JS
507#define CHECK_BUG(err, msg) \
508 do { \
509 int e = (err); \
510 if (e) \
511 BUG("%s: %s", msg, strerror(e)); \
512 } while(0)
45afb1ca
EW
513
514static void atfork_prepare(struct atfork_state *as)
515{
516 sigset_t all;
517
7e1bec1e 518 /*
82e79c63
JH
519 * POSIX says sigfillset() can fail, but an overly clever
520 * compiler can see through the header files and decide
521 * it cannot fail on a particular platform it is compiling for,
522 * triggering -Wunreachable-code false positive.
7e1bec1e 523 */
82e79c63 524 if (NOT_CONSTANT(sigfillset(&all)))
45afb1ca
EW
525 die_errno("sigfillset");
526#ifdef NO_PTHREADS
527 if (sigprocmask(SIG_SETMASK, &all, &as->old))
528 die_errno("sigprocmask");
529#else
dde74d73 530 CHECK_BUG(pthread_sigmask(SIG_SETMASK, &all, &as->old),
45afb1ca 531 "blocking all signals");
dde74d73 532 CHECK_BUG(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &as->cs),
45afb1ca
EW
533 "disabling cancellation");
534#endif
535}
536
537static void atfork_parent(struct atfork_state *as)
538{
539#ifdef NO_PTHREADS
540 if (sigprocmask(SIG_SETMASK, &as->old, NULL))
541 die_errno("sigprocmask");
542#else
dde74d73 543 CHECK_BUG(pthread_setcancelstate(as->cs, NULL),
45afb1ca 544 "re-enabling cancellation");
dde74d73 545 CHECK_BUG(pthread_sigmask(SIG_SETMASK, &as->old, NULL),
45afb1ca 546 "restoring signal mask");
200a76b7 547#endif
45afb1ca
EW
548}
549#endif /* GIT_WINDOWS_NATIVE */
a5487ddf
JS
550
551static inline void set_cloexec(int fd)
552{
553 int flags = fcntl(fd, F_GETFD);
554 if (flags >= 0)
555 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
556}
a5487ddf 557
507d7804 558static int wait_or_whine(pid_t pid, const char *argv0, int in_signal)
ab0b41da
JS
559{
560 int status, code = -1;
561 pid_t waiting;
562 int failed_errno = 0;
563
564 while ((waiting = waitpid(pid, &status, 0)) < 0 && errno == EINTR)
565 ; /* nothing */
566
567 if (waiting < 0) {
568 failed_errno = errno;
96bfb2d8
JK
569 if (!in_signal)
570 error_errno("waitpid for %s failed", argv0);
ab0b41da 571 } else if (waiting != pid) {
96bfb2d8
JK
572 if (!in_signal)
573 error("waitpid is confused (%s)", argv0);
ab0b41da
JS
574 } else if (WIFSIGNALED(status)) {
575 code = WTERMSIG(status);
96bfb2d8 576 if (!in_signal && code != SIGINT && code != SIGQUIT && code != SIGPIPE)
a2767c5c 577 error("%s died of signal %d", argv0, code);
ab0b41da
JS
578 /*
579 * This return value is chosen so that code & 0xff
580 * mimics the exit code that a POSIX shell would report for
581 * a program that died from this signal.
582 */
709ca730 583 code += 128;
ab0b41da
JS
584 } else if (WIFEXITED(status)) {
585 code = WEXITSTATUS(status);
ab0b41da 586 } else {
96bfb2d8
JK
587 if (!in_signal)
588 error("waitpid is confused (%s)", argv0);
ab0b41da 589 }
afe19ff7 590
96bfb2d8
JK
591 if (!in_signal)
592 clear_child_for_cleanup(pid);
afe19ff7 593
ab0b41da
JS
594 errno = failed_errno;
595 return code;
596}
597
c61a975d
NTND
598static void trace_add_env(struct strbuf *dst, const char *const *deltaenv)
599{
600 struct string_list envs = STRING_LIST_INIT_DUP;
601 const char *const *e;
602 int i;
603 int printed_unset = 0;
604
605 /* Last one wins, see run-command.c:prep_childenv() for context */
606 for (e = deltaenv; e && *e; e++) {
607 struct strbuf key = STRBUF_INIT;
608 char *equals = strchr(*e, '=');
609
610 if (equals) {
611 strbuf_add(&key, *e, equals - *e);
612 string_list_insert(&envs, key.buf)->util = equals + 1;
613 } else {
614 string_list_insert(&envs, *e)->util = NULL;
615 }
616 strbuf_release(&key);
617 }
618
619 /* "unset X Y...;" */
620 for (i = 0; i < envs.nr; i++) {
621 const char *var = envs.items[i].string;
622 const char *val = envs.items[i].util;
623
624 if (val || !getenv(var))
625 continue;
626
627 if (!printed_unset) {
628 strbuf_addstr(dst, " unset");
629 printed_unset = 1;
630 }
631 strbuf_addf(dst, " %s", var);
632 }
633 if (printed_unset)
634 strbuf_addch(dst, ';');
635
636 /* ... followed by "A=B C=D ..." */
637 for (i = 0; i < envs.nr; i++) {
638 const char *var = envs.items[i].string;
639 const char *val = envs.items[i].util;
640 const char *oldval;
641
642 if (!val)
643 continue;
644
645 oldval = getenv(var);
646 if (oldval && !strcmp(val, oldval))
647 continue;
648
649 strbuf_addf(dst, " %s=", var);
650 sq_quote_buf_pretty(dst, val);
651 }
652 string_list_clear(&envs, 0);
653}
654
e73dd786
NTND
655static void trace_run_command(const struct child_process *cp)
656{
657 struct strbuf buf = STRBUF_INIT;
658
659 if (!trace_want(&trace_default_key))
660 return;
661
248f66ed 662 strbuf_addstr(&buf, "trace: run_command:");
090a0927
NTND
663 if (cp->dir) {
664 strbuf_addstr(&buf, " cd ");
665 sq_quote_buf_pretty(&buf, cp->dir);
666 strbuf_addch(&buf, ';');
667 }
29fda24d 668 trace_add_env(&buf, cp->env.v);
21dfc5e0
NTND
669 if (cp->git_cmd)
670 strbuf_addstr(&buf, " git");
d3b21597 671 sq_quote_argv_pretty(&buf, cp->args.v);
e73dd786
NTND
672
673 trace_printf("%s", buf.buf);
674 strbuf_release(&buf);
675}
676
ebcb5d16 677int start_command(struct child_process *cmd)
b1bf95bb 678{
f3b33f1d
JS
679 int need_in, need_out, need_err;
680 int fdin[2], fdout[2], fderr[2];
25043d8a 681 int failed_errno;
b567004b 682 const char *str;
4919bf03 683
c20181e3
JS
684 /*
685 * In case of errors we must keep the promise to close FDs
686 * that have been passed in via ->in and ->out.
687 */
688
e4507ae8 689 need_in = !cmd->no_stdin && cmd->in < 0;
4919bf03 690 if (need_in) {
c20181e3 691 if (pipe(fdin) < 0) {
0ac77ec3 692 failed_errno = errno;
c20181e3
JS
693 if (cmd->out > 0)
694 close(cmd->out);
939296c4 695 str = "standard input";
0ac77ec3 696 goto fail_pipe;
c20181e3 697 }
4919bf03 698 cmd->in = fdin[1];
4919bf03
SP
699 }
700
e4507ae8
SP
701 need_out = !cmd->no_stdout
702 && !cmd->stdout_to_stderr
703 && cmd->out < 0;
f4bba25b
SP
704 if (need_out) {
705 if (pipe(fdout) < 0) {
0ac77ec3 706 failed_errno = errno;
f4bba25b
SP
707 if (need_in)
708 close_pair(fdin);
c20181e3
JS
709 else if (cmd->in)
710 close(cmd->in);
939296c4 711 str = "standard output";
0ac77ec3 712 goto fail_pipe;
f4bba25b
SP
713 }
714 cmd->out = fdout[0];
f4bba25b
SP
715 }
716
b73a4397 717 need_err = !cmd->no_stderr && cmd->err < 0;
f3b33f1d
JS
718 if (need_err) {
719 if (pipe(fderr) < 0) {
0ac77ec3 720 failed_errno = errno;
f3b33f1d
JS
721 if (need_in)
722 close_pair(fdin);
c20181e3
JS
723 else if (cmd->in)
724 close(cmd->in);
f3b33f1d
JS
725 if (need_out)
726 close_pair(fdout);
c20181e3
JS
727 else if (cmd->out)
728 close(cmd->out);
939296c4 729 str = "standard error";
0ac77ec3 730fail_pipe:
939296c4 731 error("cannot create %s pipe for %s: %s",
d3b21597 732 str, cmd->args.v[0], strerror(failed_errno));
2d71608e 733 child_process_clear(cmd);
0ac77ec3
JS
734 errno = failed_errno;
735 return -1;
f3b33f1d
JS
736 }
737 cmd->err = fderr[0];
738 }
739
ee4512ed 740 trace2_child_start(cmd);
e73dd786
NTND
741 trace_run_command(cmd);
742
13af8cbd 743 fflush(NULL);
8852f5d7 744
28d04e1e
JS
745 if (cmd->close_object_store)
746 close_object_store(the_repository->objects);
747
380395d0 748#ifndef GIT_WINDOWS_NATIVE
2b541bf8
JS
749{
750 int notify_pipe[2];
db015a28 751 int null_fd = -1;
ae25394b 752 char **childenv;
c972bf4c 753 struct strvec argv = STRVEC_INIT;
79319b19 754 struct child_err cerr;
45afb1ca 755 struct atfork_state as;
3967e25b 756
321fd823
JK
757 if (prepare_cmd(&argv, cmd) < 0) {
758 failed_errno = errno;
759 cmd->pid = -1;
e5a329a2 760 if (!cmd->silent_exec_failure)
d3b21597 761 error_errno("cannot run %s", cmd->args.v[0]);
321fd823
JK
762 goto end_of_spawn;
763 }
764
291ef5b6
IW
765 trace_argv_printf(&argv.v[1], "trace: start_command:");
766
2b541bf8
JS
767 if (pipe(notify_pipe))
768 notify_pipe[0] = notify_pipe[1] = -1;
769
db015a28 770 if (cmd->no_stdin || cmd->no_stdout || cmd->no_stderr) {
66e905b7 771 null_fd = xopen("/dev/null", O_RDWR | O_CLOEXEC);
db015a28
BW
772 set_cloexec(null_fd);
773 }
774
29fda24d 775 childenv = prep_childenv(cmd->env.v);
45afb1ca 776 atfork_prepare(&as);
3967e25b 777
e503cd6e
BW
778 /*
779 * NOTE: In order to prevent deadlocking when using threads special
780 * care should be taken with the function calls made in between the
781 * fork() and exec() calls. No calls should be made to functions which
782 * require acquiring a lock (e.g. malloc) as the lock could have been
783 * held by another thread at the time of forking, causing the lock to
784 * never be released in the child process. This means only
785 * Async-Signal-Safe functions are permitted in the child.
786 */
ebcb5d16 787 cmd->pid = fork();
25043d8a 788 failed_errno = errno;
ebcb5d16 789 if (!cmd->pid) {
45afb1ca 790 int sig;
a5487ddf 791 /*
79319b19
BW
792 * Ensure the default die/error/warn routines do not get
793 * called, they can take stdio locks and malloc.
a5487ddf 794 */
79319b19
BW
795 set_die_routine(child_die_fn);
796 set_error_routine(child_error_fn);
797 set_warn_routine(child_warn_fn);
a5487ddf 798
2b541bf8
JS
799 close(notify_pipe[0]);
800 set_cloexec(notify_pipe[1]);
801 child_notifier = notify_pipe[1];
2b541bf8 802
e4507ae8 803 if (cmd->no_stdin)
53fa6753 804 child_dup2(null_fd, 0);
e4507ae8 805 else if (need_in) {
53fa6753
BW
806 child_dup2(fdin[0], 0);
807 child_close_pair(fdin);
4919bf03 808 } else if (cmd->in) {
53fa6753
BW
809 child_dup2(cmd->in, 0);
810 child_close(cmd->in);
95d3c4f5 811 }
4919bf03 812
ce2cf27a 813 if (cmd->no_stderr)
53fa6753 814 child_dup2(null_fd, 2);
ce2cf27a 815 else if (need_err) {
53fa6753
BW
816 child_dup2(fderr[1], 2);
817 child_close_pair(fderr);
4f41b611 818 } else if (cmd->err > 1) {
53fa6753
BW
819 child_dup2(cmd->err, 2);
820 child_close(cmd->err);
ce2cf27a
CC
821 }
822
e4507ae8 823 if (cmd->no_stdout)
53fa6753 824 child_dup2(null_fd, 1);
e4507ae8 825 else if (cmd->stdout_to_stderr)
53fa6753 826 child_dup2(2, 1);
f4bba25b 827 else if (need_out) {
53fa6753
BW
828 child_dup2(fdout[1], 1);
829 child_close_pair(fdout);
f4bba25b 830 } else if (cmd->out > 1) {
53fa6753
BW
831 child_dup2(cmd->out, 1);
832 child_close(cmd->out);
f4bba25b
SP
833 }
834
1568fea0 835 if (cmd->dir && chdir(cmd->dir))
79319b19 836 child_die(CHILD_ERR_CHDIR);
3967e25b 837
45afb1ca
EW
838 /*
839 * restore default signal handlers here, in case
840 * we catch a signal right before execve below
841 */
842 for (sig = 1; sig < NSIG; sig++) {
843 /* ignored signals get reset to SIG_DFL on execve */
844 if (signal(sig, SIG_DFL) == SIG_IGN)
845 signal(sig, SIG_IGN);
846 }
847
848 if (sigprocmask(SIG_SETMASK, &as.old, NULL) != 0)
849 child_die(CHILD_ERR_SIGPROCMASK);
850
e3a43446
BW
851 /*
852 * Attempt to exec using the command and arguments starting at
853 * argv.argv[1]. argv.argv[0] contains SHELL_PATH which will
854 * be used in the event exec failed with ENOEXEC at which point
855 * we will try to interpret the command using 'sh'.
856 */
d70a9eb6 857 execve(argv.v[1], (char *const *) argv.v + 1,
ae25394b 858 (char *const *) childenv);
e3a43446 859 if (errno == ENOEXEC)
d70a9eb6 860 execve(argv.v[0], (char *const *) argv.v,
ae25394b 861 (char *const *) childenv);
3967e25b 862
6d224ac2
RS
863 if (cmd->silent_exec_failure && errno == ENOENT)
864 child_die(CHILD_ERR_SILENT);
865 child_die(CHILD_ERR_ERRNO);
b1bf95bb 866 }
45afb1ca 867 atfork_parent(&as);
0ac77ec3 868 if (cmd->pid < 0)
d3b21597 869 error_errno("cannot fork() for %s", cmd->args.v[0]);
afe19ff7 870 else if (cmd->clean_on_exit)
ac2fbaa6 871 mark_child_for_cleanup(cmd->pid, cmd);
2b541bf8
JS
872
873 /*
3967e25b 874 * Wait for child's exec. If the exec succeeds (or if fork()
2b541bf8 875 * failed), EOF is seen immediately by the parent. Otherwise, the
79319b19 876 * child process sends a child_err struct.
2b541bf8
JS
877 * Note that use of this infrastructure is completely advisory,
878 * therefore, we keep error checks minimal.
879 */
880 close(notify_pipe[1]);
79319b19 881 if (xread(notify_pipe[0], &cerr, sizeof(cerr)) == sizeof(cerr)) {
2b541bf8 882 /*
3967e25b 883 * At this point we know that fork() succeeded, but exec()
2b541bf8
JS
884 * failed. Errors have been reported to our stderr.
885 */
d3b21597 886 wait_or_whine(cmd->pid, cmd->args.v[0], 0);
79319b19 887 child_err_spew(cmd, &cerr);
2b541bf8
JS
888 failed_errno = errno;
889 cmd->pid = -1;
890 }
891 close(notify_pipe[0]);
3967e25b 892
db015a28
BW
893 if (null_fd >= 0)
894 close(null_fd);
c972bf4c 895 strvec_clear(&argv);
ae25394b 896 free(childenv);
2b541bf8 897}
321fd823
JK
898end_of_spawn:
899
ba26f296 900#else
0d30ad71 901{
75301f90 902 int fhin = 0, fhout = 1, fherr = 2;
d3b21597 903 const char **sargv = cmd->args.v;
c972bf4c 904 struct strvec nargv = STRVEC_INIT;
ba26f296 905
75301f90
JS
906 if (cmd->no_stdin)
907 fhin = open("/dev/null", O_RDWR);
908 else if (need_in)
909 fhin = dup(fdin[0]);
910 else if (cmd->in)
911 fhin = dup(cmd->in);
912
913 if (cmd->no_stderr)
914 fherr = open("/dev/null", O_RDWR);
915 else if (need_err)
916 fherr = dup(fderr[1]);
76d44c8c
JH
917 else if (cmd->err > 2)
918 fherr = dup(cmd->err);
75301f90
JS
919
920 if (cmd->no_stdout)
921 fhout = open("/dev/null", O_RDWR);
922 else if (cmd->stdout_to_stderr)
923 fhout = dup(fherr);
924 else if (need_out)
925 fhout = dup(fdout[1]);
926 else if (cmd->out > 1)
927 fhout = dup(cmd->out);
ba26f296 928
5a50085c 929 if (cmd->git_cmd)
d3b21597 930 cmd->args.v = prepare_git_cmd(&nargv, sargv);
5a50085c 931 else if (cmd->use_shell)
d3b21597 932 cmd->args.v = prepare_shell_cmd(&nargv, sargv);
ba26f296 933
291ef5b6 934 trace_argv_printf(cmd->args.v, "trace: start_command:");
29fda24d
ÆAB
935 cmd->pid = mingw_spawnvpe(cmd->args.v[0], cmd->args.v,
936 (char**) cmd->env.v,
937 cmd->dir, fhin, fhout, fherr);
0ac77ec3 938 failed_errno = errno;
c024beb5 939 if (cmd->pid < 0 && (!cmd->silent_exec_failure || errno != ENOENT))
d3b21597 940 error_errno("cannot spawn %s", cmd->args.v[0]);
afe19ff7 941 if (cmd->clean_on_exit && cmd->pid >= 0)
ac2fbaa6 942 mark_child_for_cleanup(cmd->pid, cmd);
ba26f296 943
c972bf4c 944 strvec_clear(&nargv);
d3b21597 945 cmd->args.v = sargv;
75301f90
JS
946 if (fhin != 0)
947 close(fhin);
948 if (fhout != 1)
949 close(fhout);
950 if (fherr != 2)
951 close(fherr);
0d30ad71 952}
ba26f296
JS
953#endif
954
955 if (cmd->pid < 0) {
ee4512ed
JH
956 trace2_child_exit(cmd, -1);
957
ba26f296
JS
958 if (need_in)
959 close_pair(fdin);
960 else if (cmd->in)
961 close(cmd->in);
962 if (need_out)
963 close_pair(fdout);
964 else if (cmd->out)
965 close(cmd->out);
966 if (need_err)
967 close_pair(fderr);
fc012c28
D
968 else if (cmd->err)
969 close(cmd->err);
2d71608e 970 child_process_clear(cmd);
0ac77ec3
JS
971 errno = failed_errno;
972 return -1;
ba26f296 973 }
4919bf03
SP
974
975 if (need_in)
976 close(fdin[0]);
977 else if (cmd->in)
978 close(cmd->in);
979
f4bba25b
SP
980 if (need_out)
981 close(fdout[1]);
c20181e3 982 else if (cmd->out)
f4bba25b
SP
983 close(cmd->out);
984
f3b33f1d
JS
985 if (need_err)
986 close(fderr[1]);
4f41b611
SP
987 else if (cmd->err)
988 close(cmd->err);
f3b33f1d 989
ebcb5d16
SP
990 return 0;
991}
992
2d22c208
JS
993int finish_command(struct child_process *cmd)
994{
d3b21597 995 int ret = wait_or_whine(cmd->pid, cmd->args.v[0], 0);
ee4512ed 996 trace2_child_exit(cmd, ret);
2d71608e 997 child_process_clear(cmd);
0d58fef5 998 invalidate_lstat_cache();
c460c0ec 999 return ret;
2d22c208
JS
1000}
1001
507d7804
TI
1002int finish_command_in_signal(struct child_process *cmd)
1003{
d3b21597 1004 int ret = wait_or_whine(cmd->pid, cmd->args.v[0], 1);
ce3986bb
JS
1005 if (ret != -1)
1006 trace2_child_exit(cmd, ret);
ee4512ed 1007 return ret;
507d7804
TI
1008}
1009
1010
ebcb5d16
SP
1011int run_command(struct child_process *cmd)
1012{
c29b3962
JK
1013 int code;
1014
1015 if (cmd->out < 0 || cmd->err < 0)
033abf97 1016 BUG("run_command with a pipe can cause deadlock");
c29b3962
JK
1017
1018 code = start_command(cmd);
ebcb5d16
SP
1019 if (code)
1020 return code;
1021 return finish_command(cmd);
1022}
1023
f6b60983 1024#ifndef NO_PTHREADS
0ea1c89b
JS
1025static pthread_t main_thread;
1026static int main_thread_set;
1027static pthread_key_t async_key;
1ece66bc 1028static pthread_key_t async_die_counter;
0ea1c89b 1029
200a76b7 1030static void *run_thread(void *data)
618ebe9f
JS
1031{
1032 struct async *async = data;
f6b60983 1033 intptr_t ret;
0ea1c89b 1034
c792d7b6
JK
1035 if (async->isolate_sigpipe) {
1036 sigset_t mask;
1037 sigemptyset(&mask);
1038 sigaddset(&mask, SIGPIPE);
786e6761 1039 if (pthread_sigmask(SIG_BLOCK, &mask, NULL)) {
c792d7b6
JK
1040 ret = error("unable to block SIGPIPE in async thread");
1041 return (void *)ret;
1042 }
1043 }
1044
0ea1c89b 1045 pthread_setspecific(async_key, async);
f6b60983 1046 ret = async->proc(async->proc_in, async->proc_out, async->data);
200a76b7 1047 return (void *)ret;
618ebe9f 1048}
0ea1c89b
JS
1049
1050static NORETURN void die_async(const char *err, va_list params)
1051{
e081a7c3
ÆAB
1052 report_fn die_message_fn = get_die_message_routine();
1053
1054 die_message_fn(err, params);
0ea1c89b 1055
661a8cf4 1056 if (in_async()) {
0ea1c89b
JS
1057 struct async *async = pthread_getspecific(async_key);
1058 if (async->proc_in >= 0)
1059 close(async->proc_in);
1060 if (async->proc_out >= 0)
1061 close(async->proc_out);
1062 pthread_exit((void *)128);
1063 }
1064
1065 exit(128);
618ebe9f 1066}
1ece66bc
JK
1067
1068static int async_die_is_recursing(void)
1069{
1070 void *ret = pthread_getspecific(async_die_counter);
4b540cf9 1071 pthread_setspecific(async_die_counter, &async_die_counter); /* set to any non-NULL valid pointer */
1ece66bc
JK
1072 return ret != NULL;
1073}
1074
661a8cf4
JK
1075int in_async(void)
1076{
1077 if (!main_thread_set)
1078 return 0; /* no asyncs started yet */
1079 return !pthread_equal(main_thread, pthread_self());
1080}
1081
b992fe10 1082static void NORETURN async_exit(int code)
9658846c
JK
1083{
1084 pthread_exit((void *)(intptr_t)code);
1085}
1086
0f4b6db3
EB
1087#else
1088
1089static struct {
1090 void (**handlers)(void);
1091 size_t nr;
1092 size_t alloc;
1093} git_atexit_hdlrs;
1094
1095static int git_atexit_installed;
1096
6066a7ea 1097static void git_atexit_dispatch(void)
0f4b6db3
EB
1098{
1099 size_t i;
1100
1101 for (i=git_atexit_hdlrs.nr ; i ; i--)
1102 git_atexit_hdlrs.handlers[i-1]();
1103}
1104
6066a7ea 1105static void git_atexit_clear(void)
0f4b6db3
EB
1106{
1107 free(git_atexit_hdlrs.handlers);
1108 memset(&git_atexit_hdlrs, 0, sizeof(git_atexit_hdlrs));
1109 git_atexit_installed = 0;
1110}
1111
1112#undef atexit
1113int git_atexit(void (*handler)(void))
1114{
1115 ALLOC_GROW(git_atexit_hdlrs.handlers, git_atexit_hdlrs.nr + 1, git_atexit_hdlrs.alloc);
1116 git_atexit_hdlrs.handlers[git_atexit_hdlrs.nr++] = handler;
1117 if (!git_atexit_installed) {
1118 if (atexit(&git_atexit_dispatch))
1119 return -1;
1120 git_atexit_installed = 1;
1121 }
1122 return 0;
1123}
1124#define atexit git_atexit
1125
661a8cf4
JK
1126static int process_is_async;
1127int in_async(void)
1128{
1129 return process_is_async;
1130}
1131
b992fe10 1132static void NORETURN async_exit(int code)
9658846c
JK
1133{
1134 exit(code);
1135}
1136
618ebe9f
JS
1137#endif
1138
b992fe10
LS
1139void check_pipe(int err)
1140{
1141 if (err == EPIPE) {
1142 if (in_async())
1143 async_exit(141);
1144
1145 signal(SIGPIPE, SIG_DFL);
1146 raise(SIGPIPE);
1147 /* Should never happen, but just in case... */
1148 exit(141);
1149 }
1150}
1151
2d22c208
JS
1152int start_async(struct async *async)
1153{
ae6a5609
EFL
1154 int need_in, need_out;
1155 int fdin[2], fdout[2];
1156 int proc_in, proc_out;
2d22c208 1157
ae6a5609
EFL
1158 need_in = async->in < 0;
1159 if (need_in) {
1160 if (pipe(fdin) < 0) {
1161 if (async->out > 0)
1162 close(async->out);
fbcb0e06 1163 return error_errno("cannot create pipe");
ae6a5609
EFL
1164 }
1165 async->in = fdin[1];
1166 }
1167
1168 need_out = async->out < 0;
1169 if (need_out) {
1170 if (pipe(fdout) < 0) {
1171 if (need_in)
1172 close_pair(fdin);
1173 else if (async->in)
1174 close(async->in);
fbcb0e06 1175 return error_errno("cannot create pipe");
ae6a5609
EFL
1176 }
1177 async->out = fdout[0];
1178 }
1179
1180 if (need_in)
1181 proc_in = fdin[0];
1182 else if (async->in)
1183 proc_in = async->in;
1184 else
1185 proc_in = -1;
1186
1187 if (need_out)
1188 proc_out = fdout[1];
1189 else if (async->out)
1190 proc_out = async->out;
1191 else
1192 proc_out = -1;
2d22c208 1193
f6b60983 1194#ifdef NO_PTHREADS
2c3766f0
AM
1195 /* Flush stdio before fork() to avoid cloning buffers */
1196 fflush(NULL);
1197
2d22c208
JS
1198 async->pid = fork();
1199 if (async->pid < 0) {
fbcb0e06 1200 error_errno("fork (async) failed");
ae6a5609 1201 goto error;
2d22c208
JS
1202 }
1203 if (!async->pid) {
ae6a5609
EFL
1204 if (need_in)
1205 close(fdin[1]);
1206 if (need_out)
1207 close(fdout[0]);
0f4b6db3 1208 git_atexit_clear();
661a8cf4 1209 process_is_async = 1;
ae6a5609 1210 exit(!!async->proc(proc_in, proc_out, async->data));
2d22c208 1211 }
ae6a5609 1212
ac2fbaa6 1213 mark_child_for_cleanup(async->pid, NULL);
afe19ff7 1214
ae6a5609
EFL
1215 if (need_in)
1216 close(fdin[0]);
1217 else if (async->in)
1218 close(async->in);
1219
1220 if (need_out)
1221 close(fdout[1]);
1222 else if (async->out)
1223 close(async->out);
618ebe9f 1224#else
0ea1c89b
JS
1225 if (!main_thread_set) {
1226 /*
1227 * We assume that the first time that start_async is called
1228 * it is from the main thread.
1229 */
1230 main_thread_set = 1;
1231 main_thread = pthread_self();
1232 pthread_key_create(&async_key, NULL);
1ece66bc 1233 pthread_key_create(&async_die_counter, NULL);
0ea1c89b 1234 set_die_routine(die_async);
1ece66bc 1235 set_die_is_recursing_routine(async_die_is_recursing);
0ea1c89b
JS
1236 }
1237
200a76b7
JS
1238 if (proc_in >= 0)
1239 set_cloexec(proc_in);
1240 if (proc_out >= 0)
1241 set_cloexec(proc_out);
ae6a5609
EFL
1242 async->proc_in = proc_in;
1243 async->proc_out = proc_out;
200a76b7
JS
1244 {
1245 int err = pthread_create(&async->tid, NULL, run_thread, async);
1246 if (err) {
2179045f 1247 error(_("cannot create async thread: %s"), strerror(err));
200a76b7
JS
1248 goto error;
1249 }
618ebe9f
JS
1250 }
1251#endif
2d22c208 1252 return 0;
ae6a5609
EFL
1253
1254error:
1255 if (need_in)
1256 close_pair(fdin);
1257 else if (async->in)
1258 close(async->in);
1259
1260 if (need_out)
1261 close_pair(fdout);
1262 else if (async->out)
1263 close(async->out);
1264 return -1;
2d22c208
JS
1265}
1266
1267int finish_async(struct async *async)
1268{
f6b60983 1269#ifdef NO_PTHREADS
0d58fef5
JS
1270 int ret = wait_or_whine(async->pid, "child process", 0);
1271
1272 invalidate_lstat_cache();
1273
1274 return ret;
618ebe9f 1275#else
200a76b7
JS
1276 void *ret = (void *)(intptr_t)(-1);
1277
1278 if (pthread_join(async->tid, &ret))
1279 error("pthread_join failed");
0d58fef5 1280 invalidate_lstat_cache();
200a76b7 1281 return (int)(intptr_t)ret;
0d58fef5 1282
618ebe9f 1283#endif
2d22c208 1284}
ae98a008 1285
c0e40a2d
NTND
1286int async_with_fork(void)
1287{
1288#ifdef NO_PTHREADS
1289 return 1;
1290#else
1291 return 0;
1292#endif
1293}
1294
96335bcf
JK
1295struct io_pump {
1296 /* initialized by caller */
1297 int fd;
1298 int type; /* POLLOUT or POLLIN */
1299 union {
1300 struct {
1301 const char *buf;
1302 size_t len;
1303 } out;
1304 struct {
1305 struct strbuf *buf;
1306 size_t hint;
1307 } in;
1308 } u;
1309
1310 /* returned by pump_io */
1311 int error; /* 0 for success, otherwise errno */
1312
1313 /* internal use */
1314 struct pollfd *pfd;
1315};
1316
1317static int pump_io_round(struct io_pump *slots, int nr, struct pollfd *pfd)
1318{
1319 int pollsize = 0;
1320 int i;
1321
1322 for (i = 0; i < nr; i++) {
1323 struct io_pump *io = &slots[i];
1324 if (io->fd < 0)
1325 continue;
1326 pfd[pollsize].fd = io->fd;
1327 pfd[pollsize].events = io->type;
1328 io->pfd = &pfd[pollsize++];
1329 }
1330
1331 if (!pollsize)
1332 return 0;
1333
1334 if (poll(pfd, pollsize, -1) < 0) {
1335 if (errno == EINTR)
1336 return 1;
1337 die_errno("poll failed");
1338 }
1339
1340 for (i = 0; i < nr; i++) {
1341 struct io_pump *io = &slots[i];
1342
1343 if (io->fd < 0)
1344 continue;
1345
1346 if (!(io->pfd->revents & (POLLOUT|POLLIN|POLLHUP|POLLERR|POLLNVAL)))
1347 continue;
1348
1349 if (io->type == POLLOUT) {
14eab817
JK
1350 ssize_t len;
1351
1352 /*
1353 * Don't use xwrite() here. It loops forever on EAGAIN,
1354 * and we're in our own poll() loop here.
1355 *
1356 * Note that we lose xwrite()'s handling of MAX_IO_SIZE
1357 * and EINTR, so we have to implement those ourselves.
1358 */
1359 len = write(io->fd, io->u.out.buf,
1360 io->u.out.len <= MAX_IO_SIZE ?
1361 io->u.out.len : MAX_IO_SIZE);
96335bcf 1362 if (len < 0) {
c6d3cce6
JK
1363 if (errno != EINTR && errno != EAGAIN &&
1364 errno != ENOSPC) {
14eab817
JK
1365 io->error = errno;
1366 close(io->fd);
1367 io->fd = -1;
1368 }
96335bcf
JK
1369 } else {
1370 io->u.out.buf += len;
1371 io->u.out.len -= len;
1372 if (!io->u.out.len) {
1373 close(io->fd);
1374 io->fd = -1;
1375 }
1376 }
1377 }
1378
1379 if (io->type == POLLIN) {
1380 ssize_t len = strbuf_read_once(io->u.in.buf,
1381 io->fd, io->u.in.hint);
1382 if (len < 0)
1383 io->error = errno;
1384 if (len <= 0) {
1385 close(io->fd);
1386 io->fd = -1;
1387 }
1388 }
1389 }
1390
1391 return 1;
1392}
1393
1394static int pump_io(struct io_pump *slots, int nr)
911ec99b 1395{
96335bcf
JK
1396 struct pollfd *pfd;
1397 int i;
1398
1399 for (i = 0; i < nr; i++)
1400 slots[i].error = 0;
1401
1402 ALLOC_ARRAY(pfd, nr);
1403 while (pump_io_round(slots, nr, pfd))
1404 ; /* nothing */
1405 free(pfd);
1406
1407 /* There may be multiple errno values, so just pick the first. */
1408 for (i = 0; i < nr; i++) {
1409 if (slots[i].error) {
1410 errno = slots[i].error;
1411 return -1;
1412 }
1413 }
1414 return 0;
1415}
1416
1417
1418int pipe_command(struct child_process *cmd,
1419 const char *in, size_t in_len,
1420 struct strbuf *out, size_t out_hint,
1421 struct strbuf *err, size_t err_hint)
1422{
1423 struct io_pump io[3];
1424 int nr = 0;
1425
1426 if (in)
1427 cmd->in = -1;
1428 if (out)
1429 cmd->out = -1;
1430 if (err)
1431 cmd->err = -1;
1432
911ec99b
JK
1433 if (start_command(cmd) < 0)
1434 return -1;
1435
96335bcf 1436 if (in) {
716c1f64
JK
1437 if (enable_pipe_nonblock(cmd->in) < 0) {
1438 error_errno("unable to make pipe non-blocking");
1439 close(cmd->in);
1440 if (out)
1441 close(cmd->out);
1442 if (err)
1443 close(cmd->err);
1444 return -1;
1445 }
96335bcf
JK
1446 io[nr].fd = cmd->in;
1447 io[nr].type = POLLOUT;
1448 io[nr].u.out.buf = in;
1449 io[nr].u.out.len = in_len;
1450 nr++;
1451 }
1452 if (out) {
1453 io[nr].fd = cmd->out;
1454 io[nr].type = POLLIN;
1455 io[nr].u.in.buf = out;
1456 io[nr].u.in.hint = out_hint;
1457 nr++;
1458 }
1459 if (err) {
1460 io[nr].fd = cmd->err;
1461 io[nr].type = POLLIN;
1462 io[nr].u.in.buf = err;
1463 io[nr].u.in.hint = err_hint;
1464 nr++;
1465 }
1466
1467 if (pump_io(io, nr) < 0) {
911ec99b
JK
1468 finish_command(cmd); /* throw away exit code */
1469 return -1;
1470 }
1471
911ec99b
JK
1472 return finish_command(cmd);
1473}
c553c72e
SB
1474
1475enum child_state {
1476 GIT_CP_FREE,
1477 GIT_CP_WORKING,
1478 GIT_CP_WAIT_CLEANUP,
1479};
1480
1481struct parallel_processes {
6a48b428 1482 size_t nr_processes;
c553c72e 1483
c553c72e
SB
1484 struct {
1485 enum child_state state;
1486 struct child_process process;
1487 struct strbuf err;
1488 void *data;
1489 } *children;
1490 /*
1491 * The struct pollfd is logically part of *children,
1492 * but the system call expects it as its own array.
1493 */
1494 struct pollfd *pfd;
1495
1496 unsigned shutdown : 1;
1497
6a48b428 1498 size_t output_owner;
c553c72e
SB
1499 struct strbuf buffered_output; /* of finished children */
1500};
1501
0b0ab95f
ÆAB
1502struct parallel_processes_for_signal {
1503 const struct run_process_parallel_opts *opts;
1504 const struct parallel_processes *pp;
1505};
1506
1507static void kill_children(const struct parallel_processes *pp,
1508 const struct run_process_parallel_opts *opts,
1509 int signo)
c553c72e 1510{
0b0ab95f 1511 for (size_t i = 0; i < opts->processes; i++)
c553c72e
SB
1512 if (pp->children[i].state == GIT_CP_WORKING)
1513 kill(pp->children[i].process.pid, signo);
1514}
1515
0b0ab95f
ÆAB
1516static void kill_children_signal(const struct parallel_processes_for_signal *pp_sig,
1517 int signo)
1518{
1519 kill_children(pp_sig->pp, pp_sig->opts, signo);
1520}
1521
1522static struct parallel_processes_for_signal *pp_for_signal;
c553c72e
SB
1523
1524static void handle_children_on_signal(int signo)
1525{
0b0ab95f 1526 kill_children_signal(pp_for_signal, signo);
c553c72e
SB
1527 sigchain_pop(signo);
1528 raise(signo);
1529}
1530
1531static void pp_init(struct parallel_processes *pp,
0b0ab95f
ÆAB
1532 const struct run_process_parallel_opts *opts,
1533 struct parallel_processes_for_signal *pp_sig)
c553c72e 1534{
6e5ba0ba 1535 const size_t n = opts->processes;
c333e6f3 1536
51243f9f
ÆAB
1537 if (!n)
1538 BUG("you must provide a non-zero number of processes!");
c553c72e 1539
6a48b428
ÆAB
1540 trace_printf("run_processes_parallel: preparing to run up to %"PRIuMAX" tasks",
1541 (uintmax_t)n);
c553c72e 1542
fa93951d 1543 if (!opts->get_next_task)
033abf97 1544 BUG("you need to specify a get_next_task function");
c553c72e 1545
ca56dadb 1546 CALLOC_ARRAY(pp->children, n);
357f8e6e 1547 if (!opts->ungroup)
fd3aaf53 1548 CALLOC_ARRAY(pp->pfd, n);
c553c72e 1549
6a48b428 1550 for (size_t i = 0; i < n; i++) {
c553c72e
SB
1551 strbuf_init(&pp->children[i].err, 0);
1552 child_process_init(&pp->children[i].process);
fd3aaf53
ÆAB
1553 if (pp->pfd) {
1554 pp->pfd[i].events = POLLIN | POLLHUP;
1555 pp->pfd[i].fd = -1;
1556 }
c553c72e
SB
1557 }
1558
0b0ab95f
ÆAB
1559 pp_sig->pp = pp;
1560 pp_sig->opts = opts;
1561 pp_for_signal = pp_sig;
c553c72e
SB
1562 sigchain_push_common(handle_children_on_signal);
1563}
1564
d1610eef
ÆAB
1565static void pp_cleanup(struct parallel_processes *pp,
1566 const struct run_process_parallel_opts *opts)
c553c72e 1567{
c553c72e 1568 trace_printf("run_processes_parallel: done");
d1610eef 1569 for (size_t i = 0; i < opts->processes; i++) {
c553c72e
SB
1570 strbuf_release(&pp->children[i].err);
1571 child_process_clear(&pp->children[i].process);
1572 }
1573
1574 free(pp->children);
1575 free(pp->pfd);
1576
1577 /*
1578 * When get_next_task added messages to the buffer in its last
1579 * iteration, the buffered output is non empty.
1580 */
2dac9b56 1581 strbuf_write(&pp->buffered_output, stderr);
c553c72e
SB
1582 strbuf_release(&pp->buffered_output);
1583
1584 sigchain_pop_common();
1585}
1586
1587/* returns
1588 * 0 if a new task was started.
1589 * 1 if no new jobs was started (get_next_task ran out of work, non critical
1590 * problem with starting a new command)
1591 * <0 no new job was started, user wishes to shutdown early. Use negative code
1592 * to signal the children.
1593 */
fa93951d
ÆAB
1594static int pp_start_one(struct parallel_processes *pp,
1595 const struct run_process_parallel_opts *opts)
c553c72e 1596{
6a48b428
ÆAB
1597 size_t i;
1598 int code;
c553c72e 1599
9f3df6c0 1600 for (i = 0; i < opts->processes; i++)
c553c72e
SB
1601 if (pp->children[i].state == GIT_CP_FREE)
1602 break;
9f3df6c0 1603 if (i == opts->processes)
033abf97 1604 BUG("bookkeeping is hard");
c553c72e 1605
54026730
ES
1606 /*
1607 * By default, do not inherit stdin from the parent process - otherwise,
1608 * all children would share stdin! Users may overwrite this to provide
1609 * something to the child's stdin by having their 'get_next_task'
1610 * callback assign 0 to .no_stdin and an appropriate integer to .in.
1611 */
1612 pp->children[i].process.no_stdin = 1;
1613
fa93951d 1614 code = opts->get_next_task(&pp->children[i].process,
357f8e6e 1615 opts->ungroup ? NULL : &pp->children[i].err,
2aa8d225 1616 opts->data,
fa93951d 1617 &pp->children[i].data);
c553c72e 1618 if (!code) {
357f8e6e 1619 if (!opts->ungroup) {
fd3aaf53
ÆAB
1620 strbuf_addbuf(&pp->buffered_output, &pp->children[i].err);
1621 strbuf_reset(&pp->children[i].err);
1622 }
c553c72e
SB
1623 return 1;
1624 }
357f8e6e 1625 if (!opts->ungroup) {
fd3aaf53
ÆAB
1626 pp->children[i].process.err = -1;
1627 pp->children[i].process.stdout_to_stderr = 1;
1628 }
c553c72e
SB
1629
1630 if (start_command(&pp->children[i].process)) {
fa93951d 1631 if (opts->start_failure)
357f8e6e 1632 code = opts->start_failure(opts->ungroup ? NULL :
fa93951d 1633 &pp->children[i].err,
2aa8d225 1634 opts->data,
fa93951d
ÆAB
1635 pp->children[i].data);
1636 else
1637 code = 0;
1638
357f8e6e 1639 if (!opts->ungroup) {
fd3aaf53
ÆAB
1640 strbuf_addbuf(&pp->buffered_output, &pp->children[i].err);
1641 strbuf_reset(&pp->children[i].err);
1642 }
c553c72e
SB
1643 if (code)
1644 pp->shutdown = 1;
1645 return code;
1646 }
1647
1648 pp->nr_processes++;
1649 pp->children[i].state = GIT_CP_WORKING;
fd3aaf53
ÆAB
1650 if (pp->pfd)
1651 pp->pfd[i].fd = pp->children[i].process.err;
c553c72e
SB
1652 return 0;
1653}
1654
d1610eef
ÆAB
1655static void pp_buffer_stderr(struct parallel_processes *pp,
1656 const struct run_process_parallel_opts *opts,
1657 int output_timeout)
c553c72e 1658{
5123e6e7 1659 while (poll(pp->pfd, opts->processes, output_timeout) < 0) {
c553c72e
SB
1660 if (errno == EINTR)
1661 continue;
d1610eef 1662 pp_cleanup(pp, opts);
c553c72e
SB
1663 die_errno("poll");
1664 }
1665
1666 /* Buffer output from all pipes. */
d1610eef 1667 for (size_t i = 0; i < opts->processes; i++) {
c553c72e
SB
1668 if (pp->children[i].state == GIT_CP_WORKING &&
1669 pp->pfd[i].revents & (POLLIN | POLLHUP)) {
1670 int n = strbuf_read_once(&pp->children[i].err,
1671 pp->children[i].process.err, 0);
1672 if (n == 0) {
1673 close(pp->children[i].process.err);
1674 pp->children[i].state = GIT_CP_WAIT_CLEANUP;
1675 } else if (n < 0)
1676 if (errno != EAGAIN)
1677 die_errno("read");
1678 }
1679 }
1680}
1681
e39c9de8 1682static void pp_output(const struct parallel_processes *pp)
c553c72e 1683{
6a48b428 1684 size_t i = pp->output_owner;
fd3aaf53 1685
c553c72e
SB
1686 if (pp->children[i].state == GIT_CP_WORKING &&
1687 pp->children[i].err.len) {
2dac9b56 1688 strbuf_write(&pp->children[i].err, stderr);
c553c72e
SB
1689 strbuf_reset(&pp->children[i].err);
1690 }
1691}
1692
fa93951d
ÆAB
1693static int pp_collect_finished(struct parallel_processes *pp,
1694 const struct run_process_parallel_opts *opts)
c553c72e 1695{
6a48b428 1696 int code;
9f3df6c0 1697 size_t i;
c553c72e
SB
1698 int result = 0;
1699
1700 while (pp->nr_processes > 0) {
9f3df6c0 1701 for (i = 0; i < opts->processes; i++)
c553c72e
SB
1702 if (pp->children[i].state == GIT_CP_WAIT_CLEANUP)
1703 break;
9f3df6c0 1704 if (i == opts->processes)
c553c72e
SB
1705 break;
1706
1707 code = finish_command(&pp->children[i].process);
1708
fa93951d 1709 if (opts->task_finished)
357f8e6e 1710 code = opts->task_finished(code, opts->ungroup ? NULL :
2aa8d225 1711 &pp->children[i].err, opts->data,
fa93951d
ÆAB
1712 pp->children[i].data);
1713 else
1714 code = 0;
c553c72e
SB
1715
1716 if (code)
1717 result = code;
1718 if (code < 0)
1719 break;
1720
1721 pp->nr_processes--;
1722 pp->children[i].state = GIT_CP_FREE;
fd3aaf53
ÆAB
1723 if (pp->pfd)
1724 pp->pfd[i].fd = -1;
c553c72e
SB
1725 child_process_init(&pp->children[i].process);
1726
357f8e6e 1727 if (opts->ungroup) {
fd3aaf53
ÆAB
1728 ; /* no strbuf_*() work to do here */
1729 } else if (i != pp->output_owner) {
c553c72e
SB
1730 strbuf_addbuf(&pp->buffered_output, &pp->children[i].err);
1731 strbuf_reset(&pp->children[i].err);
1732 } else {
9f3df6c0
ÆAB
1733 const size_t n = opts->processes;
1734
2dac9b56 1735 strbuf_write(&pp->children[i].err, stderr);
c553c72e
SB
1736 strbuf_reset(&pp->children[i].err);
1737
1738 /* Output all other finished child processes */
2dac9b56 1739 strbuf_write(&pp->buffered_output, stderr);
c553c72e
SB
1740 strbuf_reset(&pp->buffered_output);
1741
1742 /*
1743 * Pick next process to output live.
1744 * NEEDSWORK:
1745 * For now we pick it randomly by doing a round
1746 * robin. Later we may want to pick the one with
1747 * the most output or the longest or shortest
1748 * running process time.
1749 */
1750 for (i = 0; i < n; i++)
1751 if (pp->children[(pp->output_owner + i) % n].state == GIT_CP_WORKING)
1752 break;
1753 pp->output_owner = (pp->output_owner + i) % n;
1754 }
1755 }
1756 return result;
1757}
1758
6e5ba0ba 1759void run_processes_parallel(const struct run_process_parallel_opts *opts)
c553c72e
SB
1760{
1761 int i, code;
1762 int output_timeout = 100;
1763 int spawn_cap = 4;
0b0ab95f 1764 struct parallel_processes_for_signal pp_sig;
c333e6f3 1765 struct parallel_processes pp = {
c333e6f3 1766 .buffered_output = STRBUF_INIT,
c333e6f3 1767 };
6e5ba0ba
ÆAB
1768 /* options */
1769 const char *tr2_category = opts->tr2_category;
1770 const char *tr2_label = opts->tr2_label;
1771 const int do_trace2 = tr2_category && tr2_label;
c553c72e 1772
6e5ba0ba
ÆAB
1773 if (do_trace2)
1774 trace2_region_enter_printf(tr2_category, tr2_label, NULL,
89e78c7c
JH
1775 "max:%"PRIuMAX,
1776 (uintmax_t)opts->processes);
fd3aaf53 1777
0b0ab95f 1778 pp_init(&pp, opts, &pp_sig);
c553c72e
SB
1779 while (1) {
1780 for (i = 0;
1781 i < spawn_cap && !pp.shutdown &&
9f3df6c0 1782 pp.nr_processes < opts->processes;
c553c72e 1783 i++) {
fa93951d 1784 code = pp_start_one(&pp, opts);
c553c72e
SB
1785 if (!code)
1786 continue;
1787 if (code < 0) {
1788 pp.shutdown = 1;
0b0ab95f 1789 kill_children(&pp, opts, -code);
c553c72e
SB
1790 }
1791 break;
1792 }
1793 if (!pp.nr_processes)
1794 break;
6e5ba0ba 1795 if (opts->ungroup) {
9f3df6c0 1796 for (size_t i = 0; i < opts->processes; i++)
fd3aaf53
ÆAB
1797 pp.children[i].state = GIT_CP_WAIT_CLEANUP;
1798 } else {
d1610eef 1799 pp_buffer_stderr(&pp, opts, output_timeout);
fd3aaf53
ÆAB
1800 pp_output(&pp);
1801 }
fa93951d 1802 code = pp_collect_finished(&pp, opts);
c553c72e
SB
1803 if (code) {
1804 pp.shutdown = 1;
1805 if (code < 0)
0b0ab95f 1806 kill_children(&pp, opts,-code);
c553c72e
SB
1807 }
1808 }
1809
d1610eef 1810 pp_cleanup(&pp, opts);
6e5ba0ba
ÆAB
1811
1812 if (do_trace2)
1813 trace2_region_leave(tr2_category, tr2_label, NULL);
c553c72e 1814}
ee4512ed 1815
b396ee6b 1816int prepare_auto_maintenance(int quiet, struct child_process *maint)
850b6ede 1817{
98077d06 1818 int enabled, auto_detach;
850b6ede 1819
1942d483
DS
1820 if (!git_config_get_bool("maintenance.auto", &enabled) &&
1821 !enabled)
1822 return 0;
1823
98077d06
PS
1824 /*
1825 * When `maintenance.autoDetach` isn't set, then we fall back to
1826 * honoring `gc.autoDetach`. This is somewhat weird, but required to
1827 * retain behaviour from when we used to run git-gc(1) here.
1828 */
1829 if (git_config_get_bool("maintenance.autodetach", &auto_detach) &&
1830 git_config_get_bool("gc.autodetach", &auto_detach))
1831 auto_detach = 1;
1832
b396ee6b
PS
1833 maint->git_cmd = 1;
1834 maint->close_object_store = 1;
1835 strvec_pushl(&maint->args, "maintenance", "run", "--auto", NULL);
1836 strvec_push(&maint->args, quiet ? "--quiet" : "--no-quiet");
98077d06 1837 strvec_push(&maint->args, auto_detach ? "--detach" : "--no-detach");
b396ee6b
PS
1838
1839 return 1;
1840}
a95ce124 1841
b396ee6b
PS
1842int run_auto_maintenance(int quiet)
1843{
1844 struct child_process maint = CHILD_PROCESS_INIT;
1845 if (!prepare_auto_maintenance(quiet, &maint))
1846 return 0;
a95ce124 1847 return run_command(&maint);
850b6ede 1848}
d1fa9435 1849
b3193252 1850void prepare_other_repo_env(struct strvec *env, const char *new_git_dir)
d1fa9435
JT
1851{
1852 const char * const *var;
1853
1854 for (var = local_repo_env; *var; var++) {
1855 if (strcmp(*var, CONFIG_DATA_ENVIRONMENT) &&
1856 strcmp(*var, CONFIG_COUNT_ENVIRONMENT))
b3193252 1857 strvec_push(env, *var);
d1fa9435 1858 }
b3193252 1859 strvec_pushf(env, "%s=%s", GIT_DIR_ENVIRONMENT, new_git_dir);
d1fa9435 1860}
fdb13226
JH
1861
1862enum start_bg_result start_bg_command(struct child_process *cmd,
1863 start_bg_wait_cb *wait_cb,
1864 void *cb_data,
1865 unsigned int timeout_sec)
1866{
1867 enum start_bg_result sbgr = SBGR_ERROR;
1868 int ret;
1869 int wait_status;
1870 pid_t pid_seen;
1871 time_t time_limit;
1872
1873 /*
1874 * We do not allow clean-on-exit because the child process
1875 * should persist in the background and possibly/probably
1876 * after this process exits. So we don't want to kill the
1877 * child during our atexit routine.
1878 */
1879 if (cmd->clean_on_exit)
1880 BUG("start_bg_command() does not allow non-zero clean_on_exit");
1881
1882 if (!cmd->trace2_child_class)
1883 cmd->trace2_child_class = "background";
1884
1885 ret = start_command(cmd);
1886 if (ret) {
1887 /*
1888 * We assume that if `start_command()` fails, we
1889 * either get a complete `trace2_child_start() /
1890 * trace2_child_exit()` pair or it fails before the
1891 * `trace2_child_start()` is emitted, so we do not
1892 * need to worry about it here.
1893 *
1894 * We also assume that `start_command()` does not add
1895 * us to the cleanup list. And that it calls
b39a8418 1896 * `child_process_clear()`.
fdb13226
JH
1897 */
1898 sbgr = SBGR_ERROR;
1899 goto done;
1900 }
1901
1902 time(&time_limit);
1903 time_limit += timeout_sec;
1904
1905wait:
1906 pid_seen = waitpid(cmd->pid, &wait_status, WNOHANG);
1907
1908 if (!pid_seen) {
1909 /*
1910 * The child is currently running. Ask the callback
1911 * if the child is ready to do work or whether we
1912 * should keep waiting for it to boot up.
1913 */
1914 ret = (*wait_cb)(cmd, cb_data);
1915 if (!ret) {
1916 /*
1917 * The child is running and "ready".
1918 */
1919 trace2_child_ready(cmd, "ready");
1920 sbgr = SBGR_READY;
1921 goto done;
1922 } else if (ret > 0) {
1923 /*
1924 * The callback said to give it more time to boot up
1925 * (subject to our timeout limit).
1926 */
1927 time_t now;
1928
1929 time(&now);
1930 if (now < time_limit)
1931 goto wait;
1932
1933 /*
1934 * Our timeout has expired. We don't try to
1935 * kill the child, but rather let it continue
1936 * (hopefully) trying to startup.
1937 */
1938 trace2_child_ready(cmd, "timeout");
1939 sbgr = SBGR_TIMEOUT;
1940 goto done;
1941 } else {
1942 /*
1943 * The cb gave up on this child. It is still running,
1944 * but our cb got an error trying to probe it.
1945 */
1946 trace2_child_ready(cmd, "error");
1947 sbgr = SBGR_CB_ERROR;
1948 goto done;
1949 }
1950 }
1951
1952 else if (pid_seen == cmd->pid) {
1953 int child_code = -1;
1954
1955 /*
1956 * The child started, but exited or was terminated
1957 * before becoming "ready".
1958 *
1959 * We try to match the behavior of `wait_or_whine()`
1960 * WRT the handling of WIFSIGNALED() and WIFEXITED()
1961 * and convert the child's status to a return code for
1962 * tracing purposes and emit the `trace2_child_exit()`
1963 * event.
1964 *
1965 * We do not want the wait_or_whine() error message
1966 * because we will be called by client-side library
1967 * routines.
1968 */
1969 if (WIFEXITED(wait_status))
1970 child_code = WEXITSTATUS(wait_status);
1971 else if (WIFSIGNALED(wait_status))
1972 child_code = WTERMSIG(wait_status) + 128;
1973 trace2_child_exit(cmd, child_code);
1974
1975 sbgr = SBGR_DIED;
1976 goto done;
1977 }
1978
1979 else if (pid_seen < 0 && errno == EINTR)
1980 goto wait;
1981
1982 trace2_child_exit(cmd, -1);
1983 sbgr = SBGR_ERROR;
1984
1985done:
1986 child_process_clear(cmd);
1987 invalidate_lstat_cache();
1988 return sbgr;
1989}