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; |
Mathieu Chartier | b3eeceb | 2017-03-30 21:00:18 -0700 | [diff] [blame] | 24 | import android.os.Build; |
Mathieu Chartier | b3eeceb | 2017-03-30 21:00:18 -0700 | [diff] [blame] | 25 | import android.os.Environment; |
Mathew Inwood | 04194fe | 2018-04-04 14:48:03 +0100 | [diff] [blame] | 26 | import android.os.IInstalld; |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 27 | import android.os.Process; |
Jeff Sharkey | 740f523 | 2016-12-09 14:31:26 -0700 | [diff] [blame] | 28 | import android.os.RemoteException; |
| 29 | import android.os.ServiceManager; |
| 30 | import android.os.ServiceSpecificException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | import android.os.SystemClock; |
Romain Guy | c5e3638 | 2013-05-09 11:08:17 -0700 | [diff] [blame] | 32 | import android.os.SystemProperties; |
Jamie Gennis | 6ad0452 | 2013-04-15 18:53:24 -0700 | [diff] [blame] | 33 | import android.os.Trace; |
Calin Juravle | 3f3a08a | 2018-05-02 23:39:19 -0700 | [diff] [blame] | 34 | import android.os.UserHandle; |
Robert Sesek | 8f8d187 | 2016-03-18 16:52:57 -0400 | [diff] [blame] | 35 | import android.os.ZygoteProcess; |
Jeff Sharkey | c98c7bc | 2016-12-07 14:57:34 -0700 | [diff] [blame] | 36 | import android.os.storage.StorageManager; |
Sergio Giro | 69de320 | 2016-05-17 16:52:33 +0100 | [diff] [blame] | 37 | import android.security.keystore.AndroidKeyStoreProvider; |
Bill Yi | 9a76e9b | 2014-04-29 18:52:48 -0700 | [diff] [blame] | 38 | import android.system.ErrnoException; |
Elliott Hughes | 860c591 | 2014-04-28 19:19:13 -0700 | [diff] [blame] | 39 | import android.system.Os; |
| 40 | import android.system.OsConstants; |
Luis Hector Chavez | 72042c9 | 2017-07-12 10:03:30 -0700 | [diff] [blame] | 41 | import android.system.StructCapUserData; |
| 42 | import android.system.StructCapUserHeader; |
Raph Levien | c3dd1c1 | 2015-04-06 10:37:57 -0700 | [diff] [blame] | 43 | import android.text.Hyphenator; |
Mathieu Chartier | f5cd8c1 | 2019-02-04 13:28:36 -0800 | [diff] [blame] | 44 | import android.text.TextUtils; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | import android.util.EventLog; |
| 46 | import android.util.Log; |
Andreas Gampe | 76d4fc8 | 2017-02-07 19:44:37 -0800 | [diff] [blame] | 47 | import android.util.Slog; |
Mathew Inwood | 04194fe | 2018-04-04 14:48:03 +0100 | [diff] [blame] | 48 | import android.util.TimingsTraceLog; |
Torne (Richard Coles) | 08cfaf6 | 2014-05-08 16:07:05 +0100 | [diff] [blame] | 49 | import android.webkit.WebViewFactory; |
Andreas Gampe | ddc1397 | 2016-02-26 16:54:59 -0800 | [diff] [blame] | 50 | import android.widget.TextView; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | |
Fyodor Kupolov | 3235e0c | 2016-11-17 11:44:49 -0800 | [diff] [blame] | 52 | import com.android.internal.logging.MetricsLogger; |
Narayan Kamath | 669afcc | 2017-02-06 20:24:08 +0000 | [diff] [blame] | 53 | import com.android.internal.util.Preconditions; |
Mathew Inwood | 04194fe | 2018-04-04 14:48:03 +0100 | [diff] [blame] | 54 | |
Narayan Kamath | 29564cd | 2014-08-07 10:57:40 +0100 | [diff] [blame] | 55 | import dalvik.system.DexFile; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | import dalvik.system.VMRuntime; |
Andreas Gampe | 1ef8aef | 2016-04-11 08:39:52 -0700 | [diff] [blame] | 57 | import dalvik.system.ZygoteHooks; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | |
Brian Carlstrom | 46703b0 | 2011-04-06 15:41:29 -0700 | [diff] [blame] | 59 | import libcore.io.IoUtils; |
| 60 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | import java.io.BufferedReader; |
Mathieu Chartier | b3eeceb | 2017-03-30 21:00:18 -0700 | [diff] [blame] | 62 | import java.io.File; |
Ying Wang | d0c4535 | 2014-11-13 15:22:47 -0800 | [diff] [blame] | 63 | import java.io.FileInputStream; |
| 64 | import java.io.FileNotFoundException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | import java.io.IOException; |
| 66 | import java.io.InputStream; |
| 67 | import java.io.InputStreamReader; |
Sergio Giro | 6cb7b1c | 2016-05-13 16:34:46 +0100 | [diff] [blame] | 68 | import java.security.Provider; |
Mathew Inwood | 04194fe | 2018-04-04 14:48:03 +0100 | [diff] [blame] | 69 | import java.security.Security; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | |
| 71 | /** |
| 72 | * Startup class for the zygote process. |
| 73 | * |
Chris Wailes | cd92db2 | 2019-01-11 14:30:43 -0800 | [diff] [blame] | 74 | * Pre-initializes some classes, and then waits for commands on a UNIX domain socket. Based on these |
| 75 | * commands, forks off child processes that inherit the initial state of the VM. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | * |
Chris Wailes | 682b479 | 2019-01-11 16:14:43 -0800 | [diff] [blame] | 77 | * Please see {@link ZygoteArguments} for documentation on the client protocol. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | * |
| 79 | * @hide |
| 80 | */ |
| 81 | public class ZygoteInit { |
Chris Wailes | cd92db2 | 2019-01-11 14:30:43 -0800 | [diff] [blame] | 82 | |
| 83 | // TODO (chriswailes): Change this so it is set with Zygote or ZygoteSecondary as appropriate |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | private static final String TAG = "Zygote"; |
| 85 | |
Jesse Hall | 2852a28 | 2017-01-17 10:17:41 -0800 | [diff] [blame] | 86 | private static final String PROPERTY_DISABLE_OPENGL_PRELOADING = "ro.zygote.disable_gl_preload"; |
Jesse Hall | ba0370e | 2017-02-09 14:43:14 -0800 | [diff] [blame] | 87 | private static final String PROPERTY_GFX_DRIVER = "ro.gfx.driver.0"; |
Mathieu Chartier | f5cd8c1 | 2019-02-04 13:28:36 -0800 | [diff] [blame] | 88 | private static final String PROPERTY_USE_APP_IMAGE_STARTUP_CACHE = |
| 89 | "persist.device_config.runtime_native.use_app_image_startup_cache"; |
Romain Guy | c5e3638 | 2013-05-09 11:08:17 -0700 | [diff] [blame] | 90 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | private static final int LOG_BOOT_PROGRESS_PRELOAD_START = 3020; |
| 92 | private static final int LOG_BOOT_PROGRESS_PRELOAD_END = 3030; |
| 93 | |
Chris Wailes | cd92db2 | 2019-01-11 14:30:43 -0800 | [diff] [blame] | 94 | /** |
| 95 | * when preloading, GC after allocating this many bytes |
| 96 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | private static final int PRELOAD_GC_THRESHOLD = 50000; |
| 98 | |
Narayan Kamath | c41638c | 2014-04-07 13:56:15 +0100 | [diff] [blame] | 99 | private static final String ABI_LIST_ARG = "--abi-list="; |
| 100 | |
Chris Wailes | cd92db2 | 2019-01-11 14:30:43 -0800 | [diff] [blame] | 101 | // TODO (chriswailes): Re-name this --zygote-socket-name= and then add a |
| 102 | // --blastula-socket-name parameter. |
Narayan Kamath | c41638c | 2014-04-07 13:56:15 +0100 | [diff] [blame] | 103 | private static final String SOCKET_NAME_ARG = "--socket-name="; |
Barry Hayes | 0b3533a | 2010-01-20 12:46:47 -0800 | [diff] [blame] | 104 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | /** |
Tobias Sargeant | dd4bb31 | 2016-01-19 16:34:54 +0000 | [diff] [blame] | 106 | * Used to pre-load resources. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | */ |
| 108 | private static Resources mResources; |
Bob Lee | e540833 | 2009-09-04 18:31:17 -0700 | [diff] [blame] | 109 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | /** |
Ying Wang | d0c4535 | 2014-11-13 15:22:47 -0800 | [diff] [blame] | 111 | * 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] | 112 | */ |
Ying Wang | d0c4535 | 2014-11-13 15:22:47 -0800 | [diff] [blame] | 113 | 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] | 114 | |
Chris Wailes | cd92db2 | 2019-01-11 14:30:43 -0800 | [diff] [blame] | 115 | /** |
| 116 | * Controls whether we should preload resources during zygote init. |
| 117 | */ |
Steve Paik | 3c27a23 | 2015-06-10 18:25:44 -0700 | [diff] [blame] | 118 | public static final boolean PRELOAD_RESOURCES = true; |
Andy McFadden | 599c918 | 2009-04-08 00:35:56 -0700 | [diff] [blame] | 119 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | private static final int UNPRIVILEGED_UID = 9999; |
| 121 | private static final int UNPRIVILEGED_GID = 9999; |
| 122 | |
| 123 | private static final int ROOT_UID = 0; |
| 124 | private static final int ROOT_GID = 0; |
| 125 | |
Narayan Kamath | 5a3d0c6 | 2016-11-09 15:35:18 +0000 | [diff] [blame] | 126 | private static boolean sPreloadComplete; |
| 127 | |
Fyodor Kupolov | 6e3461b | 2017-08-10 17:00:43 -0700 | [diff] [blame] | 128 | static void preload(TimingsTraceLog bootTimingsTraceLog) { |
Bill Yi | 9a76e9b | 2014-04-29 18:52:48 -0700 | [diff] [blame] | 129 | Log.d(TAG, "begin preload"); |
Neil Fuller | a84056a | 2018-07-11 13:59:45 +0100 | [diff] [blame] | 130 | bootTimingsTraceLog.traceBegin("BeginPreload"); |
| 131 | beginPreload(); |
| 132 | bootTimingsTraceLog.traceEnd(); // BeginPreload |
Fyodor Kupolov | 3235e0c | 2016-11-17 11:44:49 -0800 | [diff] [blame] | 133 | bootTimingsTraceLog.traceBegin("PreloadClasses"); |
Yasuhiro Matsuda | 25878b2 | 2015-09-03 16:18:21 +0900 | [diff] [blame] | 134 | preloadClasses(); |
Fyodor Kupolov | 3235e0c | 2016-11-17 11:44:49 -0800 | [diff] [blame] | 135 | bootTimingsTraceLog.traceEnd(); // PreloadClasses |
| 136 | bootTimingsTraceLog.traceBegin("PreloadResources"); |
Yasuhiro Matsuda | 25878b2 | 2015-09-03 16:18:21 +0900 | [diff] [blame] | 137 | preloadResources(); |
Fyodor Kupolov | 3235e0c | 2016-11-17 11:44:49 -0800 | [diff] [blame] | 138 | bootTimingsTraceLog.traceEnd(); // PreloadResources |
Jesse Hall | 1fe1dc0 | 2017-07-06 15:30:39 -0700 | [diff] [blame] | 139 | Trace.traceBegin(Trace.TRACE_TAG_DALVIK, "PreloadAppProcessHALs"); |
| 140 | nativePreloadAppProcessHALs(); |
| 141 | Trace.traceEnd(Trace.TRACE_TAG_DALVIK); |
Jesse Hall | ba0370e | 2017-02-09 14:43:14 -0800 | [diff] [blame] | 142 | Trace.traceBegin(Trace.TRACE_TAG_DALVIK, "PreloadOpenGL"); |
| 143 | preloadOpenGL(); |
| 144 | Trace.traceEnd(Trace.TRACE_TAG_DALVIK); |
Brian Carlstrom | 6c9af96 | 2014-09-11 15:36:00 -0700 | [diff] [blame] | 145 | preloadSharedLibraries(); |
Raph Levien | c3dd1c1 | 2015-04-06 10:37:57 -0700 | [diff] [blame] | 146 | preloadTextResources(); |
Torne (Richard Coles) | 08cfaf6 | 2014-05-08 16:07:05 +0100 | [diff] [blame] | 147 | // Ask the WebViewFactory to do any initialization that must run in the zygote process, |
| 148 | // for memory sharing purposes. |
| 149 | WebViewFactory.prepareWebViewInZygote(); |
Neil Fuller | a84056a | 2018-07-11 13:59:45 +0100 | [diff] [blame] | 150 | endPreload(); |
Sergio Giro | 6cb7b1c | 2016-05-13 16:34:46 +0100 | [diff] [blame] | 151 | warmUpJcaProviders(); |
Bill Yi | 9a76e9b | 2014-04-29 18:52:48 -0700 | [diff] [blame] | 152 | Log.d(TAG, "end preload"); |
Narayan Kamath | 5a3d0c6 | 2016-11-09 15:35:18 +0000 | [diff] [blame] | 153 | |
| 154 | sPreloadComplete = true; |
| 155 | } |
| 156 | |
Narayan Kamath | 669afcc | 2017-02-06 20:24:08 +0000 | [diff] [blame] | 157 | public static void lazyPreload() { |
| 158 | Preconditions.checkState(!sPreloadComplete); |
| 159 | Log.i(TAG, "Lazily preloading resources."); |
| 160 | |
Fyodor Kupolov | 6e3461b | 2017-08-10 17:00:43 -0700 | [diff] [blame] | 161 | preload(new TimingsTraceLog("ZygoteInitTiming_lazy", Trace.TRACE_TAG_DALVIK)); |
Romain Guy | 74c6912 | 2013-05-08 17:54:20 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Neil Fuller | a84056a | 2018-07-11 13:59:45 +0100 | [diff] [blame] | 164 | private static void beginPreload() { |
| 165 | Log.i(TAG, "Calling ZygoteHooks.beginPreload()"); |
Neil Fuller | 4f41f61 | 2016-05-09 16:55:36 +0100 | [diff] [blame] | 166 | |
Neil Fuller | a84056a | 2018-07-11 13:59:45 +0100 | [diff] [blame] | 167 | ZygoteHooks.onBeginPreload(); |
Neil Fuller | 4f41f61 | 2016-05-09 16:55:36 +0100 | [diff] [blame] | 168 | } |
| 169 | |
Neil Fuller | a84056a | 2018-07-11 13:59:45 +0100 | [diff] [blame] | 170 | private static void endPreload() { |
| 171 | ZygoteHooks.onEndPreload(); |
Neil Fuller | 4f41f61 | 2016-05-09 16:55:36 +0100 | [diff] [blame] | 172 | |
Neil Fuller | a84056a | 2018-07-11 13:59:45 +0100 | [diff] [blame] | 173 | Log.i(TAG, "Called ZygoteHooks.endPreload()"); |
Neil Fuller | 4f41f61 | 2016-05-09 16:55:36 +0100 | [diff] [blame] | 174 | } |
| 175 | |
Brian Carlstrom | 6c9af96 | 2014-09-11 15:36:00 -0700 | [diff] [blame] | 176 | private static void preloadSharedLibraries() { |
| 177 | Log.i(TAG, "Preloading shared libraries..."); |
| 178 | System.loadLibrary("android"); |
| 179 | System.loadLibrary("compiler_rt"); |
| 180 | System.loadLibrary("jnigraphics"); |
| 181 | } |
| 182 | |
Jesse Hall | 1fe1dc0 | 2017-07-06 15:30:39 -0700 | [diff] [blame] | 183 | native private static void nativePreloadAppProcessHALs(); |
Chris Wailes | cd92db2 | 2019-01-11 14:30:43 -0800 | [diff] [blame] | 184 | |
Peter Collingbourne | 6f4986b | 2018-10-26 14:52:14 -0700 | [diff] [blame] | 185 | native private static void nativePreloadOpenGL(); |
Jesse Hall | 1fe1dc0 | 2017-07-06 15:30:39 -0700 | [diff] [blame] | 186 | |
Jesse Hall | ba0370e | 2017-02-09 14:43:14 -0800 | [diff] [blame] | 187 | private static void preloadOpenGL() { |
| 188 | String driverPackageName = SystemProperties.get(PROPERTY_GFX_DRIVER); |
Jesse Hall | 5d911fe | 2017-04-13 15:44:24 -0700 | [diff] [blame] | 189 | if (!SystemProperties.getBoolean(PROPERTY_DISABLE_OPENGL_PRELOADING, false) && |
| 190 | (driverPackageName == null || driverPackageName.isEmpty())) { |
Peter Collingbourne | 6f4986b | 2018-10-26 14:52:14 -0700 | [diff] [blame] | 191 | nativePreloadOpenGL(); |
Jesse Hall | ba0370e | 2017-02-09 14:43:14 -0800 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
Raph Levien | c3dd1c1 | 2015-04-06 10:37:57 -0700 | [diff] [blame] | 195 | private static void preloadTextResources() { |
| 196 | Hyphenator.init(); |
Andreas Gampe | ddc1397 | 2016-02-26 16:54:59 -0800 | [diff] [blame] | 197 | TextView.preloadFontCache(); |
Raph Levien | c3dd1c1 | 2015-04-06 10:37:57 -0700 | [diff] [blame] | 198 | } |
| 199 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 200 | /** |
Sergio Giro | 69de320 | 2016-05-17 16:52:33 +0100 | [diff] [blame] | 201 | * Register AndroidKeyStoreProvider and warm up the providers that are already registered. |
Sergio Giro | 6cb7b1c | 2016-05-13 16:34:46 +0100 | [diff] [blame] | 202 | * |
Chris Wailes | cd92db2 | 2019-01-11 14:30:43 -0800 | [diff] [blame] | 203 | * By doing it here we avoid that each app does it when requesting a service from the provider |
| 204 | * for the first time. |
Sergio Giro | 6cb7b1c | 2016-05-13 16:34:46 +0100 | [diff] [blame] | 205 | */ |
| 206 | private static void warmUpJcaProviders() { |
| 207 | long startTime = SystemClock.uptimeMillis(); |
| 208 | Trace.traceBegin( |
Sergio Giro | 69de320 | 2016-05-17 16:52:33 +0100 | [diff] [blame] | 209 | Trace.TRACE_TAG_DALVIK, "Starting installation of AndroidKeyStoreProvider"); |
| 210 | // AndroidKeyStoreProvider.install() manipulates the list of JCA providers to insert |
| 211 | // preferred providers. Note this is not done via security.properties as the JCA providers |
| 212 | // are not on the classpath in the case of, for example, raw dalvikvm runtimes. |
| 213 | AndroidKeyStoreProvider.install(); |
| 214 | Log.i(TAG, "Installed AndroidKeyStoreProvider in " |
| 215 | + (SystemClock.uptimeMillis() - startTime) + "ms."); |
| 216 | Trace.traceEnd(Trace.TRACE_TAG_DALVIK); |
| 217 | |
| 218 | startTime = SystemClock.uptimeMillis(); |
| 219 | Trace.traceBegin( |
Sergio Giro | 6cb7b1c | 2016-05-13 16:34:46 +0100 | [diff] [blame] | 220 | Trace.TRACE_TAG_DALVIK, "Starting warm up of JCA providers"); |
| 221 | for (Provider p : Security.getProviders()) { |
| 222 | p.warmUpServiceProvision(); |
| 223 | } |
| 224 | Log.i(TAG, "Warmed up JCA providers in " |
Sergio Giro | 69de320 | 2016-05-17 16:52:33 +0100 | [diff] [blame] | 225 | + (SystemClock.uptimeMillis() - startTime) + "ms."); |
Sergio Giro | 6cb7b1c | 2016-05-13 16:34:46 +0100 | [diff] [blame] | 226 | Trace.traceEnd(Trace.TRACE_TAG_DALVIK); |
| 227 | } |
| 228 | |
| 229 | /** |
Chris Wailes | cd92db2 | 2019-01-11 14:30:43 -0800 | [diff] [blame] | 230 | * Performs Zygote process initialization. Loads and initializes commonly used classes. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 231 | * |
Chris Wailes | cd92db2 | 2019-01-11 14:30:43 -0800 | [diff] [blame] | 232 | * Most classes only cause a few hundred bytes to be allocated, but a few will allocate a dozen |
| 233 | * Kbytes (in one case, 500+K). |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 234 | */ |
| 235 | private static void preloadClasses() { |
| 236 | final VMRuntime runtime = VMRuntime.getRuntime(); |
Bob Lee | e540833 | 2009-09-04 18:31:17 -0700 | [diff] [blame] | 237 | |
Ying Wang | d0c4535 | 2014-11-13 15:22:47 -0800 | [diff] [blame] | 238 | InputStream is; |
| 239 | try { |
| 240 | is = new FileInputStream(PRELOADED_CLASSES); |
| 241 | } catch (FileNotFoundException e) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 242 | Log.e(TAG, "Couldn't find " + PRELOADED_CLASSES + "."); |
Ying Wang | d0c4535 | 2014-11-13 15:22:47 -0800 | [diff] [blame] | 243 | return; |
| 244 | } |
Bob Lee | e540833 | 2009-09-04 18:31:17 -0700 | [diff] [blame] | 245 | |
Ying Wang | d0c4535 | 2014-11-13 15:22:47 -0800 | [diff] [blame] | 246 | Log.i(TAG, "Preloading classes..."); |
| 247 | long startTime = SystemClock.uptimeMillis(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 248 | |
Ying Wang | d0c4535 | 2014-11-13 15:22:47 -0800 | [diff] [blame] | 249 | // Drop root perms while running static initializers. |
Narayan Kamath | 23e6878 | 2015-01-16 17:22:41 +0000 | [diff] [blame] | 250 | final int reuid = Os.getuid(); |
| 251 | final int regid = Os.getgid(); |
| 252 | |
| 253 | // We need to drop root perms only if we're already root. In the case of "wrapped" |
| 254 | // processes (see WrapperInit), this function is called from an unprivileged uid |
| 255 | // and gid. |
| 256 | boolean droppedPriviliges = false; |
| 257 | if (reuid == ROOT_UID && regid == ROOT_GID) { |
| 258 | try { |
| 259 | Os.setregid(ROOT_GID, UNPRIVILEGED_GID); |
| 260 | Os.setreuid(ROOT_UID, UNPRIVILEGED_UID); |
| 261 | } catch (ErrnoException ex) { |
| 262 | throw new RuntimeException("Failed to drop root", ex); |
| 263 | } |
| 264 | |
| 265 | droppedPriviliges = true; |
Elliott Hughes | 26b56e6 | 2014-12-17 12:28:29 -0800 | [diff] [blame] | 266 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 267 | |
Ying Wang | d0c4535 | 2014-11-13 15:22:47 -0800 | [diff] [blame] | 268 | // Alter the target heap utilization. With explicit GCs this |
| 269 | // is not likely to have any effect. |
| 270 | float defaultUtilization = runtime.getTargetHeapUtilization(); |
| 271 | runtime.setTargetHeapUtilization(0.8f); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 272 | |
Ying Wang | d0c4535 | 2014-11-13 15:22:47 -0800 | [diff] [blame] | 273 | try { |
Chris Wailes | cd92db2 | 2019-01-11 14:30:43 -0800 | [diff] [blame] | 274 | BufferedReader br = |
Chris Wailes | cffbf1c | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 275 | new BufferedReader(new InputStreamReader(is), Zygote.SOCKET_BUFFER_SIZE); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 276 | |
Ying Wang | d0c4535 | 2014-11-13 15:22:47 -0800 | [diff] [blame] | 277 | int count = 0; |
| 278 | String line; |
| 279 | while ((line = br.readLine()) != null) { |
| 280 | // Skip comments and blank lines. |
| 281 | line = line.trim(); |
| 282 | if (line.startsWith("#") || line.equals("")) { |
| 283 | continue; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 284 | } |
| 285 | |
Andreas Gampe | c425362 | 2016-10-28 18:19:30 -0700 | [diff] [blame] | 286 | Trace.traceBegin(Trace.TRACE_TAG_DALVIK, line); |
Ying Wang | d0c4535 | 2014-11-13 15:22:47 -0800 | [diff] [blame] | 287 | try { |
| 288 | if (false) { |
| 289 | Log.v(TAG, "Preloading " + line + "..."); |
| 290 | } |
Andreas Gampe | dd8e5fb | 2015-04-21 09:01:51 -0700 | [diff] [blame] | 291 | // Load and explicitly initialize the given class. Use |
| 292 | // Class.forName(String, boolean, ClassLoader) to avoid repeated stack lookups |
| 293 | // (to derive the caller's class-loader). Use true to force initialization, and |
| 294 | // null for the boot classpath class-loader (could as well cache the |
| 295 | // class-loader of this class in a variable). |
Andreas Gampe | c917f74 | 2015-04-20 19:16:37 -0700 | [diff] [blame] | 296 | Class.forName(line, true, null); |
Ying Wang | d0c4535 | 2014-11-13 15:22:47 -0800 | [diff] [blame] | 297 | count++; |
| 298 | } catch (ClassNotFoundException e) { |
| 299 | Log.w(TAG, "Class not found for preloading: " + line); |
| 300 | } catch (UnsatisfiedLinkError e) { |
| 301 | Log.w(TAG, "Problem preloading " + line + ": " + e); |
| 302 | } catch (Throwable t) { |
| 303 | Log.e(TAG, "Error preloading " + line + ".", t); |
| 304 | if (t instanceof Error) { |
| 305 | throw (Error) t; |
| 306 | } |
| 307 | if (t instanceof RuntimeException) { |
| 308 | throw (RuntimeException) t; |
| 309 | } |
| 310 | throw new RuntimeException(t); |
| 311 | } |
Yasuhiro Matsuda | 25878b2 | 2015-09-03 16:18:21 +0900 | [diff] [blame] | 312 | Trace.traceEnd(Trace.TRACE_TAG_DALVIK); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 313 | } |
Ying Wang | d0c4535 | 2014-11-13 15:22:47 -0800 | [diff] [blame] | 314 | |
| 315 | Log.i(TAG, "...preloaded " + count + " classes in " |
Chris Wailes | cd92db2 | 2019-01-11 14:30:43 -0800 | [diff] [blame] | 316 | + (SystemClock.uptimeMillis() - startTime) + "ms."); |
Ying Wang | d0c4535 | 2014-11-13 15:22:47 -0800 | [diff] [blame] | 317 | } catch (IOException e) { |
| 318 | Log.e(TAG, "Error reading " + PRELOADED_CLASSES + ".", e); |
| 319 | } finally { |
| 320 | IoUtils.closeQuietly(is); |
| 321 | // Restore default. |
| 322 | runtime.setTargetHeapUtilization(defaultUtilization); |
| 323 | |
| 324 | // 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] | 325 | Trace.traceBegin(Trace.TRACE_TAG_DALVIK, "PreloadDexCaches"); |
Ying Wang | d0c4535 | 2014-11-13 15:22:47 -0800 | [diff] [blame] | 326 | runtime.preloadDexCaches(); |
Yasuhiro Matsuda | 1ab43d5 | 2015-06-30 17:07:32 +0900 | [diff] [blame] | 327 | Trace.traceEnd(Trace.TRACE_TAG_DALVIK); |
Ying Wang | d0c4535 | 2014-11-13 15:22:47 -0800 | [diff] [blame] | 328 | |
Narayan Kamath | 23e6878 | 2015-01-16 17:22:41 +0000 | [diff] [blame] | 329 | // Bring back root. We'll need it later if we're in the zygote. |
| 330 | if (droppedPriviliges) { |
| 331 | try { |
| 332 | Os.setreuid(ROOT_UID, ROOT_UID); |
| 333 | Os.setregid(ROOT_GID, ROOT_GID); |
| 334 | } catch (ErrnoException ex) { |
| 335 | throw new RuntimeException("Failed to restore root", ex); |
| 336 | } |
Elliott Hughes | 26b56e6 | 2014-12-17 12:28:29 -0800 | [diff] [blame] | 337 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 338 | } |
| 339 | } |
| 340 | |
| 341 | /** |
Chris Wailes | cd92db2 | 2019-01-11 14:30:43 -0800 | [diff] [blame] | 342 | * Load in commonly used resources, so they can be shared across processes. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 343 | * |
Chris Wailes | cd92db2 | 2019-01-11 14:30:43 -0800 | [diff] [blame] | 344 | * These tend to be a few Kbytes, but are frequently in the 20-40K range, and occasionally even |
| 345 | * larger. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 346 | */ |
| 347 | private static void preloadResources() { |
| 348 | final VMRuntime runtime = VMRuntime.getRuntime(); |
Bob Lee | e540833 | 2009-09-04 18:31:17 -0700 | [diff] [blame] | 349 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 350 | try { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 351 | mResources = Resources.getSystem(); |
| 352 | mResources.startPreloading(); |
| 353 | if (PRELOAD_RESOURCES) { |
| 354 | Log.i(TAG, "Preloading resources..."); |
| 355 | |
| 356 | long startTime = SystemClock.uptimeMillis(); |
| 357 | TypedArray ar = mResources.obtainTypedArray( |
| 358 | com.android.internal.R.array.preloaded_drawables); |
Filip Gruszczynski | 811dc3b | 2015-11-23 12:34:22 -0800 | [diff] [blame] | 359 | int N = preloadDrawables(ar); |
Jeff Brown | 14577c4 | 2012-03-08 16:40:14 -0800 | [diff] [blame] | 360 | ar.recycle(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 361 | Log.i(TAG, "...preloaded " + N + " resources in " |
Chris Wailes | cd92db2 | 2019-01-11 14:30:43 -0800 | [diff] [blame] | 362 | + (SystemClock.uptimeMillis() - startTime) + "ms."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 363 | |
| 364 | startTime = SystemClock.uptimeMillis(); |
| 365 | ar = mResources.obtainTypedArray( |
| 366 | com.android.internal.R.array.preloaded_color_state_lists); |
Filip Gruszczynski | 811dc3b | 2015-11-23 12:34:22 -0800 | [diff] [blame] | 367 | N = preloadColorStateLists(ar); |
Jeff Brown | 14577c4 | 2012-03-08 16:40:14 -0800 | [diff] [blame] | 368 | ar.recycle(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 369 | Log.i(TAG, "...preloaded " + N + " resources in " |
Chris Wailes | cd92db2 | 2019-01-11 14:30:43 -0800 | [diff] [blame] | 370 | + (SystemClock.uptimeMillis() - startTime) + "ms."); |
Filip Gruszczynski | 811dc3b | 2015-11-23 12:34:22 -0800 | [diff] [blame] | 371 | |
| 372 | if (mResources.getBoolean( |
| 373 | com.android.internal.R.bool.config_freeformWindowManagement)) { |
| 374 | startTime = SystemClock.uptimeMillis(); |
| 375 | ar = mResources.obtainTypedArray( |
| 376 | com.android.internal.R.array.preloaded_freeform_multi_window_drawables); |
| 377 | N = preloadDrawables(ar); |
| 378 | ar.recycle(); |
| 379 | Log.i(TAG, "...preloaded " + N + " resource in " |
| 380 | + (SystemClock.uptimeMillis() - startTime) + "ms."); |
| 381 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 382 | } |
| 383 | mResources.finishPreloading(); |
| 384 | } catch (RuntimeException e) { |
| 385 | Log.w(TAG, "Failure preloading resources", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 386 | } |
| 387 | } |
| 388 | |
Filip Gruszczynski | 811dc3b | 2015-11-23 12:34:22 -0800 | [diff] [blame] | 389 | private static int preloadColorStateLists(TypedArray ar) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 390 | int N = ar.length(); |
Chris Wailes | cd92db2 | 2019-01-11 14:30:43 -0800 | [diff] [blame] | 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 | 77bb6f1 | 2015-02-11 17:24:33 -0800 | [diff] [blame] | 397 | if (mResources.getColorStateList(id, null) == null) { |
Dianne Hackborn | dde331c | 2012-08-03 14:01:57 -0700 | [diff] [blame] | 398 | throw new IllegalArgumentException( |
| 399 | "Unable to find preloaded color resource #0x" |
Chris Wailes | cd92db2 | 2019-01-11 14:30:43 -0800 | [diff] [blame] | 400 | + Integer.toHexString(id) |
| 401 | + " (" + ar.getString(i) + ")"); |
Dianne Hackborn | dde331c | 2012-08-03 14:01:57 -0700 | [diff] [blame] | 402 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 403 | } |
| 404 | } |
| 405 | return N; |
| 406 | } |
| 407 | |
| 408 | |
Filip Gruszczynski | 811dc3b | 2015-11-23 12:34:22 -0800 | [diff] [blame] | 409 | private static int preloadDrawables(TypedArray ar) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 410 | int N = ar.length(); |
Chris Wailes | cd92db2 | 2019-01-11 14:30:43 -0800 | [diff] [blame] | 411 | for (int i = 0; i < N; i++) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 412 | int id = ar.getResourceId(i, 0); |
Joe Onorato | 43a1765 | 2011-04-06 19:22:23 -0700 | [diff] [blame] | 413 | if (false) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 414 | Log.v(TAG, "Preloading resource #" + Integer.toHexString(id)); |
| 415 | } |
| 416 | if (id != 0) { |
Alan Viverette | 84e001a | 2014-09-22 00:04:22 -0700 | [diff] [blame] | 417 | if (mResources.getDrawable(id, null) == null) { |
Dianne Hackborn | dde331c | 2012-08-03 14:01:57 -0700 | [diff] [blame] | 418 | throw new IllegalArgumentException( |
| 419 | "Unable to find preloaded drawable resource #0x" |
Chris Wailes | cd92db2 | 2019-01-11 14:30:43 -0800 | [diff] [blame] | 420 | + Integer.toHexString(id) |
| 421 | + " (" + ar.getString(i) + ")"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 422 | } |
| 423 | } |
| 424 | } |
| 425 | return N; |
| 426 | } |
| 427 | |
| 428 | /** |
Chris Wailes | cd92db2 | 2019-01-11 14:30:43 -0800 | [diff] [blame] | 429 | * Runs several special GCs to try to clean up a few generations of softly- and final-reachable |
| 430 | * objects, along with any other garbage. This is only useful just before a fork(). |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 431 | */ |
Neil Fuller | a84056a | 2018-07-11 13:59:45 +0100 | [diff] [blame] | 432 | private static void gcAndFinalize() { |
| 433 | ZygoteHooks.gcAndFinalize(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | /** |
| 437 | * Finish remaining work for the newly forked system server process. |
| 438 | */ |
Chris Wailes | 682b479 | 2019-01-11 16:14:43 -0800 | [diff] [blame] | 439 | private static Runnable handleSystemServerProcess(ZygoteArguments parsedArgs) { |
Mike Lockwood | 90960e8 | 2010-08-06 09:15:25 -0400 | [diff] [blame] | 440 | // 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] | 441 | Os.umask(S_IRWXG | S_IRWXO); |
Mike Lockwood | 90960e8 | 2010-08-06 09:15:25 -0400 | [diff] [blame] | 442 | |
Chris Wailes | 682b479 | 2019-01-11 16:14:43 -0800 | [diff] [blame] | 443 | if (parsedArgs.mNiceName != null) { |
| 444 | Process.setArgV0(parsedArgs.mNiceName); |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 445 | } |
| 446 | |
Narayan Kamath | 29564cd | 2014-08-07 10:57:40 +0100 | [diff] [blame] | 447 | final String systemServerClasspath = Os.getenv("SYSTEMSERVERCLASSPATH"); |
| 448 | if (systemServerClasspath != null) { |
| 449 | performSystemServerDexOpt(systemServerClasspath); |
Mathieu Chartier | b3eeceb | 2017-03-30 21:00:18 -0700 | [diff] [blame] | 450 | // Capturing profiles is only supported for debug or eng builds since selinux normally |
| 451 | // prevents it. |
| 452 | boolean profileSystemServer = SystemProperties.getBoolean( |
| 453 | "dalvik.vm.profilesystemserver", false); |
| 454 | if (profileSystemServer && (Build.IS_USERDEBUG || Build.IS_ENG)) { |
| 455 | try { |
Calin Juravle | 3f3a08a | 2018-05-02 23:39:19 -0700 | [diff] [blame] | 456 | prepareSystemServerProfile(systemServerClasspath); |
Mathieu Chartier | b3eeceb | 2017-03-30 21:00:18 -0700 | [diff] [blame] | 457 | } catch (Exception e) { |
| 458 | Log.wtf(TAG, "Failed to set up system server profile", e); |
| 459 | } |
| 460 | } |
Narayan Kamath | 29564cd | 2014-08-07 10:57:40 +0100 | [diff] [blame] | 461 | } |
| 462 | |
Chris Wailes | 682b479 | 2019-01-11 16:14:43 -0800 | [diff] [blame] | 463 | if (parsedArgs.mInvokeWith != null) { |
| 464 | String[] args = parsedArgs.mRemainingArgs; |
Narayan Kamath | 29564cd | 2014-08-07 10:57:40 +0100 | [diff] [blame] | 465 | // If we have a non-null system server class path, we'll have to duplicate the |
| 466 | // existing arguments and append the classpath to it. ART will handle the classpath |
| 467 | // correctly when we exec a new process. |
| 468 | if (systemServerClasspath != null) { |
| 469 | String[] amendedArgs = new String[args.length + 2]; |
| 470 | amendedArgs[0] = "-cp"; |
| 471 | amendedArgs[1] = systemServerClasspath; |
tony.ys_liu | 3473817 | 2016-12-14 18:50:29 +0800 | [diff] [blame] | 472 | System.arraycopy(args, 0, amendedArgs, 2, args.length); |
| 473 | args = amendedArgs; |
Narayan Kamath | 29564cd | 2014-08-07 10:57:40 +0100 | [diff] [blame] | 474 | } |
| 475 | |
Chris Wailes | 682b479 | 2019-01-11 16:14:43 -0800 | [diff] [blame] | 476 | WrapperInit.execApplication(parsedArgs.mInvokeWith, |
| 477 | parsedArgs.mNiceName, parsedArgs.mTargetSdkVersion, |
Narayan Kamath | 37ad4b0 | 2015-01-19 16:05:24 +0000 | [diff] [blame] | 478 | VMRuntime.getCurrentInstructionSet(), null, args); |
Narayan Kamath | bf99d06 | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 479 | |
| 480 | throw new IllegalStateException("Unexpected return from WrapperInit.execApplication"); |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 481 | } else { |
Narayan Kamath | 29564cd | 2014-08-07 10:57:40 +0100 | [diff] [blame] | 482 | ClassLoader cl = null; |
| 483 | if (systemServerClasspath != null) { |
Chris Wailes | 682b479 | 2019-01-11 16:14:43 -0800 | [diff] [blame] | 484 | cl = createPathClassLoader(systemServerClasspath, parsedArgs.mTargetSdkVersion); |
Dimitry Ivanov | 89dad33 | 2016-04-20 16:02:00 -0700 | [diff] [blame] | 485 | |
Narayan Kamath | 29564cd | 2014-08-07 10:57:40 +0100 | [diff] [blame] | 486 | Thread.currentThread().setContextClassLoader(cl); |
| 487 | } |
| 488 | |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 489 | /* |
| 490 | * Pass the remaining arguments to SystemServer. |
| 491 | */ |
Chris Wailes | 682b479 | 2019-01-11 16:14:43 -0800 | [diff] [blame] | 492 | return ZygoteInit.zygoteInit(parsedArgs.mTargetSdkVersion, |
| 493 | parsedArgs.mRemainingArgs, cl); |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 494 | } |
| 495 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 496 | /* should never reach here */ |
| 497 | } |
| 498 | |
Calin Juravle | 3f3a08a | 2018-05-02 23:39:19 -0700 | [diff] [blame] | 499 | /** |
Chris Wailes | cd92db2 | 2019-01-11 14:30:43 -0800 | [diff] [blame] | 500 | * Note that preparing the profiles for system server does not require special selinux |
| 501 | * permissions. From the installer perspective the system server is a regular package which can |
| 502 | * capture profile information. |
Calin Juravle | 3f3a08a | 2018-05-02 23:39:19 -0700 | [diff] [blame] | 503 | */ |
| 504 | private static void prepareSystemServerProfile(String systemServerClasspath) |
| 505 | throws RemoteException { |
| 506 | if (systemServerClasspath.isEmpty()) { |
| 507 | return; |
| 508 | } |
| 509 | String[] codePaths = systemServerClasspath.split(":"); |
| 510 | |
| 511 | final IInstalld installd = IInstalld.Stub |
| 512 | .asInterface(ServiceManager.getService("installd")); |
| 513 | |
| 514 | String systemServerPackageName = "android"; |
| 515 | String systemServerProfileName = "primary.prof"; |
| 516 | installd.prepareAppProfile( |
| 517 | systemServerPackageName, |
| 518 | UserHandle.USER_SYSTEM, |
| 519 | UserHandle.getAppId(Process.SYSTEM_UID), |
| 520 | systemServerProfileName, |
| 521 | codePaths[0], |
| 522 | /*dexMetadata*/ null); |
| 523 | |
| 524 | File profileDir = Environment.getDataProfilesDePackageDirectory( |
| 525 | UserHandle.USER_SYSTEM, systemServerPackageName); |
| 526 | String profilePath = new File(profileDir, systemServerProfileName).getAbsolutePath(); |
| 527 | VMRuntime.registerAppInfo(profilePath, codePaths); |
| 528 | } |
| 529 | |
Mathew Inwood | 8faeab8 | 2018-03-16 14:26:08 +0000 | [diff] [blame] | 530 | public static void setApiBlacklistExemptions(String[] exemptions) { |
| 531 | VMRuntime.getRuntime().setHiddenApiExemptions(exemptions); |
| 532 | } |
| 533 | |
Mathew Inwood | 04194fe | 2018-04-04 14:48:03 +0100 | [diff] [blame] | 534 | public static void setHiddenApiAccessLogSampleRate(int percent) { |
| 535 | VMRuntime.getRuntime().setHiddenApiAccessLogSamplingRate(percent); |
| 536 | } |
| 537 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 538 | /** |
Andrei Onea | 89acbab | 2019-01-25 16:29:44 +0000 | [diff] [blame] | 539 | * Sets the implementation to be used for logging hidden API accesses |
| 540 | * @param logger the implementation of the VMRuntime.HiddenApiUsageLogger interface |
| 541 | */ |
| 542 | public static void setHiddenApiUsageLogger(VMRuntime.HiddenApiUsageLogger logger) { |
| 543 | VMRuntime.getRuntime().setHiddenApiUsageLogger(logger); |
| 544 | } |
| 545 | |
| 546 | /** |
tony.ys_liu | 3473817 | 2016-12-14 18:50:29 +0800 | [diff] [blame] | 547 | * Creates a PathClassLoader for the given class path that is associated with a shared |
| 548 | * namespace, i.e., this classloader can access platform-private native libraries. The |
| 549 | * classloader will use java.library.path as the native library path. |
Dimitry Ivanov | 89dad33 | 2016-04-20 16:02:00 -0700 | [diff] [blame] | 550 | */ |
Narayan Kamath | f9419f0 | 2017-06-15 11:35:38 +0100 | [diff] [blame] | 551 | static ClassLoader createPathClassLoader(String classPath, int targetSdkVersion) { |
| 552 | String libraryPath = System.getProperty("java.library.path"); |
Dimitry Ivanov | 89dad33 | 2016-04-20 16:02:00 -0700 | [diff] [blame] | 553 | |
Nicolas Geoffray | 5d1814e | 2018-12-13 09:27:04 +0000 | [diff] [blame] | 554 | // We use the boot class loader, that's what the runtime expects at AOT. |
| 555 | ClassLoader parent = ClassLoader.getSystemClassLoader().getParent(); |
| 556 | |
Narayan Kamath | f9419f0 | 2017-06-15 11:35:38 +0100 | [diff] [blame] | 557 | return ClassLoaderFactory.createClassLoader(classPath, libraryPath, libraryPath, |
Nicolas Geoffray | 5d1814e | 2018-12-13 09:27:04 +0000 | [diff] [blame] | 558 | parent, targetSdkVersion, true /* isNamespaceShared */, null /* classLoaderName */); |
Dimitry Ivanov | 89dad33 | 2016-04-20 16:02:00 -0700 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | /** |
Chris Wailes | cd92db2 | 2019-01-11 14:30:43 -0800 | [diff] [blame] | 562 | * Performs dex-opt on the elements of {@code classPath}, if needed. We choose the instruction |
| 563 | * set of the current runtime. |
Narayan Kamath | 29564cd | 2014-08-07 10:57:40 +0100 | [diff] [blame] | 564 | */ |
| 565 | private static void performSystemServerDexOpt(String classPath) { |
| 566 | final String[] classPathElements = classPath.split(":"); |
Jeff Sharkey | 740f523 | 2016-12-09 14:31:26 -0700 | [diff] [blame] | 567 | final IInstalld installd = IInstalld.Stub |
| 568 | .asInterface(ServiceManager.getService("installd")); |
Narayan Kamath | 29564cd | 2014-08-07 10:57:40 +0100 | [diff] [blame] | 569 | final String instructionSet = VMRuntime.getRuntime().vmInstructionSet(); |
| 570 | |
Calin Juravle | 2f09ff3 | 2017-07-24 15:29:56 -0700 | [diff] [blame] | 571 | String classPathForElement = ""; |
Jeff Sharkey | 740f523 | 2016-12-09 14:31:26 -0700 | [diff] [blame] | 572 | for (String classPathElement : classPathElements) { |
| 573 | // System server is fully AOTed and never profiled |
| 574 | // for profile guided compilation. |
Mathieu Chartier | 5e07a0d | 2017-06-15 19:09:38 -0700 | [diff] [blame] | 575 | String systemServerFilter = SystemProperties.get( |
| 576 | "dalvik.vm.systemservercompilerfilter", "speed"); |
Andreas Gampe | e7bc152 | 2016-08-09 20:44:04 -0700 | [diff] [blame] | 577 | |
Jeff Sharkey | 740f523 | 2016-12-09 14:31:26 -0700 | [diff] [blame] | 578 | int dexoptNeeded; |
| 579 | try { |
| 580 | dexoptNeeded = DexFile.getDexOptNeeded( |
Chris Wailes | cd92db2 | 2019-01-11 14:30:43 -0800 | [diff] [blame] | 581 | classPathElement, instructionSet, systemServerFilter, |
| 582 | null /* classLoaderContext */, false /* newProfile */, |
| 583 | false /* downgrade */); |
Jeff Sharkey | 740f523 | 2016-12-09 14:31:26 -0700 | [diff] [blame] | 584 | } catch (FileNotFoundException ignored) { |
| 585 | // Do not add to the classpath. |
| 586 | Log.w(TAG, "Missing classpath element for system server: " + classPathElement); |
| 587 | continue; |
| 588 | } catch (IOException e) { |
| 589 | // Not fully clear what to do here as we don't know the cause of the |
| 590 | // IO exception. Add to the classpath to be conservative, but don't |
| 591 | // attempt to compile it. |
| 592 | Log.w(TAG, "Error checking classpath element for system server: " |
| 593 | + classPathElement, e); |
| 594 | dexoptNeeded = DexFile.NO_DEXOPT_NEEDED; |
Narayan Kamath | 29564cd | 2014-08-07 10:57:40 +0100 | [diff] [blame] | 595 | } |
Jeff Sharkey | 740f523 | 2016-12-09 14:31:26 -0700 | [diff] [blame] | 596 | |
| 597 | if (dexoptNeeded != DexFile.NO_DEXOPT_NEEDED) { |
| 598 | final String packageName = "*"; |
| 599 | final String outputPath = null; |
David Brazdil | f7e31c0 | 2018-02-13 17:04:26 +0000 | [diff] [blame] | 600 | final int dexFlags = 0; |
Mathieu Chartier | 5e07a0d | 2017-06-15 19:09:38 -0700 | [diff] [blame] | 601 | final String compilerFilter = systemServerFilter; |
Jeff Sharkey | 740f523 | 2016-12-09 14:31:26 -0700 | [diff] [blame] | 602 | final String uuid = StorageManager.UUID_PRIVATE_INTERNAL; |
Calin Juravle | 811a75a | 2017-04-05 22:49:38 -0700 | [diff] [blame] | 603 | final String seInfo = null; |
Calin Juravle | 2f09ff3 | 2017-07-24 15:29:56 -0700 | [diff] [blame] | 604 | final String classLoaderContext = |
| 605 | getSystemServerClassLoaderContext(classPathForElement); |
David Brazdil | 3d44ed0 | 2018-01-16 20:01:47 +0000 | [diff] [blame] | 606 | final int targetSdkVersion = 0; // SystemServer targets the system's SDK version |
Jeff Sharkey | 740f523 | 2016-12-09 14:31:26 -0700 | [diff] [blame] | 607 | try { |
| 608 | installd.dexopt(classPathElement, Process.SYSTEM_UID, packageName, |
| 609 | instructionSet, dexoptNeeded, outputPath, dexFlags, compilerFilter, |
David Brazdil | 3d44ed0 | 2018-01-16 20:01:47 +0000 | [diff] [blame] | 610 | uuid, classLoaderContext, seInfo, false /* downgrade */, |
Calin Juravle | 4bc8f4d | 2018-02-12 12:00:44 -0800 | [diff] [blame] | 611 | targetSdkVersion, /*profileName*/ null, /*dexMetadataPath*/ null, |
| 612 | "server-dexopt"); |
Jeff Sharkey | 740f523 | 2016-12-09 14:31:26 -0700 | [diff] [blame] | 613 | } catch (RemoteException | ServiceSpecificException e) { |
| 614 | // Ignore (but log), we need this on the classpath for fallback mode. |
| 615 | Log.w(TAG, "Failed compiling classpath element for system server: " |
| 616 | + classPathElement, e); |
| 617 | } |
| 618 | } |
| 619 | |
Calin Juravle | 2f09ff3 | 2017-07-24 15:29:56 -0700 | [diff] [blame] | 620 | classPathForElement = encodeSystemServerClassPath( |
| 621 | classPathForElement, classPathElement); |
Narayan Kamath | 29564cd | 2014-08-07 10:57:40 +0100 | [diff] [blame] | 622 | } |
| 623 | } |
| 624 | |
| 625 | /** |
Chris Wailes | cd92db2 | 2019-01-11 14:30:43 -0800 | [diff] [blame] | 626 | * Encodes the system server class loader context in a format that is accepted by dexopt. This |
| 627 | * assumes the system server is always loaded with a {@link dalvik.system.PathClassLoader}. |
Calin Juravle | 2f09ff3 | 2017-07-24 15:29:56 -0700 | [diff] [blame] | 628 | * |
| 629 | * Note that ideally we would use the {@code DexoptUtils} to compute this. However we have no |
| 630 | * dependency here on the server so we hard code the logic again. |
| 631 | */ |
| 632 | private static String getSystemServerClassLoaderContext(String classPath) { |
| 633 | return classPath == null ? "PCL[]" : "PCL[" + classPath + "]"; |
| 634 | } |
| 635 | |
| 636 | /** |
| 637 | * Encodes the class path in a format accepted by dexopt. |
Chris Wailes | cd92db2 | 2019-01-11 14:30:43 -0800 | [diff] [blame] | 638 | * |
| 639 | * @param classPath The old class path (may be empty). |
| 640 | * @param newElement The new class path elements |
| 641 | * @return The class path encoding resulted from appending {@code newElement} to {@code |
| 642 | * classPath}. |
Calin Juravle | 2f09ff3 | 2017-07-24 15:29:56 -0700 | [diff] [blame] | 643 | */ |
| 644 | private static String encodeSystemServerClassPath(String classPath, String newElement) { |
| 645 | return (classPath == null || classPath.isEmpty()) |
| 646 | ? newElement |
| 647 | : classPath + ":" + newElement; |
| 648 | } |
| 649 | |
| 650 | /** |
Narayan Kamath | bf99d06 | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 651 | * Prepare the arguments and forks for the system server process. |
| 652 | * |
Chris Wailes | cd92db2 | 2019-01-11 14:30:43 -0800 | [diff] [blame] | 653 | * @return A {@code Runnable} that provides an entrypoint into system_server code in the child |
| 654 | * process; {@code null} in the parent. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 655 | */ |
Narayan Kamath | bf99d06 | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 656 | private static Runnable forkSystemServer(String abiList, String socketName, |
| 657 | ZygoteServer zygoteServer) { |
Alex Klyubin | 48a06e7 | 2013-04-19 10:01:42 -0700 | [diff] [blame] | 658 | long capabilities = posixCapabilitiesAsBits( |
Chris Wailes | cd92db2 | 2019-01-11 14:30:43 -0800 | [diff] [blame] | 659 | OsConstants.CAP_IPC_LOCK, |
| 660 | OsConstants.CAP_KILL, |
| 661 | OsConstants.CAP_NET_ADMIN, |
| 662 | OsConstants.CAP_NET_BIND_SERVICE, |
| 663 | OsConstants.CAP_NET_BROADCAST, |
| 664 | OsConstants.CAP_NET_RAW, |
| 665 | OsConstants.CAP_SYS_MODULE, |
| 666 | OsConstants.CAP_SYS_NICE, |
| 667 | OsConstants.CAP_SYS_PTRACE, |
| 668 | OsConstants.CAP_SYS_TIME, |
| 669 | OsConstants.CAP_SYS_TTY_CONFIG, |
| 670 | OsConstants.CAP_WAKE_ALARM, |
| 671 | OsConstants.CAP_BLOCK_SUSPEND |
Alex Klyubin | 48a06e7 | 2013-04-19 10:01:42 -0700 | [diff] [blame] | 672 | ); |
Luis Hector Chavez | 72042c9 | 2017-07-12 10:03:30 -0700 | [diff] [blame] | 673 | /* Containers run without some capabilities, so drop any caps that are not available. */ |
| 674 | StructCapUserHeader header = new StructCapUserHeader( |
| 675 | OsConstants._LINUX_CAPABILITY_VERSION_3, 0); |
| 676 | StructCapUserData[] data; |
| 677 | try { |
| 678 | data = Os.capget(header); |
| 679 | } catch (ErrnoException ex) { |
| 680 | throw new RuntimeException("Failed to capget()", ex); |
Luis Hector Chavez | 5dd239a | 2015-10-06 14:01:45 -0700 | [diff] [blame] | 681 | } |
Luis Hector Chavez | 72042c9 | 2017-07-12 10:03:30 -0700 | [diff] [blame] | 682 | capabilities &= ((long) data[0].effective) | (((long) data[1].effective) << 32); |
| 683 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 684 | /* Hardcoded command line to start the system server */ |
| 685 | String args[] = { |
Chris Wailes | cd92db2 | 2019-01-11 14:30:43 -0800 | [diff] [blame] | 686 | "--setuid=1000", |
| 687 | "--setgid=1000", |
| 688 | "--setgroups=1001,1002,1003,1004,1005,1006,1007,1008,1009,1010,1018,1021,1023," |
| 689 | + "1024,1032,1065,3001,3002,3003,3006,3007,3009,3010", |
| 690 | "--capabilities=" + capabilities + "," + capabilities, |
| 691 | "--nice-name=system_server", |
| 692 | "--runtime-args", |
| 693 | "--target-sdk-version=" + VMRuntime.SDK_VERSION_CUR_DEVELOPMENT, |
| 694 | "com.android.server.SystemServer", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 695 | }; |
Chris Wailes | 682b479 | 2019-01-11 16:14:43 -0800 | [diff] [blame] | 696 | ZygoteArguments parsedArgs = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 697 | |
| 698 | int pid; |
| 699 | |
| 700 | try { |
Chris Wailes | 682b479 | 2019-01-11 16:14:43 -0800 | [diff] [blame] | 701 | parsedArgs = new ZygoteArguments(args); |
| 702 | Zygote.applyDebuggerSystemProperty(parsedArgs); |
| 703 | Zygote.applyInvokeWithSystemProperty(parsedArgs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 704 | |
Calin Juravle | 8eb891b | 2018-05-03 19:51:18 -0700 | [diff] [blame] | 705 | boolean profileSystemServer = SystemProperties.getBoolean( |
| 706 | "dalvik.vm.profilesystemserver", false); |
| 707 | if (profileSystemServer) { |
Chris Wailes | 682b479 | 2019-01-11 16:14:43 -0800 | [diff] [blame] | 708 | parsedArgs.mRuntimeFlags |= Zygote.PROFILE_SYSTEM_SERVER; |
Calin Juravle | 8eb891b | 2018-05-03 19:51:18 -0700 | [diff] [blame] | 709 | } |
| 710 | |
Mathieu Chartier | f5cd8c1 | 2019-02-04 13:28:36 -0800 | [diff] [blame] | 711 | String use_app_image_cache = SystemProperties.get( |
| 712 | PROPERTY_USE_APP_IMAGE_STARTUP_CACHE, ""); |
| 713 | // Property defaults to true currently. |
| 714 | if (!TextUtils.isEmpty(use_app_image_cache) && !use_app_image_cache.equals("false")) { |
| 715 | parsedArgs.mRuntimeFlags |= Zygote.USE_APP_IMAGE_STARTUP_CACHE; |
| 716 | } |
| 717 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 718 | /* Request to fork the system server process */ |
| 719 | pid = Zygote.forkSystemServer( |
Chris Wailes | 682b479 | 2019-01-11 16:14:43 -0800 | [diff] [blame] | 720 | parsedArgs.mUid, parsedArgs.mGid, |
| 721 | parsedArgs.mGids, |
| 722 | parsedArgs.mRuntimeFlags, |
Elliott Hughes | e1dfcb7 | 2011-07-08 11:08:07 -0700 | [diff] [blame] | 723 | null, |
Chris Wailes | 682b479 | 2019-01-11 16:14:43 -0800 | [diff] [blame] | 724 | parsedArgs.mPermittedCapabilities, |
| 725 | parsedArgs.mEffectiveCapabilities); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 726 | } catch (IllegalArgumentException ex) { |
| 727 | throw new RuntimeException(ex); |
Bob Lee | e540833 | 2009-09-04 18:31:17 -0700 | [diff] [blame] | 728 | } |
| 729 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 730 | /* For child process */ |
| 731 | if (pid == 0) { |
Narayan Kamath | 64cd907 | 2014-05-13 13:35:14 +0100 | [diff] [blame] | 732 | if (hasSecondZygote(abiList)) { |
| 733 | waitForSecondaryZygote(socketName); |
| 734 | } |
| 735 | |
Tobias Sargeant | dd4bb31 | 2016-01-19 16:34:54 +0000 | [diff] [blame] | 736 | zygoteServer.closeServerSocket(); |
Narayan Kamath | bf99d06 | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 737 | return handleSystemServerProcess(parsedArgs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 738 | } |
| 739 | |
Narayan Kamath | bf99d06 | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 740 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 741 | } |
| 742 | |
Alex Klyubin | 48a06e7 | 2013-04-19 10:01:42 -0700 | [diff] [blame] | 743 | /** |
| 744 | * Gets the bit array representation of the provided list of POSIX capabilities. |
| 745 | */ |
| 746 | private static long posixCapabilitiesAsBits(int... capabilities) { |
| 747 | long result = 0; |
| 748 | for (int capability : capabilities) { |
| 749 | if ((capability < 0) || (capability > OsConstants.CAP_LAST_CAP)) { |
| 750 | throw new IllegalArgumentException(String.valueOf(capability)); |
| 751 | } |
| 752 | result |= (1L << capability); |
| 753 | } |
| 754 | return result; |
| 755 | } |
| 756 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 757 | public static void main(String argv[]) { |
Tobias Sargeant | dd4bb31 | 2016-01-19 16:34:54 +0000 | [diff] [blame] | 758 | ZygoteServer zygoteServer = new ZygoteServer(); |
| 759 | |
Andreas Gampe | 1ef8aef | 2016-04-11 08:39:52 -0700 | [diff] [blame] | 760 | // Mark zygote start. This ensures that thread creation will throw |
| 761 | // an error. |
| 762 | ZygoteHooks.startZygoteNoThreadCreation(); |
| 763 | |
Robert Sesek | e4f8d69 | 2016-09-13 19:13:01 -0400 | [diff] [blame] | 764 | // Zygote goes into its own process group. |
| 765 | try { |
| 766 | Os.setpgid(0, 0); |
| 767 | } catch (ErrnoException ex) { |
| 768 | throw new RuntimeException("Failed to setpgid(0,0)", ex); |
| 769 | } |
| 770 | |
Chris Wailes | cffbf1c | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 771 | Runnable caller; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 772 | try { |
Fyodor Kupolov | 6733e6c | 2017-01-06 18:27:05 -0800 | [diff] [blame] | 773 | // Report Zygote start time to tron unless it is a runtime restart |
| 774 | if (!"1".equals(SystemProperties.get("sys.boot_completed"))) { |
| 775 | MetricsLogger.histogram(null, "boot_zygote_init", |
| 776 | (int) SystemClock.elapsedRealtime()); |
| 777 | } |
Fyodor Kupolov | 3235e0c | 2016-11-17 11:44:49 -0800 | [diff] [blame] | 778 | |
| 779 | String bootTimeTag = Process.is64Bit() ? "Zygote64Timing" : "Zygote32Timing"; |
Fyodor Kupolov | 6e3461b | 2017-08-10 17:00:43 -0700 | [diff] [blame] | 780 | TimingsTraceLog bootTimingsTraceLog = new TimingsTraceLog(bootTimeTag, |
Fyodor Kupolov | 3235e0c | 2016-11-17 11:44:49 -0800 | [diff] [blame] | 781 | Trace.TRACE_TAG_DALVIK); |
| 782 | bootTimingsTraceLog.traceBegin("ZygoteInit"); |
Yasuhiro Matsuda | 25878b2 | 2015-09-03 16:18:21 +0900 | [diff] [blame] | 783 | RuntimeInit.enableDdms(); |
Yasuhiro Matsuda | 25878b2 | 2015-09-03 16:18:21 +0900 | [diff] [blame] | 784 | |
Narayan Kamath | c41638c | 2014-04-07 13:56:15 +0100 | [diff] [blame] | 785 | boolean startSystemServer = false; |
| 786 | String socketName = "zygote"; |
| 787 | String abiList = null; |
Narayan Kamath | 5a3d0c6 | 2016-11-09 15:35:18 +0000 | [diff] [blame] | 788 | boolean enableLazyPreload = false; |
Yasuhiro Matsuda | 25878b2 | 2015-09-03 16:18:21 +0900 | [diff] [blame] | 789 | for (int i = 1; i < argv.length; i++) { |
| 790 | if ("start-system-server".equals(argv[i])) { |
| 791 | startSystemServer = true; |
Narayan Kamath | 5a3d0c6 | 2016-11-09 15:35:18 +0000 | [diff] [blame] | 792 | } else if ("--enable-lazy-preload".equals(argv[i])) { |
| 793 | enableLazyPreload = true; |
Yasuhiro Matsuda | 25878b2 | 2015-09-03 16:18:21 +0900 | [diff] [blame] | 794 | } else if (argv[i].startsWith(ABI_LIST_ARG)) { |
| 795 | abiList = argv[i].substring(ABI_LIST_ARG.length()); |
| 796 | } else if (argv[i].startsWith(SOCKET_NAME_ARG)) { |
| 797 | socketName = argv[i].substring(SOCKET_NAME_ARG.length()); |
| 798 | } else { |
| 799 | throw new RuntimeException("Unknown command line argument: " + argv[i]); |
Narayan Kamath | c41638c | 2014-04-07 13:56:15 +0100 | [diff] [blame] | 800 | } |
| 801 | } |
| 802 | |
Yasuhiro Matsuda | 25878b2 | 2015-09-03 16:18:21 +0900 | [diff] [blame] | 803 | if (abiList == null) { |
| 804 | throw new RuntimeException("No ABI list supplied."); |
| 805 | } |
| 806 | |
Chris Wailes | cffbf1c | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 807 | // TODO (chriswailes): Wrap these three calls in a helper function? |
| 808 | final String blastulaSocketName = |
| 809 | socketName.equals(ZygoteProcess.ZYGOTE_SOCKET_NAME) |
| 810 | ? ZygoteProcess.BLASTULA_POOL_SOCKET_NAME |
| 811 | : ZygoteProcess.BLASTULA_POOL_SECONDARY_SOCKET_NAME; |
| 812 | |
| 813 | zygoteServer.createZygoteSocket(socketName); |
| 814 | Zygote.createBlastulaSocket(blastulaSocketName); |
| 815 | |
| 816 | Zygote.getSocketFDs(socketName.equals(ZygoteProcess.ZYGOTE_SOCKET_NAME)); |
| 817 | |
Narayan Kamath | 5a3d0c6 | 2016-11-09 15:35:18 +0000 | [diff] [blame] | 818 | // In some configurations, we avoid preloading resources and classes eagerly. |
| 819 | // In such cases, we will preload things prior to our first fork. |
| 820 | if (!enableLazyPreload) { |
| 821 | bootTimingsTraceLog.traceBegin("ZygotePreload"); |
| 822 | EventLog.writeEvent(LOG_BOOT_PROGRESS_PRELOAD_START, |
Chris Wailes | cd92db2 | 2019-01-11 14:30:43 -0800 | [diff] [blame] | 823 | SystemClock.uptimeMillis()); |
Narayan Kamath | 5a3d0c6 | 2016-11-09 15:35:18 +0000 | [diff] [blame] | 824 | preload(bootTimingsTraceLog); |
| 825 | EventLog.writeEvent(LOG_BOOT_PROGRESS_PRELOAD_END, |
Chris Wailes | cd92db2 | 2019-01-11 14:30:43 -0800 | [diff] [blame] | 826 | SystemClock.uptimeMillis()); |
Narayan Kamath | 5a3d0c6 | 2016-11-09 15:35:18 +0000 | [diff] [blame] | 827 | bootTimingsTraceLog.traceEnd(); // ZygotePreload |
Narayan Kamath | b49996d | 2017-02-06 20:24:08 +0000 | [diff] [blame] | 828 | } else { |
Hiroshi Yamauchi | 1e3db87 | 2017-03-02 13:39:07 -0800 | [diff] [blame] | 829 | Zygote.resetNicePriority(); |
Narayan Kamath | 5a3d0c6 | 2016-11-09 15:35:18 +0000 | [diff] [blame] | 830 | } |
Yasuhiro Matsuda | 25878b2 | 2015-09-03 16:18:21 +0900 | [diff] [blame] | 831 | |
Yasuhiro Matsuda | 25878b2 | 2015-09-03 16:18:21 +0900 | [diff] [blame] | 832 | // Do an initial gc to clean up after startup |
Fyodor Kupolov | 3235e0c | 2016-11-17 11:44:49 -0800 | [diff] [blame] | 833 | bootTimingsTraceLog.traceBegin("PostZygoteInitGC"); |
Yasuhiro Matsuda | 25878b2 | 2015-09-03 16:18:21 +0900 | [diff] [blame] | 834 | gcAndFinalize(); |
Fyodor Kupolov | 3235e0c | 2016-11-17 11:44:49 -0800 | [diff] [blame] | 835 | bootTimingsTraceLog.traceEnd(); // PostZygoteInitGC |
Yasuhiro Matsuda | 25878b2 | 2015-09-03 16:18:21 +0900 | [diff] [blame] | 836 | |
Fyodor Kupolov | 3235e0c | 2016-11-17 11:44:49 -0800 | [diff] [blame] | 837 | bootTimingsTraceLog.traceEnd(); // ZygoteInit |
Jamie Gennis | 6ad0452 | 2013-04-15 18:53:24 -0700 | [diff] [blame] | 838 | // Disable tracing so that forked processes do not inherit stale tracing tags from |
| 839 | // Zygote. |
John Reck | 62cc119 | 2017-05-12 15:39:51 -0700 | [diff] [blame] | 840 | Trace.setTracingEnabled(false, 0); |
Jamie Gennis | 6ad0452 | 2013-04-15 18:53:24 -0700 | [diff] [blame] | 841 | |
Victor Hsieh | c8176ef | 2018-01-08 12:43:00 -0800 | [diff] [blame] | 842 | Zygote.nativeSecurityInit(); |
| 843 | |
doheon1.lee | 885b742 | 2016-01-20 13:07:27 +0900 | [diff] [blame] | 844 | // Zygote process unmounts root storage spaces. |
| 845 | Zygote.nativeUnmountStorageOnInit(); |
| 846 | |
Andreas Gampe | 1ef8aef | 2016-04-11 08:39:52 -0700 | [diff] [blame] | 847 | ZygoteHooks.stopZygoteNoThreadCreation(); |
| 848 | |
Narayan Kamath | c41638c | 2014-04-07 13:56:15 +0100 | [diff] [blame] | 849 | if (startSystemServer) { |
Narayan Kamath | bf99d06 | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 850 | Runnable r = forkSystemServer(abiList, socketName, zygoteServer); |
| 851 | |
| 852 | // {@code r == null} in the parent (zygote) process, and {@code r != null} in the |
| 853 | // child (system_server) process. |
| 854 | if (r != null) { |
| 855 | r.run(); |
| 856 | return; |
| 857 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 858 | } |
| 859 | |
Chris Wailes | cffbf1c | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 860 | // If the return value is null then this is the zygote process |
| 861 | // returning to the normal control flow. If it returns a Runnable |
| 862 | // object then this is a blastula that has finished specializing. |
| 863 | caller = Zygote.initBlastulaPool(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 864 | |
Chris Wailes | cffbf1c | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 865 | if (caller == null) { |
| 866 | Log.i(TAG, "Accepting command socket connections"); |
| 867 | |
| 868 | // The select loop returns early in the child process after a fork and |
| 869 | // loops forever in the zygote. |
| 870 | caller = zygoteServer.runSelectLoop(abiList); |
| 871 | } |
Christopher Ferris | ea8b5de | 2016-06-17 15:29:58 -0700 | [diff] [blame] | 872 | } catch (Throwable ex) { |
Tobias Sargeant | dd4bb31 | 2016-01-19 16:34:54 +0000 | [diff] [blame] | 873 | Log.e(TAG, "System zygote died with exception", ex); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 874 | throw ex; |
Narayan Kamath | bf99d06 | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 875 | } finally { |
| 876 | zygoteServer.closeServerSocket(); |
| 877 | } |
| 878 | |
| 879 | // We're in the child process and have exited the select loop. Proceed to execute the |
| 880 | // command. |
| 881 | if (caller != null) { |
| 882 | caller.run(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 883 | } |
| 884 | } |
| 885 | |
| 886 | /** |
Narayan Kamath | 64cd907 | 2014-05-13 13:35:14 +0100 | [diff] [blame] | 887 | * Return {@code true} if this device configuration has another zygote. |
| 888 | * |
Chris Wailes | cd92db2 | 2019-01-11 14:30:43 -0800 | [diff] [blame] | 889 | * We determine this by comparing the device ABI list with this zygotes list. If this zygote |
| 890 | * supports all ABIs this device supports, there won't be another zygote. |
Narayan Kamath | 64cd907 | 2014-05-13 13:35:14 +0100 | [diff] [blame] | 891 | */ |
| 892 | private static boolean hasSecondZygote(String abiList) { |
| 893 | return !SystemProperties.get("ro.product.cpu.abilist").equals(abiList); |
| 894 | } |
| 895 | |
| 896 | private static void waitForSecondaryZygote(String socketName) { |
Chris Wailes | 6e3c7ce | 2019-01-11 13:19:20 -0800 | [diff] [blame] | 897 | String otherZygoteName = ZygoteProcess.ZYGOTE_SOCKET_NAME.equals(socketName) |
| 898 | ? ZygoteProcess.ZYGOTE_SECONDARY_SOCKET_NAME : ZygoteProcess.ZYGOTE_SOCKET_NAME; |
Gustav Sennton | f0c52b5 | 2017-04-27 17:00:50 +0100 | [diff] [blame] | 899 | ZygoteProcess.waitForConnectionToZygote(otherZygoteName); |
Narayan Kamath | 64cd907 | 2014-05-13 13:35:14 +0100 | [diff] [blame] | 900 | } |
| 901 | |
Narayan Kamath | 669afcc | 2017-02-06 20:24:08 +0000 | [diff] [blame] | 902 | static boolean isPreloadComplete() { |
| 903 | return sPreloadComplete; |
| 904 | } |
| 905 | |
Narayan Kamath | 64cd907 | 2014-05-13 13:35:14 +0100 | [diff] [blame] | 906 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 907 | * Class not instantiable. |
| 908 | */ |
| 909 | private ZygoteInit() { |
| 910 | } |
Andreas Gampe | 76d4fc8 | 2017-02-07 19:44:37 -0800 | [diff] [blame] | 911 | |
| 912 | /** |
Chris Wailes | cd92db2 | 2019-01-11 14:30:43 -0800 | [diff] [blame] | 913 | * The main function called when started through the zygote process. This could be unified with |
| 914 | * main(), if the native code in nativeFinishInit() were rationalized with Zygote startup.<p> |
Andreas Gampe | 76d4fc8 | 2017-02-07 19:44:37 -0800 | [diff] [blame] | 915 | * |
| 916 | * Current recognized args: |
| 917 | * <ul> |
| 918 | * <li> <code> [--] <start class name> <args> |
| 919 | * </ul> |
| 920 | * |
| 921 | * @param targetSdkVersion target SDK version |
| 922 | * @param argv arg strings |
| 923 | */ |
Chris Wailes | cd92db2 | 2019-01-11 14:30:43 -0800 | [diff] [blame] | 924 | public static final Runnable zygoteInit(int targetSdkVersion, String[] argv, |
| 925 | ClassLoader classLoader) { |
Andreas Gampe | 76d4fc8 | 2017-02-07 19:44:37 -0800 | [diff] [blame] | 926 | if (RuntimeInit.DEBUG) { |
| 927 | Slog.d(RuntimeInit.TAG, "RuntimeInit: Starting application from zygote"); |
| 928 | } |
| 929 | |
| 930 | Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "ZygoteInit"); |
| 931 | RuntimeInit.redirectLogStreams(); |
| 932 | |
| 933 | RuntimeInit.commonInit(); |
| 934 | ZygoteInit.nativeZygoteInit(); |
Narayan Kamath | bf99d06 | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 935 | return RuntimeInit.applicationInit(targetSdkVersion, argv, classLoader); |
Andreas Gampe | 76d4fc8 | 2017-02-07 19:44:37 -0800 | [diff] [blame] | 936 | } |
| 937 | |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 938 | /** |
Chris Wailes | cd92db2 | 2019-01-11 14:30:43 -0800 | [diff] [blame] | 939 | * The main function called when starting a child zygote process. This is used as an alternative |
| 940 | * to zygoteInit(), which skips calling into initialization routines that start the Binder |
| 941 | * threadpool. |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 942 | */ |
| 943 | static final Runnable childZygoteInit( |
| 944 | int targetSdkVersion, String[] argv, ClassLoader classLoader) { |
| 945 | RuntimeInit.Arguments args = new RuntimeInit.Arguments(argv); |
| 946 | return RuntimeInit.findStaticMain(args.startClass, args.startArgs, classLoader); |
| 947 | } |
| 948 | |
Andreas Gampe | 76d4fc8 | 2017-02-07 19:44:37 -0800 | [diff] [blame] | 949 | private static final native void nativeZygoteInit(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 950 | } |