blob: f23326c4919aa24cd009541240d76110c11d7e84 [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;
37 /** disable the JIT compiler */
38 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;
41
42 /** No external storage should be mounted. */
43 public static final int MOUNT_EXTERNAL_NONE = 0;
44 /** Single-user external storage should be mounted. */
45 public static final int MOUNT_EXTERNAL_SINGLEUSER = 1;
46 /** Multi-user external storage should be mounted. */
47 public static final int MOUNT_EXTERNAL_MULTIUSER = 2;
48 /** All multi-user external storage should be mounted. */
49 public static final int MOUNT_EXTERNAL_MULTIUSER_ALL = 3;
50
51 private static final ZygoteHooks VM_HOOKS = new ZygoteHooks();
52
53 private Zygote() {}
54
55 /**
56 * Forks a new VM instance. The current VM must have been started
57 * with the -Xzygote flag. <b>NOTE: new instance keeps all
58 * root capabilities. The new process is expected to call capset()</b>.
59 *
60 * @param uid the UNIX uid that the new process should setuid() to after
61 * fork()ing and and before spawning any threads.
62 * @param gid the UNIX gid that the new process should setgid() to after
63 * fork()ing and and before spawning any threads.
64 * @param gids null-ok; a list of UNIX gids that the new process should
65 * setgroups() to after fork and before spawning any threads.
66 * @param debugFlags bit flags that enable debugging features.
67 * @param rlimits null-ok an array of rlimit tuples, with the second
68 * dimension having a length of 3 and representing
69 * (resource, rlim_cur, rlim_max). These are set via the posix
70 * setrlimit(2) call.
71 * @param seInfo null-ok a string specifying SELinux information for
72 * the new process.
73 * @param niceName null-ok a string specifying the process name.
74 * @param fdsToClose an array of ints, holding one or more POSIX
75 * file descriptor numbers that are to be closed by the child
76 * (and replaced by /dev/null) after forking. An integer value
77 * of -1 in any entry in the array means "ignore this one".
Andreas Gampeaec67dc2014-09-02 21:23:06 -070078 * @param instructionSet null-ok the instruction set to use.
Narayan Kamath973b4662014-03-31 13:41:26 +010079 *
80 * @return 0 if this is the child, pid of the child
81 * if this is the parent, or -1 on error.
82 */
83 public static int forkAndSpecialize(int uid, int gid, int[] gids, int debugFlags,
Andreas Gampeaec67dc2014-09-02 21:23:06 -070084 int[][] rlimits, int mountExternal, String seInfo, String niceName, int[] fdsToClose,
85 String instructionSet) {
Narayan Kamath973b4662014-03-31 13:41:26 +010086 VM_HOOKS.preFork();
87 int pid = nativeForkAndSpecialize(
Andreas Gampeaec67dc2014-09-02 21:23:06 -070088 uid, gid, gids, debugFlags, rlimits, mountExternal, seInfo, niceName, fdsToClose,
89 instructionSet);
Narayan Kamath973b4662014-03-31 13:41:26 +010090 VM_HOOKS.postForkCommon();
91 return pid;
92 }
93
94 native private static int nativeForkAndSpecialize(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,
96 String instructionSet);
Narayan Kamath973b4662014-03-31 13:41:26 +010097
98 /**
99 * Special method to start the system server process. In addition to the
100 * common actions performed in forkAndSpecialize, the pid of the child
101 * process is recorded such that the death of the child process will cause
102 * zygote to exit.
103 *
104 * @param uid the UNIX uid that the new process should setuid() to after
105 * fork()ing and and before spawning any threads.
106 * @param gid the UNIX gid that the new process should setgid() to after
107 * fork()ing and and before spawning any threads.
108 * @param gids null-ok; a list of UNIX gids that the new process should
109 * setgroups() to after fork and before spawning any threads.
110 * @param debugFlags bit flags that enable debugging features.
111 * @param rlimits null-ok an array of rlimit tuples, with the second
112 * dimension having a length of 3 and representing
113 * (resource, rlim_cur, rlim_max). These are set via the posix
114 * setrlimit(2) call.
115 * @param permittedCapabilities argument for setcap()
116 * @param effectiveCapabilities argument for setcap()
117 *
118 * @return 0 if this is the child, pid of the child
119 * if this is the parent, or -1 on error.
120 */
121 public static int forkSystemServer(int uid, int gid, int[] gids, int debugFlags,
122 int[][] rlimits, long permittedCapabilities, long effectiveCapabilities) {
123 VM_HOOKS.preFork();
124 int pid = nativeForkSystemServer(
125 uid, gid, gids, debugFlags, rlimits, permittedCapabilities, effectiveCapabilities);
126 VM_HOOKS.postForkCommon();
127 return pid;
128 }
129
130 native private static int nativeForkSystemServer(int uid, int gid, int[] gids, int debugFlags,
131 int[][] rlimits, long permittedCapabilities, long effectiveCapabilities);
132
Andreas Gampeaec67dc2014-09-02 21:23:06 -0700133 private static void callPostForkChildHooks(int debugFlags, String instructionSet) {
134 VM_HOOKS.postForkChild(debugFlags, instructionSet);
Narayan Kamath973b4662014-03-31 13:41:26 +0100135 }
136
137
138 /**
139 * Executes "/system/bin/sh -c &lt;command&gt;" using the exec() system call.
140 * This method throws a runtime exception if exec() failed, otherwise, this
141 * method never returns.
142 *
143 * @param command The shell command to execute.
144 */
145 public static void execShell(String command) {
146 String[] args = { "/system/bin/sh", "-c", command };
147 try {
Elliott Hughes860c5912014-04-28 19:19:13 -0700148 Os.execv(args[0], args);
Narayan Kamath973b4662014-03-31 13:41:26 +0100149 } catch (ErrnoException e) {
150 throw new RuntimeException(e);
151 }
152 }
153
154 /**
155 * Appends quotes shell arguments to the specified string builder.
156 * The arguments are quoted using single-quotes, escaped if necessary,
157 * prefixed with a space, and appended to the command.
158 *
159 * @param command A string builder for the shell command being constructed.
160 * @param args An array of argument strings to be quoted and appended to the command.
161 * @see #execShell(String)
162 */
163 public static void appendQuotedShellArgs(StringBuilder command, String[] args) {
164 for (String arg : args) {
165 command.append(" '").append(arg.replace("'", "'\\''")).append("'");
166 }
167 }
168}