Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server.pm; |
| 18 | |
Jeff Sharkey | f060095 | 2014-08-07 17:31:53 -0700 | [diff] [blame] | 19 | import static android.content.pm.PackageManager.INSTALL_FAILED_ABORTED; |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 20 | import static android.content.pm.PackageManager.INSTALL_FAILED_CONTAINER_ERROR; |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 21 | import static android.content.pm.PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 22 | import static android.content.pm.PackageManager.INSTALL_FAILED_INTERNAL_ERROR; |
| 23 | import static android.content.pm.PackageManager.INSTALL_FAILED_INVALID_APK; |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 24 | import static android.system.OsConstants.O_CREAT; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 25 | import static android.system.OsConstants.O_RDONLY; |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 26 | import static android.system.OsConstants.O_WRONLY; |
Jeff Sharkey | d5d5e92 | 2017-02-21 10:51:23 -0700 | [diff] [blame] | 27 | |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 28 | import static com.android.server.pm.PackageInstallerService.prepareExternalStageCid; |
Jeff Sharkey | b2b9ab8 | 2015-04-05 21:10:42 -0700 | [diff] [blame] | 29 | import static com.android.server.pm.PackageInstallerService.prepareStageDir; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 30 | |
Benjamin Franz | 39fb7fd | 2015-02-18 16:11:18 +0000 | [diff] [blame] | 31 | import android.app.admin.DevicePolicyManager; |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 32 | import android.content.Context; |
Jeff Sharkey | fbd0e9f | 2014-08-06 16:34:34 -0700 | [diff] [blame] | 33 | import android.content.Intent; |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 34 | import android.content.IntentSender; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 35 | import android.content.pm.ApplicationInfo; |
| 36 | import android.content.pm.IPackageInstallObserver2; |
| 37 | import android.content.pm.IPackageInstallerSession; |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 38 | import android.content.pm.PackageInfo; |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 39 | import android.content.pm.PackageInstaller; |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 40 | import android.content.pm.PackageInstaller.SessionInfo; |
| 41 | import android.content.pm.PackageInstaller.SessionParams; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 42 | import android.content.pm.PackageManager; |
| 43 | import android.content.pm.PackageParser; |
Jeff Sharkey | c4858a2 | 2014-06-16 10:51:20 -0700 | [diff] [blame] | 44 | import android.content.pm.PackageParser.ApkLite; |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 45 | import android.content.pm.PackageParser.PackageLite; |
Jeff Sharkey | 275e085 | 2014-06-17 18:18:49 -0700 | [diff] [blame] | 46 | import android.content.pm.PackageParser.PackageParserException; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 47 | import android.content.pm.Signature; |
Todd Kennedy | c25fbde | 2016-08-31 15:54:48 -0700 | [diff] [blame] | 48 | import android.os.Binder; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 49 | import android.os.Bundle; |
Jeff Sharkey | 78cc340 | 2014-05-22 10:52:49 -0700 | [diff] [blame] | 50 | import android.os.FileBridge; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 51 | import android.os.FileUtils; |
| 52 | import android.os.Handler; |
| 53 | import android.os.Looper; |
| 54 | import android.os.Message; |
| 55 | import android.os.ParcelFileDescriptor; |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 56 | import android.os.Process; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 57 | import android.os.RemoteException; |
Jeff Sharkey | 02d4e34 | 2017-03-10 21:53:48 -0700 | [diff] [blame] | 58 | import android.os.RevocableFileDescriptor; |
Jeff Sharkey | 57dcf5b | 2014-06-18 17:46:05 -0700 | [diff] [blame] | 59 | import android.os.UserHandle; |
Jeff Sharkey | d5d5e92 | 2017-02-21 10:51:23 -0700 | [diff] [blame] | 60 | import android.os.storage.StorageManager; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 61 | import android.system.ErrnoException; |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 62 | import android.system.Os; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 63 | import android.system.OsConstants; |
| 64 | import android.system.StructStat; |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 65 | import android.text.TextUtils; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 66 | import android.util.ArraySet; |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 67 | import android.util.ExceptionUtils; |
| 68 | import android.util.MathUtils; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 69 | import android.util.Slog; |
| 70 | |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 71 | import com.android.internal.annotations.GuardedBy; |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 72 | import com.android.internal.content.NativeLibraryHelper; |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 73 | import com.android.internal.content.PackageHelper; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 74 | import com.android.internal.util.ArrayUtils; |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 75 | import com.android.internal.util.IndentingPrintWriter; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 76 | import com.android.internal.util.Preconditions; |
Jeff Sharkey | 740f523 | 2016-12-09 14:31:26 -0700 | [diff] [blame] | 77 | import com.android.server.pm.Installer.InstallerException; |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 78 | import com.android.server.pm.PackageInstallerService.PackageInstallObserverAdapter; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 79 | |
Jeff Sharkey | d5d5e92 | 2017-02-21 10:51:23 -0700 | [diff] [blame] | 80 | import libcore.io.IoUtils; |
| 81 | import libcore.io.Libcore; |
| 82 | |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 83 | import java.io.File; |
| 84 | import java.io.FileDescriptor; |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 85 | import java.io.FileFilter; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 86 | import java.io.IOException; |
Todd Kennedy | d9d438a | 2016-04-06 14:08:14 -0700 | [diff] [blame] | 87 | import java.security.cert.Certificate; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 88 | import java.util.ArrayList; |
Narayan Kamath | cd1fc14 | 2015-05-11 13:35:59 +0100 | [diff] [blame] | 89 | import java.util.Arrays; |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 90 | import java.util.List; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 91 | import java.util.concurrent.atomic.AtomicInteger; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 92 | |
| 93 | public class PackageInstallerSession extends IPackageInstallerSession.Stub { |
| 94 | private static final String TAG = "PackageInstaller"; |
Jeff Sharkey | 9a44577 | 2014-07-16 11:32:08 -0700 | [diff] [blame] | 95 | private static final boolean LOGD = true; |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 96 | private static final String REMOVE_SPLIT_MARKER_EXTENSION = ".removed"; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 97 | |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 98 | private static final int MSG_COMMIT = 0; |
| 99 | |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 100 | // TODO: enforce INSTALL_ALLOW_TEST |
| 101 | // TODO: enforce INSTALL_ALLOW_DOWNGRADE |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 102 | |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 103 | private final PackageInstallerService.InternalCallback mCallback; |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 104 | private final Context mContext; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 105 | private final PackageManagerService mPm; |
| 106 | private final Handler mHandler; |
Benjamin Franz | 39fb7fd | 2015-02-18 16:11:18 +0000 | [diff] [blame] | 107 | private final boolean mIsInstallerDeviceOwner; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 108 | |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 109 | final int sessionId; |
| 110 | final int userId; |
| 111 | final String installerPackageName; |
Jeff Sharkey | e980804 | 2014-09-11 21:15:37 -0700 | [diff] [blame] | 112 | final int installerUid; |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 113 | final SessionParams params; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 114 | final long createdMillis; |
Todd Kennedy | f29d07a | 2016-08-08 15:17:43 -0700 | [diff] [blame] | 115 | final int defaultContainerGid; |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 116 | |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 117 | /** Staging location where client data is written. */ |
| 118 | final File stageDir; |
| 119 | final String stageCid; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 120 | |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 121 | private final AtomicInteger mActiveCount = new AtomicInteger(); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 122 | |
| 123 | private final Object mLock = new Object(); |
| 124 | |
| 125 | @GuardedBy("mLock") |
| 126 | private float mClientProgress = 0; |
| 127 | @GuardedBy("mLock") |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 128 | private float mInternalProgress = 0; |
| 129 | |
| 130 | @GuardedBy("mLock") |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 131 | private float mProgress = 0; |
| 132 | @GuardedBy("mLock") |
| 133 | private float mReportedProgress = -1; |
| 134 | |
| 135 | @GuardedBy("mLock") |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 136 | private boolean mPrepared = false; |
| 137 | @GuardedBy("mLock") |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 138 | private boolean mSealed = false; |
| 139 | @GuardedBy("mLock") |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 140 | private boolean mPermissionsAccepted = false; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 141 | @GuardedBy("mLock") |
Jeff Sharkey | 497c052 | 2015-05-12 13:07:14 -0700 | [diff] [blame] | 142 | private boolean mRelinquished = false; |
| 143 | @GuardedBy("mLock") |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 144 | private boolean mDestroyed = false; |
| 145 | |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 146 | private int mFinalStatus; |
| 147 | private String mFinalMessage; |
| 148 | |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 149 | @GuardedBy("mLock") |
Jeff Sharkey | 02d4e34 | 2017-03-10 21:53:48 -0700 | [diff] [blame] | 150 | private final ArrayList<RevocableFileDescriptor> mFds = new ArrayList<>(); |
| 151 | @GuardedBy("mLock") |
| 152 | private final ArrayList<FileBridge> mBridges = new ArrayList<>(); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 153 | |
| 154 | @GuardedBy("mLock") |
| 155 | private IPackageInstallObserver2 mRemoteObserver; |
| 156 | |
| 157 | /** Fields derived from commit parsing */ |
| 158 | private String mPackageName; |
| 159 | private int mVersionCode; |
| 160 | private Signature[] mSignatures; |
Todd Kennedy | d9d438a | 2016-04-06 14:08:14 -0700 | [diff] [blame] | 161 | private Certificate[][] mCertificates; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 162 | |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 163 | /** |
| 164 | * Path to the validated base APK for this session, which may point at an |
| 165 | * APK inside the session (when the session defines the base), or it may |
| 166 | * point at the existing base APK (when adding splits to an existing app). |
| 167 | * <p> |
| 168 | * This is used when confirming permissions, since we can't fully stage the |
| 169 | * session inside an ASEC before confirming with user. |
| 170 | */ |
| 171 | @GuardedBy("mLock") |
| 172 | private File mResolvedBaseFile; |
| 173 | |
| 174 | @GuardedBy("mLock") |
| 175 | private File mResolvedStageDir; |
| 176 | |
| 177 | @GuardedBy("mLock") |
| 178 | private final List<File> mResolvedStagedFiles = new ArrayList<>(); |
| 179 | @GuardedBy("mLock") |
| 180 | private final List<File> mResolvedInheritedFiles = new ArrayList<>(); |
Narayan Kamath | cd1fc14 | 2015-05-11 13:35:59 +0100 | [diff] [blame] | 181 | @GuardedBy("mLock") |
Narayan Kamath | e845a1e | 2015-06-05 11:59:26 +0100 | [diff] [blame] | 182 | private final List<String> mResolvedInstructionSets = new ArrayList<>(); |
| 183 | @GuardedBy("mLock") |
Narayan Kamath | cd1fc14 | 2015-05-11 13:35:59 +0100 | [diff] [blame] | 184 | private File mInheritedFilesBase; |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 185 | |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 186 | private static final FileFilter sAddedFilter = new FileFilter() { |
| 187 | @Override |
| 188 | public boolean accept(File file) { |
| 189 | // Installers can't stage directories, so it's fine to ignore |
| 190 | // entries like "lost+found". |
| 191 | if (file.isDirectory()) return false; |
| 192 | if (file.getName().endsWith(REMOVE_SPLIT_MARKER_EXTENSION)) return false; |
| 193 | return true; |
| 194 | } |
| 195 | }; |
| 196 | private static final FileFilter sRemovedFilter = new FileFilter() { |
| 197 | @Override |
| 198 | public boolean accept(File file) { |
| 199 | if (file.isDirectory()) return false; |
| 200 | if (!file.getName().endsWith(REMOVE_SPLIT_MARKER_EXTENSION)) return false; |
| 201 | return true; |
| 202 | } |
| 203 | }; |
| 204 | |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 205 | private final Handler.Callback mHandlerCallback = new Handler.Callback() { |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 206 | @Override |
| 207 | public boolean handleMessage(Message msg) { |
Todd Kennedy | f29d07a | 2016-08-08 15:17:43 -0700 | [diff] [blame] | 208 | // Cache package manager data without the lock held |
| 209 | final PackageInfo pkgInfo = mPm.getPackageInfo( |
Svet Ganov | a5c867c | 2017-05-15 01:17:05 -0700 | [diff] [blame] | 210 | params.appPackageName, PackageManager.GET_SIGNATURES |
| 211 | | PackageManager.MATCH_STATIC_SHARED_LIBRARIES /*flags*/, userId); |
Todd Kennedy | f29d07a | 2016-08-08 15:17:43 -0700 | [diff] [blame] | 212 | final ApplicationInfo appInfo = mPm.getApplicationInfo( |
| 213 | params.appPackageName, 0, userId); |
| 214 | |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 215 | synchronized (mLock) { |
| 216 | if (msg.obj != null) { |
| 217 | mRemoteObserver = (IPackageInstallObserver2) msg.obj; |
| 218 | } |
| 219 | |
| 220 | try { |
Todd Kennedy | f29d07a | 2016-08-08 15:17:43 -0700 | [diff] [blame] | 221 | commitLocked(pkgInfo, appInfo); |
Jeff Sharkey | e0b0bef | 2014-07-12 15:37:47 -0700 | [diff] [blame] | 222 | } catch (PackageManagerException e) { |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 223 | final String completeMsg = ExceptionUtils.getCompleteMessage(e); |
| 224 | Slog.e(TAG, "Commit of session " + sessionId + " failed: " + completeMsg); |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 225 | destroyInternal(); |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 226 | dispatchSessionFinished(e.error, completeMsg, null); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | return true; |
| 230 | } |
| 231 | } |
| 232 | }; |
| 233 | |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 234 | public PackageInstallerSession(PackageInstallerService.InternalCallback callback, |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 235 | Context context, PackageManagerService pm, Looper looper, int sessionId, int userId, |
Jeff Sharkey | e980804 | 2014-09-11 21:15:37 -0700 | [diff] [blame] | 236 | String installerPackageName, int installerUid, SessionParams params, long createdMillis, |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 237 | File stageDir, String stageCid, boolean prepared, boolean sealed) { |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 238 | mCallback = callback; |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 239 | mContext = context; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 240 | mPm = pm; |
| 241 | mHandler = new Handler(looper, mHandlerCallback); |
| 242 | |
| 243 | this.sessionId = sessionId; |
| 244 | this.userId = userId; |
| 245 | this.installerPackageName = installerPackageName; |
Jeff Sharkey | e980804 | 2014-09-11 21:15:37 -0700 | [diff] [blame] | 246 | this.installerUid = installerUid; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 247 | this.params = params; |
| 248 | this.createdMillis = createdMillis; |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 249 | this.stageDir = stageDir; |
| 250 | this.stageCid = stageCid; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 251 | |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 252 | if ((stageDir == null) == (stageCid == null)) { |
Jeff Sharkey | bb7b7be | 2014-08-19 16:18:28 -0700 | [diff] [blame] | 253 | throw new IllegalArgumentException( |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 254 | "Exactly one of stageDir or stageCid stage must be set"); |
Jeff Sharkey | bb7b7be | 2014-08-19 16:18:28 -0700 | [diff] [blame] | 255 | } |
| 256 | |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 257 | mPrepared = prepared; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 258 | mSealed = sealed; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 259 | |
Benjamin Franz | 39fb7fd | 2015-02-18 16:11:18 +0000 | [diff] [blame] | 260 | // Device owners are allowed to silently install packages, so the permission check is |
| 261 | // waived if the installer is the device owner. |
| 262 | DevicePolicyManager dpm = (DevicePolicyManager) mContext.getSystemService( |
| 263 | Context.DEVICE_POLICY_SERVICE); |
Todd Kennedy | a1d12cf | 2015-09-29 15:43:00 -0700 | [diff] [blame] | 264 | final boolean isPermissionGranted = |
| 265 | (mPm.checkUidPermission(android.Manifest.permission.INSTALL_PACKAGES, installerUid) |
| 266 | == PackageManager.PERMISSION_GRANTED); |
| 267 | final boolean isInstallerRoot = (installerUid == Process.ROOT_UID); |
| 268 | final boolean forcePermissionPrompt = |
| 269 | (params.installFlags & PackageManager.INSTALL_FORCE_PERMISSION_PROMPT) != 0; |
Makoto Onuki | c8a5a55 | 2015-11-19 14:29:12 -0800 | [diff] [blame] | 270 | mIsInstallerDeviceOwner = (dpm != null) && dpm.isDeviceOwnerAppOnCallingUser( |
| 271 | installerPackageName); |
Todd Kennedy | a1d12cf | 2015-09-29 15:43:00 -0700 | [diff] [blame] | 272 | if ((isPermissionGranted |
| 273 | || isInstallerRoot |
| 274 | || mIsInstallerDeviceOwner) |
| 275 | && !forcePermissionPrompt) { |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 276 | mPermissionsAccepted = true; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 277 | } else { |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 278 | mPermissionsAccepted = false; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 279 | } |
Todd Kennedy | c25fbde | 2016-08-31 15:54:48 -0700 | [diff] [blame] | 280 | final long identity = Binder.clearCallingIdentity(); |
| 281 | try { |
| 282 | final int uid = mPm.getPackageUid(PackageManagerService.DEFAULT_CONTAINER_PACKAGE, |
| 283 | PackageManager.MATCH_SYSTEM_ONLY, UserHandle.USER_SYSTEM); |
| 284 | defaultContainerGid = UserHandle.getSharedAppGid(uid); |
| 285 | } finally { |
| 286 | Binder.restoreCallingIdentity(identity); |
| 287 | } |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 288 | } |
| 289 | |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 290 | public SessionInfo generateInfo() { |
| 291 | final SessionInfo info = new SessionInfo(); |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 292 | synchronized (mLock) { |
| 293 | info.sessionId = sessionId; |
| 294 | info.installerPackageName = installerPackageName; |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 295 | info.resolvedBaseCodePath = (mResolvedBaseFile != null) ? |
| 296 | mResolvedBaseFile.getAbsolutePath() : null; |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 297 | info.progress = mProgress; |
| 298 | info.sealed = mSealed; |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 299 | info.active = mActiveCount.get() > 0; |
Jeff Sharkey | bb58067 | 2014-07-10 12:10:25 -0700 | [diff] [blame] | 300 | |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 301 | info.mode = params.mode; |
Sunny Goyal | 6d7cb23 | 2017-01-30 10:43:18 -0800 | [diff] [blame] | 302 | info.installReason = params.installReason; |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 303 | info.sizeBytes = params.sizeBytes; |
| 304 | info.appPackageName = params.appPackageName; |
| 305 | info.appIcon = params.appIcon; |
| 306 | info.appLabel = params.appLabel; |
| 307 | } |
Jeff Sharkey | bb58067 | 2014-07-10 12:10:25 -0700 | [diff] [blame] | 308 | return info; |
| 309 | } |
| 310 | |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 311 | public boolean isPrepared() { |
| 312 | synchronized (mLock) { |
| 313 | return mPrepared; |
| 314 | } |
| 315 | } |
| 316 | |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 317 | public boolean isSealed() { |
| 318 | synchronized (mLock) { |
| 319 | return mSealed; |
| 320 | } |
| 321 | } |
| 322 | |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 323 | private void assertPreparedAndNotSealed(String cookie) { |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 324 | synchronized (mLock) { |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 325 | if (!mPrepared) { |
| 326 | throw new IllegalStateException(cookie + " before prepared"); |
| 327 | } |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 328 | if (mSealed) { |
| 329 | throw new SecurityException(cookie + " not allowed after commit"); |
| 330 | } |
| 331 | } |
| 332 | } |
| 333 | |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 334 | /** |
| 335 | * Resolve the actual location where staged data should be written. This |
| 336 | * might point at an ASEC mount point, which is why we delay path resolution |
| 337 | * until someone actively works with the session. |
| 338 | */ |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 339 | private File resolveStageDir() throws IOException { |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 340 | synchronized (mLock) { |
| 341 | if (mResolvedStageDir == null) { |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 342 | if (stageDir != null) { |
| 343 | mResolvedStageDir = stageDir; |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 344 | } else { |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 345 | final String path = PackageHelper.getSdDir(stageCid); |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 346 | if (path != null) { |
| 347 | mResolvedStageDir = new File(path); |
| 348 | } else { |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 349 | throw new IOException("Failed to resolve path to container " + stageCid); |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | } |
| 353 | return mResolvedStageDir; |
| 354 | } |
| 355 | } |
| 356 | |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 357 | @Override |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 358 | public void setClientProgress(float progress) { |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 359 | synchronized (mLock) { |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 360 | // Always publish first staging movement |
| 361 | final boolean forcePublish = (mClientProgress == 0); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 362 | mClientProgress = progress; |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 363 | computeProgressLocked(forcePublish); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 364 | } |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | @Override |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 368 | public void addClientProgress(float progress) { |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 369 | synchronized (mLock) { |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 370 | setClientProgress(mClientProgress + progress); |
Jeff Sharkey | ec9bad2 | 2014-09-05 09:45:20 -0700 | [diff] [blame] | 371 | } |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 372 | } |
| 373 | |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 374 | private void computeProgressLocked(boolean forcePublish) { |
| 375 | mProgress = MathUtils.constrain(mClientProgress * 0.8f, 0f, 0.8f) |
| 376 | + MathUtils.constrain(mInternalProgress * 0.2f, 0f, 0.2f); |
| 377 | |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 378 | // Only publish when meaningful change |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 379 | if (forcePublish || Math.abs(mProgress - mReportedProgress) >= 0.01) { |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 380 | mReportedProgress = mProgress; |
| 381 | mCallback.onSessionProgressChanged(this, mProgress); |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | @Override |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 386 | public String[] getNames() { |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 387 | assertPreparedAndNotSealed("getNames"); |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 388 | try { |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 389 | return resolveStageDir().list(); |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 390 | } catch (IOException e) { |
| 391 | throw ExceptionUtils.wrap(e); |
| 392 | } |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | @Override |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 396 | public void removeSplit(String splitName) { |
| 397 | if (TextUtils.isEmpty(params.appPackageName)) { |
| 398 | throw new IllegalStateException("Must specify package name to remove a split"); |
| 399 | } |
| 400 | try { |
| 401 | createRemoveSplitMarker(splitName); |
| 402 | } catch (IOException e) { |
| 403 | throw ExceptionUtils.wrap(e); |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | private void createRemoveSplitMarker(String splitName) throws IOException { |
| 408 | try { |
| 409 | final String markerName = splitName + REMOVE_SPLIT_MARKER_EXTENSION; |
| 410 | if (!FileUtils.isValidExtFilename(markerName)) { |
| 411 | throw new IllegalArgumentException("Invalid marker: " + markerName); |
| 412 | } |
| 413 | final File target = new File(resolveStageDir(), markerName); |
| 414 | target.createNewFile(); |
| 415 | Os.chmod(target.getAbsolutePath(), 0 /*mode*/); |
| 416 | } catch (ErrnoException e) { |
| 417 | throw e.rethrowAsIOException(); |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | @Override |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 422 | public ParcelFileDescriptor openWrite(String name, long offsetBytes, long lengthBytes) { |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 423 | try { |
| 424 | return openWriteInternal(name, offsetBytes, lengthBytes); |
| 425 | } catch (IOException e) { |
| 426 | throw ExceptionUtils.wrap(e); |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | private ParcelFileDescriptor openWriteInternal(String name, long offsetBytes, long lengthBytes) |
| 431 | throws IOException { |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 432 | // Quick sanity check of state, and allocate a pipe for ourselves. We |
| 433 | // then do heavy disk allocation outside the lock, but this open pipe |
| 434 | // will block any attempted install transitions. |
Jeff Sharkey | 02d4e34 | 2017-03-10 21:53:48 -0700 | [diff] [blame] | 435 | final RevocableFileDescriptor fd; |
Jeff Sharkey | 78cc340 | 2014-05-22 10:52:49 -0700 | [diff] [blame] | 436 | final FileBridge bridge; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 437 | synchronized (mLock) { |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 438 | assertPreparedAndNotSealed("openWrite"); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 439 | |
Jeff Sharkey | 02d4e34 | 2017-03-10 21:53:48 -0700 | [diff] [blame] | 440 | if (PackageInstaller.ENABLE_REVOCABLE_FD) { |
| 441 | fd = new RevocableFileDescriptor(); |
| 442 | bridge = null; |
| 443 | mFds.add(fd); |
| 444 | } else { |
| 445 | fd = null; |
| 446 | bridge = new FileBridge(); |
| 447 | mBridges.add(bridge); |
| 448 | } |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | try { |
| 452 | // Use installer provided name for now; we always rename later |
| 453 | if (!FileUtils.isValidExtFilename(name)) { |
| 454 | throw new IllegalArgumentException("Invalid name: " + name); |
| 455 | } |
Shunta Sato | 4f26cb5 | 2016-06-28 09:29:19 +0900 | [diff] [blame] | 456 | final File target; |
| 457 | final long identity = Binder.clearCallingIdentity(); |
| 458 | try { |
| 459 | target = new File(resolveStageDir(), name); |
| 460 | } finally { |
| 461 | Binder.restoreCallingIdentity(identity); |
| 462 | } |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 463 | |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 464 | // TODO: this should delegate to DCS so the system process avoids |
| 465 | // holding open FDs into containers. |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 466 | final FileDescriptor targetFd = Libcore.os.open(target.getAbsolutePath(), |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 467 | O_CREAT | O_WRONLY, 0644); |
| 468 | Os.chmod(target.getAbsolutePath(), 0644); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 469 | |
| 470 | // If caller specified a total length, allocate it for them. Free up |
| 471 | // cache space to grow, if needed. |
Jeff Sharkey | d5d5e92 | 2017-02-21 10:51:23 -0700 | [diff] [blame] | 472 | if (stageDir != null && lengthBytes > 0) { |
Jeff Sharkey | 683bcd3 | 2017-03-18 17:54:51 -0600 | [diff] [blame] | 473 | mContext.getSystemService(StorageManager.class).allocateBytes(targetFd, lengthBytes, |
| 474 | PackageHelper.translateAllocateFlags(params.installFlags)); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | if (offsetBytes > 0) { |
| 478 | Libcore.os.lseek(targetFd, offsetBytes, OsConstants.SEEK_SET); |
| 479 | } |
| 480 | |
Jeff Sharkey | 02d4e34 | 2017-03-10 21:53:48 -0700 | [diff] [blame] | 481 | if (PackageInstaller.ENABLE_REVOCABLE_FD) { |
| 482 | fd.init(mContext, targetFd); |
| 483 | return fd.getRevocableFileDescriptor(); |
| 484 | } else { |
| 485 | bridge.setTargetFile(targetFd); |
| 486 | bridge.start(); |
| 487 | return new ParcelFileDescriptor(bridge.getClientSocket()); |
| 488 | } |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 489 | |
| 490 | } catch (ErrnoException e) { |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 491 | throw e.rethrowAsIOException(); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 492 | } |
| 493 | } |
| 494 | |
| 495 | @Override |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 496 | public ParcelFileDescriptor openRead(String name) { |
| 497 | try { |
| 498 | return openReadInternal(name); |
| 499 | } catch (IOException e) { |
| 500 | throw ExceptionUtils.wrap(e); |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | private ParcelFileDescriptor openReadInternal(String name) throws IOException { |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 505 | assertPreparedAndNotSealed("openRead"); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 506 | |
| 507 | try { |
| 508 | if (!FileUtils.isValidExtFilename(name)) { |
| 509 | throw new IllegalArgumentException("Invalid name: " + name); |
| 510 | } |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 511 | final File target = new File(resolveStageDir(), name); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 512 | |
| 513 | final FileDescriptor targetFd = Libcore.os.open(target.getAbsolutePath(), O_RDONLY, 0); |
| 514 | return new ParcelFileDescriptor(targetFd); |
| 515 | |
| 516 | } catch (ErrnoException e) { |
| 517 | throw e.rethrowAsIOException(); |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | @Override |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 522 | public void commit(IntentSender statusReceiver) { |
| 523 | Preconditions.checkNotNull(statusReceiver); |
| 524 | |
Jeff Sharkey | cbf4791 | 2014-09-12 09:55:32 -0700 | [diff] [blame] | 525 | final boolean wasSealed; |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 526 | synchronized (mLock) { |
Jeff Sharkey | cbf4791 | 2014-09-12 09:55:32 -0700 | [diff] [blame] | 527 | wasSealed = mSealed; |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 528 | if (!mSealed) { |
| 529 | // Verify that all writers are hands-off |
Jeff Sharkey | 02d4e34 | 2017-03-10 21:53:48 -0700 | [diff] [blame] | 530 | for (RevocableFileDescriptor fd : mFds) { |
| 531 | if (!fd.isRevoked()) { |
| 532 | throw new SecurityException("Files still open"); |
| 533 | } |
| 534 | } |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 535 | for (FileBridge bridge : mBridges) { |
| 536 | if (!bridge.isClosed()) { |
| 537 | throw new SecurityException("Files still open"); |
| 538 | } |
| 539 | } |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 540 | mSealed = true; |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 541 | } |
Jeff Sharkey | cbf4791 | 2014-09-12 09:55:32 -0700 | [diff] [blame] | 542 | |
| 543 | // Client staging is fully done at this point |
| 544 | mClientProgress = 1f; |
| 545 | computeProgressLocked(true); |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 546 | } |
| 547 | |
Jeff Sharkey | cbf4791 | 2014-09-12 09:55:32 -0700 | [diff] [blame] | 548 | if (!wasSealed) { |
| 549 | // Persist the fact that we've sealed ourselves to prevent |
| 550 | // mutations of any hard links we create. We do this without holding |
| 551 | // the session lock, since otherwise it's a lock inversion. |
| 552 | mCallback.onSessionSealedBlocking(this); |
| 553 | } |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 554 | |
| 555 | // This ongoing commit should keep session active, even though client |
| 556 | // will probably close their end. |
| 557 | mActiveCount.incrementAndGet(); |
| 558 | |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 559 | final PackageInstallObserverAdapter adapter = new PackageInstallObserverAdapter(mContext, |
Benjamin Franz | 39fb7fd | 2015-02-18 16:11:18 +0000 | [diff] [blame] | 560 | statusReceiver, sessionId, mIsInstallerDeviceOwner, userId); |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 561 | mHandler.obtainMessage(MSG_COMMIT, adapter.getBinder()).sendToTarget(); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 562 | } |
| 563 | |
Todd Kennedy | f29d07a | 2016-08-08 15:17:43 -0700 | [diff] [blame] | 564 | private void commitLocked(PackageInfo pkgInfo, ApplicationInfo appInfo) |
| 565 | throws PackageManagerException { |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 566 | if (mDestroyed) { |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 567 | throw new PackageManagerException(INSTALL_FAILED_INTERNAL_ERROR, "Session destroyed"); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 568 | } |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 569 | if (!mSealed) { |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 570 | throw new PackageManagerException(INSTALL_FAILED_INTERNAL_ERROR, "Session not sealed"); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 571 | } |
| 572 | |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 573 | try { |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 574 | resolveStageDir(); |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 575 | } catch (IOException e) { |
| 576 | throw new PackageManagerException(INSTALL_FAILED_CONTAINER_ERROR, |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 577 | "Failed to resolve stage location", e); |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 578 | } |
| 579 | |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 580 | // Verify that stage looks sane with respect to existing application. |
| 581 | // This currently only ensures packageName, versionCode, and certificate |
| 582 | // consistency. |
Todd Kennedy | f29d07a | 2016-08-08 15:17:43 -0700 | [diff] [blame] | 583 | validateInstallLocked(pkgInfo, appInfo); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 584 | |
| 585 | Preconditions.checkNotNull(mPackageName); |
Todd Kennedy | 373f0b4 | 2015-12-16 14:45:14 -0800 | [diff] [blame] | 586 | Preconditions.checkNotNull(mSignatures); |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 587 | Preconditions.checkNotNull(mResolvedBaseFile); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 588 | |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 589 | if (!mPermissionsAccepted) { |
| 590 | // User needs to accept permissions; give installer an intent they |
| 591 | // can use to involve user. |
| 592 | final Intent intent = new Intent(PackageInstaller.ACTION_CONFIRM_PERMISSIONS); |
Svet Ganov | f1b7f20 | 2015-07-29 08:33:42 -0700 | [diff] [blame] | 593 | intent.setPackage(mContext.getPackageManager().getPermissionControllerPackageName()); |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 594 | intent.putExtra(PackageInstaller.EXTRA_SESSION_ID, sessionId); |
| 595 | try { |
| 596 | mRemoteObserver.onUserActionRequired(intent); |
| 597 | } catch (RemoteException ignored) { |
| 598 | } |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 599 | |
| 600 | // Commit was keeping session marked as active until now; release |
| 601 | // that extra refcount so session appears idle. |
| 602 | close(); |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 603 | return; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 604 | } |
| 605 | |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 606 | if (stageCid != null) { |
| 607 | // Figure out the final installed size and resize the container once |
| 608 | // and for all. Internally the parser handles straddling between two |
| 609 | // locations when inheriting. |
| 610 | final long finalSize = calculateInstalledSize(); |
| 611 | resizeContainer(stageCid, finalSize); |
| 612 | } |
| 613 | |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 614 | // Inherit any packages and native libraries from existing install that |
| 615 | // haven't been overridden. |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 616 | if (params.mode == SessionParams.MODE_INHERIT_EXISTING) { |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 617 | try { |
Jeff Sharkey | 88d2a3c | 2014-11-22 16:49:34 -0800 | [diff] [blame] | 618 | final List<File> fromFiles = mResolvedInheritedFiles; |
| 619 | final File toDir = resolveStageDir(); |
| 620 | |
Narayan Kamath | cd1fc14 | 2015-05-11 13:35:59 +0100 | [diff] [blame] | 621 | if (LOGD) Slog.d(TAG, "Inherited files: " + mResolvedInheritedFiles); |
| 622 | if (!mResolvedInheritedFiles.isEmpty() && mInheritedFilesBase == null) { |
| 623 | throw new IllegalStateException("mInheritedFilesBase == null"); |
| 624 | } |
| 625 | |
Jeff Sharkey | 88d2a3c | 2014-11-22 16:49:34 -0800 | [diff] [blame] | 626 | if (isLinkPossible(fromFiles, toDir)) { |
Narayan Kamath | e845a1e | 2015-06-05 11:59:26 +0100 | [diff] [blame] | 627 | if (!mResolvedInstructionSets.isEmpty()) { |
| 628 | final File oatDir = new File(toDir, "oat"); |
| 629 | createOatDirs(mResolvedInstructionSets, oatDir); |
| 630 | } |
| 631 | linkFiles(fromFiles, toDir, mInheritedFilesBase); |
Jeff Sharkey | 88d2a3c | 2014-11-22 16:49:34 -0800 | [diff] [blame] | 632 | } else { |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 633 | // TODO: this should delegate to DCS so the system process |
| 634 | // avoids holding open FDs into containers. |
Jeff Sharkey | 88d2a3c | 2014-11-22 16:49:34 -0800 | [diff] [blame] | 635 | copyFiles(fromFiles, toDir); |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 636 | } |
| 637 | } catch (IOException e) { |
| 638 | throw new PackageManagerException(INSTALL_FAILED_INSUFFICIENT_STORAGE, |
| 639 | "Failed to inherit existing install", e); |
| 640 | } |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 641 | } |
| 642 | |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 643 | // TODO: surface more granular state from dexopt |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 644 | mInternalProgress = 0.5f; |
| 645 | computeProgressLocked(true); |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 646 | |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 647 | // Unpack native libraries |
| 648 | extractNativeLibraries(mResolvedStageDir, params.abiOverride); |
| 649 | |
| 650 | // Container is ready to go, let's seal it up! |
| 651 | if (stageCid != null) { |
| 652 | finalizeAndFixContainer(stageCid); |
| 653 | } |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 654 | |
| 655 | // We've reached point of no return; call into PMS to install the stage. |
| 656 | // Regardless of success or failure we always destroy session. |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 657 | final IPackageInstallObserver2 localObserver = new IPackageInstallObserver2.Stub() { |
| 658 | @Override |
Jeff Sharkey | fbd0e9f | 2014-08-06 16:34:34 -0700 | [diff] [blame] | 659 | public void onUserActionRequired(Intent intent) { |
| 660 | throw new IllegalStateException(); |
| 661 | } |
| 662 | |
| 663 | @Override |
| 664 | public void onPackageInstalled(String basePackageName, int returnCode, String msg, |
| 665 | Bundle extras) { |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 666 | destroyInternal(); |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 667 | dispatchSessionFinished(returnCode, msg, extras); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 668 | } |
| 669 | }; |
| 670 | |
Jeff Sharkey | e980804 | 2014-09-11 21:15:37 -0700 | [diff] [blame] | 671 | final UserHandle user; |
| 672 | if ((params.installFlags & PackageManager.INSTALL_ALL_USERS) != 0) { |
| 673 | user = UserHandle.ALL; |
| 674 | } else { |
| 675 | user = new UserHandle(userId); |
| 676 | } |
| 677 | |
Jeff Sharkey | 497c052 | 2015-05-12 13:07:14 -0700 | [diff] [blame] | 678 | mRelinquished = true; |
Jeff Sharkey | e980804 | 2014-09-11 21:15:37 -0700 | [diff] [blame] | 679 | mPm.installStage(mPackageName, stageDir, stageCid, localObserver, params, |
Todd Kennedy | d9d438a | 2016-04-06 14:08:14 -0700 | [diff] [blame] | 680 | installerPackageName, installerUid, user, mCertificates); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | /** |
| 684 | * Validate install by confirming that all application packages are have |
| 685 | * consistent package name, version code, and signing certificates. |
| 686 | * <p> |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 687 | * Clears and populates {@link #mResolvedBaseFile}, |
| 688 | * {@link #mResolvedStagedFiles}, and {@link #mResolvedInheritedFiles}. |
| 689 | * <p> |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 690 | * Renames package files in stage to match split names defined inside. |
Jeff Sharkey | bb7b7be | 2014-08-19 16:18:28 -0700 | [diff] [blame] | 691 | * <p> |
| 692 | * Note that upgrade compatibility is still performed by |
| 693 | * {@link PackageManagerService}. |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 694 | */ |
Todd Kennedy | f29d07a | 2016-08-08 15:17:43 -0700 | [diff] [blame] | 695 | private void validateInstallLocked(PackageInfo pkgInfo, ApplicationInfo appInfo) |
| 696 | throws PackageManagerException { |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 697 | mPackageName = null; |
| 698 | mVersionCode = -1; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 699 | mSignatures = null; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 700 | |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 701 | mResolvedBaseFile = null; |
| 702 | mResolvedStagedFiles.clear(); |
| 703 | mResolvedInheritedFiles.clear(); |
| 704 | |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 705 | final File[] removedFiles = mResolvedStageDir.listFiles(sRemovedFilter); |
| 706 | final List<String> removeSplitList = new ArrayList<>(); |
| 707 | if (!ArrayUtils.isEmpty(removedFiles)) { |
| 708 | for (File removedFile : removedFiles) { |
| 709 | final String fileName = removedFile.getName(); |
| 710 | final String splitName = fileName.substring( |
| 711 | 0, fileName.length() - REMOVE_SPLIT_MARKER_EXTENSION.length()); |
| 712 | removeSplitList.add(splitName); |
| 713 | } |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 714 | } |
| 715 | |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 716 | final File[] addedFiles = mResolvedStageDir.listFiles(sAddedFilter); |
| 717 | if (ArrayUtils.isEmpty(addedFiles) && removeSplitList.size() == 0) { |
| 718 | throw new PackageManagerException(INSTALL_FAILED_INVALID_APK, "No packages staged"); |
| 719 | } |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 720 | // Verify that all staged packages are internally consistent |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 721 | final ArraySet<String> stagedSplits = new ArraySet<>(); |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 722 | for (File addedFile : addedFiles) { |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 723 | final ApkLite apk; |
Jeff Sharkey | 275e085 | 2014-06-17 18:18:49 -0700 | [diff] [blame] | 724 | try { |
Alex Klyubin | 58bfae4 | 2017-01-03 16:25:33 -0800 | [diff] [blame] | 725 | int flags = PackageParser.PARSE_COLLECT_CERTIFICATES; |
Todd Kennedy | be0b889 | 2017-02-15 14:13:52 -0800 | [diff] [blame] | 726 | if ((params.installFlags & PackageManager.INSTALL_INSTANT_APP) != 0) { |
Alex Klyubin | 58bfae4 | 2017-01-03 16:25:33 -0800 | [diff] [blame] | 727 | flags |= PackageParser.PARSE_IS_EPHEMERAL; |
| 728 | } |
| 729 | apk = PackageParser.parseApkLite(addedFile, flags); |
Jeff Sharkey | 275e085 | 2014-06-17 18:18:49 -0700 | [diff] [blame] | 730 | } catch (PackageParserException e) { |
Jeff Sharkey | bc09755 | 2014-09-09 14:57:26 -0700 | [diff] [blame] | 731 | throw PackageManagerException.from(e); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 732 | } |
| 733 | |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 734 | if (!stagedSplits.add(apk.splitName)) { |
Jeff Sharkey | e0b0bef | 2014-07-12 15:37:47 -0700 | [diff] [blame] | 735 | throw new PackageManagerException(INSTALL_FAILED_INVALID_APK, |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 736 | "Split " + apk.splitName + " was defined multiple times"); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | // Use first package to define unknown values |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 740 | if (mPackageName == null) { |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 741 | mPackageName = apk.packageName; |
| 742 | mVersionCode = apk.versionCode; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 743 | } |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 744 | if (mSignatures == null) { |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 745 | mSignatures = apk.signatures; |
Todd Kennedy | d9d438a | 2016-04-06 14:08:14 -0700 | [diff] [blame] | 746 | mCertificates = apk.certificates; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 747 | } |
| 748 | |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 749 | assertApkConsistent(String.valueOf(addedFile), apk); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 750 | |
| 751 | // Take this opportunity to enforce uniform naming |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 752 | final String targetName; |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 753 | if (apk.splitName == null) { |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 754 | targetName = "base.apk"; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 755 | } else { |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 756 | targetName = "split_" + apk.splitName + ".apk"; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 757 | } |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 758 | if (!FileUtils.isValidExtFilename(targetName)) { |
Jeff Sharkey | e0b0bef | 2014-07-12 15:37:47 -0700 | [diff] [blame] | 759 | throw new PackageManagerException(INSTALL_FAILED_INVALID_APK, |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 760 | "Invalid filename: " + targetName); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 761 | } |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 762 | |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 763 | final File targetFile = new File(mResolvedStageDir, targetName); |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 764 | if (!addedFile.equals(targetFile)) { |
| 765 | addedFile.renameTo(targetFile); |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | // Base is coming from session |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 769 | if (apk.splitName == null) { |
| 770 | mResolvedBaseFile = targetFile; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 771 | } |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 772 | |
| 773 | mResolvedStagedFiles.add(targetFile); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 774 | } |
| 775 | |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 776 | if (removeSplitList.size() > 0) { |
| 777 | // validate split names marked for removal |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 778 | for (String splitName : removeSplitList) { |
Todd Kennedy | f29d07a | 2016-08-08 15:17:43 -0700 | [diff] [blame] | 779 | if (!ArrayUtils.contains(pkgInfo.splitNames, splitName)) { |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 780 | throw new PackageManagerException(INSTALL_FAILED_INVALID_APK, |
| 781 | "Split not found: " + splitName); |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | // ensure we've got appropriate package name, version code and signatures |
| 786 | if (mPackageName == null) { |
Todd Kennedy | f29d07a | 2016-08-08 15:17:43 -0700 | [diff] [blame] | 787 | mPackageName = pkgInfo.packageName; |
| 788 | mVersionCode = pkgInfo.versionCode; |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 789 | } |
| 790 | if (mSignatures == null) { |
Todd Kennedy | f29d07a | 2016-08-08 15:17:43 -0700 | [diff] [blame] | 791 | mSignatures = pkgInfo.signatures; |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 792 | } |
| 793 | } |
| 794 | |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 795 | if (params.mode == SessionParams.MODE_FULL_INSTALL) { |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 796 | // Full installs must include a base package |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 797 | if (!stagedSplits.contains(null)) { |
Jeff Sharkey | e0b0bef | 2014-07-12 15:37:47 -0700 | [diff] [blame] | 798 | throw new PackageManagerException(INSTALL_FAILED_INVALID_APK, |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 799 | "Full install must include a base package"); |
| 800 | } |
| 801 | |
| 802 | } else { |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 803 | // Partial installs must be consistent with existing install |
Todd Kennedy | f29d07a | 2016-08-08 15:17:43 -0700 | [diff] [blame] | 804 | if (appInfo == null) { |
Jeff Sharkey | e0b0bef | 2014-07-12 15:37:47 -0700 | [diff] [blame] | 805 | throw new PackageManagerException(INSTALL_FAILED_INVALID_APK, |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 806 | "Missing existing base package for " + mPackageName); |
| 807 | } |
| 808 | |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 809 | final PackageLite existing; |
| 810 | final ApkLite existingBase; |
Jeff Sharkey | 275e085 | 2014-06-17 18:18:49 -0700 | [diff] [blame] | 811 | try { |
Todd Kennedy | f29d07a | 2016-08-08 15:17:43 -0700 | [diff] [blame] | 812 | existing = PackageParser.parsePackageLite(new File(appInfo.getCodePath()), 0); |
| 813 | existingBase = PackageParser.parseApkLite(new File(appInfo.getBaseCodePath()), |
Jeff Sharkey | bb7b7be | 2014-08-19 16:18:28 -0700 | [diff] [blame] | 814 | PackageParser.PARSE_COLLECT_CERTIFICATES); |
Jeff Sharkey | 275e085 | 2014-06-17 18:18:49 -0700 | [diff] [blame] | 815 | } catch (PackageParserException e) { |
Jeff Sharkey | bc09755 | 2014-09-09 14:57:26 -0700 | [diff] [blame] | 816 | throw PackageManagerException.from(e); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 817 | } |
| 818 | |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 819 | assertApkConsistent("Existing base", existingBase); |
| 820 | |
| 821 | // Inherit base if not overridden |
| 822 | if (mResolvedBaseFile == null) { |
Todd Kennedy | f29d07a | 2016-08-08 15:17:43 -0700 | [diff] [blame] | 823 | mResolvedBaseFile = new File(appInfo.getBaseCodePath()); |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 824 | mResolvedInheritedFiles.add(mResolvedBaseFile); |
| 825 | } |
| 826 | |
| 827 | // Inherit splits if not overridden |
| 828 | if (!ArrayUtils.isEmpty(existing.splitNames)) { |
| 829 | for (int i = 0; i < existing.splitNames.length; i++) { |
| 830 | final String splitName = existing.splitNames[i]; |
| 831 | final File splitFile = new File(existing.splitCodePaths[i]); |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 832 | final boolean splitRemoved = removeSplitList.contains(splitName); |
| 833 | if (!stagedSplits.contains(splitName) && !splitRemoved) { |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 834 | mResolvedInheritedFiles.add(splitFile); |
| 835 | } |
| 836 | } |
| 837 | } |
Narayan Kamath | cd1fc14 | 2015-05-11 13:35:59 +0100 | [diff] [blame] | 838 | |
| 839 | // Inherit compiled oat directory. |
Todd Kennedy | f29d07a | 2016-08-08 15:17:43 -0700 | [diff] [blame] | 840 | final File packageInstallDir = (new File(appInfo.getBaseCodePath())).getParentFile(); |
Narayan Kamath | cd1fc14 | 2015-05-11 13:35:59 +0100 | [diff] [blame] | 841 | mInheritedFilesBase = packageInstallDir; |
| 842 | final File oatDir = new File(packageInstallDir, "oat"); |
| 843 | if (oatDir.exists()) { |
| 844 | final File[] archSubdirs = oatDir.listFiles(); |
Narayan Kamath | cd1fc14 | 2015-05-11 13:35:59 +0100 | [diff] [blame] | 845 | |
Narayan Kamath | e845a1e | 2015-06-05 11:59:26 +0100 | [diff] [blame] | 846 | // Keep track of all instruction sets we've seen compiled output for. |
| 847 | // If we're linking (and not copying) inherited files, we can recreate the |
| 848 | // instruction set hierarchy and link compiled output. |
| 849 | if (archSubdirs != null && archSubdirs.length > 0) { |
| 850 | final String[] instructionSets = InstructionSets.getAllDexCodeInstructionSets(); |
| 851 | for (File archSubDir : archSubdirs) { |
| 852 | // Skip any directory that isn't an ISA subdir. |
| 853 | if (!ArrayUtils.contains(instructionSets, archSubDir.getName())) { |
| 854 | continue; |
| 855 | } |
| 856 | |
| 857 | mResolvedInstructionSets.add(archSubDir.getName()); |
| 858 | List<File> oatFiles = Arrays.asList(archSubDir.listFiles()); |
Jeff Hao | d1235f5 | 2017-06-13 11:09:10 -0700 | [diff] [blame] | 859 | |
| 860 | // Only add compiled files associated with the base. |
| 861 | // Once b/62269291 is resolved, we can add all compiled files again. |
| 862 | for (File oatFile : oatFiles) { |
| 863 | if (oatFile.getName().equals("base.art") |
| 864 | || oatFile.getName().equals("base.odex") |
| 865 | || oatFile.getName().equals("base.vdex")) { |
| 866 | mResolvedInheritedFiles.add(oatFile); |
| 867 | } |
Narayan Kamath | e845a1e | 2015-06-05 11:59:26 +0100 | [diff] [blame] | 868 | } |
Narayan Kamath | cd1fc14 | 2015-05-11 13:35:59 +0100 | [diff] [blame] | 869 | } |
| 870 | } |
| 871 | } |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 872 | } |
| 873 | } |
| 874 | |
Todd Kennedy | f29d07a | 2016-08-08 15:17:43 -0700 | [diff] [blame] | 875 | private void assertApkConsistent(String tag, ApkLite apk) |
| 876 | throws PackageManagerException { |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 877 | if (!mPackageName.equals(apk.packageName)) { |
Jeff Sharkey | e0b0bef | 2014-07-12 15:37:47 -0700 | [diff] [blame] | 878 | throw new PackageManagerException(INSTALL_FAILED_INVALID_APK, tag + " package " |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 879 | + apk.packageName + " inconsistent with " + mPackageName); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 880 | } |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 881 | if (params.appPackageName != null && !params.appPackageName.equals(apk.packageName)) { |
| 882 | throw new PackageManagerException(INSTALL_FAILED_INVALID_APK, tag |
| 883 | + " specified package " + params.appPackageName |
| 884 | + " inconsistent with " + apk.packageName); |
| 885 | } |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 886 | if (mVersionCode != apk.versionCode) { |
Jeff Sharkey | e0b0bef | 2014-07-12 15:37:47 -0700 | [diff] [blame] | 887 | throw new PackageManagerException(INSTALL_FAILED_INVALID_APK, tag |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 888 | + " version code " + apk.versionCode + " inconsistent with " |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 889 | + mVersionCode); |
| 890 | } |
Todd Kennedy | 373f0b4 | 2015-12-16 14:45:14 -0800 | [diff] [blame] | 891 | if (!Signature.areExactMatch(mSignatures, apk.signatures)) { |
Jeff Sharkey | e0b0bef | 2014-07-12 15:37:47 -0700 | [diff] [blame] | 892 | throw new PackageManagerException(INSTALL_FAILED_INVALID_APK, |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 893 | tag + " signatures are inconsistent"); |
| 894 | } |
| 895 | } |
| 896 | |
| 897 | /** |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 898 | * Calculate the final install footprint size, combining both staged and |
| 899 | * existing APKs together and including unpacked native code from both. |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 900 | */ |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 901 | private long calculateInstalledSize() throws PackageManagerException { |
| 902 | Preconditions.checkNotNull(mResolvedBaseFile); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 903 | |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 904 | final ApkLite baseApk; |
| 905 | try { |
| 906 | baseApk = PackageParser.parseApkLite(mResolvedBaseFile, 0); |
| 907 | } catch (PackageParserException e) { |
Jeff Sharkey | bc09755 | 2014-09-09 14:57:26 -0700 | [diff] [blame] | 908 | throw PackageManagerException.from(e); |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 909 | } |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 910 | |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 911 | final List<String> splitPaths = new ArrayList<>(); |
| 912 | for (File file : mResolvedStagedFiles) { |
| 913 | if (mResolvedBaseFile.equals(file)) continue; |
| 914 | splitPaths.add(file.getAbsolutePath()); |
| 915 | } |
| 916 | for (File file : mResolvedInheritedFiles) { |
| 917 | if (mResolvedBaseFile.equals(file)) continue; |
| 918 | splitPaths.add(file.getAbsolutePath()); |
| 919 | } |
| 920 | |
| 921 | // This is kind of hacky; we're creating a half-parsed package that is |
| 922 | // straddled between the inherited and staged APKs. |
Adam Lesinski | 1665d0f | 2017-03-10 14:46:57 -0800 | [diff] [blame] | 923 | final PackageLite pkg = new PackageLite(null, baseApk, null, null, null, null, |
Jeff Sharkey | 88d2a3c | 2014-11-22 16:49:34 -0800 | [diff] [blame] | 924 | splitPaths.toArray(new String[splitPaths.size()]), null); |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 925 | final boolean isForwardLocked = |
| 926 | (params.installFlags & PackageManager.INSTALL_FORWARD_LOCK) != 0; |
| 927 | |
| 928 | try { |
| 929 | return PackageHelper.calculateInstalledSize(pkg, isForwardLocked, params.abiOverride); |
| 930 | } catch (IOException e) { |
| 931 | throw new PackageManagerException(INSTALL_FAILED_INVALID_APK, |
| 932 | "Failed to calculate install size", e); |
| 933 | } |
| 934 | } |
| 935 | |
Jeff Sharkey | 88d2a3c | 2014-11-22 16:49:34 -0800 | [diff] [blame] | 936 | /** |
| 937 | * Determine if creating hard links between source and destination is |
| 938 | * possible. That is, do they all live on the same underlying device. |
| 939 | */ |
| 940 | private boolean isLinkPossible(List<File> fromFiles, File toDir) { |
| 941 | try { |
| 942 | final StructStat toStat = Os.stat(toDir.getAbsolutePath()); |
| 943 | for (File fromFile : fromFiles) { |
| 944 | final StructStat fromStat = Os.stat(fromFile.getAbsolutePath()); |
| 945 | if (fromStat.st_dev != toStat.st_dev) { |
| 946 | return false; |
| 947 | } |
| 948 | } |
| 949 | } catch (ErrnoException e) { |
| 950 | Slog.w(TAG, "Failed to detect if linking possible: " + e); |
| 951 | return false; |
| 952 | } |
| 953 | return true; |
| 954 | } |
| 955 | |
Narayan Kamath | e845a1e | 2015-06-05 11:59:26 +0100 | [diff] [blame] | 956 | private static String getRelativePath(File file, File base) throws IOException { |
Narayan Kamath | cd1fc14 | 2015-05-11 13:35:59 +0100 | [diff] [blame] | 957 | final String pathStr = file.getAbsolutePath(); |
Narayan Kamath | e845a1e | 2015-06-05 11:59:26 +0100 | [diff] [blame] | 958 | final String baseStr = base.getAbsolutePath(); |
Narayan Kamath | cd1fc14 | 2015-05-11 13:35:59 +0100 | [diff] [blame] | 959 | // Don't allow relative paths. |
| 960 | if (pathStr.contains("/.") ) { |
| 961 | throw new IOException("Invalid path (was relative) : " + pathStr); |
| 962 | } |
| 963 | |
Narayan Kamath | e845a1e | 2015-06-05 11:59:26 +0100 | [diff] [blame] | 964 | if (pathStr.startsWith(baseStr)) { |
| 965 | return pathStr.substring(baseStr.length()); |
Narayan Kamath | cd1fc14 | 2015-05-11 13:35:59 +0100 | [diff] [blame] | 966 | } |
| 967 | |
Narayan Kamath | e845a1e | 2015-06-05 11:59:26 +0100 | [diff] [blame] | 968 | throw new IOException("File: " + pathStr + " outside base: " + baseStr); |
Narayan Kamath | cd1fc14 | 2015-05-11 13:35:59 +0100 | [diff] [blame] | 969 | } |
| 970 | |
Jeff Sharkey | fdeeeea | 2016-01-11 17:34:24 -0700 | [diff] [blame] | 971 | private void createOatDirs(List<String> instructionSets, File fromDir) |
| 972 | throws PackageManagerException { |
Narayan Kamath | e845a1e | 2015-06-05 11:59:26 +0100 | [diff] [blame] | 973 | for (String instructionSet : instructionSets) { |
Jeff Sharkey | fdeeeea | 2016-01-11 17:34:24 -0700 | [diff] [blame] | 974 | try { |
| 975 | mPm.mInstaller.createOatDir(fromDir.getAbsolutePath(), instructionSet); |
| 976 | } catch (InstallerException e) { |
| 977 | throw PackageManagerException.from(e); |
| 978 | } |
Narayan Kamath | e845a1e | 2015-06-05 11:59:26 +0100 | [diff] [blame] | 979 | } |
| 980 | } |
| 981 | |
| 982 | private void linkFiles(List<File> fromFiles, File toDir, File fromDir) |
Narayan Kamath | cd1fc14 | 2015-05-11 13:35:59 +0100 | [diff] [blame] | 983 | throws IOException { |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 984 | for (File fromFile : fromFiles) { |
Narayan Kamath | e845a1e | 2015-06-05 11:59:26 +0100 | [diff] [blame] | 985 | final String relativePath = getRelativePath(fromFile, fromDir); |
Jeff Sharkey | fdeeeea | 2016-01-11 17:34:24 -0700 | [diff] [blame] | 986 | try { |
| 987 | mPm.mInstaller.linkFile(relativePath, fromDir.getAbsolutePath(), |
| 988 | toDir.getAbsolutePath()); |
| 989 | } catch (InstallerException e) { |
Narayan Kamath | e845a1e | 2015-06-05 11:59:26 +0100 | [diff] [blame] | 990 | throw new IOException("failed linkOrCreateDir(" + relativePath + ", " |
Jeff Sharkey | fdeeeea | 2016-01-11 17:34:24 -0700 | [diff] [blame] | 991 | + fromDir + ", " + toDir + ")", e); |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 992 | } |
| 993 | } |
Narayan Kamath | e845a1e | 2015-06-05 11:59:26 +0100 | [diff] [blame] | 994 | |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 995 | Slog.d(TAG, "Linked " + fromFiles.size() + " files into " + toDir); |
| 996 | } |
| 997 | |
| 998 | private static void copyFiles(List<File> fromFiles, File toDir) throws IOException { |
| 999 | // Remove any partial files from previous attempt |
| 1000 | for (File file : toDir.listFiles()) { |
| 1001 | if (file.getName().endsWith(".tmp")) { |
| 1002 | file.delete(); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1003 | } |
| 1004 | } |
Jeff Sharkey | 9a44577 | 2014-07-16 11:32:08 -0700 | [diff] [blame] | 1005 | |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 1006 | for (File fromFile : fromFiles) { |
| 1007 | final File tmpFile = File.createTempFile("inherit", ".tmp", toDir); |
| 1008 | if (LOGD) Slog.d(TAG, "Copying " + fromFile + " to " + tmpFile); |
| 1009 | if (!FileUtils.copyFile(fromFile, tmpFile)) { |
| 1010 | throw new IOException("Failed to copy " + fromFile + " to " + tmpFile); |
| 1011 | } |
Jeff Sharkey | 88d2a3c | 2014-11-22 16:49:34 -0800 | [diff] [blame] | 1012 | try { |
| 1013 | Os.chmod(tmpFile.getAbsolutePath(), 0644); |
| 1014 | } catch (ErrnoException e) { |
| 1015 | throw new IOException("Failed to chmod " + tmpFile); |
| 1016 | } |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 1017 | final File toFile = new File(toDir, fromFile.getName()); |
| 1018 | if (LOGD) Slog.d(TAG, "Renaming " + tmpFile + " to " + toFile); |
| 1019 | if (!tmpFile.renameTo(toFile)) { |
| 1020 | throw new IOException("Failed to rename " + tmpFile + " to " + toFile); |
| 1021 | } |
| 1022 | } |
| 1023 | Slog.d(TAG, "Copied " + fromFiles.size() + " files into " + toDir); |
| 1024 | } |
| 1025 | |
| 1026 | private static void extractNativeLibraries(File packageDir, String abiOverride) |
| 1027 | throws PackageManagerException { |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 1028 | // Always start from a clean slate |
| 1029 | final File libDir = new File(packageDir, NativeLibraryHelper.LIB_DIR_NAME); |
| 1030 | NativeLibraryHelper.removeNativeBinariesFromDirLI(libDir, true); |
| 1031 | |
| 1032 | NativeLibraryHelper.Handle handle = null; |
| 1033 | try { |
| 1034 | handle = NativeLibraryHelper.Handle.create(packageDir); |
| 1035 | final int res = NativeLibraryHelper.copyNativeBinariesWithOverride(handle, libDir, |
| 1036 | abiOverride); |
| 1037 | if (res != PackageManager.INSTALL_SUCCEEDED) { |
| 1038 | throw new PackageManagerException(res, |
| 1039 | "Failed to extract native libraries, res=" + res); |
| 1040 | } |
| 1041 | } catch (IOException e) { |
| 1042 | throw new PackageManagerException(INSTALL_FAILED_INTERNAL_ERROR, |
| 1043 | "Failed to extract native libraries", e); |
| 1044 | } finally { |
| 1045 | IoUtils.closeQuietly(handle); |
| 1046 | } |
| 1047 | } |
| 1048 | |
| 1049 | private static void resizeContainer(String cid, long targetSize) |
| 1050 | throws PackageManagerException { |
| 1051 | String path = PackageHelper.getSdDir(cid); |
| 1052 | if (path == null) { |
| 1053 | throw new PackageManagerException(INSTALL_FAILED_CONTAINER_ERROR, |
| 1054 | "Failed to find mounted " + cid); |
| 1055 | } |
| 1056 | |
| 1057 | final long currentSize = new File(path).getTotalSpace(); |
| 1058 | if (currentSize > targetSize) { |
| 1059 | Slog.w(TAG, "Current size " + currentSize + " is larger than target size " |
| 1060 | + targetSize + "; skipping resize"); |
| 1061 | return; |
| 1062 | } |
| 1063 | |
| 1064 | if (!PackageHelper.unMountSdDir(cid)) { |
| 1065 | throw new PackageManagerException(INSTALL_FAILED_CONTAINER_ERROR, |
| 1066 | "Failed to unmount " + cid + " before resize"); |
| 1067 | } |
| 1068 | |
| 1069 | if (!PackageHelper.resizeSdDir(targetSize, cid, |
| 1070 | PackageManagerService.getEncryptKey())) { |
| 1071 | throw new PackageManagerException(INSTALL_FAILED_CONTAINER_ERROR, |
| 1072 | "Failed to resize " + cid + " to " + targetSize + " bytes"); |
| 1073 | } |
| 1074 | |
| 1075 | path = PackageHelper.mountSdDir(cid, PackageManagerService.getEncryptKey(), |
| 1076 | Process.SYSTEM_UID, false); |
| 1077 | if (path == null) { |
| 1078 | throw new PackageManagerException(INSTALL_FAILED_CONTAINER_ERROR, |
| 1079 | "Failed to mount " + cid + " after resize"); |
| 1080 | } |
| 1081 | } |
| 1082 | |
| 1083 | private void finalizeAndFixContainer(String cid) throws PackageManagerException { |
| 1084 | if (!PackageHelper.finalizeSdDir(cid)) { |
| 1085 | throw new PackageManagerException(INSTALL_FAILED_CONTAINER_ERROR, |
| 1086 | "Failed to finalize container " + cid); |
| 1087 | } |
| 1088 | |
Todd Kennedy | f29d07a | 2016-08-08 15:17:43 -0700 | [diff] [blame] | 1089 | if (!PackageHelper.fixSdPermissions(cid, defaultContainerGid, null)) { |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 1090 | throw new PackageManagerException(INSTALL_FAILED_CONTAINER_ERROR, |
| 1091 | "Failed to fix permissions on container " + cid); |
| 1092 | } |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1093 | } |
| 1094 | |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 1095 | void setPermissionsResult(boolean accepted) { |
| 1096 | if (!mSealed) { |
| 1097 | throw new SecurityException("Must be sealed to accept permissions"); |
| 1098 | } |
| 1099 | |
| 1100 | if (accepted) { |
| 1101 | // Mark and kick off another install pass |
Todd Kennedy | a1d12cf | 2015-09-29 15:43:00 -0700 | [diff] [blame] | 1102 | synchronized (mLock) { |
| 1103 | mPermissionsAccepted = true; |
| 1104 | } |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 1105 | mHandler.obtainMessage(MSG_COMMIT).sendToTarget(); |
| 1106 | } else { |
| 1107 | destroyInternal(); |
Jeff Sharkey | f060095 | 2014-08-07 17:31:53 -0700 | [diff] [blame] | 1108 | dispatchSessionFinished(INSTALL_FAILED_ABORTED, "User rejected permissions", null); |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 1109 | } |
| 1110 | } |
| 1111 | |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 1112 | public void open() throws IOException { |
| 1113 | if (mActiveCount.getAndIncrement() == 0) { |
Jeff Sharkey | bc7bce3 | 2014-09-05 15:53:05 -0700 | [diff] [blame] | 1114 | mCallback.onSessionActiveChanged(this, true); |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 1115 | } |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 1116 | |
| 1117 | synchronized (mLock) { |
| 1118 | if (!mPrepared) { |
| 1119 | if (stageDir != null) { |
Jeff Sharkey | b2b9ab8 | 2015-04-05 21:10:42 -0700 | [diff] [blame] | 1120 | prepareStageDir(stageDir); |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 1121 | } else if (stageCid != null) { |
Shunta Sato | 4f26cb5 | 2016-06-28 09:29:19 +0900 | [diff] [blame] | 1122 | final long identity = Binder.clearCallingIdentity(); |
| 1123 | try { |
| 1124 | prepareExternalStageCid(stageCid, params.sizeBytes); |
| 1125 | } finally { |
| 1126 | Binder.restoreCallingIdentity(identity); |
| 1127 | } |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 1128 | |
| 1129 | // TODO: deliver more granular progress for ASEC allocation |
| 1130 | mInternalProgress = 0.25f; |
| 1131 | computeProgressLocked(true); |
| 1132 | } else { |
| 1133 | throw new IllegalArgumentException( |
| 1134 | "Exactly one of stageDir or stageCid stage must be set"); |
| 1135 | } |
| 1136 | |
| 1137 | mPrepared = true; |
| 1138 | mCallback.onSessionPrepared(this); |
| 1139 | } |
| 1140 | } |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 1141 | } |
| 1142 | |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1143 | @Override |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 1144 | public void close() { |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 1145 | if (mActiveCount.decrementAndGet() == 0) { |
Jeff Sharkey | bc7bce3 | 2014-09-05 15:53:05 -0700 | [diff] [blame] | 1146 | mCallback.onSessionActiveChanged(this, false); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 1147 | } |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 1148 | } |
| 1149 | |
| 1150 | @Override |
| 1151 | public void abandon() { |
Jeff Sharkey | 497c052 | 2015-05-12 13:07:14 -0700 | [diff] [blame] | 1152 | if (mRelinquished) { |
| 1153 | Slog.d(TAG, "Ignoring abandon after commit relinquished control"); |
| 1154 | return; |
| 1155 | } |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 1156 | destroyInternal(); |
Jeff Sharkey | f060095 | 2014-08-07 17:31:53 -0700 | [diff] [blame] | 1157 | dispatchSessionFinished(INSTALL_FAILED_ABORTED, "Session was abandoned", null); |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 1158 | } |
| 1159 | |
| 1160 | private void dispatchSessionFinished(int returnCode, String msg, Bundle extras) { |
| 1161 | mFinalStatus = returnCode; |
| 1162 | mFinalMessage = msg; |
| 1163 | |
| 1164 | if (mRemoteObserver != null) { |
| 1165 | try { |
| 1166 | mRemoteObserver.onPackageInstalled(mPackageName, returnCode, msg, extras); |
| 1167 | } catch (RemoteException ignored) { |
| 1168 | } |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1169 | } |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 1170 | |
| 1171 | final boolean success = (returnCode == PackageManager.INSTALL_SUCCEEDED); |
Sunny Goyal | 6d7cb23 | 2017-01-30 10:43:18 -0800 | [diff] [blame] | 1172 | |
| 1173 | // Send broadcast to default launcher only if it's a new install |
| 1174 | final boolean isNewInstall = extras == null || !extras.getBoolean(Intent.EXTRA_REPLACING); |
| 1175 | if (success && isNewInstall) { |
Sunny Goyal | a31a74b | 2017-05-11 15:59:19 -0700 | [diff] [blame] | 1176 | mPm.sendSessionCommitBroadcast(generateInfo(), userId); |
Sunny Goyal | 6d7cb23 | 2017-01-30 10:43:18 -0800 | [diff] [blame] | 1177 | } |
| 1178 | |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 1179 | mCallback.onSessionFinished(this, success); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1180 | } |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 1181 | |
| 1182 | private void destroyInternal() { |
| 1183 | synchronized (mLock) { |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 1184 | mSealed = true; |
| 1185 | mDestroyed = true; |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 1186 | |
| 1187 | // Force shut down all bridges |
Jeff Sharkey | 02d4e34 | 2017-03-10 21:53:48 -0700 | [diff] [blame] | 1188 | for (RevocableFileDescriptor fd : mFds) { |
| 1189 | fd.revoke(); |
| 1190 | } |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 1191 | for (FileBridge bridge : mBridges) { |
| 1192 | bridge.forceClose(); |
| 1193 | } |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 1194 | } |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 1195 | if (stageDir != null) { |
Jeff Sharkey | fdeeeea | 2016-01-11 17:34:24 -0700 | [diff] [blame] | 1196 | try { |
| 1197 | mPm.mInstaller.rmPackageDir(stageDir.getAbsolutePath()); |
| 1198 | } catch (InstallerException ignored) { |
| 1199 | } |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 1200 | } |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 1201 | if (stageCid != null) { |
| 1202 | PackageHelper.destroySdDir(stageCid); |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 1203 | } |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 1204 | } |
| 1205 | |
| 1206 | void dump(IndentingPrintWriter pw) { |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 1207 | synchronized (mLock) { |
| 1208 | dumpLocked(pw); |
| 1209 | } |
| 1210 | } |
| 1211 | |
| 1212 | private void dumpLocked(IndentingPrintWriter pw) { |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 1213 | pw.println("Session " + sessionId + ":"); |
| 1214 | pw.increaseIndent(); |
| 1215 | |
| 1216 | pw.printPair("userId", userId); |
| 1217 | pw.printPair("installerPackageName", installerPackageName); |
| 1218 | pw.printPair("installerUid", installerUid); |
| 1219 | pw.printPair("createdMillis", createdMillis); |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 1220 | pw.printPair("stageDir", stageDir); |
| 1221 | pw.printPair("stageCid", stageCid); |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 1222 | pw.println(); |
| 1223 | |
| 1224 | params.dump(pw); |
| 1225 | |
| 1226 | pw.printPair("mClientProgress", mClientProgress); |
| 1227 | pw.printPair("mProgress", mProgress); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 1228 | pw.printPair("mSealed", mSealed); |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 1229 | pw.printPair("mPermissionsAccepted", mPermissionsAccepted); |
Jeff Sharkey | 497c052 | 2015-05-12 13:07:14 -0700 | [diff] [blame] | 1230 | pw.printPair("mRelinquished", mRelinquished); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 1231 | pw.printPair("mDestroyed", mDestroyed); |
Jeff Sharkey | 02d4e34 | 2017-03-10 21:53:48 -0700 | [diff] [blame] | 1232 | pw.printPair("mFds", mFds.size()); |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 1233 | pw.printPair("mBridges", mBridges.size()); |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 1234 | pw.printPair("mFinalStatus", mFinalStatus); |
| 1235 | pw.printPair("mFinalMessage", mFinalMessage); |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 1236 | pw.println(); |
| 1237 | |
| 1238 | pw.decreaseIndent(); |
| 1239 | } |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1240 | } |