InstalldNativeService.cpp revision 451c17d82077cca61d0d04097961e99d84f54146
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 "InstalldNativeService.h" 18 19#include <errno.h> 20#include <inttypes.h> 21#include <regex> 22#include <stdlib.h> 23#include <sys/capability.h> 24#include <sys/file.h> 25#include <sys/resource.h> 26#include <sys/stat.h> 27#include <sys/types.h> 28#include <sys/wait.h> 29#include <sys/xattr.h> 30#include <unistd.h> 31 32#include <android-base/logging.h> 33#include <android-base/stringprintf.h> 34#include <android-base/strings.h> 35#include <android-base/unique_fd.h> 36#include <cutils/fs.h> 37#include <cutils/log.h> // TODO: Move everything to base/logging. 38#include <cutils/properties.h> 39#include <cutils/sched_policy.h> 40#include <diskusage/dirsize.h> 41#include <logwrap/logwrap.h> 42#include <private/android_filesystem_config.h> 43#include <selinux/android.h> 44#include <system/thread_defs.h> 45 46#include "dexopt.h" 47#include "globals.h" 48#include "installd_deps.h" 49#include "otapreopt_utils.h" 50#include "utils.h" 51 52#ifndef LOG_TAG 53#define LOG_TAG "installd" 54#endif 55 56using android::base::StringPrintf; 57 58namespace android { 59namespace installd { 60 61static constexpr const char* kCpPath = "/system/bin/cp"; 62static constexpr const char* kXattrDefault = "user.default"; 63 64static constexpr const int MIN_RESTRICTED_HOME_SDK_VERSION = 24; // > M 65 66static constexpr const char* PKG_LIB_POSTFIX = "/lib"; 67static constexpr const char* CACHE_DIR_POSTFIX = "/cache"; 68static constexpr const char* CODE_CACHE_DIR_POSTFIX = "/code_cache"; 69 70static constexpr const char* IDMAP_PREFIX = "/data/resource-cache/"; 71static constexpr const char* IDMAP_SUFFIX = "@idmap"; 72 73// NOTE: keep in sync with StorageManager 74static constexpr int FLAG_STORAGE_DE = 1 << 0; 75static constexpr int FLAG_STORAGE_CE = 1 << 1; 76 77// NOTE: keep in sync with Installer 78static constexpr int FLAG_CLEAR_CACHE_ONLY = 1 << 8; 79static constexpr int FLAG_CLEAR_CODE_CACHE_ONLY = 1 << 9; 80 81 82namespace { 83 84constexpr const char* kDump = "android.permission.DUMP"; 85 86static binder::Status ok() { 87 return binder::Status::ok(); 88} 89 90static binder::Status exception(uint32_t code) { 91 return binder::Status::fromExceptionCode(code); 92} 93 94static binder::Status exception(uint32_t code, const std::string& msg) { 95 return binder::Status::fromExceptionCode(code, String8(msg.c_str())); 96} 97 98static binder::Status error() { 99 return binder::Status::fromServiceSpecificError(errno); 100} 101 102static binder::Status error(const std::string& msg) { 103 PLOG(ERROR) << msg; 104 return binder::Status::fromServiceSpecificError(errno, String8(msg.c_str())); 105} 106 107static binder::Status error(uint32_t code, const std::string& msg) { 108 LOG(ERROR) << msg << " (" << code << ")"; 109 return binder::Status::fromServiceSpecificError(code, String8(msg.c_str())); 110} 111 112binder::Status checkPermission(const char* permission) { 113 pid_t pid; 114 uid_t uid; 115 116 if (checkCallingPermission(String16(permission), reinterpret_cast<int32_t*>(&pid), 117 reinterpret_cast<int32_t*>(&uid))) { 118 return ok(); 119 } else { 120 return exception(binder::Status::EX_SECURITY, 121 StringPrintf("UID %d / PID %d lacks permission %s", uid, pid, permission)); 122 } 123} 124 125binder::Status checkUid(uid_t expectedUid) { 126 uid_t uid = IPCThreadState::self()->getCallingUid(); 127 if (uid == expectedUid || uid == AID_ROOT) { 128 return ok(); 129 } else { 130 return exception(binder::Status::EX_SECURITY, 131 StringPrintf("UID %d is not expected UID %d", uid, expectedUid)); 132 } 133} 134 135binder::Status checkArgumentUuid(const std::unique_ptr<std::string>& uuid) { 136 if (!uuid || is_valid_filename(*uuid)) { 137 return ok(); 138 } else { 139 return exception(binder::Status::EX_ILLEGAL_ARGUMENT, 140 StringPrintf("UUID %s is malformed", uuid->c_str())); 141 } 142} 143 144binder::Status checkArgumentPackageName(const std::string& packageName) { 145 if (is_valid_package_name(packageName.c_str())) { 146 return ok(); 147 } else { 148 return exception(binder::Status::EX_ILLEGAL_ARGUMENT, 149 StringPrintf("Package name %s is malformed", packageName.c_str())); 150 } 151} 152 153#define ENFORCE_UID(uid) { \ 154 binder::Status status = checkUid((uid)); \ 155 if (!status.isOk()) { \ 156 return status; \ 157 } \ 158} 159 160#define CHECK_ARGUMENT_UUID(uuid) { \ 161 binder::Status status = checkArgumentUuid((uuid)); \ 162 if (!status.isOk()) { \ 163 return status; \ 164 } \ 165} 166 167#define CHECK_ARGUMENT_PACKAGE_NAME(packageName) { \ 168 binder::Status status = \ 169 checkArgumentPackageName((packageName)); \ 170 if (!status.isOk()) { \ 171 return status; \ 172 } \ 173} 174 175} // namespace 176 177status_t InstalldNativeService::start() { 178 IPCThreadState::self()->disableBackgroundScheduling(true); 179 status_t ret = BinderService<InstalldNativeService>::publish(); 180 if (ret != android::OK) { 181 return ret; 182 } 183 sp<ProcessState> ps(ProcessState::self()); 184 ps->startThreadPool(); 185 ps->giveThreadPoolName(); 186 return android::OK; 187} 188 189status_t InstalldNativeService::dump(int fd, const Vector<String16> & /* args */) { 190 const binder::Status dump_permission = checkPermission(kDump); 191 if (!dump_permission.isOk()) { 192 const String8 msg(dump_permission.toString8()); 193 write(fd, msg.string(), msg.size()); 194 return PERMISSION_DENIED; 195 } 196 197 std::string msg = "installd is happy\n"; 198 write(fd, msg.c_str(), strlen(msg.c_str())); 199 return NO_ERROR; 200} 201 202/** 203 * Perform restorecon of the given path, but only perform recursive restorecon 204 * if the label of that top-level file actually changed. This can save us 205 * significant time by avoiding no-op traversals of large filesystem trees. 206 */ 207static int restorecon_app_data_lazy(const std::string& path, const std::string& seInfo, uid_t uid) { 208 int res = 0; 209 char* before = nullptr; 210 char* after = nullptr; 211 212 // Note that SELINUX_ANDROID_RESTORECON_DATADATA flag is set by 213 // libselinux. Not needed here. 214 215 if (lgetfilecon(path.c_str(), &before) < 0) { 216 PLOG(ERROR) << "Failed before getfilecon for " << path; 217 goto fail; 218 } 219 if (selinux_android_restorecon_pkgdir(path.c_str(), seInfo.c_str(), uid, 0) < 0) { 220 PLOG(ERROR) << "Failed top-level restorecon for " << path; 221 goto fail; 222 } 223 if (lgetfilecon(path.c_str(), &after) < 0) { 224 PLOG(ERROR) << "Failed after getfilecon for " << path; 225 goto fail; 226 } 227 228 // If the initial top-level restorecon above changed the label, then go 229 // back and restorecon everything recursively 230 if (strcmp(before, after)) { 231 LOG(DEBUG) << "Detected label change from " << before << " to " << after << " at " << path 232 << "; running recursive restorecon"; 233 if (selinux_android_restorecon_pkgdir(path.c_str(), seInfo.c_str(), uid, 234 SELINUX_ANDROID_RESTORECON_RECURSE) < 0) { 235 PLOG(ERROR) << "Failed recursive restorecon for " << path; 236 goto fail; 237 } 238 } 239 240 goto done; 241fail: 242 res = -1; 243done: 244 free(before); 245 free(after); 246 return res; 247} 248 249static int restorecon_app_data_lazy(const std::string& parent, const char* name, 250 const std::string& seInfo, uid_t uid) { 251 return restorecon_app_data_lazy(StringPrintf("%s/%s", parent.c_str(), name), seInfo, uid); 252} 253 254static int prepare_app_dir(const std::string& path, mode_t target_mode, uid_t uid) { 255 if (fs_prepare_dir_strict(path.c_str(), target_mode, uid, uid) != 0) { 256 PLOG(ERROR) << "Failed to prepare " << path; 257 return -1; 258 } 259 return 0; 260} 261 262static int prepare_app_dir(const std::string& parent, const char* name, mode_t target_mode, 263 uid_t uid) { 264 return prepare_app_dir(StringPrintf("%s/%s", parent.c_str(), name), target_mode, uid); 265} 266 267binder::Status InstalldNativeService::createAppData(const std::unique_ptr<std::string>& uuid, 268 const std::string& packageName, int32_t userId, int32_t flags, int32_t appId, 269 const std::string& seInfo, int32_t targetSdkVersion) { 270 ENFORCE_UID(AID_SYSTEM); 271 CHECK_ARGUMENT_UUID(uuid); 272 CHECK_ARGUMENT_PACKAGE_NAME(packageName); 273 274 const char* uuid_ = uuid ? uuid->c_str() : nullptr; 275 const char* pkgname = packageName.c_str(); 276 277 uid_t uid = multiuser_get_uid(userId, appId); 278 mode_t target_mode = targetSdkVersion >= MIN_RESTRICTED_HOME_SDK_VERSION ? 0700 : 0751; 279 if (flags & FLAG_STORAGE_CE) { 280 auto path = create_data_user_ce_package_path(uuid_, userId, pkgname); 281 if (prepare_app_dir(path, target_mode, uid) || 282 prepare_app_dir(path, "cache", 0771, uid) || 283 prepare_app_dir(path, "code_cache", 0771, uid)) { 284 return error("Failed to prepare " + path); 285 } 286 287 // Consider restorecon over contents if label changed 288 if (restorecon_app_data_lazy(path, seInfo, uid) || 289 restorecon_app_data_lazy(path, "cache", seInfo, uid) || 290 restorecon_app_data_lazy(path, "code_cache", seInfo, uid)) { 291 return error("Failed to restorecon " + path); 292 } 293 294 // Remember inode numbers of cache directories so that we can clear 295 // contents while CE storage is locked 296 if (write_path_inode(path, "cache", kXattrInodeCache) || 297 write_path_inode(path, "code_cache", kXattrInodeCodeCache)) { 298 return error("Failed to write_path_inode for " + path); 299 } 300 } 301 if (flags & FLAG_STORAGE_DE) { 302 auto path = create_data_user_de_package_path(uuid_, userId, pkgname); 303 if (prepare_app_dir(path, target_mode, uid)) { 304 return error("Failed to prepare " + path); 305 } 306 307 // Consider restorecon over contents if label changed 308 if (restorecon_app_data_lazy(path, seInfo, uid)) { 309 return error("Failed to restorecon " + path); 310 } 311 312 if (property_get_bool("dalvik.vm.usejitprofiles", false)) { 313 const std::string profile_path = create_data_user_profile_package_path(userId, pkgname); 314 // read-write-execute only for the app user. 315 if (fs_prepare_dir_strict(profile_path.c_str(), 0700, uid, uid) != 0) { 316 return error("Failed to prepare " + profile_path); 317 } 318 std::string profile_file = create_primary_profile(profile_path); 319 // read-write only for the app user. 320 if (fs_prepare_file_strict(profile_file.c_str(), 0600, uid, uid) != 0) { 321 return error("Failed to prepare " + profile_path); 322 } 323 const std::string ref_profile_path = create_data_ref_profile_package_path(pkgname); 324 // dex2oat/profman runs under the shared app gid and it needs to read/write reference 325 // profiles. 326 int shared_app_gid = multiuser_get_shared_app_gid(uid); 327 if ((shared_app_gid != -1) && fs_prepare_dir_strict( 328 ref_profile_path.c_str(), 0700, shared_app_gid, shared_app_gid) != 0) { 329 return error("Failed to prepare " + ref_profile_path); 330 } 331 } 332 } 333 return ok(); 334} 335 336binder::Status InstalldNativeService::migrateAppData(const std::unique_ptr<std::string>& uuid, 337 const std::string& packageName, int32_t userId, int32_t flags) { 338 ENFORCE_UID(AID_SYSTEM); 339 CHECK_ARGUMENT_UUID(uuid); 340 CHECK_ARGUMENT_PACKAGE_NAME(packageName); 341 342 const char* uuid_ = uuid ? uuid->c_str() : nullptr; 343 const char* pkgname = packageName.c_str(); 344 345 // This method only exists to upgrade system apps that have requested 346 // forceDeviceEncrypted, so their default storage always lives in a 347 // consistent location. This only works on non-FBE devices, since we 348 // never want to risk exposing data on a device with real CE/DE storage. 349 350 auto ce_path = create_data_user_ce_package_path(uuid_, userId, pkgname); 351 auto de_path = create_data_user_de_package_path(uuid_, userId, pkgname); 352 353 // If neither directory is marked as default, assume CE is default 354 if (getxattr(ce_path.c_str(), kXattrDefault, nullptr, 0) == -1 355 && getxattr(de_path.c_str(), kXattrDefault, nullptr, 0) == -1) { 356 if (setxattr(ce_path.c_str(), kXattrDefault, nullptr, 0, 0) != 0) { 357 return error("Failed to mark default storage " + ce_path); 358 } 359 } 360 361 // Migrate default data location if needed 362 auto target = (flags & FLAG_STORAGE_DE) ? de_path : ce_path; 363 auto source = (flags & FLAG_STORAGE_DE) ? ce_path : de_path; 364 365 if (getxattr(target.c_str(), kXattrDefault, nullptr, 0) == -1) { 366 LOG(WARNING) << "Requested default storage " << target 367 << " is not active; migrating from " << source; 368 if (delete_dir_contents_and_dir(target) != 0) { 369 return error("Failed to delete " + target); 370 } 371 if (rename(source.c_str(), target.c_str()) != 0) { 372 return error("Failed to rename " + source + " to " + target); 373 } 374 } 375 376 return ok(); 377} 378 379 380binder::Status InstalldNativeService::clearAppProfiles(const std::string& packageName) { 381 ENFORCE_UID(AID_SYSTEM); 382 CHECK_ARGUMENT_PACKAGE_NAME(packageName); 383 384 const char* pkgname = packageName.c_str(); 385 binder::Status res = ok(); 386 if (!clear_reference_profile(pkgname)) { 387 res = error("Failed to clear reference profile for " + packageName); 388 } 389 if (!clear_current_profiles(pkgname)) { 390 res = error("Failed to clear current profiles for " + packageName); 391 } 392 return res; 393} 394 395binder::Status InstalldNativeService::clearAppData(const std::unique_ptr<std::string>& uuid, 396 const std::string& packageName, int32_t userId, int32_t flags, int64_t ceDataInode) { 397 ENFORCE_UID(AID_SYSTEM); 398 CHECK_ARGUMENT_UUID(uuid); 399 CHECK_ARGUMENT_PACKAGE_NAME(packageName); 400 401 const char* uuid_ = uuid ? uuid->c_str() : nullptr; 402 const char* pkgname = packageName.c_str(); 403 404 binder::Status res = ok(); 405 if (flags & FLAG_STORAGE_CE) { 406 auto path = create_data_user_ce_package_path(uuid_, userId, pkgname, ceDataInode); 407 if (flags & FLAG_CLEAR_CACHE_ONLY) { 408 path = read_path_inode(path, "cache", kXattrInodeCache); 409 } else if (flags & FLAG_CLEAR_CODE_CACHE_ONLY) { 410 path = read_path_inode(path, "code_cache", kXattrInodeCodeCache); 411 } 412 if (access(path.c_str(), F_OK) == 0) { 413 if (delete_dir_contents(path) != 0) { 414 res = error("Failed to delete contents of " + path); 415 } 416 } 417 } 418 if (flags & FLAG_STORAGE_DE) { 419 std::string suffix = ""; 420 bool only_cache = false; 421 if (flags & FLAG_CLEAR_CACHE_ONLY) { 422 suffix = CACHE_DIR_POSTFIX; 423 only_cache = true; 424 } else if (flags & FLAG_CLEAR_CODE_CACHE_ONLY) { 425 suffix = CODE_CACHE_DIR_POSTFIX; 426 only_cache = true; 427 } 428 429 auto path = create_data_user_de_package_path(uuid_, userId, pkgname) + suffix; 430 if (access(path.c_str(), F_OK) == 0) { 431 if (delete_dir_contents(path) != 0) { 432 res = error("Failed to delete contents of " + path); 433 } 434 } 435 if (!only_cache) { 436 if (!clear_current_profile(pkgname, userId)) { 437 res = error("Failed to clear current profile for " + packageName); 438 } 439 } 440 } 441 return res; 442} 443 444static int destroy_app_reference_profile(const char *pkgname) { 445 return delete_dir_contents_and_dir( 446 create_data_ref_profile_package_path(pkgname), 447 /*ignore_if_missing*/ true); 448} 449 450static int destroy_app_current_profiles(const char *pkgname, userid_t userid) { 451 return delete_dir_contents_and_dir( 452 create_data_user_profile_package_path(userid, pkgname), 453 /*ignore_if_missing*/ true); 454} 455 456binder::Status InstalldNativeService::destroyAppProfiles(const std::string& packageName) { 457 ENFORCE_UID(AID_SYSTEM); 458 CHECK_ARGUMENT_PACKAGE_NAME(packageName); 459 460 const char* pkgname = packageName.c_str(); 461 binder::Status res = ok(); 462 std::vector<userid_t> users = get_known_users(/*volume_uuid*/ nullptr); 463 for (auto user : users) { 464 if (destroy_app_current_profiles(pkgname, user) != 0) { 465 res = error("Failed to destroy current profiles for " + packageName); 466 } 467 } 468 if (destroy_app_reference_profile(pkgname) != 0) { 469 res = error("Failed to destroy reference profile for " + packageName); 470 } 471 return res; 472} 473 474binder::Status InstalldNativeService::destroyAppData(const std::unique_ptr<std::string>& uuid, 475 const std::string& packageName, int32_t userId, int32_t flags, int64_t ceDataInode) { 476 ENFORCE_UID(AID_SYSTEM); 477 CHECK_ARGUMENT_UUID(uuid); 478 CHECK_ARGUMENT_PACKAGE_NAME(packageName); 479 480 const char* uuid_ = uuid ? uuid->c_str() : nullptr; 481 const char* pkgname = packageName.c_str(); 482 483 binder::Status res = ok(); 484 if (flags & FLAG_STORAGE_CE) { 485 auto path = create_data_user_ce_package_path(uuid_, userId, pkgname, ceDataInode); 486 if (delete_dir_contents_and_dir(path) != 0) { 487 res = error("Failed to delete " + path); 488 } 489 } 490 if (flags & FLAG_STORAGE_DE) { 491 auto path = create_data_user_de_package_path(uuid_, userId, pkgname); 492 if (delete_dir_contents_and_dir(path) != 0) { 493 res = error("Failed to delete " + path); 494 } 495 destroy_app_current_profiles(pkgname, userId); 496 // TODO(calin): If the package is still installed by other users it's probably 497 // beneficial to keep the reference profile around. 498 // Verify if it's ok to do that. 499 destroy_app_reference_profile(pkgname); 500 } 501 return res; 502} 503 504binder::Status InstalldNativeService::moveCompleteApp(const std::unique_ptr<std::string>& fromUuid, 505 const std::unique_ptr<std::string>& toUuid, const std::string& packageName, 506 const std::string& dataAppName, int32_t appId, const std::string& seInfo, 507 int32_t targetSdkVersion) { 508 ENFORCE_UID(AID_SYSTEM); 509 CHECK_ARGUMENT_UUID(fromUuid); 510 CHECK_ARGUMENT_UUID(toUuid); 511 CHECK_ARGUMENT_PACKAGE_NAME(packageName); 512 513 const char* from_uuid = fromUuid ? fromUuid->c_str() : nullptr; 514 const char* to_uuid = toUuid ? toUuid->c_str() : nullptr; 515 const char* package_name = packageName.c_str(); 516 const char* data_app_name = dataAppName.c_str(); 517 518 binder::Status res = ok(); 519 std::vector<userid_t> users = get_known_users(from_uuid); 520 521 // Copy app 522 { 523 auto from = create_data_app_package_path(from_uuid, data_app_name); 524 auto to = create_data_app_package_path(to_uuid, data_app_name); 525 auto to_parent = create_data_app_path(to_uuid); 526 527 char *argv[] = { 528 (char*) kCpPath, 529 (char*) "-F", /* delete any existing destination file first (--remove-destination) */ 530 (char*) "-p", /* preserve timestamps, ownership, and permissions */ 531 (char*) "-R", /* recurse into subdirectories (DEST must be a directory) */ 532 (char*) "-P", /* Do not follow symlinks [default] */ 533 (char*) "-d", /* don't dereference symlinks */ 534 (char*) from.c_str(), 535 (char*) to_parent.c_str() 536 }; 537 538 LOG(DEBUG) << "Copying " << from << " to " << to; 539 int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true); 540 if (rc != 0) { 541 res = error(rc, "Failed copying " + from + " to " + to); 542 goto fail; 543 } 544 545 if (selinux_android_restorecon(to.c_str(), SELINUX_ANDROID_RESTORECON_RECURSE) != 0) { 546 res = error("Failed to restorecon " + to); 547 goto fail; 548 } 549 } 550 551 // Copy private data for all known users 552 for (auto user : users) { 553 554 // Data source may not exist for all users; that's okay 555 auto from_ce = create_data_user_ce_package_path(from_uuid, user, package_name); 556 if (access(from_ce.c_str(), F_OK) != 0) { 557 LOG(INFO) << "Missing source " << from_ce; 558 continue; 559 } 560 561 if (!createAppData(toUuid, packageName, user, FLAG_STORAGE_CE | FLAG_STORAGE_DE, appId, 562 seInfo, targetSdkVersion).isOk()) { 563 res = error("Failed to create package target"); 564 goto fail; 565 } 566 567 char *argv[] = { 568 (char*) kCpPath, 569 (char*) "-F", /* delete any existing destination file first (--remove-destination) */ 570 (char*) "-p", /* preserve timestamps, ownership, and permissions */ 571 (char*) "-R", /* recurse into subdirectories (DEST must be a directory) */ 572 (char*) "-P", /* Do not follow symlinks [default] */ 573 (char*) "-d", /* don't dereference symlinks */ 574 nullptr, 575 nullptr 576 }; 577 578 { 579 auto from = create_data_user_de_package_path(from_uuid, user, package_name); 580 auto to = create_data_user_de_path(to_uuid, user); 581 argv[6] = (char*) from.c_str(); 582 argv[7] = (char*) to.c_str(); 583 584 LOG(DEBUG) << "Copying " << from << " to " << to; 585 int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true); 586 if (rc != 0) { 587 res = error(rc, "Failed copying " + from + " to " + to); 588 goto fail; 589 } 590 } 591 { 592 auto from = create_data_user_ce_package_path(from_uuid, user, package_name); 593 auto to = create_data_user_ce_path(to_uuid, user); 594 argv[6] = (char*) from.c_str(); 595 argv[7] = (char*) to.c_str(); 596 597 LOG(DEBUG) << "Copying " << from << " to " << to; 598 int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true); 599 if (rc != 0) { 600 res = error(rc, "Failed copying " + from + " to " + to); 601 goto fail; 602 } 603 } 604 605 if (!restoreconAppData(toUuid, packageName, user, FLAG_STORAGE_CE | FLAG_STORAGE_DE, 606 appId, seInfo).isOk()) { 607 res = error("Failed to restorecon"); 608 goto fail; 609 } 610 } 611 612 // We let the framework scan the new location and persist that before 613 // deleting the data in the old location; this ordering ensures that 614 // we can recover from things like battery pulls. 615 return ok(); 616 617fail: 618 // Nuke everything we might have already copied 619 { 620 auto to = create_data_app_package_path(to_uuid, data_app_name); 621 if (delete_dir_contents(to.c_str(), 1, NULL) != 0) { 622 LOG(WARNING) << "Failed to rollback " << to; 623 } 624 } 625 for (auto user : users) { 626 { 627 auto to = create_data_user_de_package_path(to_uuid, user, package_name); 628 if (delete_dir_contents(to.c_str(), 1, NULL) != 0) { 629 LOG(WARNING) << "Failed to rollback " << to; 630 } 631 } 632 { 633 auto to = create_data_user_ce_package_path(to_uuid, user, package_name); 634 if (delete_dir_contents(to.c_str(), 1, NULL) != 0) { 635 LOG(WARNING) << "Failed to rollback " << to; 636 } 637 } 638 } 639 return res; 640} 641 642binder::Status InstalldNativeService::createUserData(const std::unique_ptr<std::string>& uuid, 643 int32_t userId, int32_t userSerial ATTRIBUTE_UNUSED, int32_t flags) { 644 ENFORCE_UID(AID_SYSTEM); 645 CHECK_ARGUMENT_UUID(uuid); 646 647 const char* uuid_ = uuid ? uuid->c_str() : nullptr; 648 if (flags & FLAG_STORAGE_DE) { 649 if (uuid_ == nullptr) { 650 if (ensure_config_user_dirs(userId) != 0) { 651 return error(StringPrintf("Failed to ensure dirs for %d", userId)); 652 } 653 } 654 } 655 return ok(); 656} 657 658binder::Status InstalldNativeService::destroyUserData(const std::unique_ptr<std::string>& uuid, 659 int32_t userId, int32_t flags) { 660 ENFORCE_UID(AID_SYSTEM); 661 CHECK_ARGUMENT_UUID(uuid); 662 663 const char* uuid_ = uuid ? uuid->c_str() : nullptr; 664 binder::Status res = ok(); 665 if (flags & FLAG_STORAGE_DE) { 666 auto path = create_data_user_de_path(uuid_, userId); 667 if (delete_dir_contents_and_dir(path, true) != 0) { 668 res = error("Failed to delete " + path); 669 } 670 if (uuid_ == nullptr) { 671 path = create_data_misc_legacy_path(userId); 672 if (delete_dir_contents_and_dir(path, true) != 0) { 673 res = error("Failed to delete " + path); 674 } 675 path = create_data_user_profiles_path(userId); 676 if (delete_dir_contents_and_dir(path, true) != 0) { 677 res = error("Failed to delete " + path); 678 } 679 } 680 } 681 if (flags & FLAG_STORAGE_CE) { 682 auto path = create_data_user_ce_path(uuid_, userId); 683 if (delete_dir_contents_and_dir(path, true) != 0) { 684 res = error("Failed to delete " + path); 685 } 686 path = create_data_media_path(uuid_, userId); 687 if (delete_dir_contents_and_dir(path, true) != 0) { 688 res = error("Failed to delete " + path); 689 } 690 } 691 return res; 692} 693 694/* Try to ensure free_size bytes of storage are available. 695 * Returns 0 on success. 696 * This is rather simple-minded because doing a full LRU would 697 * be potentially memory-intensive, and without atime it would 698 * also require that apps constantly modify file metadata even 699 * when just reading from the cache, which is pretty awful. 700 */ 701binder::Status InstalldNativeService::freeCache(const std::unique_ptr<std::string>& uuid, 702 int64_t freeStorageSize) { 703 ENFORCE_UID(AID_SYSTEM); 704 CHECK_ARGUMENT_UUID(uuid); 705 706 const char* uuid_ = uuid ? uuid->c_str() : nullptr; 707 cache_t* cache; 708 int64_t avail; 709 710 auto data_path = create_data_path(uuid_); 711 712 avail = data_disk_free(data_path); 713 if (avail < 0) { 714 return error("Failed to determine free space for " + data_path); 715 } 716 717 ALOGI("free_cache(%" PRId64 ") avail %" PRId64 "\n", freeStorageSize, avail); 718 if (avail >= freeStorageSize) { 719 return ok(); 720 } 721 722 cache = start_cache_collection(); 723 724 auto users = get_known_users(uuid_); 725 for (auto user : users) { 726 add_cache_files(cache, create_data_user_ce_path(uuid_, user)); 727 add_cache_files(cache, create_data_user_de_path(uuid_, user)); 728 add_cache_files(cache, 729 StringPrintf("%s/Android/data", create_data_media_path(uuid_, user).c_str())); 730 } 731 732 clear_cache_files(data_path, cache, freeStorageSize); 733 finish_cache_collection(cache); 734 735 avail = data_disk_free(data_path); 736 if (avail >= freeStorageSize) { 737 return ok(); 738 } else { 739 return error(StringPrintf("Failed to free up %" PRId64 " on %s; final free space %" PRId64, 740 freeStorageSize, data_path.c_str(), avail)); 741 } 742} 743 744binder::Status InstalldNativeService::rmdex(const std::string& codePath, 745 const std::string& instructionSet) { 746 ENFORCE_UID(AID_SYSTEM); 747 char dex_path[PKG_PATH_MAX]; 748 749 const char* path = codePath.c_str(); 750 const char* instruction_set = instructionSet.c_str(); 751 752 if (validate_apk_path(path) && validate_system_app_path(path)) { 753 return error("Invalid path " + codePath); 754 } 755 756 if (!create_cache_path(dex_path, path, instruction_set)) { 757 return error("Failed to create cache path for " + codePath); 758 } 759 760 ALOGV("unlink %s\n", dex_path); 761 if (unlink(dex_path) < 0) { 762 return error(StringPrintf("Failed to unlink %s", dex_path)); 763 } else { 764 return ok(); 765 } 766} 767 768static void add_app_data_size(std::string& path, int64_t *codesize, int64_t *datasize, 769 int64_t *cachesize) { 770 DIR *d; 771 int dfd; 772 struct dirent *de; 773 struct stat s; 774 775 d = opendir(path.c_str()); 776 if (d == nullptr) { 777 PLOG(WARNING) << "Failed to open " << path; 778 return; 779 } 780 dfd = dirfd(d); 781 while ((de = readdir(d))) { 782 const char *name = de->d_name; 783 784 int64_t statsize = 0; 785 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) { 786 statsize = stat_size(&s); 787 } 788 789 if (de->d_type == DT_DIR) { 790 int subfd; 791 int64_t dirsize = 0; 792 /* always skip "." and ".." */ 793 if (name[0] == '.') { 794 if (name[1] == 0) continue; 795 if ((name[1] == '.') && (name[2] == 0)) continue; 796 } 797 subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY); 798 if (subfd >= 0) { 799 dirsize = calculate_dir_size(subfd); 800 close(subfd); 801 } 802 // TODO: check xattrs! 803 if (!strcmp(name, "cache") || !strcmp(name, "code_cache")) { 804 *datasize += statsize; 805 *cachesize += dirsize; 806 } else { 807 *datasize += dirsize + statsize; 808 } 809 } else if (de->d_type == DT_LNK && !strcmp(name, "lib")) { 810 *codesize += statsize; 811 } else { 812 *datasize += statsize; 813 } 814 } 815 closedir(d); 816} 817 818binder::Status InstalldNativeService::getAppSize(const std::unique_ptr<std::string>& uuid, 819 const std::string& packageName, int32_t userId, int32_t flags, int64_t ceDataInode, 820 const std::string& codePath, std::vector<int64_t>* _aidl_return) { 821 ENFORCE_UID(AID_SYSTEM); 822 CHECK_ARGUMENT_UUID(uuid); 823 CHECK_ARGUMENT_PACKAGE_NAME(packageName); 824 825 const char* uuid_ = uuid ? uuid->c_str() : nullptr; 826 const char* pkgname = packageName.c_str(); 827 const char* code_path = codePath.c_str(); 828 829 DIR *d; 830 int dfd; 831 int64_t codesize = 0; 832 int64_t datasize = 0; 833 int64_t cachesize = 0; 834 int64_t asecsize = 0; 835 836 d = opendir(code_path); 837 if (d != nullptr) { 838 dfd = dirfd(d); 839 codesize += calculate_dir_size(dfd); 840 closedir(d); 841 } 842 843 if (flags & FLAG_STORAGE_CE) { 844 auto path = create_data_user_ce_package_path(uuid_, userId, pkgname, ceDataInode); 845 add_app_data_size(path, &codesize, &datasize, &cachesize); 846 } 847 if (flags & FLAG_STORAGE_DE) { 848 auto path = create_data_user_de_package_path(uuid_, userId, pkgname); 849 add_app_data_size(path, &codesize, &datasize, &cachesize); 850 } 851 852 std::vector<int64_t> res; 853 res.push_back(codesize); 854 res.push_back(datasize); 855 res.push_back(cachesize); 856 res.push_back(asecsize); 857 *_aidl_return = res; 858 return ok(); 859} 860 861binder::Status InstalldNativeService::getAppDataInode(const std::unique_ptr<std::string>& uuid, 862 const std::string& packageName, int32_t userId, int32_t flags, int64_t* _aidl_return) { 863 ENFORCE_UID(AID_SYSTEM); 864 CHECK_ARGUMENT_UUID(uuid); 865 CHECK_ARGUMENT_PACKAGE_NAME(packageName); 866 867 const char* uuid_ = uuid ? uuid->c_str() : nullptr; 868 const char* pkgname = packageName.c_str(); 869 870 if (flags & FLAG_STORAGE_CE) { 871 auto path = create_data_user_ce_package_path(uuid_, userId, pkgname); 872 if (get_path_inode(path, reinterpret_cast<ino_t*>(_aidl_return)) == 0) { 873 return ok(); 874 } else { 875 return error("Failed to get_path_inode for " + path); 876 } 877 } 878 return exception(binder::Status::EX_UNSUPPORTED_OPERATION); 879} 880 881// Dumps the contents of a profile file, using pkgname's dex files for pretty 882// printing the result. 883binder::Status InstalldNativeService::dumpProfiles(int32_t uid, const std::string& packageName, 884 const std::string& codePaths, bool* _aidl_return) { 885 ENFORCE_UID(AID_SYSTEM); 886 CHECK_ARGUMENT_PACKAGE_NAME(packageName); 887 888 const char* pkgname = packageName.c_str(); 889 const char* code_paths = codePaths.c_str(); 890 891 *_aidl_return = dump_profiles(uid, pkgname, code_paths); 892 return ok(); 893} 894 895// TODO: Consider returning error codes. 896binder::Status InstalldNativeService::mergeProfiles(int32_t uid, const std::string& packageName, 897 bool* _aidl_return) { 898 ENFORCE_UID(AID_SYSTEM); 899 CHECK_ARGUMENT_PACKAGE_NAME(packageName); 900 901 const char* pkgname = packageName.c_str(); 902 *_aidl_return = analyse_profiles(uid, pkgname); 903 return ok(); 904} 905 906binder::Status InstalldNativeService::dexopt(const std::string& apkPath, int32_t uid, 907 const std::unique_ptr<std::string>& packageName, const std::string& instructionSet, 908 int32_t dexoptNeeded, const std::unique_ptr<std::string>& outputPath, int32_t dexFlags, 909 const std::string& compilerFilter, const std::unique_ptr<std::string>& uuid, 910 const std::unique_ptr<std::string>& sharedLibraries) { 911 ENFORCE_UID(AID_SYSTEM); 912 CHECK_ARGUMENT_UUID(uuid); 913 if (packageName && *packageName != "*") { 914 CHECK_ARGUMENT_PACKAGE_NAME(*packageName); 915 } 916 917 const char* apk_path = apkPath.c_str(); 918 const char* pkgname = packageName ? packageName->c_str() : "*"; 919 const char* instruction_set = instructionSet.c_str(); 920 const char* oat_dir = outputPath ? outputPath->c_str() : nullptr; 921 const char* compiler_filter = compilerFilter.c_str(); 922 const char* volume_uuid = uuid ? uuid->c_str() : nullptr; 923 const char* shared_libraries = sharedLibraries ? sharedLibraries->c_str() : nullptr; 924 925 int res = android::installd::dexopt(apk_path, uid, pkgname, instruction_set, dexoptNeeded, 926 oat_dir, dexFlags, compiler_filter, volume_uuid, shared_libraries); 927 return res ? error(res, "Failed to dexopt") : ok(); 928} 929 930binder::Status InstalldNativeService::markBootComplete(const std::string& instructionSet) { 931 ENFORCE_UID(AID_SYSTEM); 932 const char* instruction_set = instructionSet.c_str(); 933 934 char boot_marker_path[PKG_PATH_MAX]; 935 sprintf(boot_marker_path, 936 "%s/%s/%s/.booting", 937 android_data_dir.path, 938 DALVIK_CACHE, 939 instruction_set); 940 941 ALOGV("mark_boot_complete : %s", boot_marker_path); 942 if (unlink(boot_marker_path) != 0) { 943 return error(StringPrintf("Failed to unlink %s", boot_marker_path)); 944 } 945 return ok(); 946} 947 948void mkinnerdirs(char* path, int basepos, mode_t mode, int uid, int gid, 949 struct stat* statbuf) 950{ 951 while (path[basepos] != 0) { 952 if (path[basepos] == '/') { 953 path[basepos] = 0; 954 if (lstat(path, statbuf) < 0) { 955 ALOGV("Making directory: %s\n", path); 956 if (mkdir(path, mode) == 0) { 957 chown(path, uid, gid); 958 } else { 959 ALOGW("Unable to make directory %s: %s\n", path, strerror(errno)); 960 } 961 } 962 path[basepos] = '/'; 963 basepos++; 964 } 965 basepos++; 966 } 967} 968 969binder::Status InstalldNativeService::linkNativeLibraryDirectory( 970 const std::unique_ptr<std::string>& uuid, const std::string& packageName, 971 const std::string& nativeLibPath32, int32_t userId) { 972 ENFORCE_UID(AID_SYSTEM); 973 CHECK_ARGUMENT_UUID(uuid); 974 CHECK_ARGUMENT_PACKAGE_NAME(packageName); 975 976 const char* uuid_ = uuid ? uuid->c_str() : nullptr; 977 const char* pkgname = packageName.c_str(); 978 const char* asecLibDir = nativeLibPath32.c_str(); 979 struct stat s, libStat; 980 binder::Status res = ok(); 981 982 auto _pkgdir = create_data_user_ce_package_path(uuid_, userId, pkgname); 983 auto _libsymlink = _pkgdir + PKG_LIB_POSTFIX; 984 985 const char* pkgdir = _pkgdir.c_str(); 986 const char* libsymlink = _libsymlink.c_str(); 987 988 if (stat(pkgdir, &s) < 0) { 989 return error("Failed to stat " + _pkgdir); 990 } 991 992 if (chown(pkgdir, AID_INSTALL, AID_INSTALL) < 0) { 993 return error("Failed to chown " + _pkgdir); 994 } 995 996 if (chmod(pkgdir, 0700) < 0) { 997 res = error("Failed to chmod " + _pkgdir); 998 goto out; 999 } 1000 1001 if (lstat(libsymlink, &libStat) < 0) { 1002 if (errno != ENOENT) { 1003 res = error("Failed to stat " + _libsymlink); 1004 goto out; 1005 } 1006 } else { 1007 if (S_ISDIR(libStat.st_mode)) { 1008 if (delete_dir_contents(libsymlink, 1, NULL) < 0) { 1009 res = error("Failed to delete " + _libsymlink); 1010 goto out; 1011 } 1012 } else if (S_ISLNK(libStat.st_mode)) { 1013 if (unlink(libsymlink) < 0) { 1014 res = error("Failed to unlink " + _libsymlink); 1015 goto out; 1016 } 1017 } 1018 } 1019 1020 if (symlink(asecLibDir, libsymlink) < 0) { 1021 res = error("Failed to symlink " + _libsymlink + " to " + nativeLibPath32); 1022 goto out; 1023 } 1024 1025out: 1026 if (chmod(pkgdir, s.st_mode) < 0) { 1027 auto msg = "Failed to cleanup chmod " + _pkgdir; 1028 if (res.isOk()) { 1029 res = error(msg); 1030 } else { 1031 PLOG(ERROR) << msg; 1032 } 1033 } 1034 1035 if (chown(pkgdir, s.st_uid, s.st_gid) < 0) { 1036 auto msg = "Failed to cleanup chown " + _pkgdir; 1037 if (res.isOk()) { 1038 res = error(msg); 1039 } else { 1040 PLOG(ERROR) << msg; 1041 } 1042 } 1043 1044 return res; 1045} 1046 1047static void run_idmap(const char *target_apk, const char *overlay_apk, int idmap_fd) 1048{ 1049 static const char *IDMAP_BIN = "/system/bin/idmap"; 1050 static const size_t MAX_INT_LEN = 32; 1051 char idmap_str[MAX_INT_LEN]; 1052 1053 snprintf(idmap_str, sizeof(idmap_str), "%d", idmap_fd); 1054 1055 execl(IDMAP_BIN, IDMAP_BIN, "--fd", target_apk, overlay_apk, idmap_str, (char*)NULL); 1056 ALOGE("execl(%s) failed: %s\n", IDMAP_BIN, strerror(errno)); 1057} 1058 1059// Transform string /a/b/c.apk to (prefix)/a@b@c.apk@(suffix) 1060// eg /a/b/c.apk to /data/resource-cache/a@b@c.apk@idmap 1061static int flatten_path(const char *prefix, const char *suffix, 1062 const char *overlay_path, char *idmap_path, size_t N) 1063{ 1064 if (overlay_path == NULL || idmap_path == NULL) { 1065 return -1; 1066 } 1067 const size_t len_overlay_path = strlen(overlay_path); 1068 // will access overlay_path + 1 further below; requires absolute path 1069 if (len_overlay_path < 2 || *overlay_path != '/') { 1070 return -1; 1071 } 1072 const size_t len_idmap_root = strlen(prefix); 1073 const size_t len_suffix = strlen(suffix); 1074 if (SIZE_MAX - len_idmap_root < len_overlay_path || 1075 SIZE_MAX - (len_idmap_root + len_overlay_path) < len_suffix) { 1076 // additions below would cause overflow 1077 return -1; 1078 } 1079 if (N < len_idmap_root + len_overlay_path + len_suffix) { 1080 return -1; 1081 } 1082 memset(idmap_path, 0, N); 1083 snprintf(idmap_path, N, "%s%s%s", prefix, overlay_path + 1, suffix); 1084 char *ch = idmap_path + len_idmap_root; 1085 while (*ch != '\0') { 1086 if (*ch == '/') { 1087 *ch = '@'; 1088 } 1089 ++ch; 1090 } 1091 return 0; 1092} 1093 1094binder::Status InstalldNativeService::idmap(const std::string& targetApkPath, 1095 const std::string& overlayApkPath, int32_t uid) { 1096 ENFORCE_UID(AID_SYSTEM); 1097 const char* target_apk = targetApkPath.c_str(); 1098 const char* overlay_apk = overlayApkPath.c_str(); 1099 ALOGV("idmap target_apk=%s overlay_apk=%s uid=%d\n", target_apk, overlay_apk, uid); 1100 1101 int idmap_fd = -1; 1102 char idmap_path[PATH_MAX]; 1103 1104 if (flatten_path(IDMAP_PREFIX, IDMAP_SUFFIX, overlay_apk, 1105 idmap_path, sizeof(idmap_path)) == -1) { 1106 ALOGE("idmap cannot generate idmap path for overlay %s\n", overlay_apk); 1107 goto fail; 1108 } 1109 1110 unlink(idmap_path); 1111 idmap_fd = open(idmap_path, O_RDWR | O_CREAT | O_EXCL, 0644); 1112 if (idmap_fd < 0) { 1113 ALOGE("idmap cannot open '%s' for output: %s\n", idmap_path, strerror(errno)); 1114 goto fail; 1115 } 1116 if (fchown(idmap_fd, AID_SYSTEM, uid) < 0) { 1117 ALOGE("idmap cannot chown '%s'\n", idmap_path); 1118 goto fail; 1119 } 1120 if (fchmod(idmap_fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0) { 1121 ALOGE("idmap cannot chmod '%s'\n", idmap_path); 1122 goto fail; 1123 } 1124 1125 pid_t pid; 1126 pid = fork(); 1127 if (pid == 0) { 1128 /* child -- drop privileges before continuing */ 1129 if (setgid(uid) != 0) { 1130 ALOGE("setgid(%d) failed during idmap\n", uid); 1131 exit(1); 1132 } 1133 if (setuid(uid) != 0) { 1134 ALOGE("setuid(%d) failed during idmap\n", uid); 1135 exit(1); 1136 } 1137 if (flock(idmap_fd, LOCK_EX | LOCK_NB) != 0) { 1138 ALOGE("flock(%s) failed during idmap: %s\n", idmap_path, strerror(errno)); 1139 exit(1); 1140 } 1141 1142 run_idmap(target_apk, overlay_apk, idmap_fd); 1143 exit(1); /* only if exec call to idmap failed */ 1144 } else { 1145 int status = wait_child(pid); 1146 if (status != 0) { 1147 ALOGE("idmap failed, status=0x%04x\n", status); 1148 goto fail; 1149 } 1150 } 1151 1152 close(idmap_fd); 1153 return ok(); 1154fail: 1155 if (idmap_fd >= 0) { 1156 close(idmap_fd); 1157 unlink(idmap_path); 1158 } 1159 return error(); 1160} 1161 1162binder::Status InstalldNativeService::restoreconAppData(const std::unique_ptr<std::string>& uuid, 1163 const std::string& packageName, int32_t userId, int32_t flags, int32_t appId, 1164 const std::string& seInfo) { 1165 ENFORCE_UID(AID_SYSTEM); 1166 CHECK_ARGUMENT_UUID(uuid); 1167 CHECK_ARGUMENT_PACKAGE_NAME(packageName); 1168 1169 binder::Status res = ok(); 1170 1171 // SELINUX_ANDROID_RESTORECON_DATADATA flag is set by libselinux. Not needed here. 1172 unsigned int seflags = SELINUX_ANDROID_RESTORECON_RECURSE; 1173 const char* uuid_ = uuid ? uuid->c_str() : nullptr; 1174 const char* pkgName = packageName.c_str(); 1175 const char* seinfo = seInfo.c_str(); 1176 1177 uid_t uid = multiuser_get_uid(userId, appId); 1178 if (flags & FLAG_STORAGE_CE) { 1179 auto path = create_data_user_ce_package_path(uuid_, userId, pkgName); 1180 if (selinux_android_restorecon_pkgdir(path.c_str(), seinfo, uid, seflags) < 0) { 1181 res = error("restorecon failed for " + path); 1182 } 1183 } 1184 if (flags & FLAG_STORAGE_DE) { 1185 auto path = create_data_user_de_package_path(uuid_, userId, pkgName); 1186 if (selinux_android_restorecon_pkgdir(path.c_str(), seinfo, uid, seflags) < 0) { 1187 res = error("restorecon failed for " + path); 1188 } 1189 } 1190 return res; 1191} 1192 1193binder::Status InstalldNativeService::createOatDir(const std::string& oatDir, 1194 const std::string& instructionSet) { 1195 ENFORCE_UID(AID_SYSTEM); 1196 const char* oat_dir = oatDir.c_str(); 1197 const char* instruction_set = instructionSet.c_str(); 1198 char oat_instr_dir[PKG_PATH_MAX]; 1199 1200 if (validate_apk_path(oat_dir)) { 1201 return error("Invalid path " + oatDir); 1202 } 1203 if (fs_prepare_dir(oat_dir, S_IRWXU | S_IRWXG | S_IXOTH, AID_SYSTEM, AID_INSTALL)) { 1204 return error("Failed to prepare " + oatDir); 1205 } 1206 if (selinux_android_restorecon(oat_dir, 0)) { 1207 return error("Failed to restorecon " + oatDir); 1208 } 1209 snprintf(oat_instr_dir, PKG_PATH_MAX, "%s/%s", oat_dir, instruction_set); 1210 if (fs_prepare_dir(oat_instr_dir, S_IRWXU | S_IRWXG | S_IXOTH, AID_SYSTEM, AID_INSTALL)) { 1211 return error(StringPrintf("Failed to prepare %s", oat_instr_dir)); 1212 } 1213 return ok(); 1214} 1215 1216binder::Status InstalldNativeService::rmPackageDir(const std::string& packageDir) { 1217 ENFORCE_UID(AID_SYSTEM); 1218 if (validate_apk_path(packageDir.c_str())) { 1219 return error("Invalid path " + packageDir); 1220 } 1221 if (delete_dir_contents_and_dir(packageDir) != 0) { 1222 return error("Failed to delete " + packageDir); 1223 } 1224 return ok(); 1225} 1226 1227binder::Status InstalldNativeService::linkFile(const std::string& relativePath, 1228 const std::string& fromBase, const std::string& toBase) { 1229 ENFORCE_UID(AID_SYSTEM); 1230 const char* relative_path = relativePath.c_str(); 1231 const char* from_base = fromBase.c_str(); 1232 const char* to_base = toBase.c_str(); 1233 char from_path[PKG_PATH_MAX]; 1234 char to_path[PKG_PATH_MAX]; 1235 snprintf(from_path, PKG_PATH_MAX, "%s/%s", from_base, relative_path); 1236 snprintf(to_path, PKG_PATH_MAX, "%s/%s", to_base, relative_path); 1237 1238 if (validate_apk_path_subdirs(from_path)) { 1239 return error(StringPrintf("Invalid from path %s", from_path)); 1240 } 1241 1242 if (validate_apk_path_subdirs(to_path)) { 1243 return error(StringPrintf("Invalid to path %s", to_path)); 1244 } 1245 1246 if (link(from_path, to_path) < 0) { 1247 return error(StringPrintf("Failed to link from %s to %s", from_path, to_path)); 1248 } 1249 1250 return ok(); 1251} 1252 1253binder::Status InstalldNativeService::moveAb(const std::string& apkPath, 1254 const std::string& instructionSet, const std::string& outputPath) { 1255 ENFORCE_UID(AID_SYSTEM); 1256 1257 const char* apk_path = apkPath.c_str(); 1258 const char* instruction_set = instructionSet.c_str(); 1259 const char* oat_dir = outputPath.c_str(); 1260 1261 bool success = move_ab(apk_path, instruction_set, oat_dir); 1262 return success ? ok() : error(); 1263} 1264 1265binder::Status InstalldNativeService::deleteOdex(const std::string& apkPath, 1266 const std::string& instructionSet, const std::string& outputPath) { 1267 ENFORCE_UID(AID_SYSTEM); 1268 1269 const char* apk_path = apkPath.c_str(); 1270 const char* instruction_set = instructionSet.c_str(); 1271 const char* oat_dir = outputPath.c_str(); 1272 1273 bool res = delete_odex(apk_path, instruction_set, oat_dir); 1274 return res ? ok() : error(); 1275} 1276 1277} // namespace installd 1278} // namespace android 1279