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