Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 07578c78 authored by Andreas Gampe's avatar Andreas Gampe Committed by android-build-merger
Browse files

Merge "Framework: Add API to get zygote PID"

am: 1109e233

Change-Id: I6f50efa61bcdec321cd71edae8a28881293c0398
parents 24062f05 1109e233
Loading
Loading
Loading
Loading
+29 −0
Original line number Diff line number Diff line
@@ -461,6 +461,35 @@ public class ZygoteProcess {
        }
    }

    /**
     * Attempt to retrieve the PID of the zygote serving the given abi.
     */
    public int getZygotePid(String abi) {
        try {
            synchronized (mLock) {
                ZygoteState state = openZygoteSocketIfNeeded(abi);

                // Each query starts with the argument count (1 in this case)
                state.writer.write("1");
                // ... followed by a new-line.
                state.writer.newLine();
                // ... followed by our only argument.
                state.writer.write("--get-pid");
                state.writer.newLine();
                state.writer.flush();

                // The response is a length prefixed stream of ASCII bytes.
                int numBytes = state.inputStream.readInt();
                byte[] bytes = new byte[numBytes];
                state.inputStream.readFully(bytes);

                return Integer.parseInt(new String(bytes, StandardCharsets.US_ASCII));
            }
        } catch (Exception ex) {
            throw new RuntimeException("Failure retrieving pid", ex);
        }
    }

    /**
     * Push hidden API blacklisting exemptions into the zygote process(es).
     *
+24 −1
Original line number Diff line number Diff line
@@ -150,6 +150,11 @@ class ZygoteConnection {
            return null;
        }

        if (parsedArgs.pidQuery) {
            handlePidQuery();
            return null;
        }

        if (parsedArgs.preloadDefault) {
            handlePreload();
            return null;
@@ -266,6 +271,17 @@ class ZygoteConnection {
        }
    }

    private void handlePidQuery() {
        try {
            String pidString = String.valueOf(Process.myPid());
            final byte[] pidStringBytes = pidString.getBytes(StandardCharsets.US_ASCII);
            mSocketOutStream.writeInt(pidStringBytes.length);
            mSocketOutStream.write(pidStringBytes);
        } catch (IOException ioe) {
            throw new IllegalStateException("Error writing to command socket", ioe);
        }
    }

    /**
     * Preloads resources if the zygote is in lazily preload mode. Writes the result of the
     * preload operation; {@code 0} when a preload was initiated due to this request and {@code 1}
@@ -439,6 +455,11 @@ class ZygoteConnection {
         */
        boolean startChildZygote;

        /**
         * Whether the current arguments constitute a request for the zygote's PID.
         */
        boolean pidQuery;

        /**
         * Exemptions from API blacklisting. These are sent to the pre-forked zygote at boot time,
         * or when they change, via --set-api-blacklist-exemptions.
@@ -586,6 +607,8 @@ class ZygoteConnection {
                    mountExternal = Zygote.MOUNT_EXTERNAL_WRITE;
                } else if (arg.equals("--query-abi-list")) {
                    abiListQuery = true;
                } else if (arg.equals("--get-pid")) {
                    pidQuery = true;
                } else if (arg.startsWith("--instruction-set=")) {
                    instructionSet = arg.substring(arg.indexOf('=') + 1);
                } else if (arg.startsWith("--app-data-dir=")) {
@@ -608,7 +631,7 @@ class ZygoteConnection {
                }
            }

            if (abiListQuery) {
            if (abiListQuery || pidQuery) {
                if (args.length - curArg > 0) {
                    throw new IllegalArgumentException("Unexpected arguments after --query-abi-list.");
                }