2 #include "run-command.h"
4 #include "argv-array.h"
6 static inline void close_pair(int fd
[2])
13 static inline void dup_devnull(int to
)
15 int fd
= open("/dev/null", O_RDWR
);
21 static char *locate_in_PATH(const char *file
)
23 const char *p
= getenv("PATH");
24 struct strbuf buf
= STRBUF_INIT
;
30 const char *end
= strchrnul(p
, ':');
34 /* POSIX specifies an empty entry as the current directory. */
36 strbuf_add(&buf
, p
, end
- p
);
37 strbuf_addch(&buf
, '/');
39 strbuf_addstr(&buf
, file
);
41 if (!access(buf
.buf
, F_OK
))
42 return strbuf_detach(&buf
, NULL
);
53 static int exists_in_PATH(const char *file
)
55 char *r
= locate_in_PATH(file
);
60 int sane_execvp(const char *file
, char * const argv
[])
62 if (!execvp(file
, argv
))
63 return 0; /* cannot happen ;-) */
66 * When a command can't be found because one of the directories
67 * listed in $PATH is unsearchable, execvp reports EACCES, but
68 * careful usability testing (read: analysis of occasional bug
69 * reports) reveals that "No such file or directory" is more
72 * We avoid commands with "/", because execvp will not do $PATH
73 * lookups in that case.
75 * The reassignment of EACCES to errno looks like a no-op below,
76 * but we need to protect against exists_in_PATH overwriting errno.
78 if (errno
== EACCES
&& !strchr(file
, '/'))
79 errno
= exists_in_PATH(file
) ? EACCES
: ENOENT
;
83 static const char **prepare_shell_cmd(const char **argv
)
88 for (argc
= 0; argv
[argc
]; argc
++)
90 /* +1 for NULL, +3 for "sh -c" plus extra $0 */
91 nargv
= xmalloc(sizeof(*nargv
) * (argc
+ 1 + 3));
94 die("BUG: shell command is empty");
96 if (strcspn(argv
[0], "|&;<>()$`\\\"' \t\n*?[#~=%") != strlen(argv
[0])) {
97 nargv
[nargc
++] = "sh";
98 nargv
[nargc
++] = "-c";
101 nargv
[nargc
++] = argv
[0];
103 struct strbuf arg0
= STRBUF_INIT
;
104 strbuf_addf(&arg0
, "%s \"$@\"", argv
[0]);
105 nargv
[nargc
++] = strbuf_detach(&arg0
, NULL
);
109 for (argc
= 0; argv
[argc
]; argc
++)
110 nargv
[nargc
++] = argv
[argc
];
117 static int execv_shell_cmd(const char **argv
)
119 const char **nargv
= prepare_shell_cmd(argv
);
120 trace_argv_printf(nargv
, "trace: exec:");
121 sane_execvp(nargv
[0], (char **)nargv
);
128 static int child_err
= 2;
129 static int child_notifier
= -1;
131 static void notify_parent(void)
134 * execvp failed. If possible, we'd like to let start_command
135 * know, so failures like ENOENT can be handled right away; but
136 * otherwise, finish_command will still report the error.
138 xwrite(child_notifier
, "", 1);
141 static NORETURN
void die_child(const char *err
, va_list params
)
143 vwritef(child_err
, "fatal: ", err
, params
);
147 static void error_child(const char *err
, va_list params
)
149 vwritef(child_err
, "error: ", err
, params
);
153 static inline void set_cloexec(int fd
)
155 int flags
= fcntl(fd
, F_GETFD
);
157 fcntl(fd
, F_SETFD
, flags
| FD_CLOEXEC
);
160 static int wait_or_whine(pid_t pid
, const char *argv0
, int silent_exec_failure
)
162 int status
, code
= -1;
164 int failed_errno
= 0;
166 while ((waiting
= waitpid(pid
, &status
, 0)) < 0 && errno
== EINTR
)
170 failed_errno
= errno
;
171 error("waitpid for %s failed: %s", argv0
, strerror(errno
));
172 } else if (waiting
!= pid
) {
173 error("waitpid is confused (%s)", argv0
);
174 } else if (WIFSIGNALED(status
)) {
175 code
= WTERMSIG(status
);
176 error("%s died of signal %d", argv0
, code
);
178 * This return value is chosen so that code & 0xff
179 * mimics the exit code that a POSIX shell would report for
180 * a program that died from this signal.
183 } else if (WIFEXITED(status
)) {
184 code
= WEXITSTATUS(status
);
186 * Convert special exit code when execvp failed.
190 failed_errno
= ENOENT
;
193 error("waitpid is confused (%s)", argv0
);
195 errno
= failed_errno
;
199 int start_command(struct child_process
*cmd
)
201 int need_in
, need_out
, need_err
;
202 int fdin
[2], fdout
[2], fderr
[2];
203 int failed_errno
= failed_errno
;
206 * In case of errors we must keep the promise to close FDs
207 * that have been passed in via ->in and ->out.
210 need_in
= !cmd
->no_stdin
&& cmd
->in
< 0;
212 if (pipe(fdin
) < 0) {
213 failed_errno
= errno
;
221 need_out
= !cmd
->no_stdout
222 && !cmd
->stdout_to_stderr
225 if (pipe(fdout
) < 0) {
226 failed_errno
= errno
;
236 need_err
= !cmd
->no_stderr
&& cmd
->err
< 0;
238 if (pipe(fderr
) < 0) {
239 failed_errno
= errno
;
249 error("cannot create pipe for %s: %s",
250 cmd
->argv
[0], strerror(failed_errno
));
251 errno
= failed_errno
;
257 trace_argv_printf(cmd
->argv
, "trace: run_command:");
263 if (pipe(notify_pipe
))
264 notify_pipe
[0] = notify_pipe
[1] = -1;
269 * Redirect the channel to write syscall error messages to
270 * before redirecting the process's stderr so that all die()
271 * in subsequent call paths use the parent's stderr.
273 if (cmd
->no_stderr
|| need_err
) {
275 set_cloexec(child_err
);
277 set_die_routine(die_child
);
278 set_error_routine(error_child
);
280 close(notify_pipe
[0]);
281 set_cloexec(notify_pipe
[1]);
282 child_notifier
= notify_pipe
[1];
283 atexit(notify_parent
);
290 } else if (cmd
->in
) {
300 } else if (cmd
->err
> 1) {
307 else if (cmd
->stdout_to_stderr
)
312 } else if (cmd
->out
> 1) {
317 if (cmd
->dir
&& chdir(cmd
->dir
))
318 die_errno("exec '%s': cd to '%s' failed", cmd
->argv
[0],
321 for (; *cmd
->env
; cmd
->env
++) {
322 if (strchr(*cmd
->env
, '='))
323 putenv((char *)*cmd
->env
);
328 if (cmd
->preexec_cb
) {
330 * We cannot predict what the pre-exec callback does.
331 * Forgo parent notification.
333 close(child_notifier
);
339 execv_git_cmd(cmd
->argv
);
340 } else if (cmd
->use_shell
) {
341 execv_shell_cmd(cmd
->argv
);
343 sane_execvp(cmd
->argv
[0], (char *const*) cmd
->argv
);
345 if (errno
== ENOENT
) {
346 if (!cmd
->silent_exec_failure
)
347 error("cannot run %s: %s", cmd
->argv
[0],
351 die_errno("cannot exec '%s'", cmd
->argv
[0]);
355 error("cannot fork() for %s: %s", cmd
->argv
[0],
356 strerror(failed_errno
= errno
));
359 * Wait for child's execvp. If the execvp succeeds (or if fork()
360 * failed), EOF is seen immediately by the parent. Otherwise, the
361 * child process sends a single byte.
362 * Note that use of this infrastructure is completely advisory,
363 * therefore, we keep error checks minimal.
365 close(notify_pipe
[1]);
366 if (read(notify_pipe
[0], ¬ify_pipe
[1], 1) == 1) {
368 * At this point we know that fork() succeeded, but execvp()
369 * failed. Errors have been reported to our stderr.
371 wait_or_whine(cmd
->pid
, cmd
->argv
[0],
372 cmd
->silent_exec_failure
);
373 failed_errno
= errno
;
376 close(notify_pipe
[0]);
380 int fhin
= 0, fhout
= 1, fherr
= 2;
381 const char **sargv
= cmd
->argv
;
382 char **env
= environ
;
385 fhin
= open("/dev/null", O_RDWR
);
392 fherr
= open("/dev/null", O_RDWR
);
394 fherr
= dup(fderr
[1]);
395 else if (cmd
->err
> 2)
396 fherr
= dup(cmd
->err
);
399 fhout
= open("/dev/null", O_RDWR
);
400 else if (cmd
->stdout_to_stderr
)
403 fhout
= dup(fdout
[1]);
404 else if (cmd
->out
> 1)
405 fhout
= dup(cmd
->out
);
408 env
= make_augmented_environ(cmd
->env
);
411 cmd
->argv
= prepare_git_cmd(cmd
->argv
);
412 } else if (cmd
->use_shell
) {
413 cmd
->argv
= prepare_shell_cmd(cmd
->argv
);
416 cmd
->pid
= mingw_spawnvpe(cmd
->argv
[0], cmd
->argv
, env
, cmd
->dir
,
418 failed_errno
= errno
;
419 if (cmd
->pid
< 0 && (!cmd
->silent_exec_failure
|| errno
!= ENOENT
))
420 error("cannot spawn %s: %s", cmd
->argv
[0], strerror(errno
));
450 errno
= failed_errno
;
472 int finish_command(struct child_process
*cmd
)
474 return wait_or_whine(cmd
->pid
, cmd
->argv
[0], cmd
->silent_exec_failure
);
477 int run_command(struct child_process
*cmd
)
479 int code
= start_command(cmd
);
482 return finish_command(cmd
);
485 static void prepare_run_command_v_opt(struct child_process
*cmd
,
489 memset(cmd
, 0, sizeof(*cmd
));
491 cmd
->no_stdin
= opt
& RUN_COMMAND_NO_STDIN
? 1 : 0;
492 cmd
->git_cmd
= opt
& RUN_GIT_CMD
? 1 : 0;
493 cmd
->stdout_to_stderr
= opt
& RUN_COMMAND_STDOUT_TO_STDERR
? 1 : 0;
494 cmd
->silent_exec_failure
= opt
& RUN_SILENT_EXEC_FAILURE
? 1 : 0;
495 cmd
->use_shell
= opt
& RUN_USING_SHELL
? 1 : 0;
498 int run_command_v_opt(const char **argv
, int opt
)
500 struct child_process cmd
;
501 prepare_run_command_v_opt(&cmd
, argv
, opt
);
502 return run_command(&cmd
);
505 int run_command_v_opt_cd_env(const char **argv
, int opt
, const char *dir
, const char *const *env
)
507 struct child_process cmd
;
508 prepare_run_command_v_opt(&cmd
, argv
, opt
);
511 return run_command(&cmd
);
515 static pthread_t main_thread
;
516 static int main_thread_set
;
517 static pthread_key_t async_key
;
519 static void *run_thread(void *data
)
521 struct async
*async
= data
;
524 pthread_setspecific(async_key
, async
);
525 ret
= async
->proc(async
->proc_in
, async
->proc_out
, async
->data
);
529 static NORETURN
void die_async(const char *err
, va_list params
)
531 vreportf("fatal: ", err
, params
);
533 if (!pthread_equal(main_thread
, pthread_self())) {
534 struct async
*async
= pthread_getspecific(async_key
);
535 if (async
->proc_in
>= 0)
536 close(async
->proc_in
);
537 if (async
->proc_out
>= 0)
538 close(async
->proc_out
);
539 pthread_exit((void *)128);
546 int start_async(struct async
*async
)
548 int need_in
, need_out
;
549 int fdin
[2], fdout
[2];
550 int proc_in
, proc_out
;
552 need_in
= async
->in
< 0;
554 if (pipe(fdin
) < 0) {
557 return error("cannot create pipe: %s", strerror(errno
));
562 need_out
= async
->out
< 0;
564 if (pipe(fdout
) < 0) {
569 return error("cannot create pipe: %s", strerror(errno
));
571 async
->out
= fdout
[0];
584 proc_out
= async
->out
;
589 /* Flush stdio before fork() to avoid cloning buffers */
593 if (async
->pid
< 0) {
594 error("fork (async) failed: %s", strerror(errno
));
602 exit(!!async
->proc(proc_in
, proc_out
, async
->data
));
615 if (!main_thread_set
) {
617 * We assume that the first time that start_async is called
618 * it is from the main thread.
621 main_thread
= pthread_self();
622 pthread_key_create(&async_key
, NULL
);
623 set_die_routine(die_async
);
627 set_cloexec(proc_in
);
629 set_cloexec(proc_out
);
630 async
->proc_in
= proc_in
;
631 async
->proc_out
= proc_out
;
633 int err
= pthread_create(&async
->tid
, NULL
, run_thread
, async
);
635 error("cannot create thread: %s", strerror(err
));
655 int finish_async(struct async
*async
)
658 return wait_or_whine(async
->pid
, "child process", 0);
660 void *ret
= (void *)(intptr_t)(-1);
662 if (pthread_join(async
->tid
, &ret
))
663 error("pthread_join failed");
664 return (int)(intptr_t)ret
;
668 int run_hook(const char *index_file
, const char *name
, ...)
670 struct child_process hook
;
671 struct argv_array argv
= ARGV_ARRAY_INIT
;
672 const char *p
, *env
[2];
673 char index
[PATH_MAX
];
677 if (access(git_path("hooks/%s", name
), X_OK
) < 0)
680 va_start(args
, name
);
681 argv_array_push(&argv
, git_path("hooks/%s", name
));
682 while ((p
= va_arg(args
, const char *)))
683 argv_array_push(&argv
, p
);
686 memset(&hook
, 0, sizeof(hook
));
687 hook
.argv
= argv
.argv
;
689 hook
.stdout_to_stderr
= 1;
691 snprintf(index
, sizeof(index
), "GIT_INDEX_FILE=%s", index_file
);
697 ret
= run_command(&hook
);
698 argv_array_clear(&argv
);