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 | 1f26331 | 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 | 1f26331 | 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 | 1f26331 | 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 | 1f26331 | 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 | 1f26331 | 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 | 1f26331 | 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 | 1f26331 | 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, |
Riddle Hsu | 32dbdca | 2019-05-17 23:10:16 -0600 | [diff] [blame] | 260 | parsedArgs.mInstructionSet, parsedArgs.mAppDataDir, parsedArgs.mTargetSdkVersion, |
Ricky Wai | 5a8fe7a | 2019-12-13 15:20:47 +0000 | [diff] [blame] | 261 | parsedArgs.mIsTopApp, parsedArgs.mPkgDataInfoList); |
Narayan Kamath | bf99d06 | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 262 | |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 263 | try { |
| 264 | if (pid == 0) { |
| 265 | // in child |
Narayan Kamath | bf99d06 | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 266 | zygoteServer.setForkChild(); |
| 267 | |
Simon Baldwin | ba816e0 | 2016-01-19 16:34:54 +0000 | [diff] [blame] | 268 | zygoteServer.closeServerSocket(); |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 269 | IoUtils.closeQuietly(serverPipeFd); |
| 270 | serverPipeFd = null; |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 271 | |
Chris Wailes | ca848c4 | 2019-04-26 14:36:34 -0700 | [diff] [blame] | 272 | return handleChildProc(parsedArgs, childPipeFd, parsedArgs.mStartChildZygote); |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 273 | } else { |
Narayan Kamath | bf99d06 | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 274 | // In the parent. A pid < 0 indicates a failure and will be handled in |
| 275 | // handleParentProc. |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 276 | IoUtils.closeQuietly(childPipeFd); |
| 277 | childPipeFd = null; |
Chris Wailes | ca848c4 | 2019-04-26 14:36:34 -0700 | [diff] [blame] | 278 | handleParentProc(pid, serverPipeFd); |
Narayan Kamath | bf99d06 | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 279 | return null; |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 280 | } |
| 281 | } finally { |
| 282 | IoUtils.closeQuietly(childPipeFd); |
| 283 | IoUtils.closeQuietly(serverPipeFd); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | |
Narayan Kamath | bf99d06 | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 287 | private void handleAbiListQuery() { |
Narayan Kamath | c41638c | 2014-04-07 13:56:15 +0100 | [diff] [blame] | 288 | try { |
| 289 | final byte[] abiListBytes = abiList.getBytes(StandardCharsets.US_ASCII); |
| 290 | mSocketOutStream.writeInt(abiListBytes.length); |
| 291 | mSocketOutStream.write(abiListBytes); |
Narayan Kamath | c41638c | 2014-04-07 13:56:15 +0100 | [diff] [blame] | 292 | } catch (IOException ioe) { |
Narayan Kamath | bf99d06 | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 293 | throw new IllegalStateException("Error writing to command socket", ioe); |
Narayan Kamath | c41638c | 2014-04-07 13:56:15 +0100 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | |
Andreas Gampe | 8444dca | 2018-05-01 13:31:28 -0700 | [diff] [blame] | 297 | private void handlePidQuery() { |
| 298 | try { |
| 299 | String pidString = String.valueOf(Process.myPid()); |
| 300 | final byte[] pidStringBytes = pidString.getBytes(StandardCharsets.US_ASCII); |
| 301 | mSocketOutStream.writeInt(pidStringBytes.length); |
| 302 | mSocketOutStream.write(pidStringBytes); |
| 303 | } catch (IOException ioe) { |
| 304 | throw new IllegalStateException("Error writing to command socket", ioe); |
| 305 | } |
| 306 | } |
| 307 | |
David Srbecky | 6d0d706 | 2019-08-05 15:43:34 +0100 | [diff] [blame] | 308 | private void handleBootCompleted() { |
Josh Gao | 2736136 | 2019-10-02 16:01:51 -0700 | [diff] [blame] | 309 | try { |
| 310 | mSocketOutStream.writeInt(0); |
| 311 | } catch (IOException ioe) { |
| 312 | throw new IllegalStateException("Error writing to command socket", ioe); |
| 313 | } |
| 314 | |
David Srbecky | 6d0d706 | 2019-08-05 15:43:34 +0100 | [diff] [blame] | 315 | VMRuntime.bootCompleted(); |
| 316 | } |
| 317 | |
Narayan Kamath | 669afcc | 2017-02-06 20:24:08 +0000 | [diff] [blame] | 318 | /** |
| 319 | * Preloads resources if the zygote is in lazily preload mode. Writes the result of the |
| 320 | * preload operation; {@code 0} when a preload was initiated due to this request and {@code 1} |
| 321 | * if no preload was initiated. The latter implies that the zygote is not configured to load |
| 322 | * resources lazy or that the zygote has already handled a previous request to handlePreload. |
| 323 | */ |
Narayan Kamath | bf99d06 | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 324 | private void handlePreload() { |
Narayan Kamath | 669afcc | 2017-02-06 20:24:08 +0000 | [diff] [blame] | 325 | try { |
| 326 | if (isPreloadComplete()) { |
| 327 | mSocketOutStream.writeInt(1); |
| 328 | } else { |
| 329 | preload(); |
| 330 | mSocketOutStream.writeInt(0); |
| 331 | } |
Narayan Kamath | 669afcc | 2017-02-06 20:24:08 +0000 | [diff] [blame] | 332 | } catch (IOException ioe) { |
Narayan Kamath | bf99d06 | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 333 | throw new IllegalStateException("Error writing to command socket", ioe); |
Narayan Kamath | 669afcc | 2017-02-06 20:24:08 +0000 | [diff] [blame] | 334 | } |
| 335 | } |
| 336 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 337 | private Runnable stateChangeWithUsapPoolReset(ZygoteServer zygoteServer, |
Chris Wailes | 01060e6 | 2019-02-21 13:46:52 -0800 | [diff] [blame] | 338 | Runnable stateChangeCode) { |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 339 | try { |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 340 | if (zygoteServer.isUsapPoolEnabled()) { |
Chris Wailes | fb329ba | 2019-06-05 16:07:50 -0700 | [diff] [blame] | 341 | Log.i(TAG, "Emptying USAP Pool due to state change."); |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 342 | Zygote.emptyUsapPool(); |
Chris Wailes | 01060e6 | 2019-02-21 13:46:52 -0800 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | stateChangeCode.run(); |
| 346 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 347 | if (zygoteServer.isUsapPoolEnabled()) { |
Chris Wailes | 01060e6 | 2019-02-21 13:46:52 -0800 | [diff] [blame] | 348 | Runnable fpResult = |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 349 | zygoteServer.fillUsapPool( |
Chris Wailes | 3d74821 | 2019-05-09 17:11:00 -0700 | [diff] [blame] | 350 | new int[]{mSocket.getFileDescriptor().getInt$()}, false); |
Chris Wailes | 01060e6 | 2019-02-21 13:46:52 -0800 | [diff] [blame] | 351 | |
| 352 | if (fpResult != null) { |
| 353 | zygoteServer.setForkChild(); |
| 354 | return fpResult; |
Chris Wailes | fb329ba | 2019-06-05 16:07:50 -0700 | [diff] [blame] | 355 | } else { |
| 356 | Log.i(TAG, "Finished refilling USAP Pool after state change."); |
Chris Wailes | 01060e6 | 2019-02-21 13:46:52 -0800 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 360 | mSocketOutStream.writeInt(0); |
Chris Wailes | 01060e6 | 2019-02-21 13:46:52 -0800 | [diff] [blame] | 361 | |
| 362 | return null; |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 363 | } catch (IOException ioe) { |
| 364 | throw new IllegalStateException("Error writing to command socket", ioe); |
| 365 | } |
| 366 | } |
| 367 | |
Chris Wailes | 01060e6 | 2019-02-21 13:46:52 -0800 | [diff] [blame] | 368 | /** |
| 369 | * Makes the necessary changes to implement a new API blacklist exemption policy, and then |
| 370 | * responds to the system server, letting it know that the task has been completed. |
| 371 | * |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 372 | * This necessitates a change to the internal state of the Zygote. As such, if the USAP |
| 373 | * 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] | 374 | * properly handle this request the pool must be emptied and refilled. This process can return |
| 375 | * a Runnable object that must be returned to ZygoteServer.runSelectLoop to be invoked. |
| 376 | * |
| 377 | * @param zygoteServer The server object that received the request |
| 378 | * @param exemptions The new exemption list. |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 379 | * @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] | 380 | * zygote process will always receive a null value from this function. |
| 381 | */ |
| 382 | private Runnable handleApiBlacklistExemptions(ZygoteServer zygoteServer, String[] exemptions) { |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 383 | return stateChangeWithUsapPoolReset(zygoteServer, |
Chris Wailes | 01060e6 | 2019-02-21 13:46:52 -0800 | [diff] [blame] | 384 | () -> ZygoteInit.setApiBlacklistExemptions(exemptions)); |
| 385 | } |
| 386 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 387 | private Runnable handleUsapPoolStatusChange(ZygoteServer zygoteServer, boolean newStatus) { |
Chris Wailes | db132a3 | 2019-02-20 10:49:27 -0800 | [diff] [blame] | 388 | try { |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 389 | Runnable fpResult = zygoteServer.setUsapPoolStatus(newStatus, mSocket); |
Chris Wailes | db132a3 | 2019-02-20 10:49:27 -0800 | [diff] [blame] | 390 | |
| 391 | if (fpResult == null) { |
| 392 | mSocketOutStream.writeInt(0); |
| 393 | } else { |
| 394 | zygoteServer.setForkChild(); |
| 395 | } |
| 396 | |
| 397 | return fpResult; |
Narayan Kamath | 669afcc | 2017-02-06 20:24:08 +0000 | [diff] [blame] | 398 | } catch (IOException ioe) { |
| 399 | throw new IllegalStateException("Error writing to command socket", ioe); |
| 400 | } |
| 401 | } |
| 402 | |
Chris Wailes | 01060e6 | 2019-02-21 13:46:52 -0800 | [diff] [blame] | 403 | /** |
| 404 | * Changes the API access log sample rate for the Zygote and processes spawned from it. |
| 405 | * |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 406 | * This necessitates a change to the internal state of the Zygote. As such, if the USAP |
| 407 | * 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] | 408 | * properly handle this request the pool must be emptied and refilled. This process can return |
| 409 | * a Runnable object that must be returned to ZygoteServer.runSelectLoop to be invoked. |
| 410 | * |
| 411 | * @param zygoteServer The server object that received the request |
Andrei Onea | e8e150d | 2019-02-18 18:27:11 +0000 | [diff] [blame] | 412 | * @param samplingRate The new sample rate for regular logging |
| 413 | * @param statsdSamplingRate The new sample rate for statslog logging |
Chris Wailes | 01060e6 | 2019-02-21 13:46:52 -0800 | [diff] [blame] | 414 | * @return A Runnable object representing a new app in any blastulas spawned from here; the |
| 415 | * zygote process will always receive a null value from this function. |
| 416 | */ |
| 417 | private Runnable handleHiddenApiAccessLogSampleRate(ZygoteServer zygoteServer, |
Andrei Onea | e8e150d | 2019-02-18 18:27:11 +0000 | [diff] [blame] | 418 | int samplingRate, int statsdSamplingRate) { |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 419 | return stateChangeWithUsapPoolReset(zygoteServer, () -> { |
Andrei Onea | e8e150d | 2019-02-18 18:27:11 +0000 | [diff] [blame] | 420 | int maxSamplingRate = Math.max(samplingRate, statsdSamplingRate); |
| 421 | ZygoteInit.setHiddenApiAccessLogSampleRate(maxSamplingRate); |
Chris Wailes | 1f26331 | 2019-04-26 15:07:47 -0700 | [diff] [blame] | 422 | StatsdHiddenApiUsageLogger.setHiddenApiAccessLogSampleRates( |
| 423 | samplingRate, statsdSamplingRate); |
| 424 | ZygoteInit.setHiddenApiUsageLogger(StatsdHiddenApiUsageLogger.getInstance()); |
Chris Wailes | 01060e6 | 2019-02-21 13:46:52 -0800 | [diff] [blame] | 425 | }); |
Mathew Inwood | 04194fe | 2018-04-04 14:48:03 +0100 | [diff] [blame] | 426 | } |
| 427 | |
Narayan Kamath | 669afcc | 2017-02-06 20:24:08 +0000 | [diff] [blame] | 428 | protected void preload() { |
| 429 | ZygoteInit.lazyPreload(); |
| 430 | } |
| 431 | |
| 432 | protected boolean isPreloadComplete() { |
| 433 | return ZygoteInit.isPreloadComplete(); |
Torne (Richard Coles) | bb65893 | 2017-01-05 16:11:06 +0000 | [diff] [blame] | 434 | } |
| 435 | |
Narayan Kamath | 24a3306 | 2017-07-03 14:12:26 +0100 | [diff] [blame] | 436 | protected DataOutputStream getSocketOutputStream() { |
| 437 | return mSocketOutStream; |
| 438 | } |
| 439 | |
Torne (Richard Coles) | f4f647e | 2018-02-21 16:12:36 -0500 | [diff] [blame] | 440 | protected void handlePreloadPackage(String packagePath, String libsPath, String libFileName, |
| 441 | String cacheKey) { |
Martijn Coenen | 7e6fa67 | 2018-11-05 11:45:26 +0100 | [diff] [blame] | 442 | throw new RuntimeException("Zygote does not support package preloading"); |
| 443 | } |
| 444 | |
| 445 | protected boolean canPreloadApp() { |
| 446 | return false; |
| 447 | } |
| 448 | |
| 449 | protected void handlePreloadApp(ApplicationInfo aInfo) { |
| 450 | throw new RuntimeException("Zygote does not support app preloading"); |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 451 | } |
| 452 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 453 | /** |
| 454 | * Closes socket associated with this connection. |
| 455 | */ |
Andrei Onea | 1588439 | 2019-03-22 17:28:11 +0000 | [diff] [blame] | 456 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 457 | void closeSocket() { |
| 458 | try { |
| 459 | mSocket.close(); |
| 460 | } catch (IOException ex) { |
| 461 | Log.e(TAG, "Exception while closing command " |
| 462 | + "socket in parent", ex); |
| 463 | } |
| 464 | } |
| 465 | |
Narayan Kamath | bf99d06 | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 466 | boolean isClosedByPeer() { |
| 467 | return isEof; |
| 468 | } |
| 469 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 470 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 471 | * Handles post-fork setup of child proc, closing sockets as appropriate, |
| 472 | * reopen stdio as appropriate, and ultimately throwing MethodAndArgsCaller |
| 473 | * if successful or returning if failed. |
| 474 | * |
| 475 | * @param parsedArgs non-null; zygote args |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 476 | * @param pipeFd null-ok; pipe for communication back to Zygote. |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 477 | * @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] | 478 | */ |
Chris Wailes | ca848c4 | 2019-04-26 14:36:34 -0700 | [diff] [blame] | 479 | private Runnable handleChildProc(ZygoteArguments parsedArgs, |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 480 | FileDescriptor pipeFd, boolean isZygote) { |
Chris Wailes | 1f26331 | 2019-04-26 15:07:47 -0700 | [diff] [blame] | 481 | /* |
Dave Platt | 89d4c89 | 2014-02-05 17:06:42 -0800 | [diff] [blame] | 482 | * By the time we get here, the native code has closed the two actual Zygote |
| 483 | * socket connections, and substituted /dev/null in their place. The LocalSocket |
| 484 | * objects still need to be closed properly. |
| 485 | */ |
| 486 | |
Nick Kralevich | 468f6c1 | 2013-01-30 08:45:03 -0800 | [diff] [blame] | 487 | closeSocket(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 488 | |
Chris Wailes | 6d00c1a | 2019-04-23 14:26:13 -0700 | [diff] [blame] | 489 | Zygote.setAppProcessName(parsedArgs, TAG); |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 490 | |
Narayan Kamath | fbb32f6 | 2015-06-12 15:34:35 +0100 | [diff] [blame] | 491 | // End of the postFork event. |
| 492 | Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER); |
Chris Wailes | 2be2626 | 2019-01-11 16:14:43 -0800 | [diff] [blame] | 493 | if (parsedArgs.mInvokeWith != null) { |
| 494 | WrapperInit.execApplication(parsedArgs.mInvokeWith, |
| 495 | parsedArgs.mNiceName, parsedArgs.mTargetSdkVersion, |
Narayan Kamath | 37ad4b0 | 2015-01-19 16:05:24 +0000 | [diff] [blame] | 496 | VMRuntime.getCurrentInstructionSet(), |
Chris Wailes | 2be2626 | 2019-01-11 16:14:43 -0800 | [diff] [blame] | 497 | pipeFd, parsedArgs.mRemainingArgs); |
Narayan Kamath | bf99d06 | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 498 | |
| 499 | // Should not get here. |
| 500 | throw new IllegalStateException("WrapperInit.execApplication unexpectedly returned"); |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 501 | } else { |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 502 | if (!isZygote) { |
Chris Wailes | 2be2626 | 2019-01-11 16:14:43 -0800 | [diff] [blame] | 503 | return ZygoteInit.zygoteInit(parsedArgs.mTargetSdkVersion, |
atrost | 5ae996f | 2019-12-11 18:32:48 +0000 | [diff] [blame] | 504 | parsedArgs.mDisabledCompatChanges, |
Chris Wailes | 2be2626 | 2019-01-11 16:14:43 -0800 | [diff] [blame] | 505 | parsedArgs.mRemainingArgs, null /* classLoader */); |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 506 | } else { |
Chris Wailes | 2be2626 | 2019-01-11 16:14:43 -0800 | [diff] [blame] | 507 | return ZygoteInit.childZygoteInit(parsedArgs.mTargetSdkVersion, |
| 508 | parsedArgs.mRemainingArgs, null /* classLoader */); |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 509 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 510 | } |
| 511 | } |
| 512 | |
| 513 | /** |
| 514 | * Handles post-fork cleanup of parent proc |
| 515 | * |
| 516 | * @param pid != 0; pid of child if > 0 or indication of failed fork |
| 517 | * if < 0; |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 518 | * @param pipeFd null-ok; pipe for communication with child. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 519 | */ |
Chris Wailes | ca848c4 | 2019-04-26 14:36:34 -0700 | [diff] [blame] | 520 | private void handleParentProc(int pid, FileDescriptor pipeFd) { |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 521 | if (pid > 0) { |
| 522 | setChildPgid(pid); |
| 523 | } |
| 524 | |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 525 | boolean usingWrapper = false; |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 526 | if (pipeFd != null && pid > 0) { |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 527 | int innerPid = -1; |
| 528 | try { |
Andreas Gampe | cbc4214 | 2017-07-12 19:17:52 -0700 | [diff] [blame] | 529 | // Do a busy loop here. We can't guarantee that a failure (and thus an exception |
| 530 | // bail) happens in a timely manner. |
Andreas Gampe | cbc4214 | 2017-07-12 19:17:52 -0700 | [diff] [blame] | 531 | final int BYTES_REQUIRED = 4; // Bytes in an int. |
| 532 | |
Chris Wailes | 1f26331 | 2019-04-26 15:07:47 -0700 | [diff] [blame] | 533 | StructPollfd[] fds = new StructPollfd[] { |
Andreas Gampe | cbc4214 | 2017-07-12 19:17:52 -0700 | [diff] [blame] | 534 | new StructPollfd() |
| 535 | }; |
| 536 | |
Chris Wailes | 1f26331 | 2019-04-26 15:07:47 -0700 | [diff] [blame] | 537 | byte[] data = new byte[BYTES_REQUIRED]; |
Andreas Gampe | cbc4214 | 2017-07-12 19:17:52 -0700 | [diff] [blame] | 538 | |
Andreas Gampe | 27497c6 | 2017-07-21 11:41:00 -0700 | [diff] [blame] | 539 | int remainingSleepTime = WRAPPED_PID_TIMEOUT_MILLIS; |
Andreas Gampe | cbc4214 | 2017-07-12 19:17:52 -0700 | [diff] [blame] | 540 | int dataIndex = 0; |
| 541 | long startTime = System.nanoTime(); |
| 542 | |
| 543 | while (dataIndex < data.length && remainingSleepTime > 0) { |
| 544 | fds[0].fd = pipeFd; |
| 545 | fds[0].events = (short) POLLIN; |
| 546 | fds[0].revents = 0; |
| 547 | fds[0].userData = null; |
| 548 | |
| 549 | int res = android.system.Os.poll(fds, remainingSleepTime); |
| 550 | long endTime = System.nanoTime(); |
Chris Wailes | 1f26331 | 2019-04-26 15:07:47 -0700 | [diff] [blame] | 551 | int elapsedTimeMs = |
| 552 | (int) TimeUnit.MILLISECONDS.convert( |
| 553 | endTime - startTime, |
| 554 | TimeUnit.NANOSECONDS); |
Andreas Gampe | 27497c6 | 2017-07-21 11:41:00 -0700 | [diff] [blame] | 555 | remainingSleepTime = WRAPPED_PID_TIMEOUT_MILLIS - elapsedTimeMs; |
Andreas Gampe | cbc4214 | 2017-07-12 19:17:52 -0700 | [diff] [blame] | 556 | |
| 557 | if (res > 0) { |
| 558 | if ((fds[0].revents & POLLIN) != 0) { |
| 559 | // Only read one byte, so as not to block. |
| 560 | int readBytes = android.system.Os.read(pipeFd, data, dataIndex, 1); |
| 561 | if (readBytes < 0) { |
| 562 | throw new RuntimeException("Some error"); |
| 563 | } |
| 564 | dataIndex += readBytes; |
| 565 | } else { |
| 566 | // Error case. revents should contain one of the error bits. |
| 567 | break; |
| 568 | } |
| 569 | } else if (res == 0) { |
| 570 | Log.w(TAG, "Timed out waiting for child."); |
| 571 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 572 | } |
Andreas Gampe | cbc4214 | 2017-07-12 19:17:52 -0700 | [diff] [blame] | 573 | |
| 574 | if (dataIndex == data.length) { |
| 575 | DataInputStream is = new DataInputStream(new ByteArrayInputStream(data)); |
| 576 | innerPid = is.readInt(); |
| 577 | } |
| 578 | |
| 579 | if (innerPid == -1) { |
| 580 | Log.w(TAG, "Error reading pid from wrapped process, child may have died"); |
| 581 | } |
| 582 | } catch (Exception ex) { |
| 583 | 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] | 584 | } |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 585 | |
| 586 | // Ensure that the pid reported by the wrapped process is either the |
| 587 | // child process that we forked, or a descendant of it. |
| 588 | if (innerPid > 0) { |
| 589 | int parentPid = innerPid; |
| 590 | while (parentPid > 0 && parentPid != pid) { |
| 591 | parentPid = Process.getParentPid(parentPid); |
| 592 | } |
| 593 | if (parentPid > 0) { |
| 594 | Log.i(TAG, "Wrapped process has pid " + innerPid); |
| 595 | pid = innerPid; |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 596 | usingWrapper = true; |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 597 | } else { |
| 598 | Log.w(TAG, "Wrapped process reported a pid that is not a child of " |
| 599 | + "the process that we forked: childPid=" + pid |
| 600 | + " innerPid=" + innerPid); |
| 601 | } |
| 602 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | try { |
| 606 | mSocketOutStream.writeInt(pid); |
Jeff Brown | 3f9dd28 | 2011-07-08 20:02:19 -0700 | [diff] [blame] | 607 | mSocketOutStream.writeBoolean(usingWrapper); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 608 | } catch (IOException ex) { |
Narayan Kamath | bf99d06 | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 609 | throw new IllegalStateException("Error writing to command socket", ex); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 610 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 611 | } |
| 612 | |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 613 | private void setChildPgid(int pid) { |
| 614 | // Try to move the new child into the peer's process group. |
| 615 | try { |
Elliott Hughes | 26b56e6 | 2014-12-17 12:28:29 -0800 | [diff] [blame] | 616 | Os.setpgid(pid, Os.getpgid(peer.getPid())); |
| 617 | } catch (ErrnoException ex) { |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 618 | // This exception is expected in the case where |
| 619 | // the peer is not in our session |
| 620 | // TODO get rid of this log message in the case where |
| 621 | // getsid(0) != getsid(peer.getPid()) |
| 622 | Log.i(TAG, "Zygote: setpgid failed. This is " |
| 623 | + "normal if peer is not in our session"); |
| 624 | } |
| 625 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 626 | } |