InstalldNativeService.cpp revision 6c2c056193010cf93b4264810d462c120ce801c8
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
506c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey#include "dexopt.h"
516c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey
5202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#ifndef LOG_TAG
5302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#define LOG_TAG "installd"
5402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#endif
5541ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey
566fb5a0179d4de9c85351fd016277949804c809edAndreas Gampeusing android::base::EndsWith;
5741ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkeyusing android::base::StringPrintf;
5894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
5902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampenamespace android {
6002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampenamespace installd {
6194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
62cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkeystatic constexpr const char* kCpPath = "/system/bin/cp";
63cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkeystatic constexpr const char* kXattrDefault = "user.default";
64e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey
65d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampestatic constexpr const char* PKG_LIB_POSTFIX = "/lib";
66d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampestatic constexpr const char* CACHE_DIR_POSTFIX = "/cache";
67d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampestatic constexpr const char* CODE_CACHE_DIR_POSTFIX = "/code_cache";
68d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe
69d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampestatic constexpr const char* IDMAP_PREFIX = "/data/resource-cache/";
70d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampestatic constexpr const char* IDMAP_SUFFIX = "@idmap";
71d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe
72d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe// NOTE: keep in sync with StorageManager
73d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampestatic constexpr int FLAG_STORAGE_DE = 1 << 0;
74d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampestatic constexpr int FLAG_STORAGE_CE = 1 << 1;
75d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe
76d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe// NOTE: keep in sync with Installer
77d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampestatic constexpr int FLAG_CLEAR_CACHE_ONLY = 1 << 8;
78d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampestatic constexpr int FLAG_CLEAR_CODE_CACHE_ONLY = 1 << 9;
79d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe
80d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe/* dexopt needed flags matching those in dalvik.system.DexFile */
81920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffraystatic constexpr int DEX2OAT_FROM_SCRATCH        = 1;
82920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffraystatic constexpr int DEX2OAT_FOR_BOOT_IMAGE      = 2;
83920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffraystatic constexpr int DEX2OAT_FOR_FILTER          = 3;
84920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffraystatic constexpr int DEX2OAT_FOR_RELOCATION      = 4;
85920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffraystatic constexpr int PATCHOAT_FOR_RELOCATION     = 5;
86d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe
87eecb2d205366351af42fd0cd9e1a95de3980764eJanis Danisevskis#define MIN_RESTRICTED_HOME_SDK_VERSION 24 // > M
88eecb2d205366351af42fd0cd9e1a95de3980764eJanis Danisevskis
896a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravletypedef int fd_t;
906a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle
910274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeynamespace {
920274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey
930274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeyconstexpr const char* kDump = "android.permission.DUMP";
940274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey
950274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeybinder::Status checkPermission(const char* permission) {
960274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey    pid_t pid;
970274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey    uid_t uid;
980274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey
990274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey    if (checkCallingPermission(String16(permission), reinterpret_cast<int32_t*>(&pid),
1000274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey            reinterpret_cast<int32_t*>(&uid))) {
1010274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey        return binder::Status::ok();
1020274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey    } else {
1030274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey        auto err = StringPrintf("UID %d / PID %d lacks permission %s", uid, pid, permission);
1040274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey        return binder::Status::fromExceptionCode(binder::Status::EX_SECURITY, String8(err.c_str()));
1050274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey    }
1060274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey}
1070274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey
1080274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeybinder::Status checkUid(uid_t expectedUid) {
1090274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey    uid_t uid = IPCThreadState::self()->getCallingUid();
1100274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey    if (uid == expectedUid || uid == AID_ROOT) {
1110274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey        return binder::Status::ok();
1120274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey    } else {
1130274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey        auto err = StringPrintf("UID %d is not expected UID %d", uid, expectedUid);
1140274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey        return binder::Status::fromExceptionCode(binder::Status::EX_SECURITY, String8(err.c_str()));
1150274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey    }
1160274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey}
1170274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey
1180274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey#define ENFORCE_UID(uid) {                                  \
1190274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey    binder::Status status = checkUid((uid));                \
1200274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey    if (!status.isOk()) {                                   \
1210274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey        return status;                                      \
1220274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey    }                                                       \
1230274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey}
1240274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey
1250274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey}  // namespace
1260274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey
1270274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeystatus_t InstalldNativeService::start() {
1280274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey    IPCThreadState::self()->disableBackgroundScheduling(true);
1290274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey    status_t ret = BinderService<InstalldNativeService>::publish();
1300274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey    if (ret != android::OK) {
1310274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey        return ret;
1320274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey    }
1330274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey    sp<ProcessState> ps(ProcessState::self());
1340274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey    ps->startThreadPool();
1350274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey    ps->giveThreadPoolName();
1360274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey    return android::OK;
1370274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey}
1380274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey
1390274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeystatus_t InstalldNativeService::dump(int fd, const Vector<String16> & /* args */) {
1400274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey    const binder::Status dump_permission = checkPermission(kDump);
1410274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey    if (!dump_permission.isOk()) {
1420274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey        const String8 msg(dump_permission.toString8());
1430274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey        write(fd, msg.string(), msg.size());
1440274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey        return PERMISSION_DENIED;
1450274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey    }
1460274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey
1470274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey    std::string msg = "installd is happy\n";
1480274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey    write(fd, msg.c_str(), strlen(msg.c_str()));
1490274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey    return NO_ERROR;
1500274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey}
1510274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey
1526a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic bool property_get_bool(const char* property_name, bool default_value = false) {
1536a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    char tmp_property_value[kPropertyValueMax];
1546a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    bool have_property = get_property(property_name, tmp_property_value, nullptr) > 0;
1556a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    if (!have_property) {
1566a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        return default_value;
1576a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    }
1586a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    return strcmp(tmp_property_value, "true") == 0;
1596a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle}
1606a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle
161bc56e7dcf07e633867e1525435373a1231216b22Calin Juravle// Keep profile paths in sync with ActivityThread.
162bc56e7dcf07e633867e1525435373a1231216b22Calin Juravleconstexpr const char* PRIMARY_PROFILE_NAME = "primary.prof";
163bc56e7dcf07e633867e1525435373a1231216b22Calin Juravlestatic std::string create_primary_profile(const std::string& profile_dir) {
164bc56e7dcf07e633867e1525435373a1231216b22Calin Juravle    return StringPrintf("%s/%s", profile_dir.c_str(), PRIMARY_PROFILE_NAME);
165bc56e7dcf07e633867e1525435373a1231216b22Calin Juravle}
166bc56e7dcf07e633867e1525435373a1231216b22Calin Juravle
1677db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey/**
1687db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey * Perform restorecon of the given path, but only perform recursive restorecon
1697db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey * if the label of that top-level file actually changed.  This can save us
1707db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey * significant time by avoiding no-op traversals of large filesystem trees.
1717db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey */
1720274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeystatic int restorecon_app_data_lazy(const std::string& path, const std::string& seInfo, uid_t uid) {
1737db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey    int res = 0;
1747db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey    char* before = nullptr;
1757db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey    char* after = nullptr;
1767db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey
1777db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey    // Note that SELINUX_ANDROID_RESTORECON_DATADATA flag is set by
1787db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey    // libselinux. Not needed here.
1797db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey
18095d9ac642f082870fd4d2419f534171a9b82bbcdJeff Sharkey    if (lgetfilecon(path.c_str(), &before) < 0) {
1817db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey        PLOG(ERROR) << "Failed before getfilecon for " << path;
1827db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey        goto fail;
1837db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey    }
1840274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey    if (selinux_android_restorecon_pkgdir(path.c_str(), seInfo.c_str(), uid, 0) < 0) {
1857db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey        PLOG(ERROR) << "Failed top-level restorecon for " << path;
1867db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey        goto fail;
1877db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey    }
18895d9ac642f082870fd4d2419f534171a9b82bbcdJeff Sharkey    if (lgetfilecon(path.c_str(), &after) < 0) {
1897db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey        PLOG(ERROR) << "Failed after getfilecon for " << path;
1907db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey        goto fail;
1917db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey    }
1927db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey
1937db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey    // If the initial top-level restorecon above changed the label, then go
1947db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey    // back and restorecon everything recursively
1957db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey    if (strcmp(before, after)) {
1967db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey        LOG(DEBUG) << "Detected label change from " << before << " to " << after << " at " << path
1977db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey                << "; running recursive restorecon";
1980274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey        if (selinux_android_restorecon_pkgdir(path.c_str(), seInfo.c_str(), uid,
1997db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey                SELINUX_ANDROID_RESTORECON_RECURSE) < 0) {
2007db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey            PLOG(ERROR) << "Failed recursive restorecon for " << path;
2017db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey            goto fail;
2027db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey        }
2037db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey    }
2047db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey
2057db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey    goto done;
2067db6041d156b750689b32e340d8a0753061262a3Jeff Sharkeyfail:
2077db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey    res = -1;
2087db6041d156b750689b32e340d8a0753061262a3Jeff Sharkeydone:
2097db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey    free(before);
2107db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey    free(after);
2117db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey    return res;
2127db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey}
2137db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey
2140274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeystatic int restorecon_app_data_lazy(const std::string& parent, const char* name,
2150274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey        const std::string& seInfo, uid_t uid) {
2160274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey    return restorecon_app_data_lazy(StringPrintf("%s/%s", parent.c_str(), name), seInfo, uid);
21795d9ac642f082870fd4d2419f534171a9b82bbcdJeff Sharkey}
21895d9ac642f082870fd4d2419f534171a9b82bbcdJeff Sharkey
2197db6041d156b750689b32e340d8a0753061262a3Jeff Sharkeystatic int prepare_app_dir(const std::string& path, mode_t target_mode, uid_t uid) {
2209a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey    if (fs_prepare_dir_strict(path.c_str(), target_mode, uid, uid) != 0) {
2219a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey        PLOG(ERROR) << "Failed to prepare " << path;
2229a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey        return -1;
2239a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey    }
2249a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey    return 0;
2259a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey}
2269a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey
2279a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkeystatic int prepare_app_dir(const std::string& parent, const char* name, mode_t target_mode,
2287db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey        uid_t uid) {
2297db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey    return prepare_app_dir(StringPrintf("%s/%s", parent.c_str(), name), target_mode, uid);
2309a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey}
2319a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey
2320274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeybinder::Status InstalldNativeService::createAppData(const std::unique_ptr<std::string>& uuid,
2330274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey        const std::string& packageName, int32_t userId, int32_t flags, int32_t appId,
2340274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey        const std::string& seInfo, int32_t targetSdkVersion) {
2350274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey    ENFORCE_UID(AID_SYSTEM);
2360274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey
2370274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey    const char* uuid_ = uuid ? uuid->c_str() : nullptr;
2380274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey    const char* pkgname = packageName.c_str();
2390274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey
2400274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey    uid_t uid = multiuser_get_uid(userId, appId);
2410274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey    mode_t target_mode = targetSdkVersion >= MIN_RESTRICTED_HOME_SDK_VERSION ? 0700 : 0751;
242aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey    if (flags & FLAG_STORAGE_CE) {
2430274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey        auto path = create_data_user_ce_package_path(uuid_, userId, pkgname);
2447db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey        if (prepare_app_dir(path, target_mode, uid) ||
2457db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey                prepare_app_dir(path, "cache", 0771, uid) ||
2467db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey                prepare_app_dir(path, "code_cache", 0771, uid)) {
2470274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey            return binder::Status::fromServiceSpecificError(-1);
2487db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey        }
2497db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey
2507db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey        // Consider restorecon over contents if label changed
2510274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey        if (restorecon_app_data_lazy(path, seInfo, uid) ||
2520274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey                restorecon_app_data_lazy(path, "cache", seInfo, uid) ||
2530274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey                restorecon_app_data_lazy(path, "code_cache", seInfo, uid)) {
2540274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey            return binder::Status::fromServiceSpecificError(-1);
255c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey        }
2569a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey
2579a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey        // Remember inode numbers of cache directories so that we can clear
2589a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey        // contents while CE storage is locked
2599a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey        if (write_path_inode(path, "cache", kXattrInodeCache) ||
2609a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey                write_path_inode(path, "code_cache", kXattrInodeCodeCache)) {
2610274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey            return binder::Status::fromServiceSpecificError(-1);
262c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey        }
26394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
264aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey    if (flags & FLAG_STORAGE_DE) {
2650274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey        auto path = create_data_user_de_package_path(uuid_, userId, pkgname);
2667db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey        if (prepare_app_dir(path, target_mode, uid)) {
267a03c747ff6e1afb5c1dc86560705bd979cd5c49fJeff Sharkey            // TODO: include result once 25796509 is fixed
2680274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey            return binder::Status::ok();
269c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey        }
2706a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle
2717db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey        // Consider restorecon over contents if label changed
2720274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey        if (restorecon_app_data_lazy(path, seInfo, uid)) {
2730274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey            return binder::Status::fromServiceSpecificError(-1);
2747db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey        }
2757db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey
2766a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        if (property_get_bool("dalvik.vm.usejitprofiles")) {
2770274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey            const std::string profile_path = create_data_user_profile_package_path(userId, pkgname);
2786a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle            // read-write-execute only for the app user.
2796a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle            if (fs_prepare_dir_strict(profile_path.c_str(), 0700, uid, uid) != 0) {
2806a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle                PLOG(ERROR) << "Failed to prepare " << profile_path;
2810274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey                return binder::Status::fromServiceSpecificError(-1);
2826a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle            }
283bc56e7dcf07e633867e1525435373a1231216b22Calin Juravle            std::string profile_file = create_primary_profile(profile_path);
284bc56e7dcf07e633867e1525435373a1231216b22Calin Juravle            // read-write only for the app user.
285bc56e7dcf07e633867e1525435373a1231216b22Calin Juravle            if (fs_prepare_file_strict(profile_file.c_str(), 0600, uid, uid) != 0) {
286bc56e7dcf07e633867e1525435373a1231216b22Calin Juravle                PLOG(ERROR) << "Failed to prepare " << profile_path;
2870274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey                return binder::Status::fromServiceSpecificError(-1);
288bc56e7dcf07e633867e1525435373a1231216b22Calin Juravle            }
2896a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle            const std::string ref_profile_path = create_data_ref_profile_package_path(pkgname);
2906a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle            // dex2oat/profman runs under the shared app gid and it needs to read/write reference
2916a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle            // profiles.
2926a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle            appid_t shared_app_gid = multiuser_get_shared_app_gid(uid);
2936a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle            if (fs_prepare_dir_strict(
2946a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle                    ref_profile_path.c_str(), 0700, shared_app_gid, shared_app_gid) != 0) {
2956a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle                PLOG(ERROR) << "Failed to prepare " << ref_profile_path;
2960274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey                return binder::Status::fromServiceSpecificError(-1);
2976a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle            }
2986a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        }
29994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
3000274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey    return binder::Status::ok();
30194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
30294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
303c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkeybinder::Status InstalldNativeService::migrateAppData(const std::unique_ptr<std::string>& uuid,
304c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey        const std::string& packageName, int32_t userId, int32_t flags) {
305c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey    ENFORCE_UID(AID_SYSTEM);
306c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey    const char* uuid_ = uuid ? uuid->c_str() : nullptr;
307c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey    const char* pkgname = packageName.c_str();
308c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey
309cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey    // This method only exists to upgrade system apps that have requested
310cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey    // forceDeviceEncrypted, so their default storage always lives in a
311cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey    // consistent location.  This only works on non-FBE devices, since we
312cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey    // never want to risk exposing data on a device with real CE/DE storage.
313cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey
314c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey    auto ce_path = create_data_user_ce_package_path(uuid_, userId, pkgname);
315c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey    auto de_path = create_data_user_de_package_path(uuid_, userId, pkgname);
316cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey
317cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey    // If neither directory is marked as default, assume CE is default
318cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey    if (getxattr(ce_path.c_str(), kXattrDefault, nullptr, 0) == -1
319cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey            && getxattr(de_path.c_str(), kXattrDefault, nullptr, 0) == -1) {
320cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey        if (setxattr(ce_path.c_str(), kXattrDefault, nullptr, 0, 0) != 0) {
321cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey            PLOG(ERROR) << "Failed to mark default storage " << ce_path;
322c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey            return binder::Status::fromServiceSpecificError(-1);
323cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey        }
324cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey    }
325cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey
326cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey    // Migrate default data location if needed
327cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey    auto target = (flags & FLAG_STORAGE_DE) ? de_path : ce_path;
328cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey    auto source = (flags & FLAG_STORAGE_DE) ? ce_path : de_path;
329cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey
330cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey    if (getxattr(target.c_str(), kXattrDefault, nullptr, 0) == -1) {
331cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey        LOG(WARNING) << "Requested default storage " << target
332cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey                << " is not active; migrating from " << source;
333cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey        if (delete_dir_contents_and_dir(target) != 0) {
334cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey            PLOG(ERROR) << "Failed to delete";
335c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey            return binder::Status::fromServiceSpecificError(-1);
336cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey        }
337cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey        if (rename(source.c_str(), target.c_str()) != 0) {
338cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey            PLOG(ERROR) << "Failed to rename";
339c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey            return binder::Status::fromServiceSpecificError(-1);
340cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey        }
341cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey    }
342cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey
343c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey    return binder::Status::ok();
344cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey}
345cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey
346cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravlestatic bool clear_profile(const std::string& profile) {
34764b59cc1c89225813295d1b5d6bf4183615da204Roland Levillain    base::unique_fd ufd(open(profile.c_str(), O_WRONLY | O_NOFOLLOW | O_CLOEXEC));
34864b59cc1c89225813295d1b5d6bf4183615da204Roland Levillain    if (ufd.get() < 0) {
349278edac4b541e0a671d646a0e17bdbea411c8a08Calin Juravle        if (errno != ENOENT) {
350cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle            PLOG(WARNING) << "Could not open profile " << profile;
351d828d6839f6a01e3f0b2b8b2694139dfa801c557David Brazdil            return false;
352cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle        } else {
353cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle            // Nothing to clear. That's ok.
354cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle            return true;
355278edac4b541e0a671d646a0e17bdbea411c8a08Calin Juravle        }
3566a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    }
357cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle
35864b59cc1c89225813295d1b5d6bf4183615da204Roland Levillain    if (flock(ufd.get(), LOCK_EX | LOCK_NB) != 0) {
359cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle        if (errno != EWOULDBLOCK) {
360cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle            PLOG(WARNING) << "Error locking profile " << profile;
361cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle        }
362cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle        // This implies that the app owning this profile is running
363cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle        // (and has acquired the lock).
364cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle        //
365cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle        // If we can't acquire the lock bail out since clearing is useless anyway
366cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle        // (the app will write again to the profile).
367cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle        //
368cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle        // Note:
369cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle        // This does not impact the this is not an issue for the profiling correctness.
370cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle        // In case this is needed because of an app upgrade, profiles will still be
371cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle        // eventually cleared by the app itself due to checksum mismatch.
372cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle        // If this is needed because profman advised, then keeping the data around
373cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle        // until the next run is again not an issue.
374cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle        //
375cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle        // If the app attempts to acquire a lock while we've held one here,
376cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle        // it will simply skip the current write cycle.
377cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle        return false;
378cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle    }
379cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle
38064b59cc1c89225813295d1b5d6bf4183615da204Roland Levillain    bool truncated = ftruncate(ufd.get(), 0) == 0;
381cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle    if (!truncated) {
382cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle        PLOG(WARNING) << "Could not truncate " << profile;
383cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle    }
38464b59cc1c89225813295d1b5d6bf4183615da204Roland Levillain    if (flock(ufd.get(), LOCK_UN) != 0) {
385cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle        PLOG(WARNING) << "Error unlocking profile " << profile;
386cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle    }
387cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle    return truncated;
388cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle}
389cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle
390cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravlestatic bool clear_reference_profile(const char* pkgname) {
391cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle    std::string reference_profile_dir = create_data_ref_profile_package_path(pkgname);
392cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle    std::string reference_profile = create_primary_profile(reference_profile_dir);
393cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle    return clear_profile(reference_profile);
3946a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle}
3956a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle
396cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravlestatic bool clear_current_profile(const char* pkgname, userid_t user) {
397edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle    std::string profile_dir = create_data_user_profile_package_path(user, pkgname);
398edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle    std::string profile = create_primary_profile(profile_dir);
399cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle    return clear_profile(profile);
400edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle}
401edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle
402cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravlestatic bool clear_current_profiles(const char* pkgname) {
403d828d6839f6a01e3f0b2b8b2694139dfa801c557David Brazdil    bool success = true;
4046a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    std::vector<userid_t> users = get_known_users(/*volume_uuid*/ nullptr);
4056a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    for (auto user : users) {
406cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle        success &= clear_current_profile(pkgname, user);
4076a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    }
408d828d6839f6a01e3f0b2b8b2694139dfa801c557David Brazdil    return success;
4096a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle}
4106a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle
411475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::clearAppProfiles(const std::string& packageName) {
412475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    ENFORCE_UID(AID_SYSTEM);
413475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    const char* pkgname = packageName.c_str();
414d828d6839f6a01e3f0b2b8b2694139dfa801c557David Brazdil    bool success = true;
415cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle    success &= clear_reference_profile(pkgname);
416cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle    success &= clear_current_profiles(pkgname);
417475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    return success ? binder::Status::ok() : binder::Status::fromServiceSpecificError(-1);
4186a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle}
4196a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle
420c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkeybinder::Status InstalldNativeService::clearAppData(const std::unique_ptr<std::string>& uuid,
421c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey        const std::string& packageName, int32_t userId, int32_t flags, int64_t ceDataInode) {
422c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey    ENFORCE_UID(AID_SYSTEM);
423c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey    const char* uuid_ = uuid ? uuid->c_str() : nullptr;
424c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey    const char* pkgname = packageName.c_str();
425c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey
426ebf728fd43ab5c7d11a1f9e5fdc775d6740fae0aJeff Sharkey    int res = 0;
427aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey    if (flags & FLAG_STORAGE_CE) {
428c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey        auto path = create_data_user_ce_package_path(uuid_, userId, pkgname, ceDataInode);
4299a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey        if (flags & FLAG_CLEAR_CACHE_ONLY) {
4309a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey            path = read_path_inode(path, "cache", kXattrInodeCache);
4319a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey        } else if (flags & FLAG_CLEAR_CODE_CACHE_ONLY) {
4329a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey            path = read_path_inode(path, "code_cache", kXattrInodeCodeCache);
4339a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey        }
434c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey        if (access(path.c_str(), F_OK) == 0) {
435c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey            res |= delete_dir_contents(path);
436c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey        }
43794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
438aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey    if (flags & FLAG_STORAGE_DE) {
4399a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey        std::string suffix = "";
4409a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey        bool only_cache = false;
4419a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey        if (flags & FLAG_CLEAR_CACHE_ONLY) {
4429a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey            suffix = CACHE_DIR_POSTFIX;
4439a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey            only_cache = true;
4449a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey        } else if (flags & FLAG_CLEAR_CODE_CACHE_ONLY) {
4459a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey            suffix = CODE_CACHE_DIR_POSTFIX;
4469a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey            only_cache = true;
4479a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey        }
4489a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey
449c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey        auto path = create_data_user_de_package_path(uuid_, userId, pkgname) + suffix;
450c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey        if (access(path.c_str(), F_OK) == 0) {
451a03c747ff6e1afb5c1dc86560705bd979cd5c49fJeff Sharkey            // TODO: include result once 25796509 is fixed
452a03c747ff6e1afb5c1dc86560705bd979cd5c49fJeff Sharkey            delete_dir_contents(path);
453c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey        }
454edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle        if (!only_cache) {
455c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey            if (!clear_current_profile(pkgname, userId)) {
456edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle                res |= -1;
457edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle            }
458edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle        }
45994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
460c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey    return res ? binder::Status::fromServiceSpecificError(-1) : binder::Status::ok();
461c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey}
46294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
4637535e8e1995398befc866062d63c05d90eb70cccCalin Juravlestatic int destroy_app_reference_profile(const char *pkgname) {
4647535e8e1995398befc866062d63c05d90eb70cccCalin Juravle    return delete_dir_contents_and_dir(
4657535e8e1995398befc866062d63c05d90eb70cccCalin Juravle        create_data_ref_profile_package_path(pkgname),
4667535e8e1995398befc866062d63c05d90eb70cccCalin Juravle        /*ignore_if_missing*/ true);
4677535e8e1995398befc866062d63c05d90eb70cccCalin Juravle}
4687535e8e1995398befc866062d63c05d90eb70cccCalin Juravle
469edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravlestatic int destroy_app_current_profiles(const char *pkgname, userid_t userid) {
470b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravle    return delete_dir_contents_and_dir(
471b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravle        create_data_user_profile_package_path(userid, pkgname),
472b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravle        /*ignore_if_missing*/ true);
473edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle}
474edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle
475475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::destroyAppProfiles(const std::string& packageName) {
476475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    ENFORCE_UID(AID_SYSTEM);
477475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    const char* pkgname = packageName.c_str();
478edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle    int result = 0;
479edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle    std::vector<userid_t> users = get_known_users(/*volume_uuid*/ nullptr);
480edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle    for (auto user : users) {
481edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle        result |= destroy_app_current_profiles(pkgname, user);
482edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle    }
4837535e8e1995398befc866062d63c05d90eb70cccCalin Juravle    result |= destroy_app_reference_profile(pkgname);
484475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    return result ? binder::Status::fromServiceSpecificError(-1) : binder::Status::ok();
485caa6b809a5ed024a8f3f551c5c109a5ad50ab772Calin Juravle}
486caa6b809a5ed024a8f3f551c5c109a5ad50ab772Calin Juravle
487c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkeybinder::Status InstalldNativeService::destroyAppData(const std::unique_ptr<std::string>& uuid,
488c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey        const std::string& packageName, int32_t userId, int32_t flags, int64_t ceDataInode) {
489c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey    ENFORCE_UID(AID_SYSTEM);
490c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey    const char* uuid_ = uuid ? uuid->c_str() : nullptr;
491c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey    const char* pkgname = packageName.c_str();
492c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey
493c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey    int res = 0;
494aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey    if (flags & FLAG_STORAGE_CE) {
495c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey        res |= delete_dir_contents_and_dir(
496c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey                create_data_user_ce_package_path(uuid_, userId, pkgname, ceDataInode));
49794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
498aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey    if (flags & FLAG_STORAGE_DE) {
499caa6b809a5ed024a8f3f551c5c109a5ad50ab772Calin Juravle        res |= delete_dir_contents_and_dir(
500c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey                create_data_user_de_package_path(uuid_, userId, pkgname));
501c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey        destroy_app_current_profiles(pkgname, userId);
5027535e8e1995398befc866062d63c05d90eb70cccCalin Juravle        // TODO(calin): If the package is still installed by other users it's probably
5037535e8e1995398befc866062d63c05d90eb70cccCalin Juravle        // beneficial to keep the reference profile around.
5047535e8e1995398befc866062d63c05d90eb70cccCalin Juravle        // Verify if it's ok to do that.
5057535e8e1995398befc866062d63c05d90eb70cccCalin Juravle        destroy_app_reference_profile(pkgname);
50694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
507c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey    return res ? binder::Status::fromServiceSpecificError(-1) : binder::Status::ok();
50894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
50994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
5100274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeybinder::Status InstalldNativeService::moveCompleteApp(const std::unique_ptr<std::string>& fromUuid,
5110274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey        const std::unique_ptr<std::string>& toUuid, const std::string& packageName,
5120274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey        const std::string& dataAppName, int32_t appId, const std::string& seInfo,
5130274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey        int32_t targetSdkVersion) {
5140274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey
5150274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey    const char* from_uuid = fromUuid ? fromUuid->c_str() : nullptr;
5160274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey    const char* to_uuid = toUuid ? toUuid->c_str() : nullptr;
5170274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey    const char* package_name = packageName.c_str();
5180274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey    const char* data_app_name = dataAppName.c_str();
5190274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey
520e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    std::vector<userid_t> users = get_known_users(from_uuid);
521e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey
522d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey    // Copy app
523d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey    {
52451c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey        auto from = create_data_app_package_path(from_uuid, data_app_name);
52551c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey        auto to = create_data_app_package_path(to_uuid, data_app_name);
52651c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey        auto to_parent = create_data_app_path(to_uuid);
527d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey
528d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey        char *argv[] = {
529d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey            (char*) kCpPath,
530d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey            (char*) "-F", /* delete any existing destination file first (--remove-destination) */
531d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey            (char*) "-p", /* preserve timestamps, ownership, and permissions */
532d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey            (char*) "-R", /* recurse into subdirectories (DEST must be a directory) */
533d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey            (char*) "-P", /* Do not follow symlinks [default] */
534d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey            (char*) "-d", /* don't dereference symlinks */
535d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey            (char*) from.c_str(),
536d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey            (char*) to_parent.c_str()
537d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey        };
538d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey
539d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey        LOG(DEBUG) << "Copying " << from << " to " << to;
540d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey        int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true);
541d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey
542d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey        if (rc != 0) {
543d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey            LOG(ERROR) << "Failed copying " << from << " to " << to
544d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey                    << ": status " << rc;
545d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey            goto fail;
546d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey        }
547d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey
548d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey        if (selinux_android_restorecon(to.c_str(), SELINUX_ANDROID_RESTORECON_RECURSE) != 0) {
549d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey            LOG(ERROR) << "Failed to restorecon " << to;
550d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey            goto fail;
551d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey        }
552d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey    }
553d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey
554d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey    // Copy private data for all known users
555e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    for (auto user : users) {
556e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey
557e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey        // Data source may not exist for all users; that's okay
55851c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey        auto from_ce = create_data_user_ce_package_path(from_uuid, user, package_name);
55951c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey        if (access(from_ce.c_str(), F_OK) != 0) {
56051c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey            LOG(INFO) << "Missing source " << from_ce;
561e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey            continue;
562e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey        }
563e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey
5640274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey        if (!createAppData(toUuid, packageName, user, FLAG_STORAGE_CE | FLAG_STORAGE_DE, appId,
5650274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey                seInfo, targetSdkVersion).isOk()) {
56651c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey            LOG(ERROR) << "Failed to create package target on " << to_uuid;
567e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey            goto fail;
568e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey        }
569e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey
570e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey        char *argv[] = {
571e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey            (char*) kCpPath,
572e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey            (char*) "-F", /* delete any existing destination file first (--remove-destination) */
573e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey            (char*) "-p", /* preserve timestamps, ownership, and permissions */
574e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey            (char*) "-R", /* recurse into subdirectories (DEST must be a directory) */
575e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey            (char*) "-P", /* Do not follow symlinks [default] */
576e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey            (char*) "-d", /* don't dereference symlinks */
57751c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey            nullptr,
57851c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey            nullptr
579e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey        };
580e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey
58151c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey        {
58251c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey            auto from = create_data_user_de_package_path(from_uuid, user, package_name);
58351c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey            auto to = create_data_user_de_path(to_uuid, user);
58451c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey            argv[6] = (char*) from.c_str();
58551c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey            argv[7] = (char*) to.c_str();
58651c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey
58751c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey            LOG(DEBUG) << "Copying " << from << " to " << to;
58851c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey            int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true);
58951c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey            if (rc != 0) {
59051c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey                LOG(ERROR) << "Failed copying " << from << " to " << to << " with status " << rc;
59151c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey                goto fail;
59251c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey            }
59351c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey        }
59451c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey        {
59551c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey            auto from = create_data_user_ce_package_path(from_uuid, user, package_name);
59651c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey            auto to = create_data_user_ce_path(to_uuid, user);
59751c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey            argv[6] = (char*) from.c_str();
59851c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey            argv[7] = (char*) to.c_str();
59951c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey
60051c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey            LOG(DEBUG) << "Copying " << from << " to " << to;
60151c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey            int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true);
60251c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey            if (rc != 0) {
60351c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey                LOG(ERROR) << "Failed copying " << from << " to " << to << " with status " << rc;
60451c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey                goto fail;
60551c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey            }
606e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey        }
607e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey
608c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey        if (!restoreconAppData(toUuid, packageName, user, FLAG_STORAGE_CE | FLAG_STORAGE_DE,
609c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey                appId, seInfo).isOk()) {
610c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey            LOG(ERROR) << "Failed to restorecon";
611c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey            goto fail;
612c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey        }
613e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    }
614e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey
61531f08986f83fa6f2dcf55523b2cf706460aeed7cJeff Sharkey    // We let the framework scan the new location and persist that before
61631f08986f83fa6f2dcf55523b2cf706460aeed7cJeff Sharkey    // deleting the data in the old location; this ordering ensures that
61731f08986f83fa6f2dcf55523b2cf706460aeed7cJeff Sharkey    // we can recover from things like battery pulls.
6180274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey    return binder::Status::ok();
619e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey
620e36372423000a906bafae68844ebc6c42d09335aJeff Sharkeyfail:
621e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    // Nuke everything we might have already copied
622d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey    {
62351c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey        auto to = create_data_app_package_path(to_uuid, data_app_name);
624d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey        if (delete_dir_contents(to.c_str(), 1, NULL) != 0) {
625d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey            LOG(WARNING) << "Failed to rollback " << to;
626d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey        }
627d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey    }
628e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    for (auto user : users) {
62951c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey        {
63051c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey            auto to = create_data_user_de_package_path(to_uuid, user, package_name);
63151c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey            if (delete_dir_contents(to.c_str(), 1, NULL) != 0) {
63251c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey                LOG(WARNING) << "Failed to rollback " << to;
63351c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey            }
63451c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey        }
63551c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey        {
63651c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey            auto to = create_data_user_ce_package_path(to_uuid, user, package_name);
63751c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey            if (delete_dir_contents(to.c_str(), 1, NULL) != 0) {
63851c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey                LOG(WARNING) << "Failed to rollback " << to;
63951c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey            }
640e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey        }
641e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    }
6420274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey    return binder::Status::fromServiceSpecificError(-1);
643e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey}
644e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey
645c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkeybinder::Status InstalldNativeService::createUserData(const std::unique_ptr<std::string>& uuid,
646c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey        int32_t userId, int32_t userSerial ATTRIBUTE_UNUSED, int32_t flags) {
647c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey    ENFORCE_UID(AID_SYSTEM);
648c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey    const char* uuid_ = uuid ? uuid->c_str() : nullptr;
649c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey    int res = 0;
650379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey    if (flags & FLAG_STORAGE_DE) {
651c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey        if (uuid_ == nullptr) {
652c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey            res = ensure_config_user_dirs(userId);
653379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey        }
65494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
655c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey    return res ? binder::Status::fromServiceSpecificError(-1) : binder::Status::ok();
656095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee}
657095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee
658c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkeybinder::Status InstalldNativeService::destroyUserData(const std::unique_ptr<std::string>& uuid,
659c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey        int32_t userId, int32_t flags) {
660c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey    ENFORCE_UID(AID_SYSTEM);
661c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey    const char* uuid_ = uuid ? uuid->c_str() : nullptr;
662ebf728fd43ab5c7d11a1f9e5fdc775d6740fae0aJeff Sharkey    int res = 0;
663379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey    if (flags & FLAG_STORAGE_DE) {
664c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey        res |= delete_dir_contents_and_dir(create_data_user_de_path(uuid_, userId), true);
665c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey        if (uuid_ == nullptr) {
666c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey            res |= delete_dir_contents_and_dir(create_data_misc_legacy_path(userId), true);
667c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey            res |= delete_dir_contents_and_dir(create_data_user_profiles_path(userId), true);
66841ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey        }
66994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
670379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey    if (flags & FLAG_STORAGE_CE) {
671c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey        res |= delete_dir_contents_and_dir(create_data_user_ce_path(uuid_, userId), true);
672c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey        res |= delete_dir_contents_and_dir(create_data_media_path(uuid_, userId), true);
673379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey    }
674c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey    return res ? binder::Status::fromServiceSpecificError(-1) : binder::Status::ok();
67594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
67694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
67794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/* Try to ensure free_size bytes of storage are available.
67894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * Returns 0 on success.
67994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * This is rather simple-minded because doing a full LRU would
68094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * be potentially memory-intensive, and without atime it would
68194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * also require that apps constantly modify file metadata even
68294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * when just reading from the cache, which is pretty awful.
68394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */
684475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::freeCache(const std::unique_ptr<std::string>& uuid,
685475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey        int64_t freeStorageSize) {
686475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    ENFORCE_UID(AID_SYSTEM);
687475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    const char* uuid_ = uuid ? uuid->c_str() : nullptr;
68894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    cache_t* cache;
68994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int64_t avail;
69094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
691475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    auto data_path = create_data_path(uuid_);
69241ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey
69341ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey    avail = data_disk_free(data_path);
694475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    if (avail < 0) {
695475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey        return binder::Status::fromServiceSpecificError(-1);
696475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    }
69794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
698475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    ALOGI("free_cache(%" PRId64 ") avail %" PRId64 "\n", freeStorageSize, avail);
699475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    if (avail >= freeStorageSize) {
700475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey        return binder::Status::ok();
701475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    }
70294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
70394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    cache = start_cache_collection();
70494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
705475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    auto users = get_known_users(uuid_);
70654e292e1ea87b504b552393d2e9bc800458bd0c1Jeff Sharkey    for (auto user : users) {
707475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey        add_cache_files(cache, create_data_user_ce_path(uuid_, user));
708475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey        add_cache_files(cache, create_data_user_de_path(uuid_, user));
70954e292e1ea87b504b552393d2e9bc800458bd0c1Jeff Sharkey        add_cache_files(cache,
710475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey                StringPrintf("%s/Android/data", create_data_media_path(uuid_, user).c_str()));
71194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
71294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
713475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    clear_cache_files(data_path, cache, freeStorageSize);
71494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    finish_cache_collection(cache);
71594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
716475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    if (data_disk_free(data_path) >= freeStorageSize) {
717475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey        return binder::Status::ok();
718475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    } else {
719475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey        return binder::Status::fromServiceSpecificError(-1);
720475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    }
72194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
72294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
723475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::rmdex(const std::string& codePath,
724475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey        const std::string& instructionSet) {
725475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    ENFORCE_UID(AID_SYSTEM);
72694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    char dex_path[PKG_PATH_MAX];
72794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
728475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    const char* path = codePath.c_str();
729475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    const char* instruction_set = instructionSet.c_str();
730475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey
731770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey    if (validate_apk_path(path) && validate_system_app_path(path)) {
732770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey        ALOGE("invalid apk path '%s' (bad prefix)\n", path);
733475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey        return binder::Status::fromServiceSpecificError(-1);
734770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey    }
735770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey
736475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    if (!create_cache_path(dex_path, path, instruction_set)) {
737475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey        return binder::Status::fromServiceSpecificError(-1);
738475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    }
73994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
74094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    ALOGV("unlink %s\n", dex_path);
74194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (unlink(dex_path) < 0) {
742770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey        if (errno != ENOENT) {
743770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey            ALOGE("Couldn't unlink %s: %s\n", dex_path, strerror(errno));
744770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey        }
745475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey        return binder::Status::fromServiceSpecificError(-1);
74694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    } else {
747475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey        return binder::Status::ok();
74894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
74994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
75094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
7512f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkeystatic void add_app_data_size(std::string& path, int64_t *codesize, int64_t *datasize,
7522f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey        int64_t *cachesize) {
75394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    DIR *d;
75494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int dfd;
75594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    struct dirent *de;
75694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    struct stat s;
757d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey
7582f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey    d = opendir(path.c_str());
7592f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey    if (d == nullptr) {
7602f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey        PLOG(WARNING) << "Failed to open " << path;
7612f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey        return;
76294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
7632f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey    dfd = dirfd(d);
7642f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey    while ((de = readdir(d))) {
7652f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey        const char *name = de->d_name;
766d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey
7672f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey        int64_t statsize = 0;
7682f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey        if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
7692f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey            statsize = stat_size(&s);
77094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
77194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
7722f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey        if (de->d_type == DT_DIR) {
7732f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey            int subfd;
7742f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey            int64_t dirsize = 0;
7752f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey            /* always skip "." and ".." */
7762f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey            if (name[0] == '.') {
7772f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey                if (name[1] == 0) continue;
7782f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey                if ((name[1] == '.') && (name[2] == 0)) continue;
7792f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey            }
7802f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey            subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY);
7812f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey            if (subfd >= 0) {
7822f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey                dirsize = calculate_dir_size(subfd);
7832f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey                close(subfd);
7842f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey            }
7852f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey            // TODO: check xattrs!
7862f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey            if (!strcmp(name, "cache") || !strcmp(name, "code_cache")) {
7872f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey                *datasize += statsize;
7882f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey                *cachesize += dirsize;
7892f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey            } else {
7902f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey                *datasize += dirsize + statsize;
7912f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey            }
7922f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey        } else if (de->d_type == DT_LNK && !strcmp(name, "lib")) {
7932f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey            *codesize += statsize;
7942f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey        } else {
7952f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey            *datasize += statsize;
79694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
79794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
7982f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey    closedir(d);
7992f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey}
80094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
8016c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkeybinder::Status InstalldNativeService::getAppSize(const std::unique_ptr<std::string>& uuid,
8026c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey    const std::string& packageName, int32_t userId, int32_t flags, int64_t ceDataInode,
8036c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey    const std::string& codePath, std::vector<int64_t>* _aidl_return) {
8046c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey    ENFORCE_UID(AID_SYSTEM);
8056c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey    const char* uuid_ = uuid ? uuid->c_str() : nullptr;
8066c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey    const char* pkgname = packageName.c_str();
8076c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey    const char* code_path = codePath.c_str();
8086c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey
8092f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey    DIR *d;
8102f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey    int dfd;
8116c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey    int64_t codesize = 0;
8126c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey    int64_t datasize = 0;
8136c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey    int64_t cachesize = 0;
8146c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey    int64_t asecsize = 0;
81594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
8162f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey    d = opendir(code_path);
8172f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey    if (d != nullptr) {
8182f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey        dfd = dirfd(d);
8196c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey        codesize += calculate_dir_size(dfd);
8202f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey        closedir(d);
82194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
82294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
8232f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey    if (flags & FLAG_STORAGE_CE) {
8246c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey        auto path = create_data_user_ce_package_path(uuid_, userId, pkgname, ceDataInode);
8256c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey        add_app_data_size(path, &codesize, &datasize, &cachesize);
8262f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey    }
8272f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey    if (flags & FLAG_STORAGE_DE) {
8286c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey        auto path = create_data_user_de_package_path(uuid_, userId, pkgname);
8296c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey        add_app_data_size(path, &codesize, &datasize, &cachesize);
8302f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey    }
83194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
8326c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey    std::vector<int64_t> res;
8336c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey    res.push_back(codesize);
8346c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey    res.push_back(datasize);
8356c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey    res.push_back(cachesize);
8366c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey    res.push_back(asecsize);
8376c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey    *_aidl_return = res;
8386c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey    return binder::Status::ok();
8392f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey}
840d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey
841c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkeybinder::Status InstalldNativeService::getAppDataInode(const std::unique_ptr<std::string>& uuid,
8426c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey        const std::string& packageName, int32_t userId, int32_t flags, int64_t* _aidl_return) {
843c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey    ENFORCE_UID(AID_SYSTEM);
844c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey    const char* uuid_ = uuid ? uuid->c_str() : nullptr;
845c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey    const char* pkgname = packageName.c_str();
846c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey
847c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey    int res = 0;
8482f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey    if (flags & FLAG_STORAGE_CE) {
849c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey        auto path = create_data_user_ce_package_path(uuid_, userId, pkgname);
850c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey        res = get_path_inode(path, reinterpret_cast<ino_t*>(_aidl_return));
85194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
852c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey    return res ? binder::Status::fromServiceSpecificError(-1) : binder::Status::ok();
85394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
85494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
855b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Roubanstatic int split_count(const char *str)
856b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban{
857b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban  char *ctx;
858b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban  int count = 0;
85902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe  char buf[kPropertyValueMax];
860b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban
861b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban  strncpy(buf, str, sizeof(buf));
862b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban  char *pBuf = buf;
863b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban
864b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban  while(strtok_r(pBuf, " ", &ctx) != NULL) {
865b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban    count++;
866b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban    pBuf = NULL;
867b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban  }
868b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban
869b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban  return count;
870b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban}
871b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban
87214e084d39ac44e9d4d0a0143bf1f111011343d34neo.chaestatic int split(char *buf, const char **argv)
873b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban{
874b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban  char *ctx;
875b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban  int count = 0;
876b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban  char *tok;
877b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban  char *pBuf = buf;
878b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban
879b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban  while((tok = strtok_r(pBuf, " ", &ctx)) != NULL) {
880b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban    argv[count++] = tok;
881b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban    pBuf = NULL;
882b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban  }
883b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban
884b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban  return count;
885b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban}
886b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban
887fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdilstatic void run_patchoat(int input_oat_fd, int input_vdex_fd, int out_oat_fd, int out_vdex_fd,
888fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil    const char* input_oat_file_name, const char* input_vdex_file_name,
889fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil    const char* output_oat_file_name, const char* output_vdex_file_name,
890fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil    const char *pkgname ATTRIBUTE_UNUSED, const char *instruction_set)
8917365a10689df23334d245b211ce272502ad20669Alex Light{
8927365a10689df23334d245b211ce272502ad20669Alex Light    static const int MAX_INT_LEN = 12;      // '-'+10dig+'\0' -OR- 0x+8dig
8938fc7315a67fdbf9b56cc6fa061f0773be2223a81Calin Juravle    static const unsigned int MAX_INSTRUCTION_SET_LEN = 7;
8947365a10689df23334d245b211ce272502ad20669Alex Light
8957365a10689df23334d245b211ce272502ad20669Alex Light    static const char* PATCHOAT_BIN = "/system/bin/patchoat";
8967365a10689df23334d245b211ce272502ad20669Alex Light    if (strlen(instruction_set) >= MAX_INSTRUCTION_SET_LEN) {
8977365a10689df23334d245b211ce272502ad20669Alex Light        ALOGE("Instruction set %s longer than max length of %d",
8987365a10689df23334d245b211ce272502ad20669Alex Light              instruction_set, MAX_INSTRUCTION_SET_LEN);
8997365a10689df23334d245b211ce272502ad20669Alex Light        return;
9007365a10689df23334d245b211ce272502ad20669Alex Light    }
9017365a10689df23334d245b211ce272502ad20669Alex Light
9027365a10689df23334d245b211ce272502ad20669Alex Light    /* input_file_name/input_fd should be the .odex/.oat file that is precompiled. I think*/
9037365a10689df23334d245b211ce272502ad20669Alex Light    char instruction_set_arg[strlen("--instruction-set=") + MAX_INSTRUCTION_SET_LEN];
9047365a10689df23334d245b211ce272502ad20669Alex Light    char input_oat_fd_arg[strlen("--input-oat-fd=") + MAX_INT_LEN];
905fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil    char input_vdex_fd_arg[strlen("--input-vdex-fd=") + MAX_INT_LEN];
906fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil    char output_oat_fd_arg[strlen("--output-oat-fd=") + MAX_INT_LEN];
907fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil    char output_vdex_fd_arg[strlen("--output-vdex-fd=") + MAX_INT_LEN];
9087365a10689df23334d245b211ce272502ad20669Alex Light    const char* patched_image_location_arg = "--patched-image-location=/system/framework/boot.art";
9097365a10689df23334d245b211ce272502ad20669Alex Light    // The caller has already gotten all the locks we need.
9107365a10689df23334d245b211ce272502ad20669Alex Light    const char* no_lock_arg = "--no-lock-output";
9117365a10689df23334d245b211ce272502ad20669Alex Light    sprintf(instruction_set_arg, "--instruction-set=%s", instruction_set);
912fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil    sprintf(output_oat_fd_arg, "--output-oat-fd=%d", out_oat_fd);
913fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil    sprintf(input_oat_fd_arg, "--input-oat-fd=%d", input_oat_fd);
914fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil    ALOGV("Running %s isa=%s in-oat-fd=%d (%s) in-vdex-fd=%d (%s) "
915fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil          "out-oat-fd=%d (%s) out-vdex-fd=%d (%s)\n",
916fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil          PATCHOAT_BIN, instruction_set,
917fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil          input_oat_fd, input_oat_file_name,
918fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil          input_vdex_fd, input_vdex_file_name,
919fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil          out_oat_fd, output_oat_file_name,
920fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil          out_vdex_fd, output_vdex_file_name);
9217365a10689df23334d245b211ce272502ad20669Alex Light
9227365a10689df23334d245b211ce272502ad20669Alex Light    /* patchoat, patched-image-location, no-lock, isa, input-fd, output-fd */
923fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil    char* argv[9];
9247365a10689df23334d245b211ce272502ad20669Alex Light    argv[0] = (char*) PATCHOAT_BIN;
9257365a10689df23334d245b211ce272502ad20669Alex Light    argv[1] = (char*) patched_image_location_arg;
9267365a10689df23334d245b211ce272502ad20669Alex Light    argv[2] = (char*) no_lock_arg;
9277365a10689df23334d245b211ce272502ad20669Alex Light    argv[3] = instruction_set_arg;
928fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil    argv[4] = input_oat_fd_arg;
929fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil    argv[5] = input_vdex_fd_arg;
930fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil    argv[6] = output_oat_fd_arg;
931fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil    argv[7] = output_vdex_fd_arg;
932fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil    argv[8] = NULL;
9337365a10689df23334d245b211ce272502ad20669Alex Light
9347365a10689df23334d245b211ce272502ad20669Alex Light    execv(PATCHOAT_BIN, (char* const *)argv);
9357365a10689df23334d245b211ce272502ad20669Alex Light    ALOGE("execv(%s) failed: %s\n", PATCHOAT_BIN, strerror(errno));
9367365a10689df23334d245b211ce272502ad20669Alex Light}
9377365a10689df23334d245b211ce272502ad20669Alex Light
93853caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffraystatic void run_dex2oat(int zip_fd, int oat_fd, int input_vdex_fd, int output_vdex_fd, int image_fd,
939fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil        const char* input_file_name, const char* output_file_name, int swap_fd,
940fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil        const char *instruction_set, const char* compiler_filter, bool vm_safe_mode,
941fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil        bool debuggable, bool post_bootcomplete, int profile_fd, const char* shared_libraries) {
9428fc7315a67fdbf9b56cc6fa061f0773be2223a81Calin Juravle    static const unsigned int MAX_INSTRUCTION_SET_LEN = 7;
9438fc7315a67fdbf9b56cc6fa061f0773be2223a81Calin Juravle
9448fc7315a67fdbf9b56cc6fa061f0773be2223a81Calin Juravle    if (strlen(instruction_set) >= MAX_INSTRUCTION_SET_LEN) {
9458fc7315a67fdbf9b56cc6fa061f0773be2223a81Calin Juravle        ALOGE("Instruction set %s longer than max length of %d",
9468fc7315a67fdbf9b56cc6fa061f0773be2223a81Calin Juravle              instruction_set, MAX_INSTRUCTION_SET_LEN);
9478fc7315a67fdbf9b56cc6fa061f0773be2223a81Calin Juravle        return;
9488fc7315a67fdbf9b56cc6fa061f0773be2223a81Calin Juravle    }
9498fc7315a67fdbf9b56cc6fa061f0773be2223a81Calin Juravle
95002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    char dex2oat_Xms_flag[kPropertyValueMax];
95102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    bool have_dex2oat_Xms_flag = get_property("dalvik.vm.dex2oat-Xms", dex2oat_Xms_flag, NULL) > 0;
952e46a75a0f6007967cd0d161959af844772cdc330Brian Carlstrom
95302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    char dex2oat_Xmx_flag[kPropertyValueMax];
95402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    bool have_dex2oat_Xmx_flag = get_property("dalvik.vm.dex2oat-Xmx", dex2oat_Xmx_flag, NULL) > 0;
955e46a75a0f6007967cd0d161959af844772cdc330Brian Carlstrom
95602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    char dex2oat_threads_buf[kPropertyValueMax];
95702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    bool have_dex2oat_threads_flag = get_property(post_bootcomplete
958919461cbeef3e9f35388fc099a5fcdae1cb79cc6Andreas Gampe                                                      ? "dalvik.vm.dex2oat-threads"
959919461cbeef3e9f35388fc099a5fcdae1cb79cc6Andreas Gampe                                                      : "dalvik.vm.boot-dex2oat-threads",
960919461cbeef3e9f35388fc099a5fcdae1cb79cc6Andreas Gampe                                                  dex2oat_threads_buf,
961919461cbeef3e9f35388fc099a5fcdae1cb79cc6Andreas Gampe                                                  NULL) > 0;
96202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    char dex2oat_threads_arg[kPropertyValueMax + 2];
9638d7af8b2418cc5e7e59746f0cb359a75ed0bdfd1Andreas Gampe    if (have_dex2oat_threads_flag) {
9648d7af8b2418cc5e7e59746f0cb359a75ed0bdfd1Andreas Gampe        sprintf(dex2oat_threads_arg, "-j%s", dex2oat_threads_buf);
9658d7af8b2418cc5e7e59746f0cb359a75ed0bdfd1Andreas Gampe    }
9668d7af8b2418cc5e7e59746f0cb359a75ed0bdfd1Andreas Gampe
96702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    char dex2oat_isa_features_key[kPropertyKeyMax];
9688fc7315a67fdbf9b56cc6fa061f0773be2223a81Calin Juravle    sprintf(dex2oat_isa_features_key, "dalvik.vm.isa.%s.features", instruction_set);
96902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    char dex2oat_isa_features[kPropertyValueMax];
97002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    bool have_dex2oat_isa_features = get_property(dex2oat_isa_features_key,
9718fc7315a67fdbf9b56cc6fa061f0773be2223a81Calin Juravle                                                  dex2oat_isa_features, NULL) > 0;
9728fc7315a67fdbf9b56cc6fa061f0773be2223a81Calin Juravle
97302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    char dex2oat_isa_variant_key[kPropertyKeyMax];
97416a95b267ac6aac75453f65f1bc382ce65c0741eIan Rogers    sprintf(dex2oat_isa_variant_key, "dalvik.vm.isa.%s.variant", instruction_set);
97502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    char dex2oat_isa_variant[kPropertyValueMax];
97602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    bool have_dex2oat_isa_variant = get_property(dex2oat_isa_variant_key,
97716a95b267ac6aac75453f65f1bc382ce65c0741eIan Rogers                                                 dex2oat_isa_variant, NULL) > 0;
97816a95b267ac6aac75453f65f1bc382ce65c0741eIan Rogers
97914e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae    const char *dex2oat_norelocation = "-Xnorelocate";
98014e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae    bool have_dex2oat_relocation_skip_flag = false;
98114e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae
98202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    char dex2oat_flags[kPropertyValueMax];
98302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    int dex2oat_flags_count = get_property("dalvik.vm.dex2oat-flags",
984b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban                                 dex2oat_flags, NULL) <= 0 ? 0 : split_count(dex2oat_flags);
9850ae8e39ebcc26836ba55a5ee4481825a0f473a9eBrian Carlstrom    ALOGV("dalvik.vm.dex2oat-flags=%s\n", dex2oat_flags);
9860ae8e39ebcc26836ba55a5ee4481825a0f473a9eBrian Carlstrom
987538998f204d1e542e235de9e7ce18ef4dc68c9ccBrian Carlstrom    // If we booting without the real /data, don't spend time compiling.
98802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    char vold_decrypt[kPropertyValueMax];
98902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    bool have_vold_decrypt = get_property("vold.decrypt", vold_decrypt, "") > 0;
990538998f204d1e542e235de9e7ce18ef4dc68c9ccBrian Carlstrom    bool skip_compilation = (have_vold_decrypt &&
991538998f204d1e542e235de9e7ce18ef4dc68c9ccBrian Carlstrom                             (strcmp(vold_decrypt, "trigger_restart_min_framework") == 0 ||
992538998f204d1e542e235de9e7ce18ef4dc68c9ccBrian Carlstrom                             (strcmp(vold_decrypt, "1") == 0)));
993538998f204d1e542e235de9e7ce18ef4dc68c9ccBrian Carlstrom
9946a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    bool generate_debug_info = property_get_bool("debug.generate-debug-info");
995d4a7b459f4f383988440c0512513b321ce03fc22Mathieu Chartier
996416fa12cd0949c7f12e2067b36ac7ac64ba70b4aMathieu Chartier    char app_image_format[kPropertyValueMax];
997416fa12cd0949c7f12e2067b36ac7ac64ba70b4aMathieu Chartier    char image_format_arg[strlen("--image-format=") + kPropertyValueMax];
998416fa12cd0949c7f12e2067b36ac7ac64ba70b4aMathieu Chartier    bool have_app_image_format =
99941fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier            image_fd >= 0 && get_property("dalvik.vm.appimageformat", app_image_format, NULL) > 0;
1000416fa12cd0949c7f12e2067b36ac7ac64ba70b4aMathieu Chartier    if (have_app_image_format) {
1001416fa12cd0949c7f12e2067b36ac7ac64ba70b4aMathieu Chartier        sprintf(image_format_arg, "--image-format=%s", app_image_format);
1002416fa12cd0949c7f12e2067b36ac7ac64ba70b4aMathieu Chartier    }
1003416fa12cd0949c7f12e2067b36ac7ac64ba70b4aMathieu Chartier
1004a5cc10a8e8d47fd01c117cc51ac1f6765a1735ceAndreas Gampe    char dex2oat_large_app_threshold[kPropertyValueMax];
1005a5cc10a8e8d47fd01c117cc51ac1f6765a1735ceAndreas Gampe    bool have_dex2oat_large_app_threshold =
1006a5cc10a8e8d47fd01c117cc51ac1f6765a1735ceAndreas Gampe            get_property("dalvik.vm.dex2oat-very-large", dex2oat_large_app_threshold, NULL) > 0;
1007a5cc10a8e8d47fd01c117cc51ac1f6765a1735ceAndreas Gampe    char dex2oat_large_app_threshold_arg[strlen("--very-large-app-threshold=") + kPropertyValueMax];
1008a5cc10a8e8d47fd01c117cc51ac1f6765a1735ceAndreas Gampe    if (have_dex2oat_large_app_threshold) {
1009a5cc10a8e8d47fd01c117cc51ac1f6765a1735ceAndreas Gampe        sprintf(dex2oat_large_app_threshold_arg,
1010a5cc10a8e8d47fd01c117cc51ac1f6765a1735ceAndreas Gampe                "--very-large-app-threshold=%s",
1011a5cc10a8e8d47fd01c117cc51ac1f6765a1735ceAndreas Gampe                dex2oat_large_app_threshold);
1012a5cc10a8e8d47fd01c117cc51ac1f6765a1735ceAndreas Gampe    }
1013a5cc10a8e8d47fd01c117cc51ac1f6765a1735ceAndreas Gampe
10141705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom    static const char* DEX2OAT_BIN = "/system/bin/dex2oat";
101553e0776d967324e2908e3be56b80cddb2c9d9e03Brian Carlstrom
101653e0776d967324e2908e3be56b80cddb2c9d9e03Brian Carlstrom    static const char* RUNTIME_ARG = "--runtime-arg";
101753e0776d967324e2908e3be56b80cddb2c9d9e03Brian Carlstrom
10181705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom    static const int MAX_INT_LEN = 12;      // '-'+10dig+'\0' -OR- 0x+8dig
10191b4003207750ea8fe8c7b03eb32d80f1df83979eNarayan Kamath
10201705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom    char zip_fd_arg[strlen("--zip-fd=") + MAX_INT_LEN];
10211705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom    char zip_location_arg[strlen("--zip-location=") + PKG_PATH_MAX];
102253caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray    char input_vdex_fd_arg[strlen("--input-vdex-fd=") + MAX_INT_LEN];
102353caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray    char output_vdex_fd_arg[strlen("--output-vdex-fd=") + MAX_INT_LEN];
10241705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom    char oat_fd_arg[strlen("--oat-fd=") + MAX_INT_LEN];
10257195fcc2185c35b45a8f28c79fde4999b38850e8Brian Carlstrom    char oat_location_arg[strlen("--oat-location=") + PKG_PATH_MAX];
10261b4003207750ea8fe8c7b03eb32d80f1df83979eNarayan Kamath    char instruction_set_arg[strlen("--instruction-set=") + MAX_INSTRUCTION_SET_LEN];
102702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    char instruction_set_variant_arg[strlen("--instruction-set-variant=") + kPropertyValueMax];
102802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    char instruction_set_features_arg[strlen("--instruction-set-features=") + kPropertyValueMax];
102902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    char dex2oat_Xms_arg[strlen("-Xms") + kPropertyValueMax];
103002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    char dex2oat_Xmx_arg[strlen("-Xmx") + kPropertyValueMax];
103102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    char dex2oat_compiler_filter_arg[strlen("--compiler-filter=") + kPropertyValueMax];
1032e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe    bool have_dex2oat_swap_fd = false;
1033e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe    char dex2oat_swap_fd[strlen("--swap-fd=") + MAX_INT_LEN];
1034edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier    bool have_dex2oat_image_fd = false;
1035edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier    char dex2oat_image_fd[strlen("--app-image-fd=") + MAX_INT_LEN];
10361705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom
10371705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom    sprintf(zip_fd_arg, "--zip-fd=%d", zip_fd);
10381705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom    sprintf(zip_location_arg, "--zip-location=%s", input_file_name);
103953caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray    sprintf(input_vdex_fd_arg, "--input-vdex-fd=%d", input_vdex_fd);
104053caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray    sprintf(output_vdex_fd_arg, "--output-vdex-fd=%d", output_vdex_fd);
10411705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom    sprintf(oat_fd_arg, "--oat-fd=%d", oat_fd);
10421705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom    sprintf(oat_location_arg, "--oat-location=%s", output_file_name);
10431b4003207750ea8fe8c7b03eb32d80f1df83979eNarayan Kamath    sprintf(instruction_set_arg, "--instruction-set=%s", instruction_set);
104416a95b267ac6aac75453f65f1bc382ce65c0741eIan Rogers    sprintf(instruction_set_variant_arg, "--instruction-set-variant=%s", dex2oat_isa_variant);
10458fc7315a67fdbf9b56cc6fa061f0773be2223a81Calin Juravle    sprintf(instruction_set_features_arg, "--instruction-set-features=%s", dex2oat_isa_features);
1046e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe    if (swap_fd >= 0) {
1047e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe        have_dex2oat_swap_fd = true;
1048e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe        sprintf(dex2oat_swap_fd, "--swap-fd=%d", swap_fd);
1049e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe    }
1050edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier    if (image_fd >= 0) {
1051edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier        have_dex2oat_image_fd = true;
1052edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier        sprintf(dex2oat_image_fd, "--app-image-fd=%d", image_fd);
1053edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier    }
105457c69c39640a7d6a08b3afb56361d76f7ecf1250Calin Juravle
1055e46a75a0f6007967cd0d161959af844772cdc330Brian Carlstrom    if (have_dex2oat_Xms_flag) {
1056e46a75a0f6007967cd0d161959af844772cdc330Brian Carlstrom        sprintf(dex2oat_Xms_arg, "-Xms%s", dex2oat_Xms_flag);
1057e46a75a0f6007967cd0d161959af844772cdc330Brian Carlstrom    }
1058e46a75a0f6007967cd0d161959af844772cdc330Brian Carlstrom    if (have_dex2oat_Xmx_flag) {
1059e46a75a0f6007967cd0d161959af844772cdc330Brian Carlstrom        sprintf(dex2oat_Xmx_arg, "-Xmx%s", dex2oat_Xmx_flag);
1060e46a75a0f6007967cd0d161959af844772cdc330Brian Carlstrom    }
10614d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe
10624d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe    // Compute compiler filter.
10634d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe
10644d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe    bool have_dex2oat_compiler_filter_flag;
1065538998f204d1e542e235de9e7ce18ef4dc68c9ccBrian Carlstrom    if (skip_compilation) {
1066e18987efb5e39ca1bed15527b7b82bde55c99669Brian Carlstrom        strcpy(dex2oat_compiler_filter_arg, "--compiler-filter=verify-none");
1067538998f204d1e542e235de9e7ce18ef4dc68c9ccBrian Carlstrom        have_dex2oat_compiler_filter_flag = true;
106814e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae        have_dex2oat_relocation_skip_flag = true;
1069b1efac103523efccbe671e76cc0eaaeab810415bCalin Juravle    } else if (vm_safe_mode) {
1070b1efac103523efccbe671e76cc0eaaeab810415bCalin Juravle        strcpy(dex2oat_compiler_filter_arg, "--compiler-filter=interpret-only");
107197477d203eaf0c3235bbe2415356f20a0431cadaCalin Juravle        have_dex2oat_compiler_filter_flag = true;
10724d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe    } else if (compiler_filter != nullptr &&
10734d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe            strlen(compiler_filter) + strlen("--compiler-filter=") <
10744d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe                    arraysize(dex2oat_compiler_filter_arg)) {
10754d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe        sprintf(dex2oat_compiler_filter_arg, "--compiler-filter=%s", compiler_filter);
1076d4a7b459f4f383988440c0512513b321ce03fc22Mathieu Chartier        have_dex2oat_compiler_filter_flag = true;
10774d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe    } else {
10784d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe        char dex2oat_compiler_filter_flag[kPropertyValueMax];
10794d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe        have_dex2oat_compiler_filter_flag = get_property("dalvik.vm.dex2oat-filter",
10804d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe                                                         dex2oat_compiler_filter_flag, NULL) > 0;
10814d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe        if (have_dex2oat_compiler_filter_flag) {
10824d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe            sprintf(dex2oat_compiler_filter_arg,
10834d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe                    "--compiler-filter=%s",
10844d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe                    dex2oat_compiler_filter_flag);
10854d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe        }
1086cf51ba1360ee13459830a0502b0d454d0145544fBrian Carlstrom    }
1087e46a75a0f6007967cd0d161959af844772cdc330Brian Carlstrom
1088598c25e23f7c97470e09a2316513ddf2efdfb670Andreas Gampe    // Check whether all apps should be compiled debuggable.
1089598c25e23f7c97470e09a2316513ddf2efdfb670Andreas Gampe    if (!debuggable) {
109002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        char prop_buf[kPropertyValueMax];
1091598c25e23f7c97470e09a2316513ddf2efdfb670Andreas Gampe        debuggable =
109202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe                (get_property("dalvik.vm.always_debuggable", prop_buf, "0") > 0) &&
1093598c25e23f7c97470e09a2316513ddf2efdfb670Andreas Gampe                (prop_buf[0] == '1');
1094598c25e23f7c97470e09a2316513ddf2efdfb670Andreas Gampe    }
10956a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    char profile_arg[strlen("--profile-file-fd=") + MAX_INT_LEN];
10966a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    if (profile_fd != -1) {
10976a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        sprintf(profile_arg, "--profile-file-fd=%d", profile_fd);
109860a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle    }
1099598c25e23f7c97470e09a2316513ddf2efdfb670Andreas Gampe
11006a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle
11011705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom    ALOGV("Running %s in=%s out=%s\n", DEX2OAT_BIN, input_file_name, output_file_name);
11024fdff4616699708e58d875273c3c1a85ba4c63bdCalin Juravle
110353caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray    const char* argv[9  // program name, mandatory arguments and the final NULL
110414e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae                     + (have_dex2oat_isa_variant ? 1 : 0)
110514e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae                     + (have_dex2oat_isa_features ? 1 : 0)
110614e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae                     + (have_dex2oat_Xms_flag ? 2 : 0)
110714e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae                     + (have_dex2oat_Xmx_flag ? 2 : 0)
110814e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae                     + (have_dex2oat_compiler_filter_flag ? 1 : 0)
11098d7af8b2418cc5e7e59746f0cb359a75ed0bdfd1Andreas Gampe                     + (have_dex2oat_threads_flag ? 1 : 0)
111014e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae                     + (have_dex2oat_swap_fd ? 1 : 0)
1111edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier                     + (have_dex2oat_image_fd ? 1 : 0)
111214e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae                     + (have_dex2oat_relocation_skip_flag ? 2 : 0)
1113528c8ddc60df7b3d291b389117e4db878a20ad48David Srbecky                     + (generate_debug_info ? 1 : 0)
1114598c25e23f7c97470e09a2316513ddf2efdfb670Andreas Gampe                     + (debuggable ? 1 : 0)
1115416fa12cd0949c7f12e2067b36ac7ac64ba70b4aMathieu Chartier                     + (have_app_image_format ? 1 : 0)
111660a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle                     + dex2oat_flags_count
1117b63d91fd2737680351876406277b6c759f4db33cJeff Hao                     + (profile_fd == -1 ? 0 : 1)
1118a5cc10a8e8d47fd01c117cc51ac1f6765a1735ceAndreas Gampe                     + (shared_libraries != nullptr ? 4 : 0)
1119a5cc10a8e8d47fd01c117cc51ac1f6765a1735ceAndreas Gampe                     + (have_dex2oat_large_app_threshold ? 1 : 0)];
11204fdff4616699708e58d875273c3c1a85ba4c63bdCalin Juravle    int i = 0;
112114e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae    argv[i++] = DEX2OAT_BIN;
11224fdff4616699708e58d875273c3c1a85ba4c63bdCalin Juravle    argv[i++] = zip_fd_arg;
11234fdff4616699708e58d875273c3c1a85ba4c63bdCalin Juravle    argv[i++] = zip_location_arg;
112453caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray    argv[i++] = input_vdex_fd_arg;
112553caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray    argv[i++] = output_vdex_fd_arg;
11264fdff4616699708e58d875273c3c1a85ba4c63bdCalin Juravle    argv[i++] = oat_fd_arg;
11274fdff4616699708e58d875273c3c1a85ba4c63bdCalin Juravle    argv[i++] = oat_location_arg;
11284fdff4616699708e58d875273c3c1a85ba4c63bdCalin Juravle    argv[i++] = instruction_set_arg;
112916a95b267ac6aac75453f65f1bc382ce65c0741eIan Rogers    if (have_dex2oat_isa_variant) {
113016a95b267ac6aac75453f65f1bc382ce65c0741eIan Rogers        argv[i++] = instruction_set_variant_arg;
113116a95b267ac6aac75453f65f1bc382ce65c0741eIan Rogers    }
11328fc7315a67fdbf9b56cc6fa061f0773be2223a81Calin Juravle    if (have_dex2oat_isa_features) {
11338fc7315a67fdbf9b56cc6fa061f0773be2223a81Calin Juravle        argv[i++] = instruction_set_features_arg;
11348fc7315a67fdbf9b56cc6fa061f0773be2223a81Calin Juravle    }
1135e46a75a0f6007967cd0d161959af844772cdc330Brian Carlstrom    if (have_dex2oat_Xms_flag) {
113614e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae        argv[i++] = RUNTIME_ARG;
1137e46a75a0f6007967cd0d161959af844772cdc330Brian Carlstrom        argv[i++] = dex2oat_Xms_arg;
1138e46a75a0f6007967cd0d161959af844772cdc330Brian Carlstrom    }
1139e46a75a0f6007967cd0d161959af844772cdc330Brian Carlstrom    if (have_dex2oat_Xmx_flag) {
114014e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae        argv[i++] = RUNTIME_ARG;
1141e46a75a0f6007967cd0d161959af844772cdc330Brian Carlstrom        argv[i++] = dex2oat_Xmx_arg;
1142e46a75a0f6007967cd0d161959af844772cdc330Brian Carlstrom    }
1143cf51ba1360ee13459830a0502b0d454d0145544fBrian Carlstrom    if (have_dex2oat_compiler_filter_flag) {
1144cf51ba1360ee13459830a0502b0d454d0145544fBrian Carlstrom        argv[i++] = dex2oat_compiler_filter_arg;
1145cf51ba1360ee13459830a0502b0d454d0145544fBrian Carlstrom    }
11468d7af8b2418cc5e7e59746f0cb359a75ed0bdfd1Andreas Gampe    if (have_dex2oat_threads_flag) {
11478d7af8b2418cc5e7e59746f0cb359a75ed0bdfd1Andreas Gampe        argv[i++] = dex2oat_threads_arg;
11488d7af8b2418cc5e7e59746f0cb359a75ed0bdfd1Andreas Gampe    }
1149e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe    if (have_dex2oat_swap_fd) {
1150e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe        argv[i++] = dex2oat_swap_fd;
1151e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe    }
1152edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier    if (have_dex2oat_image_fd) {
1153edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier        argv[i++] = dex2oat_image_fd;
1154edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier    }
1155528c8ddc60df7b3d291b389117e4db878a20ad48David Srbecky    if (generate_debug_info) {
1156528c8ddc60df7b3d291b389117e4db878a20ad48David Srbecky        argv[i++] = "--generate-debug-info";
11573822b8b79cf533ee8573794176838406c32a1c20Andreas Gampe    }
1158598c25e23f7c97470e09a2316513ddf2efdfb670Andreas Gampe    if (debuggable) {
1159598c25e23f7c97470e09a2316513ddf2efdfb670Andreas Gampe        argv[i++] = "--debuggable";
1160598c25e23f7c97470e09a2316513ddf2efdfb670Andreas Gampe    }
1161416fa12cd0949c7f12e2067b36ac7ac64ba70b4aMathieu Chartier    if (have_app_image_format) {
1162416fa12cd0949c7f12e2067b36ac7ac64ba70b4aMathieu Chartier        argv[i++] = image_format_arg;
1163416fa12cd0949c7f12e2067b36ac7ac64ba70b4aMathieu Chartier    }
1164a5cc10a8e8d47fd01c117cc51ac1f6765a1735ceAndreas Gampe    if (have_dex2oat_large_app_threshold) {
1165a5cc10a8e8d47fd01c117cc51ac1f6765a1735ceAndreas Gampe        argv[i++] = dex2oat_large_app_threshold_arg;
1166a5cc10a8e8d47fd01c117cc51ac1f6765a1735ceAndreas Gampe    }
1167b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban    if (dex2oat_flags_count) {
1168b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban        i += split(dex2oat_flags, argv + i);
11694fdff4616699708e58d875273c3c1a85ba4c63bdCalin Juravle    }
117014e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae    if (have_dex2oat_relocation_skip_flag) {
117114e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae        argv[i++] = RUNTIME_ARG;
117214e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae        argv[i++] = dex2oat_norelocation;
117314e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae    }
11746a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    if (profile_fd != -1) {
11756a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        argv[i++] = profile_arg;
117660a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle    }
1177b63d91fd2737680351876406277b6c759f4db33cJeff Hao    if (shared_libraries != nullptr) {
1178b63d91fd2737680351876406277b6c759f4db33cJeff Hao        argv[i++] = RUNTIME_ARG;
1179b63d91fd2737680351876406277b6c759f4db33cJeff Hao        argv[i++] = "-classpath";
1180b63d91fd2737680351876406277b6c759f4db33cJeff Hao        argv[i++] = RUNTIME_ARG;
1181b63d91fd2737680351876406277b6c759f4db33cJeff Hao        argv[i++] = shared_libraries;
1182b63d91fd2737680351876406277b6c759f4db33cJeff Hao    }
1183e46a75a0f6007967cd0d161959af844772cdc330Brian Carlstrom    // Do not add after dex2oat_flags, they should override others for debugging.
11844fdff4616699708e58d875273c3c1a85ba4c63bdCalin Juravle    argv[i] = NULL;
11854fdff4616699708e58d875273c3c1a85ba4c63bdCalin Juravle
118614e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae    execv(DEX2OAT_BIN, (char * const *)argv);
1187b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban    ALOGE("execv(%s) failed: %s\n", DEX2OAT_BIN, strerror(errno));
11881705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom}
11891705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom
1190e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe/*
1191c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe * Whether dexopt should use a swap file when compiling an APK.
1192c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe *
1193c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe * If kAlwaysProvideSwapFile, do this on all devices (dex2oat will make a more informed decision
1194c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe * itself, anyways).
1195c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe *
1196c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe * Otherwise, read "dalvik.vm.dex2oat-swap". If the property exists, return whether it is "true".
1197c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe *
1198c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe * Otherwise, return true if this is a low-mem device.
1199c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe *
1200c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe * Otherwise, return default value.
1201e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe */
1202c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampestatic bool kAlwaysProvideSwapFile = false;
1203c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampestatic bool kDefaultProvideSwapFile = true;
1204e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe
1205e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampestatic bool ShouldUseSwapFileForDexopt() {
1206e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe    if (kAlwaysProvideSwapFile) {
1207e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe        return true;
1208e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe    }
1209e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe
1210c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe    // Check the "override" property. If it exists, return value == "true".
121102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    char dex2oat_prop_buf[kPropertyValueMax];
121202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    if (get_property("dalvik.vm.dex2oat-swap", dex2oat_prop_buf, "") > 0) {
1213c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe        if (strcmp(dex2oat_prop_buf, "true") == 0) {
1214c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe            return true;
1215c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe        } else {
1216c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe            return false;
1217c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe        }
1218c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe    }
1219c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe
1220c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe    // Shortcut for default value. This is an implementation optimization for the process sketched
1221c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe    // above. If the default value is true, we can avoid to check whether this is a low-mem device,
1222c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe    // as low-mem is never returning false. The compiler will optimize this away if it can.
1223c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe    if (kDefaultProvideSwapFile) {
1224c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe        return true;
1225c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe    }
1226c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe
12276a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    bool is_low_mem = property_get_bool("ro.config.low_ram");
1228c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe    if (is_low_mem) {
1229c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe        return true;
1230c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe    }
1231c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe
1232c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe    // Default value must be false here.
1233c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe    return kDefaultProvideSwapFile;
1234e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe}
1235e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe
123694dd3d3c5be4de57278ee668218a3464585795c0Andreas Gampestatic void SetDex2OatAndPatchOatScheduling(bool set_to_bg) {
123794dd3d3c5be4de57278ee668218a3464585795c0Andreas Gampe    if (set_to_bg) {
123894dd3d3c5be4de57278ee668218a3464585795c0Andreas Gampe        if (set_sched_policy(0, SP_BACKGROUND) < 0) {
123994dd3d3c5be4de57278ee668218a3464585795c0Andreas Gampe            ALOGE("set_sched_policy failed: %s\n", strerror(errno));
124094dd3d3c5be4de57278ee668218a3464585795c0Andreas Gampe            exit(70);
124194dd3d3c5be4de57278ee668218a3464585795c0Andreas Gampe        }
124294dd3d3c5be4de57278ee668218a3464585795c0Andreas Gampe        if (setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND) < 0) {
124394dd3d3c5be4de57278ee668218a3464585795c0Andreas Gampe            ALOGE("setpriority failed: %s\n", strerror(errno));
124494dd3d3c5be4de57278ee668218a3464585795c0Andreas Gampe            exit(71);
124594dd3d3c5be4de57278ee668218a3464585795c0Andreas Gampe        }
124694dd3d3c5be4de57278ee668218a3464585795c0Andreas Gampe    }
124794dd3d3c5be4de57278ee668218a3464585795c0Andreas Gampe}
124894dd3d3c5be4de57278ee668218a3464585795c0Andreas Gampe
12496a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic void close_all_fds(const std::vector<fd_t>& fds, const char* description) {
125060a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle    for (size_t i = 0; i < fds.size(); i++) {
125160a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle        if (close(fds[i]) != 0) {
125260a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle            PLOG(WARNING) << "Failed to close fd for " << description << " at index " << i;
125360a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle        }
125460a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle    }
125560a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle}
125660a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle
12576a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic fd_t open_profile_dir(const std::string& profile_dir) {
12586a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    fd_t profile_dir_fd = TEMP_FAILURE_RETRY(open(profile_dir.c_str(),
12596a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle            O_PATH | O_CLOEXEC | O_DIRECTORY | O_NOFOLLOW));
12606a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    if (profile_dir_fd < 0) {
12612e063af1651976b50e2da58c4d57564d878ab9c2Narayan Kamath        // In a multi-user environment, these directories can be created at
12622e063af1651976b50e2da58c4d57564d878ab9c2Narayan Kamath        // different points and it's possible we'll attempt to open a profile
12632e063af1651976b50e2da58c4d57564d878ab9c2Narayan Kamath        // dir before it exists.
12642e063af1651976b50e2da58c4d57564d878ab9c2Narayan Kamath        if (errno != ENOENT) {
12652e063af1651976b50e2da58c4d57564d878ab9c2Narayan Kamath            PLOG(ERROR) << "Failed to open profile_dir: " << profile_dir;
12662e063af1651976b50e2da58c4d57564d878ab9c2Narayan Kamath        }
126760a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle    }
12686a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    return profile_dir_fd;
126960a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle}
127060a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle
12716a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic fd_t open_primary_profile_file_from_dir(const std::string& profile_dir, mode_t open_mode) {
12726a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    fd_t profile_dir_fd  = open_profile_dir(profile_dir);
12736a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    if (profile_dir_fd < 0) {
12746a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        return -1;
12756a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    }
12766a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle
12776a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    fd_t profile_fd = -1;
12786a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    std::string profile_file = create_primary_profile(profile_dir);
12796a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle
12806a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    profile_fd = TEMP_FAILURE_RETRY(open(profile_file.c_str(), open_mode | O_NOFOLLOW));
12816a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    if (profile_fd == -1) {
12826a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        // It's not an error if the profile file does not exist.
128360a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle        if (errno != ENOENT) {
12846a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle            PLOG(ERROR) << "Failed to lstat profile_dir: " << profile_dir;
128560a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle        }
128660a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle    }
12876a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    // TODO(calin): use AutoCloseFD instead of closing the fd manually.
12886a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    if (close(profile_dir_fd) != 0) {
12896a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        PLOG(WARNING) << "Could not close profile dir " << profile_dir;
12906a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    }
12916a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    return profile_fd;
12926a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle}
129360a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle
12946a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic fd_t open_primary_profile_file(userid_t user, const char* pkgname) {
12956a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    std::string profile_dir = create_data_user_profile_package_path(user, pkgname);
12966a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    return open_primary_profile_file_from_dir(profile_dir, O_RDONLY);
12976a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle}
12986a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle
12996a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic fd_t open_reference_profile(uid_t uid, const char* pkgname, bool read_write) {
13006a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    std::string reference_profile_dir = create_data_ref_profile_package_path(pkgname);
13016a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    int flags = read_write ? O_RDWR | O_CREAT : O_RDONLY;
13026a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    fd_t fd = open_primary_profile_file_from_dir(reference_profile_dir, flags);
13036a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    if (fd < 0) {
13046a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        return -1;
130560a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle    }
13066a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    if (read_write) {
13076a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        // Fix the owner.
13086a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        if (fchown(fd, uid, uid) < 0) {
13096a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle            close(fd);
13106a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle            return -1;
13116a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        }
13126a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    }
13136a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    return fd;
13146a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle}
131560a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle
13166a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic void open_profile_files(uid_t uid, const char* pkgname,
13176a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle            /*out*/ std::vector<fd_t>* profiles_fd, /*out*/ fd_t* reference_profile_fd) {
13186a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    // Open the reference profile in read-write mode as profman might need to save the merge.
13196a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    *reference_profile_fd = open_reference_profile(uid, pkgname, /*read_write*/ true);
132060a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle    if (*reference_profile_fd < 0) {
13216a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        // We can't access the reference profile file.
132260a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle        return;
132360a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle    }
13246a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle
13256a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    std::vector<userid_t> users = get_known_users(/*volume_uuid*/ nullptr);
13266a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    for (auto user : users) {
13276a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        fd_t profile_fd = open_primary_profile_file(user, pkgname);
13286a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        // Add to the lists only if both fds are valid.
13296a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        if (profile_fd >= 0) {
13306a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle            profiles_fd->push_back(profile_fd);
13316a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        }
13326a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    }
13336a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle}
13346a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle
13356a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic void drop_capabilities(uid_t uid) {
13366a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    if (setgid(uid) != 0) {
13376a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        ALOGE("setgid(%d) failed in installd during dexopt\n", uid);
13386a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        exit(64);
13396a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    }
13406a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    if (setuid(uid) != 0) {
13416a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        ALOGE("setuid(%d) failed in installd during dexopt\n", uid);
13426a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        exit(65);
13436a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    }
13446a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    // drop capabilities
13456a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    struct __user_cap_header_struct capheader;
13466a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    struct __user_cap_data_struct capdata[2];
13476a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    memset(&capheader, 0, sizeof(capheader));
13486a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    memset(&capdata, 0, sizeof(capdata));
13496a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    capheader.version = _LINUX_CAPABILITY_VERSION_3;
13506a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    if (capset(&capheader, &capdata[0]) < 0) {
13516a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        ALOGE("capset failed: %s\n", strerror(errno));
13526a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        exit(66);
135360a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle    }
135460a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle}
135560a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle
13566a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic constexpr int PROFMAN_BIN_RETURN_CODE_COMPILE = 0;
13576a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic constexpr int PROFMAN_BIN_RETURN_CODE_SKIP_COMPILATION = 1;
13586a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic constexpr int PROFMAN_BIN_RETURN_CODE_BAD_PROFILES = 2;
13596a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic constexpr int PROFMAN_BIN_RETURN_CODE_ERROR_IO = 3;
13606a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic constexpr int PROFMAN_BIN_RETURN_CODE_ERROR_LOCKING = 4;
13616a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle
13626727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehrstatic void run_profman_merge(const std::vector<fd_t>& profiles_fd, fd_t reference_profile_fd) {
13636a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    static const size_t MAX_INT_LEN = 32;
13646a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    static const char* PROFMAN_BIN = "/system/bin/profman";
13656a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle
13666a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    std::vector<std::string> profile_args(profiles_fd.size());
13676a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    char profile_buf[strlen("--profile-file-fd=") + MAX_INT_LEN];
13686a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    for (size_t k = 0; k < profiles_fd.size(); k++) {
13696a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        sprintf(profile_buf, "--profile-file-fd=%d", profiles_fd[k]);
13706a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        profile_args[k].assign(profile_buf);
13716a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    }
13726a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    char reference_profile_arg[strlen("--reference-profile-file-fd=") + MAX_INT_LEN];
13736a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    sprintf(reference_profile_arg, "--reference-profile-file-fd=%d", reference_profile_fd);
13746a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle
13756a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    // program name, reference profile fd, the final NULL and the profile fds
13766a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    const char* argv[3 + profiles_fd.size()];
13776a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    int i = 0;
13786a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    argv[i++] = PROFMAN_BIN;
13796a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    argv[i++] = reference_profile_arg;
13806a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    for (size_t k = 0; k < profile_args.size(); k++) {
13816a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        argv[i++] = profile_args[k].c_str();
13826a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    }
13836a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    // Do not add after dex2oat_flags, they should override others for debugging.
13846a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    argv[i] = NULL;
13856a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle
13866a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    execv(PROFMAN_BIN, (char * const *)argv);
13876a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    ALOGE("execv(%s) failed: %s\n", PROFMAN_BIN, strerror(errno));
13886a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    exit(68);   /* only get here on exec failure */
13896a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle}
13906a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle
13916a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle// Decides if profile guided compilation is needed or not based on existing profiles.
13926a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle// Returns true if there is enough information in the current profiles that worth
13936a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle// a re-compilation of the package.
13946a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle// If the return value is true all the current profiles would have been merged into
13956a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle// the reference profiles accessible with open_reference_profile().
13966a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic bool analyse_profiles(uid_t uid, const char* pkgname) {
13976a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    std::vector<fd_t> profiles_fd;
13986a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    fd_t reference_profile_fd = -1;
13996a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    open_profile_files(uid, pkgname, &profiles_fd, &reference_profile_fd);
14006a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    if (profiles_fd.empty() || (reference_profile_fd == -1)) {
14016a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        // Skip profile guided compilation because no profiles were found.
14026a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        // Or if the reference profile info couldn't be opened.
14036a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        close_all_fds(profiles_fd, "profiles_fd");
14046a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        if ((reference_profile_fd != - 1) && (close(reference_profile_fd) != 0)) {
14056a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle            PLOG(WARNING) << "Failed to close fd for reference profile";
140660a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle        }
14076a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        return false;
14086a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    }
140960a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle
14106727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr    ALOGV("PROFMAN (MERGE): --- BEGIN '%s' ---\n", pkgname);
14116a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle
14126a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    pid_t pid = fork();
14136a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    if (pid == 0) {
14146a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        /* child -- drop privileges before continuing */
14156a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        drop_capabilities(uid);
14166727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr        run_profman_merge(profiles_fd, reference_profile_fd);
14176a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        exit(68);   /* only get here on exec failure */
14186a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    }
14196a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    /* parent */
14206a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    int return_code = wait_child(pid);
14216a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    bool need_to_compile = false;
1422cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle    bool should_clear_current_profiles = false;
1423cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle    bool should_clear_reference_profile = false;
14246a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    if (!WIFEXITED(return_code)) {
14256a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        LOG(WARNING) << "profman failed for package " << pkgname << ": " << return_code;
14266a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    } else {
14276a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        return_code = WEXITSTATUS(return_code);
14286a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        switch (return_code) {
14296a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle            case PROFMAN_BIN_RETURN_CODE_COMPILE:
14306a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle                need_to_compile = true;
1431cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle                should_clear_current_profiles = true;
1432cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle                should_clear_reference_profile = false;
14336a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle                break;
14346a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle            case PROFMAN_BIN_RETURN_CODE_SKIP_COMPILATION:
14356a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle                need_to_compile = false;
1436cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle                should_clear_current_profiles = false;
1437cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle                should_clear_reference_profile = false;
14386a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle                break;
14396a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle            case PROFMAN_BIN_RETURN_CODE_BAD_PROFILES:
14406a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle                LOG(WARNING) << "Bad profiles for package " << pkgname;
14416a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle                need_to_compile = false;
1442cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle                should_clear_current_profiles = true;
1443cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle                should_clear_reference_profile = true;
14446a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle                break;
14456a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle            case PROFMAN_BIN_RETURN_CODE_ERROR_IO:  // fall-through
14466a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle            case PROFMAN_BIN_RETURN_CODE_ERROR_LOCKING:
14476a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle                // Temporary IO problem (e.g. locking). Ignore but log a warning.
14486a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle                LOG(WARNING) << "IO error while reading profiles for package " << pkgname;
14496a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle                need_to_compile = false;
1450cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle                should_clear_current_profiles = false;
1451cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle                should_clear_reference_profile = false;
14526a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle                break;
14536a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle           default:
14546a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle                // Unknown return code or error. Unlink profiles.
14556a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle                LOG(WARNING) << "Unknown error code while processing profiles for package " << pkgname
14566a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle                        << ": " << return_code;
14576a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle                need_to_compile = false;
1458cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle                should_clear_current_profiles = true;
1459cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle                should_clear_reference_profile = true;
14606a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle                break;
146160a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle        }
146260a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle    }
14636a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    close_all_fds(profiles_fd, "profiles_fd");
14646a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    if (close(reference_profile_fd) != 0) {
14656a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        PLOG(WARNING) << "Failed to close fd for reference profile";
14666a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    }
1467cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle    if (should_clear_current_profiles) {
1468cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle        clear_current_profiles(pkgname);
14696a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    }
1470cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle    if (should_clear_reference_profile) {
1471cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle        clear_reference_profile(pkgname);
14726a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    }
14736a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    return need_to_compile;
147460a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle}
147560a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle
14766727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehrstatic void run_profman_dump(const std::vector<fd_t>& profile_fds,
14776727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr                             fd_t reference_profile_fd,
147840e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr                             const std::vector<std::string>& dex_locations,
147940e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr                             const std::vector<fd_t>& apk_fds,
14806727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr                             fd_t output_fd) {
148140e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr    std::vector<std::string> profman_args;
14826727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr    static const char* PROFMAN_BIN = "/system/bin/profman";
148340e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr    profman_args.push_back(PROFMAN_BIN);
148440e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr    profman_args.push_back("--dump-only");
148540e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr    profman_args.push_back(StringPrintf("--dump-output-to-fd=%d", output_fd));
148640e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr    if (reference_profile_fd != -1) {
148740e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr        profman_args.push_back(StringPrintf("--reference-profile-file-fd=%d",
148840e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr                                            reference_profile_fd));
14896727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr    }
14906727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr    for (fd_t profile_fd : profile_fds) {
149140e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr        profman_args.push_back(StringPrintf("--profile-file-fd=%d", profile_fd));
14926727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr    }
149340e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr    for (const std::string& dex_location : dex_locations) {
149440e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr        profman_args.push_back(StringPrintf("--dex-location=%s", dex_location.c_str()));
14956727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr    }
149640e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr    for (fd_t apk_fd : apk_fds) {
149740e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr        profman_args.push_back(StringPrintf("--apk-fd=%d", apk_fd));
149840e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr    }
149940e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr    const char **argv = new const char*[profman_args.size() + 1];
150040e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr    size_t i = 0;
150140e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr    for (const std::string& profman_arg : profman_args) {
150240e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr        argv[i++] = profman_arg.c_str();
15036727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr    }
15046727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr    argv[i] = NULL;
15056727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr
15066727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr    execv(PROFMAN_BIN, (char * const *)argv);
15076727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr    ALOGE("execv(%s) failed: %s\n", PROFMAN_BIN, strerror(errno));
15086727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr    exit(68);   /* only get here on exec failure */
15096727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr}
15106727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr
151140e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehrstatic const char* get_location_from_path(const char* path) {
151240e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr    static constexpr char kLocationSeparator = '/';
151340e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr    const char *location = strrchr(path, kLocationSeparator);
151440e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr    if (location == NULL) {
151540e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr        return path;
151640e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr    } else {
151740e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr        // Skip the separator character.
151840e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr        return location + 1;
151940e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr    }
152040e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr}
152140e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr
15226727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr// Dumps the contents of a profile file, using pkgname's dex files for pretty
15236727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr// printing the result.
1524475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::dumpProfiles(int32_t uid, const std::string& packageName,
1525475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey        const std::string& codePaths, bool* _aidl_return) {
1526475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    ENFORCE_UID(AID_SYSTEM);
1527475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey
1528475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    const char* pkgname = packageName.c_str();
1529475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    const char* code_path_string = codePaths.c_str();
1530475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey
15316727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr    std::vector<fd_t> profile_fds;
15326727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr    fd_t reference_profile_fd = -1;
15336727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr    std::string out_file_name = StringPrintf("/data/misc/profman/%s.txt", pkgname);
15346727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr
15356727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr    ALOGV("PROFMAN (DUMP): --- BEGIN '%s' ---\n", pkgname);
15366727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr
15376727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr    open_profile_files(uid, pkgname, &profile_fds, &reference_profile_fd);
15386727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr
15396727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr    const bool has_reference_profile = (reference_profile_fd != -1);
15406727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr    const bool has_profiles = !profile_fds.empty();
15416727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr
15426727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr    if (!has_reference_profile && !has_profiles) {
15436727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr        ALOGE("profman dump: no profiles to dump for '%s'", pkgname);
1544475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey        *_aidl_return = false;
1545475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey        return binder::Status::ok();
15466727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr    }
15476727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr
154840e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr    fd_t output_fd = open(out_file_name.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_NOFOLLOW);
15496727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr    if (fchmod(output_fd, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) < 0) {
15506727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr        ALOGE("installd cannot chmod '%s' dump_profile\n", out_file_name.c_str());
1551475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey        *_aidl_return = false;
1552475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey        return binder::Status::ok();
15536727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr    }
155440e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr    std::vector<std::string> code_full_paths = base::Split(code_path_string, ";");
155540e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr    std::vector<std::string> dex_locations;
155640e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr    std::vector<fd_t> apk_fds;
155740e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr    for (const std::string& code_full_path : code_full_paths) {
155840e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr        const char* full_path = code_full_path.c_str();
155940e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr        fd_t apk_fd = open(full_path, O_RDONLY | O_NOFOLLOW);
156040e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr        if (apk_fd == -1) {
156140e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr            ALOGE("installd cannot open '%s'\n", full_path);
1562475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey            *_aidl_return = false;
1563475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey            return binder::Status::ok();
15646727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr        }
156540e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr        dex_locations.push_back(get_location_from_path(full_path));
156640e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr        apk_fds.push_back(apk_fd);
15676727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr    }
15686727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr
15696727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr    pid_t pid = fork();
15706727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr    if (pid == 0) {
15716727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr        /* child -- drop privileges before continuing */
15726727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr        drop_capabilities(uid);
157340e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr        run_profman_dump(profile_fds, reference_profile_fd, dex_locations,
157440e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr                         apk_fds, output_fd);
15756727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr        exit(68);   /* only get here on exec failure */
15766727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr    }
15776727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr    /* parent */
157840e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr    close_all_fds(apk_fds, "apk_fds");
15796727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr    close_all_fds(profile_fds, "profile_fds");
15806727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr    if (close(reference_profile_fd) != 0) {
15816727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr        PLOG(WARNING) << "Failed to close fd for reference profile";
15826727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr    }
15836727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr    int return_code = wait_child(pid);
15846727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr    if (!WIFEXITED(return_code)) {
15856727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr        LOG(WARNING) << "profman failed for package " << pkgname << ": "
15866727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr                << return_code;
1587475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey        *_aidl_return = false;
1588475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey        return binder::Status::ok();
15896727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr    }
1590475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    *_aidl_return = true;
1591475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    return binder::Status::ok();
15926727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr}
15936727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr
1594fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdilstatic std::string replace_file_extension(const std::string& oat_path, const std::string& new_ext) {
1595fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil  // A standard dalvik-cache entry. Replace ".dex" with `new_ext`.
159689b008dfb59ac827db00a45e7de1bac1c65b9c93Andreas Gampe  if (EndsWith(oat_path, ".dex")) {
1597fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil    std::string new_path = oat_path;
1598fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil    new_path.replace(new_path.length() - strlen(".dex"), strlen(".dex"), new_ext);
1599fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil    CHECK(EndsWith(new_path, new_ext.c_str()));
1600fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil    return new_path;
1601edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier  }
160289b008dfb59ac827db00a45e7de1bac1c65b9c93Andreas Gampe
160389b008dfb59ac827db00a45e7de1bac1c65b9c93Andreas Gampe  // An odex entry. Not that this may not be an extension, e.g., in the OTA
160489b008dfb59ac827db00a45e7de1bac1c65b9c93Andreas Gampe  // case (where the base name will have an extension for the B artifact).
160589b008dfb59ac827db00a45e7de1bac1c65b9c93Andreas Gampe  size_t odex_pos = oat_path.rfind(".odex");
160689b008dfb59ac827db00a45e7de1bac1c65b9c93Andreas Gampe  if (odex_pos != std::string::npos) {
1607fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil    std::string new_path = oat_path;
1608fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil    new_path.replace(odex_pos, strlen(".odex"), new_ext);
1609fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil    CHECK_NE(new_path.find(new_ext), std::string::npos);
1610fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil    return new_path;
161189b008dfb59ac827db00a45e7de1bac1c65b9c93Andreas Gampe  }
161289b008dfb59ac827db00a45e7de1bac1c65b9c93Andreas Gampe
161389b008dfb59ac827db00a45e7de1bac1c65b9c93Andreas Gampe  // Don't know how to handle this.
161489b008dfb59ac827db00a45e7de1bac1c65b9c93Andreas Gampe  return "";
1615edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier}
1616edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier
1617fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil// Translate the given oat path to an art (app image) path. An empty string
1618fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil// denotes an error.
1619fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdilstatic std::string create_image_filename(const std::string& oat_path) {
1620fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil    return replace_file_extension(oat_path, ".art");
1621fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil}
1622fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil
1623fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil// Translate the given oat path to a vdex path. An empty string denotes an error.
1624fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdilstatic std::string create_vdex_filename(const std::string& oat_path) {
1625fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil    return replace_file_extension(oat_path, ".vdex");
1626fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil}
1627fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil
162841fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartierstatic bool add_extension_to_file_name(char* file_name, const char* extension) {
162941fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier    if (strlen(file_name) + strlen(extension) + 1 > PKG_PATH_MAX) {
163041fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier        return false;
1631edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier    }
163241fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier    strcat(file_name, extension);
163341fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier    return true;
163441fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier}
163541fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier
16366fb5a0179d4de9c85351fd016277949804c809edAndreas Gampestatic int open_output_file(const char* file_name, bool recreate, int permissions) {
163741fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier    int flags = O_RDWR | O_CREAT;
163841fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier    if (recreate) {
16395006f3a6bcfbe73cfae96b3f2659ce42e18bc2eaNarayan Kamath        if (unlink(file_name) < 0) {
16405006f3a6bcfbe73cfae96b3f2659ce42e18bc2eaNarayan Kamath            if (errno != ENOENT) {
16415006f3a6bcfbe73cfae96b3f2659ce42e18bc2eaNarayan Kamath                PLOG(ERROR) << "open_output_file: Couldn't unlink " << file_name;
16425006f3a6bcfbe73cfae96b3f2659ce42e18bc2eaNarayan Kamath            }
16435006f3a6bcfbe73cfae96b3f2659ce42e18bc2eaNarayan Kamath        }
164441fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier        flags |= O_EXCL;
164541fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier    }
16465006f3a6bcfbe73cfae96b3f2659ce42e18bc2eaNarayan Kamath    return open(file_name, flags, permissions);
1647edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier}
1648edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier
1649edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartierstatic bool set_permissions_and_ownership(int fd, bool is_public, int uid, const char* path) {
1650edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier    if (fchmod(fd,
1651edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier               S_IRUSR|S_IWUSR|S_IRGRP |
1652edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier               (is_public ? S_IROTH : 0)) < 0) {
1653edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier        ALOGE("installd cannot chmod '%s' during dexopt\n", path);
1654edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier        return false;
1655edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier    } else if (fchown(fd, AID_SYSTEM, uid) < 0) {
1656edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier        ALOGE("installd cannot chown '%s' during dexopt\n", path);
1657edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier        return false;
1658edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier    }
1659edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier    return true;
1660edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier}
1661edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier
166253caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffraystatic bool IsOutputDalvikCache(const char* oat_dir) {
166353caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray  // InstallerConnection.java (which invokes installd) transforms Java null arguments
166453caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray  // into '!'. Play it safe by handling it both.
166553caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray  // TODO: ensure we never get null.
166653caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray  // TODO: pass a flag instead of inferring if the output is dalvik cache.
166753caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray  return oat_dir == nullptr || oat_dir[0] == '!';
166853caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray}
166953caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray
16706a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic bool create_oat_out_path(const char* apk_path, const char* instruction_set,
1671fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil            const char* oat_dir, /*out*/ char* out_oat_path) {
16726a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    // Early best-effort check whether we can fit the the path into our buffers.
16736a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    // Note: the cache path will require an additional 5 bytes for ".swap", but we'll try to run
16746a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    // without a swap file, if necessary. Reference profiles file also add an extra ".prof"
16756a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    // extension to the cache path (5 bytes).
16766a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    if (strlen(apk_path) >= (PKG_PATH_MAX - 8)) {
16776a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        ALOGE("apk_path too long '%s'\n", apk_path);
16786a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        return false;
16796a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    }
16806a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle
168153caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray    if (!IsOutputDalvikCache(oat_dir)) {
16826a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        if (validate_apk_path(oat_dir)) {
168353caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray            ALOGE("cannot validate apk path with oat_dir '%s'\n", oat_dir);
16846a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle            return false;
16856a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        }
1686fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil        if (!calculate_oat_file_path(out_oat_path, oat_dir, apk_path, instruction_set)) {
16876a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle            return false;
16886a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        }
16896a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    } else {
1690fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil        if (!create_cache_path(out_oat_path, apk_path, instruction_set)) {
16916a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle            return false;
16926a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        }
16936a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    }
16946a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    return true;
16956a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle}
16966a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle
16974d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe// TODO: Consider returning error codes.
1698475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::mergeProfiles(int32_t uid, const std::string& packageName,
1699475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    bool* _aidl_return) {
1700475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    ENFORCE_UID(AID_SYSTEM);
1701475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    const char* pkgname = packageName.c_str();
1702475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    *_aidl_return = analyse_profiles(uid, pkgname);
1703475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    return binder::Status::ok();
17044d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe}
17054d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe
17066fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe// Helper for fd management. This is similar to a unique_fd in that it closes the file descriptor
17076fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe// on destruction. It will also run the given cleanup (unless told not to) after closing.
17086fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe//
17096fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe// Usage example:
17106fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe//
17116fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe//   Dex2oatFileWrapper<std::function<void ()>> file(open(...),
17126fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe//                                                   [name]() {
17136fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe//                                                       unlink(name.c_str());
17146fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe//                                                   });
17156fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe//   // Note: care needs to be taken about name, as it needs to have a lifetime longer than the
17166fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe//            wrapper if captured as a reference.
17176fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe//
17186fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe//   if (file.get() == -1) {
17196fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe//       // Error opening...
17206fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe//   }
17216fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe//
17226fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe//   ...
17236fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe//   if (error) {
17246fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe//       // At this point, when the Dex2oatFileWrapper is destructed, the cleanup function will run
17256fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe//       // and delete the file (after the fd is closed).
17266fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe//       return -1;
17276fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe//   }
17286fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe//
17296fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe//   (Success case)
17306fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe//   file.SetCleanup(false);
17316fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe//   // At this point, when the Dex2oatFileWrapper is destructed, the cleanup function will not run
17326fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe//   // (leaving the file around; after the fd is closed).
17336fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe//
17346fb5a0179d4de9c85351fd016277949804c809edAndreas Gampetemplate <typename Cleanup>
17356fb5a0179d4de9c85351fd016277949804c809edAndreas Gampeclass Dex2oatFileWrapper {
17366fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe public:
17376fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe    Dex2oatFileWrapper() : value_(-1), cleanup_(), do_cleanup_(true) {
17386fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe    }
17396fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe
17406fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe    Dex2oatFileWrapper(int value, Cleanup cleanup)
17416fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe            : value_(value), cleanup_(cleanup), do_cleanup_(true) {}
17426fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe
17436fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe    ~Dex2oatFileWrapper() {
17446fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe        reset(-1);
17456fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe    }
17466fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe
17476fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe    int get() {
17486fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe        return value_;
17496fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe    }
17506fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe
17516fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe    void SetCleanup(bool cleanup) {
17526fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe        do_cleanup_ = cleanup;
17536fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe    }
17546fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe
17556fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe    void reset(int new_value) {
17566fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe        if (value_ >= 0) {
17576fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe            close(value_);
17586fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe        }
17596fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe        if (do_cleanup_ && cleanup_ != nullptr) {
17606fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe            cleanup_();
17616fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe        }
17626fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe
17636fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe        value_ = new_value;
17646fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe    }
17656fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe
17666fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe    void reset(int new_value, Cleanup new_cleanup) {
17676fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe        if (value_ >= 0) {
17686fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe            close(value_);
17696fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe        }
17706fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe        if (do_cleanup_ && cleanup_ != nullptr) {
17716fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe            cleanup_();
17726fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe        }
17736fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe
17746fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe        value_ = new_value;
17756fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe        cleanup_ = new_cleanup;
17766fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe    }
17776fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe
17786fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe private:
17796fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe    int value_;
17806fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe    Cleanup cleanup_;
17816fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe    bool do_cleanup_;
17826fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe};
17836fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe
17846c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey// TODO: eventually move dexopt() implementation into dexopt.cpp
178560a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravleint dexopt(const char* apk_path, uid_t uid, const char* pkgname, const char* instruction_set,
17866c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey        int dexopt_needed, const char* oat_dir, int dexopt_flags,const char* compiler_filter,
17876c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey        const char* volume_uuid ATTRIBUTE_UNUSED, const char* shared_libraries) {
1788db0326a56ed8cc1ba2ed5ec2810afe49bcd9a596Calin Juravle    bool is_public = ((dexopt_flags & DEXOPT_PUBLIC) != 0);
178976e767ca14bcbb4bc809cd1279ece82a3aabe8a4Todd Kennedy    bool vm_safe_mode = (dexopt_flags & DEXOPT_SAFEMODE) != 0;
179076e767ca14bcbb4bc809cd1279ece82a3aabe8a4Todd Kennedy    bool debuggable = (dexopt_flags & DEXOPT_DEBUGGABLE) != 0;
179176e767ca14bcbb4bc809cd1279ece82a3aabe8a4Todd Kennedy    bool boot_complete = (dexopt_flags & DEXOPT_BOOTCOMPLETE) != 0;
17924d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe    bool profile_guided = (dexopt_flags & DEXOPT_PROFILE_GUIDED) != 0;
1793db0326a56ed8cc1ba2ed5ec2810afe49bcd9a596Calin Juravle
17944d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe    CHECK(pkgname != nullptr);
17954d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe    CHECK(pkgname[0] != 0);
1796db0326a56ed8cc1ba2ed5ec2810afe49bcd9a596Calin Juravle
17974d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe    // Public apps should not be compiled with profile information ever. Same goes for the special
17984d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe    // package '*' used for the system server.
17996fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe    Dex2oatFileWrapper<std::function<void ()>> reference_profile_fd;
18004d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe    if (!is_public && pkgname[0] != '*') {
18014d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe        // Open reference profile in read only mode as dex2oat does not get write permissions.
18026fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe        const std::string pkgname_str(pkgname);
18036fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe        reference_profile_fd.reset(open_reference_profile(uid, pkgname, /*read_write*/ false),
18046fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe                                   [pkgname_str]() {
18056fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe                                       clear_reference_profile(pkgname_str.c_str());
18066fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe                                   });
18074d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe        // Note: it's OK to not find a profile here.
180860a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle    }
180976e767ca14bcbb4bc809cd1279ece82a3aabe8a4Todd Kennedy
1810e296e00df65461629a20cd03b5b041a59935d00fTodd Kennedy    if ((dexopt_flags & ~DEXOPT_MASK) != 0) {
181176e767ca14bcbb4bc809cd1279ece82a3aabe8a4Todd Kennedy        LOG_FATAL("dexopt flags contains unknown fields\n");
181276e767ca14bcbb4bc809cd1279ece82a3aabe8a4Todd Kennedy    }
181394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
1814fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil    char out_oat_path[PKG_PATH_MAX];
1815fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil    if (!create_oat_out_path(apk_path, instruction_set, oat_dir, out_oat_path)) {
18166a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        return false;
181794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
181894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
18196fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe    const char *input_file;
18206fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe    char in_odex_path[PKG_PATH_MAX];
1821920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray    int dexopt_action = abs(dexopt_needed);
1822920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray    bool is_odex_location = dexopt_needed < 0;
1823920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray    switch (dexopt_action) {
1824920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray        case DEX2OAT_FROM_SCRATCH:
1825920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray        case DEX2OAT_FOR_BOOT_IMAGE:
1826920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray        case DEX2OAT_FOR_FILTER:
1827920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray        case DEX2OAT_FOR_RELOCATION:
1828c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler            input_file = apk_path;
1829c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler            break;
1830c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler
1831920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray        case PATCHOAT_FOR_RELOCATION:
1832920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray            if (is_odex_location) {
1833920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray                if (!calculate_odex_file_path(in_odex_path, apk_path, instruction_set)) {
1834920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray                    return -1;
1835920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray                }
1836920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray                input_file = in_odex_path;
1837920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray            } else {
1838920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray                input_file = out_oat_path;
1839c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler            }
1840c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler            break;
1841c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler
1842c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler        default:
1843c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler            ALOGE("Invalid dexopt needed: %d\n", dexopt_needed);
18446fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe            return 72;
18457365a10689df23334d245b211ce272502ad20669Alex Light    }
18467365a10689df23334d245b211ce272502ad20669Alex Light
18476fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe    struct stat input_stat;
18487365a10689df23334d245b211ce272502ad20669Alex Light    memset(&input_stat, 0, sizeof(input_stat));
18497365a10689df23334d245b211ce272502ad20669Alex Light    stat(input_file, &input_stat);
185094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
1851fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil    // Open the input file. If running dex2oat, `input_file` is the APK. If running
1852fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil    // patchoat, it is the OAT file to be relocated.
18536fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe    base::unique_fd input_fd(open(input_file, O_RDONLY, 0));
18546fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe    if (input_fd.get() < 0) {
18557365a10689df23334d245b211ce272502ad20669Alex Light        ALOGE("installd cannot open '%s' for input during dexopt\n", input_file);
185694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
185794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
185894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
1859fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil    // Create the output OAT file.
1860fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil    const std::string out_oat_path_str(out_oat_path);
1861fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil    Dex2oatFileWrapper<std::function<void ()>> out_oat_fd(
1862fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil            open_output_file(out_oat_path, /*recreate*/true, /*permissions*/0644),
1863fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil            [out_oat_path_str]() { unlink(out_oat_path_str.c_str()); });
1864fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil    if (out_oat_fd.get() < 0) {
1865fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil        ALOGE("installd cannot open '%s' for output during dexopt\n", out_oat_path);
1866fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil        return -1;
1867fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil    }
1868fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil    if (!set_permissions_and_ownership(out_oat_fd.get(), is_public, uid, out_oat_path)) {
1869fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil        return -1;
1870fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil    }
1871fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil
187253caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray    // Open the existing VDEX. We do this before creating the new output VDEX, which will
187353caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray    // unlink the old one.
187453caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray    base::unique_fd in_vdex_fd;
187553caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray    std::string in_vdex_path_str;
1876920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray    if (dexopt_action == PATCHOAT_FOR_RELOCATION) {
187753caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray        // `input_file` is the OAT file to be relocated. The VDEX has to be there as well.
187853caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray        in_vdex_path_str = create_vdex_filename(input_file);
187953caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray        if (in_vdex_path_str.empty()) {
188053caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray            ALOGE("installd cannot compute input vdex location for '%s'\n", input_file);
188153caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray            return -1;
188253caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray        }
188353caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray        in_vdex_fd.reset(open(in_vdex_path_str.c_str(), O_RDONLY, 0));
188453caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray        if (in_vdex_fd.get() < 0) {
188553caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray            ALOGE("installd cannot open '%s' for input during dexopt: %s\n",
188653caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray                in_vdex_path_str.c_str(), strerror(errno));
188753caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray            return -1;
188853caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray        }
1889920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray    } else if (dexopt_action != DEX2OAT_FROM_SCRATCH) {
1890920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray        // Open the possibly existing vdex. If none exist, we pass -1 to dex2oat for input-vdex-fd.
1891920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray        const char* path = nullptr;
1892920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray        if (is_odex_location) {
1893920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray            if (calculate_odex_file_path(in_odex_path, apk_path, instruction_set)) {
1894920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray                path = in_odex_path;
1895920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray            } else {
1896920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray                ALOGE("installd cannot compute input vdex location for '%s'\n", apk_path);
1897920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray                return -1;
1898920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray            }
1899920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray        } else {
1900920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray            path = out_oat_path;
1901920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray        }
1902920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray        in_vdex_path_str = create_vdex_filename(path);
190353caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray        if (in_vdex_path_str.empty()) {
1904920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray            ALOGE("installd cannot compute input vdex location for '%s'\n", path);
190553caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray            return -1;
190653caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray        }
190753caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray        in_vdex_fd.reset(open(in_vdex_path_str.c_str(), O_RDONLY, 0));
190853caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray    }
190953caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray
1910fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil    // Infer the name of the output VDEX and create it.
1911fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil    const std::string out_vdex_path_str = create_vdex_filename(out_oat_path_str);
1912fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil    if (out_vdex_path_str.empty()) {
1913fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil        return -1;
1914fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil    }
1915fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil    Dex2oatFileWrapper<std::function<void ()>> out_vdex_fd(
1916fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil            open_output_file(out_vdex_path_str.c_str(), /*recreate*/true, /*permissions*/0644),
1917fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil            [out_vdex_path_str]() { unlink(out_vdex_path_str.c_str()); });
1918fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil    if (out_vdex_fd.get() < 0) {
1919fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil        ALOGE("installd cannot open '%s' for output during dexopt\n", out_vdex_path_str.c_str());
19206fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe        return -1;
192194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
1922fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil    if (!set_permissions_and_ownership(out_vdex_fd.get(), is_public,
1923fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil                uid, out_vdex_path_str.c_str())) {
19246fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe        return -1;
192594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
192694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
1927e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe    // Create a swap file if necessary.
19286fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe    base::unique_fd swap_fd;
1929c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler    if (ShouldUseSwapFileForDexopt()) {
1930e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe        // Make sure there really is enough space.
19316fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe        char swap_file_name[PKG_PATH_MAX];
1932fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil        strcpy(swap_file_name, out_oat_path);
193341fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier        if (add_extension_to_file_name(swap_file_name, ".swap")) {
19346fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe            swap_fd.reset(open_output_file(swap_file_name, /*recreate*/true, /*permissions*/0600));
193541fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier        }
19366fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe        if (swap_fd.get() < 0) {
1937edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier            // Could not create swap file. Optimistically go on and hope that we can compile
1938edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier            // without it.
1939edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier            ALOGE("installd could not create '%s' for swap during dexopt\n", swap_file_name);
1940e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe        } else {
1941edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier            // Immediately unlink. We don't really want to hit flash.
19425006f3a6bcfbe73cfae96b3f2659ce42e18bc2eaNarayan Kamath            if (unlink(swap_file_name) < 0) {
19435006f3a6bcfbe73cfae96b3f2659ce42e18bc2eaNarayan Kamath                PLOG(ERROR) << "Couldn't unlink swap file " << swap_file_name;
19445006f3a6bcfbe73cfae96b3f2659ce42e18bc2eaNarayan Kamath            }
1945e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe        }
1946e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe    }
1947d93707342a61e66bc3eb2145628158452f577f42Dave Allison
1948e80d58db65d29ed4647e631a81aed644fea00913Mathieu Chartier    // Avoid generating an app image for extract only since it will not contain any classes.
19496fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe    Dex2oatFileWrapper<std::function<void ()>> image_fd;
1950fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil    const std::string image_path = create_image_filename(out_oat_path);
1951920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray    if (dexopt_action != PATCHOAT_FOR_RELOCATION && !image_path.empty()) {
19526fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe        char app_image_format[kPropertyValueMax];
19536fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe        bool have_app_image_format =
19546fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe                get_property("dalvik.vm.appimageformat", app_image_format, NULL) > 0;
19556fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe        // Use app images only if it is enabled (by a set image format) and we are compiling
19566fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe        // profile-guided (so the app image doesn't conservatively contain all classes).
19576fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe        if (profile_guided && have_app_image_format) {
19586fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe            // Recreate is true since we do not want to modify a mapped image. If the app is
19596fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe            // already running and we modify the image file, it can cause crashes (b/27493510).
196089b008dfb59ac827db00a45e7de1bac1c65b9c93Andreas Gampe            image_fd.reset(open_output_file(image_path.c_str(),
19616fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe                                            true /*recreate*/,
19626fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe                                            0600 /*permissions*/),
196389b008dfb59ac827db00a45e7de1bac1c65b9c93Andreas Gampe                           [image_path]() { unlink(image_path.c_str()); }
19646fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe                           );
19656fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe            if (image_fd.get() < 0) {
19666fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe                // Could not create application image file. Go on since we can compile without
19676fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe                // it.
19686fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe                LOG(ERROR) << "installd could not create '"
19696fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe                        << image_path
19706fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe                        << "' for image file during dexopt";
19716fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe            } else if (!set_permissions_and_ownership(image_fd.get(),
19726fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe                                                      is_public,
19736fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe                                                      uid,
197489b008dfb59ac827db00a45e7de1bac1c65b9c93Andreas Gampe                                                      image_path.c_str())) {
19756fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe                image_fd.reset(-1);
19766fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe            }
19776fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe        }
19786fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe        // If we have a valid image file path but no image fd, explicitly erase the image file.
19796fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe        if (image_fd.get() < 0) {
198089b008dfb59ac827db00a45e7de1bac1c65b9c93Andreas Gampe            if (unlink(image_path.c_str()) < 0) {
19816fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe                if (errno != ENOENT) {
19826fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe                    PLOG(ERROR) << "Couldn't unlink image file " << image_path;
19836fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe                }
19846fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe            }
19856fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe        }
1986edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier    }
1987e80d58db65d29ed4647e631a81aed644fea00913Mathieu Chartier
19887365a10689df23334d245b211ce272502ad20669Alex Light    ALOGV("DexInv: --- BEGIN '%s' ---\n", input_file);
198994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
19906fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe    pid_t pid = fork();
199194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (pid == 0) {
199294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        /* child -- drop privileges before continuing */
19936a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        drop_capabilities(uid);
19946a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle
199513f141910ba383404145924df64ad6626fe5b42fAndreas Gampe        SetDex2OatAndPatchOatScheduling(boot_complete);
1996fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil        if (flock(out_oat_fd.get(), LOCK_EX | LOCK_NB) != 0) {
1997fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil            ALOGE("flock(%s) failed: %s\n", out_oat_path, strerror(errno));
19986fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe            _exit(67);
199994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
200094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
2001920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray        if (dexopt_action == PATCHOAT_FOR_RELOCATION) {
20026fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe            run_patchoat(input_fd.get(),
200353caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray                         in_vdex_fd.get(),
2004fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil                         out_oat_fd.get(),
2005fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil                         out_vdex_fd.get(),
20066fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe                         input_file,
2007fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil                         in_vdex_path_str.c_str(),
2008fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil                         out_oat_path,
2009fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil                         out_vdex_path_str.c_str(),
20106fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe                         pkgname,
20116fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe                         instruction_set);
2012920c60cdaeb6394eb95aa93c02a2d8f425204074Nicolas Geoffray        } else {
2013aaa1d4e930f653ec21b711eb852194c8abf35143David Brazdil            // Pass dex2oat the relative path to the input file.
201440e566de4b17a0f5f200fbf5014d30367c1992faDavid Sehr            const char *input_file_name = get_location_from_path(input_file);
20156fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe            run_dex2oat(input_fd.get(),
2016fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil                        out_oat_fd.get(),
201753caf0d20c6831d81a0840ac6056ed4c1c691032Nicolas Geoffray                        in_vdex_fd.get(),
2018fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil                        out_vdex_fd.get(),
20196fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe                        image_fd.get(),
20206fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe                        input_file_name,
2021fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil                        out_oat_path,
20226fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe                        swap_fd.get(),
20236fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe                        instruction_set,
20246fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe                        compiler_filter,
20256fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe                        vm_safe_mode,
20266fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe                        debuggable,
20276fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe                        boot_complete,
20286fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe                        reference_profile_fd.get(),
20296fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe                        shared_libraries);
20301705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom        }
20316fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe        _exit(68);   /* only get here on exec failure */
203294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    } else {
20336fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe        int res = wait_child(pid);
203463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        if (res == 0) {
20357365a10689df23334d245b211ce272502ad20669Alex Light            ALOGV("DexInv: --- END '%s' (success) ---\n", input_file);
203663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        } else {
20377365a10689df23334d245b211ce272502ad20669Alex Light            ALOGE("DexInv: --- END '%s' --- status=0x%04x, process failed\n", input_file, res);
20386fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe            return -1;
203994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
204094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
204194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
20426fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe    struct utimbuf ut;
20437365a10689df23334d245b211ce272502ad20669Alex Light    ut.actime = input_stat.st_atime;
20447365a10689df23334d245b211ce272502ad20669Alex Light    ut.modtime = input_stat.st_mtime;
2045fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil    utime(out_oat_path, &ut);
20461705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom
20476fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe    // We've been successful, don't delete output.
2048fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil    out_oat_fd.SetCleanup(false);
2049fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil    out_vdex_fd.SetCleanup(false);
20506fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe    image_fd.SetCleanup(false);
20516fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe    reference_profile_fd.SetCleanup(false);
205294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
20536fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe    return 0;
205494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
205594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
20566c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkeybinder::Status InstalldNativeService::dexopt(const std::string& apkPath, int32_t uid,
20576c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey        const std::unique_ptr<std::string>& packageName, const std::string& instructionSet,
20586c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey        int32_t dexoptNeeded, const std::unique_ptr<std::string>& outputPath, int32_t dexFlags,
20596c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey        const std::string& compilerFilter, const std::unique_ptr<std::string>& uuid,
20606c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey        const std::unique_ptr<std::string>& sharedLibraries) {
20616c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey    ENFORCE_UID(AID_SYSTEM);
20626c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey
20636c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey    const char* apk_path = apkPath.c_str();
20646c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey    const char* pkgname = packageName ? packageName->c_str() : "*";
20656c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey    const char* instruction_set = instructionSet.c_str();
20666c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey    const char* oat_dir = outputPath ? outputPath->c_str() : nullptr;
20676c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey    const char* compiler_filter = compilerFilter.c_str();
20686c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey    const char* volume_uuid = uuid ? uuid->c_str() : nullptr;
20696c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey    const char* shared_libraries = sharedLibraries ? sharedLibraries->c_str() : nullptr;
20706c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey
20716c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey    int res = android::installd::dexopt(apk_path, uid, pkgname, instruction_set, dexoptNeeded,
20726c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey            oat_dir, dexFlags, compiler_filter, volume_uuid, shared_libraries);
20736c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey    return res ? binder::Status::fromServiceSpecificError(-1) : binder::Status::ok();
20746c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey}
20756c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey
2076475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::markBootComplete(const std::string& instructionSet) {
2077475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    ENFORCE_UID(AID_SYSTEM);
2078475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    const char* instruction_set = instructionSet.c_str();
2079475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey
2080475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    char boot_marker_path[PKG_PATH_MAX];
2081475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    sprintf(boot_marker_path,
208202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe          "%s/%s/%s/.booting",
208302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe          android_data_dir.path,
208402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe          DALVIK_CACHE,
208502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe          instruction_set);
2086091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath
2087475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    ALOGV("mark_boot_complete : %s", boot_marker_path);
2088475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    if (unlink(boot_marker_path) != 0) {
2089475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey        ALOGE("Unable to unlink boot marker at %s, error=%s", boot_marker_path, strerror(errno));
2090475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey        return binder::Status::fromServiceSpecificError(-1);
2091475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    }
2092475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    return binder::Status::ok();
2093091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath}
2094091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath
209594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodvoid mkinnerdirs(char* path, int basepos, mode_t mode, int uid, int gid,
209694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        struct stat* statbuf)
209794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
209894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    while (path[basepos] != 0) {
209994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (path[basepos] == '/') {
210094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            path[basepos] = 0;
210194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (lstat(path, statbuf) < 0) {
210294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGV("Making directory: %s\n", path);
210394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                if (mkdir(path, mode) == 0) {
210494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    chown(path, uid, gid);
210594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                } else {
210694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    ALOGW("Unable to make directory %s: %s\n", path, strerror(errno));
210794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                }
210894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
210994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            path[basepos] = '/';
211094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            basepos++;
211194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
211294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        basepos++;
211394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
211494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
211594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
2116475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::linkNativeLibraryDirectory(
2117475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey        const std::unique_ptr<std::string>& uuid, const std::string& packageName,
2118475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey        const std::string& nativeLibPath32, int32_t userId) {
2119475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    ENFORCE_UID(AID_SYSTEM);
2120475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    const char* uuid_ = uuid ? uuid->c_str() : nullptr;
2121475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    const char* pkgname = packageName.c_str();
2122475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    const char* asecLibDir = nativeLibPath32.c_str();
212394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    struct stat s, libStat;
212494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int rc = 0;
212594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
2126475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    std::string _pkgdir(create_data_user_ce_package_path(uuid_, userId, pkgname));
2127c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey    std::string _libsymlink(_pkgdir + PKG_LIB_POSTFIX);
2128c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey
2129c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey    const char* pkgdir = _pkgdir.c_str();
2130c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey    const char* libsymlink = _libsymlink.c_str();
213194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
2132475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    if (stat(pkgdir, &s) < 0) {
2133475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey        return binder::Status::fromServiceSpecificError(-1);
2134475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    }
213594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
213694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (chown(pkgdir, AID_INSTALL, AID_INSTALL) < 0) {
213794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGE("failed to chown '%s': %s\n", pkgdir, strerror(errno));
2138475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey        return binder::Status::fromServiceSpecificError(-1);
213994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
214094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
214194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (chmod(pkgdir, 0700) < 0) {
214294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGE("linklib() 1: failed to chmod '%s': %s\n", pkgdir, strerror(errno));
214394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        rc = -1;
214494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        goto out;
214594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
214694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
214794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (lstat(libsymlink, &libStat) < 0) {
214894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (errno != ENOENT) {
214994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            ALOGE("couldn't stat lib dir: %s\n", strerror(errno));
215094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            rc = -1;
215194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            goto out;
215294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
215394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    } else {
215494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (S_ISDIR(libStat.st_mode)) {
21553aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath            if (delete_dir_contents(libsymlink, 1, NULL) < 0) {
215694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                rc = -1;
215794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                goto out;
215894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
215994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        } else if (S_ISLNK(libStat.st_mode)) {
216094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (unlink(libsymlink) < 0) {
216194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("couldn't unlink lib dir: %s\n", strerror(errno));
216294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                rc = -1;
216394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                goto out;
216494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
216594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
216694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
216794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
216894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (symlink(asecLibDir, libsymlink) < 0) {
216994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGE("couldn't symlink directory '%s' -> '%s': %s\n", libsymlink, asecLibDir,
217094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                strerror(errno));
217194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        rc = -errno;
217294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        goto out;
217394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
217494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
217594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodout:
217694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (chmod(pkgdir, s.st_mode) < 0) {
217794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGE("linklib() 2: failed to chmod '%s': %s\n", pkgdir, strerror(errno));
217894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        rc = -errno;
217994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
218094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
218194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (chown(pkgdir, s.st_uid, s.st_gid) < 0) {
218294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGE("failed to chown '%s' : %s\n", pkgdir, strerror(errno));
2183475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey        rc = -errno;
218494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
218594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
2186475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    return rc ? binder::Status::fromServiceSpecificError(-1) : binder::Status::ok();
218794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
218863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad
218963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstadstatic void run_idmap(const char *target_apk, const char *overlay_apk, int idmap_fd)
219063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad{
219163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    static const char *IDMAP_BIN = "/system/bin/idmap";
219263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    static const size_t MAX_INT_LEN = 32;
219363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    char idmap_str[MAX_INT_LEN];
219463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad
219563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    snprintf(idmap_str, sizeof(idmap_str), "%d", idmap_fd);
219663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad
219763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    execl(IDMAP_BIN, IDMAP_BIN, "--fd", target_apk, overlay_apk, idmap_str, (char*)NULL);
219863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    ALOGE("execl(%s) failed: %s\n", IDMAP_BIN, strerror(errno));
219963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad}
220063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad
220163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad// Transform string /a/b/c.apk to (prefix)/a@b@c.apk@(suffix)
220263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad// eg /a/b/c.apk to /data/resource-cache/a@b@c.apk@idmap
220363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstadstatic int flatten_path(const char *prefix, const char *suffix,
220463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        const char *overlay_path, char *idmap_path, size_t N)
220563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad{
220663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    if (overlay_path == NULL || idmap_path == NULL) {
220763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        return -1;
220863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    }
220963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    const size_t len_overlay_path = strlen(overlay_path);
221063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    // will access overlay_path + 1 further below; requires absolute path
221163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    if (len_overlay_path < 2 || *overlay_path != '/') {
221263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        return -1;
221363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    }
221463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    const size_t len_idmap_root = strlen(prefix);
221563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    const size_t len_suffix = strlen(suffix);
221663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    if (SIZE_MAX - len_idmap_root < len_overlay_path ||
221763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad            SIZE_MAX - (len_idmap_root + len_overlay_path) < len_suffix) {
221863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        // additions below would cause overflow
221963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        return -1;
222063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    }
222163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    if (N < len_idmap_root + len_overlay_path + len_suffix) {
222263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        return -1;
222363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    }
222463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    memset(idmap_path, 0, N);
222563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    snprintf(idmap_path, N, "%s%s%s", prefix, overlay_path + 1, suffix);
222663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    char *ch = idmap_path + len_idmap_root;
222763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    while (*ch != '\0') {
222863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        if (*ch == '/') {
222963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad            *ch = '@';
223063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        }
223163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        ++ch;
223263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    }
223363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    return 0;
223463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad}
223563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad
2236475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::idmap(const std::string& targetApkPath,
2237475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey        const std::string& overlayApkPath, int32_t uid) {
2238475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    ENFORCE_UID(AID_SYSTEM);
2239475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    const char* target_apk = targetApkPath.c_str();
2240475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    const char* overlay_apk = overlayApkPath.c_str();
224163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    ALOGV("idmap target_apk=%s overlay_apk=%s uid=%d\n", target_apk, overlay_apk, uid);
224263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad
224363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    int idmap_fd = -1;
224463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    char idmap_path[PATH_MAX];
224563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad
224663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    if (flatten_path(IDMAP_PREFIX, IDMAP_SUFFIX, overlay_apk,
224763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad                idmap_path, sizeof(idmap_path)) == -1) {
224863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        ALOGE("idmap cannot generate idmap path for overlay %s\n", overlay_apk);
224963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        goto fail;
225063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    }
225163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad
225263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    unlink(idmap_path);
225363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    idmap_fd = open(idmap_path, O_RDWR | O_CREAT | O_EXCL, 0644);
225463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    if (idmap_fd < 0) {
225563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        ALOGE("idmap cannot open '%s' for output: %s\n", idmap_path, strerror(errno));
225663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        goto fail;
225763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    }
225863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    if (fchown(idmap_fd, AID_SYSTEM, uid) < 0) {
225963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        ALOGE("idmap cannot chown '%s'\n", idmap_path);
226063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        goto fail;
226163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    }
226263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    if (fchmod(idmap_fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0) {
226363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        ALOGE("idmap cannot chmod '%s'\n", idmap_path);
226463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        goto fail;
226563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    }
226663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad
226763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    pid_t pid;
226863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    pid = fork();
226963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    if (pid == 0) {
227063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        /* child -- drop privileges before continuing */
227163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        if (setgid(uid) != 0) {
227263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad            ALOGE("setgid(%d) failed during idmap\n", uid);
227363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad            exit(1);
227463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        }
227563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        if (setuid(uid) != 0) {
227663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad            ALOGE("setuid(%d) failed during idmap\n", uid);
227763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad            exit(1);
227863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        }
227963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        if (flock(idmap_fd, LOCK_EX | LOCK_NB) != 0) {
228063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad            ALOGE("flock(%s) failed during idmap: %s\n", idmap_path, strerror(errno));
228163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad            exit(1);
228263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        }
228363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad
228463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        run_idmap(target_apk, overlay_apk, idmap_fd);
228563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        exit(1); /* only if exec call to idmap failed */
228663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    } else {
228763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        int status = wait_child(pid);
228863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        if (status != 0) {
228963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad            ALOGE("idmap failed, status=0x%04x\n", status);
229063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad            goto fail;
229163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        }
229263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    }
229363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad
229463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    close(idmap_fd);
2295475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    return binder::Status::ok();
229663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstadfail:
229763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    if (idmap_fd >= 0) {
229863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        close(idmap_fd);
229963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        unlink(idmap_path);
230063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    }
2301475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    return binder::Status::fromServiceSpecificError(-1);
230263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad}
2303e9887e46cea4a095e4219927eadbe4c57bb1a5eeRobert Craig
2304c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkeybinder::Status InstalldNativeService::restoreconAppData(const std::unique_ptr<std::string>& uuid,
2305c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey        const std::string& packageName, int32_t userId, int32_t flags, int32_t appId,
2306c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey        const std::string& seInfo) {
2307c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey    ENFORCE_UID(AID_SYSTEM);
2308ebf728fd43ab5c7d11a1f9e5fdc775d6740fae0aJeff Sharkey    int res = 0;
2309e9887e46cea4a095e4219927eadbe4c57bb1a5eeRobert Craig
2310da30dc7336f03ca629fe173db1425fdce989119cRobert Craig    // SELINUX_ANDROID_RESTORECON_DATADATA flag is set by libselinux. Not needed here.
2311c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey    unsigned int seflags = SELINUX_ANDROID_RESTORECON_RECURSE;
2312c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey    const char* uuid_ = uuid ? uuid->c_str() : nullptr;
2313c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey    const char* pkgName = packageName.c_str();
2314c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey    const char* seinfo = seInfo.c_str();
2315e9887e46cea4a095e4219927eadbe4c57bb1a5eeRobert Craig
2316da30dc7336f03ca629fe173db1425fdce989119cRobert Craig    if (!pkgName || !seinfo) {
2317da30dc7336f03ca629fe173db1425fdce989119cRobert Craig        ALOGE("Package name or seinfo tag is null when trying to restorecon.");
2318c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey        return binder::Status::fromServiceSpecificError(-1);
2319da30dc7336f03ca629fe173db1425fdce989119cRobert Craig    }
2320e9887e46cea4a095e4219927eadbe4c57bb1a5eeRobert Craig
2321c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey    uid_t uid = multiuser_get_uid(userId, appId);
2322aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey    if (flags & FLAG_STORAGE_CE) {
2323c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey        auto path = create_data_user_ce_package_path(uuid_, userId, pkgName);
2324c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey        if (selinux_android_restorecon_pkgdir(path.c_str(), seinfo, uid, seflags) < 0) {
2325c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey            PLOG(ERROR) << "restorecon failed for " << path;
2326ebf728fd43ab5c7d11a1f9e5fdc775d6740fae0aJeff Sharkey            res = -1;
2327da30dc7336f03ca629fe173db1425fdce989119cRobert Craig        }
2328c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey    }
2329aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey    if (flags & FLAG_STORAGE_DE) {
2330c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey        auto path = create_data_user_de_package_path(uuid_, userId, pkgName);
2331c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey        if (selinux_android_restorecon_pkgdir(path.c_str(), seinfo, uid, seflags) < 0) {
2332c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey            PLOG(ERROR) << "restorecon failed for " << path;
2333ea0e4b19d942f62ea21ca4e70f939f3d4aa14b37Jeff Sharkey            // TODO: include result once 25796509 is fixed
2334da30dc7336f03ca629fe173db1425fdce989119cRobert Craig        }
2335da30dc7336f03ca629fe173db1425fdce989119cRobert Craig    }
2336c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey    return res ? binder::Status::fromServiceSpecificError(-1) : binder::Status::ok();
2337e9887e46cea4a095e4219927eadbe4c57bb1a5eeRobert Craig}
23383aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath
2339475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::createOatDir(const std::string& oatDir,
2340475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey        const std::string& instructionSet) {
2341475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    ENFORCE_UID(AID_SYSTEM);
2342475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    const char* oat_dir = oatDir.c_str();
2343475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    const char* instruction_set = instructionSet.c_str();
234488ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov    char oat_instr_dir[PKG_PATH_MAX];
234588ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov
234688ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov    if (validate_apk_path(oat_dir)) {
234788ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov        ALOGE("invalid apk path '%s' (bad prefix)\n", oat_dir);
2348475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey        return binder::Status::fromServiceSpecificError(-1);
234988ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov    }
23508eed7e6a9bb527af7380ec13b390c6e2955eba6aFyodor Kupolov    if (fs_prepare_dir(oat_dir, S_IRWXU | S_IRWXG | S_IXOTH, AID_SYSTEM, AID_INSTALL)) {
2351475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey        return binder::Status::fromServiceSpecificError(-1);
235288ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov    }
235388ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov    if (selinux_android_restorecon(oat_dir, 0)) {
235488ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov        ALOGE("cannot restorecon dir '%s': %s\n", oat_dir, strerror(errno));
2355475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey        return binder::Status::fromServiceSpecificError(-1);
235688ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov    }
235788ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov    snprintf(oat_instr_dir, PKG_PATH_MAX, "%s/%s", oat_dir, instruction_set);
23588eed7e6a9bb527af7380ec13b390c6e2955eba6aFyodor Kupolov    if (fs_prepare_dir(oat_instr_dir, S_IRWXU | S_IRWXG | S_IXOTH, AID_SYSTEM, AID_INSTALL)) {
2359475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey        return binder::Status::fromServiceSpecificError(-1);
236088ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov    }
2361475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    return binder::Status::ok();
236288ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov}
236388ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov
2364475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::rmPackageDir(const std::string& packageDir) {
2365475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    ENFORCE_UID(AID_SYSTEM);
2366475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    const char* apk_path = packageDir.c_str();
236788ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov    if (validate_apk_path(apk_path)) {
236888ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov        ALOGE("invalid apk path '%s' (bad prefix)\n", apk_path);
2369475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey        return binder::Status::fromServiceSpecificError(-1);
237088ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov    }
2371475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    int res = delete_dir_contents(apk_path, 1 /* also_delete_dir */,
2372475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey            NULL /* exclusion_predicate */);
2373475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    return res ? binder::Status::fromServiceSpecificError(-1) : binder::Status::ok();
237488ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov}
237588ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov
2376475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::linkFile(const std::string& relativePath,
2377475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey        const std::string& fromBase, const std::string& toBase) {
2378475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    ENFORCE_UID(AID_SYSTEM);
2379475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    const char* relative_path = relativePath.c_str();
2380475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    const char* from_base = fromBase.c_str();
2381475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    const char* to_base = toBase.c_str();
2382d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath    char from_path[PKG_PATH_MAX];
2383d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath    char to_path[PKG_PATH_MAX];
2384d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath    snprintf(from_path, PKG_PATH_MAX, "%s/%s", from_base, relative_path);
2385d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath    snprintf(to_path, PKG_PATH_MAX, "%s/%s", to_base, relative_path);
2386d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath
2387d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath    if (validate_apk_path_subdirs(from_path)) {
2388d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath        ALOGE("invalid app data sub-path '%s' (bad prefix)\n", from_path);
2389475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey        return binder::Status::fromServiceSpecificError(-1);
2390d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath    }
2391d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath
2392d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath    if (validate_apk_path_subdirs(to_path)) {
2393d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath        ALOGE("invalid app data sub-path '%s' (bad prefix)\n", to_path);
2394475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey        return binder::Status::fromServiceSpecificError(-1);
2395d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath    }
2396d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath
2397d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath    const int ret = link(from_path, to_path);
2398d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath    if (ret < 0) {
2399d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath        ALOGE("link(%s, %s) failed : %s", from_path, to_path, strerror(errno));
2400475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey        return binder::Status::fromServiceSpecificError(-1);
2401d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath    }
2402d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath
2403475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    return binder::Status::ok();
2404d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath}
2405d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath
2406e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe// Helper for move_ab, so that we can have common failure-case cleanup.
2407e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampestatic bool unlink_and_rename(const char* from, const char* to) {
2408e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe    // Check whether "from" exists, and if so whether it's regular. If it is, unlink. Otherwise,
2409e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe    // return a failure.
2410e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe    struct stat s;
2411e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe    if (stat(to, &s) == 0) {
2412e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe        if (!S_ISREG(s.st_mode)) {
2413e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe            LOG(ERROR) << from << " is not a regular file to replace for A/B.";
2414e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe            return false;
2415e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe        }
2416e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe        if (unlink(to) != 0) {
2417e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe            LOG(ERROR) << "Could not unlink " << to << " to move A/B.";
2418e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe            return false;
2419e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe        }
2420e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe    } else {
2421e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe        // This may be a permission problem. We could investigate the error code, but we'll just
2422e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe        // let the rename failure do the work for us.
2423e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe    }
2424e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe
2425e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe    // Try to rename "to" to "from."
2426e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe    if (rename(from, to) != 0) {
2427e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe        PLOG(ERROR) << "Could not rename " << from << " to " << to;
2428e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe        return false;
2429e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe    }
2430e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe    return true;
2431e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe}
2432e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe
2433ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe// Move/rename a B artifact (from) to an A artifact (to).
2434ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampestatic bool move_ab_path(const std::string& b_path, const std::string& a_path) {
2435ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe    // Check whether B exists.
2436ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe    {
2437ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe        struct stat s;
2438ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe        if (stat(b_path.c_str(), &s) != 0) {
2439ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe            // Silently ignore for now. The service calling this isn't smart enough to understand
2440ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe            // lack of artifacts at the moment.
2441ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe            return false;
2442ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe        }
2443ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe        if (!S_ISREG(s.st_mode)) {
2444ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe            LOG(ERROR) << "A/B artifact " << b_path << " is not a regular file.";
2445ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe            // Try to unlink, but swallow errors.
2446ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe            unlink(b_path.c_str());
2447ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe            return false;
2448ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe        }
2449ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe    }
2450ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe
2451ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe    // Rename B to A.
2452ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe    if (!unlink_and_rename(b_path.c_str(), a_path.c_str())) {
2453ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe        // Delete the b_path so we don't try again (or fail earlier).
2454ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe        if (unlink(b_path.c_str()) != 0) {
2455ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe            PLOG(ERROR) << "Could not unlink " << b_path;
2456ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe        }
2457ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe
2458ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe        return false;
2459ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe    }
2460ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe
2461ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe    return true;
2462ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe}
2463ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe
2464475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::moveAb(const std::string& apkPath,
2465475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey        const std::string& instructionSet, const std::string& outputPath) {
2466475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    ENFORCE_UID(AID_SYSTEM);
2467475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    const char* apk_path = apkPath.c_str();
2468475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    const char* instruction_set = instructionSet.c_str();
2469475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    const char* oat_dir = outputPath.c_str();
2470d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe
2471d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe    // Get the current slot suffix. No suffix, no A/B.
2472d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe    std::string slot_suffix;
2473d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe    {
2474d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe        char buf[kPropertyValueMax];
2475d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe        if (get_property("ro.boot.slot_suffix", buf, nullptr) <= 0) {
2476475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey            return binder::Status::fromServiceSpecificError(-1);
2477d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe        }
2478d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe        slot_suffix = buf;
2479d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe
2480fd12edaeab839f3f1f087cc75bd18b4d8af5b192Andreas Gampe        if (!ValidateTargetSlotSuffix(slot_suffix)) {
2481d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe            LOG(ERROR) << "Target slot suffix not legal: " << slot_suffix;
2482475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey            return binder::Status::fromServiceSpecificError(-1);
2483d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe        }
2484d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe    }
2485d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe
2486d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe    // Validate other inputs.
2487e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe    if (validate_apk_path(apk_path) != 0) {
2488e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe        LOG(ERROR) << "invalid apk_path " << apk_path;
2489475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey        return binder::Status::fromServiceSpecificError(-1);
2490e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe    }
2491e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe    if (validate_apk_path(oat_dir) != 0) {
2492e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe        LOG(ERROR) << "invalid oat_dir " << oat_dir;
2493475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey        return binder::Status::fromServiceSpecificError(-1);
2494e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe    }
2495e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe
2496e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe    char a_path[PKG_PATH_MAX];
2497e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe    if (!calculate_oat_file_path(a_path, oat_dir, apk_path, instruction_set)) {
2498475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey        return binder::Status::fromServiceSpecificError(-1);
2499e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe    }
2500fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil    const std::string a_vdex_path = create_vdex_filename(a_path);
2501ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe    const std::string a_image_path = create_image_filename(a_path);
2502e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe
2503d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe    // B path = A path + slot suffix.
2504d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe    const std::string b_path = StringPrintf("%s.%s", a_path, slot_suffix.c_str());
2505fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil    const std::string b_vdex_path = StringPrintf("%s.%s", a_vdex_path.c_str(), slot_suffix.c_str());
2506d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe    const std::string b_image_path = StringPrintf("%s.%s",
2507d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe                                                  a_image_path.c_str(),
2508d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe                                                  slot_suffix.c_str());
2509e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe
2510fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil    bool success = true;
2511fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil    if (move_ab_path(b_path, a_path)) {
2512fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil        if (move_ab_path(b_vdex_path, a_vdex_path)) {
2513fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil            // Note: we can live without an app image. As such, ignore failure to move the image file.
2514fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil            //       If we decide to require the app image, or the app image being moved correctly,
2515fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil            //       then change accordingly.
2516fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil            constexpr bool kIgnoreAppImageFailure = true;
2517fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil
2518fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil            if (!a_image_path.empty()) {
2519fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil                if (!move_ab_path(b_image_path, a_image_path)) {
2520c338c7f0b6acd1cd7204e21c0d3408b6946e5024David Brazdil                    unlink(a_image_path.c_str());
2521fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil                    if (!kIgnoreAppImageFailure) {
2522fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil                        success = false;
2523fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil                    }
2524fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil                }
2525fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil            }
2526fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil        } else {
2527fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil            // Cleanup: delete B image, ignore errors.
2528fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil            unlink(b_image_path.c_str());
2529fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil            success = false;
2530e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe        }
2531ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe    } else {
2532ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe        // Cleanup: delete B image, ignore errors.
2533fc5934e693ca52c89a78297e40ddb08e544e3631David Brazdil        unlink(b_vdex_path.c_str());
2534ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe        unlink(b_image_path.c_str());
2535ab15dbfb4a113ce1fa18d3a5f1b91a95f678f272Andreas Gampe        success = false;
2536e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe    }
2537e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe
2538475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    return success ? binder::Status::ok() : binder::Status::fromServiceSpecificError(-1);
2539e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe}
2540e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe
2541475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::deleteOdex(const std::string& apkPath,
2542475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey        const std::string& instructionSet, const std::string& outputPath) {
2543475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    ENFORCE_UID(AID_SYSTEM);
2544475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    const char* apk_path = apkPath.c_str();
2545475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    const char* instruction_set = instructionSet.c_str();
2546475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    const char* oat_dir = outputPath.c_str();
2547475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey
2548b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampe    // Delete the oat/odex file.
2549b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampe    char out_path[PKG_PATH_MAX];
2550b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampe    if (!create_oat_out_path(apk_path, instruction_set, oat_dir, out_path)) {
2551475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey        return binder::Status::fromServiceSpecificError(-1);
2552b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampe    }
2553b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampe
2554b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampe    // In case of a permission failure report the issue. Otherwise just print a warning.
2555b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampe    auto unlink_and_check = [](const char* path) -> bool {
2556b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampe        int result = unlink(path);
2557b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampe        if (result != 0) {
2558b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampe            if (errno == EACCES || errno == EPERM) {
2559b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampe                PLOG(ERROR) << "Could not unlink " << path;
2560b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampe                return false;
2561b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampe            }
2562b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampe            PLOG(WARNING) << "Could not unlink " << path;
2563b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampe        }
2564b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampe        return true;
2565b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampe    };
2566b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampe
2567b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampe    // Delete the oat/odex file.
2568b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampe    bool return_value_oat = unlink_and_check(out_path);
2569b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampe
2570b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampe    // Derive and delete the app image.
2571b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampe    bool return_value_art = unlink_and_check(create_image_filename(out_path).c_str());
2572b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampe
2573b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampe    // Report success.
2574475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    bool success = return_value_oat && return_value_art;
2575475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey    return success ? binder::Status::ok() : binder::Status::fromServiceSpecificError(-1);
2576b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampe}
2577b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampe
257802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe}  // namespace installd
257902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe}  // namespace android
2580