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