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