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