blob: 7ea8f1f4924d859224157a1e7a90d659ea13c148 [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;
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -070020import android.content.pm.ActivityInfo;
21import android.content.pm.ApplicationInfo;
22import android.content.pm.PackageInfo;
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.content.res.Resources;
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -070026import android.content.Intent;
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -070027import android.util.EventLog;
28import android.util.Slog;
29import android.os.Binder;
30import android.os.Build;
Philip Cuadraa95cea02016-07-06 16:00:32 -070031import android.os.Handler;
32import android.os.Looper;
33import android.os.Message;
34import android.os.Process;
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -070035import android.provider.MediaStore;
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -070036import android.system.ErrnoException;
37import android.system.Os;
38import android.system.OsConstants;
39import android.system.StructStat;
40
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;
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -070043
Philip Cuadrad9bd8842016-07-12 17:29:38 -070044import dalvik.system.DexFile;
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -070045import dalvik.system.VMRuntime;
46
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -070047import java.util.ArrayList;
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -070048import java.util.List;
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -070049import java.io.FileDescriptor;
50import java.io.FileOutputStream;
51import java.io.IOException;
52import java.io.PrintWriter;
53
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 Cuadra7cb2f8b2016-06-15 16:23:43 -070071 private final long MAX_CAMERA_PIN_SIZE = 50 * (1 << 20); //50MB max
72
Philip Cuadraa95cea02016-07-06 16:00:32 -070073 private PinnerHandler mPinnerHandler = null;
74
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -070075
76 public PinnerService(Context context) {
77 super(context);
78
79 mContext = context;
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -070080 mShouldPinCamera = context.getResources().getBoolean(
81 com.android.internal.R.bool.config_pinnerCameraApp);
Philip Cuadraa95cea02016-07-06 16:00:32 -070082 mPinnerHandler = new PinnerHandler(BackgroundThread.get().getLooper());
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -070083 }
84
85 @Override
86 public void onStart() {
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -070087 if (DEBUG) {
88 Slog.i(TAG, "Starting PinnerService");
89 }
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -070090 mBinderService = new BinderService();
91 publishBinderService("pinner", mBinderService);
Philip Cuadraa95cea02016-07-06 16:00:32 -070092 mPinnerHandler.sendMessage(
93 mPinnerHandler.obtainMessage(PinnerHandler.PIN_ONSTART_MSG));
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -070094 }
95
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -070096 /**
97 * Pin camera on unlock.
98 * We have to wait for unlock because the user's
99 * preference for camera is not available from PackageManager until after
100 * unlock
101 */
102 @Override
103 public void onUnlockUser(int userHandle) {
Philip Cuadraa95cea02016-07-06 16:00:32 -0700104 mPinnerHandler.sendMessage(
105 mPinnerHandler.obtainMessage(PinnerHandler.PIN_CAMERA_MSG, userHandle, 0));
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700106 }
107
108 /**
Philip Cuadraa95cea02016-07-06 16:00:32 -0700109 * Pin camera on user switch.
110 * If more than one user is using the device
111 * each user may set a different preference for the camera app.
112 * Make sure that user's preference is pinned into memory.
113 */
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700114 @Override
115 public void onSwitchUser(int userHandle) {
Philip Cuadraa95cea02016-07-06 16:00:32 -0700116 mPinnerHandler.sendMessage(
117 mPinnerHandler.obtainMessage(PinnerHandler.PIN_CAMERA_MSG, userHandle, 0));
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700118 }
119
Philip Cuadraa95cea02016-07-06 16:00:32 -0700120 /**
121 * Handler for on start pinning message
122 */
123 private void handlePinOnStart() {
124 // Files to pin come from the overlay and can be specified per-device config
125 String[] filesToPin = mContext.getResources().getStringArray(
126 com.android.internal.R.array.config_defaultPinnerServiceFiles);
127 synchronized(this) {
128 // Continue trying to pin remaining files even if there is a failure
129 for (int i = 0; i < filesToPin.length; i++){
130 PinnedFile pf = pinFile(filesToPin[i], 0, 0, 0);
131 if (pf != null) {
132 mPinnedFiles.add(pf);
133 if (DEBUG) {
134 Slog.i(TAG, "Pinned file = " + pf.mFilename);
135 }
136 } else {
137 Slog.e(TAG, "Failed to pin file = " + filesToPin[i]);
138 }
139 }
140 }
141 }
142
143 /**
144 * Handler for camera pinning message
145 */
146 private void handlePinCamera(int userHandle) {
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700147 if (mShouldPinCamera) {
Philip Cuadraa95cea02016-07-06 16:00:32 -0700148 synchronized(this) {
149 boolean success = pinCamera(userHandle);
150 if (!success) {
151 //this is not necessarily an error
152 if (DEBUG) {
153 Slog.v(TAG, "Failed to pin camera.");
154 }
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700155 }
156 }
157 }
158 }
159
160 /**
161 * determine if the camera app is already pinned by comparing the
162 * intent resolution to the pinned files list
163 */
164 private boolean alreadyPinned(int userHandle) {
165 ApplicationInfo cameraInfo = getCameraInfo(userHandle);
166 if (cameraInfo == null ) {
167 return false;
168 }
169 for (int i = 0; i < mPinnedCameraFiles.size(); i++) {
170 if (mPinnedCameraFiles.get(i).mFilename.equals(cameraInfo.sourceDir)) {
171 if (DEBUG) {
172 Slog.v(TAG, "Camera is already pinned");
173 }
174 return true;
175 }
176 }
177 return false;
178 }
179
180 private void unpinCameraApp() {
181 for (int i = 0; i < mPinnedCameraFiles.size(); i++) {
182 unpinFile(mPinnedCameraFiles.get(i));
183 }
184 mPinnedCameraFiles.clear();
185 }
186
187 private boolean isResolverActivity(ActivityInfo info) {
188 return ResolverActivity.class.getName().equals(info.name);
189 }
190
191 private ApplicationInfo getCameraInfo(int userHandle) {
192 // find the camera via an intent
193 // use INTENT_ACTION_STILL_IMAGE_CAMERA instead of _SECURE. On a
194 // device without a fbe enabled, the _SECURE intent will never get set.
195 Intent cameraIntent = new Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA);
196 PackageManager pm = mContext.getPackageManager();
197 ResolveInfo cameraResolveInfo = pm.resolveActivityAsUser(
198 cameraIntent, PackageManager.MATCH_DEFAULT_ONLY, userHandle);
199 if (cameraResolveInfo == null ) {
200 //this is not necessarily an error
201 if (DEBUG) {
202 Slog.v(TAG, "Unable to resolve camera intent");
203 }
204 return null;
205 }
206
207 if (isResolverActivity(cameraResolveInfo.activityInfo))
208 {
209 return null;
210 }
211
212 return cameraResolveInfo.activityInfo.applicationInfo;
213 }
214
215 private boolean pinCamera(int userHandle){
216 //we may have already pinned a camera app. If we've pinned this
217 //camera app, we're done. otherwise, unpin and pin the new app
218 if (alreadyPinned(userHandle)){
219 return true;
220 }
221
222 ApplicationInfo cameraInfo = getCameraInfo(userHandle);
223 if (cameraInfo == null) {
224 return false;
225 }
226
227 //unpin after checking that the camera intent has resolved
228 //this prevents us from thrashing when switching users with
229 //FBE enabled, because the intent won't resolve until the unlock
230 unpinCameraApp();
231
232 //pin APK
233 String camAPK = cameraInfo.sourceDir;
234 PinnedFile pf = pinFile(camAPK, 0, 0, MAX_CAMERA_PIN_SIZE);
235 if (pf == null) {
236 Slog.e(TAG, "Failed to pin " + camAPK);
237 return false;
238 }
239 if (DEBUG) {
240 Slog.i(TAG, "Pinned " + pf.mFilename);
241 }
242 mPinnedCameraFiles.add(pf);
243
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700244 // determine the ABI from either ApplicationInfo or Build
245 String arch = "arm";
246 if (cameraInfo.primaryCpuAbi != null
247 && VMRuntime.is64BitAbi(cameraInfo.primaryCpuAbi)) {
248 arch = arch + "64";
249 } else {
250 if (VMRuntime.is64BitAbi(Build.SUPPORTED_ABIS[0])) {
251 arch = arch + "64";
252 }
253 }
Philip Cuadrad9bd8842016-07-12 17:29:38 -0700254
255 // get the path to the odex or oat file
256 String baseCodePath = cameraInfo.getBaseCodePath();
257 String odex = null;
258 try {
259 odex = DexFile.getDexFileOutputPath(baseCodePath, arch);
260 } catch (IOException ioe) {}
261 if (odex == null) {
262 return true;
263 }
264
265 //not pinning the oat/odex is not a fatal error
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700266 pf = pinFile(odex, 0, 0, MAX_CAMERA_PIN_SIZE);
267 if (pf != null) {
268 mPinnedCameraFiles.add(pf);
269 if (DEBUG) {
270 Slog.i(TAG, "Pinned " + pf.mFilename);
271 }
272 }
Philip Cuadrad9bd8842016-07-12 17:29:38 -0700273
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700274 return true;
275 }
276
277
278 /** mlock length bytes of fileToPin in memory, starting at offset
279 * length == 0 means pin from offset to end of file
280 * maxSize == 0 means infinite
281 */
282 private static PinnedFile pinFile(String fileToPin, long offset, long length, long maxSize) {
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700283 FileDescriptor fd = new FileDescriptor();
284 try {
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700285 fd = Os.open(fileToPin,
286 OsConstants.O_RDONLY | OsConstants.O_CLOEXEC | OsConstants.O_NOFOLLOW,
287 OsConstants.O_RDONLY);
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700288
289 StructStat sb = Os.fstat(fd);
290
291 if (offset + length > sb.st_size) {
292 Os.close(fd);
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700293 Slog.e(TAG, "Failed to pin file " + fileToPin +
294 ", request extends beyond end of file. offset + length = "
295 + (offset + length) + ", file length = " + sb.st_size);
296 return null;
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700297 }
298
299 if (length == 0) {
300 length = sb.st_size - offset;
301 }
302
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700303 if (maxSize > 0 && length > maxSize) {
304 Slog.e(TAG, "Could not pin file " + fileToPin +
305 ", size = " + length + ", maxSize = " + maxSize);
306 Os.close(fd);
307 return null;
308 }
309
310 long address = Os.mmap(0, length, OsConstants.PROT_READ,
311 OsConstants.MAP_PRIVATE, fd, offset);
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700312 Os.close(fd);
313
314 Os.mlock(address, length);
315
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700316 return new PinnedFile(address, length, fileToPin);
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700317 } catch (ErrnoException e) {
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700318 Slog.e(TAG, "Could not pin file " + fileToPin + " with error " + e.getMessage());
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700319 if(fd.valid()) {
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700320 try {
321 Os.close(fd);
322 }
323 catch (ErrnoException eClose) {
324 Slog.e(TAG, "Failed to close fd, error = " + eClose.getMessage());
325 }
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700326 }
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700327 return null;
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700328 }
329 }
330
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700331 private static boolean unpinFile(PinnedFile pf) {
332 try {
333 Os.munlock(pf.mAddress, pf.mLength);
334 } catch (ErrnoException e) {
335 Slog.e(TAG, "Failed to unpin file " + pf.mFilename + " with error " + e.getMessage());
336 return false;
337 }
338 if (DEBUG) {
339 Slog.i(TAG, "Unpinned file " + pf.mFilename );
340 }
341 return true;
342 }
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700343
344 private final class BinderService extends Binder {
345 @Override
346 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
347 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG);
348 pw.println("Pinned Files:");
Philip Cuadraa95cea02016-07-06 16:00:32 -0700349 synchronized(this) {
350 for (int i = 0; i < mPinnedFiles.size(); i++) {
351 pw.println(mPinnedFiles.get(i).mFilename);
352 }
353 for (int i = 0; i < mPinnedCameraFiles.size(); i++) {
354 pw.println(mPinnedCameraFiles.get(i).mFilename);
355 }
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700356 }
357 }
358 }
359
360 private static class PinnedFile {
361 long mAddress;
362 long mLength;
363 String mFilename;
364
365 PinnedFile(long address, long length, String filename) {
366 mAddress = address;
367 mLength = length;
368 mFilename = filename;
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700369 }
370 }
Philip Cuadraa95cea02016-07-06 16:00:32 -0700371
372 final class PinnerHandler extends Handler {
373 static final int PIN_CAMERA_MSG = 4000;
374 static final int PIN_ONSTART_MSG = 4001;
375
376 public PinnerHandler(Looper looper) {
377 super(looper, null, true);
378 }
379
380 @Override
381 public void handleMessage(Message msg) {
382 switch (msg.what) {
383
384 case PIN_CAMERA_MSG:
385 {
386 handlePinCamera(msg.arg1);
387 }
388 break;
389
390 case PIN_ONSTART_MSG:
391 {
392 handlePinOnStart();
393 }
394 break;
395
396 default:
397 super.handleMessage(msg);
398 }
399 }
400 }
401
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700402}