| Tom Cherry | 44aceed | 2018-08-03 13:36:18 -0700 | [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 |  | 
|  | 17 | #include <sys/capability.h> | 
|  | 18 | #include <sys/reboot.h> | 
|  | 19 | #include <sys/syscall.h> | 
|  | 20 | #include <unistd.h> | 
|  | 21 |  | 
| Tom Cherry | 59656fb | 2019-05-28 10:19:44 -0700 | [diff] [blame] | 22 | #include <string> | 
|  | 23 |  | 
| Wei Wang | 5f181bc | 2019-08-27 16:35:35 -0700 | [diff] [blame] | 24 | #include <android-base/file.h> | 
|  | 25 | #include <android-base/logging.h> | 
|  | 26 | #include <android-base/properties.h> | 
|  | 27 | #include <android-base/strings.h> | 
|  | 28 | #include <backtrace/Backtrace.h> | 
|  | 29 | #include <cutils/android_reboot.h> | 
| Tom Cherry | 44aceed | 2018-08-03 13:36:18 -0700 | [diff] [blame] | 30 |  | 
|  | 31 | #include "capabilities.h" | 
|  | 32 |  | 
|  | 33 | namespace android { | 
|  | 34 | namespace init { | 
|  | 35 |  | 
| Tom Cherry | 75e13ba | 2019-05-21 13:53:05 -0700 | [diff] [blame] | 36 | static std::string init_fatal_reboot_target = "bootloader"; | 
|  | 37 |  | 
|  | 38 | void SetFatalRebootTarget() { | 
|  | 39 | std::string cmdline; | 
|  | 40 | android::base::ReadFileToString("/proc/cmdline", &cmdline); | 
|  | 41 | cmdline = android::base::Trim(cmdline); | 
|  | 42 |  | 
|  | 43 | const char kRebootTargetString[] = "androidboot.init_fatal_reboot_target="; | 
|  | 44 | auto start_pos = cmdline.find(kRebootTargetString); | 
|  | 45 | if (start_pos == std::string::npos) { | 
|  | 46 | return;  // We already default to bootloader if no setting is provided. | 
|  | 47 | } | 
|  | 48 | start_pos += sizeof(kRebootTargetString) - 1; | 
|  | 49 |  | 
|  | 50 | auto end_pos = cmdline.find(' ', start_pos); | 
|  | 51 | // if end_pos isn't found, then we've run off the end, but this is okay as this is the last | 
|  | 52 | // entry, and -1 is a valid size for string::substr(); | 
|  | 53 | auto size = end_pos == std::string::npos ? -1 : end_pos - start_pos; | 
|  | 54 | init_fatal_reboot_target = cmdline.substr(start_pos, size); | 
|  | 55 | } | 
|  | 56 |  | 
| Tom Cherry | 44aceed | 2018-08-03 13:36:18 -0700 | [diff] [blame] | 57 | bool IsRebootCapable() { | 
|  | 58 | if (!CAP_IS_SUPPORTED(CAP_SYS_BOOT)) { | 
|  | 59 | PLOG(WARNING) << "CAP_SYS_BOOT is not supported"; | 
|  | 60 | return true; | 
|  | 61 | } | 
|  | 62 |  | 
|  | 63 | ScopedCaps caps(cap_get_proc()); | 
|  | 64 | if (!caps) { | 
|  | 65 | PLOG(WARNING) << "cap_get_proc() failed"; | 
|  | 66 | return true; | 
|  | 67 | } | 
|  | 68 |  | 
|  | 69 | cap_flag_value_t value = CAP_SET; | 
|  | 70 | if (cap_get_flag(caps.get(), CAP_SYS_BOOT, CAP_EFFECTIVE, &value) != 0) { | 
|  | 71 | PLOG(WARNING) << "cap_get_flag(CAP_SYS_BOOT, EFFECTIVE) failed"; | 
|  | 72 | return true; | 
|  | 73 | } | 
|  | 74 | return value == CAP_SET; | 
|  | 75 | } | 
|  | 76 |  | 
|  | 77 | void __attribute__((noreturn)) RebootSystem(unsigned int cmd, const std::string& rebootTarget) { | 
|  | 78 | LOG(INFO) << "Reboot ending, jumping to kernel"; | 
|  | 79 |  | 
|  | 80 | if (!IsRebootCapable()) { | 
|  | 81 | // On systems where init does not have the capability of rebooting the | 
|  | 82 | // device, just exit cleanly. | 
|  | 83 | exit(0); | 
|  | 84 | } | 
|  | 85 |  | 
|  | 86 | switch (cmd) { | 
|  | 87 | case ANDROID_RB_POWEROFF: | 
|  | 88 | reboot(RB_POWER_OFF); | 
|  | 89 | break; | 
|  | 90 |  | 
|  | 91 | case ANDROID_RB_RESTART2: | 
|  | 92 | syscall(__NR_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, | 
|  | 93 | LINUX_REBOOT_CMD_RESTART2, rebootTarget.c_str()); | 
|  | 94 | break; | 
|  | 95 |  | 
|  | 96 | case ANDROID_RB_THERMOFF: | 
| Wei Wang | 5f181bc | 2019-08-27 16:35:35 -0700 | [diff] [blame] | 97 | if (android::base::GetBoolProperty("ro.thermal_warmreset", false)) { | 
|  | 98 | LOG(INFO) << "Try to trigger a warm reset for thermal shutdown"; | 
|  | 99 | static constexpr const char kThermalShutdownTarget[] = "shutdown,thermal"; | 
|  | 100 | syscall(__NR_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, | 
|  | 101 | LINUX_REBOOT_CMD_RESTART2, kThermalShutdownTarget); | 
|  | 102 | } else { | 
|  | 103 | reboot(RB_POWER_OFF); | 
|  | 104 | } | 
| Tom Cherry | 44aceed | 2018-08-03 13:36:18 -0700 | [diff] [blame] | 105 | break; | 
|  | 106 | } | 
|  | 107 | // In normal case, reboot should not return. | 
|  | 108 | PLOG(ERROR) << "reboot call returned"; | 
|  | 109 | abort(); | 
|  | 110 | } | 
|  | 111 |  | 
| Elliott Hughes | 636ebc9 | 2019-10-07 18:16:23 -0700 | [diff] [blame] | 112 | void __attribute__((noreturn)) InitFatalReboot(int signal_number) { | 
| Tom Cherry | 59656fb | 2019-05-28 10:19:44 -0700 | [diff] [blame] | 113 | auto pid = fork(); | 
|  | 114 |  | 
|  | 115 | if (pid == -1) { | 
|  | 116 | // Couldn't fork, don't even try to backtrace, just reboot. | 
| Tom Cherry | 75e13ba | 2019-05-21 13:53:05 -0700 | [diff] [blame] | 117 | RebootSystem(ANDROID_RB_RESTART2, init_fatal_reboot_target); | 
| Tom Cherry | 59656fb | 2019-05-28 10:19:44 -0700 | [diff] [blame] | 118 | } else if (pid == 0) { | 
|  | 119 | // Fork a child for safety, since we always want to shut down if something goes wrong, but | 
|  | 120 | // its worth trying to get the backtrace, even in the signal handler, since typically it | 
|  | 121 | // does work despite not being async-signal-safe. | 
|  | 122 | sleep(5); | 
| Tom Cherry | 75e13ba | 2019-05-21 13:53:05 -0700 | [diff] [blame] | 123 | RebootSystem(ANDROID_RB_RESTART2, init_fatal_reboot_target); | 
| Tom Cherry | 59656fb | 2019-05-28 10:19:44 -0700 | [diff] [blame] | 124 | } | 
|  | 125 |  | 
|  | 126 | // In the parent, let's try to get a backtrace then shutdown. | 
| Elliott Hughes | 636ebc9 | 2019-10-07 18:16:23 -0700 | [diff] [blame] | 127 | LOG(ERROR) << __FUNCTION__ << ": signal " << signal_number; | 
| Tom Cherry | 59656fb | 2019-05-28 10:19:44 -0700 | [diff] [blame] | 128 | std::unique_ptr<Backtrace> backtrace( | 
|  | 129 | Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD)); | 
|  | 130 | if (!backtrace->Unwind(0)) { | 
|  | 131 | LOG(ERROR) << __FUNCTION__ << ": Failed to unwind callstack."; | 
|  | 132 | } | 
|  | 133 | for (size_t i = 0; i < backtrace->NumFrames(); i++) { | 
|  | 134 | LOG(ERROR) << backtrace->FormatFrameData(i); | 
|  | 135 | } | 
| Tom Cherry | 75e13ba | 2019-05-21 13:53:05 -0700 | [diff] [blame] | 136 | RebootSystem(ANDROID_RB_RESTART2, init_fatal_reboot_target); | 
| Tom Cherry | 59656fb | 2019-05-28 10:19:44 -0700 | [diff] [blame] | 137 | } | 
|  | 138 |  | 
| Tom Cherry | 44aceed | 2018-08-03 13:36:18 -0700 | [diff] [blame] | 139 | void InstallRebootSignalHandlers() { | 
|  | 140 | // Instead of panic'ing the kernel as is the default behavior when init crashes, | 
|  | 141 | // we prefer to reboot to bootloader on development builds, as this will prevent | 
|  | 142 | // boot looping bad configurations and allow both developers and test farms to easily | 
|  | 143 | // recover. | 
|  | 144 | struct sigaction action; | 
|  | 145 | memset(&action, 0, sizeof(action)); | 
|  | 146 | sigfillset(&action.sa_mask); | 
|  | 147 | action.sa_handler = [](int signal) { | 
|  | 148 | // These signal handlers are also caught for processes forked from init, however we do not | 
|  | 149 | // want them to trigger reboot, so we directly call _exit() for children processes here. | 
|  | 150 | if (getpid() != 1) { | 
|  | 151 | _exit(signal); | 
|  | 152 | } | 
|  | 153 |  | 
|  | 154 | // Calling DoReboot() or LOG(FATAL) is not a good option as this is a signal handler. | 
|  | 155 | // RebootSystem uses syscall() which isn't actually async-signal-safe, but our only option | 
|  | 156 | // and probably good enough given this is already an error case and only enabled for | 
|  | 157 | // development builds. | 
| Elliott Hughes | 636ebc9 | 2019-10-07 18:16:23 -0700 | [diff] [blame] | 158 | InitFatalReboot(signal); | 
| Tom Cherry | 44aceed | 2018-08-03 13:36:18 -0700 | [diff] [blame] | 159 | }; | 
|  | 160 | action.sa_flags = SA_RESTART; | 
|  | 161 | sigaction(SIGABRT, &action, nullptr); | 
|  | 162 | sigaction(SIGBUS, &action, nullptr); | 
|  | 163 | sigaction(SIGFPE, &action, nullptr); | 
|  | 164 | sigaction(SIGILL, &action, nullptr); | 
|  | 165 | sigaction(SIGSEGV, &action, nullptr); | 
|  | 166 | #if defined(SIGSTKFLT) | 
|  | 167 | sigaction(SIGSTKFLT, &action, nullptr); | 
|  | 168 | #endif | 
|  | 169 | sigaction(SIGSYS, &action, nullptr); | 
|  | 170 | sigaction(SIGTRAP, &action, nullptr); | 
|  | 171 | } | 
|  | 172 |  | 
|  | 173 | }  // namespace init | 
|  | 174 | }  // namespace android |