The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.internal.os; |
| 18 | |
Narayan Kamath | 6ac7e67 | 2015-01-16 16:26:54 +0000 | [diff] [blame] | 19 | import static android.system.OsConstants.F_SETFD; |
Elliott Hughes | 3fe5951 | 2014-12-12 14:07:34 -0800 | [diff] [blame] | 20 | import static android.system.OsConstants.O_CLOEXEC; |
Andreas Gampe | cbc4214 | 2017-07-12 19:17:52 -0700 | [diff] [blame] | 21 | import static android.system.OsConstants.POLLIN; |
Elliott Hughes | dac83f5 | 2014-12-15 11:00:25 -0800 | [diff] [blame] | 22 | import static android.system.OsConstants.STDERR_FILENO; |
| 23 | import static android.system.OsConstants.STDIN_FILENO; |
| 24 | import static android.system.OsConstants.STDOUT_FILENO; |
Sudheer Shanka | d81b1d7 | 2018-09-05 16:37:30 -0700 | [diff] [blame] | 25 | |
Andreas Gampe | 27497c6 | 2017-07-21 11:41:00 -0700 | [diff] [blame] | 26 | import static com.android.internal.os.ZygoteConnectionConstants.CONNECTION_TIMEOUT_MILLIS; |
Andreas Gampe | 27497c6 | 2017-07-21 11:41:00 -0700 | [diff] [blame] | 27 | import static com.android.internal.os.ZygoteConnectionConstants.WRAPPED_PID_TIMEOUT_MILLIS; |
Elliott Hughes | 3fe5951 | 2014-12-12 14:07:34 -0800 | [diff] [blame] | 28 | |
Martijn Coenen | 7e6fa67 | 2018-11-05 11:45:26 +0100 | [diff] [blame] | 29 | import android.content.pm.ApplicationInfo; |
Andrei Onea | 6cd1b70 | 2019-01-25 16:29:44 +0000 | [diff] [blame] | 30 | import android.metrics.LogMaker; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | import android.net.Credentials; |
| 32 | import android.net.LocalSocket; |
Martijn Coenen | 7e6fa67 | 2018-11-05 11:45:26 +0100 | [diff] [blame] | 33 | import android.os.Parcel; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | import android.os.Process; |
Narayan Kamath | fbb32f6 | 2015-06-12 15:34:35 +0100 | [diff] [blame] | 35 | import android.os.Trace; |
Elliott Hughes | 860c591 | 2014-04-28 19:19:13 -0700 | [diff] [blame] | 36 | import android.system.ErrnoException; |
| 37 | import android.system.Os; |
Andreas Gampe | cbc4214 | 2017-07-12 19:17:52 -0700 | [diff] [blame] | 38 | import android.system.StructPollfd; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | import android.util.Log; |
Sudheer Shanka | d81b1d7 | 2018-09-05 16:37:30 -0700 | [diff] [blame] | 40 | |
Andrei Onea | 6cd1b70 | 2019-01-25 16:29:44 +0000 | [diff] [blame] | 41 | import com.android.internal.logging.MetricsLogger; |
| 42 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
| 43 | |
Narayan Kamath | 37ad4b0 | 2015-01-19 16:05:24 +0000 | [diff] [blame] | 44 | import dalvik.system.VMRuntime; |
Sudheer Shanka | d81b1d7 | 2018-09-05 16:37:30 -0700 | [diff] [blame] | 45 | |
| 46 | import libcore.io.IoUtils; |
| 47 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | import java.io.BufferedReader; |
Andreas Gampe | cbc4214 | 2017-07-12 19:17:52 -0700 | [diff] [blame] | 49 | import java.io.ByteArrayInputStream; |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 50 | import java.io.DataInputStream; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | import java.io.DataOutputStream; |
| 52 | import java.io.FileDescriptor; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | import java.io.IOException; |
| 54 | import java.io.InputStreamReader; |
Narayan Kamath | c41638c | 2014-04-07 13:56:15 +0100 | [diff] [blame] | 55 | import java.nio.charset.StandardCharsets; |
Martijn Coenen | 7e6fa67 | 2018-11-05 11:45:26 +0100 | [diff] [blame] | 56 | import java.util.Base64; |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 57 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | /** |
| 59 | * A connection that can make spawn requests. |
| 60 | */ |
| 61 | class ZygoteConnection { |
| 62 | private static final String TAG = "Zygote"; |
| 63 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | * The command socket. |
| 66 | * |
| 67 | * mSocket is retained in the child process in "peer wait" mode, so |
| 68 | * that it closes when the child process terminates. In other cases, |
| 69 | * it is closed in the peer. |
| 70 | */ |
| 71 | private final LocalSocket mSocket; |
| 72 | private final DataOutputStream mSocketOutStream; |
| 73 | private final BufferedReader mSocketReader; |
| 74 | private final Credentials peer; |
Narayan Kamath | c41638c | 2014-04-07 13:56:15 +0100 | [diff] [blame] | 75 | private final String abiList; |
Narayan Kamath | bf99d06 | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 76 | private boolean isEof; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | |
| 78 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | * Constructs instance from connected socket. |
| 80 | * |
| 81 | * @param socket non-null; connected socket |
Narayan Kamath | c41638c | 2014-04-07 13:56:15 +0100 | [diff] [blame] | 82 | * @param abiList non-null; a list of ABIs this zygote supports. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | * @throws IOException |
| 84 | */ |
Narayan Kamath | c41638c | 2014-04-07 13:56:15 +0100 | [diff] [blame] | 85 | ZygoteConnection(LocalSocket socket, String abiList) throws IOException { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | mSocket = socket; |
Narayan Kamath | c41638c | 2014-04-07 13:56:15 +0100 | [diff] [blame] | 87 | this.abiList = abiList; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | |
| 89 | mSocketOutStream |
| 90 | = new DataOutputStream(socket.getOutputStream()); |
| 91 | |
| 92 | mSocketReader = new BufferedReader( |
| 93 | new InputStreamReader(socket.getInputStream()), 256); |
| 94 | |
| 95 | mSocket.setSoTimeout(CONNECTION_TIMEOUT_MILLIS); |
dcashman | fc4c0bf8 | 2015-03-05 17:17:47 -0800 | [diff] [blame] | 96 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | try { |
| 98 | peer = mSocket.getPeerCredentials(); |
| 99 | } catch (IOException ex) { |
| 100 | Log.e(TAG, "Cannot read peer credentials", ex); |
| 101 | throw ex; |
| 102 | } |
Narayan Kamath | bf99d06 | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 103 | |
| 104 | isEof = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Returns the file descriptor of the associated socket. |
| 109 | * |
| 110 | * @return null-ok; file descriptor |
| 111 | */ |
Chris Wailes | 2be2626 | 2019-01-11 16:14:43 -0800 | [diff] [blame] | 112 | FileDescriptor getFileDescriptor() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | return mSocket.getFileDescriptor(); |
| 114 | } |
| 115 | |
| 116 | /** |
Narayan Kamath | bf99d06 | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 117 | * Reads one start command from the command socket. If successful, a child is forked and a |
| 118 | * {@code Runnable} that calls the childs main method (or equivalent) is returned in the child |
| 119 | * process. {@code null} is always returned in the parent process (the zygote). |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | * |
Narayan Kamath | bf99d06 | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 121 | * If the client closes the socket, an {@code EOF} condition is set, which callers can test |
| 122 | * for by calling {@code ZygoteConnection.isClosedByPeer}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 123 | */ |
Narayan Kamath | bf99d06 | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 124 | Runnable processOneCommand(ZygoteServer zygoteServer) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 125 | String args[]; |
Chris Wailes | 2be2626 | 2019-01-11 16:14:43 -0800 | [diff] [blame] | 126 | ZygoteArguments parsedArgs = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 127 | FileDescriptor[] descriptors; |
| 128 | |
| 129 | try { |
Chris Wailes | 2be2626 | 2019-01-11 16:14:43 -0800 | [diff] [blame] | 130 | args = Zygote.readArgumentList(mSocketReader); |
| 131 | |
| 132 | // TODO (chriswailes): Remove this and add an assert. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | descriptors = mSocket.getAncillaryFileDescriptors(); |
| 134 | } catch (IOException ex) { |
Narayan Kamath | bf99d06 | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 135 | throw new IllegalStateException("IOException on command socket", ex); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 136 | } |
| 137 | |
Narayan Kamath | bf99d06 | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 138 | // readArgumentList returns null only when it has reached EOF with no available |
| 139 | // data to read. This will only happen when the remote socket has disconnected. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 140 | if (args == null) { |
Narayan Kamath | bf99d06 | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 141 | isEof = true; |
| 142 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 143 | } |
| 144 | |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 145 | int pid = -1; |
| 146 | FileDescriptor childPipeFd = null; |
| 147 | FileDescriptor serverPipeFd = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | |
Chris Wailes | 2be2626 | 2019-01-11 16:14:43 -0800 | [diff] [blame] | 149 | parsedArgs = new ZygoteArguments(args); |
Narayan Kamath | c41638c | 2014-04-07 13:56:15 +0100 | [diff] [blame] | 150 | |
Chris Wailes | 2be2626 | 2019-01-11 16:14:43 -0800 | [diff] [blame] | 151 | if (parsedArgs.mAbiListQuery) { |
Narayan Kamath | bf99d06 | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 152 | handleAbiListQuery(); |
| 153 | return null; |
| 154 | } |
Narayan Kamath | c41638c | 2014-04-07 13:56:15 +0100 | [diff] [blame] | 155 | |
Chris Wailes | 2be2626 | 2019-01-11 16:14:43 -0800 | [diff] [blame] | 156 | if (parsedArgs.mPidQuery) { |
Andreas Gampe | 8444dca | 2018-05-01 13:31:28 -0700 | [diff] [blame] | 157 | handlePidQuery(); |
| 158 | return null; |
| 159 | } |
| 160 | |
Chris Wailes | db132a3 | 2019-02-20 10:49:27 -0800 | [diff] [blame] | 161 | if (parsedArgs.mBlastulaPoolStatusSpecified) { |
| 162 | return handleBlastulaPoolStatusChange(zygoteServer, parsedArgs.mBlastulaPoolEnabled); |
| 163 | } |
| 164 | |
Chris Wailes | 2be2626 | 2019-01-11 16:14:43 -0800 | [diff] [blame] | 165 | if (parsedArgs.mPreloadDefault) { |
Narayan Kamath | bf99d06 | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 166 | handlePreload(); |
| 167 | return null; |
| 168 | } |
Narayan Kamath | 5a3d0c6 | 2016-11-09 15:35:18 +0000 | [diff] [blame] | 169 | |
Chris Wailes | 2be2626 | 2019-01-11 16:14:43 -0800 | [diff] [blame] | 170 | if (parsedArgs.mPreloadPackage != null) { |
| 171 | handlePreloadPackage(parsedArgs.mPreloadPackage, parsedArgs.mPreloadPackageLibs, |
| 172 | parsedArgs.mPreloadPackageLibFileName, parsedArgs.mPreloadPackageCacheKey); |
Narayan Kamath | bf99d06 | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 173 | return null; |
| 174 | } |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 175 | |
Martijn Coenen | 7e6fa67 | 2018-11-05 11:45:26 +0100 | [diff] [blame] | 176 | if (canPreloadApp() && parsedArgs.mPreloadApp != null) { |
| 177 | byte[] rawParcelData = Base64.getDecoder().decode(parsedArgs.mPreloadApp); |
| 178 | Parcel appInfoParcel = Parcel.obtain(); |
| 179 | appInfoParcel.unmarshall(rawParcelData, 0, rawParcelData.length); |
| 180 | appInfoParcel.setDataPosition(0); |
| 181 | ApplicationInfo appInfo = ApplicationInfo.CREATOR.createFromParcel(appInfoParcel); |
| 182 | appInfoParcel.recycle(); |
| 183 | if (appInfo != null) { |
| 184 | handlePreloadApp(appInfo); |
| 185 | } else { |
| 186 | throw new IllegalArgumentException("Failed to deserialize --preload-app"); |
| 187 | } |
| 188 | return null; |
| 189 | } |
| 190 | |
Chris Wailes | 2be2626 | 2019-01-11 16:14:43 -0800 | [diff] [blame] | 191 | if (parsedArgs.mApiBlacklistExemptions != null) { |
Chris Wailes | 01060e6 | 2019-02-21 13:46:52 -0800 | [diff] [blame] | 192 | return handleApiBlacklistExemptions(zygoteServer, parsedArgs.mApiBlacklistExemptions); |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Chris Wailes | 2be2626 | 2019-01-11 16:14:43 -0800 | [diff] [blame] | 195 | if (parsedArgs.mHiddenApiAccessLogSampleRate != -1) { |
Chris Wailes | 01060e6 | 2019-02-21 13:46:52 -0800 | [diff] [blame] | 196 | return handleHiddenApiAccessLogSampleRate(zygoteServer, |
| 197 | parsedArgs.mHiddenApiAccessLogSampleRate); |
Mathew Inwood | 04194fe | 2018-04-04 14:48:03 +0100 | [diff] [blame] | 198 | } |
| 199 | |
Chris Wailes | 2be2626 | 2019-01-11 16:14:43 -0800 | [diff] [blame] | 200 | if (parsedArgs.mPermittedCapabilities != 0 || parsedArgs.mEffectiveCapabilities != 0) { |
| 201 | throw new ZygoteSecurityException("Client may not specify capabilities: " |
| 202 | + "permitted=0x" + Long.toHexString(parsedArgs.mPermittedCapabilities) |
| 203 | + ", effective=0x" + Long.toHexString(parsedArgs.mEffectiveCapabilities)); |
Narayan Kamath | bf99d06 | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 204 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 205 | |
Chris Wailes | 2be2626 | 2019-01-11 16:14:43 -0800 | [diff] [blame] | 206 | Zygote.applyUidSecurityPolicy(parsedArgs, peer); |
| 207 | Zygote.applyInvokeWithSecurityPolicy(parsedArgs, peer); |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 208 | |
Chris Wailes | 2be2626 | 2019-01-11 16:14:43 -0800 | [diff] [blame] | 209 | Zygote.applyDebuggerSystemProperty(parsedArgs); |
| 210 | Zygote.applyInvokeWithSystemProperty(parsedArgs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 211 | |
Narayan Kamath | bf99d06 | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 212 | int[][] rlimits = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 213 | |
Chris Wailes | 2be2626 | 2019-01-11 16:14:43 -0800 | [diff] [blame] | 214 | if (parsedArgs.mRLimits != null) { |
| 215 | rlimits = parsedArgs.mRLimits.toArray(Zygote.INT_ARRAY_2D); |
Narayan Kamath | bf99d06 | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 216 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 217 | |
Narayan Kamath | bf99d06 | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 218 | int[] fdsToIgnore = null; |
Andreas Gampe | 8dfa178 | 2017-01-05 12:45:58 -0800 | [diff] [blame] | 219 | |
Chris Wailes | 2be2626 | 2019-01-11 16:14:43 -0800 | [diff] [blame] | 220 | if (parsedArgs.mInvokeWith != null) { |
Narayan Kamath | bf99d06 | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 221 | try { |
Elliott Hughes | 3fe5951 | 2014-12-12 14:07:34 -0800 | [diff] [blame] | 222 | FileDescriptor[] pipeFds = Os.pipe2(O_CLOEXEC); |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 223 | childPipeFd = pipeFds[1]; |
| 224 | serverPipeFd = pipeFds[0]; |
Narayan Kamath | 6ac7e67 | 2015-01-16 16:26:54 +0000 | [diff] [blame] | 225 | Os.fcntlInt(childPipeFd, F_SETFD, 0); |
Narayan Kamath | bf99d06 | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 226 | fdsToIgnore = new int[]{childPipeFd.getInt$(), serverPipeFd.getInt$()}; |
| 227 | } catch (ErrnoException errnoEx) { |
| 228 | throw new IllegalStateException("Unable to set up pipe for invoke-with", errnoEx); |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 229 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 230 | } |
| 231 | |
Narayan Kamath | bf99d06 | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 232 | /** |
| 233 | * In order to avoid leaking descriptors to the Zygote child, |
| 234 | * the native code must close the two Zygote socket descriptors |
| 235 | * in the child process before it switches from Zygote-root to |
| 236 | * the UID and privileges of the application being launched. |
| 237 | * |
| 238 | * In order to avoid "bad file descriptor" errors when the |
| 239 | * two LocalSocket objects are closed, the Posix file |
| 240 | * descriptors are released via a dup2() call which closes |
| 241 | * the socket and substitutes an open descriptor to /dev/null. |
| 242 | */ |
| 243 | |
| 244 | int [] fdsToClose = { -1, -1 }; |
| 245 | |
| 246 | FileDescriptor fd = mSocket.getFileDescriptor(); |
| 247 | |
| 248 | if (fd != null) { |
| 249 | fdsToClose[0] = fd.getInt$(); |
| 250 | } |
| 251 | |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 252 | fd = zygoteServer.getZygoteSocketFileDescriptor(); |
Narayan Kamath | bf99d06 | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 253 | |
| 254 | if (fd != null) { |
| 255 | fdsToClose[1] = fd.getInt$(); |
| 256 | } |
| 257 | |
| 258 | fd = null; |
| 259 | |
Chris Wailes | 2be2626 | 2019-01-11 16:14:43 -0800 | [diff] [blame] | 260 | pid = Zygote.forkAndSpecialize(parsedArgs.mUid, parsedArgs.mGid, parsedArgs.mGids, |
| 261 | parsedArgs.mRuntimeFlags, rlimits, parsedArgs.mMountExternal, parsedArgs.mSeInfo, |
| 262 | parsedArgs.mNiceName, fdsToClose, fdsToIgnore, parsedArgs.mStartChildZygote, |
| 263 | parsedArgs.mInstructionSet, parsedArgs.mAppDataDir, parsedArgs.mPackageName, |
Sudheer Shanka | e51005d | 2019-02-24 10:24:09 -0800 | [diff] [blame] | 264 | parsedArgs.mPackagesForUid, parsedArgs.mSandboxId); |
Narayan Kamath | bf99d06 | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 265 | |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 266 | try { |
| 267 | if (pid == 0) { |
| 268 | // in child |
Narayan Kamath | bf99d06 | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 269 | zygoteServer.setForkChild(); |
| 270 | |
Simon Baldwin | ba816e0 | 2016-01-19 16:34:54 +0000 | [diff] [blame] | 271 | zygoteServer.closeServerSocket(); |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 272 | IoUtils.closeQuietly(serverPipeFd); |
| 273 | serverPipeFd = null; |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 274 | |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 275 | return handleChildProc(parsedArgs, descriptors, childPipeFd, |
Chris Wailes | 2be2626 | 2019-01-11 16:14:43 -0800 | [diff] [blame] | 276 | parsedArgs.mStartChildZygote); |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 277 | } else { |
Narayan Kamath | bf99d06 | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 278 | // In the parent. A pid < 0 indicates a failure and will be handled in |
| 279 | // handleParentProc. |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 280 | IoUtils.closeQuietly(childPipeFd); |
| 281 | childPipeFd = null; |
Narayan Kamath | bf99d06 | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 282 | handleParentProc(pid, descriptors, serverPipeFd); |
| 283 | return null; |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 284 | } |
| 285 | } finally { |
| 286 | IoUtils.closeQuietly(childPipeFd); |
| 287 | IoUtils.closeQuietly(serverPipeFd); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | |
Narayan Kamath | bf99d06 | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 291 | private void handleAbiListQuery() { |
Narayan Kamath | c41638c | 2014-04-07 13:56:15 +0100 | [diff] [blame] | 292 | try { |
| 293 | final byte[] abiListBytes = abiList.getBytes(StandardCharsets.US_ASCII); |
| 294 | mSocketOutStream.writeInt(abiListBytes.length); |
| 295 | mSocketOutStream.write(abiListBytes); |
Narayan Kamath | c41638c | 2014-04-07 13:56:15 +0100 | [diff] [blame] | 296 | } catch (IOException ioe) { |
Narayan Kamath | bf99d06 | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 297 | throw new IllegalStateException("Error writing to command socket", ioe); |
Narayan Kamath | c41638c | 2014-04-07 13:56:15 +0100 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | |
Andreas Gampe | 8444dca | 2018-05-01 13:31:28 -0700 | [diff] [blame] | 301 | private void handlePidQuery() { |
| 302 | try { |
| 303 | String pidString = String.valueOf(Process.myPid()); |
| 304 | final byte[] pidStringBytes = pidString.getBytes(StandardCharsets.US_ASCII); |
| 305 | mSocketOutStream.writeInt(pidStringBytes.length); |
| 306 | mSocketOutStream.write(pidStringBytes); |
| 307 | } catch (IOException ioe) { |
| 308 | throw new IllegalStateException("Error writing to command socket", ioe); |
| 309 | } |
| 310 | } |
| 311 | |
Narayan Kamath | 669afcc | 2017-02-06 20:24:08 +0000 | [diff] [blame] | 312 | /** |
| 313 | * Preloads resources if the zygote is in lazily preload mode. Writes the result of the |
| 314 | * preload operation; {@code 0} when a preload was initiated due to this request and {@code 1} |
| 315 | * if no preload was initiated. The latter implies that the zygote is not configured to load |
| 316 | * resources lazy or that the zygote has already handled a previous request to handlePreload. |
| 317 | */ |
Narayan Kamath | bf99d06 | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 318 | private void handlePreload() { |
Narayan Kamath | 669afcc | 2017-02-06 20:24:08 +0000 | [diff] [blame] | 319 | try { |
| 320 | if (isPreloadComplete()) { |
| 321 | mSocketOutStream.writeInt(1); |
| 322 | } else { |
| 323 | preload(); |
| 324 | mSocketOutStream.writeInt(0); |
| 325 | } |
Narayan Kamath | 669afcc | 2017-02-06 20:24:08 +0000 | [diff] [blame] | 326 | } catch (IOException ioe) { |
Narayan Kamath | bf99d06 | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 327 | throw new IllegalStateException("Error writing to command socket", ioe); |
Narayan Kamath | 669afcc | 2017-02-06 20:24:08 +0000 | [diff] [blame] | 328 | } |
| 329 | } |
| 330 | |
Chris Wailes | 01060e6 | 2019-02-21 13:46:52 -0800 | [diff] [blame] | 331 | private Runnable stateChangeWithBlastulaPoolReset(ZygoteServer zygoteServer, |
| 332 | Runnable stateChangeCode) { |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 333 | try { |
Chris Wailes | 01060e6 | 2019-02-21 13:46:52 -0800 | [diff] [blame] | 334 | if (zygoteServer.isBlastulaPoolEnabled()) { |
| 335 | Zygote.emptyBlastulaPool(); |
| 336 | } |
| 337 | |
| 338 | stateChangeCode.run(); |
| 339 | |
| 340 | if (zygoteServer.isBlastulaPoolEnabled()) { |
| 341 | Runnable fpResult = |
| 342 | zygoteServer.fillBlastulaPool( |
| 343 | new int[]{mSocket.getFileDescriptor().getInt$()}); |
| 344 | |
| 345 | if (fpResult != null) { |
| 346 | zygoteServer.setForkChild(); |
| 347 | return fpResult; |
| 348 | } |
| 349 | } |
| 350 | |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 351 | mSocketOutStream.writeInt(0); |
Chris Wailes | 01060e6 | 2019-02-21 13:46:52 -0800 | [diff] [blame] | 352 | |
| 353 | return null; |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 354 | } catch (IOException ioe) { |
| 355 | throw new IllegalStateException("Error writing to command socket", ioe); |
| 356 | } |
| 357 | } |
| 358 | |
Chris Wailes | 01060e6 | 2019-02-21 13:46:52 -0800 | [diff] [blame] | 359 | /** |
| 360 | * Makes the necessary changes to implement a new API blacklist exemption policy, and then |
| 361 | * responds to the system server, letting it know that the task has been completed. |
| 362 | * |
| 363 | * This necessitates a change to the internal state of the Zygote. As such, if the blastula |
| 364 | * pool is enabled all existing blastulas have an incorrect API blacklist exemption list. To |
| 365 | * properly handle this request the pool must be emptied and refilled. This process can return |
| 366 | * a Runnable object that must be returned to ZygoteServer.runSelectLoop to be invoked. |
| 367 | * |
| 368 | * @param zygoteServer The server object that received the request |
| 369 | * @param exemptions The new exemption list. |
| 370 | * @return A Runnable object representing a new app in any blastulas spawned from here; the |
| 371 | * zygote process will always receive a null value from this function. |
| 372 | */ |
| 373 | private Runnable handleApiBlacklistExemptions(ZygoteServer zygoteServer, String[] exemptions) { |
| 374 | return stateChangeWithBlastulaPoolReset(zygoteServer, |
| 375 | () -> ZygoteInit.setApiBlacklistExemptions(exemptions)); |
| 376 | } |
| 377 | |
Chris Wailes | db132a3 | 2019-02-20 10:49:27 -0800 | [diff] [blame] | 378 | private Runnable handleBlastulaPoolStatusChange(ZygoteServer zygoteServer, boolean newStatus) { |
| 379 | try { |
| 380 | Runnable fpResult = zygoteServer.setBlastulaPoolStatus(newStatus, mSocket); |
| 381 | |
| 382 | if (fpResult == null) { |
| 383 | mSocketOutStream.writeInt(0); |
| 384 | } else { |
| 385 | zygoteServer.setForkChild(); |
| 386 | } |
| 387 | |
| 388 | return fpResult; |
Narayan Kamath | 669afcc | 2017-02-06 20:24:08 +0000 | [diff] [blame] | 389 | } catch (IOException ioe) { |
| 390 | throw new IllegalStateException("Error writing to command socket", ioe); |
| 391 | } |
| 392 | } |
| 393 | |
Andrei Onea | 07aab08 | 2019-02-06 14:44:22 +0000 | [diff] [blame] | 394 | private static class HiddenApiUsageLogger implements VMRuntime.HiddenApiUsageLogger { |
Andrei Onea | 6cd1b70 | 2019-01-25 16:29:44 +0000 | [diff] [blame] | 395 | |
| 396 | private final MetricsLogger mMetricsLogger = new MetricsLogger(); |
Andrei Onea | 07aab08 | 2019-02-06 14:44:22 +0000 | [diff] [blame] | 397 | private static HiddenApiUsageLogger sInstance = new HiddenApiUsageLogger(); |
Andrei Onea | 31bde16 | 2019-02-15 13:50:47 +0000 | [diff] [blame] | 398 | private int mHiddenApiAccessLogSampleRate = 0; |
| 399 | |
| 400 | public static void setHiddenApiAccessLogSampleRate(int sampleRate) { |
| 401 | sInstance.mHiddenApiAccessLogSampleRate = sampleRate; |
| 402 | } |
Andrei Onea | 07aab08 | 2019-02-06 14:44:22 +0000 | [diff] [blame] | 403 | |
| 404 | public static HiddenApiUsageLogger getInstance() { |
| 405 | return HiddenApiUsageLogger.sInstance; |
| 406 | } |
Andrei Onea | 6cd1b70 | 2019-01-25 16:29:44 +0000 | [diff] [blame] | 407 | |
Andrei Onea | 31bde16 | 2019-02-15 13:50:47 +0000 | [diff] [blame] | 408 | public void hiddenApiUsed(int sampledValue, String packageName, String signature, |
Andrei Onea | 6cd1b70 | 2019-01-25 16:29:44 +0000 | [diff] [blame] | 409 | int accessMethod, boolean accessDenied) { |
Andrei Onea | 31bde16 | 2019-02-15 13:50:47 +0000 | [diff] [blame] | 410 | if (sampledValue < mHiddenApiAccessLogSampleRate) { |
| 411 | logUsage(packageName, signature, accessMethod, accessDenied); |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | private void logUsage(String packageName, String signature, int accessMethod, |
| 416 | boolean accessDenied) { |
Andrei Onea | 6cd1b70 | 2019-01-25 16:29:44 +0000 | [diff] [blame] | 417 | int accessMethodMetric = HiddenApiUsageLogger.ACCESS_METHOD_NONE; |
| 418 | switch(accessMethod) { |
| 419 | case HiddenApiUsageLogger.ACCESS_METHOD_NONE: |
| 420 | accessMethodMetric = MetricsEvent.ACCESS_METHOD_NONE; |
| 421 | break; |
| 422 | case HiddenApiUsageLogger.ACCESS_METHOD_REFLECTION: |
| 423 | accessMethodMetric = MetricsEvent.ACCESS_METHOD_REFLECTION; |
| 424 | break; |
| 425 | case HiddenApiUsageLogger.ACCESS_METHOD_JNI: |
| 426 | accessMethodMetric = MetricsEvent.ACCESS_METHOD_JNI; |
| 427 | break; |
| 428 | case HiddenApiUsageLogger.ACCESS_METHOD_LINKING: |
| 429 | accessMethodMetric = MetricsEvent.ACCESS_METHOD_LINKING; |
| 430 | break; |
| 431 | } |
| 432 | LogMaker logMaker = new LogMaker(MetricsEvent.ACTION_HIDDEN_API_ACCESSED) |
| 433 | .setPackageName(packageName) |
| 434 | .addTaggedData(MetricsEvent.FIELD_HIDDEN_API_SIGNATURE, signature) |
| 435 | .addTaggedData(MetricsEvent.FIELD_HIDDEN_API_ACCESS_METHOD, |
| 436 | accessMethodMetric); |
| 437 | if (accessDenied) { |
| 438 | logMaker.addTaggedData(MetricsEvent.FIELD_HIDDEN_API_ACCESS_DENIED, 1); |
| 439 | } |
| 440 | mMetricsLogger.write(logMaker); |
| 441 | } |
| 442 | } |
| 443 | |
Chris Wailes | 01060e6 | 2019-02-21 13:46:52 -0800 | [diff] [blame] | 444 | /** |
| 445 | * Changes the API access log sample rate for the Zygote and processes spawned from it. |
| 446 | * |
| 447 | * This necessitates a change to the internal state of the Zygote. As such, if the blastula |
| 448 | * pool is enabled all existing blastulas have an incorrect API access log sample rate. To |
| 449 | * properly handle this request the pool must be emptied and refilled. This process can return |
| 450 | * a Runnable object that must be returned to ZygoteServer.runSelectLoop to be invoked. |
| 451 | * |
| 452 | * @param zygoteServer The server object that received the request |
| 453 | * @param samplingRate The new sample rate |
| 454 | * @return A Runnable object representing a new app in any blastulas spawned from here; the |
| 455 | * zygote process will always receive a null value from this function. |
| 456 | */ |
| 457 | private Runnable handleHiddenApiAccessLogSampleRate(ZygoteServer zygoteServer, |
| 458 | int samplingRate) { |
| 459 | return stateChangeWithBlastulaPoolReset(zygoteServer, () -> { |
Andrei Onea | 6cd1b70 | 2019-01-25 16:29:44 +0000 | [diff] [blame] | 460 | ZygoteInit.setHiddenApiAccessLogSampleRate(samplingRate); |
Andrei Onea | 31bde16 | 2019-02-15 13:50:47 +0000 | [diff] [blame] | 461 | HiddenApiUsageLogger.setHiddenApiAccessLogSampleRate(samplingRate); |
Andrei Onea | 07aab08 | 2019-02-06 14:44:22 +0000 | [diff] [blame] | 462 | ZygoteInit.setHiddenApiUsageLogger(HiddenApiUsageLogger.getInstance()); |
Chris Wailes | 01060e6 | 2019-02-21 13:46:52 -0800 | [diff] [blame] | 463 | }); |
Mathew Inwood | 04194fe | 2018-04-04 14:48:03 +0100 | [diff] [blame] | 464 | } |
| 465 | |
Narayan Kamath | 669afcc | 2017-02-06 20:24:08 +0000 | [diff] [blame] | 466 | protected void preload() { |
| 467 | ZygoteInit.lazyPreload(); |
| 468 | } |
| 469 | |
| 470 | protected boolean isPreloadComplete() { |
| 471 | return ZygoteInit.isPreloadComplete(); |
Torne (Richard Coles) | bb65893 | 2017-01-05 16:11:06 +0000 | [diff] [blame] | 472 | } |
| 473 | |
Narayan Kamath | 24a3306 | 2017-07-03 14:12:26 +0100 | [diff] [blame] | 474 | protected DataOutputStream getSocketOutputStream() { |
| 475 | return mSocketOutStream; |
| 476 | } |
| 477 | |
Torne (Richard Coles) | f4f647e | 2018-02-21 16:12:36 -0500 | [diff] [blame] | 478 | protected void handlePreloadPackage(String packagePath, String libsPath, String libFileName, |
| 479 | String cacheKey) { |
Martijn Coenen | 7e6fa67 | 2018-11-05 11:45:26 +0100 | [diff] [blame] | 480 | throw new RuntimeException("Zygote does not support package preloading"); |
| 481 | } |
| 482 | |
| 483 | protected boolean canPreloadApp() { |
| 484 | return false; |
| 485 | } |
| 486 | |
| 487 | protected void handlePreloadApp(ApplicationInfo aInfo) { |
| 488 | throw new RuntimeException("Zygote does not support app preloading"); |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 489 | } |
| 490 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 491 | /** |
| 492 | * Closes socket associated with this connection. |
| 493 | */ |
| 494 | void closeSocket() { |
| 495 | try { |
| 496 | mSocket.close(); |
| 497 | } catch (IOException ex) { |
| 498 | Log.e(TAG, "Exception while closing command " |
| 499 | + "socket in parent", ex); |
| 500 | } |
| 501 | } |
| 502 | |
Narayan Kamath | bf99d06 | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 503 | boolean isClosedByPeer() { |
| 504 | return isEof; |
| 505 | } |
| 506 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 507 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 508 | * Handles post-fork setup of child proc, closing sockets as appropriate, |
| 509 | * reopen stdio as appropriate, and ultimately throwing MethodAndArgsCaller |
| 510 | * if successful or returning if failed. |
| 511 | * |
| 512 | * @param parsedArgs non-null; zygote args |
| 513 | * @param descriptors null-ok; new file descriptors for stdio if available. |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 514 | * @param pipeFd null-ok; pipe for communication back to Zygote. |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 515 | * @param isZygote whether this new child process is itself a new Zygote. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 516 | */ |
Chris Wailes | 2be2626 | 2019-01-11 16:14:43 -0800 | [diff] [blame] | 517 | private Runnable handleChildProc(ZygoteArguments parsedArgs, FileDescriptor[] descriptors, |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 518 | FileDescriptor pipeFd, boolean isZygote) { |
Dave Platt | 89d4c89 | 2014-02-05 17:06:42 -0800 | [diff] [blame] | 519 | /** |
| 520 | * By the time we get here, the native code has closed the two actual Zygote |
| 521 | * socket connections, and substituted /dev/null in their place. The LocalSocket |
| 522 | * objects still need to be closed properly. |
| 523 | */ |
| 524 | |
Nick Kralevich | 468f6c1 | 2013-01-30 08:45:03 -0800 | [diff] [blame] | 525 | closeSocket(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 526 | if (descriptors != null) { |
| 527 | try { |
Elliott Hughes | dac83f5 | 2014-12-15 11:00:25 -0800 | [diff] [blame] | 528 | Os.dup2(descriptors[0], STDIN_FILENO); |
| 529 | Os.dup2(descriptors[1], STDOUT_FILENO); |
| 530 | Os.dup2(descriptors[2], STDERR_FILENO); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 531 | |
| 532 | for (FileDescriptor fd: descriptors) { |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 533 | IoUtils.closeQuietly(fd); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 534 | } |
Elliott Hughes | dac83f5 | 2014-12-15 11:00:25 -0800 | [diff] [blame] | 535 | } catch (ErrnoException ex) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 536 | Log.e(TAG, "Error reopening stdio", ex); |
| 537 | } |
| 538 | } |
| 539 | |
Chris Wailes | 2be2626 | 2019-01-11 16:14:43 -0800 | [diff] [blame] | 540 | if (parsedArgs.mNiceName != null) { |
| 541 | Process.setArgV0(parsedArgs.mNiceName); |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 542 | } |
| 543 | |
Narayan Kamath | fbb32f6 | 2015-06-12 15:34:35 +0100 | [diff] [blame] | 544 | // End of the postFork event. |
| 545 | Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER); |
Chris Wailes | 2be2626 | 2019-01-11 16:14:43 -0800 | [diff] [blame] | 546 | if (parsedArgs.mInvokeWith != null) { |
| 547 | WrapperInit.execApplication(parsedArgs.mInvokeWith, |
| 548 | parsedArgs.mNiceName, parsedArgs.mTargetSdkVersion, |
Narayan Kamath | 37ad4b0 | 2015-01-19 16:05:24 +0000 | [diff] [blame] | 549 | VMRuntime.getCurrentInstructionSet(), |
Chris Wailes | 2be2626 | 2019-01-11 16:14:43 -0800 | [diff] [blame] | 550 | pipeFd, parsedArgs.mRemainingArgs); |
Narayan Kamath | bf99d06 | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 551 | |
| 552 | // Should not get here. |
| 553 | throw new IllegalStateException("WrapperInit.execApplication unexpectedly returned"); |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 554 | } else { |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 555 | if (!isZygote) { |
Chris Wailes | 2be2626 | 2019-01-11 16:14:43 -0800 | [diff] [blame] | 556 | return ZygoteInit.zygoteInit(parsedArgs.mTargetSdkVersion, |
| 557 | parsedArgs.mRemainingArgs, null /* classLoader */); |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 558 | } else { |
Chris Wailes | 2be2626 | 2019-01-11 16:14:43 -0800 | [diff] [blame] | 559 | return ZygoteInit.childZygoteInit(parsedArgs.mTargetSdkVersion, |
| 560 | parsedArgs.mRemainingArgs, null /* classLoader */); |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 561 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 562 | } |
| 563 | } |
| 564 | |
| 565 | /** |
| 566 | * Handles post-fork cleanup of parent proc |
| 567 | * |
| 568 | * @param pid != 0; pid of child if > 0 or indication of failed fork |
| 569 | * if < 0; |
| 570 | * @param descriptors null-ok; file descriptors for child's new stdio if |
| 571 | * specified. |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 572 | * @param pipeFd null-ok; pipe for communication with child. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 573 | */ |
Narayan Kamath | bf99d06 | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 574 | private void handleParentProc(int pid, FileDescriptor[] descriptors, FileDescriptor pipeFd) { |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 575 | if (pid > 0) { |
| 576 | setChildPgid(pid); |
| 577 | } |
| 578 | |
| 579 | if (descriptors != null) { |
| 580 | for (FileDescriptor fd: descriptors) { |
| 581 | IoUtils.closeQuietly(fd); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 582 | } |
| 583 | } |
| 584 | |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 585 | boolean usingWrapper = false; |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 586 | if (pipeFd != null && pid > 0) { |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 587 | int innerPid = -1; |
| 588 | try { |
Andreas Gampe | cbc4214 | 2017-07-12 19:17:52 -0700 | [diff] [blame] | 589 | // Do a busy loop here. We can't guarantee that a failure (and thus an exception |
| 590 | // bail) happens in a timely manner. |
Andreas Gampe | cbc4214 | 2017-07-12 19:17:52 -0700 | [diff] [blame] | 591 | final int BYTES_REQUIRED = 4; // Bytes in an int. |
| 592 | |
| 593 | StructPollfd fds[] = new StructPollfd[] { |
| 594 | new StructPollfd() |
| 595 | }; |
| 596 | |
| 597 | byte data[] = new byte[BYTES_REQUIRED]; |
| 598 | |
Andreas Gampe | 27497c6 | 2017-07-21 11:41:00 -0700 | [diff] [blame] | 599 | int remainingSleepTime = WRAPPED_PID_TIMEOUT_MILLIS; |
Andreas Gampe | cbc4214 | 2017-07-12 19:17:52 -0700 | [diff] [blame] | 600 | int dataIndex = 0; |
| 601 | long startTime = System.nanoTime(); |
| 602 | |
| 603 | while (dataIndex < data.length && remainingSleepTime > 0) { |
| 604 | fds[0].fd = pipeFd; |
| 605 | fds[0].events = (short) POLLIN; |
| 606 | fds[0].revents = 0; |
| 607 | fds[0].userData = null; |
| 608 | |
| 609 | int res = android.system.Os.poll(fds, remainingSleepTime); |
| 610 | long endTime = System.nanoTime(); |
Andreas Gampe | 27497c6 | 2017-07-21 11:41:00 -0700 | [diff] [blame] | 611 | int elapsedTimeMs = (int)((endTime - startTime) / 1000000l); |
| 612 | remainingSleepTime = WRAPPED_PID_TIMEOUT_MILLIS - elapsedTimeMs; |
Andreas Gampe | cbc4214 | 2017-07-12 19:17:52 -0700 | [diff] [blame] | 613 | |
| 614 | if (res > 0) { |
| 615 | if ((fds[0].revents & POLLIN) != 0) { |
| 616 | // Only read one byte, so as not to block. |
| 617 | int readBytes = android.system.Os.read(pipeFd, data, dataIndex, 1); |
| 618 | if (readBytes < 0) { |
| 619 | throw new RuntimeException("Some error"); |
| 620 | } |
| 621 | dataIndex += readBytes; |
| 622 | } else { |
| 623 | // Error case. revents should contain one of the error bits. |
| 624 | break; |
| 625 | } |
| 626 | } else if (res == 0) { |
| 627 | Log.w(TAG, "Timed out waiting for child."); |
| 628 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 629 | } |
Andreas Gampe | cbc4214 | 2017-07-12 19:17:52 -0700 | [diff] [blame] | 630 | |
| 631 | if (dataIndex == data.length) { |
| 632 | DataInputStream is = new DataInputStream(new ByteArrayInputStream(data)); |
| 633 | innerPid = is.readInt(); |
| 634 | } |
| 635 | |
| 636 | if (innerPid == -1) { |
| 637 | Log.w(TAG, "Error reading pid from wrapped process, child may have died"); |
| 638 | } |
| 639 | } catch (Exception ex) { |
| 640 | Log.w(TAG, "Error reading pid from wrapped process, child may have died", ex); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 641 | } |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 642 | |
| 643 | // Ensure that the pid reported by the wrapped process is either the |
| 644 | // child process that we forked, or a descendant of it. |
| 645 | if (innerPid > 0) { |
| 646 | int parentPid = innerPid; |
| 647 | while (parentPid > 0 && parentPid != pid) { |
| 648 | parentPid = Process.getParentPid(parentPid); |
| 649 | } |
| 650 | if (parentPid > 0) { |
| 651 | Log.i(TAG, "Wrapped process has pid " + innerPid); |
| 652 | pid = innerPid; |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 653 | usingWrapper = true; |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 654 | } else { |
| 655 | Log.w(TAG, "Wrapped process reported a pid that is not a child of " |
| 656 | + "the process that we forked: childPid=" + pid |
| 657 | + " innerPid=" + innerPid); |
| 658 | } |
| 659 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | try { |
| 663 | mSocketOutStream.writeInt(pid); |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 664 | mSocketOutStream.writeBoolean(usingWrapper); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 665 | } catch (IOException ex) { |
Narayan Kamath | bf99d06 | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 666 | throw new IllegalStateException("Error writing to command socket", ex); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 667 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 668 | } |
| 669 | |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 670 | private void setChildPgid(int pid) { |
| 671 | // Try to move the new child into the peer's process group. |
| 672 | try { |
Elliott Hughes | 26b56e6 | 2014-12-17 12:28:29 -0800 | [diff] [blame] | 673 | Os.setpgid(pid, Os.getpgid(peer.getPid())); |
| 674 | } catch (ErrnoException ex) { |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 675 | // This exception is expected in the case where |
| 676 | // the peer is not in our session |
| 677 | // TODO get rid of this log message in the case where |
| 678 | // getsid(0) != getsid(peer.getPid()) |
| 679 | Log.i(TAG, "Zygote: setpgid failed. This is " |
| 680 | + "normal if peer is not in our session"); |
| 681 | } |
| 682 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 683 | } |