blob: e744be288814450bad88c082a3610a62f6a4b796 [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>
Keun-young Park8d01f632017-03-13 11:54:47 -070022#include <mntent.h>
23#include <sys/cdefs.h>
Keun-young Park2ba5c812017-03-29 12:54:40 -070024#include <sys/ioctl.h>
Keun-young Park8d01f632017-03-13 11:54:47 -070025#include <sys/mount.h>
Keun-young Park8d01f632017-03-13 11:54:47 -070026#include <sys/stat.h>
27#include <sys/syscall.h>
28#include <sys/types.h>
29#include <sys/wait.h>
30
31#include <memory>
Keun-young Park7830d592017-03-27 16:07:02 -070032#include <set>
Keun-young Park8d01f632017-03-13 11:54:47 -070033#include <thread>
34#include <vector>
35
Tom Cherryede0d532017-07-06 14:20:11 -070036#include <android-base/chrono_utils.h>
Keun-young Park8d01f632017-03-13 11:54:47 -070037#include <android-base/file.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070038#include <android-base/logging.h>
Keun-young Park8d01f632017-03-13 11:54:47 -070039#include <android-base/macros.h>
Tom Cherryccf23532017-03-28 16:40:41 -070040#include <android-base/properties.h>
Keun-young Park8d01f632017-03-13 11:54:47 -070041#include <android-base/stringprintf.h>
42#include <android-base/strings.h>
Keun-young Park2ba5c812017-03-29 12:54:40 -070043#include <android-base/unique_fd.h>
Keun-young Park8d01f632017-03-13 11:54:47 -070044#include <bootloader_message/bootloader_message.h>
45#include <cutils/android_reboot.h>
Keun-young Park8d01f632017-03-13 11:54:47 -070046#include <fs_mgr.h>
47#include <logwrap/logwrap.h>
Todd Poynorfc827be2017-04-13 15:17:24 -070048#include <private/android_filesystem_config.h>
Tom Cherry0c8d6d22017-08-10 12:22:44 -070049#include <selinux/selinux.h>
Keun-young Park8d01f632017-03-13 11:54:47 -070050
Tom Cherry7fd3bc22018-02-13 15:36:14 -080051#include "action_manager.h"
Wei Wangeeab4912017-06-27 22:08:45 -070052#include "init.h"
Keun-young Park7830d592017-03-27 16:07:02 -070053#include "property_service.h"
Tom Cherry44aceed2018-08-03 13:36:18 -070054#include "reboot_utils.h"
Keun-young Park8d01f632017-03-13 11:54:47 -070055#include "service.h"
Luis Hector Chavez9f97f472017-09-06 13:43:57 -070056#include "sigchld_handler.h"
Keun-young Park8d01f632017-03-13 11:54:47 -070057
Tom Cherry4f4cacc2019-01-08 10:39:00 -080058using android::base::GetBoolProperty;
Mark Salyzyn62909822017-10-09 09:27:16 -070059using android::base::Split;
Keun-young Park8d01f632017-03-13 11:54:47 -070060using android::base::StringPrintf;
Tom Cherryede0d532017-07-06 14:20:11 -070061using android::base::Timer;
Keun-young Park8d01f632017-03-13 11:54:47 -070062
Tom Cherry81f5d3e2017-06-22 12:53:17 -070063namespace android {
64namespace init {
65
Keun-young Park8d01f632017-03-13 11:54:47 -070066// represents umount status during reboot / shutdown.
67enum UmountStat {
68 /* umount succeeded. */
69 UMOUNT_STAT_SUCCESS = 0,
70 /* umount was not run. */
71 UMOUNT_STAT_SKIPPED = 1,
72 /* umount failed with timeout. */
73 UMOUNT_STAT_TIMEOUT = 2,
74 /* could not run due to error */
75 UMOUNT_STAT_ERROR = 3,
76 /* not used by init but reserved for other part to use this to represent the
77 the state where umount status before reboot is not found / available. */
78 UMOUNT_STAT_NOT_AVAILABLE = 4,
79};
80
81// Utility for struct mntent
82class MountEntry {
83 public:
Keun-young Park2ba5c812017-03-29 12:54:40 -070084 explicit MountEntry(const mntent& entry)
Keun-young Park8d01f632017-03-13 11:54:47 -070085 : mnt_fsname_(entry.mnt_fsname),
86 mnt_dir_(entry.mnt_dir),
87 mnt_type_(entry.mnt_type),
Keun-young Park2ba5c812017-03-29 12:54:40 -070088 mnt_opts_(entry.mnt_opts) {}
Keun-young Park8d01f632017-03-13 11:54:47 -070089
Jaegeuk Kim0f04f722017-09-25 10:55:39 -070090 bool Umount(bool force) {
Tom Cherryc9fec9d2018-02-15 14:26:58 -080091 LOG(INFO) << "Unmounting " << mnt_fsname_ << ":" << mnt_dir_ << " opts " << mnt_opts_;
Jaegeuk Kim0f04f722017-09-25 10:55:39 -070092 int r = umount2(mnt_dir_.c_str(), force ? MNT_FORCE : 0);
Keun-young Park2ba5c812017-03-29 12:54:40 -070093 if (r == 0) {
Tom Cherryc9fec9d2018-02-15 14:26:58 -080094 LOG(INFO) << "Umounted " << mnt_fsname_ << ":" << mnt_dir_ << " opts " << mnt_opts_;
Keun-young Park2ba5c812017-03-29 12:54:40 -070095 return true;
96 } else {
Tom Cherryc9fec9d2018-02-15 14:26:58 -080097 PLOG(WARNING) << "Cannot umount " << mnt_fsname_ << ":" << mnt_dir_ << " opts "
Keun-young Park2ba5c812017-03-29 12:54:40 -070098 << mnt_opts_;
99 return false;
100 }
101 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700102
Keun-young Park2ba5c812017-03-29 12:54:40 -0700103 void DoFsck() {
104 int st;
105 if (IsF2Fs()) {
106 const char* f2fs_argv[] = {
107 "/system/bin/fsck.f2fs", "-f", mnt_fsname_.c_str(),
108 };
109 android_fork_execvp_ext(arraysize(f2fs_argv), (char**)f2fs_argv, &st, true, LOG_KLOG,
110 true, nullptr, nullptr, 0);
111 } else if (IsExt4()) {
112 const char* ext4_argv[] = {
113 "/system/bin/e2fsck", "-f", "-y", mnt_fsname_.c_str(),
114 };
115 android_fork_execvp_ext(arraysize(ext4_argv), (char**)ext4_argv, &st, true, LOG_KLOG,
116 true, nullptr, nullptr, 0);
117 }
118 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700119
120 static bool IsBlockDevice(const struct mntent& mntent) {
121 return android::base::StartsWith(mntent.mnt_fsname, "/dev/block");
122 }
123
124 static bool IsEmulatedDevice(const struct mntent& mntent) {
Keun-young Park2ba5c812017-03-29 12:54:40 -0700125 return android::base::StartsWith(mntent.mnt_fsname, "/data/");
Keun-young Park8d01f632017-03-13 11:54:47 -0700126 }
127
128 private:
Keun-young Park2ba5c812017-03-29 12:54:40 -0700129 bool IsF2Fs() const { return mnt_type_ == "f2fs"; }
130
131 bool IsExt4() const { return mnt_type_ == "ext4"; }
132
Keun-young Park8d01f632017-03-13 11:54:47 -0700133 std::string mnt_fsname_;
134 std::string mnt_dir_;
135 std::string mnt_type_;
Keun-young Park2ba5c812017-03-29 12:54:40 -0700136 std::string mnt_opts_;
Keun-young Park8d01f632017-03-13 11:54:47 -0700137};
138
139// Turn off backlight while we are performing power down cleanup activities.
140static void TurnOffBacklight() {
Steven Morelandd5eccfd2018-01-19 13:01:53 -0800141 Service* service = ServiceList::GetInstance().FindService("blank_screen");
142 if (service == nullptr) {
143 LOG(WARNING) << "cannot find blank_screen in TurnOffBacklight";
Keun-young Park8d01f632017-03-13 11:54:47 -0700144 return;
145 }
Tom Cherryd9872642018-10-11 10:38:05 -0700146 if (auto result = service->Start(); !result) {
147 LOG(WARNING) << "Could not start blank_screen service: " << result.error();
148 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700149}
150
Keun-young Park8d01f632017-03-13 11:54:47 -0700151static void ShutdownVold() {
152 const char* vdc_argv[] = {"/system/bin/vdc", "volume", "shutdown"};
153 int status;
154 android_fork_execvp_ext(arraysize(vdc_argv), (char**)vdc_argv, &status, true, LOG_KLOG, true,
155 nullptr, nullptr, 0);
156}
157
158static void LogShutdownTime(UmountStat stat, Timer* t) {
Tom Cherryede0d532017-07-06 14:20:11 -0700159 LOG(WARNING) << "powerctl_shutdown_time_ms:" << std::to_string(t->duration().count()) << ":"
160 << stat;
Keun-young Park8d01f632017-03-13 11:54:47 -0700161}
162
Keun-young Park8d01f632017-03-13 11:54:47 -0700163/* Find all read+write block devices and emulated devices in /proc/mounts
164 * and add them to correpsponding list.
165 */
166static bool FindPartitionsToUmount(std::vector<MountEntry>* blockDevPartitions,
Keun-young Park2ba5c812017-03-29 12:54:40 -0700167 std::vector<MountEntry>* emulatedPartitions, bool dump) {
Tom Cherryf274e782018-10-03 13:13:41 -0700168 std::unique_ptr<std::FILE, int (*)(std::FILE*)> fp(setmntent("/proc/mounts", "re"), endmntent);
Keun-young Park8d01f632017-03-13 11:54:47 -0700169 if (fp == nullptr) {
170 PLOG(ERROR) << "Failed to open /proc/mounts";
171 return false;
172 }
173 mntent* mentry;
174 while ((mentry = getmntent(fp.get())) != nullptr) {
Keun-young Park2ba5c812017-03-29 12:54:40 -0700175 if (dump) {
176 LOG(INFO) << "mount entry " << mentry->mnt_fsname << ":" << mentry->mnt_dir << " opts "
177 << mentry->mnt_opts << " type " << mentry->mnt_type;
178 } else if (MountEntry::IsBlockDevice(*mentry) && hasmntopt(mentry, "rw")) {
Keun-young Park6e12b382017-07-17 12:20:33 -0700179 std::string mount_dir(mentry->mnt_dir);
180 // These are R/O partitions changed to R/W after adb remount.
181 // Do not umount them as shutdown critical services may rely on them.
Wei Wanga01c27e2017-07-25 10:52:08 -0700182 if (mount_dir != "/" && mount_dir != "/system" && mount_dir != "/vendor" &&
183 mount_dir != "/oem") {
Keun-young Park6e12b382017-07-17 12:20:33 -0700184 blockDevPartitions->emplace(blockDevPartitions->begin(), *mentry);
185 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700186 } else if (MountEntry::IsEmulatedDevice(*mentry)) {
Keun-young Park2ba5c812017-03-29 12:54:40 -0700187 emulatedPartitions->emplace(emulatedPartitions->begin(), *mentry);
Keun-young Park8d01f632017-03-13 11:54:47 -0700188 }
189 }
190 return true;
191}
192
Keun-young Park1663e972017-04-21 17:29:26 -0700193static void DumpUmountDebuggingInfo(bool dump_all) {
Keun-young Park2ba5c812017-03-29 12:54:40 -0700194 int status;
195 if (!security_getenforce()) {
196 LOG(INFO) << "Run lsof";
197 const char* lsof_argv[] = {"/system/bin/lsof"};
198 android_fork_execvp_ext(arraysize(lsof_argv), (char**)lsof_argv, &status, true, LOG_KLOG,
199 true, nullptr, nullptr, 0);
Keun-young Park8d01f632017-03-13 11:54:47 -0700200 }
Keun-young Park2ba5c812017-03-29 12:54:40 -0700201 FindPartitionsToUmount(nullptr, nullptr, true);
Keun-young Park1663e972017-04-21 17:29:26 -0700202 if (dump_all) {
203 // dump current tasks, this log can be lengthy, so only dump with dump_all
204 android::base::WriteStringToFile("t", "/proc/sysrq-trigger");
205 }
Keun-young Park2ba5c812017-03-29 12:54:40 -0700206}
207
Tom Cherryede0d532017-07-06 14:20:11 -0700208static UmountStat UmountPartitions(std::chrono::milliseconds timeout) {
Keun-young Park2ba5c812017-03-29 12:54:40 -0700209 Timer t;
Keun-young Park2ba5c812017-03-29 12:54:40 -0700210 /* data partition needs all pending writes to be completed and all emulated partitions
211 * umounted.If the current waiting is not good enough, give
212 * up and leave it to e2fsck after reboot to fix it.
213 */
214 while (true) {
215 std::vector<MountEntry> block_devices;
216 std::vector<MountEntry> emulated_devices;
217 if (!FindPartitionsToUmount(&block_devices, &emulated_devices, false)) {
218 return UMOUNT_STAT_ERROR;
219 }
220 if (block_devices.size() == 0) {
Wei Wang8c00e422017-08-16 14:01:46 -0700221 return UMOUNT_STAT_SUCCESS;
Keun-young Park2ba5c812017-03-29 12:54:40 -0700222 }
Wei Wang8c00e422017-08-16 14:01:46 -0700223 bool unmount_done = true;
224 if (emulated_devices.size() > 0) {
Wei Wang25dc30f2017-10-23 15:32:03 -0700225 for (auto& entry : emulated_devices) {
226 if (!entry.Umount(false)) unmount_done = false;
227 }
Wei Wang8c00e422017-08-16 14:01:46 -0700228 if (unmount_done) {
229 sync();
230 }
Keun-young Park2ba5c812017-03-29 12:54:40 -0700231 }
Wei Wang25dc30f2017-10-23 15:32:03 -0700232 for (auto& entry : block_devices) {
233 if (!entry.Umount(timeout == 0ms)) unmount_done = false;
234 }
Wei Wang8c00e422017-08-16 14:01:46 -0700235 if (unmount_done) {
236 return UMOUNT_STAT_SUCCESS;
Keun-young Park2ba5c812017-03-29 12:54:40 -0700237 }
Wei Wang8c00e422017-08-16 14:01:46 -0700238 if ((timeout < t.duration())) { // try umount at least once
239 return UMOUNT_STAT_TIMEOUT;
Keun-young Park2ba5c812017-03-29 12:54:40 -0700240 }
Keun-young Park2ba5c812017-03-29 12:54:40 -0700241 std::this_thread::sleep_for(100ms);
242 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700243}
244
Keun-young Park3ee0df92017-03-27 11:21:09 -0700245static void KillAllProcesses() { android::base::WriteStringToFile("i", "/proc/sysrq-trigger"); }
246
Keun-young Park8d01f632017-03-13 11:54:47 -0700247/* Try umounting all emulated file systems R/W block device cfile systems.
248 * This will just try umount and give it up if it fails.
249 * For fs like ext4, this is ok as file system will be marked as unclean shutdown
250 * and necessary check can be done at the next reboot.
251 * For safer shutdown, caller needs to make sure that
252 * all processes / emulated partition for the target fs are all cleaned-up.
253 *
254 * return true when umount was successful. false when timed out.
255 */
Tom Cherryede0d532017-07-06 14:20:11 -0700256static UmountStat TryUmountAndFsck(bool runFsck, std::chrono::milliseconds timeout) {
Keun-young Park3ee0df92017-03-27 11:21:09 -0700257 Timer t;
Keun-young Park2ba5c812017-03-29 12:54:40 -0700258 std::vector<MountEntry> block_devices;
259 std::vector<MountEntry> emulated_devices;
Keun-young Park8d01f632017-03-13 11:54:47 -0700260
Keun-young Park2ba5c812017-03-29 12:54:40 -0700261 if (runFsck && !FindPartitionsToUmount(&block_devices, &emulated_devices, false)) {
Keun-young Park8d01f632017-03-13 11:54:47 -0700262 return UMOUNT_STAT_ERROR;
263 }
Keun-young Park2ba5c812017-03-29 12:54:40 -0700264
Tom Cherryede0d532017-07-06 14:20:11 -0700265 UmountStat stat = UmountPartitions(timeout - t.duration());
Keun-young Park2ba5c812017-03-29 12:54:40 -0700266 if (stat != UMOUNT_STAT_SUCCESS) {
267 LOG(INFO) << "umount timeout, last resort, kill all and try";
Keun-young Parkc59b8222017-07-18 18:52:25 -0700268 if (DUMP_ON_UMOUNT_FAILURE) DumpUmountDebuggingInfo(true);
Keun-young Park3ee0df92017-03-27 11:21:09 -0700269 KillAllProcesses();
Keun-young Park2ba5c812017-03-29 12:54:40 -0700270 // 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 -0700271 UmountStat st = UmountPartitions(0ms);
272 if ((st != UMOUNT_STAT_SUCCESS) && DUMP_ON_UMOUNT_FAILURE) DumpUmountDebuggingInfo(false);
Keun-young Park8d01f632017-03-13 11:54:47 -0700273 }
274
Keun-young Park2ba5c812017-03-29 12:54:40 -0700275 if (stat == UMOUNT_STAT_SUCCESS && runFsck) {
276 // fsck part is excluded from timeout check. It only runs for user initiated shutdown
277 // and should not affect reboot time.
278 for (auto& entry : block_devices) {
279 entry.DoFsck();
280 }
281 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700282 return stat;
283}
284
Tom Cherry44aceed2018-08-03 13:36:18 -0700285//* Reboot / shutdown the system.
286// cmd ANDROID_RB_* as defined in android_reboot.h
287// reason Reason string like "reboot", "shutdown,userrequested"
288// rebootTarget Reboot target string like "bootloader". Otherwise, it should be an
289// empty string.
290// runFsck Whether to run fsck after umount is done.
291//
292static void DoReboot(unsigned int cmd, const std::string& reason, const std::string& rebootTarget,
293 bool runFsck) {
Keun-young Park8d01f632017-03-13 11:54:47 -0700294 Timer t;
Tom Cherry0a72e6c2018-03-19 16:19:01 -0700295 LOG(INFO) << "Reboot start, reason: " << reason << ", rebootTarget: " << rebootTarget;
296
297 // Ensure last reboot reason is reduced to canonical
298 // alias reported in bootloader or system boot reason.
299 size_t skip = 0;
300 std::vector<std::string> reasons = Split(reason, ",");
301 if (reasons.size() >= 2 && reasons[0] == "reboot" &&
302 (reasons[1] == "recovery" || reasons[1] == "bootloader" || reasons[1] == "cold" ||
303 reasons[1] == "hard" || reasons[1] == "warm")) {
304 skip = strlen("reboot,");
305 }
306 property_set(LAST_REBOOT_REASON_PROPERTY, reason.c_str() + skip);
307 sync();
308
309 bool is_thermal_shutdown = cmd == ANDROID_RB_THERMOFF;
310
311 auto shutdown_timeout = 0ms;
312 if (!SHUTDOWN_ZERO_TIMEOUT) {
Wei Wangb5de0882018-10-09 12:42:06 -0700313 constexpr unsigned int shutdown_timeout_default = 6;
314 constexpr unsigned int max_thermal_shutdown_timeout = 3;
315 auto shutdown_timeout_final = android::base::GetUintProperty("ro.build.shutdown_timeout",
316 shutdown_timeout_default);
317 if (is_thermal_shutdown && shutdown_timeout_final > max_thermal_shutdown_timeout) {
318 shutdown_timeout_final = max_thermal_shutdown_timeout;
Tom Cherry0a72e6c2018-03-19 16:19:01 -0700319 }
Wei Wangb5de0882018-10-09 12:42:06 -0700320 shutdown_timeout = std::chrono::seconds(shutdown_timeout_final);
Tom Cherry0a72e6c2018-03-19 16:19:01 -0700321 }
322 LOG(INFO) << "Shutdown timeout: " << shutdown_timeout.count() << " ms";
323
Keun-young Park7830d592017-03-27 16:07:02 -0700324 // keep debugging tools until non critical ones are all gone.
325 const std::set<std::string> kill_after_apps{"tombstoned", "logd", "adbd"};
326 // watchdogd is a vendor specific component but should be alive to complete shutdown safely.
Keun-young Parkcccb34f2017-07-05 11:38:44 -0700327 const std::set<std::string> to_starts{"watchdogd"};
Tom Cherry911b9b12017-07-27 16:20:58 -0700328 for (const auto& s : ServiceList::GetInstance()) {
Keun-young Park7830d592017-03-27 16:07:02 -0700329 if (kill_after_apps.count(s->name())) {
330 s->SetShutdownCritical();
331 } else if (to_starts.count(s->name())) {
Tom Cherry702ca9a2017-08-25 10:36:52 -0700332 if (auto result = s->Start(); !result) {
333 LOG(ERROR) << "Could not start shutdown 'to_start' service '" << s->name()
334 << "': " << result.error();
335 }
Keun-young Park7830d592017-03-27 16:07:02 -0700336 s->SetShutdownCritical();
Keun-young Parkcccb34f2017-07-05 11:38:44 -0700337 } else if (s->IsShutdownCritical()) {
Tom Cherry702ca9a2017-08-25 10:36:52 -0700338 // Start shutdown critical service if not started.
339 if (auto result = s->Start(); !result) {
340 LOG(ERROR) << "Could not start shutdown critical service '" << s->name()
341 << "': " << result.error();
342 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700343 }
Tom Cherry911b9b12017-07-27 16:20:58 -0700344 }
Keun-young Park7830d592017-03-27 16:07:02 -0700345
Steven Morelandd5eccfd2018-01-19 13:01:53 -0800346 // remaining operations (specifically fsck) may take a substantial duration
Tom Cherry0a72e6c2018-03-19 16:19:01 -0700347 if (cmd == ANDROID_RB_POWEROFF || is_thermal_shutdown) {
Steven Morelandd5eccfd2018-01-19 13:01:53 -0800348 TurnOffBacklight();
349 }
350
Tom Cherry911b9b12017-07-27 16:20:58 -0700351 Service* bootAnim = ServiceList::GetInstance().FindService("bootanim");
352 Service* surfaceFlinger = ServiceList::GetInstance().FindService("surfaceflinger");
Keun-young Park7830d592017-03-27 16:07:02 -0700353 if (bootAnim != nullptr && surfaceFlinger != nullptr && surfaceFlinger->IsRunning()) {
Tom Cherry4f4cacc2019-01-08 10:39:00 -0800354 bool do_shutdown_animation = GetBoolProperty("ro.init.shutdown_animation", false);
355
356 if (do_shutdown_animation) {
357 property_set("service.bootanim.exit", "0");
358 // Could be in the middle of animation. Stop and start so that it can pick
359 // up the right mode.
360 bootAnim->Stop();
361 }
362
Tom Cherry911b9b12017-07-27 16:20:58 -0700363 for (const auto& service : ServiceList::GetInstance()) {
Tom Cherry4f4cacc2019-01-08 10:39:00 -0800364 if (service->classnames().count("animation") == 0) {
365 continue;
366 }
367
368 // start all animation classes if stopped.
369 if (do_shutdown_animation) {
370 service->Start().IgnoreError();
371 }
372 service->SetShutdownCritical(); // will not check animation class separately
373 }
374
375 if (do_shutdown_animation) {
376 bootAnim->Start().IgnoreError();
377 surfaceFlinger->SetShutdownCritical();
378 bootAnim->SetShutdownCritical();
Tom Cherry911b9b12017-07-27 16:20:58 -0700379 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700380 }
Keun-young Park7830d592017-03-27 16:07:02 -0700381
Keun-young Park8d01f632017-03-13 11:54:47 -0700382 // optional shutdown step
383 // 1. terminate all services except shutdown critical ones. wait for delay to finish
Keun-young Park30173872017-07-18 10:58:28 -0700384 if (shutdown_timeout > 0ms) {
Keun-young Park8d01f632017-03-13 11:54:47 -0700385 LOG(INFO) << "terminating init services";
Keun-young Park8d01f632017-03-13 11:54:47 -0700386
387 // Ask all services to terminate except shutdown critical ones.
Tom Cherry911b9b12017-07-27 16:20:58 -0700388 for (const auto& s : ServiceList::GetInstance().services_in_shutdown_order()) {
Keun-young Park8d01f632017-03-13 11:54:47 -0700389 if (!s->IsShutdownCritical()) s->Terminate();
Tom Cherry911b9b12017-07-27 16:20:58 -0700390 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700391
392 int service_count = 0;
Keun-young Park30173872017-07-18 10:58:28 -0700393 // Only wait up to half of timeout here
394 auto termination_wait_timeout = shutdown_timeout / 2;
Tom Cherryede0d532017-07-06 14:20:11 -0700395 while (t.duration() < termination_wait_timeout) {
Tom Cherryeeee8312017-07-28 15:22:23 -0700396 ReapAnyOutstandingChildren();
Keun-young Park8d01f632017-03-13 11:54:47 -0700397
398 service_count = 0;
Tom Cherry911b9b12017-07-27 16:20:58 -0700399 for (const auto& s : ServiceList::GetInstance()) {
Keun-young Park8d01f632017-03-13 11:54:47 -0700400 // Count the number of services running except shutdown critical.
401 // Exclude the console as it will ignore the SIGTERM signal
402 // and not exit.
403 // Note: SVC_CONSOLE actually means "requires console" but
404 // it is only used by the shell.
405 if (!s->IsShutdownCritical() && s->pid() != 0 && (s->flags() & SVC_CONSOLE) == 0) {
406 service_count++;
407 }
Tom Cherry911b9b12017-07-27 16:20:58 -0700408 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700409
410 if (service_count == 0) {
411 // All terminable services terminated. We can exit early.
412 break;
413 }
414
415 // Wait a bit before recounting the number or running services.
416 std::this_thread::sleep_for(50ms);
417 }
418 LOG(INFO) << "Terminating running services took " << t
419 << " with remaining services:" << service_count;
420 }
421
422 // minimum safety steps before restarting
423 // 2. kill all services except ones that are necessary for the shutdown sequence.
Tom Cherry911b9b12017-07-27 16:20:58 -0700424 for (const auto& s : ServiceList::GetInstance().services_in_shutdown_order()) {
Keun-young Park2ba5c812017-03-29 12:54:40 -0700425 if (!s->IsShutdownCritical()) s->Stop();
Tom Cherry911b9b12017-07-27 16:20:58 -0700426 }
Luis Hector Chavez92c49bc2018-07-27 11:19:25 -0700427 SubcontextTerminate();
Tom Cherryeeee8312017-07-28 15:22:23 -0700428 ReapAnyOutstandingChildren();
Keun-young Park8d01f632017-03-13 11:54:47 -0700429
430 // 3. send volume shutdown to vold
Tom Cherry911b9b12017-07-27 16:20:58 -0700431 Service* voldService = ServiceList::GetInstance().FindService("vold");
Keun-young Park8d01f632017-03-13 11:54:47 -0700432 if (voldService != nullptr && voldService->IsRunning()) {
433 ShutdownVold();
Keun-young Park2ba5c812017-03-29 12:54:40 -0700434 voldService->Stop();
Keun-young Park8d01f632017-03-13 11:54:47 -0700435 } else {
436 LOG(INFO) << "vold not running, skipping vold shutdown";
437 }
Keun-young Park2ba5c812017-03-29 12:54:40 -0700438 // logcat stopped here
Tom Cherry911b9b12017-07-27 16:20:58 -0700439 for (const auto& s : ServiceList::GetInstance().services_in_shutdown_order()) {
Keun-young Park2ba5c812017-03-29 12:54:40 -0700440 if (kill_after_apps.count(s->name())) s->Stop();
Tom Cherry911b9b12017-07-27 16:20:58 -0700441 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700442 // 4. sync, try umount, and optionally run fsck for user shutdown
Tom Cherry1f9d5402018-03-15 10:22:40 -0700443 {
444 Timer sync_timer;
445 LOG(INFO) << "sync() before umount...";
446 sync();
447 LOG(INFO) << "sync() before umount took" << sync_timer;
448 }
Tom Cherryede0d532017-07-06 14:20:11 -0700449 UmountStat stat = TryUmountAndFsck(runFsck, shutdown_timeout - t.duration());
Keun-young Park2ba5c812017-03-29 12:54:40 -0700450 // Follow what linux shutdown is doing: one more sync with little bit delay
Tom Cherry1f9d5402018-03-15 10:22:40 -0700451 {
452 Timer sync_timer;
453 LOG(INFO) << "sync() after umount...";
454 sync();
455 LOG(INFO) << "sync() after umount took" << sync_timer;
456 }
Tom Cherry0a72e6c2018-03-19 16:19:01 -0700457 if (!is_thermal_shutdown) std::this_thread::sleep_for(100ms);
Keun-young Park8d01f632017-03-13 11:54:47 -0700458 LogShutdownTime(stat, &t);
459 // Reboot regardless of umount status. If umount fails, fsck after reboot will fix it.
460 RebootSystem(cmd, rebootTarget);
461 abort();
462}
Tom Cherry98ad32a2017-04-17 16:34:20 -0700463
464bool HandlePowerctlMessage(const std::string& command) {
465 unsigned int cmd = 0;
Mark Salyzyn62909822017-10-09 09:27:16 -0700466 std::vector<std::string> cmd_params = Split(command, ",");
Tom Cherry98ad32a2017-04-17 16:34:20 -0700467 std::string reboot_target = "";
468 bool run_fsck = false;
469 bool command_invalid = false;
470
471 if (cmd_params.size() > 3) {
472 command_invalid = true;
473 } else if (cmd_params[0] == "shutdown") {
474 cmd = ANDROID_RB_POWEROFF;
Mark Salyzyn73e6b492017-08-14 15:56:53 -0700475 if (cmd_params.size() == 2) {
476 if (cmd_params[1] == "userrequested") {
477 // The shutdown reason is PowerManager.SHUTDOWN_USER_REQUESTED.
478 // Run fsck once the file system is remounted in read-only mode.
479 run_fsck = true;
480 } else if (cmd_params[1] == "thermal") {
Mark Salyzynbfd05b62017-09-26 11:10:12 -0700481 // Turn off sources of heat immediately.
482 TurnOffBacklight();
Mark Salyzyn73e6b492017-08-14 15:56:53 -0700483 // run_fsck is false to avoid delay
484 cmd = ANDROID_RB_THERMOFF;
485 }
Tom Cherry98ad32a2017-04-17 16:34:20 -0700486 }
487 } else if (cmd_params[0] == "reboot") {
488 cmd = ANDROID_RB_RESTART2;
489 if (cmd_params.size() >= 2) {
490 reboot_target = cmd_params[1];
Hridya Valsaraju54258262018-09-19 21:14:18 -0700491 // adb reboot fastboot should boot into bootloader for devices not
492 // supporting logical partitions.
493 if (reboot_target == "fastboot" &&
Yifan Hong0e0f8182018-11-16 12:49:06 -0800494 !android::base::GetBoolProperty("ro.boot.dynamic_partitions", false)) {
Hridya Valsaraju54258262018-09-19 21:14:18 -0700495 reboot_target = "bootloader";
496 }
Tom Cherry98ad32a2017-04-17 16:34:20 -0700497 // When rebooting to the bootloader notify the bootloader writing
498 // also the BCB.
499 if (reboot_target == "bootloader") {
500 std::string err;
501 if (!write_reboot_bootloader(&err)) {
502 LOG(ERROR) << "reboot-bootloader: Error writing "
503 "bootloader_message: "
504 << err;
505 }
Hridya Valsaraju71fb82a2018-08-02 15:02:06 -0700506 } else if (reboot_target == "sideload" || reboot_target == "sideload-auto-reboot" ||
507 reboot_target == "fastboot") {
508 std::string arg = reboot_target == "sideload-auto-reboot" ? "sideload_auto_reboot"
509 : reboot_target;
510 const std::vector<std::string> options = {
511 "--" + arg,
512 };
513 std::string err;
514 if (!write_bootloader_message(options, &err)) {
515 LOG(ERROR) << "Failed to set bootloader message: " << err;
516 return false;
517 }
518 reboot_target = "recovery";
Tom Cherry98ad32a2017-04-17 16:34:20 -0700519 }
Hridya Valsaraju71fb82a2018-08-02 15:02:06 -0700520
Mark Salyzyn73e6b492017-08-14 15:56:53 -0700521 // If there is an additional parameter, pass it along
522 if ((cmd_params.size() == 3) && cmd_params[2].size()) {
Tom Cherry98ad32a2017-04-17 16:34:20 -0700523 reboot_target += "," + cmd_params[2];
524 }
525 }
Tom Cherry98ad32a2017-04-17 16:34:20 -0700526 } else {
527 command_invalid = true;
528 }
529 if (command_invalid) {
530 LOG(ERROR) << "powerctl: unrecognized command '" << command << "'";
531 return false;
532 }
533
Wei Wangeeab4912017-06-27 22:08:45 -0700534 LOG(INFO) << "Clear action queue and start shutdown trigger";
535 ActionManager::GetInstance().ClearQueue();
536 // Queue shutdown trigger first
537 ActionManager::GetInstance().QueueEventTrigger("shutdown");
538 // Queue built-in shutdown_done
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700539 auto shutdown_handler = [cmd, command, reboot_target, run_fsck](const BuiltinArguments&) {
Wei Wangeeab4912017-06-27 22:08:45 -0700540 DoReboot(cmd, command, reboot_target, run_fsck);
Tom Cherry557946e2017-08-01 13:50:23 -0700541 return Success();
Wei Wangeeab4912017-06-27 22:08:45 -0700542 };
543 ActionManager::GetInstance().QueueBuiltinAction(shutdown_handler, "shutdown_done");
544
545 // Skip wait for prop if it is in progress
546 ResetWaitForProp();
547
Tom Cherry3b81f2d2017-07-28 14:48:41 -0700548 // Clear EXEC flag if there is one pending
Tom Cherry911b9b12017-07-27 16:20:58 -0700549 for (const auto& s : ServiceList::GetInstance()) {
550 s->UnSetExec();
551 }
Wei Wangeeab4912017-06-27 22:08:45 -0700552
Tom Cherry98ad32a2017-04-17 16:34:20 -0700553 return true;
554}
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700555
556} // namespace init
557} // namespace android