installd.c revision dc9b0128c52cc797847240e81ebc6de75229a3ab
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_fixuid(char **arg, char reply[REPLY_MAX]) 61{ 62 return fix_uid(arg[0], atoi(arg[1]), atoi(arg[2])); /* pkgname, uid, gid */ 63} 64 65static int do_free_cache(char **arg, char reply[REPLY_MAX]) /* TODO int:free_size */ 66{ 67 return free_cache((int64_t)atoll(arg[0])); /* free_size */ 68} 69 70static int do_rm_cache(char **arg, char reply[REPLY_MAX]) 71{ 72 return delete_cache(arg[0]); /* pkgname */ 73} 74 75static int do_protect(char **arg, char reply[REPLY_MAX]) 76{ 77 return protect(arg[0], atoi(arg[1])); /* pkgname, gid */ 78} 79 80static int do_get_size(char **arg, char reply[REPLY_MAX]) 81{ 82 int64_t codesize = 0; 83 int64_t datasize = 0; 84 int64_t cachesize = 0; 85 int64_t asecsize = 0; 86 int res = 0; 87 88 /* pkgdir, persona, apkpath */ 89 res = get_size(arg[0], atoi(arg[1]), arg[2], arg[3], arg[4], 90 &codesize, &datasize, &cachesize, &asecsize); 91 92 /* 93 * Each int64_t can take up 22 characters printed out. Make sure it 94 * doesn't go over REPLY_MAX in the future. 95 */ 96 snprintf(reply, REPLY_MAX, "%" PRId64 " %" PRId64 " %" PRId64 " %" PRId64, 97 codesize, datasize, cachesize, asecsize); 98 return res; 99} 100 101static int do_rm_user_data(char **arg, char reply[REPLY_MAX]) 102{ 103 return delete_user_data(arg[0], atoi(arg[1])); /* pkgname, userid */ 104} 105 106static int do_mk_user_data(char **arg, char reply[REPLY_MAX]) 107{ 108 return make_user_data(arg[0], atoi(arg[1]), atoi(arg[2])); /* pkgname, uid, userid */ 109} 110 111static int do_rm_user(char **arg, char reply[REPLY_MAX]) 112{ 113 return delete_persona(atoi(arg[0])); /* userid */ 114} 115 116static int do_clone_user_data(char **arg, char reply[REPLY_MAX]) 117{ 118 return clone_persona_data(atoi(arg[0]), atoi(arg[1]), atoi(arg[2])); 119} 120 121static int do_movefiles(char **arg, char reply[REPLY_MAX]) 122{ 123 return movefiles(); 124} 125 126static int do_linklib(char **arg, char reply[REPLY_MAX]) 127{ 128 return linklib(arg[0], arg[1]); 129} 130 131static int do_unlinklib(char **arg, char reply[REPLY_MAX]) 132{ 133 return unlinklib(arg[0]); 134} 135 136struct cmdinfo { 137 const char *name; 138 unsigned numargs; 139 int (*func)(char **arg, char reply[REPLY_MAX]); 140}; 141 142struct cmdinfo cmds[] = { 143 { "ping", 0, do_ping }, 144 { "install", 3, do_install }, 145 { "dexopt", 3, do_dexopt }, 146 { "movedex", 2, do_move_dex }, 147 { "rmdex", 1, do_rm_dex }, 148 { "remove", 2, do_remove }, 149 { "rename", 2, do_rename }, 150 { "fixuid", 3, do_fixuid }, 151 { "freecache", 1, do_free_cache }, 152 { "rmcache", 1, do_rm_cache }, 153 { "protect", 2, do_protect }, 154 { "getsize", 5, do_get_size }, 155 { "rmuserdata", 2, do_rm_user_data }, 156 { "movefiles", 0, do_movefiles }, 157 { "linklib", 2, do_linklib }, 158 { "unlinklib", 1, do_unlinklib }, 159 { "mkuserdata", 3, do_mk_user_data }, 160 { "rmuser", 1, do_rm_user }, 161 { "cloneuserdata", 3, do_clone_user_data }, 162}; 163 164static int readx(int s, void *_buf, int count) 165{ 166 char *buf = _buf; 167 int n = 0, r; 168 if (count < 0) return -1; 169 while (n < count) { 170 r = read(s, buf + n, count - n); 171 if (r < 0) { 172 if (errno == EINTR) continue; 173 ALOGE("read error: %s\n", strerror(errno)); 174 return -1; 175 } 176 if (r == 0) { 177 ALOGE("eof\n"); 178 return -1; /* EOF */ 179 } 180 n += r; 181 } 182 return 0; 183} 184 185static int writex(int s, const void *_buf, int count) 186{ 187 const char *buf = _buf; 188 int n = 0, r; 189 if (count < 0) return -1; 190 while (n < count) { 191 r = write(s, buf + n, count - n); 192 if (r < 0) { 193 if (errno == EINTR) continue; 194 ALOGE("write error: %s\n", strerror(errno)); 195 return -1; 196 } 197 n += r; 198 } 199 return 0; 200} 201 202 203/* Tokenize the command buffer, locate a matching command, 204 * ensure that the required number of arguments are provided, 205 * call the function(), return the result. 206 */ 207static int execute(int s, char cmd[BUFFER_MAX]) 208{ 209 char reply[REPLY_MAX]; 210 char *arg[TOKEN_MAX+1]; 211 unsigned i; 212 unsigned n = 0; 213 unsigned short count; 214 int ret = -1; 215 216// ALOGI("execute('%s')\n", cmd); 217 218 /* default reply is "" */ 219 reply[0] = 0; 220 221 /* n is number of args (not counting arg[0]) */ 222 arg[0] = cmd; 223 while (*cmd) { 224 if (isspace(*cmd)) { 225 *cmd++ = 0; 226 n++; 227 arg[n] = cmd; 228 if (n == TOKEN_MAX) { 229 ALOGE("too many arguments\n"); 230 goto done; 231 } 232 } 233 cmd++; 234 } 235 236 for (i = 0; i < sizeof(cmds) / sizeof(cmds[0]); i++) { 237 if (!strcmp(cmds[i].name,arg[0])) { 238 if (n != cmds[i].numargs) { 239 ALOGE("%s requires %d arguments (%d given)\n", 240 cmds[i].name, cmds[i].numargs, n); 241 } else { 242 ret = cmds[i].func(arg + 1, reply); 243 } 244 goto done; 245 } 246 } 247 ALOGE("unsupported command '%s'\n", arg[0]); 248 249done: 250 if (reply[0]) { 251 n = snprintf(cmd, BUFFER_MAX, "%d %s", ret, reply); 252 } else { 253 n = snprintf(cmd, BUFFER_MAX, "%d", ret); 254 } 255 if (n > BUFFER_MAX) n = BUFFER_MAX; 256 count = n; 257 258// ALOGI("reply: '%s'\n", cmd); 259 if (writex(s, &count, sizeof(count))) return -1; 260 if (writex(s, cmd, count)) return -1; 261 return 0; 262} 263 264/** 265 * Initialize all the global variables that are used elsewhere. Returns 0 upon 266 * success and -1 on error. 267 */ 268void free_globals() { 269 size_t i; 270 271 for (i = 0; i < android_system_dirs.count; i++) { 272 if (android_system_dirs.dirs[i].path != NULL) { 273 free(android_system_dirs.dirs[i].path); 274 } 275 } 276 277 free(android_system_dirs.dirs); 278} 279 280int initialize_globals() { 281 // Get the android data directory. 282 if (get_path_from_env(&android_data_dir, "ANDROID_DATA") < 0) { 283 return -1; 284 } 285 286 // Get the android app directory. 287 if (copy_and_append(&android_app_dir, &android_data_dir, APP_SUBDIR) < 0) { 288 return -1; 289 } 290 291 // Get the android protected app directory. 292 if (copy_and_append(&android_app_private_dir, &android_data_dir, PRIVATE_APP_SUBDIR) < 0) { 293 return -1; 294 } 295 296 // Get the sd-card ASEC mount point. 297 if (get_path_from_env(&android_asec_dir, "ASEC_MOUNTPOINT") < 0) { 298 return -1; 299 } 300 301 // Get the android media directory. 302 if (copy_and_append(&android_media_dir, &android_data_dir, MEDIA_SUBDIR) < 0) { 303 return -1; 304 } 305 306 // Take note of the system and vendor directories. 307 android_system_dirs.count = 2; 308 309 android_system_dirs.dirs = calloc(android_system_dirs.count, sizeof(dir_rec_t)); 310 if (android_system_dirs.dirs == NULL) { 311 ALOGE("Couldn't allocate array for dirs; aborting\n"); 312 return -1; 313 } 314 315 // system 316 if (get_path_from_env(&android_system_dirs.dirs[0], "ANDROID_ROOT") < 0) { 317 free_globals(); 318 return -1; 319 } 320 321 // append "app/" to dirs[0] 322 char *system_app_path = build_string2(android_system_dirs.dirs[0].path, APP_SUBDIR); 323 android_system_dirs.dirs[0].path = system_app_path; 324 android_system_dirs.dirs[0].len = strlen(system_app_path); 325 326 // vendor 327 // TODO replace this with an environment variable (doesn't exist yet) 328 android_system_dirs.dirs[1].path = "/vendor/app/"; 329 android_system_dirs.dirs[1].len = strlen(android_system_dirs.dirs[1].path); 330 331 return 0; 332} 333 334int initialize_directories() { 335 int res = -1; 336 int version = 0; 337 FILE* file; 338 339 // Read current filesystem layout version to handle upgrade paths 340 char version_path[PATH_MAX]; 341 if (snprintf(version_path, PATH_MAX, "%s.layout_version", android_data_dir.path) > PATH_MAX) { 342 return -1; 343 } 344 file = fopen(version_path, "r"); 345 if (file != NULL) { 346 fscanf(file, "%d", &version); 347 fclose(file); 348 } 349 350 // /data/user 351 char *user_data_dir = build_string2(android_data_dir.path, SECONDARY_USER_PREFIX); 352 // /data/data 353 char *legacy_data_dir = build_string2(android_data_dir.path, PRIMARY_USER_PREFIX); 354 // /data/user/0 355 char *primary_data_dir = build_string3(android_data_dir.path, SECONDARY_USER_PREFIX, "0"); 356 if (!user_data_dir || !legacy_data_dir || !primary_data_dir) { 357 goto fail; 358 } 359 360 // Make the /data/user directory if necessary 361 if (access(user_data_dir, R_OK) < 0) { 362 if (mkdir(user_data_dir, 0711) < 0) { 363 goto fail; 364 } 365 if (chown(user_data_dir, AID_SYSTEM, AID_SYSTEM) < 0) { 366 goto fail; 367 } 368 if (chmod(user_data_dir, 0711) < 0) { 369 goto fail; 370 } 371 } 372 // Make the /data/user/0 symlink to /data/data if necessary 373 if (access(primary_data_dir, R_OK) < 0) { 374 if (symlink(legacy_data_dir, primary_data_dir)) { 375 goto fail; 376 } 377 } 378 379 // /data/media/0 380 char owner_media_dir[PATH_MAX]; 381 create_persona_media_path(owner_media_dir, 0); 382 383 if (version == 0) { 384 // Introducing multi-user, so migrate /data/media contents into /data/media/0 385 ALOGD("Migrating /data/media for multi-user"); 386 387 // Ensure /data/media 388 if (ensure_dir(android_media_dir.path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) { 389 goto fail; 390 } 391 392 // /data/media.tmp 393 char media_tmp_dir[PATH_MAX]; 394 snprintf(media_tmp_dir, PATH_MAX, "%smedia.tmp", android_data_dir.path); 395 396 // Only copy when upgrade not already in progress 397 if (access(media_tmp_dir, F_OK) == -1) { 398 if (rename(android_media_dir.path, media_tmp_dir) == -1) { 399 ALOGE("Failed to move legacy media path: %s", strerror(errno)); 400 goto fail; 401 } 402 } 403 404 // Create /data/media again 405 if (ensure_dir(android_media_dir.path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) { 406 goto fail; 407 } 408 409 // Move any owner data into place 410 if (access(media_tmp_dir, F_OK) == 0) { 411 if (rename(media_tmp_dir, owner_media_dir) == -1) { 412 ALOGE("Failed to move owner media path: %s", strerror(errno)); 413 goto fail; 414 } 415 } 416 417 // Ensure media directories for any existing users 418 DIR *dir; 419 struct dirent *dirent; 420 char user_media_dir[PATH_MAX]; 421 422 dir = opendir(user_data_dir); 423 if (dir != NULL) { 424 while ((dirent = readdir(dir))) { 425 if (dirent->d_type == DT_DIR) { 426 const char *name = dirent->d_name; 427 428 // skip "." and ".." 429 if (name[0] == '.') { 430 if (name[1] == 0) continue; 431 if ((name[1] == '.') && (name[2] == 0)) continue; 432 } 433 434 // /data/media/<user_id> 435 snprintf(user_media_dir, PATH_MAX, "%s%s", android_media_dir.path, name); 436 if (ensure_dir(user_media_dir, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) { 437 ALOGE("Failed to ensure %s: %s", user_media_dir, strerror(errno)); 438 goto fail; 439 } 440 } 441 } 442 closedir(dir); 443 } 444 445 version = 1; 446 } 447 448 // Ensure /data/media/0 is always ready 449 if (ensure_dir(owner_media_dir, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) { 450 goto fail; 451 } 452 453 // Persist our current version 454 file = fopen(version_path, "w"); 455 if (file != NULL) { 456 fprintf(file, "%d", version); 457 fsync(fileno(file)); 458 fclose(file); 459 } else { 460 ALOGE("Failed to save version to %s: %s", version_path, strerror(errno)); 461 goto fail; 462 } 463 464 // Success! 465 res = 0; 466 467fail: 468 free(user_data_dir); 469 free(legacy_data_dir); 470 free(primary_data_dir); 471 return res; 472} 473 474int main(const int argc, const char *argv[]) { 475 char buf[BUFFER_MAX]; 476 struct sockaddr addr; 477 socklen_t alen; 478 int lsocket, s, count; 479 480 if (initialize_globals() < 0) { 481 ALOGE("Could not initialize globals; exiting.\n"); 482 exit(1); 483 } 484 485 if (initialize_directories() < 0) { 486 ALOGE("Could not create directories; exiting.\n"); 487 exit(1); 488 } 489 490 lsocket = android_get_control_socket(SOCKET_PATH); 491 if (lsocket < 0) { 492 ALOGE("Failed to get socket from environment: %s\n", strerror(errno)); 493 exit(1); 494 } 495 if (listen(lsocket, 5)) { 496 ALOGE("Listen on socket failed: %s\n", strerror(errno)); 497 exit(1); 498 } 499 fcntl(lsocket, F_SETFD, FD_CLOEXEC); 500 501 for (;;) { 502 alen = sizeof(addr); 503 s = accept(lsocket, &addr, &alen); 504 if (s < 0) { 505 ALOGE("Accept failed: %s\n", strerror(errno)); 506 continue; 507 } 508 fcntl(s, F_SETFD, FD_CLOEXEC); 509 510 ALOGI("new connection\n"); 511 for (;;) { 512 unsigned short count; 513 if (readx(s, &count, sizeof(count))) { 514 ALOGE("failed to read size\n"); 515 break; 516 } 517 if ((count < 1) || (count >= BUFFER_MAX)) { 518 ALOGE("invalid size %d\n", count); 519 break; 520 } 521 if (readx(s, buf, count)) { 522 ALOGE("failed to read command\n"); 523 break; 524 } 525 buf[count] = 0; 526 if (execute(s, buf)) break; 527 } 528 ALOGI("closing connection\n"); 529 close(s); 530 } 531 532 return 0; 533} 534