From 424b03f75e98c8af4061236ef1dd2785f5608299 Mon Sep 17 00:00:00 2001 From: Christopher Tate Date: Tue, 4 Apr 2017 14:23:44 -0700 Subject: [PATCH] Allow background default container service in a less crashy way Turns out we run this code during early boot, before the device idle service has even been constructed yet. Find another way to achieve the needed service execution. Bug 36865930 Test: manual Change-Id: I8e3304f37c3a5ee125b73aef2b7d7c7b387aa200 --- core/java/android/app/IActivityManager.aidl | 5 +++++ .../server/am/ActivityManagerService.java | 18 ++++++++++++++++++ .../server/pm/PackageManagerService.java | 15 +++++++++++++++ 3 files changed, 38 insertions(+) diff --git a/core/java/android/app/IActivityManager.aidl b/core/java/android/app/IActivityManager.aidl index f4d26fd7d9d85..079bbcdd4951c 100644 --- a/core/java/android/app/IActivityManager.aidl +++ b/core/java/android/app/IActivityManager.aidl @@ -635,6 +635,11 @@ interface IActivityManager { */ int getLastResumedActivityUserId(); + /** + * Add a bare uid to the background restrictions whitelist. Only the system uid may call this. + */ + void backgroundWhitelistUid(int uid); + // WARNING: when these transactions are updated, check if they are any callers on the native // side. If so, make sure they are using the correct transaction ids and arguments. // If a transaction which will also be used on the native side is being inserted, add it diff --git a/services/core/java/com/android/server/am/ActivityManagerService.java b/services/core/java/com/android/server/am/ActivityManagerService.java index 19fc2b876c3ef..ee2fdba715769 100644 --- a/services/core/java/com/android/server/am/ActivityManagerService.java +++ b/services/core/java/com/android/server/am/ActivityManagerService.java @@ -12143,6 +12143,24 @@ public class ActivityManagerService extends IActivityManager.Stub return false; } + @Override + public void backgroundWhitelistUid(final int uid) { + if (Binder.getCallingUid() != Process.SYSTEM_UID) { + throw new SecurityException("Only the OS may call backgroundWhitelistUid()"); + } + + if (DEBUG_BACKGROUND_CHECK) { + Slog.i(TAG, "Adding uid " + uid + " to bg uid whitelist"); + } + synchronized (this) { + final int N = mBackgroundUidWhitelist.length; + int[] newList = new int[N+1]; + System.arraycopy(mBackgroundUidWhitelist, 0, newList, 0, N); + newList[N] = uid; + mBackgroundUidWhitelist = newList; + } + } + final ProcessRecord addAppLocked(ApplicationInfo info, String customProcess, boolean isolated, String abiOverride) { ProcessRecord app; diff --git a/services/core/java/com/android/server/pm/PackageManagerService.java b/services/core/java/com/android/server/pm/PackageManagerService.java index dec639f928ef7..6ee7de4839c47 100644 --- a/services/core/java/com/android/server/pm/PackageManagerService.java +++ b/services/core/java/com/android/server/pm/PackageManagerService.java @@ -608,6 +608,10 @@ public class PackageManagerService extends IPackageManager.Stub { final boolean mIsPreNUpgrade; final boolean mIsPreNMR1Upgrade; + // Have we told the Activity Manager to whitelist the default container service by uid yet? + @GuardedBy("mPackages") + boolean mDefaultContainerWhitelisted = false; + @GuardedBy("mPackages") private boolean mDexOptDialogShown; @@ -13024,7 +13028,18 @@ public class PackageManagerService extends IPackageManager.Stub { intent.setComponent(DEFAULT_CONTAINER_COMPONENT); IActivityManager am = ActivityManager.getService(); if (am != null) { + int dcsUid = -1; + synchronized (mPackages) { + if (!mDefaultContainerWhitelisted) { + mDefaultContainerWhitelisted = true; + PackageSetting ps = mSettings.mPackages.get(DEFAULT_CONTAINER_PACKAGE); + dcsUid = UserHandle.getUid(UserHandle.USER_SYSTEM, ps.appId); + } + } try { + if (dcsUid > 0) { + am.backgroundWhitelistUid(dcsUid); + } am.startService(null, intent, null, -1, null, false, mContext.getOpPackageName(), UserHandle.USER_SYSTEM); } catch (RemoteException e) {