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 | a9569ff | 2014-04-28 20:53:52 -0700 | [diff] [blame] | 19 | import static android.system.OsConstants.S_IRWXG; |
| 20 | import static android.system.OsConstants.S_IRWXO; |
Kenny Root | 4c74f8c | 2012-08-17 08:45:55 -0700 | [diff] [blame] | 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; |
Neil Fuller | 4f41f61 | 2016-05-09 16:55:36 +0100 | [diff] [blame] | 24 | import android.icu.impl.CacheValue; |
| 25 | import android.icu.text.DecimalFormatSymbols; |
| 26 | import android.icu.util.ULocale; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | import android.net.LocalServerSocket; |
Romain Guy | 74c6912 | 2013-05-08 17:54:20 -0700 | [diff] [blame] | 28 | import android.opengl.EGL14; |
Jeff Sharkey | 740f523 | 2016-12-09 14:31:26 -0700 | [diff] [blame] | 29 | import android.os.IInstalld; |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 30 | import android.os.Process; |
Jeff Sharkey | 740f523 | 2016-12-09 14:31:26 -0700 | [diff] [blame] | 31 | import android.os.RemoteException; |
| 32 | import android.os.ServiceManager; |
| 33 | import android.os.ServiceSpecificException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | import android.os.SystemClock; |
Romain Guy | c5e3638 | 2013-05-09 11:08:17 -0700 | [diff] [blame] | 35 | import android.os.SystemProperties; |
Jamie Gennis | 6ad0452 | 2013-04-15 18:53:24 -0700 | [diff] [blame] | 36 | import android.os.Trace; |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 37 | import android.os.ZygoteProcess; |
Jeff Sharkey | c98c7bc | 2016-12-07 14:57:34 -0700 | [diff] [blame] | 38 | import android.os.storage.StorageManager; |
Sergio Giro | 69de320 | 2016-05-17 16:52:33 +0100 | [diff] [blame] | 39 | import android.security.keystore.AndroidKeyStoreProvider; |
Bill Yi | 9a76e9b | 2014-04-29 18:52:48 -0700 | [diff] [blame] | 40 | import android.system.ErrnoException; |
Elliott Hughes | 860c591 | 2014-04-28 19:19:13 -0700 | [diff] [blame] | 41 | import android.system.Os; |
| 42 | import android.system.OsConstants; |
Raph Levien | c3dd1c1 | 2015-04-06 10:37:57 -0700 | [diff] [blame] | 43 | import android.text.Hyphenator; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | import android.util.EventLog; |
| 45 | import android.util.Log; |
Torne (Richard Coles) | 08cfaf6 | 2014-05-08 16:07:05 +0100 | [diff] [blame] | 46 | import android.webkit.WebViewFactory; |
Andreas Gampe | ddc1397 | 2016-02-26 16:54:59 -0800 | [diff] [blame] | 47 | import android.widget.TextView; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | |
Jeff Sharkey | fdeeeea | 2016-01-11 17:34:24 -0700 | [diff] [blame] | 49 | |
Narayan Kamath | 29564cd | 2014-08-07 10:57:40 +0100 | [diff] [blame] | 50 | import dalvik.system.DexFile; |
| 51 | import dalvik.system.PathClassLoader; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | import dalvik.system.VMRuntime; |
Andreas Gampe | 1ef8aef | 2016-04-11 08:39:52 -0700 | [diff] [blame] | 53 | import dalvik.system.ZygoteHooks; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | |
Brian Carlstrom | 46703b0 | 2011-04-06 15:41:29 -0700 | [diff] [blame] | 55 | import libcore.io.IoUtils; |
| 56 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | import java.io.BufferedReader; |
Ying Wang | d0c4535 | 2014-11-13 15:22:47 -0800 | [diff] [blame] | 58 | import java.io.FileInputStream; |
| 59 | import java.io.FileNotFoundException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | import java.io.IOException; |
| 61 | import java.io.InputStream; |
| 62 | import java.io.InputStreamReader; |
Sergio Giro | 6cb7b1c | 2016-05-13 16:34:46 +0100 | [diff] [blame] | 63 | import java.security.Security; |
| 64 | import java.security.Provider; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | |
| 66 | /** |
| 67 | * Startup class for the zygote process. |
| 68 | * |
| 69 | * 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] | 70 | * 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] | 71 | * the initial state of the VM. |
| 72 | * |
| 73 | * Please see {@link ZygoteConnection.Arguments} for documentation on the |
| 74 | * client protocol. |
| 75 | * |
| 76 | * @hide |
| 77 | */ |
| 78 | public class ZygoteInit { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | private static final String TAG = "Zygote"; |
| 80 | |
Jesse Hall | e8ce127 | 2017-01-17 12:34:14 -0800 | [diff] [blame] | 81 | private static final String PROPERTY_DISABLE_OPENGL_PRELOADING = "ro.zygote.disable_gl_preload"; |
Luis Hector Chavez | 5dd239a | 2015-10-06 14:01:45 -0700 | [diff] [blame] | 82 | private static final String PROPERTY_RUNNING_IN_CONTAINER = "ro.boot.container"; |
Romain Guy | c5e3638 | 2013-05-09 11:08:17 -0700 | [diff] [blame] | 83 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | private static final int LOG_BOOT_PROGRESS_PRELOAD_START = 3020; |
| 85 | private static final int LOG_BOOT_PROGRESS_PRELOAD_END = 3030; |
| 86 | |
| 87 | /** when preloading, GC after allocating this many bytes */ |
| 88 | private static final int PRELOAD_GC_THRESHOLD = 50000; |
| 89 | |
Narayan Kamath | c41638c | 2014-04-07 13:56:15 +0100 | [diff] [blame] | 90 | private static final String ABI_LIST_ARG = "--abi-list="; |
| 91 | |
| 92 | private static final String SOCKET_NAME_ARG = "--socket-name="; |
Barry Hayes | 0b3533a | 2010-01-20 12:46:47 -0800 | [diff] [blame] | 93 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 94 | /** |
Tobias Sargeant | dd4bb31 | 2016-01-19 16:34:54 +0000 | [diff] [blame] | 95 | * Used to pre-load resources. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | */ |
| 97 | private static Resources mResources; |
Bob Lee | e540833 | 2009-09-04 18:31:17 -0700 | [diff] [blame] | 98 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 99 | /** |
Ying Wang | d0c4535 | 2014-11-13 15:22:47 -0800 | [diff] [blame] | 100 | * 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] | 101 | */ |
Ying Wang | d0c4535 | 2014-11-13 15:22:47 -0800 | [diff] [blame] | 102 | 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] | 103 | |
| 104 | /** Controls whether we should preload resources during zygote init. */ |
Steve Paik | 3c27a23 | 2015-06-10 18:25:44 -0700 | [diff] [blame] | 105 | public static final boolean PRELOAD_RESOURCES = true; |
Andy McFadden | 599c918 | 2009-04-08 00:35:56 -0700 | [diff] [blame] | 106 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | private static final int UNPRIVILEGED_UID = 9999; |
| 108 | private static final int UNPRIVILEGED_GID = 9999; |
| 109 | |
| 110 | private static final int ROOT_UID = 0; |
| 111 | private static final int ROOT_GID = 0; |
| 112 | |
Jeff Brown | 0c2313d | 2011-06-10 22:46:40 -0700 | [diff] [blame] | 113 | static void preload() { |
Bill Yi | 9a76e9b | 2014-04-29 18:52:48 -0700 | [diff] [blame] | 114 | Log.d(TAG, "begin preload"); |
Neil Fuller | 4f41f61 | 2016-05-09 16:55:36 +0100 | [diff] [blame] | 115 | Trace.traceBegin(Trace.TRACE_TAG_DALVIK, "BeginIcuCachePinning"); |
| 116 | beginIcuCachePinning(); |
| 117 | Trace.traceEnd(Trace.TRACE_TAG_DALVIK); |
Yasuhiro Matsuda | 25878b2 | 2015-09-03 16:18:21 +0900 | [diff] [blame] | 118 | Trace.traceBegin(Trace.TRACE_TAG_DALVIK, "PreloadClasses"); |
| 119 | preloadClasses(); |
| 120 | Trace.traceEnd(Trace.TRACE_TAG_DALVIK); |
| 121 | Trace.traceBegin(Trace.TRACE_TAG_DALVIK, "PreloadResources"); |
| 122 | preloadResources(); |
| 123 | Trace.traceEnd(Trace.TRACE_TAG_DALVIK); |
Jesse Hall | e8ce127 | 2017-01-17 12:34:14 -0800 | [diff] [blame] | 124 | Trace.traceBegin(Trace.TRACE_TAG_DALVIK, "PreloadOpenGL"); |
| 125 | preloadOpenGL(); |
| 126 | Trace.traceEnd(Trace.TRACE_TAG_DALVIK); |
Brian Carlstrom | 6c9af96 | 2014-09-11 15:36:00 -0700 | [diff] [blame] | 127 | preloadSharedLibraries(); |
Raph Levien | c3dd1c1 | 2015-04-06 10:37:57 -0700 | [diff] [blame] | 128 | preloadTextResources(); |
Torne (Richard Coles) | 08cfaf6 | 2014-05-08 16:07:05 +0100 | [diff] [blame] | 129 | // Ask the WebViewFactory to do any initialization that must run in the zygote process, |
| 130 | // for memory sharing purposes. |
| 131 | WebViewFactory.prepareWebViewInZygote(); |
Neil Fuller | 4f41f61 | 2016-05-09 16:55:36 +0100 | [diff] [blame] | 132 | endIcuCachePinning(); |
Sergio Giro | 6cb7b1c | 2016-05-13 16:34:46 +0100 | [diff] [blame] | 133 | warmUpJcaProviders(); |
Bill Yi | 9a76e9b | 2014-04-29 18:52:48 -0700 | [diff] [blame] | 134 | Log.d(TAG, "end preload"); |
Romain Guy | 74c6912 | 2013-05-08 17:54:20 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Neil Fuller | 4f41f61 | 2016-05-09 16:55:36 +0100 | [diff] [blame] | 137 | private static void beginIcuCachePinning() { |
| 138 | // Pin ICU data in memory from this point that would normally be held by soft references. |
| 139 | // Without this, any references created immediately below or during class preloading |
| 140 | // would be collected when the Zygote GC runs in gcAndFinalize(). |
| 141 | Log.i(TAG, "Installing ICU cache reference pinning..."); |
| 142 | |
| 143 | CacheValue.setStrength(CacheValue.Strength.STRONG); |
| 144 | |
| 145 | Log.i(TAG, "Preloading ICU data..."); |
| 146 | // Explicitly exercise code to cache data apps are likely to need. |
| 147 | ULocale[] localesToPin = { ULocale.ROOT, ULocale.US, ULocale.getDefault() }; |
| 148 | for (ULocale uLocale : localesToPin) { |
| 149 | new DecimalFormatSymbols(uLocale); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | private static void endIcuCachePinning() { |
| 154 | // All cache references created by ICU from this point will be soft. |
| 155 | CacheValue.setStrength(CacheValue.Strength.SOFT); |
| 156 | |
| 157 | Log.i(TAG, "Uninstalled ICU cache reference pinning..."); |
| 158 | } |
| 159 | |
Brian Carlstrom | 6c9af96 | 2014-09-11 15:36:00 -0700 | [diff] [blame] | 160 | private static void preloadSharedLibraries() { |
| 161 | Log.i(TAG, "Preloading shared libraries..."); |
| 162 | System.loadLibrary("android"); |
| 163 | System.loadLibrary("compiler_rt"); |
| 164 | System.loadLibrary("jnigraphics"); |
| 165 | } |
| 166 | |
Jesse Hall | e8ce127 | 2017-01-17 12:34:14 -0800 | [diff] [blame] | 167 | private static void preloadOpenGL() { |
| 168 | if (!SystemProperties.getBoolean(PROPERTY_DISABLE_OPENGL_PRELOADING, false)) { |
| 169 | EGL14.eglGetDisplay(EGL14.EGL_DEFAULT_DISPLAY); |
| 170 | } |
| 171 | } |
| 172 | |
Raph Levien | c3dd1c1 | 2015-04-06 10:37:57 -0700 | [diff] [blame] | 173 | private static void preloadTextResources() { |
| 174 | Hyphenator.init(); |
Andreas Gampe | ddc1397 | 2016-02-26 16:54:59 -0800 | [diff] [blame] | 175 | TextView.preloadFontCache(); |
Raph Levien | c3dd1c1 | 2015-04-06 10:37:57 -0700 | [diff] [blame] | 176 | } |
| 177 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 178 | /** |
Sergio Giro | 69de320 | 2016-05-17 16:52:33 +0100 | [diff] [blame] | 179 | * Register AndroidKeyStoreProvider and warm up the providers that are already registered. |
Sergio Giro | 6cb7b1c | 2016-05-13 16:34:46 +0100 | [diff] [blame] | 180 | * |
| 181 | * By doing it here we avoid that each app does it when requesting a service from the |
| 182 | * provider for the first time. |
| 183 | */ |
| 184 | private static void warmUpJcaProviders() { |
| 185 | long startTime = SystemClock.uptimeMillis(); |
| 186 | Trace.traceBegin( |
Sergio Giro | 69de320 | 2016-05-17 16:52:33 +0100 | [diff] [blame] | 187 | Trace.TRACE_TAG_DALVIK, "Starting installation of AndroidKeyStoreProvider"); |
| 188 | // AndroidKeyStoreProvider.install() manipulates the list of JCA providers to insert |
| 189 | // preferred providers. Note this is not done via security.properties as the JCA providers |
| 190 | // are not on the classpath in the case of, for example, raw dalvikvm runtimes. |
| 191 | AndroidKeyStoreProvider.install(); |
| 192 | Log.i(TAG, "Installed AndroidKeyStoreProvider in " |
| 193 | + (SystemClock.uptimeMillis() - startTime) + "ms."); |
| 194 | Trace.traceEnd(Trace.TRACE_TAG_DALVIK); |
| 195 | |
| 196 | startTime = SystemClock.uptimeMillis(); |
| 197 | Trace.traceBegin( |
Sergio Giro | 6cb7b1c | 2016-05-13 16:34:46 +0100 | [diff] [blame] | 198 | Trace.TRACE_TAG_DALVIK, "Starting warm up of JCA providers"); |
| 199 | for (Provider p : Security.getProviders()) { |
| 200 | p.warmUpServiceProvision(); |
| 201 | } |
| 202 | Log.i(TAG, "Warmed up JCA providers in " |
Sergio Giro | 69de320 | 2016-05-17 16:52:33 +0100 | [diff] [blame] | 203 | + (SystemClock.uptimeMillis() - startTime) + "ms."); |
Sergio Giro | 6cb7b1c | 2016-05-13 16:34:46 +0100 | [diff] [blame] | 204 | Trace.traceEnd(Trace.TRACE_TAG_DALVIK); |
| 205 | } |
| 206 | |
| 207 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 208 | * Performs Zygote process initialization. Loads and initializes |
| 209 | * commonly used classes. |
| 210 | * |
| 211 | * Most classes only cause a few hundred bytes to be allocated, but |
| 212 | * a few will allocate a dozen Kbytes (in one case, 500+K). |
| 213 | */ |
| 214 | private static void preloadClasses() { |
| 215 | final VMRuntime runtime = VMRuntime.getRuntime(); |
Bob Lee | e540833 | 2009-09-04 18:31:17 -0700 | [diff] [blame] | 216 | |
Ying Wang | d0c4535 | 2014-11-13 15:22:47 -0800 | [diff] [blame] | 217 | InputStream is; |
| 218 | try { |
| 219 | is = new FileInputStream(PRELOADED_CLASSES); |
| 220 | } catch (FileNotFoundException e) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 221 | Log.e(TAG, "Couldn't find " + PRELOADED_CLASSES + "."); |
Ying Wang | d0c4535 | 2014-11-13 15:22:47 -0800 | [diff] [blame] | 222 | return; |
| 223 | } |
Bob Lee | e540833 | 2009-09-04 18:31:17 -0700 | [diff] [blame] | 224 | |
Ying Wang | d0c4535 | 2014-11-13 15:22:47 -0800 | [diff] [blame] | 225 | Log.i(TAG, "Preloading classes..."); |
| 226 | long startTime = SystemClock.uptimeMillis(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 227 | |
Ying Wang | d0c4535 | 2014-11-13 15:22:47 -0800 | [diff] [blame] | 228 | // Drop root perms while running static initializers. |
Narayan Kamath | 23e6878 | 2015-01-16 17:22:41 +0000 | [diff] [blame] | 229 | final int reuid = Os.getuid(); |
| 230 | final int regid = Os.getgid(); |
| 231 | |
| 232 | // We need to drop root perms only if we're already root. In the case of "wrapped" |
| 233 | // processes (see WrapperInit), this function is called from an unprivileged uid |
| 234 | // and gid. |
| 235 | boolean droppedPriviliges = false; |
| 236 | if (reuid == ROOT_UID && regid == ROOT_GID) { |
| 237 | try { |
| 238 | Os.setregid(ROOT_GID, UNPRIVILEGED_GID); |
| 239 | Os.setreuid(ROOT_UID, UNPRIVILEGED_UID); |
| 240 | } catch (ErrnoException ex) { |
| 241 | throw new RuntimeException("Failed to drop root", ex); |
| 242 | } |
| 243 | |
| 244 | droppedPriviliges = true; |
Elliott Hughes | 26b56e6 | 2014-12-17 12:28:29 -0800 | [diff] [blame] | 245 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 246 | |
Ying Wang | d0c4535 | 2014-11-13 15:22:47 -0800 | [diff] [blame] | 247 | // Alter the target heap utilization. With explicit GCs this |
| 248 | // is not likely to have any effect. |
| 249 | float defaultUtilization = runtime.getTargetHeapUtilization(); |
| 250 | runtime.setTargetHeapUtilization(0.8f); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 251 | |
Ying Wang | d0c4535 | 2014-11-13 15:22:47 -0800 | [diff] [blame] | 252 | try { |
| 253 | BufferedReader br |
| 254 | = new BufferedReader(new InputStreamReader(is), 256); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 255 | |
Ying Wang | d0c4535 | 2014-11-13 15:22:47 -0800 | [diff] [blame] | 256 | int count = 0; |
| 257 | String line; |
| 258 | while ((line = br.readLine()) != null) { |
| 259 | // Skip comments and blank lines. |
| 260 | line = line.trim(); |
| 261 | if (line.startsWith("#") || line.equals("")) { |
| 262 | continue; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 263 | } |
| 264 | |
Andreas Gampe | c425362 | 2016-10-28 18:19:30 -0700 | [diff] [blame] | 265 | Trace.traceBegin(Trace.TRACE_TAG_DALVIK, line); |
Ying Wang | d0c4535 | 2014-11-13 15:22:47 -0800 | [diff] [blame] | 266 | try { |
| 267 | if (false) { |
| 268 | Log.v(TAG, "Preloading " + line + "..."); |
| 269 | } |
Andreas Gampe | dd8e5fb | 2015-04-21 09:01:51 -0700 | [diff] [blame] | 270 | // Load and explicitly initialize the given class. Use |
| 271 | // Class.forName(String, boolean, ClassLoader) to avoid repeated stack lookups |
| 272 | // (to derive the caller's class-loader). Use true to force initialization, and |
| 273 | // null for the boot classpath class-loader (could as well cache the |
| 274 | // class-loader of this class in a variable). |
Andreas Gampe | c917f74 | 2015-04-20 19:16:37 -0700 | [diff] [blame] | 275 | Class.forName(line, true, null); |
Ying Wang | d0c4535 | 2014-11-13 15:22:47 -0800 | [diff] [blame] | 276 | count++; |
| 277 | } catch (ClassNotFoundException e) { |
| 278 | Log.w(TAG, "Class not found for preloading: " + line); |
| 279 | } catch (UnsatisfiedLinkError e) { |
| 280 | Log.w(TAG, "Problem preloading " + line + ": " + e); |
| 281 | } catch (Throwable t) { |
| 282 | Log.e(TAG, "Error preloading " + line + ".", t); |
| 283 | if (t instanceof Error) { |
| 284 | throw (Error) t; |
| 285 | } |
| 286 | if (t instanceof RuntimeException) { |
| 287 | throw (RuntimeException) t; |
| 288 | } |
| 289 | throw new RuntimeException(t); |
| 290 | } |
Yasuhiro Matsuda | 25878b2 | 2015-09-03 16:18:21 +0900 | [diff] [blame] | 291 | Trace.traceEnd(Trace.TRACE_TAG_DALVIK); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 292 | } |
Ying Wang | d0c4535 | 2014-11-13 15:22:47 -0800 | [diff] [blame] | 293 | |
| 294 | Log.i(TAG, "...preloaded " + count + " classes in " |
| 295 | + (SystemClock.uptimeMillis()-startTime) + "ms."); |
| 296 | } catch (IOException e) { |
| 297 | Log.e(TAG, "Error reading " + PRELOADED_CLASSES + ".", e); |
| 298 | } finally { |
| 299 | IoUtils.closeQuietly(is); |
| 300 | // Restore default. |
| 301 | runtime.setTargetHeapUtilization(defaultUtilization); |
| 302 | |
| 303 | // Fill in dex caches with classes, fields, and methods brought in by preloading. |
Yasuhiro Matsuda | 1ab43d5 | 2015-06-30 17:07:32 +0900 | [diff] [blame] | 304 | Trace.traceBegin(Trace.TRACE_TAG_DALVIK, "PreloadDexCaches"); |
Ying Wang | d0c4535 | 2014-11-13 15:22:47 -0800 | [diff] [blame] | 305 | runtime.preloadDexCaches(); |
Yasuhiro Matsuda | 1ab43d5 | 2015-06-30 17:07:32 +0900 | [diff] [blame] | 306 | Trace.traceEnd(Trace.TRACE_TAG_DALVIK); |
Ying Wang | d0c4535 | 2014-11-13 15:22:47 -0800 | [diff] [blame] | 307 | |
Narayan Kamath | 23e6878 | 2015-01-16 17:22:41 +0000 | [diff] [blame] | 308 | // Bring back root. We'll need it later if we're in the zygote. |
| 309 | if (droppedPriviliges) { |
| 310 | try { |
| 311 | Os.setreuid(ROOT_UID, ROOT_UID); |
| 312 | Os.setregid(ROOT_GID, ROOT_GID); |
| 313 | } catch (ErrnoException ex) { |
| 314 | throw new RuntimeException("Failed to restore root", ex); |
| 315 | } |
Elliott Hughes | 26b56e6 | 2014-12-17 12:28:29 -0800 | [diff] [blame] | 316 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 317 | } |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * Load in commonly used resources, so they can be shared across |
| 322 | * processes. |
| 323 | * |
| 324 | * These tend to be a few Kbytes, but are frequently in the 20-40K |
| 325 | * range, and occasionally even larger. |
| 326 | */ |
| 327 | private static void preloadResources() { |
| 328 | final VMRuntime runtime = VMRuntime.getRuntime(); |
Bob Lee | e540833 | 2009-09-04 18:31:17 -0700 | [diff] [blame] | 329 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 330 | try { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 331 | mResources = Resources.getSystem(); |
| 332 | mResources.startPreloading(); |
| 333 | if (PRELOAD_RESOURCES) { |
| 334 | Log.i(TAG, "Preloading resources..."); |
| 335 | |
| 336 | long startTime = SystemClock.uptimeMillis(); |
| 337 | TypedArray ar = mResources.obtainTypedArray( |
| 338 | com.android.internal.R.array.preloaded_drawables); |
Filip Gruszczynski | 811dc3b | 2015-11-23 12:34:22 -0800 | [diff] [blame] | 339 | int N = preloadDrawables(ar); |
Jeff Brown | 14577c4 | 2012-03-08 16:40:14 -0800 | [diff] [blame] | 340 | ar.recycle(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 341 | Log.i(TAG, "...preloaded " + N + " resources in " |
| 342 | + (SystemClock.uptimeMillis()-startTime) + "ms."); |
| 343 | |
| 344 | startTime = SystemClock.uptimeMillis(); |
| 345 | ar = mResources.obtainTypedArray( |
| 346 | com.android.internal.R.array.preloaded_color_state_lists); |
Filip Gruszczynski | 811dc3b | 2015-11-23 12:34:22 -0800 | [diff] [blame] | 347 | N = preloadColorStateLists(ar); |
Jeff Brown | 14577c4 | 2012-03-08 16:40:14 -0800 | [diff] [blame] | 348 | ar.recycle(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 349 | Log.i(TAG, "...preloaded " + N + " resources in " |
| 350 | + (SystemClock.uptimeMillis()-startTime) + "ms."); |
Filip Gruszczynski | 811dc3b | 2015-11-23 12:34:22 -0800 | [diff] [blame] | 351 | |
| 352 | if (mResources.getBoolean( |
| 353 | com.android.internal.R.bool.config_freeformWindowManagement)) { |
| 354 | startTime = SystemClock.uptimeMillis(); |
| 355 | ar = mResources.obtainTypedArray( |
| 356 | com.android.internal.R.array.preloaded_freeform_multi_window_drawables); |
| 357 | N = preloadDrawables(ar); |
| 358 | ar.recycle(); |
| 359 | Log.i(TAG, "...preloaded " + N + " resource in " |
| 360 | + (SystemClock.uptimeMillis() - startTime) + "ms."); |
| 361 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 362 | } |
| 363 | mResources.finishPreloading(); |
| 364 | } catch (RuntimeException e) { |
| 365 | Log.w(TAG, "Failure preloading resources", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 366 | } |
| 367 | } |
| 368 | |
Filip Gruszczynski | 811dc3b | 2015-11-23 12:34:22 -0800 | [diff] [blame] | 369 | private static int preloadColorStateLists(TypedArray ar) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 370 | int N = ar.length(); |
| 371 | for (int i=0; i<N; i++) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 372 | int id = ar.getResourceId(i, 0); |
Joe Onorato | 43a1765 | 2011-04-06 19:22:23 -0700 | [diff] [blame] | 373 | if (false) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 374 | Log.v(TAG, "Preloading resource #" + Integer.toHexString(id)); |
| 375 | } |
| 376 | if (id != 0) { |
Alan Viverette | 77bb6f1 | 2015-02-11 17:24:33 -0800 | [diff] [blame] | 377 | if (mResources.getColorStateList(id, null) == null) { |
Dianne Hackborn | dde331c | 2012-08-03 14:01:57 -0700 | [diff] [blame] | 378 | throw new IllegalArgumentException( |
| 379 | "Unable to find preloaded color resource #0x" |
| 380 | + Integer.toHexString(id) |
| 381 | + " (" + ar.getString(i) + ")"); |
| 382 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 383 | } |
| 384 | } |
| 385 | return N; |
| 386 | } |
| 387 | |
| 388 | |
Filip Gruszczynski | 811dc3b | 2015-11-23 12:34:22 -0800 | [diff] [blame] | 389 | private static int preloadDrawables(TypedArray ar) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 390 | int N = ar.length(); |
| 391 | for (int i=0; i<N; i++) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 392 | int id = ar.getResourceId(i, 0); |
Joe Onorato | 43a1765 | 2011-04-06 19:22:23 -0700 | [diff] [blame] | 393 | if (false) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 394 | Log.v(TAG, "Preloading resource #" + Integer.toHexString(id)); |
| 395 | } |
| 396 | if (id != 0) { |
Alan Viverette | 84e001a | 2014-09-22 00:04:22 -0700 | [diff] [blame] | 397 | if (mResources.getDrawable(id, null) == null) { |
Dianne Hackborn | dde331c | 2012-08-03 14:01:57 -0700 | [diff] [blame] | 398 | throw new IllegalArgumentException( |
| 399 | "Unable to find preloaded drawable resource #0x" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 400 | + Integer.toHexString(id) |
Dianne Hackborn | dde331c | 2012-08-03 14:01:57 -0700 | [diff] [blame] | 401 | + " (" + ar.getString(i) + ")"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 402 | } |
| 403 | } |
| 404 | } |
| 405 | return N; |
| 406 | } |
| 407 | |
| 408 | /** |
| 409 | * Runs several special GCs to try to clean up a few generations of |
| 410 | * softly- and final-reachable objects, along with any other garbage. |
| 411 | * This is only useful just before a fork(). |
| 412 | */ |
Mathieu Chartier | 9a88f10 | 2014-08-20 10:24:11 -0700 | [diff] [blame] | 413 | /*package*/ static void gcAndFinalize() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 414 | final VMRuntime runtime = VMRuntime.getRuntime(); |
| 415 | |
| 416 | /* runFinalizationSync() lets finalizers be called in Zygote, |
| 417 | * which doesn't have a HeapWorker thread. |
| 418 | */ |
Brian Carlstrom | 08065b9 | 2011-04-01 15:49:41 -0700 | [diff] [blame] | 419 | System.gc(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 420 | runtime.runFinalizationSync(); |
Brian Carlstrom | 08065b9 | 2011-04-01 15:49:41 -0700 | [diff] [blame] | 421 | System.gc(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | /** |
| 425 | * Finish remaining work for the newly forked system server process. |
| 426 | */ |
| 427 | private static void handleSystemServerProcess( |
| 428 | ZygoteConnection.Arguments parsedArgs) |
Tobias Sargeant | dd4bb31 | 2016-01-19 16:34:54 +0000 | [diff] [blame] | 429 | throws Zygote.MethodAndArgsCaller { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 430 | |
Mike Lockwood | 90960e8 | 2010-08-06 09:15:25 -0400 | [diff] [blame] | 431 | // 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] | 432 | Os.umask(S_IRWXG | S_IRWXO); |
Mike Lockwood | 90960e8 | 2010-08-06 09:15:25 -0400 | [diff] [blame] | 433 | |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 434 | if (parsedArgs.niceName != null) { |
| 435 | Process.setArgV0(parsedArgs.niceName); |
| 436 | } |
| 437 | |
Narayan Kamath | 29564cd | 2014-08-07 10:57:40 +0100 | [diff] [blame] | 438 | final String systemServerClasspath = Os.getenv("SYSTEMSERVERCLASSPATH"); |
| 439 | if (systemServerClasspath != null) { |
| 440 | performSystemServerDexOpt(systemServerClasspath); |
| 441 | } |
| 442 | |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 443 | if (parsedArgs.invokeWith != null) { |
Narayan Kamath | 29564cd | 2014-08-07 10:57:40 +0100 | [diff] [blame] | 444 | String[] args = parsedArgs.remainingArgs; |
| 445 | // If we have a non-null system server class path, we'll have to duplicate the |
| 446 | // existing arguments and append the classpath to it. ART will handle the classpath |
| 447 | // correctly when we exec a new process. |
| 448 | if (systemServerClasspath != null) { |
| 449 | String[] amendedArgs = new String[args.length + 2]; |
| 450 | amendedArgs[0] = "-cp"; |
| 451 | amendedArgs[1] = systemServerClasspath; |
| 452 | System.arraycopy(parsedArgs.remainingArgs, 0, amendedArgs, 2, parsedArgs.remainingArgs.length); |
| 453 | } |
| 454 | |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 455 | WrapperInit.execApplication(parsedArgs.invokeWith, |
Elliott Hughes | e1dfcb7 | 2011-07-08 11:08:07 -0700 | [diff] [blame] | 456 | parsedArgs.niceName, parsedArgs.targetSdkVersion, |
Narayan Kamath | 37ad4b0 | 2015-01-19 16:05:24 +0000 | [diff] [blame] | 457 | VMRuntime.getCurrentInstructionSet(), null, args); |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 458 | } else { |
Narayan Kamath | 29564cd | 2014-08-07 10:57:40 +0100 | [diff] [blame] | 459 | ClassLoader cl = null; |
| 460 | if (systemServerClasspath != null) { |
Dimitry Ivanov | 89dad33 | 2016-04-20 16:02:00 -0700 | [diff] [blame] | 461 | cl = createSystemServerClassLoader(systemServerClasspath, |
| 462 | parsedArgs.targetSdkVersion); |
| 463 | |
Narayan Kamath | 29564cd | 2014-08-07 10:57:40 +0100 | [diff] [blame] | 464 | Thread.currentThread().setContextClassLoader(cl); |
| 465 | } |
| 466 | |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 467 | /* |
| 468 | * Pass the remaining arguments to SystemServer. |
| 469 | */ |
Narayan Kamath | 29564cd | 2014-08-07 10:57:40 +0100 | [diff] [blame] | 470 | RuntimeInit.zygoteInit(parsedArgs.targetSdkVersion, parsedArgs.remainingArgs, cl); |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 471 | } |
| 472 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 473 | /* should never reach here */ |
| 474 | } |
| 475 | |
| 476 | /** |
Dimitry Ivanov | 89dad33 | 2016-04-20 16:02:00 -0700 | [diff] [blame] | 477 | * Creates a PathClassLoader for the system server. It also creates |
| 478 | * a shared namespace associated with the classloader to let it access |
| 479 | * platform-private native libraries. |
| 480 | */ |
| 481 | private static PathClassLoader createSystemServerClassLoader(String systemServerClasspath, |
| 482 | int targetSdkVersion) { |
Dimitry Ivanov | 9e9061f | 2016-09-20 10:50:40 -0700 | [diff] [blame] | 483 | String libraryPath = System.getProperty("java.library.path"); |
Dimitry Ivanov | 89dad33 | 2016-04-20 16:02:00 -0700 | [diff] [blame] | 484 | |
| 485 | return PathClassLoaderFactory.createClassLoader(systemServerClasspath, |
Dimitry Ivanov | 9e9061f | 2016-09-20 10:50:40 -0700 | [diff] [blame] | 486 | libraryPath, |
| 487 | libraryPath, |
Dimitry Ivanov | 89dad33 | 2016-04-20 16:02:00 -0700 | [diff] [blame] | 488 | ClassLoader.getSystemClassLoader(), |
| 489 | targetSdkVersion, |
| 490 | true /* isNamespaceShared */); |
| 491 | } |
| 492 | |
| 493 | /** |
Narayan Kamath | 29564cd | 2014-08-07 10:57:40 +0100 | [diff] [blame] | 494 | * Performs dex-opt on the elements of {@code classPath}, if needed. We |
| 495 | * choose the instruction set of the current runtime. |
| 496 | */ |
| 497 | private static void performSystemServerDexOpt(String classPath) { |
| 498 | final String[] classPathElements = classPath.split(":"); |
Jeff Sharkey | 740f523 | 2016-12-09 14:31:26 -0700 | [diff] [blame] | 499 | final IInstalld installd = IInstalld.Stub |
| 500 | .asInterface(ServiceManager.getService("installd")); |
Narayan Kamath | 29564cd | 2014-08-07 10:57:40 +0100 | [diff] [blame] | 501 | final String instructionSet = VMRuntime.getRuntime().vmInstructionSet(); |
| 502 | |
Jeff Sharkey | 740f523 | 2016-12-09 14:31:26 -0700 | [diff] [blame] | 503 | String sharedLibraries = ""; |
| 504 | for (String classPathElement : classPathElements) { |
| 505 | // System server is fully AOTed and never profiled |
| 506 | // for profile guided compilation. |
| 507 | // TODO: Make this configurable between INTERPRET_ONLY, SPEED, SPACE and EVERYTHING? |
Andreas Gampe | e7bc152 | 2016-08-09 20:44:04 -0700 | [diff] [blame] | 508 | |
Jeff Sharkey | 740f523 | 2016-12-09 14:31:26 -0700 | [diff] [blame] | 509 | int dexoptNeeded; |
| 510 | try { |
| 511 | dexoptNeeded = DexFile.getDexOptNeeded( |
| 512 | classPathElement, instructionSet, "speed", |
| 513 | false /* newProfile */); |
| 514 | } catch (FileNotFoundException ignored) { |
| 515 | // Do not add to the classpath. |
| 516 | Log.w(TAG, "Missing classpath element for system server: " + classPathElement); |
| 517 | continue; |
| 518 | } catch (IOException e) { |
| 519 | // Not fully clear what to do here as we don't know the cause of the |
| 520 | // IO exception. Add to the classpath to be conservative, but don't |
| 521 | // attempt to compile it. |
| 522 | Log.w(TAG, "Error checking classpath element for system server: " |
| 523 | + classPathElement, e); |
| 524 | dexoptNeeded = DexFile.NO_DEXOPT_NEEDED; |
Narayan Kamath | 29564cd | 2014-08-07 10:57:40 +0100 | [diff] [blame] | 525 | } |
Jeff Sharkey | 740f523 | 2016-12-09 14:31:26 -0700 | [diff] [blame] | 526 | |
| 527 | if (dexoptNeeded != DexFile.NO_DEXOPT_NEEDED) { |
| 528 | final String packageName = "*"; |
| 529 | final String outputPath = null; |
| 530 | final int dexFlags = 0; |
| 531 | final String compilerFilter = "speed"; |
| 532 | final String uuid = StorageManager.UUID_PRIVATE_INTERNAL; |
| 533 | try { |
| 534 | installd.dexopt(classPathElement, Process.SYSTEM_UID, packageName, |
| 535 | instructionSet, dexoptNeeded, outputPath, dexFlags, compilerFilter, |
| 536 | uuid, sharedLibraries); |
| 537 | } catch (RemoteException | ServiceSpecificException e) { |
| 538 | // Ignore (but log), we need this on the classpath for fallback mode. |
| 539 | Log.w(TAG, "Failed compiling classpath element for system server: " |
| 540 | + classPathElement, e); |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | if (!sharedLibraries.isEmpty()) { |
| 545 | sharedLibraries += ":"; |
| 546 | } |
| 547 | sharedLibraries += classPathElement; |
Narayan Kamath | 29564cd | 2014-08-07 10:57:40 +0100 | [diff] [blame] | 548 | } |
| 549 | } |
| 550 | |
| 551 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 552 | * Prepare the arguments and fork for the system server process. |
| 553 | */ |
Tobias Sargeant | dd4bb31 | 2016-01-19 16:34:54 +0000 | [diff] [blame] | 554 | private static boolean startSystemServer(String abiList, String socketName, ZygoteServer zygoteServer) |
| 555 | throws Zygote.MethodAndArgsCaller, RuntimeException { |
Alex Klyubin | 48a06e7 | 2013-04-19 10:01:42 -0700 | [diff] [blame] | 556 | long capabilities = posixCapabilitiesAsBits( |
Philip Cuadra | 7bd0fdd | 2016-04-28 15:26:49 -0700 | [diff] [blame] | 557 | OsConstants.CAP_IPC_LOCK, |
Alex Klyubin | 48a06e7 | 2013-04-19 10:01:42 -0700 | [diff] [blame] | 558 | OsConstants.CAP_KILL, |
| 559 | OsConstants.CAP_NET_ADMIN, |
| 560 | OsConstants.CAP_NET_BIND_SERVICE, |
| 561 | OsConstants.CAP_NET_BROADCAST, |
| 562 | OsConstants.CAP_NET_RAW, |
Alex Klyubin | 48a06e7 | 2013-04-19 10:01:42 -0700 | [diff] [blame] | 563 | OsConstants.CAP_SYS_MODULE, |
| 564 | OsConstants.CAP_SYS_NICE, |
| 565 | OsConstants.CAP_SYS_RESOURCE, |
| 566 | OsConstants.CAP_SYS_TIME, |
John Stultz | 5733f38 | 2016-07-28 12:35:31 -0700 | [diff] [blame] | 567 | OsConstants.CAP_SYS_TTY_CONFIG, |
| 568 | OsConstants.CAP_WAKE_ALARM |
Alex Klyubin | 48a06e7 | 2013-04-19 10:01:42 -0700 | [diff] [blame] | 569 | ); |
Luis Hector Chavez | 5dd239a | 2015-10-06 14:01:45 -0700 | [diff] [blame] | 570 | /* Containers run without this capability, so avoid setting it in that case */ |
| 571 | if (!SystemProperties.getBoolean(PROPERTY_RUNNING_IN_CONTAINER, false)) { |
| 572 | capabilities |= posixCapabilitiesAsBits(OsConstants.CAP_BLOCK_SUSPEND); |
| 573 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 574 | /* Hardcoded command line to start the system server */ |
| 575 | String args[] = { |
| 576 | "--setuid=1000", |
| 577 | "--setgid=1000", |
Pavlin Radoslavov | 7016625 | 2015-11-23 17:13:25 -0800 | [diff] [blame] | 578 | "--setgroups=1001,1002,1003,1004,1005,1006,1007,1008,1009,1010,1018,1021,1032,3001,3002,3003,3006,3007,3009,3010", |
Alex Klyubin | 48a06e7 | 2013-04-19 10:01:42 -0700 | [diff] [blame] | 579 | "--capabilities=" + capabilities + "," + capabilities, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 580 | "--nice-name=system_server", |
Narayan Kamath | b6b044a | 2015-02-13 17:31:25 +0000 | [diff] [blame] | 581 | "--runtime-args", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 582 | "com.android.server.SystemServer", |
| 583 | }; |
| 584 | ZygoteConnection.Arguments parsedArgs = null; |
| 585 | |
| 586 | int pid; |
| 587 | |
| 588 | try { |
| 589 | parsedArgs = new ZygoteConnection.Arguments(args); |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 590 | ZygoteConnection.applyDebuggerSystemProperty(parsedArgs); |
| 591 | ZygoteConnection.applyInvokeWithSystemProperty(parsedArgs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 592 | |
| 593 | /* Request to fork the system server process */ |
| 594 | pid = Zygote.forkSystemServer( |
| 595 | parsedArgs.uid, parsedArgs.gid, |
Elliott Hughes | e1dfcb7 | 2011-07-08 11:08:07 -0700 | [diff] [blame] | 596 | parsedArgs.gids, |
| 597 | parsedArgs.debugFlags, |
| 598 | null, |
Andy McFadden | 1b4c796 | 2010-10-27 11:26:05 -0700 | [diff] [blame] | 599 | parsedArgs.permittedCapabilities, |
| 600 | parsedArgs.effectiveCapabilities); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 601 | } catch (IllegalArgumentException ex) { |
| 602 | throw new RuntimeException(ex); |
Bob Lee | e540833 | 2009-09-04 18:31:17 -0700 | [diff] [blame] | 603 | } |
| 604 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 605 | /* For child process */ |
| 606 | if (pid == 0) { |
Narayan Kamath | 64cd907 | 2014-05-13 13:35:14 +0100 | [diff] [blame] | 607 | if (hasSecondZygote(abiList)) { |
| 608 | waitForSecondaryZygote(socketName); |
| 609 | } |
| 610 | |
Tobias Sargeant | dd4bb31 | 2016-01-19 16:34:54 +0000 | [diff] [blame] | 611 | zygoteServer.closeServerSocket(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 612 | handleSystemServerProcess(parsedArgs); |
| 613 | } |
| 614 | |
| 615 | return true; |
| 616 | } |
| 617 | |
Alex Klyubin | 48a06e7 | 2013-04-19 10:01:42 -0700 | [diff] [blame] | 618 | /** |
| 619 | * Gets the bit array representation of the provided list of POSIX capabilities. |
| 620 | */ |
| 621 | private static long posixCapabilitiesAsBits(int... capabilities) { |
| 622 | long result = 0; |
| 623 | for (int capability : capabilities) { |
| 624 | if ((capability < 0) || (capability > OsConstants.CAP_LAST_CAP)) { |
| 625 | throw new IllegalArgumentException(String.valueOf(capability)); |
| 626 | } |
| 627 | result |= (1L << capability); |
| 628 | } |
| 629 | return result; |
| 630 | } |
| 631 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 632 | public static void main(String argv[]) { |
Tobias Sargeant | dd4bb31 | 2016-01-19 16:34:54 +0000 | [diff] [blame] | 633 | ZygoteServer zygoteServer = new ZygoteServer(); |
| 634 | |
Andreas Gampe | 1ef8aef | 2016-04-11 08:39:52 -0700 | [diff] [blame] | 635 | // Mark zygote start. This ensures that thread creation will throw |
| 636 | // an error. |
| 637 | ZygoteHooks.startZygoteNoThreadCreation(); |
| 638 | |
Robert Sesek | e4f8d69 | 2016-09-13 19:13:01 -0400 | [diff] [blame] | 639 | // Zygote goes into its own process group. |
| 640 | try { |
| 641 | Os.setpgid(0, 0); |
| 642 | } catch (ErrnoException ex) { |
| 643 | throw new RuntimeException("Failed to setpgid(0,0)", ex); |
| 644 | } |
| 645 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 646 | try { |
Yasuhiro Matsuda | 25878b2 | 2015-09-03 16:18:21 +0900 | [diff] [blame] | 647 | Trace.traceBegin(Trace.TRACE_TAG_DALVIK, "ZygoteInit"); |
| 648 | RuntimeInit.enableDdms(); |
| 649 | // Start profiling the zygote initialization. |
| 650 | SamplingProfilerIntegration.start(); |
| 651 | |
Narayan Kamath | c41638c | 2014-04-07 13:56:15 +0100 | [diff] [blame] | 652 | boolean startSystemServer = false; |
| 653 | String socketName = "zygote"; |
| 654 | String abiList = null; |
Yasuhiro Matsuda | 25878b2 | 2015-09-03 16:18:21 +0900 | [diff] [blame] | 655 | for (int i = 1; i < argv.length; i++) { |
| 656 | if ("start-system-server".equals(argv[i])) { |
| 657 | startSystemServer = true; |
| 658 | } else if (argv[i].startsWith(ABI_LIST_ARG)) { |
| 659 | abiList = argv[i].substring(ABI_LIST_ARG.length()); |
| 660 | } else if (argv[i].startsWith(SOCKET_NAME_ARG)) { |
| 661 | socketName = argv[i].substring(SOCKET_NAME_ARG.length()); |
| 662 | } else { |
| 663 | throw new RuntimeException("Unknown command line argument: " + argv[i]); |
Narayan Kamath | c41638c | 2014-04-07 13:56:15 +0100 | [diff] [blame] | 664 | } |
| 665 | } |
| 666 | |
Yasuhiro Matsuda | 25878b2 | 2015-09-03 16:18:21 +0900 | [diff] [blame] | 667 | if (abiList == null) { |
| 668 | throw new RuntimeException("No ABI list supplied."); |
| 669 | } |
| 670 | |
Tobias Sargeant | dd4bb31 | 2016-01-19 16:34:54 +0000 | [diff] [blame] | 671 | zygoteServer.registerServerSocket(socketName); |
Yasuhiro Matsuda | 25878b2 | 2015-09-03 16:18:21 +0900 | [diff] [blame] | 672 | Trace.traceBegin(Trace.TRACE_TAG_DALVIK, "ZygotePreload"); |
| 673 | EventLog.writeEvent(LOG_BOOT_PROGRESS_PRELOAD_START, |
| 674 | SystemClock.uptimeMillis()); |
| 675 | preload(); |
| 676 | EventLog.writeEvent(LOG_BOOT_PROGRESS_PRELOAD_END, |
| 677 | SystemClock.uptimeMillis()); |
| 678 | Trace.traceEnd(Trace.TRACE_TAG_DALVIK); |
| 679 | |
| 680 | // Finish profiling the zygote initialization. |
| 681 | SamplingProfilerIntegration.writeZygoteSnapshot(); |
| 682 | |
| 683 | // Do an initial gc to clean up after startup |
| 684 | Trace.traceBegin(Trace.TRACE_TAG_DALVIK, "PostZygoteInitGC"); |
| 685 | gcAndFinalize(); |
| 686 | Trace.traceEnd(Trace.TRACE_TAG_DALVIK); |
| 687 | |
Jamie Gennis | 6ad0452 | 2013-04-15 18:53:24 -0700 | [diff] [blame] | 688 | // Disable tracing so that forked processes do not inherit stale tracing tags from |
| 689 | // Zygote. |
| 690 | Trace.setTracingEnabled(false); |
| 691 | |
doheon1.lee | 885b742 | 2016-01-20 13:07:27 +0900 | [diff] [blame] | 692 | // Zygote process unmounts root storage spaces. |
| 693 | Zygote.nativeUnmountStorageOnInit(); |
| 694 | |
Andreas Gampe | 1ef8aef | 2016-04-11 08:39:52 -0700 | [diff] [blame] | 695 | ZygoteHooks.stopZygoteNoThreadCreation(); |
| 696 | |
Narayan Kamath | c41638c | 2014-04-07 13:56:15 +0100 | [diff] [blame] | 697 | if (startSystemServer) { |
Tobias Sargeant | dd4bb31 | 2016-01-19 16:34:54 +0000 | [diff] [blame] | 698 | startSystemServer(abiList, socketName, zygoteServer); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | Log.i(TAG, "Accepting command socket connections"); |
Tobias Sargeant | dd4bb31 | 2016-01-19 16:34:54 +0000 | [diff] [blame] | 702 | zygoteServer.runSelectLoop(abiList); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 703 | |
Tobias Sargeant | dd4bb31 | 2016-01-19 16:34:54 +0000 | [diff] [blame] | 704 | zygoteServer.closeServerSocket(); |
| 705 | } catch (Zygote.MethodAndArgsCaller caller) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 706 | caller.run(); |
Christopher Ferris | ea8b5de | 2016-06-17 15:29:58 -0700 | [diff] [blame] | 707 | } catch (Throwable ex) { |
Tobias Sargeant | dd4bb31 | 2016-01-19 16:34:54 +0000 | [diff] [blame] | 708 | Log.e(TAG, "System zygote died with exception", ex); |
| 709 | zygoteServer.closeServerSocket(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 710 | throw ex; |
| 711 | } |
| 712 | } |
| 713 | |
| 714 | /** |
Narayan Kamath | 64cd907 | 2014-05-13 13:35:14 +0100 | [diff] [blame] | 715 | * Return {@code true} if this device configuration has another zygote. |
| 716 | * |
| 717 | * We determine this by comparing the device ABI list with this zygotes |
| 718 | * list. If this zygote supports all ABIs this device supports, there won't |
| 719 | * be another zygote. |
| 720 | */ |
| 721 | private static boolean hasSecondZygote(String abiList) { |
| 722 | return !SystemProperties.get("ro.product.cpu.abilist").equals(abiList); |
| 723 | } |
| 724 | |
| 725 | private static void waitForSecondaryZygote(String socketName) { |
| 726 | String otherZygoteName = Process.ZYGOTE_SOCKET.equals(socketName) ? |
| 727 | Process.SECONDARY_ZYGOTE_SOCKET : Process.ZYGOTE_SOCKET; |
| 728 | while (true) { |
| 729 | try { |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 730 | final ZygoteProcess.ZygoteState zs = |
| 731 | ZygoteProcess.ZygoteState.connect(otherZygoteName); |
Narayan Kamath | 64cd907 | 2014-05-13 13:35:14 +0100 | [diff] [blame] | 732 | zs.close(); |
| 733 | break; |
| 734 | } catch (IOException ioe) { |
| 735 | Log.w(TAG, "Got error connecting to zygote, retrying. msg= " + ioe.getMessage()); |
| 736 | } |
| 737 | |
| 738 | try { |
| 739 | Thread.sleep(1000); |
| 740 | } catch (InterruptedException ie) { |
| 741 | } |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 746 | * Class not instantiable. |
| 747 | */ |
| 748 | private ZygoteInit() { |
| 749 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 750 | } |