blob: 22c543055b63e5bb521e644fbe01c6a7af870d60 [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 Carlstroma3d27182013-11-05 23:22:27 -080038#include "thread-inl.h"
William Roberts1e7d1d52013-11-11 07:28:37 -080039#include "utils.h"
Brian Carlstromcaabb1b2011-10-11 18:09:13 -070040
Elliott Hughesb4807ec2012-01-17 18:33:04 -080041#if defined(HAVE_PRCTL)
42#include <sys/prctl.h>
43#endif
44
Elliott Hughesc151f902012-06-21 20:33:21 -070045#include <selinux/android.h>
Elliott Hughesc151f902012-06-21 20:33:21 -070046
Brian Carlstrom154cef62012-05-02 22:34:03 -070047#if defined(__linux__)
48#include <sys/personality.h>
Nick Kralevichb7882782012-09-27 12:29:02 -070049#include <sys/utsname.h>
Ian Rogersef7d42f2014-01-06 12:55:46 -080050#if defined(HAVE_ANDROID_OS)
Nick Kralevich2b97c272013-03-01 11:14:38 -080051#include <sys/capability.h>
Brian Carlstrom154cef62012-05-02 22:34:03 -070052#endif
Ian Rogersef7d42f2014-01-06 12:55:46 -080053#endif
Brian Carlstrom154cef62012-05-02 22:34:03 -070054
Elliott Hughes01158d72011-09-19 19:47:10 -070055namespace art {
56
Brian Carlstromcaabb1b2011-10-11 18:09:13 -070057static pid_t gSystemServerPid = 0;
58
Elliott Hughes178cdcc2012-08-16 16:47:31 -070059// Must match values in dalvik.system.Zygote.
60enum MountExternalKind {
61 MOUNT_EXTERNAL_NONE = 0,
62 MOUNT_EXTERNAL_SINGLEUSER = 1,
63 MOUNT_EXTERNAL_MULTIUSER = 2,
Brian Carlstrombd86bcc2013-03-10 20:26:16 -070064 MOUNT_EXTERNAL_MULTIUSER_ALL = 3,
Elliott Hughes178cdcc2012-08-16 16:47:31 -070065};
66
Brian Carlstromcaabb1b2011-10-11 18:09:13 -070067// This signal handler is for zygote mode, since the zygote must reap its children
Elliott Hughes1bac54f2012-03-16 12:48:31 -070068static void SigChldHandler(int /*signal_number*/) {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -070069 pid_t pid;
70 int status;
71
72 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
73 // Log process-death status that we care about. In general it is
74 // not safe to call LOG(...) from a signal handler because of
75 // possible reentrancy. However, we know a priori that the
76 // current implementation of LOG() is safe to call from a SIGCHLD
77 // handler in the zygote process. If the LOG() implementation
78 // changes its locking strategy or its use of syscalls within the
79 // lazy-init critical section, its use here may become unsafe.
80 if (WIFEXITED(status)) {
81 if (WEXITSTATUS(status)) {
82 LOG(INFO) << "Process " << pid << " exited cleanly (" << WEXITSTATUS(status) << ")";
83 } else if (false) {
84 LOG(INFO) << "Process " << pid << " exited cleanly (" << WEXITSTATUS(status) << ")";
85 }
86 } else if (WIFSIGNALED(status)) {
87 if (WTERMSIG(status) != SIGKILL) {
88 LOG(INFO) << "Process " << pid << " terminated by signal (" << WTERMSIG(status) << ")";
89 } else if (false) {
90 LOG(INFO) << "Process " << pid << " terminated by signal (" << WTERMSIG(status) << ")";
91 }
92#ifdef WCOREDUMP
93 if (WCOREDUMP(status)) {
94 LOG(INFO) << "Process " << pid << " dumped core";
95 }
96#endif /* ifdef WCOREDUMP */
97 }
98
99 // If the just-crashed process is the system_server, bring down zygote
100 // so that it is restarted by init and system server will be restarted
101 // from there.
102 if (pid == gSystemServerPid) {
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -0700103 LOG(ERROR) << "Exit zygote because system server (" << pid << ") has terminated";
104 kill(getpid(), SIGKILL);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700105 }
106 }
107
108 if (pid < 0) {
109 PLOG(WARNING) << "Zygote SIGCHLD error in waitpid";
110 }
111}
112
Elliott Hughes0512f022012-03-15 22:10:52 -0700113// Configures the SIGCHLD handler for the zygote process. This is configured
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700114// very late, because earlier in the runtime we may fork() and exec()
115// other processes, and we want to waitpid() for those rather than
116// have them be harvested immediately.
117//
118// This ends up being called repeatedly before each fork(), but there's
119// no real harm in that.
Elliott Hughes0512f022012-03-15 22:10:52 -0700120static void SetSigChldHandler() {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700121 struct sigaction sa;
122 memset(&sa, 0, sizeof(sa));
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700123 sa.sa_handler = SigChldHandler;
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700124
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700125 int err = sigaction(SIGCHLD, &sa, NULL);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700126 if (err < 0) {
127 PLOG(WARNING) << "Error setting SIGCHLD handler";
128 }
129}
130
Elliott Hughes0512f022012-03-15 22:10:52 -0700131// Sets the SIGCHLD handler back to default behavior in zygote children.
132static void UnsetSigChldHandler() {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700133 struct sigaction sa;
134 memset(&sa, 0, sizeof(sa));
135 sa.sa_handler = SIG_DFL;
136
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700137 int err = sigaction(SIGCHLD, &sa, NULL);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700138 if (err < 0) {
139 PLOG(WARNING) << "Error unsetting SIGCHLD handler";
140 }
141}
142
143// Calls POSIX setgroups() using the int[] object as an argument.
144// A NULL argument is tolerated.
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700145static void SetGids(JNIEnv* env, jintArray javaGids) {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700146 if (javaGids == NULL) {
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700147 return;
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700148 }
149
150 COMPILE_ASSERT(sizeof(gid_t) == sizeof(jint), sizeof_gid_and_jint_are_differerent);
151 ScopedIntArrayRO gids(env, javaGids);
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700152 CHECK(gids.get() != NULL);
153 int rc = setgroups(gids.size(), reinterpret_cast<const gid_t*>(&gids[0]));
154 if (rc == -1) {
155 PLOG(FATAL) << "setgroups failed";
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700156 }
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700157}
158
159// Sets the resource limits via setrlimit(2) for the values in the
160// two-dimensional array of integers that's passed in. The second dimension
161// contains a tuple of length 3: (resource, rlim_cur, rlim_max). NULL is
162// treated as an empty array.
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700163static void SetRLimits(JNIEnv* env, jobjectArray javaRlimits) {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700164 if (javaRlimits == NULL) {
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700165 return;
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700166 }
167
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700168 rlimit rlim;
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700169 memset(&rlim, 0, sizeof(rlim));
170
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700171 for (int i = 0; i < env->GetArrayLength(javaRlimits); ++i) {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700172 ScopedLocalRef<jobject> javaRlimitObject(env, env->GetObjectArrayElement(javaRlimits, i));
173 ScopedIntArrayRO javaRlimit(env, reinterpret_cast<jintArray>(javaRlimitObject.get()));
174 if (javaRlimit.size() != 3) {
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700175 LOG(FATAL) << "rlimits array must have a second dimension of size 3";
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700176 }
177
178 rlim.rlim_cur = javaRlimit[1];
179 rlim.rlim_max = javaRlimit[2];
180
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700181 int rc = setrlimit(javaRlimit[0], &rlim);
182 if (rc == -1) {
183 PLOG(FATAL) << "setrlimit(" << javaRlimit[0] << ", "
184 << "{" << rlim.rlim_cur << ", " << rlim.rlim_max << "}) failed";
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700185 }
186 }
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700187}
188
Elliott Hughes76e36942012-03-16 13:44:56 -0700189#if defined(HAVE_ANDROID_OS)
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700190
191// The debug malloc library needs to know whether it's the zygote or a child.
192extern "C" int gMallocLeakZygoteChild;
193
194static void EnableDebugger() {
195 // To let a non-privileged gdbserver attach to this
196 // process, we must set our dumpable flag.
197 if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0) == -1) {
198 PLOG(ERROR) << "prctl(PR_SET_DUMPABLE) failed for pid " << getpid();
199 }
200 // We don't want core dumps, though, so set the core dump size to 0.
201 rlimit rl;
202 rl.rlim_cur = 0;
203 rl.rlim_max = RLIM_INFINITY;
204 if (setrlimit(RLIMIT_CORE, &rl) == -1) {
205 PLOG(ERROR) << "setrlimit(RLIMIT_CORE) failed for pid " << getpid();
206 }
207}
208
209static void EnableKeepCapabilities() {
210 int rc = prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0);
211 if (rc == -1) {
212 PLOG(FATAL) << "prctl(PR_SET_KEEPCAPS) failed";
213 }
214}
215
Nick Kralevicha0664be2013-02-13 14:39:17 -0800216static void DropCapabilitiesBoundingSet() {
217 for (int i = 0; prctl(PR_CAPBSET_READ, i, 0, 0, 0) >= 0; i++) {
218 if (i == CAP_NET_RAW) {
219 // Don't break /system/bin/ping
220 continue;
221 }
222 int rc = prctl(PR_CAPBSET_DROP, i, 0, 0, 0);
223 if (rc == -1) {
224 if (errno == EINVAL) {
225 PLOG(ERROR) << "prctl(PR_CAPBSET_DROP) failed with EINVAL. Please verify "
226 << "your kernel is compiled with file capabilities support";
227 } else {
228 PLOG(FATAL) << "prctl(PR_CAPBSET_DROP) failed";
229 }
230 }
231 }
232}
233
Elliott Hughes76e36942012-03-16 13:44:56 -0700234static void SetCapabilities(int64_t permitted, int64_t effective) {
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700235 __user_cap_header_struct capheader;
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700236 memset(&capheader, 0, sizeof(capheader));
Elliott Hughese1467652013-11-11 11:15:43 -0800237 capheader.version = _LINUX_CAPABILITY_VERSION_3;
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700238 capheader.pid = 0;
239
Elliott Hughese1467652013-11-11 11:15:43 -0800240 __user_cap_data_struct capdata[2];
241 memset(&capdata, 0, sizeof(capdata));
242 capdata[0].effective = effective;
243 capdata[1].effective = effective >> 32;
244 capdata[0].permitted = permitted;
245 capdata[1].permitted = permitted >> 32;
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700246
Elliott Hughese1467652013-11-11 11:15:43 -0800247 if (capset(&capheader, &capdata[0]) == -1) {
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800248 PLOG(FATAL) << "capset(" << permitted << ", " << effective << ") failed";
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700249 }
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700250}
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700251
252static void SetSchedulerPolicy() {
253 errno = -set_sched_policy(0, SP_DEFAULT);
254 if (errno != 0) {
255 PLOG(FATAL) << "set_sched_policy(0, SP_DEFAULT) failed";
256 }
257}
258
Elliott Hughes76e36942012-03-16 13:44:56 -0700259#else
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700260
261static int gMallocLeakZygoteChild = 0;
262
263static void EnableDebugger() {}
264static void EnableKeepCapabilities() {}
Brian Carlstrom9b7085a2013-07-18 15:15:21 -0700265static void DropCapabilitiesBoundingSet() {}
Elliott Hughes76e36942012-03-16 13:44:56 -0700266static void SetCapabilities(int64_t, int64_t) {}
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700267static void SetSchedulerPolicy() {}
268
Elliott Hughes76e36942012-03-16 13:44:56 -0700269#endif
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700270
Elliott Hughes0512f022012-03-15 22:10:52 -0700271static void EnableDebugFeatures(uint32_t debug_flags) {
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700272 // Must match values in dalvik.system.Zygote.
273 enum {
274 DEBUG_ENABLE_DEBUGGER = 1,
275 DEBUG_ENABLE_CHECKJNI = 1 << 1,
276 DEBUG_ENABLE_ASSERT = 1 << 2,
277 DEBUG_ENABLE_SAFEMODE = 1 << 3,
278 DEBUG_ENABLE_JNI_LOGGING = 1 << 4,
279 };
280
281 if ((debug_flags & DEBUG_ENABLE_CHECKJNI) != 0) {
282 Runtime* runtime = Runtime::Current();
283 JavaVMExt* vm = runtime->GetJavaVM();
284 if (!vm->check_jni) {
285 LOG(DEBUG) << "Late-enabling -Xcheck:jni";
Elliott Hughes88c5c352012-03-15 18:49:48 -0700286 vm->SetCheckJniEnabled(true);
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700287 // There's only one thread running at this point, so only one JNIEnv to fix up.
Elliott Hughes88c5c352012-03-15 18:49:48 -0700288 Thread::Current()->GetJniEnv()->SetCheckJniEnabled(true);
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700289 } else {
290 LOG(DEBUG) << "Not late-enabling -Xcheck:jni (already on)";
291 }
292 debug_flags &= ~DEBUG_ENABLE_CHECKJNI;
293 }
294
295 if ((debug_flags & DEBUG_ENABLE_JNI_LOGGING) != 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800296 gLogVerbosity.third_party_jni = true;
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700297 debug_flags &= ~DEBUG_ENABLE_JNI_LOGGING;
298 }
299
300 Dbg::SetJdwpAllowed((debug_flags & DEBUG_ENABLE_DEBUGGER) != 0);
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700301 if ((debug_flags & DEBUG_ENABLE_DEBUGGER) != 0) {
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700302 EnableDebugger();
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700303 }
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700304 debug_flags &= ~DEBUG_ENABLE_DEBUGGER;
305
306 // These two are for backwards compatibility with Dalvik.
307 debug_flags &= ~DEBUG_ENABLE_ASSERT;
308 debug_flags &= ~DEBUG_ENABLE_SAFEMODE;
309
310 if (debug_flags != 0) {
311 LOG(ERROR) << StringPrintf("Unknown bits set in debug_flags: %#x", debug_flags);
312 }
313}
314
Brian Carlstrombd86bcc2013-03-10 20:26:16 -0700315// Create a private mount namespace and bind mount appropriate emulated
316// storage for the given user.
317static bool MountEmulatedStorage(uid_t uid, jint mount_mode) {
318 if (mount_mode == MOUNT_EXTERNAL_NONE) {
319 return true;
Elliott Hughes178cdcc2012-08-16 16:47:31 -0700320 }
321
Brian Carlstrombd86bcc2013-03-10 20:26:16 -0700322 // See storage config details at http://source.android.com/tech/storage/
323 userid_t user_id = multiuser_get_user_id(uid);
Elliott Hughes178cdcc2012-08-16 16:47:31 -0700324
Brian Carlstrombd86bcc2013-03-10 20:26:16 -0700325 // Create a second private mount namespace for our process
Elliott Hughes178cdcc2012-08-16 16:47:31 -0700326 if (unshare(CLONE_NEWNS) == -1) {
Brian Carlstrombd86bcc2013-03-10 20:26:16 -0700327 PLOG(WARNING) << "Failed to unshare()";
328 return false;
Elliott Hughes178cdcc2012-08-16 16:47:31 -0700329 }
330
Brian Carlstrombd86bcc2013-03-10 20:26:16 -0700331 // Create bind mounts to expose external storage
332 if (mount_mode == MOUNT_EXTERNAL_MULTIUSER || mount_mode == MOUNT_EXTERNAL_MULTIUSER_ALL) {
333 // These paths must already be created by init.rc
334 const char* source = getenv("EMULATED_STORAGE_SOURCE");
335 const char* target = getenv("EMULATED_STORAGE_TARGET");
336 const char* legacy = getenv("EXTERNAL_STORAGE");
337 if (source == NULL || target == NULL || legacy == NULL) {
338 LOG(WARNING) << "Storage environment undefined; unable to provide external storage";
339 return false;
Elliott Hughes178cdcc2012-08-16 16:47:31 -0700340 }
Brian Carlstrombd86bcc2013-03-10 20:26:16 -0700341
342 // Prepare source paths
343
344 // /mnt/shell/emulated/0
345 std::string source_user(StringPrintf("%s/%d", source, user_id));
Brian Carlstrombd86bcc2013-03-10 20:26:16 -0700346 // /storage/emulated/0
347 std::string target_user(StringPrintf("%s/%d", target, user_id));
348
349 if (fs_prepare_dir(source_user.c_str(), 0000, 0, 0) == -1
Brian Carlstrombd86bcc2013-03-10 20:26:16 -0700350 || fs_prepare_dir(target_user.c_str(), 0000, 0, 0) == -1) {
351 return false;
352 }
353
354 if (mount_mode == MOUNT_EXTERNAL_MULTIUSER_ALL) {
355 // Mount entire external storage tree for all users
Brian Carlstrom01add2c2013-12-16 09:50:35 -0800356 if (TEMP_FAILURE_RETRY(mount(source, target, NULL, MS_BIND, NULL)) == -1) {
Brian Carlstrombd86bcc2013-03-10 20:26:16 -0700357 PLOG(WARNING) << "Failed to mount " << source << " to " << target;
358 return false;
359 }
360 } else {
361 // Only mount user-specific external storage
Brian Carlstrom01add2c2013-12-16 09:50:35 -0800362 if (TEMP_FAILURE_RETRY(
363 mount(source_user.c_str(), target_user.c_str(), NULL, MS_BIND, NULL)) == -1) {
Brian Carlstrombd86bcc2013-03-10 20:26:16 -0700364 PLOG(WARNING) << "Failed to mount " << source_user << " to " << target_user;
365 return false;
366 }
367 }
368
Jeff Sharkeyb8c46fc2013-09-10 18:28:10 -0700369 if (fs_prepare_dir(legacy, 0000, 0, 0) == -1) {
Brian Carlstrombd86bcc2013-03-10 20:26:16 -0700370 return false;
371 }
Brian Carlstrombd86bcc2013-03-10 20:26:16 -0700372
373 // Finally, mount user-specific path into place for legacy users
Brian Carlstrom01add2c2013-12-16 09:50:35 -0800374 if (TEMP_FAILURE_RETRY(
375 mount(target_user.c_str(), legacy, NULL, MS_BIND | MS_REC, NULL)) == -1) {
Brian Carlstrombd86bcc2013-03-10 20:26:16 -0700376 PLOG(WARNING) << "Failed to mount " << target_user << " to " << legacy;
377 return false;
Elliott Hughes178cdcc2012-08-16 16:47:31 -0700378 }
379 } else {
Brian Carlstrombd86bcc2013-03-10 20:26:16 -0700380 LOG(WARNING) << "Mount mode " << mount_mode << " unsupported";
381 return false;
Elliott Hughes178cdcc2012-08-16 16:47:31 -0700382 }
Brian Carlstrombd86bcc2013-03-10 20:26:16 -0700383
384 return true;
Elliott Hughes178cdcc2012-08-16 16:47:31 -0700385}
386
Nick Kralevichb7882782012-09-27 12:29:02 -0700387#if defined(__linux__)
388static bool NeedsNoRandomizeWorkaround() {
Nick Kralevich50e17442012-10-19 13:02:37 -0700389#if !defined(__arm__)
390 return false;
391#else
Nick Kralevichb7882782012-09-27 12:29:02 -0700392 int major;
393 int minor;
394 struct utsname uts;
395 if (uname(&uts) == -1) {
396 return false;
397 }
398
399 if (sscanf(uts.release, "%d.%d", &major, &minor) != 2) {
400 return false;
401 }
402
403 // Kernels before 3.4.* need the workaround.
404 return (major < 3) || ((major == 3) && (minor < 4));
Nick Kralevich50e17442012-10-19 13:02:37 -0700405#endif
Nick Kralevichb7882782012-09-27 12:29:02 -0700406}
407#endif
408
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700409// Utility routine to fork zygote and specialize the child process.
Elliott Hughes0512f022012-03-15 22:10:52 -0700410static pid_t ForkAndSpecializeCommon(JNIEnv* env, uid_t uid, gid_t gid, jintArray javaGids,
411 jint debug_flags, jobjectArray javaRlimits,
Elliott Hughesc151f902012-06-21 20:33:21 -0700412 jlong permittedCapabilities, jlong effectiveCapabilities,
Elliott Hughes178cdcc2012-08-16 16:47:31 -0700413 jint mount_external,
Brian Carlstrom01add2c2013-12-16 09:50:35 -0800414 jstring java_se_info, jstring java_se_name,
415 bool is_system_server) {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700416 Runtime* runtime = Runtime::Current();
417 CHECK(runtime->IsZygote()) << "runtime instance not started with -Xzygote";
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700418 if (!runtime->PreZygoteFork()) {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700419 LOG(FATAL) << "pre-fork heap failed";
420 }
421
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700422 SetSigChldHandler();
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700423
424 // Grab thread before fork potentially makes Thread::pthread_key_self_ unusable.
425 Thread* self = Thread::Current();
426
427 // dvmDumpLoaderStats("zygote"); // TODO: ?
428 pid_t pid = fork();
429
430 if (pid == 0) {
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700431 // The child process.
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700432 gMallocLeakZygoteChild = 1;
433
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700434 // Keep capabilities across UID change, unless we're staying root.
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700435 if (uid != 0) {
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700436 EnableKeepCapabilities();
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700437 }
438
Nick Kralevicha0664be2013-02-13 14:39:17 -0800439 DropCapabilitiesBoundingSet();
440
Brian Carlstrombd86bcc2013-03-10 20:26:16 -0700441 if (!MountEmulatedStorage(uid, mount_external)) {
442 PLOG(WARNING) << "Failed to mount emulated storage";
443 if (errno == ENOTCONN || errno == EROFS) {
444 // When device is actively encrypting, we get ENOTCONN here
445 // since FUSE was mounted before the framework restarted.
446 // When encrypted device is booting, we get EROFS since
447 // FUSE hasn't been created yet by init.
448 // In either case, continue without external storage.
449 } else {
450 LOG(FATAL) << "Cannot continue without emulated storage";
451 }
452 }
Elliott Hughes178cdcc2012-08-16 16:47:31 -0700453
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700454 SetGids(env, javaGids);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700455
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700456 SetRLimits(env, javaRlimits);
457
Nick Kralevich7e2364c2013-02-20 14:46:54 -0800458 int rc = setresgid(gid, gid, gid);
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700459 if (rc == -1) {
Nick Kralevich7e2364c2013-02-20 14:46:54 -0800460 PLOG(FATAL) << "setresgid(" << gid << ") failed";
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700461 }
462
Nick Kralevich7e2364c2013-02-20 14:46:54 -0800463 rc = setresuid(uid, uid, uid);
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700464 if (rc == -1) {
Nick Kralevich7e2364c2013-02-20 14:46:54 -0800465 PLOG(FATAL) << "setresuid(" << uid << ") failed";
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700466 }
467
Brian Carlstrom154cef62012-05-02 22:34:03 -0700468#if defined(__linux__)
Nick Kralevichb7882782012-09-27 12:29:02 -0700469 if (NeedsNoRandomizeWorkaround()) {
470 // Work around ARM kernel ASLR lossage (http://b/5817320).
471 int old_personality = personality(0xffffffff);
472 int new_personality = personality(old_personality | ADDR_NO_RANDOMIZE);
473 if (new_personality == -1) {
474 PLOG(WARNING) << "personality(" << new_personality << ") failed";
475 }
Elliott Hughes51e53862012-05-02 17:17:28 -0700476 }
Brian Carlstrom154cef62012-05-02 22:34:03 -0700477#endif
Elliott Hughes51e53862012-05-02 17:17:28 -0700478
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800479 SetCapabilities(permittedCapabilities, effectiveCapabilities);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700480
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700481 SetSchedulerPolicy();
Elliott Hughesb6636b82012-04-24 10:41:16 -0700482
Kenny Rootdc1cd102012-10-17 10:35:32 -0700483#if defined(HAVE_ANDROID_OS)
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700484 { // NOLINT(whitespace/braces)
Brian Carlstrom1a0b4752012-10-17 16:23:18 -0700485 const char* se_info_c_str = NULL;
486 UniquePtr<ScopedUtfChars> se_info;
487 if (java_se_info != NULL) {
488 se_info.reset(new ScopedUtfChars(env, java_se_info));
489 se_info_c_str = se_info->c_str();
490 CHECK(se_info_c_str != NULL);
491 }
492 const char* se_name_c_str = NULL;
493 UniquePtr<ScopedUtfChars> se_name;
494 if (java_se_name != NULL) {
495 se_name.reset(new ScopedUtfChars(env, java_se_name));
496 se_name_c_str = se_name->c_str();
497 CHECK(se_name_c_str != NULL);
498 }
499 rc = selinux_android_setcontext(uid, is_system_server, se_info_c_str, se_name_c_str);
Elliott Hughesc151f902012-06-21 20:33:21 -0700500 if (rc == -1) {
501 PLOG(FATAL) << "selinux_android_setcontext(" << uid << ", "
502 << (is_system_server ? "true" : "false") << ", "
Brian Carlstrom1a0b4752012-10-17 16:23:18 -0700503 << "\"" << se_info_c_str << "\", \"" << se_name_c_str << "\") failed";
Elliott Hughesc151f902012-06-21 20:33:21 -0700504 }
William Roberts1e7d1d52013-11-11 07:28:37 -0800505
506 // Make it easier to debug audit logs by setting the main thread's name to the
507 // nice name rather than "app_process".
508 if (se_info_c_str == NULL && is_system_server) {
509 se_name_c_str = "system_server";
510 }
511 if (se_info_c_str != NULL) {
512 SetThreadName(se_name_c_str);
513 }
Elliott Hughesc151f902012-06-21 20:33:21 -0700514 }
515#else
516 UNUSED(is_system_server);
517 UNUSED(java_se_info);
518 UNUSED(java_se_name);
519#endif
520
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700521 // Our system thread ID, etc, has changed so reset Thread state.
522 self->InitAfterFork();
523
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700524 EnableDebugFeatures(debug_flags);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700525
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700526 UnsetSigChldHandler();
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700527 runtime->DidForkFromZygote();
528 } else if (pid > 0) {
529 // the parent process
530 }
531 return pid;
532}
533
Elliott Hughes0512f022012-03-15 22:10:52 -0700534static jint Zygote_nativeForkAndSpecialize(JNIEnv* env, jclass, jint uid, jint gid, jintArray gids,
Brian Carlstrom01add2c2013-12-16 09:50:35 -0800535 jint debug_flags, jobjectArray rlimits,
536 jint mount_external, jstring se_info, jstring se_name) {
537 return ForkAndSpecializeCommon(env, uid, gid, gids, debug_flags, rlimits, 0, 0, mount_external,
538 se_info, se_name, false);
Brian Carlstroma9f19782011-10-13 00:14:47 -0700539}
540
Elliott Hughes0512f022012-03-15 22:10:52 -0700541static jint Zygote_nativeForkSystemServer(JNIEnv* env, jclass, uid_t uid, gid_t gid, jintArray gids,
542 jint debug_flags, jobjectArray rlimits,
Brian Carlstrom01add2c2013-12-16 09:50:35 -0800543 jlong permittedCapabilities,
544 jlong effectiveCapabilities) {
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700545 pid_t pid = ForkAndSpecializeCommon(env, uid, gid, gids,
546 debug_flags, rlimits,
Elliott Hughes178cdcc2012-08-16 16:47:31 -0700547 permittedCapabilities, effectiveCapabilities,
548 MOUNT_EXTERNAL_NONE, NULL, NULL, true);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700549 if (pid > 0) {
550 // The zygote process checks whether the child process has died or not.
551 LOG(INFO) << "System server process " << pid << " has been created";
552 gSystemServerPid = pid;
553 // There is a slight window that the system server process has crashed
554 // but it went unnoticed because we haven't published its pid yet. So
555 // we recheck here just to make sure that all is well.
556 int status;
557 if (waitpid(pid, &status, WNOHANG) == pid) {
558 LOG(FATAL) << "System server process " << pid << " has died. Restarting Zygote!";
559 }
560 }
561 return pid;
562}
563
Elliott Hughes01158d72011-09-19 19:47:10 -0700564static JNINativeMethod gMethods[] = {
Elliott Hughes178cdcc2012-08-16 16:47:31 -0700565 NATIVE_METHOD(Zygote, nativeForkAndSpecialize, "(II[II[[IILjava/lang/String;Ljava/lang/String;)I"),
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700566 NATIVE_METHOD(Zygote, nativeForkSystemServer, "(II[II[[IJJ)I"),
Elliott Hughes01158d72011-09-19 19:47:10 -0700567};
568
Elliott Hughes01158d72011-09-19 19:47:10 -0700569void register_dalvik_system_Zygote(JNIEnv* env) {
Elliott Hughes7756d542012-05-24 21:56:51 -0700570 REGISTER_NATIVE_METHODS("dalvik/system/Zygote");
Elliott Hughes01158d72011-09-19 19:47:10 -0700571}
572
573} // namespace art