installd.c revision 7ac3ed1d7679e9cb90b44c6ab1629318b17c0690
1/* 2** Copyright 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#include "installd.h" 18 19 20#define BUFFER_MAX 1024 /* input buffer for commands */ 21#define TOKEN_MAX 8 /* max number of arguments in buffer */ 22#define REPLY_MAX 256 /* largest reply allowed */ 23 24static int do_ping(char **arg, char reply[REPLY_MAX]) 25{ 26 return 0; 27} 28 29static int do_install(char **arg, char reply[REPLY_MAX]) 30{ 31 return install(arg[0], atoi(arg[1]), atoi(arg[2])); /* pkgname, uid, gid */ 32} 33 34static int do_dexopt(char **arg, char reply[REPLY_MAX]) 35{ 36 /* apk_path, uid, is_public */ 37 return dexopt(arg[0], atoi(arg[1]), atoi(arg[2])); 38} 39 40static int do_move_dex(char **arg, char reply[REPLY_MAX]) 41{ 42 return move_dex(arg[0], arg[1]); /* src, dst */ 43} 44 45static int do_rm_dex(char **arg, char reply[REPLY_MAX]) 46{ 47 return rm_dex(arg[0]); /* pkgname */ 48} 49 50static int do_remove(char **arg, char reply[REPLY_MAX]) 51{ 52 return uninstall(arg[0], atoi(arg[1])); /* pkgname, userid */ 53} 54 55static int do_rename(char **arg, char reply[REPLY_MAX]) 56{ 57 return renamepkg(arg[0], arg[1]); /* oldpkgname, newpkgname */ 58} 59 60static int do_free_cache(char **arg, char reply[REPLY_MAX]) /* TODO int:free_size */ 61{ 62 return free_cache((int64_t)atoll(arg[0])); /* free_size */ 63} 64 65static int do_rm_cache(char **arg, char reply[REPLY_MAX]) 66{ 67 return delete_cache(arg[0]); /* pkgname */ 68} 69 70static int do_protect(char **arg, char reply[REPLY_MAX]) 71{ 72 return protect(arg[0], atoi(arg[1])); /* pkgname, gid */ 73} 74 75static int do_get_size(char **arg, char reply[REPLY_MAX]) 76{ 77 int64_t codesize = 0; 78 int64_t datasize = 0; 79 int64_t cachesize = 0; 80 int64_t asecsize = 0; 81 int res = 0; 82 83 /* pkgdir, apkpath */ 84 res = get_size(arg[0], arg[1], arg[2], arg[3], &codesize, &datasize, &cachesize, &asecsize); 85 86 /* 87 * Each int64_t can take up 22 characters printed out. Make sure it 88 * doesn't go over REPLY_MAX in the future. 89 */ 90 snprintf(reply, REPLY_MAX, "%" PRId64 " %" PRId64 " %" PRId64 " %" PRId64, 91 codesize, datasize, cachesize, asecsize); 92 return res; 93} 94 95static int do_rm_user_data(char **arg, char reply[REPLY_MAX]) 96{ 97 return delete_user_data(arg[0], atoi(arg[1])); /* pkgname, userid */ 98} 99 100static int do_mk_user_data(char **arg, char reply[REPLY_MAX]) 101{ 102 return make_user_data(arg[0], atoi(arg[1]), atoi(arg[2])); /* pkgname, uid, userid */ 103} 104 105static int do_rm_user(char **arg, char reply[REPLY_MAX]) 106{ 107 return delete_persona(atoi(arg[0])); /* userid */ 108} 109 110static int do_clone_user_data(char **arg, char reply[REPLY_MAX]) 111{ 112 return clone_persona_data(atoi(arg[0]), atoi(arg[1]), atoi(arg[2])); 113} 114 115static int do_movefiles(char **arg, char reply[REPLY_MAX]) 116{ 117 return movefiles(); 118} 119 120static int do_linklib(char **arg, char reply[REPLY_MAX]) 121{ 122 return linklib(arg[0], arg[1]); 123} 124 125static int do_unlinklib(char **arg, char reply[REPLY_MAX]) 126{ 127 return unlinklib(arg[0]); 128} 129 130struct cmdinfo { 131 const char *name; 132 unsigned numargs; 133 int (*func)(char **arg, char reply[REPLY_MAX]); 134}; 135 136struct cmdinfo cmds[] = { 137 { "ping", 0, do_ping }, 138 { "install", 3, do_install }, 139 { "dexopt", 3, do_dexopt }, 140 { "movedex", 2, do_move_dex }, 141 { "rmdex", 1, do_rm_dex }, 142 { "remove", 2, do_remove }, 143 { "rename", 2, do_rename }, 144 { "freecache", 1, do_free_cache }, 145 { "rmcache", 1, do_rm_cache }, 146 { "protect", 2, do_protect }, 147 { "getsize", 4, do_get_size }, 148 { "rmuserdata", 2, do_rm_user_data }, 149 { "movefiles", 0, do_movefiles }, 150 { "linklib", 2, do_linklib }, 151 { "unlinklib", 1, do_unlinklib }, 152 { "mkuserdata", 3, do_mk_user_data }, 153 { "rmuser", 1, do_rm_user }, 154 { "cloneuserdata", 3, do_clone_user_data }, 155}; 156 157static int readx(int s, void *_buf, int count) 158{ 159 char *buf = _buf; 160 int n = 0, r; 161 if (count < 0) return -1; 162 while (n < count) { 163 r = read(s, buf + n, count - n); 164 if (r < 0) { 165 if (errno == EINTR) continue; 166 ALOGE("read error: %s\n", strerror(errno)); 167 return -1; 168 } 169 if (r == 0) { 170 ALOGE("eof\n"); 171 return -1; /* EOF */ 172 } 173 n += r; 174 } 175 return 0; 176} 177 178static int writex(int s, const void *_buf, int count) 179{ 180 const char *buf = _buf; 181 int n = 0, r; 182 if (count < 0) return -1; 183 while (n < count) { 184 r = write(s, buf + n, count - n); 185 if (r < 0) { 186 if (errno == EINTR) continue; 187 ALOGE("write error: %s\n", strerror(errno)); 188 return -1; 189 } 190 n += r; 191 } 192 return 0; 193} 194 195 196/* Tokenize the command buffer, locate a matching command, 197 * ensure that the required number of arguments are provided, 198 * call the function(), return the result. 199 */ 200static int execute(int s, char cmd[BUFFER_MAX]) 201{ 202 char reply[REPLY_MAX]; 203 char *arg[TOKEN_MAX+1]; 204 unsigned i; 205 unsigned n = 0; 206 unsigned short count; 207 int ret = -1; 208 209// ALOGI("execute('%s')\n", cmd); 210 211 /* default reply is "" */ 212 reply[0] = 0; 213 214 /* n is number of args (not counting arg[0]) */ 215 arg[0] = cmd; 216 while (*cmd) { 217 if (isspace(*cmd)) { 218 *cmd++ = 0; 219 n++; 220 arg[n] = cmd; 221 if (n == TOKEN_MAX) { 222 ALOGE("too many arguments\n"); 223 goto done; 224 } 225 } 226 cmd++; 227 } 228 229 for (i = 0; i < sizeof(cmds) / sizeof(cmds[0]); i++) { 230 if (!strcmp(cmds[i].name,arg[0])) { 231 if (n != cmds[i].numargs) { 232 ALOGE("%s requires %d arguments (%d given)\n", 233 cmds[i].name, cmds[i].numargs, n); 234 } else { 235 ret = cmds[i].func(arg + 1, reply); 236 } 237 goto done; 238 } 239 } 240 ALOGE("unsupported command '%s'\n", arg[0]); 241 242done: 243 if (reply[0]) { 244 n = snprintf(cmd, BUFFER_MAX, "%d %s", ret, reply); 245 } else { 246 n = snprintf(cmd, BUFFER_MAX, "%d", ret); 247 } 248 if (n > BUFFER_MAX) n = BUFFER_MAX; 249 count = n; 250 251// ALOGI("reply: '%s'\n", cmd); 252 if (writex(s, &count, sizeof(count))) return -1; 253 if (writex(s, cmd, count)) return -1; 254 return 0; 255} 256 257/** 258 * Initialize all the global variables that are used elsewhere. Returns 0 upon 259 * success and -1 on error. 260 */ 261void free_globals() { 262 size_t i; 263 264 for (i = 0; i < android_system_dirs.count; i++) { 265 if (android_system_dirs.dirs[i].path != NULL) { 266 free(android_system_dirs.dirs[i].path); 267 } 268 } 269 270 free(android_system_dirs.dirs); 271} 272 273int initialize_globals() { 274 // Get the android data directory. 275 if (get_path_from_env(&android_data_dir, "ANDROID_DATA") < 0) { 276 return -1; 277 } 278 279 // Get the android app directory. 280 if (copy_and_append(&android_app_dir, &android_data_dir, APP_SUBDIR) < 0) { 281 return -1; 282 } 283 284 // Get the android protected app directory. 285 if (copy_and_append(&android_app_private_dir, &android_data_dir, PRIVATE_APP_SUBDIR) < 0) { 286 return -1; 287 } 288 289 // Get the sd-card ASEC mount point. 290 if (get_path_from_env(&android_asec_dir, "ASEC_MOUNTPOINT") < 0) { 291 return -1; 292 } 293 294 // Take note of the system and vendor directories. 295 android_system_dirs.count = 2; 296 297 android_system_dirs.dirs = calloc(android_system_dirs.count, sizeof(dir_rec_t)); 298 if (android_system_dirs.dirs == NULL) { 299 ALOGE("Couldn't allocate array for dirs; aborting\n"); 300 return -1; 301 } 302 303 // system 304 if (get_path_from_env(&android_system_dirs.dirs[0], "ANDROID_ROOT") < 0) { 305 free_globals(); 306 return -1; 307 } 308 309 // append "app/" to dirs[0] 310 char *system_app_path = build_string2(android_system_dirs.dirs[0].path, APP_SUBDIR); 311 android_system_dirs.dirs[0].path = system_app_path; 312 android_system_dirs.dirs[0].len = strlen(system_app_path); 313 314 // vendor 315 // TODO replace this with an environment variable (doesn't exist yet) 316 android_system_dirs.dirs[1].path = "/vendor/app/"; 317 android_system_dirs.dirs[1].len = strlen(android_system_dirs.dirs[1].path); 318 319 return 0; 320} 321 322int initialize_directories() { 323 // /data/user 324 char *user_data_dir = build_string2(android_data_dir.path, SECONDARY_USER_PREFIX); 325 // /data/data 326 char *legacy_data_dir = build_string2(android_data_dir.path, PRIMARY_USER_PREFIX); 327 // /data/user/0 328 char *primary_data_dir = build_string3(android_data_dir.path, SECONDARY_USER_PREFIX, 329 "0"); 330 int ret = -1; 331 if (user_data_dir != NULL && primary_data_dir != NULL && legacy_data_dir != NULL) { 332 ret = 0; 333 // Make the /data/user directory if necessary 334 if (access(user_data_dir, R_OK) < 0) { 335 if (mkdir(user_data_dir, 0711) < 0) { 336 return -1; 337 } 338 if (chown(user_data_dir, AID_SYSTEM, AID_SYSTEM) < 0) { 339 return -1; 340 } 341 if (chmod(user_data_dir, 0711) < 0) { 342 return -1; 343 } 344 } 345 // Make the /data/user/0 symlink to /data/data if necessary 346 if (access(primary_data_dir, R_OK) < 0) { 347 ret = symlink(legacy_data_dir, primary_data_dir); 348 } 349 free(user_data_dir); 350 free(legacy_data_dir); 351 free(primary_data_dir); 352 } 353 return ret; 354} 355 356int main(const int argc, const char *argv[]) { 357 char buf[BUFFER_MAX]; 358 struct sockaddr addr; 359 socklen_t alen; 360 int lsocket, s, count; 361 362 if (initialize_globals() < 0) { 363 ALOGE("Could not initialize globals; exiting.\n"); 364 exit(1); 365 } 366 367 if (initialize_directories() < 0) { 368 ALOGE("Could not create directories; exiting.\n"); 369 exit(1); 370 } 371 372 lsocket = android_get_control_socket(SOCKET_PATH); 373 if (lsocket < 0) { 374 ALOGE("Failed to get socket from environment: %s\n", strerror(errno)); 375 exit(1); 376 } 377 if (listen(lsocket, 5)) { 378 ALOGE("Listen on socket failed: %s\n", strerror(errno)); 379 exit(1); 380 } 381 fcntl(lsocket, F_SETFD, FD_CLOEXEC); 382 383 for (;;) { 384 alen = sizeof(addr); 385 s = accept(lsocket, &addr, &alen); 386 if (s < 0) { 387 ALOGE("Accept failed: %s\n", strerror(errno)); 388 continue; 389 } 390 fcntl(s, F_SETFD, FD_CLOEXEC); 391 392 ALOGI("new connection\n"); 393 for (;;) { 394 unsigned short count; 395 if (readx(s, &count, sizeof(count))) { 396 ALOGE("failed to read size\n"); 397 break; 398 } 399 if ((count < 1) || (count >= BUFFER_MAX)) { 400 ALOGE("invalid size %d\n", count); 401 break; 402 } 403 if (readx(s, buf, count)) { 404 ALOGE("failed to read command\n"); 405 break; 406 } 407 buf[count] = 0; 408 if (execute(s, buf)) break; 409 } 410 ALOGI("closing connection\n"); 411 close(s); 412 } 413 414 return 0; 415} 416