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