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