PinnerService 工作流程介绍

  • Post author:
  • Post category:其他


PinnerService配置

// Pin using pinlist.meta when pinning apps.
private static boolean PROP_PIN_PINLIST = SystemProperties.getBoolean(
        "pinner.use_pinlist", true);
// Pin the whole odex/vdex/etc file when pinning apps.
private static boolean PROP_PIN_ODEX = SystemProperties.getBoolean(
        "pinner.whole_odex", true);

配置 PinnerService 默认列表

<!-- Default files to pin via Pinner Service -->
<string-array translatable="false" name="config_defaultPinnerServiceFiles">
</string-array>

<!-- True if camera app should be pinned via Pinner Service -->
<bool name="config_pinnerCameraApp">false</bool>

<!-- True if home app should be pinned via Pinner Service -->
<bool name="config_pinnerHomeApp">false</bool>

<!-- True if assistant app should be pinned via Pinner Service -->
<bool name="config_pinnerAssistantApp">false</bool>
mConfiguredToPinCamera = context.getResources().getBoolean(
                    com.android.internal.R.bool.config_pinnerCameraApp);
mConfiguredToPinHome = context.getResources().getBoolean(
                    com.android.internal.R.bool.config_pinnerHomeApp);
mConfiguredToPinAssistant = context.getResources().getBoolean(
                    com.android.internal.R.bool.config_pinnerAssistantApp);
private void handlePinOnStart() {
    // Files to pin come from the overlay and can be specified per-device config
    String[] filesToPin = mContext.getResources().getStringArray(
        com.android.internal.R.array.config_defaultPinnerServiceFiles);
    // Continue trying to pin each file even if we fail to pin some of them
    for (String fileToPin : filesToPin) {
        PinnedFile pf = pinFile(fileToPin,
                                Integer.MAX_VALUE,
                                /*attemptPinIntrospection=*/false);
        if (pf == null) {
            Slog.e(TAG, "Failed to pin file = " + fileToPin);
            continue;
        }
        synchronized (this) {
            mPinnedFiles.add(pf);
        }
        if (fileToPin.endsWith(".jar") | fileToPin.endsWith(".apk")) {
            // Check whether the runtime has compilation artifacts to pin.
            String arch = VMRuntime.getInstructionSet(Build.SUPPORTED_ABIS[0]);
            String[] files = null;
            try {
                files = DexFile.getDexFileOutputPaths(fileToPin, arch);
            } catch (IOException ioe) { }
            if (files == null) {
                continue;
            }
            for (String file : files) {
                PinnedFile df = pinFile(file,
                                        Integer.MAX_VALUE,
                                        /*attemptPinIntrospection=*/false);
                if (df == null) {
                    Slog.i(TAG, "Failed to pin ART file = " + file);
                    continue;
                }
                synchronized (this) {
                    mPinnedFiles.add(df);
                }
            }
        }
    }
}

解压文件后,做 mmap 操作

private static PinnedFile pinFile(String fileToPin,
                                  int maxBytesToPin,
                                  boolean attemptPinIntrospection) {
    ZipFile fileAsZip = null;
    InputStream pinRangeStream = null;
    try {
        if (attemptPinIntrospection) {
            fileAsZip = maybeOpenZip(fileToPin);
        }

        if (fileAsZip != null) {
            pinRangeStream = maybeOpenPinMetaInZip(fileAsZip, fileToPin);
        }

        Slog.d(TAG, "pinRangeStream: " + pinRangeStream);

        PinRangeSource pinRangeSource = (pinRangeStream != null)
            ? new PinRangeSourceStream(pinRangeStream)
            : new PinRangeSourceStatic(0, Integer.MAX_VALUE /* will be clipped */);
        return pinFileRanges(fileToPin, maxBytesToPin, pinRangeSource);
    } finally {
        safeClose(pinRangeStream);
        safeClose(fileAsZip);  // Also closes any streams we've opened
    }
}

将文件做 mmap 操作

/**
 * Helper for pinFile.
 *
 * @param fileToPin Name of file to pin
 * @param maxBytesToPin Maximum number of bytes to pin
 * @param pinRangeSource Read PIN_RANGE entries from this stream to tell us what bytes
 *   to pin.
 * @return PinnedFile or null on error
 */
private static PinnedFile pinFileRanges(
    String fileToPin,
    int maxBytesToPin,
    PinRangeSource pinRangeSource)
{
    FileDescriptor fd = new FileDescriptor();
    long address = -1;
    int mapSize = 0;

    try {
        int openFlags = (OsConstants.O_RDONLY | OsConstants.O_CLOEXEC);
        fd = Os.open(fileToPin, openFlags, 0);
        mapSize = (int) Math.min(Os.fstat(fd).st_size, Integer.MAX_VALUE);
        address = Os.mmap(0, mapSize,
                          OsConstants.PROT_READ,
                          OsConstants.MAP_SHARED,
                          fd, /*offset=*/0);

        PinRange pinRange = new PinRange();
        int bytesPinned = 0;

        // We pin at page granularity, so make sure the limit is page-aligned
        if (maxBytesToPin % PAGE_SIZE != 0) {
            maxBytesToPin -= maxBytesToPin % PAGE_SIZE;
        }

        while (bytesPinned < maxBytesToPin && pinRangeSource.read(pinRange)) {
            int pinStart = pinRange.start;
            int pinLength = pinRange.length;
            pinStart = clamp(0, pinStart, mapSize);
            pinLength = clamp(0, pinLength, mapSize - pinStart);
            pinLength = Math.min(maxBytesToPin - bytesPinned, pinLength);

            // mlock doesn't require the region to be page-aligned, but we snap the
            // lock region to page boundaries anyway so that we don't under-count
            // locking a single byte of a page as a charge of one byte even though the
            // OS will retain the whole page. Thanks to this adjustment, we slightly
            // over-count the pin charge of back-to-back pins touching the same page,
            // but better that than undercounting. Besides: nothing stops pin metafile
            // creators from making the actual regions page-aligned.
            pinLength += pinStart % PAGE_SIZE;
            pinStart -= pinStart % PAGE_SIZE;
            if (pinLength % PAGE_SIZE != 0) {
                pinLength += PAGE_SIZE - pinLength % PAGE_SIZE;
            }
            pinLength = clamp(0, pinLength, maxBytesToPin - bytesPinned);

            if (pinLength > 0) {
                if (DEBUG) {
                    Slog.d(TAG,
                           String.format(
                               "pinning at %s %s bytes of %s",
                               pinStart, pinLength, fileToPin));
                }
                Os.mlock(address + pinStart, pinLength);
            }
            bytesPinned += pinLength;
        }

        PinnedFile pinnedFile = new PinnedFile(address, mapSize, fileToPin, bytesPinned);
        address = -1;  // Ownership transferred
        return pinnedFile;
    } catch (ErrnoException ex) {
        Slog.e(TAG, "Could not pin file " + fileToPin, ex);
        return null;
    } finally {
        safeClose(fd);
        if (address >= 0) {
            safeMunmap(address, mapSize);
        }
    }
}



版权声明:本文为mzsyxiao原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。