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);
18 static const char **prepare_shell_cmd(const char **argv)
23 for (argc = 0; argv[argc]; argc++)
25 /* +1 for NULL, +3 for "sh -c" plus extra $0 */
26 nargv = xmalloc(sizeof(*nargv) * (argc + 1 + 3));
29 die("BUG: shell command is empty");
31 if (strcspn(argv[0], "|&;<>()$`\\\"' \t\n*?[#~=%") != strlen(argv[0])) {
32 nargv[nargc++] = "sh";
33 nargv[nargc++] = "-c";
36 nargv[nargc++] = argv[0];
38 struct strbuf arg0 = STRBUF_INIT;
39 strbuf_addf(&arg0, "%s \"$@\"", argv[0]);
40 nargv[nargc++] = strbuf_detach(&arg0, NULL);
44 for (argc = 0; argv[argc]; argc++)
45 nargv[nargc++] = argv[argc];
52 static int execv_shell_cmd(const char **argv)
54 const char **nargv = prepare_shell_cmd(argv);
55 trace_argv_printf(nargv, "trace: exec:");
56 execvp(nargv[0], (char **)nargv);
62 int start_command(struct child_process *cmd)
64 int need_in, need_out, need_err;
65 int fdin[2], fdout[2], fderr[2];
66 int failed_errno = failed_errno;
69 * In case of errors we must keep the promise to close FDs
70 * that have been passed in via ->in and ->out.
73 need_in = !cmd->no_stdin && cmd->in < 0;
84 need_out = !cmd->no_stdout
85 && !cmd->stdout_to_stderr
88 if (pipe(fdout) < 0) {
99 need_err = !cmd->no_stderr && cmd->err < 0;
101 if (pipe(fderr) < 0) {
102 failed_errno = errno;
112 error("cannot create pipe for %s: %s",
113 cmd->argv[0], strerror(failed_errno));
114 errno = failed_errno;
120 trace_argv_printf(cmd->argv, "trace: run_command:");
131 } else if (cmd->in) {
145 else if (cmd->stdout_to_stderr)
150 } else if (cmd->out > 1) {
155 if (cmd->dir && chdir(cmd->dir))
156 die_errno("exec '%s': cd to '%s' failed", cmd->argv[0],
159 for (; *cmd->env; cmd->env++) {
160 if (strchr(*cmd->env, '='))
161 putenv((char *)*cmd->env);
169 execv_git_cmd(cmd->argv);
170 } else if (cmd->use_shell) {
171 execv_shell_cmd(cmd->argv);
173 execvp(cmd->argv[0], (char *const*) cmd->argv);
175 trace_printf("trace: exec '%s' failed: %s\n", cmd->argv[0],
180 error("cannot fork() for %s: %s", cmd->argv[0],
181 strerror(failed_errno = errno));
184 int s0 = -1, s1 = -1, s2 = -1; /* backups of stdin, stdout, stderr */
185 const char **sargv = cmd->argv;
186 char **env = environ;
191 } else if (need_in) {
194 } else if (cmd->in) {
199 if (cmd->no_stderr) {
202 } else if (need_err) {
207 if (cmd->no_stdout) {
210 } else if (cmd->stdout_to_stderr) {
213 } else if (need_out) {
216 } else if (cmd->out > 1) {
222 die("chdir in start_command() not implemented");
224 env = make_augmented_environ(cmd->env);
227 cmd->argv = prepare_git_cmd(cmd->argv);
228 } else if (cmd->use_shell) {
229 cmd->argv = prepare_shell_cmd(cmd->argv);
232 cmd->pid = mingw_spawnvpe(cmd->argv[0], cmd->argv, env);
233 failed_errno = errno;
234 if (cmd->pid < 0 && (!cmd->silent_exec_failure || errno != ENOENT))
235 error("cannot spawn %s: %s", cmd->argv[0], strerror(errno));
244 dup2(s0, 0), close(s0);
246 dup2(s1, 1), close(s1);
248 dup2(s2, 2), close(s2);
263 errno = failed_errno;
283 static int wait_or_whine(pid_t pid, const char *argv0, int silent_exec_failure)
285 int status, code = -1;
287 int failed_errno = 0;
289 while ((waiting = waitpid(pid, &status, 0)) < 0 && errno == EINTR)
293 failed_errno = errno;
294 error("waitpid for %s failed: %s", argv0, strerror(errno));
295 } else if (waiting != pid) {
296 error("waitpid is confused (%s)", argv0);
297 } else if (WIFSIGNALED(status)) {
298 code = WTERMSIG(status);
299 error("%s died of signal %d", argv0, code);
301 * This return value is chosen so that code & 0xff
302 * mimics the exit code that a POSIX shell would report for
303 * a program that died from this signal.
306 } else if (WIFEXITED(status)) {
307 code = WEXITSTATUS(status);
309 * Convert special exit code when execvp failed.
313 failed_errno = ENOENT;
314 if (!silent_exec_failure)
315 error("cannot run %s: %s", argv0,
319 error("waitpid is confused (%s)", argv0);
321 errno = failed_errno;
325 int finish_command(struct child_process *cmd)
327 return wait_or_whine(cmd->pid, cmd->argv[0], cmd->silent_exec_failure);
330 int run_command(struct child_process *cmd)
332 int code = start_command(cmd);
335 return finish_command(cmd);
338 static void prepare_run_command_v_opt(struct child_process *cmd,
342 memset(cmd, 0, sizeof(*cmd));
344 cmd->no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
345 cmd->git_cmd = opt & RUN_GIT_CMD ? 1 : 0;
346 cmd->stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
347 cmd->silent_exec_failure = opt & RUN_SILENT_EXEC_FAILURE ? 1 : 0;
348 cmd->use_shell = opt & RUN_USING_SHELL ? 1 : 0;
351 int run_command_v_opt(const char **argv, int opt)
353 struct child_process cmd;
354 prepare_run_command_v_opt(&cmd, argv, opt);
355 return run_command(&cmd);
358 int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env)
360 struct child_process cmd;
361 prepare_run_command_v_opt(&cmd, argv, opt);
364 return run_command(&cmd);
368 static unsigned __stdcall run_thread(void *data)
370 struct async *async = data;
371 return async->proc(async->fd_for_proc, async->data);
375 int start_async(struct async *async)
379 if (pipe(pipe_out) < 0)
380 return error("cannot create pipe: %s", strerror(errno));
381 async->out = pipe_out[0];
384 /* Flush stdio before fork() to avoid cloning buffers */
388 if (async->pid < 0) {
389 error("fork (async) failed: %s", strerror(errno));
390 close_pair(pipe_out);
395 exit(!!async->proc(pipe_out[1], async->data));
399 async->fd_for_proc = pipe_out[1];
400 async->tid = (HANDLE) _beginthreadex(NULL, 0, run_thread, async, 0, NULL);
402 error("cannot create thread: %s", strerror(errno));
403 close_pair(pipe_out);
410 int finish_async(struct async *async)
413 int ret = wait_or_whine(async->pid, "child process", 0);
416 if (WaitForSingleObject(async->tid, INFINITE) != WAIT_OBJECT_0)
417 ret = error("waiting for thread failed: %lu", GetLastError());
418 else if (!GetExitCodeThread(async->tid, &ret))
419 ret = error("cannot get thread exit code: %lu", GetLastError());
420 CloseHandle(async->tid);
425 int run_hook(const char *index_file, const char *name, ...)
427 struct child_process hook;
428 const char **argv = NULL, *env[2];
429 char index[PATH_MAX];
432 size_t i = 0, alloc = 0;
434 if (access(git_path("hooks/%s", name), X_OK) < 0)
437 va_start(args, name);
438 ALLOC_GROW(argv, i + 1, alloc);
439 argv[i++] = git_path("hooks/%s", name);
441 ALLOC_GROW(argv, i + 1, alloc);
442 argv[i++] = va_arg(args, const char *);
446 memset(&hook, 0, sizeof(hook));
449 hook.stdout_to_stderr = 1;
451 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
457 ret = run_command(&hook);