Richard Uhler | e95d055 | 2018-12-27 15:03:41 +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.rollback; |
| 18 | |
Richard Uhler | 1fc10c1 | 2019-03-18 11:38:46 +0000 | [diff] [blame] | 19 | import android.Manifest; |
Richard Uhler | 2a5facc | 2019-02-18 11:33:03 +0000 | [diff] [blame] | 20 | import android.annotation.NonNull; |
Richard Uhler | 82913b7 | 2019-04-01 13:02:31 +0100 | [diff] [blame] | 21 | import android.annotation.UserIdInt; |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 22 | import android.app.AppOpsManager; |
| 23 | import android.content.BroadcastReceiver; |
| 24 | import android.content.Context; |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 25 | import android.content.Intent; |
| 26 | import android.content.IntentFilter; |
| 27 | import android.content.IntentSender; |
Richard Uhler | ab009ea | 2019-02-25 12:11:05 +0000 | [diff] [blame] | 28 | import android.content.pm.ApplicationInfo; |
Richard Uhler | 1fc10c1 | 2019-03-18 11:38:46 +0000 | [diff] [blame] | 29 | import android.content.pm.ModuleInfo; |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 30 | import android.content.pm.PackageInfo; |
| 31 | import android.content.pm.PackageInstaller; |
| 32 | import android.content.pm.PackageManager; |
| 33 | import android.content.pm.PackageManagerInternal; |
| 34 | import android.content.pm.PackageParser; |
| 35 | import android.content.pm.ParceledListSlice; |
Richard Uhler | 82913b7 | 2019-04-01 13:02:31 +0100 | [diff] [blame] | 36 | import android.content.pm.UserInfo; |
Richard Uhler | a7e9b2d | 2019-01-22 17:20:58 +0000 | [diff] [blame] | 37 | import android.content.pm.VersionedPackage; |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 38 | import android.content.rollback.IRollbackManager; |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 39 | import android.content.rollback.RollbackInfo; |
Richard Uhler | 2a48c29 | 2019-01-28 17:33:48 +0000 | [diff] [blame] | 40 | import android.content.rollback.RollbackManager; |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 41 | import android.os.Binder; |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 42 | import android.os.Environment; |
| 43 | import android.os.Handler; |
| 44 | import android.os.HandlerThread; |
Narayan Kamath | 869f706 | 2019-01-10 12:24:15 +0000 | [diff] [blame] | 45 | import android.os.Process; |
shafik | da5e4ee | 2019-02-12 16:29:01 +0000 | [diff] [blame] | 46 | import android.os.SystemClock; |
shafik | 74fec18 | 2019-03-14 16:29:48 +0000 | [diff] [blame] | 47 | import android.os.UserHandle; |
Richard Uhler | 798b859 | 2019-06-07 14:04:43 +0100 | [diff] [blame] | 48 | import android.os.UserManager; |
shafik | 0ad18b8 | 2019-01-24 16:27:24 +0000 | [diff] [blame] | 49 | import android.provider.DeviceConfig; |
Richard Uhler | 4756970 | 2019-05-02 12:36:39 +0100 | [diff] [blame] | 50 | import android.util.ArraySet; |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 51 | import android.util.IntArray; |
JW Wang | fab29c6 | 2019-11-19 16:17:41 +0800 | [diff] [blame] | 52 | import android.util.LongArrayQueue; |
shafik | bb771fb | 2019-06-05 14:59:57 +0100 | [diff] [blame] | 53 | import android.util.Slog; |
Richard Uhler | b9d5447 | 2019-01-22 12:50:08 +0000 | [diff] [blame] | 54 | import android.util.SparseBooleanArray; |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 55 | |
| 56 | import com.android.internal.annotations.GuardedBy; |
Oli Lan | 8753d82 | 2019-11-01 11:33:53 +0000 | [diff] [blame] | 57 | import com.android.internal.util.DumpUtils; |
shafik | 6004600 | 2019-03-12 17:54:10 +0000 | [diff] [blame] | 58 | import com.android.internal.util.IndentingPrintWriter; |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 59 | import com.android.server.LocalServices; |
Gavin Corkery | efb3ff1 | 2019-12-02 18:15:24 +0000 | [diff] [blame] | 60 | import com.android.server.PackageWatchdog; |
JW Wang | 9b51b73 | 2019-12-11 17:14:15 +0800 | [diff] [blame] | 61 | import com.android.server.SystemConfig; |
shafik | 18b627e | 2019-05-01 20:41:48 +0100 | [diff] [blame] | 62 | import com.android.server.Watchdog; |
Narayan Kamath | 869f706 | 2019-01-10 12:24:15 +0000 | [diff] [blame] | 63 | import com.android.server.pm.Installer; |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 64 | |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 65 | import java.io.File; |
shafik | 6004600 | 2019-03-12 17:54:10 +0000 | [diff] [blame] | 66 | import java.io.FileDescriptor; |
shafik | 6004600 | 2019-03-12 17:54:10 +0000 | [diff] [blame] | 67 | import java.io.PrintWriter; |
Richard Uhler | b9d5447 | 2019-01-22 12:50:08 +0000 | [diff] [blame] | 68 | import java.security.SecureRandom; |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 69 | import java.time.Instant; |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 70 | import java.time.temporal.ChronoUnit; |
| 71 | import java.util.ArrayList; |
JW Wang | 12d0c3e | 2019-12-02 21:01:35 +0800 | [diff] [blame] | 72 | import java.util.Arrays; |
Richard Uhler | 1924d6d | 2019-04-26 10:20:12 +0100 | [diff] [blame] | 73 | import java.util.HashSet; |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 74 | import java.util.Iterator; |
| 75 | import java.util.List; |
Richard Uhler | b9d5447 | 2019-01-22 12:50:08 +0000 | [diff] [blame] | 76 | import java.util.Random; |
Richard Uhler | e9aaf63 | 2019-03-01 16:03:01 +0000 | [diff] [blame] | 77 | import java.util.Set; |
Richard Uhler | 74ee299 | 2019-07-01 15:21:44 +0100 | [diff] [blame] | 78 | import java.util.concurrent.CountDownLatch; |
Narayan Kamath | fcd4a04 | 2019-02-01 14:16:37 +0000 | [diff] [blame] | 79 | import java.util.concurrent.LinkedBlockingQueue; |
shafik | 0ad18b8 | 2019-01-24 16:27:24 +0000 | [diff] [blame] | 80 | import java.util.concurrent.TimeUnit; |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 81 | |
| 82 | /** |
| 83 | * Implementation of service that manages APK level rollbacks. |
| 84 | */ |
| 85 | class RollbackManagerServiceImpl extends IRollbackManager.Stub { |
| 86 | |
| 87 | private static final String TAG = "RollbackManager"; |
JW Wang | 12d0c3e | 2019-12-02 21:01:35 +0800 | [diff] [blame] | 88 | private static final boolean LOCAL_LOGV = false; |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 89 | |
Matt Pape | 6686f1f | 2019-07-09 10:41:20 -0700 | [diff] [blame] | 90 | // Rollbacks expire after 14 days. |
shafik | 0ad18b8 | 2019-01-24 16:27:24 +0000 | [diff] [blame] | 91 | private static final long DEFAULT_ROLLBACK_LIFETIME_DURATION_MILLIS = |
Matt Pape | 6686f1f | 2019-07-09 10:41:20 -0700 | [diff] [blame] | 92 | TimeUnit.DAYS.toMillis(14); |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 93 | |
| 94 | // Lock used to synchronize accesses to in-memory rollback data |
| 95 | // structures. By convention, methods with the suffix "Locked" require |
| 96 | // mLock is held when they are called. |
| 97 | private final Object mLock = new Object(); |
| 98 | |
shafik | 0ad18b8 | 2019-01-24 16:27:24 +0000 | [diff] [blame] | 99 | // No need for guarding with lock because value is only accessed in handler thread |
| 100 | // and the value will be written on boot complete. Initialization here happens before |
| 101 | // handler threads are running so that's fine. |
| 102 | private long mRollbackLifetimeDurationInMillis = DEFAULT_ROLLBACK_LIFETIME_DURATION_MILLIS; |
| 103 | |
shafik | 18b627e | 2019-05-01 20:41:48 +0100 | [diff] [blame] | 104 | private static final long HANDLER_THREAD_TIMEOUT_DURATION_MILLIS = |
| 105 | TimeUnit.MINUTES.toMillis(10); |
| 106 | |
Richard Uhler | b9d5447 | 2019-01-22 12:50:08 +0000 | [diff] [blame] | 107 | // Used for generating rollback IDs. |
| 108 | private final Random mRandom = new SecureRandom(); |
| 109 | |
| 110 | // Set of allocated rollback ids |
| 111 | @GuardedBy("mLock") |
| 112 | private final SparseBooleanArray mAllocatedRollbackIds = new SparseBooleanArray(); |
| 113 | |
Richard Uhler | 8d512ee | 2019-07-19 14:55:01 +0100 | [diff] [blame] | 114 | // Rollbacks we are in the process of enabling. |
Richard Uhler | 2d7c7f0d | 2019-01-04 09:18:21 +0000 | [diff] [blame] | 115 | @GuardedBy("mLock") |
Richard Uhler | 4756970 | 2019-05-02 12:36:39 +0100 | [diff] [blame] | 116 | private final Set<NewRollback> mNewRollbacks = new ArraySet<>(); |
Richard Uhler | f1910c5 | 2019-01-09 14:27:36 +0000 | [diff] [blame] | 117 | |
Richard Uhler | 6f8a33b | 2019-02-26 10:40:36 +0000 | [diff] [blame] | 118 | // The list of all rollbacks, including available and committed rollbacks. |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 119 | @GuardedBy("mLock") |
Richard Uhler | 8d512ee | 2019-07-19 14:55:01 +0100 | [diff] [blame] | 120 | private final List<Rollback> mRollbacks; |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 121 | |
Oli Lan | 0bf4486 | 2019-08-29 14:25:38 +0100 | [diff] [blame] | 122 | // Apk sessions from a staged session with no matching rollback. |
| 123 | @GuardedBy("mLock") |
| 124 | private final IntArray mOrphanedApkSessionIds = new IntArray(); |
| 125 | |
Richard Uhler | 28e7323 | 2019-01-21 16:48:55 +0000 | [diff] [blame] | 126 | private final RollbackStore mRollbackStore; |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 127 | |
| 128 | private final Context mContext; |
| 129 | private final HandlerThread mHandlerThread; |
Narayan Kamath | 869f706 | 2019-01-10 12:24:15 +0000 | [diff] [blame] | 130 | private final Installer mInstaller; |
Zimuzo | c4073cc | 2019-01-18 18:39:18 +0000 | [diff] [blame] | 131 | private final RollbackPackageHealthObserver mPackageHealthObserver; |
Nikita Ioffe | 952aa7b | 2019-01-28 19:49:56 +0000 | [diff] [blame] | 132 | private final AppDataRollbackHelper mAppDataRollbackHelper; |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 133 | |
JW Wang | fab29c6 | 2019-11-19 16:17:41 +0800 | [diff] [blame] | 134 | // The # of milli-seconds to sleep for each received ACTION_PACKAGE_ENABLE_ROLLBACK. |
| 135 | // Used by #blockRollbackManager to test timeout in enabling rollbacks. |
| 136 | // Accessed on the handler thread only. |
| 137 | private final LongArrayQueue mSleepDuration = new LongArrayQueue(); |
| 138 | |
shafik | da5e4ee | 2019-02-12 16:29:01 +0000 | [diff] [blame] | 139 | // This field stores the difference in Millis between the uptime (millis since device |
Richard Uhler | 8d512ee | 2019-07-19 14:55:01 +0100 | [diff] [blame] | 140 | // has booted) and current time (device wall clock) - it's used to update rollback |
shafik | da5e4ee | 2019-02-12 16:29:01 +0000 | [diff] [blame] | 141 | // timestamps when the time is changed, by the user or by change of timezone. |
| 142 | // No need for guarding with lock because value is only accessed in handler thread. |
| 143 | private long mRelativeBootTime = calculateRelativeBootTime(); |
| 144 | |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 145 | RollbackManagerServiceImpl(Context context) { |
| 146 | mContext = context; |
Narayan Kamath | 869f706 | 2019-01-10 12:24:15 +0000 | [diff] [blame] | 147 | // Note that we're calling onStart here because this object is only constructed on |
| 148 | // SystemService#onStart. |
| 149 | mInstaller = new Installer(mContext); |
| 150 | mInstaller.onStart(); |
shafik | 18b627e | 2019-05-01 20:41:48 +0100 | [diff] [blame] | 151 | |
Richard Uhler | 28e7323 | 2019-01-21 16:48:55 +0000 | [diff] [blame] | 152 | mRollbackStore = new RollbackStore(new File(Environment.getDataDirectory(), "rollback")); |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 153 | |
Zimuzo | c4073cc | 2019-01-18 18:39:18 +0000 | [diff] [blame] | 154 | mPackageHealthObserver = new RollbackPackageHealthObserver(mContext); |
Nikita Ioffe | 952aa7b | 2019-01-28 19:49:56 +0000 | [diff] [blame] | 155 | mAppDataRollbackHelper = new AppDataRollbackHelper(mInstaller); |
Zimuzo | c4073cc | 2019-01-18 18:39:18 +0000 | [diff] [blame] | 156 | |
Richard Uhler | 74ee299 | 2019-07-01 15:21:44 +0100 | [diff] [blame] | 157 | // Load rollback data from device storage. |
| 158 | synchronized (mLock) { |
Richard Uhler | 8d512ee | 2019-07-19 14:55:01 +0100 | [diff] [blame] | 159 | mRollbacks = mRollbackStore.loadRollbacks(); |
| 160 | for (Rollback rollback : mRollbacks) { |
| 161 | mAllocatedRollbackIds.put(rollback.info.getRollbackId(), true); |
Richard Uhler | 74ee299 | 2019-07-01 15:21:44 +0100 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
| 165 | // Kick off and start monitoring the handler thread. |
| 166 | mHandlerThread = new HandlerThread("RollbackManagerServiceHandler"); |
| 167 | mHandlerThread.start(); |
| 168 | Watchdog.getInstance().addThread(getHandler(), HANDLER_THREAD_TIMEOUT_DURATION_MILLIS); |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 169 | |
Richard Uhler | 82913b7 | 2019-04-01 13:02:31 +0100 | [diff] [blame] | 170 | for (UserInfo userInfo : UserManager.get(mContext).getUsers(true)) { |
| 171 | registerUserCallbacks(userInfo.getUserHandle()); |
| 172 | } |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 173 | |
| 174 | IntentFilter enableRollbackFilter = new IntentFilter(); |
| 175 | enableRollbackFilter.addAction(Intent.ACTION_PACKAGE_ENABLE_ROLLBACK); |
| 176 | try { |
| 177 | enableRollbackFilter.addDataType("application/vnd.android.package-archive"); |
| 178 | } catch (IntentFilter.MalformedMimeTypeException e) { |
shafik | bb771fb | 2019-06-05 14:59:57 +0100 | [diff] [blame] | 179 | Slog.e(TAG, "addDataType", e); |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | mContext.registerReceiver(new BroadcastReceiver() { |
| 183 | @Override |
| 184 | public void onReceive(Context context, Intent intent) { |
| 185 | if (Intent.ACTION_PACKAGE_ENABLE_ROLLBACK.equals(intent.getAction())) { |
| 186 | int token = intent.getIntExtra( |
| 187 | PackageManagerInternal.EXTRA_ENABLE_ROLLBACK_TOKEN, -1); |
| 188 | int installFlags = intent.getIntExtra( |
| 189 | PackageManagerInternal.EXTRA_ENABLE_ROLLBACK_INSTALL_FLAGS, 0); |
Richard Uhler | 82913b7 | 2019-04-01 13:02:31 +0100 | [diff] [blame] | 190 | int user = intent.getIntExtra( |
| 191 | PackageManagerInternal.EXTRA_ENABLE_ROLLBACK_USER, 0); |
| 192 | |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 193 | File newPackageCodePath = new File(intent.getData().getPath()); |
| 194 | |
JW Wang | fab29c6 | 2019-11-19 16:17:41 +0800 | [diff] [blame] | 195 | queueSleepIfNeeded(); |
| 196 | |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 197 | getHandler().post(() -> { |
Oli Lan | 48f3cf4 | 2019-08-09 10:25:49 +0100 | [diff] [blame] | 198 | boolean success = |
| 199 | enableRollback(installFlags, newPackageCodePath, user, token); |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 200 | int ret = PackageManagerInternal.ENABLE_ROLLBACK_SUCCEEDED; |
| 201 | if (!success) { |
| 202 | ret = PackageManagerInternal.ENABLE_ROLLBACK_FAILED; |
| 203 | } |
| 204 | |
| 205 | PackageManagerInternal pm = LocalServices.getService( |
| 206 | PackageManagerInternal.class); |
| 207 | pm.setEnableRollbackCode(token, ret); |
| 208 | }); |
| 209 | |
| 210 | // We're handling the ordered broadcast. Abort the |
| 211 | // broadcast because there is no need for it to go to |
| 212 | // anyone else. |
| 213 | abortBroadcast(); |
| 214 | } |
| 215 | } |
| 216 | }, enableRollbackFilter, null, getHandler()); |
shafik | da5e4ee | 2019-02-12 16:29:01 +0000 | [diff] [blame] | 217 | |
shafik | 4831ad7 | 2019-05-03 17:36:42 +0100 | [diff] [blame] | 218 | IntentFilter enableRollbackTimedOutFilter = new IntentFilter(); |
| 219 | enableRollbackTimedOutFilter.addAction(Intent.ACTION_CANCEL_ENABLE_ROLLBACK); |
| 220 | |
| 221 | mContext.registerReceiver(new BroadcastReceiver() { |
| 222 | @Override |
| 223 | public void onReceive(Context context, Intent intent) { |
| 224 | if (Intent.ACTION_CANCEL_ENABLE_ROLLBACK.equals(intent.getAction())) { |
| 225 | int token = intent.getIntExtra( |
| 226 | PackageManagerInternal.EXTRA_ENABLE_ROLLBACK_TOKEN, -1); |
JW Wang | 12d0c3e | 2019-12-02 21:01:35 +0800 | [diff] [blame] | 227 | if (LOCAL_LOGV) { |
| 228 | Slog.v(TAG, "broadcast=ACTION_CANCEL_ENABLE_ROLLBACK token=" + token); |
| 229 | } |
shafik | 4831ad7 | 2019-05-03 17:36:42 +0100 | [diff] [blame] | 230 | synchronized (mLock) { |
| 231 | for (NewRollback rollback : mNewRollbacks) { |
| 232 | if (rollback.hasToken(token)) { |
Oli Lan | 4af2f75 | 2019-09-27 10:39:32 +0100 | [diff] [blame] | 233 | rollback.setCancelled(); |
shafik | 4831ad7 | 2019-05-03 17:36:42 +0100 | [diff] [blame] | 234 | return; |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | }, enableRollbackTimedOutFilter, null, getHandler()); |
| 241 | |
shafik | 8aea1ad | 2019-05-21 12:34:50 +0100 | [diff] [blame] | 242 | IntentFilter userAddedIntentFilter = new IntentFilter(Intent.ACTION_USER_ADDED); |
| 243 | mContext.registerReceiver(new BroadcastReceiver() { |
| 244 | @Override |
| 245 | public void onReceive(Context context, Intent intent) { |
| 246 | if (Intent.ACTION_USER_ADDED.equals(intent.getAction())) { |
| 247 | final int newUserId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
| 248 | if (newUserId == -1) { |
| 249 | return; |
| 250 | } |
| 251 | registerUserCallbacks(UserHandle.of(newUserId)); |
| 252 | } |
| 253 | } |
| 254 | }, userAddedIntentFilter, null, getHandler()); |
| 255 | |
shafik | da5e4ee | 2019-02-12 16:29:01 +0000 | [diff] [blame] | 256 | registerTimeChangeReceiver(); |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Richard Uhler | 82913b7 | 2019-04-01 13:02:31 +0100 | [diff] [blame] | 259 | private void registerUserCallbacks(UserHandle user) { |
| 260 | Context context = getContextAsUser(user); |
| 261 | if (context == null) { |
shafik | bb771fb | 2019-06-05 14:59:57 +0100 | [diff] [blame] | 262 | Slog.e(TAG, "Unable to register user callbacks for user " + user); |
Richard Uhler | 82913b7 | 2019-04-01 13:02:31 +0100 | [diff] [blame] | 263 | return; |
| 264 | } |
| 265 | |
Richard Uhler | 82913b7 | 2019-04-01 13:02:31 +0100 | [diff] [blame] | 266 | context.getPackageManager().getPackageInstaller() |
| 267 | .registerSessionCallback(new SessionCallback(), getHandler()); |
| 268 | |
| 269 | IntentFilter filter = new IntentFilter(); |
| 270 | filter.addAction(Intent.ACTION_PACKAGE_REPLACED); |
| 271 | filter.addAction(Intent.ACTION_PACKAGE_FULLY_REMOVED); |
| 272 | filter.addDataScheme("package"); |
| 273 | context.registerReceiver(new BroadcastReceiver() { |
| 274 | @Override |
| 275 | public void onReceive(Context context, Intent intent) { |
| 276 | String action = intent.getAction(); |
| 277 | if (Intent.ACTION_PACKAGE_REPLACED.equals(action)) { |
| 278 | String packageName = intent.getData().getSchemeSpecificPart(); |
JW Wang | 12d0c3e | 2019-12-02 21:01:35 +0800 | [diff] [blame] | 279 | if (LOCAL_LOGV) { |
| 280 | Slog.v(TAG, "broadcast=ACTION_PACKAGE_REPLACED" + " pkg=" + packageName); |
| 281 | } |
Richard Uhler | 82913b7 | 2019-04-01 13:02:31 +0100 | [diff] [blame] | 282 | onPackageReplaced(packageName); |
| 283 | } |
| 284 | if (Intent.ACTION_PACKAGE_FULLY_REMOVED.equals(action)) { |
| 285 | String packageName = intent.getData().getSchemeSpecificPart(); |
JW Wang | 12d0c3e | 2019-12-02 21:01:35 +0800 | [diff] [blame] | 286 | if (LOCAL_LOGV) { |
| 287 | Slog.v(TAG, "broadcast=ACTION_PACKAGE_FULLY_REMOVED" |
| 288 | + " pkg=" + packageName); |
| 289 | } |
Richard Uhler | 82913b7 | 2019-04-01 13:02:31 +0100 | [diff] [blame] | 290 | onPackageFullyRemoved(packageName); |
| 291 | } |
| 292 | } |
| 293 | }, filter, null, getHandler()); |
| 294 | } |
| 295 | |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 296 | @Override |
Richard Uhler | 150ad98 | 2019-01-23 15:16:10 +0000 | [diff] [blame] | 297 | public ParceledListSlice getAvailableRollbacks() { |
Richard Uhler | 1fc10c1 | 2019-03-18 11:38:46 +0000 | [diff] [blame] | 298 | enforceManageRollbacks("getAvailableRollbacks"); |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 299 | synchronized (mLock) { |
Richard Uhler | 150ad98 | 2019-01-23 15:16:10 +0000 | [diff] [blame] | 300 | List<RollbackInfo> rollbacks = new ArrayList<>(); |
Richard Uhler | 6f8a33b | 2019-02-26 10:40:36 +0000 | [diff] [blame] | 301 | for (int i = 0; i < mRollbacks.size(); ++i) { |
Richard Uhler | 8d512ee | 2019-07-19 14:55:01 +0100 | [diff] [blame] | 302 | Rollback rollback = mRollbacks.get(i); |
Oli Lan | 4af2f75 | 2019-09-27 10:39:32 +0100 | [diff] [blame] | 303 | if (rollback.isAvailable()) { |
| 304 | rollbacks.add(rollback.info); |
Richard Uhler | 60ac706 | 2019-02-05 13:25:39 +0000 | [diff] [blame] | 305 | } |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 306 | } |
Richard Uhler | 150ad98 | 2019-01-23 15:16:10 +0000 | [diff] [blame] | 307 | return new ParceledListSlice<>(rollbacks); |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 308 | } |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | @Override |
Richard Uhler | f498dab | 2019-06-28 10:55:45 +0100 | [diff] [blame] | 312 | public ParceledListSlice<RollbackInfo> getRecentlyCommittedRollbacks() { |
Richard Uhler | 1fc10c1 | 2019-03-18 11:38:46 +0000 | [diff] [blame] | 313 | enforceManageRollbacks("getRecentlyCommittedRollbacks"); |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 314 | |
| 315 | synchronized (mLock) { |
Richard Uhler | 6f8a33b | 2019-02-26 10:40:36 +0000 | [diff] [blame] | 316 | List<RollbackInfo> rollbacks = new ArrayList<>(); |
| 317 | for (int i = 0; i < mRollbacks.size(); ++i) { |
Richard Uhler | 8d512ee | 2019-07-19 14:55:01 +0100 | [diff] [blame] | 318 | Rollback rollback = mRollbacks.get(i); |
Oli Lan | 4af2f75 | 2019-09-27 10:39:32 +0100 | [diff] [blame] | 319 | if (rollback.isCommitted()) { |
| 320 | rollbacks.add(rollback.info); |
Richard Uhler | 6f8a33b | 2019-02-26 10:40:36 +0000 | [diff] [blame] | 321 | } |
| 322 | } |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 323 | return new ParceledListSlice<>(rollbacks); |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | @Override |
Richard Uhler | bf5b5c4 | 2019-01-28 15:26:37 +0000 | [diff] [blame] | 328 | public void commitRollback(int rollbackId, ParceledListSlice causePackages, |
| 329 | String callerPackageName, IntentSender statusReceiver) { |
Richard Uhler | f498dab | 2019-06-28 10:55:45 +0100 | [diff] [blame] | 330 | enforceManageRollbacks("commitRollback"); |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 331 | |
| 332 | final int callingUid = Binder.getCallingUid(); |
| 333 | AppOpsManager appOps = mContext.getSystemService(AppOpsManager.class); |
| 334 | appOps.checkPackage(callingUid, callerPackageName); |
| 335 | |
| 336 | getHandler().post(() -> |
Richard Uhler | bf5b5c4 | 2019-01-28 15:26:37 +0000 | [diff] [blame] | 337 | commitRollbackInternal(rollbackId, causePackages.getList(), |
| 338 | callerPackageName, statusReceiver)); |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 339 | } |
| 340 | |
shafik | da5e4ee | 2019-02-12 16:29:01 +0000 | [diff] [blame] | 341 | private void registerTimeChangeReceiver() { |
| 342 | final BroadcastReceiver timeChangeIntentReceiver = new BroadcastReceiver() { |
| 343 | @Override |
| 344 | public void onReceive(Context context, Intent intent) { |
| 345 | final long oldRelativeBootTime = mRelativeBootTime; |
| 346 | mRelativeBootTime = calculateRelativeBootTime(); |
| 347 | final long timeDifference = mRelativeBootTime - oldRelativeBootTime; |
| 348 | |
| 349 | synchronized (mLock) { |
Richard Uhler | 8d512ee | 2019-07-19 14:55:01 +0100 | [diff] [blame] | 350 | Iterator<Rollback> iter = mRollbacks.iterator(); |
shafik | da5e4ee | 2019-02-12 16:29:01 +0000 | [diff] [blame] | 351 | while (iter.hasNext()) { |
Richard Uhler | 8d512ee | 2019-07-19 14:55:01 +0100 | [diff] [blame] | 352 | Rollback rollback = iter.next(); |
Oli Lan | 4af2f75 | 2019-09-27 10:39:32 +0100 | [diff] [blame] | 353 | rollback.setTimestamp( |
| 354 | rollback.getTimestamp().plusMillis(timeDifference)); |
shafik | da5e4ee | 2019-02-12 16:29:01 +0000 | [diff] [blame] | 355 | } |
shafik | da5e4ee | 2019-02-12 16:29:01 +0000 | [diff] [blame] | 356 | } |
| 357 | } |
| 358 | }; |
| 359 | final IntentFilter filter = new IntentFilter(); |
| 360 | filter.addAction(Intent.ACTION_TIME_CHANGED); |
| 361 | mContext.registerReceiver(timeChangeIntentReceiver, filter, |
| 362 | null /* broadcastPermission */, getHandler()); |
| 363 | } |
| 364 | |
| 365 | private static long calculateRelativeBootTime() { |
| 366 | return System.currentTimeMillis() - SystemClock.elapsedRealtime(); |
| 367 | } |
| 368 | |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 369 | /** |
Richard Uhler | e87368e | 2019-01-24 16:34:14 +0000 | [diff] [blame] | 370 | * Performs the actual work to commit a rollback. |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 371 | * The work is done on the current thread. This may be a long running |
| 372 | * operation. |
| 373 | */ |
Richard Uhler | bf5b5c4 | 2019-01-28 15:26:37 +0000 | [diff] [blame] | 374 | private void commitRollbackInternal(int rollbackId, List<VersionedPackage> causePackages, |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 375 | String callerPackageName, IntentSender statusReceiver) { |
JW Wang | 12d0c3e | 2019-12-02 21:01:35 +0800 | [diff] [blame] | 376 | Slog.i(TAG, "commitRollback id=" + rollbackId + " caller=" + callerPackageName); |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 377 | |
Richard Uhler | 8d512ee | 2019-07-19 14:55:01 +0100 | [diff] [blame] | 378 | Rollback rollback = getRollbackForId(rollbackId); |
Oli Lan | 8c0084d | 2019-09-11 13:38:43 +0100 | [diff] [blame] | 379 | if (rollback == null) { |
Oli Lan | 7363a62 | 2019-09-13 10:06:12 +0100 | [diff] [blame] | 380 | sendFailure( |
| 381 | mContext, statusReceiver, RollbackManager.STATUS_FAILURE_ROLLBACK_UNAVAILABLE, |
Richard Uhler | 2a48c29 | 2019-01-28 17:33:48 +0000 | [diff] [blame] | 382 | "Rollback unavailable"); |
Narayan Kamath | bc36f8d | 2019-01-23 12:00:08 +0000 | [diff] [blame] | 383 | return; |
| 384 | } |
Oli Lan | 4af2f75 | 2019-09-27 10:39:32 +0100 | [diff] [blame] | 385 | rollback.commit(mContext, causePackages, callerPackageName, statusReceiver); |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | @Override |
| 389 | public void reloadPersistedData() { |
| 390 | mContext.enforceCallingOrSelfPermission( |
Richard Uhler | 1fc10c1 | 2019-03-18 11:38:46 +0000 | [diff] [blame] | 391 | Manifest.permission.TEST_MANAGE_ROLLBACKS, |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 392 | "reloadPersistedData"); |
| 393 | |
Richard Uhler | 74ee299 | 2019-07-01 15:21:44 +0100 | [diff] [blame] | 394 | CountDownLatch latch = new CountDownLatch(1); |
shafik | 6d61f5e | 2019-02-26 09:33:26 +0000 | [diff] [blame] | 395 | getHandler().post(() -> { |
| 396 | updateRollbackLifetimeDurationInMillis(); |
Richard Uhler | 74ee299 | 2019-07-01 15:21:44 +0100 | [diff] [blame] | 397 | synchronized (mLock) { |
| 398 | mRollbacks.clear(); |
Richard Uhler | 8d512ee | 2019-07-19 14:55:01 +0100 | [diff] [blame] | 399 | mRollbacks.addAll(mRollbackStore.loadRollbacks()); |
Richard Uhler | 74ee299 | 2019-07-01 15:21:44 +0100 | [diff] [blame] | 400 | } |
| 401 | latch.countDown(); |
shafik | 6d61f5e | 2019-02-26 09:33:26 +0000 | [diff] [blame] | 402 | }); |
Richard Uhler | 74ee299 | 2019-07-01 15:21:44 +0100 | [diff] [blame] | 403 | |
| 404 | try { |
| 405 | latch.await(); |
| 406 | } catch (InterruptedException ie) { |
| 407 | throw new IllegalStateException("RollbackManagerHandlerThread interrupted"); |
| 408 | } |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | @Override |
| 412 | public void expireRollbackForPackage(String packageName) { |
| 413 | mContext.enforceCallingOrSelfPermission( |
Richard Uhler | 1fc10c1 | 2019-03-18 11:38:46 +0000 | [diff] [blame] | 414 | Manifest.permission.TEST_MANAGE_ROLLBACKS, |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 415 | "expireRollbackForPackage"); |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 416 | synchronized (mLock) { |
Richard Uhler | 8d512ee | 2019-07-19 14:55:01 +0100 | [diff] [blame] | 417 | Iterator<Rollback> iter = mRollbacks.iterator(); |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 418 | while (iter.hasNext()) { |
Richard Uhler | 8d512ee | 2019-07-19 14:55:01 +0100 | [diff] [blame] | 419 | Rollback rollback = iter.next(); |
Oli Lan | 4af2f75 | 2019-09-27 10:39:32 +0100 | [diff] [blame] | 420 | if (rollback.includesPackage(packageName)) { |
| 421 | iter.remove(); |
| 422 | rollback.delete(mAppDataRollbackHelper); |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 423 | } |
| 424 | } |
Oli Lan | 0672808 | 2019-08-14 11:45:37 +0100 | [diff] [blame] | 425 | for (NewRollback newRollback : mNewRollbacks) { |
Oli Lan | 4af2f75 | 2019-09-27 10:39:32 +0100 | [diff] [blame] | 426 | if (newRollback.rollback.includesPackage(packageName)) { |
| 427 | newRollback.setCancelled(); |
Oli Lan | 0672808 | 2019-08-14 11:45:37 +0100 | [diff] [blame] | 428 | } |
| 429 | } |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 430 | } |
| 431 | } |
| 432 | |
Richard Uhler | 798b859 | 2019-06-07 14:04:43 +0100 | [diff] [blame] | 433 | @Override |
| 434 | public void blockRollbackManager(long millis) { |
| 435 | mContext.enforceCallingOrSelfPermission( |
| 436 | Manifest.permission.TEST_MANAGE_ROLLBACKS, |
| 437 | "blockRollbackManager"); |
| 438 | getHandler().post(() -> { |
JW Wang | fab29c6 | 2019-11-19 16:17:41 +0800 | [diff] [blame] | 439 | mSleepDuration.addLast(millis); |
| 440 | }); |
| 441 | } |
| 442 | |
| 443 | private void queueSleepIfNeeded() { |
| 444 | if (mSleepDuration.size() == 0) { |
| 445 | return; |
| 446 | } |
| 447 | long millis = mSleepDuration.removeFirst(); |
| 448 | if (millis <= 0) { |
| 449 | return; |
| 450 | } |
| 451 | getHandler().post(() -> { |
Richard Uhler | 798b859 | 2019-06-07 14:04:43 +0100 | [diff] [blame] | 452 | try { |
| 453 | Thread.sleep(millis); |
| 454 | } catch (InterruptedException e) { |
Richard Uhler | 74ee299 | 2019-07-01 15:21:44 +0100 | [diff] [blame] | 455 | throw new IllegalStateException("RollbackManagerHandlerThread interrupted"); |
Richard Uhler | 798b859 | 2019-06-07 14:04:43 +0100 | [diff] [blame] | 456 | } |
| 457 | }); |
| 458 | } |
| 459 | |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 460 | void onUnlockUser(int userId) { |
JW Wang | 12d0c3e | 2019-12-02 21:01:35 +0800 | [diff] [blame] | 461 | if (LOCAL_LOGV) { |
| 462 | Slog.v(TAG, "onUnlockUser id=" + userId); |
| 463 | } |
Gavin Corkery | f115451 | 2019-09-05 11:48:55 +0100 | [diff] [blame] | 464 | // In order to ensure that no package begins running while a backup or restore is taking |
| 465 | // place, onUnlockUser must remain blocked until all pending backups and restores have |
| 466 | // completed. |
| 467 | CountDownLatch latch = new CountDownLatch(1); |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 468 | getHandler().post(() -> { |
Richard Uhler | 8d512ee | 2019-07-19 14:55:01 +0100 | [diff] [blame] | 469 | final List<Rollback> rollbacks; |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 470 | synchronized (mLock) { |
Richard Uhler | 6f8a33b | 2019-02-26 10:40:36 +0000 | [diff] [blame] | 471 | rollbacks = new ArrayList<>(mRollbacks); |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 472 | } |
| 473 | |
Oli Lan | 31f453f | 2019-09-11 09:58:39 +0100 | [diff] [blame] | 474 | for (int i = 0; i < rollbacks.size(); i++) { |
| 475 | Rollback rollback = rollbacks.get(i); |
Oli Lan | 4af2f75 | 2019-09-27 10:39:32 +0100 | [diff] [blame] | 476 | rollback.commitPendingBackupAndRestoreForUser(userId, mAppDataRollbackHelper); |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 477 | } |
Oli Lan | 31f453f | 2019-09-11 09:58:39 +0100 | [diff] [blame] | 478 | |
Gavin Corkery | f115451 | 2019-09-05 11:48:55 +0100 | [diff] [blame] | 479 | latch.countDown(); |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 480 | }); |
Gavin Corkery | f115451 | 2019-09-05 11:48:55 +0100 | [diff] [blame] | 481 | |
| 482 | try { |
| 483 | latch.await(); |
| 484 | } catch (InterruptedException ie) { |
| 485 | throw new IllegalStateException("RollbackManagerHandlerThread interrupted"); |
| 486 | } |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 487 | } |
| 488 | |
shafik | 0ad18b8 | 2019-01-24 16:27:24 +0000 | [diff] [blame] | 489 | private void updateRollbackLifetimeDurationInMillis() { |
Stanislav Zholnin | 6b85dac | 2019-03-07 12:16:04 +0000 | [diff] [blame] | 490 | mRollbackLifetimeDurationInMillis = DeviceConfig.getLong( |
Matt Pape | 12187ae | 2019-03-14 13:37:32 -0700 | [diff] [blame] | 491 | DeviceConfig.NAMESPACE_ROLLBACK_BOOT, |
| 492 | RollbackManager.PROPERTY_ROLLBACK_LIFETIME_MILLIS, |
Stanislav Zholnin | 6b85dac | 2019-03-07 12:16:04 +0000 | [diff] [blame] | 493 | DEFAULT_ROLLBACK_LIFETIME_DURATION_MILLIS); |
shafik | 15c511f | 2019-03-08 11:52:08 +0000 | [diff] [blame] | 494 | if (mRollbackLifetimeDurationInMillis < 0) { |
| 495 | mRollbackLifetimeDurationInMillis = DEFAULT_ROLLBACK_LIFETIME_DURATION_MILLIS; |
| 496 | } |
shafik | 0ad18b8 | 2019-01-24 16:27:24 +0000 | [diff] [blame] | 497 | } |
| 498 | |
Richard Uhler | b2c5cac | 2019-02-05 16:14:35 +0000 | [diff] [blame] | 499 | void onBootCompleted() { |
shafik | 0ad18b8 | 2019-01-24 16:27:24 +0000 | [diff] [blame] | 500 | getHandler().post(() -> updateRollbackLifetimeDurationInMillis()); |
| 501 | // Also posts to handler thread |
| 502 | scheduleExpiration(0); |
| 503 | |
Richard Uhler | b2c5cac | 2019-02-05 16:14:35 +0000 | [diff] [blame] | 504 | getHandler().post(() -> { |
Richard Uhler | 6f8a33b | 2019-02-26 10:40:36 +0000 | [diff] [blame] | 505 | // Check to see if any rollback-enabled staged sessions or staged |
| 506 | // rollback sessions been applied. |
Richard Uhler | 8d512ee | 2019-07-19 14:55:01 +0100 | [diff] [blame] | 507 | List<Rollback> enabling = new ArrayList<>(); |
| 508 | List<Rollback> restoreInProgress = new ArrayList<>(); |
Richard Uhler | 1924d6d | 2019-04-26 10:20:12 +0100 | [diff] [blame] | 509 | Set<String> apexPackageNames = new HashSet<>(); |
Richard Uhler | b2c5cac | 2019-02-05 16:14:35 +0000 | [diff] [blame] | 510 | synchronized (mLock) { |
JW Wang | a4ce433 | 2019-10-29 21:26:03 +0800 | [diff] [blame] | 511 | Iterator<Rollback> iter = mRollbacks.iterator(); |
| 512 | while (iter.hasNext()) { |
| 513 | Rollback rollback = iter.next(); |
| 514 | if (!rollback.isStaged()) { |
| 515 | // We only care about staged rollbacks here |
| 516 | continue; |
| 517 | } |
| 518 | |
| 519 | PackageInstaller.SessionInfo session = mContext.getPackageManager() |
| 520 | .getPackageInstaller().getSessionInfo(rollback.getStagedSessionId()); |
| 521 | if (session == null || session.isStagedSessionFailed()) { |
| 522 | iter.remove(); |
| 523 | rollback.delete(mAppDataRollbackHelper); |
| 524 | continue; |
| 525 | } |
| 526 | |
| 527 | if (session.isStagedSessionApplied()) { |
Oli Lan | 4af2f75 | 2019-09-27 10:39:32 +0100 | [diff] [blame] | 528 | if (rollback.isEnabling()) { |
| 529 | enabling.add(rollback); |
| 530 | } else if (rollback.isRestoreUserDataInProgress()) { |
| 531 | restoreInProgress.add(rollback); |
Richard Uhler | 1924d6d | 2019-04-26 10:20:12 +0100 | [diff] [blame] | 532 | } |
Richard Uhler | b2c5cac | 2019-02-05 16:14:35 +0000 | [diff] [blame] | 533 | } |
JW Wang | a4ce433 | 2019-10-29 21:26:03 +0800 | [diff] [blame] | 534 | apexPackageNames.addAll(rollback.getApexPackageNames()); |
Richard Uhler | b2c5cac | 2019-02-05 16:14:35 +0000 | [diff] [blame] | 535 | } |
| 536 | } |
| 537 | |
Richard Uhler | 8d512ee | 2019-07-19 14:55:01 +0100 | [diff] [blame] | 538 | for (Rollback rollback : enabling) { |
JW Wang | a4ce433 | 2019-10-29 21:26:03 +0800 | [diff] [blame] | 539 | makeRollbackAvailable(rollback); |
Richard Uhler | b2c5cac | 2019-02-05 16:14:35 +0000 | [diff] [blame] | 540 | } |
Richard Uhler | 6f8a33b | 2019-02-26 10:40:36 +0000 | [diff] [blame] | 541 | |
Richard Uhler | 8d512ee | 2019-07-19 14:55:01 +0100 | [diff] [blame] | 542 | for (Rollback rollback : restoreInProgress) { |
JW Wang | a4ce433 | 2019-10-29 21:26:03 +0800 | [diff] [blame] | 543 | rollback.setRestoreUserDataInProgress(false); |
Richard Uhler | 6f8a33b | 2019-02-26 10:40:36 +0000 | [diff] [blame] | 544 | } |
Zimuzo | 841c494 | 2019-03-04 12:31:48 +0000 | [diff] [blame] | 545 | |
Richard Uhler | 1924d6d | 2019-04-26 10:20:12 +0100 | [diff] [blame] | 546 | for (String apexPackageName : apexPackageNames) { |
| 547 | // We will not recieve notifications when an apex is updated, |
| 548 | // so check now in case any rollbacks ought to be expired. The |
| 549 | // onPackagedReplace function is safe to call if the package |
| 550 | // hasn't actually been updated. |
| 551 | onPackageReplaced(apexPackageName); |
| 552 | } |
Oli Lan | 0bf4486 | 2019-08-29 14:25:38 +0100 | [diff] [blame] | 553 | |
| 554 | synchronized (mLock) { |
| 555 | mOrphanedApkSessionIds.clear(); |
| 556 | } |
| 557 | |
shafik | c5805fb9 | 2019-04-29 20:08:07 +0100 | [diff] [blame] | 558 | mPackageHealthObserver.onBootCompletedAsync(); |
Richard Uhler | b2c5cac | 2019-02-05 16:14:35 +0000 | [diff] [blame] | 559 | }); |
| 560 | } |
| 561 | |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 562 | /** |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 563 | * Called when a package has been replaced with a different version. |
| 564 | * Removes all backups for the package not matching the currently |
| 565 | * installed package version. |
| 566 | */ |
| 567 | private void onPackageReplaced(String packageName) { |
| 568 | // TODO: Could this end up incorrectly deleting a rollback for a |
| 569 | // package that is about to be installed? |
Oli Lan | 3143ee3 | 2019-09-18 17:07:21 +0100 | [diff] [blame] | 570 | long installedVersion = getInstalledPackageVersion(packageName); |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 571 | |
| 572 | synchronized (mLock) { |
Richard Uhler | 8d512ee | 2019-07-19 14:55:01 +0100 | [diff] [blame] | 573 | Iterator<Rollback> iter = mRollbacks.iterator(); |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 574 | while (iter.hasNext()) { |
Richard Uhler | 8d512ee | 2019-07-19 14:55:01 +0100 | [diff] [blame] | 575 | Rollback rollback = iter.next(); |
Oli Lan | 4af2f75 | 2019-09-27 10:39:32 +0100 | [diff] [blame] | 576 | // TODO: Should we remove rollbacks in the ENABLING state here? |
| 577 | if ((rollback.isEnabling() || rollback.isAvailable()) |
| 578 | && rollback.includesPackageWithDifferentVersion(packageName, |
| 579 | installedVersion)) { |
| 580 | iter.remove(); |
| 581 | rollback.delete(mAppDataRollbackHelper); |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 582 | } |
| 583 | } |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | /** |
| 588 | * Called when a package has been completely removed from the device. |
| 589 | * Removes all backups and rollback history for the given package. |
| 590 | */ |
| 591 | private void onPackageFullyRemoved(String packageName) { |
| 592 | expireRollbackForPackage(packageName); |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | /** |
| 596 | * Notifies an IntentSender of failure. |
| 597 | * |
| 598 | * @param statusReceiver where to send the failure |
Richard Uhler | 2a48c29 | 2019-01-28 17:33:48 +0000 | [diff] [blame] | 599 | * @param status the RollbackManager.STATUS_* code with the failure. |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 600 | * @param message the failure message. |
| 601 | */ |
Oli Lan | 7363a62 | 2019-09-13 10:06:12 +0100 | [diff] [blame] | 602 | static void sendFailure(Context context, IntentSender statusReceiver, |
| 603 | @RollbackManager.Status int status, String message) { |
shafik | bb771fb | 2019-06-05 14:59:57 +0100 | [diff] [blame] | 604 | Slog.e(TAG, message); |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 605 | try { |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 606 | final Intent fillIn = new Intent(); |
Richard Uhler | 2a48c29 | 2019-01-28 17:33:48 +0000 | [diff] [blame] | 607 | fillIn.putExtra(RollbackManager.EXTRA_STATUS, status); |
| 608 | fillIn.putExtra(RollbackManager.EXTRA_STATUS_MESSAGE, message); |
Oli Lan | 7363a62 | 2019-09-13 10:06:12 +0100 | [diff] [blame] | 609 | statusReceiver.sendIntent(context, 0, fillIn, null, null); |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 610 | } catch (IntentSender.SendIntentException e) { |
| 611 | // Nowhere to send the result back to, so don't bother. |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | // Check to see if anything needs expiration, and if so, expire it. |
| 616 | // Schedules future expiration as appropriate. |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 617 | private void runExpiration() { |
| 618 | Instant now = Instant.now(); |
| 619 | Instant oldest = null; |
| 620 | synchronized (mLock) { |
Richard Uhler | 8d512ee | 2019-07-19 14:55:01 +0100 | [diff] [blame] | 621 | Iterator<Rollback> iter = mRollbacks.iterator(); |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 622 | while (iter.hasNext()) { |
Richard Uhler | 8d512ee | 2019-07-19 14:55:01 +0100 | [diff] [blame] | 623 | Rollback rollback = iter.next(); |
Oli Lan | 4af2f75 | 2019-09-27 10:39:32 +0100 | [diff] [blame] | 624 | if (!rollback.isAvailable()) { |
| 625 | continue; |
| 626 | } |
| 627 | Instant rollbackTimestamp = rollback.getTimestamp(); |
| 628 | if (!now.isBefore( |
| 629 | rollbackTimestamp |
| 630 | .plusMillis(mRollbackLifetimeDurationInMillis))) { |
JW Wang | 12d0c3e | 2019-12-02 21:01:35 +0800 | [diff] [blame] | 631 | if (LOCAL_LOGV) { |
| 632 | Slog.v(TAG, "runExpiration id=" + rollback.info.getRollbackId()); |
| 633 | } |
Oli Lan | 4af2f75 | 2019-09-27 10:39:32 +0100 | [diff] [blame] | 634 | iter.remove(); |
| 635 | rollback.delete(mAppDataRollbackHelper); |
| 636 | } else if (oldest == null || oldest.isAfter(rollbackTimestamp)) { |
| 637 | oldest = rollbackTimestamp; |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 638 | } |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | if (oldest != null) { |
shafik | 0ad18b8 | 2019-01-24 16:27:24 +0000 | [diff] [blame] | 643 | scheduleExpiration(now.until(oldest.plusMillis(mRollbackLifetimeDurationInMillis), |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 644 | ChronoUnit.MILLIS)); |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | /** |
| 649 | * Schedules an expiration check to be run after the given duration in |
| 650 | * milliseconds has gone by. |
| 651 | */ |
| 652 | private void scheduleExpiration(long duration) { |
| 653 | getHandler().postDelayed(() -> runExpiration(), duration); |
| 654 | } |
| 655 | |
| 656 | private Handler getHandler() { |
| 657 | return mHandlerThread.getThreadHandler(); |
| 658 | } |
| 659 | |
Richard Uhler | 2d7c7f0d | 2019-01-04 09:18:21 +0000 | [diff] [blame] | 660 | // Returns true if <code>session</code> has installFlags and code path |
| 661 | // matching the installFlags and new package code path given to |
| 662 | // enableRollback. |
| 663 | private boolean sessionMatchesForEnableRollback(PackageInstaller.SessionInfo session, |
| 664 | int installFlags, File newPackageCodePath) { |
| 665 | if (session == null || session.resolvedBaseCodePath == null) { |
| 666 | return false; |
| 667 | } |
| 668 | |
| 669 | File packageCodePath = new File(session.resolvedBaseCodePath).getParentFile(); |
| 670 | if (newPackageCodePath.equals(packageCodePath) && installFlags == session.installFlags) { |
| 671 | return true; |
| 672 | } |
| 673 | |
| 674 | return false; |
| 675 | } |
| 676 | |
Richard Uhler | 82913b7 | 2019-04-01 13:02:31 +0100 | [diff] [blame] | 677 | private Context getContextAsUser(UserHandle user) { |
| 678 | try { |
| 679 | return mContext.createPackageContextAsUser(mContext.getPackageName(), 0, user); |
| 680 | } catch (PackageManager.NameNotFoundException e) { |
| 681 | return null; |
| 682 | } |
| 683 | } |
| 684 | |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 685 | /** |
| 686 | * Called via broadcast by the package manager when a package is being |
| 687 | * staged for install with rollback enabled. Called before the package has |
| 688 | * been installed. |
| 689 | * |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 690 | * @param installFlags information about what is being installed. |
| 691 | * @param newPackageCodePath path to the package about to be installed. |
Richard Uhler | 82913b7 | 2019-04-01 13:02:31 +0100 | [diff] [blame] | 692 | * @param user the user that owns the install session to enable rollback on. |
shafik | 4831ad7 | 2019-05-03 17:36:42 +0100 | [diff] [blame] | 693 | * @param token the distinct rollback token sent by package manager. |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 694 | * @return true if enabling the rollback succeeds, false otherwise. |
| 695 | */ |
Oli Lan | 48f3cf4 | 2019-08-09 10:25:49 +0100 | [diff] [blame] | 696 | private boolean enableRollback( |
| 697 | int installFlags, File newPackageCodePath, @UserIdInt int user, int token) { |
JW Wang | 12d0c3e | 2019-12-02 21:01:35 +0800 | [diff] [blame] | 698 | if (LOCAL_LOGV) { |
| 699 | Slog.v(TAG, "enableRollback user=" + user + " token=" + token |
| 700 | + " path=" + newPackageCodePath.getAbsolutePath()); |
| 701 | } |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 702 | |
Richard Uhler | f0bdca5 | 2019-01-31 16:43:59 +0000 | [diff] [blame] | 703 | // Find the session id associated with this install. |
| 704 | // TODO: It would be nice if package manager or package installer told |
| 705 | // us the session directly, rather than have to search for it |
| 706 | // ourselves. |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 707 | |
Richard Uhler | 82913b7 | 2019-04-01 13:02:31 +0100 | [diff] [blame] | 708 | // getAllSessions only returns sessions for the associated user. |
| 709 | // Create a context with the right user so we can find the matching |
| 710 | // session. |
| 711 | final Context context = getContextAsUser(UserHandle.of(user)); |
| 712 | if (context == null) { |
shafik | bb771fb | 2019-06-05 14:59:57 +0100 | [diff] [blame] | 713 | Slog.e(TAG, "Unable to create context for install session user."); |
Richard Uhler | 82913b7 | 2019-04-01 13:02:31 +0100 | [diff] [blame] | 714 | return false; |
| 715 | } |
| 716 | |
Richard Uhler | 4756970 | 2019-05-02 12:36:39 +0100 | [diff] [blame] | 717 | PackageInstaller.SessionInfo parentSession = null; |
| 718 | PackageInstaller.SessionInfo packageSession = null; |
Richard Uhler | 82913b7 | 2019-04-01 13:02:31 +0100 | [diff] [blame] | 719 | PackageInstaller installer = context.getPackageManager().getPackageInstaller(); |
Richard Uhler | 2d7c7f0d | 2019-01-04 09:18:21 +0000 | [diff] [blame] | 720 | for (PackageInstaller.SessionInfo info : installer.getAllSessions()) { |
Richard Uhler | f1910c5 | 2019-01-09 14:27:36 +0000 | [diff] [blame] | 721 | if (info.isMultiPackage()) { |
| 722 | for (int childId : info.getChildSessionIds()) { |
| 723 | PackageInstaller.SessionInfo child = installer.getSessionInfo(childId); |
| 724 | if (sessionMatchesForEnableRollback(child, installFlags, newPackageCodePath)) { |
| 725 | // TODO: Check we only have one matching session? |
Richard Uhler | 4756970 | 2019-05-02 12:36:39 +0100 | [diff] [blame] | 726 | parentSession = info; |
| 727 | packageSession = child; |
Richard Uhler | f0bdca5 | 2019-01-31 16:43:59 +0000 | [diff] [blame] | 728 | break; |
Richard Uhler | f1910c5 | 2019-01-09 14:27:36 +0000 | [diff] [blame] | 729 | } |
| 730 | } |
Richard Uhler | f0bdca5 | 2019-01-31 16:43:59 +0000 | [diff] [blame] | 731 | } else if (sessionMatchesForEnableRollback(info, installFlags, newPackageCodePath)) { |
| 732 | // TODO: Check we only have one matching session? |
Richard Uhler | 4756970 | 2019-05-02 12:36:39 +0100 | [diff] [blame] | 733 | parentSession = info; |
| 734 | packageSession = info; |
Richard Uhler | f0bdca5 | 2019-01-31 16:43:59 +0000 | [diff] [blame] | 735 | break; |
Richard Uhler | 2d7c7f0d | 2019-01-04 09:18:21 +0000 | [diff] [blame] | 736 | } |
| 737 | } |
| 738 | |
Richard Uhler | 4756970 | 2019-05-02 12:36:39 +0100 | [diff] [blame] | 739 | if (parentSession == null || packageSession == null) { |
shafik | bb771fb | 2019-06-05 14:59:57 +0100 | [diff] [blame] | 740 | Slog.e(TAG, "Unable to find session for enabled rollback."); |
Richard Uhler | 2d7c7f0d | 2019-01-04 09:18:21 +0000 | [diff] [blame] | 741 | return false; |
| 742 | } |
| 743 | |
Richard Uhler | ac0d0f9 | 2019-02-05 15:53:47 +0000 | [diff] [blame] | 744 | // Check to see if this is the apk session for a staged session with |
| 745 | // rollback enabled. |
Richard Uhler | ac0d0f9 | 2019-02-05 15:53:47 +0000 | [diff] [blame] | 746 | synchronized (mLock) { |
Richard Uhler | 6f8a33b | 2019-02-26 10:40:36 +0000 | [diff] [blame] | 747 | for (int i = 0; i < mRollbacks.size(); ++i) { |
Richard Uhler | 8d512ee | 2019-07-19 14:55:01 +0100 | [diff] [blame] | 748 | Rollback rollback = mRollbacks.get(i); |
Oli Lan | 4af2f75 | 2019-09-27 10:39:32 +0100 | [diff] [blame] | 749 | if (rollback.getApkSessionId() == parentSession.getSessionId()) { |
| 750 | // This is the apk session for a staged session with rollback enabled. We do |
| 751 | // not need to create a new rollback for this session. |
| 752 | return true; |
Richard Uhler | ac0d0f9 | 2019-02-05 15:53:47 +0000 | [diff] [blame] | 753 | } |
| 754 | } |
Richard Uhler | ac0d0f9 | 2019-02-05 15:53:47 +0000 | [diff] [blame] | 755 | } |
| 756 | |
Oli Lan | 0bf4486 | 2019-08-29 14:25:38 +0100 | [diff] [blame] | 757 | // Check to see if this is the apk session for a staged session for which rollback was |
| 758 | // cancelled. |
| 759 | synchronized (mLock) { |
| 760 | if (mOrphanedApkSessionIds.indexOf(parentSession.getSessionId()) != -1) { |
| 761 | Slog.w(TAG, "Not enabling rollback for apk as no matching staged session " |
| 762 | + "rollback exists"); |
| 763 | return false; |
| 764 | } |
| 765 | } |
| 766 | |
Richard Uhler | 4756970 | 2019-05-02 12:36:39 +0100 | [diff] [blame] | 767 | NewRollback newRollback; |
| 768 | synchronized (mLock) { |
| 769 | // See if we already have a NewRollback that contains this package |
| 770 | // session. If not, create a NewRollback for the parent session |
| 771 | // that we will use for all the packages in the session. |
| 772 | newRollback = getNewRollbackForPackageSessionLocked(packageSession.getSessionId()); |
| 773 | if (newRollback == null) { |
| 774 | newRollback = createNewRollbackLocked(parentSession); |
| 775 | mNewRollbacks.add(newRollback); |
| 776 | } |
| 777 | } |
shafik | 4831ad7 | 2019-05-03 17:36:42 +0100 | [diff] [blame] | 778 | newRollback.addToken(token); |
Richard Uhler | 4756970 | 2019-05-02 12:36:39 +0100 | [diff] [blame] | 779 | |
Richard Uhler | 8d512ee | 2019-07-19 14:55:01 +0100 | [diff] [blame] | 780 | return enableRollbackForPackageSession(newRollback.rollback, packageSession); |
Richard Uhler | f0bdca5 | 2019-01-31 16:43:59 +0000 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | /** |
| 784 | * Do code and userdata backups to enable rollback of the given session. |
| 785 | * In case of multiPackage sessions, <code>session</code> should be one of |
| 786 | * the child sessions, not the parent session. |
Richard Uhler | 4756970 | 2019-05-02 12:36:39 +0100 | [diff] [blame] | 787 | * |
| 788 | * @return true on success, false on failure. |
Richard Uhler | f0bdca5 | 2019-01-31 16:43:59 +0000 | [diff] [blame] | 789 | */ |
Richard Uhler | 8d512ee | 2019-07-19 14:55:01 +0100 | [diff] [blame] | 790 | private boolean enableRollbackForPackageSession(Rollback rollback, |
Oli Lan | 48f3cf4 | 2019-08-09 10:25:49 +0100 | [diff] [blame] | 791 | PackageInstaller.SessionInfo session) { |
Richard Uhler | f0bdca5 | 2019-01-31 16:43:59 +0000 | [diff] [blame] | 792 | // TODO: Don't attempt to enable rollback for split installs. |
| 793 | final int installFlags = session.installFlags; |
| 794 | if ((installFlags & PackageManager.INSTALL_ENABLE_ROLLBACK) == 0) { |
shafik | bb771fb | 2019-06-05 14:59:57 +0100 | [diff] [blame] | 795 | Slog.e(TAG, "Rollback is not enabled."); |
Richard Uhler | f0bdca5 | 2019-01-31 16:43:59 +0000 | [diff] [blame] | 796 | return false; |
| 797 | } |
| 798 | if ((installFlags & PackageManager.INSTALL_INSTANT_APP) != 0) { |
shafik | bb771fb | 2019-06-05 14:59:57 +0100 | [diff] [blame] | 799 | Slog.e(TAG, "Rollbacks not supported for instant app install"); |
Richard Uhler | f0bdca5 | 2019-01-31 16:43:59 +0000 | [diff] [blame] | 800 | return false; |
| 801 | } |
| 802 | |
Richard Uhler | fcad49a | 2019-03-13 10:18:46 +0000 | [diff] [blame] | 803 | if (session.resolvedBaseCodePath == null) { |
shafik | bb771fb | 2019-06-05 14:59:57 +0100 | [diff] [blame] | 804 | Slog.e(TAG, "Session code path has not been resolved."); |
Richard Uhler | fcad49a | 2019-03-13 10:18:46 +0000 | [diff] [blame] | 805 | return false; |
| 806 | } |
| 807 | |
Richard Uhler | f0bdca5 | 2019-01-31 16:43:59 +0000 | [diff] [blame] | 808 | // Get information about the package to be installed. |
Oli Lan | 856391f | 2019-09-25 16:08:01 +0100 | [diff] [blame] | 809 | PackageParser.PackageLite newPackage; |
Richard Uhler | f0bdca5 | 2019-01-31 16:43:59 +0000 | [diff] [blame] | 810 | try { |
| 811 | newPackage = PackageParser.parsePackageLite(new File(session.resolvedBaseCodePath), 0); |
| 812 | } catch (PackageParser.PackageParserException e) { |
shafik | bb771fb | 2019-06-05 14:59:57 +0100 | [diff] [blame] | 813 | Slog.e(TAG, "Unable to parse new package", e); |
Richard Uhler | f0bdca5 | 2019-01-31 16:43:59 +0000 | [diff] [blame] | 814 | return false; |
| 815 | } |
| 816 | |
| 817 | String packageName = newPackage.packageName; |
shafik | bb771fb | 2019-06-05 14:59:57 +0100 | [diff] [blame] | 818 | Slog.i(TAG, "Enabling rollback for install of " + packageName |
Narayan Kamath | fcd4a04 | 2019-02-01 14:16:37 +0000 | [diff] [blame] | 819 | + ", session:" + session.sessionId); |
Richard Uhler | f0bdca5 | 2019-01-31 16:43:59 +0000 | [diff] [blame] | 820 | |
Richard Uhler | 1fc10c1 | 2019-03-18 11:38:46 +0000 | [diff] [blame] | 821 | String installerPackageName = session.getInstallerPackageName(); |
| 822 | if (!enableRollbackAllowed(installerPackageName, packageName)) { |
shafik | bb771fb | 2019-06-05 14:59:57 +0100 | [diff] [blame] | 823 | Slog.e(TAG, "Installer " + installerPackageName |
Richard Uhler | 1fc10c1 | 2019-03-18 11:38:46 +0000 | [diff] [blame] | 824 | + " is not allowed to enable rollback on " + packageName); |
| 825 | return false; |
| 826 | } |
| 827 | |
Narayan Kamath | fcd4a04 | 2019-02-01 14:16:37 +0000 | [diff] [blame] | 828 | final boolean isApex = ((installFlags & PackageManager.INSTALL_APEX) != 0); |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 829 | |
| 830 | // Get information about the currently installed package. |
Richard Uhler | 1fc10c1 | 2019-03-18 11:38:46 +0000 | [diff] [blame] | 831 | final PackageInfo pkgInfo; |
Richard Uhler | 1f571c6 | 2019-01-31 15:16:46 +0000 | [diff] [blame] | 832 | try { |
Richard Uhler | 82913b7 | 2019-04-01 13:02:31 +0100 | [diff] [blame] | 833 | pkgInfo = getPackageInfo(packageName); |
Richard Uhler | 1f571c6 | 2019-01-31 15:16:46 +0000 | [diff] [blame] | 834 | } catch (PackageManager.NameNotFoundException e) { |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 835 | // TODO: Support rolling back fresh package installs rather than |
| 836 | // fail here. Test this case. |
shafik | bb771fb | 2019-06-05 14:59:57 +0100 | [diff] [blame] | 837 | Slog.e(TAG, packageName + " is not installed"); |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 838 | return false; |
| 839 | } |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 840 | |
Oli Lan | 856391f | 2019-09-25 16:08:01 +0100 | [diff] [blame] | 841 | ApplicationInfo appInfo = pkgInfo.applicationInfo; |
Oli Lan | 4af2f75 | 2019-09-27 10:39:32 +0100 | [diff] [blame] | 842 | return rollback.enableForPackage(packageName, newPackage.versionCode, |
| 843 | pkgInfo.getLongVersionCode(), isApex, appInfo.sourceDir, |
JW Wang | 82768bf | 2019-12-11 10:05:32 +0800 | [diff] [blame] | 844 | appInfo.splitSourceDirs, session.rollbackDataPolicy); |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 845 | } |
| 846 | |
Narayan Kamath | 869f706 | 2019-01-10 12:24:15 +0000 | [diff] [blame] | 847 | @Override |
Gavin Corkery | ab5ee41 | 2019-05-29 15:33:35 +0100 | [diff] [blame] | 848 | public void snapshotAndRestoreUserData(String packageName, int[] userIds, int appId, |
| 849 | long ceDataInode, String seInfo, int token) { |
Narayan Kamath | 869f706 | 2019-01-10 12:24:15 +0000 | [diff] [blame] | 850 | if (Binder.getCallingUid() != Process.SYSTEM_UID) { |
Gavin Corkery | ab5ee41 | 2019-05-29 15:33:35 +0100 | [diff] [blame] | 851 | throw new SecurityException( |
| 852 | "snapshotAndRestoreUserData may only be called by the system."); |
Narayan Kamath | 869f706 | 2019-01-10 12:24:15 +0000 | [diff] [blame] | 853 | } |
| 854 | |
| 855 | getHandler().post(() -> { |
Oli Lan | 48f3cf4 | 2019-08-09 10:25:49 +0100 | [diff] [blame] | 856 | snapshotUserDataInternal(packageName, userIds); |
Oli Lan | 856391f | 2019-09-25 16:08:01 +0100 | [diff] [blame] | 857 | restoreUserDataInternal(packageName, userIds, appId, seInfo); |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 858 | final PackageManagerInternal pmi = LocalServices.getService( |
| 859 | PackageManagerInternal.class); |
Narayan Kamath | 869f706 | 2019-01-10 12:24:15 +0000 | [diff] [blame] | 860 | pmi.finishPackageInstall(token, false); |
| 861 | }); |
| 862 | } |
| 863 | |
Oli Lan | 48f3cf4 | 2019-08-09 10:25:49 +0100 | [diff] [blame] | 864 | private void snapshotUserDataInternal(String packageName, int[] userIds) { |
JW Wang | 12d0c3e | 2019-12-02 21:01:35 +0800 | [diff] [blame] | 865 | if (LOCAL_LOGV) { |
| 866 | Slog.v(TAG, "snapshotUserData pkg=" + packageName |
| 867 | + " users=" + Arrays.toString(userIds)); |
| 868 | } |
Gavin Corkery | ab5ee41 | 2019-05-29 15:33:35 +0100 | [diff] [blame] | 869 | synchronized (mLock) { |
| 870 | // staged installs |
Gavin Corkery | ab5ee41 | 2019-05-29 15:33:35 +0100 | [diff] [blame] | 871 | for (int i = 0; i < mRollbacks.size(); i++) { |
Richard Uhler | 8d512ee | 2019-07-19 14:55:01 +0100 | [diff] [blame] | 872 | Rollback rollback = mRollbacks.get(i); |
Oli Lan | 4af2f75 | 2019-09-27 10:39:32 +0100 | [diff] [blame] | 873 | rollback.snapshotUserData(packageName, userIds, mAppDataRollbackHelper); |
Gavin Corkery | ab5ee41 | 2019-05-29 15:33:35 +0100 | [diff] [blame] | 874 | } |
| 875 | // non-staged installs |
Gavin Corkery | ab5ee41 | 2019-05-29 15:33:35 +0100 | [diff] [blame] | 876 | for (NewRollback rollback : mNewRollbacks) { |
Oli Lan | 4af2f75 | 2019-09-27 10:39:32 +0100 | [diff] [blame] | 877 | rollback.rollback.snapshotUserData( |
| 878 | packageName, userIds, mAppDataRollbackHelper); |
Gavin Corkery | ab5ee41 | 2019-05-29 15:33:35 +0100 | [diff] [blame] | 879 | } |
| 880 | } |
| 881 | } |
| 882 | |
Oli Lan | 856391f | 2019-09-25 16:08:01 +0100 | [diff] [blame] | 883 | private void restoreUserDataInternal( |
| 884 | String packageName, int[] userIds, int appId, String seInfo) { |
JW Wang | 12d0c3e | 2019-12-02 21:01:35 +0800 | [diff] [blame] | 885 | if (LOCAL_LOGV) { |
| 886 | Slog.v(TAG, "restoreUserData pkg=" + packageName |
| 887 | + " users=" + Arrays.toString(userIds)); |
| 888 | } |
Richard Uhler | 6f8a33b | 2019-02-26 10:40:36 +0000 | [diff] [blame] | 889 | synchronized (mLock) { |
Richard Uhler | 6f8a33b | 2019-02-26 10:40:36 +0000 | [diff] [blame] | 890 | for (int i = 0; i < mRollbacks.size(); ++i) { |
Oli Lan | 856391f | 2019-09-25 16:08:01 +0100 | [diff] [blame] | 891 | Rollback rollback = mRollbacks.get(i); |
Oli Lan | 4af2f75 | 2019-09-27 10:39:32 +0100 | [diff] [blame] | 892 | if (rollback.restoreUserDataForPackageIfInProgress( |
| 893 | packageName, userIds, appId, seInfo, mAppDataRollbackHelper)) { |
| 894 | return; |
Richard Uhler | 6f8a33b | 2019-02-26 10:40:36 +0000 | [diff] [blame] | 895 | } |
| 896 | } |
| 897 | } |
Richard Uhler | 38fab3f | 2019-02-22 16:53:10 +0000 | [diff] [blame] | 898 | } |
| 899 | |
Narayan Kamath | fcd4a04 | 2019-02-01 14:16:37 +0000 | [diff] [blame] | 900 | @Override |
Oli Lan | c72b0bb | 2019-12-02 14:03:55 +0000 | [diff] [blame] | 901 | public int notifyStagedSession(int sessionId) { |
shafik | 3be41a9 | 2019-03-25 14:10:32 +0000 | [diff] [blame] | 902 | if (Binder.getCallingUid() != Process.SYSTEM_UID) { |
| 903 | throw new SecurityException("notifyStagedSession may only be called by the system."); |
| 904 | } |
Oli Lan | c72b0bb | 2019-12-02 14:03:55 +0000 | [diff] [blame] | 905 | final LinkedBlockingQueue<Integer> result = new LinkedBlockingQueue<>(); |
Narayan Kamath | fcd4a04 | 2019-02-01 14:16:37 +0000 | [diff] [blame] | 906 | |
| 907 | // NOTE: We post this runnable on the RollbackManager's binder thread because we'd prefer |
| 908 | // to preserve the invariant that all operations that modify state happen there. |
| 909 | getHandler().post(() -> { |
| 910 | PackageInstaller installer = mContext.getPackageManager().getPackageInstaller(); |
| 911 | |
| 912 | final PackageInstaller.SessionInfo session = installer.getSessionInfo(sessionId); |
| 913 | if (session == null) { |
shafik | bb771fb | 2019-06-05 14:59:57 +0100 | [diff] [blame] | 914 | Slog.e(TAG, "No matching install session for: " + sessionId); |
Oli Lan | c72b0bb | 2019-12-02 14:03:55 +0000 | [diff] [blame] | 915 | result.offer(-1); |
Narayan Kamath | fcd4a04 | 2019-02-01 14:16:37 +0000 | [diff] [blame] | 916 | return; |
| 917 | } |
| 918 | |
Richard Uhler | 4756970 | 2019-05-02 12:36:39 +0100 | [diff] [blame] | 919 | NewRollback newRollback; |
| 920 | synchronized (mLock) { |
| 921 | newRollback = createNewRollbackLocked(session); |
| 922 | } |
| 923 | |
Narayan Kamath | fcd4a04 | 2019-02-01 14:16:37 +0000 | [diff] [blame] | 924 | if (!session.isMultiPackage()) { |
Richard Uhler | 8d512ee | 2019-07-19 14:55:01 +0100 | [diff] [blame] | 925 | if (!enableRollbackForPackageSession(newRollback.rollback, session)) { |
shafik | bb771fb | 2019-06-05 14:59:57 +0100 | [diff] [blame] | 926 | Slog.e(TAG, "Unable to enable rollback for session: " + sessionId); |
Oli Lan | c72b0bb | 2019-12-02 14:03:55 +0000 | [diff] [blame] | 927 | result.offer(-1); |
Narayan Kamath | fcd4a04 | 2019-02-01 14:16:37 +0000 | [diff] [blame] | 928 | return; |
| 929 | } |
| 930 | } else { |
| 931 | for (int childSessionId : session.getChildSessionIds()) { |
| 932 | final PackageInstaller.SessionInfo childSession = |
| 933 | installer.getSessionInfo(childSessionId); |
| 934 | if (childSession == null) { |
shafik | bb771fb | 2019-06-05 14:59:57 +0100 | [diff] [blame] | 935 | Slog.e(TAG, "No matching child install session for: " + childSessionId); |
Oli Lan | c72b0bb | 2019-12-02 14:03:55 +0000 | [diff] [blame] | 936 | result.offer(-1); |
Narayan Kamath | fcd4a04 | 2019-02-01 14:16:37 +0000 | [diff] [blame] | 937 | return; |
| 938 | } |
Richard Uhler | 8d512ee | 2019-07-19 14:55:01 +0100 | [diff] [blame] | 939 | if (!enableRollbackForPackageSession(newRollback.rollback, childSession)) { |
shafik | bb771fb | 2019-06-05 14:59:57 +0100 | [diff] [blame] | 940 | Slog.e(TAG, "Unable to enable rollback for session: " + sessionId); |
Oli Lan | c72b0bb | 2019-12-02 14:03:55 +0000 | [diff] [blame] | 941 | result.offer(-1); |
Narayan Kamath | fcd4a04 | 2019-02-01 14:16:37 +0000 | [diff] [blame] | 942 | return; |
| 943 | } |
| 944 | } |
| 945 | } |
| 946 | |
Oli Lan | c72b0bb | 2019-12-02 14:03:55 +0000 | [diff] [blame] | 947 | Rollback rollback = completeEnableRollback(newRollback, true); |
| 948 | if (rollback == null) { |
| 949 | result.offer(-1); |
| 950 | } else { |
| 951 | result.offer(rollback.info.getRollbackId()); |
| 952 | } |
Narayan Kamath | fcd4a04 | 2019-02-01 14:16:37 +0000 | [diff] [blame] | 953 | }); |
| 954 | |
| 955 | try { |
| 956 | return result.take(); |
| 957 | } catch (InterruptedException ie) { |
shafik | bb771fb | 2019-06-05 14:59:57 +0100 | [diff] [blame] | 958 | Slog.e(TAG, "Interrupted while waiting for notifyStagedSession response"); |
Oli Lan | c72b0bb | 2019-12-02 14:03:55 +0000 | [diff] [blame] | 959 | return -1; |
Narayan Kamath | fcd4a04 | 2019-02-01 14:16:37 +0000 | [diff] [blame] | 960 | } |
| 961 | } |
| 962 | |
Richard Uhler | 6fa7d13 | 2019-02-05 13:55:11 +0000 | [diff] [blame] | 963 | @Override |
| 964 | public void notifyStagedApkSession(int originalSessionId, int apkSessionId) { |
shafik | 3be41a9 | 2019-03-25 14:10:32 +0000 | [diff] [blame] | 965 | if (Binder.getCallingUid() != Process.SYSTEM_UID) { |
| 966 | throw new SecurityException("notifyStagedApkSession may only be called by the system."); |
| 967 | } |
Richard Uhler | ba13ab2 | 2019-02-05 15:27:12 +0000 | [diff] [blame] | 968 | getHandler().post(() -> { |
Richard Uhler | 8d512ee | 2019-07-19 14:55:01 +0100 | [diff] [blame] | 969 | Rollback rollback = null; |
Richard Uhler | ba13ab2 | 2019-02-05 15:27:12 +0000 | [diff] [blame] | 970 | synchronized (mLock) { |
Richard Uhler | 6f8a33b | 2019-02-26 10:40:36 +0000 | [diff] [blame] | 971 | for (int i = 0; i < mRollbacks.size(); ++i) { |
Richard Uhler | 8d512ee | 2019-07-19 14:55:01 +0100 | [diff] [blame] | 972 | Rollback candidate = mRollbacks.get(i); |
Oli Lan | d0c3041 | 2019-09-09 18:21:54 +0100 | [diff] [blame] | 973 | if (candidate.getStagedSessionId() == originalSessionId) { |
Richard Uhler | 8d512ee | 2019-07-19 14:55:01 +0100 | [diff] [blame] | 974 | rollback = candidate; |
Richard Uhler | ba13ab2 | 2019-02-05 15:27:12 +0000 | [diff] [blame] | 975 | break; |
| 976 | } |
| 977 | } |
Oli Lan | 0bf4486 | 2019-08-29 14:25:38 +0100 | [diff] [blame] | 978 | if (rollback == null) { |
| 979 | // Did not find rollback matching originalSessionId. |
| 980 | Slog.e(TAG, "notifyStagedApkSession did not find rollback for session " |
| 981 | + originalSessionId |
| 982 | + ". Adding orphaned apk session " + apkSessionId); |
| 983 | mOrphanedApkSessionIds.add(apkSessionId); |
| 984 | } |
Richard Uhler | ba13ab2 | 2019-02-05 15:27:12 +0000 | [diff] [blame] | 985 | } |
| 986 | |
Richard Uhler | 8d512ee | 2019-07-19 14:55:01 +0100 | [diff] [blame] | 987 | if (rollback != null) { |
Oli Lan | 4af2f75 | 2019-09-27 10:39:32 +0100 | [diff] [blame] | 988 | rollback.setApkSessionId(apkSessionId); |
Richard Uhler | ba13ab2 | 2019-02-05 15:27:12 +0000 | [diff] [blame] | 989 | } |
| 990 | }); |
Richard Uhler | 6fa7d13 | 2019-02-05 13:55:11 +0000 | [diff] [blame] | 991 | } |
| 992 | |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 993 | /** |
Richard Uhler | 1fc10c1 | 2019-03-18 11:38:46 +0000 | [diff] [blame] | 994 | * Returns true if the installer is allowed to enable rollback for the |
| 995 | * given named package, false otherwise. |
| 996 | */ |
| 997 | private boolean enableRollbackAllowed(String installerPackageName, String packageName) { |
| 998 | if (installerPackageName == null) { |
| 999 | return false; |
| 1000 | } |
| 1001 | |
| 1002 | PackageManager pm = mContext.getPackageManager(); |
| 1003 | boolean manageRollbacksGranted = pm.checkPermission( |
| 1004 | Manifest.permission.MANAGE_ROLLBACKS, |
| 1005 | installerPackageName) == PackageManager.PERMISSION_GRANTED; |
| 1006 | |
| 1007 | boolean testManageRollbacksGranted = pm.checkPermission( |
| 1008 | Manifest.permission.TEST_MANAGE_ROLLBACKS, |
| 1009 | installerPackageName) == PackageManager.PERMISSION_GRANTED; |
| 1010 | |
| 1011 | // For now only allow rollbacks for modules or for testing. |
JW Wang | 9b51b73 | 2019-12-11 17:14:15 +0800 | [diff] [blame] | 1012 | return (isRollbackWhitelisted(packageName) && manageRollbacksGranted) |
Richard Uhler | 1fc10c1 | 2019-03-18 11:38:46 +0000 | [diff] [blame] | 1013 | || testManageRollbacksGranted; |
| 1014 | } |
| 1015 | |
| 1016 | /** |
JW Wang | 9b51b73 | 2019-12-11 17:14:15 +0800 | [diff] [blame] | 1017 | * Returns true is this package is eligible for enabling rollback. |
| 1018 | */ |
| 1019 | private boolean isRollbackWhitelisted(String packageName) { |
| 1020 | // TODO: Remove #isModule when the white list is ready. |
| 1021 | return SystemConfig.getInstance().getRollbackWhitelistedPackages().contains(packageName) |
| 1022 | || isModule(packageName); |
| 1023 | } |
| 1024 | /** |
Richard Uhler | 1fc10c1 | 2019-03-18 11:38:46 +0000 | [diff] [blame] | 1025 | * Returns true if the package name is the name of a module. |
| 1026 | */ |
| 1027 | private boolean isModule(String packageName) { |
| 1028 | PackageManager pm = mContext.getPackageManager(); |
| 1029 | final ModuleInfo moduleInfo; |
| 1030 | try { |
| 1031 | moduleInfo = pm.getModuleInfo(packageName, 0); |
| 1032 | } catch (PackageManager.NameNotFoundException e) { |
| 1033 | return false; |
| 1034 | } |
| 1035 | |
| 1036 | return moduleInfo != null; |
| 1037 | } |
| 1038 | |
| 1039 | /** |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 1040 | * Gets the version of the package currently installed. |
Oli Lan | 3143ee3 | 2019-09-18 17:07:21 +0100 | [diff] [blame] | 1041 | * Returns -1 if the package is not currently installed. |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 1042 | */ |
Oli Lan | 3143ee3 | 2019-09-18 17:07:21 +0100 | [diff] [blame] | 1043 | private long getInstalledPackageVersion(String packageName) { |
Oli Lan | 856391f | 2019-09-25 16:08:01 +0100 | [diff] [blame] | 1044 | PackageInfo pkgInfo; |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 1045 | try { |
Richard Uhler | 82913b7 | 2019-04-01 13:02:31 +0100 | [diff] [blame] | 1046 | pkgInfo = getPackageInfo(packageName); |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 1047 | } catch (PackageManager.NameNotFoundException e) { |
Oli Lan | 3143ee3 | 2019-09-18 17:07:21 +0100 | [diff] [blame] | 1048 | return -1; |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
Oli Lan | 3143ee3 | 2019-09-18 17:07:21 +0100 | [diff] [blame] | 1051 | return pkgInfo.getLongVersionCode(); |
Richard Uhler | a7e9b2d | 2019-01-22 17:20:58 +0000 | [diff] [blame] | 1052 | } |
| 1053 | |
Richard Uhler | 82913b7 | 2019-04-01 13:02:31 +0100 | [diff] [blame] | 1054 | /** |
Oli Lan | 856391f | 2019-09-25 16:08:01 +0100 | [diff] [blame] | 1055 | * Gets PackageInfo for the given package. Matches any user and apex. |
| 1056 | * |
| 1057 | * @throws PackageManager.NameNotFoundException if no such package is installed. |
Richard Uhler | 82913b7 | 2019-04-01 13:02:31 +0100 | [diff] [blame] | 1058 | */ |
| 1059 | private PackageInfo getPackageInfo(String packageName) |
| 1060 | throws PackageManager.NameNotFoundException { |
| 1061 | PackageManager pm = mContext.getPackageManager(); |
| 1062 | try { |
| 1063 | // The MATCH_ANY_USER flag doesn't mix well with the MATCH_APEX |
| 1064 | // flag, so make two separate attempts to get the package info. |
| 1065 | // We don't need both flags at the same time because we assume |
| 1066 | // apex files are always installed for all users. |
| 1067 | return pm.getPackageInfo(packageName, PackageManager.MATCH_ANY_USER); |
| 1068 | } catch (PackageManager.NameNotFoundException e) { |
| 1069 | return pm.getPackageInfo(packageName, PackageManager.MATCH_APEX); |
| 1070 | } |
| 1071 | } |
| 1072 | |
Richard Uhler | 2d7c7f0d | 2019-01-04 09:18:21 +0000 | [diff] [blame] | 1073 | private class SessionCallback extends PackageInstaller.SessionCallback { |
| 1074 | |
| 1075 | @Override |
| 1076 | public void onCreated(int sessionId) { } |
| 1077 | |
| 1078 | @Override |
| 1079 | public void onBadgingChanged(int sessionId) { } |
| 1080 | |
| 1081 | @Override |
| 1082 | public void onActiveChanged(int sessionId, boolean active) { } |
| 1083 | |
| 1084 | @Override |
| 1085 | public void onProgressChanged(int sessionId, float progress) { } |
| 1086 | |
| 1087 | @Override |
| 1088 | public void onFinished(int sessionId, boolean success) { |
JW Wang | 12d0c3e | 2019-12-02 21:01:35 +0800 | [diff] [blame] | 1089 | if (LOCAL_LOGV) { |
| 1090 | Slog.v(TAG, "SessionCallback.onFinished id=" + sessionId + " success=" + success); |
| 1091 | } |
Richard Uhler | 4756970 | 2019-05-02 12:36:39 +0100 | [diff] [blame] | 1092 | NewRollback newRollback; |
| 1093 | synchronized (mLock) { |
| 1094 | newRollback = getNewRollbackForPackageSessionLocked(sessionId); |
| 1095 | if (newRollback != null) { |
| 1096 | mNewRollbacks.remove(newRollback); |
| 1097 | } |
| 1098 | } |
| 1099 | |
| 1100 | if (newRollback != null) { |
Richard Uhler | 8d512ee | 2019-07-19 14:55:01 +0100 | [diff] [blame] | 1101 | Rollback rollback = completeEnableRollback(newRollback, success); |
Oli Lan | 4af2f75 | 2019-09-27 10:39:32 +0100 | [diff] [blame] | 1102 | if (rollback != null && !rollback.isStaged()) { |
| 1103 | makeRollbackAvailable(rollback); |
Richard Uhler | 4756970 | 2019-05-02 12:36:39 +0100 | [diff] [blame] | 1104 | } |
Richard Uhler | 2d7c7f0d | 2019-01-04 09:18:21 +0000 | [diff] [blame] | 1105 | } |
JW Wang | fab29c6 | 2019-11-19 16:17:41 +0800 | [diff] [blame] | 1106 | |
| 1107 | // Clear the queue so it will never be leaked to next tests. |
| 1108 | mSleepDuration.clear(); |
Narayan Kamath | fcd4a04 | 2019-02-01 14:16:37 +0000 | [diff] [blame] | 1109 | } |
| 1110 | } |
Richard Uhler | 2d7c7f0d | 2019-01-04 09:18:21 +0000 | [diff] [blame] | 1111 | |
Richard Uhler | 6f8a33b | 2019-02-26 10:40:36 +0000 | [diff] [blame] | 1112 | /** |
Oli Lan | 8c0084d | 2019-09-11 13:38:43 +0100 | [diff] [blame] | 1113 | * Add a rollback to the list of rollbacks. This should be called after rollback has been |
| 1114 | * enabled for all packages in the rollback. It does not make the rollback available yet. |
| 1115 | * |
Richard Uhler | 8d512ee | 2019-07-19 14:55:01 +0100 | [diff] [blame] | 1116 | * @return the Rollback instance for a successfully enable-completed rollback, |
Richard Uhler | 4756970 | 2019-05-02 12:36:39 +0100 | [diff] [blame] | 1117 | * or null on error. |
Richard Uhler | 6f8a33b | 2019-02-26 10:40:36 +0000 | [diff] [blame] | 1118 | */ |
Richard Uhler | 8d512ee | 2019-07-19 14:55:01 +0100 | [diff] [blame] | 1119 | private Rollback completeEnableRollback(NewRollback newRollback, boolean success) { |
| 1120 | Rollback rollback = newRollback.rollback; |
JW Wang | 12d0c3e | 2019-12-02 21:01:35 +0800 | [diff] [blame] | 1121 | if (LOCAL_LOGV) { |
| 1122 | Slog.v(TAG, "completeEnableRollback id=" |
| 1123 | + rollback.info.getRollbackId() + " success=" + success); |
| 1124 | } |
Oli Lan | 4af2f75 | 2019-09-27 10:39:32 +0100 | [diff] [blame] | 1125 | if (!success) { |
| 1126 | // The install session was aborted, clean up the pending install. |
| 1127 | rollback.delete(mAppDataRollbackHelper); |
| 1128 | return null; |
Oli Lan | 8c0084d | 2019-09-11 13:38:43 +0100 | [diff] [blame] | 1129 | } |
Oli Lan | 4af2f75 | 2019-09-27 10:39:32 +0100 | [diff] [blame] | 1130 | |
| 1131 | if (newRollback.isCancelled()) { |
| 1132 | Slog.e(TAG, "Rollback has been cancelled by PackageManager"); |
| 1133 | rollback.delete(mAppDataRollbackHelper); |
| 1134 | return null; |
| 1135 | } |
| 1136 | |
| 1137 | if (rollback.getPackageCount() != newRollback.getPackageSessionIdCount()) { |
| 1138 | Slog.e(TAG, "Failed to enable rollback for all packages in session."); |
| 1139 | rollback.delete(mAppDataRollbackHelper); |
| 1140 | return null; |
| 1141 | } |
| 1142 | |
| 1143 | rollback.saveRollback(); |
Richard Uhler | 035e974 | 2019-01-09 13:11:07 +0000 | [diff] [blame] | 1144 | synchronized (mLock) { |
Richard Uhler | 6f8a33b | 2019-02-26 10:40:36 +0000 | [diff] [blame] | 1145 | // Note: There is a small window of time between when |
| 1146 | // the session has been committed by the package |
| 1147 | // manager and when we make the rollback available |
| 1148 | // here. Presumably the window is small enough that |
| 1149 | // nobody will want to roll back the newly installed |
| 1150 | // package before we make the rollback available. |
Richard Uhler | 8d512ee | 2019-07-19 14:55:01 +0100 | [diff] [blame] | 1151 | // TODO: We'll lose the rollback if the |
Richard Uhler | 6f8a33b | 2019-02-26 10:40:36 +0000 | [diff] [blame] | 1152 | // device reboots between when the session is |
| 1153 | // committed and this point. Revisit this after |
| 1154 | // adding support for rollback of staged installs. |
Richard Uhler | 8d512ee | 2019-07-19 14:55:01 +0100 | [diff] [blame] | 1155 | mRollbacks.add(rollback); |
Richard Uhler | 035e974 | 2019-01-09 13:11:07 +0000 | [diff] [blame] | 1156 | } |
Richard Uhler | 6f8a33b | 2019-02-26 10:40:36 +0000 | [diff] [blame] | 1157 | |
Richard Uhler | 8d512ee | 2019-07-19 14:55:01 +0100 | [diff] [blame] | 1158 | return rollback; |
Richard Uhler | 6f8a33b | 2019-02-26 10:40:36 +0000 | [diff] [blame] | 1159 | } |
| 1160 | |
Oli Lan | 8c0084d | 2019-09-11 13:38:43 +0100 | [diff] [blame] | 1161 | @GuardedBy("rollback.getLock") |
Richard Uhler | 8d512ee | 2019-07-19 14:55:01 +0100 | [diff] [blame] | 1162 | private void makeRollbackAvailable(Rollback rollback) { |
JW Wang | 12d0c3e | 2019-12-02 21:01:35 +0800 | [diff] [blame] | 1163 | if (LOCAL_LOGV) { |
| 1164 | Slog.v(TAG, "makeRollbackAvailable id=" + rollback.info.getRollbackId()); |
| 1165 | } |
Oli Lan | 7363a62 | 2019-09-13 10:06:12 +0100 | [diff] [blame] | 1166 | rollback.makeAvailable(); |
Richard Uhler | 6f8a33b | 2019-02-26 10:40:36 +0000 | [diff] [blame] | 1167 | |
| 1168 | // TODO(zezeozue): Provide API to explicitly start observing instead |
| 1169 | // of doing this for all rollbacks. If we do this for all rollbacks, |
| 1170 | // should document in PackageInstaller.SessionParams#setEnableRollback |
Oli Lan | 7363a62 | 2019-09-13 10:06:12 +0100 | [diff] [blame] | 1171 | // After enabling and committing any rollback, observe packages and |
Richard Uhler | 6f8a33b | 2019-02-26 10:40:36 +0000 | [diff] [blame] | 1172 | // prepare to rollback if packages crashes too frequently. |
Oli Lan | 3143ee3 | 2019-09-18 17:07:21 +0100 | [diff] [blame] | 1173 | mPackageHealthObserver.startObservingHealth(rollback.getPackageNames(), |
Richard Uhler | 6f8a33b | 2019-02-26 10:40:36 +0000 | [diff] [blame] | 1174 | mRollbackLifetimeDurationInMillis); |
| 1175 | scheduleExpiration(mRollbackLifetimeDurationInMillis); |
Richard Uhler | 035e974 | 2019-01-09 13:11:07 +0000 | [diff] [blame] | 1176 | } |
Richard Uhler | b9d5447 | 2019-01-22 12:50:08 +0000 | [diff] [blame] | 1177 | |
Richard Uhler | 0a79b32 | 2019-01-23 13:51:07 +0000 | [diff] [blame] | 1178 | /* |
Richard Uhler | 8d512ee | 2019-07-19 14:55:01 +0100 | [diff] [blame] | 1179 | * Returns the rollback with the given rollbackId, if any. |
Richard Uhler | 0a79b32 | 2019-01-23 13:51:07 +0000 | [diff] [blame] | 1180 | */ |
Richard Uhler | 8d512ee | 2019-07-19 14:55:01 +0100 | [diff] [blame] | 1181 | private Rollback getRollbackForId(int rollbackId) { |
Richard Uhler | 0a79b32 | 2019-01-23 13:51:07 +0000 | [diff] [blame] | 1182 | synchronized (mLock) { |
Richard Uhler | 6f8a33b | 2019-02-26 10:40:36 +0000 | [diff] [blame] | 1183 | for (int i = 0; i < mRollbacks.size(); ++i) { |
Richard Uhler | 8d512ee | 2019-07-19 14:55:01 +0100 | [diff] [blame] | 1184 | Rollback rollback = mRollbacks.get(i); |
| 1185 | if (rollback.info.getRollbackId() == rollbackId) { |
| 1186 | return rollback; |
Richard Uhler | 0a79b32 | 2019-01-23 13:51:07 +0000 | [diff] [blame] | 1187 | } |
| 1188 | } |
| 1189 | } |
Narayan Kamath | c034fe9 | 2019-01-23 10:48:17 +0000 | [diff] [blame] | 1190 | |
| 1191 | return null; |
| 1192 | } |
| 1193 | |
Richard Uhler | b9d5447 | 2019-01-22 12:50:08 +0000 | [diff] [blame] | 1194 | @GuardedBy("mLock") |
Richard Uhler | 4756970 | 2019-05-02 12:36:39 +0100 | [diff] [blame] | 1195 | private int allocateRollbackIdLocked() { |
Richard Uhler | b9d5447 | 2019-01-22 12:50:08 +0000 | [diff] [blame] | 1196 | int n = 0; |
| 1197 | int rollbackId; |
| 1198 | do { |
| 1199 | rollbackId = mRandom.nextInt(Integer.MAX_VALUE - 1) + 1; |
| 1200 | if (!mAllocatedRollbackIds.get(rollbackId, false)) { |
| 1201 | mAllocatedRollbackIds.put(rollbackId, true); |
| 1202 | return rollbackId; |
| 1203 | } |
| 1204 | } while (n++ < 32); |
| 1205 | |
Richard Uhler | 4756970 | 2019-05-02 12:36:39 +0100 | [diff] [blame] | 1206 | throw new IllegalStateException("Failed to allocate rollback ID"); |
Richard Uhler | b9d5447 | 2019-01-22 12:50:08 +0000 | [diff] [blame] | 1207 | } |
Nikita Ioffe | 952aa7b | 2019-01-28 19:49:56 +0000 | [diff] [blame] | 1208 | |
shafik | 6004600 | 2019-03-12 17:54:10 +0000 | [diff] [blame] | 1209 | @Override |
| 1210 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Oli Lan | 8753d82 | 2019-11-01 11:33:53 +0000 | [diff] [blame] | 1211 | if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return; |
| 1212 | |
shafik | 6004600 | 2019-03-12 17:54:10 +0000 | [diff] [blame] | 1213 | IndentingPrintWriter ipw = new IndentingPrintWriter(pw, " "); |
| 1214 | synchronized (mLock) { |
Richard Uhler | 8d512ee | 2019-07-19 14:55:01 +0100 | [diff] [blame] | 1215 | for (Rollback rollback : mRollbacks) { |
Oli Lan | 4af2f75 | 2019-09-27 10:39:32 +0100 | [diff] [blame] | 1216 | rollback.dump(ipw); |
shafik | 6004600 | 2019-03-12 17:54:10 +0000 | [diff] [blame] | 1217 | } |
Gavin Corkery | efb3ff1 | 2019-12-02 18:15:24 +0000 | [diff] [blame] | 1218 | ipw.println(); |
| 1219 | PackageWatchdog.getInstance(mContext).dump(ipw); |
shafik | 6004600 | 2019-03-12 17:54:10 +0000 | [diff] [blame] | 1220 | } |
| 1221 | } |
Richard Uhler | 1fc10c1 | 2019-03-18 11:38:46 +0000 | [diff] [blame] | 1222 | |
| 1223 | private void enforceManageRollbacks(@NonNull String message) { |
| 1224 | if ((PackageManager.PERMISSION_GRANTED != mContext.checkCallingOrSelfPermission( |
| 1225 | Manifest.permission.MANAGE_ROLLBACKS)) |
| 1226 | && (PackageManager.PERMISSION_GRANTED != mContext.checkCallingOrSelfPermission( |
| 1227 | Manifest.permission.TEST_MANAGE_ROLLBACKS))) { |
| 1228 | throw new SecurityException(message + " requires " |
| 1229 | + Manifest.permission.MANAGE_ROLLBACKS + " or " |
| 1230 | + Manifest.permission.TEST_MANAGE_ROLLBACKS); |
| 1231 | } |
| 1232 | } |
Richard Uhler | 4756970 | 2019-05-02 12:36:39 +0100 | [diff] [blame] | 1233 | |
| 1234 | private static class NewRollback { |
Richard Uhler | 8d512ee | 2019-07-19 14:55:01 +0100 | [diff] [blame] | 1235 | public final Rollback rollback; |
Richard Uhler | 4756970 | 2019-05-02 12:36:39 +0100 | [diff] [blame] | 1236 | |
| 1237 | /** |
Oli Lan | 4af2f75 | 2019-09-27 10:39:32 +0100 | [diff] [blame] | 1238 | * This array holds all of the rollback tokens associated with package sessions included in |
| 1239 | * this rollback. |
shafik | 4831ad7 | 2019-05-03 17:36:42 +0100 | [diff] [blame] | 1240 | */ |
Oli Lan | 4af2f75 | 2019-09-27 10:39:32 +0100 | [diff] [blame] | 1241 | @GuardedBy("mNewRollbackLock") |
shafik | 4831ad7 | 2019-05-03 17:36:42 +0100 | [diff] [blame] | 1242 | private final IntArray mTokens = new IntArray(); |
| 1243 | |
| 1244 | /** |
Oli Lan | 4af2f75 | 2019-09-27 10:39:32 +0100 | [diff] [blame] | 1245 | * Session ids for all packages in the install. For multi-package sessions, this is the list |
| 1246 | * of child session ids. For normal sessions, this list is a single element with the normal |
Richard Uhler | 4756970 | 2019-05-02 12:36:39 +0100 | [diff] [blame] | 1247 | * session id. |
| 1248 | */ |
Oli Lan | 4af2f75 | 2019-09-27 10:39:32 +0100 | [diff] [blame] | 1249 | private final int[] mPackageSessionIds; |
| 1250 | |
| 1251 | @GuardedBy("mNewRollbackLock") |
| 1252 | private boolean mIsCancelled = false; |
| 1253 | |
| 1254 | private final Object mNewRollbackLock = new Object(); |
| 1255 | |
| 1256 | NewRollback(Rollback rollback, int[] packageSessionIds) { |
| 1257 | this.rollback = rollback; |
| 1258 | this.mPackageSessionIds = packageSessionIds; |
| 1259 | } |
Richard Uhler | 4756970 | 2019-05-02 12:36:39 +0100 | [diff] [blame] | 1260 | |
shafik | 4831ad7 | 2019-05-03 17:36:42 +0100 | [diff] [blame] | 1261 | /** |
Oli Lan | 4af2f75 | 2019-09-27 10:39:32 +0100 | [diff] [blame] | 1262 | * Adds a rollback token to be associated with this NewRollback. This may be used to |
| 1263 | * identify which rollback should be cancelled in case {@link PackageManager} sends an |
| 1264 | * {@link Intent#ACTION_CANCEL_ENABLE_ROLLBACK} intent. |
| 1265 | */ |
| 1266 | void addToken(int token) { |
| 1267 | synchronized (mNewRollbackLock) { |
| 1268 | mTokens.add(token); |
| 1269 | } |
| 1270 | } |
| 1271 | |
| 1272 | /** |
| 1273 | * Returns true if this NewRollback is associated with the provided {@code token}. |
| 1274 | */ |
| 1275 | boolean hasToken(int token) { |
| 1276 | synchronized (mNewRollbackLock) { |
| 1277 | return mTokens.indexOf(token) != -1; |
| 1278 | } |
| 1279 | } |
| 1280 | |
| 1281 | /** |
| 1282 | * Returns true if this NewRollback has been cancelled. |
shafik | 4831ad7 | 2019-05-03 17:36:42 +0100 | [diff] [blame] | 1283 | * |
Richard Uhler | 8d512ee | 2019-07-19 14:55:01 +0100 | [diff] [blame] | 1284 | * <p>Rollback could be invalidated and cancelled if RollbackManager receives |
shafik | 4831ad7 | 2019-05-03 17:36:42 +0100 | [diff] [blame] | 1285 | * {@link Intent#ACTION_CANCEL_ENABLE_ROLLBACK} from {@link PackageManager}. |
| 1286 | * |
| 1287 | * <p>The main underlying assumption here is that if enabling the rollback times out, then |
| 1288 | * {@link PackageManager} will NOT send |
| 1289 | * {@link PackageInstaller.SessionCallback#onFinished(int, boolean)} before it broadcasts |
| 1290 | * {@link Intent#ACTION_CANCEL_ENABLE_ROLLBACK}. |
| 1291 | */ |
Oli Lan | 4af2f75 | 2019-09-27 10:39:32 +0100 | [diff] [blame] | 1292 | boolean isCancelled() { |
| 1293 | synchronized (mNewRollbackLock) { |
| 1294 | return mIsCancelled; |
| 1295 | } |
Richard Uhler | 4756970 | 2019-05-02 12:36:39 +0100 | [diff] [blame] | 1296 | } |
shafik | 4831ad7 | 2019-05-03 17:36:42 +0100 | [diff] [blame] | 1297 | |
Oli Lan | 4af2f75 | 2019-09-27 10:39:32 +0100 | [diff] [blame] | 1298 | /** |
| 1299 | * Sets this NewRollback to be marked as cancelled. |
| 1300 | */ |
| 1301 | void setCancelled() { |
| 1302 | synchronized (mNewRollbackLock) { |
| 1303 | mIsCancelled = true; |
| 1304 | } |
shafik | 4831ad7 | 2019-05-03 17:36:42 +0100 | [diff] [blame] | 1305 | } |
| 1306 | |
Oli Lan | 4af2f75 | 2019-09-27 10:39:32 +0100 | [diff] [blame] | 1307 | /** |
| 1308 | * Returns true if this NewRollback contains the provided {@code packageSessionId}. |
| 1309 | */ |
| 1310 | boolean containsSessionId(int packageSessionId) { |
| 1311 | for (int id : mPackageSessionIds) { |
| 1312 | if (id == packageSessionId) { |
| 1313 | return true; |
| 1314 | } |
| 1315 | } |
| 1316 | return false; |
| 1317 | } |
| 1318 | |
| 1319 | /** |
| 1320 | * Returns the number of package session ids in this NewRollback. |
| 1321 | */ |
| 1322 | int getPackageSessionIdCount() { |
| 1323 | return mPackageSessionIds.length; |
shafik | 4831ad7 | 2019-05-03 17:36:42 +0100 | [diff] [blame] | 1324 | } |
Richard Uhler | 4756970 | 2019-05-02 12:36:39 +0100 | [diff] [blame] | 1325 | } |
| 1326 | |
Oli Lan | 8c0084d | 2019-09-11 13:38:43 +0100 | [diff] [blame] | 1327 | @GuardedBy("mLock") |
| 1328 | private NewRollback createNewRollbackLocked(PackageInstaller.SessionInfo parentSession) { |
Richard Uhler | 4756970 | 2019-05-02 12:36:39 +0100 | [diff] [blame] | 1329 | int rollbackId = allocateRollbackIdLocked(); |
Gavin Corkery | 0987cb0 | 2019-10-07 10:46:55 +0100 | [diff] [blame] | 1330 | final int userId; |
| 1331 | if (parentSession.getUser() == UserHandle.ALL) { |
| 1332 | userId = UserHandle.USER_SYSTEM; |
| 1333 | } else { |
| 1334 | userId = parentSession.getUser().getIdentifier(); |
| 1335 | } |
| 1336 | String installerPackageName = parentSession.getInstallerPackageName(); |
Richard Uhler | 8d512ee | 2019-07-19 14:55:01 +0100 | [diff] [blame] | 1337 | final Rollback rollback; |
Richard Uhler | 4756970 | 2019-05-02 12:36:39 +0100 | [diff] [blame] | 1338 | int parentSessionId = parentSession.getSessionId(); |
| 1339 | |
JW Wang | 12d0c3e | 2019-12-02 21:01:35 +0800 | [diff] [blame] | 1340 | if (LOCAL_LOGV) { |
| 1341 | Slog.v(TAG, "createNewRollback id=" + rollbackId |
| 1342 | + " user=" + userId + " installer=" + installerPackageName); |
| 1343 | } |
| 1344 | |
Richard Uhler | 4756970 | 2019-05-02 12:36:39 +0100 | [diff] [blame] | 1345 | if (parentSession.isStaged()) { |
Gavin Corkery | 0987cb0 | 2019-10-07 10:46:55 +0100 | [diff] [blame] | 1346 | rollback = mRollbackStore.createStagedRollback(rollbackId, parentSessionId, userId, |
| 1347 | installerPackageName); |
Richard Uhler | 4756970 | 2019-05-02 12:36:39 +0100 | [diff] [blame] | 1348 | } else { |
Gavin Corkery | 0987cb0 | 2019-10-07 10:46:55 +0100 | [diff] [blame] | 1349 | rollback = mRollbackStore.createNonStagedRollback(rollbackId, userId, |
| 1350 | installerPackageName); |
Richard Uhler | 4756970 | 2019-05-02 12:36:39 +0100 | [diff] [blame] | 1351 | } |
| 1352 | |
| 1353 | int[] packageSessionIds; |
| 1354 | if (parentSession.isMultiPackage()) { |
| 1355 | packageSessionIds = parentSession.getChildSessionIds(); |
| 1356 | } else { |
| 1357 | packageSessionIds = new int[]{parentSessionId}; |
| 1358 | } |
| 1359 | |
Richard Uhler | 8d512ee | 2019-07-19 14:55:01 +0100 | [diff] [blame] | 1360 | return new NewRollback(rollback, packageSessionIds); |
Richard Uhler | 4756970 | 2019-05-02 12:36:39 +0100 | [diff] [blame] | 1361 | } |
| 1362 | |
| 1363 | /** |
| 1364 | * Returns the NewRollback associated with the given package session. |
| 1365 | * Returns null if no NewRollback is found for the given package |
| 1366 | * session. |
| 1367 | */ |
Oli Lan | 4af2f75 | 2019-09-27 10:39:32 +0100 | [diff] [blame] | 1368 | @GuardedBy("mLock") |
Richard Uhler | 4756970 | 2019-05-02 12:36:39 +0100 | [diff] [blame] | 1369 | NewRollback getNewRollbackForPackageSessionLocked(int packageSessionId) { |
| 1370 | // We expect mNewRollbacks to be a very small list; linear search |
| 1371 | // should be plenty fast. |
Richard Uhler | 8d512ee | 2019-07-19 14:55:01 +0100 | [diff] [blame] | 1372 | for (NewRollback newRollback: mNewRollbacks) { |
Oli Lan | 4af2f75 | 2019-09-27 10:39:32 +0100 | [diff] [blame] | 1373 | if (newRollback.containsSessionId(packageSessionId)) { |
| 1374 | return newRollback; |
Richard Uhler | 4756970 | 2019-05-02 12:36:39 +0100 | [diff] [blame] | 1375 | } |
| 1376 | } |
| 1377 | return null; |
| 1378 | } |
Richard Uhler | e95d055 | 2018-12-27 15:03:41 +0000 | [diff] [blame] | 1379 | } |