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