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