Android增加系统白名单,防止重要应用低内存时被误杀

Android 9以及之后版本

这里没有啥可多说的了,其原理就是调整应用的adj值,防止被lmkd杀掉!这里我们直接上我修改好的提交,如下:

diff --git a/frameworks/base/core/res/res/values/config.xml b/frameworks/base/core/res/res/values/config.xml
index aa56aa2bf9..403dcf04b4 100644
--- a/frameworks/base/core/res/res/values/config.xml
+++ b/frameworks/base/core/res/res/values/config.xml
@@ -4383,4 +4383,11 @@
     false
     
     -1
+    
+    "com.xxx.xxxdroid.tester"
+    "com.xxx.xxxdroid.tester.service"
+    "com.xxx.xxxdroid.uiautomatorservice"
+    "com.xxx.ft"
+    "com.xxx.xxx"
+    

 
diff --git a/frameworks/base/core/res/res/values/symbols.xml b/frameworks/base/core/res/res/values/symbols.xml
index 32c29cc7b2..d296b12b95 100644
--- a/frameworks/base/core/res/res/values/symbols.xml
+++ b/frameworks/base/core/res/res/values/symbols.xml
@@ -4074,4 +4074,5 @@
   
 
   
+  
 
diff --git a/frameworks/base/services/core/java/com/android/server/am/OomAdjuster.java b/frameworks/base/services/core/java/com/android/server/am/OomAdjuster.java
index a7ee0cf300..689b22b9ed 100644
--- a/frameworks/base/services/core/java/com/android/server/am/OomAdjuster.java
+++ b/frameworks/base/services/core/java/com/android/server/am/OomAdjuster.java
@@ -109,7 +109,8 @@ import java.io.PrintWriter;
 import java.util.ArrayDeque;
 import java.util.ArrayList;
 import java.util.Arrays;
-
+import java.util.List;
+import android.content.res.Resources;
 /**
  * All of the code required to compute proc states and oom_adj values.
  */
@@ -234,7 +235,7 @@ public final class OomAdjuster {
     private ArrayDeque mTmpQueue;
 
     private final IPlatformCompat mPlatformCompat;
-
+    private List mLmKillerBypassPackages = new ArrayList();
     OomAdjuster(ActivityManagerService service, ProcessList processList, ActiveUids activeUids) {
         this(service, processList, activeUids, createAdjusterThread());
     }
@@ -299,8 +300,22 @@ public final class OomAdjuster {
                 / ProcessList.CACHED_APP_IMPORTANCE_LEVELS;
         IBinder b = ServiceManager.getService(Context.PLATFORM_COMPAT_SERVICE);
         mPlatformCompat = IPlatformCompat.Stub.asInterface(b);
+        String[] lmKillerTrackerWhitelist = Resources.getSystem().getStringArray(
+                com.android.internal.R.array.low_memory_killer_tracker_whitelist);
+        mLmKillerBypassPackages = Arrays.asList(lmKillerTrackerWhitelist);
+
     }
 
+    private boolean isInWhitelist(ProcessRecord pr) {
+        String pkgName = pr.info.packageName;
+    
+        for (String token : mLmKillerBypassPackages) { 
+            if (pkgName.startsWith(token)) {
+                return true; 
+            }
+        }
+        return false;
+    }
     void initSettings() {
         mCachedAppOptimizer.init();
     }
@@ -2269,14 +2284,29 @@ public final class OomAdjuster {
                     }
                 }
             }
-            ProcessList.setOomAdj(app.pid, app.uid, app.curAdj);
-            if (DEBUG_SWITCH || DEBUG_OOM_ADJ || mService.mCurOomAdjUid == app.info.uid) {
-                String msg = "Set " + app.pid + " " + app.processName + " adj "
-                        + app.curAdj + ": " + app.adjType;
-                reportOomAdjMessageLocked(TAG_OOM_ADJ, msg);
-            }
-            app.setAdj = app.curAdj;
-            app.verifiedAdj = ProcessList.INVALID_ADJ;
+            boolean isAppWhiteProcess = false;
+            if( isInWhitelist(app) && (app.curAdj > ProcessList.PERSISTENT_SERVICE_ADJ))
+                isAppWhiteProcess = true;
+            if(isAppWhiteProcess){
+                Slog.d(TAG,"isAppWhiteProcess so not kill!");
+                ProcessList.setOomAdj(app.pid, app.uid, ProcessList.PERSISTENT_SERVICE_ADJ);
+                if (DEBUG_SWITCH || DEBUG_OOM_ADJ || mService.mCurOomAdjUid == app.info.uid) {
+                    String msg = "Set " + app.pid + " " + app.processName + " adj "
+                            + app.curAdj + ": " + app.adjType;
+                    reportOomAdjMessageLocked(TAG_OOM_ADJ, msg);
+                }    
+                app.setAdj = ProcessList.PERSISTENT_SERVICE_ADJ;
+                app.verifiedAdj = ProcessList.INVALID_ADJ;
+            }else{
+                ProcessList.setOomAdj(app.pid, app.uid, app.curAdj);
+                if (DEBUG_SWITCH || DEBUG_OOM_ADJ || mService.mCurOomAdjUid == app.info.uid) {
+                    String msg = "Set " + app.pid + " " + app.processName + " adj "
+                            + app.curAdj + ": " + app.adjType;
+                    reportOomAdjMessageLocked(TAG_OOM_ADJ, msg);
+                }
+                app.setAdj = app.curAdj;
+                app.verifiedAdj = ProcessList.INVALID_ADJ;
+            }
         }
 
         final int curSchedGroup = app.getCurrentSchedulingGroup();

总结

   如上是一种比较通用的白名单的做法,方法可能有许多但是最终的目的就是动态调整adj的值防止系统在低内存的时候误将一些重要的应用kill了。

在Andriod R go 上实践ok

你可能感兴趣的:(android,elasticsearch,数据库)