blob: 98f6857f5f987d790e0eb68b08db3b5e0d81bf72 [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"
Tom Cherry44aceed2018-08-03 13:36:18 -070034
35namespace android {
36namespace init {
37
Tom Cherry75e13ba2019-05-21 13:53:05 -070038static std::string init_fatal_reboot_target = "bootloader";
Woody Lin6bbfa262019-12-27 18:14:13 +080039static bool init_fatal_panic = false;
Tom Cherry75e13ba2019-05-21 13:53:05 -070040
Woody Lin45215ae2019-12-26 22:22:28 +080041void SetFatalRebootTarget(const std::optional<std::string>& reboot_target) {
Tom Cherry75e13ba2019-05-21 13:53:05 -070042 std::string cmdline;
43 android::base::ReadFileToString("/proc/cmdline", &cmdline);
44 cmdline = android::base::Trim(cmdline);
45
Woody Lin6bbfa262019-12-27 18:14:13 +080046 const char kInitFatalPanicString[] = "androidboot.init_fatal_panic=true";
47 init_fatal_panic = cmdline.find(kInitFatalPanicString) != std::string::npos;
48
Woody Lin45215ae2019-12-26 22:22:28 +080049 if (reboot_target) {
50 init_fatal_reboot_target = *reboot_target;
51 return;
52 }
53
Tom Cherry75e13ba2019-05-21 13:53:05 -070054 const char kRebootTargetString[] = "androidboot.init_fatal_reboot_target=";
55 auto start_pos = cmdline.find(kRebootTargetString);
56 if (start_pos == std::string::npos) {
57 return; // We already default to bootloader if no setting is provided.
58 }
59 start_pos += sizeof(kRebootTargetString) - 1;
60
61 auto end_pos = cmdline.find(' ', start_pos);
62 // if end_pos isn't found, then we've run off the end, but this is okay as this is the last
63 // entry, and -1 is a valid size for string::substr();
64 auto size = end_pos == std::string::npos ? -1 : end_pos - start_pos;
65 init_fatal_reboot_target = cmdline.substr(start_pos, size);
66}
67
Tom Cherry44aceed2018-08-03 13:36:18 -070068bool IsRebootCapable() {
69 if (!CAP_IS_SUPPORTED(CAP_SYS_BOOT)) {
70 PLOG(WARNING) << "CAP_SYS_BOOT is not supported";
71 return true;
72 }
73
74 ScopedCaps caps(cap_get_proc());
75 if (!caps) {
76 PLOG(WARNING) << "cap_get_proc() failed";
77 return true;
78 }
79
80 cap_flag_value_t value = CAP_SET;
81 if (cap_get_flag(caps.get(), CAP_SYS_BOOT, CAP_EFFECTIVE, &value) != 0) {
82 PLOG(WARNING) << "cap_get_flag(CAP_SYS_BOOT, EFFECTIVE) failed";
83 return true;
84 }
85 return value == CAP_SET;
86}
87
88void __attribute__((noreturn)) RebootSystem(unsigned int cmd, const std::string& rebootTarget) {
89 LOG(INFO) << "Reboot ending, jumping to kernel";
90
91 if (!IsRebootCapable()) {
92 // On systems where init does not have the capability of rebooting the
93 // device, just exit cleanly.
94 exit(0);
95 }
96
97 switch (cmd) {
98 case ANDROID_RB_POWEROFF:
99 reboot(RB_POWER_OFF);
100 break;
101
102 case ANDROID_RB_RESTART2:
103 syscall(__NR_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,
104 LINUX_REBOOT_CMD_RESTART2, rebootTarget.c_str());
105 break;
106
107 case ANDROID_RB_THERMOFF:
Wei Wang5f181bc2019-08-27 16:35:35 -0700108 if (android::base::GetBoolProperty("ro.thermal_warmreset", false)) {
109 LOG(INFO) << "Try to trigger a warm reset for thermal shutdown";
110 static constexpr const char kThermalShutdownTarget[] = "shutdown,thermal";
111 syscall(__NR_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,
112 LINUX_REBOOT_CMD_RESTART2, kThermalShutdownTarget);
113 } else {
114 reboot(RB_POWER_OFF);
115 }
Tom Cherry44aceed2018-08-03 13:36:18 -0700116 break;
117 }
118 // In normal case, reboot should not return.
119 PLOG(ERROR) << "reboot call returned";
120 abort();
121}
122
Elliott Hughes636ebc92019-10-07 18:16:23 -0700123void __attribute__((noreturn)) InitFatalReboot(int signal_number) {
Tom Cherry59656fb2019-05-28 10:19:44 -0700124 auto pid = fork();
125
126 if (pid == -1) {
127 // Couldn't fork, don't even try to backtrace, just reboot.
Tom Cherry75e13ba2019-05-21 13:53:05 -0700128 RebootSystem(ANDROID_RB_RESTART2, init_fatal_reboot_target);
Tom Cherry59656fb2019-05-28 10:19:44 -0700129 } else if (pid == 0) {
130 // Fork a child for safety, since we always want to shut down if something goes wrong, but
131 // its worth trying to get the backtrace, even in the signal handler, since typically it
132 // does work despite not being async-signal-safe.
133 sleep(5);
Tom Cherry75e13ba2019-05-21 13:53:05 -0700134 RebootSystem(ANDROID_RB_RESTART2, init_fatal_reboot_target);
Tom Cherry59656fb2019-05-28 10:19:44 -0700135 }
136
137 // In the parent, let's try to get a backtrace then shutdown.
Elliott Hughes636ebc92019-10-07 18:16:23 -0700138 LOG(ERROR) << __FUNCTION__ << ": signal " << signal_number;
Tom Cherry59656fb2019-05-28 10:19:44 -0700139 std::unique_ptr<Backtrace> backtrace(
140 Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
141 if (!backtrace->Unwind(0)) {
142 LOG(ERROR) << __FUNCTION__ << ": Failed to unwind callstack.";
143 }
144 for (size_t i = 0; i < backtrace->NumFrames(); i++) {
145 LOG(ERROR) << backtrace->FormatFrameData(i);
146 }
Woody Lin6bbfa262019-12-27 18:14:13 +0800147 if (init_fatal_panic) {
Woody Linbe1cf902020-05-08 16:26:56 +0800148 LOG(ERROR) << __FUNCTION__ << ": Trigger crash";
149 android::base::WriteStringToFile("c", PROC_SYSRQ);
150 LOG(ERROR) << __FUNCTION__ << ": Sys-Rq failed to crash the system; fallback to exit().";
Woody Lin6bbfa262019-12-27 18:14:13 +0800151 _exit(signal_number);
152 }
Tom Cherry75e13ba2019-05-21 13:53:05 -0700153 RebootSystem(ANDROID_RB_RESTART2, init_fatal_reboot_target);
Tom Cherry59656fb2019-05-28 10:19:44 -0700154}
155
Tom Cherry44aceed2018-08-03 13:36:18 -0700156void InstallRebootSignalHandlers() {
157 // Instead of panic'ing the kernel as is the default behavior when init crashes,
158 // we prefer to reboot to bootloader on development builds, as this will prevent
159 // boot looping bad configurations and allow both developers and test farms to easily
160 // recover.
161 struct sigaction action;
162 memset(&action, 0, sizeof(action));
163 sigfillset(&action.sa_mask);
164 action.sa_handler = [](int signal) {
165 // These signal handlers are also caught for processes forked from init, however we do not
166 // want them to trigger reboot, so we directly call _exit() for children processes here.
167 if (getpid() != 1) {
168 _exit(signal);
169 }
170
171 // Calling DoReboot() or LOG(FATAL) is not a good option as this is a signal handler.
172 // RebootSystem uses syscall() which isn't actually async-signal-safe, but our only option
173 // and probably good enough given this is already an error case and only enabled for
174 // development builds.
Elliott Hughes636ebc92019-10-07 18:16:23 -0700175 InitFatalReboot(signal);
Tom Cherry44aceed2018-08-03 13:36:18 -0700176 };
177 action.sa_flags = SA_RESTART;
178 sigaction(SIGABRT, &action, nullptr);
179 sigaction(SIGBUS, &action, nullptr);
180 sigaction(SIGFPE, &action, nullptr);
181 sigaction(SIGILL, &action, nullptr);
182 sigaction(SIGSEGV, &action, nullptr);
183#if defined(SIGSTKFLT)
184 sigaction(SIGSTKFLT, &action, nullptr);
185#endif
186 sigaction(SIGSYS, &action, nullptr);
187 sigaction(SIGTRAP, &action, nullptr);
188}
189
190} // namespace init
191} // namespace android