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 | /** |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 54 | * The maximim value that will be accepted from the USAP_POOL_SIZE_MAX device property. |
| 55 | * is a mirror of USAP_POOL_MAX_LIMIT found in com_android_internal_os_Zygote.cpp. |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 56 | */ |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 57 | private static final int USAP_POOL_SIZE_MAX_LIMIT = 100; |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 58 | |
| 59 | /** |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 60 | * The minimum value that will be accepted from the USAP_POOL_SIZE_MIN device property. |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 61 | */ |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 62 | private static final int USAP_POOL_SIZE_MIN_LIMIT = 1; |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 63 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 64 | /** The default value used for the USAP_POOL_SIZE_MAX device property */ |
| 65 | private static final String USAP_POOL_SIZE_MAX_DEFAULT = "10"; |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 66 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 67 | /** The default value used for the USAP_POOL_SIZE_MIN device property */ |
| 68 | private static final String USAP_POOL_SIZE_MIN_DEFAULT = "1"; |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 69 | |
| 70 | /** |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 71 | * Indicates if this Zygote server can support a unspecialized app process pool. Currently this |
| 72 | * should only be true for the primary and secondary Zygotes, and not the App Zygotes or the |
| 73 | * WebView Zygote. |
Chris Wailes | ae93714 | 2019-01-24 12:57:33 -0800 | [diff] [blame] | 74 | * |
| 75 | * TODO (chriswailes): Make this an explicit argument to the constructor |
| 76 | */ |
| 77 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 78 | private final boolean mUsapPoolSupported; |
Chris Wailes | ae93714 | 2019-01-24 12:57:33 -0800 | [diff] [blame] | 79 | |
| 80 | /** |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 81 | * If the unspecialized app process pool should be created and used to start applications. |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 82 | * |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 83 | * Setting this value to false will disable the creation, maintenance, and use of the USAP |
| 84 | * pool. When the USAP pool is disabled the application lifecycle will be identical to |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 85 | * previous versions of Android. |
| 86 | */ |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 87 | private boolean mUsapPoolEnabled = false; |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 88 | |
| 89 | /** |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 90 | * Listening socket that accepts new server connections. |
| 91 | */ |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 92 | private LocalServerSocket mZygoteSocket; |
Tobias Sargeant | b9679dc | 2016-01-19 16:34:54 +0000 | [diff] [blame] | 93 | |
Narayan Kamath | ac0b4be | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 94 | /** |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 95 | * The name of the unspecialized app process pool socket to use if the USAP pool is enabled. |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 96 | */ |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 97 | private LocalServerSocket mUsapPoolSocket; |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 98 | |
| 99 | /** |
| 100 | * File descriptor used for communication between the signal handler and the ZygoteServer poll |
| 101 | * loop. |
| 102 | * */ |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 103 | private FileDescriptor mUsapPoolEventFD; |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 104 | |
| 105 | /** |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 106 | * Whether or not mZygoteSocket's underlying FD should be closed directly. |
| 107 | * If mZygoteSocket is created with an existing FD, closing the socket does |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 108 | * not close the FD and it must be closed explicitly. If the socket is created |
| 109 | * with a name instead, then closing the socket will close the underlying FD |
| 110 | * and it should not be double-closed. |
| 111 | */ |
| 112 | private boolean mCloseSocketFd; |
| 113 | |
| 114 | /** |
Narayan Kamath | ac0b4be | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 115 | * Set by the child process, immediately after a call to {@code Zygote.forkAndSpecialize}. |
| 116 | */ |
| 117 | private boolean mIsForkChild; |
| 118 | |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 119 | /** |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 120 | * The runtime-adjustable maximum USAP pool size. |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 121 | */ |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 122 | private int mUsapPoolSizeMax = 0; |
Tobias Sargeant | b9679dc | 2016-01-19 16:34:54 +0000 | [diff] [blame] | 123 | |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 124 | /** |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 125 | * The runtime-adjustable minimum USAP pool size. |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 126 | */ |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 127 | private int mUsapPoolSizeMin = 0; |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 128 | |
| 129 | /** |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 130 | * The runtime-adjustable value used to determine when to re-fill the USAP pool. The pool will |
| 131 | * be re-filled when (mUsapPoolMax - gUsapPoolCount) >= sUsapPoolRefillThreshold. |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 132 | */ |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 133 | private int mUsapPoolRefillThreshold = 0; |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 134 | |
| 135 | ZygoteServer() { |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 136 | mUsapPoolEventFD = null; |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 137 | mZygoteSocket = null; |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 138 | mUsapPoolSocket = null; |
Chris Wailes | ae93714 | 2019-01-24 12:57:33 -0800 | [diff] [blame] | 139 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 140 | mUsapPoolSupported = 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 | /** |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 144 | * Initialize the Zygote server with the Zygote server socket, USAP pool server socket, and USAP |
| 145 | * 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) { |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 150 | mUsapPoolEventFD = Zygote.getUsapPoolEventFD(); |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 151 | |
| 152 | if (isPrimaryZygote) { |
| 153 | mZygoteSocket = Zygote.createManagedSocketFromInitSocket(Zygote.PRIMARY_SOCKET_NAME); |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 154 | mUsapPoolSocket = |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 155 | Zygote.createManagedSocketFromInitSocket( |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 156 | Zygote.USAP_POOL_PRIMARY_SOCKET_NAME); |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 157 | } else { |
| 158 | mZygoteSocket = Zygote.createManagedSocketFromInitSocket(Zygote.SECONDARY_SOCKET_NAME); |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 159 | mUsapPoolSocket = |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 160 | Zygote.createManagedSocketFromInitSocket( |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 161 | Zygote.USAP_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 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 164 | fetchUsapPoolPolicyProps(); |
Chris Wailes | ae93714 | 2019-01-24 12:57:33 -0800 | [diff] [blame] | 165 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 166 | mUsapPoolSupported = 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 | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 173 | public boolean isUsapPoolEnabled() { |
| 174 | return mUsapPoolEnabled; |
Chris Wailes | ae93714 | 2019-01-24 12:57:33 -0800 | [diff] [blame] | 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 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 243 | private void fetchUsapPoolPolicyProps() { |
| 244 | if (mUsapPoolSupported) { |
| 245 | final String usapPoolSizeMaxPropString = |
Chris Wailes | db132a3 | 2019-02-20 10:49:27 -0800 | [diff] [blame] | 246 | Zygote.getSystemProperty( |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 247 | DeviceConfig.RuntimeNative.USAP_POOL_SIZE_MAX, |
| 248 | USAP_POOL_SIZE_MAX_DEFAULT); |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 249 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 250 | if (!usapPoolSizeMaxPropString.isEmpty()) { |
| 251 | mUsapPoolSizeMax = |
Chris Wailes | db132a3 | 2019-02-20 10:49:27 -0800 | [diff] [blame] | 252 | Integer.min( |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 253 | Integer.parseInt(usapPoolSizeMaxPropString), |
| 254 | USAP_POOL_SIZE_MAX_LIMIT); |
Chris Wailes | db132a3 | 2019-02-20 10:49:27 -0800 | [diff] [blame] | 255 | } |
| 256 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 257 | final String usapPoolSizeMinPropString = |
Chris Wailes | db132a3 | 2019-02-20 10:49:27 -0800 | [diff] [blame] | 258 | Zygote.getSystemProperty( |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 259 | DeviceConfig.RuntimeNative.USAP_POOL_SIZE_MIN, |
| 260 | USAP_POOL_SIZE_MIN_DEFAULT); |
Chris Wailes | db132a3 | 2019-02-20 10:49:27 -0800 | [diff] [blame] | 261 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 262 | if (!usapPoolSizeMinPropString.isEmpty()) { |
| 263 | mUsapPoolSizeMin = |
Chris Wailes | db132a3 | 2019-02-20 10:49:27 -0800 | [diff] [blame] | 264 | Integer.max( |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 265 | Integer.parseInt(usapPoolSizeMinPropString), |
| 266 | USAP_POOL_SIZE_MIN_LIMIT); |
Chris Wailes | db132a3 | 2019-02-20 10:49:27 -0800 | [diff] [blame] | 267 | } |
| 268 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 269 | final String usapPoolRefillThresholdPropString = |
Chris Wailes | db132a3 | 2019-02-20 10:49:27 -0800 | [diff] [blame] | 270 | Zygote.getSystemProperty( |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 271 | DeviceConfig.RuntimeNative.USAP_POOL_REFILL_THRESHOLD, |
| 272 | Integer.toString(mUsapPoolSizeMax / 2)); |
Chris Wailes | db132a3 | 2019-02-20 10:49:27 -0800 | [diff] [blame] | 273 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 274 | if (!usapPoolRefillThresholdPropString.isEmpty()) { |
| 275 | mUsapPoolRefillThreshold = |
Chris Wailes | db132a3 | 2019-02-20 10:49:27 -0800 | [diff] [blame] | 276 | Integer.min( |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 277 | Integer.parseInt(usapPoolRefillThresholdPropString), |
| 278 | mUsapPoolSizeMax); |
Chris Wailes | db132a3 | 2019-02-20 10:49:27 -0800 | [diff] [blame] | 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 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 285 | private void fetchUsapPoolPolicyPropsWithMinInterval() { |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 286 | final long currentTimestamp = SystemClock.elapsedRealtime(); |
| 287 | |
| 288 | if (currentTimestamp - mLastPropCheckTimestamp >= Zygote.PROPERTY_CHECK_INTERVAL) { |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 289 | fetchUsapPoolPolicyProps(); |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 290 | mLastPropCheckTimestamp = currentTimestamp; |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | /** |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 295 | * Checks to see if the current policy says that pool should be refilled, and spawns new USAPs |
| 296 | * if necessary. |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 297 | * |
| 298 | * @param sessionSocketRawFDs Anonymous session sockets that are currently open |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 299 | * @return In the Zygote process this function will always return null; in unspecialized app |
| 300 | * processes this function will return a Runnable object representing the new |
| 301 | * application that is passed up from usapMain. |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 302 | */ |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 303 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 304 | Runnable fillUsapPool(int[] sessionSocketRawFDs) { |
| 305 | Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "Zygote:FillUsapPool"); |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 306 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 307 | int usapPoolCount = Zygote.getUsapPoolCount(); |
| 308 | int numUsapsToSpawn = mUsapPoolSizeMax - usapPoolCount; |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 309 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 310 | if (usapPoolCount < mUsapPoolSizeMin |
| 311 | || numUsapsToSpawn >= mUsapPoolRefillThreshold) { |
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 | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 318 | while (usapPoolCount++ < mUsapPoolSizeMax) { |
| 319 | Runnable caller = Zygote.forkUsap(mUsapPoolSocket, sessionSocketRawFDs); |
Chris Wailes | ae93714 | 2019-01-24 12:57:33 -0800 | [diff] [blame] | 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 | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 326 | // Re-enable runtime services for the Zygote. Services for unspecialized app process |
| 327 | // are re-enabled in specializeAppProcess. |
Chris Wailes | ae93714 | 2019-01-24 12:57:33 -0800 | [diff] [blame] | 328 | ZygoteHooks.postForkCommon(); |
| 329 | |
| 330 | Log.i("zygote", |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 331 | "Filled the USAP pool. New USAPs: " + numUsapsToSpawn); |
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 | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 340 | * Empty or fill the USAP pool as dictated by the current and new USAP pool statuses. |
Chris Wailes | db132a3 | 2019-02-20 10:49:27 -0800 | [diff] [blame] | 341 | */ |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 342 | Runnable setUsapPoolStatus(boolean newStatus, LocalSocket sessionSocket) { |
| 343 | if (!mUsapPoolSupported) { |
Chris Wailes | db132a3 | 2019-02-20 10:49:27 -0800 | [diff] [blame] | 344 | Log.w(TAG, |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 345 | "Attempting to enable a USAP pool for a Zygote that doesn't support it."); |
Chris Wailes | db132a3 | 2019-02-20 10:49:27 -0800 | [diff] [blame] | 346 | return null; |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 347 | } else if (mUsapPoolEnabled == newStatus) { |
Chris Wailes | db132a3 | 2019-02-20 10:49:27 -0800 | [diff] [blame] | 348 | return null; |
| 349 | } |
| 350 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 351 | mUsapPoolEnabled = newStatus; |
Chris Wailes | db132a3 | 2019-02-20 10:49:27 -0800 | [diff] [blame] | 352 | |
| 353 | if (newStatus) { |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 354 | return fillUsapPool(new int[]{ sessionSocket.getFileDescriptor().getInt$() }); |
Chris Wailes | db132a3 | 2019-02-20 10:49:27 -0800 | [diff] [blame] | 355 | } else { |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 356 | Zygote.emptyUsapPool(); |
Chris Wailes | db132a3 | 2019-02-20 10:49:27 -0800 | [diff] [blame] | 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) { |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 374 | fetchUsapPoolPolicyPropsWithMinInterval(); |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 375 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 376 | int[] usapPipeFDs = null; |
Chris Wailes | ae93714 | 2019-01-24 12:57:33 -0800 | [diff] [blame] | 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 |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 380 | // the state of the USAP pool for this Zygote (could be a |
Chris Wailes | ae93714 | 2019-01-24 12:57:33 -0800 | [diff] [blame] | 381 | // regular Zygote, a WebView Zygote, or an AppZygote). |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 382 | if (mUsapPoolEnabled) { |
| 383 | usapPipeFDs = Zygote.getUsapPipeFDs(); |
| 384 | pollFDs = new StructPollfd[socketFDs.size() + 1 + usapPipeFDs.length]; |
Chris Wailes | ae93714 | 2019-01-24 12:57:33 -0800 | [diff] [blame] | 385 | } else { |
| 386 | pollFDs = new StructPollfd[socketFDs.size()]; |
| 387 | } |
| 388 | |
| 389 | /* |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 390 | * For reasons of correctness the USAP pool pipe and event FDs |
Chris Wailes | ae93714 | 2019-01-24 12:57:33 -0800 | [diff] [blame] | 391 | * must be processed before the session and server sockets. This |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 392 | * is to ensure that the USAP pool accounting information is |
Chris Wailes | ae93714 | 2019-01-24 12:57:33 -0800 | [diff] [blame] | 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 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 405 | final int usapPoolEventFDIndex = pollIndex; |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 406 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 407 | if (mUsapPoolEnabled) { |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 408 | pollFDs[pollIndex] = new StructPollfd(); |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 409 | pollFDs[pollIndex].fd = mUsapPoolEventFD; |
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 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 413 | for (int usapPipeFD : usapPipeFDs) { |
Chris Wailes | ae93714 | 2019-01-24 12:57:33 -0800 | [diff] [blame] | 414 | FileDescriptor managedFd = new FileDescriptor(); |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 415 | managedFd.setInt$(usapPipeFD); |
Chris Wailes | ae93714 | 2019-01-24 12:57:33 -0800 | [diff] [blame] | 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 | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 430 | boolean usapPoolFDRead = false; |
Chris Wailes | ae93714 | 2019-01-24 12:57:33 -0800 | [diff] [blame] | 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 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 444 | } else if (pollIndex < usapPoolEventFDIndex) { |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 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 { |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 505 | // Either the USAP pool event FD or a USAP reporting pipe. |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 506 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 507 | // If this is the event FD the payload will be the number of USAPs removed. |
| 508 | // If this is a reporting pipe FD the payload will be the PID of the USAP |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 509 | // that was just specialized. |
| 510 | long messagePayload = -1; |
| 511 | |
| 512 | try { |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 513 | byte[] buffer = new byte[Zygote.USAP_MANAGEMENT_MESSAGE_BYTES]; |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 514 | int readBytes = Os.read(pollFDs[pollIndex].fd, buffer, 0, buffer.length); |
| 515 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 516 | if (readBytes == Zygote.USAP_MANAGEMENT_MESSAGE_BYTES) { |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 517 | DataInputStream inputStream = |
| 518 | new DataInputStream(new ByteArrayInputStream(buffer)); |
| 519 | |
| 520 | messagePayload = inputStream.readLong(); |
| 521 | } else { |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 522 | Log.e(TAG, "Incomplete read from USAP management FD of size " |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 523 | + readBytes); |
| 524 | continue; |
| 525 | } |
| 526 | } catch (Exception ex) { |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 527 | if (pollIndex == usapPoolEventFDIndex) { |
| 528 | Log.e(TAG, "Failed to read from USAP pool event FD: " |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 529 | + ex.getMessage()); |
| 530 | } else { |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 531 | Log.e(TAG, "Failed to read from USAP reporting pipe: " |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 532 | + ex.getMessage()); |
| 533 | } |
| 534 | |
| 535 | continue; |
| 536 | } |
| 537 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 538 | if (pollIndex > usapPoolEventFDIndex) { |
| 539 | Zygote.removeUsapTableEntry((int) messagePayload); |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 540 | } |
| 541 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 542 | usapPoolFDRead = true; |
Chris Wailes | ae93714 | 2019-01-24 12:57:33 -0800 | [diff] [blame] | 543 | } |
| 544 | } |
| 545 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 546 | // Check to see if the USAP pool needs to be refilled. |
| 547 | if (usapPoolFDRead) { |
Chris Wailes | ae93714 | 2019-01-24 12:57:33 -0800 | [diff] [blame] | 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 | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 554 | final Runnable command = fillUsapPool(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 | } |