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; |
Narayan Kamath | fcd4a04 | 2019-02-01 14:16:37 +0000 | [diff] [blame] | 24 | import android.content.Context; |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 25 | import android.content.IIntentReceiver; |
| 26 | import android.content.IIntentSender; |
| 27 | import android.content.Intent; |
| 28 | import android.content.IntentSender; |
Dario Freni | 2e8dffc | 2019-02-06 14:55:16 +0000 | [diff] [blame] | 29 | import android.content.pm.PackageInfo; |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 30 | import android.content.pm.PackageInstaller; |
Narayan Kamath | 9dfa674 | 2019-01-04 14:22:50 +0000 | [diff] [blame] | 31 | import android.content.pm.PackageInstaller.SessionInfo; |
| 32 | import android.content.pm.PackageManager; |
Nikita Ioffe | e0dbc98 | 2019-05-09 19:33:35 +0100 | [diff] [blame] | 33 | import android.content.pm.PackageParser; |
Narayan Kamath | 9dfa674 | 2019-01-04 14:22:50 +0000 | [diff] [blame] | 34 | import android.content.pm.PackageParser.PackageParserException; |
| 35 | import android.content.pm.PackageParser.SigningDetails; |
| 36 | import android.content.pm.PackageParser.SigningDetails.SignatureSchemeVersion; |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 37 | import android.content.pm.ParceledListSlice; |
Narayan Kamath | fcd4a04 | 2019-02-01 14:16:37 +0000 | [diff] [blame] | 38 | import android.content.rollback.IRollbackManager; |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 39 | import android.os.Bundle; |
Narayan Kamath | 94c93ab | 2019-01-04 10:47:00 +0000 | [diff] [blame] | 40 | import android.os.Handler; |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 41 | import android.os.IBinder; |
Mohammad Samiul Islam | ef40027 | 2019-08-15 16:44:57 +0100 | [diff] [blame] | 42 | import android.os.Looper; |
| 43 | import android.os.Message; |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 44 | import android.os.ParcelFileDescriptor; |
Nikita Ioffe | 39a6a5b | 2019-02-26 15:36:39 +0000 | [diff] [blame] | 45 | import android.os.PowerManager; |
Narayan Kamath | 9dfa674 | 2019-01-04 14:22:50 +0000 | [diff] [blame] | 46 | import android.os.RemoteException; |
| 47 | import android.os.ServiceManager; |
Nikita Ioffe | e0dbc98 | 2019-05-09 19:33:35 +0100 | [diff] [blame] | 48 | import android.util.IntArray; |
Narayan Kamath | 9dfa674 | 2019-01-04 14:22:50 +0000 | [diff] [blame] | 49 | import android.util.Slog; |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 50 | import android.util.SparseArray; |
Narayan Kamath | 9dfa674 | 2019-01-04 14:22:50 +0000 | [diff] [blame] | 51 | import android.util.apk.ApkSignatureVerifier; |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 52 | |
| 53 | import com.android.internal.annotations.GuardedBy; |
Narayan Kamath | 94c93ab | 2019-01-04 10:47:00 +0000 | [diff] [blame] | 54 | import com.android.internal.os.BackgroundThread; |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 55 | |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 56 | import java.io.File; |
| 57 | import java.io.IOException; |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 58 | import java.util.ArrayList; |
Dario Freni | 015f935 | 2019-01-14 21:56:17 +0000 | [diff] [blame] | 59 | import java.util.Arrays; |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 60 | import java.util.List; |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 61 | import java.util.concurrent.LinkedBlockingQueue; |
| 62 | import java.util.concurrent.TimeUnit; |
Mohammad Samiul Islam | ef40027 | 2019-08-15 16:44:57 +0100 | [diff] [blame] | 63 | import java.util.function.Consumer; |
Dario Freni | f141aab | 2019-02-04 14:48:30 +0000 | [diff] [blame] | 64 | import java.util.function.Predicate; |
Dario Freni | 015f935 | 2019-01-14 21:56:17 +0000 | [diff] [blame] | 65 | import java.util.stream.Collectors; |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 66 | |
| 67 | /** |
| 68 | * This class handles staged install sessions, i.e. install sessions that require packages to |
| 69 | * be installed only after a reboot. |
| 70 | */ |
| 71 | public class StagingManager { |
| 72 | |
| 73 | private static final String TAG = "StagingManager"; |
| 74 | |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 75 | private final PackageInstallerService mPi; |
Dario Freni | 2e8dffc | 2019-02-06 14:55:16 +0000 | [diff] [blame] | 76 | private final ApexManager mApexManager; |
Nikita Ioffe | 39a6a5b | 2019-02-26 15:36:39 +0000 | [diff] [blame] | 77 | private final PowerManager mPowerManager; |
Mohammad Samiul Islam | ef40027 | 2019-08-15 16:44:57 +0100 | [diff] [blame] | 78 | private final PreRebootVerificationHandler mPreRebootVerificationHandler; |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 79 | |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 80 | @GuardedBy("mStagedSessions") |
| 81 | private final SparseArray<PackageInstallerSession> mStagedSessions = new SparseArray<>(); |
| 82 | |
Nikita Ioffe | 1a7965e | 2019-03-12 22:32:41 +0000 | [diff] [blame] | 83 | StagingManager(PackageInstallerService pi, ApexManager am, Context context) { |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 84 | mPi = pi; |
Dario Freni | 2e8dffc | 2019-02-06 14:55:16 +0000 | [diff] [blame] | 85 | mApexManager = am; |
Nikita Ioffe | 39a6a5b | 2019-02-26 15:36:39 +0000 | [diff] [blame] | 86 | mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); |
Mohammad Samiul Islam | ef40027 | 2019-08-15 16:44:57 +0100 | [diff] [blame] | 87 | mPreRebootVerificationHandler = new PreRebootVerificationHandler( |
| 88 | BackgroundThread.get().getLooper()); |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | private void updateStoredSession(@NonNull PackageInstallerSession sessionInfo) { |
| 92 | synchronized (mStagedSessions) { |
| 93 | PackageInstallerSession storedSession = mStagedSessions.get(sessionInfo.sessionId); |
Narayan Kamath | 94c93ab | 2019-01-04 10:47:00 +0000 | [diff] [blame] | 94 | // storedSession might be null if a call to abortSession was made before the session |
| 95 | // is updated. |
| 96 | if (storedSession != null) { |
| 97 | mStagedSessions.put(sessionInfo.sessionId, sessionInfo); |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 98 | } |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
| 102 | ParceledListSlice<PackageInstaller.SessionInfo> getSessions() { |
| 103 | final List<PackageInstaller.SessionInfo> result = new ArrayList<>(); |
| 104 | synchronized (mStagedSessions) { |
| 105 | for (int i = 0; i < mStagedSessions.size(); i++) { |
| 106 | result.add(mStagedSessions.valueAt(i).generateInfo(false)); |
| 107 | } |
| 108 | } |
| 109 | return new ParceledListSlice<>(result); |
| 110 | } |
| 111 | |
Mohammad Samiul Islam | b884124 | 2019-07-29 14:38:38 +0100 | [diff] [blame] | 112 | /** |
| 113 | * Validates the signature used to sign the container of the new apex package |
| 114 | * |
| 115 | * @param newApexPkg The new apex package that is being installed |
Mohammad Samiul Islam | b884124 | 2019-07-29 14:38:38 +0100 | [diff] [blame] | 116 | * @throws PackageManagerException |
| 117 | */ |
Mohammad Samiul Islam | d7472d0 | 2019-11-11 17:53:47 +0000 | [diff] [blame] | 118 | private void validateApexSignature(PackageInfo newApexPkg) |
Nikita Ioffe | e0dbc98 | 2019-05-09 19:33:35 +0100 | [diff] [blame] | 119 | throws PackageManagerException { |
Mohammad Samiul Islam | b884124 | 2019-07-29 14:38:38 +0100 | [diff] [blame] | 120 | // Get signing details of the new package |
| 121 | final String apexPath = newApexPkg.applicationInfo.sourceDir; |
| 122 | final String packageName = newApexPkg.packageName; |
| 123 | |
Mohammad Samiul Islam | d7472d0 | 2019-11-11 17:53:47 +0000 | [diff] [blame] | 124 | final SigningDetails newSigningDetails; |
Narayan Kamath | 9dfa674 | 2019-01-04 14:22:50 +0000 | [diff] [blame] | 125 | try { |
Mohammad Samiul Islam | d7472d0 | 2019-11-11 17:53:47 +0000 | [diff] [blame] | 126 | newSigningDetails = ApkSignatureVerifier.verify(apexPath, SignatureSchemeVersion.JAR); |
Narayan Kamath | 9dfa674 | 2019-01-04 14:22:50 +0000 | [diff] [blame] | 127 | } catch (PackageParserException e) { |
Nikita Ioffe | e0dbc98 | 2019-05-09 19:33:35 +0100 | [diff] [blame] | 128 | throw new PackageManagerException(SessionInfo.STAGED_SESSION_VERIFICATION_FAILED, |
| 129 | "Failed to parse APEX package " + apexPath, e); |
Narayan Kamath | 9dfa674 | 2019-01-04 14:22:50 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Mohammad Samiul Islam | b884124 | 2019-07-29 14:38:38 +0100 | [diff] [blame] | 132 | // Get signing details of the existing package |
| 133 | final PackageInfo existingApexPkg = mApexManager.getPackageInfo(packageName, |
Nikita Ioffe | e0dbc98 | 2019-05-09 19:33:35 +0100 | [diff] [blame] | 134 | ApexManager.MATCH_ACTIVE_PACKAGE); |
Mohammad Samiul Islam | b884124 | 2019-07-29 14:38:38 +0100 | [diff] [blame] | 135 | if (existingApexPkg == null) { |
Nikita Ioffe | e0dbc98 | 2019-05-09 19:33:35 +0100 | [diff] [blame] | 136 | // This should never happen, because submitSessionToApexService ensures that no new |
| 137 | // apexes were installed. |
| 138 | throw new IllegalStateException("Unknown apex package " + packageName); |
Narayan Kamath | 9dfa674 | 2019-01-04 14:22:50 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | final SigningDetails existingSigningDetails; |
| 142 | try { |
| 143 | existingSigningDetails = ApkSignatureVerifier.verify( |
Mohammad Samiul Islam | b884124 | 2019-07-29 14:38:38 +0100 | [diff] [blame] | 144 | existingApexPkg.applicationInfo.sourceDir, SignatureSchemeVersion.JAR); |
Narayan Kamath | 9dfa674 | 2019-01-04 14:22:50 +0000 | [diff] [blame] | 145 | } catch (PackageParserException e) { |
Nikita Ioffe | e0dbc98 | 2019-05-09 19:33:35 +0100 | [diff] [blame] | 146 | throw new PackageManagerException(SessionInfo.STAGED_SESSION_VERIFICATION_FAILED, |
Mohammad Samiul Islam | b884124 | 2019-07-29 14:38:38 +0100 | [diff] [blame] | 147 | "Failed to parse APEX package " + existingApexPkg.applicationInfo.sourceDir, e); |
Narayan Kamath | 9dfa674 | 2019-01-04 14:22:50 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Mohammad Samiul Islam | b884124 | 2019-07-29 14:38:38 +0100 | [diff] [blame] | 150 | // Verify signing details for upgrade |
Mohammad Samiul Islam | d7472d0 | 2019-11-11 17:53:47 +0000 | [diff] [blame] | 151 | if (newSigningDetails.checkCapability(existingSigningDetails, |
| 152 | SigningDetails.CertCapabilities.INSTALLED_DATA) |
| 153 | || existingSigningDetails.checkCapability(newSigningDetails, |
| 154 | SigningDetails.CertCapabilities.ROLLBACK)) { |
Mohammad Samiul Islam | 8e35db1 | 2019-08-22 15:43:55 +0100 | [diff] [blame] | 155 | return; |
| 156 | } |
| 157 | |
Nikita Ioffe | e0dbc98 | 2019-05-09 19:33:35 +0100 | [diff] [blame] | 158 | throw new PackageManagerException(SessionInfo.STAGED_SESSION_VERIFICATION_FAILED, |
Mohammad Samiul Islam | b884124 | 2019-07-29 14:38:38 +0100 | [diff] [blame] | 159 | "APK-container signature of APEX package " + packageName + " with version " |
| 160 | + newApexPkg.versionCodeMajor + " and path " + apexPath + " is not" |
| 161 | + " compatible with the one currently installed on device"); |
Narayan Kamath | 9dfa674 | 2019-01-04 14:22:50 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Nikita Ioffe | e0dbc98 | 2019-05-09 19:33:35 +0100 | [diff] [blame] | 164 | private List<PackageInfo> submitSessionToApexService( |
| 165 | @NonNull PackageInstallerSession session) throws PackageManagerException { |
| 166 | final IntArray childSessionsIds = new IntArray(); |
| 167 | if (session.isMultiPackage()) { |
| 168 | for (int id : session.getChildSessionIds()) { |
| 169 | if (isApexSession(mStagedSessions.get(id))) { |
| 170 | childSessionsIds.add(id); |
| 171 | } |
Nikita Ioffe | 8bbb815 | 2019-02-21 15:54:54 +0000 | [diff] [blame] | 172 | } |
Nikita Ioffe | e0dbc98 | 2019-05-09 19:33:35 +0100 | [diff] [blame] | 173 | } |
Nikita Ioffe | 4e7d24a | 2019-07-05 15:49:45 +0100 | [diff] [blame] | 174 | // submitStagedSession will throw a PackageManagerException if apexd verification fails, |
| 175 | // which will be propagated to populate stagedSessionErrorMessage of this session. |
| 176 | final ApexInfoList apexInfoList = mApexManager.submitStagedSession(session.sessionId, |
| 177 | childSessionsIds.toArray()); |
Nikita Ioffe | e0dbc98 | 2019-05-09 19:33:35 +0100 | [diff] [blame] | 178 | final List<PackageInfo> result = new ArrayList<>(); |
Oli Lan | c2c7a22 | 2019-07-31 15:27:22 +0100 | [diff] [blame] | 179 | for (ApexInfo apexInfo : apexInfoList.apexInfos) { |
| 180 | final PackageInfo packageInfo; |
| 181 | int flags = PackageManager.GET_META_DATA; |
| 182 | PackageParser.Package pkg; |
Nikita Ioffe | e0dbc98 | 2019-05-09 19:33:35 +0100 | [diff] [blame] | 183 | try { |
Oli Lan | c2c7a22 | 2019-07-31 15:27:22 +0100 | [diff] [blame] | 184 | File apexFile = new File(apexInfo.modulePath); |
| 185 | PackageParser pp = new PackageParser(); |
| 186 | pkg = pp.parsePackage(apexFile, flags, false); |
Nikita Ioffe | e0dbc98 | 2019-05-09 19:33:35 +0100 | [diff] [blame] | 187 | } catch (PackageParserException e) { |
| 188 | throw new PackageManagerException(SessionInfo.STAGED_SESSION_VERIFICATION_FAILED, |
Oli Lan | c2c7a22 | 2019-07-31 15:27:22 +0100 | [diff] [blame] | 189 | "Failed to parse APEX package " + apexInfo.modulePath, e); |
Nikita Ioffe | e0dbc98 | 2019-05-09 19:33:35 +0100 | [diff] [blame] | 190 | } |
Oli Lan | c2c7a22 | 2019-07-31 15:27:22 +0100 | [diff] [blame] | 191 | packageInfo = PackageParser.generatePackageInfo(pkg, apexInfo, flags); |
| 192 | final PackageInfo activePackage = mApexManager.getPackageInfo(packageInfo.packageName, |
Nikita Ioffe | e0dbc98 | 2019-05-09 19:33:35 +0100 | [diff] [blame] | 193 | ApexManager.MATCH_ACTIVE_PACKAGE); |
| 194 | if (activePackage == null) { |
Oli Lan | c2c7a22 | 2019-07-31 15:27:22 +0100 | [diff] [blame] | 195 | Slog.w(TAG, "Attempting to install new APEX package " + packageInfo.packageName); |
Nikita Ioffe | e0dbc98 | 2019-05-09 19:33:35 +0100 | [diff] [blame] | 196 | throw new PackageManagerException(SessionInfo.STAGED_SESSION_VERIFICATION_FAILED, |
| 197 | "It is forbidden to install new APEX packages."); |
| 198 | } |
| 199 | checkRequiredVersionCode(session, activePackage); |
Oli Lan | c2c7a22 | 2019-07-31 15:27:22 +0100 | [diff] [blame] | 200 | checkDowngrade(session, activePackage, packageInfo); |
| 201 | result.add(packageInfo); |
Nikita Ioffe | e0dbc98 | 2019-05-09 19:33:35 +0100 | [diff] [blame] | 202 | } |
Nikita Ioffe | f2c2e01 | 2019-07-23 17:48:38 +0100 | [diff] [blame] | 203 | Slog.d(TAG, "Session " + session.sessionId + " has following APEX packages: [" |
| 204 | + result.stream().map(p -> p.packageName).collect(Collectors.joining(",")) + "]"); |
Nikita Ioffe | e0dbc98 | 2019-05-09 19:33:35 +0100 | [diff] [blame] | 205 | return result; |
| 206 | } |
| 207 | |
| 208 | private void checkRequiredVersionCode(final PackageInstallerSession session, |
| 209 | final PackageInfo activePackage) throws PackageManagerException { |
| 210 | if (session.params.requiredInstalledVersionCode == PackageManager.VERSION_CODE_HIGHEST) { |
| 211 | return; |
| 212 | } |
| 213 | final long activeVersion = activePackage.applicationInfo.longVersionCode; |
| 214 | if (activeVersion != session.params.requiredInstalledVersionCode) { |
| 215 | if (!mApexManager.abortActiveSession()) { |
| 216 | Slog.e(TAG, "Failed to abort apex session " + session.sessionId); |
| 217 | } |
| 218 | throw new PackageManagerException( |
| 219 | SessionInfo.STAGED_SESSION_VERIFICATION_FAILED, |
| 220 | "Installed version of APEX package " + activePackage.packageName |
Richard Uhler | 2124d4b | 2019-04-25 13:01:39 +0100 | [diff] [blame] | 221 | + " does not match required. Active version: " + activeVersion |
| 222 | + " required: " + session.params.requiredInstalledVersionCode); |
Nikita Ioffe | 8bbb815 | 2019-02-21 15:54:54 +0000 | [diff] [blame] | 223 | } |
Nikita Ioffe | e0dbc98 | 2019-05-09 19:33:35 +0100 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | private void checkDowngrade(final PackageInstallerSession session, |
| 227 | final PackageInfo activePackage, final PackageInfo newPackage) |
| 228 | throws PackageManagerException { |
| 229 | final long activeVersion = activePackage.applicationInfo.longVersionCode; |
| 230 | final long newVersionCode = newPackage.applicationInfo.longVersionCode; |
Nikita Ioffe | f2c2e01 | 2019-07-23 17:48:38 +0100 | [diff] [blame] | 231 | final boolean allowsDowngrade = PackageManagerServiceUtils.isDowngradePermitted( |
Nikita Ioffe | e0dbc98 | 2019-05-09 19:33:35 +0100 | [diff] [blame] | 232 | session.params.installFlags, activePackage.applicationInfo.flags); |
| 233 | if (activeVersion > newVersionCode && !allowsDowngrade) { |
| 234 | if (!mApexManager.abortActiveSession()) { |
| 235 | Slog.e(TAG, "Failed to abort apex session " + session.sessionId); |
| 236 | } |
| 237 | throw new PackageManagerException( |
| 238 | SessionInfo.STAGED_SESSION_VERIFICATION_FAILED, |
| 239 | "Downgrade of APEX package " + newPackage.packageName |
| 240 | + " is not allowed. Active version: " + activeVersion |
| 241 | + " attempted: " + newVersionCode); |
| 242 | } |
Dario Freni | 015f935 | 2019-01-14 21:56:17 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Dario Freni | 015f935 | 2019-01-14 21:56:17 +0000 | [diff] [blame] | 245 | private static boolean isApexSession(@NonNull PackageInstallerSession session) { |
| 246 | return (session.params.installFlags & PackageManager.INSTALL_APEX) != 0; |
| 247 | } |
| 248 | |
Dario Freni | f141aab | 2019-02-04 14:48:30 +0000 | [diff] [blame] | 249 | private boolean sessionContains(@NonNull PackageInstallerSession session, |
| 250 | Predicate<PackageInstallerSession> filter) { |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 251 | if (!session.isMultiPackage()) { |
Dario Freni | f141aab | 2019-02-04 14:48:30 +0000 | [diff] [blame] | 252 | return filter.test(session); |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 253 | } |
| 254 | synchronized (mStagedSessions) { |
| 255 | return !(Arrays.stream(session.getChildSessionIds()) |
| 256 | // Retrieve cached sessions matching ids. |
| 257 | .mapToObj(i -> mStagedSessions.get(i)) |
| 258 | // Filter only the ones containing APEX. |
Dario Freni | f141aab | 2019-02-04 14:48:30 +0000 | [diff] [blame] | 259 | .filter(childSession -> filter.test(childSession)) |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 260 | .collect(Collectors.toList()) |
| 261 | .isEmpty()); |
| 262 | } |
| 263 | } |
| 264 | |
Dario Freni | f141aab | 2019-02-04 14:48:30 +0000 | [diff] [blame] | 265 | private boolean sessionContainsApex(@NonNull PackageInstallerSession session) { |
| 266 | return sessionContains(session, (s) -> isApexSession(s)); |
| 267 | } |
| 268 | |
| 269 | private boolean sessionContainsApk(@NonNull PackageInstallerSession session) { |
| 270 | return sessionContains(session, (s) -> !isApexSession(s)); |
| 271 | } |
| 272 | |
Dario Freni | 61b3e25 | 2019-01-11 23:05:39 +0000 | [diff] [blame] | 273 | private void resumeSession(@NonNull PackageInstallerSession session) { |
Nikita Ioffe | f2c2e01 | 2019-07-23 17:48:38 +0100 | [diff] [blame] | 274 | Slog.d(TAG, "Resuming session " + session.sessionId); |
Nikita Ioffe | c2b87a9 | 2019-07-15 13:28:11 +0100 | [diff] [blame] | 275 | final boolean hasApex = sessionContainsApex(session); |
Nikita Ioffe | a820bd9 | 2019-02-15 14:22:44 +0000 | [diff] [blame] | 276 | if (hasApex) { |
Dario Freni | 2e8dffc | 2019-02-06 14:55:16 +0000 | [diff] [blame] | 277 | // Check with apexservice whether the apex packages have been activated. |
| 278 | ApexSessionInfo apexSessionInfo = mApexManager.getStagedSessionInfo(session.sessionId); |
| 279 | if (apexSessionInfo == null) { |
| 280 | session.setStagedSessionFailed(SessionInfo.STAGED_SESSION_ACTIVATION_FAILED, |
| 281 | "apexd did not know anything about a staged session supposed to be" |
| 282 | + "activated"); |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 283 | return; |
| 284 | } |
Nikita Ioffe | 8274222 | 2019-02-26 12:14:04 +0000 | [diff] [blame] | 285 | if (isApexSessionFailed(apexSessionInfo)) { |
Dario Freni | b6d2896 | 2019-01-31 15:52:24 +0000 | [diff] [blame] | 286 | session.setStagedSessionFailed(SessionInfo.STAGED_SESSION_ACTIVATION_FAILED, |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 287 | "APEX activation failed. Check logcat messages from apexd for " |
| 288 | + "more information."); |
| 289 | return; |
| 290 | } |
| 291 | if (apexSessionInfo.isVerified) { |
| 292 | // Session has been previously submitted to apexd, but didn't complete all the |
| 293 | // pre-reboot verification, perhaps because the device rebooted in the meantime. |
| 294 | // Greedily re-trigger the pre-reboot verification. |
| 295 | Slog.d(TAG, "Found pending staged session " + session.sessionId + " still to be " |
| 296 | + "verified, resuming pre-reboot verification"); |
Mohammad Samiul Islam | 6dc9889 | 2019-09-13 11:10:12 +0100 | [diff] [blame] | 297 | mPreRebootVerificationHandler.startPreRebootVerification(session.sessionId); |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 298 | return; |
| 299 | } |
Nikita Ioffe | a820bd9 | 2019-02-15 14:22:44 +0000 | [diff] [blame] | 300 | if (!apexSessionInfo.isActivated && !apexSessionInfo.isSuccess) { |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 301 | // In all the remaining cases apexd will try to apply the session again at next |
| 302 | // boot. Nothing to do here for now. |
| 303 | Slog.w(TAG, "Staged session " + session.sessionId + " scheduled to be applied " |
| 304 | + "at boot didn't activate nor fail. This usually means that apexd will " |
| 305 | + "retry at next reboot."); |
| 306 | return; |
| 307 | } |
Nikita Ioffe | f2c2e01 | 2019-07-23 17:48:38 +0100 | [diff] [blame] | 308 | Slog.i(TAG, "APEX packages in session " + session.sessionId |
| 309 | + " were successfully activated. Proceeding with APK packages, if any"); |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 310 | } |
| 311 | // The APEX part of the session is activated, proceed with the installation of APKs. |
Nikita Ioffe | c2b87a9 | 2019-07-15 13:28:11 +0100 | [diff] [blame] | 312 | try { |
Nikita Ioffe | f2c2e01 | 2019-07-23 17:48:38 +0100 | [diff] [blame] | 313 | Slog.d(TAG, "Installing APK packages in session " + session.sessionId); |
Mohammad Samiul Islam | 8c041df | 2019-09-10 16:26:26 +0100 | [diff] [blame] | 314 | installApksInSession(session); |
Nikita Ioffe | c2b87a9 | 2019-07-15 13:28:11 +0100 | [diff] [blame] | 315 | } catch (PackageManagerException e) { |
| 316 | session.setStagedSessionFailed(e.error, e.getMessage()); |
Nikita Ioffe | 39a6a5b | 2019-02-26 15:36:39 +0000 | [diff] [blame] | 317 | |
| 318 | if (!hasApex) { |
| 319 | return; |
| 320 | } |
| 321 | |
| 322 | if (!mApexManager.abortActiveSession()) { |
| 323 | Slog.e(TAG, "Failed to abort APEXd session"); |
| 324 | } else { |
| 325 | Slog.e(TAG, |
| 326 | "Successfully aborted apexd session. Rebooting device in order to revert " |
| 327 | + "to the previous state of APEXd."); |
| 328 | mPowerManager.reboot(null); |
| 329 | } |
Dario Freni | 61b3e25 | 2019-01-11 23:05:39 +0000 | [diff] [blame] | 330 | return; |
| 331 | } |
Nikita Ioffe | a820bd9 | 2019-02-15 14:22:44 +0000 | [diff] [blame] | 332 | |
Nikita Ioffe | f2c2e01 | 2019-07-23 17:48:38 +0100 | [diff] [blame] | 333 | Slog.d(TAG, "Marking session " + session.sessionId + " as applied"); |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 334 | session.setStagedSessionApplied(); |
Nikita Ioffe | a820bd9 | 2019-02-15 14:22:44 +0000 | [diff] [blame] | 335 | if (hasApex) { |
| 336 | mApexManager.markStagedSessionSuccessful(session.sessionId); |
| 337 | } |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 338 | } |
| 339 | |
Dario Freni | 815bd21 | 2019-02-20 14:09:36 +0000 | [diff] [blame] | 340 | private List<String> findAPKsInDir(File stageDir) { |
| 341 | List<String> ret = new ArrayList<>(); |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 342 | if (stageDir != null && stageDir.exists()) { |
| 343 | for (File file : stageDir.listFiles()) { |
| 344 | if (file.getAbsolutePath().toLowerCase().endsWith(".apk")) { |
Dario Freni | 815bd21 | 2019-02-20 14:09:36 +0000 | [diff] [blame] | 345 | ret.add(file.getAbsolutePath()); |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 346 | } |
| 347 | } |
Dario Freni | 61b3e25 | 2019-01-11 23:05:39 +0000 | [diff] [blame] | 348 | } |
Dario Freni | 815bd21 | 2019-02-20 14:09:36 +0000 | [diff] [blame] | 349 | return ret; |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Nikita Ioffe | c2b87a9 | 2019-07-15 13:28:11 +0100 | [diff] [blame] | 352 | @NonNull |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 353 | private PackageInstallerSession createAndWriteApkSession( |
Nikita Ioffe | c2b87a9 | 2019-07-15 13:28:11 +0100 | [diff] [blame] | 354 | @NonNull PackageInstallerSession originalSession, boolean preReboot) |
| 355 | throws PackageManagerException { |
| 356 | final int errorCode = preReboot ? SessionInfo.STAGED_SESSION_VERIFICATION_FAILED |
| 357 | : SessionInfo.STAGED_SESSION_ACTIVATION_FAILED; |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 358 | if (originalSession.stageDir == null) { |
| 359 | 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] | 360 | throw new PackageManagerException(errorCode, |
| 361 | "Attempting to install a staged APK session with no staging dir"); |
Dario Freni | 1473bcb | 2019-01-25 14:27:13 +0000 | [diff] [blame] | 362 | } |
Dario Freni | 815bd21 | 2019-02-20 14:09:36 +0000 | [diff] [blame] | 363 | List<String> apkFilePaths = findAPKsInDir(originalSession.stageDir); |
| 364 | if (apkFilePaths.isEmpty()) { |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 365 | Slog.w(TAG, "Can't find staged APK in " + originalSession.stageDir.getAbsolutePath()); |
Nikita Ioffe | c2b87a9 | 2019-07-15 13:28:11 +0100 | [diff] [blame] | 366 | throw new PackageManagerException(errorCode, |
| 367 | "Can't find staged APK in " + originalSession.stageDir.getAbsolutePath()); |
Dario Freni | 61b3e25 | 2019-01-11 23:05:39 +0000 | [diff] [blame] | 368 | } |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 369 | |
| 370 | PackageInstaller.SessionParams params = originalSession.params.copy(); |
| 371 | params.isStaged = false; |
Dario Freni | a2afebf | 2019-05-01 16:03:17 +0100 | [diff] [blame] | 372 | params.installFlags |= PackageManager.INSTALL_STAGED; |
Dario Freni | c3e68ea | 2019-04-02 11:45:13 +0100 | [diff] [blame] | 373 | // TODO(b/129744602): use the userid from the original session. |
Dario Freni | f141aab | 2019-02-04 14:48:30 +0000 | [diff] [blame] | 374 | if (preReboot) { |
| 375 | params.installFlags &= ~PackageManager.INSTALL_ENABLE_ROLLBACK; |
| 376 | params.installFlags |= PackageManager.INSTALL_DRY_RUN; |
| 377 | } else { |
| 378 | params.installFlags |= PackageManager.INSTALL_DISABLE_VERIFICATION; |
| 379 | } |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 380 | int apkSessionId = mPi.createSession( |
Dario Freni | c3e68ea | 2019-04-02 11:45:13 +0100 | [diff] [blame] | 381 | params, originalSession.getInstallerPackageName(), |
| 382 | 0 /* UserHandle.SYSTEM */); |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 383 | PackageInstallerSession apkSession = mPi.getSession(apkSessionId); |
| 384 | |
| 385 | try { |
| 386 | apkSession.open(); |
Dario Freni | 815bd21 | 2019-02-20 14:09:36 +0000 | [diff] [blame] | 387 | for (String apkFilePath : apkFilePaths) { |
| 388 | File apkFile = new File(apkFilePath); |
| 389 | ParcelFileDescriptor pfd = ParcelFileDescriptor.open(apkFile, |
| 390 | ParcelFileDescriptor.MODE_READ_ONLY); |
| 391 | long sizeBytes = pfd.getStatSize(); |
| 392 | if (sizeBytes < 0) { |
| 393 | Slog.e(TAG, "Unable to get size of: " + apkFilePath); |
Nikita Ioffe | c2b87a9 | 2019-07-15 13:28:11 +0100 | [diff] [blame] | 394 | throw new PackageManagerException(errorCode, |
| 395 | "Unable to get size of: " + apkFilePath); |
Dario Freni | 815bd21 | 2019-02-20 14:09:36 +0000 | [diff] [blame] | 396 | } |
| 397 | apkSession.write(apkFile.getName(), 0, sizeBytes, pfd); |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 398 | } |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 399 | } catch (IOException e) { |
| 400 | Slog.e(TAG, "Failure to install APK staged session " + originalSession.sessionId, e); |
Nikita Ioffe | c2b87a9 | 2019-07-15 13:28:11 +0100 | [diff] [blame] | 401 | throw new PackageManagerException(errorCode, "Failed to write APK session", e); |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 402 | } |
| 403 | return apkSession; |
| 404 | } |
| 405 | |
Mohammad Samiul Islam | 8c041df | 2019-09-10 16:26:26 +0100 | [diff] [blame] | 406 | /** |
| 407 | * Extract apks in the given session into a new session. Returns {@code null} if there is no |
| 408 | * apks in the given session. Only parent session is returned for multi-package session. |
| 409 | */ |
| 410 | @Nullable |
| 411 | private PackageInstallerSession extractApksInSession(PackageInstallerSession session, |
| 412 | boolean preReboot) throws PackageManagerException { |
Nikita Ioffe | c2b87a9 | 2019-07-15 13:28:11 +0100 | [diff] [blame] | 413 | final int errorCode = preReboot ? SessionInfo.STAGED_SESSION_VERIFICATION_FAILED |
| 414 | : SessionInfo.STAGED_SESSION_ACTIVATION_FAILED; |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 415 | if (!session.isMultiPackage() && !isApexSession(session)) { |
Mohammad Samiul Islam | 8c041df | 2019-09-10 16:26:26 +0100 | [diff] [blame] | 416 | return createAndWriteApkSession(session, preReboot); |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 417 | } else if (session.isMultiPackage()) { |
| 418 | // For multi-package staged sessions containing APKs, we identify which child sessions |
| 419 | // contain an APK, and with those then create a new multi-package group of sessions, |
| 420 | // carrying over all the session parameters and unmarking them as staged. On commit the |
| 421 | // sessions will be installed atomically. |
Mohammad Samiul Islam | 8c041df | 2019-09-10 16:26:26 +0100 | [diff] [blame] | 422 | final List<PackageInstallerSession> childSessions; |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 423 | synchronized (mStagedSessions) { |
| 424 | childSessions = |
| 425 | Arrays.stream(session.getChildSessionIds()) |
| 426 | // Retrieve cached sessions matching ids. |
| 427 | .mapToObj(i -> mStagedSessions.get(i)) |
| 428 | // Filter only the ones containing APKs.s |
| 429 | .filter(childSession -> !isApexSession(childSession)) |
| 430 | .collect(Collectors.toList()); |
| 431 | } |
| 432 | if (childSessions.isEmpty()) { |
| 433 | // APEX-only multi-package staged session, nothing to do. |
Mohammad Samiul Islam | 8c041df | 2019-09-10 16:26:26 +0100 | [diff] [blame] | 434 | return null; |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 435 | } |
Mohammad Samiul Islam | 8c041df | 2019-09-10 16:26:26 +0100 | [diff] [blame] | 436 | final PackageInstaller.SessionParams params = session.params.copy(); |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 437 | params.isStaged = false; |
Dario Freni | f141aab | 2019-02-04 14:48:30 +0000 | [diff] [blame] | 438 | if (preReboot) { |
| 439 | params.installFlags &= ~PackageManager.INSTALL_ENABLE_ROLLBACK; |
| 440 | } |
Dario Freni | c3e68ea | 2019-04-02 11:45:13 +0100 | [diff] [blame] | 441 | // TODO(b/129744602): use the userid from the original session. |
Mohammad Samiul Islam | 8c041df | 2019-09-10 16:26:26 +0100 | [diff] [blame] | 442 | final int apkParentSessionId = mPi.createSession( |
Dario Freni | c3e68ea | 2019-04-02 11:45:13 +0100 | [diff] [blame] | 443 | params, session.getInstallerPackageName(), |
| 444 | 0 /* UserHandle.SYSTEM */); |
Mohammad Samiul Islam | 8c041df | 2019-09-10 16:26:26 +0100 | [diff] [blame] | 445 | final PackageInstallerSession apkParentSession = mPi.getSession(apkParentSessionId); |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 446 | try { |
| 447 | apkParentSession.open(); |
| 448 | } catch (IOException e) { |
| 449 | Slog.e(TAG, "Unable to prepare multi-package session for staged session " |
| 450 | + session.sessionId); |
Nikita Ioffe | c2b87a9 | 2019-07-15 13:28:11 +0100 | [diff] [blame] | 451 | throw new PackageManagerException(errorCode, |
| 452 | "Unable to prepare multi-package session for staged session"); |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | for (PackageInstallerSession sessionToClone : childSessions) { |
Dario Freni | f141aab | 2019-02-04 14:48:30 +0000 | [diff] [blame] | 456 | PackageInstallerSession apkChildSession = |
| 457 | createAndWriteApkSession(sessionToClone, preReboot); |
Dario Freni | 0966047 | 2019-02-18 16:44:23 +0000 | [diff] [blame] | 458 | try { |
| 459 | apkParentSession.addChildSessionId(apkChildSession.sessionId); |
Patrick Baumann | 00321b7 | 2019-04-09 15:07:25 -0700 | [diff] [blame] | 460 | } catch (IllegalStateException e) { |
Dario Freni | 0966047 | 2019-02-18 16:44:23 +0000 | [diff] [blame] | 461 | 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] | 462 | throw new PackageManagerException(errorCode, |
| 463 | "Failed to add a child session " + apkChildSession.sessionId); |
Dario Freni | 0966047 | 2019-02-18 16:44:23 +0000 | [diff] [blame] | 464 | } |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 465 | } |
Mohammad Samiul Islam | 8c041df | 2019-09-10 16:26:26 +0100 | [diff] [blame] | 466 | return apkParentSession; |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 467 | } |
Mohammad Samiul Islam | 8c041df | 2019-09-10 16:26:26 +0100 | [diff] [blame] | 468 | return null; |
| 469 | } |
| 470 | |
| 471 | private void verifyApksInSession(PackageInstallerSession session) |
| 472 | throws PackageManagerException { |
| 473 | |
| 474 | final PackageInstallerSession apksToVerify = extractApksInSession( |
| 475 | session, /* preReboot */ true); |
| 476 | if (apksToVerify == null) { |
| 477 | return; |
| 478 | } |
| 479 | |
| 480 | final LocalIntentReceiverAsync receiver = new LocalIntentReceiverAsync( |
| 481 | (Intent result) -> { |
| 482 | int status = result.getIntExtra(PackageInstaller.EXTRA_STATUS, |
| 483 | PackageInstaller.STATUS_FAILURE); |
| 484 | if (status != PackageInstaller.STATUS_SUCCESS) { |
| 485 | final String errorMessage = result.getStringExtra( |
| 486 | PackageInstaller.EXTRA_STATUS_MESSAGE); |
| 487 | Slog.e(TAG, "Failure to verify APK staged session " |
| 488 | + session.sessionId + " [" + errorMessage + "]"); |
| 489 | session.setStagedSessionFailed( |
| 490 | SessionInfo.STAGED_SESSION_VERIFICATION_FAILED, errorMessage); |
| 491 | return; |
| 492 | } |
| 493 | mPreRebootVerificationHandler.notifyPreRebootVerification_Apk_Complete( |
| 494 | session.sessionId); |
| 495 | }); |
| 496 | |
| 497 | apksToVerify.commit(receiver.getIntentSender(), false); |
| 498 | } |
| 499 | |
| 500 | private void installApksInSession(@NonNull PackageInstallerSession session) |
| 501 | throws PackageManagerException { |
| 502 | |
| 503 | final PackageInstallerSession apksToInstall = extractApksInSession( |
| 504 | session, /* preReboot */ false); |
| 505 | if (apksToInstall == null) { |
| 506 | return; |
| 507 | } |
| 508 | |
| 509 | if ((apksToInstall.params.installFlags & PackageManager.INSTALL_ENABLE_ROLLBACK) != 0) { |
| 510 | // If rollback is available for this session, notify the rollback |
| 511 | // manager of the apk session so it can properly enable rollback. |
| 512 | final IRollbackManager rm = IRollbackManager.Stub.asInterface( |
| 513 | ServiceManager.getService(Context.ROLLBACK_SERVICE)); |
| 514 | try { |
| 515 | rm.notifyStagedApkSession(session.sessionId, apksToInstall.sessionId); |
| 516 | } catch (RemoteException re) { |
| 517 | Slog.e(TAG, "Failed to notifyStagedApkSession for session: " |
| 518 | + session.sessionId, re); |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | final LocalIntentReceiverSync receiver = new LocalIntentReceiverSync(); |
| 523 | apksToInstall.commit(receiver.getIntentSender(), false); |
| 524 | final Intent result = receiver.getResult(); |
| 525 | final int status = result.getIntExtra(PackageInstaller.EXTRA_STATUS, |
| 526 | PackageInstaller.STATUS_FAILURE); |
| 527 | if (status != PackageInstaller.STATUS_SUCCESS) { |
| 528 | final String errorMessage = result.getStringExtra( |
| 529 | PackageInstaller.EXTRA_STATUS_MESSAGE); |
| 530 | Slog.e(TAG, "Failure to install APK staged session " |
| 531 | + session.sessionId + " [" + errorMessage + "]"); |
| 532 | throw new PackageManagerException( |
| 533 | SessionInfo.STAGED_SESSION_ACTIVATION_FAILED, errorMessage); |
| 534 | } |
Dario Freni | 61b3e25 | 2019-01-11 23:05:39 +0000 | [diff] [blame] | 535 | } |
| 536 | |
Nikita Ioffe | 8e5b703c | 2019-03-14 18:40:17 +0000 | [diff] [blame] | 537 | void commitSession(@NonNull PackageInstallerSession session) { |
Dario Freni | 276cd07 | 2019-01-09 11:13:44 +0000 | [diff] [blame] | 538 | updateStoredSession(session); |
Mohammad Samiul Islam | 6dc9889 | 2019-09-13 11:10:12 +0100 | [diff] [blame] | 539 | mPreRebootVerificationHandler.startPreRebootVerification(session.sessionId); |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 540 | } |
| 541 | |
Mohammad Samiul Islam | da00497 | 2019-10-09 11:29:26 +0100 | [diff] [blame] | 542 | /** |
| 543 | * <p> Check if the session provided is non-overlapping with the active staged sessions. |
| 544 | * |
| 545 | * <p> A session is non-overlapping if it meets one of the following conditions: </p> |
| 546 | * <ul> |
| 547 | * <li>It is a parent session</li> |
| 548 | * <li>It is already one of the active sessions</li> |
| 549 | * <li>Its package name is not same as any of the active sessions</li> |
| 550 | * </ul> |
| 551 | * @throws PackageManagerException if session fails the check |
| 552 | */ |
| 553 | void checkNonOverlappingWithStagedSessions(@NonNull PackageInstallerSession session) |
| 554 | throws PackageManagerException { |
| 555 | if (session.isMultiPackage()) { |
| 556 | // We cannot say a parent session overlaps until we process its children |
| 557 | return; |
| 558 | } |
| 559 | if (session.getPackageName() == null) { |
| 560 | throw new PackageManagerException(PackageManager.INSTALL_FAILED_INVALID_APK, |
| 561 | "Cannot stage session " + session.sessionId + " with package name null"); |
| 562 | } |
| 563 | |
Nikita Ioffe | da998cf | 2019-03-04 22:54:30 +0000 | [diff] [blame] | 564 | synchronized (mStagedSessions) { |
| 565 | for (int i = 0; i < mStagedSessions.size(); i++) { |
Mohammad Samiul Islam | da00497 | 2019-10-09 11:29:26 +0100 | [diff] [blame] | 566 | final PackageInstallerSession stagedSession = mStagedSessions.valueAt(i); |
| 567 | if (!stagedSession.isCommitted() || stagedSession.isStagedAndInTerminalState()) { |
Nikita Ioffe | da998cf | 2019-03-04 22:54:30 +0000 | [diff] [blame] | 568 | continue; |
| 569 | } |
Mohammad Samiul Islam | da00497 | 2019-10-09 11:29:26 +0100 | [diff] [blame] | 570 | if (stagedSession.isMultiPackage()) { |
| 571 | // This active parent staged session is useless as it doesn't have a package |
| 572 | // 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] | 573 | continue; |
| 574 | } |
Mohammad Samiul Islam | da00497 | 2019-10-09 11:29:26 +0100 | [diff] [blame] | 575 | |
| 576 | // From here on, stagedSession is a non-parent active staged session |
| 577 | |
| 578 | // Check if stagedSession has an active parent session or not |
| 579 | if (stagedSession.hasParentSessionId()) { |
| 580 | int parentId = stagedSession.getParentSessionId(); |
| 581 | PackageInstallerSession parentSession = mStagedSessions.get(parentId); |
| 582 | if (parentSession == null || parentSession.isStagedAndInTerminalState()) { |
| 583 | // Parent session has been abandoned or terminated already |
| 584 | continue; |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | // Check if session is one of the active sessions |
| 589 | if (session.sessionId == stagedSession.sessionId) { |
| 590 | Slog.w(TAG, "Session " + session.sessionId + " is already staged"); |
| 591 | continue; |
| 592 | } |
| 593 | |
| 594 | // If session is not among the active sessions, then it cannot have same package |
| 595 | // name as any of the active sessions. |
| 596 | if (session.getPackageName().equals(stagedSession.getPackageName())) { |
| 597 | throw new PackageManagerException( |
| 598 | PackageManager.INSTALL_FAILED_OTHER_STAGED_SESSION_IN_PROGRESS, |
| 599 | "Package: " + session.getPackageName() + " in session: " |
| 600 | + session.sessionId + " has been staged already by session: " |
| 601 | + stagedSession.sessionId, null); |
| 602 | } |
| 603 | |
| 604 | // TODO(b/141843321): Add support for staging multiple sessions in apexd |
| 605 | // Since apexd doesn't support multiple staged sessions yet, we have to careful how |
| 606 | // we handle apex sessions. We want to allow a set of apex sessions under the same |
| 607 | // parent to be staged when there is no previously staged apex sessions. |
| 608 | if (isApexSession(session) && isApexSession(stagedSession)) { |
| 609 | // session is apex and it can co-exist with stagedSession only if they are from |
| 610 | // same parent |
| 611 | final boolean coExist; |
| 612 | if (!session.hasParentSessionId() && !stagedSession.hasParentSessionId()) { |
| 613 | // Both single package apex sessions. Cannot co-exist. |
| 614 | coExist = false; |
| 615 | } else { |
| 616 | // At least one of the session has parent. Both must be from same parent. |
| 617 | coExist = |
| 618 | session.getParentSessionId() == stagedSession.getParentSessionId(); |
| 619 | } |
| 620 | if (!coExist) { |
| 621 | throw new PackageManagerException( |
| 622 | PackageManager.INSTALL_FAILED_OTHER_STAGED_SESSION_IN_PROGRESS, |
| 623 | "Package: " + session.getPackageName() + " in session: " |
| 624 | + session.sessionId + " cannot be staged as there is " |
| 625 | + "already another apex staged session: " |
| 626 | + stagedSession.sessionId, null); |
| 627 | } |
Nikita Ioffe | da998cf | 2019-03-04 22:54:30 +0000 | [diff] [blame] | 628 | } |
| 629 | } |
| 630 | } |
Nikita Ioffe | da998cf | 2019-03-04 22:54:30 +0000 | [diff] [blame] | 631 | } |
| 632 | |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 633 | void createSession(@NonNull PackageInstallerSession sessionInfo) { |
| 634 | synchronized (mStagedSessions) { |
| 635 | mStagedSessions.append(sessionInfo.sessionId, sessionInfo); |
| 636 | } |
| 637 | } |
| 638 | |
Dario Freni | 015f935 | 2019-01-14 21:56:17 +0000 | [diff] [blame] | 639 | void abortSession(@NonNull PackageInstallerSession session) { |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 640 | synchronized (mStagedSessions) { |
Dario Freni | 015f935 | 2019-01-14 21:56:17 +0000 | [diff] [blame] | 641 | mStagedSessions.remove(session.sessionId); |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 642 | } |
| 643 | } |
Dario Freni | 8e7d0ec | 2019-01-10 15:21:40 +0000 | [diff] [blame] | 644 | |
shafik | 07205e3 | 2019-02-07 20:12:33 +0000 | [diff] [blame] | 645 | void abortCommittedSession(@NonNull PackageInstallerSession session) { |
Nikita Ioffe | 1e523b5 | 2019-03-13 16:07:48 +0000 | [diff] [blame] | 646 | if (session.isStagedSessionApplied()) { |
| 647 | Slog.w(TAG, "Cannot abort applied session : " + session.sessionId); |
shafik | 07205e3 | 2019-02-07 20:12:33 +0000 | [diff] [blame] | 648 | return; |
| 649 | } |
shafik | 07205e3 | 2019-02-07 20:12:33 +0000 | [diff] [blame] | 650 | abortSession(session); |
Nikita Ioffe | e2d52f5 | 2019-03-11 14:29:23 +0000 | [diff] [blame] | 651 | |
| 652 | boolean hasApex = sessionContainsApex(session); |
| 653 | if (hasApex) { |
| 654 | ApexSessionInfo apexSession = mApexManager.getStagedSessionInfo(session.sessionId); |
| 655 | if (apexSession == null || isApexSessionFinalized(apexSession)) { |
| 656 | Slog.w(TAG, |
Mohammad Samiul Islam | da00497 | 2019-10-09 11:29:26 +0100 | [diff] [blame] | 657 | "Cannot abort session " + session.sessionId |
| 658 | + " because it is not active or APEXD is not reachable"); |
Nikita Ioffe | e2d52f5 | 2019-03-11 14:29:23 +0000 | [diff] [blame] | 659 | return; |
| 660 | } |
| 661 | mApexManager.abortActiveSession(); |
| 662 | } |
shafik | 07205e3 | 2019-02-07 20:12:33 +0000 | [diff] [blame] | 663 | } |
| 664 | |
Nikita Ioffe | e2d52f5 | 2019-03-11 14:29:23 +0000 | [diff] [blame] | 665 | private boolean isApexSessionFinalized(ApexSessionInfo session) { |
shafik | 07205e3 | 2019-02-07 20:12:33 +0000 | [diff] [blame] | 666 | /* 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] | 667 | return session.isUnknown || session.isActivationFailed || session.isSuccess |
| 668 | || session.isRolledBack; |
| 669 | } |
| 670 | |
| 671 | private static boolean isApexSessionFailed(ApexSessionInfo apexSessionInfo) { |
Nikita Ioffe | fb6688e | 2019-02-27 15:34:08 +0000 | [diff] [blame] | 672 | // isRollbackInProgress is included to cover the scenario, when a device is rebooted in |
| 673 | // during the rollback, and apexd fails to resume the rollback after reboot. |
Nikita Ioffe | 8274222 | 2019-02-26 12:14:04 +0000 | [diff] [blame] | 674 | return apexSessionInfo.isActivationFailed || apexSessionInfo.isUnknown |
Nikita Ioffe | 7a4f08e | 2019-04-09 15:10:18 +0100 | [diff] [blame] | 675 | || apexSessionInfo.isRolledBack || apexSessionInfo.isRollbackInProgress |
| 676 | || apexSessionInfo.isRollbackFailed; |
shafik | 07205e3 | 2019-02-07 20:12:33 +0000 | [diff] [blame] | 677 | } |
| 678 | |
Dario Freni | 015f935 | 2019-01-14 21:56:17 +0000 | [diff] [blame] | 679 | @GuardedBy("mStagedSessions") |
| 680 | private boolean isMultiPackageSessionComplete(@NonNull PackageInstallerSession session) { |
| 681 | // This method assumes that the argument is either a parent session of a multi-package |
| 682 | // i.e. isMultiPackage() returns true, or that it is a child session, i.e. |
| 683 | // hasParentSessionId() returns true. |
| 684 | if (session.isMultiPackage()) { |
| 685 | // Parent session of a multi-package group. Check that we restored all the children. |
| 686 | for (int childSession : session.getChildSessionIds()) { |
| 687 | if (mStagedSessions.get(childSession) == null) { |
| 688 | return false; |
| 689 | } |
| 690 | } |
| 691 | return true; |
| 692 | } |
| 693 | if (session.hasParentSessionId()) { |
| 694 | PackageInstallerSession parent = mStagedSessions.get(session.getParentSessionId()); |
| 695 | if (parent == null) { |
| 696 | return false; |
| 697 | } |
| 698 | return isMultiPackageSessionComplete(parent); |
| 699 | } |
| 700 | Slog.wtf(TAG, "Attempting to restore an invalid multi-package session."); |
| 701 | return false; |
| 702 | } |
| 703 | |
Dario Freni | 8e7d0ec | 2019-01-10 15:21:40 +0000 | [diff] [blame] | 704 | void restoreSession(@NonNull PackageInstallerSession session) { |
Dario Freni | 015f935 | 2019-01-14 21:56:17 +0000 | [diff] [blame] | 705 | PackageInstallerSession sessionToResume = session; |
| 706 | synchronized (mStagedSessions) { |
| 707 | mStagedSessions.append(session.sessionId, session); |
| 708 | // For multi-package sessions, we don't know in which order they will be restored. We |
| 709 | // need to wait until we have restored all the session in a group before restoring them. |
| 710 | if (session.isMultiPackage() || session.hasParentSessionId()) { |
| 711 | if (!isMultiPackageSessionComplete(session)) { |
| 712 | // Still haven't recovered all sessions of the group, return. |
| 713 | return; |
| 714 | } |
| 715 | // Group recovered, find the parent if necessary and resume the installation. |
| 716 | if (session.hasParentSessionId()) { |
| 717 | sessionToResume = mStagedSessions.get(session.getParentSessionId()); |
| 718 | } |
| 719 | } |
| 720 | } |
| 721 | checkStateAndResume(sessionToResume); |
| 722 | } |
| 723 | |
| 724 | private void checkStateAndResume(@NonNull PackageInstallerSession session) { |
Dario Freni | 47799f4 | 2019-03-13 18:06:24 +0000 | [diff] [blame] | 725 | if (!session.isCommitted()) { |
| 726 | // Session hasn't been committed yet, ignore. |
| 727 | return; |
| 728 | } |
Dario Freni | 61b3e25 | 2019-01-11 23:05:39 +0000 | [diff] [blame] | 729 | // Check the state of the session and decide what to do next. |
| 730 | if (session.isStagedSessionFailed() || session.isStagedSessionApplied()) { |
| 731 | // Final states, nothing to do. |
| 732 | return; |
| 733 | } |
| 734 | if (!session.isStagedSessionReady()) { |
| 735 | // The framework got restarted before the pre-reboot verification could complete, |
| 736 | // restart the verification. |
Mohammad Samiul Islam | 6dc9889 | 2019-09-13 11:10:12 +0100 | [diff] [blame] | 737 | mPreRebootVerificationHandler.startPreRebootVerification(session.sessionId); |
Dario Freni | 61b3e25 | 2019-01-11 23:05:39 +0000 | [diff] [blame] | 738 | } else { |
| 739 | // Session had already being marked ready. Start the checks to verify if there is any |
| 740 | // follow-up work. |
Dario Freni | 4df010e | 2019-01-31 19:34:46 +0000 | [diff] [blame] | 741 | resumeSession(session); |
Dario Freni | 61b3e25 | 2019-01-11 23:05:39 +0000 | [diff] [blame] | 742 | } |
Dario Freni | 8e7d0ec | 2019-01-10 15:21:40 +0000 | [diff] [blame] | 743 | } |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 744 | |
Mohammad Samiul Islam | ef40027 | 2019-08-15 16:44:57 +0100 | [diff] [blame] | 745 | private static class LocalIntentReceiverAsync { |
| 746 | final Consumer<Intent> mConsumer; |
| 747 | |
| 748 | LocalIntentReceiverAsync(Consumer<Intent> consumer) { |
| 749 | mConsumer = consumer; |
| 750 | } |
| 751 | |
| 752 | private IIntentSender.Stub mLocalSender = new IIntentSender.Stub() { |
| 753 | @Override |
| 754 | public void send(int code, Intent intent, String resolvedType, IBinder whitelistToken, |
| 755 | IIntentReceiver finishedReceiver, String requiredPermission, Bundle options) { |
| 756 | mConsumer.accept(intent); |
| 757 | } |
| 758 | }; |
| 759 | |
| 760 | public IntentSender getIntentSender() { |
| 761 | return new IntentSender((IIntentSender) mLocalSender); |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | private static class LocalIntentReceiverSync { |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 766 | private final LinkedBlockingQueue<Intent> mResult = new LinkedBlockingQueue<>(); |
| 767 | |
| 768 | private IIntentSender.Stub mLocalSender = new IIntentSender.Stub() { |
| 769 | @Override |
| 770 | public void send(int code, Intent intent, String resolvedType, IBinder whitelistToken, |
Mohammad Samiul Islam | ef40027 | 2019-08-15 16:44:57 +0100 | [diff] [blame] | 771 | IIntentReceiver finishedReceiver, String requiredPermission, |
| 772 | Bundle options) { |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 773 | try { |
| 774 | mResult.offer(intent, 5, TimeUnit.SECONDS); |
| 775 | } catch (InterruptedException e) { |
| 776 | throw new RuntimeException(e); |
| 777 | } |
| 778 | } |
| 779 | }; |
| 780 | |
| 781 | public IntentSender getIntentSender() { |
| 782 | return new IntentSender((IIntentSender) mLocalSender); |
| 783 | } |
| 784 | |
| 785 | public Intent getResult() { |
| 786 | try { |
| 787 | return mResult.take(); |
| 788 | } catch (InterruptedException e) { |
| 789 | throw new RuntimeException(e); |
| 790 | } |
| 791 | } |
| 792 | } |
Mohammad Samiul Islam | ef40027 | 2019-08-15 16:44:57 +0100 | [diff] [blame] | 793 | |
| 794 | private final class PreRebootVerificationHandler extends Handler { |
| 795 | |
| 796 | PreRebootVerificationHandler(Looper looper) { |
| 797 | super(looper); |
| 798 | } |
| 799 | |
| 800 | /** |
| 801 | * Handler for states of pre reboot verification. The states are arranged linearly (shown |
| 802 | * below) with each state either calling the next state, or calling some other method that |
| 803 | * eventually calls the next state. |
| 804 | * |
| 805 | * <p><ul> |
| 806 | * <li>MSG_PRE_REBOOT_VERIFICATION_START</li> |
| 807 | * <li>MSG_PRE_REBOOT_VERIFICATION_APEX</li> |
| 808 | * <li>MSG_PRE_REBOOT_VERIFICATION_APK</li> |
| 809 | * <li>MSG_PRE_REBOOT_VERIFICATION_END</li> |
| 810 | * </ul></p> |
| 811 | * |
| 812 | * Details about each of state can be found in corresponding handler of node. |
| 813 | */ |
| 814 | private static final int MSG_PRE_REBOOT_VERIFICATION_START = 1; |
| 815 | private static final int MSG_PRE_REBOOT_VERIFICATION_APEX = 2; |
| 816 | private static final int MSG_PRE_REBOOT_VERIFICATION_APK = 3; |
| 817 | private static final int MSG_PRE_REBOOT_VERIFICATION_END = 4; |
| 818 | |
| 819 | @Override |
| 820 | public void handleMessage(Message msg) { |
Mohammad Samiul Islam | 6dc9889 | 2019-09-13 11:10:12 +0100 | [diff] [blame] | 821 | final int sessionId = msg.arg1; |
| 822 | final PackageInstallerSession session; |
| 823 | synchronized (mStagedSessions) { |
| 824 | session = mStagedSessions.get(sessionId); |
| 825 | } |
| 826 | // Maybe session was aborted before pre-reboot verification was complete |
| 827 | if (session == null) { |
| 828 | Slog.d(TAG, "Stopping pre-reboot verification for sessionId: " + sessionId); |
| 829 | return; |
| 830 | } |
Mohammad Samiul Islam | ef40027 | 2019-08-15 16:44:57 +0100 | [diff] [blame] | 831 | switch (msg.what) { |
| 832 | case MSG_PRE_REBOOT_VERIFICATION_START: |
| 833 | handlePreRebootVerification_Start(session); |
| 834 | break; |
| 835 | case MSG_PRE_REBOOT_VERIFICATION_APEX: |
| 836 | handlePreRebootVerification_Apex(session); |
| 837 | break; |
| 838 | case MSG_PRE_REBOOT_VERIFICATION_APK: |
| 839 | handlePreRebootVerification_Apk(session); |
| 840 | break; |
| 841 | case MSG_PRE_REBOOT_VERIFICATION_END: |
| 842 | handlePreRebootVerification_End(session); |
| 843 | break; |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | // Method for starting the pre-reboot verification |
Mohammad Samiul Islam | 6dc9889 | 2019-09-13 11:10:12 +0100 | [diff] [blame] | 848 | private void startPreRebootVerification(int sessionId) { |
| 849 | obtainMessage(MSG_PRE_REBOOT_VERIFICATION_START, sessionId, 0).sendToTarget(); |
Mohammad Samiul Islam | ef40027 | 2019-08-15 16:44:57 +0100 | [diff] [blame] | 850 | } |
| 851 | |
Mohammad Samiul Islam | 6dc9889 | 2019-09-13 11:10:12 +0100 | [diff] [blame] | 852 | private void notifyPreRebootVerification_Start_Complete(int sessionId) { |
| 853 | obtainMessage(MSG_PRE_REBOOT_VERIFICATION_APEX, sessionId, 0).sendToTarget(); |
Mohammad Samiul Islam | ef40027 | 2019-08-15 16:44:57 +0100 | [diff] [blame] | 854 | } |
| 855 | |
Mohammad Samiul Islam | 6dc9889 | 2019-09-13 11:10:12 +0100 | [diff] [blame] | 856 | private void notifyPreRebootVerification_Apex_Complete(int sessionId) { |
| 857 | obtainMessage(MSG_PRE_REBOOT_VERIFICATION_APK, sessionId, 0).sendToTarget(); |
Mohammad Samiul Islam | ef40027 | 2019-08-15 16:44:57 +0100 | [diff] [blame] | 858 | } |
| 859 | |
Mohammad Samiul Islam | 6dc9889 | 2019-09-13 11:10:12 +0100 | [diff] [blame] | 860 | private void notifyPreRebootVerification_Apk_Complete(int sessionId) { |
| 861 | obtainMessage(MSG_PRE_REBOOT_VERIFICATION_END, sessionId, 0).sendToTarget(); |
Mohammad Samiul Islam | ef40027 | 2019-08-15 16:44:57 +0100 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | /** |
| 865 | * A dummy state for starting the pre reboot verification. |
| 866 | * |
| 867 | * See {@link PreRebootVerificationHandler} to see all nodes of pre reboot verification |
| 868 | */ |
| 869 | private void handlePreRebootVerification_Start(@NonNull PackageInstallerSession session) { |
| 870 | Slog.d(TAG, "Starting preRebootVerification for session " + session.sessionId); |
Mohammad Samiul Islam | 6dc9889 | 2019-09-13 11:10:12 +0100 | [diff] [blame] | 871 | notifyPreRebootVerification_Start_Complete(session.sessionId); |
Mohammad Samiul Islam | ef40027 | 2019-08-15 16:44:57 +0100 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | /** |
| 875 | * Pre-reboot verification state for apex files: |
| 876 | * |
| 877 | * <p><ul> |
| 878 | * <li>submits session to apex service</li> |
| 879 | * <li>validates signatures of apex files</li> |
| 880 | * </ul></p> |
| 881 | */ |
| 882 | private void handlePreRebootVerification_Apex(@NonNull PackageInstallerSession session) { |
| 883 | final boolean hasApex = sessionContainsApex(session); |
| 884 | |
| 885 | // APEX checks. For single-package sessions, check if they contain an APEX. For |
| 886 | // multi-package sessions, find all the child sessions that contain an APEX. |
| 887 | if (hasApex) { |
| 888 | try { |
| 889 | final List<PackageInfo> apexPackages = |
| 890 | submitSessionToApexService(session); |
| 891 | for (PackageInfo apexPackage : apexPackages) { |
Mohammad Samiul Islam | d7472d0 | 2019-11-11 17:53:47 +0000 | [diff] [blame] | 892 | validateApexSignature(apexPackage); |
Mohammad Samiul Islam | ef40027 | 2019-08-15 16:44:57 +0100 | [diff] [blame] | 893 | } |
| 894 | } catch (PackageManagerException e) { |
| 895 | session.setStagedSessionFailed(e.error, e.getMessage()); |
| 896 | return; |
| 897 | } |
| 898 | } |
| 899 | |
Mohammad Samiul Islam | 6dc9889 | 2019-09-13 11:10:12 +0100 | [diff] [blame] | 900 | notifyPreRebootVerification_Apex_Complete(session.sessionId); |
Mohammad Samiul Islam | ef40027 | 2019-08-15 16:44:57 +0100 | [diff] [blame] | 901 | } |
| 902 | |
| 903 | /** |
| 904 | * Pre-reboot verification state for apk files: |
| 905 | * <p><ul> |
| 906 | * <li>performs a dry-run install of apk</li> |
| 907 | * </ul></p> |
| 908 | */ |
| 909 | private void handlePreRebootVerification_Apk(@NonNull PackageInstallerSession session) { |
| 910 | if (!sessionContainsApk(session)) { |
Mohammad Samiul Islam | 6dc9889 | 2019-09-13 11:10:12 +0100 | [diff] [blame] | 911 | notifyPreRebootVerification_Apk_Complete(session.sessionId); |
Mohammad Samiul Islam | ef40027 | 2019-08-15 16:44:57 +0100 | [diff] [blame] | 912 | return; |
| 913 | } |
| 914 | |
| 915 | try { |
| 916 | Slog.d(TAG, "Running a pre-reboot verification for APKs in session " |
| 917 | + session.sessionId + " by performing a dry-run install"); |
| 918 | |
Mohammad Samiul Islam | 8c041df | 2019-09-10 16:26:26 +0100 | [diff] [blame] | 919 | // verifyApksInSession will notify the handler when APK verification is complete |
| 920 | verifyApksInSession(session); |
Mohammad Samiul Islam | ef40027 | 2019-08-15 16:44:57 +0100 | [diff] [blame] | 921 | // TODO(b/118865310): abort the session on apexd. |
| 922 | } catch (PackageManagerException e) { |
| 923 | session.setStagedSessionFailed(e.error, e.getMessage()); |
| 924 | } |
| 925 | } |
| 926 | |
| 927 | /** |
| 928 | * Pre-reboot verification state for wrapping up: |
| 929 | * <p><ul> |
| 930 | * <li>enables rollback if required</li> |
| 931 | * <li>marks session as ready</li> |
| 932 | * </ul></p> |
| 933 | */ |
| 934 | private void handlePreRebootVerification_End(@NonNull PackageInstallerSession session) { |
| 935 | if ((session.params.installFlags & PackageManager.INSTALL_ENABLE_ROLLBACK) != 0) { |
| 936 | // If rollback is enabled for this session, we call through to the RollbackManager |
| 937 | // with the list of sessions it must enable rollback for. Note that |
| 938 | // notifyStagedSession is a synchronous operation. |
| 939 | final IRollbackManager rm = IRollbackManager.Stub.asInterface( |
| 940 | ServiceManager.getService(Context.ROLLBACK_SERVICE)); |
| 941 | try { |
| 942 | // NOTE: To stay consistent with the non-staged install flow, we don't fail the |
| 943 | // entire install if rollbacks can't be enabled. |
| 944 | if (!rm.notifyStagedSession(session.sessionId)) { |
| 945 | Slog.e(TAG, "Unable to enable rollback for session: " |
| 946 | + session.sessionId); |
| 947 | } |
| 948 | } catch (RemoteException re) { |
Richard Uhler | 7c81c3a | 2019-09-11 10:43:24 +0100 | [diff] [blame] | 949 | Slog.e(TAG, "Failed to notifyStagedSession for session: " |
| 950 | + session.sessionId, re); |
Mohammad Samiul Islam | ef40027 | 2019-08-15 16:44:57 +0100 | [diff] [blame] | 951 | } |
| 952 | } |
| 953 | |
| 954 | // Proactively mark session as ready before calling apexd. Although this call order |
| 955 | // looks counter-intuitive, this is the easiest way to ensure that session won't end up |
| 956 | // in the inconsistent state: |
| 957 | // - If device gets rebooted right before call to apexd, then apexd will never activate |
| 958 | // apex files of this staged session. This will result in StagingManager failing |
| 959 | // the session. |
| 960 | // On the other hand, if the order of the calls was inverted (first call apexd, then |
| 961 | // mark session as ready), then if a device gets rebooted right after the call to apexd, |
| 962 | // only apex part of the train will be applied, leaving device in an inconsistent state. |
| 963 | Slog.d(TAG, "Marking session " + session.sessionId + " as ready"); |
| 964 | session.setStagedSessionReady(); |
| 965 | final boolean hasApex = sessionContainsApex(session); |
| 966 | if (!hasApex) { |
| 967 | // Session doesn't contain apex, nothing to do. |
| 968 | return; |
| 969 | } |
| 970 | try { |
| 971 | mApexManager.markStagedSessionReady(session.sessionId); |
| 972 | } catch (PackageManagerException e) { |
| 973 | session.setStagedSessionFailed(e.error, e.getMessage()); |
| 974 | } |
| 975 | } |
| 976 | } |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 977 | } |