2 #include "run-command.h"
5 static inline void close_pair(int fd[2])
12 static inline void dup_devnull(int to)
14 int fd = open("/dev/null", O_RDWR);
20 static const char **prepare_shell_cmd(const char **argv)
25 for (argc = 0; argv[argc]; argc++)
27 /* +1 for NULL, +3 for "sh -c" plus extra $0 */
28 nargv = xmalloc(sizeof(*nargv) * (argc + 1 + 3));
31 die("BUG: shell command is empty");
33 if (strcspn(argv[0], "|&;<>()$`\\\"' \t\n*?[#~=%") != strlen(argv[0])) {
34 nargv[nargc++] = "sh";
35 nargv[nargc++] = "-c";
38 nargv[nargc++] = argv[0];
40 struct strbuf arg0 = STRBUF_INIT;
41 strbuf_addf(&arg0, "%s \"$@\"", argv[0]);
42 nargv[nargc++] = strbuf_detach(&arg0, NULL);
46 for (argc = 0; argv[argc]; argc++)
47 nargv[nargc++] = argv[argc];
54 static int execv_shell_cmd(const char **argv)
56 const char **nargv = prepare_shell_cmd(argv);
57 trace_argv_printf(nargv, "trace: exec:");
58 execvp(nargv[0], (char **)nargv);
65 static int child_err = 2;
66 static int child_notifier = -1;
68 static void notify_parent(void)
71 * execvp failed. If possible, we'd like to let start_command
72 * know, so failures like ENOENT can be handled right away; but
73 * otherwise, finish_command will still report the error.
75 xwrite(child_notifier, "", 1);
78 static NORETURN void die_child(const char *err, va_list params)
80 vwritef(child_err, "fatal: ", err, params);
84 static void error_child(const char *err, va_list params)
86 vwritef(child_err, "error: ", err, params);
90 static inline void set_cloexec(int fd)
92 int flags = fcntl(fd, F_GETFD);
94 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
97 static int wait_or_whine(pid_t pid, const char *argv0, int silent_exec_failure)
99 int status, code = -1;
101 int failed_errno = 0;
103 while ((waiting = waitpid(pid, &status, 0)) < 0 && errno == EINTR)
107 failed_errno = errno;
108 error("waitpid for %s failed: %s", argv0, strerror(errno));
109 } else if (waiting != pid) {
110 error("waitpid is confused (%s)", argv0);
111 } else if (WIFSIGNALED(status)) {
112 code = WTERMSIG(status);
113 error("%s died of signal %d", argv0, code);
115 * This return value is chosen so that code & 0xff
116 * mimics the exit code that a POSIX shell would report for
117 * a program that died from this signal.
120 } else if (WIFEXITED(status)) {
121 code = WEXITSTATUS(status);
123 * Convert special exit code when execvp failed.
127 failed_errno = ENOENT;
130 error("waitpid is confused (%s)", argv0);
132 errno = failed_errno;
136 int start_command(struct child_process *cmd)
138 int need_in, need_out, need_err;
139 int fdin[2], fdout[2], fderr[2];
140 int failed_errno = failed_errno;
143 * In case of errors we must keep the promise to close FDs
144 * that have been passed in via ->in and ->out.
147 need_in = !cmd->no_stdin && cmd->in < 0;
149 if (pipe(fdin) < 0) {
150 failed_errno = errno;
158 need_out = !cmd->no_stdout
159 && !cmd->stdout_to_stderr
162 if (pipe(fdout) < 0) {
163 failed_errno = errno;
173 need_err = !cmd->no_stderr && cmd->err < 0;
175 if (pipe(fderr) < 0) {
176 failed_errno = errno;
186 error("cannot create pipe for %s: %s",
187 cmd->argv[0], strerror(failed_errno));
188 errno = failed_errno;
194 trace_argv_printf(cmd->argv, "trace: run_command:");
200 if (pipe(notify_pipe))
201 notify_pipe[0] = notify_pipe[1] = -1;
206 * Redirect the channel to write syscall error messages to
207 * before redirecting the process's stderr so that all die()
208 * in subsequent call paths use the parent's stderr.
210 if (cmd->no_stderr || need_err) {
212 set_cloexec(child_err);
214 set_die_routine(die_child);
215 set_error_routine(error_child);
217 close(notify_pipe[0]);
218 set_cloexec(notify_pipe[1]);
219 child_notifier = notify_pipe[1];
220 atexit(notify_parent);
227 } else if (cmd->in) {
237 } else if (cmd->err > 1) {
244 else if (cmd->stdout_to_stderr)
249 } else if (cmd->out > 1) {
254 if (cmd->dir && chdir(cmd->dir))
255 die_errno("exec '%s': cd to '%s' failed", cmd->argv[0],
258 for (; *cmd->env; cmd->env++) {
259 if (strchr(*cmd->env, '='))
260 putenv((char *)*cmd->env);
265 if (cmd->preexec_cb) {
267 * We cannot predict what the pre-exec callback does.
268 * Forgo parent notification.
270 close(child_notifier);
276 execv_git_cmd(cmd->argv);
277 } else if (cmd->use_shell) {
278 execv_shell_cmd(cmd->argv);
280 execvp(cmd->argv[0], (char *const*) cmd->argv);
282 if (errno == ENOENT) {
283 if (!cmd->silent_exec_failure)
284 error("cannot run %s: %s", cmd->argv[0],
288 die_errno("cannot exec '%s'", cmd->argv[0]);
292 error("cannot fork() for %s: %s", cmd->argv[0],
293 strerror(failed_errno = errno));
296 * Wait for child's execvp. If the execvp succeeds (or if fork()
297 * failed), EOF is seen immediately by the parent. Otherwise, the
298 * child process sends a single byte.
299 * Note that use of this infrastructure is completely advisory,
300 * therefore, we keep error checks minimal.
302 close(notify_pipe[1]);
303 if (read(notify_pipe[0], ¬ify_pipe[1], 1) == 1) {
305 * At this point we know that fork() succeeded, but execvp()
306 * failed. Errors have been reported to our stderr.
308 wait_or_whine(cmd->pid, cmd->argv[0],
309 cmd->silent_exec_failure);
310 failed_errno = errno;
313 close(notify_pipe[0]);
317 int fhin = 0, fhout = 1, fherr = 2;
318 const char **sargv = cmd->argv;
319 char **env = environ;
322 fhin = open("/dev/null", O_RDWR);
329 fherr = open("/dev/null", O_RDWR);
331 fherr = dup(fderr[1]);
332 else if (cmd->err > 2)
333 fherr = dup(cmd->err);
336 fhout = open("/dev/null", O_RDWR);
337 else if (cmd->stdout_to_stderr)
340 fhout = dup(fdout[1]);
341 else if (cmd->out > 1)
342 fhout = dup(cmd->out);
345 env = make_augmented_environ(cmd->env);
348 cmd->argv = prepare_git_cmd(cmd->argv);
349 } else if (cmd->use_shell) {
350 cmd->argv = prepare_shell_cmd(cmd->argv);
353 cmd->pid = mingw_spawnvpe(cmd->argv[0], cmd->argv, env, cmd->dir,
355 failed_errno = errno;
356 if (cmd->pid < 0 && (!cmd->silent_exec_failure || errno != ENOENT))
357 error("cannot spawn %s: %s", cmd->argv[0], strerror(errno));
387 errno = failed_errno;
409 int finish_command(struct child_process *cmd)
411 return wait_or_whine(cmd->pid, cmd->argv[0], cmd->silent_exec_failure);
414 int run_command(struct child_process *cmd)
416 int code = start_command(cmd);
419 return finish_command(cmd);
422 static void prepare_run_command_v_opt(struct child_process *cmd,
426 memset(cmd, 0, sizeof(*cmd));
428 cmd->no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
429 cmd->git_cmd = opt & RUN_GIT_CMD ? 1 : 0;
430 cmd->stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
431 cmd->silent_exec_failure = opt & RUN_SILENT_EXEC_FAILURE ? 1 : 0;
432 cmd->use_shell = opt & RUN_USING_SHELL ? 1 : 0;
435 int run_command_v_opt(const char **argv, int opt)
437 struct child_process cmd;
438 prepare_run_command_v_opt(&cmd, argv, opt);
439 return run_command(&cmd);
442 int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env)
444 struct child_process cmd;
445 prepare_run_command_v_opt(&cmd, argv, opt);
448 return run_command(&cmd);
452 static pthread_t main_thread;
453 static int main_thread_set;
454 static pthread_key_t async_key;
456 static void *run_thread(void *data)
458 struct async *async = data;
461 pthread_setspecific(async_key, async);
462 ret = async->proc(async->proc_in, async->proc_out, async->data);
466 static NORETURN void die_async(const char *err, va_list params)
468 vreportf("fatal: ", err, params);
470 if (!pthread_equal(main_thread, pthread_self())) {
471 struct async *async = pthread_getspecific(async_key);
472 if (async->proc_in >= 0)
473 close(async->proc_in);
474 if (async->proc_out >= 0)
475 close(async->proc_out);
476 pthread_exit((void *)128);
483 int start_async(struct async *async)
485 int need_in, need_out;
486 int fdin[2], fdout[2];
487 int proc_in, proc_out;
489 need_in = async->in < 0;
491 if (pipe(fdin) < 0) {
494 return error("cannot create pipe: %s", strerror(errno));
499 need_out = async->out < 0;
501 if (pipe(fdout) < 0) {
506 return error("cannot create pipe: %s", strerror(errno));
508 async->out = fdout[0];
521 proc_out = async->out;
526 /* Flush stdio before fork() to avoid cloning buffers */
530 if (async->pid < 0) {
531 error("fork (async) failed: %s", strerror(errno));
539 exit(!!async->proc(proc_in, proc_out, async->data));
552 if (!main_thread_set) {
554 * We assume that the first time that start_async is called
555 * it is from the main thread.
558 main_thread = pthread_self();
559 pthread_key_create(&async_key, NULL);
560 set_die_routine(die_async);
564 set_cloexec(proc_in);
566 set_cloexec(proc_out);
567 async->proc_in = proc_in;
568 async->proc_out = proc_out;
570 int err = pthread_create(&async->tid, NULL, run_thread, async);
572 error("cannot create thread: %s", strerror(err));
592 int finish_async(struct async *async)
595 return wait_or_whine(async->pid, "child process", 0);
597 void *ret = (void *)(intptr_t)(-1);
599 if (pthread_join(async->tid, &ret))
600 error("pthread_join failed");
601 return (int)(intptr_t)ret;
605 int run_hook(const char *index_file, const char *name, ...)
607 struct child_process hook;
608 const char **argv = NULL, *env[2];
609 char index[PATH_MAX];
612 size_t i = 0, alloc = 0;
614 if (access(git_path("hooks/%s", name), X_OK) < 0)
617 va_start(args, name);
618 ALLOC_GROW(argv, i + 1, alloc);
619 argv[i++] = git_path("hooks/%s", name);
621 ALLOC_GROW(argv, i + 1, alloc);
622 argv[i++] = va_arg(args, const char *);
626 memset(&hook, 0, sizeof(hook));
629 hook.stdout_to_stderr = 1;
631 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
637 ret = run_command(&hook);