run_command: return exit code as positive value
[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
8852f5d7
JS
68 trace_argv_printf(cmd->argv, "trace: run_command:");
69
ba26f296 70#ifndef __MINGW32__
7d0b18a4 71 fflush(NULL);
ebcb5d16 72 cmd->pid = fork();
ebcb5d16 73 if (!cmd->pid) {
e4507ae8
SP
74 if (cmd->no_stdin)
75 dup_devnull(0);
76 else if (need_in) {
4919bf03 77 dup2(fdin[0], 0);
9dc09c76 78 close_pair(fdin);
4919bf03
SP
79 } else if (cmd->in) {
80 dup2(cmd->in, 0);
81 close(cmd->in);
95d3c4f5 82 }
4919bf03 83
ce2cf27a
CC
84 if (cmd->no_stderr)
85 dup_devnull(2);
86 else if (need_err) {
87 dup2(fderr[1], 2);
88 close_pair(fderr);
89 }
90
e4507ae8
SP
91 if (cmd->no_stdout)
92 dup_devnull(1);
93 else if (cmd->stdout_to_stderr)
cd83c74c 94 dup2(2, 1);
f4bba25b
SP
95 else if (need_out) {
96 dup2(fdout[1], 1);
97 close_pair(fdout);
98 } else if (cmd->out > 1) {
99 dup2(cmd->out, 1);
100 close(cmd->out);
101 }
102
1568fea0
AR
103 if (cmd->dir && chdir(cmd->dir))
104 die("exec %s: cd to %s failed (%s)", cmd->argv[0],
105 cmd->dir, strerror(errno));
ee493148 106 if (cmd->env) {
3427b375
AR
107 for (; *cmd->env; cmd->env++) {
108 if (strchr(*cmd->env, '='))
4b25d091 109 putenv((char *)*cmd->env);
3427b375
AR
110 else
111 unsetenv(*cmd->env);
112 }
ee493148 113 }
ccf08bc3
JK
114 if (cmd->preexec_cb)
115 cmd->preexec_cb();
f1000898
SP
116 if (cmd->git_cmd) {
117 execv_git_cmd(cmd->argv);
77cb17e9 118 } else {
f1000898 119 execvp(cmd->argv[0], (char *const*) cmd->argv);
128aed68 120 }
45c0961c
JK
121 trace_printf("trace: exec '%s' failed: %s\n", cmd->argv[0],
122 strerror(errno));
123 exit(127);
b1bf95bb 124 }
ba26f296
JS
125#else
126 int s0 = -1, s1 = -1, s2 = -1; /* backups of stdin, stdout, stderr */
108ac313 127 const char **sargv = cmd->argv;
ba26f296 128 char **env = environ;
ba26f296
JS
129
130 if (cmd->no_stdin) {
131 s0 = dup(0);
132 dup_devnull(0);
133 } else if (need_in) {
134 s0 = dup(0);
135 dup2(fdin[0], 0);
136 } else if (cmd->in) {
137 s0 = dup(0);
138 dup2(cmd->in, 0);
139 }
140
141 if (cmd->no_stderr) {
142 s2 = dup(2);
143 dup_devnull(2);
144 } else if (need_err) {
145 s2 = dup(2);
146 dup2(fderr[1], 2);
147 }
148
149 if (cmd->no_stdout) {
150 s1 = dup(1);
151 dup_devnull(1);
152 } else if (cmd->stdout_to_stderr) {
153 s1 = dup(1);
154 dup2(2, 1);
155 } else if (need_out) {
156 s1 = dup(1);
157 dup2(fdout[1], 1);
158 } else if (cmd->out > 1) {
159 s1 = dup(1);
160 dup2(cmd->out, 1);
161 }
162
163 if (cmd->dir)
164 die("chdir in start_command() not implemented");
165 if (cmd->env) {
166 env = copy_environ();
167 for (; *cmd->env; cmd->env++)
168 env = env_setenv(env, *cmd->env);
169 }
170
171 if (cmd->git_cmd) {
108ac313 172 cmd->argv = prepare_git_cmd(cmd->argv);
ba26f296
JS
173 }
174
7e5d7768 175 cmd->pid = mingw_spawnvpe(cmd->argv[0], cmd->argv, env);
ba26f296
JS
176
177 if (cmd->env)
178 free_environ(env);
179 if (cmd->git_cmd)
108ac313 180 free(cmd->argv);
ba26f296 181
108ac313 182 cmd->argv = sargv;
ba26f296
JS
183 if (s0 >= 0)
184 dup2(s0, 0), close(s0);
185 if (s1 >= 0)
186 dup2(s1, 1), close(s1);
187 if (s2 >= 0)
188 dup2(s2, 2), close(s2);
189#endif
190
191 if (cmd->pid < 0) {
45c0961c 192 int err = errno;
ba26f296
JS
193 if (need_in)
194 close_pair(fdin);
195 else if (cmd->in)
196 close(cmd->in);
197 if (need_out)
198 close_pair(fdout);
199 else if (cmd->out)
200 close(cmd->out);
201 if (need_err)
202 close_pair(fderr);
45c0961c
JK
203 return err == ENOENT ?
204 -ERR_RUN_COMMAND_EXEC :
205 -ERR_RUN_COMMAND_FORK;
ba26f296 206 }
4919bf03
SP
207
208 if (need_in)
209 close(fdin[0]);
210 else if (cmd->in)
211 close(cmd->in);
212
f4bba25b
SP
213 if (need_out)
214 close(fdout[1]);
c20181e3 215 else if (cmd->out)
f4bba25b
SP
216 close(cmd->out);
217
f3b33f1d
JS
218 if (need_err)
219 close(fderr[1]);
220
ebcb5d16
SP
221 return 0;
222}
223
2d22c208 224static int wait_or_whine(pid_t pid)
ebcb5d16 225{
b1bf95bb
JW
226 for (;;) {
227 int status, code;
2d22c208 228 pid_t waiting = waitpid(pid, &status, 0);
b1bf95bb 229
6f002f98 230 if (waiting < 0) {
b1bf95bb
JW
231 if (errno == EINTR)
232 continue;
6f002f98 233 error("waitpid failed (%s)", strerror(errno));
b1bf95bb
JW
234 return -ERR_RUN_COMMAND_WAITPID;
235 }
2d22c208 236 if (waiting != pid)
b1bf95bb
JW
237 return -ERR_RUN_COMMAND_WAITPID_WRONG_PID;
238 if (WIFSIGNALED(status))
239 return -ERR_RUN_COMMAND_WAITPID_SIGNAL;
240
241 if (!WIFEXITED(status))
242 return -ERR_RUN_COMMAND_WAITPID_NOEXIT;
243 code = WEXITSTATUS(status);
5709e036 244 return code == 127 ? -ERR_RUN_COMMAND_EXEC : code;
b1bf95bb
JW
245 }
246}
f1000898 247
2d22c208
JS
248int finish_command(struct child_process *cmd)
249{
2d22c208
JS
250 return wait_or_whine(cmd->pid);
251}
252
ebcb5d16
SP
253int run_command(struct child_process *cmd)
254{
255 int code = start_command(cmd);
256 if (code)
257 return code;
258 return finish_command(cmd);
259}
260
1568fea0 261static void prepare_run_command_v_opt(struct child_process *cmd,
ee493148
AR
262 const char **argv,
263 int opt)
1568fea0
AR
264{
265 memset(cmd, 0, sizeof(*cmd));
266 cmd->argv = argv;
267 cmd->no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
268 cmd->git_cmd = opt & RUN_GIT_CMD ? 1 : 0;
269 cmd->stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
270}
271
f1000898
SP
272int run_command_v_opt(const char **argv, int opt)
273{
274 struct child_process cmd;
1568fea0
AR
275 prepare_run_command_v_opt(&cmd, argv, opt);
276 return run_command(&cmd);
277}
278
ee493148
AR
279int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env)
280{
281 struct child_process cmd;
282 prepare_run_command_v_opt(&cmd, argv, opt);
283 cmd.dir = dir;
284 cmd.env = env;
285 return run_command(&cmd);
286}
2d22c208 287
618ebe9f
JS
288#ifdef __MINGW32__
289static __stdcall unsigned run_thread(void *data)
290{
291 struct async *async = data;
292 return async->proc(async->fd_for_proc, async->data);
293}
294#endif
295
2d22c208
JS
296int start_async(struct async *async)
297{
298 int pipe_out[2];
299
300 if (pipe(pipe_out) < 0)
301 return error("cannot create pipe: %s", strerror(errno));
618ebe9f 302 async->out = pipe_out[0];
2d22c208 303
618ebe9f 304#ifndef __MINGW32__
2c3766f0
AM
305 /* Flush stdio before fork() to avoid cloning buffers */
306 fflush(NULL);
307
2d22c208
JS
308 async->pid = fork();
309 if (async->pid < 0) {
310 error("fork (async) failed: %s", strerror(errno));
311 close_pair(pipe_out);
312 return -1;
313 }
314 if (!async->pid) {
315 close(pipe_out[0]);
316 exit(!!async->proc(pipe_out[1], async->data));
317 }
2d22c208 318 close(pipe_out[1]);
618ebe9f
JS
319#else
320 async->fd_for_proc = pipe_out[1];
321 async->tid = (HANDLE) _beginthreadex(NULL, 0, run_thread, async, 0, NULL);
322 if (!async->tid) {
323 error("cannot create thread: %s", strerror(errno));
324 close_pair(pipe_out);
325 return -1;
326 }
327#endif
2d22c208
JS
328 return 0;
329}
330
331int finish_async(struct async *async)
332{
618ebe9f 333#ifndef __MINGW32__
2d22c208
JS
334 int ret = 0;
335
336 if (wait_or_whine(async->pid))
337 ret = error("waitpid (async) failed");
618ebe9f
JS
338#else
339 DWORD ret = 0;
340 if (WaitForSingleObject(async->tid, INFINITE) != WAIT_OBJECT_0)
341 ret = error("waiting for thread failed: %lu", GetLastError());
342 else if (!GetExitCodeThread(async->tid, &ret))
343 ret = error("cannot get thread exit code: %lu", GetLastError());
344 CloseHandle(async->tid);
345#endif
2d22c208
JS
346 return ret;
347}
ae98a008
SB
348
349int run_hook(const char *index_file, const char *name, ...)
350{
351 struct child_process hook;
14e6298f 352 const char **argv = NULL, *env[2];
ae98a008
SB
353 char index[PATH_MAX];
354 va_list args;
355 int ret;
14e6298f 356 size_t i = 0, alloc = 0;
ae98a008 357
cf94ca8e
SB
358 if (access(git_path("hooks/%s", name), X_OK) < 0)
359 return 0;
360
ae98a008 361 va_start(args, name);
14e6298f
SB
362 ALLOC_GROW(argv, i + 1, alloc);
363 argv[i++] = git_path("hooks/%s", name);
364 while (argv[i-1]) {
365 ALLOC_GROW(argv, i + 1, alloc);
366 argv[i++] = va_arg(args, const char *);
367 }
ae98a008
SB
368 va_end(args);
369
ae98a008
SB
370 memset(&hook, 0, sizeof(hook));
371 hook.argv = argv;
372 hook.no_stdin = 1;
373 hook.stdout_to_stderr = 1;
374 if (index_file) {
375 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
376 env[0] = index;
377 env[1] = NULL;
378 hook.env = env;
379 }
380
381 ret = start_command(&hook);
14e6298f 382 free(argv);
ae98a008
SB
383 if (ret) {
384 warning("Could not spawn %s", argv[0]);
385 return ret;
386 }
387 ret = finish_command(&hook);
388 if (ret == -ERR_RUN_COMMAND_WAITPID_SIGNAL)
389 warning("%s exited due to uncaught signal", argv[0]);
390
391 return ret;
392}