blob: 11a649812eb6ef715013637dda3a97fac9c0b158 [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
19import android.content.pm.PackageStats;
Jason parksa3cdaa52011-01-13 14:15:43 -060020import android.net.LocalSocket;
Kenny Rootcf0b38c2011-03-22 14:17:59 -070021import android.net.LocalSocketAddress;
Joe Onorato8a9b2202010-02-26 18:56:32 -080022import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023
24import java.io.IOException;
25import java.io.InputStream;
26import java.io.OutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027
Jeff Brownf69c8122012-09-12 17:00:34 -070028public final class Installer {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029 private static final String TAG = "Installer";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030
Joe Onorato63ebffc2011-04-06 11:47:23 -070031 private static final boolean LOCAL_DEBUG = false;
Kenny Rootcf0b38c2011-03-22 14:17:59 -070032
33 InputStream mIn;
34
35 OutputStream mOut;
36
37 LocalSocket mSocket;
38
39 byte buf[] = new byte[1024];
40
41 int buflen = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042
43 private boolean connect() {
44 if (mSocket != null) {
45 return true;
46 }
Joe Onorato8a9b2202010-02-26 18:56:32 -080047 Slog.i(TAG, "connecting...");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048 try {
49 mSocket = new LocalSocket();
50
Kenny Rootcf0b38c2011-03-22 14:17:59 -070051 LocalSocketAddress address = new LocalSocketAddress("installd",
52 LocalSocketAddress.Namespace.RESERVED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053
54 mSocket.connect(address);
55
56 mIn = mSocket.getInputStream();
57 mOut = mSocket.getOutputStream();
58 } catch (IOException ex) {
59 disconnect();
60 return false;
61 }
62 return true;
63 }
64
Kenny Rootcf0b38c2011-03-22 14:17:59 -070065 private void disconnect() {
66 Slog.i(TAG, "disconnecting...");
67 try {
68 if (mSocket != null)
69 mSocket.close();
70 } catch (IOException ex) {
71 }
72 try {
73 if (mIn != null)
74 mIn.close();
75 } catch (IOException ex) {
76 }
77 try {
78 if (mOut != null)
79 mOut.close();
80 } catch (IOException ex) {
81 }
82 mSocket = null;
83 mIn = null;
84 mOut = null;
85 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086
Kenny Rootcf0b38c2011-03-22 14:17:59 -070087 private boolean readBytes(byte buffer[], int len) {
88 int off = 0, count;
89 if (len < 0)
90 return false;
91 while (off != len) {
92 try {
93 count = mIn.read(buffer, off, len - off);
94 if (count <= 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080095 Slog.e(TAG, "read error " + count);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 break;
97 }
Kenny Rootcf0b38c2011-03-22 14:17:59 -070098 off += count;
99 } catch (IOException ex) {
100 Slog.e(TAG, "read exception");
101 break;
102 }
103 }
104 if (LOCAL_DEBUG) {
105 Slog.i(TAG, "read " + len + " bytes");
106 }
107 if (off == len)
108 return true;
109 disconnect();
110 return false;
111 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700113 private boolean readReply() {
114 int len;
115 buflen = 0;
116 if (!readBytes(buf, 2))
117 return false;
118 len = (((int) buf[0]) & 0xff) | ((((int) buf[1]) & 0xff) << 8);
119 if ((len < 1) || (len > 1024)) {
120 Slog.e(TAG, "invalid reply length (" + len + ")");
121 disconnect();
122 return false;
123 }
124 if (!readBytes(buf, len))
125 return false;
126 buflen = len;
127 return true;
128 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700130 private boolean writeCommand(String _cmd) {
131 byte[] cmd = _cmd.getBytes();
132 int len = cmd.length;
133 if ((len < 1) || (len > 1024))
134 return false;
135 buf[0] = (byte) (len & 0xff);
136 buf[1] = (byte) ((len >> 8) & 0xff);
137 try {
138 mOut.write(buf, 0, 2);
139 mOut.write(cmd, 0, len);
140 } catch (IOException ex) {
141 Slog.e(TAG, "write error");
142 disconnect();
143 return false;
144 }
145 return true;
146 }
147
148 private synchronized String transaction(String cmd) {
149 if (!connect()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800150 Slog.e(TAG, "connection failed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 return "-1";
152 }
153
154 if (!writeCommand(cmd)) {
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700155 /*
156 * If installd died and restarted in the background (unlikely but
157 * possible) we'll fail on the next write (this one). Try to
158 * reconnect and write the command one more time before giving up.
159 */
Joe Onorato8a9b2202010-02-26 18:56:32 -0800160 Slog.e(TAG, "write command failed? reconnect!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 if (!connect() || !writeCommand(cmd)) {
162 return "-1";
163 }
164 }
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700165 if (LOCAL_DEBUG) {
166 Slog.i(TAG, "send: '" + cmd + "'");
167 }
168 if (readReply()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 String s = new String(buf, 0, buflen);
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700170 if (LOCAL_DEBUG) {
171 Slog.i(TAG, "recv: '" + s + "'");
172 }
173 return s;
174 } else {
175 if (LOCAL_DEBUG) {
176 Slog.i(TAG, "fail");
177 }
178 return "-1";
179 }
180 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700182 private int execute(String cmd) {
183 String res = transaction(cmd);
184 try {
185 return Integer.parseInt(res);
186 } catch (NumberFormatException ex) {
187 return -1;
188 }
189 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190
Robert Craig0f40dc92013-03-25 06:33:03 -0400191 public int install(String name, int uid, int gid, String seinfo) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 StringBuilder builder = new StringBuilder("install");
193 builder.append(' ');
194 builder.append(name);
195 builder.append(' ');
196 builder.append(uid);
197 builder.append(' ');
198 builder.append(gid);
Robert Craig0f40dc92013-03-25 06:33:03 -0400199 builder.append(' ');
200 builder.append(seinfo != null ? seinfo : "!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 return execute(builder.toString());
202 }
203
204 public int dexopt(String apkPath, int uid, boolean isPublic) {
205 StringBuilder builder = new StringBuilder("dexopt");
206 builder.append(' ');
207 builder.append(apkPath);
208 builder.append(' ');
209 builder.append(uid);
210 builder.append(isPublic ? " 1" : " 0");
211 return execute(builder.toString());
212 }
213
MÃ¥rten Kongstad48d22322014-01-31 14:43:27 +0100214 public int idmap(String targetApkPath, String overlayApkPath, int uid) {
215 StringBuilder builder = new StringBuilder("idmap");
216 builder.append(' ');
217 builder.append(targetApkPath);
218 builder.append(' ');
219 builder.append(overlayApkPath);
220 builder.append(' ');
221 builder.append(uid);
222 return execute(builder.toString());
223 }
224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 public int movedex(String srcPath, String dstPath) {
226 StringBuilder builder = new StringBuilder("movedex");
227 builder.append(' ');
228 builder.append(srcPath);
229 builder.append(' ');
230 builder.append(dstPath);
231 return execute(builder.toString());
232 }
233
234 public int rmdex(String codePath) {
235 StringBuilder builder = new StringBuilder("rmdex");
236 builder.append(' ');
237 builder.append(codePath);
238 return execute(builder.toString());
239 }
240
Amith Yamasani0b285492011-04-14 17:35:23 -0700241 public int remove(String name, int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 StringBuilder builder = new StringBuilder("remove");
243 builder.append(' ');
244 builder.append(name);
Amith Yamasani0b285492011-04-14 17:35:23 -0700245 builder.append(' ');
246 builder.append(userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 return execute(builder.toString());
248 }
249
Kenny Root35ab3ad2011-02-02 16:42:14 -0800250 public int rename(String oldname, String newname) {
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800251 StringBuilder builder = new StringBuilder("rename");
252 builder.append(' ');
253 builder.append(oldname);
254 builder.append(' ');
255 builder.append(newname);
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800256 return execute(builder.toString());
257 }
258
Dave Burke383fa182012-10-23 23:12:19 -0700259 public int fixUid(String name, int uid, int gid) {
Dianne Hackbornd0c5f512012-06-07 16:53:59 -0700260 StringBuilder builder = new StringBuilder("fixuid");
261 builder.append(' ');
262 builder.append(name);
263 builder.append(' ');
264 builder.append(uid);
265 builder.append(' ');
Dave Burke383fa182012-10-23 23:12:19 -0700266 builder.append(gid);
Dianne Hackbornd0c5f512012-06-07 16:53:59 -0700267 return execute(builder.toString());
268 }
269
Amith Yamasani54289b82012-10-01 10:39:14 -0700270 public int deleteCacheFiles(String name, int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 StringBuilder builder = new StringBuilder("rmcache");
272 builder.append(' ');
273 builder.append(name);
Amith Yamasani54289b82012-10-01 10:39:14 -0700274 builder.append(' ');
275 builder.append(userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 return execute(builder.toString());
277 }
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700278
Robert Craig8643dc62013-07-29 09:06:51 -0400279 public int createUserData(String name, int uid, int userId, String seinfo) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700280 StringBuilder builder = new StringBuilder("mkuserdata");
281 builder.append(' ');
282 builder.append(name);
283 builder.append(' ');
284 builder.append(uid);
285 builder.append(' ');
286 builder.append(userId);
Robert Craig8643dc62013-07-29 09:06:51 -0400287 builder.append(' ');
288 builder.append(seinfo != null ? seinfo : "!");
Amith Yamasani0b285492011-04-14 17:35:23 -0700289 return execute(builder.toString());
290 }
291
292 public int removeUserDataDirs(int userId) {
293 StringBuilder builder = new StringBuilder("rmuser");
294 builder.append(' ');
295 builder.append(userId);
296 return execute(builder.toString());
297 }
298
299 public int clearUserData(String name, int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800300 StringBuilder builder = new StringBuilder("rmuserdata");
301 builder.append(' ');
302 builder.append(name);
Amith Yamasani0b285492011-04-14 17:35:23 -0700303 builder.append(' ');
304 builder.append(userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 return execute(builder.toString());
306 }
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700307
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308 public boolean ping() {
309 if (execute("ping") < 0) {
310 return false;
311 } else {
312 return true;
313 }
314 }
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700315
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 public int freeCache(long freeStorageSize) {
317 StringBuilder builder = new StringBuilder("freecache");
318 builder.append(' ');
319 builder.append(String.valueOf(freeStorageSize));
320 return execute(builder.toString());
321 }
322
Dianne Hackbornecc8d6f2013-05-01 18:54:11 -0700323 public int getSizeInfo(String pkgName, int persona, String apkPath, String libDirPath,
324 String fwdLockApkPath, String asecPath, PackageStats pStats) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 StringBuilder builder = new StringBuilder("getsize");
326 builder.append(' ');
327 builder.append(pkgName);
328 builder.append(' ');
Dianne Hackborn0c380492012-08-20 17:23:30 -0700329 builder.append(persona);
330 builder.append(' ');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 builder.append(apkPath);
332 builder.append(' ');
Dianne Hackbornecc8d6f2013-05-01 18:54:11 -0700333 builder.append(libDirPath != null ? libDirPath : "!");
334 builder.append(' ');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 builder.append(fwdLockApkPath != null ? fwdLockApkPath : "!");
Dianne Hackborn292f8bc2011-06-27 16:27:41 -0700336 builder.append(' ');
337 builder.append(asecPath != null ? asecPath : "!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338
339 String s = transaction(builder.toString());
340 String res[] = s.split(" ");
341
Dianne Hackborn292f8bc2011-06-27 16:27:41 -0700342 if ((res == null) || (res.length != 5)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 return -1;
344 }
345 try {
346 pStats.codeSize = Long.parseLong(res[1]);
347 pStats.dataSize = Long.parseLong(res[2]);
348 pStats.cacheSize = Long.parseLong(res[3]);
Dianne Hackborn292f8bc2011-06-27 16:27:41 -0700349 pStats.externalCodeSize = Long.parseLong(res[4]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 return Integer.parseInt(res[0]);
351 } catch (NumberFormatException e) {
352 return -1;
353 }
Kenny Rootcf0b38c2011-03-22 14:17:59 -0700354 }
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800355
356 public int moveFiles() {
357 return execute("movefiles");
358 }
Kenny Root6a6b0072010-10-07 16:46:10 -0700359
Kenny Rootddbe50d2012-09-06 13:18:37 -0700360 /**
361 * Links the native library directory in an application's directory to its
362 * real location.
363 *
364 * @param dataPath data directory where the application is
365 * @param nativeLibPath target native library path
366 * @return -1 on error
367 */
Kenny Roota3e90792012-10-18 10:58:36 -0700368 public int linkNativeLibraryDirectory(String dataPath, String nativeLibPath, int userId) {
Kenny Root6a6b0072010-10-07 16:46:10 -0700369 if (dataPath == null) {
Kenny Root9bbd70a2012-09-10 11:13:36 -0700370 Slog.e(TAG, "linkNativeLibraryDirectory dataPath is null");
Kenny Root6a6b0072010-10-07 16:46:10 -0700371 return -1;
372 } else if (nativeLibPath == null) {
Kenny Root9bbd70a2012-09-10 11:13:36 -0700373 Slog.e(TAG, "linkNativeLibraryDirectory nativeLibPath is null");
Kenny Root6a6b0072010-10-07 16:46:10 -0700374 return -1;
375 }
376
377 StringBuilder builder = new StringBuilder("linklib ");
378 builder.append(dataPath);
379 builder.append(' ');
380 builder.append(nativeLibPath);
Kenny Roota3e90792012-10-18 10:58:36 -0700381 builder.append(' ');
382 builder.append(userId);
Kenny Root6a6b0072010-10-07 16:46:10 -0700383
384 return execute(builder.toString());
385 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386}