The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
Kenny Root | cf0b38c | 2011-03-22 14:17:59 -0700 | [diff] [blame] | 17 | package com.android.server.pm; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 18 | |
| 19 | import android.content.pm.PackageStats; |
Jason parks | a3cdaa5 | 2011-01-13 14:15:43 -0600 | [diff] [blame] | 20 | import android.net.LocalSocket; |
Kenny Root | cf0b38c | 2011-03-22 14:17:59 -0700 | [diff] [blame] | 21 | import android.net.LocalSocketAddress; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 22 | import android.util.Slog; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | |
| 24 | import java.io.IOException; |
| 25 | import java.io.InputStream; |
| 26 | import java.io.OutputStream; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | |
| 28 | class Installer { |
| 29 | private static final String TAG = "Installer"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | |
Joe Onorato | 63ebffc | 2011-04-06 11:47:23 -0700 | [diff] [blame] | 31 | private static final boolean LOCAL_DEBUG = false; |
Kenny Root | cf0b38c | 2011-03-22 14:17:59 -0700 | [diff] [blame] | 32 | |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | |
| 43 | private boolean connect() { |
| 44 | if (mSocket != null) { |
| 45 | return true; |
| 46 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 47 | Slog.i(TAG, "connecting..."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | try { |
| 49 | mSocket = new LocalSocket(); |
| 50 | |
Kenny Root | cf0b38c | 2011-03-22 14:17:59 -0700 | [diff] [blame] | 51 | LocalSocketAddress address = new LocalSocketAddress("installd", |
| 52 | LocalSocketAddress.Namespace.RESERVED); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | |
| 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 Root | cf0b38c | 2011-03-22 14:17:59 -0700 | [diff] [blame] | 65 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | |
Kenny Root | cf0b38c | 2011-03-22 14:17:59 -0700 | [diff] [blame] | 87 | 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 Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 95 | Slog.e(TAG, "read error " + count); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | break; |
| 97 | } |
Kenny Root | cf0b38c | 2011-03-22 14:17:59 -0700 | [diff] [blame] | 98 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | |
Kenny Root | cf0b38c | 2011-03-22 14:17:59 -0700 | [diff] [blame] | 113 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 129 | |
Kenny Root | cf0b38c | 2011-03-22 14:17:59 -0700 | [diff] [blame] | 130 | 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 Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 150 | Slog.e(TAG, "connection failed"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 151 | return "-1"; |
| 152 | } |
| 153 | |
| 154 | if (!writeCommand(cmd)) { |
Kenny Root | cf0b38c | 2011-03-22 14:17:59 -0700 | [diff] [blame] | 155 | /* |
| 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 Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 160 | Slog.e(TAG, "write command failed? reconnect!"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 161 | if (!connect() || !writeCommand(cmd)) { |
| 162 | return "-1"; |
| 163 | } |
| 164 | } |
Kenny Root | cf0b38c | 2011-03-22 14:17:59 -0700 | [diff] [blame] | 165 | if (LOCAL_DEBUG) { |
| 166 | Slog.i(TAG, "send: '" + cmd + "'"); |
| 167 | } |
| 168 | if (readReply()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 169 | String s = new String(buf, 0, buflen); |
Kenny Root | cf0b38c | 2011-03-22 14:17:59 -0700 | [diff] [blame] | 170 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 181 | |
Kenny Root | cf0b38c | 2011-03-22 14:17:59 -0700 | [diff] [blame] | 182 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 190 | |
Kenny Root | 35ab3ad | 2011-02-02 16:42:14 -0800 | [diff] [blame] | 191 | public int install(String name, int uid, int gid) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 192 | 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); |
| 199 | return execute(builder.toString()); |
| 200 | } |
| 201 | |
| 202 | public int dexopt(String apkPath, int uid, boolean isPublic) { |
| 203 | StringBuilder builder = new StringBuilder("dexopt"); |
| 204 | builder.append(' '); |
| 205 | builder.append(apkPath); |
| 206 | builder.append(' '); |
| 207 | builder.append(uid); |
| 208 | builder.append(isPublic ? " 1" : " 0"); |
| 209 | return execute(builder.toString()); |
| 210 | } |
| 211 | |
| 212 | public int movedex(String srcPath, String dstPath) { |
| 213 | StringBuilder builder = new StringBuilder("movedex"); |
| 214 | builder.append(' '); |
| 215 | builder.append(srcPath); |
| 216 | builder.append(' '); |
| 217 | builder.append(dstPath); |
| 218 | return execute(builder.toString()); |
| 219 | } |
| 220 | |
| 221 | public int rmdex(String codePath) { |
| 222 | StringBuilder builder = new StringBuilder("rmdex"); |
| 223 | builder.append(' '); |
| 224 | builder.append(codePath); |
| 225 | return execute(builder.toString()); |
| 226 | } |
| 227 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 228 | public int remove(String name, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 229 | StringBuilder builder = new StringBuilder("remove"); |
| 230 | builder.append(' '); |
| 231 | builder.append(name); |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 232 | builder.append(' '); |
| 233 | builder.append(userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 234 | return execute(builder.toString()); |
| 235 | } |
| 236 | |
Kenny Root | 35ab3ad | 2011-02-02 16:42:14 -0800 | [diff] [blame] | 237 | public int rename(String oldname, String newname) { |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 238 | StringBuilder builder = new StringBuilder("rename"); |
| 239 | builder.append(' '); |
| 240 | builder.append(oldname); |
| 241 | builder.append(' '); |
| 242 | builder.append(newname); |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 243 | return execute(builder.toString()); |
| 244 | } |
| 245 | |
Dianne Hackborn | d0c5f51 | 2012-06-07 16:53:59 -0700 | [diff] [blame] | 246 | public int fixUid(String name, int uid, int gid) { |
| 247 | StringBuilder builder = new StringBuilder("fixuid"); |
| 248 | builder.append(' '); |
| 249 | builder.append(name); |
| 250 | builder.append(' '); |
| 251 | builder.append(uid); |
| 252 | builder.append(' '); |
| 253 | builder.append(gid); |
| 254 | return execute(builder.toString()); |
| 255 | } |
| 256 | |
Kenny Root | 35ab3ad | 2011-02-02 16:42:14 -0800 | [diff] [blame] | 257 | public int deleteCacheFiles(String name) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 258 | StringBuilder builder = new StringBuilder("rmcache"); |
| 259 | builder.append(' '); |
| 260 | builder.append(name); |
| 261 | return execute(builder.toString()); |
| 262 | } |
Kenny Root | cf0b38c | 2011-03-22 14:17:59 -0700 | [diff] [blame] | 263 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 264 | public int createUserData(String name, int uid, int userId) { |
| 265 | StringBuilder builder = new StringBuilder("mkuserdata"); |
| 266 | builder.append(' '); |
| 267 | builder.append(name); |
| 268 | builder.append(' '); |
| 269 | builder.append(uid); |
| 270 | builder.append(' '); |
| 271 | builder.append(userId); |
| 272 | return execute(builder.toString()); |
| 273 | } |
| 274 | |
| 275 | public int removeUserDataDirs(int userId) { |
| 276 | StringBuilder builder = new StringBuilder("rmuser"); |
| 277 | builder.append(' '); |
| 278 | builder.append(userId); |
| 279 | return execute(builder.toString()); |
| 280 | } |
| 281 | |
| 282 | public int clearUserData(String name, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 283 | StringBuilder builder = new StringBuilder("rmuserdata"); |
| 284 | builder.append(' '); |
| 285 | builder.append(name); |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 286 | builder.append(' '); |
| 287 | builder.append(userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 288 | return execute(builder.toString()); |
| 289 | } |
Kenny Root | cf0b38c | 2011-03-22 14:17:59 -0700 | [diff] [blame] | 290 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 291 | /** |
| 292 | * Clone all the package data directories from srcUserId to targetUserId. If copyData is true, |
| 293 | * some of the data is also copied, otherwise just empty directories are created with the |
| 294 | * correct access rights. |
| 295 | * @param srcUserId user to copy the data directories from |
| 296 | * @param targetUserId user to copy the data directories to |
| 297 | * @param copyData whether the data itself is to be copied. If false, empty directories are |
| 298 | * created. |
| 299 | * @return success/error code |
| 300 | */ |
| 301 | public int cloneUserData(int srcUserId, int targetUserId, boolean copyData) { |
| 302 | StringBuilder builder = new StringBuilder("cloneuserdata"); |
| 303 | builder.append(' '); |
| 304 | builder.append(srcUserId); |
| 305 | builder.append(' '); |
| 306 | builder.append(targetUserId); |
| 307 | builder.append(' '); |
| 308 | builder.append(copyData ? '1' : '0'); |
| 309 | return execute(builder.toString()); |
| 310 | } |
| 311 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 312 | public boolean ping() { |
| 313 | if (execute("ping") < 0) { |
| 314 | return false; |
| 315 | } else { |
| 316 | return true; |
| 317 | } |
| 318 | } |
Kenny Root | cf0b38c | 2011-03-22 14:17:59 -0700 | [diff] [blame] | 319 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 320 | public int freeCache(long freeStorageSize) { |
| 321 | StringBuilder builder = new StringBuilder("freecache"); |
| 322 | builder.append(' '); |
| 323 | builder.append(String.valueOf(freeStorageSize)); |
| 324 | return execute(builder.toString()); |
| 325 | } |
| 326 | |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 327 | /* |
| 328 | * @param packagePathSuffix The name of the path relative to install |
Kenny Root | cf0b38c | 2011-03-22 14:17:59 -0700 | [diff] [blame] | 329 | * directory. Say if the path name is /data/app/com.test-1.apk, the package |
| 330 | * suffix path will be com.test-1 |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 331 | */ |
| 332 | public int setForwardLockPerm(String packagePathSuffix, int gid) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 333 | StringBuilder builder = new StringBuilder("protect"); |
| 334 | builder.append(' '); |
Suchi Amalapurapu | c028be4 | 2010-01-25 12:19:12 -0800 | [diff] [blame] | 335 | builder.append(packagePathSuffix); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 336 | builder.append(' '); |
| 337 | builder.append(gid); |
| 338 | return execute(builder.toString()); |
| 339 | } |
Kenny Root | cf0b38c | 2011-03-22 14:17:59 -0700 | [diff] [blame] | 340 | |
Dianne Hackborn | 0c38049 | 2012-08-20 17:23:30 -0700 | [diff] [blame^] | 341 | public int getSizeInfo(String pkgName, int persona, String apkPath, String fwdLockApkPath, |
Dianne Hackborn | 292f8bc | 2011-06-27 16:27:41 -0700 | [diff] [blame] | 342 | String asecPath, PackageStats pStats) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 343 | StringBuilder builder = new StringBuilder("getsize"); |
| 344 | builder.append(' '); |
| 345 | builder.append(pkgName); |
| 346 | builder.append(' '); |
Dianne Hackborn | 0c38049 | 2012-08-20 17:23:30 -0700 | [diff] [blame^] | 347 | builder.append(persona); |
| 348 | builder.append(' '); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 349 | builder.append(apkPath); |
| 350 | builder.append(' '); |
| 351 | builder.append(fwdLockApkPath != null ? fwdLockApkPath : "!"); |
Dianne Hackborn | 292f8bc | 2011-06-27 16:27:41 -0700 | [diff] [blame] | 352 | builder.append(' '); |
| 353 | builder.append(asecPath != null ? asecPath : "!"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 354 | |
| 355 | String s = transaction(builder.toString()); |
| 356 | String res[] = s.split(" "); |
| 357 | |
Dianne Hackborn | 292f8bc | 2011-06-27 16:27:41 -0700 | [diff] [blame] | 358 | if ((res == null) || (res.length != 5)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 359 | return -1; |
| 360 | } |
| 361 | try { |
| 362 | pStats.codeSize = Long.parseLong(res[1]); |
| 363 | pStats.dataSize = Long.parseLong(res[2]); |
| 364 | pStats.cacheSize = Long.parseLong(res[3]); |
Dianne Hackborn | 292f8bc | 2011-06-27 16:27:41 -0700 | [diff] [blame] | 365 | pStats.externalCodeSize = Long.parseLong(res[4]); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 366 | return Integer.parseInt(res[0]); |
| 367 | } catch (NumberFormatException e) { |
| 368 | return -1; |
| 369 | } |
Kenny Root | cf0b38c | 2011-03-22 14:17:59 -0700 | [diff] [blame] | 370 | } |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 371 | |
| 372 | public int moveFiles() { |
| 373 | return execute("movefiles"); |
| 374 | } |
Kenny Root | 6a6b007 | 2010-10-07 16:46:10 -0700 | [diff] [blame] | 375 | |
| 376 | public int linkNativeLibraryDirectory(String dataPath, String nativeLibPath) { |
| 377 | if (dataPath == null) { |
| 378 | Slog.e(TAG, "unlinkNativeLibraryDirectory dataPath is null"); |
| 379 | return -1; |
| 380 | } else if (nativeLibPath == null) { |
| 381 | Slog.e(TAG, "unlinkNativeLibraryDirectory nativeLibPath is null"); |
| 382 | return -1; |
| 383 | } |
| 384 | |
| 385 | StringBuilder builder = new StringBuilder("linklib "); |
| 386 | builder.append(dataPath); |
| 387 | builder.append(' '); |
| 388 | builder.append(nativeLibPath); |
| 389 | |
| 390 | return execute(builder.toString()); |
| 391 | } |
| 392 | |
| 393 | public int unlinkNativeLibraryDirectory(String dataPath) { |
| 394 | if (dataPath == null) { |
| 395 | Slog.e(TAG, "unlinkNativeLibraryDirectory dataPath is null"); |
| 396 | return -1; |
| 397 | } |
| 398 | |
| 399 | StringBuilder builder = new StringBuilder("unlinklib "); |
| 400 | builder.append(dataPath); |
| 401 | |
| 402 | return execute(builder.toString()); |
| 403 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 404 | } |