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
;
80 else if (errno
== ENOTDIR
&& !strchr(file
, '/'))
85 static const char **prepare_shell_cmd(const char **argv
)
90 for (argc
= 0; argv
[argc
]; argc
++)
92 /* +1 for NULL, +3 for "sh -c" plus extra $0 */
93 nargv
= xmalloc(sizeof(*nargv
) * (argc
+ 1 + 3));
96 die("BUG: shell command is empty");
98 if (strcspn(argv
[0], "|&;<>()$`\\\"' \t\n*?[#~=%") != strlen(argv
[0])) {
99 nargv
[nargc
++] = "sh";
100 nargv
[nargc
++] = "-c";
103 nargv
[nargc
++] = argv
[0];
105 struct strbuf arg0
= STRBUF_INIT
;
106 strbuf_addf(&arg0
, "%s \"$@\"", argv
[0]);
107 nargv
[nargc
++] = strbuf_detach(&arg0
, NULL
);
111 for (argc
= 0; argv
[argc
]; argc
++)
112 nargv
[nargc
++] = argv
[argc
];
119 static int execv_shell_cmd(const char **argv
)
121 const char **nargv
= prepare_shell_cmd(argv
);
122 trace_argv_printf(nargv
, "trace: exec:");
123 sane_execvp(nargv
[0], (char **)nargv
);
130 static int child_err
= 2;
131 static int child_notifier
= -1;
133 static void notify_parent(void)
136 * execvp failed. If possible, we'd like to let start_command
137 * know, so failures like ENOENT can be handled right away; but
138 * otherwise, finish_command will still report the error.
140 xwrite(child_notifier
, "", 1);
143 static NORETURN
void die_child(const char *err
, va_list params
)
145 vwritef(child_err
, "fatal: ", err
, params
);
149 static void error_child(const char *err
, va_list params
)
151 vwritef(child_err
, "error: ", err
, params
);
155 static inline void set_cloexec(int fd
)
157 int flags
= fcntl(fd
, F_GETFD
);
159 fcntl(fd
, F_SETFD
, flags
| FD_CLOEXEC
);
162 static int wait_or_whine(pid_t pid
, const char *argv0
, int silent_exec_failure
)
164 int status
, code
= -1;
166 int failed_errno
= 0;
168 while ((waiting
= waitpid(pid
, &status
, 0)) < 0 && errno
== EINTR
)
172 failed_errno
= errno
;
173 error("waitpid for %s failed: %s", argv0
, strerror(errno
));
174 } else if (waiting
!= pid
) {
175 error("waitpid is confused (%s)", argv0
);
176 } else if (WIFSIGNALED(status
)) {
177 code
= WTERMSIG(status
);
178 error("%s died of signal %d", argv0
, code
);
180 * This return value is chosen so that code & 0xff
181 * mimics the exit code that a POSIX shell would report for
182 * a program that died from this signal.
185 } else if (WIFEXITED(status
)) {
186 code
= WEXITSTATUS(status
);
188 * Convert special exit code when execvp failed.
192 failed_errno
= ENOENT
;
195 error("waitpid is confused (%s)", argv0
);
197 errno
= failed_errno
;
201 int start_command(struct child_process
*cmd
)
203 int need_in
, need_out
, need_err
;
204 int fdin
[2], fdout
[2], fderr
[2];
205 int failed_errno
= failed_errno
;
208 * In case of errors we must keep the promise to close FDs
209 * that have been passed in via ->in and ->out.
212 need_in
= !cmd
->no_stdin
&& cmd
->in
< 0;
214 if (pipe(fdin
) < 0) {
215 failed_errno
= errno
;
223 need_out
= !cmd
->no_stdout
224 && !cmd
->stdout_to_stderr
227 if (pipe(fdout
) < 0) {
228 failed_errno
= errno
;
238 need_err
= !cmd
->no_stderr
&& cmd
->err
< 0;
240 if (pipe(fderr
) < 0) {
241 failed_errno
= errno
;
251 error("cannot create pipe for %s: %s",
252 cmd
->argv
[0], strerror(failed_errno
));
253 errno
= failed_errno
;
259 trace_argv_printf(cmd
->argv
, "trace: run_command:");
265 if (pipe(notify_pipe
))
266 notify_pipe
[0] = notify_pipe
[1] = -1;
271 * Redirect the channel to write syscall error messages to
272 * before redirecting the process's stderr so that all die()
273 * in subsequent call paths use the parent's stderr.
275 if (cmd
->no_stderr
|| need_err
) {
277 set_cloexec(child_err
);
279 set_die_routine(die_child
);
280 set_error_routine(error_child
);
282 close(notify_pipe
[0]);
283 set_cloexec(notify_pipe
[1]);
284 child_notifier
= notify_pipe
[1];
285 atexit(notify_parent
);
292 } else if (cmd
->in
) {
302 } else if (cmd
->err
> 1) {
309 else if (cmd
->stdout_to_stderr
)
314 } else if (cmd
->out
> 1) {
319 if (cmd
->dir
&& chdir(cmd
->dir
))
320 die_errno("exec '%s': cd to '%s' failed", cmd
->argv
[0],
323 for (; *cmd
->env
; cmd
->env
++) {
324 if (strchr(*cmd
->env
, '='))
325 putenv((char *)*cmd
->env
);
330 if (cmd
->preexec_cb
) {
332 * We cannot predict what the pre-exec callback does.
333 * Forgo parent notification.
335 close(child_notifier
);
341 execv_git_cmd(cmd
->argv
);
342 } else if (cmd
->use_shell
) {
343 execv_shell_cmd(cmd
->argv
);
345 sane_execvp(cmd
->argv
[0], (char *const*) cmd
->argv
);
347 if (errno
== ENOENT
) {
348 if (!cmd
->silent_exec_failure
)
349 error("cannot run %s: %s", cmd
->argv
[0],
353 die_errno("cannot exec '%s'", cmd
->argv
[0]);
357 error("cannot fork() for %s: %s", cmd
->argv
[0],
358 strerror(failed_errno
= errno
));
361 * Wait for child's execvp. If the execvp succeeds (or if fork()
362 * failed), EOF is seen immediately by the parent. Otherwise, the
363 * child process sends a single byte.
364 * Note that use of this infrastructure is completely advisory,
365 * therefore, we keep error checks minimal.
367 close(notify_pipe
[1]);
368 if (read(notify_pipe
[0], ¬ify_pipe
[1], 1) == 1) {
370 * At this point we know that fork() succeeded, but execvp()
371 * failed. Errors have been reported to our stderr.
373 wait_or_whine(cmd
->pid
, cmd
->argv
[0],
374 cmd
->silent_exec_failure
);
375 failed_errno
= errno
;
378 close(notify_pipe
[0]);
382 int fhin
= 0, fhout
= 1, fherr
= 2;
383 const char **sargv
= cmd
->argv
;
384 char **env
= environ
;
387 fhin
= open("/dev/null", O_RDWR
);
394 fherr
= open("/dev/null", O_RDWR
);
396 fherr
= dup(fderr
[1]);
397 else if (cmd
->err
> 2)
398 fherr
= dup(cmd
->err
);
401 fhout
= open("/dev/null", O_RDWR
);
402 else if (cmd
->stdout_to_stderr
)
405 fhout
= dup(fdout
[1]);
406 else if (cmd
->out
> 1)
407 fhout
= dup(cmd
->out
);
410 env
= make_augmented_environ(cmd
->env
);
413 cmd
->argv
= prepare_git_cmd(cmd
->argv
);
414 } else if (cmd
->use_shell
) {
415 cmd
->argv
= prepare_shell_cmd(cmd
->argv
);
418 cmd
->pid
= mingw_spawnvpe(cmd
->argv
[0], cmd
->argv
, env
, cmd
->dir
,
420 failed_errno
= errno
;
421 if (cmd
->pid
< 0 && (!cmd
->silent_exec_failure
|| errno
!= ENOENT
))
422 error("cannot spawn %s: %s", cmd
->argv
[0], strerror(errno
));
452 errno
= failed_errno
;
474 int finish_command(struct child_process
*cmd
)
476 return wait_or_whine(cmd
->pid
, cmd
->argv
[0], cmd
->silent_exec_failure
);
479 int run_command(struct child_process
*cmd
)
481 int code
= start_command(cmd
);
484 return finish_command(cmd
);
487 static void prepare_run_command_v_opt(struct child_process
*cmd
,
491 memset(cmd
, 0, sizeof(*cmd
));
493 cmd
->no_stdin
= opt
& RUN_COMMAND_NO_STDIN
? 1 : 0;
494 cmd
->git_cmd
= opt
& RUN_GIT_CMD
? 1 : 0;
495 cmd
->stdout_to_stderr
= opt
& RUN_COMMAND_STDOUT_TO_STDERR
? 1 : 0;
496 cmd
->silent_exec_failure
= opt
& RUN_SILENT_EXEC_FAILURE
? 1 : 0;
497 cmd
->use_shell
= opt
& RUN_USING_SHELL
? 1 : 0;
500 int run_command_v_opt(const char **argv
, int opt
)
502 struct child_process cmd
;
503 prepare_run_command_v_opt(&cmd
, argv
, opt
);
504 return run_command(&cmd
);
507 int run_command_v_opt_cd_env(const char **argv
, int opt
, const char *dir
, const char *const *env
)
509 struct child_process cmd
;
510 prepare_run_command_v_opt(&cmd
, argv
, opt
);
513 return run_command(&cmd
);
517 static pthread_t main_thread
;
518 static int main_thread_set
;
519 static pthread_key_t async_key
;
521 static void *run_thread(void *data
)
523 struct async
*async
= data
;
526 pthread_setspecific(async_key
, async
);
527 ret
= async
->proc(async
->proc_in
, async
->proc_out
, async
->data
);
531 static NORETURN
void die_async(const char *err
, va_list params
)
533 vreportf("fatal: ", err
, params
);
535 if (!pthread_equal(main_thread
, pthread_self())) {
536 struct async
*async
= pthread_getspecific(async_key
);
537 if (async
->proc_in
>= 0)
538 close(async
->proc_in
);
539 if (async
->proc_out
>= 0)
540 close(async
->proc_out
);
541 pthread_exit((void *)128);
548 int start_async(struct async
*async
)
550 int need_in
, need_out
;
551 int fdin
[2], fdout
[2];
552 int proc_in
, proc_out
;
554 need_in
= async
->in
< 0;
556 if (pipe(fdin
) < 0) {
559 return error("cannot create pipe: %s", strerror(errno
));
564 need_out
= async
->out
< 0;
566 if (pipe(fdout
) < 0) {
571 return error("cannot create pipe: %s", strerror(errno
));
573 async
->out
= fdout
[0];
586 proc_out
= async
->out
;
591 /* Flush stdio before fork() to avoid cloning buffers */
595 if (async
->pid
< 0) {
596 error("fork (async) failed: %s", strerror(errno
));
604 exit(!!async
->proc(proc_in
, proc_out
, async
->data
));
617 if (!main_thread_set
) {
619 * We assume that the first time that start_async is called
620 * it is from the main thread.
623 main_thread
= pthread_self();
624 pthread_key_create(&async_key
, NULL
);
625 set_die_routine(die_async
);
629 set_cloexec(proc_in
);
631 set_cloexec(proc_out
);
632 async
->proc_in
= proc_in
;
633 async
->proc_out
= proc_out
;
635 int err
= pthread_create(&async
->tid
, NULL
, run_thread
, async
);
637 error("cannot create thread: %s", strerror(err
));
657 int finish_async(struct async
*async
)
660 return wait_or_whine(async
->pid
, "child process", 0);
662 void *ret
= (void *)(intptr_t)(-1);
664 if (pthread_join(async
->tid
, &ret
))
665 error("pthread_join failed");
666 return (int)(intptr_t)ret
;
670 int run_hook(const char *index_file
, const char *name
, ...)
672 struct child_process hook
;
673 struct argv_array argv
= ARGV_ARRAY_INIT
;
674 const char *p
, *env
[2];
675 char index
[PATH_MAX
];
679 if (access(git_path("hooks/%s", name
), X_OK
) < 0)
682 va_start(args
, name
);
683 argv_array_push(&argv
, git_path("hooks/%s", name
));
684 while ((p
= va_arg(args
, const char *)))
685 argv_array_push(&argv
, p
);
688 memset(&hook
, 0, sizeof(hook
));
689 hook
.argv
= argv
.argv
;
691 hook
.stdout_to_stderr
= 1;
693 snprintf(index
, sizeof(index
), "GIT_INDEX_FILE=%s", index_file
);
699 ret
= run_command(&hook
);
700 argv_array_clear(&argv
);