2 #include "run-command.h"
6 static inline void close_pair(int fd
[2])
12 static inline void dup_devnull(int to
)
14 int fd
= open("/dev/null", O_RDWR
);
19 int start_command(struct child_process
*cmd
)
21 int need_in
, need_out
, need_err
;
22 int fdin
[2], fdout
[2], fderr
[2];
23 char sbuf
[STRERR_BUFSIZE
];
26 * In case of errors we must keep the promise to close FDs
27 * that have been passed in via ->in and ->out.
30 need_in
= !cmd
->no_stdin
&& cmd
->in
< 0;
35 return -ERR_RUN_COMMAND_PIPE
;
40 need_out
= !cmd
->no_stdout
41 && !cmd
->stdout_to_stderr
44 if (pipe(fdout
) < 0) {
49 return -ERR_RUN_COMMAND_PIPE
;
54 need_err
= !cmd
->no_stderr
&& cmd
->err
< 0;
56 if (pipe(fderr
) < 0) {
65 return -ERR_RUN_COMMAND_PIPE
;
92 else if (cmd
->stdout_to_stderr
)
97 } else if (cmd
->out
> 1) {
102 if (cmd
->dir
&& chdir(cmd
->dir
))
103 die("exec %s: cd to %s failed (%s)", cmd
->argv
[0],
104 cmd
->dir
, strerror_r(errno
, sbuf
, sizeof(sbuf
)));
106 for (; *cmd
->env
; cmd
->env
++) {
107 if (strchr(*cmd
->env
, '='))
108 putenv((char*)*cmd
->env
);
116 execv_perf_cmd(cmd
->argv
);
118 execvp(cmd
->argv
[0], (char *const*) cmd
->argv
);
135 return err
== ENOENT
?
136 -ERR_RUN_COMMAND_EXEC
:
137 -ERR_RUN_COMMAND_FORK
;
156 static int wait_or_whine(pid_t pid
)
158 char sbuf
[STRERR_BUFSIZE
];
162 pid_t waiting
= waitpid(pid
, &status
, 0);
167 error("waitpid failed (%s)",
168 strerror_r(errno
, sbuf
, sizeof(sbuf
)));
169 return -ERR_RUN_COMMAND_WAITPID
;
172 return -ERR_RUN_COMMAND_WAITPID_WRONG_PID
;
173 if (WIFSIGNALED(status
))
174 return -ERR_RUN_COMMAND_WAITPID_SIGNAL
;
176 if (!WIFEXITED(status
))
177 return -ERR_RUN_COMMAND_WAITPID_NOEXIT
;
178 code
= WEXITSTATUS(status
);
181 return -ERR_RUN_COMMAND_EXEC
;
190 int finish_command(struct child_process
*cmd
)
192 return wait_or_whine(cmd
->pid
);
195 int run_command(struct child_process
*cmd
)
197 int code
= start_command(cmd
);
200 return finish_command(cmd
);
203 static void prepare_run_command_v_opt(struct child_process
*cmd
,
207 memset(cmd
, 0, sizeof(*cmd
));
209 cmd
->no_stdin
= opt
& RUN_COMMAND_NO_STDIN
? 1 : 0;
210 cmd
->perf_cmd
= opt
& RUN_PERF_CMD
? 1 : 0;
211 cmd
->stdout_to_stderr
= opt
& RUN_COMMAND_STDOUT_TO_STDERR
? 1 : 0;
214 int run_command_v_opt(const char **argv
, int opt
)
216 struct child_process cmd
;
217 prepare_run_command_v_opt(&cmd
, argv
, opt
);
218 return run_command(&cmd
);