blob: 328164f780563fd10fc915ae41dd02838405b612 [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>
Luis Hector Chavez519e5f02017-06-29 09:50:30 -070023#include <sys/capability.h>
Keun-young Park8d01f632017-03-13 11:54:47 -070024#include <sys/cdefs.h>
Keun-young Park2ba5c812017-03-29 12:54:40 -070025#include <sys/ioctl.h>
Keun-young Park8d01f632017-03-13 11:54:47 -070026#include <sys/mount.h>
Keun-young Park8d01f632017-03-13 11:54:47 -070027#include <sys/reboot.h>
28#include <sys/stat.h>
29#include <sys/syscall.h>
30#include <sys/types.h>
31#include <sys/wait.h>
32
33#include <memory>
Keun-young Park7830d592017-03-27 16:07:02 -070034#include <set>
Keun-young Park8d01f632017-03-13 11:54:47 -070035#include <thread>
36#include <vector>
37
Tom Cherryede0d532017-07-06 14:20:11 -070038#include <android-base/chrono_utils.h>
Keun-young Park8d01f632017-03-13 11:54:47 -070039#include <android-base/file.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070040#include <android-base/logging.h>
Keun-young Park8d01f632017-03-13 11:54:47 -070041#include <android-base/macros.h>
Tom Cherryccf23532017-03-28 16:40:41 -070042#include <android-base/properties.h>
Keun-young Park8d01f632017-03-13 11:54:47 -070043#include <android-base/stringprintf.h>
44#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
Tom Cherry7fd3bc22018-02-13 15:36:14 -080053#include "action_manager.h"
Luis Hector Chavez519e5f02017-06-29 09:50:30 -070054#include "capabilities.h"
Wei Wangeeab4912017-06-27 22:08:45 -070055#include "init.h"
Keun-young Park7830d592017-03-27 16:07:02 -070056#include "property_service.h"
Keun-young Park8d01f632017-03-13 11:54:47 -070057#include "service.h"
Luis Hector Chavez9f97f472017-09-06 13:43:57 -070058#include "sigchld_handler.h"
Keun-young Park8d01f632017-03-13 11:54:47 -070059
Mark Salyzyn62909822017-10-09 09:27:16 -070060using android::base::Split;
Keun-young Park8d01f632017-03-13 11:54:47 -070061using android::base::StringPrintf;
Tom Cherryede0d532017-07-06 14:20:11 -070062using android::base::Timer;
Keun-young Park8d01f632017-03-13 11:54:47 -070063
Tom Cherry81f5d3e2017-06-22 12:53:17 -070064namespace android {
65namespace init {
66
Keun-young Park8d01f632017-03-13 11:54:47 -070067// represents umount status during reboot / shutdown.
68enum UmountStat {
69 /* umount succeeded. */
70 UMOUNT_STAT_SUCCESS = 0,
71 /* umount was not run. */
72 UMOUNT_STAT_SKIPPED = 1,
73 /* umount failed with timeout. */
74 UMOUNT_STAT_TIMEOUT = 2,
75 /* could not run due to error */
76 UMOUNT_STAT_ERROR = 3,
77 /* not used by init but reserved for other part to use this to represent the
78 the state where umount status before reboot is not found / available. */
79 UMOUNT_STAT_NOT_AVAILABLE = 4,
80};
81
82// Utility for struct mntent
83class MountEntry {
84 public:
Keun-young Park2ba5c812017-03-29 12:54:40 -070085 explicit MountEntry(const mntent& entry)
Keun-young Park8d01f632017-03-13 11:54:47 -070086 : mnt_fsname_(entry.mnt_fsname),
87 mnt_dir_(entry.mnt_dir),
88 mnt_type_(entry.mnt_type),
Keun-young Park2ba5c812017-03-29 12:54:40 -070089 mnt_opts_(entry.mnt_opts) {}
Keun-young Park8d01f632017-03-13 11:54:47 -070090
Jaegeuk Kim0f04f722017-09-25 10:55:39 -070091 bool Umount(bool force) {
Tom Cherryc9fec9d2018-02-15 14:26:58 -080092 LOG(INFO) << "Unmounting " << mnt_fsname_ << ":" << mnt_dir_ << " opts " << mnt_opts_;
Jaegeuk Kim0f04f722017-09-25 10:55:39 -070093 int r = umount2(mnt_dir_.c_str(), force ? MNT_FORCE : 0);
Keun-young Park2ba5c812017-03-29 12:54:40 -070094 if (r == 0) {
Tom Cherryc9fec9d2018-02-15 14:26:58 -080095 LOG(INFO) << "Umounted " << mnt_fsname_ << ":" << mnt_dir_ << " opts " << mnt_opts_;
Keun-young Park2ba5c812017-03-29 12:54:40 -070096 return true;
97 } else {
Tom Cherryc9fec9d2018-02-15 14:26:58 -080098 PLOG(WARNING) << "Cannot umount " << mnt_fsname_ << ":" << mnt_dir_ << " opts "
Keun-young Park2ba5c812017-03-29 12:54:40 -070099 << mnt_opts_;
100 return false;
101 }
102 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700103
Keun-young Park2ba5c812017-03-29 12:54:40 -0700104 void DoFsck() {
105 int st;
106 if (IsF2Fs()) {
107 const char* f2fs_argv[] = {
108 "/system/bin/fsck.f2fs", "-f", mnt_fsname_.c_str(),
109 };
110 android_fork_execvp_ext(arraysize(f2fs_argv), (char**)f2fs_argv, &st, true, LOG_KLOG,
111 true, nullptr, nullptr, 0);
112 } else if (IsExt4()) {
113 const char* ext4_argv[] = {
114 "/system/bin/e2fsck", "-f", "-y", mnt_fsname_.c_str(),
115 };
116 android_fork_execvp_ext(arraysize(ext4_argv), (char**)ext4_argv, &st, true, LOG_KLOG,
117 true, nullptr, nullptr, 0);
118 }
119 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700120
121 static bool IsBlockDevice(const struct mntent& mntent) {
122 return android::base::StartsWith(mntent.mnt_fsname, "/dev/block");
123 }
124
125 static bool IsEmulatedDevice(const struct mntent& mntent) {
Keun-young Park2ba5c812017-03-29 12:54:40 -0700126 return android::base::StartsWith(mntent.mnt_fsname, "/data/");
Keun-young Park8d01f632017-03-13 11:54:47 -0700127 }
128
129 private:
Keun-young Park2ba5c812017-03-29 12:54:40 -0700130 bool IsF2Fs() const { return mnt_type_ == "f2fs"; }
131
132 bool IsExt4() const { return mnt_type_ == "ext4"; }
133
Keun-young Park8d01f632017-03-13 11:54:47 -0700134 std::string mnt_fsname_;
135 std::string mnt_dir_;
136 std::string mnt_type_;
Keun-young Park2ba5c812017-03-29 12:54:40 -0700137 std::string mnt_opts_;
Keun-young Park8d01f632017-03-13 11:54:47 -0700138};
139
140// Turn off backlight while we are performing power down cleanup activities.
141static void TurnOffBacklight() {
Steven Morelandd5eccfd2018-01-19 13:01:53 -0800142 Service* service = ServiceList::GetInstance().FindService("blank_screen");
143 if (service == nullptr) {
144 LOG(WARNING) << "cannot find blank_screen in TurnOffBacklight";
Keun-young Park8d01f632017-03-13 11:54:47 -0700145 return;
146 }
Steven Morelandd5eccfd2018-01-19 13:01:53 -0800147 service->Start();
Keun-young Park8d01f632017-03-13 11:54:47 -0700148}
149
Keun-young Park8d01f632017-03-13 11:54:47 -0700150static void ShutdownVold() {
151 const char* vdc_argv[] = {"/system/bin/vdc", "volume", "shutdown"};
152 int status;
153 android_fork_execvp_ext(arraysize(vdc_argv), (char**)vdc_argv, &status, true, LOG_KLOG, true,
154 nullptr, nullptr, 0);
155}
156
157static void LogShutdownTime(UmountStat stat, Timer* t) {
Tom Cherryede0d532017-07-06 14:20:11 -0700158 LOG(WARNING) << "powerctl_shutdown_time_ms:" << std::to_string(t->duration().count()) << ":"
159 << stat;
Keun-young Park8d01f632017-03-13 11:54:47 -0700160}
161
Luis Hector Chavez9f97f472017-09-06 13:43:57 -0700162bool IsRebootCapable() {
Luis Hector Chavez519e5f02017-06-29 09:50:30 -0700163 if (!CAP_IS_SUPPORTED(CAP_SYS_BOOT)) {
164 PLOG(WARNING) << "CAP_SYS_BOOT is not supported";
165 return true;
166 }
167
168 ScopedCaps caps(cap_get_proc());
169 if (!caps) {
170 PLOG(WARNING) << "cap_get_proc() failed";
171 return true;
172 }
173
174 cap_flag_value_t value = CAP_SET;
175 if (cap_get_flag(caps.get(), CAP_SYS_BOOT, CAP_EFFECTIVE, &value) != 0) {
176 PLOG(WARNING) << "cap_get_flag(CAP_SYS_BOOT, EFFECTIVE) failed";
177 return true;
178 }
179 return value == CAP_SET;
180}
181
Tom Cherryd8db7ab2017-08-17 17:28:30 -0700182void __attribute__((noreturn)) RebootSystem(unsigned int cmd, const std::string& rebootTarget) {
Keun-young Park3cd8c6f2017-03-23 15:33:16 -0700183 LOG(INFO) << "Reboot ending, jumping to kernel";
Luis Hector Chavez519e5f02017-06-29 09:50:30 -0700184
185 if (!IsRebootCapable()) {
186 // On systems where init does not have the capability of rebooting the
187 // device, just exit cleanly.
188 exit(0);
189 }
190
Keun-young Park8d01f632017-03-13 11:54:47 -0700191 switch (cmd) {
192 case ANDROID_RB_POWEROFF:
193 reboot(RB_POWER_OFF);
194 break;
195
196 case ANDROID_RB_RESTART2:
197 syscall(__NR_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,
198 LINUX_REBOOT_CMD_RESTART2, rebootTarget.c_str());
199 break;
200
201 case ANDROID_RB_THERMOFF:
202 reboot(RB_POWER_OFF);
203 break;
204 }
205 // In normal case, reboot should not return.
Tom Cherryd8db7ab2017-08-17 17:28:30 -0700206 PLOG(ERROR) << "reboot call returned";
Keun-young Park8d01f632017-03-13 11:54:47 -0700207 abort();
208}
209
210/* Find all read+write block devices and emulated devices in /proc/mounts
211 * and add them to correpsponding list.
212 */
213static bool FindPartitionsToUmount(std::vector<MountEntry>* blockDevPartitions,
Keun-young Park2ba5c812017-03-29 12:54:40 -0700214 std::vector<MountEntry>* emulatedPartitions, bool dump) {
Keun-young Park8d01f632017-03-13 11:54:47 -0700215 std::unique_ptr<std::FILE, int (*)(std::FILE*)> fp(setmntent("/proc/mounts", "r"), endmntent);
216 if (fp == nullptr) {
217 PLOG(ERROR) << "Failed to open /proc/mounts";
218 return false;
219 }
220 mntent* mentry;
221 while ((mentry = getmntent(fp.get())) != nullptr) {
Keun-young Park2ba5c812017-03-29 12:54:40 -0700222 if (dump) {
223 LOG(INFO) << "mount entry " << mentry->mnt_fsname << ":" << mentry->mnt_dir << " opts "
224 << mentry->mnt_opts << " type " << mentry->mnt_type;
225 } else if (MountEntry::IsBlockDevice(*mentry) && hasmntopt(mentry, "rw")) {
Keun-young Park6e12b382017-07-17 12:20:33 -0700226 std::string mount_dir(mentry->mnt_dir);
227 // These are R/O partitions changed to R/W after adb remount.
228 // Do not umount them as shutdown critical services may rely on them.
Wei Wanga01c27e2017-07-25 10:52:08 -0700229 if (mount_dir != "/" && mount_dir != "/system" && mount_dir != "/vendor" &&
230 mount_dir != "/oem") {
Keun-young Park6e12b382017-07-17 12:20:33 -0700231 blockDevPartitions->emplace(blockDevPartitions->begin(), *mentry);
232 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700233 } else if (MountEntry::IsEmulatedDevice(*mentry)) {
Keun-young Park2ba5c812017-03-29 12:54:40 -0700234 emulatedPartitions->emplace(emulatedPartitions->begin(), *mentry);
Keun-young Park8d01f632017-03-13 11:54:47 -0700235 }
236 }
237 return true;
238}
239
Keun-young Park1663e972017-04-21 17:29:26 -0700240static void DumpUmountDebuggingInfo(bool dump_all) {
Keun-young Park2ba5c812017-03-29 12:54:40 -0700241 int status;
242 if (!security_getenforce()) {
243 LOG(INFO) << "Run lsof";
244 const char* lsof_argv[] = {"/system/bin/lsof"};
245 android_fork_execvp_ext(arraysize(lsof_argv), (char**)lsof_argv, &status, true, LOG_KLOG,
246 true, nullptr, nullptr, 0);
Keun-young Park8d01f632017-03-13 11:54:47 -0700247 }
Keun-young Park2ba5c812017-03-29 12:54:40 -0700248 FindPartitionsToUmount(nullptr, nullptr, true);
Keun-young Park1663e972017-04-21 17:29:26 -0700249 if (dump_all) {
250 // dump current tasks, this log can be lengthy, so only dump with dump_all
251 android::base::WriteStringToFile("t", "/proc/sysrq-trigger");
252 }
Keun-young Park2ba5c812017-03-29 12:54:40 -0700253}
254
Tom Cherryede0d532017-07-06 14:20:11 -0700255static UmountStat UmountPartitions(std::chrono::milliseconds timeout) {
Keun-young Park2ba5c812017-03-29 12:54:40 -0700256 Timer t;
Keun-young Park2ba5c812017-03-29 12:54:40 -0700257 /* data partition needs all pending writes to be completed and all emulated partitions
258 * umounted.If the current waiting is not good enough, give
259 * up and leave it to e2fsck after reboot to fix it.
260 */
261 while (true) {
262 std::vector<MountEntry> block_devices;
263 std::vector<MountEntry> emulated_devices;
264 if (!FindPartitionsToUmount(&block_devices, &emulated_devices, false)) {
265 return UMOUNT_STAT_ERROR;
266 }
267 if (block_devices.size() == 0) {
Wei Wang8c00e422017-08-16 14:01:46 -0700268 return UMOUNT_STAT_SUCCESS;
Keun-young Park2ba5c812017-03-29 12:54:40 -0700269 }
Wei Wang8c00e422017-08-16 14:01:46 -0700270 bool unmount_done = true;
271 if (emulated_devices.size() > 0) {
Wei Wang25dc30f2017-10-23 15:32:03 -0700272 for (auto& entry : emulated_devices) {
273 if (!entry.Umount(false)) unmount_done = false;
274 }
Wei Wang8c00e422017-08-16 14:01:46 -0700275 if (unmount_done) {
276 sync();
277 }
Keun-young Park2ba5c812017-03-29 12:54:40 -0700278 }
Wei Wang25dc30f2017-10-23 15:32:03 -0700279 for (auto& entry : block_devices) {
280 if (!entry.Umount(timeout == 0ms)) unmount_done = false;
281 }
Wei Wang8c00e422017-08-16 14:01:46 -0700282 if (unmount_done) {
283 return UMOUNT_STAT_SUCCESS;
Keun-young Park2ba5c812017-03-29 12:54:40 -0700284 }
Wei Wang8c00e422017-08-16 14:01:46 -0700285 if ((timeout < t.duration())) { // try umount at least once
286 return UMOUNT_STAT_TIMEOUT;
Keun-young Park2ba5c812017-03-29 12:54:40 -0700287 }
Keun-young Park2ba5c812017-03-29 12:54:40 -0700288 std::this_thread::sleep_for(100ms);
289 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700290}
291
Keun-young Park3ee0df92017-03-27 11:21:09 -0700292static void KillAllProcesses() { android::base::WriteStringToFile("i", "/proc/sysrq-trigger"); }
293
Keun-young Park8d01f632017-03-13 11:54:47 -0700294/* Try umounting all emulated file systems R/W block device cfile systems.
295 * This will just try umount and give it up if it fails.
296 * For fs like ext4, this is ok as file system will be marked as unclean shutdown
297 * and necessary check can be done at the next reboot.
298 * For safer shutdown, caller needs to make sure that
299 * all processes / emulated partition for the target fs are all cleaned-up.
300 *
301 * return true when umount was successful. false when timed out.
302 */
Tom Cherryede0d532017-07-06 14:20:11 -0700303static UmountStat TryUmountAndFsck(bool runFsck, std::chrono::milliseconds timeout) {
Keun-young Park3ee0df92017-03-27 11:21:09 -0700304 Timer t;
Keun-young Park2ba5c812017-03-29 12:54:40 -0700305 std::vector<MountEntry> block_devices;
306 std::vector<MountEntry> emulated_devices;
Keun-young Park8d01f632017-03-13 11:54:47 -0700307
Keun-young Park2ba5c812017-03-29 12:54:40 -0700308 if (runFsck && !FindPartitionsToUmount(&block_devices, &emulated_devices, false)) {
Keun-young Park8d01f632017-03-13 11:54:47 -0700309 return UMOUNT_STAT_ERROR;
310 }
Keun-young Park2ba5c812017-03-29 12:54:40 -0700311
Tom Cherryede0d532017-07-06 14:20:11 -0700312 UmountStat stat = UmountPartitions(timeout - t.duration());
Keun-young Park2ba5c812017-03-29 12:54:40 -0700313 if (stat != UMOUNT_STAT_SUCCESS) {
314 LOG(INFO) << "umount timeout, last resort, kill all and try";
Keun-young Parkc59b8222017-07-18 18:52:25 -0700315 if (DUMP_ON_UMOUNT_FAILURE) DumpUmountDebuggingInfo(true);
Keun-young Park3ee0df92017-03-27 11:21:09 -0700316 KillAllProcesses();
Keun-young Park2ba5c812017-03-29 12:54:40 -0700317 // 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 -0700318 UmountStat st = UmountPartitions(0ms);
319 if ((st != UMOUNT_STAT_SUCCESS) && DUMP_ON_UMOUNT_FAILURE) DumpUmountDebuggingInfo(false);
Keun-young Park8d01f632017-03-13 11:54:47 -0700320 }
321
Keun-young Park2ba5c812017-03-29 12:54:40 -0700322 if (stat == UMOUNT_STAT_SUCCESS && runFsck) {
323 // fsck part is excluded from timeout check. It only runs for user initiated shutdown
324 // and should not affect reboot time.
325 for (auto& entry : block_devices) {
326 entry.DoFsck();
327 }
328 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700329 return stat;
330}
331
Tom Cherryea31ac22018-03-19 16:39:37 -0700332void DoReboot(unsigned int cmd, const std::string& reason, const std::string& rebootTarget,
333 bool runFsck) {
Keun-young Park8d01f632017-03-13 11:54:47 -0700334 Timer t;
Tom Cherryea31ac22018-03-19 16:39:37 -0700335 LOG(INFO) << "Reboot start, reason: " << reason << ", rebootTarget: " << rebootTarget;
336
337 // Ensure last reboot reason is reduced to canonical
338 // alias reported in bootloader or system boot reason.
339 size_t skip = 0;
340 std::vector<std::string> reasons = Split(reason, ",");
341 if (reasons.size() >= 2 && reasons[0] == "reboot" &&
342 (reasons[1] == "recovery" || reasons[1] == "bootloader" || reasons[1] == "cold" ||
343 reasons[1] == "hard" || reasons[1] == "warm")) {
344 skip = strlen("reboot,");
345 }
346 property_set(LAST_REBOOT_REASON_PROPERTY, reason.c_str() + skip);
347 sync();
348
349 bool is_thermal_shutdown = cmd == ANDROID_RB_THERMOFF;
350
351 auto shutdown_timeout = 0ms;
352 if (!SHUTDOWN_ZERO_TIMEOUT) {
353 if (is_thermal_shutdown) {
354 constexpr unsigned int thermal_shutdown_timeout = 1;
355 shutdown_timeout = std::chrono::seconds(thermal_shutdown_timeout);
356 } else {
357 constexpr unsigned int shutdown_timeout_default = 6;
358 auto shutdown_timeout_property = android::base::GetUintProperty(
359 "ro.build.shutdown_timeout", shutdown_timeout_default);
360 shutdown_timeout = std::chrono::seconds(shutdown_timeout_property);
361 }
362 }
363 LOG(INFO) << "Shutdown timeout: " << shutdown_timeout.count() << " ms";
364
Keun-young Park7830d592017-03-27 16:07:02 -0700365 // keep debugging tools until non critical ones are all gone.
366 const std::set<std::string> kill_after_apps{"tombstoned", "logd", "adbd"};
367 // watchdogd is a vendor specific component but should be alive to complete shutdown safely.
Keun-young Parkcccb34f2017-07-05 11:38:44 -0700368 const std::set<std::string> to_starts{"watchdogd"};
Tom Cherry911b9b12017-07-27 16:20:58 -0700369 for (const auto& s : ServiceList::GetInstance()) {
Keun-young Park7830d592017-03-27 16:07:02 -0700370 if (kill_after_apps.count(s->name())) {
371 s->SetShutdownCritical();
372 } else if (to_starts.count(s->name())) {
Tom Cherry702ca9a2017-08-25 10:36:52 -0700373 if (auto result = s->Start(); !result) {
374 LOG(ERROR) << "Could not start shutdown 'to_start' service '" << s->name()
375 << "': " << result.error();
376 }
Keun-young Park7830d592017-03-27 16:07:02 -0700377 s->SetShutdownCritical();
Keun-young Parkcccb34f2017-07-05 11:38:44 -0700378 } else if (s->IsShutdownCritical()) {
Tom Cherry702ca9a2017-08-25 10:36:52 -0700379 // Start shutdown critical service if not started.
380 if (auto result = s->Start(); !result) {
381 LOG(ERROR) << "Could not start shutdown critical service '" << s->name()
382 << "': " << result.error();
383 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700384 }
Tom Cherry911b9b12017-07-27 16:20:58 -0700385 }
Keun-young Park7830d592017-03-27 16:07:02 -0700386
Steven Morelandd5eccfd2018-01-19 13:01:53 -0800387 // remaining operations (specifically fsck) may take a substantial duration
Tom Cherryea31ac22018-03-19 16:39:37 -0700388 if (cmd == ANDROID_RB_POWEROFF || is_thermal_shutdown) {
Steven Morelandd5eccfd2018-01-19 13:01:53 -0800389 TurnOffBacklight();
390 }
391
Tom Cherry911b9b12017-07-27 16:20:58 -0700392 Service* bootAnim = ServiceList::GetInstance().FindService("bootanim");
393 Service* surfaceFlinger = ServiceList::GetInstance().FindService("surfaceflinger");
Keun-young Park7830d592017-03-27 16:07:02 -0700394 if (bootAnim != nullptr && surfaceFlinger != nullptr && surfaceFlinger->IsRunning()) {
Tom Cherry911b9b12017-07-27 16:20:58 -0700395 // will not check animation class separately
396 for (const auto& service : ServiceList::GetInstance()) {
397 if (service->classnames().count("animation")) service->SetShutdownCritical();
398 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700399 }
Keun-young Park7830d592017-03-27 16:07:02 -0700400
Keun-young Park8d01f632017-03-13 11:54:47 -0700401 // optional shutdown step
402 // 1. terminate all services except shutdown critical ones. wait for delay to finish
Keun-young Park30173872017-07-18 10:58:28 -0700403 if (shutdown_timeout > 0ms) {
Keun-young Park8d01f632017-03-13 11:54:47 -0700404 LOG(INFO) << "terminating init services";
Keun-young Park8d01f632017-03-13 11:54:47 -0700405
406 // Ask all services to terminate except shutdown critical ones.
Tom Cherry911b9b12017-07-27 16:20:58 -0700407 for (const auto& s : ServiceList::GetInstance().services_in_shutdown_order()) {
Keun-young Park8d01f632017-03-13 11:54:47 -0700408 if (!s->IsShutdownCritical()) s->Terminate();
Tom Cherry911b9b12017-07-27 16:20:58 -0700409 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700410
411 int service_count = 0;
Keun-young Park30173872017-07-18 10:58:28 -0700412 // Only wait up to half of timeout here
413 auto termination_wait_timeout = shutdown_timeout / 2;
Tom Cherryede0d532017-07-06 14:20:11 -0700414 while (t.duration() < termination_wait_timeout) {
Tom Cherryeeee8312017-07-28 15:22:23 -0700415 ReapAnyOutstandingChildren();
Keun-young Park8d01f632017-03-13 11:54:47 -0700416
417 service_count = 0;
Tom Cherry911b9b12017-07-27 16:20:58 -0700418 for (const auto& s : ServiceList::GetInstance()) {
Keun-young Park8d01f632017-03-13 11:54:47 -0700419 // Count the number of services running except shutdown critical.
420 // Exclude the console as it will ignore the SIGTERM signal
421 // and not exit.
422 // Note: SVC_CONSOLE actually means "requires console" but
423 // it is only used by the shell.
424 if (!s->IsShutdownCritical() && s->pid() != 0 && (s->flags() & SVC_CONSOLE) == 0) {
425 service_count++;
426 }
Tom Cherry911b9b12017-07-27 16:20:58 -0700427 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700428
429 if (service_count == 0) {
430 // All terminable services terminated. We can exit early.
431 break;
432 }
433
434 // Wait a bit before recounting the number or running services.
435 std::this_thread::sleep_for(50ms);
436 }
437 LOG(INFO) << "Terminating running services took " << t
438 << " with remaining services:" << service_count;
439 }
440
441 // minimum safety steps before restarting
442 // 2. kill all services except ones that are necessary for the shutdown sequence.
Tom Cherry911b9b12017-07-27 16:20:58 -0700443 for (const auto& s : ServiceList::GetInstance().services_in_shutdown_order()) {
Keun-young Park2ba5c812017-03-29 12:54:40 -0700444 if (!s->IsShutdownCritical()) s->Stop();
Tom Cherry911b9b12017-07-27 16:20:58 -0700445 }
Tom Cherryeeee8312017-07-28 15:22:23 -0700446 ReapAnyOutstandingChildren();
Keun-young Park8d01f632017-03-13 11:54:47 -0700447
448 // 3. send volume shutdown to vold
Tom Cherry911b9b12017-07-27 16:20:58 -0700449 Service* voldService = ServiceList::GetInstance().FindService("vold");
Keun-young Park8d01f632017-03-13 11:54:47 -0700450 if (voldService != nullptr && voldService->IsRunning()) {
451 ShutdownVold();
Keun-young Park2ba5c812017-03-29 12:54:40 -0700452 voldService->Stop();
Keun-young Park8d01f632017-03-13 11:54:47 -0700453 } else {
454 LOG(INFO) << "vold not running, skipping vold shutdown";
455 }
Keun-young Park2ba5c812017-03-29 12:54:40 -0700456 // logcat stopped here
Tom Cherry911b9b12017-07-27 16:20:58 -0700457 for (const auto& s : ServiceList::GetInstance().services_in_shutdown_order()) {
Keun-young Park2ba5c812017-03-29 12:54:40 -0700458 if (kill_after_apps.count(s->name())) s->Stop();
Tom Cherry911b9b12017-07-27 16:20:58 -0700459 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700460 // 4. sync, try umount, and optionally run fsck for user shutdown
Keun-young Park2ba5c812017-03-29 12:54:40 -0700461 sync();
Tom Cherryede0d532017-07-06 14:20:11 -0700462 UmountStat stat = TryUmountAndFsck(runFsck, shutdown_timeout - t.duration());
Keun-young Park2ba5c812017-03-29 12:54:40 -0700463 // Follow what linux shutdown is doing: one more sync with little bit delay
464 sync();
Tom Cherryea31ac22018-03-19 16:39:37 -0700465 if (!is_thermal_shutdown) std::this_thread::sleep_for(100ms);
Keun-young Park8d01f632017-03-13 11:54:47 -0700466 LogShutdownTime(stat, &t);
467 // Reboot regardless of umount status. If umount fails, fsck after reboot will fix it.
468 RebootSystem(cmd, rebootTarget);
469 abort();
470}
Tom Cherry98ad32a2017-04-17 16:34:20 -0700471
472bool HandlePowerctlMessage(const std::string& command) {
473 unsigned int cmd = 0;
Mark Salyzyn62909822017-10-09 09:27:16 -0700474 std::vector<std::string> cmd_params = Split(command, ",");
Tom Cherry98ad32a2017-04-17 16:34:20 -0700475 std::string reboot_target = "";
476 bool run_fsck = false;
477 bool command_invalid = false;
478
479 if (cmd_params.size() > 3) {
480 command_invalid = true;
481 } else if (cmd_params[0] == "shutdown") {
482 cmd = ANDROID_RB_POWEROFF;
Mark Salyzyn73e6b492017-08-14 15:56:53 -0700483 if (cmd_params.size() == 2) {
484 if (cmd_params[1] == "userrequested") {
485 // The shutdown reason is PowerManager.SHUTDOWN_USER_REQUESTED.
486 // Run fsck once the file system is remounted in read-only mode.
487 run_fsck = true;
488 } else if (cmd_params[1] == "thermal") {
Mark Salyzynbfd05b62017-09-26 11:10:12 -0700489 // Turn off sources of heat immediately.
490 TurnOffBacklight();
Mark Salyzyn73e6b492017-08-14 15:56:53 -0700491 // run_fsck is false to avoid delay
492 cmd = ANDROID_RB_THERMOFF;
493 }
Tom Cherry98ad32a2017-04-17 16:34:20 -0700494 }
495 } else if (cmd_params[0] == "reboot") {
496 cmd = ANDROID_RB_RESTART2;
497 if (cmd_params.size() >= 2) {
498 reboot_target = cmd_params[1];
499 // When rebooting to the bootloader notify the bootloader writing
500 // also the BCB.
501 if (reboot_target == "bootloader") {
502 std::string err;
503 if (!write_reboot_bootloader(&err)) {
504 LOG(ERROR) << "reboot-bootloader: Error writing "
505 "bootloader_message: "
506 << err;
507 }
508 }
Mark Salyzyn73e6b492017-08-14 15:56:53 -0700509 // If there is an additional parameter, pass it along
510 if ((cmd_params.size() == 3) && cmd_params[2].size()) {
Tom Cherry98ad32a2017-04-17 16:34:20 -0700511 reboot_target += "," + cmd_params[2];
512 }
513 }
Tom Cherry98ad32a2017-04-17 16:34:20 -0700514 } else {
515 command_invalid = true;
516 }
517 if (command_invalid) {
518 LOG(ERROR) << "powerctl: unrecognized command '" << command << "'";
519 return false;
520 }
521
Wei Wangeeab4912017-06-27 22:08:45 -0700522 LOG(INFO) << "Clear action queue and start shutdown trigger";
523 ActionManager::GetInstance().ClearQueue();
524 // Queue shutdown trigger first
525 ActionManager::GetInstance().QueueEventTrigger("shutdown");
526 // Queue built-in shutdown_done
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700527 auto shutdown_handler = [cmd, command, reboot_target, run_fsck](const BuiltinArguments&) {
Wei Wangeeab4912017-06-27 22:08:45 -0700528 DoReboot(cmd, command, reboot_target, run_fsck);
Tom Cherry557946e2017-08-01 13:50:23 -0700529 return Success();
Wei Wangeeab4912017-06-27 22:08:45 -0700530 };
531 ActionManager::GetInstance().QueueBuiltinAction(shutdown_handler, "shutdown_done");
532
533 // Skip wait for prop if it is in progress
534 ResetWaitForProp();
535
Tom Cherry3b81f2d2017-07-28 14:48:41 -0700536 // Clear EXEC flag if there is one pending
Tom Cherry911b9b12017-07-27 16:20:58 -0700537 for (const auto& s : ServiceList::GetInstance()) {
538 s->UnSetExec();
539 }
Wei Wangeeab4912017-06-27 22:08:45 -0700540
Tom Cherry98ad32a2017-04-17 16:34:20 -0700541 return true;
542}
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700543
544} // namespace init
545} // namespace android