blob: 16573646614bbc39432a647890f849600bc132f2 [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();
Calin Juravle8e0f9592017-05-15 20:20:50 -0700246 String[] files = null;
Philip Cuadrad9bd8842016-07-12 17:29:38 -0700247 try {
Calin Juravle8e0f9592017-05-15 20:20:50 -0700248 files = DexFile.getDexFileOutputPaths(baseCodePath, arch);
Philip Cuadrad9bd8842016-07-12 17:29:38 -0700249 } catch (IOException ioe) {}
Calin Juravle8e0f9592017-05-15 20:20:50 -0700250 if (files == null) {
Philip Cuadrad9bd8842016-07-12 17:29:38 -0700251 return true;
252 }
253
254 //not pinning the oat/odex is not a fatal error
Calin Juravle8e0f9592017-05-15 20:20:50 -0700255 for (String file : files) {
256 pf = pinFile(file, 0, 0, MAX_CAMERA_PIN_SIZE);
257 if (pf != null) {
258 mPinnedCameraFiles.add(pf);
259 if (DEBUG) {
260 Slog.i(TAG, "Pinned " + pf.mFilename);
261 }
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700262 }
263 }
Philip Cuadrad9bd8842016-07-12 17:29:38 -0700264
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700265 return true;
266 }
267
268
269 /** mlock length bytes of fileToPin in memory, starting at offset
270 * length == 0 means pin from offset to end of file
271 * maxSize == 0 means infinite
272 */
273 private static PinnedFile pinFile(String fileToPin, long offset, long length, long maxSize) {
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700274 FileDescriptor fd = new FileDescriptor();
275 try {
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700276 fd = Os.open(fileToPin,
277 OsConstants.O_RDONLY | OsConstants.O_CLOEXEC | OsConstants.O_NOFOLLOW,
278 OsConstants.O_RDONLY);
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700279
280 StructStat sb = Os.fstat(fd);
281
282 if (offset + length > sb.st_size) {
283 Os.close(fd);
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700284 Slog.e(TAG, "Failed to pin file " + fileToPin +
285 ", request extends beyond end of file. offset + length = "
286 + (offset + length) + ", file length = " + sb.st_size);
287 return null;
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700288 }
289
290 if (length == 0) {
291 length = sb.st_size - offset;
292 }
293
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700294 if (maxSize > 0 && length > maxSize) {
295 Slog.e(TAG, "Could not pin file " + fileToPin +
296 ", size = " + length + ", maxSize = " + maxSize);
297 Os.close(fd);
298 return null;
299 }
300
301 long address = Os.mmap(0, length, OsConstants.PROT_READ,
302 OsConstants.MAP_PRIVATE, fd, offset);
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700303 Os.close(fd);
304
305 Os.mlock(address, length);
306
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700307 return new PinnedFile(address, length, fileToPin);
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700308 } catch (ErrnoException e) {
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700309 Slog.e(TAG, "Could not pin file " + fileToPin + " with error " + e.getMessage());
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700310 if(fd.valid()) {
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700311 try {
312 Os.close(fd);
313 }
314 catch (ErrnoException eClose) {
315 Slog.e(TAG, "Failed to close fd, error = " + eClose.getMessage());
316 }
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700317 }
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700318 return null;
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700319 }
320 }
321
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700322 private static boolean unpinFile(PinnedFile pf) {
323 try {
324 Os.munlock(pf.mAddress, pf.mLength);
325 } catch (ErrnoException e) {
326 Slog.e(TAG, "Failed to unpin file " + pf.mFilename + " with error " + e.getMessage());
327 return false;
328 }
329 if (DEBUG) {
330 Slog.i(TAG, "Unpinned file " + pf.mFilename );
331 }
332 return true;
333 }
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700334
335 private final class BinderService extends Binder {
336 @Override
337 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
338 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG);
339 pw.println("Pinned Files:");
Philip Cuadraa95cea02016-07-06 16:00:32 -0700340 synchronized(this) {
341 for (int i = 0; i < mPinnedFiles.size(); i++) {
342 pw.println(mPinnedFiles.get(i).mFilename);
343 }
344 for (int i = 0; i < mPinnedCameraFiles.size(); i++) {
345 pw.println(mPinnedCameraFiles.get(i).mFilename);
346 }
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700347 }
348 }
349 }
350
351 private static class PinnedFile {
352 long mAddress;
353 long mLength;
354 String mFilename;
355
356 PinnedFile(long address, long length, String filename) {
357 mAddress = address;
358 mLength = length;
359 mFilename = filename;
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700360 }
361 }
Philip Cuadraa95cea02016-07-06 16:00:32 -0700362
363 final class PinnerHandler extends Handler {
364 static final int PIN_CAMERA_MSG = 4000;
365 static final int PIN_ONSTART_MSG = 4001;
366
367 public PinnerHandler(Looper looper) {
368 super(looper, null, true);
369 }
370
371 @Override
372 public void handleMessage(Message msg) {
373 switch (msg.what) {
374
375 case PIN_CAMERA_MSG:
376 {
377 handlePinCamera(msg.arg1);
378 }
379 break;
380
381 case PIN_ONSTART_MSG:
382 {
383 handlePinOnStart();
384 }
385 break;
386
387 default:
388 super.handleMessage(msg);
389 }
390 }
391 }
392
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700393}