Cleanup omissions when adding app ID migration to PMS

Follow up after multiple changes for leaving sharedUserId

- Cleanup some messy APIs in DeletePackageHelper
- Remove the SharedUserSetting if no apps remains in the group
- Set PackageRemovedInfo.mRemovedAppId when UID is removed
- Always delete app data when app ID changes during a downgrade
- Fix locking in restoreDisabledSystemPackageLIF(...)

Test: Build + TH
Bug: 179284822
Change-Id: I40853a0894788ede6e0755984d463d2212ec9f09
This commit is contained in:
John Wu
2021-12-14 15:24:55 -08:00
parent 484a8790c9
commit 37f4c472f1
3 changed files with 51 additions and 42 deletions

View File

@@ -431,10 +431,9 @@ final class DeletePackageHelper {
if (DEBUG_REMOVE) Slog.d(TAG, "Removing system package: " + ps.getPackageName());
// When an updated system application is deleted we delete the existing resources
// as well and fall back to existing code in system partition
PackageSetting disabledPs = deleteInstalledSystemPackage(action, ps, allUserHandles,
flags, outInfo, writeSettings);
deleteInstalledSystemPackage(action, allUserHandles, writeSettings);
new InstallPackageHelper(mPm).restoreDisabledSystemPackageLIF(
action, ps, allUserHandles, outInfo, writeSettings, disabledPs);
action, allUserHandles, writeSettings);
} else {
if (DEBUG_REMOVE) Slog.d(TAG, "Removing non-system package: " + ps.getPackageName());
deleteInstalledPackageLIF(ps, deleteCodeAndResources, flags, allUserHandles,
@@ -561,10 +560,11 @@ final class DeletePackageHelper {
mPm.mSettings.writeKernelMappingLPr(ps);
}
private PackageSetting deleteInstalledSystemPackage(DeletePackageAction action,
PackageSetting deletedPs,
@NonNull int[] allUserHandles, int flags, @Nullable PackageRemovedInfo outInfo,
boolean writeSettings) {
private void deleteInstalledSystemPackage(DeletePackageAction action,
@NonNull int[] allUserHandles, boolean writeSettings) {
int flags = action.mFlags;
final PackageSetting deletedPs = action.mDeletingPs;
final PackageRemovedInfo outInfo = action.mRemovedInfo;
final boolean applyUserRestrictions = outInfo != null && (outInfo.mOrigUsers != null);
final AndroidPackage deletedPkg = deletedPs.getPkg();
// Confirm if the system package has been updated
@@ -593,18 +593,16 @@ final class DeletePackageHelper {
outInfo.mIsRemovedPackageSystemUpdate = true;
}
if (disabledPs.getVersionCode() < deletedPs.getVersionCode()) {
// Delete data for downgrades
if (disabledPs.getVersionCode() < deletedPs.getVersionCode()
|| disabledPs.getAppId() != deletedPs.getAppId()) {
// Delete data for downgrades, or when the system app changed appId
flags &= ~PackageManager.DELETE_KEEP_DATA;
} else {
// Preserve data by setting flag
flags |= PackageManager.DELETE_KEEP_DATA;
}
deleteInstalledPackageLIF(deletedPs, true, flags, allUserHandles,
outInfo, writeSettings);
return disabledPs;
deleteInstalledPackageLIF(deletedPs, true, flags, allUserHandles, outInfo, writeSettings);
}
public void deletePackageVersionedInternal(VersionedPackage versionedPackage,

View File

@@ -494,7 +494,13 @@ final class InstallPackageHelper {
if (request.mPkgSetting != null && request.mPkgSetting.getSharedUser() != null
&& request.mPkgSetting.getSharedUser() != result.mPkgSetting.getSharedUser()) {
// shared user changed, remove from old shared user
request.mPkgSetting.getSharedUser().removePackage(request.mPkgSetting);
final SharedUserSetting sus = request.mPkgSetting.getSharedUser();
sus.removePackage(request.mPkgSetting);
// Prune unused SharedUserSetting
if (mPm.mSettings.checkAndPruneSharedUserLPw(sus, false)) {
// Set the app ID in removed info for UID_REMOVED broadcasts
reconciledPkg.mInstallResult.mRemovedInfo.mRemovedAppId = sus.userId;
}
}
if (result.mExistingSettingCopied) {
pkgSetting = request.mPkgSetting;
@@ -3455,14 +3461,12 @@ final class InstallPackageHelper {
/**
* Tries to restore the disabled system package after an update has been deleted.
*/
@GuardedBy({"mPm.mLock", "mPm.mInstallLock"})
public void restoreDisabledSystemPackageLIF(DeletePackageAction action,
PackageSetting deletedPs, @NonNull int[] allUserHandles,
@Nullable PackageRemovedInfo outInfo,
boolean writeSettings,
PackageSetting disabledPs)
throws SystemDeleteException {
// writer
@NonNull int[] allUserHandles, boolean writeSettings) throws SystemDeleteException {
final PackageSetting deletedPs = action.mDeletingPs;
final PackageRemovedInfo outInfo = action.mRemovedInfo;
final PackageSetting disabledPs = action.mDisabledPs;
synchronized (mPm.mLock) {
// NOTE: The system package always needs to be enabled; even if it's for
// a compressed stub. If we don't, installing the system package fails
@@ -3472,24 +3476,26 @@ final class InstallPackageHelper {
mPm.mSettings.enableSystemPackageLPw(disabledPs.getPkg().getPackageName());
// Remove any native libraries from the upgraded package.
removeNativeBinariesLI(deletedPs);
}
// Install the system package
if (DEBUG_REMOVE) Slog.d(TAG, "Re-installing system package: " + disabledPs);
try {
installPackageFromSystemLIF(disabledPs.getPathString(), allUserHandles,
outInfo == null ? null : outInfo.mOrigUsers, writeSettings);
} catch (PackageManagerException e) {
Slog.w(TAG, "Failed to restore system package:" + deletedPs.getPackageName() + ": "
+ e.getMessage());
// TODO(b/194319951): can we avoid this; throw would come from scan...
throw new SystemDeleteException(e);
} finally {
if (disabledPs.getPkg().isStub()) {
// We've re-installed the stub; make sure it's disabled here. If package was
// originally enabled, we'll install the compressed version of the application
// and re-enable it afterward.
disableStubPackage(action, deletedPs, allUserHandles);
// Install the system package
if (DEBUG_REMOVE) Slog.d(TAG, "Re-installing system package: " + disabledPs);
try {
synchronized (mPm.mInstallLock) {
installPackageFromSystemLIF(disabledPs.getPathString(), allUserHandles,
outInfo == null ? null : outInfo.mOrigUsers, writeSettings);
}
} catch (PackageManagerException e) {
Slog.w(TAG, "Failed to restore system package:" + deletedPs.getPackageName() + ": "
+ e.getMessage());
// TODO(b/194319951): can we avoid this; throw would come from scan...
throw new SystemDeleteException(e);
} finally {
if (disabledPs.getPkg().isStub()) {
// We've re-installed the stub; make sure it's disabled here. If package was
// originally enabled, we'll install the compressed version of the application
// and re-enable it afterward.
disableStubPackage(action, deletedPs, allUserHandles);
}
}
}
}

View File

@@ -1254,11 +1254,14 @@ public final class Settings implements Watchable, Snappable {
}
}
private void checkAndPruneSharedUserLPw(SharedUserSetting s, boolean skipCheck) {
boolean checkAndPruneSharedUserLPw(SharedUserSetting s, boolean skipCheck) {
if (skipCheck || (s.packages.isEmpty() && s.mDisabledPackages.isEmpty())) {
mSharedUsers.remove(s.name);
removeAppIdLPw(s.userId);
if (mSharedUsers.remove(s.name) != null) {
removeAppIdLPw(s.userId);
return true;
}
}
return false;
}
int removePackageLPw(String name) {
@@ -1267,7 +1270,9 @@ public final class Settings implements Watchable, Snappable {
removeInstallerPackageStatus(name);
if (p.getSharedUser() != null) {
p.getSharedUser().removePackage(p);
checkAndPruneSharedUserLPw(p.getSharedUser(), false);
if (checkAndPruneSharedUserLPw(p.getSharedUser(), false)) {
return p.getSharedUser().userId;
}
} else {
removeAppIdLPw(p.getAppId());
return p.getAppId();