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