| 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 |  | 
| Woody Lin | 45215ae | 2019-12-26 22:22:28 +0800 | [diff] [blame] | 22 | #include <optional> | 
| Tom Cherry | 59656fb | 2019-05-28 10:19:44 -0700 | [diff] [blame] | 23 | #include <string> | 
|  | 24 |  | 
| Wei Wang | 5f181bc | 2019-08-27 16:35:35 -0700 | [diff] [blame] | 25 | #include <android-base/file.h> | 
|  | 26 | #include <android-base/logging.h> | 
|  | 27 | #include <android-base/properties.h> | 
|  | 28 | #include <android-base/strings.h> | 
| Wei Wang | 5f181bc | 2019-08-27 16:35:35 -0700 | [diff] [blame] | 29 | #include <cutils/android_reboot.h> | 
| Yi-Yo Chiang | 79ad1e2 | 2023-07-29 17:37:23 +0800 | [diff] [blame] | 30 | #include <fs_mgr.h> | 
| Christopher Ferris | d2bd6c5 | 2022-05-11 14:42:38 -0700 | [diff] [blame] | 31 | #include <unwindstack/AndroidUnwinder.h> | 
| Tom Cherry | 44aceed | 2018-08-03 13:36:18 -0700 | [diff] [blame] | 32 |  | 
|  | 33 | #include "capabilities.h" | 
| Woody Lin | be1cf90 | 2020-05-08 16:26:56 +0800 | [diff] [blame] | 34 | #include "reboot_utils.h" | 
| Devin Moore | 2652fdb | 2021-06-18 14:47:25 -0700 | [diff] [blame] | 35 | #include "util.h" | 
| Tom Cherry | 44aceed | 2018-08-03 13:36:18 -0700 | [diff] [blame] | 36 |  | 
|  | 37 | namespace android { | 
|  | 38 | namespace init { | 
|  | 39 |  | 
| Tom Cherry | 75e13ba | 2019-05-21 13:53:05 -0700 | [diff] [blame] | 40 | static std::string init_fatal_reboot_target = "bootloader"; | 
| Woody Lin | 6bbfa26 | 2019-12-27 18:14:13 +0800 | [diff] [blame] | 41 | static bool init_fatal_panic = false; | 
| Tom Cherry | 75e13ba | 2019-05-21 13:53:05 -0700 | [diff] [blame] | 42 |  | 
| Devin Moore | 2652fdb | 2021-06-18 14:47:25 -0700 | [diff] [blame] | 43 | // this needs to read the /proc/* files directly because it is called before | 
|  | 44 | // ro.boot.* properties are initialized | 
| Woody Lin | 45215ae | 2019-12-26 22:22:28 +0800 | [diff] [blame] | 45 | void SetFatalRebootTarget(const std::optional<std::string>& reboot_target) { | 
| Tom Cherry | 75e13ba | 2019-05-21 13:53:05 -0700 | [diff] [blame] | 46 | std::string cmdline; | 
|  | 47 | android::base::ReadFileToString("/proc/cmdline", &cmdline); | 
|  | 48 | cmdline = android::base::Trim(cmdline); | 
|  | 49 |  | 
| Devin Moore | 2652fdb | 2021-06-18 14:47:25 -0700 | [diff] [blame] | 50 | const std::string kInitFatalPanicParamString = "androidboot.init_fatal_panic"; | 
|  | 51 | if (cmdline.find(kInitFatalPanicParamString) == std::string::npos) { | 
| Yi-Yo Chiang | 79ad1e2 | 2023-07-29 17:37:23 +0800 | [diff] [blame] | 52 | std::string value; | 
|  | 53 | init_fatal_panic = (android::fs_mgr::GetBootconfig(kInitFatalPanicParamString, &value) && | 
|  | 54 | value == "true"); | 
| Devin Moore | 2652fdb | 2021-06-18 14:47:25 -0700 | [diff] [blame] | 55 | } else { | 
|  | 56 | const std::string kInitFatalPanicString = kInitFatalPanicParamString + "=true"; | 
|  | 57 | init_fatal_panic = cmdline.find(kInitFatalPanicString) != std::string::npos; | 
|  | 58 | } | 
| Woody Lin | 6bbfa26 | 2019-12-27 18:14:13 +0800 | [diff] [blame] | 59 |  | 
| Woody Lin | 45215ae | 2019-12-26 22:22:28 +0800 | [diff] [blame] | 60 | if (reboot_target) { | 
|  | 61 | init_fatal_reboot_target = *reboot_target; | 
|  | 62 | return; | 
|  | 63 | } | 
|  | 64 |  | 
| Devin Moore | 2652fdb | 2021-06-18 14:47:25 -0700 | [diff] [blame] | 65 | const std::string kRebootTargetString = "androidboot.init_fatal_reboot_target"; | 
| Tom Cherry | 75e13ba | 2019-05-21 13:53:05 -0700 | [diff] [blame] | 66 | auto start_pos = cmdline.find(kRebootTargetString); | 
|  | 67 | if (start_pos == std::string::npos) { | 
| Yi-Yo Chiang | 79ad1e2 | 2023-07-29 17:37:23 +0800 | [diff] [blame] | 68 | android::fs_mgr::GetBootconfig(kRebootTargetString, &init_fatal_reboot_target); | 
| Devin Moore | 2652fdb | 2021-06-18 14:47:25 -0700 | [diff] [blame] | 69 | // We already default to bootloader if no setting is provided. | 
|  | 70 | } else { | 
|  | 71 | const std::string kRebootTargetStringPattern = kRebootTargetString + "="; | 
|  | 72 | start_pos += sizeof(kRebootTargetStringPattern) - 1; | 
| Tom Cherry | 75e13ba | 2019-05-21 13:53:05 -0700 | [diff] [blame] | 73 |  | 
| Devin Moore | 2652fdb | 2021-06-18 14:47:25 -0700 | [diff] [blame] | 74 | auto end_pos = cmdline.find(' ', start_pos); | 
|  | 75 | // if end_pos isn't found, then we've run off the end, but this is okay as this is the last | 
|  | 76 | // entry, and -1 is a valid size for string::substr(); | 
|  | 77 | auto size = end_pos == std::string::npos ? -1 : end_pos - start_pos; | 
|  | 78 | init_fatal_reboot_target = cmdline.substr(start_pos, size); | 
|  | 79 | } | 
| Tom Cherry | 75e13ba | 2019-05-21 13:53:05 -0700 | [diff] [blame] | 80 | } | 
|  | 81 |  | 
| Tom Cherry | 44aceed | 2018-08-03 13:36:18 -0700 | [diff] [blame] | 82 | bool IsRebootCapable() { | 
|  | 83 | if (!CAP_IS_SUPPORTED(CAP_SYS_BOOT)) { | 
|  | 84 | PLOG(WARNING) << "CAP_SYS_BOOT is not supported"; | 
|  | 85 | return true; | 
|  | 86 | } | 
|  | 87 |  | 
|  | 88 | ScopedCaps caps(cap_get_proc()); | 
|  | 89 | if (!caps) { | 
|  | 90 | PLOG(WARNING) << "cap_get_proc() failed"; | 
|  | 91 | return true; | 
|  | 92 | } | 
|  | 93 |  | 
|  | 94 | cap_flag_value_t value = CAP_SET; | 
|  | 95 | if (cap_get_flag(caps.get(), CAP_SYS_BOOT, CAP_EFFECTIVE, &value) != 0) { | 
|  | 96 | PLOG(WARNING) << "cap_get_flag(CAP_SYS_BOOT, EFFECTIVE) failed"; | 
|  | 97 | return true; | 
|  | 98 | } | 
|  | 99 | return value == CAP_SET; | 
|  | 100 | } | 
|  | 101 |  | 
| Sayanna Chandula | 5754b5a | 2022-10-04 15:06:05 -0700 | [diff] [blame] | 102 | void __attribute__((noreturn)) | 
|  | 103 | RebootSystem(unsigned int cmd, const std::string& rebootTarget, const std::string& reboot_reason) { | 
| Tom Cherry | 44aceed | 2018-08-03 13:36:18 -0700 | [diff] [blame] | 104 | LOG(INFO) << "Reboot ending, jumping to kernel"; | 
|  | 105 |  | 
|  | 106 | if (!IsRebootCapable()) { | 
|  | 107 | // On systems where init does not have the capability of rebooting the | 
|  | 108 | // device, just exit cleanly. | 
|  | 109 | exit(0); | 
|  | 110 | } | 
|  | 111 |  | 
|  | 112 | switch (cmd) { | 
|  | 113 | case ANDROID_RB_POWEROFF: | 
|  | 114 | reboot(RB_POWER_OFF); | 
|  | 115 | break; | 
|  | 116 |  | 
|  | 117 | case ANDROID_RB_RESTART2: | 
|  | 118 | syscall(__NR_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, | 
|  | 119 | LINUX_REBOOT_CMD_RESTART2, rebootTarget.c_str()); | 
|  | 120 | break; | 
|  | 121 |  | 
|  | 122 | case ANDROID_RB_THERMOFF: | 
| Wei Wang | 5f181bc | 2019-08-27 16:35:35 -0700 | [diff] [blame] | 123 | if (android::base::GetBoolProperty("ro.thermal_warmreset", false)) { | 
| Sayanna Chandula | 5754b5a | 2022-10-04 15:06:05 -0700 | [diff] [blame] | 124 | std::string reason = "shutdown,thermal"; | 
|  | 125 | if (!reboot_reason.empty()) reason = reboot_reason; | 
|  | 126 |  | 
| Wei Wang | 5f181bc | 2019-08-27 16:35:35 -0700 | [diff] [blame] | 127 | LOG(INFO) << "Try to trigger a warm reset for thermal shutdown"; | 
| Wei Wang | 5f181bc | 2019-08-27 16:35:35 -0700 | [diff] [blame] | 128 | syscall(__NR_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, | 
| Sayanna Chandula | 5754b5a | 2022-10-04 15:06:05 -0700 | [diff] [blame] | 129 | LINUX_REBOOT_CMD_RESTART2, reason.c_str()); | 
| Wei Wang | 5f181bc | 2019-08-27 16:35:35 -0700 | [diff] [blame] | 130 | } else { | 
|  | 131 | reboot(RB_POWER_OFF); | 
|  | 132 | } | 
| Tom Cherry | 44aceed | 2018-08-03 13:36:18 -0700 | [diff] [blame] | 133 | break; | 
|  | 134 | } | 
|  | 135 | // In normal case, reboot should not return. | 
|  | 136 | PLOG(ERROR) << "reboot call returned"; | 
|  | 137 | abort(); | 
|  | 138 | } | 
|  | 139 |  | 
| Elliott Hughes | 636ebc9 | 2019-10-07 18:16:23 -0700 | [diff] [blame] | 140 | void __attribute__((noreturn)) InitFatalReboot(int signal_number) { | 
| Tom Cherry | 59656fb | 2019-05-28 10:19:44 -0700 | [diff] [blame] | 141 | auto pid = fork(); | 
|  | 142 |  | 
|  | 143 | if (pid == -1) { | 
|  | 144 | // Couldn't fork, don't even try to backtrace, just reboot. | 
| Tom Cherry | 75e13ba | 2019-05-21 13:53:05 -0700 | [diff] [blame] | 145 | RebootSystem(ANDROID_RB_RESTART2, init_fatal_reboot_target); | 
| Tom Cherry | 59656fb | 2019-05-28 10:19:44 -0700 | [diff] [blame] | 146 | } else if (pid == 0) { | 
|  | 147 | // Fork a child for safety, since we always want to shut down if something goes wrong, but | 
|  | 148 | // its worth trying to get the backtrace, even in the signal handler, since typically it | 
|  | 149 | // does work despite not being async-signal-safe. | 
|  | 150 | sleep(5); | 
| Tom Cherry | 75e13ba | 2019-05-21 13:53:05 -0700 | [diff] [blame] | 151 | RebootSystem(ANDROID_RB_RESTART2, init_fatal_reboot_target); | 
| Tom Cherry | 59656fb | 2019-05-28 10:19:44 -0700 | [diff] [blame] | 152 | } | 
|  | 153 |  | 
|  | 154 | // In the parent, let's try to get a backtrace then shutdown. | 
| Elliott Hughes | 636ebc9 | 2019-10-07 18:16:23 -0700 | [diff] [blame] | 155 | LOG(ERROR) << __FUNCTION__ << ": signal " << signal_number; | 
| Christopher Ferris | d2bd6c5 | 2022-05-11 14:42:38 -0700 | [diff] [blame] | 156 | unwindstack::AndroidLocalUnwinder unwinder; | 
|  | 157 | unwindstack::AndroidUnwinderData data; | 
|  | 158 | if (!unwinder.Unwind(data)) { | 
|  | 159 | LOG(ERROR) << __FUNCTION__ << ": Failed to unwind callstack: " << data.GetErrorString(); | 
| Tom Cherry | 59656fb | 2019-05-28 10:19:44 -0700 | [diff] [blame] | 160 | } | 
| Christopher Ferris | d2bd6c5 | 2022-05-11 14:42:38 -0700 | [diff] [blame] | 161 | for (const auto& frame : data.frames) { | 
|  | 162 | LOG(ERROR) << unwinder.FormatFrame(frame); | 
| Tom Cherry | 59656fb | 2019-05-28 10:19:44 -0700 | [diff] [blame] | 163 | } | 
| Woody Lin | 6bbfa26 | 2019-12-27 18:14:13 +0800 | [diff] [blame] | 164 | if (init_fatal_panic) { | 
| Woody Lin | be1cf90 | 2020-05-08 16:26:56 +0800 | [diff] [blame] | 165 | LOG(ERROR) << __FUNCTION__ << ": Trigger crash"; | 
|  | 166 | android::base::WriteStringToFile("c", PROC_SYSRQ); | 
|  | 167 | LOG(ERROR) << __FUNCTION__ << ": Sys-Rq failed to crash the system; fallback to exit()."; | 
| Woody Lin | 6bbfa26 | 2019-12-27 18:14:13 +0800 | [diff] [blame] | 168 | _exit(signal_number); | 
|  | 169 | } | 
| Tom Cherry | 75e13ba | 2019-05-21 13:53:05 -0700 | [diff] [blame] | 170 | RebootSystem(ANDROID_RB_RESTART2, init_fatal_reboot_target); | 
| Tom Cherry | 59656fb | 2019-05-28 10:19:44 -0700 | [diff] [blame] | 171 | } | 
|  | 172 |  | 
| Tom Cherry | 44aceed | 2018-08-03 13:36:18 -0700 | [diff] [blame] | 173 | void InstallRebootSignalHandlers() { | 
|  | 174 | // Instead of panic'ing the kernel as is the default behavior when init crashes, | 
|  | 175 | // we prefer to reboot to bootloader on development builds, as this will prevent | 
|  | 176 | // boot looping bad configurations and allow both developers and test farms to easily | 
|  | 177 | // recover. | 
|  | 178 | struct sigaction action; | 
|  | 179 | memset(&action, 0, sizeof(action)); | 
|  | 180 | sigfillset(&action.sa_mask); | 
|  | 181 | action.sa_handler = [](int signal) { | 
|  | 182 | // These signal handlers are also caught for processes forked from init, however we do not | 
|  | 183 | // want them to trigger reboot, so we directly call _exit() for children processes here. | 
|  | 184 | if (getpid() != 1) { | 
|  | 185 | _exit(signal); | 
|  | 186 | } | 
|  | 187 |  | 
|  | 188 | // Calling DoReboot() or LOG(FATAL) is not a good option as this is a signal handler. | 
|  | 189 | // RebootSystem uses syscall() which isn't actually async-signal-safe, but our only option | 
|  | 190 | // and probably good enough given this is already an error case and only enabled for | 
|  | 191 | // development builds. | 
| Elliott Hughes | 636ebc9 | 2019-10-07 18:16:23 -0700 | [diff] [blame] | 192 | InitFatalReboot(signal); | 
| Tom Cherry | 44aceed | 2018-08-03 13:36:18 -0700 | [diff] [blame] | 193 | }; | 
|  | 194 | action.sa_flags = SA_RESTART; | 
|  | 195 | sigaction(SIGABRT, &action, nullptr); | 
|  | 196 | sigaction(SIGBUS, &action, nullptr); | 
|  | 197 | sigaction(SIGFPE, &action, nullptr); | 
|  | 198 | sigaction(SIGILL, &action, nullptr); | 
|  | 199 | sigaction(SIGSEGV, &action, nullptr); | 
|  | 200 | #if defined(SIGSTKFLT) | 
|  | 201 | sigaction(SIGSTKFLT, &action, nullptr); | 
|  | 202 | #endif | 
|  | 203 | sigaction(SIGSYS, &action, nullptr); | 
|  | 204 | sigaction(SIGTRAP, &action, nullptr); | 
|  | 205 | } | 
|  | 206 |  | 
|  | 207 | }  // namespace init | 
|  | 208 | }  // namespace android |