run-command: factor out child_process_clear()
[git/debian.git] / run-command.c
blobfc391fb9cdd6977e8e40d00d2c6142fb824f99d1
1 #include "cache.h"
2 #include "run-command.h"
3 #include "exec_cmd.h"
4 #include "sigchain.h"
5 #include "argv-array.h"
7 void child_process_init(struct child_process *child)
9 memset(child, 0, sizeof(*child));
10 argv_array_init(&child->args);
11 argv_array_init(&child->env_array);
14 void child_process_clear(struct child_process *child)
16 argv_array_clear(&child->args);
17 argv_array_clear(&child->env_array);
20 struct child_to_clean {
21 pid_t pid;
22 struct child_to_clean *next;
24 static struct child_to_clean *children_to_clean;
25 static int installed_child_cleanup_handler;
27 static void cleanup_children(int sig)
29 while (children_to_clean) {
30 struct child_to_clean *p = children_to_clean;
31 children_to_clean = p->next;
32 kill(p->pid, sig);
33 free(p);
37 static void cleanup_children_on_signal(int sig)
39 cleanup_children(sig);
40 sigchain_pop(sig);
41 raise(sig);
44 static void cleanup_children_on_exit(void)
46 cleanup_children(SIGTERM);
49 static void mark_child_for_cleanup(pid_t pid)
51 struct child_to_clean *p = xmalloc(sizeof(*p));
52 p->pid = pid;
53 p->next = children_to_clean;
54 children_to_clean = p;
56 if (!installed_child_cleanup_handler) {
57 atexit(cleanup_children_on_exit);
58 sigchain_push_common(cleanup_children_on_signal);
59 installed_child_cleanup_handler = 1;
63 static void clear_child_for_cleanup(pid_t pid)
65 struct child_to_clean **pp;
67 for (pp = &children_to_clean; *pp; pp = &(*pp)->next) {
68 struct child_to_clean *clean_me = *pp;
70 if (clean_me->pid == pid) {
71 *pp = clean_me->next;
72 free(clean_me);
73 return;
78 static inline void close_pair(int fd[2])
80 close(fd[0]);
81 close(fd[1]);
84 #ifndef GIT_WINDOWS_NATIVE
85 static inline void dup_devnull(int to)
87 int fd = open("/dev/null", O_RDWR);
88 if (fd < 0)
89 die_errno(_("open /dev/null failed"));
90 if (dup2(fd, to) < 0)
91 die_errno(_("dup2(%d,%d) failed"), fd, to);
92 close(fd);
94 #endif
96 static char *locate_in_PATH(const char *file)
98 const char *p = getenv("PATH");
99 struct strbuf buf = STRBUF_INIT;
101 if (!p || !*p)
102 return NULL;
104 while (1) {
105 const char *end = strchrnul(p, ':');
107 strbuf_reset(&buf);
109 /* POSIX specifies an empty entry as the current directory. */
110 if (end != p) {
111 strbuf_add(&buf, p, end - p);
112 strbuf_addch(&buf, '/');
114 strbuf_addstr(&buf, file);
116 if (!access(buf.buf, F_OK))
117 return strbuf_detach(&buf, NULL);
119 if (!*end)
120 break;
121 p = end + 1;
124 strbuf_release(&buf);
125 return NULL;
128 static int exists_in_PATH(const char *file)
130 char *r = locate_in_PATH(file);
131 free(r);
132 return r != NULL;
135 int sane_execvp(const char *file, char * const argv[])
137 if (!execvp(file, argv))
138 return 0; /* cannot happen ;-) */
141 * When a command can't be found because one of the directories
142 * listed in $PATH is unsearchable, execvp reports EACCES, but
143 * careful usability testing (read: analysis of occasional bug
144 * reports) reveals that "No such file or directory" is more
145 * intuitive.
147 * We avoid commands with "/", because execvp will not do $PATH
148 * lookups in that case.
150 * The reassignment of EACCES to errno looks like a no-op below,
151 * but we need to protect against exists_in_PATH overwriting errno.
153 if (errno == EACCES && !strchr(file, '/'))
154 errno = exists_in_PATH(file) ? EACCES : ENOENT;
155 else if (errno == ENOTDIR && !strchr(file, '/'))
156 errno = ENOENT;
157 return -1;
160 static const char **prepare_shell_cmd(const char **argv)
162 int argc, nargc = 0;
163 const char **nargv;
165 for (argc = 0; argv[argc]; argc++)
166 ; /* just counting */
167 /* +1 for NULL, +3 for "sh -c" plus extra $0 */
168 nargv = xmalloc(sizeof(*nargv) * (argc + 1 + 3));
170 if (argc < 1)
171 die("BUG: shell command is empty");
173 if (strcspn(argv[0], "|&;<>()$`\\\"' \t\n*?[#~=%") != strlen(argv[0])) {
174 #ifndef GIT_WINDOWS_NATIVE
175 nargv[nargc++] = SHELL_PATH;
176 #else
177 nargv[nargc++] = "sh";
178 #endif
179 nargv[nargc++] = "-c";
181 if (argc < 2)
182 nargv[nargc++] = argv[0];
183 else {
184 struct strbuf arg0 = STRBUF_INIT;
185 strbuf_addf(&arg0, "%s \"$@\"", argv[0]);
186 nargv[nargc++] = strbuf_detach(&arg0, NULL);
190 for (argc = 0; argv[argc]; argc++)
191 nargv[nargc++] = argv[argc];
192 nargv[nargc] = NULL;
194 return nargv;
197 #ifndef GIT_WINDOWS_NATIVE
198 static int execv_shell_cmd(const char **argv)
200 const char **nargv = prepare_shell_cmd(argv);
201 trace_argv_printf(nargv, "trace: exec:");
202 sane_execvp(nargv[0], (char **)nargv);
203 free(nargv);
204 return -1;
206 #endif
208 #ifndef GIT_WINDOWS_NATIVE
209 static int child_err = 2;
210 static int child_notifier = -1;
212 static void notify_parent(void)
215 * execvp failed. If possible, we'd like to let start_command
216 * know, so failures like ENOENT can be handled right away; but
217 * otherwise, finish_command will still report the error.
219 xwrite(child_notifier, "", 1);
222 static NORETURN void die_child(const char *err, va_list params)
224 vwritef(child_err, "fatal: ", err, params);
225 exit(128);
228 static void error_child(const char *err, va_list params)
230 vwritef(child_err, "error: ", err, params);
232 #endif
234 static inline void set_cloexec(int fd)
236 int flags = fcntl(fd, F_GETFD);
237 if (flags >= 0)
238 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
241 static int wait_or_whine(pid_t pid, const char *argv0)
243 int status, code = -1;
244 pid_t waiting;
245 int failed_errno = 0;
247 while ((waiting = waitpid(pid, &status, 0)) < 0 && errno == EINTR)
248 ; /* nothing */
250 if (waiting < 0) {
251 failed_errno = errno;
252 error("waitpid for %s failed: %s", argv0, strerror(errno));
253 } else if (waiting != pid) {
254 error("waitpid is confused (%s)", argv0);
255 } else if (WIFSIGNALED(status)) {
256 code = WTERMSIG(status);
257 if (code != SIGINT && code != SIGQUIT)
258 error("%s died of signal %d", argv0, code);
260 * This return value is chosen so that code & 0xff
261 * mimics the exit code that a POSIX shell would report for
262 * a program that died from this signal.
264 code += 128;
265 } else if (WIFEXITED(status)) {
266 code = WEXITSTATUS(status);
268 * Convert special exit code when execvp failed.
270 if (code == 127) {
271 code = -1;
272 failed_errno = ENOENT;
274 } else {
275 error("waitpid is confused (%s)", argv0);
278 clear_child_for_cleanup(pid);
280 errno = failed_errno;
281 return code;
284 int start_command(struct child_process *cmd)
286 int need_in, need_out, need_err;
287 int fdin[2], fdout[2], fderr[2];
288 int failed_errno;
289 char *str;
291 if (!cmd->argv)
292 cmd->argv = cmd->args.argv;
293 if (!cmd->env)
294 cmd->env = cmd->env_array.argv;
297 * In case of errors we must keep the promise to close FDs
298 * that have been passed in via ->in and ->out.
301 need_in = !cmd->no_stdin && cmd->in < 0;
302 if (need_in) {
303 if (pipe(fdin) < 0) {
304 failed_errno = errno;
305 if (cmd->out > 0)
306 close(cmd->out);
307 str = "standard input";
308 goto fail_pipe;
310 cmd->in = fdin[1];
313 need_out = !cmd->no_stdout
314 && !cmd->stdout_to_stderr
315 && cmd->out < 0;
316 if (need_out) {
317 if (pipe(fdout) < 0) {
318 failed_errno = errno;
319 if (need_in)
320 close_pair(fdin);
321 else if (cmd->in)
322 close(cmd->in);
323 str = "standard output";
324 goto fail_pipe;
326 cmd->out = fdout[0];
329 need_err = !cmd->no_stderr && cmd->err < 0;
330 if (need_err) {
331 if (pipe(fderr) < 0) {
332 failed_errno = errno;
333 if (need_in)
334 close_pair(fdin);
335 else if (cmd->in)
336 close(cmd->in);
337 if (need_out)
338 close_pair(fdout);
339 else if (cmd->out)
340 close(cmd->out);
341 str = "standard error";
342 fail_pipe:
343 error("cannot create %s pipe for %s: %s",
344 str, cmd->argv[0], strerror(failed_errno));
345 child_process_clear(cmd);
346 errno = failed_errno;
347 return -1;
349 cmd->err = fderr[0];
352 trace_argv_printf(cmd->argv, "trace: run_command:");
353 fflush(NULL);
355 #ifndef GIT_WINDOWS_NATIVE
357 int notify_pipe[2];
358 if (pipe(notify_pipe))
359 notify_pipe[0] = notify_pipe[1] = -1;
361 cmd->pid = fork();
362 failed_errno = errno;
363 if (!cmd->pid) {
365 * Redirect the channel to write syscall error messages to
366 * before redirecting the process's stderr so that all die()
367 * in subsequent call paths use the parent's stderr.
369 if (cmd->no_stderr || need_err) {
370 child_err = dup(2);
371 set_cloexec(child_err);
373 set_die_routine(die_child);
374 set_error_routine(error_child);
376 close(notify_pipe[0]);
377 set_cloexec(notify_pipe[1]);
378 child_notifier = notify_pipe[1];
379 atexit(notify_parent);
381 if (cmd->no_stdin)
382 dup_devnull(0);
383 else if (need_in) {
384 dup2(fdin[0], 0);
385 close_pair(fdin);
386 } else if (cmd->in) {
387 dup2(cmd->in, 0);
388 close(cmd->in);
391 if (cmd->no_stderr)
392 dup_devnull(2);
393 else if (need_err) {
394 dup2(fderr[1], 2);
395 close_pair(fderr);
396 } else if (cmd->err > 1) {
397 dup2(cmd->err, 2);
398 close(cmd->err);
401 if (cmd->no_stdout)
402 dup_devnull(1);
403 else if (cmd->stdout_to_stderr)
404 dup2(2, 1);
405 else if (need_out) {
406 dup2(fdout[1], 1);
407 close_pair(fdout);
408 } else if (cmd->out > 1) {
409 dup2(cmd->out, 1);
410 close(cmd->out);
413 if (cmd->dir && chdir(cmd->dir))
414 die_errno("exec '%s': cd to '%s' failed", cmd->argv[0],
415 cmd->dir);
416 if (cmd->env) {
417 for (; *cmd->env; cmd->env++) {
418 if (strchr(*cmd->env, '='))
419 putenv((char *)*cmd->env);
420 else
421 unsetenv(*cmd->env);
424 if (cmd->git_cmd)
425 execv_git_cmd(cmd->argv);
426 else if (cmd->use_shell)
427 execv_shell_cmd(cmd->argv);
428 else
429 sane_execvp(cmd->argv[0], (char *const*) cmd->argv);
430 if (errno == ENOENT) {
431 if (!cmd->silent_exec_failure)
432 error("cannot run %s: %s", cmd->argv[0],
433 strerror(ENOENT));
434 exit(127);
435 } else {
436 die_errno("cannot exec '%s'", cmd->argv[0]);
439 if (cmd->pid < 0)
440 error("cannot fork() for %s: %s", cmd->argv[0],
441 strerror(errno));
442 else if (cmd->clean_on_exit)
443 mark_child_for_cleanup(cmd->pid);
446 * Wait for child's execvp. If the execvp succeeds (or if fork()
447 * failed), EOF is seen immediately by the parent. Otherwise, the
448 * child process sends a single byte.
449 * Note that use of this infrastructure is completely advisory,
450 * therefore, we keep error checks minimal.
452 close(notify_pipe[1]);
453 if (read(notify_pipe[0], &notify_pipe[1], 1) == 1) {
455 * At this point we know that fork() succeeded, but execvp()
456 * failed. Errors have been reported to our stderr.
458 wait_or_whine(cmd->pid, cmd->argv[0]);
459 failed_errno = errno;
460 cmd->pid = -1;
462 close(notify_pipe[0]);
464 #else
466 int fhin = 0, fhout = 1, fherr = 2;
467 const char **sargv = cmd->argv;
469 if (cmd->no_stdin)
470 fhin = open("/dev/null", O_RDWR);
471 else if (need_in)
472 fhin = dup(fdin[0]);
473 else if (cmd->in)
474 fhin = dup(cmd->in);
476 if (cmd->no_stderr)
477 fherr = open("/dev/null", O_RDWR);
478 else if (need_err)
479 fherr = dup(fderr[1]);
480 else if (cmd->err > 2)
481 fherr = dup(cmd->err);
483 if (cmd->no_stdout)
484 fhout = open("/dev/null", O_RDWR);
485 else if (cmd->stdout_to_stderr)
486 fhout = dup(fherr);
487 else if (need_out)
488 fhout = dup(fdout[1]);
489 else if (cmd->out > 1)
490 fhout = dup(cmd->out);
492 if (cmd->git_cmd)
493 cmd->argv = prepare_git_cmd(cmd->argv);
494 else if (cmd->use_shell)
495 cmd->argv = prepare_shell_cmd(cmd->argv);
497 cmd->pid = mingw_spawnvpe(cmd->argv[0], cmd->argv, (char**) cmd->env,
498 cmd->dir, fhin, fhout, fherr);
499 failed_errno = errno;
500 if (cmd->pid < 0 && (!cmd->silent_exec_failure || errno != ENOENT))
501 error("cannot spawn %s: %s", cmd->argv[0], strerror(errno));
502 if (cmd->clean_on_exit && cmd->pid >= 0)
503 mark_child_for_cleanup(cmd->pid);
505 if (cmd->git_cmd)
506 free(cmd->argv);
508 cmd->argv = sargv;
509 if (fhin != 0)
510 close(fhin);
511 if (fhout != 1)
512 close(fhout);
513 if (fherr != 2)
514 close(fherr);
516 #endif
518 if (cmd->pid < 0) {
519 if (need_in)
520 close_pair(fdin);
521 else if (cmd->in)
522 close(cmd->in);
523 if (need_out)
524 close_pair(fdout);
525 else if (cmd->out)
526 close(cmd->out);
527 if (need_err)
528 close_pair(fderr);
529 else if (cmd->err)
530 close(cmd->err);
531 child_process_clear(cmd);
532 errno = failed_errno;
533 return -1;
536 if (need_in)
537 close(fdin[0]);
538 else if (cmd->in)
539 close(cmd->in);
541 if (need_out)
542 close(fdout[1]);
543 else if (cmd->out)
544 close(cmd->out);
546 if (need_err)
547 close(fderr[1]);
548 else if (cmd->err)
549 close(cmd->err);
551 return 0;
554 int finish_command(struct child_process *cmd)
556 int ret = wait_or_whine(cmd->pid, cmd->argv[0]);
557 child_process_clear(cmd);
558 return ret;
561 int run_command(struct child_process *cmd)
563 int code;
565 if (cmd->out < 0 || cmd->err < 0)
566 die("BUG: run_command with a pipe can cause deadlock");
568 code = start_command(cmd);
569 if (code)
570 return code;
571 return finish_command(cmd);
574 int run_command_v_opt(const char **argv, int opt)
576 return run_command_v_opt_cd_env(argv, opt, NULL, NULL);
579 int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env)
581 struct child_process cmd = CHILD_PROCESS_INIT;
582 cmd.argv = argv;
583 cmd.no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
584 cmd.git_cmd = opt & RUN_GIT_CMD ? 1 : 0;
585 cmd.stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
586 cmd.silent_exec_failure = opt & RUN_SILENT_EXEC_FAILURE ? 1 : 0;
587 cmd.use_shell = opt & RUN_USING_SHELL ? 1 : 0;
588 cmd.clean_on_exit = opt & RUN_CLEAN_ON_EXIT ? 1 : 0;
589 cmd.dir = dir;
590 cmd.env = env;
591 return run_command(&cmd);
594 #ifndef NO_PTHREADS
595 static pthread_t main_thread;
596 static int main_thread_set;
597 static pthread_key_t async_key;
598 static pthread_key_t async_die_counter;
600 static void *run_thread(void *data)
602 struct async *async = data;
603 intptr_t ret;
605 pthread_setspecific(async_key, async);
606 ret = async->proc(async->proc_in, async->proc_out, async->data);
607 return (void *)ret;
610 static NORETURN void die_async(const char *err, va_list params)
612 vreportf("fatal: ", err, params);
614 if (!pthread_equal(main_thread, pthread_self())) {
615 struct async *async = pthread_getspecific(async_key);
616 if (async->proc_in >= 0)
617 close(async->proc_in);
618 if (async->proc_out >= 0)
619 close(async->proc_out);
620 pthread_exit((void *)128);
623 exit(128);
626 static int async_die_is_recursing(void)
628 void *ret = pthread_getspecific(async_die_counter);
629 pthread_setspecific(async_die_counter, (void *)1);
630 return ret != NULL;
633 #else
635 static struct {
636 void (**handlers)(void);
637 size_t nr;
638 size_t alloc;
639 } git_atexit_hdlrs;
641 static int git_atexit_installed;
643 static void git_atexit_dispatch(void)
645 size_t i;
647 for (i=git_atexit_hdlrs.nr ; i ; i--)
648 git_atexit_hdlrs.handlers[i-1]();
651 static void git_atexit_clear(void)
653 free(git_atexit_hdlrs.handlers);
654 memset(&git_atexit_hdlrs, 0, sizeof(git_atexit_hdlrs));
655 git_atexit_installed = 0;
658 #undef atexit
659 int git_atexit(void (*handler)(void))
661 ALLOC_GROW(git_atexit_hdlrs.handlers, git_atexit_hdlrs.nr + 1, git_atexit_hdlrs.alloc);
662 git_atexit_hdlrs.handlers[git_atexit_hdlrs.nr++] = handler;
663 if (!git_atexit_installed) {
664 if (atexit(&git_atexit_dispatch))
665 return -1;
666 git_atexit_installed = 1;
668 return 0;
670 #define atexit git_atexit
672 #endif
674 int start_async(struct async *async)
676 int need_in, need_out;
677 int fdin[2], fdout[2];
678 int proc_in, proc_out;
680 need_in = async->in < 0;
681 if (need_in) {
682 if (pipe(fdin) < 0) {
683 if (async->out > 0)
684 close(async->out);
685 return error("cannot create pipe: %s", strerror(errno));
687 async->in = fdin[1];
690 need_out = async->out < 0;
691 if (need_out) {
692 if (pipe(fdout) < 0) {
693 if (need_in)
694 close_pair(fdin);
695 else if (async->in)
696 close(async->in);
697 return error("cannot create pipe: %s", strerror(errno));
699 async->out = fdout[0];
702 if (need_in)
703 proc_in = fdin[0];
704 else if (async->in)
705 proc_in = async->in;
706 else
707 proc_in = -1;
709 if (need_out)
710 proc_out = fdout[1];
711 else if (async->out)
712 proc_out = async->out;
713 else
714 proc_out = -1;
716 #ifdef NO_PTHREADS
717 /* Flush stdio before fork() to avoid cloning buffers */
718 fflush(NULL);
720 async->pid = fork();
721 if (async->pid < 0) {
722 error("fork (async) failed: %s", strerror(errno));
723 goto error;
725 if (!async->pid) {
726 if (need_in)
727 close(fdin[1]);
728 if (need_out)
729 close(fdout[0]);
730 git_atexit_clear();
731 exit(!!async->proc(proc_in, proc_out, async->data));
734 mark_child_for_cleanup(async->pid);
736 if (need_in)
737 close(fdin[0]);
738 else if (async->in)
739 close(async->in);
741 if (need_out)
742 close(fdout[1]);
743 else if (async->out)
744 close(async->out);
745 #else
746 if (!main_thread_set) {
748 * We assume that the first time that start_async is called
749 * it is from the main thread.
751 main_thread_set = 1;
752 main_thread = pthread_self();
753 pthread_key_create(&async_key, NULL);
754 pthread_key_create(&async_die_counter, NULL);
755 set_die_routine(die_async);
756 set_die_is_recursing_routine(async_die_is_recursing);
759 if (proc_in >= 0)
760 set_cloexec(proc_in);
761 if (proc_out >= 0)
762 set_cloexec(proc_out);
763 async->proc_in = proc_in;
764 async->proc_out = proc_out;
766 int err = pthread_create(&async->tid, NULL, run_thread, async);
767 if (err) {
768 error("cannot create thread: %s", strerror(err));
769 goto error;
772 #endif
773 return 0;
775 error:
776 if (need_in)
777 close_pair(fdin);
778 else if (async->in)
779 close(async->in);
781 if (need_out)
782 close_pair(fdout);
783 else if (async->out)
784 close(async->out);
785 return -1;
788 int finish_async(struct async *async)
790 #ifdef NO_PTHREADS
791 return wait_or_whine(async->pid, "child process");
792 #else
793 void *ret = (void *)(intptr_t)(-1);
795 if (pthread_join(async->tid, &ret))
796 error("pthread_join failed");
797 return (int)(intptr_t)ret;
798 #endif
801 char *find_hook(const char *name)
803 char *path = git_path("hooks/%s", name);
804 if (access(path, X_OK) < 0)
805 path = NULL;
807 return path;
810 int run_hook_ve(const char *const *env, const char *name, va_list args)
812 struct child_process hook = CHILD_PROCESS_INIT;
813 const char *p;
815 p = find_hook(name);
816 if (!p)
817 return 0;
819 argv_array_push(&hook.args, p);
820 while ((p = va_arg(args, const char *)))
821 argv_array_push(&hook.args, p);
822 hook.env = env;
823 hook.no_stdin = 1;
824 hook.stdout_to_stderr = 1;
826 return run_command(&hook);
829 int run_hook_le(const char *const *env, const char *name, ...)
831 va_list args;
832 int ret;
834 va_start(args, name);
835 ret = run_hook_ve(env, name, args);
836 va_end(args);
838 return ret;
841 int capture_command(struct child_process *cmd, struct strbuf *buf, size_t hint)
843 cmd->out = -1;
844 if (start_command(cmd) < 0)
845 return -1;
847 if (strbuf_read(buf, cmd->out, hint) < 0) {
848 close(cmd->out);
849 finish_command(cmd); /* throw away exit code */
850 return -1;
853 close(cmd->out);
854 return finish_command(cmd);