🌐 AI搜索 & 代理 主页
blob: ff3d8e2d8bf3208b7ad0f29d7cf76cc84a4ad0e6 [file] [log] [blame]
Josef Weidendorferb1bf95b2005-07-31 19:17:431#include "cache.h"
2#include "run-command.h"
Michal Ostrowski77cb17e2006-01-11 02:12:173#include "exec_cmd.h"
Josef Weidendorferb1bf95b2005-07-31 19:17:434
Shawn O. Pearce9dc09c72007-03-12 18:37:285static inline void close_pair(int fd[2])
6{
7 close(fd[0]);
8 close(fd[1]);
9}
10
Shawn O. Pearcee4507ae2007-03-12 18:37:5511static 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 08:28:0518int start_command(struct child_process *cmd)
Josef Weidendorferb1bf95b2005-07-31 19:17:4319{
Johannes Sixtf3b33f12007-10-19 19:47:5820 int need_in, need_out, need_err;
21 int fdin[2], fdout[2], fderr[2];
Shawn O. Pearce4919bf02007-03-10 08:28:0822
Johannes Sixtc20181e2008-02-21 22:42:5623 /*
24 * In case of errors we must keep the promise to close FDs
25 * that have been passed in via ->in and ->out.
26 */
27
Shawn O. Pearcee4507ae2007-03-12 18:37:5528 need_in = !cmd->no_stdin && cmd->in < 0;
Shawn O. Pearce4919bf02007-03-10 08:28:0829 if (need_in) {
Johannes Sixtc20181e2008-02-21 22:42:5630 if (pipe(fdin) < 0) {
31 if (cmd->out > 0)
32 close(cmd->out);
Shawn O. Pearce4919bf02007-03-10 08:28:0833 return -ERR_RUN_COMMAND_PIPE;
Johannes Sixtc20181e2008-02-21 22:42:5634 }
Shawn O. Pearce4919bf02007-03-10 08:28:0835 cmd->in = fdin[1];
Shawn O. Pearce4919bf02007-03-10 08:28:0836 }
37
Shawn O. Pearcee4507ae2007-03-12 18:37:5538 need_out = !cmd->no_stdout
39 && !cmd->stdout_to_stderr
40 && cmd->out < 0;
Shawn O. Pearcef4bba252007-03-12 18:37:4541 if (need_out) {
42 if (pipe(fdout) < 0) {
43 if (need_in)
44 close_pair(fdin);
Johannes Sixtc20181e2008-02-21 22:42:5645 else if (cmd->in)
46 close(cmd->in);
Shawn O. Pearcef4bba252007-03-12 18:37:4547 return -ERR_RUN_COMMAND_PIPE;
48 }
49 cmd->out = fdout[0];
Shawn O. Pearcef4bba252007-03-12 18:37:4550 }
51
Shawn O. Pearceb73a4392007-11-11 07:29:3752 need_err = !cmd->no_stderr && cmd->err < 0;
Johannes Sixtf3b33f12007-10-19 19:47:5853 if (need_err) {
54 if (pipe(fderr) < 0) {
55 if (need_in)
56 close_pair(fdin);
Johannes Sixtc20181e2008-02-21 22:42:5657 else if (cmd->in)
58 close(cmd->in);
Johannes Sixtf3b33f12007-10-19 19:47:5859 if (need_out)
60 close_pair(fdout);
Johannes Sixtc20181e2008-02-21 22:42:5661 else if (cmd->out)
62 close(cmd->out);
Johannes Sixtf3b33f12007-10-19 19:47:5863 return -ERR_RUN_COMMAND_PIPE;
64 }
65 cmd->err = fderr[0];
66 }
67
Johannes Schindelin8852f5d2008-07-07 13:41:3468 trace_argv_printf(cmd->argv, "trace: run_command:");
69
Johannes Sixtba26f292007-12-07 21:08:5970#ifndef __MINGW32__
Anders Melchiorsen7d0b18a2008-08-04 10:18:4071 fflush(NULL);
Shawn O. Pearceebcb5d12007-03-10 08:28:0572 cmd->pid = fork();
Shawn O. Pearceebcb5d12007-03-10 08:28:0573 if (!cmd->pid) {
Shawn O. Pearcee4507ae2007-03-12 18:37:5574 if (cmd->no_stdin)
75 dup_devnull(0);
76 else if (need_in) {
Shawn O. Pearce4919bf02007-03-10 08:28:0877 dup2(fdin[0], 0);
Shawn O. Pearce9dc09c72007-03-12 18:37:2878 close_pair(fdin);
Shawn O. Pearce4919bf02007-03-10 08:28:0879 } else if (cmd->in) {
80 dup2(cmd->in, 0);
81 close(cmd->in);
Shawn O. Pearce95d3c4f2006-12-31 02:55:2282 }
Shawn O. Pearce4919bf02007-03-10 08:28:0883
Christian Couderce2cf272008-03-05 07:35:1684 if (cmd->no_stderr)
85 dup_devnull(2);
86 else if (need_err) {
87 dup2(fderr[1], 2);
88 close_pair(fderr);
89 }
90
Shawn O. Pearcee4507ae2007-03-12 18:37:5591 if (cmd->no_stdout)
92 dup_devnull(1);
93 else if (cmd->stdout_to_stderr)
Shawn O. Pearcecd83c742006-12-31 02:55:1994 dup2(2, 1);
Shawn O. Pearcef4bba252007-03-12 18:37:4595 else if (need_out) {
96 dup2(fdout[1], 1);
97 close_pair(fdout);
98 } else if (cmd->out > 1) {
99 dup2(cmd->out, 1);
100 close(cmd->out);
101 }
102
Alex Riesen1568fea2007-05-22 21:48:23103 if (cmd->dir && chdir(cmd->dir))
Thomas Rastd824cbb2009-06-27 15:58:46104 die_errno("exec '%s': cd to '%s' failed", cmd->argv[0],
105 cmd->dir);
Alex Riesenee493142007-05-22 21:48:47106 if (cmd->env) {
Alex Riesen3427b372007-05-23 20:21:39107 for (; *cmd->env; cmd->env++) {
108 if (strchr(*cmd->env, '='))
Felipe Contreras4b25d092009-05-01 09:06:36109 putenv((char *)*cmd->env);
Alex Riesen3427b372007-05-23 20:21:39110 else
111 unsetenv(*cmd->env);
112 }
Alex Riesenee493142007-05-22 21:48:47113 }
Jeff Kingccf08bc2008-07-22 07:12:46114 if (cmd->preexec_cb)
115 cmd->preexec_cb();
Shawn O. Pearcef1000892007-03-10 08:28:00116 if (cmd->git_cmd) {
117 execv_git_cmd(cmd->argv);
Michal Ostrowski77cb17e2006-01-11 02:12:17118 } else {
Shawn O. Pearcef1000892007-03-10 08:28:00119 execvp(cmd->argv[0], (char *const*) cmd->argv);
Michal Ostrowski77cb17e2006-01-11 02:12:17120 }
Jeff King45c09612009-01-28 07:35:33121 trace_printf("trace: exec '%s' failed: %s\n", cmd->argv[0],
122 strerror(errno));
123 exit(127);
Josef Weidendorferb1bf95b2005-07-31 19:17:43124 }
Johannes Sixtba26f292007-12-07 21:08:59125#else
126 int s0 = -1, s1 = -1, s2 = -1; /* backups of stdin, stdout, stderr */
Steffen Prohaska108ac312008-07-28 05:50:28127 const char **sargv = cmd->argv;
Johannes Sixtba26f292007-12-07 21:08:59128 char **env = environ;
Johannes Sixtba26f292007-12-07 21:08:59129
130 if (cmd->no_stdin) {
131 s0 = dup(0);
132 dup_devnull(0);
133 } else if (need_in) {
134 s0 = dup(0);
135 dup2(fdin[0], 0);
136 } else if (cmd->in) {
137 s0 = dup(0);
138 dup2(cmd->in, 0);
139 }
140
141 if (cmd->no_stderr) {
142 s2 = dup(2);
143 dup_devnull(2);
144 } else if (need_err) {
145 s2 = dup(2);
146 dup2(fderr[1], 2);
147 }
148
149 if (cmd->no_stdout) {
150 s1 = dup(1);
151 dup_devnull(1);
152 } else if (cmd->stdout_to_stderr) {
153 s1 = dup(1);
154 dup2(2, 1);
155 } else if (need_out) {
156 s1 = dup(1);
157 dup2(fdout[1], 1);
158 } else if (cmd->out > 1) {
159 s1 = dup(1);
160 dup2(cmd->out, 1);
161 }
162
163 if (cmd->dir)
164 die("chdir in start_command() not implemented");
165 if (cmd->env) {
166 env = copy_environ();
167 for (; *cmd->env; cmd->env++)
168 env = env_setenv(env, *cmd->env);
169 }
170
171 if (cmd->git_cmd) {
Steffen Prohaska108ac312008-07-28 05:50:28172 cmd->argv = prepare_git_cmd(cmd->argv);
Johannes Sixtba26f292007-12-07 21:08:59173 }
174
Johannes Sixt7e5d7762007-11-24 21:49:16175 cmd->pid = mingw_spawnvpe(cmd->argv[0], cmd->argv, env);
Johannes Sixtba26f292007-12-07 21:08:59176
177 if (cmd->env)
178 free_environ(env);
179 if (cmd->git_cmd)
Steffen Prohaska108ac312008-07-28 05:50:28180 free(cmd->argv);
Johannes Sixtba26f292007-12-07 21:08:59181
Steffen Prohaska108ac312008-07-28 05:50:28182 cmd->argv = sargv;
Johannes Sixtba26f292007-12-07 21:08:59183 if (s0 >= 0)
184 dup2(s0, 0), close(s0);
185 if (s1 >= 0)
186 dup2(s1, 1), close(s1);
187 if (s2 >= 0)
188 dup2(s2, 2), close(s2);
189#endif
190
191 if (cmd->pid < 0) {
Jeff King45c09612009-01-28 07:35:33192 int err = errno;
Johannes Sixtba26f292007-12-07 21:08:59193 if (need_in)
194 close_pair(fdin);
195 else if (cmd->in)
196 close(cmd->in);
197 if (need_out)
198 close_pair(fdout);
199 else if (cmd->out)
200 close(cmd->out);
201 if (need_err)
202 close_pair(fderr);
Jeff King45c09612009-01-28 07:35:33203 return err == ENOENT ?
204 -ERR_RUN_COMMAND_EXEC :
205 -ERR_RUN_COMMAND_FORK;
Johannes Sixtba26f292007-12-07 21:08:59206 }
Shawn O. Pearce4919bf02007-03-10 08:28:08207
208 if (need_in)
209 close(fdin[0]);
210 else if (cmd->in)
211 close(cmd->in);
212
Shawn O. Pearcef4bba252007-03-12 18:37:45213 if (need_out)
214 close(fdout[1]);
Johannes Sixtc20181e2008-02-21 22:42:56215 else if (cmd->out)
Shawn O. Pearcef4bba252007-03-12 18:37:45216 close(cmd->out);
217
Johannes Sixtf3b33f12007-10-19 19:47:58218 if (need_err)
219 close(fderr[1]);
220
Shawn O. Pearceebcb5d12007-03-10 08:28:05221 return 0;
222}
223
Johannes Sixt2d22c202007-10-19 19:48:00224static int wait_or_whine(pid_t pid)
Shawn O. Pearceebcb5d12007-03-10 08:28:05225{
Josef Weidendorferb1bf95b2005-07-31 19:17:43226 for (;;) {
227 int status, code;
Johannes Sixt2d22c202007-10-19 19:48:00228 pid_t waiting = waitpid(pid, &status, 0);
Josef Weidendorferb1bf95b2005-07-31 19:17:43229
David Rientjes6f002f92006-08-15 17:40:06230 if (waiting < 0) {
Josef Weidendorferb1bf95b2005-07-31 19:17:43231 if (errno == EINTR)
232 continue;
David Rientjes6f002f92006-08-15 17:40:06233 error("waitpid failed (%s)", strerror(errno));
Josef Weidendorferb1bf95b2005-07-31 19:17:43234 return -ERR_RUN_COMMAND_WAITPID;
235 }
Johannes Sixt2d22c202007-10-19 19:48:00236 if (waiting != pid)
Josef Weidendorferb1bf95b2005-07-31 19:17:43237 return -ERR_RUN_COMMAND_WAITPID_WRONG_PID;
238 if (WIFSIGNALED(status))
239 return -ERR_RUN_COMMAND_WAITPID_SIGNAL;
240
241 if (!WIFEXITED(status))
242 return -ERR_RUN_COMMAND_WAITPID_NOEXIT;
243 code = WEXITSTATUS(status);
Jeff King45c09612009-01-28 07:35:33244 switch (code) {
245 case 127:
246 return -ERR_RUN_COMMAND_EXEC;
247 case 0:
248 return 0;
249 default:
Josef Weidendorferb1bf95b2005-07-31 19:17:43250 return -code;
Jeff King45c09612009-01-28 07:35:33251 }
Josef Weidendorferb1bf95b2005-07-31 19:17:43252 }
253}
Shawn O. Pearcef1000892007-03-10 08:28:00254
Johannes Sixt2d22c202007-10-19 19:48:00255int finish_command(struct child_process *cmd)
256{
Johannes Sixt2d22c202007-10-19 19:48:00257 return wait_or_whine(cmd->pid);
258}
259
Shawn O. Pearceebcb5d12007-03-10 08:28:05260int run_command(struct child_process *cmd)
261{
262 int code = start_command(cmd);
263 if (code)
264 return code;
265 return finish_command(cmd);
266}
267
Alex Riesen1568fea2007-05-22 21:48:23268static void prepare_run_command_v_opt(struct child_process *cmd,
Alex Riesenee493142007-05-22 21:48:47269 const char **argv,
270 int opt)
Alex Riesen1568fea2007-05-22 21:48:23271{
272 memset(cmd, 0, sizeof(*cmd));
273 cmd->argv = argv;
274 cmd->no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
275 cmd->git_cmd = opt & RUN_GIT_CMD ? 1 : 0;
276 cmd->stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
277}
278
Shawn O. Pearcef1000892007-03-10 08:28:00279int run_command_v_opt(const char **argv, int opt)
280{
281 struct child_process cmd;
Alex Riesen1568fea2007-05-22 21:48:23282 prepare_run_command_v_opt(&cmd, argv, opt);
283 return run_command(&cmd);
284}
285
Alex Riesenee493142007-05-22 21:48:47286int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env)
287{
288 struct child_process cmd;
289 prepare_run_command_v_opt(&cmd, argv, opt);
290 cmd.dir = dir;
291 cmd.env = env;
292 return run_command(&cmd);
293}
Johannes Sixt2d22c202007-10-19 19:48:00294
Johannes Sixt618ebe92007-12-08 21:19:14295#ifdef __MINGW32__
296static __stdcall unsigned run_thread(void *data)
297{
298 struct async *async = data;
299 return async->proc(async->fd_for_proc, async->data);
300}
301#endif
302
Johannes Sixt2d22c202007-10-19 19:48:00303int start_async(struct async *async)
304{
305 int pipe_out[2];
306
307 if (pipe(pipe_out) < 0)
308 return error("cannot create pipe: %s", strerror(errno));
Johannes Sixt618ebe92007-12-08 21:19:14309 async->out = pipe_out[0];
Johannes Sixt2d22c202007-10-19 19:48:00310
Johannes Sixt618ebe92007-12-08 21:19:14311#ifndef __MINGW32__
Anders Melchiorsen2c3766f2008-08-04 00:30:03312 /* Flush stdio before fork() to avoid cloning buffers */
313 fflush(NULL);
314
Johannes Sixt2d22c202007-10-19 19:48:00315 async->pid = fork();
316 if (async->pid < 0) {
317 error("fork (async) failed: %s", strerror(errno));
318 close_pair(pipe_out);
319 return -1;
320 }
321 if (!async->pid) {
322 close(pipe_out[0]);
323 exit(!!async->proc(pipe_out[1], async->data));
324 }
Johannes Sixt2d22c202007-10-19 19:48:00325 close(pipe_out[1]);
Johannes Sixt618ebe92007-12-08 21:19:14326#else
327 async->fd_for_proc = pipe_out[1];
328 async->tid = (HANDLE) _beginthreadex(NULL, 0, run_thread, async, 0, NULL);
329 if (!async->tid) {
330 error("cannot create thread: %s", strerror(errno));
331 close_pair(pipe_out);
332 return -1;
333 }
334#endif
Johannes Sixt2d22c202007-10-19 19:48:00335 return 0;
336}
337
338int finish_async(struct async *async)
339{
Johannes Sixt618ebe92007-12-08 21:19:14340#ifndef __MINGW32__
Johannes Sixt2d22c202007-10-19 19:48:00341 int ret = 0;
342
343 if (wait_or_whine(async->pid))
344 ret = error("waitpid (async) failed");
Johannes Sixt618ebe92007-12-08 21:19:14345#else
346 DWORD ret = 0;
347 if (WaitForSingleObject(async->tid, INFINITE) != WAIT_OBJECT_0)
348 ret = error("waiting for thread failed: %lu", GetLastError());
349 else if (!GetExitCodeThread(async->tid, &ret))
350 ret = error("cannot get thread exit code: %lu", GetLastError());
351 CloseHandle(async->tid);
352#endif
Johannes Sixt2d22c202007-10-19 19:48:00353 return ret;
354}
Stephan Beyerae98a002009-01-16 19:09:59355
356int run_hook(const char *index_file, const char *name, ...)
357{
358 struct child_process hook;
Stephan Beyer14e62982009-01-17 03:02:55359 const char **argv = NULL, *env[2];
Stephan Beyerae98a002009-01-16 19:09:59360 char index[PATH_MAX];
361 va_list args;
362 int ret;
Stephan Beyer14e62982009-01-17 03:02:55363 size_t i = 0, alloc = 0;
Stephan Beyerae98a002009-01-16 19:09:59364
Stephan Beyercf94ca82009-01-16 19:10:01365 if (access(git_path("hooks/%s", name), X_OK) < 0)
366 return 0;
367
Stephan Beyerae98a002009-01-16 19:09:59368 va_start(args, name);
Stephan Beyer14e62982009-01-17 03:02:55369 ALLOC_GROW(argv, i + 1, alloc);
370 argv[i++] = git_path("hooks/%s", name);
371 while (argv[i-1]) {
372 ALLOC_GROW(argv, i + 1, alloc);
373 argv[i++] = va_arg(args, const char *);
374 }
Stephan Beyerae98a002009-01-16 19:09:59375 va_end(args);
376
Stephan Beyerae98a002009-01-16 19:09:59377 memset(&hook, 0, sizeof(hook));
378 hook.argv = argv;
379 hook.no_stdin = 1;
380 hook.stdout_to_stderr = 1;
381 if (index_file) {
382 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
383 env[0] = index;
384 env[1] = NULL;
385 hook.env = env;
386 }
387
388 ret = start_command(&hook);
Stephan Beyer14e62982009-01-17 03:02:55389 free(argv);
Stephan Beyerae98a002009-01-16 19:09:59390 if (ret) {
391 warning("Could not spawn %s", argv[0]);
392 return ret;
393 }
394 ret = finish_command(&hook);
395 if (ret == -ERR_RUN_COMMAND_WAITPID_SIGNAL)
396 warning("%s exited due to uncaught signal", argv[0]);
397
398 return ret;
399}