blob: 75b6446bde1c22a92c4731539e31cf237eba87bf [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
20import dalvik.system.ZygoteHooks;
Elliott Hughes860c5912014-04-28 19:19:13 -070021import android.system.ErrnoException;
22import android.system.Os;
Narayan Kamath973b4662014-03-31 13:41:26 +010023
24/** @hide */
25public final class Zygote {
26 /*
27 * Bit values for "debugFlags" argument. The definitions are duplicated
28 * in the native code.
29 */
30
31 /** enable debugging over JDWP */
32 public static final int DEBUG_ENABLE_DEBUGGER = 1;
33 /** enable JNI checks */
34 public static final int DEBUG_ENABLE_CHECKJNI = 1 << 1;
35 /** enable Java programming language "assert" statements */
36 public static final int DEBUG_ENABLE_ASSERT = 1 << 2;
Mathieu Chartier7a490282015-03-17 09:51:36 -070037 /** disable the AOT compiler and JIT */
Narayan Kamath973b4662014-03-31 13:41:26 +010038 public static final int DEBUG_ENABLE_SAFEMODE = 1 << 3;
39 /** Enable logging of third-party JNI activity. */
40 public static final int DEBUG_ENABLE_JNI_LOGGING = 1 << 4;
Mathieu Chartier7a490282015-03-17 09:51:36 -070041 /** enable the JIT compiler */
42 public static final int DEBUG_ENABLE_JIT = 1 << 5;
43
Narayan Kamath973b4662014-03-31 13:41:26 +010044 /** No external storage should be mounted. */
45 public static final int MOUNT_EXTERNAL_NONE = 0;
Jeff Sharkey48877892015-03-18 11:27:19 -070046 /** Default user-specific external storage should be mounted. */
47 public static final int MOUNT_EXTERNAL_DEFAULT = 1;
Narayan Kamath973b4662014-03-31 13:41:26 +010048
49 private static final ZygoteHooks VM_HOOKS = new ZygoteHooks();
50
51 private Zygote() {}
52
53 /**
54 * Forks a new VM instance. The current VM must have been started
55 * with the -Xzygote flag. <b>NOTE: new instance keeps all
56 * root capabilities. The new process is expected to call capset()</b>.
57 *
58 * @param uid the UNIX uid that the new process should setuid() to after
59 * fork()ing and and before spawning any threads.
60 * @param gid the UNIX gid that the new process should setgid() to after
61 * fork()ing and and before spawning any threads.
62 * @param gids null-ok; a list of UNIX gids that the new process should
63 * setgroups() to after fork and before spawning any threads.
64 * @param debugFlags bit flags that enable debugging features.
65 * @param rlimits null-ok an array of rlimit tuples, with the second
66 * dimension having a length of 3 and representing
67 * (resource, rlim_cur, rlim_max). These are set via the posix
68 * setrlimit(2) call.
69 * @param seInfo null-ok a string specifying SELinux information for
70 * the new process.
71 * @param niceName null-ok a string specifying the process name.
72 * @param fdsToClose an array of ints, holding one or more POSIX
73 * file descriptor numbers that are to be closed by the child
74 * (and replaced by /dev/null) after forking. An integer value
75 * of -1 in any entry in the array means "ignore this one".
Andreas Gampeaec67dc2014-09-02 21:23:06 -070076 * @param instructionSet null-ok the instruction set to use.
jgu212eacd062014-09-10 06:55:07 -040077 * @param appDataDir null-ok the data directory of the app.
Narayan Kamath973b4662014-03-31 13:41:26 +010078 *
79 * @return 0 if this is the child, pid of the child
80 * if this is the parent, or -1 on error.
81 */
82 public static int forkAndSpecialize(int uid, int gid, int[] gids, int debugFlags,
Andreas Gampeaec67dc2014-09-02 21:23:06 -070083 int[][] rlimits, int mountExternal, String seInfo, String niceName, int[] fdsToClose,
jgu212eacd062014-09-10 06:55:07 -040084 String instructionSet, String appDataDir) {
Narayan Kamath973b4662014-03-31 13:41:26 +010085 VM_HOOKS.preFork();
86 int pid = nativeForkAndSpecialize(
Andreas Gampeaec67dc2014-09-02 21:23:06 -070087 uid, gid, gids, debugFlags, rlimits, mountExternal, seInfo, niceName, fdsToClose,
jgu212eacd062014-09-10 06:55:07 -040088 instructionSet, appDataDir);
Narayan Kamath973b4662014-03-31 13:41:26 +010089 VM_HOOKS.postForkCommon();
90 return pid;
91 }
92
93 native private static int nativeForkAndSpecialize(int uid, int gid, int[] gids,int debugFlags,
Andreas Gampeaec67dc2014-09-02 21:23:06 -070094 int[][] rlimits, int mountExternal, String seInfo, String niceName, int[] fdsToClose,
jgu212eacd062014-09-10 06:55:07 -040095 String instructionSet, String appDataDir);
Narayan Kamath973b4662014-03-31 13:41:26 +010096
97 /**
98 * Special method to start the system server process. In addition to the
99 * common actions performed in forkAndSpecialize, the pid of the child
100 * process is recorded such that the death of the child process will cause
101 * zygote to exit.
102 *
103 * @param uid the UNIX uid that the new process should setuid() to after
104 * fork()ing and and before spawning any threads.
105 * @param gid the UNIX gid that the new process should setgid() to after
106 * fork()ing and and before spawning any threads.
107 * @param gids null-ok; a list of UNIX gids that the new process should
108 * setgroups() to after fork and before spawning any threads.
109 * @param debugFlags bit flags that enable debugging features.
110 * @param rlimits null-ok an array of rlimit tuples, with the second
111 * dimension having a length of 3 and representing
112 * (resource, rlim_cur, rlim_max). These are set via the posix
113 * setrlimit(2) call.
114 * @param permittedCapabilities argument for setcap()
115 * @param effectiveCapabilities argument for setcap()
116 *
117 * @return 0 if this is the child, pid of the child
118 * if this is the parent, or -1 on error.
119 */
120 public static int forkSystemServer(int uid, int gid, int[] gids, int debugFlags,
121 int[][] rlimits, long permittedCapabilities, long effectiveCapabilities) {
122 VM_HOOKS.preFork();
123 int pid = nativeForkSystemServer(
124 uid, gid, gids, debugFlags, rlimits, permittedCapabilities, effectiveCapabilities);
125 VM_HOOKS.postForkCommon();
126 return pid;
127 }
128
129 native private static int nativeForkSystemServer(int uid, int gid, int[] gids, int debugFlags,
130 int[][] rlimits, long permittedCapabilities, long effectiveCapabilities);
131
Andreas Gampeaec67dc2014-09-02 21:23:06 -0700132 private static void callPostForkChildHooks(int debugFlags, String instructionSet) {
133 VM_HOOKS.postForkChild(debugFlags, instructionSet);
Narayan Kamath973b4662014-03-31 13:41:26 +0100134 }
135
136
137 /**
138 * Executes "/system/bin/sh -c &lt;command&gt;" using the exec() system call.
139 * This method throws a runtime exception if exec() failed, otherwise, this
140 * method never returns.
141 *
142 * @param command The shell command to execute.
143 */
144 public static void execShell(String command) {
145 String[] args = { "/system/bin/sh", "-c", command };
146 try {
Elliott Hughes860c5912014-04-28 19:19:13 -0700147 Os.execv(args[0], args);
Narayan Kamath973b4662014-03-31 13:41:26 +0100148 } catch (ErrnoException e) {
149 throw new RuntimeException(e);
150 }
151 }
152
153 /**
154 * Appends quotes shell arguments to the specified string builder.
155 * The arguments are quoted using single-quotes, escaped if necessary,
156 * prefixed with a space, and appended to the command.
157 *
158 * @param command A string builder for the shell command being constructed.
159 * @param args An array of argument strings to be quoted and appended to the command.
160 * @see #execShell(String)
161 */
162 public static void appendQuotedShellArgs(StringBuilder command, String[] args) {
163 for (String arg : args) {
164 command.append(" '").append(arg.replace("'", "'\\''")).append("'");
165 }
166 }
167}