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 | |
| 17 | package com.android.server; |
| 18 | |
| 19 | import android.content.pm.PackageStats; |
| 20 | import android.net.LocalSocketAddress; |
| 21 | import android.net.LocalSocket; |
| 22 | import android.util.Config; |
| 23 | import android.util.Log; |
| 24 | |
| 25 | import java.io.IOException; |
| 26 | import java.io.InputStream; |
| 27 | import java.io.OutputStream; |
| 28 | import java.net.Socket; |
| 29 | |
| 30 | |
| 31 | class Installer { |
| 32 | private static final String TAG = "Installer"; |
| 33 | InputStream mIn; |
| 34 | OutputStream mOut; |
| 35 | LocalSocket mSocket; |
| 36 | |
| 37 | byte buf[] = new byte[1024]; |
| 38 | int buflen = 0; |
| 39 | |
| 40 | private boolean connect() { |
| 41 | if (mSocket != null) { |
| 42 | return true; |
| 43 | } |
| 44 | Log.i(TAG, "connecting..."); |
| 45 | try { |
| 46 | mSocket = new LocalSocket(); |
| 47 | |
| 48 | LocalSocketAddress address = new LocalSocketAddress( |
| 49 | "installd", LocalSocketAddress.Namespace.RESERVED); |
| 50 | |
| 51 | mSocket.connect(address); |
| 52 | |
| 53 | mIn = mSocket.getInputStream(); |
| 54 | mOut = mSocket.getOutputStream(); |
| 55 | } catch (IOException ex) { |
| 56 | disconnect(); |
| 57 | return false; |
| 58 | } |
| 59 | return true; |
| 60 | } |
| 61 | |
| 62 | private void disconnect() { |
| 63 | Log.i(TAG,"disconnecting..."); |
| 64 | try { |
| 65 | if (mSocket != null) mSocket.close(); |
| 66 | } catch (IOException ex) { } |
| 67 | try { |
| 68 | if (mIn != null) mIn.close(); |
| 69 | } catch (IOException ex) { } |
| 70 | try { |
| 71 | if (mOut != null) mOut.close(); |
| 72 | } catch (IOException ex) { } |
| 73 | mSocket = null; |
| 74 | mIn = null; |
| 75 | mOut = null; |
| 76 | } |
| 77 | |
| 78 | private boolean readBytes(byte buffer[], int len) { |
| 79 | int off = 0, count; |
| 80 | if (len < 0) return false; |
| 81 | while (off != len) { |
| 82 | try { |
| 83 | count = mIn.read(buffer, off, len - off); |
| 84 | if (count <= 0) { |
| 85 | Log.e(TAG, "read error " + count); |
| 86 | break; |
| 87 | } |
| 88 | off += count; |
| 89 | } catch (IOException ex) { |
| 90 | Log.e(TAG,"read exception"); |
| 91 | break; |
| 92 | } |
| 93 | } |
| 94 | // Log.i(TAG, "read "+len+" bytes"); |
| 95 | if (off == len) return true; |
| 96 | disconnect(); |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | private boolean readReply() { |
| 101 | int len; |
| 102 | buflen = 0; |
| 103 | if (!readBytes(buf, 2)) return false; |
| 104 | len = (((int) buf[0]) & 0xff) | ((((int) buf[1]) & 0xff) << 8); |
| 105 | if ((len < 1) || (len > 1024)) { |
| 106 | Log.e(TAG,"invalid reply length ("+len+")"); |
| 107 | disconnect(); |
| 108 | return false; |
| 109 | } |
| 110 | if (!readBytes(buf, len)) return false; |
| 111 | buflen = len; |
| 112 | return true; |
| 113 | } |
| 114 | |
| 115 | private boolean writeCommand(String _cmd) { |
| 116 | byte[] cmd = _cmd.getBytes(); |
| 117 | int len = cmd.length; |
| 118 | if ((len < 1) || (len > 1024)) return false; |
| 119 | buf[0] = (byte) (len & 0xff); |
| 120 | buf[1] = (byte) ((len >> 8) & 0xff); |
| 121 | try { |
| 122 | mOut.write(buf, 0, 2); |
| 123 | mOut.write(cmd, 0, len); |
| 124 | } catch (IOException ex) { |
| 125 | Log.e(TAG,"write error"); |
| 126 | disconnect(); |
| 127 | return false; |
| 128 | } |
| 129 | return true; |
| 130 | } |
| 131 | |
| 132 | private synchronized String transaction(String cmd) { |
| 133 | if (!connect()) { |
| 134 | Log.e(TAG, "connection failed"); |
| 135 | return "-1"; |
| 136 | } |
| 137 | |
| 138 | if (!writeCommand(cmd)) { |
| 139 | /* If installd died and restarted in the background |
| 140 | * (unlikely but possible) we'll fail on the next |
| 141 | * write (this one). Try to reconnect and write |
| 142 | * the command one more time before giving up. |
| 143 | */ |
| 144 | Log.e(TAG, "write command failed? reconnect!"); |
| 145 | if (!connect() || !writeCommand(cmd)) { |
| 146 | return "-1"; |
| 147 | } |
| 148 | } |
| 149 | // Log.i(TAG,"send: '"+cmd+"'"); |
| 150 | if (readReply()) { |
| 151 | String s = new String(buf, 0, buflen); |
| 152 | // Log.i(TAG,"recv: '"+s+"'"); |
| 153 | return s; |
| 154 | } else { |
| 155 | // Log.i(TAG,"fail"); |
| 156 | return "-1"; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | private int execute(String cmd) { |
| 161 | String res = transaction(cmd); |
| 162 | try { |
| 163 | return Integer.parseInt(res); |
| 164 | } catch (NumberFormatException ex) { |
| 165 | return -1; |
| 166 | } |
| 167 | } |
| 168 | |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame^] | 169 | public int install(String name, boolean useEncryptedFilesystem, int uid, int gid) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 170 | StringBuilder builder = new StringBuilder("install"); |
| 171 | builder.append(' '); |
| 172 | builder.append(name); |
| 173 | builder.append(' '); |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame^] | 174 | if (useEncryptedFilesystem) { |
| 175 | builder.append('1'); |
| 176 | } else { |
| 177 | builder.append('0'); |
| 178 | } |
| 179 | builder.append(' '); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 180 | builder.append(uid); |
| 181 | builder.append(' '); |
| 182 | builder.append(gid); |
| 183 | return execute(builder.toString()); |
| 184 | } |
| 185 | |
| 186 | public int dexopt(String apkPath, int uid, boolean isPublic) { |
| 187 | StringBuilder builder = new StringBuilder("dexopt"); |
| 188 | builder.append(' '); |
| 189 | builder.append(apkPath); |
| 190 | builder.append(' '); |
| 191 | builder.append(uid); |
| 192 | builder.append(isPublic ? " 1" : " 0"); |
| 193 | return execute(builder.toString()); |
| 194 | } |
| 195 | |
| 196 | public int movedex(String srcPath, String dstPath) { |
| 197 | StringBuilder builder = new StringBuilder("movedex"); |
| 198 | builder.append(' '); |
| 199 | builder.append(srcPath); |
| 200 | builder.append(' '); |
| 201 | builder.append(dstPath); |
| 202 | return execute(builder.toString()); |
| 203 | } |
| 204 | |
| 205 | public int rmdex(String codePath) { |
| 206 | StringBuilder builder = new StringBuilder("rmdex"); |
| 207 | builder.append(' '); |
| 208 | builder.append(codePath); |
| 209 | return execute(builder.toString()); |
| 210 | } |
| 211 | |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame^] | 212 | public int remove(String name, boolean useEncryptedFilesystem) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 213 | StringBuilder builder = new StringBuilder("remove"); |
| 214 | builder.append(' '); |
| 215 | builder.append(name); |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame^] | 216 | builder.append(' '); |
| 217 | if (useEncryptedFilesystem) { |
| 218 | builder.append('1'); |
| 219 | } else { |
| 220 | builder.append('0'); |
| 221 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 222 | return execute(builder.toString()); |
| 223 | } |
| 224 | |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame^] | 225 | public int deleteCacheFiles(String name, boolean useEncryptedFilesystem) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 226 | StringBuilder builder = new StringBuilder("rmcache"); |
| 227 | builder.append(' '); |
| 228 | builder.append(name); |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame^] | 229 | builder.append(' '); |
| 230 | if (useEncryptedFilesystem) { |
| 231 | builder.append('1'); |
| 232 | } else { |
| 233 | builder.append('0'); |
| 234 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 235 | return execute(builder.toString()); |
| 236 | } |
| 237 | |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame^] | 238 | public int clearUserData(String name, boolean useEncryptedFilesystem) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 239 | StringBuilder builder = new StringBuilder("rmuserdata"); |
| 240 | builder.append(' '); |
| 241 | builder.append(name); |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame^] | 242 | builder.append(' '); |
| 243 | if (useEncryptedFilesystem) { |
| 244 | builder.append('1'); |
| 245 | } else { |
| 246 | builder.append('0'); |
| 247 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 248 | return execute(builder.toString()); |
| 249 | } |
| 250 | |
| 251 | public boolean ping() { |
| 252 | if (execute("ping") < 0) { |
| 253 | return false; |
| 254 | } else { |
| 255 | return true; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | public int freeCache(long freeStorageSize) { |
| 260 | StringBuilder builder = new StringBuilder("freecache"); |
| 261 | builder.append(' '); |
| 262 | builder.append(String.valueOf(freeStorageSize)); |
| 263 | return execute(builder.toString()); |
| 264 | } |
| 265 | |
| 266 | public int setForwardLockPerm(String packageName, int gid) { |
| 267 | StringBuilder builder = new StringBuilder("protect"); |
| 268 | builder.append(' '); |
| 269 | builder.append(packageName); |
| 270 | builder.append(' '); |
| 271 | builder.append(gid); |
| 272 | return execute(builder.toString()); |
| 273 | } |
| 274 | |
| 275 | public int getSizeInfo(String pkgName, String apkPath, |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame^] | 276 | String fwdLockApkPath, PackageStats pStats, boolean useEncryptedFilesystem) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 277 | StringBuilder builder = new StringBuilder("getsize"); |
| 278 | builder.append(' '); |
| 279 | builder.append(pkgName); |
| 280 | builder.append(' '); |
| 281 | builder.append(apkPath); |
| 282 | builder.append(' '); |
| 283 | builder.append(fwdLockApkPath != null ? fwdLockApkPath : "!"); |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame^] | 284 | builder.append(' '); |
| 285 | if (useEncryptedFilesystem) { |
| 286 | builder.append('1'); |
| 287 | } else { |
| 288 | builder.append('0'); |
| 289 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 290 | |
| 291 | String s = transaction(builder.toString()); |
| 292 | String res[] = s.split(" "); |
| 293 | |
| 294 | if((res == null) || (res.length != 4)) { |
| 295 | return -1; |
| 296 | } |
| 297 | try { |
| 298 | pStats.codeSize = Long.parseLong(res[1]); |
| 299 | pStats.dataSize = Long.parseLong(res[2]); |
| 300 | pStats.cacheSize = Long.parseLong(res[3]); |
| 301 | return Integer.parseInt(res[0]); |
| 302 | } catch (NumberFormatException e) { |
| 303 | return -1; |
| 304 | } |
| 305 | } |
| 306 | } |