Windows: A pipe() replacement whose ends are not inherited to children.
[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
ebcb5d16 18int start_command(struct child_process *cmd)
b1bf95bb 19{
f3b33f1d
JS
20 int need_in, need_out, need_err;
21 int fdin[2], fdout[2], fderr[2];
4919bf03 22
c20181e3
JS
23 /*
24 * In case of errors we must keep the promise to close FDs
25 * that have been passed in via ->in and ->out.
26 */
27
e4507ae8 28 need_in = !cmd->no_stdin && cmd->in < 0;
4919bf03 29 if (need_in) {
c20181e3
JS
30 if (pipe(fdin) < 0) {
31 if (cmd->out > 0)
32 close(cmd->out);
4919bf03 33 return -ERR_RUN_COMMAND_PIPE;
c20181e3 34 }
4919bf03 35 cmd->in = fdin[1];
4919bf03
SP
36 }
37
e4507ae8
SP
38 need_out = !cmd->no_stdout
39 && !cmd->stdout_to_stderr
40 && cmd->out < 0;
f4bba25b
SP
41 if (need_out) {
42 if (pipe(fdout) < 0) {
43 if (need_in)
44 close_pair(fdin);
c20181e3
JS
45 else if (cmd->in)
46 close(cmd->in);
f4bba25b
SP
47 return -ERR_RUN_COMMAND_PIPE;
48 }
49 cmd->out = fdout[0];
f4bba25b
SP
50 }
51
b73a4397 52 need_err = !cmd->no_stderr && cmd->err < 0;
f3b33f1d
JS
53 if (need_err) {
54 if (pipe(fderr) < 0) {
55 if (need_in)
56 close_pair(fdin);
c20181e3
JS
57 else if (cmd->in)
58 close(cmd->in);
f3b33f1d
JS
59 if (need_out)
60 close_pair(fdout);
c20181e3
JS
61 else if (cmd->out)
62 close(cmd->out);
f3b33f1d
JS
63 return -ERR_RUN_COMMAND_PIPE;
64 }
65 cmd->err = fderr[0];
66 }
67
ebcb5d16 68 cmd->pid = fork();
4919bf03 69 if (cmd->pid < 0) {
9dc09c76
SP
70 if (need_in)
71 close_pair(fdin);
c20181e3
JS
72 else if (cmd->in)
73 close(cmd->in);
f4bba25b
SP
74 if (need_out)
75 close_pair(fdout);
c20181e3
JS
76 else if (cmd->out)
77 close(cmd->out);
f3b33f1d
JS
78 if (need_err)
79 close_pair(fderr);
b1bf95bb 80 return -ERR_RUN_COMMAND_FORK;
4919bf03
SP
81 }
82
ebcb5d16 83 if (!cmd->pid) {
e4507ae8
SP
84 if (cmd->no_stdin)
85 dup_devnull(0);
86 else if (need_in) {
4919bf03 87 dup2(fdin[0], 0);
9dc09c76 88 close_pair(fdin);
4919bf03
SP
89 } else if (cmd->in) {
90 dup2(cmd->in, 0);
91 close(cmd->in);
95d3c4f5 92 }
4919bf03 93
ce2cf27a
CC
94 if (cmd->no_stderr)
95 dup_devnull(2);
96 else if (need_err) {
97 dup2(fderr[1], 2);
98 close_pair(fderr);
99 }
100
e4507ae8
SP
101 if (cmd->no_stdout)
102 dup_devnull(1);
103 else if (cmd->stdout_to_stderr)
cd83c74c 104 dup2(2, 1);
f4bba25b
SP
105 else if (need_out) {
106 dup2(fdout[1], 1);
107 close_pair(fdout);
108 } else if (cmd->out > 1) {
109 dup2(cmd->out, 1);
110 close(cmd->out);
111 }
112
1568fea0
AR
113 if (cmd->dir && chdir(cmd->dir))
114 die("exec %s: cd to %s failed (%s)", cmd->argv[0],
115 cmd->dir, strerror(errno));
ee493148 116 if (cmd->env) {
3427b375
AR
117 for (; *cmd->env; cmd->env++) {
118 if (strchr(*cmd->env, '='))
119 putenv((char*)*cmd->env);
120 else
121 unsetenv(*cmd->env);
122 }
ee493148 123 }
f1000898
SP
124 if (cmd->git_cmd) {
125 execv_git_cmd(cmd->argv);
77cb17e9 126 } else {
f1000898 127 execvp(cmd->argv[0], (char *const*) cmd->argv);
128aed68 128 }
f1000898 129 die("exec %s failed.", cmd->argv[0]);
b1bf95bb 130 }
4919bf03
SP
131
132 if (need_in)
133 close(fdin[0]);
134 else if (cmd->in)
135 close(cmd->in);
136
f4bba25b
SP
137 if (need_out)
138 close(fdout[1]);
c20181e3 139 else if (cmd->out)
f4bba25b
SP
140 close(cmd->out);
141
f3b33f1d
JS
142 if (need_err)
143 close(fderr[1]);
144
ebcb5d16
SP
145 return 0;
146}
147
2d22c208 148static int wait_or_whine(pid_t pid)
ebcb5d16 149{
b1bf95bb
JW
150 for (;;) {
151 int status, code;
2d22c208 152 pid_t waiting = waitpid(pid, &status, 0);
b1bf95bb 153
6f002f98 154 if (waiting < 0) {
b1bf95bb
JW
155 if (errno == EINTR)
156 continue;
6f002f98 157 error("waitpid failed (%s)", strerror(errno));
b1bf95bb
JW
158 return -ERR_RUN_COMMAND_WAITPID;
159 }
2d22c208 160 if (waiting != pid)
b1bf95bb
JW
161 return -ERR_RUN_COMMAND_WAITPID_WRONG_PID;
162 if (WIFSIGNALED(status))
163 return -ERR_RUN_COMMAND_WAITPID_SIGNAL;
164
165 if (!WIFEXITED(status))
166 return -ERR_RUN_COMMAND_WAITPID_NOEXIT;
167 code = WEXITSTATUS(status);
168 if (code)
169 return -code;
170 return 0;
171 }
172}
f1000898 173
2d22c208
JS
174int finish_command(struct child_process *cmd)
175{
2d22c208
JS
176 return wait_or_whine(cmd->pid);
177}
178
ebcb5d16
SP
179int run_command(struct child_process *cmd)
180{
181 int code = start_command(cmd);
182 if (code)
183 return code;
184 return finish_command(cmd);
185}
186
1568fea0 187static void prepare_run_command_v_opt(struct child_process *cmd,
ee493148
AR
188 const char **argv,
189 int opt)
1568fea0
AR
190{
191 memset(cmd, 0, sizeof(*cmd));
192 cmd->argv = argv;
193 cmd->no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
194 cmd->git_cmd = opt & RUN_GIT_CMD ? 1 : 0;
195 cmd->stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
196}
197
f1000898
SP
198int run_command_v_opt(const char **argv, int opt)
199{
200 struct child_process cmd;
1568fea0
AR
201 prepare_run_command_v_opt(&cmd, argv, opt);
202 return run_command(&cmd);
203}
204
205int run_command_v_opt_cd(const char **argv, int opt, const char *dir)
206{
207 struct child_process cmd;
208 prepare_run_command_v_opt(&cmd, argv, opt);
209 cmd.dir = dir;
f1000898
SP
210 return run_command(&cmd);
211}
ee493148
AR
212
213int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env)
214{
215 struct child_process cmd;
216 prepare_run_command_v_opt(&cmd, argv, opt);
217 cmd.dir = dir;
218 cmd.env = env;
219 return run_command(&cmd);
220}
2d22c208
JS
221
222int start_async(struct async *async)
223{
224 int pipe_out[2];
225
226 if (pipe(pipe_out) < 0)
227 return error("cannot create pipe: %s", strerror(errno));
228
229 async->pid = fork();
230 if (async->pid < 0) {
231 error("fork (async) failed: %s", strerror(errno));
232 close_pair(pipe_out);
233 return -1;
234 }
235 if (!async->pid) {
236 close(pipe_out[0]);
237 exit(!!async->proc(pipe_out[1], async->data));
238 }
239 async->out = pipe_out[0];
240 close(pipe_out[1]);
241 return 0;
242}
243
244int finish_async(struct async *async)
245{
246 int ret = 0;
247
248 if (wait_or_whine(async->pid))
249 ret = error("waitpid (async) failed");
250 return ret;
251}