blob: f8baf17664e221e2e4f421e1efaf46603619f013 [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;
Jeff Sharkey847bd852016-08-03 17:20:03 -060039import android.util.Slog;
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -070040
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -070041import com.android.internal.app.ResolverActivity;
Philip Cuadraa95cea02016-07-06 16:00:32 -070042import com.android.internal.os.BackgroundThread;
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -060043import com.android.internal.util.DumpUtils;
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -070044
Philip Cuadrad9bd8842016-07-12 17:29:38 -070045import dalvik.system.DexFile;
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -070046import dalvik.system.VMRuntime;
47
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -070048import java.io.FileDescriptor;
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -070049import java.io.IOException;
50import java.io.PrintWriter;
Jeff Sharkey847bd852016-08-03 17:20:03 -060051import java.util.ArrayList;
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -070052
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 Cuadra7cb2f8b2016-06-15 16:23:43 -070056 * overlay.</p>
57 * <p>Pin the default camera application if specified in config_pinnerCameraApp.</p>
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -070058 */
59public 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 Cuadra7cb2f8b2016-06-15 16:23:43 -070064 private final ArrayList<PinnedFile> mPinnedFiles = new ArrayList<PinnedFile>();
65 private final ArrayList<PinnedFile> mPinnedCameraFiles = new ArrayList<PinnedFile>();
66 private final boolean mShouldPinCamera;
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -070067
68 private BinderService mBinderService;
69
Philip Cuadraac39b962016-11-02 11:59:22 -070070 private final long MAX_CAMERA_PIN_SIZE = 80 * (1 << 20); //80MB max
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -070071
Philip Cuadraa95cea02016-07-06 16:00:32 -070072 private PinnerHandler mPinnerHandler = null;
73
Carmen Jacksonf107a232017-05-16 10:37:26 -070074 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 Cuadra7bd0fdd2016-04-28 15:26:49 -070087
88 public PinnerService(Context context) {
89 super(context);
90
91 mContext = context;
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -070092 mShouldPinCamera = context.getResources().getBoolean(
93 com.android.internal.R.bool.config_pinnerCameraApp);
Philip Cuadraa95cea02016-07-06 16:00:32 -070094 mPinnerHandler = new PinnerHandler(BackgroundThread.get().getLooper());
Carmen Jacksonf107a232017-05-16 10:37:26 -070095
96 IntentFilter filter = new IntentFilter();
97 filter.addAction(Intent.ACTION_PACKAGE_REPLACED);
98 filter.addDataScheme("package");
99 mContext.registerReceiver(mBroadcastReceiver, filter);
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700100 }
101
102 @Override
103 public void onStart() {
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700104 if (DEBUG) {
105 Slog.i(TAG, "Starting PinnerService");
106 }
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700107 mBinderService = new BinderService();
108 publishBinderService("pinner", mBinderService);
Carmen Jacksonf107a232017-05-16 10:37:26 -0700109 publishLocalService(PinnerService.class, this);
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700110
Jeff Sharkey847bd852016-08-03 17:20:03 -0600111 mPinnerHandler.obtainMessage(PinnerHandler.PIN_ONSTART_MSG).sendToTarget();
112 mPinnerHandler.obtainMessage(PinnerHandler.PIN_CAMERA_MSG, UserHandle.USER_SYSTEM, 0)
113 .sendToTarget();
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700114 }
115
116 /**
Philip Cuadraa95cea02016-07-06 16:00:32 -0700117 * 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 Cuadra7cb2f8b2016-06-15 16:23:43 -0700122 @Override
123 public void onSwitchUser(int userHandle) {
Jeff Sharkey847bd852016-08-03 17:20:03 -0600124 mPinnerHandler.obtainMessage(PinnerHandler.PIN_CAMERA_MSG, userHandle, 0).sendToTarget();
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700125 }
126
Philip Cuadraa95cea02016-07-06 16:00:32 -0700127 /**
Carmen Jacksonf107a232017-05-16 10:37:26 -0700128 * 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 Cuadraa95cea02016-07-06 16:00:32 -0700139 * 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 Cuadra7cb2f8b2016-06-15 16:23:43 -0700165 if (mShouldPinCamera) {
Philip Cuadraa95cea02016-07-06 16:00:32 -0700166 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 Cuadra7cb2f8b2016-06-15 16:23:43 -0700173 }
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 Sharkey847bd852016-08-03 17:20:03 -0600215 ResolveInfo cameraResolveInfo = pm.resolveActivityAsUser(cameraIntent,
216 PackageManager.MATCH_DEFAULT_ONLY | PackageManager.MATCH_DIRECT_BOOT_AWARE
217 | PackageManager.MATCH_DIRECT_BOOT_UNAWARE,
218 userHandle);
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700219 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 Cuadraac39b962016-11-02 11:59:22 -0700229 if (DEBUG) {
230 Slog.v(TAG, "cameraIntent returned resolverActivity");
231 }
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700232 return null;
233 }
234
235 return cameraResolveInfo.activityInfo.applicationInfo;
236 }
237
Carmen Jacksonf107a232017-05-16 10:37:26 -0700238 /**
239 * If the camera app is already pinned, unpin and repin it.
240 */
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700241 private boolean pinCamera(int userHandle){
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700242 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 Cuadra7cb2f8b2016-06-15 16:23:43 -0700264 // 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 Cuadrad9bd8842016-07-12 17:29:38 -0700274
275 // get the path to the odex or oat file
276 String baseCodePath = cameraInfo.getBaseCodePath();
Calin Juravle128721a2017-05-15 20:20:50 -0700277 String[] files = null;
Philip Cuadrad9bd8842016-07-12 17:29:38 -0700278 try {
Calin Juravle128721a2017-05-15 20:20:50 -0700279 files = DexFile.getDexFileOutputPaths(baseCodePath, arch);
Philip Cuadrad9bd8842016-07-12 17:29:38 -0700280 } catch (IOException ioe) {}
Calin Juravle128721a2017-05-15 20:20:50 -0700281 if (files == null) {
Philip Cuadrad9bd8842016-07-12 17:29:38 -0700282 return true;
283 }
284
285 //not pinning the oat/odex is not a fatal error
Calin Juravle128721a2017-05-15 20:20:50 -0700286 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 Cuadra7cb2f8b2016-06-15 16:23:43 -0700293 }
294 }
Philip Cuadrad9bd8842016-07-12 17:29:38 -0700295
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700296 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 Cuadra7bd0fdd2016-04-28 15:26:49 -0700305 FileDescriptor fd = new FileDescriptor();
306 try {
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700307 fd = Os.open(fileToPin,
308 OsConstants.O_RDONLY | OsConstants.O_CLOEXEC | OsConstants.O_NOFOLLOW,
309 OsConstants.O_RDONLY);
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700310
311 StructStat sb = Os.fstat(fd);
312
313 if (offset + length > sb.st_size) {
314 Os.close(fd);
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700315 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 Cuadra7bd0fdd2016-04-28 15:26:49 -0700319 }
320
321 if (length == 0) {
322 length = sb.st_size - offset;
323 }
324
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700325 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 Cuadra7bd0fdd2016-04-28 15:26:49 -0700334 Os.close(fd);
335
336 Os.mlock(address, length);
337
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700338 return new PinnedFile(address, length, fileToPin);
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700339 } catch (ErrnoException e) {
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700340 Slog.e(TAG, "Could not pin file " + fileToPin + " with error " + e.getMessage());
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700341 if(fd.valid()) {
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700342 try {
343 Os.close(fd);
344 }
345 catch (ErrnoException eClose) {
346 Slog.e(TAG, "Failed to close fd, error = " + eClose.getMessage());
347 }
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700348 }
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700349 return null;
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700350 }
351 }
352
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700353 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 Cuadra7bd0fdd2016-04-28 15:26:49 -0700365
366 private final class BinderService extends Binder {
367 @Override
368 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -0600369 if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return;
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700370 pw.println("Pinned Files:");
Philip Cuadraa95cea02016-07-06 16:00:32 -0700371 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 Cuadra7cb2f8b2016-06-15 16:23:43 -0700378 }
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 Cuadra7bd0fdd2016-04-28 15:26:49 -0700391 }
392 }
Philip Cuadraa95cea02016-07-06 16:00:32 -0700393
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 Cuadra7bd0fdd2016-04-28 15:26:49 -0700424}