Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 16 | |
| 17 | #include "reboot.h" |
| 18 | |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 19 | #include <dirent.h> |
| 20 | #include <fcntl.h> |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 21 | #include <linux/fs.h> |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 22 | #include <mntent.h> |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 23 | #include <selinux/selinux.h> |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 24 | #include <sys/cdefs.h> |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 25 | #include <sys/ioctl.h> |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 26 | #include <sys/mount.h> |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 27 | #include <sys/reboot.h> |
| 28 | #include <sys/stat.h> |
| 29 | #include <sys/syscall.h> |
| 30 | #include <sys/types.h> |
| 31 | #include <sys/wait.h> |
| 32 | |
| 33 | #include <memory> |
Keun-young Park | 7830d59 | 2017-03-27 16:07:02 -0700 | [diff] [blame] | 34 | #include <set> |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 35 | #include <thread> |
| 36 | #include <vector> |
| 37 | |
| 38 | #include <android-base/file.h> |
Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 39 | #include <android-base/logging.h> |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 40 | #include <android-base/macros.h> |
Tom Cherry | ccf2353 | 2017-03-28 16:40:41 -0700 | [diff] [blame] | 41 | #include <android-base/properties.h> |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 42 | #include <android-base/stringprintf.h> |
| 43 | #include <android-base/strings.h> |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 44 | #include <android-base/unique_fd.h> |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 45 | #include <bootloader_message/bootloader_message.h> |
| 46 | #include <cutils/android_reboot.h> |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 47 | #include <fs_mgr.h> |
| 48 | #include <logwrap/logwrap.h> |
Todd Poynor | fc827be | 2017-04-13 15:17:24 -0700 | [diff] [blame] | 49 | #include <private/android_filesystem_config.h> |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 50 | |
Wei Wang | 5d4d6f7 | 2017-06-27 22:08:45 -0700 | [diff] [blame^] | 51 | #include "init.h" |
Keun-young Park | 7830d59 | 2017-03-27 16:07:02 -0700 | [diff] [blame] | 52 | #include "property_service.h" |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 53 | #include "service.h" |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 54 | |
| 55 | using android::base::StringPrintf; |
| 56 | |
| 57 | // represents umount status during reboot / shutdown. |
| 58 | enum UmountStat { |
| 59 | /* umount succeeded. */ |
| 60 | UMOUNT_STAT_SUCCESS = 0, |
| 61 | /* umount was not run. */ |
| 62 | UMOUNT_STAT_SKIPPED = 1, |
| 63 | /* umount failed with timeout. */ |
| 64 | UMOUNT_STAT_TIMEOUT = 2, |
| 65 | /* could not run due to error */ |
| 66 | UMOUNT_STAT_ERROR = 3, |
| 67 | /* not used by init but reserved for other part to use this to represent the |
| 68 | the state where umount status before reboot is not found / available. */ |
| 69 | UMOUNT_STAT_NOT_AVAILABLE = 4, |
| 70 | }; |
| 71 | |
| 72 | // Utility for struct mntent |
| 73 | class MountEntry { |
| 74 | public: |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 75 | explicit MountEntry(const mntent& entry) |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 76 | : mnt_fsname_(entry.mnt_fsname), |
| 77 | mnt_dir_(entry.mnt_dir), |
| 78 | mnt_type_(entry.mnt_type), |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 79 | mnt_opts_(entry.mnt_opts) {} |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 80 | |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 81 | bool Umount() { |
| 82 | int r = umount2(mnt_dir_.c_str(), 0); |
| 83 | if (r == 0) { |
| 84 | LOG(INFO) << "umounted " << mnt_fsname_ << ":" << mnt_dir_ << " opts " << mnt_opts_; |
| 85 | return true; |
| 86 | } else { |
| 87 | PLOG(WARNING) << "cannot umount " << mnt_fsname_ << ":" << mnt_dir_ << " opts " |
| 88 | << mnt_opts_; |
| 89 | return false; |
| 90 | } |
| 91 | } |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 92 | |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 93 | void DoFsck() { |
| 94 | int st; |
| 95 | if (IsF2Fs()) { |
| 96 | const char* f2fs_argv[] = { |
| 97 | "/system/bin/fsck.f2fs", "-f", mnt_fsname_.c_str(), |
| 98 | }; |
| 99 | android_fork_execvp_ext(arraysize(f2fs_argv), (char**)f2fs_argv, &st, true, LOG_KLOG, |
| 100 | true, nullptr, nullptr, 0); |
| 101 | } else if (IsExt4()) { |
| 102 | const char* ext4_argv[] = { |
| 103 | "/system/bin/e2fsck", "-f", "-y", mnt_fsname_.c_str(), |
| 104 | }; |
| 105 | android_fork_execvp_ext(arraysize(ext4_argv), (char**)ext4_argv, &st, true, LOG_KLOG, |
| 106 | true, nullptr, nullptr, 0); |
| 107 | } |
| 108 | } |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 109 | |
| 110 | static bool IsBlockDevice(const struct mntent& mntent) { |
| 111 | return android::base::StartsWith(mntent.mnt_fsname, "/dev/block"); |
| 112 | } |
| 113 | |
| 114 | static bool IsEmulatedDevice(const struct mntent& mntent) { |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 115 | return android::base::StartsWith(mntent.mnt_fsname, "/data/"); |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | private: |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 119 | bool IsF2Fs() const { return mnt_type_ == "f2fs"; } |
| 120 | |
| 121 | bool IsExt4() const { return mnt_type_ == "ext4"; } |
| 122 | |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 123 | std::string mnt_fsname_; |
| 124 | std::string mnt_dir_; |
| 125 | std::string mnt_type_; |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 126 | std::string mnt_opts_; |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 127 | }; |
| 128 | |
| 129 | // Turn off backlight while we are performing power down cleanup activities. |
| 130 | static void TurnOffBacklight() { |
| 131 | static constexpr char OFF[] = "0"; |
| 132 | |
| 133 | android::base::WriteStringToFile(OFF, "/sys/class/leds/lcd-backlight/brightness"); |
| 134 | |
| 135 | static const char backlightDir[] = "/sys/class/backlight"; |
| 136 | std::unique_ptr<DIR, int (*)(DIR*)> dir(opendir(backlightDir), closedir); |
| 137 | if (!dir) { |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | struct dirent* dp; |
| 142 | while ((dp = readdir(dir.get())) != nullptr) { |
| 143 | if (((dp->d_type != DT_DIR) && (dp->d_type != DT_LNK)) || (dp->d_name[0] == '.')) { |
| 144 | continue; |
| 145 | } |
| 146 | |
| 147 | std::string fileName = StringPrintf("%s/%s/brightness", backlightDir, dp->d_name); |
| 148 | android::base::WriteStringToFile(OFF, fileName); |
| 149 | } |
| 150 | } |
| 151 | |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 152 | static void ShutdownVold() { |
| 153 | const char* vdc_argv[] = {"/system/bin/vdc", "volume", "shutdown"}; |
| 154 | int status; |
| 155 | android_fork_execvp_ext(arraysize(vdc_argv), (char**)vdc_argv, &status, true, LOG_KLOG, true, |
| 156 | nullptr, nullptr, 0); |
| 157 | } |
| 158 | |
| 159 | static void LogShutdownTime(UmountStat stat, Timer* t) { |
| 160 | LOG(WARNING) << "powerctl_shutdown_time_ms:" << std::to_string(t->duration_ms()) << ":" << stat; |
| 161 | } |
| 162 | |
| 163 | static void __attribute__((noreturn)) |
| 164 | RebootSystem(unsigned int cmd, const std::string& rebootTarget) { |
Keun-young Park | 3cd8c6f | 2017-03-23 15:33:16 -0700 | [diff] [blame] | 165 | LOG(INFO) << "Reboot ending, jumping to kernel"; |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 166 | switch (cmd) { |
| 167 | case ANDROID_RB_POWEROFF: |
| 168 | reboot(RB_POWER_OFF); |
| 169 | break; |
| 170 | |
| 171 | case ANDROID_RB_RESTART2: |
| 172 | syscall(__NR_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, |
| 173 | LINUX_REBOOT_CMD_RESTART2, rebootTarget.c_str()); |
| 174 | break; |
| 175 | |
| 176 | case ANDROID_RB_THERMOFF: |
| 177 | reboot(RB_POWER_OFF); |
| 178 | break; |
| 179 | } |
| 180 | // In normal case, reboot should not return. |
| 181 | PLOG(FATAL) << "reboot call returned"; |
| 182 | abort(); |
| 183 | } |
| 184 | |
| 185 | /* Find all read+write block devices and emulated devices in /proc/mounts |
| 186 | * and add them to correpsponding list. |
| 187 | */ |
| 188 | static bool FindPartitionsToUmount(std::vector<MountEntry>* blockDevPartitions, |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 189 | std::vector<MountEntry>* emulatedPartitions, bool dump) { |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 190 | std::unique_ptr<std::FILE, int (*)(std::FILE*)> fp(setmntent("/proc/mounts", "r"), endmntent); |
| 191 | if (fp == nullptr) { |
| 192 | PLOG(ERROR) << "Failed to open /proc/mounts"; |
| 193 | return false; |
| 194 | } |
| 195 | mntent* mentry; |
| 196 | while ((mentry = getmntent(fp.get())) != nullptr) { |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 197 | if (dump) { |
| 198 | LOG(INFO) << "mount entry " << mentry->mnt_fsname << ":" << mentry->mnt_dir << " opts " |
| 199 | << mentry->mnt_opts << " type " << mentry->mnt_type; |
| 200 | } else if (MountEntry::IsBlockDevice(*mentry) && hasmntopt(mentry, "rw")) { |
| 201 | blockDevPartitions->emplace(blockDevPartitions->begin(), *mentry); |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 202 | } else if (MountEntry::IsEmulatedDevice(*mentry)) { |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 203 | emulatedPartitions->emplace(emulatedPartitions->begin(), *mentry); |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | return true; |
| 207 | } |
| 208 | |
Keun-young Park | 1663e97 | 2017-04-21 17:29:26 -0700 | [diff] [blame] | 209 | static void DumpUmountDebuggingInfo(bool dump_all) { |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 210 | int status; |
| 211 | if (!security_getenforce()) { |
| 212 | LOG(INFO) << "Run lsof"; |
| 213 | const char* lsof_argv[] = {"/system/bin/lsof"}; |
| 214 | android_fork_execvp_ext(arraysize(lsof_argv), (char**)lsof_argv, &status, true, LOG_KLOG, |
| 215 | true, nullptr, nullptr, 0); |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 216 | } |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 217 | FindPartitionsToUmount(nullptr, nullptr, true); |
Keun-young Park | 1663e97 | 2017-04-21 17:29:26 -0700 | [diff] [blame] | 218 | if (dump_all) { |
| 219 | // dump current tasks, this log can be lengthy, so only dump with dump_all |
| 220 | android::base::WriteStringToFile("t", "/proc/sysrq-trigger"); |
| 221 | } |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | static UmountStat UmountPartitions(int timeoutMs) { |
| 225 | Timer t; |
| 226 | UmountStat stat = UMOUNT_STAT_TIMEOUT; |
| 227 | int retry = 0; |
| 228 | /* data partition needs all pending writes to be completed and all emulated partitions |
| 229 | * umounted.If the current waiting is not good enough, give |
| 230 | * up and leave it to e2fsck after reboot to fix it. |
| 231 | */ |
| 232 | while (true) { |
| 233 | std::vector<MountEntry> block_devices; |
| 234 | std::vector<MountEntry> emulated_devices; |
| 235 | if (!FindPartitionsToUmount(&block_devices, &emulated_devices, false)) { |
| 236 | return UMOUNT_STAT_ERROR; |
| 237 | } |
| 238 | if (block_devices.size() == 0) { |
| 239 | stat = UMOUNT_STAT_SUCCESS; |
| 240 | break; |
| 241 | } |
| 242 | if ((timeoutMs < t.duration_ms()) && retry > 0) { // try umount at least once |
| 243 | stat = UMOUNT_STAT_TIMEOUT; |
| 244 | break; |
| 245 | } |
| 246 | if (emulated_devices.size() > 0 && |
| 247 | std::all_of(emulated_devices.begin(), emulated_devices.end(), |
| 248 | [](auto& entry) { return entry.Umount(); })) { |
| 249 | sync(); |
| 250 | } |
| 251 | for (auto& entry : block_devices) { |
| 252 | entry.Umount(); |
| 253 | } |
| 254 | retry++; |
| 255 | std::this_thread::sleep_for(100ms); |
| 256 | } |
| 257 | return stat; |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Keun-young Park | 3ee0df9 | 2017-03-27 11:21:09 -0700 | [diff] [blame] | 260 | static void KillAllProcesses() { android::base::WriteStringToFile("i", "/proc/sysrq-trigger"); } |
| 261 | |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 262 | /* Try umounting all emulated file systems R/W block device cfile systems. |
| 263 | * This will just try umount and give it up if it fails. |
| 264 | * For fs like ext4, this is ok as file system will be marked as unclean shutdown |
| 265 | * and necessary check can be done at the next reboot. |
| 266 | * For safer shutdown, caller needs to make sure that |
| 267 | * all processes / emulated partition for the target fs are all cleaned-up. |
| 268 | * |
| 269 | * return true when umount was successful. false when timed out. |
| 270 | */ |
Keun-young Park | 3ee0df9 | 2017-03-27 11:21:09 -0700 | [diff] [blame] | 271 | static UmountStat TryUmountAndFsck(bool runFsck, int timeoutMs) { |
| 272 | Timer t; |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 273 | std::vector<MountEntry> block_devices; |
| 274 | std::vector<MountEntry> emulated_devices; |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 275 | |
| 276 | TurnOffBacklight(); // this part can take time. save power. |
| 277 | |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 278 | if (runFsck && !FindPartitionsToUmount(&block_devices, &emulated_devices, false)) { |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 279 | return UMOUNT_STAT_ERROR; |
| 280 | } |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 281 | |
| 282 | UmountStat stat = UmountPartitions(timeoutMs - t.duration_ms()); |
| 283 | if (stat != UMOUNT_STAT_SUCCESS) { |
| 284 | LOG(INFO) << "umount timeout, last resort, kill all and try"; |
Keun-young Park | 1663e97 | 2017-04-21 17:29:26 -0700 | [diff] [blame] | 285 | if (DUMP_ON_UMOUNT_FAILURE) DumpUmountDebuggingInfo(false); |
Keun-young Park | 3ee0df9 | 2017-03-27 11:21:09 -0700 | [diff] [blame] | 286 | KillAllProcesses(); |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 287 | // even if it succeeds, still it is timeout and do not run fsck with all processes killed |
| 288 | UmountPartitions(0); |
Keun-young Park | 1663e97 | 2017-04-21 17:29:26 -0700 | [diff] [blame] | 289 | if (DUMP_ON_UMOUNT_FAILURE) DumpUmountDebuggingInfo(true); |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 292 | if (stat == UMOUNT_STAT_SUCCESS && runFsck) { |
| 293 | // fsck part is excluded from timeout check. It only runs for user initiated shutdown |
| 294 | // and should not affect reboot time. |
| 295 | for (auto& entry : block_devices) { |
| 296 | entry.DoFsck(); |
| 297 | } |
| 298 | } |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 299 | return stat; |
| 300 | } |
| 301 | |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 302 | static void __attribute__((noreturn)) DoThermalOff() { |
| 303 | LOG(WARNING) << "Thermal system shutdown"; |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 304 | sync(); |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 305 | RebootSystem(ANDROID_RB_THERMOFF, ""); |
| 306 | abort(); |
| 307 | } |
| 308 | |
| 309 | void DoReboot(unsigned int cmd, const std::string& reason, const std::string& rebootTarget, |
| 310 | bool runFsck) { |
| 311 | Timer t; |
Keun-young Park | 3cd8c6f | 2017-03-23 15:33:16 -0700 | [diff] [blame] | 312 | LOG(INFO) << "Reboot start, reason: " << reason << ", rebootTarget: " << rebootTarget; |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 313 | |
Todd Poynor | fc827be | 2017-04-13 15:17:24 -0700 | [diff] [blame] | 314 | android::base::WriteStringToFile(StringPrintf("%s\n", reason.c_str()), LAST_REBOOT_REASON_FILE, |
| 315 | S_IRUSR | S_IWUSR, AID_SYSTEM, AID_SYSTEM); |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 316 | |
| 317 | if (cmd == ANDROID_RB_THERMOFF) { // do not wait if it is thermal |
| 318 | DoThermalOff(); |
| 319 | abort(); |
| 320 | } |
Keun-young Park | aa08ea4 | 2017-03-23 13:27:28 -0700 | [diff] [blame] | 321 | |
Keun-young Park | 7feab68 | 2017-04-26 12:36:14 -0700 | [diff] [blame] | 322 | constexpr unsigned int shutdownTimeoutDefault = 6; |
Keun-young Park | c4ffa5c | 2017-03-28 09:41:36 -0700 | [diff] [blame] | 323 | unsigned int shutdownTimeout = shutdownTimeoutDefault; |
| 324 | if (SHUTDOWN_ZERO_TIMEOUT) { // eng build |
| 325 | shutdownTimeout = 0; |
| 326 | } else { |
| 327 | shutdownTimeout = |
| 328 | android::base::GetUintProperty("ro.build.shutdown_timeout", shutdownTimeoutDefault); |
| 329 | } |
Tom Cherry | ccf2353 | 2017-03-28 16:40:41 -0700 | [diff] [blame] | 330 | LOG(INFO) << "Shutdown timeout: " << shutdownTimeout; |
Keun-young Park | aa08ea4 | 2017-03-23 13:27:28 -0700 | [diff] [blame] | 331 | |
Keun-young Park | 7830d59 | 2017-03-27 16:07:02 -0700 | [diff] [blame] | 332 | // keep debugging tools until non critical ones are all gone. |
| 333 | const std::set<std::string> kill_after_apps{"tombstoned", "logd", "adbd"}; |
| 334 | // watchdogd is a vendor specific component but should be alive to complete shutdown safely. |
Keun-young Park | 7264bee | 2017-05-18 13:28:32 -0700 | [diff] [blame] | 335 | const std::set<std::string> to_starts{"watchdogd", "vold", "ueventd"}; |
Keun-young Park | 7830d59 | 2017-03-27 16:07:02 -0700 | [diff] [blame] | 336 | ServiceManager::GetInstance().ForEachService([&kill_after_apps, &to_starts](Service* s) { |
| 337 | if (kill_after_apps.count(s->name())) { |
| 338 | s->SetShutdownCritical(); |
| 339 | } else if (to_starts.count(s->name())) { |
| 340 | s->Start(); |
| 341 | s->SetShutdownCritical(); |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 342 | } |
Keun-young Park | 7830d59 | 2017-03-27 16:07:02 -0700 | [diff] [blame] | 343 | }); |
| 344 | |
| 345 | Service* bootAnim = ServiceManager::GetInstance().FindServiceByName("bootanim"); |
| 346 | Service* surfaceFlinger = ServiceManager::GetInstance().FindServiceByName("surfaceflinger"); |
| 347 | if (bootAnim != nullptr && surfaceFlinger != nullptr && surfaceFlinger->IsRunning()) { |
Keun-young Park | 7830d59 | 2017-03-27 16:07:02 -0700 | [diff] [blame] | 348 | ServiceManager::GetInstance().ForEachServiceInClass("animation", [](Service* s) { |
Keun-young Park | 7830d59 | 2017-03-27 16:07:02 -0700 | [diff] [blame] | 349 | s->SetShutdownCritical(); // will not check animation class separately |
| 350 | }); |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 351 | } |
Keun-young Park | 7830d59 | 2017-03-27 16:07:02 -0700 | [diff] [blame] | 352 | |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 353 | // optional shutdown step |
| 354 | // 1. terminate all services except shutdown critical ones. wait for delay to finish |
Keun-young Park | 3ee0df9 | 2017-03-27 11:21:09 -0700 | [diff] [blame] | 355 | if (shutdownTimeout > 0) { |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 356 | LOG(INFO) << "terminating init services"; |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 357 | |
| 358 | // Ask all services to terminate except shutdown critical ones. |
| 359 | ServiceManager::GetInstance().ForEachService([](Service* s) { |
| 360 | if (!s->IsShutdownCritical()) s->Terminate(); |
| 361 | }); |
| 362 | |
| 363 | int service_count = 0; |
Keun-young Park | 3ee0df9 | 2017-03-27 11:21:09 -0700 | [diff] [blame] | 364 | // Up to half as long as shutdownTimeout or 3 seconds, whichever is lower. |
| 365 | unsigned int terminationWaitTimeout = std::min<unsigned int>((shutdownTimeout + 1) / 2, 3); |
| 366 | while (t.duration_s() < terminationWaitTimeout) { |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 367 | ServiceManager::GetInstance().ReapAnyOutstandingChildren(); |
| 368 | |
| 369 | service_count = 0; |
| 370 | ServiceManager::GetInstance().ForEachService([&service_count](Service* s) { |
| 371 | // Count the number of services running except shutdown critical. |
| 372 | // Exclude the console as it will ignore the SIGTERM signal |
| 373 | // and not exit. |
| 374 | // Note: SVC_CONSOLE actually means "requires console" but |
| 375 | // it is only used by the shell. |
| 376 | if (!s->IsShutdownCritical() && s->pid() != 0 && (s->flags() & SVC_CONSOLE) == 0) { |
| 377 | service_count++; |
| 378 | } |
| 379 | }); |
| 380 | |
| 381 | if (service_count == 0) { |
| 382 | // All terminable services terminated. We can exit early. |
| 383 | break; |
| 384 | } |
| 385 | |
| 386 | // Wait a bit before recounting the number or running services. |
| 387 | std::this_thread::sleep_for(50ms); |
| 388 | } |
| 389 | LOG(INFO) << "Terminating running services took " << t |
| 390 | << " with remaining services:" << service_count; |
| 391 | } |
| 392 | |
| 393 | // minimum safety steps before restarting |
| 394 | // 2. kill all services except ones that are necessary for the shutdown sequence. |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 395 | ServiceManager::GetInstance().ForEachService([](Service* s) { |
| 396 | if (!s->IsShutdownCritical()) s->Stop(); |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 397 | }); |
| 398 | ServiceManager::GetInstance().ReapAnyOutstandingChildren(); |
| 399 | |
| 400 | // 3. send volume shutdown to vold |
| 401 | Service* voldService = ServiceManager::GetInstance().FindServiceByName("vold"); |
| 402 | if (voldService != nullptr && voldService->IsRunning()) { |
| 403 | ShutdownVold(); |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 404 | voldService->Stop(); |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 405 | } else { |
| 406 | LOG(INFO) << "vold not running, skipping vold shutdown"; |
| 407 | } |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 408 | // logcat stopped here |
| 409 | ServiceManager::GetInstance().ForEachService([&kill_after_apps](Service* s) { |
| 410 | if (kill_after_apps.count(s->name())) s->Stop(); |
| 411 | }); |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 412 | // 4. sync, try umount, and optionally run fsck for user shutdown |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 413 | sync(); |
Keun-young Park | 3ee0df9 | 2017-03-27 11:21:09 -0700 | [diff] [blame] | 414 | UmountStat stat = TryUmountAndFsck(runFsck, shutdownTimeout * 1000 - t.duration_ms()); |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 415 | // Follow what linux shutdown is doing: one more sync with little bit delay |
| 416 | sync(); |
| 417 | std::this_thread::sleep_for(100ms); |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 418 | LogShutdownTime(stat, &t); |
| 419 | // Reboot regardless of umount status. If umount fails, fsck after reboot will fix it. |
| 420 | RebootSystem(cmd, rebootTarget); |
| 421 | abort(); |
| 422 | } |
Tom Cherry | 98ad32a | 2017-04-17 16:34:20 -0700 | [diff] [blame] | 423 | |
| 424 | bool HandlePowerctlMessage(const std::string& command) { |
| 425 | unsigned int cmd = 0; |
| 426 | std::vector<std::string> cmd_params = android::base::Split(command, ","); |
Tom Cherry | 98ad32a | 2017-04-17 16:34:20 -0700 | [diff] [blame] | 427 | std::string reboot_target = ""; |
| 428 | bool run_fsck = false; |
| 429 | bool command_invalid = false; |
| 430 | |
| 431 | if (cmd_params.size() > 3) { |
| 432 | command_invalid = true; |
| 433 | } else if (cmd_params[0] == "shutdown") { |
| 434 | cmd = ANDROID_RB_POWEROFF; |
| 435 | if (cmd_params.size() == 2 && cmd_params[1] == "userrequested") { |
| 436 | // The shutdown reason is PowerManager.SHUTDOWN_USER_REQUESTED. |
| 437 | // Run fsck once the file system is remounted in read-only mode. |
| 438 | run_fsck = true; |
Tom Cherry | 98ad32a | 2017-04-17 16:34:20 -0700 | [diff] [blame] | 439 | } |
| 440 | } else if (cmd_params[0] == "reboot") { |
| 441 | cmd = ANDROID_RB_RESTART2; |
| 442 | if (cmd_params.size() >= 2) { |
| 443 | reboot_target = cmd_params[1]; |
| 444 | // When rebooting to the bootloader notify the bootloader writing |
| 445 | // also the BCB. |
| 446 | if (reboot_target == "bootloader") { |
| 447 | std::string err; |
| 448 | if (!write_reboot_bootloader(&err)) { |
| 449 | LOG(ERROR) << "reboot-bootloader: Error writing " |
| 450 | "bootloader_message: " |
| 451 | << err; |
| 452 | } |
| 453 | } |
| 454 | // If there is an additional bootloader parameter, pass it along |
| 455 | if (cmd_params.size() == 3) { |
| 456 | reboot_target += "," + cmd_params[2]; |
| 457 | } |
| 458 | } |
| 459 | } else if (command == "thermal-shutdown") { // no additional parameter allowed |
| 460 | cmd = ANDROID_RB_THERMOFF; |
Wei Wang | 5d4d6f7 | 2017-06-27 22:08:45 -0700 | [diff] [blame^] | 461 | // Do not queue "shutdown" trigger since we want to shutdown immediately |
| 462 | DoReboot(cmd, command, reboot_target, run_fsck); |
| 463 | return true; |
Tom Cherry | 98ad32a | 2017-04-17 16:34:20 -0700 | [diff] [blame] | 464 | } else { |
| 465 | command_invalid = true; |
| 466 | } |
| 467 | if (command_invalid) { |
| 468 | LOG(ERROR) << "powerctl: unrecognized command '" << command << "'"; |
| 469 | return false; |
| 470 | } |
| 471 | |
Wei Wang | 5d4d6f7 | 2017-06-27 22:08:45 -0700 | [diff] [blame^] | 472 | LOG(INFO) << "Clear action queue and start shutdown trigger"; |
| 473 | ActionManager::GetInstance().ClearQueue(); |
| 474 | // Queue shutdown trigger first |
| 475 | ActionManager::GetInstance().QueueEventTrigger("shutdown"); |
| 476 | // Queue built-in shutdown_done |
| 477 | auto shutdown_handler = [cmd, command, reboot_target, |
| 478 | run_fsck](const std::vector<std::string>&) { |
| 479 | DoReboot(cmd, command, reboot_target, run_fsck); |
| 480 | return 0; |
| 481 | }; |
| 482 | ActionManager::GetInstance().QueueBuiltinAction(shutdown_handler, "shutdown_done"); |
| 483 | |
| 484 | // Skip wait for prop if it is in progress |
| 485 | ResetWaitForProp(); |
| 486 | |
| 487 | // Skip wait for exec if it is in progress |
| 488 | if (ServiceManager::GetInstance().IsWaitingForExec()) { |
| 489 | ServiceManager::GetInstance().ClearExecWait(); |
| 490 | } |
| 491 | |
Tom Cherry | 98ad32a | 2017-04-17 16:34:20 -0700 | [diff] [blame] | 492 | return true; |
| 493 | } |