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];
24 * In case of errors we must keep the promise to close FDs
25 * that have been passed in via ->in and ->out.
28 need_in
= !cmd
->no_stdin
&& cmd
->in
< 0;
33 return -ERR_RUN_COMMAND_PIPE
;
38 need_out
= !cmd
->no_stdout
39 && !cmd
->stdout_to_stderr
42 if (pipe(fdout
) < 0) {
47 return -ERR_RUN_COMMAND_PIPE
;
52 need_err
= !cmd
->no_stderr
&& cmd
->err
< 0;
54 if (pipe(fderr
) < 0) {
63 return -ERR_RUN_COMMAND_PIPE
;
90 else if (cmd
->stdout_to_stderr
)
95 } else if (cmd
->out
> 1) {
100 if (cmd
->dir
&& chdir(cmd
->dir
))
101 die("exec %s: cd to %s failed (%s)", cmd
->argv
[0],
102 cmd
->dir
, strerror(errno
));
104 for (; *cmd
->env
; cmd
->env
++) {
105 if (strchr(*cmd
->env
, '='))
106 putenv((char*)*cmd
->env
);
112 execv_git_cmd(cmd
->argv
);
114 execvp(cmd
->argv
[0], (char *const*) cmd
->argv
);
116 die("exec %s failed.", cmd
->argv
[0]);
119 int s0
= -1, s1
= -1, s2
= -1; /* backups of stdin, stdout, stderr */
120 const char *sargv0
= cmd
->argv
[0];
121 char **env
= environ
;
122 struct strbuf git_cmd
;
127 } else if (need_in
) {
130 } else if (cmd
->in
) {
135 if (cmd
->no_stderr
) {
138 } else if (need_err
) {
143 if (cmd
->no_stdout
) {
146 } else if (cmd
->stdout_to_stderr
) {
149 } else if (need_out
) {
152 } else if (cmd
->out
> 1) {
158 die("chdir in start_command() not implemented");
160 env
= copy_environ();
161 for (; *cmd
->env
; cmd
->env
++)
162 env
= env_setenv(env
, *cmd
->env
);
166 strbuf_init(&git_cmd
, 0);
167 strbuf_addf(&git_cmd
, "git-%s", cmd
->argv
[0]);
168 cmd
->argv
[0] = git_cmd
.buf
;
171 cmd
->pid
= spawnvpe(_P_NOWAIT
, cmd
->argv
[0], cmd
->argv
, (const char **)env
);
176 strbuf_release(&git_cmd
);
178 cmd
->argv
[0] = sargv0
;
180 dup2(s0
, 0), close(s0
);
182 dup2(s1
, 1), close(s1
);
184 dup2(s2
, 2), close(s2
);
198 return -ERR_RUN_COMMAND_FORK
;
217 static int wait_or_whine(pid_t pid
)
221 pid_t waiting
= waitpid(pid
, &status
, 0);
226 error("waitpid failed (%s)", strerror(errno
));
227 return -ERR_RUN_COMMAND_WAITPID
;
230 return -ERR_RUN_COMMAND_WAITPID_WRONG_PID
;
231 if (WIFSIGNALED(status
))
232 return -ERR_RUN_COMMAND_WAITPID_SIGNAL
;
234 if (!WIFEXITED(status
))
235 return -ERR_RUN_COMMAND_WAITPID_NOEXIT
;
236 code
= WEXITSTATUS(status
);
243 int finish_command(struct child_process
*cmd
)
245 return wait_or_whine(cmd
->pid
);
248 int run_command(struct child_process
*cmd
)
250 int code
= start_command(cmd
);
253 return finish_command(cmd
);
256 static void prepare_run_command_v_opt(struct child_process
*cmd
,
260 memset(cmd
, 0, sizeof(*cmd
));
262 cmd
->no_stdin
= opt
& RUN_COMMAND_NO_STDIN
? 1 : 0;
263 cmd
->git_cmd
= opt
& RUN_GIT_CMD
? 1 : 0;
264 cmd
->stdout_to_stderr
= opt
& RUN_COMMAND_STDOUT_TO_STDERR
? 1 : 0;
267 int run_command_v_opt(const char **argv
, int opt
)
269 struct child_process cmd
;
270 prepare_run_command_v_opt(&cmd
, argv
, opt
);
271 return run_command(&cmd
);
274 int run_command_v_opt_cd(const char **argv
, int opt
, const char *dir
)
276 struct child_process cmd
;
277 prepare_run_command_v_opt(&cmd
, argv
, opt
);
279 return run_command(&cmd
);
282 int run_command_v_opt_cd_env(const char **argv
, int opt
, const char *dir
, const char *const *env
)
284 struct child_process cmd
;
285 prepare_run_command_v_opt(&cmd
, argv
, opt
);
288 return run_command(&cmd
);
292 static __stdcall
unsigned run_thread(void *data
)
294 struct async
*async
= data
;
295 return async
->proc(async
->fd_for_proc
, async
->data
);
299 int start_async(struct async
*async
)
303 if (pipe(pipe_out
) < 0)
304 return error("cannot create pipe: %s", strerror(errno
));
305 async
->out
= pipe_out
[0];
309 if (async
->pid
< 0) {
310 error("fork (async) failed: %s", strerror(errno
));
311 close_pair(pipe_out
);
316 exit(!!async
->proc(pipe_out
[1], async
->data
));
320 async
->fd_for_proc
= pipe_out
[1];
321 async
->tid
= (HANDLE
) _beginthreadex(NULL
, 0, run_thread
, async
, 0, NULL
);
323 error("cannot create thread: %s", strerror(errno
));
324 close_pair(pipe_out
);
331 int finish_async(struct async
*async
)
336 if (wait_or_whine(async
->pid
))
337 ret
= error("waitpid (async) failed");
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
);