InstalldNativeService.cpp revision 6727c2d0bfc876c62d6ee60d8a05d26bef1d56ef
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> 326727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr#include <android-base/strings.h> 33e4ec9eb7b4c452493589983970ba5ccc501728d1Elliott Hughes#include <android-base/logging.h> 3464b59cc1c89225813295d1b5d6bf4183615da204Roland Levillain#include <android-base/unique_fd.h> 3502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <cutils/fs.h> 3602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <cutils/log.h> // TODO: Move everything to base/logging. 370378aaf257aee92539d30543914a50c4481c6a18Brian Carlstrom#include <cutils/sched_policy.h> 3894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#include <diskusage/dirsize.h> 39e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey#include <logwrap/logwrap.h> 4002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <private/android_filesystem_config.h> 41e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey#include <selinux/android.h> 4202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <system/thread_defs.h> 43e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 4402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <globals.h> 4502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <installd_deps.h> 4602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <utils.h> 4702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe 4802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#ifndef LOG_TAG 4902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#define LOG_TAG "installd" 5002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#endif 5141ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey 5241ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkeyusing android::base::StringPrintf; 5394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 5402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampenamespace android { 5502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampenamespace installd { 5694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 57cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkeystatic constexpr const char* kCpPath = "/system/bin/cp"; 58cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkeystatic constexpr const char* kXattrDefault = "user.default"; 59e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 60eecb2d205366351af42fd0cd9e1a95de3980764eJanis Danisevskis#define MIN_RESTRICTED_HOME_SDK_VERSION 24 // > M 61eecb2d205366351af42fd0cd9e1a95de3980764eJanis Danisevskis 626a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravletypedef int fd_t; 636a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 646a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic bool property_get_bool(const char* property_name, bool default_value = false) { 656a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle char tmp_property_value[kPropertyValueMax]; 666a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle bool have_property = get_property(property_name, tmp_property_value, nullptr) > 0; 676a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle if (!have_property) { 686a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle return default_value; 696a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 706a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle return strcmp(tmp_property_value, "true") == 0; 716a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle} 726a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 73c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkeyint create_app_data(const char *uuid, const char *pkgname, userid_t userid, int flags, 74eecb2d205366351af42fd0cd9e1a95de3980764eJanis Danisevskis appid_t appid, const char* seinfo, int target_sdk_version) { 75c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey uid_t uid = multiuser_get_uid(userid, appid); 76eecb2d205366351af42fd0cd9e1a95de3980764eJanis Danisevskis int target_mode = target_sdk_version >= MIN_RESTRICTED_HOME_SDK_VERSION ? 0700 : 0751; 77aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (flags & FLAG_STORAGE_CE) { 782f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey auto path = create_data_user_ce_package_path(uuid, userid, pkgname); 79eecb2d205366351af42fd0cd9e1a95de3980764eJanis Danisevskis if (fs_prepare_dir_strict(path.c_str(), target_mode, uid, uid) != 0) { 80c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey PLOG(ERROR) << "Failed to prepare " << path; 81c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey return -1; 82c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey } 83c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey if (selinux_android_setfilecon(path.c_str(), pkgname, seinfo, uid) < 0) { 84c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey PLOG(ERROR) << "Failed to setfilecon " << path; 85c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey return -1; 86c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey } 8794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 88aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (flags & FLAG_STORAGE_DE) { 89c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey auto path = create_data_user_de_package_path(uuid, userid, pkgname); 90eecb2d205366351af42fd0cd9e1a95de3980764eJanis Danisevskis if (fs_prepare_dir_strict(path.c_str(), target_mode, uid, uid) == -1) { 91c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey PLOG(ERROR) << "Failed to prepare " << path; 92a03c747ff6e1afb5c1dc86560705bd979cd5c49fJeff Sharkey // TODO: include result once 25796509 is fixed 93a03c747ff6e1afb5c1dc86560705bd979cd5c49fJeff Sharkey return 0; 94c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey } 95c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey if (selinux_android_setfilecon(path.c_str(), pkgname, seinfo, uid) < 0) { 96c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey PLOG(ERROR) << "Failed to setfilecon " << path; 97a03c747ff6e1afb5c1dc86560705bd979cd5c49fJeff Sharkey // TODO: include result once 25796509 is fixed 98a03c747ff6e1afb5c1dc86560705bd979cd5c49fJeff Sharkey return 0; 99c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey } 1006a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 1016a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle if (property_get_bool("dalvik.vm.usejitprofiles")) { 1026a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle const std::string profile_path = create_data_user_profile_package_path(userid, pkgname); 1036a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle // read-write-execute only for the app user. 1046a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle if (fs_prepare_dir_strict(profile_path.c_str(), 0700, uid, uid) != 0) { 1056a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle PLOG(ERROR) << "Failed to prepare " << profile_path; 1066a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle return -1; 1076a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 1086a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle const std::string ref_profile_path = create_data_ref_profile_package_path(pkgname); 1096a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle // dex2oat/profman runs under the shared app gid and it needs to read/write reference 1106a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle // profiles. 1116a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle appid_t shared_app_gid = multiuser_get_shared_app_gid(uid); 1126a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle if (fs_prepare_dir_strict( 1136a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle ref_profile_path.c_str(), 0700, shared_app_gid, shared_app_gid) != 0) { 1146a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle PLOG(ERROR) << "Failed to prepare " << ref_profile_path; 1156a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle return -1; 1166a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 1176a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 11894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 11994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return 0; 12094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 12194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 122cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkeyint migrate_app_data(const char *uuid, const char *pkgname, userid_t userid, int flags) { 123cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey // This method only exists to upgrade system apps that have requested 124cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey // forceDeviceEncrypted, so their default storage always lives in a 125cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey // consistent location. This only works on non-FBE devices, since we 126cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey // never want to risk exposing data on a device with real CE/DE storage. 127cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey 1282f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey auto ce_path = create_data_user_ce_package_path(uuid, userid, pkgname); 129cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey auto de_path = create_data_user_de_package_path(uuid, userid, pkgname); 130cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey 131cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey // If neither directory is marked as default, assume CE is default 132cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey if (getxattr(ce_path.c_str(), kXattrDefault, nullptr, 0) == -1 133cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey && getxattr(de_path.c_str(), kXattrDefault, nullptr, 0) == -1) { 134cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey if (setxattr(ce_path.c_str(), kXattrDefault, nullptr, 0, 0) != 0) { 135cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey PLOG(ERROR) << "Failed to mark default storage " << ce_path; 136cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey return -1; 137cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey } 138cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey } 139cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey 140cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey // Migrate default data location if needed 141cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey auto target = (flags & FLAG_STORAGE_DE) ? de_path : ce_path; 142cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey auto source = (flags & FLAG_STORAGE_DE) ? ce_path : de_path; 143cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey 144cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey if (getxattr(target.c_str(), kXattrDefault, nullptr, 0) == -1) { 145cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey LOG(WARNING) << "Requested default storage " << target 146cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey << " is not active; migrating from " << source; 147cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey if (delete_dir_contents_and_dir(target) != 0) { 148cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey PLOG(ERROR) << "Failed to delete"; 149cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey return -1; 150cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey } 151cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey if (rename(source.c_str(), target.c_str()) != 0) { 152cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey PLOG(ERROR) << "Failed to rename"; 153cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey return -1; 154cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey } 155cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey } 156cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey 157cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey return 0; 158cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey} 159cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey 1606a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle// Keep profile paths in sync with ActivityThread. 1616a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravleconstexpr const char* PRIMARY_PROFILE_NAME = "primary.prof"; 1626a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic std::string create_primary_profile(const std::string& profile_dir) { 1636a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle return StringPrintf("%s/%s", profile_dir.c_str(), PRIMARY_PROFILE_NAME); 1646a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle} 1656a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 166cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravlestatic bool clear_profile(const std::string& profile) { 16764b59cc1c89225813295d1b5d6bf4183615da204Roland Levillain base::unique_fd ufd(open(profile.c_str(), O_WRONLY | O_NOFOLLOW | O_CLOEXEC)); 16864b59cc1c89225813295d1b5d6bf4183615da204Roland Levillain if (ufd.get() < 0) { 169278edac4b541e0a671d646a0e17bdbea411c8a08Calin Juravle if (errno != ENOENT) { 170cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle PLOG(WARNING) << "Could not open profile " << profile; 171d828d6839f6a01e3f0b2b8b2694139dfa801c557David Brazdil return false; 172cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle } else { 173cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle // Nothing to clear. That's ok. 174cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle return true; 175278edac4b541e0a671d646a0e17bdbea411c8a08Calin Juravle } 1766a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 177cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle 17864b59cc1c89225813295d1b5d6bf4183615da204Roland Levillain if (flock(ufd.get(), LOCK_EX | LOCK_NB) != 0) { 179cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle if (errno != EWOULDBLOCK) { 180cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle PLOG(WARNING) << "Error locking profile " << profile; 181cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle } 182cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle // This implies that the app owning this profile is running 183cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle // (and has acquired the lock). 184cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle // 185cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle // If we can't acquire the lock bail out since clearing is useless anyway 186cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle // (the app will write again to the profile). 187cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle // 188cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle // Note: 189cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle // This does not impact the this is not an issue for the profiling correctness. 190cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle // In case this is needed because of an app upgrade, profiles will still be 191cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle // eventually cleared by the app itself due to checksum mismatch. 192cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle // If this is needed because profman advised, then keeping the data around 193cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle // until the next run is again not an issue. 194cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle // 195cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle // If the app attempts to acquire a lock while we've held one here, 196cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle // it will simply skip the current write cycle. 197cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle return false; 198cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle } 199cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle 20064b59cc1c89225813295d1b5d6bf4183615da204Roland Levillain bool truncated = ftruncate(ufd.get(), 0) == 0; 201cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle if (!truncated) { 202cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle PLOG(WARNING) << "Could not truncate " << profile; 203cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle } 20464b59cc1c89225813295d1b5d6bf4183615da204Roland Levillain if (flock(ufd.get(), LOCK_UN) != 0) { 205cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle PLOG(WARNING) << "Error unlocking profile " << profile; 206cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle } 207cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle return truncated; 208cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle} 209cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle 210cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravlestatic bool clear_reference_profile(const char* pkgname) { 211cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle std::string reference_profile_dir = create_data_ref_profile_package_path(pkgname); 212cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle std::string reference_profile = create_primary_profile(reference_profile_dir); 213cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle return clear_profile(reference_profile); 2146a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle} 2156a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 216cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravlestatic bool clear_current_profile(const char* pkgname, userid_t user) { 217edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle std::string profile_dir = create_data_user_profile_package_path(user, pkgname); 218edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle std::string profile = create_primary_profile(profile_dir); 219cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle return clear_profile(profile); 220edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle} 221edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle 222cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravlestatic bool clear_current_profiles(const char* pkgname) { 223d828d6839f6a01e3f0b2b8b2694139dfa801c557David Brazdil bool success = true; 2246a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle std::vector<userid_t> users = get_known_users(/*volume_uuid*/ nullptr); 2256a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle for (auto user : users) { 226cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle success &= clear_current_profile(pkgname, user); 2276a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 228d828d6839f6a01e3f0b2b8b2694139dfa801c557David Brazdil return success; 2296a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle} 2306a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 231edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravleint clear_app_profiles(const char* pkgname) { 232d828d6839f6a01e3f0b2b8b2694139dfa801c557David Brazdil bool success = true; 233cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle success &= clear_reference_profile(pkgname); 234cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle success &= clear_current_profiles(pkgname); 235edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle return success ? 0 : -1; 2366a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle} 2376a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 2382f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkeyint clear_app_data(const char *uuid, const char *pkgname, userid_t userid, int flags, 2392f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey ino_t ce_data_inode) { 240c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey std::string suffix = ""; 241edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle bool only_cache = false; 242c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey if (flags & FLAG_CLEAR_CACHE_ONLY) { 243c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey suffix = CACHE_DIR_POSTFIX; 244edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle only_cache = true; 245c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey } else if (flags & FLAG_CLEAR_CODE_CACHE_ONLY) { 246c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey suffix = CODE_CACHE_DIR_POSTFIX; 247edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle only_cache = true; 248c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey } 24994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 250ebf728fd43ab5c7d11a1f9e5fdc775d6740fae0aJeff Sharkey int res = 0; 251aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (flags & FLAG_STORAGE_CE) { 2522f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey auto path = create_data_user_ce_package_path(uuid, userid, pkgname, ce_data_inode) + suffix; 253c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey if (access(path.c_str(), F_OK) == 0) { 254c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey res |= delete_dir_contents(path); 255c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey } 25694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 257aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (flags & FLAG_STORAGE_DE) { 258c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey auto path = create_data_user_de_package_path(uuid, userid, pkgname) + suffix; 259c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey if (access(path.c_str(), F_OK) == 0) { 260a03c747ff6e1afb5c1dc86560705bd979cd5c49fJeff Sharkey // TODO: include result once 25796509 is fixed 261a03c747ff6e1afb5c1dc86560705bd979cd5c49fJeff Sharkey delete_dir_contents(path); 262c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey } 263edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle if (!only_cache) { 264cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle if (!clear_current_profile(pkgname, userid)) { 265edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle res |= -1; 266edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle } 267edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle } 26894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 269c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey return res; 270c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey} 27194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 2727535e8e1995398befc866062d63c05d90eb70cccCalin Juravlestatic int destroy_app_reference_profile(const char *pkgname) { 2737535e8e1995398befc866062d63c05d90eb70cccCalin Juravle return delete_dir_contents_and_dir( 2747535e8e1995398befc866062d63c05d90eb70cccCalin Juravle create_data_ref_profile_package_path(pkgname), 2757535e8e1995398befc866062d63c05d90eb70cccCalin Juravle /*ignore_if_missing*/ true); 2767535e8e1995398befc866062d63c05d90eb70cccCalin Juravle} 2777535e8e1995398befc866062d63c05d90eb70cccCalin Juravle 278edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravlestatic int destroy_app_current_profiles(const char *pkgname, userid_t userid) { 279b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravle return delete_dir_contents_and_dir( 280b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravle create_data_user_profile_package_path(userid, pkgname), 281b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravle /*ignore_if_missing*/ true); 282edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle} 283edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle 284edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravleint destroy_app_profiles(const char *pkgname) { 285edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle int result = 0; 286edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle std::vector<userid_t> users = get_known_users(/*volume_uuid*/ nullptr); 287edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle for (auto user : users) { 288edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle result |= destroy_app_current_profiles(pkgname, user); 289edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle } 2907535e8e1995398befc866062d63c05d90eb70cccCalin Juravle result |= destroy_app_reference_profile(pkgname); 291edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle return result; 292caa6b809a5ed024a8f3f551c5c109a5ad50ab772Calin Juravle} 293caa6b809a5ed024a8f3f551c5c109a5ad50ab772Calin Juravle 2942f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkeyint destroy_app_data(const char *uuid, const char *pkgname, userid_t userid, int flags, 2952f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey ino_t ce_data_inode) { 296c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey int res = 0; 297aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (flags & FLAG_STORAGE_CE) { 298c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey res |= delete_dir_contents_and_dir( 2992f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey create_data_user_ce_package_path(uuid, userid, pkgname, ce_data_inode)); 30094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 301aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (flags & FLAG_STORAGE_DE) { 302caa6b809a5ed024a8f3f551c5c109a5ad50ab772Calin Juravle res |= delete_dir_contents_and_dir( 303c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey create_data_user_de_package_path(uuid, userid, pkgname)); 304b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravle destroy_app_current_profiles(pkgname, userid); 3057535e8e1995398befc866062d63c05d90eb70cccCalin Juravle // TODO(calin): If the package is still installed by other users it's probably 3067535e8e1995398befc866062d63c05d90eb70cccCalin Juravle // beneficial to keep the reference profile around. 3077535e8e1995398befc866062d63c05d90eb70cccCalin Juravle // Verify if it's ok to do that. 3087535e8e1995398befc866062d63c05d90eb70cccCalin Juravle destroy_app_reference_profile(pkgname); 30994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 310c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey return res; 31194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 31294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 313c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkeyint move_complete_app(const char *from_uuid, const char *to_uuid, const char *package_name, 314eecb2d205366351af42fd0cd9e1a95de3980764eJanis Danisevskis const char *data_app_name, appid_t appid, const char* seinfo, int target_sdk_version) { 315e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey std::vector<userid_t> users = get_known_users(from_uuid); 316e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 317d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey // Copy app 318d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey { 31951c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto from = create_data_app_package_path(from_uuid, data_app_name); 32051c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto to = create_data_app_package_path(to_uuid, data_app_name); 32151c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto to_parent = create_data_app_path(to_uuid); 322d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey 323d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey char *argv[] = { 324d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey (char*) kCpPath, 325d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey (char*) "-F", /* delete any existing destination file first (--remove-destination) */ 326d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey (char*) "-p", /* preserve timestamps, ownership, and permissions */ 327d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey (char*) "-R", /* recurse into subdirectories (DEST must be a directory) */ 328d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey (char*) "-P", /* Do not follow symlinks [default] */ 329d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey (char*) "-d", /* don't dereference symlinks */ 330d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey (char*) from.c_str(), 331d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey (char*) to_parent.c_str() 332d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey }; 333d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey 334d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey LOG(DEBUG) << "Copying " << from << " to " << to; 335d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true); 336d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey 337d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey if (rc != 0) { 338d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey LOG(ERROR) << "Failed copying " << from << " to " << to 339d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey << ": status " << rc; 340d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey goto fail; 341d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey } 342d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey 343d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey if (selinux_android_restorecon(to.c_str(), SELINUX_ANDROID_RESTORECON_RECURSE) != 0) { 344d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey LOG(ERROR) << "Failed to restorecon " << to; 345d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey goto fail; 346d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey } 347d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey } 348d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey 349d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey // Copy private data for all known users 350e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey for (auto user : users) { 351e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 352e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey // Data source may not exist for all users; that's okay 35351c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto from_ce = create_data_user_ce_package_path(from_uuid, user, package_name); 35451c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey if (access(from_ce.c_str(), F_OK) != 0) { 35551c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey LOG(INFO) << "Missing source " << from_ce; 356e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey continue; 357e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey } 358e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 359aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (create_app_data(to_uuid, package_name, user, FLAG_STORAGE_CE | FLAG_STORAGE_DE, 360eecb2d205366351af42fd0cd9e1a95de3980764eJanis Danisevskis appid, seinfo, target_sdk_version) != 0) { 36151c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey LOG(ERROR) << "Failed to create package target on " << to_uuid; 362e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey goto fail; 363e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey } 364e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 365e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey char *argv[] = { 366e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey (char*) kCpPath, 367e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey (char*) "-F", /* delete any existing destination file first (--remove-destination) */ 368e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey (char*) "-p", /* preserve timestamps, ownership, and permissions */ 369e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey (char*) "-R", /* recurse into subdirectories (DEST must be a directory) */ 370e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey (char*) "-P", /* Do not follow symlinks [default] */ 371e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey (char*) "-d", /* don't dereference symlinks */ 37251c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey nullptr, 37351c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey nullptr 374e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey }; 375e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 37651c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey { 37751c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto from = create_data_user_de_package_path(from_uuid, user, package_name); 37851c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto to = create_data_user_de_path(to_uuid, user); 37951c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey argv[6] = (char*) from.c_str(); 38051c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey argv[7] = (char*) to.c_str(); 38151c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey 38251c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey LOG(DEBUG) << "Copying " << from << " to " << to; 38351c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true); 38451c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey if (rc != 0) { 38551c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey LOG(ERROR) << "Failed copying " << from << " to " << to << " with status " << rc; 38651c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey goto fail; 38751c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey } 38851c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey } 38951c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey { 39051c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto from = create_data_user_ce_package_path(from_uuid, user, package_name); 39151c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto to = create_data_user_ce_path(to_uuid, user); 39251c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey argv[6] = (char*) from.c_str(); 39351c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey argv[7] = (char*) to.c_str(); 39451c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey 39551c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey LOG(DEBUG) << "Copying " << from << " to " << to; 39651c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true); 39751c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey if (rc != 0) { 39851c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey LOG(ERROR) << "Failed copying " << from << " to " << to << " with status " << rc; 39951c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey goto fail; 40051c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey } 401e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey } 402e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 403aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (restorecon_app_data(to_uuid, package_name, user, FLAG_STORAGE_CE | FLAG_STORAGE_DE, 404c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey appid, seinfo) != 0) { 405c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey LOG(ERROR) << "Failed to restorecon"; 406c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey goto fail; 407c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey } 408e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey } 409e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 41031f08986f83fa6f2dcf55523b2cf706460aeed7cJeff Sharkey // We let the framework scan the new location and persist that before 41131f08986f83fa6f2dcf55523b2cf706460aeed7cJeff Sharkey // deleting the data in the old location; this ordering ensures that 41231f08986f83fa6f2dcf55523b2cf706460aeed7cJeff Sharkey // we can recover from things like battery pulls. 413e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey return 0; 414e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 415e36372423000a906bafae68844ebc6c42d09335aJeff Sharkeyfail: 416e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey // Nuke everything we might have already copied 417d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey { 41851c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto to = create_data_app_package_path(to_uuid, data_app_name); 419d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey if (delete_dir_contents(to.c_str(), 1, NULL) != 0) { 420d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey LOG(WARNING) << "Failed to rollback " << to; 421d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey } 422d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey } 423e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey for (auto user : users) { 42451c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey { 42551c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto to = create_data_user_de_package_path(to_uuid, user, package_name); 42651c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey if (delete_dir_contents(to.c_str(), 1, NULL) != 0) { 42751c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey LOG(WARNING) << "Failed to rollback " << to; 42851c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey } 42951c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey } 43051c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey { 43151c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto to = create_data_user_ce_package_path(to_uuid, user, package_name); 43251c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey if (delete_dir_contents(to.c_str(), 1, NULL) != 0) { 43351c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey LOG(WARNING) << "Failed to rollback " << to; 43451c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey } 435e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey } 436e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey } 437e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey return -1; 438e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey} 439e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 440379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkeyint create_user_data(const char *uuid, userid_t userid, int user_serial ATTRIBUTE_UNUSED, 441379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey int flags) { 442379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey if (flags & FLAG_STORAGE_DE) { 443379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey if (uuid == nullptr) { 444379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey return ensure_config_user_dirs(userid); 445379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey } 44694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 447095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee return 0; 448095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee} 449095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee 450379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkeyint destroy_user_data(const char *uuid, userid_t userid, int flags) { 451ebf728fd43ab5c7d11a1f9e5fdc775d6740fae0aJeff Sharkey int res = 0; 452379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey if (flags & FLAG_STORAGE_DE) { 453379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey res |= delete_dir_contents_and_dir(create_data_user_de_path(uuid, userid), true); 454379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey if (uuid == nullptr) { 455379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey res |= delete_dir_contents_and_dir(create_data_misc_legacy_path(userid), true); 456379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey res |= delete_dir_contents_and_dir(create_data_user_profiles_path(userid), true); 45741ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey } 45894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 459379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey if (flags & FLAG_STORAGE_CE) { 460379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey res |= delete_dir_contents_and_dir(create_data_user_ce_path(uuid, userid), true); 461379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey res |= delete_dir_contents_and_dir(create_data_media_path(uuid, userid), true); 462379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey } 463ebf728fd43ab5c7d11a1f9e5fdc775d6740fae0aJeff Sharkey return res; 46494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 46594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 46694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/* Try to ensure free_size bytes of storage are available. 46794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * Returns 0 on success. 46894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * This is rather simple-minded because doing a full LRU would 46994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * be potentially memory-intensive, and without atime it would 47094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * also require that apps constantly modify file metadata even 47194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * when just reading from the cache, which is pretty awful. 47294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */ 47354e292e1ea87b504b552393d2e9bc800458bd0c1Jeff Sharkeyint free_cache(const char *uuid, int64_t free_size) { 47494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache_t* cache; 47594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int64_t avail; 47694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 47754e292e1ea87b504b552393d2e9bc800458bd0c1Jeff Sharkey auto data_path = create_data_path(uuid); 47841ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey 47941ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey avail = data_disk_free(data_path); 48094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (avail < 0) return -1; 48194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 48294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGI("free_cache(%" PRId64 ") avail %" PRId64 "\n", free_size, avail); 48394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (avail >= free_size) return 0; 48494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 48594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache = start_cache_collection(); 48694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 48754e292e1ea87b504b552393d2e9bc800458bd0c1Jeff Sharkey auto users = get_known_users(uuid); 48854e292e1ea87b504b552393d2e9bc800458bd0c1Jeff Sharkey for (auto user : users) { 48954e292e1ea87b504b552393d2e9bc800458bd0c1Jeff Sharkey add_cache_files(cache, create_data_user_ce_path(uuid, user)); 49054e292e1ea87b504b552393d2e9bc800458bd0c1Jeff Sharkey add_cache_files(cache, create_data_user_de_path(uuid, user)); 49154e292e1ea87b504b552393d2e9bc800458bd0c1Jeff Sharkey add_cache_files(cache, 49254e292e1ea87b504b552393d2e9bc800458bd0c1Jeff Sharkey StringPrintf("%s/Android/data", create_data_media_path(uuid, user).c_str())); 49394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 49494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 49541ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey clear_cache_files(data_path, cache, free_size); 49694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood finish_cache_collection(cache); 49794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 49841ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey return data_disk_free(data_path) >= free_size ? 0 : -1; 49994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 50094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 5011b4003207750ea8fe8c7b03eb32d80f1df83979eNarayan Kamathint rm_dex(const char *path, const char *instruction_set) 50294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 50394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood char dex_path[PKG_PATH_MAX]; 50494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 505770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey if (validate_apk_path(path) && validate_system_app_path(path)) { 506770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey ALOGE("invalid apk path '%s' (bad prefix)\n", path); 507770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey return -1; 508770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey } 509770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey 51002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe if (!create_cache_path(dex_path, path, instruction_set)) return -1; 51194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 51294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGV("unlink %s\n", dex_path); 51394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (unlink(dex_path) < 0) { 514770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey if (errno != ENOENT) { 515770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey ALOGE("Couldn't unlink %s: %s\n", dex_path, strerror(errno)); 516770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey } 51794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return -1; 51894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else { 51994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return 0; 52094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 52194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 52294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 5232f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkeystatic void add_app_data_size(std::string& path, int64_t *codesize, int64_t *datasize, 5242f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey int64_t *cachesize) { 52594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood DIR *d; 52694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int dfd; 52794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood struct dirent *de; 52894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood struct stat s; 529d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey 5302f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey d = opendir(path.c_str()); 5312f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey if (d == nullptr) { 5322f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey PLOG(WARNING) << "Failed to open " << path; 5332f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey return; 53494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 5352f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey dfd = dirfd(d); 5362f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey while ((de = readdir(d))) { 5372f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey const char *name = de->d_name; 538d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey 5392f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey int64_t statsize = 0; 5402f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) { 5412f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey statsize = stat_size(&s); 54294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 54394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 5442f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey if (de->d_type == DT_DIR) { 5452f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey int subfd; 5462f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey int64_t dirsize = 0; 5472f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey /* always skip "." and ".." */ 5482f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey if (name[0] == '.') { 5492f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey if (name[1] == 0) continue; 5502f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey if ((name[1] == '.') && (name[2] == 0)) continue; 5512f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey } 5522f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY); 5532f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey if (subfd >= 0) { 5542f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey dirsize = calculate_dir_size(subfd); 5552f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey close(subfd); 5562f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey } 5572f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey // TODO: check xattrs! 5582f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey if (!strcmp(name, "cache") || !strcmp(name, "code_cache")) { 5592f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey *datasize += statsize; 5602f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey *cachesize += dirsize; 5612f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey } else { 5622f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey *datasize += dirsize + statsize; 5632f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey } 5642f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey } else if (de->d_type == DT_LNK && !strcmp(name, "lib")) { 5652f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey *codesize += statsize; 5662f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey } else { 5672f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey *datasize += statsize; 56894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 56994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 5702f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey closedir(d); 5712f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey} 57294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 5732f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkeyint get_app_size(const char *uuid, const char *pkgname, int userid, int flags, ino_t ce_data_inode, 5742f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey const char *code_path, int64_t *codesize, int64_t *datasize, int64_t *cachesize, 5752f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey int64_t* asecsize) { 5762f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey DIR *d; 5772f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey int dfd; 57894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 5792f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey d = opendir(code_path); 5802f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey if (d != nullptr) { 5812f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey dfd = dirfd(d); 5822f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey *codesize += calculate_dir_size(dfd); 5832f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey closedir(d); 58494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 58594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 5862f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey if (flags & FLAG_STORAGE_CE) { 5872f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey auto path = create_data_user_ce_package_path(uuid, userid, pkgname, ce_data_inode); 5882f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey add_app_data_size(path, codesize, datasize, cachesize); 5892f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey } 5902f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey if (flags & FLAG_STORAGE_DE) { 5912f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey auto path = create_data_user_de_package_path(uuid, userid, pkgname); 5922f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey add_app_data_size(path, codesize, datasize, cachesize); 5932f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey } 59494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 5952f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey *asecsize = 0; 596d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey 5972f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey return 0; 5982f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey} 599d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey 6002f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkeyint get_app_data_inode(const char *uuid, const char *pkgname, int userid, int flags, ino_t *inode) { 6012f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey struct stat buf; 6022f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey memset(&buf, 0, sizeof(buf)); 6032f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey if (flags & FLAG_STORAGE_CE) { 6042f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey auto path = create_data_user_ce_package_path(uuid, userid, pkgname); 6052f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey if (stat(path.c_str(), &buf) == 0) { 6062f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey *inode = buf.st_ino; 6072f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey return 0; 60894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 60994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 6102f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey return -1; 61194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 61294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 613b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Roubanstatic int split_count(const char *str) 614b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban{ 615b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban char *ctx; 616b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban int count = 0; 61702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe char buf[kPropertyValueMax]; 618b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban 619b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban strncpy(buf, str, sizeof(buf)); 620b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban char *pBuf = buf; 621b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban 622b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban while(strtok_r(pBuf, " ", &ctx) != NULL) { 623b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban count++; 624b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban pBuf = NULL; 625b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban } 626b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban 627b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban return count; 628b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban} 629b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban 63014e084d39ac44e9d4d0a0143bf1f111011343d34neo.chaestatic int split(char *buf, const char **argv) 631b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban{ 632b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban char *ctx; 633b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban int count = 0; 634b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban char *tok; 635b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban char *pBuf = buf; 636b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban 637b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban while((tok = strtok_r(pBuf, " ", &ctx)) != NULL) { 638b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban argv[count++] = tok; 639b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban pBuf = NULL; 640b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban } 641b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban 642b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban return count; 643b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban} 644b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban 6457365a10689df23334d245b211ce272502ad20669Alex Lightstatic void run_patchoat(int input_fd, int oat_fd, const char* input_file_name, 64602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe const char* output_file_name, const char *pkgname ATTRIBUTE_UNUSED, const char *instruction_set) 6477365a10689df23334d245b211ce272502ad20669Alex Light{ 6487365a10689df23334d245b211ce272502ad20669Alex Light static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig 6498fc7315a67fdbf9b56cc6fa061f0773be2223a81Calin Juravle static const unsigned int MAX_INSTRUCTION_SET_LEN = 7; 6507365a10689df23334d245b211ce272502ad20669Alex Light 6517365a10689df23334d245b211ce272502ad20669Alex Light static const char* PATCHOAT_BIN = "/system/bin/patchoat"; 6527365a10689df23334d245b211ce272502ad20669Alex Light if (strlen(instruction_set) >= MAX_INSTRUCTION_SET_LEN) { 6537365a10689df23334d245b211ce272502ad20669Alex Light ALOGE("Instruction set %s longer than max length of %d", 6547365a10689df23334d245b211ce272502ad20669Alex Light instruction_set, MAX_INSTRUCTION_SET_LEN); 6557365a10689df23334d245b211ce272502ad20669Alex Light return; 6567365a10689df23334d245b211ce272502ad20669Alex Light } 6577365a10689df23334d245b211ce272502ad20669Alex Light 6587365a10689df23334d245b211ce272502ad20669Alex Light /* input_file_name/input_fd should be the .odex/.oat file that is precompiled. I think*/ 6597365a10689df23334d245b211ce272502ad20669Alex Light char instruction_set_arg[strlen("--instruction-set=") + MAX_INSTRUCTION_SET_LEN]; 6607365a10689df23334d245b211ce272502ad20669Alex Light char output_oat_fd_arg[strlen("--output-oat-fd=") + MAX_INT_LEN]; 6617365a10689df23334d245b211ce272502ad20669Alex Light char input_oat_fd_arg[strlen("--input-oat-fd=") + MAX_INT_LEN]; 6627365a10689df23334d245b211ce272502ad20669Alex Light const char* patched_image_location_arg = "--patched-image-location=/system/framework/boot.art"; 6637365a10689df23334d245b211ce272502ad20669Alex Light // The caller has already gotten all the locks we need. 6647365a10689df23334d245b211ce272502ad20669Alex Light const char* no_lock_arg = "--no-lock-output"; 6657365a10689df23334d245b211ce272502ad20669Alex Light sprintf(instruction_set_arg, "--instruction-set=%s", instruction_set); 6667365a10689df23334d245b211ce272502ad20669Alex Light sprintf(output_oat_fd_arg, "--output-oat-fd=%d", oat_fd); 6677365a10689df23334d245b211ce272502ad20669Alex Light sprintf(input_oat_fd_arg, "--input-oat-fd=%d", input_fd); 668a7915d437c3971943f82dfc2370fe7a5df522801Alex Light ALOGV("Running %s isa=%s in-fd=%d (%s) out-fd=%d (%s)\n", 6697365a10689df23334d245b211ce272502ad20669Alex Light PATCHOAT_BIN, instruction_set, input_fd, input_file_name, oat_fd, output_file_name); 6707365a10689df23334d245b211ce272502ad20669Alex Light 6717365a10689df23334d245b211ce272502ad20669Alex Light /* patchoat, patched-image-location, no-lock, isa, input-fd, output-fd */ 6727365a10689df23334d245b211ce272502ad20669Alex Light char* argv[7]; 6737365a10689df23334d245b211ce272502ad20669Alex Light argv[0] = (char*) PATCHOAT_BIN; 6747365a10689df23334d245b211ce272502ad20669Alex Light argv[1] = (char*) patched_image_location_arg; 6757365a10689df23334d245b211ce272502ad20669Alex Light argv[2] = (char*) no_lock_arg; 6767365a10689df23334d245b211ce272502ad20669Alex Light argv[3] = instruction_set_arg; 6777365a10689df23334d245b211ce272502ad20669Alex Light argv[4] = output_oat_fd_arg; 6787365a10689df23334d245b211ce272502ad20669Alex Light argv[5] = input_oat_fd_arg; 6797365a10689df23334d245b211ce272502ad20669Alex Light argv[6] = NULL; 6807365a10689df23334d245b211ce272502ad20669Alex Light 6817365a10689df23334d245b211ce272502ad20669Alex Light execv(PATCHOAT_BIN, (char* const *)argv); 6827365a10689df23334d245b211ce272502ad20669Alex Light ALOGE("execv(%s) failed: %s\n", PATCHOAT_BIN, strerror(errno)); 6837365a10689df23334d245b211ce272502ad20669Alex Light} 6847365a10689df23334d245b211ce272502ad20669Alex Light 685edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartierstatic void run_dex2oat(int zip_fd, int oat_fd, int image_fd, const char* input_file_name, 6866a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle const char* output_file_name, int swap_fd, const char *instruction_set, 6874d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe const char* compiler_filter, bool vm_safe_mode, bool debuggable, bool post_bootcomplete, 688b63d91fd2737680351876406277b6c759f4db33cJeff Hao int profile_fd, const char* shared_libraries) { 6898fc7315a67fdbf9b56cc6fa061f0773be2223a81Calin Juravle static const unsigned int MAX_INSTRUCTION_SET_LEN = 7; 6908fc7315a67fdbf9b56cc6fa061f0773be2223a81Calin Juravle 6918fc7315a67fdbf9b56cc6fa061f0773be2223a81Calin Juravle if (strlen(instruction_set) >= MAX_INSTRUCTION_SET_LEN) { 6928fc7315a67fdbf9b56cc6fa061f0773be2223a81Calin Juravle ALOGE("Instruction set %s longer than max length of %d", 6938fc7315a67fdbf9b56cc6fa061f0773be2223a81Calin Juravle instruction_set, MAX_INSTRUCTION_SET_LEN); 6948fc7315a67fdbf9b56cc6fa061f0773be2223a81Calin Juravle return; 6958fc7315a67fdbf9b56cc6fa061f0773be2223a81Calin Juravle } 6968fc7315a67fdbf9b56cc6fa061f0773be2223a81Calin Juravle 69702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe char dex2oat_Xms_flag[kPropertyValueMax]; 69802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe bool have_dex2oat_Xms_flag = get_property("dalvik.vm.dex2oat-Xms", dex2oat_Xms_flag, NULL) > 0; 699e46a75a0f6007967cd0d161959af844772cdc330Brian Carlstrom 70002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe char dex2oat_Xmx_flag[kPropertyValueMax]; 70102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe bool have_dex2oat_Xmx_flag = get_property("dalvik.vm.dex2oat-Xmx", dex2oat_Xmx_flag, NULL) > 0; 702e46a75a0f6007967cd0d161959af844772cdc330Brian Carlstrom 70302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe char dex2oat_threads_buf[kPropertyValueMax]; 70402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe bool have_dex2oat_threads_flag = get_property(post_bootcomplete 705919461cbeef3e9f35388fc099a5fcdae1cb79cc6Andreas Gampe ? "dalvik.vm.dex2oat-threads" 706919461cbeef3e9f35388fc099a5fcdae1cb79cc6Andreas Gampe : "dalvik.vm.boot-dex2oat-threads", 707919461cbeef3e9f35388fc099a5fcdae1cb79cc6Andreas Gampe dex2oat_threads_buf, 708919461cbeef3e9f35388fc099a5fcdae1cb79cc6Andreas Gampe NULL) > 0; 70902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe char dex2oat_threads_arg[kPropertyValueMax + 2]; 7108d7af8b2418cc5e7e59746f0cb359a75ed0bdfd1Andreas Gampe if (have_dex2oat_threads_flag) { 7118d7af8b2418cc5e7e59746f0cb359a75ed0bdfd1Andreas Gampe sprintf(dex2oat_threads_arg, "-j%s", dex2oat_threads_buf); 7128d7af8b2418cc5e7e59746f0cb359a75ed0bdfd1Andreas Gampe } 7138d7af8b2418cc5e7e59746f0cb359a75ed0bdfd1Andreas Gampe 71402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe char dex2oat_isa_features_key[kPropertyKeyMax]; 7158fc7315a67fdbf9b56cc6fa061f0773be2223a81Calin Juravle sprintf(dex2oat_isa_features_key, "dalvik.vm.isa.%s.features", instruction_set); 71602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe char dex2oat_isa_features[kPropertyValueMax]; 71702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe bool have_dex2oat_isa_features = get_property(dex2oat_isa_features_key, 7188fc7315a67fdbf9b56cc6fa061f0773be2223a81Calin Juravle dex2oat_isa_features, NULL) > 0; 7198fc7315a67fdbf9b56cc6fa061f0773be2223a81Calin Juravle 72002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe char dex2oat_isa_variant_key[kPropertyKeyMax]; 72116a95b267ac6aac75453f65f1bc382ce65c0741eIan Rogers sprintf(dex2oat_isa_variant_key, "dalvik.vm.isa.%s.variant", instruction_set); 72202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe char dex2oat_isa_variant[kPropertyValueMax]; 72302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe bool have_dex2oat_isa_variant = get_property(dex2oat_isa_variant_key, 72416a95b267ac6aac75453f65f1bc382ce65c0741eIan Rogers dex2oat_isa_variant, NULL) > 0; 72516a95b267ac6aac75453f65f1bc382ce65c0741eIan Rogers 72614e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae const char *dex2oat_norelocation = "-Xnorelocate"; 72714e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae bool have_dex2oat_relocation_skip_flag = false; 72814e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae 72902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe char dex2oat_flags[kPropertyValueMax]; 73002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe int dex2oat_flags_count = get_property("dalvik.vm.dex2oat-flags", 731b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban dex2oat_flags, NULL) <= 0 ? 0 : split_count(dex2oat_flags); 7320ae8e39ebcc26836ba55a5ee4481825a0f473a9eBrian Carlstrom ALOGV("dalvik.vm.dex2oat-flags=%s\n", dex2oat_flags); 7330ae8e39ebcc26836ba55a5ee4481825a0f473a9eBrian Carlstrom 734538998f204d1e542e235de9e7ce18ef4dc68c9ccBrian Carlstrom // If we booting without the real /data, don't spend time compiling. 73502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe char vold_decrypt[kPropertyValueMax]; 73602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe bool have_vold_decrypt = get_property("vold.decrypt", vold_decrypt, "") > 0; 737538998f204d1e542e235de9e7ce18ef4dc68c9ccBrian Carlstrom bool skip_compilation = (have_vold_decrypt && 738538998f204d1e542e235de9e7ce18ef4dc68c9ccBrian Carlstrom (strcmp(vold_decrypt, "trigger_restart_min_framework") == 0 || 739538998f204d1e542e235de9e7ce18ef4dc68c9ccBrian Carlstrom (strcmp(vold_decrypt, "1") == 0))); 740538998f204d1e542e235de9e7ce18ef4dc68c9ccBrian Carlstrom 7416a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle bool generate_debug_info = property_get_bool("debug.generate-debug-info"); 742d4a7b459f4f383988440c0512513b321ce03fc22Mathieu Chartier 743416fa12cd0949c7f12e2067b36ac7ac64ba70b4aMathieu Chartier char app_image_format[kPropertyValueMax]; 744416fa12cd0949c7f12e2067b36ac7ac64ba70b4aMathieu Chartier char image_format_arg[strlen("--image-format=") + kPropertyValueMax]; 745416fa12cd0949c7f12e2067b36ac7ac64ba70b4aMathieu Chartier bool have_app_image_format = 74641fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier image_fd >= 0 && get_property("dalvik.vm.appimageformat", app_image_format, NULL) > 0; 747416fa12cd0949c7f12e2067b36ac7ac64ba70b4aMathieu Chartier if (have_app_image_format) { 748416fa12cd0949c7f12e2067b36ac7ac64ba70b4aMathieu Chartier sprintf(image_format_arg, "--image-format=%s", app_image_format); 749416fa12cd0949c7f12e2067b36ac7ac64ba70b4aMathieu Chartier } 750416fa12cd0949c7f12e2067b36ac7ac64ba70b4aMathieu Chartier 7511705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom static const char* DEX2OAT_BIN = "/system/bin/dex2oat"; 75253e0776d967324e2908e3be56b80cddb2c9d9e03Brian Carlstrom 75353e0776d967324e2908e3be56b80cddb2c9d9e03Brian Carlstrom static const char* RUNTIME_ARG = "--runtime-arg"; 75453e0776d967324e2908e3be56b80cddb2c9d9e03Brian Carlstrom 7551705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig 7561b4003207750ea8fe8c7b03eb32d80f1df83979eNarayan Kamath 7571705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom char zip_fd_arg[strlen("--zip-fd=") + MAX_INT_LEN]; 7581705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom char zip_location_arg[strlen("--zip-location=") + PKG_PATH_MAX]; 7591705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom char oat_fd_arg[strlen("--oat-fd=") + MAX_INT_LEN]; 7607195fcc2185c35b45a8f28c79fde4999b38850e8Brian Carlstrom char oat_location_arg[strlen("--oat-location=") + PKG_PATH_MAX]; 7611b4003207750ea8fe8c7b03eb32d80f1df83979eNarayan Kamath char instruction_set_arg[strlen("--instruction-set=") + MAX_INSTRUCTION_SET_LEN]; 76202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe char instruction_set_variant_arg[strlen("--instruction-set-variant=") + kPropertyValueMax]; 76302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe char instruction_set_features_arg[strlen("--instruction-set-features=") + kPropertyValueMax]; 76402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe char dex2oat_Xms_arg[strlen("-Xms") + kPropertyValueMax]; 76502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe char dex2oat_Xmx_arg[strlen("-Xmx") + kPropertyValueMax]; 76602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe char dex2oat_compiler_filter_arg[strlen("--compiler-filter=") + kPropertyValueMax]; 767e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe bool have_dex2oat_swap_fd = false; 768e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe char dex2oat_swap_fd[strlen("--swap-fd=") + MAX_INT_LEN]; 769edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier bool have_dex2oat_image_fd = false; 770edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier char dex2oat_image_fd[strlen("--app-image-fd=") + MAX_INT_LEN]; 7711705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom 7721705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom sprintf(zip_fd_arg, "--zip-fd=%d", zip_fd); 7731705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom sprintf(zip_location_arg, "--zip-location=%s", input_file_name); 7741705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom sprintf(oat_fd_arg, "--oat-fd=%d", oat_fd); 7751705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom sprintf(oat_location_arg, "--oat-location=%s", output_file_name); 7761b4003207750ea8fe8c7b03eb32d80f1df83979eNarayan Kamath sprintf(instruction_set_arg, "--instruction-set=%s", instruction_set); 77716a95b267ac6aac75453f65f1bc382ce65c0741eIan Rogers sprintf(instruction_set_variant_arg, "--instruction-set-variant=%s", dex2oat_isa_variant); 7788fc7315a67fdbf9b56cc6fa061f0773be2223a81Calin Juravle sprintf(instruction_set_features_arg, "--instruction-set-features=%s", dex2oat_isa_features); 779e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe if (swap_fd >= 0) { 780e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe have_dex2oat_swap_fd = true; 781e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe sprintf(dex2oat_swap_fd, "--swap-fd=%d", swap_fd); 782e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe } 783edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier if (image_fd >= 0) { 784edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier have_dex2oat_image_fd = true; 785edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier sprintf(dex2oat_image_fd, "--app-image-fd=%d", image_fd); 786edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier } 78757c69c39640a7d6a08b3afb56361d76f7ecf1250Calin Juravle 788e46a75a0f6007967cd0d161959af844772cdc330Brian Carlstrom if (have_dex2oat_Xms_flag) { 789e46a75a0f6007967cd0d161959af844772cdc330Brian Carlstrom sprintf(dex2oat_Xms_arg, "-Xms%s", dex2oat_Xms_flag); 790e46a75a0f6007967cd0d161959af844772cdc330Brian Carlstrom } 791e46a75a0f6007967cd0d161959af844772cdc330Brian Carlstrom if (have_dex2oat_Xmx_flag) { 792e46a75a0f6007967cd0d161959af844772cdc330Brian Carlstrom sprintf(dex2oat_Xmx_arg, "-Xmx%s", dex2oat_Xmx_flag); 793e46a75a0f6007967cd0d161959af844772cdc330Brian Carlstrom } 7944d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe 7954d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe // Compute compiler filter. 7964d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe 7974d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe bool have_dex2oat_compiler_filter_flag; 798538998f204d1e542e235de9e7ce18ef4dc68c9ccBrian Carlstrom if (skip_compilation) { 799e18987efb5e39ca1bed15527b7b82bde55c99669Brian Carlstrom strcpy(dex2oat_compiler_filter_arg, "--compiler-filter=verify-none"); 800538998f204d1e542e235de9e7ce18ef4dc68c9ccBrian Carlstrom have_dex2oat_compiler_filter_flag = true; 80114e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae have_dex2oat_relocation_skip_flag = true; 802b1efac103523efccbe671e76cc0eaaeab810415bCalin Juravle } else if (vm_safe_mode) { 803b1efac103523efccbe671e76cc0eaaeab810415bCalin Juravle strcpy(dex2oat_compiler_filter_arg, "--compiler-filter=interpret-only"); 80497477d203eaf0c3235bbe2415356f20a0431cadaCalin Juravle have_dex2oat_compiler_filter_flag = true; 8054d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe } else if (compiler_filter != nullptr && 8064d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe strlen(compiler_filter) + strlen("--compiler-filter=") < 8074d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe arraysize(dex2oat_compiler_filter_arg)) { 8084d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe sprintf(dex2oat_compiler_filter_arg, "--compiler-filter=%s", compiler_filter); 809d4a7b459f4f383988440c0512513b321ce03fc22Mathieu Chartier have_dex2oat_compiler_filter_flag = true; 8104d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe } else { 8114d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe char dex2oat_compiler_filter_flag[kPropertyValueMax]; 8124d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe have_dex2oat_compiler_filter_flag = get_property("dalvik.vm.dex2oat-filter", 8134d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe dex2oat_compiler_filter_flag, NULL) > 0; 8144d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe if (have_dex2oat_compiler_filter_flag) { 8154d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe sprintf(dex2oat_compiler_filter_arg, 8164d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe "--compiler-filter=%s", 8174d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe dex2oat_compiler_filter_flag); 8184d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe } 819cf51ba1360ee13459830a0502b0d454d0145544fBrian Carlstrom } 820e46a75a0f6007967cd0d161959af844772cdc330Brian Carlstrom 821598c25e23f7c97470e09a2316513ddf2efdfb670Andreas Gampe // Check whether all apps should be compiled debuggable. 822598c25e23f7c97470e09a2316513ddf2efdfb670Andreas Gampe if (!debuggable) { 82302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe char prop_buf[kPropertyValueMax]; 824598c25e23f7c97470e09a2316513ddf2efdfb670Andreas Gampe debuggable = 82502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe (get_property("dalvik.vm.always_debuggable", prop_buf, "0") > 0) && 826598c25e23f7c97470e09a2316513ddf2efdfb670Andreas Gampe (prop_buf[0] == '1'); 827598c25e23f7c97470e09a2316513ddf2efdfb670Andreas Gampe } 8286a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle char profile_arg[strlen("--profile-file-fd=") + MAX_INT_LEN]; 8296a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle if (profile_fd != -1) { 8306a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle sprintf(profile_arg, "--profile-file-fd=%d", profile_fd); 83160a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle } 832598c25e23f7c97470e09a2316513ddf2efdfb670Andreas Gampe 8336a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 8341705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom ALOGV("Running %s in=%s out=%s\n", DEX2OAT_BIN, input_file_name, output_file_name); 8354fdff4616699708e58d875273c3c1a85ba4c63bdCalin Juravle 83614e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae const char* argv[7 // program name, mandatory arguments and the final NULL 83714e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae + (have_dex2oat_isa_variant ? 1 : 0) 83814e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae + (have_dex2oat_isa_features ? 1 : 0) 83914e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae + (have_dex2oat_Xms_flag ? 2 : 0) 84014e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae + (have_dex2oat_Xmx_flag ? 2 : 0) 84114e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae + (have_dex2oat_compiler_filter_flag ? 1 : 0) 8428d7af8b2418cc5e7e59746f0cb359a75ed0bdfd1Andreas Gampe + (have_dex2oat_threads_flag ? 1 : 0) 84314e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae + (have_dex2oat_swap_fd ? 1 : 0) 844edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier + (have_dex2oat_image_fd ? 1 : 0) 84514e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae + (have_dex2oat_relocation_skip_flag ? 2 : 0) 846528c8ddc60df7b3d291b389117e4db878a20ad48David Srbecky + (generate_debug_info ? 1 : 0) 847598c25e23f7c97470e09a2316513ddf2efdfb670Andreas Gampe + (debuggable ? 1 : 0) 848416fa12cd0949c7f12e2067b36ac7ac64ba70b4aMathieu Chartier + (have_app_image_format ? 1 : 0) 84960a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle + dex2oat_flags_count 850b63d91fd2737680351876406277b6c759f4db33cJeff Hao + (profile_fd == -1 ? 0 : 1) 851b63d91fd2737680351876406277b6c759f4db33cJeff Hao + (shared_libraries != nullptr ? 4 : 0)]; 8524fdff4616699708e58d875273c3c1a85ba4c63bdCalin Juravle int i = 0; 85314e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae argv[i++] = DEX2OAT_BIN; 8544fdff4616699708e58d875273c3c1a85ba4c63bdCalin Juravle argv[i++] = zip_fd_arg; 8554fdff4616699708e58d875273c3c1a85ba4c63bdCalin Juravle argv[i++] = zip_location_arg; 8564fdff4616699708e58d875273c3c1a85ba4c63bdCalin Juravle argv[i++] = oat_fd_arg; 8574fdff4616699708e58d875273c3c1a85ba4c63bdCalin Juravle argv[i++] = oat_location_arg; 8584fdff4616699708e58d875273c3c1a85ba4c63bdCalin Juravle argv[i++] = instruction_set_arg; 85916a95b267ac6aac75453f65f1bc382ce65c0741eIan Rogers if (have_dex2oat_isa_variant) { 86016a95b267ac6aac75453f65f1bc382ce65c0741eIan Rogers argv[i++] = instruction_set_variant_arg; 86116a95b267ac6aac75453f65f1bc382ce65c0741eIan Rogers } 8628fc7315a67fdbf9b56cc6fa061f0773be2223a81Calin Juravle if (have_dex2oat_isa_features) { 8638fc7315a67fdbf9b56cc6fa061f0773be2223a81Calin Juravle argv[i++] = instruction_set_features_arg; 8648fc7315a67fdbf9b56cc6fa061f0773be2223a81Calin Juravle } 865e46a75a0f6007967cd0d161959af844772cdc330Brian Carlstrom if (have_dex2oat_Xms_flag) { 86614e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae argv[i++] = RUNTIME_ARG; 867e46a75a0f6007967cd0d161959af844772cdc330Brian Carlstrom argv[i++] = dex2oat_Xms_arg; 868e46a75a0f6007967cd0d161959af844772cdc330Brian Carlstrom } 869e46a75a0f6007967cd0d161959af844772cdc330Brian Carlstrom if (have_dex2oat_Xmx_flag) { 87014e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae argv[i++] = RUNTIME_ARG; 871e46a75a0f6007967cd0d161959af844772cdc330Brian Carlstrom argv[i++] = dex2oat_Xmx_arg; 872e46a75a0f6007967cd0d161959af844772cdc330Brian Carlstrom } 873cf51ba1360ee13459830a0502b0d454d0145544fBrian Carlstrom if (have_dex2oat_compiler_filter_flag) { 874cf51ba1360ee13459830a0502b0d454d0145544fBrian Carlstrom argv[i++] = dex2oat_compiler_filter_arg; 875cf51ba1360ee13459830a0502b0d454d0145544fBrian Carlstrom } 8768d7af8b2418cc5e7e59746f0cb359a75ed0bdfd1Andreas Gampe if (have_dex2oat_threads_flag) { 8778d7af8b2418cc5e7e59746f0cb359a75ed0bdfd1Andreas Gampe argv[i++] = dex2oat_threads_arg; 8788d7af8b2418cc5e7e59746f0cb359a75ed0bdfd1Andreas Gampe } 879e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe if (have_dex2oat_swap_fd) { 880e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe argv[i++] = dex2oat_swap_fd; 881e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe } 882edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier if (have_dex2oat_image_fd) { 883edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier argv[i++] = dex2oat_image_fd; 884edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier } 885528c8ddc60df7b3d291b389117e4db878a20ad48David Srbecky if (generate_debug_info) { 886528c8ddc60df7b3d291b389117e4db878a20ad48David Srbecky argv[i++] = "--generate-debug-info"; 8873822b8b79cf533ee8573794176838406c32a1c20Andreas Gampe } 888598c25e23f7c97470e09a2316513ddf2efdfb670Andreas Gampe if (debuggable) { 889598c25e23f7c97470e09a2316513ddf2efdfb670Andreas Gampe argv[i++] = "--debuggable"; 890598c25e23f7c97470e09a2316513ddf2efdfb670Andreas Gampe } 891416fa12cd0949c7f12e2067b36ac7ac64ba70b4aMathieu Chartier if (have_app_image_format) { 892416fa12cd0949c7f12e2067b36ac7ac64ba70b4aMathieu Chartier argv[i++] = image_format_arg; 893416fa12cd0949c7f12e2067b36ac7ac64ba70b4aMathieu Chartier } 894b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban if (dex2oat_flags_count) { 895b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban i += split(dex2oat_flags, argv + i); 8964fdff4616699708e58d875273c3c1a85ba4c63bdCalin Juravle } 89714e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae if (have_dex2oat_relocation_skip_flag) { 89814e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae argv[i++] = RUNTIME_ARG; 89914e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae argv[i++] = dex2oat_norelocation; 90014e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae } 9016a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle if (profile_fd != -1) { 9026a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle argv[i++] = profile_arg; 90360a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle } 904b63d91fd2737680351876406277b6c759f4db33cJeff Hao if (shared_libraries != nullptr) { 905b63d91fd2737680351876406277b6c759f4db33cJeff Hao argv[i++] = RUNTIME_ARG; 906b63d91fd2737680351876406277b6c759f4db33cJeff Hao argv[i++] = "-classpath"; 907b63d91fd2737680351876406277b6c759f4db33cJeff Hao argv[i++] = RUNTIME_ARG; 908b63d91fd2737680351876406277b6c759f4db33cJeff Hao argv[i++] = shared_libraries; 909b63d91fd2737680351876406277b6c759f4db33cJeff Hao } 910e46a75a0f6007967cd0d161959af844772cdc330Brian Carlstrom // Do not add after dex2oat_flags, they should override others for debugging. 9114fdff4616699708e58d875273c3c1a85ba4c63bdCalin Juravle argv[i] = NULL; 9124fdff4616699708e58d875273c3c1a85ba4c63bdCalin Juravle 91314e084d39ac44e9d4d0a0143bf1f111011343d34neo.chae execv(DEX2OAT_BIN, (char * const *)argv); 914b0d8d00b2916aef7b01ad42f6d441f5b46b7731aYevgeny Rouban ALOGE("execv(%s) failed: %s\n", DEX2OAT_BIN, strerror(errno)); 9151705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom} 9161705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom 917e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe/* 918c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe * Whether dexopt should use a swap file when compiling an APK. 919c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe * 920c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe * If kAlwaysProvideSwapFile, do this on all devices (dex2oat will make a more informed decision 921c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe * itself, anyways). 922c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe * 923c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe * Otherwise, read "dalvik.vm.dex2oat-swap". If the property exists, return whether it is "true". 924c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe * 925c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe * Otherwise, return true if this is a low-mem device. 926c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe * 927c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe * Otherwise, return default value. 928e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe */ 929c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampestatic bool kAlwaysProvideSwapFile = false; 930c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampestatic bool kDefaultProvideSwapFile = true; 931e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe 932e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampestatic bool ShouldUseSwapFileForDexopt() { 933e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe if (kAlwaysProvideSwapFile) { 934e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe return true; 935e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe } 936e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe 937c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe // Check the "override" property. If it exists, return value == "true". 93802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe char dex2oat_prop_buf[kPropertyValueMax]; 93902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe if (get_property("dalvik.vm.dex2oat-swap", dex2oat_prop_buf, "") > 0) { 940c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe if (strcmp(dex2oat_prop_buf, "true") == 0) { 941c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe return true; 942c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe } else { 943c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe return false; 944c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe } 945c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe } 946c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe 947c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe // Shortcut for default value. This is an implementation optimization for the process sketched 948c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe // above. If the default value is true, we can avoid to check whether this is a low-mem device, 949c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe // as low-mem is never returning false. The compiler will optimize this away if it can. 950c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe if (kDefaultProvideSwapFile) { 951c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe return true; 952c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe } 953c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe 9546a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle bool is_low_mem = property_get_bool("ro.config.low_ram"); 955c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe if (is_low_mem) { 956c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe return true; 957c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe } 958c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe 959c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe // Default value must be false here. 960c968c0175e967e39e72f557b5e014b9575ba4727Andreas Gampe return kDefaultProvideSwapFile; 961e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe} 962e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe 96394dd3d3c5be4de57278ee668218a3464585795c0Andreas Gampestatic void SetDex2OatAndPatchOatScheduling(bool set_to_bg) { 96494dd3d3c5be4de57278ee668218a3464585795c0Andreas Gampe if (set_to_bg) { 96594dd3d3c5be4de57278ee668218a3464585795c0Andreas Gampe if (set_sched_policy(0, SP_BACKGROUND) < 0) { 96694dd3d3c5be4de57278ee668218a3464585795c0Andreas Gampe ALOGE("set_sched_policy failed: %s\n", strerror(errno)); 96794dd3d3c5be4de57278ee668218a3464585795c0Andreas Gampe exit(70); 96894dd3d3c5be4de57278ee668218a3464585795c0Andreas Gampe } 96994dd3d3c5be4de57278ee668218a3464585795c0Andreas Gampe if (setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND) < 0) { 97094dd3d3c5be4de57278ee668218a3464585795c0Andreas Gampe ALOGE("setpriority failed: %s\n", strerror(errno)); 97194dd3d3c5be4de57278ee668218a3464585795c0Andreas Gampe exit(71); 97294dd3d3c5be4de57278ee668218a3464585795c0Andreas Gampe } 97394dd3d3c5be4de57278ee668218a3464585795c0Andreas Gampe } 97494dd3d3c5be4de57278ee668218a3464585795c0Andreas Gampe} 97594dd3d3c5be4de57278ee668218a3464585795c0Andreas Gampe 9766a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic void close_all_fds(const std::vector<fd_t>& fds, const char* description) { 97760a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle for (size_t i = 0; i < fds.size(); i++) { 97860a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle if (close(fds[i]) != 0) { 97960a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle PLOG(WARNING) << "Failed to close fd for " << description << " at index " << i; 98060a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle } 98160a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle } 98260a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle} 98360a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle 9846a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic fd_t open_profile_dir(const std::string& profile_dir) { 9856a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle fd_t profile_dir_fd = TEMP_FAILURE_RETRY(open(profile_dir.c_str(), 9866a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle O_PATH | O_CLOEXEC | O_DIRECTORY | O_NOFOLLOW)); 9876a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle if (profile_dir_fd < 0) { 9882e063af1651976b50e2da58c4d57564d878ab9c2Narayan Kamath // In a multi-user environment, these directories can be created at 9892e063af1651976b50e2da58c4d57564d878ab9c2Narayan Kamath // different points and it's possible we'll attempt to open a profile 9902e063af1651976b50e2da58c4d57564d878ab9c2Narayan Kamath // dir before it exists. 9912e063af1651976b50e2da58c4d57564d878ab9c2Narayan Kamath if (errno != ENOENT) { 9922e063af1651976b50e2da58c4d57564d878ab9c2Narayan Kamath PLOG(ERROR) << "Failed to open profile_dir: " << profile_dir; 9932e063af1651976b50e2da58c4d57564d878ab9c2Narayan Kamath } 99460a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle } 9956a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle return profile_dir_fd; 99660a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle} 99760a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle 9986a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic fd_t open_primary_profile_file_from_dir(const std::string& profile_dir, mode_t open_mode) { 9996a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle fd_t profile_dir_fd = open_profile_dir(profile_dir); 10006a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle if (profile_dir_fd < 0) { 10016a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle return -1; 10026a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 10036a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 10046a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle fd_t profile_fd = -1; 10056a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle std::string profile_file = create_primary_profile(profile_dir); 10066a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 10076a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle profile_fd = TEMP_FAILURE_RETRY(open(profile_file.c_str(), open_mode | O_NOFOLLOW)); 10086a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle if (profile_fd == -1) { 10096a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle // It's not an error if the profile file does not exist. 101060a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle if (errno != ENOENT) { 10116a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle PLOG(ERROR) << "Failed to lstat profile_dir: " << profile_dir; 101260a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle } 101360a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle } 10146a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle // TODO(calin): use AutoCloseFD instead of closing the fd manually. 10156a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle if (close(profile_dir_fd) != 0) { 10166a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle PLOG(WARNING) << "Could not close profile dir " << profile_dir; 10176a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 10186a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle return profile_fd; 10196a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle} 102060a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle 10216a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic fd_t open_primary_profile_file(userid_t user, const char* pkgname) { 10226a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle std::string profile_dir = create_data_user_profile_package_path(user, pkgname); 10236a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle return open_primary_profile_file_from_dir(profile_dir, O_RDONLY); 10246a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle} 10256a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 10266a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic fd_t open_reference_profile(uid_t uid, const char* pkgname, bool read_write) { 10276a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle std::string reference_profile_dir = create_data_ref_profile_package_path(pkgname); 10286a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle int flags = read_write ? O_RDWR | O_CREAT : O_RDONLY; 10296a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle fd_t fd = open_primary_profile_file_from_dir(reference_profile_dir, flags); 10306a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle if (fd < 0) { 10316a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle return -1; 103260a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle } 10336a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle if (read_write) { 10346a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle // Fix the owner. 10356a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle if (fchown(fd, uid, uid) < 0) { 10366a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle close(fd); 10376a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle return -1; 10386a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 10396a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 10406a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle return fd; 10416a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle} 104260a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle 10436a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic void open_profile_files(uid_t uid, const char* pkgname, 10446a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle /*out*/ std::vector<fd_t>* profiles_fd, /*out*/ fd_t* reference_profile_fd) { 10456a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle // Open the reference profile in read-write mode as profman might need to save the merge. 10466a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle *reference_profile_fd = open_reference_profile(uid, pkgname, /*read_write*/ true); 104760a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle if (*reference_profile_fd < 0) { 10486a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle // We can't access the reference profile file. 104960a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle return; 105060a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle } 10516a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 10526a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle std::vector<userid_t> users = get_known_users(/*volume_uuid*/ nullptr); 10536a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle for (auto user : users) { 10546a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle fd_t profile_fd = open_primary_profile_file(user, pkgname); 10556a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle // Add to the lists only if both fds are valid. 10566a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle if (profile_fd >= 0) { 10576a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle profiles_fd->push_back(profile_fd); 10586a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 10596a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 10606a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle} 10616a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 10626a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic void drop_capabilities(uid_t uid) { 10636a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle if (setgid(uid) != 0) { 10646a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle ALOGE("setgid(%d) failed in installd during dexopt\n", uid); 10656a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle exit(64); 10666a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 10676a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle if (setuid(uid) != 0) { 10686a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle ALOGE("setuid(%d) failed in installd during dexopt\n", uid); 10696a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle exit(65); 10706a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 10716a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle // drop capabilities 10726a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle struct __user_cap_header_struct capheader; 10736a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle struct __user_cap_data_struct capdata[2]; 10746a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle memset(&capheader, 0, sizeof(capheader)); 10756a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle memset(&capdata, 0, sizeof(capdata)); 10766a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle capheader.version = _LINUX_CAPABILITY_VERSION_3; 10776a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle if (capset(&capheader, &capdata[0]) < 0) { 10786a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle ALOGE("capset failed: %s\n", strerror(errno)); 10796a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle exit(66); 108060a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle } 108160a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle} 108260a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle 10836a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic constexpr int PROFMAN_BIN_RETURN_CODE_COMPILE = 0; 10846a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic constexpr int PROFMAN_BIN_RETURN_CODE_SKIP_COMPILATION = 1; 10856a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic constexpr int PROFMAN_BIN_RETURN_CODE_BAD_PROFILES = 2; 10866a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic constexpr int PROFMAN_BIN_RETURN_CODE_ERROR_IO = 3; 10876a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic constexpr int PROFMAN_BIN_RETURN_CODE_ERROR_LOCKING = 4; 10886a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 10896727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehrstatic void run_profman_merge(const std::vector<fd_t>& profiles_fd, fd_t reference_profile_fd) { 10906a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle static const size_t MAX_INT_LEN = 32; 10916a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle static const char* PROFMAN_BIN = "/system/bin/profman"; 10926a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 10936a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle std::vector<std::string> profile_args(profiles_fd.size()); 10946a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle char profile_buf[strlen("--profile-file-fd=") + MAX_INT_LEN]; 10956a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle for (size_t k = 0; k < profiles_fd.size(); k++) { 10966a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle sprintf(profile_buf, "--profile-file-fd=%d", profiles_fd[k]); 10976a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle profile_args[k].assign(profile_buf); 10986a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 10996a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle char reference_profile_arg[strlen("--reference-profile-file-fd=") + MAX_INT_LEN]; 11006a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle sprintf(reference_profile_arg, "--reference-profile-file-fd=%d", reference_profile_fd); 11016a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 11026a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle // program name, reference profile fd, the final NULL and the profile fds 11036a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle const char* argv[3 + profiles_fd.size()]; 11046a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle int i = 0; 11056a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle argv[i++] = PROFMAN_BIN; 11066a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle argv[i++] = reference_profile_arg; 11076a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle for (size_t k = 0; k < profile_args.size(); k++) { 11086a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle argv[i++] = profile_args[k].c_str(); 11096a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 11106a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle // Do not add after dex2oat_flags, they should override others for debugging. 11116a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle argv[i] = NULL; 11126a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 11136a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle execv(PROFMAN_BIN, (char * const *)argv); 11146a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle ALOGE("execv(%s) failed: %s\n", PROFMAN_BIN, strerror(errno)); 11156a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle exit(68); /* only get here on exec failure */ 11166a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle} 11176a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 11186a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle// Decides if profile guided compilation is needed or not based on existing profiles. 11196a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle// Returns true if there is enough information in the current profiles that worth 11206a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle// a re-compilation of the package. 11216a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle// If the return value is true all the current profiles would have been merged into 11226a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle// the reference profiles accessible with open_reference_profile(). 11236a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic bool analyse_profiles(uid_t uid, const char* pkgname) { 11246a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle std::vector<fd_t> profiles_fd; 11256a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle fd_t reference_profile_fd = -1; 11266a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle open_profile_files(uid, pkgname, &profiles_fd, &reference_profile_fd); 11276a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle if (profiles_fd.empty() || (reference_profile_fd == -1)) { 11286a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle // Skip profile guided compilation because no profiles were found. 11296a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle // Or if the reference profile info couldn't be opened. 11306a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle close_all_fds(profiles_fd, "profiles_fd"); 11316a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle if ((reference_profile_fd != - 1) && (close(reference_profile_fd) != 0)) { 11326a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle PLOG(WARNING) << "Failed to close fd for reference profile"; 113360a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle } 11346a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle return false; 11356a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 113660a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle 11376727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr ALOGV("PROFMAN (MERGE): --- BEGIN '%s' ---\n", pkgname); 11386a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 11396a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle pid_t pid = fork(); 11406a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle if (pid == 0) { 11416a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle /* child -- drop privileges before continuing */ 11426a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle drop_capabilities(uid); 11436727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr run_profman_merge(profiles_fd, reference_profile_fd); 11446a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle exit(68); /* only get here on exec failure */ 11456a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 11466a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle /* parent */ 11476a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle int return_code = wait_child(pid); 11486a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle bool need_to_compile = false; 1149cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle bool should_clear_current_profiles = false; 1150cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle bool should_clear_reference_profile = false; 11516a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle if (!WIFEXITED(return_code)) { 11526a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle LOG(WARNING) << "profman failed for package " << pkgname << ": " << return_code; 11536a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } else { 11546a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle return_code = WEXITSTATUS(return_code); 11556a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle switch (return_code) { 11566a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle case PROFMAN_BIN_RETURN_CODE_COMPILE: 11576a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle need_to_compile = true; 1158cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle should_clear_current_profiles = true; 1159cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle should_clear_reference_profile = false; 11606a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle break; 11616a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle case PROFMAN_BIN_RETURN_CODE_SKIP_COMPILATION: 11626a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle need_to_compile = false; 1163cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle should_clear_current_profiles = false; 1164cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle should_clear_reference_profile = false; 11656a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle break; 11666a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle case PROFMAN_BIN_RETURN_CODE_BAD_PROFILES: 11676a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle LOG(WARNING) << "Bad profiles for package " << pkgname; 11686a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle need_to_compile = false; 1169cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle should_clear_current_profiles = true; 1170cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle should_clear_reference_profile = true; 11716a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle break; 11726a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle case PROFMAN_BIN_RETURN_CODE_ERROR_IO: // fall-through 11736a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle case PROFMAN_BIN_RETURN_CODE_ERROR_LOCKING: 11746a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle // Temporary IO problem (e.g. locking). Ignore but log a warning. 11756a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle LOG(WARNING) << "IO error while reading profiles for package " << pkgname; 11766a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle need_to_compile = false; 1177cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle should_clear_current_profiles = false; 1178cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle should_clear_reference_profile = false; 11796a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle break; 11806a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle default: 11816a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle // Unknown return code or error. Unlink profiles. 11826a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle LOG(WARNING) << "Unknown error code while processing profiles for package " << pkgname 11836a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle << ": " << return_code; 11846a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle need_to_compile = false; 1185cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle should_clear_current_profiles = true; 1186cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle should_clear_reference_profile = true; 11876a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle break; 118860a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle } 118960a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle } 11906a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle close_all_fds(profiles_fd, "profiles_fd"); 11916a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle if (close(reference_profile_fd) != 0) { 11926a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle PLOG(WARNING) << "Failed to close fd for reference profile"; 11936a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 1194cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle if (should_clear_current_profiles) { 1195cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle clear_current_profiles(pkgname); 11966a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 1197cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle if (should_clear_reference_profile) { 1198cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle clear_reference_profile(pkgname); 11996a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 12006a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle return need_to_compile; 120160a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle} 120260a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle 12036727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehrstatic void run_profman_dump(const std::vector<fd_t>& profile_fds, 12046727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr fd_t reference_profile_fd, 12056727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr const std::vector<std::string>& code_locations, 12066727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr const std::vector<fd_t>& code_location_fds, 12076727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr fd_t output_fd) { 12086727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr static const char* PROFMAN_BIN = "/system/bin/profman"; 12096727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr const bool has_reference_profile = (reference_profile_fd != -1); 12106727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr // program name 12116727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr // --dump-only 12126727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr // --dump-output-to-fd=<output_fd> 12136727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr // (optionally, --reference-profile-file-fd=<reference_profile_fd>) 12146727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr const size_t fixed_args = (has_reference_profile ? 4 : 3); 12156727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr // Fixed arguments, profiles, code paths, code path fds, and final NULL. 12166727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr const size_t argc = fixed_args + profile_fds.size() + code_locations.size() + 12176727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr code_location_fds.size() + 1; 12186727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr const char **argv = new const char*[argc]; 12196727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr int i = 0; 12206727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr argv[i++] = PROFMAN_BIN; 12216727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr argv[i++] = "--dump-only"; 12226727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr std::string dump_output = StringPrintf("--dump-output-to-fd=%d", output_fd); 12236727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr argv[i++] = dump_output.c_str(); 12246727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr if (has_reference_profile) { 12256727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr std::string reference = 12266727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr StringPrintf("--reference-profile-file-fd=%d", reference_profile_fd); 12276727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr argv[i++] = reference.c_str(); 12286727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr } 12296727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr for (fd_t profile_fd : profile_fds) { 12306727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr std::string profile_arg = StringPrintf("--profile-file-fd=%d", profile_fd); 12316727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr argv[i++] = strdup(profile_arg.c_str()); 12326727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr } 12336727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr for (const std::string& code_location : code_locations) { 12346727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr std::string path_str = StringPrintf("--code-location=%s", code_location.c_str()); 12356727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr argv[i++] = strdup(path_str.c_str()); 12366727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr } 12376727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr for (fd_t code_location_fd : code_location_fds) { 12386727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr std::string fd_str = StringPrintf("--code-location-fd=%d", code_location_fd); 12396727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr argv[i++] = strdup(fd_str.c_str()); 12406727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr } 12416727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr argv[i] = NULL; 12426727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr assert(i == argc - 1); 12436727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr 12446727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr execv(PROFMAN_BIN, (char * const *)argv); 12456727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr ALOGE("execv(%s) failed: %s\n", PROFMAN_BIN, strerror(errno)); 12466727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr exit(68); /* only get here on exec failure */ 12476727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr} 12486727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr 12496727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr// Dumps the contents of a profile file, using pkgname's dex files for pretty 12506727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr// printing the result. 12516727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehrbool dump_profile(uid_t uid, const char* pkgname, const char* code_path_string) { 12526727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr std::vector<fd_t> profile_fds; 12536727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr fd_t reference_profile_fd = -1; 12546727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr std::string out_file_name = StringPrintf("/data/misc/profman/%s.txt", pkgname); 12556727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr 12566727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr ALOGV("PROFMAN (DUMP): --- BEGIN '%s' ---\n", pkgname); 12576727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr 12586727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr open_profile_files(uid, pkgname, &profile_fds, &reference_profile_fd); 12596727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr 12606727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr const bool has_reference_profile = (reference_profile_fd != -1); 12616727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr const bool has_profiles = !profile_fds.empty(); 12626727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr 12636727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr if (!has_reference_profile && !has_profiles) { 12646727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr ALOGE("profman dump: no profiles to dump for '%s'", pkgname); 12656727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr return false; 12666727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr } 12676727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr 12686727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr fd_t output_fd = open(out_file_name.c_str(), O_WRONLY | O_CREAT | O_NOFOLLOW); 12696727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr if (fchmod(output_fd, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) < 0) { 12706727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr ALOGE("installd cannot chmod '%s' dump_profile\n", out_file_name.c_str()); 12716727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr return false; 12726727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr } 12736727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr std::vector<std::string> code_locations = base::Split(code_path_string, ";"); 12746727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr std::vector<fd_t> code_location_fds; 12756727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr for (const std::string& code_location : code_locations) { 12766727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr fd_t code_location_fd = open(code_location.c_str(), O_RDONLY | O_NOFOLLOW); 12776727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr if (code_location_fd == -1) { 12786727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr ALOGE("installd cannot open '%s'\n", code_location.c_str()); 12796727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr return false; 12806727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr } 12816727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr code_location_fds.push_back(code_location_fd); 12826727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr } 12836727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr 12846727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr pid_t pid = fork(); 12856727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr if (pid == 0) { 12866727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr /* child -- drop privileges before continuing */ 12876727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr drop_capabilities(uid); 12886727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr run_profman_dump(profile_fds, reference_profile_fd, code_locations, 12896727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr code_location_fds, output_fd); 12906727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr exit(68); /* only get here on exec failure */ 12916727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr } 12926727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr /* parent */ 12936727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr close_all_fds(code_location_fds, "code_location_fds"); 12946727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr close_all_fds(profile_fds, "profile_fds"); 12956727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr if (close(reference_profile_fd) != 0) { 12966727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr PLOG(WARNING) << "Failed to close fd for reference profile"; 12976727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr } 12986727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr int return_code = wait_child(pid); 12996727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr if (!WIFEXITED(return_code)) { 13006727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr LOG(WARNING) << "profman failed for package " << pkgname << ": " 13016727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr << return_code; 13026727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr return false; 13036727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr } 13046727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr return true; 13056727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr} 13066727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr 1307edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartierstatic void trim_extension(char* path) { 1308edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier // Trim the extension. 1309edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier int pos = strlen(path); 1310edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier for (; pos >= 0 && path[pos] != '.'; --pos) {} 1311edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier if (pos >= 0) { 1312edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier path[pos] = '\0'; // Trim extension 1313edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier } 1314edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier} 1315edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier 131641fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartierstatic bool add_extension_to_file_name(char* file_name, const char* extension) { 131741fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier if (strlen(file_name) + strlen(extension) + 1 > PKG_PATH_MAX) { 131841fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier return false; 1319edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier } 132041fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier strcat(file_name, extension); 132141fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier return true; 132241fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier} 132341fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier 13245006f3a6bcfbe73cfae96b3f2659ce42e18bc2eaNarayan Kamathstatic int open_output_file(char* file_name, bool recreate, int permissions) { 132541fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier int flags = O_RDWR | O_CREAT; 132641fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier if (recreate) { 13275006f3a6bcfbe73cfae96b3f2659ce42e18bc2eaNarayan Kamath if (unlink(file_name) < 0) { 13285006f3a6bcfbe73cfae96b3f2659ce42e18bc2eaNarayan Kamath if (errno != ENOENT) { 13295006f3a6bcfbe73cfae96b3f2659ce42e18bc2eaNarayan Kamath PLOG(ERROR) << "open_output_file: Couldn't unlink " << file_name; 13305006f3a6bcfbe73cfae96b3f2659ce42e18bc2eaNarayan Kamath } 13315006f3a6bcfbe73cfae96b3f2659ce42e18bc2eaNarayan Kamath } 133241fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier flags |= O_EXCL; 133341fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier } 13345006f3a6bcfbe73cfae96b3f2659ce42e18bc2eaNarayan Kamath return open(file_name, flags, permissions); 1335edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier} 1336edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier 1337edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartierstatic bool set_permissions_and_ownership(int fd, bool is_public, int uid, const char* path) { 1338edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier if (fchmod(fd, 1339edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier S_IRUSR|S_IWUSR|S_IRGRP | 1340edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier (is_public ? S_IROTH : 0)) < 0) { 1341edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier ALOGE("installd cannot chmod '%s' during dexopt\n", path); 1342edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier return false; 1343edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier } else if (fchown(fd, AID_SYSTEM, uid) < 0) { 1344edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier ALOGE("installd cannot chown '%s' during dexopt\n", path); 1345edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier return false; 1346edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier } 1347edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier return true; 1348edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier} 1349edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier 13506a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic bool create_oat_out_path(const char* apk_path, const char* instruction_set, 13516a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle const char* oat_dir, /*out*/ char* out_path) { 13526a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle // Early best-effort check whether we can fit the the path into our buffers. 13536a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle // Note: the cache path will require an additional 5 bytes for ".swap", but we'll try to run 13546a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle // without a swap file, if necessary. Reference profiles file also add an extra ".prof" 13556a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle // extension to the cache path (5 bytes). 13566a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle if (strlen(apk_path) >= (PKG_PATH_MAX - 8)) { 13576a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle ALOGE("apk_path too long '%s'\n", apk_path); 13586a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle return false; 13596a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 13606a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 13616a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle if (oat_dir != NULL && oat_dir[0] != '!') { 13626a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle if (validate_apk_path(oat_dir)) { 13636a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle ALOGE("invalid oat_dir '%s'\n", oat_dir); 13646a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle return false; 13656a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 13666a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle if (!calculate_oat_file_path(out_path, oat_dir, apk_path, instruction_set)) { 13676a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle return false; 13686a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 13696a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } else { 13706a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle if (!create_cache_path(out_path, apk_path, instruction_set)) { 13716a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle return false; 13726a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 13736a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 13746a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle return true; 13756a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle} 13766a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 13774d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe// TODO: Consider returning error codes. 13784d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampebool merge_profiles(uid_t uid, const char *pkgname) { 13794d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe return analyse_profiles(uid, pkgname); 13804d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe} 13814d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe 138260a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravleint dexopt(const char* apk_path, uid_t uid, const char* pkgname, const char* instruction_set, 13834d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe int dexopt_needed, const char* oat_dir, int dexopt_flags, const char* compiler_filter, 1384b63d91fd2737680351876406277b6c759f4db33cJeff Hao const char* volume_uuid ATTRIBUTE_UNUSED, const char* shared_libraries) 138594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 138694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood struct utimbuf ut; 138726ff93c1a783d02c177a47f0a80249070fe43682Fyodor Kupolov struct stat input_stat; 13881705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom char out_path[PKG_PATH_MAX]; 1389e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe char swap_file_name[PKG_PATH_MAX]; 1390edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier char image_path[PKG_PATH_MAX]; 13917365a10689df23334d245b211ce272502ad20669Alex Light const char *input_file; 13927365a10689df23334d245b211ce272502ad20669Alex Light char in_odex_path[PKG_PATH_MAX]; 13936a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle int res; 13946a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle fd_t input_fd=-1, out_fd=-1, image_fd=-1, swap_fd=-1; 1395db0326a56ed8cc1ba2ed5ec2810afe49bcd9a596Calin Juravle bool is_public = ((dexopt_flags & DEXOPT_PUBLIC) != 0); 139676e767ca14bcbb4bc809cd1279ece82a3aabe8a4Todd Kennedy bool vm_safe_mode = (dexopt_flags & DEXOPT_SAFEMODE) != 0; 139776e767ca14bcbb4bc809cd1279ece82a3aabe8a4Todd Kennedy bool debuggable = (dexopt_flags & DEXOPT_DEBUGGABLE) != 0; 139876e767ca14bcbb4bc809cd1279ece82a3aabe8a4Todd Kennedy bool boot_complete = (dexopt_flags & DEXOPT_BOOTCOMPLETE) != 0; 13994d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe bool profile_guided = (dexopt_flags & DEXOPT_PROFILE_GUIDED) != 0; 1400db0326a56ed8cc1ba2ed5ec2810afe49bcd9a596Calin Juravle 14014d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe CHECK(pkgname != nullptr); 14024d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe CHECK(pkgname[0] != 0); 1403db0326a56ed8cc1ba2ed5ec2810afe49bcd9a596Calin Juravle 14044d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe fd_t reference_profile_fd = -1; 14054d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe // Public apps should not be compiled with profile information ever. Same goes for the special 14064d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe // package '*' used for the system server. 14074d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe if (!is_public && pkgname[0] != '*') { 14084d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe // Open reference profile in read only mode as dex2oat does not get write permissions. 14094d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe reference_profile_fd = open_reference_profile(uid, pkgname, /*read_write*/ false); 14104d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe // Note: it's OK to not find a profile here. 141160a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle } 141276e767ca14bcbb4bc809cd1279ece82a3aabe8a4Todd Kennedy 1413e296e00df65461629a20cd03b5b041a59935d00fTodd Kennedy if ((dexopt_flags & ~DEXOPT_MASK) != 0) { 141476e767ca14bcbb4bc809cd1279ece82a3aabe8a4Todd Kennedy LOG_FATAL("dexopt flags contains unknown fields\n"); 141576e767ca14bcbb4bc809cd1279ece82a3aabe8a4Todd Kennedy } 141694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 14176a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle if (!create_oat_out_path(apk_path, instruction_set, oat_dir, out_path)) { 14186a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle return false; 141994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 142094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 1421c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler switch (dexopt_needed) { 1422c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler case DEXOPT_DEX2OAT_NEEDED: 1423c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler input_file = apk_path; 1424c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler break; 1425c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler 1426c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler case DEXOPT_PATCHOAT_NEEDED: 1427c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler if (!calculate_odex_file_path(in_odex_path, apk_path, instruction_set)) { 1428c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler return -1; 1429c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler } 1430c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler input_file = in_odex_path; 1431c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler break; 1432c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler 1433c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler case DEXOPT_SELF_PATCHOAT_NEEDED: 1434c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler input_file = out_path; 1435c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler break; 1436c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler 1437c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler default: 1438c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler ALOGE("Invalid dexopt needed: %d\n", dexopt_needed); 1439c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler exit(72); 14407365a10689df23334d245b211ce272502ad20669Alex Light } 14417365a10689df23334d245b211ce272502ad20669Alex Light 14427365a10689df23334d245b211ce272502ad20669Alex Light memset(&input_stat, 0, sizeof(input_stat)); 14437365a10689df23334d245b211ce272502ad20669Alex Light stat(input_file, &input_stat); 144494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 14457365a10689df23334d245b211ce272502ad20669Alex Light input_fd = open(input_file, O_RDONLY, 0); 14467365a10689df23334d245b211ce272502ad20669Alex Light if (input_fd < 0) { 14477365a10689df23334d245b211ce272502ad20669Alex Light ALOGE("installd cannot open '%s' for input during dexopt\n", input_file); 144894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return -1; 144994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 145094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 14515006f3a6bcfbe73cfae96b3f2659ce42e18bc2eaNarayan Kamath out_fd = open_output_file(out_path, /*recreate*/true, /*permissions*/0644); 14521705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom if (out_fd < 0) { 14531705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom ALOGE("installd cannot open '%s' for output during dexopt\n", out_path); 145494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto fail; 145594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 1456edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier if (!set_permissions_and_ownership(out_fd, is_public, uid, out_path)) { 145794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto fail; 145894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 145994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 1460e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe // Create a swap file if necessary. 1461c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler if (ShouldUseSwapFileForDexopt()) { 1462e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe // Make sure there really is enough space. 1463edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier strcpy(swap_file_name, out_path); 146441fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier if (add_extension_to_file_name(swap_file_name, ".swap")) { 14655006f3a6bcfbe73cfae96b3f2659ce42e18bc2eaNarayan Kamath swap_fd = open_output_file(swap_file_name, /*recreate*/true, /*permissions*/0600); 146641fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier } 1467edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier if (swap_fd < 0) { 1468edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier // Could not create swap file. Optimistically go on and hope that we can compile 1469edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier // without it. 1470edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier ALOGE("installd could not create '%s' for swap during dexopt\n", swap_file_name); 1471e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe } else { 1472edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier // Immediately unlink. We don't really want to hit flash. 14735006f3a6bcfbe73cfae96b3f2659ce42e18bc2eaNarayan Kamath if (unlink(swap_file_name) < 0) { 14745006f3a6bcfbe73cfae96b3f2659ce42e18bc2eaNarayan Kamath PLOG(ERROR) << "Couldn't unlink swap file " << swap_file_name; 14755006f3a6bcfbe73cfae96b3f2659ce42e18bc2eaNarayan Kamath } 1476e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe } 1477e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe } 1478d93707342a61e66bc3eb2145628158452f577f42Dave Allison 1479e80d58db65d29ed4647e631a81aed644fea00913Mathieu Chartier // Avoid generating an app image for extract only since it will not contain any classes. 148041fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier strcpy(image_path, out_path); 148141fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier trim_extension(image_path); 148241fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier if (add_extension_to_file_name(image_path, ".art")) { 148341fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier char app_image_format[kPropertyValueMax]; 148441fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier bool have_app_image_format = 148541fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier get_property("dalvik.vm.appimageformat", app_image_format, NULL) > 0; 14864d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe // Use app images only if it is enabled (by a set image format) and we are compiling 14874d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe // profile-guided (so the app image doesn't conservatively contain all classes). 14884d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe if (profile_guided && have_app_image_format) { 1489a812bcfd06df38a360545ae6655a90a9564c3aa9Mathieu Chartier // Recreate is true since we do not want to modify a mapped image. If the app is already 1490a812bcfd06df38a360545ae6655a90a9564c3aa9Mathieu Chartier // running and we modify the image file, it can cause crashes (b/27493510). 14915006f3a6bcfbe73cfae96b3f2659ce42e18bc2eaNarayan Kamath image_fd = open_output_file(image_path, /*recreate*/true, /*permissions*/0600); 149241fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier if (image_fd < 0) { 149341fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier // Could not create application image file. Go on since we can compile without it. 149441fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier ALOGE("installd could not create '%s' for image file during dexopt\n", image_path); 149541fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier } else if (!set_permissions_and_ownership(image_fd, is_public, uid, image_path)) { 149641fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier image_fd = -1; 149741fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier } 149841fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier } 149941fdcbfa119698122cae9913ea85ea1d2cf9668aMathieu Chartier // If we have a valid image file path but no image fd, erase the image file. 1500e80d58db65d29ed4647e631a81aed644fea00913Mathieu Chartier if (image_fd < 0) { 15015006f3a6bcfbe73cfae96b3f2659ce42e18bc2eaNarayan Kamath if (unlink(image_path) < 0) { 15025006f3a6bcfbe73cfae96b3f2659ce42e18bc2eaNarayan Kamath if (errno != ENOENT) { 15035006f3a6bcfbe73cfae96b3f2659ce42e18bc2eaNarayan Kamath PLOG(ERROR) << "Couldn't unlink image file " << image_path; 15045006f3a6bcfbe73cfae96b3f2659ce42e18bc2eaNarayan Kamath } 15055006f3a6bcfbe73cfae96b3f2659ce42e18bc2eaNarayan Kamath } 1506e80d58db65d29ed4647e631a81aed644fea00913Mathieu Chartier } 1507edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier } 1508e80d58db65d29ed4647e631a81aed644fea00913Mathieu Chartier 15097365a10689df23334d245b211ce272502ad20669Alex Light ALOGV("DexInv: --- BEGIN '%s' ---\n", input_file); 151094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 151194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood pid_t pid; 151294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood pid = fork(); 151394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (pid == 0) { 151494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood /* child -- drop privileges before continuing */ 15156a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle drop_capabilities(uid); 15166a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 151713f141910ba383404145924df64ad6626fe5b42fAndreas Gampe SetDex2OatAndPatchOatScheduling(boot_complete); 15181705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom if (flock(out_fd, LOCK_EX | LOCK_NB) != 0) { 15191705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom ALOGE("flock(%s) failed: %s\n", out_path, strerror(errno)); 152094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood exit(67); 152194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 152294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 1523c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler if (dexopt_needed == DEXOPT_PATCHOAT_NEEDED 1524c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler || dexopt_needed == DEXOPT_SELF_PATCHOAT_NEEDED) { 1525bd872e49561282682793ab526616d609f7fbb274Andreas Gampe run_patchoat(input_fd, out_fd, input_file, out_path, pkgname, instruction_set); 1526c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler } else if (dexopt_needed == DEXOPT_DEX2OAT_NEEDED) { 1527aaa1d4e930f653ec21b711eb852194c8abf35143David Brazdil // Pass dex2oat the relative path to the input file. 1528aaa1d4e930f653ec21b711eb852194c8abf35143David Brazdil const char *input_file_name = strrchr(input_file, '/'); 1529aaa1d4e930f653ec21b711eb852194c8abf35143David Brazdil if (input_file_name == NULL) { 1530aaa1d4e930f653ec21b711eb852194c8abf35143David Brazdil input_file_name = input_file; 1531aaa1d4e930f653ec21b711eb852194c8abf35143David Brazdil } else { 1532aaa1d4e930f653ec21b711eb852194c8abf35143David Brazdil input_file_name++; 1533aaa1d4e930f653ec21b711eb852194c8abf35143David Brazdil } 1534aaa1d4e930f653ec21b711eb852194c8abf35143David Brazdil run_dex2oat(input_fd, out_fd, image_fd, input_file_name, out_path, swap_fd, 15354d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe instruction_set, compiler_filter, vm_safe_mode, debuggable, boot_complete, 1536b63d91fd2737680351876406277b6c759f4db33cJeff Hao reference_profile_fd, shared_libraries); 1537c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler } else { 1538c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler ALOGE("Invalid dexopt needed: %d\n", dexopt_needed); 1539c92fb6247d4c4fbc34c0a5deb26ccf538ca9ec81Richard Uhler exit(73); 15401705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom } 154194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood exit(68); /* only get here on exec failure */ 154294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else { 154363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad res = wait_child(pid); 154463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (res == 0) { 15457365a10689df23334d245b211ce272502ad20669Alex Light ALOGV("DexInv: --- END '%s' (success) ---\n", input_file); 154663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } else { 15477365a10689df23334d245b211ce272502ad20669Alex Light ALOGE("DexInv: --- END '%s' --- status=0x%04x, process failed\n", input_file, res); 154894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto fail; 154994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 155094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 155194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 15527365a10689df23334d245b211ce272502ad20669Alex Light ut.actime = input_stat.st_atime; 15537365a10689df23334d245b211ce272502ad20669Alex Light ut.modtime = input_stat.st_mtime; 15541705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom utime(out_path, &ut); 15551705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom 15561705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom close(out_fd); 15577365a10689df23334d245b211ce272502ad20669Alex Light close(input_fd); 1558edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier if (swap_fd >= 0) { 1559e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe close(swap_fd); 1560e1c01353c4dc6058a384eea9853238d2a87040dbAndreas Gampe } 15616a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle if (reference_profile_fd >= 0) { 15626a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle close(reference_profile_fd); 156360a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle } 1564edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier if (image_fd >= 0) { 1565edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier close(image_fd); 1566edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier } 156794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return 0; 156894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 156994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodfail: 15701705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom if (out_fd >= 0) { 15711705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom close(out_fd); 15721705fc44fb85c4232637f9f7189c3bdca98a63d5Brian Carlstrom unlink(out_path); 157394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 15747365a10689df23334d245b211ce272502ad20669Alex Light if (input_fd >= 0) { 15757365a10689df23334d245b211ce272502ad20669Alex Light close(input_fd); 157694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 15776a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle if (reference_profile_fd >= 0) { 15786a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle close(reference_profile_fd); 15796a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle // We failed to compile. Unlink the reference profile. Current profiles are already unlinked 15806a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle // when profmoan advises compilation. 1581cea31418f34680fcfea4ec885dc966ad3d76354fCalin Juravle clear_reference_profile(pkgname); 158260a794dc845b942907ca035ddc554eb8e0c7ebb6Calin Juravle } 1583edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier if (swap_fd >= 0) { 1584edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier close(swap_fd); 1585edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier } 1586edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier if (image_fd >= 0) { 1587edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier close(image_fd); 1588edc8bc48fbf143e35578bf6cccf980dfab076196Mathieu Chartier } 158994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return -1; 159094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 159194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 1592091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamathint mark_boot_complete(const char* instruction_set) 1593091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath{ 1594091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath char boot_marker_path[PKG_PATH_MAX]; 159502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe sprintf(boot_marker_path, 159602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe "%s/%s/%s/.booting", 159702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe android_data_dir.path, 159802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe DALVIK_CACHE, 159902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe instruction_set); 1600091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath 1601091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath ALOGV("mark_boot_complete : %s", boot_marker_path); 1602091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath if (unlink(boot_marker_path) != 0) { 1603091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath ALOGE("Unable to unlink boot marker at %s, error=%s", boot_marker_path, 1604091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath strerror(errno)); 1605091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath return -1; 1606091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath } 1607091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath 1608091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath return 0; 1609091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath} 1610091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath 161194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodvoid mkinnerdirs(char* path, int basepos, mode_t mode, int uid, int gid, 161294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood struct stat* statbuf) 161394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 161494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood while (path[basepos] != 0) { 161594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (path[basepos] == '/') { 161694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood path[basepos] = 0; 161794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (lstat(path, statbuf) < 0) { 161894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGV("Making directory: %s\n", path); 161994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (mkdir(path, mode) == 0) { 162094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood chown(path, uid, gid); 162194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else { 162294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGW("Unable to make directory %s: %s\n", path, strerror(errno)); 162394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 162494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 162594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood path[basepos] = '/'; 162694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood basepos++; 162794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 162894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood basepos++; 162994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 163094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 163194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 1632c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkeyint linklib(const char* uuid, const char* pkgname, const char* asecLibDir, int userId) 163394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 163494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood struct stat s, libStat; 163594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int rc = 0; 163694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 16372f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey std::string _pkgdir(create_data_user_ce_package_path(uuid, userId, pkgname)); 1638c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey std::string _libsymlink(_pkgdir + PKG_LIB_POSTFIX); 1639c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey 1640c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey const char* pkgdir = _pkgdir.c_str(); 1641c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey const char* libsymlink = _libsymlink.c_str(); 164294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 164394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (stat(pkgdir, &s) < 0) return -1; 164494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 164594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (chown(pkgdir, AID_INSTALL, AID_INSTALL) < 0) { 164694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("failed to chown '%s': %s\n", pkgdir, strerror(errno)); 164794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return -1; 164894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 164994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 165094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (chmod(pkgdir, 0700) < 0) { 165194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("linklib() 1: failed to chmod '%s': %s\n", pkgdir, strerror(errno)); 165294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood rc = -1; 165394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto out; 165494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 165594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 165694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (lstat(libsymlink, &libStat) < 0) { 165794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (errno != ENOENT) { 165894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("couldn't stat lib dir: %s\n", strerror(errno)); 165994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood rc = -1; 166094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto out; 166194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 166294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else { 166394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (S_ISDIR(libStat.st_mode)) { 16643aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath if (delete_dir_contents(libsymlink, 1, NULL) < 0) { 166594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood rc = -1; 166694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto out; 166794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 166894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else if (S_ISLNK(libStat.st_mode)) { 166994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (unlink(libsymlink) < 0) { 167094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("couldn't unlink lib dir: %s\n", strerror(errno)); 167194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood rc = -1; 167294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto out; 167394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 167494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 167594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 167694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 167794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (symlink(asecLibDir, libsymlink) < 0) { 167894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("couldn't symlink directory '%s' -> '%s': %s\n", libsymlink, asecLibDir, 167994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood strerror(errno)); 168094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood rc = -errno; 168194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto out; 168294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 168394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 168494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodout: 168594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (chmod(pkgdir, s.st_mode) < 0) { 168694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("linklib() 2: failed to chmod '%s': %s\n", pkgdir, strerror(errno)); 168794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood rc = -errno; 168894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 168994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 169094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (chown(pkgdir, s.st_uid, s.st_gid) < 0) { 169194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("failed to chown '%s' : %s\n", pkgdir, strerror(errno)); 169294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return -errno; 169394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 169494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 169594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return rc; 169694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 169763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 169863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstadstatic void run_idmap(const char *target_apk, const char *overlay_apk, int idmap_fd) 169963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad{ 170063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad static const char *IDMAP_BIN = "/system/bin/idmap"; 170163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad static const size_t MAX_INT_LEN = 32; 170263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad char idmap_str[MAX_INT_LEN]; 170363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 170463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad snprintf(idmap_str, sizeof(idmap_str), "%d", idmap_fd); 170563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 170663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad execl(IDMAP_BIN, IDMAP_BIN, "--fd", target_apk, overlay_apk, idmap_str, (char*)NULL); 170763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("execl(%s) failed: %s\n", IDMAP_BIN, strerror(errno)); 170863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad} 170963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 171063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad// Transform string /a/b/c.apk to (prefix)/a@b@c.apk@(suffix) 171163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad// eg /a/b/c.apk to /data/resource-cache/a@b@c.apk@idmap 171263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstadstatic int flatten_path(const char *prefix, const char *suffix, 171363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad const char *overlay_path, char *idmap_path, size_t N) 171463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad{ 171563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (overlay_path == NULL || idmap_path == NULL) { 171663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad return -1; 171763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 171863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad const size_t len_overlay_path = strlen(overlay_path); 171963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad // will access overlay_path + 1 further below; requires absolute path 172063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (len_overlay_path < 2 || *overlay_path != '/') { 172163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad return -1; 172263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 172363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad const size_t len_idmap_root = strlen(prefix); 172463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad const size_t len_suffix = strlen(suffix); 172563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (SIZE_MAX - len_idmap_root < len_overlay_path || 172663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad SIZE_MAX - (len_idmap_root + len_overlay_path) < len_suffix) { 172763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad // additions below would cause overflow 172863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad return -1; 172963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 173063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (N < len_idmap_root + len_overlay_path + len_suffix) { 173163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad return -1; 173263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 173363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad memset(idmap_path, 0, N); 173463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad snprintf(idmap_path, N, "%s%s%s", prefix, overlay_path + 1, suffix); 173563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad char *ch = idmap_path + len_idmap_root; 173663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad while (*ch != '\0') { 173763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (*ch == '/') { 173863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad *ch = '@'; 173963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 174063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ++ch; 174163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 174263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad return 0; 174363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad} 174463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 174563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstadint idmap(const char *target_apk, const char *overlay_apk, uid_t uid) 174663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad{ 174763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGV("idmap target_apk=%s overlay_apk=%s uid=%d\n", target_apk, overlay_apk, uid); 174863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 174963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad int idmap_fd = -1; 175063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad char idmap_path[PATH_MAX]; 175163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 175263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (flatten_path(IDMAP_PREFIX, IDMAP_SUFFIX, overlay_apk, 175363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad idmap_path, sizeof(idmap_path)) == -1) { 175463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("idmap cannot generate idmap path for overlay %s\n", overlay_apk); 175563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad goto fail; 175663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 175763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 175863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad unlink(idmap_path); 175963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad idmap_fd = open(idmap_path, O_RDWR | O_CREAT | O_EXCL, 0644); 176063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (idmap_fd < 0) { 176163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("idmap cannot open '%s' for output: %s\n", idmap_path, strerror(errno)); 176263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad goto fail; 176363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 176463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (fchown(idmap_fd, AID_SYSTEM, uid) < 0) { 176563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("idmap cannot chown '%s'\n", idmap_path); 176663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad goto fail; 176763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 176863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (fchmod(idmap_fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0) { 176963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("idmap cannot chmod '%s'\n", idmap_path); 177063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad goto fail; 177163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 177263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 177363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad pid_t pid; 177463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad pid = fork(); 177563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (pid == 0) { 177663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad /* child -- drop privileges before continuing */ 177763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (setgid(uid) != 0) { 177863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("setgid(%d) failed during idmap\n", uid); 177963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad exit(1); 178063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 178163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (setuid(uid) != 0) { 178263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("setuid(%d) failed during idmap\n", uid); 178363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad exit(1); 178463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 178563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (flock(idmap_fd, LOCK_EX | LOCK_NB) != 0) { 178663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("flock(%s) failed during idmap: %s\n", idmap_path, strerror(errno)); 178763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad exit(1); 178863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 178963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 179063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad run_idmap(target_apk, overlay_apk, idmap_fd); 179163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad exit(1); /* only if exec call to idmap failed */ 179263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } else { 179363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad int status = wait_child(pid); 179463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (status != 0) { 179563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("idmap failed, status=0x%04x\n", status); 179663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad goto fail; 179763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 179863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 179963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 180063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad close(idmap_fd); 180163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad return 0; 180263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstadfail: 180363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (idmap_fd >= 0) { 180463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad close(idmap_fd); 180563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad unlink(idmap_path); 180663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 180763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad return -1; 180863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad} 1809e9887e46cea4a095e4219927eadbe4c57bb1a5eeRobert Craig 1810c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkeyint restorecon_app_data(const char* uuid, const char* pkgName, userid_t userid, int flags, 1811c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey appid_t appid, const char* seinfo) { 1812ebf728fd43ab5c7d11a1f9e5fdc775d6740fae0aJeff Sharkey int res = 0; 1813e9887e46cea4a095e4219927eadbe4c57bb1a5eeRobert Craig 1814da30dc7336f03ca629fe173db1425fdce989119cRobert Craig // SELINUX_ANDROID_RESTORECON_DATADATA flag is set by libselinux. Not needed here. 1815c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey unsigned int seflags = SELINUX_ANDROID_RESTORECON_RECURSE; 1816e9887e46cea4a095e4219927eadbe4c57bb1a5eeRobert Craig 1817da30dc7336f03ca629fe173db1425fdce989119cRobert Craig if (!pkgName || !seinfo) { 1818da30dc7336f03ca629fe173db1425fdce989119cRobert Craig ALOGE("Package name or seinfo tag is null when trying to restorecon."); 1819da30dc7336f03ca629fe173db1425fdce989119cRobert Craig return -1; 1820da30dc7336f03ca629fe173db1425fdce989119cRobert Craig } 1821e9887e46cea4a095e4219927eadbe4c57bb1a5eeRobert Craig 1822c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey uid_t uid = multiuser_get_uid(userid, appid); 1823aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (flags & FLAG_STORAGE_CE) { 18242f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey auto path = create_data_user_ce_package_path(uuid, userid, pkgName); 1825c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey if (selinux_android_restorecon_pkgdir(path.c_str(), seinfo, uid, seflags) < 0) { 1826c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey PLOG(ERROR) << "restorecon failed for " << path; 1827ebf728fd43ab5c7d11a1f9e5fdc775d6740fae0aJeff Sharkey res = -1; 1828da30dc7336f03ca629fe173db1425fdce989119cRobert Craig } 1829c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey } 1830aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (flags & FLAG_STORAGE_DE) { 1831c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey auto path = create_data_user_de_package_path(uuid, userid, pkgName); 1832c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey if (selinux_android_restorecon_pkgdir(path.c_str(), seinfo, uid, seflags) < 0) { 1833c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey PLOG(ERROR) << "restorecon failed for " << path; 1834ea0e4b19d942f62ea21ca4e70f939f3d4aa14b37Jeff Sharkey // TODO: include result once 25796509 is fixed 1835da30dc7336f03ca629fe173db1425fdce989119cRobert Craig } 1836da30dc7336f03ca629fe173db1425fdce989119cRobert Craig } 1837da30dc7336f03ca629fe173db1425fdce989119cRobert Craig 1838ebf728fd43ab5c7d11a1f9e5fdc775d6740fae0aJeff Sharkey return res; 1839e9887e46cea4a095e4219927eadbe4c57bb1a5eeRobert Craig} 18403aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath 184188ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolovint create_oat_dir(const char* oat_dir, const char* instruction_set) 184288ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov{ 184388ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov char oat_instr_dir[PKG_PATH_MAX]; 184488ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov 184588ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov if (validate_apk_path(oat_dir)) { 184688ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov ALOGE("invalid apk path '%s' (bad prefix)\n", oat_dir); 184788ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov return -1; 184888ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov } 18498eed7e6a9bb527af7380ec13b390c6e2955eba6aFyodor Kupolov if (fs_prepare_dir(oat_dir, S_IRWXU | S_IRWXG | S_IXOTH, AID_SYSTEM, AID_INSTALL)) { 185088ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov return -1; 185188ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov } 185288ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov if (selinux_android_restorecon(oat_dir, 0)) { 185388ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov ALOGE("cannot restorecon dir '%s': %s\n", oat_dir, strerror(errno)); 185488ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov return -1; 185588ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov } 185688ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov snprintf(oat_instr_dir, PKG_PATH_MAX, "%s/%s", oat_dir, instruction_set); 18578eed7e6a9bb527af7380ec13b390c6e2955eba6aFyodor Kupolov if (fs_prepare_dir(oat_instr_dir, S_IRWXU | S_IRWXG | S_IXOTH, AID_SYSTEM, AID_INSTALL)) { 185888ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov return -1; 185988ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov } 186088ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov return 0; 186188ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov} 186288ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov 186388ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolovint rm_package_dir(const char* apk_path) 186488ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov{ 186588ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov if (validate_apk_path(apk_path)) { 186688ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov ALOGE("invalid apk path '%s' (bad prefix)\n", apk_path); 186788ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov return -1; 186888ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov } 186988ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov return delete_dir_contents(apk_path, 1 /* also_delete_dir */ , NULL /* exclusion_predicate */); 187088ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov} 187188ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov 1872d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamathint link_file(const char* relative_path, const char* from_base, const char* to_base) { 1873d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath char from_path[PKG_PATH_MAX]; 1874d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath char to_path[PKG_PATH_MAX]; 1875d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath snprintf(from_path, PKG_PATH_MAX, "%s/%s", from_base, relative_path); 1876d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath snprintf(to_path, PKG_PATH_MAX, "%s/%s", to_base, relative_path); 1877d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath 1878d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath if (validate_apk_path_subdirs(from_path)) { 1879d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath ALOGE("invalid app data sub-path '%s' (bad prefix)\n", from_path); 1880d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath return -1; 1881d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath } 1882d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath 1883d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath if (validate_apk_path_subdirs(to_path)) { 1884d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath ALOGE("invalid app data sub-path '%s' (bad prefix)\n", to_path); 1885d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath return -1; 1886d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath } 1887d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath 1888d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath const int ret = link(from_path, to_path); 1889d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath if (ret < 0) { 1890d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath ALOGE("link(%s, %s) failed : %s", from_path, to_path, strerror(errno)); 1891d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath return -1; 1892d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath } 1893d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath 1894d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath return 0; 1895d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath} 1896d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath 1897e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe// Helper for move_ab, so that we can have common failure-case cleanup. 1898e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampestatic bool unlink_and_rename(const char* from, const char* to) { 1899e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe // Check whether "from" exists, and if so whether it's regular. If it is, unlink. Otherwise, 1900e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe // return a failure. 1901e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe struct stat s; 1902e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe if (stat(to, &s) == 0) { 1903e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe if (!S_ISREG(s.st_mode)) { 1904e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe LOG(ERROR) << from << " is not a regular file to replace for A/B."; 1905e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe return false; 1906e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe } 1907e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe if (unlink(to) != 0) { 1908e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe LOG(ERROR) << "Could not unlink " << to << " to move A/B."; 1909e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe return false; 1910e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe } 1911e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe } else { 1912e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe // This may be a permission problem. We could investigate the error code, but we'll just 1913e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe // let the rename failure do the work for us. 1914e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe } 1915e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe 1916e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe // Try to rename "to" to "from." 1917e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe if (rename(from, to) != 0) { 1918e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe PLOG(ERROR) << "Could not rename " << from << " to " << to; 1919e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe return false; 1920e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe } 1921e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe 1922e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe return true; 1923e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe} 1924e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe 1925e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampeint move_ab(const char* apk_path, const char* instruction_set, const char* oat_dir) { 1926e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe if (apk_path == nullptr || instruction_set == nullptr || oat_dir == nullptr) { 1927e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe LOG(ERROR) << "Cannot move_ab with null input"; 1928e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe return -1; 1929e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe } 1930e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe if (validate_apk_path(apk_path) != 0) { 1931e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe LOG(ERROR) << "invalid apk_path " << apk_path; 1932e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe return -1; 1933e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe } 1934e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe if (validate_apk_path(oat_dir) != 0) { 1935e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe LOG(ERROR) << "invalid oat_dir " << oat_dir; 1936e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe return -1; 1937e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe } 1938e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe 1939e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe char a_path[PKG_PATH_MAX]; 1940e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe if (!calculate_oat_file_path(a_path, oat_dir, apk_path, instruction_set)) { 1941e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe return -1; 1942e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe } 1943e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe 1944e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe // B path = A path + ".b" 1945e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe std::string b_path = StringPrintf("%s.b", a_path); 1946e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe 1947e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe // Check whether B exists. 1948e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe { 1949e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe struct stat s; 1950e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe if (stat(b_path.c_str(), &s) != 0) { 19512438c6f59cc293a887e65cd34738889043a75d46Andreas Gampe // Silently ignore for now. The service calling this isn't smart enough to understand 19522438c6f59cc293a887e65cd34738889043a75d46Andreas Gampe // lack of artifacts at the moment. 1953e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe return -1; 1954e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe } 1955e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe if (!S_ISREG(s.st_mode)) { 1956e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe LOG(ERROR) << "A/B artifact " << b_path << " is not a regular file."; 1957e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe // Try to unlink, but swallow errors. 1958e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe unlink(b_path.c_str()); 1959e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe return -1; 1960e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe } 1961e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe } 1962e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe 1963e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe // Rename B to A. 1964e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe if (!unlink_and_rename(b_path.c_str(), a_path)) { 1965e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe // Delete the b_path so we don't try again (or fail earlier). 1966e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe if (unlink(b_path.c_str()) != 0) { 1967e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe PLOG(ERROR) << "Could not unlink " << b_path; 1968e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe } 1969e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe 1970e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe return -1; 1971e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe } 1972e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe 1973e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe return 0; 1974e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe} 1975e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe 197602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe} // namespace installd 197702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe} // namespace android 1978