]> git.ipfire.org Git - thirdparty/git.git/blame - run-command.c
Windows: Disambiguate DOS style paths from SSH URLs.
[thirdparty/git.git] / run-command.c
CommitLineData
b1bf95bb
JW
1#include "cache.h"
2#include "run-command.h"
77cb17e9 3#include "exec_cmd.h"
b1bf95bb 4
9dc09c76
SP
5static inline void close_pair(int fd[2])
6{
7 close(fd[0]);
8 close(fd[1]);
9}
10
e4507ae8
SP
11static inline void dup_devnull(int to)
12{
13 int fd = open("/dev/null", O_RDWR);
14 dup2(fd, to);
15 close(fd);
16}
17
ebcb5d16 18int start_command(struct child_process *cmd)
b1bf95bb 19{
f3b33f1d
JS
20 int need_in, need_out, need_err;
21 int fdin[2], fdout[2], fderr[2];
4919bf03 22
c20181e3
JS
23 /*
24 * In case of errors we must keep the promise to close FDs
25 * that have been passed in via ->in and ->out.
26 */
27
e4507ae8 28 need_in = !cmd->no_stdin && cmd->in < 0;
4919bf03 29 if (need_in) {
c20181e3
JS
30 if (pipe(fdin) < 0) {
31 if (cmd->out > 0)
32 close(cmd->out);
4919bf03 33 return -ERR_RUN_COMMAND_PIPE;
c20181e3 34 }
4919bf03 35 cmd->in = fdin[1];
4919bf03
SP
36 }
37
e4507ae8
SP
38 need_out = !cmd->no_stdout
39 && !cmd->stdout_to_stderr
40 && cmd->out < 0;
f4bba25b
SP
41 if (need_out) {
42 if (pipe(fdout) < 0) {
43 if (need_in)
44 close_pair(fdin);
c20181e3
JS
45 else if (cmd->in)
46 close(cmd->in);
f4bba25b
SP
47 return -ERR_RUN_COMMAND_PIPE;
48 }
49 cmd->out = fdout[0];
f4bba25b
SP
50 }
51
b73a4397 52 need_err = !cmd->no_stderr && cmd->err < 0;
f3b33f1d
JS
53 if (need_err) {
54 if (pipe(fderr) < 0) {
55 if (need_in)
56 close_pair(fdin);
c20181e3
JS
57 else if (cmd->in)
58 close(cmd->in);
f3b33f1d
JS
59 if (need_out)
60 close_pair(fdout);
c20181e3
JS
61 else if (cmd->out)
62 close(cmd->out);
f3b33f1d
JS
63 return -ERR_RUN_COMMAND_PIPE;
64 }
65 cmd->err = fderr[0];
66 }
67
ba26f296 68#ifndef __MINGW32__
ebcb5d16 69 cmd->pid = fork();
ebcb5d16 70 if (!cmd->pid) {
e4507ae8
SP
71 if (cmd->no_stdin)
72 dup_devnull(0);
73 else if (need_in) {
4919bf03 74 dup2(fdin[0], 0);
9dc09c76 75 close_pair(fdin);
4919bf03
SP
76 } else if (cmd->in) {
77 dup2(cmd->in, 0);
78 close(cmd->in);
95d3c4f5 79 }
4919bf03 80
ce2cf27a
CC
81 if (cmd->no_stderr)
82 dup_devnull(2);
83 else if (need_err) {
84 dup2(fderr[1], 2);
85 close_pair(fderr);
86 }
87
e4507ae8
SP
88 if (cmd->no_stdout)
89 dup_devnull(1);
90 else if (cmd->stdout_to_stderr)
cd83c74c 91 dup2(2, 1);
f4bba25b
SP
92 else if (need_out) {
93 dup2(fdout[1], 1);
94 close_pair(fdout);
95 } else if (cmd->out > 1) {
96 dup2(cmd->out, 1);
97 close(cmd->out);
98 }
99
1568fea0
AR
100 if (cmd->dir && chdir(cmd->dir))
101 die("exec %s: cd to %s failed (%s)", cmd->argv[0],
102 cmd->dir, strerror(errno));
ee493148 103 if (cmd->env) {
3427b375
AR
104 for (; *cmd->env; cmd->env++) {
105 if (strchr(*cmd->env, '='))
106 putenv((char*)*cmd->env);
107 else
108 unsetenv(*cmd->env);
109 }
ee493148 110 }
f1000898
SP
111 if (cmd->git_cmd) {
112 execv_git_cmd(cmd->argv);
77cb17e9 113 } else {
f1000898 114 execvp(cmd->argv[0], (char *const*) cmd->argv);
128aed68 115 }
f1000898 116 die("exec %s failed.", cmd->argv[0]);
b1bf95bb 117 }
ba26f296
JS
118#else
119 int s0 = -1, s1 = -1, s2 = -1; /* backups of stdin, stdout, stderr */
120 const char *sargv0 = cmd->argv[0];
121 char **env = environ;
122 struct strbuf git_cmd;
123
124 if (cmd->no_stdin) {
125 s0 = dup(0);
126 dup_devnull(0);
127 } else if (need_in) {
128 s0 = dup(0);
129 dup2(fdin[0], 0);
130 } else if (cmd->in) {
131 s0 = dup(0);
132 dup2(cmd->in, 0);
133 }
134
135 if (cmd->no_stderr) {
136 s2 = dup(2);
137 dup_devnull(2);
138 } else if (need_err) {
139 s2 = dup(2);
140 dup2(fderr[1], 2);
141 }
142
143 if (cmd->no_stdout) {
144 s1 = dup(1);
145 dup_devnull(1);
146 } else if (cmd->stdout_to_stderr) {
147 s1 = dup(1);
148 dup2(2, 1);
149 } else if (need_out) {
150 s1 = dup(1);
151 dup2(fdout[1], 1);
152 } else if (cmd->out > 1) {
153 s1 = dup(1);
154 dup2(cmd->out, 1);
155 }
156
157 if (cmd->dir)
158 die("chdir in start_command() not implemented");
159 if (cmd->env) {
160 env = copy_environ();
161 for (; *cmd->env; cmd->env++)
162 env = env_setenv(env, *cmd->env);
163 }
164
165 if (cmd->git_cmd) {
166 strbuf_init(&git_cmd, 0);
167 strbuf_addf(&git_cmd, "git-%s", cmd->argv[0]);
168 cmd->argv[0] = git_cmd.buf;
169 }
170
171 cmd->pid = spawnvpe(_P_NOWAIT, cmd->argv[0], cmd->argv, (const char **)env);
172
173 if (cmd->env)
174 free_environ(env);
175 if (cmd->git_cmd)
176 strbuf_release(&git_cmd);
177
178 cmd->argv[0] = sargv0;
179 if (s0 >= 0)
180 dup2(s0, 0), close(s0);
181 if (s1 >= 0)
182 dup2(s1, 1), close(s1);
183 if (s2 >= 0)
184 dup2(s2, 2), close(s2);
185#endif
186
187 if (cmd->pid < 0) {
188 if (need_in)
189 close_pair(fdin);
190 else if (cmd->in)
191 close(cmd->in);
192 if (need_out)
193 close_pair(fdout);
194 else if (cmd->out)
195 close(cmd->out);
196 if (need_err)
197 close_pair(fderr);
198 return -ERR_RUN_COMMAND_FORK;
199 }
4919bf03
SP
200
201 if (need_in)
202 close(fdin[0]);
203 else if (cmd->in)
204 close(cmd->in);
205
f4bba25b
SP
206 if (need_out)
207 close(fdout[1]);
c20181e3 208 else if (cmd->out)
f4bba25b
SP
209 close(cmd->out);
210
f3b33f1d
JS
211 if (need_err)
212 close(fderr[1]);
213
ebcb5d16
SP
214 return 0;
215}
216
2d22c208 217static int wait_or_whine(pid_t pid)
ebcb5d16 218{
b1bf95bb
JW
219 for (;;) {
220 int status, code;
2d22c208 221 pid_t waiting = waitpid(pid, &status, 0);
b1bf95bb 222
6f002f98 223 if (waiting < 0) {
b1bf95bb
JW
224 if (errno == EINTR)
225 continue;
6f002f98 226 error("waitpid failed (%s)", strerror(errno));
b1bf95bb
JW
227 return -ERR_RUN_COMMAND_WAITPID;
228 }
2d22c208 229 if (waiting != pid)
b1bf95bb
JW
230 return -ERR_RUN_COMMAND_WAITPID_WRONG_PID;
231 if (WIFSIGNALED(status))
232 return -ERR_RUN_COMMAND_WAITPID_SIGNAL;
233
234 if (!WIFEXITED(status))
235 return -ERR_RUN_COMMAND_WAITPID_NOEXIT;
236 code = WEXITSTATUS(status);
237 if (code)
238 return -code;
239 return 0;
240 }
241}
f1000898 242
2d22c208
JS
243int finish_command(struct child_process *cmd)
244{
2d22c208
JS
245 return wait_or_whine(cmd->pid);
246}
247
ebcb5d16
SP
248int run_command(struct child_process *cmd)
249{
250 int code = start_command(cmd);
251 if (code)
252 return code;
253 return finish_command(cmd);
254}
255
1568fea0 256static void prepare_run_command_v_opt(struct child_process *cmd,
ee493148
AR
257 const char **argv,
258 int opt)
1568fea0
AR
259{
260 memset(cmd, 0, sizeof(*cmd));
261 cmd->argv = argv;
262 cmd->no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
263 cmd->git_cmd = opt & RUN_GIT_CMD ? 1 : 0;
264 cmd->stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
265}
266
f1000898
SP
267int run_command_v_opt(const char **argv, int opt)
268{
269 struct child_process cmd;
1568fea0
AR
270 prepare_run_command_v_opt(&cmd, argv, opt);
271 return run_command(&cmd);
272}
273
274int run_command_v_opt_cd(const char **argv, int opt, const char *dir)
275{
276 struct child_process cmd;
277 prepare_run_command_v_opt(&cmd, argv, opt);
278 cmd.dir = dir;
f1000898
SP
279 return run_command(&cmd);
280}
ee493148
AR
281
282int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env)
283{
284 struct child_process cmd;
285 prepare_run_command_v_opt(&cmd, argv, opt);
286 cmd.dir = dir;
287 cmd.env = env;
288 return run_command(&cmd);
289}
2d22c208
JS
290
291int start_async(struct async *async)
292{
293 int pipe_out[2];
294
295 if (pipe(pipe_out) < 0)
296 return error("cannot create pipe: %s", strerror(errno));
297
298 async->pid = fork();
299 if (async->pid < 0) {
300 error("fork (async) failed: %s", strerror(errno));
301 close_pair(pipe_out);
302 return -1;
303 }
304 if (!async->pid) {
305 close(pipe_out[0]);
306 exit(!!async->proc(pipe_out[1], async->data));
307 }
308 async->out = pipe_out[0];
309 close(pipe_out[1]);
310 return 0;
311}
312
313int finish_async(struct async *async)
314{
315 int ret = 0;
316
317 if (wait_or_whine(async->pid))
318 ret = error("waitpid (async) failed");
319 return ret;
320}