]>
Commit | Line | Data |
---|---|---|
b1bf95bb JW |
1 | #include "cache.h" |
2 | #include "run-command.h" | |
32a8f510 | 3 | #include "environment.h" |
d807c4a0 | 4 | #include "exec-cmd.h" |
f394e093 | 5 | #include "gettext.h" |
afe19ff7 | 6 | #include "sigchain.h" |
dbbcd44f | 7 | #include "strvec.h" |
c553c72e SB |
8 | #include "thread-utils.h" |
9 | #include "strbuf.h" | |
f805a00a | 10 | #include "string-list.h" |
74ea5c95 EN |
11 | #include "trace.h" |
12 | #include "trace2.h" | |
e73dd786 | 13 | #include "quote.h" |
1942d483 | 14 | #include "config.h" |
28d04e1e | 15 | #include "packfile.h" |
5e3aba33 | 16 | #include "hook.h" |
716c1f64 | 17 | #include "compat/nonblock.h" |
b1bf95bb | 18 | |
483bbd4e RS |
19 | void child_process_init(struct child_process *child) |
20 | { | |
5726a6b4 ÆAB |
21 | struct child_process blank = CHILD_PROCESS_INIT; |
22 | memcpy(child, &blank, sizeof(*child)); | |
483bbd4e RS |
23 | } |
24 | ||
2d71608e RS |
25 | void child_process_clear(struct child_process *child) |
26 | { | |
c972bf4c | 27 | strvec_clear(&child->args); |
29fda24d | 28 | strvec_clear(&child->env); |
2d71608e RS |
29 | } |
30 | ||
afe19ff7 JK |
31 | struct child_to_clean { |
32 | pid_t pid; | |
ac2fbaa6 | 33 | struct child_process *process; |
afe19ff7 JK |
34 | struct child_to_clean *next; |
35 | }; | |
36 | static struct child_to_clean *children_to_clean; | |
37 | static int installed_child_cleanup_handler; | |
38 | ||
507d7804 | 39 | static void cleanup_children(int sig, int in_signal) |
afe19ff7 | 40 | { |
46df6906 JK |
41 | struct child_to_clean *children_to_wait_for = NULL; |
42 | ||
afe19ff7 JK |
43 | while (children_to_clean) { |
44 | struct child_to_clean *p = children_to_clean; | |
45 | children_to_clean = p->next; | |
ac2fbaa6 LS |
46 | |
47 | if (p->process && !in_signal) { | |
48 | struct child_process *process = p->process; | |
49 | if (process->clean_on_exit_handler) { | |
50 | trace_printf( | |
51 | "trace: run_command: running exit handler for pid %" | |
52 | PRIuMAX, (uintmax_t)p->pid | |
53 | ); | |
54 | process->clean_on_exit_handler(process); | |
55 | } | |
56 | } | |
57 | ||
afe19ff7 | 58 | kill(p->pid, sig); |
46df6906 | 59 | |
7b91929b | 60 | if (p->process && p->process->wait_after_clean) { |
46df6906 JK |
61 | p->next = children_to_wait_for; |
62 | children_to_wait_for = p; | |
63 | } else { | |
64 | if (!in_signal) | |
65 | free(p); | |
66 | } | |
67 | } | |
68 | ||
69 | while (children_to_wait_for) { | |
70 | struct child_to_clean *p = children_to_wait_for; | |
71 | children_to_wait_for = p->next; | |
72 | ||
73 | while (waitpid(p->pid, NULL, 0) < 0 && errno == EINTR) | |
74 | ; /* spin waiting for process exit or error */ | |
75 | ||
507d7804 TI |
76 | if (!in_signal) |
77 | free(p); | |
afe19ff7 JK |
78 | } |
79 | } | |
80 | ||
81 | static void cleanup_children_on_signal(int sig) | |
82 | { | |
507d7804 | 83 | cleanup_children(sig, 1); |
afe19ff7 JK |
84 | sigchain_pop(sig); |
85 | raise(sig); | |
86 | } | |
87 | ||
88 | static void cleanup_children_on_exit(void) | |
89 | { | |
507d7804 | 90 | cleanup_children(SIGTERM, 0); |
afe19ff7 JK |
91 | } |
92 | ||
ac2fbaa6 | 93 | static void mark_child_for_cleanup(pid_t pid, struct child_process *process) |
afe19ff7 JK |
94 | { |
95 | struct child_to_clean *p = xmalloc(sizeof(*p)); | |
96 | p->pid = pid; | |
ac2fbaa6 | 97 | p->process = process; |
afe19ff7 JK |
98 | p->next = children_to_clean; |
99 | children_to_clean = p; | |
100 | ||
101 | if (!installed_child_cleanup_handler) { | |
102 | atexit(cleanup_children_on_exit); | |
103 | sigchain_push_common(cleanup_children_on_signal); | |
104 | installed_child_cleanup_handler = 1; | |
105 | } | |
106 | } | |
107 | ||
108 | static void clear_child_for_cleanup(pid_t pid) | |
109 | { | |
bdee397d | 110 | struct child_to_clean **pp; |
afe19ff7 | 111 | |
bdee397d DG |
112 | for (pp = &children_to_clean; *pp; pp = &(*pp)->next) { |
113 | struct child_to_clean *clean_me = *pp; | |
114 | ||
115 | if (clean_me->pid == pid) { | |
116 | *pp = clean_me->next; | |
117 | free(clean_me); | |
afe19ff7 JK |
118 | return; |
119 | } | |
120 | } | |
121 | } | |
122 | ||
9dc09c76 SP |
123 | static inline void close_pair(int fd[2]) |
124 | { | |
125 | close(fd[0]); | |
126 | close(fd[1]); | |
127 | } | |
128 | ||
38124a40 BW |
129 | int is_executable(const char *name) |
130 | { | |
131 | struct stat st; | |
132 | ||
133 | if (stat(name, &st) || /* stat, not lstat */ | |
134 | !S_ISREG(st.st_mode)) | |
135 | return 0; | |
136 | ||
137 | #if defined(GIT_WINDOWS_NATIVE) | |
138 | /* | |
139 | * On Windows there is no executable bit. The file extension | |
140 | * indicates whether it can be run as an executable, and Git | |
141 | * has special-handling to detect scripts and launch them | |
142 | * through the indicated script interpreter. We test for the | |
143 | * file extension first because virus scanners may make | |
144 | * it quite expensive to open many files. | |
145 | */ | |
146 | if (ends_with(name, ".exe")) | |
147 | return S_IXUSR; | |
148 | ||
149 | { | |
150 | /* | |
151 | * Now that we know it does not have an executable extension, | |
152 | * peek into the file instead. | |
153 | */ | |
154 | char buf[3] = { 0 }; | |
155 | int n; | |
156 | int fd = open(name, O_RDONLY); | |
157 | st.st_mode &= ~S_IXUSR; | |
158 | if (fd >= 0) { | |
159 | n = read(fd, buf, 2); | |
160 | if (n == 2) | |
161 | /* look for a she-bang */ | |
162 | if (!strcmp(buf, "#!")) | |
163 | st.st_mode |= S_IXUSR; | |
164 | close(fd); | |
165 | } | |
166 | } | |
167 | #endif | |
168 | return st.st_mode & S_IXUSR; | |
169 | } | |
170 | ||
94028310 BW |
171 | /* |
172 | * Search $PATH for a command. This emulates the path search that | |
173 | * execvp would perform, without actually executing the command so it | |
174 | * can be used before fork() to prepare to run a command using | |
175 | * execve() or after execvp() to diagnose why it failed. | |
176 | * | |
177 | * The caller should ensure that file contains no directory | |
178 | * separators. | |
179 | * | |
180 | * Returns the path to the command, as found in $PATH or NULL if the | |
181 | * command could not be found. The caller inherits ownership of the memory | |
182 | * used to store the resultant path. | |
183 | * | |
184 | * This should not be used on Windows, where the $PATH search rules | |
185 | * are more complicated (e.g., a search for "foo" should find | |
186 | * "foo.exe"). | |
187 | */ | |
38f865c2 JK |
188 | static char *locate_in_PATH(const char *file) |
189 | { | |
190 | const char *p = getenv("PATH"); | |
191 | struct strbuf buf = STRBUF_INIT; | |
192 | ||
193 | if (!p || !*p) | |
194 | return NULL; | |
195 | ||
196 | while (1) { | |
197 | const char *end = strchrnul(p, ':'); | |
198 | ||
199 | strbuf_reset(&buf); | |
200 | ||
201 | /* POSIX specifies an empty entry as the current directory. */ | |
202 | if (end != p) { | |
203 | strbuf_add(&buf, p, end - p); | |
204 | strbuf_addch(&buf, '/'); | |
205 | } | |
206 | strbuf_addstr(&buf, file); | |
207 | ||
94028310 | 208 | if (is_executable(buf.buf)) |
38f865c2 JK |
209 | return strbuf_detach(&buf, NULL); |
210 | ||
211 | if (!*end) | |
212 | break; | |
213 | p = end + 1; | |
214 | } | |
215 | ||
216 | strbuf_release(&buf); | |
217 | return NULL; | |
218 | } | |
219 | ||
3f36e6f3 | 220 | int exists_in_PATH(const char *command) |
38f865c2 | 221 | { |
3f36e6f3 | 222 | char *r = locate_in_PATH(command); |
63ab08fb | 223 | int found = r != NULL; |
38f865c2 | 224 | free(r); |
63ab08fb | 225 | return found; |
38f865c2 JK |
226 | } |
227 | ||
228 | int sane_execvp(const char *file, char * const argv[]) | |
229 | { | |
ee4512ed JH |
230 | #ifndef GIT_WINDOWS_NATIVE |
231 | /* | |
232 | * execvp() doesn't return, so we all we can do is tell trace2 | |
233 | * what we are about to do and let it leave a hint in the log | |
234 | * (unless of course the execvp() fails). | |
235 | * | |
236 | * we skip this for Windows because the compat layer already | |
237 | * has to emulate the execvp() call anyway. | |
238 | */ | |
239 | int exec_id = trace2_exec(file, (const char **)argv); | |
240 | #endif | |
241 | ||
38f865c2 JK |
242 | if (!execvp(file, argv)) |
243 | return 0; /* cannot happen ;-) */ | |
244 | ||
ee4512ed JH |
245 | #ifndef GIT_WINDOWS_NATIVE |
246 | { | |
247 | int ec = errno; | |
248 | trace2_exec_result(exec_id, ec); | |
249 | errno = ec; | |
250 | } | |
251 | #endif | |
252 | ||
38f865c2 JK |
253 | /* |
254 | * When a command can't be found because one of the directories | |
255 | * listed in $PATH is unsearchable, execvp reports EACCES, but | |
256 | * careful usability testing (read: analysis of occasional bug | |
257 | * reports) reveals that "No such file or directory" is more | |
258 | * intuitive. | |
259 | * | |
260 | * We avoid commands with "/", because execvp will not do $PATH | |
261 | * lookups in that case. | |
262 | * | |
263 | * The reassignment of EACCES to errno looks like a no-op below, | |
264 | * but we need to protect against exists_in_PATH overwriting errno. | |
265 | */ | |
266 | if (errno == EACCES && !strchr(file, '/')) | |
267 | errno = exists_in_PATH(file) ? EACCES : ENOENT; | |
a7855083 JH |
268 | else if (errno == ENOTDIR && !strchr(file, '/')) |
269 | errno = ENOENT; | |
38f865c2 JK |
270 | return -1; |
271 | } | |
272 | ||
c972bf4c | 273 | static const char **prepare_shell_cmd(struct strvec *out, const char **argv) |
8dba1e63 | 274 | { |
20574f55 | 275 | if (!argv[0]) |
033abf97 | 276 | BUG("shell command is empty"); |
8dba1e63 | 277 | |
f445644f | 278 | if (strcspn(argv[0], "|&;<>()$`\\\"' \t\n*?[#~=%") != strlen(argv[0])) { |
380395d0 | 279 | #ifndef GIT_WINDOWS_NATIVE |
c972bf4c | 280 | strvec_push(out, SHELL_PATH); |
77629754 | 281 | #else |
c972bf4c | 282 | strvec_push(out, "sh"); |
77629754 | 283 | #endif |
c972bf4c | 284 | strvec_push(out, "-c"); |
8dba1e63 | 285 | |
20574f55 JK |
286 | /* |
287 | * If we have no extra arguments, we do not even need to | |
288 | * bother with the "$@" magic. | |
289 | */ | |
290 | if (!argv[1]) | |
c972bf4c | 291 | strvec_push(out, argv[0]); |
20574f55 | 292 | else |
c972bf4c | 293 | strvec_pushf(out, "%s \"$@\"", argv[0]); |
20574f55 | 294 | } |
8dba1e63 | 295 | |
c972bf4c | 296 | strvec_pushv(out, argv); |
d70a9eb6 | 297 | return out->v; |
8dba1e63 JK |
298 | } |
299 | ||
380395d0 | 300 | #ifndef GIT_WINDOWS_NATIVE |
2b541bf8 JS |
301 | static int child_notifier = -1; |
302 | ||
79319b19 BW |
303 | enum child_errcode { |
304 | CHILD_ERR_CHDIR, | |
53fa6753 BW |
305 | CHILD_ERR_DUP2, |
306 | CHILD_ERR_CLOSE, | |
45afb1ca | 307 | CHILD_ERR_SIGPROCMASK, |
79319b19 BW |
308 | CHILD_ERR_ENOENT, |
309 | CHILD_ERR_SILENT, | |
310 | CHILD_ERR_ERRNO | |
311 | }; | |
312 | ||
313 | struct child_err { | |
314 | enum child_errcode err; | |
315 | int syserr; /* errno */ | |
316 | }; | |
317 | ||
318 | static void child_die(enum child_errcode err) | |
2b541bf8 | 319 | { |
79319b19 BW |
320 | struct child_err buf; |
321 | ||
322 | buf.err = err; | |
323 | buf.syserr = errno; | |
324 | ||
325 | /* write(2) on buf smaller than PIPE_BUF (min 512) is atomic: */ | |
326 | xwrite(child_notifier, &buf, sizeof(buf)); | |
327 | _exit(1); | |
328 | } | |
329 | ||
53fa6753 BW |
330 | static void child_dup2(int fd, int to) |
331 | { | |
332 | if (dup2(fd, to) < 0) | |
333 | child_die(CHILD_ERR_DUP2); | |
334 | } | |
335 | ||
336 | static void child_close(int fd) | |
337 | { | |
338 | if (close(fd)) | |
339 | child_die(CHILD_ERR_CLOSE); | |
340 | } | |
341 | ||
342 | static void child_close_pair(int fd[2]) | |
343 | { | |
344 | child_close(fd[0]); | |
345 | child_close(fd[1]); | |
346 | } | |
347 | ||
ce41759e | 348 | static void child_error_fn(const char *err UNUSED, va_list params UNUSED) |
79319b19 BW |
349 | { |
350 | const char msg[] = "error() should not be called in child\n"; | |
351 | xwrite(2, msg, sizeof(msg) - 1); | |
352 | } | |
353 | ||
ce41759e | 354 | static void child_warn_fn(const char *err UNUSED, va_list params UNUSED) |
79319b19 BW |
355 | { |
356 | const char msg[] = "warn() should not be called in child\n"; | |
357 | xwrite(2, msg, sizeof(msg) - 1); | |
358 | } | |
359 | ||
ce41759e | 360 | static void NORETURN child_die_fn(const char *err UNUSED, va_list params UNUSED) |
79319b19 BW |
361 | { |
362 | const char msg[] = "die() should not be called in child\n"; | |
363 | xwrite(2, msg, sizeof(msg) - 1); | |
364 | _exit(2); | |
365 | } | |
366 | ||
367 | /* this runs in the parent process */ | |
368 | static void child_err_spew(struct child_process *cmd, struct child_err *cerr) | |
369 | { | |
370 | static void (*old_errfn)(const char *err, va_list params); | |
e081a7c3 | 371 | report_fn die_message_routine = get_die_message_routine(); |
79319b19 BW |
372 | |
373 | old_errfn = get_error_routine(); | |
e081a7c3 | 374 | set_error_routine(die_message_routine); |
79319b19 BW |
375 | errno = cerr->syserr; |
376 | ||
377 | switch (cerr->err) { | |
378 | case CHILD_ERR_CHDIR: | |
379 | error_errno("exec '%s': cd to '%s' failed", | |
d3b21597 | 380 | cmd->args.v[0], cmd->dir); |
79319b19 | 381 | break; |
53fa6753 BW |
382 | case CHILD_ERR_DUP2: |
383 | error_errno("dup2() in child failed"); | |
384 | break; | |
385 | case CHILD_ERR_CLOSE: | |
386 | error_errno("close() in child failed"); | |
387 | break; | |
45afb1ca EW |
388 | case CHILD_ERR_SIGPROCMASK: |
389 | error_errno("sigprocmask failed restoring signals"); | |
390 | break; | |
79319b19 | 391 | case CHILD_ERR_ENOENT: |
d3b21597 | 392 | error_errno("cannot run %s", cmd->args.v[0]); |
79319b19 BW |
393 | break; |
394 | case CHILD_ERR_SILENT: | |
395 | break; | |
396 | case CHILD_ERR_ERRNO: | |
d3b21597 | 397 | error_errno("cannot exec '%s'", cmd->args.v[0]); |
79319b19 BW |
398 | break; |
399 | } | |
400 | set_error_routine(old_errfn); | |
2b541bf8 | 401 | } |
3967e25b | 402 | |
c972bf4c | 403 | static int prepare_cmd(struct strvec *out, const struct child_process *cmd) |
3967e25b | 404 | { |
d3b21597 | 405 | if (!cmd->args.v[0]) |
033abf97 | 406 | BUG("command is empty"); |
3967e25b | 407 | |
e3a43446 BW |
408 | /* |
409 | * Add SHELL_PATH so in the event exec fails with ENOEXEC we can | |
410 | * attempt to interpret the command with 'sh'. | |
411 | */ | |
c972bf4c | 412 | strvec_push(out, SHELL_PATH); |
e3a43446 | 413 | |
3967e25b | 414 | if (cmd->git_cmd) { |
d3b21597 | 415 | prepare_git_cmd(out, cmd->args.v); |
3967e25b | 416 | } else if (cmd->use_shell) { |
d3b21597 | 417 | prepare_shell_cmd(out, cmd->args.v); |
3967e25b | 418 | } else { |
d3b21597 | 419 | strvec_pushv(out, cmd->args.v); |
3967e25b | 420 | } |
e3a43446 BW |
421 | |
422 | /* | |
05ac8582 AK |
423 | * If there are no dir separator characters in the command then perform |
424 | * a path lookup and use the resolved path as the command to exec. If | |
425 | * there are dir separator characters, we have exec attempt to invoke | |
426 | * the command directly. | |
e3a43446 | 427 | */ |
d70a9eb6 JK |
428 | if (!has_dir_sep(out->v[1])) { |
429 | char *program = locate_in_PATH(out->v[1]); | |
e3a43446 | 430 | if (program) { |
d70a9eb6 JK |
431 | free((char *)out->v[1]); |
432 | out->v[1] = program; | |
321fd823 | 433 | } else { |
c972bf4c | 434 | strvec_clear(out); |
321fd823 JK |
435 | errno = ENOENT; |
436 | return -1; | |
e3a43446 BW |
437 | } |
438 | } | |
321fd823 JK |
439 | |
440 | return 0; | |
3967e25b | 441 | } |
ae25394b BW |
442 | |
443 | static char **prep_childenv(const char *const *deltaenv) | |
444 | { | |
445 | extern char **environ; | |
446 | char **childenv; | |
447 | struct string_list env = STRING_LIST_INIT_DUP; | |
448 | struct strbuf key = STRBUF_INIT; | |
449 | const char *const *p; | |
450 | int i; | |
451 | ||
452 | /* Construct a sorted string list consisting of the current environ */ | |
453 | for (p = (const char *const *) environ; p && *p; p++) { | |
454 | const char *equals = strchr(*p, '='); | |
455 | ||
456 | if (equals) { | |
457 | strbuf_reset(&key); | |
458 | strbuf_add(&key, *p, equals - *p); | |
459 | string_list_append(&env, key.buf)->util = (void *) *p; | |
460 | } else { | |
461 | string_list_append(&env, *p)->util = (void *) *p; | |
462 | } | |
463 | } | |
464 | string_list_sort(&env); | |
465 | ||
466 | /* Merge in 'deltaenv' with the current environ */ | |
467 | for (p = deltaenv; p && *p; p++) { | |
468 | const char *equals = strchr(*p, '='); | |
469 | ||
470 | if (equals) { | |
471 | /* ('key=value'), insert or replace entry */ | |
472 | strbuf_reset(&key); | |
473 | strbuf_add(&key, *p, equals - *p); | |
474 | string_list_insert(&env, key.buf)->util = (void *) *p; | |
475 | } else { | |
476 | /* otherwise ('key') remove existing entry */ | |
477 | string_list_remove(&env, *p, 0); | |
478 | } | |
479 | } | |
480 | ||
481 | /* Create an array of 'char *' to be used as the childenv */ | |
0e187d75 | 482 | ALLOC_ARRAY(childenv, env.nr + 1); |
ae25394b BW |
483 | for (i = 0; i < env.nr; i++) |
484 | childenv[i] = env.items[i].util; | |
485 | childenv[env.nr] = NULL; | |
486 | ||
487 | string_list_clear(&env, 0); | |
488 | strbuf_release(&key); | |
489 | return childenv; | |
490 | } | |
45afb1ca EW |
491 | |
492 | struct atfork_state { | |
493 | #ifndef NO_PTHREADS | |
494 | int cs; | |
495 | #endif | |
496 | sigset_t old; | |
497 | }; | |
498 | ||
dde74d73 JS |
499 | #define CHECK_BUG(err, msg) \ |
500 | do { \ | |
501 | int e = (err); \ | |
502 | if (e) \ | |
503 | BUG("%s: %s", msg, strerror(e)); \ | |
504 | } while(0) | |
45afb1ca EW |
505 | |
506 | static void atfork_prepare(struct atfork_state *as) | |
507 | { | |
508 | sigset_t all; | |
509 | ||
510 | if (sigfillset(&all)) | |
511 | die_errno("sigfillset"); | |
512 | #ifdef NO_PTHREADS | |
513 | if (sigprocmask(SIG_SETMASK, &all, &as->old)) | |
514 | die_errno("sigprocmask"); | |
515 | #else | |
dde74d73 | 516 | CHECK_BUG(pthread_sigmask(SIG_SETMASK, &all, &as->old), |
45afb1ca | 517 | "blocking all signals"); |
dde74d73 | 518 | CHECK_BUG(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &as->cs), |
45afb1ca EW |
519 | "disabling cancellation"); |
520 | #endif | |
521 | } | |
522 | ||
523 | static void atfork_parent(struct atfork_state *as) | |
524 | { | |
525 | #ifdef NO_PTHREADS | |
526 | if (sigprocmask(SIG_SETMASK, &as->old, NULL)) | |
527 | die_errno("sigprocmask"); | |
528 | #else | |
dde74d73 | 529 | CHECK_BUG(pthread_setcancelstate(as->cs, NULL), |
45afb1ca | 530 | "re-enabling cancellation"); |
dde74d73 | 531 | CHECK_BUG(pthread_sigmask(SIG_SETMASK, &as->old, NULL), |
45afb1ca | 532 | "restoring signal mask"); |
200a76b7 | 533 | #endif |
45afb1ca EW |
534 | } |
535 | #endif /* GIT_WINDOWS_NATIVE */ | |
a5487ddf JS |
536 | |
537 | static inline void set_cloexec(int fd) | |
538 | { | |
539 | int flags = fcntl(fd, F_GETFD); | |
540 | if (flags >= 0) | |
541 | fcntl(fd, F_SETFD, flags | FD_CLOEXEC); | |
542 | } | |
a5487ddf | 543 | |
507d7804 | 544 | static int wait_or_whine(pid_t pid, const char *argv0, int in_signal) |
ab0b41da JS |
545 | { |
546 | int status, code = -1; | |
547 | pid_t waiting; | |
548 | int failed_errno = 0; | |
549 | ||
550 | while ((waiting = waitpid(pid, &status, 0)) < 0 && errno == EINTR) | |
551 | ; /* nothing */ | |
552 | ||
553 | if (waiting < 0) { | |
554 | failed_errno = errno; | |
96bfb2d8 JK |
555 | if (!in_signal) |
556 | error_errno("waitpid for %s failed", argv0); | |
ab0b41da | 557 | } else if (waiting != pid) { |
96bfb2d8 JK |
558 | if (!in_signal) |
559 | error("waitpid is confused (%s)", argv0); | |
ab0b41da JS |
560 | } else if (WIFSIGNALED(status)) { |
561 | code = WTERMSIG(status); | |
96bfb2d8 | 562 | if (!in_signal && code != SIGINT && code != SIGQUIT && code != SIGPIPE) |
a2767c5c | 563 | error("%s died of signal %d", argv0, code); |
ab0b41da JS |
564 | /* |
565 | * This return value is chosen so that code & 0xff | |
566 | * mimics the exit code that a POSIX shell would report for | |
567 | * a program that died from this signal. | |
568 | */ | |
709ca730 | 569 | code += 128; |
ab0b41da JS |
570 | } else if (WIFEXITED(status)) { |
571 | code = WEXITSTATUS(status); | |
ab0b41da | 572 | } else { |
96bfb2d8 JK |
573 | if (!in_signal) |
574 | error("waitpid is confused (%s)", argv0); | |
ab0b41da | 575 | } |
afe19ff7 | 576 | |
96bfb2d8 JK |
577 | if (!in_signal) |
578 | clear_child_for_cleanup(pid); | |
afe19ff7 | 579 | |
ab0b41da JS |
580 | errno = failed_errno; |
581 | return code; | |
582 | } | |
583 | ||
c61a975d NTND |
584 | static void trace_add_env(struct strbuf *dst, const char *const *deltaenv) |
585 | { | |
586 | struct string_list envs = STRING_LIST_INIT_DUP; | |
587 | const char *const *e; | |
588 | int i; | |
589 | int printed_unset = 0; | |
590 | ||
591 | /* Last one wins, see run-command.c:prep_childenv() for context */ | |
592 | for (e = deltaenv; e && *e; e++) { | |
593 | struct strbuf key = STRBUF_INIT; | |
594 | char *equals = strchr(*e, '='); | |
595 | ||
596 | if (equals) { | |
597 | strbuf_add(&key, *e, equals - *e); | |
598 | string_list_insert(&envs, key.buf)->util = equals + 1; | |
599 | } else { | |
600 | string_list_insert(&envs, *e)->util = NULL; | |
601 | } | |
602 | strbuf_release(&key); | |
603 | } | |
604 | ||
605 | /* "unset X Y...;" */ | |
606 | for (i = 0; i < envs.nr; i++) { | |
607 | const char *var = envs.items[i].string; | |
608 | const char *val = envs.items[i].util; | |
609 | ||
610 | if (val || !getenv(var)) | |
611 | continue; | |
612 | ||
613 | if (!printed_unset) { | |
614 | strbuf_addstr(dst, " unset"); | |
615 | printed_unset = 1; | |
616 | } | |
617 | strbuf_addf(dst, " %s", var); | |
618 | } | |
619 | if (printed_unset) | |
620 | strbuf_addch(dst, ';'); | |
621 | ||
622 | /* ... followed by "A=B C=D ..." */ | |
623 | for (i = 0; i < envs.nr; i++) { | |
624 | const char *var = envs.items[i].string; | |
625 | const char *val = envs.items[i].util; | |
626 | const char *oldval; | |
627 | ||
628 | if (!val) | |
629 | continue; | |
630 | ||
631 | oldval = getenv(var); | |
632 | if (oldval && !strcmp(val, oldval)) | |
633 | continue; | |
634 | ||
635 | strbuf_addf(dst, " %s=", var); | |
636 | sq_quote_buf_pretty(dst, val); | |
637 | } | |
638 | string_list_clear(&envs, 0); | |
639 | } | |
640 | ||
e73dd786 NTND |
641 | static void trace_run_command(const struct child_process *cp) |
642 | { | |
643 | struct strbuf buf = STRBUF_INIT; | |
644 | ||
645 | if (!trace_want(&trace_default_key)) | |
646 | return; | |
647 | ||
248f66ed | 648 | strbuf_addstr(&buf, "trace: run_command:"); |
090a0927 NTND |
649 | if (cp->dir) { |
650 | strbuf_addstr(&buf, " cd "); | |
651 | sq_quote_buf_pretty(&buf, cp->dir); | |
652 | strbuf_addch(&buf, ';'); | |
653 | } | |
29fda24d | 654 | trace_add_env(&buf, cp->env.v); |
21dfc5e0 NTND |
655 | if (cp->git_cmd) |
656 | strbuf_addstr(&buf, " git"); | |
d3b21597 | 657 | sq_quote_argv_pretty(&buf, cp->args.v); |
e73dd786 NTND |
658 | |
659 | trace_printf("%s", buf.buf); | |
660 | strbuf_release(&buf); | |
661 | } | |
662 | ||
ebcb5d16 | 663 | int start_command(struct child_process *cmd) |
b1bf95bb | 664 | { |
f3b33f1d JS |
665 | int need_in, need_out, need_err; |
666 | int fdin[2], fdout[2], fderr[2]; | |
25043d8a | 667 | int failed_errno; |
939296c4 | 668 | char *str; |
4919bf03 | 669 | |
c20181e3 JS |
670 | /* |
671 | * In case of errors we must keep the promise to close FDs | |
672 | * that have been passed in via ->in and ->out. | |
673 | */ | |
674 | ||
e4507ae8 | 675 | need_in = !cmd->no_stdin && cmd->in < 0; |
4919bf03 | 676 | if (need_in) { |
c20181e3 | 677 | if (pipe(fdin) < 0) { |
0ac77ec3 | 678 | failed_errno = errno; |
c20181e3 JS |
679 | if (cmd->out > 0) |
680 | close(cmd->out); | |
939296c4 | 681 | str = "standard input"; |
0ac77ec3 | 682 | goto fail_pipe; |
c20181e3 | 683 | } |
4919bf03 | 684 | cmd->in = fdin[1]; |
4919bf03 SP |
685 | } |
686 | ||
e4507ae8 SP |
687 | need_out = !cmd->no_stdout |
688 | && !cmd->stdout_to_stderr | |
689 | && cmd->out < 0; | |
f4bba25b SP |
690 | if (need_out) { |
691 | if (pipe(fdout) < 0) { | |
0ac77ec3 | 692 | failed_errno = errno; |
f4bba25b SP |
693 | if (need_in) |
694 | close_pair(fdin); | |
c20181e3 JS |
695 | else if (cmd->in) |
696 | close(cmd->in); | |
939296c4 | 697 | str = "standard output"; |
0ac77ec3 | 698 | goto fail_pipe; |
f4bba25b SP |
699 | } |
700 | cmd->out = fdout[0]; | |
f4bba25b SP |
701 | } |
702 | ||
b73a4397 | 703 | need_err = !cmd->no_stderr && cmd->err < 0; |
f3b33f1d JS |
704 | if (need_err) { |
705 | if (pipe(fderr) < 0) { | |
0ac77ec3 | 706 | failed_errno = errno; |
f3b33f1d JS |
707 | if (need_in) |
708 | close_pair(fdin); | |
c20181e3 JS |
709 | else if (cmd->in) |
710 | close(cmd->in); | |
f3b33f1d JS |
711 | if (need_out) |
712 | close_pair(fdout); | |
c20181e3 JS |
713 | else if (cmd->out) |
714 | close(cmd->out); | |
939296c4 | 715 | str = "standard error"; |
0ac77ec3 | 716 | fail_pipe: |
939296c4 | 717 | error("cannot create %s pipe for %s: %s", |
d3b21597 | 718 | str, cmd->args.v[0], strerror(failed_errno)); |
2d71608e | 719 | child_process_clear(cmd); |
0ac77ec3 JS |
720 | errno = failed_errno; |
721 | return -1; | |
f3b33f1d JS |
722 | } |
723 | cmd->err = fderr[0]; | |
724 | } | |
725 | ||
ee4512ed | 726 | trace2_child_start(cmd); |
e73dd786 NTND |
727 | trace_run_command(cmd); |
728 | ||
13af8cbd | 729 | fflush(NULL); |
8852f5d7 | 730 | |
28d04e1e JS |
731 | if (cmd->close_object_store) |
732 | close_object_store(the_repository->objects); | |
733 | ||
380395d0 | 734 | #ifndef GIT_WINDOWS_NATIVE |
2b541bf8 JS |
735 | { |
736 | int notify_pipe[2]; | |
db015a28 | 737 | int null_fd = -1; |
ae25394b | 738 | char **childenv; |
c972bf4c | 739 | struct strvec argv = STRVEC_INIT; |
79319b19 | 740 | struct child_err cerr; |
45afb1ca | 741 | struct atfork_state as; |
3967e25b | 742 | |
321fd823 JK |
743 | if (prepare_cmd(&argv, cmd) < 0) { |
744 | failed_errno = errno; | |
745 | cmd->pid = -1; | |
e5a329a2 | 746 | if (!cmd->silent_exec_failure) |
d3b21597 | 747 | error_errno("cannot run %s", cmd->args.v[0]); |
321fd823 JK |
748 | goto end_of_spawn; |
749 | } | |
750 | ||
2b541bf8 JS |
751 | if (pipe(notify_pipe)) |
752 | notify_pipe[0] = notify_pipe[1] = -1; | |
753 | ||
db015a28 | 754 | if (cmd->no_stdin || cmd->no_stdout || cmd->no_stderr) { |
66e905b7 | 755 | null_fd = xopen("/dev/null", O_RDWR | O_CLOEXEC); |
db015a28 BW |
756 | set_cloexec(null_fd); |
757 | } | |
758 | ||
29fda24d | 759 | childenv = prep_childenv(cmd->env.v); |
45afb1ca | 760 | atfork_prepare(&as); |
3967e25b | 761 | |
e503cd6e BW |
762 | /* |
763 | * NOTE: In order to prevent deadlocking when using threads special | |
764 | * care should be taken with the function calls made in between the | |
765 | * fork() and exec() calls. No calls should be made to functions which | |
766 | * require acquiring a lock (e.g. malloc) as the lock could have been | |
767 | * held by another thread at the time of forking, causing the lock to | |
768 | * never be released in the child process. This means only | |
769 | * Async-Signal-Safe functions are permitted in the child. | |
770 | */ | |
ebcb5d16 | 771 | cmd->pid = fork(); |
25043d8a | 772 | failed_errno = errno; |
ebcb5d16 | 773 | if (!cmd->pid) { |
45afb1ca | 774 | int sig; |
a5487ddf | 775 | /* |
79319b19 BW |
776 | * Ensure the default die/error/warn routines do not get |
777 | * called, they can take stdio locks and malloc. | |
a5487ddf | 778 | */ |
79319b19 BW |
779 | set_die_routine(child_die_fn); |
780 | set_error_routine(child_error_fn); | |
781 | set_warn_routine(child_warn_fn); | |
a5487ddf | 782 | |
2b541bf8 JS |
783 | close(notify_pipe[0]); |
784 | set_cloexec(notify_pipe[1]); | |
785 | child_notifier = notify_pipe[1]; | |
2b541bf8 | 786 | |
e4507ae8 | 787 | if (cmd->no_stdin) |
53fa6753 | 788 | child_dup2(null_fd, 0); |
e4507ae8 | 789 | else if (need_in) { |
53fa6753 BW |
790 | child_dup2(fdin[0], 0); |
791 | child_close_pair(fdin); | |
4919bf03 | 792 | } else if (cmd->in) { |
53fa6753 BW |
793 | child_dup2(cmd->in, 0); |
794 | child_close(cmd->in); | |
95d3c4f5 | 795 | } |
4919bf03 | 796 | |
ce2cf27a | 797 | if (cmd->no_stderr) |
53fa6753 | 798 | child_dup2(null_fd, 2); |
ce2cf27a | 799 | else if (need_err) { |
53fa6753 BW |
800 | child_dup2(fderr[1], 2); |
801 | child_close_pair(fderr); | |
4f41b611 | 802 | } else if (cmd->err > 1) { |
53fa6753 BW |
803 | child_dup2(cmd->err, 2); |
804 | child_close(cmd->err); | |
ce2cf27a CC |
805 | } |
806 | ||
e4507ae8 | 807 | if (cmd->no_stdout) |
53fa6753 | 808 | child_dup2(null_fd, 1); |
e4507ae8 | 809 | else if (cmd->stdout_to_stderr) |
53fa6753 | 810 | child_dup2(2, 1); |
f4bba25b | 811 | else if (need_out) { |
53fa6753 BW |
812 | child_dup2(fdout[1], 1); |
813 | child_close_pair(fdout); | |
f4bba25b | 814 | } else if (cmd->out > 1) { |
53fa6753 BW |
815 | child_dup2(cmd->out, 1); |
816 | child_close(cmd->out); | |
f4bba25b SP |
817 | } |
818 | ||
1568fea0 | 819 | if (cmd->dir && chdir(cmd->dir)) |
79319b19 | 820 | child_die(CHILD_ERR_CHDIR); |
3967e25b | 821 | |
45afb1ca EW |
822 | /* |
823 | * restore default signal handlers here, in case | |
824 | * we catch a signal right before execve below | |
825 | */ | |
826 | for (sig = 1; sig < NSIG; sig++) { | |
827 | /* ignored signals get reset to SIG_DFL on execve */ | |
828 | if (signal(sig, SIG_DFL) == SIG_IGN) | |
829 | signal(sig, SIG_IGN); | |
830 | } | |
831 | ||
832 | if (sigprocmask(SIG_SETMASK, &as.old, NULL) != 0) | |
833 | child_die(CHILD_ERR_SIGPROCMASK); | |
834 | ||
e3a43446 BW |
835 | /* |
836 | * Attempt to exec using the command and arguments starting at | |
837 | * argv.argv[1]. argv.argv[0] contains SHELL_PATH which will | |
838 | * be used in the event exec failed with ENOEXEC at which point | |
839 | * we will try to interpret the command using 'sh'. | |
840 | */ | |
d70a9eb6 | 841 | execve(argv.v[1], (char *const *) argv.v + 1, |
ae25394b | 842 | (char *const *) childenv); |
e3a43446 | 843 | if (errno == ENOEXEC) |
d70a9eb6 | 844 | execve(argv.v[0], (char *const *) argv.v, |
ae25394b | 845 | (char *const *) childenv); |
3967e25b | 846 | |
fc1b56f0 | 847 | if (errno == ENOENT) { |
79319b19 BW |
848 | if (cmd->silent_exec_failure) |
849 | child_die(CHILD_ERR_SILENT); | |
850 | child_die(CHILD_ERR_ENOENT); | |
fc1b56f0 | 851 | } else { |
79319b19 | 852 | child_die(CHILD_ERR_ERRNO); |
fc1b56f0 | 853 | } |
b1bf95bb | 854 | } |
45afb1ca | 855 | atfork_parent(&as); |
0ac77ec3 | 856 | if (cmd->pid < 0) |
d3b21597 | 857 | error_errno("cannot fork() for %s", cmd->args.v[0]); |
afe19ff7 | 858 | else if (cmd->clean_on_exit) |
ac2fbaa6 | 859 | mark_child_for_cleanup(cmd->pid, cmd); |
2b541bf8 JS |
860 | |
861 | /* | |
3967e25b | 862 | * Wait for child's exec. If the exec succeeds (or if fork() |
2b541bf8 | 863 | * failed), EOF is seen immediately by the parent. Otherwise, the |
79319b19 | 864 | * child process sends a child_err struct. |
2b541bf8 JS |
865 | * Note that use of this infrastructure is completely advisory, |
866 | * therefore, we keep error checks minimal. | |
867 | */ | |
868 | close(notify_pipe[1]); | |
79319b19 | 869 | if (xread(notify_pipe[0], &cerr, sizeof(cerr)) == sizeof(cerr)) { |
2b541bf8 | 870 | /* |
3967e25b | 871 | * At this point we know that fork() succeeded, but exec() |
2b541bf8 JS |
872 | * failed. Errors have been reported to our stderr. |
873 | */ | |
d3b21597 | 874 | wait_or_whine(cmd->pid, cmd->args.v[0], 0); |
79319b19 | 875 | child_err_spew(cmd, &cerr); |
2b541bf8 JS |
876 | failed_errno = errno; |
877 | cmd->pid = -1; | |
878 | } | |
879 | close(notify_pipe[0]); | |
3967e25b | 880 | |
db015a28 BW |
881 | if (null_fd >= 0) |
882 | close(null_fd); | |
c972bf4c | 883 | strvec_clear(&argv); |
ae25394b | 884 | free(childenv); |
2b541bf8 | 885 | } |
321fd823 JK |
886 | end_of_spawn: |
887 | ||
ba26f296 | 888 | #else |
0d30ad71 | 889 | { |
75301f90 | 890 | int fhin = 0, fhout = 1, fherr = 2; |
d3b21597 | 891 | const char **sargv = cmd->args.v; |
c972bf4c | 892 | struct strvec nargv = STRVEC_INIT; |
ba26f296 | 893 | |
75301f90 JS |
894 | if (cmd->no_stdin) |
895 | fhin = open("/dev/null", O_RDWR); | |
896 | else if (need_in) | |
897 | fhin = dup(fdin[0]); | |
898 | else if (cmd->in) | |
899 | fhin = dup(cmd->in); | |
900 | ||
901 | if (cmd->no_stderr) | |
902 | fherr = open("/dev/null", O_RDWR); | |
903 | else if (need_err) | |
904 | fherr = dup(fderr[1]); | |
76d44c8c JH |
905 | else if (cmd->err > 2) |
906 | fherr = dup(cmd->err); | |
75301f90 JS |
907 | |
908 | if (cmd->no_stdout) | |
909 | fhout = open("/dev/null", O_RDWR); | |
910 | else if (cmd->stdout_to_stderr) | |
911 | fhout = dup(fherr); | |
912 | else if (need_out) | |
913 | fhout = dup(fdout[1]); | |
914 | else if (cmd->out > 1) | |
915 | fhout = dup(cmd->out); | |
ba26f296 | 916 | |
5a50085c | 917 | if (cmd->git_cmd) |
d3b21597 | 918 | cmd->args.v = prepare_git_cmd(&nargv, sargv); |
5a50085c | 919 | else if (cmd->use_shell) |
d3b21597 | 920 | cmd->args.v = prepare_shell_cmd(&nargv, sargv); |
ba26f296 | 921 | |
29fda24d ÆAB |
922 | cmd->pid = mingw_spawnvpe(cmd->args.v[0], cmd->args.v, |
923 | (char**) cmd->env.v, | |
924 | cmd->dir, fhin, fhout, fherr); | |
0ac77ec3 | 925 | failed_errno = errno; |
c024beb5 | 926 | if (cmd->pid < 0 && (!cmd->silent_exec_failure || errno != ENOENT)) |
d3b21597 | 927 | error_errno("cannot spawn %s", cmd->args.v[0]); |
afe19ff7 | 928 | if (cmd->clean_on_exit && cmd->pid >= 0) |
ac2fbaa6 | 929 | mark_child_for_cleanup(cmd->pid, cmd); |
ba26f296 | 930 | |
c972bf4c | 931 | strvec_clear(&nargv); |
d3b21597 | 932 | cmd->args.v = sargv; |
75301f90 JS |
933 | if (fhin != 0) |
934 | close(fhin); | |
935 | if (fhout != 1) | |
936 | close(fhout); | |
937 | if (fherr != 2) | |
938 | close(fherr); | |
0d30ad71 | 939 | } |
ba26f296 JS |
940 | #endif |
941 | ||
942 | if (cmd->pid < 0) { | |
ee4512ed JH |
943 | trace2_child_exit(cmd, -1); |
944 | ||
ba26f296 JS |
945 | if (need_in) |
946 | close_pair(fdin); | |
947 | else if (cmd->in) | |
948 | close(cmd->in); | |
949 | if (need_out) | |
950 | close_pair(fdout); | |
951 | else if (cmd->out) | |
952 | close(cmd->out); | |
953 | if (need_err) | |
954 | close_pair(fderr); | |
fc012c28 D |
955 | else if (cmd->err) |
956 | close(cmd->err); | |
2d71608e | 957 | child_process_clear(cmd); |
0ac77ec3 JS |
958 | errno = failed_errno; |
959 | return -1; | |
ba26f296 | 960 | } |
4919bf03 SP |
961 | |
962 | if (need_in) | |
963 | close(fdin[0]); | |
964 | else if (cmd->in) | |
965 | close(cmd->in); | |
966 | ||
f4bba25b SP |
967 | if (need_out) |
968 | close(fdout[1]); | |
c20181e3 | 969 | else if (cmd->out) |
f4bba25b SP |
970 | close(cmd->out); |
971 | ||
f3b33f1d JS |
972 | if (need_err) |
973 | close(fderr[1]); | |
4f41b611 SP |
974 | else if (cmd->err) |
975 | close(cmd->err); | |
f3b33f1d | 976 | |
ebcb5d16 SP |
977 | return 0; |
978 | } | |
979 | ||
2d22c208 JS |
980 | int finish_command(struct child_process *cmd) |
981 | { | |
d3b21597 | 982 | int ret = wait_or_whine(cmd->pid, cmd->args.v[0], 0); |
ee4512ed | 983 | trace2_child_exit(cmd, ret); |
2d71608e | 984 | child_process_clear(cmd); |
0d58fef5 | 985 | invalidate_lstat_cache(); |
c460c0ec | 986 | return ret; |
2d22c208 JS |
987 | } |
988 | ||
507d7804 TI |
989 | int finish_command_in_signal(struct child_process *cmd) |
990 | { | |
d3b21597 | 991 | int ret = wait_or_whine(cmd->pid, cmd->args.v[0], 1); |
ce3986bb JS |
992 | if (ret != -1) |
993 | trace2_child_exit(cmd, ret); | |
ee4512ed | 994 | return ret; |
507d7804 TI |
995 | } |
996 | ||
997 | ||
ebcb5d16 SP |
998 | int run_command(struct child_process *cmd) |
999 | { | |
c29b3962 JK |
1000 | int code; |
1001 | ||
1002 | if (cmd->out < 0 || cmd->err < 0) | |
033abf97 | 1003 | BUG("run_command with a pipe can cause deadlock"); |
c29b3962 JK |
1004 | |
1005 | code = start_command(cmd); | |
ebcb5d16 SP |
1006 | if (code) |
1007 | return code; | |
1008 | return finish_command(cmd); | |
1009 | } | |
1010 | ||
f6b60983 | 1011 | #ifndef NO_PTHREADS |
0ea1c89b JS |
1012 | static pthread_t main_thread; |
1013 | static int main_thread_set; | |
1014 | static pthread_key_t async_key; | |
1ece66bc | 1015 | static pthread_key_t async_die_counter; |
0ea1c89b | 1016 | |
200a76b7 | 1017 | static void *run_thread(void *data) |
618ebe9f JS |
1018 | { |
1019 | struct async *async = data; | |
f6b60983 | 1020 | intptr_t ret; |
0ea1c89b | 1021 | |
c792d7b6 JK |
1022 | if (async->isolate_sigpipe) { |
1023 | sigset_t mask; | |
1024 | sigemptyset(&mask); | |
1025 | sigaddset(&mask, SIGPIPE); | |
786e6761 | 1026 | if (pthread_sigmask(SIG_BLOCK, &mask, NULL)) { |
c792d7b6 JK |
1027 | ret = error("unable to block SIGPIPE in async thread"); |
1028 | return (void *)ret; | |
1029 | } | |
1030 | } | |
1031 | ||
0ea1c89b | 1032 | pthread_setspecific(async_key, async); |
f6b60983 | 1033 | ret = async->proc(async->proc_in, async->proc_out, async->data); |
200a76b7 | 1034 | return (void *)ret; |
618ebe9f | 1035 | } |
0ea1c89b JS |
1036 | |
1037 | static NORETURN void die_async(const char *err, va_list params) | |
1038 | { | |
e081a7c3 ÆAB |
1039 | report_fn die_message_fn = get_die_message_routine(); |
1040 | ||
1041 | die_message_fn(err, params); | |
0ea1c89b | 1042 | |
661a8cf4 | 1043 | if (in_async()) { |
0ea1c89b JS |
1044 | struct async *async = pthread_getspecific(async_key); |
1045 | if (async->proc_in >= 0) | |
1046 | close(async->proc_in); | |
1047 | if (async->proc_out >= 0) | |
1048 | close(async->proc_out); | |
1049 | pthread_exit((void *)128); | |
1050 | } | |
1051 | ||
1052 | exit(128); | |
618ebe9f | 1053 | } |
1ece66bc JK |
1054 | |
1055 | static int async_die_is_recursing(void) | |
1056 | { | |
1057 | void *ret = pthread_getspecific(async_die_counter); | |
4b540cf9 | 1058 | pthread_setspecific(async_die_counter, &async_die_counter); /* set to any non-NULL valid pointer */ |
1ece66bc JK |
1059 | return ret != NULL; |
1060 | } | |
1061 | ||
661a8cf4 JK |
1062 | int in_async(void) |
1063 | { | |
1064 | if (!main_thread_set) | |
1065 | return 0; /* no asyncs started yet */ | |
1066 | return !pthread_equal(main_thread, pthread_self()); | |
1067 | } | |
1068 | ||
b992fe10 | 1069 | static void NORETURN async_exit(int code) |
9658846c JK |
1070 | { |
1071 | pthread_exit((void *)(intptr_t)code); | |
1072 | } | |
1073 | ||
0f4b6db3 EB |
1074 | #else |
1075 | ||
1076 | static struct { | |
1077 | void (**handlers)(void); | |
1078 | size_t nr; | |
1079 | size_t alloc; | |
1080 | } git_atexit_hdlrs; | |
1081 | ||
1082 | static int git_atexit_installed; | |
1083 | ||
6066a7ea | 1084 | static void git_atexit_dispatch(void) |
0f4b6db3 EB |
1085 | { |
1086 | size_t i; | |
1087 | ||
1088 | for (i=git_atexit_hdlrs.nr ; i ; i--) | |
1089 | git_atexit_hdlrs.handlers[i-1](); | |
1090 | } | |
1091 | ||
6066a7ea | 1092 | static void git_atexit_clear(void) |
0f4b6db3 EB |
1093 | { |
1094 | free(git_atexit_hdlrs.handlers); | |
1095 | memset(&git_atexit_hdlrs, 0, sizeof(git_atexit_hdlrs)); | |
1096 | git_atexit_installed = 0; | |
1097 | } | |
1098 | ||
1099 | #undef atexit | |
1100 | int git_atexit(void (*handler)(void)) | |
1101 | { | |
1102 | ALLOC_GROW(git_atexit_hdlrs.handlers, git_atexit_hdlrs.nr + 1, git_atexit_hdlrs.alloc); | |
1103 | git_atexit_hdlrs.handlers[git_atexit_hdlrs.nr++] = handler; | |
1104 | if (!git_atexit_installed) { | |
1105 | if (atexit(&git_atexit_dispatch)) | |
1106 | return -1; | |
1107 | git_atexit_installed = 1; | |
1108 | } | |
1109 | return 0; | |
1110 | } | |
1111 | #define atexit git_atexit | |
1112 | ||
661a8cf4 JK |
1113 | static int process_is_async; |
1114 | int in_async(void) | |
1115 | { | |
1116 | return process_is_async; | |
1117 | } | |
1118 | ||
b992fe10 | 1119 | static void NORETURN async_exit(int code) |
9658846c JK |
1120 | { |
1121 | exit(code); | |
1122 | } | |
1123 | ||
618ebe9f JS |
1124 | #endif |
1125 | ||
b992fe10 LS |
1126 | void check_pipe(int err) |
1127 | { | |
1128 | if (err == EPIPE) { | |
1129 | if (in_async()) | |
1130 | async_exit(141); | |
1131 | ||
1132 | signal(SIGPIPE, SIG_DFL); | |
1133 | raise(SIGPIPE); | |
1134 | /* Should never happen, but just in case... */ | |
1135 | exit(141); | |
1136 | } | |
1137 | } | |
1138 | ||
2d22c208 JS |
1139 | int start_async(struct async *async) |
1140 | { | |
ae6a5609 EFL |
1141 | int need_in, need_out; |
1142 | int fdin[2], fdout[2]; | |
1143 | int proc_in, proc_out; | |
2d22c208 | 1144 | |
ae6a5609 EFL |
1145 | need_in = async->in < 0; |
1146 | if (need_in) { | |
1147 | if (pipe(fdin) < 0) { | |
1148 | if (async->out > 0) | |
1149 | close(async->out); | |
fbcb0e06 | 1150 | return error_errno("cannot create pipe"); |
ae6a5609 EFL |
1151 | } |
1152 | async->in = fdin[1]; | |
1153 | } | |
1154 | ||
1155 | need_out = async->out < 0; | |
1156 | if (need_out) { | |
1157 | if (pipe(fdout) < 0) { | |
1158 | if (need_in) | |
1159 | close_pair(fdin); | |
1160 | else if (async->in) | |
1161 | close(async->in); | |
fbcb0e06 | 1162 | return error_errno("cannot create pipe"); |
ae6a5609 EFL |
1163 | } |
1164 | async->out = fdout[0]; | |
1165 | } | |
1166 | ||
1167 | if (need_in) | |
1168 | proc_in = fdin[0]; | |
1169 | else if (async->in) | |
1170 | proc_in = async->in; | |
1171 | else | |
1172 | proc_in = -1; | |
1173 | ||
1174 | if (need_out) | |
1175 | proc_out = fdout[1]; | |
1176 | else if (async->out) | |
1177 | proc_out = async->out; | |
1178 | else | |
1179 | proc_out = -1; | |
2d22c208 | 1180 | |
f6b60983 | 1181 | #ifdef NO_PTHREADS |
2c3766f0 AM |
1182 | /* Flush stdio before fork() to avoid cloning buffers */ |
1183 | fflush(NULL); | |
1184 | ||
2d22c208 JS |
1185 | async->pid = fork(); |
1186 | if (async->pid < 0) { | |
fbcb0e06 | 1187 | error_errno("fork (async) failed"); |
ae6a5609 | 1188 | goto error; |
2d22c208 JS |
1189 | } |
1190 | if (!async->pid) { | |
ae6a5609 EFL |
1191 | if (need_in) |
1192 | close(fdin[1]); | |
1193 | if (need_out) | |
1194 | close(fdout[0]); | |
0f4b6db3 | 1195 | git_atexit_clear(); |
661a8cf4 | 1196 | process_is_async = 1; |
ae6a5609 | 1197 | exit(!!async->proc(proc_in, proc_out, async->data)); |
2d22c208 | 1198 | } |
ae6a5609 | 1199 | |
ac2fbaa6 | 1200 | mark_child_for_cleanup(async->pid, NULL); |
afe19ff7 | 1201 | |
ae6a5609 EFL |
1202 | if (need_in) |
1203 | close(fdin[0]); | |
1204 | else if (async->in) | |
1205 | close(async->in); | |
1206 | ||
1207 | if (need_out) | |
1208 | close(fdout[1]); | |
1209 | else if (async->out) | |
1210 | close(async->out); | |
618ebe9f | 1211 | #else |
0ea1c89b JS |
1212 | if (!main_thread_set) { |
1213 | /* | |
1214 | * We assume that the first time that start_async is called | |
1215 | * it is from the main thread. | |
1216 | */ | |
1217 | main_thread_set = 1; | |
1218 | main_thread = pthread_self(); | |
1219 | pthread_key_create(&async_key, NULL); | |
1ece66bc | 1220 | pthread_key_create(&async_die_counter, NULL); |
0ea1c89b | 1221 | set_die_routine(die_async); |
1ece66bc | 1222 | set_die_is_recursing_routine(async_die_is_recursing); |
0ea1c89b JS |
1223 | } |
1224 | ||
200a76b7 JS |
1225 | if (proc_in >= 0) |
1226 | set_cloexec(proc_in); | |
1227 | if (proc_out >= 0) | |
1228 | set_cloexec(proc_out); | |
ae6a5609 EFL |
1229 | async->proc_in = proc_in; |
1230 | async->proc_out = proc_out; | |
200a76b7 JS |
1231 | { |
1232 | int err = pthread_create(&async->tid, NULL, run_thread, async); | |
1233 | if (err) { | |
2179045f | 1234 | error(_("cannot create async thread: %s"), strerror(err)); |
200a76b7 JS |
1235 | goto error; |
1236 | } | |
618ebe9f JS |
1237 | } |
1238 | #endif | |
2d22c208 | 1239 | return 0; |
ae6a5609 EFL |
1240 | |
1241 | error: | |
1242 | if (need_in) | |
1243 | close_pair(fdin); | |
1244 | else if (async->in) | |
1245 | close(async->in); | |
1246 | ||
1247 | if (need_out) | |
1248 | close_pair(fdout); | |
1249 | else if (async->out) | |
1250 | close(async->out); | |
1251 | return -1; | |
2d22c208 JS |
1252 | } |
1253 | ||
1254 | int finish_async(struct async *async) | |
1255 | { | |
f6b60983 | 1256 | #ifdef NO_PTHREADS |
0d58fef5 JS |
1257 | int ret = wait_or_whine(async->pid, "child process", 0); |
1258 | ||
1259 | invalidate_lstat_cache(); | |
1260 | ||
1261 | return ret; | |
618ebe9f | 1262 | #else |
200a76b7 JS |
1263 | void *ret = (void *)(intptr_t)(-1); |
1264 | ||
1265 | if (pthread_join(async->tid, &ret)) | |
1266 | error("pthread_join failed"); | |
0d58fef5 | 1267 | invalidate_lstat_cache(); |
200a76b7 | 1268 | return (int)(intptr_t)ret; |
0d58fef5 | 1269 | |
618ebe9f | 1270 | #endif |
2d22c208 | 1271 | } |
ae98a008 | 1272 | |
c0e40a2d NTND |
1273 | int async_with_fork(void) |
1274 | { | |
1275 | #ifdef NO_PTHREADS | |
1276 | return 1; | |
1277 | #else | |
1278 | return 0; | |
1279 | #endif | |
1280 | } | |
1281 | ||
96335bcf JK |
1282 | struct io_pump { |
1283 | /* initialized by caller */ | |
1284 | int fd; | |
1285 | int type; /* POLLOUT or POLLIN */ | |
1286 | union { | |
1287 | struct { | |
1288 | const char *buf; | |
1289 | size_t len; | |
1290 | } out; | |
1291 | struct { | |
1292 | struct strbuf *buf; | |
1293 | size_t hint; | |
1294 | } in; | |
1295 | } u; | |
1296 | ||
1297 | /* returned by pump_io */ | |
1298 | int error; /* 0 for success, otherwise errno */ | |
1299 | ||
1300 | /* internal use */ | |
1301 | struct pollfd *pfd; | |
1302 | }; | |
1303 | ||
1304 | static int pump_io_round(struct io_pump *slots, int nr, struct pollfd *pfd) | |
1305 | { | |
1306 | int pollsize = 0; | |
1307 | int i; | |
1308 | ||
1309 | for (i = 0; i < nr; i++) { | |
1310 | struct io_pump *io = &slots[i]; | |
1311 | if (io->fd < 0) | |
1312 | continue; | |
1313 | pfd[pollsize].fd = io->fd; | |
1314 | pfd[pollsize].events = io->type; | |
1315 | io->pfd = &pfd[pollsize++]; | |
1316 | } | |
1317 | ||
1318 | if (!pollsize) | |
1319 | return 0; | |
1320 | ||
1321 | if (poll(pfd, pollsize, -1) < 0) { | |
1322 | if (errno == EINTR) | |
1323 | return 1; | |
1324 | die_errno("poll failed"); | |
1325 | } | |
1326 | ||
1327 | for (i = 0; i < nr; i++) { | |
1328 | struct io_pump *io = &slots[i]; | |
1329 | ||
1330 | if (io->fd < 0) | |
1331 | continue; | |
1332 | ||
1333 | if (!(io->pfd->revents & (POLLOUT|POLLIN|POLLHUP|POLLERR|POLLNVAL))) | |
1334 | continue; | |
1335 | ||
1336 | if (io->type == POLLOUT) { | |
14eab817 JK |
1337 | ssize_t len; |
1338 | ||
1339 | /* | |
1340 | * Don't use xwrite() here. It loops forever on EAGAIN, | |
1341 | * and we're in our own poll() loop here. | |
1342 | * | |
1343 | * Note that we lose xwrite()'s handling of MAX_IO_SIZE | |
1344 | * and EINTR, so we have to implement those ourselves. | |
1345 | */ | |
1346 | len = write(io->fd, io->u.out.buf, | |
1347 | io->u.out.len <= MAX_IO_SIZE ? | |
1348 | io->u.out.len : MAX_IO_SIZE); | |
96335bcf | 1349 | if (len < 0) { |
c6d3cce6 JK |
1350 | if (errno != EINTR && errno != EAGAIN && |
1351 | errno != ENOSPC) { | |
14eab817 JK |
1352 | io->error = errno; |
1353 | close(io->fd); | |
1354 | io->fd = -1; | |
1355 | } | |
96335bcf JK |
1356 | } else { |
1357 | io->u.out.buf += len; | |
1358 | io->u.out.len -= len; | |
1359 | if (!io->u.out.len) { | |
1360 | close(io->fd); | |
1361 | io->fd = -1; | |
1362 | } | |
1363 | } | |
1364 | } | |
1365 | ||
1366 | if (io->type == POLLIN) { | |
1367 | ssize_t len = strbuf_read_once(io->u.in.buf, | |
1368 | io->fd, io->u.in.hint); | |
1369 | if (len < 0) | |
1370 | io->error = errno; | |
1371 | if (len <= 0) { | |
1372 | close(io->fd); | |
1373 | io->fd = -1; | |
1374 | } | |
1375 | } | |
1376 | } | |
1377 | ||
1378 | return 1; | |
1379 | } | |
1380 | ||
1381 | static int pump_io(struct io_pump *slots, int nr) | |
911ec99b | 1382 | { |
96335bcf JK |
1383 | struct pollfd *pfd; |
1384 | int i; | |
1385 | ||
1386 | for (i = 0; i < nr; i++) | |
1387 | slots[i].error = 0; | |
1388 | ||
1389 | ALLOC_ARRAY(pfd, nr); | |
1390 | while (pump_io_round(slots, nr, pfd)) | |
1391 | ; /* nothing */ | |
1392 | free(pfd); | |
1393 | ||
1394 | /* There may be multiple errno values, so just pick the first. */ | |
1395 | for (i = 0; i < nr; i++) { | |
1396 | if (slots[i].error) { | |
1397 | errno = slots[i].error; | |
1398 | return -1; | |
1399 | } | |
1400 | } | |
1401 | return 0; | |
1402 | } | |
1403 | ||
1404 | ||
1405 | int pipe_command(struct child_process *cmd, | |
1406 | const char *in, size_t in_len, | |
1407 | struct strbuf *out, size_t out_hint, | |
1408 | struct strbuf *err, size_t err_hint) | |
1409 | { | |
1410 | struct io_pump io[3]; | |
1411 | int nr = 0; | |
1412 | ||
1413 | if (in) | |
1414 | cmd->in = -1; | |
1415 | if (out) | |
1416 | cmd->out = -1; | |
1417 | if (err) | |
1418 | cmd->err = -1; | |
1419 | ||
911ec99b JK |
1420 | if (start_command(cmd) < 0) |
1421 | return -1; | |
1422 | ||
96335bcf | 1423 | if (in) { |
716c1f64 JK |
1424 | if (enable_pipe_nonblock(cmd->in) < 0) { |
1425 | error_errno("unable to make pipe non-blocking"); | |
1426 | close(cmd->in); | |
1427 | if (out) | |
1428 | close(cmd->out); | |
1429 | if (err) | |
1430 | close(cmd->err); | |
1431 | return -1; | |
1432 | } | |
96335bcf JK |
1433 | io[nr].fd = cmd->in; |
1434 | io[nr].type = POLLOUT; | |
1435 | io[nr].u.out.buf = in; | |
1436 | io[nr].u.out.len = in_len; | |
1437 | nr++; | |
1438 | } | |
1439 | if (out) { | |
1440 | io[nr].fd = cmd->out; | |
1441 | io[nr].type = POLLIN; | |
1442 | io[nr].u.in.buf = out; | |
1443 | io[nr].u.in.hint = out_hint; | |
1444 | nr++; | |
1445 | } | |
1446 | if (err) { | |
1447 | io[nr].fd = cmd->err; | |
1448 | io[nr].type = POLLIN; | |
1449 | io[nr].u.in.buf = err; | |
1450 | io[nr].u.in.hint = err_hint; | |
1451 | nr++; | |
1452 | } | |
1453 | ||
1454 | if (pump_io(io, nr) < 0) { | |
911ec99b JK |
1455 | finish_command(cmd); /* throw away exit code */ |
1456 | return -1; | |
1457 | } | |
1458 | ||
911ec99b JK |
1459 | return finish_command(cmd); |
1460 | } | |
c553c72e SB |
1461 | |
1462 | enum child_state { | |
1463 | GIT_CP_FREE, | |
1464 | GIT_CP_WORKING, | |
1465 | GIT_CP_WAIT_CLEANUP, | |
1466 | }; | |
1467 | ||
1468 | struct parallel_processes { | |
6a48b428 | 1469 | size_t nr_processes; |
c553c72e | 1470 | |
c553c72e SB |
1471 | struct { |
1472 | enum child_state state; | |
1473 | struct child_process process; | |
1474 | struct strbuf err; | |
1475 | void *data; | |
1476 | } *children; | |
1477 | /* | |
1478 | * The struct pollfd is logically part of *children, | |
1479 | * but the system call expects it as its own array. | |
1480 | */ | |
1481 | struct pollfd *pfd; | |
1482 | ||
1483 | unsigned shutdown : 1; | |
1484 | ||
6a48b428 | 1485 | size_t output_owner; |
c553c72e SB |
1486 | struct strbuf buffered_output; /* of finished children */ |
1487 | }; | |
1488 | ||
0b0ab95f ÆAB |
1489 | struct parallel_processes_for_signal { |
1490 | const struct run_process_parallel_opts *opts; | |
1491 | const struct parallel_processes *pp; | |
1492 | }; | |
1493 | ||
1494 | static void kill_children(const struct parallel_processes *pp, | |
1495 | const struct run_process_parallel_opts *opts, | |
1496 | int signo) | |
c553c72e | 1497 | { |
0b0ab95f | 1498 | for (size_t i = 0; i < opts->processes; i++) |
c553c72e SB |
1499 | if (pp->children[i].state == GIT_CP_WORKING) |
1500 | kill(pp->children[i].process.pid, signo); | |
1501 | } | |
1502 | ||
0b0ab95f ÆAB |
1503 | static void kill_children_signal(const struct parallel_processes_for_signal *pp_sig, |
1504 | int signo) | |
1505 | { | |
1506 | kill_children(pp_sig->pp, pp_sig->opts, signo); | |
1507 | } | |
1508 | ||
1509 | static struct parallel_processes_for_signal *pp_for_signal; | |
c553c72e SB |
1510 | |
1511 | static void handle_children_on_signal(int signo) | |
1512 | { | |
0b0ab95f | 1513 | kill_children_signal(pp_for_signal, signo); |
c553c72e SB |
1514 | sigchain_pop(signo); |
1515 | raise(signo); | |
1516 | } | |
1517 | ||
1518 | static void pp_init(struct parallel_processes *pp, | |
0b0ab95f ÆAB |
1519 | const struct run_process_parallel_opts *opts, |
1520 | struct parallel_processes_for_signal *pp_sig) | |
c553c72e | 1521 | { |
6e5ba0ba | 1522 | const size_t n = opts->processes; |
c333e6f3 | 1523 | |
51243f9f ÆAB |
1524 | if (!n) |
1525 | BUG("you must provide a non-zero number of processes!"); | |
c553c72e | 1526 | |
6a48b428 ÆAB |
1527 | trace_printf("run_processes_parallel: preparing to run up to %"PRIuMAX" tasks", |
1528 | (uintmax_t)n); | |
c553c72e | 1529 | |
fa93951d | 1530 | if (!opts->get_next_task) |
033abf97 | 1531 | BUG("you need to specify a get_next_task function"); |
c553c72e | 1532 | |
ca56dadb | 1533 | CALLOC_ARRAY(pp->children, n); |
357f8e6e | 1534 | if (!opts->ungroup) |
fd3aaf53 | 1535 | CALLOC_ARRAY(pp->pfd, n); |
c553c72e | 1536 | |
6a48b428 | 1537 | for (size_t i = 0; i < n; i++) { |
c553c72e SB |
1538 | strbuf_init(&pp->children[i].err, 0); |
1539 | child_process_init(&pp->children[i].process); | |
fd3aaf53 ÆAB |
1540 | if (pp->pfd) { |
1541 | pp->pfd[i].events = POLLIN | POLLHUP; | |
1542 | pp->pfd[i].fd = -1; | |
1543 | } | |
c553c72e SB |
1544 | } |
1545 | ||
0b0ab95f ÆAB |
1546 | pp_sig->pp = pp; |
1547 | pp_sig->opts = opts; | |
1548 | pp_for_signal = pp_sig; | |
c553c72e SB |
1549 | sigchain_push_common(handle_children_on_signal); |
1550 | } | |
1551 | ||
d1610eef ÆAB |
1552 | static void pp_cleanup(struct parallel_processes *pp, |
1553 | const struct run_process_parallel_opts *opts) | |
c553c72e | 1554 | { |
c553c72e | 1555 | trace_printf("run_processes_parallel: done"); |
d1610eef | 1556 | for (size_t i = 0; i < opts->processes; i++) { |
c553c72e SB |
1557 | strbuf_release(&pp->children[i].err); |
1558 | child_process_clear(&pp->children[i].process); | |
1559 | } | |
1560 | ||
1561 | free(pp->children); | |
1562 | free(pp->pfd); | |
1563 | ||
1564 | /* | |
1565 | * When get_next_task added messages to the buffer in its last | |
1566 | * iteration, the buffered output is non empty. | |
1567 | */ | |
2dac9b56 | 1568 | strbuf_write(&pp->buffered_output, stderr); |
c553c72e SB |
1569 | strbuf_release(&pp->buffered_output); |
1570 | ||
1571 | sigchain_pop_common(); | |
1572 | } | |
1573 | ||
1574 | /* returns | |
1575 | * 0 if a new task was started. | |
1576 | * 1 if no new jobs was started (get_next_task ran out of work, non critical | |
1577 | * problem with starting a new command) | |
1578 | * <0 no new job was started, user wishes to shutdown early. Use negative code | |
1579 | * to signal the children. | |
1580 | */ | |
fa93951d ÆAB |
1581 | static int pp_start_one(struct parallel_processes *pp, |
1582 | const struct run_process_parallel_opts *opts) | |
c553c72e | 1583 | { |
6a48b428 ÆAB |
1584 | size_t i; |
1585 | int code; | |
c553c72e | 1586 | |
9f3df6c0 | 1587 | for (i = 0; i < opts->processes; i++) |
c553c72e SB |
1588 | if (pp->children[i].state == GIT_CP_FREE) |
1589 | break; | |
9f3df6c0 | 1590 | if (i == opts->processes) |
033abf97 | 1591 | BUG("bookkeeping is hard"); |
c553c72e | 1592 | |
54026730 ES |
1593 | /* |
1594 | * By default, do not inherit stdin from the parent process - otherwise, | |
1595 | * all children would share stdin! Users may overwrite this to provide | |
1596 | * something to the child's stdin by having their 'get_next_task' | |
1597 | * callback assign 0 to .no_stdin and an appropriate integer to .in. | |
1598 | */ | |
1599 | pp->children[i].process.no_stdin = 1; | |
1600 | ||
fa93951d | 1601 | code = opts->get_next_task(&pp->children[i].process, |
357f8e6e | 1602 | opts->ungroup ? NULL : &pp->children[i].err, |
2aa8d225 | 1603 | opts->data, |
fa93951d | 1604 | &pp->children[i].data); |
c553c72e | 1605 | if (!code) { |
357f8e6e | 1606 | if (!opts->ungroup) { |
fd3aaf53 ÆAB |
1607 | strbuf_addbuf(&pp->buffered_output, &pp->children[i].err); |
1608 | strbuf_reset(&pp->children[i].err); | |
1609 | } | |
c553c72e SB |
1610 | return 1; |
1611 | } | |
357f8e6e | 1612 | if (!opts->ungroup) { |
fd3aaf53 ÆAB |
1613 | pp->children[i].process.err = -1; |
1614 | pp->children[i].process.stdout_to_stderr = 1; | |
1615 | } | |
c553c72e SB |
1616 | |
1617 | if (start_command(&pp->children[i].process)) { | |
fa93951d | 1618 | if (opts->start_failure) |
357f8e6e | 1619 | code = opts->start_failure(opts->ungroup ? NULL : |
fa93951d | 1620 | &pp->children[i].err, |
2aa8d225 | 1621 | opts->data, |
fa93951d ÆAB |
1622 | pp->children[i].data); |
1623 | else | |
1624 | code = 0; | |
1625 | ||
357f8e6e | 1626 | if (!opts->ungroup) { |
fd3aaf53 ÆAB |
1627 | strbuf_addbuf(&pp->buffered_output, &pp->children[i].err); |
1628 | strbuf_reset(&pp->children[i].err); | |
1629 | } | |
c553c72e SB |
1630 | if (code) |
1631 | pp->shutdown = 1; | |
1632 | return code; | |
1633 | } | |
1634 | ||
1635 | pp->nr_processes++; | |
1636 | pp->children[i].state = GIT_CP_WORKING; | |
fd3aaf53 ÆAB |
1637 | if (pp->pfd) |
1638 | pp->pfd[i].fd = pp->children[i].process.err; | |
c553c72e SB |
1639 | return 0; |
1640 | } | |
1641 | ||
d1610eef ÆAB |
1642 | static void pp_buffer_stderr(struct parallel_processes *pp, |
1643 | const struct run_process_parallel_opts *opts, | |
1644 | int output_timeout) | |
c553c72e | 1645 | { |
5123e6e7 | 1646 | while (poll(pp->pfd, opts->processes, output_timeout) < 0) { |
c553c72e SB |
1647 | if (errno == EINTR) |
1648 | continue; | |
d1610eef | 1649 | pp_cleanup(pp, opts); |
c553c72e SB |
1650 | die_errno("poll"); |
1651 | } | |
1652 | ||
1653 | /* Buffer output from all pipes. */ | |
d1610eef | 1654 | for (size_t i = 0; i < opts->processes; i++) { |
c553c72e SB |
1655 | if (pp->children[i].state == GIT_CP_WORKING && |
1656 | pp->pfd[i].revents & (POLLIN | POLLHUP)) { | |
1657 | int n = strbuf_read_once(&pp->children[i].err, | |
1658 | pp->children[i].process.err, 0); | |
1659 | if (n == 0) { | |
1660 | close(pp->children[i].process.err); | |
1661 | pp->children[i].state = GIT_CP_WAIT_CLEANUP; | |
1662 | } else if (n < 0) | |
1663 | if (errno != EAGAIN) | |
1664 | die_errno("read"); | |
1665 | } | |
1666 | } | |
1667 | } | |
1668 | ||
e39c9de8 | 1669 | static void pp_output(const struct parallel_processes *pp) |
c553c72e | 1670 | { |
6a48b428 | 1671 | size_t i = pp->output_owner; |
fd3aaf53 | 1672 | |
c553c72e SB |
1673 | if (pp->children[i].state == GIT_CP_WORKING && |
1674 | pp->children[i].err.len) { | |
2dac9b56 | 1675 | strbuf_write(&pp->children[i].err, stderr); |
c553c72e SB |
1676 | strbuf_reset(&pp->children[i].err); |
1677 | } | |
1678 | } | |
1679 | ||
fa93951d ÆAB |
1680 | static int pp_collect_finished(struct parallel_processes *pp, |
1681 | const struct run_process_parallel_opts *opts) | |
c553c72e | 1682 | { |
6a48b428 | 1683 | int code; |
9f3df6c0 | 1684 | size_t i; |
c553c72e SB |
1685 | int result = 0; |
1686 | ||
1687 | while (pp->nr_processes > 0) { | |
9f3df6c0 | 1688 | for (i = 0; i < opts->processes; i++) |
c553c72e SB |
1689 | if (pp->children[i].state == GIT_CP_WAIT_CLEANUP) |
1690 | break; | |
9f3df6c0 | 1691 | if (i == opts->processes) |
c553c72e SB |
1692 | break; |
1693 | ||
1694 | code = finish_command(&pp->children[i].process); | |
1695 | ||
fa93951d | 1696 | if (opts->task_finished) |
357f8e6e | 1697 | code = opts->task_finished(code, opts->ungroup ? NULL : |
2aa8d225 | 1698 | &pp->children[i].err, opts->data, |
fa93951d ÆAB |
1699 | pp->children[i].data); |
1700 | else | |
1701 | code = 0; | |
c553c72e SB |
1702 | |
1703 | if (code) | |
1704 | result = code; | |
1705 | if (code < 0) | |
1706 | break; | |
1707 | ||
1708 | pp->nr_processes--; | |
1709 | pp->children[i].state = GIT_CP_FREE; | |
fd3aaf53 ÆAB |
1710 | if (pp->pfd) |
1711 | pp->pfd[i].fd = -1; | |
c553c72e SB |
1712 | child_process_init(&pp->children[i].process); |
1713 | ||
357f8e6e | 1714 | if (opts->ungroup) { |
fd3aaf53 ÆAB |
1715 | ; /* no strbuf_*() work to do here */ |
1716 | } else if (i != pp->output_owner) { | |
c553c72e SB |
1717 | strbuf_addbuf(&pp->buffered_output, &pp->children[i].err); |
1718 | strbuf_reset(&pp->children[i].err); | |
1719 | } else { | |
9f3df6c0 ÆAB |
1720 | const size_t n = opts->processes; |
1721 | ||
2dac9b56 | 1722 | strbuf_write(&pp->children[i].err, stderr); |
c553c72e SB |
1723 | strbuf_reset(&pp->children[i].err); |
1724 | ||
1725 | /* Output all other finished child processes */ | |
2dac9b56 | 1726 | strbuf_write(&pp->buffered_output, stderr); |
c553c72e SB |
1727 | strbuf_reset(&pp->buffered_output); |
1728 | ||
1729 | /* | |
1730 | * Pick next process to output live. | |
1731 | * NEEDSWORK: | |
1732 | * For now we pick it randomly by doing a round | |
1733 | * robin. Later we may want to pick the one with | |
1734 | * the most output or the longest or shortest | |
1735 | * running process time. | |
1736 | */ | |
1737 | for (i = 0; i < n; i++) | |
1738 | if (pp->children[(pp->output_owner + i) % n].state == GIT_CP_WORKING) | |
1739 | break; | |
1740 | pp->output_owner = (pp->output_owner + i) % n; | |
1741 | } | |
1742 | } | |
1743 | return result; | |
1744 | } | |
1745 | ||
6e5ba0ba | 1746 | void run_processes_parallel(const struct run_process_parallel_opts *opts) |
c553c72e SB |
1747 | { |
1748 | int i, code; | |
1749 | int output_timeout = 100; | |
1750 | int spawn_cap = 4; | |
0b0ab95f | 1751 | struct parallel_processes_for_signal pp_sig; |
c333e6f3 | 1752 | struct parallel_processes pp = { |
c333e6f3 | 1753 | .buffered_output = STRBUF_INIT, |
c333e6f3 | 1754 | }; |
6e5ba0ba ÆAB |
1755 | /* options */ |
1756 | const char *tr2_category = opts->tr2_category; | |
1757 | const char *tr2_label = opts->tr2_label; | |
1758 | const int do_trace2 = tr2_category && tr2_label; | |
c553c72e | 1759 | |
6e5ba0ba ÆAB |
1760 | if (do_trace2) |
1761 | trace2_region_enter_printf(tr2_category, tr2_label, NULL, | |
1762 | "max:%d", opts->processes); | |
fd3aaf53 | 1763 | |
0b0ab95f | 1764 | pp_init(&pp, opts, &pp_sig); |
c553c72e SB |
1765 | while (1) { |
1766 | for (i = 0; | |
1767 | i < spawn_cap && !pp.shutdown && | |
9f3df6c0 | 1768 | pp.nr_processes < opts->processes; |
c553c72e | 1769 | i++) { |
fa93951d | 1770 | code = pp_start_one(&pp, opts); |
c553c72e SB |
1771 | if (!code) |
1772 | continue; | |
1773 | if (code < 0) { | |
1774 | pp.shutdown = 1; | |
0b0ab95f | 1775 | kill_children(&pp, opts, -code); |
c553c72e SB |
1776 | } |
1777 | break; | |
1778 | } | |
1779 | if (!pp.nr_processes) | |
1780 | break; | |
6e5ba0ba | 1781 | if (opts->ungroup) { |
9f3df6c0 | 1782 | for (size_t i = 0; i < opts->processes; i++) |
fd3aaf53 ÆAB |
1783 | pp.children[i].state = GIT_CP_WAIT_CLEANUP; |
1784 | } else { | |
d1610eef | 1785 | pp_buffer_stderr(&pp, opts, output_timeout); |
fd3aaf53 ÆAB |
1786 | pp_output(&pp); |
1787 | } | |
fa93951d | 1788 | code = pp_collect_finished(&pp, opts); |
c553c72e SB |
1789 | if (code) { |
1790 | pp.shutdown = 1; | |
1791 | if (code < 0) | |
0b0ab95f | 1792 | kill_children(&pp, opts,-code); |
c553c72e SB |
1793 | } |
1794 | } | |
1795 | ||
d1610eef | 1796 | pp_cleanup(&pp, opts); |
6e5ba0ba ÆAB |
1797 | |
1798 | if (do_trace2) | |
1799 | trace2_region_leave(tr2_category, tr2_label, NULL); | |
c553c72e | 1800 | } |
ee4512ed | 1801 | |
a95ce124 | 1802 | int run_auto_maintenance(int quiet) |
850b6ede | 1803 | { |
1942d483 | 1804 | int enabled; |
a95ce124 | 1805 | struct child_process maint = CHILD_PROCESS_INIT; |
850b6ede | 1806 | |
1942d483 DS |
1807 | if (!git_config_get_bool("maintenance.auto", &enabled) && |
1808 | !enabled) | |
1809 | return 0; | |
1810 | ||
a95ce124 | 1811 | maint.git_cmd = 1; |
5a22a334 | 1812 | maint.close_object_store = 1; |
a95ce124 DS |
1813 | strvec_pushl(&maint.args, "maintenance", "run", "--auto", NULL); |
1814 | strvec_push(&maint.args, quiet ? "--quiet" : "--no-quiet"); | |
1815 | ||
1816 | return run_command(&maint); | |
850b6ede | 1817 | } |
d1fa9435 | 1818 | |
b3193252 | 1819 | void prepare_other_repo_env(struct strvec *env, const char *new_git_dir) |
d1fa9435 JT |
1820 | { |
1821 | const char * const *var; | |
1822 | ||
1823 | for (var = local_repo_env; *var; var++) { | |
1824 | if (strcmp(*var, CONFIG_DATA_ENVIRONMENT) && | |
1825 | strcmp(*var, CONFIG_COUNT_ENVIRONMENT)) | |
b3193252 | 1826 | strvec_push(env, *var); |
d1fa9435 | 1827 | } |
b3193252 | 1828 | strvec_pushf(env, "%s=%s", GIT_DIR_ENVIRONMENT, new_git_dir); |
d1fa9435 | 1829 | } |
fdb13226 JH |
1830 | |
1831 | enum start_bg_result start_bg_command(struct child_process *cmd, | |
1832 | start_bg_wait_cb *wait_cb, | |
1833 | void *cb_data, | |
1834 | unsigned int timeout_sec) | |
1835 | { | |
1836 | enum start_bg_result sbgr = SBGR_ERROR; | |
1837 | int ret; | |
1838 | int wait_status; | |
1839 | pid_t pid_seen; | |
1840 | time_t time_limit; | |
1841 | ||
1842 | /* | |
1843 | * We do not allow clean-on-exit because the child process | |
1844 | * should persist in the background and possibly/probably | |
1845 | * after this process exits. So we don't want to kill the | |
1846 | * child during our atexit routine. | |
1847 | */ | |
1848 | if (cmd->clean_on_exit) | |
1849 | BUG("start_bg_command() does not allow non-zero clean_on_exit"); | |
1850 | ||
1851 | if (!cmd->trace2_child_class) | |
1852 | cmd->trace2_child_class = "background"; | |
1853 | ||
1854 | ret = start_command(cmd); | |
1855 | if (ret) { | |
1856 | /* | |
1857 | * We assume that if `start_command()` fails, we | |
1858 | * either get a complete `trace2_child_start() / | |
1859 | * trace2_child_exit()` pair or it fails before the | |
1860 | * `trace2_child_start()` is emitted, so we do not | |
1861 | * need to worry about it here. | |
1862 | * | |
1863 | * We also assume that `start_command()` does not add | |
1864 | * us to the cleanup list. And that it calls | |
b39a8418 | 1865 | * `child_process_clear()`. |
fdb13226 JH |
1866 | */ |
1867 | sbgr = SBGR_ERROR; | |
1868 | goto done; | |
1869 | } | |
1870 | ||
1871 | time(&time_limit); | |
1872 | time_limit += timeout_sec; | |
1873 | ||
1874 | wait: | |
1875 | pid_seen = waitpid(cmd->pid, &wait_status, WNOHANG); | |
1876 | ||
1877 | if (!pid_seen) { | |
1878 | /* | |
1879 | * The child is currently running. Ask the callback | |
1880 | * if the child is ready to do work or whether we | |
1881 | * should keep waiting for it to boot up. | |
1882 | */ | |
1883 | ret = (*wait_cb)(cmd, cb_data); | |
1884 | if (!ret) { | |
1885 | /* | |
1886 | * The child is running and "ready". | |
1887 | */ | |
1888 | trace2_child_ready(cmd, "ready"); | |
1889 | sbgr = SBGR_READY; | |
1890 | goto done; | |
1891 | } else if (ret > 0) { | |
1892 | /* | |
1893 | * The callback said to give it more time to boot up | |
1894 | * (subject to our timeout limit). | |
1895 | */ | |
1896 | time_t now; | |
1897 | ||
1898 | time(&now); | |
1899 | if (now < time_limit) | |
1900 | goto wait; | |
1901 | ||
1902 | /* | |
1903 | * Our timeout has expired. We don't try to | |
1904 | * kill the child, but rather let it continue | |
1905 | * (hopefully) trying to startup. | |
1906 | */ | |
1907 | trace2_child_ready(cmd, "timeout"); | |
1908 | sbgr = SBGR_TIMEOUT; | |
1909 | goto done; | |
1910 | } else { | |
1911 | /* | |
1912 | * The cb gave up on this child. It is still running, | |
1913 | * but our cb got an error trying to probe it. | |
1914 | */ | |
1915 | trace2_child_ready(cmd, "error"); | |
1916 | sbgr = SBGR_CB_ERROR; | |
1917 | goto done; | |
1918 | } | |
1919 | } | |
1920 | ||
1921 | else if (pid_seen == cmd->pid) { | |
1922 | int child_code = -1; | |
1923 | ||
1924 | /* | |
1925 | * The child started, but exited or was terminated | |
1926 | * before becoming "ready". | |
1927 | * | |
1928 | * We try to match the behavior of `wait_or_whine()` | |
1929 | * WRT the handling of WIFSIGNALED() and WIFEXITED() | |
1930 | * and convert the child's status to a return code for | |
1931 | * tracing purposes and emit the `trace2_child_exit()` | |
1932 | * event. | |
1933 | * | |
1934 | * We do not want the wait_or_whine() error message | |
1935 | * because we will be called by client-side library | |
1936 | * routines. | |
1937 | */ | |
1938 | if (WIFEXITED(wait_status)) | |
1939 | child_code = WEXITSTATUS(wait_status); | |
1940 | else if (WIFSIGNALED(wait_status)) | |
1941 | child_code = WTERMSIG(wait_status) + 128; | |
1942 | trace2_child_exit(cmd, child_code); | |
1943 | ||
1944 | sbgr = SBGR_DIED; | |
1945 | goto done; | |
1946 | } | |
1947 | ||
1948 | else if (pid_seen < 0 && errno == EINTR) | |
1949 | goto wait; | |
1950 | ||
1951 | trace2_child_exit(cmd, -1); | |
1952 | sbgr = SBGR_ERROR; | |
1953 | ||
1954 | done: | |
1955 | child_process_clear(cmd); | |
1956 | invalidate_lstat_cache(); | |
1957 | return sbgr; | |
1958 | } |