2 #include "run-command.h"
5 static inline void close_pair(int fd[2])
11 static inline void dup_devnull(int to)
13 int fd = open("/dev/null", O_RDWR);
19 static int child_err = 2;
21 static NORETURN void die_child(const char *err, va_list params)
24 int len = vsnprintf(msg, sizeof(msg), err, params);
25 if (len > sizeof(msg))
28 write(child_err, "fatal: ", 7);
29 write(child_err, msg, len);
30 write(child_err, "\n", 1);
34 static inline void set_cloexec(int fd)
36 int flags = fcntl(fd, F_GETFD);
38 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
42 int start_command(struct child_process *cmd)
44 int need_in, need_out, need_err;
45 int fdin[2], fdout[2], fderr[2];
46 int failed_errno = failed_errno;
49 * In case of errors we must keep the promise to close FDs
50 * that have been passed in via ->in and ->out.
53 need_in = !cmd->no_stdin && cmd->in < 0;
64 need_out = !cmd->no_stdout
65 && !cmd->stdout_to_stderr
68 if (pipe(fdout) < 0) {
79 need_err = !cmd->no_stderr && cmd->err < 0;
81 if (pipe(fderr) < 0) {
92 error("cannot create pipe for %s: %s",
93 cmd->argv[0], strerror(failed_errno));
100 trace_argv_printf(cmd->argv, "trace: run_command:");
107 * Redirect the channel to write syscall error messages to
108 * before redirecting the process's stderr so that all die()
109 * in subsequent call paths use the parent's stderr.
111 if (cmd->no_stderr || need_err) {
113 set_cloexec(child_err);
115 set_die_routine(die_child);
122 } else if (cmd->in) {
136 else if (cmd->stdout_to_stderr)
141 } else if (cmd->out > 1) {
146 if (cmd->dir && chdir(cmd->dir))
147 die_errno("exec '%s': cd to '%s' failed", cmd->argv[0],
150 for (; *cmd->env; cmd->env++) {
151 if (strchr(*cmd->env, '='))
152 putenv((char *)*cmd->env);
160 execv_git_cmd(cmd->argv);
162 execvp(cmd->argv[0], (char *const*) cmd->argv);
165 * Do not check for cmd->silent_exec_failure; the parent
166 * process will check it when it sees this exit code.
171 die_errno("cannot exec '%s'", cmd->argv[0]);
174 error("cannot fork() for %s: %s", cmd->argv[0],
175 strerror(failed_errno = errno));
178 int s0 = -1, s1 = -1, s2 = -1; /* backups of stdin, stdout, stderr */
179 const char **sargv = cmd->argv;
180 char **env = environ;
185 } else if (need_in) {
188 } else if (cmd->in) {
193 if (cmd->no_stderr) {
196 } else if (need_err) {
201 if (cmd->no_stdout) {
204 } else if (cmd->stdout_to_stderr) {
207 } else if (need_out) {
210 } else if (cmd->out > 1) {
216 die("chdir in start_command() not implemented");
218 env = make_augmented_environ(cmd->env);
221 cmd->argv = prepare_git_cmd(cmd->argv);
224 cmd->pid = mingw_spawnvpe(cmd->argv[0], cmd->argv, env);
225 failed_errno = errno;
226 if (cmd->pid < 0 && (!cmd->silent_exec_failure || errno != ENOENT))
227 error("cannot spawn %s: %s", cmd->argv[0], strerror(errno));
236 dup2(s0, 0), close(s0);
238 dup2(s1, 1), close(s1);
240 dup2(s2, 2), close(s2);
255 errno = failed_errno;
275 static int wait_or_whine(pid_t pid, const char *argv0, int silent_exec_failure)
277 int status, code = -1;
279 int failed_errno = 0;
281 while ((waiting = waitpid(pid, &status, 0)) < 0 && errno == EINTR)
285 failed_errno = errno;
286 error("waitpid for %s failed: %s", argv0, strerror(errno));
287 } else if (waiting != pid) {
288 error("waitpid is confused (%s)", argv0);
289 } else if (WIFSIGNALED(status)) {
290 code = WTERMSIG(status);
291 error("%s died of signal %d", argv0, code);
293 * This return value is chosen so that code & 0xff
294 * mimics the exit code that a POSIX shell would report for
295 * a program that died from this signal.
298 } else if (WIFEXITED(status)) {
299 code = WEXITSTATUS(status);
301 * Convert special exit code when execvp failed.
305 failed_errno = ENOENT;
306 if (!silent_exec_failure)
307 error("cannot run %s: %s", argv0,
311 error("waitpid is confused (%s)", argv0);
313 errno = failed_errno;
317 int finish_command(struct child_process *cmd)
319 return wait_or_whine(cmd->pid, cmd->argv[0], cmd->silent_exec_failure);
322 int run_command(struct child_process *cmd)
324 int code = start_command(cmd);
327 return finish_command(cmd);
330 static void prepare_run_command_v_opt(struct child_process *cmd,
334 memset(cmd, 0, sizeof(*cmd));
336 cmd->no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
337 cmd->git_cmd = opt & RUN_GIT_CMD ? 1 : 0;
338 cmd->stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
339 cmd->silent_exec_failure = opt & RUN_SILENT_EXEC_FAILURE ? 1 : 0;
342 int run_command_v_opt(const char **argv, int opt)
344 struct child_process cmd;
345 prepare_run_command_v_opt(&cmd, argv, opt);
346 return run_command(&cmd);
349 int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env)
351 struct child_process cmd;
352 prepare_run_command_v_opt(&cmd, argv, opt);
355 return run_command(&cmd);
359 static unsigned __stdcall run_thread(void *data)
361 struct async *async = data;
362 return async->proc(async->fd_for_proc, async->data);
366 int start_async(struct async *async)
370 if (pipe(pipe_out) < 0)
371 return error("cannot create pipe: %s", strerror(errno));
372 async->out = pipe_out[0];
375 /* Flush stdio before fork() to avoid cloning buffers */
379 if (async->pid < 0) {
380 error("fork (async) failed: %s", strerror(errno));
381 close_pair(pipe_out);
386 exit(!!async->proc(pipe_out[1], async->data));
390 async->fd_for_proc = pipe_out[1];
391 async->tid = (HANDLE) _beginthreadex(NULL, 0, run_thread, async, 0, NULL);
393 error("cannot create thread: %s", strerror(errno));
394 close_pair(pipe_out);
401 int finish_async(struct async *async)
404 int ret = wait_or_whine(async->pid, "child process", 0);
407 if (WaitForSingleObject(async->tid, INFINITE) != WAIT_OBJECT_0)
408 ret = error("waiting for thread failed: %lu", GetLastError());
409 else if (!GetExitCodeThread(async->tid, &ret))
410 ret = error("cannot get thread exit code: %lu", GetLastError());
411 CloseHandle(async->tid);
416 int run_hook(const char *index_file, const char *name, ...)
418 struct child_process hook;
419 const char **argv = NULL, *env[2];
420 char index[PATH_MAX];
423 size_t i = 0, alloc = 0;
425 if (access(git_path("hooks/%s", name), X_OK) < 0)
428 va_start(args, name);
429 ALLOC_GROW(argv, i + 1, alloc);
430 argv[i++] = git_path("hooks/%s", name);
432 ALLOC_GROW(argv, i + 1, alloc);
433 argv[i++] = va_arg(args, const char *);
437 memset(&hook, 0, sizeof(hook));
440 hook.stdout_to_stderr = 1;
442 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
448 ret = run_command(&hook);