InstalldNativeService.cpp revision b26786d647b624498c11405075e5223d1853f30a
194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/* 294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood** Copyright 2008, The Android Open Source Project 394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood** 4d93707342a61e66bc3eb2145628158452f577f42Dave Allison** Licensed under the Apache License, Version 2.0 (the "License"); 5d93707342a61e66bc3eb2145628158452f577f42Dave Allison** you may not use this file except in compliance with the License. 6d93707342a61e66bc3eb2145628158452f577f42Dave Allison** You may obtain a copy of the License at 794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood** 8d93707342a61e66bc3eb2145628158452f577f42Dave Allison** http://www.apache.org/licenses/LICENSE-2.0 994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood** 10d93707342a61e66bc3eb2145628158452f577f42Dave Allison** Unless required by applicable law or agreed to in writing, software 11d93707342a61e66bc3eb2145628158452f577f42Dave Allison** distributed under the License is distributed on an "AS IS" BASIS, 12d93707342a61e66bc3eb2145628158452f577f42Dave Allison** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13d93707342a61e66bc3eb2145628158452f577f42Dave Allison** See the License for the specific language governing permissions and 1494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood** limitations under the License. 1594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood*/ 1694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 17f3e30b936ef103dc0f3d8697e0f86ba82b49609eJeff Sharkey#include "InstalldNativeService.h" 1802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe 19466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey#define ATRACE_TAG ATRACE_TAG_PACKAGE_MANAGER 20466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey 2102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <errno.h> 2202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <inttypes.h> 233dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey#include <fstream> 243dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey#include <fts.h> 250354bd0eaf24de533e86885b811f816f1e4d15c8Andreas Gampe#include <regex> 2602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <stdlib.h> 273dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey#include <string.h> 2802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <sys/capability.h> 2902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <sys/file.h> 3002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <sys/resource.h> 313dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey#include <sys/quota.h> 3202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <sys/stat.h> 333dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey#include <sys/statvfs.h> 34cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey#include <sys/types.h> 356a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle#include <sys/wait.h> 36cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey#include <sys/xattr.h> 3702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <unistd.h> 3841ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey 39afa58d175cc3e9adaa4f8df9bc7a8fac5798a50aAndreas Gampe#include <android-base/logging.h> 40e4ec9eb7b4c452493589983970ba5ccc501728d1Elliott Hughes#include <android-base/stringprintf.h> 416727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr#include <android-base/strings.h> 4264b59cc1c89225813295d1b5d6bf4183615da204Roland Levillain#include <android-base/unique_fd.h> 4302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <cutils/fs.h> 4490aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey#include <cutils/properties.h> 450378aaf257aee92539d30543914a50c4481c6a18Brian Carlstrom#include <cutils/sched_policy.h> 467823e124e00576e20e47ec717cbe8bc89f0f2bf2Mark Salyzyn#include <log/log.h> // TODO: Move everything to base/logging. 47e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey#include <logwrap/logwrap.h> 4802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <private/android_filesystem_config.h> 49e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey#include <selinux/android.h> 5002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <system/thread_defs.h> 51466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey#include <utils/Trace.h> 52e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 536c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey#include "dexopt.h" 54f3e30b936ef103dc0f3d8697e0f86ba82b49609eJeff Sharkey#include "globals.h" 55f3e30b936ef103dc0f3d8697e0f86ba82b49609eJeff Sharkey#include "installd_deps.h" 56f3e30b936ef103dc0f3d8697e0f86ba82b49609eJeff Sharkey#include "otapreopt_utils.h" 57f3e30b936ef103dc0f3d8697e0f86ba82b49609eJeff Sharkey#include "utils.h" 586c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey 5988ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey#include "CacheTracker.h" 60df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#include "MatchExtensionGen.h" 61df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 6202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#ifndef LOG_TAG 6302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#define LOG_TAG "installd" 6402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#endif 6541ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey 6641ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkeyusing android::base::StringPrintf; 6766b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkeyusing std::endl; 6894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 6902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampenamespace android { 7002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampenamespace installd { 7194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 72cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkeystatic constexpr const char* kCpPath = "/system/bin/cp"; 73cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkeystatic constexpr const char* kXattrDefault = "user.default"; 74e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 7540bd3eff694ef1c660af2806bcc51bb5ac038c16Janis Danisevskisstatic constexpr const int MIN_RESTRICTED_HOME_SDK_VERSION = 24; // > M 76eecb2d205366351af42fd0cd9e1a95de3980764eJanis Danisevskis 770354bd0eaf24de533e86885b811f816f1e4d15c8Andreas Gampestatic constexpr const char* PKG_LIB_POSTFIX = "/lib"; 780354bd0eaf24de533e86885b811f816f1e4d15c8Andreas Gampestatic constexpr const char* CACHE_DIR_POSTFIX = "/cache"; 790354bd0eaf24de533e86885b811f816f1e4d15c8Andreas Gampestatic constexpr const char* CODE_CACHE_DIR_POSTFIX = "/code_cache"; 800354bd0eaf24de533e86885b811f816f1e4d15c8Andreas Gampe 810354bd0eaf24de533e86885b811f816f1e4d15c8Andreas Gampestatic constexpr const char* IDMAP_PREFIX = "/data/resource-cache/"; 820354bd0eaf24de533e86885b811f816f1e4d15c8Andreas Gampestatic constexpr const char* IDMAP_SUFFIX = "@idmap"; 830354bd0eaf24de533e86885b811f816f1e4d15c8Andreas Gampe 840354bd0eaf24de533e86885b811f816f1e4d15c8Andreas Gampe// NOTE: keep in sync with Installer 850354bd0eaf24de533e86885b811f816f1e4d15c8Andreas Gampestatic constexpr int FLAG_CLEAR_CACHE_ONLY = 1 << 8; 860354bd0eaf24de533e86885b811f816f1e4d15c8Andreas Gampestatic constexpr int FLAG_CLEAR_CODE_CACHE_ONLY = 1 << 9; 873dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkeystatic constexpr int FLAG_USE_QUOTA = 1 << 12; 8888ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkeystatic constexpr int FLAG_FREE_CACHE_V2 = 1 << 13; 89871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkeystatic constexpr int FLAG_FREE_CACHE_V2_DEFY_QUOTA = 1 << 14; 90871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkeystatic constexpr int FLAG_FREE_CACHE_NOOP = 1 << 15; 916a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 920274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeynamespace { 930274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 940274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeyconstexpr const char* kDump = "android.permission.DUMP"; 950274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 96423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkeystatic binder::Status ok() { 97423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return binder::Status::ok(); 98423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey} 99423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 100423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkeystatic binder::Status exception(uint32_t code, const std::string& msg) { 101423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return binder::Status::fromExceptionCode(code, String8(msg.c_str())); 102423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey} 103423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 104423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkeystatic binder::Status error() { 105423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return binder::Status::fromServiceSpecificError(errno); 106423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey} 107423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 108423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkeystatic binder::Status error(const std::string& msg) { 109423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey PLOG(ERROR) << msg; 110423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return binder::Status::fromServiceSpecificError(errno, String8(msg.c_str())); 111423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey} 112423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 113423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkeystatic binder::Status error(uint32_t code, const std::string& msg) { 114423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey LOG(ERROR) << msg << " (" << code << ")"; 115423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return binder::Status::fromServiceSpecificError(code, String8(msg.c_str())); 116423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey} 117423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 1180274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeybinder::Status checkPermission(const char* permission) { 1190274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey pid_t pid; 1200274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey uid_t uid; 1210274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 1220274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey if (checkCallingPermission(String16(permission), reinterpret_cast<int32_t*>(&pid), 1230274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey reinterpret_cast<int32_t*>(&uid))) { 124423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 1250274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey } else { 126423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return exception(binder::Status::EX_SECURITY, 127423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey StringPrintf("UID %d / PID %d lacks permission %s", uid, pid, permission)); 1280274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey } 1290274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey} 1300274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 1310274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeybinder::Status checkUid(uid_t expectedUid) { 1320274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey uid_t uid = IPCThreadState::self()->getCallingUid(); 1330274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey if (uid == expectedUid || uid == AID_ROOT) { 134423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 1350274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey } else { 136423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return exception(binder::Status::EX_SECURITY, 137423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey StringPrintf("UID %d is not expected UID %d", uid, expectedUid)); 138423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 139423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey} 140423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 141423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkeybinder::Status checkArgumentUuid(const std::unique_ptr<std::string>& uuid) { 142423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (!uuid || is_valid_filename(*uuid)) { 143423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 144423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } else { 145423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return exception(binder::Status::EX_ILLEGAL_ARGUMENT, 146423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey StringPrintf("UUID %s is malformed", uuid->c_str())); 147423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 148423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey} 149423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 150423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkeybinder::Status checkArgumentPackageName(const std::string& packageName) { 151423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (is_valid_package_name(packageName.c_str())) { 152423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 153423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } else { 154423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return exception(binder::Status::EX_ILLEGAL_ARGUMENT, 155423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey StringPrintf("Package name %s is malformed", packageName.c_str())); 1560274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey } 1570274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey} 1580274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 1590274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey#define ENFORCE_UID(uid) { \ 1600274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey binder::Status status = checkUid((uid)); \ 1610274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey if (!status.isOk()) { \ 1620274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey return status; \ 1630274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey } \ 1640274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey} 1650274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 166423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey#define CHECK_ARGUMENT_UUID(uuid) { \ 167423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey binder::Status status = checkArgumentUuid((uuid)); \ 168423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (!status.isOk()) { \ 169423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return status; \ 170423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } \ 171423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey} 172423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 173423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey#define CHECK_ARGUMENT_PACKAGE_NAME(packageName) { \ 174423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey binder::Status status = \ 175423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey checkArgumentPackageName((packageName)); \ 176423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (!status.isOk()) { \ 177423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return status; \ 178423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } \ 179423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey} 180423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 1810274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey} // namespace 1820274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 1830274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeystatus_t InstalldNativeService::start() { 1840274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey IPCThreadState::self()->disableBackgroundScheduling(true); 1850274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey status_t ret = BinderService<InstalldNativeService>::publish(); 1860274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey if (ret != android::OK) { 1870274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey return ret; 1880274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey } 1890274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey sp<ProcessState> ps(ProcessState::self()); 1900274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey ps->startThreadPool(); 1910274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey ps->giveThreadPoolName(); 1920274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey return android::OK; 1930274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey} 1940274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 1950274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeystatus_t InstalldNativeService::dump(int fd, const Vector<String16> & /* args */) { 19666b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey auto out = std::fstream(StringPrintf("/proc/self/fd/%d", fd)); 1970274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const binder::Status dump_permission = checkPermission(kDump); 1980274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey if (!dump_permission.isOk()) { 19966b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey out << dump_permission.toString8() << endl; 2000274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey return PERMISSION_DENIED; 2010274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey } 20266b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 20366b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey 20488ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey out << "installd is happy!" << endl; 20588ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey 206b26786d647b624498c11405075e5223d1853f30aJeff Sharkey { 207b26786d647b624498c11405075e5223d1853f30aJeff Sharkey std::lock_guard<std::recursive_mutex> lock(mQuotaDevicesLock); 208b26786d647b624498c11405075e5223d1853f30aJeff Sharkey out << endl << "Devices with quota support:" << endl; 209b26786d647b624498c11405075e5223d1853f30aJeff Sharkey for (const auto& n : mQuotaDevices) { 210b26786d647b624498c11405075e5223d1853f30aJeff Sharkey out << " " << n.first << " = " << n.second << endl; 211b26786d647b624498c11405075e5223d1853f30aJeff Sharkey } 21266b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey } 21388ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey 214b26786d647b624498c11405075e5223d1853f30aJeff Sharkey { 215b26786d647b624498c11405075e5223d1853f30aJeff Sharkey std::lock_guard<std::recursive_mutex> lock(mCacheQuotasLock); 216b26786d647b624498c11405075e5223d1853f30aJeff Sharkey out << endl << "Per-UID cache quotas:" << endl; 217b26786d647b624498c11405075e5223d1853f30aJeff Sharkey for (const auto& n : mCacheQuotas) { 218b26786d647b624498c11405075e5223d1853f30aJeff Sharkey out << " " << n.first << " = " << n.second << endl; 219b26786d647b624498c11405075e5223d1853f30aJeff Sharkey } 22088ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey } 22188ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey 22266b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey out << endl; 22366b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey out.flush(); 2240274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 2250274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey return NO_ERROR; 2260274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey} 2270274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 22822f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey/** 22922f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey * Perform restorecon of the given path, but only perform recursive restorecon 23022f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey * if the label of that top-level file actually changed. This can save us 23122f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey * significant time by avoiding no-op traversals of large filesystem trees. 23222f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey */ 23335b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkeystatic int restorecon_app_data_lazy(const std::string& path, const std::string& seInfo, uid_t uid, 23435b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey bool existing) { 23522f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey int res = 0; 23622f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey char* before = nullptr; 23722f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey char* after = nullptr; 23822f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey 23922f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey // Note that SELINUX_ANDROID_RESTORECON_DATADATA flag is set by 24022f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey // libselinux. Not needed here. 24122f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey 2428567ebfaa428d6454431067da68765adde328800Jeff Sharkey if (lgetfilecon(path.c_str(), &before) < 0) { 24322f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey PLOG(ERROR) << "Failed before getfilecon for " << path; 24422f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey goto fail; 24522f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey } 2460274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey if (selinux_android_restorecon_pkgdir(path.c_str(), seInfo.c_str(), uid, 0) < 0) { 24722f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey PLOG(ERROR) << "Failed top-level restorecon for " << path; 24822f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey goto fail; 24922f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey } 2508567ebfaa428d6454431067da68765adde328800Jeff Sharkey if (lgetfilecon(path.c_str(), &after) < 0) { 25122f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey PLOG(ERROR) << "Failed after getfilecon for " << path; 25222f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey goto fail; 25322f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey } 25422f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey 25522f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey // If the initial top-level restorecon above changed the label, then go 25622f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey // back and restorecon everything recursively 25722f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey if (strcmp(before, after)) { 25835b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey if (existing) { 25935b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey LOG(DEBUG) << "Detected label change from " << before << " to " << after << " at " 26035b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey << path << "; running recursive restorecon"; 26135b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey } 2620274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey if (selinux_android_restorecon_pkgdir(path.c_str(), seInfo.c_str(), uid, 26322f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey SELINUX_ANDROID_RESTORECON_RECURSE) < 0) { 26422f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey PLOG(ERROR) << "Failed recursive restorecon for " << path; 26522f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey goto fail; 26622f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey } 26722f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey } 26822f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey 26922f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey goto done; 27022f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkeyfail: 27122f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey res = -1; 27222f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkeydone: 27322f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey free(before); 27422f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey free(after); 27522f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey return res; 27622f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey} 27722f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey 2780274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeystatic int restorecon_app_data_lazy(const std::string& parent, const char* name, 27935b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey const std::string& seInfo, uid_t uid, bool existing) { 28035b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey return restorecon_app_data_lazy(StringPrintf("%s/%s", parent.c_str(), name), seInfo, uid, 28135b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey existing); 2828567ebfaa428d6454431067da68765adde328800Jeff Sharkey} 2838567ebfaa428d6454431067da68765adde328800Jeff Sharkey 28422f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkeystatic int prepare_app_dir(const std::string& path, mode_t target_mode, uid_t uid) { 2851f6a7f117dcfa0e2cdb870a1bd4b20d34363ad73Jeff Sharkey if (fs_prepare_dir_strict(path.c_str(), target_mode, uid, uid) != 0) { 2861f6a7f117dcfa0e2cdb870a1bd4b20d34363ad73Jeff Sharkey PLOG(ERROR) << "Failed to prepare " << path; 2871f6a7f117dcfa0e2cdb870a1bd4b20d34363ad73Jeff Sharkey return -1; 2881f6a7f117dcfa0e2cdb870a1bd4b20d34363ad73Jeff Sharkey } 2891f6a7f117dcfa0e2cdb870a1bd4b20d34363ad73Jeff Sharkey return 0; 2901f6a7f117dcfa0e2cdb870a1bd4b20d34363ad73Jeff Sharkey} 2911f6a7f117dcfa0e2cdb870a1bd4b20d34363ad73Jeff Sharkey 2920274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeybinder::Status InstalldNativeService::createAppData(const std::unique_ptr<std::string>& uuid, 2930274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const std::string& packageName, int32_t userId, int32_t flags, int32_t appId, 29436a900a0db526d63e899ed5d5b0a463ad7f9236eJeff Sharkey const std::string& seInfo, int32_t targetSdkVersion, int64_t* _aidl_return) { 2950274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 296423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 297423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 2987a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 2990274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 3000274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 3010274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const char* pkgname = packageName.c_str(); 3020274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 30336a900a0db526d63e899ed5d5b0a463ad7f9236eJeff Sharkey // Assume invalid inode unless filled in below 30436a900a0db526d63e899ed5d5b0a463ad7f9236eJeff Sharkey if (_aidl_return != nullptr) *_aidl_return = -1; 30536a900a0db526d63e899ed5d5b0a463ad7f9236eJeff Sharkey 30624ef15bd009204b73ebf0e94ba3d868177a5f44cJeff Sharkey int32_t uid = multiuser_get_uid(userId, appId); 30724ef15bd009204b73ebf0e94ba3d868177a5f44cJeff Sharkey int32_t cacheGid = multiuser_get_cache_gid(userId, appId); 3083dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey mode_t targetMode = targetSdkVersion >= MIN_RESTRICTED_HOME_SDK_VERSION ? 0700 : 0751; 3093dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 31024ef15bd009204b73ebf0e94ba3d868177a5f44cJeff Sharkey // If UID doesn't have a specific cache GID, use UID value 31124ef15bd009204b73ebf0e94ba3d868177a5f44cJeff Sharkey if (cacheGid == -1) { 31224ef15bd009204b73ebf0e94ba3d868177a5f44cJeff Sharkey cacheGid = uid; 31324ef15bd009204b73ebf0e94ba3d868177a5f44cJeff Sharkey } 31424ef15bd009204b73ebf0e94ba3d868177a5f44cJeff Sharkey 315aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (flags & FLAG_STORAGE_CE) { 3160274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey auto path = create_data_user_ce_package_path(uuid_, userId, pkgname); 31735b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey bool existing = (access(path.c_str(), F_OK) == 0); 31835b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey 3193dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (prepare_app_dir(path, targetMode, uid) || 3203dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey prepare_app_cache_dir(path, "cache", 02771, uid, cacheGid) || 3213dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey prepare_app_cache_dir(path, "code_cache", 02771, uid, cacheGid)) { 322423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to prepare " + path); 32322f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey } 32422f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey 32522f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey // Consider restorecon over contents if label changed 32635b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey if (restorecon_app_data_lazy(path, seInfo, uid, existing) || 32735b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey restorecon_app_data_lazy(path, "cache", seInfo, uid, existing) || 32835b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey restorecon_app_data_lazy(path, "code_cache", seInfo, uid, existing)) { 329423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to restorecon " + path); 330c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey } 3311f6a7f117dcfa0e2cdb870a1bd4b20d34363ad73Jeff Sharkey 3321f6a7f117dcfa0e2cdb870a1bd4b20d34363ad73Jeff Sharkey // Remember inode numbers of cache directories so that we can clear 3331f6a7f117dcfa0e2cdb870a1bd4b20d34363ad73Jeff Sharkey // contents while CE storage is locked 3341f6a7f117dcfa0e2cdb870a1bd4b20d34363ad73Jeff Sharkey if (write_path_inode(path, "cache", kXattrInodeCache) || 3351f6a7f117dcfa0e2cdb870a1bd4b20d34363ad73Jeff Sharkey write_path_inode(path, "code_cache", kXattrInodeCodeCache)) { 336423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to write_path_inode for " + path); 337c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey } 33836a900a0db526d63e899ed5d5b0a463ad7f9236eJeff Sharkey 33936a900a0db526d63e899ed5d5b0a463ad7f9236eJeff Sharkey // And return the CE inode of the top-level data directory so we can 34036a900a0db526d63e899ed5d5b0a463ad7f9236eJeff Sharkey // clear contents while CE storage is locked 34136a900a0db526d63e899ed5d5b0a463ad7f9236eJeff Sharkey if ((_aidl_return != nullptr) 34236a900a0db526d63e899ed5d5b0a463ad7f9236eJeff Sharkey && get_path_inode(path, reinterpret_cast<ino_t*>(_aidl_return)) != 0) { 34336a900a0db526d63e899ed5d5b0a463ad7f9236eJeff Sharkey return error("Failed to get_path_inode for " + path); 34436a900a0db526d63e899ed5d5b0a463ad7f9236eJeff Sharkey } 34594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 346aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (flags & FLAG_STORAGE_DE) { 3470274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey auto path = create_data_user_de_package_path(uuid_, userId, pkgname); 34835b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey bool existing = (access(path.c_str(), F_OK) == 0); 34935b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey 3503dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (prepare_app_dir(path, targetMode, uid) || 3513dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey prepare_app_cache_dir(path, "cache", 02771, uid, cacheGid) || 3523dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey prepare_app_cache_dir(path, "code_cache", 02771, uid, cacheGid)) { 353423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to prepare " + path); 354c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey } 3556a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 35622f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey // Consider restorecon over contents if label changed 35735b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey if (restorecon_app_data_lazy(path, seInfo, uid, existing)) { 358423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to restorecon " + path); 35922f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey } 36022f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey 36190aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey if (property_get_bool("dalvik.vm.usejitprofiles", false)) { 3620274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const std::string profile_path = create_data_user_profile_package_path(userId, pkgname); 3636a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle // read-write-execute only for the app user. 3646a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle if (fs_prepare_dir_strict(profile_path.c_str(), 0700, uid, uid) != 0) { 365423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to prepare " + profile_path); 3666a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 367bc56e7dcf07e633867e1525435373a1231216b22Calin Juravle std::string profile_file = create_primary_profile(profile_path); 368bc56e7dcf07e633867e1525435373a1231216b22Calin Juravle // read-write only for the app user. 369bc56e7dcf07e633867e1525435373a1231216b22Calin Juravle if (fs_prepare_file_strict(profile_file.c_str(), 0600, uid, uid) != 0) { 370423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to prepare " + profile_path); 371bc56e7dcf07e633867e1525435373a1231216b22Calin Juravle } 3726a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle const std::string ref_profile_path = create_data_ref_profile_package_path(pkgname); 3736a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle // dex2oat/profman runs under the shared app gid and it needs to read/write reference 3746a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle // profiles. 375adddd9876e145879169369913b5afe469b17c02dJeff Sharkey int shared_app_gid = multiuser_get_shared_gid(0, appId); 3762e6dc9baaaf5d50b42ce36435e13c2559808b837Jeff Sharkey if ((shared_app_gid != -1) && fs_prepare_dir_strict( 3776a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle ref_profile_path.c_str(), 0700, shared_app_gid, shared_app_gid) != 0) { 378423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to prepare " + ref_profile_path); 3796a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 3806a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 38194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 382423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 38394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 38494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 385c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkeybinder::Status InstalldNativeService::migrateAppData(const std::unique_ptr<std::string>& uuid, 386c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const std::string& packageName, int32_t userId, int32_t flags) { 387c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 388423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 389423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 3907a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 391423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 392c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 393c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* pkgname = packageName.c_str(); 394c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey 395cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey // This method only exists to upgrade system apps that have requested 396cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey // forceDeviceEncrypted, so their default storage always lives in a 397cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey // consistent location. This only works on non-FBE devices, since we 398cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey // never want to risk exposing data on a device with real CE/DE storage. 399cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey 400c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey auto ce_path = create_data_user_ce_package_path(uuid_, userId, pkgname); 401c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey auto de_path = create_data_user_de_package_path(uuid_, userId, pkgname); 402cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey 403cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey // If neither directory is marked as default, assume CE is default 404cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey if (getxattr(ce_path.c_str(), kXattrDefault, nullptr, 0) == -1 405cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey && getxattr(de_path.c_str(), kXattrDefault, nullptr, 0) == -1) { 406cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey if (setxattr(ce_path.c_str(), kXattrDefault, nullptr, 0, 0) != 0) { 407423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to mark default storage " + ce_path); 408cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey } 409cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey } 410cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey 411cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey // Migrate default data location if needed 412cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey auto target = (flags & FLAG_STORAGE_DE) ? de_path : ce_path; 413cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey auto source = (flags & FLAG_STORAGE_DE) ? ce_path : de_path; 414cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey 415cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey if (getxattr(target.c_str(), kXattrDefault, nullptr, 0) == -1) { 416cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey LOG(WARNING) << "Requested default storage " << target 417cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey << " is not active; migrating from " << source; 418cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey if (delete_dir_contents_and_dir(target) != 0) { 419423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to delete " + target); 420cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey } 421cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey if (rename(source.c_str(), target.c_str()) != 0) { 422423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to rename " + source + " to " + target); 423cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey } 424cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey } 425cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey 426423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 427cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey} 428cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey 4296a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 430475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::clearAppProfiles(const std::string& packageName) { 431475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 432423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 4337a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 434423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 435475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* pkgname = packageName.c_str(); 436423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey binder::Status res = ok(); 437423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (!clear_reference_profile(pkgname)) { 438423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to clear reference profile for " + packageName); 439423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 440423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (!clear_current_profiles(pkgname)) { 441423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to clear current profiles for " + packageName); 442423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 443423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return res; 4446a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle} 4456a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 446c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkeybinder::Status InstalldNativeService::clearAppData(const std::unique_ptr<std::string>& uuid, 447c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const std::string& packageName, int32_t userId, int32_t flags, int64_t ceDataInode) { 448c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 449423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 450423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 4517a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 452423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 453c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 454c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* pkgname = packageName.c_str(); 455c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey 456423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey binder::Status res = ok(); 457aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (flags & FLAG_STORAGE_CE) { 458c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey auto path = create_data_user_ce_package_path(uuid_, userId, pkgname, ceDataInode); 4591f6a7f117dcfa0e2cdb870a1bd4b20d34363ad73Jeff Sharkey if (flags & FLAG_CLEAR_CACHE_ONLY) { 4601f6a7f117dcfa0e2cdb870a1bd4b20d34363ad73Jeff Sharkey path = read_path_inode(path, "cache", kXattrInodeCache); 4611f6a7f117dcfa0e2cdb870a1bd4b20d34363ad73Jeff Sharkey } else if (flags & FLAG_CLEAR_CODE_CACHE_ONLY) { 4621f6a7f117dcfa0e2cdb870a1bd4b20d34363ad73Jeff Sharkey path = read_path_inode(path, "code_cache", kXattrInodeCodeCache); 4631f6a7f117dcfa0e2cdb870a1bd4b20d34363ad73Jeff Sharkey } 464c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey if (access(path.c_str(), F_OK) == 0) { 465423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (delete_dir_contents(path) != 0) { 466423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to delete contents of " + path); 467423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 468c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey } 46994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 470aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (flags & FLAG_STORAGE_DE) { 4711f6a7f117dcfa0e2cdb870a1bd4b20d34363ad73Jeff Sharkey std::string suffix = ""; 4721f6a7f117dcfa0e2cdb870a1bd4b20d34363ad73Jeff Sharkey bool only_cache = false; 4731f6a7f117dcfa0e2cdb870a1bd4b20d34363ad73Jeff Sharkey if (flags & FLAG_CLEAR_CACHE_ONLY) { 4741f6a7f117dcfa0e2cdb870a1bd4b20d34363ad73Jeff Sharkey suffix = CACHE_DIR_POSTFIX; 4751f6a7f117dcfa0e2cdb870a1bd4b20d34363ad73Jeff Sharkey only_cache = true; 4761f6a7f117dcfa0e2cdb870a1bd4b20d34363ad73Jeff Sharkey } else if (flags & FLAG_CLEAR_CODE_CACHE_ONLY) { 4771f6a7f117dcfa0e2cdb870a1bd4b20d34363ad73Jeff Sharkey suffix = CODE_CACHE_DIR_POSTFIX; 4781f6a7f117dcfa0e2cdb870a1bd4b20d34363ad73Jeff Sharkey only_cache = true; 4791f6a7f117dcfa0e2cdb870a1bd4b20d34363ad73Jeff Sharkey } 4801f6a7f117dcfa0e2cdb870a1bd4b20d34363ad73Jeff Sharkey 481c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey auto path = create_data_user_de_package_path(uuid_, userId, pkgname) + suffix; 482c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey if (access(path.c_str(), F_OK) == 0) { 483423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (delete_dir_contents(path) != 0) { 484423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to delete contents of " + path); 485423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 486c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey } 487edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle if (!only_cache) { 488c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey if (!clear_current_profile(pkgname, userId)) { 489423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to clear current profile for " + packageName); 490edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle } 491edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle } 49294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 493423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return res; 494c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey} 49594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 4967535e8e1995398befc866062d63c05d90eb70cccCalin Juravlestatic int destroy_app_reference_profile(const char *pkgname) { 4977535e8e1995398befc866062d63c05d90eb70cccCalin Juravle return delete_dir_contents_and_dir( 4987535e8e1995398befc866062d63c05d90eb70cccCalin Juravle create_data_ref_profile_package_path(pkgname), 4997535e8e1995398befc866062d63c05d90eb70cccCalin Juravle /*ignore_if_missing*/ true); 5007535e8e1995398befc866062d63c05d90eb70cccCalin Juravle} 5017535e8e1995398befc866062d63c05d90eb70cccCalin Juravle 502edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravlestatic int destroy_app_current_profiles(const char *pkgname, userid_t userid) { 503b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravle return delete_dir_contents_and_dir( 504b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravle create_data_user_profile_package_path(userid, pkgname), 505b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravle /*ignore_if_missing*/ true); 506edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle} 507edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle 508475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::destroyAppProfiles(const std::string& packageName) { 509475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 510423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 5117a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 512423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 513475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* pkgname = packageName.c_str(); 514423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey binder::Status res = ok(); 515edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle std::vector<userid_t> users = get_known_users(/*volume_uuid*/ nullptr); 516edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle for (auto user : users) { 517423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (destroy_app_current_profiles(pkgname, user) != 0) { 518423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to destroy current profiles for " + packageName); 519423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 520edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle } 521423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (destroy_app_reference_profile(pkgname) != 0) { 522423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to destroy reference profile for " + packageName); 523423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 524423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return res; 525caa6b809a5ed024a8f3f551c5c109a5ad50ab772Calin Juravle} 526caa6b809a5ed024a8f3f551c5c109a5ad50ab772Calin Juravle 527c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkeybinder::Status InstalldNativeService::destroyAppData(const std::unique_ptr<std::string>& uuid, 528c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const std::string& packageName, int32_t userId, int32_t flags, int64_t ceDataInode) { 529c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 530423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 531423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 5327a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 533423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 534c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 535c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* pkgname = packageName.c_str(); 536c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey 537423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey binder::Status res = ok(); 538aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (flags & FLAG_STORAGE_CE) { 539423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey auto path = create_data_user_ce_package_path(uuid_, userId, pkgname, ceDataInode); 540423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (delete_dir_contents_and_dir(path) != 0) { 541423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to delete " + path); 542423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 54394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 544aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (flags & FLAG_STORAGE_DE) { 545423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey auto path = create_data_user_de_package_path(uuid_, userId, pkgname); 546423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (delete_dir_contents_and_dir(path) != 0) { 547423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to delete " + path); 548423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 549c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey destroy_app_current_profiles(pkgname, userId); 5507535e8e1995398befc866062d63c05d90eb70cccCalin Juravle // TODO(calin): If the package is still installed by other users it's probably 5517535e8e1995398befc866062d63c05d90eb70cccCalin Juravle // beneficial to keep the reference profile around. 5527535e8e1995398befc866062d63c05d90eb70cccCalin Juravle // Verify if it's ok to do that. 5537535e8e1995398befc866062d63c05d90eb70cccCalin Juravle destroy_app_reference_profile(pkgname); 55494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 555423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return res; 55694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 55794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 5580274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeybinder::Status InstalldNativeService::moveCompleteApp(const std::unique_ptr<std::string>& fromUuid, 5590274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const std::unique_ptr<std::string>& toUuid, const std::string& packageName, 5600274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const std::string& dataAppName, int32_t appId, const std::string& seInfo, 5610274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey int32_t targetSdkVersion) { 562423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 563423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(fromUuid); 564423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(toUuid); 565423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 5667a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 5670274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 5680274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const char* from_uuid = fromUuid ? fromUuid->c_str() : nullptr; 5690274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const char* to_uuid = toUuid ? toUuid->c_str() : nullptr; 5700274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const char* package_name = packageName.c_str(); 5710274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const char* data_app_name = dataAppName.c_str(); 5720274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 573423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey binder::Status res = ok(); 574e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey std::vector<userid_t> users = get_known_users(from_uuid); 575e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 576d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey // Copy app 577d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey { 57851c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto from = create_data_app_package_path(from_uuid, data_app_name); 57951c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto to = create_data_app_package_path(to_uuid, data_app_name); 58051c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto to_parent = create_data_app_path(to_uuid); 581d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey 582d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey char *argv[] = { 583d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey (char*) kCpPath, 584d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey (char*) "-F", /* delete any existing destination file first (--remove-destination) */ 585d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey (char*) "-p", /* preserve timestamps, ownership, and permissions */ 586d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey (char*) "-R", /* recurse into subdirectories (DEST must be a directory) */ 587d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey (char*) "-P", /* Do not follow symlinks [default] */ 588d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey (char*) "-d", /* don't dereference symlinks */ 589d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey (char*) from.c_str(), 590d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey (char*) to_parent.c_str() 591d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey }; 592d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey 593d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey LOG(DEBUG) << "Copying " << from << " to " << to; 594d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true); 595d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey if (rc != 0) { 596423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error(rc, "Failed copying " + from + " to " + to); 597d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey goto fail; 598d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey } 599d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey 600d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey if (selinux_android_restorecon(to.c_str(), SELINUX_ANDROID_RESTORECON_RECURSE) != 0) { 601423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to restorecon " + to); 602d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey goto fail; 603d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey } 604d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey } 605d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey 606d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey // Copy private data for all known users 607e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey for (auto user : users) { 608e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 609e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey // Data source may not exist for all users; that's okay 61051c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto from_ce = create_data_user_ce_package_path(from_uuid, user, package_name); 61151c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey if (access(from_ce.c_str(), F_OK) != 0) { 61251c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey LOG(INFO) << "Missing source " << from_ce; 613e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey continue; 614e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey } 615e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 6160274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey if (!createAppData(toUuid, packageName, user, FLAG_STORAGE_CE | FLAG_STORAGE_DE, appId, 61736a900a0db526d63e899ed5d5b0a463ad7f9236eJeff Sharkey seInfo, targetSdkVersion, nullptr).isOk()) { 618423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to create package target"); 619e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey goto fail; 620e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey } 621e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 622e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey char *argv[] = { 623e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey (char*) kCpPath, 624e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey (char*) "-F", /* delete any existing destination file first (--remove-destination) */ 625e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey (char*) "-p", /* preserve timestamps, ownership, and permissions */ 626e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey (char*) "-R", /* recurse into subdirectories (DEST must be a directory) */ 627e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey (char*) "-P", /* Do not follow symlinks [default] */ 628e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey (char*) "-d", /* don't dereference symlinks */ 62951c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey nullptr, 63051c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey nullptr 631e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey }; 632e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 63351c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey { 63451c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto from = create_data_user_de_package_path(from_uuid, user, package_name); 63551c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto to = create_data_user_de_path(to_uuid, user); 63651c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey argv[6] = (char*) from.c_str(); 63751c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey argv[7] = (char*) to.c_str(); 63851c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey 63951c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey LOG(DEBUG) << "Copying " << from << " to " << to; 64051c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true); 64151c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey if (rc != 0) { 642423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error(rc, "Failed copying " + from + " to " + to); 64351c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey goto fail; 64451c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey } 64551c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey } 64651c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey { 64751c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto from = create_data_user_ce_package_path(from_uuid, user, package_name); 64851c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto to = create_data_user_ce_path(to_uuid, user); 64951c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey argv[6] = (char*) from.c_str(); 65051c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey argv[7] = (char*) to.c_str(); 65151c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey 65251c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey LOG(DEBUG) << "Copying " << from << " to " << to; 65351c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true); 65451c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey if (rc != 0) { 655423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error(rc, "Failed copying " + from + " to " + to); 65651c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey goto fail; 65751c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey } 658e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey } 659e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 660c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey if (!restoreconAppData(toUuid, packageName, user, FLAG_STORAGE_CE | FLAG_STORAGE_DE, 661c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey appId, seInfo).isOk()) { 662423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to restorecon"); 663c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey goto fail; 664c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey } 665e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey } 666e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 66731f08986f83fa6f2dcf55523b2cf706460aeed7cJeff Sharkey // We let the framework scan the new location and persist that before 66831f08986f83fa6f2dcf55523b2cf706460aeed7cJeff Sharkey // deleting the data in the old location; this ordering ensures that 66931f08986f83fa6f2dcf55523b2cf706460aeed7cJeff Sharkey // we can recover from things like battery pulls. 670423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 671e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 672e36372423000a906bafae68844ebc6c42d09335aJeff Sharkeyfail: 673e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey // Nuke everything we might have already copied 674d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey { 67551c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto to = create_data_app_package_path(to_uuid, data_app_name); 676d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey if (delete_dir_contents(to.c_str(), 1, NULL) != 0) { 677d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey LOG(WARNING) << "Failed to rollback " << to; 678d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey } 679d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey } 680e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey for (auto user : users) { 68151c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey { 68251c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto to = create_data_user_de_package_path(to_uuid, user, package_name); 68351c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey if (delete_dir_contents(to.c_str(), 1, NULL) != 0) { 68451c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey LOG(WARNING) << "Failed to rollback " << to; 68551c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey } 68651c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey } 68751c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey { 68851c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto to = create_data_user_ce_package_path(to_uuid, user, package_name); 68951c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey if (delete_dir_contents(to.c_str(), 1, NULL) != 0) { 69051c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey LOG(WARNING) << "Failed to rollback " << to; 69151c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey } 692e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey } 693e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey } 694423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return res; 695e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey} 696e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 697c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkeybinder::Status InstalldNativeService::createUserData(const std::unique_ptr<std::string>& uuid, 698c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey int32_t userId, int32_t userSerial ATTRIBUTE_UNUSED, int32_t flags) { 699c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 700423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 7017a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 702423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 703c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 704379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey if (flags & FLAG_STORAGE_DE) { 705c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey if (uuid_ == nullptr) { 706423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (ensure_config_user_dirs(userId) != 0) { 7077be5ead81aa4d3b2536556c762bab03445a98155Jeff Sharkey return error(StringPrintf("Failed to ensure dirs for %d", userId)); 708423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 709379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey } 71094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 7117be5ead81aa4d3b2536556c762bab03445a98155Jeff Sharkey return ok(); 712095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee} 713095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee 714c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkeybinder::Status InstalldNativeService::destroyUserData(const std::unique_ptr<std::string>& uuid, 715c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey int32_t userId, int32_t flags) { 716c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 717423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 7187a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 719423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 720c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 721423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey binder::Status res = ok(); 722379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey if (flags & FLAG_STORAGE_DE) { 723423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey auto path = create_data_user_de_path(uuid_, userId); 724423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (delete_dir_contents_and_dir(path, true) != 0) { 725423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to delete " + path); 726423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 727c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey if (uuid_ == nullptr) { 728423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey path = create_data_misc_legacy_path(userId); 729423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (delete_dir_contents_and_dir(path, true) != 0) { 730423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to delete " + path); 731423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 732df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey path = create_data_user_profile_path(userId); 733423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (delete_dir_contents_and_dir(path, true) != 0) { 734423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to delete " + path); 735423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 73641ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey } 73794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 738379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey if (flags & FLAG_STORAGE_CE) { 739423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey auto path = create_data_user_ce_path(uuid_, userId); 740423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (delete_dir_contents_and_dir(path, true) != 0) { 741423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to delete " + path); 742423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 743423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey path = create_data_media_path(uuid_, userId); 744423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (delete_dir_contents_and_dir(path, true) != 0) { 745423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to delete " + path); 746423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 747379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey } 748423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return res; 74994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 75094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 75194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/* Try to ensure free_size bytes of storage are available. 75294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * Returns 0 on success. 75394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * This is rather simple-minded because doing a full LRU would 75494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * be potentially memory-intensive, and without atime it would 75594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * also require that apps constantly modify file metadata even 75694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * when just reading from the cache, which is pretty awful. 75794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */ 758475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::freeCache(const std::unique_ptr<std::string>& uuid, 75988ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey int64_t freeStorageSize, int32_t flags) { 760475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 761423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 7627a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 763423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 76488ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 765475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey auto data_path = create_data_path(uuid_); 76688ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey auto device = findQuotaDeviceForUuid(uuid); 76788ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey auto noop = (flags & FLAG_FREE_CACHE_NOOP); 76841ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey 76988ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey int64_t free = data_disk_free(data_path); 77088ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey int64_t needed = freeStorageSize - free; 77188ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey if (free < 0) { 772423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to determine free space for " + data_path); 77388ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey } else if (free >= freeStorageSize) { 774423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 775475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey } 77694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 77788ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey LOG(DEBUG) << "Found " << data_path << " with " << free << " free; caller requested " 77888ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey << freeStorageSize; 77988ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey 78088ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey if (flags & FLAG_FREE_CACHE_V2) { 78188ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey // This new cache strategy fairly removes files from UIDs by deleting 78288ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey // files from the UIDs which are most over their allocated quota 78388ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey 78488ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey // 1. Create trackers for every known UID 78588ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey ATRACE_BEGIN("create"); 78688ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey std::unordered_map<uid_t, std::shared_ptr<CacheTracker>> trackers; 78788ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey for (auto user : get_known_users(uuid_)) { 78888ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey FTS *fts; 78988ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey FTSENT *p; 79088ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey char *argv[] = { 79188ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey (char*) create_data_user_ce_path(uuid_, user).c_str(), 79288ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey (char*) create_data_user_de_path(uuid_, user).c_str(), 79388ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey nullptr 79488ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey }; 795b26786d647b624498c11405075e5223d1853f30aJeff Sharkey if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL))) { 79688ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey return error("Failed to fts_open"); 79788ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey } 79888ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey while ((p = fts_read(fts)) != NULL) { 79988ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey if (p->fts_info == FTS_D && p->fts_level == 1) { 80088ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey uid_t uid = p->fts_statp->st_uid; 80188ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey auto search = trackers.find(uid); 80288ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey if (search != trackers.end()) { 80388ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey search->second->addDataPath(p->fts_path); 80488ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey } else { 80588ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey auto tracker = std::shared_ptr<CacheTracker>(new CacheTracker( 80688ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey multiuser_get_user_id(uid), multiuser_get_app_id(uid), device)); 80788ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey tracker->addDataPath(p->fts_path); 808b26786d647b624498c11405075e5223d1853f30aJeff Sharkey { 809b26786d647b624498c11405075e5223d1853f30aJeff Sharkey std::lock_guard<std::recursive_mutex> lock(mCacheQuotasLock); 810b26786d647b624498c11405075e5223d1853f30aJeff Sharkey tracker->cacheQuota = mCacheQuotas[uid]; 811b26786d647b624498c11405075e5223d1853f30aJeff Sharkey } 81288ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey if (tracker->cacheQuota == 0) { 81388ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey LOG(WARNING) << "UID " << uid << " has no cache quota; assuming 64MB"; 81488ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey tracker->cacheQuota = 67108864; 81588ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey } 81688ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey trackers[uid] = tracker; 81788ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey } 81888ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey fts_set(fts, p, FTS_SKIP); 81988ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey } 82088ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey } 82188ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey fts_close(fts); 82288ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey } 82388ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey ATRACE_END(); 82488ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey 82588ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey // 2. Populate tracker stats and insert into priority queue 82688ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey ATRACE_BEGIN("populate"); 82788ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey auto cmp = [](std::shared_ptr<CacheTracker> left, std::shared_ptr<CacheTracker> right) { 82888ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey return (left->getCacheRatio() < right->getCacheRatio()); 82988ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey }; 83088ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey std::priority_queue<std::shared_ptr<CacheTracker>, 83188ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey std::vector<std::shared_ptr<CacheTracker>>, decltype(cmp)> queue(cmp); 83288ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey for (const auto& it : trackers) { 83388ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey it.second->loadStats(); 83488ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey queue.push(it.second); 83588ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey } 83688ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey ATRACE_END(); 83788ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey 83888ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey // 3. Bounce across the queue, freeing items from whichever tracker is 83988ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey // the most over their assigned quota 84088ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey ATRACE_BEGIN("bounce"); 84188ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey std::shared_ptr<CacheTracker> active; 84288ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey while (active || !queue.empty()) { 843871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey // Only look at apps under quota when explicitly requested 844871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey if (active && (active->getCacheRatio() < 10000) 845871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey && !(flags & FLAG_FREE_CACHE_V2_DEFY_QUOTA)) { 846871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey LOG(DEBUG) << "Active ratio " << active->getCacheRatio() 847871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey << " isn't over quota, and defy not requested"; 848871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey break; 849871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey } 850871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey 85188ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey // Find the best tracker to work with; this might involve swapping 85288ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey // if the active tracker is no longer the most over quota 85388ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey bool nextBetter = active && !queue.empty() 85488ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey && active->getCacheRatio() < queue.top()->getCacheRatio(); 85588ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey if (!active || nextBetter) { 85688ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey if (active) { 85788ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey // Current tracker still has items, so we'll consider it 85888ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey // again later once it bubbles up to surface 85988ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey queue.push(active); 86088ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey } 86188ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey active = queue.top(); queue.pop(); 86288ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey active->ensureItems(); 86388ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey continue; 86488ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey } 86594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 86688ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey // If no items remain, go find another tracker 86788ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey if (active->items.empty()) { 86888ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey active = nullptr; 86988ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey continue; 87088ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey } else { 87188ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey auto item = active->items.back(); 87288ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey active->items.pop_back(); 87388ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey 87488ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey LOG(DEBUG) << "Purging " << item->toString() << " from " << active->toString(); 87588ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey if (!noop) { 87688ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey item->purge(); 87788ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey } 87888ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey active->cacheUsed -= item->size; 87988ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey needed -= item->size; 88088ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey } 88188ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey 88288ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey // Verify that we're actually done before bailing, since sneaky 88388ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey // apps might be using hardlinks 88488ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey if (needed <= 0) { 88588ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey free = data_disk_free(data_path); 88688ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey needed = freeStorageSize - free; 88788ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey if (needed <= 0) { 88888ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey break; 88988ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey } else { 89088ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey LOG(WARNING) << "Expected to be done but still need " << needed; 89188ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey } 89288ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey } 89388ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey } 89488ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey ATRACE_END(); 89588ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey 89688ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey } else { 89788ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey ATRACE_BEGIN("start"); 89888ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey cache_t* cache = start_cache_collection(); 89988ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey ATRACE_END(); 90088ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey 90188ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey ATRACE_BEGIN("add"); 90288ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey for (auto user : get_known_users(uuid_)) { 90388ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey add_cache_files(cache, create_data_user_ce_path(uuid_, user)); 90488ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey add_cache_files(cache, create_data_user_de_path(uuid_, user)); 90588ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey add_cache_files(cache, 90688ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey StringPrintf("%s/Android/data", create_data_media_path(uuid_, user).c_str())); 90788ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey } 908f3124d8fab2377d6b5ec2c7cbc775e2ab20df3efFyodor Kupolov // Add files from /data/preloads/file_cache 909f3124d8fab2377d6b5ec2c7cbc775e2ab20df3efFyodor Kupolov if (uuid == nullptr) { 910f3124d8fab2377d6b5ec2c7cbc775e2ab20df3efFyodor Kupolov add_preloads_file_cache(cache, uuid_); 911f3124d8fab2377d6b5ec2c7cbc775e2ab20df3efFyodor Kupolov } 91288ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey ATRACE_END(); 91394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 91488ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey ATRACE_BEGIN("clear"); 91588ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey clear_cache_files(data_path, cache, freeStorageSize); 91688ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey ATRACE_END(); 91794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 91888ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey ATRACE_BEGIN("finish"); 91988ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey finish_cache_collection(cache); 92088ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey ATRACE_END(); 92188ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey } 92288ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey 92388ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey free = data_disk_free(data_path); 92488ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey if (free >= freeStorageSize) { 925423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 926475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey } else { 927423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error(StringPrintf("Failed to free up %" PRId64 " on %s; final free space %" PRId64, 92888ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey freeStorageSize, data_path.c_str(), free)); 929475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey } 93094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 93194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 932475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::rmdex(const std::string& codePath, 933475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const std::string& instructionSet) { 934475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 9357a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 9367a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey 93794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood char dex_path[PKG_PATH_MAX]; 93894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 939475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* path = codePath.c_str(); 940475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* instruction_set = instructionSet.c_str(); 941475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey 942770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey if (validate_apk_path(path) && validate_system_app_path(path)) { 943423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Invalid path " + codePath); 944770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey } 945770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey 946475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey if (!create_cache_path(dex_path, path, instruction_set)) { 947423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to create cache path for " + codePath); 948475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey } 94994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 95094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGV("unlink %s\n", dex_path); 95194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (unlink(dex_path) < 0) { 952423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error(StringPrintf("Failed to unlink %s", dex_path)); 95394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else { 954423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 95594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 95694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 95794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 9583dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkeystruct stats { 9593dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey int64_t codeSize; 9603dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey int64_t dataSize; 9613dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey int64_t cacheSize; 9623dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey}; 9633dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 964df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 965df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkeystatic std::string toString(std::vector<int64_t> values) { 966df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey std::stringstream res; 967df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey res << "["; 968df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey for (size_t i = 0; i < values.size(); i++) { 969df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey res << values[i]; 970df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (i < values.size() - 1) { 971df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey res << ","; 972df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 973df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 974df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey res << "]"; 975df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey return res.str(); 976df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey} 977df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 9783dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 979df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkeystatic void collectQuotaStats(const std::string& device, int32_t userId, 9807bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey int32_t appId, struct stats* stats, struct stats* extStats) { 981df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (device.empty()) return; 982df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 983df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey struct dqblk dq; 9843dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 9853dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey uid_t uid = multiuser_get_uid(userId, appId); 9863dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (quotactl(QCMD(Q_GETQUOTA, USRQUOTA), device.c_str(), uid, 9873dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey reinterpret_cast<char*>(&dq)) != 0) { 9883dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (errno != ESRCH) { 9893dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey PLOG(ERROR) << "Failed to quotactl " << device << " for UID " << uid; 9903dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 9913dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } else { 992df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 993df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey LOG(DEBUG) << "quotactl() for UID " << uid << " " << dq.dqb_curspace; 994df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 9953dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey stats->dataSize += dq.dqb_curspace; 9963dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 9973dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 9983dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey int cacheGid = multiuser_get_cache_gid(userId, appId); 9993dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (cacheGid != -1) { 10003dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), cacheGid, 10013dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey reinterpret_cast<char*>(&dq)) != 0) { 10023dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (errno != ESRCH) { 10033dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey PLOG(ERROR) << "Failed to quotactl " << device << " for GID " << cacheGid; 10043dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 10053dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } else { 1006df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 10077bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey LOG(DEBUG) << "quotactl() for GID " << cacheGid << " " << dq.dqb_curspace; 1008df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 10093dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey stats->cacheSize += dq.dqb_curspace; 10103dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 10113dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 10123dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 10137bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey int extGid = multiuser_get_ext_gid(userId, appId); 10147bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey if (extGid != -1) { 10157bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), extGid, 10167bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey reinterpret_cast<char*>(&dq)) != 0) { 10177bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey if (errno != ESRCH) { 10187bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey PLOG(ERROR) << "Failed to quotactl " << device << " for GID " << extGid; 10197bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey } 10207bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey } else { 10217bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey#if MEASURE_DEBUG 10227bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey LOG(DEBUG) << "quotactl() for GID " << extGid << " " << dq.dqb_curspace; 10237bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey#endif 10247bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey extStats->dataSize += dq.dqb_curspace; 10257bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey } 10267bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey } 10277bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey 10287bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey int sharedGid = multiuser_get_shared_gid(userId, appId); 10293dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (sharedGid != -1) { 10303dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), sharedGid, 10313dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey reinterpret_cast<char*>(&dq)) != 0) { 10323dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (errno != ESRCH) { 10333dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey PLOG(ERROR) << "Failed to quotactl " << device << " for GID " << sharedGid; 10343dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 10353dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } else { 1036df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 10377bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey LOG(DEBUG) << "quotactl() for GID " << sharedGid << " " << dq.dqb_curspace; 1038df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 10393dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey stats->codeSize += dq.dqb_curspace; 10403dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 10413dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 1042df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey} 1043df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1044df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkeystatic void collectManualStats(const std::string& path, struct stats* stats) { 104594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood DIR *d; 104694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int dfd; 104794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood struct dirent *de; 104894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood struct stat s; 1049d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey 10502f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey d = opendir(path.c_str()); 10512f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey if (d == nullptr) { 10523dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (errno != ENOENT) { 10533dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey PLOG(WARNING) << "Failed to open " << path; 10543dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 10552f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey return; 105694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 10572f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey dfd = dirfd(d); 10582f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey while ((de = readdir(d))) { 10592f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey const char *name = de->d_name; 1060d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey 10613dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey int64_t size = 0; 10622f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) { 10633dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey size = s.st_blocks * 512; 106494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 106594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 10662f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey if (de->d_type == DT_DIR) { 10673dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (!strcmp(name, ".")) { 10683dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // Don't recurse, but still count node size 10693dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } else if (!strcmp(name, "..")) { 10703dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // Don't recurse or count node size 10713dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey continue; 10723dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } else { 10733dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // Measure all children nodes 10743dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey size = 0; 10753dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey calculate_tree_size(StringPrintf("%s/%s", path.c_str(), name), &size); 10762f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey } 10773dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 10782f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey if (!strcmp(name, "cache") || !strcmp(name, "code_cache")) { 10793dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey stats->cacheSize += size; 10802f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey } 108194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 10823dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 10833dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // Legacy symlink isn't owned by app 10843dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (de->d_type == DT_LNK && !strcmp(name, "lib")) { 10853dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey continue; 10863dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 10873dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 10883dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // Everything found inside is considered data 10893dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey stats->dataSize += size; 109094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 10912f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey closedir(d); 10922f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey} 109394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 1094df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkeystatic void collectManualStatsForUser(const std::string& path, struct stats* stats, 1095df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey bool exclude_apps = false) { 1096df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey DIR *d; 1097df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int dfd; 1098df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey struct dirent *de; 1099df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey struct stat s; 1100df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1101df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey d = opendir(path.c_str()); 1102df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (d == nullptr) { 1103df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (errno != ENOENT) { 1104df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey PLOG(WARNING) << "Failed to open " << path; 1105df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1106df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey return; 1107df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1108df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey dfd = dirfd(d); 1109df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey while ((de = readdir(d))) { 1110df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (de->d_type == DT_DIR) { 1111df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey const char *name = de->d_name; 1112df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) != 0) { 1113df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey continue; 1114df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1115df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (!strcmp(name, ".") || !strcmp(name, "..")) { 1116df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey continue; 1117df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } else if (exclude_apps && (s.st_uid >= AID_APP_START && s.st_uid <= AID_APP_END)) { 1118df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey continue; 1119df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } else { 1120df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey collectManualStats(StringPrintf("%s/%s", path.c_str(), name), stats); 1121df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1122df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1123df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1124df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey closedir(d); 1125df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey} 1126df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 11277bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkeystatic void collectManualExternalStatsForUser(const std::string& path, struct stats* stats) { 11287bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey FTS *fts; 11297bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey FTSENT *p; 11307bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey char *argv[] = { (char*) path.c_str(), nullptr }; 1131b26786d647b624498c11405075e5223d1853f30aJeff Sharkey if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL))) { 11327bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey PLOG(ERROR) << "Failed to fts_open " << path; 11337bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey return; 11347bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey } 11357bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey while ((p = fts_read(fts)) != NULL) { 113676ddaeb2017e422de5a8d686ab62844d23d17624Jeff Sharkey p->fts_number = p->fts_parent->fts_number; 11377bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey switch (p->fts_info) { 11387bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey case FTS_D: 11397bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey if (p->fts_level == 4 11407bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey && !strcmp(p->fts_name, "cache") 11417bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey && !strcmp(p->fts_parent->fts_parent->fts_name, "data") 11427bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey && !strcmp(p->fts_parent->fts_parent->fts_parent->fts_name, "Android")) { 11437bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey p->fts_number = 1; 11447bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey } 11457bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey // Fall through to count the directory 11467bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey case FTS_DEFAULT: 11477bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey case FTS_F: 11487bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey case FTS_SL: 11497bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey case FTS_SLNONE: 11507bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey int64_t size = (p->fts_statp->st_blocks * 512); 11517bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey if (p->fts_number == 1) { 11527bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey stats->cacheSize += size; 11537bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey } 11547bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey stats->dataSize += size; 11557bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey break; 11567bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey } 11577bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey } 11587bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey fts_close(fts); 11597bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey} 11607bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey 11616c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkeybinder::Status InstalldNativeService::getAppSize(const std::unique_ptr<std::string>& uuid, 1162df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey const std::vector<std::string>& packageNames, int32_t userId, int32_t flags, 1163df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int32_t appId, const std::vector<int64_t>& ceDataInodes, 1164df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey const std::vector<std::string>& codePaths, std::vector<int64_t>* _aidl_return) { 11656c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 1166423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 1167df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey for (auto packageName : packageNames) { 1168df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 1169df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1170b26786d647b624498c11405075e5223d1853f30aJeff Sharkey // NOTE: Locking is relaxed on this method, since it's limited to 1171b26786d647b624498c11405075e5223d1853f30aJeff Sharkey // read-only measurements without mutation. 1172423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 1173df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // When modifying this logic, always verify using tests: 1174df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // runtest -x frameworks/base/services/tests/servicestests/src/com/android/server/pm/InstallerTest.java -m testGetAppSize 1175df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1176df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 1177df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey LOG(INFO) << "Measuring user " << userId << " app " << appId; 1178df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 11796c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey 11803dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // Here's a summary of the common storage locations across the platform, 11813dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // and how they're each tagged: 11823dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // 11833dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // /data/app/com.example UID system 11843dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // /data/app/com.example/oat UID system 1185df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // /data/user/0/com.example UID u0_a10 GID u0_a10 1186df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // /data/user/0/com.example/cache UID u0_a10 GID u0_a10_cache 1187df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // /data/media/0/foo.txt UID u0_media_rw 1188df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // /data/media/0/bar.jpg UID u0_media_rw GID u0_media_image 1189df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // /data/media/0/Android/data/com.example UID u0_media_rw GID u0_a10_ext 1190df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // /data/media/0/Android/data/com.example/cache UID u0_media_rw GID u0_a10_ext_cache 1191df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // /data/media/obb/com.example UID system 11923dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 11933dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey struct stats stats; 1194df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey struct stats extStats; 11953dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey memset(&stats, 0, sizeof(stats)); 1196df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey memset(&extStats, 0, sizeof(extStats)); 11973dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 1198df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 11993dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 120066b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey auto device = findQuotaDeviceForUuid(uuid); 120166b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey if (device.empty()) { 120266b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey flags &= ~FLAG_USE_QUOTA; 120366b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey } 120466b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey 1205466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_BEGIN("obb"); 1206df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey for (auto packageName : packageNames) { 1207df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto obbCodePath = create_data_media_obb_path(uuid_, packageName.c_str()); 1208df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(obbCodePath, &extStats.codeSize); 1209df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1210466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_END(); 12113dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 1212df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (flags & FLAG_USE_QUOTA && appId >= AID_APP_START) { 1213466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_BEGIN("code"); 1214df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey for (auto codePath : codePaths) { 1215df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(codePath, &stats.codeSize, -1, 1216df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey multiuser_get_shared_gid(userId, appId)); 12173dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 1218466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_END(); 1219df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1220466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_BEGIN("quota"); 122166b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey collectQuotaStats(device, userId, appId, &stats, &extStats); 1222466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_END(); 1223df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 12243dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } else { 1225466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_BEGIN("code"); 1226df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey for (auto codePath : codePaths) { 1227df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(codePath, &stats.codeSize); 1228df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1229466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_END(); 123094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 1231df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey for (size_t i = 0; i < packageNames.size(); i++) { 1232df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey const char* pkgname = packageNames[i].c_str(); 123394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 1234466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_BEGIN("data"); 1235df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto cePath = create_data_user_ce_package_path(uuid_, userId, pkgname, ceDataInodes[i]); 1236df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey collectManualStats(cePath, &stats); 1237df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto dePath = create_data_user_de_package_path(uuid_, userId, pkgname); 1238df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey collectManualStats(dePath, &stats); 1239466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_END(); 12403dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 1241466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_BEGIN("profiles"); 1242df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto userProfilePath = create_data_user_profile_package_path(userId, pkgname); 1243df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(userProfilePath, &stats.dataSize); 1244df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto refProfilePath = create_data_ref_profile_package_path(pkgname); 1245df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(refProfilePath, &stats.codeSize); 1246466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_END(); 1247df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1248466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_BEGIN("external"); 124976ddaeb2017e422de5a8d686ab62844d23d17624Jeff Sharkey auto extPath = create_data_media_package_path(uuid_, userId, "data", pkgname); 1250df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey collectManualStats(extPath, &extStats); 125176ddaeb2017e422de5a8d686ab62844d23d17624Jeff Sharkey auto mediaPath = create_data_media_package_path(uuid_, userId, "media", pkgname); 1252df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(mediaPath, &extStats.dataSize); 1253466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_END(); 1254df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1255df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1256466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_BEGIN("dalvik"); 1257df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int32_t sharedGid = multiuser_get_shared_gid(userId, appId); 1258df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (sharedGid != -1) { 1259df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(create_data_dalvik_cache_path(), &stats.codeSize, 1260df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey sharedGid, -1); 1261df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 126235c8465e31381dcc9a59655f63a91849a50294a6Calin Juravle calculate_tree_size(create_data_user_profile_path(userId), &stats.dataSize, 1263df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey multiuser_get_uid(userId, appId), -1); 1264466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_END(); 1265df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1266df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1267df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey std::vector<int64_t> ret; 1268df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(stats.codeSize); 1269df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(stats.dataSize); 1270df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(stats.cacheSize); 1271df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(extStats.codeSize); 1272df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(extStats.dataSize); 1273df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(extStats.cacheSize); 1274df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 1275df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey LOG(DEBUG) << "Final result " << toString(ret); 1276df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 1277df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey *_aidl_return = ret; 1278df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey return ok(); 1279df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey} 1280df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1281df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkeybinder::Status InstalldNativeService::getUserSize(const std::unique_ptr<std::string>& uuid, 1282df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int32_t userId, int32_t flags, const std::vector<int32_t>& appIds, 1283df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey std::vector<int64_t>* _aidl_return) { 1284df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 1285df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 1286b26786d647b624498c11405075e5223d1853f30aJeff Sharkey // NOTE: Locking is relaxed on this method, since it's limited to 1287b26786d647b624498c11405075e5223d1853f30aJeff Sharkey // read-only measurements without mutation. 1288df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1289df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // When modifying this logic, always verify using tests: 1290df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // runtest -x frameworks/base/services/tests/servicestests/src/com/android/server/pm/InstallerTest.java -m testGetUserSize 1291df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1292df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 1293df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey LOG(INFO) << "Measuring user " << userId; 1294df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 1295df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1296df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey struct stats stats; 1297df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey struct stats extStats; 1298df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey memset(&stats, 0, sizeof(stats)); 1299df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey memset(&extStats, 0, sizeof(extStats)); 1300df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1301df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 1302df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 130366b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey auto device = findQuotaDeviceForUuid(uuid); 130466b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey if (device.empty()) { 130566b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey flags &= ~FLAG_USE_QUOTA; 130666b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey } 130766b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey 1308df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (flags & FLAG_USE_QUOTA) { 13097bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey struct dqblk dq; 13107bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey 13117bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey ATRACE_BEGIN("obb"); 13127bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), AID_MEDIA_OBB, 13137bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey reinterpret_cast<char*>(&dq)) != 0) { 13147bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey if (errno != ESRCH) { 13157bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey PLOG(ERROR) << "Failed to quotactl " << device << " for GID " << AID_MEDIA_OBB; 13167bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey } 13177bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey } else { 13187bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey#if MEASURE_DEBUG 13197bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey LOG(DEBUG) << "quotactl() for GID " << AID_MEDIA_OBB << " " << dq.dqb_curspace; 13207bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey#endif 13217bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey extStats.codeSize += dq.dqb_curspace; 13227bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey } 13237bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey ATRACE_END(); 13247bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey 1325466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_BEGIN("code"); 1326df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(create_data_app_path(uuid_), &stats.codeSize, -1, -1, true); 1327466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_END(); 1328df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1329466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_BEGIN("data"); 1330df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto cePath = create_data_user_ce_path(uuid_, userId); 1331df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey collectManualStatsForUser(cePath, &stats, true); 1332df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto dePath = create_data_user_de_path(uuid_, userId); 1333df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey collectManualStatsForUser(dePath, &stats, true); 1334466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_END(); 1335df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1336466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_BEGIN("profile"); 1337df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto userProfilePath = create_data_user_profile_path(userId); 1338df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(userProfilePath, &stats.dataSize, -1, -1, true); 1339df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto refProfilePath = create_data_ref_profile_path(); 1340df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(refProfilePath, &stats.codeSize, -1, -1, true); 1341466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_END(); 13423dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 1343466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_BEGIN("external"); 13447bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey uid_t uid = multiuser_get_uid(userId, AID_MEDIA_RW); 13457bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey if (quotactl(QCMD(Q_GETQUOTA, USRQUOTA), device.c_str(), uid, 13467bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey reinterpret_cast<char*>(&dq)) != 0) { 13477bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey if (errno != ESRCH) { 13487bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey PLOG(ERROR) << "Failed to quotactl " << device << " for UID " << uid; 13497bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey } 13507bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey } else { 13517bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey#if MEASURE_DEBUG 13527bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey LOG(DEBUG) << "quotactl() for UID " << uid << " " << dq.dqb_curspace; 1353df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 13547bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey extStats.dataSize += dq.dqb_curspace; 13557bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey } 13567bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey ATRACE_END(); 13573dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 1358466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_BEGIN("dalvik"); 1359df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(create_data_dalvik_cache_path(), &stats.codeSize, 1360df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey -1, -1, true); 136135c8465e31381dcc9a59655f63a91849a50294a6Calin Juravle calculate_tree_size(create_data_user_profile_path(userId), &stats.dataSize, 1362df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey -1, -1, true); 1363466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_END(); 1364df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1365466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_BEGIN("quota"); 1366df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey for (auto appId : appIds) { 1367df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (appId >= AID_APP_START) { 1368df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey collectQuotaStats(device, userId, appId, &stats, &extStats); 1369df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 1370df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // Sleep to make sure we don't lose logs 1371df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey usleep(1); 1372df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 1373df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1374df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1375466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_END(); 1376df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } else { 13777bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey ATRACE_BEGIN("obb"); 13787bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey auto obbPath = create_data_path(uuid_) + "/media/obb"; 13797bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey calculate_tree_size(obbPath, &extStats.codeSize); 13807bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey ATRACE_END(); 13817bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey 1382466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_BEGIN("code"); 1383df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(create_data_app_path(uuid_), &stats.codeSize); 1384466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_END(); 1385df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1386466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_BEGIN("data"); 1387df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto cePath = create_data_user_ce_path(uuid_, userId); 1388df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey collectManualStatsForUser(cePath, &stats); 1389df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto dePath = create_data_user_de_path(uuid_, userId); 1390df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey collectManualStatsForUser(dePath, &stats); 1391466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_END(); 1392df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1393466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_BEGIN("profile"); 1394df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto userProfilePath = create_data_user_profile_path(userId); 1395df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(userProfilePath, &stats.dataSize); 1396df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto refProfilePath = create_data_ref_profile_path(); 1397df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(refProfilePath, &stats.codeSize); 1398466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_END(); 1399df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1400466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_BEGIN("external"); 14017bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey auto dataMediaPath = create_data_media_path(uuid_, userId); 14027bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey collectManualExternalStatsForUser(dataMediaPath, &extStats); 14037bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey#if MEASURE_DEBUG 14047bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey LOG(DEBUG) << "Measured external data " << extStats.dataSize << " cache " 14057bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey << extStats.cacheSize; 14063dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey#endif 14077bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey ATRACE_END(); 1408df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1409466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_BEGIN("dalvik"); 1410df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(create_data_dalvik_cache_path(), &stats.codeSize); 141135c8465e31381dcc9a59655f63a91849a50294a6Calin Juravle calculate_tree_size(create_data_user_profile_path(userId), &stats.dataSize); 1412466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_END(); 14132f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey } 141494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 14153dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey std::vector<int64_t> ret; 14163dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey ret.push_back(stats.codeSize); 14173dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey ret.push_back(stats.dataSize); 14183dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey ret.push_back(stats.cacheSize); 1419df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(extStats.codeSize); 1420df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(extStats.dataSize); 1421df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(extStats.cacheSize); 1422df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 1423df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey LOG(DEBUG) << "Final result " << toString(ret); 1424df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 1425df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey *_aidl_return = ret; 1426df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey return ok(); 1427df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey} 1428df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1429df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkeybinder::Status InstalldNativeService::getExternalSize(const std::unique_ptr<std::string>& uuid, 1430df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int32_t userId, int32_t flags, std::vector<int64_t>* _aidl_return) { 1431df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 1432df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 1433b26786d647b624498c11405075e5223d1853f30aJeff Sharkey // NOTE: Locking is relaxed on this method, since it's limited to 1434b26786d647b624498c11405075e5223d1853f30aJeff Sharkey // read-only measurements without mutation. 1435df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1436df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // When modifying this logic, always verify using tests: 1437df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // runtest -x frameworks/base/services/tests/servicestests/src/com/android/server/pm/InstallerTest.java -m testGetExternalSize 1438df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1439df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 1440df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey LOG(INFO) << "Measuring external " << userId; 1441df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 1442df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1443df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 1444df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1445df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int64_t totalSize = 0; 1446df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int64_t audioSize = 0; 1447df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int64_t videoSize = 0; 1448df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int64_t imageSize = 0; 1449df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 145066b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey auto device = findQuotaDeviceForUuid(uuid); 145166b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey if (device.empty()) { 145266b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey flags &= ~FLAG_USE_QUOTA; 145366b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey } 145466b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey 1455df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (flags & FLAG_USE_QUOTA) { 1456df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey struct dqblk dq; 1457df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1458df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey uid_t uid = multiuser_get_uid(userId, AID_MEDIA_RW); 1459df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (quotactl(QCMD(Q_GETQUOTA, USRQUOTA), device.c_str(), uid, 1460df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey reinterpret_cast<char*>(&dq)) != 0) { 1461df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (errno != ESRCH) { 1462df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey PLOG(ERROR) << "Failed to quotactl " << device << " for UID " << uid; 1463df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1464df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } else { 1465df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 1466df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey LOG(DEBUG) << "quotactl() for UID " << uid << " " << dq.dqb_curspace; 1467df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 1468df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey totalSize = dq.dqb_curspace; 1469df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1470df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1471df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey gid_t audioGid = multiuser_get_uid(userId, AID_MEDIA_AUDIO); 1472df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), audioGid, 1473df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey reinterpret_cast<char*>(&dq)) == 0) { 1474df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 1475df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey LOG(DEBUG) << "quotactl() for GID " << audioGid << " " << dq.dqb_curspace; 1476df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 1477df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey audioSize = dq.dqb_curspace; 1478df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1479df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey gid_t videoGid = multiuser_get_uid(userId, AID_MEDIA_VIDEO); 1480df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), videoGid, 1481df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey reinterpret_cast<char*>(&dq)) == 0) { 1482df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 1483df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey LOG(DEBUG) << "quotactl() for GID " << videoGid << " " << dq.dqb_curspace; 1484df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 1485df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey videoSize = dq.dqb_curspace; 1486df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1487df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey gid_t imageGid = multiuser_get_uid(userId, AID_MEDIA_IMAGE); 1488df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), imageGid, 1489df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey reinterpret_cast<char*>(&dq)) == 0) { 1490df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 1491df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey LOG(DEBUG) << "quotactl() for GID " << imageGid << " " << dq.dqb_curspace; 1492df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 1493df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey imageSize = dq.dqb_curspace; 1494df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1495df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } else { 1496df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey FTS *fts; 1497df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey FTSENT *p; 1498df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto path = create_data_media_path(uuid_, userId); 1499df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey char *argv[] = { (char*) path.c_str(), nullptr }; 1500b26786d647b624498c11405075e5223d1853f30aJeff Sharkey if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL))) { 1501df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey return error("Failed to fts_open " + path); 1502df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1503df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey while ((p = fts_read(fts)) != NULL) { 1504df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey char* ext; 1505df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int64_t size = (p->fts_statp->st_blocks * 512); 1506df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey switch (p->fts_info) { 1507df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey case FTS_F: 1508df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // Only categorize files not belonging to apps 1509df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (p->fts_statp->st_gid < AID_APP_START) { 1510df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ext = strrchr(p->fts_name, '.'); 1511df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (ext != nullptr) { 1512df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey switch (MatchExtension(++ext)) { 1513df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey case AID_MEDIA_AUDIO: audioSize += size; break; 1514df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey case AID_MEDIA_VIDEO: videoSize += size; break; 1515df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey case AID_MEDIA_IMAGE: imageSize += size; break; 1516df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1517df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1518df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1519df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // Fall through to always count against total 1520df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey case FTS_D: 1521df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey case FTS_DEFAULT: 1522df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey case FTS_SL: 1523df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey case FTS_SLNONE: 1524df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey totalSize += size; 1525df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey break; 1526df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1527df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1528df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey fts_close(fts); 1529df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1530df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1531df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey std::vector<int64_t> ret; 1532df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(totalSize); 1533df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(audioSize); 1534df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(videoSize); 1535df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(imageSize); 1536df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 1537df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey LOG(DEBUG) << "Final result " << toString(ret); 1538df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 15393dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey *_aidl_return = ret; 1540423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 15412f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey} 1542d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey 154388ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkeybinder::Status InstalldNativeService::setAppQuota(const std::unique_ptr<std::string>& uuid, 154488ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey int32_t userId, int32_t appId, int64_t cacheQuota) { 154588ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey ENFORCE_UID(AID_SYSTEM); 154688ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey CHECK_ARGUMENT_UUID(uuid); 1547b26786d647b624498c11405075e5223d1853f30aJeff Sharkey std::lock_guard<std::recursive_mutex> lock(mCacheQuotasLock); 154888ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey 154988ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey int32_t uid = multiuser_get_uid(userId, appId); 155088ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey mCacheQuotas[uid] = cacheQuota; 155188ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey 155288ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey return ok(); 155388ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey} 155488ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey 15556727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr// Dumps the contents of a profile file, using pkgname's dex files for pretty 15566727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr// printing the result. 1557475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::dumpProfiles(int32_t uid, const std::string& packageName, 1558475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const std::string& codePaths, bool* _aidl_return) { 1559475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 1560423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 15617a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 1562475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey 1563475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* pkgname = packageName.c_str(); 156490aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey const char* code_paths = codePaths.c_str(); 15656727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr 156690aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey *_aidl_return = dump_profiles(uid, pkgname, code_paths); 1567423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 15686727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr} 15696727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr 15704d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe// TODO: Consider returning error codes. 1571475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::mergeProfiles(int32_t uid, const std::string& packageName, 1572423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey bool* _aidl_return) { 1573475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 1574423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 15757a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 1576423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 1577475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* pkgname = packageName.c_str(); 1578475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey *_aidl_return = analyse_profiles(uid, pkgname); 1579423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 15804d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe} 15814d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe 15826c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkeybinder::Status InstalldNativeService::dexopt(const std::string& apkPath, int32_t uid, 15836c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey const std::unique_ptr<std::string>& packageName, const std::string& instructionSet, 15846c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey int32_t dexoptNeeded, const std::unique_ptr<std::string>& outputPath, int32_t dexFlags, 15856c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey const std::string& compilerFilter, const std::unique_ptr<std::string>& uuid, 15866c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey const std::unique_ptr<std::string>& sharedLibraries) { 15876c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 1588423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 1589423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (packageName && *packageName != "*") { 1590423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(*packageName); 1591423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 15927a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 15936c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey 15946c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey const char* apk_path = apkPath.c_str(); 15956c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey const char* pkgname = packageName ? packageName->c_str() : "*"; 15966c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey const char* instruction_set = instructionSet.c_str(); 15976c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey const char* oat_dir = outputPath ? outputPath->c_str() : nullptr; 15986c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey const char* compiler_filter = compilerFilter.c_str(); 15996c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey const char* volume_uuid = uuid ? uuid->c_str() : nullptr; 16006c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey const char* shared_libraries = sharedLibraries ? sharedLibraries->c_str() : nullptr; 16016c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey 16026c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey int res = android::installd::dexopt(apk_path, uid, pkgname, instruction_set, dexoptNeeded, 16036c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey oat_dir, dexFlags, compiler_filter, volume_uuid, shared_libraries); 1604423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return res ? error(res, "Failed to dexopt") : ok(); 16056c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey} 16066c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey 1607475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::markBootComplete(const std::string& instructionSet) { 1608475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 16097a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 16107a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey 1611475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* instruction_set = instructionSet.c_str(); 1612475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey 1613475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey char boot_marker_path[PKG_PATH_MAX]; 1614475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey sprintf(boot_marker_path, 161502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe "%s/%s/%s/.booting", 161602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe android_data_dir.path, 161702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe DALVIK_CACHE, 161802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe instruction_set); 1619091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath 1620475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ALOGV("mark_boot_complete : %s", boot_marker_path); 1621475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey if (unlink(boot_marker_path) != 0) { 1622423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error(StringPrintf("Failed to unlink %s", boot_marker_path)); 1623475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey } 1624423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 1625091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath} 1626091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath 162794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodvoid mkinnerdirs(char* path, int basepos, mode_t mode, int uid, int gid, 162894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood struct stat* statbuf) 162994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 163094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood while (path[basepos] != 0) { 163194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (path[basepos] == '/') { 163294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood path[basepos] = 0; 163394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (lstat(path, statbuf) < 0) { 163494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGV("Making directory: %s\n", path); 163594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (mkdir(path, mode) == 0) { 163694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood chown(path, uid, gid); 163794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else { 163894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGW("Unable to make directory %s: %s\n", path, strerror(errno)); 163994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 164094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 164194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood path[basepos] = '/'; 164294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood basepos++; 164394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 164494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood basepos++; 164594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 164694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 164794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 1648475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::linkNativeLibraryDirectory( 1649475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const std::unique_ptr<std::string>& uuid, const std::string& packageName, 1650475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const std::string& nativeLibPath32, int32_t userId) { 1651475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 1652423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 1653423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 16547a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 1655423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 1656475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 1657475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* pkgname = packageName.c_str(); 1658475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* asecLibDir = nativeLibPath32.c_str(); 165994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood struct stat s, libStat; 1660423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey binder::Status res = ok(); 166194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 1662423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey auto _pkgdir = create_data_user_ce_package_path(uuid_, userId, pkgname); 1663423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey auto _libsymlink = _pkgdir + PKG_LIB_POSTFIX; 1664c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey 1665c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey const char* pkgdir = _pkgdir.c_str(); 1666c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey const char* libsymlink = _libsymlink.c_str(); 166794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 1668475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey if (stat(pkgdir, &s) < 0) { 1669423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to stat " + _pkgdir); 1670475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey } 167194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 167294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (chown(pkgdir, AID_INSTALL, AID_INSTALL) < 0) { 1673423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to chown " + _pkgdir); 167494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 167594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 167694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (chmod(pkgdir, 0700) < 0) { 1677423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to chmod " + _pkgdir); 167894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto out; 167994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 168094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 168194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (lstat(libsymlink, &libStat) < 0) { 168294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (errno != ENOENT) { 1683423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to stat " + _libsymlink); 168494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto out; 168594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 168694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else { 168794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (S_ISDIR(libStat.st_mode)) { 16883aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath if (delete_dir_contents(libsymlink, 1, NULL) < 0) { 1689423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to delete " + _libsymlink); 169094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto out; 169194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 169294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else if (S_ISLNK(libStat.st_mode)) { 169394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (unlink(libsymlink) < 0) { 1694423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to unlink " + _libsymlink); 169594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto out; 169694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 169794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 169894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 169994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 170094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (symlink(asecLibDir, libsymlink) < 0) { 1701423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to symlink " + _libsymlink + " to " + nativeLibPath32); 170294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto out; 170394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 170494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 170594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodout: 170694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (chmod(pkgdir, s.st_mode) < 0) { 1707423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey auto msg = "Failed to cleanup chmod " + _pkgdir; 1708423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (res.isOk()) { 1709423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error(msg); 1710423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } else { 1711423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey PLOG(ERROR) << msg; 1712423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 171394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 171494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 171594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (chown(pkgdir, s.st_uid, s.st_gid) < 0) { 1716423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey auto msg = "Failed to cleanup chown " + _pkgdir; 1717423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (res.isOk()) { 1718423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error(msg); 1719423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } else { 1720423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey PLOG(ERROR) << msg; 1721423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 172294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 172394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 1724423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return res; 172594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 172663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 172763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstadstatic void run_idmap(const char *target_apk, const char *overlay_apk, int idmap_fd) 172863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad{ 172963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad static const char *IDMAP_BIN = "/system/bin/idmap"; 173063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad static const size_t MAX_INT_LEN = 32; 173163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad char idmap_str[MAX_INT_LEN]; 173263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 173363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad snprintf(idmap_str, sizeof(idmap_str), "%d", idmap_fd); 173463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 173563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad execl(IDMAP_BIN, IDMAP_BIN, "--fd", target_apk, overlay_apk, idmap_str, (char*)NULL); 173663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("execl(%s) failed: %s\n", IDMAP_BIN, strerror(errno)); 173763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad} 173863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 173963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad// Transform string /a/b/c.apk to (prefix)/a@b@c.apk@(suffix) 174063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad// eg /a/b/c.apk to /data/resource-cache/a@b@c.apk@idmap 174163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstadstatic int flatten_path(const char *prefix, const char *suffix, 174263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad const char *overlay_path, char *idmap_path, size_t N) 174363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad{ 174463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (overlay_path == NULL || idmap_path == NULL) { 174563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad return -1; 174663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 174763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad const size_t len_overlay_path = strlen(overlay_path); 174863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad // will access overlay_path + 1 further below; requires absolute path 174963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (len_overlay_path < 2 || *overlay_path != '/') { 175063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad return -1; 175163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 175263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad const size_t len_idmap_root = strlen(prefix); 175363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad const size_t len_suffix = strlen(suffix); 175463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (SIZE_MAX - len_idmap_root < len_overlay_path || 175563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad SIZE_MAX - (len_idmap_root + len_overlay_path) < len_suffix) { 175663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad // additions below would cause overflow 175763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad return -1; 175863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 175963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (N < len_idmap_root + len_overlay_path + len_suffix) { 176063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad return -1; 176163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 176263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad memset(idmap_path, 0, N); 176363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad snprintf(idmap_path, N, "%s%s%s", prefix, overlay_path + 1, suffix); 176463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad char *ch = idmap_path + len_idmap_root; 176563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad while (*ch != '\0') { 176663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (*ch == '/') { 176763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad *ch = '@'; 176863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 176963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ++ch; 177063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 177163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad return 0; 177263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad} 177363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 1774475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::idmap(const std::string& targetApkPath, 1775475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const std::string& overlayApkPath, int32_t uid) { 1776475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 17777a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 17787a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey 1779475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* target_apk = targetApkPath.c_str(); 1780475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* overlay_apk = overlayApkPath.c_str(); 178163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGV("idmap target_apk=%s overlay_apk=%s uid=%d\n", target_apk, overlay_apk, uid); 178263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 178363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad int idmap_fd = -1; 178463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad char idmap_path[PATH_MAX]; 178563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 178663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (flatten_path(IDMAP_PREFIX, IDMAP_SUFFIX, overlay_apk, 178763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad idmap_path, sizeof(idmap_path)) == -1) { 178863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("idmap cannot generate idmap path for overlay %s\n", overlay_apk); 178963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad goto fail; 179063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 179163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 179263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad unlink(idmap_path); 179363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad idmap_fd = open(idmap_path, O_RDWR | O_CREAT | O_EXCL, 0644); 179463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (idmap_fd < 0) { 179563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("idmap cannot open '%s' for output: %s\n", idmap_path, strerror(errno)); 179663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad goto fail; 179763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 179863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (fchown(idmap_fd, AID_SYSTEM, uid) < 0) { 179963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("idmap cannot chown '%s'\n", idmap_path); 180063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad goto fail; 180163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 180263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (fchmod(idmap_fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0) { 180363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("idmap cannot chmod '%s'\n", idmap_path); 180463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad goto fail; 180563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 180663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 180763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad pid_t pid; 180863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad pid = fork(); 180963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (pid == 0) { 181063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad /* child -- drop privileges before continuing */ 181163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (setgid(uid) != 0) { 181263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("setgid(%d) failed during idmap\n", uid); 181363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad exit(1); 181463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 181563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (setuid(uid) != 0) { 181663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("setuid(%d) failed during idmap\n", uid); 181763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad exit(1); 181863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 181963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (flock(idmap_fd, LOCK_EX | LOCK_NB) != 0) { 182063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("flock(%s) failed during idmap: %s\n", idmap_path, strerror(errno)); 182163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad exit(1); 182263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 182363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 182463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad run_idmap(target_apk, overlay_apk, idmap_fd); 182563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad exit(1); /* only if exec call to idmap failed */ 182663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } else { 182763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad int status = wait_child(pid); 182863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (status != 0) { 182963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("idmap failed, status=0x%04x\n", status); 183063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad goto fail; 183163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 183263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 183363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 183463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad close(idmap_fd); 1835423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 183663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstadfail: 183763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (idmap_fd >= 0) { 183863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad close(idmap_fd); 183963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad unlink(idmap_path); 184063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 1841423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error(); 184263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad} 1843e9887e46cea4a095e4219927eadbe4c57bb1a5eeRobert Craig 18445c6944c35211accd783d2584fac6421dd0af5bddMÃ¥rten Kongstadbinder::Status InstalldNativeService::removeIdmap(const std::string& overlayApkPath) { 18455c6944c35211accd783d2584fac6421dd0af5bddMÃ¥rten Kongstad const char* overlay_apk = overlayApkPath.c_str(); 18465c6944c35211accd783d2584fac6421dd0af5bddMÃ¥rten Kongstad char idmap_path[PATH_MAX]; 18475c6944c35211accd783d2584fac6421dd0af5bddMÃ¥rten Kongstad 18485c6944c35211accd783d2584fac6421dd0af5bddMÃ¥rten Kongstad if (flatten_path(IDMAP_PREFIX, IDMAP_SUFFIX, overlay_apk, 18495c6944c35211accd783d2584fac6421dd0af5bddMÃ¥rten Kongstad idmap_path, sizeof(idmap_path)) == -1) { 18505c6944c35211accd783d2584fac6421dd0af5bddMÃ¥rten Kongstad ALOGE("idmap cannot generate idmap path for overlay %s\n", overlay_apk); 18515c6944c35211accd783d2584fac6421dd0af5bddMÃ¥rten Kongstad return error(); 18525c6944c35211accd783d2584fac6421dd0af5bddMÃ¥rten Kongstad } 18535c6944c35211accd783d2584fac6421dd0af5bddMÃ¥rten Kongstad if (unlink(idmap_path) < 0) { 18545c6944c35211accd783d2584fac6421dd0af5bddMÃ¥rten Kongstad ALOGE("couldn't unlink idmap file %s\n", idmap_path); 18555c6944c35211accd783d2584fac6421dd0af5bddMÃ¥rten Kongstad return error(); 18565c6944c35211accd783d2584fac6421dd0af5bddMÃ¥rten Kongstad } 18575c6944c35211accd783d2584fac6421dd0af5bddMÃ¥rten Kongstad return ok(); 18585c6944c35211accd783d2584fac6421dd0af5bddMÃ¥rten Kongstad} 18595c6944c35211accd783d2584fac6421dd0af5bddMÃ¥rten Kongstad 1860c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkeybinder::Status InstalldNativeService::restoreconAppData(const std::unique_ptr<std::string>& uuid, 1861c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const std::string& packageName, int32_t userId, int32_t flags, int32_t appId, 1862c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const std::string& seInfo) { 1863c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 1864423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 1865423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 18667a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 1867423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 1868423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey binder::Status res = ok(); 1869e9887e46cea4a095e4219927eadbe4c57bb1a5eeRobert Craig 1870da30dc7336f03ca629fe173db1425fdce989119cRobert Craig // SELINUX_ANDROID_RESTORECON_DATADATA flag is set by libselinux. Not needed here. 1871c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey unsigned int seflags = SELINUX_ANDROID_RESTORECON_RECURSE; 1872c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 1873c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* pkgName = packageName.c_str(); 1874c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* seinfo = seInfo.c_str(); 1875e9887e46cea4a095e4219927eadbe4c57bb1a5eeRobert Craig 1876c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey uid_t uid = multiuser_get_uid(userId, appId); 1877aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (flags & FLAG_STORAGE_CE) { 1878c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey auto path = create_data_user_ce_package_path(uuid_, userId, pkgName); 1879c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey if (selinux_android_restorecon_pkgdir(path.c_str(), seinfo, uid, seflags) < 0) { 1880423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("restorecon failed for " + path); 1881da30dc7336f03ca629fe173db1425fdce989119cRobert Craig } 1882c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey } 1883aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (flags & FLAG_STORAGE_DE) { 1884c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey auto path = create_data_user_de_package_path(uuid_, userId, pkgName); 1885c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey if (selinux_android_restorecon_pkgdir(path.c_str(), seinfo, uid, seflags) < 0) { 1886423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("restorecon failed for " + path); 1887da30dc7336f03ca629fe173db1425fdce989119cRobert Craig } 1888da30dc7336f03ca629fe173db1425fdce989119cRobert Craig } 1889423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return res; 1890e9887e46cea4a095e4219927eadbe4c57bb1a5eeRobert Craig} 18913aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath 1892475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::createOatDir(const std::string& oatDir, 1893475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const std::string& instructionSet) { 1894475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 18957a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 18967a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey 1897475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* oat_dir = oatDir.c_str(); 1898475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* instruction_set = instructionSet.c_str(); 189988ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov char oat_instr_dir[PKG_PATH_MAX]; 190088ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov 190188ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov if (validate_apk_path(oat_dir)) { 1902423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Invalid path " + oatDir); 190388ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov } 19048eed7e6a9bb527af7380ec13b390c6e2955eba6aFyodor Kupolov if (fs_prepare_dir(oat_dir, S_IRWXU | S_IRWXG | S_IXOTH, AID_SYSTEM, AID_INSTALL)) { 1905423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to prepare " + oatDir); 190688ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov } 190788ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov if (selinux_android_restorecon(oat_dir, 0)) { 1908423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to restorecon " + oatDir); 190988ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov } 191088ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov snprintf(oat_instr_dir, PKG_PATH_MAX, "%s/%s", oat_dir, instruction_set); 19118eed7e6a9bb527af7380ec13b390c6e2955eba6aFyodor Kupolov if (fs_prepare_dir(oat_instr_dir, S_IRWXU | S_IRWXG | S_IXOTH, AID_SYSTEM, AID_INSTALL)) { 1912423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error(StringPrintf("Failed to prepare %s", oat_instr_dir)); 191388ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov } 1914423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 191588ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov} 191688ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov 1917475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::rmPackageDir(const std::string& packageDir) { 1918475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 19197a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 19207a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey 1921423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (validate_apk_path(packageDir.c_str())) { 1922423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Invalid path " + packageDir); 1923423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 1924423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (delete_dir_contents_and_dir(packageDir) != 0) { 1925423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to delete " + packageDir); 192688ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov } 1927423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 192888ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov} 192988ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov 1930475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::linkFile(const std::string& relativePath, 1931475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const std::string& fromBase, const std::string& toBase) { 1932475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 19337a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 19347a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey 1935475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* relative_path = relativePath.c_str(); 1936475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* from_base = fromBase.c_str(); 1937475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* to_base = toBase.c_str(); 1938d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath char from_path[PKG_PATH_MAX]; 1939d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath char to_path[PKG_PATH_MAX]; 1940d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath snprintf(from_path, PKG_PATH_MAX, "%s/%s", from_base, relative_path); 1941d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath snprintf(to_path, PKG_PATH_MAX, "%s/%s", to_base, relative_path); 1942d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath 1943d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath if (validate_apk_path_subdirs(from_path)) { 1944423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error(StringPrintf("Invalid from path %s", from_path)); 1945d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath } 1946d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath 1947d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath if (validate_apk_path_subdirs(to_path)) { 1948423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error(StringPrintf("Invalid to path %s", to_path)); 1949d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath } 1950d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath 1951423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (link(from_path, to_path) < 0) { 1952423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error(StringPrintf("Failed to link from %s to %s", from_path, to_path)); 1953d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath } 1954d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath 1955423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 1956d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath} 1957d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath 1958475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::moveAb(const std::string& apkPath, 1959475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const std::string& instructionSet, const std::string& outputPath) { 1960475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 19617a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 196290aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey 1963475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* apk_path = apkPath.c_str(); 1964475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* instruction_set = instructionSet.c_str(); 1965475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* oat_dir = outputPath.c_str(); 19660354bd0eaf24de533e86885b811f816f1e4d15c8Andreas Gampe 196790aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey bool success = move_ab(apk_path, instruction_set, oat_dir); 1968423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return success ? ok() : error(); 1969e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe} 1970e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe 1971475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::deleteOdex(const std::string& apkPath, 1972475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const std::string& instructionSet, const std::string& outputPath) { 1973475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 19747a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 197590aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey 1976475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* apk_path = apkPath.c_str(); 1977475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* instruction_set = instructionSet.c_str(); 1978475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* oat_dir = outputPath.c_str(); 1979475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey 198090aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey bool res = delete_odex(apk_path, instruction_set, oat_dir); 198190aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey return res ? ok() : error(); 19823964da0636624ff9af42d1f316bed0752e37c490Andreas Gampe} 19833964da0636624ff9af42d1f316bed0752e37c490Andreas Gampe 1984bd9683607d391a29b1422a50f8972267e9bddc47Calin Juravlebinder::Status InstalldNativeService::reconcileSecondaryDexFile( 1985bd9683607d391a29b1422a50f8972267e9bddc47Calin Juravle const std::string& dexPath, const std::string& packageName, int32_t uid, 1986bd9683607d391a29b1422a50f8972267e9bddc47Calin Juravle const std::vector<std::string>& isas, const std::unique_ptr<std::string>& volumeUuid, 1987bd9683607d391a29b1422a50f8972267e9bddc47Calin Juravle int32_t storage_flag, bool* _aidl_return) { 1988bd9683607d391a29b1422a50f8972267e9bddc47Calin Juravle ENFORCE_UID(AID_SYSTEM); 1989bd9683607d391a29b1422a50f8972267e9bddc47Calin Juravle CHECK_ARGUMENT_UUID(volumeUuid); 1990bd9683607d391a29b1422a50f8972267e9bddc47Calin Juravle CHECK_ARGUMENT_PACKAGE_NAME(packageName); 1991bd9683607d391a29b1422a50f8972267e9bddc47Calin Juravle 1992bd9683607d391a29b1422a50f8972267e9bddc47Calin Juravle std::lock_guard<std::recursive_mutex> lock(mLock); 1993bd9683607d391a29b1422a50f8972267e9bddc47Calin Juravle bool result = android::installd::reconcile_secondary_dex_file( 1994bd9683607d391a29b1422a50f8972267e9bddc47Calin Juravle dexPath, packageName, uid, isas, volumeUuid, storage_flag, _aidl_return); 1995bd9683607d391a29b1422a50f8972267e9bddc47Calin Juravle return result ? ok() : error(); 1996bd9683607d391a29b1422a50f8972267e9bddc47Calin Juravle} 1997bd9683607d391a29b1422a50f8972267e9bddc47Calin Juravle 199866b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkeybinder::Status InstalldNativeService::invalidateMounts() { 199966b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 2000b26786d647b624498c11405075e5223d1853f30aJeff Sharkey std::lock_guard<std::recursive_mutex> lock(mQuotaDevicesLock); 200166b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey 200266b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey mQuotaDevices.clear(); 200366b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey 200466b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey std::ifstream in("/proc/mounts"); 200566b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey if (!in.is_open()) { 200666b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey return error("Failed to read mounts"); 200766b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey } 200866b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey 200966b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey std::string source; 201066b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey std::string target; 201166b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey std::string ignored; 201266b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey struct dqblk dq; 201366b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey while (!in.eof()) { 201466b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey std::getline(in, source, ' '); 201566b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey std::getline(in, target, ' '); 201666b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey std::getline(in, ignored); 201766b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey 201866b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey if (source.compare(0, 11, "/dev/block/") == 0) { 201966b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey if (quotactl(QCMD(Q_GETQUOTA, USRQUOTA), source.c_str(), 0, 202066b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey reinterpret_cast<char*>(&dq)) == 0) { 202166b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey LOG(DEBUG) << "Found " << source << " with quota"; 202266b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey mQuotaDevices[target] = source; 202366b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey } 202466b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey } 202566b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey } 202666b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey return ok(); 202766b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey} 202866b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey 202966b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkeystd::string InstalldNativeService::findQuotaDeviceForUuid( 203066b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey const std::unique_ptr<std::string>& uuid) { 2031b26786d647b624498c11405075e5223d1853f30aJeff Sharkey std::lock_guard<std::recursive_mutex> lock(mQuotaDevicesLock); 203266b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey auto path = create_data_path(uuid ? uuid->c_str() : nullptr); 203366b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey return mQuotaDevices[path]; 203466b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey} 203566b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey 2036325b8c94a5ae47b7903ea5fe386a78466d6cb3b7Jeff Sharkeybinder::Status InstalldNativeService::isQuotaSupported( 2037325b8c94a5ae47b7903ea5fe386a78466d6cb3b7Jeff Sharkey const std::unique_ptr<std::string>& volumeUuid, bool* _aidl_return) { 2038325b8c94a5ae47b7903ea5fe386a78466d6cb3b7Jeff Sharkey *_aidl_return = !findQuotaDeviceForUuid(volumeUuid).empty(); 2039325b8c94a5ae47b7903ea5fe386a78466d6cb3b7Jeff Sharkey return ok(); 2040325b8c94a5ae47b7903ea5fe386a78466d6cb3b7Jeff Sharkey} 2041325b8c94a5ae47b7903ea5fe386a78466d6cb3b7Jeff Sharkey 204202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe} // namespace installd 204302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe} // namespace android 2044