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> |
Luis Hector Chavez | 519e5f0 | 2017-06-29 09:50:30 -0700 | [diff] [blame] | 23 | #include <sys/capability.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> |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 27 | #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 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" |
Luis Hector Chavez | 519e5f0 | 2017-06-29 09:50:30 -0700 | [diff] [blame] | 54 | #include "capabilities.h" |
Wei Wang | eeab491 | 2017-06-27 22:08:45 -0700 | [diff] [blame] | 55 | #include "init.h" |
Keun-young Park | 7830d59 | 2017-03-27 16:07:02 -0700 | [diff] [blame] | 56 | #include "property_service.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 | |
Mark Salyzyn | 6290982 | 2017-10-09 09:27:16 -0700 | [diff] [blame] | 60 | using android::base::Split; |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 61 | using android::base::StringPrintf; |
Tom Cherry | ede0d53 | 2017-07-06 14:20:11 -0700 | [diff] [blame] | 62 | using android::base::Timer; |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 63 | |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 64 | namespace android { |
| 65 | namespace init { |
| 66 | |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 67 | // represents umount status during reboot / shutdown. |
| 68 | enum 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 |
| 83 | class MountEntry { |
| 84 | public: |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 85 | explicit MountEntry(const mntent& entry) |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 86 | : mnt_fsname_(entry.mnt_fsname), |
| 87 | mnt_dir_(entry.mnt_dir), |
| 88 | mnt_type_(entry.mnt_type), |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 89 | mnt_opts_(entry.mnt_opts) {} |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 90 | |
Jaegeuk Kim | 0f04f72 | 2017-09-25 10:55:39 -0700 | [diff] [blame] | 91 | bool Umount(bool force) { |
Tom Cherry | c9fec9d | 2018-02-15 14:26:58 -0800 | [diff] [blame] | 92 | LOG(INFO) << "Unmounting " << mnt_fsname_ << ":" << mnt_dir_ << " opts " << mnt_opts_; |
Jaegeuk Kim | 0f04f72 | 2017-09-25 10:55:39 -0700 | [diff] [blame] | 93 | int r = umount2(mnt_dir_.c_str(), force ? MNT_FORCE : 0); |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 94 | if (r == 0) { |
Tom Cherry | c9fec9d | 2018-02-15 14:26:58 -0800 | [diff] [blame] | 95 | LOG(INFO) << "Umounted " << mnt_fsname_ << ":" << mnt_dir_ << " opts " << mnt_opts_; |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 96 | return true; |
| 97 | } else { |
Tom Cherry | c9fec9d | 2018-02-15 14:26:58 -0800 | [diff] [blame] | 98 | PLOG(WARNING) << "Cannot umount " << mnt_fsname_ << ":" << mnt_dir_ << " opts " |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 99 | << mnt_opts_; |
| 100 | return false; |
| 101 | } |
| 102 | } |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 103 | |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 104 | void DoFsck() { |
| 105 | int st; |
| 106 | if (IsF2Fs()) { |
| 107 | const char* f2fs_argv[] = { |
| 108 | "/system/bin/fsck.f2fs", "-f", mnt_fsname_.c_str(), |
| 109 | }; |
| 110 | android_fork_execvp_ext(arraysize(f2fs_argv), (char**)f2fs_argv, &st, true, LOG_KLOG, |
| 111 | true, nullptr, nullptr, 0); |
| 112 | } else if (IsExt4()) { |
| 113 | const char* ext4_argv[] = { |
| 114 | "/system/bin/e2fsck", "-f", "-y", mnt_fsname_.c_str(), |
| 115 | }; |
| 116 | android_fork_execvp_ext(arraysize(ext4_argv), (char**)ext4_argv, &st, true, LOG_KLOG, |
| 117 | true, nullptr, nullptr, 0); |
| 118 | } |
| 119 | } |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 120 | |
| 121 | static bool IsBlockDevice(const struct mntent& mntent) { |
| 122 | return android::base::StartsWith(mntent.mnt_fsname, "/dev/block"); |
| 123 | } |
| 124 | |
| 125 | static bool IsEmulatedDevice(const struct mntent& mntent) { |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 126 | return android::base::StartsWith(mntent.mnt_fsname, "/data/"); |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | private: |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 130 | bool IsF2Fs() const { return mnt_type_ == "f2fs"; } |
| 131 | |
| 132 | bool IsExt4() const { return mnt_type_ == "ext4"; } |
| 133 | |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 134 | std::string mnt_fsname_; |
| 135 | std::string mnt_dir_; |
| 136 | std::string mnt_type_; |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 137 | std::string mnt_opts_; |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 138 | }; |
| 139 | |
| 140 | // Turn off backlight while we are performing power down cleanup activities. |
| 141 | static void TurnOffBacklight() { |
Steven Moreland | d5eccfd | 2018-01-19 13:01:53 -0800 | [diff] [blame] | 142 | Service* service = ServiceList::GetInstance().FindService("blank_screen"); |
| 143 | if (service == nullptr) { |
| 144 | LOG(WARNING) << "cannot find blank_screen in TurnOffBacklight"; |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 145 | return; |
| 146 | } |
Steven Moreland | d5eccfd | 2018-01-19 13:01:53 -0800 | [diff] [blame] | 147 | service->Start(); |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 150 | static void ShutdownVold() { |
| 151 | const char* vdc_argv[] = {"/system/bin/vdc", "volume", "shutdown"}; |
| 152 | int status; |
| 153 | android_fork_execvp_ext(arraysize(vdc_argv), (char**)vdc_argv, &status, true, LOG_KLOG, true, |
| 154 | nullptr, nullptr, 0); |
| 155 | } |
| 156 | |
| 157 | static void LogShutdownTime(UmountStat stat, Timer* t) { |
Tom Cherry | ede0d53 | 2017-07-06 14:20:11 -0700 | [diff] [blame] | 158 | LOG(WARNING) << "powerctl_shutdown_time_ms:" << std::to_string(t->duration().count()) << ":" |
| 159 | << stat; |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Luis Hector Chavez | 9f97f47 | 2017-09-06 13:43:57 -0700 | [diff] [blame] | 162 | bool IsRebootCapable() { |
Luis Hector Chavez | 519e5f0 | 2017-06-29 09:50:30 -0700 | [diff] [blame] | 163 | if (!CAP_IS_SUPPORTED(CAP_SYS_BOOT)) { |
| 164 | PLOG(WARNING) << "CAP_SYS_BOOT is not supported"; |
| 165 | return true; |
| 166 | } |
| 167 | |
| 168 | ScopedCaps caps(cap_get_proc()); |
| 169 | if (!caps) { |
| 170 | PLOG(WARNING) << "cap_get_proc() failed"; |
| 171 | return true; |
| 172 | } |
| 173 | |
| 174 | cap_flag_value_t value = CAP_SET; |
| 175 | if (cap_get_flag(caps.get(), CAP_SYS_BOOT, CAP_EFFECTIVE, &value) != 0) { |
| 176 | PLOG(WARNING) << "cap_get_flag(CAP_SYS_BOOT, EFFECTIVE) failed"; |
| 177 | return true; |
| 178 | } |
| 179 | return value == CAP_SET; |
| 180 | } |
| 181 | |
Tom Cherry | d8db7ab | 2017-08-17 17:28:30 -0700 | [diff] [blame] | 182 | void __attribute__((noreturn)) RebootSystem(unsigned int cmd, const std::string& rebootTarget) { |
Keun-young Park | 3cd8c6f | 2017-03-23 15:33:16 -0700 | [diff] [blame] | 183 | LOG(INFO) << "Reboot ending, jumping to kernel"; |
Luis Hector Chavez | 519e5f0 | 2017-06-29 09:50:30 -0700 | [diff] [blame] | 184 | |
| 185 | if (!IsRebootCapable()) { |
| 186 | // On systems where init does not have the capability of rebooting the |
| 187 | // device, just exit cleanly. |
| 188 | exit(0); |
| 189 | } |
| 190 | |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 191 | switch (cmd) { |
| 192 | case ANDROID_RB_POWEROFF: |
| 193 | reboot(RB_POWER_OFF); |
| 194 | break; |
| 195 | |
| 196 | case ANDROID_RB_RESTART2: |
| 197 | syscall(__NR_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, |
| 198 | LINUX_REBOOT_CMD_RESTART2, rebootTarget.c_str()); |
| 199 | break; |
| 200 | |
| 201 | case ANDROID_RB_THERMOFF: |
| 202 | reboot(RB_POWER_OFF); |
| 203 | break; |
| 204 | } |
| 205 | // In normal case, reboot should not return. |
Tom Cherry | d8db7ab | 2017-08-17 17:28:30 -0700 | [diff] [blame] | 206 | PLOG(ERROR) << "reboot call returned"; |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 207 | abort(); |
| 208 | } |
| 209 | |
| 210 | /* Find all read+write block devices and emulated devices in /proc/mounts |
| 211 | * and add them to correpsponding list. |
| 212 | */ |
| 213 | static bool FindPartitionsToUmount(std::vector<MountEntry>* blockDevPartitions, |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 214 | std::vector<MountEntry>* emulatedPartitions, bool dump) { |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 215 | std::unique_ptr<std::FILE, int (*)(std::FILE*)> fp(setmntent("/proc/mounts", "r"), endmntent); |
| 216 | if (fp == nullptr) { |
| 217 | PLOG(ERROR) << "Failed to open /proc/mounts"; |
| 218 | return false; |
| 219 | } |
| 220 | mntent* mentry; |
| 221 | while ((mentry = getmntent(fp.get())) != nullptr) { |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 222 | if (dump) { |
| 223 | LOG(INFO) << "mount entry " << mentry->mnt_fsname << ":" << mentry->mnt_dir << " opts " |
| 224 | << mentry->mnt_opts << " type " << mentry->mnt_type; |
| 225 | } else if (MountEntry::IsBlockDevice(*mentry) && hasmntopt(mentry, "rw")) { |
Keun-young Park | 6e12b38 | 2017-07-17 12:20:33 -0700 | [diff] [blame] | 226 | std::string mount_dir(mentry->mnt_dir); |
| 227 | // These are R/O partitions changed to R/W after adb remount. |
| 228 | // Do not umount them as shutdown critical services may rely on them. |
Wei Wang | a01c27e | 2017-07-25 10:52:08 -0700 | [diff] [blame] | 229 | if (mount_dir != "/" && mount_dir != "/system" && mount_dir != "/vendor" && |
| 230 | mount_dir != "/oem") { |
Keun-young Park | 6e12b38 | 2017-07-17 12:20:33 -0700 | [diff] [blame] | 231 | blockDevPartitions->emplace(blockDevPartitions->begin(), *mentry); |
| 232 | } |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 233 | } else if (MountEntry::IsEmulatedDevice(*mentry)) { |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 234 | emulatedPartitions->emplace(emulatedPartitions->begin(), *mentry); |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | return true; |
| 238 | } |
| 239 | |
Keun-young Park | 1663e97 | 2017-04-21 17:29:26 -0700 | [diff] [blame] | 240 | static void DumpUmountDebuggingInfo(bool dump_all) { |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 241 | int status; |
| 242 | if (!security_getenforce()) { |
| 243 | LOG(INFO) << "Run lsof"; |
| 244 | const char* lsof_argv[] = {"/system/bin/lsof"}; |
| 245 | android_fork_execvp_ext(arraysize(lsof_argv), (char**)lsof_argv, &status, true, LOG_KLOG, |
| 246 | true, nullptr, nullptr, 0); |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 247 | } |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 248 | FindPartitionsToUmount(nullptr, nullptr, true); |
Keun-young Park | 1663e97 | 2017-04-21 17:29:26 -0700 | [diff] [blame] | 249 | if (dump_all) { |
| 250 | // dump current tasks, this log can be lengthy, so only dump with dump_all |
| 251 | android::base::WriteStringToFile("t", "/proc/sysrq-trigger"); |
| 252 | } |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Tom Cherry | ede0d53 | 2017-07-06 14:20:11 -0700 | [diff] [blame] | 255 | static UmountStat UmountPartitions(std::chrono::milliseconds timeout) { |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 256 | Timer t; |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 257 | /* data partition needs all pending writes to be completed and all emulated partitions |
| 258 | * umounted.If the current waiting is not good enough, give |
| 259 | * up and leave it to e2fsck after reboot to fix it. |
| 260 | */ |
| 261 | while (true) { |
| 262 | std::vector<MountEntry> block_devices; |
| 263 | std::vector<MountEntry> emulated_devices; |
| 264 | if (!FindPartitionsToUmount(&block_devices, &emulated_devices, false)) { |
| 265 | return UMOUNT_STAT_ERROR; |
| 266 | } |
| 267 | if (block_devices.size() == 0) { |
Wei Wang | 8c00e42 | 2017-08-16 14:01:46 -0700 | [diff] [blame] | 268 | return UMOUNT_STAT_SUCCESS; |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 269 | } |
Wei Wang | 8c00e42 | 2017-08-16 14:01:46 -0700 | [diff] [blame] | 270 | bool unmount_done = true; |
| 271 | if (emulated_devices.size() > 0) { |
Wei Wang | 25dc30f | 2017-10-23 15:32:03 -0700 | [diff] [blame] | 272 | for (auto& entry : emulated_devices) { |
| 273 | if (!entry.Umount(false)) unmount_done = false; |
| 274 | } |
Wei Wang | 8c00e42 | 2017-08-16 14:01:46 -0700 | [diff] [blame] | 275 | if (unmount_done) { |
| 276 | sync(); |
| 277 | } |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 278 | } |
Wei Wang | 25dc30f | 2017-10-23 15:32:03 -0700 | [diff] [blame] | 279 | for (auto& entry : block_devices) { |
| 280 | if (!entry.Umount(timeout == 0ms)) unmount_done = false; |
| 281 | } |
Wei Wang | 8c00e42 | 2017-08-16 14:01:46 -0700 | [diff] [blame] | 282 | if (unmount_done) { |
| 283 | return UMOUNT_STAT_SUCCESS; |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 284 | } |
Wei Wang | 8c00e42 | 2017-08-16 14:01:46 -0700 | [diff] [blame] | 285 | if ((timeout < t.duration())) { // try umount at least once |
| 286 | return UMOUNT_STAT_TIMEOUT; |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 287 | } |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 288 | std::this_thread::sleep_for(100ms); |
| 289 | } |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Keun-young Park | 3ee0df9 | 2017-03-27 11:21:09 -0700 | [diff] [blame] | 292 | static void KillAllProcesses() { android::base::WriteStringToFile("i", "/proc/sysrq-trigger"); } |
| 293 | |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 294 | /* Try umounting all emulated file systems R/W block device cfile systems. |
| 295 | * This will just try umount and give it up if it fails. |
| 296 | * For fs like ext4, this is ok as file system will be marked as unclean shutdown |
| 297 | * and necessary check can be done at the next reboot. |
| 298 | * For safer shutdown, caller needs to make sure that |
| 299 | * all processes / emulated partition for the target fs are all cleaned-up. |
| 300 | * |
| 301 | * return true when umount was successful. false when timed out. |
| 302 | */ |
Tom Cherry | ede0d53 | 2017-07-06 14:20:11 -0700 | [diff] [blame] | 303 | static UmountStat TryUmountAndFsck(bool runFsck, std::chrono::milliseconds timeout) { |
Keun-young Park | 3ee0df9 | 2017-03-27 11:21:09 -0700 | [diff] [blame] | 304 | Timer t; |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 305 | std::vector<MountEntry> block_devices; |
| 306 | std::vector<MountEntry> emulated_devices; |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 307 | |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 308 | if (runFsck && !FindPartitionsToUmount(&block_devices, &emulated_devices, false)) { |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 309 | return UMOUNT_STAT_ERROR; |
| 310 | } |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 311 | |
Tom Cherry | ede0d53 | 2017-07-06 14:20:11 -0700 | [diff] [blame] | 312 | UmountStat stat = UmountPartitions(timeout - t.duration()); |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 313 | if (stat != UMOUNT_STAT_SUCCESS) { |
| 314 | LOG(INFO) << "umount timeout, last resort, kill all and try"; |
Keun-young Park | c59b822 | 2017-07-18 18:52:25 -0700 | [diff] [blame] | 315 | if (DUMP_ON_UMOUNT_FAILURE) DumpUmountDebuggingInfo(true); |
Keun-young Park | 3ee0df9 | 2017-03-27 11:21:09 -0700 | [diff] [blame] | 316 | KillAllProcesses(); |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 317 | // 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] | 318 | UmountStat st = UmountPartitions(0ms); |
| 319 | if ((st != UMOUNT_STAT_SUCCESS) && DUMP_ON_UMOUNT_FAILURE) DumpUmountDebuggingInfo(false); |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 320 | } |
| 321 | |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 322 | if (stat == UMOUNT_STAT_SUCCESS && runFsck) { |
| 323 | // fsck part is excluded from timeout check. It only runs for user initiated shutdown |
| 324 | // and should not affect reboot time. |
| 325 | for (auto& entry : block_devices) { |
| 326 | entry.DoFsck(); |
| 327 | } |
| 328 | } |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 329 | return stat; |
| 330 | } |
| 331 | |
Tom Cherry | 0a72e6c | 2018-03-19 16:19:01 -0700 | [diff] [blame^] | 332 | void DoReboot(unsigned int cmd, const std::string& reason, const std::string& rebootTarget, |
| 333 | bool runFsck) { |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 334 | Timer t; |
Tom Cherry | 0a72e6c | 2018-03-19 16:19:01 -0700 | [diff] [blame^] | 335 | LOG(INFO) << "Reboot start, reason: " << reason << ", rebootTarget: " << rebootTarget; |
| 336 | |
| 337 | // Ensure last reboot reason is reduced to canonical |
| 338 | // alias reported in bootloader or system boot reason. |
| 339 | size_t skip = 0; |
| 340 | std::vector<std::string> reasons = Split(reason, ","); |
| 341 | if (reasons.size() >= 2 && reasons[0] == "reboot" && |
| 342 | (reasons[1] == "recovery" || reasons[1] == "bootloader" || reasons[1] == "cold" || |
| 343 | reasons[1] == "hard" || reasons[1] == "warm")) { |
| 344 | skip = strlen("reboot,"); |
| 345 | } |
| 346 | property_set(LAST_REBOOT_REASON_PROPERTY, reason.c_str() + skip); |
| 347 | sync(); |
| 348 | |
| 349 | bool is_thermal_shutdown = cmd == ANDROID_RB_THERMOFF; |
| 350 | |
| 351 | auto shutdown_timeout = 0ms; |
| 352 | if (!SHUTDOWN_ZERO_TIMEOUT) { |
| 353 | if (is_thermal_shutdown) { |
| 354 | constexpr unsigned int thermal_shutdown_timeout = 1; |
| 355 | shutdown_timeout = std::chrono::seconds(thermal_shutdown_timeout); |
| 356 | } else { |
| 357 | constexpr unsigned int shutdown_timeout_default = 6; |
| 358 | auto shutdown_timeout_property = android::base::GetUintProperty( |
| 359 | "ro.build.shutdown_timeout", shutdown_timeout_default); |
| 360 | shutdown_timeout = std::chrono::seconds(shutdown_timeout_property); |
| 361 | } |
| 362 | } |
| 363 | LOG(INFO) << "Shutdown timeout: " << shutdown_timeout.count() << " ms"; |
| 364 | |
Keun-young Park | 7830d59 | 2017-03-27 16:07:02 -0700 | [diff] [blame] | 365 | // keep debugging tools until non critical ones are all gone. |
| 366 | const std::set<std::string> kill_after_apps{"tombstoned", "logd", "adbd"}; |
| 367 | // 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] | 368 | const std::set<std::string> to_starts{"watchdogd"}; |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 369 | for (const auto& s : ServiceList::GetInstance()) { |
Keun-young Park | 7830d59 | 2017-03-27 16:07:02 -0700 | [diff] [blame] | 370 | if (kill_after_apps.count(s->name())) { |
| 371 | s->SetShutdownCritical(); |
| 372 | } else if (to_starts.count(s->name())) { |
Tom Cherry | 702ca9a | 2017-08-25 10:36:52 -0700 | [diff] [blame] | 373 | if (auto result = s->Start(); !result) { |
| 374 | LOG(ERROR) << "Could not start shutdown 'to_start' service '" << s->name() |
| 375 | << "': " << result.error(); |
| 376 | } |
Keun-young Park | 7830d59 | 2017-03-27 16:07:02 -0700 | [diff] [blame] | 377 | s->SetShutdownCritical(); |
Keun-young Park | cccb34f | 2017-07-05 11:38:44 -0700 | [diff] [blame] | 378 | } else if (s->IsShutdownCritical()) { |
Tom Cherry | 702ca9a | 2017-08-25 10:36:52 -0700 | [diff] [blame] | 379 | // Start shutdown critical service if not started. |
| 380 | if (auto result = s->Start(); !result) { |
| 381 | LOG(ERROR) << "Could not start shutdown critical service '" << s->name() |
| 382 | << "': " << result.error(); |
| 383 | } |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 384 | } |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 385 | } |
Keun-young Park | 7830d59 | 2017-03-27 16:07:02 -0700 | [diff] [blame] | 386 | |
Steven Moreland | d5eccfd | 2018-01-19 13:01:53 -0800 | [diff] [blame] | 387 | // remaining operations (specifically fsck) may take a substantial duration |
Tom Cherry | 0a72e6c | 2018-03-19 16:19:01 -0700 | [diff] [blame^] | 388 | if (cmd == ANDROID_RB_POWEROFF || is_thermal_shutdown) { |
Steven Moreland | d5eccfd | 2018-01-19 13:01:53 -0800 | [diff] [blame] | 389 | TurnOffBacklight(); |
| 390 | } |
| 391 | |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 392 | Service* bootAnim = ServiceList::GetInstance().FindService("bootanim"); |
| 393 | Service* surfaceFlinger = ServiceList::GetInstance().FindService("surfaceflinger"); |
Keun-young Park | 7830d59 | 2017-03-27 16:07:02 -0700 | [diff] [blame] | 394 | if (bootAnim != nullptr && surfaceFlinger != nullptr && surfaceFlinger->IsRunning()) { |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 395 | // will not check animation class separately |
| 396 | for (const auto& service : ServiceList::GetInstance()) { |
| 397 | if (service->classnames().count("animation")) service->SetShutdownCritical(); |
| 398 | } |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 399 | } |
Keun-young Park | 7830d59 | 2017-03-27 16:07:02 -0700 | [diff] [blame] | 400 | |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 401 | // optional shutdown step |
| 402 | // 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] | 403 | if (shutdown_timeout > 0ms) { |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 404 | LOG(INFO) << "terminating init services"; |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 405 | |
| 406 | // Ask all services to terminate except shutdown critical ones. |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 407 | for (const auto& s : ServiceList::GetInstance().services_in_shutdown_order()) { |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 408 | if (!s->IsShutdownCritical()) s->Terminate(); |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 409 | } |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 410 | |
| 411 | int service_count = 0; |
Keun-young Park | 3017387 | 2017-07-18 10:58:28 -0700 | [diff] [blame] | 412 | // Only wait up to half of timeout here |
| 413 | auto termination_wait_timeout = shutdown_timeout / 2; |
Tom Cherry | ede0d53 | 2017-07-06 14:20:11 -0700 | [diff] [blame] | 414 | while (t.duration() < termination_wait_timeout) { |
Tom Cherry | eeee831 | 2017-07-28 15:22:23 -0700 | [diff] [blame] | 415 | ReapAnyOutstandingChildren(); |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 416 | |
| 417 | service_count = 0; |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 418 | for (const auto& s : ServiceList::GetInstance()) { |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 419 | // Count the number of services running except shutdown critical. |
| 420 | // Exclude the console as it will ignore the SIGTERM signal |
| 421 | // and not exit. |
| 422 | // Note: SVC_CONSOLE actually means "requires console" but |
| 423 | // it is only used by the shell. |
| 424 | if (!s->IsShutdownCritical() && s->pid() != 0 && (s->flags() & SVC_CONSOLE) == 0) { |
| 425 | service_count++; |
| 426 | } |
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 | |
| 429 | if (service_count == 0) { |
| 430 | // All terminable services terminated. We can exit early. |
| 431 | break; |
| 432 | } |
| 433 | |
| 434 | // Wait a bit before recounting the number or running services. |
| 435 | std::this_thread::sleep_for(50ms); |
| 436 | } |
| 437 | LOG(INFO) << "Terminating running services took " << t |
| 438 | << " with remaining services:" << service_count; |
| 439 | } |
| 440 | |
| 441 | // minimum safety steps before restarting |
| 442 | // 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] | 443 | for (const auto& s : ServiceList::GetInstance().services_in_shutdown_order()) { |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 444 | if (!s->IsShutdownCritical()) s->Stop(); |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 445 | } |
Tom Cherry | eeee831 | 2017-07-28 15:22:23 -0700 | [diff] [blame] | 446 | ReapAnyOutstandingChildren(); |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 447 | |
| 448 | // 3. send volume shutdown to vold |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 449 | Service* voldService = ServiceList::GetInstance().FindService("vold"); |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 450 | if (voldService != nullptr && voldService->IsRunning()) { |
| 451 | ShutdownVold(); |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 452 | voldService->Stop(); |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 453 | } else { |
| 454 | LOG(INFO) << "vold not running, skipping vold shutdown"; |
| 455 | } |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 456 | // logcat stopped here |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 457 | for (const auto& s : ServiceList::GetInstance().services_in_shutdown_order()) { |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 458 | if (kill_after_apps.count(s->name())) s->Stop(); |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 459 | } |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 460 | // 4. sync, try umount, and optionally run fsck for user shutdown |
Tom Cherry | 1f9d540 | 2018-03-15 10:22:40 -0700 | [diff] [blame] | 461 | { |
| 462 | Timer sync_timer; |
| 463 | LOG(INFO) << "sync() before umount..."; |
| 464 | sync(); |
| 465 | LOG(INFO) << "sync() before umount took" << sync_timer; |
| 466 | } |
Tom Cherry | ede0d53 | 2017-07-06 14:20:11 -0700 | [diff] [blame] | 467 | UmountStat stat = TryUmountAndFsck(runFsck, shutdown_timeout - t.duration()); |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 468 | // 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] | 469 | { |
| 470 | Timer sync_timer; |
| 471 | LOG(INFO) << "sync() after umount..."; |
| 472 | sync(); |
| 473 | LOG(INFO) << "sync() after umount took" << sync_timer; |
| 474 | } |
Tom Cherry | 0a72e6c | 2018-03-19 16:19:01 -0700 | [diff] [blame^] | 475 | if (!is_thermal_shutdown) std::this_thread::sleep_for(100ms); |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 476 | LogShutdownTime(stat, &t); |
| 477 | // Reboot regardless of umount status. If umount fails, fsck after reboot will fix it. |
| 478 | RebootSystem(cmd, rebootTarget); |
| 479 | abort(); |
| 480 | } |
Tom Cherry | 98ad32a | 2017-04-17 16:34:20 -0700 | [diff] [blame] | 481 | |
| 482 | bool HandlePowerctlMessage(const std::string& command) { |
| 483 | unsigned int cmd = 0; |
Mark Salyzyn | 6290982 | 2017-10-09 09:27:16 -0700 | [diff] [blame] | 484 | std::vector<std::string> cmd_params = Split(command, ","); |
Tom Cherry | 98ad32a | 2017-04-17 16:34:20 -0700 | [diff] [blame] | 485 | std::string reboot_target = ""; |
| 486 | bool run_fsck = false; |
| 487 | bool command_invalid = false; |
| 488 | |
| 489 | if (cmd_params.size() > 3) { |
| 490 | command_invalid = true; |
| 491 | } else if (cmd_params[0] == "shutdown") { |
| 492 | cmd = ANDROID_RB_POWEROFF; |
Mark Salyzyn | 73e6b49 | 2017-08-14 15:56:53 -0700 | [diff] [blame] | 493 | if (cmd_params.size() == 2) { |
| 494 | if (cmd_params[1] == "userrequested") { |
| 495 | // The shutdown reason is PowerManager.SHUTDOWN_USER_REQUESTED. |
| 496 | // Run fsck once the file system is remounted in read-only mode. |
| 497 | run_fsck = true; |
| 498 | } else if (cmd_params[1] == "thermal") { |
Mark Salyzyn | bfd05b6 | 2017-09-26 11:10:12 -0700 | [diff] [blame] | 499 | // Turn off sources of heat immediately. |
| 500 | TurnOffBacklight(); |
Mark Salyzyn | 73e6b49 | 2017-08-14 15:56:53 -0700 | [diff] [blame] | 501 | // run_fsck is false to avoid delay |
| 502 | cmd = ANDROID_RB_THERMOFF; |
| 503 | } |
Tom Cherry | 98ad32a | 2017-04-17 16:34:20 -0700 | [diff] [blame] | 504 | } |
| 505 | } else if (cmd_params[0] == "reboot") { |
| 506 | cmd = ANDROID_RB_RESTART2; |
| 507 | if (cmd_params.size() >= 2) { |
| 508 | reboot_target = cmd_params[1]; |
| 509 | // When rebooting to the bootloader notify the bootloader writing |
| 510 | // also the BCB. |
| 511 | if (reboot_target == "bootloader") { |
| 512 | std::string err; |
| 513 | if (!write_reboot_bootloader(&err)) { |
| 514 | LOG(ERROR) << "reboot-bootloader: Error writing " |
| 515 | "bootloader_message: " |
| 516 | << err; |
| 517 | } |
| 518 | } |
Mark Salyzyn | 73e6b49 | 2017-08-14 15:56:53 -0700 | [diff] [blame] | 519 | // If there is an additional parameter, pass it along |
| 520 | if ((cmd_params.size() == 3) && cmd_params[2].size()) { |
Tom Cherry | 98ad32a | 2017-04-17 16:34:20 -0700 | [diff] [blame] | 521 | reboot_target += "," + cmd_params[2]; |
| 522 | } |
| 523 | } |
Tom Cherry | 98ad32a | 2017-04-17 16:34:20 -0700 | [diff] [blame] | 524 | } else { |
| 525 | command_invalid = true; |
| 526 | } |
| 527 | if (command_invalid) { |
| 528 | LOG(ERROR) << "powerctl: unrecognized command '" << command << "'"; |
| 529 | return false; |
| 530 | } |
| 531 | |
Wei Wang | eeab491 | 2017-06-27 22:08:45 -0700 | [diff] [blame] | 532 | LOG(INFO) << "Clear action queue and start shutdown trigger"; |
| 533 | ActionManager::GetInstance().ClearQueue(); |
| 534 | // Queue shutdown trigger first |
| 535 | ActionManager::GetInstance().QueueEventTrigger("shutdown"); |
| 536 | // Queue built-in shutdown_done |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 537 | auto shutdown_handler = [cmd, command, reboot_target, run_fsck](const BuiltinArguments&) { |
Wei Wang | eeab491 | 2017-06-27 22:08:45 -0700 | [diff] [blame] | 538 | DoReboot(cmd, command, reboot_target, run_fsck); |
Tom Cherry | 557946e | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 539 | return Success(); |
Wei Wang | eeab491 | 2017-06-27 22:08:45 -0700 | [diff] [blame] | 540 | }; |
| 541 | ActionManager::GetInstance().QueueBuiltinAction(shutdown_handler, "shutdown_done"); |
| 542 | |
| 543 | // Skip wait for prop if it is in progress |
| 544 | ResetWaitForProp(); |
| 545 | |
Tom Cherry | 3b81f2d | 2017-07-28 14:48:41 -0700 | [diff] [blame] | 546 | // Clear EXEC flag if there is one pending |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 547 | for (const auto& s : ServiceList::GetInstance()) { |
| 548 | s->UnSetExec(); |
| 549 | } |
Wei Wang | eeab491 | 2017-06-27 22:08:45 -0700 | [diff] [blame] | 550 | |
Tom Cherry | 98ad32a | 2017-04-17 16:34:20 -0700 | [diff] [blame] | 551 | return true; |
| 552 | } |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 553 | |
| 554 | } // namespace init |
| 555 | } // namespace android |