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> |
Nikita Ioffe | 3f4b0d6 | 2019-10-09 15:23:02 +0100 | [diff] [blame] | 25 | #include <stdlib.h> |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 26 | #include <sys/cdefs.h> |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 27 | #include <sys/ioctl.h> |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 28 | #include <sys/mount.h> |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 29 | #include <sys/stat.h> |
josephjang | aaddf28 | 2019-04-16 18:46:24 +0800 | [diff] [blame] | 30 | #include <sys/swap.h> |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 31 | #include <sys/syscall.h> |
| 32 | #include <sys/types.h> |
| 33 | #include <sys/wait.h> |
| 34 | |
Nikita Ioffe | 3f4b0d6 | 2019-10-09 15:23:02 +0100 | [diff] [blame] | 35 | #include <chrono> |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 36 | #include <memory> |
Keun-young Park | 7830d59 | 2017-03-27 16:07:02 -0700 | [diff] [blame] | 37 | #include <set> |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 38 | #include <thread> |
| 39 | #include <vector> |
| 40 | |
Nikita Ioffe | 23dbd6d | 2019-11-14 01:21:24 +0000 | [diff] [blame] | 41 | #include <InitProperties.sysprop.h> |
Tom Cherry | ede0d53 | 2017-07-06 14:20:11 -0700 | [diff] [blame] | 42 | #include <android-base/chrono_utils.h> |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 43 | #include <android-base/file.h> |
Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 44 | #include <android-base/logging.h> |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 45 | #include <android-base/macros.h> |
Tom Cherry | ccf2353 | 2017-03-28 16:40:41 -0700 | [diff] [blame] | 46 | #include <android-base/properties.h> |
Nikita Ioffe | 3f4b0d6 | 2019-10-09 15:23:02 +0100 | [diff] [blame] | 47 | #include <android-base/scopeguard.h> |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 48 | #include <android-base/strings.h> |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 49 | #include <android-base/unique_fd.h> |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 50 | #include <bootloader_message/bootloader_message.h> |
| 51 | #include <cutils/android_reboot.h> |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 52 | #include <fs_mgr.h> |
| 53 | #include <logwrap/logwrap.h> |
Todd Poynor | fc827be | 2017-04-13 15:17:24 -0700 | [diff] [blame] | 54 | #include <private/android_filesystem_config.h> |
Tom Cherry | 0c8d6d2 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 55 | #include <selinux/selinux.h> |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 56 | |
Nikita Ioffe | ba6968e | 2019-10-07 16:26:33 +0100 | [diff] [blame] | 57 | #include "action.h" |
Tom Cherry | 7fd3bc2 | 2018-02-13 15:36:14 -0800 | [diff] [blame] | 58 | #include "action_manager.h" |
Nikita Ioffe | ba6968e | 2019-10-07 16:26:33 +0100 | [diff] [blame] | 59 | #include "builtin_arguments.h" |
Wei Wang | eeab491 | 2017-06-27 22:08:45 -0700 | [diff] [blame] | 60 | #include "init.h" |
Nikita Ioffe | ab91ee9 | 2019-11-06 21:40:31 +0000 | [diff] [blame] | 61 | #include "mount_namespace.h" |
Tom Cherry | 44aceed | 2018-08-03 13:36:18 -0700 | [diff] [blame] | 62 | #include "reboot_utils.h" |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 63 | #include "service.h" |
Tom Cherry | 2aeb1ad | 2019-06-26 10:46:20 -0700 | [diff] [blame] | 64 | #include "service_list.h" |
Luis Hector Chavez | 9f97f47 | 2017-09-06 13:43:57 -0700 | [diff] [blame] | 65 | #include "sigchld_handler.h" |
Nikita Ioffe | 3f4b0d6 | 2019-10-09 15:23:02 +0100 | [diff] [blame] | 66 | #include "util.h" |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 67 | |
josephjang | aaddf28 | 2019-04-16 18:46:24 +0800 | [diff] [blame] | 68 | #define PROC_SYSRQ "/proc/sysrq-trigger" |
| 69 | |
Tom Cherry | 2436e6b | 2019-09-19 11:16:19 -0700 | [diff] [blame] | 70 | using namespace std::literals; |
| 71 | |
Nikita Ioffe | c0df187 | 2019-11-13 21:47:06 +0000 | [diff] [blame] | 72 | using android::base::boot_clock; |
Tom Cherry | 4f4cacc | 2019-01-08 10:39:00 -0800 | [diff] [blame] | 73 | using android::base::GetBoolProperty; |
Nikita Ioffe | c0df187 | 2019-11-13 21:47:06 +0000 | [diff] [blame] | 74 | using android::base::SetProperty; |
Mark Salyzyn | 6290982 | 2017-10-09 09:27:16 -0700 | [diff] [blame] | 75 | using android::base::Split; |
Tom Cherry | ede0d53 | 2017-07-06 14:20:11 -0700 | [diff] [blame] | 76 | using android::base::Timer; |
Jaegeuk Kim | 2aedc82 | 2018-11-20 13:27:06 -0800 | [diff] [blame] | 77 | using android::base::unique_fd; |
Nikita Ioffe | c0df187 | 2019-11-13 21:47:06 +0000 | [diff] [blame] | 78 | using android::base::WaitForProperty; |
josephjang | aaddf28 | 2019-04-16 18:46:24 +0800 | [diff] [blame] | 79 | using android::base::WriteStringToFile; |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 80 | |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 81 | namespace android { |
| 82 | namespace init { |
| 83 | |
Nikita Ioffe | ba6968e | 2019-10-07 16:26:33 +0100 | [diff] [blame] | 84 | static bool shutting_down = false; |
| 85 | |
Nikita Ioffe | 3f4b0d6 | 2019-10-09 15:23:02 +0100 | [diff] [blame] | 86 | static const std::set<std::string> kDebuggingServices{"tombstoned", "logd", "adbd", "console"}; |
| 87 | |
| 88 | static std::vector<Service*> GetDebuggingServices(bool only_post_data) { |
| 89 | std::vector<Service*> ret; |
| 90 | ret.reserve(kDebuggingServices.size()); |
| 91 | for (const auto& s : ServiceList::GetInstance()) { |
| 92 | if (kDebuggingServices.count(s->name()) && (!only_post_data || s->is_post_data())) { |
| 93 | ret.push_back(s.get()); |
| 94 | } |
| 95 | } |
| 96 | return ret; |
| 97 | } |
| 98 | |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 99 | // represents umount status during reboot / shutdown. |
| 100 | enum UmountStat { |
| 101 | /* umount succeeded. */ |
| 102 | UMOUNT_STAT_SUCCESS = 0, |
| 103 | /* umount was not run. */ |
| 104 | UMOUNT_STAT_SKIPPED = 1, |
| 105 | /* umount failed with timeout. */ |
| 106 | UMOUNT_STAT_TIMEOUT = 2, |
| 107 | /* could not run due to error */ |
| 108 | UMOUNT_STAT_ERROR = 3, |
| 109 | /* not used by init but reserved for other part to use this to represent the |
| 110 | the state where umount status before reboot is not found / available. */ |
| 111 | UMOUNT_STAT_NOT_AVAILABLE = 4, |
| 112 | }; |
| 113 | |
| 114 | // Utility for struct mntent |
| 115 | class MountEntry { |
| 116 | public: |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 117 | explicit MountEntry(const mntent& entry) |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 118 | : mnt_fsname_(entry.mnt_fsname), |
| 119 | mnt_dir_(entry.mnt_dir), |
| 120 | mnt_type_(entry.mnt_type), |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 121 | mnt_opts_(entry.mnt_opts) {} |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 122 | |
Jaegeuk Kim | 0f04f72 | 2017-09-25 10:55:39 -0700 | [diff] [blame] | 123 | bool Umount(bool force) { |
Tom Cherry | c9fec9d | 2018-02-15 14:26:58 -0800 | [diff] [blame] | 124 | LOG(INFO) << "Unmounting " << mnt_fsname_ << ":" << mnt_dir_ << " opts " << mnt_opts_; |
Jaegeuk Kim | 0f04f72 | 2017-09-25 10:55:39 -0700 | [diff] [blame] | 125 | int r = umount2(mnt_dir_.c_str(), force ? MNT_FORCE : 0); |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 126 | if (r == 0) { |
Tom Cherry | c9fec9d | 2018-02-15 14:26:58 -0800 | [diff] [blame] | 127 | LOG(INFO) << "Umounted " << mnt_fsname_ << ":" << mnt_dir_ << " opts " << mnt_opts_; |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 128 | return true; |
| 129 | } else { |
Tom Cherry | c9fec9d | 2018-02-15 14:26:58 -0800 | [diff] [blame] | 130 | PLOG(WARNING) << "Cannot umount " << mnt_fsname_ << ":" << mnt_dir_ << " opts " |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 131 | << mnt_opts_; |
| 132 | return false; |
| 133 | } |
| 134 | } |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 135 | |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 136 | void DoFsck() { |
| 137 | int st; |
| 138 | if (IsF2Fs()) { |
| 139 | const char* f2fs_argv[] = { |
Randall Huang | df2faa4 | 2019-01-15 15:00:56 +0800 | [diff] [blame] | 140 | "/system/bin/fsck.f2fs", |
| 141 | "-a", |
| 142 | mnt_fsname_.c_str(), |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 143 | }; |
Tom Cherry | 3a803eb | 2019-09-25 16:23:50 -0700 | [diff] [blame] | 144 | logwrap_fork_execvp(arraysize(f2fs_argv), f2fs_argv, &st, false, LOG_KLOG, true, |
| 145 | nullptr); |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 146 | } else if (IsExt4()) { |
| 147 | const char* ext4_argv[] = { |
Randall Huang | df2faa4 | 2019-01-15 15:00:56 +0800 | [diff] [blame] | 148 | "/system/bin/e2fsck", |
| 149 | "-y", |
| 150 | mnt_fsname_.c_str(), |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 151 | }; |
Tom Cherry | 3a803eb | 2019-09-25 16:23:50 -0700 | [diff] [blame] | 152 | logwrap_fork_execvp(arraysize(ext4_argv), ext4_argv, &st, false, LOG_KLOG, true, |
| 153 | nullptr); |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 154 | } |
| 155 | } |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 156 | |
| 157 | static bool IsBlockDevice(const struct mntent& mntent) { |
| 158 | return android::base::StartsWith(mntent.mnt_fsname, "/dev/block"); |
| 159 | } |
| 160 | |
| 161 | static bool IsEmulatedDevice(const struct mntent& mntent) { |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 162 | return android::base::StartsWith(mntent.mnt_fsname, "/data/"); |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | private: |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 166 | bool IsF2Fs() const { return mnt_type_ == "f2fs"; } |
| 167 | |
| 168 | bool IsExt4() const { return mnt_type_ == "ext4"; } |
| 169 | |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 170 | std::string mnt_fsname_; |
| 171 | std::string mnt_dir_; |
| 172 | std::string mnt_type_; |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 173 | std::string mnt_opts_; |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 174 | }; |
| 175 | |
| 176 | // Turn off backlight while we are performing power down cleanup activities. |
| 177 | static void TurnOffBacklight() { |
Steven Moreland | d5eccfd | 2018-01-19 13:01:53 -0800 | [diff] [blame] | 178 | Service* service = ServiceList::GetInstance().FindService("blank_screen"); |
| 179 | if (service == nullptr) { |
| 180 | LOG(WARNING) << "cannot find blank_screen in TurnOffBacklight"; |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 181 | return; |
| 182 | } |
Tom Cherry | d987264 | 2018-10-11 10:38:05 -0700 | [diff] [blame] | 183 | if (auto result = service->Start(); !result) { |
| 184 | LOG(WARNING) << "Could not start blank_screen service: " << result.error(); |
| 185 | } |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Nikita Ioffe | bee7b8c | 2019-12-02 11:51:39 +0000 | [diff] [blame] | 188 | static Result<void> CallVdc(const std::string& system, const std::string& cmd) { |
| 189 | const char* vdc_argv[] = {"/system/bin/vdc", system.c_str(), cmd.c_str()}; |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 190 | int status; |
Nikita Ioffe | 12a3607 | 2019-10-23 20:11:32 +0100 | [diff] [blame] | 191 | if (logwrap_fork_execvp(arraysize(vdc_argv), vdc_argv, &status, false, LOG_KLOG, true, |
| 192 | nullptr) != 0) { |
Nikita Ioffe | bee7b8c | 2019-12-02 11:51:39 +0000 | [diff] [blame] | 193 | return ErrnoError() << "Failed to call '/system/bin/vdc " << system << " " << cmd << "'"; |
Nikita Ioffe | 12a3607 | 2019-10-23 20:11:32 +0100 | [diff] [blame] | 194 | } |
| 195 | if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { |
| 196 | return {}; |
| 197 | } |
Nikita Ioffe | bee7b8c | 2019-12-02 11:51:39 +0000 | [diff] [blame] | 198 | return Error() << "'/system/bin/vdc " << system << " " << cmd << "' failed : " << status; |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | static void LogShutdownTime(UmountStat stat, Timer* t) { |
Tom Cherry | ede0d53 | 2017-07-06 14:20:11 -0700 | [diff] [blame] | 202 | LOG(WARNING) << "powerctl_shutdown_time_ms:" << std::to_string(t->duration().count()) << ":" |
| 203 | << stat; |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Tom Cherry | 2436e6b | 2019-09-19 11:16:19 -0700 | [diff] [blame] | 206 | static bool IsDataMounted() { |
| 207 | std::unique_ptr<std::FILE, int (*)(std::FILE*)> fp(setmntent("/proc/mounts", "re"), endmntent); |
| 208 | if (fp == nullptr) { |
| 209 | PLOG(ERROR) << "Failed to open /proc/mounts"; |
| 210 | return false; |
| 211 | } |
| 212 | mntent* mentry; |
| 213 | while ((mentry = getmntent(fp.get())) != nullptr) { |
| 214 | if (mentry->mnt_dir == "/data"s) { |
| 215 | return true; |
| 216 | } |
| 217 | } |
| 218 | return false; |
| 219 | } |
| 220 | |
| 221 | // Find all read+write block devices and emulated devices in /proc/mounts and add them to |
| 222 | // the correpsponding list. |
Nikita Ioffe | e7ec8c8 | 2019-10-24 23:18:39 +0100 | [diff] [blame] | 223 | static bool FindPartitionsToUmount(std::vector<MountEntry>* block_dev_partitions, |
| 224 | std::vector<MountEntry>* emulated_partitions, bool dump) { |
Tom Cherry | f274e78 | 2018-10-03 13:13:41 -0700 | [diff] [blame] | 225 | 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] | 226 | if (fp == nullptr) { |
| 227 | PLOG(ERROR) << "Failed to open /proc/mounts"; |
| 228 | return false; |
| 229 | } |
| 230 | mntent* mentry; |
| 231 | while ((mentry = getmntent(fp.get())) != nullptr) { |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 232 | if (dump) { |
| 233 | LOG(INFO) << "mount entry " << mentry->mnt_fsname << ":" << mentry->mnt_dir << " opts " |
| 234 | << mentry->mnt_opts << " type " << mentry->mnt_type; |
| 235 | } else if (MountEntry::IsBlockDevice(*mentry) && hasmntopt(mentry, "rw")) { |
Keun-young Park | 6e12b38 | 2017-07-17 12:20:33 -0700 | [diff] [blame] | 236 | std::string mount_dir(mentry->mnt_dir); |
| 237 | // These are R/O partitions changed to R/W after adb remount. |
| 238 | // Do not umount them as shutdown critical services may rely on them. |
Wei Wang | a01c27e | 2017-07-25 10:52:08 -0700 | [diff] [blame] | 239 | if (mount_dir != "/" && mount_dir != "/system" && mount_dir != "/vendor" && |
| 240 | mount_dir != "/oem") { |
Nikita Ioffe | e7ec8c8 | 2019-10-24 23:18:39 +0100 | [diff] [blame] | 241 | block_dev_partitions->emplace(block_dev_partitions->begin(), *mentry); |
Keun-young Park | 6e12b38 | 2017-07-17 12:20:33 -0700 | [diff] [blame] | 242 | } |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 243 | } else if (MountEntry::IsEmulatedDevice(*mentry)) { |
Nikita Ioffe | e7ec8c8 | 2019-10-24 23:18:39 +0100 | [diff] [blame] | 244 | emulated_partitions->emplace(emulated_partitions->begin(), *mentry); |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | return true; |
| 248 | } |
| 249 | |
Jonglin Lee | 28a2c92 | 2019-01-15 16:38:44 -0800 | [diff] [blame] | 250 | static void DumpUmountDebuggingInfo() { |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 251 | int status; |
| 252 | if (!security_getenforce()) { |
| 253 | LOG(INFO) << "Run lsof"; |
| 254 | const char* lsof_argv[] = {"/system/bin/lsof"}; |
Tom Cherry | 3a803eb | 2019-09-25 16:23:50 -0700 | [diff] [blame] | 255 | logwrap_fork_execvp(arraysize(lsof_argv), lsof_argv, &status, false, LOG_KLOG, true, |
| 256 | nullptr); |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 257 | } |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 258 | FindPartitionsToUmount(nullptr, nullptr, true); |
Jonglin Lee | 28a2c92 | 2019-01-15 16:38:44 -0800 | [diff] [blame] | 259 | // dump current CPU stack traces and uninterruptible tasks |
josephjang | aaddf28 | 2019-04-16 18:46:24 +0800 | [diff] [blame] | 260 | WriteStringToFile("l", PROC_SYSRQ); |
| 261 | WriteStringToFile("w", PROC_SYSRQ); |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Tom Cherry | ede0d53 | 2017-07-06 14:20:11 -0700 | [diff] [blame] | 264 | static UmountStat UmountPartitions(std::chrono::milliseconds timeout) { |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 265 | Timer t; |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 266 | /* data partition needs all pending writes to be completed and all emulated partitions |
| 267 | * umounted.If the current waiting is not good enough, give |
| 268 | * up and leave it to e2fsck after reboot to fix it. |
| 269 | */ |
| 270 | while (true) { |
| 271 | std::vector<MountEntry> block_devices; |
| 272 | std::vector<MountEntry> emulated_devices; |
| 273 | if (!FindPartitionsToUmount(&block_devices, &emulated_devices, false)) { |
| 274 | return UMOUNT_STAT_ERROR; |
| 275 | } |
| 276 | if (block_devices.size() == 0) { |
Wei Wang | 8c00e42 | 2017-08-16 14:01:46 -0700 | [diff] [blame] | 277 | return UMOUNT_STAT_SUCCESS; |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 278 | } |
Wei Wang | 8c00e42 | 2017-08-16 14:01:46 -0700 | [diff] [blame] | 279 | bool unmount_done = true; |
| 280 | if (emulated_devices.size() > 0) { |
Wei Wang | 25dc30f | 2017-10-23 15:32:03 -0700 | [diff] [blame] | 281 | for (auto& entry : emulated_devices) { |
| 282 | if (!entry.Umount(false)) unmount_done = false; |
| 283 | } |
Wei Wang | 8c00e42 | 2017-08-16 14:01:46 -0700 | [diff] [blame] | 284 | if (unmount_done) { |
| 285 | sync(); |
| 286 | } |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 287 | } |
Wei Wang | 25dc30f | 2017-10-23 15:32:03 -0700 | [diff] [blame] | 288 | for (auto& entry : block_devices) { |
| 289 | if (!entry.Umount(timeout == 0ms)) unmount_done = false; |
| 290 | } |
Wei Wang | 8c00e42 | 2017-08-16 14:01:46 -0700 | [diff] [blame] | 291 | if (unmount_done) { |
| 292 | return UMOUNT_STAT_SUCCESS; |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 293 | } |
Wei Wang | 8c00e42 | 2017-08-16 14:01:46 -0700 | [diff] [blame] | 294 | if ((timeout < t.duration())) { // try umount at least once |
| 295 | return UMOUNT_STAT_TIMEOUT; |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 296 | } |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 297 | std::this_thread::sleep_for(100ms); |
| 298 | } |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 299 | } |
| 300 | |
josephjang | aaddf28 | 2019-04-16 18:46:24 +0800 | [diff] [blame] | 301 | static void KillAllProcesses() { |
| 302 | WriteStringToFile("i", PROC_SYSRQ); |
| 303 | } |
| 304 | |
| 305 | // Create reboot/shutdwon monitor thread |
Nikita Ioffe | e7ec8c8 | 2019-10-24 23:18:39 +0100 | [diff] [blame] | 306 | void RebootMonitorThread(unsigned int cmd, const std::string& reboot_target, |
| 307 | sem_t* reboot_semaphore, std::chrono::milliseconds shutdown_timeout, |
| 308 | bool* reboot_monitor_run) { |
josephjang | aaddf28 | 2019-04-16 18:46:24 +0800 | [diff] [blame] | 309 | unsigned int remaining_shutdown_time = 0; |
| 310 | |
| 311 | // 30 seconds more than the timeout passed to the thread as there is a final Umount pass |
| 312 | // after the timeout is reached. |
| 313 | constexpr unsigned int shutdown_watchdog_timeout_default = 30; |
| 314 | auto shutdown_watchdog_timeout = android::base::GetUintProperty( |
| 315 | "ro.build.shutdown.watchdog.timeout", shutdown_watchdog_timeout_default); |
| 316 | remaining_shutdown_time = shutdown_watchdog_timeout + shutdown_timeout.count() / 1000; |
| 317 | |
| 318 | while (*reboot_monitor_run == true) { |
| 319 | if (TEMP_FAILURE_RETRY(sem_wait(reboot_semaphore)) == -1) { |
| 320 | LOG(ERROR) << "sem_wait failed and exit RebootMonitorThread()"; |
| 321 | return; |
| 322 | } |
| 323 | |
| 324 | timespec shutdown_timeout_timespec; |
| 325 | if (clock_gettime(CLOCK_MONOTONIC, &shutdown_timeout_timespec) == -1) { |
| 326 | LOG(ERROR) << "clock_gettime() fail! exit RebootMonitorThread()"; |
| 327 | return; |
| 328 | } |
| 329 | |
| 330 | // If there are some remaining shutdown time left from previous round, we use |
| 331 | // remaining time here. |
| 332 | shutdown_timeout_timespec.tv_sec += remaining_shutdown_time; |
| 333 | |
| 334 | LOG(INFO) << "shutdown_timeout_timespec.tv_sec: " << shutdown_timeout_timespec.tv_sec; |
| 335 | |
| 336 | int sem_return = 0; |
| 337 | while ((sem_return = sem_timedwait_monotonic_np(reboot_semaphore, |
| 338 | &shutdown_timeout_timespec)) == -1 && |
| 339 | errno == EINTR) { |
| 340 | } |
| 341 | |
| 342 | if (sem_return == -1) { |
| 343 | LOG(ERROR) << "Reboot thread timed out"; |
| 344 | |
| 345 | if (android::base::GetBoolProperty("ro.debuggable", false) == true) { |
Tom Cherry | 2436e6b | 2019-09-19 11:16:19 -0700 | [diff] [blame] | 346 | if (false) { |
| 347 | // SEPolicy will block debuggerd from running and this is intentional. |
| 348 | // But these lines are left to be enabled during debugging. |
| 349 | LOG(INFO) << "Try to dump init process call trace:"; |
| 350 | const char* vdc_argv[] = {"/system/bin/debuggerd", "-b", "1"}; |
| 351 | int status; |
Tom Cherry | 3a803eb | 2019-09-25 16:23:50 -0700 | [diff] [blame] | 352 | logwrap_fork_execvp(arraysize(vdc_argv), vdc_argv, &status, false, LOG_KLOG, |
| 353 | true, nullptr); |
Tom Cherry | 2436e6b | 2019-09-19 11:16:19 -0700 | [diff] [blame] | 354 | } |
josephjang | aaddf28 | 2019-04-16 18:46:24 +0800 | [diff] [blame] | 355 | LOG(INFO) << "Show stack for all active CPU:"; |
| 356 | WriteStringToFile("l", PROC_SYSRQ); |
| 357 | |
| 358 | LOG(INFO) << "Show tasks that are in disk sleep(uninterruptable sleep), which are " |
| 359 | "like " |
| 360 | "blocked in mutex or hardware register access:"; |
| 361 | WriteStringToFile("w", PROC_SYSRQ); |
| 362 | } |
| 363 | |
| 364 | // In shutdown case,notify kernel to sync and umount fs to read-only before shutdown. |
| 365 | if (cmd == ANDROID_RB_POWEROFF || cmd == ANDROID_RB_THERMOFF) { |
| 366 | WriteStringToFile("s", PROC_SYSRQ); |
| 367 | |
| 368 | WriteStringToFile("u", PROC_SYSRQ); |
| 369 | |
Nikita Ioffe | e7ec8c8 | 2019-10-24 23:18:39 +0100 | [diff] [blame] | 370 | RebootSystem(cmd, reboot_target); |
josephjang | aaddf28 | 2019-04-16 18:46:24 +0800 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | LOG(ERROR) << "Trigger crash at last!"; |
| 374 | WriteStringToFile("c", PROC_SYSRQ); |
| 375 | } else { |
| 376 | timespec current_time_timespec; |
| 377 | |
| 378 | if (clock_gettime(CLOCK_MONOTONIC, ¤t_time_timespec) == -1) { |
| 379 | LOG(ERROR) << "clock_gettime() fail! exit RebootMonitorThread()"; |
| 380 | return; |
| 381 | } |
| 382 | |
| 383 | remaining_shutdown_time = |
| 384 | shutdown_timeout_timespec.tv_sec - current_time_timespec.tv_sec; |
| 385 | |
| 386 | LOG(INFO) << "remaining_shutdown_time: " << remaining_shutdown_time; |
| 387 | } |
| 388 | } |
| 389 | } |
Keun-young Park | 3ee0df9 | 2017-03-27 11:21:09 -0700 | [diff] [blame] | 390 | |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 391 | /* Try umounting all emulated file systems R/W block device cfile systems. |
| 392 | * This will just try umount and give it up if it fails. |
| 393 | * For fs like ext4, this is ok as file system will be marked as unclean shutdown |
| 394 | * and necessary check can be done at the next reboot. |
| 395 | * For safer shutdown, caller needs to make sure that |
| 396 | * all processes / emulated partition for the target fs are all cleaned-up. |
| 397 | * |
| 398 | * return true when umount was successful. false when timed out. |
| 399 | */ |
Nikita Ioffe | e7ec8c8 | 2019-10-24 23:18:39 +0100 | [diff] [blame] | 400 | static UmountStat TryUmountAndFsck(unsigned int cmd, bool run_fsck, |
josephjang | aaddf28 | 2019-04-16 18:46:24 +0800 | [diff] [blame] | 401 | std::chrono::milliseconds timeout, sem_t* reboot_semaphore) { |
Keun-young Park | 3ee0df9 | 2017-03-27 11:21:09 -0700 | [diff] [blame] | 402 | Timer t; |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 403 | std::vector<MountEntry> block_devices; |
| 404 | std::vector<MountEntry> emulated_devices; |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 405 | |
Nikita Ioffe | e7ec8c8 | 2019-10-24 23:18:39 +0100 | [diff] [blame] | 406 | if (run_fsck && !FindPartitionsToUmount(&block_devices, &emulated_devices, false)) { |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 407 | return UMOUNT_STAT_ERROR; |
| 408 | } |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 409 | |
Tom Cherry | ede0d53 | 2017-07-06 14:20:11 -0700 | [diff] [blame] | 410 | UmountStat stat = UmountPartitions(timeout - t.duration()); |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 411 | if (stat != UMOUNT_STAT_SUCCESS) { |
| 412 | LOG(INFO) << "umount timeout, last resort, kill all and try"; |
Jonglin Lee | 28a2c92 | 2019-01-15 16:38:44 -0800 | [diff] [blame] | 413 | if (DUMP_ON_UMOUNT_FAILURE) DumpUmountDebuggingInfo(); |
Keun-young Park | 3ee0df9 | 2017-03-27 11:21:09 -0700 | [diff] [blame] | 414 | KillAllProcesses(); |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 415 | // 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] | 416 | UmountStat st = UmountPartitions(0ms); |
Jonglin Lee | 28a2c92 | 2019-01-15 16:38:44 -0800 | [diff] [blame] | 417 | if ((st != UMOUNT_STAT_SUCCESS) && DUMP_ON_UMOUNT_FAILURE) DumpUmountDebuggingInfo(); |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 418 | } |
| 419 | |
Nikita Ioffe | e7ec8c8 | 2019-10-24 23:18:39 +0100 | [diff] [blame] | 420 | if (stat == UMOUNT_STAT_SUCCESS && run_fsck) { |
josephjang | aaddf28 | 2019-04-16 18:46:24 +0800 | [diff] [blame] | 421 | LOG(INFO) << "Pause reboot monitor thread before fsck"; |
| 422 | sem_post(reboot_semaphore); |
| 423 | |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 424 | // fsck part is excluded from timeout check. It only runs for user initiated shutdown |
| 425 | // and should not affect reboot time. |
| 426 | for (auto& entry : block_devices) { |
| 427 | entry.DoFsck(); |
| 428 | } |
josephjang | aaddf28 | 2019-04-16 18:46:24 +0800 | [diff] [blame] | 429 | |
| 430 | LOG(INFO) << "Resume reboot monitor thread after fsck"; |
| 431 | sem_post(reboot_semaphore); |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 432 | } |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 433 | return stat; |
| 434 | } |
| 435 | |
Jaegeuk Kim | 2aedc82 | 2018-11-20 13:27:06 -0800 | [diff] [blame] | 436 | // zram is able to use backing device on top of a loopback device. |
| 437 | // In order to unmount /data successfully, we have to kill the loopback device first |
| 438 | #define ZRAM_DEVICE "/dev/block/zram0" |
| 439 | #define ZRAM_RESET "/sys/block/zram0/reset" |
| 440 | #define ZRAM_BACK_DEV "/sys/block/zram0/backing_dev" |
Nikita Ioffe | 12a3607 | 2019-10-23 20:11:32 +0100 | [diff] [blame] | 441 | static Result<void> KillZramBackingDevice() { |
Jaegeuk Kim | 2aedc82 | 2018-11-20 13:27:06 -0800 | [diff] [blame] | 442 | std::string backing_dev; |
Nikita Ioffe | 12a3607 | 2019-10-23 20:11:32 +0100 | [diff] [blame] | 443 | if (!android::base::ReadFileToString(ZRAM_BACK_DEV, &backing_dev)) return {}; |
Jaegeuk Kim | 2aedc82 | 2018-11-20 13:27:06 -0800 | [diff] [blame] | 444 | |
Nikita Ioffe | 12a3607 | 2019-10-23 20:11:32 +0100 | [diff] [blame] | 445 | if (!android::base::StartsWith(backing_dev, "/dev/block/loop")) return {}; |
Jaegeuk Kim | 2aedc82 | 2018-11-20 13:27:06 -0800 | [diff] [blame] | 446 | |
| 447 | // cut the last "\n" |
| 448 | backing_dev.erase(backing_dev.length() - 1); |
| 449 | |
| 450 | // shutdown zram handle |
| 451 | Timer swap_timer; |
| 452 | LOG(INFO) << "swapoff() start..."; |
| 453 | if (swapoff(ZRAM_DEVICE) == -1) { |
Nikita Ioffe | 12a3607 | 2019-10-23 20:11:32 +0100 | [diff] [blame] | 454 | return ErrnoError() << "zram_backing_dev: swapoff (" << backing_dev << ")" |
| 455 | << " failed"; |
Jaegeuk Kim | 2aedc82 | 2018-11-20 13:27:06 -0800 | [diff] [blame] | 456 | } |
| 457 | LOG(INFO) << "swapoff() took " << swap_timer;; |
| 458 | |
josephjang | aaddf28 | 2019-04-16 18:46:24 +0800 | [diff] [blame] | 459 | if (!WriteStringToFile("1", ZRAM_RESET)) { |
Nikita Ioffe | 12a3607 | 2019-10-23 20:11:32 +0100 | [diff] [blame] | 460 | return Error() << "zram_backing_dev: reset (" << backing_dev << ")" |
| 461 | << " failed"; |
Jaegeuk Kim | 2aedc82 | 2018-11-20 13:27:06 -0800 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | // clear loopback device |
| 465 | unique_fd loop(TEMP_FAILURE_RETRY(open(backing_dev.c_str(), O_RDWR | O_CLOEXEC))); |
| 466 | if (loop.get() < 0) { |
Nikita Ioffe | 12a3607 | 2019-10-23 20:11:32 +0100 | [diff] [blame] | 467 | return ErrnoError() << "zram_backing_dev: open(" << backing_dev << ")" |
| 468 | << " failed"; |
Jaegeuk Kim | 2aedc82 | 2018-11-20 13:27:06 -0800 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | if (ioctl(loop.get(), LOOP_CLR_FD, 0) < 0) { |
Nikita Ioffe | 12a3607 | 2019-10-23 20:11:32 +0100 | [diff] [blame] | 472 | return ErrnoError() << "zram_backing_dev: loop_clear (" << backing_dev << ")" |
| 473 | << " failed"; |
Jaegeuk Kim | 2aedc82 | 2018-11-20 13:27:06 -0800 | [diff] [blame] | 474 | } |
| 475 | LOG(INFO) << "zram_backing_dev: `" << backing_dev << "` is cleared successfully."; |
Nikita Ioffe | 12a3607 | 2019-10-23 20:11:32 +0100 | [diff] [blame] | 476 | return {}; |
Jaegeuk Kim | 2aedc82 | 2018-11-20 13:27:06 -0800 | [diff] [blame] | 477 | } |
| 478 | |
Nikita Ioffe | 3f4b0d6 | 2019-10-09 15:23:02 +0100 | [diff] [blame] | 479 | // Stops given services, waits for them to be stopped for |timeout| ms. |
| 480 | // If terminate is true, then SIGTERM is sent to services, otherwise SIGKILL is sent. |
| 481 | static void StopServices(const std::vector<Service*>& services, std::chrono::milliseconds timeout, |
| 482 | bool terminate) { |
| 483 | LOG(INFO) << "Stopping " << services.size() << " services by sending " |
| 484 | << (terminate ? "SIGTERM" : "SIGKILL"); |
| 485 | std::vector<pid_t> pids; |
| 486 | pids.reserve(services.size()); |
| 487 | for (const auto& s : services) { |
| 488 | if (s->pid() > 0) { |
| 489 | pids.push_back(s->pid()); |
| 490 | } |
| 491 | if (terminate) { |
| 492 | s->Terminate(); |
| 493 | } else { |
| 494 | s->Stop(); |
| 495 | } |
| 496 | } |
| 497 | if (timeout > 0ms) { |
| 498 | WaitToBeReaped(pids, timeout); |
| 499 | } else { |
| 500 | // Even if we don't to wait for services to stop, we still optimistically reap zombies. |
| 501 | ReapAnyOutstandingChildren(); |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | // Like StopServices, but also logs all the services that failed to stop after the provided timeout. |
| 506 | // Returns number of violators. |
| 507 | static int StopServicesAndLogViolations(const std::vector<Service*>& services, |
| 508 | std::chrono::milliseconds timeout, bool terminate) { |
| 509 | StopServices(services, timeout, terminate); |
| 510 | int still_running = 0; |
| 511 | for (const auto& s : services) { |
| 512 | if (s->IsRunning()) { |
| 513 | LOG(ERROR) << "[service-misbehaving] : service '" << s->name() << "' is still running " |
| 514 | << timeout.count() << "ms after receiving " |
| 515 | << (terminate ? "SIGTERM" : "SIGKILL"); |
| 516 | still_running++; |
| 517 | } |
| 518 | } |
| 519 | return still_running; |
| 520 | } |
| 521 | |
Tom Cherry | 44aceed | 2018-08-03 13:36:18 -0700 | [diff] [blame] | 522 | //* Reboot / shutdown the system. |
| 523 | // cmd ANDROID_RB_* as defined in android_reboot.h |
| 524 | // reason Reason string like "reboot", "shutdown,userrequested" |
Nikita Ioffe | e7ec8c8 | 2019-10-24 23:18:39 +0100 | [diff] [blame] | 525 | // reboot_target Reboot target string like "bootloader". Otherwise, it should be an empty string. |
| 526 | // run_fsck Whether to run fsck after umount is done. |
Tom Cherry | 44aceed | 2018-08-03 13:36:18 -0700 | [diff] [blame] | 527 | // |
Nikita Ioffe | e7ec8c8 | 2019-10-24 23:18:39 +0100 | [diff] [blame] | 528 | static void DoReboot(unsigned int cmd, const std::string& reason, const std::string& reboot_target, |
| 529 | bool run_fsck) { |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 530 | Timer t; |
Nikita Ioffe | e7ec8c8 | 2019-10-24 23:18:39 +0100 | [diff] [blame] | 531 | LOG(INFO) << "Reboot start, reason: " << reason << ", reboot_target: " << reboot_target; |
Tom Cherry | 0a72e6c | 2018-03-19 16:19:01 -0700 | [diff] [blame] | 532 | |
Tom Cherry | 2436e6b | 2019-09-19 11:16:19 -0700 | [diff] [blame] | 533 | // If /data isn't mounted then we can skip the extra reboot steps below, since we don't need to |
| 534 | // worry about unmounting it. |
| 535 | if (!IsDataMounted()) { |
| 536 | sync(); |
Nikita Ioffe | e7ec8c8 | 2019-10-24 23:18:39 +0100 | [diff] [blame] | 537 | RebootSystem(cmd, reboot_target); |
Tom Cherry | 2436e6b | 2019-09-19 11:16:19 -0700 | [diff] [blame] | 538 | abort(); |
| 539 | } |
| 540 | |
Tom Cherry | 0a72e6c | 2018-03-19 16:19:01 -0700 | [diff] [blame] | 541 | // Ensure last reboot reason is reduced to canonical |
| 542 | // alias reported in bootloader or system boot reason. |
| 543 | size_t skip = 0; |
| 544 | std::vector<std::string> reasons = Split(reason, ","); |
| 545 | if (reasons.size() >= 2 && reasons[0] == "reboot" && |
| 546 | (reasons[1] == "recovery" || reasons[1] == "bootloader" || reasons[1] == "cold" || |
| 547 | reasons[1] == "hard" || reasons[1] == "warm")) { |
| 548 | skip = strlen("reboot,"); |
| 549 | } |
Tom Cherry | c88d8f9 | 2019-08-19 15:21:25 -0700 | [diff] [blame] | 550 | SetProperty(LAST_REBOOT_REASON_PROPERTY, reason.c_str() + skip); |
Tom Cherry | 0a72e6c | 2018-03-19 16:19:01 -0700 | [diff] [blame] | 551 | sync(); |
| 552 | |
| 553 | bool is_thermal_shutdown = cmd == ANDROID_RB_THERMOFF; |
| 554 | |
| 555 | auto shutdown_timeout = 0ms; |
| 556 | if (!SHUTDOWN_ZERO_TIMEOUT) { |
Wei Wang | b5de088 | 2018-10-09 12:42:06 -0700 | [diff] [blame] | 557 | constexpr unsigned int shutdown_timeout_default = 6; |
| 558 | constexpr unsigned int max_thermal_shutdown_timeout = 3; |
| 559 | auto shutdown_timeout_final = android::base::GetUintProperty("ro.build.shutdown_timeout", |
| 560 | shutdown_timeout_default); |
| 561 | if (is_thermal_shutdown && shutdown_timeout_final > max_thermal_shutdown_timeout) { |
| 562 | shutdown_timeout_final = max_thermal_shutdown_timeout; |
Tom Cherry | 0a72e6c | 2018-03-19 16:19:01 -0700 | [diff] [blame] | 563 | } |
Wei Wang | b5de088 | 2018-10-09 12:42:06 -0700 | [diff] [blame] | 564 | shutdown_timeout = std::chrono::seconds(shutdown_timeout_final); |
Tom Cherry | 0a72e6c | 2018-03-19 16:19:01 -0700 | [diff] [blame] | 565 | } |
| 566 | LOG(INFO) << "Shutdown timeout: " << shutdown_timeout.count() << " ms"; |
| 567 | |
josephjang | aaddf28 | 2019-04-16 18:46:24 +0800 | [diff] [blame] | 568 | sem_t reboot_semaphore; |
| 569 | if (sem_init(&reboot_semaphore, false, 0) == -1) { |
| 570 | // These should never fail, but if they do, skip the graceful reboot and reboot immediately. |
| 571 | LOG(ERROR) << "sem_init() fail and RebootSystem() return!"; |
Nikita Ioffe | e7ec8c8 | 2019-10-24 23:18:39 +0100 | [diff] [blame] | 572 | RebootSystem(cmd, reboot_target); |
josephjang | aaddf28 | 2019-04-16 18:46:24 +0800 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | // Start a thread to monitor init shutdown process |
| 576 | LOG(INFO) << "Create reboot monitor thread."; |
| 577 | bool reboot_monitor_run = true; |
Nikita Ioffe | e7ec8c8 | 2019-10-24 23:18:39 +0100 | [diff] [blame] | 578 | std::thread reboot_monitor_thread(&RebootMonitorThread, cmd, reboot_target, &reboot_semaphore, |
josephjang | aaddf28 | 2019-04-16 18:46:24 +0800 | [diff] [blame] | 579 | shutdown_timeout, &reboot_monitor_run); |
| 580 | reboot_monitor_thread.detach(); |
| 581 | |
| 582 | // Start reboot monitor thread |
| 583 | sem_post(&reboot_semaphore); |
| 584 | |
Keun-young Park | 7830d59 | 2017-03-27 16:07:02 -0700 | [diff] [blame] | 585 | // 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] | 586 | const std::set<std::string> to_starts{"watchdogd"}; |
Nikita Ioffe | 3f4b0d6 | 2019-10-09 15:23:02 +0100 | [diff] [blame] | 587 | std::vector<Service*> stop_first; |
| 588 | stop_first.reserve(ServiceList::GetInstance().services().size()); |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 589 | for (const auto& s : ServiceList::GetInstance()) { |
Nikita Ioffe | 3f4b0d6 | 2019-10-09 15:23:02 +0100 | [diff] [blame] | 590 | if (kDebuggingServices.count(s->name())) { |
| 591 | // keep debugging tools until non critical ones are all gone. |
Keun-young Park | 7830d59 | 2017-03-27 16:07:02 -0700 | [diff] [blame] | 592 | s->SetShutdownCritical(); |
| 593 | } else if (to_starts.count(s->name())) { |
Tom Cherry | 702ca9a | 2017-08-25 10:36:52 -0700 | [diff] [blame] | 594 | if (auto result = s->Start(); !result) { |
| 595 | LOG(ERROR) << "Could not start shutdown 'to_start' service '" << s->name() |
| 596 | << "': " << result.error(); |
| 597 | } |
Keun-young Park | 7830d59 | 2017-03-27 16:07:02 -0700 | [diff] [blame] | 598 | s->SetShutdownCritical(); |
Keun-young Park | cccb34f | 2017-07-05 11:38:44 -0700 | [diff] [blame] | 599 | } else if (s->IsShutdownCritical()) { |
Tom Cherry | 702ca9a | 2017-08-25 10:36:52 -0700 | [diff] [blame] | 600 | // Start shutdown critical service if not started. |
| 601 | if (auto result = s->Start(); !result) { |
| 602 | LOG(ERROR) << "Could not start shutdown critical service '" << s->name() |
| 603 | << "': " << result.error(); |
| 604 | } |
Nikita Ioffe | 3f4b0d6 | 2019-10-09 15:23:02 +0100 | [diff] [blame] | 605 | } else { |
| 606 | stop_first.push_back(s.get()); |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 607 | } |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 608 | } |
Keun-young Park | 7830d59 | 2017-03-27 16:07:02 -0700 | [diff] [blame] | 609 | |
Steven Moreland | d5eccfd | 2018-01-19 13:01:53 -0800 | [diff] [blame] | 610 | // remaining operations (specifically fsck) may take a substantial duration |
Tom Cherry | 0a72e6c | 2018-03-19 16:19:01 -0700 | [diff] [blame] | 611 | if (cmd == ANDROID_RB_POWEROFF || is_thermal_shutdown) { |
Steven Moreland | d5eccfd | 2018-01-19 13:01:53 -0800 | [diff] [blame] | 612 | TurnOffBacklight(); |
| 613 | } |
| 614 | |
Nikita Ioffe | e7ec8c8 | 2019-10-24 23:18:39 +0100 | [diff] [blame] | 615 | Service* boot_anim = ServiceList::GetInstance().FindService("bootanim"); |
| 616 | Service* surface_flinger = ServiceList::GetInstance().FindService("surfaceflinger"); |
| 617 | if (boot_anim != nullptr && surface_flinger != nullptr && surface_flinger->IsRunning()) { |
Tom Cherry | 4f4cacc | 2019-01-08 10:39:00 -0800 | [diff] [blame] | 618 | bool do_shutdown_animation = GetBoolProperty("ro.init.shutdown_animation", false); |
| 619 | |
| 620 | if (do_shutdown_animation) { |
Tom Cherry | c88d8f9 | 2019-08-19 15:21:25 -0700 | [diff] [blame] | 621 | SetProperty("service.bootanim.exit", "0"); |
Tom Cherry | 4f4cacc | 2019-01-08 10:39:00 -0800 | [diff] [blame] | 622 | // Could be in the middle of animation. Stop and start so that it can pick |
| 623 | // up the right mode. |
Nikita Ioffe | e7ec8c8 | 2019-10-24 23:18:39 +0100 | [diff] [blame] | 624 | boot_anim->Stop(); |
Tom Cherry | 4f4cacc | 2019-01-08 10:39:00 -0800 | [diff] [blame] | 625 | } |
| 626 | |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 627 | for (const auto& service : ServiceList::GetInstance()) { |
Tom Cherry | 4f4cacc | 2019-01-08 10:39:00 -0800 | [diff] [blame] | 628 | if (service->classnames().count("animation") == 0) { |
| 629 | continue; |
| 630 | } |
| 631 | |
| 632 | // start all animation classes if stopped. |
| 633 | if (do_shutdown_animation) { |
Tom Cherry | 9949ec5 | 2019-05-16 16:54:49 -0700 | [diff] [blame] | 634 | service->Start(); |
Tom Cherry | 4f4cacc | 2019-01-08 10:39:00 -0800 | [diff] [blame] | 635 | } |
| 636 | service->SetShutdownCritical(); // will not check animation class separately |
| 637 | } |
| 638 | |
| 639 | if (do_shutdown_animation) { |
Nikita Ioffe | e7ec8c8 | 2019-10-24 23:18:39 +0100 | [diff] [blame] | 640 | boot_anim->Start(); |
| 641 | surface_flinger->SetShutdownCritical(); |
| 642 | boot_anim->SetShutdownCritical(); |
Tom Cherry | 911b9b1 | 2017-07-27 16:20:58 -0700 | [diff] [blame] | 643 | } |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 644 | } |
Keun-young Park | 7830d59 | 2017-03-27 16:07:02 -0700 | [diff] [blame] | 645 | |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 646 | // optional shutdown step |
| 647 | // 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] | 648 | if (shutdown_timeout > 0ms) { |
Nikita Ioffe | 3f4b0d6 | 2019-10-09 15:23:02 +0100 | [diff] [blame] | 649 | StopServicesAndLogViolations(stop_first, shutdown_timeout / 2, true /* SIGTERM */); |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 650 | } |
Nikita Ioffe | 3f4b0d6 | 2019-10-09 15:23:02 +0100 | [diff] [blame] | 651 | // Send SIGKILL to ones that didn't terminate cleanly. |
| 652 | StopServicesAndLogViolations(stop_first, 0ms, false /* SIGKILL */); |
Luis Hector Chavez | 92c49bc | 2018-07-27 11:19:25 -0700 | [diff] [blame] | 653 | SubcontextTerminate(); |
Nikita Ioffe | 3f4b0d6 | 2019-10-09 15:23:02 +0100 | [diff] [blame] | 654 | // Reap subcontext pids. |
Tom Cherry | eeee831 | 2017-07-28 15:22:23 -0700 | [diff] [blame] | 655 | ReapAnyOutstandingChildren(); |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 656 | |
| 657 | // 3. send volume shutdown to vold |
Nikita Ioffe | e7ec8c8 | 2019-10-24 23:18:39 +0100 | [diff] [blame] | 658 | Service* vold_service = ServiceList::GetInstance().FindService("vold"); |
| 659 | if (vold_service != nullptr && vold_service->IsRunning()) { |
Nikita Ioffe | bee7b8c | 2019-12-02 11:51:39 +0000 | [diff] [blame] | 660 | CallVdc("volume", "shutdown"); |
Nikita Ioffe | e7ec8c8 | 2019-10-24 23:18:39 +0100 | [diff] [blame] | 661 | vold_service->Stop(); |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 662 | } else { |
| 663 | LOG(INFO) << "vold not running, skipping vold shutdown"; |
| 664 | } |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 665 | // logcat stopped here |
Nikita Ioffe | 3f4b0d6 | 2019-10-09 15:23:02 +0100 | [diff] [blame] | 666 | StopServices(GetDebuggingServices(false /* only_post_data */), 0ms, false /* SIGKILL */); |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 667 | // 4. sync, try umount, and optionally run fsck for user shutdown |
Tom Cherry | 1f9d540 | 2018-03-15 10:22:40 -0700 | [diff] [blame] | 668 | { |
| 669 | Timer sync_timer; |
| 670 | LOG(INFO) << "sync() before umount..."; |
| 671 | sync(); |
| 672 | LOG(INFO) << "sync() before umount took" << sync_timer; |
| 673 | } |
Jaegeuk Kim | 2aedc82 | 2018-11-20 13:27:06 -0800 | [diff] [blame] | 674 | // 5. drop caches and disable zram backing device, if exist |
| 675 | KillZramBackingDevice(); |
| 676 | |
Nikita Ioffe | e7ec8c8 | 2019-10-24 23:18:39 +0100 | [diff] [blame] | 677 | UmountStat stat = |
| 678 | TryUmountAndFsck(cmd, run_fsck, shutdown_timeout - t.duration(), &reboot_semaphore); |
Keun-young Park | 2ba5c81 | 2017-03-29 12:54:40 -0700 | [diff] [blame] | 679 | // 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] | 680 | { |
| 681 | Timer sync_timer; |
| 682 | LOG(INFO) << "sync() after umount..."; |
| 683 | sync(); |
| 684 | LOG(INFO) << "sync() after umount took" << sync_timer; |
| 685 | } |
Tom Cherry | 0a72e6c | 2018-03-19 16:19:01 -0700 | [diff] [blame] | 686 | if (!is_thermal_shutdown) std::this_thread::sleep_for(100ms); |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 687 | LogShutdownTime(stat, &t); |
josephjang | aaddf28 | 2019-04-16 18:46:24 +0800 | [diff] [blame] | 688 | |
| 689 | // Send signal to terminate reboot monitor thread. |
| 690 | reboot_monitor_run = false; |
| 691 | sem_post(&reboot_semaphore); |
| 692 | |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 693 | // Reboot regardless of umount status. If umount fails, fsck after reboot will fix it. |
Nikita Ioffe | e7ec8c8 | 2019-10-24 23:18:39 +0100 | [diff] [blame] | 694 | RebootSystem(cmd, reboot_target); |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 695 | abort(); |
| 696 | } |
Tom Cherry | 98ad32a | 2017-04-17 16:34:20 -0700 | [diff] [blame] | 697 | |
Nikita Ioffe | ba6968e | 2019-10-07 16:26:33 +0100 | [diff] [blame] | 698 | static void EnterShutdown() { |
Nikita Ioffe | 3f4b0d6 | 2019-10-09 15:23:02 +0100 | [diff] [blame] | 699 | LOG(INFO) << "Entering shutdown mode"; |
Nikita Ioffe | ba6968e | 2019-10-07 16:26:33 +0100 | [diff] [blame] | 700 | shutting_down = true; |
| 701 | // Skip wait for prop if it is in progress |
| 702 | ResetWaitForProp(); |
| 703 | // Clear EXEC flag if there is one pending |
| 704 | for (const auto& s : ServiceList::GetInstance()) { |
| 705 | s->UnSetExec(); |
| 706 | } |
| 707 | // We no longer process messages about properties changing coming from property service, so we |
| 708 | // need to tell property service to stop sending us these messages, otherwise it'll fill the |
| 709 | // buffers and block indefinitely, causing future property sets, including those that init makes |
| 710 | // during shutdown in Service::NotifyStateChange() to also block indefinitely. |
| 711 | SendStopSendingMessagesMessage(); |
| 712 | } |
| 713 | |
| 714 | static void LeaveShutdown() { |
Nikita Ioffe | 3f4b0d6 | 2019-10-09 15:23:02 +0100 | [diff] [blame] | 715 | LOG(INFO) << "Leaving shutdown mode"; |
Nikita Ioffe | ba6968e | 2019-10-07 16:26:33 +0100 | [diff] [blame] | 716 | shutting_down = false; |
| 717 | SendStartSendingMessagesMessage(); |
| 718 | } |
| 719 | |
Nikita Ioffe | ab91ee9 | 2019-11-06 21:40:31 +0000 | [diff] [blame] | 720 | static Result<void> UnmountAllApexes() { |
| 721 | const char* args[] = {"/system/bin/apexd", "--unmount-all"}; |
| 722 | int status; |
| 723 | if (logwrap_fork_execvp(arraysize(args), args, &status, false, LOG_KLOG, true, nullptr) != 0) { |
| 724 | return ErrnoError() << "Failed to call '/system/bin/apexd --unmount-all'"; |
| 725 | } |
| 726 | if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { |
| 727 | return {}; |
| 728 | } |
| 729 | return Error() << "'/system/bin/apexd --unmount-all' failed : " << status; |
| 730 | } |
| 731 | |
Nikita Ioffe | 3f4b0d6 | 2019-10-09 15:23:02 +0100 | [diff] [blame] | 732 | static Result<void> DoUserspaceReboot() { |
| 733 | LOG(INFO) << "Userspace reboot initiated"; |
Nikita Ioffe | c0df187 | 2019-11-13 21:47:06 +0000 | [diff] [blame] | 734 | boot_clock::time_point now = boot_clock::now(); |
Tom Cherry | c88d8f9 | 2019-08-19 15:21:25 -0700 | [diff] [blame] | 735 | SetProperty("sys.init.userspace_reboot.last_started", |
| 736 | std::to_string(now.time_since_epoch().count())); |
Nikita Ioffe | 3f4b0d6 | 2019-10-09 15:23:02 +0100 | [diff] [blame] | 737 | auto guard = android::base::make_scope_guard([] { |
| 738 | // Leave shutdown so that we can handle a full reboot. |
| 739 | LeaveShutdown(); |
Tom Cherry | 18278d2 | 2019-11-12 16:21:20 -0800 | [diff] [blame] | 740 | trigger_shutdown("reboot,abort-userspace-reboot"); |
Nikita Ioffe | 3f4b0d6 | 2019-10-09 15:23:02 +0100 | [diff] [blame] | 741 | }); |
Nikita Ioffe | c0df187 | 2019-11-13 21:47:06 +0000 | [diff] [blame] | 742 | // Triggering userspace-reboot-requested will result in a bunch of setprop |
Nikita Ioffe | ba6968e | 2019-10-07 16:26:33 +0100 | [diff] [blame] | 743 | // actions. We should make sure, that all of them are propagated before |
Nikita Ioffe | c0df187 | 2019-11-13 21:47:06 +0000 | [diff] [blame] | 744 | // proceeding with userspace reboot. Synchronously setting kUserspaceRebootInProgress property |
| 745 | // is not perfect, but it should do the trick. |
Nikita Ioffe | 23dbd6d | 2019-11-14 01:21:24 +0000 | [diff] [blame] | 746 | if (!android::sysprop::InitProperties::userspace_reboot_in_progress(true)) { |
| 747 | return Error() << "Failed to set sys.init.userspace_reboot.in_progress property"; |
Nikita Ioffe | c0df187 | 2019-11-13 21:47:06 +0000 | [diff] [blame] | 748 | } |
Nikita Ioffe | ba6968e | 2019-10-07 16:26:33 +0100 | [diff] [blame] | 749 | EnterShutdown(); |
Nikita Ioffe | 3f4b0d6 | 2019-10-09 15:23:02 +0100 | [diff] [blame] | 750 | std::vector<Service*> stop_first; |
| 751 | // Remember the services that were enabled. We will need to manually enable them again otherwise |
| 752 | // triggers like class_start won't restart them. |
| 753 | std::vector<Service*> were_enabled; |
| 754 | stop_first.reserve(ServiceList::GetInstance().services().size()); |
| 755 | for (const auto& s : ServiceList::GetInstance().services_in_shutdown_order()) { |
| 756 | if (s->is_post_data() && !kDebuggingServices.count(s->name())) { |
| 757 | stop_first.push_back(s); |
| 758 | } |
| 759 | if (s->is_post_data() && s->IsEnabled()) { |
| 760 | were_enabled.push_back(s); |
| 761 | } |
| 762 | } |
Nikita Ioffe | 82a431e | 2019-11-07 15:37:38 +0000 | [diff] [blame] | 763 | { |
| 764 | Timer sync_timer; |
| 765 | LOG(INFO) << "sync() before terminating services..."; |
| 766 | sync(); |
| 767 | LOG(INFO) << "sync() took " << sync_timer; |
| 768 | } |
Nikita Ioffe | 3f4b0d6 | 2019-10-09 15:23:02 +0100 | [diff] [blame] | 769 | // TODO(b/135984674): do we need shutdown animation for userspace reboot? |
| 770 | // TODO(b/135984674): control userspace timeout via read-only property? |
| 771 | StopServicesAndLogViolations(stop_first, 10s, true /* SIGTERM */); |
| 772 | if (int r = StopServicesAndLogViolations(stop_first, 20s, false /* SIGKILL */); r > 0) { |
| 773 | // TODO(b/135984674): store information about offending services for debugging. |
| 774 | return Error() << r << " post-data services are still running"; |
| 775 | } |
Nikita Ioffe | bee7b8c | 2019-12-02 11:51:39 +0000 | [diff] [blame] | 776 | if (auto result = KillZramBackingDevice(); !result) { |
| 777 | return result; |
| 778 | } |
| 779 | if (auto result = CallVdc("volume", "reset"); !result) { |
| 780 | return result; |
| 781 | } |
Nikita Ioffe | 3f4b0d6 | 2019-10-09 15:23:02 +0100 | [diff] [blame] | 782 | if (int r = StopServicesAndLogViolations(GetDebuggingServices(true /* only_post_data */), 5s, |
| 783 | false /* SIGKILL */); |
| 784 | r > 0) { |
| 785 | // TODO(b/135984674): store information about offending services for debugging. |
| 786 | return Error() << r << " debugging services are still running"; |
| 787 | } |
Nikita Ioffe | 82a431e | 2019-11-07 15:37:38 +0000 | [diff] [blame] | 788 | { |
| 789 | Timer sync_timer; |
| 790 | LOG(INFO) << "sync() after stopping services..."; |
| 791 | sync(); |
| 792 | LOG(INFO) << "sync() took " << sync_timer; |
| 793 | } |
Nikita Ioffe | ab91ee9 | 2019-11-06 21:40:31 +0000 | [diff] [blame] | 794 | if (auto result = UnmountAllApexes(); !result) { |
| 795 | return result; |
| 796 | } |
| 797 | if (!SwitchToBootstrapMountNamespaceIfNeeded()) { |
| 798 | return Error() << "Failed to switch to bootstrap namespace"; |
| 799 | } |
Nikita Ioffe | 091c4d1 | 2019-12-05 12:35:19 +0000 | [diff] [blame] | 800 | // Remove services that were defined in an APEX. |
| 801 | ServiceList::GetInstance().RemoveServiceIf([](const std::unique_ptr<Service>& s) -> bool { |
| 802 | if (s->is_from_apex()) { |
| 803 | LOG(INFO) << "Removing service '" << s->name() << "' because it's defined in an APEX"; |
| 804 | return true; |
| 805 | } |
| 806 | return false; |
| 807 | }); |
Nikita Ioffe | 3f4b0d6 | 2019-10-09 15:23:02 +0100 | [diff] [blame] | 808 | // Re-enable services |
| 809 | for (const auto& s : were_enabled) { |
| 810 | LOG(INFO) << "Re-enabling service '" << s->name() << "'"; |
| 811 | s->Enable(); |
| 812 | } |
| 813 | LeaveShutdown(); |
Nikita Ioffe | ba6968e | 2019-10-07 16:26:33 +0100 | [diff] [blame] | 814 | ActionManager::GetInstance().QueueEventTrigger("userspace-reboot-resume"); |
Nikita Ioffe | 3f4b0d6 | 2019-10-09 15:23:02 +0100 | [diff] [blame] | 815 | guard.Disable(); // Go on with userspace reboot. |
| 816 | return {}; |
Nikita Ioffe | ba6968e | 2019-10-07 16:26:33 +0100 | [diff] [blame] | 817 | } |
| 818 | |
Nikita Ioffe | 82a431e | 2019-11-07 15:37:38 +0000 | [diff] [blame] | 819 | static void UserspaceRebootWatchdogThread() { |
Nikita Ioffe | 57d7bb6 | 2019-11-28 18:25:24 +0000 | [diff] [blame] | 820 | if (!WaitForProperty("sys.init.userspace_reboot.in_progress", "1", 20s)) { |
Nikita Ioffe | 82a431e | 2019-11-07 15:37:38 +0000 | [diff] [blame] | 821 | // TODO(b/135984674): should we reboot instead? |
| 822 | LOG(WARNING) << "Userspace reboot didn't start in 20 seconds. Stopping watchdog"; |
| 823 | return; |
| 824 | } |
| 825 | LOG(INFO) << "Starting userspace reboot watchdog"; |
| 826 | // TODO(b/135984674): this should be configured via a read-only sysprop. |
| 827 | std::chrono::milliseconds timeout = 60s; |
| 828 | if (!WaitForProperty("sys.boot_completed", "1", timeout)) { |
| 829 | LOG(ERROR) << "Failed to boot in " << timeout.count() << "ms. Switching to full reboot"; |
| 830 | // In this case device is in a boot loop. Only way to recover is to do dirty reboot. |
| 831 | RebootSystem(ANDROID_RB_RESTART2, "userspace-reboot-watchdog-triggered"); |
| 832 | } |
| 833 | LOG(INFO) << "Device booted, stopping userspace reboot watchdog"; |
| 834 | } |
| 835 | |
Nikita Ioffe | ba6968e | 2019-10-07 16:26:33 +0100 | [diff] [blame] | 836 | static void HandleUserspaceReboot() { |
Nikita Ioffe | 018ddd7 | 2019-12-20 16:34:48 +0000 | [diff] [blame] | 837 | if (!android::sysprop::InitProperties::is_userspace_reboot_supported().value_or(false)) { |
Nikita Ioffe | 9f473c0 | 2019-12-18 13:31:42 +0000 | [diff] [blame] | 838 | LOG(ERROR) << "Attempted a userspace reboot on a device that doesn't support it"; |
| 839 | return; |
| 840 | } |
Nikita Ioffe | 82a431e | 2019-11-07 15:37:38 +0000 | [diff] [blame] | 841 | // Spinnig up a separate thread will fail the setns call later in the boot sequence. |
| 842 | // Fork a new process to monitor userspace reboot while we are investigating a better solution. |
| 843 | pid_t pid = fork(); |
| 844 | if (pid < 0) { |
| 845 | PLOG(ERROR) << "Failed to fork process for userspace reboot watchdog. Switching to full " |
| 846 | << "reboot"; |
| 847 | trigger_shutdown("reboot,userspace-reboot-failed-to-fork"); |
| 848 | return; |
| 849 | } |
| 850 | if (pid == 0) { |
| 851 | // Child |
| 852 | UserspaceRebootWatchdogThread(); |
| 853 | _exit(EXIT_SUCCESS); |
| 854 | } |
Nikita Ioffe | ba6968e | 2019-10-07 16:26:33 +0100 | [diff] [blame] | 855 | LOG(INFO) << "Clearing queue and starting userspace-reboot-requested trigger"; |
| 856 | auto& am = ActionManager::GetInstance(); |
| 857 | am.ClearQueue(); |
| 858 | am.QueueEventTrigger("userspace-reboot-requested"); |
Nikita Ioffe | 3f4b0d6 | 2019-10-09 15:23:02 +0100 | [diff] [blame] | 859 | auto handler = [](const BuiltinArguments&) { return DoUserspaceReboot(); }; |
Nikita Ioffe | ba6968e | 2019-10-07 16:26:33 +0100 | [diff] [blame] | 860 | am.QueueBuiltinAction(handler, "userspace-reboot"); |
| 861 | } |
| 862 | |
| 863 | void HandlePowerctlMessage(const std::string& command) { |
Tom Cherry | 98ad32a | 2017-04-17 16:34:20 -0700 | [diff] [blame] | 864 | unsigned int cmd = 0; |
Mark Salyzyn | 6290982 | 2017-10-09 09:27:16 -0700 | [diff] [blame] | 865 | std::vector<std::string> cmd_params = Split(command, ","); |
Tom Cherry | 98ad32a | 2017-04-17 16:34:20 -0700 | [diff] [blame] | 866 | std::string reboot_target = ""; |
| 867 | bool run_fsck = false; |
| 868 | bool command_invalid = false; |
Nikita Ioffe | ba6968e | 2019-10-07 16:26:33 +0100 | [diff] [blame] | 869 | bool userspace_reboot = false; |
Tom Cherry | 98ad32a | 2017-04-17 16:34:20 -0700 | [diff] [blame] | 870 | |
Mark Salyzyn | d7931f1 | 2019-07-10 10:33:09 -0700 | [diff] [blame] | 871 | if (cmd_params[0] == "shutdown") { |
Tom Cherry | 98ad32a | 2017-04-17 16:34:20 -0700 | [diff] [blame] | 872 | cmd = ANDROID_RB_POWEROFF; |
Mark Salyzyn | d7931f1 | 2019-07-10 10:33:09 -0700 | [diff] [blame] | 873 | if (cmd_params.size() >= 2) { |
Mark Salyzyn | 73e6b49 | 2017-08-14 15:56:53 -0700 | [diff] [blame] | 874 | if (cmd_params[1] == "userrequested") { |
| 875 | // The shutdown reason is PowerManager.SHUTDOWN_USER_REQUESTED. |
| 876 | // Run fsck once the file system is remounted in read-only mode. |
| 877 | run_fsck = true; |
| 878 | } else if (cmd_params[1] == "thermal") { |
Mark Salyzyn | bfd05b6 | 2017-09-26 11:10:12 -0700 | [diff] [blame] | 879 | // Turn off sources of heat immediately. |
| 880 | TurnOffBacklight(); |
Mark Salyzyn | 73e6b49 | 2017-08-14 15:56:53 -0700 | [diff] [blame] | 881 | // run_fsck is false to avoid delay |
| 882 | cmd = ANDROID_RB_THERMOFF; |
| 883 | } |
Tom Cherry | 98ad32a | 2017-04-17 16:34:20 -0700 | [diff] [blame] | 884 | } |
| 885 | } else if (cmd_params[0] == "reboot") { |
| 886 | cmd = ANDROID_RB_RESTART2; |
| 887 | if (cmd_params.size() >= 2) { |
| 888 | reboot_target = cmd_params[1]; |
Nikita Ioffe | ba6968e | 2019-10-07 16:26:33 +0100 | [diff] [blame] | 889 | if (reboot_target == "userspace") { |
| 890 | LOG(INFO) << "Userspace reboot requested"; |
| 891 | userspace_reboot = true; |
| 892 | } |
Hridya Valsaraju | 5425826 | 2018-09-19 21:14:18 -0700 | [diff] [blame] | 893 | // adb reboot fastboot should boot into bootloader for devices not |
| 894 | // supporting logical partitions. |
| 895 | if (reboot_target == "fastboot" && |
Yifan Hong | 0e0f818 | 2018-11-16 12:49:06 -0800 | [diff] [blame] | 896 | !android::base::GetBoolProperty("ro.boot.dynamic_partitions", false)) { |
Hridya Valsaraju | 5425826 | 2018-09-19 21:14:18 -0700 | [diff] [blame] | 897 | reboot_target = "bootloader"; |
| 898 | } |
Tom Cherry | 98ad32a | 2017-04-17 16:34:20 -0700 | [diff] [blame] | 899 | // When rebooting to the bootloader notify the bootloader writing |
| 900 | // also the BCB. |
| 901 | if (reboot_target == "bootloader") { |
| 902 | std::string err; |
| 903 | if (!write_reboot_bootloader(&err)) { |
| 904 | LOG(ERROR) << "reboot-bootloader: Error writing " |
| 905 | "bootloader_message: " |
| 906 | << err; |
| 907 | } |
Tianjie Xu | 5e98b63 | 2019-07-18 12:48:28 -0700 | [diff] [blame] | 908 | } else if (reboot_target == "recovery") { |
| 909 | bootloader_message boot = {}; |
| 910 | if (std::string err; !read_bootloader_message(&boot, &err)) { |
| 911 | LOG(ERROR) << "Failed to read bootloader message: " << err; |
| 912 | } |
| 913 | // Update the boot command field if it's empty, and preserve |
| 914 | // the other arguments in the bootloader message. |
| 915 | if (boot.command[0] == '\0') { |
| 916 | strlcpy(boot.command, "boot-recovery", sizeof(boot.command)); |
| 917 | if (std::string err; !write_bootloader_message(boot, &err)) { |
| 918 | LOG(ERROR) << "Failed to set bootloader message: " << err; |
Nikita Ioffe | ba6968e | 2019-10-07 16:26:33 +0100 | [diff] [blame] | 919 | return; |
Tianjie Xu | 5e98b63 | 2019-07-18 12:48:28 -0700 | [diff] [blame] | 920 | } |
| 921 | } |
Hridya Valsaraju | 71fb82a | 2018-08-02 15:02:06 -0700 | [diff] [blame] | 922 | } else if (reboot_target == "sideload" || reboot_target == "sideload-auto-reboot" || |
| 923 | reboot_target == "fastboot") { |
| 924 | std::string arg = reboot_target == "sideload-auto-reboot" ? "sideload_auto_reboot" |
| 925 | : reboot_target; |
| 926 | const std::vector<std::string> options = { |
| 927 | "--" + arg, |
| 928 | }; |
| 929 | std::string err; |
| 930 | if (!write_bootloader_message(options, &err)) { |
| 931 | LOG(ERROR) << "Failed to set bootloader message: " << err; |
Nikita Ioffe | ba6968e | 2019-10-07 16:26:33 +0100 | [diff] [blame] | 932 | return; |
Hridya Valsaraju | 71fb82a | 2018-08-02 15:02:06 -0700 | [diff] [blame] | 933 | } |
| 934 | reboot_target = "recovery"; |
Tom Cherry | 98ad32a | 2017-04-17 16:34:20 -0700 | [diff] [blame] | 935 | } |
Hridya Valsaraju | 71fb82a | 2018-08-02 15:02:06 -0700 | [diff] [blame] | 936 | |
Mark Salyzyn | d7931f1 | 2019-07-10 10:33:09 -0700 | [diff] [blame] | 937 | // If there are additional parameter, pass them along |
| 938 | for (size_t i = 2; (cmd_params.size() > i) && cmd_params[i].size(); ++i) { |
| 939 | reboot_target += "," + cmd_params[i]; |
Tom Cherry | 98ad32a | 2017-04-17 16:34:20 -0700 | [diff] [blame] | 940 | } |
| 941 | } |
Tom Cherry | 98ad32a | 2017-04-17 16:34:20 -0700 | [diff] [blame] | 942 | } else { |
| 943 | command_invalid = true; |
| 944 | } |
| 945 | if (command_invalid) { |
| 946 | LOG(ERROR) << "powerctl: unrecognized command '" << command << "'"; |
Nikita Ioffe | ba6968e | 2019-10-07 16:26:33 +0100 | [diff] [blame] | 947 | return; |
| 948 | } |
| 949 | |
| 950 | if (userspace_reboot) { |
| 951 | HandleUserspaceReboot(); |
| 952 | return; |
Tom Cherry | 98ad32a | 2017-04-17 16:34:20 -0700 | [diff] [blame] | 953 | } |
| 954 | |
Wei Wang | eeab491 | 2017-06-27 22:08:45 -0700 | [diff] [blame] | 955 | LOG(INFO) << "Clear action queue and start shutdown trigger"; |
| 956 | ActionManager::GetInstance().ClearQueue(); |
| 957 | // Queue shutdown trigger first |
| 958 | ActionManager::GetInstance().QueueEventTrigger("shutdown"); |
| 959 | // Queue built-in shutdown_done |
Tom Cherry | cb0f9bb | 2017-09-12 15:58:47 -0700 | [diff] [blame] | 960 | auto shutdown_handler = [cmd, command, reboot_target, run_fsck](const BuiltinArguments&) { |
Wei Wang | eeab491 | 2017-06-27 22:08:45 -0700 | [diff] [blame] | 961 | DoReboot(cmd, command, reboot_target, run_fsck); |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 962 | return Result<void>{}; |
Wei Wang | eeab491 | 2017-06-27 22:08:45 -0700 | [diff] [blame] | 963 | }; |
| 964 | ActionManager::GetInstance().QueueBuiltinAction(shutdown_handler, "shutdown_done"); |
| 965 | |
Nikita Ioffe | ba6968e | 2019-10-07 16:26:33 +0100 | [diff] [blame] | 966 | EnterShutdown(); |
| 967 | } |
Wei Wang | eeab491 | 2017-06-27 22:08:45 -0700 | [diff] [blame] | 968 | |
Nikita Ioffe | ba6968e | 2019-10-07 16:26:33 +0100 | [diff] [blame] | 969 | bool IsShuttingDown() { |
| 970 | return shutting_down; |
Tom Cherry | 98ad32a | 2017-04-17 16:34:20 -0700 | [diff] [blame] | 971 | } |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 972 | |
| 973 | } // namespace init |
| 974 | } // namespace android |