]>
Commit | Line | Data |
---|---|---|
b1bf95bb JW |
1 | #include "cache.h" |
2 | #include "run-command.h" | |
77cb17e9 | 3 | #include "exec_cmd.h" |
afe19ff7 | 4 | #include "sigchain.h" |
5d40a179 | 5 | #include "argv-array.h" |
b1bf95bb | 6 | |
afe19ff7 JK |
7 | struct child_to_clean { |
8 | pid_t pid; | |
9 | struct child_to_clean *next; | |
10 | }; | |
11 | static struct child_to_clean *children_to_clean; | |
12 | static int installed_child_cleanup_handler; | |
13 | ||
14 | static void cleanup_children(int sig) | |
15 | { | |
16 | while (children_to_clean) { | |
17 | struct child_to_clean *p = children_to_clean; | |
18 | children_to_clean = p->next; | |
19 | kill(p->pid, sig); | |
20 | free(p); | |
21 | } | |
22 | } | |
23 | ||
24 | static void cleanup_children_on_signal(int sig) | |
25 | { | |
26 | cleanup_children(sig); | |
27 | sigchain_pop(sig); | |
28 | raise(sig); | |
29 | } | |
30 | ||
31 | static void cleanup_children_on_exit(void) | |
32 | { | |
33 | cleanup_children(SIGTERM); | |
34 | } | |
35 | ||
36 | static void mark_child_for_cleanup(pid_t pid) | |
37 | { | |
38 | struct child_to_clean *p = xmalloc(sizeof(*p)); | |
39 | p->pid = pid; | |
40 | p->next = children_to_clean; | |
41 | children_to_clean = p; | |
42 | ||
43 | if (!installed_child_cleanup_handler) { | |
44 | atexit(cleanup_children_on_exit); | |
45 | sigchain_push_common(cleanup_children_on_signal); | |
46 | installed_child_cleanup_handler = 1; | |
47 | } | |
48 | } | |
49 | ||
50 | static void clear_child_for_cleanup(pid_t pid) | |
51 | { | |
52 | struct child_to_clean **last, *p; | |
53 | ||
54 | last = &children_to_clean; | |
55 | for (p = children_to_clean; p; p = p->next) { | |
56 | if (p->pid == pid) { | |
57 | *last = p->next; | |
58 | free(p); | |
59 | return; | |
60 | } | |
61 | } | |
62 | } | |
63 | ||
9dc09c76 SP |
64 | static inline void close_pair(int fd[2]) |
65 | { | |
66 | close(fd[0]); | |
67 | close(fd[1]); | |
68 | } | |
69 | ||
75301f90 | 70 | #ifndef WIN32 |
e4507ae8 SP |
71 | static inline void dup_devnull(int to) |
72 | { | |
73 | int fd = open("/dev/null", O_RDWR); | |
74 | dup2(fd, to); | |
75 | close(fd); | |
76 | } | |
75301f90 | 77 | #endif |
e4507ae8 | 78 | |
8dba1e63 JK |
79 | static const char **prepare_shell_cmd(const char **argv) |
80 | { | |
81 | int argc, nargc = 0; | |
82 | const char **nargv; | |
83 | ||
84 | for (argc = 0; argv[argc]; argc++) | |
85 | ; /* just counting */ | |
86 | /* +1 for NULL, +3 for "sh -c" plus extra $0 */ | |
87 | nargv = xmalloc(sizeof(*nargv) * (argc + 1 + 3)); | |
88 | ||
89 | if (argc < 1) | |
90 | die("BUG: shell command is empty"); | |
91 | ||
f445644f JK |
92 | if (strcspn(argv[0], "|&;<>()$`\\\"' \t\n*?[#~=%") != strlen(argv[0])) { |
93 | nargv[nargc++] = "sh"; | |
94 | nargv[nargc++] = "-c"; | |
8dba1e63 | 95 | |
f445644f JK |
96 | if (argc < 2) |
97 | nargv[nargc++] = argv[0]; | |
98 | else { | |
99 | struct strbuf arg0 = STRBUF_INIT; | |
100 | strbuf_addf(&arg0, "%s \"$@\"", argv[0]); | |
101 | nargv[nargc++] = strbuf_detach(&arg0, NULL); | |
102 | } | |
8dba1e63 JK |
103 | } |
104 | ||
105 | for (argc = 0; argv[argc]; argc++) | |
106 | nargv[nargc++] = argv[argc]; | |
107 | nargv[nargc] = NULL; | |
108 | ||
109 | return nargv; | |
110 | } | |
111 | ||
112 | #ifndef WIN32 | |
113 | static int execv_shell_cmd(const char **argv) | |
114 | { | |
115 | const char **nargv = prepare_shell_cmd(argv); | |
116 | trace_argv_printf(nargv, "trace: exec:"); | |
117 | execvp(nargv[0], (char **)nargv); | |
118 | free(nargv); | |
119 | return -1; | |
120 | } | |
121 | #endif | |
122 | ||
a5487ddf JS |
123 | #ifndef WIN32 |
124 | static int child_err = 2; | |
2b541bf8 JS |
125 | static int child_notifier = -1; |
126 | ||
127 | static void notify_parent(void) | |
128 | { | |
a111eb78 JN |
129 | /* |
130 | * execvp failed. If possible, we'd like to let start_command | |
131 | * know, so failures like ENOENT can be handled right away; but | |
132 | * otherwise, finish_command will still report the error. | |
133 | */ | |
134 | xwrite(child_notifier, "", 1); | |
2b541bf8 | 135 | } |
a5487ddf JS |
136 | |
137 | static NORETURN void die_child(const char *err, va_list params) | |
138 | { | |
3bc4181f | 139 | vwritef(child_err, "fatal: ", err, params); |
a5487ddf JS |
140 | exit(128); |
141 | } | |
3bc4181f CB |
142 | |
143 | static void error_child(const char *err, va_list params) | |
144 | { | |
145 | vwritef(child_err, "error: ", err, params); | |
146 | } | |
200a76b7 | 147 | #endif |
a5487ddf JS |
148 | |
149 | static inline void set_cloexec(int fd) | |
150 | { | |
151 | int flags = fcntl(fd, F_GETFD); | |
152 | if (flags >= 0) | |
153 | fcntl(fd, F_SETFD, flags | FD_CLOEXEC); | |
154 | } | |
a5487ddf | 155 | |
ab0b41da JS |
156 | static int wait_or_whine(pid_t pid, const char *argv0, int silent_exec_failure) |
157 | { | |
158 | int status, code = -1; | |
159 | pid_t waiting; | |
160 | int failed_errno = 0; | |
161 | ||
162 | while ((waiting = waitpid(pid, &status, 0)) < 0 && errno == EINTR) | |
163 | ; /* nothing */ | |
164 | ||
165 | if (waiting < 0) { | |
166 | failed_errno = errno; | |
167 | error("waitpid for %s failed: %s", argv0, strerror(errno)); | |
168 | } else if (waiting != pid) { | |
169 | error("waitpid is confused (%s)", argv0); | |
170 | } else if (WIFSIGNALED(status)) { | |
171 | code = WTERMSIG(status); | |
172 | error("%s died of signal %d", argv0, code); | |
173 | /* | |
174 | * This return value is chosen so that code & 0xff | |
175 | * mimics the exit code that a POSIX shell would report for | |
176 | * a program that died from this signal. | |
177 | */ | |
178 | code -= 128; | |
179 | } else if (WIFEXITED(status)) { | |
180 | code = WEXITSTATUS(status); | |
181 | /* | |
182 | * Convert special exit code when execvp failed. | |
183 | */ | |
184 | if (code == 127) { | |
185 | code = -1; | |
186 | failed_errno = ENOENT; | |
ab0b41da JS |
187 | } |
188 | } else { | |
189 | error("waitpid is confused (%s)", argv0); | |
190 | } | |
afe19ff7 JK |
191 | |
192 | clear_child_for_cleanup(pid); | |
193 | ||
ab0b41da JS |
194 | errno = failed_errno; |
195 | return code; | |
196 | } | |
197 | ||
ebcb5d16 | 198 | int start_command(struct child_process *cmd) |
b1bf95bb | 199 | { |
f3b33f1d JS |
200 | int need_in, need_out, need_err; |
201 | int fdin[2], fdout[2], fderr[2]; | |
5a7a3671 | 202 | int failed_errno = failed_errno; |
4919bf03 | 203 | |
c20181e3 JS |
204 | /* |
205 | * In case of errors we must keep the promise to close FDs | |
206 | * that have been passed in via ->in and ->out. | |
207 | */ | |
208 | ||
e4507ae8 | 209 | need_in = !cmd->no_stdin && cmd->in < 0; |
4919bf03 | 210 | if (need_in) { |
c20181e3 | 211 | if (pipe(fdin) < 0) { |
0ac77ec3 | 212 | failed_errno = errno; |
c20181e3 JS |
213 | if (cmd->out > 0) |
214 | close(cmd->out); | |
0ac77ec3 | 215 | goto fail_pipe; |
c20181e3 | 216 | } |
4919bf03 | 217 | cmd->in = fdin[1]; |
4919bf03 SP |
218 | } |
219 | ||
e4507ae8 SP |
220 | need_out = !cmd->no_stdout |
221 | && !cmd->stdout_to_stderr | |
222 | && cmd->out < 0; | |
f4bba25b SP |
223 | if (need_out) { |
224 | if (pipe(fdout) < 0) { | |
0ac77ec3 | 225 | failed_errno = errno; |
f4bba25b SP |
226 | if (need_in) |
227 | close_pair(fdin); | |
c20181e3 JS |
228 | else if (cmd->in) |
229 | close(cmd->in); | |
0ac77ec3 | 230 | goto fail_pipe; |
f4bba25b SP |
231 | } |
232 | cmd->out = fdout[0]; | |
f4bba25b SP |
233 | } |
234 | ||
b73a4397 | 235 | need_err = !cmd->no_stderr && cmd->err < 0; |
f3b33f1d JS |
236 | if (need_err) { |
237 | if (pipe(fderr) < 0) { | |
0ac77ec3 | 238 | failed_errno = errno; |
f3b33f1d JS |
239 | if (need_in) |
240 | close_pair(fdin); | |
c20181e3 JS |
241 | else if (cmd->in) |
242 | close(cmd->in); | |
f3b33f1d JS |
243 | if (need_out) |
244 | close_pair(fdout); | |
c20181e3 JS |
245 | else if (cmd->out) |
246 | close(cmd->out); | |
0ac77ec3 JS |
247 | fail_pipe: |
248 | error("cannot create pipe for %s: %s", | |
249 | cmd->argv[0], strerror(failed_errno)); | |
250 | errno = failed_errno; | |
251 | return -1; | |
f3b33f1d JS |
252 | } |
253 | cmd->err = fderr[0]; | |
254 | } | |
255 | ||
8852f5d7 | 256 | trace_argv_printf(cmd->argv, "trace: run_command:"); |
13af8cbd | 257 | fflush(NULL); |
8852f5d7 | 258 | |
71064e3f | 259 | #ifndef WIN32 |
2b541bf8 JS |
260 | { |
261 | int notify_pipe[2]; | |
262 | if (pipe(notify_pipe)) | |
263 | notify_pipe[0] = notify_pipe[1] = -1; | |
264 | ||
ebcb5d16 | 265 | cmd->pid = fork(); |
ebcb5d16 | 266 | if (!cmd->pid) { |
a5487ddf JS |
267 | /* |
268 | * Redirect the channel to write syscall error messages to | |
269 | * before redirecting the process's stderr so that all die() | |
270 | * in subsequent call paths use the parent's stderr. | |
271 | */ | |
272 | if (cmd->no_stderr || need_err) { | |
273 | child_err = dup(2); | |
274 | set_cloexec(child_err); | |
275 | } | |
276 | set_die_routine(die_child); | |
3bc4181f | 277 | set_error_routine(error_child); |
a5487ddf | 278 | |
2b541bf8 JS |
279 | close(notify_pipe[0]); |
280 | set_cloexec(notify_pipe[1]); | |
281 | child_notifier = notify_pipe[1]; | |
282 | atexit(notify_parent); | |
283 | ||
e4507ae8 SP |
284 | if (cmd->no_stdin) |
285 | dup_devnull(0); | |
286 | else if (need_in) { | |
4919bf03 | 287 | dup2(fdin[0], 0); |
9dc09c76 | 288 | close_pair(fdin); |
4919bf03 SP |
289 | } else if (cmd->in) { |
290 | dup2(cmd->in, 0); | |
291 | close(cmd->in); | |
95d3c4f5 | 292 | } |
4919bf03 | 293 | |
ce2cf27a CC |
294 | if (cmd->no_stderr) |
295 | dup_devnull(2); | |
296 | else if (need_err) { | |
297 | dup2(fderr[1], 2); | |
298 | close_pair(fderr); | |
4f41b611 SP |
299 | } else if (cmd->err > 1) { |
300 | dup2(cmd->err, 2); | |
301 | close(cmd->err); | |
ce2cf27a CC |
302 | } |
303 | ||
e4507ae8 SP |
304 | if (cmd->no_stdout) |
305 | dup_devnull(1); | |
306 | else if (cmd->stdout_to_stderr) | |
cd83c74c | 307 | dup2(2, 1); |
f4bba25b SP |
308 | else if (need_out) { |
309 | dup2(fdout[1], 1); | |
310 | close_pair(fdout); | |
311 | } else if (cmd->out > 1) { | |
312 | dup2(cmd->out, 1); | |
313 | close(cmd->out); | |
314 | } | |
315 | ||
1568fea0 | 316 | if (cmd->dir && chdir(cmd->dir)) |
d824cbba TR |
317 | die_errno("exec '%s': cd to '%s' failed", cmd->argv[0], |
318 | cmd->dir); | |
ee493148 | 319 | if (cmd->env) { |
3427b375 AR |
320 | for (; *cmd->env; cmd->env++) { |
321 | if (strchr(*cmd->env, '=')) | |
4b25d091 | 322 | putenv((char *)*cmd->env); |
3427b375 AR |
323 | else |
324 | unsetenv(*cmd->env); | |
325 | } | |
ee493148 | 326 | } |
2b541bf8 JS |
327 | if (cmd->preexec_cb) { |
328 | /* | |
329 | * We cannot predict what the pre-exec callback does. | |
330 | * Forgo parent notification. | |
331 | */ | |
332 | close(child_notifier); | |
333 | child_notifier = -1; | |
334 | ||
ccf08bc3 | 335 | cmd->preexec_cb(); |
2b541bf8 | 336 | } |
f1000898 SP |
337 | if (cmd->git_cmd) { |
338 | execv_git_cmd(cmd->argv); | |
8dba1e63 JK |
339 | } else if (cmd->use_shell) { |
340 | execv_shell_cmd(cmd->argv); | |
77cb17e9 | 341 | } else { |
f1000898 | 342 | execvp(cmd->argv[0], (char *const*) cmd->argv); |
128aed68 | 343 | } |
fc1b56f0 CB |
344 | if (errno == ENOENT) { |
345 | if (!cmd->silent_exec_failure) | |
346 | error("cannot run %s: %s", cmd->argv[0], | |
347 | strerror(ENOENT)); | |
a5487ddf | 348 | exit(127); |
fc1b56f0 | 349 | } else { |
a5487ddf | 350 | die_errno("cannot exec '%s'", cmd->argv[0]); |
fc1b56f0 | 351 | } |
b1bf95bb | 352 | } |
0ac77ec3 JS |
353 | if (cmd->pid < 0) |
354 | error("cannot fork() for %s: %s", cmd->argv[0], | |
355 | strerror(failed_errno = errno)); | |
afe19ff7 JK |
356 | else if (cmd->clean_on_exit) |
357 | mark_child_for_cleanup(cmd->pid); | |
2b541bf8 JS |
358 | |
359 | /* | |
360 | * Wait for child's execvp. If the execvp succeeds (or if fork() | |
361 | * failed), EOF is seen immediately by the parent. Otherwise, the | |
362 | * child process sends a single byte. | |
363 | * Note that use of this infrastructure is completely advisory, | |
364 | * therefore, we keep error checks minimal. | |
365 | */ | |
366 | close(notify_pipe[1]); | |
367 | if (read(notify_pipe[0], ¬ify_pipe[1], 1) == 1) { | |
368 | /* | |
369 | * At this point we know that fork() succeeded, but execvp() | |
370 | * failed. Errors have been reported to our stderr. | |
371 | */ | |
372 | wait_or_whine(cmd->pid, cmd->argv[0], | |
373 | cmd->silent_exec_failure); | |
374 | failed_errno = errno; | |
375 | cmd->pid = -1; | |
376 | } | |
377 | close(notify_pipe[0]); | |
afe19ff7 | 378 | |
2b541bf8 | 379 | } |
ba26f296 | 380 | #else |
0d30ad71 | 381 | { |
75301f90 | 382 | int fhin = 0, fhout = 1, fherr = 2; |
108ac313 | 383 | const char **sargv = cmd->argv; |
ba26f296 | 384 | char **env = environ; |
ba26f296 | 385 | |
75301f90 JS |
386 | if (cmd->no_stdin) |
387 | fhin = open("/dev/null", O_RDWR); | |
388 | else if (need_in) | |
389 | fhin = dup(fdin[0]); | |
390 | else if (cmd->in) | |
391 | fhin = dup(cmd->in); | |
392 | ||
393 | if (cmd->no_stderr) | |
394 | fherr = open("/dev/null", O_RDWR); | |
395 | else if (need_err) | |
396 | fherr = dup(fderr[1]); | |
76d44c8c JH |
397 | else if (cmd->err > 2) |
398 | fherr = dup(cmd->err); | |
75301f90 JS |
399 | |
400 | if (cmd->no_stdout) | |
401 | fhout = open("/dev/null", O_RDWR); | |
402 | else if (cmd->stdout_to_stderr) | |
403 | fhout = dup(fherr); | |
404 | else if (need_out) | |
405 | fhout = dup(fdout[1]); | |
406 | else if (cmd->out > 1) | |
407 | fhout = dup(cmd->out); | |
ba26f296 | 408 | |
2affea41 JS |
409 | if (cmd->env) |
410 | env = make_augmented_environ(cmd->env); | |
ba26f296 JS |
411 | |
412 | if (cmd->git_cmd) { | |
108ac313 | 413 | cmd->argv = prepare_git_cmd(cmd->argv); |
8dba1e63 JK |
414 | } else if (cmd->use_shell) { |
415 | cmd->argv = prepare_shell_cmd(cmd->argv); | |
ba26f296 JS |
416 | } |
417 | ||
f9a2743c | 418 | cmd->pid = mingw_spawnvpe(cmd->argv[0], cmd->argv, env, cmd->dir, |
75301f90 | 419 | fhin, fhout, fherr); |
0ac77ec3 | 420 | failed_errno = errno; |
c024beb5 | 421 | if (cmd->pid < 0 && (!cmd->silent_exec_failure || errno != ENOENT)) |
0ac77ec3 | 422 | error("cannot spawn %s: %s", cmd->argv[0], strerror(errno)); |
afe19ff7 JK |
423 | if (cmd->clean_on_exit && cmd->pid >= 0) |
424 | mark_child_for_cleanup(cmd->pid); | |
ba26f296 JS |
425 | |
426 | if (cmd->env) | |
427 | free_environ(env); | |
428 | if (cmd->git_cmd) | |
108ac313 | 429 | free(cmd->argv); |
ba26f296 | 430 | |
108ac313 | 431 | cmd->argv = sargv; |
75301f90 JS |
432 | if (fhin != 0) |
433 | close(fhin); | |
434 | if (fhout != 1) | |
435 | close(fhout); | |
436 | if (fherr != 2) | |
437 | close(fherr); | |
0d30ad71 | 438 | } |
ba26f296 JS |
439 | #endif |
440 | ||
441 | if (cmd->pid < 0) { | |
442 | if (need_in) | |
443 | close_pair(fdin); | |
444 | else if (cmd->in) | |
445 | close(cmd->in); | |
446 | if (need_out) | |
447 | close_pair(fdout); | |
448 | else if (cmd->out) | |
449 | close(cmd->out); | |
450 | if (need_err) | |
451 | close_pair(fderr); | |
fc012c28 D |
452 | else if (cmd->err) |
453 | close(cmd->err); | |
0ac77ec3 JS |
454 | errno = failed_errno; |
455 | return -1; | |
ba26f296 | 456 | } |
4919bf03 SP |
457 | |
458 | if (need_in) | |
459 | close(fdin[0]); | |
460 | else if (cmd->in) | |
461 | close(cmd->in); | |
462 | ||
f4bba25b SP |
463 | if (need_out) |
464 | close(fdout[1]); | |
c20181e3 | 465 | else if (cmd->out) |
f4bba25b SP |
466 | close(cmd->out); |
467 | ||
f3b33f1d JS |
468 | if (need_err) |
469 | close(fderr[1]); | |
4f41b611 SP |
470 | else if (cmd->err) |
471 | close(cmd->err); | |
f3b33f1d | 472 | |
ebcb5d16 SP |
473 | return 0; |
474 | } | |
475 | ||
2d22c208 JS |
476 | int finish_command(struct child_process *cmd) |
477 | { | |
c024beb5 | 478 | return wait_or_whine(cmd->pid, cmd->argv[0], cmd->silent_exec_failure); |
2d22c208 JS |
479 | } |
480 | ||
ebcb5d16 SP |
481 | int run_command(struct child_process *cmd) |
482 | { | |
483 | int code = start_command(cmd); | |
484 | if (code) | |
485 | return code; | |
486 | return finish_command(cmd); | |
487 | } | |
488 | ||
1568fea0 | 489 | static void prepare_run_command_v_opt(struct child_process *cmd, |
ee493148 AR |
490 | const char **argv, |
491 | int opt) | |
1568fea0 AR |
492 | { |
493 | memset(cmd, 0, sizeof(*cmd)); | |
494 | cmd->argv = argv; | |
495 | cmd->no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0; | |
496 | cmd->git_cmd = opt & RUN_GIT_CMD ? 1 : 0; | |
497 | cmd->stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0; | |
c024beb5 | 498 | cmd->silent_exec_failure = opt & RUN_SILENT_EXEC_FAILURE ? 1 : 0; |
8dba1e63 | 499 | cmd->use_shell = opt & RUN_USING_SHELL ? 1 : 0; |
10c6cddd | 500 | cmd->clean_on_exit = opt & RUN_CLEAN_ON_EXIT ? 1 : 0; |
1568fea0 AR |
501 | } |
502 | ||
f1000898 SP |
503 | int run_command_v_opt(const char **argv, int opt) |
504 | { | |
505 | struct child_process cmd; | |
1568fea0 AR |
506 | prepare_run_command_v_opt(&cmd, argv, opt); |
507 | return run_command(&cmd); | |
508 | } | |
509 | ||
ee493148 AR |
510 | int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env) |
511 | { | |
512 | struct child_process cmd; | |
513 | prepare_run_command_v_opt(&cmd, argv, opt); | |
514 | cmd.dir = dir; | |
515 | cmd.env = env; | |
516 | return run_command(&cmd); | |
517 | } | |
2d22c208 | 518 | |
f6b60983 | 519 | #ifndef NO_PTHREADS |
0ea1c89b JS |
520 | static pthread_t main_thread; |
521 | static int main_thread_set; | |
522 | static pthread_key_t async_key; | |
523 | ||
200a76b7 | 524 | static void *run_thread(void *data) |
618ebe9f JS |
525 | { |
526 | struct async *async = data; | |
f6b60983 | 527 | intptr_t ret; |
0ea1c89b JS |
528 | |
529 | pthread_setspecific(async_key, async); | |
f6b60983 | 530 | ret = async->proc(async->proc_in, async->proc_out, async->data); |
200a76b7 | 531 | return (void *)ret; |
618ebe9f | 532 | } |
0ea1c89b JS |
533 | |
534 | static NORETURN void die_async(const char *err, va_list params) | |
535 | { | |
536 | vreportf("fatal: ", err, params); | |
537 | ||
538 | if (!pthread_equal(main_thread, pthread_self())) { | |
539 | struct async *async = pthread_getspecific(async_key); | |
540 | if (async->proc_in >= 0) | |
541 | close(async->proc_in); | |
542 | if (async->proc_out >= 0) | |
543 | close(async->proc_out); | |
544 | pthread_exit((void *)128); | |
545 | } | |
546 | ||
547 | exit(128); | |
618ebe9f JS |
548 | } |
549 | #endif | |
550 | ||
2d22c208 JS |
551 | int start_async(struct async *async) |
552 | { | |
ae6a5609 EFL |
553 | int need_in, need_out; |
554 | int fdin[2], fdout[2]; | |
555 | int proc_in, proc_out; | |
2d22c208 | 556 | |
ae6a5609 EFL |
557 | need_in = async->in < 0; |
558 | if (need_in) { | |
559 | if (pipe(fdin) < 0) { | |
560 | if (async->out > 0) | |
561 | close(async->out); | |
562 | return error("cannot create pipe: %s", strerror(errno)); | |
563 | } | |
564 | async->in = fdin[1]; | |
565 | } | |
566 | ||
567 | need_out = async->out < 0; | |
568 | if (need_out) { | |
569 | if (pipe(fdout) < 0) { | |
570 | if (need_in) | |
571 | close_pair(fdin); | |
572 | else if (async->in) | |
573 | close(async->in); | |
574 | return error("cannot create pipe: %s", strerror(errno)); | |
575 | } | |
576 | async->out = fdout[0]; | |
577 | } | |
578 | ||
579 | if (need_in) | |
580 | proc_in = fdin[0]; | |
581 | else if (async->in) | |
582 | proc_in = async->in; | |
583 | else | |
584 | proc_in = -1; | |
585 | ||
586 | if (need_out) | |
587 | proc_out = fdout[1]; | |
588 | else if (async->out) | |
589 | proc_out = async->out; | |
590 | else | |
591 | proc_out = -1; | |
2d22c208 | 592 | |
f6b60983 | 593 | #ifdef NO_PTHREADS |
2c3766f0 AM |
594 | /* Flush stdio before fork() to avoid cloning buffers */ |
595 | fflush(NULL); | |
596 | ||
2d22c208 JS |
597 | async->pid = fork(); |
598 | if (async->pid < 0) { | |
599 | error("fork (async) failed: %s", strerror(errno)); | |
ae6a5609 | 600 | goto error; |
2d22c208 JS |
601 | } |
602 | if (!async->pid) { | |
ae6a5609 EFL |
603 | if (need_in) |
604 | close(fdin[1]); | |
605 | if (need_out) | |
606 | close(fdout[0]); | |
607 | exit(!!async->proc(proc_in, proc_out, async->data)); | |
2d22c208 | 608 | } |
ae6a5609 | 609 | |
afe19ff7 JK |
610 | mark_child_for_cleanup(async->pid); |
611 | ||
ae6a5609 EFL |
612 | if (need_in) |
613 | close(fdin[0]); | |
614 | else if (async->in) | |
615 | close(async->in); | |
616 | ||
617 | if (need_out) | |
618 | close(fdout[1]); | |
619 | else if (async->out) | |
620 | close(async->out); | |
618ebe9f | 621 | #else |
0ea1c89b JS |
622 | if (!main_thread_set) { |
623 | /* | |
624 | * We assume that the first time that start_async is called | |
625 | * it is from the main thread. | |
626 | */ | |
627 | main_thread_set = 1; | |
628 | main_thread = pthread_self(); | |
629 | pthread_key_create(&async_key, NULL); | |
630 | set_die_routine(die_async); | |
631 | } | |
632 | ||
200a76b7 JS |
633 | if (proc_in >= 0) |
634 | set_cloexec(proc_in); | |
635 | if (proc_out >= 0) | |
636 | set_cloexec(proc_out); | |
ae6a5609 EFL |
637 | async->proc_in = proc_in; |
638 | async->proc_out = proc_out; | |
200a76b7 JS |
639 | { |
640 | int err = pthread_create(&async->tid, NULL, run_thread, async); | |
641 | if (err) { | |
642 | error("cannot create thread: %s", strerror(err)); | |
643 | goto error; | |
644 | } | |
618ebe9f JS |
645 | } |
646 | #endif | |
2d22c208 | 647 | return 0; |
ae6a5609 EFL |
648 | |
649 | error: | |
650 | if (need_in) | |
651 | close_pair(fdin); | |
652 | else if (async->in) | |
653 | close(async->in); | |
654 | ||
655 | if (need_out) | |
656 | close_pair(fdout); | |
657 | else if (async->out) | |
658 | close(async->out); | |
659 | return -1; | |
2d22c208 JS |
660 | } |
661 | ||
662 | int finish_async(struct async *async) | |
663 | { | |
f6b60983 | 664 | #ifdef NO_PTHREADS |
200a76b7 | 665 | return wait_or_whine(async->pid, "child process", 0); |
618ebe9f | 666 | #else |
200a76b7 JS |
667 | void *ret = (void *)(intptr_t)(-1); |
668 | ||
669 | if (pthread_join(async->tid, &ret)) | |
670 | error("pthread_join failed"); | |
671 | return (int)(intptr_t)ret; | |
618ebe9f | 672 | #endif |
2d22c208 | 673 | } |
ae98a008 SB |
674 | |
675 | int run_hook(const char *index_file, const char *name, ...) | |
676 | { | |
677 | struct child_process hook; | |
5d40a179 JK |
678 | struct argv_array argv = ARGV_ARRAY_INIT; |
679 | const char *p, *env[2]; | |
ae98a008 SB |
680 | char index[PATH_MAX]; |
681 | va_list args; | |
682 | int ret; | |
ae98a008 | 683 | |
cf94ca8e SB |
684 | if (access(git_path("hooks/%s", name), X_OK) < 0) |
685 | return 0; | |
686 | ||
ae98a008 | 687 | va_start(args, name); |
5d40a179 JK |
688 | argv_array_push(&argv, git_path("hooks/%s", name)); |
689 | while ((p = va_arg(args, const char *))) | |
690 | argv_array_push(&argv, p); | |
ae98a008 SB |
691 | va_end(args); |
692 | ||
ae98a008 | 693 | memset(&hook, 0, sizeof(hook)); |
5d40a179 | 694 | hook.argv = argv.argv; |
ae98a008 SB |
695 | hook.no_stdin = 1; |
696 | hook.stdout_to_stderr = 1; | |
697 | if (index_file) { | |
698 | snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file); | |
699 | env[0] = index; | |
700 | env[1] = NULL; | |
701 | hook.env = env; | |
702 | } | |
703 | ||
0ac77ec3 | 704 | ret = run_command(&hook); |
5d40a179 | 705 | argv_array_clear(&argv); |
ae98a008 SB |
706 | return ret; |
707 | } |