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;
20 static int child_notifier = -1;
22 static void notify_parent(void)
24 write(child_notifier, "", 1);
27 static NORETURN void die_child(const char *err, va_list params)
30 int len = vsnprintf(msg, sizeof(msg), err, params);
31 if (len > sizeof(msg))
34 write(child_err, "fatal: ", 7);
35 write(child_err, msg, len);
36 write(child_err, "\n", 1);
40 static inline void set_cloexec(int fd)
42 int flags = fcntl(fd, F_GETFD);
44 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
48 static int wait_or_whine(pid_t pid, const char *argv0, int silent_exec_failure)
50 int status, code = -1;
54 while ((waiting = waitpid(pid, &status, 0)) < 0 && errno == EINTR)
59 error("waitpid for %s failed: %s", argv0, strerror(errno));
60 } else if (waiting != pid) {
61 error("waitpid is confused (%s)", argv0);
62 } else if (WIFSIGNALED(status)) {
63 code = WTERMSIG(status);
64 error("%s died of signal %d", argv0, code);
66 * This return value is chosen so that code & 0xff
67 * mimics the exit code that a POSIX shell would report for
68 * a program that died from this signal.
71 } else if (WIFEXITED(status)) {
72 code = WEXITSTATUS(status);
74 * Convert special exit code when execvp failed.
78 failed_errno = ENOENT;
79 if (!silent_exec_failure)
80 error("cannot run %s: %s", argv0,
84 error("waitpid is confused (%s)", argv0);
90 int start_command(struct child_process *cmd)
92 int need_in, need_out, need_err;
93 int fdin[2], fdout[2], fderr[2];
94 int failed_errno = failed_errno;
97 * In case of errors we must keep the promise to close FDs
98 * that have been passed in via ->in and ->out.
101 need_in = !cmd->no_stdin && cmd->in < 0;
103 if (pipe(fdin) < 0) {
104 failed_errno = errno;
112 need_out = !cmd->no_stdout
113 && !cmd->stdout_to_stderr
116 if (pipe(fdout) < 0) {
117 failed_errno = errno;
127 need_err = !cmd->no_stderr && cmd->err < 0;
129 if (pipe(fderr) < 0) {
130 failed_errno = errno;
140 error("cannot create pipe for %s: %s",
141 cmd->argv[0], strerror(failed_errno));
142 errno = failed_errno;
148 trace_argv_printf(cmd->argv, "trace: run_command:");
153 if (pipe(notify_pipe))
154 notify_pipe[0] = notify_pipe[1] = -1;
160 * Redirect the channel to write syscall error messages to
161 * before redirecting the process's stderr so that all die()
162 * in subsequent call paths use the parent's stderr.
164 if (cmd->no_stderr || need_err) {
166 set_cloexec(child_err);
168 set_die_routine(die_child);
170 close(notify_pipe[0]);
171 set_cloexec(notify_pipe[1]);
172 child_notifier = notify_pipe[1];
173 atexit(notify_parent);
180 } else if (cmd->in) {
194 else if (cmd->stdout_to_stderr)
199 } else if (cmd->out > 1) {
204 if (cmd->dir && chdir(cmd->dir))
205 die_errno("exec '%s': cd to '%s' failed", cmd->argv[0],
208 for (; *cmd->env; cmd->env++) {
209 if (strchr(*cmd->env, '='))
210 putenv((char *)*cmd->env);
215 if (cmd->preexec_cb) {
217 * We cannot predict what the pre-exec callback does.
218 * Forgo parent notification.
220 close(child_notifier);
226 execv_git_cmd(cmd->argv);
228 execvp(cmd->argv[0], (char *const*) cmd->argv);
231 * Do not check for cmd->silent_exec_failure; the parent
232 * process will check it when it sees this exit code.
237 die_errno("cannot exec '%s'", cmd->argv[0]);
240 error("cannot fork() for %s: %s", cmd->argv[0],
241 strerror(failed_errno = errno));
244 * Wait for child's execvp. If the execvp succeeds (or if fork()
245 * failed), EOF is seen immediately by the parent. Otherwise, the
246 * child process sends a single byte.
247 * Note that use of this infrastructure is completely advisory,
248 * therefore, we keep error checks minimal.
250 close(notify_pipe[1]);
251 if (read(notify_pipe[0], ¬ify_pipe[1], 1) == 1) {
253 * At this point we know that fork() succeeded, but execvp()
254 * failed. Errors have been reported to our stderr.
256 wait_or_whine(cmd->pid, cmd->argv[0],
257 cmd->silent_exec_failure);
258 failed_errno = errno;
261 close(notify_pipe[0]);
265 int s0 = -1, s1 = -1, s2 = -1; /* backups of stdin, stdout, stderr */
266 const char **sargv = cmd->argv;
267 char **env = environ;
272 } else if (need_in) {
275 } else if (cmd->in) {
280 if (cmd->no_stderr) {
283 } else if (need_err) {
288 if (cmd->no_stdout) {
291 } else if (cmd->stdout_to_stderr) {
294 } else if (need_out) {
297 } else if (cmd->out > 1) {
303 die("chdir in start_command() not implemented");
305 env = make_augmented_environ(cmd->env);
308 cmd->argv = prepare_git_cmd(cmd->argv);
311 cmd->pid = mingw_spawnvpe(cmd->argv[0], cmd->argv, env);
312 failed_errno = errno;
313 if (cmd->pid < 0 && (!cmd->silent_exec_failure || errno != ENOENT))
314 error("cannot spawn %s: %s", cmd->argv[0], strerror(errno));
323 dup2(s0, 0), close(s0);
325 dup2(s1, 1), close(s1);
327 dup2(s2, 2), close(s2);
342 errno = failed_errno;
362 int finish_command(struct child_process *cmd)
364 return wait_or_whine(cmd->pid, cmd->argv[0], cmd->silent_exec_failure);
367 int run_command(struct child_process *cmd)
369 int code = start_command(cmd);
372 return finish_command(cmd);
375 static void prepare_run_command_v_opt(struct child_process *cmd,
379 memset(cmd, 0, sizeof(*cmd));
381 cmd->no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
382 cmd->git_cmd = opt & RUN_GIT_CMD ? 1 : 0;
383 cmd->stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
384 cmd->silent_exec_failure = opt & RUN_SILENT_EXEC_FAILURE ? 1 : 0;
387 int run_command_v_opt(const char **argv, int opt)
389 struct child_process cmd;
390 prepare_run_command_v_opt(&cmd, argv, opt);
391 return run_command(&cmd);
394 int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env)
396 struct child_process cmd;
397 prepare_run_command_v_opt(&cmd, argv, opt);
400 return run_command(&cmd);
404 static unsigned __stdcall run_thread(void *data)
406 struct async *async = data;
407 return async->proc(async->fd_for_proc, async->data);
411 int start_async(struct async *async)
415 if (pipe(pipe_out) < 0)
416 return error("cannot create pipe: %s", strerror(errno));
417 async->out = pipe_out[0];
420 /* Flush stdio before fork() to avoid cloning buffers */
424 if (async->pid < 0) {
425 error("fork (async) failed: %s", strerror(errno));
426 close_pair(pipe_out);
431 exit(!!async->proc(pipe_out[1], async->data));
435 async->fd_for_proc = pipe_out[1];
436 async->tid = (HANDLE) _beginthreadex(NULL, 0, run_thread, async, 0, NULL);
438 error("cannot create thread: %s", strerror(errno));
439 close_pair(pipe_out);
446 int finish_async(struct async *async)
449 int ret = wait_or_whine(async->pid, "child process", 0);
452 if (WaitForSingleObject(async->tid, INFINITE) != WAIT_OBJECT_0)
453 ret = error("waiting for thread failed: %lu", GetLastError());
454 else if (!GetExitCodeThread(async->tid, &ret))
455 ret = error("cannot get thread exit code: %lu", GetLastError());
456 CloseHandle(async->tid);
461 int run_hook(const char *index_file, const char *name, ...)
463 struct child_process hook;
464 const char **argv = NULL, *env[2];
465 char index[PATH_MAX];
468 size_t i = 0, alloc = 0;
470 if (access(git_path("hooks/%s", name), X_OK) < 0)
473 va_start(args, name);
474 ALLOC_GROW(argv, i + 1, alloc);
475 argv[i++] = git_path("hooks/%s", name);
477 ALLOC_GROW(argv, i + 1, alloc);
478 argv[i++] = va_arg(args, const char *);
482 memset(&hook, 0, sizeof(hook));
485 hook.stdout_to_stderr = 1;
487 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
493 ret = run_command(&hook);