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