2 #include "run-command.h"
5 #include "argv-array.h"
8 # define SHELL_PATH "/bin/sh"
11 struct child_to_clean {
13 struct child_to_clean *next;
15 static struct child_to_clean *children_to_clean;
16 static int installed_child_cleanup_handler;
18 static void cleanup_children(int sig)
20 while (children_to_clean) {
21 struct child_to_clean *p = children_to_clean;
22 children_to_clean = p->next;
28 static void cleanup_children_on_signal(int sig)
30 cleanup_children(sig);
35 static void cleanup_children_on_exit(void)
37 cleanup_children(SIGTERM);
40 static void mark_child_for_cleanup(pid_t pid)
42 struct child_to_clean *p = xmalloc(sizeof(*p));
44 p->next = children_to_clean;
45 children_to_clean = p;
47 if (!installed_child_cleanup_handler) {
48 atexit(cleanup_children_on_exit);
49 sigchain_push_common(cleanup_children_on_signal);
50 installed_child_cleanup_handler = 1;
54 static void clear_child_for_cleanup(pid_t pid)
56 struct child_to_clean **last, *p;
58 last = &children_to_clean;
59 for (p = children_to_clean; p; p = p->next) {
68 static inline void close_pair(int fd[2])
75 static inline void dup_devnull(int to)
77 int fd = open("/dev/null", O_RDWR);
83 static const char **prepare_shell_cmd(const char **argv)
88 for (argc = 0; argv[argc]; argc++)
90 /* +1 for NULL, +3 for "sh -c" plus extra $0 */
91 nargv = xmalloc(sizeof(*nargv) * (argc + 1 + 3));
94 die("BUG: shell command is empty");
96 if (strcspn(argv[0], "|&;<>()$`\\\"' \t\n*?[#~=%") != strlen(argv[0])) {
98 nargv[nargc++] = SHELL_PATH;
100 nargv[nargc++] = "sh";
102 nargv[nargc++] = "-c";
105 nargv[nargc++] = argv[0];
107 struct strbuf arg0 = STRBUF_INIT;
108 strbuf_addf(&arg0, "%s \"$@\"", argv[0]);
109 nargv[nargc++] = strbuf_detach(&arg0, NULL);
113 for (argc = 0; argv[argc]; argc++)
114 nargv[nargc++] = argv[argc];
121 static int execv_shell_cmd(const char **argv)
123 const char **nargv = prepare_shell_cmd(argv);
124 trace_argv_printf(nargv, "trace: exec:");
125 execvp(nargv[0], (char **)nargv);
132 static int child_err = 2;
133 static int child_notifier = -1;
135 static void notify_parent(void)
138 * execvp failed. If possible, we'd like to let start_command
139 * know, so failures like ENOENT can be handled right away; but
140 * otherwise, finish_command will still report the error.
142 xwrite(child_notifier, "", 1);
145 static NORETURN void die_child(const char *err, va_list params)
147 vwritef(child_err, "fatal: ", err, params);
151 static void error_child(const char *err, va_list params)
153 vwritef(child_err, "error: ", err, params);
157 static inline void set_cloexec(int fd)
159 int flags = fcntl(fd, F_GETFD);
161 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
164 static int wait_or_whine(pid_t pid, const char *argv0, int silent_exec_failure)
166 int status, code = -1;
168 int failed_errno = 0;
170 while ((waiting = waitpid(pid, &status, 0)) < 0 && errno == EINTR)
174 failed_errno = errno;
175 error("waitpid for %s failed: %s", argv0, strerror(errno));
176 } else if (waiting != pid) {
177 error("waitpid is confused (%s)", argv0);
178 } else if (WIFSIGNALED(status)) {
179 code = WTERMSIG(status);
180 error("%s died of signal %d", argv0, code);
182 * This return value is chosen so that code & 0xff
183 * mimics the exit code that a POSIX shell would report for
184 * a program that died from this signal.
187 } else if (WIFEXITED(status)) {
188 code = WEXITSTATUS(status);
190 * Convert special exit code when execvp failed.
194 failed_errno = ENOENT;
197 error("waitpid is confused (%s)", argv0);
200 clear_child_for_cleanup(pid);
202 errno = failed_errno;
206 int start_command(struct child_process *cmd)
208 int need_in, need_out, need_err;
209 int fdin[2], fdout[2], fderr[2];
210 int failed_errno = failed_errno;
213 * In case of errors we must keep the promise to close FDs
214 * that have been passed in via ->in and ->out.
217 need_in = !cmd->no_stdin && cmd->in < 0;
219 if (pipe(fdin) < 0) {
220 failed_errno = errno;
228 need_out = !cmd->no_stdout
229 && !cmd->stdout_to_stderr
232 if (pipe(fdout) < 0) {
233 failed_errno = errno;
243 need_err = !cmd->no_stderr && cmd->err < 0;
245 if (pipe(fderr) < 0) {
246 failed_errno = errno;
256 error("cannot create pipe for %s: %s",
257 cmd->argv[0], strerror(failed_errno));
258 errno = failed_errno;
264 trace_argv_printf(cmd->argv, "trace: run_command:");
270 if (pipe(notify_pipe))
271 notify_pipe[0] = notify_pipe[1] = -1;
276 * Redirect the channel to write syscall error messages to
277 * before redirecting the process's stderr so that all die()
278 * in subsequent call paths use the parent's stderr.
280 if (cmd->no_stderr || need_err) {
282 set_cloexec(child_err);
284 set_die_routine(die_child);
285 set_error_routine(error_child);
287 close(notify_pipe[0]);
288 set_cloexec(notify_pipe[1]);
289 child_notifier = notify_pipe[1];
290 atexit(notify_parent);
297 } else if (cmd->in) {
307 } else if (cmd->err > 1) {
314 else if (cmd->stdout_to_stderr)
319 } else if (cmd->out > 1) {
324 if (cmd->dir && chdir(cmd->dir))
325 die_errno("exec '%s': cd to '%s' failed", cmd->argv[0],
328 for (; *cmd->env; cmd->env++) {
329 if (strchr(*cmd->env, '='))
330 putenv((char *)*cmd->env);
335 if (cmd->preexec_cb) {
337 * We cannot predict what the pre-exec callback does.
338 * Forgo parent notification.
340 close(child_notifier);
346 execv_git_cmd(cmd->argv);
347 } else if (cmd->use_shell) {
348 execv_shell_cmd(cmd->argv);
350 execvp(cmd->argv[0], (char *const*) cmd->argv);
352 if (errno == ENOENT) {
353 if (!cmd->silent_exec_failure)
354 error("cannot run %s: %s", cmd->argv[0],
358 die_errno("cannot exec '%s'", cmd->argv[0]);
362 error("cannot fork() for %s: %s", cmd->argv[0],
363 strerror(failed_errno = errno));
364 else if (cmd->clean_on_exit)
365 mark_child_for_cleanup(cmd->pid);
368 * Wait for child's execvp. If the execvp succeeds (or if fork()
369 * failed), EOF is seen immediately by the parent. Otherwise, the
370 * child process sends a single byte.
371 * Note that use of this infrastructure is completely advisory,
372 * therefore, we keep error checks minimal.
374 close(notify_pipe[1]);
375 if (read(notify_pipe[0], ¬ify_pipe[1], 1) == 1) {
377 * At this point we know that fork() succeeded, but execvp()
378 * failed. Errors have been reported to our stderr.
380 wait_or_whine(cmd->pid, cmd->argv[0],
381 cmd->silent_exec_failure);
382 failed_errno = errno;
385 close(notify_pipe[0]);
390 int fhin = 0, fhout = 1, fherr = 2;
391 const char **sargv = cmd->argv;
392 char **env = environ;
395 fhin = open("/dev/null", O_RDWR);
402 fherr = open("/dev/null", O_RDWR);
404 fherr = dup(fderr[1]);
405 else if (cmd->err > 2)
406 fherr = dup(cmd->err);
409 fhout = open("/dev/null", O_RDWR);
410 else if (cmd->stdout_to_stderr)
413 fhout = dup(fdout[1]);
414 else if (cmd->out > 1)
415 fhout = dup(cmd->out);
418 env = make_augmented_environ(cmd->env);
421 cmd->argv = prepare_git_cmd(cmd->argv);
422 } else if (cmd->use_shell) {
423 cmd->argv = prepare_shell_cmd(cmd->argv);
426 cmd->pid = mingw_spawnvpe(cmd->argv[0], cmd->argv, env, cmd->dir,
428 failed_errno = errno;
429 if (cmd->pid < 0 && (!cmd->silent_exec_failure || errno != ENOENT))
430 error("cannot spawn %s: %s", cmd->argv[0], strerror(errno));
431 if (cmd->clean_on_exit && cmd->pid >= 0)
432 mark_child_for_cleanup(cmd->pid);
462 errno = failed_errno;
484 int finish_command(struct child_process *cmd)
486 return wait_or_whine(cmd->pid, cmd->argv[0], cmd->silent_exec_failure);
489 int run_command(struct child_process *cmd)
491 int code = start_command(cmd);
494 return finish_command(cmd);
497 static void prepare_run_command_v_opt(struct child_process *cmd,
501 memset(cmd, 0, sizeof(*cmd));
503 cmd->no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
504 cmd->git_cmd = opt & RUN_GIT_CMD ? 1 : 0;
505 cmd->stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
506 cmd->silent_exec_failure = opt & RUN_SILENT_EXEC_FAILURE ? 1 : 0;
507 cmd->use_shell = opt & RUN_USING_SHELL ? 1 : 0;
508 cmd->clean_on_exit = opt & RUN_CLEAN_ON_EXIT ? 1 : 0;
511 int run_command_v_opt(const char **argv, int opt)
513 struct child_process cmd;
514 prepare_run_command_v_opt(&cmd, argv, opt);
515 return run_command(&cmd);
518 int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env)
520 struct child_process cmd;
521 prepare_run_command_v_opt(&cmd, argv, opt);
524 return run_command(&cmd);
528 static pthread_t main_thread;
529 static int main_thread_set;
530 static pthread_key_t async_key;
532 static void *run_thread(void *data)
534 struct async *async = data;
537 pthread_setspecific(async_key, async);
538 ret = async->proc(async->proc_in, async->proc_out, async->data);
542 static NORETURN void die_async(const char *err, va_list params)
544 vreportf("fatal: ", err, params);
546 if (!pthread_equal(main_thread, pthread_self())) {
547 struct async *async = pthread_getspecific(async_key);
548 if (async->proc_in >= 0)
549 close(async->proc_in);
550 if (async->proc_out >= 0)
551 close(async->proc_out);
552 pthread_exit((void *)128);
559 int start_async(struct async *async)
561 int need_in, need_out;
562 int fdin[2], fdout[2];
563 int proc_in, proc_out;
565 need_in = async->in < 0;
567 if (pipe(fdin) < 0) {
570 return error("cannot create pipe: %s", strerror(errno));
575 need_out = async->out < 0;
577 if (pipe(fdout) < 0) {
582 return error("cannot create pipe: %s", strerror(errno));
584 async->out = fdout[0];
597 proc_out = async->out;
602 /* Flush stdio before fork() to avoid cloning buffers */
606 if (async->pid < 0) {
607 error("fork (async) failed: %s", strerror(errno));
615 exit(!!async->proc(proc_in, proc_out, async->data));
618 mark_child_for_cleanup(async->pid);
630 if (!main_thread_set) {
632 * We assume that the first time that start_async is called
633 * it is from the main thread.
636 main_thread = pthread_self();
637 pthread_key_create(&async_key, NULL);
638 set_die_routine(die_async);
642 set_cloexec(proc_in);
644 set_cloexec(proc_out);
645 async->proc_in = proc_in;
646 async->proc_out = proc_out;
648 int err = pthread_create(&async->tid, NULL, run_thread, async);
650 error("cannot create thread: %s", strerror(err));
670 int finish_async(struct async *async)
673 return wait_or_whine(async->pid, "child process", 0);
675 void *ret = (void *)(intptr_t)(-1);
677 if (pthread_join(async->tid, &ret))
678 error("pthread_join failed");
679 return (int)(intptr_t)ret;
683 int run_hook(const char *index_file, const char *name, ...)
685 struct child_process hook;
686 struct argv_array argv = ARGV_ARRAY_INIT;
687 const char *p, *env[2];
688 char index[PATH_MAX];
692 if (access(git_path("hooks/%s", name), X_OK) < 0)
695 va_start(args, name);
696 argv_array_push(&argv, git_path("hooks/%s", name));
697 while ((p = va_arg(args, const char *)))
698 argv_array_push(&argv, p);
701 memset(&hook, 0, sizeof(hook));
702 hook.argv = argv.argv;
704 hook.stdout_to_stderr = 1;
706 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
712 ret = run_command(&hook);
713 argv_array_clear(&argv);