commands.c revision d0c5f515c05d05c9d24971695337daf9d6ce409c
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#include <diskusage/dirsize.h> 19 20/* Directory records that are used in execution of commands. */ 21dir_rec_t android_data_dir; 22dir_rec_t android_asec_dir; 23dir_rec_t android_app_dir; 24dir_rec_t android_app_private_dir; 25dir_rec_array_t android_system_dirs; 26 27int install(const char *pkgname, uid_t uid, gid_t gid) 28{ 29 char pkgdir[PKG_PATH_MAX]; 30 char libdir[PKG_PATH_MAX]; 31 32 if ((uid < AID_SYSTEM) || (gid < AID_SYSTEM)) { 33 ALOGE("invalid uid/gid: %d %d\n", uid, gid); 34 return -1; 35 } 36 37 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, 0)) { 38 ALOGE("cannot create package path\n"); 39 return -1; 40 } 41 42 if (create_pkg_path(libdir, pkgname, PKG_LIB_POSTFIX, 0)) { 43 ALOGE("cannot create package lib path\n"); 44 return -1; 45 } 46 47 if (mkdir(pkgdir, 0751) < 0) { 48 ALOGE("cannot create dir '%s': %s\n", pkgdir, strerror(errno)); 49 return -errno; 50 } 51 if (chmod(pkgdir, 0751) < 0) { 52 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno)); 53 unlink(pkgdir); 54 return -errno; 55 } 56 if (chown(pkgdir, uid, gid) < 0) { 57 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno)); 58 unlink(pkgdir); 59 return -errno; 60 } 61 if (mkdir(libdir, 0755) < 0) { 62 ALOGE("cannot create dir '%s': %s\n", libdir, strerror(errno)); 63 unlink(pkgdir); 64 return -errno; 65 } 66 if (chmod(libdir, 0755) < 0) { 67 ALOGE("cannot chmod dir '%s': %s\n", libdir, strerror(errno)); 68 unlink(libdir); 69 unlink(pkgdir); 70 return -errno; 71 } 72 if (chown(libdir, AID_SYSTEM, AID_SYSTEM) < 0) { 73 ALOGE("cannot chown dir '%s': %s\n", libdir, strerror(errno)); 74 unlink(libdir); 75 unlink(pkgdir); 76 return -errno; 77 } 78 return 0; 79} 80 81int uninstall(const char *pkgname, uid_t persona) 82{ 83 char pkgdir[PKG_PATH_MAX]; 84 85 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, persona)) 86 return -1; 87 88 /* delete contents AND directory, no exceptions */ 89 return delete_dir_contents(pkgdir, 1, NULL); 90} 91 92int renamepkg(const char *oldpkgname, const char *newpkgname) 93{ 94 char oldpkgdir[PKG_PATH_MAX]; 95 char newpkgdir[PKG_PATH_MAX]; 96 97 if (create_pkg_path(oldpkgdir, oldpkgname, PKG_DIR_POSTFIX, 0)) 98 return -1; 99 if (create_pkg_path(newpkgdir, newpkgname, PKG_DIR_POSTFIX, 0)) 100 return -1; 101 102 if (rename(oldpkgdir, newpkgdir) < 0) { 103 ALOGE("cannot rename dir '%s' to '%s': %s\n", oldpkgdir, newpkgdir, strerror(errno)); 104 return -errno; 105 } 106 return 0; 107} 108 109int fix_uid(const char *pkgname, uid_t uid, gid_t gid) 110{ 111 char pkgdir[PKG_PATH_MAX]; 112 struct stat s; 113 int rc = 0; 114 115 if ((uid < AID_SYSTEM) || (gid < AID_SYSTEM)) { 116 ALOGE("invalid uid/gid: %d %d\n", uid, gid); 117 return -1; 118 } 119 120 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, 0)) { 121 ALOGE("cannot create package path\n"); 122 return -1; 123 } 124 125 if (stat(pkgdir, &s) < 0) return -1; 126 127 if (s.st_uid != 0 || s.st_gid != 0) { 128 ALOGE("fixing uid of non-root pkg: %s %d %d\n", pkgdir, s.st_uid, s.st_gid); 129 return -1; 130 } 131 132 if (chmod(pkgdir, 0751) < 0) { 133 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno)); 134 unlink(pkgdir); 135 return -errno; 136 } 137 if (chown(pkgdir, uid, gid) < 0) { 138 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno)); 139 unlink(pkgdir); 140 return -errno; 141 } 142 143 return 0; 144} 145 146int delete_user_data(const char *pkgname, uid_t persona) 147{ 148 char pkgdir[PKG_PATH_MAX]; 149 150 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, persona)) 151 return -1; 152 153 /* delete contents, excluding "lib", but not the directory itself */ 154 return delete_dir_contents(pkgdir, 0, "lib"); 155} 156 157int make_user_data(const char *pkgname, uid_t uid, uid_t persona) 158{ 159 char pkgdir[PKG_PATH_MAX]; 160 char real_libdir[PKG_PATH_MAX]; 161 162 // Create the data dir for the package 163 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, persona)) { 164 return -1; 165 } 166 if (mkdir(pkgdir, 0751) < 0) { 167 ALOGE("cannot create dir '%s': %s\n", pkgdir, strerror(errno)); 168 return -errno; 169 } 170 if (chown(pkgdir, uid, uid) < 0) { 171 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno)); 172 unlink(pkgdir); 173 return -errno; 174 } 175 return 0; 176} 177 178int delete_persona(uid_t persona) 179{ 180 char pkgdir[PKG_PATH_MAX]; 181 182 if (create_persona_path(pkgdir, persona)) 183 return -1; 184 185 return delete_dir_contents(pkgdir, 1, NULL); 186} 187 188int clone_persona_data(uid_t src_persona, uid_t target_persona, int copy) 189{ 190 char src_data_dir[PKG_PATH_MAX]; 191 char pkg_path[PKG_PATH_MAX]; 192 DIR *d; 193 struct dirent *de; 194 struct stat s; 195 uid_t uid; 196 197 if (create_persona_path(src_data_dir, src_persona)) { 198 return -1; 199 } 200 201 d = opendir(src_data_dir); 202 if (d != NULL) { 203 while ((de = readdir(d))) { 204 const char *name = de->d_name; 205 206 if (de->d_type == DT_DIR) { 207 int subfd; 208 /* always skip "." and ".." */ 209 if (name[0] == '.') { 210 if (name[1] == 0) continue; 211 if ((name[1] == '.') && (name[2] == 0)) continue; 212 } 213 /* Create the full path to the package's data dir */ 214 create_pkg_path(pkg_path, name, PKG_DIR_POSTFIX, src_persona); 215 /* Get the file stat */ 216 if (stat(pkg_path, &s) < 0) continue; 217 /* Get the uid of the package */ 218 ALOGI("Adding datadir for uid = %d\n", s.st_uid); 219 uid = (uid_t) s.st_uid % PER_USER_RANGE; 220 /* Create the directory for the target */ 221 make_user_data(name, uid + target_persona * PER_USER_RANGE, 222 target_persona); 223 } 224 } 225 closedir(d); 226 } 227 return 0; 228} 229 230int delete_cache(const char *pkgname) 231{ 232 char cachedir[PKG_PATH_MAX]; 233 234 if (create_pkg_path(cachedir, pkgname, CACHE_DIR_POSTFIX, 0)) 235 return -1; 236 237 /* delete contents, not the directory, no exceptions */ 238 return delete_dir_contents(cachedir, 0, 0); 239} 240 241static int64_t disk_free() 242{ 243 struct statfs sfs; 244 if (statfs(android_data_dir.path, &sfs) == 0) { 245 return sfs.f_bavail * sfs.f_bsize; 246 } else { 247 ALOGE("Couldn't statfs %s: %s\n", android_data_dir.path, strerror(errno)); 248 return -1; 249 } 250} 251 252/* Try to ensure free_size bytes of storage are available. 253 * Returns 0 on success. 254 * This is rather simple-minded because doing a full LRU would 255 * be potentially memory-intensive, and without atime it would 256 * also require that apps constantly modify file metadata even 257 * when just reading from the cache, which is pretty awful. 258 */ 259int free_cache(int64_t free_size) 260{ 261 const char *name; 262 int dfd, subfd; 263 DIR *d; 264 struct dirent *de; 265 int64_t avail; 266 char datadir[PKG_PATH_MAX]; 267 268 avail = disk_free(); 269 if (avail < 0) return -1; 270 271 ALOGI("free_cache(%" PRId64 ") avail %" PRId64 "\n", free_size, avail); 272 if (avail >= free_size) return 0; 273 274 if (create_persona_path(datadir, 0)) { 275 ALOGE("couldn't get directory for persona 0"); 276 return -1; 277 } 278 279 d = opendir(datadir); 280 if (d == NULL) { 281 ALOGE("cannot open %s: %s\n", datadir, strerror(errno)); 282 return -1; 283 } 284 dfd = dirfd(d); 285 286 while ((de = readdir(d))) { 287 if (de->d_type != DT_DIR) continue; 288 name = de->d_name; 289 290 /* always skip "." and ".." */ 291 if (name[0] == '.') { 292 if (name[1] == 0) continue; 293 if ((name[1] == '.') && (name[2] == 0)) continue; 294 } 295 296 subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY); 297 if (subfd < 0) continue; 298 299 delete_dir_contents_fd(subfd, "cache"); 300 close(subfd); 301 302 avail = disk_free(); 303 if (avail >= free_size) { 304 closedir(d); 305 return 0; 306 } 307 } 308 closedir(d); 309 310 /* Fail case - not possible to free space */ 311 return -1; 312} 313 314int move_dex(const char *src, const char *dst) 315{ 316 char src_dex[PKG_PATH_MAX]; 317 char dst_dex[PKG_PATH_MAX]; 318 319 if (validate_apk_path(src)) return -1; 320 if (validate_apk_path(dst)) return -1; 321 322 if (create_cache_path(src_dex, src)) return -1; 323 if (create_cache_path(dst_dex, dst)) return -1; 324 325 ALOGV("move %s -> %s\n", src_dex, dst_dex); 326 if (rename(src_dex, dst_dex) < 0) { 327 ALOGE("Couldn't move %s: %s\n", src_dex, strerror(errno)); 328 return -1; 329 } else { 330 return 0; 331 } 332} 333 334int rm_dex(const char *path) 335{ 336 char dex_path[PKG_PATH_MAX]; 337 338 if (validate_apk_path(path)) return -1; 339 if (create_cache_path(dex_path, path)) return -1; 340 341 ALOGV("unlink %s\n", dex_path); 342 if (unlink(dex_path) < 0) { 343 ALOGE("Couldn't unlink %s: %s\n", dex_path, strerror(errno)); 344 return -1; 345 } else { 346 return 0; 347 } 348} 349 350int protect(char *pkgname, gid_t gid) 351{ 352 struct stat s; 353 char pkgpath[PKG_PATH_MAX]; 354 355 if (gid < AID_SYSTEM) return -1; 356 357 if (create_pkg_path_in_dir(pkgpath, &android_app_private_dir, pkgname, ".apk")) 358 return -1; 359 360 if (stat(pkgpath, &s) < 0) return -1; 361 362 if (chown(pkgpath, s.st_uid, gid) < 0) { 363 ALOGE("failed to chgrp '%s': %s\n", pkgpath, strerror(errno)); 364 return -1; 365 } 366 367 if (chmod(pkgpath, S_IRUSR|S_IWUSR|S_IRGRP) < 0) { 368 ALOGE("failed to chmod '%s': %s\n", pkgpath, strerror(errno)); 369 return -1; 370 } 371 372 return 0; 373} 374 375int get_size(const char *pkgname, const char *apkpath, 376 const char *fwdlock_apkpath, const char *asecpath, 377 int64_t *_codesize, int64_t *_datasize, int64_t *_cachesize, 378 int64_t* _asecsize) 379{ 380 DIR *d; 381 int dfd; 382 struct dirent *de; 383 struct stat s; 384 char path[PKG_PATH_MAX]; 385 386 int64_t codesize = 0; 387 int64_t datasize = 0; 388 int64_t cachesize = 0; 389 int64_t asecsize = 0; 390 391 /* count the source apk as code -- but only if it's not 392 * on the /system partition and its not on the sdcard. 393 */ 394 if (validate_system_app_path(apkpath) && 395 strncmp(apkpath, android_asec_dir.path, android_asec_dir.len) != 0) { 396 if (stat(apkpath, &s) == 0) { 397 codesize += stat_size(&s); 398 } 399 } 400 /* count the forward locked apk as code if it is given 401 */ 402 if (fwdlock_apkpath != NULL && fwdlock_apkpath[0] != '!') { 403 if (stat(fwdlock_apkpath, &s) == 0) { 404 codesize += stat_size(&s); 405 } 406 } 407 /* count the cached dexfile as code */ 408 if (!create_cache_path(path, apkpath)) { 409 if (stat(path, &s) == 0) { 410 codesize += stat_size(&s); 411 } 412 } 413 414 /* compute asec size if it is given 415 */ 416 if (asecpath != NULL && asecpath[0] != '!') { 417 if (stat(asecpath, &s) == 0) { 418 asecsize += stat_size(&s); 419 } 420 } 421 422 if (create_pkg_path(path, pkgname, PKG_DIR_POSTFIX, 0)) { 423 goto done; 424 } 425 426 d = opendir(path); 427 if (d == NULL) { 428 goto done; 429 } 430 dfd = dirfd(d); 431 432 /* most stuff in the pkgdir is data, except for the "cache" 433 * directory and below, which is cache, and the "lib" directory 434 * and below, which is code... 435 */ 436 while ((de = readdir(d))) { 437 const char *name = de->d_name; 438 439 if (de->d_type == DT_DIR) { 440 int subfd; 441 /* always skip "." and ".." */ 442 if (name[0] == '.') { 443 if (name[1] == 0) continue; 444 if ((name[1] == '.') && (name[2] == 0)) continue; 445 } 446 subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY); 447 if (subfd >= 0) { 448 int64_t size = calculate_dir_size(subfd); 449 if (!strcmp(name,"lib")) { 450 codesize += size; 451 } else if(!strcmp(name,"cache")) { 452 cachesize += size; 453 } else { 454 datasize += size; 455 } 456 } 457 } else { 458 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) { 459 datasize += stat_size(&s); 460 } 461 } 462 } 463 closedir(d); 464done: 465 *_codesize = codesize; 466 *_datasize = datasize; 467 *_cachesize = cachesize; 468 *_asecsize = asecsize; 469 return 0; 470} 471 472 473/* a simpler version of dexOptGenerateCacheFileName() */ 474int create_cache_path(char path[PKG_PATH_MAX], const char *src) 475{ 476 char *tmp; 477 int srclen; 478 int dstlen; 479 480 srclen = strlen(src); 481 482 /* demand that we are an absolute path */ 483 if ((src == 0) || (src[0] != '/') || strstr(src,"..")) { 484 return -1; 485 } 486 487 if (srclen > PKG_PATH_MAX) { // XXX: PKG_NAME_MAX? 488 return -1; 489 } 490 491 dstlen = srclen + strlen(DALVIK_CACHE_PREFIX) + 492 strlen(DALVIK_CACHE_POSTFIX) + 1; 493 494 if (dstlen > PKG_PATH_MAX) { 495 return -1; 496 } 497 498 sprintf(path,"%s%s%s", 499 DALVIK_CACHE_PREFIX, 500 src + 1, /* skip the leading / */ 501 DALVIK_CACHE_POSTFIX); 502 503 for(tmp = path + strlen(DALVIK_CACHE_PREFIX); *tmp; tmp++) { 504 if (*tmp == '/') { 505 *tmp = '@'; 506 } 507 } 508 509 return 0; 510} 511 512static void run_dexopt(int zip_fd, int odex_fd, const char* input_file_name, 513 const char* dexopt_flags) 514{ 515 static const char* DEX_OPT_BIN = "/system/bin/dexopt"; 516 static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig 517 char zip_num[MAX_INT_LEN]; 518 char odex_num[MAX_INT_LEN]; 519 520 sprintf(zip_num, "%d", zip_fd); 521 sprintf(odex_num, "%d", odex_fd); 522 523 execl(DEX_OPT_BIN, DEX_OPT_BIN, "--zip", zip_num, odex_num, input_file_name, 524 dexopt_flags, (char*) NULL); 525 ALOGE("execl(%s) failed: %s\n", DEX_OPT_BIN, strerror(errno)); 526} 527 528static int wait_dexopt(pid_t pid, const char* apk_path) 529{ 530 int status; 531 pid_t got_pid; 532 533 /* 534 * Wait for the optimization process to finish. 535 */ 536 while (1) { 537 got_pid = waitpid(pid, &status, 0); 538 if (got_pid == -1 && errno == EINTR) { 539 printf("waitpid interrupted, retrying\n"); 540 } else { 541 break; 542 } 543 } 544 if (got_pid != pid) { 545 ALOGW("waitpid failed: wanted %d, got %d: %s\n", 546 (int) pid, (int) got_pid, strerror(errno)); 547 return 1; 548 } 549 550 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { 551 ALOGV("DexInv: --- END '%s' (success) ---\n", apk_path); 552 return 0; 553 } else { 554 ALOGW("DexInv: --- END '%s' --- status=0x%04x, process failed\n", 555 apk_path, status); 556 return status; /* always nonzero */ 557 } 558} 559 560int dexopt(const char *apk_path, uid_t uid, int is_public) 561{ 562 struct utimbuf ut; 563 struct stat apk_stat, dex_stat; 564 char dex_path[PKG_PATH_MAX]; 565 char dexopt_flags[PROPERTY_VALUE_MAX]; 566 char *end; 567 int res, zip_fd=-1, odex_fd=-1; 568 569 /* Before anything else: is there a .odex file? If so, we have 570 * pre-optimized the apk and there is nothing to do here. 571 */ 572 if (strlen(apk_path) >= (PKG_PATH_MAX - 8)) { 573 return -1; 574 } 575 576 /* platform-specific flags affecting optimization and verification */ 577 property_get("dalvik.vm.dexopt-flags", dexopt_flags, ""); 578 579 strcpy(dex_path, apk_path); 580 end = strrchr(dex_path, '.'); 581 if (end != NULL) { 582 strcpy(end, ".odex"); 583 if (stat(dex_path, &dex_stat) == 0) { 584 return 0; 585 } 586 } 587 588 if (create_cache_path(dex_path, apk_path)) { 589 return -1; 590 } 591 592 memset(&apk_stat, 0, sizeof(apk_stat)); 593 stat(apk_path, &apk_stat); 594 595 zip_fd = open(apk_path, O_RDONLY, 0); 596 if (zip_fd < 0) { 597 ALOGE("dexopt cannot open '%s' for input\n", apk_path); 598 return -1; 599 } 600 601 unlink(dex_path); 602 odex_fd = open(dex_path, O_RDWR | O_CREAT | O_EXCL, 0644); 603 if (odex_fd < 0) { 604 ALOGE("dexopt cannot open '%s' for output\n", dex_path); 605 goto fail; 606 } 607 if (fchown(odex_fd, AID_SYSTEM, uid) < 0) { 608 ALOGE("dexopt cannot chown '%s'\n", dex_path); 609 goto fail; 610 } 611 if (fchmod(odex_fd, 612 S_IRUSR|S_IWUSR|S_IRGRP | 613 (is_public ? S_IROTH : 0)) < 0) { 614 ALOGE("dexopt cannot chmod '%s'\n", dex_path); 615 goto fail; 616 } 617 618 ALOGV("DexInv: --- BEGIN '%s' ---\n", apk_path); 619 620 pid_t pid; 621 pid = fork(); 622 if (pid == 0) { 623 /* child -- drop privileges before continuing */ 624 if (setgid(uid) != 0) { 625 ALOGE("setgid(%d) failed during dexopt\n", uid); 626 exit(64); 627 } 628 if (setuid(uid) != 0) { 629 ALOGE("setuid(%d) during dexopt\n", uid); 630 exit(65); 631 } 632 if (flock(odex_fd, LOCK_EX | LOCK_NB) != 0) { 633 ALOGE("flock(%s) failed: %s\n", dex_path, strerror(errno)); 634 exit(66); 635 } 636 637 run_dexopt(zip_fd, odex_fd, apk_path, dexopt_flags); 638 exit(67); /* only get here on exec failure */ 639 } else { 640 res = wait_dexopt(pid, apk_path); 641 if (res != 0) { 642 ALOGE("dexopt failed on '%s' res = %d\n", dex_path, res); 643 goto fail; 644 } 645 } 646 647 ut.actime = apk_stat.st_atime; 648 ut.modtime = apk_stat.st_mtime; 649 utime(dex_path, &ut); 650 651 close(odex_fd); 652 close(zip_fd); 653 return 0; 654 655fail: 656 if (odex_fd >= 0) { 657 close(odex_fd); 658 unlink(dex_path); 659 } 660 if (zip_fd >= 0) { 661 close(zip_fd); 662 } 663 return -1; 664} 665 666void mkinnerdirs(char* path, int basepos, mode_t mode, int uid, int gid, 667 struct stat* statbuf) 668{ 669 while (path[basepos] != 0) { 670 if (path[basepos] == '/') { 671 path[basepos] = 0; 672 if (lstat(path, statbuf) < 0) { 673 ALOGV("Making directory: %s\n", path); 674 if (mkdir(path, mode) == 0) { 675 chown(path, uid, gid); 676 } else { 677 ALOGW("Unable to make directory %s: %s\n", path, strerror(errno)); 678 } 679 } 680 path[basepos] = '/'; 681 basepos++; 682 } 683 basepos++; 684 } 685} 686 687int movefileordir(char* srcpath, char* dstpath, int dstbasepos, 688 int dstuid, int dstgid, struct stat* statbuf) 689{ 690 DIR *d; 691 struct dirent *de; 692 int res; 693 694 int srcend = strlen(srcpath); 695 int dstend = strlen(dstpath); 696 697 if (lstat(srcpath, statbuf) < 0) { 698 ALOGW("Unable to stat %s: %s\n", srcpath, strerror(errno)); 699 return 1; 700 } 701 702 if ((statbuf->st_mode&S_IFDIR) == 0) { 703 mkinnerdirs(dstpath, dstbasepos, S_IRWXU|S_IRWXG|S_IXOTH, 704 dstuid, dstgid, statbuf); 705 ALOGV("Renaming %s to %s (uid %d)\n", srcpath, dstpath, dstuid); 706 if (rename(srcpath, dstpath) >= 0) { 707 if (chown(dstpath, dstuid, dstgid) < 0) { 708 ALOGE("cannot chown %s: %s\n", dstpath, strerror(errno)); 709 unlink(dstpath); 710 return 1; 711 } 712 } else { 713 ALOGW("Unable to rename %s to %s: %s\n", 714 srcpath, dstpath, strerror(errno)); 715 return 1; 716 } 717 return 0; 718 } 719 720 d = opendir(srcpath); 721 if (d == NULL) { 722 ALOGW("Unable to opendir %s: %s\n", srcpath, strerror(errno)); 723 return 1; 724 } 725 726 res = 0; 727 728 while ((de = readdir(d))) { 729 const char *name = de->d_name; 730 /* always skip "." and ".." */ 731 if (name[0] == '.') { 732 if (name[1] == 0) continue; 733 if ((name[1] == '.') && (name[2] == 0)) continue; 734 } 735 736 if ((srcend+strlen(name)) >= (PKG_PATH_MAX-2)) { 737 ALOGW("Source path too long; skipping: %s/%s\n", srcpath, name); 738 continue; 739 } 740 741 if ((dstend+strlen(name)) >= (PKG_PATH_MAX-2)) { 742 ALOGW("Destination path too long; skipping: %s/%s\n", dstpath, name); 743 continue; 744 } 745 746 srcpath[srcend] = dstpath[dstend] = '/'; 747 strcpy(srcpath+srcend+1, name); 748 strcpy(dstpath+dstend+1, name); 749 750 if (movefileordir(srcpath, dstpath, dstbasepos, dstuid, dstgid, statbuf) != 0) { 751 res = 1; 752 } 753 754 // Note: we will be leaving empty directories behind in srcpath, 755 // but that is okay, the package manager will be erasing all of the 756 // data associated with .apks that disappear. 757 758 srcpath[srcend] = dstpath[dstend] = 0; 759 } 760 761 closedir(d); 762 return res; 763} 764 765int movefiles() 766{ 767 DIR *d; 768 int dfd, subfd; 769 struct dirent *de; 770 struct stat s; 771 char buf[PKG_PATH_MAX+1]; 772 int bufp, bufe, bufi, readlen; 773 774 char srcpkg[PKG_NAME_MAX]; 775 char dstpkg[PKG_NAME_MAX]; 776 char srcpath[PKG_PATH_MAX]; 777 char dstpath[PKG_PATH_MAX]; 778 int dstuid=-1, dstgid=-1; 779 int hasspace; 780 781 d = opendir(UPDATE_COMMANDS_DIR_PREFIX); 782 if (d == NULL) { 783 goto done; 784 } 785 dfd = dirfd(d); 786 787 /* Iterate through all files in the directory, executing the 788 * file movements requested there-in. 789 */ 790 while ((de = readdir(d))) { 791 const char *name = de->d_name; 792 793 if (de->d_type == DT_DIR) { 794 continue; 795 } else { 796 subfd = openat(dfd, name, O_RDONLY); 797 if (subfd < 0) { 798 ALOGW("Unable to open update commands at %s%s\n", 799 UPDATE_COMMANDS_DIR_PREFIX, name); 800 continue; 801 } 802 803 bufp = 0; 804 bufe = 0; 805 buf[PKG_PATH_MAX] = 0; 806 srcpkg[0] = dstpkg[0] = 0; 807 while (1) { 808 bufi = bufp; 809 while (bufi < bufe && buf[bufi] != '\n') { 810 bufi++; 811 } 812 if (bufi < bufe) { 813 buf[bufi] = 0; 814 ALOGV("Processing line: %s\n", buf+bufp); 815 hasspace = 0; 816 while (bufp < bufi && isspace(buf[bufp])) { 817 hasspace = 1; 818 bufp++; 819 } 820 if (buf[bufp] == '#' || bufp == bufi) { 821 // skip comments and empty lines. 822 } else if (hasspace) { 823 if (dstpkg[0] == 0) { 824 ALOGW("Path before package line in %s%s: %s\n", 825 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp); 826 } else if (srcpkg[0] == 0) { 827 // Skip -- source package no longer exists. 828 } else { 829 ALOGV("Move file: %s (from %s to %s)\n", buf+bufp, srcpkg, dstpkg); 830 if (!create_move_path(srcpath, srcpkg, buf+bufp, 0) && 831 !create_move_path(dstpath, dstpkg, buf+bufp, 0)) { 832 movefileordir(srcpath, dstpath, 833 strlen(dstpath)-strlen(buf+bufp), 834 dstuid, dstgid, &s); 835 } 836 } 837 } else { 838 char* div = strchr(buf+bufp, ':'); 839 if (div == NULL) { 840 ALOGW("Bad package spec in %s%s; no ':' sep: %s\n", 841 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp); 842 } else { 843 *div = 0; 844 div++; 845 if (strlen(buf+bufp) < PKG_NAME_MAX) { 846 strcpy(dstpkg, buf+bufp); 847 } else { 848 srcpkg[0] = dstpkg[0] = 0; 849 ALOGW("Package name too long in %s%s: %s\n", 850 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp); 851 } 852 if (strlen(div) < PKG_NAME_MAX) { 853 strcpy(srcpkg, div); 854 } else { 855 srcpkg[0] = dstpkg[0] = 0; 856 ALOGW("Package name too long in %s%s: %s\n", 857 UPDATE_COMMANDS_DIR_PREFIX, name, div); 858 } 859 if (srcpkg[0] != 0) { 860 if (!create_pkg_path(srcpath, srcpkg, PKG_DIR_POSTFIX, 0)) { 861 if (lstat(srcpath, &s) < 0) { 862 // Package no longer exists -- skip. 863 srcpkg[0] = 0; 864 } 865 } else { 866 srcpkg[0] = 0; 867 ALOGW("Can't create path %s in %s%s\n", 868 div, UPDATE_COMMANDS_DIR_PREFIX, name); 869 } 870 if (srcpkg[0] != 0) { 871 if (!create_pkg_path(dstpath, dstpkg, PKG_DIR_POSTFIX, 0)) { 872 if (lstat(dstpath, &s) == 0) { 873 dstuid = s.st_uid; 874 dstgid = s.st_gid; 875 } else { 876 // Destination package doesn't 877 // exist... due to original-package, 878 // this is normal, so don't be 879 // noisy about it. 880 srcpkg[0] = 0; 881 } 882 } else { 883 srcpkg[0] = 0; 884 ALOGW("Can't create path %s in %s%s\n", 885 div, UPDATE_COMMANDS_DIR_PREFIX, name); 886 } 887 } 888 ALOGV("Transfering from %s to %s: uid=%d\n", 889 srcpkg, dstpkg, dstuid); 890 } 891 } 892 } 893 bufp = bufi+1; 894 } else { 895 if (bufp == 0) { 896 if (bufp < bufe) { 897 ALOGW("Line too long in %s%s, skipping: %s\n", 898 UPDATE_COMMANDS_DIR_PREFIX, name, buf); 899 } 900 } else if (bufp < bufe) { 901 memcpy(buf, buf+bufp, bufe-bufp); 902 bufe -= bufp; 903 bufp = 0; 904 } 905 readlen = read(subfd, buf+bufe, PKG_PATH_MAX-bufe); 906 if (readlen < 0) { 907 ALOGW("Failure reading update commands in %s%s: %s\n", 908 UPDATE_COMMANDS_DIR_PREFIX, name, strerror(errno)); 909 break; 910 } else if (readlen == 0) { 911 break; 912 } 913 bufe += readlen; 914 buf[bufe] = 0; 915 ALOGV("Read buf: %s\n", buf); 916 } 917 } 918 close(subfd); 919 } 920 } 921 closedir(d); 922done: 923 return 0; 924} 925 926int linklib(const char* dataDir, const char* asecLibDir) 927{ 928 char libdir[PKG_PATH_MAX]; 929 struct stat s, libStat; 930 int rc = 0; 931 932 const size_t libdirLen = strlen(dataDir) + strlen(PKG_LIB_POSTFIX); 933 if (libdirLen >= PKG_PATH_MAX) { 934 ALOGE("library dir len too large"); 935 return -1; 936 } 937 938 if (snprintf(libdir, sizeof(libdir), "%s%s", dataDir, PKG_LIB_POSTFIX) != (ssize_t)libdirLen) { 939 ALOGE("library dir not written successfully: %s\n", strerror(errno)); 940 return -1; 941 } 942 943 if (stat(dataDir, &s) < 0) return -1; 944 945 if (chown(dataDir, 0, 0) < 0) { 946 ALOGE("failed to chown '%s': %s\n", dataDir, strerror(errno)); 947 return -1; 948 } 949 950 if (chmod(dataDir, 0700) < 0) { 951 ALOGE("failed to chmod '%s': %s\n", dataDir, strerror(errno)); 952 rc = -1; 953 goto out; 954 } 955 956 if (lstat(libdir, &libStat) < 0) { 957 ALOGE("couldn't stat lib dir: %s\n", strerror(errno)); 958 rc = -1; 959 goto out; 960 } 961 962 if (S_ISDIR(libStat.st_mode)) { 963 if (delete_dir_contents(libdir, 1, 0) < 0) { 964 rc = -1; 965 goto out; 966 } 967 } else if (S_ISLNK(libStat.st_mode)) { 968 if (unlink(libdir) < 0) { 969 rc = -1; 970 goto out; 971 } 972 } 973 974 if (symlink(asecLibDir, libdir) < 0) { 975 ALOGE("couldn't symlink directory '%s' -> '%s': %s\n", libdir, asecLibDir, strerror(errno)); 976 rc = -errno; 977 goto out; 978 } 979 980 if (lchown(libdir, AID_SYSTEM, AID_SYSTEM) < 0) { 981 ALOGE("cannot chown dir '%s': %s\n", libdir, strerror(errno)); 982 unlink(libdir); 983 rc = -errno; 984 goto out; 985 } 986 987out: 988 if (chmod(dataDir, s.st_mode) < 0) { 989 ALOGE("failed to chmod '%s': %s\n", dataDir, strerror(errno)); 990 rc = -errno; 991 } 992 993 if (chown(dataDir, s.st_uid, s.st_gid) < 0) { 994 ALOGE("failed to chown '%s' : %s\n", dataDir, strerror(errno)); 995 return -errno; 996 } 997 998 return rc; 999} 1000 1001int unlinklib(const char* dataDir) 1002{ 1003 char libdir[PKG_PATH_MAX]; 1004 struct stat s, libStat; 1005 int rc = 0; 1006 1007 const size_t libdirLen = strlen(dataDir) + strlen(PKG_LIB_POSTFIX); 1008 if (libdirLen >= PKG_PATH_MAX) { 1009 return -1; 1010 } 1011 1012 if (snprintf(libdir, sizeof(libdir), "%s%s", dataDir, PKG_LIB_POSTFIX) != (ssize_t)libdirLen) { 1013 ALOGE("library dir not written successfully: %s\n", strerror(errno)); 1014 return -1; 1015 } 1016 1017 if (stat(dataDir, &s) < 0) { 1018 ALOGE("couldn't state data dir"); 1019 return -1; 1020 } 1021 1022 if (chown(dataDir, 0, 0) < 0) { 1023 ALOGE("failed to chown '%s': %s\n", dataDir, strerror(errno)); 1024 return -1; 1025 } 1026 1027 if (chmod(dataDir, 0700) < 0) { 1028 ALOGE("failed to chmod '%s': %s\n", dataDir, strerror(errno)); 1029 rc = -1; 1030 goto out; 1031 } 1032 1033 if (lstat(libdir, &libStat) < 0) { 1034 ALOGE("couldn't stat lib dir: %s\n", strerror(errno)); 1035 rc = -1; 1036 goto out; 1037 } 1038 1039 if (S_ISDIR(libStat.st_mode)) { 1040 if (delete_dir_contents(libdir, 1, 0) < 0) { 1041 rc = -1; 1042 goto out; 1043 } 1044 } else if (S_ISLNK(libStat.st_mode)) { 1045 if (unlink(libdir) < 0) { 1046 rc = -1; 1047 goto out; 1048 } 1049 } 1050 1051 if (mkdir(libdir, 0755) < 0) { 1052 ALOGE("cannot create dir '%s': %s\n", libdir, strerror(errno)); 1053 rc = -errno; 1054 goto out; 1055 } 1056 1057 if (chown(libdir, AID_SYSTEM, AID_SYSTEM) < 0) { 1058 ALOGE("cannot chown dir '%s': %s\n", libdir, strerror(errno)); 1059 unlink(libdir); 1060 rc = -errno; 1061 goto out; 1062 } 1063 1064out: 1065 if (chmod(dataDir, s.st_mode) < 0) { 1066 ALOGE("failed to chmod '%s': %s\n", dataDir, strerror(errno)); 1067 rc = -1; 1068 } 1069 1070 if (chown(dataDir, s.st_uid, s.st_gid) < 0) { 1071 ALOGE("failed to chown '%s' : %s\n", dataDir, strerror(errno)); 1072 return -1; 1073 } 1074 1075 return rc; 1076} 1077