| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | ** Copyright 2016, The Android Open Source Project | 
|  | 3 | ** | 
|  | 4 | ** Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | ** you may not use this file except in compliance with the License. | 
|  | 6 | ** You may obtain a copy of the License at | 
|  | 7 | ** | 
|  | 8 | **     http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | ** | 
|  | 10 | ** Unless required by applicable law or agreed to in writing, software | 
|  | 11 | ** distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | ** See the License for the specific language governing permissions and | 
|  | 14 | ** limitations under the License. | 
|  | 15 | */ | 
|  | 16 |  | 
|  | 17 | #include <algorithm> | 
|  | 18 | #include <inttypes.h> | 
| Andreas Gampe | c4ced4f | 2017-04-14 20:39:56 -0700 | [diff] [blame] | 19 | #include <limits> | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 20 | #include <random> | 
| Andreas Gampe | 1842af3 | 2016-03-16 14:28:50 -0700 | [diff] [blame] | 21 | #include <regex> | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 22 | #include <selinux/android.h> | 
|  | 23 | #include <selinux/avc.h> | 
|  | 24 | #include <stdlib.h> | 
|  | 25 | #include <string.h> | 
|  | 26 | #include <sys/capability.h> | 
|  | 27 | #include <sys/prctl.h> | 
|  | 28 | #include <sys/stat.h> | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 29 |  | 
|  | 30 | #include <android-base/logging.h> | 
|  | 31 | #include <android-base/macros.h> | 
|  | 32 | #include <android-base/stringprintf.h> | 
| Andreas Gampe | 6db8db9 | 2016-06-03 10:22:19 -0700 | [diff] [blame] | 33 | #include <android-base/strings.h> | 
| Andreas Gampe | ce9fe7f | 2018-09-18 10:25:58 -0700 | [diff] [blame] | 34 | #include <art_image_values.h> | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 35 | #include <cutils/fs.h> | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 36 | #include <cutils/properties.h> | 
| Andreas Gampe | 54e1a40 | 2017-03-20 18:42:49 -0700 | [diff] [blame] | 37 | #include <dex2oat_return_codes.h> | 
| Mark Salyzyn | 7823e12 | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 38 | #include <log/log.h> | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 39 | #include <private/android_filesystem_config.h> | 
|  | 40 |  | 
| Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 41 | #include "dexopt.h" | 
| Jeff Sharkey | f3e30b9 | 2016-12-09 17:06:57 -0700 | [diff] [blame] | 42 | #include "file_parsing.h" | 
|  | 43 | #include "globals.h" | 
| Andreas Gampe | c4ced4f | 2017-04-14 20:39:56 -0700 | [diff] [blame] | 44 | #include "installd_constants.h" | 
| Jeff Sharkey | f3e30b9 | 2016-12-09 17:06:57 -0700 | [diff] [blame] | 45 | #include "installd_deps.h"  // Need to fill in requirements of commands. | 
| Calin Juravle | c9e7679 | 2018-02-01 14:44:56 +0000 | [diff] [blame] | 46 | #include "otapreopt_parameters.h" | 
| Jeff Sharkey | f3e30b9 | 2016-12-09 17:06:57 -0700 | [diff] [blame] | 47 | #include "otapreopt_utils.h" | 
|  | 48 | #include "system_properties.h" | 
|  | 49 | #include "utils.h" | 
| Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 50 |  | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 51 | #ifndef LOG_TAG | 
|  | 52 | #define LOG_TAG "otapreopt" | 
|  | 53 | #endif | 
|  | 54 |  | 
|  | 55 | #define BUFFER_MAX    1024  /* input buffer for commands */ | 
|  | 56 | #define TOKEN_MAX     16    /* max number of arguments in buffer */ | 
|  | 57 | #define REPLY_MAX     256   /* largest reply allowed */ | 
|  | 58 |  | 
| Andreas Gampe | 56f79f9 | 2016-06-08 15:11:37 -0700 | [diff] [blame] | 59 | using android::base::EndsWith; | 
| Andreas Gampe | 6db8db9 | 2016-06-03 10:22:19 -0700 | [diff] [blame] | 60 | using android::base::Split; | 
| Andreas Gampe | 56f79f9 | 2016-06-08 15:11:37 -0700 | [diff] [blame] | 61 | using android::base::StartsWith; | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 62 | using android::base::StringPrintf; | 
|  | 63 |  | 
|  | 64 | namespace android { | 
|  | 65 | namespace installd { | 
|  | 66 |  | 
| Andreas Gampe | ef21fd2 | 2017-05-22 13:36:06 -0700 | [diff] [blame] | 67 | // Check expected values for dexopt flags. If you need to change this: | 
|  | 68 | // | 
|  | 69 | //   RUN AN A/B OTA TO MAKE SURE THINGS STILL WORK! | 
|  | 70 | // | 
|  | 71 | // You most likely need to increase the protocol version and all that entails! | 
|  | 72 |  | 
|  | 73 | static_assert(DEXOPT_PUBLIC         == 1 << 1, "DEXOPT_PUBLIC unexpected."); | 
|  | 74 | static_assert(DEXOPT_DEBUGGABLE     == 1 << 2, "DEXOPT_DEBUGGABLE unexpected."); | 
|  | 75 | static_assert(DEXOPT_BOOTCOMPLETE   == 1 << 3, "DEXOPT_BOOTCOMPLETE unexpected."); | 
|  | 76 | static_assert(DEXOPT_PROFILE_GUIDED == 1 << 4, "DEXOPT_PROFILE_GUIDED unexpected."); | 
|  | 77 | static_assert(DEXOPT_SECONDARY_DEX  == 1 << 5, "DEXOPT_SECONDARY_DEX unexpected."); | 
|  | 78 | static_assert(DEXOPT_FORCE          == 1 << 6, "DEXOPT_FORCE unexpected."); | 
|  | 79 | static_assert(DEXOPT_STORAGE_CE     == 1 << 7, "DEXOPT_STORAGE_CE unexpected."); | 
|  | 80 | static_assert(DEXOPT_STORAGE_DE     == 1 << 8, "DEXOPT_STORAGE_DE unexpected."); | 
| David Brazdil | 22cce5a | 2018-02-12 18:04:59 -0800 | [diff] [blame] | 81 | static_assert(DEXOPT_ENABLE_HIDDEN_API_CHECKS == 1 << 10, | 
|  | 82 | "DEXOPT_ENABLE_HIDDEN_API_CHECKS unexpected"); | 
| Mathieu Chartier | 351bc94 | 2018-03-06 13:55:58 -0800 | [diff] [blame] | 83 | static_assert(DEXOPT_GENERATE_COMPACT_DEX == 1 << 11, "DEXOPT_GENERATE_COMPACT_DEX unexpected"); | 
| Mathieu Chartier | ad45a1b | 2018-03-12 17:55:06 -0700 | [diff] [blame] | 84 | static_assert(DEXOPT_GENERATE_APP_IMAGE == 1 << 12, "DEXOPT_GENERATE_APP_IMAGE unexpected"); | 
| Andreas Gampe | ef21fd2 | 2017-05-22 13:36:06 -0700 | [diff] [blame] | 85 |  | 
| Mathieu Chartier | ad45a1b | 2018-03-12 17:55:06 -0700 | [diff] [blame] | 86 | static_assert(DEXOPT_MASK           == (0x1dfe | DEXOPT_IDLE_BACKGROUND_JOB), | 
| Andreas Gampe | d32eec2 | 2018-02-28 16:02:51 -0800 | [diff] [blame] | 87 | "DEXOPT_MASK unexpected."); | 
| Andreas Gampe | ef21fd2 | 2017-05-22 13:36:06 -0700 | [diff] [blame] | 88 |  | 
|  | 89 |  | 
| Andreas Gampe | a64a627 | 2018-07-10 10:43:47 -0700 | [diff] [blame] | 90 | template<typename T> | 
|  | 91 | static constexpr bool IsPowerOfTwo(T x) { | 
|  | 92 | static_assert(std::is_integral<T>::value, "T must be integral"); | 
|  | 93 | // TODO: assert unsigned. There is currently many uses with signed values. | 
|  | 94 | return (x & (x - 1)) == 0; | 
|  | 95 | } | 
| Andreas Gampe | ef21fd2 | 2017-05-22 13:36:06 -0700 | [diff] [blame] | 96 |  | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 97 | template<typename T> | 
|  | 98 | static constexpr T RoundDown(T x, typename std::decay<T>::type n) { | 
|  | 99 | return DCHECK_CONSTEXPR(IsPowerOfTwo(n), , T(0))(x & -n); | 
|  | 100 | } | 
|  | 101 |  | 
|  | 102 | template<typename T> | 
|  | 103 | static constexpr T RoundUp(T x, typename std::remove_reference<T>::type n) { | 
|  | 104 | return RoundDown(x + n - 1, n); | 
|  | 105 | } | 
|  | 106 |  | 
|  | 107 | class OTAPreoptService { | 
|  | 108 | public: | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 109 | // Main driver. Performs the following steps. | 
|  | 110 | // | 
|  | 111 | // 1) Parse options (read system properties etc from B partition). | 
|  | 112 | // | 
|  | 113 | // 2) Read in package data. | 
|  | 114 | // | 
|  | 115 | // 3) Prepare environment variables. | 
|  | 116 | // | 
|  | 117 | // 4) Prepare(compile) boot image, if necessary. | 
|  | 118 | // | 
|  | 119 | // 5) Run update. | 
|  | 120 | int Main(int argc, char** argv) { | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 121 | if (!ReadArguments(argc, argv)) { | 
|  | 122 | LOG(ERROR) << "Failed reading command line."; | 
|  | 123 | return 1; | 
|  | 124 | } | 
|  | 125 |  | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 126 | if (!ReadSystemProperties()) { | 
|  | 127 | LOG(ERROR)<< "Failed reading system properties."; | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 128 | return 2; | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 129 | } | 
|  | 130 |  | 
|  | 131 | if (!ReadEnvironment()) { | 
|  | 132 | LOG(ERROR) << "Failed reading environment properties."; | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 133 | return 3; | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 134 | } | 
|  | 135 |  | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 136 | if (!CheckAndInitializeInstalldGlobals()) { | 
|  | 137 | LOG(ERROR) << "Failed initializing globals."; | 
|  | 138 | return 4; | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 139 | } | 
|  | 140 |  | 
| Orion Hodson | e695e01 | 2020-03-26 09:24:41 +0000 | [diff] [blame] | 141 | PrepareEnvironmentVariables(); | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 142 |  | 
| Orion Hodson | e695e01 | 2020-03-26 09:24:41 +0000 | [diff] [blame] | 143 | if (!EnsureBootImageAndDalvikCache()) { | 
|  | 144 | LOG(ERROR) << "Bad boot image."; | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 145 | return 5; | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 146 | } | 
|  | 147 |  | 
|  | 148 | int dexopt_retcode = RunPreopt(); | 
|  | 149 |  | 
|  | 150 | return dexopt_retcode; | 
|  | 151 | } | 
|  | 152 |  | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 153 | int GetProperty(const char* key, char* value, const char* default_value) const { | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 154 | const std::string* prop_value = system_properties_.GetProperty(key); | 
|  | 155 | if (prop_value == nullptr) { | 
|  | 156 | if (default_value == nullptr) { | 
|  | 157 | return 0; | 
|  | 158 | } | 
|  | 159 | // Copy in the default value. | 
| Jeff Sharkey | 1b9d9a6 | 2017-09-21 14:51:09 -0600 | [diff] [blame] | 160 | strlcpy(value, default_value, kPropertyValueMax - 1); | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 161 | value[kPropertyValueMax - 1] = 0; | 
|  | 162 | return strlen(default_value);// TODO: Need to truncate? | 
|  | 163 | } | 
| Andreas Gampe | 5696e63 | 2017-09-26 20:41:48 -0700 | [diff] [blame] | 164 | size_t size = std::min(kPropertyValueMax - 1, prop_value->length()) + 1; | 
| Jeff Sharkey | 1b9d9a6 | 2017-09-21 14:51:09 -0600 | [diff] [blame] | 165 | strlcpy(value, prop_value->data(), size); | 
| Andreas Gampe | 5696e63 | 2017-09-26 20:41:48 -0700 | [diff] [blame] | 166 | return static_cast<int>(size - 1); | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 167 | } | 
|  | 168 |  | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 169 | std::string GetOTADataDirectory() const { | 
| Calin Juravle | c9e7679 | 2018-02-01 14:44:56 +0000 | [diff] [blame] | 170 | return StringPrintf("%s/%s", GetOtaDirectoryPrefix().c_str(), GetTargetSlot().c_str()); | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 171 | } | 
|  | 172 |  | 
|  | 173 | const std::string& GetTargetSlot() const { | 
| Calin Juravle | c9e7679 | 2018-02-01 14:44:56 +0000 | [diff] [blame] | 174 | return parameters_.target_slot; | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 175 | } | 
|  | 176 |  | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 177 | private: | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 178 |  | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 179 | bool ReadSystemProperties() { | 
| Andreas Gampe | 1842af3 | 2016-03-16 14:28:50 -0700 | [diff] [blame] | 180 | static constexpr const char* kPropertyFiles[] = { | 
|  | 181 | "/default.prop", "/system/build.prop" | 
|  | 182 | }; | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 183 |  | 
| Andreas Gampe | 1842af3 | 2016-03-16 14:28:50 -0700 | [diff] [blame] | 184 | for (size_t i = 0; i < arraysize(kPropertyFiles); ++i) { | 
|  | 185 | if (!system_properties_.Load(kPropertyFiles[i])) { | 
|  | 186 | return false; | 
|  | 187 | } | 
|  | 188 | } | 
|  | 189 |  | 
|  | 190 | return true; | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 191 | } | 
|  | 192 |  | 
|  | 193 | bool ReadEnvironment() { | 
| Andreas Gampe | 1842af3 | 2016-03-16 14:28:50 -0700 | [diff] [blame] | 194 | // Parse the environment variables from init.environ.rc, which have the form | 
|  | 195 | //   export NAME VALUE | 
|  | 196 | // For simplicity, don't respect string quotation. The values we are interested in can be | 
|  | 197 | // encoded without them. | 
|  | 198 | std::regex export_regex("\\s*export\\s+(\\S+)\\s+(\\S+)"); | 
|  | 199 | bool parse_result = ParseFile("/init.environ.rc", [&](const std::string& line) { | 
|  | 200 | std::smatch export_match; | 
|  | 201 | if (!std::regex_match(line, export_match, export_regex)) { | 
|  | 202 | return true; | 
|  | 203 | } | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 204 |  | 
| Andreas Gampe | 1842af3 | 2016-03-16 14:28:50 -0700 | [diff] [blame] | 205 | if (export_match.size() != 3) { | 
|  | 206 | return true; | 
|  | 207 | } | 
|  | 208 |  | 
|  | 209 | std::string name = export_match[1].str(); | 
|  | 210 | std::string value = export_match[2].str(); | 
|  | 211 |  | 
|  | 212 | system_properties_.SetProperty(name, value); | 
|  | 213 |  | 
|  | 214 | return true; | 
|  | 215 | }); | 
|  | 216 | if (!parse_result) { | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 217 | return false; | 
|  | 218 | } | 
| Andreas Gampe | 1842af3 | 2016-03-16 14:28:50 -0700 | [diff] [blame] | 219 |  | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 220 | if (system_properties_.GetProperty(kAndroidDataPathPropertyName) == nullptr) { | 
|  | 221 | return false; | 
|  | 222 | } | 
|  | 223 | android_data_ = *system_properties_.GetProperty(kAndroidDataPathPropertyName); | 
|  | 224 |  | 
|  | 225 | if (system_properties_.GetProperty(kAndroidRootPathPropertyName) == nullptr) { | 
|  | 226 | return false; | 
|  | 227 | } | 
|  | 228 | android_root_ = *system_properties_.GetProperty(kAndroidRootPathPropertyName); | 
|  | 229 |  | 
|  | 230 | if (system_properties_.GetProperty(kBootClassPathPropertyName) == nullptr) { | 
|  | 231 | return false; | 
|  | 232 | } | 
|  | 233 | boot_classpath_ = *system_properties_.GetProperty(kBootClassPathPropertyName); | 
|  | 234 |  | 
|  | 235 | if (system_properties_.GetProperty(ASEC_MOUNTPOINT_ENV_NAME) == nullptr) { | 
|  | 236 | return false; | 
|  | 237 | } | 
|  | 238 | asec_mountpoint_ = *system_properties_.GetProperty(ASEC_MOUNTPOINT_ENV_NAME); | 
|  | 239 |  | 
|  | 240 | return true; | 
|  | 241 | } | 
|  | 242 |  | 
|  | 243 | const std::string& GetAndroidData() const { | 
|  | 244 | return android_data_; | 
|  | 245 | } | 
|  | 246 |  | 
|  | 247 | const std::string& GetAndroidRoot() const { | 
|  | 248 | return android_root_; | 
|  | 249 | } | 
|  | 250 |  | 
|  | 251 | const std::string GetOtaDirectoryPrefix() const { | 
|  | 252 | return GetAndroidData() + "/ota"; | 
|  | 253 | } | 
|  | 254 |  | 
|  | 255 | bool CheckAndInitializeInstalldGlobals() { | 
|  | 256 | // init_globals_from_data_and_root requires "ASEC_MOUNTPOINT" in the environment. We | 
|  | 257 | // do not use any datapath that includes this, but we'll still have to set it. | 
|  | 258 | CHECK(system_properties_.GetProperty(ASEC_MOUNTPOINT_ENV_NAME) != nullptr); | 
|  | 259 | int result = setenv(ASEC_MOUNTPOINT_ENV_NAME, asec_mountpoint_.c_str(), 0); | 
|  | 260 | if (result != 0) { | 
|  | 261 | LOG(ERROR) << "Could not set ASEC_MOUNTPOINT environment variable"; | 
|  | 262 | return false; | 
|  | 263 | } | 
|  | 264 |  | 
|  | 265 | if (!init_globals_from_data_and_root(GetAndroidData().c_str(), GetAndroidRoot().c_str())) { | 
|  | 266 | LOG(ERROR) << "Could not initialize globals; exiting."; | 
|  | 267 | return false; | 
|  | 268 | } | 
|  | 269 |  | 
|  | 270 | // This is different from the normal installd. We only do the base | 
|  | 271 | // directory, the rest will be created on demand when each app is compiled. | 
|  | 272 | if (access(GetOtaDirectoryPrefix().c_str(), R_OK) < 0) { | 
|  | 273 | LOG(ERROR) << "Could not access " << GetOtaDirectoryPrefix(); | 
|  | 274 | return false; | 
| Andreas Gampe | 1842af3 | 2016-03-16 14:28:50 -0700 | [diff] [blame] | 275 | } | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 276 |  | 
|  | 277 | return true; | 
|  | 278 | } | 
|  | 279 |  | 
| Shubham Ajmera | 45c8743 | 2017-06-22 11:10:27 -0700 | [diff] [blame] | 280 | bool ParseBool(const char* in) { | 
|  | 281 | if (strcmp(in, "true") == 0) { | 
|  | 282 | return true; | 
|  | 283 | } | 
|  | 284 | return false; | 
|  | 285 | } | 
|  | 286 |  | 
| Andreas Gampe | c4ced4f | 2017-04-14 20:39:56 -0700 | [diff] [blame] | 287 | bool ParseUInt(const char* in, uint32_t* out) { | 
|  | 288 | char* end; | 
|  | 289 | long long int result = strtoll(in, &end, 0); | 
|  | 290 | if (in == end || *end != '\0') { | 
|  | 291 | return false; | 
|  | 292 | } | 
|  | 293 | if (result < std::numeric_limits<uint32_t>::min() || | 
|  | 294 | std::numeric_limits<uint32_t>::max() < result) { | 
|  | 295 | return false; | 
|  | 296 | } | 
|  | 297 | *out = static_cast<uint32_t>(result); | 
|  | 298 | return true; | 
|  | 299 | } | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 300 |  | 
| Andreas Gampe | c4ced4f | 2017-04-14 20:39:56 -0700 | [diff] [blame] | 301 | bool ReadArguments(int argc, char** argv) { | 
| Calin Juravle | c9e7679 | 2018-02-01 14:44:56 +0000 | [diff] [blame] | 302 | return parameters_.ReadArguments(argc, const_cast<const char**>(argv)); | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 303 | } | 
|  | 304 |  | 
| Orion Hodson | e695e01 | 2020-03-26 09:24:41 +0000 | [diff] [blame] | 305 | void PrepareEnvironmentVariables() { | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 306 | environ_.push_back(StringPrintf("BOOTCLASSPATH=%s", boot_classpath_.c_str())); | 
|  | 307 | environ_.push_back(StringPrintf("ANDROID_DATA=%s", GetOTADataDirectory().c_str())); | 
|  | 308 | environ_.push_back(StringPrintf("ANDROID_ROOT=%s", android_root_.c_str())); | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 309 |  | 
|  | 310 | for (const std::string& e : environ_) { | 
|  | 311 | putenv(const_cast<char*>(e.c_str())); | 
|  | 312 | } | 
|  | 313 | } | 
|  | 314 |  | 
| Orion Hodson | e695e01 | 2020-03-26 09:24:41 +0000 | [diff] [blame] | 315 | // Ensure that we have the right boot image and cache file structures. | 
|  | 316 | bool EnsureBootImageAndDalvikCache() const { | 
| Calin Juravle | c9e7679 | 2018-02-01 14:44:56 +0000 | [diff] [blame] | 317 | if (parameters_.instruction_set == nullptr) { | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 318 | LOG(ERROR) << "Instruction set missing."; | 
|  | 319 | return false; | 
|  | 320 | } | 
| Calin Juravle | c9e7679 | 2018-02-01 14:44:56 +0000 | [diff] [blame] | 321 | const char* isa = parameters_.instruction_set; | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 322 | std::string dalvik_cache = GetOTADataDirectory() + "/" + DALVIK_CACHE; | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 323 | std::string isa_path = dalvik_cache + "/" + isa; | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 324 |  | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 325 | // Reset umask in otapreopt, so that we control the the access for the files we create. | 
|  | 326 | umask(0); | 
|  | 327 |  | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 328 | // Create the directories, if necessary. | 
|  | 329 | if (access(dalvik_cache.c_str(), F_OK) != 0) { | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 330 | if (!CreatePath(dalvik_cache)) { | 
|  | 331 | PLOG(ERROR) << "Could not create dalvik-cache dir " << dalvik_cache; | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 332 | return false; | 
|  | 333 | } | 
|  | 334 | } | 
|  | 335 | if (access(isa_path.c_str(), F_OK) != 0) { | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 336 | if (!CreatePath(isa_path)) { | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 337 | PLOG(ERROR) << "Could not create dalvik-cache isa dir"; | 
|  | 338 | return false; | 
|  | 339 | } | 
|  | 340 | } | 
|  | 341 |  | 
| Orion Hodson | e695e01 | 2020-03-26 09:24:41 +0000 | [diff] [blame] | 342 | // Clear cached artifacts. | 
|  | 343 | ClearDirectory(isa_path); | 
|  | 344 |  | 
|  | 345 | // Check whether we have a boot image. | 
| Andreas Gampe | bd7aef1 | 2018-10-23 13:58:44 -0700 | [diff] [blame] | 346 | // TODO: check that the files are correct wrt/ jars. | 
| Orion Hodson | e695e01 | 2020-03-26 09:24:41 +0000 | [diff] [blame] | 347 | std::string preopted_boot_art_path = | 
|  | 348 | StringPrintf("/apex/com.android.art/javalib/%s/boot.art", isa); | 
|  | 349 | if (access(preopted_boot_art_path.c_str(), F_OK) != 0) { | 
|  | 350 | PLOG(ERROR) << "Bad access() to " << preopted_boot_art_path; | 
|  | 351 | return false; | 
| Andreas Gampe | bd7aef1 | 2018-10-23 13:58:44 -0700 | [diff] [blame] | 352 | } | 
|  | 353 |  | 
| Orion Hodson | e695e01 | 2020-03-26 09:24:41 +0000 | [diff] [blame] | 354 | return true; | 
| Andreas Gampe | 5709b57 | 2016-02-12 17:42:59 -0800 | [diff] [blame] | 355 | } | 
|  | 356 |  | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 357 | static bool CreatePath(const std::string& path) { | 
|  | 358 | // Create the given path. Use string processing instead of dirname, as dirname's need for | 
|  | 359 | // a writable char buffer is painful. | 
|  | 360 |  | 
|  | 361 | // First, try to use the full path. | 
|  | 362 | if (mkdir(path.c_str(), 0711) == 0) { | 
|  | 363 | return true; | 
|  | 364 | } | 
|  | 365 | if (errno != ENOENT) { | 
|  | 366 | PLOG(ERROR) << "Could not create path " << path; | 
|  | 367 | return false; | 
|  | 368 | } | 
|  | 369 |  | 
|  | 370 | // Now find the parent and try that first. | 
|  | 371 | size_t last_slash = path.find_last_of('/'); | 
|  | 372 | if (last_slash == std::string::npos || last_slash == 0) { | 
|  | 373 | PLOG(ERROR) << "Could not create " << path; | 
|  | 374 | return false; | 
|  | 375 | } | 
|  | 376 |  | 
|  | 377 | if (!CreatePath(path.substr(0, last_slash))) { | 
|  | 378 | return false; | 
|  | 379 | } | 
|  | 380 |  | 
|  | 381 | if (mkdir(path.c_str(), 0711) == 0) { | 
|  | 382 | return true; | 
|  | 383 | } | 
|  | 384 | PLOG(ERROR) << "Could not create " << path; | 
|  | 385 | return false; | 
|  | 386 | } | 
|  | 387 |  | 
|  | 388 | static void ClearDirectory(const std::string& dir) { | 
|  | 389 | DIR* c_dir = opendir(dir.c_str()); | 
|  | 390 | if (c_dir == nullptr) { | 
|  | 391 | PLOG(WARNING) << "Unable to open " << dir << " to delete it's contents"; | 
|  | 392 | return; | 
|  | 393 | } | 
|  | 394 |  | 
|  | 395 | for (struct dirent* de = readdir(c_dir); de != nullptr; de = readdir(c_dir)) { | 
|  | 396 | const char* name = de->d_name; | 
|  | 397 | if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) { | 
|  | 398 | continue; | 
|  | 399 | } | 
|  | 400 | // We only want to delete regular files and symbolic links. | 
|  | 401 | std::string file = StringPrintf("%s/%s", dir.c_str(), name); | 
|  | 402 | if (de->d_type != DT_REG && de->d_type != DT_LNK) { | 
|  | 403 | LOG(WARNING) << "Unexpected file " | 
|  | 404 | << file | 
|  | 405 | << " of type " | 
|  | 406 | << std::hex | 
|  | 407 | << de->d_type | 
|  | 408 | << " encountered."; | 
|  | 409 | } else { | 
|  | 410 | // Try to unlink the file. | 
|  | 411 | if (unlink(file.c_str()) != 0) { | 
|  | 412 | PLOG(ERROR) << "Unable to unlink " << file; | 
|  | 413 | } | 
|  | 414 | } | 
|  | 415 | } | 
|  | 416 | CHECK_EQ(0, closedir(c_dir)) << "Unable to close directory."; | 
|  | 417 | } | 
|  | 418 |  | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 419 | static const char* ParseNull(const char* arg) { | 
|  | 420 | return (strcmp(arg, "!") == 0) ? nullptr : arg; | 
|  | 421 | } | 
|  | 422 |  | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 423 | bool ShouldSkipPreopt() const { | 
| Andreas Gampe | 56f79f9 | 2016-06-08 15:11:37 -0700 | [diff] [blame] | 424 | // There's one thing we have to be careful about: we may/will be asked to compile an app | 
|  | 425 | // living in the system image. This may be a valid request - if the app wasn't compiled, | 
|  | 426 | // e.g., if the system image wasn't large enough to include preopted files. However, the | 
|  | 427 | // data we have is from the old system, so the driver (the OTA service) can't actually | 
|  | 428 | // know. Thus, we will get requests for apps that have preopted components. To avoid | 
|  | 429 | // duplication (we'd generate files that are not used and are *not* cleaned up), do two | 
|  | 430 | // simple checks: | 
|  | 431 | // | 
|  | 432 | // 1) Does the apk_path start with the value of ANDROID_ROOT? (~in the system image) | 
|  | 433 | //    (For simplicity, assume the value of ANDROID_ROOT does not contain a symlink.) | 
|  | 434 | // | 
|  | 435 | // 2) If you replace the name in the apk_path with "oat," does the path exist? | 
|  | 436 | //    (=have a subdirectory for preopted files) | 
|  | 437 | // | 
|  | 438 | // If the answer to both is yes, skip the dexopt. | 
|  | 439 | // | 
|  | 440 | // Note: while one may think it's OK to call dexopt and it will fail (because APKs should | 
|  | 441 | //       be stripped), that's not true for APKs signed outside the build system (so the | 
|  | 442 | //       jar content must be exactly the same). | 
|  | 443 |  | 
|  | 444 | //       (This is ugly as it's the only thing where we need to understand the contents | 
| Calin Juravle | c9e7679 | 2018-02-01 14:44:56 +0000 | [diff] [blame] | 445 | //        of parameters_, but it beats postponing the decision or using the call- | 
| Andreas Gampe | 56f79f9 | 2016-06-08 15:11:37 -0700 | [diff] [blame] | 446 | //        backs to do weird things.) | 
| Calin Juravle | c9e7679 | 2018-02-01 14:44:56 +0000 | [diff] [blame] | 447 | const char* apk_path = parameters_.apk_path; | 
| Andreas Gampe | c4ced4f | 2017-04-14 20:39:56 -0700 | [diff] [blame] | 448 | CHECK(apk_path != nullptr); | 
| Elliott Hughes | 969e4f8 | 2017-12-20 12:34:09 -0800 | [diff] [blame] | 449 | if (StartsWith(apk_path, android_root_)) { | 
| Andreas Gampe | c4ced4f | 2017-04-14 20:39:56 -0700 | [diff] [blame] | 450 | const char* last_slash = strrchr(apk_path, '/'); | 
| Andreas Gampe | 56f79f9 | 2016-06-08 15:11:37 -0700 | [diff] [blame] | 451 | if (last_slash != nullptr) { | 
| Andreas Gampe | c4ced4f | 2017-04-14 20:39:56 -0700 | [diff] [blame] | 452 | std::string path(apk_path, last_slash - apk_path + 1); | 
| Andreas Gampe | 56f79f9 | 2016-06-08 15:11:37 -0700 | [diff] [blame] | 453 | CHECK(EndsWith(path, "/")); | 
|  | 454 | path = path + "oat"; | 
|  | 455 | if (access(path.c_str(), F_OK) == 0) { | 
| Calin Juravle | b3591f6 | 2017-11-17 16:38:17 -0800 | [diff] [blame] | 456 | LOG(INFO) << "Skipping A/B OTA preopt of already preopted package " << apk_path; | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 457 | return true; | 
| Andreas Gampe | 56f79f9 | 2016-06-08 15:11:37 -0700 | [diff] [blame] | 458 | } | 
|  | 459 | } | 
|  | 460 | } | 
|  | 461 |  | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 462 | // Another issue is unavailability of files in the new system. If the partition | 
|  | 463 | // layout changes, otapreopt_chroot may not know about this. Then files from that | 
|  | 464 | // partition will not be available and fail to build. This is problematic, as | 
|  | 465 | // this tool will wipe the OTA artifact cache and try again (for robustness after | 
|  | 466 | // a failed OTA with remaining cache artifacts). | 
| Andreas Gampe | c4ced4f | 2017-04-14 20:39:56 -0700 | [diff] [blame] | 467 | if (access(apk_path, F_OK) != 0) { | 
| Calin Juravle | b3591f6 | 2017-11-17 16:38:17 -0800 | [diff] [blame] | 468 | LOG(WARNING) << "Skipping A/B OTA preopt of non-existing package " << apk_path; | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 469 | return true; | 
|  | 470 | } | 
|  | 471 |  | 
|  | 472 | return false; | 
|  | 473 | } | 
|  | 474 |  | 
| Calin Juravle | c9e7679 | 2018-02-01 14:44:56 +0000 | [diff] [blame] | 475 | // Run dexopt with the parameters of parameters_. | 
| Calin Juravle | cfcd6aa | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 476 | // TODO(calin): embed the profile name in the parameters. | 
| Andreas Gampe | b39d2f0 | 2017-04-17 20:04:02 -0700 | [diff] [blame] | 477 | int Dexopt() { | 
| Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame] | 478 | std::string dummy; | 
| Calin Juravle | c9e7679 | 2018-02-01 14:44:56 +0000 | [diff] [blame] | 479 | return dexopt(parameters_.apk_path, | 
|  | 480 | parameters_.uid, | 
|  | 481 | parameters_.pkgName, | 
|  | 482 | parameters_.instruction_set, | 
|  | 483 | parameters_.dexopt_needed, | 
|  | 484 | parameters_.oat_dir, | 
|  | 485 | parameters_.dexopt_flags, | 
|  | 486 | parameters_.compiler_filter, | 
|  | 487 | parameters_.volume_uuid, | 
|  | 488 | parameters_.shared_libraries, | 
|  | 489 | parameters_.se_info, | 
|  | 490 | parameters_.downgrade, | 
|  | 491 | parameters_.target_sdk_version, | 
| Calin Juravle | cc3b8ae | 2018-02-01 17:03:23 +0000 | [diff] [blame] | 492 | parameters_.profile_name, | 
| Calin Juravle | dcccd83 | 2018-02-13 18:31:32 -0800 | [diff] [blame] | 493 | parameters_.dex_metadata_path, | 
| Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame] | 494 | parameters_.compilation_reason, | 
|  | 495 | &dummy); | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 496 | } | 
|  | 497 |  | 
| Andreas Gampe | b39d2f0 | 2017-04-17 20:04:02 -0700 | [diff] [blame] | 498 | int RunPreopt() { | 
|  | 499 | if (ShouldSkipPreopt()) { | 
|  | 500 | return 0; | 
|  | 501 | } | 
|  | 502 |  | 
|  | 503 | int dexopt_result = Dexopt(); | 
|  | 504 | if (dexopt_result == 0) { | 
|  | 505 | return 0; | 
|  | 506 | } | 
|  | 507 |  | 
| Andreas Gampe | b39d2f0 | 2017-04-17 20:04:02 -0700 | [diff] [blame] | 508 | // If this was a profile-guided run, we may have profile version issues. Try to downgrade, | 
|  | 509 | // if possible. | 
| Calin Juravle | c9e7679 | 2018-02-01 14:44:56 +0000 | [diff] [blame] | 510 | if ((parameters_.dexopt_flags & DEXOPT_PROFILE_GUIDED) == 0) { | 
| Andreas Gampe | b39d2f0 | 2017-04-17 20:04:02 -0700 | [diff] [blame] | 511 | return dexopt_result; | 
|  | 512 | } | 
|  | 513 |  | 
|  | 514 | LOG(WARNING) << "Downgrading compiler filter in an attempt to progress compilation"; | 
| Calin Juravle | c9e7679 | 2018-02-01 14:44:56 +0000 | [diff] [blame] | 515 | parameters_.dexopt_flags &= ~DEXOPT_PROFILE_GUIDED; | 
| Andreas Gampe | b39d2f0 | 2017-04-17 20:04:02 -0700 | [diff] [blame] | 516 | return Dexopt(); | 
|  | 517 | } | 
|  | 518 |  | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 519 | //////////////////////////////////// | 
|  | 520 | // Helpers, mostly taken from ART // | 
|  | 521 | //////////////////////////////////// | 
|  | 522 |  | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 523 | // Choose a random relocation offset. Taken from art/runtime/gc/image_space.cc. | 
|  | 524 | static int32_t ChooseRelocationOffsetDelta(int32_t min_delta, int32_t max_delta) { | 
|  | 525 | constexpr size_t kPageSize = PAGE_SIZE; | 
|  | 526 | CHECK_EQ(min_delta % kPageSize, 0u); | 
|  | 527 | CHECK_EQ(max_delta % kPageSize, 0u); | 
|  | 528 | CHECK_LT(min_delta, max_delta); | 
|  | 529 |  | 
|  | 530 | std::default_random_engine generator; | 
|  | 531 | generator.seed(GetSeed()); | 
|  | 532 | std::uniform_int_distribution<int32_t> distribution(min_delta, max_delta); | 
|  | 533 | int32_t r = distribution(generator); | 
|  | 534 | if (r % 2 == 0) { | 
|  | 535 | r = RoundUp(r, kPageSize); | 
|  | 536 | } else { | 
|  | 537 | r = RoundDown(r, kPageSize); | 
|  | 538 | } | 
|  | 539 | CHECK_LE(min_delta, r); | 
|  | 540 | CHECK_GE(max_delta, r); | 
|  | 541 | CHECK_EQ(r % kPageSize, 0u); | 
|  | 542 | return r; | 
|  | 543 | } | 
|  | 544 |  | 
|  | 545 | static uint64_t GetSeed() { | 
|  | 546 | #ifdef __BIONIC__ | 
|  | 547 | // Bionic exposes arc4random, use it. | 
|  | 548 | uint64_t random_data; | 
|  | 549 | arc4random_buf(&random_data, sizeof(random_data)); | 
|  | 550 | return random_data; | 
|  | 551 | #else | 
|  | 552 | #error "This is only supposed to run with bionic. Otherwise, implement..." | 
|  | 553 | #endif | 
|  | 554 | } | 
|  | 555 |  | 
|  | 556 | void AddCompilerOptionFromSystemProperty(const char* system_property, | 
|  | 557 | const char* prefix, | 
|  | 558 | bool runtime, | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 559 | std::vector<std::string>& out) const { | 
|  | 560 | const std::string* value = system_properties_.GetProperty(system_property); | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 561 | if (value != nullptr) { | 
|  | 562 | if (runtime) { | 
|  | 563 | out.push_back("--runtime-arg"); | 
|  | 564 | } | 
|  | 565 | if (prefix != nullptr) { | 
|  | 566 | out.push_back(StringPrintf("%s%s", prefix, value->c_str())); | 
|  | 567 | } else { | 
|  | 568 | out.push_back(*value); | 
|  | 569 | } | 
|  | 570 | } | 
|  | 571 | } | 
|  | 572 |  | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 573 | static constexpr const char* kBootClassPathPropertyName = "BOOTCLASSPATH"; | 
|  | 574 | static constexpr const char* kAndroidRootPathPropertyName = "ANDROID_ROOT"; | 
|  | 575 | static constexpr const char* kAndroidDataPathPropertyName = "ANDROID_DATA"; | 
|  | 576 | // The index of the instruction-set string inside the package parameters. Needed for | 
|  | 577 | // some special-casing that requires knowledge of the instruction-set. | 
|  | 578 | static constexpr size_t kISAIndex = 3; | 
|  | 579 |  | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 580 | // Stores the system properties read out of the B partition. We need to use these properties | 
|  | 581 | // to compile, instead of the A properties we could get from init/get_property. | 
|  | 582 | SystemProperties system_properties_; | 
|  | 583 |  | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 584 | // Some select properties that are always needed. | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 585 | std::string android_root_; | 
|  | 586 | std::string android_data_; | 
|  | 587 | std::string boot_classpath_; | 
|  | 588 | std::string asec_mountpoint_; | 
|  | 589 |  | 
| Calin Juravle | c9e7679 | 2018-02-01 14:44:56 +0000 | [diff] [blame] | 590 | OTAPreoptParameters parameters_; | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 591 |  | 
|  | 592 | // Store environment values we need to set. | 
|  | 593 | std::vector<std::string> environ_; | 
|  | 594 | }; | 
|  | 595 |  | 
|  | 596 | OTAPreoptService gOps; | 
|  | 597 |  | 
|  | 598 | //////////////////////// | 
|  | 599 | // Plug-in functions. // | 
|  | 600 | //////////////////////// | 
|  | 601 |  | 
|  | 602 | int get_property(const char *key, char *value, const char *default_value) { | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 603 | return gOps.GetProperty(key, value, default_value); | 
|  | 604 | } | 
|  | 605 |  | 
|  | 606 | // Compute the output path of | 
|  | 607 | bool calculate_oat_file_path(char path[PKG_PATH_MAX], const char *oat_dir, | 
|  | 608 | const char *apk_path, | 
|  | 609 | const char *instruction_set) { | 
| Dan Austin | 9c8f93a | 2016-06-03 16:15:54 -0700 | [diff] [blame] | 610 | const char *file_name_start; | 
|  | 611 | const char *file_name_end; | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 612 |  | 
|  | 613 | file_name_start = strrchr(apk_path, '/'); | 
|  | 614 | if (file_name_start == nullptr) { | 
|  | 615 | ALOGE("apk_path '%s' has no '/'s in it\n", apk_path); | 
|  | 616 | return false; | 
|  | 617 | } | 
|  | 618 | file_name_end = strrchr(file_name_start, '.'); | 
|  | 619 | if (file_name_end == nullptr) { | 
|  | 620 | ALOGE("apk_path '%s' has no extension\n", apk_path); | 
|  | 621 | return false; | 
|  | 622 | } | 
|  | 623 |  | 
|  | 624 | // Calculate file_name | 
|  | 625 | file_name_start++;  // Move past '/', is valid as file_name_end is valid. | 
|  | 626 | size_t file_name_len = file_name_end - file_name_start; | 
|  | 627 | std::string file_name(file_name_start, file_name_len); | 
|  | 628 |  | 
|  | 629 | // <apk_parent_dir>/oat/<isa>/<file_name>.odex.b | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 630 | snprintf(path, | 
|  | 631 | PKG_PATH_MAX, | 
|  | 632 | "%s/%s/%s.odex.%s", | 
|  | 633 | oat_dir, | 
|  | 634 | instruction_set, | 
|  | 635 | file_name.c_str(), | 
|  | 636 | gOps.GetTargetSlot().c_str()); | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 637 | return true; | 
|  | 638 | } | 
|  | 639 |  | 
|  | 640 | /* | 
|  | 641 | * Computes the odex file for the given apk_path and instruction_set. | 
|  | 642 | * /system/framework/whatever.jar -> /system/framework/oat/<isa>/whatever.odex | 
|  | 643 | * | 
|  | 644 | * Returns false if it failed to determine the odex file path. | 
|  | 645 | */ | 
|  | 646 | bool calculate_odex_file_path(char path[PKG_PATH_MAX], const char *apk_path, | 
|  | 647 | const char *instruction_set) { | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 648 | const char *path_end = strrchr(apk_path, '/'); | 
|  | 649 | if (path_end == nullptr) { | 
|  | 650 | ALOGE("apk_path '%s' has no '/'s in it?!\n", apk_path); | 
|  | 651 | return false; | 
|  | 652 | } | 
|  | 653 | std::string path_component(apk_path, path_end - apk_path); | 
|  | 654 |  | 
|  | 655 | const char *name_begin = path_end + 1; | 
|  | 656 | const char *extension_start = strrchr(name_begin, '.'); | 
|  | 657 | if (extension_start == nullptr) { | 
|  | 658 | ALOGE("apk_path '%s' has no extension.\n", apk_path); | 
|  | 659 | return false; | 
|  | 660 | } | 
|  | 661 | std::string name_component(name_begin, extension_start - name_begin); | 
|  | 662 |  | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 663 | std::string new_path = StringPrintf("%s/oat/%s/%s.odex.%s", | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 664 | path_component.c_str(), | 
|  | 665 | instruction_set, | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 666 | name_component.c_str(), | 
|  | 667 | gOps.GetTargetSlot().c_str()); | 
|  | 668 | if (new_path.length() >= PKG_PATH_MAX) { | 
|  | 669 | LOG(ERROR) << "apk_path of " << apk_path << " is too long: " << new_path; | 
|  | 670 | return false; | 
|  | 671 | } | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 672 | strcpy(path, new_path.c_str()); | 
|  | 673 | return true; | 
|  | 674 | } | 
|  | 675 |  | 
|  | 676 | bool create_cache_path(char path[PKG_PATH_MAX], | 
|  | 677 | const char *src, | 
|  | 678 | const char *instruction_set) { | 
|  | 679 | size_t srclen = strlen(src); | 
|  | 680 |  | 
|  | 681 | /* demand that we are an absolute path */ | 
|  | 682 | if ((src == 0) || (src[0] != '/') || strstr(src,"..")) { | 
|  | 683 | return false; | 
|  | 684 | } | 
|  | 685 |  | 
|  | 686 | if (srclen > PKG_PATH_MAX) {        // XXX: PKG_NAME_MAX? | 
|  | 687 | return false; | 
|  | 688 | } | 
|  | 689 |  | 
|  | 690 | std::string from_src = std::string(src + 1); | 
|  | 691 | std::replace(from_src.begin(), from_src.end(), '/', '@'); | 
|  | 692 |  | 
|  | 693 | std::string assembled_path = StringPrintf("%s/%s/%s/%s%s", | 
| Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 694 | gOps.GetOTADataDirectory().c_str(), | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 695 | DALVIK_CACHE, | 
|  | 696 | instruction_set, | 
|  | 697 | from_src.c_str(), | 
| David Brazdil | 249c179 | 2016-09-06 15:35:28 +0100 | [diff] [blame] | 698 | DALVIK_CACHE_POSTFIX); | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 699 |  | 
|  | 700 | if (assembled_path.length() + 1 > PKG_PATH_MAX) { | 
|  | 701 | return false; | 
|  | 702 | } | 
|  | 703 | strcpy(path, assembled_path.c_str()); | 
|  | 704 |  | 
|  | 705 | return true; | 
|  | 706 | } | 
|  | 707 |  | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 708 | static int log_callback(int type, const char *fmt, ...) { | 
|  | 709 | va_list ap; | 
|  | 710 | int priority; | 
|  | 711 |  | 
|  | 712 | switch (type) { | 
|  | 713 | case SELINUX_WARNING: | 
|  | 714 | priority = ANDROID_LOG_WARN; | 
|  | 715 | break; | 
|  | 716 | case SELINUX_INFO: | 
|  | 717 | priority = ANDROID_LOG_INFO; | 
|  | 718 | break; | 
|  | 719 | default: | 
|  | 720 | priority = ANDROID_LOG_ERROR; | 
|  | 721 | break; | 
|  | 722 | } | 
|  | 723 | va_start(ap, fmt); | 
|  | 724 | LOG_PRI_VA(priority, "SELinux", fmt, ap); | 
|  | 725 | va_end(ap); | 
|  | 726 | return 0; | 
|  | 727 | } | 
|  | 728 |  | 
|  | 729 | static int otapreopt_main(const int argc, char *argv[]) { | 
|  | 730 | int selinux_enabled = (is_selinux_enabled() > 0); | 
|  | 731 |  | 
|  | 732 | setenv("ANDROID_LOG_TAGS", "*:v", 1); | 
|  | 733 | android::base::InitLogging(argv); | 
|  | 734 |  | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 735 | if (argc < 2) { | 
|  | 736 | ALOGE("Expecting parameters"); | 
|  | 737 | exit(1); | 
|  | 738 | } | 
|  | 739 |  | 
|  | 740 | union selinux_callback cb; | 
|  | 741 | cb.func_log = log_callback; | 
|  | 742 | selinux_set_callback(SELINUX_CB_LOG, cb); | 
|  | 743 |  | 
| Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 744 | if (selinux_enabled && selinux_status_open(true) < 0) { | 
|  | 745 | ALOGE("Could not open selinux status; exiting.\n"); | 
|  | 746 | exit(1); | 
|  | 747 | } | 
|  | 748 |  | 
|  | 749 | int ret = android::installd::gOps.Main(argc, argv); | 
|  | 750 |  | 
|  | 751 | return ret; | 
|  | 752 | } | 
|  | 753 |  | 
|  | 754 | }  // namespace installd | 
|  | 755 | }  // namespace android | 
|  | 756 |  | 
|  | 757 | int main(const int argc, char *argv[]) { | 
|  | 758 | return android::installd::otapreopt_main(argc, argv); | 
|  | 759 | } |