InstalldNativeService.cpp revision edae669f18eb99b9316891fdde627e2f385c3c64
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>
2102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <stdlib.h>
2202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <sys/capability.h>
2302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <sys/file.h>
2402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <sys/resource.h>
2502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <sys/stat.h>
26cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey#include <sys/types.h>
276a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle#include <sys/wait.h>
28cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey#include <sys/xattr.h>
2902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <unistd.h>
3041ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey
31e4ec9eb7b4c452493589983970ba5ccc501728d1Elliott Hughes#include <android-base/stringprintf.h>
32e4ec9eb7b4c452493589983970ba5ccc501728d1Elliott Hughes#include <android-base/logging.h>
3302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <cutils/fs.h>
3402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <cutils/log.h>               // TODO: Move everything to base/logging.
350378aaf257aee92539d30543914a50c4481c6a18Brian Carlstrom#include <cutils/sched_policy.h>
3694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#include <diskusage/dirsize.h>
37e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey#include <logwrap/logwrap.h>
3802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <private/android_filesystem_config.h>
39e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey#include <selinux/android.h>
4002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <system/thread_defs.h>
41e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey
4202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <globals.h>
4302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <installd_deps.h>
4402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <utils.h>
4502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
4602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#ifndef LOG_TAG
4702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#define LOG_TAG "installd"
4802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#endif
4941ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey
5041ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkeyusing android::base::StringPrintf;
5194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
5202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampenamespace android {
5302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampenamespace installd {
5494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
55cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkeystatic constexpr const char* kCpPath = "/system/bin/cp";
56cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkeystatic constexpr const char* kXattrDefault = "user.default";
57e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey
58eecb2d205366351af42fd0cd9e1a95de3980764eJanis Danisevskis#define MIN_RESTRICTED_HOME_SDK_VERSION 24 // > M
59eecb2d205366351af42fd0cd9e1a95de3980764eJanis Danisevskis
606a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravletypedef int fd_t;
616a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle
626a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic bool property_get_bool(const char* property_name, bool default_value = false) {
636a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    char tmp_property_value[kPropertyValueMax];
646a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    bool have_property = get_property(property_name, tmp_property_value, nullptr) > 0;
656a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    if (!have_property) {
666a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        return default_value;
676a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    }
686a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    return strcmp(tmp_property_value, "true") == 0;
696a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle}
706a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle
71c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkeyint create_app_data(const char *uuid, const char *pkgname, userid_t userid, int flags,
72eecb2d205366351af42fd0cd9e1a95de3980764eJanis Danisevskis        appid_t appid, const char* seinfo, int target_sdk_version) {
73c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey    uid_t uid = multiuser_get_uid(userid, appid);
74eecb2d205366351af42fd0cd9e1a95de3980764eJanis Danisevskis    int target_mode = target_sdk_version >= MIN_RESTRICTED_HOME_SDK_VERSION ? 0700 : 0751;
75aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey    if (flags & FLAG_STORAGE_CE) {
76c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey        auto path = create_data_user_package_path(uuid, userid, pkgname);
77eecb2d205366351af42fd0cd9e1a95de3980764eJanis Danisevskis        if (fs_prepare_dir_strict(path.c_str(), target_mode, uid, uid) != 0) {
78c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey            PLOG(ERROR) << "Failed to prepare " << path;
79c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey            return -1;
80c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey        }
81c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey        if (selinux_android_setfilecon(path.c_str(), pkgname, seinfo, uid) < 0) {
82c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey            PLOG(ERROR) << "Failed to setfilecon " << path;
83c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey            return -1;
84c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey        }
8594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
86aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey    if (flags & FLAG_STORAGE_DE) {
87c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey        auto path = create_data_user_de_package_path(uuid, userid, pkgname);
88eecb2d205366351af42fd0cd9e1a95de3980764eJanis Danisevskis        if (fs_prepare_dir_strict(path.c_str(), target_mode, uid, uid) == -1) {
89c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey            PLOG(ERROR) << "Failed to prepare " << path;
90a03c747ff6e1afb5c1dc86560705bd979cd5c49fJeff Sharkey            // TODO: include result once 25796509 is fixed
91a03c747ff6e1afb5c1dc86560705bd979cd5c49fJeff Sharkey            return 0;
92c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey        }
93c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey        if (selinux_android_setfilecon(path.c_str(), pkgname, seinfo, uid) < 0) {
94c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey            PLOG(ERROR) << "Failed to setfilecon " << path;
95a03c747ff6e1afb5c1dc86560705bd979cd5c49fJeff Sharkey            // TODO: include result once 25796509 is fixed
96a03c747ff6e1afb5c1dc86560705bd979cd5c49fJeff Sharkey            return 0;
97c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey        }
986a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle
996a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        if (property_get_bool("dalvik.vm.usejitprofiles")) {
1006a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle            const std::string profile_path = create_data_user_profile_package_path(userid, pkgname);
1016a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle            // read-write-execute only for the app user.
1026a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle            if (fs_prepare_dir_strict(profile_path.c_str(), 0700, uid, uid) != 0) {
1036a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle                PLOG(ERROR) << "Failed to prepare " << profile_path;
1046a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle                return -1;
1056a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle            }
1066a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle            const std::string ref_profile_path = create_data_ref_profile_package_path(pkgname);
1076a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle            // dex2oat/profman runs under the shared app gid and it needs to read/write reference
1086a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle            // profiles.
1096a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle            appid_t shared_app_gid = multiuser_get_shared_app_gid(uid);
1106a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle            if (fs_prepare_dir_strict(
1116a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle                    ref_profile_path.c_str(), 0700, shared_app_gid, shared_app_gid) != 0) {
1126a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle                PLOG(ERROR) << "Failed to prepare " << ref_profile_path;
1136a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle                return -1;
1146a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle            }
1156a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        }
11694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
11794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return 0;
11894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
11994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
120cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkeyint migrate_app_data(const char *uuid, const char *pkgname, userid_t userid, int flags) {
121cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey    // This method only exists to upgrade system apps that have requested
122cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey    // forceDeviceEncrypted, so their default storage always lives in a
123cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey    // consistent location.  This only works on non-FBE devices, since we
124cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey    // never want to risk exposing data on a device with real CE/DE storage.
125cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey
126cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey    auto ce_path = create_data_user_package_path(uuid, userid, pkgname);
127cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey    auto de_path = create_data_user_de_package_path(uuid, userid, pkgname);
128cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey
129cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey    // If neither directory is marked as default, assume CE is default
130cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey    if (getxattr(ce_path.c_str(), kXattrDefault, nullptr, 0) == -1
131cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey            && getxattr(de_path.c_str(), kXattrDefault, nullptr, 0) == -1) {
132cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey        if (setxattr(ce_path.c_str(), kXattrDefault, nullptr, 0, 0) != 0) {
133cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey            PLOG(ERROR) << "Failed to mark default storage " << ce_path;
134cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey            return -1;
135cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey        }
136cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey    }
137cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey
138cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey    // Migrate default data location if needed
139cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey    auto target = (flags & FLAG_STORAGE_DE) ? de_path : ce_path;
140cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey    auto source = (flags & FLAG_STORAGE_DE) ? ce_path : de_path;
141cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey
142cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey    if (getxattr(target.c_str(), kXattrDefault, nullptr, 0) == -1) {
143cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey        LOG(WARNING) << "Requested default storage " << target
144cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey                << " is not active; migrating from " << source;
145cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey        if (delete_dir_contents_and_dir(target) != 0) {
146cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey            PLOG(ERROR) << "Failed to delete";
147cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey            return -1;
148cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey        }
149cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey        if (rename(source.c_str(), target.c_str()) != 0) {
150cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey            PLOG(ERROR) << "Failed to rename";
151cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey            return -1;
152cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey        }
153cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey    }
154cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey
155cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey    return 0;
156cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey}
157cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey
1586a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle// Keep profile paths in sync with ActivityThread.
1596a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravleconstexpr const char* PRIMARY_PROFILE_NAME = "primary.prof";
1606a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic std::string create_primary_profile(const std::string& profile_dir) {
1616a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    return StringPrintf("%s/%s", profile_dir.c_str(), PRIMARY_PROFILE_NAME);
1626a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle}
1636a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle
164d828d6839f6a01e3f0b2b8b2694139dfa801c557David Brazdilstatic bool unlink_reference_profile(const char* pkgname) {
1656a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    std::string reference_profile_dir = create_data_ref_profile_package_path(pkgname);
1666a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    std::string reference_profile = create_primary_profile(reference_profile_dir);
1676a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    if (unlink(reference_profile.c_str()) != 0) {
168278edac4b541e0a671d646a0e17bdbea411c8a08Calin Juravle        if (errno != ENOENT) {
169278edac4b541e0a671d646a0e17bdbea411c8a08Calin Juravle            PLOG(WARNING) << "Could not unlink " << reference_profile;
170d828d6839f6a01e3f0b2b8b2694139dfa801c557David Brazdil            return false;
171278edac4b541e0a671d646a0e17bdbea411c8a08Calin Juravle        }
1726a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    }
173d828d6839f6a01e3f0b2b8b2694139dfa801c557David Brazdil    return true;
1746a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle}
1756a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle
176edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravlestatic bool unlink_current_profile(const char* pkgname, userid_t user) {
177edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle    std::string profile_dir = create_data_user_profile_package_path(user, pkgname);
178edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle    std::string profile = create_primary_profile(profile_dir);
179edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle    if (unlink(profile.c_str()) != 0) {
180edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle        if (errno != ENOENT) {
181edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle            PLOG(WARNING) << "Could not unlink " << profile;
182edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle            return false;
183edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle        }
184edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle    }
185edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle    return true;
186edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle}
187edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle
188d828d6839f6a01e3f0b2b8b2694139dfa801c557David Brazdilstatic bool unlink_current_profiles(const char* pkgname) {
189d828d6839f6a01e3f0b2b8b2694139dfa801c557David Brazdil    bool success = true;
1906a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    std::vector<userid_t> users = get_known_users(/*volume_uuid*/ nullptr);
1916a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    for (auto user : users) {
192edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle        success &= unlink_current_profile(pkgname, user);
1936a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    }
194d828d6839f6a01e3f0b2b8b2694139dfa801c557David Brazdil    return success;
1956a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle}
1966a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle
197edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravleint clear_app_profiles(const char* pkgname) {
198d828d6839f6a01e3f0b2b8b2694139dfa801c557David Brazdil    bool success = true;
199d828d6839f6a01e3f0b2b8b2694139dfa801c557David Brazdil    success &= unlink_reference_profile(pkgname);
200d828d6839f6a01e3f0b2b8b2694139dfa801c557David Brazdil    success &= unlink_current_profiles(pkgname);
201edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle    return success ? 0 : -1;
2026a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle}
2036a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle
204c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkeyint clear_app_data(const char *uuid, const char *pkgname, userid_t userid, int flags) {
205c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey    std::string suffix = "";
206edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle    bool only_cache = false;
207c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey    if (flags & FLAG_CLEAR_CACHE_ONLY) {
208c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey        suffix = CACHE_DIR_POSTFIX;
209edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle        only_cache = true;
210c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey    } else if (flags & FLAG_CLEAR_CODE_CACHE_ONLY) {
211c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey        suffix = CODE_CACHE_DIR_POSTFIX;
212edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle        only_cache = true;
213c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey    }
21494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
215ebf728fd43ab5c7d11a1f9e5fdc775d6740fae0aJeff Sharkey    int res = 0;
216aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey    if (flags & FLAG_STORAGE_CE) {
217c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey        auto path = create_data_user_package_path(uuid, userid, pkgname) + suffix;
218c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey        if (access(path.c_str(), F_OK) == 0) {
219c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey            res |= delete_dir_contents(path);
220c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey        }
22194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
222aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey    if (flags & FLAG_STORAGE_DE) {
223c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey        auto path = create_data_user_de_package_path(uuid, userid, pkgname) + suffix;
224c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey        if (access(path.c_str(), F_OK) == 0) {
225a03c747ff6e1afb5c1dc86560705bd979cd5c49fJeff Sharkey            // TODO: include result once 25796509 is fixed
226a03c747ff6e1afb5c1dc86560705bd979cd5c49fJeff Sharkey            delete_dir_contents(path);
227c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey        }
228edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle        if (!only_cache) {
229edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle            if (!unlink_current_profile(pkgname, userid)) {
230edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle                res |= -1;
231edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle            }
232edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle        }
23394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
234c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey    return res;
235c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey}
23694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
237edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravlestatic int destroy_app_current_profiles(const char *pkgname, userid_t userid) {
238edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle    return delete_dir_contents_and_dir(create_data_user_profile_package_path(userid, pkgname));
239edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle}
240edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle
241edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravleint destroy_app_profiles(const char *pkgname) {
242edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle    int result = 0;
243edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle    std::vector<userid_t> users = get_known_users(/*volume_uuid*/ nullptr);
244edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle    for (auto user : users) {
245edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle        result |= destroy_app_current_profiles(pkgname, user);
246edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle    }
247edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle    result |= delete_dir_contents_and_dir(create_data_ref_profile_package_path(pkgname));
248edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle    return result;
249caa6b809a5ed024a8f3f551c5c109a5ad50ab772Calin Juravle}
250caa6b809a5ed024a8f3f551c5c109a5ad50ab772Calin Juravle
251c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkeyint destroy_app_data(const char *uuid, const char *pkgname, userid_t userid, int flags) {
252c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey    int res = 0;
253aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey    if (flags & FLAG_STORAGE_CE) {
254c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey        res |= delete_dir_contents_and_dir(
255c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey                create_data_user_package_path(uuid, userid, pkgname));
25694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
257aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey    if (flags & FLAG_STORAGE_DE) {
258caa6b809a5ed024a8f3f551c5c109a5ad50ab772Calin Juravle        res |= delete_dir_contents_and_dir(
259c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey                create_data_user_de_package_path(uuid, userid, pkgname));
26094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
261edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle    destroy_app_current_profiles(pkgname, userid);
262c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey    return res;
26394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
26494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
265c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkeyint move_complete_app(const char *from_uuid, const char *to_uuid, const char *package_name,
266eecb2d205366351af42fd0cd9e1a95de3980764eJanis Danisevskis        const char *data_app_name, appid_t appid, const char* seinfo, int target_sdk_version) {
267e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    std::vector<userid_t> users = get_known_users(from_uuid);
268e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey
269d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey    // Copy app
270d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey    {
271d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey        std::string from(create_data_app_package_path(from_uuid, data_app_name));
272d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey        std::string to(create_data_app_package_path(to_uuid, data_app_name));
273d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey        std::string to_parent(create_data_app_path(to_uuid));
274d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey
275d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey        char *argv[] = {
276d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey            (char*) kCpPath,
277d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey            (char*) "-F", /* delete any existing destination file first (--remove-destination) */
278d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey            (char*) "-p", /* preserve timestamps, ownership, and permissions */
279d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey            (char*) "-R", /* recurse into subdirectories (DEST must be a directory) */
280d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey            (char*) "-P", /* Do not follow symlinks [default] */
281d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey            (char*) "-d", /* don't dereference symlinks */
282d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey            (char*) from.c_str(),
283d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey            (char*) to_parent.c_str()
284d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey        };
285d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey
286d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey        LOG(DEBUG) << "Copying " << from << " to " << to;
287d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey        int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true);
288d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey
289d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey        if (rc != 0) {
290d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey            LOG(ERROR) << "Failed copying " << from << " to " << to
291d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey                    << ": status " << rc;
292d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey            goto fail;
293d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey        }
294d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey
295d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey        if (selinux_android_restorecon(to.c_str(), SELINUX_ANDROID_RESTORECON_RECURSE) != 0) {
296d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey            LOG(ERROR) << "Failed to restorecon " << to;
297d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey            goto fail;
298d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey        }
299d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey    }
300d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey
301d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey    // Copy private data for all known users
302ebf728fd43ab5c7d11a1f9e5fdc775d6740fae0aJeff Sharkey    // TODO: handle user_de paths
303e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    for (auto user : users) {
304d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey        std::string from(create_data_user_package_path(from_uuid, user, package_name));
305d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey        std::string to(create_data_user_package_path(to_uuid, user, package_name));
306d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey        std::string to_parent(create_data_user_path(to_uuid, user));
307e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey
308e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey        // Data source may not exist for all users; that's okay
309e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey        if (access(from.c_str(), F_OK) != 0) {
310e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey            LOG(INFO) << "Missing source " << from;
311e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey            continue;
312e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey        }
313e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey
314e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey        std::string user_path(create_data_user_path(to_uuid, user));
315e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey        if (fs_prepare_dir(user_path.c_str(), 0771, AID_SYSTEM, AID_SYSTEM) != 0) {
316e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey            LOG(ERROR) << "Failed to prepare user target " << user_path;
317e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey            goto fail;
318e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey        }
319e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey
320aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey        if (create_app_data(to_uuid, package_name, user, FLAG_STORAGE_CE | FLAG_STORAGE_DE,
321eecb2d205366351af42fd0cd9e1a95de3980764eJanis Danisevskis                appid, seinfo, target_sdk_version) != 0) {
322e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey            LOG(ERROR) << "Failed to create package target " << to;
323e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey            goto fail;
324e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey        }
325e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey
326e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey        char *argv[] = {
327e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey            (char*) kCpPath,
328e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey            (char*) "-F", /* delete any existing destination file first (--remove-destination) */
329e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey            (char*) "-p", /* preserve timestamps, ownership, and permissions */
330e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey            (char*) "-R", /* recurse into subdirectories (DEST must be a directory) */
331e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey            (char*) "-P", /* Do not follow symlinks [default] */
332e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey            (char*) "-d", /* don't dereference symlinks */
333e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey            (char*) from.c_str(),
334d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey            (char*) to_parent.c_str()
335e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey        };
336e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey
337e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey        LOG(DEBUG) << "Copying " << from << " to " << to;
338e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey        int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true);
339e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey
340e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey        if (rc != 0) {
341e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey            LOG(ERROR) << "Failed copying " << from << " to " << to
342e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey                    << ": status " << rc;
343e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey            goto fail;
344e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey        }
345e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey
346aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey        if (restorecon_app_data(to_uuid, package_name, user, FLAG_STORAGE_CE | FLAG_STORAGE_DE,
347c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey                appid, seinfo) != 0) {
348c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey            LOG(ERROR) << "Failed to restorecon";
349c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey            goto fail;
350c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey        }
351e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    }
352e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey
35331f08986f83fa6f2dcf55523b2cf706460aeed7cJeff Sharkey    // We let the framework scan the new location and persist that before
35431f08986f83fa6f2dcf55523b2cf706460aeed7cJeff Sharkey    // deleting the data in the old location; this ordering ensures that
35531f08986f83fa6f2dcf55523b2cf706460aeed7cJeff Sharkey    // we can recover from things like battery pulls.
356e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    return 0;
357e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey
358e36372423000a906bafae68844ebc6c42d09335aJeff Sharkeyfail:
359e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    // Nuke everything we might have already copied
360d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey    {
361d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey        std::string to(create_data_app_package_path(to_uuid, data_app_name));
362d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey        if (delete_dir_contents(to.c_str(), 1, NULL) != 0) {
363d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey            LOG(WARNING) << "Failed to rollback " << to;
364d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey        }
365d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey    }
366e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    for (auto user : users) {
367d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey        std::string to(create_data_user_package_path(to_uuid, user, package_name));
368e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey        if (delete_dir_contents(to.c_str(), 1, NULL) != 0) {
369e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey            LOG(WARNING) << "Failed to rollback " << to;
370e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey        }
371e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    }
372e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    return -1;
373e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey}
374e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey
3757c8bec01790087748ec7afa69a31789828b751f9Robin Leeint make_user_config(userid_t userid)
37694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
377095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee    if (ensure_config_user_dirs(userid) == -1) {
37894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
37994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
380095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee
381095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee    return 0;
382095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee}
383095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee
384ebf728fd43ab5c7d11a1f9e5fdc775d6740fae0aJeff Sharkeyint delete_user(const char *uuid, userid_t userid) {
385ebf728fd43ab5c7d11a1f9e5fdc775d6740fae0aJeff Sharkey    int res = 0;
386095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee
38741ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey    std::string data_path(create_data_user_path(uuid, userid));
388ebf728fd43ab5c7d11a1f9e5fdc775d6740fae0aJeff Sharkey    std::string data_de_path(create_data_user_de_path(uuid, userid));
38941ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey    std::string media_path(create_data_media_path(uuid, userid));
3906a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    std::string profiles_path(create_data_user_profiles_path(userid));
391ebf728fd43ab5c7d11a1f9e5fdc775d6740fae0aJeff Sharkey
392ebf728fd43ab5c7d11a1f9e5fdc775d6740fae0aJeff Sharkey    res |= delete_dir_contents_and_dir(data_path);
393a03c747ff6e1afb5c1dc86560705bd979cd5c49fJeff Sharkey    // TODO: include result once 25796509 is fixed
394a03c747ff6e1afb5c1dc86560705bd979cd5c49fJeff Sharkey    delete_dir_contents_and_dir(data_de_path);
395ebf728fd43ab5c7d11a1f9e5fdc775d6740fae0aJeff Sharkey    res |= delete_dir_contents_and_dir(media_path);
3966a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    res |= delete_dir_contents_and_dir(profiles_path);
397095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee
39841ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey    // Config paths only exist on internal storage
39941ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey    if (uuid == nullptr) {
40041ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey        char config_path[PATH_MAX];
40141ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey        if ((create_user_config_path(config_path, userid) != 0)
40241ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey                || (delete_dir_contents(config_path, 1, NULL) != 0)) {
403ebf728fd43ab5c7d11a1f9e5fdc775d6740fae0aJeff Sharkey            res = -1;
40441ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey        }
40594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
40694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
407ebf728fd43ab5c7d11a1f9e5fdc775d6740fae0aJeff Sharkey    return res;
40894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
40994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
41094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/* Try to ensure free_size bytes of storage are available.
41194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * Returns 0 on success.
41294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * This is rather simple-minded because doing a full LRU would
41394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * be potentially memory-intensive, and without atime it would
41494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * also require that apps constantly modify file metadata even
41594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * when just reading from the cache, which is pretty awful.
41694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */
41741ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkeyint free_cache(const char *uuid, int64_t free_size)
41894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
41994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    cache_t* cache;
42094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int64_t avail;
42194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    DIR *d;
42294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    struct dirent *de;
42394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    char tmpdir[PATH_MAX];
42494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    char *dirpos;
42594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
42641ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey    std::string data_path(create_data_path(uuid));
42741ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey
42841ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey    avail = data_disk_free(data_path);
42994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (avail < 0) return -1;
43094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
43194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    ALOGI("free_cache(%" PRId64 ") avail %" PRId64 "\n", free_size, avail);
43294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (avail >= free_size) return 0;
43394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
43494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    cache = start_cache_collection();
43594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
43641ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey    // Special case for owner on internal storage
43741ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey    if (uuid == nullptr) {
43841ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey        std::string _tmpdir(create_data_user_path(nullptr, 0));
43941ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey        add_cache_files(cache, _tmpdir.c_str(), "cache");
44094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
44194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
44294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // Search for other users and add any cache files from them.
44341ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey    std::string _tmpdir(create_data_path(uuid) + "/" + SECONDARY_USER_PREFIX);
44441ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey    strcpy(tmpdir, _tmpdir.c_str());
44541ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey
44694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    dirpos = tmpdir + strlen(tmpdir);
44794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    d = opendir(tmpdir);
44894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (d != NULL) {
44994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        while ((de = readdir(d))) {
45094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (de->d_type == DT_DIR) {
45194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                const char *name = de->d_name;
45294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    /* always skip "." and ".." */
45394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                if (name[0] == '.') {
45494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    if (name[1] == 0) continue;
45594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    if ((name[1] == '.') && (name[2] == 0)) continue;
45694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                }
45794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                if ((strlen(name)+(dirpos-tmpdir)) < (sizeof(tmpdir)-1)) {
45894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    strcpy(dirpos, name);
45994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    //ALOGI("adding cache files from %s\n", tmpdir);
46094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    add_cache_files(cache, tmpdir, "cache");
46194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                } else {
46294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    ALOGW("Path exceeds limit: %s%s", tmpdir, name);
46394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                }
46494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
46594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
46694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        closedir(d);
46794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
46894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
46994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // Collect cache files on external storage for all users (if it is mounted as part
47094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // of the internal storage).
47194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    strcpy(tmpdir, android_media_dir.path);
47294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    dirpos = tmpdir + strlen(tmpdir);
47394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    d = opendir(tmpdir);
47494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (d != NULL) {
47594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        while ((de = readdir(d))) {
47694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (de->d_type == DT_DIR) {
47794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                const char *name = de->d_name;
47894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    /* skip any dir that doesn't start with a number, so not a user */
47994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                if (name[0] < '0' || name[0] > '9') {
48094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    continue;
48194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                }
48294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                if ((strlen(name)+(dirpos-tmpdir)) < (sizeof(tmpdir)-1)) {
48394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    strcpy(dirpos, name);
48494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    if (lookup_media_dir(tmpdir, "Android") == 0
48594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                            && lookup_media_dir(tmpdir, "data") == 0) {
48694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                        //ALOGI("adding cache files from %s\n", tmpdir);
48794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                        add_cache_files(cache, tmpdir, "cache");
48894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    }
48994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                } else {
49094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    ALOGW("Path exceeds limit: %s%s", tmpdir, name);
49194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                }
49294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
49394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
49494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        closedir(d);
49594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
49694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
49741ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey    clear_cache_files(data_path, cache, free_size);
49894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    finish_cache_collection(cache);
49994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
50041ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey    return data_disk_free(data_path) >= free_size ? 0 : -1;
50194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
50294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
5031b4003207750ea8fe8c7b03eb32d80f1df83979eNarayan Kamathint rm_dex(const char *path, const char *instruction_set)
50494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
50594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    char dex_path[PKG_PATH_MAX];
50694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
507770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey    if (validate_apk_path(path) && validate_system_app_path(path)) {
508770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey        ALOGE("invalid apk path '%s' (bad prefix)\n", path);
509770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey        return -1;
510770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey    }
511770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey
51202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    if (!create_cache_path(dex_path, path, instruction_set)) return -1;
51394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
51494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    ALOGV("unlink %s\n", dex_path);
51594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (unlink(dex_path) < 0) {
516770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey        if (errno != ENOENT) {
517770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey            ALOGE("Couldn't unlink %s: %s\n", dex_path, strerror(errno));
518770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey        }
51994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
52094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    } else {
52194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return 0;
52294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
52394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
52494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
525c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkeyint get_app_size(const char *uuid, const char *pkgname, int userid, int flags,
526c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey        const char *apkpath, const char *libdirpath, const char *fwdlock_apkpath,
527c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey        const char *asecpath, const char *instruction_set, int64_t *_codesize, int64_t *_datasize,
528c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey        int64_t *_cachesize, int64_t* _asecsize) {
52994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    DIR *d;
53094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int dfd;
53194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    struct dirent *de;
53294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    struct stat s;
53394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    char path[PKG_PATH_MAX];
53494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
53594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int64_t codesize = 0;
53694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int64_t datasize = 0;
53794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int64_t cachesize = 0;
53894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int64_t asecsize = 0;
53994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
540d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey    /* count the source apk as code -- but only if it's not
541d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey     * on the /system partition and its not on the sdcard. */
54294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (validate_system_app_path(apkpath) &&
54394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            strncmp(apkpath, android_asec_dir.path, android_asec_dir.len) != 0) {
54494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (stat(apkpath, &s) == 0) {
54594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            codesize += stat_size(&s);
546d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey            if (S_ISDIR(s.st_mode)) {
547d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey                d = opendir(apkpath);
548d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey                if (d != NULL) {
549d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey                    dfd = dirfd(d);
550d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey                    codesize += calculate_dir_size(dfd);
551d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey                    closedir(d);
552d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey                }
553d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey            }
55494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
55594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
556d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey
557d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey    /* count the forward locked apk as code if it is given */
55894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (fwdlock_apkpath != NULL && fwdlock_apkpath[0] != '!') {
55994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (stat(fwdlock_apkpath, &s) == 0) {
56094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            codesize += stat_size(&s);
56194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
56294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
563d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey
564d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey    /* count the cached dexfile as code */
56502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    if (create_cache_path(path, apkpath, instruction_set)) {
56694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (stat(path, &s) == 0) {
56794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            codesize += stat_size(&s);
56894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
56994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
57094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
571d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey    /* add in size of any libraries */
5728b41780d73930b37b6254cc1dac5607c843839c0Dianne Hackborn    if (libdirpath != NULL && libdirpath[0] != '!') {
5738b41780d73930b37b6254cc1dac5607c843839c0Dianne Hackborn        d = opendir(libdirpath);
57494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (d != NULL) {
57594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            dfd = dirfd(d);
57694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            codesize += calculate_dir_size(dfd);
57794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            closedir(d);
57894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
57994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
58094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
581d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey    /* compute asec size if it is given */
58294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (asecpath != NULL && asecpath[0] != '!') {
58394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (stat(asecpath, &s) == 0) {
58494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            asecsize += stat_size(&s);
58594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
58694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
58794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
588d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey    std::vector<userid_t> users;
589d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey    if (userid == -1) {
590d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey        users = get_known_users(uuid);
591d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey    } else {
592d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey        users.push_back(userid);
59394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
59494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
595d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey    for (auto user : users) {
596ebf728fd43ab5c7d11a1f9e5fdc775d6740fae0aJeff Sharkey        // TODO: handle user_de directories
597aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey        if (!(flags & FLAG_STORAGE_CE)) continue;
598c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey
599d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey        std::string _pkgdir(create_data_user_package_path(uuid, user, pkgname));
600d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey        const char* pkgdir = _pkgdir.c_str();
60194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
602d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey        d = opendir(pkgdir);
603d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey        if (d == NULL) {
604d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey            PLOG(WARNING) << "Failed to open " << pkgdir;
605d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey            continue;
606d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey        }
607d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey        dfd = dirfd(d);
608d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey
609d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey        /* most stuff in the pkgdir is data, except for the "cache"
610d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey         * directory and below, which is cache, and the "lib" directory
611d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey         * and below, which is code...
612d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey         */
613d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey        while ((de = readdir(d))) {
614d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey            const char *name = de->d_name;
615d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey
616d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey            if (de->d_type == DT_DIR) {
617d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey                int subfd;
618d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey                int64_t statsize = 0;
619d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey                int64_t dirsize = 0;
620d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey                    /* always skip "." and ".." */
621d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey                if (name[0] == '.') {
622d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey                    if (name[1] == 0) continue;
623d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey                    if ((name[1] == '.') && (name[2] == 0)) continue;
624d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey                }
625d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey                if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
626d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey                    statsize = stat_size(&s);
627d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey                }
628d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey                subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY);
629d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey                if (subfd >= 0) {
630d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey                    dirsize = calculate_dir_size(subfd);
631d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey                }
632d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey                if(!strcmp(name,"lib")) {
633d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey                    codesize += dirsize + statsize;
634d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey                } else if(!strcmp(name,"cache")) {
635d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey                    cachesize += dirsize + statsize;
636d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey                } else {
637d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey                    datasize += dirsize + statsize;
638d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey                }
639d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey            } else if (de->d_type == DT_LNK && !strcmp(name,"lib")) {
640d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey                // This is the symbolic link to the application's library
641d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey                // code.  We'll count this as code instead of data, since
642d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey                // it is not something that the app creates.
643d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey                if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
644d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey                    codesize += stat_size(&s);
645d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey                }
64694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            } else {
647d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey                if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
648d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey                    datasize += stat_size(&s);
649d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey                }
65094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
65194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
652d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey        closedir(d);
65394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
65494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    *_codesize = codesize;
65594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    *_datasize = datasize;
65694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    *_cachesize = cachesize;
65794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    *_asecsize = asecsize;
65894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return 0;
65994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
66094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
661b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Roubanstatic int split_count(const char *str)
662b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban{
663b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban  char *ctx;
664b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban  int count = 0;
66502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe  char buf[kPropertyValueMax];
666b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban
667b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban  strncpy(buf, str, sizeof(buf));
668b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban  char *pBuf = buf;
669b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban
670b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban  while(strtok_r(pBuf, " ", &ctx) != NULL) {
671b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban    count++;
672b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban    pBuf = NULL;
673b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban  }
674b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban
675b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban  return count;
676b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban}
677b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban
67814e084d39ac44e9d4d0a0143bf1f111011343d34neo.chaestatic int split(char *buf, const char **argv)
679b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban{
680b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban  char *ctx;
681b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban  int count = 0;
682b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban  char *tok;
683b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban  char *pBuf = buf;
684b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban
685b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban  while((tok = strtok_r(pBuf, " ", &ctx)) != NULL) {
686b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban    argv[count++] = tok;
687b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban    pBuf = NULL;
688b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban  }
689b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban
690b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban  return count;
691b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban}
692b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban
6937365a10689df23334d245b211ce272502ad20669Alex Lightstatic void run_patchoat(int input_fd, int oat_fd, const char* input_file_name,
69402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    const char* output_file_name, const char *pkgname ATTRIBUTE_UNUSED, const char *instruction_set)
6957365a10689df23334d245b211ce272502ad20669Alex Light{
6967365a10689df23334d245b211ce272502ad20669Alex Light    static const int MAX_INT_LEN = 12;      // '-'+10dig+'\0' -OR- 0x+8dig
6978fc7315a67fdbf9b56cc6fa061f0773be2223a81Calin Juravle    static const unsigned int MAX_INSTRUCTION_SET_LEN = 7;
6987365a10689df23334d245b211ce272502ad20669Alex Light
6997365a10689df23334d245b211ce272502ad20669Alex Light    static const char* PATCHOAT_BIN = "/system/bin/patchoat";
7007365a10689df23334d245b211ce272502ad20669Alex Light    if (strlen(instruction_set) >= MAX_INSTRUCTION_SET_LEN) {
7017365a10689df23334d245b211ce272502ad20669Alex Light        ALOGE("Instruction set %s longer than max length of %d",
7027365a10689df23334d245b211ce272502ad20669Alex Light              instruction_set, MAX_INSTRUCTION_SET_LEN);
7037365a10689df23334d245b211ce272502ad20669Alex Light        return;
7047365a10689df23334d245b211ce272502ad20669Alex Light    }
7057365a10689df23334d245b211ce272502ad20669Alex Light
7067365a10689df23334d245b211ce272502ad20669Alex Light    /* input_file_name/input_fd should be the .odex/.oat file that is precompiled. I think*/
7077365a10689df23334d245b211ce272502ad20669Alex Light    char instruction_set_arg[strlen("--instruction-set=") + MAX_INSTRUCTION_SET_LEN];
7087365a10689df23334d245b211ce272502ad20669Alex Light    char output_oat_fd_arg[strlen("--output-oat-fd=") + MAX_INT_LEN];
7097365a10689df23334d245b211ce272502ad20669Alex Light    char input_oat_fd_arg[strlen("--input-oat-fd=") + MAX_INT_LEN];
7107365a10689df23334d245b211ce272502ad20669Alex Light    const char* patched_image_location_arg = "--patched-image-location=/system/framework/boot.art";
7117365a10689df23334d245b211ce272502ad20669Alex Light    // The caller has already gotten all the locks we need.
7127365a10689df23334d245b211ce272502ad20669Alex Light    const char* no_lock_arg = "--no-lock-output";
7137365a10689df23334d245b211ce272502ad20669Alex Light    sprintf(instruction_set_arg, "--instruction-set=%s", instruction_set);
7147365a10689df23334d245b211ce272502ad20669Alex Light    sprintf(output_oat_fd_arg, "--output-oat-fd=%d", oat_fd);
7157365a10689df23334d245b211ce272502ad20669Alex Light    sprintf(input_oat_fd_arg, "--input-oat-fd=%d", input_fd);
716a7915d437c3971943f82dfc2370fe7a5df522801Alex Light    ALOGV("Running %s isa=%s in-fd=%d (%s) out-fd=%d (%s)\n",
7177365a10689df23334d245b211ce272502ad20669Alex Light          PATCHOAT_BIN, instruction_set, input_fd, input_file_name, oat_fd, output_file_name);
7187365a10689df23334d245b211ce272502ad20669Alex Light
7197365a10689df23334d245b211ce272502ad20669Alex Light    /* patchoat, patched-image-location, no-lock, isa, input-fd, output-fd */
7207365a10689df23334d245b211ce272502ad20669Alex Light    char* argv[7];
7217365a10689df23334d245b211ce272502ad20669Alex Light    argv[0] = (char*) PATCHOAT_BIN;
7227365a10689df23334d245b211ce272502ad20669Alex Light    argv[1] = (char*) patched_image_location_arg;
7237365a10689df23334d245b211ce272502ad20669Alex Light    argv[2] = (char*) no_lock_arg;
7247365a10689df23334d245b211ce272502ad20669Alex Light    argv[3] = instruction_set_arg;
7257365a10689df23334d245b211ce272502ad20669Alex Light    argv[4] = output_oat_fd_arg;
7267365a10689df23334d245b211ce272502ad20669Alex Light    argv[5] = input_oat_fd_arg;
7277365a10689df23334d245b211ce272502ad20669Alex Light    argv[6] = NULL;
7287365a10689df23334d245b211ce272502ad20669Alex Light
7297365a10689df23334d245b211ce272502ad20669Alex Light    execv(PATCHOAT_BIN, (char* const *)argv);
7307365a10689df23334d245b211ce272502ad20669Alex Light    ALOGE("execv(%s) failed: %s\n", PATCHOAT_BIN, strerror(errno));
7317365a10689df23334d245b211ce272502ad20669Alex Light}
7327365a10689df23334d245b211ce272502ad20669Alex Light
733edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartierstatic void run_dex2oat(int zip_fd, int oat_fd, int image_fd, const char* input_file_name,
7346a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        const char* output_file_name, int swap_fd, const char *instruction_set,
7356a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        bool vm_safe_mode, bool debuggable, bool post_bootcomplete, bool extract_only,
7366a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        int profile_fd) {
7378fc7315a67fdbf9b56cc6fa061f0773be2223a81Calin Juravle    static const unsigned int MAX_INSTRUCTION_SET_LEN = 7;
7388fc7315a67fdbf9b56cc6fa061f0773be2223a81Calin Juravle
7398fc7315a67fdbf9b56cc6fa061f0773be2223a81Calin Juravle    if (strlen(instruction_set) >= MAX_INSTRUCTION_SET_LEN) {
7408fc7315a67fdbf9b56cc6fa061f0773be2223a81Calin Juravle        ALOGE("Instruction set %s longer than max length of %d",
7418fc7315a67fdbf9b56cc6fa061f0773be2223a81Calin Juravle              instruction_set, MAX_INSTRUCTION_SET_LEN);
7428fc7315a67fdbf9b56cc6fa061f0773be2223a81Calin Juravle        return;
7438fc7315a67fdbf9b56cc6fa061f0773be2223a81Calin Juravle    }
7448fc7315a67fdbf9b56cc6fa061f0773be2223a81Calin Juravle
74502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    char dex2oat_Xms_flag[kPropertyValueMax];
74602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    bool have_dex2oat_Xms_flag = get_property("dalvik.vm.dex2oat-Xms", dex2oat_Xms_flag, NULL) > 0;
747e46a75a0f6007967cd0d161959af844772cdc330Brian Carlstrom
74802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    char dex2oat_Xmx_flag[kPropertyValueMax];
74902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    bool have_dex2oat_Xmx_flag = get_property("dalvik.vm.dex2oat-Xmx", dex2oat_Xmx_flag, NULL) > 0;
750e46a75a0f6007967cd0d161959af844772cdc330Brian Carlstrom
75102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    char dex2oat_compiler_filter_flag[kPropertyValueMax];
75202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    bool have_dex2oat_compiler_filter_flag = get_property("dalvik.vm.dex2oat-filter",
753cf51ba1360ee13459830a0502b0d454d0145544fBrian Carlstrom                                                          dex2oat_compiler_filter_flag, NULL) > 0;
754cf51ba1360ee13459830a0502b0d454d0145544fBrian Carlstrom
75502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    char dex2oat_threads_buf[kPropertyValueMax];
75602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    bool have_dex2oat_threads_flag = get_property(post_bootcomplete
757919461cbeef3e9f35388fc099a5fcdae1cb79cc6Andreas Gampe                                                      ? "dalvik.vm.dex2oat-threads"
758919461cbeef3e9f35388fc099a5fcdae1cb79cc6Andreas Gampe                                                      : "dalvik.vm.boot-dex2oat-threads",
759919461cbeef3e9f35388fc099a5fcdae1cb79cc6Andreas Gampe                                                  dex2oat_threads_buf,
760919461cbeef3e9f35388fc099a5fcdae1cb79cc6Andreas Gampe                                                  NULL) > 0;
76102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    char dex2oat_threads_arg[kPropertyValueMax + 2];
7628d7af8b2418cc5e7e59746f0cb359a75ed0bdfd1Andreas Gampe    if (have_dex2oat_threads_flag) {
7638d7af8b2418cc5e7e59746f0cb359a75ed0bdfd1Andreas Gampe        sprintf(dex2oat_threads_arg, "-j%s", dex2oat_threads_buf);
7648d7af8b2418cc5e7e59746f0cb359a75ed0bdfd1Andreas Gampe    }
7658d7af8b2418cc5e7e59746f0cb359a75ed0bdfd1Andreas Gampe
76602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    char dex2oat_isa_features_key[kPropertyKeyMax];
7678fc7315a67fdbf9b56cc6fa061f0773be2223a81Calin Juravle    sprintf(dex2oat_isa_features_key, "dalvik.vm.isa.%s.features", instruction_set);
76802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    char dex2oat_isa_features[kPropertyValueMax];
76902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    bool have_dex2oat_isa_features = get_property(dex2oat_isa_features_key,
7708fc7315a67fdbf9b56cc6fa061f0773be2223a81Calin Juravle                                                  dex2oat_isa_features, NULL) > 0;
7718fc7315a67fdbf9b56cc6fa061f0773be2223a81Calin Juravle
77202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    char dex2oat_isa_variant_key[kPropertyKeyMax];
77316a95b267ac6aac75453f65f1bc382ce65c0741eIan Rogers    sprintf(dex2oat_isa_variant_key, "dalvik.vm.isa.%s.variant", instruction_set);
77402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    char dex2oat_isa_variant[kPropertyValueMax];
77502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    bool have_dex2oat_isa_variant = get_property(dex2oat_isa_variant_key,
77616a95b267ac6aac75453f65f1bc382ce65c0741eIan Rogers                                                 dex2oat_isa_variant, NULL) > 0;
77716a95b267ac6aac75453f65f1bc382ce65c0741eIan Rogers
77814e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae    const char *dex2oat_norelocation = "-Xnorelocate";
77914e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae    bool have_dex2oat_relocation_skip_flag = false;
78014e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae
78102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    char dex2oat_flags[kPropertyValueMax];
78202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    int dex2oat_flags_count = get_property("dalvik.vm.dex2oat-flags",
783b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban                                 dex2oat_flags, NULL) <= 0 ? 0 : split_count(dex2oat_flags);
7840ae8e39ebcc26836ba55a5ee4481825a0f473a9eBrian Carlstrom    ALOGV("dalvik.vm.dex2oat-flags=%s\n", dex2oat_flags);
7850ae8e39ebcc26836ba55a5ee4481825a0f473a9eBrian Carlstrom
786538998f204d1e542e235de9e7ce18ef4dc68c9ccBrian Carlstrom    // If we booting without the real /data, don't spend time compiling.
78702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    char vold_decrypt[kPropertyValueMax];
78802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    bool have_vold_decrypt = get_property("vold.decrypt", vold_decrypt, "") > 0;
789538998f204d1e542e235de9e7ce18ef4dc68c9ccBrian Carlstrom    bool skip_compilation = (have_vold_decrypt &&
790538998f204d1e542e235de9e7ce18ef4dc68c9ccBrian Carlstrom                             (strcmp(vold_decrypt, "trigger_restart_min_framework") == 0 ||
791538998f204d1e542e235de9e7ce18ef4dc68c9ccBrian Carlstrom                             (strcmp(vold_decrypt, "1") == 0)));
792538998f204d1e542e235de9e7ce18ef4dc68c9ccBrian Carlstrom
7936a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    bool generate_debug_info = property_get_bool("debug.generate-debug-info");
794d4a7b459f4f383988440c0512513b321ce03fc22Mathieu Chartier
795416fa12cd0949c7f12e2067b36ac7ac64ba70b4aMathieu Chartier    char app_image_format[kPropertyValueMax];
796416fa12cd0949c7f12e2067b36ac7ac64ba70b4aMathieu Chartier    char image_format_arg[strlen("--image-format=") + kPropertyValueMax];
797416fa12cd0949c7f12e2067b36ac7ac64ba70b4aMathieu Chartier    bool have_app_image_format =
79841fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier            image_fd >= 0 && get_property("dalvik.vm.appimageformat", app_image_format, NULL) > 0;
799416fa12cd0949c7f12e2067b36ac7ac64ba70b4aMathieu Chartier    if (have_app_image_format) {
800416fa12cd0949c7f12e2067b36ac7ac64ba70b4aMathieu Chartier        sprintf(image_format_arg, "--image-format=%s", app_image_format);
801416fa12cd0949c7f12e2067b36ac7ac64ba70b4aMathieu Chartier    }
802416fa12cd0949c7f12e2067b36ac7ac64ba70b4aMathieu Chartier
8031705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom    static const char* DEX2OAT_BIN = "/system/bin/dex2oat";
80453e0776d967324e2908e3be56b80cddb2c9d9e03Brian Carlstrom
80553e0776d967324e2908e3be56b80cddb2c9d9e03Brian Carlstrom    static const char* RUNTIME_ARG = "--runtime-arg";
80653e0776d967324e2908e3be56b80cddb2c9d9e03Brian Carlstrom
8071705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom    static const int MAX_INT_LEN = 12;      // '-'+10dig+'\0' -OR- 0x+8dig
8081b4003207750ea8fe8c7b03eb32d80f1df83979eNarayan Kamath
8091705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom    char zip_fd_arg[strlen("--zip-fd=") + MAX_INT_LEN];
8101705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom    char zip_location_arg[strlen("--zip-location=") + PKG_PATH_MAX];
8111705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom    char oat_fd_arg[strlen("--oat-fd=") + MAX_INT_LEN];
8127195fcc2185c35b45a8f28c79fde4999b38850e8Brian Carlstrom    char oat_location_arg[strlen("--oat-location=") + PKG_PATH_MAX];
8131b4003207750ea8fe8c7b03eb32d80f1df83979eNarayan Kamath    char instruction_set_arg[strlen("--instruction-set=") + MAX_INSTRUCTION_SET_LEN];
81402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    char instruction_set_variant_arg[strlen("--instruction-set-variant=") + kPropertyValueMax];
81502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    char instruction_set_features_arg[strlen("--instruction-set-features=") + kPropertyValueMax];
81602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    char dex2oat_Xms_arg[strlen("-Xms") + kPropertyValueMax];
81702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    char dex2oat_Xmx_arg[strlen("-Xmx") + kPropertyValueMax];
81802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    char dex2oat_compiler_filter_arg[strlen("--compiler-filter=") + kPropertyValueMax];
819e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe    bool have_dex2oat_swap_fd = false;
820e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe    char dex2oat_swap_fd[strlen("--swap-fd=") + MAX_INT_LEN];
821edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier    bool have_dex2oat_image_fd = false;
822edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier    char dex2oat_image_fd[strlen("--app-image-fd=") + MAX_INT_LEN];
8231705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom
8241705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom    sprintf(zip_fd_arg, "--zip-fd=%d", zip_fd);
8251705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom    sprintf(zip_location_arg, "--zip-location=%s", input_file_name);
8261705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom    sprintf(oat_fd_arg, "--oat-fd=%d", oat_fd);
8271705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom    sprintf(oat_location_arg, "--oat-location=%s", output_file_name);
8281b4003207750ea8fe8c7b03eb32d80f1df83979eNarayan Kamath    sprintf(instruction_set_arg, "--instruction-set=%s", instruction_set);
82916a95b267ac6aac75453f65f1bc382ce65c0741eIan Rogers    sprintf(instruction_set_variant_arg, "--instruction-set-variant=%s", dex2oat_isa_variant);
8308fc7315a67fdbf9b56cc6fa061f0773be2223a81Calin Juravle    sprintf(instruction_set_features_arg, "--instruction-set-features=%s", dex2oat_isa_features);
831e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe    if (swap_fd >= 0) {
832e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe        have_dex2oat_swap_fd = true;
833e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe        sprintf(dex2oat_swap_fd, "--swap-fd=%d", swap_fd);
834e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe    }
835edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier    if (image_fd >= 0) {
836edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier        have_dex2oat_image_fd = true;
837edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier        sprintf(dex2oat_image_fd, "--app-image-fd=%d", image_fd);
838edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier    }
83957c69c39640a7d6a08b3afb56361d76f7ecf1250Calin Juravle
840e46a75a0f6007967cd0d161959af844772cdc330Brian Carlstrom    if (have_dex2oat_Xms_flag) {
841e46a75a0f6007967cd0d161959af844772cdc330Brian Carlstrom        sprintf(dex2oat_Xms_arg, "-Xms%s", dex2oat_Xms_flag);
842e46a75a0f6007967cd0d161959af844772cdc330Brian Carlstrom    }
843e46a75a0f6007967cd0d161959af844772cdc330Brian Carlstrom    if (have_dex2oat_Xmx_flag) {
844e46a75a0f6007967cd0d161959af844772cdc330Brian Carlstrom        sprintf(dex2oat_Xmx_arg, "-Xmx%s", dex2oat_Xmx_flag);
845e46a75a0f6007967cd0d161959af844772cdc330Brian Carlstrom    }
846538998f204d1e542e235de9e7ce18ef4dc68c9ccBrian Carlstrom    if (skip_compilation) {
847e18987efb5e39ca1bed15527b7b82bde55c99669Brian Carlstrom        strcpy(dex2oat_compiler_filter_arg, "--compiler-filter=verify-none");
848538998f204d1e542e235de9e7ce18ef4dc68c9ccBrian Carlstrom        have_dex2oat_compiler_filter_flag = true;
84914e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae        have_dex2oat_relocation_skip_flag = true;
850b1efac103523efccbe671e76cc0eaaeab810415bCalin Juravle    } else if (vm_safe_mode) {
851b1efac103523efccbe671e76cc0eaaeab810415bCalin Juravle        strcpy(dex2oat_compiler_filter_arg, "--compiler-filter=interpret-only");
85297477d203eaf0c3235bbe2415356f20a0431cadaCalin Juravle        have_dex2oat_compiler_filter_flag = true;
853b0fad6d04ba05fbad5ce31c7382be174ed5a70b1David Brazdil    } else if (extract_only) {
854bb7a77082fde8f73971adadf50faecdf5f660b48Andreas Gampe        // Temporarily make extract-only mean interpret-only, so extracted files will be verified.
855bb7a77082fde8f73971adadf50faecdf5f660b48Andreas Gampe        // b/26833007
856bb7a77082fde8f73971adadf50faecdf5f660b48Andreas Gampe        strcpy(dex2oat_compiler_filter_arg, "--compiler-filter=interpret-only");
857d4a7b459f4f383988440c0512513b321ce03fc22Mathieu Chartier        have_dex2oat_compiler_filter_flag = true;
858538998f204d1e542e235de9e7ce18ef4dc68c9ccBrian Carlstrom    } else if (have_dex2oat_compiler_filter_flag) {
859cf51ba1360ee13459830a0502b0d454d0145544fBrian Carlstrom        sprintf(dex2oat_compiler_filter_arg, "--compiler-filter=%s", dex2oat_compiler_filter_flag);
860cf51ba1360ee13459830a0502b0d454d0145544fBrian Carlstrom    }
861e46a75a0f6007967cd0d161959af844772cdc330Brian Carlstrom
862598c25e23f7c97470e09a2316513ddf2efdfb670Andreas Gampe    // Check whether all apps should be compiled debuggable.
863598c25e23f7c97470e09a2316513ddf2efdfb670Andreas Gampe    if (!debuggable) {
86402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        char prop_buf[kPropertyValueMax];
865598c25e23f7c97470e09a2316513ddf2efdfb670Andreas Gampe        debuggable =
86602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe                (get_property("dalvik.vm.always_debuggable", prop_buf, "0") > 0) &&
867598c25e23f7c97470e09a2316513ddf2efdfb670Andreas Gampe                (prop_buf[0] == '1');
868598c25e23f7c97470e09a2316513ddf2efdfb670Andreas Gampe    }
8696a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    char profile_arg[strlen("--profile-file-fd=") + MAX_INT_LEN];
8706a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    if (profile_fd != -1) {
8716a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        sprintf(profile_arg, "--profile-file-fd=%d", profile_fd);
87260a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle    }
873598c25e23f7c97470e09a2316513ddf2efdfb670Andreas Gampe
8746a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle
8751705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom    ALOGV("Running %s in=%s out=%s\n", DEX2OAT_BIN, input_file_name, output_file_name);
8764fdff4616699708e58d875273c3c1a85ba4c63bdCalin Juravle
87714e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae    const char* argv[7  // program name, mandatory arguments and the final NULL
87814e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae                     + (have_dex2oat_isa_variant ? 1 : 0)
87914e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae                     + (have_dex2oat_isa_features ? 1 : 0)
88014e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae                     + (have_dex2oat_Xms_flag ? 2 : 0)
88114e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae                     + (have_dex2oat_Xmx_flag ? 2 : 0)
88214e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae                     + (have_dex2oat_compiler_filter_flag ? 1 : 0)
8838d7af8b2418cc5e7e59746f0cb359a75ed0bdfd1Andreas Gampe                     + (have_dex2oat_threads_flag ? 1 : 0)
88414e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae                     + (have_dex2oat_swap_fd ? 1 : 0)
885edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier                     + (have_dex2oat_image_fd ? 1 : 0)
88614e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae                     + (have_dex2oat_relocation_skip_flag ? 2 : 0)
887528c8ddc60df7b3d291b389117e4db878a20ad48David Srbecky                     + (generate_debug_info ? 1 : 0)
888598c25e23f7c97470e09a2316513ddf2efdfb670Andreas Gampe                     + (debuggable ? 1 : 0)
889416fa12cd0949c7f12e2067b36ac7ac64ba70b4aMathieu Chartier                     + (have_app_image_format ? 1 : 0)
89060a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle                     + dex2oat_flags_count
8916a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle                     + (profile_fd == -1 ? 0 : 1)];
8924fdff4616699708e58d875273c3c1a85ba4c63bdCalin Juravle    int i = 0;
89314e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae    argv[i++] = DEX2OAT_BIN;
8944fdff4616699708e58d875273c3c1a85ba4c63bdCalin Juravle    argv[i++] = zip_fd_arg;
8954fdff4616699708e58d875273c3c1a85ba4c63bdCalin Juravle    argv[i++] = zip_location_arg;
8964fdff4616699708e58d875273c3c1a85ba4c63bdCalin Juravle    argv[i++] = oat_fd_arg;
8974fdff4616699708e58d875273c3c1a85ba4c63bdCalin Juravle    argv[i++] = oat_location_arg;
8984fdff4616699708e58d875273c3c1a85ba4c63bdCalin Juravle    argv[i++] = instruction_set_arg;
89916a95b267ac6aac75453f65f1bc382ce65c0741eIan Rogers    if (have_dex2oat_isa_variant) {
90016a95b267ac6aac75453f65f1bc382ce65c0741eIan Rogers        argv[i++] = instruction_set_variant_arg;
90116a95b267ac6aac75453f65f1bc382ce65c0741eIan Rogers    }
9028fc7315a67fdbf9b56cc6fa061f0773be2223a81Calin Juravle    if (have_dex2oat_isa_features) {
9038fc7315a67fdbf9b56cc6fa061f0773be2223a81Calin Juravle        argv[i++] = instruction_set_features_arg;
9048fc7315a67fdbf9b56cc6fa061f0773be2223a81Calin Juravle    }
905e46a75a0f6007967cd0d161959af844772cdc330Brian Carlstrom    if (have_dex2oat_Xms_flag) {
90614e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae        argv[i++] = RUNTIME_ARG;
907e46a75a0f6007967cd0d161959af844772cdc330Brian Carlstrom        argv[i++] = dex2oat_Xms_arg;
908e46a75a0f6007967cd0d161959af844772cdc330Brian Carlstrom    }
909e46a75a0f6007967cd0d161959af844772cdc330Brian Carlstrom    if (have_dex2oat_Xmx_flag) {
91014e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae        argv[i++] = RUNTIME_ARG;
911e46a75a0f6007967cd0d161959af844772cdc330Brian Carlstrom        argv[i++] = dex2oat_Xmx_arg;
912e46a75a0f6007967cd0d161959af844772cdc330Brian Carlstrom    }
913cf51ba1360ee13459830a0502b0d454d0145544fBrian Carlstrom    if (have_dex2oat_compiler_filter_flag) {
914cf51ba1360ee13459830a0502b0d454d0145544fBrian Carlstrom        argv[i++] = dex2oat_compiler_filter_arg;
915cf51ba1360ee13459830a0502b0d454d0145544fBrian Carlstrom    }
9168d7af8b2418cc5e7e59746f0cb359a75ed0bdfd1Andreas Gampe    if (have_dex2oat_threads_flag) {
9178d7af8b2418cc5e7e59746f0cb359a75ed0bdfd1Andreas Gampe        argv[i++] = dex2oat_threads_arg;
9188d7af8b2418cc5e7e59746f0cb359a75ed0bdfd1Andreas Gampe    }
919e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe    if (have_dex2oat_swap_fd) {
920e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe        argv[i++] = dex2oat_swap_fd;
921e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe    }
922edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier    if (have_dex2oat_image_fd) {
923edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier        argv[i++] = dex2oat_image_fd;
924edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier    }
925528c8ddc60df7b3d291b389117e4db878a20ad48David Srbecky    if (generate_debug_info) {
926528c8ddc60df7b3d291b389117e4db878a20ad48David Srbecky        argv[i++] = "--generate-debug-info";
9273822b8b79cf533ee8573794176838406c32a1c20Andreas Gampe    }
928598c25e23f7c97470e09a2316513ddf2efdfb670Andreas Gampe    if (debuggable) {
929598c25e23f7c97470e09a2316513ddf2efdfb670Andreas Gampe        argv[i++] = "--debuggable";
930598c25e23f7c97470e09a2316513ddf2efdfb670Andreas Gampe    }
931416fa12cd0949c7f12e2067b36ac7ac64ba70b4aMathieu Chartier    if (have_app_image_format) {
932416fa12cd0949c7f12e2067b36ac7ac64ba70b4aMathieu Chartier        argv[i++] = image_format_arg;
933416fa12cd0949c7f12e2067b36ac7ac64ba70b4aMathieu Chartier    }
934b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban    if (dex2oat_flags_count) {
935b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban        i += split(dex2oat_flags, argv + i);
9364fdff4616699708e58d875273c3c1a85ba4c63bdCalin Juravle    }
93714e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae    if (have_dex2oat_relocation_skip_flag) {
93814e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae        argv[i++] = RUNTIME_ARG;
93914e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae        argv[i++] = dex2oat_norelocation;
94014e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae    }
9416a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    if (profile_fd != -1) {
9426a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        argv[i++] = profile_arg;
94360a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle    }
944e46a75a0f6007967cd0d161959af844772cdc330Brian Carlstrom    // Do not add after dex2oat_flags, they should override others for debugging.
9454fdff4616699708e58d875273c3c1a85ba4c63bdCalin Juravle    argv[i] = NULL;
9464fdff4616699708e58d875273c3c1a85ba4c63bdCalin Juravle
94714e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae    execv(DEX2OAT_BIN, (char * const *)argv);
948b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban    ALOGE("execv(%s) failed: %s\n", DEX2OAT_BIN, strerror(errno));
9491705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom}
9501705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom
951e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe/*
952c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe * Whether dexopt should use a swap file when compiling an APK.
953c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe *
954c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe * If kAlwaysProvideSwapFile, do this on all devices (dex2oat will make a more informed decision
955c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe * itself, anyways).
956c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe *
957c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe * Otherwise, read "dalvik.vm.dex2oat-swap". If the property exists, return whether it is "true".
958c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe *
959c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe * Otherwise, return true if this is a low-mem device.
960c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe *
961c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe * Otherwise, return default value.
962e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe */
963c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampestatic bool kAlwaysProvideSwapFile = false;
964c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampestatic bool kDefaultProvideSwapFile = true;
965e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe
966e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampestatic bool ShouldUseSwapFileForDexopt() {
967e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe    if (kAlwaysProvideSwapFile) {
968e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe        return true;
969e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe    }
970e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe
971c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe    // Check the "override" property. If it exists, return value == "true".
97202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    char dex2oat_prop_buf[kPropertyValueMax];
97302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    if (get_property("dalvik.vm.dex2oat-swap", dex2oat_prop_buf, "") > 0) {
974c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe        if (strcmp(dex2oat_prop_buf, "true") == 0) {
975c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe            return true;
976c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe        } else {
977c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe            return false;
978c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe        }
979c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe    }
980c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe
981c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe    // Shortcut for default value. This is an implementation optimization for the process sketched
982c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe    // above. If the default value is true, we can avoid to check whether this is a low-mem device,
983c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe    // as low-mem is never returning false. The compiler will optimize this away if it can.
984c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe    if (kDefaultProvideSwapFile) {
985c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe        return true;
986c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe    }
987c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe
9886a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    bool is_low_mem = property_get_bool("ro.config.low_ram");
989c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe    if (is_low_mem) {
990c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe        return true;
991c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe    }
992c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe
993c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe    // Default value must be false here.
994c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe    return kDefaultProvideSwapFile;
995e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe}
996e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe
99794dd3d3c5be4de57278ee668218a3464585795c0Andreas Gampestatic void SetDex2OatAndPatchOatScheduling(bool set_to_bg) {
99894dd3d3c5be4de57278ee668218a3464585795c0Andreas Gampe    if (set_to_bg) {
99994dd3d3c5be4de57278ee668218a3464585795c0Andreas Gampe        if (set_sched_policy(0, SP_BACKGROUND) < 0) {
100094dd3d3c5be4de57278ee668218a3464585795c0Andreas Gampe            ALOGE("set_sched_policy failed: %s\n", strerror(errno));
100194dd3d3c5be4de57278ee668218a3464585795c0Andreas Gampe            exit(70);
100294dd3d3c5be4de57278ee668218a3464585795c0Andreas Gampe        }
100394dd3d3c5be4de57278ee668218a3464585795c0Andreas Gampe        if (setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND) < 0) {
100494dd3d3c5be4de57278ee668218a3464585795c0Andreas Gampe            ALOGE("setpriority failed: %s\n", strerror(errno));
100594dd3d3c5be4de57278ee668218a3464585795c0Andreas Gampe            exit(71);
100694dd3d3c5be4de57278ee668218a3464585795c0Andreas Gampe        }
100794dd3d3c5be4de57278ee668218a3464585795c0Andreas Gampe    }
100894dd3d3c5be4de57278ee668218a3464585795c0Andreas Gampe}
100994dd3d3c5be4de57278ee668218a3464585795c0Andreas Gampe
10106a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic void close_all_fds(const std::vector<fd_t>& fds, const char* description) {
101160a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle    for (size_t i = 0; i < fds.size(); i++) {
101260a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle        if (close(fds[i]) != 0) {
101360a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle            PLOG(WARNING) << "Failed to close fd for " << description << " at index " << i;
101460a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle        }
101560a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle    }
101660a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle}
101760a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle
10186a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic fd_t open_profile_dir(const std::string& profile_dir) {
101960a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle    struct stat buffer;
10206a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    if (TEMP_FAILURE_RETRY(lstat(profile_dir.c_str(), &buffer)) == -1) {
10216a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        PLOG(ERROR) << "Failed to lstat profile_dir: " << profile_dir;
10226a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        return -1;
102360a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle    }
102460a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle
10256a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    fd_t profile_dir_fd = TEMP_FAILURE_RETRY(open(profile_dir.c_str(),
10266a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle            O_PATH | O_CLOEXEC | O_DIRECTORY | O_NOFOLLOW));
10276a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    if (profile_dir_fd < 0) {
10286a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        PLOG(ERROR) << "Failed to open profile_dir: " << profile_dir;
102960a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle    }
10306a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    return profile_dir_fd;
103160a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle}
103260a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle
10336a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic fd_t open_primary_profile_file_from_dir(const std::string& profile_dir, mode_t open_mode) {
10346a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    fd_t profile_dir_fd  = open_profile_dir(profile_dir);
10356a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    if (profile_dir_fd < 0) {
10366a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        return -1;
10376a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    }
10386a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle
10396a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    fd_t profile_fd = -1;
10406a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    std::string profile_file = create_primary_profile(profile_dir);
10416a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle
10426a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    profile_fd = TEMP_FAILURE_RETRY(open(profile_file.c_str(), open_mode | O_NOFOLLOW));
10436a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    if (profile_fd == -1) {
10446a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        // It's not an error if the profile file does not exist.
104560a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle        if (errno != ENOENT) {
10466a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle            PLOG(ERROR) << "Failed to lstat profile_dir: " << profile_dir;
104760a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle        }
104860a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle    }
10496a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    // TODO(calin): use AutoCloseFD instead of closing the fd manually.
10506a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    if (close(profile_dir_fd) != 0) {
10516a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        PLOG(WARNING) << "Could not close profile dir " << profile_dir;
10526a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    }
10536a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    return profile_fd;
10546a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle}
105560a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle
10566a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic fd_t open_primary_profile_file(userid_t user, const char* pkgname) {
10576a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    std::string profile_dir = create_data_user_profile_package_path(user, pkgname);
10586a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    return open_primary_profile_file_from_dir(profile_dir, O_RDONLY);
10596a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle}
10606a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle
10616a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic fd_t open_reference_profile(uid_t uid, const char* pkgname, bool read_write) {
10626a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    std::string reference_profile_dir = create_data_ref_profile_package_path(pkgname);
10636a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    int flags = read_write ? O_RDWR | O_CREAT : O_RDONLY;
10646a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    fd_t fd = open_primary_profile_file_from_dir(reference_profile_dir, flags);
10656a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    if (fd < 0) {
10666a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        return -1;
106760a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle    }
10686a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    if (read_write) {
10696a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        // Fix the owner.
10706a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        if (fchown(fd, uid, uid) < 0) {
10716a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle            close(fd);
10726a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle            return -1;
10736a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        }
10746a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    }
10756a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    return fd;
10766a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle}
107760a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle
10786a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic void open_profile_files(uid_t uid, const char* pkgname,
10796a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle            /*out*/ std::vector<fd_t>* profiles_fd, /*out*/ fd_t* reference_profile_fd) {
10806a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    // Open the reference profile in read-write mode as profman might need to save the merge.
10816a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    *reference_profile_fd = open_reference_profile(uid, pkgname, /*read_write*/ true);
108260a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle    if (*reference_profile_fd < 0) {
10836a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        // We can't access the reference profile file.
108460a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle        return;
108560a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle    }
10866a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle
10876a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    std::vector<userid_t> users = get_known_users(/*volume_uuid*/ nullptr);
10886a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    for (auto user : users) {
10896a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        fd_t profile_fd = open_primary_profile_file(user, pkgname);
10906a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        // Add to the lists only if both fds are valid.
10916a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        if (profile_fd >= 0) {
10926a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle            profiles_fd->push_back(profile_fd);
10936a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        }
10946a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    }
10956a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle}
10966a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle
10976a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic void drop_capabilities(uid_t uid) {
10986a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    if (setgid(uid) != 0) {
10996a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        ALOGE("setgid(%d) failed in installd during dexopt\n", uid);
11006a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        exit(64);
11016a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    }
11026a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    if (setuid(uid) != 0) {
11036a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        ALOGE("setuid(%d) failed in installd during dexopt\n", uid);
11046a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        exit(65);
11056a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    }
11066a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    // drop capabilities
11076a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    struct __user_cap_header_struct capheader;
11086a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    struct __user_cap_data_struct capdata[2];
11096a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    memset(&capheader, 0, sizeof(capheader));
11106a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    memset(&capdata, 0, sizeof(capdata));
11116a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    capheader.version = _LINUX_CAPABILITY_VERSION_3;
11126a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    if (capset(&capheader, &capdata[0]) < 0) {
11136a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        ALOGE("capset failed: %s\n", strerror(errno));
11146a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        exit(66);
111560a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle    }
111660a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle}
111760a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle
11186a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic constexpr int PROFMAN_BIN_RETURN_CODE_COMPILE = 0;
11196a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic constexpr int PROFMAN_BIN_RETURN_CODE_SKIP_COMPILATION = 1;
11206a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic constexpr int PROFMAN_BIN_RETURN_CODE_BAD_PROFILES = 2;
11216a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic constexpr int PROFMAN_BIN_RETURN_CODE_ERROR_IO = 3;
11226a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic constexpr int PROFMAN_BIN_RETURN_CODE_ERROR_LOCKING = 4;
11236a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle
11246a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic void run_profman(const std::vector<fd_t>& profiles_fd, fd_t reference_profile_fd) {
11256a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    static const size_t MAX_INT_LEN = 32;
11266a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    static const char* PROFMAN_BIN = "/system/bin/profman";
11276a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle
11286a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    std::vector<std::string> profile_args(profiles_fd.size());
11296a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    char profile_buf[strlen("--profile-file-fd=") + MAX_INT_LEN];
11306a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    for (size_t k = 0; k < profiles_fd.size(); k++) {
11316a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        sprintf(profile_buf, "--profile-file-fd=%d", profiles_fd[k]);
11326a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        profile_args[k].assign(profile_buf);
11336a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    }
11346a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    char reference_profile_arg[strlen("--reference-profile-file-fd=") + MAX_INT_LEN];
11356a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    sprintf(reference_profile_arg, "--reference-profile-file-fd=%d", reference_profile_fd);
11366a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle
11376a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    // program name, reference profile fd, the final NULL and the profile fds
11386a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    const char* argv[3 + profiles_fd.size()];
11396a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    int i = 0;
11406a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    argv[i++] = PROFMAN_BIN;
11416a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    argv[i++] = reference_profile_arg;
11426a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    for (size_t k = 0; k < profile_args.size(); k++) {
11436a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        argv[i++] = profile_args[k].c_str();
11446a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    }
11456a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    // Do not add after dex2oat_flags, they should override others for debugging.
11466a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    argv[i] = NULL;
11476a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle
11486a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    execv(PROFMAN_BIN, (char * const *)argv);
11496a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    ALOGE("execv(%s) failed: %s\n", PROFMAN_BIN, strerror(errno));
11506a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    exit(68);   /* only get here on exec failure */
11516a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle}
11526a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle
11536a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle// Decides if profile guided compilation is needed or not based on existing profiles.
11546a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle// Returns true if there is enough information in the current profiles that worth
11556a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle// a re-compilation of the package.
11566a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle// If the return value is true all the current profiles would have been merged into
11576a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle// the reference profiles accessible with open_reference_profile().
11586a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic bool analyse_profiles(uid_t uid, const char* pkgname) {
11596a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    std::vector<fd_t> profiles_fd;
11606a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    fd_t reference_profile_fd = -1;
11616a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    open_profile_files(uid, pkgname, &profiles_fd, &reference_profile_fd);
11626a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    if (profiles_fd.empty() || (reference_profile_fd == -1)) {
11636a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        // Skip profile guided compilation because no profiles were found.
11646a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        // Or if the reference profile info couldn't be opened.
11656a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        close_all_fds(profiles_fd, "profiles_fd");
11666a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        if ((reference_profile_fd != - 1) && (close(reference_profile_fd) != 0)) {
11676a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle            PLOG(WARNING) << "Failed to close fd for reference profile";
116860a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle        }
11696a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        return false;
11706a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    }
117160a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle
11726a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    ALOGV("PROFMAN: --- BEGIN '%s' ---\n", pkgname);
11736a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle
11746a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    pid_t pid = fork();
11756a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    if (pid == 0) {
11766a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        /* child -- drop privileges before continuing */
11776a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        drop_capabilities(uid);
11786a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        run_profman(profiles_fd, reference_profile_fd);
11796a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        exit(68);   /* only get here on exec failure */
11806a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    }
11816a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    /* parent */
11826a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    int return_code = wait_child(pid);
11836a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    bool need_to_compile = false;
11846a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    bool delete_current_profiles = false;
11856a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    bool delete_reference_profile = false;
11866a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    if (!WIFEXITED(return_code)) {
11876a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        LOG(WARNING) << "profman failed for package " << pkgname << ": " << return_code;
11886a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    } else {
11896a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        return_code = WEXITSTATUS(return_code);
11906a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        switch (return_code) {
11916a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle            case PROFMAN_BIN_RETURN_CODE_COMPILE:
11926a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle                need_to_compile = true;
11936a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle                delete_current_profiles = true;
11946a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle                delete_reference_profile = false;
11956a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle                break;
11966a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle            case PROFMAN_BIN_RETURN_CODE_SKIP_COMPILATION:
11976a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle                need_to_compile = false;
11986a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle                delete_current_profiles = false;
11996a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle                delete_reference_profile = false;
12006a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle                break;
12016a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle            case PROFMAN_BIN_RETURN_CODE_BAD_PROFILES:
12026a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle                LOG(WARNING) << "Bad profiles for package " << pkgname;
12036a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle                need_to_compile = false;
12046a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle                delete_current_profiles = true;
12056a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle                delete_reference_profile = true;
12066a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle                break;
12076a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle            case PROFMAN_BIN_RETURN_CODE_ERROR_IO:  // fall-through
12086a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle            case PROFMAN_BIN_RETURN_CODE_ERROR_LOCKING:
12096a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle                // Temporary IO problem (e.g. locking). Ignore but log a warning.
12106a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle                LOG(WARNING) << "IO error while reading profiles for package " << pkgname;
12116a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle                need_to_compile = false;
12126a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle                delete_current_profiles = false;
12136a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle                delete_reference_profile = false;
12146a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle                break;
12156a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle           default:
12166a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle                // Unknown return code or error. Unlink profiles.
12176a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle                LOG(WARNING) << "Unknown error code while processing profiles for package " << pkgname
12186a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle                        << ": " << return_code;
12196a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle                need_to_compile = false;
12206a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle                delete_current_profiles = true;
12216a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle                delete_reference_profile = true;
12226a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle                break;
122360a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle        }
122460a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle    }
12256a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    close_all_fds(profiles_fd, "profiles_fd");
12266a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    if (close(reference_profile_fd) != 0) {
12276a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        PLOG(WARNING) << "Failed to close fd for reference profile";
12286a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    }
12296a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    if (delete_current_profiles) {
12306a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        unlink_current_profiles(pkgname);
12316a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    }
12326a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    if (delete_reference_profile) {
12336a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        unlink_reference_profile(pkgname);
12346a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    }
12356a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    return need_to_compile;
123660a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle}
123760a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle
1238edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartierstatic void trim_extension(char* path) {
1239edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier  // Trim the extension.
1240edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier  int pos = strlen(path);
1241edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier  for (; pos >= 0 && path[pos] != '.'; --pos) {}
1242edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier  if (pos >= 0) {
1243edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier      path[pos] = '\0';  // Trim extension
1244edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier  }
1245edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier}
1246edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier
124741fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartierstatic bool add_extension_to_file_name(char* file_name, const char* extension) {
124841fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier    if (strlen(file_name) + strlen(extension) + 1 > PKG_PATH_MAX) {
124941fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier        return false;
1250edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier    }
125141fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier    strcat(file_name, extension);
125241fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier    return true;
125341fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier}
125441fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier
125541fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartierstatic int open_output_file(char* file_name, bool recreate) {
125641fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier    int flags = O_RDWR | O_CREAT;
125741fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier    if (recreate) {
125841fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier        unlink(file_name);
125941fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier        flags |= O_EXCL;
126041fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier    }
126141fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier    return open(file_name, flags, 0600);
1262edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier}
1263edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier
1264edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartierstatic bool set_permissions_and_ownership(int fd, bool is_public, int uid, const char* path) {
1265edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier    if (fchmod(fd,
1266edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier               S_IRUSR|S_IWUSR|S_IRGRP |
1267edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier               (is_public ? S_IROTH : 0)) < 0) {
1268edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier        ALOGE("installd cannot chmod '%s' during dexopt\n", path);
1269edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier        return false;
1270edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier    } else if (fchown(fd, AID_SYSTEM, uid) < 0) {
1271edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier        ALOGE("installd cannot chown '%s' during dexopt\n", path);
1272edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier        return false;
1273edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier    }
1274edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier    return true;
1275edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier}
1276edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier
12776a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic bool create_oat_out_path(const char* apk_path, const char* instruction_set,
12786a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle            const char* oat_dir, /*out*/ char* out_path) {
12796a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    // Early best-effort check whether we can fit the the path into our buffers.
12806a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    // Note: the cache path will require an additional 5 bytes for ".swap", but we'll try to run
12816a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    // without a swap file, if necessary. Reference profiles file also add an extra ".prof"
12826a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    // extension to the cache path (5 bytes).
12836a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    if (strlen(apk_path) >= (PKG_PATH_MAX - 8)) {
12846a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        ALOGE("apk_path too long '%s'\n", apk_path);
12856a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        return false;
12866a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    }
12876a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle
12886a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    if (oat_dir != NULL && oat_dir[0] != '!') {
12896a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        if (validate_apk_path(oat_dir)) {
12906a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle            ALOGE("invalid oat_dir '%s'\n", oat_dir);
12916a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle            return false;
12926a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        }
12936a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        if (!calculate_oat_file_path(out_path, oat_dir, apk_path, instruction_set)) {
12946a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle            return false;
12956a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        }
12966a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    } else {
12976a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        if (!create_cache_path(out_path, apk_path, instruction_set)) {
12986a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle            return false;
12996a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        }
13006a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    }
13016a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    return true;
13026a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle}
13036a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle
130460a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravleint dexopt(const char* apk_path, uid_t uid, const char* pkgname, const char* instruction_set,
13056a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle           int dexopt_needed, const char* oat_dir, int dexopt_flags,
13066a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle           const char* volume_uuid ATTRIBUTE_UNUSED, bool use_profiles)
130794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
130894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    struct utimbuf ut;
130926ff93c1a783d02c177a47f0a80249070fe43682Fyodor Kupolov    struct stat input_stat;
13101705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom    char out_path[PKG_PATH_MAX];
1311e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe    char swap_file_name[PKG_PATH_MAX];
1312edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier    char image_path[PKG_PATH_MAX];
13137365a10689df23334d245b211ce272502ad20669Alex Light    const char *input_file;
13147365a10689df23334d245b211ce272502ad20669Alex Light    char in_odex_path[PKG_PATH_MAX];
13156a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    int res;
13166a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    fd_t input_fd=-1, out_fd=-1, image_fd=-1, swap_fd=-1;
1317db0326a56ed8cc1ba2ed5ec2810afe49bcd9a596Calin Juravle    bool is_public = ((dexopt_flags & DEXOPT_PUBLIC) != 0);
131876e767ca14bcbb4bc809cd1279ece82a3aabe8a4Todd Kennedy    bool vm_safe_mode = (dexopt_flags & DEXOPT_SAFEMODE) != 0;
131976e767ca14bcbb4bc809cd1279ece82a3aabe8a4Todd Kennedy    bool debuggable = (dexopt_flags & DEXOPT_DEBUGGABLE) != 0;
132076e767ca14bcbb4bc809cd1279ece82a3aabe8a4Todd Kennedy    bool boot_complete = (dexopt_flags & DEXOPT_BOOTCOMPLETE) != 0;
1321b0fad6d04ba05fbad5ce31c7382be174ed5a70b1David Brazdil    bool extract_only = (dexopt_flags & DEXOPT_EXTRACTONLY) != 0;
13226a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    fd_t reference_profile_fd = -1;
1323db0326a56ed8cc1ba2ed5ec2810afe49bcd9a596Calin Juravle
1324db0326a56ed8cc1ba2ed5ec2810afe49bcd9a596Calin Juravle    if (is_public && use_profiles) {
1325db0326a56ed8cc1ba2ed5ec2810afe49bcd9a596Calin Juravle        // We should not give public access to apks compiled with profile information.
1326db0326a56ed8cc1ba2ed5ec2810afe49bcd9a596Calin Juravle        // Log an error and return early if are asked to do so.
1327db0326a56ed8cc1ba2ed5ec2810afe49bcd9a596Calin Juravle        ALOGE("use_profiles should not be used with is_public.");
1328db0326a56ed8cc1ba2ed5ec2810afe49bcd9a596Calin Juravle        return -1;
1329db0326a56ed8cc1ba2ed5ec2810afe49bcd9a596Calin Juravle    }
1330db0326a56ed8cc1ba2ed5ec2810afe49bcd9a596Calin Juravle
133160a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle    if (use_profiles) {
13326a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        if (analyse_profiles(uid, pkgname)) {
13336a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle            // Open again reference profile in read only mode as dex2oat does not get write
13346a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle            // permissions.
13356a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle            reference_profile_fd = open_reference_profile(uid, pkgname, /*read_write*/ false);
13366a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle            if (reference_profile_fd == -1) {
13376a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle                PLOG(WARNING) << "Couldn't open reference profile in read only mode " << pkgname;
13386a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle                exit(72);
13396a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle            }
13406a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        } else {
13416a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle            // No need to (re)compile. Return early.
134260a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle            return 0;
134360a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle        }
134460a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle    }
134576e767ca14bcbb4bc809cd1279ece82a3aabe8a4Todd Kennedy
1346e296e00df65461629a20cd03b5b041a59935d00fTodd Kennedy    if ((dexopt_flags & ~DEXOPT_MASK) != 0) {
134776e767ca14bcbb4bc809cd1279ece82a3aabe8a4Todd Kennedy        LOG_FATAL("dexopt flags contains unknown fields\n");
134876e767ca14bcbb4bc809cd1279ece82a3aabe8a4Todd Kennedy    }
134994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
13506a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    if (!create_oat_out_path(apk_path, instruction_set, oat_dir, out_path)) {
13516a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        return false;
135294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
135394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
1354c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler    switch (dexopt_needed) {
1355c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler        case DEXOPT_DEX2OAT_NEEDED:
1356c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler            input_file = apk_path;
1357c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler            break;
1358c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler
1359c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler        case DEXOPT_PATCHOAT_NEEDED:
1360c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler            if (!calculate_odex_file_path(in_odex_path, apk_path, instruction_set)) {
1361c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler                return -1;
1362c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler            }
1363c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler            input_file = in_odex_path;
1364c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler            break;
1365c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler
1366c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler        case DEXOPT_SELF_PATCHOAT_NEEDED:
1367c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler            input_file = out_path;
1368c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler            break;
1369c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler
1370c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler        default:
1371c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler            ALOGE("Invalid dexopt needed: %d\n", dexopt_needed);
1372c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler            exit(72);
13737365a10689df23334d245b211ce272502ad20669Alex Light    }
13747365a10689df23334d245b211ce272502ad20669Alex Light
13757365a10689df23334d245b211ce272502ad20669Alex Light    memset(&input_stat, 0, sizeof(input_stat));
13767365a10689df23334d245b211ce272502ad20669Alex Light    stat(input_file, &input_stat);
137794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
13787365a10689df23334d245b211ce272502ad20669Alex Light    input_fd = open(input_file, O_RDONLY, 0);
13797365a10689df23334d245b211ce272502ad20669Alex Light    if (input_fd < 0) {
13807365a10689df23334d245b211ce272502ad20669Alex Light        ALOGE("installd cannot open '%s' for input during dexopt\n", input_file);
138194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
138294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
138394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
13841705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom    unlink(out_path);
13851705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom    out_fd = open(out_path, O_RDWR | O_CREAT | O_EXCL, 0644);
13861705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom    if (out_fd < 0) {
13871705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom        ALOGE("installd cannot open '%s' for output during dexopt\n", out_path);
138894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        goto fail;
138994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
1390edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier    if (!set_permissions_and_ownership(out_fd, is_public, uid, out_path)) {
139194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        goto fail;
139294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
139394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
1394e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe    // Create a swap file if necessary.
1395c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler    if (ShouldUseSwapFileForDexopt()) {
1396e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe        // Make sure there really is enough space.
1397edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier        strcpy(swap_file_name, out_path);
139841fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier        if (add_extension_to_file_name(swap_file_name, ".swap")) {
139941fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier            swap_fd = open_output_file(swap_file_name, /*recreate*/true);
140041fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier        }
1401edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier        if (swap_fd < 0) {
1402edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier            // Could not create swap file. Optimistically go on and hope that we can compile
1403edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier            // without it.
1404edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier            ALOGE("installd could not create '%s' for swap during dexopt\n", swap_file_name);
1405e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe        } else {
1406edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier            // Immediately unlink. We don't really want to hit flash.
1407edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier            unlink(swap_file_name);
1408e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe        }
1409e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe    }
1410d93707342a61e66bc3eb2145628158452f577f42Dave Allison
1411e80d58db65d29ed4647e631a81aed644fea00913Mathieu Chartier    // Avoid generating an app image for extract only since it will not contain any classes.
141241fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier    strcpy(image_path, out_path);
141341fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier    trim_extension(image_path);
141441fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier    if (add_extension_to_file_name(image_path, ".art")) {
141541fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier      char app_image_format[kPropertyValueMax];
141641fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier      bool have_app_image_format =
141741fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier              get_property("dalvik.vm.appimageformat", app_image_format, NULL) > 0;
141841fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier      if (!extract_only && have_app_image_format) {
141941fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier          // Recreate is false since we want to avoid deleting the image in case dex2oat decides to
142041fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier          // not compile anything.
142141fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier          image_fd = open_output_file(image_path, /*recreate*/false);
142241fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier          if (image_fd < 0) {
142341fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier              // Could not create application image file. Go on since we can compile without it.
142441fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier              ALOGE("installd could not create '%s' for image file during dexopt\n", image_path);
142541fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier          } else if (!set_permissions_and_ownership(image_fd, is_public, uid, image_path)) {
142641fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier              image_fd = -1;
142741fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier          }
142841fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier      }
142941fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier      // If we have a valid image file path but no image fd, erase the image file.
1430e80d58db65d29ed4647e631a81aed644fea00913Mathieu Chartier      if (image_fd < 0) {
143141fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier          unlink(image_path);
1432e80d58db65d29ed4647e631a81aed644fea00913Mathieu Chartier      }
1433edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier    }
1434e80d58db65d29ed4647e631a81aed644fea00913Mathieu Chartier
14357365a10689df23334d245b211ce272502ad20669Alex Light    ALOGV("DexInv: --- BEGIN '%s' ---\n", input_file);
143694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
143794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    pid_t pid;
143894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    pid = fork();
143994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (pid == 0) {
144094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        /* child -- drop privileges before continuing */
14416a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        drop_capabilities(uid);
14426a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle
144313f141910ba383404145924df64ad6626fe5b42fAndreas Gampe        SetDex2OatAndPatchOatScheduling(boot_complete);
14441705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom        if (flock(out_fd, LOCK_EX | LOCK_NB) != 0) {
14451705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom            ALOGE("flock(%s) failed: %s\n", out_path, strerror(errno));
144694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            exit(67);
144794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
144894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
1449c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler        if (dexopt_needed == DEXOPT_PATCHOAT_NEEDED
1450c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler            || dexopt_needed == DEXOPT_SELF_PATCHOAT_NEEDED) {
1451bd872e49561282682793ab526616d609f7fbb274Andreas Gampe            run_patchoat(input_fd, out_fd, input_file, out_path, pkgname, instruction_set);
1452c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler        } else if (dexopt_needed == DEXOPT_DEX2OAT_NEEDED) {
1453aaa1d4e930f653ec21b711eb852194c8abf35143David Brazdil            // Pass dex2oat the relative path to the input file.
1454aaa1d4e930f653ec21b711eb852194c8abf35143David Brazdil            const char *input_file_name = strrchr(input_file, '/');
1455aaa1d4e930f653ec21b711eb852194c8abf35143David Brazdil            if (input_file_name == NULL) {
1456aaa1d4e930f653ec21b711eb852194c8abf35143David Brazdil                input_file_name = input_file;
1457aaa1d4e930f653ec21b711eb852194c8abf35143David Brazdil            } else {
1458aaa1d4e930f653ec21b711eb852194c8abf35143David Brazdil                input_file_name++;
1459aaa1d4e930f653ec21b711eb852194c8abf35143David Brazdil            }
1460aaa1d4e930f653ec21b711eb852194c8abf35143David Brazdil            run_dex2oat(input_fd, out_fd, image_fd, input_file_name, out_path, swap_fd,
1461b0fad6d04ba05fbad5ce31c7382be174ed5a70b1David Brazdil                        instruction_set, vm_safe_mode, debuggable, boot_complete, extract_only,
14626a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle                        reference_profile_fd);
1463c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler        } else {
1464c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler            ALOGE("Invalid dexopt needed: %d\n", dexopt_needed);
1465c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler            exit(73);
14661705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom        }
146794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        exit(68);   /* only get here on exec failure */
146894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    } else {
146963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        res = wait_child(pid);
147063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        if (res == 0) {
14717365a10689df23334d245b211ce272502ad20669Alex Light            ALOGV("DexInv: --- END '%s' (success) ---\n", input_file);
147263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        } else {
14737365a10689df23334d245b211ce272502ad20669Alex Light            ALOGE("DexInv: --- END '%s' --- status=0x%04x, process failed\n", input_file, res);
147494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            goto fail;
147594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
147694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
147794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
14787365a10689df23334d245b211ce272502ad20669Alex Light    ut.actime = input_stat.st_atime;
14797365a10689df23334d245b211ce272502ad20669Alex Light    ut.modtime = input_stat.st_mtime;
14801705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom    utime(out_path, &ut);
14811705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom
14821705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom    close(out_fd);
14837365a10689df23334d245b211ce272502ad20669Alex Light    close(input_fd);
1484edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier    if (swap_fd >= 0) {
1485e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe        close(swap_fd);
1486e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe    }
14876a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    if (reference_profile_fd >= 0) {
14886a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        close(reference_profile_fd);
148960a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle    }
1490edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier    if (image_fd >= 0) {
1491edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier        close(image_fd);
1492edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier    }
149394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return 0;
149494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
149594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodfail:
14961705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom    if (out_fd >= 0) {
14971705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom        close(out_fd);
14981705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom        unlink(out_path);
149994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
15007365a10689df23334d245b211ce272502ad20669Alex Light    if (input_fd >= 0) {
15017365a10689df23334d245b211ce272502ad20669Alex Light        close(input_fd);
150294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
15036a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    if (reference_profile_fd >= 0) {
15046a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        close(reference_profile_fd);
15056a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        // We failed to compile. Unlink the reference profile. Current profiles are already unlinked
15066a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        // when profmoan advises compilation.
15076a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle        unlink_reference_profile(pkgname);
150860a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle    }
1509edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier    if (swap_fd >= 0) {
1510edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier        close(swap_fd);
1511edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier    }
1512edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier    if (image_fd >= 0) {
1513edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier        close(image_fd);
1514edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier    }
151594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return -1;
151694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
151794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
1518091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamathint mark_boot_complete(const char* instruction_set)
1519091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath{
1520091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath  char boot_marker_path[PKG_PATH_MAX];
152102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe  sprintf(boot_marker_path,
152202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe          "%s/%s/%s/.booting",
152302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe          android_data_dir.path,
152402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe          DALVIK_CACHE,
152502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe          instruction_set);
1526091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath
1527091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath  ALOGV("mark_boot_complete : %s", boot_marker_path);
1528091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath  if (unlink(boot_marker_path) != 0) {
1529091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath      ALOGE("Unable to unlink boot marker at %s, error=%s", boot_marker_path,
1530091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath            strerror(errno));
1531091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath      return -1;
1532091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath  }
1533091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath
1534091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath  return 0;
1535091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath}
1536091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath
153794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodvoid mkinnerdirs(char* path, int basepos, mode_t mode, int uid, int gid,
153894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        struct stat* statbuf)
153994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
154094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    while (path[basepos] != 0) {
154194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (path[basepos] == '/') {
154294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            path[basepos] = 0;
154394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (lstat(path, statbuf) < 0) {
154494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGV("Making directory: %s\n", path);
154594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                if (mkdir(path, mode) == 0) {
154694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    chown(path, uid, gid);
154794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                } else {
154894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    ALOGW("Unable to make directory %s: %s\n", path, strerror(errno));
154994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                }
155094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
155194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            path[basepos] = '/';
155294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            basepos++;
155394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
155494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        basepos++;
155594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
155694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
155794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
1558c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkeyint linklib(const char* uuid, const char* pkgname, const char* asecLibDir, int userId)
155994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
156094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    struct stat s, libStat;
156194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int rc = 0;
156294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
1563d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey    std::string _pkgdir(create_data_user_package_path(uuid, userId, pkgname));
1564c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey    std::string _libsymlink(_pkgdir + PKG_LIB_POSTFIX);
1565c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey
1566c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey    const char* pkgdir = _pkgdir.c_str();
1567c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey    const char* libsymlink = _libsymlink.c_str();
156894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
156994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (stat(pkgdir, &s) < 0) return -1;
157094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
157194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (chown(pkgdir, AID_INSTALL, AID_INSTALL) < 0) {
157294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGE("failed to chown '%s': %s\n", pkgdir, strerror(errno));
157394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
157494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
157594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
157694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (chmod(pkgdir, 0700) < 0) {
157794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGE("linklib() 1: failed to chmod '%s': %s\n", pkgdir, strerror(errno));
157894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        rc = -1;
157994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        goto out;
158094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
158194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
158294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (lstat(libsymlink, &libStat) < 0) {
158394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (errno != ENOENT) {
158494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            ALOGE("couldn't stat lib dir: %s\n", strerror(errno));
158594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            rc = -1;
158694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            goto out;
158794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
158894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    } else {
158994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (S_ISDIR(libStat.st_mode)) {
15903aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath            if (delete_dir_contents(libsymlink, 1, NULL) < 0) {
159194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                rc = -1;
159294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                goto out;
159394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
159494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        } else if (S_ISLNK(libStat.st_mode)) {
159594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (unlink(libsymlink) < 0) {
159694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("couldn't unlink lib dir: %s\n", strerror(errno));
159794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                rc = -1;
159894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                goto out;
159994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
160094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
160194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
160294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
160394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (symlink(asecLibDir, libsymlink) < 0) {
160494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGE("couldn't symlink directory '%s' -> '%s': %s\n", libsymlink, asecLibDir,
160594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                strerror(errno));
160694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        rc = -errno;
160794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        goto out;
160894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
160994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
161094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodout:
161194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (chmod(pkgdir, s.st_mode) < 0) {
161294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGE("linklib() 2: failed to chmod '%s': %s\n", pkgdir, strerror(errno));
161394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        rc = -errno;
161494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
161594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
161694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (chown(pkgdir, s.st_uid, s.st_gid) < 0) {
161794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGE("failed to chown '%s' : %s\n", pkgdir, strerror(errno));
161894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -errno;
161994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
162094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
162194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return rc;
162294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
162363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad
162463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstadstatic void run_idmap(const char *target_apk, const char *overlay_apk, int idmap_fd)
162563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad{
162663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    static const char *IDMAP_BIN = "/system/bin/idmap";
162763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    static const size_t MAX_INT_LEN = 32;
162863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    char idmap_str[MAX_INT_LEN];
162963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad
163063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    snprintf(idmap_str, sizeof(idmap_str), "%d", idmap_fd);
163163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad
163263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    execl(IDMAP_BIN, IDMAP_BIN, "--fd", target_apk, overlay_apk, idmap_str, (char*)NULL);
163363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    ALOGE("execl(%s) failed: %s\n", IDMAP_BIN, strerror(errno));
163463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad}
163563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad
163663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad// Transform string /a/b/c.apk to (prefix)/a@b@c.apk@(suffix)
163763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad// eg /a/b/c.apk to /data/resource-cache/a@b@c.apk@idmap
163863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstadstatic int flatten_path(const char *prefix, const char *suffix,
163963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        const char *overlay_path, char *idmap_path, size_t N)
164063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad{
164163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    if (overlay_path == NULL || idmap_path == NULL) {
164263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        return -1;
164363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    }
164463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    const size_t len_overlay_path = strlen(overlay_path);
164563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    // will access overlay_path + 1 further below; requires absolute path
164663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    if (len_overlay_path < 2 || *overlay_path != '/') {
164763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        return -1;
164863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    }
164963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    const size_t len_idmap_root = strlen(prefix);
165063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    const size_t len_suffix = strlen(suffix);
165163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    if (SIZE_MAX - len_idmap_root < len_overlay_path ||
165263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad            SIZE_MAX - (len_idmap_root + len_overlay_path) < len_suffix) {
165363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        // additions below would cause overflow
165463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        return -1;
165563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    }
165663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    if (N < len_idmap_root + len_overlay_path + len_suffix) {
165763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        return -1;
165863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    }
165963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    memset(idmap_path, 0, N);
166063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    snprintf(idmap_path, N, "%s%s%s", prefix, overlay_path + 1, suffix);
166163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    char *ch = idmap_path + len_idmap_root;
166263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    while (*ch != '\0') {
166363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        if (*ch == '/') {
166463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad            *ch = '@';
166563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        }
166663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        ++ch;
166763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    }
166863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    return 0;
166963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad}
167063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad
167163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstadint idmap(const char *target_apk, const char *overlay_apk, uid_t uid)
167263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad{
167363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    ALOGV("idmap target_apk=%s overlay_apk=%s uid=%d\n", target_apk, overlay_apk, uid);
167463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad
167563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    int idmap_fd = -1;
167663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    char idmap_path[PATH_MAX];
167763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad
167863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    if (flatten_path(IDMAP_PREFIX, IDMAP_SUFFIX, overlay_apk,
167963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad                idmap_path, sizeof(idmap_path)) == -1) {
168063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        ALOGE("idmap cannot generate idmap path for overlay %s\n", overlay_apk);
168163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        goto fail;
168263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    }
168363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad
168463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    unlink(idmap_path);
168563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    idmap_fd = open(idmap_path, O_RDWR | O_CREAT | O_EXCL, 0644);
168663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    if (idmap_fd < 0) {
168763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        ALOGE("idmap cannot open '%s' for output: %s\n", idmap_path, strerror(errno));
168863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        goto fail;
168963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    }
169063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    if (fchown(idmap_fd, AID_SYSTEM, uid) < 0) {
169163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        ALOGE("idmap cannot chown '%s'\n", idmap_path);
169263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        goto fail;
169363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    }
169463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    if (fchmod(idmap_fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0) {
169563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        ALOGE("idmap cannot chmod '%s'\n", idmap_path);
169663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        goto fail;
169763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    }
169863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad
169963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    pid_t pid;
170063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    pid = fork();
170163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    if (pid == 0) {
170263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        /* child -- drop privileges before continuing */
170363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        if (setgid(uid) != 0) {
170463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad            ALOGE("setgid(%d) failed during idmap\n", uid);
170563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad            exit(1);
170663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        }
170763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        if (setuid(uid) != 0) {
170863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad            ALOGE("setuid(%d) failed during idmap\n", uid);
170963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad            exit(1);
171063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        }
171163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        if (flock(idmap_fd, LOCK_EX | LOCK_NB) != 0) {
171263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad            ALOGE("flock(%s) failed during idmap: %s\n", idmap_path, strerror(errno));
171363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad            exit(1);
171463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        }
171563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad
171663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        run_idmap(target_apk, overlay_apk, idmap_fd);
171763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        exit(1); /* only if exec call to idmap failed */
171863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    } else {
171963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        int status = wait_child(pid);
172063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        if (status != 0) {
172163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad            ALOGE("idmap failed, status=0x%04x\n", status);
172263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad            goto fail;
172363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        }
172463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    }
172563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad
172663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    close(idmap_fd);
172763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    return 0;
172863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstadfail:
172963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    if (idmap_fd >= 0) {
173063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        close(idmap_fd);
173163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad        unlink(idmap_path);
173263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    }
173363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad    return -1;
173463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad}
1735e9887e46cea4a095e4219927eadbe4c57bb1a5eeRobert Craig
1736c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkeyint restorecon_app_data(const char* uuid, const char* pkgName, userid_t userid, int flags,
1737c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey        appid_t appid, const char* seinfo) {
1738ebf728fd43ab5c7d11a1f9e5fdc775d6740fae0aJeff Sharkey    int res = 0;
1739e9887e46cea4a095e4219927eadbe4c57bb1a5eeRobert Craig
1740da30dc7336f03ca629fe173db1425fdce989119cRobert Craig    // SELINUX_ANDROID_RESTORECON_DATADATA flag is set by libselinux. Not needed here.
1741c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey    unsigned int seflags = SELINUX_ANDROID_RESTORECON_RECURSE;
1742e9887e46cea4a095e4219927eadbe4c57bb1a5eeRobert Craig
1743da30dc7336f03ca629fe173db1425fdce989119cRobert Craig    if (!pkgName || !seinfo) {
1744da30dc7336f03ca629fe173db1425fdce989119cRobert Craig        ALOGE("Package name or seinfo tag is null when trying to restorecon.");
1745da30dc7336f03ca629fe173db1425fdce989119cRobert Craig        return -1;
1746da30dc7336f03ca629fe173db1425fdce989119cRobert Craig    }
1747e9887e46cea4a095e4219927eadbe4c57bb1a5eeRobert Craig
1748c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey    uid_t uid = multiuser_get_uid(userid, appid);
1749aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey    if (flags & FLAG_STORAGE_CE) {
1750c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey        auto path = create_data_user_package_path(uuid, userid, pkgName);
1751c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey        if (selinux_android_restorecon_pkgdir(path.c_str(), seinfo, uid, seflags) < 0) {
1752c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey            PLOG(ERROR) << "restorecon failed for " << path;
1753ebf728fd43ab5c7d11a1f9e5fdc775d6740fae0aJeff Sharkey            res = -1;
1754da30dc7336f03ca629fe173db1425fdce989119cRobert Craig        }
1755c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey    }
1756aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey    if (flags & FLAG_STORAGE_DE) {
1757c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey        auto path = create_data_user_de_package_path(uuid, userid, pkgName);
1758c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey        if (selinux_android_restorecon_pkgdir(path.c_str(), seinfo, uid, seflags) < 0) {
1759c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey            PLOG(ERROR) << "restorecon failed for " << path;
1760ea0e4b19d942f62ea21ca4e70f939f3d4aa14b37Jeff Sharkey            // TODO: include result once 25796509 is fixed
1761da30dc7336f03ca629fe173db1425fdce989119cRobert Craig        }
1762da30dc7336f03ca629fe173db1425fdce989119cRobert Craig    }
1763da30dc7336f03ca629fe173db1425fdce989119cRobert Craig
1764ebf728fd43ab5c7d11a1f9e5fdc775d6740fae0aJeff Sharkey    return res;
1765e9887e46cea4a095e4219927eadbe4c57bb1a5eeRobert Craig}
17663aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath
176788ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolovint create_oat_dir(const char* oat_dir, const char* instruction_set)
176888ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov{
176988ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov    char oat_instr_dir[PKG_PATH_MAX];
177088ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov
177188ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov    if (validate_apk_path(oat_dir)) {
177288ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov        ALOGE("invalid apk path '%s' (bad prefix)\n", oat_dir);
177388ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov        return -1;
177488ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov    }
17758eed7e6a9bb527af7380ec13b390c6e2955eba6aFyodor Kupolov    if (fs_prepare_dir(oat_dir, S_IRWXU | S_IRWXG | S_IXOTH, AID_SYSTEM, AID_INSTALL)) {
177688ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov        return -1;
177788ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov    }
177888ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov    if (selinux_android_restorecon(oat_dir, 0)) {
177988ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov        ALOGE("cannot restorecon dir '%s': %s\n", oat_dir, strerror(errno));
178088ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov        return -1;
178188ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov    }
178288ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov    snprintf(oat_instr_dir, PKG_PATH_MAX, "%s/%s", oat_dir, instruction_set);
17838eed7e6a9bb527af7380ec13b390c6e2955eba6aFyodor Kupolov    if (fs_prepare_dir(oat_instr_dir, S_IRWXU | S_IRWXG | S_IXOTH, AID_SYSTEM, AID_INSTALL)) {
178488ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov        return -1;
178588ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov    }
178688ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov    return 0;
178788ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov}
178888ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov
178988ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolovint rm_package_dir(const char* apk_path)
179088ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov{
179188ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov    if (validate_apk_path(apk_path)) {
179288ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov        ALOGE("invalid apk path '%s' (bad prefix)\n", apk_path);
179388ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov        return -1;
179488ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov    }
179588ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov    return delete_dir_contents(apk_path, 1 /* also_delete_dir */ , NULL /* exclusion_predicate */);
179688ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov}
179788ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov
1798d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamathint link_file(const char* relative_path, const char* from_base, const char* to_base) {
1799d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath    char from_path[PKG_PATH_MAX];
1800d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath    char to_path[PKG_PATH_MAX];
1801d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath    snprintf(from_path, PKG_PATH_MAX, "%s/%s", from_base, relative_path);
1802d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath    snprintf(to_path, PKG_PATH_MAX, "%s/%s", to_base, relative_path);
1803d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath
1804d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath    if (validate_apk_path_subdirs(from_path)) {
1805d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath        ALOGE("invalid app data sub-path '%s' (bad prefix)\n", from_path);
1806d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath        return -1;
1807d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath    }
1808d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath
1809d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath    if (validate_apk_path_subdirs(to_path)) {
1810d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath        ALOGE("invalid app data sub-path '%s' (bad prefix)\n", to_path);
1811d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath        return -1;
1812d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath    }
1813d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath
1814d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath    const int ret = link(from_path, to_path);
1815d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath    if (ret < 0) {
1816d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath        ALOGE("link(%s, %s) failed : %s", from_path, to_path, strerror(errno));
1817d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath        return -1;
1818d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath    }
1819d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath
1820d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath    return 0;
1821d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath}
1822d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath
1823e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe// Helper for move_ab, so that we can have common failure-case cleanup.
1824e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampestatic bool unlink_and_rename(const char* from, const char* to) {
1825e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe    // Check whether "from" exists, and if so whether it's regular. If it is, unlink. Otherwise,
1826e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe    // return a failure.
1827e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe    struct stat s;
1828e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe    if (stat(to, &s) == 0) {
1829e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe        if (!S_ISREG(s.st_mode)) {
1830e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe            LOG(ERROR) << from << " is not a regular file to replace for A/B.";
1831e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe            return false;
1832e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe        }
1833e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe        if (unlink(to) != 0) {
1834e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe            LOG(ERROR) << "Could not unlink " << to << " to move A/B.";
1835e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe            return false;
1836e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe        }
1837e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe    } else {
1838e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe        // This may be a permission problem. We could investigate the error code, but we'll just
1839e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe        // let the rename failure do the work for us.
1840e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe    }
1841e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe
1842e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe    // Try to rename "to" to "from."
1843e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe    if (rename(from, to) != 0) {
1844e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe        PLOG(ERROR) << "Could not rename " << from << " to " << to;
1845e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe        return false;
1846e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe    }
1847e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe
1848e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe    return true;
1849e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe}
1850e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe
1851e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampeint move_ab(const char* apk_path, const char* instruction_set, const char* oat_dir) {
1852e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe    if (apk_path == nullptr || instruction_set == nullptr || oat_dir == nullptr) {
1853e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe        LOG(ERROR) << "Cannot move_ab with null input";
1854e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe        return -1;
1855e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe    }
1856e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe    if (validate_apk_path(apk_path) != 0) {
1857e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe        LOG(ERROR) << "invalid apk_path " << apk_path;
1858e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe        return -1;
1859e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe    }
1860e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe    if (validate_apk_path(oat_dir) != 0) {
1861e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe        LOG(ERROR) << "invalid oat_dir " << oat_dir;
1862e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe        return -1;
1863e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe    }
1864e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe
1865e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe    char a_path[PKG_PATH_MAX];
1866e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe    if (!calculate_oat_file_path(a_path, oat_dir, apk_path, instruction_set)) {
1867e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe        return -1;
1868e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe    }
1869e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe
1870e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe    // B path = A path + ".b"
1871e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe    std::string b_path = StringPrintf("%s.b", a_path);
1872e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe
1873e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe    // Check whether B exists.
1874e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe    {
1875e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe        struct stat s;
1876e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe        if (stat(b_path.c_str(), &s) != 0) {
18772438c6f59cc293a887e65cd34738889043a75d46Andreas Gampe            // Silently ignore for now. The service calling this isn't smart enough to understand
18782438c6f59cc293a887e65cd34738889043a75d46Andreas Gampe            // lack of artifacts at the moment.
1879e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe            return -1;
1880e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe        }
1881e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe        if (!S_ISREG(s.st_mode)) {
1882e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe            LOG(ERROR) << "A/B artifact " << b_path << " is not a regular file.";
1883e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe            // Try to unlink, but swallow errors.
1884e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe            unlink(b_path.c_str());
1885e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe            return -1;
1886e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe        }
1887e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe    }
1888e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe
1889e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe    // Rename B to A.
1890e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe    if (!unlink_and_rename(b_path.c_str(), a_path)) {
1891e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe        // Delete the b_path so we don't try again (or fail earlier).
1892e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe        if (unlink(b_path.c_str()) != 0) {
1893e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe            PLOG(ERROR) << "Could not unlink " << b_path;
1894e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe        }
1895e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe
1896e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe        return -1;
1897e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe    }
1898e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe
1899e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe    return 0;
1900e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe}
1901e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe
190202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe}  // namespace installd
190302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe}  // namespace android
1904