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