diff options
Diffstat (limited to 'run-command.c')
-rw-r--r-- | run-command.c | 71 |
1 files changed, 49 insertions, 22 deletions
diff --git a/run-command.c b/run-command.c index 757f263cd6..3914d9c511 100644 --- a/run-command.c +++ b/run-command.c @@ -72,11 +72,14 @@ 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 @@ -159,7 +162,7 @@ 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 WIN32 +#ifndef GIT_WINDOWS_NATIVE nargv[nargc++] = SHELL_PATH; #else nargv[nargc++] = "sh"; @@ -182,7 +185,7 @@ 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); @@ -193,7 +196,7 @@ static int execv_shell_cmd(const char **argv) } #endif -#ifndef WIN32 +#ifndef GIT_WINDOWS_NATIVE static int child_err = 2; static int child_notifier = -1; @@ -249,7 +252,7 @@ static int wait_or_whine(pid_t pid, const char *argv0) * 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); /* @@ -273,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 @@ -286,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]; @@ -301,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]; @@ -318,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; } @@ -330,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 @@ -398,13 +406,12 @@ fail_pipe: unsetenv(*cmd->env); } } - 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 { + 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], @@ -416,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); @@ -438,7 +445,6 @@ fail_pipe: cmd->pid = -1; } close(notify_pipe[0]); - } #else { @@ -472,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); @@ -583,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) { @@ -609,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) @@ -690,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) @@ -725,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); @@ -735,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; @@ -744,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); |