blob: eaf317a46bc15d9adc918a97a20f3be3167a36ef [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
44import dalvik.system.VMRuntime;
45
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -070046import java.util.ArrayList;
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -070047import java.util.List;
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -070048import java.io.FileDescriptor;
49import java.io.FileOutputStream;
50import java.io.IOException;
51import java.io.PrintWriter;
52
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 Cuadra7cb2f8b2016-06-15 16:23:43 -070070 private final long MAX_CAMERA_PIN_SIZE = 50 * (1 << 20); //50MB max
71
Philip Cuadraa95cea02016-07-06 16:00:32 -070072 private PinnerHandler mPinnerHandler = null;
73
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -070074
75 public PinnerService(Context context) {
76 super(context);
77
78 mContext = context;
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -070079 mShouldPinCamera = context.getResources().getBoolean(
80 com.android.internal.R.bool.config_pinnerCameraApp);
Philip Cuadraa95cea02016-07-06 16:00:32 -070081 mPinnerHandler = new PinnerHandler(BackgroundThread.get().getLooper());
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -070082 }
83
84 @Override
85 public void onStart() {
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -070086 if (DEBUG) {
87 Slog.i(TAG, "Starting PinnerService");
88 }
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -070089 mBinderService = new BinderService();
90 publishBinderService("pinner", mBinderService);
Philip Cuadraa95cea02016-07-06 16:00:32 -070091 mPinnerHandler.sendMessage(
92 mPinnerHandler.obtainMessage(PinnerHandler.PIN_ONSTART_MSG));
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -070093 }
94
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -070095 /**
96 * Pin camera on unlock.
97 * We have to wait for unlock because the user's
98 * preference for camera is not available from PackageManager until after
99 * unlock
100 */
101 @Override
102 public void onUnlockUser(int userHandle) {
Philip Cuadraa95cea02016-07-06 16:00:32 -0700103 mPinnerHandler.sendMessage(
104 mPinnerHandler.obtainMessage(PinnerHandler.PIN_CAMERA_MSG, userHandle, 0));
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700105 }
106
107 /**
Philip Cuadraa95cea02016-07-06 16:00:32 -0700108 * Pin camera on user switch.
109 * If more than one user is using the device
110 * each user may set a different preference for the camera app.
111 * Make sure that user's preference is pinned into memory.
112 */
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700113 @Override
114 public void onSwitchUser(int userHandle) {
Philip Cuadraa95cea02016-07-06 16:00:32 -0700115 mPinnerHandler.sendMessage(
116 mPinnerHandler.obtainMessage(PinnerHandler.PIN_CAMERA_MSG, userHandle, 0));
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700117 }
118
Philip Cuadraa95cea02016-07-06 16:00:32 -0700119 /**
120 * Handler for on start pinning message
121 */
122 private void handlePinOnStart() {
123 // Files to pin come from the overlay and can be specified per-device config
124 String[] filesToPin = mContext.getResources().getStringArray(
125 com.android.internal.R.array.config_defaultPinnerServiceFiles);
126 synchronized(this) {
127 // Continue trying to pin remaining files even if there is a failure
128 for (int i = 0; i < filesToPin.length; i++){
129 PinnedFile pf = pinFile(filesToPin[i], 0, 0, 0);
130 if (pf != null) {
131 mPinnedFiles.add(pf);
132 if (DEBUG) {
133 Slog.i(TAG, "Pinned file = " + pf.mFilename);
134 }
135 } else {
136 Slog.e(TAG, "Failed to pin file = " + filesToPin[i]);
137 }
138 }
139 }
140 }
141
142 /**
143 * Handler for camera pinning message
144 */
145 private void handlePinCamera(int userHandle) {
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700146 if (mShouldPinCamera) {
Philip Cuadraa95cea02016-07-06 16:00:32 -0700147 synchronized(this) {
148 boolean success = pinCamera(userHandle);
149 if (!success) {
150 //this is not necessarily an error
151 if (DEBUG) {
152 Slog.v(TAG, "Failed to pin camera.");
153 }
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700154 }
155 }
156 }
157 }
158
159 /**
160 * determine if the camera app is already pinned by comparing the
161 * intent resolution to the pinned files list
162 */
163 private boolean alreadyPinned(int userHandle) {
164 ApplicationInfo cameraInfo = getCameraInfo(userHandle);
165 if (cameraInfo == null ) {
166 return false;
167 }
168 for (int i = 0; i < mPinnedCameraFiles.size(); i++) {
169 if (mPinnedCameraFiles.get(i).mFilename.equals(cameraInfo.sourceDir)) {
170 if (DEBUG) {
171 Slog.v(TAG, "Camera is already pinned");
172 }
173 return true;
174 }
175 }
176 return false;
177 }
178
179 private void unpinCameraApp() {
180 for (int i = 0; i < mPinnedCameraFiles.size(); i++) {
181 unpinFile(mPinnedCameraFiles.get(i));
182 }
183 mPinnedCameraFiles.clear();
184 }
185
186 private boolean isResolverActivity(ActivityInfo info) {
187 return ResolverActivity.class.getName().equals(info.name);
188 }
189
190 private ApplicationInfo getCameraInfo(int userHandle) {
191 // find the camera via an intent
192 // use INTENT_ACTION_STILL_IMAGE_CAMERA instead of _SECURE. On a
193 // device without a fbe enabled, the _SECURE intent will never get set.
194 Intent cameraIntent = new Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA);
195 PackageManager pm = mContext.getPackageManager();
196 ResolveInfo cameraResolveInfo = pm.resolveActivityAsUser(
197 cameraIntent, PackageManager.MATCH_DEFAULT_ONLY, userHandle);
198 if (cameraResolveInfo == null ) {
199 //this is not necessarily an error
200 if (DEBUG) {
201 Slog.v(TAG, "Unable to resolve camera intent");
202 }
203 return null;
204 }
205
206 if (isResolverActivity(cameraResolveInfo.activityInfo))
207 {
208 return null;
209 }
210
211 return cameraResolveInfo.activityInfo.applicationInfo;
212 }
213
214 private boolean pinCamera(int userHandle){
215 //we may have already pinned a camera app. If we've pinned this
216 //camera app, we're done. otherwise, unpin and pin the new app
217 if (alreadyPinned(userHandle)){
218 return true;
219 }
220
221 ApplicationInfo cameraInfo = getCameraInfo(userHandle);
222 if (cameraInfo == null) {
223 return false;
224 }
225
226 //unpin after checking that the camera intent has resolved
227 //this prevents us from thrashing when switching users with
228 //FBE enabled, because the intent won't resolve until the unlock
229 unpinCameraApp();
230
231 //pin APK
232 String camAPK = cameraInfo.sourceDir;
233 PinnedFile pf = pinFile(camAPK, 0, 0, MAX_CAMERA_PIN_SIZE);
234 if (pf == null) {
235 Slog.e(TAG, "Failed to pin " + camAPK);
236 return false;
237 }
238 if (DEBUG) {
239 Slog.i(TAG, "Pinned " + pf.mFilename);
240 }
241 mPinnedCameraFiles.add(pf);
242
243 //find the location of the odex based on the location of the APK
244 int lastPeriod = camAPK.lastIndexOf('.');
245 int lastSlash = camAPK.lastIndexOf('/', lastPeriod);
246 String base = camAPK.substring(0, lastSlash);
247 String appName = camAPK.substring(lastSlash + 1, lastPeriod);
248
249 // determine the ABI from either ApplicationInfo or Build
250 String arch = "arm";
251 if (cameraInfo.primaryCpuAbi != null
252 && VMRuntime.is64BitAbi(cameraInfo.primaryCpuAbi)) {
253 arch = arch + "64";
254 } else {
255 if (VMRuntime.is64BitAbi(Build.SUPPORTED_ABIS[0])) {
256 arch = arch + "64";
257 }
258 }
259 String odex = base + "/oat/" + arch + "/" + appName + ".odex";
260 //not all apps have odex files, so not pinning the odex is not a fatal error
261 pf = pinFile(odex, 0, 0, MAX_CAMERA_PIN_SIZE);
262 if (pf != null) {
263 mPinnedCameraFiles.add(pf);
264 if (DEBUG) {
265 Slog.i(TAG, "Pinned " + pf.mFilename);
266 }
267 }
268 return true;
269 }
270
271
272 /** mlock length bytes of fileToPin in memory, starting at offset
273 * length == 0 means pin from offset to end of file
274 * maxSize == 0 means infinite
275 */
276 private static PinnedFile pinFile(String fileToPin, long offset, long length, long maxSize) {
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700277 FileDescriptor fd = new FileDescriptor();
278 try {
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700279 fd = Os.open(fileToPin,
280 OsConstants.O_RDONLY | OsConstants.O_CLOEXEC | OsConstants.O_NOFOLLOW,
281 OsConstants.O_RDONLY);
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700282
283 StructStat sb = Os.fstat(fd);
284
285 if (offset + length > sb.st_size) {
286 Os.close(fd);
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700287 Slog.e(TAG, "Failed to pin file " + fileToPin +
288 ", request extends beyond end of file. offset + length = "
289 + (offset + length) + ", file length = " + sb.st_size);
290 return null;
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700291 }
292
293 if (length == 0) {
294 length = sb.st_size - offset;
295 }
296
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700297 if (maxSize > 0 && length > maxSize) {
298 Slog.e(TAG, "Could not pin file " + fileToPin +
299 ", size = " + length + ", maxSize = " + maxSize);
300 Os.close(fd);
301 return null;
302 }
303
304 long address = Os.mmap(0, length, OsConstants.PROT_READ,
305 OsConstants.MAP_PRIVATE, fd, offset);
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700306 Os.close(fd);
307
308 Os.mlock(address, length);
309
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700310 return new PinnedFile(address, length, fileToPin);
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700311 } catch (ErrnoException e) {
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700312 Slog.e(TAG, "Could not pin file " + fileToPin + " with error " + e.getMessage());
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700313 if(fd.valid()) {
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700314 try {
315 Os.close(fd);
316 }
317 catch (ErrnoException eClose) {
318 Slog.e(TAG, "Failed to close fd, error = " + eClose.getMessage());
319 }
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700320 }
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700321 return null;
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700322 }
323 }
324
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700325 private static boolean unpinFile(PinnedFile pf) {
326 try {
327 Os.munlock(pf.mAddress, pf.mLength);
328 } catch (ErrnoException e) {
329 Slog.e(TAG, "Failed to unpin file " + pf.mFilename + " with error " + e.getMessage());
330 return false;
331 }
332 if (DEBUG) {
333 Slog.i(TAG, "Unpinned file " + pf.mFilename );
334 }
335 return true;
336 }
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700337
338 private final class BinderService extends Binder {
339 @Override
340 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
341 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG);
342 pw.println("Pinned Files:");
Philip Cuadraa95cea02016-07-06 16:00:32 -0700343 synchronized(this) {
344 for (int i = 0; i < mPinnedFiles.size(); i++) {
345 pw.println(mPinnedFiles.get(i).mFilename);
346 }
347 for (int i = 0; i < mPinnedCameraFiles.size(); i++) {
348 pw.println(mPinnedCameraFiles.get(i).mFilename);
349 }
Philip Cuadra7cb2f8b2016-06-15 16:23:43 -0700350 }
351 }
352 }
353
354 private static class PinnedFile {
355 long mAddress;
356 long mLength;
357 String mFilename;
358
359 PinnedFile(long address, long length, String filename) {
360 mAddress = address;
361 mLength = length;
362 mFilename = filename;
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700363 }
364 }
Philip Cuadraa95cea02016-07-06 16:00:32 -0700365
366 final class PinnerHandler extends Handler {
367 static final int PIN_CAMERA_MSG = 4000;
368 static final int PIN_ONSTART_MSG = 4001;
369
370 public PinnerHandler(Looper looper) {
371 super(looper, null, true);
372 }
373
374 @Override
375 public void handleMessage(Message msg) {
376 switch (msg.what) {
377
378 case PIN_CAMERA_MSG:
379 {
380 handlePinCamera(msg.arg1);
381 }
382 break;
383
384 case PIN_ONSTART_MSG:
385 {
386 handlePinOnStart();
387 }
388 break;
389
390 default:
391 super.handleMessage(msg);
392 }
393 }
394 }
395
Philip Cuadra7bd0fdd2016-04-28 15:26:49 -0700396}