diff options
| -rw-r--r-- | core/java/android/os/ZygoteProcess.java | 29 | ||||
| -rw-r--r-- | core/java/com/android/internal/os/ZygoteConnection.java | 25 |
2 files changed, 53 insertions, 1 deletions
diff --git a/core/java/android/os/ZygoteProcess.java b/core/java/android/os/ZygoteProcess.java index 5718ef92d6cc..bb77a9371171 100644 --- a/core/java/android/os/ZygoteProcess.java +++ b/core/java/android/os/ZygoteProcess.java @@ -462,6 +462,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). * * <p>The list of exemptions will take affect for all new processes forked from the zygote after diff --git a/core/java/com/android/internal/os/ZygoteConnection.java b/core/java/com/android/internal/os/ZygoteConnection.java index adc550866895..b9f33e7f3c85 100644 --- a/core/java/com/android/internal/os/ZygoteConnection.java +++ b/core/java/com/android/internal/os/ZygoteConnection.java @@ -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} @@ -440,6 +456,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."); } |