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