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