Tobias Sargeant | b9679dc | 2016-01-19 16:34:54 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.internal.os; |
| 18 | |
| 19 | import static android.system.OsConstants.POLLIN; |
| 20 | |
| 21 | import android.net.LocalServerSocket; |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 22 | import android.net.LocalSocket; |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 23 | import android.os.SystemClock; |
| 24 | import android.os.Trace; |
| 25 | import android.provider.DeviceConfig; |
Tobias Sargeant | b9679dc | 2016-01-19 16:34:54 +0000 | [diff] [blame] | 26 | import android.system.ErrnoException; |
Chris Wailes | 2be2626 | 2019-01-11 16:14:43 -0800 | [diff] [blame] | 27 | import android.system.Os; |
Tobias Sargeant | b9679dc | 2016-01-19 16:34:54 +0000 | [diff] [blame] | 28 | import android.system.StructPollfd; |
| 29 | import android.util.Log; |
Narayan Kamath | ac0b4be | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 30 | import android.util.Slog; |
Chris Wailes | 2be2626 | 2019-01-11 16:14:43 -0800 | [diff] [blame] | 31 | |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 32 | import dalvik.system.ZygoteHooks; |
| 33 | |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 34 | import java.io.ByteArrayInputStream; |
| 35 | import java.io.DataInputStream; |
Tobias Sargeant | b9679dc | 2016-01-19 16:34:54 +0000 | [diff] [blame] | 36 | import java.io.FileDescriptor; |
Chris Wailes | 2be2626 | 2019-01-11 16:14:43 -0800 | [diff] [blame] | 37 | import java.io.IOException; |
Tobias Sargeant | b9679dc | 2016-01-19 16:34:54 +0000 | [diff] [blame] | 38 | import java.util.ArrayList; |
| 39 | |
| 40 | /** |
| 41 | * Server socket class for zygote processes. |
| 42 | * |
| 43 | * Provides functions to wait for commands on a UNIX domain socket, and fork |
| 44 | * off child processes that inherit the initial state of the VM.% |
| 45 | * |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 46 | * Please see {@link ZygoteArguments} for documentation on the |
Tobias Sargeant | b9679dc | 2016-01-19 16:34:54 +0000 | [diff] [blame] | 47 | * client protocol. |
| 48 | */ |
| 49 | class ZygoteServer { |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 50 | // TODO (chriswailes): Change this so it is set with Zygote or ZygoteSecondary as appropriate |
Tobias Sargeant | b9679dc | 2016-01-19 16:34:54 +0000 | [diff] [blame] | 51 | public static final String TAG = "ZygoteServer"; |
| 52 | |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 53 | /** |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 54 | * The maximim value that will be accepted from the BLASTULA_POOL_SIZE_MAX device property. |
| 55 | * is a mirror of BLASTULA_POOL_MAX_LIMIT found in com_android_internal_os_Zygote.cpp. |
| 56 | */ |
| 57 | private static final int BLASTULA_POOL_SIZE_MAX_LIMIT = 100; |
| 58 | |
| 59 | /** |
| 60 | * The minimum value that will be accepted from the BLASTULA_POOL_SIZE_MIN device property. |
| 61 | */ |
| 62 | private static final int BLASTULA_POOL_SIZE_MIN_LIMIT = 1; |
| 63 | |
| 64 | /** The default value used for the BLASTULA_POOL_SIZE_MAX device property */ |
| 65 | private static final String BLASTULA_POOL_SIZE_MAX_DEFAULT = "10"; |
| 66 | |
| 67 | /** The default value used for the BLASTULA_POOL_SIZE_MIN device property */ |
| 68 | private static final String BLASTULA_POOL_SIZE_MIN_DEFAULT = "1"; |
| 69 | |
| 70 | /** |
Chris Wailes | ae93714 | 2019-01-24 12:57:33 -0800 | [diff] [blame] | 71 | * Indicates if this Zygote server can support a blastula pool. Currently this should only be |
| 72 | * true for the primary and secondary Zygotes, and not the App Zygotes or the WebView Zygote. |
| 73 | * |
| 74 | * TODO (chriswailes): Make this an explicit argument to the constructor |
| 75 | */ |
| 76 | |
| 77 | private final boolean mBlastulaPoolSupported; |
| 78 | |
| 79 | /** |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 80 | * If the blastula pool should be created and used to start applications. |
| 81 | * |
| 82 | * Setting this value to false will disable the creation, maintenance, and use of the blastula |
| 83 | * pool. When the blastula pool is disabled the application lifecycle will be identical to |
| 84 | * previous versions of Android. |
| 85 | */ |
| 86 | private boolean mBlastulaPoolEnabled = false; |
| 87 | |
| 88 | /** |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 89 | * Listening socket that accepts new server connections. |
| 90 | */ |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 91 | private LocalServerSocket mZygoteSocket; |
Tobias Sargeant | b9679dc | 2016-01-19 16:34:54 +0000 | [diff] [blame] | 92 | |
Narayan Kamath | ac0b4be | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 93 | /** |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 94 | * The name of the blastula socket to use if the blastula pool is enabled. |
| 95 | */ |
| 96 | private LocalServerSocket mBlastulaPoolSocket; |
| 97 | |
| 98 | /** |
| 99 | * File descriptor used for communication between the signal handler and the ZygoteServer poll |
| 100 | * loop. |
| 101 | * */ |
| 102 | private FileDescriptor mBlastulaPoolEventFD; |
| 103 | |
| 104 | /** |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 105 | * Whether or not mZygoteSocket's underlying FD should be closed directly. |
| 106 | * If mZygoteSocket is created with an existing FD, closing the socket does |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 107 | * not close the FD and it must be closed explicitly. If the socket is created |
| 108 | * with a name instead, then closing the socket will close the underlying FD |
| 109 | * and it should not be double-closed. |
| 110 | */ |
| 111 | private boolean mCloseSocketFd; |
| 112 | |
| 113 | /** |
Narayan Kamath | ac0b4be | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 114 | * Set by the child process, immediately after a call to {@code Zygote.forkAndSpecialize}. |
| 115 | */ |
| 116 | private boolean mIsForkChild; |
| 117 | |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 118 | /** |
| 119 | * The runtime-adjustable maximum Blastula pool size. |
| 120 | */ |
| 121 | private int mBlastulaPoolSizeMax = 0; |
Tobias Sargeant | b9679dc | 2016-01-19 16:34:54 +0000 | [diff] [blame] | 122 | |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 123 | /** |
| 124 | * The runtime-adjustable minimum Blastula pool size. |
| 125 | */ |
| 126 | private int mBlastulaPoolSizeMin = 0; |
| 127 | |
| 128 | /** |
| 129 | * The runtime-adjustable value used to determine when to re-fill the |
| 130 | * blastula pool. The pool will be re-filled when |
| 131 | * (sBlastulaPoolMax - gBlastulaPoolCount) >= sBlastulaPoolRefillThreshold. |
| 132 | */ |
| 133 | private int mBlastulaPoolRefillThreshold = 0; |
| 134 | |
| 135 | ZygoteServer() { |
| 136 | mBlastulaPoolEventFD = null; |
| 137 | mZygoteSocket = null; |
| 138 | mBlastulaPoolSocket = null; |
Chris Wailes | ae93714 | 2019-01-24 12:57:33 -0800 | [diff] [blame] | 139 | |
| 140 | mBlastulaPoolSupported = false; |
Narayan Kamath | ac0b4be | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 141 | } |
| 142 | |
Tobias Sargeant | b9679dc | 2016-01-19 16:34:54 +0000 | [diff] [blame] | 143 | /** |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 144 | * Initialize the Zygote server with the Zygote server socket, blastula pool server socket, |
| 145 | * and blastula pool event FD. |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 146 | * |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 147 | * @param isPrimaryZygote If this is the primary Zygote or not. |
Tobias Sargeant | b9679dc | 2016-01-19 16:34:54 +0000 | [diff] [blame] | 148 | */ |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 149 | ZygoteServer(boolean isPrimaryZygote) { |
| 150 | mBlastulaPoolEventFD = Zygote.getBlastulaPoolEventFD(); |
| 151 | |
| 152 | if (isPrimaryZygote) { |
| 153 | mZygoteSocket = Zygote.createManagedSocketFromInitSocket(Zygote.PRIMARY_SOCKET_NAME); |
| 154 | mBlastulaPoolSocket = |
| 155 | Zygote.createManagedSocketFromInitSocket( |
| 156 | Zygote.BLASTULA_POOL_PRIMARY_SOCKET_NAME); |
| 157 | } else { |
| 158 | mZygoteSocket = Zygote.createManagedSocketFromInitSocket(Zygote.SECONDARY_SOCKET_NAME); |
| 159 | mBlastulaPoolSocket = |
| 160 | Zygote.createManagedSocketFromInitSocket( |
| 161 | Zygote.BLASTULA_POOL_SECONDARY_SOCKET_NAME); |
Tobias Sargeant | b9679dc | 2016-01-19 16:34:54 +0000 | [diff] [blame] | 162 | } |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 163 | |
| 164 | fetchBlastulaPoolPolicyProps(); |
Chris Wailes | ae93714 | 2019-01-24 12:57:33 -0800 | [diff] [blame] | 165 | |
| 166 | mBlastulaPoolSupported = true; |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | void setForkChild() { |
| 170 | mIsForkChild = true; |
Tobias Sargeant | b9679dc | 2016-01-19 16:34:54 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Chris Wailes | ae93714 | 2019-01-24 12:57:33 -0800 | [diff] [blame] | 173 | public boolean isBlastulaPoolEnabled() { |
| 174 | return mBlastulaPoolEnabled; |
| 175 | } |
| 176 | |
Tobias Sargeant | b9679dc | 2016-01-19 16:34:54 +0000 | [diff] [blame] | 177 | /** |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 178 | * Registers a server socket for zygote command connections. This opens the server socket |
| 179 | * at the specified name in the abstract socket namespace. |
| 180 | */ |
| 181 | void registerServerSocketAtAbstractName(String socketName) { |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 182 | if (mZygoteSocket == null) { |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 183 | try { |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 184 | mZygoteSocket = new LocalServerSocket(socketName); |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 185 | mCloseSocketFd = false; |
| 186 | } catch (IOException ex) { |
| 187 | throw new RuntimeException( |
| 188 | "Error binding to abstract socket '" + socketName + "'", ex); |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | /** |
Tobias Sargeant | b9679dc | 2016-01-19 16:34:54 +0000 | [diff] [blame] | 194 | * Waits for and accepts a single command connection. Throws |
| 195 | * RuntimeException on failure. |
| 196 | */ |
| 197 | private ZygoteConnection acceptCommandPeer(String abiList) { |
| 198 | try { |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 199 | return createNewConnection(mZygoteSocket.accept(), abiList); |
Tobias Sargeant | b9679dc | 2016-01-19 16:34:54 +0000 | [diff] [blame] | 200 | } catch (IOException ex) { |
| 201 | throw new RuntimeException( |
| 202 | "IOException during accept()", ex); |
| 203 | } |
| 204 | } |
| 205 | |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 206 | protected ZygoteConnection createNewConnection(LocalSocket socket, String abiList) |
| 207 | throws IOException { |
| 208 | return new ZygoteConnection(socket, abiList); |
| 209 | } |
| 210 | |
Tobias Sargeant | b9679dc | 2016-01-19 16:34:54 +0000 | [diff] [blame] | 211 | /** |
| 212 | * Close and clean up zygote sockets. Called on shutdown and on the |
| 213 | * child's exit path. |
| 214 | */ |
| 215 | void closeServerSocket() { |
| 216 | try { |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 217 | if (mZygoteSocket != null) { |
| 218 | FileDescriptor fd = mZygoteSocket.getFileDescriptor(); |
| 219 | mZygoteSocket.close(); |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 220 | if (fd != null && mCloseSocketFd) { |
Tobias Sargeant | b9679dc | 2016-01-19 16:34:54 +0000 | [diff] [blame] | 221 | Os.close(fd); |
| 222 | } |
| 223 | } |
| 224 | } catch (IOException ex) { |
| 225 | Log.e(TAG, "Zygote: error closing sockets", ex); |
| 226 | } catch (ErrnoException ex) { |
| 227 | Log.e(TAG, "Zygote: error closing descriptor", ex); |
| 228 | } |
| 229 | |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 230 | mZygoteSocket = null; |
Tobias Sargeant | b9679dc | 2016-01-19 16:34:54 +0000 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Return the server socket's underlying file descriptor, so that |
| 235 | * ZygoteConnection can pass it to the native code for proper |
| 236 | * closure after a child process is forked off. |
| 237 | */ |
| 238 | |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 239 | FileDescriptor getZygoteSocketFileDescriptor() { |
| 240 | return mZygoteSocket.getFileDescriptor(); |
Tobias Sargeant | b9679dc | 2016-01-19 16:34:54 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 243 | private void fetchBlastulaPoolPolicyProps() { |
Chris Wailes | db132a3 | 2019-02-20 10:49:27 -0800 | [diff] [blame] | 244 | if (mBlastulaPoolSupported) { |
| 245 | final String blastulaPoolSizeMaxPropString = |
| 246 | Zygote.getSystemProperty( |
| 247 | DeviceConfig.RuntimeNative.BLASTULA_POOL_SIZE_MAX, |
| 248 | BLASTULA_POOL_SIZE_MAX_DEFAULT); |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 249 | |
Chris Wailes | db132a3 | 2019-02-20 10:49:27 -0800 | [diff] [blame] | 250 | if (!blastulaPoolSizeMaxPropString.isEmpty()) { |
| 251 | mBlastulaPoolSizeMax = |
| 252 | Integer.min( |
| 253 | Integer.parseInt(blastulaPoolSizeMaxPropString), |
| 254 | BLASTULA_POOL_SIZE_MAX_LIMIT); |
| 255 | } |
| 256 | |
| 257 | final String blastulaPoolSizeMinPropString = |
| 258 | Zygote.getSystemProperty( |
| 259 | DeviceConfig.RuntimeNative.BLASTULA_POOL_SIZE_MIN, |
| 260 | BLASTULA_POOL_SIZE_MIN_DEFAULT); |
| 261 | |
| 262 | if (!blastulaPoolSizeMinPropString.isEmpty()) { |
| 263 | mBlastulaPoolSizeMin = |
| 264 | Integer.max( |
| 265 | Integer.parseInt(blastulaPoolSizeMinPropString), |
| 266 | BLASTULA_POOL_SIZE_MIN_LIMIT); |
| 267 | } |
| 268 | |
| 269 | final String blastulaPoolRefillThresholdPropString = |
| 270 | Zygote.getSystemProperty( |
| 271 | DeviceConfig.RuntimeNative.BLASTULA_POOL_REFILL_THRESHOLD, |
| 272 | Integer.toString(mBlastulaPoolSizeMax / 2)); |
| 273 | |
| 274 | if (!blastulaPoolRefillThresholdPropString.isEmpty()) { |
| 275 | mBlastulaPoolRefillThreshold = |
| 276 | Integer.min( |
| 277 | Integer.parseInt(blastulaPoolRefillThresholdPropString), |
| 278 | mBlastulaPoolSizeMax); |
| 279 | } |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 280 | } |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | private long mLastPropCheckTimestamp = 0; |
| 284 | |
| 285 | private void fetchBlastulaPoolPolicyPropsWithMinInterval() { |
| 286 | final long currentTimestamp = SystemClock.elapsedRealtime(); |
| 287 | |
| 288 | if (currentTimestamp - mLastPropCheckTimestamp >= Zygote.PROPERTY_CHECK_INTERVAL) { |
| 289 | fetchBlastulaPoolPolicyProps(); |
| 290 | mLastPropCheckTimestamp = currentTimestamp; |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * Checks to see if the current policy says that pool should be refilled, and spawns new |
| 296 | * blastulas if necessary. |
| 297 | * |
| 298 | * @param sessionSocketRawFDs Anonymous session sockets that are currently open |
| 299 | * @return In the Zygote process this function will always return null; in blastula processes |
| 300 | * this function will return a Runnable object representing the new application that is |
| 301 | * passed up from blastulaMain. |
| 302 | */ |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 303 | |
Chris Wailes | ae93714 | 2019-01-24 12:57:33 -0800 | [diff] [blame] | 304 | Runnable fillBlastulaPool(int[] sessionSocketRawFDs) { |
| 305 | Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "Zygote:FillBlastulaPool"); |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 306 | |
Chris Wailes | ae93714 | 2019-01-24 12:57:33 -0800 | [diff] [blame] | 307 | int blastulaPoolCount = Zygote.getBlastulaPoolCount(); |
| 308 | int numBlastulasToSpawn = mBlastulaPoolSizeMax - blastulaPoolCount; |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 309 | |
Chris Wailes | ae93714 | 2019-01-24 12:57:33 -0800 | [diff] [blame] | 310 | if (blastulaPoolCount < mBlastulaPoolSizeMin |
| 311 | || numBlastulasToSpawn >= mBlastulaPoolRefillThreshold) { |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 312 | |
Chris Wailes | ae93714 | 2019-01-24 12:57:33 -0800 | [diff] [blame] | 313 | // Disable some VM functionality and reset some system values |
| 314 | // before forking. |
| 315 | ZygoteHooks.preFork(); |
| 316 | Zygote.resetNicePriority(); |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 317 | |
Chris Wailes | ae93714 | 2019-01-24 12:57:33 -0800 | [diff] [blame] | 318 | while (blastulaPoolCount++ < mBlastulaPoolSizeMax) { |
| 319 | Runnable caller = Zygote.forkBlastula(mBlastulaPoolSocket, sessionSocketRawFDs); |
| 320 | |
| 321 | if (caller != null) { |
| 322 | return caller; |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 323 | } |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 324 | } |
| 325 | |
Chris Wailes | ae93714 | 2019-01-24 12:57:33 -0800 | [diff] [blame] | 326 | // Re-enable runtime services for the Zygote. Blastula services |
| 327 | // are re-enabled in specializeBlastula. |
| 328 | ZygoteHooks.postForkCommon(); |
| 329 | |
| 330 | Log.i("zygote", |
| 331 | "Filled the blastula pool. New blastulas: " + numBlastulasToSpawn); |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 332 | } |
| 333 | |
Chris Wailes | ae93714 | 2019-01-24 12:57:33 -0800 | [diff] [blame] | 334 | Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER); |
| 335 | |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 336 | return null; |
| 337 | } |
| 338 | |
Tobias Sargeant | b9679dc | 2016-01-19 16:34:54 +0000 | [diff] [blame] | 339 | /** |
Chris Wailes | db132a3 | 2019-02-20 10:49:27 -0800 | [diff] [blame] | 340 | * Empty or fill the blastula pool as dictated by the current and new blastula pool statuses. |
| 341 | */ |
| 342 | Runnable setBlastulaPoolStatus(boolean newStatus, LocalSocket sessionSocket) { |
| 343 | if (!mBlastulaPoolSupported) { |
| 344 | Log.w(TAG, |
| 345 | "Attempting to enable a blastula pool for a Zygote that doesn't support it."); |
| 346 | return null; |
| 347 | } else if (mBlastulaPoolEnabled == newStatus) { |
| 348 | return null; |
| 349 | } |
| 350 | |
| 351 | mBlastulaPoolEnabled = newStatus; |
| 352 | |
| 353 | if (newStatus) { |
| 354 | return fillBlastulaPool(new int[]{ sessionSocket.getFileDescriptor().getInt$() }); |
| 355 | } else { |
| 356 | Zygote.emptyBlastulaPool(); |
| 357 | return null; |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | /** |
Tobias Sargeant | b9679dc | 2016-01-19 16:34:54 +0000 | [diff] [blame] | 362 | * Runs the zygote process's select loop. Accepts new connections as |
| 363 | * they happen, and reads commands from connections one spawn-request's |
| 364 | * worth at a time. |
Tobias Sargeant | b9679dc | 2016-01-19 16:34:54 +0000 | [diff] [blame] | 365 | */ |
Narayan Kamath | ac0b4be | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 366 | Runnable runSelectLoop(String abiList) { |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 367 | ArrayList<FileDescriptor> socketFDs = new ArrayList<FileDescriptor>(); |
Tobias Sargeant | b9679dc | 2016-01-19 16:34:54 +0000 | [diff] [blame] | 368 | ArrayList<ZygoteConnection> peers = new ArrayList<ZygoteConnection>(); |
| 369 | |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 370 | socketFDs.add(mZygoteSocket.getFileDescriptor()); |
Tobias Sargeant | b9679dc | 2016-01-19 16:34:54 +0000 | [diff] [blame] | 371 | peers.add(null); |
| 372 | |
| 373 | while (true) { |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 374 | fetchBlastulaPoolPolicyPropsWithMinInterval(); |
| 375 | |
Chris Wailes | ae93714 | 2019-01-24 12:57:33 -0800 | [diff] [blame] | 376 | int[] blastulaPipeFDs = null; |
| 377 | StructPollfd[] pollFDs = null; |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 378 | |
Chris Wailes | ae93714 | 2019-01-24 12:57:33 -0800 | [diff] [blame] | 379 | // Allocate enough space for the poll structs, taking into account |
| 380 | // the state of the blastula pool for this Zygote (could be a |
| 381 | // regular Zygote, a WebView Zygote, or an AppZygote). |
| 382 | if (mBlastulaPoolEnabled) { |
| 383 | blastulaPipeFDs = Zygote.getBlastulaPipeFDs(); |
| 384 | pollFDs = new StructPollfd[socketFDs.size() + 1 + blastulaPipeFDs.length]; |
| 385 | } else { |
| 386 | pollFDs = new StructPollfd[socketFDs.size()]; |
| 387 | } |
| 388 | |
| 389 | /* |
| 390 | * For reasons of correctness the blastula pool pipe and event FDs |
| 391 | * must be processed before the session and server sockets. This |
| 392 | * is to ensure that the blastula pool accounting information is |
| 393 | * accurate when handling other requests like API blacklist |
| 394 | * exemptions. |
| 395 | */ |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 396 | |
| 397 | int pollIndex = 0; |
| 398 | for (FileDescriptor socketFD : socketFDs) { |
| 399 | pollFDs[pollIndex] = new StructPollfd(); |
| 400 | pollFDs[pollIndex].fd = socketFD; |
| 401 | pollFDs[pollIndex].events = (short) POLLIN; |
| 402 | ++pollIndex; |
Tobias Sargeant | b9679dc | 2016-01-19 16:34:54 +0000 | [diff] [blame] | 403 | } |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 404 | |
| 405 | final int blastulaPoolEventFDIndex = pollIndex; |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 406 | |
Chris Wailes | ae93714 | 2019-01-24 12:57:33 -0800 | [diff] [blame] | 407 | if (mBlastulaPoolEnabled) { |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 408 | pollFDs[pollIndex] = new StructPollfd(); |
Chris Wailes | ae93714 | 2019-01-24 12:57:33 -0800 | [diff] [blame] | 409 | pollFDs[pollIndex].fd = mBlastulaPoolEventFD; |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 410 | pollFDs[pollIndex].events = (short) POLLIN; |
| 411 | ++pollIndex; |
Chris Wailes | ae93714 | 2019-01-24 12:57:33 -0800 | [diff] [blame] | 412 | |
| 413 | for (int blastulaPipeFD : blastulaPipeFDs) { |
| 414 | FileDescriptor managedFd = new FileDescriptor(); |
| 415 | managedFd.setInt$(blastulaPipeFD); |
| 416 | |
| 417 | pollFDs[pollIndex] = new StructPollfd(); |
| 418 | pollFDs[pollIndex].fd = managedFd; |
| 419 | pollFDs[pollIndex].events = (short) POLLIN; |
| 420 | ++pollIndex; |
| 421 | } |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 422 | } |
| 423 | |
Tobias Sargeant | b9679dc | 2016-01-19 16:34:54 +0000 | [diff] [blame] | 424 | try { |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 425 | Os.poll(pollFDs, -1); |
Tobias Sargeant | b9679dc | 2016-01-19 16:34:54 +0000 | [diff] [blame] | 426 | } catch (ErrnoException ex) { |
| 427 | throw new RuntimeException("poll failed", ex); |
| 428 | } |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 429 | |
Chris Wailes | ae93714 | 2019-01-24 12:57:33 -0800 | [diff] [blame] | 430 | boolean blastulaPoolFDRead = false; |
| 431 | |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 432 | while (--pollIndex >= 0) { |
| 433 | if ((pollFDs[pollIndex].revents & POLLIN) == 0) { |
Tobias Sargeant | b9679dc | 2016-01-19 16:34:54 +0000 | [diff] [blame] | 434 | continue; |
| 435 | } |
Narayan Kamath | ac0b4be | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 436 | |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 437 | if (pollIndex == 0) { |
| 438 | // Zygote server socket |
| 439 | |
Tobias Sargeant | b9679dc | 2016-01-19 16:34:54 +0000 | [diff] [blame] | 440 | ZygoteConnection newPeer = acceptCommandPeer(abiList); |
| 441 | peers.add(newPeer); |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 442 | socketFDs.add(newPeer.getFileDescriptor()); |
| 443 | |
| 444 | } else if (pollIndex < blastulaPoolEventFDIndex) { |
| 445 | // Session socket accepted from the Zygote server socket |
| 446 | |
Narayan Kamath | ac0b4be | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 447 | try { |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 448 | ZygoteConnection connection = peers.get(pollIndex); |
Narayan Kamath | ac0b4be | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 449 | final Runnable command = connection.processOneCommand(this); |
| 450 | |
Chris Wailes | db132a3 | 2019-02-20 10:49:27 -0800 | [diff] [blame] | 451 | // TODO (chriswailes): Is this extra check necessary? |
Narayan Kamath | ac0b4be | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 452 | if (mIsForkChild) { |
| 453 | // We're in the child. We should always have a command to run at this |
| 454 | // stage if processOneCommand hasn't called "exec". |
| 455 | if (command == null) { |
| 456 | throw new IllegalStateException("command == null"); |
| 457 | } |
| 458 | |
| 459 | return command; |
| 460 | } else { |
| 461 | // We're in the server - we should never have any commands to run. |
| 462 | if (command != null) { |
| 463 | throw new IllegalStateException("command != null"); |
| 464 | } |
| 465 | |
| 466 | // We don't know whether the remote side of the socket was closed or |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 467 | // not until we attempt to read from it from processOneCommand. This |
| 468 | // shows up as a regular POLLIN event in our regular processing loop. |
Narayan Kamath | ac0b4be | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 469 | if (connection.isClosedByPeer()) { |
| 470 | connection.closeSocket(); |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 471 | peers.remove(pollIndex); |
| 472 | socketFDs.remove(pollIndex); |
Narayan Kamath | ac0b4be | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 473 | } |
| 474 | } |
| 475 | } catch (Exception e) { |
| 476 | if (!mIsForkChild) { |
| 477 | // We're in the server so any exception here is one that has taken place |
| 478 | // pre-fork while processing commands or reading / writing from the |
| 479 | // control socket. Make a loud noise about any such exceptions so that |
| 480 | // we know exactly what failed and why. |
| 481 | |
| 482 | Slog.e(TAG, "Exception executing zygote command: ", e); |
| 483 | |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 484 | // Make sure the socket is closed so that the other end knows |
| 485 | // immediately that something has gone wrong and doesn't time out |
| 486 | // waiting for a response. |
| 487 | ZygoteConnection conn = peers.remove(pollIndex); |
Narayan Kamath | ac0b4be | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 488 | conn.closeSocket(); |
| 489 | |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 490 | socketFDs.remove(pollIndex); |
Narayan Kamath | ac0b4be | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 491 | } else { |
| 492 | // We're in the child so any exception caught here has happened post |
| 493 | // fork and before we execute ActivityThread.main (or any other main() |
| 494 | // method). Log the details of the exception and bring down the process. |
| 495 | Log.e(TAG, "Caught post-fork exception in child process.", e); |
| 496 | throw e; |
| 497 | } |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 498 | } finally { |
| 499 | // Reset the child flag, in the event that the child process is a child- |
| 500 | // zygote. The flag will not be consulted this loop pass after the Runnable |
| 501 | // is returned. |
| 502 | mIsForkChild = false; |
Tobias Sargeant | b9679dc | 2016-01-19 16:34:54 +0000 | [diff] [blame] | 503 | } |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 504 | } else { |
| 505 | // Either the blastula pool event FD or a blastula reporting pipe. |
| 506 | |
| 507 | // If this is the event FD the payload will be the number of blastulas removed. |
| 508 | // If this is a reporting pipe FD the payload will be the PID of the blastula |
| 509 | // that was just specialized. |
| 510 | long messagePayload = -1; |
| 511 | |
| 512 | try { |
| 513 | byte[] buffer = new byte[Zygote.BLASTULA_MANAGEMENT_MESSAGE_BYTES]; |
| 514 | int readBytes = Os.read(pollFDs[pollIndex].fd, buffer, 0, buffer.length); |
| 515 | |
| 516 | if (readBytes == Zygote.BLASTULA_MANAGEMENT_MESSAGE_BYTES) { |
| 517 | DataInputStream inputStream = |
| 518 | new DataInputStream(new ByteArrayInputStream(buffer)); |
| 519 | |
| 520 | messagePayload = inputStream.readLong(); |
| 521 | } else { |
| 522 | Log.e(TAG, "Incomplete read from blastula management FD of size " |
| 523 | + readBytes); |
| 524 | continue; |
| 525 | } |
| 526 | } catch (Exception ex) { |
| 527 | if (pollIndex == blastulaPoolEventFDIndex) { |
| 528 | Log.e(TAG, "Failed to read from blastula pool event FD: " |
| 529 | + ex.getMessage()); |
| 530 | } else { |
| 531 | Log.e(TAG, "Failed to read from blastula reporting pipe: " |
| 532 | + ex.getMessage()); |
| 533 | } |
| 534 | |
| 535 | continue; |
| 536 | } |
| 537 | |
| 538 | if (pollIndex > blastulaPoolEventFDIndex) { |
| 539 | Zygote.removeBlastulaTableEntry((int) messagePayload); |
| 540 | } |
| 541 | |
Chris Wailes | ae93714 | 2019-01-24 12:57:33 -0800 | [diff] [blame] | 542 | blastulaPoolFDRead = true; |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | // Check to see if the blastula pool needs to be refilled. |
| 547 | if (blastulaPoolFDRead) { |
| 548 | int[] sessionSocketRawFDs = |
| 549 | socketFDs.subList(1, socketFDs.size()) |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 550 | .stream() |
| 551 | .mapToInt(fd -> fd.getInt$()) |
| 552 | .toArray(); |
| 553 | |
Chris Wailes | ae93714 | 2019-01-24 12:57:33 -0800 | [diff] [blame] | 554 | final Runnable command = fillBlastulaPool(sessionSocketRawFDs); |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 555 | |
Chris Wailes | ae93714 | 2019-01-24 12:57:33 -0800 | [diff] [blame] | 556 | if (command != null) { |
| 557 | return command; |
Tobias Sargeant | b9679dc | 2016-01-19 16:34:54 +0000 | [diff] [blame] | 558 | } |
| 559 | } |
| 560 | } |
| 561 | } |
| 562 | } |