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