Installer.java revision 811a75a0c0dbe211ccabdfbfba60c8f76d5738eb
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) throws InstallerException {
262        if (!checkBeforeRemote()) return new long[4];
263        try {
264            return mInstalld.getExternalSize(uuid, userId, flags);
265        } catch (Exception e) {
266            throw InstallerException.from(e);
267        }
268    }
269
270    public void setAppQuota(String uuid, int userId, int appId, long cacheQuota)
271            throws InstallerException {
272        if (!checkBeforeRemote()) return;
273        try {
274            mInstalld.setAppQuota(uuid, userId, appId, cacheQuota);
275        } catch (Exception e) {
276            throw InstallerException.from(e);
277        }
278    }
279
280    public void dexopt(String apkPath, int uid, @Nullable String pkgName, String instructionSet,
281            int dexoptNeeded, @Nullable String outputPath, int dexFlags,
282            String compilerFilter, @Nullable String volumeUuid, @Nullable String sharedLibraries,
283            @Nullable String seInfo)
284            throws InstallerException {
285        assertValidInstructionSet(instructionSet);
286        if (!checkBeforeRemote()) return;
287        try {
288            mInstalld.dexopt(apkPath, uid, pkgName, instructionSet, dexoptNeeded, outputPath,
289                    dexFlags, compilerFilter, volumeUuid, sharedLibraries, seInfo);
290        } catch (Exception e) {
291            throw InstallerException.from(e);
292        }
293    }
294
295    public boolean mergeProfiles(int uid, String packageName) throws InstallerException {
296        if (!checkBeforeRemote()) return false;
297        try {
298            return mInstalld.mergeProfiles(uid, packageName);
299        } catch (Exception e) {
300            throw InstallerException.from(e);
301        }
302    }
303
304    public boolean dumpProfiles(int uid, String packageName, String codePaths)
305            throws InstallerException {
306        if (!checkBeforeRemote()) return false;
307        try {
308            return mInstalld.dumpProfiles(uid, packageName, codePaths);
309        } catch (Exception e) {
310            throw InstallerException.from(e);
311        }
312    }
313
314    public void idmap(String targetApkPath, String overlayApkPath, int uid)
315            throws InstallerException {
316        if (!checkBeforeRemote()) return;
317        try {
318            mInstalld.idmap(targetApkPath, overlayApkPath, uid);
319        } catch (Exception e) {
320            throw InstallerException.from(e);
321        }
322    }
323
324    public void removeIdmap(String overlayApkPath) throws InstallerException {
325        if (!checkBeforeRemote()) return;
326        try {
327            mInstalld.removeIdmap(overlayApkPath);
328        } catch (Exception e) {
329            throw InstallerException.from(e);
330        }
331    }
332
333    public void rmdex(String codePath, String instructionSet) throws InstallerException {
334        assertValidInstructionSet(instructionSet);
335        if (!checkBeforeRemote()) return;
336        try {
337            mInstalld.rmdex(codePath, instructionSet);
338        } catch (Exception e) {
339            throw InstallerException.from(e);
340        }
341    }
342
343    public void rmPackageDir(String packageDir) throws InstallerException {
344        if (!checkBeforeRemote()) return;
345        try {
346            mInstalld.rmPackageDir(packageDir);
347        } catch (Exception e) {
348            throw InstallerException.from(e);
349        }
350    }
351
352    public void clearAppProfiles(String packageName) throws InstallerException {
353        if (!checkBeforeRemote()) return;
354        try {
355            mInstalld.clearAppProfiles(packageName);
356        } catch (Exception e) {
357            throw InstallerException.from(e);
358        }
359    }
360
361    public void destroyAppProfiles(String packageName) throws InstallerException {
362        if (!checkBeforeRemote()) return;
363        try {
364            mInstalld.destroyAppProfiles(packageName);
365        } catch (Exception e) {
366            throw InstallerException.from(e);
367        }
368    }
369
370    public void createUserData(String uuid, int userId, int userSerial, int flags)
371            throws InstallerException {
372        if (!checkBeforeRemote()) return;
373        try {
374            mInstalld.createUserData(uuid, userId, userSerial, flags);
375        } catch (Exception e) {
376            throw InstallerException.from(e);
377        }
378    }
379
380    public void destroyUserData(String uuid, int userId, int flags) throws InstallerException {
381        if (!checkBeforeRemote()) return;
382        try {
383            mInstalld.destroyUserData(uuid, userId, flags);
384        } catch (Exception e) {
385            throw InstallerException.from(e);
386        }
387    }
388
389    public void markBootComplete(String instructionSet) throws InstallerException {
390        assertValidInstructionSet(instructionSet);
391        if (!checkBeforeRemote()) return;
392        try {
393            mInstalld.markBootComplete(instructionSet);
394        } catch (Exception e) {
395            throw InstallerException.from(e);
396        }
397    }
398
399    public void freeCache(String uuid, long freeStorageSize, int flags) throws InstallerException {
400        if (!checkBeforeRemote()) return;
401        try {
402            mInstalld.freeCache(uuid, freeStorageSize, flags);
403        } catch (Exception e) {
404            throw InstallerException.from(e);
405        }
406    }
407
408    /**
409     * Links the 32 bit native library directory in an application's data
410     * directory to the real location for backward compatibility. Note that no
411     * such symlink is created for 64 bit shared libraries.
412     */
413    public void linkNativeLibraryDirectory(String uuid, String packageName, String nativeLibPath32,
414            int userId) throws InstallerException {
415        if (!checkBeforeRemote()) return;
416        try {
417            mInstalld.linkNativeLibraryDirectory(uuid, packageName, nativeLibPath32, userId);
418        } catch (Exception e) {
419            throw InstallerException.from(e);
420        }
421    }
422
423    public void createOatDir(String oatDir, String dexInstructionSet)
424            throws InstallerException {
425        if (!checkBeforeRemote()) return;
426        try {
427            mInstalld.createOatDir(oatDir, dexInstructionSet);
428        } catch (Exception e) {
429            throw InstallerException.from(e);
430        }
431    }
432
433    public void linkFile(String relativePath, String fromBase, String toBase)
434            throws InstallerException {
435        if (!checkBeforeRemote()) return;
436        try {
437            mInstalld.linkFile(relativePath, fromBase, toBase);
438        } catch (Exception e) {
439            throw InstallerException.from(e);
440        }
441    }
442
443    public void moveAb(String apkPath, String instructionSet, String outputPath)
444            throws InstallerException {
445        if (!checkBeforeRemote()) return;
446        try {
447            mInstalld.moveAb(apkPath, instructionSet, outputPath);
448        } catch (Exception e) {
449            throw InstallerException.from(e);
450        }
451    }
452
453    public void deleteOdex(String apkPath, String instructionSet, String outputPath)
454            throws InstallerException {
455        if (!checkBeforeRemote()) return;
456        try {
457            mInstalld.deleteOdex(apkPath, instructionSet, outputPath);
458        } catch (Exception e) {
459            throw InstallerException.from(e);
460        }
461    }
462
463    public boolean reconcileSecondaryDexFile(String apkPath, String packageName, int uid,
464            String[] isas, @Nullable String volumeUuid, int flags) throws InstallerException {
465        for (int i = 0; i < isas.length; i++) {
466            assertValidInstructionSet(isas[i]);
467        }
468        if (!checkBeforeRemote()) return false;
469        try {
470            return mInstalld.reconcileSecondaryDexFile(apkPath, packageName, uid, isas,
471                    volumeUuid, flags);
472        } catch (Exception e) {
473            throw InstallerException.from(e);
474        }
475    }
476
477    public void invalidateMounts() throws InstallerException {
478        if (!checkBeforeRemote()) return;
479        try {
480            mInstalld.invalidateMounts();
481        } catch (Exception e) {
482            throw InstallerException.from(e);
483        }
484    }
485
486    public boolean isQuotaSupported(String volumeUuid) throws InstallerException {
487        if (!checkBeforeRemote()) return false;
488        try {
489            return mInstalld.isQuotaSupported(volumeUuid);
490        } catch (Exception e) {
491            throw InstallerException.from(e);
492        }
493    }
494
495    private static void assertValidInstructionSet(String instructionSet)
496            throws InstallerException {
497        for (String abi : Build.SUPPORTED_ABIS) {
498            if (VMRuntime.getInstructionSet(abi).equals(instructionSet)) {
499                return;
500            }
501        }
502        throw new InstallerException("Invalid instruction set: " + instructionSet);
503    }
504
505    public static class InstallerException extends Exception {
506        public InstallerException(String detailMessage) {
507            super(detailMessage);
508        }
509
510        public static InstallerException from(Exception e) throws InstallerException {
511            throw new InstallerException(e.toString());
512        }
513    }
514}
515