1 /*
2 * Copyright (C) 2016 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
17
18 #define LOG_TAG "WebViewZygote"
19
20 #include <sys/prctl.h>
21
22 #include <android_runtime/AndroidRuntime.h>
23 #include <binder/IPCThreadState.h>
24 #include <binder/ProcessState.h>
25 #include <utils/Log.h>
26 #include <utils/String8.h>
27 #include <utils/Vector.h>
28
29 namespace android {
30
31 class WebViewRuntime : public AndroidRuntime {
32 public:
WebViewRuntime(char * argBlockStart,size_t argBlockSize)33 WebViewRuntime(char* argBlockStart, size_t argBlockSize)
34 : AndroidRuntime(argBlockStart, argBlockSize) {}
35
~WebViewRuntime()36 ~WebViewRuntime() override {}
37
onStarted()38 void onStarted() override {
39 // Nothing to do since this is a zygote server.
40 }
41
onVmCreated(JNIEnv *)42 void onVmCreated(JNIEnv*) override {
43 // Nothing to do when the VM is created in the zygote.
44 }
45
onZygoteInit()46 void onZygoteInit() override {
47 // Called after a new process is forked.
48 sp<ProcessState> proc = ProcessState::self();
49 proc->startThreadPool();
50 }
51
onExit(int code)52 void onExit(int code) override {
53 IPCThreadState::self()->stopProcess();
54 AndroidRuntime::onExit(code);
55 }
56 };
57
58 } // namespace android
59
main(int argc,char * const argv[])60 int main(int argc, char* const argv[]) {
61 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) < 0) {
62 LOG_ALWAYS_FATAL("PR_SET_NO_NEW_PRIVS failed: %s", strerror(errno));
63 return 12;
64 }
65
66 size_t argBlockSize = 0;
67 for (int i = 0; i < argc; ++i) {
68 argBlockSize += strlen(argv[i]) + 1;
69 }
70
71 android::WebViewRuntime runtime(argv[0], argBlockSize);
72 runtime.addOption("-Xzygote");
73
74 android::Vector<android::String8> args;
75 runtime.start("com.android.internal.os.WebViewZygoteInit", args, /*zygote=*/ true);
76 }
77