blob: 476d00c2182e3af82a0cfe495c61c9df1eb44d26 [file] [log] [blame]
Josef Weidendorferb1bf95b2005-07-31 21:17:43 +02001#include "cache.h"
2#include "run-command.h"
Michal Ostrowski77cb17e2006-01-10 21:12:17 -05003#include "exec_cmd.h"
Josef Weidendorferb1bf95b2005-07-31 21:17:43 +02004
Shawn O. Pearce9dc09c72007-03-12 14:37:28 -04005static inline void close_pair(int fd[2])
6{
7 close(fd[0]);
8 close(fd[1]);
9}
10
Shawn O. Pearcee4507ae2007-03-12 14:37:55 -040011static inline void dup_devnull(int to)
12{
13 int fd = open("/dev/null", O_RDWR);
14 dup2(fd, to);
15 close(fd);
16}
17
Shawn O. Pearceebcb5d12007-03-10 03:28:05 -050018int start_command(struct child_process *cmd)
Josef Weidendorferb1bf95b2005-07-31 21:17:43 +020019{
Johannes Sixtf3b33f12007-10-19 21:47:58 +020020 int need_in, need_out, need_err;
21 int fdin[2], fdout[2], fderr[2];
Shawn O. Pearce4919bf02007-03-10 03:28:08 -050022
Shawn O. Pearcee4507ae2007-03-12 14:37:55 -040023 need_in = !cmd->no_stdin && cmd->in < 0;
Shawn O. Pearce4919bf02007-03-10 03:28:08 -050024 if (need_in) {
25 if (pipe(fdin) < 0)
26 return -ERR_RUN_COMMAND_PIPE;
27 cmd->in = fdin[1];
28 cmd->close_in = 1;
29 }
30
Shawn O. Pearcee4507ae2007-03-12 14:37:55 -040031 need_out = !cmd->no_stdout
32 && !cmd->stdout_to_stderr
33 && cmd->out < 0;
Shawn O. Pearcef4bba252007-03-12 14:37:45 -040034 if (need_out) {
35 if (pipe(fdout) < 0) {
36 if (need_in)
37 close_pair(fdin);
38 return -ERR_RUN_COMMAND_PIPE;
39 }
40 cmd->out = fdout[0];
41 cmd->close_out = 1;
42 }
43
Shawn O. Pearceb73a4392007-11-11 02:29:37 -050044 need_err = !cmd->no_stderr && cmd->err < 0;
Johannes Sixtf3b33f12007-10-19 21:47:58 +020045 if (need_err) {
46 if (pipe(fderr) < 0) {
47 if (need_in)
48 close_pair(fdin);
49 if (need_out)
50 close_pair(fdout);
51 return -ERR_RUN_COMMAND_PIPE;
52 }
53 cmd->err = fderr[0];
54 }
55
Shawn O. Pearceebcb5d12007-03-10 03:28:05 -050056 cmd->pid = fork();
Shawn O. Pearce4919bf02007-03-10 03:28:08 -050057 if (cmd->pid < 0) {
Shawn O. Pearce9dc09c72007-03-12 14:37:28 -040058 if (need_in)
59 close_pair(fdin);
Shawn O. Pearcef4bba252007-03-12 14:37:45 -040060 if (need_out)
61 close_pair(fdout);
Johannes Sixtf3b33f12007-10-19 21:47:58 +020062 if (need_err)
63 close_pair(fderr);
Josef Weidendorferb1bf95b2005-07-31 21:17:43 +020064 return -ERR_RUN_COMMAND_FORK;
Shawn O. Pearce4919bf02007-03-10 03:28:08 -050065 }
66
Shawn O. Pearceebcb5d12007-03-10 03:28:05 -050067 if (!cmd->pid) {
Shawn O. Pearcee4507ae2007-03-12 14:37:55 -040068 if (cmd->no_stdin)
69 dup_devnull(0);
70 else if (need_in) {
Shawn O. Pearce4919bf02007-03-10 03:28:08 -050071 dup2(fdin[0], 0);
Shawn O. Pearce9dc09c72007-03-12 14:37:28 -040072 close_pair(fdin);
Shawn O. Pearce4919bf02007-03-10 03:28:08 -050073 } else if (cmd->in) {
74 dup2(cmd->in, 0);
75 close(cmd->in);
Shawn O. Pearce95d3c4f2006-12-30 21:55:22 -050076 }
Shawn O. Pearce4919bf02007-03-10 03:28:08 -050077
Shawn O. Pearcee4507ae2007-03-12 14:37:55 -040078 if (cmd->no_stdout)
79 dup_devnull(1);
80 else if (cmd->stdout_to_stderr)
Shawn O. Pearcecd83c742006-12-30 21:55:19 -050081 dup2(2, 1);
Shawn O. Pearcef4bba252007-03-12 14:37:45 -040082 else if (need_out) {
83 dup2(fdout[1], 1);
84 close_pair(fdout);
85 } else if (cmd->out > 1) {
86 dup2(cmd->out, 1);
87 close(cmd->out);
88 }
89
Shawn O. Pearceb73a4392007-11-11 02:29:37 -050090 if (cmd->no_stderr)
91 dup_devnull(2);
92 else if (need_err) {
Johannes Sixtf3b33f12007-10-19 21:47:58 +020093 dup2(fderr[1], 2);
94 close_pair(fderr);
95 }
96
Alex Riesen1568fea2007-05-22 23:48:23 +020097 if (cmd->dir && chdir(cmd->dir))
98 die("exec %s: cd to %s failed (%s)", cmd->argv[0],
99 cmd->dir, strerror(errno));
Alex Riesenee493142007-05-22 23:48:47 +0200100 if (cmd->env) {
Alex Riesen3427b372007-05-23 22:21:39 +0200101 for (; *cmd->env; cmd->env++) {
102 if (strchr(*cmd->env, '='))
103 putenv((char*)*cmd->env);
104 else
105 unsetenv(*cmd->env);
106 }
Alex Riesenee493142007-05-22 23:48:47 +0200107 }
Shawn O. Pearcef1000892007-03-10 03:28:00 -0500108 if (cmd->git_cmd) {
109 execv_git_cmd(cmd->argv);
Michal Ostrowski77cb17e2006-01-10 21:12:17 -0500110 } else {
Shawn O. Pearcef1000892007-03-10 03:28:00 -0500111 execvp(cmd->argv[0], (char *const*) cmd->argv);
Michal Ostrowski77cb17e2006-01-10 21:12:17 -0500112 }
Shawn O. Pearcef1000892007-03-10 03:28:00 -0500113 die("exec %s failed.", cmd->argv[0]);
Josef Weidendorferb1bf95b2005-07-31 21:17:43 +0200114 }
Shawn O. Pearce4919bf02007-03-10 03:28:08 -0500115
116 if (need_in)
117 close(fdin[0]);
118 else if (cmd->in)
119 close(cmd->in);
120
Shawn O. Pearcef4bba252007-03-12 14:37:45 -0400121 if (need_out)
122 close(fdout[1]);
123 else if (cmd->out > 1)
124 close(cmd->out);
125
Johannes Sixtf3b33f12007-10-19 21:47:58 +0200126 if (need_err)
127 close(fderr[1]);
128
Shawn O. Pearceebcb5d12007-03-10 03:28:05 -0500129 return 0;
130}
131
Johannes Sixt2d22c202007-10-19 21:48:00 +0200132static int wait_or_whine(pid_t pid)
Shawn O. Pearceebcb5d12007-03-10 03:28:05 -0500133{
Josef Weidendorferb1bf95b2005-07-31 21:17:43 +0200134 for (;;) {
135 int status, code;
Johannes Sixt2d22c202007-10-19 21:48:00 +0200136 pid_t waiting = waitpid(pid, &status, 0);
Josef Weidendorferb1bf95b2005-07-31 21:17:43 +0200137
David Rientjes6f002f92006-08-15 10:40:06 -0700138 if (waiting < 0) {
Josef Weidendorferb1bf95b2005-07-31 21:17:43 +0200139 if (errno == EINTR)
140 continue;
David Rientjes6f002f92006-08-15 10:40:06 -0700141 error("waitpid failed (%s)", strerror(errno));
Josef Weidendorferb1bf95b2005-07-31 21:17:43 +0200142 return -ERR_RUN_COMMAND_WAITPID;
143 }
Johannes Sixt2d22c202007-10-19 21:48:00 +0200144 if (waiting != pid)
Josef Weidendorferb1bf95b2005-07-31 21:17:43 +0200145 return -ERR_RUN_COMMAND_WAITPID_WRONG_PID;
146 if (WIFSIGNALED(status))
147 return -ERR_RUN_COMMAND_WAITPID_SIGNAL;
148
149 if (!WIFEXITED(status))
150 return -ERR_RUN_COMMAND_WAITPID_NOEXIT;
151 code = WEXITSTATUS(status);
152 if (code)
153 return -code;
154 return 0;
155 }
156}
Shawn O. Pearcef1000892007-03-10 03:28:00 -0500157
Johannes Sixt2d22c202007-10-19 21:48:00 +0200158int finish_command(struct child_process *cmd)
159{
160 if (cmd->close_in)
161 close(cmd->in);
162 if (cmd->close_out)
163 close(cmd->out);
164 return wait_or_whine(cmd->pid);
165}
166
Shawn O. Pearceebcb5d12007-03-10 03:28:05 -0500167int run_command(struct child_process *cmd)
168{
169 int code = start_command(cmd);
170 if (code)
171 return code;
172 return finish_command(cmd);
173}
174
Alex Riesen1568fea2007-05-22 23:48:23 +0200175static void prepare_run_command_v_opt(struct child_process *cmd,
Alex Riesenee493142007-05-22 23:48:47 +0200176 const char **argv,
177 int opt)
Alex Riesen1568fea2007-05-22 23:48:23 +0200178{
179 memset(cmd, 0, sizeof(*cmd));
180 cmd->argv = argv;
181 cmd->no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
182 cmd->git_cmd = opt & RUN_GIT_CMD ? 1 : 0;
183 cmd->stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
184}
185
Shawn O. Pearcef1000892007-03-10 03:28:00 -0500186int run_command_v_opt(const char **argv, int opt)
187{
188 struct child_process cmd;
Alex Riesen1568fea2007-05-22 23:48:23 +0200189 prepare_run_command_v_opt(&cmd, argv, opt);
190 return run_command(&cmd);
191}
192
193int run_command_v_opt_cd(const char **argv, int opt, const char *dir)
194{
195 struct child_process cmd;
196 prepare_run_command_v_opt(&cmd, argv, opt);
197 cmd.dir = dir;
Shawn O. Pearcef1000892007-03-10 03:28:00 -0500198 return run_command(&cmd);
199}
Alex Riesenee493142007-05-22 23:48:47 +0200200
201int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env)
202{
203 struct child_process cmd;
204 prepare_run_command_v_opt(&cmd, argv, opt);
205 cmd.dir = dir;
206 cmd.env = env;
207 return run_command(&cmd);
208}
Johannes Sixt2d22c202007-10-19 21:48:00 +0200209
210int start_async(struct async *async)
211{
212 int pipe_out[2];
213
214 if (pipe(pipe_out) < 0)
215 return error("cannot create pipe: %s", strerror(errno));
216
217 async->pid = fork();
218 if (async->pid < 0) {
219 error("fork (async) failed: %s", strerror(errno));
220 close_pair(pipe_out);
221 return -1;
222 }
223 if (!async->pid) {
224 close(pipe_out[0]);
225 exit(!!async->proc(pipe_out[1], async->data));
226 }
227 async->out = pipe_out[0];
228 close(pipe_out[1]);
229 return 0;
230}
231
232int finish_async(struct async *async)
233{
234 int ret = 0;
235
236 if (wait_or_whine(async->pid))
237 ret = error("waitpid (async) failed");
238 return ret;
239}