InstalldNativeService.cpp revision 466459b68da52edf20962c3c80e2fe0325569c18
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> 25d089ca1703769854356a263ca640d3e07ab8548dAndreas 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 396fb5a0179d4de9c85351fd016277949804c809edAndreas 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 59df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#include "MatchExtensionGen.h" 60df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 6102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#ifndef LOG_TAG 6202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#define LOG_TAG "installd" 6302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#endif 6441ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey 6541ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkeyusing android::base::StringPrintf; 6666b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkeyusing std::endl; 6794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 6802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampenamespace android { 6902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampenamespace installd { 7094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 71cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkeystatic constexpr const char* kCpPath = "/system/bin/cp"; 72cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkeystatic constexpr const char* kXattrDefault = "user.default"; 73e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 74d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampestatic constexpr const char* PKG_LIB_POSTFIX = "/lib"; 75d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampestatic constexpr const char* CACHE_DIR_POSTFIX = "/cache"; 76d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampestatic constexpr const char* CODE_CACHE_DIR_POSTFIX = "/code_cache"; 77d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe 78d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampestatic constexpr const char* IDMAP_PREFIX = "/data/resource-cache/"; 79d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampestatic constexpr const char* IDMAP_SUFFIX = "@idmap"; 80d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe 81d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe// NOTE: keep in sync with StorageManager 82d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampestatic constexpr int FLAG_STORAGE_DE = 1 << 0; 83d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampestatic constexpr int FLAG_STORAGE_CE = 1 << 1; 84d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe 85d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe// NOTE: keep in sync with Installer 86d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampestatic constexpr int FLAG_CLEAR_CACHE_ONLY = 1 << 8; 87d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampestatic constexpr int FLAG_CLEAR_CODE_CACHE_ONLY = 1 << 9; 883dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkeystatic constexpr int FLAG_USE_QUOTA = 1 << 12; 89d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe 90eecb2d205366351af42fd0cd9e1a95de3980764eJanis Danisevskis#define MIN_RESTRICTED_HOME_SDK_VERSION 24 // > M 910274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeynamespace { 920274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 930274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeyconstexpr const char* kDump = "android.permission.DUMP"; 940274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 95423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkeystatic binder::Status ok() { 96423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return binder::Status::ok(); 97423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey} 98423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 99423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkeystatic binder::Status exception(uint32_t code, const std::string& msg) { 100423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return binder::Status::fromExceptionCode(code, String8(msg.c_str())); 101423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey} 102423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 103423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkeystatic binder::Status error() { 104423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return binder::Status::fromServiceSpecificError(errno); 105423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey} 106423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 107423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkeystatic binder::Status error(const std::string& msg) { 108423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey PLOG(ERROR) << msg; 109423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return binder::Status::fromServiceSpecificError(errno, String8(msg.c_str())); 110423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey} 111423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 112423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkeystatic binder::Status error(uint32_t code, const std::string& msg) { 113423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey LOG(ERROR) << msg << " (" << code << ")"; 114423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return binder::Status::fromServiceSpecificError(code, String8(msg.c_str())); 115423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey} 116423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 1170274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeybinder::Status checkPermission(const char* permission) { 1180274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey pid_t pid; 1190274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey uid_t uid; 1200274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 1210274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey if (checkCallingPermission(String16(permission), reinterpret_cast<int32_t*>(&pid), 1220274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey reinterpret_cast<int32_t*>(&uid))) { 123423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 1240274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey } else { 125423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return exception(binder::Status::EX_SECURITY, 126423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey StringPrintf("UID %d / PID %d lacks permission %s", uid, pid, permission)); 1270274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey } 1280274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey} 1290274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 1300274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeybinder::Status checkUid(uid_t expectedUid) { 1310274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey uid_t uid = IPCThreadState::self()->getCallingUid(); 1320274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey if (uid == expectedUid || uid == AID_ROOT) { 133423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 1340274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey } else { 135423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return exception(binder::Status::EX_SECURITY, 136423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey StringPrintf("UID %d is not expected UID %d", uid, expectedUid)); 137423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 138423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey} 139423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 140423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkeybinder::Status checkArgumentUuid(const std::unique_ptr<std::string>& uuid) { 141423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (!uuid || is_valid_filename(*uuid)) { 142423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 143423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } else { 144423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return exception(binder::Status::EX_ILLEGAL_ARGUMENT, 145423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey StringPrintf("UUID %s is malformed", uuid->c_str())); 146423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 147423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey} 148423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 149423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkeybinder::Status checkArgumentPackageName(const std::string& packageName) { 150423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (is_valid_package_name(packageName.c_str())) { 151423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 152423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } else { 153423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return exception(binder::Status::EX_ILLEGAL_ARGUMENT, 154423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey StringPrintf("Package name %s is malformed", packageName.c_str())); 1550274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey } 1560274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey} 1570274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 1580274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey#define ENFORCE_UID(uid) { \ 1590274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey binder::Status status = checkUid((uid)); \ 1600274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey if (!status.isOk()) { \ 1610274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey return status; \ 1620274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey } \ 1630274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey} 1640274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 165423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey#define CHECK_ARGUMENT_UUID(uuid) { \ 166423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey binder::Status status = checkArgumentUuid((uuid)); \ 167423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (!status.isOk()) { \ 168423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return status; \ 169423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } \ 170423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey} 171423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 172423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey#define CHECK_ARGUMENT_PACKAGE_NAME(packageName) { \ 173423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey binder::Status status = \ 174423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey checkArgumentPackageName((packageName)); \ 175423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (!status.isOk()) { \ 176423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return status; \ 177423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } \ 178423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey} 179423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 1800274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey} // namespace 1810274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 1820274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeystatus_t InstalldNativeService::start() { 1830274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey IPCThreadState::self()->disableBackgroundScheduling(true); 1840274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey status_t ret = BinderService<InstalldNativeService>::publish(); 1850274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey if (ret != android::OK) { 1860274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey return ret; 1870274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey } 1880274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey sp<ProcessState> ps(ProcessState::self()); 1890274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey ps->startThreadPool(); 1900274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey ps->giveThreadPoolName(); 1910274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey return android::OK; 1920274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey} 1930274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 1940274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeystatus_t InstalldNativeService::dump(int fd, const Vector<String16> & /* args */) { 19566b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey auto out = std::fstream(StringPrintf("/proc/self/fd/%d", fd)); 1960274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const binder::Status dump_permission = checkPermission(kDump); 1970274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey if (!dump_permission.isOk()) { 19866b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey out << dump_permission.toString8() << endl; 1990274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey return PERMISSION_DENIED; 2000274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey } 20166b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 20266b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey 20366b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey out << "installd is happy!" << endl << endl; 20466b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey out << "Devices with quota support:" << endl; 20566b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey for (const auto& n : mQuotaDevices) { 20666b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey out << " " << n.first << " = " << n.second << endl; 20766b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey } 20866b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey out << endl; 20966b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey out.flush(); 2100274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 2110274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey return NO_ERROR; 2120274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey} 2130274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 2147db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey/** 2157db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey * Perform restorecon of the given path, but only perform recursive restorecon 2167db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey * if the label of that top-level file actually changed. This can save us 2177db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey * significant time by avoiding no-op traversals of large filesystem trees. 2187db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey */ 21935b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkeystatic int restorecon_app_data_lazy(const std::string& path, const std::string& seInfo, uid_t uid, 22035b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey bool existing) { 2217db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey int res = 0; 2227db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey char* before = nullptr; 2237db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey char* after = nullptr; 2247db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey 2257db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey // Note that SELINUX_ANDROID_RESTORECON_DATADATA flag is set by 2267db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey // libselinux. Not needed here. 2277db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey 22895d9ac642f082870fd4d2419f534171a9b82bbcdJeff Sharkey if (lgetfilecon(path.c_str(), &before) < 0) { 2297db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey PLOG(ERROR) << "Failed before getfilecon for " << path; 2307db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey goto fail; 2317db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey } 2320274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey if (selinux_android_restorecon_pkgdir(path.c_str(), seInfo.c_str(), uid, 0) < 0) { 2337db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey PLOG(ERROR) << "Failed top-level restorecon for " << path; 2347db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey goto fail; 2357db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey } 23695d9ac642f082870fd4d2419f534171a9b82bbcdJeff Sharkey if (lgetfilecon(path.c_str(), &after) < 0) { 2377db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey PLOG(ERROR) << "Failed after getfilecon for " << path; 2387db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey goto fail; 2397db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey } 2407db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey 2417db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey // If the initial top-level restorecon above changed the label, then go 2427db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey // back and restorecon everything recursively 2437db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey if (strcmp(before, after)) { 24435b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey if (existing) { 24535b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey LOG(DEBUG) << "Detected label change from " << before << " to " << after << " at " 24635b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey << path << "; running recursive restorecon"; 24735b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey } 2480274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey if (selinux_android_restorecon_pkgdir(path.c_str(), seInfo.c_str(), uid, 2497db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey SELINUX_ANDROID_RESTORECON_RECURSE) < 0) { 2507db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey PLOG(ERROR) << "Failed recursive restorecon for " << path; 2517db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey goto fail; 2527db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey } 2537db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey } 2547db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey 2557db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey goto done; 2567db6041d156b750689b32e340d8a0753061262a3Jeff Sharkeyfail: 2577db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey res = -1; 2587db6041d156b750689b32e340d8a0753061262a3Jeff Sharkeydone: 2597db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey free(before); 2607db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey free(after); 2617db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey return res; 2627db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey} 2637db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey 2640274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeystatic int restorecon_app_data_lazy(const std::string& parent, const char* name, 26535b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey const std::string& seInfo, uid_t uid, bool existing) { 26635b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey return restorecon_app_data_lazy(StringPrintf("%s/%s", parent.c_str(), name), seInfo, uid, 26735b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey existing); 26895d9ac642f082870fd4d2419f534171a9b82bbcdJeff Sharkey} 26995d9ac642f082870fd4d2419f534171a9b82bbcdJeff Sharkey 2707db6041d156b750689b32e340d8a0753061262a3Jeff Sharkeystatic int prepare_app_dir(const std::string& path, mode_t target_mode, uid_t uid) { 2719a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey if (fs_prepare_dir_strict(path.c_str(), target_mode, uid, uid) != 0) { 2729a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey PLOG(ERROR) << "Failed to prepare " << path; 2739a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey return -1; 2749a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey } 2759a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey return 0; 2769a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey} 2779a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey 2783dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey/** 2793dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey * Prepare an app cache directory, which offers to fix-up the GID and 2803dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey * directory mode flags during a platform upgrade. 2813dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey */ 2823dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkeystatic int prepare_app_cache_dir(const std::string& parent, const char* name, mode_t target_mode, 2833dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey uid_t uid, gid_t gid) { 2843dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey auto path = StringPrintf("%s/%s", parent.c_str(), name); 2853dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey struct stat st; 2863dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (stat(path.c_str(), &st) != 0) { 2873dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (errno == ENOENT) { 2883dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // This is fine, just create it 2893dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (fs_prepare_dir_strict(path.c_str(), target_mode, uid, gid) != 0) { 2903dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey PLOG(ERROR) << "Failed to prepare " << path; 2913dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey return -1; 2923dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } else { 2933dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey return 0; 2943dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 2953dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } else { 2963dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey PLOG(ERROR) << "Failed to stat " << path; 2973dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey return -1; 2983dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 2993dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 3003dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 30124ef15bd009204b73ebf0e94ba3d868177a5f44cJeff Sharkey mode_t actual_mode = st.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO | S_ISGID); 3023dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (st.st_uid != uid) { 3033dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // Mismatched UID is real trouble; we can't recover 3043dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey LOG(ERROR) << "Mismatched UID at " << path << ": found " << st.st_uid 3053dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey << " but expected " << uid; 3063dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey return -1; 30724ef15bd009204b73ebf0e94ba3d868177a5f44cJeff Sharkey } else if (st.st_gid == gid && actual_mode == target_mode) { 3083dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // Everything looks good! 3093dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey return 0; 3103dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 3113dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 3123dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // Directory is owned correctly, but GID or mode mismatch means it's 3133dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // probably a platform upgrade so we need to fix them 3143dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey FTS *fts; 3153dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey FTSENT *p; 3163dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey char *argv[] = { (char*) path.c_str(), nullptr }; 3173dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_XDEV, NULL))) { 3183dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey PLOG(ERROR) << "Failed to fts_open " << path; 3193dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey return -1; 3203dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 3213dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey while ((p = fts_read(fts)) != NULL) { 3223dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey switch (p->fts_info) { 3233dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey case FTS_DP: 3243dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (chmod(p->fts_accpath, target_mode) != 0) { 3253dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey PLOG(WARNING) << "Failed to chmod " << p->fts_path; 3263dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 3273dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // Intentional fall through to also set GID 3283dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey case FTS_F: 3293dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (chown(p->fts_accpath, -1, gid) != 0) { 3303dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey PLOG(WARNING) << "Failed to chown " << p->fts_path; 3313dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 3323dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey break; 3333dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey case FTS_SL: 3343dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey case FTS_SLNONE: 3353dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (lchown(p->fts_accpath, -1, gid) != 0) { 3363dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey PLOG(WARNING) << "Failed to chown " << p->fts_path; 3373dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 3383dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey break; 3393dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 3403dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 3413dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey fts_close(fts); 3423dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey return 0; 3439a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey} 3449a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey 3450274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeybinder::Status InstalldNativeService::createAppData(const std::unique_ptr<std::string>& uuid, 3460274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const std::string& packageName, int32_t userId, int32_t flags, int32_t appId, 34736a900a0db526d63e899ed5d5b0a463ad7f9236eJeff Sharkey const std::string& seInfo, int32_t targetSdkVersion, int64_t* _aidl_return) { 3480274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 349423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 350423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 3517a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 3520274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 3530274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 3540274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const char* pkgname = packageName.c_str(); 3550274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 35636a900a0db526d63e899ed5d5b0a463ad7f9236eJeff Sharkey // Assume invalid inode unless filled in below 35736a900a0db526d63e899ed5d5b0a463ad7f9236eJeff Sharkey if (_aidl_return != nullptr) *_aidl_return = -1; 35836a900a0db526d63e899ed5d5b0a463ad7f9236eJeff Sharkey 35924ef15bd009204b73ebf0e94ba3d868177a5f44cJeff Sharkey int32_t uid = multiuser_get_uid(userId, appId); 36024ef15bd009204b73ebf0e94ba3d868177a5f44cJeff Sharkey int32_t cacheGid = multiuser_get_cache_gid(userId, appId); 3613dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey mode_t targetMode = targetSdkVersion >= MIN_RESTRICTED_HOME_SDK_VERSION ? 0700 : 0751; 3623dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 36324ef15bd009204b73ebf0e94ba3d868177a5f44cJeff Sharkey // If UID doesn't have a specific cache GID, use UID value 36424ef15bd009204b73ebf0e94ba3d868177a5f44cJeff Sharkey if (cacheGid == -1) { 36524ef15bd009204b73ebf0e94ba3d868177a5f44cJeff Sharkey cacheGid = uid; 36624ef15bd009204b73ebf0e94ba3d868177a5f44cJeff Sharkey } 36724ef15bd009204b73ebf0e94ba3d868177a5f44cJeff Sharkey 368aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (flags & FLAG_STORAGE_CE) { 3690274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey auto path = create_data_user_ce_package_path(uuid_, userId, pkgname); 37035b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey bool existing = (access(path.c_str(), F_OK) == 0); 37135b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey 3723dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (prepare_app_dir(path, targetMode, uid) || 3733dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey prepare_app_cache_dir(path, "cache", 02771, uid, cacheGid) || 3743dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey prepare_app_cache_dir(path, "code_cache", 02771, uid, cacheGid)) { 375423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to prepare " + path); 3767db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey } 3777db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey 3787db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey // Consider restorecon over contents if label changed 37935b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey if (restorecon_app_data_lazy(path, seInfo, uid, existing) || 38035b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey restorecon_app_data_lazy(path, "cache", seInfo, uid, existing) || 38135b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey restorecon_app_data_lazy(path, "code_cache", seInfo, uid, existing)) { 382423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to restorecon " + path); 383c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey } 3849a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey 3859a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey // Remember inode numbers of cache directories so that we can clear 3869a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey // contents while CE storage is locked 3879a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey if (write_path_inode(path, "cache", kXattrInodeCache) || 3889a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey write_path_inode(path, "code_cache", kXattrInodeCodeCache)) { 389423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to write_path_inode for " + path); 390c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey } 39136a900a0db526d63e899ed5d5b0a463ad7f9236eJeff Sharkey 39236a900a0db526d63e899ed5d5b0a463ad7f9236eJeff Sharkey // And return the CE inode of the top-level data directory so we can 39336a900a0db526d63e899ed5d5b0a463ad7f9236eJeff Sharkey // clear contents while CE storage is locked 39436a900a0db526d63e899ed5d5b0a463ad7f9236eJeff Sharkey if ((_aidl_return != nullptr) 39536a900a0db526d63e899ed5d5b0a463ad7f9236eJeff Sharkey && get_path_inode(path, reinterpret_cast<ino_t*>(_aidl_return)) != 0) { 39636a900a0db526d63e899ed5d5b0a463ad7f9236eJeff Sharkey return error("Failed to get_path_inode for " + path); 39736a900a0db526d63e899ed5d5b0a463ad7f9236eJeff Sharkey } 39894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 399aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (flags & FLAG_STORAGE_DE) { 4000274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey auto path = create_data_user_de_package_path(uuid_, userId, pkgname); 40135b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey bool existing = (access(path.c_str(), F_OK) == 0); 40235b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey 4033dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (prepare_app_dir(path, targetMode, uid) || 4043dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey prepare_app_cache_dir(path, "cache", 02771, uid, cacheGid) || 4053dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey prepare_app_cache_dir(path, "code_cache", 02771, uid, cacheGid)) { 406423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to prepare " + path); 407c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey } 4086a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 4097db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey // Consider restorecon over contents if label changed 41035b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey if (restorecon_app_data_lazy(path, seInfo, uid, existing)) { 411423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to restorecon " + path); 4127db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey } 4137db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey 41490aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey if (property_get_bool("dalvik.vm.usejitprofiles", false)) { 4150274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const std::string profile_path = create_data_user_profile_package_path(userId, pkgname); 4166a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle // read-write-execute only for the app user. 4176a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle if (fs_prepare_dir_strict(profile_path.c_str(), 0700, uid, uid) != 0) { 418423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to prepare " + profile_path); 4196a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 420bc56e7dcf07e633867e1525435373a1231216b22Calin Juravle std::string profile_file = create_primary_profile(profile_path); 421bc56e7dcf07e633867e1525435373a1231216b22Calin Juravle // read-write only for the app user. 422bc56e7dcf07e633867e1525435373a1231216b22Calin Juravle if (fs_prepare_file_strict(profile_file.c_str(), 0600, uid, uid) != 0) { 423423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to prepare " + profile_path); 424bc56e7dcf07e633867e1525435373a1231216b22Calin Juravle } 4256a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle const std::string ref_profile_path = create_data_ref_profile_package_path(pkgname); 4266a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle // dex2oat/profman runs under the shared app gid and it needs to read/write reference 4276a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle // profiles. 428adddd9876e145879169369913b5afe469b17c02dJeff Sharkey int shared_app_gid = multiuser_get_shared_gid(0, appId); 4292e6dc9baaaf5d50b42ce36435e13c2559808b837Jeff Sharkey if ((shared_app_gid != -1) && fs_prepare_dir_strict( 4306a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle ref_profile_path.c_str(), 0700, shared_app_gid, shared_app_gid) != 0) { 431423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to prepare " + ref_profile_path); 4326a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 4336a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 43494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 435423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 43694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 43794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 438c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkeybinder::Status InstalldNativeService::migrateAppData(const std::unique_ptr<std::string>& uuid, 439c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const std::string& packageName, int32_t userId, int32_t flags) { 440c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 441423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 442423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 4437a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 444423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 445c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 446c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* pkgname = packageName.c_str(); 447c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey 448cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey // This method only exists to upgrade system apps that have requested 449cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey // forceDeviceEncrypted, so their default storage always lives in a 450cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey // consistent location. This only works on non-FBE devices, since we 451cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey // never want to risk exposing data on a device with real CE/DE storage. 452cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey 453c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey auto ce_path = create_data_user_ce_package_path(uuid_, userId, pkgname); 454c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey auto de_path = create_data_user_de_package_path(uuid_, userId, pkgname); 455cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey 456cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey // If neither directory is marked as default, assume CE is default 457cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey if (getxattr(ce_path.c_str(), kXattrDefault, nullptr, 0) == -1 458cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey && getxattr(de_path.c_str(), kXattrDefault, nullptr, 0) == -1) { 459cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey if (setxattr(ce_path.c_str(), kXattrDefault, nullptr, 0, 0) != 0) { 460423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to mark default storage " + ce_path); 461cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey } 462cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey } 463cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey 464cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey // Migrate default data location if needed 465cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey auto target = (flags & FLAG_STORAGE_DE) ? de_path : ce_path; 466cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey auto source = (flags & FLAG_STORAGE_DE) ? ce_path : de_path; 467cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey 468cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey if (getxattr(target.c_str(), kXattrDefault, nullptr, 0) == -1) { 469cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey LOG(WARNING) << "Requested default storage " << target 470cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey << " is not active; migrating from " << source; 471cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey if (delete_dir_contents_and_dir(target) != 0) { 472423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to delete " + target); 473cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey } 474cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey if (rename(source.c_str(), target.c_str()) != 0) { 475423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to rename " + source + " to " + target); 476cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey } 477cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey } 478cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey 479423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 480cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey} 481cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey 4826a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 483475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::clearAppProfiles(const std::string& packageName) { 484475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 485423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 4867a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 487423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 488475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* pkgname = packageName.c_str(); 489423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey binder::Status res = ok(); 490423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (!clear_reference_profile(pkgname)) { 491423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to clear reference profile for " + packageName); 492423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 493423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (!clear_current_profiles(pkgname)) { 494423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to clear current profiles for " + packageName); 495423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 496423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return res; 4976a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle} 4986a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 499c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkeybinder::Status InstalldNativeService::clearAppData(const std::unique_ptr<std::string>& uuid, 500c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const std::string& packageName, int32_t userId, int32_t flags, int64_t ceDataInode) { 501c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 502423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 503423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 5047a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 505423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 506c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 507c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* pkgname = packageName.c_str(); 508c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey 509423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey binder::Status res = ok(); 510aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (flags & FLAG_STORAGE_CE) { 511c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey auto path = create_data_user_ce_package_path(uuid_, userId, pkgname, ceDataInode); 5129a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey if (flags & FLAG_CLEAR_CACHE_ONLY) { 5139a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey path = read_path_inode(path, "cache", kXattrInodeCache); 5149a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey } else if (flags & FLAG_CLEAR_CODE_CACHE_ONLY) { 5159a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey path = read_path_inode(path, "code_cache", kXattrInodeCodeCache); 5169a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey } 517c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey if (access(path.c_str(), F_OK) == 0) { 518423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (delete_dir_contents(path) != 0) { 519423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to delete contents of " + path); 520423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 521c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey } 52294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 523aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (flags & FLAG_STORAGE_DE) { 5249a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey std::string suffix = ""; 5259a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey bool only_cache = false; 5269a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey if (flags & FLAG_CLEAR_CACHE_ONLY) { 5279a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey suffix = CACHE_DIR_POSTFIX; 5289a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey only_cache = true; 5299a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey } else if (flags & FLAG_CLEAR_CODE_CACHE_ONLY) { 5309a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey suffix = CODE_CACHE_DIR_POSTFIX; 5319a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey only_cache = true; 5329a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey } 5339a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey 534c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey auto path = create_data_user_de_package_path(uuid_, userId, pkgname) + suffix; 535c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey if (access(path.c_str(), F_OK) == 0) { 536423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (delete_dir_contents(path) != 0) { 537423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to delete contents of " + path); 538423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 539c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey } 540edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle if (!only_cache) { 541c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey if (!clear_current_profile(pkgname, userId)) { 542423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to clear current profile for " + packageName); 543edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle } 544edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle } 54594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 546423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return res; 547c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey} 54894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 5497535e8e1995398befc866062d63c05d90eb70cccCalin Juravlestatic int destroy_app_reference_profile(const char *pkgname) { 5507535e8e1995398befc866062d63c05d90eb70cccCalin Juravle return delete_dir_contents_and_dir( 5517535e8e1995398befc866062d63c05d90eb70cccCalin Juravle create_data_ref_profile_package_path(pkgname), 5527535e8e1995398befc866062d63c05d90eb70cccCalin Juravle /*ignore_if_missing*/ true); 5537535e8e1995398befc866062d63c05d90eb70cccCalin Juravle} 5547535e8e1995398befc866062d63c05d90eb70cccCalin Juravle 555edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravlestatic int destroy_app_current_profiles(const char *pkgname, userid_t userid) { 556b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravle return delete_dir_contents_and_dir( 557b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravle create_data_user_profile_package_path(userid, pkgname), 558b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravle /*ignore_if_missing*/ true); 559edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle} 560edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle 561475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::destroyAppProfiles(const std::string& packageName) { 562475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 563423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 5647a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 565423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 566475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* pkgname = packageName.c_str(); 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) { 570423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (destroy_app_current_profiles(pkgname, user) != 0) { 571423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to destroy current profiles for " + packageName); 572423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 573edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle } 574423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (destroy_app_reference_profile(pkgname) != 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 } 602c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey destroy_app_current_profiles(pkgname, 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. 6067535e8e1995398befc866062d63c05d90eb70cccCalin Juravle destroy_app_reference_profile(pkgname); 60794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 608423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return res; 60994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 61094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 6110274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeybinder::Status InstalldNativeService::moveCompleteApp(const std::unique_ptr<std::string>& fromUuid, 6120274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const std::unique_ptr<std::string>& toUuid, const std::string& packageName, 6130274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const std::string& dataAppName, int32_t appId, const std::string& seInfo, 6140274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey int32_t targetSdkVersion) { 615423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 616423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(fromUuid); 617423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(toUuid); 618423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 6197a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 6200274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 6210274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const char* from_uuid = fromUuid ? fromUuid->c_str() : nullptr; 6220274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const char* to_uuid = toUuid ? toUuid->c_str() : nullptr; 6230274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const char* package_name = packageName.c_str(); 6240274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const char* data_app_name = dataAppName.c_str(); 6250274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 626423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey binder::Status res = ok(); 627e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey std::vector<userid_t> users = get_known_users(from_uuid); 628e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 629d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey // Copy app 630d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey { 63151c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto from = create_data_app_package_path(from_uuid, data_app_name); 63251c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto to = create_data_app_package_path(to_uuid, data_app_name); 63351c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto to_parent = create_data_app_path(to_uuid); 634d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey 635d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey char *argv[] = { 636d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey (char*) kCpPath, 637d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey (char*) "-F", /* delete any existing destination file first (--remove-destination) */ 638d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey (char*) "-p", /* preserve timestamps, ownership, and permissions */ 639d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey (char*) "-R", /* recurse into subdirectories (DEST must be a directory) */ 640d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey (char*) "-P", /* Do not follow symlinks [default] */ 641d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey (char*) "-d", /* don't dereference symlinks */ 642d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey (char*) from.c_str(), 643d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey (char*) to_parent.c_str() 644d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey }; 645d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey 646d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey LOG(DEBUG) << "Copying " << from << " to " << to; 647d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true); 648d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey if (rc != 0) { 649423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error(rc, "Failed copying " + from + " to " + to); 650d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey goto fail; 651d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey } 652d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey 653d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey if (selinux_android_restorecon(to.c_str(), SELINUX_ANDROID_RESTORECON_RECURSE) != 0) { 654423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to restorecon " + to); 655d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey goto fail; 656d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey } 657d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey } 658d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey 659d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey // Copy private data for all known users 660e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey for (auto user : users) { 661e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 662e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey // Data source may not exist for all users; that's okay 66351c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto from_ce = create_data_user_ce_package_path(from_uuid, user, package_name); 66451c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey if (access(from_ce.c_str(), F_OK) != 0) { 66551c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey LOG(INFO) << "Missing source " << from_ce; 666e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey continue; 667e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey } 668e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 6690274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey if (!createAppData(toUuid, packageName, user, FLAG_STORAGE_CE | FLAG_STORAGE_DE, appId, 67036a900a0db526d63e899ed5d5b0a463ad7f9236eJeff Sharkey seInfo, targetSdkVersion, nullptr).isOk()) { 671423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to create package target"); 672e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey goto fail; 673e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey } 674e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 675e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey char *argv[] = { 676e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey (char*) kCpPath, 677e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey (char*) "-F", /* delete any existing destination file first (--remove-destination) */ 678e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey (char*) "-p", /* preserve timestamps, ownership, and permissions */ 679e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey (char*) "-R", /* recurse into subdirectories (DEST must be a directory) */ 680e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey (char*) "-P", /* Do not follow symlinks [default] */ 681e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey (char*) "-d", /* don't dereference symlinks */ 68251c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey nullptr, 68351c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey nullptr 684e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey }; 685e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 68651c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey { 68751c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto from = create_data_user_de_package_path(from_uuid, user, package_name); 68851c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto to = create_data_user_de_path(to_uuid, user); 68951c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey argv[6] = (char*) from.c_str(); 69051c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey argv[7] = (char*) to.c_str(); 69151c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey 69251c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey LOG(DEBUG) << "Copying " << from << " to " << to; 69351c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true); 69451c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey if (rc != 0) { 695423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error(rc, "Failed copying " + from + " to " + to); 69651c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey goto fail; 69751c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey } 69851c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey } 69951c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey { 70051c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto from = create_data_user_ce_package_path(from_uuid, user, package_name); 70151c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto to = create_data_user_ce_path(to_uuid, user); 70251c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey argv[6] = (char*) from.c_str(); 70351c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey argv[7] = (char*) to.c_str(); 70451c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey 70551c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey LOG(DEBUG) << "Copying " << from << " to " << to; 70651c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true); 70751c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey if (rc != 0) { 708423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error(rc, "Failed copying " + from + " to " + to); 70951c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey goto fail; 71051c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey } 711e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey } 712e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 713c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey if (!restoreconAppData(toUuid, packageName, user, FLAG_STORAGE_CE | FLAG_STORAGE_DE, 714c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey appId, seInfo).isOk()) { 715423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to restorecon"); 716c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey goto fail; 717c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey } 718e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey } 719e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 72031f08986f83fa6f2dcf55523b2cf706460aeed7cJeff Sharkey // We let the framework scan the new location and persist that before 72131f08986f83fa6f2dcf55523b2cf706460aeed7cJeff Sharkey // deleting the data in the old location; this ordering ensures that 72231f08986f83fa6f2dcf55523b2cf706460aeed7cJeff Sharkey // we can recover from things like battery pulls. 723423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 724e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 725e36372423000a906bafae68844ebc6c42d09335aJeff Sharkeyfail: 726e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey // Nuke everything we might have already copied 727d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey { 72851c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto to = create_data_app_package_path(to_uuid, data_app_name); 729d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey if (delete_dir_contents(to.c_str(), 1, NULL) != 0) { 730d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey LOG(WARNING) << "Failed to rollback " << to; 731d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey } 732d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey } 733e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey for (auto user : users) { 73451c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey { 73551c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto to = create_data_user_de_package_path(to_uuid, user, package_name); 73651c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey if (delete_dir_contents(to.c_str(), 1, NULL) != 0) { 73751c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey LOG(WARNING) << "Failed to rollback " << to; 73851c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey } 73951c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey } 74051c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey { 74151c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto to = create_data_user_ce_package_path(to_uuid, user, package_name); 74251c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey if (delete_dir_contents(to.c_str(), 1, NULL) != 0) { 74351c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey LOG(WARNING) << "Failed to rollback " << to; 74451c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey } 745e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey } 746e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey } 747423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return res; 748e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey} 749e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 750c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkeybinder::Status InstalldNativeService::createUserData(const std::unique_ptr<std::string>& uuid, 751c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey int32_t userId, int32_t userSerial ATTRIBUTE_UNUSED, int32_t flags) { 752c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 753423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 7547a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 755423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 756c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 757379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey if (flags & FLAG_STORAGE_DE) { 758c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey if (uuid_ == nullptr) { 759423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (ensure_config_user_dirs(userId) != 0) { 7607be5ead81aa4d3b2536556c762bab03445a98155Jeff Sharkey return error(StringPrintf("Failed to ensure dirs for %d", userId)); 761423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 762379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey } 76394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 7647be5ead81aa4d3b2536556c762bab03445a98155Jeff Sharkey return ok(); 765095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee} 766095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee 767c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkeybinder::Status InstalldNativeService::destroyUserData(const std::unique_ptr<std::string>& uuid, 768c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey int32_t userId, int32_t flags) { 769c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 770423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 7717a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 772423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 773c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 774423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey binder::Status res = ok(); 775379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey if (flags & FLAG_STORAGE_DE) { 776423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey auto path = create_data_user_de_path(uuid_, userId); 777423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (delete_dir_contents_and_dir(path, true) != 0) { 778423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to delete " + path); 779423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 780c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey if (uuid_ == nullptr) { 781423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey path = create_data_misc_legacy_path(userId); 782423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (delete_dir_contents_and_dir(path, true) != 0) { 783423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to delete " + path); 784423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 785df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey path = create_data_user_profile_path(userId); 786423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (delete_dir_contents_and_dir(path, true) != 0) { 787423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to delete " + path); 788423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 78941ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey } 79094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 791379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey if (flags & FLAG_STORAGE_CE) { 792423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey auto path = create_data_user_ce_path(uuid_, userId); 793423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (delete_dir_contents_and_dir(path, true) != 0) { 794423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to delete " + path); 795423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 796423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey path = create_data_media_path(uuid_, userId); 797423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (delete_dir_contents_and_dir(path, true) != 0) { 798423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to delete " + path); 799423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 800379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey } 801423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return res; 80294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 80394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 80494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/* Try to ensure free_size bytes of storage are available. 80594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * Returns 0 on success. 80694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * This is rather simple-minded because doing a full LRU would 80794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * be potentially memory-intensive, and without atime it would 80894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * also require that apps constantly modify file metadata even 80994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * when just reading from the cache, which is pretty awful. 81094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */ 811475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::freeCache(const std::unique_ptr<std::string>& uuid, 812475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey int64_t freeStorageSize) { 813475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 814423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 8157a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 816423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 817475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 81894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache_t* cache; 81994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int64_t avail; 82094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 821475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey auto data_path = create_data_path(uuid_); 82241ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey 82341ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey avail = data_disk_free(data_path); 824475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey if (avail < 0) { 825423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to determine free space for " + data_path); 826475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey } 82794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 828475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ALOGI("free_cache(%" PRId64 ") avail %" PRId64 "\n", freeStorageSize, avail); 829475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey if (avail >= freeStorageSize) { 830423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 831475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey } 83294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 83394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache = start_cache_collection(); 83494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 835475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey auto users = get_known_users(uuid_); 83654e292e1ea87b504b552393d2e9bc800458bd0c1Jeff Sharkey for (auto user : users) { 837475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey add_cache_files(cache, create_data_user_ce_path(uuid_, user)); 838475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey add_cache_files(cache, create_data_user_de_path(uuid_, user)); 83954e292e1ea87b504b552393d2e9bc800458bd0c1Jeff Sharkey add_cache_files(cache, 840475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey StringPrintf("%s/Android/data", create_data_media_path(uuid_, user).c_str())); 84194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 84294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 843475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey clear_cache_files(data_path, cache, freeStorageSize); 84494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood finish_cache_collection(cache); 84594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 846423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey avail = data_disk_free(data_path); 847423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (avail >= freeStorageSize) { 848423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 849475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey } else { 850423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error(StringPrintf("Failed to free up %" PRId64 " on %s; final free space %" PRId64, 851423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey freeStorageSize, data_path.c_str(), avail)); 852475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey } 85394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 85494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 855475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::rmdex(const std::string& codePath, 856475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const std::string& instructionSet) { 857475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 8587a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 8597a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey 86094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood char dex_path[PKG_PATH_MAX]; 86194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 862475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* path = codePath.c_str(); 863475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* instruction_set = instructionSet.c_str(); 864475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey 865770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey if (validate_apk_path(path) && validate_system_app_path(path)) { 866423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Invalid path " + codePath); 867770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey } 868770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey 869475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey if (!create_cache_path(dex_path, path, instruction_set)) { 870423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to create cache path for " + codePath); 871475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey } 87294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 87394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGV("unlink %s\n", dex_path); 87494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (unlink(dex_path) < 0) { 875423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error(StringPrintf("Failed to unlink %s", dex_path)); 87694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else { 877423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 87894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 87994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 88094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 8813dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkeystruct stats { 8823dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey int64_t codeSize; 8833dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey int64_t dataSize; 8843dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey int64_t cacheSize; 8853dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey}; 8863dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 887df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 888df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkeystatic std::string toString(std::vector<int64_t> values) { 889df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey std::stringstream res; 890df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey res << "["; 891df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey for (size_t i = 0; i < values.size(); i++) { 892df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey res << values[i]; 893df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (i < values.size() - 1) { 894df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey res << ","; 895df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 896df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 897df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey res << "]"; 898df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey return res.str(); 899df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey} 900df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 9013dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 902df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkeystatic void collectQuotaStats(const std::string& device, int32_t userId, 903df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int32_t appId, struct stats* stats, struct stats* extStats ATTRIBUTE_UNUSED) { 904df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (device.empty()) return; 905df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 906df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey struct dqblk dq; 9073dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 9083dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey uid_t uid = multiuser_get_uid(userId, appId); 9093dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (quotactl(QCMD(Q_GETQUOTA, USRQUOTA), device.c_str(), uid, 9103dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey reinterpret_cast<char*>(&dq)) != 0) { 9113dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (errno != ESRCH) { 9123dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey PLOG(ERROR) << "Failed to quotactl " << device << " for UID " << uid; 9133dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 9143dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } else { 915df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 916df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey LOG(DEBUG) << "quotactl() for UID " << uid << " " << dq.dqb_curspace; 917df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 9183dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey stats->dataSize += dq.dqb_curspace; 9193dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 9203dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 9213dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey int cacheGid = multiuser_get_cache_gid(userId, appId); 9223dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (cacheGid != -1) { 9233dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), cacheGid, 9243dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey reinterpret_cast<char*>(&dq)) != 0) { 9253dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (errno != ESRCH) { 9263dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey PLOG(ERROR) << "Failed to quotactl " << device << " for GID " << cacheGid; 9273dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 9283dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } else { 929df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 930df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey LOG(DEBUG) << "quotactl() for GID " << cacheGid << " " << dq.dqb_curspace; 931df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 9323dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey stats->cacheSize += dq.dqb_curspace; 9333dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 9343dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 9353dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 9363dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey int sharedGid = multiuser_get_shared_app_gid(uid); 9373dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (sharedGid != -1) { 9383dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), sharedGid, 9393dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey reinterpret_cast<char*>(&dq)) != 0) { 9403dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (errno != ESRCH) { 9413dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey PLOG(ERROR) << "Failed to quotactl " << device << " for GID " << sharedGid; 9423dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 9433dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } else { 944df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 945df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey LOG(DEBUG) << "quotactl() for GID " << sharedGid << " " << dq.dqb_curspace; 946df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 9473dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey stats->codeSize += dq.dqb_curspace; 9483dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 9493dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 950df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 951df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_EXTERNAL 952df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // TODO: measure using external GIDs 953df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 954df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey} 955df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 956df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkeystatic void collectManualStats(const std::string& path, struct stats* stats) { 95794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood DIR *d; 95894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int dfd; 95994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood struct dirent *de; 96094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood struct stat s; 961d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey 9622f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey d = opendir(path.c_str()); 9632f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey if (d == nullptr) { 9643dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (errno != ENOENT) { 9653dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey PLOG(WARNING) << "Failed to open " << path; 9663dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 9672f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey return; 96894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 9692f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey dfd = dirfd(d); 9702f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey while ((de = readdir(d))) { 9712f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey const char *name = de->d_name; 972d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey 9733dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey int64_t size = 0; 9742f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) { 9753dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey size = s.st_blocks * 512; 97694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 97794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 9782f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey if (de->d_type == DT_DIR) { 9793dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (!strcmp(name, ".")) { 9803dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // Don't recurse, but still count node size 9813dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } else if (!strcmp(name, "..")) { 9823dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // Don't recurse or count node size 9833dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey continue; 9843dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } else { 9853dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // Measure all children nodes 9863dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey size = 0; 9873dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey calculate_tree_size(StringPrintf("%s/%s", path.c_str(), name), &size); 9882f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey } 9893dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 9902f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey if (!strcmp(name, "cache") || !strcmp(name, "code_cache")) { 9913dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey stats->cacheSize += size; 9922f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey } 99394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 9943dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 9953dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // Legacy symlink isn't owned by app 9963dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (de->d_type == DT_LNK && !strcmp(name, "lib")) { 9973dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey continue; 9983dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 9993dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 10003dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // Everything found inside is considered data 10013dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey stats->dataSize += size; 100294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 10032f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey closedir(d); 10042f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey} 100594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 1006df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkeystatic void collectManualStatsForUser(const std::string& path, struct stats* stats, 1007df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey bool exclude_apps = false) { 1008df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey DIR *d; 1009df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int dfd; 1010df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey struct dirent *de; 1011df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey struct stat s; 1012df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1013df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey d = opendir(path.c_str()); 1014df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (d == nullptr) { 1015df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (errno != ENOENT) { 1016df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey PLOG(WARNING) << "Failed to open " << path; 1017df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1018df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey return; 1019df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1020df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey dfd = dirfd(d); 1021df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey while ((de = readdir(d))) { 1022df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (de->d_type == DT_DIR) { 1023df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey const char *name = de->d_name; 1024df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) != 0) { 1025df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey continue; 1026df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1027df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (!strcmp(name, ".") || !strcmp(name, "..")) { 1028df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey continue; 1029df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } else if (exclude_apps && (s.st_uid >= AID_APP_START && s.st_uid <= AID_APP_END)) { 1030df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey continue; 1031df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } else { 1032df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey collectManualStats(StringPrintf("%s/%s", path.c_str(), name), stats); 1033df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1034df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1035df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1036df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey closedir(d); 1037df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey} 1038df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 10396c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkeybinder::Status InstalldNativeService::getAppSize(const std::unique_ptr<std::string>& uuid, 1040df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey const std::vector<std::string>& packageNames, int32_t userId, int32_t flags, 1041df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int32_t appId, const std::vector<int64_t>& ceDataInodes, 1042df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey const std::vector<std::string>& codePaths, std::vector<int64_t>* _aidl_return) { 10436c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 1044423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 1045df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey for (auto packageName : packageNames) { 1046df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 1047df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 10487a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 1049423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 1050df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // When modifying this logic, always verify using tests: 1051df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // runtest -x frameworks/base/services/tests/servicestests/src/com/android/server/pm/InstallerTest.java -m testGetAppSize 1052df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1053df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 1054df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey LOG(INFO) << "Measuring user " << userId << " app " << appId; 1055df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 10566c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey 10573dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // Here's a summary of the common storage locations across the platform, 10583dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // and how they're each tagged: 10593dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // 10603dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // /data/app/com.example UID system 10613dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // /data/app/com.example/oat UID system 1062df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // /data/user/0/com.example UID u0_a10 GID u0_a10 1063df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // /data/user/0/com.example/cache UID u0_a10 GID u0_a10_cache 1064df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // /data/media/0/foo.txt UID u0_media_rw 1065df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // /data/media/0/bar.jpg UID u0_media_rw GID u0_media_image 1066df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // /data/media/0/Android/data/com.example UID u0_media_rw GID u0_a10_ext 1067df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // /data/media/0/Android/data/com.example/cache UID u0_media_rw GID u0_a10_ext_cache 1068df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // /data/media/obb/com.example UID system 10693dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 10703dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey struct stats stats; 1071df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey struct stats extStats; 10723dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey memset(&stats, 0, sizeof(stats)); 1073df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey memset(&extStats, 0, sizeof(extStats)); 10743dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 1075df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 10763dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 107766b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey auto device = findQuotaDeviceForUuid(uuid); 107866b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey if (device.empty()) { 107966b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey flags &= ~FLAG_USE_QUOTA; 108066b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey } 108166b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey 1082466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_BEGIN("obb"); 1083df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey for (auto packageName : packageNames) { 1084df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto obbCodePath = create_data_media_obb_path(uuid_, packageName.c_str()); 1085df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(obbCodePath, &extStats.codeSize); 1086df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1087466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_END(); 10883dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 1089df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (flags & FLAG_USE_QUOTA && appId >= AID_APP_START) { 1090466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_BEGIN("code"); 1091df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey for (auto codePath : codePaths) { 1092df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(codePath, &stats.codeSize, -1, 1093df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey multiuser_get_shared_gid(userId, appId)); 10943dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 1095466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_END(); 1096df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1097466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_BEGIN("quota"); 109866b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey collectQuotaStats(device, userId, appId, &stats, &extStats); 1099466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_END(); 1100df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 11013dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } else { 1102466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_BEGIN("code"); 1103df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey for (auto codePath : codePaths) { 1104df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(codePath, &stats.codeSize); 1105df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1106466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_END(); 110794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 1108df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey for (size_t i = 0; i < packageNames.size(); i++) { 1109df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey const char* pkgname = packageNames[i].c_str(); 111094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 1111466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_BEGIN("data"); 1112df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto cePath = create_data_user_ce_package_path(uuid_, userId, pkgname, ceDataInodes[i]); 1113df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey collectManualStats(cePath, &stats); 1114df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto dePath = create_data_user_de_package_path(uuid_, userId, pkgname); 1115df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey collectManualStats(dePath, &stats); 1116466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_END(); 11173dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 1118466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_BEGIN("profiles"); 1119df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto userProfilePath = create_data_user_profile_package_path(userId, pkgname); 1120df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(userProfilePath, &stats.dataSize); 1121df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto refProfilePath = create_data_ref_profile_package_path(pkgname); 1122df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(refProfilePath, &stats.codeSize); 1123466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_END(); 1124df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1125df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_EXTERNAL 1126466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_BEGIN("external"); 1127df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto extPath = create_data_media_package_path(uuid_, userId, pkgname, "data"); 1128df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey collectManualStats(extPath, &extStats); 1129df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto mediaPath = create_data_media_package_path(uuid_, userId, pkgname, "media"); 1130df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(mediaPath, &extStats.dataSize); 1131466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_END(); 1132df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 1133df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1134df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1135466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_BEGIN("dalvik"); 1136df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int32_t sharedGid = multiuser_get_shared_gid(userId, appId); 1137df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (sharedGid != -1) { 1138df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(create_data_dalvik_cache_path(), &stats.codeSize, 1139df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey sharedGid, -1); 1140df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 11413dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey calculate_tree_size(create_data_misc_foreign_dex_path(userId), &stats.dataSize, 1142df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey multiuser_get_uid(userId, appId), -1); 1143466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_END(); 1144df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1145df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1146df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey std::vector<int64_t> ret; 1147df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(stats.codeSize); 1148df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(stats.dataSize); 1149df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(stats.cacheSize); 1150df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(extStats.codeSize); 1151df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(extStats.dataSize); 1152df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(extStats.cacheSize); 1153df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 1154df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey LOG(DEBUG) << "Final result " << toString(ret); 1155df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 1156df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey *_aidl_return = ret; 1157df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey return ok(); 1158df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey} 1159df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1160df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkeybinder::Status InstalldNativeService::getUserSize(const std::unique_ptr<std::string>& uuid, 1161df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int32_t userId, int32_t flags, const std::vector<int32_t>& appIds, 1162df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey std::vector<int64_t>* _aidl_return) { 1163df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 1164df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 11657a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 1166df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1167df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // When modifying this logic, always verify using tests: 1168df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // runtest -x frameworks/base/services/tests/servicestests/src/com/android/server/pm/InstallerTest.java -m testGetUserSize 1169df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1170df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 1171df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey LOG(INFO) << "Measuring user " << userId; 1172df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 1173df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1174df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey struct stats stats; 1175df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey struct stats extStats; 1176df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey memset(&stats, 0, sizeof(stats)); 1177df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey memset(&extStats, 0, sizeof(extStats)); 1178df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1179df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 1180df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1181466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_BEGIN("obb"); 1182df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto obbPath = create_data_path(uuid_) + "/media/obb"; 1183df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(obbPath, &extStats.codeSize); 1184466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_END(); 1185df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 118666b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey auto device = findQuotaDeviceForUuid(uuid); 118766b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey if (device.empty()) { 118866b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey flags &= ~FLAG_USE_QUOTA; 118966b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey } 119066b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey 1191df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (flags & FLAG_USE_QUOTA) { 1192466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_BEGIN("code"); 1193df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(create_data_app_path(uuid_), &stats.codeSize, -1, -1, true); 1194466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_END(); 1195df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1196466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_BEGIN("data"); 1197df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto cePath = create_data_user_ce_path(uuid_, userId); 1198df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey collectManualStatsForUser(cePath, &stats, true); 1199df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto dePath = create_data_user_de_path(uuid_, userId); 1200df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey collectManualStatsForUser(dePath, &stats, true); 1201466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_END(); 1202df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1203466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_BEGIN("profile"); 1204df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto userProfilePath = create_data_user_profile_path(userId); 1205df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(userProfilePath, &stats.dataSize, -1, -1, true); 1206df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto refProfilePath = create_data_ref_profile_path(); 1207df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(refProfilePath, &stats.codeSize, -1, -1, true); 1208466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_END(); 12093dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 12103dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey#if MEASURE_EXTERNAL 1211466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_BEGIN("external"); 1212df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // TODO: measure external storage paths 1213466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_END(); 1214df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 12153dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 1216466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_BEGIN("dalvik"); 1217df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(create_data_dalvik_cache_path(), &stats.codeSize, 1218df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey -1, -1, true); 1219df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(create_data_misc_foreign_dex_path(userId), &stats.dataSize, 1220df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey -1, -1, true); 1221466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_END(); 1222df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1223466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_BEGIN("quota"); 1224df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey for (auto appId : appIds) { 1225df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (appId >= AID_APP_START) { 1226df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey collectQuotaStats(device, userId, appId, &stats, &extStats); 1227df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 1228df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // Sleep to make sure we don't lose logs 1229df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey usleep(1); 1230df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 1231df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1232df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1233466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_END(); 1234df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } else { 1235466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_BEGIN("code"); 1236df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(create_data_app_path(uuid_), &stats.codeSize); 1237466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_END(); 1238df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1239466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_BEGIN("data"); 1240df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto cePath = create_data_user_ce_path(uuid_, userId); 1241df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey collectManualStatsForUser(cePath, &stats); 1242df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto dePath = create_data_user_de_path(uuid_, userId); 1243df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey collectManualStatsForUser(dePath, &stats); 1244466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_END(); 1245df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1246466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_BEGIN("profile"); 1247df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto userProfilePath = create_data_user_profile_path(userId); 1248df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(userProfilePath, &stats.dataSize); 1249df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto refProfilePath = create_data_ref_profile_path(); 1250df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(refProfilePath, &stats.codeSize); 1251466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_END(); 1252df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1253df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_EXTERNAL 1254466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_BEGIN("external"); 1255df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // TODO: measure external storage paths 1256466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_END(); 12573dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey#endif 1258df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1259466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_BEGIN("dalvik"); 1260df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(create_data_dalvik_cache_path(), &stats.codeSize); 1261df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(create_data_misc_foreign_dex_path(userId), &stats.dataSize); 1262466459b68da52edf20962c3c80e2fe0325569c18Jeff Sharkey ATRACE_END(); 12632f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey } 126494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 12653dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey std::vector<int64_t> ret; 12663dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey ret.push_back(stats.codeSize); 12673dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey ret.push_back(stats.dataSize); 12683dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey ret.push_back(stats.cacheSize); 1269df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(extStats.codeSize); 1270df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(extStats.dataSize); 1271df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(extStats.cacheSize); 1272df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 1273df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey LOG(DEBUG) << "Final result " << toString(ret); 1274df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 1275df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey *_aidl_return = ret; 1276df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey return ok(); 1277df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey} 1278df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1279df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkeybinder::Status InstalldNativeService::getExternalSize(const std::unique_ptr<std::string>& uuid, 1280df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int32_t userId, int32_t flags, std::vector<int64_t>* _aidl_return) { 1281df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 1282df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 12837a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 1284df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1285df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // When modifying this logic, always verify using tests: 1286df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // runtest -x frameworks/base/services/tests/servicestests/src/com/android/server/pm/InstallerTest.java -m testGetExternalSize 1287df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1288df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 1289df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey LOG(INFO) << "Measuring external " << userId; 1290df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 1291df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1292df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 1293df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1294df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int64_t totalSize = 0; 1295df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int64_t audioSize = 0; 1296df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int64_t videoSize = 0; 1297df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int64_t imageSize = 0; 1298df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 129966b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey auto device = findQuotaDeviceForUuid(uuid); 130066b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey if (device.empty()) { 130166b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey flags &= ~FLAG_USE_QUOTA; 130266b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey } 130366b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey 1304df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (flags & FLAG_USE_QUOTA) { 1305df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey struct dqblk dq; 1306df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1307df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey uid_t uid = multiuser_get_uid(userId, AID_MEDIA_RW); 1308df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (quotactl(QCMD(Q_GETQUOTA, USRQUOTA), device.c_str(), uid, 1309df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey reinterpret_cast<char*>(&dq)) != 0) { 1310df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (errno != ESRCH) { 1311df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey PLOG(ERROR) << "Failed to quotactl " << device << " for UID " << uid; 1312df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1313df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } else { 1314df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 1315df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey LOG(DEBUG) << "quotactl() for UID " << uid << " " << dq.dqb_curspace; 1316df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 1317df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey totalSize = dq.dqb_curspace; 1318df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1319df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1320df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey gid_t audioGid = multiuser_get_uid(userId, AID_MEDIA_AUDIO); 1321df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), audioGid, 1322df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey reinterpret_cast<char*>(&dq)) == 0) { 1323df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 1324df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey LOG(DEBUG) << "quotactl() for GID " << audioGid << " " << dq.dqb_curspace; 1325df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 1326df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey audioSize = dq.dqb_curspace; 1327df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1328df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey gid_t videoGid = multiuser_get_uid(userId, AID_MEDIA_VIDEO); 1329df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), videoGid, 1330df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey reinterpret_cast<char*>(&dq)) == 0) { 1331df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 1332df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey LOG(DEBUG) << "quotactl() for GID " << videoGid << " " << dq.dqb_curspace; 1333df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 1334df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey videoSize = dq.dqb_curspace; 1335df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1336df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey gid_t imageGid = multiuser_get_uid(userId, AID_MEDIA_IMAGE); 1337df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), imageGid, 1338df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey reinterpret_cast<char*>(&dq)) == 0) { 1339df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 1340df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey LOG(DEBUG) << "quotactl() for GID " << imageGid << " " << dq.dqb_curspace; 1341df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 1342df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey imageSize = dq.dqb_curspace; 1343df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1344df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } else { 1345df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey FTS *fts; 1346df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey FTSENT *p; 1347df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto path = create_data_media_path(uuid_, userId); 1348df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey char *argv[] = { (char*) path.c_str(), nullptr }; 1349df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_XDEV, NULL))) { 1350df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey return error("Failed to fts_open " + path); 1351df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1352df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey while ((p = fts_read(fts)) != NULL) { 1353df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey char* ext; 1354df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int64_t size = (p->fts_statp->st_blocks * 512); 1355df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey switch (p->fts_info) { 1356df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey case FTS_F: 1357df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // Only categorize files not belonging to apps 1358df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (p->fts_statp->st_gid < AID_APP_START) { 1359df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ext = strrchr(p->fts_name, '.'); 1360df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (ext != nullptr) { 1361df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey switch (MatchExtension(++ext)) { 1362df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey case AID_MEDIA_AUDIO: audioSize += size; break; 1363df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey case AID_MEDIA_VIDEO: videoSize += size; break; 1364df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey case AID_MEDIA_IMAGE: imageSize += size; break; 1365df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1366df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1367df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1368df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // Fall through to always count against total 1369df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey case FTS_D: 1370df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey case FTS_DEFAULT: 1371df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey case FTS_SL: 1372df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey case FTS_SLNONE: 1373df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey totalSize += size; 1374df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey break; 1375df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1376df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1377df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey fts_close(fts); 1378df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1379df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1380df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey std::vector<int64_t> ret; 1381df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(totalSize); 1382df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(audioSize); 1383df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(videoSize); 1384df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(imageSize); 1385df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 1386df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey LOG(DEBUG) << "Final result " << toString(ret); 1387df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 13883dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey *_aidl_return = ret; 1389423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 13902f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey} 1391d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey 13926727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr// Dumps the contents of a profile file, using pkgname's dex files for pretty 13936727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr// printing the result. 1394475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::dumpProfiles(int32_t uid, const std::string& packageName, 1395475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const std::string& codePaths, bool* _aidl_return) { 1396475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 1397423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 13987a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 1399475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey 1400475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* pkgname = packageName.c_str(); 140190aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey const char* code_paths = codePaths.c_str(); 14026727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr 140390aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey *_aidl_return = dump_profiles(uid, pkgname, code_paths); 1404423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 14056727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr} 14066727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr 14074d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe// TODO: Consider returning error codes. 1408475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::mergeProfiles(int32_t uid, const std::string& packageName, 1409423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey bool* _aidl_return) { 1410475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 1411423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 14127a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 1413423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 1414475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* pkgname = packageName.c_str(); 1415475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey *_aidl_return = analyse_profiles(uid, pkgname); 1416423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 14174d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe} 14184d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe 14196c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkeybinder::Status InstalldNativeService::dexopt(const std::string& apkPath, int32_t uid, 14206c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey const std::unique_ptr<std::string>& packageName, const std::string& instructionSet, 14216c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey int32_t dexoptNeeded, const std::unique_ptr<std::string>& outputPath, int32_t dexFlags, 14226c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey const std::string& compilerFilter, const std::unique_ptr<std::string>& uuid, 14236c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey const std::unique_ptr<std::string>& sharedLibraries) { 14246c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 1425423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 1426423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (packageName && *packageName != "*") { 1427423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(*packageName); 1428423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 14297a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 14306c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey 14316c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey const char* apk_path = apkPath.c_str(); 14326c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey const char* pkgname = packageName ? packageName->c_str() : "*"; 14336c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey const char* instruction_set = instructionSet.c_str(); 14346c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey const char* oat_dir = outputPath ? outputPath->c_str() : nullptr; 14356c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey const char* compiler_filter = compilerFilter.c_str(); 14366c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey const char* volume_uuid = uuid ? uuid->c_str() : nullptr; 14376c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey const char* shared_libraries = sharedLibraries ? sharedLibraries->c_str() : nullptr; 14386c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey 14396c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey int res = android::installd::dexopt(apk_path, uid, pkgname, instruction_set, dexoptNeeded, 14406c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey oat_dir, dexFlags, compiler_filter, volume_uuid, shared_libraries); 1441423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return res ? error(res, "Failed to dexopt") : ok(); 14426c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey} 14436c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey 1444475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::markBootComplete(const std::string& instructionSet) { 1445475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 14467a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 14477a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey 1448475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* instruction_set = instructionSet.c_str(); 1449475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey 1450475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey char boot_marker_path[PKG_PATH_MAX]; 1451475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey sprintf(boot_marker_path, 145202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe "%s/%s/%s/.booting", 145302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe android_data_dir.path, 145402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe DALVIK_CACHE, 145502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe instruction_set); 1456091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath 1457475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ALOGV("mark_boot_complete : %s", boot_marker_path); 1458475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey if (unlink(boot_marker_path) != 0) { 1459423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error(StringPrintf("Failed to unlink %s", boot_marker_path)); 1460475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey } 1461423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 1462091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath} 1463091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath 146494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodvoid mkinnerdirs(char* path, int basepos, mode_t mode, int uid, int gid, 146594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood struct stat* statbuf) 146694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 146794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood while (path[basepos] != 0) { 146894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (path[basepos] == '/') { 146994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood path[basepos] = 0; 147094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (lstat(path, statbuf) < 0) { 147194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGV("Making directory: %s\n", path); 147294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (mkdir(path, mode) == 0) { 147394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood chown(path, uid, gid); 147494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else { 147594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGW("Unable to make directory %s: %s\n", path, strerror(errno)); 147694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 147794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 147894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood path[basepos] = '/'; 147994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood basepos++; 148094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 148194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood basepos++; 148294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 148394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 148494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 1485475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::linkNativeLibraryDirectory( 1486475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const std::unique_ptr<std::string>& uuid, const std::string& packageName, 1487475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const std::string& nativeLibPath32, int32_t userId) { 1488475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 1489423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 1490423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 14917a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 1492423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 1493475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 1494475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* pkgname = packageName.c_str(); 1495475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* asecLibDir = nativeLibPath32.c_str(); 149694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood struct stat s, libStat; 1497423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey binder::Status res = ok(); 149894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 1499423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey auto _pkgdir = create_data_user_ce_package_path(uuid_, userId, pkgname); 1500423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey auto _libsymlink = _pkgdir + PKG_LIB_POSTFIX; 1501c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey 1502c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey const char* pkgdir = _pkgdir.c_str(); 1503c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey const char* libsymlink = _libsymlink.c_str(); 150494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 1505475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey if (stat(pkgdir, &s) < 0) { 1506423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to stat " + _pkgdir); 1507475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey } 150894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 150994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (chown(pkgdir, AID_INSTALL, AID_INSTALL) < 0) { 1510423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to chown " + _pkgdir); 151194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 151294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 151394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (chmod(pkgdir, 0700) < 0) { 1514423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to chmod " + _pkgdir); 151594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto out; 151694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 151794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 151894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (lstat(libsymlink, &libStat) < 0) { 151994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (errno != ENOENT) { 1520423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to stat " + _libsymlink); 152194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto out; 152294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 152394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else { 152494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (S_ISDIR(libStat.st_mode)) { 15253aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath if (delete_dir_contents(libsymlink, 1, NULL) < 0) { 1526423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to delete " + _libsymlink); 152794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto out; 152894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 152994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else if (S_ISLNK(libStat.st_mode)) { 153094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (unlink(libsymlink) < 0) { 1531423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to unlink " + _libsymlink); 153294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto out; 153394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 153494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 153594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 153694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 153794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (symlink(asecLibDir, libsymlink) < 0) { 1538423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to symlink " + _libsymlink + " to " + nativeLibPath32); 153994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto out; 154094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 154194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 154294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodout: 154394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (chmod(pkgdir, s.st_mode) < 0) { 1544423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey auto msg = "Failed to cleanup chmod " + _pkgdir; 1545423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (res.isOk()) { 1546423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error(msg); 1547423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } else { 1548423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey PLOG(ERROR) << msg; 1549423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 155094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 155194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 155294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (chown(pkgdir, s.st_uid, s.st_gid) < 0) { 1553423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey auto msg = "Failed to cleanup chown " + _pkgdir; 1554423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (res.isOk()) { 1555423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error(msg); 1556423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } else { 1557423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey PLOG(ERROR) << msg; 1558423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 155994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 156094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 1561423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return res; 156294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 156363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 156463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstadstatic void run_idmap(const char *target_apk, const char *overlay_apk, int idmap_fd) 156563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad{ 156663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad static const char *IDMAP_BIN = "/system/bin/idmap"; 156763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad static const size_t MAX_INT_LEN = 32; 156863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad char idmap_str[MAX_INT_LEN]; 156963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 157063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad snprintf(idmap_str, sizeof(idmap_str), "%d", idmap_fd); 157163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 157263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad execl(IDMAP_BIN, IDMAP_BIN, "--fd", target_apk, overlay_apk, idmap_str, (char*)NULL); 157363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("execl(%s) failed: %s\n", IDMAP_BIN, strerror(errno)); 157463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad} 157563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 157663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad// Transform string /a/b/c.apk to (prefix)/a@b@c.apk@(suffix) 157763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad// eg /a/b/c.apk to /data/resource-cache/a@b@c.apk@idmap 157863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstadstatic int flatten_path(const char *prefix, const char *suffix, 157963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad const char *overlay_path, char *idmap_path, size_t N) 158063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad{ 158163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (overlay_path == NULL || idmap_path == NULL) { 158263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad return -1; 158363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 158463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad const size_t len_overlay_path = strlen(overlay_path); 158563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad // will access overlay_path + 1 further below; requires absolute path 158663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (len_overlay_path < 2 || *overlay_path != '/') { 158763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad return -1; 158863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 158963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad const size_t len_idmap_root = strlen(prefix); 159063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad const size_t len_suffix = strlen(suffix); 159163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (SIZE_MAX - len_idmap_root < len_overlay_path || 159263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad SIZE_MAX - (len_idmap_root + len_overlay_path) < len_suffix) { 159363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad // additions below would cause overflow 159463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad return -1; 159563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 159663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (N < len_idmap_root + len_overlay_path + len_suffix) { 159763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad return -1; 159863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 159963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad memset(idmap_path, 0, N); 160063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad snprintf(idmap_path, N, "%s%s%s", prefix, overlay_path + 1, suffix); 160163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad char *ch = idmap_path + len_idmap_root; 160263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad while (*ch != '\0') { 160363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (*ch == '/') { 160463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad *ch = '@'; 160563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 160663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ++ch; 160763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 160863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad return 0; 160963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad} 161063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 1611475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::idmap(const std::string& targetApkPath, 1612475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const std::string& overlayApkPath, int32_t uid) { 1613475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 16147a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 16157a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey 1616475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* target_apk = targetApkPath.c_str(); 1617475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* overlay_apk = overlayApkPath.c_str(); 161863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGV("idmap target_apk=%s overlay_apk=%s uid=%d\n", target_apk, overlay_apk, uid); 161963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 162063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad int idmap_fd = -1; 162163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad char idmap_path[PATH_MAX]; 162263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 162363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (flatten_path(IDMAP_PREFIX, IDMAP_SUFFIX, overlay_apk, 162463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad idmap_path, sizeof(idmap_path)) == -1) { 162563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("idmap cannot generate idmap path for overlay %s\n", overlay_apk); 162663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad goto fail; 162763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 162863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 162963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad unlink(idmap_path); 163063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad idmap_fd = open(idmap_path, O_RDWR | O_CREAT | O_EXCL, 0644); 163163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (idmap_fd < 0) { 163263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("idmap cannot open '%s' for output: %s\n", idmap_path, strerror(errno)); 163363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad goto fail; 163463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 163563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (fchown(idmap_fd, AID_SYSTEM, uid) < 0) { 163663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("idmap cannot chown '%s'\n", idmap_path); 163763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad goto fail; 163863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 163963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (fchmod(idmap_fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0) { 164063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("idmap cannot chmod '%s'\n", idmap_path); 164163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad goto fail; 164263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 164363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 164463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad pid_t pid; 164563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad pid = fork(); 164663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (pid == 0) { 164763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad /* child -- drop privileges before continuing */ 164863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (setgid(uid) != 0) { 164963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("setgid(%d) failed during idmap\n", uid); 165063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad exit(1); 165163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 165263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (setuid(uid) != 0) { 165363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("setuid(%d) failed during idmap\n", uid); 165463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad exit(1); 165563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 165663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (flock(idmap_fd, LOCK_EX | LOCK_NB) != 0) { 165763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("flock(%s) failed during idmap: %s\n", idmap_path, strerror(errno)); 165863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad exit(1); 165963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 166063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 166163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad run_idmap(target_apk, overlay_apk, idmap_fd); 166263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad exit(1); /* only if exec call to idmap failed */ 166363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } else { 166463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad int status = wait_child(pid); 166563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (status != 0) { 166663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("idmap failed, status=0x%04x\n", status); 166763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad goto fail; 166863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 166963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 167063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 167163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad close(idmap_fd); 1672423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 167363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstadfail: 167463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (idmap_fd >= 0) { 167563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad close(idmap_fd); 167663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad unlink(idmap_path); 167763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 1678423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error(); 167963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad} 1680e9887e46cea4a095e4219927eadbe4c57bb1a5eeRobert Craig 1681c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkeybinder::Status InstalldNativeService::restoreconAppData(const std::unique_ptr<std::string>& uuid, 1682c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const std::string& packageName, int32_t userId, int32_t flags, int32_t appId, 1683c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const std::string& seInfo) { 1684c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 1685423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 1686423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 16877a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 1688423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 1689423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey binder::Status res = ok(); 1690e9887e46cea4a095e4219927eadbe4c57bb1a5eeRobert Craig 1691da30dc7336f03ca629fe173db1425fdce989119cRobert Craig // SELINUX_ANDROID_RESTORECON_DATADATA flag is set by libselinux. Not needed here. 1692c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey unsigned int seflags = SELINUX_ANDROID_RESTORECON_RECURSE; 1693c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 1694c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* pkgName = packageName.c_str(); 1695c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* seinfo = seInfo.c_str(); 1696e9887e46cea4a095e4219927eadbe4c57bb1a5eeRobert Craig 1697c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey uid_t uid = multiuser_get_uid(userId, appId); 1698aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (flags & FLAG_STORAGE_CE) { 1699c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey auto path = create_data_user_ce_package_path(uuid_, userId, pkgName); 1700c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey if (selinux_android_restorecon_pkgdir(path.c_str(), seinfo, uid, seflags) < 0) { 1701423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("restorecon failed for " + path); 1702da30dc7336f03ca629fe173db1425fdce989119cRobert Craig } 1703c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey } 1704aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (flags & FLAG_STORAGE_DE) { 1705c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey auto path = create_data_user_de_package_path(uuid_, userId, pkgName); 1706c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey if (selinux_android_restorecon_pkgdir(path.c_str(), seinfo, uid, seflags) < 0) { 1707423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("restorecon failed for " + path); 1708da30dc7336f03ca629fe173db1425fdce989119cRobert Craig } 1709da30dc7336f03ca629fe173db1425fdce989119cRobert Craig } 1710423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return res; 1711e9887e46cea4a095e4219927eadbe4c57bb1a5eeRobert Craig} 17123aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath 1713475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::createOatDir(const std::string& oatDir, 1714475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const std::string& instructionSet) { 1715475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 17167a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 17177a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey 1718475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* oat_dir = oatDir.c_str(); 1719475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* instruction_set = instructionSet.c_str(); 172088ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov char oat_instr_dir[PKG_PATH_MAX]; 172188ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov 172288ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov if (validate_apk_path(oat_dir)) { 1723423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Invalid path " + oatDir); 172488ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov } 17258eed7e6a9bb527af7380ec13b390c6e2955eba6aFyodor Kupolov if (fs_prepare_dir(oat_dir, S_IRWXU | S_IRWXG | S_IXOTH, AID_SYSTEM, AID_INSTALL)) { 1726423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to prepare " + oatDir); 172788ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov } 172888ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov if (selinux_android_restorecon(oat_dir, 0)) { 1729423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to restorecon " + oatDir); 173088ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov } 173188ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov snprintf(oat_instr_dir, PKG_PATH_MAX, "%s/%s", oat_dir, instruction_set); 17328eed7e6a9bb527af7380ec13b390c6e2955eba6aFyodor Kupolov if (fs_prepare_dir(oat_instr_dir, S_IRWXU | S_IRWXG | S_IXOTH, AID_SYSTEM, AID_INSTALL)) { 1733423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error(StringPrintf("Failed to prepare %s", oat_instr_dir)); 173488ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov } 1735423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 173688ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov} 173788ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov 1738475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::rmPackageDir(const std::string& packageDir) { 1739475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 17407a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 17417a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey 1742423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (validate_apk_path(packageDir.c_str())) { 1743423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Invalid path " + packageDir); 1744423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 1745423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (delete_dir_contents_and_dir(packageDir) != 0) { 1746423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to delete " + packageDir); 174788ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov } 1748423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 174988ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov} 175088ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov 1751475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::linkFile(const std::string& relativePath, 1752475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const std::string& fromBase, const std::string& toBase) { 1753475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 17547a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 17557a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey 1756475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* relative_path = relativePath.c_str(); 1757475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* from_base = fromBase.c_str(); 1758475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* to_base = toBase.c_str(); 1759d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath char from_path[PKG_PATH_MAX]; 1760d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath char to_path[PKG_PATH_MAX]; 1761d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath snprintf(from_path, PKG_PATH_MAX, "%s/%s", from_base, relative_path); 1762d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath snprintf(to_path, PKG_PATH_MAX, "%s/%s", to_base, relative_path); 1763d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath 1764d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath if (validate_apk_path_subdirs(from_path)) { 1765423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error(StringPrintf("Invalid from path %s", from_path)); 1766d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath } 1767d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath 1768d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath if (validate_apk_path_subdirs(to_path)) { 1769423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error(StringPrintf("Invalid to path %s", to_path)); 1770d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath } 1771d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath 1772423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (link(from_path, to_path) < 0) { 1773423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error(StringPrintf("Failed to link from %s to %s", from_path, to_path)); 1774d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath } 1775d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath 1776423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 1777d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath} 1778d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath 1779475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::moveAb(const std::string& apkPath, 1780475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const std::string& instructionSet, const std::string& outputPath) { 1781475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 17827a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 178390aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey 1784475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* apk_path = apkPath.c_str(); 1785475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* instruction_set = instructionSet.c_str(); 1786475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* oat_dir = outputPath.c_str(); 1787d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe 178890aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey bool success = move_ab(apk_path, instruction_set, oat_dir); 1789423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return success ? ok() : error(); 1790e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe} 1791e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe 1792475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::deleteOdex(const std::string& apkPath, 1793475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const std::string& instructionSet, const std::string& outputPath) { 1794475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 17957a9059e8ed297a8e025c843b632c7c8571682b46Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 179690aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey 1797475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* apk_path = apkPath.c_str(); 1798475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* instruction_set = instructionSet.c_str(); 1799475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* oat_dir = outputPath.c_str(); 1800475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey 180190aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey bool res = delete_odex(apk_path, instruction_set, oat_dir); 180290aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey return res ? ok() : error(); 1803b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampe} 1804b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampe 180566b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkeybinder::Status InstalldNativeService::invalidateMounts() { 180666b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 180766b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey std::lock_guard<std::recursive_mutex> lock(mLock); 180866b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey 180966b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey mQuotaDevices.clear(); 181066b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey 181166b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey std::ifstream in("/proc/mounts"); 181266b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey if (!in.is_open()) { 181366b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey return error("Failed to read mounts"); 181466b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey } 181566b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey 181666b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey std::string source; 181766b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey std::string target; 181866b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey std::string ignored; 181966b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey struct dqblk dq; 182066b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey while (!in.eof()) { 182166b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey std::getline(in, source, ' '); 182266b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey std::getline(in, target, ' '); 182366b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey std::getline(in, ignored); 182466b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey 182566b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey if (source.compare(0, 11, "/dev/block/") == 0) { 182666b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey if (quotactl(QCMD(Q_GETQUOTA, USRQUOTA), source.c_str(), 0, 182766b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey reinterpret_cast<char*>(&dq)) == 0) { 182866b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey LOG(DEBUG) << "Found " << source << " with quota"; 182966b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey mQuotaDevices[target] = source; 183066b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey } 183166b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey } 183266b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey } 183366b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey return ok(); 183466b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey} 183566b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey 183666b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkeystd::string InstalldNativeService::findQuotaDeviceForUuid( 183766b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey const std::unique_ptr<std::string>& uuid) { 183866b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey auto path = create_data_path(uuid ? uuid->c_str() : nullptr); 183966b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey return mQuotaDevices[path]; 184066b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey} 184166b1a12e7a120f85042669bb58f1db864616b506Jeff Sharkey 184202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe} // namespace installd 184302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe} // namespace android 1844