8 #include "subcmd-util.h"
9 #include "run-command.h"
12 #define STRERR_BUFSIZE 128
14 static inline void close_pair(int fd
[2])
20 static inline void dup_devnull(int to
)
22 int fd
= open("/dev/null", O_RDWR
);
27 int start_command(struct child_process
*cmd
)
29 int need_in
, need_out
, need_err
;
30 int fdin
[2], fdout
[2], fderr
[2];
31 char sbuf
[STRERR_BUFSIZE
];
34 * In case of errors we must keep the promise to close FDs
35 * that have been passed in via ->in and ->out.
38 need_in
= !cmd
->no_stdin
&& cmd
->in
< 0;
43 return -ERR_RUN_COMMAND_PIPE
;
48 need_out
= !cmd
->no_stdout
49 && !cmd
->stdout_to_stderr
52 if (pipe(fdout
) < 0) {
57 return -ERR_RUN_COMMAND_PIPE
;
62 need_err
= !cmd
->no_stderr
&& cmd
->err
< 0;
64 if (pipe(fderr
) < 0) {
73 return -ERR_RUN_COMMAND_PIPE
;
100 else if (cmd
->stdout_to_stderr
)
105 } else if (cmd
->out
> 1) {
110 if (cmd
->dir
&& chdir(cmd
->dir
))
111 die("exec %s: cd to %s failed (%s)", cmd
->argv
[0],
112 cmd
->dir
, strerror_r(errno
, sbuf
, sizeof(sbuf
)));
114 for (; *cmd
->env
; cmd
->env
++) {
115 if (strchr(*cmd
->env
, '='))
116 putenv((char*)*cmd
->env
);
124 execv_cmd(cmd
->argv
);
126 execvp(cmd
->argv
[0], (char *const*) cmd
->argv
);
143 return err
== ENOENT
?
144 -ERR_RUN_COMMAND_EXEC
:
145 -ERR_RUN_COMMAND_FORK
;
164 static int wait_or_whine(pid_t pid
)
166 char sbuf
[STRERR_BUFSIZE
];
170 pid_t waiting
= waitpid(pid
, &status
, 0);
175 fprintf(stderr
, " Error: waitpid failed (%s)",
176 strerror_r(errno
, sbuf
, sizeof(sbuf
)));
177 return -ERR_RUN_COMMAND_WAITPID
;
180 return -ERR_RUN_COMMAND_WAITPID_WRONG_PID
;
181 if (WIFSIGNALED(status
))
182 return -ERR_RUN_COMMAND_WAITPID_SIGNAL
;
184 if (!WIFEXITED(status
))
185 return -ERR_RUN_COMMAND_WAITPID_NOEXIT
;
186 code
= WEXITSTATUS(status
);
189 return -ERR_RUN_COMMAND_EXEC
;
198 int finish_command(struct child_process
*cmd
)
200 return wait_or_whine(cmd
->pid
);
203 int run_command(struct child_process
*cmd
)
205 int code
= start_command(cmd
);
208 return finish_command(cmd
);
211 static void prepare_run_command_v_opt(struct child_process
*cmd
,
215 memset(cmd
, 0, sizeof(*cmd
));
217 cmd
->no_stdin
= opt
& RUN_COMMAND_NO_STDIN
? 1 : 0;
218 cmd
->exec_cmd
= opt
& RUN_EXEC_CMD
? 1 : 0;
219 cmd
->stdout_to_stderr
= opt
& RUN_COMMAND_STDOUT_TO_STDERR
? 1 : 0;
222 int run_command_v_opt(const char **argv
, int opt
)
224 struct child_process cmd
;
225 prepare_run_command_v_opt(&cmd
, argv
, opt
);
226 return run_command(&cmd
);