🌐 AI搜索 & 代理 主页
blob: f91e446c86be8e27f98554567143d7ce6f934bd1 [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
Johannes Sixt75301f92010-01-15 20:12:1811#ifndef WIN32
Shawn O. Pearcee4507ae2007-03-12 18:37:5512static inline void dup_devnull(int to)
13{
14 int fd = open("/dev/null", O_RDWR);
15 dup2(fd, to);
16 close(fd);
17}
Johannes Sixt75301f92010-01-15 20:12:1818#endif
Shawn O. Pearcee4507ae2007-03-12 18:37:5519
Jeff King8dba1e62009-12-30 10:53:1620static const char **prepare_shell_cmd(const char **argv)
21{
22 int argc, nargc = 0;
23 const char **nargv;
24
25 for (argc = 0; argv[argc]; argc++)
26 ; /* just counting */
27 /* +1 for NULL, +3 for "sh -c" plus extra $0 */
28 nargv = xmalloc(sizeof(*nargv) * (argc + 1 + 3));
29
30 if (argc < 1)
31 die("BUG: shell command is empty");
32
Jeff Kingf4456442009-12-30 10:55:3633 if (strcspn(argv[0], "|&;<>()$`\\\"' \t\n*?[#~=%") != strlen(argv[0])) {
34 nargv[nargc++] = "sh";
35 nargv[nargc++] = "-c";
Jeff King8dba1e62009-12-30 10:53:1636
Jeff Kingf4456442009-12-30 10:55:3637 if (argc < 2)
38 nargv[nargc++] = argv[0];
39 else {
40 struct strbuf arg0 = STRBUF_INIT;
41 strbuf_addf(&arg0, "%s \"$@\"", argv[0]);
42 nargv[nargc++] = strbuf_detach(&arg0, NULL);
43 }
Jeff King8dba1e62009-12-30 10:53:1644 }
45
46 for (argc = 0; argv[argc]; argc++)
47 nargv[nargc++] = argv[argc];
48 nargv[nargc] = NULL;
49
50 return nargv;
51}
52
53#ifndef WIN32
54static int execv_shell_cmd(const char **argv)
55{
56 const char **nargv = prepare_shell_cmd(argv);
57 trace_argv_printf(nargv, "trace: exec:");
58 execvp(nargv[0], (char **)nargv);
59 free(nargv);
60 return -1;
61}
62#endif
63
Johannes Sixta5487dd2010-01-10 13:07:5264#ifndef WIN32
65static int child_err = 2;
Johannes Sixt2b541bf2010-01-10 13:11:2266static int child_notifier = -1;
67
68static void notify_parent(void)
69{
Junio C Hamano60e199c2011-04-18 21:14:5370 ssize_t unused;
71 unused = write(child_notifier, "", 1);
Johannes Sixt2b541bf2010-01-10 13:11:2272}
Johannes Sixta5487dd2010-01-10 13:07:5273
74static NORETURN void die_child(const char *err, va_list params)
75{
76 char msg[4096];
Junio C Hamano60e199c2011-04-18 21:14:5377 ssize_t unused;
Johannes Sixta5487dd2010-01-10 13:07:5278 int len = vsnprintf(msg, sizeof(msg), err, params);
79 if (len > sizeof(msg))
80 len = sizeof(msg);
81
Junio C Hamano60e199c2011-04-18 21:14:5382 unused = write(child_err, "fatal: ", 7);
83 unused = write(child_err, msg, len);
84 unused = write(child_err, "\n", 1);
Johannes Sixta5487dd2010-01-10 13:07:5285 exit(128);
86}
Johannes Sixt200a76b2010-03-06 15:40:4287#endif
Johannes Sixta5487dd2010-01-10 13:07:5288
89static inline void set_cloexec(int fd)
90{
91 int flags = fcntl(fd, F_GETFD);
92 if (flags >= 0)
93 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
94}
Johannes Sixta5487dd2010-01-10 13:07:5295
Johannes Sixtab0b41d2010-01-10 13:08:4596static int wait_or_whine(pid_t pid, const char *argv0, int silent_exec_failure)
97{
98 int status, code = -1;
99 pid_t waiting;
100 int failed_errno = 0;
101
102 while ((waiting = waitpid(pid, &status, 0)) < 0 && errno == EINTR)
103 ; /* nothing */
104
105 if (waiting < 0) {
106 failed_errno = errno;
107 error("waitpid for %s failed: %s", argv0, strerror(errno));
108 } else if (waiting != pid) {
109 error("waitpid is confused (%s)", argv0);
110 } else if (WIFSIGNALED(status)) {
111 code = WTERMSIG(status);
112 error("%s died of signal %d", argv0, code);
113 /*
114 * This return value is chosen so that code & 0xff
115 * mimics the exit code that a POSIX shell would report for
116 * a program that died from this signal.
117 */
118 code -= 128;
119 } else if (WIFEXITED(status)) {
120 code = WEXITSTATUS(status);
121 /*
122 * Convert special exit code when execvp failed.
123 */
124 if (code == 127) {
125 code = -1;
126 failed_errno = ENOENT;
127 if (!silent_exec_failure)
128 error("cannot run %s: %s", argv0,
129 strerror(ENOENT));
130 }
131 } else {
132 error("waitpid is confused (%s)", argv0);
133 }
134 errno = failed_errno;
135 return code;
136}
137
Shawn O. Pearceebcb5d12007-03-10 08:28:05138int start_command(struct child_process *cmd)
Josef Weidendorferb1bf95b2005-07-31 19:17:43139{
Johannes Sixtf3b33f12007-10-19 19:47:58140 int need_in, need_out, need_err;
141 int fdin[2], fdout[2], fderr[2];
David Soria Parra5a7a3672009-08-04 09:28:40142 int failed_errno = failed_errno;
Shawn O. Pearce4919bf02007-03-10 08:28:08143
Johannes Sixtc20181e2008-02-21 22:42:56144 /*
145 * In case of errors we must keep the promise to close FDs
146 * that have been passed in via ->in and ->out.
147 */
148
Shawn O. Pearcee4507ae2007-03-12 18:37:55149 need_in = !cmd->no_stdin && cmd->in < 0;
Shawn O. Pearce4919bf02007-03-10 08:28:08150 if (need_in) {
Johannes Sixtc20181e2008-02-21 22:42:56151 if (pipe(fdin) < 0) {
Johannes Sixt0ac77ec2009-07-04 19:26:40152 failed_errno = errno;
Johannes Sixtc20181e2008-02-21 22:42:56153 if (cmd->out > 0)
154 close(cmd->out);
Johannes Sixt0ac77ec2009-07-04 19:26:40155 goto fail_pipe;
Johannes Sixtc20181e2008-02-21 22:42:56156 }
Shawn O. Pearce4919bf02007-03-10 08:28:08157 cmd->in = fdin[1];
Shawn O. Pearce4919bf02007-03-10 08:28:08158 }
159
Shawn O. Pearcee4507ae2007-03-12 18:37:55160 need_out = !cmd->no_stdout
161 && !cmd->stdout_to_stderr
162 && cmd->out < 0;
Shawn O. Pearcef4bba252007-03-12 18:37:45163 if (need_out) {
164 if (pipe(fdout) < 0) {
Johannes Sixt0ac77ec2009-07-04 19:26:40165 failed_errno = errno;
Shawn O. Pearcef4bba252007-03-12 18:37:45166 if (need_in)
167 close_pair(fdin);
Johannes Sixtc20181e2008-02-21 22:42:56168 else if (cmd->in)
169 close(cmd->in);
Johannes Sixt0ac77ec2009-07-04 19:26:40170 goto fail_pipe;
Shawn O. Pearcef4bba252007-03-12 18:37:45171 }
172 cmd->out = fdout[0];
Shawn O. Pearcef4bba252007-03-12 18:37:45173 }
174
Shawn O. Pearceb73a4392007-11-11 07:29:37175 need_err = !cmd->no_stderr && cmd->err < 0;
Johannes Sixtf3b33f12007-10-19 19:47:58176 if (need_err) {
177 if (pipe(fderr) < 0) {
Johannes Sixt0ac77ec2009-07-04 19:26:40178 failed_errno = errno;
Johannes Sixtf3b33f12007-10-19 19:47:58179 if (need_in)
180 close_pair(fdin);
Johannes Sixtc20181e2008-02-21 22:42:56181 else if (cmd->in)
182 close(cmd->in);
Johannes Sixtf3b33f12007-10-19 19:47:58183 if (need_out)
184 close_pair(fdout);
Johannes Sixtc20181e2008-02-21 22:42:56185 else if (cmd->out)
186 close(cmd->out);
Johannes Sixt0ac77ec2009-07-04 19:26:40187fail_pipe:
188 error("cannot create pipe for %s: %s",
189 cmd->argv[0], strerror(failed_errno));
190 errno = failed_errno;
191 return -1;
Johannes Sixtf3b33f12007-10-19 19:47:58192 }
193 cmd->err = fderr[0];
194 }
195
Johannes Schindelin8852f5d2008-07-07 13:41:34196 trace_argv_printf(cmd->argv, "trace: run_command:");
Johannes Sixt13af8cb2011-02-04 08:41:58197 fflush(NULL);
Johannes Schindelin8852f5d2008-07-07 13:41:34198
Frank Li71064e32009-09-16 08:20:22199#ifndef WIN32
Johannes Sixt2b541bf2010-01-10 13:11:22200{
201 int notify_pipe[2];
202 if (pipe(notify_pipe))
203 notify_pipe[0] = notify_pipe[1] = -1;
204
Shawn O. Pearceebcb5d12007-03-10 08:28:05205 cmd->pid = fork();
Shawn O. Pearceebcb5d12007-03-10 08:28:05206 if (!cmd->pid) {
Johannes Sixta5487dd2010-01-10 13:07:52207 /*
208 * Redirect the channel to write syscall error messages to
209 * before redirecting the process's stderr so that all die()
210 * in subsequent call paths use the parent's stderr.
211 */
212 if (cmd->no_stderr || need_err) {
213 child_err = dup(2);
214 set_cloexec(child_err);
215 }
216 set_die_routine(die_child);
217
Johannes Sixt2b541bf2010-01-10 13:11:22218 close(notify_pipe[0]);
219 set_cloexec(notify_pipe[1]);
220 child_notifier = notify_pipe[1];
221 atexit(notify_parent);
222
Shawn O. Pearcee4507ae2007-03-12 18:37:55223 if (cmd->no_stdin)
224 dup_devnull(0);
225 else if (need_in) {
Shawn O. Pearce4919bf02007-03-10 08:28:08226 dup2(fdin[0], 0);
Shawn O. Pearce9dc09c72007-03-12 18:37:28227 close_pair(fdin);
Shawn O. Pearce4919bf02007-03-10 08:28:08228 } else if (cmd->in) {
229 dup2(cmd->in, 0);
230 close(cmd->in);
Shawn O. Pearce95d3c4f2006-12-31 02:55:22231 }
Shawn O. Pearce4919bf02007-03-10 08:28:08232
Christian Couderce2cf272008-03-05 07:35:16233 if (cmd->no_stderr)
234 dup_devnull(2);
235 else if (need_err) {
236 dup2(fderr[1], 2);
237 close_pair(fderr);
Shawn O. Pearce4f41b612010-02-05 20:57:37238 } else if (cmd->err > 1) {
239 dup2(cmd->err, 2);
240 close(cmd->err);
Christian Couderce2cf272008-03-05 07:35:16241 }
242
Shawn O. Pearcee4507ae2007-03-12 18:37:55243 if (cmd->no_stdout)
244 dup_devnull(1);
245 else if (cmd->stdout_to_stderr)
Shawn O. Pearcecd83c742006-12-31 02:55:19246 dup2(2, 1);
Shawn O. Pearcef4bba252007-03-12 18:37:45247 else if (need_out) {
248 dup2(fdout[1], 1);
249 close_pair(fdout);
250 } else if (cmd->out > 1) {
251 dup2(cmd->out, 1);
252 close(cmd->out);
253 }
254
Alex Riesen1568fea2007-05-22 21:48:23255 if (cmd->dir && chdir(cmd->dir))
Thomas Rastd824cbb2009-06-27 15:58:46256 die_errno("exec '%s': cd to '%s' failed", cmd->argv[0],
257 cmd->dir);
Alex Riesenee493142007-05-22 21:48:47258 if (cmd->env) {
Alex Riesen3427b372007-05-23 20:21:39259 for (; *cmd->env; cmd->env++) {
260 if (strchr(*cmd->env, '='))
Felipe Contreras4b25d092009-05-01 09:06:36261 putenv((char *)*cmd->env);
Alex Riesen3427b372007-05-23 20:21:39262 else
263 unsetenv(*cmd->env);
264 }
Alex Riesenee493142007-05-22 21:48:47265 }
Johannes Sixt2b541bf2010-01-10 13:11:22266 if (cmd->preexec_cb) {
267 /*
268 * We cannot predict what the pre-exec callback does.
269 * Forgo parent notification.
270 */
271 close(child_notifier);
272 child_notifier = -1;
273
Jeff Kingccf08bc2008-07-22 07:12:46274 cmd->preexec_cb();
Johannes Sixt2b541bf2010-01-10 13:11:22275 }
Shawn O. Pearcef1000892007-03-10 08:28:00276 if (cmd->git_cmd) {
277 execv_git_cmd(cmd->argv);
Jeff King8dba1e62009-12-30 10:53:16278 } else if (cmd->use_shell) {
279 execv_shell_cmd(cmd->argv);
Michal Ostrowski77cb17e2006-01-11 02:12:17280 } else {
Shawn O. Pearcef1000892007-03-10 08:28:00281 execvp(cmd->argv[0], (char *const*) cmd->argv);
Michal Ostrowski77cb17e2006-01-11 02:12:17282 }
Johannes Sixta5487dd2010-01-10 13:07:52283 /*
284 * Do not check for cmd->silent_exec_failure; the parent
285 * process will check it when it sees this exit code.
286 */
287 if (errno == ENOENT)
288 exit(127);
289 else
290 die_errno("cannot exec '%s'", cmd->argv[0]);
Josef Weidendorferb1bf95b2005-07-31 19:17:43291 }
Johannes Sixt0ac77ec2009-07-04 19:26:40292 if (cmd->pid < 0)
293 error("cannot fork() for %s: %s", cmd->argv[0],
294 strerror(failed_errno = errno));
Johannes Sixt2b541bf2010-01-10 13:11:22295
296 /*
297 * Wait for child's execvp. If the execvp succeeds (or if fork()
298 * failed), EOF is seen immediately by the parent. Otherwise, the
299 * child process sends a single byte.
300 * Note that use of this infrastructure is completely advisory,
301 * therefore, we keep error checks minimal.
302 */
303 close(notify_pipe[1]);
304 if (read(notify_pipe[0], &notify_pipe[1], 1) == 1) {
305 /*
306 * At this point we know that fork() succeeded, but execvp()
307 * failed. Errors have been reported to our stderr.
308 */
309 wait_or_whine(cmd->pid, cmd->argv[0],
310 cmd->silent_exec_failure);
311 failed_errno = errno;
312 cmd->pid = -1;
313 }
314 close(notify_pipe[0]);
315}
Johannes Sixtba26f292007-12-07 21:08:59316#else
Frank Li0d30ad72009-09-16 08:20:17317{
Johannes Sixt75301f92010-01-15 20:12:18318 int fhin = 0, fhout = 1, fherr = 2;
Steffen Prohaska108ac312008-07-28 05:50:28319 const char **sargv = cmd->argv;
Johannes Sixtba26f292007-12-07 21:08:59320 char **env = environ;
Johannes Sixtba26f292007-12-07 21:08:59321
Johannes Sixt75301f92010-01-15 20:12:18322 if (cmd->no_stdin)
323 fhin = open("/dev/null", O_RDWR);
324 else if (need_in)
325 fhin = dup(fdin[0]);
326 else if (cmd->in)
327 fhin = dup(cmd->in);
Johannes Sixtba26f292007-12-07 21:08:59328
Johannes Sixt75301f92010-01-15 20:12:18329 if (cmd->no_stderr)
330 fherr = open("/dev/null", O_RDWR);
331 else if (need_err)
332 fherr = dup(fderr[1]);
Junio C Hamano76d44c82010-02-06 05:08:53333 else if (cmd->err > 2)
334 fherr = dup(cmd->err);
Johannes Sixtba26f292007-12-07 21:08:59335
Johannes Sixt75301f92010-01-15 20:12:18336 if (cmd->no_stdout)
337 fhout = open("/dev/null", O_RDWR);
338 else if (cmd->stdout_to_stderr)
339 fhout = dup(fherr);
340 else if (need_out)
341 fhout = dup(fdout[1]);
342 else if (cmd->out > 1)
343 fhout = dup(cmd->out);
Johannes Sixtba26f292007-12-07 21:08:59344
Johannes Sixt2affea42009-09-11 17:40:08345 if (cmd->env)
346 env = make_augmented_environ(cmd->env);
Johannes Sixtba26f292007-12-07 21:08:59347
348 if (cmd->git_cmd) {
Steffen Prohaska108ac312008-07-28 05:50:28349 cmd->argv = prepare_git_cmd(cmd->argv);
Jeff King8dba1e62009-12-30 10:53:16350 } else if (cmd->use_shell) {
351 cmd->argv = prepare_shell_cmd(cmd->argv);
Johannes Sixtba26f292007-12-07 21:08:59352 }
353
Johannes Sixtf9a27432010-04-11 20:40:12354 cmd->pid = mingw_spawnvpe(cmd->argv[0], cmd->argv, env, cmd->dir,
Johannes Sixt75301f92010-01-15 20:12:18355 fhin, fhout, fherr);
Johannes Sixt0ac77ec2009-07-04 19:26:40356 failed_errno = errno;
Johannes Sixtc024beb2009-07-04 19:26:42357 if (cmd->pid < 0 && (!cmd->silent_exec_failure || errno != ENOENT))
Johannes Sixt0ac77ec2009-07-04 19:26:40358 error("cannot spawn %s: %s", cmd->argv[0], strerror(errno));
Johannes Sixtba26f292007-12-07 21:08:59359
360 if (cmd->env)
361 free_environ(env);
362 if (cmd->git_cmd)
Steffen Prohaska108ac312008-07-28 05:50:28363 free(cmd->argv);
Johannes Sixtba26f292007-12-07 21:08:59364
Steffen Prohaska108ac312008-07-28 05:50:28365 cmd->argv = sargv;
Johannes Sixt75301f92010-01-15 20:12:18366 if (fhin != 0)
367 close(fhin);
368 if (fhout != 1)
369 close(fhout);
370 if (fherr != 2)
371 close(fherr);
Frank Li0d30ad72009-09-16 08:20:17372}
Johannes Sixtba26f292007-12-07 21:08:59373#endif
374
375 if (cmd->pid < 0) {
Johannes Sixtba26f292007-12-07 21:08:59376 if (need_in)
377 close_pair(fdin);
378 else if (cmd->in)
379 close(cmd->in);
380 if (need_out)
381 close_pair(fdout);
382 else if (cmd->out)
383 close(cmd->out);
384 if (need_err)
385 close_pair(fderr);
bert Dvornikfc012c22010-05-20 18:57:52386 else if (cmd->err)
387 close(cmd->err);
Johannes Sixt0ac77ec2009-07-04 19:26:40388 errno = failed_errno;
389 return -1;
Johannes Sixtba26f292007-12-07 21:08:59390 }
Shawn O. Pearce4919bf02007-03-10 08:28:08391
392 if (need_in)
393 close(fdin[0]);
394 else if (cmd->in)
395 close(cmd->in);
396
Shawn O. Pearcef4bba252007-03-12 18:37:45397 if (need_out)
398 close(fdout[1]);
Johannes Sixtc20181e2008-02-21 22:42:56399 else if (cmd->out)
Shawn O. Pearcef4bba252007-03-12 18:37:45400 close(cmd->out);
401
Johannes Sixtf3b33f12007-10-19 19:47:58402 if (need_err)
403 close(fderr[1]);
Shawn O. Pearce4f41b612010-02-05 20:57:37404 else if (cmd->err)
405 close(cmd->err);
Johannes Sixtf3b33f12007-10-19 19:47:58406
Shawn O. Pearceebcb5d12007-03-10 08:28:05407 return 0;
408}
409
Johannes Sixt2d22c202007-10-19 19:48:00410int finish_command(struct child_process *cmd)
411{
Johannes Sixtc024beb2009-07-04 19:26:42412 return wait_or_whine(cmd->pid, cmd->argv[0], cmd->silent_exec_failure);
Johannes Sixt2d22c202007-10-19 19:48:00413}
414
Shawn O. Pearceebcb5d12007-03-10 08:28:05415int run_command(struct child_process *cmd)
416{
417 int code = start_command(cmd);
418 if (code)
419 return code;
420 return finish_command(cmd);
421}
422
Alex Riesen1568fea2007-05-22 21:48:23423static void prepare_run_command_v_opt(struct child_process *cmd,
Alex Riesenee493142007-05-22 21:48:47424 const char **argv,
425 int opt)
Alex Riesen1568fea2007-05-22 21:48:23426{
427 memset(cmd, 0, sizeof(*cmd));
428 cmd->argv = argv;
429 cmd->no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
430 cmd->git_cmd = opt & RUN_GIT_CMD ? 1 : 0;
431 cmd->stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
Johannes Sixtc024beb2009-07-04 19:26:42432 cmd->silent_exec_failure = opt & RUN_SILENT_EXEC_FAILURE ? 1 : 0;
Jeff King8dba1e62009-12-30 10:53:16433 cmd->use_shell = opt & RUN_USING_SHELL ? 1 : 0;
Alex Riesen1568fea2007-05-22 21:48:23434}
435
Shawn O. Pearcef1000892007-03-10 08:28:00436int run_command_v_opt(const char **argv, int opt)
437{
438 struct child_process cmd;
Alex Riesen1568fea2007-05-22 21:48:23439 prepare_run_command_v_opt(&cmd, argv, opt);
440 return run_command(&cmd);
441}
442
Alex Riesenee493142007-05-22 21:48:47443int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env)
444{
445 struct child_process cmd;
446 prepare_run_command_v_opt(&cmd, argv, opt);
447 cmd.dir = dir;
448 cmd.env = env;
449 return run_command(&cmd);
450}
Johannes Sixt2d22c202007-10-19 19:48:00451
Johannes Sixtf6b60982010-03-09 20:00:36452#ifndef NO_PTHREADS
Johannes Sixt0ea1c892010-03-06 15:40:43453static pthread_t main_thread;
454static int main_thread_set;
455static pthread_key_t async_key;
456
Johannes Sixt200a76b2010-03-06 15:40:42457static void *run_thread(void *data)
Johannes Sixt618ebe92007-12-08 21:19:14458{
459 struct async *async = data;
Johannes Sixtf6b60982010-03-09 20:00:36460 intptr_t ret;
Johannes Sixt0ea1c892010-03-06 15:40:43461
462 pthread_setspecific(async_key, async);
Johannes Sixtf6b60982010-03-09 20:00:36463 ret = async->proc(async->proc_in, async->proc_out, async->data);
Johannes Sixt200a76b2010-03-06 15:40:42464 return (void *)ret;
Johannes Sixt618ebe92007-12-08 21:19:14465}
Johannes Sixt0ea1c892010-03-06 15:40:43466
467static NORETURN void die_async(const char *err, va_list params)
468{
469 vreportf("fatal: ", err, params);
470
471 if (!pthread_equal(main_thread, pthread_self())) {
472 struct async *async = pthread_getspecific(async_key);
473 if (async->proc_in >= 0)
474 close(async->proc_in);
475 if (async->proc_out >= 0)
476 close(async->proc_out);
477 pthread_exit((void *)128);
478 }
479
480 exit(128);
Johannes Sixt618ebe92007-12-08 21:19:14481}
482#endif
483
Johannes Sixt2d22c202007-10-19 19:48:00484int start_async(struct async *async)
485{
Erik Faye-Lundae6a5602010-02-05 20:57:38486 int need_in, need_out;
487 int fdin[2], fdout[2];
488 int proc_in, proc_out;
Johannes Sixt2d22c202007-10-19 19:48:00489
Erik Faye-Lundae6a5602010-02-05 20:57:38490 need_in = async->in < 0;
491 if (need_in) {
492 if (pipe(fdin) < 0) {
493 if (async->out > 0)
494 close(async->out);
495 return error("cannot create pipe: %s", strerror(errno));
496 }
497 async->in = fdin[1];
498 }
499
500 need_out = async->out < 0;
501 if (need_out) {
502 if (pipe(fdout) < 0) {
503 if (need_in)
504 close_pair(fdin);
505 else if (async->in)
506 close(async->in);
507 return error("cannot create pipe: %s", strerror(errno));
508 }
509 async->out = fdout[0];
510 }
511
512 if (need_in)
513 proc_in = fdin[0];
514 else if (async->in)
515 proc_in = async->in;
516 else
517 proc_in = -1;
518
519 if (need_out)
520 proc_out = fdout[1];
521 else if (async->out)
522 proc_out = async->out;
523 else
524 proc_out = -1;
Johannes Sixt2d22c202007-10-19 19:48:00525
Johannes Sixtf6b60982010-03-09 20:00:36526#ifdef NO_PTHREADS
Anders Melchiorsen2c3766f2008-08-04 00:30:03527 /* Flush stdio before fork() to avoid cloning buffers */
528 fflush(NULL);
529
Johannes Sixt2d22c202007-10-19 19:48:00530 async->pid = fork();
531 if (async->pid < 0) {
532 error("fork (async) failed: %s", strerror(errno));
Erik Faye-Lundae6a5602010-02-05 20:57:38533 goto error;
Johannes Sixt2d22c202007-10-19 19:48:00534 }
535 if (!async->pid) {
Erik Faye-Lundae6a5602010-02-05 20:57:38536 if (need_in)
537 close(fdin[1]);
538 if (need_out)
539 close(fdout[0]);
540 exit(!!async->proc(proc_in, proc_out, async->data));
Johannes Sixt2d22c202007-10-19 19:48:00541 }
Erik Faye-Lundae6a5602010-02-05 20:57:38542
543 if (need_in)
544 close(fdin[0]);
545 else if (async->in)
546 close(async->in);
547
548 if (need_out)
549 close(fdout[1]);
550 else if (async->out)
551 close(async->out);
Johannes Sixt618ebe92007-12-08 21:19:14552#else
Johannes Sixt0ea1c892010-03-06 15:40:43553 if (!main_thread_set) {
554 /*
555 * We assume that the first time that start_async is called
556 * it is from the main thread.
557 */
558 main_thread_set = 1;
559 main_thread = pthread_self();
560 pthread_key_create(&async_key, NULL);
561 set_die_routine(die_async);
562 }
563
Johannes Sixt200a76b2010-03-06 15:40:42564 if (proc_in >= 0)
565 set_cloexec(proc_in);
566 if (proc_out >= 0)
567 set_cloexec(proc_out);
Erik Faye-Lundae6a5602010-02-05 20:57:38568 async->proc_in = proc_in;
569 async->proc_out = proc_out;
Johannes Sixt200a76b2010-03-06 15:40:42570 {
571 int err = pthread_create(&async->tid, NULL, run_thread, async);
572 if (err) {
573 error("cannot create thread: %s", strerror(err));
574 goto error;
575 }
Johannes Sixt618ebe92007-12-08 21:19:14576 }
577#endif
Johannes Sixt2d22c202007-10-19 19:48:00578 return 0;
Erik Faye-Lundae6a5602010-02-05 20:57:38579
580error:
581 if (need_in)
582 close_pair(fdin);
583 else if (async->in)
584 close(async->in);
585
586 if (need_out)
587 close_pair(fdout);
588 else if (async->out)
589 close(async->out);
590 return -1;
Johannes Sixt2d22c202007-10-19 19:48:00591}
592
593int finish_async(struct async *async)
594{
Johannes Sixtf6b60982010-03-09 20:00:36595#ifdef NO_PTHREADS
Johannes Sixt200a76b2010-03-06 15:40:42596 return wait_or_whine(async->pid, "child process", 0);
Johannes Sixt618ebe92007-12-08 21:19:14597#else
Johannes Sixt200a76b2010-03-06 15:40:42598 void *ret = (void *)(intptr_t)(-1);
599
600 if (pthread_join(async->tid, &ret))
601 error("pthread_join failed");
602 return (int)(intptr_t)ret;
Johannes Sixt618ebe92007-12-08 21:19:14603#endif
Johannes Sixt2d22c202007-10-19 19:48:00604}
Stephan Beyerae98a002009-01-16 19:09:59605
606int run_hook(const char *index_file, const char *name, ...)
607{
608 struct child_process hook;
Stephan Beyer14e62982009-01-17 03:02:55609 const char **argv = NULL, *env[2];
Stephan Beyerae98a002009-01-16 19:09:59610 char index[PATH_MAX];
611 va_list args;
612 int ret;
Stephan Beyer14e62982009-01-17 03:02:55613 size_t i = 0, alloc = 0;
Stephan Beyerae98a002009-01-16 19:09:59614
Stephan Beyercf94ca82009-01-16 19:10:01615 if (access(git_path("hooks/%s", name), X_OK) < 0)
616 return 0;
617
Stephan Beyerae98a002009-01-16 19:09:59618 va_start(args, name);
Stephan Beyer14e62982009-01-17 03:02:55619 ALLOC_GROW(argv, i + 1, alloc);
620 argv[i++] = git_path("hooks/%s", name);
621 while (argv[i-1]) {
622 ALLOC_GROW(argv, i + 1, alloc);
623 argv[i++] = va_arg(args, const char *);
624 }
Stephan Beyerae98a002009-01-16 19:09:59625 va_end(args);
626
Stephan Beyerae98a002009-01-16 19:09:59627 memset(&hook, 0, sizeof(hook));
628 hook.argv = argv;
629 hook.no_stdin = 1;
630 hook.stdout_to_stderr = 1;
631 if (index_file) {
632 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
633 env[0] = index;
634 env[1] = NULL;
635 hook.env = env;
636 }
637
Johannes Sixt0ac77ec2009-07-04 19:26:40638 ret = run_command(&hook);
Stephan Beyer14e62982009-01-17 03:02:55639 free(argv);
Stephan Beyerae98a002009-01-16 19:09:59640 return ret;
641}