InstalldNativeService.cpp revision 0274c977e3b10b52936fd5b2eb464857f0ca6358
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 1702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include "commands.h" 1802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe 1902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <errno.h> 2002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <inttypes.h> 21d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe#include <regex> 2202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <stdlib.h> 2302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <sys/capability.h> 2402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <sys/file.h> 2502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <sys/resource.h> 2602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <sys/stat.h> 27cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey#include <sys/types.h> 286a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle#include <sys/wait.h> 29cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey#include <sys/xattr.h> 3002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <unistd.h> 3141ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey 326fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe#include <android-base/logging.h> 33e4ec9eb7b4c452493589983970ba5ccc501728d1Elliott Hughes#include <android-base/stringprintf.h> 346727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr#include <android-base/strings.h> 3564b59cc1c89225813295d1b5d6bf4183615da204Roland Levillain#include <android-base/unique_fd.h> 3602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <cutils/fs.h> 3702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <cutils/log.h> // TODO: Move everything to base/logging. 380378aaf257aee92539d30543914a50c4481c6a18Brian Carlstrom#include <cutils/sched_policy.h> 3994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#include <diskusage/dirsize.h> 40e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey#include <logwrap/logwrap.h> 4102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <private/android_filesystem_config.h> 42e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey#include <selinux/android.h> 4302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <system/thread_defs.h> 44e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 4502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <globals.h> 4602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <installd_deps.h> 47fd12edaeab839f3f1f087cc75bd18b4d8af5b192Andreas Gampe#include <otapreopt_utils.h> 4802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <utils.h> 4902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe 5002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#ifndef LOG_TAG 5102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#define LOG_TAG "installd" 5202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#endif 5341ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey 546fb5a0179d4de9c85351fd016277949804c809edAndreas Gampeusing android::base::EndsWith; 5541ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkeyusing android::base::StringPrintf; 5694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 5702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampenamespace android { 5802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampenamespace installd { 5994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 60cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkeystatic constexpr const char* kCpPath = "/system/bin/cp"; 61cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkeystatic constexpr const char* kXattrDefault = "user.default"; 62e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 63d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampestatic constexpr const char* PKG_LIB_POSTFIX = "/lib"; 64d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampestatic constexpr const char* CACHE_DIR_POSTFIX = "/cache"; 65d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampestatic constexpr const char* CODE_CACHE_DIR_POSTFIX = "/code_cache"; 66d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe 67d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampestatic constexpr const char* IDMAP_PREFIX = "/data/resource-cache/"; 68d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampestatic constexpr const char* IDMAP_SUFFIX = "@idmap"; 69d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe 70d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe// NOTE: keep in sync with StorageManager 71d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampestatic constexpr int FLAG_STORAGE_DE = 1 << 0; 72d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampestatic constexpr int FLAG_STORAGE_CE = 1 << 1; 73d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe 74d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe// NOTE: keep in sync with Installer 75d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampestatic constexpr int FLAG_CLEAR_CACHE_ONLY = 1 << 8; 76d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampestatic constexpr int FLAG_CLEAR_CODE_CACHE_ONLY = 1 << 9; 77d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe 78d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe/* dexopt needed flags matching those in dalvik.system.DexFile */ 79920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffraystatic constexpr int DEX2OAT_FROM_SCRATCH = 1; 80920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffraystatic constexpr int DEX2OAT_FOR_BOOT_IMAGE = 2; 81920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffraystatic constexpr int DEX2OAT_FOR_FILTER = 3; 82920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffraystatic constexpr int DEX2OAT_FOR_RELOCATION = 4; 83920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffraystatic constexpr int PATCHOAT_FOR_RELOCATION = 5; 84d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe 85eecb2d205366351af42fd0cd9e1a95de3980764eJanis Danisevskis#define MIN_RESTRICTED_HOME_SDK_VERSION 24 // > M 86eecb2d205366351af42fd0cd9e1a95de3980764eJanis Danisevskis 876a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravletypedef int fd_t; 886a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 890274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeynamespace { 900274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 910274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeyconstexpr const char* kDump = "android.permission.DUMP"; 920274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 930274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeybinder::Status checkPermission(const char* permission) { 940274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey pid_t pid; 950274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey uid_t uid; 960274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 970274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey if (checkCallingPermission(String16(permission), reinterpret_cast<int32_t*>(&pid), 980274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey reinterpret_cast<int32_t*>(&uid))) { 990274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey return binder::Status::ok(); 1000274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey } else { 1010274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey auto err = StringPrintf("UID %d / PID %d lacks permission %s", uid, pid, permission); 1020274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey return binder::Status::fromExceptionCode(binder::Status::EX_SECURITY, String8(err.c_str())); 1030274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey } 1040274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey} 1050274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 1060274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeybinder::Status checkUid(uid_t expectedUid) { 1070274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey uid_t uid = IPCThreadState::self()->getCallingUid(); 1080274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey if (uid == expectedUid || uid == AID_ROOT) { 1090274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey return binder::Status::ok(); 1100274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey } else { 1110274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey auto err = StringPrintf("UID %d is not expected UID %d", uid, expectedUid); 1120274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey return binder::Status::fromExceptionCode(binder::Status::EX_SECURITY, String8(err.c_str())); 1130274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey } 1140274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey} 1150274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 1160274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey#define ENFORCE_UID(uid) { \ 1170274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey binder::Status status = checkUid((uid)); \ 1180274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey if (!status.isOk()) { \ 1190274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey return status; \ 1200274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey } \ 1210274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey} 1220274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 1230274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey} // namespace 1240274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 1250274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeystatus_t InstalldNativeService::start() { 1260274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey IPCThreadState::self()->disableBackgroundScheduling(true); 1270274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey status_t ret = BinderService<InstalldNativeService>::publish(); 1280274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey if (ret != android::OK) { 1290274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey return ret; 1300274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey } 1310274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey sp<ProcessState> ps(ProcessState::self()); 1320274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey ps->startThreadPool(); 1330274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey ps->giveThreadPoolName(); 1340274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey return android::OK; 1350274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey} 1360274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 1370274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeystatus_t InstalldNativeService::dump(int fd, const Vector<String16> & /* args */) { 1380274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const binder::Status dump_permission = checkPermission(kDump); 1390274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey if (!dump_permission.isOk()) { 1400274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const String8 msg(dump_permission.toString8()); 1410274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey write(fd, msg.string(), msg.size()); 1420274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey return PERMISSION_DENIED; 1430274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey } 1440274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 1450274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey std::string msg = "installd is happy\n"; 1460274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey write(fd, msg.c_str(), strlen(msg.c_str())); 1470274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey return NO_ERROR; 1480274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey} 1490274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 1506a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic bool property_get_bool(const char* property_name, bool default_value = false) { 1516a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle char tmp_property_value[kPropertyValueMax]; 1526a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle bool have_property = get_property(property_name, tmp_property_value, nullptr) > 0; 1536a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle if (!have_property) { 1546a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle return default_value; 1556a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 1566a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle return strcmp(tmp_property_value, "true") == 0; 1576a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle} 1586a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 159bc56e7dcf07e633867e1525435373a1231216b22Calin Juravle// Keep profile paths in sync with ActivityThread. 160bc56e7dcf07e633867e1525435373a1231216b22Calin Juravleconstexpr const char* PRIMARY_PROFILE_NAME = "primary.prof"; 161bc56e7dcf07e633867e1525435373a1231216b22Calin Juravlestatic std::string create_primary_profile(const std::string& profile_dir) { 162bc56e7dcf07e633867e1525435373a1231216b22Calin Juravle return StringPrintf("%s/%s", profile_dir.c_str(), PRIMARY_PROFILE_NAME); 163bc56e7dcf07e633867e1525435373a1231216b22Calin Juravle} 164bc56e7dcf07e633867e1525435373a1231216b22Calin Juravle 1657db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey/** 1667db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey * Perform restorecon of the given path, but only perform recursive restorecon 1677db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey * if the label of that top-level file actually changed. This can save us 1687db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey * significant time by avoiding no-op traversals of large filesystem trees. 1697db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey */ 1700274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeystatic int restorecon_app_data_lazy(const std::string& path, const std::string& seInfo, uid_t uid) { 1717db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey int res = 0; 1727db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey char* before = nullptr; 1737db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey char* after = nullptr; 1747db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey 1757db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey // Note that SELINUX_ANDROID_RESTORECON_DATADATA flag is set by 1767db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey // libselinux. Not needed here. 1777db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey 17895d9ac642f082870fd4d2419f534171a9b82bbcdJeff Sharkey if (lgetfilecon(path.c_str(), &before) < 0) { 1797db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey PLOG(ERROR) << "Failed before getfilecon for " << path; 1807db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey goto fail; 1817db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey } 1820274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey if (selinux_android_restorecon_pkgdir(path.c_str(), seInfo.c_str(), uid, 0) < 0) { 1837db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey PLOG(ERROR) << "Failed top-level restorecon for " << path; 1847db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey goto fail; 1857db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey } 18695d9ac642f082870fd4d2419f534171a9b82bbcdJeff Sharkey if (lgetfilecon(path.c_str(), &after) < 0) { 1877db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey PLOG(ERROR) << "Failed after getfilecon for " << path; 1887db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey goto fail; 1897db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey } 1907db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey 1917db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey // If the initial top-level restorecon above changed the label, then go 1927db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey // back and restorecon everything recursively 1937db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey if (strcmp(before, after)) { 1947db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey LOG(DEBUG) << "Detected label change from " << before << " to " << after << " at " << path 1957db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey << "; running recursive restorecon"; 1960274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey if (selinux_android_restorecon_pkgdir(path.c_str(), seInfo.c_str(), uid, 1977db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey SELINUX_ANDROID_RESTORECON_RECURSE) < 0) { 1987db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey PLOG(ERROR) << "Failed recursive restorecon for " << path; 1997db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey goto fail; 2007db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey } 2017db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey } 2027db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey 2037db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey goto done; 2047db6041d156b750689b32e340d8a0753061262a3Jeff Sharkeyfail: 2057db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey res = -1; 2067db6041d156b750689b32e340d8a0753061262a3Jeff Sharkeydone: 2077db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey free(before); 2087db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey free(after); 2097db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey return res; 2107db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey} 2117db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey 2120274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeystatic int restorecon_app_data_lazy(const std::string& parent, const char* name, 2130274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const std::string& seInfo, uid_t uid) { 2140274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey return restorecon_app_data_lazy(StringPrintf("%s/%s", parent.c_str(), name), seInfo, uid); 21595d9ac642f082870fd4d2419f534171a9b82bbcdJeff Sharkey} 21695d9ac642f082870fd4d2419f534171a9b82bbcdJeff Sharkey 2177db6041d156b750689b32e340d8a0753061262a3Jeff Sharkeystatic int prepare_app_dir(const std::string& path, mode_t target_mode, uid_t uid) { 2189a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey if (fs_prepare_dir_strict(path.c_str(), target_mode, uid, uid) != 0) { 2199a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey PLOG(ERROR) << "Failed to prepare " << path; 2209a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey return -1; 2219a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey } 2229a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey return 0; 2239a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey} 2249a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey 2259a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkeystatic int prepare_app_dir(const std::string& parent, const char* name, mode_t target_mode, 2267db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey uid_t uid) { 2277db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey return prepare_app_dir(StringPrintf("%s/%s", parent.c_str(), name), target_mode, uid); 2289a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey} 2299a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey 2300274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeybinder::Status InstalldNativeService::createAppData(const std::unique_ptr<std::string>& uuid, 2310274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const std::string& packageName, int32_t userId, int32_t flags, int32_t appId, 2320274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const std::string& seInfo, int32_t targetSdkVersion) { 2330274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 2340274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 2350274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 2360274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const char* pkgname = packageName.c_str(); 2370274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 2380274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey uid_t uid = multiuser_get_uid(userId, appId); 2390274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey mode_t target_mode = targetSdkVersion >= MIN_RESTRICTED_HOME_SDK_VERSION ? 0700 : 0751; 240aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (flags & FLAG_STORAGE_CE) { 2410274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey auto path = create_data_user_ce_package_path(uuid_, userId, pkgname); 2427db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey if (prepare_app_dir(path, target_mode, uid) || 2437db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey prepare_app_dir(path, "cache", 0771, uid) || 2447db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey prepare_app_dir(path, "code_cache", 0771, uid)) { 2450274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey return binder::Status::fromServiceSpecificError(-1); 2467db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey } 2477db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey 2487db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey // Consider restorecon over contents if label changed 2490274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey if (restorecon_app_data_lazy(path, seInfo, uid) || 2500274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey restorecon_app_data_lazy(path, "cache", seInfo, uid) || 2510274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey restorecon_app_data_lazy(path, "code_cache", seInfo, uid)) { 2520274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey return binder::Status::fromServiceSpecificError(-1); 253c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey } 2549a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey 2559a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey // Remember inode numbers of cache directories so that we can clear 2569a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey // contents while CE storage is locked 2579a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey if (write_path_inode(path, "cache", kXattrInodeCache) || 2589a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey write_path_inode(path, "code_cache", kXattrInodeCodeCache)) { 2590274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey return binder::Status::fromServiceSpecificError(-1); 260c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey } 26194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 262aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (flags & FLAG_STORAGE_DE) { 2630274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey auto path = create_data_user_de_package_path(uuid_, userId, pkgname); 2647db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey if (prepare_app_dir(path, target_mode, uid)) { 265a03c747ff6e1afb5c1dc86560705bd979cd5c49fJeff Sharkey // TODO: include result once 25796509 is fixed 2660274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey return binder::Status::ok(); 267c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey } 2686a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 2697db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey // Consider restorecon over contents if label changed 2700274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey if (restorecon_app_data_lazy(path, seInfo, uid)) { 2710274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey return binder::Status::fromServiceSpecificError(-1); 2727db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey } 2737db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey 2746a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle if (property_get_bool("dalvik.vm.usejitprofiles")) { 2750274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const std::string profile_path = create_data_user_profile_package_path(userId, pkgname); 2766a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle // read-write-execute only for the app user. 2776a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle if (fs_prepare_dir_strict(profile_path.c_str(), 0700, uid, uid) != 0) { 2786a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle PLOG(ERROR) << "Failed to prepare " << profile_path; 2790274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey return binder::Status::fromServiceSpecificError(-1); 2806a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 281bc56e7dcf07e633867e1525435373a1231216b22Calin Juravle std::string profile_file = create_primary_profile(profile_path); 282bc56e7dcf07e633867e1525435373a1231216b22Calin Juravle // read-write only for the app user. 283bc56e7dcf07e633867e1525435373a1231216b22Calin Juravle if (fs_prepare_file_strict(profile_file.c_str(), 0600, uid, uid) != 0) { 284bc56e7dcf07e633867e1525435373a1231216b22Calin Juravle PLOG(ERROR) << "Failed to prepare " << profile_path; 2850274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey return binder::Status::fromServiceSpecificError(-1); 286bc56e7dcf07e633867e1525435373a1231216b22Calin Juravle } 2876a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle const std::string ref_profile_path = create_data_ref_profile_package_path(pkgname); 2886a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle // dex2oat/profman runs under the shared app gid and it needs to read/write reference 2896a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle // profiles. 2906a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle appid_t shared_app_gid = multiuser_get_shared_app_gid(uid); 2916a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle if (fs_prepare_dir_strict( 2926a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle ref_profile_path.c_str(), 0700, shared_app_gid, shared_app_gid) != 0) { 2936a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle PLOG(ERROR) << "Failed to prepare " << ref_profile_path; 2940274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey return binder::Status::fromServiceSpecificError(-1); 2956a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 2966a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 29794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 2980274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey return binder::Status::ok(); 29994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 30094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 301cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkeyint migrate_app_data(const char *uuid, const char *pkgname, userid_t userid, int flags) { 302cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey // This method only exists to upgrade system apps that have requested 303cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey // forceDeviceEncrypted, so their default storage always lives in a 304cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey // consistent location. This only works on non-FBE devices, since we 305cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey // never want to risk exposing data on a device with real CE/DE storage. 306cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey 3072f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey auto ce_path = create_data_user_ce_package_path(uuid, userid, pkgname); 308cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey auto de_path = create_data_user_de_package_path(uuid, userid, pkgname); 309cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey 310cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey // If neither directory is marked as default, assume CE is default 311cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey if (getxattr(ce_path.c_str(), kXattrDefault, nullptr, 0) == -1 312cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey && getxattr(de_path.c_str(), kXattrDefault, nullptr, 0) == -1) { 313cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey if (setxattr(ce_path.c_str(), kXattrDefault, nullptr, 0, 0) != 0) { 314cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey PLOG(ERROR) << "Failed to mark default storage " << ce_path; 315cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey return -1; 316cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey } 317cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey } 318cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey 319cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey // Migrate default data location if needed 320cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey auto target = (flags & FLAG_STORAGE_DE) ? de_path : ce_path; 321cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey auto source = (flags & FLAG_STORAGE_DE) ? ce_path : de_path; 322cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey 323cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey if (getxattr(target.c_str(), kXattrDefault, nullptr, 0) == -1) { 324cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey LOG(WARNING) << "Requested default storage " << target 325cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey << " is not active; migrating from " << source; 326cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey if (delete_dir_contents_and_dir(target) != 0) { 327cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey PLOG(ERROR) << "Failed to delete"; 328cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey return -1; 329cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey } 330cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey if (rename(source.c_str(), target.c_str()) != 0) { 331cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey PLOG(ERROR) << "Failed to rename"; 332cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey return -1; 333cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey } 334cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey } 335cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey 336cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey return 0; 337cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey} 338cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey 339cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravlestatic bool clear_profile(const std::string& profile) { 34064b59cc1c89225813295d1b5d6bf4183615da204Roland Levillain base::unique_fd ufd(open(profile.c_str(), O_WRONLY | O_NOFOLLOW | O_CLOEXEC)); 34164b59cc1c89225813295d1b5d6bf4183615da204Roland Levillain if (ufd.get() < 0) { 342278edac4b541e0a671d646a0e17bdbea411c8a08Calin Juravle if (errno != ENOENT) { 343cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle PLOG(WARNING) << "Could not open profile " << profile; 344d828d6839f6a01e3f0b2b8b2694139dfa801c557David Brazdil return false; 345cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle } else { 346cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle // Nothing to clear. That's ok. 347cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle return true; 348278edac4b541e0a671d646a0e17bdbea411c8a08Calin Juravle } 3496a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 350cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle 35164b59cc1c89225813295d1b5d6bf4183615da204Roland Levillain if (flock(ufd.get(), LOCK_EX | LOCK_NB) != 0) { 352cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle if (errno != EWOULDBLOCK) { 353cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle PLOG(WARNING) << "Error locking profile " << profile; 354cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle } 355cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle // This implies that the app owning this profile is running 356cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle // (and has acquired the lock). 357cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle // 358cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle // If we can't acquire the lock bail out since clearing is useless anyway 359cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle // (the app will write again to the profile). 360cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle // 361cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle // Note: 362cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle // This does not impact the this is not an issue for the profiling correctness. 363cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle // In case this is needed because of an app upgrade, profiles will still be 364cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle // eventually cleared by the app itself due to checksum mismatch. 365cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle // If this is needed because profman advised, then keeping the data around 366cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle // until the next run is again not an issue. 367cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle // 368cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle // If the app attempts to acquire a lock while we've held one here, 369cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle // it will simply skip the current write cycle. 370cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle return false; 371cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle } 372cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle 37364b59cc1c89225813295d1b5d6bf4183615da204Roland Levillain bool truncated = ftruncate(ufd.get(), 0) == 0; 374cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle if (!truncated) { 375cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle PLOG(WARNING) << "Could not truncate " << profile; 376cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle } 37764b59cc1c89225813295d1b5d6bf4183615da204Roland Levillain if (flock(ufd.get(), LOCK_UN) != 0) { 378cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle PLOG(WARNING) << "Error unlocking profile " << profile; 379cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle } 380cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle return truncated; 381cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle} 382cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle 383cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravlestatic bool clear_reference_profile(const char* pkgname) { 384cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle std::string reference_profile_dir = create_data_ref_profile_package_path(pkgname); 385cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle std::string reference_profile = create_primary_profile(reference_profile_dir); 386cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle return clear_profile(reference_profile); 3876a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle} 3886a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 389cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravlestatic bool clear_current_profile(const char* pkgname, userid_t user) { 390edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle std::string profile_dir = create_data_user_profile_package_path(user, pkgname); 391edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle std::string profile = create_primary_profile(profile_dir); 392cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle return clear_profile(profile); 393edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle} 394edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle 395cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravlestatic bool clear_current_profiles(const char* pkgname) { 396d828d6839f6a01e3f0b2b8b2694139dfa801c557David Brazdil bool success = true; 3976a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle std::vector<userid_t> users = get_known_users(/*volume_uuid*/ nullptr); 3986a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle for (auto user : users) { 399cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle success &= clear_current_profile(pkgname, user); 4006a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 401d828d6839f6a01e3f0b2b8b2694139dfa801c557David Brazdil return success; 4026a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle} 4036a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 404edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravleint clear_app_profiles(const char* pkgname) { 405d828d6839f6a01e3f0b2b8b2694139dfa801c557David Brazdil bool success = true; 406cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle success &= clear_reference_profile(pkgname); 407cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle success &= clear_current_profiles(pkgname); 408edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle return success ? 0 : -1; 4096a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle} 4106a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 4112f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkeyint clear_app_data(const char *uuid, const char *pkgname, userid_t userid, int flags, 4122f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey ino_t ce_data_inode) { 413ebf728fd43ab5c7d11a1f9e5fdc775d6740fae0aJeff Sharkey int res = 0; 414aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (flags & FLAG_STORAGE_CE) { 4159a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey auto path = create_data_user_ce_package_path(uuid, userid, pkgname, ce_data_inode); 4169a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey if (flags & FLAG_CLEAR_CACHE_ONLY) { 4179a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey path = read_path_inode(path, "cache", kXattrInodeCache); 4189a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey } else if (flags & FLAG_CLEAR_CODE_CACHE_ONLY) { 4199a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey path = read_path_inode(path, "code_cache", kXattrInodeCodeCache); 4209a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey } 421c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey if (access(path.c_str(), F_OK) == 0) { 422c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey res |= delete_dir_contents(path); 423c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey } 42494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 425aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (flags & FLAG_STORAGE_DE) { 4269a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey std::string suffix = ""; 4279a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey bool only_cache = false; 4289a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey if (flags & FLAG_CLEAR_CACHE_ONLY) { 4299a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey suffix = CACHE_DIR_POSTFIX; 4309a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey only_cache = true; 4319a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey } else if (flags & FLAG_CLEAR_CODE_CACHE_ONLY) { 4329a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey suffix = CODE_CACHE_DIR_POSTFIX; 4339a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey only_cache = true; 4349a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey } 4359a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey 436c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey auto path = create_data_user_de_package_path(uuid, userid, pkgname) + suffix; 437c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey if (access(path.c_str(), F_OK) == 0) { 438a03c747ff6e1afb5c1dc86560705bd979cd5c49fJeff Sharkey // TODO: include result once 25796509 is fixed 439a03c747ff6e1afb5c1dc86560705bd979cd5c49fJeff Sharkey delete_dir_contents(path); 440c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey } 441edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle if (!only_cache) { 442cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle if (!clear_current_profile(pkgname, userid)) { 443edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle res |= -1; 444edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle } 445edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle } 44694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 447c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey return res; 448c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey} 44994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 4507535e8e1995398befc866062d63c05d90eb70cccCalin Juravlestatic int destroy_app_reference_profile(const char *pkgname) { 4517535e8e1995398befc866062d63c05d90eb70cccCalin Juravle return delete_dir_contents_and_dir( 4527535e8e1995398befc866062d63c05d90eb70cccCalin Juravle create_data_ref_profile_package_path(pkgname), 4537535e8e1995398befc866062d63c05d90eb70cccCalin Juravle /*ignore_if_missing*/ true); 4547535e8e1995398befc866062d63c05d90eb70cccCalin Juravle} 4557535e8e1995398befc866062d63c05d90eb70cccCalin Juravle 456edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravlestatic int destroy_app_current_profiles(const char *pkgname, userid_t userid) { 457b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravle return delete_dir_contents_and_dir( 458b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravle create_data_user_profile_package_path(userid, pkgname), 459b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravle /*ignore_if_missing*/ true); 460edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle} 461edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle 462edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravleint destroy_app_profiles(const char *pkgname) { 463edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle int result = 0; 464edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle std::vector<userid_t> users = get_known_users(/*volume_uuid*/ nullptr); 465edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle for (auto user : users) { 466edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle result |= destroy_app_current_profiles(pkgname, user); 467edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle } 4687535e8e1995398befc866062d63c05d90eb70cccCalin Juravle result |= destroy_app_reference_profile(pkgname); 469edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle return result; 470caa6b809a5ed024a8f3f551c5c109a5ad50ab772Calin Juravle} 471caa6b809a5ed024a8f3f551c5c109a5ad50ab772Calin Juravle 4722f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkeyint destroy_app_data(const char *uuid, const char *pkgname, userid_t userid, int flags, 4732f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey ino_t ce_data_inode) { 474c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey int res = 0; 475aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (flags & FLAG_STORAGE_CE) { 476c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey res |= delete_dir_contents_and_dir( 4772f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey create_data_user_ce_package_path(uuid, userid, pkgname, ce_data_inode)); 47894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 479aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (flags & FLAG_STORAGE_DE) { 480caa6b809a5ed024a8f3f551c5c109a5ad50ab772Calin Juravle res |= delete_dir_contents_and_dir( 481c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey create_data_user_de_package_path(uuid, userid, pkgname)); 482b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravle destroy_app_current_profiles(pkgname, userid); 4837535e8e1995398befc866062d63c05d90eb70cccCalin Juravle // TODO(calin): If the package is still installed by other users it's probably 4847535e8e1995398befc866062d63c05d90eb70cccCalin Juravle // beneficial to keep the reference profile around. 4857535e8e1995398befc866062d63c05d90eb70cccCalin Juravle // Verify if it's ok to do that. 4867535e8e1995398befc866062d63c05d90eb70cccCalin Juravle destroy_app_reference_profile(pkgname); 48794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 488c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey return res; 48994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 49094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 4910274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeybinder::Status InstalldNativeService::moveCompleteApp(const std::unique_ptr<std::string>& fromUuid, 4920274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const std::unique_ptr<std::string>& toUuid, const std::string& packageName, 4930274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const std::string& dataAppName, int32_t appId, const std::string& seInfo, 4940274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey int32_t targetSdkVersion) { 4950274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 4960274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const char* from_uuid = fromUuid ? fromUuid->c_str() : nullptr; 4970274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const char* to_uuid = toUuid ? toUuid->c_str() : nullptr; 4980274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const char* package_name = packageName.c_str(); 4990274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const char* data_app_name = dataAppName.c_str(); 5000274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const char* seinfo = seInfo.c_str(); 5010274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 502e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey std::vector<userid_t> users = get_known_users(from_uuid); 503e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 504d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey // Copy app 505d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey { 50651c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto from = create_data_app_package_path(from_uuid, data_app_name); 50751c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto to = create_data_app_package_path(to_uuid, data_app_name); 50851c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto to_parent = create_data_app_path(to_uuid); 509d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey 510d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey char *argv[] = { 511d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey (char*) kCpPath, 512d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey (char*) "-F", /* delete any existing destination file first (--remove-destination) */ 513d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey (char*) "-p", /* preserve timestamps, ownership, and permissions */ 514d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey (char*) "-R", /* recurse into subdirectories (DEST must be a directory) */ 515d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey (char*) "-P", /* Do not follow symlinks [default] */ 516d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey (char*) "-d", /* don't dereference symlinks */ 517d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey (char*) from.c_str(), 518d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey (char*) to_parent.c_str() 519d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey }; 520d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey 521d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey LOG(DEBUG) << "Copying " << from << " to " << to; 522d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true); 523d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey 524d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey if (rc != 0) { 525d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey LOG(ERROR) << "Failed copying " << from << " to " << to 526d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey << ": status " << rc; 527d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey goto fail; 528d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey } 529d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey 530d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey if (selinux_android_restorecon(to.c_str(), SELINUX_ANDROID_RESTORECON_RECURSE) != 0) { 531d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey LOG(ERROR) << "Failed to restorecon " << to; 532d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey goto fail; 533d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey } 534d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey } 535d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey 536d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey // Copy private data for all known users 537e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey for (auto user : users) { 538e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 539e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey // Data source may not exist for all users; that's okay 54051c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto from_ce = create_data_user_ce_package_path(from_uuid, user, package_name); 54151c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey if (access(from_ce.c_str(), F_OK) != 0) { 54251c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey LOG(INFO) << "Missing source " << from_ce; 543e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey continue; 544e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey } 545e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 5460274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey if (!createAppData(toUuid, packageName, user, FLAG_STORAGE_CE | FLAG_STORAGE_DE, appId, 5470274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey seInfo, targetSdkVersion).isOk()) { 54851c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey LOG(ERROR) << "Failed to create package target on " << to_uuid; 549e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey goto fail; 550e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey } 551e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 552e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey char *argv[] = { 553e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey (char*) kCpPath, 554e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey (char*) "-F", /* delete any existing destination file first (--remove-destination) */ 555e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey (char*) "-p", /* preserve timestamps, ownership, and permissions */ 556e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey (char*) "-R", /* recurse into subdirectories (DEST must be a directory) */ 557e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey (char*) "-P", /* Do not follow symlinks [default] */ 558e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey (char*) "-d", /* don't dereference symlinks */ 55951c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey nullptr, 56051c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey nullptr 561e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey }; 562e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 56351c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey { 56451c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto from = create_data_user_de_package_path(from_uuid, user, package_name); 56551c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto to = create_data_user_de_path(to_uuid, user); 56651c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey argv[6] = (char*) from.c_str(); 56751c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey argv[7] = (char*) to.c_str(); 56851c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey 56951c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey LOG(DEBUG) << "Copying " << from << " to " << to; 57051c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true); 57151c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey if (rc != 0) { 57251c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey LOG(ERROR) << "Failed copying " << from << " to " << to << " with status " << rc; 57351c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey goto fail; 57451c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey } 57551c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey } 57651c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey { 57751c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto from = create_data_user_ce_package_path(from_uuid, user, package_name); 57851c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto to = create_data_user_ce_path(to_uuid, user); 57951c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey argv[6] = (char*) from.c_str(); 58051c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey argv[7] = (char*) to.c_str(); 58151c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey 58251c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey LOG(DEBUG) << "Copying " << from << " to " << to; 58351c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true); 58451c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey if (rc != 0) { 58551c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey LOG(ERROR) << "Failed copying " << from << " to " << to << " with status " << rc; 58651c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey goto fail; 58751c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey } 588e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey } 589e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 590aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (restorecon_app_data(to_uuid, package_name, user, FLAG_STORAGE_CE | FLAG_STORAGE_DE, 5910274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey appId, seinfo) != 0) { 592c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey LOG(ERROR) << "Failed to restorecon"; 593c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey goto fail; 594c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey } 595e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey } 596e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 59731f08986f83fa6f2dcf55523b2cf706460aeed7cJeff Sharkey // We let the framework scan the new location and persist that before 59831f08986f83fa6f2dcf55523b2cf706460aeed7cJeff Sharkey // deleting the data in the old location; this ordering ensures that 59931f08986f83fa6f2dcf55523b2cf706460aeed7cJeff Sharkey // we can recover from things like battery pulls. 6000274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey return binder::Status::ok(); 601e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 602e36372423000a906bafae68844ebc6c42d09335aJeff Sharkeyfail: 603e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey // Nuke everything we might have already copied 604d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey { 60551c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto to = create_data_app_package_path(to_uuid, data_app_name); 606d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey if (delete_dir_contents(to.c_str(), 1, NULL) != 0) { 607d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey LOG(WARNING) << "Failed to rollback " << to; 608d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey } 609d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey } 610e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey for (auto user : users) { 61151c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey { 61251c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto to = create_data_user_de_package_path(to_uuid, user, package_name); 61351c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey if (delete_dir_contents(to.c_str(), 1, NULL) != 0) { 61451c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey LOG(WARNING) << "Failed to rollback " << to; 61551c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey } 61651c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey } 61751c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey { 61851c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto to = create_data_user_ce_package_path(to_uuid, user, package_name); 61951c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey if (delete_dir_contents(to.c_str(), 1, NULL) != 0) { 62051c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey LOG(WARNING) << "Failed to rollback " << to; 62151c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey } 622e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey } 623e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey } 6240274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey return binder::Status::fromServiceSpecificError(-1); 625e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey} 626e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 627379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkeyint create_user_data(const char *uuid, userid_t userid, int user_serial ATTRIBUTE_UNUSED, 628379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey int flags) { 629379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey if (flags & FLAG_STORAGE_DE) { 630379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey if (uuid == nullptr) { 631379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey return ensure_config_user_dirs(userid); 632379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey } 63394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 634095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee return 0; 635095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee} 636095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee 637379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkeyint destroy_user_data(const char *uuid, userid_t userid, int flags) { 638ebf728fd43ab5c7d11a1f9e5fdc775d6740fae0aJeff Sharkey int res = 0; 639379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey if (flags & FLAG_STORAGE_DE) { 640379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey res |= delete_dir_contents_and_dir(create_data_user_de_path(uuid, userid), true); 641379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey if (uuid == nullptr) { 642379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey res |= delete_dir_contents_and_dir(create_data_misc_legacy_path(userid), true); 643379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey res |= delete_dir_contents_and_dir(create_data_user_profiles_path(userid), true); 64441ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey } 64594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 646379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey if (flags & FLAG_STORAGE_CE) { 647379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey res |= delete_dir_contents_and_dir(create_data_user_ce_path(uuid, userid), true); 648379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey res |= delete_dir_contents_and_dir(create_data_media_path(uuid, userid), true); 649379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey } 650ebf728fd43ab5c7d11a1f9e5fdc775d6740fae0aJeff Sharkey return res; 65194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 65294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 65394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/* Try to ensure free_size bytes of storage are available. 65494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * Returns 0 on success. 65594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * This is rather simple-minded because doing a full LRU would 65694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * be potentially memory-intensive, and without atime it would 65794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * also require that apps constantly modify file metadata even 65894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * when just reading from the cache, which is pretty awful. 65994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */ 66054e292e1ea87b504b552393d2e9bc800458bd0c1Jeff Sharkeyint free_cache(const char *uuid, int64_t free_size) { 66194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache_t* cache; 66294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int64_t avail; 66394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 66454e292e1ea87b504b552393d2e9bc800458bd0c1Jeff Sharkey auto data_path = create_data_path(uuid); 66541ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey 66641ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey avail = data_disk_free(data_path); 66794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (avail < 0) return -1; 66894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 66994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGI("free_cache(%" PRId64 ") avail %" PRId64 "\n", free_size, avail); 67094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (avail >= free_size) return 0; 67194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 67294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache = start_cache_collection(); 67394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 67454e292e1ea87b504b552393d2e9bc800458bd0c1Jeff Sharkey auto users = get_known_users(uuid); 67554e292e1ea87b504b552393d2e9bc800458bd0c1Jeff Sharkey for (auto user : users) { 67654e292e1ea87b504b552393d2e9bc800458bd0c1Jeff Sharkey add_cache_files(cache, create_data_user_ce_path(uuid, user)); 67754e292e1ea87b504b552393d2e9bc800458bd0c1Jeff Sharkey add_cache_files(cache, create_data_user_de_path(uuid, user)); 67854e292e1ea87b504b552393d2e9bc800458bd0c1Jeff Sharkey add_cache_files(cache, 67954e292e1ea87b504b552393d2e9bc800458bd0c1Jeff Sharkey StringPrintf("%s/Android/data", create_data_media_path(uuid, user).c_str())); 68094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 68194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 68241ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey clear_cache_files(data_path, cache, free_size); 68394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood finish_cache_collection(cache); 68494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 68541ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey return data_disk_free(data_path) >= free_size ? 0 : -1; 68694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 68794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 6881b4003207750ea8fe8c7b03eb32d80f1df83979eNarayan Kamathint rm_dex(const char *path, const char *instruction_set) 68994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 69094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood char dex_path[PKG_PATH_MAX]; 69194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 692770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey if (validate_apk_path(path) && validate_system_app_path(path)) { 693770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey ALOGE("invalid apk path '%s' (bad prefix)\n", path); 694770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey return -1; 695770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey } 696770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey 69702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe if (!create_cache_path(dex_path, path, instruction_set)) return -1; 69894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 69994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGV("unlink %s\n", dex_path); 70094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (unlink(dex_path) < 0) { 701770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey if (errno != ENOENT) { 702770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey ALOGE("Couldn't unlink %s: %s\n", dex_path, strerror(errno)); 703770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey } 70494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return -1; 70594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else { 70694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return 0; 70794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 70894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 70994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 7102f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkeystatic void add_app_data_size(std::string& path, int64_t *codesize, int64_t *datasize, 7112f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey int64_t *cachesize) { 71294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood DIR *d; 71394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int dfd; 71494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood struct dirent *de; 71594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood struct stat s; 716d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey 7172f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey d = opendir(path.c_str()); 7182f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey if (d == nullptr) { 7192f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey PLOG(WARNING) << "Failed to open " << path; 7202f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey return; 72194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 7222f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey dfd = dirfd(d); 7232f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey while ((de = readdir(d))) { 7242f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey const char *name = de->d_name; 725d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey 7262f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey int64_t statsize = 0; 7272f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) { 7282f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey statsize = stat_size(&s); 72994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 73094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 7312f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey if (de->d_type == DT_DIR) { 7322f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey int subfd; 7332f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey int64_t dirsize = 0; 7342f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey /* always skip "." and ".." */ 7352f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey if (name[0] == '.') { 7362f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey if (name[1] == 0) continue; 7372f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey if ((name[1] == '.') && (name[2] == 0)) continue; 7382f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey } 7392f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY); 7402f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey if (subfd >= 0) { 7412f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey dirsize = calculate_dir_size(subfd); 7422f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey close(subfd); 7432f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey } 7442f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey // TODO: check xattrs! 7452f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey if (!strcmp(name, "cache") || !strcmp(name, "code_cache")) { 7462f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey *datasize += statsize; 7472f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey *cachesize += dirsize; 7482f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey } else { 7492f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey *datasize += dirsize + statsize; 7502f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey } 7512f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey } else if (de->d_type == DT_LNK && !strcmp(name, "lib")) { 7522f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey *codesize += statsize; 7532f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey } else { 7542f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey *datasize += statsize; 75594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 75694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 7572f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey closedir(d); 7582f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey} 75994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 7602f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkeyint get_app_size(const char *uuid, const char *pkgname, int userid, int flags, ino_t ce_data_inode, 7612f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey const char *code_path, int64_t *codesize, int64_t *datasize, int64_t *cachesize, 7622f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey int64_t* asecsize) { 7632f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey DIR *d; 7642f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey int dfd; 76594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 7662f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey d = opendir(code_path); 7672f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey if (d != nullptr) { 7682f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey dfd = dirfd(d); 7692f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey *codesize += calculate_dir_size(dfd); 7702f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey closedir(d); 77194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 77294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 7732f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey if (flags & FLAG_STORAGE_CE) { 7742f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey auto path = create_data_user_ce_package_path(uuid, userid, pkgname, ce_data_inode); 7752f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey add_app_data_size(path, codesize, datasize, cachesize); 7762f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey } 7772f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey if (flags & FLAG_STORAGE_DE) { 7782f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey auto path = create_data_user_de_package_path(uuid, userid, pkgname); 7792f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey add_app_data_size(path, codesize, datasize, cachesize); 7802f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey } 78194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 7822f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey *asecsize = 0; 783d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey 7842f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey return 0; 7852f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey} 786d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey 7872f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkeyint get_app_data_inode(const char *uuid, const char *pkgname, int userid, int flags, ino_t *inode) { 7882f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey if (flags & FLAG_STORAGE_CE) { 7892f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey auto path = create_data_user_ce_package_path(uuid, userid, pkgname); 7909a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey return get_path_inode(path, inode); 79194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 7922f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey return -1; 79394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 79494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 795b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Roubanstatic int split_count(const char *str) 796b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban{ 797b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban char *ctx; 798b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban int count = 0; 79902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe char buf[kPropertyValueMax]; 800b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban 801b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban strncpy(buf, str, sizeof(buf)); 802b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban char *pBuf = buf; 803b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban 804b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban while(strtok_r(pBuf, " ", &ctx) != NULL) { 805b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban count++; 806b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban pBuf = NULL; 807b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban } 808b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban 809b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban return count; 810b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban} 811b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban 81214e084d39ac44e9d4d0a0143bf1f111011343d34neo.chaestatic int split(char *buf, const char **argv) 813b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban{ 814b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban char *ctx; 815b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban int count = 0; 816b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban char *tok; 817b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban char *pBuf = buf; 818b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban 819b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban while((tok = strtok_r(pBuf, " ", &ctx)) != NULL) { 820b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban argv[count++] = tok; 821b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban pBuf = NULL; 822b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban } 823b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban 824b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban return count; 825b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban} 826b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban 827fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdilstatic void run_patchoat(int input_oat_fd, int input_vdex_fd, int out_oat_fd, int out_vdex_fd, 828fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil const char* input_oat_file_name, const char* input_vdex_file_name, 829fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil const char* output_oat_file_name, const char* output_vdex_file_name, 830fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil const char *pkgname ATTRIBUTE_UNUSED, const char *instruction_set) 8317365a10689df23334d245b211ce272502ad20669Alex Light{ 8327365a10689df23334d245b211ce272502ad20669Alex Light static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig 8338fc7315a67fdbf9b56cc6fa061f0773be2223a81Calin Juravle static const unsigned int MAX_INSTRUCTION_SET_LEN = 7; 8347365a10689df23334d245b211ce272502ad20669Alex Light 8357365a10689df23334d245b211ce272502ad20669Alex Light static const char* PATCHOAT_BIN = "/system/bin/patchoat"; 8367365a10689df23334d245b211ce272502ad20669Alex Light if (strlen(instruction_set) >= MAX_INSTRUCTION_SET_LEN) { 8377365a10689df23334d245b211ce272502ad20669Alex Light ALOGE("Instruction set %s longer than max length of %d", 8387365a10689df23334d245b211ce272502ad20669Alex Light instruction_set, MAX_INSTRUCTION_SET_LEN); 8397365a10689df23334d245b211ce272502ad20669Alex Light return; 8407365a10689df23334d245b211ce272502ad20669Alex Light } 8417365a10689df23334d245b211ce272502ad20669Alex Light 8427365a10689df23334d245b211ce272502ad20669Alex Light /* input_file_name/input_fd should be the .odex/.oat file that is precompiled. I think*/ 8437365a10689df23334d245b211ce272502ad20669Alex Light char instruction_set_arg[strlen("--instruction-set=") + MAX_INSTRUCTION_SET_LEN]; 8447365a10689df23334d245b211ce272502ad20669Alex Light char input_oat_fd_arg[strlen("--input-oat-fd=") + MAX_INT_LEN]; 845fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil char input_vdex_fd_arg[strlen("--input-vdex-fd=") + MAX_INT_LEN]; 846fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil char output_oat_fd_arg[strlen("--output-oat-fd=") + MAX_INT_LEN]; 847fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil char output_vdex_fd_arg[strlen("--output-vdex-fd=") + MAX_INT_LEN]; 8487365a10689df23334d245b211ce272502ad20669Alex Light const char* patched_image_location_arg = "--patched-image-location=/system/framework/boot.art"; 8497365a10689df23334d245b211ce272502ad20669Alex Light // The caller has already gotten all the locks we need. 8507365a10689df23334d245b211ce272502ad20669Alex Light const char* no_lock_arg = "--no-lock-output"; 8517365a10689df23334d245b211ce272502ad20669Alex Light sprintf(instruction_set_arg, "--instruction-set=%s", instruction_set); 852fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil sprintf(output_oat_fd_arg, "--output-oat-fd=%d", out_oat_fd); 853fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil sprintf(input_oat_fd_arg, "--input-oat-fd=%d", input_oat_fd); 854fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil ALOGV("Running %s isa=%s in-oat-fd=%d (%s) in-vdex-fd=%d (%s) " 855fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil "out-oat-fd=%d (%s) out-vdex-fd=%d (%s)\n", 856fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil PATCHOAT_BIN, instruction_set, 857fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil input_oat_fd, input_oat_file_name, 858fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil input_vdex_fd, input_vdex_file_name, 859fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil out_oat_fd, output_oat_file_name, 860fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil out_vdex_fd, output_vdex_file_name); 8617365a10689df23334d245b211ce272502ad20669Alex Light 8627365a10689df23334d245b211ce272502ad20669Alex Light /* patchoat, patched-image-location, no-lock, isa, input-fd, output-fd */ 863fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil char* argv[9]; 8647365a10689df23334d245b211ce272502ad20669Alex Light argv[0] = (char*) PATCHOAT_BIN; 8657365a10689df23334d245b211ce272502ad20669Alex Light argv[1] = (char*) patched_image_location_arg; 8667365a10689df23334d245b211ce272502ad20669Alex Light argv[2] = (char*) no_lock_arg; 8677365a10689df23334d245b211ce272502ad20669Alex Light argv[3] = instruction_set_arg; 868fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil argv[4] = input_oat_fd_arg; 869fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil argv[5] = input_vdex_fd_arg; 870fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil argv[6] = output_oat_fd_arg; 871fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil argv[7] = output_vdex_fd_arg; 872fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil argv[8] = NULL; 8737365a10689df23334d245b211ce272502ad20669Alex Light 8747365a10689df23334d245b211ce272502ad20669Alex Light execv(PATCHOAT_BIN, (char* const *)argv); 8757365a10689df23334d245b211ce272502ad20669Alex Light ALOGE("execv(%s) failed: %s\n", PATCHOAT_BIN, strerror(errno)); 8767365a10689df23334d245b211ce272502ad20669Alex Light} 8777365a10689df23334d245b211ce272502ad20669Alex Light 87853caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffraystatic void run_dex2oat(int zip_fd, int oat_fd, int input_vdex_fd, int output_vdex_fd, int image_fd, 879fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil const char* input_file_name, const char* output_file_name, int swap_fd, 880fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil const char *instruction_set, const char* compiler_filter, bool vm_safe_mode, 881fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil bool debuggable, bool post_bootcomplete, int profile_fd, const char* shared_libraries) { 8828fc7315a67fdbf9b56cc6fa061f0773be2223a81Calin Juravle static const unsigned int MAX_INSTRUCTION_SET_LEN = 7; 8838fc7315a67fdbf9b56cc6fa061f0773be2223a81Calin Juravle 8848fc7315a67fdbf9b56cc6fa061f0773be2223a81Calin Juravle if (strlen(instruction_set) >= MAX_INSTRUCTION_SET_LEN) { 8858fc7315a67fdbf9b56cc6fa061f0773be2223a81Calin Juravle ALOGE("Instruction set %s longer than max length of %d", 8868fc7315a67fdbf9b56cc6fa061f0773be2223a81Calin Juravle instruction_set, MAX_INSTRUCTION_SET_LEN); 8878fc7315a67fdbf9b56cc6fa061f0773be2223a81Calin Juravle return; 8888fc7315a67fdbf9b56cc6fa061f0773be2223a81Calin Juravle } 8898fc7315a67fdbf9b56cc6fa061f0773be2223a81Calin Juravle 89002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe char dex2oat_Xms_flag[kPropertyValueMax]; 89102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe bool have_dex2oat_Xms_flag = get_property("dalvik.vm.dex2oat-Xms", dex2oat_Xms_flag, NULL) > 0; 892e46a75a0f6007967cd0d161959af844772cdc330Brian Carlstrom 89302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe char dex2oat_Xmx_flag[kPropertyValueMax]; 89402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe bool have_dex2oat_Xmx_flag = get_property("dalvik.vm.dex2oat-Xmx", dex2oat_Xmx_flag, NULL) > 0; 895e46a75a0f6007967cd0d161959af844772cdc330Brian Carlstrom 89602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe char dex2oat_threads_buf[kPropertyValueMax]; 89702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe bool have_dex2oat_threads_flag = get_property(post_bootcomplete 898919461cbeef3e9f35388fc099a5fcdae1cb79cc6Andreas Gampe ? "dalvik.vm.dex2oat-threads" 899919461cbeef3e9f35388fc099a5fcdae1cb79cc6Andreas Gampe : "dalvik.vm.boot-dex2oat-threads", 900919461cbeef3e9f35388fc099a5fcdae1cb79cc6Andreas Gampe dex2oat_threads_buf, 901919461cbeef3e9f35388fc099a5fcdae1cb79cc6Andreas Gampe NULL) > 0; 90202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe char dex2oat_threads_arg[kPropertyValueMax + 2]; 9038d7af8b2418cc5e7e59746f0cb359a75ed0bdfd1Andreas Gampe if (have_dex2oat_threads_flag) { 9048d7af8b2418cc5e7e59746f0cb359a75ed0bdfd1Andreas Gampe sprintf(dex2oat_threads_arg, "-j%s", dex2oat_threads_buf); 9058d7af8b2418cc5e7e59746f0cb359a75ed0bdfd1Andreas Gampe } 9068d7af8b2418cc5e7e59746f0cb359a75ed0bdfd1Andreas Gampe 90702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe char dex2oat_isa_features_key[kPropertyKeyMax]; 9088fc7315a67fdbf9b56cc6fa061f0773be2223a81Calin Juravle sprintf(dex2oat_isa_features_key, "dalvik.vm.isa.%s.features", instruction_set); 90902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe char dex2oat_isa_features[kPropertyValueMax]; 91002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe bool have_dex2oat_isa_features = get_property(dex2oat_isa_features_key, 9118fc7315a67fdbf9b56cc6fa061f0773be2223a81Calin Juravle dex2oat_isa_features, NULL) > 0; 9128fc7315a67fdbf9b56cc6fa061f0773be2223a81Calin Juravle 91302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe char dex2oat_isa_variant_key[kPropertyKeyMax]; 91416a95b267ac6aac75453f65f1bc382ce65c0741eIan Rogers sprintf(dex2oat_isa_variant_key, "dalvik.vm.isa.%s.variant", instruction_set); 91502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe char dex2oat_isa_variant[kPropertyValueMax]; 91602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe bool have_dex2oat_isa_variant = get_property(dex2oat_isa_variant_key, 91716a95b267ac6aac75453f65f1bc382ce65c0741eIan Rogers dex2oat_isa_variant, NULL) > 0; 91816a95b267ac6aac75453f65f1bc382ce65c0741eIan Rogers 91914e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae const char *dex2oat_norelocation = "-Xnorelocate"; 92014e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae bool have_dex2oat_relocation_skip_flag = false; 92114e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae 92202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe char dex2oat_flags[kPropertyValueMax]; 92302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe int dex2oat_flags_count = get_property("dalvik.vm.dex2oat-flags", 924b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban dex2oat_flags, NULL) <= 0 ? 0 : split_count(dex2oat_flags); 9250ae8e39ebcc26836ba55a5ee4481825a0f473a9eBrian Carlstrom ALOGV("dalvik.vm.dex2oat-flags=%s\n", dex2oat_flags); 9260ae8e39ebcc26836ba55a5ee4481825a0f473a9eBrian Carlstrom 927538998f204d1e542e235de9e7ce18ef4dc68c9ccBrian Carlstrom // If we booting without the real /data, don't spend time compiling. 92802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe char vold_decrypt[kPropertyValueMax]; 92902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe bool have_vold_decrypt = get_property("vold.decrypt", vold_decrypt, "") > 0; 930538998f204d1e542e235de9e7ce18ef4dc68c9ccBrian Carlstrom bool skip_compilation = (have_vold_decrypt && 931538998f204d1e542e235de9e7ce18ef4dc68c9ccBrian Carlstrom (strcmp(vold_decrypt, "trigger_restart_min_framework") == 0 || 932538998f204d1e542e235de9e7ce18ef4dc68c9ccBrian Carlstrom (strcmp(vold_decrypt, "1") == 0))); 933538998f204d1e542e235de9e7ce18ef4dc68c9ccBrian Carlstrom 9346a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle bool generate_debug_info = property_get_bool("debug.generate-debug-info"); 935d4a7b459f4f383988440c0512513b321ce03fc22Mathieu Chartier 936416fa12cd0949c7f12e2067b36ac7ac64ba70b4aMathieu Chartier char app_image_format[kPropertyValueMax]; 937416fa12cd0949c7f12e2067b36ac7ac64ba70b4aMathieu Chartier char image_format_arg[strlen("--image-format=") + kPropertyValueMax]; 938416fa12cd0949c7f12e2067b36ac7ac64ba70b4aMathieu Chartier bool have_app_image_format = 93941fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier image_fd >= 0 && get_property("dalvik.vm.appimageformat", app_image_format, NULL) > 0; 940416fa12cd0949c7f12e2067b36ac7ac64ba70b4aMathieu Chartier if (have_app_image_format) { 941416fa12cd0949c7f12e2067b36ac7ac64ba70b4aMathieu Chartier sprintf(image_format_arg, "--image-format=%s", app_image_format); 942416fa12cd0949c7f12e2067b36ac7ac64ba70b4aMathieu Chartier } 943416fa12cd0949c7f12e2067b36ac7ac64ba70b4aMathieu Chartier 944a5cc10a8e8d47fd01c117cc51ac1f6765a1735ceAndreas Gampe char dex2oat_large_app_threshold[kPropertyValueMax]; 945a5cc10a8e8d47fd01c117cc51ac1f6765a1735ceAndreas Gampe bool have_dex2oat_large_app_threshold = 946a5cc10a8e8d47fd01c117cc51ac1f6765a1735ceAndreas Gampe get_property("dalvik.vm.dex2oat-very-large", dex2oat_large_app_threshold, NULL) > 0; 947a5cc10a8e8d47fd01c117cc51ac1f6765a1735ceAndreas Gampe char dex2oat_large_app_threshold_arg[strlen("--very-large-app-threshold=") + kPropertyValueMax]; 948a5cc10a8e8d47fd01c117cc51ac1f6765a1735ceAndreas Gampe if (have_dex2oat_large_app_threshold) { 949a5cc10a8e8d47fd01c117cc51ac1f6765a1735ceAndreas Gampe sprintf(dex2oat_large_app_threshold_arg, 950a5cc10a8e8d47fd01c117cc51ac1f6765a1735ceAndreas Gampe "--very-large-app-threshold=%s", 951a5cc10a8e8d47fd01c117cc51ac1f6765a1735ceAndreas Gampe dex2oat_large_app_threshold); 952a5cc10a8e8d47fd01c117cc51ac1f6765a1735ceAndreas Gampe } 953a5cc10a8e8d47fd01c117cc51ac1f6765a1735ceAndreas Gampe 9541705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom static const char* DEX2OAT_BIN = "/system/bin/dex2oat"; 95553e0776d967324e2908e3be56b80cddb2c9d9e03Brian Carlstrom 95653e0776d967324e2908e3be56b80cddb2c9d9e03Brian Carlstrom static const char* RUNTIME_ARG = "--runtime-arg"; 95753e0776d967324e2908e3be56b80cddb2c9d9e03Brian Carlstrom 9581705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig 9591b4003207750ea8fe8c7b03eb32d80f1df83979eNarayan Kamath 9601705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom char zip_fd_arg[strlen("--zip-fd=") + MAX_INT_LEN]; 9611705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom char zip_location_arg[strlen("--zip-location=") + PKG_PATH_MAX]; 96253caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray char input_vdex_fd_arg[strlen("--input-vdex-fd=") + MAX_INT_LEN]; 96353caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray char output_vdex_fd_arg[strlen("--output-vdex-fd=") + MAX_INT_LEN]; 9641705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom char oat_fd_arg[strlen("--oat-fd=") + MAX_INT_LEN]; 9657195fcc2185c35b45a8f28c79fde4999b38850e8Brian Carlstrom char oat_location_arg[strlen("--oat-location=") + PKG_PATH_MAX]; 9661b4003207750ea8fe8c7b03eb32d80f1df83979eNarayan Kamath char instruction_set_arg[strlen("--instruction-set=") + MAX_INSTRUCTION_SET_LEN]; 96702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe char instruction_set_variant_arg[strlen("--instruction-set-variant=") + kPropertyValueMax]; 96802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe char instruction_set_features_arg[strlen("--instruction-set-features=") + kPropertyValueMax]; 96902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe char dex2oat_Xms_arg[strlen("-Xms") + kPropertyValueMax]; 97002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe char dex2oat_Xmx_arg[strlen("-Xmx") + kPropertyValueMax]; 97102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe char dex2oat_compiler_filter_arg[strlen("--compiler-filter=") + kPropertyValueMax]; 972e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe bool have_dex2oat_swap_fd = false; 973e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe char dex2oat_swap_fd[strlen("--swap-fd=") + MAX_INT_LEN]; 974edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier bool have_dex2oat_image_fd = false; 975edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier char dex2oat_image_fd[strlen("--app-image-fd=") + MAX_INT_LEN]; 9761705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom 9771705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom sprintf(zip_fd_arg, "--zip-fd=%d", zip_fd); 9781705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom sprintf(zip_location_arg, "--zip-location=%s", input_file_name); 97953caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray sprintf(input_vdex_fd_arg, "--input-vdex-fd=%d", input_vdex_fd); 98053caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray sprintf(output_vdex_fd_arg, "--output-vdex-fd=%d", output_vdex_fd); 9811705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom sprintf(oat_fd_arg, "--oat-fd=%d", oat_fd); 9821705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom sprintf(oat_location_arg, "--oat-location=%s", output_file_name); 9831b4003207750ea8fe8c7b03eb32d80f1df83979eNarayan Kamath sprintf(instruction_set_arg, "--instruction-set=%s", instruction_set); 98416a95b267ac6aac75453f65f1bc382ce65c0741eIan Rogers sprintf(instruction_set_variant_arg, "--instruction-set-variant=%s", dex2oat_isa_variant); 9858fc7315a67fdbf9b56cc6fa061f0773be2223a81Calin Juravle sprintf(instruction_set_features_arg, "--instruction-set-features=%s", dex2oat_isa_features); 986e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe if (swap_fd >= 0) { 987e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe have_dex2oat_swap_fd = true; 988e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe sprintf(dex2oat_swap_fd, "--swap-fd=%d", swap_fd); 989e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe } 990edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier if (image_fd >= 0) { 991edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier have_dex2oat_image_fd = true; 992edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier sprintf(dex2oat_image_fd, "--app-image-fd=%d", image_fd); 993edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier } 99457c69c39640a7d6a08b3afb56361d76f7ecf1250Calin Juravle 995e46a75a0f6007967cd0d161959af844772cdc330Brian Carlstrom if (have_dex2oat_Xms_flag) { 996e46a75a0f6007967cd0d161959af844772cdc330Brian Carlstrom sprintf(dex2oat_Xms_arg, "-Xms%s", dex2oat_Xms_flag); 997e46a75a0f6007967cd0d161959af844772cdc330Brian Carlstrom } 998e46a75a0f6007967cd0d161959af844772cdc330Brian Carlstrom if (have_dex2oat_Xmx_flag) { 999e46a75a0f6007967cd0d161959af844772cdc330Brian Carlstrom sprintf(dex2oat_Xmx_arg, "-Xmx%s", dex2oat_Xmx_flag); 1000e46a75a0f6007967cd0d161959af844772cdc330Brian Carlstrom } 10014d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe 10024d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe // Compute compiler filter. 10034d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe 10044d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe bool have_dex2oat_compiler_filter_flag; 1005538998f204d1e542e235de9e7ce18ef4dc68c9ccBrian Carlstrom if (skip_compilation) { 1006e18987efb5e39ca1bed15527b7b82bde55c99669Brian Carlstrom strcpy(dex2oat_compiler_filter_arg, "--compiler-filter=verify-none"); 1007538998f204d1e542e235de9e7ce18ef4dc68c9ccBrian Carlstrom have_dex2oat_compiler_filter_flag = true; 100814e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae have_dex2oat_relocation_skip_flag = true; 1009b1efac103523efccbe671e76cc0eaaeab810415bCalin Juravle } else if (vm_safe_mode) { 1010b1efac103523efccbe671e76cc0eaaeab810415bCalin Juravle strcpy(dex2oat_compiler_filter_arg, "--compiler-filter=interpret-only"); 101197477d203eaf0c3235bbe2415356f20a0431cadaCalin Juravle have_dex2oat_compiler_filter_flag = true; 10124d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe } else if (compiler_filter != nullptr && 10134d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe strlen(compiler_filter) + strlen("--compiler-filter=") < 10144d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe arraysize(dex2oat_compiler_filter_arg)) { 10154d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe sprintf(dex2oat_compiler_filter_arg, "--compiler-filter=%s", compiler_filter); 1016d4a7b459f4f383988440c0512513b321ce03fc22Mathieu Chartier have_dex2oat_compiler_filter_flag = true; 10174d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe } else { 10184d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe char dex2oat_compiler_filter_flag[kPropertyValueMax]; 10194d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe have_dex2oat_compiler_filter_flag = get_property("dalvik.vm.dex2oat-filter", 10204d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe dex2oat_compiler_filter_flag, NULL) > 0; 10214d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe if (have_dex2oat_compiler_filter_flag) { 10224d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe sprintf(dex2oat_compiler_filter_arg, 10234d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe "--compiler-filter=%s", 10244d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe dex2oat_compiler_filter_flag); 10254d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe } 1026cf51ba1360ee13459830a0502b0d454d0145544fBrian Carlstrom } 1027e46a75a0f6007967cd0d161959af844772cdc330Brian Carlstrom 1028598c25e23f7c97470e09a2316513ddf2efdfb670Andreas Gampe // Check whether all apps should be compiled debuggable. 1029598c25e23f7c97470e09a2316513ddf2efdfb670Andreas Gampe if (!debuggable) { 103002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe char prop_buf[kPropertyValueMax]; 1031598c25e23f7c97470e09a2316513ddf2efdfb670Andreas Gampe debuggable = 103202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe (get_property("dalvik.vm.always_debuggable", prop_buf, "0") > 0) && 1033598c25e23f7c97470e09a2316513ddf2efdfb670Andreas Gampe (prop_buf[0] == '1'); 1034598c25e23f7c97470e09a2316513ddf2efdfb670Andreas Gampe } 10356a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle char profile_arg[strlen("--profile-file-fd=") + MAX_INT_LEN]; 10366a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle if (profile_fd != -1) { 10376a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle sprintf(profile_arg, "--profile-file-fd=%d", profile_fd); 103860a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle } 1039598c25e23f7c97470e09a2316513ddf2efdfb670Andreas Gampe 10406a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 10411705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom ALOGV("Running %s in=%s out=%s\n", DEX2OAT_BIN, input_file_name, output_file_name); 10424fdff4616699708e58d875273c3c1a85ba4c63bdCalin Juravle 104353caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray const char* argv[9 // program name, mandatory arguments and the final NULL 104414e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae + (have_dex2oat_isa_variant ? 1 : 0) 104514e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae + (have_dex2oat_isa_features ? 1 : 0) 104614e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae + (have_dex2oat_Xms_flag ? 2 : 0) 104714e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae + (have_dex2oat_Xmx_flag ? 2 : 0) 104814e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae + (have_dex2oat_compiler_filter_flag ? 1 : 0) 10498d7af8b2418cc5e7e59746f0cb359a75ed0bdfd1Andreas Gampe + (have_dex2oat_threads_flag ? 1 : 0) 105014e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae + (have_dex2oat_swap_fd ? 1 : 0) 1051edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier + (have_dex2oat_image_fd ? 1 : 0) 105214e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae + (have_dex2oat_relocation_skip_flag ? 2 : 0) 1053528c8ddc60df7b3d291b389117e4db878a20ad48David Srbecky + (generate_debug_info ? 1 : 0) 1054598c25e23f7c97470e09a2316513ddf2efdfb670Andreas Gampe + (debuggable ? 1 : 0) 1055416fa12cd0949c7f12e2067b36ac7ac64ba70b4aMathieu Chartier + (have_app_image_format ? 1 : 0) 105660a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle + dex2oat_flags_count 1057b63d91fd2737680351876406277b6c759f4db33cJeff Hao + (profile_fd == -1 ? 0 : 1) 1058a5cc10a8e8d47fd01c117cc51ac1f6765a1735ceAndreas Gampe + (shared_libraries != nullptr ? 4 : 0) 1059a5cc10a8e8d47fd01c117cc51ac1f6765a1735ceAndreas Gampe + (have_dex2oat_large_app_threshold ? 1 : 0)]; 10604fdff4616699708e58d875273c3c1a85ba4c63bdCalin Juravle int i = 0; 106114e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae argv[i++] = DEX2OAT_BIN; 10624fdff4616699708e58d875273c3c1a85ba4c63bdCalin Juravle argv[i++] = zip_fd_arg; 10634fdff4616699708e58d875273c3c1a85ba4c63bdCalin Juravle argv[i++] = zip_location_arg; 106453caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray argv[i++] = input_vdex_fd_arg; 106553caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray argv[i++] = output_vdex_fd_arg; 10664fdff4616699708e58d875273c3c1a85ba4c63bdCalin Juravle argv[i++] = oat_fd_arg; 10674fdff4616699708e58d875273c3c1a85ba4c63bdCalin Juravle argv[i++] = oat_location_arg; 10684fdff4616699708e58d875273c3c1a85ba4c63bdCalin Juravle argv[i++] = instruction_set_arg; 106916a95b267ac6aac75453f65f1bc382ce65c0741eIan Rogers if (have_dex2oat_isa_variant) { 107016a95b267ac6aac75453f65f1bc382ce65c0741eIan Rogers argv[i++] = instruction_set_variant_arg; 107116a95b267ac6aac75453f65f1bc382ce65c0741eIan Rogers } 10728fc7315a67fdbf9b56cc6fa061f0773be2223a81Calin Juravle if (have_dex2oat_isa_features) { 10738fc7315a67fdbf9b56cc6fa061f0773be2223a81Calin Juravle argv[i++] = instruction_set_features_arg; 10748fc7315a67fdbf9b56cc6fa061f0773be2223a81Calin Juravle } 1075e46a75a0f6007967cd0d161959af844772cdc330Brian Carlstrom if (have_dex2oat_Xms_flag) { 107614e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae argv[i++] = RUNTIME_ARG; 1077e46a75a0f6007967cd0d161959af844772cdc330Brian Carlstrom argv[i++] = dex2oat_Xms_arg; 1078e46a75a0f6007967cd0d161959af844772cdc330Brian Carlstrom } 1079e46a75a0f6007967cd0d161959af844772cdc330Brian Carlstrom if (have_dex2oat_Xmx_flag) { 108014e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae argv[i++] = RUNTIME_ARG; 1081e46a75a0f6007967cd0d161959af844772cdc330Brian Carlstrom argv[i++] = dex2oat_Xmx_arg; 1082e46a75a0f6007967cd0d161959af844772cdc330Brian Carlstrom } 1083cf51ba1360ee13459830a0502b0d454d0145544fBrian Carlstrom if (have_dex2oat_compiler_filter_flag) { 1084cf51ba1360ee13459830a0502b0d454d0145544fBrian Carlstrom argv[i++] = dex2oat_compiler_filter_arg; 1085cf51ba1360ee13459830a0502b0d454d0145544fBrian Carlstrom } 10868d7af8b2418cc5e7e59746f0cb359a75ed0bdfd1Andreas Gampe if (have_dex2oat_threads_flag) { 10878d7af8b2418cc5e7e59746f0cb359a75ed0bdfd1Andreas Gampe argv[i++] = dex2oat_threads_arg; 10888d7af8b2418cc5e7e59746f0cb359a75ed0bdfd1Andreas Gampe } 1089e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe if (have_dex2oat_swap_fd) { 1090e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe argv[i++] = dex2oat_swap_fd; 1091e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe } 1092edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier if (have_dex2oat_image_fd) { 1093edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier argv[i++] = dex2oat_image_fd; 1094edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier } 1095528c8ddc60df7b3d291b389117e4db878a20ad48David Srbecky if (generate_debug_info) { 1096528c8ddc60df7b3d291b389117e4db878a20ad48David Srbecky argv[i++] = "--generate-debug-info"; 10973822b8b79cf533ee8573794176838406c32a1c20Andreas Gampe } 1098598c25e23f7c97470e09a2316513ddf2efdfb670Andreas Gampe if (debuggable) { 1099598c25e23f7c97470e09a2316513ddf2efdfb670Andreas Gampe argv[i++] = "--debuggable"; 1100598c25e23f7c97470e09a2316513ddf2efdfb670Andreas Gampe } 1101416fa12cd0949c7f12e2067b36ac7ac64ba70b4aMathieu Chartier if (have_app_image_format) { 1102416fa12cd0949c7f12e2067b36ac7ac64ba70b4aMathieu Chartier argv[i++] = image_format_arg; 1103416fa12cd0949c7f12e2067b36ac7ac64ba70b4aMathieu Chartier } 1104a5cc10a8e8d47fd01c117cc51ac1f6765a1735ceAndreas Gampe if (have_dex2oat_large_app_threshold) { 1105a5cc10a8e8d47fd01c117cc51ac1f6765a1735ceAndreas Gampe argv[i++] = dex2oat_large_app_threshold_arg; 1106a5cc10a8e8d47fd01c117cc51ac1f6765a1735ceAndreas Gampe } 1107b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban if (dex2oat_flags_count) { 1108b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban i += split(dex2oat_flags, argv + i); 11094fdff4616699708e58d875273c3c1a85ba4c63bdCalin Juravle } 111014e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae if (have_dex2oat_relocation_skip_flag) { 111114e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae argv[i++] = RUNTIME_ARG; 111214e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae argv[i++] = dex2oat_norelocation; 111314e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae } 11146a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle if (profile_fd != -1) { 11156a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle argv[i++] = profile_arg; 111660a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle } 1117b63d91fd2737680351876406277b6c759f4db33cJeff Hao if (shared_libraries != nullptr) { 1118b63d91fd2737680351876406277b6c759f4db33cJeff Hao argv[i++] = RUNTIME_ARG; 1119b63d91fd2737680351876406277b6c759f4db33cJeff Hao argv[i++] = "-classpath"; 1120b63d91fd2737680351876406277b6c759f4db33cJeff Hao argv[i++] = RUNTIME_ARG; 1121b63d91fd2737680351876406277b6c759f4db33cJeff Hao argv[i++] = shared_libraries; 1122b63d91fd2737680351876406277b6c759f4db33cJeff Hao } 1123e46a75a0f6007967cd0d161959af844772cdc330Brian Carlstrom // Do not add after dex2oat_flags, they should override others for debugging. 11244fdff4616699708e58d875273c3c1a85ba4c63bdCalin Juravle argv[i] = NULL; 11254fdff4616699708e58d875273c3c1a85ba4c63bdCalin Juravle 112614e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae execv(DEX2OAT_BIN, (char * const *)argv); 1127b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban ALOGE("execv(%s) failed: %s\n", DEX2OAT_BIN, strerror(errno)); 11281705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom} 11291705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom 1130e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe/* 1131c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe * Whether dexopt should use a swap file when compiling an APK. 1132c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe * 1133c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe * If kAlwaysProvideSwapFile, do this on all devices (dex2oat will make a more informed decision 1134c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe * itself, anyways). 1135c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe * 1136c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe * Otherwise, read "dalvik.vm.dex2oat-swap". If the property exists, return whether it is "true". 1137c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe * 1138c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe * Otherwise, return true if this is a low-mem device. 1139c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe * 1140c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe * Otherwise, return default value. 1141e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe */ 1142c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampestatic bool kAlwaysProvideSwapFile = false; 1143c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampestatic bool kDefaultProvideSwapFile = true; 1144e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe 1145e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampestatic bool ShouldUseSwapFileForDexopt() { 1146e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe if (kAlwaysProvideSwapFile) { 1147e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe return true; 1148e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe } 1149e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe 1150c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe // Check the "override" property. If it exists, return value == "true". 115102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe char dex2oat_prop_buf[kPropertyValueMax]; 115202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe if (get_property("dalvik.vm.dex2oat-swap", dex2oat_prop_buf, "") > 0) { 1153c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe if (strcmp(dex2oat_prop_buf, "true") == 0) { 1154c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe return true; 1155c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe } else { 1156c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe return false; 1157c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe } 1158c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe } 1159c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe 1160c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe // Shortcut for default value. This is an implementation optimization for the process sketched 1161c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe // above. If the default value is true, we can avoid to check whether this is a low-mem device, 1162c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe // as low-mem is never returning false. The compiler will optimize this away if it can. 1163c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe if (kDefaultProvideSwapFile) { 1164c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe return true; 1165c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe } 1166c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe 11676a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle bool is_low_mem = property_get_bool("ro.config.low_ram"); 1168c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe if (is_low_mem) { 1169c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe return true; 1170c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe } 1171c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe 1172c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe // Default value must be false here. 1173c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe return kDefaultProvideSwapFile; 1174e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe} 1175e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe 117694dd3d3c5be4de57278ee668218a3464585795c0Andreas Gampestatic void SetDex2OatAndPatchOatScheduling(bool set_to_bg) { 117794dd3d3c5be4de57278ee668218a3464585795c0Andreas Gampe if (set_to_bg) { 117894dd3d3c5be4de57278ee668218a3464585795c0Andreas Gampe if (set_sched_policy(0, SP_BACKGROUND) < 0) { 117994dd3d3c5be4de57278ee668218a3464585795c0Andreas Gampe ALOGE("set_sched_policy failed: %s\n", strerror(errno)); 118094dd3d3c5be4de57278ee668218a3464585795c0Andreas Gampe exit(70); 118194dd3d3c5be4de57278ee668218a3464585795c0Andreas Gampe } 118294dd3d3c5be4de57278ee668218a3464585795c0Andreas Gampe if (setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND) < 0) { 118394dd3d3c5be4de57278ee668218a3464585795c0Andreas Gampe ALOGE("setpriority failed: %s\n", strerror(errno)); 118494dd3d3c5be4de57278ee668218a3464585795c0Andreas Gampe exit(71); 118594dd3d3c5be4de57278ee668218a3464585795c0Andreas Gampe } 118694dd3d3c5be4de57278ee668218a3464585795c0Andreas Gampe } 118794dd3d3c5be4de57278ee668218a3464585795c0Andreas Gampe} 118894dd3d3c5be4de57278ee668218a3464585795c0Andreas Gampe 11896a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic void close_all_fds(const std::vector<fd_t>& fds, const char* description) { 119060a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle for (size_t i = 0; i < fds.size(); i++) { 119160a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle if (close(fds[i]) != 0) { 119260a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle PLOG(WARNING) << "Failed to close fd for " << description << " at index " << i; 119360a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle } 119460a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle } 119560a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle} 119660a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle 11976a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic fd_t open_profile_dir(const std::string& profile_dir) { 11986a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle fd_t profile_dir_fd = TEMP_FAILURE_RETRY(open(profile_dir.c_str(), 11996a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle O_PATH | O_CLOEXEC | O_DIRECTORY | O_NOFOLLOW)); 12006a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle if (profile_dir_fd < 0) { 12012e063af1651976b50e2da58c4d57564d878ab9c2Narayan Kamath // In a multi-user environment, these directories can be created at 12022e063af1651976b50e2da58c4d57564d878ab9c2Narayan Kamath // different points and it's possible we'll attempt to open a profile 12032e063af1651976b50e2da58c4d57564d878ab9c2Narayan Kamath // dir before it exists. 12042e063af1651976b50e2da58c4d57564d878ab9c2Narayan Kamath if (errno != ENOENT) { 12052e063af1651976b50e2da58c4d57564d878ab9c2Narayan Kamath PLOG(ERROR) << "Failed to open profile_dir: " << profile_dir; 12062e063af1651976b50e2da58c4d57564d878ab9c2Narayan Kamath } 120760a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle } 12086a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle return profile_dir_fd; 120960a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle} 121060a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle 12116a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic fd_t open_primary_profile_file_from_dir(const std::string& profile_dir, mode_t open_mode) { 12126a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle fd_t profile_dir_fd = open_profile_dir(profile_dir); 12136a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle if (profile_dir_fd < 0) { 12146a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle return -1; 12156a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 12166a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 12176a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle fd_t profile_fd = -1; 12186a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle std::string profile_file = create_primary_profile(profile_dir); 12196a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 12206a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle profile_fd = TEMP_FAILURE_RETRY(open(profile_file.c_str(), open_mode | O_NOFOLLOW)); 12216a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle if (profile_fd == -1) { 12226a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle // It's not an error if the profile file does not exist. 122360a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle if (errno != ENOENT) { 12246a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle PLOG(ERROR) << "Failed to lstat profile_dir: " << profile_dir; 122560a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle } 122660a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle } 12276a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle // TODO(calin): use AutoCloseFD instead of closing the fd manually. 12286a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle if (close(profile_dir_fd) != 0) { 12296a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle PLOG(WARNING) << "Could not close profile dir " << profile_dir; 12306a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 12316a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle return profile_fd; 12326a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle} 123360a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle 12346a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic fd_t open_primary_profile_file(userid_t user, const char* pkgname) { 12356a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle std::string profile_dir = create_data_user_profile_package_path(user, pkgname); 12366a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle return open_primary_profile_file_from_dir(profile_dir, O_RDONLY); 12376a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle} 12386a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 12396a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic fd_t open_reference_profile(uid_t uid, const char* pkgname, bool read_write) { 12406a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle std::string reference_profile_dir = create_data_ref_profile_package_path(pkgname); 12416a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle int flags = read_write ? O_RDWR | O_CREAT : O_RDONLY; 12426a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle fd_t fd = open_primary_profile_file_from_dir(reference_profile_dir, flags); 12436a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle if (fd < 0) { 12446a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle return -1; 124560a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle } 12466a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle if (read_write) { 12476a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle // Fix the owner. 12486a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle if (fchown(fd, uid, uid) < 0) { 12496a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle close(fd); 12506a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle return -1; 12516a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 12526a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 12536a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle return fd; 12546a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle} 125560a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle 12566a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic void open_profile_files(uid_t uid, const char* pkgname, 12576a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle /*out*/ std::vector<fd_t>* profiles_fd, /*out*/ fd_t* reference_profile_fd) { 12586a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle // Open the reference profile in read-write mode as profman might need to save the merge. 12596a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle *reference_profile_fd = open_reference_profile(uid, pkgname, /*read_write*/ true); 126060a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle if (*reference_profile_fd < 0) { 12616a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle // We can't access the reference profile file. 126260a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle return; 126360a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle } 12646a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 12656a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle std::vector<userid_t> users = get_known_users(/*volume_uuid*/ nullptr); 12666a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle for (auto user : users) { 12676a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle fd_t profile_fd = open_primary_profile_file(user, pkgname); 12686a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle // Add to the lists only if both fds are valid. 12696a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle if (profile_fd >= 0) { 12706a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle profiles_fd->push_back(profile_fd); 12716a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 12726a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 12736a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle} 12746a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 12756a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic void drop_capabilities(uid_t uid) { 12766a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle if (setgid(uid) != 0) { 12776a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle ALOGE("setgid(%d) failed in installd during dexopt\n", uid); 12786a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle exit(64); 12796a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 12806a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle if (setuid(uid) != 0) { 12816a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle ALOGE("setuid(%d) failed in installd during dexopt\n", uid); 12826a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle exit(65); 12836a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 12846a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle // drop capabilities 12856a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle struct __user_cap_header_struct capheader; 12866a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle struct __user_cap_data_struct capdata[2]; 12876a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle memset(&capheader, 0, sizeof(capheader)); 12886a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle memset(&capdata, 0, sizeof(capdata)); 12896a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle capheader.version = _LINUX_CAPABILITY_VERSION_3; 12906a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle if (capset(&capheader, &capdata[0]) < 0) { 12916a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle ALOGE("capset failed: %s\n", strerror(errno)); 12926a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle exit(66); 129360a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle } 129460a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle} 129560a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle 12966a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic constexpr int PROFMAN_BIN_RETURN_CODE_COMPILE = 0; 12976a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic constexpr int PROFMAN_BIN_RETURN_CODE_SKIP_COMPILATION = 1; 12986a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic constexpr int PROFMAN_BIN_RETURN_CODE_BAD_PROFILES = 2; 12996a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic constexpr int PROFMAN_BIN_RETURN_CODE_ERROR_IO = 3; 13006a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic constexpr int PROFMAN_BIN_RETURN_CODE_ERROR_LOCKING = 4; 13016a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 13026727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehrstatic void run_profman_merge(const std::vector<fd_t>& profiles_fd, fd_t reference_profile_fd) { 13036a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle static const size_t MAX_INT_LEN = 32; 13046a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle static const char* PROFMAN_BIN = "/system/bin/profman"; 13056a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 13066a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle std::vector<std::string> profile_args(profiles_fd.size()); 13076a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle char profile_buf[strlen("--profile-file-fd=") + MAX_INT_LEN]; 13086a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle for (size_t k = 0; k < profiles_fd.size(); k++) { 13096a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle sprintf(profile_buf, "--profile-file-fd=%d", profiles_fd[k]); 13106a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle profile_args[k].assign(profile_buf); 13116a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 13126a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle char reference_profile_arg[strlen("--reference-profile-file-fd=") + MAX_INT_LEN]; 13136a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle sprintf(reference_profile_arg, "--reference-profile-file-fd=%d", reference_profile_fd); 13146a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 13156a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle // program name, reference profile fd, the final NULL and the profile fds 13166a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle const char* argv[3 + profiles_fd.size()]; 13176a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle int i = 0; 13186a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle argv[i++] = PROFMAN_BIN; 13196a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle argv[i++] = reference_profile_arg; 13206a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle for (size_t k = 0; k < profile_args.size(); k++) { 13216a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle argv[i++] = profile_args[k].c_str(); 13226a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 13236a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle // Do not add after dex2oat_flags, they should override others for debugging. 13246a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle argv[i] = NULL; 13256a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 13266a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle execv(PROFMAN_BIN, (char * const *)argv); 13276a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle ALOGE("execv(%s) failed: %s\n", PROFMAN_BIN, strerror(errno)); 13286a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle exit(68); /* only get here on exec failure */ 13296a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle} 13306a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 13316a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle// Decides if profile guided compilation is needed or not based on existing profiles. 13326a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle// Returns true if there is enough information in the current profiles that worth 13336a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle// a re-compilation of the package. 13346a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle// If the return value is true all the current profiles would have been merged into 13356a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle// the reference profiles accessible with open_reference_profile(). 13366a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic bool analyse_profiles(uid_t uid, const char* pkgname) { 13376a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle std::vector<fd_t> profiles_fd; 13386a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle fd_t reference_profile_fd = -1; 13396a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle open_profile_files(uid, pkgname, &profiles_fd, &reference_profile_fd); 13406a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle if (profiles_fd.empty() || (reference_profile_fd == -1)) { 13416a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle // Skip profile guided compilation because no profiles were found. 13426a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle // Or if the reference profile info couldn't be opened. 13436a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle close_all_fds(profiles_fd, "profiles_fd"); 13446a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle if ((reference_profile_fd != - 1) && (close(reference_profile_fd) != 0)) { 13456a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle PLOG(WARNING) << "Failed to close fd for reference profile"; 134660a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle } 13476a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle return false; 13486a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 134960a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle 13506727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr ALOGV("PROFMAN (MERGE): --- BEGIN '%s' ---\n", pkgname); 13516a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 13526a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle pid_t pid = fork(); 13536a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle if (pid == 0) { 13546a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle /* child -- drop privileges before continuing */ 13556a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle drop_capabilities(uid); 13566727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr run_profman_merge(profiles_fd, reference_profile_fd); 13576a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle exit(68); /* only get here on exec failure */ 13586a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 13596a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle /* parent */ 13606a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle int return_code = wait_child(pid); 13616a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle bool need_to_compile = false; 1362cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle bool should_clear_current_profiles = false; 1363cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle bool should_clear_reference_profile = false; 13646a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle if (!WIFEXITED(return_code)) { 13656a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle LOG(WARNING) << "profman failed for package " << pkgname << ": " << return_code; 13666a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } else { 13676a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle return_code = WEXITSTATUS(return_code); 13686a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle switch (return_code) { 13696a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle case PROFMAN_BIN_RETURN_CODE_COMPILE: 13706a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle need_to_compile = true; 1371cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle should_clear_current_profiles = true; 1372cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle should_clear_reference_profile = false; 13736a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle break; 13746a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle case PROFMAN_BIN_RETURN_CODE_SKIP_COMPILATION: 13756a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle need_to_compile = false; 1376cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle should_clear_current_profiles = false; 1377cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle should_clear_reference_profile = false; 13786a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle break; 13796a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle case PROFMAN_BIN_RETURN_CODE_BAD_PROFILES: 13806a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle LOG(WARNING) << "Bad profiles for package " << pkgname; 13816a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle need_to_compile = false; 1382cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle should_clear_current_profiles = true; 1383cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle should_clear_reference_profile = true; 13846a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle break; 13856a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle case PROFMAN_BIN_RETURN_CODE_ERROR_IO: // fall-through 13866a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle case PROFMAN_BIN_RETURN_CODE_ERROR_LOCKING: 13876a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle // Temporary IO problem (e.g. locking). Ignore but log a warning. 13886a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle LOG(WARNING) << "IO error while reading profiles for package " << pkgname; 13896a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle need_to_compile = false; 1390cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle should_clear_current_profiles = false; 1391cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle should_clear_reference_profile = false; 13926a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle break; 13936a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle default: 13946a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle // Unknown return code or error. Unlink profiles. 13956a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle LOG(WARNING) << "Unknown error code while processing profiles for package " << pkgname 13966a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle << ": " << return_code; 13976a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle need_to_compile = false; 1398cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle should_clear_current_profiles = true; 1399cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle should_clear_reference_profile = true; 14006a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle break; 140160a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle } 140260a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle } 14036a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle close_all_fds(profiles_fd, "profiles_fd"); 14046a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle if (close(reference_profile_fd) != 0) { 14056a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle PLOG(WARNING) << "Failed to close fd for reference profile"; 14066a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 1407cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle if (should_clear_current_profiles) { 1408cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle clear_current_profiles(pkgname); 14096a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 1410cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle if (should_clear_reference_profile) { 1411cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle clear_reference_profile(pkgname); 14126a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 14136a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle return need_to_compile; 141460a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle} 141560a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle 14166727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehrstatic void run_profman_dump(const std::vector<fd_t>& profile_fds, 14176727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr fd_t reference_profile_fd, 141840e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr const std::vector<std::string>& dex_locations, 141940e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr const std::vector<fd_t>& apk_fds, 14206727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr fd_t output_fd) { 142140e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr std::vector<std::string> profman_args; 14226727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr static const char* PROFMAN_BIN = "/system/bin/profman"; 142340e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr profman_args.push_back(PROFMAN_BIN); 142440e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr profman_args.push_back("--dump-only"); 142540e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr profman_args.push_back(StringPrintf("--dump-output-to-fd=%d", output_fd)); 142640e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr if (reference_profile_fd != -1) { 142740e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr profman_args.push_back(StringPrintf("--reference-profile-file-fd=%d", 142840e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr reference_profile_fd)); 14296727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr } 14306727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr for (fd_t profile_fd : profile_fds) { 143140e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr profman_args.push_back(StringPrintf("--profile-file-fd=%d", profile_fd)); 14326727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr } 143340e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr for (const std::string& dex_location : dex_locations) { 143440e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr profman_args.push_back(StringPrintf("--dex-location=%s", dex_location.c_str())); 14356727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr } 143640e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr for (fd_t apk_fd : apk_fds) { 143740e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr profman_args.push_back(StringPrintf("--apk-fd=%d", apk_fd)); 143840e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr } 143940e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr const char **argv = new const char*[profman_args.size() + 1]; 144040e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr size_t i = 0; 144140e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr for (const std::string& profman_arg : profman_args) { 144240e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr argv[i++] = profman_arg.c_str(); 14436727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr } 14446727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr argv[i] = NULL; 14456727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr 14466727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr execv(PROFMAN_BIN, (char * const *)argv); 14476727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr ALOGE("execv(%s) failed: %s\n", PROFMAN_BIN, strerror(errno)); 14486727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr exit(68); /* only get here on exec failure */ 14496727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr} 14506727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr 145140e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehrstatic const char* get_location_from_path(const char* path) { 145240e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr static constexpr char kLocationSeparator = '/'; 145340e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr const char *location = strrchr(path, kLocationSeparator); 145440e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr if (location == NULL) { 145540e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr return path; 145640e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr } else { 145740e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr // Skip the separator character. 145840e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr return location + 1; 145940e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr } 146040e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr} 146140e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr 14626727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr// Dumps the contents of a profile file, using pkgname's dex files for pretty 14636727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr// printing the result. 14646727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehrbool dump_profile(uid_t uid, const char* pkgname, const char* code_path_string) { 14656727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr std::vector<fd_t> profile_fds; 14666727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr fd_t reference_profile_fd = -1; 14676727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr std::string out_file_name = StringPrintf("/data/misc/profman/%s.txt", pkgname); 14686727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr 14696727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr ALOGV("PROFMAN (DUMP): --- BEGIN '%s' ---\n", pkgname); 14706727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr 14716727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr open_profile_files(uid, pkgname, &profile_fds, &reference_profile_fd); 14726727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr 14736727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr const bool has_reference_profile = (reference_profile_fd != -1); 14746727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr const bool has_profiles = !profile_fds.empty(); 14756727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr 14766727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr if (!has_reference_profile && !has_profiles) { 14776727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr ALOGE("profman dump: no profiles to dump for '%s'", pkgname); 14786727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr return false; 14796727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr } 14806727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr 148140e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr fd_t output_fd = open(out_file_name.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_NOFOLLOW); 14826727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr if (fchmod(output_fd, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) < 0) { 14836727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr ALOGE("installd cannot chmod '%s' dump_profile\n", out_file_name.c_str()); 14846727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr return false; 14856727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr } 148640e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr std::vector<std::string> code_full_paths = base::Split(code_path_string, ";"); 148740e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr std::vector<std::string> dex_locations; 148840e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr std::vector<fd_t> apk_fds; 148940e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr for (const std::string& code_full_path : code_full_paths) { 149040e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr const char* full_path = code_full_path.c_str(); 149140e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr fd_t apk_fd = open(full_path, O_RDONLY | O_NOFOLLOW); 149240e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr if (apk_fd == -1) { 149340e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr ALOGE("installd cannot open '%s'\n", full_path); 14946727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr return false; 14956727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr } 149640e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr dex_locations.push_back(get_location_from_path(full_path)); 149740e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr apk_fds.push_back(apk_fd); 14986727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr } 14996727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr 15006727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr pid_t pid = fork(); 15016727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr if (pid == 0) { 15026727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr /* child -- drop privileges before continuing */ 15036727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr drop_capabilities(uid); 150440e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr run_profman_dump(profile_fds, reference_profile_fd, dex_locations, 150540e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr apk_fds, output_fd); 15066727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr exit(68); /* only get here on exec failure */ 15076727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr } 15086727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr /* parent */ 150940e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr close_all_fds(apk_fds, "apk_fds"); 15106727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr close_all_fds(profile_fds, "profile_fds"); 15116727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr if (close(reference_profile_fd) != 0) { 15126727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr PLOG(WARNING) << "Failed to close fd for reference profile"; 15136727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr } 15146727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr int return_code = wait_child(pid); 15156727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr if (!WIFEXITED(return_code)) { 15166727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr LOG(WARNING) << "profman failed for package " << pkgname << ": " 15176727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr << return_code; 15186727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr return false; 15196727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr } 15206727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr return true; 15216727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr} 15226727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr 1523fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdilstatic std::string replace_file_extension(const std::string& oat_path, const std::string& new_ext) { 1524fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil // A standard dalvik-cache entry. Replace ".dex" with `new_ext`. 152589b008dfb59ac827db00a45e7de1bac1c65b9c93Andreas Gampe if (EndsWith(oat_path, ".dex")) { 1526fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil std::string new_path = oat_path; 1527fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil new_path.replace(new_path.length() - strlen(".dex"), strlen(".dex"), new_ext); 1528fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil CHECK(EndsWith(new_path, new_ext.c_str())); 1529fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil return new_path; 1530edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier } 153189b008dfb59ac827db00a45e7de1bac1c65b9c93Andreas Gampe 153289b008dfb59ac827db00a45e7de1bac1c65b9c93Andreas Gampe // An odex entry. Not that this may not be an extension, e.g., in the OTA 153389b008dfb59ac827db00a45e7de1bac1c65b9c93Andreas Gampe // case (where the base name will have an extension for the B artifact). 153489b008dfb59ac827db00a45e7de1bac1c65b9c93Andreas Gampe size_t odex_pos = oat_path.rfind(".odex"); 153589b008dfb59ac827db00a45e7de1bac1c65b9c93Andreas Gampe if (odex_pos != std::string::npos) { 1536fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil std::string new_path = oat_path; 1537fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil new_path.replace(odex_pos, strlen(".odex"), new_ext); 1538fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil CHECK_NE(new_path.find(new_ext), std::string::npos); 1539fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil return new_path; 154089b008dfb59ac827db00a45e7de1bac1c65b9c93Andreas Gampe } 154189b008dfb59ac827db00a45e7de1bac1c65b9c93Andreas Gampe 154289b008dfb59ac827db00a45e7de1bac1c65b9c93Andreas Gampe // Don't know how to handle this. 154389b008dfb59ac827db00a45e7de1bac1c65b9c93Andreas Gampe return ""; 1544edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier} 1545edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier 1546fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil// Translate the given oat path to an art (app image) path. An empty string 1547fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil// denotes an error. 1548fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdilstatic std::string create_image_filename(const std::string& oat_path) { 1549fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil return replace_file_extension(oat_path, ".art"); 1550fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil} 1551fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil 1552fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil// Translate the given oat path to a vdex path. An empty string denotes an error. 1553fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdilstatic std::string create_vdex_filename(const std::string& oat_path) { 1554fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil return replace_file_extension(oat_path, ".vdex"); 1555fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil} 1556fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil 155741fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartierstatic bool add_extension_to_file_name(char* file_name, const char* extension) { 155841fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier if (strlen(file_name) + strlen(extension) + 1 > PKG_PATH_MAX) { 155941fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier return false; 1560edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier } 156141fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier strcat(file_name, extension); 156241fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier return true; 156341fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier} 156441fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier 15656fb5a0179d4de9c85351fd016277949804c809edAndreas Gampestatic int open_output_file(const char* file_name, bool recreate, int permissions) { 156641fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier int flags = O_RDWR | O_CREAT; 156741fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier if (recreate) { 15685006f3a6bcfbe73cfae96b3f2659ce42e18bc2eaNarayan Kamath if (unlink(file_name) < 0) { 15695006f3a6bcfbe73cfae96b3f2659ce42e18bc2eaNarayan Kamath if (errno != ENOENT) { 15705006f3a6bcfbe73cfae96b3f2659ce42e18bc2eaNarayan Kamath PLOG(ERROR) << "open_output_file: Couldn't unlink " << file_name; 15715006f3a6bcfbe73cfae96b3f2659ce42e18bc2eaNarayan Kamath } 15725006f3a6bcfbe73cfae96b3f2659ce42e18bc2eaNarayan Kamath } 157341fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier flags |= O_EXCL; 157441fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier } 15755006f3a6bcfbe73cfae96b3f2659ce42e18bc2eaNarayan Kamath return open(file_name, flags, permissions); 1576edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier} 1577edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier 1578edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartierstatic bool set_permissions_and_ownership(int fd, bool is_public, int uid, const char* path) { 1579edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier if (fchmod(fd, 1580edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier S_IRUSR|S_IWUSR|S_IRGRP | 1581edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier (is_public ? S_IROTH : 0)) < 0) { 1582edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier ALOGE("installd cannot chmod '%s' during dexopt\n", path); 1583edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier return false; 1584edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier } else if (fchown(fd, AID_SYSTEM, uid) < 0) { 1585edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier ALOGE("installd cannot chown '%s' during dexopt\n", path); 1586edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier return false; 1587edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier } 1588edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier return true; 1589edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier} 1590edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier 159153caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffraystatic bool IsOutputDalvikCache(const char* oat_dir) { 159253caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray // InstallerConnection.java (which invokes installd) transforms Java null arguments 159353caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray // into '!'. Play it safe by handling it both. 159453caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray // TODO: ensure we never get null. 159553caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray // TODO: pass a flag instead of inferring if the output is dalvik cache. 159653caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray return oat_dir == nullptr || oat_dir[0] == '!'; 159753caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray} 159853caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray 15996a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic bool create_oat_out_path(const char* apk_path, const char* instruction_set, 1600fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil const char* oat_dir, /*out*/ char* out_oat_path) { 16016a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle // Early best-effort check whether we can fit the the path into our buffers. 16026a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle // Note: the cache path will require an additional 5 bytes for ".swap", but we'll try to run 16036a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle // without a swap file, if necessary. Reference profiles file also add an extra ".prof" 16046a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle // extension to the cache path (5 bytes). 16056a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle if (strlen(apk_path) >= (PKG_PATH_MAX - 8)) { 16066a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle ALOGE("apk_path too long '%s'\n", apk_path); 16076a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle return false; 16086a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 16096a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 161053caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray if (!IsOutputDalvikCache(oat_dir)) { 16116a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle if (validate_apk_path(oat_dir)) { 161253caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray ALOGE("cannot validate apk path with oat_dir '%s'\n", oat_dir); 16136a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle return false; 16146a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 1615fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil if (!calculate_oat_file_path(out_oat_path, oat_dir, apk_path, instruction_set)) { 16166a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle return false; 16176a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 16186a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } else { 1619fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil if (!create_cache_path(out_oat_path, apk_path, instruction_set)) { 16206a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle return false; 16216a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 16226a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 16236a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle return true; 16246a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle} 16256a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 16264d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe// TODO: Consider returning error codes. 16274d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampebool merge_profiles(uid_t uid, const char *pkgname) { 16284d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe return analyse_profiles(uid, pkgname); 16294d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe} 16304d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe 1631548bdb930895d8fe6651eaada0bc5739ee5248c8Andreas Gampestatic const char* parse_null(const char* arg) { 1632548bdb930895d8fe6651eaada0bc5739ee5248c8Andreas Gampe if (strcmp(arg, "!") == 0) { 1633548bdb930895d8fe6651eaada0bc5739ee5248c8Andreas Gampe return nullptr; 1634548bdb930895d8fe6651eaada0bc5739ee5248c8Andreas Gampe } else { 1635548bdb930895d8fe6651eaada0bc5739ee5248c8Andreas Gampe return arg; 1636548bdb930895d8fe6651eaada0bc5739ee5248c8Andreas Gampe } 1637548bdb930895d8fe6651eaada0bc5739ee5248c8Andreas Gampe} 1638548bdb930895d8fe6651eaada0bc5739ee5248c8Andreas Gampe 1639d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampeint dexopt(const char* const params[DEXOPT_PARAM_COUNT]) { 1640548bdb930895d8fe6651eaada0bc5739ee5248c8Andreas Gampe return dexopt(params[0], // apk_path 1641548bdb930895d8fe6651eaada0bc5739ee5248c8Andreas Gampe atoi(params[1]), // uid 1642548bdb930895d8fe6651eaada0bc5739ee5248c8Andreas Gampe params[2], // pkgname 1643548bdb930895d8fe6651eaada0bc5739ee5248c8Andreas Gampe params[3], // instruction_set 1644548bdb930895d8fe6651eaada0bc5739ee5248c8Andreas Gampe atoi(params[4]), // dexopt_needed 1645548bdb930895d8fe6651eaada0bc5739ee5248c8Andreas Gampe params[5], // oat_dir 1646548bdb930895d8fe6651eaada0bc5739ee5248c8Andreas Gampe atoi(params[6]), // dexopt_flags 1647548bdb930895d8fe6651eaada0bc5739ee5248c8Andreas Gampe params[7], // compiler_filter 1648548bdb930895d8fe6651eaada0bc5739ee5248c8Andreas Gampe parse_null(params[8]), // volume_uuid 1649548bdb930895d8fe6651eaada0bc5739ee5248c8Andreas Gampe parse_null(params[9])); // shared_libraries 1650548bdb930895d8fe6651eaada0bc5739ee5248c8Andreas Gampe static_assert(DEXOPT_PARAM_COUNT == 10U, "Unexpected dexopt param count"); 1651548bdb930895d8fe6651eaada0bc5739ee5248c8Andreas Gampe} 1652548bdb930895d8fe6651eaada0bc5739ee5248c8Andreas Gampe 16536fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe// Helper for fd management. This is similar to a unique_fd in that it closes the file descriptor 16546fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe// on destruction. It will also run the given cleanup (unless told not to) after closing. 16556fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe// 16566fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe// Usage example: 16576fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe// 16586fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe// Dex2oatFileWrapper<std::function<void ()>> file(open(...), 16596fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe// [name]() { 16606fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe// unlink(name.c_str()); 16616fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe// }); 16626fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe// // Note: care needs to be taken about name, as it needs to have a lifetime longer than the 16636fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe// wrapper if captured as a reference. 16646fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe// 16656fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe// if (file.get() == -1) { 16666fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe// // Error opening... 16676fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe// } 16686fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe// 16696fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe// ... 16706fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe// if (error) { 16716fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe// // At this point, when the Dex2oatFileWrapper is destructed, the cleanup function will run 16726fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe// // and delete the file (after the fd is closed). 16736fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe// return -1; 16746fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe// } 16756fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe// 16766fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe// (Success case) 16776fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe// file.SetCleanup(false); 16786fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe// // At this point, when the Dex2oatFileWrapper is destructed, the cleanup function will not run 16796fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe// // (leaving the file around; after the fd is closed). 16806fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe// 16816fb5a0179d4de9c85351fd016277949804c809edAndreas Gampetemplate <typename Cleanup> 16826fb5a0179d4de9c85351fd016277949804c809edAndreas Gampeclass Dex2oatFileWrapper { 16836fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe public: 16846fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe Dex2oatFileWrapper() : value_(-1), cleanup_(), do_cleanup_(true) { 16856fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe } 16866fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe 16876fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe Dex2oatFileWrapper(int value, Cleanup cleanup) 16886fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe : value_(value), cleanup_(cleanup), do_cleanup_(true) {} 16896fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe 16906fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe ~Dex2oatFileWrapper() { 16916fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe reset(-1); 16926fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe } 16936fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe 16946fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe int get() { 16956fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe return value_; 16966fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe } 16976fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe 16986fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe void SetCleanup(bool cleanup) { 16996fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe do_cleanup_ = cleanup; 17006fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe } 17016fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe 17026fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe void reset(int new_value) { 17036fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe if (value_ >= 0) { 17046fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe close(value_); 17056fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe } 17066fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe if (do_cleanup_ && cleanup_ != nullptr) { 17076fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe cleanup_(); 17086fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe } 17096fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe 17106fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe value_ = new_value; 17116fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe } 17126fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe 17136fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe void reset(int new_value, Cleanup new_cleanup) { 17146fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe if (value_ >= 0) { 17156fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe close(value_); 17166fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe } 17176fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe if (do_cleanup_ && cleanup_ != nullptr) { 17186fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe cleanup_(); 17196fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe } 17206fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe 17216fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe value_ = new_value; 17226fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe cleanup_ = new_cleanup; 17236fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe } 17246fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe 17256fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe private: 17266fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe int value_; 17276fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe Cleanup cleanup_; 17286fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe bool do_cleanup_; 17296fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe}; 17306fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe 173160a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravleint dexopt(const char* apk_path, uid_t uid, const char* pkgname, const char* instruction_set, 17324d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe int dexopt_needed, const char* oat_dir, int dexopt_flags, const char* compiler_filter, 1733b63d91fd2737680351876406277b6c759f4db33cJeff Hao const char* volume_uuid ATTRIBUTE_UNUSED, const char* shared_libraries) 173494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 1735db0326a56ed8cc1ba2ed5ec2810afe49bcd9a596Calin Juravle bool is_public = ((dexopt_flags & DEXOPT_PUBLIC) != 0); 173676e767ca14bcbb4bc809cd1279ece82a3aabe8a4Todd Kennedy bool vm_safe_mode = (dexopt_flags & DEXOPT_SAFEMODE) != 0; 173776e767ca14bcbb4bc809cd1279ece82a3aabe8a4Todd Kennedy bool debuggable = (dexopt_flags & DEXOPT_DEBUGGABLE) != 0; 173876e767ca14bcbb4bc809cd1279ece82a3aabe8a4Todd Kennedy bool boot_complete = (dexopt_flags & DEXOPT_BOOTCOMPLETE) != 0; 17394d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe bool profile_guided = (dexopt_flags & DEXOPT_PROFILE_GUIDED) != 0; 1740db0326a56ed8cc1ba2ed5ec2810afe49bcd9a596Calin Juravle 17414d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe CHECK(pkgname != nullptr); 17424d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe CHECK(pkgname[0] != 0); 1743db0326a56ed8cc1ba2ed5ec2810afe49bcd9a596Calin Juravle 17444d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe // Public apps should not be compiled with profile information ever. Same goes for the special 17454d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe // package '*' used for the system server. 17466fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe Dex2oatFileWrapper<std::function<void ()>> reference_profile_fd; 17474d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe if (!is_public && pkgname[0] != '*') { 17484d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe // Open reference profile in read only mode as dex2oat does not get write permissions. 17496fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe const std::string pkgname_str(pkgname); 17506fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe reference_profile_fd.reset(open_reference_profile(uid, pkgname, /*read_write*/ false), 17516fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe [pkgname_str]() { 17526fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe clear_reference_profile(pkgname_str.c_str()); 17536fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe }); 17544d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe // Note: it's OK to not find a profile here. 175560a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle } 175676e767ca14bcbb4bc809cd1279ece82a3aabe8a4Todd Kennedy 1757e296e00df65461629a20cd03b5b041a59935d00fTodd Kennedy if ((dexopt_flags & ~DEXOPT_MASK) != 0) { 175876e767ca14bcbb4bc809cd1279ece82a3aabe8a4Todd Kennedy LOG_FATAL("dexopt flags contains unknown fields\n"); 175976e767ca14bcbb4bc809cd1279ece82a3aabe8a4Todd Kennedy } 176094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 1761fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil char out_oat_path[PKG_PATH_MAX]; 1762fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil if (!create_oat_out_path(apk_path, instruction_set, oat_dir, out_oat_path)) { 17636a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle return false; 176494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 176594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 17666fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe const char *input_file; 17676fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe char in_odex_path[PKG_PATH_MAX]; 1768920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray int dexopt_action = abs(dexopt_needed); 1769920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray bool is_odex_location = dexopt_needed < 0; 1770920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray switch (dexopt_action) { 1771920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray case DEX2OAT_FROM_SCRATCH: 1772920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray case DEX2OAT_FOR_BOOT_IMAGE: 1773920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray case DEX2OAT_FOR_FILTER: 1774920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray case DEX2OAT_FOR_RELOCATION: 1775c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler input_file = apk_path; 1776c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler break; 1777c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler 1778920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray case PATCHOAT_FOR_RELOCATION: 1779920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray if (is_odex_location) { 1780920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray if (!calculate_odex_file_path(in_odex_path, apk_path, instruction_set)) { 1781920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray return -1; 1782920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray } 1783920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray input_file = in_odex_path; 1784920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray } else { 1785920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray input_file = out_oat_path; 1786c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler } 1787c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler break; 1788c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler 1789c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler default: 1790c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler ALOGE("Invalid dexopt needed: %d\n", dexopt_needed); 17916fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe return 72; 17927365a10689df23334d245b211ce272502ad20669Alex Light } 17937365a10689df23334d245b211ce272502ad20669Alex Light 17946fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe struct stat input_stat; 17957365a10689df23334d245b211ce272502ad20669Alex Light memset(&input_stat, 0, sizeof(input_stat)); 17967365a10689df23334d245b211ce272502ad20669Alex Light stat(input_file, &input_stat); 179794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 1798fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil // Open the input file. If running dex2oat, `input_file` is the APK. If running 1799fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil // patchoat, it is the OAT file to be relocated. 18006fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe base::unique_fd input_fd(open(input_file, O_RDONLY, 0)); 18016fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe if (input_fd.get() < 0) { 18027365a10689df23334d245b211ce272502ad20669Alex Light ALOGE("installd cannot open '%s' for input during dexopt\n", input_file); 180394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return -1; 180494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 180594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 1806fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil // Create the output OAT file. 1807fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil const std::string out_oat_path_str(out_oat_path); 1808fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil Dex2oatFileWrapper<std::function<void ()>> out_oat_fd( 1809fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil open_output_file(out_oat_path, /*recreate*/true, /*permissions*/0644), 1810fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil [out_oat_path_str]() { unlink(out_oat_path_str.c_str()); }); 1811fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil if (out_oat_fd.get() < 0) { 1812fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil ALOGE("installd cannot open '%s' for output during dexopt\n", out_oat_path); 1813fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil return -1; 1814fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil } 1815fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil if (!set_permissions_and_ownership(out_oat_fd.get(), is_public, uid, out_oat_path)) { 1816fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil return -1; 1817fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil } 1818fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil 181953caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray // Open the existing VDEX. We do this before creating the new output VDEX, which will 182053caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray // unlink the old one. 182153caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray base::unique_fd in_vdex_fd; 182253caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray std::string in_vdex_path_str; 1823920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray if (dexopt_action == PATCHOAT_FOR_RELOCATION) { 182453caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray // `input_file` is the OAT file to be relocated. The VDEX has to be there as well. 182553caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray in_vdex_path_str = create_vdex_filename(input_file); 182653caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray if (in_vdex_path_str.empty()) { 182753caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray ALOGE("installd cannot compute input vdex location for '%s'\n", input_file); 182853caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray return -1; 182953caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray } 183053caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray in_vdex_fd.reset(open(in_vdex_path_str.c_str(), O_RDONLY, 0)); 183153caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray if (in_vdex_fd.get() < 0) { 183253caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray ALOGE("installd cannot open '%s' for input during dexopt: %s\n", 183353caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray in_vdex_path_str.c_str(), strerror(errno)); 183453caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray return -1; 183553caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray } 1836920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray } else if (dexopt_action != DEX2OAT_FROM_SCRATCH) { 1837920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray // Open the possibly existing vdex. If none exist, we pass -1 to dex2oat for input-vdex-fd. 1838920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray const char* path = nullptr; 1839920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray if (is_odex_location) { 1840920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray if (calculate_odex_file_path(in_odex_path, apk_path, instruction_set)) { 1841920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray path = in_odex_path; 1842920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray } else { 1843920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray ALOGE("installd cannot compute input vdex location for '%s'\n", apk_path); 1844920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray return -1; 1845920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray } 1846920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray } else { 1847920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray path = out_oat_path; 1848920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray } 1849920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray in_vdex_path_str = create_vdex_filename(path); 185053caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray if (in_vdex_path_str.empty()) { 1851920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray ALOGE("installd cannot compute input vdex location for '%s'\n", path); 185253caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray return -1; 185353caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray } 185453caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray in_vdex_fd.reset(open(in_vdex_path_str.c_str(), O_RDONLY, 0)); 185553caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray } 185653caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray 1857fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil // Infer the name of the output VDEX and create it. 1858fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil const std::string out_vdex_path_str = create_vdex_filename(out_oat_path_str); 1859fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil if (out_vdex_path_str.empty()) { 1860fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil return -1; 1861fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil } 1862fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil Dex2oatFileWrapper<std::function<void ()>> out_vdex_fd( 1863fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil open_output_file(out_vdex_path_str.c_str(), /*recreate*/true, /*permissions*/0644), 1864fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil [out_vdex_path_str]() { unlink(out_vdex_path_str.c_str()); }); 1865fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil if (out_vdex_fd.get() < 0) { 1866fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil ALOGE("installd cannot open '%s' for output during dexopt\n", out_vdex_path_str.c_str()); 18676fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe return -1; 186894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 1869fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil if (!set_permissions_and_ownership(out_vdex_fd.get(), is_public, 1870fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil uid, out_vdex_path_str.c_str())) { 18716fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe return -1; 187294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 187394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 1874e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe // Create a swap file if necessary. 18756fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe base::unique_fd swap_fd; 1876c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler if (ShouldUseSwapFileForDexopt()) { 1877e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe // Make sure there really is enough space. 18786fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe char swap_file_name[PKG_PATH_MAX]; 1879fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil strcpy(swap_file_name, out_oat_path); 188041fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier if (add_extension_to_file_name(swap_file_name, ".swap")) { 18816fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe swap_fd.reset(open_output_file(swap_file_name, /*recreate*/true, /*permissions*/0600)); 188241fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier } 18836fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe if (swap_fd.get() < 0) { 1884edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier // Could not create swap file. Optimistically go on and hope that we can compile 1885edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier // without it. 1886edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier ALOGE("installd could not create '%s' for swap during dexopt\n", swap_file_name); 1887e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe } else { 1888edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier // Immediately unlink. We don't really want to hit flash. 18895006f3a6bcfbe73cfae96b3f2659ce42e18bc2eaNarayan Kamath if (unlink(swap_file_name) < 0) { 18905006f3a6bcfbe73cfae96b3f2659ce42e18bc2eaNarayan Kamath PLOG(ERROR) << "Couldn't unlink swap file " << swap_file_name; 18915006f3a6bcfbe73cfae96b3f2659ce42e18bc2eaNarayan Kamath } 1892e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe } 1893e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe } 1894d93707342a61e66bc3eb2145628158452f577f42Dave Allison 1895e80d58db65d29ed4647e631a81aed644fea00913Mathieu Chartier // Avoid generating an app image for extract only since it will not contain any classes. 18966fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe Dex2oatFileWrapper<std::function<void ()>> image_fd; 1897fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil const std::string image_path = create_image_filename(out_oat_path); 1898920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray if (dexopt_action != PATCHOAT_FOR_RELOCATION && !image_path.empty()) { 18996fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe char app_image_format[kPropertyValueMax]; 19006fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe bool have_app_image_format = 19016fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe get_property("dalvik.vm.appimageformat", app_image_format, NULL) > 0; 19026fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe // Use app images only if it is enabled (by a set image format) and we are compiling 19036fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe // profile-guided (so the app image doesn't conservatively contain all classes). 19046fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe if (profile_guided && have_app_image_format) { 19056fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe // Recreate is true since we do not want to modify a mapped image. If the app is 19066fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe // already running and we modify the image file, it can cause crashes (b/27493510). 190789b008dfb59ac827db00a45e7de1bac1c65b9c93Andreas Gampe image_fd.reset(open_output_file(image_path.c_str(), 19086fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe true /*recreate*/, 19096fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe 0600 /*permissions*/), 191089b008dfb59ac827db00a45e7de1bac1c65b9c93Andreas Gampe [image_path]() { unlink(image_path.c_str()); } 19116fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe ); 19126fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe if (image_fd.get() < 0) { 19136fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe // Could not create application image file. Go on since we can compile without 19146fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe // it. 19156fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe LOG(ERROR) << "installd could not create '" 19166fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe << image_path 19176fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe << "' for image file during dexopt"; 19186fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe } else if (!set_permissions_and_ownership(image_fd.get(), 19196fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe is_public, 19206fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe uid, 192189b008dfb59ac827db00a45e7de1bac1c65b9c93Andreas Gampe image_path.c_str())) { 19226fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe image_fd.reset(-1); 19236fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe } 19246fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe } 19256fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe // If we have a valid image file path but no image fd, explicitly erase the image file. 19266fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe if (image_fd.get() < 0) { 192789b008dfb59ac827db00a45e7de1bac1c65b9c93Andreas Gampe if (unlink(image_path.c_str()) < 0) { 19286fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe if (errno != ENOENT) { 19296fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe PLOG(ERROR) << "Couldn't unlink image file " << image_path; 19306fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe } 19316fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe } 19326fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe } 1933edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier } 1934e80d58db65d29ed4647e631a81aed644fea00913Mathieu Chartier 19357365a10689df23334d245b211ce272502ad20669Alex Light ALOGV("DexInv: --- BEGIN '%s' ---\n", input_file); 193694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 19376fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe pid_t pid = fork(); 193894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (pid == 0) { 193994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood /* child -- drop privileges before continuing */ 19406a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle drop_capabilities(uid); 19416a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 194213f141910ba383404145924df64ad6626fe5b42fAndreas Gampe SetDex2OatAndPatchOatScheduling(boot_complete); 1943fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil if (flock(out_oat_fd.get(), LOCK_EX | LOCK_NB) != 0) { 1944fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil ALOGE("flock(%s) failed: %s\n", out_oat_path, strerror(errno)); 19456fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe _exit(67); 194694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 194794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 1948920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray if (dexopt_action == PATCHOAT_FOR_RELOCATION) { 19496fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe run_patchoat(input_fd.get(), 195053caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray in_vdex_fd.get(), 1951fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil out_oat_fd.get(), 1952fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil out_vdex_fd.get(), 19536fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe input_file, 1954fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil in_vdex_path_str.c_str(), 1955fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil out_oat_path, 1956fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil out_vdex_path_str.c_str(), 19576fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe pkgname, 19586fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe instruction_set); 1959920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray } else { 1960aaa1d4e930f653ec21b711eb852194c8abf35143David Brazdil // Pass dex2oat the relative path to the input file. 196140e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr const char *input_file_name = get_location_from_path(input_file); 19626fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe run_dex2oat(input_fd.get(), 1963fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil out_oat_fd.get(), 196453caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray in_vdex_fd.get(), 1965fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil out_vdex_fd.get(), 19666fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe image_fd.get(), 19676fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe input_file_name, 1968fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil out_oat_path, 19696fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe swap_fd.get(), 19706fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe instruction_set, 19716fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe compiler_filter, 19726fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe vm_safe_mode, 19736fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe debuggable, 19746fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe boot_complete, 19756fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe reference_profile_fd.get(), 19766fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe shared_libraries); 19771705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom } 19786fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe _exit(68); /* only get here on exec failure */ 197994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else { 19806fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe int res = wait_child(pid); 198163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (res == 0) { 19827365a10689df23334d245b211ce272502ad20669Alex Light ALOGV("DexInv: --- END '%s' (success) ---\n", input_file); 198363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } else { 19847365a10689df23334d245b211ce272502ad20669Alex Light ALOGE("DexInv: --- END '%s' --- status=0x%04x, process failed\n", input_file, res); 19856fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe return -1; 198694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 198794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 198894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 19896fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe struct utimbuf ut; 19907365a10689df23334d245b211ce272502ad20669Alex Light ut.actime = input_stat.st_atime; 19917365a10689df23334d245b211ce272502ad20669Alex Light ut.modtime = input_stat.st_mtime; 1992fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil utime(out_oat_path, &ut); 19931705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom 19946fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe // We've been successful, don't delete output. 1995fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil out_oat_fd.SetCleanup(false); 1996fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil out_vdex_fd.SetCleanup(false); 19976fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe image_fd.SetCleanup(false); 19986fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe reference_profile_fd.SetCleanup(false); 199994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 20006fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe return 0; 200194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 200294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 2003091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamathint mark_boot_complete(const char* instruction_set) 2004091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath{ 2005091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath char boot_marker_path[PKG_PATH_MAX]; 200602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe sprintf(boot_marker_path, 200702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe "%s/%s/%s/.booting", 200802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe android_data_dir.path, 200902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe DALVIK_CACHE, 201002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe instruction_set); 2011091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath 2012091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath ALOGV("mark_boot_complete : %s", boot_marker_path); 2013091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath if (unlink(boot_marker_path) != 0) { 2014091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath ALOGE("Unable to unlink boot marker at %s, error=%s", boot_marker_path, 2015091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath strerror(errno)); 2016091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath return -1; 2017091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath } 2018091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath 2019091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath return 0; 2020091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath} 2021091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath 202294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodvoid mkinnerdirs(char* path, int basepos, mode_t mode, int uid, int gid, 202394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood struct stat* statbuf) 202494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 202594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood while (path[basepos] != 0) { 202694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (path[basepos] == '/') { 202794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood path[basepos] = 0; 202894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (lstat(path, statbuf) < 0) { 202994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGV("Making directory: %s\n", path); 203094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (mkdir(path, mode) == 0) { 203194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood chown(path, uid, gid); 203294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else { 203394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGW("Unable to make directory %s: %s\n", path, strerror(errno)); 203494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 203594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 203694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood path[basepos] = '/'; 203794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood basepos++; 203894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 203994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood basepos++; 204094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 204194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 204294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 2043c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkeyint linklib(const char* uuid, const char* pkgname, const char* asecLibDir, int userId) 204494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 204594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood struct stat s, libStat; 204694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int rc = 0; 204794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 20482f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey std::string _pkgdir(create_data_user_ce_package_path(uuid, userId, pkgname)); 2049c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey std::string _libsymlink(_pkgdir + PKG_LIB_POSTFIX); 2050c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey 2051c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey const char* pkgdir = _pkgdir.c_str(); 2052c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey const char* libsymlink = _libsymlink.c_str(); 205394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 205494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (stat(pkgdir, &s) < 0) return -1; 205594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 205694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (chown(pkgdir, AID_INSTALL, AID_INSTALL) < 0) { 205794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("failed to chown '%s': %s\n", pkgdir, strerror(errno)); 205894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return -1; 205994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 206094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 206194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (chmod(pkgdir, 0700) < 0) { 206294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("linklib() 1: failed to chmod '%s': %s\n", pkgdir, strerror(errno)); 206394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood rc = -1; 206494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto out; 206594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 206694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 206794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (lstat(libsymlink, &libStat) < 0) { 206894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (errno != ENOENT) { 206994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("couldn't stat lib dir: %s\n", strerror(errno)); 207094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood rc = -1; 207194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto out; 207294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 207394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else { 207494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (S_ISDIR(libStat.st_mode)) { 20753aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath if (delete_dir_contents(libsymlink, 1, NULL) < 0) { 207694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood rc = -1; 207794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto out; 207894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 207994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else if (S_ISLNK(libStat.st_mode)) { 208094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (unlink(libsymlink) < 0) { 208194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("couldn't unlink lib dir: %s\n", strerror(errno)); 208294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood rc = -1; 208394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto out; 208494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 208594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 208694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 208794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 208894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (symlink(asecLibDir, libsymlink) < 0) { 208994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("couldn't symlink directory '%s' -> '%s': %s\n", libsymlink, asecLibDir, 209094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood strerror(errno)); 209194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood rc = -errno; 209294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto out; 209394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 209494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 209594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodout: 209694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (chmod(pkgdir, s.st_mode) < 0) { 209794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("linklib() 2: failed to chmod '%s': %s\n", pkgdir, strerror(errno)); 209894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood rc = -errno; 209994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 210094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 210194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (chown(pkgdir, s.st_uid, s.st_gid) < 0) { 210294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("failed to chown '%s' : %s\n", pkgdir, strerror(errno)); 210394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return -errno; 210494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 210594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 210694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return rc; 210794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 210863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 210963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstadstatic void run_idmap(const char *target_apk, const char *overlay_apk, int idmap_fd) 211063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad{ 211163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad static const char *IDMAP_BIN = "/system/bin/idmap"; 211263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad static const size_t MAX_INT_LEN = 32; 211363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad char idmap_str[MAX_INT_LEN]; 211463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 211563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad snprintf(idmap_str, sizeof(idmap_str), "%d", idmap_fd); 211663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 211763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad execl(IDMAP_BIN, IDMAP_BIN, "--fd", target_apk, overlay_apk, idmap_str, (char*)NULL); 211863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("execl(%s) failed: %s\n", IDMAP_BIN, strerror(errno)); 211963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad} 212063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 212163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad// Transform string /a/b/c.apk to (prefix)/a@b@c.apk@(suffix) 212263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad// eg /a/b/c.apk to /data/resource-cache/a@b@c.apk@idmap 212363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstadstatic int flatten_path(const char *prefix, const char *suffix, 212463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad const char *overlay_path, char *idmap_path, size_t N) 212563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad{ 212663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (overlay_path == NULL || idmap_path == NULL) { 212763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad return -1; 212863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 212963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad const size_t len_overlay_path = strlen(overlay_path); 213063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad // will access overlay_path + 1 further below; requires absolute path 213163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (len_overlay_path < 2 || *overlay_path != '/') { 213263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad return -1; 213363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 213463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad const size_t len_idmap_root = strlen(prefix); 213563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad const size_t len_suffix = strlen(suffix); 213663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (SIZE_MAX - len_idmap_root < len_overlay_path || 213763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad SIZE_MAX - (len_idmap_root + len_overlay_path) < len_suffix) { 213863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad // additions below would cause overflow 213963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad return -1; 214063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 214163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (N < len_idmap_root + len_overlay_path + len_suffix) { 214263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad return -1; 214363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 214463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad memset(idmap_path, 0, N); 214563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad snprintf(idmap_path, N, "%s%s%s", prefix, overlay_path + 1, suffix); 214663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad char *ch = idmap_path + len_idmap_root; 214763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad while (*ch != '\0') { 214863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (*ch == '/') { 214963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad *ch = '@'; 215063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 215163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ++ch; 215263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 215363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad return 0; 215463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad} 215563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 215663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstadint idmap(const char *target_apk, const char *overlay_apk, uid_t uid) 215763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad{ 215863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGV("idmap target_apk=%s overlay_apk=%s uid=%d\n", target_apk, overlay_apk, uid); 215963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 216063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad int idmap_fd = -1; 216163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad char idmap_path[PATH_MAX]; 216263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 216363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (flatten_path(IDMAP_PREFIX, IDMAP_SUFFIX, overlay_apk, 216463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad idmap_path, sizeof(idmap_path)) == -1) { 216563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("idmap cannot generate idmap path for overlay %s\n", overlay_apk); 216663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad goto fail; 216763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 216863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 216963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad unlink(idmap_path); 217063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad idmap_fd = open(idmap_path, O_RDWR | O_CREAT | O_EXCL, 0644); 217163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (idmap_fd < 0) { 217263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("idmap cannot open '%s' for output: %s\n", idmap_path, strerror(errno)); 217363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad goto fail; 217463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 217563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (fchown(idmap_fd, AID_SYSTEM, uid) < 0) { 217663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("idmap cannot chown '%s'\n", idmap_path); 217763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad goto fail; 217863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 217963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (fchmod(idmap_fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0) { 218063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("idmap cannot chmod '%s'\n", idmap_path); 218163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad goto fail; 218263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 218363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 218463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad pid_t pid; 218563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad pid = fork(); 218663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (pid == 0) { 218763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad /* child -- drop privileges before continuing */ 218863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (setgid(uid) != 0) { 218963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("setgid(%d) failed during idmap\n", uid); 219063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad exit(1); 219163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 219263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (setuid(uid) != 0) { 219363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("setuid(%d) failed during idmap\n", uid); 219463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad exit(1); 219563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 219663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (flock(idmap_fd, LOCK_EX | LOCK_NB) != 0) { 219763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("flock(%s) failed during idmap: %s\n", idmap_path, strerror(errno)); 219863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad exit(1); 219963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 220063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 220163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad run_idmap(target_apk, overlay_apk, idmap_fd); 220263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad exit(1); /* only if exec call to idmap failed */ 220363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } else { 220463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad int status = wait_child(pid); 220563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (status != 0) { 220663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("idmap failed, status=0x%04x\n", status); 220763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad goto fail; 220863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 220963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 221063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 221163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad close(idmap_fd); 221263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad return 0; 221363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstadfail: 221463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (idmap_fd >= 0) { 221563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad close(idmap_fd); 221663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad unlink(idmap_path); 221763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 221863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad return -1; 221963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad} 2220e9887e46cea4a095e4219927eadbe4c57bb1a5eeRobert Craig 2221c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkeyint restorecon_app_data(const char* uuid, const char* pkgName, userid_t userid, int flags, 2222c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey appid_t appid, const char* seinfo) { 2223ebf728fd43ab5c7d11a1f9e5fdc775d6740fae0aJeff Sharkey int res = 0; 2224e9887e46cea4a095e4219927eadbe4c57bb1a5eeRobert Craig 2225da30dc7336f03ca629fe173db1425fdce989119cRobert Craig // SELINUX_ANDROID_RESTORECON_DATADATA flag is set by libselinux. Not needed here. 2226c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey unsigned int seflags = SELINUX_ANDROID_RESTORECON_RECURSE; 2227e9887e46cea4a095e4219927eadbe4c57bb1a5eeRobert Craig 2228da30dc7336f03ca629fe173db1425fdce989119cRobert Craig if (!pkgName || !seinfo) { 2229da30dc7336f03ca629fe173db1425fdce989119cRobert Craig ALOGE("Package name or seinfo tag is null when trying to restorecon."); 2230da30dc7336f03ca629fe173db1425fdce989119cRobert Craig return -1; 2231da30dc7336f03ca629fe173db1425fdce989119cRobert Craig } 2232e9887e46cea4a095e4219927eadbe4c57bb1a5eeRobert Craig 2233c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey uid_t uid = multiuser_get_uid(userid, appid); 2234aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (flags & FLAG_STORAGE_CE) { 22352f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey auto path = create_data_user_ce_package_path(uuid, userid, pkgName); 2236c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey if (selinux_android_restorecon_pkgdir(path.c_str(), seinfo, uid, seflags) < 0) { 2237c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey PLOG(ERROR) << "restorecon failed for " << path; 2238ebf728fd43ab5c7d11a1f9e5fdc775d6740fae0aJeff Sharkey res = -1; 2239da30dc7336f03ca629fe173db1425fdce989119cRobert Craig } 2240c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey } 2241aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (flags & FLAG_STORAGE_DE) { 2242c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey auto path = create_data_user_de_package_path(uuid, userid, pkgName); 2243c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey if (selinux_android_restorecon_pkgdir(path.c_str(), seinfo, uid, seflags) < 0) { 2244c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey PLOG(ERROR) << "restorecon failed for " << path; 2245ea0e4b19d942f62ea21ca4e70f939f3d4aa14b37Jeff Sharkey // TODO: include result once 25796509 is fixed 2246da30dc7336f03ca629fe173db1425fdce989119cRobert Craig } 2247da30dc7336f03ca629fe173db1425fdce989119cRobert Craig } 2248da30dc7336f03ca629fe173db1425fdce989119cRobert Craig 2249ebf728fd43ab5c7d11a1f9e5fdc775d6740fae0aJeff Sharkey return res; 2250e9887e46cea4a095e4219927eadbe4c57bb1a5eeRobert Craig} 22513aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath 225288ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolovint create_oat_dir(const char* oat_dir, const char* instruction_set) 225388ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov{ 225488ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov char oat_instr_dir[PKG_PATH_MAX]; 225588ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov 225688ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov if (validate_apk_path(oat_dir)) { 225788ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov ALOGE("invalid apk path '%s' (bad prefix)\n", oat_dir); 225888ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov return -1; 225988ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov } 22608eed7e6a9bb527af7380ec13b390c6e2955eba6aFyodor Kupolov if (fs_prepare_dir(oat_dir, S_IRWXU | S_IRWXG | S_IXOTH, AID_SYSTEM, AID_INSTALL)) { 226188ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov return -1; 226288ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov } 226388ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov if (selinux_android_restorecon(oat_dir, 0)) { 226488ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov ALOGE("cannot restorecon dir '%s': %s\n", oat_dir, strerror(errno)); 226588ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov return -1; 226688ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov } 226788ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov snprintf(oat_instr_dir, PKG_PATH_MAX, "%s/%s", oat_dir, instruction_set); 22688eed7e6a9bb527af7380ec13b390c6e2955eba6aFyodor Kupolov if (fs_prepare_dir(oat_instr_dir, S_IRWXU | S_IRWXG | S_IXOTH, AID_SYSTEM, AID_INSTALL)) { 226988ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov return -1; 227088ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov } 227188ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov return 0; 227288ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov} 227388ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov 227488ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolovint rm_package_dir(const char* apk_path) 227588ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov{ 227688ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov if (validate_apk_path(apk_path)) { 227788ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov ALOGE("invalid apk path '%s' (bad prefix)\n", apk_path); 227888ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov return -1; 227988ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov } 228088ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov return delete_dir_contents(apk_path, 1 /* also_delete_dir */ , NULL /* exclusion_predicate */); 228188ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov} 228288ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov 2283d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamathint link_file(const char* relative_path, const char* from_base, const char* to_base) { 2284d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath char from_path[PKG_PATH_MAX]; 2285d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath char to_path[PKG_PATH_MAX]; 2286d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath snprintf(from_path, PKG_PATH_MAX, "%s/%s", from_base, relative_path); 2287d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath snprintf(to_path, PKG_PATH_MAX, "%s/%s", to_base, relative_path); 2288d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath 2289d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath if (validate_apk_path_subdirs(from_path)) { 2290d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath ALOGE("invalid app data sub-path '%s' (bad prefix)\n", from_path); 2291d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath return -1; 2292d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath } 2293d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath 2294d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath if (validate_apk_path_subdirs(to_path)) { 2295d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath ALOGE("invalid app data sub-path '%s' (bad prefix)\n", to_path); 2296d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath return -1; 2297d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath } 2298d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath 2299d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath const int ret = link(from_path, to_path); 2300d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath if (ret < 0) { 2301d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath ALOGE("link(%s, %s) failed : %s", from_path, to_path, strerror(errno)); 2302d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath return -1; 2303d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath } 2304d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath 2305d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath return 0; 2306d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath} 2307d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath 2308e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe// Helper for move_ab, so that we can have common failure-case cleanup. 2309e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampestatic bool unlink_and_rename(const char* from, const char* to) { 2310e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe // Check whether "from" exists, and if so whether it's regular. If it is, unlink. Otherwise, 2311e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe // return a failure. 2312e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe struct stat s; 2313e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe if (stat(to, &s) == 0) { 2314e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe if (!S_ISREG(s.st_mode)) { 2315e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe LOG(ERROR) << from << " is not a regular file to replace for A/B."; 2316e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe return false; 2317e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe } 2318e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe if (unlink(to) != 0) { 2319e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe LOG(ERROR) << "Could not unlink " << to << " to move A/B."; 2320e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe return false; 2321e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe } 2322e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe } else { 2323e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe // This may be a permission problem. We could investigate the error code, but we'll just 2324e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe // let the rename failure do the work for us. 2325e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe } 2326e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe 2327e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe // Try to rename "to" to "from." 2328e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe if (rename(from, to) != 0) { 2329e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe PLOG(ERROR) << "Could not rename " << from << " to " << to; 2330e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe return false; 2331e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe } 2332e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe return true; 2333e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe} 2334e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe 2335ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe// Move/rename a B artifact (from) to an A artifact (to). 2336ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampestatic bool move_ab_path(const std::string& b_path, const std::string& a_path) { 2337ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe // Check whether B exists. 2338ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe { 2339ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe struct stat s; 2340ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe if (stat(b_path.c_str(), &s) != 0) { 2341ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe // Silently ignore for now. The service calling this isn't smart enough to understand 2342ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe // lack of artifacts at the moment. 2343ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe return false; 2344ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe } 2345ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe if (!S_ISREG(s.st_mode)) { 2346ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe LOG(ERROR) << "A/B artifact " << b_path << " is not a regular file."; 2347ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe // Try to unlink, but swallow errors. 2348ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe unlink(b_path.c_str()); 2349ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe return false; 2350ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe } 2351ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe } 2352ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe 2353ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe // Rename B to A. 2354ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe if (!unlink_and_rename(b_path.c_str(), a_path.c_str())) { 2355ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe // Delete the b_path so we don't try again (or fail earlier). 2356ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe if (unlink(b_path.c_str()) != 0) { 2357ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe PLOG(ERROR) << "Could not unlink " << b_path; 2358ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe } 2359ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe 2360ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe return false; 2361ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe } 2362ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe 2363ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe return true; 2364ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe} 2365ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe 2366e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampeint move_ab(const char* apk_path, const char* instruction_set, const char* oat_dir) { 2367e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe if (apk_path == nullptr || instruction_set == nullptr || oat_dir == nullptr) { 2368e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe LOG(ERROR) << "Cannot move_ab with null input"; 2369e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe return -1; 2370e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe } 2371d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe 2372d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe // Get the current slot suffix. No suffix, no A/B. 2373d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe std::string slot_suffix; 2374d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe { 2375d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe char buf[kPropertyValueMax]; 2376d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe if (get_property("ro.boot.slot_suffix", buf, nullptr) <= 0) { 2377d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe return -1; 2378d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe } 2379d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe slot_suffix = buf; 2380d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe 2381fd12edaeab839f3f1f087cc75bd18b4d8af5b192Andreas Gampe if (!ValidateTargetSlotSuffix(slot_suffix)) { 2382d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe LOG(ERROR) << "Target slot suffix not legal: " << slot_suffix; 2383d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe return -1; 2384d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe } 2385d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe } 2386d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe 2387d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe // Validate other inputs. 2388e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe if (validate_apk_path(apk_path) != 0) { 2389e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe LOG(ERROR) << "invalid apk_path " << apk_path; 2390e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe return -1; 2391e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe } 2392e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe if (validate_apk_path(oat_dir) != 0) { 2393e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe LOG(ERROR) << "invalid oat_dir " << oat_dir; 2394e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe return -1; 2395e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe } 2396e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe 2397e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe char a_path[PKG_PATH_MAX]; 2398e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe if (!calculate_oat_file_path(a_path, oat_dir, apk_path, instruction_set)) { 2399e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe return -1; 2400e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe } 2401fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil const std::string a_vdex_path = create_vdex_filename(a_path); 2402ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe const std::string a_image_path = create_image_filename(a_path); 2403e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe 2404d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe // B path = A path + slot suffix. 2405d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe const std::string b_path = StringPrintf("%s.%s", a_path, slot_suffix.c_str()); 2406fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil const std::string b_vdex_path = StringPrintf("%s.%s", a_vdex_path.c_str(), slot_suffix.c_str()); 2407d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe const std::string b_image_path = StringPrintf("%s.%s", 2408d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe a_image_path.c_str(), 2409d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe slot_suffix.c_str()); 2410e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe 2411fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil bool success = true; 2412fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil if (move_ab_path(b_path, a_path)) { 2413fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil if (move_ab_path(b_vdex_path, a_vdex_path)) { 2414fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil // Note: we can live without an app image. As such, ignore failure to move the image file. 2415fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil // If we decide to require the app image, or the app image being moved correctly, 2416fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil // then change accordingly. 2417fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil constexpr bool kIgnoreAppImageFailure = true; 2418fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil 2419fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil if (!a_image_path.empty()) { 2420fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil if (!move_ab_path(b_image_path, a_image_path)) { 2421fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil if (!kIgnoreAppImageFailure) { 2422fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil success = false; 2423fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil } 2424fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil } 2425fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil } 2426fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil } else { 2427fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil // Cleanup: delete B image, ignore errors. 2428fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil unlink(b_image_path.c_str()); 2429fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil success = false; 2430e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe } 2431ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe } else { 2432ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe // Cleanup: delete B image, ignore errors. 2433fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil unlink(b_vdex_path.c_str()); 2434ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe unlink(b_image_path.c_str()); 2435ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe success = false; 2436e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe } 2437e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe 2438ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe return success ? 0 : -1; 2439e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe} 2440e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe 2441b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampebool delete_odex(const char *apk_path, const char *instruction_set, const char *oat_dir) { 2442b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampe // Delete the oat/odex file. 2443b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampe char out_path[PKG_PATH_MAX]; 2444b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampe if (!create_oat_out_path(apk_path, instruction_set, oat_dir, out_path)) { 2445b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampe return false; 2446b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampe } 2447b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampe 2448b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampe // In case of a permission failure report the issue. Otherwise just print a warning. 2449b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampe auto unlink_and_check = [](const char* path) -> bool { 2450b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampe int result = unlink(path); 2451b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampe if (result != 0) { 2452b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampe if (errno == EACCES || errno == EPERM) { 2453b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampe PLOG(ERROR) << "Could not unlink " << path; 2454b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampe return false; 2455b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampe } 2456b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampe PLOG(WARNING) << "Could not unlink " << path; 2457b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampe } 2458b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampe return true; 2459b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampe }; 2460b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampe 2461b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampe // Delete the oat/odex file. 2462b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampe bool return_value_oat = unlink_and_check(out_path); 2463b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampe 2464b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampe // Derive and delete the app image. 2465b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampe bool return_value_art = unlink_and_check(create_image_filename(out_path).c_str()); 2466b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampe 2467b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampe // Report success. 2468b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampe return return_value_oat && return_value_art; 2469b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampe} 2470b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampe 247102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe} // namespace installd 247202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe} // namespace android 2473