InstalldNativeService.cpp revision 7d9483a508029826b69b3bac437754d7a9d993f6
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#define ATRACE_TAG ATRACE_TAG_PACKAGE_MANAGER 20 21#include <errno.h> 22#include <inttypes.h> 23#include <fstream> 24#include <fts.h> 25#include <regex> 26#include <stdlib.h> 27#include <string.h> 28#include <sys/capability.h> 29#include <sys/file.h> 30#include <sys/resource.h> 31#include <sys/quota.h> 32#include <sys/stat.h> 33#include <sys/statvfs.h> 34#include <sys/types.h> 35#include <sys/wait.h> 36#include <sys/xattr.h> 37#include <unistd.h> 38 39#include <android-base/logging.h> 40#include <android-base/stringprintf.h> 41#include <android-base/strings.h> 42#include <android-base/unique_fd.h> 43#include <cutils/fs.h> 44#include <cutils/properties.h> 45#include <cutils/sched_policy.h> 46#include <log/log.h> // TODO: Move everything to base/logging. 47#include <logwrap/logwrap.h> 48#include <private/android_filesystem_config.h> 49#include <selinux/android.h> 50#include <system/thread_defs.h> 51#include <utils/Trace.h> 52 53#include "dexopt.h" 54#include "globals.h" 55#include "installd_deps.h" 56#include "otapreopt_utils.h" 57#include "utils.h" 58 59#include "MatchExtensionGen.h" 60 61#ifndef LOG_TAG 62#define LOG_TAG "installd" 63#endif 64 65using android::base::StringPrintf; 66using std::endl; 67 68namespace android { 69namespace installd { 70 71static constexpr const char* kCpPath = "/system/bin/cp"; 72static constexpr const char* kXattrDefault = "user.default"; 73 74static constexpr const int MIN_RESTRICTED_HOME_SDK_VERSION = 24; // > M 75 76static constexpr const char* PKG_LIB_POSTFIX = "/lib"; 77static constexpr const char* CACHE_DIR_POSTFIX = "/cache"; 78static constexpr const char* CODE_CACHE_DIR_POSTFIX = "/code_cache"; 79 80static constexpr const char* IDMAP_PREFIX = "/data/resource-cache/"; 81static constexpr const char* IDMAP_SUFFIX = "@idmap"; 82 83// NOTE: keep in sync with StorageManager 84static constexpr int FLAG_STORAGE_DE = 1 << 0; 85static constexpr int FLAG_STORAGE_CE = 1 << 1; 86 87// NOTE: keep in sync with Installer 88static constexpr int FLAG_CLEAR_CACHE_ONLY = 1 << 8; 89static constexpr int FLAG_CLEAR_CODE_CACHE_ONLY = 1 << 9; 90static constexpr int FLAG_USE_QUOTA = 1 << 12; 91 92namespace { 93 94constexpr const char* kDump = "android.permission.DUMP"; 95 96static binder::Status ok() { 97 return binder::Status::ok(); 98} 99 100static binder::Status exception(uint32_t code, const std::string& msg) { 101 return binder::Status::fromExceptionCode(code, String8(msg.c_str())); 102} 103 104static binder::Status error() { 105 return binder::Status::fromServiceSpecificError(errno); 106} 107 108static binder::Status error(const std::string& msg) { 109 PLOG(ERROR) << msg; 110 return binder::Status::fromServiceSpecificError(errno, String8(msg.c_str())); 111} 112 113static binder::Status error(uint32_t code, const std::string& msg) { 114 LOG(ERROR) << msg << " (" << code << ")"; 115 return binder::Status::fromServiceSpecificError(code, String8(msg.c_str())); 116} 117 118binder::Status checkPermission(const char* permission) { 119 pid_t pid; 120 uid_t uid; 121 122 if (checkCallingPermission(String16(permission), reinterpret_cast<int32_t*>(&pid), 123 reinterpret_cast<int32_t*>(&uid))) { 124 return ok(); 125 } else { 126 return exception(binder::Status::EX_SECURITY, 127 StringPrintf("UID %d / PID %d lacks permission %s", uid, pid, permission)); 128 } 129} 130 131binder::Status checkUid(uid_t expectedUid) { 132 uid_t uid = IPCThreadState::self()->getCallingUid(); 133 if (uid == expectedUid || uid == AID_ROOT) { 134 return ok(); 135 } else { 136 return exception(binder::Status::EX_SECURITY, 137 StringPrintf("UID %d is not expected UID %d", uid, expectedUid)); 138 } 139} 140 141binder::Status checkArgumentUuid(const std::unique_ptr<std::string>& uuid) { 142 if (!uuid || is_valid_filename(*uuid)) { 143 return ok(); 144 } else { 145 return exception(binder::Status::EX_ILLEGAL_ARGUMENT, 146 StringPrintf("UUID %s is malformed", uuid->c_str())); 147 } 148} 149 150binder::Status checkArgumentPackageName(const std::string& packageName) { 151 if (is_valid_package_name(packageName.c_str())) { 152 return ok(); 153 } else { 154 return exception(binder::Status::EX_ILLEGAL_ARGUMENT, 155 StringPrintf("Package name %s is malformed", packageName.c_str())); 156 } 157} 158 159#define ENFORCE_UID(uid) { \ 160 binder::Status status = checkUid((uid)); \ 161 if (!status.isOk()) { \ 162 return status; \ 163 } \ 164} 165 166#define CHECK_ARGUMENT_UUID(uuid) { \ 167 binder::Status status = checkArgumentUuid((uuid)); \ 168 if (!status.isOk()) { \ 169 return status; \ 170 } \ 171} 172 173#define CHECK_ARGUMENT_PACKAGE_NAME(packageName) { \ 174 binder::Status status = \ 175 checkArgumentPackageName((packageName)); \ 176 if (!status.isOk()) { \ 177 return status; \ 178 } \ 179} 180 181} // namespace 182 183status_t InstalldNativeService::start() { 184 IPCThreadState::self()->disableBackgroundScheduling(true); 185 status_t ret = BinderService<InstalldNativeService>::publish(); 186 if (ret != android::OK) { 187 return ret; 188 } 189 sp<ProcessState> ps(ProcessState::self()); 190 ps->startThreadPool(); 191 ps->giveThreadPoolName(); 192 return android::OK; 193} 194 195status_t InstalldNativeService::dump(int fd, const Vector<String16> & /* args */) { 196 auto out = std::fstream(StringPrintf("/proc/self/fd/%d", fd)); 197 const binder::Status dump_permission = checkPermission(kDump); 198 if (!dump_permission.isOk()) { 199 out << dump_permission.toString8() << endl; 200 return PERMISSION_DENIED; 201 } 202 std::lock_guard<std::recursive_mutex> lock(mLock); 203 204 out << "installd is happy!" << endl << endl; 205 out << "Devices with quota support:" << endl; 206 for (const auto& n : mQuotaDevices) { 207 out << " " << n.first << " = " << n.second << endl; 208 } 209 out << endl; 210 out.flush(); 211 212 return NO_ERROR; 213} 214 215/** 216 * Perform restorecon of the given path, but only perform recursive restorecon 217 * if the label of that top-level file actually changed. This can save us 218 * significant time by avoiding no-op traversals of large filesystem trees. 219 */ 220static int restorecon_app_data_lazy(const std::string& path, const std::string& seInfo, uid_t uid, 221 bool existing) { 222 int res = 0; 223 char* before = nullptr; 224 char* after = nullptr; 225 226 // Note that SELINUX_ANDROID_RESTORECON_DATADATA flag is set by 227 // libselinux. Not needed here. 228 229 if (lgetfilecon(path.c_str(), &before) < 0) { 230 PLOG(ERROR) << "Failed before getfilecon for " << path; 231 goto fail; 232 } 233 if (selinux_android_restorecon_pkgdir(path.c_str(), seInfo.c_str(), uid, 0) < 0) { 234 PLOG(ERROR) << "Failed top-level restorecon for " << path; 235 goto fail; 236 } 237 if (lgetfilecon(path.c_str(), &after) < 0) { 238 PLOG(ERROR) << "Failed after getfilecon for " << path; 239 goto fail; 240 } 241 242 // If the initial top-level restorecon above changed the label, then go 243 // back and restorecon everything recursively 244 if (strcmp(before, after)) { 245 if (existing) { 246 LOG(DEBUG) << "Detected label change from " << before << " to " << after << " at " 247 << path << "; running recursive restorecon"; 248 } 249 if (selinux_android_restorecon_pkgdir(path.c_str(), seInfo.c_str(), uid, 250 SELINUX_ANDROID_RESTORECON_RECURSE) < 0) { 251 PLOG(ERROR) << "Failed recursive restorecon for " << path; 252 goto fail; 253 } 254 } 255 256 goto done; 257fail: 258 res = -1; 259done: 260 free(before); 261 free(after); 262 return res; 263} 264 265static int restorecon_app_data_lazy(const std::string& parent, const char* name, 266 const std::string& seInfo, uid_t uid, bool existing) { 267 return restorecon_app_data_lazy(StringPrintf("%s/%s", parent.c_str(), name), seInfo, uid, 268 existing); 269} 270 271static int prepare_app_dir(const std::string& path, mode_t target_mode, uid_t uid) { 272 if (fs_prepare_dir_strict(path.c_str(), target_mode, uid, uid) != 0) { 273 PLOG(ERROR) << "Failed to prepare " << path; 274 return -1; 275 } 276 return 0; 277} 278 279/** 280 * Prepare an app cache directory, which offers to fix-up the GID and 281 * directory mode flags during a platform upgrade. 282 */ 283static int prepare_app_cache_dir(const std::string& parent, const char* name, mode_t target_mode, 284 uid_t uid, gid_t gid) { 285 auto path = StringPrintf("%s/%s", parent.c_str(), name); 286 struct stat st; 287 if (stat(path.c_str(), &st) != 0) { 288 if (errno == ENOENT) { 289 // This is fine, just create it 290 if (fs_prepare_dir_strict(path.c_str(), target_mode, uid, gid) != 0) { 291 PLOG(ERROR) << "Failed to prepare " << path; 292 return -1; 293 } else { 294 return 0; 295 } 296 } else { 297 PLOG(ERROR) << "Failed to stat " << path; 298 return -1; 299 } 300 } 301 302 mode_t actual_mode = st.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO | S_ISGID); 303 if (st.st_uid != uid) { 304 // Mismatched UID is real trouble; we can't recover 305 LOG(ERROR) << "Mismatched UID at " << path << ": found " << st.st_uid 306 << " but expected " << uid; 307 return -1; 308 } else if (st.st_gid == gid && actual_mode == target_mode) { 309 // Everything looks good! 310 return 0; 311 } 312 313 // Directory is owned correctly, but GID or mode mismatch means it's 314 // probably a platform upgrade so we need to fix them 315 FTS *fts; 316 FTSENT *p; 317 char *argv[] = { (char*) path.c_str(), nullptr }; 318 if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_XDEV, NULL))) { 319 PLOG(ERROR) << "Failed to fts_open " << path; 320 return -1; 321 } 322 while ((p = fts_read(fts)) != NULL) { 323 switch (p->fts_info) { 324 case FTS_DP: 325 if (chmod(p->fts_accpath, target_mode) != 0) { 326 PLOG(WARNING) << "Failed to chmod " << p->fts_path; 327 } 328 // Intentional fall through to also set GID 329 case FTS_F: 330 if (chown(p->fts_accpath, -1, gid) != 0) { 331 PLOG(WARNING) << "Failed to chown " << p->fts_path; 332 } 333 break; 334 case FTS_SL: 335 case FTS_SLNONE: 336 if (lchown(p->fts_accpath, -1, gid) != 0) { 337 PLOG(WARNING) << "Failed to chown " << p->fts_path; 338 } 339 break; 340 } 341 } 342 fts_close(fts); 343 return 0; 344} 345 346binder::Status InstalldNativeService::createAppData(const std::unique_ptr<std::string>& uuid, 347 const std::string& packageName, int32_t userId, int32_t flags, int32_t appId, 348 const std::string& seInfo, int32_t targetSdkVersion, int64_t* _aidl_return) { 349 ENFORCE_UID(AID_SYSTEM); 350 CHECK_ARGUMENT_UUID(uuid); 351 CHECK_ARGUMENT_PACKAGE_NAME(packageName); 352 std::lock_guard<std::recursive_mutex> lock(mLock); 353 354 const char* uuid_ = uuid ? uuid->c_str() : nullptr; 355 const char* pkgname = packageName.c_str(); 356 357 // Assume invalid inode unless filled in below 358 if (_aidl_return != nullptr) *_aidl_return = -1; 359 360 int32_t uid = multiuser_get_uid(userId, appId); 361 int32_t cacheGid = multiuser_get_cache_gid(userId, appId); 362 mode_t targetMode = targetSdkVersion >= MIN_RESTRICTED_HOME_SDK_VERSION ? 0700 : 0751; 363 364 // If UID doesn't have a specific cache GID, use UID value 365 if (cacheGid == -1) { 366 cacheGid = uid; 367 } 368 369 if (flags & FLAG_STORAGE_CE) { 370 auto path = create_data_user_ce_package_path(uuid_, userId, pkgname); 371 bool existing = (access(path.c_str(), F_OK) == 0); 372 373 if (prepare_app_dir(path, targetMode, uid) || 374 prepare_app_cache_dir(path, "cache", 02771, uid, cacheGid) || 375 prepare_app_cache_dir(path, "code_cache", 02771, uid, cacheGid)) { 376 return error("Failed to prepare " + path); 377 } 378 379 // Consider restorecon over contents if label changed 380 if (restorecon_app_data_lazy(path, seInfo, uid, existing) || 381 restorecon_app_data_lazy(path, "cache", seInfo, uid, existing) || 382 restorecon_app_data_lazy(path, "code_cache", seInfo, uid, existing)) { 383 return error("Failed to restorecon " + path); 384 } 385 386 // Remember inode numbers of cache directories so that we can clear 387 // contents while CE storage is locked 388 if (write_path_inode(path, "cache", kXattrInodeCache) || 389 write_path_inode(path, "code_cache", kXattrInodeCodeCache)) { 390 return error("Failed to write_path_inode for " + path); 391 } 392 393 // And return the CE inode of the top-level data directory so we can 394 // clear contents while CE storage is locked 395 if ((_aidl_return != nullptr) 396 && get_path_inode(path, reinterpret_cast<ino_t*>(_aidl_return)) != 0) { 397 return error("Failed to get_path_inode for " + path); 398 } 399 } 400 if (flags & FLAG_STORAGE_DE) { 401 auto path = create_data_user_de_package_path(uuid_, userId, pkgname); 402 bool existing = (access(path.c_str(), F_OK) == 0); 403 404 if (prepare_app_dir(path, targetMode, uid) || 405 prepare_app_cache_dir(path, "cache", 02771, uid, cacheGid) || 406 prepare_app_cache_dir(path, "code_cache", 02771, uid, cacheGid)) { 407 return error("Failed to prepare " + path); 408 } 409 410 // Consider restorecon over contents if label changed 411 if (restorecon_app_data_lazy(path, seInfo, uid, existing)) { 412 return error("Failed to restorecon " + path); 413 } 414 415 if (property_get_bool("dalvik.vm.usejitprofiles", false)) { 416 const std::string profile_path = create_data_user_profile_package_path(userId, pkgname); 417 // read-write-execute only for the app user. 418 if (fs_prepare_dir_strict(profile_path.c_str(), 0700, uid, uid) != 0) { 419 return error("Failed to prepare " + profile_path); 420 } 421 std::string profile_file = create_primary_profile(profile_path); 422 // read-write only for the app user. 423 if (fs_prepare_file_strict(profile_file.c_str(), 0600, uid, uid) != 0) { 424 return error("Failed to prepare " + profile_path); 425 } 426 const std::string ref_profile_path = create_data_ref_profile_package_path(pkgname); 427 // dex2oat/profman runs under the shared app gid and it needs to read/write reference 428 // profiles. 429 int shared_app_gid = multiuser_get_shared_gid(0, appId); 430 if ((shared_app_gid != -1) && fs_prepare_dir_strict( 431 ref_profile_path.c_str(), 0700, shared_app_gid, shared_app_gid) != 0) { 432 return error("Failed to prepare " + ref_profile_path); 433 } 434 } 435 } 436 return ok(); 437} 438 439binder::Status InstalldNativeService::migrateAppData(const std::unique_ptr<std::string>& uuid, 440 const std::string& packageName, int32_t userId, int32_t flags) { 441 ENFORCE_UID(AID_SYSTEM); 442 CHECK_ARGUMENT_UUID(uuid); 443 CHECK_ARGUMENT_PACKAGE_NAME(packageName); 444 std::lock_guard<std::recursive_mutex> lock(mLock); 445 446 const char* uuid_ = uuid ? uuid->c_str() : nullptr; 447 const char* pkgname = packageName.c_str(); 448 449 // This method only exists to upgrade system apps that have requested 450 // forceDeviceEncrypted, so their default storage always lives in a 451 // consistent location. This only works on non-FBE devices, since we 452 // never want to risk exposing data on a device with real CE/DE storage. 453 454 auto ce_path = create_data_user_ce_package_path(uuid_, userId, pkgname); 455 auto de_path = create_data_user_de_package_path(uuid_, userId, pkgname); 456 457 // If neither directory is marked as default, assume CE is default 458 if (getxattr(ce_path.c_str(), kXattrDefault, nullptr, 0) == -1 459 && getxattr(de_path.c_str(), kXattrDefault, nullptr, 0) == -1) { 460 if (setxattr(ce_path.c_str(), kXattrDefault, nullptr, 0, 0) != 0) { 461 return error("Failed to mark default storage " + ce_path); 462 } 463 } 464 465 // Migrate default data location if needed 466 auto target = (flags & FLAG_STORAGE_DE) ? de_path : ce_path; 467 auto source = (flags & FLAG_STORAGE_DE) ? ce_path : de_path; 468 469 if (getxattr(target.c_str(), kXattrDefault, nullptr, 0) == -1) { 470 LOG(WARNING) << "Requested default storage " << target 471 << " is not active; migrating from " << source; 472 if (delete_dir_contents_and_dir(target) != 0) { 473 return error("Failed to delete " + target); 474 } 475 if (rename(source.c_str(), target.c_str()) != 0) { 476 return error("Failed to rename " + source + " to " + target); 477 } 478 } 479 480 return ok(); 481} 482 483 484binder::Status InstalldNativeService::clearAppProfiles(const std::string& packageName) { 485 ENFORCE_UID(AID_SYSTEM); 486 CHECK_ARGUMENT_PACKAGE_NAME(packageName); 487 std::lock_guard<std::recursive_mutex> lock(mLock); 488 489 const char* pkgname = packageName.c_str(); 490 binder::Status res = ok(); 491 if (!clear_reference_profile(pkgname)) { 492 res = error("Failed to clear reference profile for " + packageName); 493 } 494 if (!clear_current_profiles(pkgname)) { 495 res = error("Failed to clear current profiles for " + packageName); 496 } 497 return res; 498} 499 500binder::Status InstalldNativeService::clearAppData(const std::unique_ptr<std::string>& uuid, 501 const std::string& packageName, int32_t userId, int32_t flags, int64_t ceDataInode) { 502 ENFORCE_UID(AID_SYSTEM); 503 CHECK_ARGUMENT_UUID(uuid); 504 CHECK_ARGUMENT_PACKAGE_NAME(packageName); 505 std::lock_guard<std::recursive_mutex> lock(mLock); 506 507 const char* uuid_ = uuid ? uuid->c_str() : nullptr; 508 const char* pkgname = packageName.c_str(); 509 510 binder::Status res = ok(); 511 if (flags & FLAG_STORAGE_CE) { 512 auto path = create_data_user_ce_package_path(uuid_, userId, pkgname, ceDataInode); 513 if (flags & FLAG_CLEAR_CACHE_ONLY) { 514 path = read_path_inode(path, "cache", kXattrInodeCache); 515 } else if (flags & FLAG_CLEAR_CODE_CACHE_ONLY) { 516 path = read_path_inode(path, "code_cache", kXattrInodeCodeCache); 517 } 518 if (access(path.c_str(), F_OK) == 0) { 519 if (delete_dir_contents(path) != 0) { 520 res = error("Failed to delete contents of " + path); 521 } 522 } 523 } 524 if (flags & FLAG_STORAGE_DE) { 525 std::string suffix = ""; 526 bool only_cache = false; 527 if (flags & FLAG_CLEAR_CACHE_ONLY) { 528 suffix = CACHE_DIR_POSTFIX; 529 only_cache = true; 530 } else if (flags & FLAG_CLEAR_CODE_CACHE_ONLY) { 531 suffix = CODE_CACHE_DIR_POSTFIX; 532 only_cache = true; 533 } 534 535 auto path = create_data_user_de_package_path(uuid_, userId, pkgname) + suffix; 536 if (access(path.c_str(), F_OK) == 0) { 537 if (delete_dir_contents(path) != 0) { 538 res = error("Failed to delete contents of " + path); 539 } 540 } 541 if (!only_cache) { 542 if (!clear_current_profile(pkgname, userId)) { 543 res = error("Failed to clear current profile for " + packageName); 544 } 545 } 546 } 547 return res; 548} 549 550static int destroy_app_reference_profile(const char *pkgname) { 551 return delete_dir_contents_and_dir( 552 create_data_ref_profile_package_path(pkgname), 553 /*ignore_if_missing*/ true); 554} 555 556static int destroy_app_current_profiles(const char *pkgname, userid_t userid) { 557 return delete_dir_contents_and_dir( 558 create_data_user_profile_package_path(userid, pkgname), 559 /*ignore_if_missing*/ true); 560} 561 562binder::Status InstalldNativeService::destroyAppProfiles(const std::string& packageName) { 563 ENFORCE_UID(AID_SYSTEM); 564 CHECK_ARGUMENT_PACKAGE_NAME(packageName); 565 std::lock_guard<std::recursive_mutex> lock(mLock); 566 567 const char* pkgname = packageName.c_str(); 568 binder::Status res = ok(); 569 std::vector<userid_t> users = get_known_users(/*volume_uuid*/ nullptr); 570 for (auto user : users) { 571 if (destroy_app_current_profiles(pkgname, user) != 0) { 572 res = error("Failed to destroy current profiles for " + packageName); 573 } 574 } 575 if (destroy_app_reference_profile(pkgname) != 0) { 576 res = error("Failed to destroy reference profile for " + packageName); 577 } 578 return res; 579} 580 581binder::Status InstalldNativeService::destroyAppData(const std::unique_ptr<std::string>& uuid, 582 const std::string& packageName, int32_t userId, int32_t flags, int64_t ceDataInode) { 583 ENFORCE_UID(AID_SYSTEM); 584 CHECK_ARGUMENT_UUID(uuid); 585 CHECK_ARGUMENT_PACKAGE_NAME(packageName); 586 std::lock_guard<std::recursive_mutex> lock(mLock); 587 588 const char* uuid_ = uuid ? uuid->c_str() : nullptr; 589 const char* pkgname = packageName.c_str(); 590 591 binder::Status res = ok(); 592 if (flags & FLAG_STORAGE_CE) { 593 auto path = create_data_user_ce_package_path(uuid_, userId, pkgname, ceDataInode); 594 if (delete_dir_contents_and_dir(path) != 0) { 595 res = error("Failed to delete " + path); 596 } 597 } 598 if (flags & FLAG_STORAGE_DE) { 599 auto path = create_data_user_de_package_path(uuid_, userId, pkgname); 600 if (delete_dir_contents_and_dir(path) != 0) { 601 res = error("Failed to delete " + path); 602 } 603 destroy_app_current_profiles(pkgname, userId); 604 // TODO(calin): If the package is still installed by other users it's probably 605 // beneficial to keep the reference profile around. 606 // Verify if it's ok to do that. 607 destroy_app_reference_profile(pkgname); 608 } 609 return res; 610} 611 612binder::Status InstalldNativeService::moveCompleteApp(const std::unique_ptr<std::string>& fromUuid, 613 const std::unique_ptr<std::string>& toUuid, const std::string& packageName, 614 const std::string& dataAppName, int32_t appId, const std::string& seInfo, 615 int32_t targetSdkVersion) { 616 ENFORCE_UID(AID_SYSTEM); 617 CHECK_ARGUMENT_UUID(fromUuid); 618 CHECK_ARGUMENT_UUID(toUuid); 619 CHECK_ARGUMENT_PACKAGE_NAME(packageName); 620 std::lock_guard<std::recursive_mutex> lock(mLock); 621 622 const char* from_uuid = fromUuid ? fromUuid->c_str() : nullptr; 623 const char* to_uuid = toUuid ? toUuid->c_str() : nullptr; 624 const char* package_name = packageName.c_str(); 625 const char* data_app_name = dataAppName.c_str(); 626 627 binder::Status res = ok(); 628 std::vector<userid_t> users = get_known_users(from_uuid); 629 630 // Copy app 631 { 632 auto from = create_data_app_package_path(from_uuid, data_app_name); 633 auto to = create_data_app_package_path(to_uuid, data_app_name); 634 auto to_parent = create_data_app_path(to_uuid); 635 636 char *argv[] = { 637 (char*) kCpPath, 638 (char*) "-F", /* delete any existing destination file first (--remove-destination) */ 639 (char*) "-p", /* preserve timestamps, ownership, and permissions */ 640 (char*) "-R", /* recurse into subdirectories (DEST must be a directory) */ 641 (char*) "-P", /* Do not follow symlinks [default] */ 642 (char*) "-d", /* don't dereference symlinks */ 643 (char*) from.c_str(), 644 (char*) to_parent.c_str() 645 }; 646 647 LOG(DEBUG) << "Copying " << from << " to " << to; 648 int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true); 649 if (rc != 0) { 650 res = error(rc, "Failed copying " + from + " to " + to); 651 goto fail; 652 } 653 654 if (selinux_android_restorecon(to.c_str(), SELINUX_ANDROID_RESTORECON_RECURSE) != 0) { 655 res = error("Failed to restorecon " + to); 656 goto fail; 657 } 658 } 659 660 // Copy private data for all known users 661 for (auto user : users) { 662 663 // Data source may not exist for all users; that's okay 664 auto from_ce = create_data_user_ce_package_path(from_uuid, user, package_name); 665 if (access(from_ce.c_str(), F_OK) != 0) { 666 LOG(INFO) << "Missing source " << from_ce; 667 continue; 668 } 669 670 if (!createAppData(toUuid, packageName, user, FLAG_STORAGE_CE | FLAG_STORAGE_DE, appId, 671 seInfo, targetSdkVersion, nullptr).isOk()) { 672 res = error("Failed to create package target"); 673 goto fail; 674 } 675 676 char *argv[] = { 677 (char*) kCpPath, 678 (char*) "-F", /* delete any existing destination file first (--remove-destination) */ 679 (char*) "-p", /* preserve timestamps, ownership, and permissions */ 680 (char*) "-R", /* recurse into subdirectories (DEST must be a directory) */ 681 (char*) "-P", /* Do not follow symlinks [default] */ 682 (char*) "-d", /* don't dereference symlinks */ 683 nullptr, 684 nullptr 685 }; 686 687 { 688 auto from = create_data_user_de_package_path(from_uuid, user, package_name); 689 auto to = create_data_user_de_path(to_uuid, user); 690 argv[6] = (char*) from.c_str(); 691 argv[7] = (char*) to.c_str(); 692 693 LOG(DEBUG) << "Copying " << from << " to " << to; 694 int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true); 695 if (rc != 0) { 696 res = error(rc, "Failed copying " + from + " to " + to); 697 goto fail; 698 } 699 } 700 { 701 auto from = create_data_user_ce_package_path(from_uuid, user, package_name); 702 auto to = create_data_user_ce_path(to_uuid, user); 703 argv[6] = (char*) from.c_str(); 704 argv[7] = (char*) to.c_str(); 705 706 LOG(DEBUG) << "Copying " << from << " to " << to; 707 int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true); 708 if (rc != 0) { 709 res = error(rc, "Failed copying " + from + " to " + to); 710 goto fail; 711 } 712 } 713 714 if (!restoreconAppData(toUuid, packageName, user, FLAG_STORAGE_CE | FLAG_STORAGE_DE, 715 appId, seInfo).isOk()) { 716 res = error("Failed to restorecon"); 717 goto fail; 718 } 719 } 720 721 // We let the framework scan the new location and persist that before 722 // deleting the data in the old location; this ordering ensures that 723 // we can recover from things like battery pulls. 724 return ok(); 725 726fail: 727 // Nuke everything we might have already copied 728 { 729 auto to = create_data_app_package_path(to_uuid, data_app_name); 730 if (delete_dir_contents(to.c_str(), 1, NULL) != 0) { 731 LOG(WARNING) << "Failed to rollback " << to; 732 } 733 } 734 for (auto user : users) { 735 { 736 auto to = create_data_user_de_package_path(to_uuid, user, package_name); 737 if (delete_dir_contents(to.c_str(), 1, NULL) != 0) { 738 LOG(WARNING) << "Failed to rollback " << to; 739 } 740 } 741 { 742 auto to = create_data_user_ce_package_path(to_uuid, user, package_name); 743 if (delete_dir_contents(to.c_str(), 1, NULL) != 0) { 744 LOG(WARNING) << "Failed to rollback " << to; 745 } 746 } 747 } 748 return res; 749} 750 751binder::Status InstalldNativeService::createUserData(const std::unique_ptr<std::string>& uuid, 752 int32_t userId, int32_t userSerial ATTRIBUTE_UNUSED, int32_t flags) { 753 ENFORCE_UID(AID_SYSTEM); 754 CHECK_ARGUMENT_UUID(uuid); 755 std::lock_guard<std::recursive_mutex> lock(mLock); 756 757 const char* uuid_ = uuid ? uuid->c_str() : nullptr; 758 if (flags & FLAG_STORAGE_DE) { 759 if (uuid_ == nullptr) { 760 if (ensure_config_user_dirs(userId) != 0) { 761 return error(StringPrintf("Failed to ensure dirs for %d", userId)); 762 } 763 } 764 } 765 return ok(); 766} 767 768binder::Status InstalldNativeService::destroyUserData(const std::unique_ptr<std::string>& uuid, 769 int32_t userId, int32_t flags) { 770 ENFORCE_UID(AID_SYSTEM); 771 CHECK_ARGUMENT_UUID(uuid); 772 std::lock_guard<std::recursive_mutex> lock(mLock); 773 774 const char* uuid_ = uuid ? uuid->c_str() : nullptr; 775 binder::Status res = ok(); 776 if (flags & FLAG_STORAGE_DE) { 777 auto path = create_data_user_de_path(uuid_, userId); 778 if (delete_dir_contents_and_dir(path, true) != 0) { 779 res = error("Failed to delete " + path); 780 } 781 if (uuid_ == nullptr) { 782 path = create_data_misc_legacy_path(userId); 783 if (delete_dir_contents_and_dir(path, true) != 0) { 784 res = error("Failed to delete " + path); 785 } 786 path = create_data_user_profile_path(userId); 787 if (delete_dir_contents_and_dir(path, true) != 0) { 788 res = error("Failed to delete " + path); 789 } 790 } 791 } 792 if (flags & FLAG_STORAGE_CE) { 793 auto path = create_data_user_ce_path(uuid_, userId); 794 if (delete_dir_contents_and_dir(path, true) != 0) { 795 res = error("Failed to delete " + path); 796 } 797 path = create_data_media_path(uuid_, userId); 798 if (delete_dir_contents_and_dir(path, true) != 0) { 799 res = error("Failed to delete " + path); 800 } 801 } 802 return res; 803} 804 805/* Try to ensure free_size bytes of storage are available. 806 * Returns 0 on success. 807 * This is rather simple-minded because doing a full LRU would 808 * be potentially memory-intensive, and without atime it would 809 * also require that apps constantly modify file metadata even 810 * when just reading from the cache, which is pretty awful. 811 */ 812binder::Status InstalldNativeService::freeCache(const std::unique_ptr<std::string>& uuid, 813 int64_t freeStorageSize) { 814 ENFORCE_UID(AID_SYSTEM); 815 CHECK_ARGUMENT_UUID(uuid); 816 std::lock_guard<std::recursive_mutex> lock(mLock); 817 818 const char* uuid_ = uuid ? uuid->c_str() : nullptr; 819 cache_t* cache; 820 int64_t avail; 821 822 auto data_path = create_data_path(uuid_); 823 824 avail = data_disk_free(data_path); 825 if (avail < 0) { 826 return error("Failed to determine free space for " + data_path); 827 } 828 829 ALOGI("free_cache(%" PRId64 ") avail %" PRId64 "\n", freeStorageSize, avail); 830 if (avail >= freeStorageSize) { 831 return ok(); 832 } 833 834 cache = start_cache_collection(); 835 836 auto users = get_known_users(uuid_); 837 for (auto user : users) { 838 add_cache_files(cache, create_data_user_ce_path(uuid_, user)); 839 add_cache_files(cache, create_data_user_de_path(uuid_, user)); 840 add_cache_files(cache, 841 StringPrintf("%s/Android/data", create_data_media_path(uuid_, user).c_str())); 842 } 843 844 clear_cache_files(data_path, cache, freeStorageSize); 845 finish_cache_collection(cache); 846 847 avail = data_disk_free(data_path); 848 if (avail >= freeStorageSize) { 849 return ok(); 850 } else { 851 return error(StringPrintf("Failed to free up %" PRId64 " on %s; final free space %" PRId64, 852 freeStorageSize, data_path.c_str(), avail)); 853 } 854} 855 856binder::Status InstalldNativeService::rmdex(const std::string& codePath, 857 const std::string& instructionSet) { 858 ENFORCE_UID(AID_SYSTEM); 859 std::lock_guard<std::recursive_mutex> lock(mLock); 860 861 char dex_path[PKG_PATH_MAX]; 862 863 const char* path = codePath.c_str(); 864 const char* instruction_set = instructionSet.c_str(); 865 866 if (validate_apk_path(path) && validate_system_app_path(path)) { 867 return error("Invalid path " + codePath); 868 } 869 870 if (!create_cache_path(dex_path, path, instruction_set)) { 871 return error("Failed to create cache path for " + codePath); 872 } 873 874 ALOGV("unlink %s\n", dex_path); 875 if (unlink(dex_path) < 0) { 876 return error(StringPrintf("Failed to unlink %s", dex_path)); 877 } else { 878 return ok(); 879 } 880} 881 882struct stats { 883 int64_t codeSize; 884 int64_t dataSize; 885 int64_t cacheSize; 886}; 887 888#if MEASURE_DEBUG 889static std::string toString(std::vector<int64_t> values) { 890 std::stringstream res; 891 res << "["; 892 for (size_t i = 0; i < values.size(); i++) { 893 res << values[i]; 894 if (i < values.size() - 1) { 895 res << ","; 896 } 897 } 898 res << "]"; 899 return res.str(); 900} 901#endif 902 903static void collectQuotaStats(const std::string& device, int32_t userId, 904 int32_t appId, struct stats* stats, struct stats* extStats ATTRIBUTE_UNUSED) { 905 if (device.empty()) return; 906 907 struct dqblk dq; 908 909 uid_t uid = multiuser_get_uid(userId, appId); 910 if (quotactl(QCMD(Q_GETQUOTA, USRQUOTA), device.c_str(), uid, 911 reinterpret_cast<char*>(&dq)) != 0) { 912 if (errno != ESRCH) { 913 PLOG(ERROR) << "Failed to quotactl " << device << " for UID " << uid; 914 } 915 } else { 916#if MEASURE_DEBUG 917 LOG(DEBUG) << "quotactl() for UID " << uid << " " << dq.dqb_curspace; 918#endif 919 stats->dataSize += dq.dqb_curspace; 920 } 921 922 int cacheGid = multiuser_get_cache_gid(userId, appId); 923 if (cacheGid != -1) { 924 if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), cacheGid, 925 reinterpret_cast<char*>(&dq)) != 0) { 926 if (errno != ESRCH) { 927 PLOG(ERROR) << "Failed to quotactl " << device << " for GID " << cacheGid; 928 } 929 } else { 930#if MEASURE_DEBUG 931 LOG(DEBUG) << "quotactl() for GID " << cacheGid << " " << dq.dqb_curspace; 932#endif 933 stats->cacheSize += dq.dqb_curspace; 934 } 935 } 936 937 int sharedGid = multiuser_get_shared_app_gid(uid); 938 if (sharedGid != -1) { 939 if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), sharedGid, 940 reinterpret_cast<char*>(&dq)) != 0) { 941 if (errno != ESRCH) { 942 PLOG(ERROR) << "Failed to quotactl " << device << " for GID " << sharedGid; 943 } 944 } else { 945#if MEASURE_DEBUG 946 LOG(DEBUG) << "quotactl() for GID " << sharedGid << " " << dq.dqb_curspace; 947#endif 948 stats->codeSize += dq.dqb_curspace; 949 } 950 } 951 952#if MEASURE_EXTERNAL 953 // TODO: measure using external GIDs 954#endif 955} 956 957static void collectManualStats(const std::string& path, struct stats* stats) { 958 DIR *d; 959 int dfd; 960 struct dirent *de; 961 struct stat s; 962 963 d = opendir(path.c_str()); 964 if (d == nullptr) { 965 if (errno != ENOENT) { 966 PLOG(WARNING) << "Failed to open " << path; 967 } 968 return; 969 } 970 dfd = dirfd(d); 971 while ((de = readdir(d))) { 972 const char *name = de->d_name; 973 974 int64_t size = 0; 975 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) { 976 size = s.st_blocks * 512; 977 } 978 979 if (de->d_type == DT_DIR) { 980 if (!strcmp(name, ".")) { 981 // Don't recurse, but still count node size 982 } else if (!strcmp(name, "..")) { 983 // Don't recurse or count node size 984 continue; 985 } else { 986 // Measure all children nodes 987 size = 0; 988 calculate_tree_size(StringPrintf("%s/%s", path.c_str(), name), &size); 989 } 990 991 if (!strcmp(name, "cache") || !strcmp(name, "code_cache")) { 992 stats->cacheSize += size; 993 } 994 } 995 996 // Legacy symlink isn't owned by app 997 if (de->d_type == DT_LNK && !strcmp(name, "lib")) { 998 continue; 999 } 1000 1001 // Everything found inside is considered data 1002 stats->dataSize += size; 1003 } 1004 closedir(d); 1005} 1006 1007static void collectManualStatsForUser(const std::string& path, struct stats* stats, 1008 bool exclude_apps = false) { 1009 DIR *d; 1010 int dfd; 1011 struct dirent *de; 1012 struct stat s; 1013 1014 d = opendir(path.c_str()); 1015 if (d == nullptr) { 1016 if (errno != ENOENT) { 1017 PLOG(WARNING) << "Failed to open " << path; 1018 } 1019 return; 1020 } 1021 dfd = dirfd(d); 1022 while ((de = readdir(d))) { 1023 if (de->d_type == DT_DIR) { 1024 const char *name = de->d_name; 1025 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) != 0) { 1026 continue; 1027 } 1028 if (!strcmp(name, ".") || !strcmp(name, "..")) { 1029 continue; 1030 } else if (exclude_apps && (s.st_uid >= AID_APP_START && s.st_uid <= AID_APP_END)) { 1031 continue; 1032 } else { 1033 collectManualStats(StringPrintf("%s/%s", path.c_str(), name), stats); 1034 } 1035 } 1036 } 1037 closedir(d); 1038} 1039 1040binder::Status InstalldNativeService::getAppSize(const std::unique_ptr<std::string>& uuid, 1041 const std::vector<std::string>& packageNames, int32_t userId, int32_t flags, 1042 int32_t appId, const std::vector<int64_t>& ceDataInodes, 1043 const std::vector<std::string>& codePaths, std::vector<int64_t>* _aidl_return) { 1044 ENFORCE_UID(AID_SYSTEM); 1045 CHECK_ARGUMENT_UUID(uuid); 1046 for (auto packageName : packageNames) { 1047 CHECK_ARGUMENT_PACKAGE_NAME(packageName); 1048 } 1049 std::lock_guard<std::recursive_mutex> lock(mLock); 1050 1051 // When modifying this logic, always verify using tests: 1052 // runtest -x frameworks/base/services/tests/servicestests/src/com/android/server/pm/InstallerTest.java -m testGetAppSize 1053 1054#if MEASURE_DEBUG 1055 LOG(INFO) << "Measuring user " << userId << " app " << appId; 1056#endif 1057 1058 // Here's a summary of the common storage locations across the platform, 1059 // and how they're each tagged: 1060 // 1061 // /data/app/com.example UID system 1062 // /data/app/com.example/oat UID system 1063 // /data/user/0/com.example UID u0_a10 GID u0_a10 1064 // /data/user/0/com.example/cache UID u0_a10 GID u0_a10_cache 1065 // /data/media/0/foo.txt UID u0_media_rw 1066 // /data/media/0/bar.jpg UID u0_media_rw GID u0_media_image 1067 // /data/media/0/Android/data/com.example UID u0_media_rw GID u0_a10_ext 1068 // /data/media/0/Android/data/com.example/cache UID u0_media_rw GID u0_a10_ext_cache 1069 // /data/media/obb/com.example UID system 1070 1071 struct stats stats; 1072 struct stats extStats; 1073 memset(&stats, 0, sizeof(stats)); 1074 memset(&extStats, 0, sizeof(extStats)); 1075 1076 const char* uuid_ = uuid ? uuid->c_str() : nullptr; 1077 1078 auto device = findQuotaDeviceForUuid(uuid); 1079 if (device.empty()) { 1080 flags &= ~FLAG_USE_QUOTA; 1081 } 1082 1083 ATRACE_BEGIN("obb"); 1084 for (auto packageName : packageNames) { 1085 auto obbCodePath = create_data_media_obb_path(uuid_, packageName.c_str()); 1086 calculate_tree_size(obbCodePath, &extStats.codeSize); 1087 } 1088 ATRACE_END(); 1089 1090 if (flags & FLAG_USE_QUOTA && appId >= AID_APP_START) { 1091 ATRACE_BEGIN("code"); 1092 for (auto codePath : codePaths) { 1093 calculate_tree_size(codePath, &stats.codeSize, -1, 1094 multiuser_get_shared_gid(userId, appId)); 1095 } 1096 ATRACE_END(); 1097 1098 ATRACE_BEGIN("quota"); 1099 collectQuotaStats(device, userId, appId, &stats, &extStats); 1100 ATRACE_END(); 1101 1102 } else { 1103 ATRACE_BEGIN("code"); 1104 for (auto codePath : codePaths) { 1105 calculate_tree_size(codePath, &stats.codeSize); 1106 } 1107 ATRACE_END(); 1108 1109 for (size_t i = 0; i < packageNames.size(); i++) { 1110 const char* pkgname = packageNames[i].c_str(); 1111 1112 ATRACE_BEGIN("data"); 1113 auto cePath = create_data_user_ce_package_path(uuid_, userId, pkgname, ceDataInodes[i]); 1114 collectManualStats(cePath, &stats); 1115 auto dePath = create_data_user_de_package_path(uuid_, userId, pkgname); 1116 collectManualStats(dePath, &stats); 1117 ATRACE_END(); 1118 1119 ATRACE_BEGIN("profiles"); 1120 auto userProfilePath = create_data_user_profile_package_path(userId, pkgname); 1121 calculate_tree_size(userProfilePath, &stats.dataSize); 1122 auto refProfilePath = create_data_ref_profile_package_path(pkgname); 1123 calculate_tree_size(refProfilePath, &stats.codeSize); 1124 ATRACE_END(); 1125 1126#if MEASURE_EXTERNAL 1127 ATRACE_BEGIN("external"); 1128 auto extPath = create_data_media_package_path(uuid_, userId, pkgname, "data"); 1129 collectManualStats(extPath, &extStats); 1130 auto mediaPath = create_data_media_package_path(uuid_, userId, pkgname, "media"); 1131 calculate_tree_size(mediaPath, &extStats.dataSize); 1132 ATRACE_END(); 1133#endif 1134 } 1135 1136 ATRACE_BEGIN("dalvik"); 1137 int32_t sharedGid = multiuser_get_shared_gid(userId, appId); 1138 if (sharedGid != -1) { 1139 calculate_tree_size(create_data_dalvik_cache_path(), &stats.codeSize, 1140 sharedGid, -1); 1141 } 1142 calculate_tree_size(create_data_misc_foreign_dex_path(userId), &stats.dataSize, 1143 multiuser_get_uid(userId, appId), -1); 1144 ATRACE_END(); 1145 } 1146 1147 std::vector<int64_t> ret; 1148 ret.push_back(stats.codeSize); 1149 ret.push_back(stats.dataSize); 1150 ret.push_back(stats.cacheSize); 1151 ret.push_back(extStats.codeSize); 1152 ret.push_back(extStats.dataSize); 1153 ret.push_back(extStats.cacheSize); 1154#if MEASURE_DEBUG 1155 LOG(DEBUG) << "Final result " << toString(ret); 1156#endif 1157 *_aidl_return = ret; 1158 return ok(); 1159} 1160 1161binder::Status InstalldNativeService::getUserSize(const std::unique_ptr<std::string>& uuid, 1162 int32_t userId, int32_t flags, const std::vector<int32_t>& appIds, 1163 std::vector<int64_t>* _aidl_return) { 1164 ENFORCE_UID(AID_SYSTEM); 1165 CHECK_ARGUMENT_UUID(uuid); 1166 std::lock_guard<std::recursive_mutex> lock(mLock); 1167 1168 // When modifying this logic, always verify using tests: 1169 // runtest -x frameworks/base/services/tests/servicestests/src/com/android/server/pm/InstallerTest.java -m testGetUserSize 1170 1171#if MEASURE_DEBUG 1172 LOG(INFO) << "Measuring user " << userId; 1173#endif 1174 1175 struct stats stats; 1176 struct stats extStats; 1177 memset(&stats, 0, sizeof(stats)); 1178 memset(&extStats, 0, sizeof(extStats)); 1179 1180 const char* uuid_ = uuid ? uuid->c_str() : nullptr; 1181 1182 ATRACE_BEGIN("obb"); 1183 auto obbPath = create_data_path(uuid_) + "/media/obb"; 1184 calculate_tree_size(obbPath, &extStats.codeSize); 1185 ATRACE_END(); 1186 1187 auto device = findQuotaDeviceForUuid(uuid); 1188 if (device.empty()) { 1189 flags &= ~FLAG_USE_QUOTA; 1190 } 1191 1192 if (flags & FLAG_USE_QUOTA) { 1193 ATRACE_BEGIN("code"); 1194 calculate_tree_size(create_data_app_path(uuid_), &stats.codeSize, -1, -1, true); 1195 ATRACE_END(); 1196 1197 ATRACE_BEGIN("data"); 1198 auto cePath = create_data_user_ce_path(uuid_, userId); 1199 collectManualStatsForUser(cePath, &stats, true); 1200 auto dePath = create_data_user_de_path(uuid_, userId); 1201 collectManualStatsForUser(dePath, &stats, true); 1202 ATRACE_END(); 1203 1204 ATRACE_BEGIN("profile"); 1205 auto userProfilePath = create_data_user_profile_path(userId); 1206 calculate_tree_size(userProfilePath, &stats.dataSize, -1, -1, true); 1207 auto refProfilePath = create_data_ref_profile_path(); 1208 calculate_tree_size(refProfilePath, &stats.codeSize, -1, -1, true); 1209 ATRACE_END(); 1210 1211#if MEASURE_EXTERNAL 1212 ATRACE_BEGIN("external"); 1213 // TODO: measure external storage paths 1214 ATRACE_END(); 1215#endif 1216 1217 ATRACE_BEGIN("dalvik"); 1218 calculate_tree_size(create_data_dalvik_cache_path(), &stats.codeSize, 1219 -1, -1, true); 1220 calculate_tree_size(create_data_misc_foreign_dex_path(userId), &stats.dataSize, 1221 -1, -1, true); 1222 ATRACE_END(); 1223 1224 ATRACE_BEGIN("quota"); 1225 for (auto appId : appIds) { 1226 if (appId >= AID_APP_START) { 1227 collectQuotaStats(device, userId, appId, &stats, &extStats); 1228#if MEASURE_DEBUG 1229 // Sleep to make sure we don't lose logs 1230 usleep(1); 1231#endif 1232 } 1233 } 1234 ATRACE_END(); 1235 } else { 1236 ATRACE_BEGIN("code"); 1237 calculate_tree_size(create_data_app_path(uuid_), &stats.codeSize); 1238 ATRACE_END(); 1239 1240 ATRACE_BEGIN("data"); 1241 auto cePath = create_data_user_ce_path(uuid_, userId); 1242 collectManualStatsForUser(cePath, &stats); 1243 auto dePath = create_data_user_de_path(uuid_, userId); 1244 collectManualStatsForUser(dePath, &stats); 1245 ATRACE_END(); 1246 1247 ATRACE_BEGIN("profile"); 1248 auto userProfilePath = create_data_user_profile_path(userId); 1249 calculate_tree_size(userProfilePath, &stats.dataSize); 1250 auto refProfilePath = create_data_ref_profile_path(); 1251 calculate_tree_size(refProfilePath, &stats.codeSize); 1252 ATRACE_END(); 1253 1254#if MEASURE_EXTERNAL 1255 ATRACE_BEGIN("external"); 1256 // TODO: measure external storage paths 1257 ATRACE_END(); 1258#endif 1259 1260 ATRACE_BEGIN("dalvik"); 1261 calculate_tree_size(create_data_dalvik_cache_path(), &stats.codeSize); 1262 calculate_tree_size(create_data_misc_foreign_dex_path(userId), &stats.dataSize); 1263 ATRACE_END(); 1264 } 1265 1266 std::vector<int64_t> ret; 1267 ret.push_back(stats.codeSize); 1268 ret.push_back(stats.dataSize); 1269 ret.push_back(stats.cacheSize); 1270 ret.push_back(extStats.codeSize); 1271 ret.push_back(extStats.dataSize); 1272 ret.push_back(extStats.cacheSize); 1273#if MEASURE_DEBUG 1274 LOG(DEBUG) << "Final result " << toString(ret); 1275#endif 1276 *_aidl_return = ret; 1277 return ok(); 1278} 1279 1280binder::Status InstalldNativeService::getExternalSize(const std::unique_ptr<std::string>& uuid, 1281 int32_t userId, int32_t flags, std::vector<int64_t>* _aidl_return) { 1282 ENFORCE_UID(AID_SYSTEM); 1283 CHECK_ARGUMENT_UUID(uuid); 1284 std::lock_guard<std::recursive_mutex> lock(mLock); 1285 1286 // When modifying this logic, always verify using tests: 1287 // runtest -x frameworks/base/services/tests/servicestests/src/com/android/server/pm/InstallerTest.java -m testGetExternalSize 1288 1289#if MEASURE_DEBUG 1290 LOG(INFO) << "Measuring external " << userId; 1291#endif 1292 1293 const char* uuid_ = uuid ? uuid->c_str() : nullptr; 1294 1295 int64_t totalSize = 0; 1296 int64_t audioSize = 0; 1297 int64_t videoSize = 0; 1298 int64_t imageSize = 0; 1299 1300 auto device = findQuotaDeviceForUuid(uuid); 1301 if (device.empty()) { 1302 flags &= ~FLAG_USE_QUOTA; 1303 } 1304 1305 if (flags & FLAG_USE_QUOTA) { 1306 struct dqblk dq; 1307 1308 uid_t uid = multiuser_get_uid(userId, AID_MEDIA_RW); 1309 if (quotactl(QCMD(Q_GETQUOTA, USRQUOTA), device.c_str(), uid, 1310 reinterpret_cast<char*>(&dq)) != 0) { 1311 if (errno != ESRCH) { 1312 PLOG(ERROR) << "Failed to quotactl " << device << " for UID " << uid; 1313 } 1314 } else { 1315#if MEASURE_DEBUG 1316 LOG(DEBUG) << "quotactl() for UID " << uid << " " << dq.dqb_curspace; 1317#endif 1318 totalSize = dq.dqb_curspace; 1319 } 1320 1321 gid_t audioGid = multiuser_get_uid(userId, AID_MEDIA_AUDIO); 1322 if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), audioGid, 1323 reinterpret_cast<char*>(&dq)) == 0) { 1324#if MEASURE_DEBUG 1325 LOG(DEBUG) << "quotactl() for GID " << audioGid << " " << dq.dqb_curspace; 1326#endif 1327 audioSize = dq.dqb_curspace; 1328 } 1329 gid_t videoGid = multiuser_get_uid(userId, AID_MEDIA_VIDEO); 1330 if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), videoGid, 1331 reinterpret_cast<char*>(&dq)) == 0) { 1332#if MEASURE_DEBUG 1333 LOG(DEBUG) << "quotactl() for GID " << videoGid << " " << dq.dqb_curspace; 1334#endif 1335 videoSize = dq.dqb_curspace; 1336 } 1337 gid_t imageGid = multiuser_get_uid(userId, AID_MEDIA_IMAGE); 1338 if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), imageGid, 1339 reinterpret_cast<char*>(&dq)) == 0) { 1340#if MEASURE_DEBUG 1341 LOG(DEBUG) << "quotactl() for GID " << imageGid << " " << dq.dqb_curspace; 1342#endif 1343 imageSize = dq.dqb_curspace; 1344 } 1345 } else { 1346 FTS *fts; 1347 FTSENT *p; 1348 auto path = create_data_media_path(uuid_, userId); 1349 char *argv[] = { (char*) path.c_str(), nullptr }; 1350 if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_XDEV, NULL))) { 1351 return error("Failed to fts_open " + path); 1352 } 1353 while ((p = fts_read(fts)) != NULL) { 1354 char* ext; 1355 int64_t size = (p->fts_statp->st_blocks * 512); 1356 switch (p->fts_info) { 1357 case FTS_F: 1358 // Only categorize files not belonging to apps 1359 if (p->fts_statp->st_gid < AID_APP_START) { 1360 ext = strrchr(p->fts_name, '.'); 1361 if (ext != nullptr) { 1362 switch (MatchExtension(++ext)) { 1363 case AID_MEDIA_AUDIO: audioSize += size; break; 1364 case AID_MEDIA_VIDEO: videoSize += size; break; 1365 case AID_MEDIA_IMAGE: imageSize += size; break; 1366 } 1367 } 1368 } 1369 // Fall through to always count against total 1370 case FTS_D: 1371 case FTS_DEFAULT: 1372 case FTS_SL: 1373 case FTS_SLNONE: 1374 totalSize += size; 1375 break; 1376 } 1377 } 1378 fts_close(fts); 1379 } 1380 1381 std::vector<int64_t> ret; 1382 ret.push_back(totalSize); 1383 ret.push_back(audioSize); 1384 ret.push_back(videoSize); 1385 ret.push_back(imageSize); 1386#if MEASURE_DEBUG 1387 LOG(DEBUG) << "Final result " << toString(ret); 1388#endif 1389 *_aidl_return = ret; 1390 return ok(); 1391} 1392 1393// Dumps the contents of a profile file, using pkgname's dex files for pretty 1394// printing the result. 1395binder::Status InstalldNativeService::dumpProfiles(int32_t uid, const std::string& packageName, 1396 const std::string& codePaths, bool* _aidl_return) { 1397 ENFORCE_UID(AID_SYSTEM); 1398 CHECK_ARGUMENT_PACKAGE_NAME(packageName); 1399 std::lock_guard<std::recursive_mutex> lock(mLock); 1400 1401 const char* pkgname = packageName.c_str(); 1402 const char* code_paths = codePaths.c_str(); 1403 1404 *_aidl_return = dump_profiles(uid, pkgname, code_paths); 1405 return ok(); 1406} 1407 1408// TODO: Consider returning error codes. 1409binder::Status InstalldNativeService::mergeProfiles(int32_t uid, const std::string& packageName, 1410 bool* _aidl_return) { 1411 ENFORCE_UID(AID_SYSTEM); 1412 CHECK_ARGUMENT_PACKAGE_NAME(packageName); 1413 std::lock_guard<std::recursive_mutex> lock(mLock); 1414 1415 const char* pkgname = packageName.c_str(); 1416 *_aidl_return = analyse_profiles(uid, pkgname); 1417 return ok(); 1418} 1419 1420binder::Status InstalldNativeService::dexopt(const std::string& apkPath, int32_t uid, 1421 const std::unique_ptr<std::string>& packageName, const std::string& instructionSet, 1422 int32_t dexoptNeeded, const std::unique_ptr<std::string>& outputPath, int32_t dexFlags, 1423 const std::string& compilerFilter, const std::unique_ptr<std::string>& uuid, 1424 const std::unique_ptr<std::string>& sharedLibraries) { 1425 ENFORCE_UID(AID_SYSTEM); 1426 CHECK_ARGUMENT_UUID(uuid); 1427 if (packageName && *packageName != "*") { 1428 CHECK_ARGUMENT_PACKAGE_NAME(*packageName); 1429 } 1430 std::lock_guard<std::recursive_mutex> lock(mLock); 1431 1432 const char* apk_path = apkPath.c_str(); 1433 const char* pkgname = packageName ? packageName->c_str() : "*"; 1434 const char* instruction_set = instructionSet.c_str(); 1435 const char* oat_dir = outputPath ? outputPath->c_str() : nullptr; 1436 const char* compiler_filter = compilerFilter.c_str(); 1437 const char* volume_uuid = uuid ? uuid->c_str() : nullptr; 1438 const char* shared_libraries = sharedLibraries ? sharedLibraries->c_str() : nullptr; 1439 1440 int res = android::installd::dexopt(apk_path, uid, pkgname, instruction_set, dexoptNeeded, 1441 oat_dir, dexFlags, compiler_filter, volume_uuid, shared_libraries); 1442 return res ? error(res, "Failed to dexopt") : ok(); 1443} 1444 1445binder::Status InstalldNativeService::markBootComplete(const std::string& instructionSet) { 1446 ENFORCE_UID(AID_SYSTEM); 1447 std::lock_guard<std::recursive_mutex> lock(mLock); 1448 1449 const char* instruction_set = instructionSet.c_str(); 1450 1451 char boot_marker_path[PKG_PATH_MAX]; 1452 sprintf(boot_marker_path, 1453 "%s/%s/%s/.booting", 1454 android_data_dir.path, 1455 DALVIK_CACHE, 1456 instruction_set); 1457 1458 ALOGV("mark_boot_complete : %s", boot_marker_path); 1459 if (unlink(boot_marker_path) != 0) { 1460 return error(StringPrintf("Failed to unlink %s", boot_marker_path)); 1461 } 1462 return ok(); 1463} 1464 1465void mkinnerdirs(char* path, int basepos, mode_t mode, int uid, int gid, 1466 struct stat* statbuf) 1467{ 1468 while (path[basepos] != 0) { 1469 if (path[basepos] == '/') { 1470 path[basepos] = 0; 1471 if (lstat(path, statbuf) < 0) { 1472 ALOGV("Making directory: %s\n", path); 1473 if (mkdir(path, mode) == 0) { 1474 chown(path, uid, gid); 1475 } else { 1476 ALOGW("Unable to make directory %s: %s\n", path, strerror(errno)); 1477 } 1478 } 1479 path[basepos] = '/'; 1480 basepos++; 1481 } 1482 basepos++; 1483 } 1484} 1485 1486binder::Status InstalldNativeService::linkNativeLibraryDirectory( 1487 const std::unique_ptr<std::string>& uuid, const std::string& packageName, 1488 const std::string& nativeLibPath32, int32_t userId) { 1489 ENFORCE_UID(AID_SYSTEM); 1490 CHECK_ARGUMENT_UUID(uuid); 1491 CHECK_ARGUMENT_PACKAGE_NAME(packageName); 1492 std::lock_guard<std::recursive_mutex> lock(mLock); 1493 1494 const char* uuid_ = uuid ? uuid->c_str() : nullptr; 1495 const char* pkgname = packageName.c_str(); 1496 const char* asecLibDir = nativeLibPath32.c_str(); 1497 struct stat s, libStat; 1498 binder::Status res = ok(); 1499 1500 auto _pkgdir = create_data_user_ce_package_path(uuid_, userId, pkgname); 1501 auto _libsymlink = _pkgdir + PKG_LIB_POSTFIX; 1502 1503 const char* pkgdir = _pkgdir.c_str(); 1504 const char* libsymlink = _libsymlink.c_str(); 1505 1506 if (stat(pkgdir, &s) < 0) { 1507 return error("Failed to stat " + _pkgdir); 1508 } 1509 1510 if (chown(pkgdir, AID_INSTALL, AID_INSTALL) < 0) { 1511 return error("Failed to chown " + _pkgdir); 1512 } 1513 1514 if (chmod(pkgdir, 0700) < 0) { 1515 res = error("Failed to chmod " + _pkgdir); 1516 goto out; 1517 } 1518 1519 if (lstat(libsymlink, &libStat) < 0) { 1520 if (errno != ENOENT) { 1521 res = error("Failed to stat " + _libsymlink); 1522 goto out; 1523 } 1524 } else { 1525 if (S_ISDIR(libStat.st_mode)) { 1526 if (delete_dir_contents(libsymlink, 1, NULL) < 0) { 1527 res = error("Failed to delete " + _libsymlink); 1528 goto out; 1529 } 1530 } else if (S_ISLNK(libStat.st_mode)) { 1531 if (unlink(libsymlink) < 0) { 1532 res = error("Failed to unlink " + _libsymlink); 1533 goto out; 1534 } 1535 } 1536 } 1537 1538 if (symlink(asecLibDir, libsymlink) < 0) { 1539 res = error("Failed to symlink " + _libsymlink + " to " + nativeLibPath32); 1540 goto out; 1541 } 1542 1543out: 1544 if (chmod(pkgdir, s.st_mode) < 0) { 1545 auto msg = "Failed to cleanup chmod " + _pkgdir; 1546 if (res.isOk()) { 1547 res = error(msg); 1548 } else { 1549 PLOG(ERROR) << msg; 1550 } 1551 } 1552 1553 if (chown(pkgdir, s.st_uid, s.st_gid) < 0) { 1554 auto msg = "Failed to cleanup chown " + _pkgdir; 1555 if (res.isOk()) { 1556 res = error(msg); 1557 } else { 1558 PLOG(ERROR) << msg; 1559 } 1560 } 1561 1562 return res; 1563} 1564 1565static void run_idmap(const char *target_apk, const char *overlay_apk, int idmap_fd) 1566{ 1567 static const char *IDMAP_BIN = "/system/bin/idmap"; 1568 static const size_t MAX_INT_LEN = 32; 1569 char idmap_str[MAX_INT_LEN]; 1570 1571 snprintf(idmap_str, sizeof(idmap_str), "%d", idmap_fd); 1572 1573 execl(IDMAP_BIN, IDMAP_BIN, "--fd", target_apk, overlay_apk, idmap_str, (char*)NULL); 1574 ALOGE("execl(%s) failed: %s\n", IDMAP_BIN, strerror(errno)); 1575} 1576 1577// Transform string /a/b/c.apk to (prefix)/a@b@c.apk@(suffix) 1578// eg /a/b/c.apk to /data/resource-cache/a@b@c.apk@idmap 1579static int flatten_path(const char *prefix, const char *suffix, 1580 const char *overlay_path, char *idmap_path, size_t N) 1581{ 1582 if (overlay_path == NULL || idmap_path == NULL) { 1583 return -1; 1584 } 1585 const size_t len_overlay_path = strlen(overlay_path); 1586 // will access overlay_path + 1 further below; requires absolute path 1587 if (len_overlay_path < 2 || *overlay_path != '/') { 1588 return -1; 1589 } 1590 const size_t len_idmap_root = strlen(prefix); 1591 const size_t len_suffix = strlen(suffix); 1592 if (SIZE_MAX - len_idmap_root < len_overlay_path || 1593 SIZE_MAX - (len_idmap_root + len_overlay_path) < len_suffix) { 1594 // additions below would cause overflow 1595 return -1; 1596 } 1597 if (N < len_idmap_root + len_overlay_path + len_suffix) { 1598 return -1; 1599 } 1600 memset(idmap_path, 0, N); 1601 snprintf(idmap_path, N, "%s%s%s", prefix, overlay_path + 1, suffix); 1602 char *ch = idmap_path + len_idmap_root; 1603 while (*ch != '\0') { 1604 if (*ch == '/') { 1605 *ch = '@'; 1606 } 1607 ++ch; 1608 } 1609 return 0; 1610} 1611 1612binder::Status InstalldNativeService::idmap(const std::string& targetApkPath, 1613 const std::string& overlayApkPath, int32_t uid) { 1614 ENFORCE_UID(AID_SYSTEM); 1615 std::lock_guard<std::recursive_mutex> lock(mLock); 1616 1617 const char* target_apk = targetApkPath.c_str(); 1618 const char* overlay_apk = overlayApkPath.c_str(); 1619 ALOGV("idmap target_apk=%s overlay_apk=%s uid=%d\n", target_apk, overlay_apk, uid); 1620 1621 int idmap_fd = -1; 1622 char idmap_path[PATH_MAX]; 1623 1624 if (flatten_path(IDMAP_PREFIX, IDMAP_SUFFIX, overlay_apk, 1625 idmap_path, sizeof(idmap_path)) == -1) { 1626 ALOGE("idmap cannot generate idmap path for overlay %s\n", overlay_apk); 1627 goto fail; 1628 } 1629 1630 unlink(idmap_path); 1631 idmap_fd = open(idmap_path, O_RDWR | O_CREAT | O_EXCL, 0644); 1632 if (idmap_fd < 0) { 1633 ALOGE("idmap cannot open '%s' for output: %s\n", idmap_path, strerror(errno)); 1634 goto fail; 1635 } 1636 if (fchown(idmap_fd, AID_SYSTEM, uid) < 0) { 1637 ALOGE("idmap cannot chown '%s'\n", idmap_path); 1638 goto fail; 1639 } 1640 if (fchmod(idmap_fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0) { 1641 ALOGE("idmap cannot chmod '%s'\n", idmap_path); 1642 goto fail; 1643 } 1644 1645 pid_t pid; 1646 pid = fork(); 1647 if (pid == 0) { 1648 /* child -- drop privileges before continuing */ 1649 if (setgid(uid) != 0) { 1650 ALOGE("setgid(%d) failed during idmap\n", uid); 1651 exit(1); 1652 } 1653 if (setuid(uid) != 0) { 1654 ALOGE("setuid(%d) failed during idmap\n", uid); 1655 exit(1); 1656 } 1657 if (flock(idmap_fd, LOCK_EX | LOCK_NB) != 0) { 1658 ALOGE("flock(%s) failed during idmap: %s\n", idmap_path, strerror(errno)); 1659 exit(1); 1660 } 1661 1662 run_idmap(target_apk, overlay_apk, idmap_fd); 1663 exit(1); /* only if exec call to idmap failed */ 1664 } else { 1665 int status = wait_child(pid); 1666 if (status != 0) { 1667 ALOGE("idmap failed, status=0x%04x\n", status); 1668 goto fail; 1669 } 1670 } 1671 1672 close(idmap_fd); 1673 return ok(); 1674fail: 1675 if (idmap_fd >= 0) { 1676 close(idmap_fd); 1677 unlink(idmap_path); 1678 } 1679 return error(); 1680} 1681 1682binder::Status InstalldNativeService::restoreconAppData(const std::unique_ptr<std::string>& uuid, 1683 const std::string& packageName, int32_t userId, int32_t flags, int32_t appId, 1684 const std::string& seInfo) { 1685 ENFORCE_UID(AID_SYSTEM); 1686 CHECK_ARGUMENT_UUID(uuid); 1687 CHECK_ARGUMENT_PACKAGE_NAME(packageName); 1688 std::lock_guard<std::recursive_mutex> lock(mLock); 1689 1690 binder::Status res = ok(); 1691 1692 // SELINUX_ANDROID_RESTORECON_DATADATA flag is set by libselinux. Not needed here. 1693 unsigned int seflags = SELINUX_ANDROID_RESTORECON_RECURSE; 1694 const char* uuid_ = uuid ? uuid->c_str() : nullptr; 1695 const char* pkgName = packageName.c_str(); 1696 const char* seinfo = seInfo.c_str(); 1697 1698 uid_t uid = multiuser_get_uid(userId, appId); 1699 if (flags & FLAG_STORAGE_CE) { 1700 auto path = create_data_user_ce_package_path(uuid_, userId, pkgName); 1701 if (selinux_android_restorecon_pkgdir(path.c_str(), seinfo, uid, seflags) < 0) { 1702 res = error("restorecon failed for " + path); 1703 } 1704 } 1705 if (flags & FLAG_STORAGE_DE) { 1706 auto path = create_data_user_de_package_path(uuid_, userId, pkgName); 1707 if (selinux_android_restorecon_pkgdir(path.c_str(), seinfo, uid, seflags) < 0) { 1708 res = error("restorecon failed for " + path); 1709 } 1710 } 1711 return res; 1712} 1713 1714binder::Status InstalldNativeService::createOatDir(const std::string& oatDir, 1715 const std::string& instructionSet) { 1716 ENFORCE_UID(AID_SYSTEM); 1717 std::lock_guard<std::recursive_mutex> lock(mLock); 1718 1719 const char* oat_dir = oatDir.c_str(); 1720 const char* instruction_set = instructionSet.c_str(); 1721 char oat_instr_dir[PKG_PATH_MAX]; 1722 1723 if (validate_apk_path(oat_dir)) { 1724 return error("Invalid path " + oatDir); 1725 } 1726 if (fs_prepare_dir(oat_dir, S_IRWXU | S_IRWXG | S_IXOTH, AID_SYSTEM, AID_INSTALL)) { 1727 return error("Failed to prepare " + oatDir); 1728 } 1729 if (selinux_android_restorecon(oat_dir, 0)) { 1730 return error("Failed to restorecon " + oatDir); 1731 } 1732 snprintf(oat_instr_dir, PKG_PATH_MAX, "%s/%s", oat_dir, instruction_set); 1733 if (fs_prepare_dir(oat_instr_dir, S_IRWXU | S_IRWXG | S_IXOTH, AID_SYSTEM, AID_INSTALL)) { 1734 return error(StringPrintf("Failed to prepare %s", oat_instr_dir)); 1735 } 1736 return ok(); 1737} 1738 1739binder::Status InstalldNativeService::rmPackageDir(const std::string& packageDir) { 1740 ENFORCE_UID(AID_SYSTEM); 1741 std::lock_guard<std::recursive_mutex> lock(mLock); 1742 1743 if (validate_apk_path(packageDir.c_str())) { 1744 return error("Invalid path " + packageDir); 1745 } 1746 if (delete_dir_contents_and_dir(packageDir) != 0) { 1747 return error("Failed to delete " + packageDir); 1748 } 1749 return ok(); 1750} 1751 1752binder::Status InstalldNativeService::linkFile(const std::string& relativePath, 1753 const std::string& fromBase, const std::string& toBase) { 1754 ENFORCE_UID(AID_SYSTEM); 1755 std::lock_guard<std::recursive_mutex> lock(mLock); 1756 1757 const char* relative_path = relativePath.c_str(); 1758 const char* from_base = fromBase.c_str(); 1759 const char* to_base = toBase.c_str(); 1760 char from_path[PKG_PATH_MAX]; 1761 char to_path[PKG_PATH_MAX]; 1762 snprintf(from_path, PKG_PATH_MAX, "%s/%s", from_base, relative_path); 1763 snprintf(to_path, PKG_PATH_MAX, "%s/%s", to_base, relative_path); 1764 1765 if (validate_apk_path_subdirs(from_path)) { 1766 return error(StringPrintf("Invalid from path %s", from_path)); 1767 } 1768 1769 if (validate_apk_path_subdirs(to_path)) { 1770 return error(StringPrintf("Invalid to path %s", to_path)); 1771 } 1772 1773 if (link(from_path, to_path) < 0) { 1774 return error(StringPrintf("Failed to link from %s to %s", from_path, to_path)); 1775 } 1776 1777 return ok(); 1778} 1779 1780binder::Status InstalldNativeService::moveAb(const std::string& apkPath, 1781 const std::string& instructionSet, const std::string& outputPath) { 1782 ENFORCE_UID(AID_SYSTEM); 1783 std::lock_guard<std::recursive_mutex> lock(mLock); 1784 1785 const char* apk_path = apkPath.c_str(); 1786 const char* instruction_set = instructionSet.c_str(); 1787 const char* oat_dir = outputPath.c_str(); 1788 1789 bool success = move_ab(apk_path, instruction_set, oat_dir); 1790 return success ? ok() : error(); 1791} 1792 1793binder::Status InstalldNativeService::deleteOdex(const std::string& apkPath, 1794 const std::string& instructionSet, const std::string& outputPath) { 1795 ENFORCE_UID(AID_SYSTEM); 1796 std::lock_guard<std::recursive_mutex> lock(mLock); 1797 1798 const char* apk_path = apkPath.c_str(); 1799 const char* instruction_set = instructionSet.c_str(); 1800 const char* oat_dir = outputPath.c_str(); 1801 1802 bool res = delete_odex(apk_path, instruction_set, oat_dir); 1803 return res ? ok() : error(); 1804} 1805 1806binder::Status InstalldNativeService::invalidateMounts() { 1807 ENFORCE_UID(AID_SYSTEM); 1808 std::lock_guard<std::recursive_mutex> lock(mLock); 1809 1810 mQuotaDevices.clear(); 1811 1812 std::ifstream in("/proc/mounts"); 1813 if (!in.is_open()) { 1814 return error("Failed to read mounts"); 1815 } 1816 1817 std::string source; 1818 std::string target; 1819 std::string ignored; 1820 struct dqblk dq; 1821 while (!in.eof()) { 1822 std::getline(in, source, ' '); 1823 std::getline(in, target, ' '); 1824 std::getline(in, ignored); 1825 1826 if (source.compare(0, 11, "/dev/block/") == 0) { 1827 if (quotactl(QCMD(Q_GETQUOTA, USRQUOTA), source.c_str(), 0, 1828 reinterpret_cast<char*>(&dq)) == 0) { 1829 LOG(DEBUG) << "Found " << source << " with quota"; 1830 mQuotaDevices[target] = source; 1831 } 1832 } 1833 } 1834 return ok(); 1835} 1836 1837std::string InstalldNativeService::findQuotaDeviceForUuid( 1838 const std::unique_ptr<std::string>& uuid) { 1839 auto path = create_data_path(uuid ? uuid->c_str() : nullptr); 1840 return mQuotaDevices[path]; 1841} 1842 1843} // namespace installd 1844} // namespace android 1845