builtin-bundle: add a --basis option that specifies a basis
[git/pieter.git] / run-command.c
blobb7514f1719a16197cc40694a476ecceaa74a034a
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 cmd->pid = fork();
72 if (!cmd->pid) {
73 if (cmd->no_stdin)
74 dup_devnull(0);
75 else if (need_in) {
76 dup2(fdin[0], 0);
77 close_pair(fdin);
78 } else if (cmd->in) {
79 dup2(cmd->in, 0);
80 close(cmd->in);
83 if (cmd->no_stderr)
84 dup_devnull(2);
85 else if (need_err) {
86 dup2(fderr[1], 2);
87 close_pair(fderr);
90 if (cmd->no_stdout)
91 dup_devnull(1);
92 else if (cmd->stdout_to_stderr)
93 dup2(2, 1);
94 else if (need_out) {
95 dup2(fdout[1], 1);
96 close_pair(fdout);
97 } else if (cmd->out > 1) {
98 dup2(cmd->out, 1);
99 close(cmd->out);
102 if (cmd->dir && chdir(cmd->dir))
103 die("exec %s: cd to %s failed (%s)", cmd->argv[0],
104 cmd->dir, strerror(errno));
105 if (cmd->env) {
106 for (; *cmd->env; cmd->env++) {
107 if (strchr(*cmd->env, '='))
108 putenv((char*)*cmd->env);
109 else
110 unsetenv(*cmd->env);
113 if (cmd->preexec_cb)
114 cmd->preexec_cb();
115 if (cmd->git_cmd) {
116 execv_git_cmd(cmd->argv);
117 } else {
118 execvp(cmd->argv[0], (char *const*) cmd->argv);
120 die("exec %s failed.", cmd->argv[0]);
122 #else
123 int s0 = -1, s1 = -1, s2 = -1; /* backups of stdin, stdout, stderr */
124 const char **sargv = cmd->argv;
125 char **env = environ;
127 if (cmd->no_stdin) {
128 s0 = dup(0);
129 dup_devnull(0);
130 } else if (need_in) {
131 s0 = dup(0);
132 dup2(fdin[0], 0);
133 } else if (cmd->in) {
134 s0 = dup(0);
135 dup2(cmd->in, 0);
138 if (cmd->no_stderr) {
139 s2 = dup(2);
140 dup_devnull(2);
141 } else if (need_err) {
142 s2 = dup(2);
143 dup2(fderr[1], 2);
146 if (cmd->no_stdout) {
147 s1 = dup(1);
148 dup_devnull(1);
149 } else if (cmd->stdout_to_stderr) {
150 s1 = dup(1);
151 dup2(2, 1);
152 } else if (need_out) {
153 s1 = dup(1);
154 dup2(fdout[1], 1);
155 } else if (cmd->out > 1) {
156 s1 = dup(1);
157 dup2(cmd->out, 1);
160 if (cmd->dir)
161 die("chdir in start_command() not implemented");
162 if (cmd->env) {
163 env = copy_environ();
164 for (; *cmd->env; cmd->env++)
165 env = env_setenv(env, *cmd->env);
168 if (cmd->git_cmd) {
169 cmd->argv = prepare_git_cmd(cmd->argv);
172 cmd->pid = mingw_spawnvpe(cmd->argv[0], cmd->argv, env);
174 if (cmd->env)
175 free_environ(env);
176 if (cmd->git_cmd)
177 free(cmd->argv);
179 cmd->argv = sargv;
180 if (s0 >= 0)
181 dup2(s0, 0), close(s0);
182 if (s1 >= 0)
183 dup2(s1, 1), close(s1);
184 if (s2 >= 0)
185 dup2(s2, 2), close(s2);
186 #endif
188 if (cmd->pid < 0) {
189 if (need_in)
190 close_pair(fdin);
191 else if (cmd->in)
192 close(cmd->in);
193 if (need_out)
194 close_pair(fdout);
195 else if (cmd->out)
196 close(cmd->out);
197 if (need_err)
198 close_pair(fderr);
199 return -ERR_RUN_COMMAND_FORK;
202 if (need_in)
203 close(fdin[0]);
204 else if (cmd->in)
205 close(cmd->in);
207 if (need_out)
208 close(fdout[1]);
209 else if (cmd->out)
210 close(cmd->out);
212 if (need_err)
213 close(fderr[1]);
215 return 0;
218 static int wait_or_whine(pid_t pid)
220 for (;;) {
221 int status, code;
222 pid_t waiting = waitpid(pid, &status, 0);
224 if (waiting < 0) {
225 if (errno == EINTR)
226 continue;
227 error("waitpid failed (%s)", strerror(errno));
228 return -ERR_RUN_COMMAND_WAITPID;
230 if (waiting != pid)
231 return -ERR_RUN_COMMAND_WAITPID_WRONG_PID;
232 if (WIFSIGNALED(status))
233 return -ERR_RUN_COMMAND_WAITPID_SIGNAL;
235 if (!WIFEXITED(status))
236 return -ERR_RUN_COMMAND_WAITPID_NOEXIT;
237 code = WEXITSTATUS(status);
238 if (code)
239 return -code;
240 return 0;
244 int finish_command(struct child_process *cmd)
246 return wait_or_whine(cmd->pid);
249 int run_command(struct child_process *cmd)
251 int code = start_command(cmd);
252 if (code)
253 return code;
254 return finish_command(cmd);
257 static void prepare_run_command_v_opt(struct child_process *cmd,
258 const char **argv,
259 int opt)
261 memset(cmd, 0, sizeof(*cmd));
262 cmd->argv = argv;
263 cmd->no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
264 cmd->git_cmd = opt & RUN_GIT_CMD ? 1 : 0;
265 cmd->stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
268 int run_command_v_opt(const char **argv, int opt)
270 struct child_process cmd;
271 prepare_run_command_v_opt(&cmd, argv, opt);
272 return run_command(&cmd);
275 int run_command_v_opt_cd(const char **argv, int opt, const char *dir)
277 struct child_process cmd;
278 prepare_run_command_v_opt(&cmd, argv, opt);
279 cmd.dir = dir;
280 return run_command(&cmd);
283 int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env)
285 struct child_process cmd;
286 prepare_run_command_v_opt(&cmd, argv, opt);
287 cmd.dir = dir;
288 cmd.env = env;
289 return run_command(&cmd);
292 #ifdef __MINGW32__
293 static __stdcall unsigned run_thread(void *data)
295 struct async *async = data;
296 return async->proc(async->fd_for_proc, async->data);
298 #endif
300 int start_async(struct async *async)
302 int pipe_out[2];
304 if (pipe(pipe_out) < 0)
305 return error("cannot create pipe: %s", strerror(errno));
306 async->out = pipe_out[0];
308 #ifndef __MINGW32__
309 /* Flush stdio before fork() to avoid cloning buffers */
310 fflush(NULL);
312 async->pid = fork();
313 if (async->pid < 0) {
314 error("fork (async) failed: %s", strerror(errno));
315 close_pair(pipe_out);
316 return -1;
318 if (!async->pid) {
319 close(pipe_out[0]);
320 exit(!!async->proc(pipe_out[1], async->data));
322 close(pipe_out[1]);
323 #else
324 async->fd_for_proc = pipe_out[1];
325 async->tid = (HANDLE) _beginthreadex(NULL, 0, run_thread, async, 0, NULL);
326 if (!async->tid) {
327 error("cannot create thread: %s", strerror(errno));
328 close_pair(pipe_out);
329 return -1;
331 #endif
332 return 0;
335 int finish_async(struct async *async)
337 #ifndef __MINGW32__
338 int ret = 0;
340 if (wait_or_whine(async->pid))
341 ret = error("waitpid (async) failed");
342 #else
343 DWORD ret = 0;
344 if (WaitForSingleObject(async->tid, INFINITE) != WAIT_OBJECT_0)
345 ret = error("waiting for thread failed: %lu", GetLastError());
346 else if (!GetExitCodeThread(async->tid, &ret))
347 ret = error("cannot get thread exit code: %lu", GetLastError());
348 CloseHandle(async->tid);
349 #endif
350 return ret;