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:");
82 return -ERR_RUN_COMMAND_FORK
;
105 else if (cmd
->stdout_to_stderr
)
110 } else if (cmd
->out
> 1) {
115 if (cmd
->dir
&& chdir(cmd
->dir
))
116 die("exec %s: cd to %s failed (%s)", cmd
->argv
[0],
117 cmd
->dir
, strerror(errno
));
119 for (; *cmd
->env
; cmd
->env
++) {
120 if (strchr(*cmd
->env
, '='))
121 putenv((char*)*cmd
->env
);
127 execv_git_cmd(cmd
->argv
);
129 execvp(cmd
->argv
[0], (char *const*) cmd
->argv
);
131 die("exec %s failed.", cmd
->argv
[0]);
150 static int wait_or_whine(pid_t pid
)
154 pid_t waiting
= waitpid(pid
, &status
, 0);
159 error("waitpid failed (%s)", strerror(errno
));
160 return -ERR_RUN_COMMAND_WAITPID
;
163 return -ERR_RUN_COMMAND_WAITPID_WRONG_PID
;
164 if (WIFSIGNALED(status
))
165 return -ERR_RUN_COMMAND_WAITPID_SIGNAL
;
167 if (!WIFEXITED(status
))
168 return -ERR_RUN_COMMAND_WAITPID_NOEXIT
;
169 code
= WEXITSTATUS(status
);
176 int finish_command(struct child_process
*cmd
)
178 return wait_or_whine(cmd
->pid
);
181 int run_command(struct child_process
*cmd
)
183 int code
= start_command(cmd
);
186 return finish_command(cmd
);
189 static void prepare_run_command_v_opt(struct child_process
*cmd
,
193 memset(cmd
, 0, sizeof(*cmd
));
195 cmd
->no_stdin
= opt
& RUN_COMMAND_NO_STDIN
? 1 : 0;
196 cmd
->git_cmd
= opt
& RUN_GIT_CMD
? 1 : 0;
197 cmd
->stdout_to_stderr
= opt
& RUN_COMMAND_STDOUT_TO_STDERR
? 1 : 0;
200 int run_command_v_opt(const char **argv
, int opt
)
202 struct child_process cmd
;
203 prepare_run_command_v_opt(&cmd
, argv
, opt
);
204 return run_command(&cmd
);
207 int run_command_v_opt_cd(const char **argv
, int opt
, const char *dir
)
209 struct child_process cmd
;
210 prepare_run_command_v_opt(&cmd
, argv
, opt
);
212 return run_command(&cmd
);
215 int run_command_v_opt_cd_env(const char **argv
, int opt
, const char *dir
, const char *const *env
)
217 struct child_process cmd
;
218 prepare_run_command_v_opt(&cmd
, argv
, opt
);
221 return run_command(&cmd
);
224 int start_async(struct async
*async
)
228 if (pipe(pipe_out
) < 0)
229 return error("cannot create pipe: %s", strerror(errno
));
232 if (async
->pid
< 0) {
233 error("fork (async) failed: %s", strerror(errno
));
234 close_pair(pipe_out
);
239 exit(!!async
->proc(pipe_out
[1], async
->data
));
241 async
->out
= pipe_out
[0];
246 int finish_async(struct async
*async
)
250 if (wait_or_whine(async
->pid
))
251 ret
= error("waitpid (async) failed");