run-command: move wait_or_whine earlier
[git/jnareb-git.git] / run-command.c
blobdccac37c4bac6945577c24d1e0cccac9ff216d80
1 #include "cache.h"
2 #include "run-command.h"
3 #include "exec_cmd.h"
5 static inline void close_pair(int fd[2])
7 close(fd[0]);
8 close(fd[1]);
11 static inline void dup_devnull(int to)
13 int fd = open("/dev/null", O_RDWR);
14 dup2(fd, to);
15 close(fd);
18 #ifndef WIN32
19 static int child_err = 2;
21 static NORETURN void die_child(const char *err, va_list params)
23 char msg[4096];
24 int len = vsnprintf(msg, sizeof(msg), err, params);
25 if (len > sizeof(msg))
26 len = sizeof(msg);
28 write(child_err, "fatal: ", 7);
29 write(child_err, msg, len);
30 write(child_err, "\n", 1);
31 exit(128);
34 static inline void set_cloexec(int fd)
36 int flags = fcntl(fd, F_GETFD);
37 if (flags >= 0)
38 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
40 #endif
42 static int wait_or_whine(pid_t pid, const char *argv0, int silent_exec_failure)
44 int status, code = -1;
45 pid_t waiting;
46 int failed_errno = 0;
48 while ((waiting = waitpid(pid, &status, 0)) < 0 && errno == EINTR)
49 ; /* nothing */
51 if (waiting < 0) {
52 failed_errno = errno;
53 error("waitpid for %s failed: %s", argv0, strerror(errno));
54 } else if (waiting != pid) {
55 error("waitpid is confused (%s)", argv0);
56 } else if (WIFSIGNALED(status)) {
57 code = WTERMSIG(status);
58 error("%s died of signal %d", argv0, code);
60 * This return value is chosen so that code & 0xff
61 * mimics the exit code that a POSIX shell would report for
62 * a program that died from this signal.
64 code -= 128;
65 } else if (WIFEXITED(status)) {
66 code = WEXITSTATUS(status);
68 * Convert special exit code when execvp failed.
70 if (code == 127) {
71 code = -1;
72 failed_errno = ENOENT;
73 if (!silent_exec_failure)
74 error("cannot run %s: %s", argv0,
75 strerror(ENOENT));
77 } else {
78 error("waitpid is confused (%s)", argv0);
80 errno = failed_errno;
81 return code;
84 int start_command(struct child_process *cmd)
86 int need_in, need_out, need_err;
87 int fdin[2], fdout[2], fderr[2];
88 int failed_errno = failed_errno;
91 * In case of errors we must keep the promise to close FDs
92 * that have been passed in via ->in and ->out.
95 need_in = !cmd->no_stdin && cmd->in < 0;
96 if (need_in) {
97 if (pipe(fdin) < 0) {
98 failed_errno = errno;
99 if (cmd->out > 0)
100 close(cmd->out);
101 goto fail_pipe;
103 cmd->in = fdin[1];
106 need_out = !cmd->no_stdout
107 && !cmd->stdout_to_stderr
108 && cmd->out < 0;
109 if (need_out) {
110 if (pipe(fdout) < 0) {
111 failed_errno = errno;
112 if (need_in)
113 close_pair(fdin);
114 else if (cmd->in)
115 close(cmd->in);
116 goto fail_pipe;
118 cmd->out = fdout[0];
121 need_err = !cmd->no_stderr && cmd->err < 0;
122 if (need_err) {
123 if (pipe(fderr) < 0) {
124 failed_errno = errno;
125 if (need_in)
126 close_pair(fdin);
127 else if (cmd->in)
128 close(cmd->in);
129 if (need_out)
130 close_pair(fdout);
131 else if (cmd->out)
132 close(cmd->out);
133 fail_pipe:
134 error("cannot create pipe for %s: %s",
135 cmd->argv[0], strerror(failed_errno));
136 errno = failed_errno;
137 return -1;
139 cmd->err = fderr[0];
142 trace_argv_printf(cmd->argv, "trace: run_command:");
144 #ifndef WIN32
145 fflush(NULL);
146 cmd->pid = fork();
147 if (!cmd->pid) {
149 * Redirect the channel to write syscall error messages to
150 * before redirecting the process's stderr so that all die()
151 * in subsequent call paths use the parent's stderr.
153 if (cmd->no_stderr || need_err) {
154 child_err = dup(2);
155 set_cloexec(child_err);
157 set_die_routine(die_child);
159 if (cmd->no_stdin)
160 dup_devnull(0);
161 else if (need_in) {
162 dup2(fdin[0], 0);
163 close_pair(fdin);
164 } else if (cmd->in) {
165 dup2(cmd->in, 0);
166 close(cmd->in);
169 if (cmd->no_stderr)
170 dup_devnull(2);
171 else if (need_err) {
172 dup2(fderr[1], 2);
173 close_pair(fderr);
176 if (cmd->no_stdout)
177 dup_devnull(1);
178 else if (cmd->stdout_to_stderr)
179 dup2(2, 1);
180 else if (need_out) {
181 dup2(fdout[1], 1);
182 close_pair(fdout);
183 } else if (cmd->out > 1) {
184 dup2(cmd->out, 1);
185 close(cmd->out);
188 if (cmd->dir && chdir(cmd->dir))
189 die_errno("exec '%s': cd to '%s' failed", cmd->argv[0],
190 cmd->dir);
191 if (cmd->env) {
192 for (; *cmd->env; cmd->env++) {
193 if (strchr(*cmd->env, '='))
194 putenv((char *)*cmd->env);
195 else
196 unsetenv(*cmd->env);
199 if (cmd->preexec_cb)
200 cmd->preexec_cb();
201 if (cmd->git_cmd) {
202 execv_git_cmd(cmd->argv);
203 } else {
204 execvp(cmd->argv[0], (char *const*) cmd->argv);
207 * Do not check for cmd->silent_exec_failure; the parent
208 * process will check it when it sees this exit code.
210 if (errno == ENOENT)
211 exit(127);
212 else
213 die_errno("cannot exec '%s'", cmd->argv[0]);
215 if (cmd->pid < 0)
216 error("cannot fork() for %s: %s", cmd->argv[0],
217 strerror(failed_errno = errno));
218 #else
220 int s0 = -1, s1 = -1, s2 = -1; /* backups of stdin, stdout, stderr */
221 const char **sargv = cmd->argv;
222 char **env = environ;
224 if (cmd->no_stdin) {
225 s0 = dup(0);
226 dup_devnull(0);
227 } else if (need_in) {
228 s0 = dup(0);
229 dup2(fdin[0], 0);
230 } else if (cmd->in) {
231 s0 = dup(0);
232 dup2(cmd->in, 0);
235 if (cmd->no_stderr) {
236 s2 = dup(2);
237 dup_devnull(2);
238 } else if (need_err) {
239 s2 = dup(2);
240 dup2(fderr[1], 2);
243 if (cmd->no_stdout) {
244 s1 = dup(1);
245 dup_devnull(1);
246 } else if (cmd->stdout_to_stderr) {
247 s1 = dup(1);
248 dup2(2, 1);
249 } else if (need_out) {
250 s1 = dup(1);
251 dup2(fdout[1], 1);
252 } else if (cmd->out > 1) {
253 s1 = dup(1);
254 dup2(cmd->out, 1);
257 if (cmd->dir)
258 die("chdir in start_command() not implemented");
259 if (cmd->env)
260 env = make_augmented_environ(cmd->env);
262 if (cmd->git_cmd) {
263 cmd->argv = prepare_git_cmd(cmd->argv);
266 cmd->pid = mingw_spawnvpe(cmd->argv[0], cmd->argv, env);
267 failed_errno = errno;
268 if (cmd->pid < 0 && (!cmd->silent_exec_failure || errno != ENOENT))
269 error("cannot spawn %s: %s", cmd->argv[0], strerror(errno));
271 if (cmd->env)
272 free_environ(env);
273 if (cmd->git_cmd)
274 free(cmd->argv);
276 cmd->argv = sargv;
277 if (s0 >= 0)
278 dup2(s0, 0), close(s0);
279 if (s1 >= 0)
280 dup2(s1, 1), close(s1);
281 if (s2 >= 0)
282 dup2(s2, 2), close(s2);
284 #endif
286 if (cmd->pid < 0) {
287 if (need_in)
288 close_pair(fdin);
289 else if (cmd->in)
290 close(cmd->in);
291 if (need_out)
292 close_pair(fdout);
293 else if (cmd->out)
294 close(cmd->out);
295 if (need_err)
296 close_pair(fderr);
297 errno = failed_errno;
298 return -1;
301 if (need_in)
302 close(fdin[0]);
303 else if (cmd->in)
304 close(cmd->in);
306 if (need_out)
307 close(fdout[1]);
308 else if (cmd->out)
309 close(cmd->out);
311 if (need_err)
312 close(fderr[1]);
314 return 0;
317 int finish_command(struct child_process *cmd)
319 return wait_or_whine(cmd->pid, cmd->argv[0], cmd->silent_exec_failure);
322 int run_command(struct child_process *cmd)
324 int code = start_command(cmd);
325 if (code)
326 return code;
327 return finish_command(cmd);
330 static void prepare_run_command_v_opt(struct child_process *cmd,
331 const char **argv,
332 int opt)
334 memset(cmd, 0, sizeof(*cmd));
335 cmd->argv = argv;
336 cmd->no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
337 cmd->git_cmd = opt & RUN_GIT_CMD ? 1 : 0;
338 cmd->stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
339 cmd->silent_exec_failure = opt & RUN_SILENT_EXEC_FAILURE ? 1 : 0;
342 int run_command_v_opt(const char **argv, int opt)
344 struct child_process cmd;
345 prepare_run_command_v_opt(&cmd, argv, opt);
346 return run_command(&cmd);
349 int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env)
351 struct child_process cmd;
352 prepare_run_command_v_opt(&cmd, argv, opt);
353 cmd.dir = dir;
354 cmd.env = env;
355 return run_command(&cmd);
358 #ifdef WIN32
359 static unsigned __stdcall run_thread(void *data)
361 struct async *async = data;
362 return async->proc(async->fd_for_proc, async->data);
364 #endif
366 int start_async(struct async *async)
368 int pipe_out[2];
370 if (pipe(pipe_out) < 0)
371 return error("cannot create pipe: %s", strerror(errno));
372 async->out = pipe_out[0];
374 #ifndef WIN32
375 /* Flush stdio before fork() to avoid cloning buffers */
376 fflush(NULL);
378 async->pid = fork();
379 if (async->pid < 0) {
380 error("fork (async) failed: %s", strerror(errno));
381 close_pair(pipe_out);
382 return -1;
384 if (!async->pid) {
385 close(pipe_out[0]);
386 exit(!!async->proc(pipe_out[1], async->data));
388 close(pipe_out[1]);
389 #else
390 async->fd_for_proc = pipe_out[1];
391 async->tid = (HANDLE) _beginthreadex(NULL, 0, run_thread, async, 0, NULL);
392 if (!async->tid) {
393 error("cannot create thread: %s", strerror(errno));
394 close_pair(pipe_out);
395 return -1;
397 #endif
398 return 0;
401 int finish_async(struct async *async)
403 #ifndef WIN32
404 int ret = wait_or_whine(async->pid, "child process", 0);
405 #else
406 DWORD ret = 0;
407 if (WaitForSingleObject(async->tid, INFINITE) != WAIT_OBJECT_0)
408 ret = error("waiting for thread failed: %lu", GetLastError());
409 else if (!GetExitCodeThread(async->tid, &ret))
410 ret = error("cannot get thread exit code: %lu", GetLastError());
411 CloseHandle(async->tid);
412 #endif
413 return ret;
416 int run_hook(const char *index_file, const char *name, ...)
418 struct child_process hook;
419 const char **argv = NULL, *env[2];
420 char index[PATH_MAX];
421 va_list args;
422 int ret;
423 size_t i = 0, alloc = 0;
425 if (access(git_path("hooks/%s", name), X_OK) < 0)
426 return 0;
428 va_start(args, name);
429 ALLOC_GROW(argv, i + 1, alloc);
430 argv[i++] = git_path("hooks/%s", name);
431 while (argv[i-1]) {
432 ALLOC_GROW(argv, i + 1, alloc);
433 argv[i++] = va_arg(args, const char *);
435 va_end(args);
437 memset(&hook, 0, sizeof(hook));
438 hook.argv = argv;
439 hook.no_stdin = 1;
440 hook.stdout_to_stderr = 1;
441 if (index_file) {
442 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
443 env[0] = index;
444 env[1] = NULL;
445 hook.env = env;
448 ret = run_command(&hook);
449 free(argv);
450 return ret;