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