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