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