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