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