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