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> |
| 19 | #include <random> |
Andreas Gampe | 1842af3 | 2016-03-16 14:28:50 -0700 | [diff] [blame] | 20 | #include <regex> |
Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 21 | #include <selinux/android.h> |
| 22 | #include <selinux/avc.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <sys/capability.h> |
| 26 | #include <sys/prctl.h> |
| 27 | #include <sys/stat.h> |
| 28 | #include <sys/wait.h> |
| 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 | f3e30b9 | 2016-12-09 17:06:57 -0700 | [diff] [blame] | 39 | #include "InstalldNativeService.h" |
Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 40 | #include "dexopt.h" |
Jeff Sharkey | f3e30b9 | 2016-12-09 17:06:57 -0700 | [diff] [blame] | 41 | #include "file_parsing.h" |
| 42 | #include "globals.h" |
| 43 | #include "installd_deps.h" // Need to fill in requirements of commands. |
| 44 | #include "otapreopt_utils.h" |
| 45 | #include "system_properties.h" |
| 46 | #include "utils.h" |
Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 47 | |
Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 48 | #ifndef LOG_TAG |
| 49 | #define LOG_TAG "otapreopt" |
| 50 | #endif |
| 51 | |
| 52 | #define BUFFER_MAX 1024 /* input buffer for commands */ |
| 53 | #define TOKEN_MAX 16 /* max number of arguments in buffer */ |
| 54 | #define REPLY_MAX 256 /* largest reply allowed */ |
| 55 | |
Andreas Gampe | 56f79f9 | 2016-06-08 15:11:37 -0700 | [diff] [blame] | 56 | using android::base::EndsWith; |
Andreas Gampe | 6db8db9 | 2016-06-03 10:22:19 -0700 | [diff] [blame] | 57 | using android::base::Join; |
| 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 | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 65 | template<typename T> |
| 66 | static constexpr T RoundDown(T x, typename std::decay<T>::type n) { |
| 67 | return DCHECK_CONSTEXPR(IsPowerOfTwo(n), , T(0))(x & -n); |
| 68 | } |
| 69 | |
| 70 | template<typename T> |
| 71 | static constexpr T RoundUp(T x, typename std::remove_reference<T>::type n) { |
| 72 | return RoundDown(x + n - 1, n); |
| 73 | } |
| 74 | |
| 75 | class OTAPreoptService { |
| 76 | public: |
Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 77 | // Main driver. Performs the following steps. |
| 78 | // |
| 79 | // 1) Parse options (read system properties etc from B partition). |
| 80 | // |
| 81 | // 2) Read in package data. |
| 82 | // |
| 83 | // 3) Prepare environment variables. |
| 84 | // |
| 85 | // 4) Prepare(compile) boot image, if necessary. |
| 86 | // |
| 87 | // 5) Run update. |
| 88 | int Main(int argc, char** argv) { |
Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 89 | if (!ReadArguments(argc, argv)) { |
| 90 | LOG(ERROR) << "Failed reading command line."; |
| 91 | return 1; |
| 92 | } |
| 93 | |
Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 94 | if (!ReadSystemProperties()) { |
| 95 | LOG(ERROR)<< "Failed reading system properties."; |
Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 96 | return 2; |
Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | if (!ReadEnvironment()) { |
| 100 | LOG(ERROR) << "Failed reading environment properties."; |
Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 101 | return 3; |
Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 104 | if (!CheckAndInitializeInstalldGlobals()) { |
| 105 | LOG(ERROR) << "Failed initializing globals."; |
| 106 | return 4; |
Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | PrepareEnvironment(); |
| 110 | |
Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 111 | if (!PrepareBootImage(/* force */ false)) { |
Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 112 | LOG(ERROR) << "Failed preparing boot image."; |
Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 113 | return 5; |
Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | int dexopt_retcode = RunPreopt(); |
| 117 | |
| 118 | return dexopt_retcode; |
| 119 | } |
| 120 | |
Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 121 | int GetProperty(const char* key, char* value, const char* default_value) const { |
Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 122 | const std::string* prop_value = system_properties_.GetProperty(key); |
| 123 | if (prop_value == nullptr) { |
| 124 | if (default_value == nullptr) { |
| 125 | return 0; |
| 126 | } |
| 127 | // Copy in the default value. |
| 128 | strncpy(value, default_value, kPropertyValueMax - 1); |
| 129 | value[kPropertyValueMax - 1] = 0; |
| 130 | return strlen(default_value);// TODO: Need to truncate? |
| 131 | } |
| 132 | size_t size = std::min(kPropertyValueMax - 1, prop_value->length()); |
| 133 | strncpy(value, prop_value->data(), size); |
| 134 | value[size] = 0; |
| 135 | return static_cast<int>(size); |
| 136 | } |
| 137 | |
Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 138 | std::string GetOTADataDirectory() const { |
| 139 | return StringPrintf("%s/%s", GetOtaDirectoryPrefix().c_str(), target_slot_.c_str()); |
| 140 | } |
| 141 | |
| 142 | const std::string& GetTargetSlot() const { |
| 143 | return target_slot_; |
| 144 | } |
| 145 | |
Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 146 | private: |
Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 147 | |
Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 148 | bool ReadSystemProperties() { |
Andreas Gampe | 1842af3 | 2016-03-16 14:28:50 -0700 | [diff] [blame] | 149 | static constexpr const char* kPropertyFiles[] = { |
| 150 | "/default.prop", "/system/build.prop" |
| 151 | }; |
Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 152 | |
Andreas Gampe | 1842af3 | 2016-03-16 14:28:50 -0700 | [diff] [blame] | 153 | for (size_t i = 0; i < arraysize(kPropertyFiles); ++i) { |
| 154 | if (!system_properties_.Load(kPropertyFiles[i])) { |
| 155 | return false; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | return true; |
Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | bool ReadEnvironment() { |
Andreas Gampe | 1842af3 | 2016-03-16 14:28:50 -0700 | [diff] [blame] | 163 | // Parse the environment variables from init.environ.rc, which have the form |
| 164 | // export NAME VALUE |
| 165 | // For simplicity, don't respect string quotation. The values we are interested in can be |
| 166 | // encoded without them. |
| 167 | std::regex export_regex("\\s*export\\s+(\\S+)\\s+(\\S+)"); |
| 168 | bool parse_result = ParseFile("/init.environ.rc", [&](const std::string& line) { |
| 169 | std::smatch export_match; |
| 170 | if (!std::regex_match(line, export_match, export_regex)) { |
| 171 | return true; |
| 172 | } |
Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 173 | |
Andreas Gampe | 1842af3 | 2016-03-16 14:28:50 -0700 | [diff] [blame] | 174 | if (export_match.size() != 3) { |
| 175 | return true; |
| 176 | } |
| 177 | |
| 178 | std::string name = export_match[1].str(); |
| 179 | std::string value = export_match[2].str(); |
| 180 | |
| 181 | system_properties_.SetProperty(name, value); |
| 182 | |
| 183 | return true; |
| 184 | }); |
| 185 | if (!parse_result) { |
Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 186 | return false; |
| 187 | } |
Andreas Gampe | 1842af3 | 2016-03-16 14:28:50 -0700 | [diff] [blame] | 188 | |
Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 189 | if (system_properties_.GetProperty(kAndroidDataPathPropertyName) == nullptr) { |
| 190 | return false; |
| 191 | } |
| 192 | android_data_ = *system_properties_.GetProperty(kAndroidDataPathPropertyName); |
| 193 | |
| 194 | if (system_properties_.GetProperty(kAndroidRootPathPropertyName) == nullptr) { |
| 195 | return false; |
| 196 | } |
| 197 | android_root_ = *system_properties_.GetProperty(kAndroidRootPathPropertyName); |
| 198 | |
| 199 | if (system_properties_.GetProperty(kBootClassPathPropertyName) == nullptr) { |
| 200 | return false; |
| 201 | } |
| 202 | boot_classpath_ = *system_properties_.GetProperty(kBootClassPathPropertyName); |
| 203 | |
| 204 | if (system_properties_.GetProperty(ASEC_MOUNTPOINT_ENV_NAME) == nullptr) { |
| 205 | return false; |
| 206 | } |
| 207 | asec_mountpoint_ = *system_properties_.GetProperty(ASEC_MOUNTPOINT_ENV_NAME); |
| 208 | |
| 209 | return true; |
| 210 | } |
| 211 | |
| 212 | const std::string& GetAndroidData() const { |
| 213 | return android_data_; |
| 214 | } |
| 215 | |
| 216 | const std::string& GetAndroidRoot() const { |
| 217 | return android_root_; |
| 218 | } |
| 219 | |
| 220 | const std::string GetOtaDirectoryPrefix() const { |
| 221 | return GetAndroidData() + "/ota"; |
| 222 | } |
| 223 | |
| 224 | bool CheckAndInitializeInstalldGlobals() { |
| 225 | // init_globals_from_data_and_root requires "ASEC_MOUNTPOINT" in the environment. We |
| 226 | // do not use any datapath that includes this, but we'll still have to set it. |
| 227 | CHECK(system_properties_.GetProperty(ASEC_MOUNTPOINT_ENV_NAME) != nullptr); |
| 228 | int result = setenv(ASEC_MOUNTPOINT_ENV_NAME, asec_mountpoint_.c_str(), 0); |
| 229 | if (result != 0) { |
| 230 | LOG(ERROR) << "Could not set ASEC_MOUNTPOINT environment variable"; |
| 231 | return false; |
| 232 | } |
| 233 | |
| 234 | if (!init_globals_from_data_and_root(GetAndroidData().c_str(), GetAndroidRoot().c_str())) { |
| 235 | LOG(ERROR) << "Could not initialize globals; exiting."; |
| 236 | return false; |
| 237 | } |
| 238 | |
| 239 | // This is different from the normal installd. We only do the base |
| 240 | // directory, the rest will be created on demand when each app is compiled. |
| 241 | if (access(GetOtaDirectoryPrefix().c_str(), R_OK) < 0) { |
| 242 | LOG(ERROR) << "Could not access " << GetOtaDirectoryPrefix(); |
| 243 | return false; |
Andreas Gampe | 1842af3 | 2016-03-16 14:28:50 -0700 | [diff] [blame] | 244 | } |
Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 245 | |
| 246 | return true; |
| 247 | } |
| 248 | |
Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 249 | bool ReadArguments(int argc ATTRIBUTE_UNUSED, char** argv) { |
| 250 | // Expected command line: |
| 251 | // target-slot dexopt {DEXOPT_PARAMETERS} |
| 252 | // The DEXOPT_PARAMETERS are passed on to dexopt(), so we expect DEXOPT_PARAM_COUNT |
| 253 | // of them. We store them in package_parameters_ (size checks are done when |
| 254 | // parsing the special parameters and when copying into package_parameters_. |
| 255 | |
Andreas Gampe | 548bdb9 | 2016-06-02 17:56:45 -0700 | [diff] [blame] | 256 | static_assert(DEXOPT_PARAM_COUNT == ARRAY_SIZE(package_parameters_), |
| 257 | "Unexpected dexopt param count"); |
Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 258 | |
| 259 | const char* target_slot_arg = argv[1]; |
| 260 | if (target_slot_arg == nullptr) { |
| 261 | LOG(ERROR) << "Missing parameters"; |
| 262 | return false; |
| 263 | } |
| 264 | // Sanitize value. Only allow (a-zA-Z0-9_)+. |
| 265 | target_slot_ = target_slot_arg; |
Andreas Gampe | fd12eda | 2016-07-12 09:47:17 -0700 | [diff] [blame] | 266 | if (!ValidateTargetSlotSuffix(target_slot_)) { |
| 267 | LOG(ERROR) << "Target slot suffix not legal: " << target_slot_; |
| 268 | return false; |
Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | // Check for "dexopt" next. |
| 272 | if (argv[2] == nullptr) { |
| 273 | LOG(ERROR) << "Missing parameters"; |
| 274 | return false; |
| 275 | } |
| 276 | if (std::string("dexopt").compare(argv[2]) != 0) { |
| 277 | LOG(ERROR) << "Second parameter not dexopt: " << argv[2]; |
| 278 | return false; |
| 279 | } |
| 280 | |
| 281 | // Copy the rest into package_parameters_, but be careful about over- and underflow. |
| 282 | size_t index = 0; |
Andreas Gampe | 548bdb9 | 2016-06-02 17:56:45 -0700 | [diff] [blame] | 283 | while (index < DEXOPT_PARAM_COUNT && |
Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 284 | argv[index + 3] != nullptr) { |
| 285 | package_parameters_[index] = argv[index + 3]; |
Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 286 | index++; |
| 287 | } |
Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 288 | if (index != ARRAY_SIZE(package_parameters_) || argv[index + 3] != nullptr) { |
Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 289 | LOG(ERROR) << "Wrong number of parameters"; |
| 290 | return false; |
| 291 | } |
| 292 | |
| 293 | return true; |
| 294 | } |
| 295 | |
| 296 | void PrepareEnvironment() { |
Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 297 | environ_.push_back(StringPrintf("BOOTCLASSPATH=%s", boot_classpath_.c_str())); |
| 298 | environ_.push_back(StringPrintf("ANDROID_DATA=%s", GetOTADataDirectory().c_str())); |
| 299 | environ_.push_back(StringPrintf("ANDROID_ROOT=%s", android_root_.c_str())); |
Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 300 | |
| 301 | for (const std::string& e : environ_) { |
| 302 | putenv(const_cast<char*>(e.c_str())); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | // Ensure that we have the right boot image. The first time any app is |
| 307 | // compiled, we'll try to generate it. |
Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 308 | bool PrepareBootImage(bool force) const { |
Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 309 | if (package_parameters_[kISAIndex] == nullptr) { |
| 310 | LOG(ERROR) << "Instruction set missing."; |
| 311 | return false; |
| 312 | } |
| 313 | const char* isa = package_parameters_[kISAIndex]; |
| 314 | |
| 315 | // Check whether the file exists where expected. |
Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 316 | std::string dalvik_cache = GetOTADataDirectory() + "/" + DALVIK_CACHE; |
Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 317 | std::string isa_path = dalvik_cache + "/" + isa; |
| 318 | std::string art_path = isa_path + "/system@framework@boot.art"; |
| 319 | std::string oat_path = isa_path + "/system@framework@boot.oat"; |
Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 320 | bool cleared = false; |
| 321 | if (access(art_path.c_str(), F_OK) == 0 && access(oat_path.c_str(), F_OK) == 0) { |
| 322 | // Files exist, assume everything is alright if not forced. Otherwise clean up. |
| 323 | if (!force) { |
| 324 | return true; |
| 325 | } |
| 326 | ClearDirectory(isa_path); |
| 327 | cleared = true; |
Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 328 | } |
| 329 | |
Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 330 | // Reset umask in otapreopt, so that we control the the access for the files we create. |
| 331 | umask(0); |
| 332 | |
Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 333 | // Create the directories, if necessary. |
| 334 | if (access(dalvik_cache.c_str(), F_OK) != 0) { |
Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 335 | if (!CreatePath(dalvik_cache)) { |
| 336 | PLOG(ERROR) << "Could not create dalvik-cache dir " << dalvik_cache; |
Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 337 | return false; |
| 338 | } |
| 339 | } |
| 340 | if (access(isa_path.c_str(), F_OK) != 0) { |
Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 341 | if (!CreatePath(isa_path)) { |
Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 342 | PLOG(ERROR) << "Could not create dalvik-cache isa dir"; |
| 343 | return false; |
| 344 | } |
| 345 | } |
| 346 | |
Andreas Gampe | 5709b57 | 2016-02-12 17:42:59 -0800 | [diff] [blame] | 347 | // Prepare to create. |
Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 348 | if (!cleared) { |
| 349 | ClearDirectory(isa_path); |
| 350 | } |
Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 351 | |
Andreas Gampe | 9fb85b0 | 2016-03-16 10:09:29 -0700 | [diff] [blame] | 352 | std::string preopted_boot_art_path = StringPrintf("/system/framework/%s/boot.art", isa); |
Andreas Gampe | 5709b57 | 2016-02-12 17:42:59 -0800 | [diff] [blame] | 353 | if (access(preopted_boot_art_path.c_str(), F_OK) == 0) { |
| 354 | return PatchoatBootImage(art_path, isa); |
| 355 | } else { |
| 356 | // No preopted boot image. Try to compile. |
Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 357 | return Dex2oatBootImage(boot_classpath_, art_path, oat_path, isa); |
Andreas Gampe | 5709b57 | 2016-02-12 17:42:59 -0800 | [diff] [blame] | 358 | } |
| 359 | } |
| 360 | |
Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 361 | static bool CreatePath(const std::string& path) { |
| 362 | // Create the given path. Use string processing instead of dirname, as dirname's need for |
| 363 | // a writable char buffer is painful. |
| 364 | |
| 365 | // First, try to use the full path. |
| 366 | if (mkdir(path.c_str(), 0711) == 0) { |
| 367 | return true; |
| 368 | } |
| 369 | if (errno != ENOENT) { |
| 370 | PLOG(ERROR) << "Could not create path " << path; |
| 371 | return false; |
| 372 | } |
| 373 | |
| 374 | // Now find the parent and try that first. |
| 375 | size_t last_slash = path.find_last_of('/'); |
| 376 | if (last_slash == std::string::npos || last_slash == 0) { |
| 377 | PLOG(ERROR) << "Could not create " << path; |
| 378 | return false; |
| 379 | } |
| 380 | |
| 381 | if (!CreatePath(path.substr(0, last_slash))) { |
| 382 | return false; |
| 383 | } |
| 384 | |
| 385 | if (mkdir(path.c_str(), 0711) == 0) { |
| 386 | return true; |
| 387 | } |
| 388 | PLOG(ERROR) << "Could not create " << path; |
| 389 | return false; |
| 390 | } |
| 391 | |
| 392 | static void ClearDirectory(const std::string& dir) { |
| 393 | DIR* c_dir = opendir(dir.c_str()); |
| 394 | if (c_dir == nullptr) { |
| 395 | PLOG(WARNING) << "Unable to open " << dir << " to delete it's contents"; |
| 396 | return; |
| 397 | } |
| 398 | |
| 399 | for (struct dirent* de = readdir(c_dir); de != nullptr; de = readdir(c_dir)) { |
| 400 | const char* name = de->d_name; |
| 401 | if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) { |
| 402 | continue; |
| 403 | } |
| 404 | // We only want to delete regular files and symbolic links. |
| 405 | std::string file = StringPrintf("%s/%s", dir.c_str(), name); |
| 406 | if (de->d_type != DT_REG && de->d_type != DT_LNK) { |
| 407 | LOG(WARNING) << "Unexpected file " |
| 408 | << file |
| 409 | << " of type " |
| 410 | << std::hex |
| 411 | << de->d_type |
| 412 | << " encountered."; |
| 413 | } else { |
| 414 | // Try to unlink the file. |
| 415 | if (unlink(file.c_str()) != 0) { |
| 416 | PLOG(ERROR) << "Unable to unlink " << file; |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | CHECK_EQ(0, closedir(c_dir)) << "Unable to close directory."; |
| 421 | } |
| 422 | |
| 423 | bool PatchoatBootImage(const std::string& art_path, const char* isa) const { |
Andreas Gampe | 5709b57 | 2016-02-12 17:42:59 -0800 | [diff] [blame] | 424 | // This needs to be kept in sync with ART, see art/runtime/gc/space/image_space.cc. |
| 425 | |
| 426 | std::vector<std::string> cmd; |
Andreas Gampe | 9fb85b0 | 2016-03-16 10:09:29 -0700 | [diff] [blame] | 427 | cmd.push_back("/system/bin/patchoat"); |
Andreas Gampe | 5709b57 | 2016-02-12 17:42:59 -0800 | [diff] [blame] | 428 | |
| 429 | cmd.push_back("--input-image-location=/system/framework/boot.art"); |
| 430 | cmd.push_back(StringPrintf("--output-image-file=%s", art_path.c_str())); |
| 431 | |
| 432 | cmd.push_back(StringPrintf("--instruction-set=%s", isa)); |
| 433 | |
| 434 | int32_t base_offset = ChooseRelocationOffsetDelta(ART_BASE_ADDRESS_MIN_DELTA, |
| 435 | ART_BASE_ADDRESS_MAX_DELTA); |
Andreas Gampe | febf0bf | 2016-02-29 18:04:17 -0800 | [diff] [blame] | 436 | cmd.push_back(StringPrintf("--base-offset-delta=%d", base_offset)); |
Andreas Gampe | 5709b57 | 2016-02-12 17:42:59 -0800 | [diff] [blame] | 437 | |
| 438 | std::string error_msg; |
| 439 | bool result = Exec(cmd, &error_msg); |
| 440 | if (!result) { |
| 441 | LOG(ERROR) << "Could not generate boot image: " << error_msg; |
| 442 | } |
| 443 | return result; |
| 444 | } |
| 445 | |
| 446 | bool Dex2oatBootImage(const std::string& boot_cp, |
| 447 | const std::string& art_path, |
| 448 | const std::string& oat_path, |
Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 449 | const char* isa) const { |
Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 450 | // This needs to be kept in sync with ART, see art/runtime/gc/space/image_space.cc. |
| 451 | std::vector<std::string> cmd; |
Andreas Gampe | 9fb85b0 | 2016-03-16 10:09:29 -0700 | [diff] [blame] | 452 | cmd.push_back("/system/bin/dex2oat"); |
Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 453 | cmd.push_back(StringPrintf("--image=%s", art_path.c_str())); |
Andreas Gampe | 6db8db9 | 2016-06-03 10:22:19 -0700 | [diff] [blame] | 454 | for (const std::string& boot_part : Split(boot_cp, ":")) { |
Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 455 | cmd.push_back(StringPrintf("--dex-file=%s", boot_part.c_str())); |
| 456 | } |
| 457 | cmd.push_back(StringPrintf("--oat-file=%s", oat_path.c_str())); |
| 458 | |
| 459 | int32_t base_offset = ChooseRelocationOffsetDelta(ART_BASE_ADDRESS_MIN_DELTA, |
| 460 | ART_BASE_ADDRESS_MAX_DELTA); |
| 461 | cmd.push_back(StringPrintf("--base=0x%x", ART_BASE_ADDRESS + base_offset)); |
| 462 | |
| 463 | cmd.push_back(StringPrintf("--instruction-set=%s", isa)); |
| 464 | |
| 465 | // These things are pushed by AndroidRuntime, see frameworks/base/core/jni/AndroidRuntime.cpp. |
| 466 | AddCompilerOptionFromSystemProperty("dalvik.vm.image-dex2oat-Xms", |
| 467 | "-Xms", |
| 468 | true, |
| 469 | cmd); |
| 470 | AddCompilerOptionFromSystemProperty("dalvik.vm.image-dex2oat-Xmx", |
| 471 | "-Xmx", |
| 472 | true, |
| 473 | cmd); |
| 474 | AddCompilerOptionFromSystemProperty("dalvik.vm.image-dex2oat-filter", |
| 475 | "--compiler-filter=", |
| 476 | false, |
| 477 | cmd); |
Andreas Gampe | 9fb85b0 | 2016-03-16 10:09:29 -0700 | [diff] [blame] | 478 | cmd.push_back("--image-classes=/system/etc/preloaded-classes"); |
Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 479 | // TODO: Compiled-classes. |
| 480 | const std::string* extra_opts = |
| 481 | system_properties_.GetProperty("dalvik.vm.image-dex2oat-flags"); |
| 482 | if (extra_opts != nullptr) { |
Andreas Gampe | 6db8db9 | 2016-06-03 10:22:19 -0700 | [diff] [blame] | 483 | std::vector<std::string> extra_vals = Split(*extra_opts, " "); |
Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 484 | cmd.insert(cmd.end(), extra_vals.begin(), extra_vals.end()); |
| 485 | } |
| 486 | // TODO: Should we lower this? It's usually set close to max, because |
| 487 | // normally there's not much else going on at boot. |
| 488 | AddCompilerOptionFromSystemProperty("dalvik.vm.image-dex2oat-threads", |
| 489 | "-j", |
| 490 | false, |
| 491 | cmd); |
| 492 | AddCompilerOptionFromSystemProperty( |
| 493 | StringPrintf("dalvik.vm.isa.%s.variant", isa).c_str(), |
| 494 | "--instruction-set-variant=", |
| 495 | false, |
| 496 | cmd); |
| 497 | AddCompilerOptionFromSystemProperty( |
| 498 | StringPrintf("dalvik.vm.isa.%s.features", isa).c_str(), |
| 499 | "--instruction-set-features=", |
| 500 | false, |
| 501 | cmd); |
| 502 | |
| 503 | std::string error_msg; |
| 504 | bool result = Exec(cmd, &error_msg); |
| 505 | if (!result) { |
| 506 | LOG(ERROR) << "Could not generate boot image: " << error_msg; |
| 507 | } |
| 508 | return result; |
| 509 | } |
| 510 | |
| 511 | static const char* ParseNull(const char* arg) { |
| 512 | return (strcmp(arg, "!") == 0) ? nullptr : arg; |
| 513 | } |
| 514 | |
Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 515 | bool ShouldSkipPreopt() const { |
Andreas Gampe | 56f79f9 | 2016-06-08 15:11:37 -0700 | [diff] [blame] | 516 | // There's one thing we have to be careful about: we may/will be asked to compile an app |
| 517 | // living in the system image. This may be a valid request - if the app wasn't compiled, |
| 518 | // e.g., if the system image wasn't large enough to include preopted files. However, the |
| 519 | // data we have is from the old system, so the driver (the OTA service) can't actually |
| 520 | // know. Thus, we will get requests for apps that have preopted components. To avoid |
| 521 | // duplication (we'd generate files that are not used and are *not* cleaned up), do two |
| 522 | // simple checks: |
| 523 | // |
| 524 | // 1) Does the apk_path start with the value of ANDROID_ROOT? (~in the system image) |
| 525 | // (For simplicity, assume the value of ANDROID_ROOT does not contain a symlink.) |
| 526 | // |
| 527 | // 2) If you replace the name in the apk_path with "oat," does the path exist? |
| 528 | // (=have a subdirectory for preopted files) |
| 529 | // |
| 530 | // If the answer to both is yes, skip the dexopt. |
| 531 | // |
| 532 | // Note: while one may think it's OK to call dexopt and it will fail (because APKs should |
| 533 | // be stripped), that's not true for APKs signed outside the build system (so the |
| 534 | // jar content must be exactly the same). |
| 535 | |
| 536 | // (This is ugly as it's the only thing where we need to understand the contents |
| 537 | // of package_parameters_, but it beats postponing the decision or using the call- |
| 538 | // backs to do weird things.) |
| 539 | constexpr size_t kApkPathIndex = 0; |
| 540 | CHECK_GT(DEXOPT_PARAM_COUNT, kApkPathIndex); |
| 541 | CHECK(package_parameters_[kApkPathIndex] != nullptr); |
Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 542 | if (StartsWith(package_parameters_[kApkPathIndex], android_root_.c_str())) { |
Andreas Gampe | 56f79f9 | 2016-06-08 15:11:37 -0700 | [diff] [blame] | 543 | const char* last_slash = strrchr(package_parameters_[kApkPathIndex], '/'); |
| 544 | if (last_slash != nullptr) { |
| 545 | std::string path(package_parameters_[kApkPathIndex], |
| 546 | last_slash - package_parameters_[kApkPathIndex] + 1); |
| 547 | CHECK(EndsWith(path, "/")); |
| 548 | path = path + "oat"; |
| 549 | if (access(path.c_str(), F_OK) == 0) { |
Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 550 | return true; |
Andreas Gampe | 56f79f9 | 2016-06-08 15:11:37 -0700 | [diff] [blame] | 551 | } |
| 552 | } |
| 553 | } |
| 554 | |
Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 555 | // Another issue is unavailability of files in the new system. If the partition |
| 556 | // layout changes, otapreopt_chroot may not know about this. Then files from that |
| 557 | // partition will not be available and fail to build. This is problematic, as |
| 558 | // this tool will wipe the OTA artifact cache and try again (for robustness after |
| 559 | // a failed OTA with remaining cache artifacts). |
| 560 | if (access(package_parameters_[kApkPathIndex], F_OK) != 0) { |
| 561 | LOG(WARNING) << "Skipping preopt of non-existing package " |
| 562 | << package_parameters_[kApkPathIndex]; |
| 563 | return true; |
| 564 | } |
| 565 | |
| 566 | return false; |
| 567 | } |
| 568 | |
| 569 | int RunPreopt() { |
| 570 | if (ShouldSkipPreopt()) { |
| 571 | return 0; |
| 572 | } |
| 573 | |
| 574 | int dexopt_result = dexopt(package_parameters_); |
| 575 | if (dexopt_result == 0) { |
| 576 | return 0; |
| 577 | } |
| 578 | |
| 579 | // If the dexopt failed, we may have a stale boot image from a previous OTA run. |
| 580 | // Try to delete and retry. |
| 581 | |
| 582 | if (!PrepareBootImage(/* force */ true)) { |
| 583 | LOG(ERROR) << "Forced boot image creating failed. Original error return was " |
| 584 | << dexopt_result; |
| 585 | return dexopt_result; |
| 586 | } |
| 587 | |
| 588 | LOG(WARNING) << "Original dexopt failed, re-trying after boot image was regenerated."; |
Andreas Gampe | 548bdb9 | 2016-06-02 17:56:45 -0700 | [diff] [blame] | 589 | return dexopt(package_parameters_); |
Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | //////////////////////////////////// |
| 593 | // Helpers, mostly taken from ART // |
| 594 | //////////////////////////////////// |
| 595 | |
| 596 | // Wrapper on fork/execv to run a command in a subprocess. |
Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 597 | static bool Exec(const std::vector<std::string>& arg_vector, std::string* error_msg) { |
Andreas Gampe | 6db8db9 | 2016-06-03 10:22:19 -0700 | [diff] [blame] | 598 | const std::string command_line = Join(arg_vector, ' '); |
Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 599 | |
| 600 | CHECK_GE(arg_vector.size(), 1U) << command_line; |
| 601 | |
| 602 | // Convert the args to char pointers. |
| 603 | const char* program = arg_vector[0].c_str(); |
| 604 | std::vector<char*> args; |
| 605 | for (size_t i = 0; i < arg_vector.size(); ++i) { |
| 606 | const std::string& arg = arg_vector[i]; |
| 607 | char* arg_str = const_cast<char*>(arg.c_str()); |
| 608 | CHECK(arg_str != nullptr) << i; |
| 609 | args.push_back(arg_str); |
| 610 | } |
| 611 | args.push_back(nullptr); |
| 612 | |
| 613 | // Fork and exec. |
| 614 | pid_t pid = fork(); |
| 615 | if (pid == 0) { |
| 616 | // No allocation allowed between fork and exec. |
| 617 | |
| 618 | // Change process groups, so we don't get reaped by ProcessManager. |
| 619 | setpgid(0, 0); |
| 620 | |
| 621 | execv(program, &args[0]); |
| 622 | |
| 623 | PLOG(ERROR) << "Failed to execv(" << command_line << ")"; |
| 624 | // _exit to avoid atexit handlers in child. |
| 625 | _exit(1); |
| 626 | } else { |
| 627 | if (pid == -1) { |
| 628 | *error_msg = StringPrintf("Failed to execv(%s) because fork failed: %s", |
| 629 | command_line.c_str(), strerror(errno)); |
| 630 | return false; |
| 631 | } |
| 632 | |
| 633 | // wait for subprocess to finish |
| 634 | int status; |
| 635 | pid_t got_pid = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0)); |
| 636 | if (got_pid != pid) { |
| 637 | *error_msg = StringPrintf("Failed after fork for execv(%s) because waitpid failed: " |
| 638 | "wanted %d, got %d: %s", |
| 639 | command_line.c_str(), pid, got_pid, strerror(errno)); |
| 640 | return false; |
| 641 | } |
| 642 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
| 643 | *error_msg = StringPrintf("Failed execv(%s) because non-0 exit status", |
| 644 | command_line.c_str()); |
| 645 | return false; |
| 646 | } |
| 647 | } |
| 648 | return true; |
| 649 | } |
| 650 | |
| 651 | // Choose a random relocation offset. Taken from art/runtime/gc/image_space.cc. |
| 652 | static int32_t ChooseRelocationOffsetDelta(int32_t min_delta, int32_t max_delta) { |
| 653 | constexpr size_t kPageSize = PAGE_SIZE; |
| 654 | CHECK_EQ(min_delta % kPageSize, 0u); |
| 655 | CHECK_EQ(max_delta % kPageSize, 0u); |
| 656 | CHECK_LT(min_delta, max_delta); |
| 657 | |
| 658 | std::default_random_engine generator; |
| 659 | generator.seed(GetSeed()); |
| 660 | std::uniform_int_distribution<int32_t> distribution(min_delta, max_delta); |
| 661 | int32_t r = distribution(generator); |
| 662 | if (r % 2 == 0) { |
| 663 | r = RoundUp(r, kPageSize); |
| 664 | } else { |
| 665 | r = RoundDown(r, kPageSize); |
| 666 | } |
| 667 | CHECK_LE(min_delta, r); |
| 668 | CHECK_GE(max_delta, r); |
| 669 | CHECK_EQ(r % kPageSize, 0u); |
| 670 | return r; |
| 671 | } |
| 672 | |
| 673 | static uint64_t GetSeed() { |
| 674 | #ifdef __BIONIC__ |
| 675 | // Bionic exposes arc4random, use it. |
| 676 | uint64_t random_data; |
| 677 | arc4random_buf(&random_data, sizeof(random_data)); |
| 678 | return random_data; |
| 679 | #else |
| 680 | #error "This is only supposed to run with bionic. Otherwise, implement..." |
| 681 | #endif |
| 682 | } |
| 683 | |
| 684 | void AddCompilerOptionFromSystemProperty(const char* system_property, |
| 685 | const char* prefix, |
| 686 | bool runtime, |
Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 687 | std::vector<std::string>& out) const { |
| 688 | const std::string* value = system_properties_.GetProperty(system_property); |
Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 689 | if (value != nullptr) { |
| 690 | if (runtime) { |
| 691 | out.push_back("--runtime-arg"); |
| 692 | } |
| 693 | if (prefix != nullptr) { |
| 694 | out.push_back(StringPrintf("%s%s", prefix, value->c_str())); |
| 695 | } else { |
| 696 | out.push_back(*value); |
| 697 | } |
| 698 | } |
| 699 | } |
| 700 | |
Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 701 | static constexpr const char* kBootClassPathPropertyName = "BOOTCLASSPATH"; |
| 702 | static constexpr const char* kAndroidRootPathPropertyName = "ANDROID_ROOT"; |
| 703 | static constexpr const char* kAndroidDataPathPropertyName = "ANDROID_DATA"; |
| 704 | // The index of the instruction-set string inside the package parameters. Needed for |
| 705 | // some special-casing that requires knowledge of the instruction-set. |
| 706 | static constexpr size_t kISAIndex = 3; |
| 707 | |
Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 708 | // Stores the system properties read out of the B partition. We need to use these properties |
| 709 | // to compile, instead of the A properties we could get from init/get_property. |
| 710 | SystemProperties system_properties_; |
| 711 | |
Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 712 | // Some select properties that are always needed. |
| 713 | std::string target_slot_; |
| 714 | std::string android_root_; |
| 715 | std::string android_data_; |
| 716 | std::string boot_classpath_; |
| 717 | std::string asec_mountpoint_; |
| 718 | |
Andreas Gampe | 548bdb9 | 2016-06-02 17:56:45 -0700 | [diff] [blame] | 719 | const char* package_parameters_[DEXOPT_PARAM_COUNT]; |
Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 720 | |
| 721 | // Store environment values we need to set. |
| 722 | std::vector<std::string> environ_; |
| 723 | }; |
| 724 | |
| 725 | OTAPreoptService gOps; |
| 726 | |
| 727 | //////////////////////// |
| 728 | // Plug-in functions. // |
| 729 | //////////////////////// |
| 730 | |
| 731 | int get_property(const char *key, char *value, const char *default_value) { |
Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 732 | return gOps.GetProperty(key, value, default_value); |
| 733 | } |
| 734 | |
| 735 | // Compute the output path of |
| 736 | bool calculate_oat_file_path(char path[PKG_PATH_MAX], const char *oat_dir, |
| 737 | const char *apk_path, |
| 738 | const char *instruction_set) { |
Dan Austin | 9c8f93a | 2016-06-03 16:15:54 -0700 | [diff] [blame] | 739 | const char *file_name_start; |
| 740 | const char *file_name_end; |
Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 741 | |
| 742 | file_name_start = strrchr(apk_path, '/'); |
| 743 | if (file_name_start == nullptr) { |
| 744 | ALOGE("apk_path '%s' has no '/'s in it\n", apk_path); |
| 745 | return false; |
| 746 | } |
| 747 | file_name_end = strrchr(file_name_start, '.'); |
| 748 | if (file_name_end == nullptr) { |
| 749 | ALOGE("apk_path '%s' has no extension\n", apk_path); |
| 750 | return false; |
| 751 | } |
| 752 | |
| 753 | // Calculate file_name |
| 754 | file_name_start++; // Move past '/', is valid as file_name_end is valid. |
| 755 | size_t file_name_len = file_name_end - file_name_start; |
| 756 | std::string file_name(file_name_start, file_name_len); |
| 757 | |
| 758 | // <apk_parent_dir>/oat/<isa>/<file_name>.odex.b |
Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 759 | snprintf(path, |
| 760 | PKG_PATH_MAX, |
| 761 | "%s/%s/%s.odex.%s", |
| 762 | oat_dir, |
| 763 | instruction_set, |
| 764 | file_name.c_str(), |
| 765 | gOps.GetTargetSlot().c_str()); |
Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 766 | return true; |
| 767 | } |
| 768 | |
| 769 | /* |
| 770 | * Computes the odex file for the given apk_path and instruction_set. |
| 771 | * /system/framework/whatever.jar -> /system/framework/oat/<isa>/whatever.odex |
| 772 | * |
| 773 | * Returns false if it failed to determine the odex file path. |
| 774 | */ |
| 775 | bool calculate_odex_file_path(char path[PKG_PATH_MAX], const char *apk_path, |
| 776 | const char *instruction_set) { |
Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 777 | const char *path_end = strrchr(apk_path, '/'); |
| 778 | if (path_end == nullptr) { |
| 779 | ALOGE("apk_path '%s' has no '/'s in it?!\n", apk_path); |
| 780 | return false; |
| 781 | } |
| 782 | std::string path_component(apk_path, path_end - apk_path); |
| 783 | |
| 784 | const char *name_begin = path_end + 1; |
| 785 | const char *extension_start = strrchr(name_begin, '.'); |
| 786 | if (extension_start == nullptr) { |
| 787 | ALOGE("apk_path '%s' has no extension.\n", apk_path); |
| 788 | return false; |
| 789 | } |
| 790 | std::string name_component(name_begin, extension_start - name_begin); |
| 791 | |
Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 792 | std::string new_path = StringPrintf("%s/oat/%s/%s.odex.%s", |
Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 793 | path_component.c_str(), |
| 794 | instruction_set, |
Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 795 | name_component.c_str(), |
| 796 | gOps.GetTargetSlot().c_str()); |
| 797 | if (new_path.length() >= PKG_PATH_MAX) { |
| 798 | LOG(ERROR) << "apk_path of " << apk_path << " is too long: " << new_path; |
| 799 | return false; |
| 800 | } |
Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 801 | strcpy(path, new_path.c_str()); |
| 802 | return true; |
| 803 | } |
| 804 | |
| 805 | bool create_cache_path(char path[PKG_PATH_MAX], |
| 806 | const char *src, |
| 807 | const char *instruction_set) { |
| 808 | size_t srclen = strlen(src); |
| 809 | |
| 810 | /* demand that we are an absolute path */ |
| 811 | if ((src == 0) || (src[0] != '/') || strstr(src,"..")) { |
| 812 | return false; |
| 813 | } |
| 814 | |
| 815 | if (srclen > PKG_PATH_MAX) { // XXX: PKG_NAME_MAX? |
| 816 | return false; |
| 817 | } |
| 818 | |
| 819 | std::string from_src = std::string(src + 1); |
| 820 | std::replace(from_src.begin(), from_src.end(), '/', '@'); |
| 821 | |
| 822 | std::string assembled_path = StringPrintf("%s/%s/%s/%s%s", |
Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 823 | gOps.GetOTADataDirectory().c_str(), |
Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 824 | DALVIK_CACHE, |
| 825 | instruction_set, |
| 826 | from_src.c_str(), |
David Brazdil | 249c179 | 2016-09-06 15:35:28 +0100 | [diff] [blame] | 827 | DALVIK_CACHE_POSTFIX); |
Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 828 | |
| 829 | if (assembled_path.length() + 1 > PKG_PATH_MAX) { |
| 830 | return false; |
| 831 | } |
| 832 | strcpy(path, assembled_path.c_str()); |
| 833 | |
| 834 | return true; |
| 835 | } |
| 836 | |
Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 837 | static int log_callback(int type, const char *fmt, ...) { |
| 838 | va_list ap; |
| 839 | int priority; |
| 840 | |
| 841 | switch (type) { |
| 842 | case SELINUX_WARNING: |
| 843 | priority = ANDROID_LOG_WARN; |
| 844 | break; |
| 845 | case SELINUX_INFO: |
| 846 | priority = ANDROID_LOG_INFO; |
| 847 | break; |
| 848 | default: |
| 849 | priority = ANDROID_LOG_ERROR; |
| 850 | break; |
| 851 | } |
| 852 | va_start(ap, fmt); |
| 853 | LOG_PRI_VA(priority, "SELinux", fmt, ap); |
| 854 | va_end(ap); |
| 855 | return 0; |
| 856 | } |
| 857 | |
| 858 | static int otapreopt_main(const int argc, char *argv[]) { |
| 859 | int selinux_enabled = (is_selinux_enabled() > 0); |
| 860 | |
| 861 | setenv("ANDROID_LOG_TAGS", "*:v", 1); |
| 862 | android::base::InitLogging(argv); |
| 863 | |
Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 864 | if (argc < 2) { |
| 865 | ALOGE("Expecting parameters"); |
| 866 | exit(1); |
| 867 | } |
| 868 | |
| 869 | union selinux_callback cb; |
| 870 | cb.func_log = log_callback; |
| 871 | selinux_set_callback(SELINUX_CB_LOG, cb); |
| 872 | |
Andreas Gampe | 73dae11 | 2015-11-19 14:12:14 -0800 | [diff] [blame] | 873 | if (selinux_enabled && selinux_status_open(true) < 0) { |
| 874 | ALOGE("Could not open selinux status; exiting.\n"); |
| 875 | exit(1); |
| 876 | } |
| 877 | |
| 878 | int ret = android::installd::gOps.Main(argc, argv); |
| 879 | |
| 880 | return ret; |
| 881 | } |
| 882 | |
| 883 | } // namespace installd |
| 884 | } // namespace android |
| 885 | |
| 886 | int main(const int argc, char *argv[]) { |
| 887 | return android::installd::otapreopt_main(argc, argv); |
| 888 | } |