2 #include "run-command.h"
5 static inline void close_pair(int fd
[2])
11 static inline void dup_devnull(int to
)
13 int fd
= open("/dev/null", O_RDWR
);
18 int start_command(struct child_process
*cmd
)
20 int need_in
, need_out
, need_err
;
21 int fdin
[2], fdout
[2], fderr
[2];
22 int failed_errno
= failed_errno
;
25 * In case of errors we must keep the promise to close FDs
26 * that have been passed in via ->in and ->out.
29 need_in
= !cmd
->no_stdin
&& cmd
->in
< 0;
40 need_out
= !cmd
->no_stdout
41 && !cmd
->stdout_to_stderr
44 if (pipe(fdout
) < 0) {
55 need_err
= !cmd
->no_stderr
&& cmd
->err
< 0;
57 if (pipe(fderr
) < 0) {
68 error("cannot create pipe for %s: %s",
69 cmd
->argv
[0], strerror(failed_errno
));
76 trace_argv_printf(cmd
->argv
, "trace: run_command:");
97 } else if (cmd
->err
> 1) {
104 else if (cmd
->stdout_to_stderr
)
109 } else if (cmd
->out
> 1) {
114 if (cmd
->dir
&& chdir(cmd
->dir
))
115 die_errno("exec '%s': cd to '%s' failed", cmd
->argv
[0],
118 for (; *cmd
->env
; cmd
->env
++) {
119 if (strchr(*cmd
->env
, '='))
120 putenv((char *)*cmd
->env
);
128 execv_git_cmd(cmd
->argv
);
130 execvp(cmd
->argv
[0], (char *const*) cmd
->argv
);
132 trace_printf("trace: exec '%s' failed: %s\n", cmd
->argv
[0],
137 error("cannot fork() for %s: %s", cmd
->argv
[0],
138 strerror(failed_errno
= errno
));
141 int s0
= -1, s1
= -1, s2
= -1; /* backups of stdin, stdout, stderr */
142 const char **sargv
= cmd
->argv
;
143 char **env
= environ
;
148 } else if (need_in
) {
151 } else if (cmd
->in
) {
156 if (cmd
->no_stderr
) {
159 } else if (need_err
) {
162 } else if (cmd
->err
> 2) {
167 if (cmd
->no_stdout
) {
170 } else if (cmd
->stdout_to_stderr
) {
173 } else if (need_out
) {
176 } else if (cmd
->out
> 1) {
182 die("chdir in start_command() not implemented");
184 env
= make_augmented_environ(cmd
->env
);
187 cmd
->argv
= prepare_git_cmd(cmd
->argv
);
190 cmd
->pid
= mingw_spawnvpe(cmd
->argv
[0], cmd
->argv
, env
);
191 failed_errno
= errno
;
192 if (cmd
->pid
< 0 && (!cmd
->silent_exec_failure
|| errno
!= ENOENT
))
193 error("cannot spawn %s: %s", cmd
->argv
[0], strerror(errno
));
202 dup2(s0
, 0), close(s0
);
204 dup2(s1
, 1), close(s1
);
206 dup2(s2
, 2), close(s2
);
221 errno
= failed_errno
;
243 static int wait_or_whine(pid_t pid
, const char *argv0
, int silent_exec_failure
)
245 int status
, code
= -1;
247 int failed_errno
= 0;
249 while ((waiting
= waitpid(pid
, &status
, 0)) < 0 && errno
== EINTR
)
253 failed_errno
= errno
;
254 error("waitpid for %s failed: %s", argv0
, strerror(errno
));
255 } else if (waiting
!= pid
) {
256 error("waitpid is confused (%s)", argv0
);
257 } else if (WIFSIGNALED(status
)) {
258 code
= WTERMSIG(status
);
259 error("%s died of signal %d", argv0
, code
);
261 * This return value is chosen so that code & 0xff
262 * mimics the exit code that a POSIX shell would report for
263 * a program that died from this signal.
266 } else if (WIFEXITED(status
)) {
267 code
= WEXITSTATUS(status
);
269 * Convert special exit code when execvp failed.
273 failed_errno
= ENOENT
;
274 if (!silent_exec_failure
)
275 error("cannot run %s: %s", argv0
,
279 error("waitpid is confused (%s)", argv0
);
281 errno
= failed_errno
;
285 int finish_command(struct child_process
*cmd
)
287 return wait_or_whine(cmd
->pid
, cmd
->argv
[0], cmd
->silent_exec_failure
);
290 int run_command(struct child_process
*cmd
)
292 int code
= start_command(cmd
);
295 return finish_command(cmd
);
298 static void prepare_run_command_v_opt(struct child_process
*cmd
,
302 memset(cmd
, 0, sizeof(*cmd
));
304 cmd
->no_stdin
= opt
& RUN_COMMAND_NO_STDIN
? 1 : 0;
305 cmd
->git_cmd
= opt
& RUN_GIT_CMD
? 1 : 0;
306 cmd
->stdout_to_stderr
= opt
& RUN_COMMAND_STDOUT_TO_STDERR
? 1 : 0;
307 cmd
->silent_exec_failure
= opt
& RUN_SILENT_EXEC_FAILURE
? 1 : 0;
310 int run_command_v_opt(const char **argv
, int opt
)
312 struct child_process cmd
;
313 prepare_run_command_v_opt(&cmd
, argv
, opt
);
314 return run_command(&cmd
);
317 int run_command_v_opt_cd_env(const char **argv
, int opt
, const char *dir
, const char *const *env
)
319 struct child_process cmd
;
320 prepare_run_command_v_opt(&cmd
, argv
, opt
);
323 return run_command(&cmd
);
327 static unsigned __stdcall
run_thread(void *data
)
329 struct async
*async
= data
;
330 return async
->proc(async
->fd_for_proc
, async
->data
);
334 int start_async(struct async
*async
)
338 if (pipe(pipe_out
) < 0)
339 return error("cannot create pipe: %s", strerror(errno
));
340 async
->out
= pipe_out
[0];
343 /* Flush stdio before fork() to avoid cloning buffers */
347 if (async
->pid
< 0) {
348 error("fork (async) failed: %s", strerror(errno
));
349 close_pair(pipe_out
);
354 exit(!!async
->proc(pipe_out
[1], async
->data
));
358 async
->fd_for_proc
= pipe_out
[1];
359 async
->tid
= (HANDLE
) _beginthreadex(NULL
, 0, run_thread
, async
, 0, NULL
);
361 error("cannot create thread: %s", strerror(errno
));
362 close_pair(pipe_out
);
369 int finish_async(struct async
*async
)
372 int ret
= wait_or_whine(async
->pid
, "child process", 0);
375 if (WaitForSingleObject(async
->tid
, INFINITE
) != WAIT_OBJECT_0
)
376 ret
= error("waiting for thread failed: %lu", GetLastError());
377 else if (!GetExitCodeThread(async
->tid
, &ret
))
378 ret
= error("cannot get thread exit code: %lu", GetLastError());
379 CloseHandle(async
->tid
);
384 int run_hook(const char *index_file
, const char *name
, ...)
386 struct child_process hook
;
387 const char **argv
= NULL
, *env
[2];
388 char index
[PATH_MAX
];
391 size_t i
= 0, alloc
= 0;
393 if (access(git_path("hooks/%s", name
), X_OK
) < 0)
396 va_start(args
, name
);
397 ALLOC_GROW(argv
, i
+ 1, alloc
);
398 argv
[i
++] = git_path("hooks/%s", name
);
400 ALLOC_GROW(argv
, i
+ 1, alloc
);
401 argv
[i
++] = va_arg(args
, const char *);
405 memset(&hook
, 0, sizeof(hook
));
408 hook
.stdout_to_stderr
= 1;
410 snprintf(index
, sizeof(index
), "GIT_INDEX_FILE=%s", index_file
);
416 ret
= run_command(&hook
);