Merge "Framework: Add API to get zygote PID" am: 1109e2333f

am: 07578c786a

Change-Id: Ibee53631caad0d6ce2108970598f5226d20a849d
This commit is contained in:
Andreas Gampe
2018-05-24 13:49:41 -07:00
committed by android-build-merger
2 changed files with 53 additions and 1 deletions

View File

@@ -466,6 +466,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).
*

View File

@@ -150,6 +150,11 @@ class ZygoteConnection {
return null;
}
if (parsedArgs.pidQuery) {
handlePidQuery();
return null;
}
if (parsedArgs.preloadDefault) {
handlePreload();
return null;
@@ -271,6 +276,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}
@@ -468,6 +484,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.
@@ -622,6 +643,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=")) {
@@ -656,7 +679,7 @@ class ZygoteConnection {
}
}
if (abiListQuery) {
if (abiListQuery || pidQuery) {
if (args.length - curArg > 0) {
throw new IllegalArgumentException("Unexpected arguments after --query-abi-list.");
}