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