blob: 41965a19ac5aec2072e7739322e5ada70a534395 [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>
Keun-young Park8d01f632017-03-13 11:54:47 -070025#include <sys/cdefs.h>
Keun-young Park2ba5c812017-03-29 12:54:40 -070026#include <sys/ioctl.h>
Keun-young Park8d01f632017-03-13 11:54:47 -070027#include <sys/mount.h>
Keun-young Park8d01f632017-03-13 11:54:47 -070028#include <sys/stat.h>
josephjangaaddf282019-04-16 18:46:24 +080029#include <sys/swap.h>
Keun-young Park8d01f632017-03-13 11:54:47 -070030#include <sys/syscall.h>
31#include <sys/types.h>
32#include <sys/wait.h>
33
34#include <memory>
Keun-young Park7830d592017-03-27 16:07:02 -070035#include <set>
Keun-young Park8d01f632017-03-13 11:54:47 -070036#include <thread>
37#include <vector>
38
Tom Cherryede0d532017-07-06 14:20:11 -070039#include <android-base/chrono_utils.h>
Keun-young Park8d01f632017-03-13 11:54:47 -070040#include <android-base/file.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070041#include <android-base/logging.h>
Keun-young Park8d01f632017-03-13 11:54:47 -070042#include <android-base/macros.h>
Tom Cherryccf23532017-03-28 16:40:41 -070043#include <android-base/properties.h>
Keun-young Park8d01f632017-03-13 11:54:47 -070044#include <android-base/strings.h>
Keun-young Park2ba5c812017-03-29 12:54:40 -070045#include <android-base/unique_fd.h>
Keun-young Park8d01f632017-03-13 11:54:47 -070046#include <bootloader_message/bootloader_message.h>
47#include <cutils/android_reboot.h>
Keun-young Park8d01f632017-03-13 11:54:47 -070048#include <fs_mgr.h>
49#include <logwrap/logwrap.h>
Todd Poynorfc827be2017-04-13 15:17:24 -070050#include <private/android_filesystem_config.h>
Tom Cherry0c8d6d22017-08-10 12:22:44 -070051#include <selinux/selinux.h>
Keun-young Park8d01f632017-03-13 11:54:47 -070052
Nikita Ioffeba6968e2019-10-07 16:26:33 +010053#include "action.h"
Tom Cherry7fd3bc22018-02-13 15:36:14 -080054#include "action_manager.h"
Nikita Ioffeba6968e2019-10-07 16:26:33 +010055#include "builtin_arguments.h"
Wei Wangeeab4912017-06-27 22:08:45 -070056#include "init.h"
Keun-young Park7830d592017-03-27 16:07:02 -070057#include "property_service.h"
Tom Cherry44aceed2018-08-03 13:36:18 -070058#include "reboot_utils.h"
Keun-young Park8d01f632017-03-13 11:54:47 -070059#include "service.h"
Tom Cherry2aeb1ad2019-06-26 10:46:20 -070060#include "service_list.h"
Luis Hector Chavez9f97f472017-09-06 13:43:57 -070061#include "sigchld_handler.h"
Keun-young Park8d01f632017-03-13 11:54:47 -070062
josephjangaaddf282019-04-16 18:46:24 +080063#define PROC_SYSRQ "/proc/sysrq-trigger"
64
Tom Cherry2436e6b2019-09-19 11:16:19 -070065using namespace std::literals;
66
Tom Cherry4f4cacc2019-01-08 10:39:00 -080067using android::base::GetBoolProperty;
Mark Salyzyn62909822017-10-09 09:27:16 -070068using android::base::Split;
Tom Cherryede0d532017-07-06 14:20:11 -070069using android::base::Timer;
Jaegeuk Kim2aedc822018-11-20 13:27:06 -080070using android::base::unique_fd;
josephjangaaddf282019-04-16 18:46:24 +080071using android::base::WriteStringToFile;
Keun-young Park8d01f632017-03-13 11:54:47 -070072
Tom Cherry81f5d3e2017-06-22 12:53:17 -070073namespace android {
74namespace init {
75
Nikita Ioffeba6968e2019-10-07 16:26:33 +010076static bool shutting_down = false;
77
Keun-young Park8d01f632017-03-13 11:54:47 -070078// represents umount status during reboot / shutdown.
79enum UmountStat {
80 /* umount succeeded. */
81 UMOUNT_STAT_SUCCESS = 0,
82 /* umount was not run. */
83 UMOUNT_STAT_SKIPPED = 1,
84 /* umount failed with timeout. */
85 UMOUNT_STAT_TIMEOUT = 2,
86 /* could not run due to error */
87 UMOUNT_STAT_ERROR = 3,
88 /* not used by init but reserved for other part to use this to represent the
89 the state where umount status before reboot is not found / available. */
90 UMOUNT_STAT_NOT_AVAILABLE = 4,
91};
92
93// Utility for struct mntent
94class MountEntry {
95 public:
Keun-young Park2ba5c812017-03-29 12:54:40 -070096 explicit MountEntry(const mntent& entry)
Keun-young Park8d01f632017-03-13 11:54:47 -070097 : mnt_fsname_(entry.mnt_fsname),
98 mnt_dir_(entry.mnt_dir),
99 mnt_type_(entry.mnt_type),
Keun-young Park2ba5c812017-03-29 12:54:40 -0700100 mnt_opts_(entry.mnt_opts) {}
Keun-young Park8d01f632017-03-13 11:54:47 -0700101
Jaegeuk Kim0f04f722017-09-25 10:55:39 -0700102 bool Umount(bool force) {
Tom Cherryc9fec9d2018-02-15 14:26:58 -0800103 LOG(INFO) << "Unmounting " << mnt_fsname_ << ":" << mnt_dir_ << " opts " << mnt_opts_;
Jaegeuk Kim0f04f722017-09-25 10:55:39 -0700104 int r = umount2(mnt_dir_.c_str(), force ? MNT_FORCE : 0);
Keun-young Park2ba5c812017-03-29 12:54:40 -0700105 if (r == 0) {
Tom Cherryc9fec9d2018-02-15 14:26:58 -0800106 LOG(INFO) << "Umounted " << mnt_fsname_ << ":" << mnt_dir_ << " opts " << mnt_opts_;
Keun-young Park2ba5c812017-03-29 12:54:40 -0700107 return true;
108 } else {
Tom Cherryc9fec9d2018-02-15 14:26:58 -0800109 PLOG(WARNING) << "Cannot umount " << mnt_fsname_ << ":" << mnt_dir_ << " opts "
Keun-young Park2ba5c812017-03-29 12:54:40 -0700110 << mnt_opts_;
111 return false;
112 }
113 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700114
Keun-young Park2ba5c812017-03-29 12:54:40 -0700115 void DoFsck() {
116 int st;
117 if (IsF2Fs()) {
118 const char* f2fs_argv[] = {
Randall Huangdf2faa42019-01-15 15:00:56 +0800119 "/system/bin/fsck.f2fs",
120 "-a",
121 mnt_fsname_.c_str(),
Keun-young Park2ba5c812017-03-29 12:54:40 -0700122 };
Tom Cherry3a803eb2019-09-25 16:23:50 -0700123 logwrap_fork_execvp(arraysize(f2fs_argv), f2fs_argv, &st, false, LOG_KLOG, true,
124 nullptr);
Keun-young Park2ba5c812017-03-29 12:54:40 -0700125 } else if (IsExt4()) {
126 const char* ext4_argv[] = {
Randall Huangdf2faa42019-01-15 15:00:56 +0800127 "/system/bin/e2fsck",
128 "-y",
129 mnt_fsname_.c_str(),
Keun-young Park2ba5c812017-03-29 12:54:40 -0700130 };
Tom Cherry3a803eb2019-09-25 16:23:50 -0700131 logwrap_fork_execvp(arraysize(ext4_argv), ext4_argv, &st, false, LOG_KLOG, true,
132 nullptr);
Keun-young Park2ba5c812017-03-29 12:54:40 -0700133 }
134 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700135
136 static bool IsBlockDevice(const struct mntent& mntent) {
137 return android::base::StartsWith(mntent.mnt_fsname, "/dev/block");
138 }
139
140 static bool IsEmulatedDevice(const struct mntent& mntent) {
Keun-young Park2ba5c812017-03-29 12:54:40 -0700141 return android::base::StartsWith(mntent.mnt_fsname, "/data/");
Keun-young Park8d01f632017-03-13 11:54:47 -0700142 }
143
144 private:
Keun-young Park2ba5c812017-03-29 12:54:40 -0700145 bool IsF2Fs() const { return mnt_type_ == "f2fs"; }
146
147 bool IsExt4() const { return mnt_type_ == "ext4"; }
148
Keun-young Park8d01f632017-03-13 11:54:47 -0700149 std::string mnt_fsname_;
150 std::string mnt_dir_;
151 std::string mnt_type_;
Keun-young Park2ba5c812017-03-29 12:54:40 -0700152 std::string mnt_opts_;
Keun-young Park8d01f632017-03-13 11:54:47 -0700153};
154
155// Turn off backlight while we are performing power down cleanup activities.
156static void TurnOffBacklight() {
Steven Morelandd5eccfd2018-01-19 13:01:53 -0800157 Service* service = ServiceList::GetInstance().FindService("blank_screen");
158 if (service == nullptr) {
159 LOG(WARNING) << "cannot find blank_screen in TurnOffBacklight";
Keun-young Park8d01f632017-03-13 11:54:47 -0700160 return;
161 }
Tom Cherryd9872642018-10-11 10:38:05 -0700162 if (auto result = service->Start(); !result) {
163 LOG(WARNING) << "Could not start blank_screen service: " << result.error();
164 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700165}
166
Keun-young Park8d01f632017-03-13 11:54:47 -0700167static void ShutdownVold() {
168 const char* vdc_argv[] = {"/system/bin/vdc", "volume", "shutdown"};
169 int status;
Tom Cherry3a803eb2019-09-25 16:23:50 -0700170 logwrap_fork_execvp(arraysize(vdc_argv), vdc_argv, &status, false, LOG_KLOG, true, nullptr);
Keun-young Park8d01f632017-03-13 11:54:47 -0700171}
172
173static void LogShutdownTime(UmountStat stat, Timer* t) {
Tom Cherryede0d532017-07-06 14:20:11 -0700174 LOG(WARNING) << "powerctl_shutdown_time_ms:" << std::to_string(t->duration().count()) << ":"
175 << stat;
Keun-young Park8d01f632017-03-13 11:54:47 -0700176}
177
Tom Cherry2436e6b2019-09-19 11:16:19 -0700178static bool IsDataMounted() {
179 std::unique_ptr<std::FILE, int (*)(std::FILE*)> fp(setmntent("/proc/mounts", "re"), endmntent);
180 if (fp == nullptr) {
181 PLOG(ERROR) << "Failed to open /proc/mounts";
182 return false;
183 }
184 mntent* mentry;
185 while ((mentry = getmntent(fp.get())) != nullptr) {
186 if (mentry->mnt_dir == "/data"s) {
187 return true;
188 }
189 }
190 return false;
191}
192
193// Find all read+write block devices and emulated devices in /proc/mounts and add them to
194// the correpsponding list.
Keun-young Park8d01f632017-03-13 11:54:47 -0700195static bool FindPartitionsToUmount(std::vector<MountEntry>* blockDevPartitions,
Keun-young Park2ba5c812017-03-29 12:54:40 -0700196 std::vector<MountEntry>* emulatedPartitions, bool dump) {
Tom Cherryf274e782018-10-03 13:13:41 -0700197 std::unique_ptr<std::FILE, int (*)(std::FILE*)> fp(setmntent("/proc/mounts", "re"), endmntent);
Keun-young Park8d01f632017-03-13 11:54:47 -0700198 if (fp == nullptr) {
199 PLOG(ERROR) << "Failed to open /proc/mounts";
200 return false;
201 }
202 mntent* mentry;
203 while ((mentry = getmntent(fp.get())) != nullptr) {
Keun-young Park2ba5c812017-03-29 12:54:40 -0700204 if (dump) {
205 LOG(INFO) << "mount entry " << mentry->mnt_fsname << ":" << mentry->mnt_dir << " opts "
206 << mentry->mnt_opts << " type " << mentry->mnt_type;
207 } else if (MountEntry::IsBlockDevice(*mentry) && hasmntopt(mentry, "rw")) {
Keun-young Park6e12b382017-07-17 12:20:33 -0700208 std::string mount_dir(mentry->mnt_dir);
209 // These are R/O partitions changed to R/W after adb remount.
210 // Do not umount them as shutdown critical services may rely on them.
Wei Wanga01c27e2017-07-25 10:52:08 -0700211 if (mount_dir != "/" && mount_dir != "/system" && mount_dir != "/vendor" &&
212 mount_dir != "/oem") {
Keun-young Park6e12b382017-07-17 12:20:33 -0700213 blockDevPartitions->emplace(blockDevPartitions->begin(), *mentry);
214 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700215 } else if (MountEntry::IsEmulatedDevice(*mentry)) {
Keun-young Park2ba5c812017-03-29 12:54:40 -0700216 emulatedPartitions->emplace(emulatedPartitions->begin(), *mentry);
Keun-young Park8d01f632017-03-13 11:54:47 -0700217 }
218 }
219 return true;
220}
221
Jonglin Lee28a2c922019-01-15 16:38:44 -0800222static void DumpUmountDebuggingInfo() {
Keun-young Park2ba5c812017-03-29 12:54:40 -0700223 int status;
224 if (!security_getenforce()) {
225 LOG(INFO) << "Run lsof";
226 const char* lsof_argv[] = {"/system/bin/lsof"};
Tom Cherry3a803eb2019-09-25 16:23:50 -0700227 logwrap_fork_execvp(arraysize(lsof_argv), lsof_argv, &status, false, LOG_KLOG, true,
228 nullptr);
Keun-young Park8d01f632017-03-13 11:54:47 -0700229 }
Keun-young Park2ba5c812017-03-29 12:54:40 -0700230 FindPartitionsToUmount(nullptr, nullptr, true);
Jonglin Lee28a2c922019-01-15 16:38:44 -0800231 // dump current CPU stack traces and uninterruptible tasks
josephjangaaddf282019-04-16 18:46:24 +0800232 WriteStringToFile("l", PROC_SYSRQ);
233 WriteStringToFile("w", PROC_SYSRQ);
Keun-young Park2ba5c812017-03-29 12:54:40 -0700234}
235
Tom Cherryede0d532017-07-06 14:20:11 -0700236static UmountStat UmountPartitions(std::chrono::milliseconds timeout) {
Keun-young Park2ba5c812017-03-29 12:54:40 -0700237 Timer t;
Keun-young Park2ba5c812017-03-29 12:54:40 -0700238 /* data partition needs all pending writes to be completed and all emulated partitions
239 * umounted.If the current waiting is not good enough, give
240 * up and leave it to e2fsck after reboot to fix it.
241 */
242 while (true) {
243 std::vector<MountEntry> block_devices;
244 std::vector<MountEntry> emulated_devices;
245 if (!FindPartitionsToUmount(&block_devices, &emulated_devices, false)) {
246 return UMOUNT_STAT_ERROR;
247 }
248 if (block_devices.size() == 0) {
Wei Wang8c00e422017-08-16 14:01:46 -0700249 return UMOUNT_STAT_SUCCESS;
Keun-young Park2ba5c812017-03-29 12:54:40 -0700250 }
Wei Wang8c00e422017-08-16 14:01:46 -0700251 bool unmount_done = true;
252 if (emulated_devices.size() > 0) {
Wei Wang25dc30f2017-10-23 15:32:03 -0700253 for (auto& entry : emulated_devices) {
254 if (!entry.Umount(false)) unmount_done = false;
255 }
Wei Wang8c00e422017-08-16 14:01:46 -0700256 if (unmount_done) {
257 sync();
258 }
Keun-young Park2ba5c812017-03-29 12:54:40 -0700259 }
Wei Wang25dc30f2017-10-23 15:32:03 -0700260 for (auto& entry : block_devices) {
261 if (!entry.Umount(timeout == 0ms)) unmount_done = false;
262 }
Wei Wang8c00e422017-08-16 14:01:46 -0700263 if (unmount_done) {
264 return UMOUNT_STAT_SUCCESS;
Keun-young Park2ba5c812017-03-29 12:54:40 -0700265 }
Wei Wang8c00e422017-08-16 14:01:46 -0700266 if ((timeout < t.duration())) { // try umount at least once
267 return UMOUNT_STAT_TIMEOUT;
Keun-young Park2ba5c812017-03-29 12:54:40 -0700268 }
Keun-young Park2ba5c812017-03-29 12:54:40 -0700269 std::this_thread::sleep_for(100ms);
270 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700271}
272
josephjangaaddf282019-04-16 18:46:24 +0800273static void KillAllProcesses() {
274 WriteStringToFile("i", PROC_SYSRQ);
275}
276
277// Create reboot/shutdwon monitor thread
278void RebootMonitorThread(unsigned int cmd, const std::string& rebootTarget, sem_t* reboot_semaphore,
279 std::chrono::milliseconds shutdown_timeout, bool* reboot_monitor_run) {
280 unsigned int remaining_shutdown_time = 0;
281
282 // 30 seconds more than the timeout passed to the thread as there is a final Umount pass
283 // after the timeout is reached.
284 constexpr unsigned int shutdown_watchdog_timeout_default = 30;
285 auto shutdown_watchdog_timeout = android::base::GetUintProperty(
286 "ro.build.shutdown.watchdog.timeout", shutdown_watchdog_timeout_default);
287 remaining_shutdown_time = shutdown_watchdog_timeout + shutdown_timeout.count() / 1000;
288
289 while (*reboot_monitor_run == true) {
290 if (TEMP_FAILURE_RETRY(sem_wait(reboot_semaphore)) == -1) {
291 LOG(ERROR) << "sem_wait failed and exit RebootMonitorThread()";
292 return;
293 }
294
295 timespec shutdown_timeout_timespec;
296 if (clock_gettime(CLOCK_MONOTONIC, &shutdown_timeout_timespec) == -1) {
297 LOG(ERROR) << "clock_gettime() fail! exit RebootMonitorThread()";
298 return;
299 }
300
301 // If there are some remaining shutdown time left from previous round, we use
302 // remaining time here.
303 shutdown_timeout_timespec.tv_sec += remaining_shutdown_time;
304
305 LOG(INFO) << "shutdown_timeout_timespec.tv_sec: " << shutdown_timeout_timespec.tv_sec;
306
307 int sem_return = 0;
308 while ((sem_return = sem_timedwait_monotonic_np(reboot_semaphore,
309 &shutdown_timeout_timespec)) == -1 &&
310 errno == EINTR) {
311 }
312
313 if (sem_return == -1) {
314 LOG(ERROR) << "Reboot thread timed out";
315
316 if (android::base::GetBoolProperty("ro.debuggable", false) == true) {
Tom Cherry2436e6b2019-09-19 11:16:19 -0700317 if (false) {
318 // SEPolicy will block debuggerd from running and this is intentional.
319 // But these lines are left to be enabled during debugging.
320 LOG(INFO) << "Try to dump init process call trace:";
321 const char* vdc_argv[] = {"/system/bin/debuggerd", "-b", "1"};
322 int status;
Tom Cherry3a803eb2019-09-25 16:23:50 -0700323 logwrap_fork_execvp(arraysize(vdc_argv), vdc_argv, &status, false, LOG_KLOG,
324 true, nullptr);
Tom Cherry2436e6b2019-09-19 11:16:19 -0700325 }
josephjangaaddf282019-04-16 18:46:24 +0800326 LOG(INFO) << "Show stack for all active CPU:";
327 WriteStringToFile("l", PROC_SYSRQ);
328
329 LOG(INFO) << "Show tasks that are in disk sleep(uninterruptable sleep), which are "
330 "like "
331 "blocked in mutex or hardware register access:";
332 WriteStringToFile("w", PROC_SYSRQ);
333 }
334
335 // In shutdown case,notify kernel to sync and umount fs to read-only before shutdown.
336 if (cmd == ANDROID_RB_POWEROFF || cmd == ANDROID_RB_THERMOFF) {
337 WriteStringToFile("s", PROC_SYSRQ);
338
339 WriteStringToFile("u", PROC_SYSRQ);
340
341 RebootSystem(cmd, rebootTarget);
342 }
343
344 LOG(ERROR) << "Trigger crash at last!";
345 WriteStringToFile("c", PROC_SYSRQ);
346 } else {
347 timespec current_time_timespec;
348
349 if (clock_gettime(CLOCK_MONOTONIC, &current_time_timespec) == -1) {
350 LOG(ERROR) << "clock_gettime() fail! exit RebootMonitorThread()";
351 return;
352 }
353
354 remaining_shutdown_time =
355 shutdown_timeout_timespec.tv_sec - current_time_timespec.tv_sec;
356
357 LOG(INFO) << "remaining_shutdown_time: " << remaining_shutdown_time;
358 }
359 }
360}
Keun-young Park3ee0df92017-03-27 11:21:09 -0700361
Keun-young Park8d01f632017-03-13 11:54:47 -0700362/* Try umounting all emulated file systems R/W block device cfile systems.
363 * This will just try umount and give it up if it fails.
364 * For fs like ext4, this is ok as file system will be marked as unclean shutdown
365 * and necessary check can be done at the next reboot.
366 * For safer shutdown, caller needs to make sure that
367 * all processes / emulated partition for the target fs are all cleaned-up.
368 *
369 * return true when umount was successful. false when timed out.
370 */
josephjangaaddf282019-04-16 18:46:24 +0800371static UmountStat TryUmountAndFsck(unsigned int cmd, const std::string& rebootTarget, bool runFsck,
372 std::chrono::milliseconds timeout, sem_t* reboot_semaphore) {
Keun-young Park3ee0df92017-03-27 11:21:09 -0700373 Timer t;
Keun-young Park2ba5c812017-03-29 12:54:40 -0700374 std::vector<MountEntry> block_devices;
375 std::vector<MountEntry> emulated_devices;
Keun-young Park8d01f632017-03-13 11:54:47 -0700376
Keun-young Park2ba5c812017-03-29 12:54:40 -0700377 if (runFsck && !FindPartitionsToUmount(&block_devices, &emulated_devices, false)) {
Keun-young Park8d01f632017-03-13 11:54:47 -0700378 return UMOUNT_STAT_ERROR;
379 }
Keun-young Park2ba5c812017-03-29 12:54:40 -0700380
Tom Cherryede0d532017-07-06 14:20:11 -0700381 UmountStat stat = UmountPartitions(timeout - t.duration());
Keun-young Park2ba5c812017-03-29 12:54:40 -0700382 if (stat != UMOUNT_STAT_SUCCESS) {
383 LOG(INFO) << "umount timeout, last resort, kill all and try";
Jonglin Lee28a2c922019-01-15 16:38:44 -0800384 if (DUMP_ON_UMOUNT_FAILURE) DumpUmountDebuggingInfo();
Keun-young Park3ee0df92017-03-27 11:21:09 -0700385 KillAllProcesses();
Keun-young Park2ba5c812017-03-29 12:54:40 -0700386 // 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 -0700387 UmountStat st = UmountPartitions(0ms);
Jonglin Lee28a2c922019-01-15 16:38:44 -0800388 if ((st != UMOUNT_STAT_SUCCESS) && DUMP_ON_UMOUNT_FAILURE) DumpUmountDebuggingInfo();
Keun-young Park8d01f632017-03-13 11:54:47 -0700389 }
390
Keun-young Park2ba5c812017-03-29 12:54:40 -0700391 if (stat == UMOUNT_STAT_SUCCESS && runFsck) {
josephjangaaddf282019-04-16 18:46:24 +0800392 LOG(INFO) << "Pause reboot monitor thread before fsck";
393 sem_post(reboot_semaphore);
394
Keun-young Park2ba5c812017-03-29 12:54:40 -0700395 // fsck part is excluded from timeout check. It only runs for user initiated shutdown
396 // and should not affect reboot time.
397 for (auto& entry : block_devices) {
398 entry.DoFsck();
399 }
josephjangaaddf282019-04-16 18:46:24 +0800400
401 LOG(INFO) << "Resume reboot monitor thread after fsck";
402 sem_post(reboot_semaphore);
Keun-young Park2ba5c812017-03-29 12:54:40 -0700403 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700404 return stat;
405}
406
Jaegeuk Kim2aedc822018-11-20 13:27:06 -0800407// zram is able to use backing device on top of a loopback device.
408// In order to unmount /data successfully, we have to kill the loopback device first
409#define ZRAM_DEVICE "/dev/block/zram0"
410#define ZRAM_RESET "/sys/block/zram0/reset"
411#define ZRAM_BACK_DEV "/sys/block/zram0/backing_dev"
412static void KillZramBackingDevice() {
413 std::string backing_dev;
414 if (!android::base::ReadFileToString(ZRAM_BACK_DEV, &backing_dev)) return;
415
416 if (!android::base::StartsWith(backing_dev, "/dev/block/loop")) return;
417
418 // cut the last "\n"
419 backing_dev.erase(backing_dev.length() - 1);
420
421 // shutdown zram handle
422 Timer swap_timer;
423 LOG(INFO) << "swapoff() start...";
424 if (swapoff(ZRAM_DEVICE) == -1) {
425 LOG(ERROR) << "zram_backing_dev: swapoff (" << backing_dev << ")" << " failed";
426 return;
427 }
428 LOG(INFO) << "swapoff() took " << swap_timer;;
429
josephjangaaddf282019-04-16 18:46:24 +0800430 if (!WriteStringToFile("1", ZRAM_RESET)) {
Jaegeuk Kim2aedc822018-11-20 13:27:06 -0800431 LOG(ERROR) << "zram_backing_dev: reset (" << backing_dev << ")" << " failed";
432 return;
433 }
434
435 // clear loopback device
436 unique_fd loop(TEMP_FAILURE_RETRY(open(backing_dev.c_str(), O_RDWR | O_CLOEXEC)));
437 if (loop.get() < 0) {
438 LOG(ERROR) << "zram_backing_dev: open(" << backing_dev << ")" << " failed";
439 return;
440 }
441
442 if (ioctl(loop.get(), LOOP_CLR_FD, 0) < 0) {
443 LOG(ERROR) << "zram_backing_dev: loop_clear (" << backing_dev << ")" << " failed";
444 return;
445 }
446 LOG(INFO) << "zram_backing_dev: `" << backing_dev << "` is cleared successfully.";
447}
448
Tom Cherry44aceed2018-08-03 13:36:18 -0700449//* Reboot / shutdown the system.
450// cmd ANDROID_RB_* as defined in android_reboot.h
451// reason Reason string like "reboot", "shutdown,userrequested"
452// rebootTarget Reboot target string like "bootloader". Otherwise, it should be an
453// empty string.
454// runFsck Whether to run fsck after umount is done.
455//
456static void DoReboot(unsigned int cmd, const std::string& reason, const std::string& rebootTarget,
457 bool runFsck) {
Keun-young Park8d01f632017-03-13 11:54:47 -0700458 Timer t;
Tom Cherry0a72e6c2018-03-19 16:19:01 -0700459 LOG(INFO) << "Reboot start, reason: " << reason << ", rebootTarget: " << rebootTarget;
460
Tom Cherry2436e6b2019-09-19 11:16:19 -0700461 // If /data isn't mounted then we can skip the extra reboot steps below, since we don't need to
462 // worry about unmounting it.
463 if (!IsDataMounted()) {
464 sync();
465 RebootSystem(cmd, rebootTarget);
466 abort();
467 }
468
Tom Cherry0a72e6c2018-03-19 16:19:01 -0700469 // Ensure last reboot reason is reduced to canonical
470 // alias reported in bootloader or system boot reason.
471 size_t skip = 0;
472 std::vector<std::string> reasons = Split(reason, ",");
473 if (reasons.size() >= 2 && reasons[0] == "reboot" &&
474 (reasons[1] == "recovery" || reasons[1] == "bootloader" || reasons[1] == "cold" ||
475 reasons[1] == "hard" || reasons[1] == "warm")) {
476 skip = strlen("reboot,");
477 }
478 property_set(LAST_REBOOT_REASON_PROPERTY, reason.c_str() + skip);
479 sync();
480
481 bool is_thermal_shutdown = cmd == ANDROID_RB_THERMOFF;
482
483 auto shutdown_timeout = 0ms;
484 if (!SHUTDOWN_ZERO_TIMEOUT) {
Wei Wangb5de0882018-10-09 12:42:06 -0700485 constexpr unsigned int shutdown_timeout_default = 6;
486 constexpr unsigned int max_thermal_shutdown_timeout = 3;
487 auto shutdown_timeout_final = android::base::GetUintProperty("ro.build.shutdown_timeout",
488 shutdown_timeout_default);
489 if (is_thermal_shutdown && shutdown_timeout_final > max_thermal_shutdown_timeout) {
490 shutdown_timeout_final = max_thermal_shutdown_timeout;
Tom Cherry0a72e6c2018-03-19 16:19:01 -0700491 }
Wei Wangb5de0882018-10-09 12:42:06 -0700492 shutdown_timeout = std::chrono::seconds(shutdown_timeout_final);
Tom Cherry0a72e6c2018-03-19 16:19:01 -0700493 }
494 LOG(INFO) << "Shutdown timeout: " << shutdown_timeout.count() << " ms";
495
josephjangaaddf282019-04-16 18:46:24 +0800496 sem_t reboot_semaphore;
497 if (sem_init(&reboot_semaphore, false, 0) == -1) {
498 // These should never fail, but if they do, skip the graceful reboot and reboot immediately.
499 LOG(ERROR) << "sem_init() fail and RebootSystem() return!";
500 RebootSystem(cmd, rebootTarget);
501 }
502
503 // Start a thread to monitor init shutdown process
504 LOG(INFO) << "Create reboot monitor thread.";
505 bool reboot_monitor_run = true;
506 std::thread reboot_monitor_thread(&RebootMonitorThread, cmd, rebootTarget, &reboot_semaphore,
507 shutdown_timeout, &reboot_monitor_run);
508 reboot_monitor_thread.detach();
509
510 // Start reboot monitor thread
511 sem_post(&reboot_semaphore);
512
Keun-young Park7830d592017-03-27 16:07:02 -0700513 // keep debugging tools until non critical ones are all gone.
514 const std::set<std::string> kill_after_apps{"tombstoned", "logd", "adbd"};
515 // watchdogd is a vendor specific component but should be alive to complete shutdown safely.
Keun-young Parkcccb34f2017-07-05 11:38:44 -0700516 const std::set<std::string> to_starts{"watchdogd"};
Tom Cherry911b9b12017-07-27 16:20:58 -0700517 for (const auto& s : ServiceList::GetInstance()) {
Keun-young Park7830d592017-03-27 16:07:02 -0700518 if (kill_after_apps.count(s->name())) {
519 s->SetShutdownCritical();
520 } else if (to_starts.count(s->name())) {
Tom Cherry702ca9a2017-08-25 10:36:52 -0700521 if (auto result = s->Start(); !result) {
522 LOG(ERROR) << "Could not start shutdown 'to_start' service '" << s->name()
523 << "': " << result.error();
524 }
Keun-young Park7830d592017-03-27 16:07:02 -0700525 s->SetShutdownCritical();
Keun-young Parkcccb34f2017-07-05 11:38:44 -0700526 } else if (s->IsShutdownCritical()) {
Tom Cherry702ca9a2017-08-25 10:36:52 -0700527 // Start shutdown critical service if not started.
528 if (auto result = s->Start(); !result) {
529 LOG(ERROR) << "Could not start shutdown critical service '" << s->name()
530 << "': " << result.error();
531 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700532 }
Tom Cherry911b9b12017-07-27 16:20:58 -0700533 }
Keun-young Park7830d592017-03-27 16:07:02 -0700534
Steven Morelandd5eccfd2018-01-19 13:01:53 -0800535 // remaining operations (specifically fsck) may take a substantial duration
Tom Cherry0a72e6c2018-03-19 16:19:01 -0700536 if (cmd == ANDROID_RB_POWEROFF || is_thermal_shutdown) {
Steven Morelandd5eccfd2018-01-19 13:01:53 -0800537 TurnOffBacklight();
538 }
539
Tom Cherry911b9b12017-07-27 16:20:58 -0700540 Service* bootAnim = ServiceList::GetInstance().FindService("bootanim");
541 Service* surfaceFlinger = ServiceList::GetInstance().FindService("surfaceflinger");
Keun-young Park7830d592017-03-27 16:07:02 -0700542 if (bootAnim != nullptr && surfaceFlinger != nullptr && surfaceFlinger->IsRunning()) {
Tom Cherry4f4cacc2019-01-08 10:39:00 -0800543 bool do_shutdown_animation = GetBoolProperty("ro.init.shutdown_animation", false);
544
545 if (do_shutdown_animation) {
546 property_set("service.bootanim.exit", "0");
547 // Could be in the middle of animation. Stop and start so that it can pick
548 // up the right mode.
549 bootAnim->Stop();
550 }
551
Tom Cherry911b9b12017-07-27 16:20:58 -0700552 for (const auto& service : ServiceList::GetInstance()) {
Tom Cherry4f4cacc2019-01-08 10:39:00 -0800553 if (service->classnames().count("animation") == 0) {
554 continue;
555 }
556
557 // start all animation classes if stopped.
558 if (do_shutdown_animation) {
Tom Cherry9949ec52019-05-16 16:54:49 -0700559 service->Start();
Tom Cherry4f4cacc2019-01-08 10:39:00 -0800560 }
561 service->SetShutdownCritical(); // will not check animation class separately
562 }
563
564 if (do_shutdown_animation) {
Tom Cherry9949ec52019-05-16 16:54:49 -0700565 bootAnim->Start();
Tom Cherry4f4cacc2019-01-08 10:39:00 -0800566 surfaceFlinger->SetShutdownCritical();
567 bootAnim->SetShutdownCritical();
Tom Cherry911b9b12017-07-27 16:20:58 -0700568 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700569 }
Keun-young Park7830d592017-03-27 16:07:02 -0700570
Keun-young Park8d01f632017-03-13 11:54:47 -0700571 // optional shutdown step
572 // 1. terminate all services except shutdown critical ones. wait for delay to finish
Keun-young Park30173872017-07-18 10:58:28 -0700573 if (shutdown_timeout > 0ms) {
Keun-young Park8d01f632017-03-13 11:54:47 -0700574 LOG(INFO) << "terminating init services";
Keun-young Park8d01f632017-03-13 11:54:47 -0700575
576 // Ask all services to terminate except shutdown critical ones.
Tom Cherry911b9b12017-07-27 16:20:58 -0700577 for (const auto& s : ServiceList::GetInstance().services_in_shutdown_order()) {
Keun-young Park8d01f632017-03-13 11:54:47 -0700578 if (!s->IsShutdownCritical()) s->Terminate();
Tom Cherry911b9b12017-07-27 16:20:58 -0700579 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700580
581 int service_count = 0;
Keun-young Park30173872017-07-18 10:58:28 -0700582 // Only wait up to half of timeout here
583 auto termination_wait_timeout = shutdown_timeout / 2;
Tom Cherryede0d532017-07-06 14:20:11 -0700584 while (t.duration() < termination_wait_timeout) {
Tom Cherryeeee8312017-07-28 15:22:23 -0700585 ReapAnyOutstandingChildren();
Keun-young Park8d01f632017-03-13 11:54:47 -0700586
587 service_count = 0;
Tom Cherry911b9b12017-07-27 16:20:58 -0700588 for (const auto& s : ServiceList::GetInstance()) {
Keun-young Park8d01f632017-03-13 11:54:47 -0700589 // Count the number of services running except shutdown critical.
590 // Exclude the console as it will ignore the SIGTERM signal
591 // and not exit.
592 // Note: SVC_CONSOLE actually means "requires console" but
593 // it is only used by the shell.
594 if (!s->IsShutdownCritical() && s->pid() != 0 && (s->flags() & SVC_CONSOLE) == 0) {
595 service_count++;
596 }
Tom Cherry911b9b12017-07-27 16:20:58 -0700597 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700598
599 if (service_count == 0) {
600 // All terminable services terminated. We can exit early.
601 break;
602 }
603
604 // Wait a bit before recounting the number or running services.
605 std::this_thread::sleep_for(50ms);
606 }
607 LOG(INFO) << "Terminating running services took " << t
608 << " with remaining services:" << service_count;
609 }
610
611 // minimum safety steps before restarting
612 // 2. kill all services except ones that are necessary for the shutdown sequence.
Tom Cherry911b9b12017-07-27 16:20:58 -0700613 for (const auto& s : ServiceList::GetInstance().services_in_shutdown_order()) {
Keun-young Park2ba5c812017-03-29 12:54:40 -0700614 if (!s->IsShutdownCritical()) s->Stop();
Tom Cherry911b9b12017-07-27 16:20:58 -0700615 }
Luis Hector Chavez92c49bc2018-07-27 11:19:25 -0700616 SubcontextTerminate();
Tom Cherryeeee8312017-07-28 15:22:23 -0700617 ReapAnyOutstandingChildren();
Keun-young Park8d01f632017-03-13 11:54:47 -0700618
619 // 3. send volume shutdown to vold
Tom Cherry911b9b12017-07-27 16:20:58 -0700620 Service* voldService = ServiceList::GetInstance().FindService("vold");
Keun-young Park8d01f632017-03-13 11:54:47 -0700621 if (voldService != nullptr && voldService->IsRunning()) {
622 ShutdownVold();
Keun-young Park2ba5c812017-03-29 12:54:40 -0700623 voldService->Stop();
Keun-young Park8d01f632017-03-13 11:54:47 -0700624 } else {
625 LOG(INFO) << "vold not running, skipping vold shutdown";
626 }
Keun-young Park2ba5c812017-03-29 12:54:40 -0700627 // logcat stopped here
Tom Cherry911b9b12017-07-27 16:20:58 -0700628 for (const auto& s : ServiceList::GetInstance().services_in_shutdown_order()) {
Keun-young Park2ba5c812017-03-29 12:54:40 -0700629 if (kill_after_apps.count(s->name())) s->Stop();
Tom Cherry911b9b12017-07-27 16:20:58 -0700630 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700631 // 4. sync, try umount, and optionally run fsck for user shutdown
Tom Cherry1f9d5402018-03-15 10:22:40 -0700632 {
633 Timer sync_timer;
634 LOG(INFO) << "sync() before umount...";
635 sync();
636 LOG(INFO) << "sync() before umount took" << sync_timer;
637 }
Jaegeuk Kim2aedc822018-11-20 13:27:06 -0800638 // 5. drop caches and disable zram backing device, if exist
639 KillZramBackingDevice();
640
josephjangaaddf282019-04-16 18:46:24 +0800641 UmountStat stat = TryUmountAndFsck(cmd, rebootTarget, runFsck, shutdown_timeout - t.duration(),
642 &reboot_semaphore);
Keun-young Park2ba5c812017-03-29 12:54:40 -0700643 // Follow what linux shutdown is doing: one more sync with little bit delay
Tom Cherry1f9d5402018-03-15 10:22:40 -0700644 {
645 Timer sync_timer;
646 LOG(INFO) << "sync() after umount...";
647 sync();
648 LOG(INFO) << "sync() after umount took" << sync_timer;
649 }
Tom Cherry0a72e6c2018-03-19 16:19:01 -0700650 if (!is_thermal_shutdown) std::this_thread::sleep_for(100ms);
Keun-young Park8d01f632017-03-13 11:54:47 -0700651 LogShutdownTime(stat, &t);
josephjangaaddf282019-04-16 18:46:24 +0800652
653 // Send signal to terminate reboot monitor thread.
654 reboot_monitor_run = false;
655 sem_post(&reboot_semaphore);
656
Keun-young Park8d01f632017-03-13 11:54:47 -0700657 // Reboot regardless of umount status. If umount fails, fsck after reboot will fix it.
658 RebootSystem(cmd, rebootTarget);
659 abort();
660}
Tom Cherry98ad32a2017-04-17 16:34:20 -0700661
Nikita Ioffeba6968e2019-10-07 16:26:33 +0100662static void EnterShutdown() {
663 shutting_down = true;
664 // Skip wait for prop if it is in progress
665 ResetWaitForProp();
666 // Clear EXEC flag if there is one pending
667 for (const auto& s : ServiceList::GetInstance()) {
668 s->UnSetExec();
669 }
670 // We no longer process messages about properties changing coming from property service, so we
671 // need to tell property service to stop sending us these messages, otherwise it'll fill the
672 // buffers and block indefinitely, causing future property sets, including those that init makes
673 // during shutdown in Service::NotifyStateChange() to also block indefinitely.
674 SendStopSendingMessagesMessage();
675}
676
677static void LeaveShutdown() {
678 shutting_down = false;
679 SendStartSendingMessagesMessage();
680}
681
682static void DoUserspaceReboot() {
683 // Triggering userspace-reboot-requested will result in a bunch of set_prop
684 // actions. We should make sure, that all of them are propagated before
685 // proceeding with userspace reboot.
686 // TODO(b/135984674): implement proper synchronization logic.
687 std::this_thread::sleep_for(500ms);
688 EnterShutdown();
689 // TODO(b/135984674): tear down post-data services
690 LeaveShutdown();
691 // TODO(b/135984674): remount userdata
692 ActionManager::GetInstance().QueueEventTrigger("userspace-reboot-resume");
693}
694
695static void HandleUserspaceReboot() {
696 LOG(INFO) << "Clearing queue and starting userspace-reboot-requested trigger";
697 auto& am = ActionManager::GetInstance();
698 am.ClearQueue();
699 am.QueueEventTrigger("userspace-reboot-requested");
700 auto handler = [](const BuiltinArguments&) {
701 DoUserspaceReboot();
702 return Result<void>{};
703 };
704 am.QueueBuiltinAction(handler, "userspace-reboot");
705}
706
707void HandlePowerctlMessage(const std::string& command) {
Tom Cherry98ad32a2017-04-17 16:34:20 -0700708 unsigned int cmd = 0;
Mark Salyzyn62909822017-10-09 09:27:16 -0700709 std::vector<std::string> cmd_params = Split(command, ",");
Tom Cherry98ad32a2017-04-17 16:34:20 -0700710 std::string reboot_target = "";
711 bool run_fsck = false;
712 bool command_invalid = false;
Nikita Ioffeba6968e2019-10-07 16:26:33 +0100713 bool userspace_reboot = false;
Tom Cherry98ad32a2017-04-17 16:34:20 -0700714
Mark Salyzynd7931f12019-07-10 10:33:09 -0700715 if (cmd_params[0] == "shutdown") {
Tom Cherry98ad32a2017-04-17 16:34:20 -0700716 cmd = ANDROID_RB_POWEROFF;
Mark Salyzynd7931f12019-07-10 10:33:09 -0700717 if (cmd_params.size() >= 2) {
Mark Salyzyn73e6b492017-08-14 15:56:53 -0700718 if (cmd_params[1] == "userrequested") {
719 // The shutdown reason is PowerManager.SHUTDOWN_USER_REQUESTED.
720 // Run fsck once the file system is remounted in read-only mode.
721 run_fsck = true;
722 } else if (cmd_params[1] == "thermal") {
Mark Salyzynbfd05b62017-09-26 11:10:12 -0700723 // Turn off sources of heat immediately.
724 TurnOffBacklight();
Mark Salyzyn73e6b492017-08-14 15:56:53 -0700725 // run_fsck is false to avoid delay
726 cmd = ANDROID_RB_THERMOFF;
727 }
Tom Cherry98ad32a2017-04-17 16:34:20 -0700728 }
729 } else if (cmd_params[0] == "reboot") {
730 cmd = ANDROID_RB_RESTART2;
731 if (cmd_params.size() >= 2) {
732 reboot_target = cmd_params[1];
Nikita Ioffeba6968e2019-10-07 16:26:33 +0100733 if (reboot_target == "userspace") {
734 LOG(INFO) << "Userspace reboot requested";
735 userspace_reboot = true;
736 }
Hridya Valsaraju54258262018-09-19 21:14:18 -0700737 // adb reboot fastboot should boot into bootloader for devices not
738 // supporting logical partitions.
739 if (reboot_target == "fastboot" &&
Yifan Hong0e0f8182018-11-16 12:49:06 -0800740 !android::base::GetBoolProperty("ro.boot.dynamic_partitions", false)) {
Hridya Valsaraju54258262018-09-19 21:14:18 -0700741 reboot_target = "bootloader";
742 }
Tom Cherry98ad32a2017-04-17 16:34:20 -0700743 // When rebooting to the bootloader notify the bootloader writing
744 // also the BCB.
745 if (reboot_target == "bootloader") {
746 std::string err;
747 if (!write_reboot_bootloader(&err)) {
748 LOG(ERROR) << "reboot-bootloader: Error writing "
749 "bootloader_message: "
750 << err;
751 }
Tianjie Xu5e98b632019-07-18 12:48:28 -0700752 } else if (reboot_target == "recovery") {
753 bootloader_message boot = {};
754 if (std::string err; !read_bootloader_message(&boot, &err)) {
755 LOG(ERROR) << "Failed to read bootloader message: " << err;
756 }
757 // Update the boot command field if it's empty, and preserve
758 // the other arguments in the bootloader message.
759 if (boot.command[0] == '\0') {
760 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
761 if (std::string err; !write_bootloader_message(boot, &err)) {
762 LOG(ERROR) << "Failed to set bootloader message: " << err;
Nikita Ioffeba6968e2019-10-07 16:26:33 +0100763 return;
Tianjie Xu5e98b632019-07-18 12:48:28 -0700764 }
765 }
Hridya Valsaraju71fb82a2018-08-02 15:02:06 -0700766 } else if (reboot_target == "sideload" || reboot_target == "sideload-auto-reboot" ||
767 reboot_target == "fastboot") {
768 std::string arg = reboot_target == "sideload-auto-reboot" ? "sideload_auto_reboot"
769 : reboot_target;
770 const std::vector<std::string> options = {
771 "--" + arg,
772 };
773 std::string err;
774 if (!write_bootloader_message(options, &err)) {
775 LOG(ERROR) << "Failed to set bootloader message: " << err;
Nikita Ioffeba6968e2019-10-07 16:26:33 +0100776 return;
Hridya Valsaraju71fb82a2018-08-02 15:02:06 -0700777 }
778 reboot_target = "recovery";
Tom Cherry98ad32a2017-04-17 16:34:20 -0700779 }
Hridya Valsaraju71fb82a2018-08-02 15:02:06 -0700780
Mark Salyzynd7931f12019-07-10 10:33:09 -0700781 // If there are additional parameter, pass them along
782 for (size_t i = 2; (cmd_params.size() > i) && cmd_params[i].size(); ++i) {
783 reboot_target += "," + cmd_params[i];
Tom Cherry98ad32a2017-04-17 16:34:20 -0700784 }
785 }
Tom Cherry98ad32a2017-04-17 16:34:20 -0700786 } else {
787 command_invalid = true;
788 }
789 if (command_invalid) {
790 LOG(ERROR) << "powerctl: unrecognized command '" << command << "'";
Nikita Ioffeba6968e2019-10-07 16:26:33 +0100791 return;
792 }
793
794 if (userspace_reboot) {
795 HandleUserspaceReboot();
796 return;
Tom Cherry98ad32a2017-04-17 16:34:20 -0700797 }
798
Wei Wangeeab4912017-06-27 22:08:45 -0700799 LOG(INFO) << "Clear action queue and start shutdown trigger";
800 ActionManager::GetInstance().ClearQueue();
801 // Queue shutdown trigger first
802 ActionManager::GetInstance().QueueEventTrigger("shutdown");
803 // Queue built-in shutdown_done
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700804 auto shutdown_handler = [cmd, command, reboot_target, run_fsck](const BuiltinArguments&) {
Wei Wangeeab4912017-06-27 22:08:45 -0700805 DoReboot(cmd, command, reboot_target, run_fsck);
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700806 return Result<void>{};
Wei Wangeeab4912017-06-27 22:08:45 -0700807 };
808 ActionManager::GetInstance().QueueBuiltinAction(shutdown_handler, "shutdown_done");
809
Nikita Ioffeba6968e2019-10-07 16:26:33 +0100810 EnterShutdown();
811}
Wei Wangeeab4912017-06-27 22:08:45 -0700812
Nikita Ioffeba6968e2019-10-07 16:26:33 +0100813bool IsShuttingDown() {
814 return shutting_down;
Tom Cherry98ad32a2017-04-17 16:34:20 -0700815}
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700816
817} // namespace init
818} // namespace android