Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 19 | import static android.system.OsConstants.O_CLOEXEC; |
| 20 | |
Chris Wailes | 2be2626 | 2019-01-11 16:14:43 -0800 | [diff] [blame] | 21 | import static com.android.internal.os.ZygoteConnectionConstants.MAX_ZYGOTE_ARGC; |
| 22 | |
Martijn Coenen | 4b98816 | 2019-01-28 13:58:45 +0100 | [diff] [blame] | 23 | import android.content.pm.ApplicationInfo; |
Chris Wailes | 2be2626 | 2019-01-11 16:14:43 -0800 | [diff] [blame] | 24 | import android.net.Credentials; |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 25 | import android.net.LocalServerSocket; |
| 26 | import android.net.LocalSocket; |
Jeff Vander Stoep | 739c0b5 | 2019-03-25 20:27:52 -0700 | [diff] [blame] | 27 | import android.os.Build; |
Chris Wailes | 2be2626 | 2019-01-11 16:14:43 -0800 | [diff] [blame] | 28 | import android.os.FactoryTest; |
Jeff Sharkey | ace874b | 2017-09-07 15:27:33 -0600 | [diff] [blame] | 29 | import android.os.IVold; |
Chris Wailes | 2be2626 | 2019-01-11 16:14:43 -0800 | [diff] [blame] | 30 | import android.os.Process; |
| 31 | import android.os.SystemProperties; |
Narayan Kamath | fbb32f6 | 2015-06-12 15:34:35 +0100 | [diff] [blame] | 32 | import android.os.Trace; |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 33 | import android.provider.DeviceConfig; |
Elliott Hughes | 860c591 | 2014-04-28 19:19:13 -0700 | [diff] [blame] | 34 | import android.system.ErrnoException; |
| 35 | import android.system.Os; |
Chris Wailes | 2be2626 | 2019-01-11 16:14:43 -0800 | [diff] [blame] | 36 | import android.util.Log; |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 37 | |
Jeff Sharkey | ace874b | 2017-09-07 15:27:33 -0600 | [diff] [blame] | 38 | import dalvik.system.ZygoteHooks; |
Tobias Sargeant | b9679dc | 2016-01-19 16:34:54 +0000 | [diff] [blame] | 39 | |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 40 | import libcore.io.IoUtils; |
| 41 | |
Chris Wailes | 2be2626 | 2019-01-11 16:14:43 -0800 | [diff] [blame] | 42 | import java.io.BufferedReader; |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 43 | import java.io.ByteArrayOutputStream; |
| 44 | import java.io.DataOutputStream; |
| 45 | import java.io.FileDescriptor; |
Chris Wailes | 2be2626 | 2019-01-11 16:14:43 -0800 | [diff] [blame] | 46 | import java.io.IOException; |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 47 | import java.io.InputStreamReader; |
Chris Wailes | 2be2626 | 2019-01-11 16:14:43 -0800 | [diff] [blame] | 48 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 49 | /** @hide */ |
| 50 | public final class Zygote { |
| 51 | /* |
Nicolas Geoffray | 81edac4 | 2017-09-07 14:13:29 +0100 | [diff] [blame] | 52 | * Bit values for "runtimeFlags" argument. The definitions are duplicated |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 53 | * in the native code. |
| 54 | */ |
| 55 | |
| 56 | /** enable debugging over JDWP */ |
Nicolas Geoffray | 347b1df | 2016-12-20 14:05:05 +0000 | [diff] [blame] | 57 | public static final int DEBUG_ENABLE_JDWP = 1; |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 58 | /** enable JNI checks */ |
| 59 | public static final int DEBUG_ENABLE_CHECKJNI = 1 << 1; |
| 60 | /** enable Java programming language "assert" statements */ |
| 61 | public static final int DEBUG_ENABLE_ASSERT = 1 << 2; |
Mathieu Chartier | 7a49028 | 2015-03-17 09:51:36 -0700 | [diff] [blame] | 62 | /** disable the AOT compiler and JIT */ |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 63 | public static final int DEBUG_ENABLE_SAFEMODE = 1 << 3; |
| 64 | /** Enable logging of third-party JNI activity. */ |
| 65 | public static final int DEBUG_ENABLE_JNI_LOGGING = 1 << 4; |
David Srbecky | 065075e | 2015-05-28 17:16:09 +0100 | [diff] [blame] | 66 | /** Force generation of native debugging information. */ |
Nicolas Geoffray | 9abbf45 | 2015-11-05 11:29:42 +0000 | [diff] [blame] | 67 | public static final int DEBUG_GENERATE_DEBUG_INFO = 1 << 5; |
Tamas Berghammer | df6cb28 | 2016-01-29 12:07:00 +0000 | [diff] [blame] | 68 | /** Always use JIT-ed code. */ |
| 69 | public static final int DEBUG_ALWAYS_JIT = 1 << 6; |
Nicolas Geoffray | 347b1df | 2016-12-20 14:05:05 +0000 | [diff] [blame] | 70 | /** Make the code native debuggable by turning off some optimizations. */ |
Tamas Berghammer | df6cb28 | 2016-01-29 12:07:00 +0000 | [diff] [blame] | 71 | public static final int DEBUG_NATIVE_DEBUGGABLE = 1 << 7; |
Nicolas Geoffray | 347b1df | 2016-12-20 14:05:05 +0000 | [diff] [blame] | 72 | /** Make the code Java debuggable by turning off some optimizations. */ |
| 73 | public static final int DEBUG_JAVA_DEBUGGABLE = 1 << 8; |
Mathieu Chartier | 7a49028 | 2015-03-17 09:51:36 -0700 | [diff] [blame] | 74 | |
Nicolas Geoffray | 1f88ad6 | 2017-09-13 14:21:00 +0100 | [diff] [blame] | 75 | /** Turn off the verifier. */ |
| 76 | public static final int DISABLE_VERIFIER = 1 << 9; |
| 77 | /** Only use oat files located in /system. Otherwise use dex/jar/apk . */ |
| 78 | public static final int ONLY_USE_SYSTEM_OAT_FILES = 1 << 10; |
David Srbecky | 156ed92 | 2018-01-30 14:37:37 +0000 | [diff] [blame] | 79 | /** Force generation of native debugging information for backtraces. */ |
Mathew Inwood | 16073b8 | 2018-03-23 10:05:01 +0000 | [diff] [blame] | 80 | public static final int DEBUG_GENERATE_MINI_DEBUG_INFO = 1 << 11; |
| 81 | /** |
| 82 | * Hidden API access restrictions. This is a mask for bits representing the API enforcement |
| 83 | * policy, defined by {@code @ApplicationInfo.HiddenApiEnforcementPolicy}. |
| 84 | */ |
| 85 | public static final int API_ENFORCEMENT_POLICY_MASK = (1 << 12) | (1 << 13); |
| 86 | /** |
| 87 | * Bit shift for use with {@link #API_ENFORCEMENT_POLICY_MASK}. |
| 88 | * |
| 89 | * (flags & API_ENFORCEMENT_POLICY_MASK) >> API_ENFORCEMENT_POLICY_SHIFT gives |
| 90 | * @ApplicationInfo.ApiEnforcementPolicy values. |
| 91 | */ |
| 92 | public static final int API_ENFORCEMENT_POLICY_SHIFT = |
| 93 | Integer.numberOfTrailingZeros(API_ENFORCEMENT_POLICY_MASK); |
Calin Juravle | 8eb891b | 2018-05-03 19:51:18 -0700 | [diff] [blame] | 94 | /** |
| 95 | * Enable system server ART profiling. |
| 96 | */ |
| 97 | public static final int PROFILE_SYSTEM_SERVER = 1 << 14; |
Nicolas Geoffray | 1f88ad6 | 2017-09-13 14:21:00 +0100 | [diff] [blame] | 98 | |
Yabin Cui | 4d8546d | 2019-01-29 16:29:20 -0800 | [diff] [blame] | 99 | /** |
| 100 | * Enable profiling from shell. |
| 101 | */ |
| 102 | public static final int PROFILE_FROM_SHELL = 1 << 15; |
| 103 | |
Mathieu Chartier | ced7e08 | 2019-02-04 13:28:36 -0800 | [diff] [blame] | 104 | /* |
| 105 | * Enable using the ART app image startup cache |
| 106 | */ |
| 107 | public static final int USE_APP_IMAGE_STARTUP_CACHE = 1 << 16; |
| 108 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 109 | /** No external storage should be mounted. */ |
Jeff Sharkey | ace874b | 2017-09-07 15:27:33 -0600 | [diff] [blame] | 110 | public static final int MOUNT_EXTERNAL_NONE = IVold.REMOUNT_MODE_NONE; |
Jeff Sharkey | 9527b22 | 2015-06-24 15:24:48 -0700 | [diff] [blame] | 111 | /** Default external storage should be mounted. */ |
Jeff Sharkey | ace874b | 2017-09-07 15:27:33 -0600 | [diff] [blame] | 112 | public static final int MOUNT_EXTERNAL_DEFAULT = IVold.REMOUNT_MODE_DEFAULT; |
Jeff Sharkey | 9527b22 | 2015-06-24 15:24:48 -0700 | [diff] [blame] | 113 | /** Read-only external storage should be mounted. */ |
Jeff Sharkey | ace874b | 2017-09-07 15:27:33 -0600 | [diff] [blame] | 114 | public static final int MOUNT_EXTERNAL_READ = IVold.REMOUNT_MODE_READ; |
Jeff Sharkey | 9527b22 | 2015-06-24 15:24:48 -0700 | [diff] [blame] | 115 | /** Read-write external storage should be mounted. */ |
Jeff Sharkey | ace874b | 2017-09-07 15:27:33 -0600 | [diff] [blame] | 116 | public static final int MOUNT_EXTERNAL_WRITE = IVold.REMOUNT_MODE_WRITE; |
Sudheer Shanka | 3a0df3b | 2018-12-12 12:43:43 -0800 | [diff] [blame] | 117 | /** |
Sudheer Shanka | 0b6da53 | 2019-01-09 12:06:51 -0800 | [diff] [blame] | 118 | * Mount mode for apps that are already installed on the device before the isolated_storage |
| 119 | * feature is enabled. |
| 120 | */ |
| 121 | public static final int MOUNT_EXTERNAL_LEGACY = IVold.REMOUNT_MODE_LEGACY; |
| 122 | /** |
Sudheer Shanka | 3a0df3b | 2018-12-12 12:43:43 -0800 | [diff] [blame] | 123 | * Mount mode for package installers which should give them access to |
| 124 | * all obb dirs in addition to their package sandboxes |
| 125 | */ |
| 126 | public static final int MOUNT_EXTERNAL_INSTALLER = IVold.REMOUNT_MODE_INSTALLER; |
Sudheer Shanka | 98cb3f0 | 2018-08-17 16:10:29 -0700 | [diff] [blame] | 127 | /** Read-write external storage should be mounted instead of package sandbox */ |
| 128 | public static final int MOUNT_EXTERNAL_FULL = IVold.REMOUNT_MODE_FULL; |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 129 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 130 | /** Number of bytes sent to the Zygote over USAP pipes or the pool event FD */ |
| 131 | public static final int USAP_MANAGEMENT_MESSAGE_BYTES = 8; |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 132 | |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 133 | /** |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 134 | * An extraArg passed when a zygote process is forking a child-zygote, specifying a name |
| 135 | * in the abstract socket namespace. This socket name is what the new child zygote |
| 136 | * should listen for connections on. |
| 137 | */ |
| 138 | public static final String CHILD_ZYGOTE_SOCKET_NAME_ARG = "--zygote-socket="; |
| 139 | |
Martijn Coenen | 7e6fa67 | 2018-11-05 11:45:26 +0100 | [diff] [blame] | 140 | /** |
| 141 | * An extraArg passed when a zygote process is forking a child-zygote, specifying the |
| 142 | * requested ABI for the child Zygote. |
| 143 | */ |
| 144 | public static final String CHILD_ZYGOTE_ABI_LIST_ARG = "--abi-list="; |
| 145 | |
Martijn Coenen | 86f08a5 | 2019-01-03 16:23:01 +0100 | [diff] [blame] | 146 | /** |
| 147 | * An extraArg passed when a zygote process is forking a child-zygote, specifying the |
| 148 | * start of the UID range the children of the Zygote may setuid()/setgid() to. This |
| 149 | * will be enforced with a seccomp filter. |
| 150 | */ |
| 151 | public static final String CHILD_ZYGOTE_UID_RANGE_START = "--uid-range-start="; |
| 152 | |
| 153 | /** |
| 154 | * An extraArg passed when a zygote process is forking a child-zygote, specifying the |
| 155 | * end of the UID range the children of the Zygote may setuid()/setgid() to. This |
| 156 | * will be enforced with a seccomp filter. |
| 157 | */ |
| 158 | public static final String CHILD_ZYGOTE_UID_RANGE_END = "--uid-range-end="; |
| 159 | |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 160 | /** Prefix prepended to socket names created by init */ |
| 161 | private static final String ANDROID_SOCKET_PREFIX = "ANDROID_SOCKET_"; |
| 162 | |
| 163 | /** |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 164 | * The duration to wait before re-checking Zygote related system properties. |
| 165 | * |
Chris Wailes | db132a3 | 2019-02-20 10:49:27 -0800 | [diff] [blame] | 166 | * One minute in milliseconds. |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 167 | */ |
Chris Wailes | db132a3 | 2019-02-20 10:49:27 -0800 | [diff] [blame] | 168 | public static final long PROPERTY_CHECK_INTERVAL = 60000; |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 169 | |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 170 | /** |
| 171 | * @hide for internal use only |
| 172 | */ |
| 173 | public static final int SOCKET_BUFFER_SIZE = 256; |
| 174 | |
Chris Wailes | 2be2626 | 2019-01-11 16:14:43 -0800 | [diff] [blame] | 175 | /** a prototype instance for a future List.toArray() */ |
| 176 | protected static final int[][] INT_ARRAY_2D = new int[0][0]; |
| 177 | |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 178 | /** |
| 179 | * @hide for internal use only. |
| 180 | */ |
| 181 | public static final String PRIMARY_SOCKET_NAME = "zygote"; |
| 182 | |
| 183 | /** |
| 184 | * @hide for internal use only. |
| 185 | */ |
| 186 | public static final String SECONDARY_SOCKET_NAME = "zygote_secondary"; |
| 187 | |
| 188 | /** |
| 189 | * @hide for internal use only |
| 190 | */ |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 191 | public static final String USAP_POOL_PRIMARY_SOCKET_NAME = "usap_pool_primary"; |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 192 | |
| 193 | /** |
| 194 | * @hide for internal use only |
| 195 | */ |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 196 | public static final String USAP_POOL_SECONDARY_SOCKET_NAME = "usap_pool_secondary"; |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 197 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 198 | private Zygote() {} |
| 199 | |
| 200 | /** |
| 201 | * Forks a new VM instance. The current VM must have been started |
| 202 | * with the -Xzygote flag. <b>NOTE: new instance keeps all |
| 203 | * root capabilities. The new process is expected to call capset()</b>. |
| 204 | * |
| 205 | * @param uid the UNIX uid that the new process should setuid() to after |
| 206 | * fork()ing and and before spawning any threads. |
| 207 | * @param gid the UNIX gid that the new process should setgid() to after |
| 208 | * fork()ing and and before spawning any threads. |
| 209 | * @param gids null-ok; a list of UNIX gids that the new process should |
| 210 | * setgroups() to after fork and before spawning any threads. |
Nicolas Geoffray | 81edac4 | 2017-09-07 14:13:29 +0100 | [diff] [blame] | 211 | * @param runtimeFlags bit flags that enable ART features. |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 212 | * @param rlimits null-ok an array of rlimit tuples, with the second |
| 213 | * dimension having a length of 3 and representing |
| 214 | * (resource, rlim_cur, rlim_max). These are set via the posix |
| 215 | * setrlimit(2) call. |
| 216 | * @param seInfo null-ok a string specifying SELinux information for |
| 217 | * the new process. |
| 218 | * @param niceName null-ok a string specifying the process name. |
| 219 | * @param fdsToClose an array of ints, holding one or more POSIX |
| 220 | * file descriptor numbers that are to be closed by the child |
| 221 | * (and replaced by /dev/null) after forking. An integer value |
| 222 | * of -1 in any entry in the array means "ignore this one". |
Andreas Gampe | 8dfa178 | 2017-01-05 12:45:58 -0800 | [diff] [blame] | 223 | * @param fdsToIgnore null-ok an array of ints, either null or holding |
| 224 | * one or more POSIX file descriptor numbers that are to be ignored |
| 225 | * in the file descriptor table check. |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 226 | * @param startChildZygote if true, the new child process will itself be a |
| 227 | * new zygote process. |
Andreas Gampe | aec67dc | 2014-09-02 21:23:06 -0700 | [diff] [blame] | 228 | * @param instructionSet null-ok the instruction set to use. |
jgu21 | 2eacd06 | 2014-09-10 06:55:07 -0400 | [diff] [blame] | 229 | * @param appDataDir null-ok the data directory of the app. |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 230 | * |
| 231 | * @return 0 if this is the child, pid of the child |
| 232 | * if this is the parent, or -1 on error. |
| 233 | */ |
Nicolas Geoffray | 81edac4 | 2017-09-07 14:13:29 +0100 | [diff] [blame] | 234 | public static int forkAndSpecialize(int uid, int gid, int[] gids, int runtimeFlags, |
Sudheer Shanka | 3f0645b | 2018-09-18 13:07:59 -0700 | [diff] [blame] | 235 | int[][] rlimits, int mountExternal, String seInfo, String niceName, int[] fdsToClose, |
| 236 | int[] fdsToIgnore, boolean startChildZygote, String instructionSet, String appDataDir, |
Jeff Vander Stoep | 739c0b5 | 2019-03-25 20:27:52 -0700 | [diff] [blame] | 237 | String packageName, String[] packagesForUID, String sandboxId, int targetSdkVersion) { |
Neil Fuller | 555d8b7 | 2019-01-28 18:29:20 +0000 | [diff] [blame] | 238 | ZygoteHooks.preFork(); |
Hiroshi Yamauchi | 1e3db87 | 2017-03-02 13:39:07 -0800 | [diff] [blame] | 239 | // Resets nice priority for zygote process. |
| 240 | resetNicePriority(); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 241 | int pid = nativeForkAndSpecialize( |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 242 | uid, gid, gids, runtimeFlags, rlimits, mountExternal, seInfo, niceName, fdsToClose, |
| 243 | fdsToIgnore, startChildZygote, instructionSet, appDataDir, packageName, |
Sudheer Shanka | e51005d | 2019-02-24 10:24:09 -0800 | [diff] [blame] | 244 | packagesForUID, sandboxId); |
Narayan Kamath | fbb32f6 | 2015-06-12 15:34:35 +0100 | [diff] [blame] | 245 | // Enable tracing as soon as possible for the child process. |
| 246 | if (pid == 0) { |
Jeff Vander Stoep | 739c0b5 | 2019-03-25 20:27:52 -0700 | [diff] [blame] | 247 | Zygote.disableExecuteOnly(targetSdkVersion); |
Andreas Gampe | 8f4eab2 | 2017-09-13 18:16:13 -0700 | [diff] [blame] | 248 | Trace.setTracingEnabled(true, runtimeFlags); |
Narayan Kamath | fbb32f6 | 2015-06-12 15:34:35 +0100 | [diff] [blame] | 249 | |
| 250 | // Note that this event ends at the end of handleChildProc, |
| 251 | Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "PostFork"); |
| 252 | } |
Neil Fuller | 555d8b7 | 2019-01-28 18:29:20 +0000 | [diff] [blame] | 253 | ZygoteHooks.postForkCommon(); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 254 | return pid; |
| 255 | } |
| 256 | |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 257 | private static native int nativeForkAndSpecialize(int uid, int gid, int[] gids, |
| 258 | int runtimeFlags, int[][] rlimits, int mountExternal, String seInfo, String niceName, |
| 259 | int[] fdsToClose, int[] fdsToIgnore, boolean startChildZygote, String instructionSet, |
Sudheer Shanka | e51005d | 2019-02-24 10:24:09 -0800 | [diff] [blame] | 260 | String appDataDir, String packageName, String[] packagesForUID, |
Sudheer Shanka | 03fd40b | 2019-02-06 12:39:14 -0800 | [diff] [blame] | 261 | String sandboxId); |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 262 | |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 263 | /** |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 264 | * Specialize an unspecialized app process. The current VM must have been started |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 265 | * with the -Xzygote flag. |
| 266 | * |
| 267 | * @param uid The UNIX uid that the new process should setuid() to before spawning any threads |
| 268 | * @param gid The UNIX gid that the new process should setgid() to before spawning any threads |
| 269 | * @param gids null-ok; A list of UNIX gids that the new process should |
| 270 | * setgroups() to before spawning any threads |
| 271 | * @param runtimeFlags Bit flags that enable ART features |
| 272 | * @param rlimits null-ok An array of rlimit tuples, with the second |
| 273 | * dimension having a length of 3 and representing |
| 274 | * (resource, rlim_cur, rlim_max). These are set via the posix |
| 275 | * setrlimit(2) call. |
| 276 | * @param seInfo null-ok A string specifying SELinux information for |
| 277 | * the new process. |
| 278 | * @param niceName null-ok A string specifying the process name. |
| 279 | * @param startChildZygote If true, the new child process will itself be a |
| 280 | * new zygote process. |
| 281 | * @param instructionSet null-ok The instruction set to use. |
| 282 | * @param appDataDir null-ok The data directory of the app. |
| 283 | */ |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 284 | public static void specializeAppProcess(int uid, int gid, int[] gids, int runtimeFlags, |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 285 | int[][] rlimits, int mountExternal, String seInfo, String niceName, |
Chris Wailes | ba4c2eb | 2019-01-11 17:13:00 -0800 | [diff] [blame] | 286 | boolean startChildZygote, String instructionSet, String appDataDir, String packageName, |
Sudheer Shanka | e51005d | 2019-02-24 10:24:09 -0800 | [diff] [blame] | 287 | String[] packagesForUID, String sandboxId) { |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 288 | nativeSpecializeAppProcess(uid, gid, gids, runtimeFlags, rlimits, mountExternal, seInfo, |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 289 | niceName, startChildZygote, instructionSet, appDataDir, |
Sudheer Shanka | e51005d | 2019-02-24 10:24:09 -0800 | [diff] [blame] | 290 | packageName, packagesForUID, sandboxId); |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 291 | |
| 292 | // Enable tracing as soon as possible for the child process. |
| 293 | Trace.setTracingEnabled(true, runtimeFlags); |
| 294 | |
| 295 | // Note that this event ends at the end of handleChildProc. |
| 296 | Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "PostFork"); |
| 297 | |
| 298 | /* |
| 299 | * This is called here (instead of after the fork but before the specialize) to maintain |
| 300 | * consistancy with the code paths for forkAndSpecialize. |
| 301 | * |
| 302 | * TODO (chriswailes): Look into moving this to immediately after the fork. |
| 303 | */ |
Neil Fuller | 555d8b7 | 2019-01-28 18:29:20 +0000 | [diff] [blame] | 304 | ZygoteHooks.postForkCommon(); |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 305 | } |
| 306 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 307 | private static native void nativeSpecializeAppProcess(int uid, int gid, int[] gids, |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 308 | int runtimeFlags, int[][] rlimits, int mountExternal, String seInfo, String niceName, |
| 309 | boolean startChildZygote, String instructionSet, String appDataDir, String packageName, |
Sudheer Shanka | e51005d | 2019-02-24 10:24:09 -0800 | [diff] [blame] | 310 | String[] packagesForUID, String sandboxId); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 311 | |
| 312 | /** |
Christopher Ferris | 2980de4 | 2017-06-20 16:13:40 -0700 | [diff] [blame] | 313 | * Called to do any initialization before starting an application. |
| 314 | */ |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 315 | static native void nativePreApplicationInit(); |
Christopher Ferris | 2980de4 | 2017-06-20 16:13:40 -0700 | [diff] [blame] | 316 | |
| 317 | /** |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 318 | * Special method to start the system server process. In addition to the |
| 319 | * common actions performed in forkAndSpecialize, the pid of the child |
| 320 | * process is recorded such that the death of the child process will cause |
| 321 | * zygote to exit. |
| 322 | * |
| 323 | * @param uid the UNIX uid that the new process should setuid() to after |
| 324 | * fork()ing and and before spawning any threads. |
| 325 | * @param gid the UNIX gid that the new process should setgid() to after |
| 326 | * fork()ing and and before spawning any threads. |
| 327 | * @param gids null-ok; a list of UNIX gids that the new process should |
| 328 | * setgroups() to after fork and before spawning any threads. |
Nicolas Geoffray | 81edac4 | 2017-09-07 14:13:29 +0100 | [diff] [blame] | 329 | * @param runtimeFlags bit flags that enable ART features. |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 330 | * @param rlimits null-ok an array of rlimit tuples, with the second |
| 331 | * dimension having a length of 3 and representing |
| 332 | * (resource, rlim_cur, rlim_max). These are set via the posix |
| 333 | * setrlimit(2) call. |
| 334 | * @param permittedCapabilities argument for setcap() |
| 335 | * @param effectiveCapabilities argument for setcap() |
| 336 | * |
| 337 | * @return 0 if this is the child, pid of the child |
| 338 | * if this is the parent, or -1 on error. |
| 339 | */ |
Nicolas Geoffray | 81edac4 | 2017-09-07 14:13:29 +0100 | [diff] [blame] | 340 | public static int forkSystemServer(int uid, int gid, int[] gids, int runtimeFlags, |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 341 | int[][] rlimits, long permittedCapabilities, long effectiveCapabilities) { |
Neil Fuller | 555d8b7 | 2019-01-28 18:29:20 +0000 | [diff] [blame] | 342 | ZygoteHooks.preFork(); |
Hiroshi Yamauchi | 1e3db87 | 2017-03-02 13:39:07 -0800 | [diff] [blame] | 343 | // Resets nice priority for zygote process. |
| 344 | resetNicePriority(); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 345 | int pid = nativeForkSystemServer( |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 346 | uid, gid, gids, runtimeFlags, rlimits, |
| 347 | permittedCapabilities, effectiveCapabilities); |
Narayan Kamath | fbb32f6 | 2015-06-12 15:34:35 +0100 | [diff] [blame] | 348 | // Enable tracing as soon as we enter the system_server. |
| 349 | if (pid == 0) { |
Andreas Gampe | 8f4eab2 | 2017-09-13 18:16:13 -0700 | [diff] [blame] | 350 | Trace.setTracingEnabled(true, runtimeFlags); |
Narayan Kamath | fbb32f6 | 2015-06-12 15:34:35 +0100 | [diff] [blame] | 351 | } |
Neil Fuller | 555d8b7 | 2019-01-28 18:29:20 +0000 | [diff] [blame] | 352 | ZygoteHooks.postForkCommon(); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 353 | return pid; |
| 354 | } |
| 355 | |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 356 | private static native int nativeForkSystemServer(int uid, int gid, int[] gids, int runtimeFlags, |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 357 | int[][] rlimits, long permittedCapabilities, long effectiveCapabilities); |
| 358 | |
doheon1.lee | 885b742 | 2016-01-20 13:07:27 +0900 | [diff] [blame] | 359 | /** |
Robert Sesek | 54e387d | 2016-12-02 17:27:50 -0500 | [diff] [blame] | 360 | * Lets children of the zygote inherit open file descriptors to this path. |
| 361 | */ |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 362 | protected static native void nativeAllowFileAcrossFork(String path); |
Robert Sesek | 54e387d | 2016-12-02 17:27:50 -0500 | [diff] [blame] | 363 | |
| 364 | /** |
Martijn Coenen | 4b98816 | 2019-01-28 13:58:45 +0100 | [diff] [blame] | 365 | * Lets children of the zygote inherit open file descriptors that belong to the |
| 366 | * ApplicationInfo that is passed in. |
| 367 | * |
| 368 | * @param appInfo ApplicationInfo of the application |
| 369 | */ |
| 370 | protected static void allowAppFilesAcrossFork(ApplicationInfo appInfo) { |
Torne (Richard Coles) | 1aaae10 | 2019-03-01 15:02:29 -0500 | [diff] [blame] | 371 | for (String path : appInfo.getAllApkPaths()) { |
| 372 | Zygote.nativeAllowFileAcrossFork(path); |
Martijn Coenen | 4b98816 | 2019-01-28 13:58:45 +0100 | [diff] [blame] | 373 | } |
| 374 | } |
| 375 | |
| 376 | /** |
Martijn Coenen | 86f08a5 | 2019-01-03 16:23:01 +0100 | [diff] [blame] | 377 | * Installs a seccomp filter that limits setresuid()/setresgid() to the passed-in range |
| 378 | * @param uidGidMin The smallest allowed uid/gid |
| 379 | * @param uidGidMax The largest allowed uid/gid |
| 380 | */ |
| 381 | native protected static void nativeInstallSeccompUidGidFilter(int uidGidMin, int uidGidMax); |
| 382 | |
| 383 | /** |
Chris Wailes | 6d482d54 | 2019-04-03 13:00:52 -0700 | [diff] [blame] | 384 | * Initialize the native state of the Zygote. This inclues |
| 385 | * - Fetching socket FDs from the environment |
| 386 | * - Initializing security properties |
| 387 | * - Unmounting storage as appropriate |
| 388 | * - Loading necessary performance profile information |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 389 | * |
| 390 | * @param isPrimary True if this is the zygote process, false if it is zygote_secondary |
| 391 | */ |
Chris Wailes | 6d482d54 | 2019-04-03 13:00:52 -0700 | [diff] [blame] | 392 | static void initNativeState(boolean isPrimary) { |
| 393 | nativeInitNativeState(isPrimary); |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 394 | } |
| 395 | |
Chris Wailes | 6d482d54 | 2019-04-03 13:00:52 -0700 | [diff] [blame] | 396 | protected static native void nativeInitNativeState(boolean isPrimary); |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 397 | |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 398 | /** |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 399 | * Returns the raw string value of a system property. |
| 400 | * |
| 401 | * Note that Device Config is not available without an application so SystemProperties is used |
| 402 | * instead. |
| 403 | * |
| 404 | * TODO (chriswailes): Cache the system property location in native code and then write a JNI |
| 405 | * function to fetch it. |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 406 | */ |
Chris Wailes | fe3a348 | 2019-03-13 14:38:04 -0700 | [diff] [blame] | 407 | public static String getConfigurationProperty(String propertyName, String defaultValue) { |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 408 | return SystemProperties.get( |
| 409 | String.join(".", |
| 410 | "persist.device_config", |
Matt Pape | e9b680a | 2019-03-05 15:53:13 -0800 | [diff] [blame] | 411 | DeviceConfig.NAMESPACE_RUNTIME_NATIVE, |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 412 | propertyName), |
| 413 | defaultValue); |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 414 | } |
| 415 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 416 | protected static void emptyUsapPool() { |
| 417 | nativeEmptyUsapPool(); |
Chris Wailes | ae93714 | 2019-01-24 12:57:33 -0800 | [diff] [blame] | 418 | } |
| 419 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 420 | private static native void nativeEmptyUsapPool(); |
Chris Wailes | ae93714 | 2019-01-24 12:57:33 -0800 | [diff] [blame] | 421 | |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 422 | /** |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 423 | * Returns the value of a system property converted to a boolean using specific logic. |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 424 | * |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 425 | * Note that Device Config is not available without an application so SystemProperties is used |
| 426 | * instead. |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 427 | * |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 428 | * @see SystemProperties.getBoolean |
| 429 | * |
| 430 | * TODO (chriswailes): Cache the system property location in native code and then write a JNI |
| 431 | * function to fetch it. |
Chris Wailes | fe3a348 | 2019-03-13 14:38:04 -0700 | [diff] [blame] | 432 | * TODO (chriswailes): Move into ZygoteConfig.java once the necessary CL lands (go/ag/6580627) |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 433 | */ |
Chris Wailes | fe3a348 | 2019-03-13 14:38:04 -0700 | [diff] [blame] | 434 | public static boolean getConfigurationPropertyBoolean( |
| 435 | String propertyName, Boolean defaultValue) { |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 436 | return SystemProperties.getBoolean( |
| 437 | String.join(".", |
| 438 | "persist.device_config", |
Matt Pape | e9b680a | 2019-03-05 15:53:13 -0800 | [diff] [blame] | 439 | DeviceConfig.NAMESPACE_RUNTIME_NATIVE, |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 440 | propertyName), |
| 441 | defaultValue); |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | /** |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 445 | * @return Number of unspecialized app processes currently in the pool |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 446 | */ |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 447 | static int getUsapPoolCount() { |
| 448 | return nativeGetUsapPoolCount(); |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 449 | } |
| 450 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 451 | private static native int nativeGetUsapPoolCount(); |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 452 | |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 453 | /** |
| 454 | * @return The event FD used for communication between the signal handler and the ZygoteServer |
| 455 | * poll loop |
| 456 | */ |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 457 | static FileDescriptor getUsapPoolEventFD() { |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 458 | FileDescriptor fd = new FileDescriptor(); |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 459 | fd.setInt$(nativeGetUsapPoolEventFD()); |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 460 | |
| 461 | return fd; |
| 462 | } |
| 463 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 464 | private static native int nativeGetUsapPoolEventFD(); |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 465 | |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 466 | /** |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 467 | * Fork a new unspecialized app process from the zygote |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 468 | * |
| 469 | * @param sessionSocketRawFDs Anonymous session sockets that are currently open |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 470 | * @return In the Zygote process this function will always return null; in unspecialized app |
| 471 | * processes this function will return a Runnable object representing the new |
| 472 | * application that is passed up from usapMain. |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 473 | */ |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 474 | static Runnable forkUsap(LocalServerSocket usapPoolSocket, |
| 475 | int[] sessionSocketRawFDs) { |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 476 | FileDescriptor[] pipeFDs = null; |
| 477 | |
| 478 | try { |
| 479 | pipeFDs = Os.pipe2(O_CLOEXEC); |
| 480 | } catch (ErrnoException errnoEx) { |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 481 | throw new IllegalStateException("Unable to create USAP pipe.", errnoEx); |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | int pid = |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 485 | nativeForkUsap(pipeFDs[0].getInt$(), pipeFDs[1].getInt$(), sessionSocketRawFDs); |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 486 | |
| 487 | if (pid == 0) { |
| 488 | IoUtils.closeQuietly(pipeFDs[0]); |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 489 | return usapMain(usapPoolSocket, pipeFDs[1]); |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 490 | } else { |
| 491 | // The read-end of the pipe will be closed by the native code. |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 492 | // See removeUsapTableEntry(); |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 493 | IoUtils.closeQuietly(pipeFDs[1]); |
| 494 | return null; |
| 495 | } |
| 496 | } |
| 497 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 498 | private static native int nativeForkUsap(int readPipeFD, |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 499 | int writePipeFD, |
| 500 | int[] sessionSocketRawFDs); |
| 501 | |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 502 | /** |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 503 | * This function is used by unspecialized app processes to wait for specialization requests from |
| 504 | * the system server. |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 505 | * |
| 506 | * @param writePipe The write end of the reporting pipe used to communicate with the poll loop |
| 507 | * of the ZygoteServer. |
| 508 | * @return A runnable oject representing the new application. |
| 509 | */ |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 510 | private static Runnable usapMain(LocalServerSocket usapPoolSocket, |
| 511 | FileDescriptor writePipe) { |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 512 | final int pid = Process.myPid(); |
Chris Wailes | 0753040 | 2019-03-25 18:01:30 -0700 | [diff] [blame] | 513 | Process.setArgV0(Process.is64Bit() ? "usap64" : "usap32"); |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 514 | |
| 515 | LocalSocket sessionSocket = null; |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 516 | DataOutputStream usapOutputStream = null; |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 517 | Credentials peerCredentials = null; |
Chris Wailes | ae93714 | 2019-01-24 12:57:33 -0800 | [diff] [blame] | 518 | ZygoteArguments args = null; |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 519 | |
| 520 | while (true) { |
| 521 | try { |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 522 | sessionSocket = usapPoolSocket.accept(); |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 523 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 524 | BufferedReader usapReader = |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 525 | new BufferedReader(new InputStreamReader(sessionSocket.getInputStream())); |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 526 | usapOutputStream = |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 527 | new DataOutputStream(sessionSocket.getOutputStream()); |
| 528 | |
| 529 | peerCredentials = sessionSocket.getPeerCredentials(); |
| 530 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 531 | String[] argStrings = readArgumentList(usapReader); |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 532 | |
| 533 | if (argStrings != null) { |
Chris Wailes | ae93714 | 2019-01-24 12:57:33 -0800 | [diff] [blame] | 534 | args = new ZygoteArguments(argStrings); |
| 535 | |
| 536 | // TODO (chriswailes): Should this only be run for debug builds? |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 537 | validateUsapCommand(args); |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 538 | break; |
| 539 | } else { |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 540 | Log.e("USAP", "Truncated command received."); |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 541 | IoUtils.closeQuietly(sessionSocket); |
| 542 | } |
Chris Wailes | ae93714 | 2019-01-24 12:57:33 -0800 | [diff] [blame] | 543 | } catch (Exception ex) { |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 544 | Log.e("USAP", ex.getMessage()); |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 545 | IoUtils.closeQuietly(sessionSocket); |
| 546 | } |
| 547 | } |
| 548 | |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 549 | applyUidSecurityPolicy(args, peerCredentials); |
| 550 | applyDebuggerSystemProperty(args); |
| 551 | |
| 552 | int[][] rlimits = null; |
| 553 | |
| 554 | if (args.mRLimits != null) { |
| 555 | rlimits = args.mRLimits.toArray(INT_ARRAY_2D); |
| 556 | } |
| 557 | |
| 558 | // This must happen before the SELinux policy for this process is |
| 559 | // changed when specializing. |
| 560 | try { |
| 561 | // Used by ZygoteProcess.zygoteSendArgsAndGetResult to fill in a |
| 562 | // Process.ProcessStartResult object. |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 563 | usapOutputStream.writeInt(pid); |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 564 | } catch (IOException ioEx) { |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 565 | Log.e("USAP", "Failed to write response to session socket: " + ioEx.getMessage()); |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 566 | System.exit(-1); |
| 567 | } finally { |
| 568 | IoUtils.closeQuietly(sessionSocket); |
Chris Wailes | b63a00a | 2019-04-10 15:55:29 -0700 | [diff] [blame^] | 569 | |
| 570 | try { |
| 571 | // This socket is closed using Os.close due to an issue with the implementation of |
| 572 | // LocalSocketImp.close. Because the raw FD is created by init and then loaded from |
| 573 | // an environment variable (as opposed to being created by the LocalSocketImpl |
| 574 | // itself) the current implementation will not actually close the underlying FD. |
| 575 | // |
| 576 | // See b/130309968 for discussion of this issue. |
| 577 | Os.close(usapPoolSocket.getFileDescriptor()); |
| 578 | } catch (ErrnoException ex) { |
| 579 | Log.e("USAP", "Failed to close USAP pool socket: " + ex.getMessage()); |
| 580 | } |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | try { |
| 584 | ByteArrayOutputStream buffer = |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 585 | new ByteArrayOutputStream(Zygote.USAP_MANAGEMENT_MESSAGE_BYTES); |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 586 | DataOutputStream outputStream = new DataOutputStream(buffer); |
| 587 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 588 | // This is written as a long so that the USAP reporting pipe and USAP pool event FD |
| 589 | // handlers in ZygoteServer.runSelectLoop can be unified. These two cases should both |
| 590 | // send/receive 8 bytes. |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 591 | outputStream.writeLong(pid); |
| 592 | outputStream.flush(); |
| 593 | |
| 594 | Os.write(writePipe, buffer.toByteArray(), 0, buffer.size()); |
| 595 | } catch (Exception ex) { |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 596 | Log.e("USAP", |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 597 | String.format("Failed to write PID (%d) to pipe (%d): %s", |
| 598 | pid, writePipe.getInt$(), ex.getMessage())); |
| 599 | System.exit(-1); |
| 600 | } finally { |
| 601 | IoUtils.closeQuietly(writePipe); |
| 602 | } |
| 603 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 604 | specializeAppProcess(args.mUid, args.mGid, args.mGids, |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 605 | args.mRuntimeFlags, rlimits, args.mMountExternal, |
| 606 | args.mSeInfo, args.mNiceName, args.mStartChildZygote, |
| 607 | args.mInstructionSet, args.mAppDataDir, args.mPackageName, |
Sudheer Shanka | e51005d | 2019-02-24 10:24:09 -0800 | [diff] [blame] | 608 | args.mPackagesForUid, args.mSandboxId); |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 609 | |
Jeff Vander Stoep | 739c0b5 | 2019-03-25 20:27:52 -0700 | [diff] [blame] | 610 | disableExecuteOnly(args.mTargetSdkVersion); |
| 611 | |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 612 | if (args.mNiceName != null) { |
| 613 | Process.setArgV0(args.mNiceName); |
| 614 | } |
| 615 | |
| 616 | // End of the postFork event. |
| 617 | Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER); |
| 618 | |
| 619 | return ZygoteInit.zygoteInit(args.mTargetSdkVersion, |
| 620 | args.mRemainingArgs, |
| 621 | null /* classLoader */); |
| 622 | } |
| 623 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 624 | private static final String USAP_ERROR_PREFIX = "Invalid command to USAP: "; |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 625 | |
| 626 | /** |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 627 | * Checks a set of zygote arguments to see if they can be handled by a USAP. Throws an |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 628 | * exception if an invalid arugment is encountered. |
| 629 | * @param args The arguments to test |
| 630 | */ |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 631 | private static void validateUsapCommand(ZygoteArguments args) { |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 632 | if (args.mAbiListQuery) { |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 633 | throw new IllegalArgumentException(USAP_ERROR_PREFIX + "--query-abi-list"); |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 634 | } else if (args.mPidQuery) { |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 635 | throw new IllegalArgumentException(USAP_ERROR_PREFIX + "--get-pid"); |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 636 | } else if (args.mPreloadDefault) { |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 637 | throw new IllegalArgumentException(USAP_ERROR_PREFIX + "--preload-default"); |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 638 | } else if (args.mPreloadPackage != null) { |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 639 | throw new IllegalArgumentException(USAP_ERROR_PREFIX + "--preload-package"); |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 640 | } else if (args.mPreloadApp != null) { |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 641 | throw new IllegalArgumentException(USAP_ERROR_PREFIX + "--preload-app"); |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 642 | } else if (args.mStartChildZygote) { |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 643 | throw new IllegalArgumentException(USAP_ERROR_PREFIX + "--start-child-zygote"); |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 644 | } else if (args.mApiBlacklistExemptions != null) { |
| 645 | throw new IllegalArgumentException( |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 646 | USAP_ERROR_PREFIX + "--set-api-blacklist-exemptions"); |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 647 | } else if (args.mHiddenApiAccessLogSampleRate != -1) { |
| 648 | throw new IllegalArgumentException( |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 649 | USAP_ERROR_PREFIX + "--hidden-api-log-sampling-rate="); |
Andrei Onea | e8e150d | 2019-02-18 18:27:11 +0000 | [diff] [blame] | 650 | } else if (args.mHiddenApiAccessStatslogSampleRate != -1) { |
| 651 | throw new IllegalArgumentException( |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 652 | USAP_ERROR_PREFIX + "--hidden-api-statslog-sampling-rate="); |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 653 | } else if (args.mInvokeWith != null) { |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 654 | throw new IllegalArgumentException(USAP_ERROR_PREFIX + "--invoke-with"); |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 655 | } else if (args.mPermittedCapabilities != 0 || args.mEffectiveCapabilities != 0) { |
| 656 | throw new ZygoteSecurityException("Client may not specify capabilities: " |
| 657 | + "permitted=0x" + Long.toHexString(args.mPermittedCapabilities) |
| 658 | + ", effective=0x" + Long.toHexString(args.mEffectiveCapabilities)); |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | /** |
Jeff Vander Stoep | 739c0b5 | 2019-03-25 20:27:52 -0700 | [diff] [blame] | 663 | * Mark execute-only segments of libraries read+execute for apps with targetSdkVersion<Q. |
| 664 | */ |
| 665 | protected static void disableExecuteOnly(int targetSdkVersion) { |
| 666 | if ((targetSdkVersion < Build.VERSION_CODES.Q) && !nativeDisableExecuteOnly()) { |
| 667 | Log.e("Zygote", "Failed to set libraries to read+execute."); |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | private static native boolean nativeDisableExecuteOnly(); |
| 672 | |
| 673 | /** |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 674 | * @return Raw file descriptors for the read-end of USAP reporting pipes. |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 675 | */ |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 676 | protected static int[] getUsapPipeFDs() { |
| 677 | return nativeGetUsapPipeFDs(); |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 678 | } |
| 679 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 680 | private static native int[] nativeGetUsapPipeFDs(); |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 681 | |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 682 | /** |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 683 | * Remove the USAP table entry for the provided process ID. |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 684 | * |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 685 | * @param usapPID Process ID of the entry to remove |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 686 | * @return True if the entry was removed; false if it doesn't exist |
| 687 | */ |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 688 | protected static boolean removeUsapTableEntry(int usapPID) { |
| 689 | return nativeRemoveUsapTableEntry(usapPID); |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 690 | } |
| 691 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 692 | private static native boolean nativeRemoveUsapTableEntry(int usapPID); |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 693 | |
Chris Wailes | 2be2626 | 2019-01-11 16:14:43 -0800 | [diff] [blame] | 694 | /** |
| 695 | * uid 1000 (Process.SYSTEM_UID) may specify any uid > 1000 in normal |
| 696 | * operation. It may also specify any gid and setgroups() list it chooses. |
| 697 | * In factory test mode, it may specify any UID. |
| 698 | * |
| 699 | * @param args non-null; zygote spawner arguments |
| 700 | * @param peer non-null; peer credentials |
| 701 | * @throws ZygoteSecurityException |
| 702 | */ |
| 703 | protected static void applyUidSecurityPolicy(ZygoteArguments args, Credentials peer) |
| 704 | throws ZygoteSecurityException { |
| 705 | |
| 706 | if (peer.getUid() == Process.SYSTEM_UID) { |
| 707 | /* In normal operation, SYSTEM_UID can only specify a restricted |
| 708 | * set of UIDs. In factory test mode, SYSTEM_UID may specify any uid. |
| 709 | */ |
| 710 | boolean uidRestricted = FactoryTest.getMode() == FactoryTest.FACTORY_TEST_OFF; |
| 711 | |
| 712 | if (uidRestricted && args.mUidSpecified && (args.mUid < Process.SYSTEM_UID)) { |
| 713 | throw new ZygoteSecurityException( |
| 714 | "System UID may not launch process with UID < " |
| 715 | + Process.SYSTEM_UID); |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | // If not otherwise specified, uid and gid are inherited from peer |
| 720 | if (!args.mUidSpecified) { |
| 721 | args.mUid = peer.getUid(); |
| 722 | args.mUidSpecified = true; |
| 723 | } |
| 724 | if (!args.mGidSpecified) { |
| 725 | args.mGid = peer.getGid(); |
| 726 | args.mGidSpecified = true; |
| 727 | } |
| 728 | } |
| 729 | |
| 730 | /** |
| 731 | * Applies debugger system properties to the zygote arguments. |
| 732 | * |
| 733 | * If "ro.debuggable" is "1", all apps are debuggable. Otherwise, |
| 734 | * the debugger state is specified via the "--enable-jdwp" flag |
| 735 | * in the spawn request. |
| 736 | * |
| 737 | * @param args non-null; zygote spawner args |
| 738 | */ |
| 739 | protected static void applyDebuggerSystemProperty(ZygoteArguments args) { |
| 740 | if (RoSystemProperties.DEBUGGABLE) { |
| 741 | args.mRuntimeFlags |= Zygote.DEBUG_ENABLE_JDWP; |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | /** |
| 746 | * Applies zygote security policy. |
| 747 | * Based on the credentials of the process issuing a zygote command: |
| 748 | * <ol> |
| 749 | * <li> uid 0 (root) may specify --invoke-with to launch Zygote with a |
| 750 | * wrapper command. |
| 751 | * <li> Any other uid may not specify any invoke-with argument. |
| 752 | * </ul> |
| 753 | * |
| 754 | * @param args non-null; zygote spawner arguments |
| 755 | * @param peer non-null; peer credentials |
| 756 | * @throws ZygoteSecurityException |
| 757 | */ |
| 758 | protected static void applyInvokeWithSecurityPolicy(ZygoteArguments args, Credentials peer) |
| 759 | throws ZygoteSecurityException { |
| 760 | int peerUid = peer.getUid(); |
| 761 | |
| 762 | if (args.mInvokeWith != null && peerUid != 0 |
| 763 | && (args.mRuntimeFlags & Zygote.DEBUG_ENABLE_JDWP) == 0) { |
Chris Wailes | ae93714 | 2019-01-24 12:57:33 -0800 | [diff] [blame] | 764 | throw new ZygoteSecurityException("Peer is permitted to specify an " |
| 765 | + "explicit invoke-with wrapper command only for debuggable " |
Chris Wailes | 2be2626 | 2019-01-11 16:14:43 -0800 | [diff] [blame] | 766 | + "applications."); |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | /** |
| 771 | * Applies invoke-with system properties to the zygote arguments. |
| 772 | * |
| 773 | * @param args non-null; zygote args |
| 774 | */ |
| 775 | protected static void applyInvokeWithSystemProperty(ZygoteArguments args) { |
| 776 | if (args.mInvokeWith == null && args.mNiceName != null) { |
| 777 | String property = "wrap." + args.mNiceName; |
| 778 | args.mInvokeWith = SystemProperties.get(property); |
| 779 | if (args.mInvokeWith != null && args.mInvokeWith.length() == 0) { |
| 780 | args.mInvokeWith = null; |
| 781 | } |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | /** |
| 786 | * Reads an argument list from the provided socket |
| 787 | * @return Argument list or null if EOF is reached |
| 788 | * @throws IOException passed straight through |
| 789 | */ |
| 790 | static String[] readArgumentList(BufferedReader socketReader) throws IOException { |
Chris Wailes | 2be2626 | 2019-01-11 16:14:43 -0800 | [diff] [blame] | 791 | int argc; |
| 792 | |
| 793 | try { |
| 794 | String argc_string = socketReader.readLine(); |
| 795 | |
| 796 | if (argc_string == null) { |
| 797 | // EOF reached. |
| 798 | return null; |
| 799 | } |
| 800 | argc = Integer.parseInt(argc_string); |
| 801 | |
| 802 | } catch (NumberFormatException ex) { |
| 803 | Log.e("Zygote", "Invalid Zygote wire format: non-int at argc"); |
| 804 | throw new IOException("Invalid wire format"); |
| 805 | } |
| 806 | |
| 807 | // See bug 1092107: large argc can be used for a DOS attack |
| 808 | if (argc > MAX_ZYGOTE_ARGC) { |
| 809 | throw new IOException("Max arg count exceeded"); |
| 810 | } |
| 811 | |
| 812 | String[] args = new String[argc]; |
| 813 | for (int arg_index = 0; arg_index < argc; arg_index++) { |
| 814 | args[arg_index] = socketReader.readLine(); |
| 815 | if (args[arg_index] == null) { |
| 816 | // We got an unexpected EOF. |
| 817 | throw new IOException("Truncated request"); |
| 818 | } |
| 819 | } |
| 820 | |
| 821 | return args; |
| 822 | } |
| 823 | |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 824 | /** |
Chris Wailes | cb0b37f | 2019-01-11 17:04:41 -0800 | [diff] [blame] | 825 | * Creates a managed LocalServerSocket object using a file descriptor |
| 826 | * created by an init.rc script. The init scripts that specify the |
| 827 | * sockets name can be found in system/core/rootdir. The socket is bound |
| 828 | * to the file system in the /dev/sockets/ directory, and the file |
| 829 | * descriptor is shared via the ANDROID_SOCKET_<socketName> environment |
| 830 | * variable. |
| 831 | */ |
| 832 | static LocalServerSocket createManagedSocketFromInitSocket(String socketName) { |
| 833 | int fileDesc; |
| 834 | final String fullSocketName = ANDROID_SOCKET_PREFIX + socketName; |
| 835 | |
| 836 | try { |
| 837 | String env = System.getenv(fullSocketName); |
| 838 | fileDesc = Integer.parseInt(env); |
| 839 | } catch (RuntimeException ex) { |
| 840 | throw new RuntimeException("Socket unset or invalid: " + fullSocketName, ex); |
| 841 | } |
| 842 | |
| 843 | try { |
| 844 | FileDescriptor fd = new FileDescriptor(); |
| 845 | fd.setInt$(fileDesc); |
| 846 | return new LocalServerSocket(fd); |
| 847 | } catch (IOException ex) { |
| 848 | throw new RuntimeException( |
| 849 | "Error building socket from file descriptor: " + fileDesc, ex); |
| 850 | } |
| 851 | } |
doheon1.lee | 885b742 | 2016-01-20 13:07:27 +0900 | [diff] [blame] | 852 | |
Orion Hodson | 46724e7 | 2018-10-19 13:05:33 +0100 | [diff] [blame] | 853 | private static void callPostForkSystemServerHooks() { |
| 854 | // SystemServer specific post fork hooks run before child post fork hooks. |
Neil Fuller | 555d8b7 | 2019-01-28 18:29:20 +0000 | [diff] [blame] | 855 | ZygoteHooks.postForkSystemServer(); |
Orion Hodson | 46724e7 | 2018-10-19 13:05:33 +0100 | [diff] [blame] | 856 | } |
| 857 | |
Nicolas Geoffray | 81edac4 | 2017-09-07 14:13:29 +0100 | [diff] [blame] | 858 | private static void callPostForkChildHooks(int runtimeFlags, boolean isSystemServer, |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 859 | boolean isZygote, String instructionSet) { |
Neil Fuller | 555d8b7 | 2019-01-28 18:29:20 +0000 | [diff] [blame] | 860 | ZygoteHooks.postForkChild(runtimeFlags, isSystemServer, isZygote, instructionSet); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 861 | } |
| 862 | |
Narayan Kamath | b49996d | 2017-02-06 20:24:08 +0000 | [diff] [blame] | 863 | /** |
Hiroshi Yamauchi | 1e3db87 | 2017-03-02 13:39:07 -0800 | [diff] [blame] | 864 | * Resets the calling thread priority to the default value (Thread.NORM_PRIORITY |
| 865 | * or nice value 0). This updates both the priority value in java.lang.Thread and |
| 866 | * the nice value (setpriority). |
Narayan Kamath | b49996d | 2017-02-06 20:24:08 +0000 | [diff] [blame] | 867 | */ |
Hiroshi Yamauchi | 1e3db87 | 2017-03-02 13:39:07 -0800 | [diff] [blame] | 868 | static void resetNicePriority() { |
| 869 | Thread.currentThread().setPriority(Thread.NORM_PRIORITY); |
| 870 | } |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 871 | |
| 872 | /** |
| 873 | * Executes "/system/bin/sh -c <command>" using the exec() system call. |
| 874 | * This method throws a runtime exception if exec() failed, otherwise, this |
| 875 | * method never returns. |
| 876 | * |
| 877 | * @param command The shell command to execute. |
| 878 | */ |
| 879 | public static void execShell(String command) { |
| 880 | String[] args = { "/system/bin/sh", "-c", command }; |
| 881 | try { |
Elliott Hughes | 860c591 | 2014-04-28 19:19:13 -0700 | [diff] [blame] | 882 | Os.execv(args[0], args); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 883 | } catch (ErrnoException e) { |
| 884 | throw new RuntimeException(e); |
| 885 | } |
| 886 | } |
| 887 | |
| 888 | /** |
| 889 | * Appends quotes shell arguments to the specified string builder. |
| 890 | * The arguments are quoted using single-quotes, escaped if necessary, |
| 891 | * prefixed with a space, and appended to the command. |
| 892 | * |
| 893 | * @param command A string builder for the shell command being constructed. |
| 894 | * @param args An array of argument strings to be quoted and appended to the command. |
| 895 | * @see #execShell(String) |
| 896 | */ |
| 897 | public static void appendQuotedShellArgs(StringBuilder command, String[] args) { |
| 898 | for (String arg : args) { |
| 899 | command.append(" '").append(arg.replace("'", "'\\''")).append("'"); |
| 900 | } |
| 901 | } |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 902 | } |