remote-hg: adjust to hg 1.9
[git/dscho.git] / run-command.c
blob6d0dc3da915bfb0e4956c9b421638ad0b51bfb76
1 #include "cache.h"
2 #include "run-command.h"
3 #include "exec_cmd.h"
4 #include "sigchain.h"
5 #include "argv-array.h"
7 struct child_to_clean {
8 pid_t pid;
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;
19 kill(p->pid, sig);
20 free(p);
24 static void cleanup_children_on_signal(int sig)
26 cleanup_children(sig);
27 sigchain_pop(sig);
28 raise(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));
39 p->pid = pid;
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 **last, *p;
54 last = &children_to_clean;
55 for (p = children_to_clean; p; p = p->next) {
56 if (p->pid == pid) {
57 *last = p->next;
58 free(p);
59 return;
64 static inline void close_pair(int fd[2])
66 close(fd[0]);
67 close(fd[1]);
70 #ifndef WIN32
71 static inline void dup_devnull(int to)
73 int fd = open("/dev/null", O_RDWR);
74 dup2(fd, to);
75 close(fd);
77 #endif
79 static const char **prepare_shell_cmd(const char **argv)
81 int argc, nargc = 0;
82 const char **nargv;
84 for (argc = 0; argv[argc]; argc++)
85 ; /* just counting */
86 /* +1 for NULL, +3 for "sh -c" plus extra $0 */
87 nargv = xmalloc(sizeof(*nargv) * (argc + 1 + 3));
89 if (argc < 1)
90 die("BUG: shell command is empty");
92 if (strcspn(argv[0], "|&;<>()$`\\\"' \t\n*?[#~=%") != strlen(argv[0])) {
93 nargv[nargc++] = "sh";
94 nargv[nargc++] = "-c";
96 if (argc < 2)
97 nargv[nargc++] = argv[0];
98 else {
99 struct strbuf arg0 = STRBUF_INIT;
100 strbuf_addf(&arg0, "%s \"$@\"", argv[0]);
101 nargv[nargc++] = strbuf_detach(&arg0, NULL);
105 for (argc = 0; argv[argc]; argc++)
106 nargv[nargc++] = argv[argc];
107 nargv[nargc] = NULL;
109 return nargv;
112 #ifndef WIN32
113 static int execv_shell_cmd(const char **argv)
115 const char **nargv = prepare_shell_cmd(argv);
116 trace_argv_printf(nargv, "trace: exec:");
117 execvp(nargv[0], (char **)nargv);
118 free(nargv);
119 return -1;
121 #endif
123 #ifndef WIN32
124 static int child_err = 2;
125 static int child_notifier = -1;
127 static void notify_parent(void)
130 * execvp failed. If possible, we'd like to let start_command
131 * know, so failures like ENOENT can be handled right away; but
132 * otherwise, finish_command will still report the error.
134 xwrite(child_notifier, "", 1);
137 static NORETURN void die_child(const char *err, va_list params)
139 vwritef(child_err, "fatal: ", err, params);
140 exit(128);
143 static void error_child(const char *err, va_list params)
145 vwritef(child_err, "error: ", err, params);
147 #endif
149 static inline void set_cloexec(int fd)
151 int flags = fcntl(fd, F_GETFD);
152 if (flags >= 0)
153 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
156 static int wait_or_whine(pid_t pid, const char *argv0, int silent_exec_failure)
158 int status, code = -1;
159 pid_t waiting;
160 int failed_errno = 0;
162 while ((waiting = waitpid(pid, &status, 0)) < 0 && errno == EINTR)
163 ; /* nothing */
165 if (waiting < 0) {
166 failed_errno = errno;
167 error("waitpid for %s failed: %s", argv0, strerror(errno));
168 } else if (waiting != pid) {
169 error("waitpid is confused (%s)", argv0);
170 } else if (WIFSIGNALED(status)) {
171 code = WTERMSIG(status);
172 error("%s died of signal %d", argv0, code);
174 * This return value is chosen so that code & 0xff
175 * mimics the exit code that a POSIX shell would report for
176 * a program that died from this signal.
178 code -= 128;
179 } else if (WIFEXITED(status)) {
180 code = WEXITSTATUS(status);
182 * Convert special exit code when execvp failed.
184 if (code == 127) {
185 code = -1;
186 failed_errno = ENOENT;
188 } else {
189 error("waitpid is confused (%s)", argv0);
192 clear_child_for_cleanup(pid);
194 errno = failed_errno;
195 return code;
198 int start_command(struct child_process *cmd)
200 int need_in, need_out, need_err;
201 int fdin[2], fdout[2], fderr[2];
202 int failed_errno = failed_errno;
205 * In case of errors we must keep the promise to close FDs
206 * that have been passed in via ->in and ->out.
209 need_in = !cmd->no_stdin && cmd->in < 0;
210 if (need_in) {
211 if (pipe(fdin) < 0) {
212 failed_errno = errno;
213 if (cmd->out > 0)
214 close(cmd->out);
215 goto fail_pipe;
217 cmd->in = fdin[1];
220 need_out = !cmd->no_stdout
221 && !cmd->stdout_to_stderr
222 && cmd->out < 0;
223 if (need_out) {
224 if (pipe(fdout) < 0) {
225 failed_errno = errno;
226 if (need_in)
227 close_pair(fdin);
228 else if (cmd->in)
229 close(cmd->in);
230 goto fail_pipe;
232 cmd->out = fdout[0];
235 need_err = !cmd->no_stderr && cmd->err < 0;
236 if (need_err) {
237 if (pipe(fderr) < 0) {
238 failed_errno = errno;
239 if (need_in)
240 close_pair(fdin);
241 else if (cmd->in)
242 close(cmd->in);
243 if (need_out)
244 close_pair(fdout);
245 else if (cmd->out)
246 close(cmd->out);
247 fail_pipe:
248 error("cannot create pipe for %s: %s",
249 cmd->argv[0], strerror(failed_errno));
250 errno = failed_errno;
251 return -1;
253 cmd->err = fderr[0];
256 trace_argv_printf(cmd->argv, "trace: run_command:");
257 fflush(NULL);
259 #ifndef WIN32
261 int notify_pipe[2];
262 if (pipe(notify_pipe))
263 notify_pipe[0] = notify_pipe[1] = -1;
265 cmd->pid = fork();
266 if (!cmd->pid) {
268 * Redirect the channel to write syscall error messages to
269 * before redirecting the process's stderr so that all die()
270 * in subsequent call paths use the parent's stderr.
272 if (cmd->no_stderr || need_err) {
273 child_err = dup(2);
274 set_cloexec(child_err);
276 set_die_routine(die_child);
277 set_error_routine(error_child);
279 close(notify_pipe[0]);
280 set_cloexec(notify_pipe[1]);
281 child_notifier = notify_pipe[1];
282 atexit(notify_parent);
284 if (cmd->no_stdin)
285 dup_devnull(0);
286 else if (need_in) {
287 dup2(fdin[0], 0);
288 close_pair(fdin);
289 } else if (cmd->in) {
290 dup2(cmd->in, 0);
291 close(cmd->in);
294 if (cmd->no_stderr)
295 dup_devnull(2);
296 else if (need_err) {
297 dup2(fderr[1], 2);
298 close_pair(fderr);
299 } else if (cmd->err > 1) {
300 dup2(cmd->err, 2);
301 close(cmd->err);
304 if (cmd->no_stdout)
305 dup_devnull(1);
306 else if (cmd->stdout_to_stderr)
307 dup2(2, 1);
308 else if (need_out) {
309 dup2(fdout[1], 1);
310 close_pair(fdout);
311 } else if (cmd->out > 1) {
312 dup2(cmd->out, 1);
313 close(cmd->out);
316 if (cmd->dir && chdir(cmd->dir))
317 die_errno("exec '%s': cd to '%s' failed", cmd->argv[0],
318 cmd->dir);
319 if (cmd->env) {
320 for (; *cmd->env; cmd->env++) {
321 if (strchr(*cmd->env, '='))
322 putenv((char *)*cmd->env);
323 else
324 unsetenv(*cmd->env);
327 if (cmd->preexec_cb) {
329 * We cannot predict what the pre-exec callback does.
330 * Forgo parent notification.
332 close(child_notifier);
333 child_notifier = -1;
335 cmd->preexec_cb();
337 if (cmd->git_cmd) {
338 execv_git_cmd(cmd->argv);
339 } else if (cmd->use_shell) {
340 execv_shell_cmd(cmd->argv);
341 } else {
342 execvp(cmd->argv[0], (char *const*) cmd->argv);
344 if (errno == ENOENT) {
345 if (!cmd->silent_exec_failure)
346 error("cannot run %s: %s", cmd->argv[0],
347 strerror(ENOENT));
348 exit(127);
349 } else {
350 die_errno("cannot exec '%s'", cmd->argv[0]);
353 if (cmd->pid < 0)
354 error("cannot fork() for %s: %s", cmd->argv[0],
355 strerror(failed_errno = errno));
356 else if (cmd->clean_on_exit)
357 mark_child_for_cleanup(cmd->pid);
360 * Wait for child's execvp. If the execvp succeeds (or if fork()
361 * failed), EOF is seen immediately by the parent. Otherwise, the
362 * child process sends a single byte.
363 * Note that use of this infrastructure is completely advisory,
364 * therefore, we keep error checks minimal.
366 close(notify_pipe[1]);
367 if (read(notify_pipe[0], &notify_pipe[1], 1) == 1) {
369 * At this point we know that fork() succeeded, but execvp()
370 * failed. Errors have been reported to our stderr.
372 wait_or_whine(cmd->pid, cmd->argv[0],
373 cmd->silent_exec_failure);
374 failed_errno = errno;
375 cmd->pid = -1;
377 close(notify_pipe[0]);
380 #else
382 int fhin = 0, fhout = 1, fherr = 2;
383 const char **sargv = cmd->argv;
385 if (cmd->no_stdin)
386 fhin = open("/dev/null", O_RDWR);
387 else if (need_in)
388 fhin = dup(fdin[0]);
389 else if (cmd->in)
390 fhin = dup(cmd->in);
392 if (cmd->no_stderr)
393 fherr = open("/dev/null", O_RDWR);
394 else if (need_err)
395 fherr = dup(fderr[1]);
396 else if (cmd->err > 2)
397 fherr = dup(cmd->err);
399 if (cmd->no_stdout)
400 fhout = open("/dev/null", O_RDWR);
401 else if (cmd->stdout_to_stderr)
402 fhout = dup(fherr);
403 else if (need_out)
404 fhout = dup(fdout[1]);
405 else if (cmd->out > 1)
406 fhout = dup(cmd->out);
408 if (cmd->git_cmd) {
409 cmd->argv = prepare_git_cmd(cmd->argv);
410 } else if (cmd->use_shell) {
411 cmd->argv = prepare_shell_cmd(cmd->argv);
414 cmd->pid = mingw_spawnvpe(cmd->argv[0], cmd->argv, (char**) cmd->env,
415 cmd->dir, fhin, fhout, fherr);
416 failed_errno = errno;
417 if (cmd->pid < 0 && (!cmd->silent_exec_failure || errno != ENOENT))
418 error("cannot spawn %s: %s", cmd->argv[0], strerror(errno));
419 if (cmd->clean_on_exit && cmd->pid >= 0)
420 mark_child_for_cleanup(cmd->pid);
422 if (cmd->git_cmd)
423 free(cmd->argv);
425 cmd->argv = sargv;
426 if (fhin != 0)
427 close(fhin);
428 if (fhout != 1)
429 close(fhout);
430 if (fherr != 2)
431 close(fherr);
433 #endif
435 if (cmd->pid < 0) {
436 if (need_in)
437 close_pair(fdin);
438 else if (cmd->in)
439 close(cmd->in);
440 if (need_out)
441 close_pair(fdout);
442 else if (cmd->out)
443 close(cmd->out);
444 if (need_err)
445 close_pair(fderr);
446 else if (cmd->err)
447 close(cmd->err);
448 errno = failed_errno;
449 return -1;
452 if (need_in)
453 close(fdin[0]);
454 else if (cmd->in)
455 close(cmd->in);
457 if (need_out)
458 close(fdout[1]);
459 else if (cmd->out)
460 close(cmd->out);
462 if (need_err)
463 close(fderr[1]);
464 else if (cmd->err)
465 close(cmd->err);
467 return 0;
470 int finish_command(struct child_process *cmd)
472 return wait_or_whine(cmd->pid, cmd->argv[0], cmd->silent_exec_failure);
475 int run_command(struct child_process *cmd)
477 int code = start_command(cmd);
478 if (code)
479 return code;
480 return finish_command(cmd);
483 static void prepare_run_command_v_opt(struct child_process *cmd,
484 const char **argv,
485 int opt)
487 memset(cmd, 0, sizeof(*cmd));
488 cmd->argv = argv;
489 cmd->no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
490 cmd->git_cmd = opt & RUN_GIT_CMD ? 1 : 0;
491 cmd->stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
492 cmd->silent_exec_failure = opt & RUN_SILENT_EXEC_FAILURE ? 1 : 0;
493 cmd->use_shell = opt & RUN_USING_SHELL ? 1 : 0;
494 cmd->clean_on_exit = opt & RUN_CLEAN_ON_EXIT ? 1 : 0;
497 int run_command_v_opt(const char **argv, int opt)
499 struct child_process cmd;
500 prepare_run_command_v_opt(&cmd, argv, opt);
501 return run_command(&cmd);
504 int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env)
506 struct child_process cmd;
507 prepare_run_command_v_opt(&cmd, argv, opt);
508 cmd.dir = dir;
509 cmd.env = env;
510 return run_command(&cmd);
513 #ifndef NO_PTHREADS
514 static pthread_t main_thread;
515 static int main_thread_set;
516 static pthread_key_t async_key;
518 static void *run_thread(void *data)
520 struct async *async = data;
521 intptr_t ret;
523 pthread_setspecific(async_key, async);
524 ret = async->proc(async->proc_in, async->proc_out, async->data);
525 return (void *)ret;
528 static NORETURN void die_async(const char *err, va_list params)
530 vreportf("fatal: ", err, params);
532 if (!pthread_equal(main_thread, pthread_self())) {
533 struct async *async = pthread_getspecific(async_key);
534 if (async->proc_in >= 0)
535 close(async->proc_in);
536 if (async->proc_out >= 0)
537 close(async->proc_out);
538 pthread_exit((void *)128);
541 exit(128);
543 #endif
545 int start_async(struct async *async)
547 int need_in, need_out;
548 int fdin[2], fdout[2];
549 int proc_in, proc_out;
551 need_in = async->in < 0;
552 if (need_in) {
553 if (pipe(fdin) < 0) {
554 if (async->out > 0)
555 close(async->out);
556 return error("cannot create pipe: %s", strerror(errno));
558 async->in = fdin[1];
561 need_out = async->out < 0;
562 if (need_out) {
563 if (pipe(fdout) < 0) {
564 if (need_in)
565 close_pair(fdin);
566 else if (async->in)
567 close(async->in);
568 return error("cannot create pipe: %s", strerror(errno));
570 async->out = fdout[0];
573 if (need_in)
574 proc_in = fdin[0];
575 else if (async->in)
576 proc_in = async->in;
577 else
578 proc_in = -1;
580 if (need_out)
581 proc_out = fdout[1];
582 else if (async->out)
583 proc_out = async->out;
584 else
585 proc_out = -1;
587 #ifdef NO_PTHREADS
588 /* Flush stdio before fork() to avoid cloning buffers */
589 fflush(NULL);
591 async->pid = fork();
592 if (async->pid < 0) {
593 error("fork (async) failed: %s", strerror(errno));
594 goto error;
596 if (!async->pid) {
597 if (need_in)
598 close(fdin[1]);
599 if (need_out)
600 close(fdout[0]);
601 exit(!!async->proc(proc_in, proc_out, async->data));
604 mark_child_for_cleanup(async->pid);
606 if (need_in)
607 close(fdin[0]);
608 else if (async->in)
609 close(async->in);
611 if (need_out)
612 close(fdout[1]);
613 else if (async->out)
614 close(async->out);
615 #else
616 if (!main_thread_set) {
618 * We assume that the first time that start_async is called
619 * it is from the main thread.
621 main_thread_set = 1;
622 main_thread = pthread_self();
623 pthread_key_create(&async_key, NULL);
624 set_die_routine(die_async);
627 if (proc_in >= 0)
628 set_cloexec(proc_in);
629 if (proc_out >= 0)
630 set_cloexec(proc_out);
631 async->proc_in = proc_in;
632 async->proc_out = proc_out;
634 int err = pthread_create(&async->tid, NULL, run_thread, async);
635 if (err) {
636 error("cannot create thread: %s", strerror(err));
637 goto error;
640 #endif
641 return 0;
643 error:
644 if (need_in)
645 close_pair(fdin);
646 else if (async->in)
647 close(async->in);
649 if (need_out)
650 close_pair(fdout);
651 else if (async->out)
652 close(async->out);
653 return -1;
656 int finish_async(struct async *async)
658 #ifdef NO_PTHREADS
659 return wait_or_whine(async->pid, "child process", 0);
660 #else
661 void *ret = (void *)(intptr_t)(-1);
663 if (pthread_join(async->tid, &ret))
664 error("pthread_join failed");
665 return (int)(intptr_t)ret;
666 #endif
669 int run_hook(const char *index_file, const char *name, ...)
671 struct child_process hook;
672 struct argv_array argv = ARGV_ARRAY_INIT;
673 const char *p, *env[2];
674 char index[PATH_MAX];
675 va_list args;
676 int ret;
678 if (access(git_path("hooks/%s", name), X_OK) < 0)
679 return 0;
681 va_start(args, name);
682 argv_array_push(&argv, git_path("hooks/%s", name));
683 while ((p = va_arg(args, const char *)))
684 argv_array_push(&argv, p);
685 va_end(args);
687 memset(&hook, 0, sizeof(hook));
688 hook.argv = argv.argv;
689 hook.no_stdin = 1;
690 hook.stdout_to_stderr = 1;
691 if (index_file) {
692 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
693 env[0] = index;
694 env[1] = NULL;
695 hook.env = env;
698 ret = run_command(&hook);
699 argv_array_clear(&argv);
700 return ret;