blob: 242750a858acb19e2edc9b8f15be998eb2b68d92 [file] [log] [blame]
Keun-young Park8d01f632017-03-13 11:54:47 -07001/*
2 * Copyright (C) 2017 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 */
Tom Cherry3f5eaae52017-04-06 16:30:22 -070016
17#include "reboot.h"
18
Keun-young Park8d01f632017-03-13 11:54:47 -070019#include <dirent.h>
20#include <fcntl.h>
Keun-young Park2ba5c812017-03-29 12:54:40 -070021#include <linux/fs.h>
Keun-young Park8d01f632017-03-13 11:54:47 -070022#include <mntent.h>
Luis Hector Chavez519e5f02017-06-29 09:50:30 -070023#include <sys/capability.h>
Keun-young Park8d01f632017-03-13 11:54:47 -070024#include <sys/cdefs.h>
Keun-young Park2ba5c812017-03-29 12:54:40 -070025#include <sys/ioctl.h>
Keun-young Park8d01f632017-03-13 11:54:47 -070026#include <sys/mount.h>
Keun-young Park8d01f632017-03-13 11:54:47 -070027#include <sys/reboot.h>
28#include <sys/stat.h>
29#include <sys/syscall.h>
30#include <sys/types.h>
31#include <sys/wait.h>
32
33#include <memory>
Keun-young Park7830d592017-03-27 16:07:02 -070034#include <set>
Keun-young Park8d01f632017-03-13 11:54:47 -070035#include <thread>
36#include <vector>
37
Tom Cherryede0d532017-07-06 14:20:11 -070038#include <android-base/chrono_utils.h>
Keun-young Park8d01f632017-03-13 11:54:47 -070039#include <android-base/file.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070040#include <android-base/logging.h>
Keun-young Park8d01f632017-03-13 11:54:47 -070041#include <android-base/macros.h>
Tom Cherryccf23532017-03-28 16:40:41 -070042#include <android-base/properties.h>
Keun-young Park8d01f632017-03-13 11:54:47 -070043#include <android-base/stringprintf.h>
44#include <android-base/strings.h>
Keun-young Park2ba5c812017-03-29 12:54:40 -070045#include <android-base/unique_fd.h>
Keun-young Park8d01f632017-03-13 11:54:47 -070046#include <bootloader_message/bootloader_message.h>
47#include <cutils/android_reboot.h>
Keun-young Park8d01f632017-03-13 11:54:47 -070048#include <fs_mgr.h>
49#include <logwrap/logwrap.h>
Todd Poynorfc827be2017-04-13 15:17:24 -070050#include <private/android_filesystem_config.h>
Tom Cherry0c8d6d22017-08-10 12:22:44 -070051#include <selinux/selinux.h>
Keun-young Park8d01f632017-03-13 11:54:47 -070052
Tom Cherry7fd3bc22018-02-13 15:36:14 -080053#include "action_manager.h"
Luis Hector Chavez519e5f02017-06-29 09:50:30 -070054#include "capabilities.h"
Wei Wangeeab4912017-06-27 22:08:45 -070055#include "init.h"
Keun-young Park7830d592017-03-27 16:07:02 -070056#include "property_service.h"
Keun-young Park8d01f632017-03-13 11:54:47 -070057#include "service.h"
Luis Hector Chavez9f97f472017-09-06 13:43:57 -070058#include "sigchld_handler.h"
Keun-young Park8d01f632017-03-13 11:54:47 -070059
Mark Salyzyn62909822017-10-09 09:27:16 -070060using android::base::Split;
Keun-young Park8d01f632017-03-13 11:54:47 -070061using android::base::StringPrintf;
Tom Cherryede0d532017-07-06 14:20:11 -070062using android::base::Timer;
Keun-young Park8d01f632017-03-13 11:54:47 -070063
Tom Cherry81f5d3e2017-06-22 12:53:17 -070064namespace android {
65namespace init {
66
Keun-young Park8d01f632017-03-13 11:54:47 -070067// represents umount status during reboot / shutdown.
68enum UmountStat {
69 /* umount succeeded. */
70 UMOUNT_STAT_SUCCESS = 0,
71 /* umount was not run. */
72 UMOUNT_STAT_SKIPPED = 1,
73 /* umount failed with timeout. */
74 UMOUNT_STAT_TIMEOUT = 2,
75 /* could not run due to error */
76 UMOUNT_STAT_ERROR = 3,
77 /* not used by init but reserved for other part to use this to represent the
78 the state where umount status before reboot is not found / available. */
79 UMOUNT_STAT_NOT_AVAILABLE = 4,
80};
81
82// Utility for struct mntent
83class MountEntry {
84 public:
Keun-young Park2ba5c812017-03-29 12:54:40 -070085 explicit MountEntry(const mntent& entry)
Keun-young Park8d01f632017-03-13 11:54:47 -070086 : mnt_fsname_(entry.mnt_fsname),
87 mnt_dir_(entry.mnt_dir),
88 mnt_type_(entry.mnt_type),
Keun-young Park2ba5c812017-03-29 12:54:40 -070089 mnt_opts_(entry.mnt_opts) {}
Keun-young Park8d01f632017-03-13 11:54:47 -070090
Jaegeuk Kim0f04f722017-09-25 10:55:39 -070091 bool Umount(bool force) {
92 int r = umount2(mnt_dir_.c_str(), force ? MNT_FORCE : 0);
Keun-young Park2ba5c812017-03-29 12:54:40 -070093 if (r == 0) {
94 LOG(INFO) << "umounted " << mnt_fsname_ << ":" << mnt_dir_ << " opts " << mnt_opts_;
95 return true;
96 } else {
97 PLOG(WARNING) << "cannot umount " << mnt_fsname_ << ":" << mnt_dir_ << " opts "
98 << mnt_opts_;
99 return false;
100 }
101 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700102
Keun-young Park2ba5c812017-03-29 12:54:40 -0700103 void DoFsck() {
104 int st;
105 if (IsF2Fs()) {
106 const char* f2fs_argv[] = {
107 "/system/bin/fsck.f2fs", "-f", mnt_fsname_.c_str(),
108 };
109 android_fork_execvp_ext(arraysize(f2fs_argv), (char**)f2fs_argv, &st, true, LOG_KLOG,
110 true, nullptr, nullptr, 0);
111 } else if (IsExt4()) {
112 const char* ext4_argv[] = {
113 "/system/bin/e2fsck", "-f", "-y", mnt_fsname_.c_str(),
114 };
115 android_fork_execvp_ext(arraysize(ext4_argv), (char**)ext4_argv, &st, true, LOG_KLOG,
116 true, nullptr, nullptr, 0);
117 }
118 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700119
120 static bool IsBlockDevice(const struct mntent& mntent) {
121 return android::base::StartsWith(mntent.mnt_fsname, "/dev/block");
122 }
123
124 static bool IsEmulatedDevice(const struct mntent& mntent) {
Keun-young Park2ba5c812017-03-29 12:54:40 -0700125 return android::base::StartsWith(mntent.mnt_fsname, "/data/");
Keun-young Park8d01f632017-03-13 11:54:47 -0700126 }
127
128 private:
Keun-young Park2ba5c812017-03-29 12:54:40 -0700129 bool IsF2Fs() const { return mnt_type_ == "f2fs"; }
130
131 bool IsExt4() const { return mnt_type_ == "ext4"; }
132
Keun-young Park8d01f632017-03-13 11:54:47 -0700133 std::string mnt_fsname_;
134 std::string mnt_dir_;
135 std::string mnt_type_;
Keun-young Park2ba5c812017-03-29 12:54:40 -0700136 std::string mnt_opts_;
Keun-young Park8d01f632017-03-13 11:54:47 -0700137};
138
139// Turn off backlight while we are performing power down cleanup activities.
140static void TurnOffBacklight() {
Steven Morelandd5eccfd2018-01-19 13:01:53 -0800141 Service* service = ServiceList::GetInstance().FindService("blank_screen");
142 if (service == nullptr) {
143 LOG(WARNING) << "cannot find blank_screen in TurnOffBacklight";
Keun-young Park8d01f632017-03-13 11:54:47 -0700144 return;
145 }
Steven Morelandd5eccfd2018-01-19 13:01:53 -0800146 service->Start();
Keun-young Park8d01f632017-03-13 11:54:47 -0700147}
148
Keun-young Park8d01f632017-03-13 11:54:47 -0700149static void ShutdownVold() {
150 const char* vdc_argv[] = {"/system/bin/vdc", "volume", "shutdown"};
151 int status;
152 android_fork_execvp_ext(arraysize(vdc_argv), (char**)vdc_argv, &status, true, LOG_KLOG, true,
153 nullptr, nullptr, 0);
154}
155
156static void LogShutdownTime(UmountStat stat, Timer* t) {
Tom Cherryede0d532017-07-06 14:20:11 -0700157 LOG(WARNING) << "powerctl_shutdown_time_ms:" << std::to_string(t->duration().count()) << ":"
158 << stat;
Keun-young Park8d01f632017-03-13 11:54:47 -0700159}
160
Luis Hector Chavez9f97f472017-09-06 13:43:57 -0700161bool IsRebootCapable() {
Luis Hector Chavez519e5f02017-06-29 09:50:30 -0700162 if (!CAP_IS_SUPPORTED(CAP_SYS_BOOT)) {
163 PLOG(WARNING) << "CAP_SYS_BOOT is not supported";
164 return true;
165 }
166
167 ScopedCaps caps(cap_get_proc());
168 if (!caps) {
169 PLOG(WARNING) << "cap_get_proc() failed";
170 return true;
171 }
172
173 cap_flag_value_t value = CAP_SET;
174 if (cap_get_flag(caps.get(), CAP_SYS_BOOT, CAP_EFFECTIVE, &value) != 0) {
175 PLOG(WARNING) << "cap_get_flag(CAP_SYS_BOOT, EFFECTIVE) failed";
176 return true;
177 }
178 return value == CAP_SET;
179}
180
Tom Cherryd8db7ab2017-08-17 17:28:30 -0700181void __attribute__((noreturn)) RebootSystem(unsigned int cmd, const std::string& rebootTarget) {
Keun-young Park3cd8c6f2017-03-23 15:33:16 -0700182 LOG(INFO) << "Reboot ending, jumping to kernel";
Luis Hector Chavez519e5f02017-06-29 09:50:30 -0700183
184 if (!IsRebootCapable()) {
185 // On systems where init does not have the capability of rebooting the
186 // device, just exit cleanly.
187 exit(0);
188 }
189
Keun-young Park8d01f632017-03-13 11:54:47 -0700190 switch (cmd) {
191 case ANDROID_RB_POWEROFF:
192 reboot(RB_POWER_OFF);
193 break;
194
195 case ANDROID_RB_RESTART2:
196 syscall(__NR_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,
197 LINUX_REBOOT_CMD_RESTART2, rebootTarget.c_str());
198 break;
199
200 case ANDROID_RB_THERMOFF:
201 reboot(RB_POWER_OFF);
202 break;
203 }
204 // In normal case, reboot should not return.
Tom Cherryd8db7ab2017-08-17 17:28:30 -0700205 PLOG(ERROR) << "reboot call returned";
Keun-young Park8d01f632017-03-13 11:54:47 -0700206 abort();
207}
208
209/* Find all read+write block devices and emulated devices in /proc/mounts
210 * and add them to correpsponding list.
211 */
212static bool FindPartitionsToUmount(std::vector<MountEntry>* blockDevPartitions,
Keun-young Park2ba5c812017-03-29 12:54:40 -0700213 std::vector<MountEntry>* emulatedPartitions, bool dump) {
Keun-young Park8d01f632017-03-13 11:54:47 -0700214 std::unique_ptr<std::FILE, int (*)(std::FILE*)> fp(setmntent("/proc/mounts", "r"), endmntent);
215 if (fp == nullptr) {
216 PLOG(ERROR) << "Failed to open /proc/mounts";
217 return false;
218 }
219 mntent* mentry;
220 while ((mentry = getmntent(fp.get())) != nullptr) {
Keun-young Park2ba5c812017-03-29 12:54:40 -0700221 if (dump) {
222 LOG(INFO) << "mount entry " << mentry->mnt_fsname << ":" << mentry->mnt_dir << " opts "
223 << mentry->mnt_opts << " type " << mentry->mnt_type;
224 } else if (MountEntry::IsBlockDevice(*mentry) && hasmntopt(mentry, "rw")) {
Keun-young Park6e12b382017-07-17 12:20:33 -0700225 std::string mount_dir(mentry->mnt_dir);
226 // These are R/O partitions changed to R/W after adb remount.
227 // Do not umount them as shutdown critical services may rely on them.
Wei Wanga01c27e2017-07-25 10:52:08 -0700228 if (mount_dir != "/" && mount_dir != "/system" && mount_dir != "/vendor" &&
229 mount_dir != "/oem") {
Keun-young Park6e12b382017-07-17 12:20:33 -0700230 blockDevPartitions->emplace(blockDevPartitions->begin(), *mentry);
231 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700232 } else if (MountEntry::IsEmulatedDevice(*mentry)) {
Keun-young Park2ba5c812017-03-29 12:54:40 -0700233 emulatedPartitions->emplace(emulatedPartitions->begin(), *mentry);
Keun-young Park8d01f632017-03-13 11:54:47 -0700234 }
235 }
236 return true;
237}
238
Keun-young Park1663e972017-04-21 17:29:26 -0700239static void DumpUmountDebuggingInfo(bool dump_all) {
Keun-young Park2ba5c812017-03-29 12:54:40 -0700240 int status;
241 if (!security_getenforce()) {
242 LOG(INFO) << "Run lsof";
243 const char* lsof_argv[] = {"/system/bin/lsof"};
244 android_fork_execvp_ext(arraysize(lsof_argv), (char**)lsof_argv, &status, true, LOG_KLOG,
245 true, nullptr, nullptr, 0);
Keun-young Park8d01f632017-03-13 11:54:47 -0700246 }
Keun-young Park2ba5c812017-03-29 12:54:40 -0700247 FindPartitionsToUmount(nullptr, nullptr, true);
Keun-young Park1663e972017-04-21 17:29:26 -0700248 if (dump_all) {
249 // dump current tasks, this log can be lengthy, so only dump with dump_all
250 android::base::WriteStringToFile("t", "/proc/sysrq-trigger");
251 }
Keun-young Park2ba5c812017-03-29 12:54:40 -0700252}
253
Tom Cherryede0d532017-07-06 14:20:11 -0700254static UmountStat UmountPartitions(std::chrono::milliseconds timeout) {
Keun-young Park2ba5c812017-03-29 12:54:40 -0700255 Timer t;
Keun-young Park2ba5c812017-03-29 12:54:40 -0700256 /* data partition needs all pending writes to be completed and all emulated partitions
257 * umounted.If the current waiting is not good enough, give
258 * up and leave it to e2fsck after reboot to fix it.
259 */
260 while (true) {
261 std::vector<MountEntry> block_devices;
262 std::vector<MountEntry> emulated_devices;
263 if (!FindPartitionsToUmount(&block_devices, &emulated_devices, false)) {
264 return UMOUNT_STAT_ERROR;
265 }
266 if (block_devices.size() == 0) {
Wei Wang8c00e422017-08-16 14:01:46 -0700267 return UMOUNT_STAT_SUCCESS;
Keun-young Park2ba5c812017-03-29 12:54:40 -0700268 }
Wei Wang8c00e422017-08-16 14:01:46 -0700269 bool unmount_done = true;
270 if (emulated_devices.size() > 0) {
Wei Wang25dc30f2017-10-23 15:32:03 -0700271 for (auto& entry : emulated_devices) {
272 if (!entry.Umount(false)) unmount_done = false;
273 }
Wei Wang8c00e422017-08-16 14:01:46 -0700274 if (unmount_done) {
275 sync();
276 }
Keun-young Park2ba5c812017-03-29 12:54:40 -0700277 }
Wei Wang25dc30f2017-10-23 15:32:03 -0700278 for (auto& entry : block_devices) {
279 if (!entry.Umount(timeout == 0ms)) unmount_done = false;
280 }
Wei Wang8c00e422017-08-16 14:01:46 -0700281 if (unmount_done) {
282 return UMOUNT_STAT_SUCCESS;
Keun-young Park2ba5c812017-03-29 12:54:40 -0700283 }
Wei Wang8c00e422017-08-16 14:01:46 -0700284 if ((timeout < t.duration())) { // try umount at least once
285 return UMOUNT_STAT_TIMEOUT;
Keun-young Park2ba5c812017-03-29 12:54:40 -0700286 }
Keun-young Park2ba5c812017-03-29 12:54:40 -0700287 std::this_thread::sleep_for(100ms);
288 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700289}
290
Keun-young Park3ee0df92017-03-27 11:21:09 -0700291static void KillAllProcesses() { android::base::WriteStringToFile("i", "/proc/sysrq-trigger"); }
292
Keun-young Park8d01f632017-03-13 11:54:47 -0700293/* Try umounting all emulated file systems R/W block device cfile systems.
294 * This will just try umount and give it up if it fails.
295 * For fs like ext4, this is ok as file system will be marked as unclean shutdown
296 * and necessary check can be done at the next reboot.
297 * For safer shutdown, caller needs to make sure that
298 * all processes / emulated partition for the target fs are all cleaned-up.
299 *
300 * return true when umount was successful. false when timed out.
301 */
Tom Cherryede0d532017-07-06 14:20:11 -0700302static UmountStat TryUmountAndFsck(bool runFsck, std::chrono::milliseconds timeout) {
Keun-young Park3ee0df92017-03-27 11:21:09 -0700303 Timer t;
Keun-young Park2ba5c812017-03-29 12:54:40 -0700304 std::vector<MountEntry> block_devices;
305 std::vector<MountEntry> emulated_devices;
Keun-young Park8d01f632017-03-13 11:54:47 -0700306
Keun-young Park2ba5c812017-03-29 12:54:40 -0700307 if (runFsck && !FindPartitionsToUmount(&block_devices, &emulated_devices, false)) {
Keun-young Park8d01f632017-03-13 11:54:47 -0700308 return UMOUNT_STAT_ERROR;
309 }
Keun-young Park2ba5c812017-03-29 12:54:40 -0700310
Tom Cherryede0d532017-07-06 14:20:11 -0700311 UmountStat stat = UmountPartitions(timeout - t.duration());
Keun-young Park2ba5c812017-03-29 12:54:40 -0700312 if (stat != UMOUNT_STAT_SUCCESS) {
313 LOG(INFO) << "umount timeout, last resort, kill all and try";
Keun-young Parkc59b8222017-07-18 18:52:25 -0700314 if (DUMP_ON_UMOUNT_FAILURE) DumpUmountDebuggingInfo(true);
Keun-young Park3ee0df92017-03-27 11:21:09 -0700315 KillAllProcesses();
Keun-young Park2ba5c812017-03-29 12:54:40 -0700316 // even if it succeeds, still it is timeout and do not run fsck with all processes killed
Keun-young Parkc59b8222017-07-18 18:52:25 -0700317 UmountStat st = UmountPartitions(0ms);
318 if ((st != UMOUNT_STAT_SUCCESS) && DUMP_ON_UMOUNT_FAILURE) DumpUmountDebuggingInfo(false);
Keun-young Park8d01f632017-03-13 11:54:47 -0700319 }
320
Keun-young Park2ba5c812017-03-29 12:54:40 -0700321 if (stat == UMOUNT_STAT_SUCCESS && runFsck) {
322 // fsck part is excluded from timeout check. It only runs for user initiated shutdown
323 // and should not affect reboot time.
324 for (auto& entry : block_devices) {
325 entry.DoFsck();
326 }
327 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700328 return stat;
329}
330
Keun-young Park8d01f632017-03-13 11:54:47 -0700331void DoReboot(unsigned int cmd, const std::string& reason, const std::string& rebootTarget,
332 bool runFsck) {
333 Timer t;
Keun-young Park3cd8c6f2017-03-23 15:33:16 -0700334 LOG(INFO) << "Reboot start, reason: " << reason << ", rebootTarget: " << rebootTarget;
Keun-young Park8d01f632017-03-13 11:54:47 -0700335
Mark Salyzyn62909822017-10-09 09:27:16 -0700336 // Ensure last reboot reason is reduced to canonical
337 // alias reported in bootloader or system boot reason.
338 size_t skip = 0;
339 std::vector<std::string> reasons = Split(reason, ",");
340 if (reasons.size() >= 2 && reasons[0] == "reboot" &&
341 (reasons[1] == "recovery" || reasons[1] == "bootloader" || reasons[1] == "cold" ||
342 reasons[1] == "hard" || reasons[1] == "warm")) {
343 skip = strlen("reboot,");
344 }
345 property_set(LAST_REBOOT_REASON_PROPERTY, reason.c_str() + skip);
Mark Salyzyn73e6b492017-08-14 15:56:53 -0700346 sync();
Keun-young Park8d01f632017-03-13 11:54:47 -0700347
Mark Salyzyn73e6b492017-08-14 15:56:53 -0700348 bool is_thermal_shutdown = cmd == ANDROID_RB_THERMOFF;
Keun-young Parkaa08ea42017-03-23 13:27:28 -0700349
Keun-young Park30173872017-07-18 10:58:28 -0700350 auto shutdown_timeout = 0ms;
Tom Cherryede0d532017-07-06 14:20:11 -0700351 if (!SHUTDOWN_ZERO_TIMEOUT) {
Keun-young Park30173872017-07-18 10:58:28 -0700352 if (is_thermal_shutdown) {
353 constexpr unsigned int thermal_shutdown_timeout = 1;
354 shutdown_timeout = std::chrono::seconds(thermal_shutdown_timeout);
355 } else {
356 constexpr unsigned int shutdown_timeout_default = 6;
357 auto shutdown_timeout_property = android::base::GetUintProperty(
358 "ro.build.shutdown_timeout", shutdown_timeout_default);
359 shutdown_timeout = std::chrono::seconds(shutdown_timeout_property);
360 }
Keun-young Parkc4ffa5c2017-03-28 09:41:36 -0700361 }
Keun-young Park30173872017-07-18 10:58:28 -0700362 LOG(INFO) << "Shutdown timeout: " << shutdown_timeout.count() << " ms";
Keun-young Parkaa08ea42017-03-23 13:27:28 -0700363
Keun-young Park7830d592017-03-27 16:07:02 -0700364 // keep debugging tools until non critical ones are all gone.
365 const std::set<std::string> kill_after_apps{"tombstoned", "logd", "adbd"};
366 // watchdogd is a vendor specific component but should be alive to complete shutdown safely.
Keun-young Parkcccb34f2017-07-05 11:38:44 -0700367 const std::set<std::string> to_starts{"watchdogd"};
Tom Cherry911b9b12017-07-27 16:20:58 -0700368 for (const auto& s : ServiceList::GetInstance()) {
Keun-young Park7830d592017-03-27 16:07:02 -0700369 if (kill_after_apps.count(s->name())) {
370 s->SetShutdownCritical();
371 } else if (to_starts.count(s->name())) {
Tom Cherry702ca9a2017-08-25 10:36:52 -0700372 if (auto result = s->Start(); !result) {
373 LOG(ERROR) << "Could not start shutdown 'to_start' service '" << s->name()
374 << "': " << result.error();
375 }
Keun-young Park7830d592017-03-27 16:07:02 -0700376 s->SetShutdownCritical();
Keun-young Parkcccb34f2017-07-05 11:38:44 -0700377 } else if (s->IsShutdownCritical()) {
Tom Cherry702ca9a2017-08-25 10:36:52 -0700378 // Start shutdown critical service if not started.
379 if (auto result = s->Start(); !result) {
380 LOG(ERROR) << "Could not start shutdown critical service '" << s->name()
381 << "': " << result.error();
382 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700383 }
Tom Cherry911b9b12017-07-27 16:20:58 -0700384 }
Keun-young Park7830d592017-03-27 16:07:02 -0700385
Steven Morelandd5eccfd2018-01-19 13:01:53 -0800386 // remaining operations (specifically fsck) may take a substantial duration
387 if (cmd == ANDROID_RB_POWEROFF || is_thermal_shutdown) {
388 TurnOffBacklight();
389 }
390
Tom Cherry911b9b12017-07-27 16:20:58 -0700391 Service* bootAnim = ServiceList::GetInstance().FindService("bootanim");
392 Service* surfaceFlinger = ServiceList::GetInstance().FindService("surfaceflinger");
Keun-young Park7830d592017-03-27 16:07:02 -0700393 if (bootAnim != nullptr && surfaceFlinger != nullptr && surfaceFlinger->IsRunning()) {
Tom Cherry911b9b12017-07-27 16:20:58 -0700394 // will not check animation class separately
395 for (const auto& service : ServiceList::GetInstance()) {
396 if (service->classnames().count("animation")) service->SetShutdownCritical();
397 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700398 }
Keun-young Park7830d592017-03-27 16:07:02 -0700399
Keun-young Park8d01f632017-03-13 11:54:47 -0700400 // optional shutdown step
401 // 1. terminate all services except shutdown critical ones. wait for delay to finish
Keun-young Park30173872017-07-18 10:58:28 -0700402 if (shutdown_timeout > 0ms) {
Keun-young Park8d01f632017-03-13 11:54:47 -0700403 LOG(INFO) << "terminating init services";
Keun-young Park8d01f632017-03-13 11:54:47 -0700404
405 // Ask all services to terminate except shutdown critical ones.
Tom Cherry911b9b12017-07-27 16:20:58 -0700406 for (const auto& s : ServiceList::GetInstance().services_in_shutdown_order()) {
Keun-young Park8d01f632017-03-13 11:54:47 -0700407 if (!s->IsShutdownCritical()) s->Terminate();
Tom Cherry911b9b12017-07-27 16:20:58 -0700408 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700409
410 int service_count = 0;
Keun-young Park30173872017-07-18 10:58:28 -0700411 // Only wait up to half of timeout here
412 auto termination_wait_timeout = shutdown_timeout / 2;
Tom Cherryede0d532017-07-06 14:20:11 -0700413 while (t.duration() < termination_wait_timeout) {
Tom Cherryeeee8312017-07-28 15:22:23 -0700414 ReapAnyOutstandingChildren();
Keun-young Park8d01f632017-03-13 11:54:47 -0700415
416 service_count = 0;
Tom Cherry911b9b12017-07-27 16:20:58 -0700417 for (const auto& s : ServiceList::GetInstance()) {
Keun-young Park8d01f632017-03-13 11:54:47 -0700418 // Count the number of services running except shutdown critical.
419 // Exclude the console as it will ignore the SIGTERM signal
420 // and not exit.
421 // Note: SVC_CONSOLE actually means "requires console" but
422 // it is only used by the shell.
423 if (!s->IsShutdownCritical() && s->pid() != 0 && (s->flags() & SVC_CONSOLE) == 0) {
424 service_count++;
425 }
Tom Cherry911b9b12017-07-27 16:20:58 -0700426 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700427
428 if (service_count == 0) {
429 // All terminable services terminated. We can exit early.
430 break;
431 }
432
433 // Wait a bit before recounting the number or running services.
434 std::this_thread::sleep_for(50ms);
435 }
436 LOG(INFO) << "Terminating running services took " << t
437 << " with remaining services:" << service_count;
438 }
439
440 // minimum safety steps before restarting
441 // 2. kill all services except ones that are necessary for the shutdown sequence.
Tom Cherry911b9b12017-07-27 16:20:58 -0700442 for (const auto& s : ServiceList::GetInstance().services_in_shutdown_order()) {
Keun-young Park2ba5c812017-03-29 12:54:40 -0700443 if (!s->IsShutdownCritical()) s->Stop();
Tom Cherry911b9b12017-07-27 16:20:58 -0700444 }
Tom Cherryeeee8312017-07-28 15:22:23 -0700445 ReapAnyOutstandingChildren();
Keun-young Park8d01f632017-03-13 11:54:47 -0700446
447 // 3. send volume shutdown to vold
Tom Cherry911b9b12017-07-27 16:20:58 -0700448 Service* voldService = ServiceList::GetInstance().FindService("vold");
Keun-young Park8d01f632017-03-13 11:54:47 -0700449 if (voldService != nullptr && voldService->IsRunning()) {
450 ShutdownVold();
Keun-young Park2ba5c812017-03-29 12:54:40 -0700451 voldService->Stop();
Keun-young Park8d01f632017-03-13 11:54:47 -0700452 } else {
453 LOG(INFO) << "vold not running, skipping vold shutdown";
454 }
Keun-young Park2ba5c812017-03-29 12:54:40 -0700455 // logcat stopped here
Tom Cherry911b9b12017-07-27 16:20:58 -0700456 for (const auto& s : ServiceList::GetInstance().services_in_shutdown_order()) {
Keun-young Park2ba5c812017-03-29 12:54:40 -0700457 if (kill_after_apps.count(s->name())) s->Stop();
Tom Cherry911b9b12017-07-27 16:20:58 -0700458 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700459 // 4. sync, try umount, and optionally run fsck for user shutdown
Keun-young Park2ba5c812017-03-29 12:54:40 -0700460 sync();
Tom Cherryede0d532017-07-06 14:20:11 -0700461 UmountStat stat = TryUmountAndFsck(runFsck, shutdown_timeout - t.duration());
Keun-young Park2ba5c812017-03-29 12:54:40 -0700462 // Follow what linux shutdown is doing: one more sync with little bit delay
463 sync();
Keun-young Park30173872017-07-18 10:58:28 -0700464 if (!is_thermal_shutdown) std::this_thread::sleep_for(100ms);
Keun-young Park8d01f632017-03-13 11:54:47 -0700465 LogShutdownTime(stat, &t);
466 // Reboot regardless of umount status. If umount fails, fsck after reboot will fix it.
467 RebootSystem(cmd, rebootTarget);
468 abort();
469}
Tom Cherry98ad32a2017-04-17 16:34:20 -0700470
471bool HandlePowerctlMessage(const std::string& command) {
472 unsigned int cmd = 0;
Mark Salyzyn62909822017-10-09 09:27:16 -0700473 std::vector<std::string> cmd_params = Split(command, ",");
Tom Cherry98ad32a2017-04-17 16:34:20 -0700474 std::string reboot_target = "";
475 bool run_fsck = false;
476 bool command_invalid = false;
477
478 if (cmd_params.size() > 3) {
479 command_invalid = true;
480 } else if (cmd_params[0] == "shutdown") {
481 cmd = ANDROID_RB_POWEROFF;
Mark Salyzyn73e6b492017-08-14 15:56:53 -0700482 if (cmd_params.size() == 2) {
483 if (cmd_params[1] == "userrequested") {
484 // The shutdown reason is PowerManager.SHUTDOWN_USER_REQUESTED.
485 // Run fsck once the file system is remounted in read-only mode.
486 run_fsck = true;
487 } else if (cmd_params[1] == "thermal") {
Mark Salyzynbfd05b62017-09-26 11:10:12 -0700488 // Turn off sources of heat immediately.
489 TurnOffBacklight();
Mark Salyzyn73e6b492017-08-14 15:56:53 -0700490 // run_fsck is false to avoid delay
491 cmd = ANDROID_RB_THERMOFF;
492 }
Tom Cherry98ad32a2017-04-17 16:34:20 -0700493 }
494 } else if (cmd_params[0] == "reboot") {
495 cmd = ANDROID_RB_RESTART2;
496 if (cmd_params.size() >= 2) {
497 reboot_target = cmd_params[1];
498 // When rebooting to the bootloader notify the bootloader writing
499 // also the BCB.
500 if (reboot_target == "bootloader") {
501 std::string err;
502 if (!write_reboot_bootloader(&err)) {
503 LOG(ERROR) << "reboot-bootloader: Error writing "
504 "bootloader_message: "
505 << err;
506 }
507 }
Mark Salyzyn73e6b492017-08-14 15:56:53 -0700508 // If there is an additional parameter, pass it along
509 if ((cmd_params.size() == 3) && cmd_params[2].size()) {
Tom Cherry98ad32a2017-04-17 16:34:20 -0700510 reboot_target += "," + cmd_params[2];
511 }
512 }
Tom Cherry98ad32a2017-04-17 16:34:20 -0700513 } else {
514 command_invalid = true;
515 }
516 if (command_invalid) {
517 LOG(ERROR) << "powerctl: unrecognized command '" << command << "'";
518 return false;
519 }
520
Wei Wangeeab4912017-06-27 22:08:45 -0700521 LOG(INFO) << "Clear action queue and start shutdown trigger";
522 ActionManager::GetInstance().ClearQueue();
523 // Queue shutdown trigger first
524 ActionManager::GetInstance().QueueEventTrigger("shutdown");
525 // Queue built-in shutdown_done
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700526 auto shutdown_handler = [cmd, command, reboot_target, run_fsck](const BuiltinArguments&) {
Wei Wangeeab4912017-06-27 22:08:45 -0700527 DoReboot(cmd, command, reboot_target, run_fsck);
Tom Cherry557946e2017-08-01 13:50:23 -0700528 return Success();
Wei Wangeeab4912017-06-27 22:08:45 -0700529 };
530 ActionManager::GetInstance().QueueBuiltinAction(shutdown_handler, "shutdown_done");
531
532 // Skip wait for prop if it is in progress
533 ResetWaitForProp();
534
Tom Cherry3b81f2d2017-07-28 14:48:41 -0700535 // Clear EXEC flag if there is one pending
Tom Cherry911b9b12017-07-27 16:20:58 -0700536 for (const auto& s : ServiceList::GetInstance()) {
537 s->UnSetExec();
538 }
Wei Wangeeab4912017-06-27 22:08:45 -0700539
Tom Cherry98ad32a2017-04-17 16:34:20 -0700540 return true;
541}
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700542
543} // namespace init
544} // namespace android