diff options
Diffstat (limited to 'run-command.c')
-rw-r--r-- | run-command.c | 178 |
1 files changed, 134 insertions, 44 deletions
diff --git a/run-command.c b/run-command.c index 1db8abf984..3914d9c511 100644 --- a/run-command.c +++ b/run-command.c @@ -4,6 +4,10 @@ #include "sigchain.h" #include "argv-array.h" +#ifndef SHELL_PATH +# define SHELL_PATH "/bin/sh" +#endif + struct child_to_clean { pid_t pid; struct child_to_clean *next; @@ -49,13 +53,14 @@ static void mark_child_for_cleanup(pid_t pid) static void clear_child_for_cleanup(pid_t pid) { - struct child_to_clean **last, *p; + struct child_to_clean **pp; + + for (pp = &children_to_clean; *pp; pp = &(*pp)->next) { + struct child_to_clean *clean_me = *pp; - last = &children_to_clean; - for (p = children_to_clean; p; p = p->next) { - if (p->pid == pid) { - *last = p->next; - free(p); + if (clean_me->pid == pid) { + *pp = clean_me->next; + free(clean_me); return; } } @@ -67,15 +72,82 @@ static inline void close_pair(int fd[2]) close(fd[1]); } -#ifndef WIN32 +#ifndef GIT_WINDOWS_NATIVE static inline void dup_devnull(int to) { int fd = open("/dev/null", O_RDWR); - dup2(fd, to); + if (fd < 0) + die_errno(_("open /dev/null failed")); + if (dup2(fd, to) < 0) + die_errno(_("dup2(%d,%d) failed"), fd, to); close(fd); } #endif +static char *locate_in_PATH(const char *file) +{ + const char *p = getenv("PATH"); + struct strbuf buf = STRBUF_INIT; + + if (!p || !*p) + return NULL; + + while (1) { + const char *end = strchrnul(p, ':'); + + strbuf_reset(&buf); + + /* POSIX specifies an empty entry as the current directory. */ + if (end != p) { + strbuf_add(&buf, p, end - p); + strbuf_addch(&buf, '/'); + } + strbuf_addstr(&buf, file); + + if (!access(buf.buf, F_OK)) + return strbuf_detach(&buf, NULL); + + if (!*end) + break; + p = end + 1; + } + + strbuf_release(&buf); + return NULL; +} + +static int exists_in_PATH(const char *file) +{ + char *r = locate_in_PATH(file); + free(r); + return r != NULL; +} + +int sane_execvp(const char *file, char * const argv[]) +{ + if (!execvp(file, argv)) + return 0; /* cannot happen ;-) */ + + /* + * When a command can't be found because one of the directories + * listed in $PATH is unsearchable, execvp reports EACCES, but + * careful usability testing (read: analysis of occasional bug + * reports) reveals that "No such file or directory" is more + * intuitive. + * + * We avoid commands with "/", because execvp will not do $PATH + * lookups in that case. + * + * The reassignment of EACCES to errno looks like a no-op below, + * but we need to protect against exists_in_PATH overwriting errno. + */ + if (errno == EACCES && !strchr(file, '/')) + errno = exists_in_PATH(file) ? EACCES : ENOENT; + else if (errno == ENOTDIR && !strchr(file, '/')) + errno = ENOENT; + return -1; +} + static const char **prepare_shell_cmd(const char **argv) { int argc, nargc = 0; @@ -90,7 +162,11 @@ static const char **prepare_shell_cmd(const char **argv) die("BUG: shell command is empty"); if (strcspn(argv[0], "|&;<>()$`\\\"' \t\n*?[#~=%") != strlen(argv[0])) { +#ifndef GIT_WINDOWS_NATIVE + nargv[nargc++] = SHELL_PATH; +#else nargv[nargc++] = "sh"; +#endif nargv[nargc++] = "-c"; if (argc < 2) @@ -109,18 +185,18 @@ static const char **prepare_shell_cmd(const char **argv) return nargv; } -#ifndef WIN32 +#ifndef GIT_WINDOWS_NATIVE static int execv_shell_cmd(const char **argv) { const char **nargv = prepare_shell_cmd(argv); trace_argv_printf(nargv, "trace: exec:"); - execvp(nargv[0], (char **)nargv); + sane_execvp(nargv[0], (char **)nargv); free(nargv); return -1; } #endif -#ifndef WIN32 +#ifndef GIT_WINDOWS_NATIVE static int child_err = 2; static int child_notifier = -1; @@ -153,7 +229,7 @@ static inline void set_cloexec(int fd) fcntl(fd, F_SETFD, flags | FD_CLOEXEC); } -static int wait_or_whine(pid_t pid, const char *argv0, int silent_exec_failure) +static int wait_or_whine(pid_t pid, const char *argv0) { int status, code = -1; pid_t waiting; @@ -169,13 +245,14 @@ static int wait_or_whine(pid_t pid, const char *argv0, int silent_exec_failure) error("waitpid is confused (%s)", argv0); } else if (WIFSIGNALED(status)) { code = WTERMSIG(status); - error("%s died of signal %d", argv0, code); + if (code != SIGINT && code != SIGQUIT) + error("%s died of signal %d", argv0, code); /* * This return value is chosen so that code & 0xff * mimics the exit code that a POSIX shell would report for * a program that died from this signal. */ - code -= 128; + code += 128; } else if (WIFEXITED(status)) { code = WEXITSTATUS(status); /* @@ -199,7 +276,8 @@ int start_command(struct child_process *cmd) { int need_in, need_out, need_err; int fdin[2], fdout[2], fderr[2]; - int failed_errno = failed_errno; + int failed_errno; + char *str; /* * In case of errors we must keep the promise to close FDs @@ -212,6 +290,7 @@ int start_command(struct child_process *cmd) failed_errno = errno; if (cmd->out > 0) close(cmd->out); + str = "standard input"; goto fail_pipe; } cmd->in = fdin[1]; @@ -227,6 +306,7 @@ int start_command(struct child_process *cmd) close_pair(fdin); else if (cmd->in) close(cmd->in); + str = "standard output"; goto fail_pipe; } cmd->out = fdout[0]; @@ -244,9 +324,10 @@ int start_command(struct child_process *cmd) close_pair(fdout); else if (cmd->out) close(cmd->out); + str = "standard error"; fail_pipe: - error("cannot create pipe for %s: %s", - cmd->argv[0], strerror(failed_errno)); + error("cannot create %s pipe for %s: %s", + str, cmd->argv[0], strerror(failed_errno)); errno = failed_errno; return -1; } @@ -256,13 +337,14 @@ fail_pipe: trace_argv_printf(cmd->argv, "trace: run_command:"); fflush(NULL); -#ifndef WIN32 +#ifndef GIT_WINDOWS_NATIVE { int notify_pipe[2]; if (pipe(notify_pipe)) notify_pipe[0] = notify_pipe[1] = -1; cmd->pid = fork(); + failed_errno = errno; if (!cmd->pid) { /* * Redirect the channel to write syscall error messages to @@ -324,23 +406,12 @@ fail_pipe: unsetenv(*cmd->env); } } - if (cmd->preexec_cb) { - /* - * We cannot predict what the pre-exec callback does. - * Forgo parent notification. - */ - close(child_notifier); - child_notifier = -1; - - cmd->preexec_cb(); - } - if (cmd->git_cmd) { + if (cmd->git_cmd) execv_git_cmd(cmd->argv); - } else if (cmd->use_shell) { + else if (cmd->use_shell) execv_shell_cmd(cmd->argv); - } else { - execvp(cmd->argv[0], (char *const*) cmd->argv); - } + else + sane_execvp(cmd->argv[0], (char *const*) cmd->argv); if (errno == ENOENT) { if (!cmd->silent_exec_failure) error("cannot run %s: %s", cmd->argv[0], @@ -352,7 +423,7 @@ fail_pipe: } if (cmd->pid < 0) error("cannot fork() for %s: %s", cmd->argv[0], - strerror(failed_errno = errno)); + strerror(errno)); else if (cmd->clean_on_exit) mark_child_for_cleanup(cmd->pid); @@ -369,13 +440,11 @@ fail_pipe: * At this point we know that fork() succeeded, but execvp() * failed. Errors have been reported to our stderr. */ - wait_or_whine(cmd->pid, cmd->argv[0], - cmd->silent_exec_failure); + wait_or_whine(cmd->pid, cmd->argv[0]); failed_errno = errno; cmd->pid = -1; } close(notify_pipe[0]); - } #else { @@ -409,11 +478,10 @@ fail_pipe: if (cmd->env) env = make_augmented_environ(cmd->env); - if (cmd->git_cmd) { + if (cmd->git_cmd) cmd->argv = prepare_git_cmd(cmd->argv); - } else if (cmd->use_shell) { + else if (cmd->use_shell) cmd->argv = prepare_shell_cmd(cmd->argv); - } cmd->pid = mingw_spawnvpe(cmd->argv[0], cmd->argv, env, cmd->dir, fhin, fhout, fherr); @@ -475,7 +543,7 @@ fail_pipe: int finish_command(struct child_process *cmd) { - return wait_or_whine(cmd->pid, cmd->argv[0], cmd->silent_exec_failure); + return wait_or_whine(cmd->pid, cmd->argv[0]); } int run_command(struct child_process *cmd) @@ -520,6 +588,7 @@ int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const static pthread_t main_thread; static int main_thread_set; static pthread_key_t async_key; +static pthread_key_t async_die_counter; static void *run_thread(void *data) { @@ -546,6 +615,14 @@ static NORETURN void die_async(const char *err, va_list params) exit(128); } + +static int async_die_is_recursing(void) +{ + void *ret = pthread_getspecific(async_die_counter); + pthread_setspecific(async_die_counter, (void *)1); + return ret != NULL; +} + #endif int start_async(struct async *async) @@ -627,7 +704,9 @@ int start_async(struct async *async) main_thread_set = 1; main_thread = pthread_self(); pthread_key_create(&async_key, NULL); + pthread_key_create(&async_die_counter, NULL); set_die_routine(die_async); + set_die_is_recursing_routine(async_die_is_recursing); } if (proc_in >= 0) @@ -662,7 +741,7 @@ error: int finish_async(struct async *async) { #ifdef NO_PTHREADS - return wait_or_whine(async->pid, "child process", 0); + return wait_or_whine(async->pid, "child process"); #else void *ret = (void *)(intptr_t)(-1); @@ -672,6 +751,15 @@ int finish_async(struct async *async) #endif } +char *find_hook(const char *name) +{ + char *path = git_path("hooks/%s", name); + if (access(path, X_OK) < 0) + path = NULL; + + return path; +} + int run_hook(const char *index_file, const char *name, ...) { struct child_process hook; @@ -681,11 +769,13 @@ int run_hook(const char *index_file, const char *name, ...) va_list args; int ret; - if (access(git_path("hooks/%s", name), X_OK) < 0) + p = find_hook(name); + if (!p) return 0; + argv_array_push(&argv, p); + va_start(args, name); - argv_array_push(&argv, git_path("hooks/%s", name)); while ((p = va_arg(args, const char *))) argv_array_push(&argv, p); va_end(args); |