blob: e1878623711d35c28e628e8c0e2ff5d2d7f5d841 [file] [log] [blame]
Narayan Kamath973b4662014-03-31 13:41:26 +01001/*
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
17package com.android.internal.os;
18
19
Narayan Kamathfbb32f62015-06-12 15:34:35 +010020import android.os.Trace;
Narayan Kamath973b4662014-03-31 13:41:26 +010021import dalvik.system.ZygoteHooks;
Elliott Hughes860c5912014-04-28 19:19:13 -070022import android.system.ErrnoException;
23import android.system.Os;
Narayan Kamath973b4662014-03-31 13:41:26 +010024
Tobias Sargeantb9679dc2016-01-19 16:34:54 +000025import java.lang.reflect.InvocationTargetException;
26import java.lang.reflect.Method;
27
Narayan Kamath973b4662014-03-31 13:41:26 +010028/** @hide */
29public final class Zygote {
30 /*
Nicolas Geoffray81edac42017-09-07 14:13:29 +010031 * Bit values for "runtimeFlags" argument. The definitions are duplicated
Narayan Kamath973b4662014-03-31 13:41:26 +010032 * in the native code.
33 */
34
35 /** enable debugging over JDWP */
Nicolas Geoffray347b1df2016-12-20 14:05:05 +000036 public static final int DEBUG_ENABLE_JDWP = 1;
Narayan Kamath973b4662014-03-31 13:41:26 +010037 /** enable JNI checks */
38 public static final int DEBUG_ENABLE_CHECKJNI = 1 << 1;
39 /** enable Java programming language "assert" statements */
40 public static final int DEBUG_ENABLE_ASSERT = 1 << 2;
Mathieu Chartier7a490282015-03-17 09:51:36 -070041 /** disable the AOT compiler and JIT */
Narayan Kamath973b4662014-03-31 13:41:26 +010042 public static final int DEBUG_ENABLE_SAFEMODE = 1 << 3;
43 /** Enable logging of third-party JNI activity. */
44 public static final int DEBUG_ENABLE_JNI_LOGGING = 1 << 4;
David Srbecky065075e2015-05-28 17:16:09 +010045 /** Force generation of native debugging information. */
Nicolas Geoffray9abbf452015-11-05 11:29:42 +000046 public static final int DEBUG_GENERATE_DEBUG_INFO = 1 << 5;
Tamas Berghammerdf6cb282016-01-29 12:07:00 +000047 /** Always use JIT-ed code. */
48 public static final int DEBUG_ALWAYS_JIT = 1 << 6;
Nicolas Geoffray347b1df2016-12-20 14:05:05 +000049 /** Make the code native debuggable by turning off some optimizations. */
Tamas Berghammerdf6cb282016-01-29 12:07:00 +000050 public static final int DEBUG_NATIVE_DEBUGGABLE = 1 << 7;
Nicolas Geoffray347b1df2016-12-20 14:05:05 +000051 /** Make the code Java debuggable by turning off some optimizations. */
52 public static final int DEBUG_JAVA_DEBUGGABLE = 1 << 8;
Mathieu Chartier7a490282015-03-17 09:51:36 -070053
Nicolas Geoffray1f88ad62017-09-13 14:21:00 +010054 /** Turn off the verifier. */
55 public static final int DISABLE_VERIFIER = 1 << 9;
56 /** Only use oat files located in /system. Otherwise use dex/jar/apk . */
57 public static final int ONLY_USE_SYSTEM_OAT_FILES = 1 << 10;
David Srbecky156ed922018-01-30 14:37:37 +000058 /** Force generation of native debugging information for backtraces. */
Mathew Inwood16073b82018-03-23 10:05:01 +000059 public static final int DEBUG_GENERATE_MINI_DEBUG_INFO = 1 << 11;
60 /**
61 * Hidden API access restrictions. This is a mask for bits representing the API enforcement
62 * policy, defined by {@code @ApplicationInfo.HiddenApiEnforcementPolicy}.
63 */
64 public static final int API_ENFORCEMENT_POLICY_MASK = (1 << 12) | (1 << 13);
65 /**
66 * Bit shift for use with {@link #API_ENFORCEMENT_POLICY_MASK}.
67 *
68 * (flags & API_ENFORCEMENT_POLICY_MASK) >> API_ENFORCEMENT_POLICY_SHIFT gives
69 * @ApplicationInfo.ApiEnforcementPolicy values.
70 */
71 public static final int API_ENFORCEMENT_POLICY_SHIFT =
72 Integer.numberOfTrailingZeros(API_ENFORCEMENT_POLICY_MASK);
Calin Juravle8eb891b2018-05-03 19:51:18 -070073 /**
74 * Enable system server ART profiling.
75 */
76 public static final int PROFILE_SYSTEM_SERVER = 1 << 14;
Nicolas Geoffray1f88ad62017-09-13 14:21:00 +010077
Narayan Kamath973b4662014-03-31 13:41:26 +010078 /** No external storage should be mounted. */
79 public static final int MOUNT_EXTERNAL_NONE = 0;
Jeff Sharkey9527b222015-06-24 15:24:48 -070080 /** Default external storage should be mounted. */
Jeff Sharkey48877892015-03-18 11:27:19 -070081 public static final int MOUNT_EXTERNAL_DEFAULT = 1;
Jeff Sharkey9527b222015-06-24 15:24:48 -070082 /** Read-only external storage should be mounted. */
83 public static final int MOUNT_EXTERNAL_READ = 2;
84 /** Read-write external storage should be mounted. */
85 public static final int MOUNT_EXTERNAL_WRITE = 3;
Narayan Kamath973b4662014-03-31 13:41:26 +010086
87 private static final ZygoteHooks VM_HOOKS = new ZygoteHooks();
88
Robert Sesekd0a190df2018-02-12 18:46:01 -050089 /**
90 * An extraArg passed when a zygote process is forking a child-zygote, specifying a name
91 * in the abstract socket namespace. This socket name is what the new child zygote
92 * should listen for connections on.
93 */
94 public static final String CHILD_ZYGOTE_SOCKET_NAME_ARG = "--zygote-socket=";
95
Narayan Kamath973b4662014-03-31 13:41:26 +010096 private Zygote() {}
97
Victor Hsiehc8176ef2018-01-08 12:43:00 -080098 /** Called for some security initialization before any fork. */
99 native static void nativeSecurityInit();
100
Narayan Kamath973b4662014-03-31 13:41:26 +0100101 /**
102 * Forks a new VM instance. The current VM must have been started
103 * with the -Xzygote flag. <b>NOTE: new instance keeps all
104 * root capabilities. The new process is expected to call capset()</b>.
105 *
106 * @param uid the UNIX uid that the new process should setuid() to after
107 * fork()ing and and before spawning any threads.
108 * @param gid the UNIX gid that the new process should setgid() to after
109 * fork()ing and and before spawning any threads.
110 * @param gids null-ok; a list of UNIX gids that the new process should
111 * setgroups() to after fork and before spawning any threads.
Nicolas Geoffray81edac42017-09-07 14:13:29 +0100112 * @param runtimeFlags bit flags that enable ART features.
Narayan Kamath973b4662014-03-31 13:41:26 +0100113 * @param rlimits null-ok an array of rlimit tuples, with the second
114 * dimension having a length of 3 and representing
115 * (resource, rlim_cur, rlim_max). These are set via the posix
116 * setrlimit(2) call.
117 * @param seInfo null-ok a string specifying SELinux information for
118 * the new process.
119 * @param niceName null-ok a string specifying the process name.
120 * @param fdsToClose an array of ints, holding one or more POSIX
121 * file descriptor numbers that are to be closed by the child
122 * (and replaced by /dev/null) after forking. An integer value
123 * of -1 in any entry in the array means "ignore this one".
Andreas Gampe8dfa1782017-01-05 12:45:58 -0800124 * @param fdsToIgnore null-ok an array of ints, either null or holding
125 * one or more POSIX file descriptor numbers that are to be ignored
126 * in the file descriptor table check.
Robert Sesekd0a190df2018-02-12 18:46:01 -0500127 * @param startChildZygote if true, the new child process will itself be a
128 * new zygote process.
Andreas Gampeaec67dc2014-09-02 21:23:06 -0700129 * @param instructionSet null-ok the instruction set to use.
jgu212eacd062014-09-10 06:55:07 -0400130 * @param appDataDir null-ok the data directory of the app.
Narayan Kamath973b4662014-03-31 13:41:26 +0100131 *
132 * @return 0 if this is the child, pid of the child
133 * if this is the parent, or -1 on error.
134 */
Nicolas Geoffray81edac42017-09-07 14:13:29 +0100135 public static int forkAndSpecialize(int uid, int gid, int[] gids, int runtimeFlags,
Andreas Gampeaec67dc2014-09-02 21:23:06 -0700136 int[][] rlimits, int mountExternal, String seInfo, String niceName, int[] fdsToClose,
Robert Sesekd0a190df2018-02-12 18:46:01 -0500137 int[] fdsToIgnore, boolean startChildZygote, String instructionSet, String appDataDir) {
Narayan Kamath973b4662014-03-31 13:41:26 +0100138 VM_HOOKS.preFork();
Hiroshi Yamauchi1e3db872017-03-02 13:39:07 -0800139 // Resets nice priority for zygote process.
140 resetNicePriority();
Narayan Kamath973b4662014-03-31 13:41:26 +0100141 int pid = nativeForkAndSpecialize(
Nicolas Geoffray81edac42017-09-07 14:13:29 +0100142 uid, gid, gids, runtimeFlags, rlimits, mountExternal, seInfo, niceName, fdsToClose,
Robert Sesekd0a190df2018-02-12 18:46:01 -0500143 fdsToIgnore, startChildZygote, instructionSet, appDataDir);
Narayan Kamathfbb32f62015-06-12 15:34:35 +0100144 // Enable tracing as soon as possible for the child process.
145 if (pid == 0) {
Nicolas Geoffray1bf40f62017-09-13 12:59:21 +0100146 Trace.setTracingEnabled(true, runtimeFlags);
Narayan Kamathfbb32f62015-06-12 15:34:35 +0100147
148 // Note that this event ends at the end of handleChildProc,
149 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "PostFork");
150 }
Narayan Kamath973b4662014-03-31 13:41:26 +0100151 VM_HOOKS.postForkCommon();
152 return pid;
153 }
154
Nicolas Geoffray81edac42017-09-07 14:13:29 +0100155 native private static int nativeForkAndSpecialize(int uid, int gid, int[] gids,int runtimeFlags,
Andreas Gampeaec67dc2014-09-02 21:23:06 -0700156 int[][] rlimits, int mountExternal, String seInfo, String niceName, int[] fdsToClose,
Robert Sesekd0a190df2018-02-12 18:46:01 -0500157 int[] fdsToIgnore, boolean startChildZygote, String instructionSet, String appDataDir);
Narayan Kamath973b4662014-03-31 13:41:26 +0100158
159 /**
Christopher Ferris76de39e2017-06-20 16:13:40 -0700160 * Called to do any initialization before starting an application.
161 */
162 native static void nativePreApplicationInit();
163
164 /**
Narayan Kamath973b4662014-03-31 13:41:26 +0100165 * Special method to start the system server process. In addition to the
166 * common actions performed in forkAndSpecialize, the pid of the child
167 * process is recorded such that the death of the child process will cause
168 * zygote to exit.
169 *
170 * @param uid the UNIX uid that the new process should setuid() to after
171 * fork()ing and and before spawning any threads.
172 * @param gid the UNIX gid that the new process should setgid() to after
173 * fork()ing and and before spawning any threads.
174 * @param gids null-ok; a list of UNIX gids that the new process should
175 * setgroups() to after fork and before spawning any threads.
Nicolas Geoffray81edac42017-09-07 14:13:29 +0100176 * @param runtimeFlags bit flags that enable ART features.
Narayan Kamath973b4662014-03-31 13:41:26 +0100177 * @param rlimits null-ok an array of rlimit tuples, with the second
178 * dimension having a length of 3 and representing
179 * (resource, rlim_cur, rlim_max). These are set via the posix
180 * setrlimit(2) call.
181 * @param permittedCapabilities argument for setcap()
182 * @param effectiveCapabilities argument for setcap()
183 *
184 * @return 0 if this is the child, pid of the child
185 * if this is the parent, or -1 on error.
186 */
Nicolas Geoffray81edac42017-09-07 14:13:29 +0100187 public static int forkSystemServer(int uid, int gid, int[] gids, int runtimeFlags,
Narayan Kamath973b4662014-03-31 13:41:26 +0100188 int[][] rlimits, long permittedCapabilities, long effectiveCapabilities) {
189 VM_HOOKS.preFork();
Hiroshi Yamauchi1e3db872017-03-02 13:39:07 -0800190 // Resets nice priority for zygote process.
191 resetNicePriority();
Narayan Kamath973b4662014-03-31 13:41:26 +0100192 int pid = nativeForkSystemServer(
Nicolas Geoffray81edac42017-09-07 14:13:29 +0100193 uid, gid, gids, runtimeFlags, rlimits, permittedCapabilities, effectiveCapabilities);
Narayan Kamathfbb32f62015-06-12 15:34:35 +0100194 // Enable tracing as soon as we enter the system_server.
195 if (pid == 0) {
Nicolas Geoffray1bf40f62017-09-13 12:59:21 +0100196 Trace.setTracingEnabled(true, runtimeFlags);
Narayan Kamathfbb32f62015-06-12 15:34:35 +0100197 }
Narayan Kamath973b4662014-03-31 13:41:26 +0100198 VM_HOOKS.postForkCommon();
199 return pid;
200 }
201
Nicolas Geoffray81edac42017-09-07 14:13:29 +0100202 native private static int nativeForkSystemServer(int uid, int gid, int[] gids, int runtimeFlags,
Narayan Kamath973b4662014-03-31 13:41:26 +0100203 int[][] rlimits, long permittedCapabilities, long effectiveCapabilities);
204
doheon1.lee885b7422016-01-20 13:07:27 +0900205 /**
Robert Sesek54e387d2016-12-02 17:27:50 -0500206 * Lets children of the zygote inherit open file descriptors to this path.
207 */
208 native protected static void nativeAllowFileAcrossFork(String path);
209
210 /**
doheon1.lee885b7422016-01-20 13:07:27 +0900211 * Zygote unmount storage space on initializing.
212 * This method is called once.
213 */
214 native protected static void nativeUnmountStorageOnInit();
215
Nicolas Geoffray81edac42017-09-07 14:13:29 +0100216 private static void callPostForkChildHooks(int runtimeFlags, boolean isSystemServer,
Robert Sesekd0a190df2018-02-12 18:46:01 -0500217 boolean isZygote, String instructionSet) {
218 VM_HOOKS.postForkChild(runtimeFlags, isSystemServer, isZygote, instructionSet);
Narayan Kamath973b4662014-03-31 13:41:26 +0100219 }
220
Narayan Kamathb49996d2017-02-06 20:24:08 +0000221 /**
Hiroshi Yamauchi1e3db872017-03-02 13:39:07 -0800222 * Resets the calling thread priority to the default value (Thread.NORM_PRIORITY
223 * or nice value 0). This updates both the priority value in java.lang.Thread and
224 * the nice value (setpriority).
Narayan Kamathb49996d2017-02-06 20:24:08 +0000225 */
Hiroshi Yamauchi1e3db872017-03-02 13:39:07 -0800226 static void resetNicePriority() {
227 Thread.currentThread().setPriority(Thread.NORM_PRIORITY);
228 }
Narayan Kamath973b4662014-03-31 13:41:26 +0100229
230 /**
231 * Executes "/system/bin/sh -c &lt;command&gt;" using the exec() system call.
232 * This method throws a runtime exception if exec() failed, otherwise, this
233 * method never returns.
234 *
235 * @param command The shell command to execute.
236 */
237 public static void execShell(String command) {
238 String[] args = { "/system/bin/sh", "-c", command };
239 try {
Elliott Hughes860c5912014-04-28 19:19:13 -0700240 Os.execv(args[0], args);
Narayan Kamath973b4662014-03-31 13:41:26 +0100241 } catch (ErrnoException e) {
242 throw new RuntimeException(e);
243 }
244 }
245
246 /**
247 * Appends quotes shell arguments to the specified string builder.
248 * The arguments are quoted using single-quotes, escaped if necessary,
249 * prefixed with a space, and appended to the command.
250 *
251 * @param command A string builder for the shell command being constructed.
252 * @param args An array of argument strings to be quoted and appended to the command.
253 * @see #execShell(String)
254 */
255 public static void appendQuotedShellArgs(StringBuilder command, String[] args) {
256 for (String arg : args) {
257 command.append(" '").append(arg.replace("'", "'\\''")).append("'");
258 }
259 }
Narayan Kamath973b4662014-03-31 13:41:26 +0100260}