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 | |
Kenny Root | 4c74f8c | 2012-08-17 08:45:55 -0700 | [diff] [blame] | 19 | import static libcore.io.OsConstants.S_IRWXG; |
| 20 | import static libcore.io.OsConstants.S_IRWXO; |
| 21 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | import android.content.res.Resources; |
| 23 | import android.content.res.TypedArray; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | import android.net.LocalServerSocket; |
Romain Guy | 74c6912 | 2013-05-08 17:54:20 -0700 | [diff] [blame] | 25 | import android.opengl.EGL14; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | import android.os.Debug; |
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; |
Elliott Hughes | 860c591 | 2014-04-28 19:19:13 -0700 | [diff] [blame^] | 31 | import android.system.Os; |
| 32 | import android.system.OsConstants; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | import android.util.EventLog; |
| 34 | import android.util.Log; |
| 35 | |
| 36 | import dalvik.system.VMRuntime; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | |
Brian Carlstrom | 46703b0 | 2011-04-06 15:41:29 -0700 | [diff] [blame] | 38 | import libcore.io.IoUtils; |
| 39 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | import java.io.BufferedReader; |
| 41 | import java.io.FileDescriptor; |
| 42 | import java.io.IOException; |
| 43 | import java.io.InputStream; |
| 44 | import java.io.InputStreamReader; |
| 45 | import java.lang.reflect.InvocationTargetException; |
| 46 | import java.lang.reflect.Method; |
| 47 | import java.lang.reflect.Modifier; |
| 48 | import java.util.ArrayList; |
| 49 | |
| 50 | /** |
| 51 | * Startup class for the zygote process. |
| 52 | * |
| 53 | * 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] | 54 | * 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] | 55 | * the initial state of the VM. |
| 56 | * |
| 57 | * Please see {@link ZygoteConnection.Arguments} for documentation on the |
| 58 | * client protocol. |
| 59 | * |
| 60 | * @hide |
| 61 | */ |
| 62 | public class ZygoteInit { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | private static final String TAG = "Zygote"; |
| 64 | |
Romain Guy | c5e3638 | 2013-05-09 11:08:17 -0700 | [diff] [blame] | 65 | private static final String PROPERTY_DISABLE_OPENGL_PRELOADING = "ro.zygote.disable_gl_preload"; |
| 66 | |
Narayan Kamath | c41638c | 2014-04-07 13:56:15 +0100 | [diff] [blame] | 67 | private static final String ANDROID_SOCKET_PREFIX = "ANDROID_SOCKET_"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | |
| 69 | private static final int LOG_BOOT_PROGRESS_PRELOAD_START = 3020; |
| 70 | private static final int LOG_BOOT_PROGRESS_PRELOAD_END = 3030; |
| 71 | |
| 72 | /** when preloading, GC after allocating this many bytes */ |
| 73 | private static final int PRELOAD_GC_THRESHOLD = 50000; |
| 74 | |
Narayan Kamath | c41638c | 2014-04-07 13:56:15 +0100 | [diff] [blame] | 75 | private static final String ABI_LIST_ARG = "--abi-list="; |
| 76 | |
| 77 | private static final String SOCKET_NAME_ARG = "--socket-name="; |
Barry Hayes | 0b3533a | 2010-01-20 12:46:47 -0800 | [diff] [blame] | 78 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | private static LocalServerSocket sServerSocket; |
| 80 | |
| 81 | /** |
| 82 | * Used to pre-load resources. We hold a global reference on it so it |
| 83 | * never gets destroyed. |
| 84 | */ |
| 85 | private static Resources mResources; |
Bob Lee | e540833 | 2009-09-04 18:31:17 -0700 | [diff] [blame] | 86 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | /** |
| 88 | * The number of times that the main Zygote loop |
| 89 | * should run before calling gc() again. |
| 90 | */ |
| 91 | static final int GC_LOOP_COUNT = 10; |
| 92 | |
| 93 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 94 | * The name of a resource file that contains classes to preload. |
| 95 | */ |
| 96 | private static final String PRELOADED_CLASSES = "preloaded-classes"; |
| 97 | |
| 98 | /** Controls whether we should preload resources during zygote init. */ |
| 99 | private static final boolean PRELOAD_RESOURCES = true; |
Andy McFadden | 599c918 | 2009-04-08 00:35:56 -0700 | [diff] [blame] | 100 | |
| 101 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | * Invokes a static "main(argv[]) method on class "className". |
| 103 | * Converts various failing exceptions into RuntimeExceptions, with |
| 104 | * the assumption that they will then cause the VM instance to exit. |
| 105 | * |
| 106 | * @param loader class loader to use |
| 107 | * @param className Fully-qualified class name |
| 108 | * @param argv Argument vector for main() |
| 109 | */ |
| 110 | static void invokeStaticMain(ClassLoader loader, |
| 111 | String className, String[] argv) |
| 112 | throws ZygoteInit.MethodAndArgsCaller { |
| 113 | Class<?> cl; |
| 114 | |
| 115 | try { |
| 116 | cl = loader.loadClass(className); |
| 117 | } catch (ClassNotFoundException ex) { |
| 118 | throw new RuntimeException( |
| 119 | "Missing class when invoking static main " + className, |
| 120 | ex); |
| 121 | } |
| 122 | |
| 123 | Method m; |
| 124 | try { |
| 125 | m = cl.getMethod("main", new Class[] { String[].class }); |
| 126 | } catch (NoSuchMethodException ex) { |
| 127 | throw new RuntimeException( |
| 128 | "Missing static main on " + className, ex); |
| 129 | } catch (SecurityException ex) { |
| 130 | throw new RuntimeException( |
| 131 | "Problem getting static main on " + className, ex); |
| 132 | } |
| 133 | |
| 134 | int modifiers = m.getModifiers(); |
| 135 | if (! (Modifier.isStatic(modifiers) && Modifier.isPublic(modifiers))) { |
| 136 | throw new RuntimeException( |
| 137 | "Main method is not public and static on " + className); |
| 138 | } |
| 139 | |
| 140 | /* |
| 141 | * This throw gets caught in ZygoteInit.main(), which responds |
| 142 | * by invoking the exception's run() method. This arrangement |
| 143 | * clears up all the stack frames that were required in setting |
| 144 | * up the process. |
| 145 | */ |
| 146 | throw new ZygoteInit.MethodAndArgsCaller(m, argv); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Registers a server socket for zygote command connections |
| 151 | * |
| 152 | * @throws RuntimeException when open fails |
| 153 | */ |
Narayan Kamath | c41638c | 2014-04-07 13:56:15 +0100 | [diff] [blame] | 154 | private static void registerZygoteSocket(String socketName) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 155 | if (sServerSocket == null) { |
| 156 | int fileDesc; |
Narayan Kamath | c41638c | 2014-04-07 13:56:15 +0100 | [diff] [blame] | 157 | final String fullSocketName = ANDROID_SOCKET_PREFIX + socketName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 158 | try { |
Narayan Kamath | c41638c | 2014-04-07 13:56:15 +0100 | [diff] [blame] | 159 | String env = System.getenv(fullSocketName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 160 | fileDesc = Integer.parseInt(env); |
| 161 | } catch (RuntimeException ex) { |
Narayan Kamath | c41638c | 2014-04-07 13:56:15 +0100 | [diff] [blame] | 162 | throw new RuntimeException(fullSocketName + " unset or invalid", ex); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | try { |
| 166 | sServerSocket = new LocalServerSocket( |
| 167 | createFileDescriptor(fileDesc)); |
| 168 | } catch (IOException ex) { |
| 169 | throw new RuntimeException( |
| 170 | "Error binding to local socket '" + fileDesc + "'", ex); |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Waits for and accepts a single command connection. Throws |
| 177 | * RuntimeException on failure. |
| 178 | */ |
Narayan Kamath | c41638c | 2014-04-07 13:56:15 +0100 | [diff] [blame] | 179 | private static ZygoteConnection acceptCommandPeer(String abiList) { |
Bob Lee | e540833 | 2009-09-04 18:31:17 -0700 | [diff] [blame] | 180 | try { |
Narayan Kamath | c41638c | 2014-04-07 13:56:15 +0100 | [diff] [blame] | 181 | return new ZygoteConnection(sServerSocket.accept(), abiList); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 182 | } catch (IOException ex) { |
| 183 | throw new RuntimeException( |
| 184 | "IOException during accept()", ex); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Close and clean up zygote sockets. Called on shutdown and on the |
| 190 | * child's exit path. |
| 191 | */ |
| 192 | static void closeServerSocket() { |
| 193 | try { |
| 194 | if (sServerSocket != null) { |
| 195 | sServerSocket.close(); |
| 196 | } |
| 197 | } catch (IOException ex) { |
| 198 | Log.e(TAG, "Zygote: error closing sockets", ex); |
| 199 | } |
| 200 | |
| 201 | sServerSocket = null; |
| 202 | } |
| 203 | |
Dave Platt | 89d4c89 | 2014-02-05 17:06:42 -0800 | [diff] [blame] | 204 | /** |
| 205 | * Return the server socket's underlying file descriptor, so that |
| 206 | * ZygoteConnection can pass it to the native code for proper |
| 207 | * closure after a child process is forked off. |
| 208 | */ |
| 209 | |
| 210 | static FileDescriptor getServerSocketFileDescriptor() { |
| 211 | return sServerSocket.getFileDescriptor(); |
| 212 | } |
| 213 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 214 | private static final int UNPRIVILEGED_UID = 9999; |
| 215 | private static final int UNPRIVILEGED_GID = 9999; |
| 216 | |
| 217 | private static final int ROOT_UID = 0; |
| 218 | private static final int ROOT_GID = 0; |
| 219 | |
| 220 | /** |
| 221 | * Sets effective user ID. |
| 222 | */ |
| 223 | private static void setEffectiveUser(int uid) { |
| 224 | int errno = setreuid(ROOT_UID, uid); |
| 225 | if (errno != 0) { |
| 226 | Log.e(TAG, "setreuid() failed. errno: " + errno); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Sets effective group ID. |
| 232 | */ |
| 233 | private static void setEffectiveGroup(int gid) { |
| 234 | int errno = setregid(ROOT_GID, gid); |
| 235 | if (errno != 0) { |
| 236 | Log.e(TAG, "setregid() failed. errno: " + errno); |
| 237 | } |
| 238 | } |
| 239 | |
Jeff Brown | 0c2313d | 2011-06-10 22:46:40 -0700 | [diff] [blame] | 240 | static void preload() { |
| 241 | preloadClasses(); |
| 242 | preloadResources(); |
Romain Guy | 74c6912 | 2013-05-08 17:54:20 -0700 | [diff] [blame] | 243 | preloadOpenGL(); |
| 244 | } |
| 245 | |
| 246 | private static void preloadOpenGL() { |
Romain Guy | c5e3638 | 2013-05-09 11:08:17 -0700 | [diff] [blame] | 247 | if (!SystemProperties.getBoolean(PROPERTY_DISABLE_OPENGL_PRELOADING, false)) { |
| 248 | EGL14.eglGetDisplay(EGL14.EGL_DEFAULT_DISPLAY); |
| 249 | } |
Jeff Brown | 0c2313d | 2011-06-10 22:46:40 -0700 | [diff] [blame] | 250 | } |
| 251 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 252 | /** |
| 253 | * Performs Zygote process initialization. Loads and initializes |
| 254 | * commonly used classes. |
| 255 | * |
| 256 | * Most classes only cause a few hundred bytes to be allocated, but |
| 257 | * a few will allocate a dozen Kbytes (in one case, 500+K). |
| 258 | */ |
| 259 | private static void preloadClasses() { |
| 260 | final VMRuntime runtime = VMRuntime.getRuntime(); |
Bob Lee | e540833 | 2009-09-04 18:31:17 -0700 | [diff] [blame] | 261 | |
Andy McFadden | dc7bf5d | 2012-01-23 09:48:53 -0800 | [diff] [blame] | 262 | InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 263 | PRELOADED_CLASSES); |
| 264 | if (is == null) { |
| 265 | Log.e(TAG, "Couldn't find " + PRELOADED_CLASSES + "."); |
| 266 | } else { |
| 267 | Log.i(TAG, "Preloading classes..."); |
| 268 | long startTime = SystemClock.uptimeMillis(); |
Bob Lee | e540833 | 2009-09-04 18:31:17 -0700 | [diff] [blame] | 269 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 270 | // Drop root perms while running static initializers. |
| 271 | setEffectiveGroup(UNPRIVILEGED_GID); |
| 272 | setEffectiveUser(UNPRIVILEGED_UID); |
| 273 | |
| 274 | // Alter the target heap utilization. With explicit GCs this |
| 275 | // is not likely to have any effect. |
| 276 | float defaultUtilization = runtime.getTargetHeapUtilization(); |
| 277 | runtime.setTargetHeapUtilization(0.8f); |
| 278 | |
| 279 | // Start with a clean slate. |
Brian Carlstrom | 08065b9 | 2011-04-01 15:49:41 -0700 | [diff] [blame] | 280 | System.gc(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 281 | runtime.runFinalizationSync(); |
| 282 | Debug.startAllocCounting(); |
| 283 | |
| 284 | try { |
Bob Lee | e540833 | 2009-09-04 18:31:17 -0700 | [diff] [blame] | 285 | BufferedReader br |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 286 | = new BufferedReader(new InputStreamReader(is), 256); |
| 287 | |
| 288 | int count = 0; |
| 289 | String line; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 290 | while ((line = br.readLine()) != null) { |
| 291 | // Skip comments and blank lines. |
| 292 | line = line.trim(); |
| 293 | if (line.startsWith("#") || line.equals("")) { |
| 294 | continue; |
| 295 | } |
| 296 | |
| 297 | try { |
Joe Onorato | 43a1765 | 2011-04-06 19:22:23 -0700 | [diff] [blame] | 298 | if (false) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 299 | Log.v(TAG, "Preloading " + line + "..."); |
| 300 | } |
| 301 | Class.forName(line); |
| 302 | if (Debug.getGlobalAllocSize() > PRELOAD_GC_THRESHOLD) { |
Joe Onorato | 43a1765 | 2011-04-06 19:22:23 -0700 | [diff] [blame] | 303 | if (false) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 304 | Log.v(TAG, |
| 305 | " GC at " + Debug.getGlobalAllocSize()); |
| 306 | } |
Brian Carlstrom | 08065b9 | 2011-04-01 15:49:41 -0700 | [diff] [blame] | 307 | System.gc(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 308 | runtime.runFinalizationSync(); |
| 309 | Debug.resetGlobalAllocSize(); |
| 310 | } |
| 311 | count++; |
| 312 | } catch (ClassNotFoundException e) { |
Jesse Wilson | e241754 | 2010-06-03 12:33:26 -0700 | [diff] [blame] | 313 | Log.w(TAG, "Class not found for preloading: " + line); |
Brian Carlstrom | ad13bd5 | 2013-11-03 22:47:11 -0800 | [diff] [blame] | 314 | } catch (UnsatisfiedLinkError e) { |
| 315 | Log.w(TAG, "Problem preloading " + line + ": " + e); |
Bob Lee | 2e93f65 | 2009-08-11 01:16:03 -0700 | [diff] [blame] | 316 | } catch (Throwable t) { |
| 317 | Log.e(TAG, "Error preloading " + line + ".", t); |
| 318 | if (t instanceof Error) { |
| 319 | throw (Error) t; |
| 320 | } |
| 321 | if (t instanceof RuntimeException) { |
| 322 | throw (RuntimeException) t; |
| 323 | } |
| 324 | throw new RuntimeException(t); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 325 | } |
| 326 | } |
| 327 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 328 | Log.i(TAG, "...preloaded " + count + " classes in " |
| 329 | + (SystemClock.uptimeMillis()-startTime) + "ms."); |
| 330 | } catch (IOException e) { |
| 331 | Log.e(TAG, "Error reading " + PRELOADED_CLASSES + ".", e); |
| 332 | } finally { |
Brian Carlstrom | 46703b0 | 2011-04-06 15:41:29 -0700 | [diff] [blame] | 333 | IoUtils.closeQuietly(is); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 334 | // Restore default. |
| 335 | runtime.setTargetHeapUtilization(defaultUtilization); |
| 336 | |
Brian Carlstrom | 6c3baf1 | 2013-10-07 17:47:37 -0700 | [diff] [blame] | 337 | // Fill in dex caches with classes, fields, and methods brought in by preloading. |
| 338 | runtime.preloadDexCaches(); |
| 339 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 340 | Debug.stopAllocCounting(); |
| 341 | |
| 342 | // Bring back root. We'll need it later. |
| 343 | setEffectiveUser(ROOT_UID); |
| 344 | setEffectiveGroup(ROOT_GID); |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | /** |
| 350 | * Load in commonly used resources, so they can be shared across |
| 351 | * processes. |
| 352 | * |
| 353 | * These tend to be a few Kbytes, but are frequently in the 20-40K |
| 354 | * range, and occasionally even larger. |
| 355 | */ |
| 356 | private static void preloadResources() { |
| 357 | final VMRuntime runtime = VMRuntime.getRuntime(); |
Bob Lee | e540833 | 2009-09-04 18:31:17 -0700 | [diff] [blame] | 358 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 359 | Debug.startAllocCounting(); |
| 360 | try { |
Brian Carlstrom | 08065b9 | 2011-04-01 15:49:41 -0700 | [diff] [blame] | 361 | System.gc(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 362 | runtime.runFinalizationSync(); |
| 363 | mResources = Resources.getSystem(); |
| 364 | mResources.startPreloading(); |
| 365 | if (PRELOAD_RESOURCES) { |
| 366 | Log.i(TAG, "Preloading resources..."); |
| 367 | |
| 368 | long startTime = SystemClock.uptimeMillis(); |
| 369 | TypedArray ar = mResources.obtainTypedArray( |
| 370 | com.android.internal.R.array.preloaded_drawables); |
| 371 | int N = preloadDrawables(runtime, ar); |
Jeff Brown | 14577c4 | 2012-03-08 16:40:14 -0800 | [diff] [blame] | 372 | ar.recycle(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 373 | Log.i(TAG, "...preloaded " + N + " resources in " |
| 374 | + (SystemClock.uptimeMillis()-startTime) + "ms."); |
| 375 | |
| 376 | startTime = SystemClock.uptimeMillis(); |
| 377 | ar = mResources.obtainTypedArray( |
| 378 | com.android.internal.R.array.preloaded_color_state_lists); |
| 379 | N = preloadColorStateLists(runtime, ar); |
Jeff Brown | 14577c4 | 2012-03-08 16:40:14 -0800 | [diff] [blame] | 380 | ar.recycle(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 381 | Log.i(TAG, "...preloaded " + N + " resources in " |
| 382 | + (SystemClock.uptimeMillis()-startTime) + "ms."); |
| 383 | } |
| 384 | mResources.finishPreloading(); |
| 385 | } catch (RuntimeException e) { |
| 386 | Log.w(TAG, "Failure preloading resources", e); |
| 387 | } finally { |
| 388 | Debug.stopAllocCounting(); |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | private static int preloadColorStateLists(VMRuntime runtime, TypedArray ar) { |
| 393 | int N = ar.length(); |
| 394 | for (int i=0; i<N; i++) { |
| 395 | if (Debug.getGlobalAllocSize() > PRELOAD_GC_THRESHOLD) { |
Joe Onorato | 43a1765 | 2011-04-06 19:22:23 -0700 | [diff] [blame] | 396 | if (false) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 397 | Log.v(TAG, " GC at " + Debug.getGlobalAllocSize()); |
| 398 | } |
Brian Carlstrom | 08065b9 | 2011-04-01 15:49:41 -0700 | [diff] [blame] | 399 | System.gc(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 400 | runtime.runFinalizationSync(); |
| 401 | Debug.resetGlobalAllocSize(); |
| 402 | } |
| 403 | int id = ar.getResourceId(i, 0); |
Joe Onorato | 43a1765 | 2011-04-06 19:22:23 -0700 | [diff] [blame] | 404 | if (false) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 405 | Log.v(TAG, "Preloading resource #" + Integer.toHexString(id)); |
| 406 | } |
| 407 | if (id != 0) { |
Dianne Hackborn | dde331c | 2012-08-03 14:01:57 -0700 | [diff] [blame] | 408 | if (mResources.getColorStateList(id) == null) { |
| 409 | throw new IllegalArgumentException( |
| 410 | "Unable to find preloaded color resource #0x" |
| 411 | + Integer.toHexString(id) |
| 412 | + " (" + ar.getString(i) + ")"); |
| 413 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 414 | } |
| 415 | } |
| 416 | return N; |
| 417 | } |
| 418 | |
| 419 | |
| 420 | private static int preloadDrawables(VMRuntime runtime, TypedArray ar) { |
| 421 | int N = ar.length(); |
| 422 | for (int i=0; i<N; i++) { |
| 423 | if (Debug.getGlobalAllocSize() > PRELOAD_GC_THRESHOLD) { |
Joe Onorato | 43a1765 | 2011-04-06 19:22:23 -0700 | [diff] [blame] | 424 | if (false) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 425 | Log.v(TAG, " GC at " + Debug.getGlobalAllocSize()); |
| 426 | } |
Brian Carlstrom | 08065b9 | 2011-04-01 15:49:41 -0700 | [diff] [blame] | 427 | System.gc(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 428 | runtime.runFinalizationSync(); |
| 429 | Debug.resetGlobalAllocSize(); |
| 430 | } |
| 431 | int id = ar.getResourceId(i, 0); |
Joe Onorato | 43a1765 | 2011-04-06 19:22:23 -0700 | [diff] [blame] | 432 | if (false) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 433 | Log.v(TAG, "Preloading resource #" + Integer.toHexString(id)); |
| 434 | } |
| 435 | if (id != 0) { |
Dianne Hackborn | dde331c | 2012-08-03 14:01:57 -0700 | [diff] [blame] | 436 | if (mResources.getDrawable(id) == null) { |
| 437 | throw new IllegalArgumentException( |
| 438 | "Unable to find preloaded drawable resource #0x" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 439 | + Integer.toHexString(id) |
Dianne Hackborn | dde331c | 2012-08-03 14:01:57 -0700 | [diff] [blame] | 440 | + " (" + ar.getString(i) + ")"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 441 | } |
| 442 | } |
| 443 | } |
| 444 | return N; |
| 445 | } |
| 446 | |
| 447 | /** |
| 448 | * Runs several special GCs to try to clean up a few generations of |
| 449 | * softly- and final-reachable objects, along with any other garbage. |
| 450 | * This is only useful just before a fork(). |
| 451 | */ |
| 452 | /*package*/ static void gc() { |
| 453 | final VMRuntime runtime = VMRuntime.getRuntime(); |
| 454 | |
| 455 | /* runFinalizationSync() lets finalizers be called in Zygote, |
| 456 | * which doesn't have a HeapWorker thread. |
| 457 | */ |
Brian Carlstrom | 08065b9 | 2011-04-01 15:49:41 -0700 | [diff] [blame] | 458 | System.gc(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 459 | runtime.runFinalizationSync(); |
Brian Carlstrom | 08065b9 | 2011-04-01 15:49:41 -0700 | [diff] [blame] | 460 | System.gc(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 461 | runtime.runFinalizationSync(); |
Brian Carlstrom | 08065b9 | 2011-04-01 15:49:41 -0700 | [diff] [blame] | 462 | System.gc(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 463 | runtime.runFinalizationSync(); |
| 464 | } |
| 465 | |
| 466 | /** |
| 467 | * Finish remaining work for the newly forked system server process. |
| 468 | */ |
| 469 | private static void handleSystemServerProcess( |
| 470 | ZygoteConnection.Arguments parsedArgs) |
| 471 | throws ZygoteInit.MethodAndArgsCaller { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 472 | |
| 473 | closeServerSocket(); |
| 474 | |
Mike Lockwood | 90960e8 | 2010-08-06 09:15:25 -0400 | [diff] [blame] | 475 | // 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^] | 476 | Os.umask(S_IRWXG | S_IRWXO); |
Mike Lockwood | 90960e8 | 2010-08-06 09:15:25 -0400 | [diff] [blame] | 477 | |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 478 | if (parsedArgs.niceName != null) { |
| 479 | Process.setArgV0(parsedArgs.niceName); |
| 480 | } |
| 481 | |
| 482 | if (parsedArgs.invokeWith != null) { |
| 483 | WrapperInit.execApplication(parsedArgs.invokeWith, |
Elliott Hughes | e1dfcb7 | 2011-07-08 11:08:07 -0700 | [diff] [blame] | 484 | parsedArgs.niceName, parsedArgs.targetSdkVersion, |
| 485 | null, parsedArgs.remainingArgs); |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 486 | } else { |
| 487 | /* |
| 488 | * Pass the remaining arguments to SystemServer. |
| 489 | */ |
Elliott Hughes | e1dfcb7 | 2011-07-08 11:08:07 -0700 | [diff] [blame] | 490 | RuntimeInit.zygoteInit(parsedArgs.targetSdkVersion, parsedArgs.remainingArgs); |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 491 | } |
| 492 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 493 | /* should never reach here */ |
| 494 | } |
| 495 | |
| 496 | /** |
| 497 | * Prepare the arguments and fork for the system server process. |
| 498 | */ |
Bob Lee | e540833 | 2009-09-04 18:31:17 -0700 | [diff] [blame] | 499 | private static boolean startSystemServer() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 500 | throws MethodAndArgsCaller, RuntimeException { |
Alex Klyubin | 48a06e7 | 2013-04-19 10:01:42 -0700 | [diff] [blame] | 501 | long capabilities = posixCapabilitiesAsBits( |
Daniel Leung | 49cbafa | 2013-12-20 10:37:35 -0800 | [diff] [blame] | 502 | OsConstants.CAP_BLOCK_SUSPEND, |
Alex Klyubin | 48a06e7 | 2013-04-19 10:01:42 -0700 | [diff] [blame] | 503 | OsConstants.CAP_KILL, |
| 504 | OsConstants.CAP_NET_ADMIN, |
| 505 | OsConstants.CAP_NET_BIND_SERVICE, |
| 506 | OsConstants.CAP_NET_BROADCAST, |
| 507 | OsConstants.CAP_NET_RAW, |
Alex Klyubin | 48a06e7 | 2013-04-19 10:01:42 -0700 | [diff] [blame] | 508 | OsConstants.CAP_SYS_MODULE, |
| 509 | OsConstants.CAP_SYS_NICE, |
| 510 | OsConstants.CAP_SYS_RESOURCE, |
| 511 | OsConstants.CAP_SYS_TIME, |
| 512 | OsConstants.CAP_SYS_TTY_CONFIG |
| 513 | ); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 514 | /* Hardcoded command line to start the system server */ |
| 515 | String args[] = { |
| 516 | "--setuid=1000", |
| 517 | "--setgid=1000", |
Jeff Sharkey | 184a010 | 2013-07-10 16:19:52 -0700 | [diff] [blame] | 518 | "--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] | 519 | "--capabilities=" + capabilities + "," + capabilities, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 520 | "--runtime-init", |
| 521 | "--nice-name=system_server", |
| 522 | "com.android.server.SystemServer", |
| 523 | }; |
| 524 | ZygoteConnection.Arguments parsedArgs = null; |
| 525 | |
| 526 | int pid; |
| 527 | |
| 528 | try { |
| 529 | parsedArgs = new ZygoteConnection.Arguments(args); |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 530 | ZygoteConnection.applyDebuggerSystemProperty(parsedArgs); |
| 531 | ZygoteConnection.applyInvokeWithSystemProperty(parsedArgs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 532 | |
| 533 | /* Request to fork the system server process */ |
| 534 | pid = Zygote.forkSystemServer( |
| 535 | parsedArgs.uid, parsedArgs.gid, |
Elliott Hughes | e1dfcb7 | 2011-07-08 11:08:07 -0700 | [diff] [blame] | 536 | parsedArgs.gids, |
| 537 | parsedArgs.debugFlags, |
| 538 | null, |
Andy McFadden | 1b4c796 | 2010-10-27 11:26:05 -0700 | [diff] [blame] | 539 | parsedArgs.permittedCapabilities, |
| 540 | parsedArgs.effectiveCapabilities); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 541 | } catch (IllegalArgumentException ex) { |
| 542 | throw new RuntimeException(ex); |
Bob Lee | e540833 | 2009-09-04 18:31:17 -0700 | [diff] [blame] | 543 | } |
| 544 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 545 | /* For child process */ |
| 546 | if (pid == 0) { |
| 547 | handleSystemServerProcess(parsedArgs); |
| 548 | } |
| 549 | |
| 550 | return true; |
| 551 | } |
| 552 | |
Alex Klyubin | 48a06e7 | 2013-04-19 10:01:42 -0700 | [diff] [blame] | 553 | /** |
| 554 | * Gets the bit array representation of the provided list of POSIX capabilities. |
| 555 | */ |
| 556 | private static long posixCapabilitiesAsBits(int... capabilities) { |
| 557 | long result = 0; |
| 558 | for (int capability : capabilities) { |
| 559 | if ((capability < 0) || (capability > OsConstants.CAP_LAST_CAP)) { |
| 560 | throw new IllegalArgumentException(String.valueOf(capability)); |
| 561 | } |
| 562 | result |= (1L << capability); |
| 563 | } |
| 564 | return result; |
| 565 | } |
| 566 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 567 | public static void main(String argv[]) { |
| 568 | try { |
Bob Lee | e540833 | 2009-09-04 18:31:17 -0700 | [diff] [blame] | 569 | // Start profiling the zygote initialization. |
| 570 | SamplingProfilerIntegration.start(); |
| 571 | |
Narayan Kamath | c41638c | 2014-04-07 13:56:15 +0100 | [diff] [blame] | 572 | boolean startSystemServer = false; |
| 573 | String socketName = "zygote"; |
| 574 | String abiList = null; |
| 575 | for (int i = 1; i < argv.length; i++) { |
| 576 | if ("start-system-server".equals(argv[i])) { |
| 577 | startSystemServer = true; |
| 578 | } else if (argv[i].startsWith(ABI_LIST_ARG)) { |
| 579 | abiList = argv[i].substring(ABI_LIST_ARG.length()); |
| 580 | } else if (argv[i].startsWith(SOCKET_NAME_ARG)) { |
| 581 | socketName = argv[i].substring(SOCKET_NAME_ARG.length()); |
| 582 | } else { |
| 583 | throw new RuntimeException("Unknown command line argument: " + argv[i]); |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | if (abiList == null) { |
| 588 | throw new RuntimeException("No ABI list supplied."); |
| 589 | } |
| 590 | |
| 591 | registerZygoteSocket(socketName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 592 | EventLog.writeEvent(LOG_BOOT_PROGRESS_PRELOAD_START, |
| 593 | SystemClock.uptimeMillis()); |
Jeff Brown | 0c2313d | 2011-06-10 22:46:40 -0700 | [diff] [blame] | 594 | preload(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 595 | EventLog.writeEvent(LOG_BOOT_PROGRESS_PRELOAD_END, |
| 596 | SystemClock.uptimeMillis()); |
| 597 | |
Brian Carlstrom | 1751086 | 2010-08-18 14:27:40 -0700 | [diff] [blame] | 598 | // Finish profiling the zygote initialization. |
| 599 | SamplingProfilerIntegration.writeZygoteSnapshot(); |
Bob Lee | e540833 | 2009-09-04 18:31:17 -0700 | [diff] [blame] | 600 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 601 | // Do an initial gc to clean up after startup |
| 602 | gc(); |
| 603 | |
Jamie Gennis | 6ad0452 | 2013-04-15 18:53:24 -0700 | [diff] [blame] | 604 | // Disable tracing so that forked processes do not inherit stale tracing tags from |
| 605 | // Zygote. |
| 606 | Trace.setTracingEnabled(false); |
| 607 | |
Narayan Kamath | c41638c | 2014-04-07 13:56:15 +0100 | [diff] [blame] | 608 | if (startSystemServer) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 609 | startSystemServer(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | Log.i(TAG, "Accepting command socket connections"); |
Narayan Kamath | c41638c | 2014-04-07 13:56:15 +0100 | [diff] [blame] | 613 | runSelectLoop(abiList); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 614 | |
| 615 | closeServerSocket(); |
| 616 | } catch (MethodAndArgsCaller caller) { |
| 617 | caller.run(); |
| 618 | } catch (RuntimeException ex) { |
| 619 | Log.e(TAG, "Zygote died with exception", ex); |
| 620 | closeServerSocket(); |
| 621 | throw ex; |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 626 | * Runs the zygote process's select loop. Accepts new connections as |
| 627 | * they happen, and reads commands from connections one spawn-request's |
| 628 | * worth at a time. |
| 629 | * |
| 630 | * @throws MethodAndArgsCaller in a child process when a main() should |
| 631 | * be executed. |
| 632 | */ |
Narayan Kamath | c41638c | 2014-04-07 13:56:15 +0100 | [diff] [blame] | 633 | private static void runSelectLoop(String abiList) throws MethodAndArgsCaller { |
Nick Kralevich | cae3d9f | 2013-01-30 09:51:40 -0800 | [diff] [blame] | 634 | ArrayList<FileDescriptor> fds = new ArrayList<FileDescriptor>(); |
| 635 | ArrayList<ZygoteConnection> peers = new ArrayList<ZygoteConnection>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 636 | FileDescriptor[] fdArray = new FileDescriptor[4]; |
| 637 | |
| 638 | fds.add(sServerSocket.getFileDescriptor()); |
| 639 | peers.add(null); |
| 640 | |
| 641 | int loopCount = GC_LOOP_COUNT; |
| 642 | while (true) { |
| 643 | int index; |
| 644 | |
| 645 | /* |
| 646 | * Call gc() before we block in select(). |
| 647 | * It's work that has to be done anyway, and it's better |
| 648 | * to avoid making every child do it. It will also |
| 649 | * madvise() any free memory as a side-effect. |
| 650 | * |
| 651 | * Don't call it every time, because walking the entire |
| 652 | * heap is a lot of overhead to free a few hundred bytes. |
| 653 | */ |
| 654 | if (loopCount <= 0) { |
| 655 | gc(); |
| 656 | loopCount = GC_LOOP_COUNT; |
| 657 | } else { |
| 658 | loopCount--; |
| 659 | } |
| 660 | |
| 661 | |
| 662 | try { |
| 663 | fdArray = fds.toArray(fdArray); |
| 664 | index = selectReadable(fdArray); |
| 665 | } catch (IOException ex) { |
| 666 | throw new RuntimeException("Error in select()", ex); |
| 667 | } |
| 668 | |
| 669 | if (index < 0) { |
| 670 | throw new RuntimeException("Error in select()"); |
| 671 | } else if (index == 0) { |
Narayan Kamath | c41638c | 2014-04-07 13:56:15 +0100 | [diff] [blame] | 672 | ZygoteConnection newPeer = acceptCommandPeer(abiList); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 673 | peers.add(newPeer); |
| 674 | fds.add(newPeer.getFileDesciptor()); |
| 675 | } else { |
| 676 | boolean done; |
| 677 | done = peers.get(index).runOnce(); |
| 678 | |
| 679 | if (done) { |
| 680 | peers.remove(index); |
| 681 | fds.remove(index); |
| 682 | } |
| 683 | } |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | /** |
| 688 | * The Linux syscall "setreuid()" |
| 689 | * @param ruid real uid |
| 690 | * @param euid effective uid |
| 691 | * @return 0 on success, non-zero errno on fail |
| 692 | */ |
| 693 | static native int setreuid(int ruid, int euid); |
| 694 | |
| 695 | /** |
| 696 | * The Linux syscall "setregid()" |
| 697 | * @param rgid real gid |
| 698 | * @param egid effective gid |
| 699 | * @return 0 on success, non-zero errno on fail |
| 700 | */ |
| 701 | static native int setregid(int rgid, int egid); |
| 702 | |
| 703 | /** |
| 704 | * Invokes the linux syscall "setpgid" |
| 705 | * |
| 706 | * @param pid pid to change |
| 707 | * @param pgid new process group of pid |
| 708 | * @return 0 on success or non-zero errno on fail |
| 709 | */ |
| 710 | static native int setpgid(int pid, int pgid); |
| 711 | |
| 712 | /** |
| 713 | * Invokes the linux syscall "getpgid" |
| 714 | * |
| 715 | * @param pid pid to query |
| 716 | * @return pgid of pid in question |
| 717 | * @throws IOException on error |
| 718 | */ |
| 719 | static native int getpgid(int pid) throws IOException; |
| 720 | |
| 721 | /** |
| 722 | * Invokes the syscall dup2() to copy the specified descriptors into |
| 723 | * stdin, stdout, and stderr. The existing stdio descriptors will be |
| 724 | * closed and errors during close will be ignored. The specified |
| 725 | * descriptors will also remain open at their original descriptor numbers, |
| 726 | * so the caller may want to close the original descriptors. |
| 727 | * |
| 728 | * @param in new stdin |
| 729 | * @param out new stdout |
| 730 | * @param err new stderr |
| 731 | * @throws IOException |
| 732 | */ |
| 733 | static native void reopenStdio(FileDescriptor in, |
| 734 | FileDescriptor out, FileDescriptor err) throws IOException; |
| 735 | |
| 736 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 737 | * Toggles the close-on-exec flag for the specified file descriptor. |
| 738 | * |
| 739 | * @param fd non-null; file descriptor |
| 740 | * @param flag desired close-on-exec flag state |
| 741 | * @throws IOException |
| 742 | */ |
| 743 | static native void setCloseOnExec(FileDescriptor fd, boolean flag) |
| 744 | throws IOException; |
| 745 | |
| 746 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 747 | * Invokes select() on the provider array of file descriptors (selecting |
| 748 | * for readability only). Array elements of null are ignored. |
| 749 | * |
| 750 | * @param fds non-null; array of readable file descriptors |
| 751 | * @return index of descriptor that is now readable or -1 for empty array. |
| 752 | * @throws IOException if an error occurs |
| 753 | */ |
| 754 | static native int selectReadable(FileDescriptor[] fds) throws IOException; |
| 755 | |
| 756 | /** |
| 757 | * Creates a file descriptor from an int fd. |
| 758 | * |
| 759 | * @param fd integer OS file descriptor |
| 760 | * @return non-null; FileDescriptor instance |
| 761 | * @throws IOException if fd is invalid |
| 762 | */ |
| 763 | static native FileDescriptor createFileDescriptor(int fd) |
| 764 | throws IOException; |
| 765 | |
| 766 | /** |
| 767 | * Class not instantiable. |
| 768 | */ |
| 769 | private ZygoteInit() { |
| 770 | } |
| 771 | |
| 772 | /** |
| 773 | * Helper exception class which holds a method and arguments and |
| 774 | * can call them. This is used as part of a trampoline to get rid of |
| 775 | * the initial process setup stack frames. |
| 776 | */ |
| 777 | public static class MethodAndArgsCaller extends Exception |
| 778 | implements Runnable { |
| 779 | /** method to call */ |
| 780 | private final Method mMethod; |
| 781 | |
| 782 | /** argument array */ |
| 783 | private final String[] mArgs; |
| 784 | |
| 785 | public MethodAndArgsCaller(Method method, String[] args) { |
| 786 | mMethod = method; |
| 787 | mArgs = args; |
| 788 | } |
| 789 | |
| 790 | public void run() { |
| 791 | try { |
| 792 | mMethod.invoke(null, new Object[] { mArgs }); |
| 793 | } catch (IllegalAccessException ex) { |
| 794 | throw new RuntimeException(ex); |
| 795 | } catch (InvocationTargetException ex) { |
| 796 | Throwable cause = ex.getCause(); |
| 797 | if (cause instanceof RuntimeException) { |
| 798 | throw (RuntimeException) cause; |
| 799 | } else if (cause instanceof Error) { |
| 800 | throw (Error) cause; |
| 801 | } |
| 802 | throw new RuntimeException(ex); |
| 803 | } |
| 804 | } |
| 805 | } |
| 806 | } |