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
;
68 trace_argv_printf(cmd
->argv
, "trace: run_command:");
93 else if (cmd
->stdout_to_stderr
)
98 } else if (cmd
->out
> 1) {
103 if (cmd
->dir
&& chdir(cmd
->dir
))
104 die("exec %s: cd to %s failed (%s)", cmd
->argv
[0],
105 cmd
->dir
, strerror(errno
));
107 for (; *cmd
->env
; cmd
->env
++) {
108 if (strchr(*cmd
->env
, '='))
109 putenv((char*)*cmd
->env
);
117 execv_git_cmd(cmd
->argv
);
119 execvp(cmd
->argv
[0], (char *const*) cmd
->argv
);
121 die("exec %s failed.", cmd
->argv
[0]);
124 int s0
= -1, s1
= -1, s2
= -1; /* backups of stdin, stdout, stderr */
125 const char **sargv
= cmd
->argv
;
126 char **env
= environ
;
131 } else if (need_in
) {
134 } else if (cmd
->in
) {
139 if (cmd
->no_stderr
) {
142 } else if (need_err
) {
147 if (cmd
->no_stdout
) {
150 } else if (cmd
->stdout_to_stderr
) {
153 } else if (need_out
) {
156 } else if (cmd
->out
> 1) {
162 die("chdir in start_command() not implemented");
164 env
= copy_environ();
165 for (; *cmd
->env
; cmd
->env
++)
166 env
= env_setenv(env
, *cmd
->env
);
170 cmd
->argv
= prepare_git_cmd(cmd
->argv
);
173 cmd
->pid
= mingw_spawnvpe(cmd
->argv
[0], cmd
->argv
, env
);
182 dup2(s0
, 0), close(s0
);
184 dup2(s1
, 1), close(s1
);
186 dup2(s2
, 2), close(s2
);
200 return -ERR_RUN_COMMAND_FORK
;
219 static int wait_or_whine(pid_t pid
)
223 pid_t waiting
= waitpid(pid
, &status
, 0);
228 error("waitpid failed (%s)", strerror(errno
));
229 return -ERR_RUN_COMMAND_WAITPID
;
232 return -ERR_RUN_COMMAND_WAITPID_WRONG_PID
;
233 if (WIFSIGNALED(status
))
234 return -ERR_RUN_COMMAND_WAITPID_SIGNAL
;
236 if (!WIFEXITED(status
))
237 return -ERR_RUN_COMMAND_WAITPID_NOEXIT
;
238 code
= WEXITSTATUS(status
);
245 int finish_command(struct child_process
*cmd
)
247 return wait_or_whine(cmd
->pid
);
250 int run_command(struct child_process
*cmd
)
252 int code
= start_command(cmd
);
255 return finish_command(cmd
);
258 static void prepare_run_command_v_opt(struct child_process
*cmd
,
262 memset(cmd
, 0, sizeof(*cmd
));
264 cmd
->no_stdin
= opt
& RUN_COMMAND_NO_STDIN
? 1 : 0;
265 cmd
->git_cmd
= opt
& RUN_GIT_CMD
? 1 : 0;
266 cmd
->stdout_to_stderr
= opt
& RUN_COMMAND_STDOUT_TO_STDERR
? 1 : 0;
269 int run_command_v_opt(const char **argv
, int opt
)
271 struct child_process cmd
;
272 prepare_run_command_v_opt(&cmd
, argv
, opt
);
273 return run_command(&cmd
);
276 int run_command_v_opt_cd(const char **argv
, int opt
, const char *dir
)
278 struct child_process cmd
;
279 prepare_run_command_v_opt(&cmd
, argv
, opt
);
281 return run_command(&cmd
);
284 int run_command_v_opt_cd_env(const char **argv
, int opt
, const char *dir
, const char *const *env
)
286 struct child_process cmd
;
287 prepare_run_command_v_opt(&cmd
, argv
, opt
);
290 return run_command(&cmd
);
294 static __stdcall
unsigned run_thread(void *data
)
296 struct async
*async
= data
;
297 return async
->proc(async
->fd_for_proc
, async
->data
);
301 int start_async(struct async
*async
)
305 if (pipe(pipe_out
) < 0)
306 return error("cannot create pipe: %s", strerror(errno
));
307 async
->out
= pipe_out
[0];
310 /* Flush stdio before fork() to avoid cloning buffers */
314 if (async
->pid
< 0) {
315 error("fork (async) failed: %s", strerror(errno
));
316 close_pair(pipe_out
);
321 exit(!!async
->proc(pipe_out
[1], async
->data
));
325 async
->fd_for_proc
= pipe_out
[1];
326 async
->tid
= (HANDLE
) _beginthreadex(NULL
, 0, run_thread
, async
, 0, NULL
);
328 error("cannot create thread: %s", strerror(errno
));
329 close_pair(pipe_out
);
336 int finish_async(struct async
*async
)
341 if (wait_or_whine(async
->pid
))
342 ret
= error("waitpid (async) failed");
345 if (WaitForSingleObject(async
->tid
, INFINITE
) != WAIT_OBJECT_0
)
346 ret
= error("waiting for thread failed: %lu", GetLastError());
347 else if (!GetExitCodeThread(async
->tid
, &ret
))
348 ret
= error("cannot get thread exit code: %lu", GetLastError());
349 CloseHandle(async
->tid
);