blob: 2869114601b7ae51929dee8a8be6457760067154 [file] [log] [blame]
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -07001/*
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
17package com.android.server;
18
Carmen Jacksonf107a232017-05-16 10:37:26 -070019import android.content.BroadcastReceiver;
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -070020import android.content.Context;
Jeff Sharkey847bd852016-08-03 17:20:03 -060021import android.content.Intent;
Carmen Jacksonf107a232017-05-16 10:37:26 -070022import android.content.IntentFilter;
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -070023import android.content.pm.ActivityInfo;
24import android.content.pm.ApplicationInfo;
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -070025import android.content.pm.PackageManager;
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -070026import android.content.pm.ResolveInfo;
Carmen Jacksonf107a232017-05-16 10:37:26 -070027import android.net.Uri;
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -070028import android.os.Binder;
29import android.os.Build;
Philip Cuadraa95cea02016-07-06 16:00:32 -070030import android.os.Handler;
31import android.os.Looper;
32import android.os.Message;
Jeff Sharkey847bd852016-08-03 17:20:03 -060033import android.os.UserHandle;
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -070034import android.provider.MediaStore;
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -070035import android.system.ErrnoException;
36import android.system.Os;
37import android.system.OsConstants;
38import android.system.StructStat;
Calin Juravle31ce3a82017-05-22 17:49:01 -070039import android.util.ArraySet;
Jeff Sharkey847bd852016-08-03 17:20:03 -060040import android.util.Slog;
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -070041
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -070042import com.android.internal.app.ResolverActivity;
Philip Cuadraa95cea02016-07-06 16:00:32 -070043import com.android.internal.os.BackgroundThread;
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -060044import com.android.internal.util.DumpUtils;
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -070045
Philip Cuadrad9bd8842016-07-12 17:29:38 -070046import dalvik.system.DexFile;
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -070047import dalvik.system.VMRuntime;
48
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -070049import java.io.FileDescriptor;
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -070050import java.io.IOException;
51import java.io.PrintWriter;
Jeff Sharkey847bd852016-08-03 17:20:03 -060052import java.util.ArrayList;
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -070053
54/**
55 * <p>PinnerService pins important files for key processes in memory.</p>
56 * <p>Files to pin are specified in the config_defaultPinnerServiceFiles
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -070057 * overlay.</p>
58 * <p>Pin the default camera application if specified in config_pinnerCameraApp.</p>
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -070059 */
60public final class PinnerService extends SystemService {
61 private static final boolean DEBUG = false;
62 private static final String TAG = "PinnerService";
63
64 private final Context mContext;
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -070065 private final ArrayList<PinnedFile> mPinnedFiles = new ArrayList<PinnedFile>();
66 private final ArrayList<PinnedFile> mPinnedCameraFiles = new ArrayList<PinnedFile>();
67 private final boolean mShouldPinCamera;
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -070068
69 private BinderService mBinderService;
70
Philip Cuadraac39b962016-11-02 11:59:22 -070071 private final long MAX_CAMERA_PIN_SIZE = 80 * (1 << 20); //80MB max
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -070072
Philip Cuadraa95cea02016-07-06 16:00:32 -070073 private PinnerHandler mPinnerHandler = null;
74
Carmen Jacksonf107a232017-05-16 10:37:26 -070075 private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
76 @Override
77 public void onReceive(Context context, Intent intent) {
78 // If this user's camera app has been updated, update pinned files accordingly.
79 if (intent.getAction() == Intent.ACTION_PACKAGE_REPLACED) {
80 Uri packageUri = intent.getData();
Calin Juravle31ce3a82017-05-22 17:49:01 -070081 String packageName = packageUri.getSchemeSpecificPart();
82 ArraySet<String> updatedPackages = new ArraySet<>();
83 updatedPackages.add(packageName);
84 update(updatedPackages);
Carmen Jacksonf107a232017-05-16 10:37:26 -070085 }
86 }
87 };
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -070088
89 public PinnerService(Context context) {
90 super(context);
91
92 mContext = context;
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -070093 mShouldPinCamera = context.getResources().getBoolean(
94 com.android.internal.R.bool.config_pinnerCameraApp);
Philip Cuadraa95cea02016-07-06 16:00:32 -070095 mPinnerHandler = new PinnerHandler(BackgroundThread.get().getLooper());
Carmen Jacksonf107a232017-05-16 10:37:26 -070096
97 IntentFilter filter = new IntentFilter();
98 filter.addAction(Intent.ACTION_PACKAGE_REPLACED);
99 filter.addDataScheme("package");
100 mContext.registerReceiver(mBroadcastReceiver, filter);
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700101 }
102
103 @Override
104 public void onStart() {
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700105 if (DEBUG) {
106 Slog.i(TAG, "Starting PinnerService");
107 }
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700108 mBinderService = new BinderService();
109 publishBinderService("pinner", mBinderService);
Carmen Jacksonf107a232017-05-16 10:37:26 -0700110 publishLocalService(PinnerService.class, this);
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700111
Jeff Sharkey847bd852016-08-03 17:20:03 -0600112 mPinnerHandler.obtainMessage(PinnerHandler.PIN_ONSTART_MSG).sendToTarget();
113 mPinnerHandler.obtainMessage(PinnerHandler.PIN_CAMERA_MSG, UserHandle.USER_SYSTEM, 0)
114 .sendToTarget();
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700115 }
116
117 /**
Philip Cuadraa95cea02016-07-06 16:00:32 -0700118 * Pin camera on user switch.
119 * If more than one user is using the device
120 * each user may set a different preference for the camera app.
121 * Make sure that user's preference is pinned into memory.
122 */
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700123 @Override
124 public void onSwitchUser(int userHandle) {
Jeff Sharkey847bd852016-08-03 17:20:03 -0600125 mPinnerHandler.obtainMessage(PinnerHandler.PIN_CAMERA_MSG, userHandle, 0).sendToTarget();
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700126 }
127
Philip Cuadraa95cea02016-07-06 16:00:32 -0700128 /**
Carmen Jacksonf107a232017-05-16 10:37:26 -0700129 * Update the currently pinned files.
130 * Specifically, this only updates camera pinning.
131 * The other files pinned in onStart will not need to be updated.
132 */
Calin Juravle31ce3a82017-05-22 17:49:01 -0700133 public void update(ArraySet<String> updatedPackages) {
134 ApplicationInfo cameraInfo = getCameraInfo(UserHandle.USER_SYSTEM);
135 if (cameraInfo != null && updatedPackages.contains(cameraInfo.packageName)) {
136 Slog.i(TAG, "Updating pinned files.");
137 mPinnerHandler.obtainMessage(PinnerHandler.PIN_CAMERA_MSG, UserHandle.USER_SYSTEM, 0)
138 .sendToTarget();
139 }
Carmen Jacksonf107a232017-05-16 10:37:26 -0700140 }
141
142 /**
Philip Cuadraa95cea02016-07-06 16:00:32 -0700143 * Handler for on start pinning message
144 */
145 private void handlePinOnStart() {
146 // Files to pin come from the overlay and can be specified per-device config
147 String[] filesToPin = mContext.getResources().getStringArray(
148 com.android.internal.R.array.config_defaultPinnerServiceFiles);
149 synchronized(this) {
150 // Continue trying to pin remaining files even if there is a failure
151 for (int i = 0; i < filesToPin.length; i++){
152 PinnedFile pf = pinFile(filesToPin[i], 0, 0, 0);
153 if (pf != null) {
154 mPinnedFiles.add(pf);
155 if (DEBUG) {
156 Slog.i(TAG, "Pinned file = " + pf.mFilename);
157 }
158 } else {
159 Slog.e(TAG, "Failed to pin file = " + filesToPin[i]);
160 }
161 }
162 }
163 }
164
165 /**
166 * Handler for camera pinning message
167 */
168 private void handlePinCamera(int userHandle) {
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700169 if (mShouldPinCamera) {
Philip Cuadraa95cea02016-07-06 16:00:32 -0700170 synchronized(this) {
171 boolean success = pinCamera(userHandle);
172 if (!success) {
173 //this is not necessarily an error
174 if (DEBUG) {
175 Slog.v(TAG, "Failed to pin camera.");
176 }
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700177 }
178 }
179 }
180 }
181
182 /**
183 * determine if the camera app is already pinned by comparing the
184 * intent resolution to the pinned files list
185 */
186 private boolean alreadyPinned(int userHandle) {
187 ApplicationInfo cameraInfo = getCameraInfo(userHandle);
188 if (cameraInfo == null ) {
189 return false;
190 }
191 for (int i = 0; i < mPinnedCameraFiles.size(); i++) {
192 if (mPinnedCameraFiles.get(i).mFilename.equals(cameraInfo.sourceDir)) {
193 if (DEBUG) {
194 Slog.v(TAG, "Camera is already pinned");
195 }
196 return true;
197 }
198 }
199 return false;
200 }
201
202 private void unpinCameraApp() {
203 for (int i = 0; i < mPinnedCameraFiles.size(); i++) {
204 unpinFile(mPinnedCameraFiles.get(i));
205 }
206 mPinnedCameraFiles.clear();
207 }
208
209 private boolean isResolverActivity(ActivityInfo info) {
210 return ResolverActivity.class.getName().equals(info.name);
211 }
212
213 private ApplicationInfo getCameraInfo(int userHandle) {
214 // find the camera via an intent
215 // use INTENT_ACTION_STILL_IMAGE_CAMERA instead of _SECURE. On a
216 // device without a fbe enabled, the _SECURE intent will never get set.
217 Intent cameraIntent = new Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA);
218 PackageManager pm = mContext.getPackageManager();
Jeff Sharkey847bd852016-08-03 17:20:03 -0600219 ResolveInfo cameraResolveInfo = pm.resolveActivityAsUser(cameraIntent,
220 PackageManager.MATCH_DEFAULT_ONLY | PackageManager.MATCH_DIRECT_BOOT_AWARE
221 | PackageManager.MATCH_DIRECT_BOOT_UNAWARE,
222 userHandle);
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700223 if (cameraResolveInfo == null ) {
224 //this is not necessarily an error
225 if (DEBUG) {
226 Slog.v(TAG, "Unable to resolve camera intent");
227 }
228 return null;
229 }
230
231 if (isResolverActivity(cameraResolveInfo.activityInfo))
232 {
Philip Cuadraac39b962016-11-02 11:59:22 -0700233 if (DEBUG) {
234 Slog.v(TAG, "cameraIntent returned resolverActivity");
235 }
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700236 return null;
237 }
238
239 return cameraResolveInfo.activityInfo.applicationInfo;
240 }
241
Carmen Jacksonf107a232017-05-16 10:37:26 -0700242 /**
243 * If the camera app is already pinned, unpin and repin it.
244 */
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700245 private boolean pinCamera(int userHandle){
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700246 ApplicationInfo cameraInfo = getCameraInfo(userHandle);
247 if (cameraInfo == null) {
248 return false;
249 }
250
251 //unpin after checking that the camera intent has resolved
252 //this prevents us from thrashing when switching users with
253 //FBE enabled, because the intent won't resolve until the unlock
254 unpinCameraApp();
255
256 //pin APK
257 String camAPK = cameraInfo.sourceDir;
258 PinnedFile pf = pinFile(camAPK, 0, 0, MAX_CAMERA_PIN_SIZE);
259 if (pf == null) {
260 Slog.e(TAG, "Failed to pin " + camAPK);
261 return false;
262 }
263 if (DEBUG) {
264 Slog.i(TAG, "Pinned " + pf.mFilename);
265 }
266 mPinnedCameraFiles.add(pf);
267
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700268 // determine the ABI from either ApplicationInfo or Build
269 String arch = "arm";
John Eckerdal60b07cd2016-11-03 14:04:47 +0100270 if (cameraInfo.primaryCpuAbi != null) {
271 if (VMRuntime.is64BitAbi(cameraInfo.primaryCpuAbi)) {
272 arch = arch + "64";
273 }
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700274 } else {
275 if (VMRuntime.is64BitAbi(Build.SUPPORTED_ABIS[0])) {
276 arch = arch + "64";
277 }
278 }
Philip Cuadrad9bd8842016-07-12 17:29:38 -0700279
280 // get the path to the odex or oat file
281 String baseCodePath = cameraInfo.getBaseCodePath();
Calin Juravle128721a2017-05-15 20:20:50 -0700282 String[] files = null;
Philip Cuadrad9bd8842016-07-12 17:29:38 -0700283 try {
Calin Juravle128721a2017-05-15 20:20:50 -0700284 files = DexFile.getDexFileOutputPaths(baseCodePath, arch);
Philip Cuadrad9bd8842016-07-12 17:29:38 -0700285 } catch (IOException ioe) {}
Calin Juravle128721a2017-05-15 20:20:50 -0700286 if (files == null) {
Philip Cuadrad9bd8842016-07-12 17:29:38 -0700287 return true;
288 }
289
290 //not pinning the oat/odex is not a fatal error
Calin Juravle128721a2017-05-15 20:20:50 -0700291 for (String file : files) {
292 pf = pinFile(file, 0, 0, MAX_CAMERA_PIN_SIZE);
293 if (pf != null) {
294 mPinnedCameraFiles.add(pf);
295 if (DEBUG) {
296 Slog.i(TAG, "Pinned " + pf.mFilename);
297 }
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700298 }
299 }
Philip Cuadrad9bd8842016-07-12 17:29:38 -0700300
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700301 return true;
302 }
303
304
305 /** mlock length bytes of fileToPin in memory, starting at offset
306 * length == 0 means pin from offset to end of file
307 * maxSize == 0 means infinite
308 */
309 private static PinnedFile pinFile(String fileToPin, long offset, long length, long maxSize) {
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700310 FileDescriptor fd = new FileDescriptor();
311 try {
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700312 fd = Os.open(fileToPin,
313 OsConstants.O_RDONLY | OsConstants.O_CLOEXEC | OsConstants.O_NOFOLLOW,
314 OsConstants.O_RDONLY);
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700315
316 StructStat sb = Os.fstat(fd);
317
318 if (offset + length > sb.st_size) {
319 Os.close(fd);
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700320 Slog.e(TAG, "Failed to pin file " + fileToPin +
321 ", request extends beyond end of file. offset + length = "
322 + (offset + length) + ", file length = " + sb.st_size);
323 return null;
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700324 }
325
326 if (length == 0) {
327 length = sb.st_size - offset;
328 }
329
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700330 if (maxSize > 0 && length > maxSize) {
331 Slog.e(TAG, "Could not pin file " + fileToPin +
332 ", size = " + length + ", maxSize = " + maxSize);
333 Os.close(fd);
334 return null;
335 }
336
337 long address = Os.mmap(0, length, OsConstants.PROT_READ,
338 OsConstants.MAP_PRIVATE, fd, offset);
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700339 Os.close(fd);
340
341 Os.mlock(address, length);
342
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700343 return new PinnedFile(address, length, fileToPin);
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700344 } catch (ErrnoException e) {
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700345 Slog.e(TAG, "Could not pin file " + fileToPin + " with error " + e.getMessage());
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700346 if(fd.valid()) {
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700347 try {
348 Os.close(fd);
349 }
350 catch (ErrnoException eClose) {
351 Slog.e(TAG, "Failed to close fd, error = " + eClose.getMessage());
352 }
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700353 }
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700354 return null;
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700355 }
356 }
357
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700358 private static boolean unpinFile(PinnedFile pf) {
359 try {
360 Os.munlock(pf.mAddress, pf.mLength);
361 } catch (ErrnoException e) {
362 Slog.e(TAG, "Failed to unpin file " + pf.mFilename + " with error " + e.getMessage());
363 return false;
364 }
365 if (DEBUG) {
366 Slog.i(TAG, "Unpinned file " + pf.mFilename );
367 }
368 return true;
369 }
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700370
371 private final class BinderService extends Binder {
372 @Override
373 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -0600374 if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return;
Tim Murrayf77f7242018-01-24 14:05:29 -0800375 long totalSize = 0;
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700376 pw.println("Pinned Files:");
Philip Cuadraa95cea02016-07-06 16:00:32 -0700377 synchronized(this) {
378 for (int i = 0; i < mPinnedFiles.size(); i++) {
379 pw.println(mPinnedFiles.get(i).mFilename);
Tim Murrayf77f7242018-01-24 14:05:29 -0800380 totalSize += mPinnedFiles.get(i).mLength;
Philip Cuadraa95cea02016-07-06 16:00:32 -0700381 }
382 for (int i = 0; i < mPinnedCameraFiles.size(); i++) {
383 pw.println(mPinnedCameraFiles.get(i).mFilename);
Tim Murrayf77f7242018-01-24 14:05:29 -0800384 totalSize += mPinnedCameraFiles.get(i).mLength;
Philip Cuadraa95cea02016-07-06 16:00:32 -0700385 }
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700386 }
Tim Murrayf77f7242018-01-24 14:05:29 -0800387 pw.println("Total size: " + totalSize);
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700388 }
389 }
390
391 private static class PinnedFile {
392 long mAddress;
393 long mLength;
394 String mFilename;
395
396 PinnedFile(long address, long length, String filename) {
397 mAddress = address;
398 mLength = length;
399 mFilename = filename;
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700400 }
401 }
Philip Cuadraa95cea02016-07-06 16:00:32 -0700402
403 final class PinnerHandler extends Handler {
404 static final int PIN_CAMERA_MSG = 4000;
405 static final int PIN_ONSTART_MSG = 4001;
406
407 public PinnerHandler(Looper looper) {
408 super(looper, null, true);
409 }
410
411 @Override
412 public void handleMessage(Message msg) {
413 switch (msg.what) {
414
415 case PIN_CAMERA_MSG:
416 {
417 handlePinCamera(msg.arg1);
418 }
419 break;
420
421 case PIN_ONSTART_MSG:
422 {
423 handlePinOnStart();
424 }
425 break;
426
427 default:
428 super.handleMessage(msg);
429 }
430 }
431 }
432
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700433}