Move most zygote related code to frameworks/base.
Avoids code duplication across art/dalvik. Also, most of
this code is not directly related to the runtime (mounting external
storage, multi user etc.) and therefore belongs in the frameworks.
Change-Id: Icf4723dd0ec4521ef6b1f785c99d50aebca7779a
diff --git a/runtime/native/dalvik_system_ZygoteHooks.cc b/runtime/native/dalvik_system_ZygoteHooks.cc
new file mode 100644
index 0000000..5455daa
--- /dev/null
+++ b/runtime/native/dalvik_system_ZygoteHooks.cc
@@ -0,0 +1,118 @@
+/*
+ * Copyright (C) 2008 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdlib.h>
+
+#include "debugger.h"
+#include "jni_internal.h"
+#include "JNIHelp.h"
+#include "thread-inl.h"
+
+#if defined(HAVE_PRCTL)
+#include <sys/prctl.h>
+#endif
+
+namespace art {
+
+static void EnableDebugger() {
+ // To let a non-privileged gdbserver attach to this
+ // process, we must set our dumpable flag.
+ if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0) == -1) {
+ PLOG(ERROR) << "prctl(PR_SET_DUMPABLE) failed for pid " << getpid();
+ }
+ // We don't want core dumps, though, so set the core dump size to 0.
+ rlimit rl;
+ rl.rlim_cur = 0;
+ rl.rlim_max = RLIM_INFINITY;
+ if (setrlimit(RLIMIT_CORE, &rl) == -1) {
+ PLOG(ERROR) << "setrlimit(RLIMIT_CORE) failed for pid " << getpid();
+ }
+}
+
+static void EnableDebugFeatures(uint32_t debug_flags) {
+ // Must match values in dalvik.system.Zygote.
+ enum {
+ DEBUG_ENABLE_DEBUGGER = 1,
+ DEBUG_ENABLE_CHECKJNI = 1 << 1,
+ DEBUG_ENABLE_ASSERT = 1 << 2,
+ DEBUG_ENABLE_SAFEMODE = 1 << 3,
+ DEBUG_ENABLE_JNI_LOGGING = 1 << 4,
+ };
+
+ if ((debug_flags & DEBUG_ENABLE_CHECKJNI) != 0) {
+ Runtime* runtime = Runtime::Current();
+ JavaVMExt* vm = runtime->GetJavaVM();
+ if (!vm->check_jni) {
+ LOG(DEBUG) << "Late-enabling -Xcheck:jni";
+ vm->SetCheckJniEnabled(true);
+ // There's only one thread running at this point, so only one JNIEnv to fix up.
+ Thread::Current()->GetJniEnv()->SetCheckJniEnabled(true);
+ } else {
+ LOG(DEBUG) << "Not late-enabling -Xcheck:jni (already on)";
+ }
+ debug_flags &= ~DEBUG_ENABLE_CHECKJNI;
+ }
+
+ if ((debug_flags & DEBUG_ENABLE_JNI_LOGGING) != 0) {
+ gLogVerbosity.third_party_jni = true;
+ debug_flags &= ~DEBUG_ENABLE_JNI_LOGGING;
+ }
+
+ Dbg::SetJdwpAllowed((debug_flags & DEBUG_ENABLE_DEBUGGER) != 0);
+ if ((debug_flags & DEBUG_ENABLE_DEBUGGER) != 0) {
+ EnableDebugger();
+ }
+ debug_flags &= ~DEBUG_ENABLE_DEBUGGER;
+
+ // These two are for backwards compatibility with Dalvik.
+ debug_flags &= ~DEBUG_ENABLE_ASSERT;
+ debug_flags &= ~DEBUG_ENABLE_SAFEMODE;
+
+ if (debug_flags != 0) {
+ LOG(ERROR) << StringPrintf("Unknown bits set in debug_flags: %#x", debug_flags);
+ }
+}
+
+static jlong ZygoteHooks_nativePreFork(JNIEnv* env, jclass) {
+ Runtime* runtime = Runtime::Current();
+ CHECK(runtime->IsZygote()) << "runtime instance not started with -Xzygote";
+ if (!runtime->PreZygoteFork()) {
+ LOG(FATAL) << "pre-fork heap failed";
+ }
+
+ // Grab thread before fork potentially makes Thread::pthread_key_self_ unusable.
+ Thread* self = Thread::Current();
+ return reinterpret_cast<jlong>(self);
+}
+
+static void ZygoteHooks_nativePostForkChild(JNIEnv* env, jclass, jlong token, jint debug_flags) {
+ Thread* thread = reinterpret_cast<Thread*>(token);
+ // Our system thread ID, etc, has changed so reset Thread state.
+ thread->InitAfterFork();
+ EnableDebugFeatures(debug_flags);
+ Runtime::Current()->DidForkFromZygote();
+}
+
+static JNINativeMethod gMethods[] = {
+ NATIVE_METHOD(ZygoteHooks, nativePreFork, "()J"),
+ NATIVE_METHOD(ZygoteHooks, nativePostForkChild, "(JI)V"),
+};
+
+void register_dalvik_system_ZygoteHooks(JNIEnv* env) {
+ REGISTER_NATIVE_METHODS("dalvik/system/ZygoteHooks");
+}
+
+} // namespace art