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> |
Luis Hector Chavez | 519e5f0 | 2017-06-29 09:50:30 -0700 | [diff] [blame] | 23 | #include <sys/capability.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 | |
Tom Cherry | ede0d53 | 2017-07-06 14:20:11 -0700 | [diff] [blame] | 38 | #include <android-base/chrono_utils.h> |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 39 | #include <android-base/file.h> |
Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 40 | #include <android-base/logging.h> |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 41 | #include <android-base/macros.h> |
Tom Cherry | ccf2353 | 2017-03-28 16:40:41 -0700 | [diff] [blame] | 42 | #include <android-base/properties.h> |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 43 | #include <android-base/stringprintf.h> |
| 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 | |
Luis Hector Chavez | 519e5f0 | 2017-06-29 09:50:30 -0700 | [diff] [blame] | 53 | #include "capabilities.h" |
Wei Wang | eeab491 | 2017-06-27 22:08:45 -0700 | [diff] [blame] | 54 | #include "init.h" |
Keun-young Park | 7830d59 | 2017-03-27 16:07:02 -0700 | [diff] [blame] | 55 | #include "property_service.h" |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 56 | #include "service.h" |
Luis Hector Chavez | 9f97f47 | 2017-09-06 13:43:57 -0700 | [diff] [blame] | 57 | #include "sigchld_handler.h" |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 58 | |
Mark Salyzyn | 6290982 | 2017-10-09 09:27:16 -0700 | [diff] [blame^] | 59 | using android::base::Split; |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 60 | using android::base::StringPrintf; |
Tom Cherry | ede0d53 | 2017-07-06 14:20:11 -0700 | [diff] [blame] | 61 | using android::base::Timer; |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 62 | |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 63 | namespace android { |
| 64 | namespace init { |
| 65 | |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 66 | // represents umount status during reboot / shutdown. |
| 67 | enum UmountStat { |
| 68 | /* umount succeeded. */ |
| 69 | UMOUNT_STAT_SUCCESS = 0, |
| 70 | /* umount was not run. */ |
| 71 | UMOUNT_STAT_SKIPPED = 1, |
| 72 | /* umount failed with timeout. */ |
| 73 | UMOUNT_STAT_TIMEOUT = 2, |
| 74 | /* could not run due to error */ |
| 75 | UMOUNT_STAT_ERROR = 3, |
| 76 | /* not used by init but reserved for other part to use this to represent the |
| 77 | the state where umount status before reboot is not found / available. */ |
| 78 | UMOUNT_STAT_NOT_AVAILABLE = 4, |
| 79 | }; |
| 80 | |
| 81 | // Utility for struct mntent |
| 82 | class MountEntry { |
| 83 | public: |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 84 | explicit MountEntry(const mntent& entry) |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 85 | : mnt_fsname_(entry.mnt_fsname), |
| 86 | mnt_dir_(entry.mnt_dir), |
| 87 | mnt_type_(entry.mnt_type), |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 88 | mnt_opts_(entry.mnt_opts) {} |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 89 | |
Jaegeuk Kim | 0f04f72 | 2017-09-25 10:55:39 -0700 | [diff] [blame] | 90 | bool Umount(bool force) { |
| 91 | int r = umount2(mnt_dir_.c_str(), force ? MNT_FORCE : 0); |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 92 | if (r == 0) { |
| 93 | LOG(INFO) << "umounted " << mnt_fsname_ << ":" << mnt_dir_ << " opts " << mnt_opts_; |
| 94 | return true; |
| 95 | } else { |
| 96 | PLOG(WARNING) << "cannot umount " << mnt_fsname_ << ":" << mnt_dir_ << " opts " |
| 97 | << mnt_opts_; |
| 98 | return false; |
| 99 | } |
| 100 | } |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 101 | |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 102 | void DoFsck() { |
| 103 | int st; |
| 104 | if (IsF2Fs()) { |
| 105 | const char* f2fs_argv[] = { |
| 106 | "/system/bin/fsck.f2fs", "-f", mnt_fsname_.c_str(), |
| 107 | }; |
| 108 | android_fork_execvp_ext(arraysize(f2fs_argv), (char**)f2fs_argv, &st, true, LOG_KLOG, |
| 109 | true, nullptr, nullptr, 0); |
| 110 | } else if (IsExt4()) { |
| 111 | const char* ext4_argv[] = { |
| 112 | "/system/bin/e2fsck", "-f", "-y", mnt_fsname_.c_str(), |
| 113 | }; |
| 114 | android_fork_execvp_ext(arraysize(ext4_argv), (char**)ext4_argv, &st, true, LOG_KLOG, |
| 115 | true, nullptr, nullptr, 0); |
| 116 | } |
| 117 | } |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 118 | |
| 119 | static bool IsBlockDevice(const struct mntent& mntent) { |
| 120 | return android::base::StartsWith(mntent.mnt_fsname, "/dev/block"); |
| 121 | } |
| 122 | |
| 123 | static bool IsEmulatedDevice(const struct mntent& mntent) { |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 124 | return android::base::StartsWith(mntent.mnt_fsname, "/data/"); |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | private: |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 128 | bool IsF2Fs() const { return mnt_type_ == "f2fs"; } |
| 129 | |
| 130 | bool IsExt4() const { return mnt_type_ == "ext4"; } |
| 131 | |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 132 | std::string mnt_fsname_; |
| 133 | std::string mnt_dir_; |
| 134 | std::string mnt_type_; |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 135 | std::string mnt_opts_; |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 136 | }; |
| 137 | |
| 138 | // Turn off backlight while we are performing power down cleanup activities. |
| 139 | static void TurnOffBacklight() { |
| 140 | static constexpr char OFF[] = "0"; |
| 141 | |
| 142 | android::base::WriteStringToFile(OFF, "/sys/class/leds/lcd-backlight/brightness"); |
| 143 | |
| 144 | static const char backlightDir[] = "/sys/class/backlight"; |
| 145 | std::unique_ptr<DIR, int (*)(DIR*)> dir(opendir(backlightDir), closedir); |
| 146 | if (!dir) { |
| 147 | return; |
| 148 | } |
| 149 | |
| 150 | struct dirent* dp; |
| 151 | while ((dp = readdir(dir.get())) != nullptr) { |
| 152 | if (((dp->d_type != DT_DIR) && (dp->d_type != DT_LNK)) || (dp->d_name[0] == '.')) { |
| 153 | continue; |
| 154 | } |
| 155 | |
| 156 | std::string fileName = StringPrintf("%s/%s/brightness", backlightDir, dp->d_name); |
| 157 | android::base::WriteStringToFile(OFF, fileName); |
| 158 | } |
| 159 | } |
| 160 | |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 161 | static void ShutdownVold() { |
| 162 | const char* vdc_argv[] = {"/system/bin/vdc", "volume", "shutdown"}; |
| 163 | int status; |
| 164 | android_fork_execvp_ext(arraysize(vdc_argv), (char**)vdc_argv, &status, true, LOG_KLOG, true, |
| 165 | nullptr, nullptr, 0); |
| 166 | } |
| 167 | |
| 168 | static void LogShutdownTime(UmountStat stat, Timer* t) { |
Tom Cherry | ede0d53 | 2017-07-06 14:20:11 -0700 | [diff] [blame] | 169 | LOG(WARNING) << "powerctl_shutdown_time_ms:" << std::to_string(t->duration().count()) << ":" |
| 170 | << stat; |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Luis Hector Chavez | 9f97f47 | 2017-09-06 13:43:57 -0700 | [diff] [blame] | 173 | bool IsRebootCapable() { |
Luis Hector Chavez | 519e5f0 | 2017-06-29 09:50:30 -0700 | [diff] [blame] | 174 | if (!CAP_IS_SUPPORTED(CAP_SYS_BOOT)) { |
| 175 | PLOG(WARNING) << "CAP_SYS_BOOT is not supported"; |
| 176 | return true; |
| 177 | } |
| 178 | |
| 179 | ScopedCaps caps(cap_get_proc()); |
| 180 | if (!caps) { |
| 181 | PLOG(WARNING) << "cap_get_proc() failed"; |
| 182 | return true; |
| 183 | } |
| 184 | |
| 185 | cap_flag_value_t value = CAP_SET; |
| 186 | if (cap_get_flag(caps.get(), CAP_SYS_BOOT, CAP_EFFECTIVE, &value) != 0) { |
| 187 | PLOG(WARNING) << "cap_get_flag(CAP_SYS_BOOT, EFFECTIVE) failed"; |
| 188 | return true; |
| 189 | } |
| 190 | return value == CAP_SET; |
| 191 | } |
| 192 | |
Tom Cherry | d8db7ab | 2017-08-17 17:28:30 -0700 | [diff] [blame] | 193 | void __attribute__((noreturn)) RebootSystem(unsigned int cmd, const std::string& rebootTarget) { |
Keun-young Park | 3cd8c6f | 2017-03-23 15:33:16 -0700 | [diff] [blame] | 194 | LOG(INFO) << "Reboot ending, jumping to kernel"; |
Luis Hector Chavez | 519e5f0 | 2017-06-29 09:50:30 -0700 | [diff] [blame] | 195 | |
| 196 | if (!IsRebootCapable()) { |
| 197 | // On systems where init does not have the capability of rebooting the |
| 198 | // device, just exit cleanly. |
| 199 | exit(0); |
| 200 | } |
| 201 | |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 202 | switch (cmd) { |
| 203 | case ANDROID_RB_POWEROFF: |
| 204 | reboot(RB_POWER_OFF); |
| 205 | break; |
| 206 | |
| 207 | case ANDROID_RB_RESTART2: |
| 208 | syscall(__NR_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, |
| 209 | LINUX_REBOOT_CMD_RESTART2, rebootTarget.c_str()); |
| 210 | break; |
| 211 | |
| 212 | case ANDROID_RB_THERMOFF: |
| 213 | reboot(RB_POWER_OFF); |
| 214 | break; |
| 215 | } |
| 216 | // In normal case, reboot should not return. |
Tom Cherry | d8db7ab | 2017-08-17 17:28:30 -0700 | [diff] [blame] | 217 | PLOG(ERROR) << "reboot call returned"; |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 218 | abort(); |
| 219 | } |
| 220 | |
| 221 | /* Find all read+write block devices and emulated devices in /proc/mounts |
| 222 | * and add them to correpsponding list. |
| 223 | */ |
| 224 | static bool FindPartitionsToUmount(std::vector<MountEntry>* blockDevPartitions, |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 225 | std::vector<MountEntry>* emulatedPartitions, bool dump) { |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 226 | std::unique_ptr<std::FILE, int (*)(std::FILE*)> fp(setmntent("/proc/mounts", "r"), endmntent); |
| 227 | if (fp == nullptr) { |
| 228 | PLOG(ERROR) << "Failed to open /proc/mounts"; |
| 229 | return false; |
| 230 | } |
| 231 | mntent* mentry; |
| 232 | while ((mentry = getmntent(fp.get())) != nullptr) { |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 233 | if (dump) { |
| 234 | LOG(INFO) << "mount entry " << mentry->mnt_fsname << ":" << mentry->mnt_dir << " opts " |
| 235 | << mentry->mnt_opts << " type " << mentry->mnt_type; |
| 236 | } else if (MountEntry::IsBlockDevice(*mentry) && hasmntopt(mentry, "rw")) { |
Keun-young Park | 6e12b38 | 2017-07-17 12:20:33 -0700 | [diff] [blame] | 237 | std::string mount_dir(mentry->mnt_dir); |
| 238 | // These are R/O partitions changed to R/W after adb remount. |
| 239 | // Do not umount them as shutdown critical services may rely on them. |
Wei Wang | a01c27e | 2017-07-25 10:52:08 -0700 | [diff] [blame] | 240 | if (mount_dir != "/" && mount_dir != "/system" && mount_dir != "/vendor" && |
| 241 | mount_dir != "/oem") { |
Keun-young Park | 6e12b38 | 2017-07-17 12:20:33 -0700 | [diff] [blame] | 242 | blockDevPartitions->emplace(blockDevPartitions->begin(), *mentry); |
| 243 | } |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 244 | } else if (MountEntry::IsEmulatedDevice(*mentry)) { |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 245 | emulatedPartitions->emplace(emulatedPartitions->begin(), *mentry); |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | return true; |
| 249 | } |
| 250 | |
Keun-young Park | 1663e97 | 2017-04-21 17:29:26 -0700 | [diff] [blame] | 251 | static void DumpUmountDebuggingInfo(bool dump_all) { |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 252 | int status; |
| 253 | if (!security_getenforce()) { |
| 254 | LOG(INFO) << "Run lsof"; |
| 255 | const char* lsof_argv[] = {"/system/bin/lsof"}; |
| 256 | android_fork_execvp_ext(arraysize(lsof_argv), (char**)lsof_argv, &status, true, LOG_KLOG, |
| 257 | true, nullptr, nullptr, 0); |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 258 | } |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 259 | FindPartitionsToUmount(nullptr, nullptr, true); |
Keun-young Park | 1663e97 | 2017-04-21 17:29:26 -0700 | [diff] [blame] | 260 | if (dump_all) { |
| 261 | // dump current tasks, this log can be lengthy, so only dump with dump_all |
| 262 | android::base::WriteStringToFile("t", "/proc/sysrq-trigger"); |
| 263 | } |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 264 | } |
| 265 | |
Tom Cherry | ede0d53 | 2017-07-06 14:20:11 -0700 | [diff] [blame] | 266 | static UmountStat UmountPartitions(std::chrono::milliseconds timeout) { |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 267 | Timer t; |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 268 | /* data partition needs all pending writes to be completed and all emulated partitions |
| 269 | * umounted.If the current waiting is not good enough, give |
| 270 | * up and leave it to e2fsck after reboot to fix it. |
| 271 | */ |
| 272 | while (true) { |
| 273 | std::vector<MountEntry> block_devices; |
| 274 | std::vector<MountEntry> emulated_devices; |
| 275 | if (!FindPartitionsToUmount(&block_devices, &emulated_devices, false)) { |
| 276 | return UMOUNT_STAT_ERROR; |
| 277 | } |
| 278 | if (block_devices.size() == 0) { |
Wei Wang | 8c00e42 | 2017-08-16 14:01:46 -0700 | [diff] [blame] | 279 | return UMOUNT_STAT_SUCCESS; |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 280 | } |
Wei Wang | 8c00e42 | 2017-08-16 14:01:46 -0700 | [diff] [blame] | 281 | bool unmount_done = true; |
| 282 | if (emulated_devices.size() > 0) { |
| 283 | unmount_done = std::all_of(emulated_devices.begin(), emulated_devices.end(), |
Jaegeuk Kim | 0f04f72 | 2017-09-25 10:55:39 -0700 | [diff] [blame] | 284 | [](auto& entry) { return entry.Umount(false); }); |
Wei Wang | 8c00e42 | 2017-08-16 14:01:46 -0700 | [diff] [blame] | 285 | if (unmount_done) { |
| 286 | sync(); |
| 287 | } |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 288 | } |
Jaegeuk Kim | 0f04f72 | 2017-09-25 10:55:39 -0700 | [diff] [blame] | 289 | unmount_done = |
| 290 | std::all_of(block_devices.begin(), block_devices.end(), |
| 291 | [&timeout](auto& entry) { return entry.Umount(timeout == 0ms); }) && |
| 292 | unmount_done; |
Wei Wang | 8c00e42 | 2017-08-16 14:01:46 -0700 | [diff] [blame] | 293 | if (unmount_done) { |
| 294 | return UMOUNT_STAT_SUCCESS; |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 295 | } |
Wei Wang | 8c00e42 | 2017-08-16 14:01:46 -0700 | [diff] [blame] | 296 | if ((timeout < t.duration())) { // try umount at least once |
| 297 | return UMOUNT_STAT_TIMEOUT; |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 298 | } |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 299 | std::this_thread::sleep_for(100ms); |
| 300 | } |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 301 | } |
| 302 | |
Keun-young Park | 3ee0df9 | 2017-03-27 11:21:09 -0700 | [diff] [blame] | 303 | static void KillAllProcesses() { android::base::WriteStringToFile("i", "/proc/sysrq-trigger"); } |
| 304 | |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 305 | /* Try umounting all emulated file systems R/W block device cfile systems. |
| 306 | * This will just try umount and give it up if it fails. |
| 307 | * For fs like ext4, this is ok as file system will be marked as unclean shutdown |
| 308 | * and necessary check can be done at the next reboot. |
| 309 | * For safer shutdown, caller needs to make sure that |
| 310 | * all processes / emulated partition for the target fs are all cleaned-up. |
| 311 | * |
| 312 | * return true when umount was successful. false when timed out. |
| 313 | */ |
Tom Cherry | ede0d53 | 2017-07-06 14:20:11 -0700 | [diff] [blame] | 314 | static UmountStat TryUmountAndFsck(bool runFsck, std::chrono::milliseconds timeout) { |
Keun-young Park | 3ee0df9 | 2017-03-27 11:21:09 -0700 | [diff] [blame] | 315 | Timer t; |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 316 | std::vector<MountEntry> block_devices; |
| 317 | std::vector<MountEntry> emulated_devices; |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 318 | |
| 319 | TurnOffBacklight(); // this part can take time. save power. |
| 320 | |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 321 | if (runFsck && !FindPartitionsToUmount(&block_devices, &emulated_devices, false)) { |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 322 | return UMOUNT_STAT_ERROR; |
| 323 | } |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 324 | |
Tom Cherry | ede0d53 | 2017-07-06 14:20:11 -0700 | [diff] [blame] | 325 | UmountStat stat = UmountPartitions(timeout - t.duration()); |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 326 | if (stat != UMOUNT_STAT_SUCCESS) { |
| 327 | LOG(INFO) << "umount timeout, last resort, kill all and try"; |
Keun-young Park | c59b822 | 2017-07-18 18:52:25 -0700 | [diff] [blame] | 328 | if (DUMP_ON_UMOUNT_FAILURE) DumpUmountDebuggingInfo(true); |
Keun-young Park | 3ee0df9 | 2017-03-27 11:21:09 -0700 | [diff] [blame] | 329 | KillAllProcesses(); |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 330 | // 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] | 331 | UmountStat st = UmountPartitions(0ms); |
| 332 | if ((st != UMOUNT_STAT_SUCCESS) && DUMP_ON_UMOUNT_FAILURE) DumpUmountDebuggingInfo(false); |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 333 | } |
| 334 | |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 335 | if (stat == UMOUNT_STAT_SUCCESS && runFsck) { |
| 336 | // fsck part is excluded from timeout check. It only runs for user initiated shutdown |
| 337 | // and should not affect reboot time. |
| 338 | for (auto& entry : block_devices) { |
| 339 | entry.DoFsck(); |
| 340 | } |
| 341 | } |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 342 | return stat; |
| 343 | } |
| 344 | |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 345 | void DoReboot(unsigned int cmd, const std::string& reason, const std::string& rebootTarget, |
| 346 | bool runFsck) { |
| 347 | Timer t; |
Keun-young Park | 3cd8c6f | 2017-03-23 15:33:16 -0700 | [diff] [blame] | 348 | LOG(INFO) << "Reboot start, reason: " << reason << ", rebootTarget: " << rebootTarget; |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 349 | |
Mark Salyzyn | 6290982 | 2017-10-09 09:27:16 -0700 | [diff] [blame^] | 350 | // Ensure last reboot reason is reduced to canonical |
| 351 | // alias reported in bootloader or system boot reason. |
| 352 | size_t skip = 0; |
| 353 | std::vector<std::string> reasons = Split(reason, ","); |
| 354 | if (reasons.size() >= 2 && reasons[0] == "reboot" && |
| 355 | (reasons[1] == "recovery" || reasons[1] == "bootloader" || reasons[1] == "cold" || |
| 356 | reasons[1] == "hard" || reasons[1] == "warm")) { |
| 357 | skip = strlen("reboot,"); |
| 358 | } |
| 359 | property_set(LAST_REBOOT_REASON_PROPERTY, reason.c_str() + skip); |
Mark Salyzyn | 73e6b49 | 2017-08-14 15:56:53 -0700 | [diff] [blame] | 360 | sync(); |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 361 | |
Mark Salyzyn | 73e6b49 | 2017-08-14 15:56:53 -0700 | [diff] [blame] | 362 | bool is_thermal_shutdown = cmd == ANDROID_RB_THERMOFF; |
Keun-young Park | aa08ea4 | 2017-03-23 13:27:28 -0700 | [diff] [blame] | 363 | |
Keun-young Park | 3017387 | 2017-07-18 10:58:28 -0700 | [diff] [blame] | 364 | auto shutdown_timeout = 0ms; |
Tom Cherry | ede0d53 | 2017-07-06 14:20:11 -0700 | [diff] [blame] | 365 | if (!SHUTDOWN_ZERO_TIMEOUT) { |
Keun-young Park | 3017387 | 2017-07-18 10:58:28 -0700 | [diff] [blame] | 366 | if (is_thermal_shutdown) { |
| 367 | constexpr unsigned int thermal_shutdown_timeout = 1; |
| 368 | shutdown_timeout = std::chrono::seconds(thermal_shutdown_timeout); |
| 369 | } else { |
| 370 | constexpr unsigned int shutdown_timeout_default = 6; |
| 371 | auto shutdown_timeout_property = android::base::GetUintProperty( |
| 372 | "ro.build.shutdown_timeout", shutdown_timeout_default); |
| 373 | shutdown_timeout = std::chrono::seconds(shutdown_timeout_property); |
| 374 | } |
Keun-young Park | c4ffa5c | 2017-03-28 09:41:36 -0700 | [diff] [blame] | 375 | } |
Keun-young Park | 3017387 | 2017-07-18 10:58:28 -0700 | [diff] [blame] | 376 | LOG(INFO) << "Shutdown timeout: " << shutdown_timeout.count() << " ms"; |
Keun-young Park | aa08ea4 | 2017-03-23 13:27:28 -0700 | [diff] [blame] | 377 | |
Keun-young Park | 7830d59 | 2017-03-27 16:07:02 -0700 | [diff] [blame] | 378 | // keep debugging tools until non critical ones are all gone. |
| 379 | const std::set<std::string> kill_after_apps{"tombstoned", "logd", "adbd"}; |
| 380 | // 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] | 381 | const std::set<std::string> to_starts{"watchdogd"}; |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 382 | for (const auto& s : ServiceList::GetInstance()) { |
Keun-young Park | 7830d59 | 2017-03-27 16:07:02 -0700 | [diff] [blame] | 383 | if (kill_after_apps.count(s->name())) { |
| 384 | s->SetShutdownCritical(); |
| 385 | } else if (to_starts.count(s->name())) { |
Tom Cherry | 702ca9a | 2017-08-25 10:36:52 -0700 | [diff] [blame] | 386 | if (auto result = s->Start(); !result) { |
| 387 | LOG(ERROR) << "Could not start shutdown 'to_start' service '" << s->name() |
| 388 | << "': " << result.error(); |
| 389 | } |
Keun-young Park | 7830d59 | 2017-03-27 16:07:02 -0700 | [diff] [blame] | 390 | s->SetShutdownCritical(); |
Keun-young Park | cccb34f | 2017-07-05 11:38:44 -0700 | [diff] [blame] | 391 | } else if (s->IsShutdownCritical()) { |
Tom Cherry | 702ca9a | 2017-08-25 10:36:52 -0700 | [diff] [blame] | 392 | // Start shutdown critical service if not started. |
| 393 | if (auto result = s->Start(); !result) { |
| 394 | LOG(ERROR) << "Could not start shutdown critical service '" << s->name() |
| 395 | << "': " << result.error(); |
| 396 | } |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 397 | } |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 398 | } |
Keun-young Park | 7830d59 | 2017-03-27 16:07:02 -0700 | [diff] [blame] | 399 | |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 400 | Service* bootAnim = ServiceList::GetInstance().FindService("bootanim"); |
| 401 | Service* surfaceFlinger = ServiceList::GetInstance().FindService("surfaceflinger"); |
Keun-young Park | 7830d59 | 2017-03-27 16:07:02 -0700 | [diff] [blame] | 402 | if (bootAnim != nullptr && surfaceFlinger != nullptr && surfaceFlinger->IsRunning()) { |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 403 | // will not check animation class separately |
| 404 | for (const auto& service : ServiceList::GetInstance()) { |
| 405 | if (service->classnames().count("animation")) service->SetShutdownCritical(); |
| 406 | } |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 407 | } |
Keun-young Park | 7830d59 | 2017-03-27 16:07:02 -0700 | [diff] [blame] | 408 | |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 409 | // optional shutdown step |
| 410 | // 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] | 411 | if (shutdown_timeout > 0ms) { |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 412 | LOG(INFO) << "terminating init services"; |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 413 | |
| 414 | // Ask all services to terminate except shutdown critical ones. |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 415 | for (const auto& s : ServiceList::GetInstance().services_in_shutdown_order()) { |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 416 | if (!s->IsShutdownCritical()) s->Terminate(); |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 417 | } |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 418 | |
| 419 | int service_count = 0; |
Keun-young Park | 3017387 | 2017-07-18 10:58:28 -0700 | [diff] [blame] | 420 | // Only wait up to half of timeout here |
| 421 | auto termination_wait_timeout = shutdown_timeout / 2; |
Tom Cherry | ede0d53 | 2017-07-06 14:20:11 -0700 | [diff] [blame] | 422 | while (t.duration() < termination_wait_timeout) { |
Tom Cherry | eeee831 | 2017-07-28 15:22:23 -0700 | [diff] [blame] | 423 | ReapAnyOutstandingChildren(); |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 424 | |
| 425 | service_count = 0; |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 426 | for (const auto& s : ServiceList::GetInstance()) { |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 427 | // Count the number of services running except shutdown critical. |
| 428 | // Exclude the console as it will ignore the SIGTERM signal |
| 429 | // and not exit. |
| 430 | // Note: SVC_CONSOLE actually means "requires console" but |
| 431 | // it is only used by the shell. |
| 432 | if (!s->IsShutdownCritical() && s->pid() != 0 && (s->flags() & SVC_CONSOLE) == 0) { |
| 433 | service_count++; |
| 434 | } |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 435 | } |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 436 | |
| 437 | if (service_count == 0) { |
| 438 | // All terminable services terminated. We can exit early. |
| 439 | break; |
| 440 | } |
| 441 | |
| 442 | // Wait a bit before recounting the number or running services. |
| 443 | std::this_thread::sleep_for(50ms); |
| 444 | } |
| 445 | LOG(INFO) << "Terminating running services took " << t |
| 446 | << " with remaining services:" << service_count; |
| 447 | } |
| 448 | |
| 449 | // minimum safety steps before restarting |
| 450 | // 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] | 451 | for (const auto& s : ServiceList::GetInstance().services_in_shutdown_order()) { |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 452 | if (!s->IsShutdownCritical()) s->Stop(); |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 453 | } |
Tom Cherry | eeee831 | 2017-07-28 15:22:23 -0700 | [diff] [blame] | 454 | ReapAnyOutstandingChildren(); |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 455 | |
| 456 | // 3. send volume shutdown to vold |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 457 | Service* voldService = ServiceList::GetInstance().FindService("vold"); |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 458 | if (voldService != nullptr && voldService->IsRunning()) { |
| 459 | ShutdownVold(); |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 460 | voldService->Stop(); |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 461 | } else { |
| 462 | LOG(INFO) << "vold not running, skipping vold shutdown"; |
| 463 | } |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 464 | // logcat stopped here |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 465 | for (const auto& s : ServiceList::GetInstance().services_in_shutdown_order()) { |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 466 | if (kill_after_apps.count(s->name())) s->Stop(); |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 467 | } |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 468 | // 4. sync, try umount, and optionally run fsck for user shutdown |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 469 | sync(); |
Tom Cherry | ede0d53 | 2017-07-06 14:20:11 -0700 | [diff] [blame] | 470 | UmountStat stat = TryUmountAndFsck(runFsck, shutdown_timeout - t.duration()); |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 471 | // Follow what linux shutdown is doing: one more sync with little bit delay |
| 472 | sync(); |
Keun-young Park | 3017387 | 2017-07-18 10:58:28 -0700 | [diff] [blame] | 473 | if (!is_thermal_shutdown) std::this_thread::sleep_for(100ms); |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 474 | LogShutdownTime(stat, &t); |
| 475 | // Reboot regardless of umount status. If umount fails, fsck after reboot will fix it. |
| 476 | RebootSystem(cmd, rebootTarget); |
| 477 | abort(); |
| 478 | } |
Tom Cherry | 98ad32a | 2017-04-17 16:34:20 -0700 | [diff] [blame] | 479 | |
| 480 | bool HandlePowerctlMessage(const std::string& command) { |
| 481 | unsigned int cmd = 0; |
Mark Salyzyn | 6290982 | 2017-10-09 09:27:16 -0700 | [diff] [blame^] | 482 | std::vector<std::string> cmd_params = Split(command, ","); |
Tom Cherry | 98ad32a | 2017-04-17 16:34:20 -0700 | [diff] [blame] | 483 | std::string reboot_target = ""; |
| 484 | bool run_fsck = false; |
| 485 | bool command_invalid = false; |
| 486 | |
| 487 | if (cmd_params.size() > 3) { |
| 488 | command_invalid = true; |
| 489 | } else if (cmd_params[0] == "shutdown") { |
| 490 | cmd = ANDROID_RB_POWEROFF; |
Mark Salyzyn | 73e6b49 | 2017-08-14 15:56:53 -0700 | [diff] [blame] | 491 | if (cmd_params.size() == 2) { |
| 492 | if (cmd_params[1] == "userrequested") { |
| 493 | // The shutdown reason is PowerManager.SHUTDOWN_USER_REQUESTED. |
| 494 | // Run fsck once the file system is remounted in read-only mode. |
| 495 | run_fsck = true; |
| 496 | } else if (cmd_params[1] == "thermal") { |
Mark Salyzyn | bfd05b6 | 2017-09-26 11:10:12 -0700 | [diff] [blame] | 497 | // Turn off sources of heat immediately. |
| 498 | TurnOffBacklight(); |
Mark Salyzyn | 73e6b49 | 2017-08-14 15:56:53 -0700 | [diff] [blame] | 499 | // run_fsck is false to avoid delay |
| 500 | cmd = ANDROID_RB_THERMOFF; |
| 501 | } |
Tom Cherry | 98ad32a | 2017-04-17 16:34:20 -0700 | [diff] [blame] | 502 | } |
| 503 | } else if (cmd_params[0] == "reboot") { |
| 504 | cmd = ANDROID_RB_RESTART2; |
| 505 | if (cmd_params.size() >= 2) { |
| 506 | reboot_target = cmd_params[1]; |
| 507 | // When rebooting to the bootloader notify the bootloader writing |
| 508 | // also the BCB. |
| 509 | if (reboot_target == "bootloader") { |
| 510 | std::string err; |
| 511 | if (!write_reboot_bootloader(&err)) { |
| 512 | LOG(ERROR) << "reboot-bootloader: Error writing " |
| 513 | "bootloader_message: " |
| 514 | << err; |
| 515 | } |
| 516 | } |
Mark Salyzyn | 73e6b49 | 2017-08-14 15:56:53 -0700 | [diff] [blame] | 517 | // If there is an additional parameter, pass it along |
| 518 | if ((cmd_params.size() == 3) && cmd_params[2].size()) { |
Tom Cherry | 98ad32a | 2017-04-17 16:34:20 -0700 | [diff] [blame] | 519 | reboot_target += "," + cmd_params[2]; |
| 520 | } |
| 521 | } |
Tom Cherry | 98ad32a | 2017-04-17 16:34:20 -0700 | [diff] [blame] | 522 | } else { |
| 523 | command_invalid = true; |
| 524 | } |
| 525 | if (command_invalid) { |
| 526 | LOG(ERROR) << "powerctl: unrecognized command '" << command << "'"; |
| 527 | return false; |
| 528 | } |
| 529 | |
Wei Wang | eeab491 | 2017-06-27 22:08:45 -0700 | [diff] [blame] | 530 | LOG(INFO) << "Clear action queue and start shutdown trigger"; |
| 531 | ActionManager::GetInstance().ClearQueue(); |
| 532 | // Queue shutdown trigger first |
| 533 | ActionManager::GetInstance().QueueEventTrigger("shutdown"); |
| 534 | // Queue built-in shutdown_done |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 535 | auto shutdown_handler = [cmd, command, reboot_target, run_fsck](const BuiltinArguments&) { |
Wei Wang | eeab491 | 2017-06-27 22:08:45 -0700 | [diff] [blame] | 536 | DoReboot(cmd, command, reboot_target, run_fsck); |
Tom Cherry | 557946e | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 537 | return Success(); |
Wei Wang | eeab491 | 2017-06-27 22:08:45 -0700 | [diff] [blame] | 538 | }; |
| 539 | ActionManager::GetInstance().QueueBuiltinAction(shutdown_handler, "shutdown_done"); |
| 540 | |
| 541 | // Skip wait for prop if it is in progress |
| 542 | ResetWaitForProp(); |
| 543 | |
Tom Cherry | 3b81f2d | 2017-07-28 14:48:41 -0700 | [diff] [blame] | 544 | // Clear EXEC flag if there is one pending |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 545 | for (const auto& s : ServiceList::GetInstance()) { |
| 546 | s->UnSetExec(); |
| 547 | } |
Wei Wang | eeab491 | 2017-06-27 22:08:45 -0700 | [diff] [blame] | 548 | |
Tom Cherry | 98ad32a | 2017-04-17 16:34:20 -0700 | [diff] [blame] | 549 | return true; |
| 550 | } |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 551 | |
| 552 | } // namespace init |
| 553 | } // namespace android |