blob: b3fa9fd3bac0b0f63cd8308ffc7776a0bb18c99a [file] [log] [blame]
Tom Cherry44aceed2018-08-03 13:36:18 -07001/*
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 Lin45215ae2019-12-26 22:22:28 +080022#include <optional>
Tom Cherry59656fb2019-05-28 10:19:44 -070023#include <string>
24
Wei Wang5f181bc2019-08-27 16:35:35 -070025#include <android-base/file.h>
26#include <android-base/logging.h>
27#include <android-base/properties.h>
28#include <android-base/strings.h>
29#include <backtrace/Backtrace.h>
30#include <cutils/android_reboot.h>
Tom Cherry44aceed2018-08-03 13:36:18 -070031
32#include "capabilities.h"
Woody Linbe1cf902020-05-08 16:26:56 +080033#include "reboot_utils.h"
Devin Moore2652fdb2021-06-18 14:47:25 -070034#include "util.h"
Tom Cherry44aceed2018-08-03 13:36:18 -070035
36namespace android {
37namespace init {
38
Tom Cherry75e13ba2019-05-21 13:53:05 -070039static std::string init_fatal_reboot_target = "bootloader";
Woody Lin6bbfa262019-12-27 18:14:13 +080040static bool init_fatal_panic = false;
Tom Cherry75e13ba2019-05-21 13:53:05 -070041
Devin Moore2652fdb2021-06-18 14:47:25 -070042// this needs to read the /proc/* files directly because it is called before
43// ro.boot.* properties are initialized
Woody Lin45215ae2019-12-26 22:22:28 +080044void SetFatalRebootTarget(const std::optional<std::string>& reboot_target) {
Tom Cherry75e13ba2019-05-21 13:53:05 -070045 std::string cmdline;
46 android::base::ReadFileToString("/proc/cmdline", &cmdline);
47 cmdline = android::base::Trim(cmdline);
48
Devin Moore2652fdb2021-06-18 14:47:25 -070049 const std::string kInitFatalPanicParamString = "androidboot.init_fatal_panic";
50 if (cmdline.find(kInitFatalPanicParamString) == std::string::npos) {
51 init_fatal_panic = false;
52 ImportBootconfig(
53 [kInitFatalPanicParamString](const std::string& key, const std::string& value) {
54 if (key == kInitFatalPanicParamString && value == "true") {
55 init_fatal_panic = true;
56 }
57 });
58 } else {
59 const std::string kInitFatalPanicString = kInitFatalPanicParamString + "=true";
60 init_fatal_panic = cmdline.find(kInitFatalPanicString) != std::string::npos;
61 }
Woody Lin6bbfa262019-12-27 18:14:13 +080062
Woody Lin45215ae2019-12-26 22:22:28 +080063 if (reboot_target) {
64 init_fatal_reboot_target = *reboot_target;
65 return;
66 }
67
Devin Moore2652fdb2021-06-18 14:47:25 -070068 const std::string kRebootTargetString = "androidboot.init_fatal_reboot_target";
Tom Cherry75e13ba2019-05-21 13:53:05 -070069 auto start_pos = cmdline.find(kRebootTargetString);
70 if (start_pos == std::string::npos) {
Devin Moore2652fdb2021-06-18 14:47:25 -070071 ImportBootconfig([kRebootTargetString](const std::string& key, const std::string& value) {
72 if (key == kRebootTargetString) {
73 init_fatal_reboot_target = value;
74 }
75 });
76 // We already default to bootloader if no setting is provided.
77 } else {
78 const std::string kRebootTargetStringPattern = kRebootTargetString + "=";
79 start_pos += sizeof(kRebootTargetStringPattern) - 1;
Tom Cherry75e13ba2019-05-21 13:53:05 -070080
Devin Moore2652fdb2021-06-18 14:47:25 -070081 auto end_pos = cmdline.find(' ', start_pos);
82 // if end_pos isn't found, then we've run off the end, but this is okay as this is the last
83 // entry, and -1 is a valid size for string::substr();
84 auto size = end_pos == std::string::npos ? -1 : end_pos - start_pos;
85 init_fatal_reboot_target = cmdline.substr(start_pos, size);
86 }
Tom Cherry75e13ba2019-05-21 13:53:05 -070087}
88
Tom Cherry44aceed2018-08-03 13:36:18 -070089bool IsRebootCapable() {
90 if (!CAP_IS_SUPPORTED(CAP_SYS_BOOT)) {
91 PLOG(WARNING) << "CAP_SYS_BOOT is not supported";
92 return true;
93 }
94
95 ScopedCaps caps(cap_get_proc());
96 if (!caps) {
97 PLOG(WARNING) << "cap_get_proc() failed";
98 return true;
99 }
100
101 cap_flag_value_t value = CAP_SET;
102 if (cap_get_flag(caps.get(), CAP_SYS_BOOT, CAP_EFFECTIVE, &value) != 0) {
103 PLOG(WARNING) << "cap_get_flag(CAP_SYS_BOOT, EFFECTIVE) failed";
104 return true;
105 }
106 return value == CAP_SET;
107}
108
109void __attribute__((noreturn)) RebootSystem(unsigned int cmd, const std::string& rebootTarget) {
110 LOG(INFO) << "Reboot ending, jumping to kernel";
111
112 if (!IsRebootCapable()) {
113 // On systems where init does not have the capability of rebooting the
114 // device, just exit cleanly.
115 exit(0);
116 }
117
118 switch (cmd) {
119 case ANDROID_RB_POWEROFF:
120 reboot(RB_POWER_OFF);
121 break;
122
123 case ANDROID_RB_RESTART2:
124 syscall(__NR_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,
125 LINUX_REBOOT_CMD_RESTART2, rebootTarget.c_str());
126 break;
127
128 case ANDROID_RB_THERMOFF:
Wei Wang5f181bc2019-08-27 16:35:35 -0700129 if (android::base::GetBoolProperty("ro.thermal_warmreset", false)) {
130 LOG(INFO) << "Try to trigger a warm reset for thermal shutdown";
131 static constexpr const char kThermalShutdownTarget[] = "shutdown,thermal";
132 syscall(__NR_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,
133 LINUX_REBOOT_CMD_RESTART2, kThermalShutdownTarget);
134 } else {
135 reboot(RB_POWER_OFF);
136 }
Tom Cherry44aceed2018-08-03 13:36:18 -0700137 break;
138 }
139 // In normal case, reboot should not return.
140 PLOG(ERROR) << "reboot call returned";
141 abort();
142}
143
Elliott Hughes636ebc92019-10-07 18:16:23 -0700144void __attribute__((noreturn)) InitFatalReboot(int signal_number) {
Tom Cherry59656fb2019-05-28 10:19:44 -0700145 auto pid = fork();
146
147 if (pid == -1) {
148 // Couldn't fork, don't even try to backtrace, just reboot.
Tom Cherry75e13ba2019-05-21 13:53:05 -0700149 RebootSystem(ANDROID_RB_RESTART2, init_fatal_reboot_target);
Tom Cherry59656fb2019-05-28 10:19:44 -0700150 } else if (pid == 0) {
151 // Fork a child for safety, since we always want to shut down if something goes wrong, but
152 // its worth trying to get the backtrace, even in the signal handler, since typically it
153 // does work despite not being async-signal-safe.
154 sleep(5);
Tom Cherry75e13ba2019-05-21 13:53:05 -0700155 RebootSystem(ANDROID_RB_RESTART2, init_fatal_reboot_target);
Tom Cherry59656fb2019-05-28 10:19:44 -0700156 }
157
158 // In the parent, let's try to get a backtrace then shutdown.
Elliott Hughes636ebc92019-10-07 18:16:23 -0700159 LOG(ERROR) << __FUNCTION__ << ": signal " << signal_number;
Tom Cherry59656fb2019-05-28 10:19:44 -0700160 std::unique_ptr<Backtrace> backtrace(
161 Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
162 if (!backtrace->Unwind(0)) {
163 LOG(ERROR) << __FUNCTION__ << ": Failed to unwind callstack.";
164 }
165 for (size_t i = 0; i < backtrace->NumFrames(); i++) {
166 LOG(ERROR) << backtrace->FormatFrameData(i);
167 }
Woody Lin6bbfa262019-12-27 18:14:13 +0800168 if (init_fatal_panic) {
Woody Linbe1cf902020-05-08 16:26:56 +0800169 LOG(ERROR) << __FUNCTION__ << ": Trigger crash";
170 android::base::WriteStringToFile("c", PROC_SYSRQ);
171 LOG(ERROR) << __FUNCTION__ << ": Sys-Rq failed to crash the system; fallback to exit().";
Woody Lin6bbfa262019-12-27 18:14:13 +0800172 _exit(signal_number);
173 }
Tom Cherry75e13ba2019-05-21 13:53:05 -0700174 RebootSystem(ANDROID_RB_RESTART2, init_fatal_reboot_target);
Tom Cherry59656fb2019-05-28 10:19:44 -0700175}
176
Tom Cherry44aceed2018-08-03 13:36:18 -0700177void InstallRebootSignalHandlers() {
178 // Instead of panic'ing the kernel as is the default behavior when init crashes,
179 // we prefer to reboot to bootloader on development builds, as this will prevent
180 // boot looping bad configurations and allow both developers and test farms to easily
181 // recover.
182 struct sigaction action;
183 memset(&action, 0, sizeof(action));
184 sigfillset(&action.sa_mask);
185 action.sa_handler = [](int signal) {
186 // These signal handlers are also caught for processes forked from init, however we do not
187 // want them to trigger reboot, so we directly call _exit() for children processes here.
188 if (getpid() != 1) {
189 _exit(signal);
190 }
191
192 // Calling DoReboot() or LOG(FATAL) is not a good option as this is a signal handler.
193 // RebootSystem uses syscall() which isn't actually async-signal-safe, but our only option
194 // and probably good enough given this is already an error case and only enabled for
195 // development builds.
Elliott Hughes636ebc92019-10-07 18:16:23 -0700196 InitFatalReboot(signal);
Tom Cherry44aceed2018-08-03 13:36:18 -0700197 };
198 action.sa_flags = SA_RESTART;
199 sigaction(SIGABRT, &action, nullptr);
200 sigaction(SIGBUS, &action, nullptr);
201 sigaction(SIGFPE, &action, nullptr);
202 sigaction(SIGILL, &action, nullptr);
203 sigaction(SIGSEGV, &action, nullptr);
204#if defined(SIGSTKFLT)
205 sigaction(SIGSTKFLT, &action, nullptr);
206#endif
207 sigaction(SIGSYS, &action, nullptr);
208 sigaction(SIGTRAP, &action, nullptr);
209}
210
211} // namespace init
212} // namespace android