xref: /linux/tools/lib/subcmd/run-command.c (revision b6ebbac51bedf9e98e837688bc838f400196da5e)
1 #include <unistd.h>
2 #include <sys/types.h>
3 #include <sys/stat.h>
4 #include <fcntl.h>
5 #include <string.h>
6 #include <linux/string.h>
7 #include <errno.h>
8 #include <sys/wait.h>
9 #include "subcmd-util.h"
10 #include "run-command.h"
11 #include "exec-cmd.h"
12 
13 #define STRERR_BUFSIZE 128
14 
15 static inline void close_pair(int fd[2])
16 {
17 	close(fd[0]);
18 	close(fd[1]);
19 }
20 
21 static inline void dup_devnull(int to)
22 {
23 	int fd = open("/dev/null", O_RDWR);
24 	dup2(fd, to);
25 	close(fd);
26 }
27 
28 int start_command(struct child_process *cmd)
29 {
30 	int need_in, need_out, need_err;
31 	int fdin[2], fdout[2], fderr[2];
32 	char sbuf[STRERR_BUFSIZE];
33 
34 	/*
35 	 * In case of errors we must keep the promise to close FDs
36 	 * that have been passed in via ->in and ->out.
37 	 */
38 
39 	need_in = !cmd->no_stdin && cmd->in < 0;
40 	if (need_in) {
41 		if (pipe(fdin) < 0) {
42 			if (cmd->out > 0)
43 				close(cmd->out);
44 			return -ERR_RUN_COMMAND_PIPE;
45 		}
46 		cmd->in = fdin[1];
47 	}
48 
49 	need_out = !cmd->no_stdout
50 		&& !cmd->stdout_to_stderr
51 		&& cmd->out < 0;
52 	if (need_out) {
53 		if (pipe(fdout) < 0) {
54 			if (need_in)
55 				close_pair(fdin);
56 			else if (cmd->in)
57 				close(cmd->in);
58 			return -ERR_RUN_COMMAND_PIPE;
59 		}
60 		cmd->out = fdout[0];
61 	}
62 
63 	need_err = !cmd->no_stderr && cmd->err < 0;
64 	if (need_err) {
65 		if (pipe(fderr) < 0) {
66 			if (need_in)
67 				close_pair(fdin);
68 			else if (cmd->in)
69 				close(cmd->in);
70 			if (need_out)
71 				close_pair(fdout);
72 			else if (cmd->out)
73 				close(cmd->out);
74 			return -ERR_RUN_COMMAND_PIPE;
75 		}
76 		cmd->err = fderr[0];
77 	}
78 
79 	fflush(NULL);
80 	cmd->pid = fork();
81 	if (!cmd->pid) {
82 		if (cmd->no_stdin)
83 			dup_devnull(0);
84 		else if (need_in) {
85 			dup2(fdin[0], 0);
86 			close_pair(fdin);
87 		} else if (cmd->in) {
88 			dup2(cmd->in, 0);
89 			close(cmd->in);
90 		}
91 
92 		if (cmd->no_stderr)
93 			dup_devnull(2);
94 		else if (need_err) {
95 			dup2(fderr[1], 2);
96 			close_pair(fderr);
97 		}
98 
99 		if (cmd->no_stdout)
100 			dup_devnull(1);
101 		else if (cmd->stdout_to_stderr)
102 			dup2(2, 1);
103 		else if (need_out) {
104 			dup2(fdout[1], 1);
105 			close_pair(fdout);
106 		} else if (cmd->out > 1) {
107 			dup2(cmd->out, 1);
108 			close(cmd->out);
109 		}
110 
111 		if (cmd->dir && chdir(cmd->dir))
112 			die("exec %s: cd to %s failed (%s)", cmd->argv[0],
113 			    cmd->dir, str_error_r(errno, sbuf, sizeof(sbuf)));
114 		if (cmd->env) {
115 			for (; *cmd->env; cmd->env++) {
116 				if (strchr(*cmd->env, '='))
117 					putenv((char*)*cmd->env);
118 				else
119 					unsetenv(*cmd->env);
120 			}
121 		}
122 		if (cmd->preexec_cb)
123 			cmd->preexec_cb();
124 		if (cmd->exec_cmd) {
125 			execv_cmd(cmd->argv);
126 		} else {
127 			execvp(cmd->argv[0], (char *const*) cmd->argv);
128 		}
129 		exit(127);
130 	}
131 
132 	if (cmd->pid < 0) {
133 		int err = errno;
134 		if (need_in)
135 			close_pair(fdin);
136 		else if (cmd->in)
137 			close(cmd->in);
138 		if (need_out)
139 			close_pair(fdout);
140 		else if (cmd->out)
141 			close(cmd->out);
142 		if (need_err)
143 			close_pair(fderr);
144 		return err == ENOENT ?
145 			-ERR_RUN_COMMAND_EXEC :
146 			-ERR_RUN_COMMAND_FORK;
147 	}
148 
149 	if (need_in)
150 		close(fdin[0]);
151 	else if (cmd->in)
152 		close(cmd->in);
153 
154 	if (need_out)
155 		close(fdout[1]);
156 	else if (cmd->out)
157 		close(cmd->out);
158 
159 	if (need_err)
160 		close(fderr[1]);
161 
162 	return 0;
163 }
164 
165 static int wait_or_whine(pid_t pid)
166 {
167 	char sbuf[STRERR_BUFSIZE];
168 
169 	for (;;) {
170 		int status, code;
171 		pid_t waiting = waitpid(pid, &status, 0);
172 
173 		if (waiting < 0) {
174 			if (errno == EINTR)
175 				continue;
176 			fprintf(stderr, " Error: waitpid failed (%s)",
177 				str_error_r(errno, sbuf, sizeof(sbuf)));
178 			return -ERR_RUN_COMMAND_WAITPID;
179 		}
180 		if (waiting != pid)
181 			return -ERR_RUN_COMMAND_WAITPID_WRONG_PID;
182 		if (WIFSIGNALED(status))
183 			return -ERR_RUN_COMMAND_WAITPID_SIGNAL;
184 
185 		if (!WIFEXITED(status))
186 			return -ERR_RUN_COMMAND_WAITPID_NOEXIT;
187 		code = WEXITSTATUS(status);
188 		switch (code) {
189 		case 127:
190 			return -ERR_RUN_COMMAND_EXEC;
191 		case 0:
192 			return 0;
193 		default:
194 			return -code;
195 		}
196 	}
197 }
198 
199 int finish_command(struct child_process *cmd)
200 {
201 	return wait_or_whine(cmd->pid);
202 }
203 
204 int run_command(struct child_process *cmd)
205 {
206 	int code = start_command(cmd);
207 	if (code)
208 		return code;
209 	return finish_command(cmd);
210 }
211 
212 static void prepare_run_command_v_opt(struct child_process *cmd,
213 				      const char **argv,
214 				      int opt)
215 {
216 	memset(cmd, 0, sizeof(*cmd));
217 	cmd->argv = argv;
218 	cmd->no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
219 	cmd->exec_cmd = opt & RUN_EXEC_CMD ? 1 : 0;
220 	cmd->stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
221 }
222 
223 int run_command_v_opt(const char **argv, int opt)
224 {
225 	struct child_process cmd;
226 	prepare_run_command_v_opt(&cmd, argv, opt);
227 	return run_command(&cmd);
228 }
229