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 <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>
39 #include <file_parsing.h>
40 #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 
54 using android::base::StringPrintf;
55 
56 namespace android {
57 namespace installd {
58 
59 static constexpr const char* kBootClassPathPropertyName = "BOOTCLASSPATH";
60 static constexpr const char* kAndroidRootPathPropertyName = "ANDROID_ROOT";
61 static constexpr const char* kOTARootDirectory = "/system-b";
62 static constexpr size_t kISAIndex = 3;
63 
64 template<typename T>
RoundDown(T x,typename std::decay<T>::type n)65 static constexpr T RoundDown(T x, typename std::decay<T>::type n) {
66     return DCHECK_CONSTEXPR(IsPowerOfTwo(n), , T(0))(x & -n);
67 }
68 
69 template<typename T>
RoundUp(T x,typename std::remove_reference<T>::type n)70 static constexpr T RoundUp(T x, typename std::remove_reference<T>::type n) {
71     return RoundDown(x + n - 1, n);
72 }
73 
74 class 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.
Main(int argc,char ** argv)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 
GetProperty(const char * key,char * value,const char * default_value)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 
134 private:
ReadSystemProperties()135     bool ReadSystemProperties() {
136         static constexpr const char* kPropertyFiles[] = {
137                 "/default.prop", "/system/build.prop"
138         };
139 
140         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;
147     }
148 
ReadEnvironment()149     bool ReadEnvironment() {
150         // 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             }
160 
161             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) {
173             return false;
174         }
175 
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         }
185 
186         return true;
187     }
188 
ReadPackage(int argc ATTRIBUTE_UNUSED,char ** argv)189     bool ReadPackage(int argc ATTRIBUTE_UNUSED, char** argv) {
190         size_t index = 0;
191         while (index < ARRAY_SIZE(package_parameters_) &&
192                 argv[index + 1] != nullptr) {
193             package_parameters_[index] = argv[index + 1];
194             index++;
195         }
196         if (index != ARRAY_SIZE(package_parameters_)) {
197             LOG(ERROR) << "Wrong number of parameters";
198             return false;
199         }
200 
201         return true;
202     }
203 
PrepareEnvironment()204     void PrepareEnvironment() {
205         CHECK(system_properties_.GetProperty(kBootClassPathPropertyName) != nullptr);
206         const std::string& boot_cp =
207                 *system_properties_.GetProperty(kBootClassPathPropertyName);
208         environ_.push_back(StringPrintf("BOOTCLASSPATH=%s", boot_cp.c_str()));
209         environ_.push_back(StringPrintf("ANDROID_DATA=%s", kOTADataDirectory));
210         CHECK(system_properties_.GetProperty(kAndroidRootPathPropertyName) != nullptr);
211         const std::string& android_root =
212                 *system_properties_.GetProperty(kAndroidRootPathPropertyName);
213         environ_.push_back(StringPrintf("ANDROID_ROOT=%s", android_root.c_str()));
214 
215         for (const std::string& e : environ_) {
216             putenv(const_cast<char*>(e.c_str()));
217         }
218     }
219 
220     // Ensure that we have the right boot image. The first time any app is
221     // compiled, we'll try to generate it.
PrepareBootImage()222     bool PrepareBootImage() {
223         if (package_parameters_[kISAIndex] == nullptr) {
224             LOG(ERROR) << "Instruction set missing.";
225             return false;
226         }
227         const char* isa = package_parameters_[kISAIndex];
228 
229         // Check whether the file exists where expected.
230         std::string dalvik_cache = std::string(kOTADataDirectory) + "/" + DALVIK_CACHE;
231         std::string isa_path = dalvik_cache + "/" + isa;
232         std::string art_path = isa_path + "/system@framework@boot.art";
233         std::string oat_path = isa_path + "/system@framework@boot.oat";
234         if (access(art_path.c_str(), F_OK) == 0 &&
235                 access(oat_path.c_str(), F_OK) == 0) {
236             // Files exist, assume everything is alright.
237             return true;
238         }
239 
240         // Create the directories, if necessary.
241         if (access(dalvik_cache.c_str(), F_OK) != 0) {
242             if (mkdir(dalvik_cache.c_str(), 0711) != 0) {
243                 PLOG(ERROR) << "Could not create dalvik-cache dir";
244                 return false;
245             }
246         }
247         if (access(isa_path.c_str(), F_OK) != 0) {
248             if (mkdir(isa_path.c_str(), 0711) != 0) {
249                 PLOG(ERROR) << "Could not create dalvik-cache isa dir";
250                 return false;
251             }
252         }
253 
254         // Prepare to create.
255         // TODO: Delete files, just for a blank slate.
256         const std::string& boot_cp = *system_properties_.GetProperty(kBootClassPathPropertyName);
257 
258         std::string preopted_boot_art_path = StringPrintf("/system/framework/%s/boot.art", isa);
259         if (access(preopted_boot_art_path.c_str(), F_OK) == 0) {
260           return PatchoatBootImage(art_path, isa);
261         } else {
262           // No preopted boot image. Try to compile.
263           return Dex2oatBootImage(boot_cp, art_path, oat_path, isa);
264         }
265     }
266 
PatchoatBootImage(const std::string & art_path,const char * isa)267     bool PatchoatBootImage(const std::string& art_path, const char* isa) {
268         // This needs to be kept in sync with ART, see art/runtime/gc/space/image_space.cc.
269 
270         std::vector<std::string> cmd;
271         cmd.push_back("/system/bin/patchoat");
272 
273         cmd.push_back("--input-image-location=/system/framework/boot.art");
274         cmd.push_back(StringPrintf("--output-image-file=%s", art_path.c_str()));
275 
276         cmd.push_back(StringPrintf("--instruction-set=%s", isa));
277 
278         int32_t base_offset = ChooseRelocationOffsetDelta(ART_BASE_ADDRESS_MIN_DELTA,
279                                                           ART_BASE_ADDRESS_MAX_DELTA);
280         cmd.push_back(StringPrintf("--base-offset-delta=%d", base_offset));
281 
282         std::string error_msg;
283         bool result = Exec(cmd, &error_msg);
284         if (!result) {
285             LOG(ERROR) << "Could not generate boot image: " << error_msg;
286         }
287         return result;
288     }
289 
Dex2oatBootImage(const std::string & boot_cp,const std::string & art_path,const std::string & oat_path,const char * isa)290     bool Dex2oatBootImage(const std::string& boot_cp,
291                           const std::string& art_path,
292                           const std::string& oat_path,
293                           const char* isa) {
294         // This needs to be kept in sync with ART, see art/runtime/gc/space/image_space.cc.
295         std::vector<std::string> cmd;
296         cmd.push_back("/system/bin/dex2oat");
297         cmd.push_back(StringPrintf("--image=%s", art_path.c_str()));
298         for (const std::string& boot_part : Split(boot_cp, ':')) {
299             cmd.push_back(StringPrintf("--dex-file=%s", boot_part.c_str()));
300         }
301         cmd.push_back(StringPrintf("--oat-file=%s", oat_path.c_str()));
302 
303         int32_t base_offset = ChooseRelocationOffsetDelta(ART_BASE_ADDRESS_MIN_DELTA,
304                 ART_BASE_ADDRESS_MAX_DELTA);
305         cmd.push_back(StringPrintf("--base=0x%x", ART_BASE_ADDRESS + base_offset));
306 
307         cmd.push_back(StringPrintf("--instruction-set=%s", isa));
308 
309         // These things are pushed by AndroidRuntime, see frameworks/base/core/jni/AndroidRuntime.cpp.
310         AddCompilerOptionFromSystemProperty("dalvik.vm.image-dex2oat-Xms",
311                 "-Xms",
312                 true,
313                 cmd);
314         AddCompilerOptionFromSystemProperty("dalvik.vm.image-dex2oat-Xmx",
315                 "-Xmx",
316                 true,
317                 cmd);
318         AddCompilerOptionFromSystemProperty("dalvik.vm.image-dex2oat-filter",
319                 "--compiler-filter=",
320                 false,
321                 cmd);
322         cmd.push_back("--image-classes=/system/etc/preloaded-classes");
323         // TODO: Compiled-classes.
324         const std::string* extra_opts =
325                 system_properties_.GetProperty("dalvik.vm.image-dex2oat-flags");
326         if (extra_opts != nullptr) {
327             std::vector<std::string> extra_vals = Split(*extra_opts, ' ');
328             cmd.insert(cmd.end(), extra_vals.begin(), extra_vals.end());
329         }
330         // TODO: Should we lower this? It's usually set close to max, because
331         //       normally there's not much else going on at boot.
332         AddCompilerOptionFromSystemProperty("dalvik.vm.image-dex2oat-threads",
333                 "-j",
334                 false,
335                 cmd);
336         AddCompilerOptionFromSystemProperty(
337                 StringPrintf("dalvik.vm.isa.%s.variant", isa).c_str(),
338                 "--instruction-set-variant=",
339                 false,
340                 cmd);
341         AddCompilerOptionFromSystemProperty(
342                 StringPrintf("dalvik.vm.isa.%s.features", isa).c_str(),
343                 "--instruction-set-features=",
344                 false,
345                 cmd);
346 
347         std::string error_msg;
348         bool result = Exec(cmd, &error_msg);
349         if (!result) {
350             LOG(ERROR) << "Could not generate boot image: " << error_msg;
351         }
352         return result;
353     }
354 
ParseNull(const char * arg)355     static const char* ParseNull(const char* arg) {
356         return (strcmp(arg, "!") == 0) ? nullptr : arg;
357     }
358 
RunPreopt()359     int RunPreopt() {
360         int ret = dexopt(package_parameters_[0],          // apk_path
361                 atoi(package_parameters_[1]),             // uid
362                 package_parameters_[2],                   // pkgname
363                 package_parameters_[3],                   // instruction_set
364                 atoi(package_parameters_[4]),             // dexopt_needed
365                 package_parameters_[5],                   // oat_dir
366                 atoi(package_parameters_[6]),             // dexopt_flags
367                 package_parameters_[7],                   // compiler_filter
368                 ParseNull(package_parameters_[8]),        // volume_uuid
369                 ParseNull(package_parameters_[9]));       // shared_libraries
370         return ret;
371     }
372 
373     ////////////////////////////////////
374     // Helpers, mostly taken from ART //
375     ////////////////////////////////////
376 
377     // Wrapper on fork/execv to run a command in a subprocess.
Exec(const std::vector<std::string> & arg_vector,std::string * error_msg)378     bool Exec(const std::vector<std::string>& arg_vector, std::string* error_msg) {
379         const std::string command_line(Join(arg_vector, ' '));
380 
381         CHECK_GE(arg_vector.size(), 1U) << command_line;
382 
383         // Convert the args to char pointers.
384         const char* program = arg_vector[0].c_str();
385         std::vector<char*> args;
386         for (size_t i = 0; i < arg_vector.size(); ++i) {
387             const std::string& arg = arg_vector[i];
388             char* arg_str = const_cast<char*>(arg.c_str());
389             CHECK(arg_str != nullptr) << i;
390             args.push_back(arg_str);
391         }
392         args.push_back(nullptr);
393 
394         // Fork and exec.
395         pid_t pid = fork();
396         if (pid == 0) {
397             // No allocation allowed between fork and exec.
398 
399             // Change process groups, so we don't get reaped by ProcessManager.
400             setpgid(0, 0);
401 
402             execv(program, &args[0]);
403 
404             PLOG(ERROR) << "Failed to execv(" << command_line << ")";
405             // _exit to avoid atexit handlers in child.
406             _exit(1);
407         } else {
408             if (pid == -1) {
409                 *error_msg = StringPrintf("Failed to execv(%s) because fork failed: %s",
410                         command_line.c_str(), strerror(errno));
411                 return false;
412             }
413 
414             // wait for subprocess to finish
415             int status;
416             pid_t got_pid = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
417             if (got_pid != pid) {
418                 *error_msg = StringPrintf("Failed after fork for execv(%s) because waitpid failed: "
419                         "wanted %d, got %d: %s",
420                         command_line.c_str(), pid, got_pid, strerror(errno));
421                 return false;
422             }
423             if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
424                 *error_msg = StringPrintf("Failed execv(%s) because non-0 exit status",
425                         command_line.c_str());
426                 return false;
427             }
428         }
429         return true;
430     }
431 
432     // Choose a random relocation offset. Taken from art/runtime/gc/image_space.cc.
ChooseRelocationOffsetDelta(int32_t min_delta,int32_t max_delta)433     static int32_t ChooseRelocationOffsetDelta(int32_t min_delta, int32_t max_delta) {
434         constexpr size_t kPageSize = PAGE_SIZE;
435         CHECK_EQ(min_delta % kPageSize, 0u);
436         CHECK_EQ(max_delta % kPageSize, 0u);
437         CHECK_LT(min_delta, max_delta);
438 
439         std::default_random_engine generator;
440         generator.seed(GetSeed());
441         std::uniform_int_distribution<int32_t> distribution(min_delta, max_delta);
442         int32_t r = distribution(generator);
443         if (r % 2 == 0) {
444             r = RoundUp(r, kPageSize);
445         } else {
446             r = RoundDown(r, kPageSize);
447         }
448         CHECK_LE(min_delta, r);
449         CHECK_GE(max_delta, r);
450         CHECK_EQ(r % kPageSize, 0u);
451         return r;
452     }
453 
GetSeed()454     static uint64_t GetSeed() {
455 #ifdef __BIONIC__
456         // Bionic exposes arc4random, use it.
457         uint64_t random_data;
458         arc4random_buf(&random_data, sizeof(random_data));
459         return random_data;
460 #else
461 #error "This is only supposed to run with bionic. Otherwise, implement..."
462 #endif
463     }
464 
AddCompilerOptionFromSystemProperty(const char * system_property,const char * prefix,bool runtime,std::vector<std::string> & out)465     void AddCompilerOptionFromSystemProperty(const char* system_property,
466             const char* prefix,
467             bool runtime,
468             std::vector<std::string>& out) {
469         const std::string* value =
470         system_properties_.GetProperty(system_property);
471         if (value != nullptr) {
472             if (runtime) {
473                 out.push_back("--runtime-arg");
474             }
475             if (prefix != nullptr) {
476                 out.push_back(StringPrintf("%s%s", prefix, value->c_str()));
477             } else {
478                 out.push_back(*value);
479             }
480         }
481     }
482 
483     // Stores the system properties read out of the B partition. We need to use these properties
484     // to compile, instead of the A properties we could get from init/get_property.
485     SystemProperties system_properties_;
486 
487     const char* package_parameters_[10];
488 
489     // Store environment values we need to set.
490     std::vector<std::string> environ_;
491 };
492 
493 OTAPreoptService gOps;
494 
495 ////////////////////////
496 // Plug-in functions. //
497 ////////////////////////
498 
get_property(const char * key,char * value,const char * default_value)499 int get_property(const char *key, char *value, const char *default_value) {
500     // TODO: Replace with system-properties map.
501     return gOps.GetProperty(key, value, default_value);
502 }
503 
504 // Compute the output path of
calculate_oat_file_path(char path[PKG_PATH_MAX],const char * oat_dir,const char * apk_path,const char * instruction_set)505 bool calculate_oat_file_path(char path[PKG_PATH_MAX], const char *oat_dir,
506                              const char *apk_path,
507                              const char *instruction_set) {
508     // TODO: Insert B directory.
509     char *file_name_start;
510     char *file_name_end;
511 
512     file_name_start = strrchr(apk_path, '/');
513     if (file_name_start == nullptr) {
514         ALOGE("apk_path '%s' has no '/'s in it\n", apk_path);
515         return false;
516     }
517     file_name_end = strrchr(file_name_start, '.');
518     if (file_name_end == nullptr) {
519         ALOGE("apk_path '%s' has no extension\n", apk_path);
520         return false;
521     }
522 
523     // Calculate file_name
524     file_name_start++;  // Move past '/', is valid as file_name_end is valid.
525     size_t file_name_len = file_name_end - file_name_start;
526     std::string file_name(file_name_start, file_name_len);
527 
528     // <apk_parent_dir>/oat/<isa>/<file_name>.odex.b
529     snprintf(path, PKG_PATH_MAX, "%s/%s/%s.odex.b", oat_dir, instruction_set,
530              file_name.c_str());
531     return true;
532 }
533 
534 /*
535  * Computes the odex file for the given apk_path and instruction_set.
536  * /system/framework/whatever.jar -> /system/framework/oat/<isa>/whatever.odex
537  *
538  * Returns false if it failed to determine the odex file path.
539  */
calculate_odex_file_path(char path[PKG_PATH_MAX],const char * apk_path,const char * instruction_set)540 bool calculate_odex_file_path(char path[PKG_PATH_MAX], const char *apk_path,
541                               const char *instruction_set) {
542     if (StringPrintf("%soat/%s/odex.b", apk_path, instruction_set).length() + 1 > PKG_PATH_MAX) {
543         ALOGE("apk_path '%s' may be too long to form odex file path.\n", apk_path);
544         return false;
545     }
546 
547     const char *path_end = strrchr(apk_path, '/');
548     if (path_end == nullptr) {
549         ALOGE("apk_path '%s' has no '/'s in it?!\n", apk_path);
550         return false;
551     }
552     std::string path_component(apk_path, path_end - apk_path);
553 
554     const char *name_begin = path_end + 1;
555     const char *extension_start = strrchr(name_begin, '.');
556     if (extension_start == nullptr) {
557         ALOGE("apk_path '%s' has no extension.\n", apk_path);
558         return false;
559     }
560     std::string name_component(name_begin, extension_start - name_begin);
561 
562     std::string new_path = StringPrintf("%s/oat/%s/%s.odex.b",
563                                         path_component.c_str(),
564                                         instruction_set,
565                                         name_component.c_str());
566     CHECK_LT(new_path.length(), PKG_PATH_MAX);
567     strcpy(path, new_path.c_str());
568     return true;
569 }
570 
create_cache_path(char path[PKG_PATH_MAX],const char * src,const char * instruction_set)571 bool create_cache_path(char path[PKG_PATH_MAX],
572                        const char *src,
573                        const char *instruction_set) {
574     size_t srclen = strlen(src);
575 
576         /* demand that we are an absolute path */
577     if ((src == 0) || (src[0] != '/') || strstr(src,"..")) {
578         return false;
579     }
580 
581     if (srclen > PKG_PATH_MAX) {        // XXX: PKG_NAME_MAX?
582         return false;
583     }
584 
585     std::string from_src = std::string(src + 1);
586     std::replace(from_src.begin(), from_src.end(), '/', '@');
587 
588     std::string assembled_path = StringPrintf("%s/%s/%s/%s%s",
589                                               OTAPreoptService::kOTADataDirectory,
590                                               DALVIK_CACHE,
591                                               instruction_set,
592                                               from_src.c_str(),
593                                               DALVIK_CACHE_POSTFIX2);
594 
595     if (assembled_path.length() + 1 > PKG_PATH_MAX) {
596         return false;
597     }
598     strcpy(path, assembled_path.c_str());
599 
600     return true;
601 }
602 
initialize_globals()603 bool initialize_globals() {
604     const char* data_path = getenv("ANDROID_DATA");
605     if (data_path == nullptr) {
606         ALOGE("Could not find ANDROID_DATA");
607         return false;
608     }
609     return init_globals_from_data_and_root(data_path, kOTARootDirectory);
610 }
611 
initialize_directories()612 static bool initialize_directories() {
613     // This is different from the normal installd. We only do the base
614     // directory, the rest will be created on demand when each app is compiled.
615     mode_t old_umask = umask(0);
616     LOG(INFO) << "Old umask: " << old_umask;
617     if (access(OTAPreoptService::kOTADataDirectory, R_OK) < 0) {
618         ALOGE("Could not access %s\n", OTAPreoptService::kOTADataDirectory);
619         return false;
620     }
621     return true;
622 }
623 
log_callback(int type,const char * fmt,...)624 static int log_callback(int type, const char *fmt, ...) {
625     va_list ap;
626     int priority;
627 
628     switch (type) {
629         case SELINUX_WARNING:
630             priority = ANDROID_LOG_WARN;
631             break;
632         case SELINUX_INFO:
633             priority = ANDROID_LOG_INFO;
634             break;
635         default:
636             priority = ANDROID_LOG_ERROR;
637             break;
638     }
639     va_start(ap, fmt);
640     LOG_PRI_VA(priority, "SELinux", fmt, ap);
641     va_end(ap);
642     return 0;
643 }
644 
otapreopt_main(const int argc,char * argv[])645 static int otapreopt_main(const int argc, char *argv[]) {
646     int selinux_enabled = (is_selinux_enabled() > 0);
647 
648     setenv("ANDROID_LOG_TAGS", "*:v", 1);
649     android::base::InitLogging(argv);
650 
651     ALOGI("otapreopt firing up\n");
652 
653     if (argc < 2) {
654         ALOGE("Expecting parameters");
655         exit(1);
656     }
657 
658     union selinux_callback cb;
659     cb.func_log = log_callback;
660     selinux_set_callback(SELINUX_CB_LOG, cb);
661 
662     if (!initialize_globals()) {
663         ALOGE("Could not initialize globals; exiting.\n");
664         exit(1);
665     }
666 
667     if (!initialize_directories()) {
668         ALOGE("Could not create directories; exiting.\n");
669         exit(1);
670     }
671 
672     if (selinux_enabled && selinux_status_open(true) < 0) {
673         ALOGE("Could not open selinux status; exiting.\n");
674         exit(1);
675     }
676 
677     int ret = android::installd::gOps.Main(argc, argv);
678 
679     return ret;
680 }
681 
682 }  // namespace installd
683 }  // namespace android
684 
main(const int argc,char * argv[])685 int main(const int argc, char *argv[]) {
686     return android::installd::otapreopt_main(argc, argv);
687 }
688