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 | |
| 19 | import android.net.LocalSocket; |
| 20 | import android.net.LocalSocketAddress; |
| 21 | import android.util.Log; |
Gustav Sennton | f0c52b5 | 2017-04-27 17:00:50 +0100 | [diff] [blame] | 22 | import android.util.Slog; |
Nicolas Geoffray | 81edac4 | 2017-09-07 14:13:29 +0100 | [diff] [blame] | 23 | |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 24 | import com.android.internal.annotations.GuardedBy; |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 25 | import com.android.internal.os.Zygote; |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 26 | import com.android.internal.util.Preconditions; |
Nicolas Geoffray | 81edac4 | 2017-09-07 14:13:29 +0100 | [diff] [blame] | 27 | |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 28 | import java.io.BufferedWriter; |
| 29 | import java.io.DataInputStream; |
| 30 | import java.io.IOException; |
| 31 | import java.io.OutputStreamWriter; |
| 32 | import java.nio.charset.StandardCharsets; |
| 33 | import java.util.ArrayList; |
| 34 | import java.util.Arrays; |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 35 | import java.util.Collections; |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 36 | import java.util.List; |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 37 | import java.util.UUID; |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 38 | |
| 39 | /*package*/ class ZygoteStartFailedEx extends Exception { |
| 40 | ZygoteStartFailedEx(String s) { |
| 41 | super(s); |
| 42 | } |
| 43 | |
| 44 | ZygoteStartFailedEx(Throwable cause) { |
| 45 | super(cause); |
| 46 | } |
| 47 | |
| 48 | ZygoteStartFailedEx(String s, Throwable cause) { |
| 49 | super(s, cause); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Maintains communication state with the zygote processes. This class is responsible |
| 55 | * for the sockets opened to the zygotes and for starting processes on behalf of the |
| 56 | * {@link android.os.Process} class. |
| 57 | * |
| 58 | * {@hide} |
| 59 | */ |
| 60 | public class ZygoteProcess { |
| 61 | private static final String LOG_TAG = "ZygoteProcess"; |
| 62 | |
| 63 | /** |
| 64 | * The name of the socket used to communicate with the primary zygote. |
| 65 | */ |
Robert Sesek | 5ac8abf | 2018-01-26 14:26:53 -0500 | [diff] [blame] | 66 | private final LocalSocketAddress mSocket; |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 67 | |
| 68 | /** |
| 69 | * The name of the secondary (alternate ABI) zygote socket. |
| 70 | */ |
Robert Sesek | 5ac8abf | 2018-01-26 14:26:53 -0500 | [diff] [blame] | 71 | private final LocalSocketAddress mSecondarySocket; |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 72 | |
| 73 | public ZygoteProcess(String primarySocket, String secondarySocket) { |
Robert Sesek | 5ac8abf | 2018-01-26 14:26:53 -0500 | [diff] [blame] | 74 | this(new LocalSocketAddress(primarySocket, LocalSocketAddress.Namespace.RESERVED), |
| 75 | new LocalSocketAddress(secondarySocket, LocalSocketAddress.Namespace.RESERVED)); |
| 76 | } |
| 77 | |
| 78 | public ZygoteProcess(LocalSocketAddress primarySocket, LocalSocketAddress secondarySocket) { |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 79 | mSocket = primarySocket; |
| 80 | mSecondarySocket = secondarySocket; |
| 81 | } |
| 82 | |
Robert Sesek | 5ac8abf | 2018-01-26 14:26:53 -0500 | [diff] [blame] | 83 | public LocalSocketAddress getPrimarySocketAddress() { |
| 84 | return mSocket; |
| 85 | } |
| 86 | |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 87 | /** |
| 88 | * State for communicating with the zygote process. |
| 89 | */ |
| 90 | public static class ZygoteState { |
| 91 | final LocalSocket socket; |
| 92 | final DataInputStream inputStream; |
| 93 | final BufferedWriter writer; |
| 94 | final List<String> abiList; |
| 95 | |
| 96 | boolean mClosed; |
| 97 | |
| 98 | private ZygoteState(LocalSocket socket, DataInputStream inputStream, |
| 99 | BufferedWriter writer, List<String> abiList) { |
| 100 | this.socket = socket; |
| 101 | this.inputStream = inputStream; |
| 102 | this.writer = writer; |
| 103 | this.abiList = abiList; |
| 104 | } |
| 105 | |
Robert Sesek | 5ac8abf | 2018-01-26 14:26:53 -0500 | [diff] [blame] | 106 | public static ZygoteState connect(LocalSocketAddress address) throws IOException { |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 107 | DataInputStream zygoteInputStream = null; |
| 108 | BufferedWriter zygoteWriter = null; |
| 109 | final LocalSocket zygoteSocket = new LocalSocket(); |
| 110 | |
| 111 | try { |
Robert Sesek | 5ac8abf | 2018-01-26 14:26:53 -0500 | [diff] [blame] | 112 | zygoteSocket.connect(address); |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 113 | |
| 114 | zygoteInputStream = new DataInputStream(zygoteSocket.getInputStream()); |
| 115 | |
| 116 | zygoteWriter = new BufferedWriter(new OutputStreamWriter( |
| 117 | zygoteSocket.getOutputStream()), 256); |
| 118 | } catch (IOException ex) { |
| 119 | try { |
| 120 | zygoteSocket.close(); |
| 121 | } catch (IOException ignore) { |
| 122 | } |
| 123 | |
| 124 | throw ex; |
| 125 | } |
| 126 | |
| 127 | String abiListString = getAbiList(zygoteWriter, zygoteInputStream); |
Robert Sesek | 5ac8abf | 2018-01-26 14:26:53 -0500 | [diff] [blame] | 128 | Log.i("Zygote", "Process: zygote socket " + address.getNamespace() + "/" |
| 129 | + address.getName() + " opened, supported ABIS: " + abiListString); |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 130 | |
| 131 | return new ZygoteState(zygoteSocket, zygoteInputStream, zygoteWriter, |
| 132 | Arrays.asList(abiListString.split(","))); |
| 133 | } |
| 134 | |
| 135 | boolean matches(String abi) { |
| 136 | return abiList.contains(abi); |
| 137 | } |
| 138 | |
| 139 | public void close() { |
| 140 | try { |
| 141 | socket.close(); |
| 142 | } catch (IOException ex) { |
| 143 | Log.e(LOG_TAG,"I/O exception on routine close", ex); |
| 144 | } |
| 145 | |
| 146 | mClosed = true; |
| 147 | } |
| 148 | |
| 149 | boolean isClosed() { |
| 150 | return mClosed; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | /** |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 155 | * Lock object to protect access to the two ZygoteStates below. This lock must be |
| 156 | * acquired while communicating over the ZygoteState's socket, to prevent |
| 157 | * interleaved access. |
| 158 | */ |
| 159 | private final Object mLock = new Object(); |
| 160 | |
| 161 | /** |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 162 | * List of exemptions to the API blacklist. These are prefix matches on the runtime format |
| 163 | * symbol signature. Any matching symbol is treated by the runtime as being on the light grey |
| 164 | * list. |
| 165 | */ |
| 166 | private List<String> mApiBlacklistExemptions = Collections.emptyList(); |
| 167 | |
| 168 | /** |
Mathew Inwood | 04194fe | 2018-04-04 14:48:03 +0100 | [diff] [blame] | 169 | * Proportion of hidden API accesses that should be logged to the event log; 0 - 0x10000. |
| 170 | */ |
| 171 | private int mHiddenApiAccessLogSampleRate; |
| 172 | |
| 173 | /** |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 174 | * The state of the connection to the primary zygote. |
| 175 | */ |
| 176 | private ZygoteState primaryZygoteState; |
| 177 | |
| 178 | /** |
| 179 | * The state of the connection to the secondary zygote. |
| 180 | */ |
| 181 | private ZygoteState secondaryZygoteState; |
| 182 | |
| 183 | /** |
| 184 | * Start a new process. |
| 185 | * |
| 186 | * <p>If processes are enabled, a new process is created and the |
| 187 | * static main() function of a <var>processClass</var> is executed there. |
| 188 | * The process will continue running after this function returns. |
| 189 | * |
| 190 | * <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] | 191 | * process is created and main() of <var>processclass</var> called there. |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 192 | * |
| 193 | * <p>The niceName parameter, if not an empty string, is a custom name to |
| 194 | * give to the process instead of using processClass. This allows you to |
| 195 | * make easily identifyable processes even if you are using the same base |
| 196 | * <var>processClass</var> to start them. |
| 197 | * |
Tamas Berghammer | b8f7c35 | 2016-11-11 16:08:26 +0000 | [diff] [blame] | 198 | * 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] | 199 | * 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] | 200 | * runtimeFlags contains DEBUG_ENABLE_DEBUGGER. |
Tamas Berghammer | b8f7c35 | 2016-11-11 16:08:26 +0000 | [diff] [blame] | 201 | * |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 202 | * @param processClass The class to use as the process's main entry |
| 203 | * point. |
| 204 | * @param niceName A more readable name to use for the process. |
| 205 | * @param uid The user-id under which the process will run. |
| 206 | * @param gid The group-id under which the process will run. |
| 207 | * @param gids Additional group-ids associated with the process. |
Nicolas Geoffray | 81edac4 | 2017-09-07 14:13:29 +0100 | [diff] [blame] | 208 | * @param runtimeFlags Additional flags. |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 209 | * @param targetSdkVersion The target SDK version for the app. |
| 210 | * @param seInfo null-ok SELinux information for the new process. |
| 211 | * @param abi non-null the ABI this app should be started with. |
| 212 | * @param instructionSet null-ok the instruction set to use. |
| 213 | * @param appDataDir null-ok the data directory of the app. |
Tamas Berghammer | b8f7c35 | 2016-11-11 16:08:26 +0000 | [diff] [blame] | 214 | * @param invokeWith null-ok the command to invoke with. |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 215 | * @param zygoteArgs Additional arguments to supply to the zygote process. |
| 216 | * |
| 217 | * @return An object that describes the result of the attempt to start the process. |
| 218 | * @throws RuntimeException on fatal start failure |
| 219 | */ |
| 220 | public final Process.ProcessStartResult start(final String processClass, |
| 221 | final String niceName, |
| 222 | int uid, int gid, int[] gids, |
Nicolas Geoffray | 81edac4 | 2017-09-07 14:13:29 +0100 | [diff] [blame] | 223 | int runtimeFlags, int mountExternal, |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 224 | int targetSdkVersion, |
| 225 | String seInfo, |
| 226 | String abi, |
| 227 | String instructionSet, |
| 228 | String appDataDir, |
Tamas Berghammer | b8f7c35 | 2016-11-11 16:08:26 +0000 | [diff] [blame] | 229 | String invokeWith, |
Sudheer Shanka | 154fe3f | 2018-07-30 14:44:26 -0700 | [diff] [blame] | 230 | String packageName, |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 231 | String[] zygoteArgs) { |
| 232 | try { |
| 233 | return startViaZygote(processClass, niceName, uid, gid, gids, |
Nicolas Geoffray | 81edac4 | 2017-09-07 14:13:29 +0100 | [diff] [blame] | 234 | runtimeFlags, mountExternal, targetSdkVersion, seInfo, |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 235 | abi, instructionSet, appDataDir, invokeWith, false /* startChildZygote */, |
Sudheer Shanka | 154fe3f | 2018-07-30 14:44:26 -0700 | [diff] [blame] | 236 | packageName, zygoteArgs); |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 237 | } catch (ZygoteStartFailedEx ex) { |
| 238 | Log.e(LOG_TAG, |
| 239 | "Starting VM process through Zygote failed"); |
| 240 | throw new RuntimeException( |
| 241 | "Starting VM process through Zygote failed", ex); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | /** retry interval for opening a zygote socket */ |
| 246 | static final int ZYGOTE_RETRY_MILLIS = 500; |
| 247 | |
| 248 | /** |
| 249 | * Queries the zygote for the list of ABIS it supports. |
| 250 | * |
| 251 | * @throws ZygoteStartFailedEx if the query failed. |
| 252 | */ |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 253 | @GuardedBy("mLock") |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 254 | private static String getAbiList(BufferedWriter writer, DataInputStream inputStream) |
| 255 | throws IOException { |
| 256 | // Each query starts with the argument count (1 in this case) |
| 257 | writer.write("1"); |
| 258 | // ... followed by a new-line. |
| 259 | writer.newLine(); |
| 260 | // ... followed by our only argument. |
| 261 | writer.write("--query-abi-list"); |
| 262 | writer.newLine(); |
| 263 | writer.flush(); |
| 264 | |
| 265 | // The response is a length prefixed stream of ASCII bytes. |
| 266 | int numBytes = inputStream.readInt(); |
| 267 | byte[] bytes = new byte[numBytes]; |
| 268 | inputStream.readFully(bytes); |
| 269 | |
| 270 | return new String(bytes, StandardCharsets.US_ASCII); |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * Sends an argument list to the zygote process, which starts a new child |
| 275 | * and returns the child's pid. Please note: the present implementation |
| 276 | * replaces newlines in the argument list with spaces. |
| 277 | * |
| 278 | * @throws ZygoteStartFailedEx if process start failed for any reason |
| 279 | */ |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 280 | @GuardedBy("mLock") |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 281 | private static Process.ProcessStartResult zygoteSendArgsAndGetResult( |
| 282 | ZygoteState zygoteState, ArrayList<String> args) |
| 283 | throws ZygoteStartFailedEx { |
| 284 | try { |
Robert Sesek | 0b58f19 | 2016-10-10 18:34:42 -0400 | [diff] [blame] | 285 | // Throw early if any of the arguments are malformed. This means we can |
| 286 | // avoid writing a partial response to the zygote. |
| 287 | int sz = args.size(); |
| 288 | for (int i = 0; i < sz; i++) { |
| 289 | if (args.get(i).indexOf('\n') >= 0) { |
| 290 | throw new ZygoteStartFailedEx("embedded newlines not allowed"); |
| 291 | } |
| 292 | } |
| 293 | |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 294 | /** |
| 295 | * See com.android.internal.os.SystemZygoteInit.readArgumentList() |
| 296 | * Presently the wire format to the zygote process is: |
| 297 | * a) a count of arguments (argc, in essence) |
| 298 | * b) a number of newline-separated argument strings equal to count |
| 299 | * |
| 300 | * After the zygote process reads these it will write the pid of |
| 301 | * the child or -1 on failure, followed by boolean to |
| 302 | * indicate whether a wrapper process was used. |
| 303 | */ |
| 304 | final BufferedWriter writer = zygoteState.writer; |
| 305 | final DataInputStream inputStream = zygoteState.inputStream; |
| 306 | |
| 307 | writer.write(Integer.toString(args.size())); |
| 308 | writer.newLine(); |
| 309 | |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 310 | for (int i = 0; i < sz; i++) { |
| 311 | String arg = args.get(i); |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 312 | writer.write(arg); |
| 313 | writer.newLine(); |
| 314 | } |
| 315 | |
| 316 | writer.flush(); |
| 317 | |
| 318 | // Should there be a timeout on this? |
| 319 | Process.ProcessStartResult result = new Process.ProcessStartResult(); |
Robert Sesek | 0b58f19 | 2016-10-10 18:34:42 -0400 | [diff] [blame] | 320 | |
| 321 | // Always read the entire result from the input stream to avoid leaving |
| 322 | // bytes in the stream for future process starts to accidentally stumble |
| 323 | // upon. |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 324 | result.pid = inputStream.readInt(); |
Robert Sesek | 0b58f19 | 2016-10-10 18:34:42 -0400 | [diff] [blame] | 325 | result.usingWrapper = inputStream.readBoolean(); |
| 326 | |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 327 | if (result.pid < 0) { |
| 328 | throw new ZygoteStartFailedEx("fork() failed"); |
| 329 | } |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 330 | return result; |
| 331 | } catch (IOException ex) { |
| 332 | zygoteState.close(); |
| 333 | throw new ZygoteStartFailedEx(ex); |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | * Starts a new process via the zygote mechanism. |
| 339 | * |
| 340 | * @param processClass Class name whose static main() to run |
| 341 | * @param niceName 'nice' process name to appear in ps |
| 342 | * @param uid a POSIX uid that the new process should setuid() to |
| 343 | * @param gid a POSIX gid that the new process shuold setgid() to |
| 344 | * @param gids null-ok; a list of supplementary group IDs that the |
| 345 | * new process should setgroup() to. |
Nicolas Geoffray | 81edac4 | 2017-09-07 14:13:29 +0100 | [diff] [blame] | 346 | * @param runtimeFlags Additional flags for the runtime. |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 347 | * @param targetSdkVersion The target SDK version for the app. |
| 348 | * @param seInfo null-ok SELinux information for the new process. |
| 349 | * @param abi the ABI the process should use. |
| 350 | * @param instructionSet null-ok the instruction set to use. |
| 351 | * @param appDataDir null-ok the data directory of the app. |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 352 | * @param startChildZygote Start a sub-zygote. This creates a new zygote process |
| 353 | * that has its state cloned from this zygote process. |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 354 | * @param extraArgs Additional arguments to supply to the zygote process. |
| 355 | * @return An object that describes the result of the attempt to start the process. |
| 356 | * @throws ZygoteStartFailedEx if process start failed for any reason |
| 357 | */ |
| 358 | private Process.ProcessStartResult startViaZygote(final String processClass, |
| 359 | final String niceName, |
| 360 | final int uid, final int gid, |
| 361 | final int[] gids, |
Nicolas Geoffray | 81edac4 | 2017-09-07 14:13:29 +0100 | [diff] [blame] | 362 | int runtimeFlags, int mountExternal, |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 363 | int targetSdkVersion, |
| 364 | String seInfo, |
| 365 | String abi, |
| 366 | String instructionSet, |
| 367 | String appDataDir, |
Tamas Berghammer | b8f7c35 | 2016-11-11 16:08:26 +0000 | [diff] [blame] | 368 | String invokeWith, |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 369 | boolean startChildZygote, |
Sudheer Shanka | 154fe3f | 2018-07-30 14:44:26 -0700 | [diff] [blame] | 370 | String packageName, |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 371 | String[] extraArgs) |
| 372 | throws ZygoteStartFailedEx { |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 373 | ArrayList<String> argsForZygote = new ArrayList<String>(); |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 374 | |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 375 | // --runtime-args, --setuid=, --setgid=, |
| 376 | // and --setgroups= must go first |
| 377 | argsForZygote.add("--runtime-args"); |
| 378 | argsForZygote.add("--setuid=" + uid); |
| 379 | argsForZygote.add("--setgid=" + gid); |
Nicolas Geoffray | 81edac4 | 2017-09-07 14:13:29 +0100 | [diff] [blame] | 380 | argsForZygote.add("--runtime-flags=" + runtimeFlags); |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 381 | if (mountExternal == Zygote.MOUNT_EXTERNAL_DEFAULT) { |
| 382 | argsForZygote.add("--mount-external-default"); |
| 383 | } else if (mountExternal == Zygote.MOUNT_EXTERNAL_READ) { |
| 384 | argsForZygote.add("--mount-external-read"); |
| 385 | } else if (mountExternal == Zygote.MOUNT_EXTERNAL_WRITE) { |
| 386 | argsForZygote.add("--mount-external-write"); |
Sudheer Shanka | 98cb3f0 | 2018-08-17 16:10:29 -0700 | [diff] [blame^] | 387 | } else if (mountExternal == Zygote.MOUNT_EXTERNAL_FULL) { |
| 388 | argsForZygote.add("--mount-external-full"); |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 389 | } |
Sudheer Shanka | 98cb3f0 | 2018-08-17 16:10:29 -0700 | [diff] [blame^] | 390 | |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 391 | argsForZygote.add("--target-sdk-version=" + targetSdkVersion); |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 392 | |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 393 | // --setgroups is a comma-separated list |
| 394 | if (gids != null && gids.length > 0) { |
| 395 | StringBuilder sb = new StringBuilder(); |
| 396 | sb.append("--setgroups="); |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 397 | |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 398 | int sz = gids.length; |
| 399 | for (int i = 0; i < sz; i++) { |
| 400 | if (i != 0) { |
| 401 | sb.append(','); |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 402 | } |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 403 | sb.append(gids[i]); |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 404 | } |
| 405 | |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 406 | argsForZygote.add(sb.toString()); |
| 407 | } |
| 408 | |
| 409 | if (niceName != null) { |
| 410 | argsForZygote.add("--nice-name=" + niceName); |
| 411 | } |
| 412 | |
| 413 | if (seInfo != null) { |
| 414 | argsForZygote.add("--seinfo=" + seInfo); |
| 415 | } |
| 416 | |
| 417 | if (instructionSet != null) { |
| 418 | argsForZygote.add("--instruction-set=" + instructionSet); |
| 419 | } |
| 420 | |
| 421 | if (appDataDir != null) { |
| 422 | argsForZygote.add("--app-data-dir=" + appDataDir); |
| 423 | } |
| 424 | |
Tamas Berghammer | b8f7c35 | 2016-11-11 16:08:26 +0000 | [diff] [blame] | 425 | if (invokeWith != null) { |
| 426 | argsForZygote.add("--invoke-with"); |
| 427 | argsForZygote.add(invokeWith); |
| 428 | } |
| 429 | |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 430 | if (startChildZygote) { |
| 431 | argsForZygote.add("--start-child-zygote"); |
| 432 | } |
| 433 | |
Sudheer Shanka | 154fe3f | 2018-07-30 14:44:26 -0700 | [diff] [blame] | 434 | if (packageName != null) { |
| 435 | argsForZygote.add("--package-name=" + packageName); |
| 436 | } |
| 437 | |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 438 | argsForZygote.add(processClass); |
| 439 | |
| 440 | if (extraArgs != null) { |
| 441 | for (String arg : extraArgs) { |
| 442 | argsForZygote.add(arg); |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 443 | } |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 444 | } |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 445 | |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 446 | synchronized(mLock) { |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 447 | return zygoteSendArgsAndGetResult(openZygoteSocketIfNeeded(abi), argsForZygote); |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | /** |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 452 | * Closes the connections to the zygote, if they exist. |
| 453 | */ |
| 454 | public void close() { |
| 455 | if (primaryZygoteState != null) { |
| 456 | primaryZygoteState.close(); |
| 457 | } |
| 458 | if (secondaryZygoteState != null) { |
| 459 | secondaryZygoteState.close(); |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | /** |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 464 | * Tries to establish a connection to the zygote that handles a given {@code abi}. Might block |
| 465 | * and retry if the zygote is unresponsive. This method is a no-op if a connection is |
| 466 | * already open. |
| 467 | */ |
| 468 | public void establishZygoteConnectionForAbi(String abi) { |
| 469 | try { |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 470 | synchronized(mLock) { |
| 471 | openZygoteSocketIfNeeded(abi); |
| 472 | } |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 473 | } catch (ZygoteStartFailedEx ex) { |
| 474 | throw new RuntimeException("Unable to connect to zygote for abi: " + abi, ex); |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | /** |
Andreas Gampe | 8444dca | 2018-05-01 13:31:28 -0700 | [diff] [blame] | 479 | * Attempt to retrieve the PID of the zygote serving the given abi. |
| 480 | */ |
| 481 | public int getZygotePid(String abi) { |
| 482 | try { |
| 483 | synchronized (mLock) { |
| 484 | ZygoteState state = openZygoteSocketIfNeeded(abi); |
| 485 | |
| 486 | // Each query starts with the argument count (1 in this case) |
| 487 | state.writer.write("1"); |
| 488 | // ... followed by a new-line. |
| 489 | state.writer.newLine(); |
| 490 | // ... followed by our only argument. |
| 491 | state.writer.write("--get-pid"); |
| 492 | state.writer.newLine(); |
| 493 | state.writer.flush(); |
| 494 | |
| 495 | // The response is a length prefixed stream of ASCII bytes. |
| 496 | int numBytes = state.inputStream.readInt(); |
| 497 | byte[] bytes = new byte[numBytes]; |
| 498 | state.inputStream.readFully(bytes); |
| 499 | |
| 500 | return Integer.parseInt(new String(bytes, StandardCharsets.US_ASCII)); |
| 501 | } |
| 502 | } catch (Exception ex) { |
| 503 | throw new RuntimeException("Failure retrieving pid", ex); |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | /** |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 508 | * Push hidden API blacklisting exemptions into the zygote process(es). |
| 509 | * |
| 510 | * <p>The list of exemptions will take affect for all new processes forked from the zygote after |
| 511 | * this call. |
| 512 | * |
Mathew Inwood | 33d5138 | 2018-04-05 13:56:39 +0100 | [diff] [blame] | 513 | * @param exemptions List of hidden API exemption prefixes. Any matching members are treated as |
| 514 | * whitelisted/public APIs (i.e. allowed, no logging of usage). |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 515 | */ |
Mathew Inwood | 9f6bb5b | 2018-04-09 17:29:12 +0100 | [diff] [blame] | 516 | public boolean setApiBlacklistExemptions(List<String> exemptions) { |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 517 | synchronized (mLock) { |
| 518 | mApiBlacklistExemptions = exemptions; |
Mathew Inwood | 9f6bb5b | 2018-04-09 17:29:12 +0100 | [diff] [blame] | 519 | boolean ok = maybeSetApiBlacklistExemptions(primaryZygoteState, true); |
| 520 | if (ok) { |
| 521 | ok = maybeSetApiBlacklistExemptions(secondaryZygoteState, true); |
| 522 | } |
| 523 | return ok; |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 524 | } |
| 525 | } |
| 526 | |
Mathew Inwood | 04194fe | 2018-04-04 14:48:03 +0100 | [diff] [blame] | 527 | /** |
| 528 | * Set the precentage of detected hidden API accesses that are logged to the event log. |
| 529 | * |
| 530 | * <p>This rate will take affect for all new processes forked from the zygote after this call. |
| 531 | * |
| 532 | * @param rate An integer between 0 and 0x10000 inclusive. 0 means no event logging. |
| 533 | */ |
| 534 | public void setHiddenApiAccessLogSampleRate(int rate) { |
| 535 | synchronized (mLock) { |
| 536 | mHiddenApiAccessLogSampleRate = rate; |
| 537 | maybeSetHiddenApiAccessLogSampleRate(primaryZygoteState); |
| 538 | maybeSetHiddenApiAccessLogSampleRate(secondaryZygoteState); |
| 539 | } |
| 540 | } |
| 541 | |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 542 | @GuardedBy("mLock") |
Mathew Inwood | 9f6bb5b | 2018-04-09 17:29:12 +0100 | [diff] [blame] | 543 | private boolean maybeSetApiBlacklistExemptions(ZygoteState state, boolean sendIfEmpty) { |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 544 | if (state == null || state.isClosed()) { |
Mathew Inwood | 9f6bb5b | 2018-04-09 17:29:12 +0100 | [diff] [blame] | 545 | Slog.e(LOG_TAG, "Can't set API blacklist exemptions: no zygote connection"); |
| 546 | return false; |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 547 | } |
| 548 | if (!sendIfEmpty && mApiBlacklistExemptions.isEmpty()) { |
Mathew Inwood | 9f6bb5b | 2018-04-09 17:29:12 +0100 | [diff] [blame] | 549 | return true; |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 550 | } |
| 551 | try { |
| 552 | state.writer.write(Integer.toString(mApiBlacklistExemptions.size() + 1)); |
| 553 | state.writer.newLine(); |
| 554 | state.writer.write("--set-api-blacklist-exemptions"); |
| 555 | state.writer.newLine(); |
| 556 | for (int i = 0; i < mApiBlacklistExemptions.size(); ++i) { |
| 557 | state.writer.write(mApiBlacklistExemptions.get(i)); |
| 558 | state.writer.newLine(); |
| 559 | } |
| 560 | state.writer.flush(); |
| 561 | int status = state.inputStream.readInt(); |
| 562 | if (status != 0) { |
| 563 | Slog.e(LOG_TAG, "Failed to set API blacklist exemptions; status " + status); |
| 564 | } |
Mathew Inwood | 9f6bb5b | 2018-04-09 17:29:12 +0100 | [diff] [blame] | 565 | return true; |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 566 | } catch (IOException ioe) { |
| 567 | Slog.e(LOG_TAG, "Failed to set API blacklist exemptions", ioe); |
Mathew Inwood | 9f6bb5b | 2018-04-09 17:29:12 +0100 | [diff] [blame] | 568 | mApiBlacklistExemptions = Collections.emptyList(); |
| 569 | return false; |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 570 | } |
| 571 | } |
| 572 | |
Mathew Inwood | 04194fe | 2018-04-04 14:48:03 +0100 | [diff] [blame] | 573 | private void maybeSetHiddenApiAccessLogSampleRate(ZygoteState state) { |
| 574 | if (state == null || state.isClosed()) { |
| 575 | return; |
| 576 | } |
| 577 | if (mHiddenApiAccessLogSampleRate == -1) { |
| 578 | return; |
| 579 | } |
| 580 | try { |
| 581 | state.writer.write(Integer.toString(1)); |
| 582 | state.writer.newLine(); |
| 583 | state.writer.write("--hidden-api-log-sampling-rate=" |
| 584 | + Integer.toString(mHiddenApiAccessLogSampleRate)); |
| 585 | state.writer.newLine(); |
| 586 | state.writer.flush(); |
| 587 | int status = state.inputStream.readInt(); |
| 588 | if (status != 0) { |
| 589 | Slog.e(LOG_TAG, "Failed to set hidden API log sampling rate; status " + status); |
| 590 | } |
| 591 | } catch (IOException ioe) { |
| 592 | Slog.e(LOG_TAG, "Failed to set hidden API log sampling rate", ioe); |
| 593 | } |
| 594 | } |
| 595 | |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 596 | /** |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 597 | * Tries to open socket to Zygote process if not already open. If |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 598 | * already open, does nothing. May block and retry. Requires that mLock be held. |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 599 | */ |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 600 | @GuardedBy("mLock") |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 601 | private ZygoteState openZygoteSocketIfNeeded(String abi) throws ZygoteStartFailedEx { |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 602 | Preconditions.checkState(Thread.holdsLock(mLock), "ZygoteProcess lock not held"); |
| 603 | |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 604 | if (primaryZygoteState == null || primaryZygoteState.isClosed()) { |
| 605 | try { |
| 606 | primaryZygoteState = ZygoteState.connect(mSocket); |
| 607 | } catch (IOException ioe) { |
| 608 | throw new ZygoteStartFailedEx("Error connecting to primary zygote", ioe); |
| 609 | } |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 610 | maybeSetApiBlacklistExemptions(primaryZygoteState, false); |
Mathew Inwood | 04194fe | 2018-04-04 14:48:03 +0100 | [diff] [blame] | 611 | maybeSetHiddenApiAccessLogSampleRate(primaryZygoteState); |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 612 | } |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 613 | if (primaryZygoteState.matches(abi)) { |
| 614 | return primaryZygoteState; |
| 615 | } |
| 616 | |
| 617 | // The primary zygote didn't match. Try the secondary. |
| 618 | if (secondaryZygoteState == null || secondaryZygoteState.isClosed()) { |
| 619 | try { |
| 620 | secondaryZygoteState = ZygoteState.connect(mSecondarySocket); |
| 621 | } catch (IOException ioe) { |
| 622 | throw new ZygoteStartFailedEx("Error connecting to secondary zygote", ioe); |
| 623 | } |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 624 | maybeSetApiBlacklistExemptions(secondaryZygoteState, false); |
Mathew Inwood | 04194fe | 2018-04-04 14:48:03 +0100 | [diff] [blame] | 625 | maybeSetHiddenApiAccessLogSampleRate(secondaryZygoteState); |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | if (secondaryZygoteState.matches(abi)) { |
| 629 | return secondaryZygoteState; |
| 630 | } |
| 631 | |
| 632 | throw new ZygoteStartFailedEx("Unsupported zygote ABI: " + abi); |
| 633 | } |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 634 | |
| 635 | /** |
| 636 | * Instructs the zygote to pre-load the classes and native libraries at the given paths |
| 637 | * for the specified abi. Not all zygotes support this function. |
| 638 | */ |
Torne (Richard Coles) | f4f647e | 2018-02-21 16:12:36 -0500 | [diff] [blame] | 639 | public boolean preloadPackageForAbi(String packagePath, String libsPath, String libFileName, |
| 640 | String cacheKey, String abi) throws ZygoteStartFailedEx, |
| 641 | IOException { |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 642 | synchronized(mLock) { |
| 643 | ZygoteState state = openZygoteSocketIfNeeded(abi); |
Torne (Richard Coles) | f4f647e | 2018-02-21 16:12:36 -0500 | [diff] [blame] | 644 | state.writer.write("5"); |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 645 | state.writer.newLine(); |
| 646 | |
| 647 | state.writer.write("--preload-package"); |
| 648 | state.writer.newLine(); |
| 649 | |
| 650 | state.writer.write(packagePath); |
| 651 | state.writer.newLine(); |
| 652 | |
| 653 | state.writer.write(libsPath); |
| 654 | state.writer.newLine(); |
| 655 | |
Torne (Richard Coles) | f4f647e | 2018-02-21 16:12:36 -0500 | [diff] [blame] | 656 | state.writer.write(libFileName); |
| 657 | state.writer.newLine(); |
| 658 | |
Torne (Richard Coles) | 0452670 | 2017-01-13 14:19:39 +0000 | [diff] [blame] | 659 | state.writer.write(cacheKey); |
| 660 | state.writer.newLine(); |
| 661 | |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 662 | state.writer.flush(); |
Narayan Kamath | bae484a | 2017-07-03 14:12:26 +0100 | [diff] [blame] | 663 | |
| 664 | return (state.inputStream.readInt() == 0); |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 665 | } |
| 666 | } |
Narayan Kamath | 669afcc | 2017-02-06 20:24:08 +0000 | [diff] [blame] | 667 | |
| 668 | /** |
| 669 | * Instructs the zygote to preload the default set of classes and resources. Returns |
| 670 | * {@code true} if a preload was performed as a result of this call, and {@code false} |
| 671 | * otherwise. The latter usually means that the zygote eagerly preloaded at startup |
| 672 | * or due to a previous call to {@code preloadDefault}. Note that this call is synchronous. |
| 673 | */ |
| 674 | public boolean preloadDefault(String abi) throws ZygoteStartFailedEx, IOException { |
| 675 | synchronized (mLock) { |
| 676 | ZygoteState state = openZygoteSocketIfNeeded(abi); |
| 677 | // Each query starts with the argument count (1 in this case) |
| 678 | state.writer.write("1"); |
| 679 | state.writer.newLine(); |
| 680 | state.writer.write("--preload-default"); |
| 681 | state.writer.newLine(); |
| 682 | state.writer.flush(); |
| 683 | |
| 684 | return (state.inputStream.readInt() == 0); |
| 685 | } |
| 686 | } |
Gustav Sennton | f0c52b5 | 2017-04-27 17:00:50 +0100 | [diff] [blame] | 687 | |
| 688 | /** |
| 689 | * Try connecting to the Zygote over and over again until we hit a time-out. |
| 690 | * @param socketName The name of the socket to connect to. |
| 691 | */ |
| 692 | public static void waitForConnectionToZygote(String socketName) { |
Robert Sesek | 5ac8abf | 2018-01-26 14:26:53 -0500 | [diff] [blame] | 693 | final LocalSocketAddress address = |
| 694 | new LocalSocketAddress(socketName, LocalSocketAddress.Namespace.RESERVED); |
| 695 | waitForConnectionToZygote(address); |
| 696 | } |
| 697 | |
| 698 | /** |
| 699 | * Try connecting to the Zygote over and over again until we hit a time-out. |
| 700 | * @param address The name of the socket to connect to. |
| 701 | */ |
| 702 | public static void waitForConnectionToZygote(LocalSocketAddress address) { |
Gustav Sennton | f0c52b5 | 2017-04-27 17:00:50 +0100 | [diff] [blame] | 703 | for (int n = 20; n >= 0; n--) { |
| 704 | try { |
Robert Sesek | 5ac8abf | 2018-01-26 14:26:53 -0500 | [diff] [blame] | 705 | final ZygoteState zs = ZygoteState.connect(address); |
Gustav Sennton | f0c52b5 | 2017-04-27 17:00:50 +0100 | [diff] [blame] | 706 | zs.close(); |
| 707 | return; |
| 708 | } catch (IOException ioe) { |
| 709 | Log.w(LOG_TAG, |
| 710 | "Got error connecting to zygote, retrying. msg= " + ioe.getMessage()); |
| 711 | } |
| 712 | |
| 713 | try { |
| 714 | Thread.sleep(1000); |
| 715 | } catch (InterruptedException ie) { |
| 716 | } |
| 717 | } |
Robert Sesek | 5ac8abf | 2018-01-26 14:26:53 -0500 | [diff] [blame] | 718 | Slog.wtf(LOG_TAG, "Failed to connect to Zygote through socket " + address.getName()); |
Gustav Sennton | f0c52b5 | 2017-04-27 17:00:50 +0100 | [diff] [blame] | 719 | } |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 720 | |
| 721 | /** |
| 722 | * Starts a new zygote process as a child of this zygote. This is used to create |
| 723 | * secondary zygotes that inherit data from the zygote that this object |
| 724 | * communicates with. This returns a new ZygoteProcess representing a connection |
| 725 | * to the newly created zygote. Throws an exception if the zygote cannot be started. |
| 726 | */ |
| 727 | public ChildZygoteProcess startChildZygote(final String processClass, |
| 728 | final String niceName, |
| 729 | int uid, int gid, int[] gids, |
| 730 | int runtimeFlags, |
| 731 | String seInfo, |
| 732 | String abi, |
| 733 | String instructionSet) { |
| 734 | // Create an unguessable address in the global abstract namespace. |
| 735 | final LocalSocketAddress serverAddress = new LocalSocketAddress( |
| 736 | processClass + "/" + UUID.randomUUID().toString()); |
| 737 | |
| 738 | final String[] extraArgs = {Zygote.CHILD_ZYGOTE_SOCKET_NAME_ARG + serverAddress.getName()}; |
| 739 | |
| 740 | Process.ProcessStartResult result; |
| 741 | try { |
| 742 | result = startViaZygote(processClass, niceName, uid, gid, |
| 743 | gids, runtimeFlags, 0 /* mountExternal */, 0 /* targetSdkVersion */, seInfo, |
| 744 | abi, instructionSet, null /* appDataDir */, null /* invokeWith */, |
Sudheer Shanka | 154fe3f | 2018-07-30 14:44:26 -0700 | [diff] [blame] | 745 | true /* startChildZygote */, null /* packageName */, extraArgs); |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 746 | } catch (ZygoteStartFailedEx ex) { |
| 747 | throw new RuntimeException("Starting child-zygote through Zygote failed", ex); |
| 748 | } |
| 749 | |
| 750 | return new ChildZygoteProcess(serverAddress, result.pid); |
| 751 | } |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 752 | } |