blob: a88a42d743882e9cf09039052c18c0f0295ea332 [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
Luis Hector Chavez519e5f02017-06-29 09:50:30 -070053#include "capabilities.h"
Wei Wangeeab4912017-06-27 22:08:45 -070054#include "init.h"
Keun-young Park7830d592017-03-27 16:07:02 -070055#include "property_service.h"
Keun-young Park8d01f632017-03-13 11:54:47 -070056#include "service.h"
Luis Hector Chavez9f97f472017-09-06 13:43:57 -070057#include "sigchld_handler.h"
Keun-young Park8d01f632017-03-13 11:54:47 -070058
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) {
91 int r = umount2(mnt_dir_.c_str(), force ? MNT_FORCE : 0);
Keun-young Park2ba5c812017-03-29 12:54:40 -070092 if (r == 0) {
93 LOG(INFO) << "umounted " << mnt_fsname_ << ":" << mnt_dir_ << " opts " << mnt_opts_;
94 return true;
95 } else {
96 PLOG(WARNING) << "cannot umount " << mnt_fsname_ << ":" << mnt_dir_ << " opts "
97 << mnt_opts_;
98 return false;
99 }
100 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700101
Keun-young Park2ba5c812017-03-29 12:54:40 -0700102 void DoFsck() {
103 int st;
104 if (IsF2Fs()) {
105 const char* f2fs_argv[] = {
106 "/system/bin/fsck.f2fs", "-f", mnt_fsname_.c_str(),
107 };
108 android_fork_execvp_ext(arraysize(f2fs_argv), (char**)f2fs_argv, &st, true, LOG_KLOG,
109 true, nullptr, nullptr, 0);
110 } else if (IsExt4()) {
111 const char* ext4_argv[] = {
112 "/system/bin/e2fsck", "-f", "-y", mnt_fsname_.c_str(),
113 };
114 android_fork_execvp_ext(arraysize(ext4_argv), (char**)ext4_argv, &st, true, LOG_KLOG,
115 true, nullptr, nullptr, 0);
116 }
117 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700118
119 static bool IsBlockDevice(const struct mntent& mntent) {
120 return android::base::StartsWith(mntent.mnt_fsname, "/dev/block");
121 }
122
123 static bool IsEmulatedDevice(const struct mntent& mntent) {
Keun-young Park2ba5c812017-03-29 12:54:40 -0700124 return android::base::StartsWith(mntent.mnt_fsname, "/data/");
Keun-young Park8d01f632017-03-13 11:54:47 -0700125 }
126
127 private:
Keun-young Park2ba5c812017-03-29 12:54:40 -0700128 bool IsF2Fs() const { return mnt_type_ == "f2fs"; }
129
130 bool IsExt4() const { return mnt_type_ == "ext4"; }
131
Keun-young Park8d01f632017-03-13 11:54:47 -0700132 std::string mnt_fsname_;
133 std::string mnt_dir_;
134 std::string mnt_type_;
Keun-young Park2ba5c812017-03-29 12:54:40 -0700135 std::string mnt_opts_;
Keun-young Park8d01f632017-03-13 11:54:47 -0700136};
137
138// Turn off backlight while we are performing power down cleanup activities.
139static void TurnOffBacklight() {
Steven Morelandd5eccfd2018-01-19 13:01:53 -0800140 Service* service = ServiceList::GetInstance().FindService("blank_screen");
141 if (service == nullptr) {
142 LOG(WARNING) << "cannot find blank_screen in TurnOffBacklight";
Keun-young Park8d01f632017-03-13 11:54:47 -0700143 return;
144 }
Steven Morelandd5eccfd2018-01-19 13:01:53 -0800145 service->Start();
Keun-young Park8d01f632017-03-13 11:54:47 -0700146}
147
Keun-young Park8d01f632017-03-13 11:54:47 -0700148static void ShutdownVold() {
149 const char* vdc_argv[] = {"/system/bin/vdc", "volume", "shutdown"};
150 int status;
151 android_fork_execvp_ext(arraysize(vdc_argv), (char**)vdc_argv, &status, true, LOG_KLOG, true,
152 nullptr, nullptr, 0);
153}
154
155static void LogShutdownTime(UmountStat stat, Timer* t) {
Tom Cherryede0d532017-07-06 14:20:11 -0700156 LOG(WARNING) << "powerctl_shutdown_time_ms:" << std::to_string(t->duration().count()) << ":"
157 << stat;
Keun-young Park8d01f632017-03-13 11:54:47 -0700158}
159
Luis Hector Chavez9f97f472017-09-06 13:43:57 -0700160bool IsRebootCapable() {
Luis Hector Chavez519e5f02017-06-29 09:50:30 -0700161 if (!CAP_IS_SUPPORTED(CAP_SYS_BOOT)) {
162 PLOG(WARNING) << "CAP_SYS_BOOT is not supported";
163 return true;
164 }
165
166 ScopedCaps caps(cap_get_proc());
167 if (!caps) {
168 PLOG(WARNING) << "cap_get_proc() failed";
169 return true;
170 }
171
172 cap_flag_value_t value = CAP_SET;
173 if (cap_get_flag(caps.get(), CAP_SYS_BOOT, CAP_EFFECTIVE, &value) != 0) {
174 PLOG(WARNING) << "cap_get_flag(CAP_SYS_BOOT, EFFECTIVE) failed";
175 return true;
176 }
177 return value == CAP_SET;
178}
179
Tom Cherryd8db7ab2017-08-17 17:28:30 -0700180void __attribute__((noreturn)) RebootSystem(unsigned int cmd, const std::string& rebootTarget) {
Keun-young Park3cd8c6f2017-03-23 15:33:16 -0700181 LOG(INFO) << "Reboot ending, jumping to kernel";
Luis Hector Chavez519e5f02017-06-29 09:50:30 -0700182
183 if (!IsRebootCapable()) {
184 // On systems where init does not have the capability of rebooting the
185 // device, just exit cleanly.
186 exit(0);
187 }
188
Keun-young Park8d01f632017-03-13 11:54:47 -0700189 switch (cmd) {
190 case ANDROID_RB_POWEROFF:
191 reboot(RB_POWER_OFF);
192 break;
193
194 case ANDROID_RB_RESTART2:
195 syscall(__NR_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,
196 LINUX_REBOOT_CMD_RESTART2, rebootTarget.c_str());
197 break;
198
199 case ANDROID_RB_THERMOFF:
200 reboot(RB_POWER_OFF);
201 break;
202 }
203 // In normal case, reboot should not return.
Tom Cherryd8db7ab2017-08-17 17:28:30 -0700204 PLOG(ERROR) << "reboot call returned";
Keun-young Park8d01f632017-03-13 11:54:47 -0700205 abort();
206}
207
208/* Find all read+write block devices and emulated devices in /proc/mounts
209 * and add them to correpsponding list.
210 */
211static bool FindPartitionsToUmount(std::vector<MountEntry>* blockDevPartitions,
Keun-young Park2ba5c812017-03-29 12:54:40 -0700212 std::vector<MountEntry>* emulatedPartitions, bool dump) {
Keun-young Park8d01f632017-03-13 11:54:47 -0700213 std::unique_ptr<std::FILE, int (*)(std::FILE*)> fp(setmntent("/proc/mounts", "r"), endmntent);
214 if (fp == nullptr) {
215 PLOG(ERROR) << "Failed to open /proc/mounts";
216 return false;
217 }
218 mntent* mentry;
219 while ((mentry = getmntent(fp.get())) != nullptr) {
Keun-young Park2ba5c812017-03-29 12:54:40 -0700220 if (dump) {
221 LOG(INFO) << "mount entry " << mentry->mnt_fsname << ":" << mentry->mnt_dir << " opts "
222 << mentry->mnt_opts << " type " << mentry->mnt_type;
223 } else if (MountEntry::IsBlockDevice(*mentry) && hasmntopt(mentry, "rw")) {
Keun-young Park6e12b382017-07-17 12:20:33 -0700224 std::string mount_dir(mentry->mnt_dir);
225 // These are R/O partitions changed to R/W after adb remount.
226 // Do not umount them as shutdown critical services may rely on them.
Wei Wanga01c27e2017-07-25 10:52:08 -0700227 if (mount_dir != "/" && mount_dir != "/system" && mount_dir != "/vendor" &&
228 mount_dir != "/oem") {
Keun-young Park6e12b382017-07-17 12:20:33 -0700229 blockDevPartitions->emplace(blockDevPartitions->begin(), *mentry);
230 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700231 } else if (MountEntry::IsEmulatedDevice(*mentry)) {
Keun-young Park2ba5c812017-03-29 12:54:40 -0700232 emulatedPartitions->emplace(emulatedPartitions->begin(), *mentry);
Keun-young Park8d01f632017-03-13 11:54:47 -0700233 }
234 }
235 return true;
236}
237
Keun-young Park1663e972017-04-21 17:29:26 -0700238static void DumpUmountDebuggingInfo(bool dump_all) {
Keun-young Park2ba5c812017-03-29 12:54:40 -0700239 int status;
240 if (!security_getenforce()) {
241 LOG(INFO) << "Run lsof";
242 const char* lsof_argv[] = {"/system/bin/lsof"};
243 android_fork_execvp_ext(arraysize(lsof_argv), (char**)lsof_argv, &status, true, LOG_KLOG,
244 true, nullptr, nullptr, 0);
Keun-young Park8d01f632017-03-13 11:54:47 -0700245 }
Keun-young Park2ba5c812017-03-29 12:54:40 -0700246 FindPartitionsToUmount(nullptr, nullptr, true);
Keun-young Park1663e972017-04-21 17:29:26 -0700247 if (dump_all) {
248 // dump current tasks, this log can be lengthy, so only dump with dump_all
249 android::base::WriteStringToFile("t", "/proc/sysrq-trigger");
250 }
Keun-young Park2ba5c812017-03-29 12:54:40 -0700251}
252
Tom Cherryede0d532017-07-06 14:20:11 -0700253static UmountStat UmountPartitions(std::chrono::milliseconds timeout) {
Keun-young Park2ba5c812017-03-29 12:54:40 -0700254 Timer t;
Keun-young Park2ba5c812017-03-29 12:54:40 -0700255 /* data partition needs all pending writes to be completed and all emulated partitions
256 * umounted.If the current waiting is not good enough, give
257 * up and leave it to e2fsck after reboot to fix it.
258 */
259 while (true) {
260 std::vector<MountEntry> block_devices;
261 std::vector<MountEntry> emulated_devices;
262 if (!FindPartitionsToUmount(&block_devices, &emulated_devices, false)) {
263 return UMOUNT_STAT_ERROR;
264 }
265 if (block_devices.size() == 0) {
Wei Wang8c00e422017-08-16 14:01:46 -0700266 return UMOUNT_STAT_SUCCESS;
Keun-young Park2ba5c812017-03-29 12:54:40 -0700267 }
Wei Wang8c00e422017-08-16 14:01:46 -0700268 bool unmount_done = true;
269 if (emulated_devices.size() > 0) {
Wei Wang25dc30f2017-10-23 15:32:03 -0700270 for (auto& entry : emulated_devices) {
271 if (!entry.Umount(false)) unmount_done = false;
272 }
Wei Wang8c00e422017-08-16 14:01:46 -0700273 if (unmount_done) {
274 sync();
275 }
Keun-young Park2ba5c812017-03-29 12:54:40 -0700276 }
Wei Wang25dc30f2017-10-23 15:32:03 -0700277 for (auto& entry : block_devices) {
278 if (!entry.Umount(timeout == 0ms)) unmount_done = false;
279 }
Wei Wang8c00e422017-08-16 14:01:46 -0700280 if (unmount_done) {
281 return UMOUNT_STAT_SUCCESS;
Keun-young Park2ba5c812017-03-29 12:54:40 -0700282 }
Wei Wang8c00e422017-08-16 14:01:46 -0700283 if ((timeout < t.duration())) { // try umount at least once
284 return UMOUNT_STAT_TIMEOUT;
Keun-young Park2ba5c812017-03-29 12:54:40 -0700285 }
Keun-young Park2ba5c812017-03-29 12:54:40 -0700286 std::this_thread::sleep_for(100ms);
287 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700288}
289
Keun-young Park3ee0df92017-03-27 11:21:09 -0700290static void KillAllProcesses() { android::base::WriteStringToFile("i", "/proc/sysrq-trigger"); }
291
Keun-young Park8d01f632017-03-13 11:54:47 -0700292/* Try umounting all emulated file systems R/W block device cfile systems.
293 * This will just try umount and give it up if it fails.
294 * For fs like ext4, this is ok as file system will be marked as unclean shutdown
295 * and necessary check can be done at the next reboot.
296 * For safer shutdown, caller needs to make sure that
297 * all processes / emulated partition for the target fs are all cleaned-up.
298 *
299 * return true when umount was successful. false when timed out.
300 */
Tom Cherryede0d532017-07-06 14:20:11 -0700301static UmountStat TryUmountAndFsck(bool runFsck, std::chrono::milliseconds timeout) {
Keun-young Park3ee0df92017-03-27 11:21:09 -0700302 Timer t;
Keun-young Park2ba5c812017-03-29 12:54:40 -0700303 std::vector<MountEntry> block_devices;
304 std::vector<MountEntry> emulated_devices;
Keun-young Park8d01f632017-03-13 11:54:47 -0700305
Keun-young Park2ba5c812017-03-29 12:54:40 -0700306 if (runFsck && !FindPartitionsToUmount(&block_devices, &emulated_devices, false)) {
Keun-young Park8d01f632017-03-13 11:54:47 -0700307 return UMOUNT_STAT_ERROR;
308 }
Keun-young Park2ba5c812017-03-29 12:54:40 -0700309
Tom Cherryede0d532017-07-06 14:20:11 -0700310 UmountStat stat = UmountPartitions(timeout - t.duration());
Keun-young Park2ba5c812017-03-29 12:54:40 -0700311 if (stat != UMOUNT_STAT_SUCCESS) {
312 LOG(INFO) << "umount timeout, last resort, kill all and try";
Keun-young Parkc59b8222017-07-18 18:52:25 -0700313 if (DUMP_ON_UMOUNT_FAILURE) DumpUmountDebuggingInfo(true);
Keun-young Park3ee0df92017-03-27 11:21:09 -0700314 KillAllProcesses();
Keun-young Park2ba5c812017-03-29 12:54:40 -0700315 // 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 -0700316 UmountStat st = UmountPartitions(0ms);
317 if ((st != UMOUNT_STAT_SUCCESS) && DUMP_ON_UMOUNT_FAILURE) DumpUmountDebuggingInfo(false);
Keun-young Park8d01f632017-03-13 11:54:47 -0700318 }
319
Keun-young Park2ba5c812017-03-29 12:54:40 -0700320 if (stat == UMOUNT_STAT_SUCCESS && runFsck) {
321 // fsck part is excluded from timeout check. It only runs for user initiated shutdown
322 // and should not affect reboot time.
323 for (auto& entry : block_devices) {
324 entry.DoFsck();
325 }
326 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700327 return stat;
328}
329
Keun-young Park8d01f632017-03-13 11:54:47 -0700330void DoReboot(unsigned int cmd, const std::string& reason, const std::string& rebootTarget,
331 bool runFsck) {
332 Timer t;
Keun-young Park3cd8c6f2017-03-23 15:33:16 -0700333 LOG(INFO) << "Reboot start, reason: " << reason << ", rebootTarget: " << rebootTarget;
Keun-young Park8d01f632017-03-13 11:54:47 -0700334
Mark Salyzyn62909822017-10-09 09:27:16 -0700335 // Ensure last reboot reason is reduced to canonical
336 // alias reported in bootloader or system boot reason.
337 size_t skip = 0;
338 std::vector<std::string> reasons = Split(reason, ",");
339 if (reasons.size() >= 2 && reasons[0] == "reboot" &&
340 (reasons[1] == "recovery" || reasons[1] == "bootloader" || reasons[1] == "cold" ||
341 reasons[1] == "hard" || reasons[1] == "warm")) {
342 skip = strlen("reboot,");
343 }
344 property_set(LAST_REBOOT_REASON_PROPERTY, reason.c_str() + skip);
Mark Salyzyn73e6b492017-08-14 15:56:53 -0700345 sync();
Keun-young Park8d01f632017-03-13 11:54:47 -0700346
Mark Salyzyn73e6b492017-08-14 15:56:53 -0700347 bool is_thermal_shutdown = cmd == ANDROID_RB_THERMOFF;
Keun-young Parkaa08ea42017-03-23 13:27:28 -0700348
Keun-young Park30173872017-07-18 10:58:28 -0700349 auto shutdown_timeout = 0ms;
Tom Cherryede0d532017-07-06 14:20:11 -0700350 if (!SHUTDOWN_ZERO_TIMEOUT) {
Keun-young Park30173872017-07-18 10:58:28 -0700351 if (is_thermal_shutdown) {
352 constexpr unsigned int thermal_shutdown_timeout = 1;
353 shutdown_timeout = std::chrono::seconds(thermal_shutdown_timeout);
354 } else {
355 constexpr unsigned int shutdown_timeout_default = 6;
356 auto shutdown_timeout_property = android::base::GetUintProperty(
357 "ro.build.shutdown_timeout", shutdown_timeout_default);
358 shutdown_timeout = std::chrono::seconds(shutdown_timeout_property);
359 }
Keun-young Parkc4ffa5c2017-03-28 09:41:36 -0700360 }
Keun-young Park30173872017-07-18 10:58:28 -0700361 LOG(INFO) << "Shutdown timeout: " << shutdown_timeout.count() << " ms";
Keun-young Parkaa08ea42017-03-23 13:27:28 -0700362
Keun-young Park7830d592017-03-27 16:07:02 -0700363 // keep debugging tools until non critical ones are all gone.
364 const std::set<std::string> kill_after_apps{"tombstoned", "logd", "adbd"};
365 // watchdogd is a vendor specific component but should be alive to complete shutdown safely.
Keun-young Parkcccb34f2017-07-05 11:38:44 -0700366 const std::set<std::string> to_starts{"watchdogd"};
Tom Cherry911b9b12017-07-27 16:20:58 -0700367 for (const auto& s : ServiceList::GetInstance()) {
Keun-young Park7830d592017-03-27 16:07:02 -0700368 if (kill_after_apps.count(s->name())) {
369 s->SetShutdownCritical();
370 } else if (to_starts.count(s->name())) {
Tom Cherry702ca9a2017-08-25 10:36:52 -0700371 if (auto result = s->Start(); !result) {
372 LOG(ERROR) << "Could not start shutdown 'to_start' service '" << s->name()
373 << "': " << result.error();
374 }
Keun-young Park7830d592017-03-27 16:07:02 -0700375 s->SetShutdownCritical();
Keun-young Parkcccb34f2017-07-05 11:38:44 -0700376 } else if (s->IsShutdownCritical()) {
Tom Cherry702ca9a2017-08-25 10:36:52 -0700377 // Start shutdown critical service if not started.
378 if (auto result = s->Start(); !result) {
379 LOG(ERROR) << "Could not start shutdown critical service '" << s->name()
380 << "': " << result.error();
381 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700382 }
Tom Cherry911b9b12017-07-27 16:20:58 -0700383 }
Keun-young Park7830d592017-03-27 16:07:02 -0700384
Steven Morelandd5eccfd2018-01-19 13:01:53 -0800385 // remaining operations (specifically fsck) may take a substantial duration
386 if (cmd == ANDROID_RB_POWEROFF || is_thermal_shutdown) {
387 TurnOffBacklight();
388 }
389
Tom Cherry911b9b12017-07-27 16:20:58 -0700390 Service* bootAnim = ServiceList::GetInstance().FindService("bootanim");
391 Service* surfaceFlinger = ServiceList::GetInstance().FindService("surfaceflinger");
Keun-young Park7830d592017-03-27 16:07:02 -0700392 if (bootAnim != nullptr && surfaceFlinger != nullptr && surfaceFlinger->IsRunning()) {
Tom Cherry911b9b12017-07-27 16:20:58 -0700393 // will not check animation class separately
394 for (const auto& service : ServiceList::GetInstance()) {
395 if (service->classnames().count("animation")) service->SetShutdownCritical();
396 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700397 }
Keun-young Park7830d592017-03-27 16:07:02 -0700398
Keun-young Park8d01f632017-03-13 11:54:47 -0700399 // optional shutdown step
400 // 1. terminate all services except shutdown critical ones. wait for delay to finish
Keun-young Park30173872017-07-18 10:58:28 -0700401 if (shutdown_timeout > 0ms) {
Keun-young Park8d01f632017-03-13 11:54:47 -0700402 LOG(INFO) << "terminating init services";
Keun-young Park8d01f632017-03-13 11:54:47 -0700403
404 // Ask all services to terminate except shutdown critical ones.
Tom Cherry911b9b12017-07-27 16:20:58 -0700405 for (const auto& s : ServiceList::GetInstance().services_in_shutdown_order()) {
Keun-young Park8d01f632017-03-13 11:54:47 -0700406 if (!s->IsShutdownCritical()) s->Terminate();
Tom Cherry911b9b12017-07-27 16:20:58 -0700407 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700408
409 int service_count = 0;
Keun-young Park30173872017-07-18 10:58:28 -0700410 // Only wait up to half of timeout here
411 auto termination_wait_timeout = shutdown_timeout / 2;
Tom Cherryede0d532017-07-06 14:20:11 -0700412 while (t.duration() < termination_wait_timeout) {
Tom Cherryeeee8312017-07-28 15:22:23 -0700413 ReapAnyOutstandingChildren();
Keun-young Park8d01f632017-03-13 11:54:47 -0700414
415 service_count = 0;
Tom Cherry911b9b12017-07-27 16:20:58 -0700416 for (const auto& s : ServiceList::GetInstance()) {
Keun-young Park8d01f632017-03-13 11:54:47 -0700417 // Count the number of services running except shutdown critical.
418 // Exclude the console as it will ignore the SIGTERM signal
419 // and not exit.
420 // Note: SVC_CONSOLE actually means "requires console" but
421 // it is only used by the shell.
422 if (!s->IsShutdownCritical() && s->pid() != 0 && (s->flags() & SVC_CONSOLE) == 0) {
423 service_count++;
424 }
Tom Cherry911b9b12017-07-27 16:20:58 -0700425 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700426
427 if (service_count == 0) {
428 // All terminable services terminated. We can exit early.
429 break;
430 }
431
432 // Wait a bit before recounting the number or running services.
433 std::this_thread::sleep_for(50ms);
434 }
435 LOG(INFO) << "Terminating running services took " << t
436 << " with remaining services:" << service_count;
437 }
438
439 // minimum safety steps before restarting
440 // 2. kill all services except ones that are necessary for the shutdown sequence.
Tom Cherry911b9b12017-07-27 16:20:58 -0700441 for (const auto& s : ServiceList::GetInstance().services_in_shutdown_order()) {
Keun-young Park2ba5c812017-03-29 12:54:40 -0700442 if (!s->IsShutdownCritical()) s->Stop();
Tom Cherry911b9b12017-07-27 16:20:58 -0700443 }
Tom Cherryeeee8312017-07-28 15:22:23 -0700444 ReapAnyOutstandingChildren();
Keun-young Park8d01f632017-03-13 11:54:47 -0700445
446 // 3. send volume shutdown to vold
Tom Cherry911b9b12017-07-27 16:20:58 -0700447 Service* voldService = ServiceList::GetInstance().FindService("vold");
Keun-young Park8d01f632017-03-13 11:54:47 -0700448 if (voldService != nullptr && voldService->IsRunning()) {
449 ShutdownVold();
Keun-young Park2ba5c812017-03-29 12:54:40 -0700450 voldService->Stop();
Keun-young Park8d01f632017-03-13 11:54:47 -0700451 } else {
452 LOG(INFO) << "vold not running, skipping vold shutdown";
453 }
Keun-young Park2ba5c812017-03-29 12:54:40 -0700454 // logcat stopped here
Tom Cherry911b9b12017-07-27 16:20:58 -0700455 for (const auto& s : ServiceList::GetInstance().services_in_shutdown_order()) {
Keun-young Park2ba5c812017-03-29 12:54:40 -0700456 if (kill_after_apps.count(s->name())) s->Stop();
Tom Cherry911b9b12017-07-27 16:20:58 -0700457 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700458 // 4. sync, try umount, and optionally run fsck for user shutdown
Keun-young Park2ba5c812017-03-29 12:54:40 -0700459 sync();
Tom Cherryede0d532017-07-06 14:20:11 -0700460 UmountStat stat = TryUmountAndFsck(runFsck, shutdown_timeout - t.duration());
Keun-young Park2ba5c812017-03-29 12:54:40 -0700461 // Follow what linux shutdown is doing: one more sync with little bit delay
462 sync();
Keun-young Park30173872017-07-18 10:58:28 -0700463 if (!is_thermal_shutdown) std::this_thread::sleep_for(100ms);
Keun-young Park8d01f632017-03-13 11:54:47 -0700464 LogShutdownTime(stat, &t);
465 // Reboot regardless of umount status. If umount fails, fsck after reboot will fix it.
466 RebootSystem(cmd, rebootTarget);
467 abort();
468}
Tom Cherry98ad32a2017-04-17 16:34:20 -0700469
470bool HandlePowerctlMessage(const std::string& command) {
471 unsigned int cmd = 0;
Mark Salyzyn62909822017-10-09 09:27:16 -0700472 std::vector<std::string> cmd_params = Split(command, ",");
Tom Cherry98ad32a2017-04-17 16:34:20 -0700473 std::string reboot_target = "";
474 bool run_fsck = false;
475 bool command_invalid = false;
476
477 if (cmd_params.size() > 3) {
478 command_invalid = true;
479 } else if (cmd_params[0] == "shutdown") {
480 cmd = ANDROID_RB_POWEROFF;
Mark Salyzyn73e6b492017-08-14 15:56:53 -0700481 if (cmd_params.size() == 2) {
482 if (cmd_params[1] == "userrequested") {
483 // The shutdown reason is PowerManager.SHUTDOWN_USER_REQUESTED.
484 // Run fsck once the file system is remounted in read-only mode.
485 run_fsck = true;
486 } else if (cmd_params[1] == "thermal") {
Mark Salyzynbfd05b62017-09-26 11:10:12 -0700487 // Turn off sources of heat immediately.
488 TurnOffBacklight();
Mark Salyzyn73e6b492017-08-14 15:56:53 -0700489 // run_fsck is false to avoid delay
490 cmd = ANDROID_RB_THERMOFF;
491 }
Tom Cherry98ad32a2017-04-17 16:34:20 -0700492 }
493 } else if (cmd_params[0] == "reboot") {
494 cmd = ANDROID_RB_RESTART2;
495 if (cmd_params.size() >= 2) {
496 reboot_target = cmd_params[1];
497 // 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 }
506 }
Mark Salyzyn73e6b492017-08-14 15:56:53 -0700507 // If there is an additional parameter, pass it along
508 if ((cmd_params.size() == 3) && cmd_params[2].size()) {
Tom Cherry98ad32a2017-04-17 16:34:20 -0700509 reboot_target += "," + cmd_params[2];
510 }
511 }
Tom Cherry98ad32a2017-04-17 16:34:20 -0700512 } else {
513 command_invalid = true;
514 }
515 if (command_invalid) {
516 LOG(ERROR) << "powerctl: unrecognized command '" << command << "'";
517 return false;
518 }
519
Wei Wangeeab4912017-06-27 22:08:45 -0700520 LOG(INFO) << "Clear action queue and start shutdown trigger";
521 ActionManager::GetInstance().ClearQueue();
522 // Queue shutdown trigger first
523 ActionManager::GetInstance().QueueEventTrigger("shutdown");
524 // Queue built-in shutdown_done
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700525 auto shutdown_handler = [cmd, command, reboot_target, run_fsck](const BuiltinArguments&) {
Wei Wangeeab4912017-06-27 22:08:45 -0700526 DoReboot(cmd, command, reboot_target, run_fsck);
Tom Cherry557946e2017-08-01 13:50:23 -0700527 return Success();
Wei Wangeeab4912017-06-27 22:08:45 -0700528 };
529 ActionManager::GetInstance().QueueBuiltinAction(shutdown_handler, "shutdown_done");
530
531 // Skip wait for prop if it is in progress
532 ResetWaitForProp();
533
Tom Cherry3b81f2d2017-07-28 14:48:41 -0700534 // Clear EXEC flag if there is one pending
Tom Cherry911b9b12017-07-27 16:20:58 -0700535 for (const auto& s : ServiceList::GetInstance()) {
536 s->UnSetExec();
537 }
Wei Wangeeab4912017-06-27 22:08:45 -0700538
Tom Cherry98ad32a2017-04-17 16:34:20 -0700539 return true;
540}
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700541
542} // namespace init
543} // namespace android