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> |
Jaegeuk Kim | 2aedc82 | 2018-11-20 13:27:06 -0800 | [diff] [blame] | 22 | #include <linux/loop.h> |
josephjang | aaddf28 | 2019-04-16 18:46:24 +0800 | [diff] [blame] | 23 | #include <mntent.h> |
| 24 | #include <semaphore.h> |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 25 | #include <sys/cdefs.h> |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 26 | #include <sys/ioctl.h> |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 27 | #include <sys/mount.h> |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 28 | #include <sys/stat.h> |
josephjang | aaddf28 | 2019-04-16 18:46:24 +0800 | [diff] [blame] | 29 | #include <sys/swap.h> |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 30 | #include <sys/syscall.h> |
| 31 | #include <sys/types.h> |
| 32 | #include <sys/wait.h> |
| 33 | |
| 34 | #include <memory> |
Keun-young Park | 7830d59 | 2017-03-27 16:07:02 -0700 | [diff] [blame] | 35 | #include <set> |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 36 | #include <thread> |
| 37 | #include <vector> |
| 38 | |
Tom Cherry | ede0d53 | 2017-07-06 14:20:11 -0700 | [diff] [blame] | 39 | #include <android-base/chrono_utils.h> |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 40 | #include <android-base/file.h> |
Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 41 | #include <android-base/logging.h> |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 42 | #include <android-base/macros.h> |
Tom Cherry | ccf2353 | 2017-03-28 16:40:41 -0700 | [diff] [blame] | 43 | #include <android-base/properties.h> |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 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" |
Tom Cherry | 2aeb1ad | 2019-06-26 10:46:20 -0700 | [diff] [blame] | 58 | #include "service_list.h" |
Luis Hector Chavez | 9f97f47 | 2017-09-06 13:43:57 -0700 | [diff] [blame] | 59 | #include "sigchld_handler.h" |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 60 | |
josephjang | aaddf28 | 2019-04-16 18:46:24 +0800 | [diff] [blame] | 61 | #define PROC_SYSRQ "/proc/sysrq-trigger" |
| 62 | |
Tom Cherry | 2436e6b | 2019-09-19 11:16:19 -0700 | [diff] [blame] | 63 | using namespace std::literals; |
| 64 | |
Tom Cherry | 4f4cacc | 2019-01-08 10:39:00 -0800 | [diff] [blame] | 65 | using android::base::GetBoolProperty; |
Mark Salyzyn | 6290982 | 2017-10-09 09:27:16 -0700 | [diff] [blame] | 66 | using android::base::Split; |
Tom Cherry | ede0d53 | 2017-07-06 14:20:11 -0700 | [diff] [blame] | 67 | using android::base::Timer; |
Jaegeuk Kim | 2aedc82 | 2018-11-20 13:27:06 -0800 | [diff] [blame] | 68 | using android::base::unique_fd; |
josephjang | aaddf28 | 2019-04-16 18:46:24 +0800 | [diff] [blame] | 69 | using android::base::WriteStringToFile; |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 70 | |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 71 | namespace android { |
| 72 | namespace init { |
| 73 | |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 74 | // represents umount status during reboot / shutdown. |
| 75 | enum UmountStat { |
| 76 | /* umount succeeded. */ |
| 77 | UMOUNT_STAT_SUCCESS = 0, |
| 78 | /* umount was not run. */ |
| 79 | UMOUNT_STAT_SKIPPED = 1, |
| 80 | /* umount failed with timeout. */ |
| 81 | UMOUNT_STAT_TIMEOUT = 2, |
| 82 | /* could not run due to error */ |
| 83 | UMOUNT_STAT_ERROR = 3, |
| 84 | /* not used by init but reserved for other part to use this to represent the |
| 85 | the state where umount status before reboot is not found / available. */ |
| 86 | UMOUNT_STAT_NOT_AVAILABLE = 4, |
| 87 | }; |
| 88 | |
| 89 | // Utility for struct mntent |
| 90 | class MountEntry { |
| 91 | public: |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 92 | explicit MountEntry(const mntent& entry) |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 93 | : mnt_fsname_(entry.mnt_fsname), |
| 94 | mnt_dir_(entry.mnt_dir), |
| 95 | mnt_type_(entry.mnt_type), |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 96 | mnt_opts_(entry.mnt_opts) {} |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 97 | |
Jaegeuk Kim | 0f04f72 | 2017-09-25 10:55:39 -0700 | [diff] [blame] | 98 | bool Umount(bool force) { |
Tom Cherry | c9fec9d | 2018-02-15 14:26:58 -0800 | [diff] [blame] | 99 | LOG(INFO) << "Unmounting " << mnt_fsname_ << ":" << mnt_dir_ << " opts " << mnt_opts_; |
Jaegeuk Kim | 0f04f72 | 2017-09-25 10:55:39 -0700 | [diff] [blame] | 100 | int r = umount2(mnt_dir_.c_str(), force ? MNT_FORCE : 0); |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 101 | if (r == 0) { |
Tom Cherry | c9fec9d | 2018-02-15 14:26:58 -0800 | [diff] [blame] | 102 | LOG(INFO) << "Umounted " << mnt_fsname_ << ":" << mnt_dir_ << " opts " << mnt_opts_; |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 103 | return true; |
| 104 | } else { |
Tom Cherry | c9fec9d | 2018-02-15 14:26:58 -0800 | [diff] [blame] | 105 | PLOG(WARNING) << "Cannot umount " << mnt_fsname_ << ":" << mnt_dir_ << " opts " |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 106 | << mnt_opts_; |
| 107 | return false; |
| 108 | } |
| 109 | } |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 110 | |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 111 | void DoFsck() { |
| 112 | int st; |
| 113 | if (IsF2Fs()) { |
| 114 | const char* f2fs_argv[] = { |
Randall Huang | df2faa4 | 2019-01-15 15:00:56 +0800 | [diff] [blame] | 115 | "/system/bin/fsck.f2fs", |
| 116 | "-a", |
| 117 | mnt_fsname_.c_str(), |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 118 | }; |
Tom Cherry | 3a803eb | 2019-09-25 16:23:50 -0700 | [diff] [blame] | 119 | logwrap_fork_execvp(arraysize(f2fs_argv), f2fs_argv, &st, false, LOG_KLOG, true, |
| 120 | nullptr); |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 121 | } else if (IsExt4()) { |
| 122 | const char* ext4_argv[] = { |
Randall Huang | df2faa4 | 2019-01-15 15:00:56 +0800 | [diff] [blame] | 123 | "/system/bin/e2fsck", |
| 124 | "-y", |
| 125 | mnt_fsname_.c_str(), |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 126 | }; |
Tom Cherry | 3a803eb | 2019-09-25 16:23:50 -0700 | [diff] [blame] | 127 | logwrap_fork_execvp(arraysize(ext4_argv), ext4_argv, &st, false, LOG_KLOG, true, |
| 128 | nullptr); |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 129 | } |
| 130 | } |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 131 | |
| 132 | static bool IsBlockDevice(const struct mntent& mntent) { |
| 133 | return android::base::StartsWith(mntent.mnt_fsname, "/dev/block"); |
| 134 | } |
| 135 | |
| 136 | static bool IsEmulatedDevice(const struct mntent& mntent) { |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 137 | return android::base::StartsWith(mntent.mnt_fsname, "/data/"); |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | private: |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 141 | bool IsF2Fs() const { return mnt_type_ == "f2fs"; } |
| 142 | |
| 143 | bool IsExt4() const { return mnt_type_ == "ext4"; } |
| 144 | |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 145 | std::string mnt_fsname_; |
| 146 | std::string mnt_dir_; |
| 147 | std::string mnt_type_; |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 148 | std::string mnt_opts_; |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 149 | }; |
| 150 | |
| 151 | // Turn off backlight while we are performing power down cleanup activities. |
| 152 | static void TurnOffBacklight() { |
Steven Moreland | d5eccfd | 2018-01-19 13:01:53 -0800 | [diff] [blame] | 153 | Service* service = ServiceList::GetInstance().FindService("blank_screen"); |
| 154 | if (service == nullptr) { |
| 155 | LOG(WARNING) << "cannot find blank_screen in TurnOffBacklight"; |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 156 | return; |
| 157 | } |
Tom Cherry | d987264 | 2018-10-11 10:38:05 -0700 | [diff] [blame] | 158 | if (auto result = service->Start(); !result) { |
| 159 | LOG(WARNING) << "Could not start blank_screen service: " << result.error(); |
| 160 | } |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 163 | static void ShutdownVold() { |
| 164 | const char* vdc_argv[] = {"/system/bin/vdc", "volume", "shutdown"}; |
| 165 | int status; |
Tom Cherry | 3a803eb | 2019-09-25 16:23:50 -0700 | [diff] [blame] | 166 | logwrap_fork_execvp(arraysize(vdc_argv), vdc_argv, &status, false, LOG_KLOG, true, nullptr); |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | static void LogShutdownTime(UmountStat stat, Timer* t) { |
Tom Cherry | ede0d53 | 2017-07-06 14:20:11 -0700 | [diff] [blame] | 170 | LOG(WARNING) << "powerctl_shutdown_time_ms:" << std::to_string(t->duration().count()) << ":" |
| 171 | << stat; |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Tom Cherry | 2436e6b | 2019-09-19 11:16:19 -0700 | [diff] [blame] | 174 | static bool IsDataMounted() { |
| 175 | std::unique_ptr<std::FILE, int (*)(std::FILE*)> fp(setmntent("/proc/mounts", "re"), endmntent); |
| 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) { |
| 182 | if (mentry->mnt_dir == "/data"s) { |
| 183 | return true; |
| 184 | } |
| 185 | } |
| 186 | return false; |
| 187 | } |
| 188 | |
| 189 | // Find all read+write block devices and emulated devices in /proc/mounts and add them to |
| 190 | // the correpsponding list. |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 191 | static bool FindPartitionsToUmount(std::vector<MountEntry>* blockDevPartitions, |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 192 | std::vector<MountEntry>* emulatedPartitions, bool dump) { |
Tom Cherry | f274e78 | 2018-10-03 13:13:41 -0700 | [diff] [blame] | 193 | 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] | 194 | if (fp == nullptr) { |
| 195 | PLOG(ERROR) << "Failed to open /proc/mounts"; |
| 196 | return false; |
| 197 | } |
| 198 | mntent* mentry; |
| 199 | while ((mentry = getmntent(fp.get())) != nullptr) { |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 200 | if (dump) { |
| 201 | LOG(INFO) << "mount entry " << mentry->mnt_fsname << ":" << mentry->mnt_dir << " opts " |
| 202 | << mentry->mnt_opts << " type " << mentry->mnt_type; |
| 203 | } else if (MountEntry::IsBlockDevice(*mentry) && hasmntopt(mentry, "rw")) { |
Keun-young Park | 6e12b38 | 2017-07-17 12:20:33 -0700 | [diff] [blame] | 204 | std::string mount_dir(mentry->mnt_dir); |
| 205 | // These are R/O partitions changed to R/W after adb remount. |
| 206 | // Do not umount them as shutdown critical services may rely on them. |
Wei Wang | a01c27e | 2017-07-25 10:52:08 -0700 | [diff] [blame] | 207 | if (mount_dir != "/" && mount_dir != "/system" && mount_dir != "/vendor" && |
| 208 | mount_dir != "/oem") { |
Keun-young Park | 6e12b38 | 2017-07-17 12:20:33 -0700 | [diff] [blame] | 209 | blockDevPartitions->emplace(blockDevPartitions->begin(), *mentry); |
| 210 | } |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 211 | } else if (MountEntry::IsEmulatedDevice(*mentry)) { |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 212 | emulatedPartitions->emplace(emulatedPartitions->begin(), *mentry); |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | return true; |
| 216 | } |
| 217 | |
Jonglin Lee | 28a2c92 | 2019-01-15 16:38:44 -0800 | [diff] [blame] | 218 | static void DumpUmountDebuggingInfo() { |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 219 | int status; |
| 220 | if (!security_getenforce()) { |
| 221 | LOG(INFO) << "Run lsof"; |
| 222 | const char* lsof_argv[] = {"/system/bin/lsof"}; |
Tom Cherry | 3a803eb | 2019-09-25 16:23:50 -0700 | [diff] [blame] | 223 | logwrap_fork_execvp(arraysize(lsof_argv), lsof_argv, &status, false, LOG_KLOG, true, |
| 224 | nullptr); |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 225 | } |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 226 | FindPartitionsToUmount(nullptr, nullptr, true); |
Jonglin Lee | 28a2c92 | 2019-01-15 16:38:44 -0800 | [diff] [blame] | 227 | // dump current CPU stack traces and uninterruptible tasks |
josephjang | aaddf28 | 2019-04-16 18:46:24 +0800 | [diff] [blame] | 228 | WriteStringToFile("l", PROC_SYSRQ); |
| 229 | WriteStringToFile("w", PROC_SYSRQ); |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Tom Cherry | ede0d53 | 2017-07-06 14:20:11 -0700 | [diff] [blame] | 232 | static UmountStat UmountPartitions(std::chrono::milliseconds timeout) { |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 233 | Timer t; |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 234 | /* data partition needs all pending writes to be completed and all emulated partitions |
| 235 | * umounted.If the current waiting is not good enough, give |
| 236 | * up and leave it to e2fsck after reboot to fix it. |
| 237 | */ |
| 238 | while (true) { |
| 239 | std::vector<MountEntry> block_devices; |
| 240 | std::vector<MountEntry> emulated_devices; |
| 241 | if (!FindPartitionsToUmount(&block_devices, &emulated_devices, false)) { |
| 242 | return UMOUNT_STAT_ERROR; |
| 243 | } |
| 244 | if (block_devices.size() == 0) { |
Wei Wang | 8c00e42 | 2017-08-16 14:01:46 -0700 | [diff] [blame] | 245 | return UMOUNT_STAT_SUCCESS; |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 246 | } |
Wei Wang | 8c00e42 | 2017-08-16 14:01:46 -0700 | [diff] [blame] | 247 | bool unmount_done = true; |
| 248 | if (emulated_devices.size() > 0) { |
Wei Wang | 25dc30f | 2017-10-23 15:32:03 -0700 | [diff] [blame] | 249 | for (auto& entry : emulated_devices) { |
| 250 | if (!entry.Umount(false)) unmount_done = false; |
| 251 | } |
Wei Wang | 8c00e42 | 2017-08-16 14:01:46 -0700 | [diff] [blame] | 252 | if (unmount_done) { |
| 253 | sync(); |
| 254 | } |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 255 | } |
Wei Wang | 25dc30f | 2017-10-23 15:32:03 -0700 | [diff] [blame] | 256 | for (auto& entry : block_devices) { |
| 257 | if (!entry.Umount(timeout == 0ms)) unmount_done = false; |
| 258 | } |
Wei Wang | 8c00e42 | 2017-08-16 14:01:46 -0700 | [diff] [blame] | 259 | if (unmount_done) { |
| 260 | return UMOUNT_STAT_SUCCESS; |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 261 | } |
Wei Wang | 8c00e42 | 2017-08-16 14:01:46 -0700 | [diff] [blame] | 262 | if ((timeout < t.duration())) { // try umount at least once |
| 263 | return UMOUNT_STAT_TIMEOUT; |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 264 | } |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 265 | std::this_thread::sleep_for(100ms); |
| 266 | } |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 267 | } |
| 268 | |
josephjang | aaddf28 | 2019-04-16 18:46:24 +0800 | [diff] [blame] | 269 | static void KillAllProcesses() { |
| 270 | WriteStringToFile("i", PROC_SYSRQ); |
| 271 | } |
| 272 | |
| 273 | // Create reboot/shutdwon monitor thread |
| 274 | void RebootMonitorThread(unsigned int cmd, const std::string& rebootTarget, sem_t* reboot_semaphore, |
| 275 | std::chrono::milliseconds shutdown_timeout, bool* reboot_monitor_run) { |
| 276 | unsigned int remaining_shutdown_time = 0; |
| 277 | |
| 278 | // 30 seconds more than the timeout passed to the thread as there is a final Umount pass |
| 279 | // after the timeout is reached. |
| 280 | constexpr unsigned int shutdown_watchdog_timeout_default = 30; |
| 281 | auto shutdown_watchdog_timeout = android::base::GetUintProperty( |
| 282 | "ro.build.shutdown.watchdog.timeout", shutdown_watchdog_timeout_default); |
| 283 | remaining_shutdown_time = shutdown_watchdog_timeout + shutdown_timeout.count() / 1000; |
| 284 | |
| 285 | while (*reboot_monitor_run == true) { |
| 286 | if (TEMP_FAILURE_RETRY(sem_wait(reboot_semaphore)) == -1) { |
| 287 | LOG(ERROR) << "sem_wait failed and exit RebootMonitorThread()"; |
| 288 | return; |
| 289 | } |
| 290 | |
| 291 | timespec shutdown_timeout_timespec; |
| 292 | if (clock_gettime(CLOCK_MONOTONIC, &shutdown_timeout_timespec) == -1) { |
| 293 | LOG(ERROR) << "clock_gettime() fail! exit RebootMonitorThread()"; |
| 294 | return; |
| 295 | } |
| 296 | |
| 297 | // If there are some remaining shutdown time left from previous round, we use |
| 298 | // remaining time here. |
| 299 | shutdown_timeout_timespec.tv_sec += remaining_shutdown_time; |
| 300 | |
| 301 | LOG(INFO) << "shutdown_timeout_timespec.tv_sec: " << shutdown_timeout_timespec.tv_sec; |
| 302 | |
| 303 | int sem_return = 0; |
| 304 | while ((sem_return = sem_timedwait_monotonic_np(reboot_semaphore, |
| 305 | &shutdown_timeout_timespec)) == -1 && |
| 306 | errno == EINTR) { |
| 307 | } |
| 308 | |
| 309 | if (sem_return == -1) { |
| 310 | LOG(ERROR) << "Reboot thread timed out"; |
| 311 | |
| 312 | if (android::base::GetBoolProperty("ro.debuggable", false) == true) { |
Tom Cherry | 2436e6b | 2019-09-19 11:16:19 -0700 | [diff] [blame] | 313 | if (false) { |
| 314 | // SEPolicy will block debuggerd from running and this is intentional. |
| 315 | // But these lines are left to be enabled during debugging. |
| 316 | LOG(INFO) << "Try to dump init process call trace:"; |
| 317 | const char* vdc_argv[] = {"/system/bin/debuggerd", "-b", "1"}; |
| 318 | int status; |
Tom Cherry | 3a803eb | 2019-09-25 16:23:50 -0700 | [diff] [blame] | 319 | logwrap_fork_execvp(arraysize(vdc_argv), vdc_argv, &status, false, LOG_KLOG, |
| 320 | true, nullptr); |
Tom Cherry | 2436e6b | 2019-09-19 11:16:19 -0700 | [diff] [blame] | 321 | } |
josephjang | aaddf28 | 2019-04-16 18:46:24 +0800 | [diff] [blame] | 322 | LOG(INFO) << "Show stack for all active CPU:"; |
| 323 | WriteStringToFile("l", PROC_SYSRQ); |
| 324 | |
| 325 | LOG(INFO) << "Show tasks that are in disk sleep(uninterruptable sleep), which are " |
| 326 | "like " |
| 327 | "blocked in mutex or hardware register access:"; |
| 328 | WriteStringToFile("w", PROC_SYSRQ); |
| 329 | } |
| 330 | |
| 331 | // In shutdown case,notify kernel to sync and umount fs to read-only before shutdown. |
| 332 | if (cmd == ANDROID_RB_POWEROFF || cmd == ANDROID_RB_THERMOFF) { |
| 333 | WriteStringToFile("s", PROC_SYSRQ); |
| 334 | |
| 335 | WriteStringToFile("u", PROC_SYSRQ); |
| 336 | |
| 337 | RebootSystem(cmd, rebootTarget); |
| 338 | } |
| 339 | |
| 340 | LOG(ERROR) << "Trigger crash at last!"; |
| 341 | WriteStringToFile("c", PROC_SYSRQ); |
| 342 | } else { |
| 343 | timespec current_time_timespec; |
| 344 | |
| 345 | if (clock_gettime(CLOCK_MONOTONIC, ¤t_time_timespec) == -1) { |
| 346 | LOG(ERROR) << "clock_gettime() fail! exit RebootMonitorThread()"; |
| 347 | return; |
| 348 | } |
| 349 | |
| 350 | remaining_shutdown_time = |
| 351 | shutdown_timeout_timespec.tv_sec - current_time_timespec.tv_sec; |
| 352 | |
| 353 | LOG(INFO) << "remaining_shutdown_time: " << remaining_shutdown_time; |
| 354 | } |
| 355 | } |
| 356 | } |
Keun-young Park | 3ee0df9 | 2017-03-27 11:21:09 -0700 | [diff] [blame] | 357 | |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 358 | /* Try umounting all emulated file systems R/W block device cfile systems. |
| 359 | * This will just try umount and give it up if it fails. |
| 360 | * For fs like ext4, this is ok as file system will be marked as unclean shutdown |
| 361 | * and necessary check can be done at the next reboot. |
| 362 | * For safer shutdown, caller needs to make sure that |
| 363 | * all processes / emulated partition for the target fs are all cleaned-up. |
| 364 | * |
| 365 | * return true when umount was successful. false when timed out. |
| 366 | */ |
josephjang | aaddf28 | 2019-04-16 18:46:24 +0800 | [diff] [blame] | 367 | static UmountStat TryUmountAndFsck(unsigned int cmd, const std::string& rebootTarget, bool runFsck, |
| 368 | std::chrono::milliseconds timeout, sem_t* reboot_semaphore) { |
Keun-young Park | 3ee0df9 | 2017-03-27 11:21:09 -0700 | [diff] [blame] | 369 | Timer t; |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 370 | std::vector<MountEntry> block_devices; |
| 371 | std::vector<MountEntry> emulated_devices; |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 372 | |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 373 | if (runFsck && !FindPartitionsToUmount(&block_devices, &emulated_devices, false)) { |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 374 | return UMOUNT_STAT_ERROR; |
| 375 | } |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 376 | |
Tom Cherry | ede0d53 | 2017-07-06 14:20:11 -0700 | [diff] [blame] | 377 | UmountStat stat = UmountPartitions(timeout - t.duration()); |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 378 | if (stat != UMOUNT_STAT_SUCCESS) { |
| 379 | LOG(INFO) << "umount timeout, last resort, kill all and try"; |
Jonglin Lee | 28a2c92 | 2019-01-15 16:38:44 -0800 | [diff] [blame] | 380 | if (DUMP_ON_UMOUNT_FAILURE) DumpUmountDebuggingInfo(); |
Keun-young Park | 3ee0df9 | 2017-03-27 11:21:09 -0700 | [diff] [blame] | 381 | KillAllProcesses(); |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 382 | // 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] | 383 | UmountStat st = UmountPartitions(0ms); |
Jonglin Lee | 28a2c92 | 2019-01-15 16:38:44 -0800 | [diff] [blame] | 384 | if ((st != UMOUNT_STAT_SUCCESS) && DUMP_ON_UMOUNT_FAILURE) DumpUmountDebuggingInfo(); |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 385 | } |
| 386 | |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 387 | if (stat == UMOUNT_STAT_SUCCESS && runFsck) { |
josephjang | aaddf28 | 2019-04-16 18:46:24 +0800 | [diff] [blame] | 388 | LOG(INFO) << "Pause reboot monitor thread before fsck"; |
| 389 | sem_post(reboot_semaphore); |
| 390 | |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 391 | // fsck part is excluded from timeout check. It only runs for user initiated shutdown |
| 392 | // and should not affect reboot time. |
| 393 | for (auto& entry : block_devices) { |
| 394 | entry.DoFsck(); |
| 395 | } |
josephjang | aaddf28 | 2019-04-16 18:46:24 +0800 | [diff] [blame] | 396 | |
| 397 | LOG(INFO) << "Resume reboot monitor thread after fsck"; |
| 398 | sem_post(reboot_semaphore); |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 399 | } |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 400 | return stat; |
| 401 | } |
| 402 | |
Jaegeuk Kim | 2aedc82 | 2018-11-20 13:27:06 -0800 | [diff] [blame] | 403 | // zram is able to use backing device on top of a loopback device. |
| 404 | // In order to unmount /data successfully, we have to kill the loopback device first |
| 405 | #define ZRAM_DEVICE "/dev/block/zram0" |
| 406 | #define ZRAM_RESET "/sys/block/zram0/reset" |
| 407 | #define ZRAM_BACK_DEV "/sys/block/zram0/backing_dev" |
| 408 | static void KillZramBackingDevice() { |
| 409 | std::string backing_dev; |
| 410 | if (!android::base::ReadFileToString(ZRAM_BACK_DEV, &backing_dev)) return; |
| 411 | |
| 412 | if (!android::base::StartsWith(backing_dev, "/dev/block/loop")) return; |
| 413 | |
| 414 | // cut the last "\n" |
| 415 | backing_dev.erase(backing_dev.length() - 1); |
| 416 | |
| 417 | // shutdown zram handle |
| 418 | Timer swap_timer; |
| 419 | LOG(INFO) << "swapoff() start..."; |
| 420 | if (swapoff(ZRAM_DEVICE) == -1) { |
| 421 | LOG(ERROR) << "zram_backing_dev: swapoff (" << backing_dev << ")" << " failed"; |
| 422 | return; |
| 423 | } |
| 424 | LOG(INFO) << "swapoff() took " << swap_timer;; |
| 425 | |
josephjang | aaddf28 | 2019-04-16 18:46:24 +0800 | [diff] [blame] | 426 | if (!WriteStringToFile("1", ZRAM_RESET)) { |
Jaegeuk Kim | 2aedc82 | 2018-11-20 13:27:06 -0800 | [diff] [blame] | 427 | LOG(ERROR) << "zram_backing_dev: reset (" << backing_dev << ")" << " failed"; |
| 428 | return; |
| 429 | } |
| 430 | |
| 431 | // clear loopback device |
| 432 | unique_fd loop(TEMP_FAILURE_RETRY(open(backing_dev.c_str(), O_RDWR | O_CLOEXEC))); |
| 433 | if (loop.get() < 0) { |
| 434 | LOG(ERROR) << "zram_backing_dev: open(" << backing_dev << ")" << " failed"; |
| 435 | return; |
| 436 | } |
| 437 | |
| 438 | if (ioctl(loop.get(), LOOP_CLR_FD, 0) < 0) { |
| 439 | LOG(ERROR) << "zram_backing_dev: loop_clear (" << backing_dev << ")" << " failed"; |
| 440 | return; |
| 441 | } |
| 442 | LOG(INFO) << "zram_backing_dev: `" << backing_dev << "` is cleared successfully."; |
| 443 | } |
| 444 | |
Tom Cherry | 44aceed | 2018-08-03 13:36:18 -0700 | [diff] [blame] | 445 | //* Reboot / shutdown the system. |
| 446 | // cmd ANDROID_RB_* as defined in android_reboot.h |
| 447 | // reason Reason string like "reboot", "shutdown,userrequested" |
| 448 | // rebootTarget Reboot target string like "bootloader". Otherwise, it should be an |
| 449 | // empty string. |
| 450 | // runFsck Whether to run fsck after umount is done. |
| 451 | // |
| 452 | static void DoReboot(unsigned int cmd, const std::string& reason, const std::string& rebootTarget, |
| 453 | bool runFsck) { |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 454 | Timer t; |
Tom Cherry | 0a72e6c | 2018-03-19 16:19:01 -0700 | [diff] [blame] | 455 | LOG(INFO) << "Reboot start, reason: " << reason << ", rebootTarget: " << rebootTarget; |
| 456 | |
Tom Cherry | 2436e6b | 2019-09-19 11:16:19 -0700 | [diff] [blame] | 457 | // If /data isn't mounted then we can skip the extra reboot steps below, since we don't need to |
| 458 | // worry about unmounting it. |
| 459 | if (!IsDataMounted()) { |
| 460 | sync(); |
| 461 | RebootSystem(cmd, rebootTarget); |
| 462 | abort(); |
| 463 | } |
| 464 | |
Tom Cherry | 0a72e6c | 2018-03-19 16:19:01 -0700 | [diff] [blame] | 465 | // Ensure last reboot reason is reduced to canonical |
| 466 | // alias reported in bootloader or system boot reason. |
| 467 | size_t skip = 0; |
| 468 | std::vector<std::string> reasons = Split(reason, ","); |
| 469 | if (reasons.size() >= 2 && reasons[0] == "reboot" && |
| 470 | (reasons[1] == "recovery" || reasons[1] == "bootloader" || reasons[1] == "cold" || |
| 471 | reasons[1] == "hard" || reasons[1] == "warm")) { |
| 472 | skip = strlen("reboot,"); |
| 473 | } |
| 474 | property_set(LAST_REBOOT_REASON_PROPERTY, reason.c_str() + skip); |
| 475 | sync(); |
| 476 | |
| 477 | bool is_thermal_shutdown = cmd == ANDROID_RB_THERMOFF; |
| 478 | |
| 479 | auto shutdown_timeout = 0ms; |
| 480 | if (!SHUTDOWN_ZERO_TIMEOUT) { |
Wei Wang | b5de088 | 2018-10-09 12:42:06 -0700 | [diff] [blame] | 481 | constexpr unsigned int shutdown_timeout_default = 6; |
| 482 | constexpr unsigned int max_thermal_shutdown_timeout = 3; |
| 483 | auto shutdown_timeout_final = android::base::GetUintProperty("ro.build.shutdown_timeout", |
| 484 | shutdown_timeout_default); |
| 485 | if (is_thermal_shutdown && shutdown_timeout_final > max_thermal_shutdown_timeout) { |
| 486 | shutdown_timeout_final = max_thermal_shutdown_timeout; |
Tom Cherry | 0a72e6c | 2018-03-19 16:19:01 -0700 | [diff] [blame] | 487 | } |
Wei Wang | b5de088 | 2018-10-09 12:42:06 -0700 | [diff] [blame] | 488 | shutdown_timeout = std::chrono::seconds(shutdown_timeout_final); |
Tom Cherry | 0a72e6c | 2018-03-19 16:19:01 -0700 | [diff] [blame] | 489 | } |
| 490 | LOG(INFO) << "Shutdown timeout: " << shutdown_timeout.count() << " ms"; |
| 491 | |
josephjang | aaddf28 | 2019-04-16 18:46:24 +0800 | [diff] [blame] | 492 | sem_t reboot_semaphore; |
| 493 | if (sem_init(&reboot_semaphore, false, 0) == -1) { |
| 494 | // These should never fail, but if they do, skip the graceful reboot and reboot immediately. |
| 495 | LOG(ERROR) << "sem_init() fail and RebootSystem() return!"; |
| 496 | RebootSystem(cmd, rebootTarget); |
| 497 | } |
| 498 | |
| 499 | // Start a thread to monitor init shutdown process |
| 500 | LOG(INFO) << "Create reboot monitor thread."; |
| 501 | bool reboot_monitor_run = true; |
| 502 | std::thread reboot_monitor_thread(&RebootMonitorThread, cmd, rebootTarget, &reboot_semaphore, |
| 503 | shutdown_timeout, &reboot_monitor_run); |
| 504 | reboot_monitor_thread.detach(); |
| 505 | |
| 506 | // Start reboot monitor thread |
| 507 | sem_post(&reboot_semaphore); |
| 508 | |
Keun-young Park | 7830d59 | 2017-03-27 16:07:02 -0700 | [diff] [blame] | 509 | // keep debugging tools until non critical ones are all gone. |
| 510 | const std::set<std::string> kill_after_apps{"tombstoned", "logd", "adbd"}; |
| 511 | // 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] | 512 | const std::set<std::string> to_starts{"watchdogd"}; |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 513 | for (const auto& s : ServiceList::GetInstance()) { |
Keun-young Park | 7830d59 | 2017-03-27 16:07:02 -0700 | [diff] [blame] | 514 | if (kill_after_apps.count(s->name())) { |
| 515 | s->SetShutdownCritical(); |
| 516 | } else if (to_starts.count(s->name())) { |
Tom Cherry | 702ca9a | 2017-08-25 10:36:52 -0700 | [diff] [blame] | 517 | if (auto result = s->Start(); !result) { |
| 518 | LOG(ERROR) << "Could not start shutdown 'to_start' service '" << s->name() |
| 519 | << "': " << result.error(); |
| 520 | } |
Keun-young Park | 7830d59 | 2017-03-27 16:07:02 -0700 | [diff] [blame] | 521 | s->SetShutdownCritical(); |
Keun-young Park | cccb34f | 2017-07-05 11:38:44 -0700 | [diff] [blame] | 522 | } else if (s->IsShutdownCritical()) { |
Tom Cherry | 702ca9a | 2017-08-25 10:36:52 -0700 | [diff] [blame] | 523 | // Start shutdown critical service if not started. |
| 524 | if (auto result = s->Start(); !result) { |
| 525 | LOG(ERROR) << "Could not start shutdown critical service '" << s->name() |
| 526 | << "': " << result.error(); |
| 527 | } |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 528 | } |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 529 | } |
Keun-young Park | 7830d59 | 2017-03-27 16:07:02 -0700 | [diff] [blame] | 530 | |
Steven Moreland | d5eccfd | 2018-01-19 13:01:53 -0800 | [diff] [blame] | 531 | // remaining operations (specifically fsck) may take a substantial duration |
Tom Cherry | 0a72e6c | 2018-03-19 16:19:01 -0700 | [diff] [blame] | 532 | if (cmd == ANDROID_RB_POWEROFF || is_thermal_shutdown) { |
Steven Moreland | d5eccfd | 2018-01-19 13:01:53 -0800 | [diff] [blame] | 533 | TurnOffBacklight(); |
| 534 | } |
| 535 | |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 536 | Service* bootAnim = ServiceList::GetInstance().FindService("bootanim"); |
| 537 | Service* surfaceFlinger = ServiceList::GetInstance().FindService("surfaceflinger"); |
Keun-young Park | 7830d59 | 2017-03-27 16:07:02 -0700 | [diff] [blame] | 538 | if (bootAnim != nullptr && surfaceFlinger != nullptr && surfaceFlinger->IsRunning()) { |
Tom Cherry | 4f4cacc | 2019-01-08 10:39:00 -0800 | [diff] [blame] | 539 | bool do_shutdown_animation = GetBoolProperty("ro.init.shutdown_animation", false); |
| 540 | |
| 541 | if (do_shutdown_animation) { |
| 542 | property_set("service.bootanim.exit", "0"); |
| 543 | // Could be in the middle of animation. Stop and start so that it can pick |
| 544 | // up the right mode. |
| 545 | bootAnim->Stop(); |
| 546 | } |
| 547 | |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 548 | for (const auto& service : ServiceList::GetInstance()) { |
Tom Cherry | 4f4cacc | 2019-01-08 10:39:00 -0800 | [diff] [blame] | 549 | if (service->classnames().count("animation") == 0) { |
| 550 | continue; |
| 551 | } |
| 552 | |
| 553 | // start all animation classes if stopped. |
| 554 | if (do_shutdown_animation) { |
Tom Cherry | 9949ec5 | 2019-05-16 16:54:49 -0700 | [diff] [blame] | 555 | service->Start(); |
Tom Cherry | 4f4cacc | 2019-01-08 10:39:00 -0800 | [diff] [blame] | 556 | } |
| 557 | service->SetShutdownCritical(); // will not check animation class separately |
| 558 | } |
| 559 | |
| 560 | if (do_shutdown_animation) { |
Tom Cherry | 9949ec5 | 2019-05-16 16:54:49 -0700 | [diff] [blame] | 561 | bootAnim->Start(); |
Tom Cherry | 4f4cacc | 2019-01-08 10:39:00 -0800 | [diff] [blame] | 562 | surfaceFlinger->SetShutdownCritical(); |
| 563 | bootAnim->SetShutdownCritical(); |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 564 | } |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 565 | } |
Keun-young Park | 7830d59 | 2017-03-27 16:07:02 -0700 | [diff] [blame] | 566 | |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 567 | // optional shutdown step |
| 568 | // 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] | 569 | if (shutdown_timeout > 0ms) { |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 570 | LOG(INFO) << "terminating init services"; |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 571 | |
| 572 | // Ask all services to terminate except shutdown critical ones. |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 573 | for (const auto& s : ServiceList::GetInstance().services_in_shutdown_order()) { |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 574 | if (!s->IsShutdownCritical()) s->Terminate(); |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 575 | } |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 576 | |
| 577 | int service_count = 0; |
Keun-young Park | 3017387 | 2017-07-18 10:58:28 -0700 | [diff] [blame] | 578 | // Only wait up to half of timeout here |
| 579 | auto termination_wait_timeout = shutdown_timeout / 2; |
Tom Cherry | ede0d53 | 2017-07-06 14:20:11 -0700 | [diff] [blame] | 580 | while (t.duration() < termination_wait_timeout) { |
Tom Cherry | eeee831 | 2017-07-28 15:22:23 -0700 | [diff] [blame] | 581 | ReapAnyOutstandingChildren(); |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 582 | |
| 583 | service_count = 0; |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 584 | for (const auto& s : ServiceList::GetInstance()) { |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 585 | // Count the number of services running except shutdown critical. |
| 586 | // Exclude the console as it will ignore the SIGTERM signal |
| 587 | // and not exit. |
| 588 | // Note: SVC_CONSOLE actually means "requires console" but |
| 589 | // it is only used by the shell. |
| 590 | if (!s->IsShutdownCritical() && s->pid() != 0 && (s->flags() & SVC_CONSOLE) == 0) { |
| 591 | service_count++; |
| 592 | } |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 593 | } |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 594 | |
| 595 | if (service_count == 0) { |
| 596 | // All terminable services terminated. We can exit early. |
| 597 | break; |
| 598 | } |
| 599 | |
| 600 | // Wait a bit before recounting the number or running services. |
| 601 | std::this_thread::sleep_for(50ms); |
| 602 | } |
| 603 | LOG(INFO) << "Terminating running services took " << t |
| 604 | << " with remaining services:" << service_count; |
| 605 | } |
| 606 | |
| 607 | // minimum safety steps before restarting |
| 608 | // 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] | 609 | for (const auto& s : ServiceList::GetInstance().services_in_shutdown_order()) { |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 610 | if (!s->IsShutdownCritical()) s->Stop(); |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 611 | } |
Luis Hector Chavez | 92c49bc | 2018-07-27 11:19:25 -0700 | [diff] [blame] | 612 | SubcontextTerminate(); |
Tom Cherry | eeee831 | 2017-07-28 15:22:23 -0700 | [diff] [blame] | 613 | ReapAnyOutstandingChildren(); |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 614 | |
| 615 | // 3. send volume shutdown to vold |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 616 | Service* voldService = ServiceList::GetInstance().FindService("vold"); |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 617 | if (voldService != nullptr && voldService->IsRunning()) { |
| 618 | ShutdownVold(); |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 619 | voldService->Stop(); |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 620 | } else { |
| 621 | LOG(INFO) << "vold not running, skipping vold shutdown"; |
| 622 | } |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 623 | // logcat stopped here |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 624 | for (const auto& s : ServiceList::GetInstance().services_in_shutdown_order()) { |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 625 | if (kill_after_apps.count(s->name())) s->Stop(); |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 626 | } |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 627 | // 4. sync, try umount, and optionally run fsck for user shutdown |
Tom Cherry | 1f9d540 | 2018-03-15 10:22:40 -0700 | [diff] [blame] | 628 | { |
| 629 | Timer sync_timer; |
| 630 | LOG(INFO) << "sync() before umount..."; |
| 631 | sync(); |
| 632 | LOG(INFO) << "sync() before umount took" << sync_timer; |
| 633 | } |
Jaegeuk Kim | 2aedc82 | 2018-11-20 13:27:06 -0800 | [diff] [blame] | 634 | // 5. drop caches and disable zram backing device, if exist |
| 635 | KillZramBackingDevice(); |
| 636 | |
josephjang | aaddf28 | 2019-04-16 18:46:24 +0800 | [diff] [blame] | 637 | UmountStat stat = TryUmountAndFsck(cmd, rebootTarget, runFsck, shutdown_timeout - t.duration(), |
| 638 | &reboot_semaphore); |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 639 | // 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] | 640 | { |
| 641 | Timer sync_timer; |
| 642 | LOG(INFO) << "sync() after umount..."; |
| 643 | sync(); |
| 644 | LOG(INFO) << "sync() after umount took" << sync_timer; |
| 645 | } |
Tom Cherry | 0a72e6c | 2018-03-19 16:19:01 -0700 | [diff] [blame] | 646 | if (!is_thermal_shutdown) std::this_thread::sleep_for(100ms); |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 647 | LogShutdownTime(stat, &t); |
josephjang | aaddf28 | 2019-04-16 18:46:24 +0800 | [diff] [blame] | 648 | |
| 649 | // Send signal to terminate reboot monitor thread. |
| 650 | reboot_monitor_run = false; |
| 651 | sem_post(&reboot_semaphore); |
| 652 | |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 653 | // Reboot regardless of umount status. If umount fails, fsck after reboot will fix it. |
| 654 | RebootSystem(cmd, rebootTarget); |
| 655 | abort(); |
| 656 | } |
Tom Cherry | 98ad32a | 2017-04-17 16:34:20 -0700 | [diff] [blame] | 657 | |
| 658 | bool HandlePowerctlMessage(const std::string& command) { |
| 659 | unsigned int cmd = 0; |
Mark Salyzyn | 6290982 | 2017-10-09 09:27:16 -0700 | [diff] [blame] | 660 | std::vector<std::string> cmd_params = Split(command, ","); |
Tom Cherry | 98ad32a | 2017-04-17 16:34:20 -0700 | [diff] [blame] | 661 | std::string reboot_target = ""; |
| 662 | bool run_fsck = false; |
| 663 | bool command_invalid = false; |
| 664 | |
Mark Salyzyn | d7931f1 | 2019-07-10 10:33:09 -0700 | [diff] [blame] | 665 | if (cmd_params[0] == "shutdown") { |
Tom Cherry | 98ad32a | 2017-04-17 16:34:20 -0700 | [diff] [blame] | 666 | cmd = ANDROID_RB_POWEROFF; |
Mark Salyzyn | d7931f1 | 2019-07-10 10:33:09 -0700 | [diff] [blame] | 667 | if (cmd_params.size() >= 2) { |
Mark Salyzyn | 73e6b49 | 2017-08-14 15:56:53 -0700 | [diff] [blame] | 668 | if (cmd_params[1] == "userrequested") { |
| 669 | // The shutdown reason is PowerManager.SHUTDOWN_USER_REQUESTED. |
| 670 | // Run fsck once the file system is remounted in read-only mode. |
| 671 | run_fsck = true; |
| 672 | } else if (cmd_params[1] == "thermal") { |
Mark Salyzyn | bfd05b6 | 2017-09-26 11:10:12 -0700 | [diff] [blame] | 673 | // Turn off sources of heat immediately. |
| 674 | TurnOffBacklight(); |
Mark Salyzyn | 73e6b49 | 2017-08-14 15:56:53 -0700 | [diff] [blame] | 675 | // run_fsck is false to avoid delay |
| 676 | cmd = ANDROID_RB_THERMOFF; |
| 677 | } |
Tom Cherry | 98ad32a | 2017-04-17 16:34:20 -0700 | [diff] [blame] | 678 | } |
| 679 | } else if (cmd_params[0] == "reboot") { |
| 680 | cmd = ANDROID_RB_RESTART2; |
| 681 | if (cmd_params.size() >= 2) { |
| 682 | reboot_target = cmd_params[1]; |
Hridya Valsaraju | 5425826 | 2018-09-19 21:14:18 -0700 | [diff] [blame] | 683 | // adb reboot fastboot should boot into bootloader for devices not |
| 684 | // supporting logical partitions. |
| 685 | if (reboot_target == "fastboot" && |
Yifan Hong | 0e0f818 | 2018-11-16 12:49:06 -0800 | [diff] [blame] | 686 | !android::base::GetBoolProperty("ro.boot.dynamic_partitions", false)) { |
Hridya Valsaraju | 5425826 | 2018-09-19 21:14:18 -0700 | [diff] [blame] | 687 | reboot_target = "bootloader"; |
| 688 | } |
Tom Cherry | 98ad32a | 2017-04-17 16:34:20 -0700 | [diff] [blame] | 689 | // When rebooting to the bootloader notify the bootloader writing |
| 690 | // also the BCB. |
| 691 | if (reboot_target == "bootloader") { |
| 692 | std::string err; |
| 693 | if (!write_reboot_bootloader(&err)) { |
| 694 | LOG(ERROR) << "reboot-bootloader: Error writing " |
| 695 | "bootloader_message: " |
| 696 | << err; |
| 697 | } |
Tianjie Xu | 5e98b63 | 2019-07-18 12:48:28 -0700 | [diff] [blame] | 698 | } else if (reboot_target == "recovery") { |
| 699 | bootloader_message boot = {}; |
| 700 | if (std::string err; !read_bootloader_message(&boot, &err)) { |
| 701 | LOG(ERROR) << "Failed to read bootloader message: " << err; |
| 702 | } |
| 703 | // Update the boot command field if it's empty, and preserve |
| 704 | // the other arguments in the bootloader message. |
| 705 | if (boot.command[0] == '\0') { |
| 706 | strlcpy(boot.command, "boot-recovery", sizeof(boot.command)); |
| 707 | if (std::string err; !write_bootloader_message(boot, &err)) { |
| 708 | LOG(ERROR) << "Failed to set bootloader message: " << err; |
| 709 | return false; |
| 710 | } |
| 711 | } |
Hridya Valsaraju | 71fb82a | 2018-08-02 15:02:06 -0700 | [diff] [blame] | 712 | } else if (reboot_target == "sideload" || reboot_target == "sideload-auto-reboot" || |
| 713 | reboot_target == "fastboot") { |
| 714 | std::string arg = reboot_target == "sideload-auto-reboot" ? "sideload_auto_reboot" |
| 715 | : reboot_target; |
| 716 | const std::vector<std::string> options = { |
| 717 | "--" + arg, |
| 718 | }; |
| 719 | std::string err; |
| 720 | if (!write_bootloader_message(options, &err)) { |
| 721 | LOG(ERROR) << "Failed to set bootloader message: " << err; |
| 722 | return false; |
| 723 | } |
| 724 | reboot_target = "recovery"; |
Tom Cherry | 98ad32a | 2017-04-17 16:34:20 -0700 | [diff] [blame] | 725 | } |
Hridya Valsaraju | 71fb82a | 2018-08-02 15:02:06 -0700 | [diff] [blame] | 726 | |
Mark Salyzyn | d7931f1 | 2019-07-10 10:33:09 -0700 | [diff] [blame] | 727 | // If there are additional parameter, pass them along |
| 728 | for (size_t i = 2; (cmd_params.size() > i) && cmd_params[i].size(); ++i) { |
| 729 | reboot_target += "," + cmd_params[i]; |
Tom Cherry | 98ad32a | 2017-04-17 16:34:20 -0700 | [diff] [blame] | 730 | } |
| 731 | } |
Tom Cherry | 98ad32a | 2017-04-17 16:34:20 -0700 | [diff] [blame] | 732 | } else { |
| 733 | command_invalid = true; |
| 734 | } |
| 735 | if (command_invalid) { |
| 736 | LOG(ERROR) << "powerctl: unrecognized command '" << command << "'"; |
| 737 | return false; |
| 738 | } |
| 739 | |
Wei Wang | eeab491 | 2017-06-27 22:08:45 -0700 | [diff] [blame] | 740 | LOG(INFO) << "Clear action queue and start shutdown trigger"; |
| 741 | ActionManager::GetInstance().ClearQueue(); |
| 742 | // Queue shutdown trigger first |
| 743 | ActionManager::GetInstance().QueueEventTrigger("shutdown"); |
| 744 | // Queue built-in shutdown_done |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 745 | auto shutdown_handler = [cmd, command, reboot_target, run_fsck](const BuiltinArguments&) { |
Wei Wang | eeab491 | 2017-06-27 22:08:45 -0700 | [diff] [blame] | 746 | DoReboot(cmd, command, reboot_target, run_fsck); |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 747 | return Result<void>{}; |
Wei Wang | eeab491 | 2017-06-27 22:08:45 -0700 | [diff] [blame] | 748 | }; |
| 749 | ActionManager::GetInstance().QueueBuiltinAction(shutdown_handler, "shutdown_done"); |
| 750 | |
| 751 | // Skip wait for prop if it is in progress |
| 752 | ResetWaitForProp(); |
| 753 | |
Tom Cherry | 3b81f2d | 2017-07-28 14:48:41 -0700 | [diff] [blame] | 754 | // Clear EXEC flag if there is one pending |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 755 | for (const auto& s : ServiceList::GetInstance()) { |
| 756 | s->UnSetExec(); |
| 757 | } |
Wei Wang | eeab491 | 2017-06-27 22:08:45 -0700 | [diff] [blame] | 758 | |
Tom Cherry | 1ab3dfc | 2019-04-22 17:46:37 -0700 | [diff] [blame] | 759 | // We no longer process messages about properties changing coming from property service, so we |
| 760 | // need to tell property service to stop sending us these messages, otherwise it'll fill the |
| 761 | // buffers and block indefinitely, causing future property sets, including those that init makes |
| 762 | // during shutdown in Service::NotifyStateChange() to also block indefinitely. |
| 763 | SendStopSendingMessagesMessage(); |
| 764 | |
Tom Cherry | 98ad32a | 2017-04-17 16:34:20 -0700 | [diff] [blame] | 765 | return true; |
| 766 | } |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 767 | |
| 768 | } // namespace init |
| 769 | } // namespace android |