blob: 2acacc995948c3aed6ba1a23a3152dd33220d9a4 [file] [log] [blame]
Elliott Hughes01158d72011-09-19 19:47:10 -07001/*
2 * Copyright (C) 2008 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
Brian Carlstrombd86bcc2013-03-10 20:26:16 -070017// sys/mount.h has to come before linux/fs.h due to redefinition of MS_RDONLY, MS_BIND, etc
18#include <sys/mount.h>
19#include <linux/fs.h>
20
Brian Carlstromcaabb1b2011-10-11 18:09:13 -070021#include <grp.h>
Elliott Hughes01158d72011-09-19 19:47:10 -070022#include <paths.h>
Elliott Hughesada2da92012-01-17 17:58:58 -080023#include <signal.h>
Elliott Hughes01158d72011-09-19 19:47:10 -070024#include <stdlib.h>
Brian Carlstromcaabb1b2011-10-11 18:09:13 -070025#include <sys/types.h>
26#include <sys/wait.h>
Elliott Hughes01158d72011-09-19 19:47:10 -070027#include <unistd.h>
28
Brian Carlstrombd86bcc2013-03-10 20:26:16 -070029#include "cutils/fs.h"
30#include "cutils/multiuser.h"
Elliott Hughesb6636b82012-04-24 10:41:16 -070031#include "cutils/sched_policy.h"
Elliott Hughes4ffd3132011-10-24 12:06:42 -070032#include "debugger.h"
33#include "jni_internal.h"
Elliott Hughes4ffd3132011-10-24 12:06:42 -070034#include "JNIHelp.h"
35#include "ScopedLocalRef.h"
36#include "ScopedPrimitiveArray.h"
37#include "ScopedUtfChars.h"
Brian Carlstromcaabb1b2011-10-11 18:09:13 -070038#include "thread.h"
39
Elliott Hughesb4807ec2012-01-17 18:33:04 -080040#if defined(HAVE_PRCTL)
41#include <sys/prctl.h>
42#endif
43
Elliott Hughesc151f902012-06-21 20:33:21 -070044#include <selinux/android.h>
Elliott Hughesc151f902012-06-21 20:33:21 -070045
Brian Carlstrom154cef62012-05-02 22:34:03 -070046#if defined(__linux__)
47#include <sys/personality.h>
Nick Kralevichb7882782012-09-27 12:29:02 -070048#include <sys/utsname.h>
Nick Kralevich2b97c272013-03-01 11:14:38 -080049#include <sys/capability.h>
Brian Carlstrom154cef62012-05-02 22:34:03 -070050#endif
51
Elliott Hughes01158d72011-09-19 19:47:10 -070052namespace art {
53
Brian Carlstromcaabb1b2011-10-11 18:09:13 -070054static pid_t gSystemServerPid = 0;
55
Elliott Hughes178cdcc2012-08-16 16:47:31 -070056// Must match values in dalvik.system.Zygote.
57enum MountExternalKind {
58 MOUNT_EXTERNAL_NONE = 0,
59 MOUNT_EXTERNAL_SINGLEUSER = 1,
60 MOUNT_EXTERNAL_MULTIUSER = 2,
Brian Carlstrombd86bcc2013-03-10 20:26:16 -070061 MOUNT_EXTERNAL_MULTIUSER_ALL = 3,
Elliott Hughes178cdcc2012-08-16 16:47:31 -070062};
63
Brian Carlstromcaabb1b2011-10-11 18:09:13 -070064// This signal handler is for zygote mode, since the zygote must reap its children
Elliott Hughes1bac54f2012-03-16 12:48:31 -070065static void SigChldHandler(int /*signal_number*/) {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -070066 pid_t pid;
67 int status;
68
69 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
70 // Log process-death status that we care about. In general it is
71 // not safe to call LOG(...) from a signal handler because of
72 // possible reentrancy. However, we know a priori that the
73 // current implementation of LOG() is safe to call from a SIGCHLD
74 // handler in the zygote process. If the LOG() implementation
75 // changes its locking strategy or its use of syscalls within the
76 // lazy-init critical section, its use here may become unsafe.
77 if (WIFEXITED(status)) {
78 if (WEXITSTATUS(status)) {
79 LOG(INFO) << "Process " << pid << " exited cleanly (" << WEXITSTATUS(status) << ")";
80 } else if (false) {
81 LOG(INFO) << "Process " << pid << " exited cleanly (" << WEXITSTATUS(status) << ")";
82 }
83 } else if (WIFSIGNALED(status)) {
84 if (WTERMSIG(status) != SIGKILL) {
85 LOG(INFO) << "Process " << pid << " terminated by signal (" << WTERMSIG(status) << ")";
86 } else if (false) {
87 LOG(INFO) << "Process " << pid << " terminated by signal (" << WTERMSIG(status) << ")";
88 }
89#ifdef WCOREDUMP
90 if (WCOREDUMP(status)) {
91 LOG(INFO) << "Process " << pid << " dumped core";
92 }
93#endif /* ifdef WCOREDUMP */
94 }
95
96 // If the just-crashed process is the system_server, bring down zygote
97 // so that it is restarted by init and system server will be restarted
98 // from there.
99 if (pid == gSystemServerPid) {
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -0700100 LOG(ERROR) << "Exit zygote because system server (" << pid << ") has terminated";
101 kill(getpid(), SIGKILL);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700102 }
103 }
104
105 if (pid < 0) {
106 PLOG(WARNING) << "Zygote SIGCHLD error in waitpid";
107 }
108}
109
Elliott Hughes0512f022012-03-15 22:10:52 -0700110// Configures the SIGCHLD handler for the zygote process. This is configured
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700111// very late, because earlier in the runtime we may fork() and exec()
112// other processes, and we want to waitpid() for those rather than
113// have them be harvested immediately.
114//
115// This ends up being called repeatedly before each fork(), but there's
116// no real harm in that.
Elliott Hughes0512f022012-03-15 22:10:52 -0700117static void SetSigChldHandler() {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700118 struct sigaction sa;
119 memset(&sa, 0, sizeof(sa));
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700120 sa.sa_handler = SigChldHandler;
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700121
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700122 int err = sigaction(SIGCHLD, &sa, NULL);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700123 if (err < 0) {
124 PLOG(WARNING) << "Error setting SIGCHLD handler";
125 }
126}
127
Elliott Hughes0512f022012-03-15 22:10:52 -0700128// Sets the SIGCHLD handler back to default behavior in zygote children.
129static void UnsetSigChldHandler() {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700130 struct sigaction sa;
131 memset(&sa, 0, sizeof(sa));
132 sa.sa_handler = SIG_DFL;
133
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700134 int err = sigaction(SIGCHLD, &sa, NULL);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700135 if (err < 0) {
136 PLOG(WARNING) << "Error unsetting SIGCHLD handler";
137 }
138}
139
140// Calls POSIX setgroups() using the int[] object as an argument.
141// A NULL argument is tolerated.
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700142static void SetGids(JNIEnv* env, jintArray javaGids) {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700143 if (javaGids == NULL) {
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700144 return;
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700145 }
146
147 COMPILE_ASSERT(sizeof(gid_t) == sizeof(jint), sizeof_gid_and_jint_are_differerent);
148 ScopedIntArrayRO gids(env, javaGids);
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700149 CHECK(gids.get() != NULL);
150 int rc = setgroups(gids.size(), reinterpret_cast<const gid_t*>(&gids[0]));
151 if (rc == -1) {
152 PLOG(FATAL) << "setgroups failed";
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700153 }
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700154}
155
156// Sets the resource limits via setrlimit(2) for the values in the
157// two-dimensional array of integers that's passed in. The second dimension
158// contains a tuple of length 3: (resource, rlim_cur, rlim_max). NULL is
159// treated as an empty array.
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700160static void SetRLimits(JNIEnv* env, jobjectArray javaRlimits) {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700161 if (javaRlimits == NULL) {
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700162 return;
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700163 }
164
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700165 rlimit rlim;
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700166 memset(&rlim, 0, sizeof(rlim));
167
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700168 for (int i = 0; i < env->GetArrayLength(javaRlimits); ++i) {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700169 ScopedLocalRef<jobject> javaRlimitObject(env, env->GetObjectArrayElement(javaRlimits, i));
170 ScopedIntArrayRO javaRlimit(env, reinterpret_cast<jintArray>(javaRlimitObject.get()));
171 if (javaRlimit.size() != 3) {
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700172 LOG(FATAL) << "rlimits array must have a second dimension of size 3";
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700173 }
174
175 rlim.rlim_cur = javaRlimit[1];
176 rlim.rlim_max = javaRlimit[2];
177
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700178 int rc = setrlimit(javaRlimit[0], &rlim);
179 if (rc == -1) {
180 PLOG(FATAL) << "setrlimit(" << javaRlimit[0] << ", "
181 << "{" << rlim.rlim_cur << ", " << rlim.rlim_max << "}) failed";
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700182 }
183 }
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700184}
185
Elliott Hughes76e36942012-03-16 13:44:56 -0700186#if defined(HAVE_ANDROID_OS)
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700187
188// The debug malloc library needs to know whether it's the zygote or a child.
189extern "C" int gMallocLeakZygoteChild;
190
191static void EnableDebugger() {
192 // To let a non-privileged gdbserver attach to this
193 // process, we must set our dumpable flag.
194 if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0) == -1) {
195 PLOG(ERROR) << "prctl(PR_SET_DUMPABLE) failed for pid " << getpid();
196 }
197 // We don't want core dumps, though, so set the core dump size to 0.
198 rlimit rl;
199 rl.rlim_cur = 0;
200 rl.rlim_max = RLIM_INFINITY;
201 if (setrlimit(RLIMIT_CORE, &rl) == -1) {
202 PLOG(ERROR) << "setrlimit(RLIMIT_CORE) failed for pid " << getpid();
203 }
204}
205
206static void EnableKeepCapabilities() {
207 int rc = prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0);
208 if (rc == -1) {
209 PLOG(FATAL) << "prctl(PR_SET_KEEPCAPS) failed";
210 }
211}
212
Nick Kralevicha0664be2013-02-13 14:39:17 -0800213static void DropCapabilitiesBoundingSet() {
214 for (int i = 0; prctl(PR_CAPBSET_READ, i, 0, 0, 0) >= 0; i++) {
215 if (i == CAP_NET_RAW) {
216 // Don't break /system/bin/ping
217 continue;
218 }
219 int rc = prctl(PR_CAPBSET_DROP, i, 0, 0, 0);
220 if (rc == -1) {
221 if (errno == EINVAL) {
222 PLOG(ERROR) << "prctl(PR_CAPBSET_DROP) failed with EINVAL. Please verify "
223 << "your kernel is compiled with file capabilities support";
224 } else {
225 PLOG(FATAL) << "prctl(PR_CAPBSET_DROP) failed";
226 }
227 }
228 }
229}
230
Elliott Hughes76e36942012-03-16 13:44:56 -0700231static void SetCapabilities(int64_t permitted, int64_t effective) {
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700232 __user_cap_header_struct capheader;
233 __user_cap_data_struct capdata;
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700234
235 memset(&capheader, 0, sizeof(capheader));
236 memset(&capdata, 0, sizeof(capdata));
237
238 capheader.version = _LINUX_CAPABILITY_VERSION;
239 capheader.pid = 0;
240
241 capdata.effective = effective;
242 capdata.permitted = permitted;
243
244 if (capset(&capheader, &capdata) != 0) {
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800245 PLOG(FATAL) << "capset(" << permitted << ", " << effective << ") failed";
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700246 }
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700247}
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700248
249static void SetSchedulerPolicy() {
250 errno = -set_sched_policy(0, SP_DEFAULT);
251 if (errno != 0) {
252 PLOG(FATAL) << "set_sched_policy(0, SP_DEFAULT) failed";
253 }
254}
255
Elliott Hughes76e36942012-03-16 13:44:56 -0700256#else
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700257
258static int gMallocLeakZygoteChild = 0;
259
260static void EnableDebugger() {}
261static void EnableKeepCapabilities() {}
Brian Carlstrom9b7085a2013-07-18 15:15:21 -0700262static void DropCapabilitiesBoundingSet() {}
Elliott Hughes76e36942012-03-16 13:44:56 -0700263static void SetCapabilities(int64_t, int64_t) {}
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700264static void SetSchedulerPolicy() {}
265
Elliott Hughes76e36942012-03-16 13:44:56 -0700266#endif
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700267
Elliott Hughes0512f022012-03-15 22:10:52 -0700268static void EnableDebugFeatures(uint32_t debug_flags) {
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700269 // Must match values in dalvik.system.Zygote.
270 enum {
271 DEBUG_ENABLE_DEBUGGER = 1,
272 DEBUG_ENABLE_CHECKJNI = 1 << 1,
273 DEBUG_ENABLE_ASSERT = 1 << 2,
274 DEBUG_ENABLE_SAFEMODE = 1 << 3,
275 DEBUG_ENABLE_JNI_LOGGING = 1 << 4,
276 };
277
278 if ((debug_flags & DEBUG_ENABLE_CHECKJNI) != 0) {
279 Runtime* runtime = Runtime::Current();
280 JavaVMExt* vm = runtime->GetJavaVM();
281 if (!vm->check_jni) {
282 LOG(DEBUG) << "Late-enabling -Xcheck:jni";
Elliott Hughes88c5c352012-03-15 18:49:48 -0700283 vm->SetCheckJniEnabled(true);
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700284 // There's only one thread running at this point, so only one JNIEnv to fix up.
Elliott Hughes88c5c352012-03-15 18:49:48 -0700285 Thread::Current()->GetJniEnv()->SetCheckJniEnabled(true);
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700286 } else {
287 LOG(DEBUG) << "Not late-enabling -Xcheck:jni (already on)";
288 }
289 debug_flags &= ~DEBUG_ENABLE_CHECKJNI;
290 }
291
292 if ((debug_flags & DEBUG_ENABLE_JNI_LOGGING) != 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800293 gLogVerbosity.third_party_jni = true;
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700294 debug_flags &= ~DEBUG_ENABLE_JNI_LOGGING;
295 }
296
297 Dbg::SetJdwpAllowed((debug_flags & DEBUG_ENABLE_DEBUGGER) != 0);
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700298 if ((debug_flags & DEBUG_ENABLE_DEBUGGER) != 0) {
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700299 EnableDebugger();
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700300 }
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700301 debug_flags &= ~DEBUG_ENABLE_DEBUGGER;
302
303 // These two are for backwards compatibility with Dalvik.
304 debug_flags &= ~DEBUG_ENABLE_ASSERT;
305 debug_flags &= ~DEBUG_ENABLE_SAFEMODE;
306
307 if (debug_flags != 0) {
308 LOG(ERROR) << StringPrintf("Unknown bits set in debug_flags: %#x", debug_flags);
309 }
310}
311
Brian Carlstrombd86bcc2013-03-10 20:26:16 -0700312// Create a private mount namespace and bind mount appropriate emulated
313// storage for the given user.
314static bool MountEmulatedStorage(uid_t uid, jint mount_mode) {
315 if (mount_mode == MOUNT_EXTERNAL_NONE) {
316 return true;
Elliott Hughes178cdcc2012-08-16 16:47:31 -0700317 }
318
Brian Carlstrombd86bcc2013-03-10 20:26:16 -0700319 // See storage config details at http://source.android.com/tech/storage/
320 userid_t user_id = multiuser_get_user_id(uid);
Elliott Hughes178cdcc2012-08-16 16:47:31 -0700321
Brian Carlstrombd86bcc2013-03-10 20:26:16 -0700322 // Create a second private mount namespace for our process
Elliott Hughes178cdcc2012-08-16 16:47:31 -0700323 if (unshare(CLONE_NEWNS) == -1) {
Brian Carlstrombd86bcc2013-03-10 20:26:16 -0700324 PLOG(WARNING) << "Failed to unshare()";
325 return false;
Elliott Hughes178cdcc2012-08-16 16:47:31 -0700326 }
327
Brian Carlstrombd86bcc2013-03-10 20:26:16 -0700328 // Create bind mounts to expose external storage
329 if (mount_mode == MOUNT_EXTERNAL_MULTIUSER || mount_mode == MOUNT_EXTERNAL_MULTIUSER_ALL) {
330 // These paths must already be created by init.rc
331 const char* source = getenv("EMULATED_STORAGE_SOURCE");
332 const char* target = getenv("EMULATED_STORAGE_TARGET");
333 const char* legacy = getenv("EXTERNAL_STORAGE");
334 if (source == NULL || target == NULL || legacy == NULL) {
335 LOG(WARNING) << "Storage environment undefined; unable to provide external storage";
336 return false;
Elliott Hughes178cdcc2012-08-16 16:47:31 -0700337 }
Brian Carlstrombd86bcc2013-03-10 20:26:16 -0700338
339 // Prepare source paths
340
341 // /mnt/shell/emulated/0
342 std::string source_user(StringPrintf("%s/%d", source, user_id));
Brian Carlstrombd86bcc2013-03-10 20:26:16 -0700343 // /storage/emulated/0
344 std::string target_user(StringPrintf("%s/%d", target, user_id));
345
346 if (fs_prepare_dir(source_user.c_str(), 0000, 0, 0) == -1
Brian Carlstrombd86bcc2013-03-10 20:26:16 -0700347 || fs_prepare_dir(target_user.c_str(), 0000, 0, 0) == -1) {
348 return false;
349 }
350
351 if (mount_mode == MOUNT_EXTERNAL_MULTIUSER_ALL) {
352 // Mount entire external storage tree for all users
353 if (mount(source, target, NULL, MS_BIND, NULL) == -1) {
354 PLOG(WARNING) << "Failed to mount " << source << " to " << target;
355 return false;
356 }
357 } else {
358 // Only mount user-specific external storage
359 if (mount(source_user.c_str(), target_user.c_str(), NULL, MS_BIND, NULL) == -1) {
360 PLOG(WARNING) << "Failed to mount " << source_user << " to " << target_user;
361 return false;
362 }
363 }
364
Jeff Sharkeyb8c46fc2013-09-10 18:28:10 -0700365 if (fs_prepare_dir(legacy, 0000, 0, 0) == -1) {
Brian Carlstrombd86bcc2013-03-10 20:26:16 -0700366 return false;
367 }
Brian Carlstrombd86bcc2013-03-10 20:26:16 -0700368
369 // Finally, mount user-specific path into place for legacy users
370 if (mount(target_user.c_str(), legacy, NULL, MS_BIND | MS_REC, NULL) == -1) {
371 PLOG(WARNING) << "Failed to mount " << target_user << " to " << legacy;
372 return false;
Elliott Hughes178cdcc2012-08-16 16:47:31 -0700373 }
374 } else {
Brian Carlstrombd86bcc2013-03-10 20:26:16 -0700375 LOG(WARNING) << "Mount mode " << mount_mode << " unsupported";
376 return false;
Elliott Hughes178cdcc2012-08-16 16:47:31 -0700377 }
Brian Carlstrombd86bcc2013-03-10 20:26:16 -0700378
379 return true;
Elliott Hughes178cdcc2012-08-16 16:47:31 -0700380}
381
Nick Kralevichb7882782012-09-27 12:29:02 -0700382#if defined(__linux__)
383static bool NeedsNoRandomizeWorkaround() {
Nick Kralevich50e17442012-10-19 13:02:37 -0700384#if !defined(__arm__)
385 return false;
386#else
Nick Kralevichb7882782012-09-27 12:29:02 -0700387 int major;
388 int minor;
389 struct utsname uts;
390 if (uname(&uts) == -1) {
391 return false;
392 }
393
394 if (sscanf(uts.release, "%d.%d", &major, &minor) != 2) {
395 return false;
396 }
397
398 // Kernels before 3.4.* need the workaround.
399 return (major < 3) || ((major == 3) && (minor < 4));
Nick Kralevich50e17442012-10-19 13:02:37 -0700400#endif
Nick Kralevichb7882782012-09-27 12:29:02 -0700401}
402#endif
403
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700404// Utility routine to fork zygote and specialize the child process.
Elliott Hughes0512f022012-03-15 22:10:52 -0700405static pid_t ForkAndSpecializeCommon(JNIEnv* env, uid_t uid, gid_t gid, jintArray javaGids,
406 jint debug_flags, jobjectArray javaRlimits,
Elliott Hughesc151f902012-06-21 20:33:21 -0700407 jlong permittedCapabilities, jlong effectiveCapabilities,
Elliott Hughes178cdcc2012-08-16 16:47:31 -0700408 jint mount_external,
Elliott Hughesc151f902012-06-21 20:33:21 -0700409 jstring java_se_info, jstring java_se_name, bool is_system_server) {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700410 Runtime* runtime = Runtime::Current();
411 CHECK(runtime->IsZygote()) << "runtime instance not started with -Xzygote";
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700412 if (!runtime->PreZygoteFork()) {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700413 LOG(FATAL) << "pre-fork heap failed";
414 }
415
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700416 SetSigChldHandler();
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700417
418 // Grab thread before fork potentially makes Thread::pthread_key_self_ unusable.
419 Thread* self = Thread::Current();
420
421 // dvmDumpLoaderStats("zygote"); // TODO: ?
422 pid_t pid = fork();
423
424 if (pid == 0) {
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700425 // The child process.
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700426 gMallocLeakZygoteChild = 1;
427
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700428 // Keep capabilities across UID change, unless we're staying root.
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700429 if (uid != 0) {
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700430 EnableKeepCapabilities();
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700431 }
432
Nick Kralevicha0664be2013-02-13 14:39:17 -0800433 DropCapabilitiesBoundingSet();
434
Brian Carlstrombd86bcc2013-03-10 20:26:16 -0700435 if (!MountEmulatedStorage(uid, mount_external)) {
436 PLOG(WARNING) << "Failed to mount emulated storage";
437 if (errno == ENOTCONN || errno == EROFS) {
438 // When device is actively encrypting, we get ENOTCONN here
439 // since FUSE was mounted before the framework restarted.
440 // When encrypted device is booting, we get EROFS since
441 // FUSE hasn't been created yet by init.
442 // In either case, continue without external storage.
443 } else {
444 LOG(FATAL) << "Cannot continue without emulated storage";
445 }
446 }
Elliott Hughes178cdcc2012-08-16 16:47:31 -0700447
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700448 SetGids(env, javaGids);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700449
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700450 SetRLimits(env, javaRlimits);
451
Nick Kralevich7e2364c2013-02-20 14:46:54 -0800452 int rc = setresgid(gid, gid, gid);
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700453 if (rc == -1) {
Nick Kralevich7e2364c2013-02-20 14:46:54 -0800454 PLOG(FATAL) << "setresgid(" << gid << ") failed";
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700455 }
456
Nick Kralevich7e2364c2013-02-20 14:46:54 -0800457 rc = setresuid(uid, uid, uid);
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700458 if (rc == -1) {
Nick Kralevich7e2364c2013-02-20 14:46:54 -0800459 PLOG(FATAL) << "setresuid(" << uid << ") failed";
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700460 }
461
Brian Carlstrom154cef62012-05-02 22:34:03 -0700462#if defined(__linux__)
Nick Kralevichb7882782012-09-27 12:29:02 -0700463 if (NeedsNoRandomizeWorkaround()) {
464 // Work around ARM kernel ASLR lossage (http://b/5817320).
465 int old_personality = personality(0xffffffff);
466 int new_personality = personality(old_personality | ADDR_NO_RANDOMIZE);
467 if (new_personality == -1) {
468 PLOG(WARNING) << "personality(" << new_personality << ") failed";
469 }
Elliott Hughes51e53862012-05-02 17:17:28 -0700470 }
Brian Carlstrom154cef62012-05-02 22:34:03 -0700471#endif
Elliott Hughes51e53862012-05-02 17:17:28 -0700472
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800473 SetCapabilities(permittedCapabilities, effectiveCapabilities);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700474
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700475 SetSchedulerPolicy();
Elliott Hughesb6636b82012-04-24 10:41:16 -0700476
Kenny Rootdc1cd102012-10-17 10:35:32 -0700477#if defined(HAVE_ANDROID_OS)
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700478 { // NOLINT(whitespace/braces)
Brian Carlstrom1a0b4752012-10-17 16:23:18 -0700479 const char* se_info_c_str = NULL;
480 UniquePtr<ScopedUtfChars> se_info;
481 if (java_se_info != NULL) {
482 se_info.reset(new ScopedUtfChars(env, java_se_info));
483 se_info_c_str = se_info->c_str();
484 CHECK(se_info_c_str != NULL);
485 }
486 const char* se_name_c_str = NULL;
487 UniquePtr<ScopedUtfChars> se_name;
488 if (java_se_name != NULL) {
489 se_name.reset(new ScopedUtfChars(env, java_se_name));
490 se_name_c_str = se_name->c_str();
491 CHECK(se_name_c_str != NULL);
492 }
493 rc = selinux_android_setcontext(uid, is_system_server, se_info_c_str, se_name_c_str);
Elliott Hughesc151f902012-06-21 20:33:21 -0700494 if (rc == -1) {
495 PLOG(FATAL) << "selinux_android_setcontext(" << uid << ", "
496 << (is_system_server ? "true" : "false") << ", "
Brian Carlstrom1a0b4752012-10-17 16:23:18 -0700497 << "\"" << se_info_c_str << "\", \"" << se_name_c_str << "\") failed";
Elliott Hughesc151f902012-06-21 20:33:21 -0700498 }
499 }
500#else
501 UNUSED(is_system_server);
502 UNUSED(java_se_info);
503 UNUSED(java_se_name);
504#endif
505
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700506 // Our system thread ID, etc, has changed so reset Thread state.
507 self->InitAfterFork();
508
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700509 EnableDebugFeatures(debug_flags);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700510
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700511 UnsetSigChldHandler();
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700512 runtime->DidForkFromZygote();
513 } else if (pid > 0) {
514 // the parent process
515 }
516 return pid;
517}
518
Elliott Hughes0512f022012-03-15 22:10:52 -0700519static jint Zygote_nativeForkAndSpecialize(JNIEnv* env, jclass, jint uid, jint gid, jintArray gids,
Elliott Hughes178cdcc2012-08-16 16:47:31 -0700520 jint debug_flags, jobjectArray rlimits, jint mount_external,
Elliott Hughesc151f902012-06-21 20:33:21 -0700521 jstring se_info, jstring se_name) {
Elliott Hughes178cdcc2012-08-16 16:47:31 -0700522 return ForkAndSpecializeCommon(env, uid, gid, gids, debug_flags, rlimits, 0, 0, mount_external, se_info, se_name, false);
Brian Carlstroma9f19782011-10-13 00:14:47 -0700523}
524
Elliott Hughes0512f022012-03-15 22:10:52 -0700525static jint Zygote_nativeForkSystemServer(JNIEnv* env, jclass, uid_t uid, gid_t gid, jintArray gids,
526 jint debug_flags, jobjectArray rlimits,
527 jlong permittedCapabilities, jlong effectiveCapabilities) {
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700528 pid_t pid = ForkAndSpecializeCommon(env, uid, gid, gids,
529 debug_flags, rlimits,
Elliott Hughes178cdcc2012-08-16 16:47:31 -0700530 permittedCapabilities, effectiveCapabilities,
531 MOUNT_EXTERNAL_NONE, NULL, NULL, true);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700532 if (pid > 0) {
533 // The zygote process checks whether the child process has died or not.
534 LOG(INFO) << "System server process " << pid << " has been created";
535 gSystemServerPid = pid;
536 // There is a slight window that the system server process has crashed
537 // but it went unnoticed because we haven't published its pid yet. So
538 // we recheck here just to make sure that all is well.
539 int status;
540 if (waitpid(pid, &status, WNOHANG) == pid) {
541 LOG(FATAL) << "System server process " << pid << " has died. Restarting Zygote!";
542 }
543 }
544 return pid;
545}
546
Elliott Hughes01158d72011-09-19 19:47:10 -0700547static JNINativeMethod gMethods[] = {
Elliott Hughes178cdcc2012-08-16 16:47:31 -0700548 NATIVE_METHOD(Zygote, nativeForkAndSpecialize, "(II[II[[IILjava/lang/String;Ljava/lang/String;)I"),
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700549 NATIVE_METHOD(Zygote, nativeForkSystemServer, "(II[II[[IJJ)I"),
Elliott Hughes01158d72011-09-19 19:47:10 -0700550};
551
Elliott Hughes01158d72011-09-19 19:47:10 -0700552void register_dalvik_system_Zygote(JNIEnv* env) {
Elliott Hughes7756d542012-05-24 21:56:51 -0700553 REGISTER_NATIVE_METHODS("dalvik/system/Zygote");
Elliott Hughes01158d72011-09-19 19:47:10 -0700554}
555
556} // namespace art