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