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