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