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 | |
Eugene Susla | 922cd08 | 2020-03-11 12:38:17 -0700 | [diff] [blame] | 19 | import static android.app.AppOpsManager.MODE_DEFAULT; |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 20 | import static android.content.pm.DataLoaderType.INCREMENTAL; |
| 21 | import static android.content.pm.DataLoaderType.STREAMING; |
Alex Buynytskyy | e069787 | 2020-01-13 09:25:21 -0800 | [diff] [blame] | 22 | import static android.content.pm.PackageInstaller.LOCATION_DATA_APP; |
Jeff Sharkey | f060095 | 2014-08-07 17:31:53 -0700 | [diff] [blame] | 23 | import static android.content.pm.PackageManager.INSTALL_FAILED_ABORTED; |
Victor Hsieh | c0cd748 | 2018-10-04 10:10:54 -0700 | [diff] [blame] | 24 | import static android.content.pm.PackageManager.INSTALL_FAILED_BAD_SIGNATURE; |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 25 | import static android.content.pm.PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 26 | import static android.content.pm.PackageManager.INSTALL_FAILED_INTERNAL_ERROR; |
| 27 | import static android.content.pm.PackageManager.INSTALL_FAILED_INVALID_APK; |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 28 | import static android.content.pm.PackageManager.INSTALL_FAILED_MEDIA_UNAVAILABLE; |
Todd Kennedy | 29cfa27 | 2018-09-26 10:25:24 -0700 | [diff] [blame] | 29 | import static android.content.pm.PackageManager.INSTALL_FAILED_MISSING_SPLIT; |
Mohammad Samiul Islam | ad7e270 | 2019-07-05 14:47:17 +0100 | [diff] [blame] | 30 | import static android.content.pm.PackageParser.APEX_FILE_EXTENSION; |
Calin Juravle | 3fc56c3 | 2017-12-11 18:26:13 -0800 | [diff] [blame] | 31 | import static android.content.pm.PackageParser.APK_FILE_EXTENSION; |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 32 | import static android.system.OsConstants.O_CREAT; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 33 | import static android.system.OsConstants.O_RDONLY; |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 34 | import static android.system.OsConstants.O_WRONLY; |
Jeff Sharkey | d5d5e92 | 2017-02-21 10:51:23 -0700 | [diff] [blame] | 35 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 36 | import static com.android.internal.util.XmlUtils.readBitmapAttribute; |
| 37 | import static com.android.internal.util.XmlUtils.readBooleanAttribute; |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 38 | import static com.android.internal.util.XmlUtils.readByteArrayAttribute; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 39 | import static com.android.internal.util.XmlUtils.readIntAttribute; |
| 40 | import static com.android.internal.util.XmlUtils.readLongAttribute; |
| 41 | import static com.android.internal.util.XmlUtils.readStringAttribute; |
| 42 | import static com.android.internal.util.XmlUtils.readUriAttribute; |
| 43 | import static com.android.internal.util.XmlUtils.writeBooleanAttribute; |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 44 | import static com.android.internal.util.XmlUtils.writeByteArrayAttribute; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 45 | import static com.android.internal.util.XmlUtils.writeIntAttribute; |
| 46 | import static com.android.internal.util.XmlUtils.writeLongAttribute; |
| 47 | import static com.android.internal.util.XmlUtils.writeStringAttribute; |
| 48 | import static com.android.internal.util.XmlUtils.writeUriAttribute; |
Jeff Sharkey | b2b9ab8 | 2015-04-05 21:10:42 -0700 | [diff] [blame] | 49 | import static com.android.server.pm.PackageInstallerService.prepareStageDir; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 50 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 51 | import android.Manifest; |
| 52 | import android.annotation.NonNull; |
Todd Kennedy | 544b383 | 2017-08-22 10:48:18 -0700 | [diff] [blame] | 53 | import android.annotation.Nullable; |
Mohammad Samiul Islam | 10323a1 | 2020-03-17 10:04:43 +0000 | [diff] [blame] | 54 | import android.app.Notification; |
| 55 | import android.app.NotificationManager; |
Rubin Xu | 8b17ad0 | 2019-03-07 17:42:37 +0000 | [diff] [blame] | 56 | import android.app.admin.DevicePolicyEventLogger; |
Benjamin Franz | dabae88 | 2017-08-08 12:33:19 +0100 | [diff] [blame] | 57 | import android.app.admin.DevicePolicyManagerInternal; |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 58 | import android.content.ComponentName; |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 59 | import android.content.Context; |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 60 | import android.content.IIntentReceiver; |
| 61 | import android.content.IIntentSender; |
Jeff Sharkey | fbd0e9f | 2014-08-06 16:34:34 -0700 | [diff] [blame] | 62 | import android.content.Intent; |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 63 | import android.content.IntentSender; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 64 | import android.content.pm.ApplicationInfo; |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 65 | import android.content.pm.DataLoaderManager; |
| 66 | import android.content.pm.DataLoaderParams; |
Alex Buynytskyy | e069787 | 2020-01-13 09:25:21 -0800 | [diff] [blame] | 67 | import android.content.pm.DataLoaderParamsParcel; |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 68 | import android.content.pm.FileSystemControlParcel; |
| 69 | import android.content.pm.IDataLoader; |
| 70 | import android.content.pm.IDataLoaderStatusListener; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 71 | import android.content.pm.IPackageInstallObserver2; |
| 72 | import android.content.pm.IPackageInstallerSession; |
Alex Buynytskyy | ea14d19 | 2019-12-13 15:42:18 -0800 | [diff] [blame] | 73 | import android.content.pm.IPackageInstallerSessionFileSystemConnector; |
Songchun Fan | 4e75869 | 2019-11-29 15:43:27 -0800 | [diff] [blame] | 74 | import android.content.pm.InstallationFile; |
Songchun Fan | 6381d61 | 2020-02-26 17:59:41 -0800 | [diff] [blame] | 75 | import android.content.pm.InstallationFileParcel; |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 76 | import android.content.pm.PackageInfo; |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 77 | import android.content.pm.PackageInstaller; |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 78 | import android.content.pm.PackageInstaller.SessionInfo; |
Narayan Kamath | 9dfa674 | 2019-01-04 14:22:50 +0000 | [diff] [blame] | 79 | import android.content.pm.PackageInstaller.SessionInfo.StagedSessionErrorCode; |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 80 | import android.content.pm.PackageInstaller.SessionParams; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 81 | import android.content.pm.PackageManager; |
Todd Kennedy | c961a87 | 2020-01-24 14:08:14 -0800 | [diff] [blame] | 82 | import android.content.pm.PackageManagerInternal; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 83 | import android.content.pm.PackageParser; |
Jeff Sharkey | c4858a2 | 2014-06-16 10:51:20 -0700 | [diff] [blame] | 84 | import android.content.pm.PackageParser.ApkLite; |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 85 | import android.content.pm.PackageParser.PackageLite; |
Jeff Sharkey | 275e085 | 2014-06-17 18:18:49 -0700 | [diff] [blame] | 86 | import android.content.pm.PackageParser.PackageParserException; |
Sudheer Shanka | a05a994 | 2018-08-29 23:28:23 -0700 | [diff] [blame] | 87 | import android.content.pm.dex.DexMetadataHelper; |
Winson | 14ff717 | 2019-10-23 10:42:27 -0700 | [diff] [blame] | 88 | import android.content.pm.parsing.ApkLiteParseUtils; |
Winson | 3cb5610 | 2020-04-17 16:24:57 -0700 | [diff] [blame] | 89 | import android.content.pm.parsing.result.ParseResult; |
| 90 | import android.content.pm.parsing.result.ParseTypeImpl; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 91 | import android.graphics.Bitmap; |
| 92 | import android.graphics.BitmapFactory; |
Todd Kennedy | c25fbde | 2016-08-31 15:54:48 -0700 | [diff] [blame] | 93 | import android.os.Binder; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 94 | import android.os.Bundle; |
Jeff Sharkey | 78cc340 | 2014-05-22 10:52:49 -0700 | [diff] [blame] | 95 | import android.os.FileBridge; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 96 | import android.os.FileUtils; |
| 97 | import android.os.Handler; |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 98 | import android.os.IBinder; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 99 | import android.os.Looper; |
| 100 | import android.os.Message; |
| 101 | import android.os.ParcelFileDescriptor; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 102 | import android.os.ParcelableException; |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 103 | import android.os.Process; |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 104 | import android.os.RemoteException; |
Jeff Sharkey | 02d4e34 | 2017-03-10 21:53:48 -0700 | [diff] [blame] | 105 | import android.os.RevocableFileDescriptor; |
Patrick Baumann | 1bea237 | 2018-03-13 14:26:58 -0700 | [diff] [blame] | 106 | import android.os.SystemProperties; |
Jeff Sharkey | 57dcf5b | 2014-06-18 17:46:05 -0700 | [diff] [blame] | 107 | import android.os.UserHandle; |
Alex Buynytskyy | 8ef61ae | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 108 | import android.os.incremental.IStorageHealthListener; |
Songchun Fan | 4e75869 | 2019-11-29 15:43:27 -0800 | [diff] [blame] | 109 | import android.os.incremental.IncrementalFileStorages; |
Alex Buynytskyy | 8e9e6a3 | 2020-02-08 14:26:45 -0800 | [diff] [blame] | 110 | import android.os.incremental.IncrementalManager; |
Alex Buynytskyy | 8ef61ae | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 111 | import android.os.incremental.StorageHealthCheckParams; |
Jeff Sharkey | d5d5e92 | 2017-02-21 10:51:23 -0700 | [diff] [blame] | 112 | import android.os.storage.StorageManager; |
Todd Kennedy | c961a87 | 2020-01-24 14:08:14 -0800 | [diff] [blame] | 113 | import android.provider.Settings.Secure; |
Rubin Xu | 8b17ad0 | 2019-03-07 17:42:37 +0000 | [diff] [blame] | 114 | import android.stats.devicepolicy.DevicePolicyEnums; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 115 | import android.system.ErrnoException; |
Jeff Sharkey | 0451de6 | 2018-02-02 11:27:21 -0700 | [diff] [blame] | 116 | import android.system.Int64Ref; |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 117 | import android.system.Os; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 118 | import android.system.OsConstants; |
| 119 | import android.system.StructStat; |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 120 | import android.text.TextUtils; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 121 | import android.util.ArraySet; |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 122 | import android.util.ExceptionUtils; |
| 123 | import android.util.MathUtils; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 124 | import android.util.Slog; |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 125 | import android.util.SparseIntArray; |
Patrick Baumann | 420d58a | 2017-12-19 10:17:21 -0800 | [diff] [blame] | 126 | import android.util.apk.ApkSignatureVerifier; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 127 | |
Mohammad Samiul Islam | 10323a1 | 2020-03-17 10:04:43 +0000 | [diff] [blame] | 128 | import com.android.internal.R; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 129 | import com.android.internal.annotations.GuardedBy; |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 130 | import com.android.internal.content.NativeLibraryHelper; |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 131 | import com.android.internal.content.PackageHelper; |
Mohammad Samiul Islam | 10323a1 | 2020-03-17 10:04:43 +0000 | [diff] [blame] | 132 | import com.android.internal.messages.nano.SystemMessageProto; |
Philip P. Moltmann | 0e4ac4c | 2017-09-14 10:29:03 -0700 | [diff] [blame] | 133 | import com.android.internal.os.SomeArgs; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 134 | import com.android.internal.util.ArrayUtils; |
Songchun Fan | c296cf7 | 2020-03-31 18:38:52 -0700 | [diff] [blame] | 135 | import com.android.internal.util.FrameworkStatsLog; |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 136 | import com.android.internal.util.IndentingPrintWriter; |
Benjamin Franz | dabae88 | 2017-08-08 12:33:19 +0100 | [diff] [blame] | 137 | import com.android.server.LocalServices; |
Jeff Sharkey | 740f523 | 2016-12-09 14:31:26 -0700 | [diff] [blame] | 138 | import com.android.server.pm.Installer.InstallerException; |
Victor Hsieh | e7b5a8d | 2018-11-16 10:27:06 -0800 | [diff] [blame] | 139 | import com.android.server.pm.dex.DexManager; |
Winson | 5e0a1d5 | 2020-01-24 12:00:33 -0800 | [diff] [blame] | 140 | import com.android.server.pm.parsing.pkg.AndroidPackage; |
Victor Hsieh | c0cd748 | 2018-10-04 10:10:54 -0700 | [diff] [blame] | 141 | import com.android.server.security.VerityUtils; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 142 | |
Jeff Sharkey | d5d5e92 | 2017-02-21 10:51:23 -0700 | [diff] [blame] | 143 | import libcore.io.IoUtils; |
Alan Stokes | ee59d54 | 2020-02-04 17:35:15 +0000 | [diff] [blame] | 144 | import libcore.util.EmptyArray; |
Jeff Sharkey | d5d5e92 | 2017-02-21 10:51:23 -0700 | [diff] [blame] | 145 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 146 | import org.xmlpull.v1.XmlPullParser; |
| 147 | import org.xmlpull.v1.XmlPullParserException; |
| 148 | import org.xmlpull.v1.XmlSerializer; |
| 149 | |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 150 | import java.io.File; |
| 151 | import java.io.FileDescriptor; |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 152 | import java.io.FileFilter; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 153 | import java.io.FileOutputStream; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 154 | import java.io.IOException; |
| 155 | import java.util.ArrayList; |
Narayan Kamath | cd1fc14 | 2015-05-11 13:35:59 +0100 | [diff] [blame] | 156 | import java.util.Arrays; |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 157 | import java.util.List; |
Daulet Zhanguzin | 82adfcb | 2020-01-02 17:31:40 +0000 | [diff] [blame] | 158 | import java.util.Objects; |
Alex Buynytskyy | 476138c | 2019-12-20 14:41:47 -0800 | [diff] [blame] | 159 | import java.util.Set; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 160 | import java.util.concurrent.atomic.AtomicInteger; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 161 | |
| 162 | public class PackageInstallerSession extends IPackageInstallerSession.Stub { |
Dario Freni | aac4ba4 | 2018-12-06 15:47:16 +0000 | [diff] [blame] | 163 | private static final String TAG = "PackageInstallerSession"; |
Jeff Sharkey | 9a44577 | 2014-07-16 11:32:08 -0700 | [diff] [blame] | 164 | private static final boolean LOGD = true; |
Alex Buynytskyy | 0054993 | 2019-11-14 08:25:05 -0800 | [diff] [blame] | 165 | private static final String REMOVE_MARKER_EXTENSION = ".removed"; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 166 | |
Alex Buynytskyy | 133a628 | 2020-01-28 10:47:43 -0800 | [diff] [blame] | 167 | private static final int MSG_STREAM_VALIDATE_AND_COMMIT = 1; |
| 168 | private static final int MSG_INSTALL = 2; |
| 169 | private static final int MSG_ON_PACKAGE_INSTALLED = 3; |
Alex Buynytskyy | 64067b2 | 2020-04-25 15:56:52 -0700 | [diff] [blame] | 170 | private static final int MSG_SESSION_VERIFICATION_FAILURE = 4; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 171 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 172 | /** XML constants used for persisting a session */ |
| 173 | static final String TAG_SESSION = "session"; |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 174 | static final String TAG_CHILD_SESSION = "childSession"; |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 175 | static final String TAG_SESSION_FILE = "sessionFile"; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 176 | private static final String TAG_GRANTED_RUNTIME_PERMISSION = "granted-runtime-permission"; |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 177 | private static final String TAG_WHITELISTED_RESTRICTED_PERMISSION = |
| 178 | "whitelisted-restricted-permission"; |
Eugene Susla | 922cd08 | 2020-03-11 12:38:17 -0700 | [diff] [blame] | 179 | private static final String TAG_AUTO_REVOKE_PERMISSIONS_MODE = |
| 180 | "auto-revoke-permissions-mode"; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 181 | private static final String ATTR_SESSION_ID = "sessionId"; |
| 182 | private static final String ATTR_USER_ID = "userId"; |
| 183 | private static final String ATTR_INSTALLER_PACKAGE_NAME = "installerPackageName"; |
| 184 | private static final String ATTR_INSTALLER_UID = "installerUid"; |
Alan Stokes | 69d2abf | 2019-10-10 11:02:38 +0100 | [diff] [blame] | 185 | private static final String ATTR_INITIATING_PACKAGE_NAME = |
| 186 | "installInitiatingPackageName"; |
Alan Stokes | 5ed8537 | 2019-11-06 09:32:49 +0000 | [diff] [blame] | 187 | private static final String ATTR_ORIGINATING_PACKAGE_NAME = |
| 188 | "installOriginatingPackageName"; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 189 | private static final String ATTR_CREATED_MILLIS = "createdMillis"; |
Gavin Corkery | d831121 | 2019-02-22 17:52:30 +0000 | [diff] [blame] | 190 | private static final String ATTR_UPDATED_MILLIS = "updatedMillis"; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 191 | private static final String ATTR_SESSION_STAGE_DIR = "sessionStageDir"; |
| 192 | private static final String ATTR_SESSION_STAGE_CID = "sessionStageCid"; |
| 193 | private static final String ATTR_PREPARED = "prepared"; |
Dario Freni | 47799f4 | 2019-03-13 18:06:24 +0000 | [diff] [blame] | 194 | private static final String ATTR_COMMITTED = "committed"; |
Mohammad Samiul Islam | 731bd96 | 2020-04-23 16:23:21 +0100 | [diff] [blame] | 195 | private static final String ATTR_DESTROYED = "destroyed"; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 196 | private static final String ATTR_SEALED = "sealed"; |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 197 | private static final String ATTR_MULTI_PACKAGE = "multiPackage"; |
| 198 | private static final String ATTR_PARENT_SESSION_ID = "parentSessionId"; |
Dario Freni | aac4ba4 | 2018-12-06 15:47:16 +0000 | [diff] [blame] | 199 | private static final String ATTR_STAGED_SESSION = "stagedSession"; |
Dario Freni | 8e7d0ec | 2019-01-10 15:21:40 +0000 | [diff] [blame] | 200 | private static final String ATTR_IS_READY = "isReady"; |
| 201 | private static final String ATTR_IS_FAILED = "isFailed"; |
| 202 | private static final String ATTR_IS_APPLIED = "isApplied"; |
| 203 | private static final String ATTR_STAGED_SESSION_ERROR_CODE = "errorCode"; |
Dario Freni | 275b4ab | 2019-01-25 09:55:16 +0000 | [diff] [blame] | 204 | private static final String ATTR_STAGED_SESSION_ERROR_MESSAGE = "errorMessage"; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 205 | private static final String ATTR_MODE = "mode"; |
| 206 | private static final String ATTR_INSTALL_FLAGS = "installFlags"; |
| 207 | private static final String ATTR_INSTALL_LOCATION = "installLocation"; |
| 208 | private static final String ATTR_SIZE_BYTES = "sizeBytes"; |
| 209 | private static final String ATTR_APP_PACKAGE_NAME = "appPackageName"; |
| 210 | @Deprecated |
| 211 | private static final String ATTR_APP_ICON = "appIcon"; |
| 212 | private static final String ATTR_APP_LABEL = "appLabel"; |
| 213 | private static final String ATTR_ORIGINATING_URI = "originatingUri"; |
| 214 | private static final String ATTR_ORIGINATING_UID = "originatingUid"; |
| 215 | private static final String ATTR_REFERRER_URI = "referrerUri"; |
| 216 | private static final String ATTR_ABI_OVERRIDE = "abiOverride"; |
| 217 | private static final String ATTR_VOLUME_UUID = "volumeUuid"; |
| 218 | private static final String ATTR_NAME = "name"; |
| 219 | private static final String ATTR_INSTALL_REASON = "installRason"; |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 220 | private static final String ATTR_IS_DATALOADER = "isDataLoader"; |
| 221 | private static final String ATTR_DATALOADER_TYPE = "dataLoaderType"; |
| 222 | private static final String ATTR_DATALOADER_PACKAGE_NAME = "dataLoaderPackageName"; |
| 223 | private static final String ATTR_DATALOADER_CLASS_NAME = "dataLoaderClassName"; |
| 224 | private static final String ATTR_DATALOADER_ARGUMENTS = "dataLoaderArguments"; |
Alex Buynytskyy | e069787 | 2020-01-13 09:25:21 -0800 | [diff] [blame] | 225 | private static final String ATTR_LOCATION = "location"; |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 226 | private static final String ATTR_LENGTH_BYTES = "lengthBytes"; |
| 227 | private static final String ATTR_METADATA = "metadata"; |
Alex Buynytskyy | e069787 | 2020-01-13 09:25:21 -0800 | [diff] [blame] | 228 | private static final String ATTR_SIGNATURE = "signature"; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 229 | |
Patrick Baumann | 1bea237 | 2018-03-13 14:26:58 -0700 | [diff] [blame] | 230 | private static final String PROPERTY_NAME_INHERIT_NATIVE = "pi.inherit_native_on_dont_kill"; |
Alan Stokes | ee59d54 | 2020-02-04 17:35:15 +0000 | [diff] [blame] | 231 | private static final int[] EMPTY_CHILD_SESSION_ARRAY = EmptyArray.INT; |
Alex Buynytskyy | cd4d387 | 2020-02-08 17:50:50 -0800 | [diff] [blame] | 232 | private static final InstallationFile[] EMPTY_INSTALLATION_FILE_ARRAY = {}; |
Patrick Baumann | 1bea237 | 2018-03-13 14:26:58 -0700 | [diff] [blame] | 233 | |
Alex Buynytskyy | 476138c | 2019-12-20 14:41:47 -0800 | [diff] [blame] | 234 | private static final String SYSTEM_DATA_LOADER_PACKAGE = "android"; |
| 235 | |
Alex Buynytskyy | 8ef61ae | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 236 | private static final int INCREMENTAL_STORAGE_BLOCKED_TIMEOUT_MS = 2000; |
| 237 | private static final int INCREMENTAL_STORAGE_UNHEALTHY_TIMEOUT_MS = 7000; |
| 238 | private static final int INCREMENTAL_STORAGE_UNHEALTHY_MONITORING_MS = 60000; |
| 239 | |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 240 | // TODO: enforce INSTALL_ALLOW_TEST |
| 241 | // TODO: enforce INSTALL_ALLOW_DOWNGRADE |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 242 | |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 243 | private final PackageInstallerService.InternalCallback mCallback; |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 244 | private final Context mContext; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 245 | private final PackageManagerService mPm; |
| 246 | private final Handler mHandler; |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 247 | private final PackageSessionProvider mSessionProvider; |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 248 | private final StagingManager mStagingManager; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 249 | |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 250 | final int sessionId; |
| 251 | final int userId; |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 252 | final SessionParams params; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 253 | final long createdMillis; |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 254 | |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 255 | /** Staging location where client data is written. */ |
| 256 | final File stageDir; |
| 257 | final String stageCid; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 258 | |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 259 | private final AtomicInteger mActiveCount = new AtomicInteger(); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 260 | |
| 261 | private final Object mLock = new Object(); |
| 262 | |
Gavin Corkery | d831121 | 2019-02-22 17:52:30 +0000 | [diff] [blame] | 263 | /** Timestamp of the last time this session changed state */ |
| 264 | @GuardedBy("mLock") |
Gavin Corkery | 13f8161 | 2019-03-20 18:22:58 +0000 | [diff] [blame] | 265 | private long updatedMillis; |
Gavin Corkery | d831121 | 2019-02-22 17:52:30 +0000 | [diff] [blame] | 266 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 267 | /** Uid of the creator of this session. */ |
| 268 | private final int mOriginalInstallerUid; |
| 269 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 270 | /** Uid of the owner of the installer session */ |
| 271 | @GuardedBy("mLock") |
| 272 | private int mInstallerUid; |
| 273 | |
Alan Stokes | 69d2abf | 2019-10-10 11:02:38 +0100 | [diff] [blame] | 274 | /** Where this install request came from */ |
| 275 | @GuardedBy("mLock") |
| 276 | private InstallSource mInstallSource; |
| 277 | |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 278 | @GuardedBy("mLock") |
| 279 | private float mClientProgress = 0; |
| 280 | @GuardedBy("mLock") |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 281 | private float mInternalProgress = 0; |
| 282 | |
| 283 | @GuardedBy("mLock") |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 284 | private float mProgress = 0; |
| 285 | @GuardedBy("mLock") |
| 286 | private float mReportedProgress = -1; |
| 287 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 288 | /** State of the session. */ |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 289 | @GuardedBy("mLock") |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 290 | private boolean mPrepared = false; |
| 291 | @GuardedBy("mLock") |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 292 | private boolean mSealed = false; |
| 293 | @GuardedBy("mLock") |
shafik | 43f1af9 | 2019-03-14 15:14:00 +0000 | [diff] [blame] | 294 | private boolean mShouldBeSealed = false; |
| 295 | @GuardedBy("mLock") |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 296 | private boolean mCommitted = false; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 297 | @GuardedBy("mLock") |
Jeff Sharkey | 497c052 | 2015-05-12 13:07:14 -0700 | [diff] [blame] | 298 | private boolean mRelinquished = false; |
| 299 | @GuardedBy("mLock") |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 300 | private boolean mDestroyed = false; |
| 301 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 302 | /** Permissions have been accepted by the user (see {@link #setPermissionsResult}) */ |
| 303 | @GuardedBy("mLock") |
| 304 | private boolean mPermissionsManuallyAccepted = false; |
| 305 | |
| 306 | @GuardedBy("mLock") |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 307 | private int mFinalStatus; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 308 | @GuardedBy("mLock") |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 309 | private String mFinalMessage; |
| 310 | |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 311 | @GuardedBy("mLock") |
Jeff Sharkey | 02d4e34 | 2017-03-10 21:53:48 -0700 | [diff] [blame] | 312 | private final ArrayList<RevocableFileDescriptor> mFds = new ArrayList<>(); |
| 313 | @GuardedBy("mLock") |
| 314 | private final ArrayList<FileBridge> mBridges = new ArrayList<>(); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 315 | |
| 316 | @GuardedBy("mLock") |
Alex Buynytskyy | 2b1d32a | 2019-11-14 10:06:30 -0800 | [diff] [blame] | 317 | private IntentSender mRemoteStatusReceiver; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 318 | |
| 319 | /** Fields derived from commit parsing */ |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 320 | @GuardedBy("mLock") |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 321 | private String mPackageName; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 322 | @GuardedBy("mLock") |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 323 | private long mVersionCode; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 324 | @GuardedBy("mLock") |
Patrick Baumann | 420d58a | 2017-12-19 10:17:21 -0800 | [diff] [blame] | 325 | private PackageParser.SigningDetails mSigningDetails; |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 326 | @GuardedBy("mLock") |
| 327 | private SparseIntArray mChildSessionIds = new SparseIntArray(); |
| 328 | @GuardedBy("mLock") |
| 329 | private int mParentSessionId; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 330 | |
Alex Buynytskyy | c282de9 | 2020-03-10 10:27:42 -0700 | [diff] [blame] | 331 | static class FileEntry { |
| 332 | private final int mIndex; |
| 333 | private final InstallationFile mFile; |
| 334 | |
| 335 | FileEntry(int index, InstallationFile file) { |
| 336 | this.mIndex = index; |
| 337 | this.mFile = file; |
| 338 | } |
| 339 | |
| 340 | int getIndex() { |
| 341 | return this.mIndex; |
| 342 | } |
| 343 | |
| 344 | InstallationFile getFile() { |
| 345 | return this.mFile; |
| 346 | } |
| 347 | |
| 348 | @Override |
| 349 | public boolean equals(Object obj) { |
| 350 | if (!(obj instanceof FileEntry)) { |
| 351 | return false; |
| 352 | } |
| 353 | final FileEntry rhs = (FileEntry) obj; |
| 354 | return (mFile.getLocation() == rhs.mFile.getLocation()) && TextUtils.equals( |
| 355 | mFile.getName(), rhs.mFile.getName()); |
| 356 | } |
| 357 | |
| 358 | @Override |
| 359 | public int hashCode() { |
| 360 | return Objects.hash(mFile.getLocation(), mFile.getName()); |
| 361 | } |
| 362 | } |
| 363 | |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 364 | @GuardedBy("mLock") |
Alex Buynytskyy | c282de9 | 2020-03-10 10:27:42 -0700 | [diff] [blame] | 365 | private ArraySet<FileEntry> mFiles = new ArraySet<>(); |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 366 | |
Dario Freni | 71eee5e | 2018-12-06 15:47:16 +0000 | [diff] [blame] | 367 | @GuardedBy("mLock") |
| 368 | private boolean mStagedSessionApplied; |
| 369 | @GuardedBy("mLock") |
| 370 | private boolean mStagedSessionReady; |
| 371 | @GuardedBy("mLock") |
| 372 | private boolean mStagedSessionFailed; |
| 373 | @GuardedBy("mLock") |
Dario Freni | b6d2896 | 2019-01-31 15:52:24 +0000 | [diff] [blame] | 374 | private int mStagedSessionErrorCode = SessionInfo.STAGED_SESSION_NO_ERROR; |
Dario Freni | 275b4ab | 2019-01-25 09:55:16 +0000 | [diff] [blame] | 375 | @GuardedBy("mLock") |
| 376 | private String mStagedSessionErrorMessage; |
Dario Freni | 71eee5e | 2018-12-06 15:47:16 +0000 | [diff] [blame] | 377 | |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 378 | /** |
| 379 | * Path to the validated base APK for this session, which may point at an |
| 380 | * APK inside the session (when the session defines the base), or it may |
| 381 | * point at the existing base APK (when adding splits to an existing app). |
| 382 | * <p> |
| 383 | * This is used when confirming permissions, since we can't fully stage the |
| 384 | * session inside an ASEC before confirming with user. |
| 385 | */ |
| 386 | @GuardedBy("mLock") |
| 387 | private File mResolvedBaseFile; |
| 388 | |
| 389 | @GuardedBy("mLock") |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 390 | private final List<File> mResolvedStagedFiles = new ArrayList<>(); |
| 391 | @GuardedBy("mLock") |
| 392 | private final List<File> mResolvedInheritedFiles = new ArrayList<>(); |
Narayan Kamath | cd1fc14 | 2015-05-11 13:35:59 +0100 | [diff] [blame] | 393 | @GuardedBy("mLock") |
Narayan Kamath | e845a1e | 2015-06-05 11:59:26 +0100 | [diff] [blame] | 394 | private final List<String> mResolvedInstructionSets = new ArrayList<>(); |
| 395 | @GuardedBy("mLock") |
Patrick Baumann | 1bea237 | 2018-03-13 14:26:58 -0700 | [diff] [blame] | 396 | private final List<String> mResolvedNativeLibPaths = new ArrayList<>(); |
| 397 | @GuardedBy("mLock") |
Narayan Kamath | cd1fc14 | 2015-05-11 13:35:59 +0100 | [diff] [blame] | 398 | private File mInheritedFilesBase; |
Victor Hsieh | c0cd748 | 2018-10-04 10:10:54 -0700 | [diff] [blame] | 399 | @GuardedBy("mLock") |
| 400 | private boolean mVerityFound; |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 401 | |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 402 | private boolean mDataLoaderFinished = false; |
| 403 | |
JW Wang | 8a207c4 | 2020-06-22 16:22:08 +0800 | [diff] [blame] | 404 | // TODO(b/159663586): should be protected by mLock |
Songchun Fan | 4e75869 | 2019-11-29 15:43:27 -0800 | [diff] [blame] | 405 | private IncrementalFileStorages mIncrementalFileStorages; |
| 406 | |
Alex Buynytskyy | eb5f7cd | 2020-01-23 17:56:48 -0800 | [diff] [blame] | 407 | private static final FileFilter sAddedApkFilter = new FileFilter() { |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 408 | @Override |
| 409 | public boolean accept(File file) { |
| 410 | // Installers can't stage directories, so it's fine to ignore |
| 411 | // entries like "lost+found". |
| 412 | if (file.isDirectory()) return false; |
Alex Buynytskyy | 0054993 | 2019-11-14 08:25:05 -0800 | [diff] [blame] | 413 | if (file.getName().endsWith(REMOVE_MARKER_EXTENSION)) return false; |
Calin Juravle | 3fc56c3 | 2017-12-11 18:26:13 -0800 | [diff] [blame] | 414 | if (DexMetadataHelper.isDexMetadataFile(file)) return false; |
Victor Hsieh | c0cd748 | 2018-10-04 10:10:54 -0700 | [diff] [blame] | 415 | if (VerityUtils.isFsveritySignatureFile(file)) return false; |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 416 | return true; |
| 417 | } |
| 418 | }; |
Alex Buynytskyy | eb5f7cd | 2020-01-23 17:56:48 -0800 | [diff] [blame] | 419 | private static final FileFilter sAddedFilter = new FileFilter() { |
| 420 | @Override |
| 421 | public boolean accept(File file) { |
| 422 | // Installers can't stage directories, so it's fine to ignore |
| 423 | // entries like "lost+found". |
| 424 | if (file.isDirectory()) return false; |
| 425 | if (file.getName().endsWith(REMOVE_MARKER_EXTENSION)) return false; |
| 426 | return true; |
| 427 | } |
| 428 | }; |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 429 | private static final FileFilter sRemovedFilter = new FileFilter() { |
| 430 | @Override |
| 431 | public boolean accept(File file) { |
| 432 | if (file.isDirectory()) return false; |
Alex Buynytskyy | 0054993 | 2019-11-14 08:25:05 -0800 | [diff] [blame] | 433 | if (!file.getName().endsWith(REMOVE_MARKER_EXTENSION)) return false; |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 434 | return true; |
| 435 | } |
| 436 | }; |
| 437 | |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 438 | private final Handler.Callback mHandlerCallback = new Handler.Callback() { |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 439 | @Override |
| 440 | public boolean handleMessage(Message msg) { |
Philip P. Moltmann | 0e4ac4c | 2017-09-14 10:29:03 -0700 | [diff] [blame] | 441 | switch (msg.what) { |
Alex Buynytskyy | 133a628 | 2020-01-28 10:47:43 -0800 | [diff] [blame] | 442 | case MSG_STREAM_VALIDATE_AND_COMMIT: |
| 443 | handleStreamValidateAndCommit(); |
Alex Buynytskyy | 15a17ba | 2019-11-14 16:28:09 -0800 | [diff] [blame] | 444 | break; |
Alex Buynytskyy | 133a628 | 2020-01-28 10:47:43 -0800 | [diff] [blame] | 445 | case MSG_INSTALL: |
| 446 | handleInstall(); |
Philip P. Moltmann | 0e4ac4c | 2017-09-14 10:29:03 -0700 | [diff] [blame] | 447 | break; |
| 448 | case MSG_ON_PACKAGE_INSTALLED: |
Alex Buynytskyy | 354d669 | 2020-01-17 15:23:38 +0000 | [diff] [blame] | 449 | final SomeArgs args = (SomeArgs) msg.obj; |
| 450 | final String packageName = (String) args.arg1; |
Philip P. Moltmann | 0e4ac4c | 2017-09-14 10:29:03 -0700 | [diff] [blame] | 451 | final String message = (String) args.arg2; |
| 452 | final Bundle extras = (Bundle) args.arg3; |
Alex Buynytskyy | 354d669 | 2020-01-17 15:23:38 +0000 | [diff] [blame] | 453 | final IntentSender statusReceiver = (IntentSender) args.arg4; |
Philip P. Moltmann | 0e4ac4c | 2017-09-14 10:29:03 -0700 | [diff] [blame] | 454 | final int returnCode = args.argi1; |
| 455 | args.recycle(); |
| 456 | |
Mohammad Samiul Islam | 10323a1 | 2020-03-17 10:04:43 +0000 | [diff] [blame] | 457 | sendOnPackageInstalled(mContext, statusReceiver, sessionId, |
Alex Buynytskyy | 2b1d32a | 2019-11-14 10:06:30 -0800 | [diff] [blame] | 458 | isInstallerDeviceOwnerOrAffiliatedProfileOwnerLocked(), userId, |
| 459 | packageName, returnCode, message, extras); |
Philip P. Moltmann | 0e4ac4c | 2017-09-14 10:29:03 -0700 | [diff] [blame] | 460 | |
| 461 | break; |
Alex Buynytskyy | 64067b2 | 2020-04-25 15:56:52 -0700 | [diff] [blame] | 462 | case MSG_SESSION_VERIFICATION_FAILURE: |
| 463 | final int error = msg.arg1; |
| 464 | final String detailMessage = (String) msg.obj; |
| 465 | onSessionVerificationFailure(error, detailMessage); |
| 466 | break; |
Philip P. Moltmann | 9890f8b | 2017-08-08 10:49:38 -0700 | [diff] [blame] | 467 | } |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 468 | |
| 469 | return true; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 470 | } |
| 471 | }; |
| 472 | |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 473 | private boolean isDataLoaderInstallation() { |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 474 | return params.dataLoaderParams != null; |
| 475 | } |
| 476 | |
| 477 | private boolean isStreamingInstallation() { |
| 478 | return isDataLoaderInstallation() && params.dataLoaderParams.getType() == STREAMING; |
| 479 | } |
| 480 | |
| 481 | private boolean isIncrementalInstallation() { |
| 482 | return isDataLoaderInstallation() && params.dataLoaderParams.getType() == INCREMENTAL; |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 483 | } |
| 484 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 485 | /** |
Benjamin Franz | dabae88 | 2017-08-08 12:33:19 +0100 | [diff] [blame] | 486 | * @return {@code true} iff the installing is app an device owner or affiliated profile owner. |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 487 | */ |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 488 | @GuardedBy("mLock") |
Benjamin Franz | dabae88 | 2017-08-08 12:33:19 +0100 | [diff] [blame] | 489 | private boolean isInstallerDeviceOwnerOrAffiliatedProfileOwnerLocked() { |
Rubin Xu | fd4a3b4 | 2018-12-05 16:03:27 +0000 | [diff] [blame] | 490 | if (userId != UserHandle.getUserId(mInstallerUid)) { |
| 491 | return false; |
| 492 | } |
Benjamin Franz | dabae88 | 2017-08-08 12:33:19 +0100 | [diff] [blame] | 493 | DevicePolicyManagerInternal dpmi = |
| 494 | LocalServices.getService(DevicePolicyManagerInternal.class); |
Alan Stokes | 819fea2 | 2019-10-16 16:54:09 +0100 | [diff] [blame] | 495 | return dpmi != null && dpmi.canSilentlyInstallPackage( |
| 496 | mInstallSource.installerPackageName, mInstallerUid); |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | /** |
| 500 | * Checks if the permissions still need to be confirmed. |
| 501 | * |
| 502 | * <p>This is dependant on the identity of the installer, hence this cannot be cached if the |
Alex Buynytskyy | 354d669 | 2020-01-17 15:23:38 +0000 | [diff] [blame] | 503 | * installer might still {@link #transfer(String) change}. |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 504 | * |
| 505 | * @return {@code true} iff we need to ask to confirm the permissions? |
| 506 | */ |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 507 | @GuardedBy("mLock") |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 508 | private boolean needToAskForPermissionsLocked() { |
| 509 | if (mPermissionsManuallyAccepted) { |
| 510 | return false; |
| 511 | } |
| 512 | |
Chad Brubaker | cfdc1ee | 2017-08-18 12:52:47 -0700 | [diff] [blame] | 513 | final boolean isInstallPermissionGranted = |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 514 | (mPm.checkUidPermission(android.Manifest.permission.INSTALL_PACKAGES, |
| 515 | mInstallerUid) == PackageManager.PERMISSION_GRANTED); |
Chad Brubaker | cfdc1ee | 2017-08-18 12:52:47 -0700 | [diff] [blame] | 516 | final boolean isSelfUpdatePermissionGranted = |
| 517 | (mPm.checkUidPermission(android.Manifest.permission.INSTALL_SELF_UPDATES, |
| 518 | mInstallerUid) == PackageManager.PERMISSION_GRANTED); |
Chad Brubaker | adcb522 | 2018-01-11 14:22:15 -0800 | [diff] [blame] | 519 | final boolean isUpdatePermissionGranted = |
| 520 | (mPm.checkUidPermission(android.Manifest.permission.INSTALL_PACKAGE_UPDATES, |
| 521 | mInstallerUid) == PackageManager.PERMISSION_GRANTED); |
| 522 | final int targetPackageUid = mPm.getPackageUid(mPackageName, 0, userId); |
Chad Brubaker | cfdc1ee | 2017-08-18 12:52:47 -0700 | [diff] [blame] | 523 | final boolean isPermissionGranted = isInstallPermissionGranted |
Chad Brubaker | adcb522 | 2018-01-11 14:22:15 -0800 | [diff] [blame] | 524 | || (isUpdatePermissionGranted && targetPackageUid != -1) |
| 525 | || (isSelfUpdatePermissionGranted && targetPackageUid == mInstallerUid); |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 526 | final boolean isInstallerRoot = (mInstallerUid == Process.ROOT_UID); |
Philip P. Moltmann | 79c238a | 2017-12-13 15:59:07 -0800 | [diff] [blame] | 527 | final boolean isInstallerSystem = (mInstallerUid == Process.SYSTEM_UID); |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 528 | final boolean forcePermissionPrompt = |
| 529 | (params.installFlags & PackageManager.INSTALL_FORCE_PERMISSION_PROMPT) != 0; |
| 530 | |
Benjamin Franz | dabae88 | 2017-08-08 12:33:19 +0100 | [diff] [blame] | 531 | // Device owners and affiliated profile owners are allowed to silently install packages, so |
| 532 | // the permission check is waived if the installer is the device owner. |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 533 | return forcePermissionPrompt || !(isPermissionGranted || isInstallerRoot |
Philip P. Moltmann | 79c238a | 2017-12-13 15:59:07 -0800 | [diff] [blame] | 534 | || isInstallerSystem || isInstallerDeviceOwnerOrAffiliatedProfileOwnerLocked()); |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 535 | } |
| 536 | |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 537 | public PackageInstallerSession(PackageInstallerService.InternalCallback callback, |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 538 | Context context, PackageManagerService pm, |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 539 | PackageSessionProvider sessionProvider, Looper looper, StagingManager stagingManager, |
Alan Stokes | 819fea2 | 2019-10-16 16:54:09 +0100 | [diff] [blame] | 540 | int sessionId, int userId, int installerUid, @NonNull InstallSource installSource, |
Alan Stokes | 69d2abf | 2019-10-10 11:02:38 +0100 | [diff] [blame] | 541 | SessionParams params, long createdMillis, |
Alex Buynytskyy | cd4d387 | 2020-02-08 17:50:50 -0800 | [diff] [blame] | 542 | File stageDir, String stageCid, InstallationFile[] files, boolean prepared, |
Mohammad Samiul Islam | 731bd96 | 2020-04-23 16:23:21 +0100 | [diff] [blame] | 543 | boolean committed, boolean destroyed, boolean sealed, |
Dario Freni | 8e7d0ec | 2019-01-10 15:21:40 +0000 | [diff] [blame] | 544 | @Nullable int[] childSessionIds, int parentSessionId, boolean isReady, |
Dario Freni | 275b4ab | 2019-01-25 09:55:16 +0000 | [diff] [blame] | 545 | boolean isFailed, boolean isApplied, int stagedSessionErrorCode, |
| 546 | String stagedSessionErrorMessage) { |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 547 | mCallback = callback; |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 548 | mContext = context; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 549 | mPm = pm; |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 550 | mSessionProvider = sessionProvider; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 551 | mHandler = new Handler(looper, mHandlerCallback); |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 552 | mStagingManager = stagingManager; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 553 | |
| 554 | this.sessionId = sessionId; |
| 555 | this.userId = userId; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 556 | mOriginalInstallerUid = installerUid; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 557 | mInstallerUid = installerUid; |
Daulet Zhanguzin | 82adfcb | 2020-01-02 17:31:40 +0000 | [diff] [blame] | 558 | mInstallSource = Objects.requireNonNull(installSource); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 559 | this.params = params; |
| 560 | this.createdMillis = createdMillis; |
Gavin Corkery | d831121 | 2019-02-22 17:52:30 +0000 | [diff] [blame] | 561 | this.updatedMillis = createdMillis; |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 562 | this.stageDir = stageDir; |
| 563 | this.stageCid = stageCid; |
shafik | 43f1af9 | 2019-03-14 15:14:00 +0000 | [diff] [blame] | 564 | this.mShouldBeSealed = sealed; |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 565 | if (childSessionIds != null) { |
| 566 | for (int childSessionId : childSessionIds) { |
| 567 | mChildSessionIds.put(childSessionId, 0); |
| 568 | } |
| 569 | } |
| 570 | this.mParentSessionId = parentSessionId; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 571 | |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 572 | if (files != null) { |
Alex Buynytskyy | c282de9 | 2020-03-10 10:27:42 -0700 | [diff] [blame] | 573 | for (int i = 0, size = files.length; i < size; ++i) { |
| 574 | InstallationFile file = files[i]; |
| 575 | if (!mFiles.add(new FileEntry(i, file))) { |
| 576 | throw new IllegalArgumentException( |
| 577 | "Trying to add a duplicate installation file"); |
| 578 | } |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 579 | } |
| 580 | } |
| 581 | |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 582 | if (!params.isMultiPackage && (stageDir == null) == (stageCid == null)) { |
Jeff Sharkey | bb7b7be | 2014-08-19 16:18:28 -0700 | [diff] [blame] | 583 | throw new IllegalArgumentException( |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 584 | "Exactly one of stageDir or stageCid stage must be set"); |
Jeff Sharkey | bb7b7be | 2014-08-19 16:18:28 -0700 | [diff] [blame] | 585 | } |
| 586 | |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 587 | mPrepared = prepared; |
Dario Freni | 47799f4 | 2019-03-13 18:06:24 +0000 | [diff] [blame] | 588 | mCommitted = committed; |
Mohammad Samiul Islam | 731bd96 | 2020-04-23 16:23:21 +0100 | [diff] [blame] | 589 | mDestroyed = destroyed; |
Dario Freni | 8e7d0ec | 2019-01-10 15:21:40 +0000 | [diff] [blame] | 590 | mStagedSessionReady = isReady; |
| 591 | mStagedSessionFailed = isFailed; |
| 592 | mStagedSessionApplied = isApplied; |
| 593 | mStagedSessionErrorCode = stagedSessionErrorCode; |
Dario Freni | 275b4ab | 2019-01-25 09:55:16 +0000 | [diff] [blame] | 594 | mStagedSessionErrorMessage = |
| 595 | stagedSessionErrorMessage != null ? stagedSessionErrorMessage : ""; |
Songchun Fan | 4e75869 | 2019-11-29 15:43:27 -0800 | [diff] [blame] | 596 | |
Alex Buynytskyy | ae3eabe | 2020-02-03 15:00:21 -0800 | [diff] [blame] | 597 | if (isDataLoaderInstallation()) { |
| 598 | if (isApexInstallation()) { |
| 599 | throw new IllegalArgumentException( |
| 600 | "DataLoader installation of APEX modules is not allowed."); |
| 601 | } |
Alex Buynytskyy | ae3eabe | 2020-02-03 15:00:21 -0800 | [diff] [blame] | 602 | if (this.params.dataLoaderParams.getComponentName().getPackageName() |
| 603 | == SYSTEM_DATA_LOADER_PACKAGE) { |
| 604 | assertShellOrSystemCalling("System data loaders"); |
| 605 | } |
Alex Buynytskyy | 476138c | 2019-12-20 14:41:47 -0800 | [diff] [blame] | 606 | } |
Alex Buynytskyy | 8e9e6a3 | 2020-02-08 14:26:45 -0800 | [diff] [blame] | 607 | |
| 608 | if (isIncrementalInstallation()) { |
| 609 | if (!IncrementalManager.isAllowed()) { |
| 610 | throw new IllegalArgumentException("Incremental installation not allowed."); |
| 611 | } |
| 612 | if (!isIncrementalInstallationAllowed(mPackageName)) { |
| 613 | throw new IllegalArgumentException( |
| 614 | "Incremental installation of this package is not allowed."); |
| 615 | } |
| 616 | } |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 617 | } |
| 618 | |
Patrick Baumann | 6bc126b | 2020-03-06 10:34:17 -0800 | [diff] [blame] | 619 | /** |
| 620 | * Returns {@code true} if the {@link SessionInfo} object should be produced with potentially |
| 621 | * sensitive data scrubbed from its fields. |
| 622 | * |
| 623 | * @param callingUid the uid of the caller; the recipient of the {@link SessionInfo} that may |
| 624 | * need to be scrubbed |
| 625 | */ |
| 626 | private boolean shouldScrubData(int callingUid) { |
| 627 | return !(callingUid < Process.FIRST_APPLICATION_UID || getInstallerUid() == callingUid); |
Jeff Sharkey | da1247a | 2017-06-08 14:13:29 -0600 | [diff] [blame] | 628 | } |
| 629 | |
Patrick Baumann | 6bc126b | 2020-03-06 10:34:17 -0800 | [diff] [blame] | 630 | /** |
| 631 | * Generates a {@link SessionInfo} object for the provided uid. This may result in some fields |
| 632 | * that may contain sensitive info being filtered. |
| 633 | * |
| 634 | * @param includeIcon true if the icon should be included in the object |
| 635 | * @param callingUid the uid of the caller; the recipient of the {@link SessionInfo} that may |
| 636 | * need to be scrubbed |
| 637 | * @see #shouldScrubData(int) |
| 638 | */ |
| 639 | public SessionInfo generateInfoForCaller(boolean includeIcon, int callingUid) { |
| 640 | return generateInfoInternal(includeIcon, shouldScrubData(callingUid)); |
| 641 | } |
| 642 | |
| 643 | /** |
| 644 | * Generates a {@link SessionInfo} object to ensure proper hiding of sensitive fields. |
| 645 | * |
| 646 | * @param includeIcon true if the icon should be included in the object |
| 647 | * @see #generateInfoForCaller(boolean, int) |
| 648 | */ |
| 649 | public SessionInfo generateInfoScrubbed(boolean includeIcon) { |
| 650 | return generateInfoInternal(includeIcon, true /*scrubData*/); |
| 651 | } |
| 652 | |
| 653 | private SessionInfo generateInfoInternal(boolean includeIcon, boolean scrubData) { |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 654 | final SessionInfo info = new SessionInfo(); |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 655 | synchronized (mLock) { |
| 656 | info.sessionId = sessionId; |
Jon Miranda | 2b340a2 | 2019-01-25 14:03:49 -0800 | [diff] [blame] | 657 | info.userId = userId; |
Alan Stokes | 819fea2 | 2019-10-16 16:54:09 +0100 | [diff] [blame] | 658 | info.installerPackageName = mInstallSource.installerPackageName; |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 659 | info.resolvedBaseCodePath = (mResolvedBaseFile != null) ? |
| 660 | mResolvedBaseFile.getAbsolutePath() : null; |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 661 | info.progress = mProgress; |
| 662 | info.sealed = mSealed; |
Nikita Ioffe | 00a08f1 | 2019-03-07 20:55:08 +0000 | [diff] [blame] | 663 | info.isCommitted = mCommitted; |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 664 | info.active = mActiveCount.get() > 0; |
Jeff Sharkey | bb58067 | 2014-07-10 12:10:25 -0700 | [diff] [blame] | 665 | |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 666 | info.mode = params.mode; |
Sunny Goyal | 6d7cb23 | 2017-01-30 10:43:18 -0800 | [diff] [blame] | 667 | info.installReason = params.installReason; |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 668 | info.sizeBytes = params.sizeBytes; |
| 669 | info.appPackageName = params.appPackageName; |
Jeff Sharkey | da1247a | 2017-06-08 14:13:29 -0600 | [diff] [blame] | 670 | if (includeIcon) { |
| 671 | info.appIcon = params.appIcon; |
| 672 | } |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 673 | info.appLabel = params.appLabel; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 674 | |
| 675 | info.installLocation = params.installLocation; |
Patrick Baumann | 6bc126b | 2020-03-06 10:34:17 -0800 | [diff] [blame] | 676 | if (!scrubData) { |
| 677 | info.originatingUri = params.originatingUri; |
| 678 | } |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 679 | info.originatingUid = params.originatingUid; |
Patrick Baumann | 6bc126b | 2020-03-06 10:34:17 -0800 | [diff] [blame] | 680 | if (!scrubData) { |
| 681 | info.referrerUri = params.referrerUri; |
| 682 | } |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 683 | info.grantedRuntimePermissions = params.grantedRuntimePermissions; |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 684 | info.whitelistedRestrictedPermissions = params.whitelistedRestrictedPermissions; |
Eugene Susla | 922cd08 | 2020-03-11 12:38:17 -0700 | [diff] [blame] | 685 | info.autoRevokePermissionsMode = params.autoRevokePermissionsMode; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 686 | info.installFlags = params.installFlags; |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 687 | info.isMultiPackage = params.isMultiPackage; |
Dario Freni | aac4ba4 | 2018-12-06 15:47:16 +0000 | [diff] [blame] | 688 | info.isStaged = params.isStaged; |
JW Wang | 0bb6808 | 2019-12-05 18:00:06 +0800 | [diff] [blame] | 689 | info.rollbackDataPolicy = params.rollbackDataPolicy; |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 690 | info.parentSessionId = mParentSessionId; |
| 691 | info.childSessionIds = mChildSessionIds.copyKeys(); |
| 692 | if (info.childSessionIds == null) { |
| 693 | info.childSessionIds = EMPTY_CHILD_SESSION_ARRAY; |
| 694 | } |
Dario Freni | 60a96c1 | 2019-02-24 21:01:29 +0000 | [diff] [blame] | 695 | info.isStagedSessionApplied = mStagedSessionApplied; |
| 696 | info.isStagedSessionReady = mStagedSessionReady; |
| 697 | info.isStagedSessionFailed = mStagedSessionFailed; |
Dario Freni | 275b4ab | 2019-01-25 09:55:16 +0000 | [diff] [blame] | 698 | info.setStagedSessionErrorCode(mStagedSessionErrorCode, mStagedSessionErrorMessage); |
Pinyao Ting | bc969f4 | 2019-12-09 15:15:01 -0800 | [diff] [blame] | 699 | info.createdMillis = createdMillis; |
Dario Freni | 56c14dd | 2019-04-03 16:20:22 +0100 | [diff] [blame] | 700 | info.updatedMillis = updatedMillis; |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 701 | } |
Jeff Sharkey | bb58067 | 2014-07-10 12:10:25 -0700 | [diff] [blame] | 702 | return info; |
| 703 | } |
| 704 | |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 705 | public boolean isPrepared() { |
| 706 | synchronized (mLock) { |
| 707 | return mPrepared; |
| 708 | } |
| 709 | } |
| 710 | |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 711 | public boolean isSealed() { |
| 712 | synchronized (mLock) { |
| 713 | return mSealed; |
| 714 | } |
| 715 | } |
| 716 | |
Nikita Ioffe | da998cf | 2019-03-04 22:54:30 +0000 | [diff] [blame] | 717 | /** {@hide} */ |
| 718 | boolean isCommitted() { |
| 719 | synchronized (mLock) { |
| 720 | return mCommitted; |
| 721 | } |
| 722 | } |
| 723 | |
Mohammad Samiul Islam | 731bd96 | 2020-04-23 16:23:21 +0100 | [diff] [blame] | 724 | /** {@hide} */ |
| 725 | boolean isDestroyed() { |
| 726 | synchronized (mLock) { |
| 727 | return mDestroyed; |
| 728 | } |
| 729 | } |
| 730 | |
Gavin Corkery | d831121 | 2019-02-22 17:52:30 +0000 | [diff] [blame] | 731 | /** Returns true if a staged session has reached a final state and can be forgotten about */ |
| 732 | public boolean isStagedAndInTerminalState() { |
| 733 | synchronized (mLock) { |
| 734 | return params.isStaged && (mStagedSessionApplied || mStagedSessionFailed); |
| 735 | } |
| 736 | } |
| 737 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 738 | @GuardedBy("mLock") |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 739 | private void assertPreparedAndNotSealedLocked(String cookie) { |
| 740 | assertPreparedAndNotCommittedOrDestroyedLocked(cookie); |
| 741 | if (mSealed) { |
| 742 | throw new SecurityException(cookie + " not allowed after sealing"); |
| 743 | } |
| 744 | } |
| 745 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 746 | @GuardedBy("mLock") |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 747 | private void assertPreparedAndNotCommittedOrDestroyedLocked(String cookie) { |
| 748 | assertPreparedAndNotDestroyedLocked(cookie); |
| 749 | if (mCommitted) { |
| 750 | throw new SecurityException(cookie + " not allowed after commit"); |
| 751 | } |
| 752 | } |
| 753 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 754 | @GuardedBy("mLock") |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 755 | private void assertPreparedAndNotDestroyedLocked(String cookie) { |
| 756 | if (!mPrepared) { |
| 757 | throw new IllegalStateException(cookie + " before prepared"); |
| 758 | } |
| 759 | if (mDestroyed) { |
| 760 | throw new SecurityException(cookie + " not allowed after destruction"); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 761 | } |
| 762 | } |
| 763 | |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 764 | @GuardedBy("mLock") |
| 765 | private void setClientProgressLocked(float progress) { |
| 766 | // Always publish first staging movement |
| 767 | final boolean forcePublish = (mClientProgress == 0); |
| 768 | mClientProgress = progress; |
| 769 | computeProgressLocked(forcePublish); |
| 770 | } |
| 771 | |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 772 | @Override |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 773 | public void setClientProgress(float progress) { |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 774 | synchronized (mLock) { |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 775 | assertCallerIsOwnerOrRootLocked(); |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 776 | setClientProgressLocked(progress); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 777 | } |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | @Override |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 781 | public void addClientProgress(float progress) { |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 782 | synchronized (mLock) { |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 783 | assertCallerIsOwnerOrRootLocked(); |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 784 | setClientProgressLocked(mClientProgress + progress); |
Jeff Sharkey | ec9bad2 | 2014-09-05 09:45:20 -0700 | [diff] [blame] | 785 | } |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 786 | } |
| 787 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 788 | @GuardedBy("mLock") |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 789 | private void computeProgressLocked(boolean forcePublish) { |
| 790 | mProgress = MathUtils.constrain(mClientProgress * 0.8f, 0f, 0.8f) |
| 791 | + MathUtils.constrain(mInternalProgress * 0.2f, 0f, 0.2f); |
| 792 | |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 793 | // Only publish when meaningful change |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 794 | if (forcePublish || Math.abs(mProgress - mReportedProgress) >= 0.01) { |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 795 | mReportedProgress = mProgress; |
| 796 | mCallback.onSessionProgressChanged(this, mProgress); |
| 797 | } |
| 798 | } |
| 799 | |
| 800 | @Override |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 801 | public String[] getNames() { |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 802 | synchronized (mLock) { |
| 803 | assertCallerIsOwnerOrRootLocked(); |
| 804 | assertPreparedAndNotCommittedOrDestroyedLocked("getNames"); |
| 805 | |
Alex Buynytskyy | 0054993 | 2019-11-14 08:25:05 -0800 | [diff] [blame] | 806 | return getNamesLocked(); |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 807 | } |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 808 | } |
| 809 | |
Alex Buynytskyy | 0054993 | 2019-11-14 08:25:05 -0800 | [diff] [blame] | 810 | @GuardedBy("mLock") |
| 811 | private String[] getNamesLocked() { |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 812 | if (!isDataLoaderInstallation()) { |
Alex Buynytskyy | 769f815 | 2020-01-23 16:58:45 +0000 | [diff] [blame] | 813 | String[] result = stageDir.list(); |
| 814 | if (result == null) { |
Alan Stokes | ee59d54 | 2020-02-04 17:35:15 +0000 | [diff] [blame] | 815 | result = EmptyArray.STRING; |
Alex Buynytskyy | 769f815 | 2020-01-23 16:58:45 +0000 | [diff] [blame] | 816 | } |
| 817 | return result; |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 818 | } |
Alex Buynytskyy | c0c5a37 | 2020-01-29 18:59:20 -0800 | [diff] [blame] | 819 | |
Alex Buynytskyy | c282de9 | 2020-03-10 10:27:42 -0700 | [diff] [blame] | 820 | InstallationFile[] files = getInstallationFilesLocked(); |
| 821 | String[] result = new String[files.length]; |
| 822 | for (int i = 0, size = files.length; i < size; ++i) { |
| 823 | result[i] = files[i].getName(); |
| 824 | } |
| 825 | return result; |
| 826 | } |
| 827 | |
| 828 | @GuardedBy("mLock") |
| 829 | private InstallationFile[] getInstallationFilesLocked() { |
| 830 | final InstallationFile[] result = new InstallationFile[mFiles.size()]; |
| 831 | for (FileEntry fileEntry : mFiles) { |
| 832 | result[fileEntry.getIndex()] = fileEntry.getFile(); |
Alex Buynytskyy | c0c5a37 | 2020-01-29 18:59:20 -0800 | [diff] [blame] | 833 | } |
| 834 | return result; |
Alex Buynytskyy | 0054993 | 2019-11-14 08:25:05 -0800 | [diff] [blame] | 835 | } |
| 836 | |
Alex Buynytskyy | c0c5a37 | 2020-01-29 18:59:20 -0800 | [diff] [blame] | 837 | private static ArrayList<File> filterFiles(File parent, String[] names, FileFilter filter) { |
| 838 | ArrayList<File> result = new ArrayList<>(names.length); |
| 839 | for (String name : names) { |
| 840 | File file = new File(parent, name); |
| 841 | if (filter.accept(file)) { |
| 842 | result.add(file); |
| 843 | } |
| 844 | } |
| 845 | return result; |
Alex Buynytskyy | 0054993 | 2019-11-14 08:25:05 -0800 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | @GuardedBy("mLock") |
Alex Buynytskyy | c0c5a37 | 2020-01-29 18:59:20 -0800 | [diff] [blame] | 849 | private List<File> getAddedApksLocked() { |
Alex Buynytskyy | 0054993 | 2019-11-14 08:25:05 -0800 | [diff] [blame] | 850 | String[] names = getNamesLocked(); |
Alex Buynytskyy | eb5f7cd | 2020-01-23 17:56:48 -0800 | [diff] [blame] | 851 | return filterFiles(stageDir, names, sAddedApkFilter); |
Alex Buynytskyy | 0054993 | 2019-11-14 08:25:05 -0800 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | @GuardedBy("mLock") |
Alex Buynytskyy | c0c5a37 | 2020-01-29 18:59:20 -0800 | [diff] [blame] | 855 | private List<File> getRemovedFilesLocked() { |
Alex Buynytskyy | 0054993 | 2019-11-14 08:25:05 -0800 | [diff] [blame] | 856 | String[] names = getNamesLocked(); |
| 857 | return filterFiles(stageDir, names, sRemovedFilter); |
| 858 | } |
| 859 | |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 860 | @Override |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 861 | public void removeSplit(String splitName) { |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 862 | if (isDataLoaderInstallation()) { |
| 863 | throw new IllegalStateException( |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 864 | "Cannot remove splits in a data loader installation session."); |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 865 | } |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 866 | if (TextUtils.isEmpty(params.appPackageName)) { |
| 867 | throw new IllegalStateException("Must specify package name to remove a split"); |
| 868 | } |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 869 | |
| 870 | synchronized (mLock) { |
| 871 | assertCallerIsOwnerOrRootLocked(); |
| 872 | assertPreparedAndNotCommittedOrDestroyedLocked("removeSplit"); |
| 873 | |
| 874 | try { |
| 875 | createRemoveSplitMarkerLocked(splitName); |
| 876 | } catch (IOException e) { |
| 877 | throw ExceptionUtils.wrap(e); |
| 878 | } |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 879 | } |
| 880 | } |
| 881 | |
Alex Buynytskyy | 0054993 | 2019-11-14 08:25:05 -0800 | [diff] [blame] | 882 | private static String getRemoveMarkerName(String name) { |
| 883 | final String markerName = name + REMOVE_MARKER_EXTENSION; |
| 884 | if (!FileUtils.isValidExtFilename(markerName)) { |
| 885 | throw new IllegalArgumentException("Invalid marker: " + markerName); |
| 886 | } |
| 887 | return markerName; |
| 888 | } |
| 889 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 890 | private void createRemoveSplitMarkerLocked(String splitName) throws IOException { |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 891 | try { |
Alex Buynytskyy | 0054993 | 2019-11-14 08:25:05 -0800 | [diff] [blame] | 892 | final File target = new File(stageDir, getRemoveMarkerName(splitName)); |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 893 | target.createNewFile(); |
| 894 | Os.chmod(target.getAbsolutePath(), 0 /*mode*/); |
| 895 | } catch (ErrnoException e) { |
| 896 | throw e.rethrowAsIOException(); |
| 897 | } |
| 898 | } |
| 899 | |
Alex Buynytskyy | 476138c | 2019-12-20 14:41:47 -0800 | [diff] [blame] | 900 | private void assertShellOrSystemCalling(String operation) { |
| 901 | switch (Binder.getCallingUid()) { |
| 902 | case android.os.Process.SHELL_UID: |
| 903 | case android.os.Process.ROOT_UID: |
| 904 | case android.os.Process.SYSTEM_UID: |
| 905 | break; |
| 906 | default: |
| 907 | throw new SecurityException(operation + " only supported from shell or system"); |
| 908 | } |
| 909 | } |
| 910 | |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 911 | private void assertCanWrite(boolean reverseMode) { |
| 912 | if (isDataLoaderInstallation()) { |
| 913 | throw new IllegalStateException( |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 914 | "Cannot write regular files in a data loader installation session."); |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 915 | } |
| 916 | synchronized (mLock) { |
| 917 | assertCallerIsOwnerOrRootLocked(); |
| 918 | assertPreparedAndNotSealedLocked("assertCanWrite"); |
| 919 | } |
| 920 | if (reverseMode) { |
Alex Buynytskyy | 476138c | 2019-12-20 14:41:47 -0800 | [diff] [blame] | 921 | assertShellOrSystemCalling("Reverse mode"); |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 922 | } |
| 923 | } |
| 924 | |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 925 | @Override |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 926 | public ParcelFileDescriptor openWrite(String name, long offsetBytes, long lengthBytes) { |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 927 | assertCanWrite(false); |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 928 | try { |
Jeff Sharkey | 0451de6 | 2018-02-02 11:27:21 -0700 | [diff] [blame] | 929 | return doWriteInternal(name, offsetBytes, lengthBytes, null); |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 930 | } catch (IOException e) { |
| 931 | throw ExceptionUtils.wrap(e); |
| 932 | } |
| 933 | } |
| 934 | |
Jeff Sharkey | 0451de6 | 2018-02-02 11:27:21 -0700 | [diff] [blame] | 935 | @Override |
| 936 | public void write(String name, long offsetBytes, long lengthBytes, |
| 937 | ParcelFileDescriptor fd) { |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 938 | assertCanWrite(fd != null); |
Jeff Sharkey | 0451de6 | 2018-02-02 11:27:21 -0700 | [diff] [blame] | 939 | try { |
| 940 | doWriteInternal(name, offsetBytes, lengthBytes, fd); |
| 941 | } catch (IOException e) { |
| 942 | throw ExceptionUtils.wrap(e); |
| 943 | } |
| 944 | } |
| 945 | |
| 946 | private ParcelFileDescriptor doWriteInternal(String name, long offsetBytes, long lengthBytes, |
| 947 | ParcelFileDescriptor incomingFd) throws IOException { |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 948 | // Quick sanity check of state, and allocate a pipe for ourselves. We |
| 949 | // then do heavy disk allocation outside the lock, but this open pipe |
| 950 | // will block any attempted install transitions. |
Jeff Sharkey | 02d4e34 | 2017-03-10 21:53:48 -0700 | [diff] [blame] | 951 | final RevocableFileDescriptor fd; |
Jeff Sharkey | 78cc340 | 2014-05-22 10:52:49 -0700 | [diff] [blame] | 952 | final FileBridge bridge; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 953 | synchronized (mLock) { |
Jeff Sharkey | 02d4e34 | 2017-03-10 21:53:48 -0700 | [diff] [blame] | 954 | if (PackageInstaller.ENABLE_REVOCABLE_FD) { |
| 955 | fd = new RevocableFileDescriptor(); |
| 956 | bridge = null; |
| 957 | mFds.add(fd); |
| 958 | } else { |
| 959 | fd = null; |
| 960 | bridge = new FileBridge(); |
| 961 | mBridges.add(bridge); |
| 962 | } |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 963 | } |
| 964 | |
| 965 | try { |
| 966 | // Use installer provided name for now; we always rename later |
| 967 | if (!FileUtils.isValidExtFilename(name)) { |
| 968 | throw new IllegalArgumentException("Invalid name: " + name); |
| 969 | } |
Shunta Sato | 4f26cb5 | 2016-06-28 09:29:19 +0900 | [diff] [blame] | 970 | final File target; |
| 971 | final long identity = Binder.clearCallingIdentity(); |
| 972 | try { |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 973 | target = new File(stageDir, name); |
Shunta Sato | 4f26cb5 | 2016-06-28 09:29:19 +0900 | [diff] [blame] | 974 | } finally { |
| 975 | Binder.restoreCallingIdentity(identity); |
| 976 | } |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 977 | |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 978 | // TODO: this should delegate to DCS so the system process avoids |
| 979 | // holding open FDs into containers. |
Tobias Thierer | 96aac9b3 | 2017-10-17 20:26:20 +0100 | [diff] [blame] | 980 | final FileDescriptor targetFd = Os.open(target.getAbsolutePath(), |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 981 | O_CREAT | O_WRONLY, 0644); |
| 982 | Os.chmod(target.getAbsolutePath(), 0644); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 983 | |
| 984 | // If caller specified a total length, allocate it for them. Free up |
| 985 | // cache space to grow, if needed. |
Jeff Sharkey | d5d5e92 | 2017-02-21 10:51:23 -0700 | [diff] [blame] | 986 | if (stageDir != null && lengthBytes > 0) { |
Jeff Sharkey | 683bcd3 | 2017-03-18 17:54:51 -0600 | [diff] [blame] | 987 | mContext.getSystemService(StorageManager.class).allocateBytes(targetFd, lengthBytes, |
| 988 | PackageHelper.translateAllocateFlags(params.installFlags)); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | if (offsetBytes > 0) { |
Tobias Thierer | 96aac9b3 | 2017-10-17 20:26:20 +0100 | [diff] [blame] | 992 | Os.lseek(targetFd, offsetBytes, OsConstants.SEEK_SET); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 993 | } |
| 994 | |
Jeff Sharkey | 0451de6 | 2018-02-02 11:27:21 -0700 | [diff] [blame] | 995 | if (incomingFd != null) { |
Jeff Sharkey | 0451de6 | 2018-02-02 11:27:21 -0700 | [diff] [blame] | 996 | // In "reverse" mode, we're streaming data ourselves from the |
| 997 | // incoming FD, which means we never have to hand out our |
| 998 | // sensitive internal FD. We still rely on a "bridge" being |
| 999 | // inserted above to hold the session active. |
| 1000 | try { |
| 1001 | final Int64Ref last = new Int64Ref(0); |
Jeff Sharkey | 5aae0c9 | 2018-07-09 16:38:20 -0600 | [diff] [blame] | 1002 | FileUtils.copy(incomingFd.getFileDescriptor(), targetFd, lengthBytes, null, |
| 1003 | Runnable::run, (long progress) -> { |
| 1004 | if (params.sizeBytes > 0) { |
| 1005 | final long delta = progress - last.value; |
| 1006 | last.value = progress; |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 1007 | synchronized (mLock) { |
| 1008 | setClientProgressLocked(mClientProgress |
| 1009 | + (float) delta / (float) params.sizeBytes); |
| 1010 | } |
Jeff Sharkey | 5aae0c9 | 2018-07-09 16:38:20 -0600 | [diff] [blame] | 1011 | } |
| 1012 | }); |
Jeff Sharkey | 0451de6 | 2018-02-02 11:27:21 -0700 | [diff] [blame] | 1013 | } finally { |
| 1014 | IoUtils.closeQuietly(targetFd); |
| 1015 | IoUtils.closeQuietly(incomingFd); |
| 1016 | |
| 1017 | // We're done here, so remove the "bridge" that was holding |
| 1018 | // the session active. |
| 1019 | synchronized (mLock) { |
| 1020 | if (PackageInstaller.ENABLE_REVOCABLE_FD) { |
| 1021 | mFds.remove(fd); |
| 1022 | } else { |
Chuanghua Zhao | b584c6e | 2018-10-17 20:00:04 +0800 | [diff] [blame] | 1023 | bridge.forceClose(); |
Jeff Sharkey | 0451de6 | 2018-02-02 11:27:21 -0700 | [diff] [blame] | 1024 | mBridges.remove(bridge); |
| 1025 | } |
| 1026 | } |
| 1027 | } |
| 1028 | return null; |
| 1029 | } else if (PackageInstaller.ENABLE_REVOCABLE_FD) { |
Jeff Sharkey | 02d4e34 | 2017-03-10 21:53:48 -0700 | [diff] [blame] | 1030 | fd.init(mContext, targetFd); |
| 1031 | return fd.getRevocableFileDescriptor(); |
| 1032 | } else { |
| 1033 | bridge.setTargetFile(targetFd); |
| 1034 | bridge.start(); |
| 1035 | return new ParcelFileDescriptor(bridge.getClientSocket()); |
| 1036 | } |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1037 | |
| 1038 | } catch (ErrnoException e) { |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 1039 | throw e.rethrowAsIOException(); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1040 | } |
| 1041 | } |
| 1042 | |
| 1043 | @Override |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 1044 | public ParcelFileDescriptor openRead(String name) { |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 1045 | if (isDataLoaderInstallation()) { |
| 1046 | throw new IllegalStateException( |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 1047 | "Cannot read regular files in a data loader installation session."); |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 1048 | } |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 1049 | synchronized (mLock) { |
| 1050 | assertCallerIsOwnerOrRootLocked(); |
| 1051 | assertPreparedAndNotCommittedOrDestroyedLocked("openRead"); |
| 1052 | try { |
| 1053 | return openReadInternalLocked(name); |
| 1054 | } catch (IOException e) { |
| 1055 | throw ExceptionUtils.wrap(e); |
| 1056 | } |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 1057 | } |
| 1058 | } |
| 1059 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 1060 | private ParcelFileDescriptor openReadInternalLocked(String name) throws IOException { |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 1061 | try { |
| 1062 | if (!FileUtils.isValidExtFilename(name)) { |
| 1063 | throw new IllegalArgumentException("Invalid name: " + name); |
| 1064 | } |
Alex Buynytskyy | 0054993 | 2019-11-14 08:25:05 -0800 | [diff] [blame] | 1065 | final File target = new File(stageDir, name); |
Tobias Thierer | 96aac9b3 | 2017-10-17 20:26:20 +0100 | [diff] [blame] | 1066 | final FileDescriptor targetFd = Os.open(target.getAbsolutePath(), O_RDONLY, 0); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 1067 | return new ParcelFileDescriptor(targetFd); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 1068 | } catch (ErrnoException e) { |
| 1069 | throw e.rethrowAsIOException(); |
| 1070 | } |
| 1071 | } |
| 1072 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 1073 | /** |
| 1074 | * Check if the caller is the owner of this session. Otherwise throw a |
| 1075 | * {@link SecurityException}. |
| 1076 | */ |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 1077 | @GuardedBy("mLock") |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 1078 | private void assertCallerIsOwnerOrRootLocked() { |
| 1079 | final int callingUid = Binder.getCallingUid(); |
| 1080 | if (callingUid != Process.ROOT_UID && callingUid != mInstallerUid) { |
| 1081 | throw new SecurityException("Session does not belong to uid " + callingUid); |
| 1082 | } |
| 1083 | } |
| 1084 | |
| 1085 | /** |
Mohammad Samiul Islam | a0623e2 | 2020-04-24 09:26:19 +0100 | [diff] [blame] | 1086 | * Check if the caller is the owner of this session. Otherwise throw a |
| 1087 | * {@link SecurityException}. |
| 1088 | */ |
| 1089 | @GuardedBy("mLock") |
| 1090 | private void assertCallerIsOwnerOrRootOrSystemLocked() { |
| 1091 | final int callingUid = Binder.getCallingUid(); |
| 1092 | if (callingUid != Process.ROOT_UID && callingUid != mInstallerUid |
| 1093 | && callingUid != Process.SYSTEM_UID) { |
| 1094 | throw new SecurityException("Session does not belong to uid " + callingUid); |
| 1095 | } |
| 1096 | } |
| 1097 | |
| 1098 | /** |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 1099 | * If anybody is reading or writing data of the session, throw an {@link SecurityException}. |
| 1100 | */ |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 1101 | @GuardedBy("mLock") |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 1102 | private void assertNoWriteFileTransfersOpenLocked() { |
| 1103 | // Verify that all writers are hands-off |
| 1104 | for (RevocableFileDescriptor fd : mFds) { |
| 1105 | if (!fd.isRevoked()) { |
| 1106 | throw new SecurityException("Files still open"); |
| 1107 | } |
| 1108 | } |
| 1109 | for (FileBridge bridge : mBridges) { |
| 1110 | if (!bridge.isClosed()) { |
| 1111 | throw new SecurityException("Files still open"); |
| 1112 | } |
| 1113 | } |
| 1114 | } |
| 1115 | |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 1116 | @Override |
Todd Kennedy | beec8e2 | 2017-08-11 10:15:04 -0700 | [diff] [blame] | 1117 | public void commit(@NonNull IntentSender statusReceiver, boolean forTransfer) { |
Patrick Baumann | 00321b7 | 2019-04-09 15:07:25 -0700 | [diff] [blame] | 1118 | if (hasParentSessionId()) { |
| 1119 | throw new IllegalStateException( |
| 1120 | "Session " + sessionId + " is a child of multi-package session " |
| 1121 | + mParentSessionId + " and may not be committed directly."); |
| 1122 | } |
Alex Buynytskyy | 15a17ba | 2019-11-14 16:28:09 -0800 | [diff] [blame] | 1123 | |
Alex Buynytskyy | 133a628 | 2020-01-28 10:47:43 -0800 | [diff] [blame] | 1124 | if (!markAsSealed(statusReceiver, forTransfer)) { |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 1125 | return; |
| 1126 | } |
| 1127 | if (isMultiPackage()) { |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 1128 | final SparseIntArray remainingSessions = mChildSessionIds.clone(); |
Todd Kennedy | 0af7143 | 2019-03-29 06:35:19 -0700 | [diff] [blame] | 1129 | final IntentSender childIntentSender = |
| 1130 | new ChildStatusIntentReceiver(remainingSessions, statusReceiver) |
| 1131 | .getIntentSender(); |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 1132 | boolean sealFailed = false; |
| 1133 | for (int i = mChildSessionIds.size() - 1; i >= 0; --i) { |
| 1134 | final int childSessionId = mChildSessionIds.keyAt(i); |
| 1135 | // seal all children, regardless if any of them fail; we'll throw/return |
| 1136 | // as appropriate once all children have been processed |
| 1137 | if (!mSessionProvider.getSession(childSessionId) |
Alex Buynytskyy | 133a628 | 2020-01-28 10:47:43 -0800 | [diff] [blame] | 1138 | .markAsSealed(childIntentSender, forTransfer)) { |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 1139 | sealFailed = true; |
| 1140 | } |
| 1141 | } |
| 1142 | if (sealFailed) { |
| 1143 | return; |
| 1144 | } |
| 1145 | } |
| 1146 | |
Alex Buynytskyy | 133a628 | 2020-01-28 10:47:43 -0800 | [diff] [blame] | 1147 | dispatchStreamValidateAndCommit(); |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 1148 | } |
| 1149 | |
Alex Buynytskyy | 133a628 | 2020-01-28 10:47:43 -0800 | [diff] [blame] | 1150 | private void dispatchStreamValidateAndCommit() { |
| 1151 | mHandler.obtainMessage(MSG_STREAM_VALIDATE_AND_COMMIT).sendToTarget(); |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 1152 | } |
| 1153 | |
Alex Buynytskyy | 133a628 | 2020-01-28 10:47:43 -0800 | [diff] [blame] | 1154 | private void handleStreamValidateAndCommit() { |
Patrick Baumann | a7e1a57 | 2020-04-27 10:56:49 -0700 | [diff] [blame] | 1155 | PackageManagerException unrecoverableFailure = null; |
| 1156 | // This will track whether the session and any children were validated and are ready to |
| 1157 | // progress to the next phase of install |
| 1158 | boolean allSessionsReady = false; |
| 1159 | try { |
| 1160 | allSessionsReady = streamValidateAndCommit(); |
| 1161 | } catch (PackageManagerException e) { |
| 1162 | unrecoverableFailure = e; |
| 1163 | } |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 1164 | |
| 1165 | if (isMultiPackage()) { |
Patrick Baumann | a7e1a57 | 2020-04-27 10:56:49 -0700 | [diff] [blame] | 1166 | int childCount = mChildSessionIds.size(); |
| 1167 | |
| 1168 | // This will contain all child sessions that do not encounter an unrecoverable failure |
| 1169 | ArrayList<PackageInstallerSession> nonFailingSessions = new ArrayList<>(childCount); |
| 1170 | |
| 1171 | for (int i = childCount - 1; i >= 0; --i) { |
Todd Kennedy | 0af7143 | 2019-03-29 06:35:19 -0700 | [diff] [blame] | 1172 | final int childSessionId = mChildSessionIds.keyAt(i); |
Alex Buynytskyy | 15a17ba | 2019-11-14 16:28:09 -0800 | [diff] [blame] | 1173 | // commit all children, regardless if any of them fail; we'll throw/return |
| 1174 | // as appropriate once all children have been processed |
Patrick Baumann | a7e1a57 | 2020-04-27 10:56:49 -0700 | [diff] [blame] | 1175 | try { |
| 1176 | PackageInstallerSession session = mSessionProvider.getSession(childSessionId); |
Patrick Baumann | 2e510db | 2020-05-06 12:18:41 -0700 | [diff] [blame] | 1177 | allSessionsReady &= session.streamValidateAndCommit(); |
Patrick Baumann | a7e1a57 | 2020-04-27 10:56:49 -0700 | [diff] [blame] | 1178 | nonFailingSessions.add(session); |
| 1179 | } catch (PackageManagerException e) { |
| 1180 | allSessionsReady = false; |
| 1181 | if (unrecoverableFailure == null) { |
| 1182 | unrecoverableFailure = e; |
| 1183 | } |
| 1184 | } |
| 1185 | } |
Patrick Baumann | 2e510db | 2020-05-06 12:18:41 -0700 | [diff] [blame] | 1186 | // If we encountered any unrecoverable failures, destroy all other sessions including |
| 1187 | // the parent |
Patrick Baumann | a7e1a57 | 2020-04-27 10:56:49 -0700 | [diff] [blame] | 1188 | if (unrecoverableFailure != null) { |
Patrick Baumann | 2e510db | 2020-05-06 12:18:41 -0700 | [diff] [blame] | 1189 | // {@link #streamValidateAndCommit()} calls |
| 1190 | // {@link #onSessionVerificationFailure(PackageManagerException)}, but we don't |
| 1191 | // expect it to ever do so for parent sessions. Call that on this parent to clean |
| 1192 | // it up and notify listeners of the error. |
| 1193 | onSessionVerificationFailure(unrecoverableFailure); |
Patrick Baumann | a7e1a57 | 2020-04-27 10:56:49 -0700 | [diff] [blame] | 1194 | // fail other child sessions that did not already fail |
| 1195 | for (int i = nonFailingSessions.size() - 1; i >= 0; --i) { |
| 1196 | PackageInstallerSession session = nonFailingSessions.get(i); |
| 1197 | session.onSessionVerificationFailure(unrecoverableFailure); |
Todd Kennedy | 0af7143 | 2019-03-29 06:35:19 -0700 | [diff] [blame] | 1198 | } |
| 1199 | } |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 1200 | } |
| 1201 | |
Patrick Baumann | a7e1a57 | 2020-04-27 10:56:49 -0700 | [diff] [blame] | 1202 | if (!allSessionsReady) { |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 1203 | return; |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 1204 | } |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 1205 | |
Alex Buynytskyy | 133a628 | 2020-01-28 10:47:43 -0800 | [diff] [blame] | 1206 | mHandler.obtainMessage(MSG_INSTALL).sendToTarget(); |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 1207 | } |
| 1208 | |
Alex Buynytskyy | 476138c | 2019-12-20 14:41:47 -0800 | [diff] [blame] | 1209 | private final class FileSystemConnector extends |
| 1210 | IPackageInstallerSessionFileSystemConnector.Stub { |
Alex Buynytskyy | c0c5a37 | 2020-01-29 18:59:20 -0800 | [diff] [blame] | 1211 | final Set<String> mAddedFiles = new ArraySet<>(); |
Alex Buynytskyy | 476138c | 2019-12-20 14:41:47 -0800 | [diff] [blame] | 1212 | |
Songchun Fan | 6381d61 | 2020-02-26 17:59:41 -0800 | [diff] [blame] | 1213 | FileSystemConnector(List<InstallationFileParcel> addedFiles) { |
| 1214 | for (InstallationFileParcel file : addedFiles) { |
| 1215 | mAddedFiles.add(file.name); |
Alex Buynytskyy | c0c5a37 | 2020-01-29 18:59:20 -0800 | [diff] [blame] | 1216 | } |
Alex Buynytskyy | 476138c | 2019-12-20 14:41:47 -0800 | [diff] [blame] | 1217 | } |
| 1218 | |
Alex Buynytskyy | ea14d19 | 2019-12-13 15:42:18 -0800 | [diff] [blame] | 1219 | @Override |
| 1220 | public void writeData(String name, long offsetBytes, long lengthBytes, |
| 1221 | ParcelFileDescriptor incomingFd) { |
| 1222 | if (incomingFd == null) { |
| 1223 | throw new IllegalArgumentException("incomingFd can't be null"); |
| 1224 | } |
Alex Buynytskyy | 476138c | 2019-12-20 14:41:47 -0800 | [diff] [blame] | 1225 | if (!mAddedFiles.contains(name)) { |
| 1226 | throw new SecurityException("File name is not in the list of added files."); |
| 1227 | } |
Alex Buynytskyy | ea14d19 | 2019-12-13 15:42:18 -0800 | [diff] [blame] | 1228 | try { |
| 1229 | doWriteInternal(name, offsetBytes, lengthBytes, incomingFd); |
| 1230 | } catch (IOException e) { |
| 1231 | throw ExceptionUtils.wrap(e); |
| 1232 | } |
| 1233 | } |
| 1234 | } |
| 1235 | |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 1236 | private class ChildStatusIntentReceiver { |
| 1237 | private final SparseIntArray mChildSessionsRemaining; |
| 1238 | private final IntentSender mStatusReceiver; |
| 1239 | private final IIntentSender.Stub mLocalSender = new IIntentSender.Stub() { |
| 1240 | @Override |
| 1241 | public void send(int code, Intent intent, String resolvedType, IBinder whitelistToken, |
| 1242 | IIntentReceiver finishedReceiver, String requiredPermission, Bundle options) { |
| 1243 | statusUpdate(intent); |
| 1244 | } |
| 1245 | }; |
| 1246 | |
| 1247 | private ChildStatusIntentReceiver(SparseIntArray remainingSessions, |
| 1248 | IntentSender statusReceiver) { |
| 1249 | this.mChildSessionsRemaining = remainingSessions; |
| 1250 | this.mStatusReceiver = statusReceiver; |
| 1251 | } |
| 1252 | |
| 1253 | public IntentSender getIntentSender() { |
| 1254 | return new IntentSender((IIntentSender) mLocalSender); |
| 1255 | } |
| 1256 | |
| 1257 | public void statusUpdate(Intent intent) { |
| 1258 | mHandler.post(() -> { |
| 1259 | if (mChildSessionsRemaining.size() == 0) { |
Patrick Baumann | 2e510db | 2020-05-06 12:18:41 -0700 | [diff] [blame] | 1260 | // no children to deal with, ignore. |
| 1261 | return; |
| 1262 | } |
| 1263 | final boolean destroyed; |
| 1264 | synchronized (mLock) { |
| 1265 | destroyed = mDestroyed; |
| 1266 | } |
| 1267 | if (destroyed) { |
| 1268 | // the parent has already been terminated, ignore. |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 1269 | return; |
| 1270 | } |
| 1271 | final int sessionId = intent.getIntExtra( |
| 1272 | PackageInstaller.EXTRA_SESSION_ID, 0); |
| 1273 | final int status = intent.getIntExtra(PackageInstaller.EXTRA_STATUS, |
| 1274 | PackageInstaller.STATUS_FAILURE); |
| 1275 | final int sessionIndex = mChildSessionsRemaining.indexOfKey(sessionId); |
| 1276 | if (PackageInstaller.STATUS_SUCCESS == status) { |
| 1277 | mChildSessionsRemaining.removeAt(sessionIndex); |
| 1278 | if (mChildSessionsRemaining.size() == 0) { |
| 1279 | try { |
| 1280 | intent.putExtra(PackageInstaller.EXTRA_SESSION_ID, |
| 1281 | PackageInstallerSession.this.sessionId); |
| 1282 | mStatusReceiver.sendIntent(mContext, 0, intent, null, null); |
| 1283 | } catch (IntentSender.SendIntentException ignore) { |
| 1284 | } |
| 1285 | } |
| 1286 | } else if (PackageInstaller.STATUS_PENDING_USER_ACTION == status) { |
| 1287 | try { |
| 1288 | mStatusReceiver.sendIntent(mContext, 0, intent, null, null); |
| 1289 | } catch (IntentSender.SendIntentException ignore) { |
| 1290 | } |
Patrick Baumann | a7e1a57 | 2020-04-27 10:56:49 -0700 | [diff] [blame] | 1291 | } else { // failure, let's forward and clean up this session. |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 1292 | intent.putExtra(PackageInstaller.EXTRA_SESSION_ID, |
| 1293 | PackageInstallerSession.this.sessionId); |
| 1294 | mChildSessionsRemaining.clear(); // we're done. Don't send any more. |
Patrick Baumann | 2e510db | 2020-05-06 12:18:41 -0700 | [diff] [blame] | 1295 | try { |
| 1296 | mStatusReceiver.sendIntent(mContext, 0, intent, null, null); |
| 1297 | } catch (IntentSender.SendIntentException ignore) { |
| 1298 | } |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 1299 | } |
| 1300 | }); |
| 1301 | } |
| 1302 | } |
| 1303 | |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 1304 | /** {@hide} */ |
| 1305 | private class StreamingException extends Exception { |
| 1306 | StreamingException(Throwable cause) { |
| 1307 | super(cause); |
| 1308 | } |
| 1309 | } |
| 1310 | |
Todd Kennedy | c961a87 | 2020-01-24 14:08:14 -0800 | [diff] [blame] | 1311 | /** |
| 1312 | * Returns whether or not a package can be installed while Secure FRP is enabled. |
| 1313 | * <p> |
| 1314 | * Only callers with the INSTALL_PACKAGES permission are allowed to install. However, |
| 1315 | * prevent the package installer from installing anything because, while it has the |
| 1316 | * permission, it will allows packages to be installed from anywhere. |
| 1317 | */ |
| 1318 | private static boolean isSecureFrpInstallAllowed(Context context, int callingUid) { |
| 1319 | final PackageManagerInternal pmi = LocalServices.getService(PackageManagerInternal.class); |
| 1320 | final String[] systemInstaller = pmi.getKnownPackageNames( |
| 1321 | PackageManagerInternal.PACKAGE_INSTALLER, UserHandle.USER_SYSTEM); |
| 1322 | final AndroidPackage callingInstaller = pmi.getPackage(callingUid); |
| 1323 | if (callingInstaller != null |
| 1324 | && ArrayUtils.contains(systemInstaller, callingInstaller.getPackageName())) { |
| 1325 | // don't allow the system package installer to install while under secure FRP |
| 1326 | return false; |
| 1327 | } |
| 1328 | |
| 1329 | // require caller to hold the INSTALL_PACKAGES permission |
| 1330 | return context.checkCallingOrSelfPermission(Manifest.permission.INSTALL_PACKAGES) |
| 1331 | == PackageManager.PERMISSION_GRANTED; |
| 1332 | } |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 1333 | |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 1334 | /** |
Alex Buynytskyy | ae3eabe | 2020-02-03 15:00:21 -0800 | [diff] [blame] | 1335 | * Checks if the package can be installed on IncFs. |
| 1336 | */ |
| 1337 | private static boolean isIncrementalInstallationAllowed(String packageName) { |
| 1338 | final PackageManagerInternal pmi = LocalServices.getService(PackageManagerInternal.class); |
Winson | e075629 | 2020-01-31 12:21:54 -0800 | [diff] [blame] | 1339 | final PackageSetting existingPkgSetting = pmi.getPackageSetting(packageName); |
| 1340 | if (existingPkgSetting == null || existingPkgSetting.pkg == null) { |
Alex Buynytskyy | ae3eabe | 2020-02-03 15:00:21 -0800 | [diff] [blame] | 1341 | return true; |
| 1342 | } |
| 1343 | |
Winson | e075629 | 2020-01-31 12:21:54 -0800 | [diff] [blame] | 1344 | return !existingPkgSetting.pkg.isSystem() |
| 1345 | && !existingPkgSetting.getPkgState().isUpdatedSystemApp(); |
Alex Buynytskyy | ae3eabe | 2020-02-03 15:00:21 -0800 | [diff] [blame] | 1346 | } |
| 1347 | |
| 1348 | /** |
Alex Buynytskyy | 133a628 | 2020-01-28 10:47:43 -0800 | [diff] [blame] | 1349 | * If this was not already called, the session will be sealed. |
| 1350 | * |
| 1351 | * This method may be called multiple times to update the status receiver validate caller |
| 1352 | * permissions. |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 1353 | */ |
Alex Buynytskyy | 133a628 | 2020-01-28 10:47:43 -0800 | [diff] [blame] | 1354 | private boolean markAsSealed(@NonNull IntentSender statusReceiver, boolean forTransfer) { |
| 1355 | Objects.requireNonNull(statusReceiver); |
| 1356 | |
JW Wang | 8a207c4 | 2020-06-22 16:22:08 +0800 | [diff] [blame] | 1357 | List<PackageInstallerSession> childSessions = getChildSessionsNotLocked(); |
Alex Buynytskyy | 133a628 | 2020-01-28 10:47:43 -0800 | [diff] [blame] | 1358 | |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 1359 | synchronized (mLock) { |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 1360 | assertCallerIsOwnerOrRootLocked(); |
| 1361 | assertPreparedAndNotDestroyedLocked("commit"); |
Alex Buynytskyy | 15a17ba | 2019-11-14 16:28:09 -0800 | [diff] [blame] | 1362 | assertNoWriteFileTransfersOpenLocked(); |
Todd Kennedy | beec8e2 | 2017-08-11 10:15:04 -0700 | [diff] [blame] | 1363 | |
Todd Kennedy | c961a87 | 2020-01-24 14:08:14 -0800 | [diff] [blame] | 1364 | final boolean isSecureFrpEnabled = |
| 1365 | (Secure.getInt(mContext.getContentResolver(), Secure.SECURE_FRP_MODE, 0) == 1); |
| 1366 | if (isSecureFrpEnabled |
| 1367 | && !isSecureFrpInstallAllowed(mContext, Binder.getCallingUid())) { |
| 1368 | throw new SecurityException("Can't install packages while in secure FRP"); |
Todd Kennedy | 7e2b8e6 | 2019-11-25 15:36:12 -0800 | [diff] [blame] | 1369 | } |
Todd Kennedy | c961a87 | 2020-01-24 14:08:14 -0800 | [diff] [blame] | 1370 | |
Todd Kennedy | 7e2b8e6 | 2019-11-25 15:36:12 -0800 | [diff] [blame] | 1371 | if (forTransfer) { |
Todd Kennedy | c961a87 | 2020-01-24 14:08:14 -0800 | [diff] [blame] | 1372 | mContext.enforceCallingOrSelfPermission(Manifest.permission.INSTALL_PACKAGES, null); |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 1373 | if (mInstallerUid == mOriginalInstallerUid) { |
| 1374 | throw new IllegalArgumentException("Session has not been transferred"); |
| 1375 | } |
| 1376 | } else { |
| 1377 | if (mInstallerUid != mOriginalInstallerUid) { |
| 1378 | throw new IllegalArgumentException("Session has been transferred"); |
| 1379 | } |
| 1380 | } |
Alex Buynytskyy | 15a17ba | 2019-11-14 16:28:09 -0800 | [diff] [blame] | 1381 | |
Alex Buynytskyy | 15a17ba | 2019-11-14 16:28:09 -0800 | [diff] [blame] | 1382 | mRemoteStatusReceiver = statusReceiver; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 1383 | |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 1384 | // After updating the observer, we can skip re-sealing. |
| 1385 | if (mSealed) { |
| 1386 | return true; |
| 1387 | } |
| 1388 | |
| 1389 | try { |
| 1390 | sealLocked(childSessions); |
| 1391 | } catch (PackageManagerException e) { |
| 1392 | return false; |
| 1393 | } |
| 1394 | } |
| 1395 | |
| 1396 | // Persist the fact that we've sealed ourselves to prevent |
| 1397 | // mutations of any hard links we create. We do this without holding |
| 1398 | // the session lock, since otherwise it's a lock inversion. |
| 1399 | mCallback.onSessionSealedBlocking(this); |
| 1400 | |
| 1401 | return true; |
| 1402 | } |
| 1403 | |
Patrick Baumann | a7e1a57 | 2020-04-27 10:56:49 -0700 | [diff] [blame] | 1404 | /** |
| 1405 | * Returns true if the session is successfully validated and committed. Returns false if the |
| 1406 | * dataloader could not be prepared. This can be called multiple times so long as no |
| 1407 | * exception is thrown. |
| 1408 | * @throws PackageManagerException on an unrecoverable error. |
| 1409 | */ |
| 1410 | private boolean streamValidateAndCommit() throws PackageManagerException { |
Patrick Baumann | 2e510db | 2020-05-06 12:18:41 -0700 | [diff] [blame] | 1411 | // TODO(patb): since the work done here for a parent session in a multi-package install is |
| 1412 | // mostly superficial, consider splitting this method for the parent and |
| 1413 | // single / child sessions. |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 1414 | synchronized (mLock) { |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 1415 | if (mCommitted) { |
| 1416 | return true; |
| 1417 | } |
| 1418 | |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 1419 | if (!streamAndValidateLocked()) { |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 1420 | return false; |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 1421 | } |
Jeff Sharkey | cbf4791 | 2014-09-12 09:55:32 -0700 | [diff] [blame] | 1422 | |
| 1423 | // Client staging is fully done at this point |
| 1424 | mClientProgress = 1f; |
| 1425 | computeProgressLocked(true); |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 1426 | |
| 1427 | // This ongoing commit should keep session active, even though client |
| 1428 | // will probably close their end. |
| 1429 | mActiveCount.incrementAndGet(); |
| 1430 | |
| 1431 | mCommitted = true; |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 1432 | } |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 1433 | return true; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1434 | } |
| 1435 | |
shafik | 43f1af9 | 2019-03-14 15:14:00 +0000 | [diff] [blame] | 1436 | /** Return a list of child sessions or null if the session is not multipackage |
| 1437 | * |
| 1438 | * <p> This method is handy to prevent potential deadlocks (b/123391593) |
| 1439 | */ |
JW Wang | 8a207c4 | 2020-06-22 16:22:08 +0800 | [diff] [blame] | 1440 | private @Nullable List<PackageInstallerSession> getChildSessionsNotLocked() { |
| 1441 | if (Thread.holdsLock(mLock)) { |
| 1442 | Slog.wtf(TAG, "Calling thread " + Thread.currentThread().getName() |
| 1443 | + " is holding mLock", new Throwable()); |
| 1444 | } |
shafik | 43f1af9 | 2019-03-14 15:14:00 +0000 | [diff] [blame] | 1445 | List<PackageInstallerSession> childSessions = null; |
| 1446 | if (isMultiPackage()) { |
| 1447 | final int[] childSessionIds = getChildSessionIds(); |
| 1448 | childSessions = new ArrayList<>(childSessionIds.length); |
| 1449 | for (int childSessionId : childSessionIds) { |
| 1450 | childSessions.add(mSessionProvider.getSession(childSessionId)); |
| 1451 | } |
| 1452 | } |
| 1453 | return childSessions; |
| 1454 | } |
| 1455 | |
| 1456 | /** |
| 1457 | * Assert multipackage install has consistent sessions. |
| 1458 | * |
| 1459 | * @throws PackageManagerException if child sessions don't match parent session |
| 1460 | * in respect to staged and enable rollback parameters. |
| 1461 | */ |
| 1462 | @GuardedBy("mLock") |
| 1463 | private void assertMultiPackageConsistencyLocked( |
| 1464 | @NonNull List<PackageInstallerSession> childSessions) throws PackageManagerException { |
| 1465 | for (PackageInstallerSession childSession : childSessions) { |
| 1466 | // It might be that the parent session is loaded before all of it's child sessions are, |
| 1467 | // e.g. when reading sessions from XML. Those sessions will be null here, and their |
| 1468 | // conformance with the multipackage params will be checked when they're loaded. |
| 1469 | if (childSession == null) { |
| 1470 | continue; |
| 1471 | } |
| 1472 | assertConsistencyWithLocked(childSession); |
| 1473 | } |
| 1474 | } |
| 1475 | |
| 1476 | /** |
| 1477 | * Assert consistency with the given session. |
| 1478 | * |
| 1479 | * @throws PackageManagerException if other sessions doesn't match this session |
| 1480 | * in respect to staged and enable rollback parameters. |
| 1481 | */ |
| 1482 | @GuardedBy("mLock") |
| 1483 | private void assertConsistencyWithLocked(PackageInstallerSession other) |
| 1484 | throws PackageManagerException { |
| 1485 | // Session groups must be consistent wrt to isStaged parameter. Non-staging session |
| 1486 | // cannot be grouped with staging sessions. |
| 1487 | if (this.params.isStaged != other.params.isStaged) { |
| 1488 | throw new PackageManagerException( |
| 1489 | PackageManager.INSTALL_FAILED_MULTIPACKAGE_INCONSISTENCY, |
| 1490 | "Multipackage Inconsistency: session " + other.sessionId |
| 1491 | + " and session " + sessionId |
| 1492 | + " have inconsistent staged settings"); |
| 1493 | } |
| 1494 | if (this.params.getEnableRollback() != other.params.getEnableRollback()) { |
| 1495 | throw new PackageManagerException( |
| 1496 | PackageManager.INSTALL_FAILED_MULTIPACKAGE_INCONSISTENCY, |
| 1497 | "Multipackage Inconsistency: session " + other.sessionId |
| 1498 | + " and session " + sessionId |
| 1499 | + " have inconsistent rollback settings"); |
| 1500 | } |
| 1501 | } |
| 1502 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 1503 | /** |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 1504 | * Seal the session to prevent further modification. |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 1505 | * |
| 1506 | * <p>The session will be sealed after calling this method even if it failed. |
| 1507 | * |
Philip P. Moltmann | 37dd1ba | 2017-09-08 09:46:22 -0700 | [diff] [blame] | 1508 | * @throws PackageManagerException if the session was sealed but something went wrong. If the |
| 1509 | * session was sealed this is the only possible exception. |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 1510 | */ |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 1511 | @GuardedBy("mLock") |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 1512 | private void sealLocked(List<PackageInstallerSession> childSessions) |
Alex Buynytskyy | 0054993 | 2019-11-14 08:25:05 -0800 | [diff] [blame] | 1513 | throws PackageManagerException { |
| 1514 | try { |
| 1515 | assertNoWriteFileTransfersOpenLocked(); |
| 1516 | assertPreparedAndNotDestroyedLocked("sealing of session"); |
Philip P. Moltmann | 37dd1ba | 2017-09-08 09:46:22 -0700 | [diff] [blame] | 1517 | |
Alex Buynytskyy | 0054993 | 2019-11-14 08:25:05 -0800 | [diff] [blame] | 1518 | mSealed = true; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 1519 | |
Alex Buynytskyy | 0054993 | 2019-11-14 08:25:05 -0800 | [diff] [blame] | 1520 | if (childSessions != null) { |
| 1521 | assertMultiPackageConsistencyLocked(childSessions); |
Nikita Ioffe | 8e5b703c | 2019-03-14 18:40:17 +0000 | [diff] [blame] | 1522 | } |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 1523 | } catch (PackageManagerException e) { |
| 1524 | throw onSessionVerificationFailure(e); |
| 1525 | } catch (Throwable e) { |
| 1526 | // Convert all exceptions into package manager exceptions as only those are handled |
| 1527 | // in the code above. |
| 1528 | throw onSessionVerificationFailure(new PackageManagerException(e)); |
| 1529 | } |
| 1530 | } |
Nikita Ioffe | 8e5b703c | 2019-03-14 18:40:17 +0000 | [diff] [blame] | 1531 | |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 1532 | /** |
| 1533 | * Prepare DataLoader and stream content for DataLoader sessions. |
| 1534 | * Validate the contents of all session. |
| 1535 | * |
Patrick Baumann | a7e1a57 | 2020-04-27 10:56:49 -0700 | [diff] [blame] | 1536 | * @return false if the data loader could not be prepared. |
| 1537 | * @throws PackageManagerException when an unrecoverable exception is encountered |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 1538 | */ |
| 1539 | @GuardedBy("mLock") |
Patrick Baumann | a7e1a57 | 2020-04-27 10:56:49 -0700 | [diff] [blame] | 1540 | private boolean streamAndValidateLocked() throws PackageManagerException { |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 1541 | try { |
Alex Buynytskyy | 15a17ba | 2019-11-14 16:28:09 -0800 | [diff] [blame] | 1542 | // Read transfers from the original owner stay open, but as the session's data cannot |
| 1543 | // be modified anymore, there is no leak of information. For staged sessions, further |
| 1544 | // validation is performed by the staging manager. |
Alex Buynytskyy | 0054993 | 2019-11-14 08:25:05 -0800 | [diff] [blame] | 1545 | if (!params.isMultiPackage) { |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 1546 | if (!prepareDataLoaderLocked()) { |
| 1547 | return false; |
| 1548 | } |
| 1549 | |
Alex Buynytskyy | 769f815 | 2020-01-23 16:58:45 +0000 | [diff] [blame] | 1550 | if (isApexInstallation()) { |
Alex Buynytskyy | 15a17ba | 2019-11-14 16:28:09 -0800 | [diff] [blame] | 1551 | validateApexInstallLocked(); |
| 1552 | } else { |
Alex Buynytskyy | ae3eabe | 2020-02-03 15:00:21 -0800 | [diff] [blame] | 1553 | validateApkInstallLocked(); |
Alex Buynytskyy | 0054993 | 2019-11-14 08:25:05 -0800 | [diff] [blame] | 1554 | } |
| 1555 | } |
Mohammad Samiul Islam | da00497 | 2019-10-09 11:29:26 +0100 | [diff] [blame] | 1556 | |
| 1557 | if (params.isStaged) { |
| 1558 | mStagingManager.checkNonOverlappingWithStagedSessions(this); |
| 1559 | } |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 1560 | return true; |
Alex Buynytskyy | 0054993 | 2019-11-14 08:25:05 -0800 | [diff] [blame] | 1561 | } catch (PackageManagerException e) { |
Patrick Baumann | a7e1a57 | 2020-04-27 10:56:49 -0700 | [diff] [blame] | 1562 | throw onSessionVerificationFailure(e); |
Alex Buynytskyy | 15a17ba | 2019-11-14 16:28:09 -0800 | [diff] [blame] | 1563 | } catch (Throwable e) { |
| 1564 | // Convert all exceptions into package manager exceptions as only those are handled |
| 1565 | // in the code above. |
Patrick Baumann | a7e1a57 | 2020-04-27 10:56:49 -0700 | [diff] [blame] | 1566 | throw onSessionVerificationFailure(new PackageManagerException(e)); |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 1567 | } |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 1568 | } |
| 1569 | |
Alex Buynytskyy | 15a17ba | 2019-11-14 16:28:09 -0800 | [diff] [blame] | 1570 | private PackageManagerException onSessionVerificationFailure(PackageManagerException e) { |
Alex Buynytskyy | 64067b2 | 2020-04-25 15:56:52 -0700 | [diff] [blame] | 1571 | onSessionVerificationFailure(e.error, ExceptionUtils.getCompleteMessage(e)); |
| 1572 | return e; |
| 1573 | } |
| 1574 | |
| 1575 | private void onSessionVerificationFailure(int error, String detailMessage) { |
Alex Buynytskyy | 15a17ba | 2019-11-14 16:28:09 -0800 | [diff] [blame] | 1576 | // Session is sealed but could not be verified, we need to destroy it. |
| 1577 | destroyInternal(); |
| 1578 | // Dispatch message to remove session from PackageInstallerService. |
Alex Buynytskyy | 64067b2 | 2020-04-25 15:56:52 -0700 | [diff] [blame] | 1579 | dispatchSessionFinished(error, detailMessage, null); |
Alex Buynytskyy | 15a17ba | 2019-11-14 16:28:09 -0800 | [diff] [blame] | 1580 | } |
| 1581 | |
Alex Buynytskyy | 8ef61ae | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 1582 | private void onStorageUnhealthy() { |
Alex Buynytskyy | 03a2c38 | 2020-04-24 10:04:50 -0700 | [diff] [blame] | 1583 | if (TextUtils.isEmpty(mPackageName)) { |
| 1584 | // The package has not been installed. |
| 1585 | return; |
| 1586 | } |
Alex Buynytskyy | 2cf1d18 | 2020-03-17 09:33:45 -0700 | [diff] [blame] | 1587 | final PackageManagerService packageManagerService = mPm; |
| 1588 | final String packageName = mPackageName; |
| 1589 | mHandler.post(() -> { |
| 1590 | if (packageManagerService.deletePackageX(packageName, |
| 1591 | PackageManager.VERSION_CODE_HIGHEST, UserHandle.USER_SYSTEM, |
| 1592 | PackageManager.DELETE_ALL_USERS) != PackageManager.DELETE_SUCCEEDED) { |
| 1593 | Slog.e(TAG, "Failed to uninstall package with failed dataloader: " + packageName); |
| 1594 | } |
| 1595 | }); |
| 1596 | } |
| 1597 | |
shafik | 43f1af9 | 2019-03-14 15:14:00 +0000 | [diff] [blame] | 1598 | /** |
Alex Buynytskyy | 769f815 | 2020-01-23 16:58:45 +0000 | [diff] [blame] | 1599 | * If session should be sealed, then it's sealed to prevent further modification. |
| 1600 | * If the session can't be sealed then it's destroyed. |
Mohammad Samiul Islam | 6367296 | 2020-01-22 12:02:38 +0000 | [diff] [blame] | 1601 | * |
Alex Buynytskyy | 769f815 | 2020-01-23 16:58:45 +0000 | [diff] [blame] | 1602 | * Additionally for staged APEX sessions read+validate the package and populate req'd fields. |
shafik | 43f1af9 | 2019-03-14 15:14:00 +0000 | [diff] [blame] | 1603 | * |
| 1604 | * <p> This is meant to be called after all of the sessions are loaded and added to |
| 1605 | * PackageInstallerService |
| 1606 | */ |
Alex Buynytskyy | 769f815 | 2020-01-23 16:58:45 +0000 | [diff] [blame] | 1607 | void onAfterSessionRead() { |
shafik | 43f1af9 | 2019-03-14 15:14:00 +0000 | [diff] [blame] | 1608 | synchronized (mLock) { |
Dario Freni | f2449f7 | 2019-05-02 15:46:03 +0100 | [diff] [blame] | 1609 | if (!mShouldBeSealed || isStagedAndInTerminalState()) { |
shafik | 43f1af9 | 2019-03-14 15:14:00 +0000 | [diff] [blame] | 1610 | return; |
| 1611 | } |
| 1612 | } |
JW Wang | 8a207c4 | 2020-06-22 16:22:08 +0800 | [diff] [blame] | 1613 | List<PackageInstallerSession> childSessions = getChildSessionsNotLocked(); |
shafik | 43f1af9 | 2019-03-14 15:14:00 +0000 | [diff] [blame] | 1614 | synchronized (mLock) { |
| 1615 | try { |
Alex Buynytskyy | 769f815 | 2020-01-23 16:58:45 +0000 | [diff] [blame] | 1616 | sealLocked(childSessions); |
| 1617 | |
| 1618 | if (isApexInstallation()) { |
| 1619 | // APEX installations rely on certain fields to be populated after reboot. |
| 1620 | // E.g. mPackageName. |
| 1621 | validateApexInstallLocked(); |
| 1622 | } |
shafik | 43f1af9 | 2019-03-14 15:14:00 +0000 | [diff] [blame] | 1623 | } catch (PackageManagerException e) { |
| 1624 | Slog.e(TAG, "Package not valid", e); |
shafik | 43f1af9 | 2019-03-14 15:14:00 +0000 | [diff] [blame] | 1625 | } |
| 1626 | } |
| 1627 | } |
| 1628 | |
Gavin Corkery | d831121 | 2019-02-22 17:52:30 +0000 | [diff] [blame] | 1629 | /** Update the timestamp of when the staged session last changed state */ |
| 1630 | public void markUpdated() { |
| 1631 | synchronized (mLock) { |
| 1632 | this.updatedMillis = System.currentTimeMillis(); |
| 1633 | } |
| 1634 | } |
| 1635 | |
Alex Buynytskyy | 354d669 | 2020-01-17 15:23:38 +0000 | [diff] [blame] | 1636 | @Override |
| 1637 | public void transfer(String packageName) { |
| 1638 | Objects.requireNonNull(packageName); |
| 1639 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 1640 | ApplicationInfo newOwnerAppInfo = mPm.getApplicationInfo(packageName, 0, userId); |
| 1641 | if (newOwnerAppInfo == null) { |
Alex Buynytskyy | 354d669 | 2020-01-17 15:23:38 +0000 | [diff] [blame] | 1642 | throw new ParcelableException(new PackageManager.NameNotFoundException(packageName)); |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 1643 | } |
| 1644 | |
| 1645 | if (PackageManager.PERMISSION_GRANTED != mPm.checkUidPermission( |
| 1646 | Manifest.permission.INSTALL_PACKAGES, newOwnerAppInfo.uid)) { |
| 1647 | throw new SecurityException("Destination package " + packageName + " does not have " |
| 1648 | + "the " + Manifest.permission.INSTALL_PACKAGES + " permission"); |
| 1649 | } |
| 1650 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 1651 | // Only install flags that can be verified by the app the session is transferred to are |
| 1652 | // allowed. The parameters can be read via PackageInstaller.SessionInfo. |
| 1653 | if (!params.areHiddenOptionsSet()) { |
| 1654 | throw new SecurityException("Can only transfer sessions that use public options"); |
| 1655 | } |
| 1656 | |
JW Wang | 8a207c4 | 2020-06-22 16:22:08 +0800 | [diff] [blame] | 1657 | List<PackageInstallerSession> childSessions = getChildSessionsNotLocked(); |
shafik | 43f1af9 | 2019-03-14 15:14:00 +0000 | [diff] [blame] | 1658 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 1659 | synchronized (mLock) { |
| 1660 | assertCallerIsOwnerOrRootLocked(); |
| 1661 | assertPreparedAndNotSealedLocked("transfer"); |
| 1662 | |
Alex Buynytskyy | 354d669 | 2020-01-17 15:23:38 +0000 | [diff] [blame] | 1663 | try { |
| 1664 | sealLocked(childSessions); |
| 1665 | } catch (PackageManagerException e) { |
| 1666 | throw new IllegalArgumentException("Package is not valid", e); |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 1667 | } |
Alex Buynytskyy | 354d669 | 2020-01-17 15:23:38 +0000 | [diff] [blame] | 1668 | |
| 1669 | if (!mPackageName.equals(mInstallSource.installerPackageName)) { |
| 1670 | throw new SecurityException("Can only transfer sessions that update the original " |
| 1671 | + "installer"); |
| 1672 | } |
| 1673 | |
| 1674 | mInstallerUid = newOwnerAppInfo.uid; |
| 1675 | mInstallSource = InstallSource.create(packageName, null, packageName); |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 1676 | } |
| 1677 | |
| 1678 | // Persist the fact that we've sealed ourselves to prevent |
| 1679 | // mutations of any hard links we create. We do this without holding |
| 1680 | // the session lock, since otherwise it's a lock inversion. |
| 1681 | mCallback.onSessionSealedBlocking(this); |
| 1682 | } |
| 1683 | |
Alex Buynytskyy | 133a628 | 2020-01-28 10:47:43 -0800 | [diff] [blame] | 1684 | private void handleInstall() { |
Rubin Xu | 8b17ad0 | 2019-03-07 17:42:37 +0000 | [diff] [blame] | 1685 | if (isInstallerDeviceOwnerOrAffiliatedProfileOwnerLocked()) { |
| 1686 | DevicePolicyEventLogger |
| 1687 | .createEvent(DevicePolicyEnums.INSTALL_PACKAGE) |
Alan Stokes | 819fea2 | 2019-10-16 16:54:09 +0100 | [diff] [blame] | 1688 | .setAdmin(mInstallSource.installerPackageName) |
Rubin Xu | 8b17ad0 | 2019-03-07 17:42:37 +0000 | [diff] [blame] | 1689 | .write(); |
| 1690 | } |
Dario Freni | a8f4b13 | 2018-12-30 00:36:49 +0000 | [diff] [blame] | 1691 | if (params.isStaged) { |
Nikita Ioffe | 8e5b703c | 2019-03-14 18:40:17 +0000 | [diff] [blame] | 1692 | mStagingManager.commitSession(this); |
Dario Freni | a8f4b13 | 2018-12-30 00:36:49 +0000 | [diff] [blame] | 1693 | destroyInternal(); |
| 1694 | dispatchSessionFinished(PackageManager.INSTALL_SUCCEEDED, "Session staged", null); |
| 1695 | return; |
| 1696 | } |
Richard Uhler | 8c09089 | 2019-02-12 11:59:51 +0000 | [diff] [blame] | 1697 | |
Alex Buynytskyy | 769f815 | 2020-01-23 16:58:45 +0000 | [diff] [blame] | 1698 | if (isApexInstallation()) { |
Richard Uhler | 8c09089 | 2019-02-12 11:59:51 +0000 | [diff] [blame] | 1699 | destroyInternal(); |
| 1700 | dispatchSessionFinished(PackageManager.INSTALL_FAILED_INTERNAL_ERROR, |
| 1701 | "APEX packages can only be installed using staged sessions.", null); |
| 1702 | return; |
Dario Freni | 3ad7361 | 2019-02-06 15:03:05 +0000 | [diff] [blame] | 1703 | } |
Richard Uhler | 8c09089 | 2019-02-12 11:59:51 +0000 | [diff] [blame] | 1704 | |
| 1705 | // For a multiPackage session, read the child sessions |
| 1706 | // outside of the lock, because reading the child |
| 1707 | // sessions with the lock held could lead to deadlock |
| 1708 | // (b/123391593). |
JW Wang | 8a207c4 | 2020-06-22 16:22:08 +0800 | [diff] [blame] | 1709 | List<PackageInstallerSession> childSessions = getChildSessionsNotLocked(); |
Richard Uhler | 8c09089 | 2019-02-12 11:59:51 +0000 | [diff] [blame] | 1710 | |
| 1711 | try { |
| 1712 | synchronized (mLock) { |
Alex Buynytskyy | 133a628 | 2020-01-28 10:47:43 -0800 | [diff] [blame] | 1713 | installNonStagedLocked(childSessions); |
Richard Uhler | 8c09089 | 2019-02-12 11:59:51 +0000 | [diff] [blame] | 1714 | } |
| 1715 | } catch (PackageManagerException e) { |
| 1716 | final String completeMsg = ExceptionUtils.getCompleteMessage(e); |
| 1717 | Slog.e(TAG, "Commit of session " + sessionId + " failed: " + completeMsg); |
| 1718 | destroyInternal(); |
| 1719 | dispatchSessionFinished(e.error, completeMsg, null); |
| 1720 | } |
| 1721 | } |
| 1722 | |
| 1723 | @GuardedBy("mLock") |
Alex Buynytskyy | 133a628 | 2020-01-28 10:47:43 -0800 | [diff] [blame] | 1724 | private void installNonStagedLocked(List<PackageInstallerSession> childSessions) |
Richard Uhler | 8c09089 | 2019-02-12 11:59:51 +0000 | [diff] [blame] | 1725 | throws PackageManagerException { |
Alex Buynytskyy | 133a628 | 2020-01-28 10:47:43 -0800 | [diff] [blame] | 1726 | final PackageManagerService.ActiveInstallSession installingSession = |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 1727 | makeSessionActiveLocked(); |
Alex Buynytskyy | 133a628 | 2020-01-28 10:47:43 -0800 | [diff] [blame] | 1728 | if (installingSession == null) { |
Philip P. Moltmann | d9d343c | 2018-07-03 15:17:11 -0700 | [diff] [blame] | 1729 | return; |
| 1730 | } |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 1731 | if (isMultiPackage()) { |
Alex Buynytskyy | 133a628 | 2020-01-28 10:47:43 -0800 | [diff] [blame] | 1732 | List<PackageManagerService.ActiveInstallSession> installingChildSessions = |
Richard Uhler | 8c09089 | 2019-02-12 11:59:51 +0000 | [diff] [blame] | 1733 | new ArrayList<>(childSessions.size()); |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 1734 | boolean success = true; |
| 1735 | PackageManagerException failure = null; |
Richard Uhler | 8c09089 | 2019-02-12 11:59:51 +0000 | [diff] [blame] | 1736 | for (int i = 0; i < childSessions.size(); ++i) { |
| 1737 | final PackageInstallerSession session = childSessions.get(i); |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 1738 | try { |
Alex Buynytskyy | 133a628 | 2020-01-28 10:47:43 -0800 | [diff] [blame] | 1739 | final PackageManagerService.ActiveInstallSession installingChildSession = |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 1740 | session.makeSessionActiveLocked(); |
Alex Buynytskyy | 133a628 | 2020-01-28 10:47:43 -0800 | [diff] [blame] | 1741 | if (installingChildSession != null) { |
| 1742 | installingChildSessions.add(installingChildSession); |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 1743 | } |
| 1744 | } catch (PackageManagerException e) { |
| 1745 | failure = e; |
| 1746 | success = false; |
| 1747 | } |
| 1748 | } |
| 1749 | if (!success) { |
Mohammad Samiul Islam | 10323a1 | 2020-03-17 10:04:43 +0000 | [diff] [blame] | 1750 | sendOnPackageInstalled(mContext, mRemoteStatusReceiver, sessionId, |
Alex Buynytskyy | 2b1d32a | 2019-11-14 10:06:30 -0800 | [diff] [blame] | 1751 | isInstallerDeviceOwnerOrAffiliatedProfileOwnerLocked(), userId, null, |
| 1752 | failure.error, failure.getLocalizedMessage(), null); |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 1753 | return; |
| 1754 | } |
Alex Buynytskyy | 133a628 | 2020-01-28 10:47:43 -0800 | [diff] [blame] | 1755 | mPm.installStage(installingChildSessions); |
Dario Freni | d8bf22e | 2018-08-31 14:18:04 +0100 | [diff] [blame] | 1756 | } else { |
Alex Buynytskyy | 133a628 | 2020-01-28 10:47:43 -0800 | [diff] [blame] | 1757 | mPm.installStage(installingSession); |
Dario Freni | d8bf22e | 2018-08-31 14:18:04 +0100 | [diff] [blame] | 1758 | } |
| 1759 | } |
| 1760 | |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 1761 | /** |
| 1762 | * Stages this session for install and returns a |
| 1763 | * {@link PackageManagerService.ActiveInstallSession} representing this new staged state or null |
| 1764 | * in case permissions need to be requested before install can proceed. |
| 1765 | */ |
Dario Freni | d8bf22e | 2018-08-31 14:18:04 +0100 | [diff] [blame] | 1766 | @GuardedBy("mLock") |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 1767 | private PackageManagerService.ActiveInstallSession makeSessionActiveLocked() |
| 1768 | throws PackageManagerException { |
| 1769 | if (mRelinquished) { |
| 1770 | throw new PackageManagerException(INSTALL_FAILED_INTERNAL_ERROR, |
| 1771 | "Session relinquished"); |
| 1772 | } |
| 1773 | if (mDestroyed) { |
| 1774 | throw new PackageManagerException(INSTALL_FAILED_INTERNAL_ERROR, "Session destroyed"); |
| 1775 | } |
| 1776 | if (!mSealed) { |
| 1777 | throw new PackageManagerException(INSTALL_FAILED_INTERNAL_ERROR, "Session not sealed"); |
Jie Song | 7e1c9d7 | 2018-11-07 22:59:18 +0000 | [diff] [blame] | 1778 | } |
Patrick | 44da627 | 2018-09-13 15:06:22 -0700 | [diff] [blame] | 1779 | |
Patrick Baumann | a77a677 | 2018-11-12 12:58:46 -0800 | [diff] [blame] | 1780 | final IPackageInstallObserver2 localObserver; |
Alex Buynytskyy | 769f815 | 2020-01-23 16:58:45 +0000 | [diff] [blame] | 1781 | if (isApexInstallation()) { |
Patrick Baumann | a77a677 | 2018-11-12 12:58:46 -0800 | [diff] [blame] | 1782 | localObserver = null; |
| 1783 | } else { |
| 1784 | if (!params.isMultiPackage) { |
Daulet Zhanguzin | 82adfcb | 2020-01-02 17:31:40 +0000 | [diff] [blame] | 1785 | Objects.requireNonNull(mPackageName); |
| 1786 | Objects.requireNonNull(mSigningDetails); |
| 1787 | Objects.requireNonNull(mResolvedBaseFile); |
Jie Song | 7e1c9d7 | 2018-11-07 22:59:18 +0000 | [diff] [blame] | 1788 | |
Patrick Baumann | a77a677 | 2018-11-12 12:58:46 -0800 | [diff] [blame] | 1789 | if (needToAskForPermissionsLocked()) { |
| 1790 | // User needs to confirm installation; |
| 1791 | // give installer an intent they can use to involve |
| 1792 | // user. |
| 1793 | final Intent intent = new Intent(PackageInstaller.ACTION_CONFIRM_INSTALL); |
| 1794 | intent.setPackage(mPm.getPackageInstallerPackageName()); |
| 1795 | intent.putExtra(PackageInstaller.EXTRA_SESSION_ID, sessionId); |
Alex Buynytskyy | 2b1d32a | 2019-11-14 10:06:30 -0800 | [diff] [blame] | 1796 | |
Mohammad Samiul Islam | 10323a1 | 2020-03-17 10:04:43 +0000 | [diff] [blame] | 1797 | sendOnUserActionRequired(mContext, mRemoteStatusReceiver, sessionId, intent); |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 1798 | |
Patrick Baumann | a77a677 | 2018-11-12 12:58:46 -0800 | [diff] [blame] | 1799 | // Commit was keeping session marked as active until now; release |
| 1800 | // that extra refcount so session appears idle. |
| 1801 | closeInternal(false); |
| 1802 | return null; |
| 1803 | } |
| 1804 | |
| 1805 | // Inherit any packages and native libraries from existing install that |
| 1806 | // haven't been overridden. |
| 1807 | if (params.mode == SessionParams.MODE_INHERIT_EXISTING) { |
| 1808 | try { |
| 1809 | final List<File> fromFiles = mResolvedInheritedFiles; |
Alex Buynytskyy | 0054993 | 2019-11-14 08:25:05 -0800 | [diff] [blame] | 1810 | final File toDir = stageDir; |
Patrick Baumann | a77a677 | 2018-11-12 12:58:46 -0800 | [diff] [blame] | 1811 | |
| 1812 | if (LOGD) Slog.d(TAG, "Inherited files: " + mResolvedInheritedFiles); |
| 1813 | if (!mResolvedInheritedFiles.isEmpty() && mInheritedFilesBase == null) { |
| 1814 | throw new IllegalStateException("mInheritedFilesBase == null"); |
Patrick | 44da627 | 2018-09-13 15:06:22 -0700 | [diff] [blame] | 1815 | } |
Patrick Baumann | a77a677 | 2018-11-12 12:58:46 -0800 | [diff] [blame] | 1816 | |
| 1817 | if (isLinkPossible(fromFiles, toDir)) { |
| 1818 | if (!mResolvedInstructionSets.isEmpty()) { |
| 1819 | final File oatDir = new File(toDir, "oat"); |
| 1820 | createOatDirs(mResolvedInstructionSets, oatDir); |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 1821 | } |
Patrick Baumann | a77a677 | 2018-11-12 12:58:46 -0800 | [diff] [blame] | 1822 | // pre-create lib dirs for linking if necessary |
| 1823 | if (!mResolvedNativeLibPaths.isEmpty()) { |
| 1824 | for (String libPath : mResolvedNativeLibPaths) { |
| 1825 | // "/lib/arm64" -> ["lib", "arm64"] |
| 1826 | final int splitIndex = libPath.lastIndexOf('/'); |
| 1827 | if (splitIndex < 0 || splitIndex >= libPath.length() - 1) { |
| 1828 | Slog.e(TAG, |
| 1829 | "Skipping native library creation for linking due" |
| 1830 | + " to invalid path: " + libPath); |
| 1831 | continue; |
| 1832 | } |
| 1833 | final String libDirPath = libPath.substring(1, splitIndex); |
| 1834 | final File libDir = new File(toDir, libDirPath); |
| 1835 | if (!libDir.exists()) { |
| 1836 | NativeLibraryHelper.createNativeLibrarySubdir(libDir); |
| 1837 | } |
| 1838 | final String archDirPath = libPath.substring(splitIndex + 1); |
| 1839 | NativeLibraryHelper.createNativeLibrarySubdir( |
| 1840 | new File(libDir, archDirPath)); |
| 1841 | } |
| 1842 | } |
| 1843 | linkFiles(fromFiles, toDir, mInheritedFilesBase); |
| 1844 | } else { |
| 1845 | // TODO: this should delegate to DCS so the system process |
| 1846 | // avoids holding open FDs into containers. |
| 1847 | copyFiles(fromFiles, toDir); |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 1848 | } |
Patrick Baumann | a77a677 | 2018-11-12 12:58:46 -0800 | [diff] [blame] | 1849 | } catch (IOException e) { |
| 1850 | throw new PackageManagerException(INSTALL_FAILED_INSUFFICIENT_STORAGE, |
| 1851 | "Failed to inherit existing install", e); |
Patrick Baumann | 1bea237 | 2018-03-13 14:26:58 -0700 | [diff] [blame] | 1852 | } |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 1853 | } |
Patrick Baumann | a77a677 | 2018-11-12 12:58:46 -0800 | [diff] [blame] | 1854 | |
| 1855 | // TODO: surface more granular state from dexopt |
| 1856 | mInternalProgress = 0.5f; |
| 1857 | computeProgressLocked(true); |
| 1858 | |
Songchun Fan | 03f92b2 | 2020-02-05 17:31:26 -0800 | [diff] [blame] | 1859 | extractNativeLibraries(stageDir, params.abiOverride, mayInheritNativeLibs()); |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 1860 | } |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 1861 | |
Patrick Baumann | a77a677 | 2018-11-12 12:58:46 -0800 | [diff] [blame] | 1862 | // We've reached point of no return; call into PMS to install the stage. |
| 1863 | // Regardless of success or failure we always destroy session. |
| 1864 | localObserver = new IPackageInstallObserver2.Stub() { |
| 1865 | @Override |
| 1866 | public void onUserActionRequired(Intent intent) { |
| 1867 | throw new IllegalStateException(); |
| 1868 | } |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 1869 | |
Patrick Baumann | a77a677 | 2018-11-12 12:58:46 -0800 | [diff] [blame] | 1870 | @Override |
| 1871 | public void onPackageInstalled(String basePackageName, int returnCode, String msg, |
| 1872 | Bundle extras) { |
| 1873 | destroyInternal(); |
| 1874 | dispatchSessionFinished(returnCode, msg, extras); |
| 1875 | } |
| 1876 | }; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1877 | } |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1878 | |
Jeff Sharkey | e980804 | 2014-09-11 21:15:37 -0700 | [diff] [blame] | 1879 | final UserHandle user; |
| 1880 | if ((params.installFlags & PackageManager.INSTALL_ALL_USERS) != 0) { |
| 1881 | user = UserHandle.ALL; |
| 1882 | } else { |
| 1883 | user = new UserHandle(userId); |
| 1884 | } |
| 1885 | |
Jeff Sharkey | 497c052 | 2015-05-12 13:07:14 -0700 | [diff] [blame] | 1886 | mRelinquished = true; |
JW Wang | 0757179 | 2020-02-17 16:14:19 +0800 | [diff] [blame] | 1887 | return new PackageManagerService.ActiveInstallSession(mPackageName, stageDir, localObserver, |
| 1888 | sessionId, params, mInstallerUid, mInstallSource, user, mSigningDetails); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1889 | } |
| 1890 | |
Calin Juravle | 3fc56c3 | 2017-12-11 18:26:13 -0800 | [diff] [blame] | 1891 | private static void maybeRenameFile(File from, File to) throws PackageManagerException { |
| 1892 | if (!from.equals(to)) { |
| 1893 | if (!from.renameTo(to)) { |
| 1894 | throw new PackageManagerException(INSTALL_FAILED_INTERNAL_ERROR, |
| 1895 | "Could not rename file " + from + " to " + to); |
| 1896 | } |
| 1897 | } |
| 1898 | } |
| 1899 | |
Songchun Fan | cf463af | 2020-04-29 12:43:53 -0700 | [diff] [blame] | 1900 | private void logDataLoaderInstallationSession(int returnCode) { |
| 1901 | // Skip logging the side-loaded app installations, as those are private and aren't reported |
| 1902 | // anywhere; app stores already have a record of the installation and that's why reporting |
| 1903 | // it here is fine |
| 1904 | final String packageNameToLog = |
| 1905 | (params.installFlags & PackageManager.INSTALL_FROM_ADB) == 0 ? mPackageName : ""; |
Songchun Fan | c296cf7 | 2020-03-31 18:38:52 -0700 | [diff] [blame] | 1906 | final long currentTimestamp = System.currentTimeMillis(); |
| 1907 | FrameworkStatsLog.write(FrameworkStatsLog.PACKAGE_INSTALLER_V2_REPORTED, |
| 1908 | isIncrementalInstallation(), |
Songchun Fan | cf463af | 2020-04-29 12:43:53 -0700 | [diff] [blame] | 1909 | packageNameToLog, |
Songchun Fan | c296cf7 | 2020-03-31 18:38:52 -0700 | [diff] [blame] | 1910 | currentTimestamp - createdMillis, |
Songchun Fan | aff0dce | 2020-04-29 15:23:40 -0700 | [diff] [blame] | 1911 | returnCode, |
| 1912 | getApksSize()); |
| 1913 | } |
| 1914 | |
| 1915 | private long getApksSize() { |
| 1916 | final PackageSetting ps = mPm.getPackageSetting(mPackageName); |
| 1917 | if (ps == null) { |
| 1918 | return 0; |
| 1919 | } |
| 1920 | final File apkDirOrPath = ps.codePath; |
| 1921 | if (apkDirOrPath == null) { |
| 1922 | return 0; |
| 1923 | } |
| 1924 | if (apkDirOrPath.isFile() && apkDirOrPath.getName().toLowerCase().endsWith(".apk")) { |
| 1925 | return apkDirOrPath.length(); |
| 1926 | } |
| 1927 | if (!apkDirOrPath.isDirectory()) { |
| 1928 | return 0; |
| 1929 | } |
| 1930 | final File[] files = apkDirOrPath.listFiles(); |
| 1931 | long apksSize = 0; |
| 1932 | for (int i = 0; i < files.length; i++) { |
| 1933 | if (files[i].getName().toLowerCase().endsWith(".apk")) { |
| 1934 | apksSize += files[i].length(); |
| 1935 | } |
| 1936 | } |
| 1937 | return apksSize; |
Songchun Fan | c296cf7 | 2020-03-31 18:38:52 -0700 | [diff] [blame] | 1938 | } |
| 1939 | |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1940 | /** |
Patrick Baumann | 1bea237 | 2018-03-13 14:26:58 -0700 | [diff] [blame] | 1941 | * Returns true if the session should attempt to inherit any existing native libraries already |
| 1942 | * extracted at the current install location. This is necessary to prevent double loading of |
| 1943 | * native libraries already loaded by the running app. |
| 1944 | */ |
| 1945 | private boolean mayInheritNativeLibs() { |
| 1946 | return SystemProperties.getBoolean(PROPERTY_NAME_INHERIT_NATIVE, true) && |
| 1947 | params.mode == SessionParams.MODE_INHERIT_EXISTING && |
| 1948 | (params.installFlags & PackageManager.DONT_KILL_APP) != 0; |
| 1949 | } |
| 1950 | |
| 1951 | /** |
Alex Buynytskyy | 769f815 | 2020-01-23 16:58:45 +0000 | [diff] [blame] | 1952 | * Returns true if the session is installing an APEX package. |
| 1953 | */ |
| 1954 | private boolean isApexInstallation() { |
| 1955 | return (params.installFlags & PackageManager.INSTALL_APEX) != 0; |
| 1956 | } |
| 1957 | |
| 1958 | /** |
Richard Uhler | ca05351 | 2019-01-30 15:20:07 +0000 | [diff] [blame] | 1959 | * Validate apex install. |
| 1960 | * <p> |
Mohammad Samiul Islam | da00497 | 2019-10-09 11:29:26 +0100 | [diff] [blame] | 1961 | * Sets {@link #mResolvedBaseFile} for RollbackManager to use. Sets {@link #mPackageName} for |
| 1962 | * StagingManager to use. |
Richard Uhler | ca05351 | 2019-01-30 15:20:07 +0000 | [diff] [blame] | 1963 | */ |
| 1964 | @GuardedBy("mLock") |
| 1965 | private void validateApexInstallLocked() |
| 1966 | throws PackageManagerException { |
Alex Buynytskyy | c0c5a37 | 2020-01-29 18:59:20 -0800 | [diff] [blame] | 1967 | final List<File> addedFiles = getAddedApksLocked(); |
| 1968 | if (addedFiles.isEmpty()) { |
Nikita Ioffe | 961c7ef | 2020-04-02 22:17:19 +0100 | [diff] [blame] | 1969 | throw new PackageManagerException(INSTALL_FAILED_INVALID_APK, |
| 1970 | String.format("Session: %d. No packages staged in %s", sessionId, |
| 1971 | stageDir.getAbsolutePath())); |
Richard Uhler | ca05351 | 2019-01-30 15:20:07 +0000 | [diff] [blame] | 1972 | } |
| 1973 | |
| 1974 | if (ArrayUtils.size(addedFiles) > 1) { |
| 1975 | throw new PackageManagerException(INSTALL_FAILED_INVALID_APK, |
| 1976 | "Too many files for apex install"); |
| 1977 | } |
| 1978 | |
Alex Buynytskyy | c0c5a37 | 2020-01-29 18:59:20 -0800 | [diff] [blame] | 1979 | File addedFile = addedFiles.get(0); // there is only one file |
Mohammad Samiul Islam | ad7e270 | 2019-07-05 14:47:17 +0100 | [diff] [blame] | 1980 | |
| 1981 | // Ensure file name has proper suffix |
| 1982 | final String sourceName = addedFile.getName(); |
| 1983 | final String targetName = sourceName.endsWith(APEX_FILE_EXTENSION) |
| 1984 | ? sourceName |
| 1985 | : sourceName + APEX_FILE_EXTENSION; |
| 1986 | if (!FileUtils.isValidExtFilename(targetName)) { |
| 1987 | throw new PackageManagerException(INSTALL_FAILED_INVALID_APK, |
| 1988 | "Invalid filename: " + targetName); |
| 1989 | } |
| 1990 | |
Alex Buynytskyy | 0054993 | 2019-11-14 08:25:05 -0800 | [diff] [blame] | 1991 | final File targetFile = new File(stageDir, targetName); |
Mohammad Samiul Islam | ad7e270 | 2019-07-05 14:47:17 +0100 | [diff] [blame] | 1992 | resolveAndStageFile(addedFile, targetFile); |
Mohammad Samiul Islam | ad7e270 | 2019-07-05 14:47:17 +0100 | [diff] [blame] | 1993 | mResolvedBaseFile = targetFile; |
Mohammad Samiul Islam | da00497 | 2019-10-09 11:29:26 +0100 | [diff] [blame] | 1994 | |
| 1995 | // Populate package name of the apex session |
| 1996 | mPackageName = null; |
| 1997 | final ApkLite apk; |
| 1998 | try { |
| 1999 | apk = PackageParser.parseApkLite( |
| 2000 | mResolvedBaseFile, PackageParser.PARSE_COLLECT_CERTIFICATES); |
| 2001 | } catch (PackageParserException e) { |
| 2002 | throw PackageManagerException.from(e); |
| 2003 | } |
| 2004 | |
| 2005 | if (mPackageName == null) { |
| 2006 | mPackageName = apk.packageName; |
| 2007 | mVersionCode = apk.getLongVersionCode(); |
| 2008 | } |
Richard Uhler | ca05351 | 2019-01-30 15:20:07 +0000 | [diff] [blame] | 2009 | } |
| 2010 | |
| 2011 | /** |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 2012 | * Validate install by confirming that all application packages are have |
| 2013 | * consistent package name, version code, and signing certificates. |
| 2014 | * <p> |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 2015 | * Clears and populates {@link #mResolvedBaseFile}, |
| 2016 | * {@link #mResolvedStagedFiles}, and {@link #mResolvedInheritedFiles}. |
| 2017 | * <p> |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 2018 | * Renames package files in stage to match split names defined inside. |
Jeff Sharkey | bb7b7be | 2014-08-19 16:18:28 -0700 | [diff] [blame] | 2019 | * <p> |
| 2020 | * Note that upgrade compatibility is still performed by |
| 2021 | * {@link PackageManagerService}. |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 2022 | */ |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 2023 | @GuardedBy("mLock") |
Alex Buynytskyy | ae3eabe | 2020-02-03 15:00:21 -0800 | [diff] [blame] | 2024 | private void validateApkInstallLocked() throws PackageManagerException { |
Todd Kennedy | 29cfa27 | 2018-09-26 10:25:24 -0700 | [diff] [blame] | 2025 | ApkLite baseApk = null; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 2026 | mPackageName = null; |
| 2027 | mVersionCode = -1; |
Patrick Baumann | 420d58a | 2017-12-19 10:17:21 -0800 | [diff] [blame] | 2028 | mSigningDetails = PackageParser.SigningDetails.UNKNOWN; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 2029 | |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 2030 | mResolvedBaseFile = null; |
| 2031 | mResolvedStagedFiles.clear(); |
| 2032 | mResolvedInheritedFiles.clear(); |
| 2033 | |
Alex Buynytskyy | ae3eabe | 2020-02-03 15:00:21 -0800 | [diff] [blame] | 2034 | final PackageInfo pkgInfo = mPm.getPackageInfo( |
| 2035 | params.appPackageName, PackageManager.GET_SIGNATURES |
| 2036 | | PackageManager.MATCH_STATIC_SHARED_LIBRARIES /*flags*/, userId); |
| 2037 | |
Victor Hsieh | c0cd748 | 2018-10-04 10:10:54 -0700 | [diff] [blame] | 2038 | // Partial installs must be consistent with existing install |
| 2039 | if (params.mode == SessionParams.MODE_INHERIT_EXISTING |
| 2040 | && (pkgInfo == null || pkgInfo.applicationInfo == null)) { |
| 2041 | throw new PackageManagerException(INSTALL_FAILED_INVALID_APK, |
| 2042 | "Missing existing base package"); |
| 2043 | } |
| 2044 | // Default to require only if existing base has fs-verity. |
Victor Hsieh | 0663df4 | 2019-01-07 15:28:27 -0800 | [diff] [blame] | 2045 | mVerityFound = PackageManagerServiceUtils.isApkVerityEnabled() |
| 2046 | && params.mode == SessionParams.MODE_INHERIT_EXISTING |
Victor Hsieh | c0cd748 | 2018-10-04 10:10:54 -0700 | [diff] [blame] | 2047 | && VerityUtils.hasFsverity(pkgInfo.applicationInfo.getBaseCodePath()); |
| 2048 | |
Alex Buynytskyy | c0c5a37 | 2020-01-29 18:59:20 -0800 | [diff] [blame] | 2049 | final List<File> removedFiles = getRemovedFilesLocked(); |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 2050 | final List<String> removeSplitList = new ArrayList<>(); |
Alex Buynytskyy | c0c5a37 | 2020-01-29 18:59:20 -0800 | [diff] [blame] | 2051 | if (!removedFiles.isEmpty()) { |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 2052 | for (File removedFile : removedFiles) { |
| 2053 | final String fileName = removedFile.getName(); |
| 2054 | final String splitName = fileName.substring( |
Alex Buynytskyy | 0054993 | 2019-11-14 08:25:05 -0800 | [diff] [blame] | 2055 | 0, fileName.length() - REMOVE_MARKER_EXTENSION.length()); |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 2056 | removeSplitList.add(splitName); |
| 2057 | } |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 2058 | } |
| 2059 | |
Alex Buynytskyy | c0c5a37 | 2020-01-29 18:59:20 -0800 | [diff] [blame] | 2060 | final List<File> addedFiles = getAddedApksLocked(); |
| 2061 | if (addedFiles.isEmpty() && removeSplitList.size() == 0) { |
Nikita Ioffe | 961c7ef | 2020-04-02 22:17:19 +0100 | [diff] [blame] | 2062 | throw new PackageManagerException(INSTALL_FAILED_INVALID_APK, |
| 2063 | String.format("Session: %d. No packages staged in %s", sessionId, |
| 2064 | stageDir.getAbsolutePath())); |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 2065 | } |
Calin Juravle | 3fc56c3 | 2017-12-11 18:26:13 -0800 | [diff] [blame] | 2066 | |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 2067 | // Verify that all staged packages are internally consistent |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 2068 | final ArraySet<String> stagedSplits = new ArraySet<>(); |
Winson | 3cb5610 | 2020-04-17 16:24:57 -0700 | [diff] [blame] | 2069 | ParseTypeImpl input = ParseTypeImpl.forDefaultParsing(); |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 2070 | for (File addedFile : addedFiles) { |
Winson | 3cb5610 | 2020-04-17 16:24:57 -0700 | [diff] [blame] | 2071 | ParseResult<ApkLite> result = ApkLiteParseUtils.parseApkLite(input.reset(), |
| 2072 | addedFile, PackageParser.PARSE_COLLECT_CERTIFICATES); |
| 2073 | if (result.isError()) { |
| 2074 | throw new PackageManagerException(result.getErrorCode(), |
| 2075 | result.getErrorMessage(), result.getException()); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 2076 | } |
| 2077 | |
Winson | 3cb5610 | 2020-04-17 16:24:57 -0700 | [diff] [blame] | 2078 | final ApkLite apk = result.getResult(); |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 2079 | if (!stagedSplits.add(apk.splitName)) { |
Jeff Sharkey | e0b0bef | 2014-07-12 15:37:47 -0700 | [diff] [blame] | 2080 | throw new PackageManagerException(INSTALL_FAILED_INVALID_APK, |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 2081 | "Split " + apk.splitName + " was defined multiple times"); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 2082 | } |
| 2083 | |
| 2084 | // Use first package to define unknown values |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 2085 | if (mPackageName == null) { |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 2086 | mPackageName = apk.packageName; |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 2087 | mVersionCode = apk.getLongVersionCode(); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 2088 | } |
Patrick Baumann | 420d58a | 2017-12-19 10:17:21 -0800 | [diff] [blame] | 2089 | if (mSigningDetails == PackageParser.SigningDetails.UNKNOWN) { |
| 2090 | mSigningDetails = apk.signingDetails; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 2091 | } |
| 2092 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 2093 | assertApkConsistentLocked(String.valueOf(addedFile), apk); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 2094 | |
| 2095 | // Take this opportunity to enforce uniform naming |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 2096 | final String targetName; |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 2097 | if (apk.splitName == null) { |
Calin Juravle | 3fc56c3 | 2017-12-11 18:26:13 -0800 | [diff] [blame] | 2098 | targetName = "base" + APK_FILE_EXTENSION; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 2099 | } else { |
Calin Juravle | 3fc56c3 | 2017-12-11 18:26:13 -0800 | [diff] [blame] | 2100 | targetName = "split_" + apk.splitName + APK_FILE_EXTENSION; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 2101 | } |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 2102 | if (!FileUtils.isValidExtFilename(targetName)) { |
Jeff Sharkey | e0b0bef | 2014-07-12 15:37:47 -0700 | [diff] [blame] | 2103 | throw new PackageManagerException(INSTALL_FAILED_INVALID_APK, |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 2104 | "Invalid filename: " + targetName); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 2105 | } |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 2106 | |
Alex Buynytskyy | 0054993 | 2019-11-14 08:25:05 -0800 | [diff] [blame] | 2107 | final File targetFile = new File(stageDir, targetName); |
Victor Hsieh | c0cd748 | 2018-10-04 10:10:54 -0700 | [diff] [blame] | 2108 | resolveAndStageFile(addedFile, targetFile); |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 2109 | |
| 2110 | // Base is coming from session |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 2111 | if (apk.splitName == null) { |
| 2112 | mResolvedBaseFile = targetFile; |
Todd Kennedy | 29cfa27 | 2018-09-26 10:25:24 -0700 | [diff] [blame] | 2113 | baseApk = apk; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 2114 | } |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 2115 | |
Alex Buynytskyy | 0403545 | 2020-06-06 20:15:58 -0700 | [diff] [blame] | 2116 | // Validate and add Dex Metadata (.dm). |
Calin Juravle | 3fc56c3 | 2017-12-11 18:26:13 -0800 | [diff] [blame] | 2117 | final File dexMetadataFile = DexMetadataHelper.findDexMetadataForFile(addedFile); |
| 2118 | if (dexMetadataFile != null) { |
| 2119 | if (!FileUtils.isValidExtFilename(dexMetadataFile.getName())) { |
| 2120 | throw new PackageManagerException(INSTALL_FAILED_INVALID_APK, |
| 2121 | "Invalid filename: " + dexMetadataFile); |
| 2122 | } |
Alex Buynytskyy | 0054993 | 2019-11-14 08:25:05 -0800 | [diff] [blame] | 2123 | final File targetDexMetadataFile = new File(stageDir, |
Calin Juravle | 3fc56c3 | 2017-12-11 18:26:13 -0800 | [diff] [blame] | 2124 | DexMetadataHelper.buildDexMetadataPathForApk(targetName)); |
Victor Hsieh | c0cd748 | 2018-10-04 10:10:54 -0700 | [diff] [blame] | 2125 | resolveAndStageFile(dexMetadataFile, targetDexMetadataFile); |
Calin Juravle | 3fc56c3 | 2017-12-11 18:26:13 -0800 | [diff] [blame] | 2126 | } |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 2127 | } |
| 2128 | |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 2129 | if (removeSplitList.size() > 0) { |
Todd Kennedy | 544b383 | 2017-08-22 10:48:18 -0700 | [diff] [blame] | 2130 | if (pkgInfo == null) { |
| 2131 | throw new PackageManagerException(INSTALL_FAILED_INVALID_APK, |
| 2132 | "Missing existing base package for " + mPackageName); |
| 2133 | } |
| 2134 | |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 2135 | // validate split names marked for removal |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 2136 | for (String splitName : removeSplitList) { |
Todd Kennedy | f29d07a | 2016-08-08 15:17:43 -0700 | [diff] [blame] | 2137 | if (!ArrayUtils.contains(pkgInfo.splitNames, splitName)) { |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 2138 | throw new PackageManagerException(INSTALL_FAILED_INVALID_APK, |
| 2139 | "Split not found: " + splitName); |
| 2140 | } |
| 2141 | } |
| 2142 | |
| 2143 | // ensure we've got appropriate package name, version code and signatures |
| 2144 | if (mPackageName == null) { |
Todd Kennedy | f29d07a | 2016-08-08 15:17:43 -0700 | [diff] [blame] | 2145 | mPackageName = pkgInfo.packageName; |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 2146 | mVersionCode = pkgInfo.getLongVersionCode(); |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 2147 | } |
Patrick Baumann | 420d58a | 2017-12-19 10:17:21 -0800 | [diff] [blame] | 2148 | if (mSigningDetails == PackageParser.SigningDetails.UNKNOWN) { |
| 2149 | try { |
Gavin Corkery | ed521ab | 2019-01-31 16:59:41 +0000 | [diff] [blame] | 2150 | mSigningDetails = ApkSignatureVerifier.unsafeGetCertsWithoutVerification( |
Patrick Baumann | 420d58a | 2017-12-19 10:17:21 -0800 | [diff] [blame] | 2151 | pkgInfo.applicationInfo.sourceDir, |
| 2152 | PackageParser.SigningDetails.SignatureSchemeVersion.JAR); |
| 2153 | } catch (PackageParserException e) { |
| 2154 | throw new PackageManagerException(INSTALL_FAILED_INVALID_APK, |
| 2155 | "Couldn't obtain signatures from base APK"); |
| 2156 | } |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 2157 | } |
| 2158 | } |
| 2159 | |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 2160 | if (params.mode == SessionParams.MODE_FULL_INSTALL) { |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 2161 | // Full installs must include a base package |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 2162 | if (!stagedSplits.contains(null)) { |
Jeff Sharkey | e0b0bef | 2014-07-12 15:37:47 -0700 | [diff] [blame] | 2163 | throw new PackageManagerException(INSTALL_FAILED_INVALID_APK, |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 2164 | "Full install must include a base package"); |
| 2165 | } |
| 2166 | |
| 2167 | } else { |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 2168 | ApplicationInfo appInfo = pkgInfo.applicationInfo; |
Winson | 3cb5610 | 2020-04-17 16:24:57 -0700 | [diff] [blame] | 2169 | ParseResult<PackageLite> pkgLiteResult = ApkLiteParseUtils.parsePackageLite( |
| 2170 | input.reset(), new File(appInfo.getCodePath()), 0); |
| 2171 | if (pkgLiteResult.isError()) { |
| 2172 | throw new PackageManagerException(PackageManager.INSTALL_FAILED_INTERNAL_ERROR, |
| 2173 | pkgLiteResult.getErrorMessage(), pkgLiteResult.getException()); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 2174 | } |
Winson | 3cb5610 | 2020-04-17 16:24:57 -0700 | [diff] [blame] | 2175 | final PackageLite existing = pkgLiteResult.getResult(); |
| 2176 | ParseResult<ApkLite> apkLiteResult = ApkLiteParseUtils.parseApkLite(input.reset(), |
| 2177 | new File(appInfo.getBaseCodePath()), |
| 2178 | PackageParser.PARSE_COLLECT_CERTIFICATES); |
| 2179 | if (apkLiteResult.isError()) { |
| 2180 | throw new PackageManagerException(PackageManager.INSTALL_FAILED_INTERNAL_ERROR, |
| 2181 | apkLiteResult.getErrorMessage(), apkLiteResult.getException()); |
| 2182 | } |
| 2183 | final ApkLite existingBase = apkLiteResult.getResult(); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 2184 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 2185 | assertApkConsistentLocked("Existing base", existingBase); |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 2186 | |
| 2187 | // Inherit base if not overridden |
| 2188 | if (mResolvedBaseFile == null) { |
Todd Kennedy | f29d07a | 2016-08-08 15:17:43 -0700 | [diff] [blame] | 2189 | mResolvedBaseFile = new File(appInfo.getBaseCodePath()); |
Victor Hsieh | fdc5208 | 2019-02-22 15:22:34 -0800 | [diff] [blame] | 2190 | resolveInheritedFile(mResolvedBaseFile); |
Calin Juravle | 3fc56c3 | 2017-12-11 18:26:13 -0800 | [diff] [blame] | 2191 | // Inherit the dex metadata if present. |
| 2192 | final File baseDexMetadataFile = |
| 2193 | DexMetadataHelper.findDexMetadataForFile(mResolvedBaseFile); |
| 2194 | if (baseDexMetadataFile != null) { |
Victor Hsieh | fdc5208 | 2019-02-22 15:22:34 -0800 | [diff] [blame] | 2195 | resolveInheritedFile(baseDexMetadataFile); |
Calin Juravle | 3fc56c3 | 2017-12-11 18:26:13 -0800 | [diff] [blame] | 2196 | } |
Todd Kennedy | 29cfa27 | 2018-09-26 10:25:24 -0700 | [diff] [blame] | 2197 | baseApk = existingBase; |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 2198 | } |
| 2199 | |
| 2200 | // Inherit splits if not overridden |
| 2201 | if (!ArrayUtils.isEmpty(existing.splitNames)) { |
| 2202 | for (int i = 0; i < existing.splitNames.length; i++) { |
| 2203 | final String splitName = existing.splitNames[i]; |
| 2204 | final File splitFile = new File(existing.splitCodePaths[i]); |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 2205 | final boolean splitRemoved = removeSplitList.contains(splitName); |
| 2206 | if (!stagedSplits.contains(splitName) && !splitRemoved) { |
Victor Hsieh | fdc5208 | 2019-02-22 15:22:34 -0800 | [diff] [blame] | 2207 | resolveInheritedFile(splitFile); |
Calin Juravle | 3fc56c3 | 2017-12-11 18:26:13 -0800 | [diff] [blame] | 2208 | // Inherit the dex metadata if present. |
| 2209 | final File splitDexMetadataFile = |
| 2210 | DexMetadataHelper.findDexMetadataForFile(splitFile); |
| 2211 | if (splitDexMetadataFile != null) { |
Victor Hsieh | fdc5208 | 2019-02-22 15:22:34 -0800 | [diff] [blame] | 2212 | resolveInheritedFile(splitDexMetadataFile); |
Calin Juravle | 3fc56c3 | 2017-12-11 18:26:13 -0800 | [diff] [blame] | 2213 | } |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 2214 | } |
| 2215 | } |
| 2216 | } |
Narayan Kamath | cd1fc14 | 2015-05-11 13:35:59 +0100 | [diff] [blame] | 2217 | |
| 2218 | // Inherit compiled oat directory. |
Todd Kennedy | f29d07a | 2016-08-08 15:17:43 -0700 | [diff] [blame] | 2219 | final File packageInstallDir = (new File(appInfo.getBaseCodePath())).getParentFile(); |
Narayan Kamath | cd1fc14 | 2015-05-11 13:35:59 +0100 | [diff] [blame] | 2220 | mInheritedFilesBase = packageInstallDir; |
| 2221 | final File oatDir = new File(packageInstallDir, "oat"); |
| 2222 | if (oatDir.exists()) { |
| 2223 | final File[] archSubdirs = oatDir.listFiles(); |
Narayan Kamath | cd1fc14 | 2015-05-11 13:35:59 +0100 | [diff] [blame] | 2224 | |
Narayan Kamath | e845a1e | 2015-06-05 11:59:26 +0100 | [diff] [blame] | 2225 | // Keep track of all instruction sets we've seen compiled output for. |
| 2226 | // If we're linking (and not copying) inherited files, we can recreate the |
| 2227 | // instruction set hierarchy and link compiled output. |
| 2228 | if (archSubdirs != null && archSubdirs.length > 0) { |
| 2229 | final String[] instructionSets = InstructionSets.getAllDexCodeInstructionSets(); |
| 2230 | for (File archSubDir : archSubdirs) { |
| 2231 | // Skip any directory that isn't an ISA subdir. |
| 2232 | if (!ArrayUtils.contains(instructionSets, archSubDir.getName())) { |
| 2233 | continue; |
| 2234 | } |
| 2235 | |
Yurii Zubrytskyi | 83727858 | 2020-04-18 20:58:45 -0700 | [diff] [blame] | 2236 | File[] files = archSubDir.listFiles(); |
| 2237 | if (files == null || files.length == 0) { |
| 2238 | continue; |
Narayan Kamath | e845a1e | 2015-06-05 11:59:26 +0100 | [diff] [blame] | 2239 | } |
Yurii Zubrytskyi | 83727858 | 2020-04-18 20:58:45 -0700 | [diff] [blame] | 2240 | |
| 2241 | mResolvedInstructionSets.add(archSubDir.getName()); |
| 2242 | mResolvedInheritedFiles.addAll(Arrays.asList(files)); |
Narayan Kamath | cd1fc14 | 2015-05-11 13:35:59 +0100 | [diff] [blame] | 2243 | } |
| 2244 | } |
| 2245 | } |
Patrick Baumann | 1bea237 | 2018-03-13 14:26:58 -0700 | [diff] [blame] | 2246 | |
| 2247 | // Inherit native libraries for DONT_KILL sessions. |
| 2248 | if (mayInheritNativeLibs() && removeSplitList.isEmpty()) { |
| 2249 | File[] libDirs = new File[]{ |
| 2250 | new File(packageInstallDir, NativeLibraryHelper.LIB_DIR_NAME), |
| 2251 | new File(packageInstallDir, NativeLibraryHelper.LIB64_DIR_NAME)}; |
| 2252 | for (File libDir : libDirs) { |
| 2253 | if (!libDir.exists() || !libDir.isDirectory()) { |
| 2254 | continue; |
| 2255 | } |
Yurii Zubrytskyi | 83727858 | 2020-04-18 20:58:45 -0700 | [diff] [blame] | 2256 | final List<String> libDirsToInherit = new ArrayList<>(); |
| 2257 | final List<File> libFilesToInherit = new ArrayList<>(); |
Patrick Baumann | 1bea237 | 2018-03-13 14:26:58 -0700 | [diff] [blame] | 2258 | for (File archSubDir : libDir.listFiles()) { |
| 2259 | if (!archSubDir.isDirectory()) { |
| 2260 | continue; |
| 2261 | } |
| 2262 | String relLibPath; |
| 2263 | try { |
| 2264 | relLibPath = getRelativePath(archSubDir, packageInstallDir); |
| 2265 | } catch (IOException e) { |
| 2266 | Slog.e(TAG, "Skipping linking of native library directory!", e); |
| 2267 | // shouldn't be possible, but let's avoid inheriting these to be safe |
| 2268 | libDirsToInherit.clear(); |
Yurii Zubrytskyi | 83727858 | 2020-04-18 20:58:45 -0700 | [diff] [blame] | 2269 | libFilesToInherit.clear(); |
Patrick Baumann | 1bea237 | 2018-03-13 14:26:58 -0700 | [diff] [blame] | 2270 | break; |
| 2271 | } |
Yurii Zubrytskyi | 83727858 | 2020-04-18 20:58:45 -0700 | [diff] [blame] | 2272 | |
| 2273 | File[] files = archSubDir.listFiles(); |
| 2274 | if (files == null || files.length == 0) { |
| 2275 | continue; |
Patrick Baumann | 1bea237 | 2018-03-13 14:26:58 -0700 | [diff] [blame] | 2276 | } |
Yurii Zubrytskyi | 83727858 | 2020-04-18 20:58:45 -0700 | [diff] [blame] | 2277 | |
| 2278 | libDirsToInherit.add(relLibPath); |
| 2279 | libFilesToInherit.addAll(Arrays.asList(files)); |
Patrick Baumann | 1bea237 | 2018-03-13 14:26:58 -0700 | [diff] [blame] | 2280 | } |
Yurii Zubrytskyi | 83727858 | 2020-04-18 20:58:45 -0700 | [diff] [blame] | 2281 | for (String subDir : libDirsToInherit) { |
| 2282 | if (!mResolvedNativeLibPaths.contains(subDir)) { |
| 2283 | mResolvedNativeLibPaths.add(subDir); |
| 2284 | } |
| 2285 | } |
| 2286 | mResolvedInheritedFiles.addAll(libFilesToInherit); |
Patrick Baumann | 1bea237 | 2018-03-13 14:26:58 -0700 | [diff] [blame] | 2287 | } |
| 2288 | } |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 2289 | } |
Victor Hsieh | fa9df0b | 2019-01-29 12:48:36 -0800 | [diff] [blame] | 2290 | if (baseApk.useEmbeddedDex) { |
Victor Hsieh | e7b5a8d | 2018-11-16 10:27:06 -0800 | [diff] [blame] | 2291 | for (File file : mResolvedStagedFiles) { |
| 2292 | if (file.getName().endsWith(".apk") |
Victor Hsieh | fa9df0b | 2019-01-29 12:48:36 -0800 | [diff] [blame] | 2293 | && !DexManager.auditUncompressedDexInApk(file.getPath())) { |
Victor Hsieh | e7b5a8d | 2018-11-16 10:27:06 -0800 | [diff] [blame] | 2294 | throw new PackageManagerException(INSTALL_FAILED_INVALID_APK, |
Victor Hsieh | fa9df0b | 2019-01-29 12:48:36 -0800 | [diff] [blame] | 2295 | "Some dex are not uncompressed and aligned correctly for " |
Victor Hsieh | e7b5a8d | 2018-11-16 10:27:06 -0800 | [diff] [blame] | 2296 | + mPackageName); |
| 2297 | } |
| 2298 | } |
| 2299 | } |
Todd Kennedy | 29cfa27 | 2018-09-26 10:25:24 -0700 | [diff] [blame] | 2300 | if (baseApk.isSplitRequired && stagedSplits.size() <= 1) { |
| 2301 | throw new PackageManagerException(INSTALL_FAILED_MISSING_SPLIT, |
| 2302 | "Missing split for " + mPackageName); |
| 2303 | } |
Alex Buynytskyy | 0403545 | 2020-06-06 20:15:58 -0700 | [diff] [blame] | 2304 | |
| 2305 | final boolean isInstallerShell = (mInstallerUid == Process.SHELL_UID); |
| 2306 | if (isInstallerShell && isIncrementalInstallation() && mIncrementalFileStorages != null) { |
| 2307 | if (!baseApk.debuggable && !baseApk.profilableByShell) { |
| 2308 | mIncrementalFileStorages.disableReadLogs(); |
| 2309 | } |
| 2310 | } |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 2311 | } |
| 2312 | |
Victor Hsieh | c0cd748 | 2018-10-04 10:10:54 -0700 | [diff] [blame] | 2313 | private void resolveAndStageFile(File origFile, File targetFile) |
| 2314 | throws PackageManagerException { |
| 2315 | mResolvedStagedFiles.add(targetFile); |
| 2316 | maybeRenameFile(origFile, targetFile); |
| 2317 | |
| 2318 | final File originalSignature = new File( |
| 2319 | VerityUtils.getFsveritySignatureFilePath(origFile.getPath())); |
| 2320 | // Make sure .fsv_sig exists when it should, then resolve and stage it. |
| 2321 | if (originalSignature.exists()) { |
| 2322 | // mVerityFound can only change from false to true here during the staging loop. Since |
| 2323 | // all or none of files should have .fsv_sig, this should only happen in the first time |
| 2324 | // (or never), otherwise bail out. |
| 2325 | if (!mVerityFound) { |
| 2326 | mVerityFound = true; |
| 2327 | if (mResolvedStagedFiles.size() > 1) { |
| 2328 | throw new PackageManagerException(INSTALL_FAILED_BAD_SIGNATURE, |
| 2329 | "Some file is missing fs-verity signature"); |
| 2330 | } |
| 2331 | } |
| 2332 | } else { |
| 2333 | if (!mVerityFound) { |
| 2334 | return; |
| 2335 | } |
| 2336 | throw new PackageManagerException(INSTALL_FAILED_BAD_SIGNATURE, |
| 2337 | "Missing corresponding fs-verity signature to " + origFile); |
| 2338 | } |
| 2339 | |
| 2340 | final File stagedSignature = new File( |
| 2341 | VerityUtils.getFsveritySignatureFilePath(targetFile.getPath())); |
| 2342 | maybeRenameFile(originalSignature, stagedSignature); |
| 2343 | mResolvedStagedFiles.add(stagedSignature); |
| 2344 | } |
| 2345 | |
Victor Hsieh | fdc5208 | 2019-02-22 15:22:34 -0800 | [diff] [blame] | 2346 | private void resolveInheritedFile(File origFile) { |
| 2347 | mResolvedInheritedFiles.add(origFile); |
| 2348 | |
| 2349 | // Inherit the fsverity signature file if present. |
| 2350 | final File fsveritySignatureFile = new File( |
| 2351 | VerityUtils.getFsveritySignatureFilePath(origFile.getPath())); |
| 2352 | if (fsveritySignatureFile.exists()) { |
| 2353 | mResolvedInheritedFiles.add(fsveritySignatureFile); |
| 2354 | } |
| 2355 | } |
| 2356 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 2357 | @GuardedBy("mLock") |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 2358 | private void assertApkConsistentLocked(String tag, ApkLite apk) |
Todd Kennedy | f29d07a | 2016-08-08 15:17:43 -0700 | [diff] [blame] | 2359 | throws PackageManagerException { |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 2360 | if (!mPackageName.equals(apk.packageName)) { |
Jeff Sharkey | e0b0bef | 2014-07-12 15:37:47 -0700 | [diff] [blame] | 2361 | throw new PackageManagerException(INSTALL_FAILED_INVALID_APK, tag + " package " |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 2362 | + apk.packageName + " inconsistent with " + mPackageName); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 2363 | } |
Todd Kennedy | eb9b053 | 2016-03-08 10:10:54 -0800 | [diff] [blame] | 2364 | if (params.appPackageName != null && !params.appPackageName.equals(apk.packageName)) { |
| 2365 | throw new PackageManagerException(INSTALL_FAILED_INVALID_APK, tag |
| 2366 | + " specified package " + params.appPackageName |
| 2367 | + " inconsistent with " + apk.packageName); |
| 2368 | } |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 2369 | if (mVersionCode != apk.getLongVersionCode()) { |
Jeff Sharkey | e0b0bef | 2014-07-12 15:37:47 -0700 | [diff] [blame] | 2370 | throw new PackageManagerException(INSTALL_FAILED_INVALID_APK, tag |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 2371 | + " version code " + apk.versionCode + " inconsistent with " |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 2372 | + mVersionCode); |
| 2373 | } |
Patrick Baumann | 420d58a | 2017-12-19 10:17:21 -0800 | [diff] [blame] | 2374 | if (!mSigningDetails.signaturesMatchExactly(apk.signingDetails)) { |
Jeff Sharkey | e0b0bef | 2014-07-12 15:37:47 -0700 | [diff] [blame] | 2375 | throw new PackageManagerException(INSTALL_FAILED_INVALID_APK, |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 2376 | tag + " signatures are inconsistent"); |
| 2377 | } |
| 2378 | } |
| 2379 | |
| 2380 | /** |
Jeff Sharkey | 88d2a3c | 2014-11-22 16:49:34 -0800 | [diff] [blame] | 2381 | * Determine if creating hard links between source and destination is |
| 2382 | * possible. That is, do they all live on the same underlying device. |
| 2383 | */ |
| 2384 | private boolean isLinkPossible(List<File> fromFiles, File toDir) { |
| 2385 | try { |
| 2386 | final StructStat toStat = Os.stat(toDir.getAbsolutePath()); |
| 2387 | for (File fromFile : fromFiles) { |
| 2388 | final StructStat fromStat = Os.stat(fromFile.getAbsolutePath()); |
| 2389 | if (fromStat.st_dev != toStat.st_dev) { |
| 2390 | return false; |
| 2391 | } |
| 2392 | } |
| 2393 | } catch (ErrnoException e) { |
| 2394 | Slog.w(TAG, "Failed to detect if linking possible: " + e); |
| 2395 | return false; |
| 2396 | } |
| 2397 | return true; |
| 2398 | } |
| 2399 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 2400 | /** |
| 2401 | * @return the uid of the owner this session |
| 2402 | */ |
| 2403 | public int getInstallerUid() { |
| 2404 | synchronized (mLock) { |
| 2405 | return mInstallerUid; |
| 2406 | } |
| 2407 | } |
| 2408 | |
Gavin Corkery | 13f8161 | 2019-03-20 18:22:58 +0000 | [diff] [blame] | 2409 | /** |
Mohammad Samiul Islam | da00497 | 2019-10-09 11:29:26 +0100 | [diff] [blame] | 2410 | * @return the package name of this session |
| 2411 | */ |
| 2412 | String getPackageName() { |
| 2413 | synchronized (mLock) { |
| 2414 | return mPackageName; |
| 2415 | } |
| 2416 | } |
| 2417 | |
| 2418 | /** |
Gavin Corkery | 13f8161 | 2019-03-20 18:22:58 +0000 | [diff] [blame] | 2419 | * @return the timestamp of when this session last changed state |
| 2420 | */ |
| 2421 | public long getUpdatedMillis() { |
| 2422 | synchronized (mLock) { |
| 2423 | return updatedMillis; |
| 2424 | } |
| 2425 | } |
| 2426 | |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 2427 | String getInstallerPackageName() { |
Alan Stokes | 0f0b3e5 | 2020-01-27 12:09:01 +0000 | [diff] [blame] | 2428 | return getInstallSource().installerPackageName; |
| 2429 | } |
| 2430 | |
| 2431 | InstallSource getInstallSource() { |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 2432 | synchronized (mLock) { |
Alan Stokes | 0f0b3e5 | 2020-01-27 12:09:01 +0000 | [diff] [blame] | 2433 | return mInstallSource; |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 2434 | } |
| 2435 | } |
| 2436 | |
Narayan Kamath | e845a1e | 2015-06-05 11:59:26 +0100 | [diff] [blame] | 2437 | private static String getRelativePath(File file, File base) throws IOException { |
Narayan Kamath | cd1fc14 | 2015-05-11 13:35:59 +0100 | [diff] [blame] | 2438 | final String pathStr = file.getAbsolutePath(); |
Narayan Kamath | e845a1e | 2015-06-05 11:59:26 +0100 | [diff] [blame] | 2439 | final String baseStr = base.getAbsolutePath(); |
Narayan Kamath | cd1fc14 | 2015-05-11 13:35:59 +0100 | [diff] [blame] | 2440 | // Don't allow relative paths. |
| 2441 | if (pathStr.contains("/.") ) { |
| 2442 | throw new IOException("Invalid path (was relative) : " + pathStr); |
| 2443 | } |
| 2444 | |
Narayan Kamath | e845a1e | 2015-06-05 11:59:26 +0100 | [diff] [blame] | 2445 | if (pathStr.startsWith(baseStr)) { |
| 2446 | return pathStr.substring(baseStr.length()); |
Narayan Kamath | cd1fc14 | 2015-05-11 13:35:59 +0100 | [diff] [blame] | 2447 | } |
| 2448 | |
Narayan Kamath | e845a1e | 2015-06-05 11:59:26 +0100 | [diff] [blame] | 2449 | throw new IOException("File: " + pathStr + " outside base: " + baseStr); |
Narayan Kamath | cd1fc14 | 2015-05-11 13:35:59 +0100 | [diff] [blame] | 2450 | } |
| 2451 | |
Jeff Sharkey | fdeeeea | 2016-01-11 17:34:24 -0700 | [diff] [blame] | 2452 | private void createOatDirs(List<String> instructionSets, File fromDir) |
| 2453 | throws PackageManagerException { |
Narayan Kamath | e845a1e | 2015-06-05 11:59:26 +0100 | [diff] [blame] | 2454 | for (String instructionSet : instructionSets) { |
Jeff Sharkey | fdeeeea | 2016-01-11 17:34:24 -0700 | [diff] [blame] | 2455 | try { |
| 2456 | mPm.mInstaller.createOatDir(fromDir.getAbsolutePath(), instructionSet); |
| 2457 | } catch (InstallerException e) { |
| 2458 | throw PackageManagerException.from(e); |
| 2459 | } |
Narayan Kamath | e845a1e | 2015-06-05 11:59:26 +0100 | [diff] [blame] | 2460 | } |
| 2461 | } |
| 2462 | |
| 2463 | private void linkFiles(List<File> fromFiles, File toDir, File fromDir) |
Narayan Kamath | cd1fc14 | 2015-05-11 13:35:59 +0100 | [diff] [blame] | 2464 | throws IOException { |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 2465 | for (File fromFile : fromFiles) { |
Narayan Kamath | e845a1e | 2015-06-05 11:59:26 +0100 | [diff] [blame] | 2466 | final String relativePath = getRelativePath(fromFile, fromDir); |
Jeff Sharkey | fdeeeea | 2016-01-11 17:34:24 -0700 | [diff] [blame] | 2467 | try { |
| 2468 | mPm.mInstaller.linkFile(relativePath, fromDir.getAbsolutePath(), |
| 2469 | toDir.getAbsolutePath()); |
| 2470 | } catch (InstallerException e) { |
Narayan Kamath | e845a1e | 2015-06-05 11:59:26 +0100 | [diff] [blame] | 2471 | throw new IOException("failed linkOrCreateDir(" + relativePath + ", " |
Jeff Sharkey | fdeeeea | 2016-01-11 17:34:24 -0700 | [diff] [blame] | 2472 | + fromDir + ", " + toDir + ")", e); |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 2473 | } |
| 2474 | } |
Narayan Kamath | e845a1e | 2015-06-05 11:59:26 +0100 | [diff] [blame] | 2475 | |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 2476 | Slog.d(TAG, "Linked " + fromFiles.size() + " files into " + toDir); |
| 2477 | } |
| 2478 | |
| 2479 | private static void copyFiles(List<File> fromFiles, File toDir) throws IOException { |
| 2480 | // Remove any partial files from previous attempt |
| 2481 | for (File file : toDir.listFiles()) { |
| 2482 | if (file.getName().endsWith(".tmp")) { |
| 2483 | file.delete(); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 2484 | } |
| 2485 | } |
Jeff Sharkey | 9a44577 | 2014-07-16 11:32:08 -0700 | [diff] [blame] | 2486 | |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 2487 | for (File fromFile : fromFiles) { |
| 2488 | final File tmpFile = File.createTempFile("inherit", ".tmp", toDir); |
| 2489 | if (LOGD) Slog.d(TAG, "Copying " + fromFile + " to " + tmpFile); |
| 2490 | if (!FileUtils.copyFile(fromFile, tmpFile)) { |
| 2491 | throw new IOException("Failed to copy " + fromFile + " to " + tmpFile); |
| 2492 | } |
Jeff Sharkey | 88d2a3c | 2014-11-22 16:49:34 -0800 | [diff] [blame] | 2493 | try { |
| 2494 | Os.chmod(tmpFile.getAbsolutePath(), 0644); |
| 2495 | } catch (ErrnoException e) { |
| 2496 | throw new IOException("Failed to chmod " + tmpFile); |
| 2497 | } |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 2498 | final File toFile = new File(toDir, fromFile.getName()); |
| 2499 | if (LOGD) Slog.d(TAG, "Renaming " + tmpFile + " to " + toFile); |
| 2500 | if (!tmpFile.renameTo(toFile)) { |
| 2501 | throw new IOException("Failed to rename " + tmpFile + " to " + toFile); |
| 2502 | } |
| 2503 | } |
| 2504 | Slog.d(TAG, "Copied " + fromFiles.size() + " files into " + toDir); |
| 2505 | } |
| 2506 | |
Songchun Fan | 03f92b2 | 2020-02-05 17:31:26 -0800 | [diff] [blame] | 2507 | private void extractNativeLibraries(File packageDir, String abiOverride, boolean inherit) |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 2508 | throws PackageManagerException { |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 2509 | final File libDir = new File(packageDir, NativeLibraryHelper.LIB_DIR_NAME); |
Patrick Baumann | 1bea237 | 2018-03-13 14:26:58 -0700 | [diff] [blame] | 2510 | if (!inherit) { |
| 2511 | // Start from a clean slate |
| 2512 | NativeLibraryHelper.removeNativeBinariesFromDirLI(libDir, true); |
| 2513 | } |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 2514 | |
| 2515 | NativeLibraryHelper.Handle handle = null; |
| 2516 | try { |
| 2517 | handle = NativeLibraryHelper.Handle.create(packageDir); |
| 2518 | final int res = NativeLibraryHelper.copyNativeBinariesWithOverride(handle, libDir, |
Songchun Fan | 03f92b2 | 2020-02-05 17:31:26 -0800 | [diff] [blame] | 2519 | abiOverride, isIncrementalInstallation()); |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 2520 | if (res != PackageManager.INSTALL_SUCCEEDED) { |
| 2521 | throw new PackageManagerException(res, |
| 2522 | "Failed to extract native libraries, res=" + res); |
| 2523 | } |
| 2524 | } catch (IOException e) { |
| 2525 | throw new PackageManagerException(INSTALL_FAILED_INTERNAL_ERROR, |
| 2526 | "Failed to extract native libraries", e); |
| 2527 | } finally { |
| 2528 | IoUtils.closeQuietly(handle); |
| 2529 | } |
| 2530 | } |
| 2531 | |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 2532 | void setPermissionsResult(boolean accepted) { |
| 2533 | if (!mSealed) { |
| 2534 | throw new SecurityException("Must be sealed to accept permissions"); |
| 2535 | } |
| 2536 | |
| 2537 | if (accepted) { |
| 2538 | // Mark and kick off another install pass |
Todd Kennedy | a1d12cf | 2015-09-29 15:43:00 -0700 | [diff] [blame] | 2539 | synchronized (mLock) { |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 2540 | mPermissionsManuallyAccepted = true; |
Alex Buynytskyy | 133a628 | 2020-01-28 10:47:43 -0800 | [diff] [blame] | 2541 | mHandler.obtainMessage(MSG_INSTALL).sendToTarget(); |
Todd Kennedy | a1d12cf | 2015-09-29 15:43:00 -0700 | [diff] [blame] | 2542 | } |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 2543 | } else { |
| 2544 | destroyInternal(); |
Jeff Sharkey | f060095 | 2014-08-07 17:31:53 -0700 | [diff] [blame] | 2545 | dispatchSessionFinished(INSTALL_FAILED_ABORTED, "User rejected permissions", null); |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 2546 | } |
| 2547 | } |
| 2548 | |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 2549 | /** |
| 2550 | * Adds a child session ID without any safety / sanity checks. This should only be used to |
| 2551 | * build a session from XML or similar. |
| 2552 | */ |
| 2553 | void addChildSessionIdInternal(int sessionId) { |
| 2554 | mChildSessionIds.put(sessionId, 0); |
| 2555 | } |
| 2556 | |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 2557 | public void open() throws IOException { |
| 2558 | if (mActiveCount.getAndIncrement() == 0) { |
Jeff Sharkey | bc7bce3 | 2014-09-05 15:53:05 -0700 | [diff] [blame] | 2559 | mCallback.onSessionActiveChanged(this, true); |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 2560 | } |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 2561 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 2562 | boolean wasPrepared; |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 2563 | synchronized (mLock) { |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 2564 | wasPrepared = mPrepared; |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 2565 | if (!mPrepared) { |
| 2566 | if (stageDir != null) { |
Jeff Sharkey | b2b9ab8 | 2015-04-05 21:10:42 -0700 | [diff] [blame] | 2567 | prepareStageDir(stageDir); |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 2568 | } else if (params.isMultiPackage) { |
| 2569 | // it's all ok |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 2570 | } else { |
Jeff Sharkey | f8bb244 | 2017-09-21 19:09:30 -0600 | [diff] [blame] | 2571 | throw new IllegalArgumentException("stageDir must be set"); |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 2572 | } |
| 2573 | |
| 2574 | mPrepared = true; |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 2575 | } |
| 2576 | } |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 2577 | |
| 2578 | if (!wasPrepared) { |
| 2579 | mCallback.onSessionPrepared(this); |
| 2580 | } |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 2581 | } |
| 2582 | |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 2583 | @Override |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 2584 | public void close() { |
Philip P. Moltmann | f46edf5 | 2017-08-08 10:44:34 -0700 | [diff] [blame] | 2585 | closeInternal(true); |
| 2586 | } |
| 2587 | |
| 2588 | private void closeInternal(boolean checkCaller) { |
Philip P. Moltmann | 3306c0d | 2017-08-08 09:19:23 -0700 | [diff] [blame] | 2589 | int activeCount; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 2590 | synchronized (mLock) { |
Philip P. Moltmann | f46edf5 | 2017-08-08 10:44:34 -0700 | [diff] [blame] | 2591 | if (checkCaller) { |
| 2592 | assertCallerIsOwnerOrRootLocked(); |
| 2593 | } |
| 2594 | |
Philip P. Moltmann | 3306c0d | 2017-08-08 09:19:23 -0700 | [diff] [blame] | 2595 | activeCount = mActiveCount.decrementAndGet(); |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 2596 | } |
| 2597 | |
Philip P. Moltmann | 3306c0d | 2017-08-08 09:19:23 -0700 | [diff] [blame] | 2598 | if (activeCount == 0) { |
Jeff Sharkey | bc7bce3 | 2014-09-05 15:53:05 -0700 | [diff] [blame] | 2599 | mCallback.onSessionActiveChanged(this, false); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 2600 | } |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 2601 | } |
| 2602 | |
| 2603 | @Override |
| 2604 | public void abandon() { |
Patrick Baumann | 00321b7 | 2019-04-09 15:07:25 -0700 | [diff] [blame] | 2605 | if (hasParentSessionId()) { |
| 2606 | throw new IllegalStateException( |
| 2607 | "Session " + sessionId + " is a child of multi-package session " |
| 2608 | + mParentSessionId + " and may not be abandoned directly."); |
| 2609 | } |
JW Wang | 8a207c4 | 2020-06-22 16:22:08 +0800 | [diff] [blame] | 2610 | |
| 2611 | List<PackageInstallerSession> childSessions = getChildSessionsNotLocked(); |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 2612 | synchronized (mLock) { |
Mohammad Samiul Islam | a0623e2 | 2020-04-24 09:26:19 +0100 | [diff] [blame] | 2613 | if (params.isStaged && mDestroyed) { |
| 2614 | // If a user abandons staged session in an unsafe state, then system will try to |
| 2615 | // abandon the destroyed staged session when it is safe on behalf of the user. |
| 2616 | assertCallerIsOwnerOrRootOrSystemLocked(); |
| 2617 | } else { |
| 2618 | assertCallerIsOwnerOrRootLocked(); |
| 2619 | } |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 2620 | |
Dario Freni | 503c8a0 | 2019-04-25 10:57:50 +0100 | [diff] [blame] | 2621 | if (isStagedAndInTerminalState()) { |
| 2622 | // We keep the session in the database if it's in a finalized state. It will be |
| 2623 | // removed by PackageInstallerService when the last update time is old enough. |
| 2624 | // Also, in such cases cleanStageDir() has already been executed so no need to |
| 2625 | // do it now. |
| 2626 | return; |
| 2627 | } |
shafik | 07205e3 | 2019-02-07 20:12:33 +0000 | [diff] [blame] | 2628 | if (mCommitted && params.isStaged) { |
Mohammad Samiul Islam | a0623e2 | 2020-04-24 09:26:19 +0100 | [diff] [blame] | 2629 | mDestroyed = true; |
| 2630 | if (!mStagingManager.abortCommittedSessionLocked(this)) { |
| 2631 | // Do not clean up the staged session from system. It is not safe yet. |
| 2632 | mCallback.onStagedSessionChanged(this); |
| 2633 | return; |
shafik | 07205e3 | 2019-02-07 20:12:33 +0000 | [diff] [blame] | 2634 | } |
JW Wang | 8a207c4 | 2020-06-22 16:22:08 +0800 | [diff] [blame] | 2635 | cleanStageDir(childSessions); |
shafik | 07205e3 | 2019-02-07 20:12:33 +0000 | [diff] [blame] | 2636 | } |
| 2637 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 2638 | if (mRelinquished) { |
| 2639 | Slog.d(TAG, "Ignoring abandon after commit relinquished control"); |
| 2640 | return; |
| 2641 | } |
| 2642 | destroyInternal(); |
Jeff Sharkey | 497c052 | 2015-05-12 13:07:14 -0700 | [diff] [blame] | 2643 | } |
Jeff Sharkey | f060095 | 2014-08-07 17:31:53 -0700 | [diff] [blame] | 2644 | dispatchSessionFinished(INSTALL_FAILED_ABORTED, "Session was abandoned", null); |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 2645 | } |
| 2646 | |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 2647 | @Override |
| 2648 | public boolean isMultiPackage() { |
| 2649 | return params.isMultiPackage; |
| 2650 | } |
| 2651 | |
| 2652 | @Override |
Dario Freni | aac4ba4 | 2018-12-06 15:47:16 +0000 | [diff] [blame] | 2653 | public boolean isStaged() { |
| 2654 | return params.isStaged; |
| 2655 | } |
| 2656 | |
| 2657 | @Override |
Alex Buynytskyy | e069787 | 2020-01-13 09:25:21 -0800 | [diff] [blame] | 2658 | public DataLoaderParamsParcel getDataLoaderParams() { |
Todd Kennedy | 66ed8df | 2020-03-25 08:59:34 -0700 | [diff] [blame] | 2659 | mContext.enforceCallingOrSelfPermission(Manifest.permission.USE_INSTALLER_V2, null); |
Alex Buynytskyy | e069787 | 2020-01-13 09:25:21 -0800 | [diff] [blame] | 2660 | return params.dataLoaderParams != null ? params.dataLoaderParams.getData() : null; |
| 2661 | } |
| 2662 | |
| 2663 | @Override |
| 2664 | public void addFile(int location, String name, long lengthBytes, byte[] metadata, |
| 2665 | byte[] signature) { |
Todd Kennedy | 66ed8df | 2020-03-25 08:59:34 -0700 | [diff] [blame] | 2666 | mContext.enforceCallingOrSelfPermission(Manifest.permission.USE_INSTALLER_V2, null); |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 2667 | if (!isDataLoaderInstallation()) { |
| 2668 | throw new IllegalStateException( |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 2669 | "Cannot add files to non-data loader installation session."); |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 2670 | } |
Alex Buynytskyy | 8e9e6a3 | 2020-02-08 14:26:45 -0800 | [diff] [blame] | 2671 | if (isStreamingInstallation()) { |
Alex Buynytskyy | e069787 | 2020-01-13 09:25:21 -0800 | [diff] [blame] | 2672 | if (location != LOCATION_DATA_APP) { |
| 2673 | throw new IllegalArgumentException( |
| 2674 | "Non-incremental installation only supports /data/app placement: " + name); |
| 2675 | } |
| 2676 | } |
Alex Buynytskyy | 8e9e6a3 | 2020-02-08 14:26:45 -0800 | [diff] [blame] | 2677 | if (metadata == null) { |
| 2678 | throw new IllegalArgumentException( |
| 2679 | "DataLoader installation requires valid metadata: " + name); |
| 2680 | } |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 2681 | // Use installer provided name for now; we always rename later |
| 2682 | if (!FileUtils.isValidExtFilename(name)) { |
| 2683 | throw new IllegalArgumentException("Invalid name: " + name); |
| 2684 | } |
| 2685 | |
| 2686 | synchronized (mLock) { |
| 2687 | assertCallerIsOwnerOrRootLocked(); |
| 2688 | assertPreparedAndNotSealedLocked("addFile"); |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 2689 | |
Alex Buynytskyy | c282de9 | 2020-03-10 10:27:42 -0700 | [diff] [blame] | 2690 | if (!mFiles.add(new FileEntry(mFiles.size(), |
| 2691 | new InstallationFile(location, name, lengthBytes, metadata, signature)))) { |
| 2692 | throw new IllegalArgumentException("File already added: " + name); |
| 2693 | } |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 2694 | } |
| 2695 | } |
| 2696 | |
| 2697 | @Override |
Alex Buynytskyy | e069787 | 2020-01-13 09:25:21 -0800 | [diff] [blame] | 2698 | public void removeFile(int location, String name) { |
Todd Kennedy | 66ed8df | 2020-03-25 08:59:34 -0700 | [diff] [blame] | 2699 | mContext.enforceCallingOrSelfPermission(Manifest.permission.USE_INSTALLER_V2, null); |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 2700 | if (!isDataLoaderInstallation()) { |
| 2701 | throw new IllegalStateException( |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 2702 | "Cannot add files to non-data loader installation session."); |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 2703 | } |
| 2704 | if (TextUtils.isEmpty(params.appPackageName)) { |
| 2705 | throw new IllegalStateException("Must specify package name to remove a split"); |
| 2706 | } |
| 2707 | |
| 2708 | synchronized (mLock) { |
| 2709 | assertCallerIsOwnerOrRootLocked(); |
| 2710 | assertPreparedAndNotSealedLocked("removeFile"); |
| 2711 | |
Alex Buynytskyy | c282de9 | 2020-03-10 10:27:42 -0700 | [diff] [blame] | 2712 | if (!mFiles.add(new FileEntry(mFiles.size(), |
| 2713 | new InstallationFile(location, getRemoveMarkerName(name), -1, null, null)))) { |
| 2714 | throw new IllegalArgumentException("File already removed: " + name); |
| 2715 | } |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 2716 | } |
| 2717 | } |
| 2718 | |
| 2719 | /** |
| 2720 | * Makes sure files are present in staging location. |
Alex Buynytskyy | 4dbc060 | 2020-05-12 11:24:14 -0700 | [diff] [blame] | 2721 | * @return if the image is ready for installation |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 2722 | */ |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 2723 | @GuardedBy("mLock") |
| 2724 | private boolean prepareDataLoaderLocked() |
| 2725 | throws PackageManagerException { |
Songchun Fan | 9439be2 | 2020-01-07 18:16:27 -0800 | [diff] [blame] | 2726 | if (!isDataLoaderInstallation()) { |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 2727 | return true; |
| 2728 | } |
| 2729 | if (mDataLoaderFinished) { |
| 2730 | return true; |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 2731 | } |
| 2732 | |
Alex Buynytskyy | 4dbc060 | 2020-05-12 11:24:14 -0700 | [diff] [blame] | 2733 | // Retrying commit. |
| 2734 | if (mIncrementalFileStorages != null) { |
| 2735 | try { |
| 2736 | mIncrementalFileStorages.startLoading(); |
| 2737 | } catch (IOException e) { |
| 2738 | throw new PackageManagerException(INSTALL_FAILED_MEDIA_UNAVAILABLE, e.getMessage(), |
| 2739 | e.getCause()); |
| 2740 | } |
| 2741 | return false; |
| 2742 | } |
| 2743 | |
Songchun Fan | 6381d61 | 2020-02-26 17:59:41 -0800 | [diff] [blame] | 2744 | final List<InstallationFileParcel> addedFiles = new ArrayList<>(); |
| 2745 | final List<String> removedFiles = new ArrayList<>(); |
| 2746 | |
Alex Buynytskyy | c282de9 | 2020-03-10 10:27:42 -0700 | [diff] [blame] | 2747 | final InstallationFile[] files = getInstallationFilesLocked(); |
| 2748 | for (InstallationFile file : files) { |
Alex Buynytskyy | cd4d387 | 2020-02-08 17:50:50 -0800 | [diff] [blame] | 2749 | if (sAddedFilter.accept(new File(this.stageDir, file.getName()))) { |
Songchun Fan | 6381d61 | 2020-02-26 17:59:41 -0800 | [diff] [blame] | 2750 | addedFiles.add(file.getData()); |
| 2751 | continue; |
Alex Buynytskyy | c0c5a37 | 2020-01-29 18:59:20 -0800 | [diff] [blame] | 2752 | } |
Alex Buynytskyy | cd4d387 | 2020-02-08 17:50:50 -0800 | [diff] [blame] | 2753 | if (sRemovedFilter.accept(new File(this.stageDir, file.getName()))) { |
| 2754 | String name = file.getName().substring( |
| 2755 | 0, file.getName().length() - REMOVE_MARKER_EXTENSION.length()); |
Alex Buynytskyy | c0c5a37 | 2020-01-29 18:59:20 -0800 | [diff] [blame] | 2756 | removedFiles.add(name); |
| 2757 | } |
| 2758 | } |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 2759 | |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 2760 | final DataLoaderManager dataLoaderManager = mContext.getSystemService( |
| 2761 | DataLoaderManager.class); |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 2762 | if (dataLoaderManager == null) { |
| 2763 | throw new PackageManagerException(INSTALL_FAILED_MEDIA_UNAVAILABLE, |
| 2764 | "Failed to find data loader manager service"); |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 2765 | } |
| 2766 | |
Alex Buynytskyy | ea1390f | 2020-04-22 16:08:50 -0700 | [diff] [blame] | 2767 | final DataLoaderParams params = this.params.dataLoaderParams; |
Alex Buynytskyy | 04f7391 | 2020-02-10 08:34:18 -0800 | [diff] [blame] | 2768 | final boolean manualStartAndDestroy = !isIncrementalInstallation(); |
Alex Buynytskyy | 8ef61ae | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 2769 | final IDataLoaderStatusListener statusListener = new IDataLoaderStatusListener.Stub() { |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 2770 | @Override |
| 2771 | public void onStatusChanged(int dataLoaderId, int status) { |
Alex Buynytskyy | 2cf1d18 | 2020-03-17 09:33:45 -0700 | [diff] [blame] | 2772 | switch (status) { |
| 2773 | case IDataLoaderStatusListener.DATA_LOADER_STOPPED: |
| 2774 | case IDataLoaderStatusListener.DATA_LOADER_DESTROYED: |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 2775 | return; |
Alex Buynytskyy | 2cf1d18 | 2020-03-17 09:33:45 -0700 | [diff] [blame] | 2776 | } |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 2777 | |
Alex Buynytskyy | 2cf1d18 | 2020-03-17 09:33:45 -0700 | [diff] [blame] | 2778 | if (mDestroyed || mDataLoaderFinished) { |
Alex Buynytskyy | 03a2c38 | 2020-04-24 10:04:50 -0700 | [diff] [blame] | 2779 | switch (status) { |
| 2780 | case IDataLoaderStatusListener.DATA_LOADER_UNRECOVERABLE: |
Alex Buynytskyy | 8ef61ae | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 2781 | onStorageUnhealthy(); |
Alex Buynytskyy | 03a2c38 | 2020-04-24 10:04:50 -0700 | [diff] [blame] | 2782 | return; |
| 2783 | } |
Alex Buynytskyy | 2cf1d18 | 2020-03-17 09:33:45 -0700 | [diff] [blame] | 2784 | return; |
| 2785 | } |
| 2786 | |
| 2787 | try { |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 2788 | IDataLoader dataLoader = dataLoaderManager.getDataLoader(dataLoaderId); |
| 2789 | if (dataLoader == null) { |
| 2790 | mDataLoaderFinished = true; |
Alex Buynytskyy | 64067b2 | 2020-04-25 15:56:52 -0700 | [diff] [blame] | 2791 | dispatchSessionVerificationFailure(INSTALL_FAILED_MEDIA_UNAVAILABLE, |
| 2792 | "Failure to obtain data loader"); |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 2793 | return; |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 2794 | } |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 2795 | |
| 2796 | switch (status) { |
Alex Buynytskyy | ea1390f | 2020-04-22 16:08:50 -0700 | [diff] [blame] | 2797 | case IDataLoaderStatusListener.DATA_LOADER_BOUND: { |
| 2798 | if (manualStartAndDestroy) { |
| 2799 | FileSystemControlParcel control = new FileSystemControlParcel(); |
| 2800 | control.callback = new FileSystemConnector(addedFiles); |
| 2801 | dataLoader.create(dataLoaderId, params.getData(), control, this); |
| 2802 | } |
| 2803 | |
| 2804 | break; |
| 2805 | } |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 2806 | case IDataLoaderStatusListener.DATA_LOADER_CREATED: { |
Alex Buynytskyy | 04f7391 | 2020-02-10 08:34:18 -0800 | [diff] [blame] | 2807 | if (manualStartAndDestroy) { |
| 2808 | // IncrementalFileStorages will call start after all files are |
| 2809 | // created in IncFS. |
Alex Buynytskyy | b6e02f7 | 2020-03-17 18:12:23 -0700 | [diff] [blame] | 2810 | dataLoader.start(dataLoaderId); |
Alex Buynytskyy | 04f7391 | 2020-02-10 08:34:18 -0800 | [diff] [blame] | 2811 | } |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 2812 | break; |
| 2813 | } |
| 2814 | case IDataLoaderStatusListener.DATA_LOADER_STARTED: { |
Songchun Fan | 6381d61 | 2020-02-26 17:59:41 -0800 | [diff] [blame] | 2815 | dataLoader.prepareImage( |
Alex Buynytskyy | b6e02f7 | 2020-03-17 18:12:23 -0700 | [diff] [blame] | 2816 | dataLoaderId, |
Songchun Fan | 6381d61 | 2020-02-26 17:59:41 -0800 | [diff] [blame] | 2817 | addedFiles.toArray( |
| 2818 | new InstallationFileParcel[addedFiles.size()]), |
| 2819 | removedFiles.toArray(new String[removedFiles.size()])); |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 2820 | break; |
| 2821 | } |
| 2822 | case IDataLoaderStatusListener.DATA_LOADER_IMAGE_READY: { |
| 2823 | mDataLoaderFinished = true; |
| 2824 | if (hasParentSessionId()) { |
| 2825 | mSessionProvider.getSession( |
Alex Buynytskyy | 133a628 | 2020-01-28 10:47:43 -0800 | [diff] [blame] | 2826 | mParentSessionId).dispatchStreamValidateAndCommit(); |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 2827 | } else { |
Alex Buynytskyy | 133a628 | 2020-01-28 10:47:43 -0800 | [diff] [blame] | 2828 | dispatchStreamValidateAndCommit(); |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 2829 | } |
Alex Buynytskyy | 04f7391 | 2020-02-10 08:34:18 -0800 | [diff] [blame] | 2830 | if (manualStartAndDestroy) { |
Alex Buynytskyy | b6e02f7 | 2020-03-17 18:12:23 -0700 | [diff] [blame] | 2831 | dataLoader.destroy(dataLoaderId); |
Alex Buynytskyy | 04f7391 | 2020-02-10 08:34:18 -0800 | [diff] [blame] | 2832 | } |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 2833 | break; |
| 2834 | } |
| 2835 | case IDataLoaderStatusListener.DATA_LOADER_IMAGE_NOT_READY: { |
| 2836 | mDataLoaderFinished = true; |
Alex Buynytskyy | 64067b2 | 2020-04-25 15:56:52 -0700 | [diff] [blame] | 2837 | dispatchSessionVerificationFailure(INSTALL_FAILED_MEDIA_UNAVAILABLE, |
| 2838 | "Failed to prepare image."); |
Alex Buynytskyy | 04f7391 | 2020-02-10 08:34:18 -0800 | [diff] [blame] | 2839 | if (manualStartAndDestroy) { |
Alex Buynytskyy | b6e02f7 | 2020-03-17 18:12:23 -0700 | [diff] [blame] | 2840 | dataLoader.destroy(dataLoaderId); |
Alex Buynytskyy | 04f7391 | 2020-02-10 08:34:18 -0800 | [diff] [blame] | 2841 | } |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 2842 | break; |
| 2843 | } |
Alex Buynytskyy | 7e0a1a8 | 2020-04-27 17:06:10 -0700 | [diff] [blame] | 2844 | case IDataLoaderStatusListener.DATA_LOADER_UNAVAILABLE: { |
| 2845 | // Don't fail or commit the session. Allow caller to commit again. |
Alex Buynytskyy | 221cd08 | 2020-05-19 22:18:18 -0700 | [diff] [blame] | 2846 | sendPendingStreaming("DataLoader unavailable"); |
Alex Buynytskyy | 7e0a1a8 | 2020-04-27 17:06:10 -0700 | [diff] [blame] | 2847 | break; |
| 2848 | } |
Alex Buynytskyy | 03a2c38 | 2020-04-24 10:04:50 -0700 | [diff] [blame] | 2849 | case IDataLoaderStatusListener.DATA_LOADER_UNRECOVERABLE: |
| 2850 | mDataLoaderFinished = true; |
Alex Buynytskyy | 64067b2 | 2020-04-25 15:56:52 -0700 | [diff] [blame] | 2851 | dispatchSessionVerificationFailure(INSTALL_FAILED_MEDIA_UNAVAILABLE, |
| 2852 | "DataLoader reported unrecoverable failure."); |
Alex Buynytskyy | 7e0a1a8 | 2020-04-27 17:06:10 -0700 | [diff] [blame] | 2853 | break; |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 2854 | } |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 2855 | } catch (RemoteException e) { |
| 2856 | // In case of streaming failure we don't want to fail or commit the session. |
| 2857 | // Just return from this method and allow caller to commit again. |
Alex Buynytskyy | 221cd08 | 2020-05-19 22:18:18 -0700 | [diff] [blame] | 2858 | sendPendingStreaming(e.getMessage()); |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 2859 | } |
| 2860 | } |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 2861 | }; |
| 2862 | |
Alex Buynytskyy | 04f7391 | 2020-02-10 08:34:18 -0800 | [diff] [blame] | 2863 | if (!manualStartAndDestroy) { |
Alex Buynytskyy | 8ef61ae | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 2864 | final StorageHealthCheckParams healthCheckParams = new StorageHealthCheckParams(); |
| 2865 | healthCheckParams.blockedTimeoutMs = INCREMENTAL_STORAGE_BLOCKED_TIMEOUT_MS; |
| 2866 | healthCheckParams.unhealthyTimeoutMs = INCREMENTAL_STORAGE_UNHEALTHY_TIMEOUT_MS; |
| 2867 | healthCheckParams.unhealthyMonitoringMs = INCREMENTAL_STORAGE_UNHEALTHY_MONITORING_MS; |
| 2868 | |
| 2869 | final boolean systemDataLoader = |
| 2870 | params.getComponentName().getPackageName() == SYSTEM_DATA_LOADER_PACKAGE; |
| 2871 | final IStorageHealthListener healthListener = new IStorageHealthListener.Stub() { |
| 2872 | @Override |
| 2873 | public void onHealthStatus(int storageId, int status) { |
| 2874 | if (mDestroyed || mDataLoaderFinished) { |
| 2875 | // App's installed. |
| 2876 | switch (status) { |
| 2877 | case IStorageHealthListener.HEALTH_STATUS_UNHEALTHY: |
| 2878 | onStorageUnhealthy(); |
| 2879 | return; |
| 2880 | } |
| 2881 | return; |
| 2882 | } |
| 2883 | |
| 2884 | switch (status) { |
| 2885 | case IStorageHealthListener.HEALTH_STATUS_OK: |
| 2886 | break; |
| 2887 | case IStorageHealthListener.HEALTH_STATUS_READS_PENDING: |
| 2888 | case IStorageHealthListener.HEALTH_STATUS_BLOCKED: |
| 2889 | if (systemDataLoader) { |
| 2890 | // It's OK for ADB data loader to wait for pages. |
| 2891 | break; |
| 2892 | } |
| 2893 | // fallthrough |
| 2894 | case IStorageHealthListener.HEALTH_STATUS_UNHEALTHY: |
| 2895 | // Even ADB installation can't wait for missing pages for too long. |
| 2896 | mDataLoaderFinished = true; |
| 2897 | dispatchSessionVerificationFailure(INSTALL_FAILED_MEDIA_UNAVAILABLE, |
| 2898 | "Image is missing pages required for installation."); |
| 2899 | break; |
| 2900 | } |
| 2901 | } |
| 2902 | }; |
| 2903 | |
Alex Buynytskyy | 04f7391 | 2020-02-10 08:34:18 -0800 | [diff] [blame] | 2904 | try { |
Alex Buynytskyy | ea1390f | 2020-04-22 16:08:50 -0700 | [diff] [blame] | 2905 | mIncrementalFileStorages = IncrementalFileStorages.initialize(mContext, stageDir, |
Alex Buynytskyy | 8ef61ae | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 2906 | params, statusListener, healthCheckParams, healthListener, addedFiles); |
Alex Buynytskyy | 04f7391 | 2020-02-10 08:34:18 -0800 | [diff] [blame] | 2907 | return false; |
| 2908 | } catch (IOException e) { |
| 2909 | throw new PackageManagerException(INSTALL_FAILED_MEDIA_UNAVAILABLE, e.getMessage(), |
| 2910 | e.getCause()); |
| 2911 | } |
| 2912 | } |
| 2913 | |
Alex Buynytskyy | 8ef61ae | 2020-05-08 16:18:52 -0700 | [diff] [blame] | 2914 | if (!dataLoaderManager.bindToDataLoader(sessionId, params.getData(), statusListener)) { |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 2915 | throw new PackageManagerException(INSTALL_FAILED_MEDIA_UNAVAILABLE, |
| 2916 | "Failed to initialize data loader"); |
| 2917 | } |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 2918 | |
Alex Buynytskyy | 6bce3f6 | 2020-01-16 13:39:16 -0800 | [diff] [blame] | 2919 | return false; |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 2920 | } |
| 2921 | |
Alex Buynytskyy | 64067b2 | 2020-04-25 15:56:52 -0700 | [diff] [blame] | 2922 | private void dispatchSessionVerificationFailure(int error, String detailMessage) { |
| 2923 | mHandler.obtainMessage(MSG_SESSION_VERIFICATION_FAILURE, error, -1, |
| 2924 | detailMessage).sendToTarget(); |
| 2925 | } |
| 2926 | |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 2927 | @Override |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 2928 | public int[] getChildSessionIds() { |
| 2929 | final int[] childSessionIds = mChildSessionIds.copyKeys(); |
| 2930 | if (childSessionIds != null) { |
| 2931 | return childSessionIds; |
| 2932 | } |
| 2933 | return EMPTY_CHILD_SESSION_ARRAY; |
| 2934 | } |
| 2935 | |
| 2936 | @Override |
Patrick Baumann | 00321b7 | 2019-04-09 15:07:25 -0700 | [diff] [blame] | 2937 | public void addChildSessionId(int childSessionId) { |
Dario Freni | 015f935 | 2019-01-14 21:56:17 +0000 | [diff] [blame] | 2938 | final PackageInstallerSession childSession = mSessionProvider.getSession(childSessionId); |
Patrick Baumann | 00321b7 | 2019-04-09 15:07:25 -0700 | [diff] [blame] | 2939 | if (childSession == null |
| 2940 | || (childSession.hasParentSessionId() && childSession.mParentSessionId != sessionId) |
| 2941 | || childSession.mCommitted |
| 2942 | || childSession.mDestroyed) { |
| 2943 | throw new IllegalStateException("Unable to add child session " + childSessionId |
| 2944 | + " as it does not exist or is in an invalid state."); |
Dario Freni | 015f935 | 2019-01-14 21:56:17 +0000 | [diff] [blame] | 2945 | } |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 2946 | synchronized (mLock) { |
Richard Uhler | 257a487 | 2019-02-21 16:02:01 +0000 | [diff] [blame] | 2947 | assertCallerIsOwnerOrRootLocked(); |
| 2948 | assertPreparedAndNotSealedLocked("addChildSessionId"); |
| 2949 | |
Dario Freni | 015f935 | 2019-01-14 21:56:17 +0000 | [diff] [blame] | 2950 | final int indexOfSession = mChildSessionIds.indexOfKey(childSessionId); |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 2951 | if (indexOfSession >= 0) { |
| 2952 | return; |
| 2953 | } |
Dario Freni | 015f935 | 2019-01-14 21:56:17 +0000 | [diff] [blame] | 2954 | childSession.setParentSessionId(this.sessionId); |
| 2955 | addChildSessionIdInternal(childSessionId); |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 2956 | } |
| 2957 | } |
| 2958 | |
| 2959 | @Override |
| 2960 | public void removeChildSessionId(int sessionId) { |
| 2961 | final PackageInstallerSession session = mSessionProvider.getSession(sessionId); |
| 2962 | synchronized (mLock) { |
| 2963 | final int indexOfSession = mChildSessionIds.indexOfKey(sessionId); |
| 2964 | if (session != null) { |
| 2965 | session.setParentSessionId(SessionInfo.INVALID_ID); |
| 2966 | } |
| 2967 | if (indexOfSession < 0) { |
| 2968 | // not added in the first place; no-op |
| 2969 | return; |
| 2970 | } |
| 2971 | mChildSessionIds.removeAt(indexOfSession); |
| 2972 | } |
| 2973 | } |
| 2974 | |
| 2975 | /** |
| 2976 | * Sets the parent session ID if not already set. |
| 2977 | * If {@link SessionInfo#INVALID_ID} is passed, it will be unset. |
| 2978 | */ |
| 2979 | void setParentSessionId(int parentSessionId) { |
| 2980 | synchronized (mLock) { |
| 2981 | if (parentSessionId != SessionInfo.INVALID_ID |
| 2982 | && mParentSessionId != SessionInfo.INVALID_ID) { |
Patrick Baumann | 00321b7 | 2019-04-09 15:07:25 -0700 | [diff] [blame] | 2983 | throw new IllegalStateException("The parent of " + sessionId + " is" + " already" |
| 2984 | + "set to " + mParentSessionId); |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 2985 | } |
| 2986 | this.mParentSessionId = parentSessionId; |
| 2987 | } |
| 2988 | } |
| 2989 | |
| 2990 | boolean hasParentSessionId() { |
| 2991 | return mParentSessionId != SessionInfo.INVALID_ID; |
| 2992 | } |
| 2993 | |
| 2994 | @Override |
| 2995 | public int getParentSessionId() { |
| 2996 | return mParentSessionId; |
| 2997 | } |
| 2998 | |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 2999 | private void dispatchSessionFinished(int returnCode, String msg, Bundle extras) { |
Alex Buynytskyy | 2b1d32a | 2019-11-14 10:06:30 -0800 | [diff] [blame] | 3000 | final IntentSender statusReceiver; |
Todd Kennedy | beec8e2 | 2017-08-11 10:15:04 -0700 | [diff] [blame] | 3001 | final String packageName; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 3002 | synchronized (mLock) { |
| 3003 | mFinalStatus = returnCode; |
| 3004 | mFinalMessage = msg; |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 3005 | |
Alex Buynytskyy | 2b1d32a | 2019-11-14 10:06:30 -0800 | [diff] [blame] | 3006 | statusReceiver = mRemoteStatusReceiver; |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 3007 | packageName = mPackageName; |
| 3008 | } |
| 3009 | |
Alex Buynytskyy | 2b1d32a | 2019-11-14 10:06:30 -0800 | [diff] [blame] | 3010 | if (statusReceiver != null) { |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 3011 | // Execute observer.onPackageInstalled on different thread as we don't want callers |
Philip P. Moltmann | 0e4ac4c | 2017-09-14 10:29:03 -0700 | [diff] [blame] | 3012 | // inside the system server have to worry about catching the callbacks while they are |
| 3013 | // calling into the session |
| 3014 | final SomeArgs args = SomeArgs.obtain(); |
| 3015 | args.arg1 = packageName; |
| 3016 | args.arg2 = msg; |
| 3017 | args.arg3 = extras; |
Alex Buynytskyy | 2b1d32a | 2019-11-14 10:06:30 -0800 | [diff] [blame] | 3018 | args.arg4 = statusReceiver; |
Philip P. Moltmann | 0e4ac4c | 2017-09-14 10:29:03 -0700 | [diff] [blame] | 3019 | args.argi1 = returnCode; |
| 3020 | |
| 3021 | mHandler.obtainMessage(MSG_ON_PACKAGE_INSTALLED, args).sendToTarget(); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 3022 | } |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 3023 | |
| 3024 | final boolean success = (returnCode == PackageManager.INSTALL_SUCCEEDED); |
Sunny Goyal | 6d7cb23 | 2017-01-30 10:43:18 -0800 | [diff] [blame] | 3025 | |
| 3026 | // Send broadcast to default launcher only if it's a new install |
Gavin Corkery | 8f5109dd | 2019-11-11 12:35:14 +0000 | [diff] [blame] | 3027 | // TODO(b/144270665): Secure the usage of this broadcast. |
Sunny Goyal | 6d7cb23 | 2017-01-30 10:43:18 -0800 | [diff] [blame] | 3028 | final boolean isNewInstall = extras == null || !extras.getBoolean(Intent.EXTRA_REPLACING); |
Gavin Corkery | 8f5109dd | 2019-11-11 12:35:14 +0000 | [diff] [blame] | 3029 | if (success && isNewInstall && mPm.mInstallerService.okToSendBroadcasts() |
| 3030 | && (params.installFlags & PackageManager.INSTALL_DRY_RUN) == 0) { |
Patrick Baumann | 6bc126b | 2020-03-06 10:34:17 -0800 | [diff] [blame] | 3031 | mPm.sendSessionCommitBroadcast(generateInfoScrubbed(true /*icon*/), userId); |
Sunny Goyal | 6d7cb23 | 2017-01-30 10:43:18 -0800 | [diff] [blame] | 3032 | } |
| 3033 | |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 3034 | mCallback.onSessionFinished(this, success); |
Songchun Fan | c296cf7 | 2020-03-31 18:38:52 -0700 | [diff] [blame] | 3035 | if (isDataLoaderInstallation()) { |
Songchun Fan | cf463af | 2020-04-29 12:43:53 -0700 | [diff] [blame] | 3036 | logDataLoaderInstallationSession(returnCode); |
Songchun Fan | c296cf7 | 2020-03-31 18:38:52 -0700 | [diff] [blame] | 3037 | } |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 3038 | } |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 3039 | |
Dario Freni | 0180d0b | 2019-01-11 21:08:13 +0000 | [diff] [blame] | 3040 | /** {@hide} */ |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 3041 | void setStagedSessionReady() { |
| 3042 | synchronized (mLock) { |
Mohammad Samiul Islam | a0623e2 | 2020-04-24 09:26:19 +0100 | [diff] [blame] | 3043 | if (mDestroyed) return; // Do not allow destroyed staged session to change state |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 3044 | mStagedSessionReady = true; |
| 3045 | mStagedSessionApplied = false; |
| 3046 | mStagedSessionFailed = false; |
Dario Freni | b6d2896 | 2019-01-31 15:52:24 +0000 | [diff] [blame] | 3047 | mStagedSessionErrorCode = SessionInfo.STAGED_SESSION_NO_ERROR; |
Dario Freni | 275b4ab | 2019-01-25 09:55:16 +0000 | [diff] [blame] | 3048 | mStagedSessionErrorMessage = ""; |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 3049 | } |
Dario Freni | 0180d0b | 2019-01-11 21:08:13 +0000 | [diff] [blame] | 3050 | mCallback.onStagedSessionChanged(this); |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 3051 | } |
| 3052 | |
Narayan Kamath | 9dfa674 | 2019-01-04 14:22:50 +0000 | [diff] [blame] | 3053 | /** {@hide} */ |
Dario Freni | 275b4ab | 2019-01-25 09:55:16 +0000 | [diff] [blame] | 3054 | void setStagedSessionFailed(@StagedSessionErrorCode int errorCode, |
| 3055 | String errorMessage) { |
Narayan Kamath | 9dfa674 | 2019-01-04 14:22:50 +0000 | [diff] [blame] | 3056 | synchronized (mLock) { |
Mohammad Samiul Islam | a0623e2 | 2020-04-24 09:26:19 +0100 | [diff] [blame] | 3057 | if (mDestroyed) return; // Do not allow destroyed staged session to change state |
Narayan Kamath | 9dfa674 | 2019-01-04 14:22:50 +0000 | [diff] [blame] | 3058 | mStagedSessionReady = false; |
| 3059 | mStagedSessionApplied = false; |
| 3060 | mStagedSessionFailed = true; |
| 3061 | mStagedSessionErrorCode = errorCode; |
Dario Freni | 275b4ab | 2019-01-25 09:55:16 +0000 | [diff] [blame] | 3062 | mStagedSessionErrorMessage = errorMessage; |
| 3063 | Slog.d(TAG, "Marking session " + sessionId + " as failed: " + errorMessage); |
Narayan Kamath | 9dfa674 | 2019-01-04 14:22:50 +0000 | [diff] [blame] | 3064 | } |
JW Wang | 8a207c4 | 2020-06-22 16:22:08 +0800 | [diff] [blame] | 3065 | cleanStageDirNotLocked(); |
Dario Freni | 0180d0b | 2019-01-11 21:08:13 +0000 | [diff] [blame] | 3066 | mCallback.onStagedSessionChanged(this); |
| 3067 | } |
| 3068 | |
| 3069 | /** {@hide} */ |
| 3070 | void setStagedSessionApplied() { |
| 3071 | synchronized (mLock) { |
Mohammad Samiul Islam | a0623e2 | 2020-04-24 09:26:19 +0100 | [diff] [blame] | 3072 | if (mDestroyed) return; // Do not allow destroyed staged session to change state |
Dario Freni | 0180d0b | 2019-01-11 21:08:13 +0000 | [diff] [blame] | 3073 | mStagedSessionReady = false; |
| 3074 | mStagedSessionApplied = true; |
| 3075 | mStagedSessionFailed = false; |
Dario Freni | b6d2896 | 2019-01-31 15:52:24 +0000 | [diff] [blame] | 3076 | mStagedSessionErrorCode = SessionInfo.STAGED_SESSION_NO_ERROR; |
Dario Freni | 275b4ab | 2019-01-25 09:55:16 +0000 | [diff] [blame] | 3077 | mStagedSessionErrorMessage = ""; |
Mohammad Samiul Islam | 5eacf09 | 2019-02-07 14:05:21 +0000 | [diff] [blame] | 3078 | Slog.d(TAG, "Marking session " + sessionId + " as applied"); |
Dario Freni | 0180d0b | 2019-01-11 21:08:13 +0000 | [diff] [blame] | 3079 | } |
JW Wang | 8a207c4 | 2020-06-22 16:22:08 +0800 | [diff] [blame] | 3080 | cleanStageDirNotLocked(); |
Dario Freni | 0180d0b | 2019-01-11 21:08:13 +0000 | [diff] [blame] | 3081 | mCallback.onStagedSessionChanged(this); |
| 3082 | } |
| 3083 | |
| 3084 | /** {@hide} */ |
| 3085 | boolean isStagedSessionReady() { |
| 3086 | return mStagedSessionReady; |
| 3087 | } |
| 3088 | |
| 3089 | /** {@hide} */ |
| 3090 | boolean isStagedSessionApplied() { |
| 3091 | return mStagedSessionApplied; |
| 3092 | } |
| 3093 | |
| 3094 | /** {@hide} */ |
| 3095 | boolean isStagedSessionFailed() { |
| 3096 | return mStagedSessionFailed; |
Narayan Kamath | 9dfa674 | 2019-01-04 14:22:50 +0000 | [diff] [blame] | 3097 | } |
| 3098 | |
Dario Freni | a6f1128 | 2019-01-21 12:16:04 +0000 | [diff] [blame] | 3099 | /** {@hide} */ |
| 3100 | @StagedSessionErrorCode int getStagedSessionErrorCode() { |
| 3101 | return mStagedSessionErrorCode; |
| 3102 | } |
| 3103 | |
Dario Freni | 275b4ab | 2019-01-25 09:55:16 +0000 | [diff] [blame] | 3104 | /** {@hide} */ |
| 3105 | String getStagedSessionErrorMessage() { |
| 3106 | return mStagedSessionErrorMessage; |
| 3107 | } |
| 3108 | |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 3109 | private void destroyInternal() { |
| 3110 | synchronized (mLock) { |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 3111 | mSealed = true; |
Gavin Corkery | d831121 | 2019-02-22 17:52:30 +0000 | [diff] [blame] | 3112 | if (!params.isStaged || isStagedAndInTerminalState()) { |
Dario Freni | 8e7d0ec | 2019-01-10 15:21:40 +0000 | [diff] [blame] | 3113 | mDestroyed = true; |
| 3114 | } |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 3115 | // Force shut down all bridges |
Jeff Sharkey | 02d4e34 | 2017-03-10 21:53:48 -0700 | [diff] [blame] | 3116 | for (RevocableFileDescriptor fd : mFds) { |
| 3117 | fd.revoke(); |
| 3118 | } |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 3119 | for (FileBridge bridge : mBridges) { |
| 3120 | bridge.forceClose(); |
| 3121 | } |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 3122 | } |
Alex Buynytskyy | c5682f5 | 2020-01-30 13:17:05 -0800 | [diff] [blame] | 3123 | if (mIncrementalFileStorages != null) { |
| 3124 | mIncrementalFileStorages.cleanUp(); |
| 3125 | mIncrementalFileStorages = null; |
| 3126 | } |
Dario Freni | a8f4b13 | 2018-12-30 00:36:49 +0000 | [diff] [blame] | 3127 | // For staged sessions, we don't delete the directory where the packages have been copied, |
shafik | 988f779 | 2019-02-26 12:15:57 +0000 | [diff] [blame] | 3128 | // since these packages are supposed to be read on reboot. |
| 3129 | // Those dirs are deleted when the staged session has reached a final state. |
Dario Freni | a8f4b13 | 2018-12-30 00:36:49 +0000 | [diff] [blame] | 3130 | if (stageDir != null && !params.isStaged) { |
Jeff Sharkey | fdeeeea | 2016-01-11 17:34:24 -0700 | [diff] [blame] | 3131 | try { |
| 3132 | mPm.mInstaller.rmPackageDir(stageDir.getAbsolutePath()); |
| 3133 | } catch (InstallerException ignored) { |
| 3134 | } |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 3135 | } |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 3136 | } |
| 3137 | |
JW Wang | 8a207c4 | 2020-06-22 16:22:08 +0800 | [diff] [blame] | 3138 | /** |
| 3139 | * <b>must not hold {@link #mLock}</b> |
| 3140 | */ |
| 3141 | private void cleanStageDirNotLocked() { |
| 3142 | if (Thread.holdsLock(mLock)) { |
| 3143 | Slog.wtf(TAG, "Calling thread " + Thread.currentThread().getName() |
| 3144 | + " is holding mLock", new Throwable()); |
| 3145 | } |
| 3146 | cleanStageDir(getChildSessionsNotLocked()); |
| 3147 | } |
| 3148 | |
| 3149 | private void cleanStageDir(List<PackageInstallerSession> childSessions) { |
| 3150 | if (childSessions != null) { |
| 3151 | for (PackageInstallerSession childSession : childSessions) { |
| 3152 | if (childSession != null) { |
| 3153 | childSession.cleanStageDir(); |
| 3154 | } |
Mohammad Samiul Islam | 5eacf09 | 2019-02-07 14:05:21 +0000 | [diff] [blame] | 3155 | } |
| 3156 | } else { |
JW Wang | 8a207c4 | 2020-06-22 16:22:08 +0800 | [diff] [blame] | 3157 | cleanStageDir(); |
| 3158 | } |
| 3159 | } |
| 3160 | |
| 3161 | private void cleanStageDir() { |
| 3162 | if (mIncrementalFileStorages != null) { |
| 3163 | mIncrementalFileStorages.cleanUp(); |
| 3164 | mIncrementalFileStorages = null; |
| 3165 | } |
| 3166 | try { |
| 3167 | mPm.mInstaller.rmPackageDir(stageDir.getAbsolutePath()); |
| 3168 | } catch (InstallerException ignored) { |
Mohammad Samiul Islam | 5eacf09 | 2019-02-07 14:05:21 +0000 | [diff] [blame] | 3169 | } |
| 3170 | } |
| 3171 | |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 3172 | void dump(IndentingPrintWriter pw) { |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 3173 | synchronized (mLock) { |
| 3174 | dumpLocked(pw); |
| 3175 | } |
| 3176 | } |
| 3177 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 3178 | @GuardedBy("mLock") |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 3179 | private void dumpLocked(IndentingPrintWriter pw) { |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 3180 | pw.println("Session " + sessionId + ":"); |
| 3181 | pw.increaseIndent(); |
| 3182 | |
| 3183 | pw.printPair("userId", userId); |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 3184 | pw.printPair("mOriginalInstallerUid", mOriginalInstallerUid); |
Alan Stokes | 5ed8537 | 2019-11-06 09:32:49 +0000 | [diff] [blame] | 3185 | pw.printPair("installerPackageName", mInstallSource.installerPackageName); |
| 3186 | pw.printPair("installInitiatingPackageName", mInstallSource.initiatingPackageName); |
| 3187 | pw.printPair("installOriginatingPackageName", mInstallSource.originatingPackageName); |
Alan Stokes | 819fea2 | 2019-10-16 16:54:09 +0100 | [diff] [blame] | 3188 | pw.printPair("mInstallerUid", mInstallerUid); |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 3189 | pw.printPair("createdMillis", createdMillis); |
Mohammad Samiul Islam | 52560d5 | 2019-08-16 12:07:31 +0100 | [diff] [blame] | 3190 | pw.printPair("updatedMillis", updatedMillis); |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 3191 | pw.printPair("stageDir", stageDir); |
| 3192 | pw.printPair("stageCid", stageCid); |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 3193 | pw.println(); |
| 3194 | |
| 3195 | params.dump(pw); |
| 3196 | |
| 3197 | pw.printPair("mClientProgress", mClientProgress); |
| 3198 | pw.printPair("mProgress", mProgress); |
Dario Freni | 47799f4 | 2019-03-13 18:06:24 +0000 | [diff] [blame] | 3199 | pw.printPair("mCommitted", mCommitted); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 3200 | pw.printPair("mSealed", mSealed); |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 3201 | pw.printPair("mPermissionsManuallyAccepted", mPermissionsManuallyAccepted); |
Jeff Sharkey | 497c052 | 2015-05-12 13:07:14 -0700 | [diff] [blame] | 3202 | pw.printPair("mRelinquished", mRelinquished); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 3203 | pw.printPair("mDestroyed", mDestroyed); |
Jeff Sharkey | 02d4e34 | 2017-03-10 21:53:48 -0700 | [diff] [blame] | 3204 | pw.printPair("mFds", mFds.size()); |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 3205 | pw.printPair("mBridges", mBridges.size()); |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 3206 | pw.printPair("mFinalStatus", mFinalStatus); |
| 3207 | pw.printPair("mFinalMessage", mFinalMessage); |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 3208 | pw.printPair("params.isMultiPackage", params.isMultiPackage); |
Dario Freni | aac4ba4 | 2018-12-06 15:47:16 +0000 | [diff] [blame] | 3209 | pw.printPair("params.isStaged", params.isStaged); |
Mohammad Samiul Islam | 52560d5 | 2019-08-16 12:07:31 +0100 | [diff] [blame] | 3210 | pw.printPair("mParentSessionId", mParentSessionId); |
| 3211 | pw.printPair("mChildSessionIds", mChildSessionIds); |
| 3212 | pw.printPair("mStagedSessionApplied", mStagedSessionApplied); |
| 3213 | pw.printPair("mStagedSessionFailed", mStagedSessionFailed); |
| 3214 | pw.printPair("mStagedSessionReady", mStagedSessionReady); |
| 3215 | pw.printPair("mStagedSessionErrorCode", mStagedSessionErrorCode); |
| 3216 | pw.printPair("mStagedSessionErrorMessage", mStagedSessionErrorMessage); |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 3217 | pw.println(); |
| 3218 | |
| 3219 | pw.decreaseIndent(); |
| 3220 | } |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 3221 | |
Mohammad Samiul Islam | 10323a1 | 2020-03-17 10:04:43 +0000 | [diff] [blame] | 3222 | private static void sendOnUserActionRequired(Context context, IntentSender target, |
| 3223 | int sessionId, Intent intent) { |
| 3224 | final Intent fillIn = new Intent(); |
| 3225 | fillIn.putExtra(PackageInstaller.EXTRA_SESSION_ID, sessionId); |
| 3226 | fillIn.putExtra(PackageInstaller.EXTRA_STATUS, PackageInstaller.STATUS_PENDING_USER_ACTION); |
| 3227 | fillIn.putExtra(Intent.EXTRA_INTENT, intent); |
| 3228 | try { |
| 3229 | target.sendIntent(context, 0, fillIn, null, null); |
| 3230 | } catch (IntentSender.SendIntentException ignored) { |
| 3231 | } |
| 3232 | } |
| 3233 | |
| 3234 | private static void sendOnPackageInstalled(Context context, IntentSender target, int sessionId, |
| 3235 | boolean showNotification, int userId, String basePackageName, int returnCode, |
| 3236 | String msg, Bundle extras) { |
| 3237 | if (PackageManager.INSTALL_SUCCEEDED == returnCode && showNotification) { |
| 3238 | boolean update = (extras != null) && extras.getBoolean(Intent.EXTRA_REPLACING); |
| 3239 | Notification notification = PackageInstallerService.buildSuccessNotification(context, |
| 3240 | context.getResources() |
| 3241 | .getString(update ? R.string.package_updated_device_owner : |
| 3242 | R.string.package_installed_device_owner), |
| 3243 | basePackageName, |
| 3244 | userId); |
| 3245 | if (notification != null) { |
| 3246 | NotificationManager notificationManager = (NotificationManager) |
| 3247 | context.getSystemService(Context.NOTIFICATION_SERVICE); |
| 3248 | notificationManager.notify(basePackageName, |
| 3249 | SystemMessageProto.SystemMessage.NOTE_PACKAGE_STATE, |
| 3250 | notification); |
| 3251 | } |
| 3252 | } |
| 3253 | final Intent fillIn = new Intent(); |
| 3254 | fillIn.putExtra(PackageInstaller.EXTRA_PACKAGE_NAME, basePackageName); |
| 3255 | fillIn.putExtra(PackageInstaller.EXTRA_SESSION_ID, sessionId); |
| 3256 | fillIn.putExtra(PackageInstaller.EXTRA_STATUS, |
| 3257 | PackageManager.installStatusToPublicStatus(returnCode)); |
| 3258 | fillIn.putExtra(PackageInstaller.EXTRA_STATUS_MESSAGE, |
| 3259 | PackageManager.installStatusToString(returnCode, msg)); |
| 3260 | fillIn.putExtra(PackageInstaller.EXTRA_LEGACY_STATUS, returnCode); |
| 3261 | if (extras != null) { |
| 3262 | final String existing = extras.getString( |
| 3263 | PackageManager.EXTRA_FAILURE_EXISTING_PACKAGE); |
| 3264 | if (!TextUtils.isEmpty(existing)) { |
| 3265 | fillIn.putExtra(PackageInstaller.EXTRA_OTHER_PACKAGE_NAME, existing); |
| 3266 | } |
| 3267 | } |
| 3268 | try { |
| 3269 | target.sendIntent(context, 0, fillIn, null, null); |
| 3270 | } catch (IntentSender.SendIntentException ignored) { |
| 3271 | } |
| 3272 | } |
| 3273 | |
Alex Buynytskyy | 221cd08 | 2020-05-19 22:18:18 -0700 | [diff] [blame] | 3274 | private void sendPendingStreaming(@Nullable String cause) { |
| 3275 | final IntentSender statusReceiver; |
| 3276 | synchronized (mLock) { |
| 3277 | statusReceiver = mRemoteStatusReceiver; |
| 3278 | } |
| 3279 | |
| 3280 | if (statusReceiver == null) { |
| 3281 | Slog.e(TAG, "Missing receiver for pending streaming status."); |
| 3282 | return; |
| 3283 | } |
| 3284 | |
Mohammad Samiul Islam | 10323a1 | 2020-03-17 10:04:43 +0000 | [diff] [blame] | 3285 | final Intent intent = new Intent(); |
| 3286 | intent.putExtra(PackageInstaller.EXTRA_SESSION_ID, sessionId); |
| 3287 | intent.putExtra(PackageInstaller.EXTRA_STATUS, PackageInstaller.STATUS_PENDING_STREAMING); |
Alex Buynytskyy | 7e0a1a8 | 2020-04-27 17:06:10 -0700 | [diff] [blame] | 3288 | if (!TextUtils.isEmpty(cause)) { |
Mohammad Samiul Islam | 10323a1 | 2020-03-17 10:04:43 +0000 | [diff] [blame] | 3289 | intent.putExtra(PackageInstaller.EXTRA_STATUS_MESSAGE, |
Alex Buynytskyy | 7e0a1a8 | 2020-04-27 17:06:10 -0700 | [diff] [blame] | 3290 | "Staging Image Not Ready [" + cause + "]"); |
Mohammad Samiul Islam | 10323a1 | 2020-03-17 10:04:43 +0000 | [diff] [blame] | 3291 | } else { |
| 3292 | intent.putExtra(PackageInstaller.EXTRA_STATUS_MESSAGE, "Staging Image Not Ready"); |
| 3293 | } |
| 3294 | try { |
Alex Buynytskyy | 221cd08 | 2020-05-19 22:18:18 -0700 | [diff] [blame] | 3295 | statusReceiver.sendIntent(mContext, 0, intent, null, null); |
Mohammad Samiul Islam | 10323a1 | 2020-03-17 10:04:43 +0000 | [diff] [blame] | 3296 | } catch (IntentSender.SendIntentException ignored) { |
| 3297 | } |
| 3298 | } |
| 3299 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 3300 | private static void writeGrantedRuntimePermissionsLocked(XmlSerializer out, |
| 3301 | String[] grantedRuntimePermissions) throws IOException { |
| 3302 | if (grantedRuntimePermissions != null) { |
| 3303 | for (String permission : grantedRuntimePermissions) { |
| 3304 | out.startTag(null, TAG_GRANTED_RUNTIME_PERMISSION); |
| 3305 | writeStringAttribute(out, ATTR_NAME, permission); |
| 3306 | out.endTag(null, TAG_GRANTED_RUNTIME_PERMISSION); |
| 3307 | } |
| 3308 | } |
| 3309 | } |
| 3310 | |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 3311 | private static void writeWhitelistedRestrictedPermissionsLocked(@NonNull XmlSerializer out, |
| 3312 | @Nullable List<String> whitelistedRestrictedPermissions) throws IOException { |
| 3313 | if (whitelistedRestrictedPermissions != null) { |
| 3314 | final int permissionCount = whitelistedRestrictedPermissions.size(); |
| 3315 | for (int i = 0; i < permissionCount; i++) { |
| 3316 | out.startTag(null, TAG_WHITELISTED_RESTRICTED_PERMISSION); |
| 3317 | writeStringAttribute(out, ATTR_NAME, whitelistedRestrictedPermissions.get(i)); |
| 3318 | out.endTag(null, TAG_WHITELISTED_RESTRICTED_PERMISSION); |
| 3319 | } |
| 3320 | } |
| 3321 | } |
| 3322 | |
Eugene Susla | 922cd08 | 2020-03-11 12:38:17 -0700 | [diff] [blame] | 3323 | private static void writeAutoRevokePermissionsMode(@NonNull XmlSerializer out, int mode) |
| 3324 | throws IOException { |
| 3325 | out.startTag(null, TAG_AUTO_REVOKE_PERMISSIONS_MODE); |
| 3326 | writeIntAttribute(out, ATTR_MODE, mode); |
| 3327 | out.endTag(null, TAG_AUTO_REVOKE_PERMISSIONS_MODE); |
| 3328 | } |
| 3329 | |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 3330 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 3331 | private static File buildAppIconFile(int sessionId, @NonNull File sessionsDir) { |
| 3332 | return new File(sessionsDir, "app_icon." + sessionId + ".png"); |
| 3333 | } |
| 3334 | |
| 3335 | /** |
| 3336 | * Write this session to a {@link XmlSerializer}. |
| 3337 | * |
| 3338 | * @param out Where to write the session to |
| 3339 | * @param sessionsDir The directory containing the sessions |
| 3340 | */ |
| 3341 | void write(@NonNull XmlSerializer out, @NonNull File sessionsDir) throws IOException { |
| 3342 | synchronized (mLock) { |
Mohammad Samiul Islam | 731bd96 | 2020-04-23 16:23:21 +0100 | [diff] [blame] | 3343 | if (mDestroyed && !params.isStaged) { |
Philip P. Moltmann | 37dd1ba | 2017-09-08 09:46:22 -0700 | [diff] [blame] | 3344 | return; |
| 3345 | } |
| 3346 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 3347 | out.startTag(null, TAG_SESSION); |
| 3348 | |
| 3349 | writeIntAttribute(out, ATTR_SESSION_ID, sessionId); |
| 3350 | writeIntAttribute(out, ATTR_USER_ID, userId); |
| 3351 | writeStringAttribute(out, ATTR_INSTALLER_PACKAGE_NAME, |
Alan Stokes | 819fea2 | 2019-10-16 16:54:09 +0100 | [diff] [blame] | 3352 | mInstallSource.installerPackageName); |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 3353 | writeIntAttribute(out, ATTR_INSTALLER_UID, mInstallerUid); |
Alan Stokes | 69d2abf | 2019-10-10 11:02:38 +0100 | [diff] [blame] | 3354 | writeStringAttribute(out, ATTR_INITIATING_PACKAGE_NAME, |
| 3355 | mInstallSource.initiatingPackageName); |
Alan Stokes | 5ed8537 | 2019-11-06 09:32:49 +0000 | [diff] [blame] | 3356 | writeStringAttribute(out, ATTR_ORIGINATING_PACKAGE_NAME, |
| 3357 | mInstallSource.originatingPackageName); |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 3358 | writeLongAttribute(out, ATTR_CREATED_MILLIS, createdMillis); |
Gavin Corkery | d831121 | 2019-02-22 17:52:30 +0000 | [diff] [blame] | 3359 | writeLongAttribute(out, ATTR_UPDATED_MILLIS, updatedMillis); |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 3360 | if (stageDir != null) { |
| 3361 | writeStringAttribute(out, ATTR_SESSION_STAGE_DIR, |
| 3362 | stageDir.getAbsolutePath()); |
| 3363 | } |
| 3364 | if (stageCid != null) { |
| 3365 | writeStringAttribute(out, ATTR_SESSION_STAGE_CID, stageCid); |
| 3366 | } |
| 3367 | writeBooleanAttribute(out, ATTR_PREPARED, isPrepared()); |
Dario Freni | 47799f4 | 2019-03-13 18:06:24 +0000 | [diff] [blame] | 3368 | writeBooleanAttribute(out, ATTR_COMMITTED, isCommitted()); |
Mohammad Samiul Islam | 731bd96 | 2020-04-23 16:23:21 +0100 | [diff] [blame] | 3369 | writeBooleanAttribute(out, ATTR_DESTROYED, isDestroyed()); |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 3370 | writeBooleanAttribute(out, ATTR_SEALED, isSealed()); |
| 3371 | |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 3372 | writeBooleanAttribute(out, ATTR_MULTI_PACKAGE, params.isMultiPackage); |
Dario Freni | aac4ba4 | 2018-12-06 15:47:16 +0000 | [diff] [blame] | 3373 | writeBooleanAttribute(out, ATTR_STAGED_SESSION, params.isStaged); |
Dario Freni | 8e7d0ec | 2019-01-10 15:21:40 +0000 | [diff] [blame] | 3374 | writeBooleanAttribute(out, ATTR_IS_READY, mStagedSessionReady); |
| 3375 | writeBooleanAttribute(out, ATTR_IS_FAILED, mStagedSessionFailed); |
| 3376 | writeBooleanAttribute(out, ATTR_IS_APPLIED, mStagedSessionApplied); |
| 3377 | writeIntAttribute(out, ATTR_STAGED_SESSION_ERROR_CODE, mStagedSessionErrorCode); |
Dario Freni | 275b4ab | 2019-01-25 09:55:16 +0000 | [diff] [blame] | 3378 | writeStringAttribute(out, ATTR_STAGED_SESSION_ERROR_MESSAGE, |
| 3379 | mStagedSessionErrorMessage); |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 3380 | // TODO(patb,109941548): avoid writing to xml and instead infer / validate this after |
| 3381 | // we've read all sessions. |
| 3382 | writeIntAttribute(out, ATTR_PARENT_SESSION_ID, mParentSessionId); |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 3383 | writeIntAttribute(out, ATTR_MODE, params.mode); |
| 3384 | writeIntAttribute(out, ATTR_INSTALL_FLAGS, params.installFlags); |
| 3385 | writeIntAttribute(out, ATTR_INSTALL_LOCATION, params.installLocation); |
| 3386 | writeLongAttribute(out, ATTR_SIZE_BYTES, params.sizeBytes); |
| 3387 | writeStringAttribute(out, ATTR_APP_PACKAGE_NAME, params.appPackageName); |
| 3388 | writeStringAttribute(out, ATTR_APP_LABEL, params.appLabel); |
| 3389 | writeUriAttribute(out, ATTR_ORIGINATING_URI, params.originatingUri); |
| 3390 | writeIntAttribute(out, ATTR_ORIGINATING_UID, params.originatingUid); |
| 3391 | writeUriAttribute(out, ATTR_REFERRER_URI, params.referrerUri); |
| 3392 | writeStringAttribute(out, ATTR_ABI_OVERRIDE, params.abiOverride); |
| 3393 | writeStringAttribute(out, ATTR_VOLUME_UUID, params.volumeUuid); |
| 3394 | writeIntAttribute(out, ATTR_INSTALL_REASON, params.installReason); |
| 3395 | |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 3396 | final boolean isDataLoader = params.dataLoaderParams != null; |
| 3397 | writeBooleanAttribute(out, ATTR_IS_DATALOADER, isDataLoader); |
| 3398 | if (isDataLoader) { |
| 3399 | writeIntAttribute(out, ATTR_DATALOADER_TYPE, params.dataLoaderParams.getType()); |
| 3400 | writeStringAttribute(out, ATTR_DATALOADER_PACKAGE_NAME, |
| 3401 | params.dataLoaderParams.getComponentName().getPackageName()); |
| 3402 | writeStringAttribute(out, ATTR_DATALOADER_CLASS_NAME, |
| 3403 | params.dataLoaderParams.getComponentName().getClassName()); |
| 3404 | writeStringAttribute(out, ATTR_DATALOADER_ARGUMENTS, |
| 3405 | params.dataLoaderParams.getArguments()); |
| 3406 | } |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 3407 | |
Philip P. Moltmann | 656a2a0 | 2018-03-01 09:14:44 -0800 | [diff] [blame] | 3408 | writeGrantedRuntimePermissionsLocked(out, params.grantedRuntimePermissions); |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 3409 | writeWhitelistedRestrictedPermissionsLocked(out, |
| 3410 | params.whitelistedRestrictedPermissions); |
Eugene Susla | 922cd08 | 2020-03-11 12:38:17 -0700 | [diff] [blame] | 3411 | writeAutoRevokePermissionsMode(out, params.autoRevokePermissionsMode); |
Philip P. Moltmann | 656a2a0 | 2018-03-01 09:14:44 -0800 | [diff] [blame] | 3412 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 3413 | // Persist app icon if changed since last written |
| 3414 | File appIconFile = buildAppIconFile(sessionId, sessionsDir); |
| 3415 | if (params.appIcon == null && appIconFile.exists()) { |
| 3416 | appIconFile.delete(); |
| 3417 | } else if (params.appIcon != null |
| 3418 | && appIconFile.lastModified() != params.appIconLastModified) { |
| 3419 | if (LOGD) Slog.w(TAG, "Writing changed icon " + appIconFile); |
| 3420 | FileOutputStream os = null; |
| 3421 | try { |
| 3422 | os = new FileOutputStream(appIconFile); |
| 3423 | params.appIcon.compress(Bitmap.CompressFormat.PNG, 90, os); |
| 3424 | } catch (IOException e) { |
| 3425 | Slog.w(TAG, "Failed to write icon " + appIconFile + ": " + e.getMessage()); |
| 3426 | } finally { |
| 3427 | IoUtils.closeQuietly(os); |
| 3428 | } |
| 3429 | |
| 3430 | params.appIconLastModified = appIconFile.lastModified(); |
| 3431 | } |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 3432 | final int[] childSessionIds = getChildSessionIds(); |
| 3433 | for (int childSessionId : childSessionIds) { |
| 3434 | out.startTag(null, TAG_CHILD_SESSION); |
| 3435 | writeIntAttribute(out, ATTR_SESSION_ID, childSessionId); |
| 3436 | out.endTag(null, TAG_CHILD_SESSION); |
| 3437 | } |
Alex Buynytskyy | c282de9 | 2020-03-10 10:27:42 -0700 | [diff] [blame] | 3438 | |
| 3439 | final InstallationFile[] files = getInstallationFilesLocked(); |
| 3440 | for (InstallationFile file : getInstallationFilesLocked()) { |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 3441 | out.startTag(null, TAG_SESSION_FILE); |
Alex Buynytskyy | cd4d387 | 2020-02-08 17:50:50 -0800 | [diff] [blame] | 3442 | writeIntAttribute(out, ATTR_LOCATION, file.getLocation()); |
| 3443 | writeStringAttribute(out, ATTR_NAME, file.getName()); |
| 3444 | writeLongAttribute(out, ATTR_LENGTH_BYTES, file.getLengthBytes()); |
| 3445 | writeByteArrayAttribute(out, ATTR_METADATA, file.getMetadata()); |
| 3446 | writeByteArrayAttribute(out, ATTR_SIGNATURE, file.getSignature()); |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 3447 | out.endTag(null, TAG_SESSION_FILE); |
| 3448 | } |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 3449 | } |
| 3450 | |
| 3451 | out.endTag(null, TAG_SESSION); |
| 3452 | } |
| 3453 | |
Dario Freni | 0180d0b | 2019-01-11 21:08:13 +0000 | [diff] [blame] | 3454 | // Sanity check to be performed when the session is restored from an external file. Only one |
| 3455 | // of the session states should be true, or none of them. |
| 3456 | private static boolean isStagedSessionStateValid(boolean isReady, boolean isApplied, |
| 3457 | boolean isFailed) { |
| 3458 | return (!isReady && !isApplied && !isFailed) |
| 3459 | || (isReady && !isApplied && !isFailed) |
| 3460 | || (!isReady && isApplied && !isFailed) |
| 3461 | || (!isReady && !isApplied && isFailed); |
| 3462 | } |
| 3463 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 3464 | /** |
| 3465 | * Read new session from a {@link XmlPullParser xml description} and create it. |
| 3466 | * |
| 3467 | * @param in The source of the description |
| 3468 | * @param callback Callback the session uses to notify about changes of it's state |
| 3469 | * @param context Context to be used by the session |
| 3470 | * @param pm PackageManager to use by the session |
| 3471 | * @param installerThread Thread to be used for callbacks of this session |
| 3472 | * @param sessionsDir The directory the sessions are stored in |
| 3473 | * |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 3474 | * @param sessionProvider |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 3475 | * @return The newly created session |
| 3476 | */ |
| 3477 | public static PackageInstallerSession readFromXml(@NonNull XmlPullParser in, |
| 3478 | @NonNull PackageInstallerService.InternalCallback callback, @NonNull Context context, |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 3479 | @NonNull PackageManagerService pm, Looper installerThread, |
| 3480 | @NonNull StagingManager stagingManager, @NonNull File sessionsDir, |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 3481 | @NonNull PackageSessionProvider sessionProvider) |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 3482 | throws IOException, XmlPullParserException { |
| 3483 | final int sessionId = readIntAttribute(in, ATTR_SESSION_ID); |
| 3484 | final int userId = readIntAttribute(in, ATTR_USER_ID); |
| 3485 | final String installerPackageName = readStringAttribute(in, ATTR_INSTALLER_PACKAGE_NAME); |
| 3486 | final int installerUid = readIntAttribute(in, ATTR_INSTALLER_UID, pm.getPackageUid( |
| 3487 | installerPackageName, PackageManager.MATCH_UNINSTALLED_PACKAGES, userId)); |
Alan Stokes | 69d2abf | 2019-10-10 11:02:38 +0100 | [diff] [blame] | 3488 | final String installInitiatingPackageName = |
| 3489 | readStringAttribute(in, ATTR_INITIATING_PACKAGE_NAME); |
Alan Stokes | 5ed8537 | 2019-11-06 09:32:49 +0000 | [diff] [blame] | 3490 | final String installOriginatingPackageName = |
| 3491 | readStringAttribute(in, ATTR_ORIGINATING_PACKAGE_NAME); |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 3492 | final long createdMillis = readLongAttribute(in, ATTR_CREATED_MILLIS); |
Gavin Corkery | d831121 | 2019-02-22 17:52:30 +0000 | [diff] [blame] | 3493 | long updatedMillis = readLongAttribute(in, ATTR_UPDATED_MILLIS); |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 3494 | final String stageDirRaw = readStringAttribute(in, ATTR_SESSION_STAGE_DIR); |
| 3495 | final File stageDir = (stageDirRaw != null) ? new File(stageDirRaw) : null; |
| 3496 | final String stageCid = readStringAttribute(in, ATTR_SESSION_STAGE_CID); |
| 3497 | final boolean prepared = readBooleanAttribute(in, ATTR_PREPARED, true); |
Dario Freni | 47799f4 | 2019-03-13 18:06:24 +0000 | [diff] [blame] | 3498 | final boolean committed = readBooleanAttribute(in, ATTR_COMMITTED); |
Mohammad Samiul Islam | 731bd96 | 2020-04-23 16:23:21 +0100 | [diff] [blame] | 3499 | final boolean destroyed = readBooleanAttribute(in, ATTR_DESTROYED); |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 3500 | final boolean sealed = readBooleanAttribute(in, ATTR_SEALED); |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 3501 | final int parentSessionId = readIntAttribute(in, ATTR_PARENT_SESSION_ID, |
| 3502 | SessionInfo.INVALID_ID); |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 3503 | |
| 3504 | final SessionParams params = new SessionParams( |
| 3505 | SessionParams.MODE_INVALID); |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 3506 | params.isMultiPackage = readBooleanAttribute(in, ATTR_MULTI_PACKAGE, false); |
Dario Freni | aac4ba4 | 2018-12-06 15:47:16 +0000 | [diff] [blame] | 3507 | params.isStaged = readBooleanAttribute(in, ATTR_STAGED_SESSION, false); |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 3508 | params.mode = readIntAttribute(in, ATTR_MODE); |
| 3509 | params.installFlags = readIntAttribute(in, ATTR_INSTALL_FLAGS); |
| 3510 | params.installLocation = readIntAttribute(in, ATTR_INSTALL_LOCATION); |
| 3511 | params.sizeBytes = readLongAttribute(in, ATTR_SIZE_BYTES); |
| 3512 | params.appPackageName = readStringAttribute(in, ATTR_APP_PACKAGE_NAME); |
| 3513 | params.appIcon = readBitmapAttribute(in, ATTR_APP_ICON); |
| 3514 | params.appLabel = readStringAttribute(in, ATTR_APP_LABEL); |
| 3515 | params.originatingUri = readUriAttribute(in, ATTR_ORIGINATING_URI); |
| 3516 | params.originatingUid = |
| 3517 | readIntAttribute(in, ATTR_ORIGINATING_UID, SessionParams.UID_UNKNOWN); |
| 3518 | params.referrerUri = readUriAttribute(in, ATTR_REFERRER_URI); |
| 3519 | params.abiOverride = readStringAttribute(in, ATTR_ABI_OVERRIDE); |
| 3520 | params.volumeUuid = readStringAttribute(in, ATTR_VOLUME_UUID); |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 3521 | params.installReason = readIntAttribute(in, ATTR_INSTALL_REASON); |
| 3522 | |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 3523 | if (readBooleanAttribute(in, ATTR_IS_DATALOADER)) { |
| 3524 | params.dataLoaderParams = new DataLoaderParams( |
| 3525 | readIntAttribute(in, ATTR_DATALOADER_TYPE), |
| 3526 | new ComponentName( |
| 3527 | readStringAttribute(in, ATTR_DATALOADER_PACKAGE_NAME), |
| 3528 | readStringAttribute(in, ATTR_DATALOADER_CLASS_NAME)), |
Alex Buynytskyy | 686a537 | 2020-03-26 15:00:15 -0700 | [diff] [blame] | 3529 | readStringAttribute(in, ATTR_DATALOADER_ARGUMENTS)); |
Alex Buynytskyy | 1ecfcec | 2019-12-17 12:10:41 -0800 | [diff] [blame] | 3530 | } |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 3531 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 3532 | final File appIconFile = buildAppIconFile(sessionId, sessionsDir); |
| 3533 | if (appIconFile.exists()) { |
| 3534 | params.appIcon = BitmapFactory.decodeFile(appIconFile.getAbsolutePath()); |
| 3535 | params.appIconLastModified = appIconFile.lastModified(); |
| 3536 | } |
Dario Freni | 8e7d0ec | 2019-01-10 15:21:40 +0000 | [diff] [blame] | 3537 | final boolean isReady = readBooleanAttribute(in, ATTR_IS_READY); |
| 3538 | final boolean isFailed = readBooleanAttribute(in, ATTR_IS_FAILED); |
| 3539 | final boolean isApplied = readBooleanAttribute(in, ATTR_IS_APPLIED); |
Dario Freni | a6f1128 | 2019-01-21 12:16:04 +0000 | [diff] [blame] | 3540 | final int stagedSessionErrorCode = readIntAttribute(in, ATTR_STAGED_SESSION_ERROR_CODE, |
Dario Freni | b6d2896 | 2019-01-31 15:52:24 +0000 | [diff] [blame] | 3541 | SessionInfo.STAGED_SESSION_NO_ERROR); |
Dario Freni | 275b4ab | 2019-01-25 09:55:16 +0000 | [diff] [blame] | 3542 | final String stagedSessionErrorMessage = readStringAttribute(in, |
| 3543 | ATTR_STAGED_SESSION_ERROR_MESSAGE); |
Dario Freni | 8e7d0ec | 2019-01-10 15:21:40 +0000 | [diff] [blame] | 3544 | |
Dario Freni | 0180d0b | 2019-01-11 21:08:13 +0000 | [diff] [blame] | 3545 | if (!isStagedSessionStateValid(isReady, isApplied, isFailed)) { |
| 3546 | throw new IllegalArgumentException("Can't restore staged session with invalid state."); |
| 3547 | } |
| 3548 | |
Dario Freni | a6f1128 | 2019-01-21 12:16:04 +0000 | [diff] [blame] | 3549 | // Parse sub tags of this session, typically used for repeated values / arrays. |
| 3550 | // Sub tags can come in any order, therefore we need to keep track of what we find while |
| 3551 | // parsing and only set the right values at the end. |
| 3552 | |
| 3553 | // Store the current depth. We should stop parsing when we reach an end tag at the same |
| 3554 | // depth. |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 3555 | List<String> grantedRuntimePermissions = new ArrayList<>(); |
| 3556 | List<String> whitelistedRestrictedPermissions = new ArrayList<>(); |
Eugene Susla | 922cd08 | 2020-03-11 12:38:17 -0700 | [diff] [blame] | 3557 | int autoRevokePermissionsMode = MODE_DEFAULT; |
Dario Freni | a6f1128 | 2019-01-21 12:16:04 +0000 | [diff] [blame] | 3558 | List<Integer> childSessionIds = new ArrayList<>(); |
Alex Buynytskyy | cd4d387 | 2020-02-08 17:50:50 -0800 | [diff] [blame] | 3559 | List<InstallationFile> files = new ArrayList<>(); |
Dario Freni | a6f1128 | 2019-01-21 12:16:04 +0000 | [diff] [blame] | 3560 | int outerDepth = in.getDepth(); |
| 3561 | int type; |
| 3562 | while ((type = in.next()) != XmlPullParser.END_DOCUMENT |
| 3563 | && (type != XmlPullParser.END_TAG || in.getDepth() > outerDepth)) { |
| 3564 | if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { |
| 3565 | continue; |
| 3566 | } |
| 3567 | if (TAG_GRANTED_RUNTIME_PERMISSION.equals(in.getName())) { |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 3568 | grantedRuntimePermissions.add(readStringAttribute(in, ATTR_NAME)); |
| 3569 | } |
| 3570 | if (TAG_WHITELISTED_RESTRICTED_PERMISSION.equals(in.getName())) { |
| 3571 | whitelistedRestrictedPermissions.add(readStringAttribute(in, ATTR_NAME)); |
| 3572 | |
Dario Freni | a6f1128 | 2019-01-21 12:16:04 +0000 | [diff] [blame] | 3573 | } |
Eugene Susla | 922cd08 | 2020-03-11 12:38:17 -0700 | [diff] [blame] | 3574 | if (TAG_AUTO_REVOKE_PERMISSIONS_MODE.equals(in.getName())) { |
| 3575 | autoRevokePermissionsMode = readIntAttribute(in, ATTR_MODE); |
| 3576 | } |
Dario Freni | a6f1128 | 2019-01-21 12:16:04 +0000 | [diff] [blame] | 3577 | if (TAG_CHILD_SESSION.equals(in.getName())) { |
| 3578 | childSessionIds.add(readIntAttribute(in, ATTR_SESSION_ID, SessionInfo.INVALID_ID)); |
| 3579 | } |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 3580 | if (TAG_SESSION_FILE.equals(in.getName())) { |
Alex Buynytskyy | cd4d387 | 2020-02-08 17:50:50 -0800 | [diff] [blame] | 3581 | files.add(new InstallationFile( |
Alex Buynytskyy | e069787 | 2020-01-13 09:25:21 -0800 | [diff] [blame] | 3582 | readIntAttribute(in, ATTR_LOCATION, 0), |
| 3583 | readStringAttribute(in, ATTR_NAME), |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 3584 | readLongAttribute(in, ATTR_LENGTH_BYTES, -1), |
Alex Buynytskyy | e069787 | 2020-01-13 09:25:21 -0800 | [diff] [blame] | 3585 | readByteArrayAttribute(in, ATTR_METADATA), |
| 3586 | readByteArrayAttribute(in, ATTR_SIGNATURE))); |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 3587 | } |
Dario Freni | a6f1128 | 2019-01-21 12:16:04 +0000 | [diff] [blame] | 3588 | } |
| 3589 | |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 3590 | if (grantedRuntimePermissions.size() > 0) { |
Alan Stokes | ee59d54 | 2020-02-04 17:35:15 +0000 | [diff] [blame] | 3591 | params.grantedRuntimePermissions = |
| 3592 | grantedRuntimePermissions.toArray(EmptyArray.STRING); |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 3593 | } |
| 3594 | |
| 3595 | if (whitelistedRestrictedPermissions.size() > 0) { |
| 3596 | params.whitelistedRestrictedPermissions = whitelistedRestrictedPermissions; |
Dario Freni | a6f1128 | 2019-01-21 12:16:04 +0000 | [diff] [blame] | 3597 | } |
| 3598 | |
Eugene Susla | 922cd08 | 2020-03-11 12:38:17 -0700 | [diff] [blame] | 3599 | params.autoRevokePermissionsMode = autoRevokePermissionsMode; |
| 3600 | |
Dario Freni | a6f1128 | 2019-01-21 12:16:04 +0000 | [diff] [blame] | 3601 | int[] childSessionIdsArray; |
| 3602 | if (childSessionIds.size() > 0) { |
Alex Buynytskyy | c0c5a37 | 2020-01-29 18:59:20 -0800 | [diff] [blame] | 3603 | childSessionIdsArray = new int[childSessionIds.size()]; |
| 3604 | for (int i = 0, size = childSessionIds.size(); i < size; ++i) { |
| 3605 | childSessionIdsArray[i] = childSessionIds.get(i); |
| 3606 | } |
Dario Freni | a6f1128 | 2019-01-21 12:16:04 +0000 | [diff] [blame] | 3607 | } else { |
| 3608 | childSessionIdsArray = EMPTY_CHILD_SESSION_ARRAY; |
| 3609 | } |
| 3610 | |
Alex Buynytskyy | cd4d387 | 2020-02-08 17:50:50 -0800 | [diff] [blame] | 3611 | InstallationFile[] fileArray = null; |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 3612 | if (!files.isEmpty()) { |
Alex Buynytskyy | cd4d387 | 2020-02-08 17:50:50 -0800 | [diff] [blame] | 3613 | fileArray = files.toArray(EMPTY_INSTALLATION_FILE_ARRAY); |
Alex Buynytskyy | da20815 | 2019-11-11 09:34:05 -0800 | [diff] [blame] | 3614 | } |
| 3615 | |
Alan Stokes | 819fea2 | 2019-10-16 16:54:09 +0100 | [diff] [blame] | 3616 | InstallSource installSource = InstallSource.create(installInitiatingPackageName, |
Alan Stokes | 72b7e67 | 2020-01-07 15:46:49 +0000 | [diff] [blame] | 3617 | installOriginatingPackageName, installerPackageName); |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 3618 | return new PackageInstallerSession(callback, context, pm, sessionProvider, |
Alan Stokes | 819fea2 | 2019-10-16 16:54:09 +0100 | [diff] [blame] | 3619 | installerThread, stagingManager, sessionId, userId, installerUid, |
Alex Buynytskyy | cd4d387 | 2020-02-08 17:50:50 -0800 | [diff] [blame] | 3620 | installSource, params, createdMillis, stageDir, stageCid, fileArray, |
Mohammad Samiul Islam | 731bd96 | 2020-04-23 16:23:21 +0100 | [diff] [blame] | 3621 | prepared, committed, destroyed, sealed, childSessionIdsArray, parentSessionId, |
Alan Stokes | 69d2abf | 2019-10-10 11:02:38 +0100 | [diff] [blame] | 3622 | isReady, isFailed, isApplied, stagedSessionErrorCode, stagedSessionErrorMessage); |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 3623 | } |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 3624 | } |