Merge branch 'master' of git://git.kernel.org/pub/scm/git/git
[git/jnareb-git.git] / run-command.c
blobc90cdc50e3165bcdb798c85b2dc7b929a9b0a144
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 die("exec %s failed.", cmd->argv[0]);
123 #else
124 int s0 = -1, s1 = -1, s2 = -1; /* backups of stdin, stdout, stderr */
125 const char **sargv = cmd->argv;
126 char **env = environ;
128 if (cmd->no_stdin) {
129 s0 = dup(0);
130 dup_devnull(0);
131 } else if (need_in) {
132 s0 = dup(0);
133 dup2(fdin[0], 0);
134 } else if (cmd->in) {
135 s0 = dup(0);
136 dup2(cmd->in, 0);
139 if (cmd->no_stderr) {
140 s2 = dup(2);
141 dup_devnull(2);
142 } else if (need_err) {
143 s2 = dup(2);
144 dup2(fderr[1], 2);
147 if (cmd->no_stdout) {
148 s1 = dup(1);
149 dup_devnull(1);
150 } else if (cmd->stdout_to_stderr) {
151 s1 = dup(1);
152 dup2(2, 1);
153 } else if (need_out) {
154 s1 = dup(1);
155 dup2(fdout[1], 1);
156 } else if (cmd->out > 1) {
157 s1 = dup(1);
158 dup2(cmd->out, 1);
161 if (cmd->dir)
162 die("chdir in start_command() not implemented");
163 if (cmd->env) {
164 env = copy_environ();
165 for (; *cmd->env; cmd->env++)
166 env = env_setenv(env, *cmd->env);
169 if (cmd->git_cmd) {
170 cmd->argv = prepare_git_cmd(cmd->argv);
173 cmd->pid = mingw_spawnvpe(cmd->argv[0], cmd->argv, env);
175 if (cmd->env)
176 free_environ(env);
177 if (cmd->git_cmd)
178 free(cmd->argv);
180 cmd->argv = sargv;
181 if (s0 >= 0)
182 dup2(s0, 0), close(s0);
183 if (s1 >= 0)
184 dup2(s1, 1), close(s1);
185 if (s2 >= 0)
186 dup2(s2, 2), close(s2);
187 #endif
189 if (cmd->pid < 0) {
190 if (need_in)
191 close_pair(fdin);
192 else if (cmd->in)
193 close(cmd->in);
194 if (need_out)
195 close_pair(fdout);
196 else if (cmd->out)
197 close(cmd->out);
198 if (need_err)
199 close_pair(fderr);
200 return -ERR_RUN_COMMAND_FORK;
203 if (need_in)
204 close(fdin[0]);
205 else if (cmd->in)
206 close(cmd->in);
208 if (need_out)
209 close(fdout[1]);
210 else if (cmd->out)
211 close(cmd->out);
213 if (need_err)
214 close(fderr[1]);
216 return 0;
219 static int wait_or_whine(pid_t pid)
221 for (;;) {
222 int status, code;
223 pid_t waiting = waitpid(pid, &status, 0);
225 if (waiting < 0) {
226 if (errno == EINTR)
227 continue;
228 error("waitpid failed (%s)", strerror(errno));
229 return -ERR_RUN_COMMAND_WAITPID;
231 if (waiting != pid)
232 return -ERR_RUN_COMMAND_WAITPID_WRONG_PID;
233 if (WIFSIGNALED(status))
234 return -ERR_RUN_COMMAND_WAITPID_SIGNAL;
236 if (!WIFEXITED(status))
237 return -ERR_RUN_COMMAND_WAITPID_NOEXIT;
238 code = WEXITSTATUS(status);
239 if (code)
240 return -code;
241 return 0;
245 int finish_command(struct child_process *cmd)
247 return wait_or_whine(cmd->pid);
250 int run_command(struct child_process *cmd)
252 int code = start_command(cmd);
253 if (code)
254 return code;
255 return finish_command(cmd);
258 static void prepare_run_command_v_opt(struct child_process *cmd,
259 const char **argv,
260 int opt)
262 memset(cmd, 0, sizeof(*cmd));
263 cmd->argv = argv;
264 cmd->no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
265 cmd->git_cmd = opt & RUN_GIT_CMD ? 1 : 0;
266 cmd->stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
269 int run_command_v_opt(const char **argv, int opt)
271 struct child_process cmd;
272 prepare_run_command_v_opt(&cmd, argv, opt);
273 return run_command(&cmd);
276 int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env)
278 struct child_process cmd;
279 prepare_run_command_v_opt(&cmd, argv, opt);
280 cmd.dir = dir;
281 cmd.env = env;
282 return run_command(&cmd);
285 #ifdef __MINGW32__
286 static __stdcall unsigned run_thread(void *data)
288 struct async *async = data;
289 return async->proc(async->fd_for_proc, async->data);
291 #endif
293 int start_async(struct async *async)
295 int pipe_out[2];
297 if (pipe(pipe_out) < 0)
298 return error("cannot create pipe: %s", strerror(errno));
299 async->out = pipe_out[0];
301 #ifndef __MINGW32__
302 /* Flush stdio before fork() to avoid cloning buffers */
303 fflush(NULL);
305 async->pid = fork();
306 if (async->pid < 0) {
307 error("fork (async) failed: %s", strerror(errno));
308 close_pair(pipe_out);
309 return -1;
311 if (!async->pid) {
312 close(pipe_out[0]);
313 exit(!!async->proc(pipe_out[1], async->data));
315 close(pipe_out[1]);
316 #else
317 async->fd_for_proc = pipe_out[1];
318 async->tid = (HANDLE) _beginthreadex(NULL, 0, run_thread, async, 0, NULL);
319 if (!async->tid) {
320 error("cannot create thread: %s", strerror(errno));
321 close_pair(pipe_out);
322 return -1;
324 #endif
325 return 0;
328 int finish_async(struct async *async)
330 #ifndef __MINGW32__
331 int ret = 0;
333 if (wait_or_whine(async->pid))
334 ret = error("waitpid (async) failed");
335 #else
336 DWORD ret = 0;
337 if (WaitForSingleObject(async->tid, INFINITE) != WAIT_OBJECT_0)
338 ret = error("waiting for thread failed: %lu", GetLastError());
339 else if (!GetExitCodeThread(async->tid, &ret))
340 ret = error("cannot get thread exit code: %lu", GetLastError());
341 CloseHandle(async->tid);
342 #endif
343 return ret;