Tom Cherry | 618d310 | 2018-01-19 14:25:48 -0800 | [diff] [blame] | 1 | /* |
| 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 Cherry | 7bfea3d | 2018-11-06 14:12:05 -0800 | [diff] [blame] | 17 | #include "builtins.h" |
| 18 | #include "first_stage_init.h" |
Tom Cherry | 618d310 | 2018-01-19 14:25:48 -0800 | [diff] [blame] | 19 | #include "init.h" |
Tom Cherry | 7bfea3d | 2018-11-06 14:12:05 -0800 | [diff] [blame] | 20 | #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> |
David Anderson | 8138717 | 2021-07-07 18:46:11 -0700 | [diff] [blame] | 28 | #elif __has_feature(hwaddress_sanitizer) |
| 29 | #include <sanitizer/hwasan_interface.h> |
Tom Cherry | 7bfea3d | 2018-11-06 14:12:05 -0800 | [diff] [blame] | 30 | #endif |
| 31 | |
David Anderson | 8138717 | 2021-07-07 18:46:11 -0700 | [diff] [blame] | 32 | #if __has_feature(address_sanitizer) || __has_feature(hwaddress_sanitizer) |
Tom Cherry | 7bfea3d | 2018-11-06 14:12:05 -0800 | [diff] [blame] | 33 | // Load asan.options if it exists since these are not yet in the environment. |
| 34 | // Always ensure detect_container_overflow=0 as there are false positives with this check. |
| 35 | // Always ensure abort_on_error=1 to ensure we reboot to bootloader for development builds. |
| 36 | extern "C" const char* __asan_default_options() { |
| 37 | return "include_if_exists=/system/asan.options:detect_container_overflow=0:abort_on_error=1"; |
| 38 | } |
| 39 | |
| 40 | __attribute__((no_sanitize("address", "memory", "thread", "undefined"))) extern "C" void |
| 41 | __sanitizer_report_error_summary(const char* summary) { |
| 42 | LOG(ERROR) << "Init (error summary): " << summary; |
| 43 | } |
| 44 | |
| 45 | __attribute__((no_sanitize("address", "memory", "thread", "undefined"))) static void |
| 46 | AsanReportCallback(const char* str) { |
| 47 | LOG(ERROR) << "Init: " << str; |
| 48 | } |
| 49 | #endif |
| 50 | |
| 51 | using namespace android::init; |
Tom Cherry | 618d310 | 2018-01-19 14:25:48 -0800 | [diff] [blame] | 52 | |
| 53 | int main(int argc, char** argv) { |
Tom Cherry | 7bfea3d | 2018-11-06 14:12:05 -0800 | [diff] [blame] | 54 | #if __has_feature(address_sanitizer) |
| 55 | __asan_set_error_report_callback(AsanReportCallback); |
David Anderson | 8138717 | 2021-07-07 18:46:11 -0700 | [diff] [blame] | 56 | #elif __has_feature(hwaddress_sanitizer) |
| 57 | __hwasan_set_error_report_callback(AsanReportCallback); |
Tom Cherry | 7bfea3d | 2018-11-06 14:12:05 -0800 | [diff] [blame] | 58 | #endif |
Wei Wang | 30bbf7d | 2020-07-06 15:26:49 -0700 | [diff] [blame] | 59 | // Boost prio which will be restored later |
| 60 | setpriority(PRIO_PROCESS, 0, -20); |
Tom Cherry | 7bfea3d | 2018-11-06 14:12:05 -0800 | [diff] [blame] | 61 | if (!strcmp(basename(argv[0]), "ueventd")) { |
| 62 | return ueventd_main(argc, argv); |
| 63 | } |
| 64 | |
Haoyu Tang | f32bc7c | 2019-01-05 09:31:28 +0800 | [diff] [blame] | 65 | if (argc > 1) { |
| 66 | if (!strcmp(argv[1], "subcontext")) { |
| 67 | android::base::InitLogging(argv, &android::base::KernelLogger); |
Tom Cherry | d52a5b3 | 2019-07-22 16:05:36 -0700 | [diff] [blame] | 68 | const BuiltinFunctionMap& function_map = GetBuiltinFunctionMap(); |
Haoyu Tang | f32bc7c | 2019-01-05 09:31:28 +0800 | [diff] [blame] | 69 | |
| 70 | return SubcontextMain(argc, argv, &function_map); |
| 71 | } |
| 72 | |
| 73 | if (!strcmp(argv[1], "selinux_setup")) { |
| 74 | return SetupSelinux(argv); |
| 75 | } |
| 76 | |
| 77 | if (!strcmp(argv[1], "second_stage")) { |
| 78 | return SecondStageMain(argc, argv); |
| 79 | } |
Tom Cherry | 7bfea3d | 2018-11-06 14:12:05 -0800 | [diff] [blame] | 80 | } |
| 81 | |
Haoyu Tang | f32bc7c | 2019-01-05 09:31:28 +0800 | [diff] [blame] | 82 | return FirstStageMain(argc, argv); |
Tom Cherry | 618d310 | 2018-01-19 14:25:48 -0800 | [diff] [blame] | 83 | } |