start_command: do not clobber cmd->env on Windows code path
[git/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];
5a7a3671 22 int failed_errno = failed_errno;
4919bf03 23
c20181e3
JS
24 /*
25 * In case of errors we must keep the promise to close FDs
26 * that have been passed in via ->in and ->out.
27 */
28
e4507ae8 29 need_in = !cmd->no_stdin && cmd->in < 0;
4919bf03 30 if (need_in) {
c20181e3 31 if (pipe(fdin) < 0) {
0ac77ec3 32 failed_errno = errno;
c20181e3
JS
33 if (cmd->out > 0)
34 close(cmd->out);
0ac77ec3 35 goto fail_pipe;
c20181e3 36 }
4919bf03 37 cmd->in = fdin[1];
4919bf03
SP
38 }
39
e4507ae8
SP
40 need_out = !cmd->no_stdout
41 && !cmd->stdout_to_stderr
42 && cmd->out < 0;
f4bba25b
SP
43 if (need_out) {
44 if (pipe(fdout) < 0) {
0ac77ec3 45 failed_errno = errno;
f4bba25b
SP
46 if (need_in)
47 close_pair(fdin);
c20181e3
JS
48 else if (cmd->in)
49 close(cmd->in);
0ac77ec3 50 goto fail_pipe;
f4bba25b
SP
51 }
52 cmd->out = fdout[0];
f4bba25b
SP
53 }
54
b73a4397 55 need_err = !cmd->no_stderr && cmd->err < 0;
f3b33f1d
JS
56 if (need_err) {
57 if (pipe(fderr) < 0) {
0ac77ec3 58 failed_errno = errno;
f3b33f1d
JS
59 if (need_in)
60 close_pair(fdin);
c20181e3
JS
61 else if (cmd->in)
62 close(cmd->in);
f3b33f1d
JS
63 if (need_out)
64 close_pair(fdout);
c20181e3
JS
65 else if (cmd->out)
66 close(cmd->out);
0ac77ec3
JS
67fail_pipe:
68 error("cannot create pipe for %s: %s",
69 cmd->argv[0], strerror(failed_errno));
70 errno = failed_errno;
71 return -1;
f3b33f1d
JS
72 }
73 cmd->err = fderr[0];
74 }
75
8852f5d7
JS
76 trace_argv_printf(cmd->argv, "trace: run_command:");
77
ba26f296 78#ifndef __MINGW32__
7d0b18a4 79 fflush(NULL);
ebcb5d16 80 cmd->pid = fork();
ebcb5d16 81 if (!cmd->pid) {
e4507ae8
SP
82 if (cmd->no_stdin)
83 dup_devnull(0);
84 else if (need_in) {
4919bf03 85 dup2(fdin[0], 0);
9dc09c76 86 close_pair(fdin);
4919bf03
SP
87 } else if (cmd->in) {
88 dup2(cmd->in, 0);
89 close(cmd->in);
95d3c4f5 90 }
4919bf03 91
ce2cf27a
CC
92 if (cmd->no_stderr)
93 dup_devnull(2);
94 else if (need_err) {
95 dup2(fderr[1], 2);
96 close_pair(fderr);
97 }
98
e4507ae8
SP
99 if (cmd->no_stdout)
100 dup_devnull(1);
101 else if (cmd->stdout_to_stderr)
cd83c74c 102 dup2(2, 1);
f4bba25b
SP
103 else if (need_out) {
104 dup2(fdout[1], 1);
105 close_pair(fdout);
106 } else if (cmd->out > 1) {
107 dup2(cmd->out, 1);
108 close(cmd->out);
109 }
110
1568fea0 111 if (cmd->dir && chdir(cmd->dir))
d824cbba
TR
112 die_errno("exec '%s': cd to '%s' failed", cmd->argv[0],
113 cmd->dir);
ee493148 114 if (cmd->env) {
3427b375
AR
115 for (; *cmd->env; cmd->env++) {
116 if (strchr(*cmd->env, '='))
4b25d091 117 putenv((char *)*cmd->env);
3427b375
AR
118 else
119 unsetenv(*cmd->env);
120 }
ee493148 121 }
ccf08bc3
JK
122 if (cmd->preexec_cb)
123 cmd->preexec_cb();
f1000898
SP
124 if (cmd->git_cmd) {
125 execv_git_cmd(cmd->argv);
77cb17e9 126 } else {
f1000898 127 execvp(cmd->argv[0], (char *const*) cmd->argv);
128aed68 128 }
45c0961c
JK
129 trace_printf("trace: exec '%s' failed: %s\n", cmd->argv[0],
130 strerror(errno));
131 exit(127);
b1bf95bb 132 }
0ac77ec3
JS
133 if (cmd->pid < 0)
134 error("cannot fork() for %s: %s", cmd->argv[0],
135 strerror(failed_errno = errno));
ba26f296
JS
136#else
137 int s0 = -1, s1 = -1, s2 = -1; /* backups of stdin, stdout, stderr */
108ac313 138 const char **sargv = cmd->argv;
ba26f296 139 char **env = environ;
ba26f296
JS
140
141 if (cmd->no_stdin) {
142 s0 = dup(0);
143 dup_devnull(0);
144 } else if (need_in) {
145 s0 = dup(0);
146 dup2(fdin[0], 0);
147 } else if (cmd->in) {
148 s0 = dup(0);
149 dup2(cmd->in, 0);
150 }
151
152 if (cmd->no_stderr) {
153 s2 = dup(2);
154 dup_devnull(2);
155 } else if (need_err) {
156 s2 = dup(2);
157 dup2(fderr[1], 2);
158 }
159
160 if (cmd->no_stdout) {
161 s1 = dup(1);
162 dup_devnull(1);
163 } else if (cmd->stdout_to_stderr) {
164 s1 = dup(1);
165 dup2(2, 1);
166 } else if (need_out) {
167 s1 = dup(1);
168 dup2(fdout[1], 1);
169 } else if (cmd->out > 1) {
170 s1 = dup(1);
171 dup2(cmd->out, 1);
172 }
173
174 if (cmd->dir)
175 die("chdir in start_command() not implemented");
2affea41
JS
176 if (cmd->env)
177 env = make_augmented_environ(cmd->env);
ba26f296
JS
178
179 if (cmd->git_cmd) {
108ac313 180 cmd->argv = prepare_git_cmd(cmd->argv);
ba26f296
JS
181 }
182
7e5d7768 183 cmd->pid = mingw_spawnvpe(cmd->argv[0], cmd->argv, env);
0ac77ec3 184 failed_errno = errno;
c024beb5 185 if (cmd->pid < 0 && (!cmd->silent_exec_failure || errno != ENOENT))
0ac77ec3 186 error("cannot spawn %s: %s", cmd->argv[0], strerror(errno));
ba26f296
JS
187
188 if (cmd->env)
189 free_environ(env);
190 if (cmd->git_cmd)
108ac313 191 free(cmd->argv);
ba26f296 192
108ac313 193 cmd->argv = sargv;
ba26f296
JS
194 if (s0 >= 0)
195 dup2(s0, 0), close(s0);
196 if (s1 >= 0)
197 dup2(s1, 1), close(s1);
198 if (s2 >= 0)
199 dup2(s2, 2), close(s2);
200#endif
201
202 if (cmd->pid < 0) {
203 if (need_in)
204 close_pair(fdin);
205 else if (cmd->in)
206 close(cmd->in);
207 if (need_out)
208 close_pair(fdout);
209 else if (cmd->out)
210 close(cmd->out);
211 if (need_err)
212 close_pair(fderr);
0ac77ec3
JS
213 errno = failed_errno;
214 return -1;
ba26f296 215 }
4919bf03
SP
216
217 if (need_in)
218 close(fdin[0]);
219 else if (cmd->in)
220 close(cmd->in);
221
f4bba25b
SP
222 if (need_out)
223 close(fdout[1]);
c20181e3 224 else if (cmd->out)
f4bba25b
SP
225 close(cmd->out);
226
f3b33f1d
JS
227 if (need_err)
228 close(fderr[1]);
229
ebcb5d16
SP
230 return 0;
231}
232
c024beb5 233static int wait_or_whine(pid_t pid, const char *argv0, int silent_exec_failure)
ebcb5d16 234{
0ac77ec3
JS
235 int status, code = -1;
236 pid_t waiting;
237 int failed_errno = 0;
238
239 while ((waiting = waitpid(pid, &status, 0)) < 0 && errno == EINTR)
240 ; /* nothing */
241
242 if (waiting < 0) {
243 failed_errno = errno;
244 error("waitpid for %s failed: %s", argv0, strerror(errno));
245 } else if (waiting != pid) {
246 error("waitpid is confused (%s)", argv0);
247 } else if (WIFSIGNALED(status)) {
b99d5f40
JS
248 code = WTERMSIG(status);
249 error("%s died of signal %d", argv0, code);
250 /*
251 * This return value is chosen so that code & 0xff
252 * mimics the exit code that a POSIX shell would report for
253 * a program that died from this signal.
254 */
255 code -= 128;
0ac77ec3 256 } else if (WIFEXITED(status)) {
b1bf95bb 257 code = WEXITSTATUS(status);
0ac77ec3
JS
258 /*
259 * Convert special exit code when execvp failed.
260 */
261 if (code == 127) {
262 code = -1;
263 failed_errno = ENOENT;
c024beb5
JS
264 if (!silent_exec_failure)
265 error("cannot run %s: %s", argv0,
266 strerror(ENOENT));
45c0961c 267 }
0ac77ec3
JS
268 } else {
269 error("waitpid is confused (%s)", argv0);
b1bf95bb 270 }
0ac77ec3
JS
271 errno = failed_errno;
272 return code;
b1bf95bb 273}
f1000898 274
2d22c208
JS
275int finish_command(struct child_process *cmd)
276{
c024beb5 277 return wait_or_whine(cmd->pid, cmd->argv[0], cmd->silent_exec_failure);
2d22c208
JS
278}
279
ebcb5d16
SP
280int run_command(struct child_process *cmd)
281{
282 int code = start_command(cmd);
283 if (code)
284 return code;
285 return finish_command(cmd);
286}
287
1568fea0 288static void prepare_run_command_v_opt(struct child_process *cmd,
ee493148
AR
289 const char **argv,
290 int opt)
1568fea0
AR
291{
292 memset(cmd, 0, sizeof(*cmd));
293 cmd->argv = argv;
294 cmd->no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
295 cmd->git_cmd = opt & RUN_GIT_CMD ? 1 : 0;
296 cmd->stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
c024beb5 297 cmd->silent_exec_failure = opt & RUN_SILENT_EXEC_FAILURE ? 1 : 0;
1568fea0
AR
298}
299
f1000898
SP
300int run_command_v_opt(const char **argv, int opt)
301{
302 struct child_process cmd;
1568fea0
AR
303 prepare_run_command_v_opt(&cmd, argv, opt);
304 return run_command(&cmd);
305}
306
ee493148
AR
307int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env)
308{
309 struct child_process cmd;
310 prepare_run_command_v_opt(&cmd, argv, opt);
311 cmd.dir = dir;
312 cmd.env = env;
313 return run_command(&cmd);
314}
2d22c208 315
618ebe9f
JS
316#ifdef __MINGW32__
317static __stdcall unsigned run_thread(void *data)
318{
319 struct async *async = data;
320 return async->proc(async->fd_for_proc, async->data);
321}
322#endif
323
2d22c208
JS
324int start_async(struct async *async)
325{
326 int pipe_out[2];
327
328 if (pipe(pipe_out) < 0)
329 return error("cannot create pipe: %s", strerror(errno));
618ebe9f 330 async->out = pipe_out[0];
2d22c208 331
618ebe9f 332#ifndef __MINGW32__
2c3766f0
AM
333 /* Flush stdio before fork() to avoid cloning buffers */
334 fflush(NULL);
335
2d22c208
JS
336 async->pid = fork();
337 if (async->pid < 0) {
338 error("fork (async) failed: %s", strerror(errno));
339 close_pair(pipe_out);
340 return -1;
341 }
342 if (!async->pid) {
343 close(pipe_out[0]);
344 exit(!!async->proc(pipe_out[1], async->data));
345 }
2d22c208 346 close(pipe_out[1]);
618ebe9f
JS
347#else
348 async->fd_for_proc = pipe_out[1];
349 async->tid = (HANDLE) _beginthreadex(NULL, 0, run_thread, async, 0, NULL);
350 if (!async->tid) {
351 error("cannot create thread: %s", strerror(errno));
352 close_pair(pipe_out);
353 return -1;
354 }
355#endif
2d22c208
JS
356 return 0;
357}
358
359int finish_async(struct async *async)
360{
618ebe9f 361#ifndef __MINGW32__
c024beb5 362 int ret = wait_or_whine(async->pid, "child process", 0);
618ebe9f
JS
363#else
364 DWORD ret = 0;
365 if (WaitForSingleObject(async->tid, INFINITE) != WAIT_OBJECT_0)
366 ret = error("waiting for thread failed: %lu", GetLastError());
367 else if (!GetExitCodeThread(async->tid, &ret))
368 ret = error("cannot get thread exit code: %lu", GetLastError());
369 CloseHandle(async->tid);
370#endif
2d22c208
JS
371 return ret;
372}
ae98a008
SB
373
374int run_hook(const char *index_file, const char *name, ...)
375{
376 struct child_process hook;
14e6298f 377 const char **argv = NULL, *env[2];
ae98a008
SB
378 char index[PATH_MAX];
379 va_list args;
380 int ret;
14e6298f 381 size_t i = 0, alloc = 0;
ae98a008 382
cf94ca8e
SB
383 if (access(git_path("hooks/%s", name), X_OK) < 0)
384 return 0;
385
ae98a008 386 va_start(args, name);
14e6298f
SB
387 ALLOC_GROW(argv, i + 1, alloc);
388 argv[i++] = git_path("hooks/%s", name);
389 while (argv[i-1]) {
390 ALLOC_GROW(argv, i + 1, alloc);
391 argv[i++] = va_arg(args, const char *);
392 }
ae98a008
SB
393 va_end(args);
394
ae98a008
SB
395 memset(&hook, 0, sizeof(hook));
396 hook.argv = argv;
397 hook.no_stdin = 1;
398 hook.stdout_to_stderr = 1;
399 if (index_file) {
400 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
401 env[0] = index;
402 env[1] = NULL;
403 hook.env = env;
404 }
405
0ac77ec3 406 ret = run_command(&hook);
14e6298f 407 free(argv);
ae98a008
SB
408 return ret;
409}