blob: 0d1088896f407ea6b060a7e18119767abfdbe989 [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);
Nicolas Geoffray1f88ad62017-09-13 14:21:00 +010073
Narayan Kamath973b4662014-03-31 13:41:26 +010074 /** No external storage should be mounted. */
75 public static final int MOUNT_EXTERNAL_NONE = 0;
Jeff Sharkey9527b222015-06-24 15:24:48 -070076 /** Default external storage should be mounted. */
Jeff Sharkey48877892015-03-18 11:27:19 -070077 public static final int MOUNT_EXTERNAL_DEFAULT = 1;
Jeff Sharkey9527b222015-06-24 15:24:48 -070078 /** Read-only external storage should be mounted. */
79 public static final int MOUNT_EXTERNAL_READ = 2;
80 /** Read-write external storage should be mounted. */
81 public static final int MOUNT_EXTERNAL_WRITE = 3;
Narayan Kamath973b4662014-03-31 13:41:26 +010082
83 private static final ZygoteHooks VM_HOOKS = new ZygoteHooks();
84
Robert Sesekd0a190df2018-02-12 18:46:01 -050085 /**
86 * An extraArg passed when a zygote process is forking a child-zygote, specifying a name
87 * in the abstract socket namespace. This socket name is what the new child zygote
88 * should listen for connections on.
89 */
90 public static final String CHILD_ZYGOTE_SOCKET_NAME_ARG = "--zygote-socket=";
91
Narayan Kamath973b4662014-03-31 13:41:26 +010092 private Zygote() {}
93
Victor Hsiehc8176ef2018-01-08 12:43:00 -080094 /** Called for some security initialization before any fork. */
95 native static void nativeSecurityInit();
96
Narayan Kamath973b4662014-03-31 13:41:26 +010097 /**
98 * Forks a new VM instance. The current VM must have been started
99 * with the -Xzygote flag. <b>NOTE: new instance keeps all
100 * root capabilities. The new process is expected to call capset()</b>.
101 *
102 * @param uid the UNIX uid that the new process should setuid() to after
103 * fork()ing and and before spawning any threads.
104 * @param gid the UNIX gid that the new process should setgid() to after
105 * fork()ing and and before spawning any threads.
106 * @param gids null-ok; a list of UNIX gids that the new process should
107 * setgroups() to after fork and before spawning any threads.
Nicolas Geoffray81edac42017-09-07 14:13:29 +0100108 * @param runtimeFlags bit flags that enable ART features.
Narayan Kamath973b4662014-03-31 13:41:26 +0100109 * @param rlimits null-ok an array of rlimit tuples, with the second
110 * dimension having a length of 3 and representing
111 * (resource, rlim_cur, rlim_max). These are set via the posix
112 * setrlimit(2) call.
113 * @param seInfo null-ok a string specifying SELinux information for
114 * the new process.
115 * @param niceName null-ok a string specifying the process name.
116 * @param fdsToClose an array of ints, holding one or more POSIX
117 * file descriptor numbers that are to be closed by the child
118 * (and replaced by /dev/null) after forking. An integer value
119 * of -1 in any entry in the array means "ignore this one".
Andreas Gampe8dfa1782017-01-05 12:45:58 -0800120 * @param fdsToIgnore null-ok an array of ints, either null or holding
121 * one or more POSIX file descriptor numbers that are to be ignored
122 * in the file descriptor table check.
Robert Sesekd0a190df2018-02-12 18:46:01 -0500123 * @param startChildZygote if true, the new child process will itself be a
124 * new zygote process.
Andreas Gampeaec67dc2014-09-02 21:23:06 -0700125 * @param instructionSet null-ok the instruction set to use.
jgu212eacd062014-09-10 06:55:07 -0400126 * @param appDataDir null-ok the data directory of the app.
Narayan Kamath973b4662014-03-31 13:41:26 +0100127 *
128 * @return 0 if this is the child, pid of the child
129 * if this is the parent, or -1 on error.
130 */
Nicolas Geoffray81edac42017-09-07 14:13:29 +0100131 public static int forkAndSpecialize(int uid, int gid, int[] gids, int runtimeFlags,
Andreas Gampeaec67dc2014-09-02 21:23:06 -0700132 int[][] rlimits, int mountExternal, String seInfo, String niceName, int[] fdsToClose,
Robert Sesekd0a190df2018-02-12 18:46:01 -0500133 int[] fdsToIgnore, boolean startChildZygote, String instructionSet, String appDataDir) {
Narayan Kamath973b4662014-03-31 13:41:26 +0100134 VM_HOOKS.preFork();
Hiroshi Yamauchi1e3db872017-03-02 13:39:07 -0800135 // Resets nice priority for zygote process.
136 resetNicePriority();
Narayan Kamath973b4662014-03-31 13:41:26 +0100137 int pid = nativeForkAndSpecialize(
Nicolas Geoffray81edac42017-09-07 14:13:29 +0100138 uid, gid, gids, runtimeFlags, rlimits, mountExternal, seInfo, niceName, fdsToClose,
Robert Sesekd0a190df2018-02-12 18:46:01 -0500139 fdsToIgnore, startChildZygote, instructionSet, appDataDir);
Narayan Kamathfbb32f62015-06-12 15:34:35 +0100140 // Enable tracing as soon as possible for the child process.
141 if (pid == 0) {
Nicolas Geoffray1bf40f62017-09-13 12:59:21 +0100142 Trace.setTracingEnabled(true, runtimeFlags);
Narayan Kamathfbb32f62015-06-12 15:34:35 +0100143
144 // Note that this event ends at the end of handleChildProc,
145 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "PostFork");
146 }
Narayan Kamath973b4662014-03-31 13:41:26 +0100147 VM_HOOKS.postForkCommon();
148 return pid;
149 }
150
Nicolas Geoffray81edac42017-09-07 14:13:29 +0100151 native private static int nativeForkAndSpecialize(int uid, int gid, int[] gids,int runtimeFlags,
Andreas Gampeaec67dc2014-09-02 21:23:06 -0700152 int[][] rlimits, int mountExternal, String seInfo, String niceName, int[] fdsToClose,
Robert Sesekd0a190df2018-02-12 18:46:01 -0500153 int[] fdsToIgnore, boolean startChildZygote, String instructionSet, String appDataDir);
Narayan Kamath973b4662014-03-31 13:41:26 +0100154
155 /**
Christopher Ferris76de39e2017-06-20 16:13:40 -0700156 * Called to do any initialization before starting an application.
157 */
158 native static void nativePreApplicationInit();
159
160 /**
Narayan Kamath973b4662014-03-31 13:41:26 +0100161 * Special method to start the system server process. In addition to the
162 * common actions performed in forkAndSpecialize, the pid of the child
163 * process is recorded such that the death of the child process will cause
164 * zygote to exit.
165 *
166 * @param uid the UNIX uid that the new process should setuid() to after
167 * fork()ing and and before spawning any threads.
168 * @param gid the UNIX gid that the new process should setgid() to after
169 * fork()ing and and before spawning any threads.
170 * @param gids null-ok; a list of UNIX gids that the new process should
171 * setgroups() to after fork and before spawning any threads.
Nicolas Geoffray81edac42017-09-07 14:13:29 +0100172 * @param runtimeFlags bit flags that enable ART features.
Narayan Kamath973b4662014-03-31 13:41:26 +0100173 * @param rlimits null-ok an array of rlimit tuples, with the second
174 * dimension having a length of 3 and representing
175 * (resource, rlim_cur, rlim_max). These are set via the posix
176 * setrlimit(2) call.
177 * @param permittedCapabilities argument for setcap()
178 * @param effectiveCapabilities argument for setcap()
179 *
180 * @return 0 if this is the child, pid of the child
181 * if this is the parent, or -1 on error.
182 */
Nicolas Geoffray81edac42017-09-07 14:13:29 +0100183 public static int forkSystemServer(int uid, int gid, int[] gids, int runtimeFlags,
Narayan Kamath973b4662014-03-31 13:41:26 +0100184 int[][] rlimits, long permittedCapabilities, long effectiveCapabilities) {
185 VM_HOOKS.preFork();
Hiroshi Yamauchi1e3db872017-03-02 13:39:07 -0800186 // Resets nice priority for zygote process.
187 resetNicePriority();
Narayan Kamath973b4662014-03-31 13:41:26 +0100188 int pid = nativeForkSystemServer(
Nicolas Geoffray81edac42017-09-07 14:13:29 +0100189 uid, gid, gids, runtimeFlags, rlimits, permittedCapabilities, effectiveCapabilities);
Narayan Kamathfbb32f62015-06-12 15:34:35 +0100190 // Enable tracing as soon as we enter the system_server.
191 if (pid == 0) {
Nicolas Geoffray1bf40f62017-09-13 12:59:21 +0100192 Trace.setTracingEnabled(true, runtimeFlags);
Narayan Kamathfbb32f62015-06-12 15:34:35 +0100193 }
Narayan Kamath973b4662014-03-31 13:41:26 +0100194 VM_HOOKS.postForkCommon();
195 return pid;
196 }
197
Nicolas Geoffray81edac42017-09-07 14:13:29 +0100198 native private static int nativeForkSystemServer(int uid, int gid, int[] gids, int runtimeFlags,
Narayan Kamath973b4662014-03-31 13:41:26 +0100199 int[][] rlimits, long permittedCapabilities, long effectiveCapabilities);
200
doheon1.lee885b7422016-01-20 13:07:27 +0900201 /**
Robert Sesek54e387d2016-12-02 17:27:50 -0500202 * Lets children of the zygote inherit open file descriptors to this path.
203 */
204 native protected static void nativeAllowFileAcrossFork(String path);
205
206 /**
doheon1.lee885b7422016-01-20 13:07:27 +0900207 * Zygote unmount storage space on initializing.
208 * This method is called once.
209 */
210 native protected static void nativeUnmountStorageOnInit();
211
Nicolas Geoffray81edac42017-09-07 14:13:29 +0100212 private static void callPostForkChildHooks(int runtimeFlags, boolean isSystemServer,
Robert Sesekd0a190df2018-02-12 18:46:01 -0500213 boolean isZygote, String instructionSet) {
214 VM_HOOKS.postForkChild(runtimeFlags, isSystemServer, isZygote, instructionSet);
Narayan Kamath973b4662014-03-31 13:41:26 +0100215 }
216
Narayan Kamathb49996d2017-02-06 20:24:08 +0000217 /**
Hiroshi Yamauchi1e3db872017-03-02 13:39:07 -0800218 * Resets the calling thread priority to the default value (Thread.NORM_PRIORITY
219 * or nice value 0). This updates both the priority value in java.lang.Thread and
220 * the nice value (setpriority).
Narayan Kamathb49996d2017-02-06 20:24:08 +0000221 */
Hiroshi Yamauchi1e3db872017-03-02 13:39:07 -0800222 static void resetNicePriority() {
223 Thread.currentThread().setPriority(Thread.NORM_PRIORITY);
224 }
Narayan Kamath973b4662014-03-31 13:41:26 +0100225
226 /**
227 * Executes "/system/bin/sh -c &lt;command&gt;" using the exec() system call.
228 * This method throws a runtime exception if exec() failed, otherwise, this
229 * method never returns.
230 *
231 * @param command The shell command to execute.
232 */
233 public static void execShell(String command) {
234 String[] args = { "/system/bin/sh", "-c", command };
235 try {
Elliott Hughes860c5912014-04-28 19:19:13 -0700236 Os.execv(args[0], args);
Narayan Kamath973b4662014-03-31 13:41:26 +0100237 } catch (ErrnoException e) {
238 throw new RuntimeException(e);
239 }
240 }
241
242 /**
243 * Appends quotes shell arguments to the specified string builder.
244 * The arguments are quoted using single-quotes, escaped if necessary,
245 * prefixed with a space, and appended to the command.
246 *
247 * @param command A string builder for the shell command being constructed.
248 * @param args An array of argument strings to be quoted and appended to the command.
249 * @see #execShell(String)
250 */
251 public static void appendQuotedShellArgs(StringBuilder command, String[] args) {
252 for (String arg : args) {
253 command.append(" '").append(arg.replace("'", "'\\''")).append("'");
254 }
255 }
Narayan Kamath973b4662014-03-31 13:41:26 +0100256}