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