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