Redirect update hook stdout to 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
9b0b5093 5int run_command_v_opt(const char **argv, int flags)
b1bf95bb
JW
6{
7 pid_t pid = fork();
8
9 if (pid < 0)
10 return -ERR_RUN_COMMAND_FORK;
11 if (!pid) {
128aed68
DB
12 if (flags & RUN_COMMAND_NO_STDIO) {
13 int fd = open("/dev/null", O_RDWR);
14 dup2(fd, 0);
15 dup2(fd, 1);
77cb17e9 16 close(fd);
cd83c74c
SP
17 } else if (flags & RUN_COMMAND_STDOUT_TO_STDERR)
18 dup2(2, 1);
77cb17e9
MO
19 if (flags & RUN_GIT_CMD) {
20 execv_git_cmd(argv);
21 } else {
22 execvp(argv[0], (char *const*) argv);
128aed68 23 }
19614330 24 die("exec %s failed.", argv[0]);
b1bf95bb
JW
25 }
26 for (;;) {
27 int status, code;
6f002f98 28 pid_t waiting = waitpid(pid, &status, 0);
b1bf95bb 29
6f002f98 30 if (waiting < 0) {
b1bf95bb
JW
31 if (errno == EINTR)
32 continue;
6f002f98 33 error("waitpid failed (%s)", strerror(errno));
b1bf95bb
JW
34 return -ERR_RUN_COMMAND_WAITPID;
35 }
6f002f98 36 if (waiting != pid)
b1bf95bb
JW
37 return -ERR_RUN_COMMAND_WAITPID_WRONG_PID;
38 if (WIFSIGNALED(status))
39 return -ERR_RUN_COMMAND_WAITPID_SIGNAL;
40
41 if (!WIFEXITED(status))
42 return -ERR_RUN_COMMAND_WAITPID_NOEXIT;
43 code = WEXITSTATUS(status);
44 if (code)
45 return -code;
46 return 0;
47 }
48}
49
9b0b5093 50int run_command_v(const char **argv)
128aed68 51{
9b0b5093 52 return run_command_v_opt(argv, 0);
128aed68
DB
53}
54
cd83c74c 55static int run_command_va_opt(int opt, const char *cmd, va_list param)
b1bf95bb
JW
56{
57 int argc;
9201c707 58 const char *argv[MAX_RUN_COMMAND_ARGS];
b1bf95bb 59 const char *arg;
b1bf95bb 60
19614330 61 argv[0] = (char*) cmd;
b1bf95bb
JW
62 argc = 1;
63 while (argc < MAX_RUN_COMMAND_ARGS) {
64 arg = argv[argc++] = va_arg(param, char *);
65 if (!arg)
66 break;
67 }
b1bf95bb
JW
68 if (MAX_RUN_COMMAND_ARGS <= argc)
69 return error("too many args to run %s", cmd);
cd83c74c
SP
70 return run_command_v_opt(argv, opt);
71}
72
73int run_command_opt(int opt, const char *cmd, ...)
74{
75 va_list params;
76 int r;
77
78 va_start(params, cmd);
79 r = run_command_va_opt(opt, cmd, params);
80 va_end(params);
81 return r;
82}
83
84int run_command(const char *cmd, ...)
85{
86 va_list params;
87 int r;
88
89 va_start(params, cmd);
90 r = run_command_va_opt(0, cmd, params);
91 va_end(params);
92 return r;
b1bf95bb 93}