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