2 #include "run-command.h"
5 #include "argv-array.h"
7 struct child_to_clean {
9 struct child_to_clean *next;
11 static struct child_to_clean *children_to_clean;
12 static int installed_child_cleanup_handler;
14 static void cleanup_children(int sig)
16 while (children_to_clean) {
17 struct child_to_clean *p = children_to_clean;
18 children_to_clean = p->next;
24 static void cleanup_children_on_signal(int sig)
26 cleanup_children(sig);
31 static void cleanup_children_on_exit(void)
33 cleanup_children(SIGTERM);
36 static void mark_child_for_cleanup(pid_t pid)
38 struct child_to_clean *p = xmalloc(sizeof(*p));
40 p->next = children_to_clean;
41 children_to_clean = p;
43 if (!installed_child_cleanup_handler) {
44 atexit(cleanup_children_on_exit);
45 sigchain_push_common(cleanup_children_on_signal);
46 installed_child_cleanup_handler = 1;
50 static void clear_child_for_cleanup(pid_t pid)
52 struct child_to_clean **pp;
54 for (pp = &children_to_clean; *pp; pp = &(*pp)->next) {
55 struct child_to_clean *clean_me = *pp;
57 if (clean_me->pid == pid) {
65 static inline void close_pair(int fd[2])
72 static inline void dup_devnull(int to)
74 int fd = open("/dev/null", O_RDWR);
80 static const char **prepare_shell_cmd(const char **argv)
85 for (argc = 0; argv[argc]; argc++)
87 /* +1 for NULL, +3 for "sh -c" plus extra $0 */
88 nargv = xmalloc(sizeof(*nargv) * (argc + 1 + 3));
91 die("BUG: shell command is empty");
93 if (strcspn(argv[0], "|&;<>()$`\\\"' \t\n*?[#~=%") != strlen(argv[0])) {
94 nargv[nargc++] = "sh";
95 nargv[nargc++] = "-c";
98 nargv[nargc++] = argv[0];
100 struct strbuf arg0 = STRBUF_INIT;
101 strbuf_addf(&arg0, "%s \"$@\"", argv[0]);
102 nargv[nargc++] = strbuf_detach(&arg0, NULL);
106 for (argc = 0; argv[argc]; argc++)
107 nargv[nargc++] = argv[argc];
114 static int execv_shell_cmd(const char **argv)
116 const char **nargv = prepare_shell_cmd(argv);
117 trace_argv_printf(nargv, "trace: exec:");
118 execvp(nargv[0], (char **)nargv);
125 static int child_err = 2;
126 static int child_notifier = -1;
128 static void notify_parent(void)
131 * execvp failed. If possible, we'd like to let start_command
132 * know, so failures like ENOENT can be handled right away; but
133 * otherwise, finish_command will still report the error.
135 xwrite(child_notifier, "", 1);
138 static NORETURN void die_child(const char *err, va_list params)
140 vwritef(child_err, "fatal: ", err, params);
144 static void error_child(const char *err, va_list params)
146 vwritef(child_err, "error: ", err, params);
150 static inline void set_cloexec(int fd)
152 int flags = fcntl(fd, F_GETFD);
154 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
157 static int wait_or_whine(pid_t pid, const char *argv0, int silent_exec_failure)
159 int status, code = -1;
161 int failed_errno = 0;
163 while ((waiting = waitpid(pid, &status, 0)) < 0 && errno == EINTR)
167 failed_errno = errno;
168 error("waitpid for %s failed: %s", argv0, strerror(errno));
169 } else if (waiting != pid) {
170 error("waitpid is confused (%s)", argv0);
171 } else if (WIFSIGNALED(status)) {
172 code = WTERMSIG(status);
173 error("%s died of signal %d", argv0, code);
175 * This return value is chosen so that code & 0xff
176 * mimics the exit code that a POSIX shell would report for
177 * a program that died from this signal.
180 } else if (WIFEXITED(status)) {
181 code = WEXITSTATUS(status);
183 * Convert special exit code when execvp failed.
187 failed_errno = ENOENT;
190 error("waitpid is confused (%s)", argv0);
193 clear_child_for_cleanup(pid);
195 errno = failed_errno;
199 int start_command(struct child_process *cmd)
201 int need_in, need_out, need_err;
202 int fdin[2], fdout[2], fderr[2];
203 int failed_errno = failed_errno;
206 * In case of errors we must keep the promise to close FDs
207 * that have been passed in via ->in and ->out.
210 need_in = !cmd->no_stdin && cmd->in < 0;
212 if (pipe(fdin) < 0) {
213 failed_errno = errno;
221 need_out = !cmd->no_stdout
222 && !cmd->stdout_to_stderr
225 if (pipe(fdout) < 0) {
226 failed_errno = errno;
236 need_err = !cmd->no_stderr && cmd->err < 0;
238 if (pipe(fderr) < 0) {
239 failed_errno = errno;
249 error("cannot create pipe for %s: %s",
250 cmd->argv[0], strerror(failed_errno));
251 errno = failed_errno;
257 trace_argv_printf(cmd->argv, "trace: run_command:");
263 if (pipe(notify_pipe))
264 notify_pipe[0] = notify_pipe[1] = -1;
269 * Redirect the channel to write syscall error messages to
270 * before redirecting the process's stderr so that all die()
271 * in subsequent call paths use the parent's stderr.
273 if (cmd->no_stderr || need_err) {
275 set_cloexec(child_err);
277 set_die_routine(die_child);
278 set_error_routine(error_child);
280 close(notify_pipe[0]);
281 set_cloexec(notify_pipe[1]);
282 child_notifier = notify_pipe[1];
283 atexit(notify_parent);
290 } else if (cmd->in) {
300 } else if (cmd->err > 1) {
307 else if (cmd->stdout_to_stderr)
312 } else if (cmd->out > 1) {
317 if (cmd->dir && chdir(cmd->dir))
318 die_errno("exec '%s': cd to '%s' failed", cmd->argv[0],
321 for (; *cmd->env; cmd->env++) {
322 if (strchr(*cmd->env, '='))
323 putenv((char *)*cmd->env);
328 if (cmd->preexec_cb) {
330 * We cannot predict what the pre-exec callback does.
331 * Forgo parent notification.
333 close(child_notifier);
339 execv_git_cmd(cmd->argv);
340 } else if (cmd->use_shell) {
341 execv_shell_cmd(cmd->argv);
343 execvp(cmd->argv[0], (char *const*) cmd->argv);
345 if (errno == ENOENT) {
346 if (!cmd->silent_exec_failure)
347 error("cannot run %s: %s", cmd->argv[0],
351 die_errno("cannot exec '%s'", cmd->argv[0]);
355 error("cannot fork() for %s: %s", cmd->argv[0],
356 strerror(failed_errno = errno));
357 else if (cmd->clean_on_exit)
358 mark_child_for_cleanup(cmd->pid);
361 * Wait for child's execvp. If the execvp succeeds (or if fork()
362 * failed), EOF is seen immediately by the parent. Otherwise, the
363 * child process sends a single byte.
364 * Note that use of this infrastructure is completely advisory,
365 * therefore, we keep error checks minimal.
367 close(notify_pipe[1]);
368 if (read(notify_pipe[0], ¬ify_pipe[1], 1) == 1) {
370 * At this point we know that fork() succeeded, but execvp()
371 * failed. Errors have been reported to our stderr.
373 wait_or_whine(cmd->pid, cmd->argv[0],
374 cmd->silent_exec_failure);
375 failed_errno = errno;
378 close(notify_pipe[0]);
383 int fhin = 0, fhout = 1, fherr = 2;
384 const char **sargv = cmd->argv;
385 char **env = environ;
388 fhin = open("/dev/null", O_RDWR);
395 fherr = open("/dev/null", O_RDWR);
397 fherr = dup(fderr[1]);
398 else if (cmd->err > 2)
399 fherr = dup(cmd->err);
402 fhout = open("/dev/null", O_RDWR);
403 else if (cmd->stdout_to_stderr)
406 fhout = dup(fdout[1]);
407 else if (cmd->out > 1)
408 fhout = dup(cmd->out);
411 env = make_augmented_environ(cmd->env);
414 cmd->argv = prepare_git_cmd(cmd->argv);
415 } else if (cmd->use_shell) {
416 cmd->argv = prepare_shell_cmd(cmd->argv);
419 cmd->pid = mingw_spawnvpe(cmd->argv[0], cmd->argv, env, cmd->dir,
421 failed_errno = errno;
422 if (cmd->pid < 0 && (!cmd->silent_exec_failure || errno != ENOENT))
423 error("cannot spawn %s: %s", cmd->argv[0], strerror(errno));
424 if (cmd->clean_on_exit && cmd->pid >= 0)
425 mark_child_for_cleanup(cmd->pid);
455 errno = failed_errno;
477 int finish_command(struct child_process *cmd)
479 return wait_or_whine(cmd->pid, cmd->argv[0], cmd->silent_exec_failure);
482 int run_command(struct child_process *cmd)
484 int code = start_command(cmd);
487 return finish_command(cmd);
490 static void prepare_run_command_v_opt(struct child_process *cmd,
494 memset(cmd, 0, sizeof(*cmd));
496 cmd->no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
497 cmd->git_cmd = opt & RUN_GIT_CMD ? 1 : 0;
498 cmd->stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
499 cmd->silent_exec_failure = opt & RUN_SILENT_EXEC_FAILURE ? 1 : 0;
500 cmd->use_shell = opt & RUN_USING_SHELL ? 1 : 0;
503 int run_command_v_opt(const char **argv, int opt)
505 struct child_process cmd;
506 prepare_run_command_v_opt(&cmd, argv, opt);
507 return run_command(&cmd);
510 int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env)
512 struct child_process cmd;
513 prepare_run_command_v_opt(&cmd, argv, opt);
516 return run_command(&cmd);
520 static pthread_t main_thread;
521 static int main_thread_set;
522 static pthread_key_t async_key;
524 static void *run_thread(void *data)
526 struct async *async = data;
529 pthread_setspecific(async_key, async);
530 ret = async->proc(async->proc_in, async->proc_out, async->data);
534 static NORETURN void die_async(const char *err, va_list params)
536 vreportf("fatal: ", err, params);
538 if (!pthread_equal(main_thread, pthread_self())) {
539 struct async *async = pthread_getspecific(async_key);
540 if (async->proc_in >= 0)
541 close(async->proc_in);
542 if (async->proc_out >= 0)
543 close(async->proc_out);
544 pthread_exit((void *)128);
551 int start_async(struct async *async)
553 int need_in, need_out;
554 int fdin[2], fdout[2];
555 int proc_in, proc_out;
557 need_in = async->in < 0;
559 if (pipe(fdin) < 0) {
562 return error("cannot create pipe: %s", strerror(errno));
567 need_out = async->out < 0;
569 if (pipe(fdout) < 0) {
574 return error("cannot create pipe: %s", strerror(errno));
576 async->out = fdout[0];
589 proc_out = async->out;
594 /* Flush stdio before fork() to avoid cloning buffers */
598 if (async->pid < 0) {
599 error("fork (async) failed: %s", strerror(errno));
607 exit(!!async->proc(proc_in, proc_out, async->data));
610 mark_child_for_cleanup(async->pid);
622 if (!main_thread_set) {
624 * We assume that the first time that start_async is called
625 * it is from the main thread.
628 main_thread = pthread_self();
629 pthread_key_create(&async_key, NULL);
630 set_die_routine(die_async);
634 set_cloexec(proc_in);
636 set_cloexec(proc_out);
637 async->proc_in = proc_in;
638 async->proc_out = proc_out;
640 int err = pthread_create(&async->tid, NULL, run_thread, async);
642 error("cannot create thread: %s", strerror(err));
662 int finish_async(struct async *async)
665 return wait_or_whine(async->pid, "child process", 0);
667 void *ret = (void *)(intptr_t)(-1);
669 if (pthread_join(async->tid, &ret))
670 error("pthread_join failed");
671 return (int)(intptr_t)ret;
675 int run_hook(const char *index_file, const char *name, ...)
677 struct child_process hook;
678 struct argv_array argv = ARGV_ARRAY_INIT;
679 const char *p, *env[2];
680 char index[PATH_MAX];
684 if (access(git_path("hooks/%s", name), X_OK) < 0)
687 va_start(args, name);
688 argv_array_push(&argv, git_path("hooks/%s", name));
689 while ((p = va_arg(args, const char *)))
690 argv_array_push(&argv, p);
693 memset(&hook, 0, sizeof(hook));
694 hook.argv = argv.argv;
696 hook.stdout_to_stderr = 1;
698 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
704 ret = run_command(&hook);
705 argv_array_clear(&argv);