Philip Cuadra | 7bd0fdd | 2016-04-28 15:26:49 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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; |
| 18 | |
Jorim Jaggi | 7119800f | 2018-07-09 17:57:10 +0200 | [diff] [blame] | 19 | import static android.app.ActivityManager.UID_OBSERVER_ACTIVE; |
| 20 | import static android.app.ActivityManager.UID_OBSERVER_GONE; |
| 21 | |
| 22 | import android.annotation.IntDef; |
Daniel Colascione | 9779b5e | 2018-03-21 19:13:57 -0700 | [diff] [blame] | 23 | import android.annotation.Nullable; |
Jorim Jaggi | 7119800f | 2018-07-09 17:57:10 +0200 | [diff] [blame] | 24 | import android.app.ActivityManager; |
| 25 | import android.app.ActivityManagerInternal; |
| 26 | import android.app.IActivityManager; |
| 27 | import android.app.IUidObserver; |
Valentin Iftime | 6a652e1 | 2019-07-24 16:51:38 +0200 | [diff] [blame] | 28 | import android.app.SearchManager; |
Carmen Jackson | f107a23 | 2017-05-16 10:37:26 -0700 | [diff] [blame] | 29 | import android.content.BroadcastReceiver; |
Philip Cuadra | 7bd0fdd | 2016-04-28 15:26:49 -0700 | [diff] [blame] | 30 | import android.content.Context; |
Jeff Sharkey | 847bd85 | 2016-08-03 17:20:03 -0600 | [diff] [blame] | 31 | import android.content.Intent; |
Carmen Jackson | f107a23 | 2017-05-16 10:37:26 -0700 | [diff] [blame] | 32 | import android.content.IntentFilter; |
Philip Cuadra | 7cb2f8b | 2016-06-15 16:23:43 -0700 | [diff] [blame] | 33 | import android.content.pm.ActivityInfo; |
| 34 | import android.content.pm.ApplicationInfo; |
Philip Cuadra | 7bd0fdd | 2016-04-28 15:26:49 -0700 | [diff] [blame] | 35 | import android.content.pm.PackageManager; |
Philip Cuadra | 7cb2f8b | 2016-06-15 16:23:43 -0700 | [diff] [blame] | 36 | import android.content.pm.ResolveInfo; |
Jorim Jaggi | 0c84996 | 2018-07-15 14:24:38 +0200 | [diff] [blame] | 37 | import android.database.ContentObserver; |
Carmen Jackson | f107a23 | 2017-05-16 10:37:26 -0700 | [diff] [blame] | 38 | import android.net.Uri; |
Philip Cuadra | 7bd0fdd | 2016-04-28 15:26:49 -0700 | [diff] [blame] | 39 | import android.os.Binder; |
| 40 | import android.os.Build; |
Philip Cuadra | a95cea0 | 2016-07-06 16:00:32 -0700 | [diff] [blame] | 41 | import android.os.Handler; |
| 42 | import android.os.Looper; |
| 43 | import android.os.Message; |
Jorim Jaggi | 7119800f | 2018-07-09 17:57:10 +0200 | [diff] [blame] | 44 | import android.os.RemoteException; |
Nicolas Geoffray | 8b5976e | 2019-02-20 15:41:03 +0000 | [diff] [blame] | 45 | import android.os.SystemProperties; |
Jeff Sharkey | 847bd85 | 2016-08-03 17:20:03 -0600 | [diff] [blame] | 46 | import android.os.UserHandle; |
Carmen Jackson | 0ff9946 | 2018-08-09 14:51:39 -0700 | [diff] [blame] | 47 | import android.os.UserManager; |
Philip Cuadra | 7cb2f8b | 2016-06-15 16:23:43 -0700 | [diff] [blame] | 48 | import android.provider.MediaStore; |
Jorim Jaggi | 0c84996 | 2018-07-15 14:24:38 +0200 | [diff] [blame] | 49 | import android.provider.Settings; |
Philip Cuadra | 7bd0fdd | 2016-04-28 15:26:49 -0700 | [diff] [blame] | 50 | import android.system.ErrnoException; |
| 51 | import android.system.Os; |
| 52 | import android.system.OsConstants; |
Jorim Jaggi | 7119800f | 2018-07-09 17:57:10 +0200 | [diff] [blame] | 53 | import android.util.ArrayMap; |
Calin Juravle | 31ce3a8 | 2017-05-22 17:49:01 -0700 | [diff] [blame] | 54 | import android.util.ArraySet; |
Jeff Sharkey | 847bd85 | 2016-08-03 17:20:03 -0600 | [diff] [blame] | 55 | import android.util.Slog; |
Philip Cuadra | 7bd0fdd | 2016-04-28 15:26:49 -0700 | [diff] [blame] | 56 | |
Jorim Jaggi | 7119800f | 2018-07-09 17:57:10 +0200 | [diff] [blame] | 57 | import com.android.internal.annotations.GuardedBy; |
Philip Cuadra | 7cb2f8b | 2016-06-15 16:23:43 -0700 | [diff] [blame] | 58 | import com.android.internal.app.ResolverActivity; |
Philip Cuadra | a95cea0 | 2016-07-06 16:00:32 -0700 | [diff] [blame] | 59 | import com.android.internal.os.BackgroundThread; |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 60 | import com.android.internal.util.DumpUtils; |
Jorim Jaggi | 7119800f | 2018-07-09 17:57:10 +0200 | [diff] [blame] | 61 | import com.android.internal.util.function.pooled.PooledLambda; |
Wale Ogunwale | 214f348 | 2018-10-04 11:00:47 -0700 | [diff] [blame] | 62 | import com.android.server.wm.ActivityTaskManagerInternal; |
Valentin Iftime | d22452d | 2019-07-12 15:15:54 +0200 | [diff] [blame] | 63 | |
Philip Cuadra | d9bd884 | 2016-07-12 17:29:38 -0700 | [diff] [blame] | 64 | import dalvik.system.DexFile; |
Philip Cuadra | 7cb2f8b | 2016-06-15 16:23:43 -0700 | [diff] [blame] | 65 | import dalvik.system.VMRuntime; |
| 66 | |
Daniel Colascione | 9779b5e | 2018-03-21 19:13:57 -0700 | [diff] [blame] | 67 | import java.io.Closeable; |
Daniel Colascione | 9779b5e | 2018-03-21 19:13:57 -0700 | [diff] [blame] | 68 | import java.io.DataInputStream; |
Valentin Iftime | d22452d | 2019-07-12 15:15:54 +0200 | [diff] [blame] | 69 | import java.io.FileDescriptor; |
Philip Cuadra | 7bd0fdd | 2016-04-28 15:26:49 -0700 | [diff] [blame] | 70 | import java.io.IOException; |
Valentin Iftime | d22452d | 2019-07-12 15:15:54 +0200 | [diff] [blame] | 71 | import java.io.InputStream; |
Philip Cuadra | 7bd0fdd | 2016-04-28 15:26:49 -0700 | [diff] [blame] | 72 | import java.io.PrintWriter; |
Jorim Jaggi | 7119800f | 2018-07-09 17:57:10 +0200 | [diff] [blame] | 73 | import java.lang.annotation.Retention; |
| 74 | import java.lang.annotation.RetentionPolicy; |
Jeff Sharkey | 847bd85 | 2016-08-03 17:20:03 -0600 | [diff] [blame] | 75 | import java.util.ArrayList; |
Valentin Iftime | d22452d | 2019-07-12 15:15:54 +0200 | [diff] [blame] | 76 | import java.util.List; |
Daniel Colascione | 9779b5e | 2018-03-21 19:13:57 -0700 | [diff] [blame] | 77 | import java.util.zip.ZipEntry; |
Valentin Iftime | d22452d | 2019-07-12 15:15:54 +0200 | [diff] [blame] | 78 | import java.util.zip.ZipFile; |
Daniel Colascione | 9779b5e | 2018-03-21 19:13:57 -0700 | [diff] [blame] | 79 | |
Philip Cuadra | 7bd0fdd | 2016-04-28 15:26:49 -0700 | [diff] [blame] | 80 | /** |
| 81 | * <p>PinnerService pins important files for key processes in memory.</p> |
| 82 | * <p>Files to pin are specified in the config_defaultPinnerServiceFiles |
Philip Cuadra | 7cb2f8b | 2016-06-15 16:23:43 -0700 | [diff] [blame] | 83 | * overlay.</p> |
| 84 | * <p>Pin the default camera application if specified in config_pinnerCameraApp.</p> |
Philip Cuadra | 7bd0fdd | 2016-04-28 15:26:49 -0700 | [diff] [blame] | 85 | */ |
| 86 | public final class PinnerService extends SystemService { |
| 87 | private static final boolean DEBUG = false; |
| 88 | private static final String TAG = "PinnerService"; |
Jorim Jaggi | 7119800f | 2018-07-09 17:57:10 +0200 | [diff] [blame] | 89 | |
Daniel Colascione | 9779b5e | 2018-03-21 19:13:57 -0700 | [diff] [blame] | 90 | private static final String PIN_META_FILENAME = "pinlist.meta"; |
| 91 | private static final int PAGE_SIZE = (int) Os.sysconf(OsConstants._SC_PAGESIZE); |
Jorim Jaggi | 7119800f | 2018-07-09 17:57:10 +0200 | [diff] [blame] | 92 | private static final int MATCH_FLAGS = PackageManager.MATCH_DEFAULT_ONLY |
| 93 | | PackageManager.MATCH_DIRECT_BOOT_AWARE |
| 94 | | PackageManager.MATCH_DIRECT_BOOT_UNAWARE; |
| 95 | |
| 96 | private static final int KEY_CAMERA = 0; |
| 97 | private static final int KEY_HOME = 1; |
Valentin Iftime | 6a652e1 | 2019-07-24 16:51:38 +0200 | [diff] [blame] | 98 | private static final int KEY_ASSISTANT = 2; |
Jorim Jaggi | 7119800f | 2018-07-09 17:57:10 +0200 | [diff] [blame] | 99 | |
Igor Murashkin | a6d3cd1 | 2019-05-13 16:40:39 -0700 | [diff] [blame] | 100 | // Pin the camera application. |
| 101 | private static boolean PROP_PIN_CAMERA = SystemProperties.getBoolean( |
| 102 | "pinner.pin_camera", true); |
| 103 | // Pin using pinlist.meta when pinning apps. |
| 104 | private static boolean PROP_PIN_PINLIST = SystemProperties.getBoolean( |
| 105 | "pinner.use_pinlist", true); |
| 106 | // Pin the whole odex/vdex/etc file when pinning apps. |
| 107 | private static boolean PROP_PIN_ODEX = SystemProperties.getBoolean( |
| 108 | "pinner.whole_odex", true); |
| 109 | |
Jorim Jaggi | 7119800f | 2018-07-09 17:57:10 +0200 | [diff] [blame] | 110 | private static final int MAX_CAMERA_PIN_SIZE = 80 * (1 << 20); // 80MB max for camera app. |
| 111 | private static final int MAX_HOME_PIN_SIZE = 6 * (1 << 20); // 6MB max for home app. |
Valentin Iftime | 6a652e1 | 2019-07-24 16:51:38 +0200 | [diff] [blame] | 112 | private static final int MAX_ASSISTANT_PIN_SIZE = 60 * (1 << 20); // 60MB max for assistant app. |
Jorim Jaggi | 7119800f | 2018-07-09 17:57:10 +0200 | [diff] [blame] | 113 | |
Valentin Iftime | 6a652e1 | 2019-07-24 16:51:38 +0200 | [diff] [blame] | 114 | @IntDef({KEY_CAMERA, KEY_HOME, KEY_ASSISTANT}) |
Jorim Jaggi | 7119800f | 2018-07-09 17:57:10 +0200 | [diff] [blame] | 115 | @Retention(RetentionPolicy.SOURCE) |
| 116 | public @interface AppKey {} |
Philip Cuadra | 7bd0fdd | 2016-04-28 15:26:49 -0700 | [diff] [blame] | 117 | |
| 118 | private final Context mContext; |
Wale Ogunwale | 214f348 | 2018-10-04 11:00:47 -0700 | [diff] [blame] | 119 | private final ActivityTaskManagerInternal mAtmInternal; |
Jorim Jaggi | 7119800f | 2018-07-09 17:57:10 +0200 | [diff] [blame] | 120 | private final ActivityManagerInternal mAmInternal; |
| 121 | private final IActivityManager mAm; |
Carmen Jackson | 0ff9946 | 2018-08-09 14:51:39 -0700 | [diff] [blame] | 122 | private final UserManager mUserManager; |
Valentin Iftime | 6a652e1 | 2019-07-24 16:51:38 +0200 | [diff] [blame] | 123 | private SearchManager mSearchManager; |
Philip Cuadra | 7bd0fdd | 2016-04-28 15:26:49 -0700 | [diff] [blame] | 124 | |
Jorim Jaggi | 7119800f | 2018-07-09 17:57:10 +0200 | [diff] [blame] | 125 | /** The list of the statically pinned files. */ |
| 126 | @GuardedBy("this") |
| 127 | private final ArrayList<PinnedFile> mPinnedFiles = new ArrayList<>(); |
| 128 | |
| 129 | /** The list of the pinned apps. This is a map from {@link AppKey} to a pinned app. */ |
| 130 | @GuardedBy("this") |
| 131 | private final ArrayMap<Integer, PinnedApp> mPinnedApps = new ArrayMap<>(); |
| 132 | |
| 133 | /** |
| 134 | * The list of the pinned apps that need to be repinned as soon as the all processes of a given |
| 135 | * uid are no longer active. Note that with background dex opt, the new dex/vdex files are only |
| 136 | * loaded into the processes once it restarts. So in case background dex opt recompiled these |
| 137 | * files, we still need to keep the old ones pinned until the processes restart. |
| 138 | * <p> |
| 139 | * This is a map from uid to {@link AppKey} |
| 140 | */ |
| 141 | @GuardedBy("this") |
| 142 | private final ArrayMap<Integer, Integer> mPendingRepin = new ArrayMap<>(); |
| 143 | |
| 144 | /** |
| 145 | * A set of {@link AppKey} that are configured to be pinned. |
| 146 | */ |
| 147 | private final ArraySet<Integer> mPinKeys = new ArraySet<>(); |
Daniel Colascione | 9779b5e | 2018-03-21 19:13:57 -0700 | [diff] [blame] | 148 | |
Philip Cuadra | 7bd0fdd | 2016-04-28 15:26:49 -0700 | [diff] [blame] | 149 | private BinderService mBinderService; |
Philip Cuadra | a95cea0 | 2016-07-06 16:00:32 -0700 | [diff] [blame] | 150 | private PinnerHandler mPinnerHandler = null; |
| 151 | |
Carmen Jackson | f107a23 | 2017-05-16 10:37:26 -0700 | [diff] [blame] | 152 | private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { |
| 153 | @Override |
| 154 | public void onReceive(Context context, Intent intent) { |
Jorim Jaggi | 7119800f | 2018-07-09 17:57:10 +0200 | [diff] [blame] | 155 | // If an app has updated, update pinned files accordingly. |
| 156 | if (Intent.ACTION_PACKAGE_REPLACED.equals(intent.getAction())) { |
Carmen Jackson | f107a23 | 2017-05-16 10:37:26 -0700 | [diff] [blame] | 157 | Uri packageUri = intent.getData(); |
Calin Juravle | 31ce3a8 | 2017-05-22 17:49:01 -0700 | [diff] [blame] | 158 | String packageName = packageUri.getSchemeSpecificPart(); |
| 159 | ArraySet<String> updatedPackages = new ArraySet<>(); |
| 160 | updatedPackages.add(packageName); |
Jorim Jaggi | 7119800f | 2018-07-09 17:57:10 +0200 | [diff] [blame] | 161 | update(updatedPackages, true /* force */); |
Carmen Jackson | f107a23 | 2017-05-16 10:37:26 -0700 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | }; |
Philip Cuadra | 7bd0fdd | 2016-04-28 15:26:49 -0700 | [diff] [blame] | 165 | |
| 166 | public PinnerService(Context context) { |
| 167 | super(context); |
| 168 | |
| 169 | mContext = context; |
Jorim Jaggi | 7119800f | 2018-07-09 17:57:10 +0200 | [diff] [blame] | 170 | boolean shouldPinCamera = context.getResources().getBoolean( |
Philip Cuadra | 7cb2f8b | 2016-06-15 16:23:43 -0700 | [diff] [blame] | 171 | com.android.internal.R.bool.config_pinnerCameraApp); |
Jorim Jaggi | 7119800f | 2018-07-09 17:57:10 +0200 | [diff] [blame] | 172 | boolean shouldPinHome = context.getResources().getBoolean( |
| 173 | com.android.internal.R.bool.config_pinnerHomeApp); |
Valentin Iftime | 6a652e1 | 2019-07-24 16:51:38 +0200 | [diff] [blame] | 174 | boolean shouldPinAssistant = context.getResources().getBoolean( |
| 175 | com.android.internal.R.bool.config_pinnerAssistantApp); |
Jorim Jaggi | 7119800f | 2018-07-09 17:57:10 +0200 | [diff] [blame] | 176 | if (shouldPinCamera) { |
Igor Murashkin | a6d3cd1 | 2019-05-13 16:40:39 -0700 | [diff] [blame] | 177 | if (PROP_PIN_CAMERA) { |
| 178 | mPinKeys.add(KEY_CAMERA); |
| 179 | } else if (DEBUG) { |
| 180 | Slog.i(TAG, "Pinner - skip pinning camera app"); |
| 181 | } |
Jorim Jaggi | 7119800f | 2018-07-09 17:57:10 +0200 | [diff] [blame] | 182 | } |
| 183 | if (shouldPinHome) { |
| 184 | mPinKeys.add(KEY_HOME); |
| 185 | } |
Valentin Iftime | 6a652e1 | 2019-07-24 16:51:38 +0200 | [diff] [blame] | 186 | if (shouldPinAssistant) { |
| 187 | mPinKeys.add(KEY_ASSISTANT); |
| 188 | } |
Philip Cuadra | a95cea0 | 2016-07-06 16:00:32 -0700 | [diff] [blame] | 189 | mPinnerHandler = new PinnerHandler(BackgroundThread.get().getLooper()); |
Carmen Jackson | f107a23 | 2017-05-16 10:37:26 -0700 | [diff] [blame] | 190 | |
Wale Ogunwale | 214f348 | 2018-10-04 11:00:47 -0700 | [diff] [blame] | 191 | mAtmInternal = LocalServices.getService(ActivityTaskManagerInternal.class); |
Jorim Jaggi | 7119800f | 2018-07-09 17:57:10 +0200 | [diff] [blame] | 192 | mAmInternal = LocalServices.getService(ActivityManagerInternal.class); |
| 193 | mAm = ActivityManager.getService(); |
| 194 | |
Carmen Jackson | 0ff9946 | 2018-08-09 14:51:39 -0700 | [diff] [blame] | 195 | mUserManager = mContext.getSystemService(UserManager.class); |
| 196 | |
Carmen Jackson | f107a23 | 2017-05-16 10:37:26 -0700 | [diff] [blame] | 197 | IntentFilter filter = new IntentFilter(); |
| 198 | filter.addAction(Intent.ACTION_PACKAGE_REPLACED); |
| 199 | filter.addDataScheme("package"); |
| 200 | mContext.registerReceiver(mBroadcastReceiver, filter); |
Jorim Jaggi | 0c84996 | 2018-07-15 14:24:38 +0200 | [diff] [blame] | 201 | |
Jorim Jaggi | 7119800f | 2018-07-09 17:57:10 +0200 | [diff] [blame] | 202 | registerUidListener(); |
Jorim Jaggi | 0c84996 | 2018-07-15 14:24:38 +0200 | [diff] [blame] | 203 | registerUserSetupCompleteListener(); |
Philip Cuadra | 7bd0fdd | 2016-04-28 15:26:49 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | @Override |
| 207 | public void onStart() { |
Philip Cuadra | 7cb2f8b | 2016-06-15 16:23:43 -0700 | [diff] [blame] | 208 | if (DEBUG) { |
| 209 | Slog.i(TAG, "Starting PinnerService"); |
| 210 | } |
Philip Cuadra | 7bd0fdd | 2016-04-28 15:26:49 -0700 | [diff] [blame] | 211 | mBinderService = new BinderService(); |
| 212 | publishBinderService("pinner", mBinderService); |
Carmen Jackson | f107a23 | 2017-05-16 10:37:26 -0700 | [diff] [blame] | 213 | publishLocalService(PinnerService.class, this); |
Philip Cuadra | 7bd0fdd | 2016-04-28 15:26:49 -0700 | [diff] [blame] | 214 | |
Jeff Sharkey | 847bd85 | 2016-08-03 17:20:03 -0600 | [diff] [blame] | 215 | mPinnerHandler.obtainMessage(PinnerHandler.PIN_ONSTART_MSG).sendToTarget(); |
Jorim Jaggi | 7119800f | 2018-07-09 17:57:10 +0200 | [diff] [blame] | 216 | sendPinAppsMessage(UserHandle.USER_SYSTEM); |
Philip Cuadra | 7cb2f8b | 2016-06-15 16:23:43 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Valentin Iftime | 6a652e1 | 2019-07-24 16:51:38 +0200 | [diff] [blame] | 219 | @Override |
| 220 | public void onBootPhase(int phase) { |
| 221 | // SearchManagerService is started after PinnerService, wait for PHASE_SYSTEM_SERVICES_READY |
| 222 | if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) { |
| 223 | mSearchManager = (SearchManager) mContext.getSystemService(Context.SEARCH_SERVICE); |
| 224 | sendPinAppsMessage(UserHandle.USER_SYSTEM); |
| 225 | } |
| 226 | } |
| 227 | |
Philip Cuadra | 7cb2f8b | 2016-06-15 16:23:43 -0700 | [diff] [blame] | 228 | /** |
Jorim Jaggi | 7119800f | 2018-07-09 17:57:10 +0200 | [diff] [blame] | 229 | * Repin apps on user switch. |
| 230 | * <p> |
| 231 | * If more than one user is using the device each user may set a different preference for the |
| 232 | * individual apps. Make sure that user's preference is pinned into memory. |
Philip Cuadra | a95cea0 | 2016-07-06 16:00:32 -0700 | [diff] [blame] | 233 | */ |
Philip Cuadra | 7cb2f8b | 2016-06-15 16:23:43 -0700 | [diff] [blame] | 234 | @Override |
| 235 | public void onSwitchUser(int userHandle) { |
Carmen Jackson | 0ff9946 | 2018-08-09 14:51:39 -0700 | [diff] [blame] | 236 | if (!mUserManager.isManagedProfile(userHandle)) { |
| 237 | sendPinAppsMessage(userHandle); |
| 238 | } |
Jorim Jaggi | 7119800f | 2018-07-09 17:57:10 +0200 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | @Override |
| 242 | public void onUnlockUser(int userHandle) { |
Carmen Jackson | 0ff9946 | 2018-08-09 14:51:39 -0700 | [diff] [blame] | 243 | if (!mUserManager.isManagedProfile(userHandle)) { |
| 244 | sendPinAppsMessage(userHandle); |
| 245 | } |
Philip Cuadra | 7cb2f8b | 2016-06-15 16:23:43 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Philip Cuadra | a95cea0 | 2016-07-06 16:00:32 -0700 | [diff] [blame] | 248 | /** |
Carmen Jackson | f107a23 | 2017-05-16 10:37:26 -0700 | [diff] [blame] | 249 | * Update the currently pinned files. |
Jorim Jaggi | 7119800f | 2018-07-09 17:57:10 +0200 | [diff] [blame] | 250 | * Specifically, this only updates pinning for the apps that need to be pinned. |
Carmen Jackson | f107a23 | 2017-05-16 10:37:26 -0700 | [diff] [blame] | 251 | * The other files pinned in onStart will not need to be updated. |
| 252 | */ |
Jorim Jaggi | 7119800f | 2018-07-09 17:57:10 +0200 | [diff] [blame] | 253 | public void update(ArraySet<String> updatedPackages, boolean force) { |
| 254 | int currentUser = ActivityManager.getCurrentUser(); |
| 255 | for (int i = mPinKeys.size() - 1; i >= 0; i--) { |
| 256 | int key = mPinKeys.valueAt(i); |
| 257 | ApplicationInfo info = getInfoForKey(key, currentUser); |
| 258 | if (info != null && updatedPackages.contains(info.packageName)) { |
| 259 | Slog.i(TAG, "Updating pinned files for " + info.packageName + " force=" + force); |
| 260 | sendPinAppMessage(key, currentUser, force); |
| 261 | } |
Calin Juravle | 31ce3a8 | 2017-05-22 17:49:01 -0700 | [diff] [blame] | 262 | } |
Carmen Jackson | f107a23 | 2017-05-16 10:37:26 -0700 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | /** |
Philip Cuadra | a95cea0 | 2016-07-06 16:00:32 -0700 | [diff] [blame] | 266 | * Handler for on start pinning message |
| 267 | */ |
| 268 | private void handlePinOnStart() { |
Nicolas Geoffray | 8b5976e | 2019-02-20 15:41:03 +0000 | [diff] [blame] | 269 | final String bootImage = SystemProperties.get("dalvik.vm.boot-image", ""); |
| 270 | String[] filesToPin = null; |
| 271 | if (bootImage.endsWith("apex.art")) { |
| 272 | // Use the files listed for that specific boot image |
| 273 | filesToPin = mContext.getResources().getStringArray( |
| 274 | com.android.internal.R.array.config_apexBootImagePinnerServiceFiles); |
| 275 | } else { |
| 276 | // Files to pin come from the overlay and can be specified per-device config |
| 277 | filesToPin = mContext.getResources().getStringArray( |
| 278 | com.android.internal.R.array.config_defaultPinnerServiceFiles); |
| 279 | } |
Daniel Colascione | 9779b5e | 2018-03-21 19:13:57 -0700 | [diff] [blame] | 280 | // Continue trying to pin each file even if we fail to pin some of them |
| 281 | for (String fileToPin : filesToPin) { |
| 282 | PinnedFile pf = pinFile(fileToPin, |
| 283 | Integer.MAX_VALUE, |
| 284 | /*attemptPinIntrospection=*/false); |
| 285 | if (pf == null) { |
| 286 | Slog.e(TAG, "Failed to pin file = " + fileToPin); |
| 287 | continue; |
| 288 | } |
| 289 | |
| 290 | synchronized (this) { |
| 291 | mPinnedFiles.add(pf); |
Philip Cuadra | a95cea0 | 2016-07-06 16:00:32 -0700 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | } |
| 295 | |
Jorim Jaggi | 0c84996 | 2018-07-15 14:24:38 +0200 | [diff] [blame] | 296 | /** |
| 297 | * Registers a listener to repin the home app when user setup is complete, as the home intent |
| 298 | * initially resolves to setup wizard, but once setup is complete, it will resolve to the |
| 299 | * regular home app. |
| 300 | */ |
| 301 | private void registerUserSetupCompleteListener() { |
| 302 | Uri userSetupCompleteUri = Settings.Secure.getUriFor( |
| 303 | Settings.Secure.USER_SETUP_COMPLETE); |
| 304 | mContext.getContentResolver().registerContentObserver(userSetupCompleteUri, |
| 305 | false, new ContentObserver(null) { |
| 306 | @Override |
| 307 | public void onChange(boolean selfChange, Uri uri) { |
| 308 | if (userSetupCompleteUri.equals(uri)) { |
| 309 | sendPinAppMessage(KEY_HOME, ActivityManager.getCurrentUser(), |
| 310 | true /* force */); |
| 311 | } |
| 312 | } |
| 313 | }, UserHandle.USER_ALL); |
| 314 | } |
| 315 | |
Jorim Jaggi | 7119800f | 2018-07-09 17:57:10 +0200 | [diff] [blame] | 316 | private void registerUidListener() { |
| 317 | try { |
| 318 | mAm.registerUidObserver(new IUidObserver.Stub() { |
| 319 | @Override |
| 320 | public void onUidGone(int uid, boolean disabled) throws RemoteException { |
| 321 | mPinnerHandler.sendMessage(PooledLambda.obtainMessage( |
| 322 | PinnerService::handleUidGone, PinnerService.this, uid)); |
| 323 | } |
| 324 | |
| 325 | @Override |
| 326 | public void onUidActive(int uid) throws RemoteException { |
| 327 | mPinnerHandler.sendMessage(PooledLambda.obtainMessage( |
| 328 | PinnerService::handleUidActive, PinnerService.this, uid)); |
| 329 | } |
| 330 | |
| 331 | @Override |
| 332 | public void onUidIdle(int uid, boolean disabled) throws RemoteException { |
| 333 | } |
| 334 | |
| 335 | @Override |
Hui Yu | 2696932 | 2019-08-21 14:56:35 -0700 | [diff] [blame^] | 336 | public void onUidStateChanged(int uid, int procState, long procStateSeq, |
| 337 | int capability) throws RemoteException { |
Jorim Jaggi | 7119800f | 2018-07-09 17:57:10 +0200 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | @Override |
| 341 | public void onUidCachedChanged(int uid, boolean cached) throws RemoteException { |
| 342 | } |
| 343 | }, UID_OBSERVER_GONE | UID_OBSERVER_ACTIVE, 0, "system"); |
| 344 | } catch (RemoteException e) { |
| 345 | Slog.e(TAG, "Failed to register uid observer", e); |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | private void handleUidGone(int uid) { |
| 350 | updateActiveState(uid, false /* active */); |
| 351 | int key; |
| 352 | synchronized (this) { |
| 353 | |
| 354 | // In case we have a pending repin, repin now. See mPendingRepin for more information. |
| 355 | key = mPendingRepin.getOrDefault(uid, -1); |
| 356 | if (key == -1) { |
| 357 | return; |
| 358 | } |
| 359 | mPendingRepin.remove(uid); |
| 360 | } |
| 361 | pinApp(key, ActivityManager.getCurrentUser(), false /* force */); |
| 362 | } |
| 363 | |
| 364 | private void handleUidActive(int uid) { |
| 365 | updateActiveState(uid, true /* active */); |
| 366 | } |
| 367 | |
| 368 | private void updateActiveState(int uid, boolean active) { |
| 369 | synchronized (this) { |
| 370 | for (int i = mPinnedApps.size() - 1; i >= 0; i--) { |
| 371 | PinnedApp app = mPinnedApps.valueAt(i); |
| 372 | if (app.uid == uid) { |
| 373 | app.active = active; |
| 374 | } |
Philip Cuadra | 7cb2f8b | 2016-06-15 16:23:43 -0700 | [diff] [blame] | 375 | } |
| 376 | } |
| 377 | } |
| 378 | |
Jorim Jaggi | 7119800f | 2018-07-09 17:57:10 +0200 | [diff] [blame] | 379 | private void unpinApp(@AppKey int key) { |
| 380 | ArrayList<PinnedFile> pinnedAppFiles; |
Daniel Colascione | 9779b5e | 2018-03-21 19:13:57 -0700 | [diff] [blame] | 381 | synchronized (this) { |
Jorim Jaggi | 7119800f | 2018-07-09 17:57:10 +0200 | [diff] [blame] | 382 | PinnedApp app = mPinnedApps.get(key); |
| 383 | if (app == null) { |
| 384 | return; |
| 385 | } |
| 386 | mPinnedApps.remove(key); |
| 387 | pinnedAppFiles = new ArrayList<>(app.mFiles); |
Philip Cuadra | 7cb2f8b | 2016-06-15 16:23:43 -0700 | [diff] [blame] | 388 | } |
Jorim Jaggi | 7119800f | 2018-07-09 17:57:10 +0200 | [diff] [blame] | 389 | for (PinnedFile pinnedFile : pinnedAppFiles) { |
Daniel Colascione | 9779b5e | 2018-03-21 19:13:57 -0700 | [diff] [blame] | 390 | pinnedFile.close(); |
| 391 | } |
Philip Cuadra | 7cb2f8b | 2016-06-15 16:23:43 -0700 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | private boolean isResolverActivity(ActivityInfo info) { |
| 395 | return ResolverActivity.class.getName().equals(info.name); |
| 396 | } |
| 397 | |
| 398 | private ApplicationInfo getCameraInfo(int userHandle) { |
Philip Cuadra | 7cb2f8b | 2016-06-15 16:23:43 -0700 | [diff] [blame] | 399 | Intent cameraIntent = new Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA); |
Carmen Jackson | 879fb68 | 2018-07-20 16:43:09 -0700 | [diff] [blame] | 400 | ApplicationInfo info = getApplicationInfoForIntent(cameraIntent, userHandle, |
| 401 | false /* defaultToSystemApp */); |
| 402 | |
| 403 | // If the STILL_IMAGE_CAMERA intent doesn't resolve, try the _SECURE intent. |
| 404 | // We don't use _SECURE first because it will never get set on a device |
| 405 | // without File-based Encryption. But if the user has only set the intent |
| 406 | // before unlocking their device, we may still be able to identify their |
| 407 | // preference using this intent. |
| 408 | if (info == null) { |
| 409 | cameraIntent = new Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA_SECURE); |
| 410 | info = getApplicationInfoForIntent(cameraIntent, userHandle, |
| 411 | false /* defaultToSystemApp */); |
| 412 | } |
| 413 | |
| 414 | // If the _SECURE intent doesn't resolve, try the original intent but request |
| 415 | // the system app for camera if there was more than one result. |
| 416 | if (info == null) { |
| 417 | cameraIntent = new Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA); |
| 418 | info = getApplicationInfoForIntent(cameraIntent, userHandle, |
| 419 | true /* defaultToSystemApp */); |
| 420 | } |
| 421 | return info; |
Jorim Jaggi | 7119800f | 2018-07-09 17:57:10 +0200 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | private ApplicationInfo getHomeInfo(int userHandle) { |
Wale Ogunwale | 214f348 | 2018-10-04 11:00:47 -0700 | [diff] [blame] | 425 | Intent intent = mAtmInternal.getHomeIntent(); |
Carmen Jackson | 879fb68 | 2018-07-20 16:43:09 -0700 | [diff] [blame] | 426 | return getApplicationInfoForIntent(intent, userHandle, false); |
Jorim Jaggi | 7119800f | 2018-07-09 17:57:10 +0200 | [diff] [blame] | 427 | } |
| 428 | |
Valentin Iftime | 6a652e1 | 2019-07-24 16:51:38 +0200 | [diff] [blame] | 429 | private ApplicationInfo getAssistantInfo(int userHandle) { |
| 430 | if (mSearchManager != null) { |
| 431 | Intent intent = mSearchManager.getAssistIntent(false); |
| 432 | return getApplicationInfoForIntent(intent, userHandle, true); |
| 433 | } |
| 434 | return null; |
| 435 | } |
| 436 | |
Carmen Jackson | 879fb68 | 2018-07-20 16:43:09 -0700 | [diff] [blame] | 437 | private ApplicationInfo getApplicationInfoForIntent(Intent intent, int userHandle, |
| 438 | boolean defaultToSystemApp) { |
Jorim Jaggi | 7119800f | 2018-07-09 17:57:10 +0200 | [diff] [blame] | 439 | if (intent == null) { |
Philip Cuadra | 7cb2f8b | 2016-06-15 16:23:43 -0700 | [diff] [blame] | 440 | return null; |
| 441 | } |
Carmen Jackson | 879fb68 | 2018-07-20 16:43:09 -0700 | [diff] [blame] | 442 | |
| 443 | ResolveInfo resolveInfo = mContext.getPackageManager().resolveActivityAsUser(intent, |
Jorim Jaggi | 7119800f | 2018-07-09 17:57:10 +0200 | [diff] [blame] | 444 | MATCH_FLAGS, userHandle); |
Carmen Jackson | 879fb68 | 2018-07-20 16:43:09 -0700 | [diff] [blame] | 445 | |
| 446 | // If this intent can resolve to only one app, choose that one. |
| 447 | // Otherwise, if we've requested to default to the system app, return it; |
| 448 | // if we have not requested that default, return null if there's more than one option. |
| 449 | // If there's more than one system app, return null since we don't know which to pick. |
| 450 | if (resolveInfo == null) { |
Philip Cuadra | 7cb2f8b | 2016-06-15 16:23:43 -0700 | [diff] [blame] | 451 | return null; |
| 452 | } |
Carmen Jackson | 879fb68 | 2018-07-20 16:43:09 -0700 | [diff] [blame] | 453 | |
| 454 | if (!isResolverActivity(resolveInfo.activityInfo)) { |
| 455 | return resolveInfo.activityInfo.applicationInfo; |
Jorim Jaggi | 7119800f | 2018-07-09 17:57:10 +0200 | [diff] [blame] | 456 | } |
Carmen Jackson | 879fb68 | 2018-07-20 16:43:09 -0700 | [diff] [blame] | 457 | |
| 458 | if (defaultToSystemApp) { |
| 459 | List<ResolveInfo> infoList = mContext.getPackageManager() |
| 460 | .queryIntentActivitiesAsUser(intent, MATCH_FLAGS, userHandle); |
| 461 | ApplicationInfo systemAppInfo = null; |
| 462 | for (ResolveInfo info : infoList) { |
| 463 | if ((info.activityInfo.applicationInfo.flags |
| 464 | & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 465 | if (systemAppInfo == null) { |
| 466 | systemAppInfo = info.activityInfo.applicationInfo; |
| 467 | } else { |
| 468 | // If there's more than one system app, return null due to ambiguity. |
| 469 | return null; |
| 470 | } |
| 471 | } |
| 472 | } |
| 473 | return systemAppInfo; |
| 474 | } |
| 475 | |
| 476 | return null; |
Jorim Jaggi | 7119800f | 2018-07-09 17:57:10 +0200 | [diff] [blame] | 477 | } |
Philip Cuadra | 7cb2f8b | 2016-06-15 16:23:43 -0700 | [diff] [blame] | 478 | |
Jorim Jaggi | 7119800f | 2018-07-09 17:57:10 +0200 | [diff] [blame] | 479 | private void sendPinAppsMessage(int userHandle) { |
| 480 | mPinnerHandler.sendMessage(PooledLambda.obtainMessage(PinnerService::pinApps, this, |
| 481 | userHandle)); |
| 482 | } |
| 483 | |
| 484 | private void pinApps(int userHandle) { |
| 485 | for (int i = mPinKeys.size() - 1; i >= 0; i--) { |
| 486 | int key = mPinKeys.valueAt(i); |
| 487 | pinApp(key, userHandle, true /* force */); |
| 488 | } |
Philip Cuadra | 7cb2f8b | 2016-06-15 16:23:43 -0700 | [diff] [blame] | 489 | } |
| 490 | |
Carmen Jackson | f107a23 | 2017-05-16 10:37:26 -0700 | [diff] [blame] | 491 | /** |
Jorim Jaggi | 7119800f | 2018-07-09 17:57:10 +0200 | [diff] [blame] | 492 | * @see #pinApp(int, int, boolean) |
Carmen Jackson | f107a23 | 2017-05-16 10:37:26 -0700 | [diff] [blame] | 493 | */ |
Jorim Jaggi | 7119800f | 2018-07-09 17:57:10 +0200 | [diff] [blame] | 494 | private void sendPinAppMessage(int key, int userHandle, boolean force) { |
| 495 | mPinnerHandler.sendMessage(PooledLambda.obtainMessage(PinnerService::pinApp, this, |
| 496 | key, userHandle, force)); |
| 497 | } |
| 498 | |
| 499 | /** |
| 500 | * Pins an app of a specific type {@code key}. |
| 501 | * |
| 502 | * @param force If false, this will not repin the app if it's currently active. See |
| 503 | * {@link #mPendingRepin}. |
| 504 | */ |
| 505 | private void pinApp(int key, int userHandle, boolean force) { |
| 506 | int uid = getUidForKey(key); |
| 507 | |
| 508 | // In case the app is currently active, don't repin until next process restart. See |
| 509 | // mPendingRepin for more information. |
| 510 | if (!force && uid != -1) { |
| 511 | synchronized (this) { |
| 512 | mPendingRepin.put(uid, key); |
| 513 | } |
| 514 | return; |
| 515 | } |
| 516 | unpinApp(key); |
| 517 | ApplicationInfo info = getInfoForKey(key, userHandle); |
| 518 | if (info != null) { |
| 519 | pinApp(key, info); |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | /** |
| 524 | * Checks whether the pinned package with {@code key} is active or not. |
| 525 | |
| 526 | * @return The uid of the pinned app, or {@code -1} otherwise. |
| 527 | */ |
| 528 | private int getUidForKey(@AppKey int key) { |
| 529 | synchronized (this) { |
| 530 | PinnedApp existing = mPinnedApps.get(key); |
| 531 | return existing != null && existing.active |
| 532 | ? existing.uid |
| 533 | : -1; |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | /** |
| 538 | * Retrieves the current application info for the given app type. |
| 539 | * |
| 540 | * @param key The app type to retrieve the info for. |
| 541 | * @param userHandle The user id of the current user. |
| 542 | */ |
| 543 | private @Nullable ApplicationInfo getInfoForKey(@AppKey int key, int userHandle) { |
| 544 | switch (key) { |
| 545 | case KEY_CAMERA: |
| 546 | return getCameraInfo(userHandle); |
| 547 | case KEY_HOME: |
| 548 | return getHomeInfo(userHandle); |
Valentin Iftime | 6a652e1 | 2019-07-24 16:51:38 +0200 | [diff] [blame] | 549 | case KEY_ASSISTANT: |
| 550 | return getAssistantInfo(userHandle); |
Jorim Jaggi | 7119800f | 2018-07-09 17:57:10 +0200 | [diff] [blame] | 551 | default: |
| 552 | return null; |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | /** |
| 557 | * @return The app type name for {@code key}. |
| 558 | */ |
| 559 | private String getNameForKey(@AppKey int key) { |
| 560 | switch (key) { |
| 561 | case KEY_CAMERA: |
| 562 | return "Camera"; |
| 563 | case KEY_HOME: |
| 564 | return "Home"; |
Valentin Iftime | 6a652e1 | 2019-07-24 16:51:38 +0200 | [diff] [blame] | 565 | case KEY_ASSISTANT: |
| 566 | return "Assistant"; |
Jorim Jaggi | 7119800f | 2018-07-09 17:57:10 +0200 | [diff] [blame] | 567 | default: |
| 568 | return null; |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | /** |
| 573 | * @return The maximum amount of bytes to be pinned for an app of type {@code key}. |
| 574 | */ |
| 575 | private int getSizeLimitForKey(@AppKey int key) { |
| 576 | switch (key) { |
| 577 | case KEY_CAMERA: |
| 578 | return MAX_CAMERA_PIN_SIZE; |
| 579 | case KEY_HOME: |
| 580 | return MAX_HOME_PIN_SIZE; |
Valentin Iftime | 6a652e1 | 2019-07-24 16:51:38 +0200 | [diff] [blame] | 581 | case KEY_ASSISTANT: |
| 582 | return MAX_ASSISTANT_PIN_SIZE; |
Jorim Jaggi | 7119800f | 2018-07-09 17:57:10 +0200 | [diff] [blame] | 583 | default: |
| 584 | return 0; |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | /** |
| 589 | * Pins an application. |
| 590 | * |
| 591 | * @param key The key of the app to pin. |
| 592 | * @param appInfo The corresponding app info. |
| 593 | */ |
| 594 | private void pinApp(@AppKey int key, @Nullable ApplicationInfo appInfo) { |
| 595 | if (appInfo == null) { |
| 596 | return; |
Philip Cuadra | 7cb2f8b | 2016-06-15 16:23:43 -0700 | [diff] [blame] | 597 | } |
| 598 | |
Jorim Jaggi | 7119800f | 2018-07-09 17:57:10 +0200 | [diff] [blame] | 599 | PinnedApp pinnedApp = new PinnedApp(appInfo); |
| 600 | synchronized (this) { |
| 601 | mPinnedApps.put(key, pinnedApp); |
| 602 | } |
Philip Cuadra | 7cb2f8b | 2016-06-15 16:23:43 -0700 | [diff] [blame] | 603 | |
Jorim Jaggi | 7119800f | 2018-07-09 17:57:10 +0200 | [diff] [blame] | 604 | // pin APK |
| 605 | int pinSizeLimit = getSizeLimitForKey(key); |
| 606 | String apk = appInfo.sourceDir; |
| 607 | PinnedFile pf = pinFile(apk, pinSizeLimit, /*attemptPinIntrospection=*/true); |
Philip Cuadra | 7cb2f8b | 2016-06-15 16:23:43 -0700 | [diff] [blame] | 608 | if (pf == null) { |
Jorim Jaggi | 7119800f | 2018-07-09 17:57:10 +0200 | [diff] [blame] | 609 | Slog.e(TAG, "Failed to pin " + apk); |
| 610 | return; |
Philip Cuadra | 7cb2f8b | 2016-06-15 16:23:43 -0700 | [diff] [blame] | 611 | } |
| 612 | if (DEBUG) { |
Daniel Colascione | 9779b5e | 2018-03-21 19:13:57 -0700 | [diff] [blame] | 613 | Slog.i(TAG, "Pinned " + pf.fileName); |
Philip Cuadra | 7cb2f8b | 2016-06-15 16:23:43 -0700 | [diff] [blame] | 614 | } |
Daniel Colascione | 9779b5e | 2018-03-21 19:13:57 -0700 | [diff] [blame] | 615 | synchronized (this) { |
Jorim Jaggi | 7119800f | 2018-07-09 17:57:10 +0200 | [diff] [blame] | 616 | pinnedApp.mFiles.add(pf); |
Daniel Colascione | 9779b5e | 2018-03-21 19:13:57 -0700 | [diff] [blame] | 617 | } |
Philip Cuadra | 7cb2f8b | 2016-06-15 16:23:43 -0700 | [diff] [blame] | 618 | |
Philip Cuadra | 7cb2f8b | 2016-06-15 16:23:43 -0700 | [diff] [blame] | 619 | // determine the ABI from either ApplicationInfo or Build |
Valentin Iftime | d22452d | 2019-07-12 15:15:54 +0200 | [diff] [blame] | 620 | String abi = appInfo.primaryCpuAbi != null ? appInfo.primaryCpuAbi : |
| 621 | Build.SUPPORTED_ABIS[0]; |
| 622 | String arch = VMRuntime.getInstructionSet(abi); |
Philip Cuadra | d9bd884 | 2016-07-12 17:29:38 -0700 | [diff] [blame] | 623 | // get the path to the odex or oat file |
Jorim Jaggi | 7119800f | 2018-07-09 17:57:10 +0200 | [diff] [blame] | 624 | String baseCodePath = appInfo.getBaseCodePath(); |
Calin Juravle | 128721a | 2017-05-15 20:20:50 -0700 | [diff] [blame] | 625 | String[] files = null; |
Philip Cuadra | d9bd884 | 2016-07-12 17:29:38 -0700 | [diff] [blame] | 626 | try { |
Calin Juravle | 128721a | 2017-05-15 20:20:50 -0700 | [diff] [blame] | 627 | files = DexFile.getDexFileOutputPaths(baseCodePath, arch); |
Philip Cuadra | d9bd884 | 2016-07-12 17:29:38 -0700 | [diff] [blame] | 628 | } catch (IOException ioe) {} |
Calin Juravle | 128721a | 2017-05-15 20:20:50 -0700 | [diff] [blame] | 629 | if (files == null) { |
Jorim Jaggi | 7119800f | 2018-07-09 17:57:10 +0200 | [diff] [blame] | 630 | return; |
Philip Cuadra | d9bd884 | 2016-07-12 17:29:38 -0700 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | //not pinning the oat/odex is not a fatal error |
Calin Juravle | 128721a | 2017-05-15 20:20:50 -0700 | [diff] [blame] | 634 | for (String file : files) { |
Jorim Jaggi | 7119800f | 2018-07-09 17:57:10 +0200 | [diff] [blame] | 635 | pf = pinFile(file, pinSizeLimit, /*attemptPinIntrospection=*/false); |
Calin Juravle | 128721a | 2017-05-15 20:20:50 -0700 | [diff] [blame] | 636 | if (pf != null) { |
Daniel Colascione | 9779b5e | 2018-03-21 19:13:57 -0700 | [diff] [blame] | 637 | synchronized (this) { |
Igor Murashkin | a6d3cd1 | 2019-05-13 16:40:39 -0700 | [diff] [blame] | 638 | if (PROP_PIN_ODEX) { |
| 639 | pinnedApp.mFiles.add(pf); |
| 640 | } |
Daniel Colascione | 9779b5e | 2018-03-21 19:13:57 -0700 | [diff] [blame] | 641 | } |
Calin Juravle | 128721a | 2017-05-15 20:20:50 -0700 | [diff] [blame] | 642 | if (DEBUG) { |
Igor Murashkin | a6d3cd1 | 2019-05-13 16:40:39 -0700 | [diff] [blame] | 643 | if (PROP_PIN_ODEX) { |
| 644 | Slog.i(TAG, "Pinned " + pf.fileName); |
| 645 | } else { |
| 646 | Slog.i(TAG, "Pinned [skip] " + pf.fileName); |
| 647 | } |
Calin Juravle | 128721a | 2017-05-15 20:20:50 -0700 | [diff] [blame] | 648 | } |
Philip Cuadra | 7cb2f8b | 2016-06-15 16:23:43 -0700 | [diff] [blame] | 649 | } |
| 650 | } |
Philip Cuadra | 7cb2f8b | 2016-06-15 16:23:43 -0700 | [diff] [blame] | 651 | } |
| 652 | |
Daniel Colascione | 9779b5e | 2018-03-21 19:13:57 -0700 | [diff] [blame] | 653 | /** mlock length bytes of fileToPin in memory |
| 654 | * |
| 655 | * If attemptPinIntrospection is true, then treat the file to pin as a zip file and |
| 656 | * look for a "pinlist.meta" file in the archive root directory. The structure of this |
| 657 | * file is a PINLIST_META as described below: |
| 658 | * |
| 659 | * <pre> |
| 660 | * PINLIST_META: PIN_RANGE* |
| 661 | * PIN_RANGE: PIN_START PIN_LENGTH |
| 662 | * PIN_START: big endian i32: offset in bytes of pin region from file start |
| 663 | * PIN_LENGTH: big endian i32: length of pin region in bytes |
| 664 | * </pre> |
| 665 | * |
| 666 | * (We use big endian because that's what DataInputStream is hardcoded to use.) |
| 667 | * |
| 668 | * If attemptPinIntrospection is false, then we use a single implicit PIN_RANGE of (0, |
| 669 | * maxBytesToPin); that is, we attempt to pin the first maxBytesToPin bytes of the file. |
| 670 | * |
| 671 | * After we open a file, we march through the list of pin ranges and attempt to pin |
| 672 | * each one, stopping after we've pinned maxBytesToPin bytes. (We may truncate the last |
| 673 | * pinned range to fit.) In this way, by choosing to emit certain PIN_RANGE pairs |
| 674 | * before others, file generators can express pins in priority order, making most |
| 675 | * effective use of the pinned-page quota. |
| 676 | * |
| 677 | * N.B. Each PIN_RANGE is clamped to the actual bounds of the file; all inputs have a |
| 678 | * meaningful interpretation. Also, a range locking a single byte of a page locks the |
| 679 | * whole page. Any truncated PIN_RANGE at EOF is ignored. Overlapping pinned entries |
| 680 | * are legal, but each pin of a byte counts toward the pin quota regardless of whether |
| 681 | * that byte has already been pinned, so the generator of PINLIST_META ought to ensure |
| 682 | * that ranges are non-overlapping. |
| 683 | * |
| 684 | * @param fileToPin Path to file to pin |
| 685 | * @param maxBytesToPin Maximum number of bytes to pin |
| 686 | * @param attemptPinIntrospection If true, try to open file as a |
| 687 | * zip in order to extract the |
| 688 | * @return Pinned memory resource owner thing or null on error |
Philip Cuadra | 7cb2f8b | 2016-06-15 16:23:43 -0700 | [diff] [blame] | 689 | */ |
Daniel Colascione | 9779b5e | 2018-03-21 19:13:57 -0700 | [diff] [blame] | 690 | private static PinnedFile pinFile(String fileToPin, |
| 691 | int maxBytesToPin, |
| 692 | boolean attemptPinIntrospection) { |
| 693 | ZipFile fileAsZip = null; |
| 694 | InputStream pinRangeStream = null; |
Philip Cuadra | 7bd0fdd | 2016-04-28 15:26:49 -0700 | [diff] [blame] | 695 | try { |
Daniel Colascione | 9779b5e | 2018-03-21 19:13:57 -0700 | [diff] [blame] | 696 | if (attemptPinIntrospection) { |
| 697 | fileAsZip = maybeOpenZip(fileToPin); |
Philip Cuadra | 7bd0fdd | 2016-04-28 15:26:49 -0700 | [diff] [blame] | 698 | } |
| 699 | |
Daniel Colascione | 9779b5e | 2018-03-21 19:13:57 -0700 | [diff] [blame] | 700 | if (fileAsZip != null) { |
| 701 | pinRangeStream = maybeOpenPinMetaInZip(fileAsZip, fileToPin); |
Philip Cuadra | 7bd0fdd | 2016-04-28 15:26:49 -0700 | [diff] [blame] | 702 | } |
| 703 | |
Daniel Colascione | 9779b5e | 2018-03-21 19:13:57 -0700 | [diff] [blame] | 704 | Slog.d(TAG, "pinRangeStream: " + pinRangeStream); |
Philip Cuadra | 7cb2f8b | 2016-06-15 16:23:43 -0700 | [diff] [blame] | 705 | |
Daniel Colascione | 9779b5e | 2018-03-21 19:13:57 -0700 | [diff] [blame] | 706 | PinRangeSource pinRangeSource = (pinRangeStream != null) |
| 707 | ? new PinRangeSourceStream(pinRangeStream) |
| 708 | : new PinRangeSourceStatic(0, Integer.MAX_VALUE /* will be clipped */); |
| 709 | return pinFileRanges(fileToPin, maxBytesToPin, pinRangeSource); |
| 710 | } finally { |
| 711 | safeClose(pinRangeStream); |
| 712 | safeClose(fileAsZip); // Also closes any streams we've opened |
Philip Cuadra | 7bd0fdd | 2016-04-28 15:26:49 -0700 | [diff] [blame] | 713 | } |
| 714 | } |
| 715 | |
Daniel Colascione | 9779b5e | 2018-03-21 19:13:57 -0700 | [diff] [blame] | 716 | /** |
| 717 | * Attempt to open a file as a zip file. On any sort of corruption, log, swallow the |
| 718 | * error, and return null. |
| 719 | */ |
| 720 | private static ZipFile maybeOpenZip(String fileName) { |
| 721 | ZipFile zip = null; |
Philip Cuadra | 7cb2f8b | 2016-06-15 16:23:43 -0700 | [diff] [blame] | 722 | try { |
Daniel Colascione | 9779b5e | 2018-03-21 19:13:57 -0700 | [diff] [blame] | 723 | zip = new ZipFile(fileName); |
| 724 | } catch (IOException ex) { |
| 725 | Slog.w(TAG, |
| 726 | String.format( |
| 727 | "could not open \"%s\" as zip: pinning as blob", |
| 728 | fileName), |
| 729 | ex); |
Philip Cuadra | 7cb2f8b | 2016-06-15 16:23:43 -0700 | [diff] [blame] | 730 | } |
Daniel Colascione | 9779b5e | 2018-03-21 19:13:57 -0700 | [diff] [blame] | 731 | return zip; |
| 732 | } |
| 733 | |
| 734 | /** |
| 735 | * Open a pin metadata file in the zip if one is present. |
| 736 | * |
| 737 | * @param zipFile Zip file to search |
| 738 | * @return Open input stream or null on any error |
| 739 | */ |
| 740 | private static InputStream maybeOpenPinMetaInZip(ZipFile zipFile, String fileName) { |
Igor Murashkin | a6d3cd1 | 2019-05-13 16:40:39 -0700 | [diff] [blame] | 741 | if (!PROP_PIN_PINLIST) { |
| 742 | if (DEBUG) { |
| 743 | Slog.i(TAG, "Pin - skip pinlist.meta in " + fileName); |
| 744 | } |
| 745 | return null; |
| 746 | } |
| 747 | |
Daniel Colascione | 9779b5e | 2018-03-21 19:13:57 -0700 | [diff] [blame] | 748 | ZipEntry pinMetaEntry = zipFile.getEntry(PIN_META_FILENAME); |
| 749 | InputStream pinMetaStream = null; |
| 750 | if (pinMetaEntry != null) { |
| 751 | try { |
| 752 | pinMetaStream = zipFile.getInputStream(pinMetaEntry); |
| 753 | } catch (IOException ex) { |
| 754 | Slog.w(TAG, |
| 755 | String.format("error reading pin metadata \"%s\": pinning as blob", |
| 756 | fileName), |
| 757 | ex); |
| 758 | } |
Philip Cuadra | 7cb2f8b | 2016-06-15 16:23:43 -0700 | [diff] [blame] | 759 | } |
Daniel Colascione | 9779b5e | 2018-03-21 19:13:57 -0700 | [diff] [blame] | 760 | return pinMetaStream; |
| 761 | } |
| 762 | |
| 763 | private static abstract class PinRangeSource { |
| 764 | /** Retrive a range to pin. |
| 765 | * |
| 766 | * @param outPinRange Receives the pin region |
| 767 | * @return True if we filled in outPinRange or false if we're out of pin entries |
| 768 | */ |
| 769 | abstract boolean read(PinRange outPinRange); |
| 770 | } |
| 771 | |
| 772 | private static final class PinRangeSourceStatic extends PinRangeSource { |
| 773 | private final int mPinStart; |
| 774 | private final int mPinLength; |
| 775 | private boolean mDone = false; |
| 776 | |
| 777 | PinRangeSourceStatic(int pinStart, int pinLength) { |
| 778 | mPinStart = pinStart; |
| 779 | mPinLength = pinLength; |
| 780 | } |
| 781 | |
| 782 | @Override |
| 783 | boolean read(PinRange outPinRange) { |
| 784 | outPinRange.start = mPinStart; |
| 785 | outPinRange.length = mPinLength; |
| 786 | boolean done = mDone; |
| 787 | mDone = true; |
| 788 | return !done; |
| 789 | } |
| 790 | } |
| 791 | |
| 792 | private static final class PinRangeSourceStream extends PinRangeSource { |
| 793 | private final DataInputStream mStream; |
| 794 | private boolean mDone = false; |
| 795 | |
| 796 | PinRangeSourceStream(InputStream stream) { |
| 797 | mStream = new DataInputStream(stream); |
| 798 | } |
| 799 | |
| 800 | @Override |
| 801 | boolean read(PinRange outPinRange) { |
| 802 | if (!mDone) { |
| 803 | try { |
| 804 | outPinRange.start = mStream.readInt(); |
| 805 | outPinRange.length = mStream.readInt(); |
| 806 | } catch (IOException ex) { |
| 807 | mDone = true; |
| 808 | } |
| 809 | } |
| 810 | return !mDone; |
| 811 | } |
| 812 | } |
| 813 | |
| 814 | /** |
| 815 | * Helper for pinFile. |
| 816 | * |
| 817 | * @param fileToPin Name of file to pin |
| 818 | * @param maxBytesToPin Maximum number of bytes to pin |
| 819 | * @param pinRangeSource Read PIN_RANGE entries from this stream to tell us what bytes |
| 820 | * to pin. |
| 821 | * @return PinnedFile or null on error |
| 822 | */ |
| 823 | private static PinnedFile pinFileRanges( |
| 824 | String fileToPin, |
| 825 | int maxBytesToPin, |
| 826 | PinRangeSource pinRangeSource) |
| 827 | { |
| 828 | FileDescriptor fd = new FileDescriptor(); |
| 829 | long address = -1; |
| 830 | int mapSize = 0; |
| 831 | |
| 832 | try { |
| 833 | int openFlags = (OsConstants.O_RDONLY | |
| 834 | OsConstants.O_CLOEXEC | |
| 835 | OsConstants.O_NOFOLLOW); |
| 836 | fd = Os.open(fileToPin, openFlags, 0); |
| 837 | mapSize = (int) Math.min(Os.fstat(fd).st_size, Integer.MAX_VALUE); |
| 838 | address = Os.mmap(0, mapSize, |
| 839 | OsConstants.PROT_READ, |
| 840 | OsConstants.MAP_SHARED, |
| 841 | fd, /*offset=*/0); |
| 842 | |
| 843 | PinRange pinRange = new PinRange(); |
| 844 | int bytesPinned = 0; |
| 845 | |
| 846 | // We pin at page granularity, so make sure the limit is page-aligned |
| 847 | if (maxBytesToPin % PAGE_SIZE != 0) { |
| 848 | maxBytesToPin -= maxBytesToPin % PAGE_SIZE; |
| 849 | } |
| 850 | |
| 851 | while (bytesPinned < maxBytesToPin && pinRangeSource.read(pinRange)) { |
| 852 | int pinStart = pinRange.start; |
| 853 | int pinLength = pinRange.length; |
| 854 | pinStart = clamp(0, pinStart, mapSize); |
| 855 | pinLength = clamp(0, pinLength, mapSize - pinStart); |
| 856 | pinLength = Math.min(maxBytesToPin - bytesPinned, pinLength); |
| 857 | |
| 858 | // mlock doesn't require the region to be page-aligned, but we snap the |
| 859 | // lock region to page boundaries anyway so that we don't under-count |
| 860 | // locking a single byte of a page as a charge of one byte even though the |
| 861 | // OS will retain the whole page. Thanks to this adjustment, we slightly |
| 862 | // over-count the pin charge of back-to-back pins touching the same page, |
| 863 | // but better that than undercounting. Besides: nothing stops pin metafile |
| 864 | // creators from making the actual regions page-aligned. |
| 865 | pinLength += pinStart % PAGE_SIZE; |
| 866 | pinStart -= pinStart % PAGE_SIZE; |
| 867 | if (pinLength % PAGE_SIZE != 0) { |
| 868 | pinLength += PAGE_SIZE - pinLength % PAGE_SIZE; |
| 869 | } |
| 870 | pinLength = clamp(0, pinLength, maxBytesToPin - bytesPinned); |
| 871 | |
| 872 | if (pinLength > 0) { |
| 873 | if (DEBUG) { |
| 874 | Slog.d(TAG, |
| 875 | String.format( |
| 876 | "pinning at %s %s bytes of %s", |
| 877 | pinStart, pinLength, fileToPin)); |
| 878 | } |
| 879 | Os.mlock(address + pinStart, pinLength); |
| 880 | } |
| 881 | bytesPinned += pinLength; |
| 882 | } |
| 883 | |
| 884 | PinnedFile pinnedFile = new PinnedFile(address, mapSize, fileToPin, bytesPinned); |
| 885 | address = -1; // Ownership transferred |
| 886 | return pinnedFile; |
| 887 | } catch (ErrnoException ex) { |
| 888 | Slog.e(TAG, "Could not pin file " + fileToPin, ex); |
| 889 | return null; |
| 890 | } finally { |
| 891 | safeClose(fd); |
| 892 | if (address >= 0) { |
| 893 | safeMunmap(address, mapSize); |
| 894 | } |
| 895 | } |
| 896 | } |
| 897 | |
| 898 | private static int clamp(int min, int value, int max) { |
| 899 | return Math.max(min, Math.min(value, max)); |
| 900 | } |
| 901 | |
| 902 | private static void safeMunmap(long address, long mapSize) { |
| 903 | try { |
| 904 | Os.munmap(address, mapSize); |
| 905 | } catch (ErrnoException ex) { |
| 906 | Slog.w(TAG, "ignoring error in unmap", ex); |
| 907 | } |
| 908 | } |
| 909 | |
| 910 | /** |
| 911 | * Close FD, swallowing irrelevant errors. |
| 912 | */ |
| 913 | private static void safeClose(@Nullable FileDescriptor fd) { |
| 914 | if (fd != null && fd.valid()) { |
| 915 | try { |
| 916 | Os.close(fd); |
| 917 | } catch (ErrnoException ex) { |
| 918 | // Swallow the exception: non-EBADF errors in close(2) |
| 919 | // indicate deferred paging write errors, which we |
| 920 | // don't care about here. The underlying file |
| 921 | // descriptor is always closed. |
| 922 | if (ex.errno == OsConstants.EBADF) { |
| 923 | throw new AssertionError(ex); |
| 924 | } |
| 925 | } |
| 926 | } |
| 927 | } |
| 928 | |
| 929 | /** |
| 930 | * Close closeable thing, swallowing errors. |
| 931 | */ |
| 932 | private static void safeClose(@Nullable Closeable thing) { |
| 933 | if (thing != null) { |
| 934 | try { |
| 935 | thing.close(); |
| 936 | } catch (IOException ex) { |
| 937 | Slog.w(TAG, "ignoring error closing resource: " + thing, ex); |
| 938 | } |
| 939 | } |
| 940 | } |
| 941 | |
Philip Cuadra | 7bd0fdd | 2016-04-28 15:26:49 -0700 | [diff] [blame] | 942 | private final class BinderService extends Binder { |
| 943 | @Override |
| 944 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 945 | if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return; |
Jorim Jaggi | 7119800f | 2018-07-09 17:57:10 +0200 | [diff] [blame] | 946 | synchronized (PinnerService.this) { |
| 947 | long totalSize = 0; |
| 948 | for (PinnedFile pinnedFile : mPinnedFiles) { |
| 949 | pw.format("%s %s\n", pinnedFile.fileName, pinnedFile.bytesPinned); |
| 950 | totalSize += pinnedFile.bytesPinned; |
| 951 | } |
| 952 | pw.println(); |
| 953 | for (int key : mPinnedApps.keySet()) { |
| 954 | PinnedApp app = mPinnedApps.get(key); |
| 955 | pw.print(getNameForKey(key)); |
| 956 | pw.print(" uid="); pw.print(app.uid); |
| 957 | pw.print(" active="); pw.print(app.active); |
| 958 | pw.println(); |
| 959 | for (PinnedFile pf : mPinnedApps.get(key).mFiles) { |
| 960 | pw.print(" "); pw.format("%s %s\n", pf.fileName, pf.bytesPinned); |
| 961 | totalSize += pf.bytesPinned; |
| 962 | } |
| 963 | } |
| 964 | pw.format("Total size: %s\n", totalSize); |
| 965 | pw.println(); |
| 966 | if (!mPendingRepin.isEmpty()) { |
| 967 | pw.print("Pending repin: "); |
| 968 | for (int key : mPendingRepin.values()) { |
| 969 | pw.print(getNameForKey(key)); pw.print(' '); |
| 970 | } |
| 971 | pw.println(); |
| 972 | } |
Philip Cuadra | 7cb2f8b | 2016-06-15 16:23:43 -0700 | [diff] [blame] | 973 | } |
| 974 | } |
| 975 | } |
| 976 | |
Daniel Colascione | 9779b5e | 2018-03-21 19:13:57 -0700 | [diff] [blame] | 977 | private static final class PinnedFile implements AutoCloseable { |
| 978 | private long mAddress; |
| 979 | final int mapSize; |
| 980 | final String fileName; |
| 981 | final int bytesPinned; |
Philip Cuadra | 7cb2f8b | 2016-06-15 16:23:43 -0700 | [diff] [blame] | 982 | |
Daniel Colascione | 9779b5e | 2018-03-21 19:13:57 -0700 | [diff] [blame] | 983 | PinnedFile(long address, int mapSize, String fileName, int bytesPinned) { |
Philip Cuadra | 7cb2f8b | 2016-06-15 16:23:43 -0700 | [diff] [blame] | 984 | mAddress = address; |
Daniel Colascione | 9779b5e | 2018-03-21 19:13:57 -0700 | [diff] [blame] | 985 | this.mapSize = mapSize; |
| 986 | this.fileName = fileName; |
| 987 | this.bytesPinned = bytesPinned; |
Philip Cuadra | 7bd0fdd | 2016-04-28 15:26:49 -0700 | [diff] [blame] | 988 | } |
Daniel Colascione | 9779b5e | 2018-03-21 19:13:57 -0700 | [diff] [blame] | 989 | |
| 990 | @Override |
| 991 | public void close() { |
| 992 | if (mAddress >= 0) { |
| 993 | safeMunmap(mAddress, mapSize); |
| 994 | mAddress = -1; |
| 995 | } |
| 996 | } |
| 997 | |
| 998 | @Override |
| 999 | public void finalize() { |
| 1000 | close(); |
| 1001 | } |
| 1002 | } |
| 1003 | |
| 1004 | final static class PinRange { |
| 1005 | int start; |
| 1006 | int length; |
Philip Cuadra | 7bd0fdd | 2016-04-28 15:26:49 -0700 | [diff] [blame] | 1007 | } |
Philip Cuadra | a95cea0 | 2016-07-06 16:00:32 -0700 | [diff] [blame] | 1008 | |
Jorim Jaggi | 7119800f | 2018-07-09 17:57:10 +0200 | [diff] [blame] | 1009 | /** |
| 1010 | * Represents an app that was pinned. |
| 1011 | */ |
| 1012 | private final class PinnedApp { |
| 1013 | |
| 1014 | /** |
| 1015 | * The uid of the package being pinned. This stays constant while the package stays |
| 1016 | * installed. |
| 1017 | */ |
| 1018 | final int uid; |
| 1019 | |
| 1020 | /** Whether it is currently active, i.e. there is a running process from that package. */ |
| 1021 | boolean active; |
| 1022 | |
| 1023 | /** List of pinned files. */ |
| 1024 | final ArrayList<PinnedFile> mFiles = new ArrayList<>(); |
| 1025 | |
| 1026 | private PinnedApp(ApplicationInfo appInfo) { |
| 1027 | uid = appInfo.uid; |
| 1028 | active = mAmInternal.isUidActive(uid); |
| 1029 | } |
| 1030 | } |
| 1031 | |
Philip Cuadra | a95cea0 | 2016-07-06 16:00:32 -0700 | [diff] [blame] | 1032 | final class PinnerHandler extends Handler { |
Philip Cuadra | a95cea0 | 2016-07-06 16:00:32 -0700 | [diff] [blame] | 1033 | static final int PIN_ONSTART_MSG = 4001; |
| 1034 | |
| 1035 | public PinnerHandler(Looper looper) { |
| 1036 | super(looper, null, true); |
| 1037 | } |
| 1038 | |
| 1039 | @Override |
| 1040 | public void handleMessage(Message msg) { |
| 1041 | switch (msg.what) { |
Philip Cuadra | a95cea0 | 2016-07-06 16:00:32 -0700 | [diff] [blame] | 1042 | case PIN_ONSTART_MSG: |
| 1043 | { |
| 1044 | handlePinOnStart(); |
| 1045 | } |
| 1046 | break; |
| 1047 | |
| 1048 | default: |
| 1049 | super.handleMessage(msg); |
| 1050 | } |
| 1051 | } |
| 1052 | } |
| 1053 | |
Philip Cuadra | 7bd0fdd | 2016-04-28 15:26:49 -0700 | [diff] [blame] | 1054 | } |