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