blob: e3ebf4d3a7ee23611b55cfa4891fbad39c0e9051 [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;
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -060040import com.android.internal.util.DumpUtils;
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -070041
Philip Cuadrad9bd8842016-07-12 17:29:38 -070042import dalvik.system.DexFile;
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -070043import dalvik.system.VMRuntime;
44
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -070045import java.io.FileDescriptor;
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -070046import java.io.IOException;
47import java.io.PrintWriter;
Jeff Sharkey847bd852016-08-03 17:20:03 -060048import java.util.ArrayList;
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -070049
50/**
51 * <p>PinnerService pins important files for key processes in memory.</p>
52 * <p>Files to pin are specified in the config_defaultPinnerServiceFiles
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -070053 * overlay.</p>
54 * <p>Pin the default camera application if specified in config_pinnerCameraApp.</p>
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -070055 */
56public final class PinnerService extends SystemService {
57 private static final boolean DEBUG = false;
58 private static final String TAG = "PinnerService";
59
60 private final Context mContext;
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -070061 private final ArrayList<PinnedFile> mPinnedFiles = new ArrayList<PinnedFile>();
62 private final ArrayList<PinnedFile> mPinnedCameraFiles = new ArrayList<PinnedFile>();
63 private final boolean mShouldPinCamera;
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -070064
65 private BinderService mBinderService;
66
Philip Cuadraac39b962016-11-02 11:59:22 -070067 private final long MAX_CAMERA_PIN_SIZE = 80 * (1 << 20); //80MB max
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -070068
Philip Cuadraa95cea02016-07-06 16:00:32 -070069 private PinnerHandler mPinnerHandler = null;
70
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -070071
72 public PinnerService(Context context) {
73 super(context);
74
75 mContext = context;
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -070076 mShouldPinCamera = context.getResources().getBoolean(
77 com.android.internal.R.bool.config_pinnerCameraApp);
Philip Cuadraa95cea02016-07-06 16:00:32 -070078 mPinnerHandler = new PinnerHandler(BackgroundThread.get().getLooper());
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -070079 }
80
81 @Override
82 public void onStart() {
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -070083 if (DEBUG) {
84 Slog.i(TAG, "Starting PinnerService");
85 }
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -070086 mBinderService = new BinderService();
87 publishBinderService("pinner", mBinderService);
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -070088
Jeff Sharkey847bd852016-08-03 17:20:03 -060089 mPinnerHandler.obtainMessage(PinnerHandler.PIN_ONSTART_MSG).sendToTarget();
90 mPinnerHandler.obtainMessage(PinnerHandler.PIN_CAMERA_MSG, UserHandle.USER_SYSTEM, 0)
91 .sendToTarget();
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -070092 }
93
94 /**
Philip Cuadraa95cea02016-07-06 16:00:32 -070095 * Pin camera on user switch.
96 * If more than one user is using the device
97 * each user may set a different preference for the camera app.
98 * Make sure that user's preference is pinned into memory.
99 */
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700100 @Override
101 public void onSwitchUser(int userHandle) {
Jeff Sharkey847bd852016-08-03 17:20:03 -0600102 mPinnerHandler.obtainMessage(PinnerHandler.PIN_CAMERA_MSG, userHandle, 0).sendToTarget();
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700103 }
104
Philip Cuadraa95cea02016-07-06 16:00:32 -0700105 /**
106 * Handler for on start pinning message
107 */
108 private void handlePinOnStart() {
109 // Files to pin come from the overlay and can be specified per-device config
110 String[] filesToPin = mContext.getResources().getStringArray(
111 com.android.internal.R.array.config_defaultPinnerServiceFiles);
112 synchronized(this) {
113 // Continue trying to pin remaining files even if there is a failure
114 for (int i = 0; i < filesToPin.length; i++){
115 PinnedFile pf = pinFile(filesToPin[i], 0, 0, 0);
116 if (pf != null) {
117 mPinnedFiles.add(pf);
118 if (DEBUG) {
119 Slog.i(TAG, "Pinned file = " + pf.mFilename);
120 }
121 } else {
122 Slog.e(TAG, "Failed to pin file = " + filesToPin[i]);
123 }
124 }
125 }
126 }
127
128 /**
129 * Handler for camera pinning message
130 */
131 private void handlePinCamera(int userHandle) {
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700132 if (mShouldPinCamera) {
Philip Cuadraa95cea02016-07-06 16:00:32 -0700133 synchronized(this) {
134 boolean success = pinCamera(userHandle);
135 if (!success) {
136 //this is not necessarily an error
137 if (DEBUG) {
138 Slog.v(TAG, "Failed to pin camera.");
139 }
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700140 }
141 }
142 }
143 }
144
145 /**
146 * determine if the camera app is already pinned by comparing the
147 * intent resolution to the pinned files list
148 */
149 private boolean alreadyPinned(int userHandle) {
150 ApplicationInfo cameraInfo = getCameraInfo(userHandle);
151 if (cameraInfo == null ) {
152 return false;
153 }
154 for (int i = 0; i < mPinnedCameraFiles.size(); i++) {
155 if (mPinnedCameraFiles.get(i).mFilename.equals(cameraInfo.sourceDir)) {
156 if (DEBUG) {
157 Slog.v(TAG, "Camera is already pinned");
158 }
159 return true;
160 }
161 }
162 return false;
163 }
164
165 private void unpinCameraApp() {
166 for (int i = 0; i < mPinnedCameraFiles.size(); i++) {
167 unpinFile(mPinnedCameraFiles.get(i));
168 }
169 mPinnedCameraFiles.clear();
170 }
171
172 private boolean isResolverActivity(ActivityInfo info) {
173 return ResolverActivity.class.getName().equals(info.name);
174 }
175
176 private ApplicationInfo getCameraInfo(int userHandle) {
177 // find the camera via an intent
178 // use INTENT_ACTION_STILL_IMAGE_CAMERA instead of _SECURE. On a
179 // device without a fbe enabled, the _SECURE intent will never get set.
180 Intent cameraIntent = new Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA);
181 PackageManager pm = mContext.getPackageManager();
Jeff Sharkey847bd852016-08-03 17:20:03 -0600182 ResolveInfo cameraResolveInfo = pm.resolveActivityAsUser(cameraIntent,
183 PackageManager.MATCH_DEFAULT_ONLY | PackageManager.MATCH_DIRECT_BOOT_AWARE
184 | PackageManager.MATCH_DIRECT_BOOT_UNAWARE,
185 userHandle);
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700186 if (cameraResolveInfo == null ) {
187 //this is not necessarily an error
188 if (DEBUG) {
189 Slog.v(TAG, "Unable to resolve camera intent");
190 }
191 return null;
192 }
193
194 if (isResolverActivity(cameraResolveInfo.activityInfo))
195 {
Philip Cuadraac39b962016-11-02 11:59:22 -0700196 if (DEBUG) {
197 Slog.v(TAG, "cameraIntent returned resolverActivity");
198 }
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700199 return null;
200 }
201
202 return cameraResolveInfo.activityInfo.applicationInfo;
203 }
204
205 private boolean pinCamera(int userHandle){
206 //we may have already pinned a camera app. If we've pinned this
207 //camera app, we're done. otherwise, unpin and pin the new app
208 if (alreadyPinned(userHandle)){
209 return true;
210 }
211
212 ApplicationInfo cameraInfo = getCameraInfo(userHandle);
213 if (cameraInfo == null) {
214 return false;
215 }
216
217 //unpin after checking that the camera intent has resolved
218 //this prevents us from thrashing when switching users with
219 //FBE enabled, because the intent won't resolve until the unlock
220 unpinCameraApp();
221
222 //pin APK
223 String camAPK = cameraInfo.sourceDir;
224 PinnedFile pf = pinFile(camAPK, 0, 0, MAX_CAMERA_PIN_SIZE);
225 if (pf == null) {
226 Slog.e(TAG, "Failed to pin " + camAPK);
227 return false;
228 }
229 if (DEBUG) {
230 Slog.i(TAG, "Pinned " + pf.mFilename);
231 }
232 mPinnedCameraFiles.add(pf);
233
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700234 // determine the ABI from either ApplicationInfo or Build
235 String arch = "arm";
236 if (cameraInfo.primaryCpuAbi != null
237 && VMRuntime.is64BitAbi(cameraInfo.primaryCpuAbi)) {
238 arch = arch + "64";
239 } else {
240 if (VMRuntime.is64BitAbi(Build.SUPPORTED_ABIS[0])) {
241 arch = arch + "64";
242 }
243 }
Philip Cuadrad9bd8842016-07-12 17:29:38 -0700244
245 // get the path to the odex or oat file
246 String baseCodePath = cameraInfo.getBaseCodePath();
Calin Juravle128721a2017-05-15 20:20:50 -0700247 String[] files = null;
Philip Cuadrad9bd8842016-07-12 17:29:38 -0700248 try {
Calin Juravle128721a2017-05-15 20:20:50 -0700249 files = DexFile.getDexFileOutputPaths(baseCodePath, arch);
Philip Cuadrad9bd8842016-07-12 17:29:38 -0700250 } catch (IOException ioe) {}
Calin Juravle128721a2017-05-15 20:20:50 -0700251 if (files == null) {
Philip Cuadrad9bd8842016-07-12 17:29:38 -0700252 return true;
253 }
254
255 //not pinning the oat/odex is not a fatal error
Calin Juravle128721a2017-05-15 20:20:50 -0700256 for (String file : files) {
257 pf = pinFile(file, 0, 0, MAX_CAMERA_PIN_SIZE);
258 if (pf != null) {
259 mPinnedCameraFiles.add(pf);
260 if (DEBUG) {
261 Slog.i(TAG, "Pinned " + pf.mFilename);
262 }
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700263 }
264 }
Philip Cuadrad9bd8842016-07-12 17:29:38 -0700265
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700266 return true;
267 }
268
269
270 /** mlock length bytes of fileToPin in memory, starting at offset
271 * length == 0 means pin from offset to end of file
272 * maxSize == 0 means infinite
273 */
274 private static PinnedFile pinFile(String fileToPin, long offset, long length, long maxSize) {
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700275 FileDescriptor fd = new FileDescriptor();
276 try {
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700277 fd = Os.open(fileToPin,
278 OsConstants.O_RDONLY | OsConstants.O_CLOEXEC | OsConstants.O_NOFOLLOW,
279 OsConstants.O_RDONLY);
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700280
281 StructStat sb = Os.fstat(fd);
282
283 if (offset + length > sb.st_size) {
284 Os.close(fd);
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700285 Slog.e(TAG, "Failed to pin file " + fileToPin +
286 ", request extends beyond end of file. offset + length = "
287 + (offset + length) + ", file length = " + sb.st_size);
288 return null;
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700289 }
290
291 if (length == 0) {
292 length = sb.st_size - offset;
293 }
294
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700295 if (maxSize > 0 && length > maxSize) {
296 Slog.e(TAG, "Could not pin file " + fileToPin +
297 ", size = " + length + ", maxSize = " + maxSize);
298 Os.close(fd);
299 return null;
300 }
301
302 long address = Os.mmap(0, length, OsConstants.PROT_READ,
303 OsConstants.MAP_PRIVATE, fd, offset);
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700304 Os.close(fd);
305
306 Os.mlock(address, length);
307
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700308 return new PinnedFile(address, length, fileToPin);
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700309 } catch (ErrnoException e) {
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700310 Slog.e(TAG, "Could not pin file " + fileToPin + " with error " + e.getMessage());
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700311 if(fd.valid()) {
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700312 try {
313 Os.close(fd);
314 }
315 catch (ErrnoException eClose) {
316 Slog.e(TAG, "Failed to close fd, error = " + eClose.getMessage());
317 }
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700318 }
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700319 return null;
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700320 }
321 }
322
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700323 private static boolean unpinFile(PinnedFile pf) {
324 try {
325 Os.munlock(pf.mAddress, pf.mLength);
326 } catch (ErrnoException e) {
327 Slog.e(TAG, "Failed to unpin file " + pf.mFilename + " with error " + e.getMessage());
328 return false;
329 }
330 if (DEBUG) {
331 Slog.i(TAG, "Unpinned file " + pf.mFilename );
332 }
333 return true;
334 }
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700335
336 private final class BinderService extends Binder {
337 @Override
338 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -0600339 if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return;
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700340 pw.println("Pinned Files:");
Philip Cuadraa95cea02016-07-06 16:00:32 -0700341 synchronized(this) {
342 for (int i = 0; i < mPinnedFiles.size(); i++) {
343 pw.println(mPinnedFiles.get(i).mFilename);
344 }
345 for (int i = 0; i < mPinnedCameraFiles.size(); i++) {
346 pw.println(mPinnedCameraFiles.get(i).mFilename);
347 }
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700348 }
349 }
350 }
351
352 private static class PinnedFile {
353 long mAddress;
354 long mLength;
355 String mFilename;
356
357 PinnedFile(long address, long length, String filename) {
358 mAddress = address;
359 mLength = length;
360 mFilename = filename;
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700361 }
362 }
Philip Cuadraa95cea02016-07-06 16:00:32 -0700363
364 final class PinnerHandler extends Handler {
365 static final int PIN_CAMERA_MSG = 4000;
366 static final int PIN_ONSTART_MSG = 4001;
367
368 public PinnerHandler(Looper looper) {
369 super(looper, null, true);
370 }
371
372 @Override
373 public void handleMessage(Message msg) {
374 switch (msg.what) {
375
376 case PIN_CAMERA_MSG:
377 {
378 handlePinCamera(msg.arg1);
379 }
380 break;
381
382 case PIN_ONSTART_MSG:
383 {
384 handlePinOnStart();
385 }
386 break;
387
388 default:
389 super.handleMessage(msg);
390 }
391 }
392 }
393
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700394}