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