blob: 6e7c245d3a238a1c654660332243ef12e789c76c [file] [log] [blame]
Andreas Gampe73dae112015-11-19 14:12:14 -08001/*
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 Gampe1842af32016-03-16 14:28:50 -070020#include <regex>
Andreas Gampe73dae112015-11-19 14:12:14 -080021#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 <cutils/fs.h>
34#include <cutils/log.h>
35#include <cutils/properties.h>
36#include <private/android_filesystem_config.h>
37
38#include <commands.h>
Andreas Gampe1842af32016-03-16 14:28:50 -070039#include <file_parsing.h>
Andreas Gampe73dae112015-11-19 14:12:14 -080040#include <globals.h>
41#include <installd_deps.h> // Need to fill in requirements of commands.
42#include <string_helpers.h>
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::StringPrintf;
55
56namespace android {
57namespace installd {
58
Andreas Gampe1842af32016-03-16 14:28:50 -070059static constexpr const char* kBootClassPathPropertyName = "BOOTCLASSPATH";
60static constexpr const char* kAndroidRootPathPropertyName = "ANDROID_ROOT";
Andreas Gampe73dae112015-11-19 14:12:14 -080061static constexpr const char* kOTARootDirectory = "/system-b";
62static constexpr size_t kISAIndex = 3;
63
64template<typename T>
65static constexpr T RoundDown(T x, typename std::decay<T>::type n) {
66 return DCHECK_CONSTEXPR(IsPowerOfTwo(n), , T(0))(x & -n);
67}
68
69template<typename T>
70static constexpr T RoundUp(T x, typename std::remove_reference<T>::type n) {
71 return RoundDown(x + n - 1, n);
72}
73
74class OTAPreoptService {
75 public:
76 static constexpr const char* kOTADataDirectory = "/data/ota";
77
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) {
90 if (!ReadSystemProperties()) {
91 LOG(ERROR)<< "Failed reading system properties.";
92 return 1;
93 }
94
95 if (!ReadEnvironment()) {
96 LOG(ERROR) << "Failed reading environment properties.";
97 return 2;
98 }
99
100 if (!ReadPackage(argc, argv)) {
101 LOG(ERROR) << "Failed reading command line file.";
102 return 3;
103 }
104
105 PrepareEnvironment();
106
107 if (!PrepareBootImage()) {
108 LOG(ERROR) << "Failed preparing boot image.";
109 return 4;
110 }
111
112 int dexopt_retcode = RunPreopt();
113
114 return dexopt_retcode;
115 }
116
117 int GetProperty(const char* key, char* value, const char* default_value) {
118 const std::string* prop_value = system_properties_.GetProperty(key);
119 if (prop_value == nullptr) {
120 if (default_value == nullptr) {
121 return 0;
122 }
123 // Copy in the default value.
124 strncpy(value, default_value, kPropertyValueMax - 1);
125 value[kPropertyValueMax - 1] = 0;
126 return strlen(default_value);// TODO: Need to truncate?
127 }
128 size_t size = std::min(kPropertyValueMax - 1, prop_value->length());
129 strncpy(value, prop_value->data(), size);
130 value[size] = 0;
131 return static_cast<int>(size);
132 }
133
134private:
135 bool ReadSystemProperties() {
Andreas Gampe1842af32016-03-16 14:28:50 -0700136 static constexpr const char* kPropertyFiles[] = {
137 "/default.prop", "/system/build.prop"
138 };
Andreas Gampe73dae112015-11-19 14:12:14 -0800139
Andreas Gampe1842af32016-03-16 14:28:50 -0700140 for (size_t i = 0; i < arraysize(kPropertyFiles); ++i) {
141 if (!system_properties_.Load(kPropertyFiles[i])) {
142 return false;
143 }
144 }
145
146 return true;
Andreas Gampe73dae112015-11-19 14:12:14 -0800147 }
148
149 bool ReadEnvironment() {
Andreas Gampe1842af32016-03-16 14:28:50 -0700150 // Parse the environment variables from init.environ.rc, which have the form
151 // export NAME VALUE
152 // For simplicity, don't respect string quotation. The values we are interested in can be
153 // encoded without them.
154 std::regex export_regex("\\s*export\\s+(\\S+)\\s+(\\S+)");
155 bool parse_result = ParseFile("/init.environ.rc", [&](const std::string& line) {
156 std::smatch export_match;
157 if (!std::regex_match(line, export_match, export_regex)) {
158 return true;
159 }
Andreas Gampe73dae112015-11-19 14:12:14 -0800160
Andreas Gampe1842af32016-03-16 14:28:50 -0700161 if (export_match.size() != 3) {
162 return true;
163 }
164
165 std::string name = export_match[1].str();
166 std::string value = export_match[2].str();
167
168 system_properties_.SetProperty(name, value);
169
170 return true;
171 });
172 if (!parse_result) {
Andreas Gampe73dae112015-11-19 14:12:14 -0800173 return false;
174 }
Andreas Gampe1842af32016-03-16 14:28:50 -0700175
176 // Check that we found important properties.
177 constexpr const char* kRequiredProperties[] = {
178 kBootClassPathPropertyName, kAndroidRootPathPropertyName
179 };
180 for (size_t i = 0; i < arraysize(kRequiredProperties); ++i) {
181 if (system_properties_.GetProperty(kRequiredProperties[i]) == nullptr) {
182 return false;
183 }
184 }
Andreas Gampe73dae112015-11-19 14:12:14 -0800185
186 return true;
187 }
188
189 bool ReadPackage(int argc ATTRIBUTE_UNUSED, char** argv) {
190 size_t index = 0;
Andreas Gampe548bdb92016-06-02 17:56:45 -0700191 static_assert(DEXOPT_PARAM_COUNT == ARRAY_SIZE(package_parameters_),
192 "Unexpected dexopt param count");
193 while (index < DEXOPT_PARAM_COUNT &&
Andreas Gampe73dae112015-11-19 14:12:14 -0800194 argv[index + 1] != nullptr) {
195 package_parameters_[index] = argv[index + 1];
196 index++;
197 }
Andreas Gampe548bdb92016-06-02 17:56:45 -0700198 if (index != ARRAY_SIZE(package_parameters_) || argv[index + 1] != nullptr) {
Andreas Gampe73dae112015-11-19 14:12:14 -0800199 LOG(ERROR) << "Wrong number of parameters";
200 return false;
201 }
202
203 return true;
204 }
205
206 void PrepareEnvironment() {
207 CHECK(system_properties_.GetProperty(kBootClassPathPropertyName) != nullptr);
208 const std::string& boot_cp =
209 *system_properties_.GetProperty(kBootClassPathPropertyName);
210 environ_.push_back(StringPrintf("BOOTCLASSPATH=%s", boot_cp.c_str()));
211 environ_.push_back(StringPrintf("ANDROID_DATA=%s", kOTADataDirectory));
212 CHECK(system_properties_.GetProperty(kAndroidRootPathPropertyName) != nullptr);
213 const std::string& android_root =
214 *system_properties_.GetProperty(kAndroidRootPathPropertyName);
215 environ_.push_back(StringPrintf("ANDROID_ROOT=%s", android_root.c_str()));
216
217 for (const std::string& e : environ_) {
218 putenv(const_cast<char*>(e.c_str()));
219 }
220 }
221
222 // Ensure that we have the right boot image. The first time any app is
223 // compiled, we'll try to generate it.
224 bool PrepareBootImage() {
225 if (package_parameters_[kISAIndex] == nullptr) {
226 LOG(ERROR) << "Instruction set missing.";
227 return false;
228 }
229 const char* isa = package_parameters_[kISAIndex];
230
231 // Check whether the file exists where expected.
232 std::string dalvik_cache = std::string(kOTADataDirectory) + "/" + DALVIK_CACHE;
233 std::string isa_path = dalvik_cache + "/" + isa;
234 std::string art_path = isa_path + "/system@framework@boot.art";
235 std::string oat_path = isa_path + "/system@framework@boot.oat";
236 if (access(art_path.c_str(), F_OK) == 0 &&
237 access(oat_path.c_str(), F_OK) == 0) {
238 // Files exist, assume everything is alright.
239 return true;
240 }
241
242 // Create the directories, if necessary.
243 if (access(dalvik_cache.c_str(), F_OK) != 0) {
244 if (mkdir(dalvik_cache.c_str(), 0711) != 0) {
245 PLOG(ERROR) << "Could not create dalvik-cache dir";
246 return false;
247 }
248 }
249 if (access(isa_path.c_str(), F_OK) != 0) {
250 if (mkdir(isa_path.c_str(), 0711) != 0) {
251 PLOG(ERROR) << "Could not create dalvik-cache isa dir";
252 return false;
253 }
254 }
255
Andreas Gampe5709b572016-02-12 17:42:59 -0800256 // Prepare to create.
Andreas Gampe73dae112015-11-19 14:12:14 -0800257 // TODO: Delete files, just for a blank slate.
258 const std::string& boot_cp = *system_properties_.GetProperty(kBootClassPathPropertyName);
259
Andreas Gampe9fb85b02016-03-16 10:09:29 -0700260 std::string preopted_boot_art_path = StringPrintf("/system/framework/%s/boot.art", isa);
Andreas Gampe5709b572016-02-12 17:42:59 -0800261 if (access(preopted_boot_art_path.c_str(), F_OK) == 0) {
262 return PatchoatBootImage(art_path, isa);
263 } else {
264 // No preopted boot image. Try to compile.
265 return Dex2oatBootImage(boot_cp, art_path, oat_path, isa);
266 }
267 }
268
269 bool PatchoatBootImage(const std::string& art_path, const char* isa) {
270 // This needs to be kept in sync with ART, see art/runtime/gc/space/image_space.cc.
271
272 std::vector<std::string> cmd;
Andreas Gampe9fb85b02016-03-16 10:09:29 -0700273 cmd.push_back("/system/bin/patchoat");
Andreas Gampe5709b572016-02-12 17:42:59 -0800274
275 cmd.push_back("--input-image-location=/system/framework/boot.art");
276 cmd.push_back(StringPrintf("--output-image-file=%s", art_path.c_str()));
277
278 cmd.push_back(StringPrintf("--instruction-set=%s", isa));
279
280 int32_t base_offset = ChooseRelocationOffsetDelta(ART_BASE_ADDRESS_MIN_DELTA,
281 ART_BASE_ADDRESS_MAX_DELTA);
Andreas Gampefebf0bf2016-02-29 18:04:17 -0800282 cmd.push_back(StringPrintf("--base-offset-delta=%d", base_offset));
Andreas Gampe5709b572016-02-12 17:42:59 -0800283
284 std::string error_msg;
285 bool result = Exec(cmd, &error_msg);
286 if (!result) {
287 LOG(ERROR) << "Could not generate boot image: " << error_msg;
288 }
289 return result;
290 }
291
292 bool Dex2oatBootImage(const std::string& boot_cp,
293 const std::string& art_path,
294 const std::string& oat_path,
295 const char* isa) {
Andreas Gampe73dae112015-11-19 14:12:14 -0800296 // This needs to be kept in sync with ART, see art/runtime/gc/space/image_space.cc.
297 std::vector<std::string> cmd;
Andreas Gampe9fb85b02016-03-16 10:09:29 -0700298 cmd.push_back("/system/bin/dex2oat");
Andreas Gampe73dae112015-11-19 14:12:14 -0800299 cmd.push_back(StringPrintf("--image=%s", art_path.c_str()));
300 for (const std::string& boot_part : Split(boot_cp, ':')) {
301 cmd.push_back(StringPrintf("--dex-file=%s", boot_part.c_str()));
302 }
303 cmd.push_back(StringPrintf("--oat-file=%s", oat_path.c_str()));
304
305 int32_t base_offset = ChooseRelocationOffsetDelta(ART_BASE_ADDRESS_MIN_DELTA,
306 ART_BASE_ADDRESS_MAX_DELTA);
307 cmd.push_back(StringPrintf("--base=0x%x", ART_BASE_ADDRESS + base_offset));
308
309 cmd.push_back(StringPrintf("--instruction-set=%s", isa));
310
311 // These things are pushed by AndroidRuntime, see frameworks/base/core/jni/AndroidRuntime.cpp.
312 AddCompilerOptionFromSystemProperty("dalvik.vm.image-dex2oat-Xms",
313 "-Xms",
314 true,
315 cmd);
316 AddCompilerOptionFromSystemProperty("dalvik.vm.image-dex2oat-Xmx",
317 "-Xmx",
318 true,
319 cmd);
320 AddCompilerOptionFromSystemProperty("dalvik.vm.image-dex2oat-filter",
321 "--compiler-filter=",
322 false,
323 cmd);
Andreas Gampe9fb85b02016-03-16 10:09:29 -0700324 cmd.push_back("--image-classes=/system/etc/preloaded-classes");
Andreas Gampe73dae112015-11-19 14:12:14 -0800325 // TODO: Compiled-classes.
326 const std::string* extra_opts =
327 system_properties_.GetProperty("dalvik.vm.image-dex2oat-flags");
328 if (extra_opts != nullptr) {
329 std::vector<std::string> extra_vals = Split(*extra_opts, ' ');
330 cmd.insert(cmd.end(), extra_vals.begin(), extra_vals.end());
331 }
332 // TODO: Should we lower this? It's usually set close to max, because
333 // normally there's not much else going on at boot.
334 AddCompilerOptionFromSystemProperty("dalvik.vm.image-dex2oat-threads",
335 "-j",
336 false,
337 cmd);
338 AddCompilerOptionFromSystemProperty(
339 StringPrintf("dalvik.vm.isa.%s.variant", isa).c_str(),
340 "--instruction-set-variant=",
341 false,
342 cmd);
343 AddCompilerOptionFromSystemProperty(
344 StringPrintf("dalvik.vm.isa.%s.features", isa).c_str(),
345 "--instruction-set-features=",
346 false,
347 cmd);
348
349 std::string error_msg;
350 bool result = Exec(cmd, &error_msg);
351 if (!result) {
352 LOG(ERROR) << "Could not generate boot image: " << error_msg;
353 }
354 return result;
355 }
356
357 static const char* ParseNull(const char* arg) {
358 return (strcmp(arg, "!") == 0) ? nullptr : arg;
359 }
360
361 int RunPreopt() {
Andreas Gampe548bdb92016-06-02 17:56:45 -0700362 return dexopt(package_parameters_);
Andreas Gampe73dae112015-11-19 14:12:14 -0800363 }
364
365 ////////////////////////////////////
366 // Helpers, mostly taken from ART //
367 ////////////////////////////////////
368
369 // Wrapper on fork/execv to run a command in a subprocess.
370 bool Exec(const std::vector<std::string>& arg_vector, std::string* error_msg) {
371 const std::string command_line(Join(arg_vector, ' '));
372
373 CHECK_GE(arg_vector.size(), 1U) << command_line;
374
375 // Convert the args to char pointers.
376 const char* program = arg_vector[0].c_str();
377 std::vector<char*> args;
378 for (size_t i = 0; i < arg_vector.size(); ++i) {
379 const std::string& arg = arg_vector[i];
380 char* arg_str = const_cast<char*>(arg.c_str());
381 CHECK(arg_str != nullptr) << i;
382 args.push_back(arg_str);
383 }
384 args.push_back(nullptr);
385
386 // Fork and exec.
387 pid_t pid = fork();
388 if (pid == 0) {
389 // No allocation allowed between fork and exec.
390
391 // Change process groups, so we don't get reaped by ProcessManager.
392 setpgid(0, 0);
393
394 execv(program, &args[0]);
395
396 PLOG(ERROR) << "Failed to execv(" << command_line << ")";
397 // _exit to avoid atexit handlers in child.
398 _exit(1);
399 } else {
400 if (pid == -1) {
401 *error_msg = StringPrintf("Failed to execv(%s) because fork failed: %s",
402 command_line.c_str(), strerror(errno));
403 return false;
404 }
405
406 // wait for subprocess to finish
407 int status;
408 pid_t got_pid = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
409 if (got_pid != pid) {
410 *error_msg = StringPrintf("Failed after fork for execv(%s) because waitpid failed: "
411 "wanted %d, got %d: %s",
412 command_line.c_str(), pid, got_pid, strerror(errno));
413 return false;
414 }
415 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
416 *error_msg = StringPrintf("Failed execv(%s) because non-0 exit status",
417 command_line.c_str());
418 return false;
419 }
420 }
421 return true;
422 }
423
424 // Choose a random relocation offset. Taken from art/runtime/gc/image_space.cc.
425 static int32_t ChooseRelocationOffsetDelta(int32_t min_delta, int32_t max_delta) {
426 constexpr size_t kPageSize = PAGE_SIZE;
427 CHECK_EQ(min_delta % kPageSize, 0u);
428 CHECK_EQ(max_delta % kPageSize, 0u);
429 CHECK_LT(min_delta, max_delta);
430
431 std::default_random_engine generator;
432 generator.seed(GetSeed());
433 std::uniform_int_distribution<int32_t> distribution(min_delta, max_delta);
434 int32_t r = distribution(generator);
435 if (r % 2 == 0) {
436 r = RoundUp(r, kPageSize);
437 } else {
438 r = RoundDown(r, kPageSize);
439 }
440 CHECK_LE(min_delta, r);
441 CHECK_GE(max_delta, r);
442 CHECK_EQ(r % kPageSize, 0u);
443 return r;
444 }
445
446 static uint64_t GetSeed() {
447#ifdef __BIONIC__
448 // Bionic exposes arc4random, use it.
449 uint64_t random_data;
450 arc4random_buf(&random_data, sizeof(random_data));
451 return random_data;
452#else
453#error "This is only supposed to run with bionic. Otherwise, implement..."
454#endif
455 }
456
457 void AddCompilerOptionFromSystemProperty(const char* system_property,
458 const char* prefix,
459 bool runtime,
460 std::vector<std::string>& out) {
461 const std::string* value =
462 system_properties_.GetProperty(system_property);
463 if (value != nullptr) {
464 if (runtime) {
465 out.push_back("--runtime-arg");
466 }
467 if (prefix != nullptr) {
468 out.push_back(StringPrintf("%s%s", prefix, value->c_str()));
469 } else {
470 out.push_back(*value);
471 }
472 }
473 }
474
Andreas Gampe73dae112015-11-19 14:12:14 -0800475 // Stores the system properties read out of the B partition. We need to use these properties
476 // to compile, instead of the A properties we could get from init/get_property.
477 SystemProperties system_properties_;
478
Andreas Gampe548bdb92016-06-02 17:56:45 -0700479 const char* package_parameters_[DEXOPT_PARAM_COUNT];
Andreas Gampe73dae112015-11-19 14:12:14 -0800480
481 // Store environment values we need to set.
482 std::vector<std::string> environ_;
483};
484
485OTAPreoptService gOps;
486
487////////////////////////
488// Plug-in functions. //
489////////////////////////
490
491int get_property(const char *key, char *value, const char *default_value) {
492 // TODO: Replace with system-properties map.
493 return gOps.GetProperty(key, value, default_value);
494}
495
496// Compute the output path of
497bool calculate_oat_file_path(char path[PKG_PATH_MAX], const char *oat_dir,
498 const char *apk_path,
499 const char *instruction_set) {
500 // TODO: Insert B directory.
Dan Austin9c8f93a2016-06-03 16:15:54 -0700501 const char *file_name_start;
502 const char *file_name_end;
Andreas Gampe73dae112015-11-19 14:12:14 -0800503
504 file_name_start = strrchr(apk_path, '/');
505 if (file_name_start == nullptr) {
506 ALOGE("apk_path '%s' has no '/'s in it\n", apk_path);
507 return false;
508 }
509 file_name_end = strrchr(file_name_start, '.');
510 if (file_name_end == nullptr) {
511 ALOGE("apk_path '%s' has no extension\n", apk_path);
512 return false;
513 }
514
515 // Calculate file_name
516 file_name_start++; // Move past '/', is valid as file_name_end is valid.
517 size_t file_name_len = file_name_end - file_name_start;
518 std::string file_name(file_name_start, file_name_len);
519
520 // <apk_parent_dir>/oat/<isa>/<file_name>.odex.b
521 snprintf(path, PKG_PATH_MAX, "%s/%s/%s.odex.b", oat_dir, instruction_set,
522 file_name.c_str());
523 return true;
524}
525
526/*
527 * Computes the odex file for the given apk_path and instruction_set.
528 * /system/framework/whatever.jar -> /system/framework/oat/<isa>/whatever.odex
529 *
530 * Returns false if it failed to determine the odex file path.
531 */
532bool calculate_odex_file_path(char path[PKG_PATH_MAX], const char *apk_path,
533 const char *instruction_set) {
534 if (StringPrintf("%soat/%s/odex.b", apk_path, instruction_set).length() + 1 > PKG_PATH_MAX) {
535 ALOGE("apk_path '%s' may be too long to form odex file path.\n", apk_path);
536 return false;
537 }
538
539 const char *path_end = strrchr(apk_path, '/');
540 if (path_end == nullptr) {
541 ALOGE("apk_path '%s' has no '/'s in it?!\n", apk_path);
542 return false;
543 }
544 std::string path_component(apk_path, path_end - apk_path);
545
546 const char *name_begin = path_end + 1;
547 const char *extension_start = strrchr(name_begin, '.');
548 if (extension_start == nullptr) {
549 ALOGE("apk_path '%s' has no extension.\n", apk_path);
550 return false;
551 }
552 std::string name_component(name_begin, extension_start - name_begin);
553
554 std::string new_path = StringPrintf("%s/oat/%s/%s.odex.b",
555 path_component.c_str(),
556 instruction_set,
557 name_component.c_str());
558 CHECK_LT(new_path.length(), PKG_PATH_MAX);
559 strcpy(path, new_path.c_str());
560 return true;
561}
562
563bool create_cache_path(char path[PKG_PATH_MAX],
564 const char *src,
565 const char *instruction_set) {
566 size_t srclen = strlen(src);
567
568 /* demand that we are an absolute path */
569 if ((src == 0) || (src[0] != '/') || strstr(src,"..")) {
570 return false;
571 }
572
573 if (srclen > PKG_PATH_MAX) { // XXX: PKG_NAME_MAX?
574 return false;
575 }
576
577 std::string from_src = std::string(src + 1);
578 std::replace(from_src.begin(), from_src.end(), '/', '@');
579
580 std::string assembled_path = StringPrintf("%s/%s/%s/%s%s",
581 OTAPreoptService::kOTADataDirectory,
582 DALVIK_CACHE,
583 instruction_set,
584 from_src.c_str(),
585 DALVIK_CACHE_POSTFIX2);
586
587 if (assembled_path.length() + 1 > PKG_PATH_MAX) {
588 return false;
589 }
590 strcpy(path, assembled_path.c_str());
591
592 return true;
593}
594
595bool initialize_globals() {
596 const char* data_path = getenv("ANDROID_DATA");
597 if (data_path == nullptr) {
598 ALOGE("Could not find ANDROID_DATA");
599 return false;
600 }
601 return init_globals_from_data_and_root(data_path, kOTARootDirectory);
602}
603
604static bool initialize_directories() {
605 // This is different from the normal installd. We only do the base
606 // directory, the rest will be created on demand when each app is compiled.
607 mode_t old_umask = umask(0);
608 LOG(INFO) << "Old umask: " << old_umask;
609 if (access(OTAPreoptService::kOTADataDirectory, R_OK) < 0) {
610 ALOGE("Could not access %s\n", OTAPreoptService::kOTADataDirectory);
611 return false;
612 }
613 return true;
614}
615
616static int log_callback(int type, const char *fmt, ...) {
617 va_list ap;
618 int priority;
619
620 switch (type) {
621 case SELINUX_WARNING:
622 priority = ANDROID_LOG_WARN;
623 break;
624 case SELINUX_INFO:
625 priority = ANDROID_LOG_INFO;
626 break;
627 default:
628 priority = ANDROID_LOG_ERROR;
629 break;
630 }
631 va_start(ap, fmt);
632 LOG_PRI_VA(priority, "SELinux", fmt, ap);
633 va_end(ap);
634 return 0;
635}
636
637static int otapreopt_main(const int argc, char *argv[]) {
638 int selinux_enabled = (is_selinux_enabled() > 0);
639
640 setenv("ANDROID_LOG_TAGS", "*:v", 1);
641 android::base::InitLogging(argv);
642
643 ALOGI("otapreopt firing up\n");
644
645 if (argc < 2) {
646 ALOGE("Expecting parameters");
647 exit(1);
648 }
649
650 union selinux_callback cb;
651 cb.func_log = log_callback;
652 selinux_set_callback(SELINUX_CB_LOG, cb);
653
654 if (!initialize_globals()) {
655 ALOGE("Could not initialize globals; exiting.\n");
656 exit(1);
657 }
658
659 if (!initialize_directories()) {
660 ALOGE("Could not create directories; exiting.\n");
661 exit(1);
662 }
663
664 if (selinux_enabled && selinux_status_open(true) < 0) {
665 ALOGE("Could not open selinux status; exiting.\n");
666 exit(1);
667 }
668
669 int ret = android::installd::gOps.Main(argc, argv);
670
671 return ret;
672}
673
674} // namespace installd
675} // namespace android
676
677int main(const int argc, char *argv[]) {
678 return android::installd::otapreopt_main(argc, argv);
679}