]> git.ipfire.org Git - thirdparty/git.git/blame - run-command.c
run-command: prepare command before forking
[thirdparty/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
380395d0 120#ifndef GIT_WINDOWS_NATIVE
e4507ae8
SP
121static inline void dup_devnull(int to)
122{
123 int fd = open("/dev/null", O_RDWR);
a77f106c
TR
124 if (fd < 0)
125 die_errno(_("open /dev/null failed"));
126 if (dup2(fd, to) < 0)
127 die_errno(_("dup2(%d,%d) failed"), fd, to);
e4507ae8
SP
128 close(fd);
129}
75301f90 130#endif
e4507ae8 131
38f865c2
JK
132static char *locate_in_PATH(const char *file)
133{
134 const char *p = getenv("PATH");
135 struct strbuf buf = STRBUF_INIT;
136
137 if (!p || !*p)
138 return NULL;
139
140 while (1) {
141 const char *end = strchrnul(p, ':');
142
143 strbuf_reset(&buf);
144
145 /* POSIX specifies an empty entry as the current directory. */
146 if (end != p) {
147 strbuf_add(&buf, p, end - p);
148 strbuf_addch(&buf, '/');
149 }
150 strbuf_addstr(&buf, file);
151
152 if (!access(buf.buf, F_OK))
153 return strbuf_detach(&buf, NULL);
154
155 if (!*end)
156 break;
157 p = end + 1;
158 }
159
160 strbuf_release(&buf);
161 return NULL;
162}
163
164static int exists_in_PATH(const char *file)
165{
166 char *r = locate_in_PATH(file);
167 free(r);
168 return r != NULL;
169}
170
171int sane_execvp(const char *file, char * const argv[])
172{
173 if (!execvp(file, argv))
174 return 0; /* cannot happen ;-) */
175
176 /*
177 * When a command can't be found because one of the directories
178 * listed in $PATH is unsearchable, execvp reports EACCES, but
179 * careful usability testing (read: analysis of occasional bug
180 * reports) reveals that "No such file or directory" is more
181 * intuitive.
182 *
183 * We avoid commands with "/", because execvp will not do $PATH
184 * lookups in that case.
185 *
186 * The reassignment of EACCES to errno looks like a no-op below,
187 * but we need to protect against exists_in_PATH overwriting errno.
188 */
189 if (errno == EACCES && !strchr(file, '/'))
190 errno = exists_in_PATH(file) ? EACCES : ENOENT;
a7855083
JH
191 else if (errno == ENOTDIR && !strchr(file, '/'))
192 errno = ENOENT;
38f865c2
JK
193 return -1;
194}
195
20574f55 196static const char **prepare_shell_cmd(struct argv_array *out, const char **argv)
8dba1e63 197{
20574f55 198 if (!argv[0])
8dba1e63
JK
199 die("BUG: shell command is empty");
200
f445644f 201 if (strcspn(argv[0], "|&;<>()$`\\\"' \t\n*?[#~=%") != strlen(argv[0])) {
380395d0 202#ifndef GIT_WINDOWS_NATIVE
20574f55 203 argv_array_push(out, SHELL_PATH);
77629754 204#else
20574f55 205 argv_array_push(out, "sh");
77629754 206#endif
20574f55 207 argv_array_push(out, "-c");
8dba1e63 208
20574f55
JK
209 /*
210 * If we have no extra arguments, we do not even need to
211 * bother with the "$@" magic.
212 */
213 if (!argv[1])
214 argv_array_push(out, argv[0]);
215 else
216 argv_array_pushf(out, "%s \"$@\"", argv[0]);
217 }
8dba1e63 218
20574f55
JK
219 argv_array_pushv(out, argv);
220 return out->argv;
8dba1e63
JK
221}
222
380395d0 223#ifndef GIT_WINDOWS_NATIVE
2b541bf8
JS
224static int child_notifier = -1;
225
226static void notify_parent(void)
227{
a111eb78
JN
228 /*
229 * execvp failed. If possible, we'd like to let start_command
230 * know, so failures like ENOENT can be handled right away; but
231 * otherwise, finish_command will still report the error.
232 */
233 xwrite(child_notifier, "", 1);
2b541bf8 234}
3967e25b
BW
235
236static void prepare_cmd(struct argv_array *out, const struct child_process *cmd)
237{
238 if (!cmd->argv[0])
239 die("BUG: command is empty");
240
241 if (cmd->git_cmd) {
242 argv_array_push(out, "git");
243 argv_array_pushv(out, cmd->argv);
244 } else if (cmd->use_shell) {
245 prepare_shell_cmd(out, cmd->argv);
246 } else {
247 argv_array_pushv(out, cmd->argv);
248 }
249}
200a76b7 250#endif
a5487ddf
JS
251
252static inline void set_cloexec(int fd)
253{
254 int flags = fcntl(fd, F_GETFD);
255 if (flags >= 0)
256 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
257}
a5487ddf 258
507d7804 259static int wait_or_whine(pid_t pid, const char *argv0, int in_signal)
ab0b41da
JS
260{
261 int status, code = -1;
262 pid_t waiting;
263 int failed_errno = 0;
264
265 while ((waiting = waitpid(pid, &status, 0)) < 0 && errno == EINTR)
266 ; /* nothing */
507d7804
TI
267 if (in_signal)
268 return 0;
ab0b41da
JS
269
270 if (waiting < 0) {
271 failed_errno = errno;
fbcb0e06 272 error_errno("waitpid for %s failed", argv0);
ab0b41da
JS
273 } else if (waiting != pid) {
274 error("waitpid is confused (%s)", argv0);
275 } else if (WIFSIGNALED(status)) {
276 code = WTERMSIG(status);
ac78663b 277 if (code != SIGINT && code != SIGQUIT && code != SIGPIPE)
a2767c5c 278 error("%s died of signal %d", argv0, code);
ab0b41da
JS
279 /*
280 * This return value is chosen so that code & 0xff
281 * mimics the exit code that a POSIX shell would report for
282 * a program that died from this signal.
283 */
709ca730 284 code += 128;
ab0b41da
JS
285 } else if (WIFEXITED(status)) {
286 code = WEXITSTATUS(status);
287 /*
288 * Convert special exit code when execvp failed.
289 */
290 if (code == 127) {
291 code = -1;
292 failed_errno = ENOENT;
ab0b41da
JS
293 }
294 } else {
295 error("waitpid is confused (%s)", argv0);
296 }
afe19ff7
JK
297
298 clear_child_for_cleanup(pid);
299
ab0b41da
JS
300 errno = failed_errno;
301 return code;
302}
303
ebcb5d16 304int start_command(struct child_process *cmd)
b1bf95bb 305{
f3b33f1d
JS
306 int need_in, need_out, need_err;
307 int fdin[2], fdout[2], fderr[2];
25043d8a 308 int failed_errno;
939296c4 309 char *str;
4919bf03 310
c460c0ec
JK
311 if (!cmd->argv)
312 cmd->argv = cmd->args.argv;
19a583dc
RS
313 if (!cmd->env)
314 cmd->env = cmd->env_array.argv;
c460c0ec 315
c20181e3
JS
316 /*
317 * In case of errors we must keep the promise to close FDs
318 * that have been passed in via ->in and ->out.
319 */
320
e4507ae8 321 need_in = !cmd->no_stdin && cmd->in < 0;
4919bf03 322 if (need_in) {
c20181e3 323 if (pipe(fdin) < 0) {
0ac77ec3 324 failed_errno = errno;
c20181e3
JS
325 if (cmd->out > 0)
326 close(cmd->out);
939296c4 327 str = "standard input";
0ac77ec3 328 goto fail_pipe;
c20181e3 329 }
4919bf03 330 cmd->in = fdin[1];
4919bf03
SP
331 }
332
e4507ae8
SP
333 need_out = !cmd->no_stdout
334 && !cmd->stdout_to_stderr
335 && cmd->out < 0;
f4bba25b
SP
336 if (need_out) {
337 if (pipe(fdout) < 0) {
0ac77ec3 338 failed_errno = errno;
f4bba25b
SP
339 if (need_in)
340 close_pair(fdin);
c20181e3
JS
341 else if (cmd->in)
342 close(cmd->in);
939296c4 343 str = "standard output";
0ac77ec3 344 goto fail_pipe;
f4bba25b
SP
345 }
346 cmd->out = fdout[0];
f4bba25b
SP
347 }
348
b73a4397 349 need_err = !cmd->no_stderr && cmd->err < 0;
f3b33f1d
JS
350 if (need_err) {
351 if (pipe(fderr) < 0) {
0ac77ec3 352 failed_errno = errno;
f3b33f1d
JS
353 if (need_in)
354 close_pair(fdin);
c20181e3
JS
355 else if (cmd->in)
356 close(cmd->in);
f3b33f1d
JS
357 if (need_out)
358 close_pair(fdout);
c20181e3
JS
359 else if (cmd->out)
360 close(cmd->out);
939296c4 361 str = "standard error";
0ac77ec3 362fail_pipe:
939296c4
SB
363 error("cannot create %s pipe for %s: %s",
364 str, cmd->argv[0], strerror(failed_errno));
2d71608e 365 child_process_clear(cmd);
0ac77ec3
JS
366 errno = failed_errno;
367 return -1;
f3b33f1d
JS
368 }
369 cmd->err = fderr[0];
370 }
371
8852f5d7 372 trace_argv_printf(cmd->argv, "trace: run_command:");
13af8cbd 373 fflush(NULL);
8852f5d7 374
380395d0 375#ifndef GIT_WINDOWS_NATIVE
2b541bf8
JS
376{
377 int notify_pipe[2];
3967e25b
BW
378 struct argv_array argv = ARGV_ARRAY_INIT;
379
2b541bf8
JS
380 if (pipe(notify_pipe))
381 notify_pipe[0] = notify_pipe[1] = -1;
382
3967e25b
BW
383 prepare_cmd(&argv, cmd);
384
ebcb5d16 385 cmd->pid = fork();
25043d8a 386 failed_errno = errno;
ebcb5d16 387 if (!cmd->pid) {
a5487ddf
JS
388 /*
389 * Redirect the channel to write syscall error messages to
390 * before redirecting the process's stderr so that all die()
391 * in subsequent call paths use the parent's stderr.
392 */
393 if (cmd->no_stderr || need_err) {
3b331e92 394 int child_err = dup(2);
a5487ddf 395 set_cloexec(child_err);
3b331e92 396 set_error_handle(fdopen(child_err, "w"));
a5487ddf 397 }
a5487ddf 398
2b541bf8
JS
399 close(notify_pipe[0]);
400 set_cloexec(notify_pipe[1]);
401 child_notifier = notify_pipe[1];
402 atexit(notify_parent);
403
e4507ae8
SP
404 if (cmd->no_stdin)
405 dup_devnull(0);
406 else if (need_in) {
4919bf03 407 dup2(fdin[0], 0);
9dc09c76 408 close_pair(fdin);
4919bf03
SP
409 } else if (cmd->in) {
410 dup2(cmd->in, 0);
411 close(cmd->in);
95d3c4f5 412 }
4919bf03 413
ce2cf27a
CC
414 if (cmd->no_stderr)
415 dup_devnull(2);
416 else if (need_err) {
417 dup2(fderr[1], 2);
418 close_pair(fderr);
4f41b611
SP
419 } else if (cmd->err > 1) {
420 dup2(cmd->err, 2);
421 close(cmd->err);
ce2cf27a
CC
422 }
423
e4507ae8
SP
424 if (cmd->no_stdout)
425 dup_devnull(1);
426 else if (cmd->stdout_to_stderr)
cd83c74c 427 dup2(2, 1);
f4bba25b
SP
428 else if (need_out) {
429 dup2(fdout[1], 1);
430 close_pair(fdout);
431 } else if (cmd->out > 1) {
432 dup2(cmd->out, 1);
433 close(cmd->out);
434 }
435
1568fea0 436 if (cmd->dir && chdir(cmd->dir))
d824cbba
TR
437 die_errno("exec '%s': cd to '%s' failed", cmd->argv[0],
438 cmd->dir);
ee493148 439 if (cmd->env) {
3427b375
AR
440 for (; *cmd->env; cmd->env++) {
441 if (strchr(*cmd->env, '='))
4b25d091 442 putenv((char *)*cmd->env);
3427b375
AR
443 else
444 unsetenv(*cmd->env);
445 }
ee493148 446 }
3967e25b
BW
447
448 sane_execvp(argv.argv[0], (char *const *) argv.argv);
449
fc1b56f0
CB
450 if (errno == ENOENT) {
451 if (!cmd->silent_exec_failure)
452 error("cannot run %s: %s", cmd->argv[0],
453 strerror(ENOENT));
a5487ddf 454 exit(127);
fc1b56f0 455 } else {
a5487ddf 456 die_errno("cannot exec '%s'", cmd->argv[0]);
fc1b56f0 457 }
b1bf95bb 458 }
0ac77ec3 459 if (cmd->pid < 0)
fbcb0e06 460 error_errno("cannot fork() for %s", cmd->argv[0]);
afe19ff7 461 else if (cmd->clean_on_exit)
ac2fbaa6 462 mark_child_for_cleanup(cmd->pid, cmd);
2b541bf8
JS
463
464 /*
3967e25b 465 * Wait for child's exec. If the exec succeeds (or if fork()
2b541bf8
JS
466 * failed), EOF is seen immediately by the parent. Otherwise, the
467 * child process sends a single byte.
468 * Note that use of this infrastructure is completely advisory,
469 * therefore, we keep error checks minimal.
470 */
471 close(notify_pipe[1]);
472 if (read(notify_pipe[0], &notify_pipe[1], 1) == 1) {
473 /*
3967e25b 474 * At this point we know that fork() succeeded, but exec()
2b541bf8
JS
475 * failed. Errors have been reported to our stderr.
476 */
507d7804 477 wait_or_whine(cmd->pid, cmd->argv[0], 0);
2b541bf8
JS
478 failed_errno = errno;
479 cmd->pid = -1;
480 }
481 close(notify_pipe[0]);
3967e25b
BW
482
483 argv_array_clear(&argv);
2b541bf8 484}
ba26f296 485#else
0d30ad71 486{
75301f90 487 int fhin = 0, fhout = 1, fherr = 2;
108ac313 488 const char **sargv = cmd->argv;
20574f55 489 struct argv_array nargv = ARGV_ARRAY_INIT;
ba26f296 490
75301f90
JS
491 if (cmd->no_stdin)
492 fhin = open("/dev/null", O_RDWR);
493 else if (need_in)
494 fhin = dup(fdin[0]);
495 else if (cmd->in)
496 fhin = dup(cmd->in);
497
498 if (cmd->no_stderr)
499 fherr = open("/dev/null", O_RDWR);
500 else if (need_err)
501 fherr = dup(fderr[1]);
76d44c8c
JH
502 else if (cmd->err > 2)
503 fherr = dup(cmd->err);
75301f90
JS
504
505 if (cmd->no_stdout)
506 fhout = open("/dev/null", O_RDWR);
507 else if (cmd->stdout_to_stderr)
508 fhout = dup(fherr);
509 else if (need_out)
510 fhout = dup(fdout[1]);
511 else if (cmd->out > 1)
512 fhout = dup(cmd->out);
ba26f296 513
5a50085c 514 if (cmd->git_cmd)
20574f55 515 cmd->argv = prepare_git_cmd(&nargv, cmd->argv);
5a50085c 516 else if (cmd->use_shell)
20574f55 517 cmd->argv = prepare_shell_cmd(&nargv, cmd->argv);
ba26f296 518
77734da2
KB
519 cmd->pid = mingw_spawnvpe(cmd->argv[0], cmd->argv, (char**) cmd->env,
520 cmd->dir, fhin, fhout, fherr);
0ac77ec3 521 failed_errno = errno;
c024beb5 522 if (cmd->pid < 0 && (!cmd->silent_exec_failure || errno != ENOENT))
fbcb0e06 523 error_errno("cannot spawn %s", cmd->argv[0]);
afe19ff7 524 if (cmd->clean_on_exit && cmd->pid >= 0)
ac2fbaa6 525 mark_child_for_cleanup(cmd->pid, cmd);
ba26f296 526
20574f55 527 argv_array_clear(&nargv);
108ac313 528 cmd->argv = sargv;
75301f90
JS
529 if (fhin != 0)
530 close(fhin);
531 if (fhout != 1)
532 close(fhout);
533 if (fherr != 2)
534 close(fherr);
0d30ad71 535}
ba26f296
JS
536#endif
537
538 if (cmd->pid < 0) {
539 if (need_in)
540 close_pair(fdin);
541 else if (cmd->in)
542 close(cmd->in);
543 if (need_out)
544 close_pair(fdout);
545 else if (cmd->out)
546 close(cmd->out);
547 if (need_err)
548 close_pair(fderr);
fc012c28
D
549 else if (cmd->err)
550 close(cmd->err);
2d71608e 551 child_process_clear(cmd);
0ac77ec3
JS
552 errno = failed_errno;
553 return -1;
ba26f296 554 }
4919bf03
SP
555
556 if (need_in)
557 close(fdin[0]);
558 else if (cmd->in)
559 close(cmd->in);
560
f4bba25b
SP
561 if (need_out)
562 close(fdout[1]);
c20181e3 563 else if (cmd->out)
f4bba25b
SP
564 close(cmd->out);
565
f3b33f1d
JS
566 if (need_err)
567 close(fderr[1]);
4f41b611
SP
568 else if (cmd->err)
569 close(cmd->err);
f3b33f1d 570
ebcb5d16
SP
571 return 0;
572}
573
2d22c208
JS
574int finish_command(struct child_process *cmd)
575{
507d7804 576 int ret = wait_or_whine(cmd->pid, cmd->argv[0], 0);
2d71608e 577 child_process_clear(cmd);
c460c0ec 578 return ret;
2d22c208
JS
579}
580
507d7804
TI
581int finish_command_in_signal(struct child_process *cmd)
582{
583 return wait_or_whine(cmd->pid, cmd->argv[0], 1);
584}
585
586
ebcb5d16
SP
587int run_command(struct child_process *cmd)
588{
c29b3962
JK
589 int code;
590
591 if (cmd->out < 0 || cmd->err < 0)
592 die("BUG: run_command with a pipe can cause deadlock");
593
594 code = start_command(cmd);
ebcb5d16
SP
595 if (code)
596 return code;
597 return finish_command(cmd);
598}
599
f1000898
SP
600int run_command_v_opt(const char **argv, int opt)
601{
41e9bad7 602 return run_command_v_opt_cd_env(argv, opt, NULL, NULL);
1568fea0
AR
603}
604
ee493148
AR
605int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env)
606{
1f87293d
RS
607 struct child_process cmd = CHILD_PROCESS_INIT;
608 cmd.argv = argv;
609 cmd.no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
610 cmd.git_cmd = opt & RUN_GIT_CMD ? 1 : 0;
611 cmd.stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
612 cmd.silent_exec_failure = opt & RUN_SILENT_EXEC_FAILURE ? 1 : 0;
613 cmd.use_shell = opt & RUN_USING_SHELL ? 1 : 0;
614 cmd.clean_on_exit = opt & RUN_CLEAN_ON_EXIT ? 1 : 0;
ee493148
AR
615 cmd.dir = dir;
616 cmd.env = env;
617 return run_command(&cmd);
618}
2d22c208 619
f6b60983 620#ifndef NO_PTHREADS
0ea1c89b
JS
621static pthread_t main_thread;
622static int main_thread_set;
623static pthread_key_t async_key;
1ece66bc 624static pthread_key_t async_die_counter;
0ea1c89b 625
200a76b7 626static void *run_thread(void *data)
618ebe9f
JS
627{
628 struct async *async = data;
f6b60983 629 intptr_t ret;
0ea1c89b 630
c792d7b6
JK
631 if (async->isolate_sigpipe) {
632 sigset_t mask;
633 sigemptyset(&mask);
634 sigaddset(&mask, SIGPIPE);
635 if (pthread_sigmask(SIG_BLOCK, &mask, NULL) < 0) {
636 ret = error("unable to block SIGPIPE in async thread");
637 return (void *)ret;
638 }
639 }
640
0ea1c89b 641 pthread_setspecific(async_key, async);
f6b60983 642 ret = async->proc(async->proc_in, async->proc_out, async->data);
200a76b7 643 return (void *)ret;
618ebe9f 644}
0ea1c89b
JS
645
646static NORETURN void die_async(const char *err, va_list params)
647{
648 vreportf("fatal: ", err, params);
649
661a8cf4 650 if (in_async()) {
0ea1c89b
JS
651 struct async *async = pthread_getspecific(async_key);
652 if (async->proc_in >= 0)
653 close(async->proc_in);
654 if (async->proc_out >= 0)
655 close(async->proc_out);
656 pthread_exit((void *)128);
657 }
658
659 exit(128);
618ebe9f 660}
1ece66bc
JK
661
662static int async_die_is_recursing(void)
663{
664 void *ret = pthread_getspecific(async_die_counter);
665 pthread_setspecific(async_die_counter, (void *)1);
666 return ret != NULL;
667}
668
661a8cf4
JK
669int in_async(void)
670{
671 if (!main_thread_set)
672 return 0; /* no asyncs started yet */
673 return !pthread_equal(main_thread, pthread_self());
674}
675
b992fe10 676static void NORETURN async_exit(int code)
9658846c
JK
677{
678 pthread_exit((void *)(intptr_t)code);
679}
680
0f4b6db3
EB
681#else
682
683static struct {
684 void (**handlers)(void);
685 size_t nr;
686 size_t alloc;
687} git_atexit_hdlrs;
688
689static int git_atexit_installed;
690
6066a7ea 691static void git_atexit_dispatch(void)
0f4b6db3
EB
692{
693 size_t i;
694
695 for (i=git_atexit_hdlrs.nr ; i ; i--)
696 git_atexit_hdlrs.handlers[i-1]();
697}
698
6066a7ea 699static void git_atexit_clear(void)
0f4b6db3
EB
700{
701 free(git_atexit_hdlrs.handlers);
702 memset(&git_atexit_hdlrs, 0, sizeof(git_atexit_hdlrs));
703 git_atexit_installed = 0;
704}
705
706#undef atexit
707int git_atexit(void (*handler)(void))
708{
709 ALLOC_GROW(git_atexit_hdlrs.handlers, git_atexit_hdlrs.nr + 1, git_atexit_hdlrs.alloc);
710 git_atexit_hdlrs.handlers[git_atexit_hdlrs.nr++] = handler;
711 if (!git_atexit_installed) {
712 if (atexit(&git_atexit_dispatch))
713 return -1;
714 git_atexit_installed = 1;
715 }
716 return 0;
717}
718#define atexit git_atexit
719
661a8cf4
JK
720static int process_is_async;
721int in_async(void)
722{
723 return process_is_async;
724}
725
b992fe10 726static void NORETURN async_exit(int code)
9658846c
JK
727{
728 exit(code);
729}
730
618ebe9f
JS
731#endif
732
b992fe10
LS
733void check_pipe(int err)
734{
735 if (err == EPIPE) {
736 if (in_async())
737 async_exit(141);
738
739 signal(SIGPIPE, SIG_DFL);
740 raise(SIGPIPE);
741 /* Should never happen, but just in case... */
742 exit(141);
743 }
744}
745
2d22c208
JS
746int start_async(struct async *async)
747{
ae6a5609
EFL
748 int need_in, need_out;
749 int fdin[2], fdout[2];
750 int proc_in, proc_out;
2d22c208 751
ae6a5609
EFL
752 need_in = async->in < 0;
753 if (need_in) {
754 if (pipe(fdin) < 0) {
755 if (async->out > 0)
756 close(async->out);
fbcb0e06 757 return error_errno("cannot create pipe");
ae6a5609
EFL
758 }
759 async->in = fdin[1];
760 }
761
762 need_out = async->out < 0;
763 if (need_out) {
764 if (pipe(fdout) < 0) {
765 if (need_in)
766 close_pair(fdin);
767 else if (async->in)
768 close(async->in);
fbcb0e06 769 return error_errno("cannot create pipe");
ae6a5609
EFL
770 }
771 async->out = fdout[0];
772 }
773
774 if (need_in)
775 proc_in = fdin[0];
776 else if (async->in)
777 proc_in = async->in;
778 else
779 proc_in = -1;
780
781 if (need_out)
782 proc_out = fdout[1];
783 else if (async->out)
784 proc_out = async->out;
785 else
786 proc_out = -1;
2d22c208 787
f6b60983 788#ifdef NO_PTHREADS
2c3766f0
AM
789 /* Flush stdio before fork() to avoid cloning buffers */
790 fflush(NULL);
791
2d22c208
JS
792 async->pid = fork();
793 if (async->pid < 0) {
fbcb0e06 794 error_errno("fork (async) failed");
ae6a5609 795 goto error;
2d22c208
JS
796 }
797 if (!async->pid) {
ae6a5609
EFL
798 if (need_in)
799 close(fdin[1]);
800 if (need_out)
801 close(fdout[0]);
0f4b6db3 802 git_atexit_clear();
661a8cf4 803 process_is_async = 1;
ae6a5609 804 exit(!!async->proc(proc_in, proc_out, async->data));
2d22c208 805 }
ae6a5609 806
ac2fbaa6 807 mark_child_for_cleanup(async->pid, NULL);
afe19ff7 808
ae6a5609
EFL
809 if (need_in)
810 close(fdin[0]);
811 else if (async->in)
812 close(async->in);
813
814 if (need_out)
815 close(fdout[1]);
816 else if (async->out)
817 close(async->out);
618ebe9f 818#else
0ea1c89b
JS
819 if (!main_thread_set) {
820 /*
821 * We assume that the first time that start_async is called
822 * it is from the main thread.
823 */
824 main_thread_set = 1;
825 main_thread = pthread_self();
826 pthread_key_create(&async_key, NULL);
1ece66bc 827 pthread_key_create(&async_die_counter, NULL);
0ea1c89b 828 set_die_routine(die_async);
1ece66bc 829 set_die_is_recursing_routine(async_die_is_recursing);
0ea1c89b
JS
830 }
831
200a76b7
JS
832 if (proc_in >= 0)
833 set_cloexec(proc_in);
834 if (proc_out >= 0)
835 set_cloexec(proc_out);
ae6a5609
EFL
836 async->proc_in = proc_in;
837 async->proc_out = proc_out;
200a76b7
JS
838 {
839 int err = pthread_create(&async->tid, NULL, run_thread, async);
840 if (err) {
fbcb0e06 841 error_errno("cannot create thread");
200a76b7
JS
842 goto error;
843 }
618ebe9f
JS
844 }
845#endif
2d22c208 846 return 0;
ae6a5609
EFL
847
848error:
849 if (need_in)
850 close_pair(fdin);
851 else if (async->in)
852 close(async->in);
853
854 if (need_out)
855 close_pair(fdout);
856 else if (async->out)
857 close(async->out);
858 return -1;
2d22c208
JS
859}
860
861int finish_async(struct async *async)
862{
f6b60983 863#ifdef NO_PTHREADS
507d7804 864 return wait_or_whine(async->pid, "child process", 0);
618ebe9f 865#else
200a76b7
JS
866 void *ret = (void *)(intptr_t)(-1);
867
868 if (pthread_join(async->tid, &ret))
869 error("pthread_join failed");
870 return (int)(intptr_t)ret;
618ebe9f 871#endif
2d22c208 872}
ae98a008 873
dcf69262 874const char *find_hook(const char *name)
5a7da2dc 875{
03f2c773 876 static struct strbuf path = STRBUF_INIT;
5a7da2dc 877
03f2c773 878 strbuf_reset(&path);
9445b492 879 strbuf_git_path(&path, "hooks/%s", name);
235be51f
JS
880 if (access(path.buf, X_OK) < 0) {
881#ifdef STRIP_EXTENSION
882 strbuf_addstr(&path, STRIP_EXTENSION);
883 if (access(path.buf, X_OK) >= 0)
884 return path.buf;
885#endif
03f2c773 886 return NULL;
235be51f 887 }
03f2c773 888 return path.buf;
5a7da2dc
AS
889}
890
15048f8a 891int run_hook_ve(const char *const *env, const char *name, va_list args)
ae98a008 892{
d3180279 893 struct child_process hook = CHILD_PROCESS_INIT;
15048f8a 894 const char *p;
ae98a008 895
5a7da2dc
AS
896 p = find_hook(name);
897 if (!p)
cf94ca8e
SB
898 return 0;
899
d1d09456
RS
900 argv_array_push(&hook.args, p);
901 while ((p = va_arg(args, const char *)))
902 argv_array_push(&hook.args, p);
15048f8a 903 hook.env = env;
ae98a008
SB
904 hook.no_stdin = 1;
905 hook.stdout_to_stderr = 1;
ae98a008 906
d1d09456 907 return run_command(&hook);
ae98a008 908}
15048f8a
BP
909
910int run_hook_le(const char *const *env, const char *name, ...)
911{
912 va_list args;
913 int ret;
914
915 va_start(args, name);
916 ret = run_hook_ve(env, name, args);
917 va_end(args);
918
919 return ret;
920}
911ec99b 921
96335bcf
JK
922struct io_pump {
923 /* initialized by caller */
924 int fd;
925 int type; /* POLLOUT or POLLIN */
926 union {
927 struct {
928 const char *buf;
929 size_t len;
930 } out;
931 struct {
932 struct strbuf *buf;
933 size_t hint;
934 } in;
935 } u;
936
937 /* returned by pump_io */
938 int error; /* 0 for success, otherwise errno */
939
940 /* internal use */
941 struct pollfd *pfd;
942};
943
944static int pump_io_round(struct io_pump *slots, int nr, struct pollfd *pfd)
945{
946 int pollsize = 0;
947 int i;
948
949 for (i = 0; i < nr; i++) {
950 struct io_pump *io = &slots[i];
951 if (io->fd < 0)
952 continue;
953 pfd[pollsize].fd = io->fd;
954 pfd[pollsize].events = io->type;
955 io->pfd = &pfd[pollsize++];
956 }
957
958 if (!pollsize)
959 return 0;
960
961 if (poll(pfd, pollsize, -1) < 0) {
962 if (errno == EINTR)
963 return 1;
964 die_errno("poll failed");
965 }
966
967 for (i = 0; i < nr; i++) {
968 struct io_pump *io = &slots[i];
969
970 if (io->fd < 0)
971 continue;
972
973 if (!(io->pfd->revents & (POLLOUT|POLLIN|POLLHUP|POLLERR|POLLNVAL)))
974 continue;
975
976 if (io->type == POLLOUT) {
977 ssize_t len = xwrite(io->fd,
978 io->u.out.buf, io->u.out.len);
979 if (len < 0) {
980 io->error = errno;
981 close(io->fd);
982 io->fd = -1;
983 } else {
984 io->u.out.buf += len;
985 io->u.out.len -= len;
986 if (!io->u.out.len) {
987 close(io->fd);
988 io->fd = -1;
989 }
990 }
991 }
992
993 if (io->type == POLLIN) {
994 ssize_t len = strbuf_read_once(io->u.in.buf,
995 io->fd, io->u.in.hint);
996 if (len < 0)
997 io->error = errno;
998 if (len <= 0) {
999 close(io->fd);
1000 io->fd = -1;
1001 }
1002 }
1003 }
1004
1005 return 1;
1006}
1007
1008static int pump_io(struct io_pump *slots, int nr)
911ec99b 1009{
96335bcf
JK
1010 struct pollfd *pfd;
1011 int i;
1012
1013 for (i = 0; i < nr; i++)
1014 slots[i].error = 0;
1015
1016 ALLOC_ARRAY(pfd, nr);
1017 while (pump_io_round(slots, nr, pfd))
1018 ; /* nothing */
1019 free(pfd);
1020
1021 /* There may be multiple errno values, so just pick the first. */
1022 for (i = 0; i < nr; i++) {
1023 if (slots[i].error) {
1024 errno = slots[i].error;
1025 return -1;
1026 }
1027 }
1028 return 0;
1029}
1030
1031
1032int pipe_command(struct child_process *cmd,
1033 const char *in, size_t in_len,
1034 struct strbuf *out, size_t out_hint,
1035 struct strbuf *err, size_t err_hint)
1036{
1037 struct io_pump io[3];
1038 int nr = 0;
1039
1040 if (in)
1041 cmd->in = -1;
1042 if (out)
1043 cmd->out = -1;
1044 if (err)
1045 cmd->err = -1;
1046
911ec99b
JK
1047 if (start_command(cmd) < 0)
1048 return -1;
1049
96335bcf
JK
1050 if (in) {
1051 io[nr].fd = cmd->in;
1052 io[nr].type = POLLOUT;
1053 io[nr].u.out.buf = in;
1054 io[nr].u.out.len = in_len;
1055 nr++;
1056 }
1057 if (out) {
1058 io[nr].fd = cmd->out;
1059 io[nr].type = POLLIN;
1060 io[nr].u.in.buf = out;
1061 io[nr].u.in.hint = out_hint;
1062 nr++;
1063 }
1064 if (err) {
1065 io[nr].fd = cmd->err;
1066 io[nr].type = POLLIN;
1067 io[nr].u.in.buf = err;
1068 io[nr].u.in.hint = err_hint;
1069 nr++;
1070 }
1071
1072 if (pump_io(io, nr) < 0) {
911ec99b
JK
1073 finish_command(cmd); /* throw away exit code */
1074 return -1;
1075 }
1076
911ec99b
JK
1077 return finish_command(cmd);
1078}
c553c72e
SB
1079
1080enum child_state {
1081 GIT_CP_FREE,
1082 GIT_CP_WORKING,
1083 GIT_CP_WAIT_CLEANUP,
1084};
1085
1086struct parallel_processes {
1087 void *data;
1088
1089 int max_processes;
1090 int nr_processes;
1091
1092 get_next_task_fn get_next_task;
1093 start_failure_fn start_failure;
1094 task_finished_fn task_finished;
1095
1096 struct {
1097 enum child_state state;
1098 struct child_process process;
1099 struct strbuf err;
1100 void *data;
1101 } *children;
1102 /*
1103 * The struct pollfd is logically part of *children,
1104 * but the system call expects it as its own array.
1105 */
1106 struct pollfd *pfd;
1107
1108 unsigned shutdown : 1;
1109
1110 int output_owner;
1111 struct strbuf buffered_output; /* of finished children */
1112};
1113
aa710494 1114static int default_start_failure(struct strbuf *out,
c553c72e
SB
1115 void *pp_cb,
1116 void *pp_task_cb)
1117{
c553c72e
SB
1118 return 0;
1119}
1120
1121static int default_task_finished(int result,
aa710494 1122 struct strbuf *out,
c553c72e
SB
1123 void *pp_cb,
1124 void *pp_task_cb)
1125{
c553c72e
SB
1126 return 0;
1127}
1128
1129static void kill_children(struct parallel_processes *pp, int signo)
1130{
1131 int i, n = pp->max_processes;
1132
1133 for (i = 0; i < n; i++)
1134 if (pp->children[i].state == GIT_CP_WORKING)
1135 kill(pp->children[i].process.pid, signo);
1136}
1137
1138static struct parallel_processes *pp_for_signal;
1139
1140static void handle_children_on_signal(int signo)
1141{
1142 kill_children(pp_for_signal, signo);
1143 sigchain_pop(signo);
1144 raise(signo);
1145}
1146
1147static void pp_init(struct parallel_processes *pp,
1148 int n,
1149 get_next_task_fn get_next_task,
1150 start_failure_fn start_failure,
1151 task_finished_fn task_finished,
1152 void *data)
1153{
1154 int i;
1155
1156 if (n < 1)
1157 n = online_cpus();
1158
1159 pp->max_processes = n;
1160
1161 trace_printf("run_processes_parallel: preparing to run up to %d tasks", n);
1162
1163 pp->data = data;
1164 if (!get_next_task)
1165 die("BUG: you need to specify a get_next_task function");
1166 pp->get_next_task = get_next_task;
1167
1168 pp->start_failure = start_failure ? start_failure : default_start_failure;
1169 pp->task_finished = task_finished ? task_finished : default_task_finished;
1170
1171 pp->nr_processes = 0;
1172 pp->output_owner = 0;
1173 pp->shutdown = 0;
1174 pp->children = xcalloc(n, sizeof(*pp->children));
1175 pp->pfd = xcalloc(n, sizeof(*pp->pfd));
1176 strbuf_init(&pp->buffered_output, 0);
1177
1178 for (i = 0; i < n; i++) {
1179 strbuf_init(&pp->children[i].err, 0);
1180 child_process_init(&pp->children[i].process);
1181 pp->pfd[i].events = POLLIN | POLLHUP;
1182 pp->pfd[i].fd = -1;
1183 }
1184
1185 pp_for_signal = pp;
1186 sigchain_push_common(handle_children_on_signal);
1187}
1188
1189static void pp_cleanup(struct parallel_processes *pp)
1190{
1191 int i;
1192
1193 trace_printf("run_processes_parallel: done");
1194 for (i = 0; i < pp->max_processes; i++) {
1195 strbuf_release(&pp->children[i].err);
1196 child_process_clear(&pp->children[i].process);
1197 }
1198
1199 free(pp->children);
1200 free(pp->pfd);
1201
1202 /*
1203 * When get_next_task added messages to the buffer in its last
1204 * iteration, the buffered output is non empty.
1205 */
2dac9b56 1206 strbuf_write(&pp->buffered_output, stderr);
c553c72e
SB
1207 strbuf_release(&pp->buffered_output);
1208
1209 sigchain_pop_common();
1210}
1211
1212/* returns
1213 * 0 if a new task was started.
1214 * 1 if no new jobs was started (get_next_task ran out of work, non critical
1215 * problem with starting a new command)
1216 * <0 no new job was started, user wishes to shutdown early. Use negative code
1217 * to signal the children.
1218 */
1219static int pp_start_one(struct parallel_processes *pp)
1220{
1221 int i, code;
1222
1223 for (i = 0; i < pp->max_processes; i++)
1224 if (pp->children[i].state == GIT_CP_FREE)
1225 break;
1226 if (i == pp->max_processes)
1227 die("BUG: bookkeeping is hard");
1228
1229 code = pp->get_next_task(&pp->children[i].process,
1230 &pp->children[i].err,
1231 pp->data,
1232 &pp->children[i].data);
1233 if (!code) {
1234 strbuf_addbuf(&pp->buffered_output, &pp->children[i].err);
1235 strbuf_reset(&pp->children[i].err);
1236 return 1;
1237 }
1238 pp->children[i].process.err = -1;
1239 pp->children[i].process.stdout_to_stderr = 1;
1240 pp->children[i].process.no_stdin = 1;
1241
1242 if (start_command(&pp->children[i].process)) {
2a73b3da 1243 code = pp->start_failure(&pp->children[i].err,
c553c72e
SB
1244 pp->data,
1245 &pp->children[i].data);
1246 strbuf_addbuf(&pp->buffered_output, &pp->children[i].err);
1247 strbuf_reset(&pp->children[i].err);
1248 if (code)
1249 pp->shutdown = 1;
1250 return code;
1251 }
1252
1253 pp->nr_processes++;
1254 pp->children[i].state = GIT_CP_WORKING;
1255 pp->pfd[i].fd = pp->children[i].process.err;
1256 return 0;
1257}
1258
1259static void pp_buffer_stderr(struct parallel_processes *pp, int output_timeout)
1260{
1261 int i;
1262
1263 while ((i = poll(pp->pfd, pp->max_processes, output_timeout)) < 0) {
1264 if (errno == EINTR)
1265 continue;
1266 pp_cleanup(pp);
1267 die_errno("poll");
1268 }
1269
1270 /* Buffer output from all pipes. */
1271 for (i = 0; i < pp->max_processes; i++) {
1272 if (pp->children[i].state == GIT_CP_WORKING &&
1273 pp->pfd[i].revents & (POLLIN | POLLHUP)) {
1274 int n = strbuf_read_once(&pp->children[i].err,
1275 pp->children[i].process.err, 0);
1276 if (n == 0) {
1277 close(pp->children[i].process.err);
1278 pp->children[i].state = GIT_CP_WAIT_CLEANUP;
1279 } else if (n < 0)
1280 if (errno != EAGAIN)
1281 die_errno("read");
1282 }
1283 }
1284}
1285
1286static void pp_output(struct parallel_processes *pp)
1287{
1288 int i = pp->output_owner;
1289 if (pp->children[i].state == GIT_CP_WORKING &&
1290 pp->children[i].err.len) {
2dac9b56 1291 strbuf_write(&pp->children[i].err, stderr);
c553c72e
SB
1292 strbuf_reset(&pp->children[i].err);
1293 }
1294}
1295
1296static int pp_collect_finished(struct parallel_processes *pp)
1297{
1298 int i, code;
1299 int n = pp->max_processes;
1300 int result = 0;
1301
1302 while (pp->nr_processes > 0) {
1303 for (i = 0; i < pp->max_processes; i++)
1304 if (pp->children[i].state == GIT_CP_WAIT_CLEANUP)
1305 break;
1306 if (i == pp->max_processes)
1307 break;
1308
1309 code = finish_command(&pp->children[i].process);
1310
2a73b3da 1311 code = pp->task_finished(code,
c553c72e
SB
1312 &pp->children[i].err, pp->data,
1313 &pp->children[i].data);
1314
1315 if (code)
1316 result = code;
1317 if (code < 0)
1318 break;
1319
1320 pp->nr_processes--;
1321 pp->children[i].state = GIT_CP_FREE;
1322 pp->pfd[i].fd = -1;
1323 child_process_init(&pp->children[i].process);
1324
1325 if (i != pp->output_owner) {
1326 strbuf_addbuf(&pp->buffered_output, &pp->children[i].err);
1327 strbuf_reset(&pp->children[i].err);
1328 } else {
2dac9b56 1329 strbuf_write(&pp->children[i].err, stderr);
c553c72e
SB
1330 strbuf_reset(&pp->children[i].err);
1331
1332 /* Output all other finished child processes */
2dac9b56 1333 strbuf_write(&pp->buffered_output, stderr);
c553c72e
SB
1334 strbuf_reset(&pp->buffered_output);
1335
1336 /*
1337 * Pick next process to output live.
1338 * NEEDSWORK:
1339 * For now we pick it randomly by doing a round
1340 * robin. Later we may want to pick the one with
1341 * the most output or the longest or shortest
1342 * running process time.
1343 */
1344 for (i = 0; i < n; i++)
1345 if (pp->children[(pp->output_owner + i) % n].state == GIT_CP_WORKING)
1346 break;
1347 pp->output_owner = (pp->output_owner + i) % n;
1348 }
1349 }
1350 return result;
1351}
1352
1353int run_processes_parallel(int n,
1354 get_next_task_fn get_next_task,
1355 start_failure_fn start_failure,
1356 task_finished_fn task_finished,
1357 void *pp_cb)
1358{
1359 int i, code;
1360 int output_timeout = 100;
1361 int spawn_cap = 4;
1362 struct parallel_processes pp;
1363
1364 pp_init(&pp, n, get_next_task, start_failure, task_finished, pp_cb);
1365 while (1) {
1366 for (i = 0;
1367 i < spawn_cap && !pp.shutdown &&
1368 pp.nr_processes < pp.max_processes;
1369 i++) {
1370 code = pp_start_one(&pp);
1371 if (!code)
1372 continue;
1373 if (code < 0) {
1374 pp.shutdown = 1;
1375 kill_children(&pp, -code);
1376 }
1377 break;
1378 }
1379 if (!pp.nr_processes)
1380 break;
1381 pp_buffer_stderr(&pp, output_timeout);
1382 pp_output(&pp);
1383 code = pp_collect_finished(&pp);
1384 if (code) {
1385 pp.shutdown = 1;
1386 if (code < 0)
1387 kill_children(&pp, -code);
1388 }
1389 }
1390
1391 pp_cleanup(&pp);
1392 return 0;
1393}