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