Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | package com.android.server.pm; |
| 18 | |
| 19 | import android.annotation.NonNull; |
Nikita Ioffe | da998cf | 2019-03-04 22:54:30 +0000 | [diff] [blame] | 20 | import android.annotation.Nullable; |
Narayan Kamath | 9dfa674 | 2019-01-04 14:22:50 +0000 | [diff] [blame] | 21 | import android.apex.ApexInfo; |
Dario Freni | 276cd07 | 2019-01-09 11:13:44 +0000 | [diff] [blame] | 22 | import android.apex.ApexInfoList; |
Dario Freni | 61b3e25 | 2019-01-11 23:05:39 +0000 | [diff] [blame] | 23 | import android.apex.ApexSessionInfo; |
Oli Lan | c72b0bb | 2019-12-02 14:03:55 +0000 | [diff] [blame] | 24 | import android.apex.ApexSessionParams; |
Rhed Jao | b793c21 | 2019-10-30 20:54:37 +0800 | [diff] [blame] | 25 | import android.content.BroadcastReceiver; |
Narayan Kamath | fcd4a04 | 2019-02-01 14:16:37 +0000 | [diff] [blame] | 26 | import android.content.Context; |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 27 | import android.content.IIntentReceiver; |
| 28 | import android.content.IIntentSender; |
| 29 | import android.content.Intent; |
Rhed Jao | b793c21 | 2019-10-30 20:54:37 +0800 | [diff] [blame] | 30 | import android.content.IntentFilter; |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 31 | import android.content.IntentSender; |
Winson | f00c755 | 2020-01-28 12:52:01 -0800 | [diff] [blame] | 32 | import android.content.pm.ApplicationInfo; |
Dario Freni | 2e8dffc | 2019-02-06 14:55:16 +0000 | [diff] [blame] | 33 | import android.content.pm.PackageInfo; |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 34 | import android.content.pm.PackageInstaller; |
Narayan Kamath | 9dfa674 | 2019-01-04 14:22:50 +0000 | [diff] [blame] | 35 | import android.content.pm.PackageInstaller.SessionInfo; |
| 36 | import android.content.pm.PackageManager; |
Mohammad Samiul Islam | 3fcecfc | 2019-12-20 17:46:01 +0000 | [diff] [blame] | 37 | import android.content.pm.PackageManagerInternal; |
Narayan Kamath | 9dfa674 | 2019-01-04 14:22:50 +0000 | [diff] [blame] | 38 | import android.content.pm.PackageParser.PackageParserException; |
| 39 | import android.content.pm.PackageParser.SigningDetails; |
| 40 | import android.content.pm.PackageParser.SigningDetails.SignatureSchemeVersion; |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 41 | import android.content.pm.ParceledListSlice; |
Rhed Jao | 269616b | 2020-03-30 13:23:14 +0800 | [diff] [blame] | 42 | import android.content.pm.parsing.PackageInfoWithoutStateUtils; |
Narayan Kamath | fcd4a04 | 2019-02-01 14:16:37 +0000 | [diff] [blame] | 43 | import android.content.rollback.IRollbackManager; |
Oli Lan | c72b0bb | 2019-12-02 14:03:55 +0000 | [diff] [blame] | 44 | import android.content.rollback.RollbackInfo; |
| 45 | import android.content.rollback.RollbackManager; |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 46 | import android.os.Bundle; |
Narayan Kamath | 94c93ab | 2019-01-04 10:47:00 +0000 | [diff] [blame] | 47 | import android.os.Handler; |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 48 | import android.os.IBinder; |
Mohammad Samiul Islam | ef40027 | 2019-08-15 16:44:57 +0100 | [diff] [blame] | 49 | import android.os.Looper; |
| 50 | import android.os.Message; |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 51 | import android.os.ParcelFileDescriptor; |
Mohammad Samiul Islam | 8ce32fe | 2020-01-14 18:33:39 +0000 | [diff] [blame] | 52 | import android.os.ParcelableException; |
Nikita Ioffe | 39a6a5b | 2019-02-26 15:36:39 +0000 | [diff] [blame] | 53 | import android.os.PowerManager; |
Narayan Kamath | 9dfa674 | 2019-01-04 14:22:50 +0000 | [diff] [blame] | 54 | import android.os.RemoteException; |
| 55 | import android.os.ServiceManager; |
Mohammad Samiul Islam | a922531 | 2020-06-10 18:00:24 +0100 | [diff] [blame] | 56 | import android.os.SystemProperties; |
Mohammad Samiul Islam | 3fcecfc | 2019-12-20 17:46:01 +0000 | [diff] [blame] | 57 | import android.os.UserHandle; |
| 58 | import android.os.UserManagerInternal; |
Mohammad Samiul Islam | 7e9fdb0 | 2019-11-28 18:14:40 +0000 | [diff] [blame] | 59 | import android.os.storage.IStorageManager; |
Mohammad Samiul Islam | cc4c7d8 | 2019-11-05 18:18:28 +0000 | [diff] [blame] | 60 | import android.os.storage.StorageManager; |
Gavin Corkery | 333136c | 2020-02-03 19:03:43 +0000 | [diff] [blame] | 61 | import android.text.TextUtils; |
Nikita Ioffe | e0dbc98 | 2019-05-09 19:33:35 +0100 | [diff] [blame] | 62 | import android.util.IntArray; |
Narayan Kamath | 9dfa674 | 2019-01-04 14:22:50 +0000 | [diff] [blame] | 63 | import android.util.Slog; |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 64 | import android.util.SparseArray; |
Mohammad Samiul Islam | a0623e2 | 2020-04-24 09:26:19 +0100 | [diff] [blame] | 65 | import android.util.SparseBooleanArray; |
Oli Lan | c72b0bb | 2019-12-02 14:03:55 +0000 | [diff] [blame] | 66 | import android.util.SparseIntArray; |
Narayan Kamath | 9dfa674 | 2019-01-04 14:22:50 +0000 | [diff] [blame] | 67 | import android.util.apk.ApkSignatureVerifier; |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 68 | |
| 69 | import com.android.internal.annotations.GuardedBy; |
Mohammad Samiul Islam | 7e9fdb0 | 2019-11-28 18:14:40 +0000 | [diff] [blame] | 70 | import com.android.internal.content.PackageHelper; |
Narayan Kamath | 94c93ab | 2019-01-04 10:47:00 +0000 | [diff] [blame] | 71 | import com.android.internal.os.BackgroundThread; |
Mohammad Samiul Islam | 3fcecfc | 2019-12-20 17:46:01 +0000 | [diff] [blame] | 72 | import com.android.server.LocalServices; |
Gavin Corkery | 24b4ed5 | 2020-04-29 16:30:10 +0100 | [diff] [blame] | 73 | import com.android.server.SystemService; |
| 74 | import com.android.server.SystemServiceManager; |
Rhed Jao | 269616b | 2020-03-30 13:23:14 +0800 | [diff] [blame] | 75 | import com.android.server.pm.parsing.PackageParser2; |
Winson | 5e0a1d5 | 2020-01-24 12:00:33 -0800 | [diff] [blame] | 76 | import com.android.server.pm.parsing.pkg.AndroidPackage; |
Winson | e075629 | 2020-01-31 12:21:54 -0800 | [diff] [blame] | 77 | import com.android.server.pm.parsing.pkg.AndroidPackageUtils; |
Rhed Jao | 269616b | 2020-03-30 13:23:14 +0800 | [diff] [blame] | 78 | import com.android.server.pm.parsing.pkg.ParsedPackage; |
Gavin Corkery | 333136c | 2020-02-03 19:03:43 +0000 | [diff] [blame] | 79 | import com.android.server.rollback.WatchdogRollbackLogger; |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 80 | |
Mohammad Samiul Islam | 992b637 | 2020-05-07 12:27:54 +0100 | [diff] [blame] | 81 | import java.io.BufferedReader; |
| 82 | import java.io.BufferedWriter; |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 83 | import java.io.File; |
Mohammad Samiul Islam | 992b637 | 2020-05-07 12:27:54 +0100 | [diff] [blame] | 84 | import java.io.FileReader; |
| 85 | import java.io.FileWriter; |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 86 | import java.io.IOException; |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 87 | import java.util.ArrayList; |
| 88 | import java.util.List; |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 89 | import java.util.concurrent.LinkedBlockingQueue; |
| 90 | import java.util.concurrent.TimeUnit; |
Mohammad Samiul Islam | ef40027 | 2019-08-15 16:44:57 +0100 | [diff] [blame] | 91 | import java.util.function.Consumer; |
Dario Freni | f141aab | 2019-02-04 14:48:30 +0000 | [diff] [blame] | 92 | import java.util.function.Predicate; |
Rhed Jao | 269616b | 2020-03-30 13:23:14 +0800 | [diff] [blame] | 93 | import java.util.function.Supplier; |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 94 | |
| 95 | /** |
| 96 | * This class handles staged install sessions, i.e. install sessions that require packages to |
| 97 | * be installed only after a reboot. |
| 98 | */ |
| 99 | public class StagingManager { |
| 100 | |
| 101 | private static final String TAG = "StagingManager"; |
| 102 | |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 103 | private final PackageInstallerService mPi; |
Dario Freni | 2e8dffc | 2019-02-06 14:55:16 +0000 | [diff] [blame] | 104 | private final ApexManager mApexManager; |
Nikita Ioffe | 39a6a5b | 2019-02-26 15:36:39 +0000 | [diff] [blame] | 105 | private final PowerManager mPowerManager; |
Mohammad Samiul Islam | cc4c7d8 | 2019-11-05 18:18:28 +0000 | [diff] [blame] | 106 | private final Context mContext; |
Mohammad Samiul Islam | ef40027 | 2019-08-15 16:44:57 +0100 | [diff] [blame] | 107 | private final PreRebootVerificationHandler mPreRebootVerificationHandler; |
Rhed Jao | 269616b | 2020-03-30 13:23:14 +0800 | [diff] [blame] | 108 | private final Supplier<PackageParser2> mPackageParserSupplier; |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 109 | |
Mohammad Samiul Islam | 992b637 | 2020-05-07 12:27:54 +0100 | [diff] [blame] | 110 | private final File mFailureReasonFile = new File("/metadata/staged-install/failure_reason.txt"); |
| 111 | private String mFailureReason; |
| 112 | |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 113 | @GuardedBy("mStagedSessions") |
| 114 | private final SparseArray<PackageInstallerSession> mStagedSessions = new SparseArray<>(); |
| 115 | |
Oli Lan | c72b0bb | 2019-12-02 14:03:55 +0000 | [diff] [blame] | 116 | @GuardedBy("mStagedSessions") |
| 117 | private final SparseIntArray mSessionRollbackIds = new SparseIntArray(); |
| 118 | |
Gavin Corkery | 333136c | 2020-02-03 19:03:43 +0000 | [diff] [blame] | 119 | @GuardedBy("mFailedPackageNames") |
| 120 | private final List<String> mFailedPackageNames = new ArrayList<>(); |
| 121 | private String mNativeFailureReason; |
| 122 | |
Gavin Corkery | 24b4ed5 | 2020-04-29 16:30:10 +0100 | [diff] [blame] | 123 | @GuardedBy("mSuccessfulStagedSessionIds") |
| 124 | private final List<Integer> mSuccessfulStagedSessionIds = new ArrayList<>(); |
| 125 | |
Rhed Jao | 269616b | 2020-03-30 13:23:14 +0800 | [diff] [blame] | 126 | StagingManager(PackageInstallerService pi, Context context, |
| 127 | Supplier<PackageParser2> packageParserSupplier) { |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 128 | mPi = pi; |
Mohammad Samiul Islam | cc4c7d8 | 2019-11-05 18:18:28 +0000 | [diff] [blame] | 129 | mContext = context; |
Rhed Jao | 269616b | 2020-03-30 13:23:14 +0800 | [diff] [blame] | 130 | mPackageParserSupplier = packageParserSupplier; |
Mohammad Samiul Islam | 3fcecfc | 2019-12-20 17:46:01 +0000 | [diff] [blame] | 131 | |
| 132 | mApexManager = ApexManager.getInstance(); |
Nikita Ioffe | 39a6a5b | 2019-02-26 15:36:39 +0000 | [diff] [blame] | 133 | mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); |
Mohammad Samiul Islam | ef40027 | 2019-08-15 16:44:57 +0100 | [diff] [blame] | 134 | mPreRebootVerificationHandler = new PreRebootVerificationHandler( |
| 135 | BackgroundThread.get().getLooper()); |
Mohammad Samiul Islam | 992b637 | 2020-05-07 12:27:54 +0100 | [diff] [blame] | 136 | |
| 137 | if (mFailureReasonFile.exists()) { |
| 138 | try (BufferedReader reader = new BufferedReader(new FileReader(mFailureReasonFile))) { |
| 139 | mFailureReason = reader.readLine(); |
| 140 | } catch (Exception ignore) { } |
| 141 | } |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Gavin Corkery | 24b4ed5 | 2020-04-29 16:30:10 +0100 | [diff] [blame] | 144 | /** |
| 145 | This class manages lifecycle events for StagingManager. |
| 146 | */ |
| 147 | public static final class Lifecycle extends SystemService { |
| 148 | private static StagingManager sStagingManager; |
| 149 | |
| 150 | public Lifecycle(Context context) { |
| 151 | super(context); |
| 152 | } |
| 153 | |
| 154 | void startService(StagingManager stagingManager) { |
| 155 | sStagingManager = stagingManager; |
| 156 | LocalServices.getService(SystemServiceManager.class).startService(this); |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public void onStart() { |
| 161 | // no-op |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public void onBootPhase(int phase) { |
| 166 | if (phase == SystemService.PHASE_BOOT_COMPLETED && sStagingManager != null) { |
| 167 | sStagingManager.markStagedSessionsAsSuccessful(); |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 172 | private void updateStoredSession(@NonNull PackageInstallerSession sessionInfo) { |
| 173 | synchronized (mStagedSessions) { |
| 174 | PackageInstallerSession storedSession = mStagedSessions.get(sessionInfo.sessionId); |
Narayan Kamath | 94c93ab | 2019-01-04 10:47:00 +0000 | [diff] [blame] | 175 | // storedSession might be null if a call to abortSession was made before the session |
| 176 | // is updated. |
| 177 | if (storedSession != null) { |
| 178 | mStagedSessions.put(sessionInfo.sessionId, sessionInfo); |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 179 | } |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
Patrick Baumann | 6bc126b | 2020-03-06 10:34:17 -0800 | [diff] [blame] | 183 | ParceledListSlice<PackageInstaller.SessionInfo> getSessions(int callingUid) { |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 184 | final List<PackageInstaller.SessionInfo> result = new ArrayList<>(); |
| 185 | synchronized (mStagedSessions) { |
| 186 | for (int i = 0; i < mStagedSessions.size(); i++) { |
Patrick Baumann | 6bc126b | 2020-03-06 10:34:17 -0800 | [diff] [blame] | 187 | final PackageInstallerSession stagedSession = mStagedSessions.valueAt(i); |
Mohammad Samiul Islam | 731bd96 | 2020-04-23 16:23:21 +0100 | [diff] [blame] | 188 | if (stagedSession.isDestroyed()) { |
| 189 | continue; |
| 190 | } |
Patrick Baumann | 6bc126b | 2020-03-06 10:34:17 -0800 | [diff] [blame] | 191 | result.add(stagedSession.generateInfoForCaller(false /*icon*/, callingUid)); |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | return new ParceledListSlice<>(result); |
| 195 | } |
| 196 | |
Mohammad Samiul Islam | b884124 | 2019-07-29 14:38:38 +0100 | [diff] [blame] | 197 | /** |
| 198 | * Validates the signature used to sign the container of the new apex package |
| 199 | * |
| 200 | * @param newApexPkg The new apex package that is being installed |
Mohammad Samiul Islam | b884124 | 2019-07-29 14:38:38 +0100 | [diff] [blame] | 201 | * @throws PackageManagerException |
| 202 | */ |
Mohammad Samiul Islam | d7472d0 | 2019-11-11 17:53:47 +0000 | [diff] [blame] | 203 | private void validateApexSignature(PackageInfo newApexPkg) |
Nikita Ioffe | e0dbc98 | 2019-05-09 19:33:35 +0100 | [diff] [blame] | 204 | throws PackageManagerException { |
Mohammad Samiul Islam | b884124 | 2019-07-29 14:38:38 +0100 | [diff] [blame] | 205 | // Get signing details of the new package |
| 206 | final String apexPath = newApexPkg.applicationInfo.sourceDir; |
| 207 | final String packageName = newApexPkg.packageName; |
Michael Groover | 33df7c4 | 2020-01-24 19:00:01 -0800 | [diff] [blame] | 208 | int minSignatureScheme = ApkSignatureVerifier.getMinimumSignatureSchemeVersionForTargetSdk( |
| 209 | newApexPkg.applicationInfo.targetSdkVersion); |
Mohammad Samiul Islam | b884124 | 2019-07-29 14:38:38 +0100 | [diff] [blame] | 210 | |
Mohammad Samiul Islam | d7472d0 | 2019-11-11 17:53:47 +0000 | [diff] [blame] | 211 | final SigningDetails newSigningDetails; |
Narayan Kamath | 9dfa674 | 2019-01-04 14:22:50 +0000 | [diff] [blame] | 212 | try { |
Michael Groover | 33df7c4 | 2020-01-24 19:00:01 -0800 | [diff] [blame] | 213 | newSigningDetails = ApkSignatureVerifier.verify(apexPath, minSignatureScheme); |
Narayan Kamath | 9dfa674 | 2019-01-04 14:22:50 +0000 | [diff] [blame] | 214 | } catch (PackageParserException e) { |
Nikita Ioffe | e0dbc98 | 2019-05-09 19:33:35 +0100 | [diff] [blame] | 215 | throw new PackageManagerException(SessionInfo.STAGED_SESSION_VERIFICATION_FAILED, |
| 216 | "Failed to parse APEX package " + apexPath, e); |
Narayan Kamath | 9dfa674 | 2019-01-04 14:22:50 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Mohammad Samiul Islam | b884124 | 2019-07-29 14:38:38 +0100 | [diff] [blame] | 219 | // Get signing details of the existing package |
| 220 | final PackageInfo existingApexPkg = mApexManager.getPackageInfo(packageName, |
Nikita Ioffe | e0dbc98 | 2019-05-09 19:33:35 +0100 | [diff] [blame] | 221 | ApexManager.MATCH_ACTIVE_PACKAGE); |
Mohammad Samiul Islam | b884124 | 2019-07-29 14:38:38 +0100 | [diff] [blame] | 222 | if (existingApexPkg == null) { |
Nikita Ioffe | e0dbc98 | 2019-05-09 19:33:35 +0100 | [diff] [blame] | 223 | // This should never happen, because submitSessionToApexService ensures that no new |
| 224 | // apexes were installed. |
| 225 | throw new IllegalStateException("Unknown apex package " + packageName); |
Narayan Kamath | 9dfa674 | 2019-01-04 14:22:50 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | final SigningDetails existingSigningDetails; |
| 229 | try { |
| 230 | existingSigningDetails = ApkSignatureVerifier.verify( |
Mohammad Samiul Islam | b884124 | 2019-07-29 14:38:38 +0100 | [diff] [blame] | 231 | existingApexPkg.applicationInfo.sourceDir, SignatureSchemeVersion.JAR); |
Narayan Kamath | 9dfa674 | 2019-01-04 14:22:50 +0000 | [diff] [blame] | 232 | } catch (PackageParserException e) { |
Nikita Ioffe | e0dbc98 | 2019-05-09 19:33:35 +0100 | [diff] [blame] | 233 | throw new PackageManagerException(SessionInfo.STAGED_SESSION_VERIFICATION_FAILED, |
Mohammad Samiul Islam | b884124 | 2019-07-29 14:38:38 +0100 | [diff] [blame] | 234 | "Failed to parse APEX package " + existingApexPkg.applicationInfo.sourceDir, e); |
Narayan Kamath | 9dfa674 | 2019-01-04 14:22:50 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Mohammad Samiul Islam | b884124 | 2019-07-29 14:38:38 +0100 | [diff] [blame] | 237 | // Verify signing details for upgrade |
Mohammad Samiul Islam | d7472d0 | 2019-11-11 17:53:47 +0000 | [diff] [blame] | 238 | if (newSigningDetails.checkCapability(existingSigningDetails, |
| 239 | SigningDetails.CertCapabilities.INSTALLED_DATA) |
| 240 | || existingSigningDetails.checkCapability(newSigningDetails, |
| 241 | SigningDetails.CertCapabilities.ROLLBACK)) { |
Mohammad Samiul Islam | 8e35db1 | 2019-08-22 15:43:55 +0100 | [diff] [blame] | 242 | return; |
| 243 | } |
| 244 | |
Nikita Ioffe | e0dbc98 | 2019-05-09 19:33:35 +0100 | [diff] [blame] | 245 | throw new PackageManagerException(SessionInfo.STAGED_SESSION_VERIFICATION_FAILED, |
Mohammad Samiul Islam | b884124 | 2019-07-29 14:38:38 +0100 | [diff] [blame] | 246 | "APK-container signature of APEX package " + packageName + " with version " |
| 247 | + newApexPkg.versionCodeMajor + " and path " + apexPath + " is not" |
| 248 | + " compatible with the one currently installed on device"); |
Narayan Kamath | 9dfa674 | 2019-01-04 14:22:50 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Nikita Ioffe | e0dbc98 | 2019-05-09 19:33:35 +0100 | [diff] [blame] | 251 | private List<PackageInfo> submitSessionToApexService( |
| 252 | @NonNull PackageInstallerSession session) throws PackageManagerException { |
Oli Lan | c72b0bb | 2019-12-02 14:03:55 +0000 | [diff] [blame] | 253 | final IntArray childSessionIds = new IntArray(); |
Nikita Ioffe | e0dbc98 | 2019-05-09 19:33:35 +0100 | [diff] [blame] | 254 | if (session.isMultiPackage()) { |
| 255 | for (int id : session.getChildSessionIds()) { |
Mohammad Samiul Islam | a0623e2 | 2020-04-24 09:26:19 +0100 | [diff] [blame] | 256 | if (isApexSession(getStagedSession(id))) { |
Oli Lan | c72b0bb | 2019-12-02 14:03:55 +0000 | [diff] [blame] | 257 | childSessionIds.add(id); |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | ApexSessionParams apexSessionParams = new ApexSessionParams(); |
| 262 | apexSessionParams.sessionId = session.sessionId; |
| 263 | apexSessionParams.childSessionIds = childSessionIds.toArray(); |
| 264 | if (session.params.installReason == PackageManager.INSTALL_REASON_ROLLBACK) { |
| 265 | apexSessionParams.isRollback = true; |
| 266 | apexSessionParams.rollbackId = retrieveRollbackIdForCommitSession(session.sessionId); |
| 267 | } else { |
| 268 | synchronized (mStagedSessions) { |
| 269 | int rollbackId = mSessionRollbackIds.get(session.sessionId, -1); |
| 270 | if (rollbackId != -1) { |
| 271 | apexSessionParams.hasRollbackEnabled = true; |
| 272 | apexSessionParams.rollbackId = rollbackId; |
Nikita Ioffe | e0dbc98 | 2019-05-09 19:33:35 +0100 | [diff] [blame] | 273 | } |
Nikita Ioffe | 8bbb815 | 2019-02-21 15:54:54 +0000 | [diff] [blame] | 274 | } |
Nikita Ioffe | e0dbc98 | 2019-05-09 19:33:35 +0100 | [diff] [blame] | 275 | } |
Nikita Ioffe | 4e7d24a | 2019-07-05 15:49:45 +0100 | [diff] [blame] | 276 | // submitStagedSession will throw a PackageManagerException if apexd verification fails, |
| 277 | // which will be propagated to populate stagedSessionErrorMessage of this session. |
Oli Lan | c72b0bb | 2019-12-02 14:03:55 +0000 | [diff] [blame] | 278 | final ApexInfoList apexInfoList = mApexManager.submitStagedSession(apexSessionParams); |
Nikita Ioffe | e0dbc98 | 2019-05-09 19:33:35 +0100 | [diff] [blame] | 279 | final List<PackageInfo> result = new ArrayList<>(); |
Jackal Guo | ae03792 | 2020-03-25 15:15:18 +0800 | [diff] [blame] | 280 | final List<String> apexPackageNames = new ArrayList<>(); |
Oli Lan | c2c7a22 | 2019-07-31 15:27:22 +0100 | [diff] [blame] | 281 | for (ApexInfo apexInfo : apexInfoList.apexInfos) { |
| 282 | final PackageInfo packageInfo; |
Rhed Jao | 269616b | 2020-03-30 13:23:14 +0800 | [diff] [blame] | 283 | final int flags = PackageManager.GET_META_DATA; |
| 284 | try (PackageParser2 packageParser = mPackageParserSupplier.get()) { |
Oli Lan | c2c7a22 | 2019-07-31 15:27:22 +0100 | [diff] [blame] | 285 | File apexFile = new File(apexInfo.modulePath); |
Rhed Jao | 269616b | 2020-03-30 13:23:14 +0800 | [diff] [blame] | 286 | final ParsedPackage parsedPackage = packageParser.parsePackage( |
| 287 | apexFile, flags, false); |
| 288 | packageInfo = PackageInfoWithoutStateUtils.generate(parsedPackage, apexInfo, flags); |
| 289 | if (packageInfo == null) { |
| 290 | throw new PackageManagerException( |
| 291 | SessionInfo.STAGED_SESSION_VERIFICATION_FAILED, |
| 292 | "Unable to generate package info: " + apexInfo.modulePath); |
| 293 | } |
Nikita Ioffe | e0dbc98 | 2019-05-09 19:33:35 +0100 | [diff] [blame] | 294 | } catch (PackageParserException e) { |
| 295 | throw new PackageManagerException(SessionInfo.STAGED_SESSION_VERIFICATION_FAILED, |
Oli Lan | c2c7a22 | 2019-07-31 15:27:22 +0100 | [diff] [blame] | 296 | "Failed to parse APEX package " + apexInfo.modulePath, e); |
Nikita Ioffe | e0dbc98 | 2019-05-09 19:33:35 +0100 | [diff] [blame] | 297 | } |
Oli Lan | c2c7a22 | 2019-07-31 15:27:22 +0100 | [diff] [blame] | 298 | final PackageInfo activePackage = mApexManager.getPackageInfo(packageInfo.packageName, |
Nikita Ioffe | e0dbc98 | 2019-05-09 19:33:35 +0100 | [diff] [blame] | 299 | ApexManager.MATCH_ACTIVE_PACKAGE); |
| 300 | if (activePackage == null) { |
Oli Lan | c2c7a22 | 2019-07-31 15:27:22 +0100 | [diff] [blame] | 301 | Slog.w(TAG, "Attempting to install new APEX package " + packageInfo.packageName); |
Nikita Ioffe | e0dbc98 | 2019-05-09 19:33:35 +0100 | [diff] [blame] | 302 | throw new PackageManagerException(SessionInfo.STAGED_SESSION_VERIFICATION_FAILED, |
| 303 | "It is forbidden to install new APEX packages."); |
| 304 | } |
| 305 | checkRequiredVersionCode(session, activePackage); |
Oli Lan | c2c7a22 | 2019-07-31 15:27:22 +0100 | [diff] [blame] | 306 | checkDowngrade(session, activePackage, packageInfo); |
| 307 | result.add(packageInfo); |
Jackal Guo | ae03792 | 2020-03-25 15:15:18 +0800 | [diff] [blame] | 308 | apexPackageNames.add(packageInfo.packageName); |
Nikita Ioffe | e0dbc98 | 2019-05-09 19:33:35 +0100 | [diff] [blame] | 309 | } |
Jackal Guo | ae03792 | 2020-03-25 15:15:18 +0800 | [diff] [blame] | 310 | Slog.d(TAG, "Session " + session.sessionId + " has following APEX packages: " |
| 311 | + apexPackageNames); |
Nikita Ioffe | e0dbc98 | 2019-05-09 19:33:35 +0100 | [diff] [blame] | 312 | return result; |
| 313 | } |
| 314 | |
Oli Lan | c72b0bb | 2019-12-02 14:03:55 +0000 | [diff] [blame] | 315 | private int retrieveRollbackIdForCommitSession(int sessionId) throws PackageManagerException { |
| 316 | RollbackManager rm = mContext.getSystemService(RollbackManager.class); |
| 317 | |
Jackal Guo | 1b33309 | 2020-03-30 11:12:56 +0800 | [diff] [blame] | 318 | final List<RollbackInfo> rollbacks = rm.getRecentlyCommittedRollbacks(); |
| 319 | for (int i = 0, size = rollbacks.size(); i < size; i++) { |
| 320 | final RollbackInfo rollback = rollbacks.get(i); |
Oli Lan | c72b0bb | 2019-12-02 14:03:55 +0000 | [diff] [blame] | 321 | if (rollback.getCommittedSessionId() == sessionId) { |
| 322 | return rollback.getRollbackId(); |
| 323 | } |
| 324 | } |
| 325 | throw new PackageManagerException( |
| 326 | "Could not find rollback id for commit session: " + sessionId); |
| 327 | } |
| 328 | |
Nikita Ioffe | e0dbc98 | 2019-05-09 19:33:35 +0100 | [diff] [blame] | 329 | private void checkRequiredVersionCode(final PackageInstallerSession session, |
| 330 | final PackageInfo activePackage) throws PackageManagerException { |
| 331 | if (session.params.requiredInstalledVersionCode == PackageManager.VERSION_CODE_HIGHEST) { |
| 332 | return; |
| 333 | } |
| 334 | final long activeVersion = activePackage.applicationInfo.longVersionCode; |
| 335 | if (activeVersion != session.params.requiredInstalledVersionCode) { |
Mohammad Samiul Islam | 44ad95b | 2019-11-20 15:14:36 +0000 | [diff] [blame] | 336 | if (!mApexManager.abortStagedSession(session.sessionId)) { |
Nikita Ioffe | e0dbc98 | 2019-05-09 19:33:35 +0100 | [diff] [blame] | 337 | Slog.e(TAG, "Failed to abort apex session " + session.sessionId); |
| 338 | } |
| 339 | throw new PackageManagerException( |
| 340 | SessionInfo.STAGED_SESSION_VERIFICATION_FAILED, |
| 341 | "Installed version of APEX package " + activePackage.packageName |
Richard Uhler | 2124d4b | 2019-04-25 13:01:39 +0100 | [diff] [blame] | 342 | + " does not match required. Active version: " + activeVersion |
| 343 | + " required: " + session.params.requiredInstalledVersionCode); |
Nikita Ioffe | 8bbb815 | 2019-02-21 15:54:54 +0000 | [diff] [blame] | 344 | } |
Nikita Ioffe | e0dbc98 | 2019-05-09 19:33:35 +0100 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | private void checkDowngrade(final PackageInstallerSession session, |
| 348 | final PackageInfo activePackage, final PackageInfo newPackage) |
| 349 | throws PackageManagerException { |
| 350 | final long activeVersion = activePackage.applicationInfo.longVersionCode; |
| 351 | final long newVersionCode = newPackage.applicationInfo.longVersionCode; |
Winson | f00c755 | 2020-01-28 12:52:01 -0800 | [diff] [blame] | 352 | boolean isAppDebuggable = (activePackage.applicationInfo.flags |
| 353 | & ApplicationInfo.FLAG_DEBUGGABLE) != 0; |
Nikita Ioffe | f2c2e01 | 2019-07-23 17:48:38 +0100 | [diff] [blame] | 354 | final boolean allowsDowngrade = PackageManagerServiceUtils.isDowngradePermitted( |
Winson | f00c755 | 2020-01-28 12:52:01 -0800 | [diff] [blame] | 355 | session.params.installFlags, isAppDebuggable); |
Nikita Ioffe | e0dbc98 | 2019-05-09 19:33:35 +0100 | [diff] [blame] | 356 | if (activeVersion > newVersionCode && !allowsDowngrade) { |
Mohammad Samiul Islam | 44ad95b | 2019-11-20 15:14:36 +0000 | [diff] [blame] | 357 | if (!mApexManager.abortStagedSession(session.sessionId)) { |
Nikita Ioffe | e0dbc98 | 2019-05-09 19:33:35 +0100 | [diff] [blame] | 358 | Slog.e(TAG, "Failed to abort apex session " + session.sessionId); |
| 359 | } |
| 360 | throw new PackageManagerException( |
| 361 | SessionInfo.STAGED_SESSION_VERIFICATION_FAILED, |
| 362 | "Downgrade of APEX package " + newPackage.packageName |
| 363 | + " is not allowed. Active version: " + activeVersion |
| 364 | + " attempted: " + newVersionCode); |
| 365 | } |
Dario Freni | 015f935 | 2019-01-14 21:56:17 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Dario Freni | 015f935 | 2019-01-14 21:56:17 +0000 | [diff] [blame] | 368 | private static boolean isApexSession(@NonNull PackageInstallerSession session) { |
| 369 | return (session.params.installFlags & PackageManager.INSTALL_APEX) != 0; |
| 370 | } |
| 371 | |
Dario Freni | f141aab | 2019-02-04 14:48:30 +0000 | [diff] [blame] | 372 | private boolean sessionContains(@NonNull PackageInstallerSession session, |
| 373 | Predicate<PackageInstallerSession> filter) { |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 374 | if (!session.isMultiPackage()) { |
Dario Freni | f141aab | 2019-02-04 14:48:30 +0000 | [diff] [blame] | 375 | return filter.test(session); |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 376 | } |
| 377 | synchronized (mStagedSessions) { |
Jackal Guo | ae03792 | 2020-03-25 15:15:18 +0800 | [diff] [blame] | 378 | final int[] childSessionIds = session.getChildSessionIds(); |
| 379 | for (int id : childSessionIds) { |
| 380 | // Retrieve cached sessions matching ids. |
| 381 | final PackageInstallerSession s = mStagedSessions.get(id); |
| 382 | // Filter only the ones containing APEX. |
| 383 | if (filter.test(s)) { |
| 384 | return true; |
| 385 | } |
| 386 | } |
| 387 | return false; |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 388 | } |
| 389 | } |
| 390 | |
Dario Freni | f141aab | 2019-02-04 14:48:30 +0000 | [diff] [blame] | 391 | private boolean sessionContainsApex(@NonNull PackageInstallerSession session) { |
| 392 | return sessionContains(session, (s) -> isApexSession(s)); |
| 393 | } |
| 394 | |
| 395 | private boolean sessionContainsApk(@NonNull PackageInstallerSession session) { |
| 396 | return sessionContains(session, (s) -> !isApexSession(s)); |
| 397 | } |
| 398 | |
Mohammad Samiul Islam | 7e9fdb0 | 2019-11-28 18:14:40 +0000 | [diff] [blame] | 399 | // Reverts apex sessions and user data (if checkpoint is supported). Also reboots the device. |
Mohammad Samiul Islam | 992b637 | 2020-05-07 12:27:54 +0100 | [diff] [blame] | 400 | private void abortCheckpoint(int sessionId, String errorMsg) { |
| 401 | String failureReason = "Failed to install sessionId: " + sessionId + " Error: " + errorMsg; |
| 402 | Slog.e(TAG, failureReason); |
Mohammad Samiul Islam | 7e9fdb0 | 2019-11-28 18:14:40 +0000 | [diff] [blame] | 403 | try { |
| 404 | if (supportsCheckpoint() && needsCheckpoint()) { |
Mohammad Samiul Islam | 992b637 | 2020-05-07 12:27:54 +0100 | [diff] [blame] | 405 | // Store failure reason for next reboot |
| 406 | try (BufferedWriter writer = |
| 407 | new BufferedWriter(new FileWriter(mFailureReasonFile))) { |
| 408 | writer.write(failureReason); |
| 409 | } catch (Exception e) { |
| 410 | Slog.w(TAG, "Failed to save failure reason: ", e); |
| 411 | } |
| 412 | |
Mohammad Samiul Islam | 5bf077d | 2020-05-04 16:36:01 +0100 | [diff] [blame] | 413 | // Only revert apex sessions if device supports updating apex |
| 414 | if (mApexManager.isApexSupported()) { |
| 415 | mApexManager.revertActiveSessions(); |
| 416 | } |
Mohammad Samiul Islam | 7e9fdb0 | 2019-11-28 18:14:40 +0000 | [diff] [blame] | 417 | PackageHelper.getStorageManager().abortChanges( |
| 418 | "StagingManager initiated", false /*retry*/); |
| 419 | } |
| 420 | } catch (Exception e) { |
| 421 | Slog.wtf(TAG, "Failed to abort checkpoint", e); |
Mohammad Samiul Islam | 5bf077d | 2020-05-04 16:36:01 +0100 | [diff] [blame] | 422 | // Only revert apex sessions if device supports updating apex |
| 423 | if (mApexManager.isApexSupported()) { |
| 424 | mApexManager.revertActiveSessions(); |
| 425 | } |
Mohammad Samiul Islam | 7e9fdb0 | 2019-11-28 18:14:40 +0000 | [diff] [blame] | 426 | mPowerManager.reboot(null); |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | private boolean supportsCheckpoint() throws RemoteException { |
| 431 | return PackageHelper.getStorageManager().supportsCheckpoint(); |
| 432 | } |
| 433 | |
| 434 | private boolean needsCheckpoint() throws RemoteException { |
| 435 | return PackageHelper.getStorageManager().needsCheckpoint(); |
| 436 | } |
| 437 | |
Mohammad Samiul Islam | 3fcecfc | 2019-12-20 17:46:01 +0000 | [diff] [blame] | 438 | /** |
Mohammad Samiul Islam | cefe39f | 2020-03-22 21:32:33 +0000 | [diff] [blame] | 439 | * Utility function for extracting apex sessions out of multi-package/single session. |
Mohammad Samiul Islam | 3fcecfc | 2019-12-20 17:46:01 +0000 | [diff] [blame] | 440 | */ |
Mohammad Samiul Islam | cefe39f | 2020-03-22 21:32:33 +0000 | [diff] [blame] | 441 | private List<PackageInstallerSession> extractApexSessions(PackageInstallerSession session) { |
Mohammad Samiul Islam | 3fcecfc | 2019-12-20 17:46:01 +0000 | [diff] [blame] | 442 | List<PackageInstallerSession> apexSessions = new ArrayList<>(); |
| 443 | if (session.isMultiPackage()) { |
| 444 | List<PackageInstallerSession> childrenSessions = new ArrayList<>(); |
| 445 | synchronized (mStagedSessions) { |
| 446 | for (int childSessionId : session.getChildSessionIds()) { |
| 447 | PackageInstallerSession childSession = mStagedSessions.get(childSessionId); |
| 448 | if (childSession != null) { |
| 449 | childrenSessions.add(childSession); |
| 450 | } |
| 451 | } |
| 452 | } |
Jackal Guo | 1b33309 | 2020-03-30 11:12:56 +0800 | [diff] [blame] | 453 | for (int i = 0, size = childrenSessions.size(); i < size; i++) { |
| 454 | final PackageInstallerSession childSession = childrenSessions.get(i); |
Mohammad Samiul Islam | 3fcecfc | 2019-12-20 17:46:01 +0000 | [diff] [blame] | 455 | if (sessionContainsApex(childSession)) { |
| 456 | apexSessions.add(childSession); |
| 457 | } |
| 458 | } |
| 459 | } else { |
| 460 | apexSessions.add(session); |
| 461 | } |
Mohammad Samiul Islam | cefe39f | 2020-03-22 21:32:33 +0000 | [diff] [blame] | 462 | return apexSessions; |
| 463 | } |
| 464 | |
| 465 | /** |
| 466 | * Checks if all apk-in-apex were installed without errors for all of the apex sessions. Throws |
| 467 | * error for any apk-in-apex failed to install. |
| 468 | * |
| 469 | * @throws PackageManagerException if any apk-in-apex failed to install |
| 470 | */ |
| 471 | private void checkInstallationOfApkInApexSuccessful(PackageInstallerSession session) |
| 472 | throws PackageManagerException { |
| 473 | final List<PackageInstallerSession> apexSessions = extractApexSessions(session); |
| 474 | if (apexSessions.isEmpty()) { |
| 475 | return; |
| 476 | } |
| 477 | |
| 478 | for (PackageInstallerSession apexSession : apexSessions) { |
| 479 | String packageName = apexSession.getPackageName(); |
| 480 | if (!mApexManager.isApkInApexInstallSuccess(packageName)) { |
| 481 | throw new PackageManagerException(SessionInfo.STAGED_SESSION_ACTIVATION_FAILED, |
| 482 | "Failed to install apk-in-apex of " + packageName); |
| 483 | } |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | /** |
| 488 | * Perform snapshot and restore as required both for APEXes themselves and for apks in APEX. |
| 489 | * Apks inside apex are not installed using apk-install flow. They are scanned from the system |
| 490 | * directory directly by PackageManager, as such, RollbackManager need to handle their data |
| 491 | * separately here. |
| 492 | */ |
| 493 | private void snapshotAndRestoreForApexSession(PackageInstallerSession session) { |
| 494 | boolean doSnapshotOrRestore = |
| 495 | (session.params.installFlags & PackageManager.INSTALL_ENABLE_ROLLBACK) != 0 |
| 496 | || session.params.installReason == PackageManager.INSTALL_REASON_ROLLBACK; |
| 497 | if (!doSnapshotOrRestore) { |
| 498 | return; |
| 499 | } |
| 500 | |
| 501 | // Find all the apex sessions that needs processing |
| 502 | final List<PackageInstallerSession> apexSessions = extractApexSessions(session); |
| 503 | if (apexSessions.isEmpty()) { |
| 504 | return; |
| 505 | } |
Mohammad Samiul Islam | 3fcecfc | 2019-12-20 17:46:01 +0000 | [diff] [blame] | 506 | |
Oli Lan | 7bbd8b4 | 2020-01-14 10:11:42 +0000 | [diff] [blame] | 507 | final UserManagerInternal um = LocalServices.getService(UserManagerInternal.class); |
| 508 | final int[] allUsers = um.getUserIds(); |
| 509 | IRollbackManager rm = IRollbackManager.Stub.asInterface( |
| 510 | ServiceManager.getService(Context.ROLLBACK_SERVICE)); |
| 511 | |
Jackal Guo | 1b33309 | 2020-03-30 11:12:56 +0800 | [diff] [blame] | 512 | for (int i = 0, sessionsSize = apexSessions.size(); i < sessionsSize; i++) { |
| 513 | final String packageName = apexSessions.get(i).getPackageName(); |
Oli Lan | 7bbd8b4 | 2020-01-14 10:11:42 +0000 | [diff] [blame] | 514 | // Perform any snapshots or restores for the APEX itself |
| 515 | snapshotAndRestoreApexUserData(packageName, allUsers, rm); |
| 516 | |
| 517 | // Process the apks inside the APEX |
Jackal Guo | 1b33309 | 2020-03-30 11:12:56 +0800 | [diff] [blame] | 518 | final List<String> apksInApex = mApexManager.getApksInApex(packageName); |
| 519 | for (int j = 0, apksSize = apksInApex.size(); j < apksSize; j++) { |
| 520 | snapshotAndRestoreApkInApexUserData(apksInApex.get(j), allUsers, rm); |
Mohammad Samiul Islam | 3fcecfc | 2019-12-20 17:46:01 +0000 | [diff] [blame] | 521 | } |
| 522 | } |
| 523 | } |
| 524 | |
Oli Lan | 7bbd8b4 | 2020-01-14 10:11:42 +0000 | [diff] [blame] | 525 | private void snapshotAndRestoreApexUserData( |
| 526 | String packageName, int[] allUsers, IRollbackManager rm) { |
| 527 | try { |
| 528 | // appId, ceDataInode, and seInfo are not needed for APEXes |
| 529 | rm.snapshotAndRestoreUserData(packageName, allUsers, 0, 0, |
| 530 | null, 0 /*token*/); |
| 531 | } catch (RemoteException re) { |
| 532 | Slog.e(TAG, "Error snapshotting/restoring user data: " + re); |
| 533 | } |
| 534 | } |
Mohammad Samiul Islam | 3fcecfc | 2019-12-20 17:46:01 +0000 | [diff] [blame] | 535 | |
Oli Lan | 7bbd8b4 | 2020-01-14 10:11:42 +0000 | [diff] [blame] | 536 | private void snapshotAndRestoreApkInApexUserData( |
| 537 | String packageName, int[] allUsers, IRollbackManager rm) { |
Mohammad Samiul Islam | 3fcecfc | 2019-12-20 17:46:01 +0000 | [diff] [blame] | 538 | PackageManagerInternal mPmi = LocalServices.getService(PackageManagerInternal.class); |
| 539 | AndroidPackage pkg = mPmi.getPackage(packageName); |
| 540 | if (pkg == null) { |
| 541 | Slog.e(TAG, "Could not find package: " + packageName |
| 542 | + "for snapshotting/restoring user data."); |
| 543 | return; |
| 544 | } |
Mohammad Samiul Islam | 3fcecfc | 2019-12-20 17:46:01 +0000 | [diff] [blame] | 545 | |
| 546 | int appId = -1; |
| 547 | long ceDataInode = -1; |
Winson | e075629 | 2020-01-31 12:21:54 -0800 | [diff] [blame] | 548 | final PackageSetting ps = mPmi.getPackageSetting(packageName); |
Oli Lan | 7bbd8b4 | 2020-01-14 10:11:42 +0000 | [diff] [blame] | 549 | if (ps != null) { |
Mohammad Samiul Islam | 3fcecfc | 2019-12-20 17:46:01 +0000 | [diff] [blame] | 550 | appId = ps.appId; |
| 551 | ceDataInode = ps.getCeDataInode(UserHandle.USER_SYSTEM); |
| 552 | // NOTE: We ignore the user specified in the InstallParam because we know this is |
| 553 | // an update, and hence need to restore data for all installed users. |
| 554 | final int[] installedUsers = ps.queryInstalledUsers(allUsers, true); |
| 555 | |
Winson | e075629 | 2020-01-31 12:21:54 -0800 | [diff] [blame] | 556 | final String seInfo = AndroidPackageUtils.getSeInfo(pkg, ps); |
Mohammad Samiul Islam | 3fcecfc | 2019-12-20 17:46:01 +0000 | [diff] [blame] | 557 | try { |
| 558 | rm.snapshotAndRestoreUserData(packageName, installedUsers, appId, ceDataInode, |
| 559 | seInfo, 0 /*token*/); |
| 560 | } catch (RemoteException re) { |
| 561 | Slog.e(TAG, "Error snapshotting/restoring user data: " + re); |
| 562 | } |
| 563 | } |
| 564 | } |
| 565 | |
Gavin Corkery | 333136c | 2020-02-03 19:03:43 +0000 | [diff] [blame] | 566 | /** |
| 567 | * Prepares for the logging of apexd reverts by storing the native failure reason if necessary, |
| 568 | * and adding the package name of the session which apexd reverted to the list of reverted |
| 569 | * session package names. |
| 570 | * Logging needs to wait until the ACTION_BOOT_COMPLETED broadcast is sent. |
| 571 | */ |
| 572 | private void prepareForLoggingApexdRevert(@NonNull PackageInstallerSession session, |
| 573 | @NonNull String nativeFailureReason) { |
| 574 | synchronized (mFailedPackageNames) { |
| 575 | mNativeFailureReason = nativeFailureReason; |
| 576 | if (session.getPackageName() != null) { |
| 577 | mFailedPackageNames.add(session.getPackageName()); |
| 578 | } |
| 579 | } |
| 580 | } |
| 581 | |
Dario Freni | 61b3e25 | 2019-01-11 23:05:39 +0000 | [diff] [blame] | 582 | private void resumeSession(@NonNull PackageInstallerSession session) { |
Nikita Ioffe | f2c2e01 | 2019-07-23 17:48:38 +0100 | [diff] [blame] | 583 | Slog.d(TAG, "Resuming session " + session.sessionId); |
Mohammad Samiul Islam | 7e9fdb0 | 2019-11-28 18:14:40 +0000 | [diff] [blame] | 584 | |
Nikita Ioffe | c2b87a9 | 2019-07-15 13:28:11 +0100 | [diff] [blame] | 585 | final boolean hasApex = sessionContainsApex(session); |
Mohammad Samiul Islam | 7e9fdb0 | 2019-11-28 18:14:40 +0000 | [diff] [blame] | 586 | ApexSessionInfo apexSessionInfo = null; |
Nikita Ioffe | a820bd9 | 2019-02-15 14:22:44 +0000 | [diff] [blame] | 587 | if (hasApex) { |
Dario Freni | 2e8dffc | 2019-02-06 14:55:16 +0000 | [diff] [blame] | 588 | // Check with apexservice whether the apex packages have been activated. |
Mohammad Samiul Islam | 7e9fdb0 | 2019-11-28 18:14:40 +0000 | [diff] [blame] | 589 | apexSessionInfo = mApexManager.getStagedSessionInfo(session.sessionId); |
| 590 | |
Gavin Corkery | 333136c | 2020-02-03 19:03:43 +0000 | [diff] [blame] | 591 | // Prepare for logging a native crash during boot, if one occurred. |
| 592 | if (apexSessionInfo != null && !TextUtils.isEmpty( |
| 593 | apexSessionInfo.crashingNativeProcess)) { |
| 594 | prepareForLoggingApexdRevert(session, apexSessionInfo.crashingNativeProcess); |
| 595 | } |
| 596 | |
Mohammad Samiul Islam | 7e9fdb0 | 2019-11-28 18:14:40 +0000 | [diff] [blame] | 597 | if (apexSessionInfo != null && apexSessionInfo.isVerified) { |
| 598 | // Session has been previously submitted to apexd, but didn't complete all the |
| 599 | // pre-reboot verification, perhaps because the device rebooted in the meantime. |
| 600 | // Greedily re-trigger the pre-reboot verification. We want to avoid marking it as |
| 601 | // failed when not in checkpoint mode, hence it is being processed separately. |
| 602 | Slog.d(TAG, "Found pending staged session " + session.sessionId + " still to " |
| 603 | + "be verified, resuming pre-reboot verification"); |
| 604 | mPreRebootVerificationHandler.startPreRebootVerification(session.sessionId); |
| 605 | return; |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | // Before we resume session, we check if revert is needed or not. Typically, we enter file- |
| 610 | // system checkpoint mode when we reboot first time in order to install staged sessions. We |
| 611 | // want to install staged sessions in this mode as rebooting now will revert user data. If |
| 612 | // something goes wrong, then we reboot again to enter fs-rollback mode. Rebooting now will |
| 613 | // have no effect on user data, so mark the sessions as failed instead. |
| 614 | try { |
| 615 | // If checkpoint is supported, then we only resume sessions if we are in checkpointing |
| 616 | // mode. If not, we fail all sessions. |
| 617 | if (supportsCheckpoint() && !needsCheckpoint()) { |
Mohammad Samiul Islam | 992b637 | 2020-05-07 12:27:54 +0100 | [diff] [blame] | 618 | String errorMsg = "Reverting back to safe state. Marking " + session.sessionId |
| 619 | + " as failed"; |
| 620 | if (!TextUtils.isEmpty(mFailureReason)) { |
| 621 | errorMsg = errorMsg + ": " + mFailureReason; |
Gavin Corkery | 7e8b5a3 | 2020-04-20 09:49:18 +0100 | [diff] [blame] | 622 | } |
Mohammad Samiul Islam | 992b637 | 2020-05-07 12:27:54 +0100 | [diff] [blame] | 623 | Slog.d(TAG, errorMsg); |
Gavin Corkery | 7e8b5a3 | 2020-04-20 09:49:18 +0100 | [diff] [blame] | 624 | session.setStagedSessionFailed(SessionInfo.STAGED_SESSION_UNKNOWN, errorMsg); |
Mohammad Samiul Islam | 7e9fdb0 | 2019-11-28 18:14:40 +0000 | [diff] [blame] | 625 | return; |
| 626 | } |
| 627 | } catch (RemoteException e) { |
| 628 | // Cannot continue staged install without knowing if fs-checkpoint is supported |
| 629 | Slog.e(TAG, "Checkpoint support unknown. Aborting staged install for session " |
| 630 | + session.sessionId, e); |
| 631 | // TODO: Mark all staged sessions together and reboot only once |
| 632 | session.setStagedSessionFailed(SessionInfo.STAGED_SESSION_UNKNOWN, |
| 633 | "Checkpoint support unknown. Aborting staged install."); |
| 634 | if (hasApex) { |
| 635 | mApexManager.revertActiveSessions(); |
| 636 | } |
| 637 | mPowerManager.reboot("Checkpoint support unknown"); |
| 638 | return; |
| 639 | } |
| 640 | |
Mohammad Samiul Islam | cefe39f | 2020-03-22 21:32:33 +0000 | [diff] [blame] | 641 | // Check if apex packages in the session failed to activate |
Mohammad Samiul Islam | 7e9fdb0 | 2019-11-28 18:14:40 +0000 | [diff] [blame] | 642 | if (hasApex) { |
Dario Freni | 2e8dffc | 2019-02-06 14:55:16 +0000 | [diff] [blame] | 643 | if (apexSessionInfo == null) { |
Mohammad Samiul Islam | cefe39f | 2020-03-22 21:32:33 +0000 | [diff] [blame] | 644 | final String errorMsg = "apexd did not know anything about a staged session " |
| 645 | + "supposed to be activated"; |
Dario Freni | 2e8dffc | 2019-02-06 14:55:16 +0000 | [diff] [blame] | 646 | session.setStagedSessionFailed(SessionInfo.STAGED_SESSION_ACTIVATION_FAILED, |
Mohammad Samiul Islam | 166243f | 2020-03-06 16:19:22 +0000 | [diff] [blame] | 647 | errorMsg); |
Mohammad Samiul Islam | 992b637 | 2020-05-07 12:27:54 +0100 | [diff] [blame] | 648 | abortCheckpoint(session.sessionId, errorMsg); |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 649 | return; |
| 650 | } |
Nikita Ioffe | 8274222 | 2019-02-26 12:14:04 +0000 | [diff] [blame] | 651 | if (isApexSessionFailed(apexSessionInfo)) { |
Mohammad Samiul Islam | cefe39f | 2020-03-22 21:32:33 +0000 | [diff] [blame] | 652 | String errorMsg = "APEX activation failed. Check logcat messages from apexd " |
| 653 | + "for more information."; |
Gavin Corkery | 7e8b5a3 | 2020-04-20 09:49:18 +0100 | [diff] [blame] | 654 | if (!TextUtils.isEmpty(mNativeFailureReason)) { |
| 655 | errorMsg = "Session reverted due to crashing native process: " |
| 656 | + mNativeFailureReason; |
| 657 | } |
Dario Freni | b6d2896 | 2019-01-31 15:52:24 +0000 | [diff] [blame] | 658 | session.setStagedSessionFailed(SessionInfo.STAGED_SESSION_ACTIVATION_FAILED, |
Mohammad Samiul Islam | 166243f | 2020-03-06 16:19:22 +0000 | [diff] [blame] | 659 | errorMsg); |
Mohammad Samiul Islam | 992b637 | 2020-05-07 12:27:54 +0100 | [diff] [blame] | 660 | abortCheckpoint(session.sessionId, errorMsg); |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 661 | return; |
| 662 | } |
Nikita Ioffe | a820bd9 | 2019-02-15 14:22:44 +0000 | [diff] [blame] | 663 | if (!apexSessionInfo.isActivated && !apexSessionInfo.isSuccess) { |
Mohammad Samiul Islam | cefe39f | 2020-03-22 21:32:33 +0000 | [diff] [blame] | 664 | // Apexd did not apply the session for some unknown reason. There is no |
| 665 | // guarantee that apexd will install it next time. Safer to proactively mark |
| 666 | // it as failed. |
| 667 | final String errorMsg = "Staged session " + session.sessionId + "at boot " |
| 668 | + "didn't activate nor fail. Marking it as failed anyway."; |
Mohammad Samiul Islam | 7e9fdb0 | 2019-11-28 18:14:40 +0000 | [diff] [blame] | 669 | session.setStagedSessionFailed(SessionInfo.STAGED_SESSION_ACTIVATION_FAILED, |
Mohammad Samiul Islam | 166243f | 2020-03-06 16:19:22 +0000 | [diff] [blame] | 670 | errorMsg); |
Mohammad Samiul Islam | 992b637 | 2020-05-07 12:27:54 +0100 | [diff] [blame] | 671 | abortCheckpoint(session.sessionId, errorMsg); |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 672 | return; |
| 673 | } |
| 674 | } |
Mohammad Samiul Islam | cefe39f | 2020-03-22 21:32:33 +0000 | [diff] [blame] | 675 | // Handle apk and apk-in-apex installation |
Nikita Ioffe | c2b87a9 | 2019-07-15 13:28:11 +0100 | [diff] [blame] | 676 | try { |
Mohammad Samiul Islam | cefe39f | 2020-03-22 21:32:33 +0000 | [diff] [blame] | 677 | if (hasApex) { |
| 678 | checkInstallationOfApkInApexSuccessful(session); |
| 679 | snapshotAndRestoreForApexSession(session); |
| 680 | Slog.i(TAG, "APEX packages in session " + session.sessionId |
| 681 | + " were successfully activated. Proceeding with APK packages, if any"); |
| 682 | } |
| 683 | // The APEX part of the session is activated, proceed with the installation of APKs. |
Nikita Ioffe | f2c2e01 | 2019-07-23 17:48:38 +0100 | [diff] [blame] | 684 | Slog.d(TAG, "Installing APK packages in session " + session.sessionId); |
Mohammad Samiul Islam | 8c041df | 2019-09-10 16:26:26 +0100 | [diff] [blame] | 685 | installApksInSession(session); |
Nikita Ioffe | c2b87a9 | 2019-07-15 13:28:11 +0100 | [diff] [blame] | 686 | } catch (PackageManagerException e) { |
| 687 | session.setStagedSessionFailed(e.error, e.getMessage()); |
Mohammad Samiul Islam | 992b637 | 2020-05-07 12:27:54 +0100 | [diff] [blame] | 688 | abortCheckpoint(session.sessionId, e.getMessage()); |
Nikita Ioffe | 39a6a5b | 2019-02-26 15:36:39 +0000 | [diff] [blame] | 689 | |
Mohammad Samiul Islam | 7e9fdb0 | 2019-11-28 18:14:40 +0000 | [diff] [blame] | 690 | // If checkpoint is not supported, we have to handle failure for one staged session. |
Nikita Ioffe | 39a6a5b | 2019-02-26 15:36:39 +0000 | [diff] [blame] | 691 | if (!hasApex) { |
| 692 | return; |
| 693 | } |
| 694 | |
Mohammad Samiul Islam | 1a96eb6 | 2019-11-21 10:38:06 +0000 | [diff] [blame] | 695 | if (!mApexManager.revertActiveSessions()) { |
Nikita Ioffe | 39a6a5b | 2019-02-26 15:36:39 +0000 | [diff] [blame] | 696 | Slog.e(TAG, "Failed to abort APEXd session"); |
| 697 | } else { |
| 698 | Slog.e(TAG, |
| 699 | "Successfully aborted apexd session. Rebooting device in order to revert " |
| 700 | + "to the previous state of APEXd."); |
| 701 | mPowerManager.reboot(null); |
| 702 | } |
Dario Freni | 61b3e25 | 2019-01-11 23:05:39 +0000 | [diff] [blame] | 703 | return; |
| 704 | } |
Nikita Ioffe | a820bd9 | 2019-02-15 14:22:44 +0000 | [diff] [blame] | 705 | |
Nikita Ioffe | f2c2e01 | 2019-07-23 17:48:38 +0100 | [diff] [blame] | 706 | Slog.d(TAG, "Marking session " + session.sessionId + " as applied"); |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 707 | session.setStagedSessionApplied(); |
Nikita Ioffe | a820bd9 | 2019-02-15 14:22:44 +0000 | [diff] [blame] | 708 | if (hasApex) { |
Gavin Corkery | 24b4ed5 | 2020-04-29 16:30:10 +0100 | [diff] [blame] | 709 | try { |
| 710 | if (supportsCheckpoint()) { |
| 711 | // Store the session ID, which will be marked as successful by ApexManager |
| 712 | // upon boot completion. |
| 713 | synchronized (mSuccessfulStagedSessionIds) { |
| 714 | mSuccessfulStagedSessionIds.add(session.sessionId); |
| 715 | } |
| 716 | } else { |
| 717 | // Mark sessions as successful immediately on non-checkpointing devices. |
| 718 | mApexManager.markStagedSessionSuccessful(session.sessionId); |
| 719 | } |
| 720 | } catch (RemoteException e) { |
| 721 | Slog.w(TAG, "Checkpoint support unknown, marking session as successful " |
| 722 | + "immediately."); |
| 723 | mApexManager.markStagedSessionSuccessful(session.sessionId); |
| 724 | } |
Nikita Ioffe | a820bd9 | 2019-02-15 14:22:44 +0000 | [diff] [blame] | 725 | } |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 726 | } |
| 727 | |
Dario Freni | 815bd21 | 2019-02-20 14:09:36 +0000 | [diff] [blame] | 728 | private List<String> findAPKsInDir(File stageDir) { |
| 729 | List<String> ret = new ArrayList<>(); |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 730 | if (stageDir != null && stageDir.exists()) { |
| 731 | for (File file : stageDir.listFiles()) { |
| 732 | if (file.getAbsolutePath().toLowerCase().endsWith(".apk")) { |
Dario Freni | 815bd21 | 2019-02-20 14:09:36 +0000 | [diff] [blame] | 733 | ret.add(file.getAbsolutePath()); |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 734 | } |
| 735 | } |
Dario Freni | 61b3e25 | 2019-01-11 23:05:39 +0000 | [diff] [blame] | 736 | } |
Dario Freni | 815bd21 | 2019-02-20 14:09:36 +0000 | [diff] [blame] | 737 | return ret; |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 738 | } |
| 739 | |
Nikita Ioffe | c2b87a9 | 2019-07-15 13:28:11 +0100 | [diff] [blame] | 740 | @NonNull |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 741 | private PackageInstallerSession createAndWriteApkSession( |
Nikita Ioffe | c2b87a9 | 2019-07-15 13:28:11 +0100 | [diff] [blame] | 742 | @NonNull PackageInstallerSession originalSession, boolean preReboot) |
| 743 | throws PackageManagerException { |
| 744 | final int errorCode = preReboot ? SessionInfo.STAGED_SESSION_VERIFICATION_FAILED |
| 745 | : SessionInfo.STAGED_SESSION_ACTIVATION_FAILED; |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 746 | if (originalSession.stageDir == null) { |
| 747 | Slog.wtf(TAG, "Attempting to install a staged APK session with no staging dir"); |
Nikita Ioffe | c2b87a9 | 2019-07-15 13:28:11 +0100 | [diff] [blame] | 748 | throw new PackageManagerException(errorCode, |
| 749 | "Attempting to install a staged APK session with no staging dir"); |
Dario Freni | 1473bcb | 2019-01-25 14:27:13 +0000 | [diff] [blame] | 750 | } |
Dario Freni | 815bd21 | 2019-02-20 14:09:36 +0000 | [diff] [blame] | 751 | List<String> apkFilePaths = findAPKsInDir(originalSession.stageDir); |
| 752 | if (apkFilePaths.isEmpty()) { |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 753 | Slog.w(TAG, "Can't find staged APK in " + originalSession.stageDir.getAbsolutePath()); |
Nikita Ioffe | c2b87a9 | 2019-07-15 13:28:11 +0100 | [diff] [blame] | 754 | throw new PackageManagerException(errorCode, |
| 755 | "Can't find staged APK in " + originalSession.stageDir.getAbsolutePath()); |
Dario Freni | 61b3e25 | 2019-01-11 23:05:39 +0000 | [diff] [blame] | 756 | } |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 757 | |
| 758 | PackageInstaller.SessionParams params = originalSession.params.copy(); |
| 759 | params.isStaged = false; |
Dario Freni | a2afebf | 2019-05-01 16:03:17 +0100 | [diff] [blame] | 760 | params.installFlags |= PackageManager.INSTALL_STAGED; |
Dario Freni | c3e68ea | 2019-04-02 11:45:13 +0100 | [diff] [blame] | 761 | // TODO(b/129744602): use the userid from the original session. |
Dario Freni | f141aab | 2019-02-04 14:48:30 +0000 | [diff] [blame] | 762 | if (preReboot) { |
| 763 | params.installFlags &= ~PackageManager.INSTALL_ENABLE_ROLLBACK; |
| 764 | params.installFlags |= PackageManager.INSTALL_DRY_RUN; |
| 765 | } else { |
| 766 | params.installFlags |= PackageManager.INSTALL_DISABLE_VERIFICATION; |
| 767 | } |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 768 | try { |
Mohammad Samiul Islam | 8ce32fe | 2020-01-14 18:33:39 +0000 | [diff] [blame] | 769 | int apkSessionId = mPi.createSession( |
| 770 | params, originalSession.getInstallerPackageName(), |
| 771 | 0 /* UserHandle.SYSTEM */); |
| 772 | PackageInstallerSession apkSession = mPi.getSession(apkSessionId); |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 773 | apkSession.open(); |
Jackal Guo | 1b33309 | 2020-03-30 11:12:56 +0800 | [diff] [blame] | 774 | for (int i = 0, size = apkFilePaths.size(); i < size; i++) { |
| 775 | final String apkFilePath = apkFilePaths.get(i); |
Dario Freni | 815bd21 | 2019-02-20 14:09:36 +0000 | [diff] [blame] | 776 | File apkFile = new File(apkFilePath); |
| 777 | ParcelFileDescriptor pfd = ParcelFileDescriptor.open(apkFile, |
| 778 | ParcelFileDescriptor.MODE_READ_ONLY); |
Mohammad Samiul Islam | 8ce32fe | 2020-01-14 18:33:39 +0000 | [diff] [blame] | 779 | long sizeBytes = (pfd == null) ? -1 : pfd.getStatSize(); |
Dario Freni | 815bd21 | 2019-02-20 14:09:36 +0000 | [diff] [blame] | 780 | if (sizeBytes < 0) { |
| 781 | Slog.e(TAG, "Unable to get size of: " + apkFilePath); |
Nikita Ioffe | c2b87a9 | 2019-07-15 13:28:11 +0100 | [diff] [blame] | 782 | throw new PackageManagerException(errorCode, |
| 783 | "Unable to get size of: " + apkFilePath); |
Dario Freni | 815bd21 | 2019-02-20 14:09:36 +0000 | [diff] [blame] | 784 | } |
| 785 | apkSession.write(apkFile.getName(), 0, sizeBytes, pfd); |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 786 | } |
Mohammad Samiul Islam | 8ce32fe | 2020-01-14 18:33:39 +0000 | [diff] [blame] | 787 | return apkSession; |
| 788 | } catch (IOException | ParcelableException e) { |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 789 | Slog.e(TAG, "Failure to install APK staged session " + originalSession.sessionId, e); |
Mohammad Samiul Islam | 8ce32fe | 2020-01-14 18:33:39 +0000 | [diff] [blame] | 790 | throw new PackageManagerException(errorCode, "Failed to create/write APK session", e); |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 791 | } |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 792 | } |
| 793 | |
Mohammad Samiul Islam | 8c041df | 2019-09-10 16:26:26 +0100 | [diff] [blame] | 794 | /** |
| 795 | * Extract apks in the given session into a new session. Returns {@code null} if there is no |
| 796 | * apks in the given session. Only parent session is returned for multi-package session. |
| 797 | */ |
| 798 | @Nullable |
| 799 | private PackageInstallerSession extractApksInSession(PackageInstallerSession session, |
| 800 | boolean preReboot) throws PackageManagerException { |
Nikita Ioffe | c2b87a9 | 2019-07-15 13:28:11 +0100 | [diff] [blame] | 801 | final int errorCode = preReboot ? SessionInfo.STAGED_SESSION_VERIFICATION_FAILED |
| 802 | : SessionInfo.STAGED_SESSION_ACTIVATION_FAILED; |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 803 | if (!session.isMultiPackage() && !isApexSession(session)) { |
Mohammad Samiul Islam | 8c041df | 2019-09-10 16:26:26 +0100 | [diff] [blame] | 804 | return createAndWriteApkSession(session, preReboot); |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 805 | } else if (session.isMultiPackage()) { |
| 806 | // For multi-package staged sessions containing APKs, we identify which child sessions |
| 807 | // contain an APK, and with those then create a new multi-package group of sessions, |
| 808 | // carrying over all the session parameters and unmarking them as staged. On commit the |
| 809 | // sessions will be installed atomically. |
Jackal Guo | ae03792 | 2020-03-25 15:15:18 +0800 | [diff] [blame] | 810 | final List<PackageInstallerSession> childSessions = new ArrayList<>(); |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 811 | synchronized (mStagedSessions) { |
Jackal Guo | ae03792 | 2020-03-25 15:15:18 +0800 | [diff] [blame] | 812 | final int[] childSessionIds = session.getChildSessionIds(); |
| 813 | for (int id : childSessionIds) { |
| 814 | final PackageInstallerSession s = mStagedSessions.get(id); |
| 815 | if (!isApexSession(s)) { |
| 816 | childSessions.add(s); |
| 817 | } |
| 818 | } |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 819 | } |
| 820 | if (childSessions.isEmpty()) { |
| 821 | // APEX-only multi-package staged session, nothing to do. |
Mohammad Samiul Islam | 8c041df | 2019-09-10 16:26:26 +0100 | [diff] [blame] | 822 | return null; |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 823 | } |
Mohammad Samiul Islam | 8c041df | 2019-09-10 16:26:26 +0100 | [diff] [blame] | 824 | final PackageInstaller.SessionParams params = session.params.copy(); |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 825 | params.isStaged = false; |
Dario Freni | f141aab | 2019-02-04 14:48:30 +0000 | [diff] [blame] | 826 | if (preReboot) { |
| 827 | params.installFlags &= ~PackageManager.INSTALL_ENABLE_ROLLBACK; |
| 828 | } |
Dario Freni | c3e68ea | 2019-04-02 11:45:13 +0100 | [diff] [blame] | 829 | // TODO(b/129744602): use the userid from the original session. |
Mohammad Samiul Islam | 8c041df | 2019-09-10 16:26:26 +0100 | [diff] [blame] | 830 | final int apkParentSessionId = mPi.createSession( |
Dario Freni | c3e68ea | 2019-04-02 11:45:13 +0100 | [diff] [blame] | 831 | params, session.getInstallerPackageName(), |
| 832 | 0 /* UserHandle.SYSTEM */); |
Mohammad Samiul Islam | 8c041df | 2019-09-10 16:26:26 +0100 | [diff] [blame] | 833 | final PackageInstallerSession apkParentSession = mPi.getSession(apkParentSessionId); |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 834 | try { |
| 835 | apkParentSession.open(); |
| 836 | } catch (IOException e) { |
| 837 | Slog.e(TAG, "Unable to prepare multi-package session for staged session " |
| 838 | + session.sessionId); |
Nikita Ioffe | c2b87a9 | 2019-07-15 13:28:11 +0100 | [diff] [blame] | 839 | throw new PackageManagerException(errorCode, |
| 840 | "Unable to prepare multi-package session for staged session"); |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 841 | } |
| 842 | |
Jackal Guo | 1b33309 | 2020-03-30 11:12:56 +0800 | [diff] [blame] | 843 | for (int i = 0, size = childSessions.size(); i < size; i++) { |
| 844 | final PackageInstallerSession apkChildSession = createAndWriteApkSession( |
| 845 | childSessions.get(i), preReboot); |
Dario Freni | 0966047 | 2019-02-18 16:44:23 +0000 | [diff] [blame] | 846 | try { |
| 847 | apkParentSession.addChildSessionId(apkChildSession.sessionId); |
Patrick Baumann | 00321b7 | 2019-04-09 15:07:25 -0700 | [diff] [blame] | 848 | } catch (IllegalStateException e) { |
Dario Freni | 0966047 | 2019-02-18 16:44:23 +0000 | [diff] [blame] | 849 | Slog.e(TAG, "Failed to add a child session for installing the APK files", e); |
Nikita Ioffe | c2b87a9 | 2019-07-15 13:28:11 +0100 | [diff] [blame] | 850 | throw new PackageManagerException(errorCode, |
| 851 | "Failed to add a child session " + apkChildSession.sessionId); |
Dario Freni | 0966047 | 2019-02-18 16:44:23 +0000 | [diff] [blame] | 852 | } |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 853 | } |
Mohammad Samiul Islam | 8c041df | 2019-09-10 16:26:26 +0100 | [diff] [blame] | 854 | return apkParentSession; |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 855 | } |
Mohammad Samiul Islam | 8c041df | 2019-09-10 16:26:26 +0100 | [diff] [blame] | 856 | return null; |
| 857 | } |
| 858 | |
| 859 | private void verifyApksInSession(PackageInstallerSession session) |
| 860 | throws PackageManagerException { |
| 861 | |
| 862 | final PackageInstallerSession apksToVerify = extractApksInSession( |
| 863 | session, /* preReboot */ true); |
| 864 | if (apksToVerify == null) { |
| 865 | return; |
| 866 | } |
| 867 | |
| 868 | final LocalIntentReceiverAsync receiver = new LocalIntentReceiverAsync( |
| 869 | (Intent result) -> { |
| 870 | int status = result.getIntExtra(PackageInstaller.EXTRA_STATUS, |
| 871 | PackageInstaller.STATUS_FAILURE); |
| 872 | if (status != PackageInstaller.STATUS_SUCCESS) { |
| 873 | final String errorMessage = result.getStringExtra( |
| 874 | PackageInstaller.EXTRA_STATUS_MESSAGE); |
| 875 | Slog.e(TAG, "Failure to verify APK staged session " |
| 876 | + session.sessionId + " [" + errorMessage + "]"); |
| 877 | session.setStagedSessionFailed( |
| 878 | SessionInfo.STAGED_SESSION_VERIFICATION_FAILED, errorMessage); |
Mohammad Samiul Islam | a0623e2 | 2020-04-24 09:26:19 +0100 | [diff] [blame] | 879 | mPreRebootVerificationHandler.onPreRebootVerificationComplete( |
| 880 | session.sessionId); |
Mohammad Samiul Islam | 8c041df | 2019-09-10 16:26:26 +0100 | [diff] [blame] | 881 | return; |
| 882 | } |
| 883 | mPreRebootVerificationHandler.notifyPreRebootVerification_Apk_Complete( |
| 884 | session.sessionId); |
| 885 | }); |
| 886 | |
| 887 | apksToVerify.commit(receiver.getIntentSender(), false); |
| 888 | } |
| 889 | |
| 890 | private void installApksInSession(@NonNull PackageInstallerSession session) |
| 891 | throws PackageManagerException { |
| 892 | |
| 893 | final PackageInstallerSession apksToInstall = extractApksInSession( |
| 894 | session, /* preReboot */ false); |
| 895 | if (apksToInstall == null) { |
| 896 | return; |
| 897 | } |
| 898 | |
| 899 | if ((apksToInstall.params.installFlags & PackageManager.INSTALL_ENABLE_ROLLBACK) != 0) { |
| 900 | // If rollback is available for this session, notify the rollback |
| 901 | // manager of the apk session so it can properly enable rollback. |
| 902 | final IRollbackManager rm = IRollbackManager.Stub.asInterface( |
| 903 | ServiceManager.getService(Context.ROLLBACK_SERVICE)); |
| 904 | try { |
| 905 | rm.notifyStagedApkSession(session.sessionId, apksToInstall.sessionId); |
| 906 | } catch (RemoteException re) { |
| 907 | Slog.e(TAG, "Failed to notifyStagedApkSession for session: " |
| 908 | + session.sessionId, re); |
| 909 | } |
| 910 | } |
| 911 | |
| 912 | final LocalIntentReceiverSync receiver = new LocalIntentReceiverSync(); |
| 913 | apksToInstall.commit(receiver.getIntentSender(), false); |
| 914 | final Intent result = receiver.getResult(); |
| 915 | final int status = result.getIntExtra(PackageInstaller.EXTRA_STATUS, |
| 916 | PackageInstaller.STATUS_FAILURE); |
| 917 | if (status != PackageInstaller.STATUS_SUCCESS) { |
| 918 | final String errorMessage = result.getStringExtra( |
| 919 | PackageInstaller.EXTRA_STATUS_MESSAGE); |
| 920 | Slog.e(TAG, "Failure to install APK staged session " |
| 921 | + session.sessionId + " [" + errorMessage + "]"); |
| 922 | throw new PackageManagerException( |
| 923 | SessionInfo.STAGED_SESSION_ACTIVATION_FAILED, errorMessage); |
| 924 | } |
Dario Freni | 61b3e25 | 2019-01-11 23:05:39 +0000 | [diff] [blame] | 925 | } |
| 926 | |
Nikita Ioffe | 8e5b703c | 2019-03-14 18:40:17 +0000 | [diff] [blame] | 927 | void commitSession(@NonNull PackageInstallerSession session) { |
Dario Freni | 276cd07 | 2019-01-09 11:13:44 +0000 | [diff] [blame] | 928 | updateStoredSession(session); |
Mohammad Samiul Islam | 6dc9889 | 2019-09-13 11:10:12 +0100 | [diff] [blame] | 929 | mPreRebootVerificationHandler.startPreRebootVerification(session.sessionId); |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 930 | } |
| 931 | |
Mohammad Samiul Islam | cc4c7d8 | 2019-11-05 18:18:28 +0000 | [diff] [blame] | 932 | private int parentOrOwnSessionId(PackageInstallerSession session) { |
| 933 | return session.hasParentSessionId() ? session.getParentSessionId() : session.sessionId; |
| 934 | } |
| 935 | |
Mohammad Samiul Islam | da00497 | 2019-10-09 11:29:26 +0100 | [diff] [blame] | 936 | /** |
| 937 | * <p> Check if the session provided is non-overlapping with the active staged sessions. |
| 938 | * |
| 939 | * <p> A session is non-overlapping if it meets one of the following conditions: </p> |
| 940 | * <ul> |
| 941 | * <li>It is a parent session</li> |
| 942 | * <li>It is already one of the active sessions</li> |
| 943 | * <li>Its package name is not same as any of the active sessions</li> |
| 944 | * </ul> |
| 945 | * @throws PackageManagerException if session fails the check |
| 946 | */ |
| 947 | void checkNonOverlappingWithStagedSessions(@NonNull PackageInstallerSession session) |
| 948 | throws PackageManagerException { |
| 949 | if (session.isMultiPackage()) { |
| 950 | // We cannot say a parent session overlaps until we process its children |
| 951 | return; |
| 952 | } |
| 953 | if (session.getPackageName() == null) { |
| 954 | throw new PackageManagerException(PackageManager.INSTALL_FAILED_INVALID_APK, |
| 955 | "Cannot stage session " + session.sessionId + " with package name null"); |
| 956 | } |
| 957 | |
Mohammad Samiul Islam | cc4c7d8 | 2019-11-05 18:18:28 +0000 | [diff] [blame] | 958 | boolean supportsCheckpoint = ((StorageManager) mContext.getSystemService( |
| 959 | Context.STORAGE_SERVICE)).isCheckpointSupported(); |
| 960 | |
Nikita Ioffe | da998cf | 2019-03-04 22:54:30 +0000 | [diff] [blame] | 961 | synchronized (mStagedSessions) { |
| 962 | for (int i = 0; i < mStagedSessions.size(); i++) { |
Mohammad Samiul Islam | da00497 | 2019-10-09 11:29:26 +0100 | [diff] [blame] | 963 | final PackageInstallerSession stagedSession = mStagedSessions.valueAt(i); |
Mohammad Samiul Islam | a0623e2 | 2020-04-24 09:26:19 +0100 | [diff] [blame] | 964 | if (!stagedSession.isCommitted() || stagedSession.isStagedAndInTerminalState() |
| 965 | || stagedSession.isDestroyed()) { |
Nikita Ioffe | da998cf | 2019-03-04 22:54:30 +0000 | [diff] [blame] | 966 | continue; |
| 967 | } |
Mohammad Samiul Islam | da00497 | 2019-10-09 11:29:26 +0100 | [diff] [blame] | 968 | if (stagedSession.isMultiPackage()) { |
| 969 | // This active parent staged session is useless as it doesn't have a package |
| 970 | // name and the session we are checking is not a parent session either. |
Nikita Ioffe | da998cf | 2019-03-04 22:54:30 +0000 | [diff] [blame] | 971 | continue; |
| 972 | } |
Mohammad Samiul Islam | da00497 | 2019-10-09 11:29:26 +0100 | [diff] [blame] | 973 | |
| 974 | // From here on, stagedSession is a non-parent active staged session |
| 975 | |
| 976 | // Check if stagedSession has an active parent session or not |
| 977 | if (stagedSession.hasParentSessionId()) { |
| 978 | int parentId = stagedSession.getParentSessionId(); |
| 979 | PackageInstallerSession parentSession = mStagedSessions.get(parentId); |
| 980 | if (parentSession == null || parentSession.isStagedAndInTerminalState()) { |
| 981 | // Parent session has been abandoned or terminated already |
| 982 | continue; |
| 983 | } |
| 984 | } |
| 985 | |
| 986 | // Check if session is one of the active sessions |
| 987 | if (session.sessionId == stagedSession.sessionId) { |
| 988 | Slog.w(TAG, "Session " + session.sessionId + " is already staged"); |
| 989 | continue; |
| 990 | } |
| 991 | |
| 992 | // If session is not among the active sessions, then it cannot have same package |
| 993 | // name as any of the active sessions. |
| 994 | if (session.getPackageName().equals(stagedSession.getPackageName())) { |
| 995 | throw new PackageManagerException( |
| 996 | PackageManager.INSTALL_FAILED_OTHER_STAGED_SESSION_IN_PROGRESS, |
| 997 | "Package: " + session.getPackageName() + " in session: " |
| 998 | + session.sessionId + " has been staged already by session: " |
| 999 | + stagedSession.sessionId, null); |
| 1000 | } |
| 1001 | |
Mohammad Samiul Islam | cc4c7d8 | 2019-11-05 18:18:28 +0000 | [diff] [blame] | 1002 | // Staging multiple root sessions is not allowed if device doesn't support |
| 1003 | // checkpoint. If session and stagedSession do not have common ancestor, they are |
| 1004 | // from two different root sessions. |
| 1005 | if (!supportsCheckpoint |
| 1006 | && parentOrOwnSessionId(session) != parentOrOwnSessionId(stagedSession)) { |
| 1007 | throw new PackageManagerException( |
| 1008 | PackageManager.INSTALL_FAILED_OTHER_STAGED_SESSION_IN_PROGRESS, |
| 1009 | "Cannot stage multiple sessions without checkpoint support", null); |
| 1010 | } |
Nikita Ioffe | da998cf | 2019-03-04 22:54:30 +0000 | [diff] [blame] | 1011 | } |
| 1012 | } |
Nikita Ioffe | da998cf | 2019-03-04 22:54:30 +0000 | [diff] [blame] | 1013 | } |
| 1014 | |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 1015 | void createSession(@NonNull PackageInstallerSession sessionInfo) { |
| 1016 | synchronized (mStagedSessions) { |
| 1017 | mStagedSessions.append(sessionInfo.sessionId, sessionInfo); |
| 1018 | } |
| 1019 | } |
| 1020 | |
Dario Freni | 015f935 | 2019-01-14 21:56:17 +0000 | [diff] [blame] | 1021 | void abortSession(@NonNull PackageInstallerSession session) { |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 1022 | synchronized (mStagedSessions) { |
Dario Freni | 015f935 | 2019-01-14 21:56:17 +0000 | [diff] [blame] | 1023 | mStagedSessions.remove(session.sessionId); |
Oli Lan | c72b0bb | 2019-12-02 14:03:55 +0000 | [diff] [blame] | 1024 | mSessionRollbackIds.delete(session.sessionId); |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 1025 | } |
| 1026 | } |
Dario Freni | 8e7d0ec | 2019-01-10 15:21:40 +0000 | [diff] [blame] | 1027 | |
Mohammad Samiul Islam | a0623e2 | 2020-04-24 09:26:19 +0100 | [diff] [blame] | 1028 | /** |
| 1029 | * <p>Abort committed staged session |
| 1030 | * |
| 1031 | * <p>This method must be called while holding {@link PackageInstallerSession.mLock}. |
| 1032 | * |
| 1033 | * <p>The method returns {@code false} to indicate it is not safe to clean up the session from |
| 1034 | * system yet. When it is safe, the method returns {@code true}. |
| 1035 | * |
| 1036 | * <p> When it is safe to clean up, {@link StagingManager} will call |
| 1037 | * {@link PackageInstallerSession#abandon()} on the session again. |
| 1038 | * |
| 1039 | * @return {@code true} if it is safe to cleanup the session resources, otherwise {@code false}. |
| 1040 | */ |
| 1041 | boolean abortCommittedSessionLocked(@NonNull PackageInstallerSession session) { |
| 1042 | int sessionId = session.sessionId; |
Nikita Ioffe | 1e523b5 | 2019-03-13 16:07:48 +0000 | [diff] [blame] | 1043 | if (session.isStagedSessionApplied()) { |
Mohammad Samiul Islam | a0623e2 | 2020-04-24 09:26:19 +0100 | [diff] [blame] | 1044 | Slog.w(TAG, "Cannot abort applied session : " + sessionId); |
| 1045 | return false; |
shafik | 07205e3 | 2019-02-07 20:12:33 +0000 | [diff] [blame] | 1046 | } |
Mohammad Samiul Islam | a0623e2 | 2020-04-24 09:26:19 +0100 | [diff] [blame] | 1047 | if (!session.isDestroyed()) { |
| 1048 | throw new IllegalStateException("Committed session must be destroyed before aborting it" |
| 1049 | + " from StagingManager"); |
| 1050 | } |
| 1051 | if (getStagedSession(sessionId) == null) { |
| 1052 | Slog.w(TAG, "Session " + sessionId + " has been abandoned already"); |
| 1053 | return false; |
| 1054 | } |
Nikita Ioffe | e2d52f5 | 2019-03-11 14:29:23 +0000 | [diff] [blame] | 1055 | |
Mohammad Samiul Islam | a0623e2 | 2020-04-24 09:26:19 +0100 | [diff] [blame] | 1056 | // If pre-reboot verification is running, then return false. StagingManager will call |
| 1057 | // abandon again when pre-reboot verification ends. |
| 1058 | if (mPreRebootVerificationHandler.isVerificationRunning(sessionId)) { |
| 1059 | Slog.w(TAG, "Session " + sessionId + " aborted before pre-reboot " |
| 1060 | + "verification completed."); |
| 1061 | return false; |
| 1062 | } |
| 1063 | |
| 1064 | // A session could be marked ready once its pre-reboot verification ends |
| 1065 | if (session.isStagedSessionReady()) { |
| 1066 | if (sessionContainsApex(session)) { |
| 1067 | try { |
| 1068 | ApexSessionInfo apexSession = |
| 1069 | mApexManager.getStagedSessionInfo(session.sessionId); |
| 1070 | if (apexSession == null || isApexSessionFinalized(apexSession)) { |
| 1071 | Slog.w(TAG, |
| 1072 | "Cannot abort session " + session.sessionId |
| 1073 | + " because it is not active."); |
| 1074 | } else { |
| 1075 | mApexManager.abortStagedSession(session.sessionId); |
| 1076 | } |
| 1077 | } catch (Exception e) { |
| 1078 | // Failed to contact apexd service. The apex might still be staged. We can still |
| 1079 | // safely cleanup the staged session since pre-reboot verification is complete. |
| 1080 | // Also, cleaning up the stageDir prevents the apex from being activated. |
| 1081 | Slog.w(TAG, "Could not contact apexd to abort staged session " + sessionId); |
| 1082 | } |
Mohammad Samiul Islam | 44ad95b | 2019-11-20 15:14:36 +0000 | [diff] [blame] | 1083 | } |
Nikita Ioffe | e2d52f5 | 2019-03-11 14:29:23 +0000 | [diff] [blame] | 1084 | } |
Mohammad Samiul Islam | a0623e2 | 2020-04-24 09:26:19 +0100 | [diff] [blame] | 1085 | |
| 1086 | // Session was successfully aborted from apexd (if required) and pre-reboot verification |
| 1087 | // is also complete. It is now safe to clean up the session from system. |
| 1088 | abortSession(session); |
| 1089 | return true; |
shafik | 07205e3 | 2019-02-07 20:12:33 +0000 | [diff] [blame] | 1090 | } |
| 1091 | |
Nikita Ioffe | e2d52f5 | 2019-03-11 14:29:23 +0000 | [diff] [blame] | 1092 | private boolean isApexSessionFinalized(ApexSessionInfo session) { |
shafik | 07205e3 | 2019-02-07 20:12:33 +0000 | [diff] [blame] | 1093 | /* checking if the session is in a final state, i.e., not active anymore */ |
Nikita Ioffe | 8274222 | 2019-02-26 12:14:04 +0000 | [diff] [blame] | 1094 | return session.isUnknown || session.isActivationFailed || session.isSuccess |
Mohammad Samiul Islam | 2dfc86a | 2019-11-20 13:53:07 +0000 | [diff] [blame] | 1095 | || session.isReverted; |
Nikita Ioffe | 8274222 | 2019-02-26 12:14:04 +0000 | [diff] [blame] | 1096 | } |
| 1097 | |
| 1098 | private static boolean isApexSessionFailed(ApexSessionInfo apexSessionInfo) { |
Mohammad Samiul Islam | 2dfc86a | 2019-11-20 13:53:07 +0000 | [diff] [blame] | 1099 | // isRevertInProgress is included to cover the scenario, when a device is rebooted |
| 1100 | // during the revert, and apexd fails to resume the revert after reboot. |
Nikita Ioffe | 8274222 | 2019-02-26 12:14:04 +0000 | [diff] [blame] | 1101 | return apexSessionInfo.isActivationFailed || apexSessionInfo.isUnknown |
Mohammad Samiul Islam | 2dfc86a | 2019-11-20 13:53:07 +0000 | [diff] [blame] | 1102 | || apexSessionInfo.isReverted || apexSessionInfo.isRevertInProgress |
| 1103 | || apexSessionInfo.isRevertFailed; |
shafik | 07205e3 | 2019-02-07 20:12:33 +0000 | [diff] [blame] | 1104 | } |
| 1105 | |
Dario Freni | 015f935 | 2019-01-14 21:56:17 +0000 | [diff] [blame] | 1106 | @GuardedBy("mStagedSessions") |
| 1107 | private boolean isMultiPackageSessionComplete(@NonNull PackageInstallerSession session) { |
| 1108 | // This method assumes that the argument is either a parent session of a multi-package |
| 1109 | // i.e. isMultiPackage() returns true, or that it is a child session, i.e. |
| 1110 | // hasParentSessionId() returns true. |
| 1111 | if (session.isMultiPackage()) { |
| 1112 | // Parent session of a multi-package group. Check that we restored all the children. |
| 1113 | for (int childSession : session.getChildSessionIds()) { |
| 1114 | if (mStagedSessions.get(childSession) == null) { |
| 1115 | return false; |
| 1116 | } |
| 1117 | } |
| 1118 | return true; |
| 1119 | } |
| 1120 | if (session.hasParentSessionId()) { |
| 1121 | PackageInstallerSession parent = mStagedSessions.get(session.getParentSessionId()); |
| 1122 | if (parent == null) { |
| 1123 | return false; |
| 1124 | } |
| 1125 | return isMultiPackageSessionComplete(parent); |
| 1126 | } |
| 1127 | Slog.wtf(TAG, "Attempting to restore an invalid multi-package session."); |
| 1128 | return false; |
| 1129 | } |
| 1130 | |
Rhed Jao | 1fc8b36 | 2020-01-16 18:38:17 +0800 | [diff] [blame] | 1131 | void restoreSession(@NonNull PackageInstallerSession session, boolean isDeviceUpgrading) { |
Dario Freni | 015f935 | 2019-01-14 21:56:17 +0000 | [diff] [blame] | 1132 | PackageInstallerSession sessionToResume = session; |
| 1133 | synchronized (mStagedSessions) { |
| 1134 | mStagedSessions.append(session.sessionId, session); |
| 1135 | // For multi-package sessions, we don't know in which order they will be restored. We |
| 1136 | // need to wait until we have restored all the session in a group before restoring them. |
| 1137 | if (session.isMultiPackage() || session.hasParentSessionId()) { |
| 1138 | if (!isMultiPackageSessionComplete(session)) { |
| 1139 | // Still haven't recovered all sessions of the group, return. |
| 1140 | return; |
| 1141 | } |
| 1142 | // Group recovered, find the parent if necessary and resume the installation. |
| 1143 | if (session.hasParentSessionId()) { |
| 1144 | sessionToResume = mStagedSessions.get(session.getParentSessionId()); |
| 1145 | } |
| 1146 | } |
| 1147 | } |
Rhed Jao | 1fc8b36 | 2020-01-16 18:38:17 +0800 | [diff] [blame] | 1148 | // The preconditions used during pre-reboot verification might have changed when device |
| 1149 | // is upgrading. Updated staged sessions to activation failed before we resume the session. |
| 1150 | if (isDeviceUpgrading && !sessionToResume.isStagedAndInTerminalState()) { |
| 1151 | sessionToResume.setStagedSessionFailed(SessionInfo.STAGED_SESSION_ACTIVATION_FAILED, |
| 1152 | "Build fingerprint has changed"); |
| 1153 | return; |
| 1154 | } |
Dario Freni | 015f935 | 2019-01-14 21:56:17 +0000 | [diff] [blame] | 1155 | checkStateAndResume(sessionToResume); |
| 1156 | } |
| 1157 | |
| 1158 | private void checkStateAndResume(@NonNull PackageInstallerSession session) { |
Mohammad Samiul Islam | a922531 | 2020-06-10 18:00:24 +0100 | [diff] [blame] | 1159 | // Do not resume session if boot completed already |
| 1160 | if (SystemProperties.getBoolean("sys.boot_completed", false)) { |
| 1161 | return; |
| 1162 | } |
| 1163 | |
Dario Freni | 47799f4 | 2019-03-13 18:06:24 +0000 | [diff] [blame] | 1164 | if (!session.isCommitted()) { |
| 1165 | // Session hasn't been committed yet, ignore. |
| 1166 | return; |
| 1167 | } |
Dario Freni | 61b3e25 | 2019-01-11 23:05:39 +0000 | [diff] [blame] | 1168 | // Check the state of the session and decide what to do next. |
| 1169 | if (session.isStagedSessionFailed() || session.isStagedSessionApplied()) { |
| 1170 | // Final states, nothing to do. |
| 1171 | return; |
| 1172 | } |
Mohammad Samiul Islam | a0623e2 | 2020-04-24 09:26:19 +0100 | [diff] [blame] | 1173 | if (session.isDestroyed()) { |
| 1174 | // Device rebooted before abandoned session was cleaned up. |
| 1175 | session.abandon(); |
| 1176 | return; |
| 1177 | } |
Dario Freni | 61b3e25 | 2019-01-11 23:05:39 +0000 | [diff] [blame] | 1178 | if (!session.isStagedSessionReady()) { |
| 1179 | // The framework got restarted before the pre-reboot verification could complete, |
| 1180 | // restart the verification. |
Mohammad Samiul Islam | 6dc9889 | 2019-09-13 11:10:12 +0100 | [diff] [blame] | 1181 | mPreRebootVerificationHandler.startPreRebootVerification(session.sessionId); |
Dario Freni | 61b3e25 | 2019-01-11 23:05:39 +0000 | [diff] [blame] | 1182 | } else { |
| 1183 | // Session had already being marked ready. Start the checks to verify if there is any |
| 1184 | // follow-up work. |
Dario Freni | 4df010e | 2019-01-31 19:34:46 +0000 | [diff] [blame] | 1185 | resumeSession(session); |
Dario Freni | 61b3e25 | 2019-01-11 23:05:39 +0000 | [diff] [blame] | 1186 | } |
Dario Freni | 8e7d0ec | 2019-01-10 15:21:40 +0000 | [diff] [blame] | 1187 | } |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 1188 | |
Gavin Corkery | 333136c | 2020-02-03 19:03:43 +0000 | [diff] [blame] | 1189 | private void logFailedApexSessionsIfNecessary() { |
| 1190 | synchronized (mFailedPackageNames) { |
| 1191 | if (!mFailedPackageNames.isEmpty()) { |
| 1192 | WatchdogRollbackLogger.logApexdRevert(mContext, |
| 1193 | mFailedPackageNames, mNativeFailureReason); |
| 1194 | } |
| 1195 | } |
| 1196 | } |
| 1197 | |
Gavin Corkery | 24b4ed5 | 2020-04-29 16:30:10 +0100 | [diff] [blame] | 1198 | void markStagedSessionsAsSuccessful() { |
| 1199 | synchronized (mSuccessfulStagedSessionIds) { |
| 1200 | for (int i = 0; i < mSuccessfulStagedSessionIds.size(); i++) { |
| 1201 | mApexManager.markStagedSessionSuccessful(mSuccessfulStagedSessionIds.get(i)); |
| 1202 | } |
| 1203 | } |
| 1204 | } |
| 1205 | |
Rhed Jao | b793c21 | 2019-10-30 20:54:37 +0800 | [diff] [blame] | 1206 | void systemReady() { |
Gavin Corkery | 24b4ed5 | 2020-04-29 16:30:10 +0100 | [diff] [blame] | 1207 | new Lifecycle(mContext).startService(this); |
Rhed Jao | b793c21 | 2019-10-30 20:54:37 +0800 | [diff] [blame] | 1208 | // Register the receiver of boot completed intent for staging manager. |
| 1209 | mContext.registerReceiver(new BroadcastReceiver() { |
| 1210 | @Override |
| 1211 | public void onReceive(Context ctx, Intent intent) { |
| 1212 | mPreRebootVerificationHandler.readyToStart(); |
Gavin Corkery | 333136c | 2020-02-03 19:03:43 +0000 | [diff] [blame] | 1213 | BackgroundThread.getExecutor().execute( |
| 1214 | () -> logFailedApexSessionsIfNecessary()); |
Rhed Jao | b793c21 | 2019-10-30 20:54:37 +0800 | [diff] [blame] | 1215 | ctx.unregisterReceiver(this); |
| 1216 | } |
| 1217 | }, new IntentFilter(Intent.ACTION_BOOT_COMPLETED)); |
Mohammad Samiul Islam | 992b637 | 2020-05-07 12:27:54 +0100 | [diff] [blame] | 1218 | |
| 1219 | mFailureReasonFile.delete(); |
Rhed Jao | b793c21 | 2019-10-30 20:54:37 +0800 | [diff] [blame] | 1220 | } |
| 1221 | |
Mohammad Samiul Islam | ef40027 | 2019-08-15 16:44:57 +0100 | [diff] [blame] | 1222 | private static class LocalIntentReceiverAsync { |
| 1223 | final Consumer<Intent> mConsumer; |
| 1224 | |
| 1225 | LocalIntentReceiverAsync(Consumer<Intent> consumer) { |
| 1226 | mConsumer = consumer; |
| 1227 | } |
| 1228 | |
| 1229 | private IIntentSender.Stub mLocalSender = new IIntentSender.Stub() { |
| 1230 | @Override |
| 1231 | public void send(int code, Intent intent, String resolvedType, IBinder whitelistToken, |
| 1232 | IIntentReceiver finishedReceiver, String requiredPermission, Bundle options) { |
| 1233 | mConsumer.accept(intent); |
| 1234 | } |
| 1235 | }; |
| 1236 | |
| 1237 | public IntentSender getIntentSender() { |
| 1238 | return new IntentSender((IIntentSender) mLocalSender); |
| 1239 | } |
| 1240 | } |
| 1241 | |
| 1242 | private static class LocalIntentReceiverSync { |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 1243 | private final LinkedBlockingQueue<Intent> mResult = new LinkedBlockingQueue<>(); |
| 1244 | |
| 1245 | private IIntentSender.Stub mLocalSender = new IIntentSender.Stub() { |
| 1246 | @Override |
| 1247 | public void send(int code, Intent intent, String resolvedType, IBinder whitelistToken, |
Mohammad Samiul Islam | ef40027 | 2019-08-15 16:44:57 +0100 | [diff] [blame] | 1248 | IIntentReceiver finishedReceiver, String requiredPermission, |
| 1249 | Bundle options) { |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 1250 | try { |
| 1251 | mResult.offer(intent, 5, TimeUnit.SECONDS); |
| 1252 | } catch (InterruptedException e) { |
| 1253 | throw new RuntimeException(e); |
| 1254 | } |
| 1255 | } |
| 1256 | }; |
| 1257 | |
| 1258 | public IntentSender getIntentSender() { |
| 1259 | return new IntentSender((IIntentSender) mLocalSender); |
| 1260 | } |
| 1261 | |
| 1262 | public Intent getResult() { |
| 1263 | try { |
| 1264 | return mResult.take(); |
| 1265 | } catch (InterruptedException e) { |
| 1266 | throw new RuntimeException(e); |
| 1267 | } |
| 1268 | } |
| 1269 | } |
Mohammad Samiul Islam | ef40027 | 2019-08-15 16:44:57 +0100 | [diff] [blame] | 1270 | |
Mohammad Samiul Islam | a0623e2 | 2020-04-24 09:26:19 +0100 | [diff] [blame] | 1271 | private PackageInstallerSession getStagedSession(int sessionId) { |
| 1272 | PackageInstallerSession session; |
| 1273 | synchronized (mStagedSessions) { |
| 1274 | session = mStagedSessions.get(sessionId); |
| 1275 | } |
| 1276 | return session; |
| 1277 | } |
| 1278 | |
Mohammad Samiul Islam | ef40027 | 2019-08-15 16:44:57 +0100 | [diff] [blame] | 1279 | private final class PreRebootVerificationHandler extends Handler { |
Rhed Jao | b793c21 | 2019-10-30 20:54:37 +0800 | [diff] [blame] | 1280 | // Hold session ids before handler gets ready to do the verification. |
| 1281 | private IntArray mPendingSessionIds; |
| 1282 | private boolean mIsReady; |
Mohammad Samiul Islam | a0623e2 | 2020-04-24 09:26:19 +0100 | [diff] [blame] | 1283 | @GuardedBy("mVerificationRunning") |
| 1284 | private final SparseBooleanArray mVerificationRunning = new SparseBooleanArray(); |
Mohammad Samiul Islam | ef40027 | 2019-08-15 16:44:57 +0100 | [diff] [blame] | 1285 | |
| 1286 | PreRebootVerificationHandler(Looper looper) { |
| 1287 | super(looper); |
| 1288 | } |
| 1289 | |
| 1290 | /** |
| 1291 | * Handler for states of pre reboot verification. The states are arranged linearly (shown |
| 1292 | * below) with each state either calling the next state, or calling some other method that |
| 1293 | * eventually calls the next state. |
| 1294 | * |
| 1295 | * <p><ul> |
| 1296 | * <li>MSG_PRE_REBOOT_VERIFICATION_START</li> |
| 1297 | * <li>MSG_PRE_REBOOT_VERIFICATION_APEX</li> |
| 1298 | * <li>MSG_PRE_REBOOT_VERIFICATION_APK</li> |
| 1299 | * <li>MSG_PRE_REBOOT_VERIFICATION_END</li> |
| 1300 | * </ul></p> |
| 1301 | * |
| 1302 | * Details about each of state can be found in corresponding handler of node. |
| 1303 | */ |
| 1304 | private static final int MSG_PRE_REBOOT_VERIFICATION_START = 1; |
| 1305 | private static final int MSG_PRE_REBOOT_VERIFICATION_APEX = 2; |
| 1306 | private static final int MSG_PRE_REBOOT_VERIFICATION_APK = 3; |
| 1307 | private static final int MSG_PRE_REBOOT_VERIFICATION_END = 4; |
| 1308 | |
| 1309 | @Override |
| 1310 | public void handleMessage(Message msg) { |
Mohammad Samiul Islam | 6dc9889 | 2019-09-13 11:10:12 +0100 | [diff] [blame] | 1311 | final int sessionId = msg.arg1; |
Mohammad Samiul Islam | a0623e2 | 2020-04-24 09:26:19 +0100 | [diff] [blame] | 1312 | final PackageInstallerSession session = getStagedSession(sessionId); |
Mohammad Samiul Islam | 6dc9889 | 2019-09-13 11:10:12 +0100 | [diff] [blame] | 1313 | if (session == null) { |
Mohammad Samiul Islam | a0623e2 | 2020-04-24 09:26:19 +0100 | [diff] [blame] | 1314 | Slog.wtf(TAG, "Session disappeared in the middle of pre-reboot verification: " |
| 1315 | + sessionId); |
| 1316 | return; |
| 1317 | } |
| 1318 | if (session.isDestroyed()) { |
| 1319 | // No point in running verification on a destroyed session |
| 1320 | onPreRebootVerificationComplete(sessionId); |
Mohammad Samiul Islam | 6dc9889 | 2019-09-13 11:10:12 +0100 | [diff] [blame] | 1321 | return; |
| 1322 | } |
Mohammad Samiul Islam | ef40027 | 2019-08-15 16:44:57 +0100 | [diff] [blame] | 1323 | switch (msg.what) { |
| 1324 | case MSG_PRE_REBOOT_VERIFICATION_START: |
| 1325 | handlePreRebootVerification_Start(session); |
| 1326 | break; |
| 1327 | case MSG_PRE_REBOOT_VERIFICATION_APEX: |
| 1328 | handlePreRebootVerification_Apex(session); |
| 1329 | break; |
| 1330 | case MSG_PRE_REBOOT_VERIFICATION_APK: |
| 1331 | handlePreRebootVerification_Apk(session); |
| 1332 | break; |
| 1333 | case MSG_PRE_REBOOT_VERIFICATION_END: |
| 1334 | handlePreRebootVerification_End(session); |
| 1335 | break; |
| 1336 | } |
| 1337 | } |
| 1338 | |
Rhed Jao | b793c21 | 2019-10-30 20:54:37 +0800 | [diff] [blame] | 1339 | // Notify the handler that system is ready, and reschedule the pre-reboot verifications. |
| 1340 | private synchronized void readyToStart() { |
| 1341 | mIsReady = true; |
| 1342 | if (mPendingSessionIds != null) { |
| 1343 | for (int i = 0; i < mPendingSessionIds.size(); i++) { |
| 1344 | startPreRebootVerification(mPendingSessionIds.get(i)); |
| 1345 | } |
| 1346 | mPendingSessionIds = null; |
| 1347 | } |
| 1348 | } |
| 1349 | |
Mohammad Samiul Islam | ef40027 | 2019-08-15 16:44:57 +0100 | [diff] [blame] | 1350 | // Method for starting the pre-reboot verification |
Rhed Jao | b793c21 | 2019-10-30 20:54:37 +0800 | [diff] [blame] | 1351 | private synchronized void startPreRebootVerification(int sessionId) { |
| 1352 | if (!mIsReady) { |
| 1353 | if (mPendingSessionIds == null) { |
| 1354 | mPendingSessionIds = new IntArray(); |
| 1355 | } |
| 1356 | mPendingSessionIds.add(sessionId); |
| 1357 | return; |
| 1358 | } |
Mohammad Samiul Islam | a0623e2 | 2020-04-24 09:26:19 +0100 | [diff] [blame] | 1359 | |
| 1360 | PackageInstallerSession session = getStagedSession(sessionId); |
| 1361 | synchronized (mVerificationRunning) { |
| 1362 | // Do not start verification on a session that has been abandoned |
| 1363 | if (session == null || session.isDestroyed()) { |
| 1364 | return; |
| 1365 | } |
| 1366 | Slog.d(TAG, "Starting preRebootVerification for session " + sessionId); |
| 1367 | mVerificationRunning.put(sessionId, true); |
| 1368 | } |
Mohammad Samiul Islam | 6dc9889 | 2019-09-13 11:10:12 +0100 | [diff] [blame] | 1369 | obtainMessage(MSG_PRE_REBOOT_VERIFICATION_START, sessionId, 0).sendToTarget(); |
Mohammad Samiul Islam | ef40027 | 2019-08-15 16:44:57 +0100 | [diff] [blame] | 1370 | } |
| 1371 | |
Mohammad Samiul Islam | a0623e2 | 2020-04-24 09:26:19 +0100 | [diff] [blame] | 1372 | // Things to do when pre-reboot verification completes for a particular sessionId |
| 1373 | private void onPreRebootVerificationComplete(int sessionId) { |
| 1374 | // Remove it from mVerificationRunning so that verification is considered complete |
| 1375 | synchronized (mVerificationRunning) { |
| 1376 | Slog.d(TAG, "Stopping preRebootVerification for session " + sessionId); |
| 1377 | mVerificationRunning.delete(sessionId); |
| 1378 | } |
| 1379 | // Check if the session was destroyed while pre-reboot verification was running. If so, |
| 1380 | // abandon it again. |
| 1381 | PackageInstallerSession session = getStagedSession(sessionId); |
| 1382 | if (session != null && session.isDestroyed()) { |
| 1383 | session.abandon(); |
| 1384 | } |
| 1385 | } |
| 1386 | |
| 1387 | private boolean isVerificationRunning(int sessionId) { |
| 1388 | synchronized (mVerificationRunning) { |
| 1389 | return mVerificationRunning.get(sessionId); |
| 1390 | } |
| 1391 | } |
| 1392 | |
Mohammad Samiul Islam | 6dc9889 | 2019-09-13 11:10:12 +0100 | [diff] [blame] | 1393 | private void notifyPreRebootVerification_Start_Complete(int sessionId) { |
| 1394 | obtainMessage(MSG_PRE_REBOOT_VERIFICATION_APEX, sessionId, 0).sendToTarget(); |
Mohammad Samiul Islam | ef40027 | 2019-08-15 16:44:57 +0100 | [diff] [blame] | 1395 | } |
| 1396 | |
Mohammad Samiul Islam | 6dc9889 | 2019-09-13 11:10:12 +0100 | [diff] [blame] | 1397 | private void notifyPreRebootVerification_Apex_Complete(int sessionId) { |
| 1398 | obtainMessage(MSG_PRE_REBOOT_VERIFICATION_APK, sessionId, 0).sendToTarget(); |
Mohammad Samiul Islam | ef40027 | 2019-08-15 16:44:57 +0100 | [diff] [blame] | 1399 | } |
| 1400 | |
Mohammad Samiul Islam | 6dc9889 | 2019-09-13 11:10:12 +0100 | [diff] [blame] | 1401 | private void notifyPreRebootVerification_Apk_Complete(int sessionId) { |
| 1402 | obtainMessage(MSG_PRE_REBOOT_VERIFICATION_END, sessionId, 0).sendToTarget(); |
Mohammad Samiul Islam | ef40027 | 2019-08-15 16:44:57 +0100 | [diff] [blame] | 1403 | } |
| 1404 | |
| 1405 | /** |
| 1406 | * A dummy state for starting the pre reboot verification. |
| 1407 | * |
| 1408 | * See {@link PreRebootVerificationHandler} to see all nodes of pre reboot verification |
| 1409 | */ |
| 1410 | private void handlePreRebootVerification_Start(@NonNull PackageInstallerSession session) { |
Oli Lan | c72b0bb | 2019-12-02 14:03:55 +0000 | [diff] [blame] | 1411 | if ((session.params.installFlags & PackageManager.INSTALL_ENABLE_ROLLBACK) != 0) { |
| 1412 | // If rollback is enabled for this session, we call through to the RollbackManager |
| 1413 | // with the list of sessions it must enable rollback for. Note that |
| 1414 | // notifyStagedSession is a synchronous operation. |
| 1415 | final IRollbackManager rm = IRollbackManager.Stub.asInterface( |
| 1416 | ServiceManager.getService(Context.ROLLBACK_SERVICE)); |
| 1417 | try { |
| 1418 | // NOTE: To stay consistent with the non-staged install flow, we don't fail the |
| 1419 | // entire install if rollbacks can't be enabled. |
| 1420 | int rollbackId = rm.notifyStagedSession(session.sessionId); |
| 1421 | if (rollbackId != -1) { |
| 1422 | synchronized (mStagedSessions) { |
| 1423 | mSessionRollbackIds.put(session.sessionId, rollbackId); |
| 1424 | } |
| 1425 | } |
| 1426 | } catch (RemoteException re) { |
| 1427 | Slog.e(TAG, "Failed to notifyStagedSession for session: " |
| 1428 | + session.sessionId, re); |
| 1429 | } |
| 1430 | } |
| 1431 | |
Mohammad Samiul Islam | 6dc9889 | 2019-09-13 11:10:12 +0100 | [diff] [blame] | 1432 | notifyPreRebootVerification_Start_Complete(session.sessionId); |
Mohammad Samiul Islam | ef40027 | 2019-08-15 16:44:57 +0100 | [diff] [blame] | 1433 | } |
| 1434 | |
| 1435 | /** |
| 1436 | * Pre-reboot verification state for apex files: |
| 1437 | * |
| 1438 | * <p><ul> |
| 1439 | * <li>submits session to apex service</li> |
| 1440 | * <li>validates signatures of apex files</li> |
| 1441 | * </ul></p> |
| 1442 | */ |
| 1443 | private void handlePreRebootVerification_Apex(@NonNull PackageInstallerSession session) { |
| 1444 | final boolean hasApex = sessionContainsApex(session); |
| 1445 | |
| 1446 | // APEX checks. For single-package sessions, check if they contain an APEX. For |
| 1447 | // multi-package sessions, find all the child sessions that contain an APEX. |
| 1448 | if (hasApex) { |
Jackal Guo | 63aa98d | 2020-04-14 16:47:32 +0800 | [diff] [blame] | 1449 | final List<PackageInfo> apexPackages; |
Mohammad Samiul Islam | ef40027 | 2019-08-15 16:44:57 +0100 | [diff] [blame] | 1450 | try { |
Jackal Guo | 63aa98d | 2020-04-14 16:47:32 +0800 | [diff] [blame] | 1451 | apexPackages = submitSessionToApexService(session); |
Jackal Guo | 1b33309 | 2020-03-30 11:12:56 +0800 | [diff] [blame] | 1452 | for (int i = 0, size = apexPackages.size(); i < size; i++) { |
| 1453 | validateApexSignature(apexPackages.get(i)); |
Mohammad Samiul Islam | ef40027 | 2019-08-15 16:44:57 +0100 | [diff] [blame] | 1454 | } |
| 1455 | } catch (PackageManagerException e) { |
| 1456 | session.setStagedSessionFailed(e.error, e.getMessage()); |
Mohammad Samiul Islam | a0623e2 | 2020-04-24 09:26:19 +0100 | [diff] [blame] | 1457 | onPreRebootVerificationComplete(session.sessionId); |
Mohammad Samiul Islam | ef40027 | 2019-08-15 16:44:57 +0100 | [diff] [blame] | 1458 | return; |
| 1459 | } |
Jackal Guo | 63aa98d | 2020-04-14 16:47:32 +0800 | [diff] [blame] | 1460 | |
| 1461 | final PackageManagerInternal packageManagerInternal = |
| 1462 | LocalServices.getService(PackageManagerInternal.class); |
| 1463 | packageManagerInternal.pruneCachedApksInApex(apexPackages); |
Mohammad Samiul Islam | ef40027 | 2019-08-15 16:44:57 +0100 | [diff] [blame] | 1464 | } |
| 1465 | |
Mohammad Samiul Islam | 6dc9889 | 2019-09-13 11:10:12 +0100 | [diff] [blame] | 1466 | notifyPreRebootVerification_Apex_Complete(session.sessionId); |
Mohammad Samiul Islam | ef40027 | 2019-08-15 16:44:57 +0100 | [diff] [blame] | 1467 | } |
| 1468 | |
| 1469 | /** |
| 1470 | * Pre-reboot verification state for apk files: |
| 1471 | * <p><ul> |
| 1472 | * <li>performs a dry-run install of apk</li> |
| 1473 | * </ul></p> |
| 1474 | */ |
| 1475 | private void handlePreRebootVerification_Apk(@NonNull PackageInstallerSession session) { |
| 1476 | if (!sessionContainsApk(session)) { |
Mohammad Samiul Islam | 6dc9889 | 2019-09-13 11:10:12 +0100 | [diff] [blame] | 1477 | notifyPreRebootVerification_Apk_Complete(session.sessionId); |
Mohammad Samiul Islam | ef40027 | 2019-08-15 16:44:57 +0100 | [diff] [blame] | 1478 | return; |
| 1479 | } |
| 1480 | |
| 1481 | try { |
| 1482 | Slog.d(TAG, "Running a pre-reboot verification for APKs in session " |
| 1483 | + session.sessionId + " by performing a dry-run install"); |
| 1484 | |
Mohammad Samiul Islam | 8c041df | 2019-09-10 16:26:26 +0100 | [diff] [blame] | 1485 | // verifyApksInSession will notify the handler when APK verification is complete |
| 1486 | verifyApksInSession(session); |
Mohammad Samiul Islam | ef40027 | 2019-08-15 16:44:57 +0100 | [diff] [blame] | 1487 | // TODO(b/118865310): abort the session on apexd. |
| 1488 | } catch (PackageManagerException e) { |
| 1489 | session.setStagedSessionFailed(e.error, e.getMessage()); |
Mohammad Samiul Islam | a0623e2 | 2020-04-24 09:26:19 +0100 | [diff] [blame] | 1490 | onPreRebootVerificationComplete(session.sessionId); |
Mohammad Samiul Islam | ef40027 | 2019-08-15 16:44:57 +0100 | [diff] [blame] | 1491 | } |
| 1492 | } |
| 1493 | |
| 1494 | /** |
| 1495 | * Pre-reboot verification state for wrapping up: |
| 1496 | * <p><ul> |
| 1497 | * <li>enables rollback if required</li> |
| 1498 | * <li>marks session as ready</li> |
| 1499 | * </ul></p> |
| 1500 | */ |
| 1501 | private void handlePreRebootVerification_End(@NonNull PackageInstallerSession session) { |
Mohammad Samiul Islam | 7e9fdb0 | 2019-11-28 18:14:40 +0000 | [diff] [blame] | 1502 | // Before marking the session as ready, start checkpoint service if available |
| 1503 | try { |
| 1504 | IStorageManager storageManager = PackageHelper.getStorageManager(); |
| 1505 | if (storageManager.supportsCheckpoint()) { |
Gavin Corkery | ab2544c | 2020-02-04 08:50:53 +0000 | [diff] [blame] | 1506 | storageManager.startCheckpoint(2); |
Mohammad Samiul Islam | 7e9fdb0 | 2019-11-28 18:14:40 +0000 | [diff] [blame] | 1507 | } |
| 1508 | } catch (Exception e) { |
| 1509 | // Failed to get hold of StorageManager |
| 1510 | Slog.e(TAG, "Failed to get hold of StorageManager", e); |
| 1511 | session.setStagedSessionFailed(SessionInfo.STAGED_SESSION_UNKNOWN, |
| 1512 | "Failed to get hold of StorageManager"); |
Mohammad Samiul Islam | a0623e2 | 2020-04-24 09:26:19 +0100 | [diff] [blame] | 1513 | onPreRebootVerificationComplete(session.sessionId); |
Mohammad Samiul Islam | 7e9fdb0 | 2019-11-28 18:14:40 +0000 | [diff] [blame] | 1514 | return; |
| 1515 | } |
| 1516 | |
Mohammad Samiul Islam | a0623e2 | 2020-04-24 09:26:19 +0100 | [diff] [blame] | 1517 | // Stop pre-reboot verification before marking session ready. From this point on, if we |
| 1518 | // abandon the session then it will be cleaned up immediately. If session is abandoned |
| 1519 | // after this point, then even if for some reason system tries to install the session |
| 1520 | // or activate its apex, there won't be any files to work with as they will be cleaned |
| 1521 | // up by the system as part of abandonment. If session is abandoned before this point, |
| 1522 | // then the session is already destroyed and cannot be marked ready anymore. |
| 1523 | onPreRebootVerificationComplete(session.sessionId); |
| 1524 | |
Mohammad Samiul Islam | ef40027 | 2019-08-15 16:44:57 +0100 | [diff] [blame] | 1525 | // Proactively mark session as ready before calling apexd. Although this call order |
| 1526 | // looks counter-intuitive, this is the easiest way to ensure that session won't end up |
| 1527 | // in the inconsistent state: |
| 1528 | // - If device gets rebooted right before call to apexd, then apexd will never activate |
| 1529 | // apex files of this staged session. This will result in StagingManager failing |
| 1530 | // the session. |
| 1531 | // On the other hand, if the order of the calls was inverted (first call apexd, then |
| 1532 | // mark session as ready), then if a device gets rebooted right after the call to apexd, |
| 1533 | // only apex part of the train will be applied, leaving device in an inconsistent state. |
| 1534 | Slog.d(TAG, "Marking session " + session.sessionId + " as ready"); |
| 1535 | session.setStagedSessionReady(); |
Mohammad Samiul Islam | a0623e2 | 2020-04-24 09:26:19 +0100 | [diff] [blame] | 1536 | if (session.isStagedSessionReady()) { |
| 1537 | final boolean hasApex = sessionContainsApex(session); |
| 1538 | if (hasApex) { |
| 1539 | try { |
| 1540 | mApexManager.markStagedSessionReady(session.sessionId); |
| 1541 | } catch (PackageManagerException e) { |
| 1542 | session.setStagedSessionFailed(e.error, e.getMessage()); |
| 1543 | return; |
| 1544 | } |
| 1545 | } |
Mohammad Samiul Islam | ef40027 | 2019-08-15 16:44:57 +0100 | [diff] [blame] | 1546 | } |
| 1547 | } |
| 1548 | } |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 1549 | } |