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 | |
| 19 | import android.content.Context; |
Philip Cuadra | 7cb2f8b | 2016-06-15 16:23:43 -0700 | [diff] [blame] | 20 | import android.content.pm.ActivityInfo; |
| 21 | import android.content.pm.ApplicationInfo; |
| 22 | import android.content.pm.PackageInfo; |
Philip Cuadra | 7bd0fdd | 2016-04-28 15:26:49 -0700 | [diff] [blame] | 23 | import android.content.pm.PackageManager; |
Philip Cuadra | 7cb2f8b | 2016-06-15 16:23:43 -0700 | [diff] [blame] | 24 | import android.content.pm.ResolveInfo; |
Philip Cuadra | 7bd0fdd | 2016-04-28 15:26:49 -0700 | [diff] [blame] | 25 | import android.content.res.Resources; |
Philip Cuadra | 7cb2f8b | 2016-06-15 16:23:43 -0700 | [diff] [blame] | 26 | import android.content.Intent; |
Philip Cuadra | 7bd0fdd | 2016-04-28 15:26:49 -0700 | [diff] [blame] | 27 | import android.util.EventLog; |
| 28 | import android.util.Slog; |
| 29 | import android.os.Binder; |
| 30 | import android.os.Build; |
Philip Cuadra | a95cea0 | 2016-07-06 16:00:32 -0700 | [diff] [blame^] | 31 | import android.os.Handler; |
| 32 | import android.os.Looper; |
| 33 | import android.os.Message; |
| 34 | import android.os.Process; |
Philip Cuadra | 7cb2f8b | 2016-06-15 16:23:43 -0700 | [diff] [blame] | 35 | import android.provider.MediaStore; |
Philip Cuadra | 7bd0fdd | 2016-04-28 15:26:49 -0700 | [diff] [blame] | 36 | import android.system.ErrnoException; |
| 37 | import android.system.Os; |
| 38 | import android.system.OsConstants; |
| 39 | import android.system.StructStat; |
| 40 | |
Philip Cuadra | 7cb2f8b | 2016-06-15 16:23:43 -0700 | [diff] [blame] | 41 | import com.android.internal.app.ResolverActivity; |
Philip Cuadra | a95cea0 | 2016-07-06 16:00:32 -0700 | [diff] [blame^] | 42 | import com.android.internal.os.BackgroundThread; |
Philip Cuadra | 7cb2f8b | 2016-06-15 16:23:43 -0700 | [diff] [blame] | 43 | |
| 44 | import dalvik.system.VMRuntime; |
| 45 | |
Philip Cuadra | 7bd0fdd | 2016-04-28 15:26:49 -0700 | [diff] [blame] | 46 | import java.util.ArrayList; |
Philip Cuadra | 7cb2f8b | 2016-06-15 16:23:43 -0700 | [diff] [blame] | 47 | import java.util.List; |
Philip Cuadra | 7bd0fdd | 2016-04-28 15:26:49 -0700 | [diff] [blame] | 48 | import java.io.FileDescriptor; |
| 49 | import java.io.FileOutputStream; |
| 50 | import java.io.IOException; |
| 51 | import java.io.PrintWriter; |
| 52 | |
| 53 | /** |
| 54 | * <p>PinnerService pins important files for key processes in memory.</p> |
| 55 | * <p>Files to pin are specified in the config_defaultPinnerServiceFiles |
Philip Cuadra | 7cb2f8b | 2016-06-15 16:23:43 -0700 | [diff] [blame] | 56 | * overlay.</p> |
| 57 | * <p>Pin the default camera application if specified in config_pinnerCameraApp.</p> |
Philip Cuadra | 7bd0fdd | 2016-04-28 15:26:49 -0700 | [diff] [blame] | 58 | */ |
| 59 | public final class PinnerService extends SystemService { |
| 60 | private static final boolean DEBUG = false; |
| 61 | private static final String TAG = "PinnerService"; |
| 62 | |
| 63 | private final Context mContext; |
Philip Cuadra | 7cb2f8b | 2016-06-15 16:23:43 -0700 | [diff] [blame] | 64 | private final ArrayList<PinnedFile> mPinnedFiles = new ArrayList<PinnedFile>(); |
| 65 | private final ArrayList<PinnedFile> mPinnedCameraFiles = new ArrayList<PinnedFile>(); |
| 66 | private final boolean mShouldPinCamera; |
Philip Cuadra | 7bd0fdd | 2016-04-28 15:26:49 -0700 | [diff] [blame] | 67 | |
| 68 | private BinderService mBinderService; |
| 69 | |
Philip Cuadra | 7cb2f8b | 2016-06-15 16:23:43 -0700 | [diff] [blame] | 70 | private final long MAX_CAMERA_PIN_SIZE = 50 * (1 << 20); //50MB max |
| 71 | |
Philip Cuadra | a95cea0 | 2016-07-06 16:00:32 -0700 | [diff] [blame^] | 72 | private PinnerHandler mPinnerHandler = null; |
| 73 | |
Philip Cuadra | 7bd0fdd | 2016-04-28 15:26:49 -0700 | [diff] [blame] | 74 | |
| 75 | public PinnerService(Context context) { |
| 76 | super(context); |
| 77 | |
| 78 | mContext = context; |
Philip Cuadra | 7cb2f8b | 2016-06-15 16:23:43 -0700 | [diff] [blame] | 79 | mShouldPinCamera = context.getResources().getBoolean( |
| 80 | com.android.internal.R.bool.config_pinnerCameraApp); |
Philip Cuadra | a95cea0 | 2016-07-06 16:00:32 -0700 | [diff] [blame^] | 81 | mPinnerHandler = new PinnerHandler(BackgroundThread.get().getLooper()); |
Philip Cuadra | 7bd0fdd | 2016-04-28 15:26:49 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public void onStart() { |
Philip Cuadra | 7cb2f8b | 2016-06-15 16:23:43 -0700 | [diff] [blame] | 86 | if (DEBUG) { |
| 87 | Slog.i(TAG, "Starting PinnerService"); |
| 88 | } |
Philip Cuadra | 7bd0fdd | 2016-04-28 15:26:49 -0700 | [diff] [blame] | 89 | mBinderService = new BinderService(); |
| 90 | publishBinderService("pinner", mBinderService); |
Philip Cuadra | a95cea0 | 2016-07-06 16:00:32 -0700 | [diff] [blame^] | 91 | mPinnerHandler.sendMessage( |
| 92 | mPinnerHandler.obtainMessage(PinnerHandler.PIN_ONSTART_MSG)); |
Philip Cuadra | 7bd0fdd | 2016-04-28 15:26:49 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Philip Cuadra | 7cb2f8b | 2016-06-15 16:23:43 -0700 | [diff] [blame] | 95 | /** |
| 96 | * Pin camera on unlock. |
| 97 | * We have to wait for unlock because the user's |
| 98 | * preference for camera is not available from PackageManager until after |
| 99 | * unlock |
| 100 | */ |
| 101 | @Override |
| 102 | public void onUnlockUser(int userHandle) { |
Philip Cuadra | a95cea0 | 2016-07-06 16:00:32 -0700 | [diff] [blame^] | 103 | mPinnerHandler.sendMessage( |
| 104 | mPinnerHandler.obtainMessage(PinnerHandler.PIN_CAMERA_MSG, userHandle, 0)); |
Philip Cuadra | 7cb2f8b | 2016-06-15 16:23:43 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /** |
Philip Cuadra | a95cea0 | 2016-07-06 16:00:32 -0700 | [diff] [blame^] | 108 | * Pin camera on user switch. |
| 109 | * If more than one user is using the device |
| 110 | * each user may set a different preference for the camera app. |
| 111 | * Make sure that user's preference is pinned into memory. |
| 112 | */ |
Philip Cuadra | 7cb2f8b | 2016-06-15 16:23:43 -0700 | [diff] [blame] | 113 | @Override |
| 114 | public void onSwitchUser(int userHandle) { |
Philip Cuadra | a95cea0 | 2016-07-06 16:00:32 -0700 | [diff] [blame^] | 115 | mPinnerHandler.sendMessage( |
| 116 | mPinnerHandler.obtainMessage(PinnerHandler.PIN_CAMERA_MSG, userHandle, 0)); |
Philip Cuadra | 7cb2f8b | 2016-06-15 16:23:43 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Philip Cuadra | a95cea0 | 2016-07-06 16:00:32 -0700 | [diff] [blame^] | 119 | /** |
| 120 | * Handler for on start pinning message |
| 121 | */ |
| 122 | private void handlePinOnStart() { |
| 123 | // Files to pin come from the overlay and can be specified per-device config |
| 124 | String[] filesToPin = mContext.getResources().getStringArray( |
| 125 | com.android.internal.R.array.config_defaultPinnerServiceFiles); |
| 126 | synchronized(this) { |
| 127 | // Continue trying to pin remaining files even if there is a failure |
| 128 | for (int i = 0; i < filesToPin.length; i++){ |
| 129 | PinnedFile pf = pinFile(filesToPin[i], 0, 0, 0); |
| 130 | if (pf != null) { |
| 131 | mPinnedFiles.add(pf); |
| 132 | if (DEBUG) { |
| 133 | Slog.i(TAG, "Pinned file = " + pf.mFilename); |
| 134 | } |
| 135 | } else { |
| 136 | Slog.e(TAG, "Failed to pin file = " + filesToPin[i]); |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Handler for camera pinning message |
| 144 | */ |
| 145 | private void handlePinCamera(int userHandle) { |
Philip Cuadra | 7cb2f8b | 2016-06-15 16:23:43 -0700 | [diff] [blame] | 146 | if (mShouldPinCamera) { |
Philip Cuadra | a95cea0 | 2016-07-06 16:00:32 -0700 | [diff] [blame^] | 147 | synchronized(this) { |
| 148 | boolean success = pinCamera(userHandle); |
| 149 | if (!success) { |
| 150 | //this is not necessarily an error |
| 151 | if (DEBUG) { |
| 152 | Slog.v(TAG, "Failed to pin camera."); |
| 153 | } |
Philip Cuadra | 7cb2f8b | 2016-06-15 16:23:43 -0700 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * determine if the camera app is already pinned by comparing the |
| 161 | * intent resolution to the pinned files list |
| 162 | */ |
| 163 | private boolean alreadyPinned(int userHandle) { |
| 164 | ApplicationInfo cameraInfo = getCameraInfo(userHandle); |
| 165 | if (cameraInfo == null ) { |
| 166 | return false; |
| 167 | } |
| 168 | for (int i = 0; i < mPinnedCameraFiles.size(); i++) { |
| 169 | if (mPinnedCameraFiles.get(i).mFilename.equals(cameraInfo.sourceDir)) { |
| 170 | if (DEBUG) { |
| 171 | Slog.v(TAG, "Camera is already pinned"); |
| 172 | } |
| 173 | return true; |
| 174 | } |
| 175 | } |
| 176 | return false; |
| 177 | } |
| 178 | |
| 179 | private void unpinCameraApp() { |
| 180 | for (int i = 0; i < mPinnedCameraFiles.size(); i++) { |
| 181 | unpinFile(mPinnedCameraFiles.get(i)); |
| 182 | } |
| 183 | mPinnedCameraFiles.clear(); |
| 184 | } |
| 185 | |
| 186 | private boolean isResolverActivity(ActivityInfo info) { |
| 187 | return ResolverActivity.class.getName().equals(info.name); |
| 188 | } |
| 189 | |
| 190 | private ApplicationInfo getCameraInfo(int userHandle) { |
| 191 | // find the camera via an intent |
| 192 | // use INTENT_ACTION_STILL_IMAGE_CAMERA instead of _SECURE. On a |
| 193 | // device without a fbe enabled, the _SECURE intent will never get set. |
| 194 | Intent cameraIntent = new Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA); |
| 195 | PackageManager pm = mContext.getPackageManager(); |
| 196 | ResolveInfo cameraResolveInfo = pm.resolveActivityAsUser( |
| 197 | cameraIntent, PackageManager.MATCH_DEFAULT_ONLY, userHandle); |
| 198 | if (cameraResolveInfo == null ) { |
| 199 | //this is not necessarily an error |
| 200 | if (DEBUG) { |
| 201 | Slog.v(TAG, "Unable to resolve camera intent"); |
| 202 | } |
| 203 | return null; |
| 204 | } |
| 205 | |
| 206 | if (isResolverActivity(cameraResolveInfo.activityInfo)) |
| 207 | { |
| 208 | return null; |
| 209 | } |
| 210 | |
| 211 | return cameraResolveInfo.activityInfo.applicationInfo; |
| 212 | } |
| 213 | |
| 214 | private boolean pinCamera(int userHandle){ |
| 215 | //we may have already pinned a camera app. If we've pinned this |
| 216 | //camera app, we're done. otherwise, unpin and pin the new app |
| 217 | if (alreadyPinned(userHandle)){ |
| 218 | return true; |
| 219 | } |
| 220 | |
| 221 | ApplicationInfo cameraInfo = getCameraInfo(userHandle); |
| 222 | if (cameraInfo == null) { |
| 223 | return false; |
| 224 | } |
| 225 | |
| 226 | //unpin after checking that the camera intent has resolved |
| 227 | //this prevents us from thrashing when switching users with |
| 228 | //FBE enabled, because the intent won't resolve until the unlock |
| 229 | unpinCameraApp(); |
| 230 | |
| 231 | //pin APK |
| 232 | String camAPK = cameraInfo.sourceDir; |
| 233 | PinnedFile pf = pinFile(camAPK, 0, 0, MAX_CAMERA_PIN_SIZE); |
| 234 | if (pf == null) { |
| 235 | Slog.e(TAG, "Failed to pin " + camAPK); |
| 236 | return false; |
| 237 | } |
| 238 | if (DEBUG) { |
| 239 | Slog.i(TAG, "Pinned " + pf.mFilename); |
| 240 | } |
| 241 | mPinnedCameraFiles.add(pf); |
| 242 | |
| 243 | //find the location of the odex based on the location of the APK |
| 244 | int lastPeriod = camAPK.lastIndexOf('.'); |
| 245 | int lastSlash = camAPK.lastIndexOf('/', lastPeriod); |
| 246 | String base = camAPK.substring(0, lastSlash); |
| 247 | String appName = camAPK.substring(lastSlash + 1, lastPeriod); |
| 248 | |
| 249 | // determine the ABI from either ApplicationInfo or Build |
| 250 | String arch = "arm"; |
| 251 | if (cameraInfo.primaryCpuAbi != null |
| 252 | && VMRuntime.is64BitAbi(cameraInfo.primaryCpuAbi)) { |
| 253 | arch = arch + "64"; |
| 254 | } else { |
| 255 | if (VMRuntime.is64BitAbi(Build.SUPPORTED_ABIS[0])) { |
| 256 | arch = arch + "64"; |
| 257 | } |
| 258 | } |
| 259 | String odex = base + "/oat/" + arch + "/" + appName + ".odex"; |
| 260 | //not all apps have odex files, so not pinning the odex is not a fatal error |
| 261 | pf = pinFile(odex, 0, 0, MAX_CAMERA_PIN_SIZE); |
| 262 | if (pf != null) { |
| 263 | mPinnedCameraFiles.add(pf); |
| 264 | if (DEBUG) { |
| 265 | Slog.i(TAG, "Pinned " + pf.mFilename); |
| 266 | } |
| 267 | } |
| 268 | return true; |
| 269 | } |
| 270 | |
| 271 | |
| 272 | /** mlock length bytes of fileToPin in memory, starting at offset |
| 273 | * length == 0 means pin from offset to end of file |
| 274 | * maxSize == 0 means infinite |
| 275 | */ |
| 276 | private static PinnedFile pinFile(String fileToPin, long offset, long length, long maxSize) { |
Philip Cuadra | 7bd0fdd | 2016-04-28 15:26:49 -0700 | [diff] [blame] | 277 | FileDescriptor fd = new FileDescriptor(); |
| 278 | try { |
Philip Cuadra | 7cb2f8b | 2016-06-15 16:23:43 -0700 | [diff] [blame] | 279 | fd = Os.open(fileToPin, |
| 280 | OsConstants.O_RDONLY | OsConstants.O_CLOEXEC | OsConstants.O_NOFOLLOW, |
| 281 | OsConstants.O_RDONLY); |
Philip Cuadra | 7bd0fdd | 2016-04-28 15:26:49 -0700 | [diff] [blame] | 282 | |
| 283 | StructStat sb = Os.fstat(fd); |
| 284 | |
| 285 | if (offset + length > sb.st_size) { |
| 286 | Os.close(fd); |
Philip Cuadra | 7cb2f8b | 2016-06-15 16:23:43 -0700 | [diff] [blame] | 287 | Slog.e(TAG, "Failed to pin file " + fileToPin + |
| 288 | ", request extends beyond end of file. offset + length = " |
| 289 | + (offset + length) + ", file length = " + sb.st_size); |
| 290 | return null; |
Philip Cuadra | 7bd0fdd | 2016-04-28 15:26:49 -0700 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | if (length == 0) { |
| 294 | length = sb.st_size - offset; |
| 295 | } |
| 296 | |
Philip Cuadra | 7cb2f8b | 2016-06-15 16:23:43 -0700 | [diff] [blame] | 297 | if (maxSize > 0 && length > maxSize) { |
| 298 | Slog.e(TAG, "Could not pin file " + fileToPin + |
| 299 | ", size = " + length + ", maxSize = " + maxSize); |
| 300 | Os.close(fd); |
| 301 | return null; |
| 302 | } |
| 303 | |
| 304 | long address = Os.mmap(0, length, OsConstants.PROT_READ, |
| 305 | OsConstants.MAP_PRIVATE, fd, offset); |
Philip Cuadra | 7bd0fdd | 2016-04-28 15:26:49 -0700 | [diff] [blame] | 306 | Os.close(fd); |
| 307 | |
| 308 | Os.mlock(address, length); |
| 309 | |
Philip Cuadra | 7cb2f8b | 2016-06-15 16:23:43 -0700 | [diff] [blame] | 310 | return new PinnedFile(address, length, fileToPin); |
Philip Cuadra | 7bd0fdd | 2016-04-28 15:26:49 -0700 | [diff] [blame] | 311 | } catch (ErrnoException e) { |
Philip Cuadra | 7cb2f8b | 2016-06-15 16:23:43 -0700 | [diff] [blame] | 312 | Slog.e(TAG, "Could not pin file " + fileToPin + " with error " + e.getMessage()); |
Philip Cuadra | 7bd0fdd | 2016-04-28 15:26:49 -0700 | [diff] [blame] | 313 | if(fd.valid()) { |
Philip Cuadra | 7cb2f8b | 2016-06-15 16:23:43 -0700 | [diff] [blame] | 314 | try { |
| 315 | Os.close(fd); |
| 316 | } |
| 317 | catch (ErrnoException eClose) { |
| 318 | Slog.e(TAG, "Failed to close fd, error = " + eClose.getMessage()); |
| 319 | } |
Philip Cuadra | 7bd0fdd | 2016-04-28 15:26:49 -0700 | [diff] [blame] | 320 | } |
Philip Cuadra | 7cb2f8b | 2016-06-15 16:23:43 -0700 | [diff] [blame] | 321 | return null; |
Philip Cuadra | 7bd0fdd | 2016-04-28 15:26:49 -0700 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | |
Philip Cuadra | 7cb2f8b | 2016-06-15 16:23:43 -0700 | [diff] [blame] | 325 | private static boolean unpinFile(PinnedFile pf) { |
| 326 | try { |
| 327 | Os.munlock(pf.mAddress, pf.mLength); |
| 328 | } catch (ErrnoException e) { |
| 329 | Slog.e(TAG, "Failed to unpin file " + pf.mFilename + " with error " + e.getMessage()); |
| 330 | return false; |
| 331 | } |
| 332 | if (DEBUG) { |
| 333 | Slog.i(TAG, "Unpinned file " + pf.mFilename ); |
| 334 | } |
| 335 | return true; |
| 336 | } |
Philip Cuadra | 7bd0fdd | 2016-04-28 15:26:49 -0700 | [diff] [blame] | 337 | |
| 338 | private final class BinderService extends Binder { |
| 339 | @Override |
| 340 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 341 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG); |
| 342 | pw.println("Pinned Files:"); |
Philip Cuadra | a95cea0 | 2016-07-06 16:00:32 -0700 | [diff] [blame^] | 343 | synchronized(this) { |
| 344 | for (int i = 0; i < mPinnedFiles.size(); i++) { |
| 345 | pw.println(mPinnedFiles.get(i).mFilename); |
| 346 | } |
| 347 | for (int i = 0; i < mPinnedCameraFiles.size(); i++) { |
| 348 | pw.println(mPinnedCameraFiles.get(i).mFilename); |
| 349 | } |
Philip Cuadra | 7cb2f8b | 2016-06-15 16:23:43 -0700 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | private static class PinnedFile { |
| 355 | long mAddress; |
| 356 | long mLength; |
| 357 | String mFilename; |
| 358 | |
| 359 | PinnedFile(long address, long length, String filename) { |
| 360 | mAddress = address; |
| 361 | mLength = length; |
| 362 | mFilename = filename; |
Philip Cuadra | 7bd0fdd | 2016-04-28 15:26:49 -0700 | [diff] [blame] | 363 | } |
| 364 | } |
Philip Cuadra | a95cea0 | 2016-07-06 16:00:32 -0700 | [diff] [blame^] | 365 | |
| 366 | final class PinnerHandler extends Handler { |
| 367 | static final int PIN_CAMERA_MSG = 4000; |
| 368 | static final int PIN_ONSTART_MSG = 4001; |
| 369 | |
| 370 | public PinnerHandler(Looper looper) { |
| 371 | super(looper, null, true); |
| 372 | } |
| 373 | |
| 374 | @Override |
| 375 | public void handleMessage(Message msg) { |
| 376 | switch (msg.what) { |
| 377 | |
| 378 | case PIN_CAMERA_MSG: |
| 379 | { |
| 380 | handlePinCamera(msg.arg1); |
| 381 | } |
| 382 | break; |
| 383 | |
| 384 | case PIN_ONSTART_MSG: |
| 385 | { |
| 386 | handlePinOnStart(); |
| 387 | } |
| 388 | break; |
| 389 | |
| 390 | default: |
| 391 | super.handleMessage(msg); |
| 392 | } |
| 393 | } |
| 394 | } |
| 395 | |
Philip Cuadra | 7bd0fdd | 2016-04-28 15:26:49 -0700 | [diff] [blame] | 396 | } |