| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 1 | /* | 
|  | 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 Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 16 |  | 
|  | 17 | #include "reboot.h" | 
|  | 18 |  | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 19 | #include <dirent.h> | 
|  | 20 | #include <fcntl.h> | 
| Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 21 | #include <linux/fs.h> | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 22 | #include <mntent.h> | 
| Jaegeuk Kim | 2aedc82 | 2018-11-20 13:27:06 -0800 | [diff] [blame] | 23 | #include <linux/loop.h> | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 24 | #include <sys/cdefs.h> | 
| Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 25 | #include <sys/ioctl.h> | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 26 | #include <sys/mount.h> | 
| Jaegeuk Kim | 2aedc82 | 2018-11-20 13:27:06 -0800 | [diff] [blame] | 27 | #include <sys/swap.h> | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 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 Park | 7830d59 | 2017-03-27 16:07:02 -0700 | [diff] [blame] | 34 | #include <set> | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 35 | #include <thread> | 
|  | 36 | #include <vector> | 
|  | 37 |  | 
| Tom Cherry | ede0d53 | 2017-07-06 14:20:11 -0700 | [diff] [blame] | 38 | #include <android-base/chrono_utils.h> | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 39 | #include <android-base/file.h> | 
| Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 40 | #include <android-base/logging.h> | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 41 | #include <android-base/macros.h> | 
| Tom Cherry | ccf2353 | 2017-03-28 16:40:41 -0700 | [diff] [blame] | 42 | #include <android-base/properties.h> | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 43 | #include <android-base/stringprintf.h> | 
|  | 44 | #include <android-base/strings.h> | 
| Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 45 | #include <android-base/unique_fd.h> | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 46 | #include <bootloader_message/bootloader_message.h> | 
|  | 47 | #include <cutils/android_reboot.h> | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 48 | #include <fs_mgr.h> | 
|  | 49 | #include <logwrap/logwrap.h> | 
| Todd Poynor | fc827be | 2017-04-13 15:17:24 -0700 | [diff] [blame] | 50 | #include <private/android_filesystem_config.h> | 
| Tom Cherry | 0c8d6d2 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 51 | #include <selinux/selinux.h> | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 52 |  | 
| Tom Cherry | 7fd3bc2 | 2018-02-13 15:36:14 -0800 | [diff] [blame] | 53 | #include "action_manager.h" | 
| Wei Wang | eeab491 | 2017-06-27 22:08:45 -0700 | [diff] [blame] | 54 | #include "init.h" | 
| Keun-young Park | 7830d59 | 2017-03-27 16:07:02 -0700 | [diff] [blame] | 55 | #include "property_service.h" | 
| Tom Cherry | 44aceed | 2018-08-03 13:36:18 -0700 | [diff] [blame] | 56 | #include "reboot_utils.h" | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 57 | #include "service.h" | 
| Luis Hector Chavez | 9f97f47 | 2017-09-06 13:43:57 -0700 | [diff] [blame] | 58 | #include "sigchld_handler.h" | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 59 |  | 
| Tom Cherry | 4f4cacc | 2019-01-08 10:39:00 -0800 | [diff] [blame] | 60 | using android::base::GetBoolProperty; | 
| Mark Salyzyn | 6290982 | 2017-10-09 09:27:16 -0700 | [diff] [blame] | 61 | using android::base::Split; | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 62 | using android::base::StringPrintf; | 
| Tom Cherry | ede0d53 | 2017-07-06 14:20:11 -0700 | [diff] [blame] | 63 | using android::base::Timer; | 
| Jaegeuk Kim | 2aedc82 | 2018-11-20 13:27:06 -0800 | [diff] [blame] | 64 | using android::base::unique_fd; | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 65 |  | 
| Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 66 | namespace android { | 
|  | 67 | namespace init { | 
|  | 68 |  | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 69 | // represents umount status during reboot / shutdown. | 
|  | 70 | enum UmountStat { | 
|  | 71 | /* umount succeeded. */ | 
|  | 72 | UMOUNT_STAT_SUCCESS = 0, | 
|  | 73 | /* umount was not run. */ | 
|  | 74 | UMOUNT_STAT_SKIPPED = 1, | 
|  | 75 | /* umount failed with timeout. */ | 
|  | 76 | UMOUNT_STAT_TIMEOUT = 2, | 
|  | 77 | /* could not run due to error */ | 
|  | 78 | UMOUNT_STAT_ERROR = 3, | 
|  | 79 | /* not used by init but reserved for other part to use this to represent the | 
|  | 80 | the state where umount status before reboot is not found / available. */ | 
|  | 81 | UMOUNT_STAT_NOT_AVAILABLE = 4, | 
|  | 82 | }; | 
|  | 83 |  | 
|  | 84 | // Utility for struct mntent | 
|  | 85 | class MountEntry { | 
|  | 86 | public: | 
| Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 87 | explicit MountEntry(const mntent& entry) | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 88 | : mnt_fsname_(entry.mnt_fsname), | 
|  | 89 | mnt_dir_(entry.mnt_dir), | 
|  | 90 | mnt_type_(entry.mnt_type), | 
| Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 91 | mnt_opts_(entry.mnt_opts) {} | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 92 |  | 
| Jaegeuk Kim | 0f04f72 | 2017-09-25 10:55:39 -0700 | [diff] [blame] | 93 | bool Umount(bool force) { | 
| Tom Cherry | c9fec9d | 2018-02-15 14:26:58 -0800 | [diff] [blame] | 94 | LOG(INFO) << "Unmounting " << mnt_fsname_ << ":" << mnt_dir_ << " opts " << mnt_opts_; | 
| Jaegeuk Kim | 0f04f72 | 2017-09-25 10:55:39 -0700 | [diff] [blame] | 95 | int r = umount2(mnt_dir_.c_str(), force ? MNT_FORCE : 0); | 
| Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 96 | if (r == 0) { | 
| Tom Cherry | c9fec9d | 2018-02-15 14:26:58 -0800 | [diff] [blame] | 97 | LOG(INFO) << "Umounted " << mnt_fsname_ << ":" << mnt_dir_ << " opts " << mnt_opts_; | 
| Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 98 | return true; | 
|  | 99 | } else { | 
| Tom Cherry | c9fec9d | 2018-02-15 14:26:58 -0800 | [diff] [blame] | 100 | PLOG(WARNING) << "Cannot umount " << mnt_fsname_ << ":" << mnt_dir_ << " opts " | 
| Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 101 | << mnt_opts_; | 
|  | 102 | return false; | 
|  | 103 | } | 
|  | 104 | } | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 105 |  | 
| Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 106 | void DoFsck() { | 
|  | 107 | int st; | 
|  | 108 | if (IsF2Fs()) { | 
|  | 109 | const char* f2fs_argv[] = { | 
| Randall Huang | df2faa4 | 2019-01-15 15:00:56 +0800 | [diff] [blame] | 110 | "/system/bin/fsck.f2fs", | 
|  | 111 | "-a", | 
|  | 112 | mnt_fsname_.c_str(), | 
| Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 113 | }; | 
|  | 114 | android_fork_execvp_ext(arraysize(f2fs_argv), (char**)f2fs_argv, &st, true, LOG_KLOG, | 
|  | 115 | true, nullptr, nullptr, 0); | 
|  | 116 | } else if (IsExt4()) { | 
|  | 117 | const char* ext4_argv[] = { | 
| Randall Huang | df2faa4 | 2019-01-15 15:00:56 +0800 | [diff] [blame] | 118 | "/system/bin/e2fsck", | 
|  | 119 | "-y", | 
|  | 120 | mnt_fsname_.c_str(), | 
| Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 121 | }; | 
|  | 122 | android_fork_execvp_ext(arraysize(ext4_argv), (char**)ext4_argv, &st, true, LOG_KLOG, | 
|  | 123 | true, nullptr, nullptr, 0); | 
|  | 124 | } | 
|  | 125 | } | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 126 |  | 
|  | 127 | static bool IsBlockDevice(const struct mntent& mntent) { | 
|  | 128 | return android::base::StartsWith(mntent.mnt_fsname, "/dev/block"); | 
|  | 129 | } | 
|  | 130 |  | 
|  | 131 | static bool IsEmulatedDevice(const struct mntent& mntent) { | 
| Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 132 | return android::base::StartsWith(mntent.mnt_fsname, "/data/"); | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 133 | } | 
|  | 134 |  | 
|  | 135 | private: | 
| Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 136 | bool IsF2Fs() const { return mnt_type_ == "f2fs"; } | 
|  | 137 |  | 
|  | 138 | bool IsExt4() const { return mnt_type_ == "ext4"; } | 
|  | 139 |  | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 140 | std::string mnt_fsname_; | 
|  | 141 | std::string mnt_dir_; | 
|  | 142 | std::string mnt_type_; | 
| Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 143 | std::string mnt_opts_; | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 144 | }; | 
|  | 145 |  | 
|  | 146 | // Turn off backlight while we are performing power down cleanup activities. | 
|  | 147 | static void TurnOffBacklight() { | 
| Steven Moreland | d5eccfd | 2018-01-19 13:01:53 -0800 | [diff] [blame] | 148 | Service* service = ServiceList::GetInstance().FindService("blank_screen"); | 
|  | 149 | if (service == nullptr) { | 
|  | 150 | LOG(WARNING) << "cannot find blank_screen in TurnOffBacklight"; | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 151 | return; | 
|  | 152 | } | 
| Tom Cherry | d987264 | 2018-10-11 10:38:05 -0700 | [diff] [blame] | 153 | if (auto result = service->Start(); !result) { | 
|  | 154 | LOG(WARNING) << "Could not start blank_screen service: " << result.error(); | 
|  | 155 | } | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 156 | } | 
|  | 157 |  | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 158 | static void ShutdownVold() { | 
|  | 159 | const char* vdc_argv[] = {"/system/bin/vdc", "volume", "shutdown"}; | 
|  | 160 | int status; | 
|  | 161 | android_fork_execvp_ext(arraysize(vdc_argv), (char**)vdc_argv, &status, true, LOG_KLOG, true, | 
|  | 162 | nullptr, nullptr, 0); | 
|  | 163 | } | 
|  | 164 |  | 
|  | 165 | static void LogShutdownTime(UmountStat stat, Timer* t) { | 
| Tom Cherry | ede0d53 | 2017-07-06 14:20:11 -0700 | [diff] [blame] | 166 | LOG(WARNING) << "powerctl_shutdown_time_ms:" << std::to_string(t->duration().count()) << ":" | 
|  | 167 | << stat; | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 168 | } | 
|  | 169 |  | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 170 | /* Find all read+write block devices and emulated devices in /proc/mounts | 
|  | 171 | * and add them to correpsponding list. | 
|  | 172 | */ | 
|  | 173 | static bool FindPartitionsToUmount(std::vector<MountEntry>* blockDevPartitions, | 
| Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 174 | std::vector<MountEntry>* emulatedPartitions, bool dump) { | 
| Tom Cherry | f274e78 | 2018-10-03 13:13:41 -0700 | [diff] [blame] | 175 | std::unique_ptr<std::FILE, int (*)(std::FILE*)> fp(setmntent("/proc/mounts", "re"), endmntent); | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 176 | if (fp == nullptr) { | 
|  | 177 | PLOG(ERROR) << "Failed to open /proc/mounts"; | 
|  | 178 | return false; | 
|  | 179 | } | 
|  | 180 | mntent* mentry; | 
|  | 181 | while ((mentry = getmntent(fp.get())) != nullptr) { | 
| Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 182 | if (dump) { | 
|  | 183 | LOG(INFO) << "mount entry " << mentry->mnt_fsname << ":" << mentry->mnt_dir << " opts " | 
|  | 184 | << mentry->mnt_opts << " type " << mentry->mnt_type; | 
|  | 185 | } else if (MountEntry::IsBlockDevice(*mentry) && hasmntopt(mentry, "rw")) { | 
| Keun-young Park | 6e12b38 | 2017-07-17 12:20:33 -0700 | [diff] [blame] | 186 | std::string mount_dir(mentry->mnt_dir); | 
|  | 187 | // These are R/O partitions changed to R/W after adb remount. | 
|  | 188 | // Do not umount them as shutdown critical services may rely on them. | 
| Wei Wang | a01c27e | 2017-07-25 10:52:08 -0700 | [diff] [blame] | 189 | if (mount_dir != "/" && mount_dir != "/system" && mount_dir != "/vendor" && | 
|  | 190 | mount_dir != "/oem") { | 
| Keun-young Park | 6e12b38 | 2017-07-17 12:20:33 -0700 | [diff] [blame] | 191 | blockDevPartitions->emplace(blockDevPartitions->begin(), *mentry); | 
|  | 192 | } | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 193 | } else if (MountEntry::IsEmulatedDevice(*mentry)) { | 
| Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 194 | emulatedPartitions->emplace(emulatedPartitions->begin(), *mentry); | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 195 | } | 
|  | 196 | } | 
|  | 197 | return true; | 
|  | 198 | } | 
|  | 199 |  | 
| Jonglin Lee | 28a2c92 | 2019-01-15 16:38:44 -0800 | [diff] [blame] | 200 | static void DumpUmountDebuggingInfo() { | 
| Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 201 | int status; | 
|  | 202 | if (!security_getenforce()) { | 
|  | 203 | LOG(INFO) << "Run lsof"; | 
|  | 204 | const char* lsof_argv[] = {"/system/bin/lsof"}; | 
|  | 205 | android_fork_execvp_ext(arraysize(lsof_argv), (char**)lsof_argv, &status, true, LOG_KLOG, | 
|  | 206 | true, nullptr, nullptr, 0); | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 207 | } | 
| Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 208 | FindPartitionsToUmount(nullptr, nullptr, true); | 
| Jonglin Lee | 28a2c92 | 2019-01-15 16:38:44 -0800 | [diff] [blame] | 209 | // dump current CPU stack traces and uninterruptible tasks | 
|  | 210 | android::base::WriteStringToFile("l", "/proc/sysrq-trigger"); | 
|  | 211 | android::base::WriteStringToFile("w", "/proc/sysrq-trigger"); | 
| Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 212 | } | 
|  | 213 |  | 
| Tom Cherry | ede0d53 | 2017-07-06 14:20:11 -0700 | [diff] [blame] | 214 | static UmountStat UmountPartitions(std::chrono::milliseconds timeout) { | 
| Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 215 | Timer t; | 
| Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 216 | /* data partition needs all pending writes to be completed and all emulated partitions | 
|  | 217 | * umounted.If the current waiting is not good enough, give | 
|  | 218 | * up and leave it to e2fsck after reboot to fix it. | 
|  | 219 | */ | 
|  | 220 | while (true) { | 
|  | 221 | std::vector<MountEntry> block_devices; | 
|  | 222 | std::vector<MountEntry> emulated_devices; | 
|  | 223 | if (!FindPartitionsToUmount(&block_devices, &emulated_devices, false)) { | 
|  | 224 | return UMOUNT_STAT_ERROR; | 
|  | 225 | } | 
|  | 226 | if (block_devices.size() == 0) { | 
| Wei Wang | 8c00e42 | 2017-08-16 14:01:46 -0700 | [diff] [blame] | 227 | return UMOUNT_STAT_SUCCESS; | 
| Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 228 | } | 
| Wei Wang | 8c00e42 | 2017-08-16 14:01:46 -0700 | [diff] [blame] | 229 | bool unmount_done = true; | 
|  | 230 | if (emulated_devices.size() > 0) { | 
| Wei Wang | 25dc30f | 2017-10-23 15:32:03 -0700 | [diff] [blame] | 231 | for (auto& entry : emulated_devices) { | 
|  | 232 | if (!entry.Umount(false)) unmount_done = false; | 
|  | 233 | } | 
| Wei Wang | 8c00e42 | 2017-08-16 14:01:46 -0700 | [diff] [blame] | 234 | if (unmount_done) { | 
|  | 235 | sync(); | 
|  | 236 | } | 
| Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 237 | } | 
| Wei Wang | 25dc30f | 2017-10-23 15:32:03 -0700 | [diff] [blame] | 238 | for (auto& entry : block_devices) { | 
|  | 239 | if (!entry.Umount(timeout == 0ms)) unmount_done = false; | 
|  | 240 | } | 
| Wei Wang | 8c00e42 | 2017-08-16 14:01:46 -0700 | [diff] [blame] | 241 | if (unmount_done) { | 
|  | 242 | return UMOUNT_STAT_SUCCESS; | 
| Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 243 | } | 
| Wei Wang | 8c00e42 | 2017-08-16 14:01:46 -0700 | [diff] [blame] | 244 | if ((timeout < t.duration())) {  // try umount at least once | 
|  | 245 | return UMOUNT_STAT_TIMEOUT; | 
| Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 246 | } | 
| Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 247 | std::this_thread::sleep_for(100ms); | 
|  | 248 | } | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 249 | } | 
|  | 250 |  | 
| Keun-young Park | 3ee0df9 | 2017-03-27 11:21:09 -0700 | [diff] [blame] | 251 | static void KillAllProcesses() { android::base::WriteStringToFile("i", "/proc/sysrq-trigger"); } | 
|  | 252 |  | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 253 | /* Try umounting all emulated file systems R/W block device cfile systems. | 
|  | 254 | * This will just try umount and give it up if it fails. | 
|  | 255 | * For fs like ext4, this is ok as file system will be marked as unclean shutdown | 
|  | 256 | * and necessary check can be done at the next reboot. | 
|  | 257 | * For safer shutdown, caller needs to make sure that | 
|  | 258 | * all processes / emulated partition for the target fs are all cleaned-up. | 
|  | 259 | * | 
|  | 260 | * return true when umount was successful. false when timed out. | 
|  | 261 | */ | 
| Tom Cherry | ede0d53 | 2017-07-06 14:20:11 -0700 | [diff] [blame] | 262 | static UmountStat TryUmountAndFsck(bool runFsck, std::chrono::milliseconds timeout) { | 
| Keun-young Park | 3ee0df9 | 2017-03-27 11:21:09 -0700 | [diff] [blame] | 263 | Timer t; | 
| Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 264 | std::vector<MountEntry> block_devices; | 
|  | 265 | std::vector<MountEntry> emulated_devices; | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 266 |  | 
| Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 267 | if (runFsck && !FindPartitionsToUmount(&block_devices, &emulated_devices, false)) { | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 268 | return UMOUNT_STAT_ERROR; | 
|  | 269 | } | 
| Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 270 |  | 
| Tom Cherry | ede0d53 | 2017-07-06 14:20:11 -0700 | [diff] [blame] | 271 | UmountStat stat = UmountPartitions(timeout - t.duration()); | 
| Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 272 | if (stat != UMOUNT_STAT_SUCCESS) { | 
|  | 273 | LOG(INFO) << "umount timeout, last resort, kill all and try"; | 
| Jonglin Lee | 28a2c92 | 2019-01-15 16:38:44 -0800 | [diff] [blame] | 274 | if (DUMP_ON_UMOUNT_FAILURE) DumpUmountDebuggingInfo(); | 
| Keun-young Park | 3ee0df9 | 2017-03-27 11:21:09 -0700 | [diff] [blame] | 275 | KillAllProcesses(); | 
| Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 276 | // even if it succeeds, still it is timeout and do not run fsck with all processes killed | 
| Keun-young Park | c59b822 | 2017-07-18 18:52:25 -0700 | [diff] [blame] | 277 | UmountStat st = UmountPartitions(0ms); | 
| Jonglin Lee | 28a2c92 | 2019-01-15 16:38:44 -0800 | [diff] [blame] | 278 | if ((st != UMOUNT_STAT_SUCCESS) && DUMP_ON_UMOUNT_FAILURE) DumpUmountDebuggingInfo(); | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 279 | } | 
|  | 280 |  | 
| Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 281 | if (stat == UMOUNT_STAT_SUCCESS && runFsck) { | 
|  | 282 | // fsck part is excluded from timeout check. It only runs for user initiated shutdown | 
|  | 283 | // and should not affect reboot time. | 
|  | 284 | for (auto& entry : block_devices) { | 
|  | 285 | entry.DoFsck(); | 
|  | 286 | } | 
|  | 287 | } | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 288 | return stat; | 
|  | 289 | } | 
|  | 290 |  | 
| Jaegeuk Kim | 2aedc82 | 2018-11-20 13:27:06 -0800 | [diff] [blame] | 291 | // zram is able to use backing device on top of a loopback device. | 
|  | 292 | // In order to unmount /data successfully, we have to kill the loopback device first | 
|  | 293 | #define ZRAM_DEVICE   "/dev/block/zram0" | 
|  | 294 | #define ZRAM_RESET    "/sys/block/zram0/reset" | 
|  | 295 | #define ZRAM_BACK_DEV "/sys/block/zram0/backing_dev" | 
|  | 296 | static void KillZramBackingDevice() { | 
|  | 297 | std::string backing_dev; | 
|  | 298 | if (!android::base::ReadFileToString(ZRAM_BACK_DEV, &backing_dev)) return; | 
|  | 299 |  | 
|  | 300 | if (!android::base::StartsWith(backing_dev, "/dev/block/loop")) return; | 
|  | 301 |  | 
|  | 302 | // cut the last "\n" | 
|  | 303 | backing_dev.erase(backing_dev.length() - 1); | 
|  | 304 |  | 
|  | 305 | // shutdown zram handle | 
|  | 306 | Timer swap_timer; | 
|  | 307 | LOG(INFO) << "swapoff() start..."; | 
|  | 308 | if (swapoff(ZRAM_DEVICE) == -1) { | 
|  | 309 | LOG(ERROR) << "zram_backing_dev: swapoff (" << backing_dev << ")" << " failed"; | 
|  | 310 | return; | 
|  | 311 | } | 
|  | 312 | LOG(INFO) << "swapoff() took " << swap_timer;; | 
|  | 313 |  | 
|  | 314 | if (!android::base::WriteStringToFile("1", ZRAM_RESET)) { | 
|  | 315 | LOG(ERROR) << "zram_backing_dev: reset (" << backing_dev << ")" << " failed"; | 
|  | 316 | return; | 
|  | 317 | } | 
|  | 318 |  | 
|  | 319 | // clear loopback device | 
|  | 320 | unique_fd loop(TEMP_FAILURE_RETRY(open(backing_dev.c_str(), O_RDWR | O_CLOEXEC))); | 
|  | 321 | if (loop.get() < 0) { | 
|  | 322 | LOG(ERROR) << "zram_backing_dev: open(" << backing_dev << ")" << " failed"; | 
|  | 323 | return; | 
|  | 324 | } | 
|  | 325 |  | 
|  | 326 | if (ioctl(loop.get(), LOOP_CLR_FD, 0) < 0) { | 
|  | 327 | LOG(ERROR) << "zram_backing_dev: loop_clear (" << backing_dev << ")" << " failed"; | 
|  | 328 | return; | 
|  | 329 | } | 
|  | 330 | LOG(INFO) << "zram_backing_dev: `" << backing_dev << "` is cleared successfully."; | 
|  | 331 | } | 
|  | 332 |  | 
| Tom Cherry | 44aceed | 2018-08-03 13:36:18 -0700 | [diff] [blame] | 333 | //* Reboot / shutdown the system. | 
|  | 334 | // cmd ANDROID_RB_* as defined in android_reboot.h | 
|  | 335 | // reason Reason string like "reboot", "shutdown,userrequested" | 
|  | 336 | // rebootTarget Reboot target string like "bootloader". Otherwise, it should be an | 
|  | 337 | //              empty string. | 
|  | 338 | // runFsck Whether to run fsck after umount is done. | 
|  | 339 | // | 
|  | 340 | static void DoReboot(unsigned int cmd, const std::string& reason, const std::string& rebootTarget, | 
|  | 341 | bool runFsck) { | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 342 | Timer t; | 
| Tom Cherry | 0a72e6c | 2018-03-19 16:19:01 -0700 | [diff] [blame] | 343 | LOG(INFO) << "Reboot start, reason: " << reason << ", rebootTarget: " << rebootTarget; | 
|  | 344 |  | 
|  | 345 | // Ensure last reboot reason is reduced to canonical | 
|  | 346 | // alias reported in bootloader or system boot reason. | 
|  | 347 | size_t skip = 0; | 
|  | 348 | std::vector<std::string> reasons = Split(reason, ","); | 
|  | 349 | if (reasons.size() >= 2 && reasons[0] == "reboot" && | 
|  | 350 | (reasons[1] == "recovery" || reasons[1] == "bootloader" || reasons[1] == "cold" || | 
|  | 351 | reasons[1] == "hard" || reasons[1] == "warm")) { | 
|  | 352 | skip = strlen("reboot,"); | 
|  | 353 | } | 
|  | 354 | property_set(LAST_REBOOT_REASON_PROPERTY, reason.c_str() + skip); | 
|  | 355 | sync(); | 
|  | 356 |  | 
|  | 357 | bool is_thermal_shutdown = cmd == ANDROID_RB_THERMOFF; | 
|  | 358 |  | 
|  | 359 | auto shutdown_timeout = 0ms; | 
|  | 360 | if (!SHUTDOWN_ZERO_TIMEOUT) { | 
| Wei Wang | b5de088 | 2018-10-09 12:42:06 -0700 | [diff] [blame] | 361 | constexpr unsigned int shutdown_timeout_default = 6; | 
|  | 362 | constexpr unsigned int max_thermal_shutdown_timeout = 3; | 
|  | 363 | auto shutdown_timeout_final = android::base::GetUintProperty("ro.build.shutdown_timeout", | 
|  | 364 | shutdown_timeout_default); | 
|  | 365 | if (is_thermal_shutdown && shutdown_timeout_final > max_thermal_shutdown_timeout) { | 
|  | 366 | shutdown_timeout_final = max_thermal_shutdown_timeout; | 
| Tom Cherry | 0a72e6c | 2018-03-19 16:19:01 -0700 | [diff] [blame] | 367 | } | 
| Wei Wang | b5de088 | 2018-10-09 12:42:06 -0700 | [diff] [blame] | 368 | shutdown_timeout = std::chrono::seconds(shutdown_timeout_final); | 
| Tom Cherry | 0a72e6c | 2018-03-19 16:19:01 -0700 | [diff] [blame] | 369 | } | 
|  | 370 | LOG(INFO) << "Shutdown timeout: " << shutdown_timeout.count() << " ms"; | 
|  | 371 |  | 
| Keun-young Park | 7830d59 | 2017-03-27 16:07:02 -0700 | [diff] [blame] | 372 | // keep debugging tools until non critical ones are all gone. | 
|  | 373 | const std::set<std::string> kill_after_apps{"tombstoned", "logd", "adbd"}; | 
|  | 374 | // watchdogd is a vendor specific component but should be alive to complete shutdown safely. | 
| Keun-young Park | cccb34f | 2017-07-05 11:38:44 -0700 | [diff] [blame] | 375 | const std::set<std::string> to_starts{"watchdogd"}; | 
| Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 376 | for (const auto& s : ServiceList::GetInstance()) { | 
| Keun-young Park | 7830d59 | 2017-03-27 16:07:02 -0700 | [diff] [blame] | 377 | if (kill_after_apps.count(s->name())) { | 
|  | 378 | s->SetShutdownCritical(); | 
|  | 379 | } else if (to_starts.count(s->name())) { | 
| Tom Cherry | 702ca9a | 2017-08-25 10:36:52 -0700 | [diff] [blame] | 380 | if (auto result = s->Start(); !result) { | 
|  | 381 | LOG(ERROR) << "Could not start shutdown 'to_start' service '" << s->name() | 
|  | 382 | << "': " << result.error(); | 
|  | 383 | } | 
| Keun-young Park | 7830d59 | 2017-03-27 16:07:02 -0700 | [diff] [blame] | 384 | s->SetShutdownCritical(); | 
| Keun-young Park | cccb34f | 2017-07-05 11:38:44 -0700 | [diff] [blame] | 385 | } else if (s->IsShutdownCritical()) { | 
| Tom Cherry | 702ca9a | 2017-08-25 10:36:52 -0700 | [diff] [blame] | 386 | // Start shutdown critical service if not started. | 
|  | 387 | if (auto result = s->Start(); !result) { | 
|  | 388 | LOG(ERROR) << "Could not start shutdown critical service '" << s->name() | 
|  | 389 | << "': " << result.error(); | 
|  | 390 | } | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 391 | } | 
| Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 392 | } | 
| Keun-young Park | 7830d59 | 2017-03-27 16:07:02 -0700 | [diff] [blame] | 393 |  | 
| Steven Moreland | d5eccfd | 2018-01-19 13:01:53 -0800 | [diff] [blame] | 394 | // remaining operations (specifically fsck) may take a substantial duration | 
| Tom Cherry | 0a72e6c | 2018-03-19 16:19:01 -0700 | [diff] [blame] | 395 | if (cmd == ANDROID_RB_POWEROFF || is_thermal_shutdown) { | 
| Steven Moreland | d5eccfd | 2018-01-19 13:01:53 -0800 | [diff] [blame] | 396 | TurnOffBacklight(); | 
|  | 397 | } | 
|  | 398 |  | 
| Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 399 | Service* bootAnim = ServiceList::GetInstance().FindService("bootanim"); | 
|  | 400 | Service* surfaceFlinger = ServiceList::GetInstance().FindService("surfaceflinger"); | 
| Keun-young Park | 7830d59 | 2017-03-27 16:07:02 -0700 | [diff] [blame] | 401 | if (bootAnim != nullptr && surfaceFlinger != nullptr && surfaceFlinger->IsRunning()) { | 
| Tom Cherry | 4f4cacc | 2019-01-08 10:39:00 -0800 | [diff] [blame] | 402 | bool do_shutdown_animation = GetBoolProperty("ro.init.shutdown_animation", false); | 
|  | 403 |  | 
|  | 404 | if (do_shutdown_animation) { | 
|  | 405 | property_set("service.bootanim.exit", "0"); | 
|  | 406 | // Could be in the middle of animation. Stop and start so that it can pick | 
|  | 407 | // up the right mode. | 
|  | 408 | bootAnim->Stop(); | 
|  | 409 | } | 
|  | 410 |  | 
| Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 411 | for (const auto& service : ServiceList::GetInstance()) { | 
| Tom Cherry | 4f4cacc | 2019-01-08 10:39:00 -0800 | [diff] [blame] | 412 | if (service->classnames().count("animation") == 0) { | 
|  | 413 | continue; | 
|  | 414 | } | 
|  | 415 |  | 
|  | 416 | // start all animation classes if stopped. | 
|  | 417 | if (do_shutdown_animation) { | 
|  | 418 | service->Start().IgnoreError(); | 
|  | 419 | } | 
|  | 420 | service->SetShutdownCritical();  // will not check animation class separately | 
|  | 421 | } | 
|  | 422 |  | 
|  | 423 | if (do_shutdown_animation) { | 
|  | 424 | bootAnim->Start().IgnoreError(); | 
|  | 425 | surfaceFlinger->SetShutdownCritical(); | 
|  | 426 | bootAnim->SetShutdownCritical(); | 
| Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 427 | } | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 428 | } | 
| Keun-young Park | 7830d59 | 2017-03-27 16:07:02 -0700 | [diff] [blame] | 429 |  | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 430 | // optional shutdown step | 
|  | 431 | // 1. terminate all services except shutdown critical ones. wait for delay to finish | 
| Keun-young Park | 3017387 | 2017-07-18 10:58:28 -0700 | [diff] [blame] | 432 | if (shutdown_timeout > 0ms) { | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 433 | LOG(INFO) << "terminating init services"; | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 434 |  | 
|  | 435 | // Ask all services to terminate except shutdown critical ones. | 
| Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 436 | for (const auto& s : ServiceList::GetInstance().services_in_shutdown_order()) { | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 437 | if (!s->IsShutdownCritical()) s->Terminate(); | 
| Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 438 | } | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 439 |  | 
|  | 440 | int service_count = 0; | 
| Keun-young Park | 3017387 | 2017-07-18 10:58:28 -0700 | [diff] [blame] | 441 | // Only wait up to half of timeout here | 
|  | 442 | auto termination_wait_timeout = shutdown_timeout / 2; | 
| Tom Cherry | ede0d53 | 2017-07-06 14:20:11 -0700 | [diff] [blame] | 443 | while (t.duration() < termination_wait_timeout) { | 
| Tom Cherry | eeee831 | 2017-07-28 15:22:23 -0700 | [diff] [blame] | 444 | ReapAnyOutstandingChildren(); | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 445 |  | 
|  | 446 | service_count = 0; | 
| Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 447 | for (const auto& s : ServiceList::GetInstance()) { | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 448 | // Count the number of services running except shutdown critical. | 
|  | 449 | // Exclude the console as it will ignore the SIGTERM signal | 
|  | 450 | // and not exit. | 
|  | 451 | // Note: SVC_CONSOLE actually means "requires console" but | 
|  | 452 | // it is only used by the shell. | 
|  | 453 | if (!s->IsShutdownCritical() && s->pid() != 0 && (s->flags() & SVC_CONSOLE) == 0) { | 
|  | 454 | service_count++; | 
|  | 455 | } | 
| Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 456 | } | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 457 |  | 
|  | 458 | if (service_count == 0) { | 
|  | 459 | // All terminable services terminated. We can exit early. | 
|  | 460 | break; | 
|  | 461 | } | 
|  | 462 |  | 
|  | 463 | // Wait a bit before recounting the number or running services. | 
|  | 464 | std::this_thread::sleep_for(50ms); | 
|  | 465 | } | 
|  | 466 | LOG(INFO) << "Terminating running services took " << t | 
|  | 467 | << " with remaining services:" << service_count; | 
|  | 468 | } | 
|  | 469 |  | 
|  | 470 | // minimum safety steps before restarting | 
|  | 471 | // 2. kill all services except ones that are necessary for the shutdown sequence. | 
| Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 472 | for (const auto& s : ServiceList::GetInstance().services_in_shutdown_order()) { | 
| Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 473 | if (!s->IsShutdownCritical()) s->Stop(); | 
| Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 474 | } | 
| Luis Hector Chavez | 92c49bc | 2018-07-27 11:19:25 -0700 | [diff] [blame] | 475 | SubcontextTerminate(); | 
| Tom Cherry | eeee831 | 2017-07-28 15:22:23 -0700 | [diff] [blame] | 476 | ReapAnyOutstandingChildren(); | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 477 |  | 
|  | 478 | // 3. send volume shutdown to vold | 
| Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 479 | Service* voldService = ServiceList::GetInstance().FindService("vold"); | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 480 | if (voldService != nullptr && voldService->IsRunning()) { | 
|  | 481 | ShutdownVold(); | 
| Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 482 | voldService->Stop(); | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 483 | } else { | 
|  | 484 | LOG(INFO) << "vold not running, skipping vold shutdown"; | 
|  | 485 | } | 
| Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 486 | // logcat stopped here | 
| Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 487 | for (const auto& s : ServiceList::GetInstance().services_in_shutdown_order()) { | 
| Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 488 | if (kill_after_apps.count(s->name())) s->Stop(); | 
| Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 489 | } | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 490 | // 4. sync, try umount, and optionally run fsck for user shutdown | 
| Tom Cherry | 1f9d540 | 2018-03-15 10:22:40 -0700 | [diff] [blame] | 491 | { | 
|  | 492 | Timer sync_timer; | 
|  | 493 | LOG(INFO) << "sync() before umount..."; | 
|  | 494 | sync(); | 
|  | 495 | LOG(INFO) << "sync() before umount took" << sync_timer; | 
|  | 496 | } | 
| Jaegeuk Kim | 2aedc82 | 2018-11-20 13:27:06 -0800 | [diff] [blame] | 497 | // 5. drop caches and disable zram backing device, if exist | 
|  | 498 | KillZramBackingDevice(); | 
|  | 499 |  | 
| Tom Cherry | ede0d53 | 2017-07-06 14:20:11 -0700 | [diff] [blame] | 500 | UmountStat stat = TryUmountAndFsck(runFsck, shutdown_timeout - t.duration()); | 
| Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 501 | // Follow what linux shutdown is doing: one more sync with little bit delay | 
| Tom Cherry | 1f9d540 | 2018-03-15 10:22:40 -0700 | [diff] [blame] | 502 | { | 
|  | 503 | Timer sync_timer; | 
|  | 504 | LOG(INFO) << "sync() after umount..."; | 
|  | 505 | sync(); | 
|  | 506 | LOG(INFO) << "sync() after umount took" << sync_timer; | 
|  | 507 | } | 
| Tom Cherry | 0a72e6c | 2018-03-19 16:19:01 -0700 | [diff] [blame] | 508 | if (!is_thermal_shutdown) std::this_thread::sleep_for(100ms); | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 509 | LogShutdownTime(stat, &t); | 
|  | 510 | // Reboot regardless of umount status. If umount fails, fsck after reboot will fix it. | 
|  | 511 | RebootSystem(cmd, rebootTarget); | 
|  | 512 | abort(); | 
|  | 513 | } | 
| Tom Cherry | 98ad32a | 2017-04-17 16:34:20 -0700 | [diff] [blame] | 514 |  | 
|  | 515 | bool HandlePowerctlMessage(const std::string& command) { | 
|  | 516 | unsigned int cmd = 0; | 
| Mark Salyzyn | 6290982 | 2017-10-09 09:27:16 -0700 | [diff] [blame] | 517 | std::vector<std::string> cmd_params = Split(command, ","); | 
| Tom Cherry | 98ad32a | 2017-04-17 16:34:20 -0700 | [diff] [blame] | 518 | std::string reboot_target = ""; | 
|  | 519 | bool run_fsck = false; | 
|  | 520 | bool command_invalid = false; | 
|  | 521 |  | 
|  | 522 | if (cmd_params.size() > 3) { | 
|  | 523 | command_invalid = true; | 
|  | 524 | } else if (cmd_params[0] == "shutdown") { | 
|  | 525 | cmd = ANDROID_RB_POWEROFF; | 
| Mark Salyzyn | 73e6b49 | 2017-08-14 15:56:53 -0700 | [diff] [blame] | 526 | if (cmd_params.size() == 2) { | 
|  | 527 | if (cmd_params[1] == "userrequested") { | 
|  | 528 | // The shutdown reason is PowerManager.SHUTDOWN_USER_REQUESTED. | 
|  | 529 | // Run fsck once the file system is remounted in read-only mode. | 
|  | 530 | run_fsck = true; | 
|  | 531 | } else if (cmd_params[1] == "thermal") { | 
| Mark Salyzyn | bfd05b6 | 2017-09-26 11:10:12 -0700 | [diff] [blame] | 532 | // Turn off sources of heat immediately. | 
|  | 533 | TurnOffBacklight(); | 
| Mark Salyzyn | 73e6b49 | 2017-08-14 15:56:53 -0700 | [diff] [blame] | 534 | // run_fsck is false to avoid delay | 
|  | 535 | cmd = ANDROID_RB_THERMOFF; | 
|  | 536 | } | 
| Tom Cherry | 98ad32a | 2017-04-17 16:34:20 -0700 | [diff] [blame] | 537 | } | 
|  | 538 | } else if (cmd_params[0] == "reboot") { | 
|  | 539 | cmd = ANDROID_RB_RESTART2; | 
|  | 540 | if (cmd_params.size() >= 2) { | 
|  | 541 | reboot_target = cmd_params[1]; | 
| Hridya Valsaraju | 5425826 | 2018-09-19 21:14:18 -0700 | [diff] [blame] | 542 | // adb reboot fastboot should boot into bootloader for devices not | 
|  | 543 | // supporting logical partitions. | 
|  | 544 | if (reboot_target == "fastboot" && | 
| Yifan Hong | 0e0f818 | 2018-11-16 12:49:06 -0800 | [diff] [blame] | 545 | !android::base::GetBoolProperty("ro.boot.dynamic_partitions", false)) { | 
| Hridya Valsaraju | 5425826 | 2018-09-19 21:14:18 -0700 | [diff] [blame] | 546 | reboot_target = "bootloader"; | 
|  | 547 | } | 
| Tom Cherry | 98ad32a | 2017-04-17 16:34:20 -0700 | [diff] [blame] | 548 | // When rebooting to the bootloader notify the bootloader writing | 
|  | 549 | // also the BCB. | 
|  | 550 | if (reboot_target == "bootloader") { | 
|  | 551 | std::string err; | 
|  | 552 | if (!write_reboot_bootloader(&err)) { | 
|  | 553 | LOG(ERROR) << "reboot-bootloader: Error writing " | 
|  | 554 | "bootloader_message: " | 
|  | 555 | << err; | 
|  | 556 | } | 
| Hridya Valsaraju | 71fb82a | 2018-08-02 15:02:06 -0700 | [diff] [blame] | 557 | } else if (reboot_target == "sideload" || reboot_target == "sideload-auto-reboot" || | 
|  | 558 | reboot_target == "fastboot") { | 
|  | 559 | std::string arg = reboot_target == "sideload-auto-reboot" ? "sideload_auto_reboot" | 
|  | 560 | : reboot_target; | 
|  | 561 | const std::vector<std::string> options = { | 
|  | 562 | "--" + arg, | 
|  | 563 | }; | 
|  | 564 | std::string err; | 
|  | 565 | if (!write_bootloader_message(options, &err)) { | 
|  | 566 | LOG(ERROR) << "Failed to set bootloader message: " << err; | 
|  | 567 | return false; | 
|  | 568 | } | 
|  | 569 | reboot_target = "recovery"; | 
| Tom Cherry | 98ad32a | 2017-04-17 16:34:20 -0700 | [diff] [blame] | 570 | } | 
| Hridya Valsaraju | 71fb82a | 2018-08-02 15:02:06 -0700 | [diff] [blame] | 571 |  | 
| Mark Salyzyn | 73e6b49 | 2017-08-14 15:56:53 -0700 | [diff] [blame] | 572 | // If there is an additional parameter, pass it along | 
|  | 573 | if ((cmd_params.size() == 3) && cmd_params[2].size()) { | 
| Tom Cherry | 98ad32a | 2017-04-17 16:34:20 -0700 | [diff] [blame] | 574 | reboot_target += "," + cmd_params[2]; | 
|  | 575 | } | 
|  | 576 | } | 
| Tom Cherry | 98ad32a | 2017-04-17 16:34:20 -0700 | [diff] [blame] | 577 | } else { | 
|  | 578 | command_invalid = true; | 
|  | 579 | } | 
|  | 580 | if (command_invalid) { | 
|  | 581 | LOG(ERROR) << "powerctl: unrecognized command '" << command << "'"; | 
|  | 582 | return false; | 
|  | 583 | } | 
|  | 584 |  | 
| Wei Wang | eeab491 | 2017-06-27 22:08:45 -0700 | [diff] [blame] | 585 | LOG(INFO) << "Clear action queue and start shutdown trigger"; | 
|  | 586 | ActionManager::GetInstance().ClearQueue(); | 
|  | 587 | // Queue shutdown trigger first | 
|  | 588 | ActionManager::GetInstance().QueueEventTrigger("shutdown"); | 
|  | 589 | // Queue built-in shutdown_done | 
| Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 590 | auto shutdown_handler = [cmd, command, reboot_target, run_fsck](const BuiltinArguments&) { | 
| Wei Wang | eeab491 | 2017-06-27 22:08:45 -0700 | [diff] [blame] | 591 | DoReboot(cmd, command, reboot_target, run_fsck); | 
| Tom Cherry | 557946e | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 592 | return Success(); | 
| Wei Wang | eeab491 | 2017-06-27 22:08:45 -0700 | [diff] [blame] | 593 | }; | 
|  | 594 | ActionManager::GetInstance().QueueBuiltinAction(shutdown_handler, "shutdown_done"); | 
|  | 595 |  | 
|  | 596 | // Skip wait for prop if it is in progress | 
|  | 597 | ResetWaitForProp(); | 
|  | 598 |  | 
| Tom Cherry | 3b81f2d | 2017-07-28 14:48:41 -0700 | [diff] [blame] | 599 | // Clear EXEC flag if there is one pending | 
| Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 600 | for (const auto& s : ServiceList::GetInstance()) { | 
|  | 601 | s->UnSetExec(); | 
|  | 602 | } | 
| Wei Wang | eeab491 | 2017-06-27 22:08:45 -0700 | [diff] [blame] | 603 |  | 
| Tom Cherry | 98ad32a | 2017-04-17 16:34:20 -0700 | [diff] [blame] | 604 | return true; | 
|  | 605 | } | 
| Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 606 |  | 
|  | 607 | }  // namespace init | 
|  | 608 | }  // namespace android |