1 // SPDX-License-Identifier: GPL-2.0
8 #include <linux/string.h>
11 #include "subcmd-util.h"
12 #include "run-command.h"
15 #define STRERR_BUFSIZE 128
17 static inline void close_pair(int fd[2])
23 static inline void dup_devnull(int to)
25 int fd = open("/dev/null", O_RDWR);
30 int start_command(struct child_process *cmd)
32 int need_in, need_out, need_err;
33 int fdin[2], fdout[2], fderr[2];
34 char sbuf[STRERR_BUFSIZE];
37 * In case of errors we must keep the promise to close FDs
38 * that have been passed in via ->in and ->out.
41 need_in = !cmd->no_stdin && cmd->in < 0;
46 return -ERR_RUN_COMMAND_PIPE;
51 need_out = !cmd->no_stdout
52 && !cmd->stdout_to_stderr
55 if (pipe(fdout) < 0) {
60 return -ERR_RUN_COMMAND_PIPE;
65 need_err = !cmd->no_stderr && cmd->err < 0;
67 if (pipe(fderr) < 0) {
76 return -ERR_RUN_COMMAND_PIPE;
103 else if (cmd->stdout_to_stderr)
108 } else if (cmd->out > 1) {
113 if (cmd->dir && chdir(cmd->dir))
114 die("exec %s: cd to %s failed (%s)", cmd->argv[0],
115 cmd->dir, str_error_r(errno, sbuf, sizeof(sbuf)));
117 for (; *cmd->env; cmd->env++) {
118 if (strchr(*cmd->env, '='))
119 putenv((char*)*cmd->env);
126 if (cmd->no_exec_cmd)
127 exit(cmd->no_exec_cmd(cmd));
129 execv_cmd(cmd->argv);
131 execvp(cmd->argv[0], (char *const*) cmd->argv);
148 return err == ENOENT ?
149 -ERR_RUN_COMMAND_EXEC :
150 -ERR_RUN_COMMAND_FORK;
169 static int wait_or_whine(struct child_process *cmd, bool block)
171 bool finished = cmd->finished;
172 int result = cmd->finish_result;
176 pid_t waiting = waitpid(cmd->pid, &status, block ? 0 : WNOHANG);
178 if (!block && waiting == 0)
181 if (waiting < 0 && errno == EINTR)
186 char sbuf[STRERR_BUFSIZE];
188 fprintf(stderr, " Error: waitpid failed (%s)",
189 str_error_r(errno, sbuf, sizeof(sbuf)));
190 result = -ERR_RUN_COMMAND_WAITPID;
191 } else if (waiting != cmd->pid) {
192 result = -ERR_RUN_COMMAND_WAITPID_WRONG_PID;
193 } else if (WIFSIGNALED(status)) {
194 result = -ERR_RUN_COMMAND_WAITPID_SIGNAL;
195 } else if (!WIFEXITED(status)) {
196 result = -ERR_RUN_COMMAND_WAITPID_NOEXIT;
198 code = WEXITSTATUS(status);
201 result = -ERR_RUN_COMMAND_EXEC;
214 cmd->finish_result = result;
219 int check_if_command_finished(struct child_process *cmd)
222 char filename[FILENAME_MAX + 12];
223 char status_line[256];
227 * Check by reading /proc/<pid>/status as calling waitpid causes
228 * stdout/stderr to be closed and data lost.
230 sprintf(filename, "/proc/%d/status", cmd->pid);
231 status_file = fopen(filename, "r");
232 if (status_file == NULL) {
233 /* Open failed assume finish_command was called. */
236 while (fgets(status_line, sizeof(status_line), status_file) != NULL) {
239 if (strncmp(status_line, "State:", 6))
246 return *p == 'Z' ? 1 : 0;
248 /* Read failed assume finish_command was called. */
252 wait_or_whine(cmd, /*block=*/false);
253 return cmd->finished;
257 int finish_command(struct child_process *cmd)
259 return wait_or_whine(cmd, /*block=*/true);
262 int run_command(struct child_process *cmd)
264 int code = start_command(cmd);
267 return finish_command(cmd);
270 static void prepare_run_command_v_opt(struct child_process *cmd,
274 memset(cmd, 0, sizeof(*cmd));
276 cmd->no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
277 cmd->exec_cmd = opt & RUN_EXEC_CMD ? 1 : 0;
278 cmd->stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
281 int run_command_v_opt(const char **argv, int opt)
283 struct child_process cmd;
284 prepare_run_command_v_opt(&cmd, argv, opt);
285 return run_command(&cmd);