blob: 868c409d0b7853465d01668f35cbec40b85bf18c [file] [log] [blame]
Tom Cherry618d3102018-01-19 14:25:48 -08001/*
2 * Copyright (C) 2018 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
Tom Cherry7bfea3d2018-11-06 14:12:05 -080017#include "builtins.h"
18#include "first_stage_init.h"
Tom Cherry618d3102018-01-19 14:25:48 -080019#include "init.h"
Tom Cherry7bfea3d2018-11-06 14:12:05 -080020#include "selinux.h"
21#include "subcontext.h"
22#include "ueventd.h"
23
24#include <android-base/logging.h>
25
26#if __has_feature(address_sanitizer)
27#include <sanitizer/asan_interface.h>
28#endif
29
30#if __has_feature(address_sanitizer)
31// Load asan.options if it exists since these are not yet in the environment.
32// Always ensure detect_container_overflow=0 as there are false positives with this check.
33// Always ensure abort_on_error=1 to ensure we reboot to bootloader for development builds.
34extern "C" const char* __asan_default_options() {
35 return "include_if_exists=/system/asan.options:detect_container_overflow=0:abort_on_error=1";
36}
37
38__attribute__((no_sanitize("address", "memory", "thread", "undefined"))) extern "C" void
39__sanitizer_report_error_summary(const char* summary) {
40 LOG(ERROR) << "Init (error summary): " << summary;
41}
42
43__attribute__((no_sanitize("address", "memory", "thread", "undefined"))) static void
44AsanReportCallback(const char* str) {
45 LOG(ERROR) << "Init: " << str;
46}
47#endif
48
49using namespace android::init;
Tom Cherry618d3102018-01-19 14:25:48 -080050
51int main(int argc, char** argv) {
Tom Cherry7bfea3d2018-11-06 14:12:05 -080052#if __has_feature(address_sanitizer)
53 __asan_set_error_report_callback(AsanReportCallback);
54#endif
55
56 if (!strcmp(basename(argv[0]), "ueventd")) {
57 return ueventd_main(argc, argv);
58 }
59
60 if (argc < 2) {
61 return FirstStageMain(argc, argv);
62 }
63
64 if (!strcmp(argv[1], "subcontext")) {
65 android::base::InitLogging(argv, &android::base::KernelLogger);
66 const BuiltinFunctionMap function_map;
67
68 return SubcontextMain(argc, argv, &function_map);
69 }
70
71 if (!strcmp(argv[1], "selinux_setup")) {
72 return SetupSelinux(argv);
73 }
74
75 if (!strcmp(argv[1], "second_stage")) {
76 return SecondStageMain(argc, argv);
77 }
78
79 android::base::InitLogging(argv, &android::base::KernelLogger);
80
81 LOG(ERROR) << "Unknown argument passed to init '" << argv[1] << "'";
82 return 1;
Tom Cherry618d3102018-01-19 14:25:48 -080083}