]> git.ipfire.org Git - thirdparty/git.git/blame - run-command.c
run-command: call run_command_v_opt_cd_env() instead of duplicating it
[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"
b1bf95bb 6
b3e34ddd
BW
7#ifndef SHELL_PATH
8# define SHELL_PATH "/bin/sh"
9#endif
10
483bbd4e
RS
11void child_process_init(struct child_process *child)
12{
13 memset(child, 0, sizeof(*child));
14 argv_array_init(&child->args);
15}
16
afe19ff7
JK
17struct child_to_clean {
18 pid_t pid;
19 struct child_to_clean *next;
20};
21static struct child_to_clean *children_to_clean;
22static int installed_child_cleanup_handler;
23
24static void cleanup_children(int sig)
25{
26 while (children_to_clean) {
27 struct child_to_clean *p = children_to_clean;
28 children_to_clean = p->next;
29 kill(p->pid, sig);
30 free(p);
31 }
32}
33
34static void cleanup_children_on_signal(int sig)
35{
36 cleanup_children(sig);
37 sigchain_pop(sig);
38 raise(sig);
39}
40
41static void cleanup_children_on_exit(void)
42{
43 cleanup_children(SIGTERM);
44}
45
46static void mark_child_for_cleanup(pid_t pid)
47{
48 struct child_to_clean *p = xmalloc(sizeof(*p));
49 p->pid = pid;
50 p->next = children_to_clean;
51 children_to_clean = p;
52
53 if (!installed_child_cleanup_handler) {
54 atexit(cleanup_children_on_exit);
55 sigchain_push_common(cleanup_children_on_signal);
56 installed_child_cleanup_handler = 1;
57 }
58}
59
60static void clear_child_for_cleanup(pid_t pid)
61{
bdee397d 62 struct child_to_clean **pp;
afe19ff7 63
bdee397d
DG
64 for (pp = &children_to_clean; *pp; pp = &(*pp)->next) {
65 struct child_to_clean *clean_me = *pp;
66
67 if (clean_me->pid == pid) {
68 *pp = clean_me->next;
69 free(clean_me);
afe19ff7
JK
70 return;
71 }
72 }
73}
74
9dc09c76
SP
75static inline void close_pair(int fd[2])
76{
77 close(fd[0]);
78 close(fd[1]);
79}
80
380395d0 81#ifndef GIT_WINDOWS_NATIVE
e4507ae8
SP
82static inline void dup_devnull(int to)
83{
84 int fd = open("/dev/null", O_RDWR);
a77f106c
TR
85 if (fd < 0)
86 die_errno(_("open /dev/null failed"));
87 if (dup2(fd, to) < 0)
88 die_errno(_("dup2(%d,%d) failed"), fd, to);
e4507ae8
SP
89 close(fd);
90}
75301f90 91#endif
e4507ae8 92
38f865c2
JK
93static char *locate_in_PATH(const char *file)
94{
95 const char *p = getenv("PATH");
96 struct strbuf buf = STRBUF_INIT;
97
98 if (!p || !*p)
99 return NULL;
100
101 while (1) {
102 const char *end = strchrnul(p, ':');
103
104 strbuf_reset(&buf);
105
106 /* POSIX specifies an empty entry as the current directory. */
107 if (end != p) {
108 strbuf_add(&buf, p, end - p);
109 strbuf_addch(&buf, '/');
110 }
111 strbuf_addstr(&buf, file);
112
113 if (!access(buf.buf, F_OK))
114 return strbuf_detach(&buf, NULL);
115
116 if (!*end)
117 break;
118 p = end + 1;
119 }
120
121 strbuf_release(&buf);
122 return NULL;
123}
124
125static int exists_in_PATH(const char *file)
126{
127 char *r = locate_in_PATH(file);
128 free(r);
129 return r != NULL;
130}
131
132int sane_execvp(const char *file, char * const argv[])
133{
134 if (!execvp(file, argv))
135 return 0; /* cannot happen ;-) */
136
137 /*
138 * When a command can't be found because one of the directories
139 * listed in $PATH is unsearchable, execvp reports EACCES, but
140 * careful usability testing (read: analysis of occasional bug
141 * reports) reveals that "No such file or directory" is more
142 * intuitive.
143 *
144 * We avoid commands with "/", because execvp will not do $PATH
145 * lookups in that case.
146 *
147 * The reassignment of EACCES to errno looks like a no-op below,
148 * but we need to protect against exists_in_PATH overwriting errno.
149 */
150 if (errno == EACCES && !strchr(file, '/'))
151 errno = exists_in_PATH(file) ? EACCES : ENOENT;
a7855083
JH
152 else if (errno == ENOTDIR && !strchr(file, '/'))
153 errno = ENOENT;
38f865c2
JK
154 return -1;
155}
156
8dba1e63
JK
157static const char **prepare_shell_cmd(const char **argv)
158{
159 int argc, nargc = 0;
160 const char **nargv;
161
162 for (argc = 0; argv[argc]; argc++)
163 ; /* just counting */
164 /* +1 for NULL, +3 for "sh -c" plus extra $0 */
165 nargv = xmalloc(sizeof(*nargv) * (argc + 1 + 3));
166
167 if (argc < 1)
168 die("BUG: shell command is empty");
169
f445644f 170 if (strcspn(argv[0], "|&;<>()$`\\\"' \t\n*?[#~=%") != strlen(argv[0])) {
380395d0 171#ifndef GIT_WINDOWS_NATIVE
b3e34ddd 172 nargv[nargc++] = SHELL_PATH;
77629754
JS
173#else
174 nargv[nargc++] = "sh";
175#endif
f445644f 176 nargv[nargc++] = "-c";
8dba1e63 177
f445644f
JK
178 if (argc < 2)
179 nargv[nargc++] = argv[0];
180 else {
181 struct strbuf arg0 = STRBUF_INIT;
182 strbuf_addf(&arg0, "%s \"$@\"", argv[0]);
183 nargv[nargc++] = strbuf_detach(&arg0, NULL);
184 }
8dba1e63
JK
185 }
186
187 for (argc = 0; argv[argc]; argc++)
188 nargv[nargc++] = argv[argc];
189 nargv[nargc] = NULL;
190
191 return nargv;
192}
193
380395d0 194#ifndef GIT_WINDOWS_NATIVE
8dba1e63
JK
195static int execv_shell_cmd(const char **argv)
196{
197 const char **nargv = prepare_shell_cmd(argv);
198 trace_argv_printf(nargv, "trace: exec:");
38f865c2 199 sane_execvp(nargv[0], (char **)nargv);
8dba1e63
JK
200 free(nargv);
201 return -1;
202}
203#endif
204
380395d0 205#ifndef GIT_WINDOWS_NATIVE
a5487ddf 206static int child_err = 2;
2b541bf8
JS
207static int child_notifier = -1;
208
209static void notify_parent(void)
210{
a111eb78
JN
211 /*
212 * execvp failed. If possible, we'd like to let start_command
213 * know, so failures like ENOENT can be handled right away; but
214 * otherwise, finish_command will still report the error.
215 */
216 xwrite(child_notifier, "", 1);
2b541bf8 217}
a5487ddf
JS
218
219static NORETURN void die_child(const char *err, va_list params)
220{
3bc4181f 221 vwritef(child_err, "fatal: ", err, params);
a5487ddf
JS
222 exit(128);
223}
3bc4181f
CB
224
225static void error_child(const char *err, va_list params)
226{
227 vwritef(child_err, "error: ", err, params);
228}
200a76b7 229#endif
a5487ddf
JS
230
231static inline void set_cloexec(int fd)
232{
233 int flags = fcntl(fd, F_GETFD);
234 if (flags >= 0)
235 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
236}
a5487ddf 237
13274526 238static int wait_or_whine(pid_t pid, const char *argv0)
ab0b41da
JS
239{
240 int status, code = -1;
241 pid_t waiting;
242 int failed_errno = 0;
243
244 while ((waiting = waitpid(pid, &status, 0)) < 0 && errno == EINTR)
245 ; /* nothing */
246
247 if (waiting < 0) {
248 failed_errno = errno;
249 error("waitpid for %s failed: %s", argv0, strerror(errno));
250 } else if (waiting != pid) {
251 error("waitpid is confused (%s)", argv0);
252 } else if (WIFSIGNALED(status)) {
253 code = WTERMSIG(status);
a2767c5c
JK
254 if (code != SIGINT && code != SIGQUIT)
255 error("%s died of signal %d", argv0, code);
ab0b41da
JS
256 /*
257 * This return value is chosen so that code & 0xff
258 * mimics the exit code that a POSIX shell would report for
259 * a program that died from this signal.
260 */
709ca730 261 code += 128;
ab0b41da
JS
262 } else if (WIFEXITED(status)) {
263 code = WEXITSTATUS(status);
264 /*
265 * Convert special exit code when execvp failed.
266 */
267 if (code == 127) {
268 code = -1;
269 failed_errno = ENOENT;
ab0b41da
JS
270 }
271 } else {
272 error("waitpid is confused (%s)", argv0);
273 }
afe19ff7
JK
274
275 clear_child_for_cleanup(pid);
276
ab0b41da
JS
277 errno = failed_errno;
278 return code;
279}
280
ebcb5d16 281int start_command(struct child_process *cmd)
b1bf95bb 282{
f3b33f1d
JS
283 int need_in, need_out, need_err;
284 int fdin[2], fdout[2], fderr[2];
25043d8a 285 int failed_errno;
939296c4 286 char *str;
4919bf03 287
c460c0ec
JK
288 if (!cmd->argv)
289 cmd->argv = cmd->args.argv;
290
c20181e3
JS
291 /*
292 * In case of errors we must keep the promise to close FDs
293 * that have been passed in via ->in and ->out.
294 */
295
e4507ae8 296 need_in = !cmd->no_stdin && cmd->in < 0;
4919bf03 297 if (need_in) {
c20181e3 298 if (pipe(fdin) < 0) {
0ac77ec3 299 failed_errno = errno;
c20181e3
JS
300 if (cmd->out > 0)
301 close(cmd->out);
939296c4 302 str = "standard input";
0ac77ec3 303 goto fail_pipe;
c20181e3 304 }
4919bf03 305 cmd->in = fdin[1];
4919bf03
SP
306 }
307
e4507ae8
SP
308 need_out = !cmd->no_stdout
309 && !cmd->stdout_to_stderr
310 && cmd->out < 0;
f4bba25b
SP
311 if (need_out) {
312 if (pipe(fdout) < 0) {
0ac77ec3 313 failed_errno = errno;
f4bba25b
SP
314 if (need_in)
315 close_pair(fdin);
c20181e3
JS
316 else if (cmd->in)
317 close(cmd->in);
939296c4 318 str = "standard output";
0ac77ec3 319 goto fail_pipe;
f4bba25b
SP
320 }
321 cmd->out = fdout[0];
f4bba25b
SP
322 }
323
b73a4397 324 need_err = !cmd->no_stderr && cmd->err < 0;
f3b33f1d
JS
325 if (need_err) {
326 if (pipe(fderr) < 0) {
0ac77ec3 327 failed_errno = errno;
f3b33f1d
JS
328 if (need_in)
329 close_pair(fdin);
c20181e3
JS
330 else if (cmd->in)
331 close(cmd->in);
f3b33f1d
JS
332 if (need_out)
333 close_pair(fdout);
c20181e3
JS
334 else if (cmd->out)
335 close(cmd->out);
939296c4 336 str = "standard error";
0ac77ec3 337fail_pipe:
939296c4
SB
338 error("cannot create %s pipe for %s: %s",
339 str, cmd->argv[0], strerror(failed_errno));
c460c0ec 340 argv_array_clear(&cmd->args);
0ac77ec3
JS
341 errno = failed_errno;
342 return -1;
f3b33f1d
JS
343 }
344 cmd->err = fderr[0];
345 }
346
8852f5d7 347 trace_argv_printf(cmd->argv, "trace: run_command:");
13af8cbd 348 fflush(NULL);
8852f5d7 349
380395d0 350#ifndef GIT_WINDOWS_NATIVE
2b541bf8
JS
351{
352 int notify_pipe[2];
353 if (pipe(notify_pipe))
354 notify_pipe[0] = notify_pipe[1] = -1;
355
ebcb5d16 356 cmd->pid = fork();
25043d8a 357 failed_errno = errno;
ebcb5d16 358 if (!cmd->pid) {
a5487ddf
JS
359 /*
360 * Redirect the channel to write syscall error messages to
361 * before redirecting the process's stderr so that all die()
362 * in subsequent call paths use the parent's stderr.
363 */
364 if (cmd->no_stderr || need_err) {
365 child_err = dup(2);
366 set_cloexec(child_err);
367 }
368 set_die_routine(die_child);
3bc4181f 369 set_error_routine(error_child);
a5487ddf 370
2b541bf8
JS
371 close(notify_pipe[0]);
372 set_cloexec(notify_pipe[1]);
373 child_notifier = notify_pipe[1];
374 atexit(notify_parent);
375
e4507ae8
SP
376 if (cmd->no_stdin)
377 dup_devnull(0);
378 else if (need_in) {
4919bf03 379 dup2(fdin[0], 0);
9dc09c76 380 close_pair(fdin);
4919bf03
SP
381 } else if (cmd->in) {
382 dup2(cmd->in, 0);
383 close(cmd->in);
95d3c4f5 384 }
4919bf03 385
ce2cf27a
CC
386 if (cmd->no_stderr)
387 dup_devnull(2);
388 else if (need_err) {
389 dup2(fderr[1], 2);
390 close_pair(fderr);
4f41b611
SP
391 } else if (cmd->err > 1) {
392 dup2(cmd->err, 2);
393 close(cmd->err);
ce2cf27a
CC
394 }
395
e4507ae8
SP
396 if (cmd->no_stdout)
397 dup_devnull(1);
398 else if (cmd->stdout_to_stderr)
cd83c74c 399 dup2(2, 1);
f4bba25b
SP
400 else if (need_out) {
401 dup2(fdout[1], 1);
402 close_pair(fdout);
403 } else if (cmd->out > 1) {
404 dup2(cmd->out, 1);
405 close(cmd->out);
406 }
407
1568fea0 408 if (cmd->dir && chdir(cmd->dir))
d824cbba
TR
409 die_errno("exec '%s': cd to '%s' failed", cmd->argv[0],
410 cmd->dir);
ee493148 411 if (cmd->env) {
3427b375
AR
412 for (; *cmd->env; cmd->env++) {
413 if (strchr(*cmd->env, '='))
4b25d091 414 putenv((char *)*cmd->env);
3427b375
AR
415 else
416 unsetenv(*cmd->env);
417 }
ee493148 418 }
5a50085c 419 if (cmd->git_cmd)
f1000898 420 execv_git_cmd(cmd->argv);
5a50085c 421 else if (cmd->use_shell)
8dba1e63 422 execv_shell_cmd(cmd->argv);
5a50085c 423 else
38f865c2 424 sane_execvp(cmd->argv[0], (char *const*) cmd->argv);
fc1b56f0
CB
425 if (errno == ENOENT) {
426 if (!cmd->silent_exec_failure)
427 error("cannot run %s: %s", cmd->argv[0],
428 strerror(ENOENT));
a5487ddf 429 exit(127);
fc1b56f0 430 } else {
a5487ddf 431 die_errno("cannot exec '%s'", cmd->argv[0]);
fc1b56f0 432 }
b1bf95bb 433 }
0ac77ec3
JS
434 if (cmd->pid < 0)
435 error("cannot fork() for %s: %s", cmd->argv[0],
25043d8a 436 strerror(errno));
afe19ff7
JK
437 else if (cmd->clean_on_exit)
438 mark_child_for_cleanup(cmd->pid);
2b541bf8
JS
439
440 /*
441 * Wait for child's execvp. If the execvp succeeds (or if fork()
442 * failed), EOF is seen immediately by the parent. Otherwise, the
443 * child process sends a single byte.
444 * Note that use of this infrastructure is completely advisory,
445 * therefore, we keep error checks minimal.
446 */
447 close(notify_pipe[1]);
448 if (read(notify_pipe[0], &notify_pipe[1], 1) == 1) {
449 /*
450 * At this point we know that fork() succeeded, but execvp()
451 * failed. Errors have been reported to our stderr.
452 */
13274526 453 wait_or_whine(cmd->pid, cmd->argv[0]);
2b541bf8
JS
454 failed_errno = errno;
455 cmd->pid = -1;
456 }
457 close(notify_pipe[0]);
458}
ba26f296 459#else
0d30ad71 460{
75301f90 461 int fhin = 0, fhout = 1, fherr = 2;
108ac313 462 const char **sargv = cmd->argv;
ba26f296 463
75301f90
JS
464 if (cmd->no_stdin)
465 fhin = open("/dev/null", O_RDWR);
466 else if (need_in)
467 fhin = dup(fdin[0]);
468 else if (cmd->in)
469 fhin = dup(cmd->in);
470
471 if (cmd->no_stderr)
472 fherr = open("/dev/null", O_RDWR);
473 else if (need_err)
474 fherr = dup(fderr[1]);
76d44c8c
JH
475 else if (cmd->err > 2)
476 fherr = dup(cmd->err);
75301f90
JS
477
478 if (cmd->no_stdout)
479 fhout = open("/dev/null", O_RDWR);
480 else if (cmd->stdout_to_stderr)
481 fhout = dup(fherr);
482 else if (need_out)
483 fhout = dup(fdout[1]);
484 else if (cmd->out > 1)
485 fhout = dup(cmd->out);
ba26f296 486
5a50085c 487 if (cmd->git_cmd)
108ac313 488 cmd->argv = prepare_git_cmd(cmd->argv);
5a50085c 489 else if (cmd->use_shell)
8dba1e63 490 cmd->argv = prepare_shell_cmd(cmd->argv);
ba26f296 491
77734da2
KB
492 cmd->pid = mingw_spawnvpe(cmd->argv[0], cmd->argv, (char**) cmd->env,
493 cmd->dir, fhin, fhout, fherr);
0ac77ec3 494 failed_errno = errno;
c024beb5 495 if (cmd->pid < 0 && (!cmd->silent_exec_failure || errno != ENOENT))
0ac77ec3 496 error("cannot spawn %s: %s", cmd->argv[0], strerror(errno));
afe19ff7
JK
497 if (cmd->clean_on_exit && cmd->pid >= 0)
498 mark_child_for_cleanup(cmd->pid);
ba26f296 499
ba26f296 500 if (cmd->git_cmd)
108ac313 501 free(cmd->argv);
ba26f296 502
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);
c460c0ec 526 argv_array_clear(&cmd->args);
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{
c460c0ec
JK
551 int ret = wait_or_whine(cmd->pid, cmd->argv[0]);
552 argv_array_clear(&cmd->args);
553 return ret;
2d22c208
JS
554}
555
ebcb5d16
SP
556int run_command(struct child_process *cmd)
557{
558 int code = start_command(cmd);
559 if (code)
560 return code;
561 return finish_command(cmd);
562}
563
1568fea0 564static void prepare_run_command_v_opt(struct child_process *cmd,
ee493148
AR
565 const char **argv,
566 int opt)
1568fea0
AR
567{
568 memset(cmd, 0, sizeof(*cmd));
569 cmd->argv = argv;
570 cmd->no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
571 cmd->git_cmd = opt & RUN_GIT_CMD ? 1 : 0;
572 cmd->stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
c024beb5 573 cmd->silent_exec_failure = opt & RUN_SILENT_EXEC_FAILURE ? 1 : 0;
8dba1e63 574 cmd->use_shell = opt & RUN_USING_SHELL ? 1 : 0;
10c6cddd 575 cmd->clean_on_exit = opt & RUN_CLEAN_ON_EXIT ? 1 : 0;
1568fea0
AR
576}
577
f1000898
SP
578int run_command_v_opt(const char **argv, int opt)
579{
41e9bad7 580 return run_command_v_opt_cd_env(argv, opt, NULL, NULL);
1568fea0
AR
581}
582
ee493148
AR
583int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env)
584{
585 struct child_process cmd;
586 prepare_run_command_v_opt(&cmd, argv, opt);
587 cmd.dir = dir;
588 cmd.env = env;
589 return run_command(&cmd);
590}
2d22c208 591
f6b60983 592#ifndef NO_PTHREADS
0ea1c89b
JS
593static pthread_t main_thread;
594static int main_thread_set;
595static pthread_key_t async_key;
1ece66bc 596static pthread_key_t async_die_counter;
0ea1c89b 597
200a76b7 598static void *run_thread(void *data)
618ebe9f
JS
599{
600 struct async *async = data;
f6b60983 601 intptr_t ret;
0ea1c89b
JS
602
603 pthread_setspecific(async_key, async);
f6b60983 604 ret = async->proc(async->proc_in, async->proc_out, async->data);
200a76b7 605 return (void *)ret;
618ebe9f 606}
0ea1c89b
JS
607
608static NORETURN void die_async(const char *err, va_list params)
609{
610 vreportf("fatal: ", err, params);
611
612 if (!pthread_equal(main_thread, pthread_self())) {
613 struct async *async = pthread_getspecific(async_key);
614 if (async->proc_in >= 0)
615 close(async->proc_in);
616 if (async->proc_out >= 0)
617 close(async->proc_out);
618 pthread_exit((void *)128);
619 }
620
621 exit(128);
618ebe9f 622}
1ece66bc
JK
623
624static int async_die_is_recursing(void)
625{
626 void *ret = pthread_getspecific(async_die_counter);
627 pthread_setspecific(async_die_counter, (void *)1);
628 return ret != NULL;
629}
630
618ebe9f
JS
631#endif
632
2d22c208
JS
633int start_async(struct async *async)
634{
ae6a5609
EFL
635 int need_in, need_out;
636 int fdin[2], fdout[2];
637 int proc_in, proc_out;
2d22c208 638
ae6a5609
EFL
639 need_in = async->in < 0;
640 if (need_in) {
641 if (pipe(fdin) < 0) {
642 if (async->out > 0)
643 close(async->out);
644 return error("cannot create pipe: %s", strerror(errno));
645 }
646 async->in = fdin[1];
647 }
648
649 need_out = async->out < 0;
650 if (need_out) {
651 if (pipe(fdout) < 0) {
652 if (need_in)
653 close_pair(fdin);
654 else if (async->in)
655 close(async->in);
656 return error("cannot create pipe: %s", strerror(errno));
657 }
658 async->out = fdout[0];
659 }
660
661 if (need_in)
662 proc_in = fdin[0];
663 else if (async->in)
664 proc_in = async->in;
665 else
666 proc_in = -1;
667
668 if (need_out)
669 proc_out = fdout[1];
670 else if (async->out)
671 proc_out = async->out;
672 else
673 proc_out = -1;
2d22c208 674
f6b60983 675#ifdef NO_PTHREADS
2c3766f0
AM
676 /* Flush stdio before fork() to avoid cloning buffers */
677 fflush(NULL);
678
2d22c208
JS
679 async->pid = fork();
680 if (async->pid < 0) {
681 error("fork (async) failed: %s", strerror(errno));
ae6a5609 682 goto error;
2d22c208
JS
683 }
684 if (!async->pid) {
ae6a5609
EFL
685 if (need_in)
686 close(fdin[1]);
687 if (need_out)
688 close(fdout[0]);
689 exit(!!async->proc(proc_in, proc_out, async->data));
2d22c208 690 }
ae6a5609 691
afe19ff7
JK
692 mark_child_for_cleanup(async->pid);
693
ae6a5609
EFL
694 if (need_in)
695 close(fdin[0]);
696 else if (async->in)
697 close(async->in);
698
699 if (need_out)
700 close(fdout[1]);
701 else if (async->out)
702 close(async->out);
618ebe9f 703#else
0ea1c89b
JS
704 if (!main_thread_set) {
705 /*
706 * We assume that the first time that start_async is called
707 * it is from the main thread.
708 */
709 main_thread_set = 1;
710 main_thread = pthread_self();
711 pthread_key_create(&async_key, NULL);
1ece66bc 712 pthread_key_create(&async_die_counter, NULL);
0ea1c89b 713 set_die_routine(die_async);
1ece66bc 714 set_die_is_recursing_routine(async_die_is_recursing);
0ea1c89b
JS
715 }
716
200a76b7
JS
717 if (proc_in >= 0)
718 set_cloexec(proc_in);
719 if (proc_out >= 0)
720 set_cloexec(proc_out);
ae6a5609
EFL
721 async->proc_in = proc_in;
722 async->proc_out = proc_out;
200a76b7
JS
723 {
724 int err = pthread_create(&async->tid, NULL, run_thread, async);
725 if (err) {
726 error("cannot create thread: %s", strerror(err));
727 goto error;
728 }
618ebe9f
JS
729 }
730#endif
2d22c208 731 return 0;
ae6a5609
EFL
732
733error:
734 if (need_in)
735 close_pair(fdin);
736 else if (async->in)
737 close(async->in);
738
739 if (need_out)
740 close_pair(fdout);
741 else if (async->out)
742 close(async->out);
743 return -1;
2d22c208
JS
744}
745
746int finish_async(struct async *async)
747{
f6b60983 748#ifdef NO_PTHREADS
0398fc34 749 return wait_or_whine(async->pid, "child process");
618ebe9f 750#else
200a76b7
JS
751 void *ret = (void *)(intptr_t)(-1);
752
753 if (pthread_join(async->tid, &ret))
754 error("pthread_join failed");
755 return (int)(intptr_t)ret;
618ebe9f 756#endif
2d22c208 757}
ae98a008 758
5a7da2dc
AS
759char *find_hook(const char *name)
760{
761 char *path = git_path("hooks/%s", name);
762 if (access(path, X_OK) < 0)
763 path = NULL;
764
765 return path;
766}
767
15048f8a 768int run_hook_ve(const char *const *env, const char *name, va_list args)
ae98a008 769{
d3180279 770 struct child_process hook = CHILD_PROCESS_INIT;
15048f8a 771 const char *p;
ae98a008 772
5a7da2dc
AS
773 p = find_hook(name);
774 if (!p)
cf94ca8e
SB
775 return 0;
776
d1d09456
RS
777 argv_array_push(&hook.args, p);
778 while ((p = va_arg(args, const char *)))
779 argv_array_push(&hook.args, p);
15048f8a 780 hook.env = env;
ae98a008
SB
781 hook.no_stdin = 1;
782 hook.stdout_to_stderr = 1;
ae98a008 783
d1d09456 784 return run_command(&hook);
ae98a008 785}
15048f8a
BP
786
787int run_hook_le(const char *const *env, const char *name, ...)
788{
789 va_list args;
790 int ret;
791
792 va_start(args, name);
793 ret = run_hook_ve(env, name, args);
794 va_end(args);
795
796 return ret;
797}
798
799int run_hook_with_custom_index(const char *index_file, const char *name, ...)
800{
801 const char *hook_env[3] = { NULL };
802 char index[PATH_MAX];
803 va_list args;
804 int ret;
805
806 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
807 hook_env[0] = index;
808
809 va_start(args, name);
810 ret = run_hook_ve(hook_env, name, args);
811 va_end(args);
812
813 return ret;
814}