Windows: simplify the pipe(2) implementation
[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
71064e3f 78#ifndef WIN32
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 136#else
0d30ad71 137{
ba26f296 138 int s0 = -1, s1 = -1, s2 = -1; /* backups of stdin, stdout, stderr */
108ac313 139 const char **sargv = cmd->argv;
ba26f296 140 char **env = environ;
ba26f296
JS
141
142 if (cmd->no_stdin) {
143 s0 = dup(0);
144 dup_devnull(0);
145 } else if (need_in) {
146 s0 = dup(0);
147 dup2(fdin[0], 0);
148 } else if (cmd->in) {
149 s0 = dup(0);
150 dup2(cmd->in, 0);
151 }
152
153 if (cmd->no_stderr) {
154 s2 = dup(2);
155 dup_devnull(2);
156 } else if (need_err) {
157 s2 = dup(2);
158 dup2(fderr[1], 2);
159 }
160
161 if (cmd->no_stdout) {
162 s1 = dup(1);
163 dup_devnull(1);
164 } else if (cmd->stdout_to_stderr) {
165 s1 = dup(1);
166 dup2(2, 1);
167 } else if (need_out) {
168 s1 = dup(1);
169 dup2(fdout[1], 1);
170 } else if (cmd->out > 1) {
171 s1 = dup(1);
172 dup2(cmd->out, 1);
173 }
174
175 if (cmd->dir)
176 die("chdir in start_command() not implemented");
2affea41
JS
177 if (cmd->env)
178 env = make_augmented_environ(cmd->env);
ba26f296
JS
179
180 if (cmd->git_cmd) {
108ac313 181 cmd->argv = prepare_git_cmd(cmd->argv);
ba26f296
JS
182 }
183
7e5d7768 184 cmd->pid = mingw_spawnvpe(cmd->argv[0], cmd->argv, env);
0ac77ec3 185 failed_errno = errno;
c024beb5 186 if (cmd->pid < 0 && (!cmd->silent_exec_failure || errno != ENOENT))
0ac77ec3 187 error("cannot spawn %s: %s", cmd->argv[0], strerror(errno));
ba26f296
JS
188
189 if (cmd->env)
190 free_environ(env);
191 if (cmd->git_cmd)
108ac313 192 free(cmd->argv);
ba26f296 193
108ac313 194 cmd->argv = sargv;
ba26f296
JS
195 if (s0 >= 0)
196 dup2(s0, 0), close(s0);
197 if (s1 >= 0)
198 dup2(s1, 1), close(s1);
199 if (s2 >= 0)
200 dup2(s2, 2), close(s2);
0d30ad71 201}
ba26f296
JS
202#endif
203
204 if (cmd->pid < 0) {
205 if (need_in)
206 close_pair(fdin);
207 else if (cmd->in)
208 close(cmd->in);
209 if (need_out)
210 close_pair(fdout);
211 else if (cmd->out)
212 close(cmd->out);
213 if (need_err)
214 close_pair(fderr);
0ac77ec3
JS
215 errno = failed_errno;
216 return -1;
ba26f296 217 }
4919bf03
SP
218
219 if (need_in)
220 close(fdin[0]);
221 else if (cmd->in)
222 close(cmd->in);
223
f4bba25b
SP
224 if (need_out)
225 close(fdout[1]);
c20181e3 226 else if (cmd->out)
f4bba25b
SP
227 close(cmd->out);
228
f3b33f1d
JS
229 if (need_err)
230 close(fderr[1]);
231
ebcb5d16
SP
232 return 0;
233}
234
c024beb5 235static int wait_or_whine(pid_t pid, const char *argv0, int silent_exec_failure)
ebcb5d16 236{
0ac77ec3
JS
237 int status, code = -1;
238 pid_t waiting;
239 int failed_errno = 0;
240
241 while ((waiting = waitpid(pid, &status, 0)) < 0 && errno == EINTR)
242 ; /* nothing */
243
244 if (waiting < 0) {
245 failed_errno = errno;
246 error("waitpid for %s failed: %s", argv0, strerror(errno));
247 } else if (waiting != pid) {
248 error("waitpid is confused (%s)", argv0);
249 } else if (WIFSIGNALED(status)) {
b99d5f40
JS
250 code = WTERMSIG(status);
251 error("%s died of signal %d", argv0, code);
252 /*
253 * This return value is chosen so that code & 0xff
254 * mimics the exit code that a POSIX shell would report for
255 * a program that died from this signal.
256 */
257 code -= 128;
0ac77ec3 258 } else if (WIFEXITED(status)) {
b1bf95bb 259 code = WEXITSTATUS(status);
0ac77ec3
JS
260 /*
261 * Convert special exit code when execvp failed.
262 */
263 if (code == 127) {
264 code = -1;
265 failed_errno = ENOENT;
c024beb5
JS
266 if (!silent_exec_failure)
267 error("cannot run %s: %s", argv0,
268 strerror(ENOENT));
45c0961c 269 }
0ac77ec3
JS
270 } else {
271 error("waitpid is confused (%s)", argv0);
b1bf95bb 272 }
0ac77ec3
JS
273 errno = failed_errno;
274 return code;
b1bf95bb 275}
f1000898 276
2d22c208
JS
277int finish_command(struct child_process *cmd)
278{
c024beb5 279 return wait_or_whine(cmd->pid, cmd->argv[0], cmd->silent_exec_failure);
2d22c208
JS
280}
281
ebcb5d16
SP
282int run_command(struct child_process *cmd)
283{
284 int code = start_command(cmd);
285 if (code)
286 return code;
287 return finish_command(cmd);
288}
289
1568fea0 290static void prepare_run_command_v_opt(struct child_process *cmd,
ee493148
AR
291 const char **argv,
292 int opt)
1568fea0
AR
293{
294 memset(cmd, 0, sizeof(*cmd));
295 cmd->argv = argv;
296 cmd->no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
297 cmd->git_cmd = opt & RUN_GIT_CMD ? 1 : 0;
298 cmd->stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
c024beb5 299 cmd->silent_exec_failure = opt & RUN_SILENT_EXEC_FAILURE ? 1 : 0;
1568fea0
AR
300}
301
f1000898
SP
302int run_command_v_opt(const char **argv, int opt)
303{
304 struct child_process cmd;
1568fea0
AR
305 prepare_run_command_v_opt(&cmd, argv, opt);
306 return run_command(&cmd);
307}
308
ee493148
AR
309int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env)
310{
311 struct child_process cmd;
312 prepare_run_command_v_opt(&cmd, argv, opt);
313 cmd.dir = dir;
314 cmd.env = env;
315 return run_command(&cmd);
316}
2d22c208 317
71064e3f 318#ifdef WIN32
d7fa500f 319static unsigned __stdcall run_thread(void *data)
618ebe9f
JS
320{
321 struct async *async = data;
322 return async->proc(async->fd_for_proc, async->data);
323}
324#endif
325
2d22c208
JS
326int start_async(struct async *async)
327{
328 int pipe_out[2];
329
330 if (pipe(pipe_out) < 0)
331 return error("cannot create pipe: %s", strerror(errno));
618ebe9f 332 async->out = pipe_out[0];
2d22c208 333
71064e3f 334#ifndef WIN32
2c3766f0
AM
335 /* Flush stdio before fork() to avoid cloning buffers */
336 fflush(NULL);
337
2d22c208
JS
338 async->pid = fork();
339 if (async->pid < 0) {
340 error("fork (async) failed: %s", strerror(errno));
341 close_pair(pipe_out);
342 return -1;
343 }
344 if (!async->pid) {
345 close(pipe_out[0]);
346 exit(!!async->proc(pipe_out[1], async->data));
347 }
2d22c208 348 close(pipe_out[1]);
618ebe9f
JS
349#else
350 async->fd_for_proc = pipe_out[1];
351 async->tid = (HANDLE) _beginthreadex(NULL, 0, run_thread, async, 0, NULL);
352 if (!async->tid) {
353 error("cannot create thread: %s", strerror(errno));
354 close_pair(pipe_out);
355 return -1;
356 }
357#endif
2d22c208
JS
358 return 0;
359}
360
361int finish_async(struct async *async)
362{
71064e3f 363#ifndef WIN32
c024beb5 364 int ret = wait_or_whine(async->pid, "child process", 0);
618ebe9f
JS
365#else
366 DWORD ret = 0;
367 if (WaitForSingleObject(async->tid, INFINITE) != WAIT_OBJECT_0)
368 ret = error("waiting for thread failed: %lu", GetLastError());
369 else if (!GetExitCodeThread(async->tid, &ret))
370 ret = error("cannot get thread exit code: %lu", GetLastError());
371 CloseHandle(async->tid);
372#endif
2d22c208
JS
373 return ret;
374}
ae98a008
SB
375
376int run_hook(const char *index_file, const char *name, ...)
377{
378 struct child_process hook;
14e6298f 379 const char **argv = NULL, *env[2];
ae98a008
SB
380 char index[PATH_MAX];
381 va_list args;
382 int ret;
14e6298f 383 size_t i = 0, alloc = 0;
ae98a008 384
cf94ca8e
SB
385 if (access(git_path("hooks/%s", name), X_OK) < 0)
386 return 0;
387
ae98a008 388 va_start(args, name);
14e6298f
SB
389 ALLOC_GROW(argv, i + 1, alloc);
390 argv[i++] = git_path("hooks/%s", name);
391 while (argv[i-1]) {
392 ALLOC_GROW(argv, i + 1, alloc);
393 argv[i++] = va_arg(args, const char *);
394 }
ae98a008
SB
395 va_end(args);
396
ae98a008
SB
397 memset(&hook, 0, sizeof(hook));
398 hook.argv = argv;
399 hook.no_stdin = 1;
400 hook.stdout_to_stderr = 1;
401 if (index_file) {
402 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
403 env[0] = index;
404 env[1] = NULL;
405 hook.env = env;
406 }
407
0ac77ec3 408 ret = run_command(&hook);
14e6298f 409 free(argv);
ae98a008
SB
410 return ret;
411}