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