blob: a5c083e1d03ce1aec204e2e480d237546ee80bd4 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 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
Kenny Rootcf0b38c2011-03-22 14:17:59 -070017package com.android.server.pm;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018
Calin Juravle3621be72018-01-18 15:17:29 -080019import android.annotation.AppIdInt;
Victor Hsieh5f761242018-01-20 10:30:12 -080020import android.annotation.NonNull;
Fyodor Kupolovb94c1652015-03-03 12:25:30 -080021import android.annotation.Nullable;
Calin Juravle3621be72018-01-18 15:17:29 -080022import android.annotation.UserIdInt;
Jeff Brownb880d882014-02-10 19:47:07 -080023import android.content.Context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.content.pm.PackageStats;
Narayan Kamath29564cd2014-08-07 10:57:40 +010025import android.os.Build;
Jeff Sharkey9f2f2182016-12-21 09:18:33 -070026import android.os.IBinder;
27import android.os.IBinder.DeathRecipient;
Jeff Sharkey70b4d102016-12-05 11:19:28 -070028import android.os.IInstalld;
Jeff Sharkey9f2f2182016-12-21 09:18:33 -070029import android.os.RemoteException;
Jeff Sharkey70b4d102016-12-05 11:19:28 -070030import android.os.ServiceManager;
Jeff Sharkey9f2f2182016-12-21 09:18:33 -070031import android.text.format.DateUtils;
Joe Onorato8a9b2202010-02-26 18:56:32 -080032import android.util.Slog;
Jeff Sharkey790a4ec2015-04-09 13:18:44 -070033
Jeff Sharkey9f2f2182016-12-21 09:18:33 -070034import com.android.internal.os.BackgroundThread;
Narayan Kamath29564cd2014-08-07 10:57:40 +010035import com.android.server.SystemService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036
Jeff Sharkeydd02e332018-06-27 14:41:57 -060037import dalvik.system.BlockGuard;
Jeff Sharkeyfdeeeea2016-01-11 17:34:24 -070038import dalvik.system.VMRuntime;
39
Victor Hsieh55f14992018-01-13 14:12:59 -080040import java.io.FileDescriptor;
41
Jeff Sharkeyc98c7bc2016-12-07 14:57:34 -070042public class Installer extends SystemService {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043 private static final String TAG = "Installer";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044
Todd Kennedyfa54ab72015-09-25 07:46:12 -070045 /* ***************************************************************************
46 * IMPORTANT: These values are passed to native code. Keep them in sync with
Mathew Inwood3c8277d2018-03-09 14:06:08 +000047 * frameworks/native/cmds/installd/installd_constants.h
Todd Kennedyfa54ab72015-09-25 07:46:12 -070048 * **************************************************************************/
49 /** Application should be visible to everyone */
Andreas Gampebdd30d82016-03-20 11:32:11 -070050 public static final int DEXOPT_PUBLIC = 1 << 1;
Todd Kennedyfa54ab72015-09-25 07:46:12 -070051 /** Application wants to allow debugging of its code */
Nicolas Geoffray56123ba2017-05-05 14:30:02 +010052 public static final int DEXOPT_DEBUGGABLE = 1 << 2;
Todd Kennedyfa54ab72015-09-25 07:46:12 -070053 /** The system boot has finished */
Nicolas Geoffray56123ba2017-05-05 14:30:02 +010054 public static final int DEXOPT_BOOTCOMPLETE = 1 << 3;
Andreas Gampebdd30d82016-03-20 11:32:11 -070055 /** Hint that the dexopt type is profile-guided. */
Nicolas Geoffray56123ba2017-05-05 14:30:02 +010056 public static final int DEXOPT_PROFILE_GUIDED = 1 << 4;
Calin Juravlec22c30e2017-01-16 19:18:48 -080057 /** The compilation is for a secondary dex file. */
Nicolas Geoffray56123ba2017-05-05 14:30:02 +010058 public static final int DEXOPT_SECONDARY_DEX = 1 << 5;
Calin Juravlec22c30e2017-01-16 19:18:48 -080059 /** Ignore the result of dexoptNeeded and force compilation. */
Nicolas Geoffray56123ba2017-05-05 14:30:02 +010060 public static final int DEXOPT_FORCE = 1 << 6;
Calin Juravlec22c30e2017-01-16 19:18:48 -080061 /** Indicates that the dex file passed to dexopt in on CE storage. */
Nicolas Geoffray56123ba2017-05-05 14:30:02 +010062 public static final int DEXOPT_STORAGE_CE = 1 << 7;
Calin Juravlec22c30e2017-01-16 19:18:48 -080063 /** Indicates that the dex file passed to dexopt in on DE storage. */
Nicolas Geoffray56123ba2017-05-05 14:30:02 +010064 public static final int DEXOPT_STORAGE_DE = 1 << 8;
David Sehr2118ec42017-10-25 14:28:29 -070065 /** Indicates that dexopt is invoked from the background service. */
66 public static final int DEXOPT_IDLE_BACKGROUND_JOB = 1 << 9;
David Brazdilf7e31c02018-02-13 17:04:26 +000067 /** Indicates that dexopt should restrict access to private APIs. */
68 public static final int DEXOPT_ENABLE_HIDDEN_API_CHECKS = 1 << 10;
Mathieu Chartierf890c3e2018-03-06 18:28:22 -080069 /** Indicates that dexopt should convert to CompactDex. */
70 public static final int DEXOPT_GENERATE_COMPACT_DEX = 1 << 11;
Mathieu Chartiera9c34332018-03-12 17:11:07 -070071 /** Indicates that dexopt should generate an app image */
72 public static final int DEXOPT_GENERATE_APP_IMAGE = 1 << 12;
Todd Kennedyfa54ab72015-09-25 07:46:12 -070073
Jeff Sharkey4e7a7652018-08-24 17:25:42 -060074 public static final int FLAG_STORAGE_DE = IInstalld.FLAG_STORAGE_DE;
75 public static final int FLAG_STORAGE_CE = IInstalld.FLAG_STORAGE_CE;
76
77 public static final int FLAG_CLEAR_CACHE_ONLY = IInstalld.FLAG_CLEAR_CACHE_ONLY;
78 public static final int FLAG_CLEAR_CODE_CACHE_ONLY = IInstalld.FLAG_CLEAR_CODE_CACHE_ONLY;
79
80 public static final int FLAG_FREE_CACHE_V2 = IInstalld.FLAG_FREE_CACHE_V2;
81 public static final int FLAG_FREE_CACHE_V2_DEFY_QUOTA = IInstalld.FLAG_FREE_CACHE_V2_DEFY_QUOTA;
82 public static final int FLAG_FREE_CACHE_NOOP = IInstalld.FLAG_FREE_CACHE_NOOP;
83
84 public static final int FLAG_USE_QUOTA = IInstalld.FLAG_USE_QUOTA;
85 public static final int FLAG_FORCE = IInstalld.FLAG_FORCE;
Jeff Sharkeyfdeeeea2016-01-11 17:34:24 -070086
Jeff Sharkeyc98c7bc2016-12-07 14:57:34 -070087 private final boolean mIsolated;
88
Jeff Sharkey740f5232016-12-09 14:31:26 -070089 private volatile IInstalld mInstalld;
Jeff Sharkeyc8ddc2d2016-12-05 23:14:41 -070090 private volatile Object mWarnIfHeld;
91
Jeff Brownb880d882014-02-10 19:47:07 -080092 public Installer(Context context) {
Jeff Sharkeyc98c7bc2016-12-07 14:57:34 -070093 this(context, false);
Jeff Brownb880d882014-02-10 19:47:07 -080094 }
95
Jeff Sharkeyc98c7bc2016-12-07 14:57:34 -070096 /**
97 * @param isolated indicates if this object should <em>not</em> connect to
98 * the real {@code installd}. All remote calls will be ignored
99 * unless you extend this class and intercept them.
100 */
101 public Installer(Context context, boolean isolated) {
Andreas Gamped15300c2016-06-23 20:27:12 -0700102 super(context);
Jeff Sharkeyc98c7bc2016-12-07 14:57:34 -0700103 mIsolated = isolated;
Andreas Gamped15300c2016-06-23 20:27:12 -0700104 }
105
Jeff Sharkey8948c012015-11-03 12:33:54 -0800106 /**
107 * Yell loudly if someone tries making future calls while holding a lock on
108 * the given object.
109 */
110 public void setWarnIfHeld(Object warnIfHeld) {
Jeff Sharkeyc8ddc2d2016-12-05 23:14:41 -0700111 mWarnIfHeld = warnIfHeld;
Jeff Sharkey8948c012015-11-03 12:33:54 -0800112 }
113
Jeff Brown6f357d32014-01-15 20:40:55 -0800114 @Override
115 public void onStart() {
Jeff Sharkey740f5232016-12-09 14:31:26 -0700116 if (mIsolated) {
117 mInstalld = null;
118 } else {
Jeff Sharkey9f2f2182016-12-21 09:18:33 -0700119 connect();
120 }
121 }
122
123 private void connect() {
124 IBinder binder = ServiceManager.getService("installd");
125 if (binder != null) {
126 try {
127 binder.linkToDeath(new DeathRecipient() {
128 @Override
129 public void binderDied() {
130 Slog.w(TAG, "installd died; reconnecting");
131 connect();
132 }
133 }, 0);
134 } catch (RemoteException e) {
135 binder = null;
136 }
137 }
138
139 if (binder != null) {
140 mInstalld = IInstalld.Stub.asInterface(binder);
Jeff Sharkey82add8a2017-03-11 19:44:16 -0700141 try {
142 invalidateMounts();
143 } catch (InstallerException ignored) {
144 }
Jeff Sharkey9f2f2182016-12-21 09:18:33 -0700145 } else {
146 Slog.w(TAG, "installd not found; trying again");
147 BackgroundThread.getHandler().postDelayed(() -> {
148 connect();
149 }, DateUtils.SECOND_IN_MILLIS);
Jeff Sharkeyc98c7bc2016-12-07 14:57:34 -0700150 }
Jeff Brown6f357d32014-01-15 20:40:55 -0800151 }
152
Jeff Sharkeyc98c7bc2016-12-07 14:57:34 -0700153 /**
154 * Do several pre-flight checks before making a remote call.
155 *
156 * @return if the remote call should continue.
157 */
158 private boolean checkBeforeRemote() {
Jeff Sharkeyc8ddc2d2016-12-05 23:14:41 -0700159 if (mWarnIfHeld != null && Thread.holdsLock(mWarnIfHeld)) {
160 Slog.wtf(TAG, "Calling thread " + Thread.currentThread().getName() + " is holding 0x"
161 + Integer.toHexString(System.identityHashCode(mWarnIfHeld)), new Throwable());
162 }
Jeff Sharkeyc98c7bc2016-12-07 14:57:34 -0700163 if (mIsolated) {
164 Slog.i(TAG, "Ignoring request because this installer is isolated");
165 return false;
166 } else {
167 return true;
168 }
Jeff Sharkeyc8ddc2d2016-12-05 23:14:41 -0700169 }
170
Jeff Sharkey1c6f7232016-12-19 16:39:02 -0700171 public long createAppData(String uuid, String packageName, int userId, int flags, int appId,
Jeff Sharkeyc8ddc2d2016-12-05 23:14:41 -0700172 String seInfo, int targetSdkVersion) throws InstallerException {
Jeff Sharkey1c6f7232016-12-19 16:39:02 -0700173 if (!checkBeforeRemote()) return -1;
Jeff Sharkeyc8ddc2d2016-12-05 23:14:41 -0700174 try {
Jeff Sharkey1c6f7232016-12-19 16:39:02 -0700175 return mInstalld.createAppData(uuid, packageName, userId, flags, appId, seInfo,
Jeff Sharkey70b4d102016-12-05 11:19:28 -0700176 targetSdkVersion);
Jeff Sharkey447a3ac2016-12-12 10:15:37 -0700177 } catch (Exception e) {
Jeff Sharkey740f5232016-12-09 14:31:26 -0700178 throw InstallerException.from(e);
Jeff Sharkey70b4d102016-12-05 11:19:28 -0700179 }
Jeff Sharkey790a4ec2015-04-09 13:18:44 -0700180 }
181
Jeff Sharkey019ac852016-12-05 23:39:46 -0700182 public void restoreconAppData(String uuid, String packageName, int userId, int flags, int appId,
183 String seInfo) throws InstallerException {
Jeff Sharkeyc98c7bc2016-12-07 14:57:34 -0700184 if (!checkBeforeRemote()) return;
Jeff Sharkey019ac852016-12-05 23:39:46 -0700185 try {
186 mInstalld.restoreconAppData(uuid, packageName, userId, flags, appId, seInfo);
Jeff Sharkey447a3ac2016-12-12 10:15:37 -0700187 } catch (Exception e) {
Jeff Sharkey740f5232016-12-09 14:31:26 -0700188 throw InstallerException.from(e);
Jeff Sharkey019ac852016-12-05 23:39:46 -0700189 }
Jeff Sharkey790a4ec2015-04-09 13:18:44 -0700190 }
191
Jeff Sharkey019ac852016-12-05 23:39:46 -0700192 public void migrateAppData(String uuid, String packageName, int userId, int flags)
Jeff Sharkeye4697132016-02-06 19:46:15 -0700193 throws InstallerException {
Jeff Sharkeyc98c7bc2016-12-07 14:57:34 -0700194 if (!checkBeforeRemote()) return;
Jeff Sharkey019ac852016-12-05 23:39:46 -0700195 try {
196 mInstalld.migrateAppData(uuid, packageName, userId, flags);
Jeff Sharkey447a3ac2016-12-12 10:15:37 -0700197 } catch (Exception e) {
Jeff Sharkey740f5232016-12-09 14:31:26 -0700198 throw InstallerException.from(e);
Jeff Sharkey019ac852016-12-05 23:39:46 -0700199 }
Jeff Sharkeye4697132016-02-06 19:46:15 -0700200 }
201
Jeff Sharkey019ac852016-12-05 23:39:46 -0700202 public void clearAppData(String uuid, String packageName, int userId, int flags,
203 long ceDataInode) throws InstallerException {
Jeff Sharkeyc98c7bc2016-12-07 14:57:34 -0700204 if (!checkBeforeRemote()) return;
Jeff Sharkey019ac852016-12-05 23:39:46 -0700205 try {
206 mInstalld.clearAppData(uuid, packageName, userId, flags, ceDataInode);
Jeff Sharkey447a3ac2016-12-12 10:15:37 -0700207 } catch (Exception e) {
Jeff Sharkey740f5232016-12-09 14:31:26 -0700208 throw InstallerException.from(e);
Jeff Sharkey019ac852016-12-05 23:39:46 -0700209 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 }
211
Jeff Sharkey019ac852016-12-05 23:39:46 -0700212 public void destroyAppData(String uuid, String packageName, int userId, int flags,
213 long ceDataInode) throws InstallerException {
Jeff Sharkeyc98c7bc2016-12-07 14:57:34 -0700214 if (!checkBeforeRemote()) return;
Jeff Sharkey019ac852016-12-05 23:39:46 -0700215 try {
216 mInstalld.destroyAppData(uuid, packageName, userId, flags, ceDataInode);
Jeff Sharkey447a3ac2016-12-12 10:15:37 -0700217 } catch (Exception e) {
Jeff Sharkey740f5232016-12-09 14:31:26 -0700218 throw InstallerException.from(e);
Jeff Sharkey019ac852016-12-05 23:39:46 -0700219 }
Dave Allison0efbd9a2014-01-30 14:19:51 -0800220 }
221
Jeff Sharkey15662332017-04-03 16:41:29 -0600222 public void fixupAppData(String uuid, int flags) throws InstallerException {
223 if (!checkBeforeRemote()) return;
224 try {
225 mInstalld.fixupAppData(uuid, flags);
226 } catch (Exception e) {
227 throw InstallerException.from(e);
228 }
229 }
230
Jeff Sharkeyc8ddc2d2016-12-05 23:14:41 -0700231 public void moveCompleteApp(String fromUuid, String toUuid, String packageName,
232 String dataAppName, int appId, String seInfo, int targetSdkVersion)
Janis Danisevskisf3409742016-01-12 14:46:33 +0000233 throws InstallerException {
Jeff Sharkeyc98c7bc2016-12-07 14:57:34 -0700234 if (!checkBeforeRemote()) return;
Jeff Sharkeyc8ddc2d2016-12-05 23:14:41 -0700235 try {
236 mInstalld.moveCompleteApp(fromUuid, toUuid, packageName, dataAppName, appId, seInfo,
237 targetSdkVersion);
Jeff Sharkey447a3ac2016-12-12 10:15:37 -0700238 } catch (Exception e) {
Jeff Sharkey740f5232016-12-09 14:31:26 -0700239 throw InstallerException.from(e);
Jeff Sharkeyc8ddc2d2016-12-05 23:14:41 -0700240 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 }
242
Jeff Sharkey82ca9012017-01-08 17:06:58 -0700243 public void getAppSize(String uuid, String[] packageNames, int userId, int flags, int appId,
244 long[] ceDataInodes, String[] codePaths, PackageStats stats)
Jeff Sharkey5eb3eb52016-12-13 08:44:51 -0700245 throws InstallerException {
Jeff Sharkeyc98c7bc2016-12-07 14:57:34 -0700246 if (!checkBeforeRemote()) return;
Jeff Sharkeydd02e332018-06-27 14:41:57 -0600247 if (codePaths != null) {
248 for (String codePath : codePaths) {
249 BlockGuard.getVmPolicy().onPathAccess(codePath);
250 }
251 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 try {
Jeff Sharkey82ca9012017-01-08 17:06:58 -0700253 final long[] res = mInstalld.getAppSize(uuid, packageNames, userId, flags,
254 appId, ceDataInodes, codePaths);
Jeff Sharkey740f5232016-12-09 14:31:26 -0700255 stats.codeSize += res[0];
256 stats.dataSize += res[1];
257 stats.cacheSize += res[2];
Jeff Sharkey82ca9012017-01-08 17:06:58 -0700258 stats.externalCodeSize += res[3];
259 stats.externalDataSize += res[4];
260 stats.externalCacheSize += res[5];
261 } catch (Exception e) {
262 throw InstallerException.from(e);
263 }
264 }
265
266 public void getUserSize(String uuid, int userId, int flags, int[] appIds, PackageStats stats)
267 throws InstallerException {
268 if (!checkBeforeRemote()) return;
269 try {
270 final long[] res = mInstalld.getUserSize(uuid, userId, flags, appIds);
271 stats.codeSize += res[0];
272 stats.dataSize += res[1];
273 stats.cacheSize += res[2];
274 stats.externalCodeSize += res[3];
275 stats.externalDataSize += res[4];
276 stats.externalCacheSize += res[5];
277 } catch (Exception e) {
278 throw InstallerException.from(e);
279 }
280 }
281
Jeff Sharkey00347882017-04-17 16:44:12 -0600282 public long[] getExternalSize(String uuid, int userId, int flags, int[] appIds)
283 throws InstallerException {
Jeff Sharkeyc8b29ac2017-07-06 11:29:06 -0600284 if (!checkBeforeRemote()) return new long[6];
Jeff Sharkey82ca9012017-01-08 17:06:58 -0700285 try {
Jeff Sharkey00347882017-04-17 16:44:12 -0600286 return mInstalld.getExternalSize(uuid, userId, flags, appIds);
Jeff Sharkey447a3ac2016-12-12 10:15:37 -0700287 } catch (Exception e) {
Jeff Sharkey740f5232016-12-09 14:31:26 -0700288 throw InstallerException.from(e);
Jeff Sharkey42884192016-04-09 16:12:01 -0600289 }
290 }
291
Jeff Sharkey36ba0222017-01-18 12:11:31 -0700292 public void setAppQuota(String uuid, int userId, int appId, long cacheQuota)
293 throws InstallerException {
294 if (!checkBeforeRemote()) return;
295 try {
296 mInstalld.setAppQuota(uuid, userId, appId, cacheQuota);
297 } catch (Exception e) {
298 throw InstallerException.from(e);
299 }
300 }
301
Jeff Sharkeyc98c7bc2016-12-07 14:57:34 -0700302 public void dexopt(String apkPath, int uid, @Nullable String pkgName, String instructionSet,
Calin Juravledb4a79a2015-12-23 18:55:08 +0200303 int dexoptNeeded, @Nullable String outputPath, int dexFlags,
Calin Juravle811a75a2017-04-05 22:49:38 -0700304 String compilerFilter, @Nullable String volumeUuid, @Nullable String sharedLibraries,
Calin Juravle6ae39fc2018-01-19 20:32:47 -0800305 @Nullable String seInfo, boolean downgrade, int targetSdkVersion,
Calin Juravle4bc8f4d2018-02-12 12:00:44 -0800306 @Nullable String profileName, @Nullable String dexMetadataPath,
307 @Nullable String compilationReason) throws InstallerException {
Jeff Sharkeyfdeeeea2016-01-11 17:34:24 -0700308 assertValidInstructionSet(instructionSet);
Jeff Sharkeydd02e332018-06-27 14:41:57 -0600309 BlockGuard.getVmPolicy().onPathAccess(apkPath);
310 BlockGuard.getVmPolicy().onPathAccess(outputPath);
311 BlockGuard.getVmPolicy().onPathAccess(dexMetadataPath);
Jeff Sharkeyc98c7bc2016-12-07 14:57:34 -0700312 if (!checkBeforeRemote()) return;
Jeff Sharkey740f5232016-12-09 14:31:26 -0700313 try {
314 mInstalld.dexopt(apkPath, uid, pkgName, instructionSet, dexoptNeeded, outputPath,
David Brazdil3d44ed02018-01-16 20:01:47 +0000315 dexFlags, compilerFilter, volumeUuid, sharedLibraries, seInfo, downgrade,
Calin Juravle4bc8f4d2018-02-12 12:00:44 -0800316 targetSdkVersion, profileName, dexMetadataPath, compilationReason);
Jeff Sharkey447a3ac2016-12-12 10:15:37 -0700317 } catch (Exception e) {
Jeff Sharkey740f5232016-12-09 14:31:26 -0700318 throw InstallerException.from(e);
319 }
Andreas Gampebdd30d82016-03-20 11:32:11 -0700320 }
321
Calin Juravle6ae39fc2018-01-19 20:32:47 -0800322 public boolean mergeProfiles(int uid, String packageName, String profileName)
David Sehra8777082016-05-24 15:25:23 -0700323 throws InstallerException {
Jeff Sharkeyc98c7bc2016-12-07 14:57:34 -0700324 if (!checkBeforeRemote()) return false;
Jeff Sharkeyc24fa022016-12-07 10:37:47 -0700325 try {
Calin Juravle6ae39fc2018-01-19 20:32:47 -0800326 return mInstalld.mergeProfiles(uid, packageName, profileName);
Jeff Sharkey447a3ac2016-12-12 10:15:37 -0700327 } catch (Exception e) {
Jeff Sharkey740f5232016-12-09 14:31:26 -0700328 throw InstallerException.from(e);
Jeff Sharkeyc24fa022016-12-07 10:37:47 -0700329 }
David Sehra8777082016-05-24 15:25:23 -0700330 }
331
Calin Juravle6ae39fc2018-01-19 20:32:47 -0800332 public boolean dumpProfiles(int uid, String packageName, String profileName, String codePath)
Mathieu Chartier235845d2017-05-10 15:09:19 -0700333 throws InstallerException {
334 if (!checkBeforeRemote()) return false;
Jeff Sharkeydd02e332018-06-27 14:41:57 -0600335 BlockGuard.getVmPolicy().onPathAccess(codePath);
Mathieu Chartier235845d2017-05-10 15:09:19 -0700336 try {
Calin Juravle6ae39fc2018-01-19 20:32:47 -0800337 return mInstalld.dumpProfiles(uid, packageName, profileName, codePath);
338 } catch (Exception e) {
339 throw InstallerException.from(e);
340 }
341 }
342
343 public boolean copySystemProfile(String systemProfile, int uid, String packageName,
344 String profileName) throws InstallerException {
345 if (!checkBeforeRemote()) return false;
346 try {
347 return mInstalld.copySystemProfile(systemProfile, uid, packageName, profileName);
Mathieu Chartier235845d2017-05-10 15:09:19 -0700348 } catch (Exception e) {
349 throw InstallerException.from(e);
350 }
351 }
352
Jeff Sharkeyfdeeeea2016-01-11 17:34:24 -0700353 public void idmap(String targetApkPath, String overlayApkPath, int uid)
354 throws InstallerException {
Jeff Sharkeyc98c7bc2016-12-07 14:57:34 -0700355 if (!checkBeforeRemote()) return;
Jeff Sharkeydd02e332018-06-27 14:41:57 -0600356 BlockGuard.getVmPolicy().onPathAccess(targetApkPath);
357 BlockGuard.getVmPolicy().onPathAccess(overlayApkPath);
Jeff Sharkeyc24fa022016-12-07 10:37:47 -0700358 try {
359 mInstalld.idmap(targetApkPath, overlayApkPath, uid);
Jeff Sharkey447a3ac2016-12-12 10:15:37 -0700360 } catch (Exception e) {
Jeff Sharkey740f5232016-12-09 14:31:26 -0700361 throw InstallerException.from(e);
Jeff Sharkeyc24fa022016-12-07 10:37:47 -0700362 }
Jeff Sharkeyfdeeeea2016-01-11 17:34:24 -0700363 }
364
MÃ¥rten Kongstad163826342016-06-02 09:35:09 +0200365 public void removeIdmap(String overlayApkPath) throws InstallerException {
366 if (!checkBeforeRemote()) return;
Jeff Sharkeydd02e332018-06-27 14:41:57 -0600367 BlockGuard.getVmPolicy().onPathAccess(overlayApkPath);
MÃ¥rten Kongstad163826342016-06-02 09:35:09 +0200368 try {
369 mInstalld.removeIdmap(overlayApkPath);
370 } catch (Exception e) {
371 throw InstallerException.from(e);
372 }
373 }
374
Jeff Sharkeyfdeeeea2016-01-11 17:34:24 -0700375 public void rmdex(String codePath, String instructionSet) throws InstallerException {
376 assertValidInstructionSet(instructionSet);
Jeff Sharkeyc98c7bc2016-12-07 14:57:34 -0700377 if (!checkBeforeRemote()) return;
Jeff Sharkeydd02e332018-06-27 14:41:57 -0600378 BlockGuard.getVmPolicy().onPathAccess(codePath);
Jeff Sharkeyc24fa022016-12-07 10:37:47 -0700379 try {
380 mInstalld.rmdex(codePath, instructionSet);
Jeff Sharkey447a3ac2016-12-12 10:15:37 -0700381 } catch (Exception e) {
Jeff Sharkey740f5232016-12-09 14:31:26 -0700382 throw InstallerException.from(e);
Jeff Sharkeyc24fa022016-12-07 10:37:47 -0700383 }
Jeff Sharkeyfdeeeea2016-01-11 17:34:24 -0700384 }
385
386 public void rmPackageDir(String packageDir) throws InstallerException {
Jeff Sharkeyc98c7bc2016-12-07 14:57:34 -0700387 if (!checkBeforeRemote()) return;
Jeff Sharkeydd02e332018-06-27 14:41:57 -0600388 BlockGuard.getVmPolicy().onPathAccess(packageDir);
Jeff Sharkeyc24fa022016-12-07 10:37:47 -0700389 try {
390 mInstalld.rmPackageDir(packageDir);
Jeff Sharkey447a3ac2016-12-12 10:15:37 -0700391 } catch (Exception e) {
Jeff Sharkey740f5232016-12-09 14:31:26 -0700392 throw InstallerException.from(e);
Jeff Sharkeyc24fa022016-12-07 10:37:47 -0700393 }
Jeff Sharkeyfdeeeea2016-01-11 17:34:24 -0700394 }
395
Calin Juravle6ae39fc2018-01-19 20:32:47 -0800396 public void clearAppProfiles(String packageName, String profileName) throws InstallerException {
Jeff Sharkeyc98c7bc2016-12-07 14:57:34 -0700397 if (!checkBeforeRemote()) return;
Jeff Sharkeyc24fa022016-12-07 10:37:47 -0700398 try {
Calin Juravle6ae39fc2018-01-19 20:32:47 -0800399 mInstalld.clearAppProfiles(packageName, profileName);
Jeff Sharkey447a3ac2016-12-12 10:15:37 -0700400 } catch (Exception e) {
Jeff Sharkey740f5232016-12-09 14:31:26 -0700401 throw InstallerException.from(e);
Jeff Sharkeyc24fa022016-12-07 10:37:47 -0700402 }
Calin Juravled6d27e32016-03-23 13:59:18 +0000403 }
404
Jeff Sharkeyc24fa022016-12-07 10:37:47 -0700405 public void destroyAppProfiles(String packageName) throws InstallerException {
Jeff Sharkeyc98c7bc2016-12-07 14:57:34 -0700406 if (!checkBeforeRemote()) return;
Jeff Sharkeyc24fa022016-12-07 10:37:47 -0700407 try {
408 mInstalld.destroyAppProfiles(packageName);
Jeff Sharkey447a3ac2016-12-12 10:15:37 -0700409 } catch (Exception e) {
Jeff Sharkey740f5232016-12-09 14:31:26 -0700410 throw InstallerException.from(e);
Jeff Sharkeyc24fa022016-12-07 10:37:47 -0700411 }
David Brazdil9aa6db02016-03-08 12:57:12 +0000412 }
413
Jeff Sharkeyfcf1e552016-04-14 20:44:58 -0600414 public void createUserData(String uuid, int userId, int userSerial, int flags)
415 throws InstallerException {
Jeff Sharkeyc98c7bc2016-12-07 14:57:34 -0700416 if (!checkBeforeRemote()) return;
Jeff Sharkey019ac852016-12-05 23:39:46 -0700417 try {
418 mInstalld.createUserData(uuid, userId, userSerial, flags);
Jeff Sharkey447a3ac2016-12-12 10:15:37 -0700419 } catch (Exception e) {
Jeff Sharkey740f5232016-12-09 14:31:26 -0700420 throw InstallerException.from(e);
Jeff Sharkey019ac852016-12-05 23:39:46 -0700421 }
Jeff Sharkeyfdeeeea2016-01-11 17:34:24 -0700422 }
423
Jeff Sharkeyfcf1e552016-04-14 20:44:58 -0600424 public void destroyUserData(String uuid, int userId, int flags) throws InstallerException {
Jeff Sharkeyc98c7bc2016-12-07 14:57:34 -0700425 if (!checkBeforeRemote()) return;
Jeff Sharkey019ac852016-12-05 23:39:46 -0700426 try {
427 mInstalld.destroyUserData(uuid, userId, flags);
Jeff Sharkey447a3ac2016-12-12 10:15:37 -0700428 } catch (Exception e) {
Jeff Sharkey740f5232016-12-09 14:31:26 -0700429 throw InstallerException.from(e);
Jeff Sharkey019ac852016-12-05 23:39:46 -0700430 }
Jeff Sharkeyfdeeeea2016-01-11 17:34:24 -0700431 }
432
433 public void markBootComplete(String instructionSet) throws InstallerException {
434 assertValidInstructionSet(instructionSet);
Jeff Sharkeyc98c7bc2016-12-07 14:57:34 -0700435 if (!checkBeforeRemote()) return;
Jeff Sharkeyc24fa022016-12-07 10:37:47 -0700436 try {
437 mInstalld.markBootComplete(instructionSet);
Jeff Sharkey447a3ac2016-12-12 10:15:37 -0700438 } catch (Exception e) {
Jeff Sharkey740f5232016-12-09 14:31:26 -0700439 throw InstallerException.from(e);
Jeff Sharkeyc24fa022016-12-07 10:37:47 -0700440 }
Jeff Sharkeyfdeeeea2016-01-11 17:34:24 -0700441 }
442
Jeff Sharkeyddff8072017-05-26 13:10:46 -0600443 public void freeCache(String uuid, long targetFreeBytes, long cacheReservedBytes, int flags)
444 throws InstallerException {
Jeff Sharkeyc98c7bc2016-12-07 14:57:34 -0700445 if (!checkBeforeRemote()) return;
Jeff Sharkeyc24fa022016-12-07 10:37:47 -0700446 try {
Jeff Sharkeyddff8072017-05-26 13:10:46 -0600447 mInstalld.freeCache(uuid, targetFreeBytes, cacheReservedBytes, flags);
Jeff Sharkey447a3ac2016-12-12 10:15:37 -0700448 } catch (Exception e) {
Jeff Sharkey740f5232016-12-09 14:31:26 -0700449 throw InstallerException.from(e);
Jeff Sharkeyc24fa022016-12-07 10:37:47 -0700450 }
Jeff Sharkeyfdeeeea2016-01-11 17:34:24 -0700451 }
452
Kenny Rootddbe50d2012-09-06 13:18:37 -0700453 /**
Jeff Sharkeyfdeeeea2016-01-11 17:34:24 -0700454 * Links the 32 bit native library directory in an application's data
455 * directory to the real location for backward compatibility. Note that no
456 * such symlink is created for 64 bit shared libraries.
Kenny Rootddbe50d2012-09-06 13:18:37 -0700457 */
Jeff Sharkeyc24fa022016-12-07 10:37:47 -0700458 public void linkNativeLibraryDirectory(String uuid, String packageName, String nativeLibPath32,
Jeff Sharkeyfdeeeea2016-01-11 17:34:24 -0700459 int userId) throws InstallerException {
Jeff Sharkeyc98c7bc2016-12-07 14:57:34 -0700460 if (!checkBeforeRemote()) return;
Jeff Sharkeydd02e332018-06-27 14:41:57 -0600461 BlockGuard.getVmPolicy().onPathAccess(nativeLibPath32);
Jeff Sharkeyc24fa022016-12-07 10:37:47 -0700462 try {
463 mInstalld.linkNativeLibraryDirectory(uuid, packageName, nativeLibPath32, userId);
Jeff Sharkey447a3ac2016-12-12 10:15:37 -0700464 } catch (Exception e) {
Jeff Sharkey740f5232016-12-09 14:31:26 -0700465 throw InstallerException.from(e);
Jeff Sharkeyc24fa022016-12-07 10:37:47 -0700466 }
Kenny Root6a6b0072010-10-07 16:46:10 -0700467 }
Robert Craig43853432014-03-04 11:57:23 -0500468
Jeff Sharkeyfdeeeea2016-01-11 17:34:24 -0700469 public void createOatDir(String oatDir, String dexInstructionSet)
470 throws InstallerException {
Jeff Sharkeyc98c7bc2016-12-07 14:57:34 -0700471 if (!checkBeforeRemote()) return;
Jeff Sharkeyc24fa022016-12-07 10:37:47 -0700472 try {
473 mInstalld.createOatDir(oatDir, dexInstructionSet);
Jeff Sharkey447a3ac2016-12-12 10:15:37 -0700474 } catch (Exception e) {
Jeff Sharkey740f5232016-12-09 14:31:26 -0700475 throw InstallerException.from(e);
Jeff Sharkeyc24fa022016-12-07 10:37:47 -0700476 }
Jeff Sharkey790a4ec2015-04-09 13:18:44 -0700477 }
478
Jeff Sharkeyfdeeeea2016-01-11 17:34:24 -0700479 public void linkFile(String relativePath, String fromBase, String toBase)
480 throws InstallerException {
Jeff Sharkeyc98c7bc2016-12-07 14:57:34 -0700481 if (!checkBeforeRemote()) return;
Jeff Sharkeydd02e332018-06-27 14:41:57 -0600482 BlockGuard.getVmPolicy().onPathAccess(fromBase);
483 BlockGuard.getVmPolicy().onPathAccess(toBase);
Jeff Sharkeyc24fa022016-12-07 10:37:47 -0700484 try {
485 mInstalld.linkFile(relativePath, fromBase, toBase);
Jeff Sharkey447a3ac2016-12-12 10:15:37 -0700486 } catch (Exception e) {
Jeff Sharkey740f5232016-12-09 14:31:26 -0700487 throw InstallerException.from(e);
Jeff Sharkeyc24fa022016-12-07 10:37:47 -0700488 }
Robert Craig43853432014-03-04 11:57:23 -0500489 }
Narayan Kamath6c4b9de2014-08-08 12:44:12 +0100490
Andreas Gampeabcbe2f2016-02-26 11:25:36 -0800491 public void moveAb(String apkPath, String instructionSet, String outputPath)
492 throws InstallerException {
Jeff Sharkeyc98c7bc2016-12-07 14:57:34 -0700493 if (!checkBeforeRemote()) return;
Jeff Sharkeydd02e332018-06-27 14:41:57 -0600494 BlockGuard.getVmPolicy().onPathAccess(apkPath);
495 BlockGuard.getVmPolicy().onPathAccess(outputPath);
Jeff Sharkeyc24fa022016-12-07 10:37:47 -0700496 try {
497 mInstalld.moveAb(apkPath, instructionSet, outputPath);
Jeff Sharkey447a3ac2016-12-12 10:15:37 -0700498 } catch (Exception e) {
Jeff Sharkey740f5232016-12-09 14:31:26 -0700499 throw InstallerException.from(e);
Jeff Sharkeyc24fa022016-12-07 10:37:47 -0700500 }
Andreas Gampeabcbe2f2016-02-26 11:25:36 -0800501 }
502
Andreas Gampe33c592d2016-09-09 17:08:53 -0700503 public void deleteOdex(String apkPath, String instructionSet, String outputPath)
504 throws InstallerException {
Jeff Sharkeyc98c7bc2016-12-07 14:57:34 -0700505 if (!checkBeforeRemote()) return;
Jeff Sharkeydd02e332018-06-27 14:41:57 -0600506 BlockGuard.getVmPolicy().onPathAccess(apkPath);
507 BlockGuard.getVmPolicy().onPathAccess(outputPath);
Jeff Sharkeyc24fa022016-12-07 10:37:47 -0700508 try {
509 mInstalld.deleteOdex(apkPath, instructionSet, outputPath);
Jeff Sharkey447a3ac2016-12-12 10:15:37 -0700510 } catch (Exception e) {
Jeff Sharkey740f5232016-12-09 14:31:26 -0700511 throw InstallerException.from(e);
Jeff Sharkeyc24fa022016-12-07 10:37:47 -0700512 }
Andreas Gampe33c592d2016-09-09 17:08:53 -0700513 }
514
Victor Hsiehcccad192018-03-19 15:57:02 -0700515 public void installApkVerity(String filePath, FileDescriptor verityInput, int contentSize)
Victor Hsieh55f14992018-01-13 14:12:59 -0800516 throws InstallerException {
517 if (!checkBeforeRemote()) return;
Jeff Sharkeydd02e332018-06-27 14:41:57 -0600518 BlockGuard.getVmPolicy().onPathAccess(filePath);
Victor Hsieh55f14992018-01-13 14:12:59 -0800519 try {
Victor Hsiehcccad192018-03-19 15:57:02 -0700520 mInstalld.installApkVerity(filePath, verityInput, contentSize);
Victor Hsieh55f14992018-01-13 14:12:59 -0800521 } catch (Exception e) {
522 throw InstallerException.from(e);
523 }
524 }
525
Victor Hsieh5f761242018-01-20 10:30:12 -0800526 public void assertFsverityRootHashMatches(String filePath, @NonNull byte[] expectedHash)
527 throws InstallerException {
528 if (!checkBeforeRemote()) return;
Jeff Sharkeydd02e332018-06-27 14:41:57 -0600529 BlockGuard.getVmPolicy().onPathAccess(filePath);
Victor Hsieh5f761242018-01-20 10:30:12 -0800530 try {
531 mInstalld.assertFsverityRootHashMatches(filePath, expectedHash);
532 } catch (Exception e) {
533 throw InstallerException.from(e);
534 }
535 }
536
Calin Juravle1aa5f882017-01-25 01:05:50 -0800537 public boolean reconcileSecondaryDexFile(String apkPath, String packageName, int uid,
538 String[] isas, @Nullable String volumeUuid, int flags) throws InstallerException {
539 for (int i = 0; i < isas.length; i++) {
540 assertValidInstructionSet(isas[i]);
541 }
542 if (!checkBeforeRemote()) return false;
Jeff Sharkeydd02e332018-06-27 14:41:57 -0600543 BlockGuard.getVmPolicy().onPathAccess(apkPath);
Calin Juravle1aa5f882017-01-25 01:05:50 -0800544 try {
545 return mInstalld.reconcileSecondaryDexFile(apkPath, packageName, uid, isas,
546 volumeUuid, flags);
547 } catch (Exception e) {
548 throw InstallerException.from(e);
549 }
550 }
551
Alan Stokesa0023602017-10-16 12:31:44 +0100552 public byte[] hashSecondaryDexFile(String dexPath, String packageName, int uid,
553 @Nullable String volumeUuid, int flags) throws InstallerException {
554 if (!checkBeforeRemote()) return new byte[0];
Jeff Sharkeydd02e332018-06-27 14:41:57 -0600555 BlockGuard.getVmPolicy().onPathAccess(dexPath);
Alan Stokesa0023602017-10-16 12:31:44 +0100556 try {
557 return mInstalld.hashSecondaryDexFile(dexPath, packageName, uid, volumeUuid, flags);
558 } catch (Exception e) {
559 throw InstallerException.from(e);
560 }
561 }
562
Calin Juravlefcbb74a2018-01-21 21:39:18 -0800563 public boolean createProfileSnapshot(int appId, String packageName, String profileName,
564 String classpath) throws InstallerException {
Calin Juravlefd9f8ae2017-11-29 18:26:55 -0800565 if (!checkBeforeRemote()) return false;
566 try {
Calin Juravlefcbb74a2018-01-21 21:39:18 -0800567 return mInstalld.createProfileSnapshot(appId, packageName, profileName, classpath);
Calin Juravlefd9f8ae2017-11-29 18:26:55 -0800568 } catch (Exception e) {
569 throw InstallerException.from(e);
570 }
571 }
572
Calin Juravle6ae39fc2018-01-19 20:32:47 -0800573 public void destroyProfileSnapshot(String packageName, String profileName)
Calin Juravlefd9f8ae2017-11-29 18:26:55 -0800574 throws InstallerException {
575 if (!checkBeforeRemote()) return;
576 try {
Calin Juravle6ae39fc2018-01-19 20:32:47 -0800577 mInstalld.destroyProfileSnapshot(packageName, profileName);
Calin Juravlefd9f8ae2017-11-29 18:26:55 -0800578 } catch (Exception e) {
579 throw InstallerException.from(e);
580 }
581 }
582
Jeff Sharkey7d25faf2017-01-16 20:58:40 -0700583 public void invalidateMounts() throws InstallerException {
584 if (!checkBeforeRemote()) return;
585 try {
586 mInstalld.invalidateMounts();
587 } catch (Exception e) {
588 throw InstallerException.from(e);
589 }
590 }
591
Jeff Sharkeyd5d5e922017-02-21 10:51:23 -0700592 public boolean isQuotaSupported(String volumeUuid) throws InstallerException {
593 if (!checkBeforeRemote()) return false;
594 try {
595 return mInstalld.isQuotaSupported(volumeUuid);
596 } catch (Exception e) {
597 throw InstallerException.from(e);
598 }
599 }
600
Calin Juravle3621be72018-01-18 15:17:29 -0800601 public boolean prepareAppProfile(String pkg, @UserIdInt int userId, @AppIdInt int appId,
602 String profileName, String codePath, String dexMetadataPath) throws InstallerException {
603 if (!checkBeforeRemote()) return false;
Jeff Sharkeydd02e332018-06-27 14:41:57 -0600604 BlockGuard.getVmPolicy().onPathAccess(codePath);
605 BlockGuard.getVmPolicy().onPathAccess(dexMetadataPath);
Calin Juravle3621be72018-01-18 15:17:29 -0800606 try {
607 return mInstalld.prepareAppProfile(pkg, userId, appId, profileName, codePath,
608 dexMetadataPath);
609 } catch (Exception e) {
610 throw InstallerException.from(e);
611 }
612 }
613
Jeff Sharkeyfdeeeea2016-01-11 17:34:24 -0700614 private static void assertValidInstructionSet(String instructionSet)
615 throws InstallerException {
Narayan Kamath6c4b9de2014-08-08 12:44:12 +0100616 for (String abi : Build.SUPPORTED_ABIS) {
Jeff Sharkeyfdeeeea2016-01-11 17:34:24 -0700617 if (VMRuntime.getInstructionSet(abi).equals(instructionSet)) {
618 return;
Narayan Kamath6c4b9de2014-08-08 12:44:12 +0100619 }
620 }
Jeff Sharkeyfdeeeea2016-01-11 17:34:24 -0700621 throw new InstallerException("Invalid instruction set: " + instructionSet);
Narayan Kamath6c4b9de2014-08-08 12:44:12 +0100622 }
Jeff Sharkey740f5232016-12-09 14:31:26 -0700623
624 public static class InstallerException extends Exception {
625 public InstallerException(String detailMessage) {
626 super(detailMessage);
627 }
628
629 public static InstallerException from(Exception e) throws InstallerException {
Jeff Sharkey447a3ac2016-12-12 10:15:37 -0700630 throw new InstallerException(e.toString());
Jeff Sharkey740f5232016-12-09 14:31:26 -0700631 }
632 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633}