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 | |
Elliott Hughes | 894724b | 2014-12-15 12:39:51 -0800 | [diff] [blame] | 19 | import static android.system.OsConstants.POLLIN; |
Elliott Hughes | a9569ff | 2014-04-28 20:53:52 -0700 | [diff] [blame] | 20 | import static android.system.OsConstants.S_IRWXG; |
| 21 | import static android.system.OsConstants.S_IRWXO; |
Kenny Root | 4c74f8c | 2012-08-17 08:45:55 -0700 | [diff] [blame] | 22 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | import android.content.res.Resources; |
| 24 | import android.content.res.TypedArray; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | import android.net.LocalServerSocket; |
Romain Guy | 74c6912 | 2013-05-08 17:54:20 -0700 | [diff] [blame] | 26 | import android.opengl.EGL14; |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 27 | import android.os.Process; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | import android.os.SystemClock; |
Romain Guy | c5e3638 | 2013-05-09 11:08:17 -0700 | [diff] [blame] | 29 | import android.os.SystemProperties; |
Jamie Gennis | 6ad0452 | 2013-04-15 18:53:24 -0700 | [diff] [blame] | 30 | import android.os.Trace; |
Bill Yi | 9a76e9b | 2014-04-29 18:52:48 -0700 | [diff] [blame] | 31 | import android.system.ErrnoException; |
Elliott Hughes | 860c591 | 2014-04-28 19:19:13 -0700 | [diff] [blame] | 32 | import android.system.Os; |
| 33 | import android.system.OsConstants; |
Elliott Hughes | 894724b | 2014-12-15 12:39:51 -0800 | [diff] [blame] | 34 | import android.system.StructPollfd; |
Raph Levien | c3dd1c1 | 2015-04-06 10:37:57 -0700 | [diff] [blame] | 35 | import android.text.Hyphenator; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | import android.util.EventLog; |
| 37 | import android.util.Log; |
Torne (Richard Coles) | 08cfaf6 | 2014-05-08 16:07:05 +0100 | [diff] [blame] | 38 | import android.webkit.WebViewFactory; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | |
Narayan Kamath | 29564cd | 2014-08-07 10:57:40 +0100 | [diff] [blame] | 40 | import dalvik.system.DexFile; |
| 41 | import dalvik.system.PathClassLoader; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | import dalvik.system.VMRuntime; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | |
Brian Carlstrom | 46703b0 | 2011-04-06 15:41:29 -0700 | [diff] [blame] | 44 | import libcore.io.IoUtils; |
| 45 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | import java.io.BufferedReader; |
| 47 | import java.io.FileDescriptor; |
Ying Wang | d0c4535 | 2014-11-13 15:22:47 -0800 | [diff] [blame] | 48 | import java.io.FileInputStream; |
| 49 | import java.io.FileNotFoundException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | import java.io.IOException; |
| 51 | import java.io.InputStream; |
| 52 | import java.io.InputStreamReader; |
| 53 | import java.lang.reflect.InvocationTargetException; |
| 54 | import java.lang.reflect.Method; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | import java.util.ArrayList; |
| 56 | |
| 57 | /** |
| 58 | * Startup class for the zygote process. |
| 59 | * |
| 60 | * Pre-initializes some classes, and then waits for commands on a UNIX domain |
Elliott Hughes | e1dfcb7 | 2011-07-08 11:08:07 -0700 | [diff] [blame] | 61 | * socket. Based on these commands, forks off child processes that inherit |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | * the initial state of the VM. |
| 63 | * |
| 64 | * Please see {@link ZygoteConnection.Arguments} for documentation on the |
| 65 | * client protocol. |
| 66 | * |
| 67 | * @hide |
| 68 | */ |
| 69 | public class ZygoteInit { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | private static final String TAG = "Zygote"; |
| 71 | |
Romain Guy | c5e3638 | 2013-05-09 11:08:17 -0700 | [diff] [blame] | 72 | private static final String PROPERTY_DISABLE_OPENGL_PRELOADING = "ro.zygote.disable_gl_preload"; |
| 73 | |
Narayan Kamath | c41638c | 2014-04-07 13:56:15 +0100 | [diff] [blame] | 74 | private static final String ANDROID_SOCKET_PREFIX = "ANDROID_SOCKET_"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | |
| 76 | private static final int LOG_BOOT_PROGRESS_PRELOAD_START = 3020; |
| 77 | private static final int LOG_BOOT_PROGRESS_PRELOAD_END = 3030; |
| 78 | |
| 79 | /** when preloading, GC after allocating this many bytes */ |
| 80 | private static final int PRELOAD_GC_THRESHOLD = 50000; |
| 81 | |
Narayan Kamath | c41638c | 2014-04-07 13:56:15 +0100 | [diff] [blame] | 82 | private static final String ABI_LIST_ARG = "--abi-list="; |
| 83 | |
| 84 | private static final String SOCKET_NAME_ARG = "--socket-name="; |
Barry Hayes | 0b3533a | 2010-01-20 12:46:47 -0800 | [diff] [blame] | 85 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | private static LocalServerSocket sServerSocket; |
| 87 | |
| 88 | /** |
| 89 | * Used to pre-load resources. We hold a global reference on it so it |
| 90 | * never gets destroyed. |
| 91 | */ |
| 92 | private static Resources mResources; |
Bob Lee | e540833 | 2009-09-04 18:31:17 -0700 | [diff] [blame] | 93 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 94 | /** |
Ying Wang | d0c4535 | 2014-11-13 15:22:47 -0800 | [diff] [blame] | 95 | * The path of a file that contains classes to preload. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | */ |
Ying Wang | d0c4535 | 2014-11-13 15:22:47 -0800 | [diff] [blame] | 97 | private static final String PRELOADED_CLASSES = "/system/etc/preloaded-classes"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 98 | |
| 99 | /** Controls whether we should preload resources during zygote init. */ |
| 100 | private static final boolean PRELOAD_RESOURCES = true; |
Andy McFadden | 599c918 | 2009-04-08 00:35:56 -0700 | [diff] [blame] | 101 | |
| 102 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 103 | * Registers a server socket for zygote command connections |
| 104 | * |
| 105 | * @throws RuntimeException when open fails |
| 106 | */ |
Narayan Kamath | c41638c | 2014-04-07 13:56:15 +0100 | [diff] [blame] | 107 | private static void registerZygoteSocket(String socketName) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | if (sServerSocket == null) { |
| 109 | int fileDesc; |
Narayan Kamath | c41638c | 2014-04-07 13:56:15 +0100 | [diff] [blame] | 110 | final String fullSocketName = ANDROID_SOCKET_PREFIX + socketName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | try { |
Narayan Kamath | c41638c | 2014-04-07 13:56:15 +0100 | [diff] [blame] | 112 | String env = System.getenv(fullSocketName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | fileDesc = Integer.parseInt(env); |
| 114 | } catch (RuntimeException ex) { |
Narayan Kamath | c41638c | 2014-04-07 13:56:15 +0100 | [diff] [blame] | 115 | throw new RuntimeException(fullSocketName + " unset or invalid", ex); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | try { |
Elliott Hughes | 3fe5951 | 2014-12-12 14:07:34 -0800 | [diff] [blame] | 119 | FileDescriptor fd = new FileDescriptor(); |
| 120 | fd.setInt$(fileDesc); |
| 121 | sServerSocket = new LocalServerSocket(fd); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 122 | } catch (IOException ex) { |
| 123 | throw new RuntimeException( |
| 124 | "Error binding to local socket '" + fileDesc + "'", ex); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Waits for and accepts a single command connection. Throws |
| 131 | * RuntimeException on failure. |
| 132 | */ |
Narayan Kamath | c41638c | 2014-04-07 13:56:15 +0100 | [diff] [blame] | 133 | private static ZygoteConnection acceptCommandPeer(String abiList) { |
Bob Lee | e540833 | 2009-09-04 18:31:17 -0700 | [diff] [blame] | 134 | try { |
Narayan Kamath | c41638c | 2014-04-07 13:56:15 +0100 | [diff] [blame] | 135 | return new ZygoteConnection(sServerSocket.accept(), abiList); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 136 | } catch (IOException ex) { |
| 137 | throw new RuntimeException( |
| 138 | "IOException during accept()", ex); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Close and clean up zygote sockets. Called on shutdown and on the |
| 144 | * child's exit path. |
| 145 | */ |
| 146 | static void closeServerSocket() { |
| 147 | try { |
| 148 | if (sServerSocket != null) { |
Dave Platt | 02f042d | 2013-12-12 15:45:49 -0800 | [diff] [blame] | 149 | FileDescriptor fd = sServerSocket.getFileDescriptor(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 150 | sServerSocket.close(); |
Dave Platt | 02f042d | 2013-12-12 15:45:49 -0800 | [diff] [blame] | 151 | if (fd != null) { |
Bill Yi | 9a76e9b | 2014-04-29 18:52:48 -0700 | [diff] [blame] | 152 | Os.close(fd); |
Dave Platt | 02f042d | 2013-12-12 15:45:49 -0800 | [diff] [blame] | 153 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 154 | } |
| 155 | } catch (IOException ex) { |
| 156 | Log.e(TAG, "Zygote: error closing sockets", ex); |
Bill Yi | 9a76e9b | 2014-04-29 18:52:48 -0700 | [diff] [blame] | 157 | } catch (ErrnoException ex) { |
Dave Platt | 02f042d | 2013-12-12 15:45:49 -0800 | [diff] [blame] | 158 | Log.e(TAG, "Zygote: error closing descriptor", ex); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | sServerSocket = null; |
| 162 | } |
| 163 | |
Dave Platt | 89d4c89 | 2014-02-05 17:06:42 -0800 | [diff] [blame] | 164 | /** |
| 165 | * Return the server socket's underlying file descriptor, so that |
| 166 | * ZygoteConnection can pass it to the native code for proper |
| 167 | * closure after a child process is forked off. |
| 168 | */ |
| 169 | |
| 170 | static FileDescriptor getServerSocketFileDescriptor() { |
| 171 | return sServerSocket.getFileDescriptor(); |
| 172 | } |
| 173 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 174 | private static final int UNPRIVILEGED_UID = 9999; |
| 175 | private static final int UNPRIVILEGED_GID = 9999; |
| 176 | |
| 177 | private static final int ROOT_UID = 0; |
| 178 | private static final int ROOT_GID = 0; |
| 179 | |
Jeff Brown | 0c2313d | 2011-06-10 22:46:40 -0700 | [diff] [blame] | 180 | static void preload() { |
Bill Yi | 9a76e9b | 2014-04-29 18:52:48 -0700 | [diff] [blame] | 181 | Log.d(TAG, "begin preload"); |
Jeff Brown | 0c2313d | 2011-06-10 22:46:40 -0700 | [diff] [blame] | 182 | preloadClasses(); |
| 183 | preloadResources(); |
Romain Guy | 74c6912 | 2013-05-08 17:54:20 -0700 | [diff] [blame] | 184 | preloadOpenGL(); |
Brian Carlstrom | 6c9af96 | 2014-09-11 15:36:00 -0700 | [diff] [blame] | 185 | preloadSharedLibraries(); |
Raph Levien | c3dd1c1 | 2015-04-06 10:37:57 -0700 | [diff] [blame] | 186 | preloadTextResources(); |
Torne (Richard Coles) | 08cfaf6 | 2014-05-08 16:07:05 +0100 | [diff] [blame] | 187 | // Ask the WebViewFactory to do any initialization that must run in the zygote process, |
| 188 | // for memory sharing purposes. |
| 189 | WebViewFactory.prepareWebViewInZygote(); |
Bill Yi | 9a76e9b | 2014-04-29 18:52:48 -0700 | [diff] [blame] | 190 | Log.d(TAG, "end preload"); |
Romain Guy | 74c6912 | 2013-05-08 17:54:20 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Brian Carlstrom | 6c9af96 | 2014-09-11 15:36:00 -0700 | [diff] [blame] | 193 | private static void preloadSharedLibraries() { |
| 194 | Log.i(TAG, "Preloading shared libraries..."); |
| 195 | System.loadLibrary("android"); |
| 196 | System.loadLibrary("compiler_rt"); |
| 197 | System.loadLibrary("jnigraphics"); |
| 198 | } |
| 199 | |
Romain Guy | 74c6912 | 2013-05-08 17:54:20 -0700 | [diff] [blame] | 200 | private static void preloadOpenGL() { |
Romain Guy | c5e3638 | 2013-05-09 11:08:17 -0700 | [diff] [blame] | 201 | if (!SystemProperties.getBoolean(PROPERTY_DISABLE_OPENGL_PRELOADING, false)) { |
| 202 | EGL14.eglGetDisplay(EGL14.EGL_DEFAULT_DISPLAY); |
| 203 | } |
Jeff Brown | 0c2313d | 2011-06-10 22:46:40 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Raph Levien | c3dd1c1 | 2015-04-06 10:37:57 -0700 | [diff] [blame] | 206 | private static void preloadTextResources() { |
| 207 | Hyphenator.init(); |
| 208 | } |
| 209 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 210 | /** |
| 211 | * Performs Zygote process initialization. Loads and initializes |
| 212 | * commonly used classes. |
| 213 | * |
| 214 | * Most classes only cause a few hundred bytes to be allocated, but |
| 215 | * a few will allocate a dozen Kbytes (in one case, 500+K). |
| 216 | */ |
| 217 | private static void preloadClasses() { |
| 218 | final VMRuntime runtime = VMRuntime.getRuntime(); |
Bob Lee | e540833 | 2009-09-04 18:31:17 -0700 | [diff] [blame] | 219 | |
Ying Wang | d0c4535 | 2014-11-13 15:22:47 -0800 | [diff] [blame] | 220 | InputStream is; |
| 221 | try { |
| 222 | is = new FileInputStream(PRELOADED_CLASSES); |
| 223 | } catch (FileNotFoundException e) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 224 | Log.e(TAG, "Couldn't find " + PRELOADED_CLASSES + "."); |
Ying Wang | d0c4535 | 2014-11-13 15:22:47 -0800 | [diff] [blame] | 225 | return; |
| 226 | } |
Bob Lee | e540833 | 2009-09-04 18:31:17 -0700 | [diff] [blame] | 227 | |
Ying Wang | d0c4535 | 2014-11-13 15:22:47 -0800 | [diff] [blame] | 228 | Log.i(TAG, "Preloading classes..."); |
| 229 | long startTime = SystemClock.uptimeMillis(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 230 | |
Ying Wang | d0c4535 | 2014-11-13 15:22:47 -0800 | [diff] [blame] | 231 | // Drop root perms while running static initializers. |
Narayan Kamath | 23e6878 | 2015-01-16 17:22:41 +0000 | [diff] [blame] | 232 | final int reuid = Os.getuid(); |
| 233 | final int regid = Os.getgid(); |
| 234 | |
| 235 | // We need to drop root perms only if we're already root. In the case of "wrapped" |
| 236 | // processes (see WrapperInit), this function is called from an unprivileged uid |
| 237 | // and gid. |
| 238 | boolean droppedPriviliges = false; |
| 239 | if (reuid == ROOT_UID && regid == ROOT_GID) { |
| 240 | try { |
| 241 | Os.setregid(ROOT_GID, UNPRIVILEGED_GID); |
| 242 | Os.setreuid(ROOT_UID, UNPRIVILEGED_UID); |
| 243 | } catch (ErrnoException ex) { |
| 244 | throw new RuntimeException("Failed to drop root", ex); |
| 245 | } |
| 246 | |
| 247 | droppedPriviliges = true; |
Elliott Hughes | 26b56e6 | 2014-12-17 12:28:29 -0800 | [diff] [blame] | 248 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 249 | |
Ying Wang | d0c4535 | 2014-11-13 15:22:47 -0800 | [diff] [blame] | 250 | // Alter the target heap utilization. With explicit GCs this |
| 251 | // is not likely to have any effect. |
| 252 | float defaultUtilization = runtime.getTargetHeapUtilization(); |
| 253 | runtime.setTargetHeapUtilization(0.8f); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 254 | |
Ying Wang | d0c4535 | 2014-11-13 15:22:47 -0800 | [diff] [blame] | 255 | try { |
| 256 | BufferedReader br |
| 257 | = new BufferedReader(new InputStreamReader(is), 256); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 258 | |
Ying Wang | d0c4535 | 2014-11-13 15:22:47 -0800 | [diff] [blame] | 259 | int count = 0; |
| 260 | String line; |
| 261 | while ((line = br.readLine()) != null) { |
| 262 | // Skip comments and blank lines. |
| 263 | line = line.trim(); |
| 264 | if (line.startsWith("#") || line.equals("")) { |
| 265 | continue; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 266 | } |
| 267 | |
Ying Wang | d0c4535 | 2014-11-13 15:22:47 -0800 | [diff] [blame] | 268 | try { |
| 269 | if (false) { |
| 270 | Log.v(TAG, "Preloading " + line + "..."); |
| 271 | } |
| 272 | Class.forName(line); |
| 273 | count++; |
| 274 | } catch (ClassNotFoundException e) { |
| 275 | Log.w(TAG, "Class not found for preloading: " + line); |
| 276 | } catch (UnsatisfiedLinkError e) { |
| 277 | Log.w(TAG, "Problem preloading " + line + ": " + e); |
| 278 | } catch (Throwable t) { |
| 279 | Log.e(TAG, "Error preloading " + line + ".", t); |
| 280 | if (t instanceof Error) { |
| 281 | throw (Error) t; |
| 282 | } |
| 283 | if (t instanceof RuntimeException) { |
| 284 | throw (RuntimeException) t; |
| 285 | } |
| 286 | throw new RuntimeException(t); |
| 287 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 288 | } |
Ying Wang | d0c4535 | 2014-11-13 15:22:47 -0800 | [diff] [blame] | 289 | |
| 290 | Log.i(TAG, "...preloaded " + count + " classes in " |
| 291 | + (SystemClock.uptimeMillis()-startTime) + "ms."); |
| 292 | } catch (IOException e) { |
| 293 | Log.e(TAG, "Error reading " + PRELOADED_CLASSES + ".", e); |
| 294 | } finally { |
| 295 | IoUtils.closeQuietly(is); |
| 296 | // Restore default. |
| 297 | runtime.setTargetHeapUtilization(defaultUtilization); |
| 298 | |
| 299 | // Fill in dex caches with classes, fields, and methods brought in by preloading. |
| 300 | runtime.preloadDexCaches(); |
| 301 | |
Narayan Kamath | 23e6878 | 2015-01-16 17:22:41 +0000 | [diff] [blame] | 302 | // Bring back root. We'll need it later if we're in the zygote. |
| 303 | if (droppedPriviliges) { |
| 304 | try { |
| 305 | Os.setreuid(ROOT_UID, ROOT_UID); |
| 306 | Os.setregid(ROOT_GID, ROOT_GID); |
| 307 | } catch (ErrnoException ex) { |
| 308 | throw new RuntimeException("Failed to restore root", ex); |
| 309 | } |
Elliott Hughes | 26b56e6 | 2014-12-17 12:28:29 -0800 | [diff] [blame] | 310 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * Load in commonly used resources, so they can be shared across |
| 316 | * processes. |
| 317 | * |
| 318 | * These tend to be a few Kbytes, but are frequently in the 20-40K |
| 319 | * range, and occasionally even larger. |
| 320 | */ |
| 321 | private static void preloadResources() { |
| 322 | final VMRuntime runtime = VMRuntime.getRuntime(); |
Bob Lee | e540833 | 2009-09-04 18:31:17 -0700 | [diff] [blame] | 323 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 324 | try { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 325 | mResources = Resources.getSystem(); |
| 326 | mResources.startPreloading(); |
| 327 | if (PRELOAD_RESOURCES) { |
| 328 | Log.i(TAG, "Preloading resources..."); |
| 329 | |
| 330 | long startTime = SystemClock.uptimeMillis(); |
| 331 | TypedArray ar = mResources.obtainTypedArray( |
| 332 | com.android.internal.R.array.preloaded_drawables); |
| 333 | int N = preloadDrawables(runtime, ar); |
Jeff Brown | 14577c4 | 2012-03-08 16:40:14 -0800 | [diff] [blame] | 334 | ar.recycle(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 335 | Log.i(TAG, "...preloaded " + N + " resources in " |
| 336 | + (SystemClock.uptimeMillis()-startTime) + "ms."); |
| 337 | |
| 338 | startTime = SystemClock.uptimeMillis(); |
| 339 | ar = mResources.obtainTypedArray( |
| 340 | com.android.internal.R.array.preloaded_color_state_lists); |
| 341 | N = preloadColorStateLists(runtime, ar); |
Jeff Brown | 14577c4 | 2012-03-08 16:40:14 -0800 | [diff] [blame] | 342 | ar.recycle(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 343 | Log.i(TAG, "...preloaded " + N + " resources in " |
| 344 | + (SystemClock.uptimeMillis()-startTime) + "ms."); |
| 345 | } |
| 346 | mResources.finishPreloading(); |
| 347 | } catch (RuntimeException e) { |
| 348 | Log.w(TAG, "Failure preloading resources", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 349 | } |
| 350 | } |
| 351 | |
| 352 | private static int preloadColorStateLists(VMRuntime runtime, TypedArray ar) { |
| 353 | int N = ar.length(); |
| 354 | for (int i=0; i<N; i++) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 355 | int id = ar.getResourceId(i, 0); |
Joe Onorato | 43a1765 | 2011-04-06 19:22:23 -0700 | [diff] [blame] | 356 | if (false) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 357 | Log.v(TAG, "Preloading resource #" + Integer.toHexString(id)); |
| 358 | } |
| 359 | if (id != 0) { |
Alan Viverette | 77bb6f1 | 2015-02-11 17:24:33 -0800 | [diff] [blame] | 360 | if (mResources.getColorStateList(id, null) == null) { |
Dianne Hackborn | dde331c | 2012-08-03 14:01:57 -0700 | [diff] [blame] | 361 | throw new IllegalArgumentException( |
| 362 | "Unable to find preloaded color resource #0x" |
| 363 | + Integer.toHexString(id) |
| 364 | + " (" + ar.getString(i) + ")"); |
| 365 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 366 | } |
| 367 | } |
| 368 | return N; |
| 369 | } |
| 370 | |
| 371 | |
| 372 | private static int preloadDrawables(VMRuntime runtime, TypedArray ar) { |
| 373 | int N = ar.length(); |
| 374 | for (int i=0; i<N; i++) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 375 | int id = ar.getResourceId(i, 0); |
Joe Onorato | 43a1765 | 2011-04-06 19:22:23 -0700 | [diff] [blame] | 376 | if (false) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 377 | Log.v(TAG, "Preloading resource #" + Integer.toHexString(id)); |
| 378 | } |
| 379 | if (id != 0) { |
Alan Viverette | 84e001a | 2014-09-22 00:04:22 -0700 | [diff] [blame] | 380 | if (mResources.getDrawable(id, null) == null) { |
Dianne Hackborn | dde331c | 2012-08-03 14:01:57 -0700 | [diff] [blame] | 381 | throw new IllegalArgumentException( |
| 382 | "Unable to find preloaded drawable resource #0x" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 383 | + Integer.toHexString(id) |
Dianne Hackborn | dde331c | 2012-08-03 14:01:57 -0700 | [diff] [blame] | 384 | + " (" + ar.getString(i) + ")"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 385 | } |
| 386 | } |
| 387 | } |
| 388 | return N; |
| 389 | } |
| 390 | |
| 391 | /** |
| 392 | * Runs several special GCs to try to clean up a few generations of |
| 393 | * softly- and final-reachable objects, along with any other garbage. |
| 394 | * This is only useful just before a fork(). |
| 395 | */ |
Mathieu Chartier | 9a88f10 | 2014-08-20 10:24:11 -0700 | [diff] [blame] | 396 | /*package*/ static void gcAndFinalize() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 397 | final VMRuntime runtime = VMRuntime.getRuntime(); |
| 398 | |
| 399 | /* runFinalizationSync() lets finalizers be called in Zygote, |
| 400 | * which doesn't have a HeapWorker thread. |
| 401 | */ |
Brian Carlstrom | 08065b9 | 2011-04-01 15:49:41 -0700 | [diff] [blame] | 402 | System.gc(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 403 | runtime.runFinalizationSync(); |
Brian Carlstrom | 08065b9 | 2011-04-01 15:49:41 -0700 | [diff] [blame] | 404 | System.gc(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | /** |
| 408 | * Finish remaining work for the newly forked system server process. |
| 409 | */ |
| 410 | private static void handleSystemServerProcess( |
| 411 | ZygoteConnection.Arguments parsedArgs) |
| 412 | throws ZygoteInit.MethodAndArgsCaller { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 413 | |
| 414 | closeServerSocket(); |
| 415 | |
Mike Lockwood | 90960e8 | 2010-08-06 09:15:25 -0400 | [diff] [blame] | 416 | // set umask to 0077 so new files and directories will default to owner-only permissions. |
Elliott Hughes | 860c591 | 2014-04-28 19:19:13 -0700 | [diff] [blame] | 417 | Os.umask(S_IRWXG | S_IRWXO); |
Mike Lockwood | 90960e8 | 2010-08-06 09:15:25 -0400 | [diff] [blame] | 418 | |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 419 | if (parsedArgs.niceName != null) { |
| 420 | Process.setArgV0(parsedArgs.niceName); |
| 421 | } |
| 422 | |
Narayan Kamath | 29564cd | 2014-08-07 10:57:40 +0100 | [diff] [blame] | 423 | final String systemServerClasspath = Os.getenv("SYSTEMSERVERCLASSPATH"); |
| 424 | if (systemServerClasspath != null) { |
| 425 | performSystemServerDexOpt(systemServerClasspath); |
| 426 | } |
| 427 | |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 428 | if (parsedArgs.invokeWith != null) { |
Narayan Kamath | 29564cd | 2014-08-07 10:57:40 +0100 | [diff] [blame] | 429 | String[] args = parsedArgs.remainingArgs; |
| 430 | // If we have a non-null system server class path, we'll have to duplicate the |
| 431 | // existing arguments and append the classpath to it. ART will handle the classpath |
| 432 | // correctly when we exec a new process. |
| 433 | if (systemServerClasspath != null) { |
| 434 | String[] amendedArgs = new String[args.length + 2]; |
| 435 | amendedArgs[0] = "-cp"; |
| 436 | amendedArgs[1] = systemServerClasspath; |
| 437 | System.arraycopy(parsedArgs.remainingArgs, 0, amendedArgs, 2, parsedArgs.remainingArgs.length); |
| 438 | } |
| 439 | |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 440 | WrapperInit.execApplication(parsedArgs.invokeWith, |
Elliott Hughes | e1dfcb7 | 2011-07-08 11:08:07 -0700 | [diff] [blame] | 441 | parsedArgs.niceName, parsedArgs.targetSdkVersion, |
Narayan Kamath | 37ad4b0 | 2015-01-19 16:05:24 +0000 | [diff] [blame] | 442 | VMRuntime.getCurrentInstructionSet(), null, args); |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 443 | } else { |
Narayan Kamath | 29564cd | 2014-08-07 10:57:40 +0100 | [diff] [blame] | 444 | ClassLoader cl = null; |
| 445 | if (systemServerClasspath != null) { |
| 446 | cl = new PathClassLoader(systemServerClasspath, ClassLoader.getSystemClassLoader()); |
| 447 | Thread.currentThread().setContextClassLoader(cl); |
| 448 | } |
| 449 | |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 450 | /* |
| 451 | * Pass the remaining arguments to SystemServer. |
| 452 | */ |
Narayan Kamath | 29564cd | 2014-08-07 10:57:40 +0100 | [diff] [blame] | 453 | RuntimeInit.zygoteInit(parsedArgs.targetSdkVersion, parsedArgs.remainingArgs, cl); |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 454 | } |
| 455 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 456 | /* should never reach here */ |
| 457 | } |
| 458 | |
| 459 | /** |
Narayan Kamath | 29564cd | 2014-08-07 10:57:40 +0100 | [diff] [blame] | 460 | * Performs dex-opt on the elements of {@code classPath}, if needed. We |
| 461 | * choose the instruction set of the current runtime. |
| 462 | */ |
| 463 | private static void performSystemServerDexOpt(String classPath) { |
| 464 | final String[] classPathElements = classPath.split(":"); |
| 465 | final InstallerConnection installer = new InstallerConnection(); |
| 466 | final String instructionSet = VMRuntime.getRuntime().vmInstructionSet(); |
| 467 | |
| 468 | try { |
| 469 | for (String classPathElement : classPathElements) { |
Richard Uhler | 7b08b35 | 2015-03-25 16:25:57 -0700 | [diff] [blame] | 470 | final int dexoptNeeded = DexFile.getDexOptNeeded( |
| 471 | classPathElement, "*", instructionSet, false /* defer */); |
| 472 | if (dexoptNeeded != DexFile.NO_DEXOPT_NEEDED) { |
| 473 | installer.dexopt(classPathElement, Process.SYSTEM_UID, false, |
| 474 | instructionSet, dexoptNeeded); |
Narayan Kamath | 29564cd | 2014-08-07 10:57:40 +0100 | [diff] [blame] | 475 | } |
| 476 | } |
| 477 | } catch (IOException ioe) { |
| 478 | throw new RuntimeException("Error starting system_server", ioe); |
| 479 | } finally { |
| 480 | installer.disconnect(); |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 485 | * Prepare the arguments and fork for the system server process. |
| 486 | */ |
Narayan Kamath | 64cd907 | 2014-05-13 13:35:14 +0100 | [diff] [blame] | 487 | private static boolean startSystemServer(String abiList, String socketName) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 488 | throws MethodAndArgsCaller, RuntimeException { |
Alex Klyubin | 48a06e7 | 2013-04-19 10:01:42 -0700 | [diff] [blame] | 489 | long capabilities = posixCapabilitiesAsBits( |
Daniel Leung | 49cbafa | 2013-12-20 10:37:35 -0800 | [diff] [blame] | 490 | OsConstants.CAP_BLOCK_SUSPEND, |
Alex Klyubin | 48a06e7 | 2013-04-19 10:01:42 -0700 | [diff] [blame] | 491 | OsConstants.CAP_KILL, |
| 492 | OsConstants.CAP_NET_ADMIN, |
| 493 | OsConstants.CAP_NET_BIND_SERVICE, |
| 494 | OsConstants.CAP_NET_BROADCAST, |
| 495 | OsConstants.CAP_NET_RAW, |
Alex Klyubin | 48a06e7 | 2013-04-19 10:01:42 -0700 | [diff] [blame] | 496 | OsConstants.CAP_SYS_MODULE, |
| 497 | OsConstants.CAP_SYS_NICE, |
| 498 | OsConstants.CAP_SYS_RESOURCE, |
| 499 | OsConstants.CAP_SYS_TIME, |
| 500 | OsConstants.CAP_SYS_TTY_CONFIG |
| 501 | ); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 502 | /* Hardcoded command line to start the system server */ |
| 503 | String args[] = { |
| 504 | "--setuid=1000", |
| 505 | "--setgid=1000", |
Jeff Sharkey | 184a010 | 2013-07-10 16:19:52 -0700 | [diff] [blame] | 506 | "--setgroups=1001,1002,1003,1004,1005,1006,1007,1008,1009,1010,1018,1032,3001,3002,3003,3006,3007", |
Alex Klyubin | 48a06e7 | 2013-04-19 10:01:42 -0700 | [diff] [blame] | 507 | "--capabilities=" + capabilities + "," + capabilities, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 508 | "--nice-name=system_server", |
Narayan Kamath | b6b044a | 2015-02-13 17:31:25 +0000 | [diff] [blame] | 509 | "--runtime-args", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 510 | "com.android.server.SystemServer", |
| 511 | }; |
| 512 | ZygoteConnection.Arguments parsedArgs = null; |
| 513 | |
| 514 | int pid; |
| 515 | |
| 516 | try { |
| 517 | parsedArgs = new ZygoteConnection.Arguments(args); |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 518 | ZygoteConnection.applyDebuggerSystemProperty(parsedArgs); |
| 519 | ZygoteConnection.applyInvokeWithSystemProperty(parsedArgs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 520 | |
| 521 | /* Request to fork the system server process */ |
| 522 | pid = Zygote.forkSystemServer( |
| 523 | parsedArgs.uid, parsedArgs.gid, |
Elliott Hughes | e1dfcb7 | 2011-07-08 11:08:07 -0700 | [diff] [blame] | 524 | parsedArgs.gids, |
| 525 | parsedArgs.debugFlags, |
| 526 | null, |
Andy McFadden | 1b4c796 | 2010-10-27 11:26:05 -0700 | [diff] [blame] | 527 | parsedArgs.permittedCapabilities, |
| 528 | parsedArgs.effectiveCapabilities); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 529 | } catch (IllegalArgumentException ex) { |
| 530 | throw new RuntimeException(ex); |
Bob Lee | e540833 | 2009-09-04 18:31:17 -0700 | [diff] [blame] | 531 | } |
| 532 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 533 | /* For child process */ |
| 534 | if (pid == 0) { |
Narayan Kamath | 64cd907 | 2014-05-13 13:35:14 +0100 | [diff] [blame] | 535 | if (hasSecondZygote(abiList)) { |
| 536 | waitForSecondaryZygote(socketName); |
| 537 | } |
| 538 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 539 | handleSystemServerProcess(parsedArgs); |
| 540 | } |
| 541 | |
| 542 | return true; |
| 543 | } |
| 544 | |
Alex Klyubin | 48a06e7 | 2013-04-19 10:01:42 -0700 | [diff] [blame] | 545 | /** |
| 546 | * Gets the bit array representation of the provided list of POSIX capabilities. |
| 547 | */ |
| 548 | private static long posixCapabilitiesAsBits(int... capabilities) { |
| 549 | long result = 0; |
| 550 | for (int capability : capabilities) { |
| 551 | if ((capability < 0) || (capability > OsConstants.CAP_LAST_CAP)) { |
| 552 | throw new IllegalArgumentException(String.valueOf(capability)); |
| 553 | } |
| 554 | result |= (1L << capability); |
| 555 | } |
| 556 | return result; |
| 557 | } |
| 558 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 559 | public static void main(String argv[]) { |
| 560 | try { |
Piotr Jastrzebski | da74a62 | 2015-02-12 13:55:23 +0000 | [diff] [blame] | 561 | RuntimeInit.enableDdms(); |
Bob Lee | e540833 | 2009-09-04 18:31:17 -0700 | [diff] [blame] | 562 | // Start profiling the zygote initialization. |
| 563 | SamplingProfilerIntegration.start(); |
| 564 | |
Narayan Kamath | c41638c | 2014-04-07 13:56:15 +0100 | [diff] [blame] | 565 | boolean startSystemServer = false; |
| 566 | String socketName = "zygote"; |
| 567 | String abiList = null; |
| 568 | for (int i = 1; i < argv.length; i++) { |
| 569 | if ("start-system-server".equals(argv[i])) { |
| 570 | startSystemServer = true; |
| 571 | } else if (argv[i].startsWith(ABI_LIST_ARG)) { |
| 572 | abiList = argv[i].substring(ABI_LIST_ARG.length()); |
| 573 | } else if (argv[i].startsWith(SOCKET_NAME_ARG)) { |
| 574 | socketName = argv[i].substring(SOCKET_NAME_ARG.length()); |
| 575 | } else { |
| 576 | throw new RuntimeException("Unknown command line argument: " + argv[i]); |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | if (abiList == null) { |
| 581 | throw new RuntimeException("No ABI list supplied."); |
| 582 | } |
| 583 | |
| 584 | registerZygoteSocket(socketName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 585 | EventLog.writeEvent(LOG_BOOT_PROGRESS_PRELOAD_START, |
| 586 | SystemClock.uptimeMillis()); |
Jeff Brown | 0c2313d | 2011-06-10 22:46:40 -0700 | [diff] [blame] | 587 | preload(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 588 | EventLog.writeEvent(LOG_BOOT_PROGRESS_PRELOAD_END, |
| 589 | SystemClock.uptimeMillis()); |
| 590 | |
Brian Carlstrom | 1751086 | 2010-08-18 14:27:40 -0700 | [diff] [blame] | 591 | // Finish profiling the zygote initialization. |
| 592 | SamplingProfilerIntegration.writeZygoteSnapshot(); |
Bob Lee | e540833 | 2009-09-04 18:31:17 -0700 | [diff] [blame] | 593 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 594 | // Do an initial gc to clean up after startup |
Mathieu Chartier | 9a88f10 | 2014-08-20 10:24:11 -0700 | [diff] [blame] | 595 | gcAndFinalize(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 596 | |
Jamie Gennis | 6ad0452 | 2013-04-15 18:53:24 -0700 | [diff] [blame] | 597 | // Disable tracing so that forked processes do not inherit stale tracing tags from |
| 598 | // Zygote. |
| 599 | Trace.setTracingEnabled(false); |
| 600 | |
Narayan Kamath | c41638c | 2014-04-07 13:56:15 +0100 | [diff] [blame] | 601 | if (startSystemServer) { |
Narayan Kamath | 64cd907 | 2014-05-13 13:35:14 +0100 | [diff] [blame] | 602 | startSystemServer(abiList, socketName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | Log.i(TAG, "Accepting command socket connections"); |
Narayan Kamath | c41638c | 2014-04-07 13:56:15 +0100 | [diff] [blame] | 606 | runSelectLoop(abiList); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 607 | |
| 608 | closeServerSocket(); |
| 609 | } catch (MethodAndArgsCaller caller) { |
| 610 | caller.run(); |
| 611 | } catch (RuntimeException ex) { |
| 612 | Log.e(TAG, "Zygote died with exception", ex); |
| 613 | closeServerSocket(); |
| 614 | throw ex; |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | /** |
Narayan Kamath | 64cd907 | 2014-05-13 13:35:14 +0100 | [diff] [blame] | 619 | * Return {@code true} if this device configuration has another zygote. |
| 620 | * |
| 621 | * We determine this by comparing the device ABI list with this zygotes |
| 622 | * list. If this zygote supports all ABIs this device supports, there won't |
| 623 | * be another zygote. |
| 624 | */ |
| 625 | private static boolean hasSecondZygote(String abiList) { |
| 626 | return !SystemProperties.get("ro.product.cpu.abilist").equals(abiList); |
| 627 | } |
| 628 | |
| 629 | private static void waitForSecondaryZygote(String socketName) { |
| 630 | String otherZygoteName = Process.ZYGOTE_SOCKET.equals(socketName) ? |
| 631 | Process.SECONDARY_ZYGOTE_SOCKET : Process.ZYGOTE_SOCKET; |
| 632 | while (true) { |
| 633 | try { |
| 634 | final Process.ZygoteState zs = Process.ZygoteState.connect(otherZygoteName); |
| 635 | zs.close(); |
| 636 | break; |
| 637 | } catch (IOException ioe) { |
| 638 | Log.w(TAG, "Got error connecting to zygote, retrying. msg= " + ioe.getMessage()); |
| 639 | } |
| 640 | |
| 641 | try { |
| 642 | Thread.sleep(1000); |
| 643 | } catch (InterruptedException ie) { |
| 644 | } |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 649 | * Runs the zygote process's select loop. Accepts new connections as |
| 650 | * they happen, and reads commands from connections one spawn-request's |
| 651 | * worth at a time. |
| 652 | * |
| 653 | * @throws MethodAndArgsCaller in a child process when a main() should |
| 654 | * be executed. |
| 655 | */ |
Narayan Kamath | c41638c | 2014-04-07 13:56:15 +0100 | [diff] [blame] | 656 | private static void runSelectLoop(String abiList) throws MethodAndArgsCaller { |
Nick Kralevich | cae3d9f | 2013-01-30 09:51:40 -0800 | [diff] [blame] | 657 | ArrayList<FileDescriptor> fds = new ArrayList<FileDescriptor>(); |
| 658 | ArrayList<ZygoteConnection> peers = new ArrayList<ZygoteConnection>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 659 | |
| 660 | fds.add(sServerSocket.getFileDescriptor()); |
| 661 | peers.add(null); |
| 662 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 663 | while (true) { |
Elliott Hughes | 894724b | 2014-12-15 12:39:51 -0800 | [diff] [blame] | 664 | StructPollfd[] pollFds = new StructPollfd[fds.size()]; |
| 665 | for (int i = 0; i < pollFds.length; ++i) { |
| 666 | pollFds[i] = new StructPollfd(); |
| 667 | pollFds[i].fd = fds.get(i); |
| 668 | pollFds[i].events = (short) POLLIN; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 669 | } |
Elliott Hughes | 894724b | 2014-12-15 12:39:51 -0800 | [diff] [blame] | 670 | try { |
| 671 | Os.poll(pollFds, -1); |
| 672 | } catch (ErrnoException ex) { |
| 673 | throw new RuntimeException("poll failed", ex); |
| 674 | } |
| 675 | for (int i = pollFds.length - 1; i >= 0; --i) { |
| 676 | if ((pollFds[i].revents & POLLIN) == 0) { |
| 677 | continue; |
| 678 | } |
| 679 | if (i == 0) { |
| 680 | ZygoteConnection newPeer = acceptCommandPeer(abiList); |
| 681 | peers.add(newPeer); |
| 682 | fds.add(newPeer.getFileDesciptor()); |
| 683 | } else { |
| 684 | boolean done = peers.get(i).runOnce(); |
| 685 | if (done) { |
| 686 | peers.remove(i); |
| 687 | fds.remove(i); |
| 688 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 689 | } |
| 690 | } |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 695 | * Class not instantiable. |
| 696 | */ |
| 697 | private ZygoteInit() { |
| 698 | } |
| 699 | |
| 700 | /** |
| 701 | * Helper exception class which holds a method and arguments and |
| 702 | * can call them. This is used as part of a trampoline to get rid of |
| 703 | * the initial process setup stack frames. |
| 704 | */ |
| 705 | public static class MethodAndArgsCaller extends Exception |
| 706 | implements Runnable { |
| 707 | /** method to call */ |
| 708 | private final Method mMethod; |
| 709 | |
| 710 | /** argument array */ |
| 711 | private final String[] mArgs; |
| 712 | |
| 713 | public MethodAndArgsCaller(Method method, String[] args) { |
| 714 | mMethod = method; |
| 715 | mArgs = args; |
| 716 | } |
| 717 | |
| 718 | public void run() { |
| 719 | try { |
| 720 | mMethod.invoke(null, new Object[] { mArgs }); |
| 721 | } catch (IllegalAccessException ex) { |
| 722 | throw new RuntimeException(ex); |
| 723 | } catch (InvocationTargetException ex) { |
| 724 | Throwable cause = ex.getCause(); |
| 725 | if (cause instanceof RuntimeException) { |
| 726 | throw (RuntimeException) cause; |
| 727 | } else if (cause instanceof Error) { |
| 728 | throw (Error) cause; |
| 729 | } |
| 730 | throw new RuntimeException(ex); |
| 731 | } |
| 732 | } |
| 733 | } |
| 734 | } |