| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.os; |
| 18 |
|
| 19 |
import static android.os.Process.ZYGOTE_POLICY_FLAG_LATENCY_SENSITIVE; |
| 20 |
import static android.os.Process.ZYGOTE_POLICY_FLAG_SYSTEM_PROCESS; |
| 21 |
|
| 22 |
import android.annotation.NonNull; |
| 23 |
import android.annotation.Nullable; |
| 24 |
import android.compat.annotation.UnsupportedAppUsage; |
| 25 |
import android.content.pm.ApplicationInfo; |
| 26 |
import android.net.LocalSocket; |
| 27 |
import android.net.LocalSocketAddress; |
| 28 |
import android.util.Log; |
| 29 |
import android.util.Pair; |
| 30 |
import android.util.Slog; |
| 31 |
|
| 32 |
import com.android.internal.annotations.GuardedBy; |
| 33 |
import com.android.internal.os.Zygote; |
| 34 |
import com.android.internal.os.ZygoteConfig; |
| 35 |
|
| 36 |
import java.io.BufferedWriter; |
| 37 |
import java.io.DataInputStream; |
| 38 |
import java.io.IOException; |
| 39 |
import java.io.OutputStreamWriter; |
| 40 |
import java.nio.charset.StandardCharsets; |
| 41 |
import java.util.ArrayList; |
| 42 |
import java.util.Arrays; |
| 43 |
import java.util.Base64; |
| 44 |
import java.util.Collections; |
| 45 |
import java.util.List; |
| 46 |
import java.util.Map; |
| 47 |
import java.util.UUID; |
| 48 |
|
| 49 |
class ZygoteStartFailedEx extends Exception { |
| 50 |
@UnsupportedAppUsage |
| 51 |
ZygoteStartFailedEx(String s) { |
| 52 |
super(s); |
| 53 |
} |
| 54 |
|
| 55 |
@UnsupportedAppUsage |
| 56 |
ZygoteStartFailedEx(Throwable cause) { |
| 57 |
super(cause); |
| 58 |
} |
| 59 |
|
| 60 |
ZygoteStartFailedEx(String s, Throwable cause) { |
| 61 |
super(s, cause); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
public class ZygoteProcess { |
| 73 |
|
| 74 |
private static final int ZYGOTE_CONNECT_TIMEOUT_MS = 20000; |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
private static final int ZYGOTE_CONNECT_RETRY_DELAY_MS = 50; |
| 81 |
|
| 82 |
private static final String LOG_TAG = "ZygoteProcess"; |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
private final LocalSocketAddress mZygoteSocketAddress; |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
private final LocalSocketAddress mZygoteSecondarySocketAddress; |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
private final LocalSocketAddress mUsapPoolSocketAddress; |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
private final LocalSocketAddress mUsapPoolSecondarySocketAddress; |
| 103 |
|
| 104 |
public ZygoteProcess() { |
| 105 |
mZygoteSocketAddress = |
| 106 |
new LocalSocketAddress(Zygote.PRIMARY_SOCKET_NAME, |
| 107 |
LocalSocketAddress.Namespace.RESERVED); |
| 108 |
mZygoteSecondarySocketAddress = |
| 109 |
new LocalSocketAddress(Zygote.SECONDARY_SOCKET_NAME, |
| 110 |
LocalSocketAddress.Namespace.RESERVED); |
| 111 |
|
| 112 |
mUsapPoolSocketAddress = |
| 113 |
new LocalSocketAddress(Zygote.USAP_POOL_PRIMARY_SOCKET_NAME, |
| 114 |
LocalSocketAddress.Namespace.RESERVED); |
| 115 |
mUsapPoolSecondarySocketAddress = |
| 116 |
new LocalSocketAddress(Zygote.USAP_POOL_SECONDARY_SOCKET_NAME, |
| 117 |
LocalSocketAddress.Namespace.RESERVED); |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
mUsapPoolSupported = true; |
| 122 |
} |
| 123 |
|
| 124 |
public ZygoteProcess(LocalSocketAddress primarySocketAddress, |
| 125 |
LocalSocketAddress secondarySocketAddress) { |
| 126 |
mZygoteSocketAddress = primarySocketAddress; |
| 127 |
mZygoteSecondarySocketAddress = secondarySocketAddress; |
| 128 |
|
| 129 |
mUsapPoolSocketAddress = null; |
| 130 |
mUsapPoolSecondarySocketAddress = null; |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
mUsapPoolSupported = false; |
| 135 |
} |
| 136 |
|
| 137 |
public LocalSocketAddress getPrimarySocketAddress() { |
| 138 |
return mZygoteSocketAddress; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
private static class ZygoteState implements AutoCloseable { |
| 145 |
final LocalSocketAddress mZygoteSocketAddress; |
| 146 |
final LocalSocketAddress mUsapSocketAddress; |
| 147 |
|
| 148 |
private final LocalSocket mZygoteSessionSocket; |
| 149 |
|
| 150 |
final DataInputStream mZygoteInputStream; |
| 151 |
final BufferedWriter mZygoteOutputWriter; |
| 152 |
|
| 153 |
private final List<String> mAbiList; |
| 154 |
|
| 155 |
private boolean mClosed; |
| 156 |
|
| 157 |
private ZygoteState(LocalSocketAddress zygoteSocketAddress, |
| 158 |
LocalSocketAddress usapSocketAddress, |
| 159 |
LocalSocket zygoteSessionSocket, |
| 160 |
DataInputStream zygoteInputStream, |
| 161 |
BufferedWriter zygoteOutputWriter, |
| 162 |
List<String> abiList) { |
| 163 |
this.mZygoteSocketAddress = zygoteSocketAddress; |
| 164 |
this.mUsapSocketAddress = usapSocketAddress; |
| 165 |
this.mZygoteSessionSocket = zygoteSessionSocket; |
| 166 |
this.mZygoteInputStream = zygoteInputStream; |
| 167 |
this.mZygoteOutputWriter = zygoteOutputWriter; |
| 168 |
this.mAbiList = abiList; |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
static ZygoteState connect(@NonNull LocalSocketAddress zygoteSocketAddress, |
| 182 |
@Nullable LocalSocketAddress usapSocketAddress) |
| 183 |
throws IOException { |
| 184 |
|
| 185 |
DataInputStream zygoteInputStream; |
| 186 |
BufferedWriter zygoteOutputWriter; |
| 187 |
final LocalSocket zygoteSessionSocket = new LocalSocket(); |
| 188 |
|
| 189 |
if (zygoteSocketAddress == null) { |
| 190 |
throw new IllegalArgumentException("zygoteSocketAddress can't be null"); |
| 191 |
} |
| 192 |
|
| 193 |
try { |
| 194 |
zygoteSessionSocket.connect(zygoteSocketAddress); |
| 195 |
zygoteInputStream = new DataInputStream(zygoteSessionSocket.getInputStream()); |
| 196 |
zygoteOutputWriter = |
| 197 |
new BufferedWriter( |
| 198 |
new OutputStreamWriter(zygoteSessionSocket.getOutputStream()), |
| 199 |
Zygote.SOCKET_BUFFER_SIZE); |
| 200 |
} catch (IOException ex) { |
| 201 |
try { |
| 202 |
zygoteSessionSocket.close(); |
| 203 |
} catch (IOException ignore) { } |
| 204 |
|
| 205 |
throw ex; |
| 206 |
} |
| 207 |
|
| 208 |
return new ZygoteState(zygoteSocketAddress, usapSocketAddress, |
| 209 |
zygoteSessionSocket, zygoteInputStream, zygoteOutputWriter, |
| 210 |
getAbiList(zygoteOutputWriter, zygoteInputStream)); |
| 211 |
} |
| 212 |
|
| 213 |
LocalSocket getUsapSessionSocket() throws IOException { |
| 214 |
final LocalSocket usapSessionSocket = new LocalSocket(); |
| 215 |
usapSessionSocket.connect(this.mUsapSocketAddress); |
| 216 |
|
| 217 |
return usapSessionSocket; |
| 218 |
} |
| 219 |
|
| 220 |
boolean matches(String abi) { |
| 221 |
return mAbiList.contains(abi); |
| 222 |
} |
| 223 |
|
| 224 |
public void close() { |
| 225 |
try { |
| 226 |
mZygoteSessionSocket.close(); |
| 227 |
} catch (IOException ex) { |
| 228 |
Log.e(LOG_TAG,"I/O exception on routine close", ex); |
| 229 |
} |
| 230 |
|
| 231 |
mClosed = true; |
| 232 |
} |
| 233 |
|
| 234 |
boolean isClosed() { |
| 235 |
return mClosed; |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
private final Object mLock = new Object(); |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
private List<String> mApiDenylistExemptions = Collections.emptyList(); |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
private int mHiddenApiAccessLogSampleRate; |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
private int mHiddenApiAccessStatslogSampleRate; |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
private ZygoteState primaryZygoteState; |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
private ZygoteState secondaryZygoteState; |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
private final boolean mUsapPoolSupported; |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
private boolean mUsapPoolEnabled = false; |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
public final Process.ProcessStartResult start(@NonNull final String processClass, |
| 339 |
final String niceName, |
| 340 |
int uid, int gid, @Nullable int[] gids, |
| 341 |
int runtimeFlags, int mountExternal, |
| 342 |
int targetSdkVersion, |
| 343 |
@Nullable String seInfo, |
| 344 |
@NonNull String abi, |
| 345 |
@Nullable String instructionSet, |
| 346 |
@Nullable String appDataDir, |
| 347 |
@Nullable String invokeWith, |
| 348 |
@Nullable String packageName, |
| 349 |
int zygotePolicyFlags, |
| 350 |
boolean isTopApp, |
| 351 |
@Nullable long[] disabledCompatChanges, |
| 352 |
@Nullable Map<String, Pair<String, Long>> |
| 353 |
pkgDataInfoMap, |
| 354 |
@Nullable Map<String, Pair<String, Long>> |
| 355 |
allowlistedDataInfoList, |
| 356 |
boolean bindMountAppsData, |
| 357 |
boolean bindMountAppStorageDirs, |
| 358 |
@Nullable String[] zygoteArgs) { |
| 359 |
|
| 360 |
if (fetchUsapPoolEnabledPropWithMinInterval()) { |
| 361 |
informZygotesOfUsapPoolStatus(); |
| 362 |
} |
| 363 |
|
| 364 |
try { |
| 365 |
return startViaZygote(processClass, niceName, uid, gid, gids, |
| 366 |
runtimeFlags, mountExternal, targetSdkVersion, seInfo, |
| 367 |
abi, instructionSet, appDataDir, invokeWith, false, |
| 368 |
packageName, zygotePolicyFlags, isTopApp, disabledCompatChanges, |
| 369 |
pkgDataInfoMap, allowlistedDataInfoList, bindMountAppsData, |
| 370 |
bindMountAppStorageDirs, zygoteArgs); |
| 371 |
} catch (ZygoteStartFailedEx ex) { |
| 372 |
Log.e(LOG_TAG, |
| 373 |
"Starting VM process through Zygote failed"); |
| 374 |
throw new RuntimeException( |
| 375 |
"Starting VM process through Zygote failed", ex); |
| 376 |
} |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
static final int ZYGOTE_RETRY_MILLIS = 500; |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
@GuardedBy("mLock") |
| 386 |
private static List<String> getAbiList(BufferedWriter writer, DataInputStream inputStream) |
| 387 |
throws IOException { |
| 388 |
|
| 389 |
writer.write("1"); |
| 390 |
|
| 391 |
writer.newLine(); |
| 392 |
|
| 393 |
writer.write("--query-abi-list"); |
| 394 |
writer.newLine(); |
| 395 |
writer.flush(); |
| 396 |
|
| 397 |
|
| 398 |
int numBytes = inputStream.readInt(); |
| 399 |
byte[] bytes = new byte[numBytes]; |
| 400 |
inputStream.readFully(bytes); |
| 401 |
|
| 402 |
final String rawList = new String(bytes, StandardCharsets.US_ASCII); |
| 403 |
|
| 404 |
return Arrays.asList(rawList.split(",")); |
| 405 |
} |
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
@GuardedBy("mLock") |
| 415 |
private Process.ProcessStartResult zygoteSendArgsAndGetResult( |
| 416 |
ZygoteState zygoteState, int zygotePolicyFlags, @NonNull ArrayList<String> args) |
| 417 |
throws ZygoteStartFailedEx { |
| 418 |
|
| 419 |
|
| 420 |
for (String arg : args) { |
| 421 |
|
| 422 |
|
| 423 |
if (arg.indexOf('\n') >= 0) { |
| 424 |
throw new ZygoteStartFailedEx("Embedded newlines not allowed"); |
| 425 |
} else if (arg.indexOf('\r') >= 0) { |
| 426 |
throw new ZygoteStartFailedEx("Embedded carriage returns not allowed"); |
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
String msgStr = args.size() + "\n" + String.join("\n", args) + "\n"; |
| 441 |
|
| 442 |
if (shouldAttemptUsapLaunch(zygotePolicyFlags, args)) { |
| 443 |
try { |
| 444 |
return attemptUsapSendArgsAndGetResult(zygoteState, msgStr); |
| 445 |
} catch (IOException ex) { |
| 446 |
|
| 447 |
|
| 448 |
Log.e(LOG_TAG, "IO Exception while communicating with USAP pool - " |
| 449 |
+ ex.getMessage()); |
| 450 |
} |
| 451 |
} |
| 452 |
|
| 453 |
return attemptZygoteSendArgsAndGetResult(zygoteState, msgStr); |
| 454 |
} |
| 455 |
|
| 456 |
private Process.ProcessStartResult attemptZygoteSendArgsAndGetResult( |
| 457 |
ZygoteState zygoteState, String msgStr) throws ZygoteStartFailedEx { |
| 458 |
try { |
| 459 |
final BufferedWriter zygoteWriter = zygoteState.mZygoteOutputWriter; |
| 460 |
final DataInputStream zygoteInputStream = zygoteState.mZygoteInputStream; |
| 461 |
|
| 462 |
zygoteWriter.write(msgStr); |
| 463 |
zygoteWriter.flush(); |
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
Process.ProcessStartResult result = new Process.ProcessStartResult(); |
| 469 |
result.pid = zygoteInputStream.readInt(); |
| 470 |
result.usingWrapper = zygoteInputStream.readBoolean(); |
| 471 |
|
| 472 |
if (result.pid < 0) { |
| 473 |
throw new ZygoteStartFailedEx("fork() failed"); |
| 474 |
} |
| 475 |
|
| 476 |
return result; |
| 477 |
} catch (IOException ex) { |
| 478 |
zygoteState.close(); |
| 479 |
Log.e(LOG_TAG, "IO Exception while communicating with Zygote - " |
| 480 |
+ ex.toString()); |
| 481 |
throw new ZygoteStartFailedEx(ex); |
| 482 |
} |
| 483 |
} |
| 484 |
|
| 485 |
private Process.ProcessStartResult attemptUsapSendArgsAndGetResult( |
| 486 |
ZygoteState zygoteState, String msgStr) |
| 487 |
throws ZygoteStartFailedEx, IOException { |
| 488 |
try (LocalSocket usapSessionSocket = zygoteState.getUsapSessionSocket()) { |
| 489 |
final BufferedWriter usapWriter = |
| 490 |
new BufferedWriter( |
| 491 |
new OutputStreamWriter(usapSessionSocket.getOutputStream()), |
| 492 |
Zygote.SOCKET_BUFFER_SIZE); |
| 493 |
final DataInputStream usapReader = |
| 494 |
new DataInputStream(usapSessionSocket.getInputStream()); |
| 495 |
|
| 496 |
usapWriter.write(msgStr); |
| 497 |
usapWriter.flush(); |
| 498 |
|
| 499 |
Process.ProcessStartResult result = new Process.ProcessStartResult(); |
| 500 |
result.pid = usapReader.readInt(); |
| 501 |
|
| 502 |
result.usingWrapper = false; |
| 503 |
|
| 504 |
if (result.pid >= 0) { |
| 505 |
return result; |
| 506 |
} else { |
| 507 |
throw new ZygoteStartFailedEx("USAP specialization failed"); |
| 508 |
} |
| 509 |
} |
| 510 |
} |
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
private boolean shouldAttemptUsapLaunch(int zygotePolicyFlags, ArrayList<String> args) { |
| 522 |
return mUsapPoolSupported |
| 523 |
&& mUsapPoolEnabled |
| 524 |
&& policySpecifiesUsapPoolLaunch(zygotePolicyFlags) |
| 525 |
&& commandSupportedByUsap(args); |
| 526 |
} |
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
private static boolean policySpecifiesUsapPoolLaunch(int zygotePolicyFlags) { |
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
return (zygotePolicyFlags |
| 543 |
& (ZYGOTE_POLICY_FLAG_SYSTEM_PROCESS | ZYGOTE_POLICY_FLAG_LATENCY_SENSITIVE)) |
| 544 |
== ZYGOTE_POLICY_FLAG_LATENCY_SENSITIVE; |
| 545 |
} |
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
private static final String[] INVALID_USAP_FLAGS = { |
| 552 |
"--query-abi-list", |
| 553 |
"--get-pid", |
| 554 |
"--preload-default", |
| 555 |
"--preload-package", |
| 556 |
"--preload-app", |
| 557 |
"--start-child-zygote", |
| 558 |
"--set-api-denylist-exemptions", |
| 559 |
"--hidden-api-log-sampling-rate", |
| 560 |
"--hidden-api-statslog-sampling-rate", |
| 561 |
"--invoke-with" |
| 562 |
}; |
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
private static boolean commandSupportedByUsap(ArrayList<String> args) { |
| 571 |
for (String flag : args) { |
| 572 |
for (String badFlag : INVALID_USAP_FLAGS) { |
| 573 |
if (flag.startsWith(badFlag)) { |
| 574 |
return false; |
| 575 |
} |
| 576 |
} |
| 577 |
if (flag.startsWith("--nice-name=")) { |
| 578 |
|
| 579 |
if (Zygote.getWrapProperty(flag.substring(12)) != null) { |
| 580 |
return false; |
| 581 |
} |
| 582 |
} |
| 583 |
} |
| 584 |
|
| 585 |
return true; |
| 586 |
} |
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
private Process.ProcessStartResult startViaZygote(@NonNull final String processClass, |
| 620 |
@Nullable final String niceName, |
| 621 |
final int uid, final int gid, |
| 622 |
@Nullable final int[] gids, |
| 623 |
int runtimeFlags, int mountExternal, |
| 624 |
int targetSdkVersion, |
| 625 |
@Nullable String seInfo, |
| 626 |
@NonNull String abi, |
| 627 |
@Nullable String instructionSet, |
| 628 |
@Nullable String appDataDir, |
| 629 |
@Nullable String invokeWith, |
| 630 |
boolean startChildZygote, |
| 631 |
@Nullable String packageName, |
| 632 |
int zygotePolicyFlags, |
| 633 |
boolean isTopApp, |
| 634 |
@Nullable long[] disabledCompatChanges, |
| 635 |
@Nullable Map<String, Pair<String, Long>> |
| 636 |
pkgDataInfoMap, |
| 637 |
@Nullable Map<String, Pair<String, Long>> |
| 638 |
allowlistedDataInfoList, |
| 639 |
boolean bindMountAppsData, |
| 640 |
boolean bindMountAppStorageDirs, |
| 641 |
@Nullable String[] extraArgs) |
| 642 |
throws ZygoteStartFailedEx { |
| 643 |
ArrayList<String> argsForZygote = new ArrayList<>(); |
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
argsForZygote.add("--runtime-args"); |
| 648 |
argsForZygote.add("--setuid=" + uid); |
| 649 |
argsForZygote.add("--setgid=" + gid); |
| 650 |
argsForZygote.add("--runtime-flags=" + runtimeFlags); |
| 651 |
if (mountExternal == Zygote.MOUNT_EXTERNAL_DEFAULT) { |
| 652 |
argsForZygote.add("--mount-external-default"); |
| 653 |
} else if (mountExternal == Zygote.MOUNT_EXTERNAL_INSTALLER) { |
| 654 |
argsForZygote.add("--mount-external-installer"); |
| 655 |
} else if (mountExternal == Zygote.MOUNT_EXTERNAL_PASS_THROUGH) { |
| 656 |
argsForZygote.add("--mount-external-pass-through"); |
| 657 |
} else if (mountExternal == Zygote.MOUNT_EXTERNAL_ANDROID_WRITABLE) { |
| 658 |
argsForZygote.add("--mount-external-android-writable"); |
| 659 |
} |
| 660 |
|
| 661 |
argsForZygote.add("--target-sdk-version=" + targetSdkVersion); |
| 662 |
|
| 663 |
|
| 664 |
if (gids != null && gids.length > 0) { |
| 665 |
final StringBuilder sb = new StringBuilder(); |
| 666 |
sb.append("--setgroups="); |
| 667 |
|
| 668 |
final int sz = gids.length; |
| 669 |
for (int i = 0; i < sz; i++) { |
| 670 |
if (i != 0) { |
| 671 |
sb.append(','); |
| 672 |
} |
| 673 |
sb.append(gids[i]); |
| 674 |
} |
| 675 |
|
| 676 |
argsForZygote.add(sb.toString()); |
| 677 |
} |
| 678 |
|
| 679 |
if (niceName != null) { |
| 680 |
argsForZygote.add("--nice-name=" + niceName); |
| 681 |
} |
| 682 |
|
| 683 |
if (seInfo != null) { |
| 684 |
argsForZygote.add("--seinfo=" + seInfo); |
| 685 |
} |
| 686 |
|
| 687 |
if (instructionSet != null) { |
| 688 |
argsForZygote.add("--instruction-set=" + instructionSet); |
| 689 |
} |
| 690 |
|
| 691 |
if (appDataDir != null) { |
| 692 |
argsForZygote.add("--app-data-dir=" + appDataDir); |
| 693 |
} |
| 694 |
|
| 695 |
if (invokeWith != null) { |
| 696 |
argsForZygote.add("--invoke-with"); |
| 697 |
argsForZygote.add(invokeWith); |
| 698 |
} |
| 699 |
|
| 700 |
if (startChildZygote) { |
| 701 |
argsForZygote.add("--start-child-zygote"); |
| 702 |
} |
| 703 |
|
| 704 |
if (packageName != null) { |
| 705 |
argsForZygote.add("--package-name=" + packageName); |
| 706 |
} |
| 707 |
|
| 708 |
if (isTopApp) { |
| 709 |
argsForZygote.add(Zygote.START_AS_TOP_APP_ARG); |
| 710 |
} |
| 711 |
if (pkgDataInfoMap != null && pkgDataInfoMap.size() > 0) { |
| 712 |
StringBuilder sb = new StringBuilder(); |
| 713 |
sb.append(Zygote.PKG_DATA_INFO_MAP); |
| 714 |
sb.append("="); |
| 715 |
boolean started = false; |
| 716 |
for (Map.Entry<String, Pair<String, Long>> entry : pkgDataInfoMap.entrySet()) { |
| 717 |
if (started) { |
| 718 |
sb.append(','); |
| 719 |
} |
| 720 |
started = true; |
| 721 |
sb.append(entry.getKey()); |
| 722 |
sb.append(','); |
| 723 |
sb.append(entry.getValue().first); |
| 724 |
sb.append(','); |
| 725 |
sb.append(entry.getValue().second); |
| 726 |
} |
| 727 |
argsForZygote.add(sb.toString()); |
| 728 |
} |
| 729 |
if (allowlistedDataInfoList != null && allowlistedDataInfoList.size() > 0) { |
| 730 |
StringBuilder sb = new StringBuilder(); |
| 731 |
sb.append(Zygote.ALLOWLISTED_DATA_INFO_MAP); |
| 732 |
sb.append("="); |
| 733 |
boolean started = false; |
| 734 |
for (Map.Entry<String, Pair<String, Long>> entry : allowlistedDataInfoList.entrySet()) { |
| 735 |
if (started) { |
| 736 |
sb.append(','); |
| 737 |
} |
| 738 |
started = true; |
| 739 |
sb.append(entry.getKey()); |
| 740 |
sb.append(','); |
| 741 |
sb.append(entry.getValue().first); |
| 742 |
sb.append(','); |
| 743 |
sb.append(entry.getValue().second); |
| 744 |
} |
| 745 |
argsForZygote.add(sb.toString()); |
| 746 |
} |
| 747 |
|
| 748 |
if (bindMountAppStorageDirs) { |
| 749 |
argsForZygote.add(Zygote.BIND_MOUNT_APP_STORAGE_DIRS); |
| 750 |
} |
| 751 |
|
| 752 |
if (bindMountAppsData) { |
| 753 |
argsForZygote.add(Zygote.BIND_MOUNT_APP_DATA_DIRS); |
| 754 |
} |
| 755 |
|
| 756 |
if (disabledCompatChanges != null && disabledCompatChanges.length > 0) { |
| 757 |
StringBuilder sb = new StringBuilder(); |
| 758 |
sb.append("--disabled-compat-changes="); |
| 759 |
|
| 760 |
int sz = disabledCompatChanges.length; |
| 761 |
for (int i = 0; i < sz; i++) { |
| 762 |
if (i != 0) { |
| 763 |
sb.append(','); |
| 764 |
} |
| 765 |
sb.append(disabledCompatChanges[i]); |
| 766 |
} |
| 767 |
|
| 768 |
argsForZygote.add(sb.toString()); |
| 769 |
} |
| 770 |
|
| 771 |
argsForZygote.add(processClass); |
| 772 |
|
| 773 |
if (extraArgs != null) { |
| 774 |
Collections.addAll(argsForZygote, extraArgs); |
| 775 |
} |
| 776 |
|
| 777 |
synchronized(mLock) { |
| 778 |
|
| 779 |
|
| 780 |
return zygoteSendArgsAndGetResult(openZygoteSocketIfNeeded(abi), |
| 781 |
zygotePolicyFlags, |
| 782 |
argsForZygote); |
| 783 |
} |
| 784 |
} |
| 785 |
|
| 786 |
private boolean fetchUsapPoolEnabledProp() { |
| 787 |
boolean origVal = mUsapPoolEnabled; |
| 788 |
|
| 789 |
mUsapPoolEnabled = ZygoteConfig.getBool( |
| 790 |
ZygoteConfig.USAP_POOL_ENABLED, ZygoteConfig.USAP_POOL_ENABLED_DEFAULT); |
| 791 |
|
| 792 |
boolean valueChanged = origVal != mUsapPoolEnabled; |
| 793 |
|
| 794 |
if (valueChanged) { |
| 795 |
Log.i(LOG_TAG, "usapPoolEnabled = " + mUsapPoolEnabled); |
| 796 |
} |
| 797 |
|
| 798 |
return valueChanged; |
| 799 |
} |
| 800 |
|
| 801 |
private boolean mIsFirstPropCheck = true; |
| 802 |
private long mLastPropCheckTimestamp = 0; |
| 803 |
|
| 804 |
private boolean fetchUsapPoolEnabledPropWithMinInterval() { |
| 805 |
|
| 806 |
|
| 807 |
if (!mUsapPoolSupported) return false; |
| 808 |
|
| 809 |
final long currentTimestamp = SystemClock.elapsedRealtime(); |
| 810 |
|
| 811 |
if (mIsFirstPropCheck |
| 812 |
|| (currentTimestamp - mLastPropCheckTimestamp >= Zygote.PROPERTY_CHECK_INTERVAL)) { |
| 813 |
mIsFirstPropCheck = false; |
| 814 |
mLastPropCheckTimestamp = currentTimestamp; |
| 815 |
return fetchUsapPoolEnabledProp(); |
| 816 |
} |
| 817 |
|
| 818 |
return false; |
| 819 |
} |
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
public void close() { |
| 825 |
if (primaryZygoteState != null) { |
| 826 |
primaryZygoteState.close(); |
| 827 |
} |
| 828 |
if (secondaryZygoteState != null) { |
| 829 |
secondaryZygoteState.close(); |
| 830 |
} |
| 831 |
} |
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
public void establishZygoteConnectionForAbi(String abi) { |
| 839 |
try { |
| 840 |
synchronized(mLock) { |
| 841 |
openZygoteSocketIfNeeded(abi); |
| 842 |
} |
| 843 |
} catch (ZygoteStartFailedEx ex) { |
| 844 |
throw new RuntimeException("Unable to connect to zygote for abi: " + abi, ex); |
| 845 |
} |
| 846 |
} |
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
public int getZygotePid(String abi) { |
| 852 |
try { |
| 853 |
synchronized (mLock) { |
| 854 |
ZygoteState state = openZygoteSocketIfNeeded(abi); |
| 855 |
|
| 856 |
|
| 857 |
state.mZygoteOutputWriter.write("1"); |
| 858 |
|
| 859 |
state.mZygoteOutputWriter.newLine(); |
| 860 |
|
| 861 |
state.mZygoteOutputWriter.write("--get-pid"); |
| 862 |
state.mZygoteOutputWriter.newLine(); |
| 863 |
state.mZygoteOutputWriter.flush(); |
| 864 |
|
| 865 |
|
| 866 |
int numBytes = state.mZygoteInputStream.readInt(); |
| 867 |
byte[] bytes = new byte[numBytes]; |
| 868 |
state.mZygoteInputStream.readFully(bytes); |
| 869 |
|
| 870 |
return Integer.parseInt(new String(bytes, StandardCharsets.US_ASCII)); |
| 871 |
} |
| 872 |
} catch (Exception ex) { |
| 873 |
throw new RuntimeException("Failure retrieving pid", ex); |
| 874 |
} |
| 875 |
} |
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
public void bootCompleted() { |
| 881 |
|
| 882 |
if (Build.SUPPORTED_32_BIT_ABIS.length > 0) { |
| 883 |
bootCompleted(Build.SUPPORTED_32_BIT_ABIS[0]); |
| 884 |
} |
| 885 |
if (Build.SUPPORTED_64_BIT_ABIS.length > 0) { |
| 886 |
bootCompleted(Build.SUPPORTED_64_BIT_ABIS[0]); |
| 887 |
} |
| 888 |
} |
| 889 |
|
| 890 |
private void bootCompleted(String abi) { |
| 891 |
try { |
| 892 |
synchronized (mLock) { |
| 893 |
ZygoteState state = openZygoteSocketIfNeeded(abi); |
| 894 |
state.mZygoteOutputWriter.write("1\n--boot-completed\n"); |
| 895 |
state.mZygoteOutputWriter.flush(); |
| 896 |
state.mZygoteInputStream.readInt(); |
| 897 |
} |
| 898 |
} catch (Exception ex) { |
| 899 |
throw new RuntimeException("Failed to inform zygote of boot_completed", ex); |
| 900 |
} |
| 901 |
} |
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
public boolean setApiDenylistExemptions(List<String> exemptions) { |
| 913 |
synchronized (mLock) { |
| 914 |
mApiDenylistExemptions = exemptions; |
| 915 |
boolean ok = maybeSetApiDenylistExemptions(primaryZygoteState, true); |
| 916 |
if (ok) { |
| 917 |
ok = maybeSetApiDenylistExemptions(secondaryZygoteState, true); |
| 918 |
} |
| 919 |
return ok; |
| 920 |
} |
| 921 |
} |
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
public void setHiddenApiAccessLogSampleRate(int rate) { |
| 931 |
synchronized (mLock) { |
| 932 |
mHiddenApiAccessLogSampleRate = rate; |
| 933 |
maybeSetHiddenApiAccessLogSampleRate(primaryZygoteState); |
| 934 |
maybeSetHiddenApiAccessLogSampleRate(secondaryZygoteState); |
| 935 |
} |
| 936 |
} |
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
public void setHiddenApiAccessStatslogSampleRate(int rate) { |
| 946 |
synchronized (mLock) { |
| 947 |
mHiddenApiAccessStatslogSampleRate = rate; |
| 948 |
maybeSetHiddenApiAccessStatslogSampleRate(primaryZygoteState); |
| 949 |
maybeSetHiddenApiAccessStatslogSampleRate(secondaryZygoteState); |
| 950 |
} |
| 951 |
} |
| 952 |
|
| 953 |
@GuardedBy("mLock") |
| 954 |
private boolean maybeSetApiDenylistExemptions(ZygoteState state, boolean sendIfEmpty) { |
| 955 |
if (state == null || state.isClosed()) { |
| 956 |
Slog.e(LOG_TAG, "Can't set API denylist exemptions: no zygote connection"); |
| 957 |
return false; |
| 958 |
} else if (!sendIfEmpty && mApiDenylistExemptions.isEmpty()) { |
| 959 |
return true; |
| 960 |
} |
| 961 |
|
| 962 |
try { |
| 963 |
state.mZygoteOutputWriter.write(Integer.toString(mApiDenylistExemptions.size() + 1)); |
| 964 |
state.mZygoteOutputWriter.newLine(); |
| 965 |
state.mZygoteOutputWriter.write("--set-api-denylist-exemptions"); |
| 966 |
state.mZygoteOutputWriter.newLine(); |
| 967 |
for (int i = 0; i < mApiDenylistExemptions.size(); ++i) { |
| 968 |
state.mZygoteOutputWriter.write(mApiDenylistExemptions.get(i)); |
| 969 |
state.mZygoteOutputWriter.newLine(); |
| 970 |
} |
| 971 |
state.mZygoteOutputWriter.flush(); |
| 972 |
int status = state.mZygoteInputStream.readInt(); |
| 973 |
if (status != 0) { |
| 974 |
Slog.e(LOG_TAG, "Failed to set API denylist exemptions; status " + status); |
| 975 |
} |
| 976 |
return true; |
| 977 |
} catch (IOException ioe) { |
| 978 |
Slog.e(LOG_TAG, "Failed to set API denylist exemptions", ioe); |
| 979 |
mApiDenylistExemptions = Collections.emptyList(); |
| 980 |
return false; |
| 981 |
} |
| 982 |
} |
| 983 |
|
| 984 |
private void maybeSetHiddenApiAccessLogSampleRate(ZygoteState state) { |
| 985 |
if (state == null || state.isClosed() || mHiddenApiAccessLogSampleRate == -1) { |
| 986 |
return; |
| 987 |
} |
| 988 |
|
| 989 |
try { |
| 990 |
state.mZygoteOutputWriter.write(Integer.toString(1)); |
| 991 |
state.mZygoteOutputWriter.newLine(); |
| 992 |
state.mZygoteOutputWriter.write("--hidden-api-log-sampling-rate=" |
| 993 |
+ mHiddenApiAccessLogSampleRate); |
| 994 |
state.mZygoteOutputWriter.newLine(); |
| 995 |
state.mZygoteOutputWriter.flush(); |
| 996 |
int status = state.mZygoteInputStream.readInt(); |
| 997 |
if (status != 0) { |
| 998 |
Slog.e(LOG_TAG, "Failed to set hidden API log sampling rate; status " + status); |
| 999 |
} |
| 1000 |
} catch (IOException ioe) { |
| 1001 |
Slog.e(LOG_TAG, "Failed to set hidden API log sampling rate", ioe); |
| 1002 |
} |
| 1003 |
} |
| 1004 |
|
| 1005 |
private void maybeSetHiddenApiAccessStatslogSampleRate(ZygoteState state) { |
| 1006 |
if (state == null || state.isClosed() || mHiddenApiAccessStatslogSampleRate == -1) { |
| 1007 |
return; |
| 1008 |
} |
| 1009 |
|
| 1010 |
try { |
| 1011 |
state.mZygoteOutputWriter.write(Integer.toString(1)); |
| 1012 |
state.mZygoteOutputWriter.newLine(); |
| 1013 |
state.mZygoteOutputWriter.write("--hidden-api-statslog-sampling-rate=" |
| 1014 |
+ mHiddenApiAccessStatslogSampleRate); |
| 1015 |
state.mZygoteOutputWriter.newLine(); |
| 1016 |
state.mZygoteOutputWriter.flush(); |
| 1017 |
int status = state.mZygoteInputStream.readInt(); |
| 1018 |
if (status != 0) { |
| 1019 |
Slog.e(LOG_TAG, "Failed to set hidden API statslog sampling rate; status " |
| 1020 |
+ status); |
| 1021 |
} |
| 1022 |
} catch (IOException ioe) { |
| 1023 |
Slog.e(LOG_TAG, "Failed to set hidden API statslog sampling rate", ioe); |
| 1024 |
} |
| 1025 |
} |
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
@GuardedBy("mLock") |
| 1031 |
private void attemptConnectionToPrimaryZygote() throws IOException { |
| 1032 |
if (primaryZygoteState == null || primaryZygoteState.isClosed()) { |
| 1033 |
primaryZygoteState = |
| 1034 |
ZygoteState.connect(mZygoteSocketAddress, mUsapPoolSocketAddress); |
| 1035 |
|
| 1036 |
maybeSetApiDenylistExemptions(primaryZygoteState, false); |
| 1037 |
maybeSetHiddenApiAccessLogSampleRate(primaryZygoteState); |
| 1038 |
} |
| 1039 |
} |
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
@GuardedBy("mLock") |
| 1045 |
private void attemptConnectionToSecondaryZygote() throws IOException { |
| 1046 |
if (secondaryZygoteState == null || secondaryZygoteState.isClosed()) { |
| 1047 |
secondaryZygoteState = |
| 1048 |
ZygoteState.connect(mZygoteSecondarySocketAddress, |
| 1049 |
mUsapPoolSecondarySocketAddress); |
| 1050 |
|
| 1051 |
maybeSetApiDenylistExemptions(secondaryZygoteState, false); |
| 1052 |
maybeSetHiddenApiAccessLogSampleRate(secondaryZygoteState); |
| 1053 |
} |
| 1054 |
} |
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
@GuardedBy("mLock") |
| 1063 |
private ZygoteState openZygoteSocketIfNeeded(String abi) throws ZygoteStartFailedEx { |
| 1064 |
try { |
| 1065 |
attemptConnectionToPrimaryZygote(); |
| 1066 |
|
| 1067 |
if (primaryZygoteState.matches(abi)) { |
| 1068 |
return primaryZygoteState; |
| 1069 |
} |
| 1070 |
|
| 1071 |
if (mZygoteSecondarySocketAddress != null) { |
| 1072 |
|
| 1073 |
attemptConnectionToSecondaryZygote(); |
| 1074 |
|
| 1075 |
if (secondaryZygoteState.matches(abi)) { |
| 1076 |
return secondaryZygoteState; |
| 1077 |
} |
| 1078 |
} |
| 1079 |
} catch (IOException ioe) { |
| 1080 |
throw new ZygoteStartFailedEx("Error connecting to zygote", ioe); |
| 1081 |
} |
| 1082 |
|
| 1083 |
throw new ZygoteStartFailedEx("Unsupported zygote ABI: " + abi); |
| 1084 |
} |
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 1091 |
public boolean preloadApp(ApplicationInfo appInfo, String abi) |
| 1092 |
throws ZygoteStartFailedEx, IOException { |
| 1093 |
synchronized (mLock) { |
| 1094 |
ZygoteState state = openZygoteSocketIfNeeded(abi); |
| 1095 |
state.mZygoteOutputWriter.write("2"); |
| 1096 |
state.mZygoteOutputWriter.newLine(); |
| 1097 |
|
| 1098 |
state.mZygoteOutputWriter.write("--preload-app"); |
| 1099 |
state.mZygoteOutputWriter.newLine(); |
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
Parcel parcel = Parcel.obtain(); |
| 1104 |
appInfo.writeToParcel(parcel, 0 ); |
| 1105 |
String encodedParcelData = Base64.getEncoder().encodeToString(parcel.marshall()); |
| 1106 |
parcel.recycle(); |
| 1107 |
state.mZygoteOutputWriter.write(encodedParcelData); |
| 1108 |
state.mZygoteOutputWriter.newLine(); |
| 1109 |
|
| 1110 |
state.mZygoteOutputWriter.flush(); |
| 1111 |
|
| 1112 |
return (state.mZygoteInputStream.readInt() == 0); |
| 1113 |
} |
| 1114 |
} |
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
public boolean preloadPackageForAbi( |
| 1121 |
String packagePath, String libsPath, String libFileName, String cacheKey, String abi) |
| 1122 |
throws ZygoteStartFailedEx, IOException { |
| 1123 |
synchronized (mLock) { |
| 1124 |
ZygoteState state = openZygoteSocketIfNeeded(abi); |
| 1125 |
state.mZygoteOutputWriter.write("5"); |
| 1126 |
state.mZygoteOutputWriter.newLine(); |
| 1127 |
|
| 1128 |
state.mZygoteOutputWriter.write("--preload-package"); |
| 1129 |
state.mZygoteOutputWriter.newLine(); |
| 1130 |
|
| 1131 |
state.mZygoteOutputWriter.write(packagePath); |
| 1132 |
state.mZygoteOutputWriter.newLine(); |
| 1133 |
|
| 1134 |
state.mZygoteOutputWriter.write(libsPath); |
| 1135 |
state.mZygoteOutputWriter.newLine(); |
| 1136 |
|
| 1137 |
state.mZygoteOutputWriter.write(libFileName); |
| 1138 |
state.mZygoteOutputWriter.newLine(); |
| 1139 |
|
| 1140 |
state.mZygoteOutputWriter.write(cacheKey); |
| 1141 |
state.mZygoteOutputWriter.newLine(); |
| 1142 |
|
| 1143 |
state.mZygoteOutputWriter.flush(); |
| 1144 |
|
| 1145 |
return (state.mZygoteInputStream.readInt() == 0); |
| 1146 |
} |
| 1147 |
} |
| 1148 |
|
| 1149 |
|
| 1150 |
|
| 1151 |
|
| 1152 |
|
| 1153 |
|
| 1154 |
|
| 1155 |
public boolean preloadDefault(String abi) throws ZygoteStartFailedEx, IOException { |
| 1156 |
synchronized (mLock) { |
| 1157 |
ZygoteState state = openZygoteSocketIfNeeded(abi); |
| 1158 |
|
| 1159 |
state.mZygoteOutputWriter.write("1"); |
| 1160 |
state.mZygoteOutputWriter.newLine(); |
| 1161 |
state.mZygoteOutputWriter.write("--preload-default"); |
| 1162 |
state.mZygoteOutputWriter.newLine(); |
| 1163 |
state.mZygoteOutputWriter.flush(); |
| 1164 |
|
| 1165 |
return (state.mZygoteInputStream.readInt() == 0); |
| 1166 |
} |
| 1167 |
} |
| 1168 |
|
| 1169 |
|
| 1170 |
|
| 1171 |
|
| 1172 |
|
| 1173 |
public static void waitForConnectionToZygote(String zygoteSocketName) { |
| 1174 |
final LocalSocketAddress zygoteSocketAddress = |
| 1175 |
new LocalSocketAddress(zygoteSocketName, LocalSocketAddress.Namespace.RESERVED); |
| 1176 |
waitForConnectionToZygote(zygoteSocketAddress); |
| 1177 |
} |
| 1178 |
|
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
public static void waitForConnectionToZygote(LocalSocketAddress zygoteSocketAddress) { |
| 1184 |
int numRetries = ZYGOTE_CONNECT_TIMEOUT_MS / ZYGOTE_CONNECT_RETRY_DELAY_MS; |
| 1185 |
for (int n = numRetries; n >= 0; n--) { |
| 1186 |
try { |
| 1187 |
final ZygoteState zs = |
| 1188 |
ZygoteState.connect(zygoteSocketAddress, null); |
| 1189 |
zs.close(); |
| 1190 |
return; |
| 1191 |
} catch (IOException ioe) { |
| 1192 |
Log.w(LOG_TAG, |
| 1193 |
"Got error connecting to zygote, retrying. msg= " + ioe.getMessage()); |
| 1194 |
} |
| 1195 |
|
| 1196 |
try { |
| 1197 |
Thread.sleep(ZYGOTE_CONNECT_RETRY_DELAY_MS); |
| 1198 |
} catch (InterruptedException ignored) { } |
| 1199 |
} |
| 1200 |
Slog.wtf(LOG_TAG, "Failed to connect to Zygote through socket " |
| 1201 |
+ zygoteSocketAddress.getName()); |
| 1202 |
} |
| 1203 |
|
| 1204 |
|
| 1205 |
|
| 1206 |
|
| 1207 |
|
| 1208 |
private void informZygotesOfUsapPoolStatus() { |
| 1209 |
final String command = "1\n--usap-pool-enabled=" + mUsapPoolEnabled + "\n"; |
| 1210 |
|
| 1211 |
synchronized (mLock) { |
| 1212 |
try { |
| 1213 |
attemptConnectionToPrimaryZygote(); |
| 1214 |
|
| 1215 |
primaryZygoteState.mZygoteOutputWriter.write(command); |
| 1216 |
primaryZygoteState.mZygoteOutputWriter.flush(); |
| 1217 |
} catch (IOException ioe) { |
| 1218 |
mUsapPoolEnabled = !mUsapPoolEnabled; |
| 1219 |
Log.w(LOG_TAG, "Failed to inform zygotes of USAP pool status: " |
| 1220 |
+ ioe.getMessage()); |
| 1221 |
return; |
| 1222 |
} |
| 1223 |
|
| 1224 |
if (mZygoteSecondarySocketAddress != null) { |
| 1225 |
try { |
| 1226 |
attemptConnectionToSecondaryZygote(); |
| 1227 |
|
| 1228 |
try { |
| 1229 |
secondaryZygoteState.mZygoteOutputWriter.write(command); |
| 1230 |
secondaryZygoteState.mZygoteOutputWriter.flush(); |
| 1231 |
|
| 1232 |
|
| 1233 |
secondaryZygoteState.mZygoteInputStream.readInt(); |
| 1234 |
} catch (IOException ioe) { |
| 1235 |
throw new IllegalStateException( |
| 1236 |
"USAP pool state change cause an irrecoverable error", |
| 1237 |
ioe); |
| 1238 |
} |
| 1239 |
} catch (IOException ioe) { |
| 1240 |
|
| 1241 |
} |
| 1242 |
} |
| 1243 |
|
| 1244 |
|
| 1245 |
|
| 1246 |
try { |
| 1247 |
|
| 1248 |
primaryZygoteState.mZygoteInputStream.readInt(); |
| 1249 |
} catch (IOException ioe) { |
| 1250 |
throw new IllegalStateException( |
| 1251 |
"USAP pool state change cause an irrecoverable error", |
| 1252 |
ioe); |
| 1253 |
} |
| 1254 |
} |
| 1255 |
} |
| 1256 |
|
| 1257 |
|
| 1258 |
|
| 1259 |
|
| 1260 |
|
| 1261 |
|
| 1262 |
|
| 1263 |
|
| 1264 |
|
| 1265 |
|
| 1266 |
|
| 1267 |
|
| 1268 |
|
| 1269 |
|
| 1270 |
|
| 1271 |
|
| 1272 |
|
| 1273 |
|
| 1274 |
|
| 1275 |
|
| 1276 |
|
| 1277 |
|
| 1278 |
|
| 1279 |
public ChildZygoteProcess startChildZygote(final String processClass, |
| 1280 |
final String niceName, |
| 1281 |
int uid, int gid, int[] gids, |
| 1282 |
int runtimeFlags, |
| 1283 |
String seInfo, |
| 1284 |
String abi, |
| 1285 |
String acceptedAbiList, |
| 1286 |
String instructionSet, |
| 1287 |
int uidRangeStart, |
| 1288 |
int uidRangeEnd) { |
| 1289 |
|
| 1290 |
final LocalSocketAddress serverAddress = new LocalSocketAddress( |
| 1291 |
processClass + "/" + UUID.randomUUID().toString()); |
| 1292 |
|
| 1293 |
final String[] extraArgs = {Zygote.CHILD_ZYGOTE_SOCKET_NAME_ARG + serverAddress.getName(), |
| 1294 |
Zygote.CHILD_ZYGOTE_ABI_LIST_ARG + acceptedAbiList, |
| 1295 |
Zygote.CHILD_ZYGOTE_UID_RANGE_START + uidRangeStart, |
| 1296 |
Zygote.CHILD_ZYGOTE_UID_RANGE_END + uidRangeEnd}; |
| 1297 |
|
| 1298 |
Process.ProcessStartResult result; |
| 1299 |
try { |
| 1300 |
|
| 1301 |
|
| 1302 |
result = startViaZygote(processClass, niceName, uid, gid, |
| 1303 |
gids, runtimeFlags, 0 , 0 , seInfo, |
| 1304 |
abi, instructionSet, null , null , |
| 1305 |
true , null , |
| 1306 |
ZYGOTE_POLICY_FLAG_SYSTEM_PROCESS , false , |
| 1307 |
null , null , |
| 1308 |
null , true , |
| 1309 |
false, extraArgs); |
| 1310 |
|
| 1311 |
} catch (ZygoteStartFailedEx ex) { |
| 1312 |
throw new RuntimeException("Starting child-zygote through Zygote failed", ex); |
| 1313 |
} |
| 1314 |
|
| 1315 |
return new ChildZygoteProcess(serverAddress, result.pid); |
| 1316 |
} |
| 1317 |
} |