blob: 4524f6983b1f1557c2a3f6d5e156b93adee69f98 [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
Tom Cherry59656fb2019-05-28 10:19:44 -070022#include <string>
23
24#include "android-base/file.h"
25#include "android-base/logging.h"
26#include "android-base/strings.h"
27#include "backtrace/Backtrace.h"
28#include "cutils/android_reboot.h"
Tom Cherry44aceed2018-08-03 13:36:18 -070029
30#include "capabilities.h"
31
32namespace android {
33namespace init {
34
35bool IsRebootCapable() {
36 if (!CAP_IS_SUPPORTED(CAP_SYS_BOOT)) {
37 PLOG(WARNING) << "CAP_SYS_BOOT is not supported";
38 return true;
39 }
40
41 ScopedCaps caps(cap_get_proc());
42 if (!caps) {
43 PLOG(WARNING) << "cap_get_proc() failed";
44 return true;
45 }
46
47 cap_flag_value_t value = CAP_SET;
48 if (cap_get_flag(caps.get(), CAP_SYS_BOOT, CAP_EFFECTIVE, &value) != 0) {
49 PLOG(WARNING) << "cap_get_flag(CAP_SYS_BOOT, EFFECTIVE) failed";
50 return true;
51 }
52 return value == CAP_SET;
53}
54
55void __attribute__((noreturn)) RebootSystem(unsigned int cmd, const std::string& rebootTarget) {
56 LOG(INFO) << "Reboot ending, jumping to kernel";
57
58 if (!IsRebootCapable()) {
59 // On systems where init does not have the capability of rebooting the
60 // device, just exit cleanly.
61 exit(0);
62 }
63
64 switch (cmd) {
65 case ANDROID_RB_POWEROFF:
66 reboot(RB_POWER_OFF);
67 break;
68
69 case ANDROID_RB_RESTART2:
70 syscall(__NR_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,
71 LINUX_REBOOT_CMD_RESTART2, rebootTarget.c_str());
72 break;
73
74 case ANDROID_RB_THERMOFF:
75 reboot(RB_POWER_OFF);
76 break;
77 }
78 // In normal case, reboot should not return.
79 PLOG(ERROR) << "reboot call returned";
80 abort();
81}
82
Tom Cherry59656fb2019-05-28 10:19:44 -070083void __attribute__((noreturn)) InitFatalReboot() {
84 auto pid = fork();
85
86 if (pid == -1) {
87 // Couldn't fork, don't even try to backtrace, just reboot.
88 RebootSystem(ANDROID_RB_RESTART2, "bootloader");
89 } else if (pid == 0) {
90 // Fork a child for safety, since we always want to shut down if something goes wrong, but
91 // its worth trying to get the backtrace, even in the signal handler, since typically it
92 // does work despite not being async-signal-safe.
93 sleep(5);
94 RebootSystem(ANDROID_RB_RESTART2, "bootloader");
95 }
96
97 // In the parent, let's try to get a backtrace then shutdown.
98 std::unique_ptr<Backtrace> backtrace(
99 Backtrace::Create(BACKTRACE_CURRENT_PROCESS, BACKTRACE_CURRENT_THREAD));
100 if (!backtrace->Unwind(0)) {
101 LOG(ERROR) << __FUNCTION__ << ": Failed to unwind callstack.";
102 }
103 for (size_t i = 0; i < backtrace->NumFrames(); i++) {
104 LOG(ERROR) << backtrace->FormatFrameData(i);
105 }
106 RebootSystem(ANDROID_RB_RESTART2, "bootloader");
107}
108
Tom Cherry44aceed2018-08-03 13:36:18 -0700109void InstallRebootSignalHandlers() {
110 // Instead of panic'ing the kernel as is the default behavior when init crashes,
111 // we prefer to reboot to bootloader on development builds, as this will prevent
112 // boot looping bad configurations and allow both developers and test farms to easily
113 // recover.
114 struct sigaction action;
115 memset(&action, 0, sizeof(action));
116 sigfillset(&action.sa_mask);
117 action.sa_handler = [](int signal) {
118 // These signal handlers are also caught for processes forked from init, however we do not
119 // want them to trigger reboot, so we directly call _exit() for children processes here.
120 if (getpid() != 1) {
121 _exit(signal);
122 }
123
124 // Calling DoReboot() or LOG(FATAL) is not a good option as this is a signal handler.
125 // RebootSystem uses syscall() which isn't actually async-signal-safe, but our only option
126 // and probably good enough given this is already an error case and only enabled for
127 // development builds.
Tom Cherry59656fb2019-05-28 10:19:44 -0700128 InitFatalReboot();
Tom Cherry44aceed2018-08-03 13:36:18 -0700129 };
130 action.sa_flags = SA_RESTART;
131 sigaction(SIGABRT, &action, nullptr);
132 sigaction(SIGBUS, &action, nullptr);
133 sigaction(SIGFPE, &action, nullptr);
134 sigaction(SIGILL, &action, nullptr);
135 sigaction(SIGSEGV, &action, nullptr);
136#if defined(SIGSTKFLT)
137 sigaction(SIGSTKFLT, &action, nullptr);
138#endif
139 sigaction(SIGSYS, &action, nullptr);
140 sigaction(SIGTRAP, &action, nullptr);
141}
142
143} // namespace init
144} // namespace android