InstalldNativeService.cpp revision d712f0ccc120357e1267a04ac6de9dd732b27e76
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; 91e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkeystatic constexpr int FLAG_FORCE = 1 << 16; 926a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 930274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeynamespace { 940274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 950274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeyconstexpr const char* kDump = "android.permission.DUMP"; 960274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 97423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkeystatic binder::Status ok() { 98423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return binder::Status::ok(); 99423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey} 100423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 101423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkeystatic binder::Status exception(uint32_t code, const std::string& msg) { 102423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return binder::Status::fromExceptionCode(code, String8(msg.c_str())); 103423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey} 104423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 105423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkeystatic binder::Status error() { 106423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return binder::Status::fromServiceSpecificError(errno); 107423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey} 108423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 109423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkeystatic binder::Status error(const std::string& msg) { 110423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey PLOG(ERROR) << msg; 111423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return binder::Status::fromServiceSpecificError(errno, String8(msg.c_str())); 112423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey} 113423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 114423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkeystatic binder::Status error(uint32_t code, const std::string& msg) { 115423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey LOG(ERROR) << msg << " (" << code << ")"; 116423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return binder::Status::fromServiceSpecificError(code, String8(msg.c_str())); 117423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey} 118423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 1190274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeybinder::Status checkPermission(const char* permission) { 1200274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey pid_t pid; 1210274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey uid_t uid; 1220274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 1230274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey if (checkCallingPermission(String16(permission), reinterpret_cast<int32_t*>(&pid), 1240274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey reinterpret_cast<int32_t*>(&uid))) { 125423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 1260274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey } else { 127423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return exception(binder::Status::EX_SECURITY, 128423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey StringPrintf("UID %d / PID %d lacks permission %s", uid, pid, permission)); 1290274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey } 1300274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey} 1310274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 1320274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeybinder::Status checkUid(uid_t expectedUid) { 1330274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey uid_t uid = IPCThreadState::self()->getCallingUid(); 1340274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey if (uid == expectedUid || uid == AID_ROOT) { 135423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 1360274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey } else { 137423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return exception(binder::Status::EX_SECURITY, 138423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey StringPrintf("UID %d is not expected UID %d", uid, expectedUid)); 139423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 140423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey} 141423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 142423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkeybinder::Status checkArgumentUuid(const std::unique_ptr<std::string>& uuid) { 143423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (!uuid || is_valid_filename(*uuid)) { 144423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 145423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } else { 146423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return exception(binder::Status::EX_ILLEGAL_ARGUMENT, 147423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey StringPrintf("UUID %s is malformed", uuid->c_str())); 148423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 149423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey} 150423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 151423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkeybinder::Status checkArgumentPackageName(const std::string& packageName) { 152423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (is_valid_package_name(packageName.c_str())) { 153423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 154423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } else { 155423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return exception(binder::Status::EX_ILLEGAL_ARGUMENT, 156423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey StringPrintf("Package name %s is malformed", packageName.c_str())); 1570274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey } 1580274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey} 1590274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 1600274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey#define ENFORCE_UID(uid) { \ 1610274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey binder::Status status = checkUid((uid)); \ 1620274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey if (!status.isOk()) { \ 1630274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey return status; \ 1640274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey } \ 1650274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey} 1660274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 167423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey#define CHECK_ARGUMENT_UUID(uuid) { \ 168423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey binder::Status status = checkArgumentUuid((uuid)); \ 169423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (!status.isOk()) { \ 170423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return status; \ 171423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } \ 172423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey} 173423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 174423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey#define CHECK_ARGUMENT_PACKAGE_NAME(packageName) { \ 175423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey binder::Status status = \ 176423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey checkArgumentPackageName((packageName)); \ 177423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (!status.isOk()) { \ 178423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return status; \ 179423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } \ 180423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey} 181423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 1820274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey} // namespace 1830274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 1840274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeystatus_t InstalldNativeService::start() { 1850274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey IPCThreadState::self()->disableBackgroundScheduling(true); 1860274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey status_t ret = BinderService<InstalldNativeService>::publish(); 1870274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey if (ret != android::OK) { 1880274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey return ret; 1890274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey } 1900274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey sp<ProcessState> ps(ProcessState::self()); 1910274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey ps->startThreadPool(); 1920274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey ps->giveThreadPoolName(); 1930274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey return android::OK; 1940274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey} 1950274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 1960274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeystatus_t InstalldNativeService::dump(int fd, const Vector<String16> & /* args */) { 19766b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey auto out = std::fstream(StringPrintf("/proc/self/fd/%d", fd)); 1980274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const binder::Status dump_permission = checkPermission(kDump); 1990274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey if (!dump_permission.isOk()) { 20066b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey out << dump_permission.toString8() << endl; 2010274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey return PERMISSION_DENIED; 2020274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey } 20366b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 20466b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey 20588ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey out << "installd is happy!" << endl; 20688ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey 207b26786d647b624498c11405075e5223d1853f30aJeff Sharkey { 208d712f0ccc120357e1267a04ac6de9dd732b27e76Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mMountsLock); 209d712f0ccc120357e1267a04ac6de9dd732b27e76Jeff Sharkey out << endl << "Storage mounts:" << endl; 210d712f0ccc120357e1267a04ac6de9dd732b27e76Jeff Sharkey for (const auto& n : mStorageMounts) { 211d712f0ccc120357e1267a04ac6de9dd732b27e76Jeff Sharkey out << " " << n.first << " = " << n.second << endl; 212d712f0ccc120357e1267a04ac6de9dd732b27e76Jeff Sharkey } 213d712f0ccc120357e1267a04ac6de9dd732b27e76Jeff Sharkey 214d712f0ccc120357e1267a04ac6de9dd732b27e76Jeff Sharkey out << endl << "Quota reverse mounts:" << endl; 215d712f0ccc120357e1267a04ac6de9dd732b27e76Jeff Sharkey for (const auto& n : mQuotaReverseMounts) { 216b26786d647b624498c11405075e5223d1853f30aJeff Sharkey out << " " << n.first << " = " << n.second << endl; 217b26786d647b624498c11405075e5223d1853f30aJeff Sharkey } 21866b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey } 21988ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey 220b26786d647b624498c11405075e5223d1853f30aJeff Sharkey { 221d712f0ccc120357e1267a04ac6de9dd732b27e76Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mQuotasLock); 222b26786d647b624498c11405075e5223d1853f30aJeff Sharkey out << endl << "Per-UID cache quotas:" << endl; 223b26786d647b624498c11405075e5223d1853f30aJeff Sharkey for (const auto& n : mCacheQuotas) { 224b26786d647b624498c11405075e5223d1853f30aJeff Sharkey out << " " << n.first << " = " << n.second << endl; 225b26786d647b624498c11405075e5223d1853f30aJeff Sharkey } 22688ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey } 22788ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey 22866b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey out << endl; 22966b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey out.flush(); 2300274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 2310274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey return NO_ERROR; 2320274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey} 2330274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 23422f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey/** 23522f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey * Perform restorecon of the given path, but only perform recursive restorecon 23622f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey * if the label of that top-level file actually changed. This can save us 23722f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey * significant time by avoiding no-op traversals of large filesystem trees. 23822f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey */ 23935b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkeystatic int restorecon_app_data_lazy(const std::string& path, const std::string& seInfo, uid_t uid, 24035b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey bool existing) { 24122f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey int res = 0; 24222f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey char* before = nullptr; 24322f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey char* after = nullptr; 24422f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey 24522f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey // Note that SELINUX_ANDROID_RESTORECON_DATADATA flag is set by 24622f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey // libselinux. Not needed here. 24722f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey 2488567ebfaa428d6454431067da68765adde328800Jeff Sharkey if (lgetfilecon(path.c_str(), &before) < 0) { 24922f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey PLOG(ERROR) << "Failed before getfilecon for " << path; 25022f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey goto fail; 25122f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey } 2520274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey if (selinux_android_restorecon_pkgdir(path.c_str(), seInfo.c_str(), uid, 0) < 0) { 25322f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey PLOG(ERROR) << "Failed top-level restorecon for " << path; 25422f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey goto fail; 25522f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey } 2568567ebfaa428d6454431067da68765adde328800Jeff Sharkey if (lgetfilecon(path.c_str(), &after) < 0) { 25722f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey PLOG(ERROR) << "Failed after getfilecon for " << path; 25822f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey goto fail; 25922f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey } 26022f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey 26122f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey // If the initial top-level restorecon above changed the label, then go 26222f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey // back and restorecon everything recursively 26322f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey if (strcmp(before, after)) { 26435b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey if (existing) { 26535b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey LOG(DEBUG) << "Detected label change from " << before << " to " << after << " at " 26635b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey << path << "; running recursive restorecon"; 26735b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey } 2680274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey if (selinux_android_restorecon_pkgdir(path.c_str(), seInfo.c_str(), uid, 26922f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey SELINUX_ANDROID_RESTORECON_RECURSE) < 0) { 27022f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey PLOG(ERROR) << "Failed recursive restorecon for " << path; 27122f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey goto fail; 27222f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey } 27322f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey } 27422f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey 27522f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey goto done; 27622f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkeyfail: 27722f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey res = -1; 27822f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkeydone: 27922f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey free(before); 28022f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey free(after); 28122f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey return res; 28222f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey} 28322f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey 2840274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeystatic int restorecon_app_data_lazy(const std::string& parent, const char* name, 28535b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey const std::string& seInfo, uid_t uid, bool existing) { 28635b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey return restorecon_app_data_lazy(StringPrintf("%s/%s", parent.c_str(), name), seInfo, uid, 28735b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey existing); 2888567ebfaa428d6454431067da68765adde328800Jeff Sharkey} 2898567ebfaa428d6454431067da68765adde328800Jeff Sharkey 29022f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkeystatic int prepare_app_dir(const std::string& path, mode_t target_mode, uid_t uid) { 2911f6a7f117dcfa0e2cdb870a1bd4b20d34363ad73Jeff Sharkey if (fs_prepare_dir_strict(path.c_str(), target_mode, uid, uid) != 0) { 2921f6a7f117dcfa0e2cdb870a1bd4b20d34363ad73Jeff Sharkey PLOG(ERROR) << "Failed to prepare " << path; 2931f6a7f117dcfa0e2cdb870a1bd4b20d34363ad73Jeff Sharkey return -1; 2941f6a7f117dcfa0e2cdb870a1bd4b20d34363ad73Jeff Sharkey } 2951f6a7f117dcfa0e2cdb870a1bd4b20d34363ad73Jeff Sharkey return 0; 2961f6a7f117dcfa0e2cdb870a1bd4b20d34363ad73Jeff Sharkey} 2971f6a7f117dcfa0e2cdb870a1bd4b20d34363ad73Jeff Sharkey 298e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey/** 299e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey * Ensure that we have a hard-limit quota to protect against abusive apps; 300e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey * they should never use more than 90% of blocks or 50% of inodes. 301e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey */ 302e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkeystatic int prepare_app_quota(const std::unique_ptr<std::string>& uuid, const std::string& device, 303e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey uid_t uid) { 304e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey if (device.empty()) return 0; 305e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey 306e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey struct dqblk dq; 307e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey if (quotactl(QCMD(Q_GETQUOTA, USRQUOTA), device.c_str(), uid, 308e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey reinterpret_cast<char*>(&dq)) != 0) { 309e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey PLOG(WARNING) << "Failed to find quota for " << uid; 310e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey return -1; 311e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey } 312e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey 313e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey if ((dq.dqb_bhardlimit == 0) || (dq.dqb_ihardlimit == 0)) { 314e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey auto path = create_data_path(uuid ? uuid->c_str() : nullptr); 315e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey struct statvfs stat; 316e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey if (statvfs(path.c_str(), &stat) != 0) { 317e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey PLOG(WARNING) << "Failed to statvfs " << path; 318e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey return -1; 319e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey } 320e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey 321e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey dq.dqb_valid = QIF_LIMITS; 322e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey dq.dqb_bhardlimit = (((stat.f_blocks * stat.f_frsize) / 10) * 9) / QIF_DQBLKSIZE; 323e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey dq.dqb_ihardlimit = (stat.f_files / 2); 324e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey if (quotactl(QCMD(Q_SETQUOTA, USRQUOTA), device.c_str(), uid, 325e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey reinterpret_cast<char*>(&dq)) != 0) { 326e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey PLOG(WARNING) << "Failed to set hard quota for " << uid; 327e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey return -1; 328e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey } else { 329e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey LOG(DEBUG) << "Applied hard quotas for " << uid; 330e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey return 0; 331e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey } 332e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey } else { 333e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey // Hard quota already set; assume it's reasonable 334e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey return 0; 335e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey } 336e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey} 337e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey 3380274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeybinder::Status InstalldNativeService::createAppData(const std::unique_ptr<std::string>& uuid, 3390274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const std::string& packageName, int32_t userId, int32_t flags, int32_t appId, 34036a900a0db526d63e899ed5d5b0a463ad7f9236eJeff Sharkey const std::string& seInfo, int32_t targetSdkVersion, int64_t* _aidl_return) { 3410274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 342423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 343423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 3447a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 3450274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 3460274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 3470274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const char* pkgname = packageName.c_str(); 3480274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 34936a900a0db526d63e899ed5d5b0a463ad7f9236eJeff Sharkey // Assume invalid inode unless filled in below 35036a900a0db526d63e899ed5d5b0a463ad7f9236eJeff Sharkey if (_aidl_return != nullptr) *_aidl_return = -1; 35136a900a0db526d63e899ed5d5b0a463ad7f9236eJeff Sharkey 35224ef15bd009204b73ebf0e94ba3d868177a5f44cJeff Sharkey int32_t uid = multiuser_get_uid(userId, appId); 35324ef15bd009204b73ebf0e94ba3d868177a5f44cJeff Sharkey int32_t cacheGid = multiuser_get_cache_gid(userId, appId); 3543dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey mode_t targetMode = targetSdkVersion >= MIN_RESTRICTED_HOME_SDK_VERSION ? 0700 : 0751; 3553dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 35624ef15bd009204b73ebf0e94ba3d868177a5f44cJeff Sharkey // If UID doesn't have a specific cache GID, use UID value 35724ef15bd009204b73ebf0e94ba3d868177a5f44cJeff Sharkey if (cacheGid == -1) { 35824ef15bd009204b73ebf0e94ba3d868177a5f44cJeff Sharkey cacheGid = uid; 35924ef15bd009204b73ebf0e94ba3d868177a5f44cJeff Sharkey } 36024ef15bd009204b73ebf0e94ba3d868177a5f44cJeff Sharkey 361aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (flags & FLAG_STORAGE_CE) { 3620274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey auto path = create_data_user_ce_package_path(uuid_, userId, pkgname); 36335b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey bool existing = (access(path.c_str(), F_OK) == 0); 36435b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey 3653dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (prepare_app_dir(path, targetMode, uid) || 3663dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey prepare_app_cache_dir(path, "cache", 02771, uid, cacheGid) || 3673dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey prepare_app_cache_dir(path, "code_cache", 02771, uid, cacheGid)) { 368423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to prepare " + path); 36922f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey } 37022f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey 37122f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey // Consider restorecon over contents if label changed 37235b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey if (restorecon_app_data_lazy(path, seInfo, uid, existing) || 37335b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey restorecon_app_data_lazy(path, "cache", seInfo, uid, existing) || 37435b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey restorecon_app_data_lazy(path, "code_cache", seInfo, uid, existing)) { 375423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to restorecon " + path); 376c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey } 3771f6a7f117dcfa0e2cdb870a1bd4b20d34363ad73Jeff Sharkey 3781f6a7f117dcfa0e2cdb870a1bd4b20d34363ad73Jeff Sharkey // Remember inode numbers of cache directories so that we can clear 3791f6a7f117dcfa0e2cdb870a1bd4b20d34363ad73Jeff Sharkey // contents while CE storage is locked 3801f6a7f117dcfa0e2cdb870a1bd4b20d34363ad73Jeff Sharkey if (write_path_inode(path, "cache", kXattrInodeCache) || 3811f6a7f117dcfa0e2cdb870a1bd4b20d34363ad73Jeff Sharkey write_path_inode(path, "code_cache", kXattrInodeCodeCache)) { 382423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to write_path_inode for " + path); 383c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey } 38436a900a0db526d63e899ed5d5b0a463ad7f9236eJeff Sharkey 38536a900a0db526d63e899ed5d5b0a463ad7f9236eJeff Sharkey // And return the CE inode of the top-level data directory so we can 38636a900a0db526d63e899ed5d5b0a463ad7f9236eJeff Sharkey // clear contents while CE storage is locked 38736a900a0db526d63e899ed5d5b0a463ad7f9236eJeff Sharkey if ((_aidl_return != nullptr) 38836a900a0db526d63e899ed5d5b0a463ad7f9236eJeff Sharkey && get_path_inode(path, reinterpret_cast<ino_t*>(_aidl_return)) != 0) { 38936a900a0db526d63e899ed5d5b0a463ad7f9236eJeff Sharkey return error("Failed to get_path_inode for " + path); 39036a900a0db526d63e899ed5d5b0a463ad7f9236eJeff Sharkey } 39194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 392aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (flags & FLAG_STORAGE_DE) { 3930274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey auto path = create_data_user_de_package_path(uuid_, userId, pkgname); 39435b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey bool existing = (access(path.c_str(), F_OK) == 0); 39535b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey 3963dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (prepare_app_dir(path, targetMode, uid) || 3973dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey prepare_app_cache_dir(path, "cache", 02771, uid, cacheGid) || 3983dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey prepare_app_cache_dir(path, "code_cache", 02771, uid, cacheGid)) { 399423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to prepare " + path); 400c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey } 4016a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 40222f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey // Consider restorecon over contents if label changed 4037b6c840c4281aeb2cc42ed022d1708fec0cda2ecJeff Sharkey if (restorecon_app_data_lazy(path, seInfo, uid, existing) || 4047b6c840c4281aeb2cc42ed022d1708fec0cda2ecJeff Sharkey restorecon_app_data_lazy(path, "cache", seInfo, uid, existing) || 4057b6c840c4281aeb2cc42ed022d1708fec0cda2ecJeff Sharkey restorecon_app_data_lazy(path, "code_cache", seInfo, uid, existing)) { 406423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to restorecon " + path); 40722f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey } 40822f6fd53c3edaa1862cbc3ea54a0f461173c72fbJeff Sharkey 409e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey if (prepare_app_quota(uuid, findQuotaDeviceForUuid(uuid), uid)) { 410e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey return error("Failed to set hard quota " + path); 411e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey } 412e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey 41390aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey if (property_get_bool("dalvik.vm.usejitprofiles", false)) { 414114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle const std::string profile_dir = 415114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle create_primary_current_profile_package_dir_path(userId, pkgname); 4166a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle // read-write-execute only for the app user. 417114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle if (fs_prepare_dir_strict(profile_dir.c_str(), 0700, uid, uid) != 0) { 418114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle return error("Failed to prepare " + profile_dir); 4196a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 420114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle const std::string profile_file = create_current_profile_path(userId, pkgname, 421114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle /*is_secondary_dex*/false); 422bc56e7dcf07e633867e1525435373a1231216b22Calin Juravle // read-write only for the app user. 423bc56e7dcf07e633867e1525435373a1231216b22Calin Juravle if (fs_prepare_file_strict(profile_file.c_str(), 0600, uid, uid) != 0) { 424114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle return error("Failed to prepare " + profile_file); 425bc56e7dcf07e633867e1525435373a1231216b22Calin Juravle } 426114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle const std::string ref_profile_path = 427114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle create_primary_reference_profile_package_dir_path(pkgname); 4286a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle // dex2oat/profman runs under the shared app gid and it needs to read/write reference 4296a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle // profiles. 430adddd9876e145879169369913b5afe469b17c02dJeff Sharkey int shared_app_gid = multiuser_get_shared_gid(0, appId); 4312e6dc9baaaf5d50b42ce36435e13c2559808b837Jeff Sharkey if ((shared_app_gid != -1) && fs_prepare_dir_strict( 4326a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle ref_profile_path.c_str(), 0700, shared_app_gid, shared_app_gid) != 0) { 433423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to prepare " + ref_profile_path); 4346a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 4356a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 43694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 437423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 43894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 43994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 440c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkeybinder::Status InstalldNativeService::migrateAppData(const std::unique_ptr<std::string>& uuid, 441c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const std::string& packageName, int32_t userId, int32_t flags) { 442c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 443423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 444423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 4457a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 446423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 447c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 448c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* pkgname = packageName.c_str(); 449c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey 450cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey // This method only exists to upgrade system apps that have requested 451cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey // forceDeviceEncrypted, so their default storage always lives in a 452cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey // consistent location. This only works on non-FBE devices, since we 453cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey // never want to risk exposing data on a device with real CE/DE storage. 454cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey 455c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey auto ce_path = create_data_user_ce_package_path(uuid_, userId, pkgname); 456c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey auto de_path = create_data_user_de_package_path(uuid_, userId, pkgname); 457cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey 458cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey // If neither directory is marked as default, assume CE is default 459cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey if (getxattr(ce_path.c_str(), kXattrDefault, nullptr, 0) == -1 460cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey && getxattr(de_path.c_str(), kXattrDefault, nullptr, 0) == -1) { 461cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey if (setxattr(ce_path.c_str(), kXattrDefault, nullptr, 0, 0) != 0) { 462423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to mark default storage " + ce_path); 463cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey } 464cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey } 465cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey 466cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey // Migrate default data location if needed 467cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey auto target = (flags & FLAG_STORAGE_DE) ? de_path : ce_path; 468cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey auto source = (flags & FLAG_STORAGE_DE) ? ce_path : de_path; 469cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey 470cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey if (getxattr(target.c_str(), kXattrDefault, nullptr, 0) == -1) { 471cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey LOG(WARNING) << "Requested default storage " << target 472cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey << " is not active; migrating from " << source; 473cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey if (delete_dir_contents_and_dir(target) != 0) { 474423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to delete " + target); 475cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey } 476cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey if (rename(source.c_str(), target.c_str()) != 0) { 477423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to rename " + source + " to " + target); 478cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey } 479cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey } 480cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey 481423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 482cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey} 483cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey 4846a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 485475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::clearAppProfiles(const std::string& packageName) { 486475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 487423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 4887a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 489423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 490423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey binder::Status res = ok(); 491114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle if (!clear_primary_reference_profile(packageName)) { 492423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to clear reference profile for " + packageName); 493423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 494114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle if (!clear_primary_current_profiles(packageName)) { 495423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to clear current profiles for " + packageName); 496423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 497423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return res; 4986a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle} 4996a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 500c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkeybinder::Status InstalldNativeService::clearAppData(const std::unique_ptr<std::string>& uuid, 501c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const std::string& packageName, int32_t userId, int32_t flags, int64_t ceDataInode) { 502c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 503423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 504423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 5057a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 506423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 507c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 508c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* pkgname = packageName.c_str(); 509c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey 510423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey binder::Status res = ok(); 511aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (flags & FLAG_STORAGE_CE) { 512c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey auto path = create_data_user_ce_package_path(uuid_, userId, pkgname, ceDataInode); 5131f6a7f117dcfa0e2cdb870a1bd4b20d34363ad73Jeff Sharkey if (flags & FLAG_CLEAR_CACHE_ONLY) { 5141f6a7f117dcfa0e2cdb870a1bd4b20d34363ad73Jeff Sharkey path = read_path_inode(path, "cache", kXattrInodeCache); 5151f6a7f117dcfa0e2cdb870a1bd4b20d34363ad73Jeff Sharkey } else if (flags & FLAG_CLEAR_CODE_CACHE_ONLY) { 5161f6a7f117dcfa0e2cdb870a1bd4b20d34363ad73Jeff Sharkey path = read_path_inode(path, "code_cache", kXattrInodeCodeCache); 5171f6a7f117dcfa0e2cdb870a1bd4b20d34363ad73Jeff Sharkey } 518c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey if (access(path.c_str(), F_OK) == 0) { 519423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (delete_dir_contents(path) != 0) { 520423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to delete contents of " + path); 521423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 522c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey } 52394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 524aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (flags & FLAG_STORAGE_DE) { 5251f6a7f117dcfa0e2cdb870a1bd4b20d34363ad73Jeff Sharkey std::string suffix = ""; 5261f6a7f117dcfa0e2cdb870a1bd4b20d34363ad73Jeff Sharkey bool only_cache = false; 5271f6a7f117dcfa0e2cdb870a1bd4b20d34363ad73Jeff Sharkey if (flags & FLAG_CLEAR_CACHE_ONLY) { 5281f6a7f117dcfa0e2cdb870a1bd4b20d34363ad73Jeff Sharkey suffix = CACHE_DIR_POSTFIX; 5291f6a7f117dcfa0e2cdb870a1bd4b20d34363ad73Jeff Sharkey only_cache = true; 5301f6a7f117dcfa0e2cdb870a1bd4b20d34363ad73Jeff Sharkey } else if (flags & FLAG_CLEAR_CODE_CACHE_ONLY) { 5311f6a7f117dcfa0e2cdb870a1bd4b20d34363ad73Jeff Sharkey suffix = CODE_CACHE_DIR_POSTFIX; 5321f6a7f117dcfa0e2cdb870a1bd4b20d34363ad73Jeff Sharkey only_cache = true; 5331f6a7f117dcfa0e2cdb870a1bd4b20d34363ad73Jeff Sharkey } 5341f6a7f117dcfa0e2cdb870a1bd4b20d34363ad73Jeff Sharkey 535c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey auto path = create_data_user_de_package_path(uuid_, userId, pkgname) + suffix; 536c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey if (access(path.c_str(), F_OK) == 0) { 537423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (delete_dir_contents(path) != 0) { 538423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to delete contents of " + path); 539423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 540c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey } 541edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle if (!only_cache) { 542114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle if (!clear_primary_current_profile(packageName, userId)) { 543423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to clear current profile for " + packageName); 544edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle } 545edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle } 54694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 547423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return res; 548c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey} 54994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 55076268c56febde9a77183387fbd4baabe6694e6b5Calin Juravlestatic int destroy_app_reference_profile(const std::string& pkgname) { 5517535e8e1995398befc866062d63c05d90eb70cccCalin Juravle return delete_dir_contents_and_dir( 552114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle create_primary_reference_profile_package_dir_path(pkgname), 5537535e8e1995398befc866062d63c05d90eb70cccCalin Juravle /*ignore_if_missing*/ true); 5547535e8e1995398befc866062d63c05d90eb70cccCalin Juravle} 5557535e8e1995398befc866062d63c05d90eb70cccCalin Juravle 55676268c56febde9a77183387fbd4baabe6694e6b5Calin Juravlestatic int destroy_app_current_profiles(const std::string& pkgname, userid_t userid) { 557b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravle return delete_dir_contents_and_dir( 558114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle create_primary_current_profile_package_dir_path(userid, pkgname), 559b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravle /*ignore_if_missing*/ true); 560edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle} 561edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle 562475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::destroyAppProfiles(const std::string& packageName) { 563475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 564423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 5657a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 566423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 567423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey binder::Status res = ok(); 568edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle std::vector<userid_t> users = get_known_users(/*volume_uuid*/ nullptr); 569edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle for (auto user : users) { 57076268c56febde9a77183387fbd4baabe6694e6b5Calin Juravle if (destroy_app_current_profiles(packageName, user) != 0) { 571423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to destroy current profiles for " + packageName); 572423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 573edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle } 57476268c56febde9a77183387fbd4baabe6694e6b5Calin Juravle if (destroy_app_reference_profile(packageName) != 0) { 575423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to destroy reference profile for " + packageName); 576423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 577423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return res; 578caa6b809a5ed024a8f3f551c5c109a5ad50ab772Calin Juravle} 579caa6b809a5ed024a8f3f551c5c109a5ad50ab772Calin Juravle 580c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkeybinder::Status InstalldNativeService::destroyAppData(const std::unique_ptr<std::string>& uuid, 581c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const std::string& packageName, int32_t userId, int32_t flags, int64_t ceDataInode) { 582c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 583423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 584423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 5857a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 586423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 587c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 588c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* pkgname = packageName.c_str(); 589c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey 590423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey binder::Status res = ok(); 591aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (flags & FLAG_STORAGE_CE) { 592423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey auto path = create_data_user_ce_package_path(uuid_, userId, pkgname, ceDataInode); 593423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (delete_dir_contents_and_dir(path) != 0) { 594423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to delete " + path); 595423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 59694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 597aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (flags & FLAG_STORAGE_DE) { 598423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey auto path = create_data_user_de_package_path(uuid_, userId, pkgname); 599423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (delete_dir_contents_and_dir(path) != 0) { 600423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to delete " + path); 601423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 60276268c56febde9a77183387fbd4baabe6694e6b5Calin Juravle destroy_app_current_profiles(packageName, userId); 6037535e8e1995398befc866062d63c05d90eb70cccCalin Juravle // TODO(calin): If the package is still installed by other users it's probably 6047535e8e1995398befc866062d63c05d90eb70cccCalin Juravle // beneficial to keep the reference profile around. 6057535e8e1995398befc866062d63c05d90eb70cccCalin Juravle // Verify if it's ok to do that. 60676268c56febde9a77183387fbd4baabe6694e6b5Calin Juravle destroy_app_reference_profile(packageName); 60794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 608423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return res; 60994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 61094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 611e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkeystatic gid_t get_cache_gid(uid_t uid) { 612e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey int32_t gid = multiuser_get_cache_gid(multiuser_get_user_id(uid), multiuser_get_app_id(uid)); 613e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey return (gid != -1) ? gid : uid; 614e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey} 615e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey 616e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkeybinder::Status InstalldNativeService::fixupAppData(const std::unique_ptr<std::string>& uuid, 617e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey int32_t flags) { 618e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 619e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 620e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 621e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey 622e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 623e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey for (auto user : get_known_users(uuid_)) { 624e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey ATRACE_BEGIN("fixup user"); 625e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey FTS* fts; 626e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey FTSENT* p; 627d9ff77fa4b934b54e287ba259cb02a0eee0d9ce8Jeff Sharkey auto ce_path = create_data_user_ce_path(uuid_, user); 628d9ff77fa4b934b54e287ba259cb02a0eee0d9ce8Jeff Sharkey auto de_path = create_data_user_de_path(uuid_, user); 629d9ff77fa4b934b54e287ba259cb02a0eee0d9ce8Jeff Sharkey char *argv[] = { (char*) ce_path.c_str(), (char*) de_path.c_str(), nullptr }; 630e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL))) { 631e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey return error("Failed to fts_open"); 632e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey } 633e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey while ((p = fts_read(fts)) != nullptr) { 634e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey if (p->fts_info == FTS_D && p->fts_level == 1) { 635e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey // Track down inodes of cache directories 636e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey uint64_t raw = 0; 637e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey ino_t inode_cache = 0; 638e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey ino_t inode_code_cache = 0; 639e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey if (getxattr(p->fts_path, kXattrInodeCache, &raw, sizeof(raw)) == sizeof(raw)) { 640e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey inode_cache = raw; 641e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey } 642e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey if (getxattr(p->fts_path, kXattrInodeCodeCache, &raw, sizeof(raw)) == sizeof(raw)) { 643e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey inode_code_cache = raw; 644e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey } 645e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey 646e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey // Figure out expected GID of each child 647e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey FTSENT* child = fts_children(fts, 0); 648e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey while (child != nullptr) { 649e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey if ((child->fts_statp->st_ino == inode_cache) 650e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey || (child->fts_statp->st_ino == inode_code_cache) 651e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey || !strcmp(child->fts_name, "cache") 652e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey || !strcmp(child->fts_name, "code_cache")) { 653e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey child->fts_number = get_cache_gid(p->fts_statp->st_uid); 654e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey } else { 655e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey child->fts_number = p->fts_statp->st_uid; 656e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey } 657e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey child = child->fts_link; 658e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey } 659e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey } else if (p->fts_level >= 2) { 660e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey if (p->fts_level > 2) { 661e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey // Inherit GID from parent once we're deeper into tree 662e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey p->fts_number = p->fts_parent->fts_number; 663e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey } 664e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey 665e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey uid_t uid = p->fts_parent->fts_statp->st_uid; 666e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey gid_t cache_gid = get_cache_gid(uid); 667e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey gid_t expected = p->fts_number; 668e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey gid_t actual = p->fts_statp->st_gid; 669e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey if (actual == expected) { 670e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey#if FIXUP_DEBUG 671e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey LOG(DEBUG) << "Ignoring " << p->fts_path << " with expected GID " << expected; 672e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey#endif 673e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey if (!(flags & FLAG_FORCE)) { 674e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey fts_set(fts, p, FTS_SKIP); 675e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey } 676e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey } else if ((actual == uid) || (actual == cache_gid)) { 677e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey // Only consider fixing up when current GID belongs to app 678e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey if (p->fts_info != FTS_D) { 679e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey LOG(INFO) << "Fixing " << p->fts_path << " with unexpected GID " << actual 680e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey << " instead of " << expected; 681e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey } 682e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey switch (p->fts_info) { 683e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey case FTS_DP: 684e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey // If we're moving towards cache GID, we need to set S_ISGID 685e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey if (expected == cache_gid) { 686e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey if (chmod(p->fts_path, 02771) != 0) { 687e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey PLOG(WARNING) << "Failed to chmod " << p->fts_path; 688e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey } 689e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey } 690e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey // Intentional fall through to also set GID 691e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey case FTS_F: 692e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey if (chown(p->fts_path, -1, expected) != 0) { 693e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey PLOG(WARNING) << "Failed to chown " << p->fts_path; 694e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey } 695e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey break; 696e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey case FTS_SL: 697e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey case FTS_SLNONE: 698e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey if (lchown(p->fts_path, -1, expected) != 0) { 699e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey PLOG(WARNING) << "Failed to chown " << p->fts_path; 700e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey } 701e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey break; 702e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey } 703e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey } else { 704e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey // Ignore all other GID transitions, since they're kinda shady 705e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey LOG(WARNING) << "Ignoring " << p->fts_path << " with unexpected GID " << actual 706e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey << " instead of " << expected; 707e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey } 708e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey } 709e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey } 710e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey fts_close(fts); 711e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey ATRACE_END(); 712e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey } 713e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey return ok(); 714e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey} 715e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey 7160274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeybinder::Status InstalldNativeService::moveCompleteApp(const std::unique_ptr<std::string>& fromUuid, 7170274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const std::unique_ptr<std::string>& toUuid, const std::string& packageName, 7180274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const std::string& dataAppName, int32_t appId, const std::string& seInfo, 7190274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey int32_t targetSdkVersion) { 720423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 721423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(fromUuid); 722423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(toUuid); 723423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 7247a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 7250274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 7260274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const char* from_uuid = fromUuid ? fromUuid->c_str() : nullptr; 7270274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const char* to_uuid = toUuid ? toUuid->c_str() : nullptr; 7280274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const char* package_name = packageName.c_str(); 7290274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const char* data_app_name = dataAppName.c_str(); 7300274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 731423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey binder::Status res = ok(); 732e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey std::vector<userid_t> users = get_known_users(from_uuid); 733e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 734d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey // Copy app 735d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey { 73651c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto from = create_data_app_package_path(from_uuid, data_app_name); 73751c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto to = create_data_app_package_path(to_uuid, data_app_name); 73851c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto to_parent = create_data_app_path(to_uuid); 739d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey 740d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey char *argv[] = { 741d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey (char*) kCpPath, 742d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey (char*) "-F", /* delete any existing destination file first (--remove-destination) */ 743d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey (char*) "-p", /* preserve timestamps, ownership, and permissions */ 744d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey (char*) "-R", /* recurse into subdirectories (DEST must be a directory) */ 745d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey (char*) "-P", /* Do not follow symlinks [default] */ 746d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey (char*) "-d", /* don't dereference symlinks */ 747d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey (char*) from.c_str(), 748d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey (char*) to_parent.c_str() 749d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey }; 750d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey 751d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey LOG(DEBUG) << "Copying " << from << " to " << to; 752d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true); 753d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey if (rc != 0) { 754423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error(rc, "Failed copying " + from + " to " + to); 755d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey goto fail; 756d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey } 757d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey 758d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey if (selinux_android_restorecon(to.c_str(), SELINUX_ANDROID_RESTORECON_RECURSE) != 0) { 759423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to restorecon " + to); 760d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey goto fail; 761d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey } 762d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey } 763d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey 764d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey // Copy private data for all known users 765e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey for (auto user : users) { 766e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 767e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey // Data source may not exist for all users; that's okay 76851c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto from_ce = create_data_user_ce_package_path(from_uuid, user, package_name); 76951c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey if (access(from_ce.c_str(), F_OK) != 0) { 77051c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey LOG(INFO) << "Missing source " << from_ce; 771e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey continue; 772e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey } 773e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 7740274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey if (!createAppData(toUuid, packageName, user, FLAG_STORAGE_CE | FLAG_STORAGE_DE, appId, 77536a900a0db526d63e899ed5d5b0a463ad7f9236eJeff Sharkey seInfo, targetSdkVersion, nullptr).isOk()) { 776423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to create package target"); 777e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey goto fail; 778e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey } 779e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 780e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey char *argv[] = { 781e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey (char*) kCpPath, 782e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey (char*) "-F", /* delete any existing destination file first (--remove-destination) */ 783e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey (char*) "-p", /* preserve timestamps, ownership, and permissions */ 784e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey (char*) "-R", /* recurse into subdirectories (DEST must be a directory) */ 785e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey (char*) "-P", /* Do not follow symlinks [default] */ 786e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey (char*) "-d", /* don't dereference symlinks */ 78751c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey nullptr, 78851c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey nullptr 789e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey }; 790e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 79151c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey { 79251c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto from = create_data_user_de_package_path(from_uuid, user, package_name); 79351c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto to = create_data_user_de_path(to_uuid, user); 79451c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey argv[6] = (char*) from.c_str(); 79551c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey argv[7] = (char*) to.c_str(); 79651c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey 79751c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey LOG(DEBUG) << "Copying " << from << " to " << to; 79851c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true); 79951c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey if (rc != 0) { 800423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error(rc, "Failed copying " + from + " to " + to); 80151c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey goto fail; 80251c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey } 80351c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey } 80451c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey { 80551c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto from = create_data_user_ce_package_path(from_uuid, user, package_name); 80651c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto to = create_data_user_ce_path(to_uuid, user); 80751c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey argv[6] = (char*) from.c_str(); 80851c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey argv[7] = (char*) to.c_str(); 80951c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey 81051c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey LOG(DEBUG) << "Copying " << from << " to " << to; 81151c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true); 81251c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey if (rc != 0) { 813423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error(rc, "Failed copying " + from + " to " + to); 81451c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey goto fail; 81551c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey } 816e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey } 817e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 818c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey if (!restoreconAppData(toUuid, packageName, user, FLAG_STORAGE_CE | FLAG_STORAGE_DE, 819c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey appId, seInfo).isOk()) { 820423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to restorecon"); 821c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey goto fail; 822c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey } 823e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey } 824e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 82531f08986f83fa6f2dcf55523b2cf706460aeed7cJeff Sharkey // We let the framework scan the new location and persist that before 82631f08986f83fa6f2dcf55523b2cf706460aeed7cJeff Sharkey // deleting the data in the old location; this ordering ensures that 82731f08986f83fa6f2dcf55523b2cf706460aeed7cJeff Sharkey // we can recover from things like battery pulls. 828423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 829e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 830e36372423000a906bafae68844ebc6c42d09335aJeff Sharkeyfail: 831e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey // Nuke everything we might have already copied 832d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey { 83351c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto to = create_data_app_package_path(to_uuid, data_app_name); 834d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey if (delete_dir_contents(to.c_str(), 1, NULL) != 0) { 835d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey LOG(WARNING) << "Failed to rollback " << to; 836d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey } 837d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey } 838e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey for (auto user : users) { 83951c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey { 84051c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto to = create_data_user_de_package_path(to_uuid, user, package_name); 84151c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey if (delete_dir_contents(to.c_str(), 1, NULL) != 0) { 84251c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey LOG(WARNING) << "Failed to rollback " << to; 84351c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey } 84451c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey } 84551c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey { 84651c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto to = create_data_user_ce_package_path(to_uuid, user, package_name); 84751c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey if (delete_dir_contents(to.c_str(), 1, NULL) != 0) { 84851c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey LOG(WARNING) << "Failed to rollback " << to; 84951c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey } 850e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey } 851e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey } 852423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return res; 853e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey} 854e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 855c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkeybinder::Status InstalldNativeService::createUserData(const std::unique_ptr<std::string>& uuid, 856c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey int32_t userId, int32_t userSerial ATTRIBUTE_UNUSED, int32_t flags) { 857c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 858423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 8597a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 860423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 861c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 862379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey if (flags & FLAG_STORAGE_DE) { 863c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey if (uuid_ == nullptr) { 864423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (ensure_config_user_dirs(userId) != 0) { 8657be5ead81aa4d3b2536556c762bab03445a98155Jeff Sharkey return error(StringPrintf("Failed to ensure dirs for %d", userId)); 866423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 867379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey } 86894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 869e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey 870e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey // Data under /data/media doesn't have an app, but we still want 871e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey // to limit it to prevent abuse. 872e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey if (prepare_app_quota(uuid, findQuotaDeviceForUuid(uuid), 873e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey multiuser_get_uid(userId, AID_MEDIA_RW))) { 874e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey return error("Failed to set hard quota for media_rw"); 875e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey } 876e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey 8777be5ead81aa4d3b2536556c762bab03445a98155Jeff Sharkey return ok(); 878095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee} 879095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee 880c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkeybinder::Status InstalldNativeService::destroyUserData(const std::unique_ptr<std::string>& uuid, 881c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey int32_t userId, int32_t flags) { 882c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 883423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 8847a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 885423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 886c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 887423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey binder::Status res = ok(); 888379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey if (flags & FLAG_STORAGE_DE) { 889423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey auto path = create_data_user_de_path(uuid_, userId); 890423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (delete_dir_contents_and_dir(path, true) != 0) { 891423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to delete " + path); 892423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 893c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey if (uuid_ == nullptr) { 894423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey path = create_data_misc_legacy_path(userId); 895423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (delete_dir_contents_and_dir(path, true) != 0) { 896423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to delete " + path); 897423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 898114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle path = create_primary_cur_profile_dir_path(userId); 899423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (delete_dir_contents_and_dir(path, true) != 0) { 900423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to delete " + path); 901423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 90241ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey } 90394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 904379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey if (flags & FLAG_STORAGE_CE) { 905423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey auto path = create_data_user_ce_path(uuid_, userId); 906423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (delete_dir_contents_and_dir(path, true) != 0) { 907423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to delete " + path); 908423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 909d712f0ccc120357e1267a04ac6de9dd732b27e76Jeff Sharkey path = findDataMediaPath(uuid, userId); 910423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (delete_dir_contents_and_dir(path, true) != 0) { 911423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to delete " + path); 912423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 913379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey } 914423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return res; 91594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 91694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 91794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/* Try to ensure free_size bytes of storage are available. 91894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * Returns 0 on success. 91994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * This is rather simple-minded because doing a full LRU would 92094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * be potentially memory-intensive, and without atime it would 92194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * also require that apps constantly modify file metadata even 92294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * when just reading from the cache, which is pretty awful. 92394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */ 924475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::freeCache(const std::unique_ptr<std::string>& uuid, 92588ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey int64_t freeStorageSize, int32_t flags) { 926475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 927423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 9287a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 929423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 93088ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 931475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey auto data_path = create_data_path(uuid_); 93288ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey auto device = findQuotaDeviceForUuid(uuid); 93388ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey auto noop = (flags & FLAG_FREE_CACHE_NOOP); 93441ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey 93588ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey int64_t free = data_disk_free(data_path); 93688ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey if (free < 0) { 937423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to determine free space for " + data_path); 938475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey } 93994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 940ed909ae8db2f44ce7fe7003c6fee457f13669702Jeff Sharkey int64_t needed = freeStorageSize - free; 941ed909ae8db2f44ce7fe7003c6fee457f13669702Jeff Sharkey LOG(DEBUG) << "Device " << data_path << " has " << free << " free; requested " 942ed909ae8db2f44ce7fe7003c6fee457f13669702Jeff Sharkey << freeStorageSize << "; needed " << needed; 943ed909ae8db2f44ce7fe7003c6fee457f13669702Jeff Sharkey 944ed909ae8db2f44ce7fe7003c6fee457f13669702Jeff Sharkey if (free >= freeStorageSize) { 945ed909ae8db2f44ce7fe7003c6fee457f13669702Jeff Sharkey return ok(); 946ed909ae8db2f44ce7fe7003c6fee457f13669702Jeff Sharkey } 94788ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey 94888ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey if (flags & FLAG_FREE_CACHE_V2) { 94988ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey // This new cache strategy fairly removes files from UIDs by deleting 95088ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey // files from the UIDs which are most over their allocated quota 95188ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey 95288ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey // 1. Create trackers for every known UID 95388ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey ATRACE_BEGIN("create"); 95488ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey std::unordered_map<uid_t, std::shared_ptr<CacheTracker>> trackers; 95588ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey for (auto user : get_known_users(uuid_)) { 95688ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey FTS *fts; 95788ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey FTSENT *p; 958d9ff77fa4b934b54e287ba259cb02a0eee0d9ce8Jeff Sharkey auto ce_path = create_data_user_ce_path(uuid_, user); 959d9ff77fa4b934b54e287ba259cb02a0eee0d9ce8Jeff Sharkey auto de_path = create_data_user_de_path(uuid_, user); 960d712f0ccc120357e1267a04ac6de9dd732b27e76Jeff Sharkey auto media_path = findDataMediaPath(uuid, user) + "/Android/data/"; 961d712f0ccc120357e1267a04ac6de9dd732b27e76Jeff Sharkey char *argv[] = { (char*) ce_path.c_str(), (char*) de_path.c_str(), 962d712f0ccc120357e1267a04ac6de9dd732b27e76Jeff Sharkey (char*) media_path.c_str(), nullptr }; 963b26786d647b624498c11405075e5223d1853f30aJeff Sharkey if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL))) { 96488ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey return error("Failed to fts_open"); 96588ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey } 96688ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey while ((p = fts_read(fts)) != NULL) { 96788ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey if (p->fts_info == FTS_D && p->fts_level == 1) { 96888ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey uid_t uid = p->fts_statp->st_uid; 969d712f0ccc120357e1267a04ac6de9dd732b27e76Jeff Sharkey if (multiuser_get_app_id(uid) == AID_MEDIA_RW) { 970d712f0ccc120357e1267a04ac6de9dd732b27e76Jeff Sharkey uid = (multiuser_get_app_id(p->fts_statp->st_gid) - AID_EXT_GID_START) 971d712f0ccc120357e1267a04ac6de9dd732b27e76Jeff Sharkey + AID_APP_START; 972d712f0ccc120357e1267a04ac6de9dd732b27e76Jeff Sharkey } 97388ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey auto search = trackers.find(uid); 97488ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey if (search != trackers.end()) { 97588ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey search->second->addDataPath(p->fts_path); 97688ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey } else { 97788ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey auto tracker = std::shared_ptr<CacheTracker>(new CacheTracker( 97888ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey multiuser_get_user_id(uid), multiuser_get_app_id(uid), device)); 97988ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey tracker->addDataPath(p->fts_path); 980b26786d647b624498c11405075e5223d1853f30aJeff Sharkey { 981d712f0ccc120357e1267a04ac6de9dd732b27e76Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mQuotasLock); 982b26786d647b624498c11405075e5223d1853f30aJeff Sharkey tracker->cacheQuota = mCacheQuotas[uid]; 983b26786d647b624498c11405075e5223d1853f30aJeff Sharkey } 98488ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey if (tracker->cacheQuota == 0) { 985ed909ae8db2f44ce7fe7003c6fee457f13669702Jeff Sharkey#if MEASURE_DEBUG 98688ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey LOG(WARNING) << "UID " << uid << " has no cache quota; assuming 64MB"; 987ed909ae8db2f44ce7fe7003c6fee457f13669702Jeff Sharkey#endif 98888ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey tracker->cacheQuota = 67108864; 98988ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey } 99088ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey trackers[uid] = tracker; 99188ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey } 99288ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey fts_set(fts, p, FTS_SKIP); 99388ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey } 99488ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey } 99588ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey fts_close(fts); 99688ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey } 99788ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey ATRACE_END(); 99888ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey 99988ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey // 2. Populate tracker stats and insert into priority queue 100088ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey ATRACE_BEGIN("populate"); 100188ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey auto cmp = [](std::shared_ptr<CacheTracker> left, std::shared_ptr<CacheTracker> right) { 100288ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey return (left->getCacheRatio() < right->getCacheRatio()); 100388ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey }; 100488ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey std::priority_queue<std::shared_ptr<CacheTracker>, 100588ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey std::vector<std::shared_ptr<CacheTracker>>, decltype(cmp)> queue(cmp); 100688ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey for (const auto& it : trackers) { 100788ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey it.second->loadStats(); 100888ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey queue.push(it.second); 100988ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey } 101088ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey ATRACE_END(); 101188ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey 101288ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey // 3. Bounce across the queue, freeing items from whichever tracker is 101388ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey // the most over their assigned quota 101488ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey ATRACE_BEGIN("bounce"); 101588ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey std::shared_ptr<CacheTracker> active; 101688ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey while (active || !queue.empty()) { 1017871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey // Only look at apps under quota when explicitly requested 1018871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey if (active && (active->getCacheRatio() < 10000) 1019871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey && !(flags & FLAG_FREE_CACHE_V2_DEFY_QUOTA)) { 1020871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey LOG(DEBUG) << "Active ratio " << active->getCacheRatio() 1021871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey << " isn't over quota, and defy not requested"; 1022871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey break; 1023871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey } 1024871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey 102588ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey // Find the best tracker to work with; this might involve swapping 102688ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey // if the active tracker is no longer the most over quota 102788ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey bool nextBetter = active && !queue.empty() 102888ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey && active->getCacheRatio() < queue.top()->getCacheRatio(); 102988ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey if (!active || nextBetter) { 103088ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey if (active) { 103188ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey // Current tracker still has items, so we'll consider it 103288ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey // again later once it bubbles up to surface 103388ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey queue.push(active); 103488ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey } 103588ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey active = queue.top(); queue.pop(); 103688ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey active->ensureItems(); 103788ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey continue; 103888ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey } 103994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 104088ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey // If no items remain, go find another tracker 104188ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey if (active->items.empty()) { 104288ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey active = nullptr; 104388ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey continue; 104488ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey } else { 104588ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey auto item = active->items.back(); 104688ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey active->items.pop_back(); 104788ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey 104888ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey LOG(DEBUG) << "Purging " << item->toString() << " from " << active->toString(); 104988ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey if (!noop) { 105088ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey item->purge(); 105188ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey } 105288ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey active->cacheUsed -= item->size; 105388ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey needed -= item->size; 105488ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey } 105588ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey 105688ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey // Verify that we're actually done before bailing, since sneaky 105788ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey // apps might be using hardlinks 105888ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey if (needed <= 0) { 105988ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey free = data_disk_free(data_path); 106088ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey needed = freeStorageSize - free; 106188ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey if (needed <= 0) { 106288ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey break; 106388ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey } else { 106488ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey LOG(WARNING) << "Expected to be done but still need " << needed; 106588ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey } 106688ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey } 106788ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey } 106888ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey ATRACE_END(); 106988ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey 107088ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey } else { 10711cb4aafc17daae520a5c3a991f59605ea3ee4ee5Jeff Sharkey return error("Legacy cache logic no longer supported"); 107288ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey } 107388ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey 107488ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey free = data_disk_free(data_path); 107588ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey if (free >= freeStorageSize) { 1076423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 1077475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey } else { 1078423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error(StringPrintf("Failed to free up %" PRId64 " on %s; final free space %" PRId64, 107988ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey freeStorageSize, data_path.c_str(), free)); 1080475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey } 108194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 108294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 1083475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::rmdex(const std::string& codePath, 1084475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const std::string& instructionSet) { 1085475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 10867a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 10877a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey 108894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood char dex_path[PKG_PATH_MAX]; 108994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 1090475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* path = codePath.c_str(); 1091475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* instruction_set = instructionSet.c_str(); 1092475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey 1093770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey if (validate_apk_path(path) && validate_system_app_path(path)) { 1094423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Invalid path " + codePath); 1095770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey } 1096770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey 1097475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey if (!create_cache_path(dex_path, path, instruction_set)) { 1098423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to create cache path for " + codePath); 1099475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey } 110094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 110194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGV("unlink %s\n", dex_path); 110294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (unlink(dex_path) < 0) { 1103423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error(StringPrintf("Failed to unlink %s", dex_path)); 110494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else { 1105423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 110694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 110794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 110894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 11093dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkeystruct stats { 11103dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey int64_t codeSize; 11113dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey int64_t dataSize; 11123dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey int64_t cacheSize; 11133dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey}; 11143dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 1115df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 1116df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkeystatic std::string toString(std::vector<int64_t> values) { 1117df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey std::stringstream res; 1118df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey res << "["; 1119df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey for (size_t i = 0; i < values.size(); i++) { 1120df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey res << values[i]; 1121df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (i < values.size() - 1) { 1122df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey res << ","; 1123df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1124df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1125df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey res << "]"; 1126df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey return res.str(); 1127df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey} 1128df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 11293dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 1130df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkeystatic void collectQuotaStats(const std::string& device, int32_t userId, 11317bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey int32_t appId, struct stats* stats, struct stats* extStats) { 1132df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (device.empty()) return; 1133df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1134df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey struct dqblk dq; 11353dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 1136a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey if (stats != nullptr) { 1137a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey uid_t uid = multiuser_get_uid(userId, appId); 1138a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey if (quotactl(QCMD(Q_GETQUOTA, USRQUOTA), device.c_str(), uid, 11393dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey reinterpret_cast<char*>(&dq)) != 0) { 11403dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (errno != ESRCH) { 1141a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey PLOG(ERROR) << "Failed to quotactl " << device << " for UID " << uid; 11423dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 11433dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } else { 1144df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 1145a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey LOG(DEBUG) << "quotactl() for UID " << uid << " " << dq.dqb_curspace; 1146df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 1147a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey stats->dataSize += dq.dqb_curspace; 11483dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 11493dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 1150a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey int cacheGid = multiuser_get_cache_gid(userId, appId); 1151a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey if (cacheGid != -1) { 1152a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), cacheGid, 1153a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey reinterpret_cast<char*>(&dq)) != 0) { 1154a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey if (errno != ESRCH) { 1155a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey PLOG(ERROR) << "Failed to quotactl " << device << " for GID " << cacheGid; 1156a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey } 1157a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey } else { 1158a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey#if MEASURE_DEBUG 1159a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey LOG(DEBUG) << "quotactl() for GID " << cacheGid << " " << dq.dqb_curspace; 1160a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey#endif 1161a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey stats->cacheSize += dq.dqb_curspace; 11627bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey } 1163a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey } 1164a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey 1165a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey int sharedGid = multiuser_get_shared_gid(0, appId); 1166a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey if (sharedGid != -1) { 1167a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), sharedGid, 1168a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey reinterpret_cast<char*>(&dq)) != 0) { 1169a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey if (errno != ESRCH) { 1170a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey PLOG(ERROR) << "Failed to quotactl " << device << " for GID " << sharedGid; 1171a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey } 1172a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey } else { 11737bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey#if MEASURE_DEBUG 1174a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey LOG(DEBUG) << "quotactl() for GID " << sharedGid << " " << dq.dqb_curspace; 11757bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey#endif 1176a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey stats->codeSize += dq.dqb_curspace; 1177a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey } 11787bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey } 11797bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey } 11807bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey 1181a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey if (extStats != nullptr) { 1182a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey int extGid = multiuser_get_ext_gid(userId, appId); 1183a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey if (extGid != -1) { 1184a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), extGid, 1185a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey reinterpret_cast<char*>(&dq)) != 0) { 1186a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey if (errno != ESRCH) { 1187a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey PLOG(ERROR) << "Failed to quotactl " << device << " for GID " << extGid; 1188a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey } 1189a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey } else { 1190a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey#if MEASURE_DEBUG 1191a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey LOG(DEBUG) << "quotactl() for GID " << extGid << " " << dq.dqb_curspace; 1192a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey#endif 1193a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey extStats->dataSize += dq.dqb_curspace; 11943dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 1195a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey } 1196a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey 1197a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey int extCacheGid = multiuser_get_ext_cache_gid(userId, appId); 1198a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey if (extCacheGid != -1) { 1199a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), extCacheGid, 1200a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey reinterpret_cast<char*>(&dq)) != 0) { 1201a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey if (errno != ESRCH) { 1202a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey PLOG(ERROR) << "Failed to quotactl " << device << " for GID " << extCacheGid; 1203a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey } 1204a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey } else { 1205df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 1206a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey LOG(DEBUG) << "quotactl() for GID " << extCacheGid << " " << dq.dqb_curspace; 1207df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 1208a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey extStats->dataSize += dq.dqb_curspace; 1209a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey extStats->cacheSize += dq.dqb_curspace; 1210a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey } 12113dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 12123dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 1213df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey} 1214df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1215df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkeystatic void collectManualStats(const std::string& path, struct stats* stats) { 121694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood DIR *d; 121794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int dfd; 121894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood struct dirent *de; 121994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood struct stat s; 1220d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey 12212f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey d = opendir(path.c_str()); 12222f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey if (d == nullptr) { 12233dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (errno != ENOENT) { 12243dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey PLOG(WARNING) << "Failed to open " << path; 12253dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 12262f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey return; 122794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 12282f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey dfd = dirfd(d); 12292f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey while ((de = readdir(d))) { 12302f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey const char *name = de->d_name; 1231d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey 12323dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey int64_t size = 0; 12332f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) { 12343dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey size = s.st_blocks * 512; 123594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 123694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 12372f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey if (de->d_type == DT_DIR) { 12383dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (!strcmp(name, ".")) { 12393dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // Don't recurse, but still count node size 12403dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } else if (!strcmp(name, "..")) { 12413dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // Don't recurse or count node size 12423dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey continue; 12433dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } else { 12443dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // Measure all children nodes 12453dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey size = 0; 12463dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey calculate_tree_size(StringPrintf("%s/%s", path.c_str(), name), &size); 12472f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey } 12483dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 12492f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey if (!strcmp(name, "cache") || !strcmp(name, "code_cache")) { 12503dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey stats->cacheSize += size; 12512f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey } 125294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 12533dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 12543dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // Legacy symlink isn't owned by app 12553dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (de->d_type == DT_LNK && !strcmp(name, "lib")) { 12563dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey continue; 12573dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 12583dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 12593dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // Everything found inside is considered data 12603dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey stats->dataSize += size; 126194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 12622f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey closedir(d); 12632f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey} 126494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 1265df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkeystatic void collectManualStatsForUser(const std::string& path, struct stats* stats, 1266df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey bool exclude_apps = false) { 1267df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey DIR *d; 1268df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int dfd; 1269df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey struct dirent *de; 1270df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey struct stat s; 1271df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1272df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey d = opendir(path.c_str()); 1273df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (d == nullptr) { 1274df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (errno != ENOENT) { 1275df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey PLOG(WARNING) << "Failed to open " << path; 1276df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1277df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey return; 1278df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1279df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey dfd = dirfd(d); 1280df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey while ((de = readdir(d))) { 1281df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (de->d_type == DT_DIR) { 1282df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey const char *name = de->d_name; 1283df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) != 0) { 1284df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey continue; 1285df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1286a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey int32_t user_uid = multiuser_get_app_id(s.st_uid); 1287df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (!strcmp(name, ".") || !strcmp(name, "..")) { 1288df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey continue; 1289a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey } else if (exclude_apps && (user_uid >= AID_APP_START && user_uid <= AID_APP_END)) { 1290df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey continue; 1291df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } else { 1292df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey collectManualStats(StringPrintf("%s/%s", path.c_str(), name), stats); 1293df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1294df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1295df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1296df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey closedir(d); 1297df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey} 1298df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 12997bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkeystatic void collectManualExternalStatsForUser(const std::string& path, struct stats* stats) { 13007bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey FTS *fts; 13017bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey FTSENT *p; 13027bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey char *argv[] = { (char*) path.c_str(), nullptr }; 1303b26786d647b624498c11405075e5223d1853f30aJeff Sharkey if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL))) { 13047bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey PLOG(ERROR) << "Failed to fts_open " << path; 13057bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey return; 13067bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey } 13077bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey while ((p = fts_read(fts)) != NULL) { 130876ddaeb2017e422de5a8d686ab62844d23d17624Jeff Sharkey p->fts_number = p->fts_parent->fts_number; 13097bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey switch (p->fts_info) { 13107bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey case FTS_D: 13117bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey if (p->fts_level == 4 13127bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey && !strcmp(p->fts_name, "cache") 13137bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey && !strcmp(p->fts_parent->fts_parent->fts_name, "data") 13147bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey && !strcmp(p->fts_parent->fts_parent->fts_parent->fts_name, "Android")) { 13157bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey p->fts_number = 1; 13167bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey } 13177bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey // Fall through to count the directory 13187bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey case FTS_DEFAULT: 13197bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey case FTS_F: 13207bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey case FTS_SL: 13217bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey case FTS_SLNONE: 13227bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey int64_t size = (p->fts_statp->st_blocks * 512); 13237bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey if (p->fts_number == 1) { 13247bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey stats->cacheSize += size; 13257bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey } 13267bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey stats->dataSize += size; 13277bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey break; 13287bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey } 13297bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey } 13307bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey fts_close(fts); 13317bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey} 13327bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey 13336c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkeybinder::Status InstalldNativeService::getAppSize(const std::unique_ptr<std::string>& uuid, 1334df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey const std::vector<std::string>& packageNames, int32_t userId, int32_t flags, 1335df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int32_t appId, const std::vector<int64_t>& ceDataInodes, 1336df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey const std::vector<std::string>& codePaths, std::vector<int64_t>* _aidl_return) { 13376c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 1338423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 1339df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey for (auto packageName : packageNames) { 1340df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 1341df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1342b26786d647b624498c11405075e5223d1853f30aJeff Sharkey // NOTE: Locking is relaxed on this method, since it's limited to 1343b26786d647b624498c11405075e5223d1853f30aJeff Sharkey // read-only measurements without mutation. 1344423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 1345df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // When modifying this logic, always verify using tests: 1346df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // runtest -x frameworks/base/services/tests/servicestests/src/com/android/server/pm/InstallerTest.java -m testGetAppSize 1347df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1348df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 1349df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey LOG(INFO) << "Measuring user " << userId << " app " << appId; 1350df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 13516c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey 13523dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // Here's a summary of the common storage locations across the platform, 13533dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // and how they're each tagged: 13543dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // 13553dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // /data/app/com.example UID system 13563dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // /data/app/com.example/oat UID system 1357df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // /data/user/0/com.example UID u0_a10 GID u0_a10 1358df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // /data/user/0/com.example/cache UID u0_a10 GID u0_a10_cache 1359df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // /data/media/0/foo.txt UID u0_media_rw 1360df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // /data/media/0/bar.jpg UID u0_media_rw GID u0_media_image 1361df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // /data/media/0/Android/data/com.example UID u0_media_rw GID u0_a10_ext 1362df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // /data/media/0/Android/data/com.example/cache UID u0_media_rw GID u0_a10_ext_cache 1363df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // /data/media/obb/com.example UID system 13643dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 13653dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey struct stats stats; 1366df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey struct stats extStats; 13673dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey memset(&stats, 0, sizeof(stats)); 1368df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey memset(&extStats, 0, sizeof(extStats)); 13693dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 1370df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 13713dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 137266b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey auto device = findQuotaDeviceForUuid(uuid); 137366b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey if (device.empty()) { 137466b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey flags &= ~FLAG_USE_QUOTA; 137566b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey } 137666b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey 1377466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_BEGIN("obb"); 1378df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey for (auto packageName : packageNames) { 1379df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto obbCodePath = create_data_media_obb_path(uuid_, packageName.c_str()); 1380df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(obbCodePath, &extStats.codeSize); 1381df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1382466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_END(); 13833dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 1384df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (flags & FLAG_USE_QUOTA && appId >= AID_APP_START) { 1385466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_BEGIN("code"); 1386df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey for (auto codePath : codePaths) { 1387df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(codePath, &stats.codeSize, -1, 1388a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey multiuser_get_shared_gid(0, appId)); 13893dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 1390466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_END(); 1391df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1392466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_BEGIN("quota"); 139366b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey collectQuotaStats(device, userId, appId, &stats, &extStats); 1394466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_END(); 13953dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } else { 1396466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_BEGIN("code"); 1397df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey for (auto codePath : codePaths) { 1398df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(codePath, &stats.codeSize); 1399df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1400466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_END(); 140194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 1402df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey for (size_t i = 0; i < packageNames.size(); i++) { 1403df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey const char* pkgname = packageNames[i].c_str(); 140494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 1405466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_BEGIN("data"); 1406df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto cePath = create_data_user_ce_package_path(uuid_, userId, pkgname, ceDataInodes[i]); 1407df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey collectManualStats(cePath, &stats); 1408df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto dePath = create_data_user_de_package_path(uuid_, userId, pkgname); 1409df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey collectManualStats(dePath, &stats); 1410466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_END(); 14113dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 141206a6184da6c2d80ea8a30f7cf827ecdfe191622dJeff Sharkey if (!uuid) { 141306a6184da6c2d80ea8a30f7cf827ecdfe191622dJeff Sharkey ATRACE_BEGIN("profiles"); 141406a6184da6c2d80ea8a30f7cf827ecdfe191622dJeff Sharkey calculate_tree_size( 141506a6184da6c2d80ea8a30f7cf827ecdfe191622dJeff Sharkey create_primary_current_profile_package_dir_path(userId, pkgname), 141606a6184da6c2d80ea8a30f7cf827ecdfe191622dJeff Sharkey &stats.dataSize); 141706a6184da6c2d80ea8a30f7cf827ecdfe191622dJeff Sharkey calculate_tree_size( 141806a6184da6c2d80ea8a30f7cf827ecdfe191622dJeff Sharkey create_primary_reference_profile_package_dir_path(pkgname), 141906a6184da6c2d80ea8a30f7cf827ecdfe191622dJeff Sharkey &stats.codeSize); 142006a6184da6c2d80ea8a30f7cf827ecdfe191622dJeff Sharkey ATRACE_END(); 142106a6184da6c2d80ea8a30f7cf827ecdfe191622dJeff Sharkey } 1422df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1423466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_BEGIN("external"); 142476ddaeb2017e422de5a8d686ab62844d23d17624Jeff Sharkey auto extPath = create_data_media_package_path(uuid_, userId, "data", pkgname); 1425df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey collectManualStats(extPath, &extStats); 142676ddaeb2017e422de5a8d686ab62844d23d17624Jeff Sharkey auto mediaPath = create_data_media_package_path(uuid_, userId, "media", pkgname); 1427df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(mediaPath, &extStats.dataSize); 1428466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_END(); 1429df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1430df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 143106a6184da6c2d80ea8a30f7cf827ecdfe191622dJeff Sharkey if (!uuid) { 143206a6184da6c2d80ea8a30f7cf827ecdfe191622dJeff Sharkey ATRACE_BEGIN("dalvik"); 1433a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey int32_t sharedGid = multiuser_get_shared_gid(0, appId); 143406a6184da6c2d80ea8a30f7cf827ecdfe191622dJeff Sharkey if (sharedGid != -1) { 143506a6184da6c2d80ea8a30f7cf827ecdfe191622dJeff Sharkey calculate_tree_size(create_data_dalvik_cache_path(), &stats.codeSize, 143606a6184da6c2d80ea8a30f7cf827ecdfe191622dJeff Sharkey sharedGid, -1); 143706a6184da6c2d80ea8a30f7cf827ecdfe191622dJeff Sharkey } 143806a6184da6c2d80ea8a30f7cf827ecdfe191622dJeff Sharkey ATRACE_END(); 1439df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1440df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1441df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1442df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey std::vector<int64_t> ret; 1443df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(stats.codeSize); 1444df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(stats.dataSize); 1445df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(stats.cacheSize); 1446df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(extStats.codeSize); 1447df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(extStats.dataSize); 1448df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(extStats.cacheSize); 1449df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 1450df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey LOG(DEBUG) << "Final result " << toString(ret); 1451df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 1452df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey *_aidl_return = ret; 1453df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey return ok(); 1454df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey} 1455df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1456df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkeybinder::Status InstalldNativeService::getUserSize(const std::unique_ptr<std::string>& uuid, 1457df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int32_t userId, int32_t flags, const std::vector<int32_t>& appIds, 1458df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey std::vector<int64_t>* _aidl_return) { 1459df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 1460df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 1461b26786d647b624498c11405075e5223d1853f30aJeff Sharkey // NOTE: Locking is relaxed on this method, since it's limited to 1462b26786d647b624498c11405075e5223d1853f30aJeff Sharkey // read-only measurements without mutation. 1463df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1464df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // When modifying this logic, always verify using tests: 1465df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // runtest -x frameworks/base/services/tests/servicestests/src/com/android/server/pm/InstallerTest.java -m testGetUserSize 1466df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1467df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 1468df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey LOG(INFO) << "Measuring user " << userId; 1469df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 1470df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1471df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey struct stats stats; 1472df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey struct stats extStats; 1473df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey memset(&stats, 0, sizeof(stats)); 1474df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey memset(&extStats, 0, sizeof(extStats)); 1475df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1476df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 1477df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 147866b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey auto device = findQuotaDeviceForUuid(uuid); 147966b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey if (device.empty()) { 148066b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey flags &= ~FLAG_USE_QUOTA; 148166b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey } 148266b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey 1483df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (flags & FLAG_USE_QUOTA) { 14847bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey struct dqblk dq; 14857bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey 14867bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey ATRACE_BEGIN("obb"); 14877bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), AID_MEDIA_OBB, 14887bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey reinterpret_cast<char*>(&dq)) != 0) { 14897bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey if (errno != ESRCH) { 14907bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey PLOG(ERROR) << "Failed to quotactl " << device << " for GID " << AID_MEDIA_OBB; 14917bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey } 14927bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey } else { 14937bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey#if MEASURE_DEBUG 14947bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey LOG(DEBUG) << "quotactl() for GID " << AID_MEDIA_OBB << " " << dq.dqb_curspace; 14957bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey#endif 14967bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey extStats.codeSize += dq.dqb_curspace; 14977bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey } 14987bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey ATRACE_END(); 14997bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey 1500466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_BEGIN("code"); 1501df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(create_data_app_path(uuid_), &stats.codeSize, -1, -1, true); 1502466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_END(); 1503df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1504466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_BEGIN("data"); 1505df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto cePath = create_data_user_ce_path(uuid_, userId); 1506df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey collectManualStatsForUser(cePath, &stats, true); 1507df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto dePath = create_data_user_de_path(uuid_, userId); 1508df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey collectManualStatsForUser(dePath, &stats, true); 1509466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_END(); 1510df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 151106a6184da6c2d80ea8a30f7cf827ecdfe191622dJeff Sharkey if (!uuid) { 151206a6184da6c2d80ea8a30f7cf827ecdfe191622dJeff Sharkey ATRACE_BEGIN("profile"); 151306a6184da6c2d80ea8a30f7cf827ecdfe191622dJeff Sharkey auto userProfilePath = create_primary_cur_profile_dir_path(userId); 151406a6184da6c2d80ea8a30f7cf827ecdfe191622dJeff Sharkey calculate_tree_size(userProfilePath, &stats.dataSize, -1, -1, true); 151506a6184da6c2d80ea8a30f7cf827ecdfe191622dJeff Sharkey auto refProfilePath = create_primary_ref_profile_dir_path(); 151606a6184da6c2d80ea8a30f7cf827ecdfe191622dJeff Sharkey calculate_tree_size(refProfilePath, &stats.codeSize, -1, -1, true); 151706a6184da6c2d80ea8a30f7cf827ecdfe191622dJeff Sharkey ATRACE_END(); 151806a6184da6c2d80ea8a30f7cf827ecdfe191622dJeff Sharkey } 15193dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 1520466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_BEGIN("external"); 15217bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey uid_t uid = multiuser_get_uid(userId, AID_MEDIA_RW); 15227bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey if (quotactl(QCMD(Q_GETQUOTA, USRQUOTA), device.c_str(), uid, 15237bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey reinterpret_cast<char*>(&dq)) != 0) { 15247bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey if (errno != ESRCH) { 15257bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey PLOG(ERROR) << "Failed to quotactl " << device << " for UID " << uid; 15267bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey } 15277bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey } else { 15287bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey#if MEASURE_DEBUG 15297bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey LOG(DEBUG) << "quotactl() for UID " << uid << " " << dq.dqb_curspace; 1530df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 15317bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey extStats.dataSize += dq.dqb_curspace; 15327bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey } 15337bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey ATRACE_END(); 15343dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 153506a6184da6c2d80ea8a30f7cf827ecdfe191622dJeff Sharkey if (!uuid) { 153606a6184da6c2d80ea8a30f7cf827ecdfe191622dJeff Sharkey ATRACE_BEGIN("dalvik"); 153706a6184da6c2d80ea8a30f7cf827ecdfe191622dJeff Sharkey calculate_tree_size(create_data_dalvik_cache_path(), &stats.codeSize, 153806a6184da6c2d80ea8a30f7cf827ecdfe191622dJeff Sharkey -1, -1, true); 153906a6184da6c2d80ea8a30f7cf827ecdfe191622dJeff Sharkey calculate_tree_size(create_primary_cur_profile_dir_path(userId), &stats.dataSize, 154006a6184da6c2d80ea8a30f7cf827ecdfe191622dJeff Sharkey -1, -1, true); 154106a6184da6c2d80ea8a30f7cf827ecdfe191622dJeff Sharkey ATRACE_END(); 154206a6184da6c2d80ea8a30f7cf827ecdfe191622dJeff Sharkey } 1543df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1544466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_BEGIN("quota"); 1545a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey int64_t dataSize = extStats.dataSize; 1546df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey for (auto appId : appIds) { 1547df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (appId >= AID_APP_START) { 1548df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey collectQuotaStats(device, userId, appId, &stats, &extStats); 1549ec1be62c18b216901d738be0ccc8a05aab8edef0Jeff Sharkey 1550df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 1551df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // Sleep to make sure we don't lose logs 1552df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey usleep(1); 1553df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 1554df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1555df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1556a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey extStats.dataSize = dataSize; 1557466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_END(); 1558df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } else { 15597bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey ATRACE_BEGIN("obb"); 15607bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey auto obbPath = create_data_path(uuid_) + "/media/obb"; 15617bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey calculate_tree_size(obbPath, &extStats.codeSize); 15627bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey ATRACE_END(); 15637bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey 1564466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_BEGIN("code"); 1565df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(create_data_app_path(uuid_), &stats.codeSize); 1566466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_END(); 1567df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1568466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_BEGIN("data"); 1569df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto cePath = create_data_user_ce_path(uuid_, userId); 1570df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey collectManualStatsForUser(cePath, &stats); 1571df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto dePath = create_data_user_de_path(uuid_, userId); 1572df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey collectManualStatsForUser(dePath, &stats); 1573466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_END(); 1574df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 157506a6184da6c2d80ea8a30f7cf827ecdfe191622dJeff Sharkey if (!uuid) { 157606a6184da6c2d80ea8a30f7cf827ecdfe191622dJeff Sharkey ATRACE_BEGIN("profile"); 157706a6184da6c2d80ea8a30f7cf827ecdfe191622dJeff Sharkey auto userProfilePath = create_primary_cur_profile_dir_path(userId); 157806a6184da6c2d80ea8a30f7cf827ecdfe191622dJeff Sharkey calculate_tree_size(userProfilePath, &stats.dataSize); 157906a6184da6c2d80ea8a30f7cf827ecdfe191622dJeff Sharkey auto refProfilePath = create_primary_ref_profile_dir_path(); 158006a6184da6c2d80ea8a30f7cf827ecdfe191622dJeff Sharkey calculate_tree_size(refProfilePath, &stats.codeSize); 158106a6184da6c2d80ea8a30f7cf827ecdfe191622dJeff Sharkey ATRACE_END(); 158206a6184da6c2d80ea8a30f7cf827ecdfe191622dJeff Sharkey } 1583df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1584466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_BEGIN("external"); 15857bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey auto dataMediaPath = create_data_media_path(uuid_, userId); 15867bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey collectManualExternalStatsForUser(dataMediaPath, &extStats); 15877bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey#if MEASURE_DEBUG 15887bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey LOG(DEBUG) << "Measured external data " << extStats.dataSize << " cache " 15897bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey << extStats.cacheSize; 15903dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey#endif 15917bf5b9532e192e846be3a31f28171cdba0f3c830Jeff Sharkey ATRACE_END(); 1592df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 159306a6184da6c2d80ea8a30f7cf827ecdfe191622dJeff Sharkey if (!uuid) { 159406a6184da6c2d80ea8a30f7cf827ecdfe191622dJeff Sharkey ATRACE_BEGIN("dalvik"); 159506a6184da6c2d80ea8a30f7cf827ecdfe191622dJeff Sharkey calculate_tree_size(create_data_dalvik_cache_path(), &stats.codeSize); 159606a6184da6c2d80ea8a30f7cf827ecdfe191622dJeff Sharkey calculate_tree_size(create_primary_cur_profile_dir_path(userId), &stats.dataSize); 159706a6184da6c2d80ea8a30f7cf827ecdfe191622dJeff Sharkey ATRACE_END(); 159806a6184da6c2d80ea8a30f7cf827ecdfe191622dJeff Sharkey } 15992f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey } 160094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 16013dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey std::vector<int64_t> ret; 16023dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey ret.push_back(stats.codeSize); 16033dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey ret.push_back(stats.dataSize); 16043dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey ret.push_back(stats.cacheSize); 1605df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(extStats.codeSize); 1606df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(extStats.dataSize); 1607df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(extStats.cacheSize); 1608df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 1609df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey LOG(DEBUG) << "Final result " << toString(ret); 1610df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 1611df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey *_aidl_return = ret; 1612df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey return ok(); 1613df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey} 1614df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1615df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkeybinder::Status InstalldNativeService::getExternalSize(const std::unique_ptr<std::string>& uuid, 1616a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey int32_t userId, int32_t flags, const std::vector<int32_t>& appIds, 1617a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey std::vector<int64_t>* _aidl_return) { 1618df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 1619df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 1620b26786d647b624498c11405075e5223d1853f30aJeff Sharkey // NOTE: Locking is relaxed on this method, since it's limited to 1621b26786d647b624498c11405075e5223d1853f30aJeff Sharkey // read-only measurements without mutation. 1622df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1623df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // When modifying this logic, always verify using tests: 1624df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // runtest -x frameworks/base/services/tests/servicestests/src/com/android/server/pm/InstallerTest.java -m testGetExternalSize 1625df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1626df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 1627df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey LOG(INFO) << "Measuring external " << userId; 1628df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 1629df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1630df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 1631df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1632df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int64_t totalSize = 0; 1633df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int64_t audioSize = 0; 1634df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int64_t videoSize = 0; 1635df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int64_t imageSize = 0; 1636a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey int64_t appSize = 0; 1637df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 163866b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey auto device = findQuotaDeviceForUuid(uuid); 163966b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey if (device.empty()) { 164066b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey flags &= ~FLAG_USE_QUOTA; 164166b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey } 164266b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey 1643df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (flags & FLAG_USE_QUOTA) { 1644df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey struct dqblk dq; 1645df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1646a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey ATRACE_BEGIN("quota"); 1647df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey uid_t uid = multiuser_get_uid(userId, AID_MEDIA_RW); 1648df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (quotactl(QCMD(Q_GETQUOTA, USRQUOTA), device.c_str(), uid, 1649df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey reinterpret_cast<char*>(&dq)) != 0) { 1650df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (errno != ESRCH) { 1651df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey PLOG(ERROR) << "Failed to quotactl " << device << " for UID " << uid; 1652df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1653df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } else { 1654df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 1655a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey LOG(DEBUG) << "quotactl() for UID " << uid << " " << dq.dqb_curspace; 1656df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 1657df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey totalSize = dq.dqb_curspace; 1658df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1659df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1660df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey gid_t audioGid = multiuser_get_uid(userId, AID_MEDIA_AUDIO); 1661df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), audioGid, 1662df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey reinterpret_cast<char*>(&dq)) == 0) { 1663df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 1664a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey LOG(DEBUG) << "quotactl() for GID " << audioGid << " " << dq.dqb_curspace; 1665df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 1666df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey audioSize = dq.dqb_curspace; 1667df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1668df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey gid_t videoGid = multiuser_get_uid(userId, AID_MEDIA_VIDEO); 1669df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), videoGid, 1670df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey reinterpret_cast<char*>(&dq)) == 0) { 1671df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 1672a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey LOG(DEBUG) << "quotactl() for GID " << videoGid << " " << dq.dqb_curspace; 1673df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 1674df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey videoSize = dq.dqb_curspace; 1675df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1676df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey gid_t imageGid = multiuser_get_uid(userId, AID_MEDIA_IMAGE); 1677df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), imageGid, 1678df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey reinterpret_cast<char*>(&dq)) == 0) { 1679df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 1680a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey LOG(DEBUG) << "quotactl() for GID " << imageGid << " " << dq.dqb_curspace; 1681df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 1682df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey imageSize = dq.dqb_curspace; 1683df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1684a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey ATRACE_END(); 1685a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey 1686a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey ATRACE_BEGIN("apps"); 1687a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey struct stats extStats; 1688a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey memset(&extStats, 0, sizeof(extStats)); 1689a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey for (auto appId : appIds) { 1690a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey if (appId >= AID_APP_START) { 1691a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey collectQuotaStats(device, userId, appId, nullptr, &extStats); 1692a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey } 1693a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey } 1694a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey appSize = extStats.dataSize + extStats.cacheSize; 1695a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey ATRACE_END(); 1696df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } else { 1697a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey ATRACE_BEGIN("manual"); 1698df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey FTS *fts; 1699df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey FTSENT *p; 1700df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto path = create_data_media_path(uuid_, userId); 1701df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey char *argv[] = { (char*) path.c_str(), nullptr }; 1702b26786d647b624498c11405075e5223d1853f30aJeff Sharkey if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL))) { 1703df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey return error("Failed to fts_open " + path); 1704df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1705df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey while ((p = fts_read(fts)) != NULL) { 1706df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey char* ext; 1707df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int64_t size = (p->fts_statp->st_blocks * 512); 1708df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey switch (p->fts_info) { 1709df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey case FTS_F: 1710df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // Only categorize files not belonging to apps 1711444ad1ee82a549c96bf9e5087b9041d6264c93ecJeff Sharkey if (p->fts_parent->fts_number == 0) { 1712df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ext = strrchr(p->fts_name, '.'); 1713df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (ext != nullptr) { 1714df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey switch (MatchExtension(++ext)) { 1715df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey case AID_MEDIA_AUDIO: audioSize += size; break; 1716df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey case AID_MEDIA_VIDEO: videoSize += size; break; 1717df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey case AID_MEDIA_IMAGE: imageSize += size; break; 1718df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1719df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1720df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1721df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // Fall through to always count against total 1722df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey case FTS_D: 1723444ad1ee82a549c96bf9e5087b9041d6264c93ecJeff Sharkey // Ignore data belonging to specific apps 1724444ad1ee82a549c96bf9e5087b9041d6264c93ecJeff Sharkey p->fts_number = p->fts_parent->fts_number; 1725444ad1ee82a549c96bf9e5087b9041d6264c93ecJeff Sharkey if (p->fts_level == 1 && !strcmp(p->fts_name, "Android")) { 1726444ad1ee82a549c96bf9e5087b9041d6264c93ecJeff Sharkey p->fts_number = 1; 1727444ad1ee82a549c96bf9e5087b9041d6264c93ecJeff Sharkey } 1728df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey case FTS_DEFAULT: 1729df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey case FTS_SL: 1730df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey case FTS_SLNONE: 1731a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey if (p->fts_parent->fts_number == 1) { 1732a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey appSize += size; 1733a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey } 1734df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey totalSize += size; 1735df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey break; 1736df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1737df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1738df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey fts_close(fts); 1739a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey ATRACE_END(); 1740df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1741df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1742df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey std::vector<int64_t> ret; 1743df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(totalSize); 1744df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(audioSize); 1745df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(videoSize); 1746df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(imageSize); 1747a084fcdcd052d71e8205a2eea0955911b99160b9Jeff Sharkey ret.push_back(appSize); 1748df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 1749df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey LOG(DEBUG) << "Final result " << toString(ret); 1750df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 17513dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey *_aidl_return = ret; 1752423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 17532f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey} 1754d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey 175588ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkeybinder::Status InstalldNativeService::setAppQuota(const std::unique_ptr<std::string>& uuid, 175688ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey int32_t userId, int32_t appId, int64_t cacheQuota) { 175788ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey ENFORCE_UID(AID_SYSTEM); 175888ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey CHECK_ARGUMENT_UUID(uuid); 1759d712f0ccc120357e1267a04ac6de9dd732b27e76Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mQuotasLock); 176088ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey 176188ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey int32_t uid = multiuser_get_uid(userId, appId); 176288ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey mCacheQuotas[uid] = cacheQuota; 176388ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey 176488ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey return ok(); 176588ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey} 176688ddd94834dae9c5862a07a1e4432b171b2f5d9fJeff Sharkey 17676727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr// Dumps the contents of a profile file, using pkgname's dex files for pretty 17686727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr// printing the result. 1769475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::dumpProfiles(int32_t uid, const std::string& packageName, 1770475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const std::string& codePaths, bool* _aidl_return) { 1771475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 1772423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 17737a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 1774475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey 1775475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* pkgname = packageName.c_str(); 177690aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey const char* code_paths = codePaths.c_str(); 17776727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr 177890aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey *_aidl_return = dump_profiles(uid, pkgname, code_paths); 1779423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 17806727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr} 17816727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr 17824d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe// TODO: Consider returning error codes. 1783475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::mergeProfiles(int32_t uid, const std::string& packageName, 1784423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey bool* _aidl_return) { 1785475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 1786423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 17877a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 1788423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 1789114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle *_aidl_return = analyze_primary_profiles(uid, packageName); 1790423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 17914d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe} 17924d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe 17936c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkeybinder::Status InstalldNativeService::dexopt(const std::string& apkPath, int32_t uid, 17946c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey const std::unique_ptr<std::string>& packageName, const std::string& instructionSet, 17956c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey int32_t dexoptNeeded, const std::unique_ptr<std::string>& outputPath, int32_t dexFlags, 17966c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey const std::string& compilerFilter, const std::unique_ptr<std::string>& uuid, 1797cb556e340b50d16e8fac41b0c69d8d35bd7915f9Calin Juravle const std::unique_ptr<std::string>& sharedLibraries, 1798cb556e340b50d16e8fac41b0c69d8d35bd7915f9Calin Juravle const std::unique_ptr<std::string>& seInfo) { 17996c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 1800423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 1801423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (packageName && *packageName != "*") { 1802423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(*packageName); 1803423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 18047a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 18056c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey 18066c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey const char* apk_path = apkPath.c_str(); 18076c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey const char* pkgname = packageName ? packageName->c_str() : "*"; 18086c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey const char* instruction_set = instructionSet.c_str(); 18096c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey const char* oat_dir = outputPath ? outputPath->c_str() : nullptr; 18106c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey const char* compiler_filter = compilerFilter.c_str(); 18116c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey const char* volume_uuid = uuid ? uuid->c_str() : nullptr; 18126c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey const char* shared_libraries = sharedLibraries ? sharedLibraries->c_str() : nullptr; 1813cb556e340b50d16e8fac41b0c69d8d35bd7915f9Calin Juravle const char* se_info = seInfo ? seInfo->c_str() : nullptr; 18146c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey int res = android::installd::dexopt(apk_path, uid, pkgname, instruction_set, dexoptNeeded, 1815cb556e340b50d16e8fac41b0c69d8d35bd7915f9Calin Juravle oat_dir, dexFlags, compiler_filter, volume_uuid, shared_libraries, se_info); 1816423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return res ? error(res, "Failed to dexopt") : ok(); 18176c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey} 18186c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey 1819475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::markBootComplete(const std::string& instructionSet) { 1820475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 18217a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 18227a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey 1823475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* instruction_set = instructionSet.c_str(); 1824475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey 1825475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey char boot_marker_path[PKG_PATH_MAX]; 1826475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey sprintf(boot_marker_path, 182702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe "%s/%s/%s/.booting", 182802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe android_data_dir.path, 182902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe DALVIK_CACHE, 183002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe instruction_set); 1831091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath 1832475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ALOGV("mark_boot_complete : %s", boot_marker_path); 1833475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey if (unlink(boot_marker_path) != 0) { 1834423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error(StringPrintf("Failed to unlink %s", boot_marker_path)); 1835475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey } 1836423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 1837091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath} 1838091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath 183994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodvoid mkinnerdirs(char* path, int basepos, mode_t mode, int uid, int gid, 184094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood struct stat* statbuf) 184194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 184294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood while (path[basepos] != 0) { 184394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (path[basepos] == '/') { 184494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood path[basepos] = 0; 184594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (lstat(path, statbuf) < 0) { 184694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGV("Making directory: %s\n", path); 184794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (mkdir(path, mode) == 0) { 184894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood chown(path, uid, gid); 184994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else { 185094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGW("Unable to make directory %s: %s\n", path, strerror(errno)); 185194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 185294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 185394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood path[basepos] = '/'; 185494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood basepos++; 185594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 185694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood basepos++; 185794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 185894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 185994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 1860475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::linkNativeLibraryDirectory( 1861475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const std::unique_ptr<std::string>& uuid, const std::string& packageName, 1862475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const std::string& nativeLibPath32, int32_t userId) { 1863475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff 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 1868475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 1869475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* pkgname = packageName.c_str(); 1870475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* asecLibDir = nativeLibPath32.c_str(); 187194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood struct stat s, libStat; 1872423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey binder::Status res = ok(); 187394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 1874423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey auto _pkgdir = create_data_user_ce_package_path(uuid_, userId, pkgname); 1875423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey auto _libsymlink = _pkgdir + PKG_LIB_POSTFIX; 1876c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey 1877c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey const char* pkgdir = _pkgdir.c_str(); 1878c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey const char* libsymlink = _libsymlink.c_str(); 187994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 1880475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey if (stat(pkgdir, &s) < 0) { 1881423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to stat " + _pkgdir); 1882475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey } 188394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 188494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (chown(pkgdir, AID_INSTALL, AID_INSTALL) < 0) { 1885423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to chown " + _pkgdir); 188694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 188794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 188894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (chmod(pkgdir, 0700) < 0) { 1889423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to chmod " + _pkgdir); 189094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto out; 189194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 189294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 189394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (lstat(libsymlink, &libStat) < 0) { 189494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (errno != ENOENT) { 1895423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to stat " + _libsymlink); 189694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto out; 189794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 189894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else { 189994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (S_ISDIR(libStat.st_mode)) { 19003aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath if (delete_dir_contents(libsymlink, 1, NULL) < 0) { 1901423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to delete " + _libsymlink); 190294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto out; 190394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 190494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else if (S_ISLNK(libStat.st_mode)) { 190594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (unlink(libsymlink) < 0) { 1906423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to unlink " + _libsymlink); 190794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto out; 190894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 190994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 191094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 191194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 191294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (symlink(asecLibDir, libsymlink) < 0) { 1913423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to symlink " + _libsymlink + " to " + nativeLibPath32); 191494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto out; 191594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 191694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 191794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodout: 191894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (chmod(pkgdir, s.st_mode) < 0) { 1919423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey auto msg = "Failed to cleanup chmod " + _pkgdir; 1920423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (res.isOk()) { 1921423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error(msg); 1922423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } else { 1923423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey PLOG(ERROR) << msg; 1924423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 192594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 192694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 192794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (chown(pkgdir, s.st_uid, s.st_gid) < 0) { 1928423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey auto msg = "Failed to cleanup chown " + _pkgdir; 1929423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (res.isOk()) { 1930423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error(msg); 1931423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } else { 1932423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey PLOG(ERROR) << msg; 1933423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 193494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 193594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 1936423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return res; 193794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 193863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 193963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstadstatic void run_idmap(const char *target_apk, const char *overlay_apk, int idmap_fd) 194063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad{ 194163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad static const char *IDMAP_BIN = "/system/bin/idmap"; 194263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad static const size_t MAX_INT_LEN = 32; 194363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad char idmap_str[MAX_INT_LEN]; 194463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 194563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad snprintf(idmap_str, sizeof(idmap_str), "%d", idmap_fd); 194663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 194763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad execl(IDMAP_BIN, IDMAP_BIN, "--fd", target_apk, overlay_apk, idmap_str, (char*)NULL); 194863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("execl(%s) failed: %s\n", IDMAP_BIN, strerror(errno)); 194963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad} 195063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 195163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad// Transform string /a/b/c.apk to (prefix)/a@b@c.apk@(suffix) 195263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad// eg /a/b/c.apk to /data/resource-cache/a@b@c.apk@idmap 195363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstadstatic int flatten_path(const char *prefix, const char *suffix, 195463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad const char *overlay_path, char *idmap_path, size_t N) 195563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad{ 195663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (overlay_path == NULL || idmap_path == NULL) { 195763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad return -1; 195863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 195963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad const size_t len_overlay_path = strlen(overlay_path); 196063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad // will access overlay_path + 1 further below; requires absolute path 196163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (len_overlay_path < 2 || *overlay_path != '/') { 196263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad return -1; 196363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 196463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad const size_t len_idmap_root = strlen(prefix); 196563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad const size_t len_suffix = strlen(suffix); 196663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (SIZE_MAX - len_idmap_root < len_overlay_path || 196763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad SIZE_MAX - (len_idmap_root + len_overlay_path) < len_suffix) { 196863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad // additions below would cause overflow 196963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad return -1; 197063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 197163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (N < len_idmap_root + len_overlay_path + len_suffix) { 197263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad return -1; 197363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 197463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad memset(idmap_path, 0, N); 197563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad snprintf(idmap_path, N, "%s%s%s", prefix, overlay_path + 1, suffix); 197663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad char *ch = idmap_path + len_idmap_root; 197763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad while (*ch != '\0') { 197863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (*ch == '/') { 197963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad *ch = '@'; 198063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 198163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ++ch; 198263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 198363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad return 0; 198463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad} 198563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 1986475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::idmap(const std::string& targetApkPath, 1987475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const std::string& overlayApkPath, int32_t uid) { 1988475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 19897a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 19907a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey 1991475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* target_apk = targetApkPath.c_str(); 1992475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* overlay_apk = overlayApkPath.c_str(); 199363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGV("idmap target_apk=%s overlay_apk=%s uid=%d\n", target_apk, overlay_apk, uid); 199463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 199563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad int idmap_fd = -1; 199663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad char idmap_path[PATH_MAX]; 19971d091465334784adeded4ef182cc6f732eb6f99eJaekyun Seok struct stat target_apk_stat, overlay_apk_stat, idmap_stat; 19981d091465334784adeded4ef182cc6f732eb6f99eJaekyun Seok bool outdated = false; 199963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 200063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (flatten_path(IDMAP_PREFIX, IDMAP_SUFFIX, overlay_apk, 200163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad idmap_path, sizeof(idmap_path)) == -1) { 200263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("idmap cannot generate idmap path for overlay %s\n", overlay_apk); 200363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad goto fail; 200463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 200563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 20061d091465334784adeded4ef182cc6f732eb6f99eJaekyun Seok if (stat(idmap_path, &idmap_stat) < 0 || 20071d091465334784adeded4ef182cc6f732eb6f99eJaekyun Seok stat(target_apk, &target_apk_stat) < 0 || 20081d091465334784adeded4ef182cc6f732eb6f99eJaekyun Seok stat(overlay_apk, &overlay_apk_stat) < 0) { 20091d091465334784adeded4ef182cc6f732eb6f99eJaekyun Seok outdated = true; 20101d091465334784adeded4ef182cc6f732eb6f99eJaekyun Seok } else if (idmap_stat.st_mtime < target_apk_stat.st_mtime || 20111d091465334784adeded4ef182cc6f732eb6f99eJaekyun Seok idmap_stat.st_mtime < overlay_apk_stat.st_mtime) { 20121d091465334784adeded4ef182cc6f732eb6f99eJaekyun Seok outdated = true; 20131d091465334784adeded4ef182cc6f732eb6f99eJaekyun Seok } 20141d091465334784adeded4ef182cc6f732eb6f99eJaekyun Seok 20151d091465334784adeded4ef182cc6f732eb6f99eJaekyun Seok if (outdated) { 20161d091465334784adeded4ef182cc6f732eb6f99eJaekyun Seok unlink(idmap_path); 20171d091465334784adeded4ef182cc6f732eb6f99eJaekyun Seok idmap_fd = open(idmap_path, O_RDWR | O_CREAT | O_EXCL, 0644); 20181d091465334784adeded4ef182cc6f732eb6f99eJaekyun Seok } else { 20191d091465334784adeded4ef182cc6f732eb6f99eJaekyun Seok idmap_fd = open(idmap_path, O_RDWR); 20201d091465334784adeded4ef182cc6f732eb6f99eJaekyun Seok } 20211d091465334784adeded4ef182cc6f732eb6f99eJaekyun Seok 202263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (idmap_fd < 0) { 202363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("idmap cannot open '%s' for output: %s\n", idmap_path, strerror(errno)); 202463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad goto fail; 202563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 202663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (fchown(idmap_fd, AID_SYSTEM, uid) < 0) { 202763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("idmap cannot chown '%s'\n", idmap_path); 202863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad goto fail; 202963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 203063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (fchmod(idmap_fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0) { 203163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("idmap cannot chmod '%s'\n", idmap_path); 203263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad goto fail; 203363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 203463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 20351d091465334784adeded4ef182cc6f732eb6f99eJaekyun Seok if (!outdated) { 20361d091465334784adeded4ef182cc6f732eb6f99eJaekyun Seok close(idmap_fd); 20371d091465334784adeded4ef182cc6f732eb6f99eJaekyun Seok return ok(); 20381d091465334784adeded4ef182cc6f732eb6f99eJaekyun Seok } 20391d091465334784adeded4ef182cc6f732eb6f99eJaekyun Seok 204063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad pid_t pid; 204163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad pid = fork(); 204263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (pid == 0) { 204363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad /* child -- drop privileges before continuing */ 204463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (setgid(uid) != 0) { 204563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("setgid(%d) failed during idmap\n", uid); 204663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad exit(1); 204763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 204863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (setuid(uid) != 0) { 204963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("setuid(%d) failed during idmap\n", uid); 205063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad exit(1); 205163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 205263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (flock(idmap_fd, LOCK_EX | LOCK_NB) != 0) { 205363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("flock(%s) failed during idmap: %s\n", idmap_path, strerror(errno)); 205463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad exit(1); 205563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 205663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 205763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad run_idmap(target_apk, overlay_apk, idmap_fd); 205863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad exit(1); /* only if exec call to idmap failed */ 205963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } else { 206063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad int status = wait_child(pid); 206163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (status != 0) { 206263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("idmap failed, status=0x%04x\n", status); 206363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad goto fail; 206463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 206563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 206663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 206763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad close(idmap_fd); 2068423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 206963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstadfail: 207063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (idmap_fd >= 0) { 207163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad close(idmap_fd); 207263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad unlink(idmap_path); 207363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 2074423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error(); 207563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad} 2076e9887e46cea4a095e4219927eadbe4c57bb1a5eeRobert Craig 20775c6944c35211accd783d2584fac6421dd0af5bddMÃ¥rten Kongstadbinder::Status InstalldNativeService::removeIdmap(const std::string& overlayApkPath) { 20785c6944c35211accd783d2584fac6421dd0af5bddMÃ¥rten Kongstad const char* overlay_apk = overlayApkPath.c_str(); 20795c6944c35211accd783d2584fac6421dd0af5bddMÃ¥rten Kongstad char idmap_path[PATH_MAX]; 20805c6944c35211accd783d2584fac6421dd0af5bddMÃ¥rten Kongstad 20815c6944c35211accd783d2584fac6421dd0af5bddMÃ¥rten Kongstad if (flatten_path(IDMAP_PREFIX, IDMAP_SUFFIX, overlay_apk, 20825c6944c35211accd783d2584fac6421dd0af5bddMÃ¥rten Kongstad idmap_path, sizeof(idmap_path)) == -1) { 20835c6944c35211accd783d2584fac6421dd0af5bddMÃ¥rten Kongstad ALOGE("idmap cannot generate idmap path for overlay %s\n", overlay_apk); 20845c6944c35211accd783d2584fac6421dd0af5bddMÃ¥rten Kongstad return error(); 20855c6944c35211accd783d2584fac6421dd0af5bddMÃ¥rten Kongstad } 20865c6944c35211accd783d2584fac6421dd0af5bddMÃ¥rten Kongstad if (unlink(idmap_path) < 0) { 20875c6944c35211accd783d2584fac6421dd0af5bddMÃ¥rten Kongstad ALOGE("couldn't unlink idmap file %s\n", idmap_path); 20885c6944c35211accd783d2584fac6421dd0af5bddMÃ¥rten Kongstad return error(); 20895c6944c35211accd783d2584fac6421dd0af5bddMÃ¥rten Kongstad } 20905c6944c35211accd783d2584fac6421dd0af5bddMÃ¥rten Kongstad return ok(); 20915c6944c35211accd783d2584fac6421dd0af5bddMÃ¥rten Kongstad} 20925c6944c35211accd783d2584fac6421dd0af5bddMÃ¥rten Kongstad 2093c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkeybinder::Status InstalldNativeService::restoreconAppData(const std::unique_ptr<std::string>& uuid, 2094c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const std::string& packageName, int32_t userId, int32_t flags, int32_t appId, 2095c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const std::string& seInfo) { 2096c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 2097423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 2098423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 20997a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 2100423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 2101423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey binder::Status res = ok(); 2102e9887e46cea4a095e4219927eadbe4c57bb1a5eeRobert Craig 2103da30dc7336f03ca629fe173db1425fdce989119cRobert Craig // SELINUX_ANDROID_RESTORECON_DATADATA flag is set by libselinux. Not needed here. 2104c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey unsigned int seflags = SELINUX_ANDROID_RESTORECON_RECURSE; 2105c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 2106c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* pkgName = packageName.c_str(); 2107c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* seinfo = seInfo.c_str(); 2108e9887e46cea4a095e4219927eadbe4c57bb1a5eeRobert Craig 2109c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey uid_t uid = multiuser_get_uid(userId, appId); 2110aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (flags & FLAG_STORAGE_CE) { 2111c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey auto path = create_data_user_ce_package_path(uuid_, userId, pkgName); 2112c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey if (selinux_android_restorecon_pkgdir(path.c_str(), seinfo, uid, seflags) < 0) { 2113423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("restorecon failed for " + path); 2114da30dc7336f03ca629fe173db1425fdce989119cRobert Craig } 2115c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey } 2116aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (flags & FLAG_STORAGE_DE) { 2117c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey auto path = create_data_user_de_package_path(uuid_, userId, pkgName); 2118c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey if (selinux_android_restorecon_pkgdir(path.c_str(), seinfo, uid, seflags) < 0) { 2119423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("restorecon failed for " + path); 2120da30dc7336f03ca629fe173db1425fdce989119cRobert Craig } 2121da30dc7336f03ca629fe173db1425fdce989119cRobert Craig } 2122423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return res; 2123e9887e46cea4a095e4219927eadbe4c57bb1a5eeRobert Craig} 21243aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath 2125475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::createOatDir(const std::string& oatDir, 2126475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const std::string& instructionSet) { 2127475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 21287a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 21297a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey 2130475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* oat_dir = oatDir.c_str(); 2131475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* instruction_set = instructionSet.c_str(); 213288ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov char oat_instr_dir[PKG_PATH_MAX]; 213388ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov 213488ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov if (validate_apk_path(oat_dir)) { 2135423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Invalid path " + oatDir); 213688ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov } 21378eed7e6a9bb527af7380ec13b390c6e2955eba6aFyodor Kupolov if (fs_prepare_dir(oat_dir, S_IRWXU | S_IRWXG | S_IXOTH, AID_SYSTEM, AID_INSTALL)) { 2138423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to prepare " + oatDir); 213988ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov } 214088ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov if (selinux_android_restorecon(oat_dir, 0)) { 2141423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to restorecon " + oatDir); 214288ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov } 214388ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov snprintf(oat_instr_dir, PKG_PATH_MAX, "%s/%s", oat_dir, instruction_set); 21448eed7e6a9bb527af7380ec13b390c6e2955eba6aFyodor Kupolov if (fs_prepare_dir(oat_instr_dir, S_IRWXU | S_IRWXG | S_IXOTH, AID_SYSTEM, AID_INSTALL)) { 2145423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error(StringPrintf("Failed to prepare %s", oat_instr_dir)); 214688ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov } 2147423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 214888ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov} 214988ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov 2150475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::rmPackageDir(const std::string& packageDir) { 2151475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 21527a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 21537a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey 2154423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (validate_apk_path(packageDir.c_str())) { 2155423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Invalid path " + packageDir); 2156423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 2157423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (delete_dir_contents_and_dir(packageDir) != 0) { 2158423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to delete " + packageDir); 215988ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov } 2160423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 216188ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov} 216288ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov 2163475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::linkFile(const std::string& relativePath, 2164475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const std::string& fromBase, const std::string& toBase) { 2165475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 21667a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 21677a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey 2168475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* relative_path = relativePath.c_str(); 2169475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* from_base = fromBase.c_str(); 2170475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* to_base = toBase.c_str(); 2171d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath char from_path[PKG_PATH_MAX]; 2172d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath char to_path[PKG_PATH_MAX]; 2173d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath snprintf(from_path, PKG_PATH_MAX, "%s/%s", from_base, relative_path); 2174d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath snprintf(to_path, PKG_PATH_MAX, "%s/%s", to_base, relative_path); 2175d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath 2176d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath if (validate_apk_path_subdirs(from_path)) { 2177423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error(StringPrintf("Invalid from path %s", from_path)); 2178d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath } 2179d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath 2180d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath if (validate_apk_path_subdirs(to_path)) { 2181423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error(StringPrintf("Invalid to path %s", to_path)); 2182d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath } 2183d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath 2184423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (link(from_path, to_path) < 0) { 2185423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error(StringPrintf("Failed to link from %s to %s", from_path, to_path)); 2186d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath } 2187d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath 2188423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 2189d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath} 2190d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath 2191475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::moveAb(const std::string& apkPath, 2192475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const std::string& instructionSet, const std::string& outputPath) { 2193475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 21947a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 219590aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey 2196475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* apk_path = apkPath.c_str(); 2197475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* instruction_set = instructionSet.c_str(); 2198475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* oat_dir = outputPath.c_str(); 21990354bd0eaf24de533e86885b811f816f1e4d15c8Andreas Gampe 220090aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey bool success = move_ab(apk_path, instruction_set, oat_dir); 2201423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return success ? ok() : error(); 2202e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe} 2203e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe 2204475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::deleteOdex(const std::string& apkPath, 2205475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const std::string& instructionSet, const std::string& outputPath) { 2206475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 22077a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 220890aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey 2209475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* apk_path = apkPath.c_str(); 2210475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* instruction_set = instructionSet.c_str(); 2211475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* oat_dir = outputPath.c_str(); 2212475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey 221390aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey bool res = delete_odex(apk_path, instruction_set, oat_dir); 221490aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey return res ? ok() : error(); 22153964da0636624ff9af42d1f316bed0752e37c490Andreas Gampe} 22163964da0636624ff9af42d1f316bed0752e37c490Andreas Gampe 2217bd9683607d391a29b1422a50f8972267e9bddc47Calin Juravlebinder::Status InstalldNativeService::reconcileSecondaryDexFile( 2218bd9683607d391a29b1422a50f8972267e9bddc47Calin Juravle const std::string& dexPath, const std::string& packageName, int32_t uid, 2219bd9683607d391a29b1422a50f8972267e9bddc47Calin Juravle const std::vector<std::string>& isas, const std::unique_ptr<std::string>& volumeUuid, 2220bd9683607d391a29b1422a50f8972267e9bddc47Calin Juravle int32_t storage_flag, bool* _aidl_return) { 2221bd9683607d391a29b1422a50f8972267e9bddc47Calin Juravle ENFORCE_UID(AID_SYSTEM); 2222bd9683607d391a29b1422a50f8972267e9bddc47Calin Juravle CHECK_ARGUMENT_UUID(volumeUuid); 2223bd9683607d391a29b1422a50f8972267e9bddc47Calin Juravle CHECK_ARGUMENT_PACKAGE_NAME(packageName); 2224bd9683607d391a29b1422a50f8972267e9bddc47Calin Juravle 2225bd9683607d391a29b1422a50f8972267e9bddc47Calin Juravle std::lock_guard<std::recursive_mutex> lock(mLock); 2226bd9683607d391a29b1422a50f8972267e9bddc47Calin Juravle bool result = android::installd::reconcile_secondary_dex_file( 2227bd9683607d391a29b1422a50f8972267e9bddc47Calin Juravle dexPath, packageName, uid, isas, volumeUuid, storage_flag, _aidl_return); 2228bd9683607d391a29b1422a50f8972267e9bddc47Calin Juravle return result ? ok() : error(); 2229bd9683607d391a29b1422a50f8972267e9bddc47Calin Juravle} 2230bd9683607d391a29b1422a50f8972267e9bddc47Calin Juravle 223166b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkeybinder::Status InstalldNativeService::invalidateMounts() { 223266b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 2233d712f0ccc120357e1267a04ac6de9dd732b27e76Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mMountsLock); 223466b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey 2235d712f0ccc120357e1267a04ac6de9dd732b27e76Jeff Sharkey mStorageMounts.clear(); 2236d712f0ccc120357e1267a04ac6de9dd732b27e76Jeff Sharkey mQuotaReverseMounts.clear(); 223766b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey 223866b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey std::ifstream in("/proc/mounts"); 223966b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey if (!in.is_open()) { 224066b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey return error("Failed to read mounts"); 224166b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey } 224266b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey 224366b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey std::string source; 224466b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey std::string target; 224566b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey std::string ignored; 224666b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey while (!in.eof()) { 224766b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey std::getline(in, source, ' '); 224866b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey std::getline(in, target, ' '); 224966b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey std::getline(in, ignored); 225066b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey 2251d712f0ccc120357e1267a04ac6de9dd732b27e76Jeff Sharkey#if !BYPASS_SDCARDFS 2252d712f0ccc120357e1267a04ac6de9dd732b27e76Jeff Sharkey if (target.compare(0, 21, "/mnt/runtime/default/") == 0) { 2253d712f0ccc120357e1267a04ac6de9dd732b27e76Jeff Sharkey LOG(DEBUG) << "Found storage mount " << source << " at " << target; 2254d712f0ccc120357e1267a04ac6de9dd732b27e76Jeff Sharkey mStorageMounts[source] = target; 2255d712f0ccc120357e1267a04ac6de9dd732b27e76Jeff Sharkey } 2256d712f0ccc120357e1267a04ac6de9dd732b27e76Jeff Sharkey#endif 2257d712f0ccc120357e1267a04ac6de9dd732b27e76Jeff Sharkey 2258d712f0ccc120357e1267a04ac6de9dd732b27e76Jeff Sharkey#if !BYPASS_QUOTA 225966b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey if (source.compare(0, 11, "/dev/block/") == 0) { 2260d712f0ccc120357e1267a04ac6de9dd732b27e76Jeff Sharkey struct dqblk dq; 226166b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey if (quotactl(QCMD(Q_GETQUOTA, USRQUOTA), source.c_str(), 0, 226266b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey reinterpret_cast<char*>(&dq)) == 0) { 2263d712f0ccc120357e1267a04ac6de9dd732b27e76Jeff Sharkey LOG(DEBUG) << "Found quota mount " << source << " at " << target; 2264d712f0ccc120357e1267a04ac6de9dd732b27e76Jeff Sharkey mQuotaReverseMounts[target] = source; 2265e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey 2266e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey // ext4 only enables DQUOT_USAGE_ENABLED by default, so we 2267e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey // need to kick it again to enable DQUOT_LIMITS_ENABLED. 2268e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey if (quotactl(QCMD(Q_QUOTAON, USRQUOTA), source.c_str(), QFMT_VFS_V1, nullptr) != 0 2269e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey && errno != EBUSY) { 2270e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey PLOG(ERROR) << "Failed to enable USRQUOTA on " << source; 2271e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey } 2272e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey if (quotactl(QCMD(Q_QUOTAON, GRPQUOTA), source.c_str(), QFMT_VFS_V1, nullptr) != 0 2273e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey && errno != EBUSY) { 2274e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey PLOG(ERROR) << "Failed to enable GRPQUOTA on " << source; 2275e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey } 227666b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey } 227766b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey } 2278d712f0ccc120357e1267a04ac6de9dd732b27e76Jeff Sharkey#endif 227966b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey } 228066b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey return ok(); 228166b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey} 228266b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey 2283d712f0ccc120357e1267a04ac6de9dd732b27e76Jeff Sharkeystd::string InstalldNativeService::findDataMediaPath( 2284d712f0ccc120357e1267a04ac6de9dd732b27e76Jeff Sharkey const std::unique_ptr<std::string>& uuid, userid_t userid) { 2285d712f0ccc120357e1267a04ac6de9dd732b27e76Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mMountsLock); 2286d712f0ccc120357e1267a04ac6de9dd732b27e76Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 2287d712f0ccc120357e1267a04ac6de9dd732b27e76Jeff Sharkey auto path = StringPrintf("%s/media", create_data_path(uuid_).c_str()); 2288d712f0ccc120357e1267a04ac6de9dd732b27e76Jeff Sharkey auto resolved = mStorageMounts[path]; 2289d712f0ccc120357e1267a04ac6de9dd732b27e76Jeff Sharkey if (resolved.empty()) { 2290d712f0ccc120357e1267a04ac6de9dd732b27e76Jeff Sharkey LOG(WARNING) << "Failed to find storage mount for " << path; 2291d712f0ccc120357e1267a04ac6de9dd732b27e76Jeff Sharkey resolved = path; 2292d712f0ccc120357e1267a04ac6de9dd732b27e76Jeff Sharkey } 2293d712f0ccc120357e1267a04ac6de9dd732b27e76Jeff Sharkey return StringPrintf("%s/%u", resolved.c_str(), userid); 2294d712f0ccc120357e1267a04ac6de9dd732b27e76Jeff Sharkey} 2295d712f0ccc120357e1267a04ac6de9dd732b27e76Jeff Sharkey 229666b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkeystd::string InstalldNativeService::findQuotaDeviceForUuid( 229766b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey const std::unique_ptr<std::string>& uuid) { 2298d712f0ccc120357e1267a04ac6de9dd732b27e76Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mMountsLock); 229966b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey auto path = create_data_path(uuid ? uuid->c_str() : nullptr); 2300d712f0ccc120357e1267a04ac6de9dd732b27e76Jeff Sharkey return mQuotaReverseMounts[path]; 230166b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey} 230266b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey 2303325b8c94a5ae47b7903ea5fe386a78466d6cb3b7Jeff Sharkeybinder::Status InstalldNativeService::isQuotaSupported( 2304325b8c94a5ae47b7903ea5fe386a78466d6cb3b7Jeff Sharkey const std::unique_ptr<std::string>& volumeUuid, bool* _aidl_return) { 2305325b8c94a5ae47b7903ea5fe386a78466d6cb3b7Jeff Sharkey *_aidl_return = !findQuotaDeviceForUuid(volumeUuid).empty(); 2306325b8c94a5ae47b7903ea5fe386a78466d6cb3b7Jeff Sharkey return ok(); 2307325b8c94a5ae47b7903ea5fe386a78466d6cb3b7Jeff Sharkey} 2308325b8c94a5ae47b7903ea5fe386a78466d6cb3b7Jeff Sharkey 230902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe} // namespace installd 231002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe} // namespace android 2311