2 #include "run-command.h"
5 #include "argv-array.h"
8 # define SHELL_PATH "/bin/sh"
11 void child_process_init(struct child_process
*child
)
13 memset(child
, 0, sizeof(*child
));
14 argv_array_init(&child
->args
);
15 argv_array_init(&child
->env_array
);
18 struct child_to_clean
{
20 struct child_to_clean
*next
;
22 static struct child_to_clean
*children_to_clean
;
23 static int installed_child_cleanup_handler
;
25 static void cleanup_children(int sig
)
27 while (children_to_clean
) {
28 struct child_to_clean
*p
= children_to_clean
;
29 children_to_clean
= p
->next
;
35 static void cleanup_children_on_signal(int sig
)
37 cleanup_children(sig
);
42 static void cleanup_children_on_exit(void)
44 cleanup_children(SIGTERM
);
47 static void mark_child_for_cleanup(pid_t pid
)
49 struct child_to_clean
*p
= xmalloc(sizeof(*p
));
51 p
->next
= children_to_clean
;
52 children_to_clean
= p
;
54 if (!installed_child_cleanup_handler
) {
55 atexit(cleanup_children_on_exit
);
56 sigchain_push_common(cleanup_children_on_signal
);
57 installed_child_cleanup_handler
= 1;
61 static void clear_child_for_cleanup(pid_t pid
)
63 struct child_to_clean
**pp
;
65 for (pp
= &children_to_clean
; *pp
; pp
= &(*pp
)->next
) {
66 struct child_to_clean
*clean_me
= *pp
;
68 if (clean_me
->pid
== pid
) {
76 static inline void close_pair(int fd
[2])
82 #ifndef GIT_WINDOWS_NATIVE
83 static inline void dup_devnull(int to
)
85 int fd
= open("/dev/null", O_RDWR
);
87 die_errno(_("open /dev/null failed"));
89 die_errno(_("dup2(%d,%d) failed"), fd
, to
);
94 static char *locate_in_PATH(const char *file
)
96 const char *p
= getenv("PATH");
97 struct strbuf buf
= STRBUF_INIT
;
103 const char *end
= strchrnul(p
, ':');
107 /* POSIX specifies an empty entry as the current directory. */
109 strbuf_add(&buf
, p
, end
- p
);
110 strbuf_addch(&buf
, '/');
112 strbuf_addstr(&buf
, file
);
114 if (!access(buf
.buf
, F_OK
))
115 return strbuf_detach(&buf
, NULL
);
122 strbuf_release(&buf
);
126 static int exists_in_PATH(const char *file
)
128 char *r
= locate_in_PATH(file
);
133 int sane_execvp(const char *file
, char * const argv
[])
135 if (!execvp(file
, argv
))
136 return 0; /* cannot happen ;-) */
139 * When a command can't be found because one of the directories
140 * listed in $PATH is unsearchable, execvp reports EACCES, but
141 * careful usability testing (read: analysis of occasional bug
142 * reports) reveals that "No such file or directory" is more
145 * We avoid commands with "/", because execvp will not do $PATH
146 * lookups in that case.
148 * The reassignment of EACCES to errno looks like a no-op below,
149 * but we need to protect against exists_in_PATH overwriting errno.
151 if (errno
== EACCES
&& !strchr(file
, '/'))
152 errno
= exists_in_PATH(file
) ? EACCES
: ENOENT
;
153 else if (errno
== ENOTDIR
&& !strchr(file
, '/'))
158 static const char **prepare_shell_cmd(const char **argv
)
163 for (argc
= 0; argv
[argc
]; argc
++)
164 ; /* just counting */
165 /* +1 for NULL, +3 for "sh -c" plus extra $0 */
166 nargv
= xmalloc(sizeof(*nargv
) * (argc
+ 1 + 3));
169 die("BUG: shell command is empty");
171 if (strcspn(argv
[0], "|&;<>()$`\\\"' \t\n*?[#~=%") != strlen(argv
[0])) {
172 #ifndef GIT_WINDOWS_NATIVE
173 nargv
[nargc
++] = SHELL_PATH
;
175 nargv
[nargc
++] = "sh";
177 nargv
[nargc
++] = "-c";
180 nargv
[nargc
++] = argv
[0];
182 struct strbuf arg0
= STRBUF_INIT
;
183 strbuf_addf(&arg0
, "%s \"$@\"", argv
[0]);
184 nargv
[nargc
++] = strbuf_detach(&arg0
, NULL
);
188 for (argc
= 0; argv
[argc
]; argc
++)
189 nargv
[nargc
++] = argv
[argc
];
195 #ifndef GIT_WINDOWS_NATIVE
196 static int execv_shell_cmd(const char **argv
)
198 const char **nargv
= prepare_shell_cmd(argv
);
199 trace_argv_printf(nargv
, "trace: exec:");
200 sane_execvp(nargv
[0], (char **)nargv
);
206 #ifndef GIT_WINDOWS_NATIVE
207 static int child_err
= 2;
208 static int child_notifier
= -1;
210 static void notify_parent(void)
213 * execvp failed. If possible, we'd like to let start_command
214 * know, so failures like ENOENT can be handled right away; but
215 * otherwise, finish_command will still report the error.
217 xwrite(child_notifier
, "", 1);
220 static NORETURN
void die_child(const char *err
, va_list params
)
222 vwritef(child_err
, "fatal: ", err
, params
);
226 static void error_child(const char *err
, va_list params
)
228 vwritef(child_err
, "error: ", err
, params
);
232 static inline void set_cloexec(int fd
)
234 int flags
= fcntl(fd
, F_GETFD
);
236 fcntl(fd
, F_SETFD
, flags
| FD_CLOEXEC
);
239 static int wait_or_whine(pid_t pid
, const char *argv0
)
241 int status
, code
= -1;
243 int failed_errno
= 0;
245 while ((waiting
= waitpid(pid
, &status
, 0)) < 0 && errno
== EINTR
)
249 failed_errno
= errno
;
250 error("waitpid for %s failed: %s", argv0
, strerror(errno
));
251 } else if (waiting
!= pid
) {
252 error("waitpid is confused (%s)", argv0
);
253 } else if (WIFSIGNALED(status
)) {
254 code
= WTERMSIG(status
);
255 if (code
!= SIGINT
&& code
!= SIGQUIT
)
256 error("%s died of signal %d", argv0
, code
);
258 * This return value is chosen so that code & 0xff
259 * mimics the exit code that a POSIX shell would report for
260 * a program that died from this signal.
263 } else if (WIFEXITED(status
)) {
264 code
= WEXITSTATUS(status
);
266 * Convert special exit code when execvp failed.
270 failed_errno
= ENOENT
;
273 error("waitpid is confused (%s)", argv0
);
276 clear_child_for_cleanup(pid
);
278 errno
= failed_errno
;
282 int start_command(struct child_process
*cmd
)
284 int need_in
, need_out
, need_err
;
285 int fdin
[2], fdout
[2], fderr
[2];
290 cmd
->argv
= cmd
->args
.argv
;
292 cmd
->env
= cmd
->env_array
.argv
;
295 * In case of errors we must keep the promise to close FDs
296 * that have been passed in via ->in and ->out.
299 need_in
= !cmd
->no_stdin
&& cmd
->in
< 0;
301 if (pipe(fdin
) < 0) {
302 failed_errno
= errno
;
305 str
= "standard input";
311 need_out
= !cmd
->no_stdout
312 && !cmd
->stdout_to_stderr
315 if (pipe(fdout
) < 0) {
316 failed_errno
= errno
;
321 str
= "standard output";
327 need_err
= !cmd
->no_stderr
&& cmd
->err
< 0;
329 if (pipe(fderr
) < 0) {
330 failed_errno
= errno
;
339 str
= "standard error";
341 error("cannot create %s pipe for %s: %s",
342 str
, cmd
->argv
[0], strerror(failed_errno
));
343 argv_array_clear(&cmd
->args
);
344 argv_array_clear(&cmd
->env_array
);
345 errno
= failed_errno
;
351 trace_argv_printf(cmd
->argv
, "trace: run_command:");
354 #ifndef GIT_WINDOWS_NATIVE
357 if (pipe(notify_pipe
))
358 notify_pipe
[0] = notify_pipe
[1] = -1;
361 failed_errno
= errno
;
364 * Redirect the channel to write syscall error messages to
365 * before redirecting the process's stderr so that all die()
366 * in subsequent call paths use the parent's stderr.
368 if (cmd
->no_stderr
|| need_err
) {
370 set_cloexec(child_err
);
372 set_die_routine(die_child
);
373 set_error_routine(error_child
);
375 close(notify_pipe
[0]);
376 set_cloexec(notify_pipe
[1]);
377 child_notifier
= notify_pipe
[1];
378 atexit(notify_parent
);
385 } else if (cmd
->in
) {
395 } else if (cmd
->err
> 1) {
402 else if (cmd
->stdout_to_stderr
)
407 } else if (cmd
->out
> 1) {
412 if (cmd
->dir
&& chdir(cmd
->dir
))
413 die_errno("exec '%s': cd to '%s' failed", cmd
->argv
[0],
416 for (; *cmd
->env
; cmd
->env
++) {
417 if (strchr(*cmd
->env
, '='))
418 putenv((char *)*cmd
->env
);
424 execv_git_cmd(cmd
->argv
);
425 else if (cmd
->use_shell
)
426 execv_shell_cmd(cmd
->argv
);
428 sane_execvp(cmd
->argv
[0], (char *const*) cmd
->argv
);
429 if (errno
== ENOENT
) {
430 if (!cmd
->silent_exec_failure
)
431 error("cannot run %s: %s", cmd
->argv
[0],
435 die_errno("cannot exec '%s'", cmd
->argv
[0]);
439 error("cannot fork() for %s: %s", cmd
->argv
[0],
441 else if (cmd
->clean_on_exit
)
442 mark_child_for_cleanup(cmd
->pid
);
445 * Wait for child's execvp. If the execvp succeeds (or if fork()
446 * failed), EOF is seen immediately by the parent. Otherwise, the
447 * child process sends a single byte.
448 * Note that use of this infrastructure is completely advisory,
449 * therefore, we keep error checks minimal.
451 close(notify_pipe
[1]);
452 if (read(notify_pipe
[0], ¬ify_pipe
[1], 1) == 1) {
454 * At this point we know that fork() succeeded, but execvp()
455 * failed. Errors have been reported to our stderr.
457 wait_or_whine(cmd
->pid
, cmd
->argv
[0]);
458 failed_errno
= errno
;
461 close(notify_pipe
[0]);
465 int fhin
= 0, fhout
= 1, fherr
= 2;
466 const char **sargv
= cmd
->argv
;
469 fhin
= open("/dev/null", O_RDWR
);
476 fherr
= open("/dev/null", O_RDWR
);
478 fherr
= dup(fderr
[1]);
479 else if (cmd
->err
> 2)
480 fherr
= dup(cmd
->err
);
483 fhout
= open("/dev/null", O_RDWR
);
484 else if (cmd
->stdout_to_stderr
)
487 fhout
= dup(fdout
[1]);
488 else if (cmd
->out
> 1)
489 fhout
= dup(cmd
->out
);
492 cmd
->argv
= prepare_git_cmd(cmd
->argv
);
493 else if (cmd
->use_shell
)
494 cmd
->argv
= prepare_shell_cmd(cmd
->argv
);
496 cmd
->pid
= mingw_spawnvpe(cmd
->argv
[0], cmd
->argv
, (char**) cmd
->env
,
497 cmd
->dir
, fhin
, fhout
, fherr
);
498 failed_errno
= errno
;
499 if (cmd
->pid
< 0 && (!cmd
->silent_exec_failure
|| errno
!= ENOENT
))
500 error("cannot spawn %s: %s", cmd
->argv
[0], strerror(errno
));
501 if (cmd
->clean_on_exit
&& cmd
->pid
>= 0)
502 mark_child_for_cleanup(cmd
->pid
);
530 argv_array_clear(&cmd
->args
);
531 argv_array_clear(&cmd
->env_array
);
532 errno
= failed_errno
;
554 int finish_command(struct child_process
*cmd
)
556 int ret
= wait_or_whine(cmd
->pid
, cmd
->argv
[0]);
557 argv_array_clear(&cmd
->args
);
558 argv_array_clear(&cmd
->env_array
);
562 int run_command(struct child_process
*cmd
)
564 int code
= start_command(cmd
);
567 return finish_command(cmd
);
570 int run_command_v_opt(const char **argv
, int opt
)
572 return run_command_v_opt_cd_env(argv
, opt
, NULL
, NULL
);
575 int run_command_v_opt_cd_env(const char **argv
, int opt
, const char *dir
, const char *const *env
)
577 struct child_process cmd
= CHILD_PROCESS_INIT
;
579 cmd
.no_stdin
= opt
& RUN_COMMAND_NO_STDIN
? 1 : 0;
580 cmd
.git_cmd
= opt
& RUN_GIT_CMD
? 1 : 0;
581 cmd
.stdout_to_stderr
= opt
& RUN_COMMAND_STDOUT_TO_STDERR
? 1 : 0;
582 cmd
.silent_exec_failure
= opt
& RUN_SILENT_EXEC_FAILURE
? 1 : 0;
583 cmd
.use_shell
= opt
& RUN_USING_SHELL
? 1 : 0;
584 cmd
.clean_on_exit
= opt
& RUN_CLEAN_ON_EXIT
? 1 : 0;
587 return run_command(&cmd
);
591 static pthread_t main_thread
;
592 static int main_thread_set
;
593 static pthread_key_t async_key
;
594 static pthread_key_t async_die_counter
;
596 static void *run_thread(void *data
)
598 struct async
*async
= data
;
601 pthread_setspecific(async_key
, async
);
602 ret
= async
->proc(async
->proc_in
, async
->proc_out
, async
->data
);
606 static NORETURN
void die_async(const char *err
, va_list params
)
608 vreportf("fatal: ", err
, params
);
610 if (!pthread_equal(main_thread
, pthread_self())) {
611 struct async
*async
= pthread_getspecific(async_key
);
612 if (async
->proc_in
>= 0)
613 close(async
->proc_in
);
614 if (async
->proc_out
>= 0)
615 close(async
->proc_out
);
616 pthread_exit((void *)128);
622 static int async_die_is_recursing(void)
624 void *ret
= pthread_getspecific(async_die_counter
);
625 pthread_setspecific(async_die_counter
, (void *)1);
632 void (**handlers
)(void);
637 static int git_atexit_installed
;
639 static void git_atexit_dispatch(void)
643 for (i
=git_atexit_hdlrs
.nr
; i
; i
--)
644 git_atexit_hdlrs
.handlers
[i
-1]();
647 static void git_atexit_clear(void)
649 free(git_atexit_hdlrs
.handlers
);
650 memset(&git_atexit_hdlrs
, 0, sizeof(git_atexit_hdlrs
));
651 git_atexit_installed
= 0;
655 int git_atexit(void (*handler
)(void))
657 ALLOC_GROW(git_atexit_hdlrs
.handlers
, git_atexit_hdlrs
.nr
+ 1, git_atexit_hdlrs
.alloc
);
658 git_atexit_hdlrs
.handlers
[git_atexit_hdlrs
.nr
++] = handler
;
659 if (!git_atexit_installed
) {
660 if (atexit(&git_atexit_dispatch
))
662 git_atexit_installed
= 1;
666 #define atexit git_atexit
670 int start_async(struct async
*async
)
672 int need_in
, need_out
;
673 int fdin
[2], fdout
[2];
674 int proc_in
, proc_out
;
676 need_in
= async
->in
< 0;
678 if (pipe(fdin
) < 0) {
681 return error("cannot create pipe: %s", strerror(errno
));
686 need_out
= async
->out
< 0;
688 if (pipe(fdout
) < 0) {
693 return error("cannot create pipe: %s", strerror(errno
));
695 async
->out
= fdout
[0];
708 proc_out
= async
->out
;
713 /* Flush stdio before fork() to avoid cloning buffers */
717 if (async
->pid
< 0) {
718 error("fork (async) failed: %s", strerror(errno
));
727 exit(!!async
->proc(proc_in
, proc_out
, async
->data
));
730 mark_child_for_cleanup(async
->pid
);
742 if (!main_thread_set
) {
744 * We assume that the first time that start_async is called
745 * it is from the main thread.
748 main_thread
= pthread_self();
749 pthread_key_create(&async_key
, NULL
);
750 pthread_key_create(&async_die_counter
, NULL
);
751 set_die_routine(die_async
);
752 set_die_is_recursing_routine(async_die_is_recursing
);
756 set_cloexec(proc_in
);
758 set_cloexec(proc_out
);
759 async
->proc_in
= proc_in
;
760 async
->proc_out
= proc_out
;
762 int err
= pthread_create(&async
->tid
, NULL
, run_thread
, async
);
764 error("cannot create thread: %s", strerror(err
));
784 int finish_async(struct async
*async
)
787 return wait_or_whine(async
->pid
, "child process");
789 void *ret
= (void *)(intptr_t)(-1);
791 if (pthread_join(async
->tid
, &ret
))
792 error("pthread_join failed");
793 return (int)(intptr_t)ret
;
797 char *find_hook(const char *name
)
799 char *path
= git_path("hooks/%s", name
);
800 if (access(path
, X_OK
) < 0)
806 int run_hook_ve(const char *const *env
, const char *name
, va_list args
)
808 struct child_process hook
= CHILD_PROCESS_INIT
;
815 argv_array_push(&hook
.args
, p
);
816 while ((p
= va_arg(args
, const char *)))
817 argv_array_push(&hook
.args
, p
);
820 hook
.stdout_to_stderr
= 1;
822 return run_command(&hook
);
825 int run_hook_le(const char *const *env
, const char *name
, ...)
830 va_start(args
, name
);
831 ret
= run_hook_ve(env
, name
, args
);
837 int capture_command(struct child_process
*cmd
, struct strbuf
*buf
, size_t hint
)
840 if (start_command(cmd
) < 0)
843 if (strbuf_read(buf
, cmd
->out
, hint
) < 0) {
845 finish_command(cmd
); /* throw away exit code */
850 return finish_command(cmd
);