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