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];
23 need_in
= !cmd
->no_stdin
&& cmd
->in
< 0;
26 return -ERR_RUN_COMMAND_PIPE
;
31 need_out
= !cmd
->no_stdout
32 && !cmd
->stdout_to_stderr
35 if (pipe(fdout
) < 0) {
38 return -ERR_RUN_COMMAND_PIPE
;
44 need_err
= !cmd
->no_stderr
&& cmd
->err
< 0;
46 if (pipe(fderr
) < 0) {
51 return -ERR_RUN_COMMAND_PIPE
;
71 else if (cmd
->stdout_to_stderr
)
76 } else if (cmd
->out
> 1) {
88 if (cmd
->dir
&& chdir(cmd
->dir
))
89 die("exec %s: cd to %s failed (%s)", cmd
->argv
[0],
90 cmd
->dir
, strerror(errno
));
92 for (; *cmd
->env
; cmd
->env
++) {
93 if (strchr(*cmd
->env
, '='))
94 putenv((char*)*cmd
->env
);
100 execv_git_cmd(cmd
->argv
);
102 execvp(cmd
->argv
[0], (char *const*) cmd
->argv
);
104 die("exec %s failed.", cmd
->argv
[0]);
107 int s0
= -1, s1
= -1, s2
= -1; /* backups of stdin, stdout, stderr */
108 const char *sargv0
= cmd
->argv
[0];
109 char **env
= environ
;
110 struct strbuf git_cmd
;
115 } else if (need_in
) {
118 } else if (cmd
->in
) {
123 if (cmd
->no_stdout
) {
126 } else if (cmd
->stdout_to_stderr
) {
129 } else if (need_out
) {
132 } else if (cmd
->out
> 1) {
137 if (cmd
->no_stderr
) {
140 } else if (need_err
) {
146 die("chdir in start_command() not implemented");
148 env
= copy_environ();
149 for (; *cmd
->env
; cmd
->env
++) {
150 if (strchr(*cmd
->env
, '='))
151 die("setting environment in start_command() not implemented");
153 env_unsetenv(env
, *cmd
->env
);
158 strbuf_init(&git_cmd
, 0);
159 strbuf_addf(&git_cmd
, "git-%s", cmd
->argv
[0]);
160 cmd
->argv
[0] = git_cmd
.buf
;
163 cmd
->pid
= mingw_spawnvpe(cmd
->argv
[0], cmd
->argv
, env
);
165 /* TODO: if (cmd->env) free env; */
168 strbuf_release(&git_cmd
);
170 cmd
->argv
[0] = sargv0
;
172 dup2(s0
, 0), close(s0
);
174 dup2(s1
, 1), close(s1
);
176 dup2(s2
, 2), close(s2
);
186 return -ERR_RUN_COMMAND_FORK
;
196 else if (cmd
->out
> 1)
205 static int wait_or_whine(pid_t pid
)
209 pid_t waiting
= waitpid(pid
, &status
, 0);
214 error("waitpid failed (%s)", strerror(errno
));
215 return -ERR_RUN_COMMAND_WAITPID
;
218 return -ERR_RUN_COMMAND_WAITPID_WRONG_PID
;
219 if (WIFSIGNALED(status
))
220 return -ERR_RUN_COMMAND_WAITPID_SIGNAL
;
222 if (!WIFEXITED(status
))
223 return -ERR_RUN_COMMAND_WAITPID_NOEXIT
;
224 code
= WEXITSTATUS(status
);
231 int finish_command(struct child_process
*cmd
)
237 return wait_or_whine(cmd
->pid
);
240 int run_command(struct child_process
*cmd
)
242 int code
= start_command(cmd
);
245 return finish_command(cmd
);
248 static void prepare_run_command_v_opt(struct child_process
*cmd
,
252 memset(cmd
, 0, sizeof(*cmd
));
254 cmd
->no_stdin
= opt
& RUN_COMMAND_NO_STDIN
? 1 : 0;
255 cmd
->git_cmd
= opt
& RUN_GIT_CMD
? 1 : 0;
256 cmd
->stdout_to_stderr
= opt
& RUN_COMMAND_STDOUT_TO_STDERR
? 1 : 0;
259 int run_command_v_opt(const char **argv
, int opt
)
261 struct child_process cmd
;
262 prepare_run_command_v_opt(&cmd
, argv
, opt
);
263 return run_command(&cmd
);
266 int run_command_v_opt_cd(const char **argv
, int opt
, const char *dir
)
268 struct child_process cmd
;
269 prepare_run_command_v_opt(&cmd
, argv
, opt
);
271 return run_command(&cmd
);
274 int run_command_v_opt_cd_env(const char **argv
, int opt
, const char *dir
, const char *const *env
)
276 struct child_process cmd
;
277 prepare_run_command_v_opt(&cmd
, argv
, opt
);
280 return run_command(&cmd
);
284 static __stdcall
unsigned run_thread(void *data
)
286 struct async
*async
= data
;
287 return async
->proc(async
->fd_for_proc
, async
->data
);
291 int start_async(struct async
*async
)
295 if (pipe(pipe_out
) < 0)
296 return error("cannot create pipe: %s", strerror(errno
));
297 async
->out
= pipe_out
[0];
301 if (async
->pid
< 0) {
302 error("fork (async) failed: %s", strerror(errno
));
303 close_pair(pipe_out
);
308 exit(!!async
->proc(pipe_out
[1], async
->data
));
312 async
->fd_for_proc
= pipe_out
[1];
313 async
->tid
= (HANDLE
) _beginthreadex(NULL
, 0, run_thread
, async
, 0, NULL
);
315 error("cannot create thread: %s", strerror(errno
));
316 close_pair(pipe_out
);
323 int finish_async(struct async
*async
)
328 if (wait_or_whine(async
->pid
))
329 ret
= error("waitpid (async) failed");
332 if (WaitForSingleObject(async
->tid
, INFINITE
) != WAIT_OBJECT_0
)
333 ret
= error("waiting for thread failed: %lu", GetLastError());
334 else if (!GetExitCodeThread(async
->tid
, &ret
))
335 ret
= error("cannot get thread exit code: %lu", GetLastError());
336 CloseHandle(async
->tid
);