2 #include "run-command.h"
4 #include "spawn-pipe.h"
6 static inline void close_pair(int fd
[2])
12 int start_command(struct child_process
*cmd
)
14 int need_in
, need_out
, need_err
;
15 int fdin
[2] = { -1, -1 };
16 int fdout
[2] = { -1, -1 };
17 int fderr
[2] = { -1, -1 };
20 need_in
= !cmd
->no_stdin
&& cmd
->in
< 0;
23 return -ERR_RUN_COMMAND_PIPE
;
28 need_out
= !cmd
->no_stdout
29 && !cmd
->stdout_to_stderr
32 if (pipe(fdout
) < 0) {
35 return -ERR_RUN_COMMAND_PIPE
;
41 need_err
= !cmd
->no_stderr
&& cmd
->err
< 0;
43 if (pipe(fderr
) < 0) {
48 return -ERR_RUN_COMMAND_PIPE
;
55 fdin
[0] = open("/dev/null", O_RDWR
);
63 fdout
[1] = open("/dev/null", O_RDWR
);
64 else if (cmd
->stdout_to_stderr
)
68 } else if (cmd
->out
> 1) {
73 fderr
[1] = open("/dev/null", O_RDWR
);
79 die("chdir in start_command() not implemented");
80 if (cmd
->dir
&& chdir(cmd
->dir
))
81 die("exec %s: cd to %s failed (%s)", cmd
->argv
[0],
82 cmd
->dir
, strerror(errno
));
85 die("modifying environment for git_cmd in start_command() not implemented");
87 for (; *cmd
->env
; cmd
->env
++) {
88 if (strchr(*cmd
->env
, '='))
89 die("setting environment in start_command() not implemented");
91 env_unsetenv(env
, *cmd
->env
);
95 cmd
->pid
= spawnv_git_cmd(cmd
->argv
, fdin
, fdout
);
97 cmd
->pid
= spawnvpe_pipe(cmd
->argv
[0], cmd
->argv
, env
, fdin
, fdout
);
107 return -ERR_RUN_COMMAND_FORK
;
113 static int wait_or_whine(pid_t pid
)
117 pid_t waiting
= waitpid(pid
, &status
, 0);
122 error("waitpid failed (%s)", strerror(errno
));
123 return -ERR_RUN_COMMAND_WAITPID
;
126 return -ERR_RUN_COMMAND_WAITPID_WRONG_PID
;
127 if (WIFSIGNALED(status
))
128 return -ERR_RUN_COMMAND_WAITPID_SIGNAL
;
130 if (!WIFEXITED(status
))
131 return -ERR_RUN_COMMAND_WAITPID_NOEXIT
;
132 code
= WEXITSTATUS(status
);
139 int finish_command(struct child_process
*cmd
)
145 return wait_or_whine(cmd
->pid
);
148 int run_command(struct child_process
*cmd
)
150 int code
= start_command(cmd
);
153 return finish_command(cmd
);
156 static void prepare_run_command_v_opt(struct child_process
*cmd
,
160 memset(cmd
, 0, sizeof(*cmd
));
162 cmd
->no_stdin
= opt
& RUN_COMMAND_NO_STDIN
? 1 : 0;
163 cmd
->git_cmd
= opt
& RUN_GIT_CMD
? 1 : 0;
164 cmd
->stdout_to_stderr
= opt
& RUN_COMMAND_STDOUT_TO_STDERR
? 1 : 0;
167 int run_command_v_opt(const char **argv
, int opt
)
169 struct child_process cmd
;
170 prepare_run_command_v_opt(&cmd
, argv
, opt
);
171 return run_command(&cmd
);
174 int run_command_v_opt_cd(const char **argv
, int opt
, const char *dir
)
176 struct child_process cmd
;
177 prepare_run_command_v_opt(&cmd
, argv
, opt
);
179 return run_command(&cmd
);
182 int run_command_v_opt_cd_env(const char **argv
, int opt
, const char *dir
, const char *const *env
)
184 struct child_process cmd
;
185 prepare_run_command_v_opt(&cmd
, argv
, opt
);
188 return run_command(&cmd
);
192 static __stdcall
unsigned run_thread(void *data
)
194 struct async
*async
= data
;
195 return async
->proc(async
->fd_for_proc
, async
->data
);
199 int start_async(struct async
*async
)
203 if (pipe(pipe_out
) < 0)
204 return error("cannot create pipe: %s", strerror(errno
));
205 async
->out
= pipe_out
[0];
209 if (async
->pid
< 0) {
210 error("fork (async) failed: %s", strerror(errno
));
211 close_pair(pipe_out
);
216 exit(!!async
->proc(pipe_out
[1], async
->data
));
220 async
->fd_for_proc
= pipe_out
[1];
221 async
->tid
= (HANDLE
) _beginthreadex(NULL
, 0, run_thread
, async
, 0, NULL
);
223 error("cannot create thread: %s", strerror(errno
));
224 close_pair(pipe_out
);
231 int finish_async(struct async
*async
)
236 if (wait_or_whine(async
->pid
))
237 ret
= error("waitpid (async) failed");
240 if (WaitForSingleObject(async
->tid
, INFINITE
) != WAIT_OBJECT_0
)
241 ret
= error("waiting for thread failed: %lu", GetLastError());
242 else if (!GetExitCodeThread(async
->tid
, &ret
))
243 ret
= error("cannot get thread exit code: %lu", GetLastError());
244 CloseHandle(async
->tid
);