2 #include "run-command.h"
5 #include "argv-array.h"
7 void child_process_init(struct child_process
*child
)
9 memset(child
, 0, sizeof(*child
));
10 argv_array_init(&child
->args
);
11 argv_array_init(&child
->env_array
);
14 void child_process_clear(struct child_process
*child
)
16 argv_array_clear(&child
->args
);
17 argv_array_clear(&child
->env_array
);
20 struct child_to_clean
{
22 struct child_to_clean
*next
;
24 static struct child_to_clean
*children_to_clean
;
25 static int installed_child_cleanup_handler
;
27 static void cleanup_children(int sig
, int in_signal
)
29 while (children_to_clean
) {
30 struct child_to_clean
*p
= children_to_clean
;
31 children_to_clean
= p
->next
;
38 static void cleanup_children_on_signal(int sig
)
40 cleanup_children(sig
, 1);
45 static void cleanup_children_on_exit(void)
47 cleanup_children(SIGTERM
, 0);
50 static void mark_child_for_cleanup(pid_t pid
)
52 struct child_to_clean
*p
= xmalloc(sizeof(*p
));
54 p
->next
= children_to_clean
;
55 children_to_clean
= p
;
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;
64 static void clear_child_for_cleanup(pid_t pid
)
66 struct child_to_clean
**pp
;
68 for (pp
= &children_to_clean
; *pp
; pp
= &(*pp
)->next
) {
69 struct child_to_clean
*clean_me
= *pp
;
71 if (clean_me
->pid
== pid
) {
79 static inline void close_pair(int fd
[2])
85 #ifndef GIT_WINDOWS_NATIVE
86 static inline void dup_devnull(int to
)
88 int fd
= open("/dev/null", O_RDWR
);
90 die_errno(_("open /dev/null failed"));
92 die_errno(_("dup2(%d,%d) failed"), fd
, to
);
97 static char *locate_in_PATH(const char *file
)
99 const char *p
= getenv("PATH");
100 struct strbuf buf
= STRBUF_INIT
;
106 const char *end
= strchrnul(p
, ':');
110 /* POSIX specifies an empty entry as the current directory. */
112 strbuf_add(&buf
, p
, end
- p
);
113 strbuf_addch(&buf
, '/');
115 strbuf_addstr(&buf
, file
);
117 if (!access(buf
.buf
, F_OK
))
118 return strbuf_detach(&buf
, NULL
);
125 strbuf_release(&buf
);
129 static int exists_in_PATH(const char *file
)
131 char *r
= locate_in_PATH(file
);
136 int sane_execvp(const char *file
, char * const argv
[])
138 if (!execvp(file
, argv
))
139 return 0; /* cannot happen ;-) */
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
148 * We avoid commands with "/", because execvp will not do $PATH
149 * lookups in that case.
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.
154 if (errno
== EACCES
&& !strchr(file
, '/'))
155 errno
= exists_in_PATH(file
) ? EACCES
: ENOENT
;
156 else if (errno
== ENOTDIR
&& !strchr(file
, '/'))
161 static const char **prepare_shell_cmd(struct argv_array
*out
, const char **argv
)
164 die("BUG: shell command is empty");
166 if (strcspn(argv
[0], "|&;<>()$`\\\"' \t\n*?[#~=%") != strlen(argv
[0])) {
167 #ifndef GIT_WINDOWS_NATIVE
168 argv_array_push(out
, SHELL_PATH
);
170 argv_array_push(out
, "sh");
172 argv_array_push(out
, "-c");
175 * If we have no extra arguments, we do not even need to
176 * bother with the "$@" magic.
179 argv_array_push(out
, argv
[0]);
181 argv_array_pushf(out
, "%s \"$@\"", argv
[0]);
184 argv_array_pushv(out
, argv
);
188 #ifndef GIT_WINDOWS_NATIVE
189 static int execv_shell_cmd(const char **argv
)
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
);
200 #ifndef GIT_WINDOWS_NATIVE
201 static int child_notifier
= -1;
203 static void notify_parent(void)
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.
210 xwrite(child_notifier
, "", 1);
214 static inline void set_cloexec(int fd
)
216 int flags
= fcntl(fd
, F_GETFD
);
218 fcntl(fd
, F_SETFD
, flags
| FD_CLOEXEC
);
221 static int wait_or_whine(pid_t pid
, const char *argv0
, int in_signal
)
223 int status
, code
= -1;
225 int failed_errno
= 0;
227 while ((waiting
= waitpid(pid
, &status
, 0)) < 0 && errno
== EINTR
)
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
);
239 if (code
!= SIGINT
&& code
!= SIGQUIT
)
240 error("%s died of signal %d", argv0
, code
);
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.
247 } else if (WIFEXITED(status
)) {
248 code
= WEXITSTATUS(status
);
250 * Convert special exit code when execvp failed.
254 failed_errno
= ENOENT
;
257 error("waitpid is confused (%s)", argv0
);
260 clear_child_for_cleanup(pid
);
262 errno
= failed_errno
;
266 int start_command(struct child_process
*cmd
)
268 int need_in
, need_out
, need_err
;
269 int fdin
[2], fdout
[2], fderr
[2];
274 cmd
->argv
= cmd
->args
.argv
;
276 cmd
->env
= cmd
->env_array
.argv
;
279 * In case of errors we must keep the promise to close FDs
280 * that have been passed in via ->in and ->out.
283 need_in
= !cmd
->no_stdin
&& cmd
->in
< 0;
285 if (pipe(fdin
) < 0) {
286 failed_errno
= errno
;
289 str
= "standard input";
295 need_out
= !cmd
->no_stdout
296 && !cmd
->stdout_to_stderr
299 if (pipe(fdout
) < 0) {
300 failed_errno
= errno
;
305 str
= "standard output";
311 need_err
= !cmd
->no_stderr
&& cmd
->err
< 0;
313 if (pipe(fderr
) < 0) {
314 failed_errno
= errno
;
323 str
= "standard error";
325 error("cannot create %s pipe for %s: %s",
326 str
, cmd
->argv
[0], strerror(failed_errno
));
327 child_process_clear(cmd
);
328 errno
= failed_errno
;
334 trace_argv_printf(cmd
->argv
, "trace: run_command:");
337 #ifndef GIT_WINDOWS_NATIVE
340 if (pipe(notify_pipe
))
341 notify_pipe
[0] = notify_pipe
[1] = -1;
344 failed_errno
= errno
;
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.
351 if (cmd
->no_stderr
|| need_err
) {
352 int child_err
= dup(2);
353 set_cloexec(child_err
);
354 set_error_handle(fdopen(child_err
, "w"));
357 close(notify_pipe
[0]);
358 set_cloexec(notify_pipe
[1]);
359 child_notifier
= notify_pipe
[1];
360 atexit(notify_parent
);
367 } else if (cmd
->in
) {
377 } else if (cmd
->err
> 1) {
384 else if (cmd
->stdout_to_stderr
)
389 } else if (cmd
->out
> 1) {
394 if (cmd
->dir
&& chdir(cmd
->dir
))
395 die_errno("exec '%s': cd to '%s' failed", cmd
->argv
[0],
398 for (; *cmd
->env
; cmd
->env
++) {
399 if (strchr(*cmd
->env
, '='))
400 putenv((char *)*cmd
->env
);
406 execv_git_cmd(cmd
->argv
);
407 else if (cmd
->use_shell
)
408 execv_shell_cmd(cmd
->argv
);
410 sane_execvp(cmd
->argv
[0], (char *const*) cmd
->argv
);
411 if (errno
== ENOENT
) {
412 if (!cmd
->silent_exec_failure
)
413 error("cannot run %s: %s", cmd
->argv
[0],
417 die_errno("cannot exec '%s'", cmd
->argv
[0]);
421 error("cannot fork() for %s: %s", cmd
->argv
[0],
423 else if (cmd
->clean_on_exit
)
424 mark_child_for_cleanup(cmd
->pid
);
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.
433 close(notify_pipe
[1]);
434 if (read(notify_pipe
[0], ¬ify_pipe
[1], 1) == 1) {
436 * At this point we know that fork() succeeded, but execvp()
437 * failed. Errors have been reported to our stderr.
439 wait_or_whine(cmd
->pid
, cmd
->argv
[0], 0);
440 failed_errno
= errno
;
443 close(notify_pipe
[0]);
447 int fhin
= 0, fhout
= 1, fherr
= 2;
448 const char **sargv
= cmd
->argv
;
449 struct argv_array nargv
= ARGV_ARRAY_INIT
;
452 fhin
= open("/dev/null", O_RDWR
);
459 fherr
= open("/dev/null", O_RDWR
);
461 fherr
= dup(fderr
[1]);
462 else if (cmd
->err
> 2)
463 fherr
= dup(cmd
->err
);
466 fhout
= open("/dev/null", O_RDWR
);
467 else if (cmd
->stdout_to_stderr
)
470 fhout
= dup(fdout
[1]);
471 else if (cmd
->out
> 1)
472 fhout
= dup(cmd
->out
);
475 cmd
->argv
= prepare_git_cmd(&nargv
, cmd
->argv
);
476 else if (cmd
->use_shell
)
477 cmd
->argv
= prepare_shell_cmd(&nargv
, cmd
->argv
);
479 cmd
->pid
= mingw_spawnvpe(cmd
->argv
[0], cmd
->argv
, (char**) cmd
->env
,
480 cmd
->dir
, fhin
, fhout
, fherr
);
481 failed_errno
= errno
;
482 if (cmd
->pid
< 0 && (!cmd
->silent_exec_failure
|| errno
!= ENOENT
))
483 error("cannot spawn %s: %s", cmd
->argv
[0], strerror(errno
));
484 if (cmd
->clean_on_exit
&& cmd
->pid
>= 0)
485 mark_child_for_cleanup(cmd
->pid
);
487 argv_array_clear(&nargv
);
511 child_process_clear(cmd
);
512 errno
= failed_errno
;
534 int finish_command(struct child_process
*cmd
)
536 int ret
= wait_or_whine(cmd
->pid
, cmd
->argv
[0], 0);
537 child_process_clear(cmd
);
541 int finish_command_in_signal(struct child_process
*cmd
)
543 return wait_or_whine(cmd
->pid
, cmd
->argv
[0], 1);
547 int run_command(struct child_process
*cmd
)
551 if (cmd
->out
< 0 || cmd
->err
< 0)
552 die("BUG: run_command with a pipe can cause deadlock");
554 code
= start_command(cmd
);
557 return finish_command(cmd
);
560 int run_command_v_opt(const char **argv
, int opt
)
562 return run_command_v_opt_cd_env(argv
, opt
, NULL
, NULL
);
565 int run_command_v_opt_cd_env(const char **argv
, int opt
, const char *dir
, const char *const *env
)
567 struct child_process cmd
= CHILD_PROCESS_INIT
;
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;
577 return run_command(&cmd
);
581 static pthread_t main_thread
;
582 static int main_thread_set
;
583 static pthread_key_t async_key
;
584 static pthread_key_t async_die_counter
;
586 static void *run_thread(void *data
)
588 struct async
*async
= data
;
591 pthread_setspecific(async_key
, async
);
592 ret
= async
->proc(async
->proc_in
, async
->proc_out
, async
->data
);
596 static NORETURN
void die_async(const char *err
, va_list params
)
598 vreportf("fatal: ", err
, params
);
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);
612 static int async_die_is_recursing(void)
614 void *ret
= pthread_getspecific(async_die_counter
);
615 pthread_setspecific(async_die_counter
, (void *)1);
621 if (!main_thread_set
)
622 return 0; /* no asyncs started yet */
623 return !pthread_equal(main_thread
, pthread_self());
626 void NORETURN
async_exit(int code
)
628 pthread_exit((void *)(intptr_t)code
);
634 void (**handlers
)(void);
639 static int git_atexit_installed
;
641 static void git_atexit_dispatch(void)
645 for (i
=git_atexit_hdlrs
.nr
; i
; i
--)
646 git_atexit_hdlrs
.handlers
[i
-1]();
649 static void git_atexit_clear(void)
651 free(git_atexit_hdlrs
.handlers
);
652 memset(&git_atexit_hdlrs
, 0, sizeof(git_atexit_hdlrs
));
653 git_atexit_installed
= 0;
657 int git_atexit(void (*handler
)(void))
659 ALLOC_GROW(git_atexit_hdlrs
.handlers
, git_atexit_hdlrs
.nr
+ 1, git_atexit_hdlrs
.alloc
);
660 git_atexit_hdlrs
.handlers
[git_atexit_hdlrs
.nr
++] = handler
;
661 if (!git_atexit_installed
) {
662 if (atexit(&git_atexit_dispatch
))
664 git_atexit_installed
= 1;
668 #define atexit git_atexit
670 static int process_is_async
;
673 return process_is_async
;
676 void NORETURN
async_exit(int code
)
683 int start_async(struct async
*async
)
685 int need_in
, need_out
;
686 int fdin
[2], fdout
[2];
687 int proc_in
, proc_out
;
689 need_in
= async
->in
< 0;
691 if (pipe(fdin
) < 0) {
694 return error("cannot create pipe: %s", strerror(errno
));
699 need_out
= async
->out
< 0;
701 if (pipe(fdout
) < 0) {
706 return error("cannot create pipe: %s", strerror(errno
));
708 async
->out
= fdout
[0];
721 proc_out
= async
->out
;
726 /* Flush stdio before fork() to avoid cloning buffers */
730 if (async
->pid
< 0) {
731 error("fork (async) failed: %s", strerror(errno
));
740 process_is_async
= 1;
741 exit(!!async
->proc(proc_in
, proc_out
, async
->data
));
744 mark_child_for_cleanup(async
->pid
);
756 if (!main_thread_set
) {
758 * We assume that the first time that start_async is called
759 * it is from the main thread.
762 main_thread
= pthread_self();
763 pthread_key_create(&async_key
, NULL
);
764 pthread_key_create(&async_die_counter
, NULL
);
765 set_die_routine(die_async
);
766 set_die_is_recursing_routine(async_die_is_recursing
);
770 set_cloexec(proc_in
);
772 set_cloexec(proc_out
);
773 async
->proc_in
= proc_in
;
774 async
->proc_out
= proc_out
;
776 int err
= pthread_create(&async
->tid
, NULL
, run_thread
, async
);
778 error("cannot create thread: %s", strerror(err
));
798 int finish_async(struct async
*async
)
801 return wait_or_whine(async
->pid
, "child process", 0);
803 void *ret
= (void *)(intptr_t)(-1);
805 if (pthread_join(async
->tid
, &ret
))
806 error("pthread_join failed");
807 return (int)(intptr_t)ret
;
811 const char *find_hook(const char *name
)
813 static struct strbuf path
= STRBUF_INIT
;
816 strbuf_git_path(&path
, "hooks/%s", name
);
817 if (access(path
.buf
, X_OK
) < 0)
822 int run_hook_ve(const char *const *env
, const char *name
, va_list args
)
824 struct child_process hook
= CHILD_PROCESS_INIT
;
831 argv_array_push(&hook
.args
, p
);
832 while ((p
= va_arg(args
, const char *)))
833 argv_array_push(&hook
.args
, p
);
836 hook
.stdout_to_stderr
= 1;
838 return run_command(&hook
);
841 int run_hook_le(const char *const *env
, const char *name
, ...)
846 va_start(args
, name
);
847 ret
= run_hook_ve(env
, name
, args
);
853 int capture_command(struct child_process
*cmd
, struct strbuf
*buf
, size_t hint
)
856 if (start_command(cmd
) < 0)
859 if (strbuf_read(buf
, cmd
->out
, hint
) < 0) {
861 finish_command(cmd
); /* throw away exit code */
866 return finish_command(cmd
);