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