]> git.ipfire.org Git - thirdparty/git.git/blame - run-command.c
error_routine: use parent's stderr if exec fails
[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
75301f90 11#ifndef WIN32
e4507ae8
SP
12static inline void dup_devnull(int to)
13{
14 int fd = open("/dev/null", O_RDWR);
15 dup2(fd, to);
16 close(fd);
17}
75301f90 18#endif
e4507ae8 19
8dba1e63
JK
20static const char **prepare_shell_cmd(const char **argv)
21{
22 int argc, nargc = 0;
23 const char **nargv;
24
25 for (argc = 0; argv[argc]; argc++)
26 ; /* just counting */
27 /* +1 for NULL, +3 for "sh -c" plus extra $0 */
28 nargv = xmalloc(sizeof(*nargv) * (argc + 1 + 3));
29
30 if (argc < 1)
31 die("BUG: shell command is empty");
32
f445644f
JK
33 if (strcspn(argv[0], "|&;<>()$`\\\"' \t\n*?[#~=%") != strlen(argv[0])) {
34 nargv[nargc++] = "sh";
35 nargv[nargc++] = "-c";
8dba1e63 36
f445644f
JK
37 if (argc < 2)
38 nargv[nargc++] = argv[0];
39 else {
40 struct strbuf arg0 = STRBUF_INIT;
41 strbuf_addf(&arg0, "%s \"$@\"", argv[0]);
42 nargv[nargc++] = strbuf_detach(&arg0, NULL);
43 }
8dba1e63
JK
44 }
45
46 for (argc = 0; argv[argc]; argc++)
47 nargv[nargc++] = argv[argc];
48 nargv[nargc] = NULL;
49
50 return nargv;
51}
52
53#ifndef WIN32
54static int execv_shell_cmd(const char **argv)
55{
56 const char **nargv = prepare_shell_cmd(argv);
57 trace_argv_printf(nargv, "trace: exec:");
58 execvp(nargv[0], (char **)nargv);
59 free(nargv);
60 return -1;
61}
62#endif
63
a5487ddf
JS
64#ifndef WIN32
65static int child_err = 2;
2b541bf8
JS
66static int child_notifier = -1;
67
68static void notify_parent(void)
69{
a111eb78
JN
70 /*
71 * execvp failed. If possible, we'd like to let start_command
72 * know, so failures like ENOENT can be handled right away; but
73 * otherwise, finish_command will still report the error.
74 */
75 xwrite(child_notifier, "", 1);
2b541bf8 76}
a5487ddf
JS
77
78static NORETURN void die_child(const char *err, va_list params)
79{
3bc4181f 80 vwritef(child_err, "fatal: ", err, params);
a5487ddf
JS
81 exit(128);
82}
3bc4181f
CB
83
84static void error_child(const char *err, va_list params)
85{
86 vwritef(child_err, "error: ", err, params);
87}
200a76b7 88#endif
a5487ddf
JS
89
90static inline void set_cloexec(int fd)
91{
92 int flags = fcntl(fd, F_GETFD);
93 if (flags >= 0)
94 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
95}
a5487ddf 96
ab0b41da
JS
97static int wait_or_whine(pid_t pid, const char *argv0, int silent_exec_failure)
98{
99 int status, code = -1;
100 pid_t waiting;
101 int failed_errno = 0;
102
103 while ((waiting = waitpid(pid, &status, 0)) < 0 && errno == EINTR)
104 ; /* nothing */
105
106 if (waiting < 0) {
107 failed_errno = errno;
108 error("waitpid for %s failed: %s", argv0, strerror(errno));
109 } else if (waiting != pid) {
110 error("waitpid is confused (%s)", argv0);
111 } else if (WIFSIGNALED(status)) {
112 code = WTERMSIG(status);
113 error("%s died of signal %d", argv0, code);
114 /*
115 * This return value is chosen so that code & 0xff
116 * mimics the exit code that a POSIX shell would report for
117 * a program that died from this signal.
118 */
119 code -= 128;
120 } else if (WIFEXITED(status)) {
121 code = WEXITSTATUS(status);
122 /*
123 * Convert special exit code when execvp failed.
124 */
125 if (code == 127) {
126 code = -1;
127 failed_errno = ENOENT;
128 if (!silent_exec_failure)
129 error("cannot run %s: %s", argv0,
130 strerror(ENOENT));
131 }
132 } else {
133 error("waitpid is confused (%s)", argv0);
134 }
135 errno = failed_errno;
136 return code;
137}
138
ebcb5d16 139int start_command(struct child_process *cmd)
b1bf95bb 140{
f3b33f1d
JS
141 int need_in, need_out, need_err;
142 int fdin[2], fdout[2], fderr[2];
5a7a3671 143 int failed_errno = failed_errno;
4919bf03 144
c20181e3
JS
145 /*
146 * In case of errors we must keep the promise to close FDs
147 * that have been passed in via ->in and ->out.
148 */
149
e4507ae8 150 need_in = !cmd->no_stdin && cmd->in < 0;
4919bf03 151 if (need_in) {
c20181e3 152 if (pipe(fdin) < 0) {
0ac77ec3 153 failed_errno = errno;
c20181e3
JS
154 if (cmd->out > 0)
155 close(cmd->out);
0ac77ec3 156 goto fail_pipe;
c20181e3 157 }
4919bf03 158 cmd->in = fdin[1];
4919bf03
SP
159 }
160
e4507ae8
SP
161 need_out = !cmd->no_stdout
162 && !cmd->stdout_to_stderr
163 && cmd->out < 0;
f4bba25b
SP
164 if (need_out) {
165 if (pipe(fdout) < 0) {
0ac77ec3 166 failed_errno = errno;
f4bba25b
SP
167 if (need_in)
168 close_pair(fdin);
c20181e3
JS
169 else if (cmd->in)
170 close(cmd->in);
0ac77ec3 171 goto fail_pipe;
f4bba25b
SP
172 }
173 cmd->out = fdout[0];
f4bba25b
SP
174 }
175
b73a4397 176 need_err = !cmd->no_stderr && cmd->err < 0;
f3b33f1d
JS
177 if (need_err) {
178 if (pipe(fderr) < 0) {
0ac77ec3 179 failed_errno = errno;
f3b33f1d
JS
180 if (need_in)
181 close_pair(fdin);
c20181e3
JS
182 else if (cmd->in)
183 close(cmd->in);
f3b33f1d
JS
184 if (need_out)
185 close_pair(fdout);
c20181e3
JS
186 else if (cmd->out)
187 close(cmd->out);
0ac77ec3
JS
188fail_pipe:
189 error("cannot create pipe for %s: %s",
190 cmd->argv[0], strerror(failed_errno));
191 errno = failed_errno;
192 return -1;
f3b33f1d
JS
193 }
194 cmd->err = fderr[0];
195 }
196
8852f5d7 197 trace_argv_printf(cmd->argv, "trace: run_command:");
13af8cbd 198 fflush(NULL);
8852f5d7 199
71064e3f 200#ifndef WIN32
2b541bf8
JS
201{
202 int notify_pipe[2];
203 if (pipe(notify_pipe))
204 notify_pipe[0] = notify_pipe[1] = -1;
205
ebcb5d16 206 cmd->pid = fork();
ebcb5d16 207 if (!cmd->pid) {
a5487ddf
JS
208 /*
209 * Redirect the channel to write syscall error messages to
210 * before redirecting the process's stderr so that all die()
211 * in subsequent call paths use the parent's stderr.
212 */
213 if (cmd->no_stderr || need_err) {
214 child_err = dup(2);
215 set_cloexec(child_err);
216 }
217 set_die_routine(die_child);
3bc4181f 218 set_error_routine(error_child);
a5487ddf 219
2b541bf8
JS
220 close(notify_pipe[0]);
221 set_cloexec(notify_pipe[1]);
222 child_notifier = notify_pipe[1];
223 atexit(notify_parent);
224
e4507ae8
SP
225 if (cmd->no_stdin)
226 dup_devnull(0);
227 else if (need_in) {
4919bf03 228 dup2(fdin[0], 0);
9dc09c76 229 close_pair(fdin);
4919bf03
SP
230 } else if (cmd->in) {
231 dup2(cmd->in, 0);
232 close(cmd->in);
95d3c4f5 233 }
4919bf03 234
ce2cf27a
CC
235 if (cmd->no_stderr)
236 dup_devnull(2);
237 else if (need_err) {
238 dup2(fderr[1], 2);
239 close_pair(fderr);
4f41b611
SP
240 } else if (cmd->err > 1) {
241 dup2(cmd->err, 2);
242 close(cmd->err);
ce2cf27a
CC
243 }
244
e4507ae8
SP
245 if (cmd->no_stdout)
246 dup_devnull(1);
247 else if (cmd->stdout_to_stderr)
cd83c74c 248 dup2(2, 1);
f4bba25b
SP
249 else if (need_out) {
250 dup2(fdout[1], 1);
251 close_pair(fdout);
252 } else if (cmd->out > 1) {
253 dup2(cmd->out, 1);
254 close(cmd->out);
255 }
256
1568fea0 257 if (cmd->dir && chdir(cmd->dir))
d824cbba
TR
258 die_errno("exec '%s': cd to '%s' failed", cmd->argv[0],
259 cmd->dir);
ee493148 260 if (cmd->env) {
3427b375
AR
261 for (; *cmd->env; cmd->env++) {
262 if (strchr(*cmd->env, '='))
4b25d091 263 putenv((char *)*cmd->env);
3427b375
AR
264 else
265 unsetenv(*cmd->env);
266 }
ee493148 267 }
2b541bf8
JS
268 if (cmd->preexec_cb) {
269 /*
270 * We cannot predict what the pre-exec callback does.
271 * Forgo parent notification.
272 */
273 close(child_notifier);
274 child_notifier = -1;
275
ccf08bc3 276 cmd->preexec_cb();
2b541bf8 277 }
f1000898
SP
278 if (cmd->git_cmd) {
279 execv_git_cmd(cmd->argv);
8dba1e63
JK
280 } else if (cmd->use_shell) {
281 execv_shell_cmd(cmd->argv);
77cb17e9 282 } else {
f1000898 283 execvp(cmd->argv[0], (char *const*) cmd->argv);
128aed68 284 }
a5487ddf
JS
285 /*
286 * Do not check for cmd->silent_exec_failure; the parent
287 * process will check it when it sees this exit code.
288 */
289 if (errno == ENOENT)
290 exit(127);
291 else
292 die_errno("cannot exec '%s'", cmd->argv[0]);
b1bf95bb 293 }
0ac77ec3
JS
294 if (cmd->pid < 0)
295 error("cannot fork() for %s: %s", cmd->argv[0],
296 strerror(failed_errno = errno));
2b541bf8
JS
297
298 /*
299 * Wait for child's execvp. If the execvp succeeds (or if fork()
300 * failed), EOF is seen immediately by the parent. Otherwise, the
301 * child process sends a single byte.
302 * Note that use of this infrastructure is completely advisory,
303 * therefore, we keep error checks minimal.
304 */
305 close(notify_pipe[1]);
306 if (read(notify_pipe[0], &notify_pipe[1], 1) == 1) {
307 /*
308 * At this point we know that fork() succeeded, but execvp()
309 * failed. Errors have been reported to our stderr.
310 */
311 wait_or_whine(cmd->pid, cmd->argv[0],
312 cmd->silent_exec_failure);
313 failed_errno = errno;
314 cmd->pid = -1;
315 }
316 close(notify_pipe[0]);
317}
ba26f296 318#else
0d30ad71 319{
75301f90 320 int fhin = 0, fhout = 1, fherr = 2;
108ac313 321 const char **sargv = cmd->argv;
ba26f296 322 char **env = environ;
ba26f296 323
75301f90
JS
324 if (cmd->no_stdin)
325 fhin = open("/dev/null", O_RDWR);
326 else if (need_in)
327 fhin = dup(fdin[0]);
328 else if (cmd->in)
329 fhin = dup(cmd->in);
330
331 if (cmd->no_stderr)
332 fherr = open("/dev/null", O_RDWR);
333 else if (need_err)
334 fherr = dup(fderr[1]);
76d44c8c
JH
335 else if (cmd->err > 2)
336 fherr = dup(cmd->err);
75301f90
JS
337
338 if (cmd->no_stdout)
339 fhout = open("/dev/null", O_RDWR);
340 else if (cmd->stdout_to_stderr)
341 fhout = dup(fherr);
342 else if (need_out)
343 fhout = dup(fdout[1]);
344 else if (cmd->out > 1)
345 fhout = dup(cmd->out);
ba26f296 346
2affea41
JS
347 if (cmd->env)
348 env = make_augmented_environ(cmd->env);
ba26f296
JS
349
350 if (cmd->git_cmd) {
108ac313 351 cmd->argv = prepare_git_cmd(cmd->argv);
8dba1e63
JK
352 } else if (cmd->use_shell) {
353 cmd->argv = prepare_shell_cmd(cmd->argv);
ba26f296
JS
354 }
355
f9a2743c 356 cmd->pid = mingw_spawnvpe(cmd->argv[0], cmd->argv, env, cmd->dir,
75301f90 357 fhin, fhout, fherr);
0ac77ec3 358 failed_errno = errno;
c024beb5 359 if (cmd->pid < 0 && (!cmd->silent_exec_failure || errno != ENOENT))
0ac77ec3 360 error("cannot spawn %s: %s", cmd->argv[0], strerror(errno));
ba26f296
JS
361
362 if (cmd->env)
363 free_environ(env);
364 if (cmd->git_cmd)
108ac313 365 free(cmd->argv);
ba26f296 366
108ac313 367 cmd->argv = sargv;
75301f90
JS
368 if (fhin != 0)
369 close(fhin);
370 if (fhout != 1)
371 close(fhout);
372 if (fherr != 2)
373 close(fherr);
0d30ad71 374}
ba26f296
JS
375#endif
376
377 if (cmd->pid < 0) {
378 if (need_in)
379 close_pair(fdin);
380 else if (cmd->in)
381 close(cmd->in);
382 if (need_out)
383 close_pair(fdout);
384 else if (cmd->out)
385 close(cmd->out);
386 if (need_err)
387 close_pair(fderr);
fc012c28
D
388 else if (cmd->err)
389 close(cmd->err);
0ac77ec3
JS
390 errno = failed_errno;
391 return -1;
ba26f296 392 }
4919bf03
SP
393
394 if (need_in)
395 close(fdin[0]);
396 else if (cmd->in)
397 close(cmd->in);
398
f4bba25b
SP
399 if (need_out)
400 close(fdout[1]);
c20181e3 401 else if (cmd->out)
f4bba25b
SP
402 close(cmd->out);
403
f3b33f1d
JS
404 if (need_err)
405 close(fderr[1]);
4f41b611
SP
406 else if (cmd->err)
407 close(cmd->err);
f3b33f1d 408
ebcb5d16
SP
409 return 0;
410}
411
2d22c208
JS
412int finish_command(struct child_process *cmd)
413{
c024beb5 414 return wait_or_whine(cmd->pid, cmd->argv[0], cmd->silent_exec_failure);
2d22c208
JS
415}
416
ebcb5d16
SP
417int run_command(struct child_process *cmd)
418{
419 int code = start_command(cmd);
420 if (code)
421 return code;
422 return finish_command(cmd);
423}
424
1568fea0 425static void prepare_run_command_v_opt(struct child_process *cmd,
ee493148
AR
426 const char **argv,
427 int opt)
1568fea0
AR
428{
429 memset(cmd, 0, sizeof(*cmd));
430 cmd->argv = argv;
431 cmd->no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
432 cmd->git_cmd = opt & RUN_GIT_CMD ? 1 : 0;
433 cmd->stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
c024beb5 434 cmd->silent_exec_failure = opt & RUN_SILENT_EXEC_FAILURE ? 1 : 0;
8dba1e63 435 cmd->use_shell = opt & RUN_USING_SHELL ? 1 : 0;
1568fea0
AR
436}
437
f1000898
SP
438int run_command_v_opt(const char **argv, int opt)
439{
440 struct child_process cmd;
1568fea0
AR
441 prepare_run_command_v_opt(&cmd, argv, opt);
442 return run_command(&cmd);
443}
444
ee493148
AR
445int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env)
446{
447 struct child_process cmd;
448 prepare_run_command_v_opt(&cmd, argv, opt);
449 cmd.dir = dir;
450 cmd.env = env;
451 return run_command(&cmd);
452}
2d22c208 453
f6b60983 454#ifndef NO_PTHREADS
0ea1c89b
JS
455static pthread_t main_thread;
456static int main_thread_set;
457static pthread_key_t async_key;
458
200a76b7 459static void *run_thread(void *data)
618ebe9f
JS
460{
461 struct async *async = data;
f6b60983 462 intptr_t ret;
0ea1c89b
JS
463
464 pthread_setspecific(async_key, async);
f6b60983 465 ret = async->proc(async->proc_in, async->proc_out, async->data);
200a76b7 466 return (void *)ret;
618ebe9f 467}
0ea1c89b
JS
468
469static NORETURN void die_async(const char *err, va_list params)
470{
471 vreportf("fatal: ", err, params);
472
473 if (!pthread_equal(main_thread, pthread_self())) {
474 struct async *async = pthread_getspecific(async_key);
475 if (async->proc_in >= 0)
476 close(async->proc_in);
477 if (async->proc_out >= 0)
478 close(async->proc_out);
479 pthread_exit((void *)128);
480 }
481
482 exit(128);
618ebe9f
JS
483}
484#endif
485
2d22c208
JS
486int start_async(struct async *async)
487{
ae6a5609
EFL
488 int need_in, need_out;
489 int fdin[2], fdout[2];
490 int proc_in, proc_out;
2d22c208 491
ae6a5609
EFL
492 need_in = async->in < 0;
493 if (need_in) {
494 if (pipe(fdin) < 0) {
495 if (async->out > 0)
496 close(async->out);
497 return error("cannot create pipe: %s", strerror(errno));
498 }
499 async->in = fdin[1];
500 }
501
502 need_out = async->out < 0;
503 if (need_out) {
504 if (pipe(fdout) < 0) {
505 if (need_in)
506 close_pair(fdin);
507 else if (async->in)
508 close(async->in);
509 return error("cannot create pipe: %s", strerror(errno));
510 }
511 async->out = fdout[0];
512 }
513
514 if (need_in)
515 proc_in = fdin[0];
516 else if (async->in)
517 proc_in = async->in;
518 else
519 proc_in = -1;
520
521 if (need_out)
522 proc_out = fdout[1];
523 else if (async->out)
524 proc_out = async->out;
525 else
526 proc_out = -1;
2d22c208 527
f6b60983 528#ifdef NO_PTHREADS
2c3766f0
AM
529 /* Flush stdio before fork() to avoid cloning buffers */
530 fflush(NULL);
531
2d22c208
JS
532 async->pid = fork();
533 if (async->pid < 0) {
534 error("fork (async) failed: %s", strerror(errno));
ae6a5609 535 goto error;
2d22c208
JS
536 }
537 if (!async->pid) {
ae6a5609
EFL
538 if (need_in)
539 close(fdin[1]);
540 if (need_out)
541 close(fdout[0]);
542 exit(!!async->proc(proc_in, proc_out, async->data));
2d22c208 543 }
ae6a5609
EFL
544
545 if (need_in)
546 close(fdin[0]);
547 else if (async->in)
548 close(async->in);
549
550 if (need_out)
551 close(fdout[1]);
552 else if (async->out)
553 close(async->out);
618ebe9f 554#else
0ea1c89b
JS
555 if (!main_thread_set) {
556 /*
557 * We assume that the first time that start_async is called
558 * it is from the main thread.
559 */
560 main_thread_set = 1;
561 main_thread = pthread_self();
562 pthread_key_create(&async_key, NULL);
563 set_die_routine(die_async);
564 }
565
200a76b7
JS
566 if (proc_in >= 0)
567 set_cloexec(proc_in);
568 if (proc_out >= 0)
569 set_cloexec(proc_out);
ae6a5609
EFL
570 async->proc_in = proc_in;
571 async->proc_out = proc_out;
200a76b7
JS
572 {
573 int err = pthread_create(&async->tid, NULL, run_thread, async);
574 if (err) {
575 error("cannot create thread: %s", strerror(err));
576 goto error;
577 }
618ebe9f
JS
578 }
579#endif
2d22c208 580 return 0;
ae6a5609
EFL
581
582error:
583 if (need_in)
584 close_pair(fdin);
585 else if (async->in)
586 close(async->in);
587
588 if (need_out)
589 close_pair(fdout);
590 else if (async->out)
591 close(async->out);
592 return -1;
2d22c208
JS
593}
594
595int finish_async(struct async *async)
596{
f6b60983 597#ifdef NO_PTHREADS
200a76b7 598 return wait_or_whine(async->pid, "child process", 0);
618ebe9f 599#else
200a76b7
JS
600 void *ret = (void *)(intptr_t)(-1);
601
602 if (pthread_join(async->tid, &ret))
603 error("pthread_join failed");
604 return (int)(intptr_t)ret;
618ebe9f 605#endif
2d22c208 606}
ae98a008
SB
607
608int run_hook(const char *index_file, const char *name, ...)
609{
610 struct child_process hook;
14e6298f 611 const char **argv = NULL, *env[2];
ae98a008
SB
612 char index[PATH_MAX];
613 va_list args;
614 int ret;
14e6298f 615 size_t i = 0, alloc = 0;
ae98a008 616
cf94ca8e
SB
617 if (access(git_path("hooks/%s", name), X_OK) < 0)
618 return 0;
619
ae98a008 620 va_start(args, name);
14e6298f
SB
621 ALLOC_GROW(argv, i + 1, alloc);
622 argv[i++] = git_path("hooks/%s", name);
623 while (argv[i-1]) {
624 ALLOC_GROW(argv, i + 1, alloc);
625 argv[i++] = va_arg(args, const char *);
626 }
ae98a008
SB
627 va_end(args);
628
ae98a008
SB
629 memset(&hook, 0, sizeof(hook));
630 hook.argv = argv;
631 hook.no_stdin = 1;
632 hook.stdout_to_stderr = 1;
633 if (index_file) {
634 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
635 env[0] = index;
636 env[1] = NULL;
637 hook.env = env;
638 }
639
0ac77ec3 640 ret = run_command(&hook);
14e6298f 641 free(argv);
ae98a008
SB
642 return ret;
643}