Win32: patch Windows environment on startup
[git/mingw/4msysgit/kblees.git] / run-command.c
blobb6c2126549fa403eb05bbb60ced7d7751d742535
1 #include "cache.h"
2 #include "run-command.h"
3 #include "exec_cmd.h"
4 #include "argv-array.h"
6 static inline void close_pair(int fd[2])
8 close(fd[0]);
9 close(fd[1]);
12 #ifndef WIN32
13 static inline void dup_devnull(int to)
15 int fd = open("/dev/null", O_RDWR);
16 dup2(fd, to);
17 close(fd);
19 #endif
21 static const char **prepare_shell_cmd(const char **argv)
23 int argc, nargc = 0;
24 const char **nargv;
26 for (argc = 0; argv[argc]; argc++)
27 ; /* just counting */
28 /* +1 for NULL, +3 for "sh -c" plus extra $0 */
29 nargv = xmalloc(sizeof(*nargv) * (argc + 1 + 3));
31 if (argc < 1)
32 die("BUG: shell command is empty");
34 if (strcspn(argv[0], "|&;<>()$`\\\"' \t\n*?[#~=%") != strlen(argv[0])) {
35 nargv[nargc++] = "sh";
36 nargv[nargc++] = "-c";
38 if (argc < 2)
39 nargv[nargc++] = argv[0];
40 else {
41 struct strbuf arg0 = STRBUF_INIT;
42 strbuf_addf(&arg0, "%s \"$@\"", argv[0]);
43 nargv[nargc++] = strbuf_detach(&arg0, NULL);
47 for (argc = 0; argv[argc]; argc++)
48 nargv[nargc++] = argv[argc];
49 nargv[nargc] = NULL;
51 return nargv;
54 #ifndef WIN32
55 static int execv_shell_cmd(const char **argv)
57 const char **nargv = prepare_shell_cmd(argv);
58 trace_argv_printf(nargv, "trace: exec:");
59 execvp(nargv[0], (char **)nargv);
60 free(nargv);
61 return -1;
63 #endif
65 #ifndef WIN32
66 static int child_err = 2;
67 static int child_notifier = -1;
69 static void notify_parent(void)
72 * execvp failed. If possible, we'd like to let start_command
73 * know, so failures like ENOENT can be handled right away; but
74 * otherwise, finish_command will still report the error.
76 xwrite(child_notifier, "", 1);
79 static NORETURN void die_child(const char *err, va_list params)
81 vwritef(child_err, "fatal: ", err, params);
82 exit(128);
85 static void error_child(const char *err, va_list params)
87 vwritef(child_err, "error: ", err, params);
89 #endif
91 static inline void set_cloexec(int fd)
93 int flags = fcntl(fd, F_GETFD);
94 if (flags >= 0)
95 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
98 static int wait_or_whine(pid_t pid, const char *argv0, int silent_exec_failure)
100 int status, code = -1;
101 pid_t waiting;
102 int failed_errno = 0;
104 while ((waiting = waitpid(pid, &status, 0)) < 0 && errno == EINTR)
105 ; /* nothing */
107 if (waiting < 0) {
108 failed_errno = errno;
109 error("waitpid for %s failed: %s", argv0, strerror(errno));
110 } else if (waiting != pid) {
111 error("waitpid is confused (%s)", argv0);
112 } else if (WIFSIGNALED(status)) {
113 code = WTERMSIG(status);
114 error("%s died of signal %d", argv0, code);
116 * This return value is chosen so that code & 0xff
117 * mimics the exit code that a POSIX shell would report for
118 * a program that died from this signal.
120 code -= 128;
121 } else if (WIFEXITED(status)) {
122 code = WEXITSTATUS(status);
124 * Convert special exit code when execvp failed.
126 if (code == 127) {
127 code = -1;
128 failed_errno = ENOENT;
130 } else {
131 error("waitpid is confused (%s)", argv0);
133 errno = failed_errno;
134 return code;
137 int start_command(struct child_process *cmd)
139 int need_in, need_out, need_err;
140 int fdin[2], fdout[2], fderr[2];
141 int failed_errno = failed_errno;
144 * In case of errors we must keep the promise to close FDs
145 * that have been passed in via ->in and ->out.
148 need_in = !cmd->no_stdin && cmd->in < 0;
149 if (need_in) {
150 if (pipe(fdin) < 0) {
151 failed_errno = errno;
152 if (cmd->out > 0)
153 close(cmd->out);
154 goto fail_pipe;
156 cmd->in = fdin[1];
159 need_out = !cmd->no_stdout
160 && !cmd->stdout_to_stderr
161 && cmd->out < 0;
162 if (need_out) {
163 if (pipe(fdout) < 0) {
164 failed_errno = errno;
165 if (need_in)
166 close_pair(fdin);
167 else if (cmd->in)
168 close(cmd->in);
169 goto fail_pipe;
171 cmd->out = fdout[0];
174 need_err = !cmd->no_stderr && cmd->err < 0;
175 if (need_err) {
176 if (pipe(fderr) < 0) {
177 failed_errno = errno;
178 if (need_in)
179 close_pair(fdin);
180 else if (cmd->in)
181 close(cmd->in);
182 if (need_out)
183 close_pair(fdout);
184 else if (cmd->out)
185 close(cmd->out);
186 fail_pipe:
187 error("cannot create pipe for %s: %s",
188 cmd->argv[0], strerror(failed_errno));
189 errno = failed_errno;
190 return -1;
192 cmd->err = fderr[0];
195 trace_argv_printf(cmd->argv, "trace: run_command:");
196 fflush(NULL);
198 #ifndef WIN32
200 int notify_pipe[2];
201 if (pipe(notify_pipe))
202 notify_pipe[0] = notify_pipe[1] = -1;
204 cmd->pid = fork();
205 if (!cmd->pid) {
207 * Redirect the channel to write syscall error messages to
208 * before redirecting the process's stderr so that all die()
209 * in subsequent call paths use the parent's stderr.
211 if (cmd->no_stderr || need_err) {
212 child_err = dup(2);
213 set_cloexec(child_err);
215 set_die_routine(die_child);
216 set_error_routine(error_child);
218 close(notify_pipe[0]);
219 set_cloexec(notify_pipe[1]);
220 child_notifier = notify_pipe[1];
221 atexit(notify_parent);
223 if (cmd->no_stdin)
224 dup_devnull(0);
225 else if (need_in) {
226 dup2(fdin[0], 0);
227 close_pair(fdin);
228 } else if (cmd->in) {
229 dup2(cmd->in, 0);
230 close(cmd->in);
233 if (cmd->no_stderr)
234 dup_devnull(2);
235 else if (need_err) {
236 dup2(fderr[1], 2);
237 close_pair(fderr);
238 } else if (cmd->err > 1) {
239 dup2(cmd->err, 2);
240 close(cmd->err);
243 if (cmd->no_stdout)
244 dup_devnull(1);
245 else if (cmd->stdout_to_stderr)
246 dup2(2, 1);
247 else if (need_out) {
248 dup2(fdout[1], 1);
249 close_pair(fdout);
250 } else if (cmd->out > 1) {
251 dup2(cmd->out, 1);
252 close(cmd->out);
255 if (cmd->dir && chdir(cmd->dir))
256 die_errno("exec '%s': cd to '%s' failed", cmd->argv[0],
257 cmd->dir);
258 if (cmd->env) {
259 for (; *cmd->env; cmd->env++) {
260 if (strchr(*cmd->env, '='))
261 putenv((char *)*cmd->env);
262 else
263 unsetenv(*cmd->env);
266 if (cmd->preexec_cb) {
268 * We cannot predict what the pre-exec callback does.
269 * Forgo parent notification.
271 close(child_notifier);
272 child_notifier = -1;
274 cmd->preexec_cb();
276 if (cmd->git_cmd) {
277 execv_git_cmd(cmd->argv);
278 } else if (cmd->use_shell) {
279 execv_shell_cmd(cmd->argv);
280 } else {
281 execvp(cmd->argv[0], (char *const*) cmd->argv);
283 if (errno == ENOENT) {
284 if (!cmd->silent_exec_failure)
285 error("cannot run %s: %s", cmd->argv[0],
286 strerror(ENOENT));
287 exit(127);
288 } else {
289 die_errno("cannot exec '%s'", cmd->argv[0]);
292 if (cmd->pid < 0)
293 error("cannot fork() for %s: %s", cmd->argv[0],
294 strerror(failed_errno = errno));
297 * Wait for child's execvp. If the execvp succeeds (or if fork()
298 * failed), EOF is seen immediately by the parent. Otherwise, the
299 * child process sends a single byte.
300 * Note that use of this infrastructure is completely advisory,
301 * therefore, we keep error checks minimal.
303 close(notify_pipe[1]);
304 if (read(notify_pipe[0], &notify_pipe[1], 1) == 1) {
306 * At this point we know that fork() succeeded, but execvp()
307 * failed. Errors have been reported to our stderr.
309 wait_or_whine(cmd->pid, cmd->argv[0],
310 cmd->silent_exec_failure);
311 failed_errno = errno;
312 cmd->pid = -1;
314 close(notify_pipe[0]);
316 #else
318 int fhin = 0, fhout = 1, fherr = 2;
319 const char **sargv = cmd->argv;
321 if (cmd->no_stdin)
322 fhin = open("/dev/null", O_RDWR);
323 else if (need_in)
324 fhin = dup(fdin[0]);
325 else if (cmd->in)
326 fhin = dup(cmd->in);
328 if (cmd->no_stderr)
329 fherr = open("/dev/null", O_RDWR);
330 else if (need_err)
331 fherr = dup(fderr[1]);
332 else if (cmd->err > 2)
333 fherr = dup(cmd->err);
335 if (cmd->no_stdout)
336 fhout = open("/dev/null", O_RDWR);
337 else if (cmd->stdout_to_stderr)
338 fhout = dup(fherr);
339 else if (need_out)
340 fhout = dup(fdout[1]);
341 else if (cmd->out > 1)
342 fhout = dup(cmd->out);
344 if (cmd->git_cmd) {
345 cmd->argv = prepare_git_cmd(cmd->argv);
346 } else if (cmd->use_shell) {
347 cmd->argv = prepare_shell_cmd(cmd->argv);
350 cmd->pid = mingw_spawnvpe(cmd->argv[0], cmd->argv, (char**) cmd->env,
351 cmd->dir, fhin, fhout, fherr);
352 failed_errno = errno;
353 if (cmd->pid < 0 && (!cmd->silent_exec_failure || errno != ENOENT))
354 error("cannot spawn %s: %s", cmd->argv[0], strerror(errno));
356 if (cmd->git_cmd)
357 free(cmd->argv);
359 cmd->argv = sargv;
360 if (fhin != 0)
361 close(fhin);
362 if (fhout != 1)
363 close(fhout);
364 if (fherr != 2)
365 close(fherr);
367 #endif
369 if (cmd->pid < 0) {
370 if (need_in)
371 close_pair(fdin);
372 else if (cmd->in)
373 close(cmd->in);
374 if (need_out)
375 close_pair(fdout);
376 else if (cmd->out)
377 close(cmd->out);
378 if (need_err)
379 close_pair(fderr);
380 else if (cmd->err)
381 close(cmd->err);
382 errno = failed_errno;
383 return -1;
386 if (need_in)
387 close(fdin[0]);
388 else if (cmd->in)
389 close(cmd->in);
391 if (need_out)
392 close(fdout[1]);
393 else if (cmd->out)
394 close(cmd->out);
396 if (need_err)
397 close(fderr[1]);
398 else if (cmd->err)
399 close(cmd->err);
401 return 0;
404 int finish_command(struct child_process *cmd)
406 return wait_or_whine(cmd->pid, cmd->argv[0], cmd->silent_exec_failure);
409 int run_command(struct child_process *cmd)
411 int code = start_command(cmd);
412 if (code)
413 return code;
414 return finish_command(cmd);
417 static void prepare_run_command_v_opt(struct child_process *cmd,
418 const char **argv,
419 int opt)
421 memset(cmd, 0, sizeof(*cmd));
422 cmd->argv = argv;
423 cmd->no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
424 cmd->git_cmd = opt & RUN_GIT_CMD ? 1 : 0;
425 cmd->stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
426 cmd->silent_exec_failure = opt & RUN_SILENT_EXEC_FAILURE ? 1 : 0;
427 cmd->use_shell = opt & RUN_USING_SHELL ? 1 : 0;
430 int run_command_v_opt(const char **argv, int opt)
432 struct child_process cmd;
433 prepare_run_command_v_opt(&cmd, argv, opt);
434 return run_command(&cmd);
437 int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env)
439 struct child_process cmd;
440 prepare_run_command_v_opt(&cmd, argv, opt);
441 cmd.dir = dir;
442 cmd.env = env;
443 return run_command(&cmd);
446 #ifndef NO_PTHREADS
447 static pthread_t main_thread;
448 static int main_thread_set;
449 static pthread_key_t async_key;
451 static void *run_thread(void *data)
453 struct async *async = data;
454 intptr_t ret;
456 pthread_setspecific(async_key, async);
457 ret = async->proc(async->proc_in, async->proc_out, async->data);
458 return (void *)ret;
461 static NORETURN void die_async(const char *err, va_list params)
463 vreportf("fatal: ", err, params);
465 if (!pthread_equal(main_thread, pthread_self())) {
466 struct async *async = pthread_getspecific(async_key);
467 if (async->proc_in >= 0)
468 close(async->proc_in);
469 if (async->proc_out >= 0)
470 close(async->proc_out);
471 pthread_exit((void *)128);
474 exit(128);
476 #endif
478 int start_async(struct async *async)
480 int need_in, need_out;
481 int fdin[2], fdout[2];
482 int proc_in, proc_out;
484 need_in = async->in < 0;
485 if (need_in) {
486 if (pipe(fdin) < 0) {
487 if (async->out > 0)
488 close(async->out);
489 return error("cannot create pipe: %s", strerror(errno));
491 async->in = fdin[1];
494 need_out = async->out < 0;
495 if (need_out) {
496 if (pipe(fdout) < 0) {
497 if (need_in)
498 close_pair(fdin);
499 else if (async->in)
500 close(async->in);
501 return error("cannot create pipe: %s", strerror(errno));
503 async->out = fdout[0];
506 if (need_in)
507 proc_in = fdin[0];
508 else if (async->in)
509 proc_in = async->in;
510 else
511 proc_in = -1;
513 if (need_out)
514 proc_out = fdout[1];
515 else if (async->out)
516 proc_out = async->out;
517 else
518 proc_out = -1;
520 #ifdef NO_PTHREADS
521 /* Flush stdio before fork() to avoid cloning buffers */
522 fflush(NULL);
524 async->pid = fork();
525 if (async->pid < 0) {
526 error("fork (async) failed: %s", strerror(errno));
527 goto error;
529 if (!async->pid) {
530 if (need_in)
531 close(fdin[1]);
532 if (need_out)
533 close(fdout[0]);
534 exit(!!async->proc(proc_in, proc_out, async->data));
537 if (need_in)
538 close(fdin[0]);
539 else if (async->in)
540 close(async->in);
542 if (need_out)
543 close(fdout[1]);
544 else if (async->out)
545 close(async->out);
546 #else
547 if (!main_thread_set) {
549 * We assume that the first time that start_async is called
550 * it is from the main thread.
552 main_thread_set = 1;
553 main_thread = pthread_self();
554 pthread_key_create(&async_key, NULL);
555 set_die_routine(die_async);
558 if (proc_in >= 0)
559 set_cloexec(proc_in);
560 if (proc_out >= 0)
561 set_cloexec(proc_out);
562 async->proc_in = proc_in;
563 async->proc_out = proc_out;
565 int err = pthread_create(&async->tid, NULL, run_thread, async);
566 if (err) {
567 error("cannot create thread: %s", strerror(err));
568 goto error;
571 #endif
572 return 0;
574 error:
575 if (need_in)
576 close_pair(fdin);
577 else if (async->in)
578 close(async->in);
580 if (need_out)
581 close_pair(fdout);
582 else if (async->out)
583 close(async->out);
584 return -1;
587 int finish_async(struct async *async)
589 #ifdef NO_PTHREADS
590 return wait_or_whine(async->pid, "child process", 0);
591 #else
592 void *ret = (void *)(intptr_t)(-1);
594 if (pthread_join(async->tid, &ret))
595 error("pthread_join failed");
596 return (int)(intptr_t)ret;
597 #endif
600 int run_hook(const char *index_file, const char *name, ...)
602 struct child_process hook;
603 struct argv_array argv = ARGV_ARRAY_INIT;
604 const char *p, *env[2];
605 char index[PATH_MAX];
606 va_list args;
607 int ret;
609 if (access(git_path("hooks/%s", name), X_OK) < 0)
610 return 0;
612 va_start(args, name);
613 argv_array_push(&argv, git_path("hooks/%s", name));
614 while ((p = va_arg(args, const char *)))
615 argv_array_push(&argv, p);
616 va_end(args);
618 memset(&hook, 0, sizeof(hook));
619 hook.argv = argv.argv;
620 hook.no_stdin = 1;
621 hook.stdout_to_stderr = 1;
622 if (index_file) {
623 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
624 env[0] = index;
625 env[1] = NULL;
626 hook.env = env;
629 ret = run_command(&hook);
630 argv_array_clear(&argv);
631 return ret;