blob: 3daec750bae549d600e9ef4137460247331b9838 [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 Carlstromcaabb1b2011-10-11 18:09:13 -070017#include <grp.h>
Elliott Hughes01158d72011-09-19 19:47:10 -070018#include <paths.h>
Elliott Hughesada2da92012-01-17 17:58:58 -080019#include <signal.h>
Elliott Hughes01158d72011-09-19 19:47:10 -070020#include <stdlib.h>
Elliott Hughes178cdcc2012-08-16 16:47:31 -070021#include <sys/mount.h>
Brian Carlstromcaabb1b2011-10-11 18:09:13 -070022#include <sys/types.h>
23#include <sys/wait.h>
Elliott Hughes01158d72011-09-19 19:47:10 -070024#include <unistd.h>
25
Elliott Hughesb6636b82012-04-24 10:41:16 -070026#include "cutils/sched_policy.h"
Elliott Hughes4ffd3132011-10-24 12:06:42 -070027#include "debugger.h"
28#include "jni_internal.h"
Elliott Hughes4ffd3132011-10-24 12:06:42 -070029#include "JNIHelp.h"
30#include "ScopedLocalRef.h"
31#include "ScopedPrimitiveArray.h"
32#include "ScopedUtfChars.h"
Brian Carlstromcaabb1b2011-10-11 18:09:13 -070033#include "thread.h"
34
Elliott Hughesb4807ec2012-01-17 18:33:04 -080035#if defined(HAVE_PRCTL)
36#include <sys/prctl.h>
37#endif
38
Elliott Hughesc151f902012-06-21 20:33:21 -070039#include <selinux/android.h>
Elliott Hughesc151f902012-06-21 20:33:21 -070040
Brian Carlstrom154cef62012-05-02 22:34:03 -070041#if defined(__linux__)
42#include <sys/personality.h>
Nick Kralevichb7882782012-09-27 12:29:02 -070043#include <sys/utsname.h>
Brian Carlstrom154cef62012-05-02 22:34:03 -070044#endif
45
Elliott Hughes01158d72011-09-19 19:47:10 -070046namespace art {
47
Brian Carlstromcaabb1b2011-10-11 18:09:13 -070048static pid_t gSystemServerPid = 0;
49
Elliott Hughes178cdcc2012-08-16 16:47:31 -070050// Must match values in dalvik.system.Zygote.
51enum MountExternalKind {
52 MOUNT_EXTERNAL_NONE = 0,
53 MOUNT_EXTERNAL_SINGLEUSER = 1,
54 MOUNT_EXTERNAL_MULTIUSER = 2,
55};
56
Brian Carlstromcaabb1b2011-10-11 18:09:13 -070057// This signal handler is for zygote mode, since the zygote must reap its children
Elliott Hughes1bac54f2012-03-16 12:48:31 -070058static void SigChldHandler(int /*signal_number*/) {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -070059 pid_t pid;
60 int status;
61
62 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
63 // Log process-death status that we care about. In general it is
64 // not safe to call LOG(...) from a signal handler because of
65 // possible reentrancy. However, we know a priori that the
66 // current implementation of LOG() is safe to call from a SIGCHLD
67 // handler in the zygote process. If the LOG() implementation
68 // changes its locking strategy or its use of syscalls within the
69 // lazy-init critical section, its use here may become unsafe.
70 if (WIFEXITED(status)) {
71 if (WEXITSTATUS(status)) {
72 LOG(INFO) << "Process " << pid << " exited cleanly (" << WEXITSTATUS(status) << ")";
73 } else if (false) {
74 LOG(INFO) << "Process " << pid << " exited cleanly (" << WEXITSTATUS(status) << ")";
75 }
76 } else if (WIFSIGNALED(status)) {
77 if (WTERMSIG(status) != SIGKILL) {
78 LOG(INFO) << "Process " << pid << " terminated by signal (" << WTERMSIG(status) << ")";
79 } else if (false) {
80 LOG(INFO) << "Process " << pid << " terminated by signal (" << WTERMSIG(status) << ")";
81 }
82#ifdef WCOREDUMP
83 if (WCOREDUMP(status)) {
84 LOG(INFO) << "Process " << pid << " dumped core";
85 }
86#endif /* ifdef WCOREDUMP */
87 }
88
89 // If the just-crashed process is the system_server, bring down zygote
90 // so that it is restarted by init and system server will be restarted
91 // from there.
92 if (pid == gSystemServerPid) {
Brian Carlstrom6a4be3a2011-10-20 16:34:03 -070093 LOG(ERROR) << "Exit zygote because system server (" << pid << ") has terminated";
94 kill(getpid(), SIGKILL);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -070095 }
96 }
97
98 if (pid < 0) {
99 PLOG(WARNING) << "Zygote SIGCHLD error in waitpid";
100 }
101}
102
Elliott Hughes0512f022012-03-15 22:10:52 -0700103// Configures the SIGCHLD handler for the zygote process. This is configured
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700104// very late, because earlier in the runtime we may fork() and exec()
105// other processes, and we want to waitpid() for those rather than
106// have them be harvested immediately.
107//
108// This ends up being called repeatedly before each fork(), but there's
109// no real harm in that.
Elliott Hughes0512f022012-03-15 22:10:52 -0700110static void SetSigChldHandler() {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700111 struct sigaction sa;
112 memset(&sa, 0, sizeof(sa));
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700113 sa.sa_handler = SigChldHandler;
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700114
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700115 int err = sigaction(SIGCHLD, &sa, NULL);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700116 if (err < 0) {
117 PLOG(WARNING) << "Error setting SIGCHLD handler";
118 }
119}
120
Elliott Hughes0512f022012-03-15 22:10:52 -0700121// Sets the SIGCHLD handler back to default behavior in zygote children.
122static void UnsetSigChldHandler() {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700123 struct sigaction sa;
124 memset(&sa, 0, sizeof(sa));
125 sa.sa_handler = SIG_DFL;
126
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700127 int err = sigaction(SIGCHLD, &sa, NULL);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700128 if (err < 0) {
129 PLOG(WARNING) << "Error unsetting SIGCHLD handler";
130 }
131}
132
133// Calls POSIX setgroups() using the int[] object as an argument.
134// A NULL argument is tolerated.
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700135static void SetGids(JNIEnv* env, jintArray javaGids) {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700136 if (javaGids == NULL) {
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700137 return;
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700138 }
139
140 COMPILE_ASSERT(sizeof(gid_t) == sizeof(jint), sizeof_gid_and_jint_are_differerent);
141 ScopedIntArrayRO gids(env, javaGids);
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700142 CHECK(gids.get() != NULL);
143 int rc = setgroups(gids.size(), reinterpret_cast<const gid_t*>(&gids[0]));
144 if (rc == -1) {
145 PLOG(FATAL) << "setgroups failed";
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700146 }
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700147}
148
149// Sets the resource limits via setrlimit(2) for the values in the
150// two-dimensional array of integers that's passed in. The second dimension
151// contains a tuple of length 3: (resource, rlim_cur, rlim_max). NULL is
152// treated as an empty array.
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700153static void SetRLimits(JNIEnv* env, jobjectArray javaRlimits) {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700154 if (javaRlimits == NULL) {
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700155 return;
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700156 }
157
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700158 rlimit rlim;
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700159 memset(&rlim, 0, sizeof(rlim));
160
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700161 for (int i = 0; i < env->GetArrayLength(javaRlimits); ++i) {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700162 ScopedLocalRef<jobject> javaRlimitObject(env, env->GetObjectArrayElement(javaRlimits, i));
163 ScopedIntArrayRO javaRlimit(env, reinterpret_cast<jintArray>(javaRlimitObject.get()));
164 if (javaRlimit.size() != 3) {
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700165 LOG(FATAL) << "rlimits array must have a second dimension of size 3";
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700166 }
167
168 rlim.rlim_cur = javaRlimit[1];
169 rlim.rlim_max = javaRlimit[2];
170
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700171 int rc = setrlimit(javaRlimit[0], &rlim);
172 if (rc == -1) {
173 PLOG(FATAL) << "setrlimit(" << javaRlimit[0] << ", "
174 << "{" << rlim.rlim_cur << ", " << rlim.rlim_max << "}) failed";
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700175 }
176 }
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700177}
178
Elliott Hughes76e36942012-03-16 13:44:56 -0700179#if defined(HAVE_ANDROID_OS)
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700180
181// The debug malloc library needs to know whether it's the zygote or a child.
182extern "C" int gMallocLeakZygoteChild;
183
184static void EnableDebugger() {
185 // To let a non-privileged gdbserver attach to this
186 // process, we must set our dumpable flag.
187 if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0) == -1) {
188 PLOG(ERROR) << "prctl(PR_SET_DUMPABLE) failed for pid " << getpid();
189 }
190 // We don't want core dumps, though, so set the core dump size to 0.
191 rlimit rl;
192 rl.rlim_cur = 0;
193 rl.rlim_max = RLIM_INFINITY;
194 if (setrlimit(RLIMIT_CORE, &rl) == -1) {
195 PLOG(ERROR) << "setrlimit(RLIMIT_CORE) failed for pid " << getpid();
196 }
197}
198
199static void EnableKeepCapabilities() {
200 int rc = prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0);
201 if (rc == -1) {
202 PLOG(FATAL) << "prctl(PR_SET_KEEPCAPS) failed";
203 }
204}
205
Elliott Hughes76e36942012-03-16 13:44:56 -0700206static void SetCapabilities(int64_t permitted, int64_t effective) {
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700207 __user_cap_header_struct capheader;
208 __user_cap_data_struct capdata;
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700209
210 memset(&capheader, 0, sizeof(capheader));
211 memset(&capdata, 0, sizeof(capdata));
212
213 capheader.version = _LINUX_CAPABILITY_VERSION;
214 capheader.pid = 0;
215
216 capdata.effective = effective;
217 capdata.permitted = permitted;
218
219 if (capset(&capheader, &capdata) != 0) {
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800220 PLOG(FATAL) << "capset(" << permitted << ", " << effective << ") failed";
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700221 }
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700222}
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700223
224static void SetSchedulerPolicy() {
225 errno = -set_sched_policy(0, SP_DEFAULT);
226 if (errno != 0) {
227 PLOG(FATAL) << "set_sched_policy(0, SP_DEFAULT) failed";
228 }
229}
230
Elliott Hughes76e36942012-03-16 13:44:56 -0700231#else
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700232
233static int gMallocLeakZygoteChild = 0;
234
235static void EnableDebugger() {}
236static void EnableKeepCapabilities() {}
Elliott Hughes76e36942012-03-16 13:44:56 -0700237static void SetCapabilities(int64_t, int64_t) {}
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700238static void SetSchedulerPolicy() {}
239
Elliott Hughes76e36942012-03-16 13:44:56 -0700240#endif
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700241
Elliott Hughes0512f022012-03-15 22:10:52 -0700242static void EnableDebugFeatures(uint32_t debug_flags) {
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700243 // Must match values in dalvik.system.Zygote.
244 enum {
245 DEBUG_ENABLE_DEBUGGER = 1,
246 DEBUG_ENABLE_CHECKJNI = 1 << 1,
247 DEBUG_ENABLE_ASSERT = 1 << 2,
248 DEBUG_ENABLE_SAFEMODE = 1 << 3,
249 DEBUG_ENABLE_JNI_LOGGING = 1 << 4,
250 };
251
252 if ((debug_flags & DEBUG_ENABLE_CHECKJNI) != 0) {
253 Runtime* runtime = Runtime::Current();
254 JavaVMExt* vm = runtime->GetJavaVM();
255 if (!vm->check_jni) {
256 LOG(DEBUG) << "Late-enabling -Xcheck:jni";
Elliott Hughes88c5c352012-03-15 18:49:48 -0700257 vm->SetCheckJniEnabled(true);
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700258 // There's only one thread running at this point, so only one JNIEnv to fix up.
Elliott Hughes88c5c352012-03-15 18:49:48 -0700259 Thread::Current()->GetJniEnv()->SetCheckJniEnabled(true);
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700260 } else {
261 LOG(DEBUG) << "Not late-enabling -Xcheck:jni (already on)";
262 }
263 debug_flags &= ~DEBUG_ENABLE_CHECKJNI;
264 }
265
266 if ((debug_flags & DEBUG_ENABLE_JNI_LOGGING) != 0) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800267 gLogVerbosity.third_party_jni = true;
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700268 debug_flags &= ~DEBUG_ENABLE_JNI_LOGGING;
269 }
270
271 Dbg::SetJdwpAllowed((debug_flags & DEBUG_ENABLE_DEBUGGER) != 0);
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700272 if ((debug_flags & DEBUG_ENABLE_DEBUGGER) != 0) {
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700273 EnableDebugger();
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700274 }
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700275 debug_flags &= ~DEBUG_ENABLE_DEBUGGER;
276
277 // These two are for backwards compatibility with Dalvik.
278 debug_flags &= ~DEBUG_ENABLE_ASSERT;
279 debug_flags &= ~DEBUG_ENABLE_SAFEMODE;
280
281 if (debug_flags != 0) {
282 LOG(ERROR) << StringPrintf("Unknown bits set in debug_flags: %#x", debug_flags);
283 }
284}
285
Elliott Hughes178cdcc2012-08-16 16:47:31 -0700286// Create private mount space for this process and mount SD card
287// into it, based on the active user.
288static void MountExternalStorage(uid_t uid, jint mount_external) {
289 if (mount_external == MOUNT_EXTERNAL_NONE) {
290 return;
291 }
292
293#if 0
294 userid_t user_id = multiuser_getUserId(uid);
295
296 // Create private mount namespace for our process.
297 if (unshare(CLONE_NEWNS) == -1) {
298 PLOG(FATAL) << "unshare(CLONE_NEWNS) failed";
299 }
300
301 // Mark rootfs as being a slave in our process so that changes
302 // from parent namespace flow into our process.
303 if (mount("rootfs", "/", NULL, (MS_SLAVE | MS_REC), NULL) == -1) {
304 PLOG(FATAL) << "mount(\"rootfs\", \"/\", NULL, (MS_SLAVE | MS_REC), NULL) failed";
305 }
306
307 // Create bind mount from specific path.
308 if (mount_external == MOUNT_EXTERNAL_SINGLEUSER) {
309 if (mount(EXTERNAL_STORAGE_SYSTEM, EXTERNAL_STORAGE_APP, "none", MS_BIND, NULL) == -1) {
310 PLOG(FATAL) << "mount(\"" << EXTERNAL_STORAGE_SYSTEM << "\", \"" << EXTERNAL_STORAGE_APP << "\", \"none\", MS_BIND, NULL) failed";
311 }
312 } else if (mount_external == MOUNT_EXTERNAL_MULTIUSER) {
313 // Assume path has already been created by installd.
314 std::string source_path(StringPrintf("%s/%d", EXTERNAL_STORAGE_SYSTEM, user_id));
315 if (mount(source_path.c_str(), EXTERNAL_STORAGE_APP, "none", MS_BIND, NULL) == -1) {
316 PLOG(FATAL) << "mount(\"" << source_path.c_str() << "\", \"" << EXTERNAL_STORAGE_APP << "\", \"none\", MS_BIND, NULL) failed";
317 }
318 } else {
319 LOG(FATAL) << "Mount mode unsupported: " << mount_external;
320 }
321#else
322 UNUSED(uid);
323 UNIMPLEMENTED(FATAL);
324#endif
325}
326
Nick Kralevichb7882782012-09-27 12:29:02 -0700327#if defined(__linux__)
328static bool NeedsNoRandomizeWorkaround() {
Nick Kralevich50e17442012-10-19 13:02:37 -0700329#if !defined(__arm__)
330 return false;
331#else
Nick Kralevichb7882782012-09-27 12:29:02 -0700332 int major;
333 int minor;
334 struct utsname uts;
335 if (uname(&uts) == -1) {
336 return false;
337 }
338
339 if (sscanf(uts.release, "%d.%d", &major, &minor) != 2) {
340 return false;
341 }
342
343 // Kernels before 3.4.* need the workaround.
344 return (major < 3) || ((major == 3) && (minor < 4));
Nick Kralevich50e17442012-10-19 13:02:37 -0700345#endif
Nick Kralevichb7882782012-09-27 12:29:02 -0700346}
347#endif
348
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700349// Utility routine to fork zygote and specialize the child process.
Elliott Hughes0512f022012-03-15 22:10:52 -0700350static pid_t ForkAndSpecializeCommon(JNIEnv* env, uid_t uid, gid_t gid, jintArray javaGids,
351 jint debug_flags, jobjectArray javaRlimits,
Elliott Hughesc151f902012-06-21 20:33:21 -0700352 jlong permittedCapabilities, jlong effectiveCapabilities,
Elliott Hughes178cdcc2012-08-16 16:47:31 -0700353 jint mount_external,
Elliott Hughesc151f902012-06-21 20:33:21 -0700354 jstring java_se_info, jstring java_se_name, bool is_system_server) {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700355 Runtime* runtime = Runtime::Current();
356 CHECK(runtime->IsZygote()) << "runtime instance not started with -Xzygote";
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700357 if (!runtime->PreZygoteFork()) {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700358 LOG(FATAL) << "pre-fork heap failed";
359 }
360
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700361 SetSigChldHandler();
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700362
363 // Grab thread before fork potentially makes Thread::pthread_key_self_ unusable.
364 Thread* self = Thread::Current();
365
366 // dvmDumpLoaderStats("zygote"); // TODO: ?
367 pid_t pid = fork();
368
369 if (pid == 0) {
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700370 // The child process.
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700371 gMallocLeakZygoteChild = 1;
372
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700373 // Keep capabilities across UID change, unless we're staying root.
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700374 if (uid != 0) {
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700375 EnableKeepCapabilities();
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700376 }
377
Elliott Hughes178cdcc2012-08-16 16:47:31 -0700378 MountExternalStorage(uid, mount_external);
379
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700380 SetGids(env, javaGids);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700381
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700382 SetRLimits(env, javaRlimits);
383
384 int rc = setgid(gid);
385 if (rc == -1) {
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800386 PLOG(FATAL) << "setgid(" << gid << ") failed";
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700387 }
388
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700389 rc = setuid(uid);
390 if (rc == -1) {
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800391 PLOG(FATAL) << "setuid(" << uid << ") failed";
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700392 }
393
Brian Carlstrom154cef62012-05-02 22:34:03 -0700394#if defined(__linux__)
Nick Kralevichb7882782012-09-27 12:29:02 -0700395 if (NeedsNoRandomizeWorkaround()) {
396 // Work around ARM kernel ASLR lossage (http://b/5817320).
397 int old_personality = personality(0xffffffff);
398 int new_personality = personality(old_personality | ADDR_NO_RANDOMIZE);
399 if (new_personality == -1) {
400 PLOG(WARNING) << "personality(" << new_personality << ") failed";
401 }
Elliott Hughes51e53862012-05-02 17:17:28 -0700402 }
Brian Carlstrom154cef62012-05-02 22:34:03 -0700403#endif
Elliott Hughes51e53862012-05-02 17:17:28 -0700404
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800405 SetCapabilities(permittedCapabilities, effectiveCapabilities);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700406
Elliott Hughes2abeb9b2012-05-11 23:42:02 -0700407 SetSchedulerPolicy();
Elliott Hughesb6636b82012-04-24 10:41:16 -0700408
Kenny Rootdc1cd102012-10-17 10:35:32 -0700409#if defined(HAVE_ANDROID_OS)
Elliott Hughesc151f902012-06-21 20:33:21 -0700410 {
Brian Carlstrom1a0b4752012-10-17 16:23:18 -0700411 const char* se_info_c_str = NULL;
412 UniquePtr<ScopedUtfChars> se_info;
413 if (java_se_info != NULL) {
414 se_info.reset(new ScopedUtfChars(env, java_se_info));
415 se_info_c_str = se_info->c_str();
416 CHECK(se_info_c_str != NULL);
417 }
418 const char* se_name_c_str = NULL;
419 UniquePtr<ScopedUtfChars> se_name;
420 if (java_se_name != NULL) {
421 se_name.reset(new ScopedUtfChars(env, java_se_name));
422 se_name_c_str = se_name->c_str();
423 CHECK(se_name_c_str != NULL);
424 }
425 rc = selinux_android_setcontext(uid, is_system_server, se_info_c_str, se_name_c_str);
Elliott Hughesc151f902012-06-21 20:33:21 -0700426 if (rc == -1) {
427 PLOG(FATAL) << "selinux_android_setcontext(" << uid << ", "
428 << (is_system_server ? "true" : "false") << ", "
Brian Carlstrom1a0b4752012-10-17 16:23:18 -0700429 << "\"" << se_info_c_str << "\", \"" << se_name_c_str << "\") failed";
Elliott Hughesc151f902012-06-21 20:33:21 -0700430 }
431 }
432#else
433 UNUSED(is_system_server);
434 UNUSED(java_se_info);
435 UNUSED(java_se_name);
436#endif
437
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700438 // Our system thread ID, etc, has changed so reset Thread state.
439 self->InitAfterFork();
440
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700441 EnableDebugFeatures(debug_flags);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700442
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700443 UnsetSigChldHandler();
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700444 runtime->DidForkFromZygote();
445 } else if (pid > 0) {
446 // the parent process
447 }
448 return pid;
449}
450
Elliott Hughes0512f022012-03-15 22:10:52 -0700451static jint Zygote_nativeForkAndSpecialize(JNIEnv* env, jclass, jint uid, jint gid, jintArray gids,
Elliott Hughes178cdcc2012-08-16 16:47:31 -0700452 jint debug_flags, jobjectArray rlimits, jint mount_external,
Elliott Hughesc151f902012-06-21 20:33:21 -0700453 jstring se_info, jstring se_name) {
Elliott Hughes178cdcc2012-08-16 16:47:31 -0700454 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 -0700455}
456
Elliott Hughes0512f022012-03-15 22:10:52 -0700457static jint Zygote_nativeForkSystemServer(JNIEnv* env, jclass, uid_t uid, gid_t gid, jintArray gids,
458 jint debug_flags, jobjectArray rlimits,
459 jlong permittedCapabilities, jlong effectiveCapabilities) {
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700460 pid_t pid = ForkAndSpecializeCommon(env, uid, gid, gids,
461 debug_flags, rlimits,
Elliott Hughes178cdcc2012-08-16 16:47:31 -0700462 permittedCapabilities, effectiveCapabilities,
463 MOUNT_EXTERNAL_NONE, NULL, NULL, true);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700464 if (pid > 0) {
465 // The zygote process checks whether the child process has died or not.
466 LOG(INFO) << "System server process " << pid << " has been created";
467 gSystemServerPid = pid;
468 // There is a slight window that the system server process has crashed
469 // but it went unnoticed because we haven't published its pid yet. So
470 // we recheck here just to make sure that all is well.
471 int status;
472 if (waitpid(pid, &status, WNOHANG) == pid) {
473 LOG(FATAL) << "System server process " << pid << " has died. Restarting Zygote!";
474 }
475 }
476 return pid;
477}
478
Elliott Hughes01158d72011-09-19 19:47:10 -0700479static JNINativeMethod gMethods[] = {
Elliott Hughes178cdcc2012-08-16 16:47:31 -0700480 NATIVE_METHOD(Zygote, nativeForkAndSpecialize, "(II[II[[IILjava/lang/String;Ljava/lang/String;)I"),
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700481 NATIVE_METHOD(Zygote, nativeForkSystemServer, "(II[II[[IJJ)I"),
Elliott Hughes01158d72011-09-19 19:47:10 -0700482};
483
Elliott Hughes01158d72011-09-19 19:47:10 -0700484void register_dalvik_system_Zygote(JNIEnv* env) {
Elliott Hughes7756d542012-05-24 21:56:51 -0700485 REGISTER_NATIVE_METHODS("dalvik/system/Zygote");
Elliott Hughes01158d72011-09-19 19:47:10 -0700486}
487
488} // namespace art