Installer.java revision 0034788844fd053f87beee60092c1dd44c7046e2
1/*
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
17package com.android.server.pm;
18
19import android.annotation.Nullable;
20import android.content.Context;
21import android.content.pm.PackageStats;
22import android.os.Build;
23import android.os.IBinder;
24import android.os.IBinder.DeathRecipient;
25import android.os.IInstalld;
26import android.os.RemoteException;
27import android.os.ServiceManager;
28import android.text.format.DateUtils;
29import android.util.Slog;
30
31import com.android.internal.os.BackgroundThread;
32import com.android.server.SystemService;
33
34import dalvik.system.VMRuntime;
35
36public class Installer extends SystemService {
37    private static final String TAG = "Installer";
38
39    /* ***************************************************************************
40     * IMPORTANT: These values are passed to native code. Keep them in sync with
41     * frameworks/native/cmds/installd/installd.h
42     * **************************************************************************/
43    /** Application should be visible to everyone */
44    public static final int DEXOPT_PUBLIC         = 1 << 1;
45    /** Application wants to run in VM safe mode */
46    public static final int DEXOPT_SAFEMODE       = 1 << 2;
47    /** Application wants to allow debugging of its code */
48    public static final int DEXOPT_DEBUGGABLE     = 1 << 3;
49    /** The system boot has finished */
50    public static final int DEXOPT_BOOTCOMPLETE   = 1 << 4;
51    /** Hint that the dexopt type is profile-guided. */
52    public static final int DEXOPT_PROFILE_GUIDED = 1 << 5;
53    /** The compilation is for a secondary dex file. */
54    public static final int DEXOPT_SECONDARY_DEX  = 1 << 6;
55    /** Ignore the result of dexoptNeeded and force compilation. */
56    public static final int DEXOPT_FORCE          = 1 << 7;
57    /** Indicates that the dex file passed to dexopt in on CE storage. */
58    public static final int DEXOPT_STORAGE_CE     = 1 << 8;
59    /** Indicates that the dex file passed to dexopt in on DE storage. */
60    public static final int DEXOPT_STORAGE_DE     = 1 << 9;
61
62    // NOTE: keep in sync with installd
63    public static final int FLAG_CLEAR_CACHE_ONLY = 1 << 8;
64    public static final int FLAG_CLEAR_CODE_CACHE_ONLY = 1 << 9;
65    public static final int FLAG_USE_QUOTA = 1 << 12;
66    public static final int FLAG_FREE_CACHE_V2 = 1 << 13;
67    public static final int FLAG_FREE_CACHE_V2_DEFY_QUOTA = 1 << 14;
68    public static final int FLAG_FREE_CACHE_NOOP = 1 << 15;
69    public static final int FLAG_FORCE = 1 << 16;
70
71    private final boolean mIsolated;
72
73    private volatile IInstalld mInstalld;
74    private volatile Object mWarnIfHeld;
75
76    public Installer(Context context) {
77        this(context, false);
78    }
79
80    /**
81     * @param isolated indicates if this object should <em>not</em> connect to
82     *            the real {@code installd}. All remote calls will be ignored
83     *            unless you extend this class and intercept them.
84     */
85    public Installer(Context context, boolean isolated) {
86        super(context);
87        mIsolated = isolated;
88    }
89
90    /**
91     * Yell loudly if someone tries making future calls while holding a lock on
92     * the given object.
93     */
94    public void setWarnIfHeld(Object warnIfHeld) {
95        mWarnIfHeld = warnIfHeld;
96    }
97
98    @Override
99    public void onStart() {
100        if (mIsolated) {
101            mInstalld = null;
102        } else {
103            connect();
104        }
105    }
106
107    private void connect() {
108        IBinder binder = ServiceManager.getService("installd");
109        if (binder != null) {
110            try {
111                binder.linkToDeath(new DeathRecipient() {
112                    @Override
113                    public void binderDied() {
114                        Slog.w(TAG, "installd died; reconnecting");
115                        connect();
116                    }
117                }, 0);
118            } catch (RemoteException e) {
119                binder = null;
120            }
121        }
122
123        if (binder != null) {
124            mInstalld = IInstalld.Stub.asInterface(binder);
125            try {
126                invalidateMounts();
127            } catch (InstallerException ignored) {
128            }
129        } else {
130            Slog.w(TAG, "installd not found; trying again");
131            BackgroundThread.getHandler().postDelayed(() -> {
132                connect();
133            }, DateUtils.SECOND_IN_MILLIS);
134        }
135    }
136
137    /**
138     * Do several pre-flight checks before making a remote call.
139     *
140     * @return if the remote call should continue.
141     */
142    private boolean checkBeforeRemote() {
143        if (mWarnIfHeld != null && Thread.holdsLock(mWarnIfHeld)) {
144            Slog.wtf(TAG, "Calling thread " + Thread.currentThread().getName() + " is holding 0x"
145                    + Integer.toHexString(System.identityHashCode(mWarnIfHeld)), new Throwable());
146        }
147        if (mIsolated) {
148            Slog.i(TAG, "Ignoring request because this installer is isolated");
149            return false;
150        } else {
151            return true;
152        }
153    }
154
155    public long createAppData(String uuid, String packageName, int userId, int flags, int appId,
156            String seInfo, int targetSdkVersion) throws InstallerException {
157        if (!checkBeforeRemote()) return -1;
158        try {
159            return mInstalld.createAppData(uuid, packageName, userId, flags, appId, seInfo,
160                    targetSdkVersion);
161        } catch (Exception e) {
162            throw InstallerException.from(e);
163        }
164    }
165
166    public void restoreconAppData(String uuid, String packageName, int userId, int flags, int appId,
167            String seInfo) throws InstallerException {
168        if (!checkBeforeRemote()) return;
169        try {
170            mInstalld.restoreconAppData(uuid, packageName, userId, flags, appId, seInfo);
171        } catch (Exception e) {
172            throw InstallerException.from(e);
173        }
174    }
175
176    public void migrateAppData(String uuid, String packageName, int userId, int flags)
177            throws InstallerException {
178        if (!checkBeforeRemote()) return;
179        try {
180            mInstalld.migrateAppData(uuid, packageName, userId, flags);
181        } catch (Exception e) {
182            throw InstallerException.from(e);
183        }
184    }
185
186    public void clearAppData(String uuid, String packageName, int userId, int flags,
187            long ceDataInode) throws InstallerException {
188        if (!checkBeforeRemote()) return;
189        try {
190            mInstalld.clearAppData(uuid, packageName, userId, flags, ceDataInode);
191        } catch (Exception e) {
192            throw InstallerException.from(e);
193        }
194    }
195
196    public void destroyAppData(String uuid, String packageName, int userId, int flags,
197            long ceDataInode) throws InstallerException {
198        if (!checkBeforeRemote()) return;
199        try {
200            mInstalld.destroyAppData(uuid, packageName, userId, flags, ceDataInode);
201        } catch (Exception e) {
202            throw InstallerException.from(e);
203        }
204    }
205
206    public void fixupAppData(String uuid, int flags) throws InstallerException {
207        if (!checkBeforeRemote()) return;
208        try {
209            mInstalld.fixupAppData(uuid, flags);
210        } catch (Exception e) {
211            throw InstallerException.from(e);
212        }
213    }
214
215    public void moveCompleteApp(String fromUuid, String toUuid, String packageName,
216            String dataAppName, int appId, String seInfo, int targetSdkVersion)
217            throws InstallerException {
218        if (!checkBeforeRemote()) return;
219        try {
220            mInstalld.moveCompleteApp(fromUuid, toUuid, packageName, dataAppName, appId, seInfo,
221                    targetSdkVersion);
222        } catch (Exception e) {
223            throw InstallerException.from(e);
224        }
225    }
226
227    public void getAppSize(String uuid, String[] packageNames, int userId, int flags, int appId,
228            long[] ceDataInodes, String[] codePaths, PackageStats stats)
229            throws InstallerException {
230        if (!checkBeforeRemote()) return;
231        try {
232            final long[] res = mInstalld.getAppSize(uuid, packageNames, userId, flags,
233                    appId, ceDataInodes, codePaths);
234            stats.codeSize += res[0];
235            stats.dataSize += res[1];
236            stats.cacheSize += res[2];
237            stats.externalCodeSize += res[3];
238            stats.externalDataSize += res[4];
239            stats.externalCacheSize += res[5];
240        } catch (Exception e) {
241            throw InstallerException.from(e);
242        }
243    }
244
245    public void getUserSize(String uuid, int userId, int flags, int[] appIds, PackageStats stats)
246            throws InstallerException {
247        if (!checkBeforeRemote()) return;
248        try {
249            final long[] res = mInstalld.getUserSize(uuid, userId, flags, appIds);
250            stats.codeSize += res[0];
251            stats.dataSize += res[1];
252            stats.cacheSize += res[2];
253            stats.externalCodeSize += res[3];
254            stats.externalDataSize += res[4];
255            stats.externalCacheSize += res[5];
256        } catch (Exception e) {
257            throw InstallerException.from(e);
258        }
259    }
260
261    public long[] getExternalSize(String uuid, int userId, int flags, int[] appIds)
262            throws InstallerException {
263        if (!checkBeforeRemote()) return new long[4];
264        try {
265            return mInstalld.getExternalSize(uuid, userId, flags, appIds);
266        } catch (Exception e) {
267            throw InstallerException.from(e);
268        }
269    }
270
271    public void setAppQuota(String uuid, int userId, int appId, long cacheQuota)
272            throws InstallerException {
273        if (!checkBeforeRemote()) return;
274        try {
275            mInstalld.setAppQuota(uuid, userId, appId, cacheQuota);
276        } catch (Exception e) {
277            throw InstallerException.from(e);
278        }
279    }
280
281    public void dexopt(String apkPath, int uid, @Nullable String pkgName, String instructionSet,
282            int dexoptNeeded, @Nullable String outputPath, int dexFlags,
283            String compilerFilter, @Nullable String volumeUuid, @Nullable String sharedLibraries,
284            @Nullable String seInfo)
285            throws InstallerException {
286        assertValidInstructionSet(instructionSet);
287        if (!checkBeforeRemote()) return;
288        try {
289            mInstalld.dexopt(apkPath, uid, pkgName, instructionSet, dexoptNeeded, outputPath,
290                    dexFlags, compilerFilter, volumeUuid, sharedLibraries, seInfo);
291        } catch (Exception e) {
292            throw InstallerException.from(e);
293        }
294    }
295
296    public boolean mergeProfiles(int uid, String packageName) throws InstallerException {
297        if (!checkBeforeRemote()) return false;
298        try {
299            return mInstalld.mergeProfiles(uid, packageName);
300        } catch (Exception e) {
301            throw InstallerException.from(e);
302        }
303    }
304
305    public boolean dumpProfiles(int uid, String packageName, String codePaths)
306            throws InstallerException {
307        if (!checkBeforeRemote()) return false;
308        try {
309            return mInstalld.dumpProfiles(uid, packageName, codePaths);
310        } catch (Exception e) {
311            throw InstallerException.from(e);
312        }
313    }
314
315    public void idmap(String targetApkPath, String overlayApkPath, int uid)
316            throws InstallerException {
317        if (!checkBeforeRemote()) return;
318        try {
319            mInstalld.idmap(targetApkPath, overlayApkPath, uid);
320        } catch (Exception e) {
321            throw InstallerException.from(e);
322        }
323    }
324
325    public void removeIdmap(String overlayApkPath) throws InstallerException {
326        if (!checkBeforeRemote()) return;
327        try {
328            mInstalld.removeIdmap(overlayApkPath);
329        } catch (Exception e) {
330            throw InstallerException.from(e);
331        }
332    }
333
334    public void rmdex(String codePath, String instructionSet) throws InstallerException {
335        assertValidInstructionSet(instructionSet);
336        if (!checkBeforeRemote()) return;
337        try {
338            mInstalld.rmdex(codePath, instructionSet);
339        } catch (Exception e) {
340            throw InstallerException.from(e);
341        }
342    }
343
344    public void rmPackageDir(String packageDir) throws InstallerException {
345        if (!checkBeforeRemote()) return;
346        try {
347            mInstalld.rmPackageDir(packageDir);
348        } catch (Exception e) {
349            throw InstallerException.from(e);
350        }
351    }
352
353    public void clearAppProfiles(String packageName) throws InstallerException {
354        if (!checkBeforeRemote()) return;
355        try {
356            mInstalld.clearAppProfiles(packageName);
357        } catch (Exception e) {
358            throw InstallerException.from(e);
359        }
360    }
361
362    public void destroyAppProfiles(String packageName) throws InstallerException {
363        if (!checkBeforeRemote()) return;
364        try {
365            mInstalld.destroyAppProfiles(packageName);
366        } catch (Exception e) {
367            throw InstallerException.from(e);
368        }
369    }
370
371    public void createUserData(String uuid, int userId, int userSerial, int flags)
372            throws InstallerException {
373        if (!checkBeforeRemote()) return;
374        try {
375            mInstalld.createUserData(uuid, userId, userSerial, flags);
376        } catch (Exception e) {
377            throw InstallerException.from(e);
378        }
379    }
380
381    public void destroyUserData(String uuid, int userId, int flags) throws InstallerException {
382        if (!checkBeforeRemote()) return;
383        try {
384            mInstalld.destroyUserData(uuid, userId, flags);
385        } catch (Exception e) {
386            throw InstallerException.from(e);
387        }
388    }
389
390    public void markBootComplete(String instructionSet) throws InstallerException {
391        assertValidInstructionSet(instructionSet);
392        if (!checkBeforeRemote()) return;
393        try {
394            mInstalld.markBootComplete(instructionSet);
395        } catch (Exception e) {
396            throw InstallerException.from(e);
397        }
398    }
399
400    public void freeCache(String uuid, long freeStorageSize, int flags) throws InstallerException {
401        if (!checkBeforeRemote()) return;
402        try {
403            mInstalld.freeCache(uuid, freeStorageSize, flags);
404        } catch (Exception e) {
405            throw InstallerException.from(e);
406        }
407    }
408
409    /**
410     * Links the 32 bit native library directory in an application's data
411     * directory to the real location for backward compatibility. Note that no
412     * such symlink is created for 64 bit shared libraries.
413     */
414    public void linkNativeLibraryDirectory(String uuid, String packageName, String nativeLibPath32,
415            int userId) throws InstallerException {
416        if (!checkBeforeRemote()) return;
417        try {
418            mInstalld.linkNativeLibraryDirectory(uuid, packageName, nativeLibPath32, userId);
419        } catch (Exception e) {
420            throw InstallerException.from(e);
421        }
422    }
423
424    public void createOatDir(String oatDir, String dexInstructionSet)
425            throws InstallerException {
426        if (!checkBeforeRemote()) return;
427        try {
428            mInstalld.createOatDir(oatDir, dexInstructionSet);
429        } catch (Exception e) {
430            throw InstallerException.from(e);
431        }
432    }
433
434    public void linkFile(String relativePath, String fromBase, String toBase)
435            throws InstallerException {
436        if (!checkBeforeRemote()) return;
437        try {
438            mInstalld.linkFile(relativePath, fromBase, toBase);
439        } catch (Exception e) {
440            throw InstallerException.from(e);
441        }
442    }
443
444    public void moveAb(String apkPath, String instructionSet, String outputPath)
445            throws InstallerException {
446        if (!checkBeforeRemote()) return;
447        try {
448            mInstalld.moveAb(apkPath, instructionSet, outputPath);
449        } catch (Exception e) {
450            throw InstallerException.from(e);
451        }
452    }
453
454    public void deleteOdex(String apkPath, String instructionSet, String outputPath)
455            throws InstallerException {
456        if (!checkBeforeRemote()) return;
457        try {
458            mInstalld.deleteOdex(apkPath, instructionSet, outputPath);
459        } catch (Exception e) {
460            throw InstallerException.from(e);
461        }
462    }
463
464    public boolean reconcileSecondaryDexFile(String apkPath, String packageName, int uid,
465            String[] isas, @Nullable String volumeUuid, int flags) throws InstallerException {
466        for (int i = 0; i < isas.length; i++) {
467            assertValidInstructionSet(isas[i]);
468        }
469        if (!checkBeforeRemote()) return false;
470        try {
471            return mInstalld.reconcileSecondaryDexFile(apkPath, packageName, uid, isas,
472                    volumeUuid, flags);
473        } catch (Exception e) {
474            throw InstallerException.from(e);
475        }
476    }
477
478    public void invalidateMounts() throws InstallerException {
479        if (!checkBeforeRemote()) return;
480        try {
481            mInstalld.invalidateMounts();
482        } catch (Exception e) {
483            throw InstallerException.from(e);
484        }
485    }
486
487    public boolean isQuotaSupported(String volumeUuid) throws InstallerException {
488        if (!checkBeforeRemote()) return false;
489        try {
490            return mInstalld.isQuotaSupported(volumeUuid);
491        } catch (Exception e) {
492            throw InstallerException.from(e);
493        }
494    }
495
496    private static void assertValidInstructionSet(String instructionSet)
497            throws InstallerException {
498        for (String abi : Build.SUPPORTED_ABIS) {
499            if (VMRuntime.getInstructionSet(abi).equals(instructionSet)) {
500                return;
501            }
502        }
503        throw new InstallerException("Invalid instruction set: " + instructionSet);
504    }
505
506    public static class InstallerException extends Exception {
507        public InstallerException(String detailMessage) {
508            super(detailMessage);
509        }
510
511        public static InstallerException from(Exception e) throws InstallerException {
512            throw new InstallerException(e.toString());
513        }
514    }
515}
516