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