init: set oom_adj early before fork vendor_init
right now vendor_init is forked before we set oom_adj for init which
leaves a chance vendor_init could be killed in heavy memory pressure.
this CL set the oom_adj before forking everything to ensure all native
have correct oom_adj settings.
Fixes: 130824864
Test: procrank -o
Change-Id: I8af129076c3efa29f7b781459449f8f2dc853c98
diff --git a/init/init.cpp b/init/init.cpp
index bbef1a9..2e28ec6 100644
--- a/init/init.cpp
+++ b/init/init.cpp
@@ -625,6 +625,11 @@
InitKernelLogging(argv, InitAborter);
LOG(INFO) << "init second stage started!";
+ // Set init and its forked children's oom_adj.
+ if (auto result = WriteFile("/proc/1/oom_score_adj", "-1000"); !result) {
+ LOG(ERROR) << "Unable to write -1000 to /proc/1/oom_score_adj: " << result.error();
+ }
+
// Enable seccomp if global boot option was passed (otherwise it is enabled in zygote).
GlobalSeccomp();
diff --git a/rootdir/init.rc b/rootdir/init.rc
index f2e7a7c..06a4f84 100644
--- a/rootdir/init.rc
+++ b/rootdir/init.rc
@@ -13,9 +13,6 @@
# Cgroups are mounted right before early-init using list from /etc/cgroups.json
on early-init
- # Set init and its forked children's oom_adj.
- write /proc/1/oom_score_adj -1000
-
# Disable sysrq from keyboard
write /proc/sys/kernel/sysrq 0