blob: fa5a52c712cd9c8a24f8034bbe2aed46a61ea7c3 [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
19import android.content.Context;
Jeff Sharkey847bd852016-08-03 17:20:03 -060020import android.content.Intent;
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -070021import android.content.pm.ActivityInfo;
22import android.content.pm.ApplicationInfo;
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -070023import android.content.pm.PackageManager;
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -070024import android.content.pm.ResolveInfo;
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -070025import android.os.Binder;
26import android.os.Build;
Philip Cuadraa95cea02016-07-06 16:00:32 -070027import android.os.Handler;
28import android.os.Looper;
29import android.os.Message;
Jeff Sharkey847bd852016-08-03 17:20:03 -060030import android.os.UserHandle;
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -070031import android.provider.MediaStore;
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -070032import android.system.ErrnoException;
33import android.system.Os;
34import android.system.OsConstants;
35import android.system.StructStat;
Jeff Sharkey847bd852016-08-03 17:20:03 -060036import android.util.Slog;
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -070037
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -070038import com.android.internal.app.ResolverActivity;
Philip Cuadraa95cea02016-07-06 16:00:32 -070039import com.android.internal.os.BackgroundThread;
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -070040
Philip Cuadrad9bd8842016-07-12 17:29:38 -070041import dalvik.system.DexFile;
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -070042import dalvik.system.VMRuntime;
43
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -070044import java.io.FileDescriptor;
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -070045import java.io.IOException;
46import java.io.PrintWriter;
Jeff Sharkey847bd852016-08-03 17:20:03 -060047import java.util.ArrayList;
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -070048
49/**
50 * <p>PinnerService pins important files for key processes in memory.</p>
51 * <p>Files to pin are specified in the config_defaultPinnerServiceFiles
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -070052 * overlay.</p>
53 * <p>Pin the default camera application if specified in config_pinnerCameraApp.</p>
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -070054 */
55public final class PinnerService extends SystemService {
56 private static final boolean DEBUG = false;
57 private static final String TAG = "PinnerService";
58
59 private final Context mContext;
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -070060 private final ArrayList<PinnedFile> mPinnedFiles = new ArrayList<PinnedFile>();
61 private final ArrayList<PinnedFile> mPinnedCameraFiles = new ArrayList<PinnedFile>();
62 private final boolean mShouldPinCamera;
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -070063
64 private BinderService mBinderService;
65
Philip Cuadraac39b962016-11-02 11:59:22 -070066 private final long MAX_CAMERA_PIN_SIZE = 80 * (1 << 20); //80MB max
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -070067
Philip Cuadraa95cea02016-07-06 16:00:32 -070068 private PinnerHandler mPinnerHandler = null;
69
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -070070
71 public PinnerService(Context context) {
72 super(context);
73
74 mContext = context;
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -070075 mShouldPinCamera = context.getResources().getBoolean(
76 com.android.internal.R.bool.config_pinnerCameraApp);
Philip Cuadraa95cea02016-07-06 16:00:32 -070077 mPinnerHandler = new PinnerHandler(BackgroundThread.get().getLooper());
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -070078 }
79
80 @Override
81 public void onStart() {
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -070082 if (DEBUG) {
83 Slog.i(TAG, "Starting PinnerService");
84 }
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -070085 mBinderService = new BinderService();
86 publishBinderService("pinner", mBinderService);
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -070087
Jeff Sharkey847bd852016-08-03 17:20:03 -060088 mPinnerHandler.obtainMessage(PinnerHandler.PIN_ONSTART_MSG).sendToTarget();
89 mPinnerHandler.obtainMessage(PinnerHandler.PIN_CAMERA_MSG, UserHandle.USER_SYSTEM, 0)
90 .sendToTarget();
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -070091 }
92
93 /**
Philip Cuadraa95cea02016-07-06 16:00:32 -070094 * Pin camera on user switch.
95 * If more than one user is using the device
96 * each user may set a different preference for the camera app.
97 * Make sure that user's preference is pinned into memory.
98 */
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -070099 @Override
100 public void onSwitchUser(int userHandle) {
Jeff Sharkey847bd852016-08-03 17:20:03 -0600101 mPinnerHandler.obtainMessage(PinnerHandler.PIN_CAMERA_MSG, userHandle, 0).sendToTarget();
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700102 }
103
Philip Cuadraa95cea02016-07-06 16:00:32 -0700104 /**
105 * Handler for on start pinning message
106 */
107 private void handlePinOnStart() {
108 // Files to pin come from the overlay and can be specified per-device config
109 String[] filesToPin = mContext.getResources().getStringArray(
110 com.android.internal.R.array.config_defaultPinnerServiceFiles);
111 synchronized(this) {
112 // Continue trying to pin remaining files even if there is a failure
113 for (int i = 0; i < filesToPin.length; i++){
114 PinnedFile pf = pinFile(filesToPin[i], 0, 0, 0);
115 if (pf != null) {
116 mPinnedFiles.add(pf);
117 if (DEBUG) {
118 Slog.i(TAG, "Pinned file = " + pf.mFilename);
119 }
120 } else {
121 Slog.e(TAG, "Failed to pin file = " + filesToPin[i]);
122 }
123 }
124 }
125 }
126
127 /**
128 * Handler for camera pinning message
129 */
130 private void handlePinCamera(int userHandle) {
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700131 if (mShouldPinCamera) {
Philip Cuadraa95cea02016-07-06 16:00:32 -0700132 synchronized(this) {
133 boolean success = pinCamera(userHandle);
134 if (!success) {
135 //this is not necessarily an error
136 if (DEBUG) {
137 Slog.v(TAG, "Failed to pin camera.");
138 }
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700139 }
140 }
141 }
142 }
143
144 /**
145 * determine if the camera app is already pinned by comparing the
146 * intent resolution to the pinned files list
147 */
148 private boolean alreadyPinned(int userHandle) {
149 ApplicationInfo cameraInfo = getCameraInfo(userHandle);
150 if (cameraInfo == null ) {
151 return false;
152 }
153 for (int i = 0; i < mPinnedCameraFiles.size(); i++) {
154 if (mPinnedCameraFiles.get(i).mFilename.equals(cameraInfo.sourceDir)) {
155 if (DEBUG) {
156 Slog.v(TAG, "Camera is already pinned");
157 }
158 return true;
159 }
160 }
161 return false;
162 }
163
164 private void unpinCameraApp() {
165 for (int i = 0; i < mPinnedCameraFiles.size(); i++) {
166 unpinFile(mPinnedCameraFiles.get(i));
167 }
168 mPinnedCameraFiles.clear();
169 }
170
171 private boolean isResolverActivity(ActivityInfo info) {
172 return ResolverActivity.class.getName().equals(info.name);
173 }
174
175 private ApplicationInfo getCameraInfo(int userHandle) {
176 // find the camera via an intent
177 // use INTENT_ACTION_STILL_IMAGE_CAMERA instead of _SECURE. On a
178 // device without a fbe enabled, the _SECURE intent will never get set.
179 Intent cameraIntent = new Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA);
180 PackageManager pm = mContext.getPackageManager();
Jeff Sharkey847bd852016-08-03 17:20:03 -0600181 ResolveInfo cameraResolveInfo = pm.resolveActivityAsUser(cameraIntent,
182 PackageManager.MATCH_DEFAULT_ONLY | PackageManager.MATCH_DIRECT_BOOT_AWARE
183 | PackageManager.MATCH_DIRECT_BOOT_UNAWARE,
184 userHandle);
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700185 if (cameraResolveInfo == null ) {
186 //this is not necessarily an error
187 if (DEBUG) {
188 Slog.v(TAG, "Unable to resolve camera intent");
189 }
190 return null;
191 }
192
193 if (isResolverActivity(cameraResolveInfo.activityInfo))
194 {
Philip Cuadraac39b962016-11-02 11:59:22 -0700195 if (DEBUG) {
196 Slog.v(TAG, "cameraIntent returned resolverActivity");
197 }
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700198 return null;
199 }
200
201 return cameraResolveInfo.activityInfo.applicationInfo;
202 }
203
204 private boolean pinCamera(int userHandle){
205 //we may have already pinned a camera app. If we've pinned this
206 //camera app, we're done. otherwise, unpin and pin the new app
207 if (alreadyPinned(userHandle)){
208 return true;
209 }
210
211 ApplicationInfo cameraInfo = getCameraInfo(userHandle);
212 if (cameraInfo == null) {
213 return false;
214 }
215
216 //unpin after checking that the camera intent has resolved
217 //this prevents us from thrashing when switching users with
218 //FBE enabled, because the intent won't resolve until the unlock
219 unpinCameraApp();
220
221 //pin APK
222 String camAPK = cameraInfo.sourceDir;
223 PinnedFile pf = pinFile(camAPK, 0, 0, MAX_CAMERA_PIN_SIZE);
224 if (pf == null) {
225 Slog.e(TAG, "Failed to pin " + camAPK);
226 return false;
227 }
228 if (DEBUG) {
229 Slog.i(TAG, "Pinned " + pf.mFilename);
230 }
231 mPinnedCameraFiles.add(pf);
232
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700233 // determine the ABI from either ApplicationInfo or Build
234 String arch = "arm";
235 if (cameraInfo.primaryCpuAbi != null
236 && VMRuntime.is64BitAbi(cameraInfo.primaryCpuAbi)) {
237 arch = arch + "64";
238 } else {
239 if (VMRuntime.is64BitAbi(Build.SUPPORTED_ABIS[0])) {
240 arch = arch + "64";
241 }
242 }
Philip Cuadrad9bd8842016-07-12 17:29:38 -0700243
244 // get the path to the odex or oat file
245 String baseCodePath = cameraInfo.getBaseCodePath();
246 String odex = null;
247 try {
248 odex = DexFile.getDexFileOutputPath(baseCodePath, arch);
249 } catch (IOException ioe) {}
250 if (odex == null) {
251 return true;
252 }
253
254 //not pinning the oat/odex is not a fatal error
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700255 pf = pinFile(odex, 0, 0, MAX_CAMERA_PIN_SIZE);
256 if (pf != null) {
257 mPinnedCameraFiles.add(pf);
258 if (DEBUG) {
259 Slog.i(TAG, "Pinned " + pf.mFilename);
260 }
261 }
Philip Cuadrad9bd8842016-07-12 17:29:38 -0700262
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700263 return true;
264 }
265
266
267 /** mlock length bytes of fileToPin in memory, starting at offset
268 * length == 0 means pin from offset to end of file
269 * maxSize == 0 means infinite
270 */
271 private static PinnedFile pinFile(String fileToPin, long offset, long length, long maxSize) {
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700272 FileDescriptor fd = new FileDescriptor();
273 try {
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700274 fd = Os.open(fileToPin,
275 OsConstants.O_RDONLY | OsConstants.O_CLOEXEC | OsConstants.O_NOFOLLOW,
276 OsConstants.O_RDONLY);
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700277
278 StructStat sb = Os.fstat(fd);
279
280 if (offset + length > sb.st_size) {
281 Os.close(fd);
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700282 Slog.e(TAG, "Failed to pin file " + fileToPin +
283 ", request extends beyond end of file. offset + length = "
284 + (offset + length) + ", file length = " + sb.st_size);
285 return null;
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700286 }
287
288 if (length == 0) {
289 length = sb.st_size - offset;
290 }
291
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700292 if (maxSize > 0 && length > maxSize) {
293 Slog.e(TAG, "Could not pin file " + fileToPin +
294 ", size = " + length + ", maxSize = " + maxSize);
295 Os.close(fd);
296 return null;
297 }
298
299 long address = Os.mmap(0, length, OsConstants.PROT_READ,
300 OsConstants.MAP_PRIVATE, fd, offset);
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700301 Os.close(fd);
302
303 Os.mlock(address, length);
304
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700305 return new PinnedFile(address, length, fileToPin);
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700306 } catch (ErrnoException e) {
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700307 Slog.e(TAG, "Could not pin file " + fileToPin + " with error " + e.getMessage());
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700308 if(fd.valid()) {
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700309 try {
310 Os.close(fd);
311 }
312 catch (ErrnoException eClose) {
313 Slog.e(TAG, "Failed to close fd, error = " + eClose.getMessage());
314 }
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700315 }
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700316 return null;
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700317 }
318 }
319
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700320 private static boolean unpinFile(PinnedFile pf) {
321 try {
322 Os.munlock(pf.mAddress, pf.mLength);
323 } catch (ErrnoException e) {
324 Slog.e(TAG, "Failed to unpin file " + pf.mFilename + " with error " + e.getMessage());
325 return false;
326 }
327 if (DEBUG) {
328 Slog.i(TAG, "Unpinned file " + pf.mFilename );
329 }
330 return true;
331 }
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700332
333 private final class BinderService extends Binder {
334 @Override
335 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
336 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG);
337 pw.println("Pinned Files:");
Philip Cuadraa95cea02016-07-06 16:00:32 -0700338 synchronized(this) {
339 for (int i = 0; i < mPinnedFiles.size(); i++) {
340 pw.println(mPinnedFiles.get(i).mFilename);
341 }
342 for (int i = 0; i < mPinnedCameraFiles.size(); i++) {
343 pw.println(mPinnedCameraFiles.get(i).mFilename);
344 }
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700345 }
346 }
347 }
348
349 private static class PinnedFile {
350 long mAddress;
351 long mLength;
352 String mFilename;
353
354 PinnedFile(long address, long length, String filename) {
355 mAddress = address;
356 mLength = length;
357 mFilename = filename;
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700358 }
359 }
Philip Cuadraa95cea02016-07-06 16:00:32 -0700360
361 final class PinnerHandler extends Handler {
362 static final int PIN_CAMERA_MSG = 4000;
363 static final int PIN_ONSTART_MSG = 4001;
364
365 public PinnerHandler(Looper looper) {
366 super(looper, null, true);
367 }
368
369 @Override
370 public void handleMessage(Message msg) {
371 switch (msg.what) {
372
373 case PIN_CAMERA_MSG:
374 {
375 handlePinCamera(msg.arg1);
376 }
377 break;
378
379 case PIN_ONSTART_MSG:
380 {
381 handlePinOnStart();
382 }
383 break;
384
385 default:
386 super.handleMessage(msg);
387 }
388 }
389 }
390
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700391}