Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 android.os; |
| 18 | |
Sudheer Shanka | d81b1d7 | 2018-09-05 16:37:30 -0700 | [diff] [blame] | 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.Nullable; |
Martijn Coenen | 7e6fa67 | 2018-11-05 11:45:26 +0100 | [diff] [blame] | 21 | import android.content.pm.ApplicationInfo; |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 22 | import android.net.LocalSocket; |
| 23 | import android.net.LocalSocketAddress; |
| 24 | import android.util.Log; |
Gustav Sennton | f0c52b5 | 2017-04-27 17:00:50 +0100 | [diff] [blame] | 25 | import android.util.Slog; |
Nicolas Geoffray | 81edac4 | 2017-09-07 14:13:29 +0100 | [diff] [blame] | 26 | |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 27 | import com.android.internal.annotations.GuardedBy; |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 28 | import com.android.internal.os.Zygote; |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 29 | import com.android.internal.util.Preconditions; |
Nicolas Geoffray | 81edac4 | 2017-09-07 14:13:29 +0100 | [diff] [blame] | 30 | |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 31 | import java.io.BufferedWriter; |
| 32 | import java.io.DataInputStream; |
| 33 | import java.io.IOException; |
| 34 | import java.io.OutputStreamWriter; |
| 35 | import java.nio.charset.StandardCharsets; |
| 36 | import java.util.ArrayList; |
| 37 | import java.util.Arrays; |
Martijn Coenen | 7e6fa67 | 2018-11-05 11:45:26 +0100 | [diff] [blame] | 38 | import java.util.Base64; |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 39 | import java.util.Collections; |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 40 | import java.util.List; |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 41 | import java.util.UUID; |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 42 | |
| 43 | /*package*/ class ZygoteStartFailedEx extends Exception { |
| 44 | ZygoteStartFailedEx(String s) { |
| 45 | super(s); |
| 46 | } |
| 47 | |
| 48 | ZygoteStartFailedEx(Throwable cause) { |
| 49 | super(cause); |
| 50 | } |
| 51 | |
| 52 | ZygoteStartFailedEx(String s, Throwable cause) { |
| 53 | super(s, cause); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Maintains communication state with the zygote processes. This class is responsible |
| 59 | * for the sockets opened to the zygotes and for starting processes on behalf of the |
| 60 | * {@link android.os.Process} class. |
| 61 | * |
| 62 | * {@hide} |
| 63 | */ |
| 64 | public class ZygoteProcess { |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 65 | |
| 66 | /** |
| 67 | * @hide for internal use only. |
| 68 | */ |
| 69 | public static final String ZYGOTE_SOCKET_NAME = "zygote"; |
| 70 | |
| 71 | /** |
| 72 | * @hide for internal use only. |
| 73 | */ |
| 74 | public static final String ZYGOTE_SECONDARY_SOCKET_NAME = "zygote_secondary"; |
| 75 | |
| 76 | /** |
Martijn Coenen | 82dc85a | 2019-01-28 13:12:28 +0100 | [diff] [blame] | 77 | * @hide for internal use only. |
| 78 | */ |
| 79 | public static final int ZYGOTE_CONNECT_TIMEOUT_MS = 20000; |
| 80 | |
| 81 | /** |
| 82 | * @hide for internal use only. |
| 83 | * |
| 84 | * Use a relatively short delay, because for app zygote, this is in the critical path of |
| 85 | * service launch. |
| 86 | */ |
| 87 | public static final int ZYGOTE_CONNECT_RETRY_DELAY_MS = 50; |
| 88 | |
| 89 | /** |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 90 | * @hide for internal use only |
| 91 | */ |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 92 | public static final String BLASTULA_POOL_SOCKET_NAME = "blastula_pool"; |
| 93 | |
| 94 | /** |
| 95 | * @hide for internal use only |
| 96 | */ |
| 97 | public static final String BLASTULA_POOL_SECONDARY_SOCKET_NAME = "blastula_pool_secondary"; |
| 98 | |
| 99 | /** |
| 100 | * @hide for internal use only |
| 101 | */ |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 102 | private static final String LOG_TAG = "ZygoteProcess"; |
| 103 | |
| 104 | /** |
| 105 | * The name of the socket used to communicate with the primary zygote. |
| 106 | */ |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 107 | private final LocalSocketAddress mZygoteSocketAddress; |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 108 | |
| 109 | /** |
| 110 | * The name of the secondary (alternate ABI) zygote socket. |
| 111 | */ |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 112 | private final LocalSocketAddress mZygoteSecondarySocketAddress; |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 113 | /** |
| 114 | * The name of the socket used to communicate with the primary blastula pool. |
| 115 | */ |
| 116 | private final LocalSocketAddress mBlastulaPoolSocketAddress; |
| 117 | |
| 118 | /** |
| 119 | * The name of the socket used to communicate with the secondary (alternate ABI) blastula pool. |
| 120 | */ |
| 121 | private final LocalSocketAddress mBlastulaPoolSecondarySocketAddress; |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 122 | |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 123 | public ZygoteProcess() { |
| 124 | mZygoteSocketAddress = |
| 125 | new LocalSocketAddress(ZYGOTE_SOCKET_NAME, LocalSocketAddress.Namespace.RESERVED); |
| 126 | mZygoteSecondarySocketAddress = |
| 127 | new LocalSocketAddress(ZYGOTE_SECONDARY_SOCKET_NAME, |
| 128 | LocalSocketAddress.Namespace.RESERVED); |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 129 | |
| 130 | mBlastulaPoolSocketAddress = |
| 131 | new LocalSocketAddress(BLASTULA_POOL_SOCKET_NAME, |
| 132 | LocalSocketAddress.Namespace.RESERVED); |
| 133 | mBlastulaPoolSecondarySocketAddress = |
| 134 | new LocalSocketAddress(BLASTULA_POOL_SECONDARY_SOCKET_NAME, |
| 135 | LocalSocketAddress.Namespace.RESERVED); |
Robert Sesek | 5ac8abf | 2018-01-26 14:26:53 -0500 | [diff] [blame] | 136 | } |
| 137 | |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 138 | public ZygoteProcess(LocalSocketAddress primarySocketAddress, |
| 139 | LocalSocketAddress secondarySocketAddress) { |
| 140 | mZygoteSocketAddress = primarySocketAddress; |
| 141 | mZygoteSecondarySocketAddress = secondarySocketAddress; |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 142 | |
| 143 | mBlastulaPoolSocketAddress = null; |
| 144 | mBlastulaPoolSecondarySocketAddress = null; |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 145 | } |
| 146 | |
Robert Sesek | 5ac8abf | 2018-01-26 14:26:53 -0500 | [diff] [blame] | 147 | public LocalSocketAddress getPrimarySocketAddress() { |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 148 | return mZygoteSocketAddress; |
Robert Sesek | 5ac8abf | 2018-01-26 14:26:53 -0500 | [diff] [blame] | 149 | } |
| 150 | |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 151 | /** |
| 152 | * State for communicating with the zygote process. |
| 153 | */ |
| 154 | public static class ZygoteState { |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 155 | final LocalSocketAddress mZygoteSocketAddress; |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 156 | final LocalSocketAddress mBlastulaSocketAddress; |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 157 | |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 158 | private final LocalSocket mZygoteSessionSocket; |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 159 | |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 160 | final DataInputStream mZygoteInputStream; |
| 161 | final BufferedWriter mZygoteOutputWriter; |
| 162 | |
| 163 | private final List<String> mABIList; |
| 164 | |
| 165 | private boolean mClosed; |
| 166 | |
| 167 | private ZygoteState(LocalSocketAddress zygoteSocketAddress, |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 168 | LocalSocketAddress blastulaSocketAddress, |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 169 | LocalSocket zygoteSessionSocket, |
| 170 | DataInputStream zygoteInputStream, |
| 171 | BufferedWriter zygoteOutputWriter, |
| 172 | List<String> abiList) { |
| 173 | this.mZygoteSocketAddress = zygoteSocketAddress; |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 174 | this.mBlastulaSocketAddress = blastulaSocketAddress; |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 175 | this.mZygoteSessionSocket = zygoteSessionSocket; |
| 176 | this.mZygoteInputStream = zygoteInputStream; |
| 177 | this.mZygoteOutputWriter = zygoteOutputWriter; |
| 178 | this.mABIList = abiList; |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 179 | } |
| 180 | |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 181 | /** |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 182 | * Create a new ZygoteState object by connecting to the given Zygote socket and saving the |
| 183 | * given blastula socket address. |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 184 | * |
| 185 | * @param zygoteSocketAddress Zygote socket to connect to |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 186 | * @param blastulaSocketAddress Blastula socket address to save for later |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 187 | * @return A new ZygoteState object containing a session socket for the given Zygote socket |
| 188 | * address |
| 189 | * @throws IOException |
| 190 | */ |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 191 | public static ZygoteState connect(LocalSocketAddress zygoteSocketAddress, |
| 192 | LocalSocketAddress blastulaSocketAddress) |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 193 | throws IOException { |
| 194 | |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 195 | DataInputStream zygoteInputStream = null; |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 196 | BufferedWriter zygoteOutputWriter = null; |
| 197 | final LocalSocket zygoteSessionSocket = new LocalSocket(); |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 198 | |
| 199 | try { |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 200 | zygoteSessionSocket.connect(zygoteSocketAddress); |
| 201 | zygoteInputStream = new DataInputStream(zygoteSessionSocket.getInputStream()); |
| 202 | zygoteOutputWriter = |
| 203 | new BufferedWriter( |
| 204 | new OutputStreamWriter(zygoteSessionSocket.getOutputStream()), |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 205 | Zygote.SOCKET_BUFFER_SIZE); |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 206 | } catch (IOException ex) { |
| 207 | try { |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 208 | zygoteSessionSocket.close(); |
| 209 | } catch (IOException ignore) { } |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 210 | |
| 211 | throw ex; |
| 212 | } |
| 213 | |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 214 | return new ZygoteState(zygoteSocketAddress, blastulaSocketAddress, |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 215 | zygoteSessionSocket, zygoteInputStream, zygoteOutputWriter, |
| 216 | getAbiList(zygoteOutputWriter, zygoteInputStream)); |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 217 | } |
| 218 | |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 219 | LocalSocket getBlastulaSessionSocket() throws IOException { |
| 220 | final LocalSocket blastulaSessionSocket = new LocalSocket(); |
| 221 | blastulaSessionSocket.connect(this.mBlastulaSocketAddress); |
| 222 | |
| 223 | return blastulaSessionSocket; |
| 224 | } |
| 225 | |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 226 | boolean matches(String abi) { |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 227 | return mABIList.contains(abi); |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | public void close() { |
| 231 | try { |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 232 | mZygoteSessionSocket.close(); |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 233 | } catch (IOException ex) { |
| 234 | Log.e(LOG_TAG,"I/O exception on routine close", ex); |
| 235 | } |
| 236 | |
| 237 | mClosed = true; |
| 238 | } |
| 239 | |
| 240 | boolean isClosed() { |
| 241 | return mClosed; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | /** |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 246 | * Lock object to protect access to the two ZygoteStates below. This lock must be |
| 247 | * acquired while communicating over the ZygoteState's socket, to prevent |
| 248 | * interleaved access. |
| 249 | */ |
| 250 | private final Object mLock = new Object(); |
| 251 | |
| 252 | /** |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 253 | * List of exemptions to the API blacklist. These are prefix matches on the runtime format |
| 254 | * symbol signature. Any matching symbol is treated by the runtime as being on the light grey |
| 255 | * list. |
| 256 | */ |
| 257 | private List<String> mApiBlacklistExemptions = Collections.emptyList(); |
| 258 | |
| 259 | /** |
Mathew Inwood | 04194fe | 2018-04-04 14:48:03 +0100 | [diff] [blame] | 260 | * Proportion of hidden API accesses that should be logged to the event log; 0 - 0x10000. |
| 261 | */ |
| 262 | private int mHiddenApiAccessLogSampleRate; |
| 263 | |
| 264 | /** |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 265 | * The state of the connection to the primary zygote. |
| 266 | */ |
| 267 | private ZygoteState primaryZygoteState; |
| 268 | |
| 269 | /** |
| 270 | * The state of the connection to the secondary zygote. |
| 271 | */ |
| 272 | private ZygoteState secondaryZygoteState; |
| 273 | |
| 274 | /** |
| 275 | * Start a new process. |
| 276 | * |
| 277 | * <p>If processes are enabled, a new process is created and the |
| 278 | * static main() function of a <var>processClass</var> is executed there. |
| 279 | * The process will continue running after this function returns. |
| 280 | * |
| 281 | * <p>If processes are not enabled, a new thread in the caller's |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 282 | * process is created and main() of <var>processclass</var> called there. |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 283 | * |
| 284 | * <p>The niceName parameter, if not an empty string, is a custom name to |
| 285 | * give to the process instead of using processClass. This allows you to |
| 286 | * make easily identifyable processes even if you are using the same base |
| 287 | * <var>processClass</var> to start them. |
| 288 | * |
Tamas Berghammer | b8f7c35 | 2016-11-11 16:08:26 +0000 | [diff] [blame] | 289 | * When invokeWith is not null, the process will be started as a fresh app |
Tamas Berghammer | 0ca16fa | 2016-11-11 16:08:26 +0000 | [diff] [blame] | 290 | * and not a zygote fork. Note that this is only allowed for uid 0 or when |
Nicolas Geoffray | 81edac4 | 2017-09-07 14:13:29 +0100 | [diff] [blame] | 291 | * runtimeFlags contains DEBUG_ENABLE_DEBUGGER. |
Tamas Berghammer | b8f7c35 | 2016-11-11 16:08:26 +0000 | [diff] [blame] | 292 | * |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 293 | * @param processClass The class to use as the process's main entry |
| 294 | * point. |
| 295 | * @param niceName A more readable name to use for the process. |
| 296 | * @param uid The user-id under which the process will run. |
| 297 | * @param gid The group-id under which the process will run. |
| 298 | * @param gids Additional group-ids associated with the process. |
Nicolas Geoffray | 81edac4 | 2017-09-07 14:13:29 +0100 | [diff] [blame] | 299 | * @param runtimeFlags Additional flags. |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 300 | * @param targetSdkVersion The target SDK version for the app. |
| 301 | * @param seInfo null-ok SELinux information for the new process. |
| 302 | * @param abi non-null the ABI this app should be started with. |
| 303 | * @param instructionSet null-ok the instruction set to use. |
| 304 | * @param appDataDir null-ok the data directory of the app. |
Tamas Berghammer | b8f7c35 | 2016-11-11 16:08:26 +0000 | [diff] [blame] | 305 | * @param invokeWith null-ok the command to invoke with. |
Sudheer Shanka | d81b1d7 | 2018-09-05 16:37:30 -0700 | [diff] [blame] | 306 | * @param packageName null-ok the name of the package this process belongs to. |
Sudheer Shanka | 3f0645b | 2018-09-18 13:07:59 -0700 | [diff] [blame] | 307 | * @param packagesForUid null-ok all the packages with the same uid as this process. |
| 308 | * @param visibleVols null-ok storage volumes that can be accessed by this process. |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 309 | * @param zygoteArgs Additional arguments to supply to the zygote process. |
| 310 | * |
| 311 | * @return An object that describes the result of the attempt to start the process. |
| 312 | * @throws RuntimeException on fatal start failure |
| 313 | */ |
Sudheer Shanka | d81b1d7 | 2018-09-05 16:37:30 -0700 | [diff] [blame] | 314 | public final Process.ProcessStartResult start(@NonNull final String processClass, |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 315 | final String niceName, |
Sudheer Shanka | d81b1d7 | 2018-09-05 16:37:30 -0700 | [diff] [blame] | 316 | int uid, int gid, @Nullable int[] gids, |
Nicolas Geoffray | 81edac4 | 2017-09-07 14:13:29 +0100 | [diff] [blame] | 317 | int runtimeFlags, int mountExternal, |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 318 | int targetSdkVersion, |
Sudheer Shanka | d81b1d7 | 2018-09-05 16:37:30 -0700 | [diff] [blame] | 319 | @Nullable String seInfo, |
| 320 | @NonNull String abi, |
| 321 | @Nullable String instructionSet, |
| 322 | @Nullable String appDataDir, |
| 323 | @Nullable String invokeWith, |
| 324 | @Nullable String packageName, |
Sudheer Shanka | 3f0645b | 2018-09-18 13:07:59 -0700 | [diff] [blame] | 325 | @Nullable String[] packagesForUid, |
| 326 | @Nullable String[] visibleVols, |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 327 | boolean useBlastulaPool, |
Sudheer Shanka | d81b1d7 | 2018-09-05 16:37:30 -0700 | [diff] [blame] | 328 | @Nullable String[] zygoteArgs) { |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 329 | try { |
| 330 | return startViaZygote(processClass, niceName, uid, gid, gids, |
Nicolas Geoffray | 81edac4 | 2017-09-07 14:13:29 +0100 | [diff] [blame] | 331 | runtimeFlags, mountExternal, targetSdkVersion, seInfo, |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 332 | abi, instructionSet, appDataDir, invokeWith, /*startChildZygote=*/false, |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 333 | packageName, packagesForUid, visibleVols, useBlastulaPool, zygoteArgs); |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 334 | } catch (ZygoteStartFailedEx ex) { |
| 335 | Log.e(LOG_TAG, |
| 336 | "Starting VM process through Zygote failed"); |
| 337 | throw new RuntimeException( |
| 338 | "Starting VM process through Zygote failed", ex); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | /** retry interval for opening a zygote socket */ |
| 343 | static final int ZYGOTE_RETRY_MILLIS = 500; |
| 344 | |
| 345 | /** |
| 346 | * Queries the zygote for the list of ABIS it supports. |
| 347 | * |
| 348 | * @throws ZygoteStartFailedEx if the query failed. |
| 349 | */ |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 350 | @GuardedBy("mLock") |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 351 | private static List<String> getAbiList(BufferedWriter writer, DataInputStream inputStream) |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 352 | throws IOException { |
| 353 | // Each query starts with the argument count (1 in this case) |
| 354 | writer.write("1"); |
| 355 | // ... followed by a new-line. |
| 356 | writer.newLine(); |
| 357 | // ... followed by our only argument. |
| 358 | writer.write("--query-abi-list"); |
| 359 | writer.newLine(); |
| 360 | writer.flush(); |
| 361 | |
| 362 | // The response is a length prefixed stream of ASCII bytes. |
| 363 | int numBytes = inputStream.readInt(); |
| 364 | byte[] bytes = new byte[numBytes]; |
| 365 | inputStream.readFully(bytes); |
| 366 | |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 367 | String rawList = new String(bytes, StandardCharsets.US_ASCII); |
| 368 | |
| 369 | return Arrays.asList(rawList.split(",")); |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | /** |
| 373 | * Sends an argument list to the zygote process, which starts a new child |
| 374 | * and returns the child's pid. Please note: the present implementation |
| 375 | * replaces newlines in the argument list with spaces. |
| 376 | * |
| 377 | * @throws ZygoteStartFailedEx if process start failed for any reason |
| 378 | */ |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 379 | @GuardedBy("mLock") |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 380 | private static Process.ProcessStartResult zygoteSendArgsAndGetResult( |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 381 | ZygoteState zygoteState, boolean useBlastulaPool, ArrayList<String> args) |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 382 | throws ZygoteStartFailedEx { |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 383 | // Throw early if any of the arguments are malformed. This means we can |
| 384 | // avoid writing a partial response to the zygote. |
| 385 | for (String arg : args) { |
| 386 | if (arg.indexOf('\n') >= 0) { |
| 387 | throw new ZygoteStartFailedEx("embedded newlines not allowed"); |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | /** |
| 392 | * See com.android.internal.os.SystemZygoteInit.readArgumentList() |
| 393 | * Presently the wire format to the zygote process is: |
| 394 | * a) a count of arguments (argc, in essence) |
| 395 | * b) a number of newline-separated argument strings equal to count |
| 396 | * |
| 397 | * After the zygote process reads these it will write the pid of |
| 398 | * the child or -1 on failure, followed by boolean to |
| 399 | * indicate whether a wrapper process was used. |
| 400 | */ |
| 401 | String msgStr = Integer.toString(args.size()) + "\n" |
| 402 | + String.join("\n", args) + "\n"; |
| 403 | |
| 404 | // Should there be a timeout on this? |
| 405 | Process.ProcessStartResult result = new Process.ProcessStartResult(); |
| 406 | |
| 407 | // TODO (chriswailes): Move branch body into separate function. |
| 408 | if (useBlastulaPool && Zygote.BLASTULA_POOL_ENABLED && isValidBlastulaCommand(args)) { |
| 409 | LocalSocket blastulaSessionSocket = null; |
| 410 | |
| 411 | try { |
| 412 | blastulaSessionSocket = zygoteState.getBlastulaSessionSocket(); |
| 413 | |
| 414 | final BufferedWriter blastulaWriter = |
| 415 | new BufferedWriter( |
| 416 | new OutputStreamWriter(blastulaSessionSocket.getOutputStream()), |
| 417 | Zygote.SOCKET_BUFFER_SIZE); |
| 418 | final DataInputStream blastulaReader = |
| 419 | new DataInputStream(blastulaSessionSocket.getInputStream()); |
| 420 | |
| 421 | blastulaWriter.write(msgStr); |
| 422 | blastulaWriter.flush(); |
| 423 | |
| 424 | result.pid = blastulaReader.readInt(); |
| 425 | // Blastulas can't be used to spawn processes that need wrappers. |
| 426 | result.usingWrapper = false; |
| 427 | |
| 428 | if (result.pid < 0) { |
| 429 | throw new ZygoteStartFailedEx("Blastula specialization failed"); |
| 430 | } |
| 431 | |
| 432 | return result; |
| 433 | } catch (IOException ex) { |
| 434 | // If there was an IOException using the blastula pool we will log the error and |
| 435 | // attempt to start the process through the Zygote. |
| 436 | Log.e(LOG_TAG, "IO Exception while communicating with blastula pool - " |
| 437 | + ex.toString()); |
| 438 | } finally { |
| 439 | try { |
| 440 | blastulaSessionSocket.close(); |
| 441 | } catch (IOException ex) { |
| 442 | Log.e(LOG_TAG, "Failed to close blastula session socket: " + ex.getMessage()); |
Robert Sesek | 0b58f19 | 2016-10-10 18:34:42 -0400 | [diff] [blame] | 443 | } |
| 444 | } |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 445 | } |
Robert Sesek | 0b58f19 | 2016-10-10 18:34:42 -0400 | [diff] [blame] | 446 | |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 447 | try { |
| 448 | final BufferedWriter zygoteWriter = zygoteState.mZygoteOutputWriter; |
| 449 | final DataInputStream zygoteInputStream = zygoteState.mZygoteInputStream; |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 450 | |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 451 | zygoteWriter.write(msgStr); |
| 452 | zygoteWriter.flush(); |
Robert Sesek | 0b58f19 | 2016-10-10 18:34:42 -0400 | [diff] [blame] | 453 | |
| 454 | // Always read the entire result from the input stream to avoid leaving |
| 455 | // bytes in the stream for future process starts to accidentally stumble |
| 456 | // upon. |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 457 | result.pid = zygoteInputStream.readInt(); |
| 458 | result.usingWrapper = zygoteInputStream.readBoolean(); |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 459 | } catch (IOException ex) { |
| 460 | zygoteState.close(); |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 461 | Log.e(LOG_TAG, "IO Exception while communicating with Zygote - " |
| 462 | + ex.toString()); |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 463 | throw new ZygoteStartFailedEx(ex); |
| 464 | } |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 465 | |
| 466 | if (result.pid < 0) { |
| 467 | throw new ZygoteStartFailedEx("fork() failed"); |
| 468 | } |
| 469 | |
| 470 | return result; |
| 471 | } |
| 472 | |
| 473 | /** |
| 474 | * Flags that may not be passed to a blastula. |
| 475 | */ |
| 476 | private static final String[] INVALID_BLASTULA_FLAGS = { |
| 477 | "--query-abi-list", |
| 478 | "--get-pid", |
| 479 | "--preload-default", |
| 480 | "--preload-package", |
| 481 | "--preload-app", |
| 482 | "--start-child-zygote", |
| 483 | "--set-api-blacklist-exemptions", |
| 484 | "--hidden-api-log-sampling-rate", |
| 485 | "--invoke-with" |
| 486 | }; |
| 487 | |
| 488 | /** |
| 489 | * Tests a command list to see if it is valid to send to a blastula. |
| 490 | * @param args Zygote/Blastula command arguments |
| 491 | * @return True if the command can be passed to a blastula; false otherwise |
| 492 | */ |
| 493 | private static boolean isValidBlastulaCommand(ArrayList<String> args) { |
| 494 | for (String flag : args) { |
| 495 | for (String badFlag : INVALID_BLASTULA_FLAGS) { |
| 496 | if (flag.startsWith(badFlag)) { |
| 497 | return false; |
| 498 | } |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | return true; |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | /** |
| 506 | * Starts a new process via the zygote mechanism. |
| 507 | * |
| 508 | * @param processClass Class name whose static main() to run |
| 509 | * @param niceName 'nice' process name to appear in ps |
| 510 | * @param uid a POSIX uid that the new process should setuid() to |
| 511 | * @param gid a POSIX gid that the new process shuold setgid() to |
| 512 | * @param gids null-ok; a list of supplementary group IDs that the |
| 513 | * new process should setgroup() to. |
Nicolas Geoffray | 81edac4 | 2017-09-07 14:13:29 +0100 | [diff] [blame] | 514 | * @param runtimeFlags Additional flags for the runtime. |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 515 | * @param targetSdkVersion The target SDK version for the app. |
| 516 | * @param seInfo null-ok SELinux information for the new process. |
| 517 | * @param abi the ABI the process should use. |
| 518 | * @param instructionSet null-ok the instruction set to use. |
| 519 | * @param appDataDir null-ok the data directory of the app. |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 520 | * @param startChildZygote Start a sub-zygote. This creates a new zygote process |
| 521 | * that has its state cloned from this zygote process. |
Sudheer Shanka | d81b1d7 | 2018-09-05 16:37:30 -0700 | [diff] [blame] | 522 | * @param packageName null-ok the name of the package this process belongs to. |
Sudheer Shanka | 3f0645b | 2018-09-18 13:07:59 -0700 | [diff] [blame] | 523 | * @param packagesForUid null-ok all the packages with the same uid as this process. |
| 524 | * @param visibleVols null-ok storage volumes that can be accessed by this process. |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 525 | * @param extraArgs Additional arguments to supply to the zygote process. |
| 526 | * @return An object that describes the result of the attempt to start the process. |
| 527 | * @throws ZygoteStartFailedEx if process start failed for any reason |
| 528 | */ |
Sudheer Shanka | d81b1d7 | 2018-09-05 16:37:30 -0700 | [diff] [blame] | 529 | private Process.ProcessStartResult startViaZygote(@NonNull final String processClass, |
| 530 | @Nullable final String niceName, |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 531 | final int uid, final int gid, |
Sudheer Shanka | d81b1d7 | 2018-09-05 16:37:30 -0700 | [diff] [blame] | 532 | @Nullable final int[] gids, |
Nicolas Geoffray | 81edac4 | 2017-09-07 14:13:29 +0100 | [diff] [blame] | 533 | int runtimeFlags, int mountExternal, |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 534 | int targetSdkVersion, |
Sudheer Shanka | d81b1d7 | 2018-09-05 16:37:30 -0700 | [diff] [blame] | 535 | @Nullable String seInfo, |
| 536 | @NonNull String abi, |
| 537 | @Nullable String instructionSet, |
| 538 | @Nullable String appDataDir, |
| 539 | @Nullable String invokeWith, |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 540 | boolean startChildZygote, |
Sudheer Shanka | d81b1d7 | 2018-09-05 16:37:30 -0700 | [diff] [blame] | 541 | @Nullable String packageName, |
Sudheer Shanka | 3f0645b | 2018-09-18 13:07:59 -0700 | [diff] [blame] | 542 | @Nullable String[] packagesForUid, |
| 543 | @Nullable String[] visibleVols, |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 544 | boolean useBlastulaPool, |
Sudheer Shanka | d81b1d7 | 2018-09-05 16:37:30 -0700 | [diff] [blame] | 545 | @Nullable String[] extraArgs) |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 546 | throws ZygoteStartFailedEx { |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 547 | ArrayList<String> argsForZygote = new ArrayList<String>(); |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 548 | |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 549 | // --runtime-args, --setuid=, --setgid=, |
| 550 | // and --setgroups= must go first |
| 551 | argsForZygote.add("--runtime-args"); |
| 552 | argsForZygote.add("--setuid=" + uid); |
| 553 | argsForZygote.add("--setgid=" + gid); |
Nicolas Geoffray | 81edac4 | 2017-09-07 14:13:29 +0100 | [diff] [blame] | 554 | argsForZygote.add("--runtime-flags=" + runtimeFlags); |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 555 | if (mountExternal == Zygote.MOUNT_EXTERNAL_DEFAULT) { |
| 556 | argsForZygote.add("--mount-external-default"); |
| 557 | } else if (mountExternal == Zygote.MOUNT_EXTERNAL_READ) { |
| 558 | argsForZygote.add("--mount-external-read"); |
| 559 | } else if (mountExternal == Zygote.MOUNT_EXTERNAL_WRITE) { |
| 560 | argsForZygote.add("--mount-external-write"); |
Sudheer Shanka | 98cb3f0 | 2018-08-17 16:10:29 -0700 | [diff] [blame] | 561 | } else if (mountExternal == Zygote.MOUNT_EXTERNAL_FULL) { |
| 562 | argsForZygote.add("--mount-external-full"); |
Sudheer Shanka | 3a0df3b | 2018-12-12 12:43:43 -0800 | [diff] [blame] | 563 | } else if (mountExternal == Zygote.MOUNT_EXTERNAL_INSTALLER) { |
| 564 | argsForZygote.add("--mount-external-installer"); |
Sudheer Shanka | 0b6da53 | 2019-01-09 12:06:51 -0800 | [diff] [blame] | 565 | } else if (mountExternal == Zygote.MOUNT_EXTERNAL_LEGACY) { |
| 566 | argsForZygote.add("--mount-external-legacy"); |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 567 | } |
Sudheer Shanka | 98cb3f0 | 2018-08-17 16:10:29 -0700 | [diff] [blame] | 568 | |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 569 | argsForZygote.add("--target-sdk-version=" + targetSdkVersion); |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 570 | |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 571 | // --setgroups is a comma-separated list |
| 572 | if (gids != null && gids.length > 0) { |
| 573 | StringBuilder sb = new StringBuilder(); |
| 574 | sb.append("--setgroups="); |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 575 | |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 576 | int sz = gids.length; |
| 577 | for (int i = 0; i < sz; i++) { |
| 578 | if (i != 0) { |
| 579 | sb.append(','); |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 580 | } |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 581 | sb.append(gids[i]); |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 582 | } |
| 583 | |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 584 | argsForZygote.add(sb.toString()); |
| 585 | } |
| 586 | |
| 587 | if (niceName != null) { |
| 588 | argsForZygote.add("--nice-name=" + niceName); |
| 589 | } |
| 590 | |
| 591 | if (seInfo != null) { |
| 592 | argsForZygote.add("--seinfo=" + seInfo); |
| 593 | } |
| 594 | |
| 595 | if (instructionSet != null) { |
| 596 | argsForZygote.add("--instruction-set=" + instructionSet); |
| 597 | } |
| 598 | |
| 599 | if (appDataDir != null) { |
| 600 | argsForZygote.add("--app-data-dir=" + appDataDir); |
| 601 | } |
| 602 | |
Tamas Berghammer | b8f7c35 | 2016-11-11 16:08:26 +0000 | [diff] [blame] | 603 | if (invokeWith != null) { |
| 604 | argsForZygote.add("--invoke-with"); |
| 605 | argsForZygote.add(invokeWith); |
| 606 | } |
| 607 | |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 608 | if (startChildZygote) { |
| 609 | argsForZygote.add("--start-child-zygote"); |
| 610 | } |
| 611 | |
Sudheer Shanka | 154fe3f | 2018-07-30 14:44:26 -0700 | [diff] [blame] | 612 | if (packageName != null) { |
| 613 | argsForZygote.add("--package-name=" + packageName); |
| 614 | } |
| 615 | |
Sudheer Shanka | 3f0645b | 2018-09-18 13:07:59 -0700 | [diff] [blame] | 616 | if (packagesForUid != null && packagesForUid.length > 0) { |
| 617 | final StringBuilder sb = new StringBuilder(); |
| 618 | sb.append("--packages-for-uid="); |
| 619 | |
| 620 | for (int i = 0; i < packagesForUid.length; ++i) { |
| 621 | if (i != 0) { |
| 622 | sb.append(','); |
| 623 | } |
| 624 | sb.append(packagesForUid[i]); |
| 625 | } |
| 626 | argsForZygote.add(sb.toString()); |
| 627 | } |
| 628 | |
| 629 | if (visibleVols != null && visibleVols.length > 0) { |
| 630 | final StringBuilder sb = new StringBuilder(); |
| 631 | sb.append("--visible-vols="); |
| 632 | |
| 633 | for (int i = 0; i < visibleVols.length; ++i) { |
| 634 | if (i != 0) { |
| 635 | sb.append(','); |
| 636 | } |
| 637 | sb.append(visibleVols[i]); |
| 638 | } |
| 639 | argsForZygote.add(sb.toString()); |
| 640 | } |
| 641 | |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 642 | argsForZygote.add(processClass); |
| 643 | |
| 644 | if (extraArgs != null) { |
| 645 | for (String arg : extraArgs) { |
| 646 | argsForZygote.add(arg); |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 647 | } |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 648 | } |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 649 | |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 650 | synchronized(mLock) { |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 651 | return zygoteSendArgsAndGetResult(openZygoteSocketIfNeeded(abi), |
| 652 | useBlastulaPool, |
| 653 | argsForZygote); |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 654 | } |
| 655 | } |
| 656 | |
| 657 | /** |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 658 | * Closes the connections to the zygote, if they exist. |
| 659 | */ |
| 660 | public void close() { |
| 661 | if (primaryZygoteState != null) { |
| 662 | primaryZygoteState.close(); |
| 663 | } |
| 664 | if (secondaryZygoteState != null) { |
| 665 | secondaryZygoteState.close(); |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | /** |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 670 | * Tries to establish a connection to the zygote that handles a given {@code abi}. Might block |
| 671 | * and retry if the zygote is unresponsive. This method is a no-op if a connection is |
| 672 | * already open. |
| 673 | */ |
| 674 | public void establishZygoteConnectionForAbi(String abi) { |
| 675 | try { |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 676 | synchronized(mLock) { |
| 677 | openZygoteSocketIfNeeded(abi); |
| 678 | } |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 679 | } catch (ZygoteStartFailedEx ex) { |
| 680 | throw new RuntimeException("Unable to connect to zygote for abi: " + abi, ex); |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | /** |
Andreas Gampe | 8444dca | 2018-05-01 13:31:28 -0700 | [diff] [blame] | 685 | * Attempt to retrieve the PID of the zygote serving the given abi. |
| 686 | */ |
| 687 | public int getZygotePid(String abi) { |
| 688 | try { |
| 689 | synchronized (mLock) { |
| 690 | ZygoteState state = openZygoteSocketIfNeeded(abi); |
| 691 | |
| 692 | // Each query starts with the argument count (1 in this case) |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 693 | state.mZygoteOutputWriter.write("1"); |
Andreas Gampe | 8444dca | 2018-05-01 13:31:28 -0700 | [diff] [blame] | 694 | // ... followed by a new-line. |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 695 | state.mZygoteOutputWriter.newLine(); |
Andreas Gampe | 8444dca | 2018-05-01 13:31:28 -0700 | [diff] [blame] | 696 | // ... followed by our only argument. |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 697 | state.mZygoteOutputWriter.write("--get-pid"); |
| 698 | state.mZygoteOutputWriter.newLine(); |
| 699 | state.mZygoteOutputWriter.flush(); |
Andreas Gampe | 8444dca | 2018-05-01 13:31:28 -0700 | [diff] [blame] | 700 | |
| 701 | // The response is a length prefixed stream of ASCII bytes. |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 702 | int numBytes = state.mZygoteInputStream.readInt(); |
Andreas Gampe | 8444dca | 2018-05-01 13:31:28 -0700 | [diff] [blame] | 703 | byte[] bytes = new byte[numBytes]; |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 704 | state.mZygoteInputStream.readFully(bytes); |
Andreas Gampe | 8444dca | 2018-05-01 13:31:28 -0700 | [diff] [blame] | 705 | |
| 706 | return Integer.parseInt(new String(bytes, StandardCharsets.US_ASCII)); |
| 707 | } |
| 708 | } catch (Exception ex) { |
| 709 | throw new RuntimeException("Failure retrieving pid", ex); |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | /** |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 714 | * Push hidden API blacklisting exemptions into the zygote process(es). |
| 715 | * |
| 716 | * <p>The list of exemptions will take affect for all new processes forked from the zygote after |
| 717 | * this call. |
| 718 | * |
Mathew Inwood | 33d5138 | 2018-04-05 13:56:39 +0100 | [diff] [blame] | 719 | * @param exemptions List of hidden API exemption prefixes. Any matching members are treated as |
| 720 | * whitelisted/public APIs (i.e. allowed, no logging of usage). |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 721 | */ |
Mathew Inwood | 9f6bb5b | 2018-04-09 17:29:12 +0100 | [diff] [blame] | 722 | public boolean setApiBlacklistExemptions(List<String> exemptions) { |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 723 | synchronized (mLock) { |
| 724 | mApiBlacklistExemptions = exemptions; |
Mathew Inwood | 9f6bb5b | 2018-04-09 17:29:12 +0100 | [diff] [blame] | 725 | boolean ok = maybeSetApiBlacklistExemptions(primaryZygoteState, true); |
| 726 | if (ok) { |
| 727 | ok = maybeSetApiBlacklistExemptions(secondaryZygoteState, true); |
| 728 | } |
| 729 | return ok; |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 730 | } |
| 731 | } |
| 732 | |
Mathew Inwood | 04194fe | 2018-04-04 14:48:03 +0100 | [diff] [blame] | 733 | /** |
| 734 | * Set the precentage of detected hidden API accesses that are logged to the event log. |
| 735 | * |
| 736 | * <p>This rate will take affect for all new processes forked from the zygote after this call. |
| 737 | * |
| 738 | * @param rate An integer between 0 and 0x10000 inclusive. 0 means no event logging. |
| 739 | */ |
| 740 | public void setHiddenApiAccessLogSampleRate(int rate) { |
| 741 | synchronized (mLock) { |
| 742 | mHiddenApiAccessLogSampleRate = rate; |
| 743 | maybeSetHiddenApiAccessLogSampleRate(primaryZygoteState); |
| 744 | maybeSetHiddenApiAccessLogSampleRate(secondaryZygoteState); |
| 745 | } |
| 746 | } |
| 747 | |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 748 | @GuardedBy("mLock") |
Mathew Inwood | 9f6bb5b | 2018-04-09 17:29:12 +0100 | [diff] [blame] | 749 | private boolean maybeSetApiBlacklistExemptions(ZygoteState state, boolean sendIfEmpty) { |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 750 | if (state == null || state.isClosed()) { |
Mathew Inwood | 9f6bb5b | 2018-04-09 17:29:12 +0100 | [diff] [blame] | 751 | Slog.e(LOG_TAG, "Can't set API blacklist exemptions: no zygote connection"); |
| 752 | return false; |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 753 | } |
| 754 | if (!sendIfEmpty && mApiBlacklistExemptions.isEmpty()) { |
Mathew Inwood | 9f6bb5b | 2018-04-09 17:29:12 +0100 | [diff] [blame] | 755 | return true; |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 756 | } |
| 757 | try { |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 758 | state.mZygoteOutputWriter.write(Integer.toString(mApiBlacklistExemptions.size() + 1)); |
| 759 | state.mZygoteOutputWriter.newLine(); |
| 760 | state.mZygoteOutputWriter.write("--set-api-blacklist-exemptions"); |
| 761 | state.mZygoteOutputWriter.newLine(); |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 762 | for (int i = 0; i < mApiBlacklistExemptions.size(); ++i) { |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 763 | state.mZygoteOutputWriter.write(mApiBlacklistExemptions.get(i)); |
| 764 | state.mZygoteOutputWriter.newLine(); |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 765 | } |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 766 | state.mZygoteOutputWriter.flush(); |
| 767 | int status = state.mZygoteInputStream.readInt(); |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 768 | if (status != 0) { |
| 769 | Slog.e(LOG_TAG, "Failed to set API blacklist exemptions; status " + status); |
| 770 | } |
Mathew Inwood | 9f6bb5b | 2018-04-09 17:29:12 +0100 | [diff] [blame] | 771 | return true; |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 772 | } catch (IOException ioe) { |
| 773 | Slog.e(LOG_TAG, "Failed to set API blacklist exemptions", ioe); |
Mathew Inwood | 9f6bb5b | 2018-04-09 17:29:12 +0100 | [diff] [blame] | 774 | mApiBlacklistExemptions = Collections.emptyList(); |
| 775 | return false; |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 776 | } |
| 777 | } |
| 778 | |
Mathew Inwood | 04194fe | 2018-04-04 14:48:03 +0100 | [diff] [blame] | 779 | private void maybeSetHiddenApiAccessLogSampleRate(ZygoteState state) { |
| 780 | if (state == null || state.isClosed()) { |
| 781 | return; |
| 782 | } |
| 783 | if (mHiddenApiAccessLogSampleRate == -1) { |
| 784 | return; |
| 785 | } |
| 786 | try { |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 787 | state.mZygoteOutputWriter.write(Integer.toString(1)); |
| 788 | state.mZygoteOutputWriter.newLine(); |
| 789 | state.mZygoteOutputWriter.write("--hidden-api-log-sampling-rate=" |
Mathew Inwood | 04194fe | 2018-04-04 14:48:03 +0100 | [diff] [blame] | 790 | + Integer.toString(mHiddenApiAccessLogSampleRate)); |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 791 | state.mZygoteOutputWriter.newLine(); |
| 792 | state.mZygoteOutputWriter.flush(); |
| 793 | int status = state.mZygoteInputStream.readInt(); |
Mathew Inwood | 04194fe | 2018-04-04 14:48:03 +0100 | [diff] [blame] | 794 | if (status != 0) { |
| 795 | Slog.e(LOG_TAG, "Failed to set hidden API log sampling rate; status " + status); |
| 796 | } |
| 797 | } catch (IOException ioe) { |
| 798 | Slog.e(LOG_TAG, "Failed to set hidden API log sampling rate", ioe); |
| 799 | } |
| 800 | } |
| 801 | |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 802 | /** |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 803 | * Tries to open a session socket to a Zygote process with a compatible ABI if one is not |
| 804 | * already open. If a compatible session socket is already open that session socket is returned. |
| 805 | * This function may block and may have to try connecting to multiple Zygotes to find the |
| 806 | * appropriate one. Requires that mLock be held. |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 807 | */ |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 808 | @GuardedBy("mLock") |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 809 | private ZygoteState openZygoteSocketIfNeeded(String abi) |
| 810 | throws ZygoteStartFailedEx { |
| 811 | |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 812 | Preconditions.checkState(Thread.holdsLock(mLock), "ZygoteProcess lock not held"); |
| 813 | |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 814 | if (primaryZygoteState == null || primaryZygoteState.isClosed()) { |
| 815 | try { |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 816 | primaryZygoteState = |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 817 | ZygoteState.connect(mZygoteSocketAddress, mBlastulaPoolSocketAddress); |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 818 | } catch (IOException ioe) { |
| 819 | throw new ZygoteStartFailedEx("Error connecting to primary zygote", ioe); |
| 820 | } |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 821 | |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 822 | maybeSetApiBlacklistExemptions(primaryZygoteState, false); |
Mathew Inwood | 04194fe | 2018-04-04 14:48:03 +0100 | [diff] [blame] | 823 | maybeSetHiddenApiAccessLogSampleRate(primaryZygoteState); |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 824 | } |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 825 | |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 826 | if (primaryZygoteState.matches(abi)) { |
| 827 | return primaryZygoteState; |
| 828 | } |
| 829 | |
| 830 | // The primary zygote didn't match. Try the secondary. |
| 831 | if (secondaryZygoteState == null || secondaryZygoteState.isClosed()) { |
| 832 | try { |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 833 | secondaryZygoteState = |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 834 | ZygoteState.connect(mZygoteSecondarySocketAddress, |
| 835 | mBlastulaPoolSecondarySocketAddress); |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 836 | } catch (IOException ioe) { |
| 837 | throw new ZygoteStartFailedEx("Error connecting to secondary zygote", ioe); |
| 838 | } |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 839 | |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 840 | maybeSetApiBlacklistExemptions(secondaryZygoteState, false); |
Mathew Inwood | 04194fe | 2018-04-04 14:48:03 +0100 | [diff] [blame] | 841 | maybeSetHiddenApiAccessLogSampleRate(secondaryZygoteState); |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 842 | } |
| 843 | |
| 844 | if (secondaryZygoteState.matches(abi)) { |
| 845 | return secondaryZygoteState; |
| 846 | } |
| 847 | |
| 848 | throw new ZygoteStartFailedEx("Unsupported zygote ABI: " + abi); |
| 849 | } |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 850 | |
| 851 | /** |
Martijn Coenen | 7e6fa67 | 2018-11-05 11:45:26 +0100 | [diff] [blame] | 852 | * Instructs the zygote to pre-load the application code for the given Application. |
| 853 | * Only the app zygote supports this function. |
| 854 | * TODO preloadPackageForAbi() can probably be removed and the callers an use this instead. |
| 855 | */ |
| 856 | public boolean preloadApp(ApplicationInfo appInfo, String abi) throws ZygoteStartFailedEx, |
| 857 | IOException { |
| 858 | synchronized (mLock) { |
| 859 | ZygoteState state = openZygoteSocketIfNeeded(abi); |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 860 | state.mZygoteOutputWriter.write("2"); |
| 861 | state.mZygoteOutputWriter.newLine(); |
Martijn Coenen | 7e6fa67 | 2018-11-05 11:45:26 +0100 | [diff] [blame] | 862 | |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 863 | state.mZygoteOutputWriter.write("--preload-app"); |
| 864 | state.mZygoteOutputWriter.newLine(); |
Martijn Coenen | 7e6fa67 | 2018-11-05 11:45:26 +0100 | [diff] [blame] | 865 | |
| 866 | // Zygote args needs to be strings, so in order to pass ApplicationInfo, |
| 867 | // write it to a Parcel, and base64 the raw Parcel bytes to the other side. |
| 868 | Parcel parcel = Parcel.obtain(); |
| 869 | appInfo.writeToParcel(parcel, 0 /* flags */); |
| 870 | String encodedParcelData = Base64.getEncoder().encodeToString(parcel.marshall()); |
| 871 | parcel.recycle(); |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 872 | state.mZygoteOutputWriter.write(encodedParcelData); |
| 873 | state.mZygoteOutputWriter.newLine(); |
Martijn Coenen | 7e6fa67 | 2018-11-05 11:45:26 +0100 | [diff] [blame] | 874 | |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 875 | state.mZygoteOutputWriter.flush(); |
Martijn Coenen | 7e6fa67 | 2018-11-05 11:45:26 +0100 | [diff] [blame] | 876 | |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 877 | return (state.mZygoteInputStream.readInt() == 0); |
Martijn Coenen | 7e6fa67 | 2018-11-05 11:45:26 +0100 | [diff] [blame] | 878 | } |
| 879 | } |
| 880 | |
| 881 | /** |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 882 | * Instructs the zygote to pre-load the classes and native libraries at the given paths |
| 883 | * for the specified abi. Not all zygotes support this function. |
| 884 | */ |
Torne (Richard Coles) | f4f647e | 2018-02-21 16:12:36 -0500 | [diff] [blame] | 885 | public boolean preloadPackageForAbi(String packagePath, String libsPath, String libFileName, |
| 886 | String cacheKey, String abi) throws ZygoteStartFailedEx, |
| 887 | IOException { |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 888 | synchronized(mLock) { |
| 889 | ZygoteState state = openZygoteSocketIfNeeded(abi); |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 890 | state.mZygoteOutputWriter.write("5"); |
| 891 | state.mZygoteOutputWriter.newLine(); |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 892 | |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 893 | state.mZygoteOutputWriter.write("--preload-package"); |
| 894 | state.mZygoteOutputWriter.newLine(); |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 895 | |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 896 | state.mZygoteOutputWriter.write(packagePath); |
| 897 | state.mZygoteOutputWriter.newLine(); |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 898 | |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 899 | state.mZygoteOutputWriter.write(libsPath); |
| 900 | state.mZygoteOutputWriter.newLine(); |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 901 | |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 902 | state.mZygoteOutputWriter.write(libFileName); |
| 903 | state.mZygoteOutputWriter.newLine(); |
Torne (Richard Coles) | f4f647e | 2018-02-21 16:12:36 -0500 | [diff] [blame] | 904 | |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 905 | state.mZygoteOutputWriter.write(cacheKey); |
| 906 | state.mZygoteOutputWriter.newLine(); |
Torne (Richard Coles) | 0452670 | 2017-01-13 14:19:39 +0000 | [diff] [blame] | 907 | |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 908 | state.mZygoteOutputWriter.flush(); |
Narayan Kamath | bae484a | 2017-07-03 14:12:26 +0100 | [diff] [blame] | 909 | |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 910 | return (state.mZygoteInputStream.readInt() == 0); |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 911 | } |
| 912 | } |
Narayan Kamath | 669afcc | 2017-02-06 20:24:08 +0000 | [diff] [blame] | 913 | |
| 914 | /** |
| 915 | * Instructs the zygote to preload the default set of classes and resources. Returns |
| 916 | * {@code true} if a preload was performed as a result of this call, and {@code false} |
| 917 | * otherwise. The latter usually means that the zygote eagerly preloaded at startup |
| 918 | * or due to a previous call to {@code preloadDefault}. Note that this call is synchronous. |
| 919 | */ |
| 920 | public boolean preloadDefault(String abi) throws ZygoteStartFailedEx, IOException { |
| 921 | synchronized (mLock) { |
| 922 | ZygoteState state = openZygoteSocketIfNeeded(abi); |
| 923 | // Each query starts with the argument count (1 in this case) |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 924 | state.mZygoteOutputWriter.write("1"); |
| 925 | state.mZygoteOutputWriter.newLine(); |
| 926 | state.mZygoteOutputWriter.write("--preload-default"); |
| 927 | state.mZygoteOutputWriter.newLine(); |
| 928 | state.mZygoteOutputWriter.flush(); |
Narayan Kamath | 669afcc | 2017-02-06 20:24:08 +0000 | [diff] [blame] | 929 | |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 930 | return (state.mZygoteInputStream.readInt() == 0); |
Narayan Kamath | 669afcc | 2017-02-06 20:24:08 +0000 | [diff] [blame] | 931 | } |
| 932 | } |
Gustav Sennton | f0c52b5 | 2017-04-27 17:00:50 +0100 | [diff] [blame] | 933 | |
| 934 | /** |
| 935 | * Try connecting to the Zygote over and over again until we hit a time-out. |
| 936 | * @param socketName The name of the socket to connect to. |
| 937 | */ |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 938 | public static void waitForConnectionToZygote(String zygoteSocketName) { |
| 939 | final LocalSocketAddress zygoteSocketAddress = |
| 940 | new LocalSocketAddress(zygoteSocketName, LocalSocketAddress.Namespace.RESERVED); |
| 941 | waitForConnectionToZygote(zygoteSocketAddress); |
Robert Sesek | 5ac8abf | 2018-01-26 14:26:53 -0500 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | /** |
| 945 | * Try connecting to the Zygote over and over again until we hit a time-out. |
| 946 | * @param address The name of the socket to connect to. |
| 947 | */ |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 948 | public static void waitForConnectionToZygote(LocalSocketAddress zygoteSocketAddress) { |
Martijn Coenen | 82dc85a | 2019-01-28 13:12:28 +0100 | [diff] [blame] | 949 | int numRetries = ZYGOTE_CONNECT_TIMEOUT_MS / ZYGOTE_CONNECT_RETRY_DELAY_MS; |
| 950 | for (int n = numRetries; n >= 0; n--) { |
Gustav Sennton | f0c52b5 | 2017-04-27 17:00:50 +0100 | [diff] [blame] | 951 | try { |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 952 | final ZygoteState zs = |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 953 | ZygoteState.connect(zygoteSocketAddress, null); |
Gustav Sennton | f0c52b5 | 2017-04-27 17:00:50 +0100 | [diff] [blame] | 954 | zs.close(); |
| 955 | return; |
| 956 | } catch (IOException ioe) { |
| 957 | Log.w(LOG_TAG, |
| 958 | "Got error connecting to zygote, retrying. msg= " + ioe.getMessage()); |
| 959 | } |
| 960 | |
| 961 | try { |
Martijn Coenen | 82dc85a | 2019-01-28 13:12:28 +0100 | [diff] [blame] | 962 | Thread.sleep(ZYGOTE_CONNECT_RETRY_DELAY_MS); |
Gustav Sennton | f0c52b5 | 2017-04-27 17:00:50 +0100 | [diff] [blame] | 963 | } catch (InterruptedException ie) { |
| 964 | } |
| 965 | } |
Chris Wailes | efce929 | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 966 | Slog.wtf(LOG_TAG, "Failed to connect to Zygote through socket " |
| 967 | + zygoteSocketAddress.getName()); |
Gustav Sennton | f0c52b5 | 2017-04-27 17:00:50 +0100 | [diff] [blame] | 968 | } |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 969 | |
| 970 | /** |
| 971 | * Starts a new zygote process as a child of this zygote. This is used to create |
| 972 | * secondary zygotes that inherit data from the zygote that this object |
| 973 | * communicates with. This returns a new ZygoteProcess representing a connection |
| 974 | * to the newly created zygote. Throws an exception if the zygote cannot be started. |
Martijn Coenen | 7e6fa67 | 2018-11-05 11:45:26 +0100 | [diff] [blame] | 975 | * |
| 976 | * @param processClass The class to use as the child zygote's main entry |
| 977 | * point. |
| 978 | * @param niceName A more readable name to use for the process. |
| 979 | * @param uid The user-id under which the child zygote will run. |
| 980 | * @param gid The group-id under which the child zygote will run. |
| 981 | * @param gids Additional group-ids associated with the child zygote process. |
| 982 | * @param runtimeFlags Additional flags. |
| 983 | * @param seInfo null-ok SELinux information for the child zygote process. |
| 984 | * @param abi non-null the ABI of the child zygote |
| 985 | * @param acceptedAbiList ABIs this child zygote will accept connections for; this |
| 986 | * may be different from <code>abi</code> in case the children |
| 987 | * spawned from this Zygote only communicate using ABI-safe methods. |
| 988 | * @param instructionSet null-ok the instruction set to use. |
Martijn Coenen | 86f08a5 | 2019-01-03 16:23:01 +0100 | [diff] [blame] | 989 | * @param uidRangeStart The first UID in the range the child zygote may setuid()/setgid() to |
| 990 | * @param uidRangeEnd The last UID in the range the child zygote may setuid()/setgid() to |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 991 | */ |
| 992 | public ChildZygoteProcess startChildZygote(final String processClass, |
| 993 | final String niceName, |
| 994 | int uid, int gid, int[] gids, |
| 995 | int runtimeFlags, |
| 996 | String seInfo, |
| 997 | String abi, |
Martijn Coenen | 7e6fa67 | 2018-11-05 11:45:26 +0100 | [diff] [blame] | 998 | String acceptedAbiList, |
Martijn Coenen | 86f08a5 | 2019-01-03 16:23:01 +0100 | [diff] [blame] | 999 | String instructionSet, |
| 1000 | int uidRangeStart, |
| 1001 | int uidRangeEnd) { |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 1002 | // Create an unguessable address in the global abstract namespace. |
| 1003 | final LocalSocketAddress serverAddress = new LocalSocketAddress( |
| 1004 | processClass + "/" + UUID.randomUUID().toString()); |
| 1005 | |
Martijn Coenen | 7e6fa67 | 2018-11-05 11:45:26 +0100 | [diff] [blame] | 1006 | final String[] extraArgs = {Zygote.CHILD_ZYGOTE_SOCKET_NAME_ARG + serverAddress.getName(), |
Martijn Coenen | 86f08a5 | 2019-01-03 16:23:01 +0100 | [diff] [blame] | 1007 | Zygote.CHILD_ZYGOTE_ABI_LIST_ARG + acceptedAbiList, |
| 1008 | Zygote.CHILD_ZYGOTE_UID_RANGE_START + uidRangeStart, |
| 1009 | Zygote.CHILD_ZYGOTE_UID_RANGE_END + uidRangeEnd}; |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 1010 | |
| 1011 | Process.ProcessStartResult result; |
| 1012 | try { |
| 1013 | result = startViaZygote(processClass, niceName, uid, gid, |
| 1014 | gids, runtimeFlags, 0 /* mountExternal */, 0 /* targetSdkVersion */, seInfo, |
| 1015 | abi, instructionSet, null /* appDataDir */, null /* invokeWith */, |
Sudheer Shanka | 3f0645b | 2018-09-18 13:07:59 -0700 | [diff] [blame] | 1016 | true /* startChildZygote */, null /* packageName */, |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 1017 | null /* packagesForUid */, null /* visibleVolumes */, |
| 1018 | false /* useBlastulaPool */, extraArgs); |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 1019 | } catch (ZygoteStartFailedEx ex) { |
| 1020 | throw new RuntimeException("Starting child-zygote through Zygote failed", ex); |
| 1021 | } |
| 1022 | |
| 1023 | return new ChildZygoteProcess(serverAddress, result.pid); |
| 1024 | } |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 1025 | } |