InstalldNativeService.cpp revision 24ef15bd009204b73ebf0e94ba3d868177a5f44c
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 1902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <errno.h> 2002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <inttypes.h> 213dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey#include <fstream> 223dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey#include <fts.h> 23d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe#include <regex> 2402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <stdlib.h> 253dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey#include <string.h> 2602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <sys/capability.h> 2702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <sys/file.h> 2802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <sys/resource.h> 293dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey#include <sys/quota.h> 3002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <sys/stat.h> 313dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey#include <sys/statvfs.h> 32cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey#include <sys/types.h> 336a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle#include <sys/wait.h> 34cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey#include <sys/xattr.h> 3502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <unistd.h> 3641ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey 376fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe#include <android-base/logging.h> 38e4ec9eb7b4c452493589983970ba5ccc501728d1Elliott Hughes#include <android-base/stringprintf.h> 396727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr#include <android-base/strings.h> 4064b59cc1c89225813295d1b5d6bf4183615da204Roland Levillain#include <android-base/unique_fd.h> 4102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <cutils/fs.h> 4290aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey#include <cutils/properties.h> 430378aaf257aee92539d30543914a50c4481c6a18Brian Carlstrom#include <cutils/sched_policy.h> 447823e124e00576e20e47ec717cbe8bc89f0f2bf2Mark Salyzyn#include <log/log.h> // TODO: Move everything to base/logging. 45e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey#include <logwrap/logwrap.h> 4602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <private/android_filesystem_config.h> 47e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey#include <selinux/android.h> 4802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <system/thread_defs.h> 49e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 506c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey#include "dexopt.h" 51f3e30b936ef103dc0f3d8697e0f86ba82b49609eJeff Sharkey#include "globals.h" 52f3e30b936ef103dc0f3d8697e0f86ba82b49609eJeff Sharkey#include "installd_deps.h" 53f3e30b936ef103dc0f3d8697e0f86ba82b49609eJeff Sharkey#include "otapreopt_utils.h" 54f3e30b936ef103dc0f3d8697e0f86ba82b49609eJeff Sharkey#include "utils.h" 556c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey 56df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#include "MatchExtensionGen.h" 57df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 5802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#ifndef LOG_TAG 5902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#define LOG_TAG "installd" 6002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#endif 6141ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey 6241ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkeyusing android::base::StringPrintf; 6394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 6402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampenamespace android { 6502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampenamespace installd { 6694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 67cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkeystatic constexpr const char* kCpPath = "/system/bin/cp"; 68cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkeystatic constexpr const char* kXattrDefault = "user.default"; 69e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 70d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampestatic constexpr const char* PKG_LIB_POSTFIX = "/lib"; 71d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampestatic constexpr const char* CACHE_DIR_POSTFIX = "/cache"; 72d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampestatic constexpr const char* CODE_CACHE_DIR_POSTFIX = "/code_cache"; 73d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe 74d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampestatic constexpr const char* IDMAP_PREFIX = "/data/resource-cache/"; 75d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampestatic constexpr const char* IDMAP_SUFFIX = "@idmap"; 76d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe 77d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe// NOTE: keep in sync with StorageManager 78d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampestatic constexpr int FLAG_STORAGE_DE = 1 << 0; 79d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampestatic constexpr int FLAG_STORAGE_CE = 1 << 1; 80d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe 81d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe// NOTE: keep in sync with Installer 82d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampestatic constexpr int FLAG_CLEAR_CACHE_ONLY = 1 << 8; 83d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampestatic constexpr int FLAG_CLEAR_CODE_CACHE_ONLY = 1 << 9; 843dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkeystatic constexpr int FLAG_USE_QUOTA = 1 << 12; 85d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe 86eecb2d205366351af42fd0cd9e1a95de3980764eJanis Danisevskis#define MIN_RESTRICTED_HOME_SDK_VERSION 24 // > M 870274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeynamespace { 880274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 890274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeyconstexpr const char* kDump = "android.permission.DUMP"; 900274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 91423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkeystatic binder::Status ok() { 92423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return binder::Status::ok(); 93423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey} 94423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 95423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkeystatic binder::Status exception(uint32_t code, const std::string& msg) { 96423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return binder::Status::fromExceptionCode(code, String8(msg.c_str())); 97423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey} 98423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 99423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkeystatic binder::Status error() { 100423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return binder::Status::fromServiceSpecificError(errno); 101423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey} 102423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 103423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkeystatic binder::Status error(const std::string& msg) { 104423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey PLOG(ERROR) << msg; 105423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return binder::Status::fromServiceSpecificError(errno, String8(msg.c_str())); 106423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey} 107423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 108423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkeystatic binder::Status error(uint32_t code, const std::string& msg) { 109423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey LOG(ERROR) << msg << " (" << code << ")"; 110423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return binder::Status::fromServiceSpecificError(code, String8(msg.c_str())); 111423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey} 112423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 1130274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeybinder::Status checkPermission(const char* permission) { 1140274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey pid_t pid; 1150274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey uid_t uid; 1160274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 1170274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey if (checkCallingPermission(String16(permission), reinterpret_cast<int32_t*>(&pid), 1180274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey reinterpret_cast<int32_t*>(&uid))) { 119423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 1200274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey } else { 121423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return exception(binder::Status::EX_SECURITY, 122423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey StringPrintf("UID %d / PID %d lacks permission %s", uid, pid, permission)); 1230274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey } 1240274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey} 1250274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 1260274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeybinder::Status checkUid(uid_t expectedUid) { 1270274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey uid_t uid = IPCThreadState::self()->getCallingUid(); 1280274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey if (uid == expectedUid || uid == AID_ROOT) { 129423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 1300274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey } else { 131423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return exception(binder::Status::EX_SECURITY, 132423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey StringPrintf("UID %d is not expected UID %d", uid, expectedUid)); 133423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 134423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey} 135423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 136423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkeybinder::Status checkArgumentUuid(const std::unique_ptr<std::string>& uuid) { 137423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (!uuid || is_valid_filename(*uuid)) { 138423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 139423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } else { 140423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return exception(binder::Status::EX_ILLEGAL_ARGUMENT, 141423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey StringPrintf("UUID %s is malformed", uuid->c_str())); 142423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 143423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey} 144423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 145423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkeybinder::Status checkArgumentPackageName(const std::string& packageName) { 146423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (is_valid_package_name(packageName.c_str())) { 147423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 148423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } else { 149423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return exception(binder::Status::EX_ILLEGAL_ARGUMENT, 150423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey StringPrintf("Package name %s is malformed", packageName.c_str())); 1510274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey } 1520274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey} 1530274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 1540274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey#define ENFORCE_UID(uid) { \ 1550274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey binder::Status status = checkUid((uid)); \ 1560274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey if (!status.isOk()) { \ 1570274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey return status; \ 1580274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey } \ 1590274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey} 1600274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 161423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey#define CHECK_ARGUMENT_UUID(uuid) { \ 162423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey binder::Status status = checkArgumentUuid((uuid)); \ 163423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (!status.isOk()) { \ 164423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return status; \ 165423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } \ 166423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey} 167423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 168423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey#define CHECK_ARGUMENT_PACKAGE_NAME(packageName) { \ 169423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey binder::Status status = \ 170423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey checkArgumentPackageName((packageName)); \ 171423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (!status.isOk()) { \ 172423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return status; \ 173423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } \ 174423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey} 175423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 1760274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey} // namespace 1770274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 1780274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeystatus_t InstalldNativeService::start() { 1790274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey IPCThreadState::self()->disableBackgroundScheduling(true); 1800274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey status_t ret = BinderService<InstalldNativeService>::publish(); 1810274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey if (ret != android::OK) { 1820274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey return ret; 1830274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey } 1840274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey sp<ProcessState> ps(ProcessState::self()); 1850274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey ps->startThreadPool(); 1860274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey ps->giveThreadPoolName(); 1870274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey return android::OK; 1880274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey} 1890274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 1900274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeystatus_t InstalldNativeService::dump(int fd, const Vector<String16> & /* args */) { 1910274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const binder::Status dump_permission = checkPermission(kDump); 1920274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey if (!dump_permission.isOk()) { 1930274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const String8 msg(dump_permission.toString8()); 1940274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey write(fd, msg.string(), msg.size()); 1950274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey return PERMISSION_DENIED; 1960274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey } 1970274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 1980274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey std::string msg = "installd is happy\n"; 1990274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey write(fd, msg.c_str(), strlen(msg.c_str())); 2000274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey return NO_ERROR; 2010274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey} 2020274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 2037db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey/** 2047db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey * Perform restorecon of the given path, but only perform recursive restorecon 2057db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey * if the label of that top-level file actually changed. This can save us 2067db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey * significant time by avoiding no-op traversals of large filesystem trees. 2077db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey */ 20835b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkeystatic int restorecon_app_data_lazy(const std::string& path, const std::string& seInfo, uid_t uid, 20935b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey bool existing) { 2107db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey int res = 0; 2117db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey char* before = nullptr; 2127db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey char* after = nullptr; 2137db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey 2147db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey // Note that SELINUX_ANDROID_RESTORECON_DATADATA flag is set by 2157db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey // libselinux. Not needed here. 2167db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey 21795d9ac642f082870fd4d2419f534171a9b82bbcdJeff Sharkey if (lgetfilecon(path.c_str(), &before) < 0) { 2187db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey PLOG(ERROR) << "Failed before getfilecon for " << path; 2197db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey goto fail; 2207db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey } 2210274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey if (selinux_android_restorecon_pkgdir(path.c_str(), seInfo.c_str(), uid, 0) < 0) { 2227db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey PLOG(ERROR) << "Failed top-level restorecon for " << path; 2237db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey goto fail; 2247db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey } 22595d9ac642f082870fd4d2419f534171a9b82bbcdJeff Sharkey if (lgetfilecon(path.c_str(), &after) < 0) { 2267db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey PLOG(ERROR) << "Failed after getfilecon for " << path; 2277db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey goto fail; 2287db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey } 2297db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey 2307db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey // If the initial top-level restorecon above changed the label, then go 2317db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey // back and restorecon everything recursively 2327db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey if (strcmp(before, after)) { 23335b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey if (existing) { 23435b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey LOG(DEBUG) << "Detected label change from " << before << " to " << after << " at " 23535b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey << path << "; running recursive restorecon"; 23635b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey } 2370274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey if (selinux_android_restorecon_pkgdir(path.c_str(), seInfo.c_str(), uid, 2387db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey SELINUX_ANDROID_RESTORECON_RECURSE) < 0) { 2397db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey PLOG(ERROR) << "Failed recursive restorecon for " << path; 2407db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey goto fail; 2417db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey } 2427db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey } 2437db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey 2447db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey goto done; 2457db6041d156b750689b32e340d8a0753061262a3Jeff Sharkeyfail: 2467db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey res = -1; 2477db6041d156b750689b32e340d8a0753061262a3Jeff Sharkeydone: 2487db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey free(before); 2497db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey free(after); 2507db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey return res; 2517db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey} 2527db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey 2530274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeystatic int restorecon_app_data_lazy(const std::string& parent, const char* name, 25435b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey const std::string& seInfo, uid_t uid, bool existing) { 25535b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey return restorecon_app_data_lazy(StringPrintf("%s/%s", parent.c_str(), name), seInfo, uid, 25635b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey existing); 25795d9ac642f082870fd4d2419f534171a9b82bbcdJeff Sharkey} 25895d9ac642f082870fd4d2419f534171a9b82bbcdJeff Sharkey 2597db6041d156b750689b32e340d8a0753061262a3Jeff Sharkeystatic int prepare_app_dir(const std::string& path, mode_t target_mode, uid_t uid) { 2609a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey if (fs_prepare_dir_strict(path.c_str(), target_mode, uid, uid) != 0) { 2619a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey PLOG(ERROR) << "Failed to prepare " << path; 2629a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey return -1; 2639a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey } 2649a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey return 0; 2659a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey} 2669a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey 2673dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey/** 2683dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey * Prepare an app cache directory, which offers to fix-up the GID and 2693dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey * directory mode flags during a platform upgrade. 2703dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey */ 2713dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkeystatic int prepare_app_cache_dir(const std::string& parent, const char* name, mode_t target_mode, 2723dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey uid_t uid, gid_t gid) { 2733dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey auto path = StringPrintf("%s/%s", parent.c_str(), name); 2743dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey struct stat st; 2753dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (stat(path.c_str(), &st) != 0) { 2763dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (errno == ENOENT) { 2773dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // This is fine, just create it 2783dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (fs_prepare_dir_strict(path.c_str(), target_mode, uid, gid) != 0) { 2793dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey PLOG(ERROR) << "Failed to prepare " << path; 2803dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey return -1; 2813dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } else { 2823dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey return 0; 2833dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 2843dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } else { 2853dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey PLOG(ERROR) << "Failed to stat " << path; 2863dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey return -1; 2873dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 2883dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 2893dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 29024ef15bd009204b73ebf0e94ba3d868177a5f44cJeff Sharkey mode_t actual_mode = st.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO | S_ISGID); 2913dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (st.st_uid != uid) { 2923dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // Mismatched UID is real trouble; we can't recover 2933dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey LOG(ERROR) << "Mismatched UID at " << path << ": found " << st.st_uid 2943dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey << " but expected " << uid; 2953dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey return -1; 29624ef15bd009204b73ebf0e94ba3d868177a5f44cJeff Sharkey } else if (st.st_gid == gid && actual_mode == target_mode) { 2973dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // Everything looks good! 2983dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey return 0; 2993dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 3003dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 3013dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // Directory is owned correctly, but GID or mode mismatch means it's 3023dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // probably a platform upgrade so we need to fix them 3033dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey FTS *fts; 3043dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey FTSENT *p; 3053dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey char *argv[] = { (char*) path.c_str(), nullptr }; 3063dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_XDEV, NULL))) { 3073dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey PLOG(ERROR) << "Failed to fts_open " << path; 3083dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey return -1; 3093dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 3103dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey while ((p = fts_read(fts)) != NULL) { 3113dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey switch (p->fts_info) { 3123dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey case FTS_DP: 3133dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (chmod(p->fts_accpath, target_mode) != 0) { 3143dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey PLOG(WARNING) << "Failed to chmod " << p->fts_path; 3153dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 3163dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // Intentional fall through to also set GID 3173dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey case FTS_F: 3183dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (chown(p->fts_accpath, -1, gid) != 0) { 3193dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey PLOG(WARNING) << "Failed to chown " << p->fts_path; 3203dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 3213dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey break; 3223dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey case FTS_SL: 3233dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey case FTS_SLNONE: 3243dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (lchown(p->fts_accpath, -1, gid) != 0) { 3253dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey PLOG(WARNING) << "Failed to chown " << p->fts_path; 3263dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 3273dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey break; 3283dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 3293dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 3303dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey fts_close(fts); 3313dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey return 0; 3329a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey} 3339a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey 3340274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeybinder::Status InstalldNativeService::createAppData(const std::unique_ptr<std::string>& uuid, 3350274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const std::string& packageName, int32_t userId, int32_t flags, int32_t appId, 33636a900a0db526d63e899ed5d5b0a463ad7f9236eJeff Sharkey const std::string& seInfo, int32_t targetSdkVersion, int64_t* _aidl_return) { 3370274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 338423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 339423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 3400274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 3410274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 3420274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const char* pkgname = packageName.c_str(); 3430274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 34436a900a0db526d63e899ed5d5b0a463ad7f9236eJeff Sharkey // Assume invalid inode unless filled in below 34536a900a0db526d63e899ed5d5b0a463ad7f9236eJeff Sharkey if (_aidl_return != nullptr) *_aidl_return = -1; 34636a900a0db526d63e899ed5d5b0a463ad7f9236eJeff Sharkey 34724ef15bd009204b73ebf0e94ba3d868177a5f44cJeff Sharkey int32_t uid = multiuser_get_uid(userId, appId); 34824ef15bd009204b73ebf0e94ba3d868177a5f44cJeff Sharkey int32_t cacheGid = multiuser_get_cache_gid(userId, appId); 3493dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey mode_t targetMode = targetSdkVersion >= MIN_RESTRICTED_HOME_SDK_VERSION ? 0700 : 0751; 3503dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 35124ef15bd009204b73ebf0e94ba3d868177a5f44cJeff Sharkey // If UID doesn't have a specific cache GID, use UID value 35224ef15bd009204b73ebf0e94ba3d868177a5f44cJeff Sharkey if (cacheGid == -1) { 35324ef15bd009204b73ebf0e94ba3d868177a5f44cJeff Sharkey cacheGid = uid; 35424ef15bd009204b73ebf0e94ba3d868177a5f44cJeff Sharkey } 35524ef15bd009204b73ebf0e94ba3d868177a5f44cJeff Sharkey 356aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (flags & FLAG_STORAGE_CE) { 3570274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey auto path = create_data_user_ce_package_path(uuid_, userId, pkgname); 35835b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey bool existing = (access(path.c_str(), F_OK) == 0); 35935b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey 3603dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (prepare_app_dir(path, targetMode, uid) || 3613dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey prepare_app_cache_dir(path, "cache", 02771, uid, cacheGid) || 3623dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey prepare_app_cache_dir(path, "code_cache", 02771, uid, cacheGid)) { 363423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to prepare " + path); 3647db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey } 3657db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey 3667db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey // Consider restorecon over contents if label changed 36735b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey if (restorecon_app_data_lazy(path, seInfo, uid, existing) || 36835b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey restorecon_app_data_lazy(path, "cache", seInfo, uid, existing) || 36935b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey restorecon_app_data_lazy(path, "code_cache", seInfo, uid, existing)) { 370423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to restorecon " + path); 371c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey } 3729a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey 3739a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey // Remember inode numbers of cache directories so that we can clear 3749a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey // contents while CE storage is locked 3759a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey if (write_path_inode(path, "cache", kXattrInodeCache) || 3769a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey write_path_inode(path, "code_cache", kXattrInodeCodeCache)) { 377423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to write_path_inode for " + path); 378c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey } 37936a900a0db526d63e899ed5d5b0a463ad7f9236eJeff Sharkey 38036a900a0db526d63e899ed5d5b0a463ad7f9236eJeff Sharkey // And return the CE inode of the top-level data directory so we can 38136a900a0db526d63e899ed5d5b0a463ad7f9236eJeff Sharkey // clear contents while CE storage is locked 38236a900a0db526d63e899ed5d5b0a463ad7f9236eJeff Sharkey if ((_aidl_return != nullptr) 38336a900a0db526d63e899ed5d5b0a463ad7f9236eJeff Sharkey && get_path_inode(path, reinterpret_cast<ino_t*>(_aidl_return)) != 0) { 38436a900a0db526d63e899ed5d5b0a463ad7f9236eJeff Sharkey return error("Failed to get_path_inode for " + path); 38536a900a0db526d63e899ed5d5b0a463ad7f9236eJeff Sharkey } 38694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 387aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (flags & FLAG_STORAGE_DE) { 3880274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey auto path = create_data_user_de_package_path(uuid_, userId, pkgname); 38935b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey bool existing = (access(path.c_str(), F_OK) == 0); 39035b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey 3913dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (prepare_app_dir(path, targetMode, uid) || 3923dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey prepare_app_cache_dir(path, "cache", 02771, uid, cacheGid) || 3933dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey prepare_app_cache_dir(path, "code_cache", 02771, uid, cacheGid)) { 394423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to prepare " + path); 395c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey } 3966a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 3977db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey // Consider restorecon over contents if label changed 39835b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey if (restorecon_app_data_lazy(path, seInfo, uid, existing)) { 399423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to restorecon " + path); 4007db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey } 4017db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey 40290aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey if (property_get_bool("dalvik.vm.usejitprofiles", false)) { 4030274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const std::string profile_path = create_data_user_profile_package_path(userId, pkgname); 4046a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle // read-write-execute only for the app user. 4056a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle if (fs_prepare_dir_strict(profile_path.c_str(), 0700, uid, uid) != 0) { 406423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to prepare " + profile_path); 4076a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 408bc56e7dcf07e633867e1525435373a1231216b22Calin Juravle std::string profile_file = create_primary_profile(profile_path); 409bc56e7dcf07e633867e1525435373a1231216b22Calin Juravle // read-write only for the app user. 410bc56e7dcf07e633867e1525435373a1231216b22Calin Juravle if (fs_prepare_file_strict(profile_file.c_str(), 0600, uid, uid) != 0) { 411423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to prepare " + profile_path); 412bc56e7dcf07e633867e1525435373a1231216b22Calin Juravle } 4136a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle const std::string ref_profile_path = create_data_ref_profile_package_path(pkgname); 4146a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle // dex2oat/profman runs under the shared app gid and it needs to read/write reference 4156a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle // profiles. 416adddd9876e145879169369913b5afe469b17c02dJeff Sharkey int shared_app_gid = multiuser_get_shared_gid(0, appId); 4172e6dc9baaaf5d50b42ce36435e13c2559808b837Jeff Sharkey if ((shared_app_gid != -1) && fs_prepare_dir_strict( 4186a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle ref_profile_path.c_str(), 0700, shared_app_gid, shared_app_gid) != 0) { 419423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to prepare " + ref_profile_path); 4206a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 4216a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 42294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 423423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 42494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 42594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 426c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkeybinder::Status InstalldNativeService::migrateAppData(const std::unique_ptr<std::string>& uuid, 427c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const std::string& packageName, int32_t userId, int32_t flags) { 428c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 429423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 430423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 431423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 432c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 433c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* pkgname = packageName.c_str(); 434c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey 435cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey // This method only exists to upgrade system apps that have requested 436cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey // forceDeviceEncrypted, so their default storage always lives in a 437cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey // consistent location. This only works on non-FBE devices, since we 438cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey // never want to risk exposing data on a device with real CE/DE storage. 439cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey 440c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey auto ce_path = create_data_user_ce_package_path(uuid_, userId, pkgname); 441c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey auto de_path = create_data_user_de_package_path(uuid_, userId, pkgname); 442cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey 443cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey // If neither directory is marked as default, assume CE is default 444cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey if (getxattr(ce_path.c_str(), kXattrDefault, nullptr, 0) == -1 445cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey && getxattr(de_path.c_str(), kXattrDefault, nullptr, 0) == -1) { 446cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey if (setxattr(ce_path.c_str(), kXattrDefault, nullptr, 0, 0) != 0) { 447423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to mark default storage " + ce_path); 448cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey } 449cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey } 450cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey 451cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey // Migrate default data location if needed 452cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey auto target = (flags & FLAG_STORAGE_DE) ? de_path : ce_path; 453cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey auto source = (flags & FLAG_STORAGE_DE) ? ce_path : de_path; 454cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey 455cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey if (getxattr(target.c_str(), kXattrDefault, nullptr, 0) == -1) { 456cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey LOG(WARNING) << "Requested default storage " << target 457cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey << " is not active; migrating from " << source; 458cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey if (delete_dir_contents_and_dir(target) != 0) { 459423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to delete " + target); 460cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey } 461cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey if (rename(source.c_str(), target.c_str()) != 0) { 462423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to rename " + source + " to " + target); 463cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey } 464cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey } 465cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey 466423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 467cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey} 468cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey 4696a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 470475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::clearAppProfiles(const std::string& packageName) { 471475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 472423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 473423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 474475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* pkgname = packageName.c_str(); 475423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey binder::Status res = ok(); 476423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (!clear_reference_profile(pkgname)) { 477423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to clear reference profile for " + packageName); 478423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 479423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (!clear_current_profiles(pkgname)) { 480423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to clear current profiles for " + packageName); 481423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 482423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return res; 4836a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle} 4846a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 485c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkeybinder::Status InstalldNativeService::clearAppData(const std::unique_ptr<std::string>& uuid, 486c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const std::string& packageName, int32_t userId, int32_t flags, int64_t ceDataInode) { 487c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 488423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 489423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 490423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 491c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 492c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* pkgname = packageName.c_str(); 493c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey 494423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey binder::Status res = ok(); 495aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (flags & FLAG_STORAGE_CE) { 496c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey auto path = create_data_user_ce_package_path(uuid_, userId, pkgname, ceDataInode); 4979a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey if (flags & FLAG_CLEAR_CACHE_ONLY) { 4989a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey path = read_path_inode(path, "cache", kXattrInodeCache); 4999a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey } else if (flags & FLAG_CLEAR_CODE_CACHE_ONLY) { 5009a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey path = read_path_inode(path, "code_cache", kXattrInodeCodeCache); 5019a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey } 502c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey if (access(path.c_str(), F_OK) == 0) { 503423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (delete_dir_contents(path) != 0) { 504423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to delete contents of " + path); 505423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 506c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey } 50794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 508aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (flags & FLAG_STORAGE_DE) { 5099a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey std::string suffix = ""; 5109a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey bool only_cache = false; 5119a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey if (flags & FLAG_CLEAR_CACHE_ONLY) { 5129a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey suffix = CACHE_DIR_POSTFIX; 5139a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey only_cache = true; 5149a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey } else if (flags & FLAG_CLEAR_CODE_CACHE_ONLY) { 5159a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey suffix = CODE_CACHE_DIR_POSTFIX; 5169a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey only_cache = true; 5179a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey } 5189a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey 519c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey auto path = create_data_user_de_package_path(uuid_, userId, pkgname) + suffix; 520c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey if (access(path.c_str(), F_OK) == 0) { 521423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (delete_dir_contents(path) != 0) { 522423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to delete contents of " + path); 523423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 524c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey } 525edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle if (!only_cache) { 526c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey if (!clear_current_profile(pkgname, userId)) { 527423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to clear current profile for " + packageName); 528edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle } 529edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle } 53094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 531423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return res; 532c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey} 53394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 5347535e8e1995398befc866062d63c05d90eb70cccCalin Juravlestatic int destroy_app_reference_profile(const char *pkgname) { 5357535e8e1995398befc866062d63c05d90eb70cccCalin Juravle return delete_dir_contents_and_dir( 5367535e8e1995398befc866062d63c05d90eb70cccCalin Juravle create_data_ref_profile_package_path(pkgname), 5377535e8e1995398befc866062d63c05d90eb70cccCalin Juravle /*ignore_if_missing*/ true); 5387535e8e1995398befc866062d63c05d90eb70cccCalin Juravle} 5397535e8e1995398befc866062d63c05d90eb70cccCalin Juravle 540edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravlestatic int destroy_app_current_profiles(const char *pkgname, userid_t userid) { 541b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravle return delete_dir_contents_and_dir( 542b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravle create_data_user_profile_package_path(userid, pkgname), 543b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravle /*ignore_if_missing*/ true); 544edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle} 545edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle 546475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::destroyAppProfiles(const std::string& packageName) { 547475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 548423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 549423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 550475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* pkgname = packageName.c_str(); 551423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey binder::Status res = ok(); 552edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle std::vector<userid_t> users = get_known_users(/*volume_uuid*/ nullptr); 553edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle for (auto user : users) { 554423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (destroy_app_current_profiles(pkgname, user) != 0) { 555423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to destroy current profiles for " + packageName); 556423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 557edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle } 558423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (destroy_app_reference_profile(pkgname) != 0) { 559423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to destroy reference profile for " + packageName); 560423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 561423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return res; 562caa6b809a5ed024a8f3f551c5c109a5ad50ab772Calin Juravle} 563caa6b809a5ed024a8f3f551c5c109a5ad50ab772Calin Juravle 564c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkeybinder::Status InstalldNativeService::destroyAppData(const std::unique_ptr<std::string>& uuid, 565c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const std::string& packageName, int32_t userId, int32_t flags, int64_t ceDataInode) { 566c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 567423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 568423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 569423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 570c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 571c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* pkgname = packageName.c_str(); 572c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey 573423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey binder::Status res = ok(); 574aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (flags & FLAG_STORAGE_CE) { 575423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey auto path = create_data_user_ce_package_path(uuid_, userId, pkgname, ceDataInode); 576423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (delete_dir_contents_and_dir(path) != 0) { 577423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to delete " + path); 578423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 57994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 580aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (flags & FLAG_STORAGE_DE) { 581423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey auto path = create_data_user_de_package_path(uuid_, userId, pkgname); 582423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (delete_dir_contents_and_dir(path) != 0) { 583423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to delete " + path); 584423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 585c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey destroy_app_current_profiles(pkgname, userId); 5867535e8e1995398befc866062d63c05d90eb70cccCalin Juravle // TODO(calin): If the package is still installed by other users it's probably 5877535e8e1995398befc866062d63c05d90eb70cccCalin Juravle // beneficial to keep the reference profile around. 5887535e8e1995398befc866062d63c05d90eb70cccCalin Juravle // Verify if it's ok to do that. 5897535e8e1995398befc866062d63c05d90eb70cccCalin Juravle destroy_app_reference_profile(pkgname); 59094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 591423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return res; 59294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 59394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 5940274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeybinder::Status InstalldNativeService::moveCompleteApp(const std::unique_ptr<std::string>& fromUuid, 5950274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const std::unique_ptr<std::string>& toUuid, const std::string& packageName, 5960274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const std::string& dataAppName, int32_t appId, const std::string& seInfo, 5970274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey int32_t targetSdkVersion) { 598423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 599423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(fromUuid); 600423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(toUuid); 601423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 6020274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 6030274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const char* from_uuid = fromUuid ? fromUuid->c_str() : nullptr; 6040274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const char* to_uuid = toUuid ? toUuid->c_str() : nullptr; 6050274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const char* package_name = packageName.c_str(); 6060274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const char* data_app_name = dataAppName.c_str(); 6070274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 608423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey binder::Status res = ok(); 609e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey std::vector<userid_t> users = get_known_users(from_uuid); 610e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 611d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey // Copy app 612d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey { 61351c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto from = create_data_app_package_path(from_uuid, data_app_name); 61451c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto to = create_data_app_package_path(to_uuid, data_app_name); 61551c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto to_parent = create_data_app_path(to_uuid); 616d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey 617d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey char *argv[] = { 618d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey (char*) kCpPath, 619d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey (char*) "-F", /* delete any existing destination file first (--remove-destination) */ 620d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey (char*) "-p", /* preserve timestamps, ownership, and permissions */ 621d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey (char*) "-R", /* recurse into subdirectories (DEST must be a directory) */ 622d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey (char*) "-P", /* Do not follow symlinks [default] */ 623d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey (char*) "-d", /* don't dereference symlinks */ 624d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey (char*) from.c_str(), 625d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey (char*) to_parent.c_str() 626d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey }; 627d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey 628d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey LOG(DEBUG) << "Copying " << from << " to " << to; 629d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true); 630d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey if (rc != 0) { 631423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error(rc, "Failed copying " + from + " to " + to); 632d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey goto fail; 633d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey } 634d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey 635d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey if (selinux_android_restorecon(to.c_str(), SELINUX_ANDROID_RESTORECON_RECURSE) != 0) { 636423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to restorecon " + to); 637d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey goto fail; 638d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey } 639d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey } 640d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey 641d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey // Copy private data for all known users 642e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey for (auto user : users) { 643e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 644e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey // Data source may not exist for all users; that's okay 64551c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto from_ce = create_data_user_ce_package_path(from_uuid, user, package_name); 64651c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey if (access(from_ce.c_str(), F_OK) != 0) { 64751c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey LOG(INFO) << "Missing source " << from_ce; 648e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey continue; 649e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey } 650e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 6510274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey if (!createAppData(toUuid, packageName, user, FLAG_STORAGE_CE | FLAG_STORAGE_DE, appId, 65236a900a0db526d63e899ed5d5b0a463ad7f9236eJeff Sharkey seInfo, targetSdkVersion, nullptr).isOk()) { 653423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to create package target"); 654e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey goto fail; 655e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey } 656e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 657e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey char *argv[] = { 658e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey (char*) kCpPath, 659e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey (char*) "-F", /* delete any existing destination file first (--remove-destination) */ 660e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey (char*) "-p", /* preserve timestamps, ownership, and permissions */ 661e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey (char*) "-R", /* recurse into subdirectories (DEST must be a directory) */ 662e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey (char*) "-P", /* Do not follow symlinks [default] */ 663e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey (char*) "-d", /* don't dereference symlinks */ 66451c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey nullptr, 66551c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey nullptr 666e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey }; 667e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 66851c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey { 66951c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto from = create_data_user_de_package_path(from_uuid, user, package_name); 67051c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto to = create_data_user_de_path(to_uuid, user); 67151c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey argv[6] = (char*) from.c_str(); 67251c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey argv[7] = (char*) to.c_str(); 67351c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey 67451c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey LOG(DEBUG) << "Copying " << from << " to " << to; 67551c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true); 67651c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey if (rc != 0) { 677423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error(rc, "Failed copying " + from + " to " + to); 67851c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey goto fail; 67951c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey } 68051c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey } 68151c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey { 68251c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto from = create_data_user_ce_package_path(from_uuid, user, package_name); 68351c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto to = create_data_user_ce_path(to_uuid, user); 68451c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey argv[6] = (char*) from.c_str(); 68551c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey argv[7] = (char*) to.c_str(); 68651c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey 68751c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey LOG(DEBUG) << "Copying " << from << " to " << to; 68851c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true); 68951c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey if (rc != 0) { 690423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error(rc, "Failed copying " + from + " to " + to); 69151c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey goto fail; 69251c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey } 693e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey } 694e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 695c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey if (!restoreconAppData(toUuid, packageName, user, FLAG_STORAGE_CE | FLAG_STORAGE_DE, 696c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey appId, seInfo).isOk()) { 697423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to restorecon"); 698c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey goto fail; 699c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey } 700e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey } 701e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 70231f08986f83fa6f2dcf55523b2cf706460aeed7cJeff Sharkey // We let the framework scan the new location and persist that before 70331f08986f83fa6f2dcf55523b2cf706460aeed7cJeff Sharkey // deleting the data in the old location; this ordering ensures that 70431f08986f83fa6f2dcf55523b2cf706460aeed7cJeff Sharkey // we can recover from things like battery pulls. 705423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 706e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 707e36372423000a906bafae68844ebc6c42d09335aJeff Sharkeyfail: 708e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey // Nuke everything we might have already copied 709d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey { 71051c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto to = create_data_app_package_path(to_uuid, data_app_name); 711d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey if (delete_dir_contents(to.c_str(), 1, NULL) != 0) { 712d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey LOG(WARNING) << "Failed to rollback " << to; 713d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey } 714d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey } 715e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey for (auto user : users) { 71651c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey { 71751c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto to = create_data_user_de_package_path(to_uuid, user, package_name); 71851c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey if (delete_dir_contents(to.c_str(), 1, NULL) != 0) { 71951c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey LOG(WARNING) << "Failed to rollback " << to; 72051c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey } 72151c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey } 72251c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey { 72351c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto to = create_data_user_ce_package_path(to_uuid, user, package_name); 72451c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey if (delete_dir_contents(to.c_str(), 1, NULL) != 0) { 72551c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey LOG(WARNING) << "Failed to rollback " << to; 72651c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey } 727e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey } 728e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey } 729423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return res; 730e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey} 731e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 732c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkeybinder::Status InstalldNativeService::createUserData(const std::unique_ptr<std::string>& uuid, 733c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey int32_t userId, int32_t userSerial ATTRIBUTE_UNUSED, int32_t flags) { 734c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 735423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 736423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 737c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 738379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey if (flags & FLAG_STORAGE_DE) { 739c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey if (uuid_ == nullptr) { 740423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (ensure_config_user_dirs(userId) != 0) { 7417be5ead81aa4d3b2536556c762bab03445a98155Jeff Sharkey return error(StringPrintf("Failed to ensure dirs for %d", userId)); 742423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 743379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey } 74494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 7457be5ead81aa4d3b2536556c762bab03445a98155Jeff Sharkey return ok(); 746095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee} 747095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee 748c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkeybinder::Status InstalldNativeService::destroyUserData(const std::unique_ptr<std::string>& uuid, 749c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey int32_t userId, int32_t flags) { 750c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 751423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 752423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 753c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 754423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey binder::Status res = ok(); 755379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey if (flags & FLAG_STORAGE_DE) { 756423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey auto path = create_data_user_de_path(uuid_, userId); 757423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (delete_dir_contents_and_dir(path, true) != 0) { 758423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to delete " + path); 759423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 760c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey if (uuid_ == nullptr) { 761423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey path = create_data_misc_legacy_path(userId); 762423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (delete_dir_contents_and_dir(path, true) != 0) { 763423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to delete " + path); 764423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 765df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey path = create_data_user_profile_path(userId); 766423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (delete_dir_contents_and_dir(path, true) != 0) { 767423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to delete " + path); 768423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 76941ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey } 77094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 771379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey if (flags & FLAG_STORAGE_CE) { 772423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey auto path = create_data_user_ce_path(uuid_, userId); 773423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (delete_dir_contents_and_dir(path, true) != 0) { 774423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to delete " + path); 775423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 776423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey path = create_data_media_path(uuid_, userId); 777423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (delete_dir_contents_and_dir(path, true) != 0) { 778423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to delete " + path); 779423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 780379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey } 781423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return res; 78294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 78394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 78494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/* Try to ensure free_size bytes of storage are available. 78594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * Returns 0 on success. 78694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * This is rather simple-minded because doing a full LRU would 78794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * be potentially memory-intensive, and without atime it would 78894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * also require that apps constantly modify file metadata even 78994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * when just reading from the cache, which is pretty awful. 79094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */ 791475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::freeCache(const std::unique_ptr<std::string>& uuid, 792475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey int64_t freeStorageSize) { 793475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 794423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 795423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 796475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 79794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache_t* cache; 79894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int64_t avail; 79994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 800475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey auto data_path = create_data_path(uuid_); 80141ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey 80241ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey avail = data_disk_free(data_path); 803475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey if (avail < 0) { 804423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to determine free space for " + data_path); 805475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey } 80694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 807475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ALOGI("free_cache(%" PRId64 ") avail %" PRId64 "\n", freeStorageSize, avail); 808475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey if (avail >= freeStorageSize) { 809423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 810475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey } 81194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 81294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache = start_cache_collection(); 81394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 814475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey auto users = get_known_users(uuid_); 81554e292e1ea87b504b552393d2e9bc800458bd0c1Jeff Sharkey for (auto user : users) { 816475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey add_cache_files(cache, create_data_user_ce_path(uuid_, user)); 817475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey add_cache_files(cache, create_data_user_de_path(uuid_, user)); 81854e292e1ea87b504b552393d2e9bc800458bd0c1Jeff Sharkey add_cache_files(cache, 819475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey StringPrintf("%s/Android/data", create_data_media_path(uuid_, user).c_str())); 82094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 82194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 822475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey clear_cache_files(data_path, cache, freeStorageSize); 82394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood finish_cache_collection(cache); 82494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 825423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey avail = data_disk_free(data_path); 826423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (avail >= freeStorageSize) { 827423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 828475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey } else { 829423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error(StringPrintf("Failed to free up %" PRId64 " on %s; final free space %" PRId64, 830423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey freeStorageSize, data_path.c_str(), avail)); 831475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey } 83294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 83394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 834475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::rmdex(const std::string& codePath, 835475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const std::string& instructionSet) { 836475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 83794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood char dex_path[PKG_PATH_MAX]; 83894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 839475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* path = codePath.c_str(); 840475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* instruction_set = instructionSet.c_str(); 841475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey 842770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey if (validate_apk_path(path) && validate_system_app_path(path)) { 843423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Invalid path " + codePath); 844770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey } 845770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey 846475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey if (!create_cache_path(dex_path, path, instruction_set)) { 847423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to create cache path for " + codePath); 848475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey } 84994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 85094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGV("unlink %s\n", dex_path); 85194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (unlink(dex_path) < 0) { 852423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error(StringPrintf("Failed to unlink %s", dex_path)); 85394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else { 854423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 85594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 85694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 85794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 8583dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkeystruct stats { 8593dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey int64_t codeSize; 8603dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey int64_t dataSize; 8613dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey int64_t cacheSize; 8623dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey}; 8633dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 864df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 865df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkeystatic std::string toString(std::vector<int64_t> values) { 866df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey std::stringstream res; 867df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey res << "["; 868df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey for (size_t i = 0; i < values.size(); i++) { 869df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey res << values[i]; 870df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (i < values.size() - 1) { 871df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey res << ","; 872df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 873df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 874df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey res << "]"; 875df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey return res.str(); 876df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey} 877df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 8783dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 879df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkeystatic std::string findDeviceForUuid(const std::unique_ptr<std::string>& uuid) { 8803dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey auto path = create_data_path(uuid ? uuid->c_str() : nullptr); 881df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey std::ifstream in("/proc/mounts"); 882df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (!in.is_open()) { 883df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey PLOG(ERROR) << "Failed to read mounts"; 884df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey return ""; 8853dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 886df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey std::string source; 887df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey std::string target; 888df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey while (!in.eof()) { 889df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey std::getline(in, source, ' '); 890df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey std::getline(in, target, ' '); 891df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (target == path) { 892df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey return source; 893df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 894df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // Skip to next line 895df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey std::getline(in, source); 8963dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 897df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey PLOG(ERROR) << "Failed to resolve block device for " << path; 898df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey return ""; 899df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey} 900df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 901df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkeystatic void collectQuotaStats(const std::string& device, int32_t userId, 902df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int32_t appId, struct stats* stats, struct stats* extStats ATTRIBUTE_UNUSED) { 903df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (device.empty()) return; 904df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 905df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey struct dqblk dq; 9063dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 9073dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey uid_t uid = multiuser_get_uid(userId, appId); 9083dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (quotactl(QCMD(Q_GETQUOTA, USRQUOTA), device.c_str(), uid, 9093dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey reinterpret_cast<char*>(&dq)) != 0) { 9103dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (errno != ESRCH) { 9113dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey PLOG(ERROR) << "Failed to quotactl " << device << " for UID " << uid; 9123dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 9133dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } else { 914df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 915df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey LOG(DEBUG) << "quotactl() for UID " << uid << " " << dq.dqb_curspace; 916df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 9173dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey stats->dataSize += dq.dqb_curspace; 9183dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 9193dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 9203dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey int cacheGid = multiuser_get_cache_gid(userId, appId); 9213dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (cacheGid != -1) { 9223dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), cacheGid, 9233dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey reinterpret_cast<char*>(&dq)) != 0) { 9243dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (errno != ESRCH) { 9253dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey PLOG(ERROR) << "Failed to quotactl " << device << " for GID " << cacheGid; 9263dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 9273dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } else { 928df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 929df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey LOG(DEBUG) << "quotactl() for GID " << cacheGid << " " << dq.dqb_curspace; 930df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 9313dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey stats->cacheSize += dq.dqb_curspace; 9323dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 9333dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 9343dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 9353dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey int sharedGid = multiuser_get_shared_app_gid(uid); 9363dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (sharedGid != -1) { 9373dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), sharedGid, 9383dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey reinterpret_cast<char*>(&dq)) != 0) { 9393dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (errno != ESRCH) { 9403dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey PLOG(ERROR) << "Failed to quotactl " << device << " for GID " << sharedGid; 9413dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 9423dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } else { 943df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 944df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey LOG(DEBUG) << "quotactl() for GID " << sharedGid << " " << dq.dqb_curspace; 945df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 9463dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey stats->codeSize += dq.dqb_curspace; 9473dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 9483dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 949df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 950df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_EXTERNAL 951df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // TODO: measure using external GIDs 952df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 953df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey} 954df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 955df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkeystatic void collectQuotaStats(const std::unique_ptr<std::string>& uuid, int32_t userId, 956df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int32_t appId, struct stats* stats, struct stats* extStats) { 957df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey collectQuotaStats(findDeviceForUuid(uuid), userId, appId, stats, extStats); 9583dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey} 9593dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 960df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkeystatic void collectManualStats(const std::string& path, struct stats* stats) { 96194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood DIR *d; 96294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int dfd; 96394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood struct dirent *de; 96494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood struct stat s; 965d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey 9662f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey d = opendir(path.c_str()); 9672f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey if (d == nullptr) { 9683dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (errno != ENOENT) { 9693dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey PLOG(WARNING) << "Failed to open " << path; 9703dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 9712f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey return; 97294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 9732f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey dfd = dirfd(d); 9742f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey while ((de = readdir(d))) { 9752f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey const char *name = de->d_name; 976d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey 9773dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey int64_t size = 0; 9782f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) { 9793dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey size = s.st_blocks * 512; 98094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 98194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 9822f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey if (de->d_type == DT_DIR) { 9833dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (!strcmp(name, ".")) { 9843dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // Don't recurse, but still count node size 9853dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } else if (!strcmp(name, "..")) { 9863dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // Don't recurse or count node size 9873dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey continue; 9883dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } else { 9893dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // Measure all children nodes 9903dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey size = 0; 9913dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey calculate_tree_size(StringPrintf("%s/%s", path.c_str(), name), &size); 9922f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey } 9933dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 9942f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey if (!strcmp(name, "cache") || !strcmp(name, "code_cache")) { 9953dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey stats->cacheSize += size; 9962f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey } 99794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 9983dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 9993dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // Legacy symlink isn't owned by app 10003dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (de->d_type == DT_LNK && !strcmp(name, "lib")) { 10013dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey continue; 10023dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 10033dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 10043dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // Everything found inside is considered data 10053dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey stats->dataSize += size; 100694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 10072f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey closedir(d); 10082f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey} 100994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 1010df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkeystatic void collectManualStatsForUser(const std::string& path, struct stats* stats, 1011df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey bool exclude_apps = false) { 1012df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey DIR *d; 1013df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int dfd; 1014df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey struct dirent *de; 1015df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey struct stat s; 1016df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1017df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey d = opendir(path.c_str()); 1018df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (d == nullptr) { 1019df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (errno != ENOENT) { 1020df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey PLOG(WARNING) << "Failed to open " << path; 1021df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1022df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey return; 1023df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1024df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey dfd = dirfd(d); 1025df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey while ((de = readdir(d))) { 1026df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (de->d_type == DT_DIR) { 1027df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey const char *name = de->d_name; 1028df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) != 0) { 1029df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey continue; 1030df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1031df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (!strcmp(name, ".") || !strcmp(name, "..")) { 1032df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey continue; 1033df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } else if (exclude_apps && (s.st_uid >= AID_APP_START && s.st_uid <= AID_APP_END)) { 1034df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey continue; 1035df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } else { 1036df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey collectManualStats(StringPrintf("%s/%s", path.c_str(), name), stats); 1037df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1038df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1039df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1040df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey closedir(d); 1041df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey} 1042df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 10436c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkeybinder::Status InstalldNativeService::getAppSize(const std::unique_ptr<std::string>& uuid, 1044df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey const std::vector<std::string>& packageNames, int32_t userId, int32_t flags, 1045df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int32_t appId, const std::vector<int64_t>& ceDataInodes, 1046df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey const std::vector<std::string>& codePaths, std::vector<int64_t>* _aidl_return) { 10476c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 1048423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 1049df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey for (auto packageName : packageNames) { 1050df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 1051df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1052423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 1053df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // When modifying this logic, always verify using tests: 1054df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // runtest -x frameworks/base/services/tests/servicestests/src/com/android/server/pm/InstallerTest.java -m testGetAppSize 1055df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1056df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 1057df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey LOG(INFO) << "Measuring user " << userId << " app " << appId; 1058df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 10596c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey 10603dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // Here's a summary of the common storage locations across the platform, 10613dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // and how they're each tagged: 10623dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // 10633dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // /data/app/com.example UID system 10643dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // /data/app/com.example/oat UID system 1065df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // /data/user/0/com.example UID u0_a10 GID u0_a10 1066df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // /data/user/0/com.example/cache UID u0_a10 GID u0_a10_cache 1067df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // /data/media/0/foo.txt UID u0_media_rw 1068df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // /data/media/0/bar.jpg UID u0_media_rw GID u0_media_image 1069df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // /data/media/0/Android/data/com.example UID u0_media_rw GID u0_a10_ext 1070df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // /data/media/0/Android/data/com.example/cache UID u0_media_rw GID u0_a10_ext_cache 1071df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // /data/media/obb/com.example UID system 10723dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 10733dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey struct stats stats; 1074df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey struct stats extStats; 10753dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey memset(&stats, 0, sizeof(stats)); 1076df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey memset(&extStats, 0, sizeof(extStats)); 10773dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 1078df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 10793dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 1080df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey for (auto packageName : packageNames) { 1081df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto obbCodePath = create_data_media_obb_path(uuid_, packageName.c_str()); 1082df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(obbCodePath, &extStats.codeSize); 1083df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 10843dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 1085df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (flags & FLAG_USE_QUOTA && appId >= AID_APP_START) { 1086df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey for (auto codePath : codePaths) { 1087df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(codePath, &stats.codeSize, -1, 1088df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey multiuser_get_shared_gid(userId, appId)); 10893dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 1090df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1091df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey collectQuotaStats(uuid, userId, appId, &stats, &extStats); 1092df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 10933dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } else { 1094df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey for (auto codePath : codePaths) { 1095df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(codePath, &stats.codeSize); 1096df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 109794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 1098df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey for (size_t i = 0; i < packageNames.size(); i++) { 1099df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey const char* pkgname = packageNames[i].c_str(); 110094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 1101df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto cePath = create_data_user_ce_package_path(uuid_, userId, pkgname, ceDataInodes[i]); 1102df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey collectManualStats(cePath, &stats); 11033dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 1104df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto dePath = create_data_user_de_package_path(uuid_, userId, pkgname); 1105df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey collectManualStats(dePath, &stats); 11063dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 1107df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto userProfilePath = create_data_user_profile_package_path(userId, pkgname); 1108df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(userProfilePath, &stats.dataSize); 11093dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 1110df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto refProfilePath = create_data_ref_profile_package_path(pkgname); 1111df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(refProfilePath, &stats.codeSize); 1112df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1113df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_EXTERNAL 1114df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto extPath = create_data_media_package_path(uuid_, userId, pkgname, "data"); 1115df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey collectManualStats(extPath, &extStats); 1116df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1117df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto mediaPath = create_data_media_package_path(uuid_, userId, pkgname, "media"); 1118df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(mediaPath, &extStats.dataSize); 1119df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 1120df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1121df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1122df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int32_t sharedGid = multiuser_get_shared_gid(userId, appId); 1123df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (sharedGid != -1) { 1124df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(create_data_dalvik_cache_path(), &stats.codeSize, 1125df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey sharedGid, -1); 1126df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 11273dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 11283dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey calculate_tree_size(create_data_misc_foreign_dex_path(userId), &stats.dataSize, 1129df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey multiuser_get_uid(userId, appId), -1); 1130df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1131df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1132df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey std::vector<int64_t> ret; 1133df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(stats.codeSize); 1134df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(stats.dataSize); 1135df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(stats.cacheSize); 1136df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(extStats.codeSize); 1137df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(extStats.dataSize); 1138df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(extStats.cacheSize); 1139df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 1140df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey LOG(DEBUG) << "Final result " << toString(ret); 1141df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 1142df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey *_aidl_return = ret; 1143df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey return ok(); 1144df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey} 1145df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1146df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkeybinder::Status InstalldNativeService::getUserSize(const std::unique_ptr<std::string>& uuid, 1147df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int32_t userId, int32_t flags, const std::vector<int32_t>& appIds, 1148df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey std::vector<int64_t>* _aidl_return) { 1149df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 1150df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 1151df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1152df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // When modifying this logic, always verify using tests: 1153df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // runtest -x frameworks/base/services/tests/servicestests/src/com/android/server/pm/InstallerTest.java -m testGetUserSize 1154df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1155df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 1156df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey LOG(INFO) << "Measuring user " << userId; 1157df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 1158df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1159df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey struct stats stats; 1160df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey struct stats extStats; 1161df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey memset(&stats, 0, sizeof(stats)); 1162df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey memset(&extStats, 0, sizeof(extStats)); 1163df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1164df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 1165df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1166df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto obbPath = create_data_path(uuid_) + "/media/obb"; 1167df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(obbPath, &extStats.codeSize); 1168df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1169df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (flags & FLAG_USE_QUOTA) { 1170df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(create_data_app_path(uuid_), &stats.codeSize, -1, -1, true); 1171df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1172df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto cePath = create_data_user_ce_path(uuid_, userId); 1173df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey collectManualStatsForUser(cePath, &stats, true); 1174df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1175df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto dePath = create_data_user_de_path(uuid_, userId); 1176df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey collectManualStatsForUser(dePath, &stats, true); 1177df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1178df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto userProfilePath = create_data_user_profile_path(userId); 1179df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(userProfilePath, &stats.dataSize, -1, -1, true); 1180df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1181df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto refProfilePath = create_data_ref_profile_path(); 1182df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(refProfilePath, &stats.codeSize, -1, -1, true); 11833dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 11843dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey#if MEASURE_EXTERNAL 1185df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // TODO: measure external storage paths 1186df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 11873dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 1188df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(create_data_dalvik_cache_path(), &stats.codeSize, 1189df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey -1, -1, true); 1190df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1191df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(create_data_misc_foreign_dex_path(userId), &stats.dataSize, 1192df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey -1, -1, true); 1193df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1194df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto device = findDeviceForUuid(uuid); 1195df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey for (auto appId : appIds) { 1196df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (appId >= AID_APP_START) { 1197df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey collectQuotaStats(device, userId, appId, &stats, &extStats); 1198df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 1199df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // Sleep to make sure we don't lose logs 1200df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey usleep(1); 1201df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 1202df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1203df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1204df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } else { 1205df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(create_data_app_path(uuid_), &stats.codeSize); 1206df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1207df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto cePath = create_data_user_ce_path(uuid_, userId); 1208df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey collectManualStatsForUser(cePath, &stats); 1209df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1210df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto dePath = create_data_user_de_path(uuid_, userId); 1211df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey collectManualStatsForUser(dePath, &stats); 1212df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1213df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto userProfilePath = create_data_user_profile_path(userId); 1214df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(userProfilePath, &stats.dataSize); 1215df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1216df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto refProfilePath = create_data_ref_profile_path(); 1217df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(refProfilePath, &stats.codeSize); 1218df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1219df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_EXTERNAL 1220df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // TODO: measure external storage paths 12213dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey#endif 1222df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1223df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(create_data_dalvik_cache_path(), &stats.codeSize); 1224df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1225df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(create_data_misc_foreign_dex_path(userId), &stats.dataSize); 12262f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey } 122794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 12283dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey std::vector<int64_t> ret; 12293dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey ret.push_back(stats.codeSize); 12303dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey ret.push_back(stats.dataSize); 12313dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey ret.push_back(stats.cacheSize); 1232df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(extStats.codeSize); 1233df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(extStats.dataSize); 1234df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(extStats.cacheSize); 1235df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 1236df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey LOG(DEBUG) << "Final result " << toString(ret); 1237df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 1238df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey *_aidl_return = ret; 1239df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey return ok(); 1240df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey} 1241df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1242df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkeybinder::Status InstalldNativeService::getExternalSize(const std::unique_ptr<std::string>& uuid, 1243df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int32_t userId, int32_t flags, std::vector<int64_t>* _aidl_return) { 1244df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 1245df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 1246df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1247df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // When modifying this logic, always verify using tests: 1248df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // runtest -x frameworks/base/services/tests/servicestests/src/com/android/server/pm/InstallerTest.java -m testGetExternalSize 1249df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1250df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 1251df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey LOG(INFO) << "Measuring external " << userId; 1252df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 1253df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1254df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 1255df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1256df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int64_t totalSize = 0; 1257df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int64_t audioSize = 0; 1258df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int64_t videoSize = 0; 1259df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int64_t imageSize = 0; 1260df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1261df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (flags & FLAG_USE_QUOTA) { 1262df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey struct dqblk dq; 1263df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1264df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto device = findDeviceForUuid(uuid); 1265df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1266df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey uid_t uid = multiuser_get_uid(userId, AID_MEDIA_RW); 1267df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (quotactl(QCMD(Q_GETQUOTA, USRQUOTA), device.c_str(), uid, 1268df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey reinterpret_cast<char*>(&dq)) != 0) { 1269df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (errno != ESRCH) { 1270df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey PLOG(ERROR) << "Failed to quotactl " << device << " for UID " << uid; 1271df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1272df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } else { 1273df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 1274df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey LOG(DEBUG) << "quotactl() for UID " << uid << " " << dq.dqb_curspace; 1275df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 1276df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey totalSize = dq.dqb_curspace; 1277df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1278df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1279df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey gid_t audioGid = multiuser_get_uid(userId, AID_MEDIA_AUDIO); 1280df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), audioGid, 1281df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey reinterpret_cast<char*>(&dq)) == 0) { 1282df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 1283df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey LOG(DEBUG) << "quotactl() for GID " << audioGid << " " << dq.dqb_curspace; 1284df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 1285df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey audioSize = dq.dqb_curspace; 1286df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1287df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey gid_t videoGid = multiuser_get_uid(userId, AID_MEDIA_VIDEO); 1288df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), videoGid, 1289df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey reinterpret_cast<char*>(&dq)) == 0) { 1290df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 1291df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey LOG(DEBUG) << "quotactl() for GID " << videoGid << " " << dq.dqb_curspace; 1292df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 1293df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey videoSize = dq.dqb_curspace; 1294df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1295df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey gid_t imageGid = multiuser_get_uid(userId, AID_MEDIA_IMAGE); 1296df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), imageGid, 1297df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey reinterpret_cast<char*>(&dq)) == 0) { 1298df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 1299df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey LOG(DEBUG) << "quotactl() for GID " << imageGid << " " << dq.dqb_curspace; 1300df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 1301df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey imageSize = dq.dqb_curspace; 1302df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1303df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } else { 1304df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey FTS *fts; 1305df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey FTSENT *p; 1306df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto path = create_data_media_path(uuid_, userId); 1307df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey char *argv[] = { (char*) path.c_str(), nullptr }; 1308df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_XDEV, NULL))) { 1309df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey return error("Failed to fts_open " + path); 1310df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1311df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey while ((p = fts_read(fts)) != NULL) { 1312df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey char* ext; 1313df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int64_t size = (p->fts_statp->st_blocks * 512); 1314df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey switch (p->fts_info) { 1315df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey case FTS_F: 1316df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // Only categorize files not belonging to apps 1317df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (p->fts_statp->st_gid < AID_APP_START) { 1318df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ext = strrchr(p->fts_name, '.'); 1319df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (ext != nullptr) { 1320df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey switch (MatchExtension(++ext)) { 1321df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey case AID_MEDIA_AUDIO: audioSize += size; break; 1322df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey case AID_MEDIA_VIDEO: videoSize += size; break; 1323df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey case AID_MEDIA_IMAGE: imageSize += size; break; 1324df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1325df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1326df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1327df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // Fall through to always count against total 1328df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey case FTS_D: 1329df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey case FTS_DEFAULT: 1330df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey case FTS_SL: 1331df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey case FTS_SLNONE: 1332df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey totalSize += size; 1333df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey break; 1334df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1335df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1336df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey fts_close(fts); 1337df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1338df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1339df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey std::vector<int64_t> ret; 1340df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(totalSize); 1341df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(audioSize); 1342df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(videoSize); 1343df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(imageSize); 1344df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 1345df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey LOG(DEBUG) << "Final result " << toString(ret); 1346df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 13473dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey *_aidl_return = ret; 1348423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 13492f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey} 1350d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey 13516727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr// Dumps the contents of a profile file, using pkgname's dex files for pretty 13526727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr// printing the result. 1353475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::dumpProfiles(int32_t uid, const std::string& packageName, 1354475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const std::string& codePaths, bool* _aidl_return) { 1355475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 1356423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 1357475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey 1358475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* pkgname = packageName.c_str(); 135990aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey const char* code_paths = codePaths.c_str(); 13606727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr 136190aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey *_aidl_return = dump_profiles(uid, pkgname, code_paths); 1362423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 13636727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr} 13646727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr 13654d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe// TODO: Consider returning error codes. 1366475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::mergeProfiles(int32_t uid, const std::string& packageName, 1367423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey bool* _aidl_return) { 1368475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 1369423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 1370423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 1371475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* pkgname = packageName.c_str(); 1372475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey *_aidl_return = analyse_profiles(uid, pkgname); 1373423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 13744d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe} 13754d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe 13766c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkeybinder::Status InstalldNativeService::dexopt(const std::string& apkPath, int32_t uid, 13776c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey const std::unique_ptr<std::string>& packageName, const std::string& instructionSet, 13786c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey int32_t dexoptNeeded, const std::unique_ptr<std::string>& outputPath, int32_t dexFlags, 13796c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey const std::string& compilerFilter, const std::unique_ptr<std::string>& uuid, 13806c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey const std::unique_ptr<std::string>& sharedLibraries) { 13816c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 1382423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 1383423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (packageName && *packageName != "*") { 1384423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(*packageName); 1385423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 13866c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey 13876c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey const char* apk_path = apkPath.c_str(); 13886c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey const char* pkgname = packageName ? packageName->c_str() : "*"; 13896c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey const char* instruction_set = instructionSet.c_str(); 13906c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey const char* oat_dir = outputPath ? outputPath->c_str() : nullptr; 13916c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey const char* compiler_filter = compilerFilter.c_str(); 13926c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey const char* volume_uuid = uuid ? uuid->c_str() : nullptr; 13936c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey const char* shared_libraries = sharedLibraries ? sharedLibraries->c_str() : nullptr; 13946c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey 13956c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey int res = android::installd::dexopt(apk_path, uid, pkgname, instruction_set, dexoptNeeded, 13966c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey oat_dir, dexFlags, compiler_filter, volume_uuid, shared_libraries); 1397423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return res ? error(res, "Failed to dexopt") : ok(); 13986c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey} 13996c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey 1400475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::markBootComplete(const std::string& instructionSet) { 1401475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 1402475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* instruction_set = instructionSet.c_str(); 1403475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey 1404475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey char boot_marker_path[PKG_PATH_MAX]; 1405475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey sprintf(boot_marker_path, 140602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe "%s/%s/%s/.booting", 140702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe android_data_dir.path, 140802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe DALVIK_CACHE, 140902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe instruction_set); 1410091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath 1411475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ALOGV("mark_boot_complete : %s", boot_marker_path); 1412475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey if (unlink(boot_marker_path) != 0) { 1413423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error(StringPrintf("Failed to unlink %s", boot_marker_path)); 1414475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey } 1415423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 1416091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath} 1417091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath 141894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodvoid mkinnerdirs(char* path, int basepos, mode_t mode, int uid, int gid, 141994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood struct stat* statbuf) 142094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 142194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood while (path[basepos] != 0) { 142294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (path[basepos] == '/') { 142394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood path[basepos] = 0; 142494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (lstat(path, statbuf) < 0) { 142594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGV("Making directory: %s\n", path); 142694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (mkdir(path, mode) == 0) { 142794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood chown(path, uid, gid); 142894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else { 142994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGW("Unable to make directory %s: %s\n", path, strerror(errno)); 143094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 143194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 143294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood path[basepos] = '/'; 143394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood basepos++; 143494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 143594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood basepos++; 143694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 143794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 143894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 1439475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::linkNativeLibraryDirectory( 1440475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const std::unique_ptr<std::string>& uuid, const std::string& packageName, 1441475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const std::string& nativeLibPath32, int32_t userId) { 1442475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 1443423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 1444423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 1445423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 1446475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 1447475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* pkgname = packageName.c_str(); 1448475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* asecLibDir = nativeLibPath32.c_str(); 144994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood struct stat s, libStat; 1450423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey binder::Status res = ok(); 145194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 1452423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey auto _pkgdir = create_data_user_ce_package_path(uuid_, userId, pkgname); 1453423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey auto _libsymlink = _pkgdir + PKG_LIB_POSTFIX; 1454c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey 1455c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey const char* pkgdir = _pkgdir.c_str(); 1456c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey const char* libsymlink = _libsymlink.c_str(); 145794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 1458475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey if (stat(pkgdir, &s) < 0) { 1459423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to stat " + _pkgdir); 1460475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey } 146194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 146294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (chown(pkgdir, AID_INSTALL, AID_INSTALL) < 0) { 1463423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to chown " + _pkgdir); 146494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 146594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 146694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (chmod(pkgdir, 0700) < 0) { 1467423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to chmod " + _pkgdir); 146894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto out; 146994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 147094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 147194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (lstat(libsymlink, &libStat) < 0) { 147294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (errno != ENOENT) { 1473423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to stat " + _libsymlink); 147494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto out; 147594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 147694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else { 147794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (S_ISDIR(libStat.st_mode)) { 14783aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath if (delete_dir_contents(libsymlink, 1, NULL) < 0) { 1479423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to delete " + _libsymlink); 148094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto out; 148194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 148294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else if (S_ISLNK(libStat.st_mode)) { 148394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (unlink(libsymlink) < 0) { 1484423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to unlink " + _libsymlink); 148594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto out; 148694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 148794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 148894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 148994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 149094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (symlink(asecLibDir, libsymlink) < 0) { 1491423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to symlink " + _libsymlink + " to " + nativeLibPath32); 149294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto out; 149394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 149494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 149594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodout: 149694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (chmod(pkgdir, s.st_mode) < 0) { 1497423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey auto msg = "Failed to cleanup chmod " + _pkgdir; 1498423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (res.isOk()) { 1499423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error(msg); 1500423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } else { 1501423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey PLOG(ERROR) << msg; 1502423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 150394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 150494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 150594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (chown(pkgdir, s.st_uid, s.st_gid) < 0) { 1506423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey auto msg = "Failed to cleanup chown " + _pkgdir; 1507423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (res.isOk()) { 1508423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error(msg); 1509423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } else { 1510423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey PLOG(ERROR) << msg; 1511423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 151294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 151394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 1514423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return res; 151594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 151663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 151763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstadstatic void run_idmap(const char *target_apk, const char *overlay_apk, int idmap_fd) 151863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad{ 151963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad static const char *IDMAP_BIN = "/system/bin/idmap"; 152063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad static const size_t MAX_INT_LEN = 32; 152163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad char idmap_str[MAX_INT_LEN]; 152263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 152363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad snprintf(idmap_str, sizeof(idmap_str), "%d", idmap_fd); 152463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 152563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad execl(IDMAP_BIN, IDMAP_BIN, "--fd", target_apk, overlay_apk, idmap_str, (char*)NULL); 152663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("execl(%s) failed: %s\n", IDMAP_BIN, strerror(errno)); 152763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad} 152863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 152963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad// Transform string /a/b/c.apk to (prefix)/a@b@c.apk@(suffix) 153063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad// eg /a/b/c.apk to /data/resource-cache/a@b@c.apk@idmap 153163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstadstatic int flatten_path(const char *prefix, const char *suffix, 153263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad const char *overlay_path, char *idmap_path, size_t N) 153363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad{ 153463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (overlay_path == NULL || idmap_path == NULL) { 153563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad return -1; 153663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 153763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad const size_t len_overlay_path = strlen(overlay_path); 153863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad // will access overlay_path + 1 further below; requires absolute path 153963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (len_overlay_path < 2 || *overlay_path != '/') { 154063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad return -1; 154163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 154263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad const size_t len_idmap_root = strlen(prefix); 154363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad const size_t len_suffix = strlen(suffix); 154463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (SIZE_MAX - len_idmap_root < len_overlay_path || 154563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad SIZE_MAX - (len_idmap_root + len_overlay_path) < len_suffix) { 154663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad // additions below would cause overflow 154763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad return -1; 154863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 154963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (N < len_idmap_root + len_overlay_path + len_suffix) { 155063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad return -1; 155163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 155263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad memset(idmap_path, 0, N); 155363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad snprintf(idmap_path, N, "%s%s%s", prefix, overlay_path + 1, suffix); 155463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad char *ch = idmap_path + len_idmap_root; 155563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad while (*ch != '\0') { 155663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (*ch == '/') { 155763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad *ch = '@'; 155863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 155963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ++ch; 156063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 156163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad return 0; 156263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad} 156363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 1564475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::idmap(const std::string& targetApkPath, 1565475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const std::string& overlayApkPath, int32_t uid) { 1566475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 1567475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* target_apk = targetApkPath.c_str(); 1568475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* overlay_apk = overlayApkPath.c_str(); 156963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGV("idmap target_apk=%s overlay_apk=%s uid=%d\n", target_apk, overlay_apk, uid); 157063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 157163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad int idmap_fd = -1; 157263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad char idmap_path[PATH_MAX]; 157363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 157463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (flatten_path(IDMAP_PREFIX, IDMAP_SUFFIX, overlay_apk, 157563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad idmap_path, sizeof(idmap_path)) == -1) { 157663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("idmap cannot generate idmap path for overlay %s\n", overlay_apk); 157763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad goto fail; 157863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 157963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 158063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad unlink(idmap_path); 158163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad idmap_fd = open(idmap_path, O_RDWR | O_CREAT | O_EXCL, 0644); 158263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (idmap_fd < 0) { 158363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("idmap cannot open '%s' for output: %s\n", idmap_path, strerror(errno)); 158463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad goto fail; 158563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 158663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (fchown(idmap_fd, AID_SYSTEM, uid) < 0) { 158763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("idmap cannot chown '%s'\n", idmap_path); 158863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad goto fail; 158963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 159063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (fchmod(idmap_fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0) { 159163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("idmap cannot chmod '%s'\n", idmap_path); 159263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad goto fail; 159363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 159463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 159563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad pid_t pid; 159663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad pid = fork(); 159763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (pid == 0) { 159863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad /* child -- drop privileges before continuing */ 159963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (setgid(uid) != 0) { 160063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("setgid(%d) failed during idmap\n", uid); 160163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad exit(1); 160263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 160363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (setuid(uid) != 0) { 160463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("setuid(%d) failed during idmap\n", uid); 160563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad exit(1); 160663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 160763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (flock(idmap_fd, LOCK_EX | LOCK_NB) != 0) { 160863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("flock(%s) failed during idmap: %s\n", idmap_path, strerror(errno)); 160963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad exit(1); 161063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 161163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 161263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad run_idmap(target_apk, overlay_apk, idmap_fd); 161363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad exit(1); /* only if exec call to idmap failed */ 161463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } else { 161563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad int status = wait_child(pid); 161663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (status != 0) { 161763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("idmap failed, status=0x%04x\n", status); 161863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad goto fail; 161963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 162063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 162163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 162263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad close(idmap_fd); 1623423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 162463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstadfail: 162563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (idmap_fd >= 0) { 162663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad close(idmap_fd); 162763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad unlink(idmap_path); 162863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 1629423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error(); 163063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad} 1631e9887e46cea4a095e4219927eadbe4c57bb1a5eeRobert Craig 1632c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkeybinder::Status InstalldNativeService::restoreconAppData(const std::unique_ptr<std::string>& uuid, 1633c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const std::string& packageName, int32_t userId, int32_t flags, int32_t appId, 1634c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const std::string& seInfo) { 1635c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 1636423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 1637423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 1638423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 1639423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey binder::Status res = ok(); 1640e9887e46cea4a095e4219927eadbe4c57bb1a5eeRobert Craig 1641da30dc7336f03ca629fe173db1425fdce989119cRobert Craig // SELINUX_ANDROID_RESTORECON_DATADATA flag is set by libselinux. Not needed here. 1642c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey unsigned int seflags = SELINUX_ANDROID_RESTORECON_RECURSE; 1643c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 1644c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* pkgName = packageName.c_str(); 1645c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* seinfo = seInfo.c_str(); 1646e9887e46cea4a095e4219927eadbe4c57bb1a5eeRobert Craig 1647c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey uid_t uid = multiuser_get_uid(userId, appId); 1648aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (flags & FLAG_STORAGE_CE) { 1649c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey auto path = create_data_user_ce_package_path(uuid_, userId, pkgName); 1650c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey if (selinux_android_restorecon_pkgdir(path.c_str(), seinfo, uid, seflags) < 0) { 1651423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("restorecon failed for " + path); 1652da30dc7336f03ca629fe173db1425fdce989119cRobert Craig } 1653c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey } 1654aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (flags & FLAG_STORAGE_DE) { 1655c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey auto path = create_data_user_de_package_path(uuid_, userId, pkgName); 1656c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey if (selinux_android_restorecon_pkgdir(path.c_str(), seinfo, uid, seflags) < 0) { 1657423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("restorecon failed for " + path); 1658da30dc7336f03ca629fe173db1425fdce989119cRobert Craig } 1659da30dc7336f03ca629fe173db1425fdce989119cRobert Craig } 1660423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return res; 1661e9887e46cea4a095e4219927eadbe4c57bb1a5eeRobert Craig} 16623aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath 1663475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::createOatDir(const std::string& oatDir, 1664475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const std::string& instructionSet) { 1665475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 1666475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* oat_dir = oatDir.c_str(); 1667475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* instruction_set = instructionSet.c_str(); 166888ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov char oat_instr_dir[PKG_PATH_MAX]; 166988ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov 167088ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov if (validate_apk_path(oat_dir)) { 1671423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Invalid path " + oatDir); 167288ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov } 16738eed7e6a9bb527af7380ec13b390c6e2955eba6aFyodor Kupolov if (fs_prepare_dir(oat_dir, S_IRWXU | S_IRWXG | S_IXOTH, AID_SYSTEM, AID_INSTALL)) { 1674423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to prepare " + oatDir); 167588ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov } 167688ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov if (selinux_android_restorecon(oat_dir, 0)) { 1677423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to restorecon " + oatDir); 167888ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov } 167988ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov snprintf(oat_instr_dir, PKG_PATH_MAX, "%s/%s", oat_dir, instruction_set); 16808eed7e6a9bb527af7380ec13b390c6e2955eba6aFyodor Kupolov if (fs_prepare_dir(oat_instr_dir, S_IRWXU | S_IRWXG | S_IXOTH, AID_SYSTEM, AID_INSTALL)) { 1681423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error(StringPrintf("Failed to prepare %s", oat_instr_dir)); 168288ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov } 1683423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 168488ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov} 168588ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov 1686475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::rmPackageDir(const std::string& packageDir) { 1687475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 1688423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (validate_apk_path(packageDir.c_str())) { 1689423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Invalid path " + packageDir); 1690423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 1691423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (delete_dir_contents_and_dir(packageDir) != 0) { 1692423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to delete " + packageDir); 169388ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov } 1694423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 169588ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov} 169688ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov 1697475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::linkFile(const std::string& relativePath, 1698475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const std::string& fromBase, const std::string& toBase) { 1699475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 1700475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* relative_path = relativePath.c_str(); 1701475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* from_base = fromBase.c_str(); 1702475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* to_base = toBase.c_str(); 1703d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath char from_path[PKG_PATH_MAX]; 1704d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath char to_path[PKG_PATH_MAX]; 1705d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath snprintf(from_path, PKG_PATH_MAX, "%s/%s", from_base, relative_path); 1706d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath snprintf(to_path, PKG_PATH_MAX, "%s/%s", to_base, relative_path); 1707d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath 1708d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath if (validate_apk_path_subdirs(from_path)) { 1709423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error(StringPrintf("Invalid from path %s", from_path)); 1710d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath } 1711d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath 1712d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath if (validate_apk_path_subdirs(to_path)) { 1713423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error(StringPrintf("Invalid to path %s", to_path)); 1714d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath } 1715d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath 1716423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (link(from_path, to_path) < 0) { 1717423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error(StringPrintf("Failed to link from %s to %s", from_path, to_path)); 1718d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath } 1719d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath 1720423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 1721d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath} 1722d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath 1723475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::moveAb(const std::string& apkPath, 1724475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const std::string& instructionSet, const std::string& outputPath) { 1725475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 172690aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey 1727475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* apk_path = apkPath.c_str(); 1728475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* instruction_set = instructionSet.c_str(); 1729475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* oat_dir = outputPath.c_str(); 1730d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe 173190aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey bool success = move_ab(apk_path, instruction_set, oat_dir); 1732423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return success ? ok() : error(); 1733e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe} 1734e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe 1735475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::deleteOdex(const std::string& apkPath, 1736475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const std::string& instructionSet, const std::string& outputPath) { 1737475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 173890aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey 1739475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* apk_path = apkPath.c_str(); 1740475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* instruction_set = instructionSet.c_str(); 1741475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* oat_dir = outputPath.c_str(); 1742475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey 174390aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey bool res = delete_odex(apk_path, instruction_set, oat_dir); 174490aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey return res ? ok() : error(); 1745b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampe} 1746b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampe 174702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe} // namespace installd 174802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe} // namespace android 1749