blob: d9acee57f32a0703882e58fa6af0fea5dba71d75 [file] [log] [blame]
Keun-young Park8d01f632017-03-13 11:54:47 -07001/*
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 Cherry3f5eaae52017-04-06 16:30:22 -070016
17#include "reboot.h"
18
Keun-young Park8d01f632017-03-13 11:54:47 -070019#include <dirent.h>
20#include <fcntl.h>
Keun-young Park2ba5c812017-03-29 12:54:40 -070021#include <linux/fs.h>
Jaegeuk Kim2aedc822018-11-20 13:27:06 -080022#include <linux/loop.h>
josephjangaaddf282019-04-16 18:46:24 +080023#include <mntent.h>
24#include <semaphore.h>
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +010025#include <stdlib.h>
Keun-young Park8d01f632017-03-13 11:54:47 -070026#include <sys/cdefs.h>
Keun-young Park2ba5c812017-03-29 12:54:40 -070027#include <sys/ioctl.h>
Keun-young Park8d01f632017-03-13 11:54:47 -070028#include <sys/mount.h>
Keun-young Park8d01f632017-03-13 11:54:47 -070029#include <sys/stat.h>
josephjangaaddf282019-04-16 18:46:24 +080030#include <sys/swap.h>
Keun-young Park8d01f632017-03-13 11:54:47 -070031#include <sys/syscall.h>
32#include <sys/types.h>
33#include <sys/wait.h>
34
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +010035#include <chrono>
Keun-young Park8d01f632017-03-13 11:54:47 -070036#include <memory>
Keun-young Park7830d592017-03-27 16:07:02 -070037#include <set>
Keun-young Park8d01f632017-03-13 11:54:47 -070038#include <thread>
39#include <vector>
40
Nikita Ioffe23dbd6d2019-11-14 01:21:24 +000041#include <InitProperties.sysprop.h>
Tom Cherryede0d532017-07-06 14:20:11 -070042#include <android-base/chrono_utils.h>
Keun-young Park8d01f632017-03-13 11:54:47 -070043#include <android-base/file.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070044#include <android-base/logging.h>
Keun-young Park8d01f632017-03-13 11:54:47 -070045#include <android-base/macros.h>
Tom Cherryccf23532017-03-28 16:40:41 -070046#include <android-base/properties.h>
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +010047#include <android-base/scopeguard.h>
Keun-young Park8d01f632017-03-13 11:54:47 -070048#include <android-base/strings.h>
Keun-young Park2ba5c812017-03-29 12:54:40 -070049#include <android-base/unique_fd.h>
Keun-young Park8d01f632017-03-13 11:54:47 -070050#include <bootloader_message/bootloader_message.h>
51#include <cutils/android_reboot.h>
Keun-young Park8d01f632017-03-13 11:54:47 -070052#include <fs_mgr.h>
53#include <logwrap/logwrap.h>
Todd Poynorfc827be2017-04-13 15:17:24 -070054#include <private/android_filesystem_config.h>
Tom Cherry0c8d6d22017-08-10 12:22:44 -070055#include <selinux/selinux.h>
Keun-young Park8d01f632017-03-13 11:54:47 -070056
Nikita Ioffeba6968e2019-10-07 16:26:33 +010057#include "action.h"
Tom Cherry7fd3bc22018-02-13 15:36:14 -080058#include "action_manager.h"
Nikita Ioffeba6968e2019-10-07 16:26:33 +010059#include "builtin_arguments.h"
Wei Wangeeab4912017-06-27 22:08:45 -070060#include "init.h"
Nikita Ioffeab91ee92019-11-06 21:40:31 +000061#include "mount_namespace.h"
Tom Cherry802864c2020-03-12 14:29:25 -070062#include "property_service.h"
Tom Cherry44aceed2018-08-03 13:36:18 -070063#include "reboot_utils.h"
Keun-young Park8d01f632017-03-13 11:54:47 -070064#include "service.h"
Tom Cherry2aeb1ad2019-06-26 10:46:20 -070065#include "service_list.h"
Luis Hector Chavez9f97f472017-09-06 13:43:57 -070066#include "sigchld_handler.h"
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +010067#include "util.h"
Keun-young Park8d01f632017-03-13 11:54:47 -070068
Tom Cherry2436e6b2019-09-19 11:16:19 -070069using namespace std::literals;
70
Nikita Ioffec0df1872019-11-13 21:47:06 +000071using android::base::boot_clock;
Tom Cherry4f4cacc2019-01-08 10:39:00 -080072using android::base::GetBoolProperty;
Nikita Ioffe7b41a152020-03-25 00:06:51 +000073using android::base::GetUintProperty;
Nikita Ioffec0df1872019-11-13 21:47:06 +000074using android::base::SetProperty;
Mark Salyzyn62909822017-10-09 09:27:16 -070075using android::base::Split;
Tom Cherryede0d532017-07-06 14:20:11 -070076using android::base::Timer;
Jaegeuk Kim2aedc822018-11-20 13:27:06 -080077using android::base::unique_fd;
Nikita Ioffec0df1872019-11-13 21:47:06 +000078using android::base::WaitForProperty;
josephjangaaddf282019-04-16 18:46:24 +080079using android::base::WriteStringToFile;
Keun-young Park8d01f632017-03-13 11:54:47 -070080
Tom Cherry81f5d3e2017-06-22 12:53:17 -070081namespace android {
82namespace init {
83
Nikita Ioffeba6968e2019-10-07 16:26:33 +010084static bool shutting_down = false;
85
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +010086static const std::set<std::string> kDebuggingServices{"tombstoned", "logd", "adbd", "console"};
87
Nikita Ioffe7ba50302020-11-27 18:57:44 +000088static std::set<std::string> GetPostDataDebuggingServices() {
89 std::set<std::string> ret;
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +010090 for (const auto& s : ServiceList::GetInstance()) {
Nikita Ioffe7ba50302020-11-27 18:57:44 +000091 if (kDebuggingServices.count(s->name()) && s->is_post_data()) {
92 ret.insert(s->name());
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +010093 }
94 }
95 return ret;
96}
97
Nikita Ioffed485bbb2020-02-26 15:18:56 +000098static void PersistRebootReason(const char* reason, bool write_to_property) {
99 if (write_to_property) {
100 SetProperty(LAST_REBOOT_REASON_PROPERTY, reason);
101 }
Tom Cherry10615eb2020-03-31 16:31:37 -0700102 auto fd = unique_fd(TEMP_FAILURE_RETRY(open(
103 LAST_REBOOT_REASON_FILE, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_BINARY, 0666)));
104 if (!fd.ok()) {
105 PLOG(ERROR) << "Could not open '" << LAST_REBOOT_REASON_FILE
106 << "' to persist reboot reason";
107 return;
108 }
109 WriteStringToFd(reason, fd);
110 fsync(fd.get());
Nikita Ioffed0bc0b62020-02-19 16:21:11 +0000111}
112
Keun-young Park8d01f632017-03-13 11:54:47 -0700113// represents umount status during reboot / shutdown.
114enum UmountStat {
115 /* umount succeeded. */
116 UMOUNT_STAT_SUCCESS = 0,
117 /* umount was not run. */
118 UMOUNT_STAT_SKIPPED = 1,
119 /* umount failed with timeout. */
120 UMOUNT_STAT_TIMEOUT = 2,
121 /* could not run due to error */
122 UMOUNT_STAT_ERROR = 3,
123 /* not used by init but reserved for other part to use this to represent the
124 the state where umount status before reboot is not found / available. */
125 UMOUNT_STAT_NOT_AVAILABLE = 4,
126};
127
128// Utility for struct mntent
129class MountEntry {
130 public:
Keun-young Park2ba5c812017-03-29 12:54:40 -0700131 explicit MountEntry(const mntent& entry)
Keun-young Park8d01f632017-03-13 11:54:47 -0700132 : mnt_fsname_(entry.mnt_fsname),
133 mnt_dir_(entry.mnt_dir),
134 mnt_type_(entry.mnt_type),
Keun-young Park2ba5c812017-03-29 12:54:40 -0700135 mnt_opts_(entry.mnt_opts) {}
Keun-young Park8d01f632017-03-13 11:54:47 -0700136
Jaegeuk Kim0f04f722017-09-25 10:55:39 -0700137 bool Umount(bool force) {
Tom Cherryc9fec9d2018-02-15 14:26:58 -0800138 LOG(INFO) << "Unmounting " << mnt_fsname_ << ":" << mnt_dir_ << " opts " << mnt_opts_;
Jaegeuk Kim0f04f722017-09-25 10:55:39 -0700139 int r = umount2(mnt_dir_.c_str(), force ? MNT_FORCE : 0);
Keun-young Park2ba5c812017-03-29 12:54:40 -0700140 if (r == 0) {
Tom Cherryc9fec9d2018-02-15 14:26:58 -0800141 LOG(INFO) << "Umounted " << mnt_fsname_ << ":" << mnt_dir_ << " opts " << mnt_opts_;
Keun-young Park2ba5c812017-03-29 12:54:40 -0700142 return true;
143 } else {
Tom Cherryc9fec9d2018-02-15 14:26:58 -0800144 PLOG(WARNING) << "Cannot umount " << mnt_fsname_ << ":" << mnt_dir_ << " opts "
Keun-young Park2ba5c812017-03-29 12:54:40 -0700145 << mnt_opts_;
146 return false;
147 }
148 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700149
Keun-young Park2ba5c812017-03-29 12:54:40 -0700150 void DoFsck() {
151 int st;
152 if (IsF2Fs()) {
153 const char* f2fs_argv[] = {
Randall Huangdf2faa42019-01-15 15:00:56 +0800154 "/system/bin/fsck.f2fs",
155 "-a",
156 mnt_fsname_.c_str(),
Keun-young Park2ba5c812017-03-29 12:54:40 -0700157 };
Tom Cherry3a803eb2019-09-25 16:23:50 -0700158 logwrap_fork_execvp(arraysize(f2fs_argv), f2fs_argv, &st, false, LOG_KLOG, true,
159 nullptr);
Keun-young Park2ba5c812017-03-29 12:54:40 -0700160 } else if (IsExt4()) {
161 const char* ext4_argv[] = {
Randall Huangdf2faa42019-01-15 15:00:56 +0800162 "/system/bin/e2fsck",
163 "-y",
164 mnt_fsname_.c_str(),
Keun-young Park2ba5c812017-03-29 12:54:40 -0700165 };
Tom Cherry3a803eb2019-09-25 16:23:50 -0700166 logwrap_fork_execvp(arraysize(ext4_argv), ext4_argv, &st, false, LOG_KLOG, true,
167 nullptr);
Keun-young Park2ba5c812017-03-29 12:54:40 -0700168 }
169 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700170
171 static bool IsBlockDevice(const struct mntent& mntent) {
172 return android::base::StartsWith(mntent.mnt_fsname, "/dev/block");
173 }
174
175 static bool IsEmulatedDevice(const struct mntent& mntent) {
Keun-young Park2ba5c812017-03-29 12:54:40 -0700176 return android::base::StartsWith(mntent.mnt_fsname, "/data/");
Keun-young Park8d01f632017-03-13 11:54:47 -0700177 }
178
179 private:
Keun-young Park2ba5c812017-03-29 12:54:40 -0700180 bool IsF2Fs() const { return mnt_type_ == "f2fs"; }
181
182 bool IsExt4() const { return mnt_type_ == "ext4"; }
183
Keun-young Park8d01f632017-03-13 11:54:47 -0700184 std::string mnt_fsname_;
185 std::string mnt_dir_;
186 std::string mnt_type_;
Keun-young Park2ba5c812017-03-29 12:54:40 -0700187 std::string mnt_opts_;
Keun-young Park8d01f632017-03-13 11:54:47 -0700188};
189
190// Turn off backlight while we are performing power down cleanup activities.
Tom Cherry832f9f12020-03-10 11:47:24 -0700191static void TurnOffBacklight() {
Steven Morelandd5eccfd2018-01-19 13:01:53 -0800192 Service* service = ServiceList::GetInstance().FindService("blank_screen");
193 if (service == nullptr) {
194 LOG(WARNING) << "cannot find blank_screen in TurnOffBacklight";
Keun-young Park8d01f632017-03-13 11:54:47 -0700195 return;
196 }
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900197 if (auto result = service->Start(); !result.ok()) {
Tom Cherryd9872642018-10-11 10:38:05 -0700198 LOG(WARNING) << "Could not start blank_screen service: " << result.error();
199 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700200}
201
Nikita Ioffebee7b8c2019-12-02 11:51:39 +0000202static Result<void> CallVdc(const std::string& system, const std::string& cmd) {
Nikita Ioffea3be9962020-04-16 21:37:03 +0100203 LOG(INFO) << "Calling /system/bin/vdc " << system << " " << cmd;
Nikita Ioffebee7b8c2019-12-02 11:51:39 +0000204 const char* vdc_argv[] = {"/system/bin/vdc", system.c_str(), cmd.c_str()};
Keun-young Park8d01f632017-03-13 11:54:47 -0700205 int status;
Nikita Ioffe12a36072019-10-23 20:11:32 +0100206 if (logwrap_fork_execvp(arraysize(vdc_argv), vdc_argv, &status, false, LOG_KLOG, true,
207 nullptr) != 0) {
Nikita Ioffebee7b8c2019-12-02 11:51:39 +0000208 return ErrnoError() << "Failed to call '/system/bin/vdc " << system << " " << cmd << "'";
Nikita Ioffe12a36072019-10-23 20:11:32 +0100209 }
210 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
211 return {};
212 }
Nikita Ioffebee7b8c2019-12-02 11:51:39 +0000213 return Error() << "'/system/bin/vdc " << system << " " << cmd << "' failed : " << status;
Keun-young Park8d01f632017-03-13 11:54:47 -0700214}
215
216static void LogShutdownTime(UmountStat stat, Timer* t) {
Tom Cherryede0d532017-07-06 14:20:11 -0700217 LOG(WARNING) << "powerctl_shutdown_time_ms:" << std::to_string(t->duration().count()) << ":"
218 << stat;
Keun-young Park8d01f632017-03-13 11:54:47 -0700219}
220
Tom Cherry2436e6b2019-09-19 11:16:19 -0700221static bool IsDataMounted() {
222 std::unique_ptr<std::FILE, int (*)(std::FILE*)> fp(setmntent("/proc/mounts", "re"), endmntent);
223 if (fp == nullptr) {
224 PLOG(ERROR) << "Failed to open /proc/mounts";
225 return false;
226 }
227 mntent* mentry;
228 while ((mentry = getmntent(fp.get())) != nullptr) {
229 if (mentry->mnt_dir == "/data"s) {
230 return true;
231 }
232 }
233 return false;
234}
235
236// Find all read+write block devices and emulated devices in /proc/mounts and add them to
237// the correpsponding list.
Nikita Ioffee7ec8c82019-10-24 23:18:39 +0100238static bool FindPartitionsToUmount(std::vector<MountEntry>* block_dev_partitions,
239 std::vector<MountEntry>* emulated_partitions, bool dump) {
Tom Cherryf274e782018-10-03 13:13:41 -0700240 std::unique_ptr<std::FILE, int (*)(std::FILE*)> fp(setmntent("/proc/mounts", "re"), endmntent);
Keun-young Park8d01f632017-03-13 11:54:47 -0700241 if (fp == nullptr) {
242 PLOG(ERROR) << "Failed to open /proc/mounts";
243 return false;
244 }
245 mntent* mentry;
246 while ((mentry = getmntent(fp.get())) != nullptr) {
Keun-young Park2ba5c812017-03-29 12:54:40 -0700247 if (dump) {
248 LOG(INFO) << "mount entry " << mentry->mnt_fsname << ":" << mentry->mnt_dir << " opts "
249 << mentry->mnt_opts << " type " << mentry->mnt_type;
250 } else if (MountEntry::IsBlockDevice(*mentry) && hasmntopt(mentry, "rw")) {
Keun-young Park6e12b382017-07-17 12:20:33 -0700251 std::string mount_dir(mentry->mnt_dir);
252 // These are R/O partitions changed to R/W after adb remount.
253 // Do not umount them as shutdown critical services may rely on them.
Wei Wanga01c27e2017-07-25 10:52:08 -0700254 if (mount_dir != "/" && mount_dir != "/system" && mount_dir != "/vendor" &&
255 mount_dir != "/oem") {
Nikita Ioffee7ec8c82019-10-24 23:18:39 +0100256 block_dev_partitions->emplace(block_dev_partitions->begin(), *mentry);
Keun-young Park6e12b382017-07-17 12:20:33 -0700257 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700258 } else if (MountEntry::IsEmulatedDevice(*mentry)) {
Nikita Ioffee7ec8c82019-10-24 23:18:39 +0100259 emulated_partitions->emplace(emulated_partitions->begin(), *mentry);
Keun-young Park8d01f632017-03-13 11:54:47 -0700260 }
261 }
262 return true;
263}
264
Jonglin Lee28a2c922019-01-15 16:38:44 -0800265static void DumpUmountDebuggingInfo() {
Keun-young Park2ba5c812017-03-29 12:54:40 -0700266 int status;
267 if (!security_getenforce()) {
268 LOG(INFO) << "Run lsof";
269 const char* lsof_argv[] = {"/system/bin/lsof"};
Tom Cherry3a803eb2019-09-25 16:23:50 -0700270 logwrap_fork_execvp(arraysize(lsof_argv), lsof_argv, &status, false, LOG_KLOG, true,
271 nullptr);
Keun-young Park8d01f632017-03-13 11:54:47 -0700272 }
Keun-young Park2ba5c812017-03-29 12:54:40 -0700273 FindPartitionsToUmount(nullptr, nullptr, true);
Jonglin Lee28a2c922019-01-15 16:38:44 -0800274 // dump current CPU stack traces and uninterruptible tasks
josephjangaaddf282019-04-16 18:46:24 +0800275 WriteStringToFile("l", PROC_SYSRQ);
276 WriteStringToFile("w", PROC_SYSRQ);
Keun-young Park2ba5c812017-03-29 12:54:40 -0700277}
278
Tom Cherryede0d532017-07-06 14:20:11 -0700279static UmountStat UmountPartitions(std::chrono::milliseconds timeout) {
Keun-young Park2ba5c812017-03-29 12:54:40 -0700280 Timer t;
Keun-young Park2ba5c812017-03-29 12:54:40 -0700281 /* data partition needs all pending writes to be completed and all emulated partitions
282 * umounted.If the current waiting is not good enough, give
283 * up and leave it to e2fsck after reboot to fix it.
284 */
285 while (true) {
286 std::vector<MountEntry> block_devices;
287 std::vector<MountEntry> emulated_devices;
288 if (!FindPartitionsToUmount(&block_devices, &emulated_devices, false)) {
289 return UMOUNT_STAT_ERROR;
290 }
291 if (block_devices.size() == 0) {
Wei Wang8c00e422017-08-16 14:01:46 -0700292 return UMOUNT_STAT_SUCCESS;
Keun-young Park2ba5c812017-03-29 12:54:40 -0700293 }
Wei Wang8c00e422017-08-16 14:01:46 -0700294 bool unmount_done = true;
295 if (emulated_devices.size() > 0) {
Wei Wang25dc30f2017-10-23 15:32:03 -0700296 for (auto& entry : emulated_devices) {
297 if (!entry.Umount(false)) unmount_done = false;
298 }
Wei Wang8c00e422017-08-16 14:01:46 -0700299 if (unmount_done) {
300 sync();
301 }
Keun-young Park2ba5c812017-03-29 12:54:40 -0700302 }
Wei Wang25dc30f2017-10-23 15:32:03 -0700303 for (auto& entry : block_devices) {
304 if (!entry.Umount(timeout == 0ms)) unmount_done = false;
305 }
Wei Wang8c00e422017-08-16 14:01:46 -0700306 if (unmount_done) {
307 return UMOUNT_STAT_SUCCESS;
Keun-young Park2ba5c812017-03-29 12:54:40 -0700308 }
Wei Wang8c00e422017-08-16 14:01:46 -0700309 if ((timeout < t.duration())) { // try umount at least once
310 return UMOUNT_STAT_TIMEOUT;
Keun-young Park2ba5c812017-03-29 12:54:40 -0700311 }
Keun-young Park2ba5c812017-03-29 12:54:40 -0700312 std::this_thread::sleep_for(100ms);
313 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700314}
315
josephjangaaddf282019-04-16 18:46:24 +0800316static void KillAllProcesses() {
317 WriteStringToFile("i", PROC_SYSRQ);
318}
319
320// Create reboot/shutdwon monitor thread
Nikita Ioffee7ec8c82019-10-24 23:18:39 +0100321void RebootMonitorThread(unsigned int cmd, const std::string& reboot_target,
322 sem_t* reboot_semaphore, std::chrono::milliseconds shutdown_timeout,
323 bool* reboot_monitor_run) {
josephjangaaddf282019-04-16 18:46:24 +0800324 unsigned int remaining_shutdown_time = 0;
325
Tom Cherry10615eb2020-03-31 16:31:37 -0700326 // 300 seconds more than the timeout passed to the thread as there is a final Umount pass
josephjangaaddf282019-04-16 18:46:24 +0800327 // after the timeout is reached.
Tom Cherry10615eb2020-03-31 16:31:37 -0700328 constexpr unsigned int shutdown_watchdog_timeout_default = 300;
josephjangaaddf282019-04-16 18:46:24 +0800329 auto shutdown_watchdog_timeout = android::base::GetUintProperty(
330 "ro.build.shutdown.watchdog.timeout", shutdown_watchdog_timeout_default);
331 remaining_shutdown_time = shutdown_watchdog_timeout + shutdown_timeout.count() / 1000;
332
333 while (*reboot_monitor_run == true) {
334 if (TEMP_FAILURE_RETRY(sem_wait(reboot_semaphore)) == -1) {
335 LOG(ERROR) << "sem_wait failed and exit RebootMonitorThread()";
336 return;
337 }
338
339 timespec shutdown_timeout_timespec;
340 if (clock_gettime(CLOCK_MONOTONIC, &shutdown_timeout_timespec) == -1) {
341 LOG(ERROR) << "clock_gettime() fail! exit RebootMonitorThread()";
342 return;
343 }
344
345 // If there are some remaining shutdown time left from previous round, we use
346 // remaining time here.
347 shutdown_timeout_timespec.tv_sec += remaining_shutdown_time;
348
349 LOG(INFO) << "shutdown_timeout_timespec.tv_sec: " << shutdown_timeout_timespec.tv_sec;
350
351 int sem_return = 0;
352 while ((sem_return = sem_timedwait_monotonic_np(reboot_semaphore,
353 &shutdown_timeout_timespec)) == -1 &&
354 errno == EINTR) {
355 }
356
357 if (sem_return == -1) {
358 LOG(ERROR) << "Reboot thread timed out";
359
360 if (android::base::GetBoolProperty("ro.debuggable", false) == true) {
Tom Cherry2436e6b2019-09-19 11:16:19 -0700361 if (false) {
362 // SEPolicy will block debuggerd from running and this is intentional.
363 // But these lines are left to be enabled during debugging.
364 LOG(INFO) << "Try to dump init process call trace:";
365 const char* vdc_argv[] = {"/system/bin/debuggerd", "-b", "1"};
366 int status;
Tom Cherry3a803eb2019-09-25 16:23:50 -0700367 logwrap_fork_execvp(arraysize(vdc_argv), vdc_argv, &status, false, LOG_KLOG,
368 true, nullptr);
Tom Cherry2436e6b2019-09-19 11:16:19 -0700369 }
josephjangaaddf282019-04-16 18:46:24 +0800370 LOG(INFO) << "Show stack for all active CPU:";
371 WriteStringToFile("l", PROC_SYSRQ);
372
373 LOG(INFO) << "Show tasks that are in disk sleep(uninterruptable sleep), which are "
374 "like "
375 "blocked in mutex or hardware register access:";
376 WriteStringToFile("w", PROC_SYSRQ);
377 }
378
379 // In shutdown case,notify kernel to sync and umount fs to read-only before shutdown.
380 if (cmd == ANDROID_RB_POWEROFF || cmd == ANDROID_RB_THERMOFF) {
381 WriteStringToFile("s", PROC_SYSRQ);
382
383 WriteStringToFile("u", PROC_SYSRQ);
384
Nikita Ioffee7ec8c82019-10-24 23:18:39 +0100385 RebootSystem(cmd, reboot_target);
josephjangaaddf282019-04-16 18:46:24 +0800386 }
387
388 LOG(ERROR) << "Trigger crash at last!";
389 WriteStringToFile("c", PROC_SYSRQ);
390 } else {
391 timespec current_time_timespec;
392
393 if (clock_gettime(CLOCK_MONOTONIC, &current_time_timespec) == -1) {
394 LOG(ERROR) << "clock_gettime() fail! exit RebootMonitorThread()";
395 return;
396 }
397
398 remaining_shutdown_time =
399 shutdown_timeout_timespec.tv_sec - current_time_timespec.tv_sec;
400
401 LOG(INFO) << "remaining_shutdown_time: " << remaining_shutdown_time;
402 }
403 }
404}
Keun-young Park3ee0df92017-03-27 11:21:09 -0700405
Keun-young Park8d01f632017-03-13 11:54:47 -0700406/* Try umounting all emulated file systems R/W block device cfile systems.
407 * This will just try umount and give it up if it fails.
408 * For fs like ext4, this is ok as file system will be marked as unclean shutdown
409 * and necessary check can be done at the next reboot.
410 * For safer shutdown, caller needs to make sure that
411 * all processes / emulated partition for the target fs are all cleaned-up.
412 *
413 * return true when umount was successful. false when timed out.
414 */
Nikita Ioffee7ec8c82019-10-24 23:18:39 +0100415static UmountStat TryUmountAndFsck(unsigned int cmd, bool run_fsck,
josephjangaaddf282019-04-16 18:46:24 +0800416 std::chrono::milliseconds timeout, sem_t* reboot_semaphore) {
Keun-young Park3ee0df92017-03-27 11:21:09 -0700417 Timer t;
Keun-young Park2ba5c812017-03-29 12:54:40 -0700418 std::vector<MountEntry> block_devices;
419 std::vector<MountEntry> emulated_devices;
Keun-young Park8d01f632017-03-13 11:54:47 -0700420
Nikita Ioffee7ec8c82019-10-24 23:18:39 +0100421 if (run_fsck && !FindPartitionsToUmount(&block_devices, &emulated_devices, false)) {
Keun-young Park8d01f632017-03-13 11:54:47 -0700422 return UMOUNT_STAT_ERROR;
423 }
Keun-young Park2ba5c812017-03-29 12:54:40 -0700424
Tom Cherryede0d532017-07-06 14:20:11 -0700425 UmountStat stat = UmountPartitions(timeout - t.duration());
Keun-young Park2ba5c812017-03-29 12:54:40 -0700426 if (stat != UMOUNT_STAT_SUCCESS) {
427 LOG(INFO) << "umount timeout, last resort, kill all and try";
Jonglin Lee28a2c922019-01-15 16:38:44 -0800428 if (DUMP_ON_UMOUNT_FAILURE) DumpUmountDebuggingInfo();
Keun-young Park3ee0df92017-03-27 11:21:09 -0700429 KillAllProcesses();
Keun-young Park2ba5c812017-03-29 12:54:40 -0700430 // even if it succeeds, still it is timeout and do not run fsck with all processes killed
Keun-young Parkc59b8222017-07-18 18:52:25 -0700431 UmountStat st = UmountPartitions(0ms);
Jonglin Lee28a2c922019-01-15 16:38:44 -0800432 if ((st != UMOUNT_STAT_SUCCESS) && DUMP_ON_UMOUNT_FAILURE) DumpUmountDebuggingInfo();
Keun-young Park8d01f632017-03-13 11:54:47 -0700433 }
434
Nikita Ioffee7ec8c82019-10-24 23:18:39 +0100435 if (stat == UMOUNT_STAT_SUCCESS && run_fsck) {
josephjangaaddf282019-04-16 18:46:24 +0800436 LOG(INFO) << "Pause reboot monitor thread before fsck";
437 sem_post(reboot_semaphore);
438
Keun-young Park2ba5c812017-03-29 12:54:40 -0700439 // fsck part is excluded from timeout check. It only runs for user initiated shutdown
440 // and should not affect reboot time.
441 for (auto& entry : block_devices) {
442 entry.DoFsck();
443 }
josephjangaaddf282019-04-16 18:46:24 +0800444
445 LOG(INFO) << "Resume reboot monitor thread after fsck";
446 sem_post(reboot_semaphore);
Keun-young Park2ba5c812017-03-29 12:54:40 -0700447 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700448 return stat;
449}
450
Jaegeuk Kim2aedc822018-11-20 13:27:06 -0800451// zram is able to use backing device on top of a loopback device.
452// In order to unmount /data successfully, we have to kill the loopback device first
453#define ZRAM_DEVICE "/dev/block/zram0"
454#define ZRAM_RESET "/sys/block/zram0/reset"
455#define ZRAM_BACK_DEV "/sys/block/zram0/backing_dev"
Nikita Ioffe12a36072019-10-23 20:11:32 +0100456static Result<void> KillZramBackingDevice() {
Nikita Ioffea3be9962020-04-16 21:37:03 +0100457 if (access(ZRAM_BACK_DEV, F_OK) != 0 && errno == ENOENT) {
458 LOG(INFO) << "No zram backing device configured";
459 return {};
460 }
Jaegeuk Kim2aedc822018-11-20 13:27:06 -0800461 std::string backing_dev;
Nikita Ioffea3be9962020-04-16 21:37:03 +0100462 if (!android::base::ReadFileToString(ZRAM_BACK_DEV, &backing_dev)) {
463 return ErrnoError() << "Failed to read " << ZRAM_BACK_DEV;
464 }
Jaegeuk Kim2aedc822018-11-20 13:27:06 -0800465
466 // cut the last "\n"
467 backing_dev.erase(backing_dev.length() - 1);
468
469 // shutdown zram handle
470 Timer swap_timer;
471 LOG(INFO) << "swapoff() start...";
472 if (swapoff(ZRAM_DEVICE) == -1) {
Nikita Ioffe12a36072019-10-23 20:11:32 +0100473 return ErrnoError() << "zram_backing_dev: swapoff (" << backing_dev << ")"
474 << " failed";
Jaegeuk Kim2aedc822018-11-20 13:27:06 -0800475 }
476 LOG(INFO) << "swapoff() took " << swap_timer;;
477
josephjangaaddf282019-04-16 18:46:24 +0800478 if (!WriteStringToFile("1", ZRAM_RESET)) {
Nikita Ioffe12a36072019-10-23 20:11:32 +0100479 return Error() << "zram_backing_dev: reset (" << backing_dev << ")"
480 << " failed";
Jaegeuk Kim2aedc822018-11-20 13:27:06 -0800481 }
482
Nikita Ioffea3be9962020-04-16 21:37:03 +0100483 if (!android::base::StartsWith(backing_dev, "/dev/block/loop")) {
484 LOG(INFO) << backing_dev << " is not a loop device. Exiting early";
485 return {};
486 }
487
Jaegeuk Kim2aedc822018-11-20 13:27:06 -0800488 // clear loopback device
489 unique_fd loop(TEMP_FAILURE_RETRY(open(backing_dev.c_str(), O_RDWR | O_CLOEXEC)));
490 if (loop.get() < 0) {
Nikita Ioffe12a36072019-10-23 20:11:32 +0100491 return ErrnoError() << "zram_backing_dev: open(" << backing_dev << ")"
492 << " failed";
Jaegeuk Kim2aedc822018-11-20 13:27:06 -0800493 }
494
495 if (ioctl(loop.get(), LOOP_CLR_FD, 0) < 0) {
Nikita Ioffe12a36072019-10-23 20:11:32 +0100496 return ErrnoError() << "zram_backing_dev: loop_clear (" << backing_dev << ")"
497 << " failed";
Jaegeuk Kim2aedc822018-11-20 13:27:06 -0800498 }
499 LOG(INFO) << "zram_backing_dev: `" << backing_dev << "` is cleared successfully.";
Nikita Ioffe12a36072019-10-23 20:11:32 +0100500 return {};
Jaegeuk Kim2aedc822018-11-20 13:27:06 -0800501}
502
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +0100503// Stops given services, waits for them to be stopped for |timeout| ms.
504// If terminate is true, then SIGTERM is sent to services, otherwise SIGKILL is sent.
Nikita Ioffe7ba50302020-11-27 18:57:44 +0000505// Note that services are stopped in order given by |ServiceList::services_in_shutdown_order|
506// function.
507static void StopServices(const std::set<std::string>& services, std::chrono::milliseconds timeout,
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +0100508 bool terminate) {
509 LOG(INFO) << "Stopping " << services.size() << " services by sending "
510 << (terminate ? "SIGTERM" : "SIGKILL");
511 std::vector<pid_t> pids;
512 pids.reserve(services.size());
Nikita Ioffe7ba50302020-11-27 18:57:44 +0000513 for (const auto& s : ServiceList::GetInstance().services_in_shutdown_order()) {
514 if (services.count(s->name()) == 0) {
515 continue;
516 }
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +0100517 if (s->pid() > 0) {
518 pids.push_back(s->pid());
519 }
520 if (terminate) {
521 s->Terminate();
522 } else {
523 s->Stop();
524 }
525 }
526 if (timeout > 0ms) {
527 WaitToBeReaped(pids, timeout);
528 } else {
529 // Even if we don't to wait for services to stop, we still optimistically reap zombies.
530 ReapAnyOutstandingChildren();
531 }
532}
533
534// Like StopServices, but also logs all the services that failed to stop after the provided timeout.
535// Returns number of violators.
Nikita Ioffe7ba50302020-11-27 18:57:44 +0000536static int StopServicesAndLogViolations(const std::set<std::string>& services,
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +0100537 std::chrono::milliseconds timeout, bool terminate) {
538 StopServices(services, timeout, terminate);
539 int still_running = 0;
Nikita Ioffe7ba50302020-11-27 18:57:44 +0000540 for (const auto& s : ServiceList::GetInstance()) {
541 if (s->IsRunning() && services.count(s->name())) {
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +0100542 LOG(ERROR) << "[service-misbehaving] : service '" << s->name() << "' is still running "
543 << timeout.count() << "ms after receiving "
544 << (terminate ? "SIGTERM" : "SIGKILL");
545 still_running++;
546 }
547 }
548 return still_running;
549}
550
Nikita Ioffe91a98482020-06-10 00:53:59 +0100551static Result<void> UnmountAllApexes() {
552 const char* args[] = {"/system/bin/apexd", "--unmount-all"};
553 int status;
554 if (logwrap_fork_execvp(arraysize(args), args, &status, false, LOG_KLOG, true, nullptr) != 0) {
555 return ErrnoError() << "Failed to call '/system/bin/apexd --unmount-all'";
556 }
557 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
558 return {};
559 }
560 return Error() << "'/system/bin/apexd --unmount-all' failed : " << status;
561}
562
Tom Cherry44aceed2018-08-03 13:36:18 -0700563//* Reboot / shutdown the system.
564// cmd ANDROID_RB_* as defined in android_reboot.h
565// reason Reason string like "reboot", "shutdown,userrequested"
Nikita Ioffee7ec8c82019-10-24 23:18:39 +0100566// reboot_target Reboot target string like "bootloader". Otherwise, it should be an empty string.
567// run_fsck Whether to run fsck after umount is done.
Tom Cherry44aceed2018-08-03 13:36:18 -0700568//
Nikita Ioffee7ec8c82019-10-24 23:18:39 +0100569static void DoReboot(unsigned int cmd, const std::string& reason, const std::string& reboot_target,
570 bool run_fsck) {
Keun-young Park8d01f632017-03-13 11:54:47 -0700571 Timer t;
Nikita Ioffee7ec8c82019-10-24 23:18:39 +0100572 LOG(INFO) << "Reboot start, reason: " << reason << ", reboot_target: " << reboot_target;
Tom Cherry0a72e6c2018-03-19 16:19:01 -0700573
Tom Cherry0a72e6c2018-03-19 16:19:01 -0700574 bool is_thermal_shutdown = cmd == ANDROID_RB_THERMOFF;
575
576 auto shutdown_timeout = 0ms;
577 if (!SHUTDOWN_ZERO_TIMEOUT) {
Wei Wangb5de0882018-10-09 12:42:06 -0700578 constexpr unsigned int shutdown_timeout_default = 6;
579 constexpr unsigned int max_thermal_shutdown_timeout = 3;
580 auto shutdown_timeout_final = android::base::GetUintProperty("ro.build.shutdown_timeout",
581 shutdown_timeout_default);
582 if (is_thermal_shutdown && shutdown_timeout_final > max_thermal_shutdown_timeout) {
583 shutdown_timeout_final = max_thermal_shutdown_timeout;
Tom Cherry0a72e6c2018-03-19 16:19:01 -0700584 }
Wei Wangb5de0882018-10-09 12:42:06 -0700585 shutdown_timeout = std::chrono::seconds(shutdown_timeout_final);
Tom Cherry0a72e6c2018-03-19 16:19:01 -0700586 }
587 LOG(INFO) << "Shutdown timeout: " << shutdown_timeout.count() << " ms";
588
josephjangaaddf282019-04-16 18:46:24 +0800589 sem_t reboot_semaphore;
590 if (sem_init(&reboot_semaphore, false, 0) == -1) {
591 // These should never fail, but if they do, skip the graceful reboot and reboot immediately.
592 LOG(ERROR) << "sem_init() fail and RebootSystem() return!";
Nikita Ioffee7ec8c82019-10-24 23:18:39 +0100593 RebootSystem(cmd, reboot_target);
josephjangaaddf282019-04-16 18:46:24 +0800594 }
595
596 // Start a thread to monitor init shutdown process
597 LOG(INFO) << "Create reboot monitor thread.";
598 bool reboot_monitor_run = true;
Nikita Ioffee7ec8c82019-10-24 23:18:39 +0100599 std::thread reboot_monitor_thread(&RebootMonitorThread, cmd, reboot_target, &reboot_semaphore,
josephjangaaddf282019-04-16 18:46:24 +0800600 shutdown_timeout, &reboot_monitor_run);
601 reboot_monitor_thread.detach();
602
603 // Start reboot monitor thread
604 sem_post(&reboot_semaphore);
605
Tom Cherry10615eb2020-03-31 16:31:37 -0700606 // Ensure last reboot reason is reduced to canonical
607 // alias reported in bootloader or system boot reason.
608 size_t skip = 0;
609 std::vector<std::string> reasons = Split(reason, ",");
610 if (reasons.size() >= 2 && reasons[0] == "reboot" &&
611 (reasons[1] == "recovery" || reasons[1] == "bootloader" || reasons[1] == "cold" ||
612 reasons[1] == "hard" || reasons[1] == "warm")) {
613 skip = strlen("reboot,");
614 }
615 PersistRebootReason(reason.c_str() + skip, true);
616
617 // If /data isn't mounted then we can skip the extra reboot steps below, since we don't need to
618 // worry about unmounting it.
619 if (!IsDataMounted()) {
620 sync();
621 RebootSystem(cmd, reboot_target);
622 abort();
623 }
624
Keun-young Park7830d592017-03-27 16:07:02 -0700625 // watchdogd is a vendor specific component but should be alive to complete shutdown safely.
Keun-young Parkcccb34f2017-07-05 11:38:44 -0700626 const std::set<std::string> to_starts{"watchdogd"};
Nikita Ioffe7ba50302020-11-27 18:57:44 +0000627 std::set<std::string> stop_first;
Tom Cherry911b9b12017-07-27 16:20:58 -0700628 for (const auto& s : ServiceList::GetInstance()) {
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +0100629 if (kDebuggingServices.count(s->name())) {
630 // keep debugging tools until non critical ones are all gone.
Keun-young Park7830d592017-03-27 16:07:02 -0700631 s->SetShutdownCritical();
632 } else if (to_starts.count(s->name())) {
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900633 if (auto result = s->Start(); !result.ok()) {
Tom Cherry702ca9a2017-08-25 10:36:52 -0700634 LOG(ERROR) << "Could not start shutdown 'to_start' service '" << s->name()
635 << "': " << result.error();
636 }
Keun-young Park7830d592017-03-27 16:07:02 -0700637 s->SetShutdownCritical();
Keun-young Parkcccb34f2017-07-05 11:38:44 -0700638 } else if (s->IsShutdownCritical()) {
Tom Cherry702ca9a2017-08-25 10:36:52 -0700639 // Start shutdown critical service if not started.
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900640 if (auto result = s->Start(); !result.ok()) {
Tom Cherry702ca9a2017-08-25 10:36:52 -0700641 LOG(ERROR) << "Could not start shutdown critical service '" << s->name()
642 << "': " << result.error();
643 }
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +0100644 } else {
Nikita Ioffe7ba50302020-11-27 18:57:44 +0000645 stop_first.insert(s->name());
Keun-young Park8d01f632017-03-13 11:54:47 -0700646 }
Tom Cherry911b9b12017-07-27 16:20:58 -0700647 }
Keun-young Park7830d592017-03-27 16:07:02 -0700648
Steven Morelandd5eccfd2018-01-19 13:01:53 -0800649 // remaining operations (specifically fsck) may take a substantial duration
Tom Cherry0a72e6c2018-03-19 16:19:01 -0700650 if (cmd == ANDROID_RB_POWEROFF || is_thermal_shutdown) {
Steven Morelandd5eccfd2018-01-19 13:01:53 -0800651 TurnOffBacklight();
652 }
653
Nikita Ioffee7ec8c82019-10-24 23:18:39 +0100654 Service* boot_anim = ServiceList::GetInstance().FindService("bootanim");
655 Service* surface_flinger = ServiceList::GetInstance().FindService("surfaceflinger");
656 if (boot_anim != nullptr && surface_flinger != nullptr && surface_flinger->IsRunning()) {
Tom Cherry4f4cacc2019-01-08 10:39:00 -0800657 bool do_shutdown_animation = GetBoolProperty("ro.init.shutdown_animation", false);
658
659 if (do_shutdown_animation) {
Tom Cherryc88d8f92019-08-19 15:21:25 -0700660 SetProperty("service.bootanim.exit", "0");
Nicolas Geoffraye106f0a2020-12-15 18:34:47 +0000661 SetProperty("service.bootanim.progress", "0");
Tom Cherry4f4cacc2019-01-08 10:39:00 -0800662 // Could be in the middle of animation. Stop and start so that it can pick
663 // up the right mode.
Nikita Ioffee7ec8c82019-10-24 23:18:39 +0100664 boot_anim->Stop();
Tom Cherry4f4cacc2019-01-08 10:39:00 -0800665 }
666
Tom Cherry911b9b12017-07-27 16:20:58 -0700667 for (const auto& service : ServiceList::GetInstance()) {
Tom Cherry4f4cacc2019-01-08 10:39:00 -0800668 if (service->classnames().count("animation") == 0) {
669 continue;
670 }
671
672 // start all animation classes if stopped.
673 if (do_shutdown_animation) {
Tom Cherry9949ec52019-05-16 16:54:49 -0700674 service->Start();
Tom Cherry4f4cacc2019-01-08 10:39:00 -0800675 }
676 service->SetShutdownCritical(); // will not check animation class separately
677 }
678
679 if (do_shutdown_animation) {
Nikita Ioffee7ec8c82019-10-24 23:18:39 +0100680 boot_anim->Start();
681 surface_flinger->SetShutdownCritical();
682 boot_anim->SetShutdownCritical();
Tom Cherry911b9b12017-07-27 16:20:58 -0700683 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700684 }
Keun-young Park7830d592017-03-27 16:07:02 -0700685
Keun-young Park8d01f632017-03-13 11:54:47 -0700686 // optional shutdown step
687 // 1. terminate all services except shutdown critical ones. wait for delay to finish
Keun-young Park30173872017-07-18 10:58:28 -0700688 if (shutdown_timeout > 0ms) {
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +0100689 StopServicesAndLogViolations(stop_first, shutdown_timeout / 2, true /* SIGTERM */);
Keun-young Park8d01f632017-03-13 11:54:47 -0700690 }
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +0100691 // Send SIGKILL to ones that didn't terminate cleanly.
692 StopServicesAndLogViolations(stop_first, 0ms, false /* SIGKILL */);
Luis Hector Chavez92c49bc2018-07-27 11:19:25 -0700693 SubcontextTerminate();
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +0100694 // Reap subcontext pids.
Tom Cherryeeee8312017-07-28 15:22:23 -0700695 ReapAnyOutstandingChildren();
Keun-young Park8d01f632017-03-13 11:54:47 -0700696
Martijn Coenen860ba642020-05-28 16:42:10 +0200697 // 3. send volume abort_fuse and volume shutdown to vold
Nikita Ioffee7ec8c82019-10-24 23:18:39 +0100698 Service* vold_service = ServiceList::GetInstance().FindService("vold");
699 if (vold_service != nullptr && vold_service->IsRunning()) {
Martijn Coenen860ba642020-05-28 16:42:10 +0200700 // Manually abort FUSE connections, since the FUSE daemon is already dead
701 // at this point, and unmounting it might hang.
702 CallVdc("volume", "abort_fuse");
Nikita Ioffebee7b8c2019-12-02 11:51:39 +0000703 CallVdc("volume", "shutdown");
Nikita Ioffee7ec8c82019-10-24 23:18:39 +0100704 vold_service->Stop();
Keun-young Park8d01f632017-03-13 11:54:47 -0700705 } else {
706 LOG(INFO) << "vold not running, skipping vold shutdown";
707 }
Keun-young Park2ba5c812017-03-29 12:54:40 -0700708 // logcat stopped here
Nikita Ioffe7ba50302020-11-27 18:57:44 +0000709 StopServices(kDebuggingServices, 0ms, false /* SIGKILL */);
Keun-young Park8d01f632017-03-13 11:54:47 -0700710 // 4. sync, try umount, and optionally run fsck for user shutdown
Tom Cherry1f9d5402018-03-15 10:22:40 -0700711 {
712 Timer sync_timer;
713 LOG(INFO) << "sync() before umount...";
714 sync();
715 LOG(INFO) << "sync() before umount took" << sync_timer;
716 }
Jaegeuk Kim2aedc822018-11-20 13:27:06 -0800717 // 5. drop caches and disable zram backing device, if exist
718 KillZramBackingDevice();
719
Nikita Ioffe91a98482020-06-10 00:53:59 +0100720 LOG(INFO) << "Ready to unmount apexes. So far shutdown sequence took " << t;
721 // 6. unmount active apexes, otherwise they might prevent clean unmount of /data.
722 if (auto ret = UnmountAllApexes(); !ret.ok()) {
723 LOG(ERROR) << ret.error();
724 }
Nikita Ioffee7ec8c82019-10-24 23:18:39 +0100725 UmountStat stat =
726 TryUmountAndFsck(cmd, run_fsck, shutdown_timeout - t.duration(), &reboot_semaphore);
Keun-young Park2ba5c812017-03-29 12:54:40 -0700727 // Follow what linux shutdown is doing: one more sync with little bit delay
Tom Cherry1f9d5402018-03-15 10:22:40 -0700728 {
729 Timer sync_timer;
730 LOG(INFO) << "sync() after umount...";
731 sync();
732 LOG(INFO) << "sync() after umount took" << sync_timer;
733 }
Tom Cherry0a72e6c2018-03-19 16:19:01 -0700734 if (!is_thermal_shutdown) std::this_thread::sleep_for(100ms);
Keun-young Park8d01f632017-03-13 11:54:47 -0700735 LogShutdownTime(stat, &t);
josephjangaaddf282019-04-16 18:46:24 +0800736
737 // Send signal to terminate reboot monitor thread.
738 reboot_monitor_run = false;
739 sem_post(&reboot_semaphore);
740
Keun-young Park8d01f632017-03-13 11:54:47 -0700741 // Reboot regardless of umount status. If umount fails, fsck after reboot will fix it.
Nikita Ioffee7ec8c82019-10-24 23:18:39 +0100742 RebootSystem(cmd, reboot_target);
Keun-young Park8d01f632017-03-13 11:54:47 -0700743 abort();
744}
Tom Cherry98ad32a2017-04-17 16:34:20 -0700745
Nikita Ioffeba6968e2019-10-07 16:26:33 +0100746static void EnterShutdown() {
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +0100747 LOG(INFO) << "Entering shutdown mode";
Nikita Ioffeba6968e2019-10-07 16:26:33 +0100748 shutting_down = true;
749 // Skip wait for prop if it is in progress
750 ResetWaitForProp();
751 // Clear EXEC flag if there is one pending
752 for (const auto& s : ServiceList::GetInstance()) {
753 s->UnSetExec();
754 }
Nikita Ioffeba6968e2019-10-07 16:26:33 +0100755}
756
757static void LeaveShutdown() {
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +0100758 LOG(INFO) << "Leaving shutdown mode";
Nikita Ioffeba6968e2019-10-07 16:26:33 +0100759 shutting_down = false;
Tom Cherry802864c2020-03-12 14:29:25 -0700760 StartSendingMessages();
Nikita Ioffeba6968e2019-10-07 16:26:33 +0100761}
762
Nikita Ioffe7b41a152020-03-25 00:06:51 +0000763static std::chrono::milliseconds GetMillisProperty(const std::string& name,
764 std::chrono::milliseconds default_value) {
765 auto value = GetUintProperty(name, static_cast<uint64_t>(default_value.count()));
766 return std::chrono::milliseconds(std::move(value));
767}
768
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +0100769static Result<void> DoUserspaceReboot() {
770 LOG(INFO) << "Userspace reboot initiated";
Nikita Ioffe39d45532020-04-30 22:37:15 +0100771 // An ugly way to pass a more precise reason on why fallback to hard reboot was triggered.
772 std::string sub_reason = "";
773 auto guard = android::base::make_scope_guard([&sub_reason] {
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +0100774 // Leave shutdown so that we can handle a full reboot.
775 LeaveShutdown();
Nikita Ioffe39d45532020-04-30 22:37:15 +0100776 trigger_shutdown("reboot,userspace_failed,shutdown_aborted," + sub_reason);
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +0100777 });
Nikita Ioffec0df1872019-11-13 21:47:06 +0000778 // Triggering userspace-reboot-requested will result in a bunch of setprop
Nikita Ioffeba6968e2019-10-07 16:26:33 +0100779 // actions. We should make sure, that all of them are propagated before
Nikita Ioffe764c1ac2020-01-27 17:14:46 +0000780 // proceeding with userspace reboot. Synchronously setting sys.init.userspace_reboot.in_progress
781 // property is not perfect, but it should do the trick.
Nikita Ioffe23dbd6d2019-11-14 01:21:24 +0000782 if (!android::sysprop::InitProperties::userspace_reboot_in_progress(true)) {
Nikita Ioffe39d45532020-04-30 22:37:15 +0100783 sub_reason = "setprop";
Nikita Ioffe23dbd6d2019-11-14 01:21:24 +0000784 return Error() << "Failed to set sys.init.userspace_reboot.in_progress property";
Nikita Ioffec0df1872019-11-13 21:47:06 +0000785 }
Nikita Ioffeba6968e2019-10-07 16:26:33 +0100786 EnterShutdown();
Nikita Ioffe764c1ac2020-01-27 17:14:46 +0000787 if (!SetProperty("sys.powerctl", "")) {
Nikita Ioffe39d45532020-04-30 22:37:15 +0100788 sub_reason = "resetprop";
Nikita Ioffe764c1ac2020-01-27 17:14:46 +0000789 return Error() << "Failed to reset sys.powerctl property";
790 }
Nikita Ioffe7ba50302020-11-27 18:57:44 +0000791 std::set<std::string> stop_first;
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +0100792 // Remember the services that were enabled. We will need to manually enable them again otherwise
793 // triggers like class_start won't restart them.
Nikita Ioffe7ba50302020-11-27 18:57:44 +0000794 std::set<std::string> were_enabled;
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +0100795 for (const auto& s : ServiceList::GetInstance().services_in_shutdown_order()) {
796 if (s->is_post_data() && !kDebuggingServices.count(s->name())) {
Nikita Ioffe7ba50302020-11-27 18:57:44 +0000797 stop_first.insert(s->name());
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +0100798 }
Nikita Ioffe7ba50302020-11-27 18:57:44 +0000799 // TODO(ioffe): we should also filter out temporary services here.
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +0100800 if (s->is_post_data() && s->IsEnabled()) {
Nikita Ioffe7ba50302020-11-27 18:57:44 +0000801 were_enabled.insert(s->name());
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +0100802 }
803 }
Nikita Ioffe82a431e2019-11-07 15:37:38 +0000804 {
805 Timer sync_timer;
806 LOG(INFO) << "sync() before terminating services...";
807 sync();
808 LOG(INFO) << "sync() took " << sync_timer;
809 }
Nikita Ioffe7b41a152020-03-25 00:06:51 +0000810 auto sigterm_timeout = GetMillisProperty("init.userspace_reboot.sigterm.timeoutmillis", 5s);
811 auto sigkill_timeout = GetMillisProperty("init.userspace_reboot.sigkill.timeoutmillis", 10s);
Nikita Ioffea3be9962020-04-16 21:37:03 +0100812 LOG(INFO) << "Timeout to terminate services: " << sigterm_timeout.count() << "ms "
Nikita Ioffe7b41a152020-03-25 00:06:51 +0000813 << "Timeout to kill services: " << sigkill_timeout.count() << "ms";
Gavin Corkery8c922562020-05-11 14:10:09 +0100814 std::string services_file_name = "/metadata/userspacereboot/services.txt";
815 const int flags = O_RDWR | O_CREAT | O_SYNC | O_APPEND | O_CLOEXEC;
Nikita Ioffe7b41a152020-03-25 00:06:51 +0000816 StopServicesAndLogViolations(stop_first, sigterm_timeout, true /* SIGTERM */);
817 if (int r = StopServicesAndLogViolations(stop_first, sigkill_timeout, false /* SIGKILL */);
818 r > 0) {
Gavin Corkery8c922562020-05-11 14:10:09 +0100819 auto fd = unique_fd(TEMP_FAILURE_RETRY(open(services_file_name.c_str(), flags, 0666)));
820 android::base::WriteStringToFd("Post-data services still running: \n", fd);
Nikita Ioffe7ba50302020-11-27 18:57:44 +0000821 for (const auto& s : ServiceList::GetInstance()) {
822 if (s->IsRunning() && stop_first.count(s->name())) {
Gavin Corkery8c922562020-05-11 14:10:09 +0100823 android::base::WriteStringToFd(s->name() + "\n", fd);
824 }
825 }
Nikita Ioffe39d45532020-04-30 22:37:15 +0100826 sub_reason = "sigkill";
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +0100827 return Error() << r << " post-data services are still running";
828 }
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900829 if (auto result = KillZramBackingDevice(); !result.ok()) {
Nikita Ioffe39d45532020-04-30 22:37:15 +0100830 sub_reason = "zram";
Nikita Ioffebee7b8c2019-12-02 11:51:39 +0000831 return result;
832 }
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900833 if (auto result = CallVdc("volume", "reset"); !result.ok()) {
Nikita Ioffe39d45532020-04-30 22:37:15 +0100834 sub_reason = "vold_reset";
Nikita Ioffebee7b8c2019-12-02 11:51:39 +0000835 return result;
836 }
Nikita Ioffe7ba50302020-11-27 18:57:44 +0000837 const auto& debugging_services = GetPostDataDebuggingServices();
838 if (int r = StopServicesAndLogViolations(debugging_services, sigkill_timeout,
839 false /* SIGKILL */);
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +0100840 r > 0) {
Gavin Corkery8c922562020-05-11 14:10:09 +0100841 auto fd = unique_fd(TEMP_FAILURE_RETRY(open(services_file_name.c_str(), flags, 0666)));
842 android::base::WriteStringToFd("Debugging services still running: \n", fd);
Nikita Ioffe7ba50302020-11-27 18:57:44 +0000843 for (const auto& s : ServiceList::GetInstance()) {
844 if (s->IsRunning() && debugging_services.count(s->name())) {
Gavin Corkery8c922562020-05-11 14:10:09 +0100845 android::base::WriteStringToFd(s->name() + "\n", fd);
846 }
847 }
Nikita Ioffe39d45532020-04-30 22:37:15 +0100848 sub_reason = "sigkill_debug";
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +0100849 return Error() << r << " debugging services are still running";
850 }
Nikita Ioffe82a431e2019-11-07 15:37:38 +0000851 {
852 Timer sync_timer;
853 LOG(INFO) << "sync() after stopping services...";
854 sync();
855 LOG(INFO) << "sync() took " << sync_timer;
856 }
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900857 if (auto result = UnmountAllApexes(); !result.ok()) {
Nikita Ioffe39d45532020-04-30 22:37:15 +0100858 sub_reason = "apex";
Nikita Ioffeab91ee92019-11-06 21:40:31 +0000859 return result;
860 }
Bernie Innocenti062ef532020-02-07 23:20:54 +0900861 if (!SwitchToMountNamespaceIfNeeded(NS_BOOTSTRAP).ok()) {
Nikita Ioffe39d45532020-04-30 22:37:15 +0100862 sub_reason = "ns_switch";
Nikita Ioffeab91ee92019-11-06 21:40:31 +0000863 return Error() << "Failed to switch to bootstrap namespace";
864 }
Nikita Ioffe091c4d12019-12-05 12:35:19 +0000865 // Remove services that were defined in an APEX.
866 ServiceList::GetInstance().RemoveServiceIf([](const std::unique_ptr<Service>& s) -> bool {
867 if (s->is_from_apex()) {
868 LOG(INFO) << "Removing service '" << s->name() << "' because it's defined in an APEX";
869 return true;
870 }
871 return false;
872 });
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +0100873 // Re-enable services
Nikita Ioffe7ba50302020-11-27 18:57:44 +0000874 for (const auto& s : ServiceList::GetInstance()) {
875 if (were_enabled.count(s->name())) {
876 LOG(INFO) << "Re-enabling service '" << s->name() << "'";
877 s->Enable();
878 }
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +0100879 }
Nikita Ioffe3ad29202020-02-27 20:46:27 +0000880 ServiceList::GetInstance().ResetState();
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +0100881 LeaveShutdown();
Nikita Ioffeba6968e2019-10-07 16:26:33 +0100882 ActionManager::GetInstance().QueueEventTrigger("userspace-reboot-resume");
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +0100883 guard.Disable(); // Go on with userspace reboot.
884 return {};
Nikita Ioffeba6968e2019-10-07 16:26:33 +0100885}
886
Nikita Ioffe82a431e2019-11-07 15:37:38 +0000887static void UserspaceRebootWatchdogThread() {
Nikita Ioffed0553542020-04-11 01:55:24 +0100888 auto started_timeout = GetMillisProperty("init.userspace_reboot.started.timeoutmillis", 10s);
889 if (!WaitForProperty("sys.init.userspace_reboot.in_progress", "1", started_timeout)) {
890 LOG(ERROR) << "Userspace reboot didn't start in " << started_timeout.count()
891 << "ms. Switching to full reboot";
892 // Init might be wedged, don't try to write reboot reason into a persistent property and do
893 // a dirty reboot.
894 PersistRebootReason("userspace_failed,watchdog_triggered,failed_to_start", false);
895 RebootSystem(ANDROID_RB_RESTART2, "userspace_failed,watchdog_triggered,failed_to_start");
Nikita Ioffe82a431e2019-11-07 15:37:38 +0000896 }
897 LOG(INFO) << "Starting userspace reboot watchdog";
Nikita Ioffed0553542020-04-11 01:55:24 +0100898 auto watchdog_timeout = GetMillisProperty("init.userspace_reboot.watchdog.timeoutmillis", 5min);
899 LOG(INFO) << "UserspaceRebootWatchdog timeout: " << watchdog_timeout.count() << "ms";
900 if (!WaitForProperty("sys.boot_completed", "1", watchdog_timeout)) {
901 LOG(ERROR) << "Failed to boot in " << watchdog_timeout.count()
902 << "ms. Switching to full reboot";
Nikita Ioffe82a431e2019-11-07 15:37:38 +0000903 // In this case device is in a boot loop. Only way to recover is to do dirty reboot.
Nikita Ioffed485bbb2020-02-26 15:18:56 +0000904 // Since init might be wedged, don't try to write reboot reason into a persistent property.
Nikita Ioffed0553542020-04-11 01:55:24 +0100905 PersistRebootReason("userspace_failed,watchdog_triggered,failed_to_boot", false);
906 RebootSystem(ANDROID_RB_RESTART2, "userspace_failed,watchdog_triggered,failed_to_boot");
Nikita Ioffe82a431e2019-11-07 15:37:38 +0000907 }
908 LOG(INFO) << "Device booted, stopping userspace reboot watchdog";
909}
910
Nikita Ioffeba6968e2019-10-07 16:26:33 +0100911static void HandleUserspaceReboot() {
Nikita Ioffe018ddd72019-12-20 16:34:48 +0000912 if (!android::sysprop::InitProperties::is_userspace_reboot_supported().value_or(false)) {
Nikita Ioffe9f473c02019-12-18 13:31:42 +0000913 LOG(ERROR) << "Attempted a userspace reboot on a device that doesn't support it";
914 return;
915 }
Nikita Ioffe82a431e2019-11-07 15:37:38 +0000916 // Spinnig up a separate thread will fail the setns call later in the boot sequence.
917 // Fork a new process to monitor userspace reboot while we are investigating a better solution.
918 pid_t pid = fork();
919 if (pid < 0) {
920 PLOG(ERROR) << "Failed to fork process for userspace reboot watchdog. Switching to full "
921 << "reboot";
Nikita Ioffe4a787d92020-01-15 23:23:13 +0000922 trigger_shutdown("reboot,userspace_failed,watchdog_fork");
Nikita Ioffe82a431e2019-11-07 15:37:38 +0000923 return;
924 }
925 if (pid == 0) {
926 // Child
927 UserspaceRebootWatchdogThread();
928 _exit(EXIT_SUCCESS);
929 }
Nikita Ioffeba6968e2019-10-07 16:26:33 +0100930 LOG(INFO) << "Clearing queue and starting userspace-reboot-requested trigger";
931 auto& am = ActionManager::GetInstance();
932 am.ClearQueue();
933 am.QueueEventTrigger("userspace-reboot-requested");
Nikita Ioffe3f4b0d62019-10-09 15:23:02 +0100934 auto handler = [](const BuiltinArguments&) { return DoUserspaceReboot(); };
Nikita Ioffeba6968e2019-10-07 16:26:33 +0100935 am.QueueBuiltinAction(handler, "userspace-reboot");
936}
937
Sam Protsenko2c7c3c72019-12-26 21:55:08 +0200938/**
939 * Check if "command" field is set in bootloader message.
940 *
941 * If "command" field is broken (contains non-printable characters prior to
942 * terminating zero), it will be zeroed.
943 *
944 * @param[in,out] boot Bootloader message (BCB) structure
945 * @return true if "command" field is already set, and false if it's empty
946 */
947static bool CommandIsPresent(bootloader_message* boot) {
948 if (boot->command[0] == '\0')
949 return false;
950
951 for (size_t i = 0; i < arraysize(boot->command); ++i) {
952 if (boot->command[i] == '\0')
953 return true;
954 if (!isprint(boot->command[i]))
955 break;
956 }
957
958 memset(boot->command, 0, sizeof(boot->command));
959 return false;
960}
961
Nikita Ioffeba6968e2019-10-07 16:26:33 +0100962void HandlePowerctlMessage(const std::string& command) {
Tom Cherry98ad32a2017-04-17 16:34:20 -0700963 unsigned int cmd = 0;
Mark Salyzyn62909822017-10-09 09:27:16 -0700964 std::vector<std::string> cmd_params = Split(command, ",");
Tom Cherry98ad32a2017-04-17 16:34:20 -0700965 std::string reboot_target = "";
966 bool run_fsck = false;
967 bool command_invalid = false;
Nikita Ioffeba6968e2019-10-07 16:26:33 +0100968 bool userspace_reboot = false;
Tom Cherry98ad32a2017-04-17 16:34:20 -0700969
Mark Salyzynd7931f12019-07-10 10:33:09 -0700970 if (cmd_params[0] == "shutdown") {
Tom Cherry98ad32a2017-04-17 16:34:20 -0700971 cmd = ANDROID_RB_POWEROFF;
Mark Salyzynd7931f12019-07-10 10:33:09 -0700972 if (cmd_params.size() >= 2) {
Mark Salyzyn73e6b492017-08-14 15:56:53 -0700973 if (cmd_params[1] == "userrequested") {
974 // The shutdown reason is PowerManager.SHUTDOWN_USER_REQUESTED.
975 // Run fsck once the file system is remounted in read-only mode.
976 run_fsck = true;
977 } else if (cmd_params[1] == "thermal") {
Mark Salyzynbfd05b62017-09-26 11:10:12 -0700978 // Turn off sources of heat immediately.
979 TurnOffBacklight();
Mark Salyzyn73e6b492017-08-14 15:56:53 -0700980 // run_fsck is false to avoid delay
981 cmd = ANDROID_RB_THERMOFF;
982 }
Tom Cherry98ad32a2017-04-17 16:34:20 -0700983 }
984 } else if (cmd_params[0] == "reboot") {
985 cmd = ANDROID_RB_RESTART2;
986 if (cmd_params.size() >= 2) {
987 reboot_target = cmd_params[1];
Nikita Ioffeba6968e2019-10-07 16:26:33 +0100988 if (reboot_target == "userspace") {
989 LOG(INFO) << "Userspace reboot requested";
990 userspace_reboot = true;
991 }
Hridya Valsaraju54258262018-09-19 21:14:18 -0700992 // adb reboot fastboot should boot into bootloader for devices not
993 // supporting logical partitions.
994 if (reboot_target == "fastboot" &&
Yifan Hong0e0f8182018-11-16 12:49:06 -0800995 !android::base::GetBoolProperty("ro.boot.dynamic_partitions", false)) {
Hridya Valsaraju54258262018-09-19 21:14:18 -0700996 reboot_target = "bootloader";
997 }
Tom Cherry98ad32a2017-04-17 16:34:20 -0700998 // When rebooting to the bootloader notify the bootloader writing
999 // also the BCB.
1000 if (reboot_target == "bootloader") {
1001 std::string err;
1002 if (!write_reboot_bootloader(&err)) {
1003 LOG(ERROR) << "reboot-bootloader: Error writing "
1004 "bootloader_message: "
1005 << err;
1006 }
Tianjie Xu5e98b632019-07-18 12:48:28 -07001007 } else if (reboot_target == "recovery") {
1008 bootloader_message boot = {};
1009 if (std::string err; !read_bootloader_message(&boot, &err)) {
1010 LOG(ERROR) << "Failed to read bootloader message: " << err;
1011 }
1012 // Update the boot command field if it's empty, and preserve
1013 // the other arguments in the bootloader message.
Sam Protsenko2c7c3c72019-12-26 21:55:08 +02001014 if (!CommandIsPresent(&boot)) {
Tianjie Xu5e98b632019-07-18 12:48:28 -07001015 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
1016 if (std::string err; !write_bootloader_message(boot, &err)) {
1017 LOG(ERROR) << "Failed to set bootloader message: " << err;
Nikita Ioffeba6968e2019-10-07 16:26:33 +01001018 return;
Tianjie Xu5e98b632019-07-18 12:48:28 -07001019 }
1020 }
Hridya Valsaraju71fb82a2018-08-02 15:02:06 -07001021 } else if (reboot_target == "sideload" || reboot_target == "sideload-auto-reboot" ||
1022 reboot_target == "fastboot") {
1023 std::string arg = reboot_target == "sideload-auto-reboot" ? "sideload_auto_reboot"
1024 : reboot_target;
1025 const std::vector<std::string> options = {
1026 "--" + arg,
1027 };
1028 std::string err;
1029 if (!write_bootloader_message(options, &err)) {
1030 LOG(ERROR) << "Failed to set bootloader message: " << err;
Nikita Ioffeba6968e2019-10-07 16:26:33 +01001031 return;
Hridya Valsaraju71fb82a2018-08-02 15:02:06 -07001032 }
1033 reboot_target = "recovery";
Tom Cherry98ad32a2017-04-17 16:34:20 -07001034 }
Hridya Valsaraju71fb82a2018-08-02 15:02:06 -07001035
Mark Salyzynd7931f12019-07-10 10:33:09 -07001036 // If there are additional parameter, pass them along
1037 for (size_t i = 2; (cmd_params.size() > i) && cmd_params[i].size(); ++i) {
1038 reboot_target += "," + cmd_params[i];
Tom Cherry98ad32a2017-04-17 16:34:20 -07001039 }
1040 }
Tom Cherry98ad32a2017-04-17 16:34:20 -07001041 } else {
1042 command_invalid = true;
1043 }
1044 if (command_invalid) {
1045 LOG(ERROR) << "powerctl: unrecognized command '" << command << "'";
Nikita Ioffeba6968e2019-10-07 16:26:33 +01001046 return;
1047 }
1048
Tom Cherry802864c2020-03-12 14:29:25 -07001049 // We do not want to process any messages (queue'ing triggers, shutdown messages, control
1050 // messages, etc) from properties during reboot.
1051 StopSendingMessages();
1052
Nikita Ioffeba6968e2019-10-07 16:26:33 +01001053 if (userspace_reboot) {
1054 HandleUserspaceReboot();
1055 return;
Tom Cherry98ad32a2017-04-17 16:34:20 -07001056 }
1057
Wei Wangeeab4912017-06-27 22:08:45 -07001058 LOG(INFO) << "Clear action queue and start shutdown trigger";
1059 ActionManager::GetInstance().ClearQueue();
1060 // Queue shutdown trigger first
1061 ActionManager::GetInstance().QueueEventTrigger("shutdown");
1062 // Queue built-in shutdown_done
Tom Cherrycb0f9bb2017-09-12 15:58:47 -07001063 auto shutdown_handler = [cmd, command, reboot_target, run_fsck](const BuiltinArguments&) {
Wei Wangeeab4912017-06-27 22:08:45 -07001064 DoReboot(cmd, command, reboot_target, run_fsck);
Tom Cherrybbcbc2f2019-06-10 11:08:01 -07001065 return Result<void>{};
Wei Wangeeab4912017-06-27 22:08:45 -07001066 };
1067 ActionManager::GetInstance().QueueBuiltinAction(shutdown_handler, "shutdown_done");
1068
Nikita Ioffeba6968e2019-10-07 16:26:33 +01001069 EnterShutdown();
1070}
Wei Wangeeab4912017-06-27 22:08:45 -07001071
Nikita Ioffeba6968e2019-10-07 16:26:33 +01001072bool IsShuttingDown() {
1073 return shutting_down;
Tom Cherry98ad32a2017-04-17 16:34:20 -07001074}
Tom Cherry81f5d3e2017-06-22 12:53:17 -07001075
1076} // namespace init
1077} // namespace android