InstalldNativeService.cpp revision df2d754b38796e0c49c70e0a67f7d383e3079ff2
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> 4202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <cutils/log.h> // TODO: Move everything to base/logging. 4390aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey#include <cutils/properties.h> 440378aaf257aee92539d30543914a50c4481c6a18Brian Carlstrom#include <cutils/sched_policy.h> 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 2903dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (st.st_uid != uid) { 2913dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // Mismatched UID is real trouble; we can't recover 2923dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey LOG(ERROR) << "Mismatched UID at " << path << ": found " << st.st_uid 2933dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey << " but expected " << uid; 2943dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey return -1; 2953dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } else if (st.st_gid == gid && st.st_mode == target_mode) { 2963dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // Everything looks good! 2973dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey return 0; 2983dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 2993dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 3003dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // Directory is owned correctly, but GID or mode mismatch means it's 3013dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // probably a platform upgrade so we need to fix them 3023dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey FTS *fts; 3033dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey FTSENT *p; 3043dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey char *argv[] = { (char*) path.c_str(), nullptr }; 3053dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_XDEV, NULL))) { 3063dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey PLOG(ERROR) << "Failed to fts_open " << path; 3073dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey return -1; 3083dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 3093dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey while ((p = fts_read(fts)) != NULL) { 3103dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey switch (p->fts_info) { 3113dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey case FTS_DP: 3123dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (chmod(p->fts_accpath, target_mode) != 0) { 3133dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey PLOG(WARNING) << "Failed to chmod " << p->fts_path; 3143dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 3153dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // Intentional fall through to also set GID 3163dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey case FTS_F: 3173dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (chown(p->fts_accpath, -1, gid) != 0) { 3183dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey PLOG(WARNING) << "Failed to chown " << p->fts_path; 3193dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 3203dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey break; 3213dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey case FTS_SL: 3223dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey case FTS_SLNONE: 3233dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (lchown(p->fts_accpath, -1, gid) != 0) { 3243dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey PLOG(WARNING) << "Failed to chown " << p->fts_path; 3253dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 3263dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey break; 3273dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 3283dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 3293dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey fts_close(fts); 3303dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey return 0; 3319a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey} 3329a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey 3330274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeybinder::Status InstalldNativeService::createAppData(const std::unique_ptr<std::string>& uuid, 3340274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const std::string& packageName, int32_t userId, int32_t flags, int32_t appId, 33536a900a0db526d63e899ed5d5b0a463ad7f9236eJeff Sharkey const std::string& seInfo, int32_t targetSdkVersion, int64_t* _aidl_return) { 3360274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 337423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 338423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 3390274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 3400274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 3410274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const char* pkgname = packageName.c_str(); 3420274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 34336a900a0db526d63e899ed5d5b0a463ad7f9236eJeff Sharkey // Assume invalid inode unless filled in below 34436a900a0db526d63e899ed5d5b0a463ad7f9236eJeff Sharkey if (_aidl_return != nullptr) *_aidl_return = -1; 34536a900a0db526d63e899ed5d5b0a463ad7f9236eJeff Sharkey 3460274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey uid_t uid = multiuser_get_uid(userId, appId); 3473dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey gid_t cacheGid = multiuser_get_cache_gid(userId, appId); 3483dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey mode_t targetMode = targetSdkVersion >= MIN_RESTRICTED_HOME_SDK_VERSION ? 0700 : 0751; 3493dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 350aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (flags & FLAG_STORAGE_CE) { 3510274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey auto path = create_data_user_ce_package_path(uuid_, userId, pkgname); 35235b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey bool existing = (access(path.c_str(), F_OK) == 0); 35335b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey 3543dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (prepare_app_dir(path, targetMode, uid) || 3553dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey prepare_app_cache_dir(path, "cache", 02771, uid, cacheGid) || 3563dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey prepare_app_cache_dir(path, "code_cache", 02771, uid, cacheGid)) { 357423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to prepare " + path); 3587db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey } 3597db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey 3607db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey // Consider restorecon over contents if label changed 36135b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey if (restorecon_app_data_lazy(path, seInfo, uid, existing) || 36235b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey restorecon_app_data_lazy(path, "cache", seInfo, uid, existing) || 36335b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey restorecon_app_data_lazy(path, "code_cache", seInfo, uid, existing)) { 364423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to restorecon " + path); 365c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey } 3669a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey 3679a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey // Remember inode numbers of cache directories so that we can clear 3689a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey // contents while CE storage is locked 3699a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey if (write_path_inode(path, "cache", kXattrInodeCache) || 3709a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey write_path_inode(path, "code_cache", kXattrInodeCodeCache)) { 371423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to write_path_inode for " + path); 372c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey } 37336a900a0db526d63e899ed5d5b0a463ad7f9236eJeff Sharkey 37436a900a0db526d63e899ed5d5b0a463ad7f9236eJeff Sharkey // And return the CE inode of the top-level data directory so we can 37536a900a0db526d63e899ed5d5b0a463ad7f9236eJeff Sharkey // clear contents while CE storage is locked 37636a900a0db526d63e899ed5d5b0a463ad7f9236eJeff Sharkey if ((_aidl_return != nullptr) 37736a900a0db526d63e899ed5d5b0a463ad7f9236eJeff Sharkey && get_path_inode(path, reinterpret_cast<ino_t*>(_aidl_return)) != 0) { 37836a900a0db526d63e899ed5d5b0a463ad7f9236eJeff Sharkey return error("Failed to get_path_inode for " + path); 37936a900a0db526d63e899ed5d5b0a463ad7f9236eJeff Sharkey } 38094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 381aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (flags & FLAG_STORAGE_DE) { 3820274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey auto path = create_data_user_de_package_path(uuid_, userId, pkgname); 38335b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey bool existing = (access(path.c_str(), F_OK) == 0); 38435b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey 3853dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (prepare_app_dir(path, targetMode, uid) || 3863dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey prepare_app_cache_dir(path, "cache", 02771, uid, cacheGid) || 3873dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey prepare_app_cache_dir(path, "code_cache", 02771, uid, cacheGid)) { 388423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to prepare " + path); 389c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey } 3906a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 3917db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey // Consider restorecon over contents if label changed 39235b83dff3de2b5fe781a060566ee09266b3c2617Jeff Sharkey if (restorecon_app_data_lazy(path, seInfo, uid, existing)) { 393423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to restorecon " + path); 3947db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey } 3957db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey 39690aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey if (property_get_bool("dalvik.vm.usejitprofiles", false)) { 3970274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const std::string profile_path = create_data_user_profile_package_path(userId, pkgname); 3986a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle // read-write-execute only for the app user. 3996a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle if (fs_prepare_dir_strict(profile_path.c_str(), 0700, uid, uid) != 0) { 400423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to prepare " + profile_path); 4016a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 402bc56e7dcf07e633867e1525435373a1231216b22Calin Juravle std::string profile_file = create_primary_profile(profile_path); 403bc56e7dcf07e633867e1525435373a1231216b22Calin Juravle // read-write only for the app user. 404bc56e7dcf07e633867e1525435373a1231216b22Calin Juravle if (fs_prepare_file_strict(profile_file.c_str(), 0600, uid, uid) != 0) { 405423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to prepare " + profile_path); 406bc56e7dcf07e633867e1525435373a1231216b22Calin Juravle } 4076a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle const std::string ref_profile_path = create_data_ref_profile_package_path(pkgname); 4086a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle // dex2oat/profman runs under the shared app gid and it needs to read/write reference 4096a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle // profiles. 410adddd9876e145879169369913b5afe469b17c02dJeff Sharkey int shared_app_gid = multiuser_get_shared_gid(0, appId); 4112e6dc9baaaf5d50b42ce36435e13c2559808b837Jeff Sharkey if ((shared_app_gid != -1) && fs_prepare_dir_strict( 4126a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle ref_profile_path.c_str(), 0700, shared_app_gid, shared_app_gid) != 0) { 413423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to prepare " + ref_profile_path); 4146a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 4156a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 41694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 417423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 41894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 41994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 420c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkeybinder::Status InstalldNativeService::migrateAppData(const std::unique_ptr<std::string>& uuid, 421c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const std::string& packageName, int32_t userId, int32_t flags) { 422c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 423423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 424423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 425423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 426c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 427c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* pkgname = packageName.c_str(); 428c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey 429cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey // This method only exists to upgrade system apps that have requested 430cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey // forceDeviceEncrypted, so their default storage always lives in a 431cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey // consistent location. This only works on non-FBE devices, since we 432cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey // never want to risk exposing data on a device with real CE/DE storage. 433cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey 434c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey auto ce_path = create_data_user_ce_package_path(uuid_, userId, pkgname); 435c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey auto de_path = create_data_user_de_package_path(uuid_, userId, pkgname); 436cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey 437cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey // If neither directory is marked as default, assume CE is default 438cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey if (getxattr(ce_path.c_str(), kXattrDefault, nullptr, 0) == -1 439cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey && getxattr(de_path.c_str(), kXattrDefault, nullptr, 0) == -1) { 440cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey if (setxattr(ce_path.c_str(), kXattrDefault, nullptr, 0, 0) != 0) { 441423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to mark default storage " + ce_path); 442cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey } 443cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey } 444cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey 445cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey // Migrate default data location if needed 446cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey auto target = (flags & FLAG_STORAGE_DE) ? de_path : ce_path; 447cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey auto source = (flags & FLAG_STORAGE_DE) ? ce_path : de_path; 448cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey 449cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey if (getxattr(target.c_str(), kXattrDefault, nullptr, 0) == -1) { 450cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey LOG(WARNING) << "Requested default storage " << target 451cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey << " is not active; migrating from " << source; 452cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey if (delete_dir_contents_and_dir(target) != 0) { 453423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to delete " + target); 454cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey } 455cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey if (rename(source.c_str(), target.c_str()) != 0) { 456423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to rename " + source + " to " + target); 457cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey } 458cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey } 459cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey 460423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 461cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey} 462cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey 4636a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 464475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::clearAppProfiles(const std::string& packageName) { 465475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 466423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 467423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 468475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* pkgname = packageName.c_str(); 469423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey binder::Status res = ok(); 470423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (!clear_reference_profile(pkgname)) { 471423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to clear reference profile for " + packageName); 472423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 473423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (!clear_current_profiles(pkgname)) { 474423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to clear current profiles for " + packageName); 475423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 476423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return res; 4776a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle} 4786a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 479c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkeybinder::Status InstalldNativeService::clearAppData(const std::unique_ptr<std::string>& uuid, 480c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const std::string& packageName, int32_t userId, int32_t flags, int64_t ceDataInode) { 481c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 482423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 483423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 484423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 485c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 486c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* pkgname = packageName.c_str(); 487c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey 488423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey binder::Status res = ok(); 489aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (flags & FLAG_STORAGE_CE) { 490c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey auto path = create_data_user_ce_package_path(uuid_, userId, pkgname, ceDataInode); 4919a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey if (flags & FLAG_CLEAR_CACHE_ONLY) { 4929a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey path = read_path_inode(path, "cache", kXattrInodeCache); 4939a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey } else if (flags & FLAG_CLEAR_CODE_CACHE_ONLY) { 4949a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey path = read_path_inode(path, "code_cache", kXattrInodeCodeCache); 4959a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey } 496c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey if (access(path.c_str(), F_OK) == 0) { 497423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (delete_dir_contents(path) != 0) { 498423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to delete contents of " + path); 499423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 500c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey } 50194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 502aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (flags & FLAG_STORAGE_DE) { 5039a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey std::string suffix = ""; 5049a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey bool only_cache = false; 5059a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey if (flags & FLAG_CLEAR_CACHE_ONLY) { 5069a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey suffix = CACHE_DIR_POSTFIX; 5079a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey only_cache = true; 5089a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey } else if (flags & FLAG_CLEAR_CODE_CACHE_ONLY) { 5099a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey suffix = CODE_CACHE_DIR_POSTFIX; 5109a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey only_cache = true; 5119a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey } 5129a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey 513c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey auto path = create_data_user_de_package_path(uuid_, userId, pkgname) + suffix; 514c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey if (access(path.c_str(), F_OK) == 0) { 515423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (delete_dir_contents(path) != 0) { 516423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to delete contents of " + path); 517423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 518c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey } 519edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle if (!only_cache) { 520c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey if (!clear_current_profile(pkgname, userId)) { 521423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to clear current profile for " + packageName); 522edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle } 523edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle } 52494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 525423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return res; 526c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey} 52794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 5287535e8e1995398befc866062d63c05d90eb70cccCalin Juravlestatic int destroy_app_reference_profile(const char *pkgname) { 5297535e8e1995398befc866062d63c05d90eb70cccCalin Juravle return delete_dir_contents_and_dir( 5307535e8e1995398befc866062d63c05d90eb70cccCalin Juravle create_data_ref_profile_package_path(pkgname), 5317535e8e1995398befc866062d63c05d90eb70cccCalin Juravle /*ignore_if_missing*/ true); 5327535e8e1995398befc866062d63c05d90eb70cccCalin Juravle} 5337535e8e1995398befc866062d63c05d90eb70cccCalin Juravle 534edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravlestatic int destroy_app_current_profiles(const char *pkgname, userid_t userid) { 535b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravle return delete_dir_contents_and_dir( 536b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravle create_data_user_profile_package_path(userid, pkgname), 537b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravle /*ignore_if_missing*/ true); 538edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle} 539edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle 540475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::destroyAppProfiles(const std::string& packageName) { 541475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 542423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 543423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 544475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* pkgname = packageName.c_str(); 545423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey binder::Status res = ok(); 546edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle std::vector<userid_t> users = get_known_users(/*volume_uuid*/ nullptr); 547edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle for (auto user : users) { 548423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (destroy_app_current_profiles(pkgname, user) != 0) { 549423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to destroy current profiles for " + packageName); 550423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 551edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle } 552423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (destroy_app_reference_profile(pkgname) != 0) { 553423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to destroy reference profile for " + packageName); 554423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 555423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return res; 556caa6b809a5ed024a8f3f551c5c109a5ad50ab772Calin Juravle} 557caa6b809a5ed024a8f3f551c5c109a5ad50ab772Calin Juravle 558c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkeybinder::Status InstalldNativeService::destroyAppData(const std::unique_ptr<std::string>& uuid, 559c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const std::string& packageName, int32_t userId, int32_t flags, int64_t ceDataInode) { 560c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 561423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 562423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 563423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 564c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 565c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* pkgname = packageName.c_str(); 566c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey 567423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey binder::Status res = ok(); 568aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (flags & FLAG_STORAGE_CE) { 569423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey auto path = create_data_user_ce_package_path(uuid_, userId, pkgname, ceDataInode); 570423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (delete_dir_contents_and_dir(path) != 0) { 571423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to delete " + path); 572423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 57394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 574aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (flags & FLAG_STORAGE_DE) { 575423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey auto path = create_data_user_de_package_path(uuid_, userId, pkgname); 576423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (delete_dir_contents_and_dir(path) != 0) { 577423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to delete " + path); 578423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 579c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey destroy_app_current_profiles(pkgname, userId); 5807535e8e1995398befc866062d63c05d90eb70cccCalin Juravle // TODO(calin): If the package is still installed by other users it's probably 5817535e8e1995398befc866062d63c05d90eb70cccCalin Juravle // beneficial to keep the reference profile around. 5827535e8e1995398befc866062d63c05d90eb70cccCalin Juravle // Verify if it's ok to do that. 5837535e8e1995398befc866062d63c05d90eb70cccCalin Juravle destroy_app_reference_profile(pkgname); 58494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 585423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return res; 58694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 58794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 5880274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeybinder::Status InstalldNativeService::moveCompleteApp(const std::unique_ptr<std::string>& fromUuid, 5890274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const std::unique_ptr<std::string>& toUuid, const std::string& packageName, 5900274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const std::string& dataAppName, int32_t appId, const std::string& seInfo, 5910274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey int32_t targetSdkVersion) { 592423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 593423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(fromUuid); 594423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(toUuid); 595423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 5960274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 5970274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const char* from_uuid = fromUuid ? fromUuid->c_str() : nullptr; 5980274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const char* to_uuid = toUuid ? toUuid->c_str() : nullptr; 5990274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const char* package_name = packageName.c_str(); 6000274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const char* data_app_name = dataAppName.c_str(); 6010274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 602423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey binder::Status res = ok(); 603e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey std::vector<userid_t> users = get_known_users(from_uuid); 604e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 605d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey // Copy app 606d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey { 60751c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto from = create_data_app_package_path(from_uuid, data_app_name); 60851c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto to = create_data_app_package_path(to_uuid, data_app_name); 60951c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto to_parent = create_data_app_path(to_uuid); 610d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey 611d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey char *argv[] = { 612d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey (char*) kCpPath, 613d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey (char*) "-F", /* delete any existing destination file first (--remove-destination) */ 614d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey (char*) "-p", /* preserve timestamps, ownership, and permissions */ 615d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey (char*) "-R", /* recurse into subdirectories (DEST must be a directory) */ 616d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey (char*) "-P", /* Do not follow symlinks [default] */ 617d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey (char*) "-d", /* don't dereference symlinks */ 618d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey (char*) from.c_str(), 619d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey (char*) to_parent.c_str() 620d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey }; 621d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey 622d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey LOG(DEBUG) << "Copying " << from << " to " << to; 623d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true); 624d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey if (rc != 0) { 625423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error(rc, "Failed copying " + from + " to " + to); 626d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey goto fail; 627d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey } 628d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey 629d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey if (selinux_android_restorecon(to.c_str(), SELINUX_ANDROID_RESTORECON_RECURSE) != 0) { 630423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to restorecon " + to); 631d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey goto fail; 632d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey } 633d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey } 634d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey 635d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey // Copy private data for all known users 636e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey for (auto user : users) { 637e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 638e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey // Data source may not exist for all users; that's okay 63951c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto from_ce = create_data_user_ce_package_path(from_uuid, user, package_name); 64051c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey if (access(from_ce.c_str(), F_OK) != 0) { 64151c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey LOG(INFO) << "Missing source " << from_ce; 642e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey continue; 643e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey } 644e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 6450274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey if (!createAppData(toUuid, packageName, user, FLAG_STORAGE_CE | FLAG_STORAGE_DE, appId, 64636a900a0db526d63e899ed5d5b0a463ad7f9236eJeff Sharkey seInfo, targetSdkVersion, nullptr).isOk()) { 647423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to create package target"); 648e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey goto fail; 649e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey } 650e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 651e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey char *argv[] = { 652e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey (char*) kCpPath, 653e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey (char*) "-F", /* delete any existing destination file first (--remove-destination) */ 654e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey (char*) "-p", /* preserve timestamps, ownership, and permissions */ 655e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey (char*) "-R", /* recurse into subdirectories (DEST must be a directory) */ 656e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey (char*) "-P", /* Do not follow symlinks [default] */ 657e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey (char*) "-d", /* don't dereference symlinks */ 65851c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey nullptr, 65951c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey nullptr 660e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey }; 661e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 66251c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey { 66351c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto from = create_data_user_de_package_path(from_uuid, user, package_name); 66451c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto to = create_data_user_de_path(to_uuid, user); 66551c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey argv[6] = (char*) from.c_str(); 66651c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey argv[7] = (char*) to.c_str(); 66751c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey 66851c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey LOG(DEBUG) << "Copying " << from << " to " << to; 66951c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true); 67051c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey if (rc != 0) { 671423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error(rc, "Failed copying " + from + " to " + to); 67251c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey goto fail; 67351c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey } 67451c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey } 67551c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey { 67651c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto from = create_data_user_ce_package_path(from_uuid, user, package_name); 67751c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto to = create_data_user_ce_path(to_uuid, user); 67851c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey argv[6] = (char*) from.c_str(); 67951c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey argv[7] = (char*) to.c_str(); 68051c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey 68151c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey LOG(DEBUG) << "Copying " << from << " to " << to; 68251c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true); 68351c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey if (rc != 0) { 684423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error(rc, "Failed copying " + from + " to " + to); 68551c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey goto fail; 68651c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey } 687e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey } 688e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 689c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey if (!restoreconAppData(toUuid, packageName, user, FLAG_STORAGE_CE | FLAG_STORAGE_DE, 690c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey appId, seInfo).isOk()) { 691423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to restorecon"); 692c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey goto fail; 693c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey } 694e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey } 695e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 69631f08986f83fa6f2dcf55523b2cf706460aeed7cJeff Sharkey // We let the framework scan the new location and persist that before 69731f08986f83fa6f2dcf55523b2cf706460aeed7cJeff Sharkey // deleting the data in the old location; this ordering ensures that 69831f08986f83fa6f2dcf55523b2cf706460aeed7cJeff Sharkey // we can recover from things like battery pulls. 699423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 700e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 701e36372423000a906bafae68844ebc6c42d09335aJeff Sharkeyfail: 702e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey // Nuke everything we might have already copied 703d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey { 70451c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto to = create_data_app_package_path(to_uuid, data_app_name); 705d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey if (delete_dir_contents(to.c_str(), 1, NULL) != 0) { 706d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey LOG(WARNING) << "Failed to rollback " << to; 707d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey } 708d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey } 709e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey for (auto user : users) { 71051c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey { 71151c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto to = create_data_user_de_package_path(to_uuid, user, package_name); 71251c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey if (delete_dir_contents(to.c_str(), 1, NULL) != 0) { 71351c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey LOG(WARNING) << "Failed to rollback " << to; 71451c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey } 71551c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey } 71651c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey { 71751c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto to = create_data_user_ce_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 } 721e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey } 722e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey } 723423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return res; 724e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey} 725e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 726c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkeybinder::Status InstalldNativeService::createUserData(const std::unique_ptr<std::string>& uuid, 727c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey int32_t userId, int32_t userSerial ATTRIBUTE_UNUSED, int32_t flags) { 728c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 729423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 730423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 731c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 732379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey if (flags & FLAG_STORAGE_DE) { 733c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey if (uuid_ == nullptr) { 734423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (ensure_config_user_dirs(userId) != 0) { 7357be5ead81aa4d3b2536556c762bab03445a98155Jeff Sharkey return error(StringPrintf("Failed to ensure dirs for %d", userId)); 736423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 737379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey } 73894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 7397be5ead81aa4d3b2536556c762bab03445a98155Jeff Sharkey return ok(); 740095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee} 741095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee 742c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkeybinder::Status InstalldNativeService::destroyUserData(const std::unique_ptr<std::string>& uuid, 743c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey int32_t userId, int32_t flags) { 744c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 745423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 746423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 747c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 748423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey binder::Status res = ok(); 749379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey if (flags & FLAG_STORAGE_DE) { 750423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey auto path = create_data_user_de_path(uuid_, userId); 751423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (delete_dir_contents_and_dir(path, true) != 0) { 752423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to delete " + path); 753423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 754c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey if (uuid_ == nullptr) { 755423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey path = create_data_misc_legacy_path(userId); 756423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (delete_dir_contents_and_dir(path, true) != 0) { 757423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to delete " + path); 758423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 759df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey path = create_data_user_profile_path(userId); 760423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (delete_dir_contents_and_dir(path, true) != 0) { 761423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to delete " + path); 762423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 76341ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey } 76494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 765379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey if (flags & FLAG_STORAGE_CE) { 766423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey auto path = create_data_user_ce_path(uuid_, userId); 767423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (delete_dir_contents_and_dir(path, true) != 0) { 768423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to delete " + path); 769423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 770423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey path = create_data_media_path(uuid_, userId); 771423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (delete_dir_contents_and_dir(path, true) != 0) { 772423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to delete " + path); 773423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 774379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey } 775423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return res; 77694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 77794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 77894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/* Try to ensure free_size bytes of storage are available. 77994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * Returns 0 on success. 78094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * This is rather simple-minded because doing a full LRU would 78194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * be potentially memory-intensive, and without atime it would 78294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * also require that apps constantly modify file metadata even 78394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * when just reading from the cache, which is pretty awful. 78494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */ 785475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::freeCache(const std::unique_ptr<std::string>& uuid, 786475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey int64_t freeStorageSize) { 787475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 788423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 789423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 790475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 79194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache_t* cache; 79294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int64_t avail; 79394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 794475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey auto data_path = create_data_path(uuid_); 79541ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey 79641ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey avail = data_disk_free(data_path); 797475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey if (avail < 0) { 798423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to determine free space for " + data_path); 799475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey } 80094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 801475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ALOGI("free_cache(%" PRId64 ") avail %" PRId64 "\n", freeStorageSize, avail); 802475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey if (avail >= freeStorageSize) { 803423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 804475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey } 80594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 80694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache = start_cache_collection(); 80794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 808475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey auto users = get_known_users(uuid_); 80954e292e1ea87b504b552393d2e9bc800458bd0c1Jeff Sharkey for (auto user : users) { 810475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey add_cache_files(cache, create_data_user_ce_path(uuid_, user)); 811475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey add_cache_files(cache, create_data_user_de_path(uuid_, user)); 81254e292e1ea87b504b552393d2e9bc800458bd0c1Jeff Sharkey add_cache_files(cache, 813475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey StringPrintf("%s/Android/data", create_data_media_path(uuid_, user).c_str())); 81494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 81594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 816475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey clear_cache_files(data_path, cache, freeStorageSize); 81794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood finish_cache_collection(cache); 81894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 819423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey avail = data_disk_free(data_path); 820423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (avail >= freeStorageSize) { 821423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 822475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey } else { 823423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error(StringPrintf("Failed to free up %" PRId64 " on %s; final free space %" PRId64, 824423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey freeStorageSize, data_path.c_str(), avail)); 825475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey } 82694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 82794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 828475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::rmdex(const std::string& codePath, 829475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const std::string& instructionSet) { 830475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 83194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood char dex_path[PKG_PATH_MAX]; 83294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 833475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* path = codePath.c_str(); 834475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* instruction_set = instructionSet.c_str(); 835475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey 836770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey if (validate_apk_path(path) && validate_system_app_path(path)) { 837423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Invalid path " + codePath); 838770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey } 839770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey 840475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey if (!create_cache_path(dex_path, path, instruction_set)) { 841423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to create cache path for " + codePath); 842475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey } 84394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 84494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGV("unlink %s\n", dex_path); 84594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (unlink(dex_path) < 0) { 846423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error(StringPrintf("Failed to unlink %s", dex_path)); 84794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else { 848423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 84994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 85094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 85194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 8523dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkeystruct stats { 8533dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey int64_t codeSize; 8543dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey int64_t dataSize; 8553dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey int64_t cacheSize; 8563dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey}; 8573dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 858df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 859df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkeystatic std::string toString(std::vector<int64_t> values) { 860df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey std::stringstream res; 861df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey res << "["; 862df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey for (size_t i = 0; i < values.size(); i++) { 863df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey res << values[i]; 864df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (i < values.size() - 1) { 865df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey res << ","; 866df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 867df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 868df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey res << "]"; 869df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey return res.str(); 870df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey} 871df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 8723dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 873df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkeystatic std::string findDeviceForUuid(const std::unique_ptr<std::string>& uuid) { 8743dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey auto path = create_data_path(uuid ? uuid->c_str() : nullptr); 875df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey std::ifstream in("/proc/mounts"); 876df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (!in.is_open()) { 877df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey PLOG(ERROR) << "Failed to read mounts"; 878df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey return ""; 8793dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 880df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey std::string source; 881df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey std::string target; 882df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey while (!in.eof()) { 883df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey std::getline(in, source, ' '); 884df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey std::getline(in, target, ' '); 885df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (target == path) { 886df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey return source; 887df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 888df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // Skip to next line 889df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey std::getline(in, source); 8903dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 891df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey PLOG(ERROR) << "Failed to resolve block device for " << path; 892df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey return ""; 893df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey} 894df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 895df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkeystatic void collectQuotaStats(const std::string& device, int32_t userId, 896df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int32_t appId, struct stats* stats, struct stats* extStats ATTRIBUTE_UNUSED) { 897df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (device.empty()) return; 898df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 899df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey struct dqblk dq; 9003dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 9013dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey uid_t uid = multiuser_get_uid(userId, appId); 9023dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (quotactl(QCMD(Q_GETQUOTA, USRQUOTA), device.c_str(), uid, 9033dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey reinterpret_cast<char*>(&dq)) != 0) { 9043dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (errno != ESRCH) { 9053dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey PLOG(ERROR) << "Failed to quotactl " << device << " for UID " << uid; 9063dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 9073dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } else { 908df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 909df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey LOG(DEBUG) << "quotactl() for UID " << uid << " " << dq.dqb_curspace; 910df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 9113dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey stats->dataSize += dq.dqb_curspace; 9123dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 9133dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 9143dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey int cacheGid = multiuser_get_cache_gid(userId, appId); 9153dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (cacheGid != -1) { 9163dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), cacheGid, 9173dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey reinterpret_cast<char*>(&dq)) != 0) { 9183dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (errno != ESRCH) { 9193dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey PLOG(ERROR) << "Failed to quotactl " << device << " for GID " << cacheGid; 9203dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 9213dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } else { 922df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 923df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey LOG(DEBUG) << "quotactl() for GID " << cacheGid << " " << dq.dqb_curspace; 924df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 9253dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey stats->cacheSize += dq.dqb_curspace; 9263dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 9273dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 9283dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 9293dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey int sharedGid = multiuser_get_shared_app_gid(uid); 9303dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (sharedGid != -1) { 9313dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), sharedGid, 9323dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey reinterpret_cast<char*>(&dq)) != 0) { 9333dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (errno != ESRCH) { 9343dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey PLOG(ERROR) << "Failed to quotactl " << device << " for GID " << sharedGid; 9353dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 9363dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } else { 937df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 938df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey LOG(DEBUG) << "quotactl() for GID " << sharedGid << " " << dq.dqb_curspace; 939df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 9403dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey stats->codeSize += dq.dqb_curspace; 9413dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 9423dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 943df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 944df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_EXTERNAL 945df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // TODO: measure using external GIDs 946df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 947df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey} 948df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 949df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkeystatic void collectQuotaStats(const std::unique_ptr<std::string>& uuid, int32_t userId, 950df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int32_t appId, struct stats* stats, struct stats* extStats) { 951df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey collectQuotaStats(findDeviceForUuid(uuid), userId, appId, stats, extStats); 9523dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey} 9533dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 954df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkeystatic void collectManualStats(const std::string& path, struct stats* stats) { 95594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood DIR *d; 95694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int dfd; 95794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood struct dirent *de; 95894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood struct stat s; 959d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey 9602f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey d = opendir(path.c_str()); 9612f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey if (d == nullptr) { 9623dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (errno != ENOENT) { 9633dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey PLOG(WARNING) << "Failed to open " << path; 9643dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 9652f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey return; 96694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 9672f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey dfd = dirfd(d); 9682f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey while ((de = readdir(d))) { 9692f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey const char *name = de->d_name; 970d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey 9713dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey int64_t size = 0; 9722f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) { 9733dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey size = s.st_blocks * 512; 97494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 97594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 9762f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey if (de->d_type == DT_DIR) { 9773dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (!strcmp(name, ".")) { 9783dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // Don't recurse, but still count node size 9793dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } else if (!strcmp(name, "..")) { 9803dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // Don't recurse or count node size 9813dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey continue; 9823dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } else { 9833dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // Measure all children nodes 9843dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey size = 0; 9853dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey calculate_tree_size(StringPrintf("%s/%s", path.c_str(), name), &size); 9862f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey } 9873dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 9882f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey if (!strcmp(name, "cache") || !strcmp(name, "code_cache")) { 9893dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey stats->cacheSize += size; 9902f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey } 99194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 9923dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 9933dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // Legacy symlink isn't owned by app 9943dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (de->d_type == DT_LNK && !strcmp(name, "lib")) { 9953dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey continue; 9963dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 9973dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 9983dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // Everything found inside is considered data 9993dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey stats->dataSize += size; 100094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 10012f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey closedir(d); 10022f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey} 100394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 1004df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkeystatic void collectManualStatsForUser(const std::string& path, struct stats* stats, 1005df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey bool exclude_apps = false) { 1006df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey DIR *d; 1007df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int dfd; 1008df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey struct dirent *de; 1009df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey struct stat s; 1010df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1011df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey d = opendir(path.c_str()); 1012df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (d == nullptr) { 1013df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (errno != ENOENT) { 1014df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey PLOG(WARNING) << "Failed to open " << path; 1015df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1016df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey return; 1017df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1018df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey dfd = dirfd(d); 1019df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey while ((de = readdir(d))) { 1020df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (de->d_type == DT_DIR) { 1021df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey const char *name = de->d_name; 1022df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) != 0) { 1023df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey continue; 1024df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1025df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (!strcmp(name, ".") || !strcmp(name, "..")) { 1026df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey continue; 1027df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } else if (exclude_apps && (s.st_uid >= AID_APP_START && s.st_uid <= AID_APP_END)) { 1028df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey continue; 1029df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } else { 1030df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey collectManualStats(StringPrintf("%s/%s", path.c_str(), name), stats); 1031df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1032df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1033df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1034df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey closedir(d); 1035df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey} 1036df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 10376c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkeybinder::Status InstalldNativeService::getAppSize(const std::unique_ptr<std::string>& uuid, 1038df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey const std::vector<std::string>& packageNames, int32_t userId, int32_t flags, 1039df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int32_t appId, const std::vector<int64_t>& ceDataInodes, 1040df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey const std::vector<std::string>& codePaths, std::vector<int64_t>* _aidl_return) { 10416c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 1042423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 1043df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey for (auto packageName : packageNames) { 1044df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 1045df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1046423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 1047df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // When modifying this logic, always verify using tests: 1048df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // runtest -x frameworks/base/services/tests/servicestests/src/com/android/server/pm/InstallerTest.java -m testGetAppSize 1049df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1050df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 1051df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey LOG(INFO) << "Measuring user " << userId << " app " << appId; 1052df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 10536c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey 10543dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // Here's a summary of the common storage locations across the platform, 10553dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // and how they're each tagged: 10563dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // 10573dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // /data/app/com.example UID system 10583dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey // /data/app/com.example/oat UID system 1059df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // /data/user/0/com.example UID u0_a10 GID u0_a10 1060df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // /data/user/0/com.example/cache UID u0_a10 GID u0_a10_cache 1061df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // /data/media/0/foo.txt UID u0_media_rw 1062df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // /data/media/0/bar.jpg UID u0_media_rw GID u0_media_image 1063df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // /data/media/0/Android/data/com.example UID u0_media_rw GID u0_a10_ext 1064df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // /data/media/0/Android/data/com.example/cache UID u0_media_rw GID u0_a10_ext_cache 1065df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // /data/media/obb/com.example UID system 10663dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 10673dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey struct stats stats; 1068df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey struct stats extStats; 10693dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey memset(&stats, 0, sizeof(stats)); 1070df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey memset(&extStats, 0, sizeof(extStats)); 10713dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 1072df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 10733dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 1074df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey for (auto packageName : packageNames) { 1075df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto obbCodePath = create_data_media_obb_path(uuid_, packageName.c_str()); 1076df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(obbCodePath, &extStats.codeSize); 1077df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 10783dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 1079df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (flags & FLAG_USE_QUOTA && appId >= AID_APP_START) { 1080df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey for (auto codePath : codePaths) { 1081df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(codePath, &stats.codeSize, -1, 1082df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey multiuser_get_shared_gid(userId, appId)); 10833dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 1084df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1085df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey collectQuotaStats(uuid, userId, appId, &stats, &extStats); 1086df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 10873dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } else { 1088df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey for (auto codePath : codePaths) { 1089df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(codePath, &stats.codeSize); 1090df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 109194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 1092df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey for (size_t i = 0; i < packageNames.size(); i++) { 1093df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey const char* pkgname = packageNames[i].c_str(); 109494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 1095df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto cePath = create_data_user_ce_package_path(uuid_, userId, pkgname, ceDataInodes[i]); 1096df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey collectManualStats(cePath, &stats); 10973dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 1098df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto dePath = create_data_user_de_package_path(uuid_, userId, pkgname); 1099df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey collectManualStats(dePath, &stats); 11003dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 1101df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto userProfilePath = create_data_user_profile_package_path(userId, pkgname); 1102df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(userProfilePath, &stats.dataSize); 11033dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 1104df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto refProfilePath = create_data_ref_profile_package_path(pkgname); 1105df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(refProfilePath, &stats.codeSize); 1106df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1107df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_EXTERNAL 1108df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto extPath = create_data_media_package_path(uuid_, userId, pkgname, "data"); 1109df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey collectManualStats(extPath, &extStats); 1110df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1111df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto mediaPath = create_data_media_package_path(uuid_, userId, pkgname, "media"); 1112df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(mediaPath, &extStats.dataSize); 1113df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 1114df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1115df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1116df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int32_t sharedGid = multiuser_get_shared_gid(userId, appId); 1117df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (sharedGid != -1) { 1118df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(create_data_dalvik_cache_path(), &stats.codeSize, 1119df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey sharedGid, -1); 1120df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 11213dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 11223dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey calculate_tree_size(create_data_misc_foreign_dex_path(userId), &stats.dataSize, 1123df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey multiuser_get_uid(userId, appId), -1); 1124df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1125df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1126df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey std::vector<int64_t> ret; 1127df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(stats.codeSize); 1128df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(stats.dataSize); 1129df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(stats.cacheSize); 1130df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(extStats.codeSize); 1131df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(extStats.dataSize); 1132df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(extStats.cacheSize); 1133df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 1134df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey LOG(DEBUG) << "Final result " << toString(ret); 1135df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 1136df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey *_aidl_return = ret; 1137df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey return ok(); 1138df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey} 1139df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1140df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkeybinder::Status InstalldNativeService::getUserSize(const std::unique_ptr<std::string>& uuid, 1141df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int32_t userId, int32_t flags, const std::vector<int32_t>& appIds, 1142df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey std::vector<int64_t>* _aidl_return) { 1143df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 1144df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 1145df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1146df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // When modifying this logic, always verify using tests: 1147df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // runtest -x frameworks/base/services/tests/servicestests/src/com/android/server/pm/InstallerTest.java -m testGetUserSize 1148df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1149df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 1150df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey LOG(INFO) << "Measuring user " << userId; 1151df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 1152df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1153df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey struct stats stats; 1154df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey struct stats extStats; 1155df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey memset(&stats, 0, sizeof(stats)); 1156df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey memset(&extStats, 0, sizeof(extStats)); 1157df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1158df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 1159df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1160df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto obbPath = create_data_path(uuid_) + "/media/obb"; 1161df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(obbPath, &extStats.codeSize); 1162df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1163df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (flags & FLAG_USE_QUOTA) { 1164df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(create_data_app_path(uuid_), &stats.codeSize, -1, -1, true); 1165df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1166df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto cePath = create_data_user_ce_path(uuid_, userId); 1167df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey collectManualStatsForUser(cePath, &stats, true); 1168df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1169df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto dePath = create_data_user_de_path(uuid_, userId); 1170df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey collectManualStatsForUser(dePath, &stats, true); 1171df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1172df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto userProfilePath = create_data_user_profile_path(userId); 1173df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(userProfilePath, &stats.dataSize, -1, -1, true); 1174df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1175df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto refProfilePath = create_data_ref_profile_path(); 1176df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(refProfilePath, &stats.codeSize, -1, -1, true); 11773dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 11783dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey#if MEASURE_EXTERNAL 1179df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // TODO: measure external storage paths 1180df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 11813dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 1182df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(create_data_dalvik_cache_path(), &stats.codeSize, 1183df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey -1, -1, true); 1184df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1185df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(create_data_misc_foreign_dex_path(userId), &stats.dataSize, 1186df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey -1, -1, true); 1187df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1188df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto device = findDeviceForUuid(uuid); 1189df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey for (auto appId : appIds) { 1190df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (appId >= AID_APP_START) { 1191df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey collectQuotaStats(device, userId, appId, &stats, &extStats); 1192df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 1193df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // Sleep to make sure we don't lose logs 1194df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey usleep(1); 1195df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 1196df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1197df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1198df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } else { 1199df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(create_data_app_path(uuid_), &stats.codeSize); 1200df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1201df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto cePath = create_data_user_ce_path(uuid_, userId); 1202df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey collectManualStatsForUser(cePath, &stats); 1203df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1204df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto dePath = create_data_user_de_path(uuid_, userId); 1205df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey collectManualStatsForUser(dePath, &stats); 1206df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1207df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto userProfilePath = create_data_user_profile_path(userId); 1208df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(userProfilePath, &stats.dataSize); 1209df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1210df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto refProfilePath = create_data_ref_profile_path(); 1211df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(refProfilePath, &stats.codeSize); 1212df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1213df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_EXTERNAL 1214df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // TODO: measure external storage paths 12153dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey#endif 1216df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1217df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(create_data_dalvik_cache_path(), &stats.codeSize); 1218df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1219df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey calculate_tree_size(create_data_misc_foreign_dex_path(userId), &stats.dataSize); 12202f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey } 122194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 12223dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey std::vector<int64_t> ret; 12233dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey ret.push_back(stats.codeSize); 12243dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey ret.push_back(stats.dataSize); 12253dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey ret.push_back(stats.cacheSize); 1226df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(extStats.codeSize); 1227df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(extStats.dataSize); 1228df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(extStats.cacheSize); 1229df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 1230df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey LOG(DEBUG) << "Final result " << toString(ret); 1231df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 1232df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey *_aidl_return = ret; 1233df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey return ok(); 1234df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey} 1235df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1236df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkeybinder::Status InstalldNativeService::getExternalSize(const std::unique_ptr<std::string>& uuid, 1237df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int32_t userId, int32_t flags, std::vector<int64_t>* _aidl_return) { 1238df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 1239df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 1240df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1241df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // When modifying this logic, always verify using tests: 1242df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // runtest -x frameworks/base/services/tests/servicestests/src/com/android/server/pm/InstallerTest.java -m testGetExternalSize 1243df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1244df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 1245df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey LOG(INFO) << "Measuring external " << userId; 1246df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 1247df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1248df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 1249df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1250df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int64_t totalSize = 0; 1251df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int64_t audioSize = 0; 1252df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int64_t videoSize = 0; 1253df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int64_t imageSize = 0; 1254df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1255df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (flags & FLAG_USE_QUOTA) { 1256df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey struct dqblk dq; 1257df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1258df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto device = findDeviceForUuid(uuid); 1259df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1260df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey uid_t uid = multiuser_get_uid(userId, AID_MEDIA_RW); 1261df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (quotactl(QCMD(Q_GETQUOTA, USRQUOTA), device.c_str(), uid, 1262df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey reinterpret_cast<char*>(&dq)) != 0) { 1263df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (errno != ESRCH) { 1264df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey PLOG(ERROR) << "Failed to quotactl " << device << " for UID " << uid; 1265df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1266df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } else { 1267df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 1268df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey LOG(DEBUG) << "quotactl() for UID " << uid << " " << dq.dqb_curspace; 1269df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 1270df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey totalSize = dq.dqb_curspace; 1271df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1272df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1273df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey gid_t audioGid = multiuser_get_uid(userId, AID_MEDIA_AUDIO); 1274df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), audioGid, 1275df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey reinterpret_cast<char*>(&dq)) == 0) { 1276df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 1277df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey LOG(DEBUG) << "quotactl() for GID " << audioGid << " " << dq.dqb_curspace; 1278df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 1279df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey audioSize = dq.dqb_curspace; 1280df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1281df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey gid_t videoGid = multiuser_get_uid(userId, AID_MEDIA_VIDEO); 1282df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), videoGid, 1283df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey reinterpret_cast<char*>(&dq)) == 0) { 1284df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 1285df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey LOG(DEBUG) << "quotactl() for GID " << videoGid << " " << dq.dqb_curspace; 1286df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 1287df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey videoSize = dq.dqb_curspace; 1288df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1289df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey gid_t imageGid = multiuser_get_uid(userId, AID_MEDIA_IMAGE); 1290df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), imageGid, 1291df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey reinterpret_cast<char*>(&dq)) == 0) { 1292df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 1293df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey LOG(DEBUG) << "quotactl() for GID " << imageGid << " " << dq.dqb_curspace; 1294df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 1295df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey imageSize = dq.dqb_curspace; 1296df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1297df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } else { 1298df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey FTS *fts; 1299df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey FTSENT *p; 1300df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey auto path = create_data_media_path(uuid_, userId); 1301df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey char *argv[] = { (char*) path.c_str(), nullptr }; 1302df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_XDEV, NULL))) { 1303df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey return error("Failed to fts_open " + path); 1304df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1305df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey while ((p = fts_read(fts)) != NULL) { 1306df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey char* ext; 1307df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int64_t size = (p->fts_statp->st_blocks * 512); 1308df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey switch (p->fts_info) { 1309df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey case FTS_F: 1310df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // Only categorize files not belonging to apps 1311df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (p->fts_statp->st_gid < AID_APP_START) { 1312df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ext = strrchr(p->fts_name, '.'); 1313df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (ext != nullptr) { 1314df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey switch (MatchExtension(++ext)) { 1315df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey case AID_MEDIA_AUDIO: audioSize += size; break; 1316df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey case AID_MEDIA_VIDEO: videoSize += size; break; 1317df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey case AID_MEDIA_IMAGE: imageSize += size; break; 1318df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1319df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1320df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1321df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // Fall through to always count against total 1322df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey case FTS_D: 1323df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey case FTS_DEFAULT: 1324df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey case FTS_SL: 1325df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey case FTS_SLNONE: 1326df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey totalSize += size; 1327df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey break; 1328df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1329df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1330df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey fts_close(fts); 1331df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 1332df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 1333df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey std::vector<int64_t> ret; 1334df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(totalSize); 1335df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(audioSize); 1336df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(videoSize); 1337df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey ret.push_back(imageSize); 1338df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 1339df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey LOG(DEBUG) << "Final result " << toString(ret); 1340df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 13413dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey *_aidl_return = ret; 1342423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 13432f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey} 1344d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey 13456727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr// Dumps the contents of a profile file, using pkgname's dex files for pretty 13466727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr// printing the result. 1347475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::dumpProfiles(int32_t uid, const std::string& packageName, 1348475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const std::string& codePaths, bool* _aidl_return) { 1349475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 1350423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 1351475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey 1352475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* pkgname = packageName.c_str(); 135390aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey const char* code_paths = codePaths.c_str(); 13546727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr 135590aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey *_aidl_return = dump_profiles(uid, pkgname, code_paths); 1356423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 13576727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr} 13586727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr 13594d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe// TODO: Consider returning error codes. 1360475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::mergeProfiles(int32_t uid, const std::string& packageName, 1361423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey bool* _aidl_return) { 1362475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 1363423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 1364423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 1365475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* pkgname = packageName.c_str(); 1366475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey *_aidl_return = analyse_profiles(uid, pkgname); 1367423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 13684d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe} 13694d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe 13706c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkeybinder::Status InstalldNativeService::dexopt(const std::string& apkPath, int32_t uid, 13716c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey const std::unique_ptr<std::string>& packageName, const std::string& instructionSet, 13726c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey int32_t dexoptNeeded, const std::unique_ptr<std::string>& outputPath, int32_t dexFlags, 13736c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey const std::string& compilerFilter, const std::unique_ptr<std::string>& uuid, 13746c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey const std::unique_ptr<std::string>& sharedLibraries) { 13756c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 1376423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 1377423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (packageName && *packageName != "*") { 1378423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(*packageName); 1379423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 13806c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey 13816c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey const char* apk_path = apkPath.c_str(); 13826c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey const char* pkgname = packageName ? packageName->c_str() : "*"; 13836c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey const char* instruction_set = instructionSet.c_str(); 13846c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey const char* oat_dir = outputPath ? outputPath->c_str() : nullptr; 13856c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey const char* compiler_filter = compilerFilter.c_str(); 13866c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey const char* volume_uuid = uuid ? uuid->c_str() : nullptr; 13876c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey const char* shared_libraries = sharedLibraries ? sharedLibraries->c_str() : nullptr; 13886c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey 13896c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey int res = android::installd::dexopt(apk_path, uid, pkgname, instruction_set, dexoptNeeded, 13906c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey oat_dir, dexFlags, compiler_filter, volume_uuid, shared_libraries); 1391423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return res ? error(res, "Failed to dexopt") : ok(); 13926c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey} 13936c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey 1394475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::markBootComplete(const std::string& instructionSet) { 1395475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 1396475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* instruction_set = instructionSet.c_str(); 1397475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey 1398475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey char boot_marker_path[PKG_PATH_MAX]; 1399475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey sprintf(boot_marker_path, 140002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe "%s/%s/%s/.booting", 140102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe android_data_dir.path, 140202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe DALVIK_CACHE, 140302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe instruction_set); 1404091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath 1405475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ALOGV("mark_boot_complete : %s", boot_marker_path); 1406475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey if (unlink(boot_marker_path) != 0) { 1407423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error(StringPrintf("Failed to unlink %s", boot_marker_path)); 1408475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey } 1409423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 1410091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath} 1411091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath 141294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodvoid mkinnerdirs(char* path, int basepos, mode_t mode, int uid, int gid, 141394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood struct stat* statbuf) 141494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 141594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood while (path[basepos] != 0) { 141694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (path[basepos] == '/') { 141794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood path[basepos] = 0; 141894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (lstat(path, statbuf) < 0) { 141994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGV("Making directory: %s\n", path); 142094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (mkdir(path, mode) == 0) { 142194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood chown(path, uid, gid); 142294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else { 142394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGW("Unable to make directory %s: %s\n", path, strerror(errno)); 142494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 142594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 142694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood path[basepos] = '/'; 142794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood basepos++; 142894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 142994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood basepos++; 143094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 143194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 143294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 1433475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::linkNativeLibraryDirectory( 1434475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const std::unique_ptr<std::string>& uuid, const std::string& packageName, 1435475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const std::string& nativeLibPath32, int32_t userId) { 1436475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 1437423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 1438423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 1439423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 1440475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 1441475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* pkgname = packageName.c_str(); 1442475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* asecLibDir = nativeLibPath32.c_str(); 144394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood struct stat s, libStat; 1444423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey binder::Status res = ok(); 144594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 1446423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey auto _pkgdir = create_data_user_ce_package_path(uuid_, userId, pkgname); 1447423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey auto _libsymlink = _pkgdir + PKG_LIB_POSTFIX; 1448c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey 1449c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey const char* pkgdir = _pkgdir.c_str(); 1450c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey const char* libsymlink = _libsymlink.c_str(); 145194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 1452475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey if (stat(pkgdir, &s) < 0) { 1453423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to stat " + _pkgdir); 1454475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey } 145594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 145694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (chown(pkgdir, AID_INSTALL, AID_INSTALL) < 0) { 1457423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to chown " + _pkgdir); 145894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 145994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 146094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (chmod(pkgdir, 0700) < 0) { 1461423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to chmod " + _pkgdir); 146294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto out; 146394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 146494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 146594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (lstat(libsymlink, &libStat) < 0) { 146694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (errno != ENOENT) { 1467423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to stat " + _libsymlink); 146894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto out; 146994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 147094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else { 147194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (S_ISDIR(libStat.st_mode)) { 14723aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath if (delete_dir_contents(libsymlink, 1, NULL) < 0) { 1473423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to delete " + _libsymlink); 147494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto out; 147594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 147694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else if (S_ISLNK(libStat.st_mode)) { 147794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (unlink(libsymlink) < 0) { 1478423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to unlink " + _libsymlink); 147994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto out; 148094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 148194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 148294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 148394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 148494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (symlink(asecLibDir, libsymlink) < 0) { 1485423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to symlink " + _libsymlink + " to " + nativeLibPath32); 148694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto out; 148794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 148894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 148994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodout: 149094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (chmod(pkgdir, s.st_mode) < 0) { 1491423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey auto msg = "Failed to cleanup chmod " + _pkgdir; 1492423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (res.isOk()) { 1493423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error(msg); 1494423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } else { 1495423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey PLOG(ERROR) << msg; 1496423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 149794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 149894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 149994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (chown(pkgdir, s.st_uid, s.st_gid) < 0) { 1500423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey auto msg = "Failed to cleanup chown " + _pkgdir; 1501423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (res.isOk()) { 1502423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error(msg); 1503423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } else { 1504423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey PLOG(ERROR) << msg; 1505423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 150694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 150794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 1508423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return res; 150994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 151063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 151163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstadstatic void run_idmap(const char *target_apk, const char *overlay_apk, int idmap_fd) 151263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad{ 151363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad static const char *IDMAP_BIN = "/system/bin/idmap"; 151463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad static const size_t MAX_INT_LEN = 32; 151563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad char idmap_str[MAX_INT_LEN]; 151663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 151763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad snprintf(idmap_str, sizeof(idmap_str), "%d", idmap_fd); 151863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 151963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad execl(IDMAP_BIN, IDMAP_BIN, "--fd", target_apk, overlay_apk, idmap_str, (char*)NULL); 152063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("execl(%s) failed: %s\n", IDMAP_BIN, strerror(errno)); 152163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad} 152263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 152363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad// Transform string /a/b/c.apk to (prefix)/a@b@c.apk@(suffix) 152463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad// eg /a/b/c.apk to /data/resource-cache/a@b@c.apk@idmap 152563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstadstatic int flatten_path(const char *prefix, const char *suffix, 152663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad const char *overlay_path, char *idmap_path, size_t N) 152763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad{ 152863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (overlay_path == NULL || idmap_path == NULL) { 152963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad return -1; 153063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 153163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad const size_t len_overlay_path = strlen(overlay_path); 153263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad // will access overlay_path + 1 further below; requires absolute path 153363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (len_overlay_path < 2 || *overlay_path != '/') { 153463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad return -1; 153563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 153663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad const size_t len_idmap_root = strlen(prefix); 153763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad const size_t len_suffix = strlen(suffix); 153863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (SIZE_MAX - len_idmap_root < len_overlay_path || 153963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad SIZE_MAX - (len_idmap_root + len_overlay_path) < len_suffix) { 154063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad // additions below would cause overflow 154163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad return -1; 154263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 154363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (N < len_idmap_root + len_overlay_path + len_suffix) { 154463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad return -1; 154563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 154663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad memset(idmap_path, 0, N); 154763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad snprintf(idmap_path, N, "%s%s%s", prefix, overlay_path + 1, suffix); 154863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad char *ch = idmap_path + len_idmap_root; 154963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad while (*ch != '\0') { 155063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (*ch == '/') { 155163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad *ch = '@'; 155263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 155363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ++ch; 155463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 155563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad return 0; 155663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad} 155763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 1558475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::idmap(const std::string& targetApkPath, 1559475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const std::string& overlayApkPath, int32_t uid) { 1560475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 1561475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* target_apk = targetApkPath.c_str(); 1562475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* overlay_apk = overlayApkPath.c_str(); 156363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGV("idmap target_apk=%s overlay_apk=%s uid=%d\n", target_apk, overlay_apk, uid); 156463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 156563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad int idmap_fd = -1; 156663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad char idmap_path[PATH_MAX]; 156763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 156863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (flatten_path(IDMAP_PREFIX, IDMAP_SUFFIX, overlay_apk, 156963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad idmap_path, sizeof(idmap_path)) == -1) { 157063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("idmap cannot generate idmap path for overlay %s\n", overlay_apk); 157163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad goto fail; 157263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 157363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 157463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad unlink(idmap_path); 157563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad idmap_fd = open(idmap_path, O_RDWR | O_CREAT | O_EXCL, 0644); 157663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (idmap_fd < 0) { 157763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("idmap cannot open '%s' for output: %s\n", idmap_path, strerror(errno)); 157863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad goto fail; 157963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 158063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (fchown(idmap_fd, AID_SYSTEM, uid) < 0) { 158163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("idmap cannot chown '%s'\n", idmap_path); 158263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad goto fail; 158363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 158463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (fchmod(idmap_fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0) { 158563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("idmap cannot chmod '%s'\n", idmap_path); 158663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad goto fail; 158763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 158863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 158963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad pid_t pid; 159063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad pid = fork(); 159163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (pid == 0) { 159263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad /* child -- drop privileges before continuing */ 159363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (setgid(uid) != 0) { 159463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("setgid(%d) failed during idmap\n", uid); 159563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad exit(1); 159663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 159763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (setuid(uid) != 0) { 159863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("setuid(%d) failed during idmap\n", uid); 159963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad exit(1); 160063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 160163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (flock(idmap_fd, LOCK_EX | LOCK_NB) != 0) { 160263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("flock(%s) failed during idmap: %s\n", idmap_path, strerror(errno)); 160363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad exit(1); 160463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 160563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 160663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad run_idmap(target_apk, overlay_apk, idmap_fd); 160763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad exit(1); /* only if exec call to idmap failed */ 160863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } else { 160963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad int status = wait_child(pid); 161063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (status != 0) { 161163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("idmap failed, status=0x%04x\n", status); 161263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad goto fail; 161363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 161463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 161563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 161663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad close(idmap_fd); 1617423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 161863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstadfail: 161963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (idmap_fd >= 0) { 162063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad close(idmap_fd); 162163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad unlink(idmap_path); 162263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 1623423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error(); 162463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad} 1625e9887e46cea4a095e4219927eadbe4c57bb1a5eeRobert Craig 1626c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkeybinder::Status InstalldNativeService::restoreconAppData(const std::unique_ptr<std::string>& uuid, 1627c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const std::string& packageName, int32_t userId, int32_t flags, int32_t appId, 1628c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const std::string& seInfo) { 1629c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 1630423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 1631423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 1632423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 1633423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey binder::Status res = ok(); 1634e9887e46cea4a095e4219927eadbe4c57bb1a5eeRobert Craig 1635da30dc7336f03ca629fe173db1425fdce989119cRobert Craig // SELINUX_ANDROID_RESTORECON_DATADATA flag is set by libselinux. Not needed here. 1636c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey unsigned int seflags = SELINUX_ANDROID_RESTORECON_RECURSE; 1637c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 1638c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* pkgName = packageName.c_str(); 1639c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* seinfo = seInfo.c_str(); 1640e9887e46cea4a095e4219927eadbe4c57bb1a5eeRobert Craig 1641c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey uid_t uid = multiuser_get_uid(userId, appId); 1642aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (flags & FLAG_STORAGE_CE) { 1643c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey auto path = create_data_user_ce_package_path(uuid_, userId, pkgName); 1644c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey if (selinux_android_restorecon_pkgdir(path.c_str(), seinfo, uid, seflags) < 0) { 1645423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("restorecon failed for " + path); 1646da30dc7336f03ca629fe173db1425fdce989119cRobert Craig } 1647c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey } 1648aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (flags & FLAG_STORAGE_DE) { 1649c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey auto path = create_data_user_de_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 } 1653da30dc7336f03ca629fe173db1425fdce989119cRobert Craig } 1654423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return res; 1655e9887e46cea4a095e4219927eadbe4c57bb1a5eeRobert Craig} 16563aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath 1657475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::createOatDir(const std::string& oatDir, 1658475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const std::string& instructionSet) { 1659475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 1660475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* oat_dir = oatDir.c_str(); 1661475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* instruction_set = instructionSet.c_str(); 166288ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov char oat_instr_dir[PKG_PATH_MAX]; 166388ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov 166488ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov if (validate_apk_path(oat_dir)) { 1665423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Invalid path " + oatDir); 166688ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov } 16678eed7e6a9bb527af7380ec13b390c6e2955eba6aFyodor Kupolov if (fs_prepare_dir(oat_dir, S_IRWXU | S_IRWXG | S_IXOTH, AID_SYSTEM, AID_INSTALL)) { 1668423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to prepare " + oatDir); 166988ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov } 167088ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov if (selinux_android_restorecon(oat_dir, 0)) { 1671423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to restorecon " + oatDir); 167288ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov } 167388ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov snprintf(oat_instr_dir, PKG_PATH_MAX, "%s/%s", oat_dir, instruction_set); 16748eed7e6a9bb527af7380ec13b390c6e2955eba6aFyodor Kupolov if (fs_prepare_dir(oat_instr_dir, S_IRWXU | S_IRWXG | S_IXOTH, AID_SYSTEM, AID_INSTALL)) { 1675423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error(StringPrintf("Failed to prepare %s", oat_instr_dir)); 167688ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov } 1677423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 167888ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov} 167988ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov 1680475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::rmPackageDir(const std::string& packageDir) { 1681475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 1682423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (validate_apk_path(packageDir.c_str())) { 1683423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Invalid path " + packageDir); 1684423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 1685423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (delete_dir_contents_and_dir(packageDir) != 0) { 1686423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to delete " + packageDir); 168788ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov } 1688423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 168988ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov} 169088ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov 1691475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::linkFile(const std::string& relativePath, 1692475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const std::string& fromBase, const std::string& toBase) { 1693475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 1694475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* relative_path = relativePath.c_str(); 1695475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* from_base = fromBase.c_str(); 1696475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* to_base = toBase.c_str(); 1697d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath char from_path[PKG_PATH_MAX]; 1698d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath char to_path[PKG_PATH_MAX]; 1699d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath snprintf(from_path, PKG_PATH_MAX, "%s/%s", from_base, relative_path); 1700d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath snprintf(to_path, PKG_PATH_MAX, "%s/%s", to_base, relative_path); 1701d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath 1702d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath if (validate_apk_path_subdirs(from_path)) { 1703423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error(StringPrintf("Invalid from path %s", from_path)); 1704d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath } 1705d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath 1706d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath if (validate_apk_path_subdirs(to_path)) { 1707423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error(StringPrintf("Invalid to path %s", to_path)); 1708d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath } 1709d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath 1710423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (link(from_path, to_path) < 0) { 1711423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error(StringPrintf("Failed to link from %s to %s", from_path, to_path)); 1712d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath } 1713d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath 1714423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 1715d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath} 1716d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath 1717475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::moveAb(const std::string& apkPath, 1718475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const std::string& instructionSet, const std::string& outputPath) { 1719475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 172090aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey 1721475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* apk_path = apkPath.c_str(); 1722475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* instruction_set = instructionSet.c_str(); 1723475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* oat_dir = outputPath.c_str(); 1724d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe 172590aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey bool success = move_ab(apk_path, instruction_set, oat_dir); 1726423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return success ? ok() : error(); 1727e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe} 1728e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe 1729475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::deleteOdex(const std::string& apkPath, 1730475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const std::string& instructionSet, const std::string& outputPath) { 1731475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 173290aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey 1733475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* apk_path = apkPath.c_str(); 1734475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* instruction_set = instructionSet.c_str(); 1735475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* oat_dir = outputPath.c_str(); 1736475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey 173790aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey bool res = delete_odex(apk_path, instruction_set, oat_dir); 173890aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey return res ? ok() : error(); 1739b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampe} 1740b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampe 174102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe} // namespace installd 174202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe} // namespace android 1743