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