Windows: Work around intermittent failures in mingw_rename
[git/dscho.git] / run-command.c
blob09725b983f0cc08ed064e4da07c1e5c27ddba541
1 #include "cache.h"
2 #include "run-command.h"
3 #include "exec_cmd.h"
5 static inline void close_pair(int fd[2])
7 close(fd[0]);
8 close(fd[1]);
11 static inline void dup_devnull(int to)
13 int fd = open("/dev/null", O_RDWR);
14 dup2(fd, to);
15 close(fd);
18 int start_command(struct child_process *cmd)
20 int need_in, need_out, need_err;
21 int fdin[2], fdout[2], fderr[2];
24 * In case of errors we must keep the promise to close FDs
25 * that have been passed in via ->in and ->out.
28 need_in = !cmd->no_stdin && cmd->in < 0;
29 if (need_in) {
30 if (pipe(fdin) < 0) {
31 if (cmd->out > 0)
32 close(cmd->out);
33 return -ERR_RUN_COMMAND_PIPE;
35 cmd->in = fdin[1];
38 need_out = !cmd->no_stdout
39 && !cmd->stdout_to_stderr
40 && cmd->out < 0;
41 if (need_out) {
42 if (pipe(fdout) < 0) {
43 if (need_in)
44 close_pair(fdin);
45 else if (cmd->in)
46 close(cmd->in);
47 return -ERR_RUN_COMMAND_PIPE;
49 cmd->out = fdout[0];
52 need_err = !cmd->no_stderr && cmd->err < 0;
53 if (need_err) {
54 if (pipe(fderr) < 0) {
55 if (need_in)
56 close_pair(fdin);
57 else if (cmd->in)
58 close(cmd->in);
59 if (need_out)
60 close_pair(fdout);
61 else if (cmd->out)
62 close(cmd->out);
63 return -ERR_RUN_COMMAND_PIPE;
65 cmd->err = fderr[0];
68 trace_argv_printf(cmd->argv, "trace: run_command:");
70 #ifndef __MINGW32__
71 fflush(NULL);
72 cmd->pid = fork();
73 if (!cmd->pid) {
74 if (cmd->no_stdin)
75 dup_devnull(0);
76 else if (need_in) {
77 dup2(fdin[0], 0);
78 close_pair(fdin);
79 } else if (cmd->in) {
80 dup2(cmd->in, 0);
81 close(cmd->in);
84 if (cmd->no_stderr)
85 dup_devnull(2);
86 else if (need_err) {
87 dup2(fderr[1], 2);
88 close_pair(fderr);
91 if (cmd->no_stdout)
92 dup_devnull(1);
93 else if (cmd->stdout_to_stderr)
94 dup2(2, 1);
95 else if (need_out) {
96 dup2(fdout[1], 1);
97 close_pair(fdout);
98 } else if (cmd->out > 1) {
99 dup2(cmd->out, 1);
100 close(cmd->out);
103 if (cmd->dir && chdir(cmd->dir))
104 die("exec %s: cd to %s failed (%s)", cmd->argv[0],
105 cmd->dir, strerror(errno));
106 if (cmd->env) {
107 for (; *cmd->env; cmd->env++) {
108 if (strchr(*cmd->env, '='))
109 putenv((char*)*cmd->env);
110 else
111 unsetenv(*cmd->env);
114 if (cmd->preexec_cb)
115 cmd->preexec_cb();
116 if (cmd->git_cmd) {
117 execv_git_cmd(cmd->argv);
118 } else {
119 execvp(cmd->argv[0], (char *const*) cmd->argv);
121 trace_printf("trace: exec '%s' failed: %s\n", cmd->argv[0],
122 strerror(errno));
123 exit(127);
125 #else
126 int s0 = -1, s1 = -1, s2 = -1; /* backups of stdin, stdout, stderr */
127 const char **sargv = cmd->argv;
128 char **env = environ;
130 if (cmd->no_stdin) {
131 s0 = dup(0);
132 dup_devnull(0);
133 } else if (need_in) {
134 s0 = dup(0);
135 dup2(fdin[0], 0);
136 } else if (cmd->in) {
137 s0 = dup(0);
138 dup2(cmd->in, 0);
141 if (cmd->no_stderr) {
142 s2 = dup(2);
143 dup_devnull(2);
144 } else if (need_err) {
145 s2 = dup(2);
146 dup2(fderr[1], 2);
149 if (cmd->no_stdout) {
150 s1 = dup(1);
151 dup_devnull(1);
152 } else if (cmd->stdout_to_stderr) {
153 s1 = dup(1);
154 dup2(2, 1);
155 } else if (need_out) {
156 s1 = dup(1);
157 dup2(fdout[1], 1);
158 } else if (cmd->out > 1) {
159 s1 = dup(1);
160 dup2(cmd->out, 1);
163 if (cmd->dir)
164 die("chdir in start_command() not implemented");
165 if (cmd->env) {
166 env = copy_environ();
167 for (; *cmd->env; cmd->env++)
168 env = env_setenv(env, *cmd->env);
171 if (cmd->git_cmd) {
172 cmd->argv = prepare_git_cmd(cmd->argv);
175 cmd->pid = mingw_spawnvpe(cmd->argv[0], cmd->argv, env);
177 if (cmd->env)
178 free_environ(env);
179 if (cmd->git_cmd)
180 free(cmd->argv);
182 cmd->argv = sargv;
183 if (s0 >= 0)
184 dup2(s0, 0), close(s0);
185 if (s1 >= 0)
186 dup2(s1, 1), close(s1);
187 if (s2 >= 0)
188 dup2(s2, 2), close(s2);
189 #endif
191 if (cmd->pid < 0) {
192 int err = errno;
193 if (need_in)
194 close_pair(fdin);
195 else if (cmd->in)
196 close(cmd->in);
197 if (need_out)
198 close_pair(fdout);
199 else if (cmd->out)
200 close(cmd->out);
201 if (need_err)
202 close_pair(fderr);
203 return err == ENOENT ?
204 -ERR_RUN_COMMAND_EXEC :
205 -ERR_RUN_COMMAND_FORK;
208 if (need_in)
209 close(fdin[0]);
210 else if (cmd->in)
211 close(cmd->in);
213 if (need_out)
214 close(fdout[1]);
215 else if (cmd->out)
216 close(cmd->out);
218 if (need_err)
219 close(fderr[1]);
221 return 0;
224 static int wait_or_whine(pid_t pid)
226 for (;;) {
227 int status, code;
228 pid_t waiting = waitpid(pid, &status, 0);
230 if (waiting < 0) {
231 if (errno == EINTR)
232 continue;
233 error("waitpid failed (%s)", strerror(errno));
234 return -ERR_RUN_COMMAND_WAITPID;
236 if (waiting != pid)
237 return -ERR_RUN_COMMAND_WAITPID_WRONG_PID;
238 if (WIFSIGNALED(status))
239 return -ERR_RUN_COMMAND_WAITPID_SIGNAL;
241 if (!WIFEXITED(status))
242 return -ERR_RUN_COMMAND_WAITPID_NOEXIT;
243 code = WEXITSTATUS(status);
244 switch (code) {
245 case 127:
246 return -ERR_RUN_COMMAND_EXEC;
247 case 0:
248 return 0;
249 default:
250 return -code;
255 int finish_command(struct child_process *cmd)
257 return wait_or_whine(cmd->pid);
260 int run_command(struct child_process *cmd)
262 int code = start_command(cmd);
263 if (code)
264 return code;
265 return finish_command(cmd);
268 static void prepare_run_command_v_opt(struct child_process *cmd,
269 const char **argv,
270 int opt)
272 memset(cmd, 0, sizeof(*cmd));
273 cmd->argv = argv;
274 cmd->no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
275 cmd->git_cmd = opt & RUN_GIT_CMD ? 1 : 0;
276 cmd->stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
279 int run_command_v_opt(const char **argv, int opt)
281 struct child_process cmd;
282 prepare_run_command_v_opt(&cmd, argv, opt);
283 return run_command(&cmd);
286 int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env)
288 struct child_process cmd;
289 prepare_run_command_v_opt(&cmd, argv, opt);
290 cmd.dir = dir;
291 cmd.env = env;
292 return run_command(&cmd);
295 #ifdef __MINGW32__
296 static int close_fd = -1;
297 NORETURN void die_from_thread(const char *err, va_list params)
299 vreport("fatal: ", err, params);
300 if (close_fd >= 0)
301 close(close_fd);
302 _endthreadex(128);
303 exit(128); /* silence compiler */
306 static __stdcall unsigned run_thread(void *data)
308 struct async *async = data;
309 close_fd = async->fd_for_proc;
310 extern NORETURN void die_from_thread(const char *err, va_list params);
311 set_die_routine(die_from_thread);
312 return async->proc(async->fd_for_proc, async->data);
314 #endif
316 int start_async(struct async *async)
318 int pipe_out[2];
320 if (pipe(pipe_out) < 0)
321 return error("cannot create pipe: %s", strerror(errno));
322 async->out = pipe_out[0];
324 #ifndef __MINGW32__
325 /* Flush stdio before fork() to avoid cloning buffers */
326 fflush(NULL);
328 async->pid = fork();
329 if (async->pid < 0) {
330 error("fork (async) failed: %s", strerror(errno));
331 close_pair(pipe_out);
332 return -1;
334 if (!async->pid) {
335 close(pipe_out[0]);
336 exit(!!async->proc(pipe_out[1], async->data));
338 close(pipe_out[1]);
339 #else
340 async->fd_for_proc = pipe_out[1];
341 async->tid = (HANDLE) _beginthreadex(NULL, 0, run_thread, async, 0, NULL);
342 if (!async->tid) {
343 error("cannot create thread: %s", strerror(errno));
344 close_pair(pipe_out);
345 return -1;
347 #endif
348 return 0;
351 int finish_async(struct async *async)
353 #ifndef __MINGW32__
354 int ret = 0;
356 if (wait_or_whine(async->pid))
357 ret = error("waitpid (async) failed");
358 #else
359 DWORD ret = 0;
360 if (WaitForSingleObject(async->tid, INFINITE) != WAIT_OBJECT_0)
361 ret = error("waiting for thread failed: %lu", GetLastError());
362 else if (!GetExitCodeThread(async->tid, &ret))
363 ret = error("cannot get thread exit code: %lu", GetLastError());
364 CloseHandle(async->tid);
365 if (ret)
366 /* test suite tests for this string */
367 error("waitpid (async) failed");
368 #endif
369 return ret;
372 int run_hook(const char *index_file, const char *name, ...)
374 struct child_process hook;
375 const char **argv = NULL, *env[2];
376 char index[PATH_MAX];
377 va_list args;
378 int ret;
379 size_t i = 0, alloc = 0;
381 if (access(git_path("hooks/%s", name), X_OK) < 0)
382 return 0;
384 va_start(args, name);
385 ALLOC_GROW(argv, i + 1, alloc);
386 argv[i++] = git_path("hooks/%s", name);
387 while (argv[i-1]) {
388 ALLOC_GROW(argv, i + 1, alloc);
389 argv[i++] = va_arg(args, const char *);
391 va_end(args);
393 memset(&hook, 0, sizeof(hook));
394 hook.argv = argv;
395 hook.no_stdin = 1;
396 hook.stdout_to_stderr = 1;
397 if (index_file) {
398 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
399 env[0] = index;
400 env[1] = NULL;
401 hook.env = env;
404 ret = start_command(&hook);
405 free(argv);
406 if (ret) {
407 warning("Could not spawn %s", argv[0]);
408 return ret;
410 ret = finish_command(&hook);
411 if (ret == -ERR_RUN_COMMAND_WAITPID_SIGNAL)
412 warning("%s exited due to uncaught signal", argv[0]);
414 return ret;