Create the WebViewZygote and implement WebViewZygoteInit.

This adds a new init-spawned daemon, webview_zygote, that starts a JVM and
acts as a zygote process for WebView isolated_app services.

Test: m
Test: angler boots
Test: Turn on Settings>Developer>Multiprocess Webview. webview_zygote32 or
      webview_zygote64 start (requires dependent CLs).

Bug: 21643067
Change-Id: Ida98bd04b4d77736b672b03af651c4eb97ce88c1
diff --git a/cmds/webview_zygote/webview_zygote.cpp b/cmds/webview_zygote/webview_zygote.cpp
new file mode 100644
index 0000000..88fee64
--- /dev/null
+++ b/cmds/webview_zygote/webview_zygote.cpp
@@ -0,0 +1,76 @@
+/*
+ * Copyright (C) 2016 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.
+ */
+
+
+#define LOG_TAG "WebViewZygote"
+
+#include <sys/prctl.h>
+
+#include <android_runtime/AndroidRuntime.h>
+#include <binder/IPCThreadState.h>
+#include <binder/ProcessState.h>
+#include <utils/Log.h>
+#include <utils/String8.h>
+#include <utils/Vector.h>
+
+namespace android {
+
+class WebViewRuntime : public AndroidRuntime {
+public:
+    WebViewRuntime(char* argBlockStart, size_t argBlockSize)
+        : AndroidRuntime(argBlockStart, argBlockSize) {}
+
+    ~WebViewRuntime() override {}
+
+    void onStarted() override {
+        // Nothing to do since this is a zygote server.
+    }
+
+    void onVmCreated(JNIEnv*) override {
+        // Nothing to do when the VM is created in the zygote.
+    }
+
+    void onZygoteInit() override {
+        // Called after a new process is forked.
+        sp<ProcessState> proc = ProcessState::self();
+        proc->startThreadPool();
+    }
+
+    void onExit(int code) override {
+        IPCThreadState::self()->stopProcess();
+        AndroidRuntime::onExit(code);
+    }
+};
+
+}  // namespace android
+
+int main(int argc, char* const argv[]) {
+    if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) < 0) {
+        LOG_ALWAYS_FATAL("PR_SET_NO_NEW_PRIVS failed: %s", strerror(errno));
+        return 12;
+    }
+
+    size_t argBlockSize = 0;
+    for (int i = 0; i < argc; ++i) {
+        argBlockSize += strlen(argv[i]) + 1;
+    }
+
+    android::WebViewRuntime runtime(argv[0], argBlockSize);
+    runtime.addOption("-Xzygote");
+
+    android::Vector<android::String8> args;
+    runtime.start("com.android.internal.os.WebViewZygoteInit", args, /*zygote=*/ true);
+}