|
41 | 41 | #include <sys/utsname.h>
|
42 | 42 | #include <sys/capability.h>
|
43 | 43 |
|
| 44 | +#ifdef HAVE_ANDROID_OS |
| 45 | +#include "fd_utils-inl.h" |
| 46 | +#endif |
| 47 | + |
44 | 48 | #if defined(HAVE_PRCTL)
|
45 | 49 | # include <sys/prctl.h>
|
46 | 50 | #endif
|
@@ -523,6 +527,11 @@ static void detachDescriptors(ArrayObject* fdsToClose) {
|
523 | 527 |
|
524 | 528 | #endif
|
525 | 529 |
|
| 530 | +#ifdef HAVE_ANDROID_OS |
| 531 | +// The list of open zygote file descriptors. |
| 532 | +static FileDescriptorTable* gOpenFdTable = NULL; |
| 533 | +#endif |
| 534 | + |
526 | 535 | /*
|
527 | 536 | * Utility routine to fork zygote and specialize the child process.
|
528 | 537 | */
|
@@ -590,6 +599,27 @@ static pid_t forkAndSpecializeCommon(const u4* args, bool isSystemServer, bool l
|
590 | 599 | setSignalHandler();
|
591 | 600 |
|
592 | 601 | dvmDumpLoaderStats("zygote");
|
| 602 | + |
| 603 | + // Close any logging related FDs before we start evaluating the list of |
| 604 | + // file descriptors. |
| 605 | + __android_log_close(); |
| 606 | + |
| 607 | +#ifdef HAVE_ANDROID_OS |
| 608 | + // If this is the first fork for this zygote, create the open FD table. |
| 609 | + // If it isn't, we just need to check whether the list of open files |
| 610 | + // has changed (and it shouldn't in the normal case). |
| 611 | + if (gOpenFdTable == NULL) { |
| 612 | + gOpenFdTable = FileDescriptorTable::Create(); |
| 613 | + if (gOpenFdTable == NULL) { |
| 614 | + ALOGE("Unable to construct file descriptor table."); |
| 615 | + dvmAbort(); |
| 616 | + } |
| 617 | + } else if (!gOpenFdTable->Restat()) { |
| 618 | + ALOGE("Unable to restat file descriptor table."); |
| 619 | + dvmAbort(); |
| 620 | + } |
| 621 | +#endif |
| 622 | + |
593 | 623 | pid = fork();
|
594 | 624 |
|
595 | 625 | if (pid == 0) {
|
@@ -629,6 +659,12 @@ static pid_t forkAndSpecializeCommon(const u4* args, bool isSystemServer, bool l
|
629 | 659 | }
|
630 | 660 | }
|
631 | 661 |
|
| 662 | + // Re-open all remaining open file descriptors so that they aren't |
| 663 | + // shared with the zygote across a fork. |
| 664 | + if (!gOpenFdTable->ReopenOrDetach()) { |
| 665 | + ALOGE("Unable to reopen whitelisted descriptors."); |
| 666 | + dvmAbort(); |
| 667 | + } |
632 | 668 | #endif /* HAVE_ANDROID_OS */
|
633 | 669 |
|
634 | 670 | if (mountMode != MOUNT_EXTERNAL_NONE) {
|
|
0 commit comments