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