blob: 919254a36a76f08e9d48654a86c5f4a3603ca29e [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
25/** @hide */
26public final class Zygote {
27 /*
28 * Bit values for "debugFlags" argument. The definitions are duplicated
29 * in the native code.
30 */
31
32 /** enable debugging over JDWP */
33 public static final int DEBUG_ENABLE_DEBUGGER = 1;
34 /** enable JNI checks */
35 public static final int DEBUG_ENABLE_CHECKJNI = 1 << 1;
36 /** enable Java programming language "assert" statements */
37 public static final int DEBUG_ENABLE_ASSERT = 1 << 2;
Mathieu Chartier7a490282015-03-17 09:51:36 -070038 /** disable the AOT compiler and JIT */
Narayan Kamath973b4662014-03-31 13:41:26 +010039 public static final int DEBUG_ENABLE_SAFEMODE = 1 << 3;
40 /** Enable logging of third-party JNI activity. */
41 public static final int DEBUG_ENABLE_JNI_LOGGING = 1 << 4;
David Srbecky065075e2015-05-28 17:16:09 +010042 /** Force generation of native debugging information. */
Nicolas Geoffray9abbf452015-11-05 11:29:42 +000043 public static final int DEBUG_GENERATE_DEBUG_INFO = 1 << 5;
Tamas Berghammerdf6cb282016-01-29 12:07:00 +000044 /** Always use JIT-ed code. */
45 public static final int DEBUG_ALWAYS_JIT = 1 << 6;
46 /** Make the code debuggable with turning off some optimizations. */
47 public static final int DEBUG_NATIVE_DEBUGGABLE = 1 << 7;
Mathieu Chartier7a490282015-03-17 09:51:36 -070048
Narayan Kamath973b4662014-03-31 13:41:26 +010049 /** No external storage should be mounted. */
50 public static final int MOUNT_EXTERNAL_NONE = 0;
Jeff Sharkey9527b222015-06-24 15:24:48 -070051 /** Default external storage should be mounted. */
Jeff Sharkey48877892015-03-18 11:27:19 -070052 public static final int MOUNT_EXTERNAL_DEFAULT = 1;
Jeff Sharkey9527b222015-06-24 15:24:48 -070053 /** Read-only external storage should be mounted. */
54 public static final int MOUNT_EXTERNAL_READ = 2;
55 /** Read-write external storage should be mounted. */
56 public static final int MOUNT_EXTERNAL_WRITE = 3;
Narayan Kamath973b4662014-03-31 13:41:26 +010057
58 private static final ZygoteHooks VM_HOOKS = new ZygoteHooks();
59
60 private Zygote() {}
61
62 /**
63 * Forks a new VM instance. The current VM must have been started
64 * with the -Xzygote flag. <b>NOTE: new instance keeps all
65 * root capabilities. The new process is expected to call capset()</b>.
66 *
67 * @param uid the UNIX uid that the new process should setuid() to after
68 * fork()ing and and before spawning any threads.
69 * @param gid the UNIX gid that the new process should setgid() to after
70 * fork()ing and and before spawning any threads.
71 * @param gids null-ok; a list of UNIX gids that the new process should
72 * setgroups() to after fork and before spawning any threads.
73 * @param debugFlags bit flags that enable debugging features.
74 * @param rlimits null-ok an array of rlimit tuples, with the second
75 * dimension having a length of 3 and representing
76 * (resource, rlim_cur, rlim_max). These are set via the posix
77 * setrlimit(2) call.
78 * @param seInfo null-ok a string specifying SELinux information for
79 * the new process.
80 * @param niceName null-ok a string specifying the process name.
81 * @param fdsToClose an array of ints, holding one or more POSIX
82 * file descriptor numbers that are to be closed by the child
83 * (and replaced by /dev/null) after forking. An integer value
84 * of -1 in any entry in the array means "ignore this one".
Andreas Gampeaec67dc2014-09-02 21:23:06 -070085 * @param instructionSet null-ok the instruction set to use.
jgu212eacd062014-09-10 06:55:07 -040086 * @param appDataDir null-ok the data directory of the app.
Narayan Kamath973b4662014-03-31 13:41:26 +010087 *
88 * @return 0 if this is the child, pid of the child
89 * if this is the parent, or -1 on error.
90 */
91 public static int forkAndSpecialize(int uid, int gid, int[] gids, int debugFlags,
Andreas Gampeaec67dc2014-09-02 21:23:06 -070092 int[][] rlimits, int mountExternal, String seInfo, String niceName, int[] fdsToClose,
jgu212eacd062014-09-10 06:55:07 -040093 String instructionSet, String appDataDir) {
Narayan Kamath973b4662014-03-31 13:41:26 +010094 VM_HOOKS.preFork();
95 int pid = nativeForkAndSpecialize(
Andreas Gampeaec67dc2014-09-02 21:23:06 -070096 uid, gid, gids, debugFlags, rlimits, mountExternal, seInfo, niceName, fdsToClose,
jgu212eacd062014-09-10 06:55:07 -040097 instructionSet, appDataDir);
Narayan Kamathfbb32f62015-06-12 15:34:35 +010098 // Enable tracing as soon as possible for the child process.
99 if (pid == 0) {
100 Trace.setTracingEnabled(true);
101
102 // Note that this event ends at the end of handleChildProc,
103 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "PostFork");
104 }
Narayan Kamath973b4662014-03-31 13:41:26 +0100105 VM_HOOKS.postForkCommon();
106 return pid;
107 }
108
109 native private static int nativeForkAndSpecialize(int uid, int gid, int[] gids,int debugFlags,
Andreas Gampeaec67dc2014-09-02 21:23:06 -0700110 int[][] rlimits, int mountExternal, String seInfo, String niceName, int[] fdsToClose,
jgu212eacd062014-09-10 06:55:07 -0400111 String instructionSet, String appDataDir);
Narayan Kamath973b4662014-03-31 13:41:26 +0100112
113 /**
114 * Special method to start the system server process. In addition to the
115 * common actions performed in forkAndSpecialize, the pid of the child
116 * process is recorded such that the death of the child process will cause
117 * zygote to exit.
118 *
119 * @param uid the UNIX uid that the new process should setuid() to after
120 * fork()ing and and before spawning any threads.
121 * @param gid the UNIX gid that the new process should setgid() to after
122 * fork()ing and and before spawning any threads.
123 * @param gids null-ok; a list of UNIX gids that the new process should
124 * setgroups() to after fork and before spawning any threads.
125 * @param debugFlags bit flags that enable debugging features.
126 * @param rlimits null-ok an array of rlimit tuples, with the second
127 * dimension having a length of 3 and representing
128 * (resource, rlim_cur, rlim_max). These are set via the posix
129 * setrlimit(2) call.
130 * @param permittedCapabilities argument for setcap()
131 * @param effectiveCapabilities argument for setcap()
132 *
133 * @return 0 if this is the child, pid of the child
134 * if this is the parent, or -1 on error.
135 */
136 public static int forkSystemServer(int uid, int gid, int[] gids, int debugFlags,
137 int[][] rlimits, long permittedCapabilities, long effectiveCapabilities) {
138 VM_HOOKS.preFork();
139 int pid = nativeForkSystemServer(
140 uid, gid, gids, debugFlags, rlimits, permittedCapabilities, effectiveCapabilities);
Narayan Kamathfbb32f62015-06-12 15:34:35 +0100141 // Enable tracing as soon as we enter the system_server.
142 if (pid == 0) {
143 Trace.setTracingEnabled(true);
144 }
Narayan Kamath973b4662014-03-31 13:41:26 +0100145 VM_HOOKS.postForkCommon();
146 return pid;
147 }
148
149 native private static int nativeForkSystemServer(int uid, int gid, int[] gids, int debugFlags,
150 int[][] rlimits, long permittedCapabilities, long effectiveCapabilities);
151
Nicolas Geoffray3c43b382015-12-11 15:01:04 +0000152 private static void callPostForkChildHooks(int debugFlags, boolean isSystemServer,
153 String instructionSet) {
154 VM_HOOKS.postForkChild(debugFlags, isSystemServer, instructionSet);
Narayan Kamath973b4662014-03-31 13:41:26 +0100155 }
156
157
158 /**
159 * Executes "/system/bin/sh -c &lt;command&gt;" using the exec() system call.
160 * This method throws a runtime exception if exec() failed, otherwise, this
161 * method never returns.
162 *
163 * @param command The shell command to execute.
164 */
165 public static void execShell(String command) {
166 String[] args = { "/system/bin/sh", "-c", command };
167 try {
Elliott Hughes860c5912014-04-28 19:19:13 -0700168 Os.execv(args[0], args);
Narayan Kamath973b4662014-03-31 13:41:26 +0100169 } catch (ErrnoException e) {
170 throw new RuntimeException(e);
171 }
172 }
173
174 /**
175 * Appends quotes shell arguments to the specified string builder.
176 * The arguments are quoted using single-quotes, escaped if necessary,
177 * prefixed with a space, and appended to the command.
178 *
179 * @param command A string builder for the shell command being constructed.
180 * @param args An array of argument strings to be quoted and appended to the command.
181 * @see #execShell(String)
182 */
183 public static void appendQuotedShellArgs(StringBuilder command, String[] args) {
184 for (String arg : args) {
185 command.append(" '").append(arg.replace("'", "'\\''")).append("'");
186 }
187 }
188}