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