blob: 7fa9457eb357494e413db74638fb64904641f43d [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>
Nick Kralevich2b97c272013-03-01 11:14:38 -080050#include <sys/capability.h>
Brian Carlstrom154cef62012-05-02 22:34:03 -070051#endif
52
Elliott Hughes01158d72011-09-19 19:47:10 -070053namespace art {
54
Brian Carlstromcaabb1b2011-10-11 18:09:13 -070055static pid_t gSystemServerPid = 0;
56
Elliott Hughes178cdcc2012-08-16 16:47:31 -070057// Must match values in dalvik.system.Zygote.
58enum MountExternalKind {
59 MOUNT_EXTERNAL_NONE = 0,
60 MOUNT_EXTERNAL_SINGLEUSER = 1,
61 MOUNT_EXTERNAL_MULTIUSER = 2,
Brian Carlstrombd86bcc2013-03-10 20:26:16 -070062 MOUNT_EXTERNAL_MULTIUSER_ALL = 3,
Elliott Hughes178cdcc2012-08-16 16:47:31 -070063};
64
Brian Carlstromcaabb1b2011-10-11 18:09:13 -070065// This signal handler is for zygote mode, since the zygote must reap its children
Elliott Hughes1bac54f2012-03-16 12:48:31 -070066static void SigChldHandler(int /*signal_number*/) {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -070067 pid_t pid;
68 int status;
69
70 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
71 // Log process-death status that we care about. In general it is
72 // not safe to call LOG(...) from a signal handler because of
73 // possible reentrancy. However, we know a priori that the
74 // current implementation of LOG() is safe to call from a SIGCHLD
75 // handler in the zygote process. If the LOG() implementation
76 // changes its locking strategy or its use of syscalls within the
77 // lazy-init critical section, its use here may become unsafe.
78 if (WIFEXITED(status)) {
79 if (WEXITSTATUS(status)) {
80 LOG(INFO) << "Process " << pid << " exited cleanly (" << WEXITSTATUS(status) << ")";
81 } else if (false) {
82 LOG(INFO) << "Process " << pid << " exited cleanly (" << WEXITSTATUS(status) << ")";
83 }
84 } else if (WIFSIGNALED(status)) {
85 if (WTERMSIG(status) != SIGKILL) {
86 LOG(INFO) << "Process " << pid << " terminated by signal (" << WTERMSIG(status) << ")";
87 } else if (false) {
88 LOG(INFO) << "Process " << pid << " terminated by signal (" << WTERMSIG(status) << ")";
89 }
90#ifdef WCOREDUMP
91 if (WCOREDUMP(status)) {
92 LOG(INFO) << "Process " << pid << " dumped core";
93 }
94#endif /* ifdef WCOREDUMP */
95 }
96
97 // If the just-crashed process is the system_server, bring down zygote
98 // so that it is restarted by init and system server will be restarted
99 // from there.
100 if (pid == gSystemServerPid) {
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -0700101 LOG(ERROR) << "Exit zygote because system server (" << pid << ") has terminated";
102 kill(getpid(), SIGKILL);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700103 }
104 }
105
106 if (pid < 0) {
107 PLOG(WARNING) << "Zygote SIGCHLD error in waitpid";
108 }
109}
110
Elliott Hughes0512f022012-03-15 22:10:52 -0700111// Configures the SIGCHLD handler for the zygote process. This is configured
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700112// very late, because earlier in the runtime we may fork() and exec()
113// other processes, and we want to waitpid() for those rather than
114// have them be harvested immediately.
115//
116// This ends up being called repeatedly before each fork(), but there's
117// no real harm in that.
Elliott Hughes0512f022012-03-15 22:10:52 -0700118static void SetSigChldHandler() {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700119 struct sigaction sa;
120 memset(&sa, 0, sizeof(sa));
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700121 sa.sa_handler = SigChldHandler;
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700122
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700123 int err = sigaction(SIGCHLD, &sa, NULL);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700124 if (err < 0) {
125 PLOG(WARNING) << "Error setting SIGCHLD handler";
126 }
127}
128
Elliott Hughes0512f022012-03-15 22:10:52 -0700129// Sets the SIGCHLD handler back to default behavior in zygote children.
130static void UnsetSigChldHandler() {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700131 struct sigaction sa;
132 memset(&sa, 0, sizeof(sa));
133 sa.sa_handler = SIG_DFL;
134
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700135 int err = sigaction(SIGCHLD, &sa, NULL);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700136 if (err < 0) {
137 PLOG(WARNING) << "Error unsetting SIGCHLD handler";
138 }
139}
140
141// Calls POSIX setgroups() using the int[] object as an argument.
142// A NULL argument is tolerated.
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700143static void SetGids(JNIEnv* env, jintArray javaGids) {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700144 if (javaGids == NULL) {
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700145 return;
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700146 }
147
148 COMPILE_ASSERT(sizeof(gid_t) == sizeof(jint), sizeof_gid_and_jint_are_differerent);
149 ScopedIntArrayRO gids(env, javaGids);
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700150 CHECK(gids.get() != NULL);
151 int rc = setgroups(gids.size(), reinterpret_cast<const gid_t*>(&gids[0]));
152 if (rc == -1) {
153 PLOG(FATAL) << "setgroups failed";
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700154 }
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700155}
156
157// Sets the resource limits via setrlimit(2) for the values in the
158// two-dimensional array of integers that's passed in. The second dimension
159// contains a tuple of length 3: (resource, rlim_cur, rlim_max). NULL is
160// treated as an empty array.
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700161static void SetRLimits(JNIEnv* env, jobjectArray javaRlimits) {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700162 if (javaRlimits == NULL) {
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700163 return;
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700164 }
165
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700166 rlimit rlim;
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700167 memset(&rlim, 0, sizeof(rlim));
168
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700169 for (int i = 0; i < env->GetArrayLength(javaRlimits); ++i) {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700170 ScopedLocalRef<jobject> javaRlimitObject(env, env->GetObjectArrayElement(javaRlimits, i));
171 ScopedIntArrayRO javaRlimit(env, reinterpret_cast<jintArray>(javaRlimitObject.get()));
172 if (javaRlimit.size() != 3) {
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700173 LOG(FATAL) << "rlimits array must have a second dimension of size 3";
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700174 }
175
176 rlim.rlim_cur = javaRlimit[1];
177 rlim.rlim_max = javaRlimit[2];
178
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700179 int rc = setrlimit(javaRlimit[0], &rlim);
180 if (rc == -1) {
181 PLOG(FATAL) << "setrlimit(" << javaRlimit[0] << ", "
182 << "{" << rlim.rlim_cur << ", " << rlim.rlim_max << "}) failed";
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700183 }
184 }
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700185}
186
Elliott Hughes76e36942012-03-16 13:44:56 -0700187#if defined(HAVE_ANDROID_OS)
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700188
189// The debug malloc library needs to know whether it's the zygote or a child.
190extern "C" int gMallocLeakZygoteChild;
191
192static void EnableDebugger() {
193 // To let a non-privileged gdbserver attach to this
194 // process, we must set our dumpable flag.
195 if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0) == -1) {
196 PLOG(ERROR) << "prctl(PR_SET_DUMPABLE) failed for pid " << getpid();
197 }
198 // We don't want core dumps, though, so set the core dump size to 0.
199 rlimit rl;
200 rl.rlim_cur = 0;
201 rl.rlim_max = RLIM_INFINITY;
202 if (setrlimit(RLIMIT_CORE, &rl) == -1) {
203 PLOG(ERROR) << "setrlimit(RLIMIT_CORE) failed for pid " << getpid();
204 }
205}
206
207static void EnableKeepCapabilities() {
208 int rc = prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0);
209 if (rc == -1) {
210 PLOG(FATAL) << "prctl(PR_SET_KEEPCAPS) failed";
211 }
212}
213
Nick Kralevicha0664be2013-02-13 14:39:17 -0800214static void DropCapabilitiesBoundingSet() {
215 for (int i = 0; prctl(PR_CAPBSET_READ, i, 0, 0, 0) >= 0; i++) {
216 if (i == CAP_NET_RAW) {
217 // Don't break /system/bin/ping
218 continue;
219 }
220 int rc = prctl(PR_CAPBSET_DROP, i, 0, 0, 0);
221 if (rc == -1) {
222 if (errno == EINVAL) {
223 PLOG(ERROR) << "prctl(PR_CAPBSET_DROP) failed with EINVAL. Please verify "
224 << "your kernel is compiled with file capabilities support";
225 } else {
226 PLOG(FATAL) << "prctl(PR_CAPBSET_DROP) failed";
227 }
228 }
229 }
230}
231
Elliott Hughes76e36942012-03-16 13:44:56 -0700232static void SetCapabilities(int64_t permitted, int64_t effective) {
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700233 __user_cap_header_struct capheader;
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700234 memset(&capheader, 0, sizeof(capheader));
Elliott Hughese1467652013-11-11 11:15:43 -0800235 capheader.version = _LINUX_CAPABILITY_VERSION_3;
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700236 capheader.pid = 0;
237
Elliott Hughese1467652013-11-11 11:15:43 -0800238 __user_cap_data_struct capdata[2];
239 memset(&capdata, 0, sizeof(capdata));
240 capdata[0].effective = effective;
241 capdata[1].effective = effective >> 32;
242 capdata[0].permitted = permitted;
243 capdata[1].permitted = permitted >> 32;
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700244
Elliott Hughese1467652013-11-11 11:15:43 -0800245 if (capset(&capheader, &capdata[0]) == -1) {
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800246 PLOG(FATAL) << "capset(" << permitted << ", " << effective << ") failed";
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700247 }
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700248}
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700249
250static void SetSchedulerPolicy() {
251 errno = -set_sched_policy(0, SP_DEFAULT);
252 if (errno != 0) {
253 PLOG(FATAL) << "set_sched_policy(0, SP_DEFAULT) failed";
254 }
255}
256
Elliott Hughes76e36942012-03-16 13:44:56 -0700257#else
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700258
259static int gMallocLeakZygoteChild = 0;
260
261static void EnableDebugger() {}
262static void EnableKeepCapabilities() {}
Brian Carlstrom9b7085a2013-07-18 15:15:21 -0700263static void DropCapabilitiesBoundingSet() {}
Elliott Hughes76e36942012-03-16 13:44:56 -0700264static void SetCapabilities(int64_t, int64_t) {}
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700265static void SetSchedulerPolicy() {}
266
Elliott Hughes76e36942012-03-16 13:44:56 -0700267#endif
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700268
Elliott Hughes0512f022012-03-15 22:10:52 -0700269static void EnableDebugFeatures(uint32_t debug_flags) {
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700270 // Must match values in dalvik.system.Zygote.
271 enum {
272 DEBUG_ENABLE_DEBUGGER = 1,
273 DEBUG_ENABLE_CHECKJNI = 1 << 1,
274 DEBUG_ENABLE_ASSERT = 1 << 2,
275 DEBUG_ENABLE_SAFEMODE = 1 << 3,
276 DEBUG_ENABLE_JNI_LOGGING = 1 << 4,
277 };
278
279 if ((debug_flags & DEBUG_ENABLE_CHECKJNI) != 0) {
280 Runtime* runtime = Runtime::Current();
281 JavaVMExt* vm = runtime->GetJavaVM();
282 if (!vm->check_jni) {
283 LOG(DEBUG) << "Late-enabling -Xcheck:jni";
Elliott Hughes88c5c352012-03-15 18:49:48 -0700284 vm->SetCheckJniEnabled(true);
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700285 // There's only one thread running at this point, so only one JNIEnv to fix up.
Elliott Hughes88c5c352012-03-15 18:49:48 -0700286 Thread::Current()->GetJniEnv()->SetCheckJniEnabled(true);
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700287 } else {
288 LOG(DEBUG) << "Not late-enabling -Xcheck:jni (already on)";
289 }
290 debug_flags &= ~DEBUG_ENABLE_CHECKJNI;
291 }
292
293 if ((debug_flags & DEBUG_ENABLE_JNI_LOGGING) != 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800294 gLogVerbosity.third_party_jni = true;
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700295 debug_flags &= ~DEBUG_ENABLE_JNI_LOGGING;
296 }
297
298 Dbg::SetJdwpAllowed((debug_flags & DEBUG_ENABLE_DEBUGGER) != 0);
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700299 if ((debug_flags & DEBUG_ENABLE_DEBUGGER) != 0) {
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700300 EnableDebugger();
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700301 }
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700302 debug_flags &= ~DEBUG_ENABLE_DEBUGGER;
303
304 // These two are for backwards compatibility with Dalvik.
305 debug_flags &= ~DEBUG_ENABLE_ASSERT;
306 debug_flags &= ~DEBUG_ENABLE_SAFEMODE;
307
308 if (debug_flags != 0) {
309 LOG(ERROR) << StringPrintf("Unknown bits set in debug_flags: %#x", debug_flags);
310 }
311}
312
Brian Carlstrombd86bcc2013-03-10 20:26:16 -0700313// Create a private mount namespace and bind mount appropriate emulated
314// storage for the given user.
315static bool MountEmulatedStorage(uid_t uid, jint mount_mode) {
316 if (mount_mode == MOUNT_EXTERNAL_NONE) {
317 return true;
Elliott Hughes178cdcc2012-08-16 16:47:31 -0700318 }
319
Brian Carlstrombd86bcc2013-03-10 20:26:16 -0700320 // See storage config details at http://source.android.com/tech/storage/
321 userid_t user_id = multiuser_get_user_id(uid);
Elliott Hughes178cdcc2012-08-16 16:47:31 -0700322
Brian Carlstrombd86bcc2013-03-10 20:26:16 -0700323 // Create a second private mount namespace for our process
Elliott Hughes178cdcc2012-08-16 16:47:31 -0700324 if (unshare(CLONE_NEWNS) == -1) {
Brian Carlstrombd86bcc2013-03-10 20:26:16 -0700325 PLOG(WARNING) << "Failed to unshare()";
326 return false;
Elliott Hughes178cdcc2012-08-16 16:47:31 -0700327 }
328
Brian Carlstrombd86bcc2013-03-10 20:26:16 -0700329 // Create bind mounts to expose external storage
330 if (mount_mode == MOUNT_EXTERNAL_MULTIUSER || mount_mode == MOUNT_EXTERNAL_MULTIUSER_ALL) {
331 // These paths must already be created by init.rc
332 const char* source = getenv("EMULATED_STORAGE_SOURCE");
333 const char* target = getenv("EMULATED_STORAGE_TARGET");
334 const char* legacy = getenv("EXTERNAL_STORAGE");
335 if (source == NULL || target == NULL || legacy == NULL) {
336 LOG(WARNING) << "Storage environment undefined; unable to provide external storage";
337 return false;
Elliott Hughes178cdcc2012-08-16 16:47:31 -0700338 }
Brian Carlstrombd86bcc2013-03-10 20:26:16 -0700339
340 // Prepare source paths
341
342 // /mnt/shell/emulated/0
343 std::string source_user(StringPrintf("%s/%d", source, user_id));
Brian Carlstrombd86bcc2013-03-10 20:26:16 -0700344 // /storage/emulated/0
345 std::string target_user(StringPrintf("%s/%d", target, user_id));
346
347 if (fs_prepare_dir(source_user.c_str(), 0000, 0, 0) == -1
Brian Carlstrombd86bcc2013-03-10 20:26:16 -0700348 || fs_prepare_dir(target_user.c_str(), 0000, 0, 0) == -1) {
349 return false;
350 }
351
352 if (mount_mode == MOUNT_EXTERNAL_MULTIUSER_ALL) {
353 // Mount entire external storage tree for all users
Brian Carlstrom01add2c2013-12-16 09:50:35 -0800354 if (TEMP_FAILURE_RETRY(mount(source, target, NULL, MS_BIND, NULL)) == -1) {
Brian Carlstrombd86bcc2013-03-10 20:26:16 -0700355 PLOG(WARNING) << "Failed to mount " << source << " to " << target;
356 return false;
357 }
358 } else {
359 // Only mount user-specific external storage
Brian Carlstrom01add2c2013-12-16 09:50:35 -0800360 if (TEMP_FAILURE_RETRY(
361 mount(source_user.c_str(), target_user.c_str(), NULL, MS_BIND, NULL)) == -1) {
Brian Carlstrombd86bcc2013-03-10 20:26:16 -0700362 PLOG(WARNING) << "Failed to mount " << source_user << " to " << target_user;
363 return false;
364 }
365 }
366
Jeff Sharkeyb8c46fc2013-09-10 18:28:10 -0700367 if (fs_prepare_dir(legacy, 0000, 0, 0) == -1) {
Brian Carlstrombd86bcc2013-03-10 20:26:16 -0700368 return false;
369 }
Brian Carlstrombd86bcc2013-03-10 20:26:16 -0700370
371 // Finally, mount user-specific path into place for legacy users
Brian Carlstrom01add2c2013-12-16 09:50:35 -0800372 if (TEMP_FAILURE_RETRY(
373 mount(target_user.c_str(), legacy, NULL, MS_BIND | MS_REC, NULL)) == -1) {
Brian Carlstrombd86bcc2013-03-10 20:26:16 -0700374 PLOG(WARNING) << "Failed to mount " << target_user << " to " << legacy;
375 return false;
Elliott Hughes178cdcc2012-08-16 16:47:31 -0700376 }
377 } else {
Brian Carlstrombd86bcc2013-03-10 20:26:16 -0700378 LOG(WARNING) << "Mount mode " << mount_mode << " unsupported";
379 return false;
Elliott Hughes178cdcc2012-08-16 16:47:31 -0700380 }
Brian Carlstrombd86bcc2013-03-10 20:26:16 -0700381
382 return true;
Elliott Hughes178cdcc2012-08-16 16:47:31 -0700383}
384
Nick Kralevichb7882782012-09-27 12:29:02 -0700385#if defined(__linux__)
386static bool NeedsNoRandomizeWorkaround() {
Nick Kralevich50e17442012-10-19 13:02:37 -0700387#if !defined(__arm__)
388 return false;
389#else
Nick Kralevichb7882782012-09-27 12:29:02 -0700390 int major;
391 int minor;
392 struct utsname uts;
393 if (uname(&uts) == -1) {
394 return false;
395 }
396
397 if (sscanf(uts.release, "%d.%d", &major, &minor) != 2) {
398 return false;
399 }
400
401 // Kernels before 3.4.* need the workaround.
402 return (major < 3) || ((major == 3) && (minor < 4));
Nick Kralevich50e17442012-10-19 13:02:37 -0700403#endif
Nick Kralevichb7882782012-09-27 12:29:02 -0700404}
405#endif
406
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700407// Utility routine to fork zygote and specialize the child process.
Elliott Hughes0512f022012-03-15 22:10:52 -0700408static pid_t ForkAndSpecializeCommon(JNIEnv* env, uid_t uid, gid_t gid, jintArray javaGids,
409 jint debug_flags, jobjectArray javaRlimits,
Elliott Hughesc151f902012-06-21 20:33:21 -0700410 jlong permittedCapabilities, jlong effectiveCapabilities,
Elliott Hughes178cdcc2012-08-16 16:47:31 -0700411 jint mount_external,
Brian Carlstrom01add2c2013-12-16 09:50:35 -0800412 jstring java_se_info, jstring java_se_name,
413 bool is_system_server) {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700414 Runtime* runtime = Runtime::Current();
415 CHECK(runtime->IsZygote()) << "runtime instance not started with -Xzygote";
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700416 if (!runtime->PreZygoteFork()) {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700417 LOG(FATAL) << "pre-fork heap failed";
418 }
419
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700420 SetSigChldHandler();
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700421
422 // Grab thread before fork potentially makes Thread::pthread_key_self_ unusable.
423 Thread* self = Thread::Current();
424
425 // dvmDumpLoaderStats("zygote"); // TODO: ?
426 pid_t pid = fork();
427
428 if (pid == 0) {
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700429 // The child process.
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700430 gMallocLeakZygoteChild = 1;
431
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700432 // Keep capabilities across UID change, unless we're staying root.
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700433 if (uid != 0) {
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700434 EnableKeepCapabilities();
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700435 }
436
Nick Kralevicha0664be2013-02-13 14:39:17 -0800437 DropCapabilitiesBoundingSet();
438
Brian Carlstrombd86bcc2013-03-10 20:26:16 -0700439 if (!MountEmulatedStorage(uid, mount_external)) {
440 PLOG(WARNING) << "Failed to mount emulated storage";
441 if (errno == ENOTCONN || errno == EROFS) {
442 // When device is actively encrypting, we get ENOTCONN here
443 // since FUSE was mounted before the framework restarted.
444 // When encrypted device is booting, we get EROFS since
445 // FUSE hasn't been created yet by init.
446 // In either case, continue without external storage.
447 } else {
448 LOG(FATAL) << "Cannot continue without emulated storage";
449 }
450 }
Elliott Hughes178cdcc2012-08-16 16:47:31 -0700451
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700452 SetGids(env, javaGids);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700453
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700454 SetRLimits(env, javaRlimits);
455
Nick Kralevich7e2364c2013-02-20 14:46:54 -0800456 int rc = setresgid(gid, gid, gid);
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700457 if (rc == -1) {
Nick Kralevich7e2364c2013-02-20 14:46:54 -0800458 PLOG(FATAL) << "setresgid(" << gid << ") failed";
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700459 }
460
Nick Kralevich7e2364c2013-02-20 14:46:54 -0800461 rc = setresuid(uid, uid, uid);
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700462 if (rc == -1) {
Nick Kralevich7e2364c2013-02-20 14:46:54 -0800463 PLOG(FATAL) << "setresuid(" << uid << ") failed";
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700464 }
465
Brian Carlstrom154cef62012-05-02 22:34:03 -0700466#if defined(__linux__)
Nick Kralevichb7882782012-09-27 12:29:02 -0700467 if (NeedsNoRandomizeWorkaround()) {
468 // Work around ARM kernel ASLR lossage (http://b/5817320).
469 int old_personality = personality(0xffffffff);
470 int new_personality = personality(old_personality | ADDR_NO_RANDOMIZE);
471 if (new_personality == -1) {
472 PLOG(WARNING) << "personality(" << new_personality << ") failed";
473 }
Elliott Hughes51e53862012-05-02 17:17:28 -0700474 }
Brian Carlstrom154cef62012-05-02 22:34:03 -0700475#endif
Elliott Hughes51e53862012-05-02 17:17:28 -0700476
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800477 SetCapabilities(permittedCapabilities, effectiveCapabilities);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700478
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700479 SetSchedulerPolicy();
Elliott Hughesb6636b82012-04-24 10:41:16 -0700480
Kenny Rootdc1cd102012-10-17 10:35:32 -0700481#if defined(HAVE_ANDROID_OS)
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700482 { // NOLINT(whitespace/braces)
Brian Carlstrom1a0b4752012-10-17 16:23:18 -0700483 const char* se_info_c_str = NULL;
484 UniquePtr<ScopedUtfChars> se_info;
485 if (java_se_info != NULL) {
486 se_info.reset(new ScopedUtfChars(env, java_se_info));
487 se_info_c_str = se_info->c_str();
488 CHECK(se_info_c_str != NULL);
489 }
490 const char* se_name_c_str = NULL;
491 UniquePtr<ScopedUtfChars> se_name;
492 if (java_se_name != NULL) {
493 se_name.reset(new ScopedUtfChars(env, java_se_name));
494 se_name_c_str = se_name->c_str();
495 CHECK(se_name_c_str != NULL);
496 }
497 rc = selinux_android_setcontext(uid, is_system_server, se_info_c_str, se_name_c_str);
Elliott Hughesc151f902012-06-21 20:33:21 -0700498 if (rc == -1) {
499 PLOG(FATAL) << "selinux_android_setcontext(" << uid << ", "
500 << (is_system_server ? "true" : "false") << ", "
Brian Carlstrom1a0b4752012-10-17 16:23:18 -0700501 << "\"" << se_info_c_str << "\", \"" << se_name_c_str << "\") failed";
Elliott Hughesc151f902012-06-21 20:33:21 -0700502 }
William Roberts1e7d1d52013-11-11 07:28:37 -0800503
504 // Make it easier to debug audit logs by setting the main thread's name to the
505 // nice name rather than "app_process".
506 if (se_info_c_str == NULL && is_system_server) {
507 se_name_c_str = "system_server";
508 }
509 if (se_info_c_str != NULL) {
510 SetThreadName(se_name_c_str);
511 }
Elliott Hughesc151f902012-06-21 20:33:21 -0700512 }
513#else
514 UNUSED(is_system_server);
515 UNUSED(java_se_info);
516 UNUSED(java_se_name);
517#endif
518
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700519 // Our system thread ID, etc, has changed so reset Thread state.
520 self->InitAfterFork();
521
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700522 EnableDebugFeatures(debug_flags);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700523
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700524 UnsetSigChldHandler();
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700525 runtime->DidForkFromZygote();
526 } else if (pid > 0) {
527 // the parent process
528 }
529 return pid;
530}
531
Elliott Hughes0512f022012-03-15 22:10:52 -0700532static jint Zygote_nativeForkAndSpecialize(JNIEnv* env, jclass, jint uid, jint gid, jintArray gids,
Brian Carlstrom01add2c2013-12-16 09:50:35 -0800533 jint debug_flags, jobjectArray rlimits,
534 jint mount_external, jstring se_info, jstring se_name) {
535 return ForkAndSpecializeCommon(env, uid, gid, gids, debug_flags, rlimits, 0, 0, mount_external,
536 se_info, se_name, false);
Brian Carlstroma9f19782011-10-13 00:14:47 -0700537}
538
Elliott Hughes0512f022012-03-15 22:10:52 -0700539static jint Zygote_nativeForkSystemServer(JNIEnv* env, jclass, uid_t uid, gid_t gid, jintArray gids,
540 jint debug_flags, jobjectArray rlimits,
Brian Carlstrom01add2c2013-12-16 09:50:35 -0800541 jlong permittedCapabilities,
542 jlong effectiveCapabilities) {
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700543 pid_t pid = ForkAndSpecializeCommon(env, uid, gid, gids,
544 debug_flags, rlimits,
Elliott Hughes178cdcc2012-08-16 16:47:31 -0700545 permittedCapabilities, effectiveCapabilities,
546 MOUNT_EXTERNAL_NONE, NULL, NULL, true);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700547 if (pid > 0) {
548 // The zygote process checks whether the child process has died or not.
549 LOG(INFO) << "System server process " << pid << " has been created";
550 gSystemServerPid = pid;
551 // There is a slight window that the system server process has crashed
552 // but it went unnoticed because we haven't published its pid yet. So
553 // we recheck here just to make sure that all is well.
554 int status;
555 if (waitpid(pid, &status, WNOHANG) == pid) {
556 LOG(FATAL) << "System server process " << pid << " has died. Restarting Zygote!";
557 }
558 }
559 return pid;
560}
561
Elliott Hughes01158d72011-09-19 19:47:10 -0700562static JNINativeMethod gMethods[] = {
Elliott Hughes178cdcc2012-08-16 16:47:31 -0700563 NATIVE_METHOD(Zygote, nativeForkAndSpecialize, "(II[II[[IILjava/lang/String;Ljava/lang/String;)I"),
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700564 NATIVE_METHOD(Zygote, nativeForkSystemServer, "(II[II[[IJJ)I"),
Elliott Hughes01158d72011-09-19 19:47:10 -0700565};
566
Elliott Hughes01158d72011-09-19 19:47:10 -0700567void register_dalvik_system_Zygote(JNIEnv* env) {
Elliott Hughes7756d542012-05-24 21:56:51 -0700568 REGISTER_NATIVE_METHODS("dalvik/system/Zygote");
Elliott Hughes01158d72011-09-19 19:47:10 -0700569}
570
571} // namespace art