blob: 293de3d713324ca008e3b0ced4beddfc3ac40c70 [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 /*
31 * Bit values for "debugFlags" argument. The definitions are duplicated
32 * in the native code.
33 */
34
35 /** enable debugging over JDWP */
36 public static final int DEBUG_ENABLE_DEBUGGER = 1;
37 /** 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;
49 /** Make the code debuggable with turning off some optimizations. */
50 public static final int DEBUG_NATIVE_DEBUGGABLE = 1 << 7;
Mathieu Chartier7a490282015-03-17 09:51:36 -070051
Narayan Kamath973b4662014-03-31 13:41:26 +010052 /** No external storage should be mounted. */
53 public static final int MOUNT_EXTERNAL_NONE = 0;
Jeff Sharkey9527b222015-06-24 15:24:48 -070054 /** Default external storage should be mounted. */
Jeff Sharkey48877892015-03-18 11:27:19 -070055 public static final int MOUNT_EXTERNAL_DEFAULT = 1;
Jeff Sharkey9527b222015-06-24 15:24:48 -070056 /** Read-only external storage should be mounted. */
57 public static final int MOUNT_EXTERNAL_READ = 2;
58 /** Read-write external storage should be mounted. */
59 public static final int MOUNT_EXTERNAL_WRITE = 3;
Narayan Kamath973b4662014-03-31 13:41:26 +010060
61 private static final ZygoteHooks VM_HOOKS = new ZygoteHooks();
62
63 private Zygote() {}
64
65 /**
66 * Forks a new VM instance. The current VM must have been started
67 * with the -Xzygote flag. <b>NOTE: new instance keeps all
68 * root capabilities. The new process is expected to call capset()</b>.
69 *
70 * @param uid the UNIX uid that the new process should setuid() to after
71 * fork()ing and and before spawning any threads.
72 * @param gid the UNIX gid that the new process should setgid() to after
73 * fork()ing and and before spawning any threads.
74 * @param gids null-ok; a list of UNIX gids that the new process should
75 * setgroups() to after fork and before spawning any threads.
76 * @param debugFlags bit flags that enable debugging features.
77 * @param rlimits null-ok an array of rlimit tuples, with the second
78 * dimension having a length of 3 and representing
79 * (resource, rlim_cur, rlim_max). These are set via the posix
80 * setrlimit(2) call.
81 * @param seInfo null-ok a string specifying SELinux information for
82 * the new process.
83 * @param niceName null-ok a string specifying the process name.
84 * @param fdsToClose an array of ints, holding one or more POSIX
85 * file descriptor numbers that are to be closed by the child
86 * (and replaced by /dev/null) after forking. An integer value
87 * of -1 in any entry in the array means "ignore this one".
Andreas Gampeaec67dc2014-09-02 21:23:06 -070088 * @param instructionSet null-ok the instruction set to use.
jgu212eacd062014-09-10 06:55:07 -040089 * @param appDataDir null-ok the data directory of the app.
Narayan Kamath973b4662014-03-31 13:41:26 +010090 *
91 * @return 0 if this is the child, pid of the child
92 * if this is the parent, or -1 on error.
93 */
94 public static int forkAndSpecialize(int uid, int gid, int[] gids, int debugFlags,
Andreas Gampeaec67dc2014-09-02 21:23:06 -070095 int[][] rlimits, int mountExternal, String seInfo, String niceName, int[] fdsToClose,
jgu212eacd062014-09-10 06:55:07 -040096 String instructionSet, String appDataDir) {
Narayan Kamath973b4662014-03-31 13:41:26 +010097 VM_HOOKS.preFork();
98 int pid = nativeForkAndSpecialize(
Andreas Gampeaec67dc2014-09-02 21:23:06 -070099 uid, gid, gids, debugFlags, rlimits, mountExternal, seInfo, niceName, fdsToClose,
jgu212eacd062014-09-10 06:55:07 -0400100 instructionSet, appDataDir);
Narayan Kamathfbb32f62015-06-12 15:34:35 +0100101 // Enable tracing as soon as possible for the child process.
102 if (pid == 0) {
103 Trace.setTracingEnabled(true);
104
105 // Note that this event ends at the end of handleChildProc,
106 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "PostFork");
107 }
Narayan Kamath973b4662014-03-31 13:41:26 +0100108 VM_HOOKS.postForkCommon();
109 return pid;
110 }
111
112 native private static int nativeForkAndSpecialize(int uid, int gid, int[] gids,int debugFlags,
Andreas Gampeaec67dc2014-09-02 21:23:06 -0700113 int[][] rlimits, int mountExternal, String seInfo, String niceName, int[] fdsToClose,
jgu212eacd062014-09-10 06:55:07 -0400114 String instructionSet, String appDataDir);
Narayan Kamath973b4662014-03-31 13:41:26 +0100115
116 /**
117 * Special method to start the system server process. In addition to the
118 * common actions performed in forkAndSpecialize, the pid of the child
119 * process is recorded such that the death of the child process will cause
120 * zygote to exit.
121 *
122 * @param uid the UNIX uid that the new process should setuid() to after
123 * fork()ing and and before spawning any threads.
124 * @param gid the UNIX gid that the new process should setgid() to after
125 * fork()ing and and before spawning any threads.
126 * @param gids null-ok; a list of UNIX gids that the new process should
127 * setgroups() to after fork and before spawning any threads.
128 * @param debugFlags bit flags that enable debugging features.
129 * @param rlimits null-ok an array of rlimit tuples, with the second
130 * dimension having a length of 3 and representing
131 * (resource, rlim_cur, rlim_max). These are set via the posix
132 * setrlimit(2) call.
133 * @param permittedCapabilities argument for setcap()
134 * @param effectiveCapabilities argument for setcap()
135 *
136 * @return 0 if this is the child, pid of the child
137 * if this is the parent, or -1 on error.
138 */
139 public static int forkSystemServer(int uid, int gid, int[] gids, int debugFlags,
140 int[][] rlimits, long permittedCapabilities, long effectiveCapabilities) {
141 VM_HOOKS.preFork();
142 int pid = nativeForkSystemServer(
143 uid, gid, gids, debugFlags, rlimits, permittedCapabilities, effectiveCapabilities);
Narayan Kamathfbb32f62015-06-12 15:34:35 +0100144 // Enable tracing as soon as we enter the system_server.
145 if (pid == 0) {
146 Trace.setTracingEnabled(true);
147 }
Narayan Kamath973b4662014-03-31 13:41:26 +0100148 VM_HOOKS.postForkCommon();
149 return pid;
150 }
151
152 native private static int nativeForkSystemServer(int uid, int gid, int[] gids, int debugFlags,
153 int[][] rlimits, long permittedCapabilities, long effectiveCapabilities);
154
doheon1.lee885b7422016-01-20 13:07:27 +0900155 /**
Robert Sesek54e387d2016-12-02 17:27:50 -0500156 * Lets children of the zygote inherit open file descriptors to this path.
157 */
158 native protected static void nativeAllowFileAcrossFork(String path);
159
160 /**
doheon1.lee885b7422016-01-20 13:07:27 +0900161 * Zygote unmount storage space on initializing.
162 * This method is called once.
163 */
164 native protected static void nativeUnmountStorageOnInit();
165
Nicolas Geoffray3c43b382015-12-11 15:01:04 +0000166 private static void callPostForkChildHooks(int debugFlags, boolean isSystemServer,
167 String instructionSet) {
168 VM_HOOKS.postForkChild(debugFlags, isSystemServer, instructionSet);
Narayan Kamath973b4662014-03-31 13:41:26 +0100169 }
170
171
172 /**
173 * Executes "/system/bin/sh -c &lt;command&gt;" using the exec() system call.
174 * This method throws a runtime exception if exec() failed, otherwise, this
175 * method never returns.
176 *
177 * @param command The shell command to execute.
178 */
179 public static void execShell(String command) {
180 String[] args = { "/system/bin/sh", "-c", command };
181 try {
Elliott Hughes860c5912014-04-28 19:19:13 -0700182 Os.execv(args[0], args);
Narayan Kamath973b4662014-03-31 13:41:26 +0100183 } catch (ErrnoException e) {
184 throw new RuntimeException(e);
185 }
186 }
187
188 /**
189 * Appends quotes shell arguments to the specified string builder.
190 * The arguments are quoted using single-quotes, escaped if necessary,
191 * prefixed with a space, and appended to the command.
192 *
193 * @param command A string builder for the shell command being constructed.
194 * @param args An array of argument strings to be quoted and appended to the command.
195 * @see #execShell(String)
196 */
197 public static void appendQuotedShellArgs(StringBuilder command, String[] args) {
198 for (String arg : args) {
199 command.append(" '").append(arg.replace("'", "'\\''")).append("'");
200 }
201 }
Tobias Sargeantb9679dc2016-01-19 16:34:54 +0000202
203 /**
204 * Helper exception class which holds a method and arguments and
205 * can call them. This is used as part of a trampoline to get rid of
206 * the initial process setup stack frames.
207 */
208 public static class MethodAndArgsCaller extends Exception
209 implements Runnable {
210 /** method to call */
211 private final Method mMethod;
212
213 /** argument array */
214 private final String[] mArgs;
215
216 public MethodAndArgsCaller(Method method, String[] args) {
217 mMethod = method;
218 mArgs = args;
219 }
220
221 public void run() {
222 try {
223 mMethod.invoke(null, new Object[] { mArgs });
224 } catch (IllegalAccessException ex) {
225 throw new RuntimeException(ex);
226 } catch (InvocationTargetException ex) {
227 Throwable cause = ex.getCause();
228 if (cause instanceof RuntimeException) {
229 throw (RuntimeException) cause;
230 } else if (cause instanceof Error) {
231 throw (Error) cause;
232 }
233 throw new RuntimeException(ex);
234 }
235 }
236 }
Narayan Kamath973b4662014-03-31 13:41:26 +0100237}