Do not use SHELL_PATH from build system in prepare_shell_cmd on Windows
[git/git-svn.git] / run-command.c
blobe4edede8b9f95a0960a170d2cc7bda385778186c
1 #include "cache.h"
2 #include "run-command.h"
3 #include "exec_cmd.h"
4 #include "sigchain.h"
5 #include "argv-array.h"
7 #ifndef SHELL_PATH
8 # define SHELL_PATH "/bin/sh"
9 #endif
11 struct child_to_clean {
12 pid_t pid;
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;
23 kill(p->pid, sig);
24 free(p);
28 static void cleanup_children_on_signal(int sig)
30 cleanup_children(sig);
31 sigchain_pop(sig);
32 raise(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));
43 p->pid = pid;
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) {
60 if (p->pid == pid) {
61 *last = p->next;
62 free(p);
63 return;
68 static inline void close_pair(int fd[2])
70 close(fd[0]);
71 close(fd[1]);
74 #ifndef WIN32
75 static inline void dup_devnull(int to)
77 int fd = open("/dev/null", O_RDWR);
78 dup2(fd, to);
79 close(fd);
81 #endif
83 static const char **prepare_shell_cmd(const char **argv)
85 int argc, nargc = 0;
86 const char **nargv;
88 for (argc = 0; argv[argc]; argc++)
89 ; /* just counting */
90 /* +1 for NULL, +3 for "sh -c" plus extra $0 */
91 nargv = xmalloc(sizeof(*nargv) * (argc + 1 + 3));
93 if (argc < 1)
94 die("BUG: shell command is empty");
96 if (strcspn(argv[0], "|&;<>()$`\\\"' \t\n*?[#~=%") != strlen(argv[0])) {
97 #ifndef WIN32
98 nargv[nargc++] = SHELL_PATH;
99 #else
100 nargv[nargc++] = "sh";
101 #endif
102 nargv[nargc++] = "-c";
104 if (argc < 2)
105 nargv[nargc++] = argv[0];
106 else {
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];
115 nargv[nargc] = NULL;
117 return nargv;
120 #ifndef WIN32
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);
126 free(nargv);
127 return -1;
129 #endif
131 #ifndef WIN32
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);
148 exit(128);
151 static void error_child(const char *err, va_list params)
153 vwritef(child_err, "error: ", err, params);
155 #endif
157 static inline void set_cloexec(int fd)
159 int flags = fcntl(fd, F_GETFD);
160 if (flags >= 0)
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;
167 pid_t waiting;
168 int failed_errno = 0;
170 while ((waiting = waitpid(pid, &status, 0)) < 0 && errno == EINTR)
171 ; /* nothing */
173 if (waiting < 0) {
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.
186 code -= 128;
187 } else if (WIFEXITED(status)) {
188 code = WEXITSTATUS(status);
190 * Convert special exit code when execvp failed.
192 if (code == 127) {
193 code = -1;
194 failed_errno = ENOENT;
196 } else {
197 error("waitpid is confused (%s)", argv0);
200 clear_child_for_cleanup(pid);
202 errno = failed_errno;
203 return code;
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;
218 if (need_in) {
219 if (pipe(fdin) < 0) {
220 failed_errno = errno;
221 if (cmd->out > 0)
222 close(cmd->out);
223 goto fail_pipe;
225 cmd->in = fdin[1];
228 need_out = !cmd->no_stdout
229 && !cmd->stdout_to_stderr
230 && cmd->out < 0;
231 if (need_out) {
232 if (pipe(fdout) < 0) {
233 failed_errno = errno;
234 if (need_in)
235 close_pair(fdin);
236 else if (cmd->in)
237 close(cmd->in);
238 goto fail_pipe;
240 cmd->out = fdout[0];
243 need_err = !cmd->no_stderr && cmd->err < 0;
244 if (need_err) {
245 if (pipe(fderr) < 0) {
246 failed_errno = errno;
247 if (need_in)
248 close_pair(fdin);
249 else if (cmd->in)
250 close(cmd->in);
251 if (need_out)
252 close_pair(fdout);
253 else if (cmd->out)
254 close(cmd->out);
255 fail_pipe:
256 error("cannot create pipe for %s: %s",
257 cmd->argv[0], strerror(failed_errno));
258 errno = failed_errno;
259 return -1;
261 cmd->err = fderr[0];
264 trace_argv_printf(cmd->argv, "trace: run_command:");
265 fflush(NULL);
267 #ifndef WIN32
269 int notify_pipe[2];
270 if (pipe(notify_pipe))
271 notify_pipe[0] = notify_pipe[1] = -1;
273 cmd->pid = fork();
274 if (!cmd->pid) {
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) {
281 child_err = dup(2);
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);
292 if (cmd->no_stdin)
293 dup_devnull(0);
294 else if (need_in) {
295 dup2(fdin[0], 0);
296 close_pair(fdin);
297 } else if (cmd->in) {
298 dup2(cmd->in, 0);
299 close(cmd->in);
302 if (cmd->no_stderr)
303 dup_devnull(2);
304 else if (need_err) {
305 dup2(fderr[1], 2);
306 close_pair(fderr);
307 } else if (cmd->err > 1) {
308 dup2(cmd->err, 2);
309 close(cmd->err);
312 if (cmd->no_stdout)
313 dup_devnull(1);
314 else if (cmd->stdout_to_stderr)
315 dup2(2, 1);
316 else if (need_out) {
317 dup2(fdout[1], 1);
318 close_pair(fdout);
319 } else if (cmd->out > 1) {
320 dup2(cmd->out, 1);
321 close(cmd->out);
324 if (cmd->dir && chdir(cmd->dir))
325 die_errno("exec '%s': cd to '%s' failed", cmd->argv[0],
326 cmd->dir);
327 if (cmd->env) {
328 for (; *cmd->env; cmd->env++) {
329 if (strchr(*cmd->env, '='))
330 putenv((char *)*cmd->env);
331 else
332 unsetenv(*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);
341 child_notifier = -1;
343 cmd->preexec_cb();
345 if (cmd->git_cmd) {
346 execv_git_cmd(cmd->argv);
347 } else if (cmd->use_shell) {
348 execv_shell_cmd(cmd->argv);
349 } else {
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],
355 strerror(ENOENT));
356 exit(127);
357 } else {
358 die_errno("cannot exec '%s'", cmd->argv[0]);
361 if (cmd->pid < 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], &notify_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;
383 cmd->pid = -1;
385 close(notify_pipe[0]);
388 #else
390 int fhin = 0, fhout = 1, fherr = 2;
391 const char **sargv = cmd->argv;
392 char **env = environ;
394 if (cmd->no_stdin)
395 fhin = open("/dev/null", O_RDWR);
396 else if (need_in)
397 fhin = dup(fdin[0]);
398 else if (cmd->in)
399 fhin = dup(cmd->in);
401 if (cmd->no_stderr)
402 fherr = open("/dev/null", O_RDWR);
403 else if (need_err)
404 fherr = dup(fderr[1]);
405 else if (cmd->err > 2)
406 fherr = dup(cmd->err);
408 if (cmd->no_stdout)
409 fhout = open("/dev/null", O_RDWR);
410 else if (cmd->stdout_to_stderr)
411 fhout = dup(fherr);
412 else if (need_out)
413 fhout = dup(fdout[1]);
414 else if (cmd->out > 1)
415 fhout = dup(cmd->out);
417 if (cmd->env)
418 env = make_augmented_environ(cmd->env);
420 if (cmd->git_cmd) {
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,
427 fhin, fhout, fherr);
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);
434 if (cmd->env)
435 free_environ(env);
436 if (cmd->git_cmd)
437 free(cmd->argv);
439 cmd->argv = sargv;
440 if (fhin != 0)
441 close(fhin);
442 if (fhout != 1)
443 close(fhout);
444 if (fherr != 2)
445 close(fherr);
447 #endif
449 if (cmd->pid < 0) {
450 if (need_in)
451 close_pair(fdin);
452 else if (cmd->in)
453 close(cmd->in);
454 if (need_out)
455 close_pair(fdout);
456 else if (cmd->out)
457 close(cmd->out);
458 if (need_err)
459 close_pair(fderr);
460 else if (cmd->err)
461 close(cmd->err);
462 errno = failed_errno;
463 return -1;
466 if (need_in)
467 close(fdin[0]);
468 else if (cmd->in)
469 close(cmd->in);
471 if (need_out)
472 close(fdout[1]);
473 else if (cmd->out)
474 close(cmd->out);
476 if (need_err)
477 close(fderr[1]);
478 else if (cmd->err)
479 close(cmd->err);
481 return 0;
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);
492 if (code)
493 return code;
494 return finish_command(cmd);
497 static void prepare_run_command_v_opt(struct child_process *cmd,
498 const char **argv,
499 int opt)
501 memset(cmd, 0, sizeof(*cmd));
502 cmd->argv = argv;
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);
522 cmd.dir = dir;
523 cmd.env = env;
524 return run_command(&cmd);
527 #ifndef NO_PTHREADS
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;
535 intptr_t ret;
537 pthread_setspecific(async_key, async);
538 ret = async->proc(async->proc_in, async->proc_out, async->data);
539 return (void *)ret;
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);
555 exit(128);
557 #endif
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;
566 if (need_in) {
567 if (pipe(fdin) < 0) {
568 if (async->out > 0)
569 close(async->out);
570 return error("cannot create pipe: %s", strerror(errno));
572 async->in = fdin[1];
575 need_out = async->out < 0;
576 if (need_out) {
577 if (pipe(fdout) < 0) {
578 if (need_in)
579 close_pair(fdin);
580 else if (async->in)
581 close(async->in);
582 return error("cannot create pipe: %s", strerror(errno));
584 async->out = fdout[0];
587 if (need_in)
588 proc_in = fdin[0];
589 else if (async->in)
590 proc_in = async->in;
591 else
592 proc_in = -1;
594 if (need_out)
595 proc_out = fdout[1];
596 else if (async->out)
597 proc_out = async->out;
598 else
599 proc_out = -1;
601 #ifdef NO_PTHREADS
602 /* Flush stdio before fork() to avoid cloning buffers */
603 fflush(NULL);
605 async->pid = fork();
606 if (async->pid < 0) {
607 error("fork (async) failed: %s", strerror(errno));
608 goto error;
610 if (!async->pid) {
611 if (need_in)
612 close(fdin[1]);
613 if (need_out)
614 close(fdout[0]);
615 exit(!!async->proc(proc_in, proc_out, async->data));
618 mark_child_for_cleanup(async->pid);
620 if (need_in)
621 close(fdin[0]);
622 else if (async->in)
623 close(async->in);
625 if (need_out)
626 close(fdout[1]);
627 else if (async->out)
628 close(async->out);
629 #else
630 if (!main_thread_set) {
632 * We assume that the first time that start_async is called
633 * it is from the main thread.
635 main_thread_set = 1;
636 main_thread = pthread_self();
637 pthread_key_create(&async_key, NULL);
638 set_die_routine(die_async);
641 if (proc_in >= 0)
642 set_cloexec(proc_in);
643 if (proc_out >= 0)
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);
649 if (err) {
650 error("cannot create thread: %s", strerror(err));
651 goto error;
654 #endif
655 return 0;
657 error:
658 if (need_in)
659 close_pair(fdin);
660 else if (async->in)
661 close(async->in);
663 if (need_out)
664 close_pair(fdout);
665 else if (async->out)
666 close(async->out);
667 return -1;
670 int finish_async(struct async *async)
672 #ifdef NO_PTHREADS
673 return wait_or_whine(async->pid, "child process", 0);
674 #else
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;
680 #endif
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];
689 va_list args;
690 int ret;
692 if (access(git_path("hooks/%s", name), X_OK) < 0)
693 return 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);
699 va_end(args);
701 memset(&hook, 0, sizeof(hook));
702 hook.argv = argv.argv;
703 hook.no_stdin = 1;
704 hook.stdout_to_stderr = 1;
705 if (index_file) {
706 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
707 env[0] = index;
708 env[1] = NULL;
709 hook.env = env;
712 ret = run_command(&hook);
713 argv_array_clear(&argv);
714 return ret;