Installer.java revision 70b4d101fe92adf972eaf51ab91e77c46adf1b36
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.IInstalld;
24import android.os.RemoteException;
25import android.os.ServiceManager;
26import android.os.ServiceSpecificException;
27import android.util.Slog;
28
29import com.android.internal.os.InstallerConnection;
30import com.android.internal.os.InstallerConnection.InstallerException;
31import com.android.server.SystemService;
32
33import dalvik.system.VMRuntime;
34
35import java.util.Arrays;
36
37public final class Installer extends SystemService {
38    private static final String TAG = "Installer";
39
40    private static final boolean USE_BINDER = true;
41
42    /* ***************************************************************************
43     * IMPORTANT: These values are passed to native code. Keep them in sync with
44     * frameworks/native/cmds/installd/installd.h
45     * **************************************************************************/
46    /** Application should be visible to everyone */
47    public static final int DEXOPT_PUBLIC         = 1 << 1;
48    /** Application wants to run in VM safe mode */
49    public static final int DEXOPT_SAFEMODE       = 1 << 2;
50    /** Application wants to allow debugging of its code */
51    public static final int DEXOPT_DEBUGGABLE     = 1 << 3;
52    /** The system boot has finished */
53    public static final int DEXOPT_BOOTCOMPLETE   = 1 << 4;
54    /** Hint that the dexopt type is profile-guided. */
55    public static final int DEXOPT_PROFILE_GUIDED = 1 << 5;
56    /** This is an OTA update dexopt */
57    public static final int DEXOPT_OTA            = 1 << 6;
58
59    // NOTE: keep in sync with installd
60    public static final int FLAG_CLEAR_CACHE_ONLY = 1 << 8;
61    public static final int FLAG_CLEAR_CODE_CACHE_ONLY = 1 << 9;
62
63    private final InstallerConnection mInstaller;
64    private final IInstalld mInstalld;
65
66    public Installer(Context context) {
67        super(context);
68        mInstaller = new InstallerConnection();
69        // TODO: reconnect if installd restarts
70        mInstalld = IInstalld.Stub.asInterface(ServiceManager.getService("installd"));
71    }
72
73    // Package-private installer that accepts a custom InstallerConnection. Used for
74    // OtaDexoptService.
75    Installer(Context context, InstallerConnection connection) {
76        super(context);
77        mInstaller = connection;
78        // TODO: reconnect if installd restarts
79        mInstalld = IInstalld.Stub.asInterface(ServiceManager.getService("installd"));
80    }
81
82    /**
83     * Yell loudly if someone tries making future calls while holding a lock on
84     * the given object.
85     */
86    public void setWarnIfHeld(Object warnIfHeld) {
87        mInstaller.setWarnIfHeld(warnIfHeld);
88    }
89
90    @Override
91    public void onStart() {
92        Slog.i(TAG, "Waiting for installd to be ready.");
93        mInstaller.waitForConnection();
94    }
95
96    public void createAppData(String uuid, String pkgname, int userid, int flags, int appid,
97            String seinfo, int targetSdkVersion) throws InstallerException {
98        if (USE_BINDER) {
99            try {
100                mInstalld.createAppData(uuid, pkgname, userid, flags, appid, seinfo,
101                        targetSdkVersion);
102            } catch (RemoteException | ServiceSpecificException e) {
103                throw new InstallerException(e.getMessage());
104            }
105        } else {
106            mInstaller.execute("create_app_data", uuid, pkgname, userid, flags, appid, seinfo,
107                    targetSdkVersion);
108        }
109    }
110
111    public void restoreconAppData(String uuid, String pkgname, int userid, int flags, int appid,
112            String seinfo) throws InstallerException {
113        mInstaller.execute("restorecon_app_data", uuid, pkgname, userid, flags, appid,
114                seinfo);
115    }
116
117    public void migrateAppData(String uuid, String pkgname, int userid, int flags)
118            throws InstallerException {
119        mInstaller.execute("migrate_app_data", uuid, pkgname, userid, flags);
120    }
121
122    public void clearAppData(String uuid, String pkgname, int userid, int flags, long ceDataInode)
123            throws InstallerException {
124        mInstaller.execute("clear_app_data", uuid, pkgname, userid, flags, ceDataInode);
125    }
126
127    public void destroyAppData(String uuid, String pkgname, int userid, int flags, long ceDataInode)
128            throws InstallerException {
129        mInstaller.execute("destroy_app_data", uuid, pkgname, userid, flags, ceDataInode);
130    }
131
132    public void moveCompleteApp(String from_uuid, String to_uuid, String package_name,
133            String data_app_name, int appid, String seinfo, int targetSdkVersion)
134            throws InstallerException {
135        mInstaller.execute("move_complete_app", from_uuid, to_uuid, package_name,
136                data_app_name, appid, seinfo, targetSdkVersion);
137    }
138
139    public void getAppSize(String uuid, String pkgname, int userid, int flags, long ceDataInode,
140            String codePath, PackageStats stats) throws InstallerException {
141        final String[] res = mInstaller.execute("get_app_size", uuid, pkgname, userid, flags,
142                ceDataInode, codePath);
143        try {
144            stats.codeSize += Long.parseLong(res[1]);
145            stats.dataSize += Long.parseLong(res[2]);
146            stats.cacheSize += Long.parseLong(res[3]);
147        } catch (ArrayIndexOutOfBoundsException | NumberFormatException e) {
148            throw new InstallerException("Invalid size result: " + Arrays.toString(res));
149        }
150    }
151
152    public long getAppDataInode(String uuid, String pkgname, int userid, int flags)
153            throws InstallerException {
154        final String[] res = mInstaller.execute("get_app_data_inode", uuid, pkgname, userid, flags);
155        try {
156            return Long.parseLong(res[1]);
157        } catch (ArrayIndexOutOfBoundsException | NumberFormatException e) {
158            throw new InstallerException("Invalid inode result: " + Arrays.toString(res));
159        }
160    }
161
162    public void dexopt(String apkPath, int uid, String instructionSet, int dexoptNeeded,
163            int dexFlags, String compilerFilter, String volumeUuid, String sharedLibraries)
164            throws InstallerException {
165        assertValidInstructionSet(instructionSet);
166        mInstaller.dexopt(apkPath, uid, instructionSet, dexoptNeeded, dexFlags,
167                compilerFilter, volumeUuid, sharedLibraries);
168    }
169
170    public void dexopt(String apkPath, int uid, String pkgName, String instructionSet,
171            int dexoptNeeded, @Nullable String outputPath, int dexFlags,
172            String compilerFilter, String volumeUuid, String sharedLibraries)
173            throws InstallerException {
174        assertValidInstructionSet(instructionSet);
175        mInstaller.dexopt(apkPath, uid, pkgName, instructionSet, dexoptNeeded,
176                outputPath, dexFlags, compilerFilter, volumeUuid, sharedLibraries);
177    }
178
179    public boolean mergeProfiles(int uid, String pkgName) throws InstallerException {
180        return mInstaller.mergeProfiles(uid, pkgName);
181    }
182
183    public boolean dumpProfiles(String gid, String packageName, String codePaths)
184            throws InstallerException {
185        return mInstaller.dumpProfiles(gid, packageName, codePaths);
186    }
187
188    public void idmap(String targetApkPath, String overlayApkPath, int uid)
189            throws InstallerException {
190        mInstaller.execute("idmap", targetApkPath, overlayApkPath, uid);
191    }
192
193    public void rmdex(String codePath, String instructionSet) throws InstallerException {
194        assertValidInstructionSet(instructionSet);
195        mInstaller.execute("rmdex", codePath, instructionSet);
196    }
197
198    public void rmPackageDir(String packageDir) throws InstallerException {
199        mInstaller.execute("rmpackagedir", packageDir);
200    }
201
202    public void clearAppProfiles(String pkgName) throws InstallerException {
203        mInstaller.execute("clear_app_profiles", pkgName);
204    }
205
206    public void destroyAppProfiles(String pkgName) throws InstallerException {
207        mInstaller.execute("destroy_app_profiles", pkgName);
208    }
209
210    public void createUserData(String uuid, int userId, int userSerial, int flags)
211            throws InstallerException {
212        mInstaller.execute("create_user_data", uuid, userId, userSerial, flags);
213    }
214
215    public void destroyUserData(String uuid, int userId, int flags) throws InstallerException {
216        mInstaller.execute("destroy_user_data", uuid, userId, flags);
217    }
218
219    public void markBootComplete(String instructionSet) throws InstallerException {
220        assertValidInstructionSet(instructionSet);
221        mInstaller.execute("markbootcomplete", instructionSet);
222    }
223
224    public void freeCache(String uuid, long freeStorageSize) throws InstallerException {
225        mInstaller.execute("freecache", uuid, freeStorageSize);
226    }
227
228    /**
229     * Links the 32 bit native library directory in an application's data
230     * directory to the real location for backward compatibility. Note that no
231     * such symlink is created for 64 bit shared libraries.
232     */
233    public void linkNativeLibraryDirectory(String uuid, String dataPath, String nativeLibPath32,
234            int userId) throws InstallerException {
235        mInstaller.execute("linklib", uuid, dataPath, nativeLibPath32, userId);
236    }
237
238    public void createOatDir(String oatDir, String dexInstructionSet)
239            throws InstallerException {
240        mInstaller.execute("createoatdir", oatDir, dexInstructionSet);
241    }
242
243    public void linkFile(String relativePath, String fromBase, String toBase)
244            throws InstallerException {
245        mInstaller.execute("linkfile", relativePath, fromBase, toBase);
246    }
247
248    public void moveAb(String apkPath, String instructionSet, String outputPath)
249            throws InstallerException {
250        mInstaller.execute("move_ab", apkPath, instructionSet, outputPath);
251    }
252
253    public void deleteOdex(String apkPath, String instructionSet, String outputPath)
254            throws InstallerException {
255        mInstaller.execute("delete_odex", apkPath, instructionSet, outputPath);
256    }
257
258    private static void assertValidInstructionSet(String instructionSet)
259            throws InstallerException {
260        for (String abi : Build.SUPPORTED_ABIS) {
261            if (VMRuntime.getInstructionSet(abi).equals(instructionSet)) {
262                return;
263            }
264        }
265        throw new InstallerException("Invalid instruction set: " + instructionSet);
266    }
267}
268