otapreopt.cpp revision 56f79f96207c9c0101a5ca0237a8da631d93d5c5
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> 20#include <regex> 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> 33#include <android-base/strings.h> 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> 40#include <file_parsing.h> 41#include <globals.h> 42#include <installd_deps.h> // Need to fill in requirements of commands. 43#include <system_properties.h> 44#include <utils.h> 45 46#ifndef LOG_TAG 47#define LOG_TAG "otapreopt" 48#endif 49 50#define BUFFER_MAX 1024 /* input buffer for commands */ 51#define TOKEN_MAX 16 /* max number of arguments in buffer */ 52#define REPLY_MAX 256 /* largest reply allowed */ 53 54using android::base::EndsWith; 55using android::base::Join; 56using android::base::Split; 57using android::base::StartsWith; 58using android::base::StringPrintf; 59 60namespace android { 61namespace installd { 62 63static constexpr const char* kBootClassPathPropertyName = "BOOTCLASSPATH"; 64static constexpr const char* kAndroidRootPathPropertyName = "ANDROID_ROOT"; 65static constexpr const char* kOTARootDirectory = "/system-b"; 66static constexpr size_t kISAIndex = 3; 67 68template<typename T> 69static constexpr T RoundDown(T x, typename std::decay<T>::type n) { 70 return DCHECK_CONSTEXPR(IsPowerOfTwo(n), , T(0))(x & -n); 71} 72 73template<typename T> 74static constexpr T RoundUp(T x, typename std::remove_reference<T>::type n) { 75 return RoundDown(x + n - 1, n); 76} 77 78class OTAPreoptService { 79 public: 80 static constexpr const char* kOTADataDirectory = "/data/ota"; 81 82 // Main driver. Performs the following steps. 83 // 84 // 1) Parse options (read system properties etc from B partition). 85 // 86 // 2) Read in package data. 87 // 88 // 3) Prepare environment variables. 89 // 90 // 4) Prepare(compile) boot image, if necessary. 91 // 92 // 5) Run update. 93 int Main(int argc, char** argv) { 94 if (!ReadSystemProperties()) { 95 LOG(ERROR)<< "Failed reading system properties."; 96 return 1; 97 } 98 99 if (!ReadEnvironment()) { 100 LOG(ERROR) << "Failed reading environment properties."; 101 return 2; 102 } 103 104 if (!ReadPackage(argc, argv)) { 105 LOG(ERROR) << "Failed reading command line file."; 106 return 3; 107 } 108 109 PrepareEnvironment(); 110 111 if (!PrepareBootImage()) { 112 LOG(ERROR) << "Failed preparing boot image."; 113 return 4; 114 } 115 116 int dexopt_retcode = RunPreopt(); 117 118 return dexopt_retcode; 119 } 120 121 int GetProperty(const char* key, char* value, const char* default_value) { 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 138private: 139 bool ReadSystemProperties() { 140 static constexpr const char* kPropertyFiles[] = { 141 "/default.prop", "/system/build.prop" 142 }; 143 144 for (size_t i = 0; i < arraysize(kPropertyFiles); ++i) { 145 if (!system_properties_.Load(kPropertyFiles[i])) { 146 return false; 147 } 148 } 149 150 return true; 151 } 152 153 bool ReadEnvironment() { 154 // Parse the environment variables from init.environ.rc, which have the form 155 // export NAME VALUE 156 // For simplicity, don't respect string quotation. The values we are interested in can be 157 // encoded without them. 158 std::regex export_regex("\\s*export\\s+(\\S+)\\s+(\\S+)"); 159 bool parse_result = ParseFile("/init.environ.rc", [&](const std::string& line) { 160 std::smatch export_match; 161 if (!std::regex_match(line, export_match, export_regex)) { 162 return true; 163 } 164 165 if (export_match.size() != 3) { 166 return true; 167 } 168 169 std::string name = export_match[1].str(); 170 std::string value = export_match[2].str(); 171 172 system_properties_.SetProperty(name, value); 173 174 return true; 175 }); 176 if (!parse_result) { 177 return false; 178 } 179 180 // Check that we found important properties. 181 constexpr const char* kRequiredProperties[] = { 182 kBootClassPathPropertyName, kAndroidRootPathPropertyName 183 }; 184 for (size_t i = 0; i < arraysize(kRequiredProperties); ++i) { 185 if (system_properties_.GetProperty(kRequiredProperties[i]) == nullptr) { 186 return false; 187 } 188 } 189 190 return true; 191 } 192 193 bool ReadPackage(int argc ATTRIBUTE_UNUSED, char** argv) { 194 size_t index = 0; 195 static_assert(DEXOPT_PARAM_COUNT == ARRAY_SIZE(package_parameters_), 196 "Unexpected dexopt param count"); 197 while (index < DEXOPT_PARAM_COUNT && 198 argv[index + 1] != nullptr) { 199 package_parameters_[index] = argv[index + 1]; 200 index++; 201 } 202 if (index != ARRAY_SIZE(package_parameters_) || argv[index + 1] != nullptr) { 203 LOG(ERROR) << "Wrong number of parameters"; 204 return false; 205 } 206 207 return true; 208 } 209 210 void PrepareEnvironment() { 211 CHECK(system_properties_.GetProperty(kBootClassPathPropertyName) != nullptr); 212 const std::string& boot_cp = 213 *system_properties_.GetProperty(kBootClassPathPropertyName); 214 environ_.push_back(StringPrintf("BOOTCLASSPATH=%s", boot_cp.c_str())); 215 environ_.push_back(StringPrintf("ANDROID_DATA=%s", kOTADataDirectory)); 216 CHECK(system_properties_.GetProperty(kAndroidRootPathPropertyName) != nullptr); 217 const std::string& android_root = 218 *system_properties_.GetProperty(kAndroidRootPathPropertyName); 219 environ_.push_back(StringPrintf("ANDROID_ROOT=%s", android_root.c_str())); 220 221 for (const std::string& e : environ_) { 222 putenv(const_cast<char*>(e.c_str())); 223 } 224 } 225 226 // Ensure that we have the right boot image. The first time any app is 227 // compiled, we'll try to generate it. 228 bool PrepareBootImage() { 229 if (package_parameters_[kISAIndex] == nullptr) { 230 LOG(ERROR) << "Instruction set missing."; 231 return false; 232 } 233 const char* isa = package_parameters_[kISAIndex]; 234 235 // Check whether the file exists where expected. 236 std::string dalvik_cache = std::string(kOTADataDirectory) + "/" + DALVIK_CACHE; 237 std::string isa_path = dalvik_cache + "/" + isa; 238 std::string art_path = isa_path + "/system@framework@boot.art"; 239 std::string oat_path = isa_path + "/system@framework@boot.oat"; 240 if (access(art_path.c_str(), F_OK) == 0 && 241 access(oat_path.c_str(), F_OK) == 0) { 242 // Files exist, assume everything is alright. 243 return true; 244 } 245 246 // Create the directories, if necessary. 247 if (access(dalvik_cache.c_str(), F_OK) != 0) { 248 if (mkdir(dalvik_cache.c_str(), 0711) != 0) { 249 PLOG(ERROR) << "Could not create dalvik-cache dir"; 250 return false; 251 } 252 } 253 if (access(isa_path.c_str(), F_OK) != 0) { 254 if (mkdir(isa_path.c_str(), 0711) != 0) { 255 PLOG(ERROR) << "Could not create dalvik-cache isa dir"; 256 return false; 257 } 258 } 259 260 // Prepare to create. 261 // TODO: Delete files, just for a blank slate. 262 const std::string& boot_cp = *system_properties_.GetProperty(kBootClassPathPropertyName); 263 264 std::string preopted_boot_art_path = StringPrintf("/system/framework/%s/boot.art", isa); 265 if (access(preopted_boot_art_path.c_str(), F_OK) == 0) { 266 return PatchoatBootImage(art_path, isa); 267 } else { 268 // No preopted boot image. Try to compile. 269 return Dex2oatBootImage(boot_cp, art_path, oat_path, isa); 270 } 271 } 272 273 bool PatchoatBootImage(const std::string& art_path, const char* isa) { 274 // This needs to be kept in sync with ART, see art/runtime/gc/space/image_space.cc. 275 276 std::vector<std::string> cmd; 277 cmd.push_back("/system/bin/patchoat"); 278 279 cmd.push_back("--input-image-location=/system/framework/boot.art"); 280 cmd.push_back(StringPrintf("--output-image-file=%s", art_path.c_str())); 281 282 cmd.push_back(StringPrintf("--instruction-set=%s", isa)); 283 284 int32_t base_offset = ChooseRelocationOffsetDelta(ART_BASE_ADDRESS_MIN_DELTA, 285 ART_BASE_ADDRESS_MAX_DELTA); 286 cmd.push_back(StringPrintf("--base-offset-delta=%d", base_offset)); 287 288 std::string error_msg; 289 bool result = Exec(cmd, &error_msg); 290 if (!result) { 291 LOG(ERROR) << "Could not generate boot image: " << error_msg; 292 } 293 return result; 294 } 295 296 bool Dex2oatBootImage(const std::string& boot_cp, 297 const std::string& art_path, 298 const std::string& oat_path, 299 const char* isa) { 300 // This needs to be kept in sync with ART, see art/runtime/gc/space/image_space.cc. 301 std::vector<std::string> cmd; 302 cmd.push_back("/system/bin/dex2oat"); 303 cmd.push_back(StringPrintf("--image=%s", art_path.c_str())); 304 for (const std::string& boot_part : Split(boot_cp, ":")) { 305 cmd.push_back(StringPrintf("--dex-file=%s", boot_part.c_str())); 306 } 307 cmd.push_back(StringPrintf("--oat-file=%s", oat_path.c_str())); 308 309 int32_t base_offset = ChooseRelocationOffsetDelta(ART_BASE_ADDRESS_MIN_DELTA, 310 ART_BASE_ADDRESS_MAX_DELTA); 311 cmd.push_back(StringPrintf("--base=0x%x", ART_BASE_ADDRESS + base_offset)); 312 313 cmd.push_back(StringPrintf("--instruction-set=%s", isa)); 314 315 // These things are pushed by AndroidRuntime, see frameworks/base/core/jni/AndroidRuntime.cpp. 316 AddCompilerOptionFromSystemProperty("dalvik.vm.image-dex2oat-Xms", 317 "-Xms", 318 true, 319 cmd); 320 AddCompilerOptionFromSystemProperty("dalvik.vm.image-dex2oat-Xmx", 321 "-Xmx", 322 true, 323 cmd); 324 AddCompilerOptionFromSystemProperty("dalvik.vm.image-dex2oat-filter", 325 "--compiler-filter=", 326 false, 327 cmd); 328 cmd.push_back("--image-classes=/system/etc/preloaded-classes"); 329 // TODO: Compiled-classes. 330 const std::string* extra_opts = 331 system_properties_.GetProperty("dalvik.vm.image-dex2oat-flags"); 332 if (extra_opts != nullptr) { 333 std::vector<std::string> extra_vals = Split(*extra_opts, " "); 334 cmd.insert(cmd.end(), extra_vals.begin(), extra_vals.end()); 335 } 336 // TODO: Should we lower this? It's usually set close to max, because 337 // normally there's not much else going on at boot. 338 AddCompilerOptionFromSystemProperty("dalvik.vm.image-dex2oat-threads", 339 "-j", 340 false, 341 cmd); 342 AddCompilerOptionFromSystemProperty( 343 StringPrintf("dalvik.vm.isa.%s.variant", isa).c_str(), 344 "--instruction-set-variant=", 345 false, 346 cmd); 347 AddCompilerOptionFromSystemProperty( 348 StringPrintf("dalvik.vm.isa.%s.features", isa).c_str(), 349 "--instruction-set-features=", 350 false, 351 cmd); 352 353 std::string error_msg; 354 bool result = Exec(cmd, &error_msg); 355 if (!result) { 356 LOG(ERROR) << "Could not generate boot image: " << error_msg; 357 } 358 return result; 359 } 360 361 static const char* ParseNull(const char* arg) { 362 return (strcmp(arg, "!") == 0) ? nullptr : arg; 363 } 364 365 int RunPreopt() { 366 // Run the preopt. 367 // 368 // There's one thing we have to be careful about: we may/will be asked to compile an app 369 // living in the system image. This may be a valid request - if the app wasn't compiled, 370 // e.g., if the system image wasn't large enough to include preopted files. However, the 371 // data we have is from the old system, so the driver (the OTA service) can't actually 372 // know. Thus, we will get requests for apps that have preopted components. To avoid 373 // duplication (we'd generate files that are not used and are *not* cleaned up), do two 374 // simple checks: 375 // 376 // 1) Does the apk_path start with the value of ANDROID_ROOT? (~in the system image) 377 // (For simplicity, assume the value of ANDROID_ROOT does not contain a symlink.) 378 // 379 // 2) If you replace the name in the apk_path with "oat," does the path exist? 380 // (=have a subdirectory for preopted files) 381 // 382 // If the answer to both is yes, skip the dexopt. 383 // 384 // Note: while one may think it's OK to call dexopt and it will fail (because APKs should 385 // be stripped), that's not true for APKs signed outside the build system (so the 386 // jar content must be exactly the same). 387 388 // (This is ugly as it's the only thing where we need to understand the contents 389 // of package_parameters_, but it beats postponing the decision or using the call- 390 // backs to do weird things.) 391 constexpr size_t kApkPathIndex = 0; 392 CHECK_GT(DEXOPT_PARAM_COUNT, kApkPathIndex); 393 CHECK(package_parameters_[kApkPathIndex] != nullptr); 394 CHECK(system_properties_.GetProperty(kAndroidRootPathPropertyName) != nullptr); 395 if (StartsWith(package_parameters_[kApkPathIndex], 396 system_properties_.GetProperty(kAndroidRootPathPropertyName)->c_str())) { 397 const char* last_slash = strrchr(package_parameters_[kApkPathIndex], '/'); 398 if (last_slash != nullptr) { 399 std::string path(package_parameters_[kApkPathIndex], 400 last_slash - package_parameters_[kApkPathIndex] + 1); 401 CHECK(EndsWith(path, "/")); 402 path = path + "oat"; 403 if (access(path.c_str(), F_OK) == 0) { 404 return 0; 405 } 406 } 407 } 408 409 return dexopt(package_parameters_); 410 } 411 412 //////////////////////////////////// 413 // Helpers, mostly taken from ART // 414 //////////////////////////////////// 415 416 // Wrapper on fork/execv to run a command in a subprocess. 417 bool Exec(const std::vector<std::string>& arg_vector, std::string* error_msg) { 418 const std::string command_line = Join(arg_vector, ' '); 419 420 CHECK_GE(arg_vector.size(), 1U) << command_line; 421 422 // Convert the args to char pointers. 423 const char* program = arg_vector[0].c_str(); 424 std::vector<char*> args; 425 for (size_t i = 0; i < arg_vector.size(); ++i) { 426 const std::string& arg = arg_vector[i]; 427 char* arg_str = const_cast<char*>(arg.c_str()); 428 CHECK(arg_str != nullptr) << i; 429 args.push_back(arg_str); 430 } 431 args.push_back(nullptr); 432 433 // Fork and exec. 434 pid_t pid = fork(); 435 if (pid == 0) { 436 // No allocation allowed between fork and exec. 437 438 // Change process groups, so we don't get reaped by ProcessManager. 439 setpgid(0, 0); 440 441 execv(program, &args[0]); 442 443 PLOG(ERROR) << "Failed to execv(" << command_line << ")"; 444 // _exit to avoid atexit handlers in child. 445 _exit(1); 446 } else { 447 if (pid == -1) { 448 *error_msg = StringPrintf("Failed to execv(%s) because fork failed: %s", 449 command_line.c_str(), strerror(errno)); 450 return false; 451 } 452 453 // wait for subprocess to finish 454 int status; 455 pid_t got_pid = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0)); 456 if (got_pid != pid) { 457 *error_msg = StringPrintf("Failed after fork for execv(%s) because waitpid failed: " 458 "wanted %d, got %d: %s", 459 command_line.c_str(), pid, got_pid, strerror(errno)); 460 return false; 461 } 462 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { 463 *error_msg = StringPrintf("Failed execv(%s) because non-0 exit status", 464 command_line.c_str()); 465 return false; 466 } 467 } 468 return true; 469 } 470 471 // Choose a random relocation offset. Taken from art/runtime/gc/image_space.cc. 472 static int32_t ChooseRelocationOffsetDelta(int32_t min_delta, int32_t max_delta) { 473 constexpr size_t kPageSize = PAGE_SIZE; 474 CHECK_EQ(min_delta % kPageSize, 0u); 475 CHECK_EQ(max_delta % kPageSize, 0u); 476 CHECK_LT(min_delta, max_delta); 477 478 std::default_random_engine generator; 479 generator.seed(GetSeed()); 480 std::uniform_int_distribution<int32_t> distribution(min_delta, max_delta); 481 int32_t r = distribution(generator); 482 if (r % 2 == 0) { 483 r = RoundUp(r, kPageSize); 484 } else { 485 r = RoundDown(r, kPageSize); 486 } 487 CHECK_LE(min_delta, r); 488 CHECK_GE(max_delta, r); 489 CHECK_EQ(r % kPageSize, 0u); 490 return r; 491 } 492 493 static uint64_t GetSeed() { 494#ifdef __BIONIC__ 495 // Bionic exposes arc4random, use it. 496 uint64_t random_data; 497 arc4random_buf(&random_data, sizeof(random_data)); 498 return random_data; 499#else 500#error "This is only supposed to run with bionic. Otherwise, implement..." 501#endif 502 } 503 504 void AddCompilerOptionFromSystemProperty(const char* system_property, 505 const char* prefix, 506 bool runtime, 507 std::vector<std::string>& out) { 508 const std::string* value = 509 system_properties_.GetProperty(system_property); 510 if (value != nullptr) { 511 if (runtime) { 512 out.push_back("--runtime-arg"); 513 } 514 if (prefix != nullptr) { 515 out.push_back(StringPrintf("%s%s", prefix, value->c_str())); 516 } else { 517 out.push_back(*value); 518 } 519 } 520 } 521 522 // Stores the system properties read out of the B partition. We need to use these properties 523 // to compile, instead of the A properties we could get from init/get_property. 524 SystemProperties system_properties_; 525 526 const char* package_parameters_[DEXOPT_PARAM_COUNT]; 527 528 // Store environment values we need to set. 529 std::vector<std::string> environ_; 530}; 531 532OTAPreoptService gOps; 533 534//////////////////////// 535// Plug-in functions. // 536//////////////////////// 537 538int get_property(const char *key, char *value, const char *default_value) { 539 return gOps.GetProperty(key, value, default_value); 540} 541 542// Compute the output path of 543bool calculate_oat_file_path(char path[PKG_PATH_MAX], const char *oat_dir, 544 const char *apk_path, 545 const char *instruction_set) { 546 const char *file_name_start; 547 const char *file_name_end; 548 549 file_name_start = strrchr(apk_path, '/'); 550 if (file_name_start == nullptr) { 551 ALOGE("apk_path '%s' has no '/'s in it\n", apk_path); 552 return false; 553 } 554 file_name_end = strrchr(file_name_start, '.'); 555 if (file_name_end == nullptr) { 556 ALOGE("apk_path '%s' has no extension\n", apk_path); 557 return false; 558 } 559 560 // Calculate file_name 561 file_name_start++; // Move past '/', is valid as file_name_end is valid. 562 size_t file_name_len = file_name_end - file_name_start; 563 std::string file_name(file_name_start, file_name_len); 564 565 // <apk_parent_dir>/oat/<isa>/<file_name>.odex.b 566 snprintf(path, PKG_PATH_MAX, "%s/%s/%s.odex.b", oat_dir, instruction_set, 567 file_name.c_str()); 568 return true; 569} 570 571/* 572 * Computes the odex file for the given apk_path and instruction_set. 573 * /system/framework/whatever.jar -> /system/framework/oat/<isa>/whatever.odex 574 * 575 * Returns false if it failed to determine the odex file path. 576 */ 577bool calculate_odex_file_path(char path[PKG_PATH_MAX], const char *apk_path, 578 const char *instruction_set) { 579 if (StringPrintf("%soat/%s/odex.b", apk_path, instruction_set).length() + 1 > PKG_PATH_MAX) { 580 ALOGE("apk_path '%s' may be too long to form odex file path.\n", apk_path); 581 return false; 582 } 583 584 const char *path_end = strrchr(apk_path, '/'); 585 if (path_end == nullptr) { 586 ALOGE("apk_path '%s' has no '/'s in it?!\n", apk_path); 587 return false; 588 } 589 std::string path_component(apk_path, path_end - apk_path); 590 591 const char *name_begin = path_end + 1; 592 const char *extension_start = strrchr(name_begin, '.'); 593 if (extension_start == nullptr) { 594 ALOGE("apk_path '%s' has no extension.\n", apk_path); 595 return false; 596 } 597 std::string name_component(name_begin, extension_start - name_begin); 598 599 std::string new_path = StringPrintf("%s/oat/%s/%s.odex.b", 600 path_component.c_str(), 601 instruction_set, 602 name_component.c_str()); 603 CHECK_LT(new_path.length(), PKG_PATH_MAX); 604 strcpy(path, new_path.c_str()); 605 return true; 606} 607 608bool create_cache_path(char path[PKG_PATH_MAX], 609 const char *src, 610 const char *instruction_set) { 611 size_t srclen = strlen(src); 612 613 /* demand that we are an absolute path */ 614 if ((src == 0) || (src[0] != '/') || strstr(src,"..")) { 615 return false; 616 } 617 618 if (srclen > PKG_PATH_MAX) { // XXX: PKG_NAME_MAX? 619 return false; 620 } 621 622 std::string from_src = std::string(src + 1); 623 std::replace(from_src.begin(), from_src.end(), '/', '@'); 624 625 std::string assembled_path = StringPrintf("%s/%s/%s/%s%s", 626 OTAPreoptService::kOTADataDirectory, 627 DALVIK_CACHE, 628 instruction_set, 629 from_src.c_str(), 630 DALVIK_CACHE_POSTFIX2); 631 632 if (assembled_path.length() + 1 > PKG_PATH_MAX) { 633 return false; 634 } 635 strcpy(path, assembled_path.c_str()); 636 637 return true; 638} 639 640bool initialize_globals() { 641 const char* data_path = getenv("ANDROID_DATA"); 642 if (data_path == nullptr) { 643 ALOGE("Could not find ANDROID_DATA"); 644 return false; 645 } 646 return init_globals_from_data_and_root(data_path, kOTARootDirectory); 647} 648 649static bool initialize_directories() { 650 // This is different from the normal installd. We only do the base 651 // directory, the rest will be created on demand when each app is compiled. 652 mode_t old_umask = umask(0); 653 LOG(INFO) << "Old umask: " << old_umask; 654 if (access(OTAPreoptService::kOTADataDirectory, R_OK) < 0) { 655 ALOGE("Could not access %s\n", OTAPreoptService::kOTADataDirectory); 656 return false; 657 } 658 return true; 659} 660 661static int log_callback(int type, const char *fmt, ...) { 662 va_list ap; 663 int priority; 664 665 switch (type) { 666 case SELINUX_WARNING: 667 priority = ANDROID_LOG_WARN; 668 break; 669 case SELINUX_INFO: 670 priority = ANDROID_LOG_INFO; 671 break; 672 default: 673 priority = ANDROID_LOG_ERROR; 674 break; 675 } 676 va_start(ap, fmt); 677 LOG_PRI_VA(priority, "SELinux", fmt, ap); 678 va_end(ap); 679 return 0; 680} 681 682static int otapreopt_main(const int argc, char *argv[]) { 683 int selinux_enabled = (is_selinux_enabled() > 0); 684 685 setenv("ANDROID_LOG_TAGS", "*:v", 1); 686 android::base::InitLogging(argv); 687 688 ALOGI("otapreopt firing up\n"); 689 690 if (argc < 2) { 691 ALOGE("Expecting parameters"); 692 exit(1); 693 } 694 695 union selinux_callback cb; 696 cb.func_log = log_callback; 697 selinux_set_callback(SELINUX_CB_LOG, cb); 698 699 if (!initialize_globals()) { 700 ALOGE("Could not initialize globals; exiting.\n"); 701 exit(1); 702 } 703 704 if (!initialize_directories()) { 705 ALOGE("Could not create directories; exiting.\n"); 706 exit(1); 707 } 708 709 if (selinux_enabled && selinux_status_open(true) < 0) { 710 ALOGE("Could not open selinux status; exiting.\n"); 711 exit(1); 712 } 713 714 int ret = android::installd::gOps.Main(argc, argv); 715 716 return ret; 717} 718 719} // namespace installd 720} // namespace android 721 722int main(const int argc, char *argv[]) { 723 return android::installd::otapreopt_main(argc, argv); 724} 725