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