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;
90 else if (cmd->stdout_to_stderr)
95 } else if (cmd->out > 1) {
100 if (cmd->dir && chdir(cmd->dir))
101 die("exec %s: cd to %s failed (%s)", cmd->argv[0],
102 cmd->dir, strerror(errno));
104 for (; *cmd->env; cmd->env++) {
105 if (strchr(*cmd->env, '='))
106 putenv((char*)*cmd->env);
114 execv_perf_cmd(cmd->argv);
116 execvp(cmd->argv[0], (char *const*) cmd->argv);
133 return err == ENOENT ?
134 -ERR_RUN_COMMAND_EXEC :
135 -ERR_RUN_COMMAND_FORK;
154 static int wait_or_whine(pid_t pid)
158 pid_t waiting = waitpid(pid, &status, 0);
163 error("waitpid failed (%s)", strerror(errno));
164 return -ERR_RUN_COMMAND_WAITPID;
167 return -ERR_RUN_COMMAND_WAITPID_WRONG_PID;
168 if (WIFSIGNALED(status))
169 return -ERR_RUN_COMMAND_WAITPID_SIGNAL;
171 if (!WIFEXITED(status))
172 return -ERR_RUN_COMMAND_WAITPID_NOEXIT;
173 code = WEXITSTATUS(status);
176 return -ERR_RUN_COMMAND_EXEC;
185 int finish_command(struct child_process *cmd)
187 return wait_or_whine(cmd->pid);
190 int run_command(struct child_process *cmd)
192 int code = start_command(cmd);
195 return finish_command(cmd);
198 static void prepare_run_command_v_opt(struct child_process *cmd,
202 memset(cmd, 0, sizeof(*cmd));
204 cmd->no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
205 cmd->perf_cmd = opt & RUN_PERF_CMD ? 1 : 0;
206 cmd->stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
209 int run_command_v_opt(const char **argv, int opt)
211 struct child_process cmd;
212 prepare_run_command_v_opt(&cmd, argv, opt);
213 return run_command(&cmd);
216 int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env)
218 struct child_process cmd;
219 prepare_run_command_v_opt(&cmd, argv, opt);
222 return run_command(&cmd);
225 int start_async(struct async *async)
229 if (pipe(pipe_out) < 0)
230 return error("cannot create pipe: %s", strerror(errno));
231 async->out = pipe_out[0];
233 /* Flush stdio before fork() to avoid cloning buffers */
237 if (async->pid < 0) {
238 error("fork (async) failed: %s", strerror(errno));
239 close_pair(pipe_out);
244 exit(!!async->proc(pipe_out[1], async->data));
251 int finish_async(struct async *async)
255 if (wait_or_whine(async->pid))
256 ret = error("waitpid (async) failed");
261 int run_hook(const char *index_file, const char *name, ...)
263 struct child_process hook;
264 const char **argv = NULL, *env[2];
268 size_t i = 0, alloc = 0;
270 if (access(perf_path("hooks/%s", name), X_OK) < 0)
273 va_start(args, name);
274 ALLOC_GROW(argv, i + 1, alloc);
275 argv[i++] = perf_path("hooks/%s", name);
277 ALLOC_GROW(argv, i + 1, alloc);
278 argv[i++] = va_arg(args, const char *);
282 memset(&hook, 0, sizeof(hook));
285 hook.stdout_to_stderr = 1;
287 snprintf(idx, sizeof(idx), "PERF_INDEX_FILE=%s", index_file);
293 ret = start_command(&hook);
296 warning("Could not spawn %s", argv[0]);
299 ret = finish_command(&hook);
300 if (ret == -ERR_RUN_COMMAND_WAITPID_SIGNAL)
301 warning("%s exited due to uncaught signal", argv[0]);