Remove now unused spawn-pipe.c with spawnvpe_pipe() function.
[git/dscho.git] / run-command.c
blob05934f708f0849fc2035739f9a03e9b6797c2e94
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];
23 need_in = !cmd->no_stdin && cmd->in < 0;
24 if (need_in) {
25 if (pipe(fdin) < 0)
26 return -ERR_RUN_COMMAND_PIPE;
27 cmd->in = fdin[1];
28 cmd->close_in = 1;
31 need_out = !cmd->no_stdout
32 && !cmd->stdout_to_stderr
33 && cmd->out < 0;
34 if (need_out) {
35 if (pipe(fdout) < 0) {
36 if (need_in)
37 close_pair(fdin);
38 return -ERR_RUN_COMMAND_PIPE;
40 cmd->out = fdout[0];
41 cmd->close_out = 1;
44 need_err = !cmd->no_stderr && cmd->err < 0;
45 if (need_err) {
46 if (pipe(fderr) < 0) {
47 if (need_in)
48 close_pair(fdin);
49 if (need_out)
50 close_pair(fdout);
51 return -ERR_RUN_COMMAND_PIPE;
53 cmd->err = fderr[0];
56 #ifndef __MINGW32__
57 cmd->pid = fork();
58 if (!cmd->pid) {
59 if (cmd->no_stdin)
60 dup_devnull(0);
61 else if (need_in) {
62 dup2(fdin[0], 0);
63 close_pair(fdin);
64 } else if (cmd->in) {
65 dup2(cmd->in, 0);
66 close(cmd->in);
69 if (cmd->no_stdout)
70 dup_devnull(1);
71 else if (cmd->stdout_to_stderr)
72 dup2(2, 1);
73 else if (need_out) {
74 dup2(fdout[1], 1);
75 close_pair(fdout);
76 } else if (cmd->out > 1) {
77 dup2(cmd->out, 1);
78 close(cmd->out);
81 if (cmd->no_stderr)
82 dup_devnull(2);
83 else if (need_err) {
84 dup2(fderr[1], 2);
85 close_pair(fderr);
88 if (cmd->dir && chdir(cmd->dir))
89 die("exec %s: cd to %s failed (%s)", cmd->argv[0],
90 cmd->dir, strerror(errno));
91 if (cmd->env) {
92 for (; *cmd->env; cmd->env++) {
93 if (strchr(*cmd->env, '='))
94 putenv((char*)*cmd->env);
95 else
96 unsetenv(*cmd->env);
99 if (cmd->git_cmd) {
100 execv_git_cmd(cmd->argv);
101 } else {
102 execvp(cmd->argv[0], (char *const*) cmd->argv);
104 die("exec %s failed.", cmd->argv[0]);
106 #else
107 int s0 = -1, s1 = -1, s2 = -1; /* backups of stdin, stdout, stderr */
108 const char *sargv0 = cmd->argv[0];
109 char **env = environ;
110 struct strbuf git_cmd;
112 if (cmd->no_stdin) {
113 s0 = dup(0);
114 dup_devnull(0);
115 } else if (need_in) {
116 s0 = dup(0);
117 dup2(fdin[0], 0);
118 } else if (cmd->in) {
119 s0 = dup(0);
120 dup2(cmd->in, 0);
123 if (cmd->no_stdout) {
124 s1 = dup(1);
125 dup_devnull(1);
126 } else if (cmd->stdout_to_stderr) {
127 s1 = dup(1);
128 dup2(2, 1);
129 } else if (need_out) {
130 s1 = dup(1);
131 dup2(fdout[1], 1);
132 } else if (cmd->out > 1) {
133 s1 = dup(1);
134 dup2(cmd->out, 1);
137 if (cmd->no_stderr) {
138 s2 = dup(2);
139 dup_devnull(2);
140 } else if (need_err) {
141 s2 = dup(2);
142 dup2(fderr[1], 2);
145 if (cmd->dir)
146 die("chdir in start_command() not implemented");
147 if (cmd->env) {
148 env = copy_environ();
149 for (; *cmd->env; cmd->env++) {
150 if (strchr(*cmd->env, '='))
151 die("setting environment in start_command() not implemented");
152 else
153 env_unsetenv(env, *cmd->env);
157 if (cmd->git_cmd) {
158 strbuf_init(&git_cmd, 0);
159 strbuf_addf(&git_cmd, "git-%s", cmd->argv[0]);
160 cmd->argv[0] = git_cmd.buf;
163 char **path = mingw_get_path_split();
164 const char *argv0 = cmd->argv[0];
165 const char **qargv;
166 char *prog = mingw_path_lookup(argv0, path);
167 const char *interpr = parse_interpreter(prog);
168 int argc;
170 for (argc = 0; cmd->argv[argc];) argc++;
171 qargv = xmalloc((argc+2)*sizeof(char*));
172 if (!interpr) {
173 quote_argv(qargv, cmd->argv);
174 cmd->pid = spawnve(_P_NOWAIT, prog, qargv, (const char **)env);
175 } else {
176 qargv[0] = interpr;
177 cmd->argv[0] = prog;
178 quote_argv(&qargv[1], cmd->argv);
179 cmd->pid = spawnvpe(_P_NOWAIT, interpr, qargv, (const char **)env);
182 free(qargv); /* TODO: quoted args should be freed, too */
183 free(prog);
185 mingw_free_path_split(path);
186 /* TODO: if (cmd->env) free env; */
188 if (cmd->git_cmd)
189 strbuf_release(&git_cmd);
191 cmd->argv[0] = sargv0;
192 if (s0 >= 0)
193 dup2(s0, 0), close(s0);
194 if (s1 >= 0)
195 dup2(s1, 1), close(s1);
196 if (s2 >= 0)
197 dup2(s2, 2), close(s2);
198 #endif
200 if (cmd->pid < 0) {
201 if (need_in)
202 close_pair(fdin);
203 if (need_out)
204 close_pair(fdout);
205 if (need_err)
206 close_pair(fderr);
207 return -ERR_RUN_COMMAND_FORK;
210 if (need_in)
211 close(fdin[0]);
212 else if (cmd->in)
213 close(cmd->in);
215 if (need_out)
216 close(fdout[1]);
217 else if (cmd->out > 1)
218 close(cmd->out);
220 if (need_err)
221 close(fderr[1]);
223 return 0;
226 static int wait_or_whine(pid_t pid)
228 for (;;) {
229 int status, code;
230 pid_t waiting = waitpid(pid, &status, 0);
232 if (waiting < 0) {
233 if (errno == EINTR)
234 continue;
235 error("waitpid failed (%s)", strerror(errno));
236 return -ERR_RUN_COMMAND_WAITPID;
238 if (waiting != pid)
239 return -ERR_RUN_COMMAND_WAITPID_WRONG_PID;
240 if (WIFSIGNALED(status))
241 return -ERR_RUN_COMMAND_WAITPID_SIGNAL;
243 if (!WIFEXITED(status))
244 return -ERR_RUN_COMMAND_WAITPID_NOEXIT;
245 code = WEXITSTATUS(status);
246 if (code)
247 return -code;
248 return 0;
252 int finish_command(struct child_process *cmd)
254 if (cmd->close_in)
255 close(cmd->in);
256 if (cmd->close_out)
257 close(cmd->out);
258 return wait_or_whine(cmd->pid);
261 int run_command(struct child_process *cmd)
263 int code = start_command(cmd);
264 if (code)
265 return code;
266 return finish_command(cmd);
269 static void prepare_run_command_v_opt(struct child_process *cmd,
270 const char **argv,
271 int opt)
273 memset(cmd, 0, sizeof(*cmd));
274 cmd->argv = argv;
275 cmd->no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
276 cmd->git_cmd = opt & RUN_GIT_CMD ? 1 : 0;
277 cmd->stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
280 int run_command_v_opt(const char **argv, int opt)
282 struct child_process cmd;
283 prepare_run_command_v_opt(&cmd, argv, opt);
284 return run_command(&cmd);
287 int run_command_v_opt_cd(const char **argv, int opt, const char *dir)
289 struct child_process cmd;
290 prepare_run_command_v_opt(&cmd, argv, opt);
291 cmd.dir = dir;
292 return run_command(&cmd);
295 int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env)
297 struct child_process cmd;
298 prepare_run_command_v_opt(&cmd, argv, opt);
299 cmd.dir = dir;
300 cmd.env = env;
301 return run_command(&cmd);
304 #ifdef __MINGW32__
305 static __stdcall unsigned run_thread(void *data)
307 struct async *async = data;
308 return async->proc(async->fd_for_proc, async->data);
310 #endif
312 int start_async(struct async *async)
314 int pipe_out[2];
316 if (pipe(pipe_out) < 0)
317 return error("cannot create pipe: %s", strerror(errno));
318 async->out = pipe_out[0];
320 #ifndef __MINGW32__
321 async->pid = fork();
322 if (async->pid < 0) {
323 error("fork (async) failed: %s", strerror(errno));
324 close_pair(pipe_out);
325 return -1;
327 if (!async->pid) {
328 close(pipe_out[0]);
329 exit(!!async->proc(pipe_out[1], async->data));
331 close(pipe_out[1]);
332 #else
333 async->fd_for_proc = pipe_out[1];
334 async->tid = (HANDLE) _beginthreadex(NULL, 0, run_thread, async, 0, NULL);
335 if (!async->tid) {
336 error("cannot create thread: %s", strerror(errno));
337 close_pair(pipe_out);
338 return -1;
340 #endif
341 return 0;
344 int finish_async(struct async *async)
346 #ifndef __MINGW32__
347 int ret = 0;
349 if (wait_or_whine(async->pid))
350 ret = error("waitpid (async) failed");
351 #else
352 DWORD ret = 0;
353 if (WaitForSingleObject(async->tid, INFINITE) != WAIT_OBJECT_0)
354 ret = error("waiting for thread failed: %lu", GetLastError());
355 else if (!GetExitCodeThread(async->tid, &ret))
356 ret = error("cannot get thread exit code: %lu", GetLastError());
357 CloseHandle(async->tid);
358 #endif
359 return ret;