blob: 4e4b5b87e0ad7b43bc818eb09bc7089b3c125c0d [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
Jeff Sharkeyace874b2017-09-07 15:27:33 -060019import android.os.IVold;
Narayan Kamathfbb32f62015-06-12 15:34:35 +010020import android.os.Trace;
Elliott Hughes860c5912014-04-28 19:19:13 -070021import android.system.ErrnoException;
22import android.system.Os;
Narayan Kamath973b4662014-03-31 13:41:26 +010023
Jeff Sharkeyace874b2017-09-07 15:27:33 -060024import dalvik.system.ZygoteHooks;
Tobias Sargeantb9679dc2016-01-19 16:34:54 +000025
Narayan Kamath973b4662014-03-31 13:41:26 +010026/** @hide */
27public final class Zygote {
28 /*
29 * Bit values for "debugFlags" argument. The definitions are duplicated
30 * in the native code.
31 */
32
33 /** enable debugging over JDWP */
Nicolas Geoffray347b1df2016-12-20 14:05:05 +000034 public static final int DEBUG_ENABLE_JDWP = 1;
Narayan Kamath973b4662014-03-31 13:41:26 +010035 /** enable JNI checks */
36 public static final int DEBUG_ENABLE_CHECKJNI = 1 << 1;
37 /** enable Java programming language "assert" statements */
38 public static final int DEBUG_ENABLE_ASSERT = 1 << 2;
Mathieu Chartier7a490282015-03-17 09:51:36 -070039 /** disable the AOT compiler and JIT */
Narayan Kamath973b4662014-03-31 13:41:26 +010040 public static final int DEBUG_ENABLE_SAFEMODE = 1 << 3;
41 /** Enable logging of third-party JNI activity. */
42 public static final int DEBUG_ENABLE_JNI_LOGGING = 1 << 4;
David Srbecky065075e2015-05-28 17:16:09 +010043 /** Force generation of native debugging information. */
Nicolas Geoffray9abbf452015-11-05 11:29:42 +000044 public static final int DEBUG_GENERATE_DEBUG_INFO = 1 << 5;
Tamas Berghammerdf6cb282016-01-29 12:07:00 +000045 /** Always use JIT-ed code. */
46 public static final int DEBUG_ALWAYS_JIT = 1 << 6;
Nicolas Geoffray347b1df2016-12-20 14:05:05 +000047 /** Make the code native debuggable by turning off some optimizations. */
Tamas Berghammerdf6cb282016-01-29 12:07:00 +000048 public static final int DEBUG_NATIVE_DEBUGGABLE = 1 << 7;
Nicolas Geoffray347b1df2016-12-20 14:05:05 +000049 /** Make the code Java debuggable by turning off some optimizations. */
50 public static final int DEBUG_JAVA_DEBUGGABLE = 1 << 8;
Mathieu Chartier7a490282015-03-17 09:51:36 -070051
Narayan Kamath973b4662014-03-31 13:41:26 +010052 /** No external storage should be mounted. */
Jeff Sharkeyace874b2017-09-07 15:27:33 -060053 public static final int MOUNT_EXTERNAL_NONE = IVold.REMOUNT_MODE_NONE;
Jeff Sharkey9527b222015-06-24 15:24:48 -070054 /** Default external storage should be mounted. */
Jeff Sharkeyace874b2017-09-07 15:27:33 -060055 public static final int MOUNT_EXTERNAL_DEFAULT = IVold.REMOUNT_MODE_DEFAULT;
Jeff Sharkey9527b222015-06-24 15:24:48 -070056 /** Read-only external storage should be mounted. */
Jeff Sharkeyace874b2017-09-07 15:27:33 -060057 public static final int MOUNT_EXTERNAL_READ = IVold.REMOUNT_MODE_READ;
Jeff Sharkey9527b222015-06-24 15:24:48 -070058 /** Read-write external storage should be mounted. */
Jeff Sharkeyace874b2017-09-07 15:27:33 -060059 public static final int MOUNT_EXTERNAL_WRITE = IVold.REMOUNT_MODE_WRITE;
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 Gampe8dfa1782017-01-05 12:45:58 -080088 * @param fdsToIgnore null-ok an array of ints, either null or holding
89 * one or more POSIX file descriptor numbers that are to be ignored
90 * in the file descriptor table check.
Andreas Gampeaec67dc2014-09-02 21:23:06 -070091 * @param instructionSet null-ok the instruction set to use.
jgu212eacd062014-09-10 06:55:07 -040092 * @param appDataDir null-ok the data directory of the app.
Narayan Kamath973b4662014-03-31 13:41:26 +010093 *
94 * @return 0 if this is the child, pid of the child
95 * if this is the parent, or -1 on error.
96 */
97 public static int forkAndSpecialize(int uid, int gid, int[] gids, int debugFlags,
Andreas Gampeaec67dc2014-09-02 21:23:06 -070098 int[][] rlimits, int mountExternal, String seInfo, String niceName, int[] fdsToClose,
Andreas Gampe8dfa1782017-01-05 12:45:58 -080099 int[] fdsToIgnore, String instructionSet, String appDataDir) {
Narayan Kamath973b4662014-03-31 13:41:26 +0100100 VM_HOOKS.preFork();
Hiroshi Yamauchi1e3db872017-03-02 13:39:07 -0800101 // Resets nice priority for zygote process.
102 resetNicePriority();
Narayan Kamath973b4662014-03-31 13:41:26 +0100103 int pid = nativeForkAndSpecialize(
Andreas Gampeaec67dc2014-09-02 21:23:06 -0700104 uid, gid, gids, debugFlags, rlimits, mountExternal, seInfo, niceName, fdsToClose,
Andreas Gampe8dfa1782017-01-05 12:45:58 -0800105 fdsToIgnore, instructionSet, appDataDir);
Narayan Kamathfbb32f62015-06-12 15:34:35 +0100106 // Enable tracing as soon as possible for the child process.
107 if (pid == 0) {
John Reck62cc1192017-05-12 15:39:51 -0700108 Trace.setTracingEnabled(true, debugFlags);
Narayan Kamathfbb32f62015-06-12 15:34:35 +0100109
110 // Note that this event ends at the end of handleChildProc,
111 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "PostFork");
112 }
Narayan Kamath973b4662014-03-31 13:41:26 +0100113 VM_HOOKS.postForkCommon();
114 return pid;
115 }
116
117 native private static int nativeForkAndSpecialize(int uid, int gid, int[] gids,int debugFlags,
Andreas Gampeaec67dc2014-09-02 21:23:06 -0700118 int[][] rlimits, int mountExternal, String seInfo, String niceName, int[] fdsToClose,
Andreas Gampe8dfa1782017-01-05 12:45:58 -0800119 int[] fdsToIgnore, String instructionSet, String appDataDir);
Narayan Kamath973b4662014-03-31 13:41:26 +0100120
121 /**
Christopher Ferris2980de42017-06-20 16:13:40 -0700122 * Called to do any initialization before starting an application.
123 */
124 native static void nativePreApplicationInit();
125
126 /**
Narayan Kamath973b4662014-03-31 13:41:26 +0100127 * Special method to start the system server process. In addition to the
128 * common actions performed in forkAndSpecialize, the pid of the child
129 * process is recorded such that the death of the child process will cause
130 * zygote to exit.
131 *
132 * @param uid the UNIX uid that the new process should setuid() to after
133 * fork()ing and and before spawning any threads.
134 * @param gid the UNIX gid that the new process should setgid() to after
135 * fork()ing and and before spawning any threads.
136 * @param gids null-ok; a list of UNIX gids that the new process should
137 * setgroups() to after fork and before spawning any threads.
138 * @param debugFlags bit flags that enable debugging features.
139 * @param rlimits null-ok an array of rlimit tuples, with the second
140 * dimension having a length of 3 and representing
141 * (resource, rlim_cur, rlim_max). These are set via the posix
142 * setrlimit(2) call.
143 * @param permittedCapabilities argument for setcap()
144 * @param effectiveCapabilities argument for setcap()
145 *
146 * @return 0 if this is the child, pid of the child
147 * if this is the parent, or -1 on error.
148 */
149 public static int forkSystemServer(int uid, int gid, int[] gids, int debugFlags,
150 int[][] rlimits, long permittedCapabilities, long effectiveCapabilities) {
151 VM_HOOKS.preFork();
Hiroshi Yamauchi1e3db872017-03-02 13:39:07 -0800152 // Resets nice priority for zygote process.
153 resetNicePriority();
Narayan Kamath973b4662014-03-31 13:41:26 +0100154 int pid = nativeForkSystemServer(
155 uid, gid, gids, debugFlags, rlimits, permittedCapabilities, effectiveCapabilities);
Narayan Kamathfbb32f62015-06-12 15:34:35 +0100156 // Enable tracing as soon as we enter the system_server.
157 if (pid == 0) {
John Reck62cc1192017-05-12 15:39:51 -0700158 Trace.setTracingEnabled(true, debugFlags);
Narayan Kamathfbb32f62015-06-12 15:34:35 +0100159 }
Narayan Kamath973b4662014-03-31 13:41:26 +0100160 VM_HOOKS.postForkCommon();
161 return pid;
162 }
163
164 native private static int nativeForkSystemServer(int uid, int gid, int[] gids, int debugFlags,
165 int[][] rlimits, long permittedCapabilities, long effectiveCapabilities);
166
doheon1.lee885b7422016-01-20 13:07:27 +0900167 /**
Robert Sesek54e387d2016-12-02 17:27:50 -0500168 * Lets children of the zygote inherit open file descriptors to this path.
169 */
170 native protected static void nativeAllowFileAcrossFork(String path);
171
172 /**
doheon1.lee885b7422016-01-20 13:07:27 +0900173 * Zygote unmount storage space on initializing.
174 * This method is called once.
175 */
176 native protected static void nativeUnmountStorageOnInit();
177
Nicolas Geoffray3c43b382015-12-11 15:01:04 +0000178 private static void callPostForkChildHooks(int debugFlags, boolean isSystemServer,
179 String instructionSet) {
180 VM_HOOKS.postForkChild(debugFlags, isSystemServer, instructionSet);
Narayan Kamath973b4662014-03-31 13:41:26 +0100181 }
182
Narayan Kamathb49996d2017-02-06 20:24:08 +0000183 /**
Hiroshi Yamauchi1e3db872017-03-02 13:39:07 -0800184 * Resets the calling thread priority to the default value (Thread.NORM_PRIORITY
185 * or nice value 0). This updates both the priority value in java.lang.Thread and
186 * the nice value (setpriority).
Narayan Kamathb49996d2017-02-06 20:24:08 +0000187 */
Hiroshi Yamauchi1e3db872017-03-02 13:39:07 -0800188 static void resetNicePriority() {
189 Thread.currentThread().setPriority(Thread.NORM_PRIORITY);
190 }
Narayan Kamath973b4662014-03-31 13:41:26 +0100191
192 /**
193 * Executes "/system/bin/sh -c &lt;command&gt;" using the exec() system call.
194 * This method throws a runtime exception if exec() failed, otherwise, this
195 * method never returns.
196 *
197 * @param command The shell command to execute.
198 */
199 public static void execShell(String command) {
200 String[] args = { "/system/bin/sh", "-c", command };
201 try {
Elliott Hughes860c5912014-04-28 19:19:13 -0700202 Os.execv(args[0], args);
Narayan Kamath973b4662014-03-31 13:41:26 +0100203 } catch (ErrnoException e) {
204 throw new RuntimeException(e);
205 }
206 }
207
208 /**
209 * Appends quotes shell arguments to the specified string builder.
210 * The arguments are quoted using single-quotes, escaped if necessary,
211 * prefixed with a space, and appended to the command.
212 *
213 * @param command A string builder for the shell command being constructed.
214 * @param args An array of argument strings to be quoted and appended to the command.
215 * @see #execShell(String)
216 */
217 public static void appendQuotedShellArgs(StringBuilder command, String[] args) {
218 for (String arg : args) {
219 command.append(" '").append(arg.replace("'", "'\\''")).append("'");
220 }
221 }
Narayan Kamath973b4662014-03-31 13:41:26 +0100222}