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