2017-03-13 19:54:47 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2017 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2017-04-07 01:30:22 +02:00
|
|
|
|
|
|
|
#include "reboot.h"
|
|
|
|
|
2017-03-13 19:54:47 +01:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <fcntl.h>
|
2017-03-29 21:54:40 +02:00
|
|
|
#include <linux/fs.h>
|
2017-03-13 19:54:47 +01:00
|
|
|
#include <mntent.h>
|
2017-06-29 18:50:30 +02:00
|
|
|
#include <sys/capability.h>
|
2017-03-13 19:54:47 +01:00
|
|
|
#include <sys/cdefs.h>
|
2017-03-29 21:54:40 +02:00
|
|
|
#include <sys/ioctl.h>
|
2017-03-13 19:54:47 +01:00
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/reboot.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/syscall.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
|
|
|
#include <memory>
|
2017-03-28 01:07:02 +02:00
|
|
|
#include <set>
|
2017-03-13 19:54:47 +01:00
|
|
|
#include <thread>
|
|
|
|
#include <vector>
|
|
|
|
|
2017-07-06 23:20:11 +02:00
|
|
|
#include <android-base/chrono_utils.h>
|
2017-03-13 19:54:47 +01:00
|
|
|
#include <android-base/file.h>
|
2017-04-07 01:30:22 +02:00
|
|
|
#include <android-base/logging.h>
|
2017-03-13 19:54:47 +01:00
|
|
|
#include <android-base/macros.h>
|
2017-03-29 01:40:41 +02:00
|
|
|
#include <android-base/properties.h>
|
2017-03-13 19:54:47 +01:00
|
|
|
#include <android-base/stringprintf.h>
|
|
|
|
#include <android-base/strings.h>
|
2017-03-29 21:54:40 +02:00
|
|
|
#include <android-base/unique_fd.h>
|
2017-03-13 19:54:47 +01:00
|
|
|
#include <bootloader_message/bootloader_message.h>
|
|
|
|
#include <cutils/android_reboot.h>
|
|
|
|
#include <fs_mgr.h>
|
|
|
|
#include <logwrap/logwrap.h>
|
2017-04-14 00:17:24 +02:00
|
|
|
#include <private/android_filesystem_config.h>
|
2017-08-10 21:22:44 +02:00
|
|
|
#include <selinux/selinux.h>
|
2017-03-13 19:54:47 +01:00
|
|
|
|
2018-02-14 00:36:14 +01:00
|
|
|
#include "action_manager.h"
|
2017-06-29 18:50:30 +02:00
|
|
|
#include "capabilities.h"
|
2017-06-28 07:08:45 +02:00
|
|
|
#include "init.h"
|
2017-03-28 01:07:02 +02:00
|
|
|
#include "property_service.h"
|
2017-03-13 19:54:47 +01:00
|
|
|
#include "service.h"
|
2017-09-06 22:43:57 +02:00
|
|
|
#include "sigchld_handler.h"
|
2017-03-13 19:54:47 +01:00
|
|
|
|
2017-10-09 18:27:16 +02:00
|
|
|
using android::base::Split;
|
2017-03-13 19:54:47 +01:00
|
|
|
using android::base::StringPrintf;
|
2017-07-06 23:20:11 +02:00
|
|
|
using android::base::Timer;
|
2017-03-13 19:54:47 +01:00
|
|
|
|
2017-06-22 21:53:17 +02:00
|
|
|
namespace android {
|
|
|
|
namespace init {
|
|
|
|
|
2017-03-13 19:54:47 +01:00
|
|
|
// represents umount status during reboot / shutdown.
|
|
|
|
enum UmountStat {
|
|
|
|
/* umount succeeded. */
|
|
|
|
UMOUNT_STAT_SUCCESS = 0,
|
|
|
|
/* umount was not run. */
|
|
|
|
UMOUNT_STAT_SKIPPED = 1,
|
|
|
|
/* umount failed with timeout. */
|
|
|
|
UMOUNT_STAT_TIMEOUT = 2,
|
|
|
|
/* could not run due to error */
|
|
|
|
UMOUNT_STAT_ERROR = 3,
|
|
|
|
/* not used by init but reserved for other part to use this to represent the
|
|
|
|
the state where umount status before reboot is not found / available. */
|
|
|
|
UMOUNT_STAT_NOT_AVAILABLE = 4,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Utility for struct mntent
|
|
|
|
class MountEntry {
|
|
|
|
public:
|
2017-03-29 21:54:40 +02:00
|
|
|
explicit MountEntry(const mntent& entry)
|
2017-03-13 19:54:47 +01:00
|
|
|
: mnt_fsname_(entry.mnt_fsname),
|
|
|
|
mnt_dir_(entry.mnt_dir),
|
|
|
|
mnt_type_(entry.mnt_type),
|
2017-03-29 21:54:40 +02:00
|
|
|
mnt_opts_(entry.mnt_opts) {}
|
2017-03-13 19:54:47 +01:00
|
|
|
|
2017-09-25 19:55:39 +02:00
|
|
|
bool Umount(bool force) {
|
2018-02-15 23:26:58 +01:00
|
|
|
LOG(INFO) << "Unmounting " << mnt_fsname_ << ":" << mnt_dir_ << " opts " << mnt_opts_;
|
2017-09-25 19:55:39 +02:00
|
|
|
int r = umount2(mnt_dir_.c_str(), force ? MNT_FORCE : 0);
|
2017-03-29 21:54:40 +02:00
|
|
|
if (r == 0) {
|
2018-02-15 23:26:58 +01:00
|
|
|
LOG(INFO) << "Umounted " << mnt_fsname_ << ":" << mnt_dir_ << " opts " << mnt_opts_;
|
2017-03-29 21:54:40 +02:00
|
|
|
return true;
|
|
|
|
} else {
|
2018-02-15 23:26:58 +01:00
|
|
|
PLOG(WARNING) << "Cannot umount " << mnt_fsname_ << ":" << mnt_dir_ << " opts "
|
2017-03-29 21:54:40 +02:00
|
|
|
<< mnt_opts_;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2017-03-13 19:54:47 +01:00
|
|
|
|
2017-03-29 21:54:40 +02:00
|
|
|
void DoFsck() {
|
|
|
|
int st;
|
|
|
|
if (IsF2Fs()) {
|
|
|
|
const char* f2fs_argv[] = {
|
|
|
|
"/system/bin/fsck.f2fs", "-f", mnt_fsname_.c_str(),
|
|
|
|
};
|
|
|
|
android_fork_execvp_ext(arraysize(f2fs_argv), (char**)f2fs_argv, &st, true, LOG_KLOG,
|
|
|
|
true, nullptr, nullptr, 0);
|
|
|
|
} else if (IsExt4()) {
|
|
|
|
const char* ext4_argv[] = {
|
|
|
|
"/system/bin/e2fsck", "-f", "-y", mnt_fsname_.c_str(),
|
|
|
|
};
|
|
|
|
android_fork_execvp_ext(arraysize(ext4_argv), (char**)ext4_argv, &st, true, LOG_KLOG,
|
|
|
|
true, nullptr, nullptr, 0);
|
|
|
|
}
|
|
|
|
}
|
2017-03-13 19:54:47 +01:00
|
|
|
|
|
|
|
static bool IsBlockDevice(const struct mntent& mntent) {
|
|
|
|
return android::base::StartsWith(mntent.mnt_fsname, "/dev/block");
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool IsEmulatedDevice(const struct mntent& mntent) {
|
2017-03-29 21:54:40 +02:00
|
|
|
return android::base::StartsWith(mntent.mnt_fsname, "/data/");
|
2017-03-13 19:54:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-03-29 21:54:40 +02:00
|
|
|
bool IsF2Fs() const { return mnt_type_ == "f2fs"; }
|
|
|
|
|
|
|
|
bool IsExt4() const { return mnt_type_ == "ext4"; }
|
|
|
|
|
2017-03-13 19:54:47 +01:00
|
|
|
std::string mnt_fsname_;
|
|
|
|
std::string mnt_dir_;
|
|
|
|
std::string mnt_type_;
|
2017-03-29 21:54:40 +02:00
|
|
|
std::string mnt_opts_;
|
2017-03-13 19:54:47 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Turn off backlight while we are performing power down cleanup activities.
|
|
|
|
static void TurnOffBacklight() {
|
2018-01-19 22:01:53 +01:00
|
|
|
Service* service = ServiceList::GetInstance().FindService("blank_screen");
|
|
|
|
if (service == nullptr) {
|
|
|
|
LOG(WARNING) << "cannot find blank_screen in TurnOffBacklight";
|
2017-03-13 19:54:47 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-01-19 22:01:53 +01:00
|
|
|
service->Start();
|
2017-03-13 19:54:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void ShutdownVold() {
|
|
|
|
const char* vdc_argv[] = {"/system/bin/vdc", "volume", "shutdown"};
|
|
|
|
int status;
|
|
|
|
android_fork_execvp_ext(arraysize(vdc_argv), (char**)vdc_argv, &status, true, LOG_KLOG, true,
|
|
|
|
nullptr, nullptr, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void LogShutdownTime(UmountStat stat, Timer* t) {
|
2017-07-06 23:20:11 +02:00
|
|
|
LOG(WARNING) << "powerctl_shutdown_time_ms:" << std::to_string(t->duration().count()) << ":"
|
|
|
|
<< stat;
|
2017-03-13 19:54:47 +01:00
|
|
|
}
|
|
|
|
|
2017-09-06 22:43:57 +02:00
|
|
|
bool IsRebootCapable() {
|
2017-06-29 18:50:30 +02:00
|
|
|
if (!CAP_IS_SUPPORTED(CAP_SYS_BOOT)) {
|
|
|
|
PLOG(WARNING) << "CAP_SYS_BOOT is not supported";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
ScopedCaps caps(cap_get_proc());
|
|
|
|
if (!caps) {
|
|
|
|
PLOG(WARNING) << "cap_get_proc() failed";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
cap_flag_value_t value = CAP_SET;
|
|
|
|
if (cap_get_flag(caps.get(), CAP_SYS_BOOT, CAP_EFFECTIVE, &value) != 0) {
|
|
|
|
PLOG(WARNING) << "cap_get_flag(CAP_SYS_BOOT, EFFECTIVE) failed";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return value == CAP_SET;
|
|
|
|
}
|
|
|
|
|
2017-08-18 02:28:30 +02:00
|
|
|
void __attribute__((noreturn)) RebootSystem(unsigned int cmd, const std::string& rebootTarget) {
|
2017-03-23 23:33:16 +01:00
|
|
|
LOG(INFO) << "Reboot ending, jumping to kernel";
|
2017-06-29 18:50:30 +02:00
|
|
|
|
|
|
|
if (!IsRebootCapable()) {
|
|
|
|
// On systems where init does not have the capability of rebooting the
|
|
|
|
// device, just exit cleanly.
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2017-03-13 19:54:47 +01:00
|
|
|
switch (cmd) {
|
|
|
|
case ANDROID_RB_POWEROFF:
|
|
|
|
reboot(RB_POWER_OFF);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ANDROID_RB_RESTART2:
|
|
|
|
syscall(__NR_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,
|
|
|
|
LINUX_REBOOT_CMD_RESTART2, rebootTarget.c_str());
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ANDROID_RB_THERMOFF:
|
|
|
|
reboot(RB_POWER_OFF);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// In normal case, reboot should not return.
|
2017-08-18 02:28:30 +02:00
|
|
|
PLOG(ERROR) << "reboot call returned";
|
2017-03-13 19:54:47 +01:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find all read+write block devices and emulated devices in /proc/mounts
|
|
|
|
* and add them to correpsponding list.
|
|
|
|
*/
|
|
|
|
static bool FindPartitionsToUmount(std::vector<MountEntry>* blockDevPartitions,
|
2017-03-29 21:54:40 +02:00
|
|
|
std::vector<MountEntry>* emulatedPartitions, bool dump) {
|
2017-03-13 19:54:47 +01:00
|
|
|
std::unique_ptr<std::FILE, int (*)(std::FILE*)> fp(setmntent("/proc/mounts", "r"), endmntent);
|
|
|
|
if (fp == nullptr) {
|
|
|
|
PLOG(ERROR) << "Failed to open /proc/mounts";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
mntent* mentry;
|
|
|
|
while ((mentry = getmntent(fp.get())) != nullptr) {
|
2017-03-29 21:54:40 +02:00
|
|
|
if (dump) {
|
|
|
|
LOG(INFO) << "mount entry " << mentry->mnt_fsname << ":" << mentry->mnt_dir << " opts "
|
|
|
|
<< mentry->mnt_opts << " type " << mentry->mnt_type;
|
|
|
|
} else if (MountEntry::IsBlockDevice(*mentry) && hasmntopt(mentry, "rw")) {
|
2017-07-17 21:20:33 +02:00
|
|
|
std::string mount_dir(mentry->mnt_dir);
|
|
|
|
// These are R/O partitions changed to R/W after adb remount.
|
|
|
|
// Do not umount them as shutdown critical services may rely on them.
|
2017-07-25 19:52:08 +02:00
|
|
|
if (mount_dir != "/" && mount_dir != "/system" && mount_dir != "/vendor" &&
|
|
|
|
mount_dir != "/oem") {
|
2017-07-17 21:20:33 +02:00
|
|
|
blockDevPartitions->emplace(blockDevPartitions->begin(), *mentry);
|
|
|
|
}
|
2017-03-13 19:54:47 +01:00
|
|
|
} else if (MountEntry::IsEmulatedDevice(*mentry)) {
|
2017-03-29 21:54:40 +02:00
|
|
|
emulatedPartitions->emplace(emulatedPartitions->begin(), *mentry);
|
2017-03-13 19:54:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-04-22 02:29:26 +02:00
|
|
|
static void DumpUmountDebuggingInfo(bool dump_all) {
|
2017-03-29 21:54:40 +02:00
|
|
|
int status;
|
|
|
|
if (!security_getenforce()) {
|
|
|
|
LOG(INFO) << "Run lsof";
|
|
|
|
const char* lsof_argv[] = {"/system/bin/lsof"};
|
|
|
|
android_fork_execvp_ext(arraysize(lsof_argv), (char**)lsof_argv, &status, true, LOG_KLOG,
|
|
|
|
true, nullptr, nullptr, 0);
|
|
|
|
}
|
|
|
|
FindPartitionsToUmount(nullptr, nullptr, true);
|
2017-04-22 02:29:26 +02:00
|
|
|
if (dump_all) {
|
|
|
|
// dump current tasks, this log can be lengthy, so only dump with dump_all
|
|
|
|
android::base::WriteStringToFile("t", "/proc/sysrq-trigger");
|
|
|
|
}
|
2017-03-29 21:54:40 +02:00
|
|
|
}
|
2017-03-13 19:54:47 +01:00
|
|
|
|
2017-07-06 23:20:11 +02:00
|
|
|
static UmountStat UmountPartitions(std::chrono::milliseconds timeout) {
|
2017-03-29 21:54:40 +02:00
|
|
|
Timer t;
|
|
|
|
/* data partition needs all pending writes to be completed and all emulated partitions
|
|
|
|
* umounted.If the current waiting is not good enough, give
|
|
|
|
* up and leave it to e2fsck after reboot to fix it.
|
|
|
|
*/
|
2017-03-13 19:54:47 +01:00
|
|
|
while (true) {
|
2017-03-29 21:54:40 +02:00
|
|
|
std::vector<MountEntry> block_devices;
|
|
|
|
std::vector<MountEntry> emulated_devices;
|
|
|
|
if (!FindPartitionsToUmount(&block_devices, &emulated_devices, false)) {
|
|
|
|
return UMOUNT_STAT_ERROR;
|
2017-03-13 19:54:47 +01:00
|
|
|
}
|
2017-03-29 21:54:40 +02:00
|
|
|
if (block_devices.size() == 0) {
|
2017-08-16 23:01:46 +02:00
|
|
|
return UMOUNT_STAT_SUCCESS;
|
2017-03-29 21:54:40 +02:00
|
|
|
}
|
2017-08-16 23:01:46 +02:00
|
|
|
bool unmount_done = true;
|
|
|
|
if (emulated_devices.size() > 0) {
|
2017-10-24 00:32:03 +02:00
|
|
|
for (auto& entry : emulated_devices) {
|
|
|
|
if (!entry.Umount(false)) unmount_done = false;
|
|
|
|
}
|
2017-08-16 23:01:46 +02:00
|
|
|
if (unmount_done) {
|
|
|
|
sync();
|
|
|
|
}
|
2017-03-29 21:54:40 +02:00
|
|
|
}
|
2017-10-24 00:32:03 +02:00
|
|
|
for (auto& entry : block_devices) {
|
|
|
|
if (!entry.Umount(timeout == 0ms)) unmount_done = false;
|
|
|
|
}
|
2017-08-16 23:01:46 +02:00
|
|
|
if (unmount_done) {
|
|
|
|
return UMOUNT_STAT_SUCCESS;
|
2017-03-29 21:54:40 +02:00
|
|
|
}
|
2017-08-16 23:01:46 +02:00
|
|
|
if ((timeout < t.duration())) { // try umount at least once
|
|
|
|
return UMOUNT_STAT_TIMEOUT;
|
2017-03-29 21:54:40 +02:00
|
|
|
}
|
|
|
|
std::this_thread::sleep_for(100ms);
|
2017-03-13 19:54:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-27 20:21:09 +02:00
|
|
|
static void KillAllProcesses() { android::base::WriteStringToFile("i", "/proc/sysrq-trigger"); }
|
|
|
|
|
2017-03-13 19:54:47 +01:00
|
|
|
/* Try umounting all emulated file systems R/W block device cfile systems.
|
|
|
|
* This will just try umount and give it up if it fails.
|
|
|
|
* For fs like ext4, this is ok as file system will be marked as unclean shutdown
|
|
|
|
* and necessary check can be done at the next reboot.
|
|
|
|
* For safer shutdown, caller needs to make sure that
|
|
|
|
* all processes / emulated partition for the target fs are all cleaned-up.
|
|
|
|
*
|
|
|
|
* return true when umount was successful. false when timed out.
|
|
|
|
*/
|
2017-07-06 23:20:11 +02:00
|
|
|
static UmountStat TryUmountAndFsck(bool runFsck, std::chrono::milliseconds timeout) {
|
2017-03-27 20:21:09 +02:00
|
|
|
Timer t;
|
2017-03-29 21:54:40 +02:00
|
|
|
std::vector<MountEntry> block_devices;
|
|
|
|
std::vector<MountEntry> emulated_devices;
|
2017-03-13 19:54:47 +01:00
|
|
|
|
2017-03-29 21:54:40 +02:00
|
|
|
if (runFsck && !FindPartitionsToUmount(&block_devices, &emulated_devices, false)) {
|
2017-03-13 19:54:47 +01:00
|
|
|
return UMOUNT_STAT_ERROR;
|
|
|
|
}
|
2017-03-29 21:54:40 +02:00
|
|
|
|
2017-07-06 23:20:11 +02:00
|
|
|
UmountStat stat = UmountPartitions(timeout - t.duration());
|
2017-03-29 21:54:40 +02:00
|
|
|
if (stat != UMOUNT_STAT_SUCCESS) {
|
|
|
|
LOG(INFO) << "umount timeout, last resort, kill all and try";
|
2017-07-19 03:52:25 +02:00
|
|
|
if (DUMP_ON_UMOUNT_FAILURE) DumpUmountDebuggingInfo(true);
|
2017-03-27 20:21:09 +02:00
|
|
|
KillAllProcesses();
|
2017-03-29 21:54:40 +02:00
|
|
|
// even if it succeeds, still it is timeout and do not run fsck with all processes killed
|
2017-07-19 03:52:25 +02:00
|
|
|
UmountStat st = UmountPartitions(0ms);
|
|
|
|
if ((st != UMOUNT_STAT_SUCCESS) && DUMP_ON_UMOUNT_FAILURE) DumpUmountDebuggingInfo(false);
|
2017-03-13 19:54:47 +01:00
|
|
|
}
|
2017-03-29 21:54:40 +02:00
|
|
|
|
2017-03-13 19:54:47 +01:00
|
|
|
if (stat == UMOUNT_STAT_SUCCESS && runFsck) {
|
2017-03-29 21:54:40 +02:00
|
|
|
// fsck part is excluded from timeout check. It only runs for user initiated shutdown
|
|
|
|
// and should not affect reboot time.
|
|
|
|
for (auto& entry : block_devices) {
|
|
|
|
entry.DoFsck();
|
2017-03-13 19:54:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return stat;
|
|
|
|
}
|
|
|
|
|
2018-03-20 00:19:01 +01:00
|
|
|
void DoReboot(unsigned int cmd, const std::string& reason, const std::string& rebootTarget,
|
|
|
|
bool runFsck) {
|
2017-03-13 19:54:47 +01:00
|
|
|
Timer t;
|
2018-03-20 00:19:01 +01:00
|
|
|
LOG(INFO) << "Reboot start, reason: " << reason << ", rebootTarget: " << rebootTarget;
|
|
|
|
|
|
|
|
// Ensure last reboot reason is reduced to canonical
|
|
|
|
// alias reported in bootloader or system boot reason.
|
|
|
|
size_t skip = 0;
|
|
|
|
std::vector<std::string> reasons = Split(reason, ",");
|
|
|
|
if (reasons.size() >= 2 && reasons[0] == "reboot" &&
|
|
|
|
(reasons[1] == "recovery" || reasons[1] == "bootloader" || reasons[1] == "cold" ||
|
|
|
|
reasons[1] == "hard" || reasons[1] == "warm")) {
|
|
|
|
skip = strlen("reboot,");
|
|
|
|
}
|
|
|
|
property_set(LAST_REBOOT_REASON_PROPERTY, reason.c_str() + skip);
|
|
|
|
sync();
|
|
|
|
|
|
|
|
bool is_thermal_shutdown = cmd == ANDROID_RB_THERMOFF;
|
|
|
|
|
|
|
|
auto shutdown_timeout = 0ms;
|
|
|
|
if (!SHUTDOWN_ZERO_TIMEOUT) {
|
|
|
|
if (is_thermal_shutdown) {
|
|
|
|
constexpr unsigned int thermal_shutdown_timeout = 1;
|
|
|
|
shutdown_timeout = std::chrono::seconds(thermal_shutdown_timeout);
|
|
|
|
} else {
|
|
|
|
constexpr unsigned int shutdown_timeout_default = 6;
|
|
|
|
auto shutdown_timeout_property = android::base::GetUintProperty(
|
|
|
|
"ro.build.shutdown_timeout", shutdown_timeout_default);
|
|
|
|
shutdown_timeout = std::chrono::seconds(shutdown_timeout_property);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LOG(INFO) << "Shutdown timeout: " << shutdown_timeout.count() << " ms";
|
|
|
|
|
2017-03-28 01:07:02 +02:00
|
|
|
// keep debugging tools until non critical ones are all gone.
|
|
|
|
const std::set<std::string> kill_after_apps{"tombstoned", "logd", "adbd"};
|
|
|
|
// watchdogd is a vendor specific component but should be alive to complete shutdown safely.
|
2017-07-05 20:38:44 +02:00
|
|
|
const std::set<std::string> to_starts{"watchdogd"};
|
2017-07-28 01:20:58 +02:00
|
|
|
for (const auto& s : ServiceList::GetInstance()) {
|
2017-03-28 01:07:02 +02:00
|
|
|
if (kill_after_apps.count(s->name())) {
|
|
|
|
s->SetShutdownCritical();
|
|
|
|
} else if (to_starts.count(s->name())) {
|
2017-08-25 19:36:52 +02:00
|
|
|
if (auto result = s->Start(); !result) {
|
|
|
|
LOG(ERROR) << "Could not start shutdown 'to_start' service '" << s->name()
|
|
|
|
<< "': " << result.error();
|
|
|
|
}
|
2017-03-28 01:07:02 +02:00
|
|
|
s->SetShutdownCritical();
|
2017-07-05 20:38:44 +02:00
|
|
|
} else if (s->IsShutdownCritical()) {
|
2017-08-25 19:36:52 +02:00
|
|
|
// Start shutdown critical service if not started.
|
|
|
|
if (auto result = s->Start(); !result) {
|
|
|
|
LOG(ERROR) << "Could not start shutdown critical service '" << s->name()
|
|
|
|
<< "': " << result.error();
|
|
|
|
}
|
2017-03-13 19:54:47 +01:00
|
|
|
}
|
2017-07-28 01:20:58 +02:00
|
|
|
}
|
2017-03-28 01:07:02 +02:00
|
|
|
|
2018-01-19 22:01:53 +01:00
|
|
|
// remaining operations (specifically fsck) may take a substantial duration
|
2018-03-20 00:19:01 +01:00
|
|
|
if (cmd == ANDROID_RB_POWEROFF || is_thermal_shutdown) {
|
2018-01-19 22:01:53 +01:00
|
|
|
TurnOffBacklight();
|
|
|
|
}
|
|
|
|
|
2017-07-28 01:20:58 +02:00
|
|
|
Service* bootAnim = ServiceList::GetInstance().FindService("bootanim");
|
|
|
|
Service* surfaceFlinger = ServiceList::GetInstance().FindService("surfaceflinger");
|
2017-03-28 01:07:02 +02:00
|
|
|
if (bootAnim != nullptr && surfaceFlinger != nullptr && surfaceFlinger->IsRunning()) {
|
2017-07-28 01:20:58 +02:00
|
|
|
// will not check animation class separately
|
|
|
|
for (const auto& service : ServiceList::GetInstance()) {
|
|
|
|
if (service->classnames().count("animation")) service->SetShutdownCritical();
|
|
|
|
}
|
2017-03-13 19:54:47 +01:00
|
|
|
}
|
2017-03-28 01:07:02 +02:00
|
|
|
|
2017-03-13 19:54:47 +01:00
|
|
|
// optional shutdown step
|
|
|
|
// 1. terminate all services except shutdown critical ones. wait for delay to finish
|
2017-07-18 19:58:28 +02:00
|
|
|
if (shutdown_timeout > 0ms) {
|
2017-03-13 19:54:47 +01:00
|
|
|
LOG(INFO) << "terminating init services";
|
|
|
|
|
|
|
|
// Ask all services to terminate except shutdown critical ones.
|
2017-07-28 01:20:58 +02:00
|
|
|
for (const auto& s : ServiceList::GetInstance().services_in_shutdown_order()) {
|
2017-03-13 19:54:47 +01:00
|
|
|
if (!s->IsShutdownCritical()) s->Terminate();
|
2017-07-28 01:20:58 +02:00
|
|
|
}
|
2017-03-13 19:54:47 +01:00
|
|
|
|
|
|
|
int service_count = 0;
|
2017-07-18 19:58:28 +02:00
|
|
|
// Only wait up to half of timeout here
|
|
|
|
auto termination_wait_timeout = shutdown_timeout / 2;
|
2017-07-06 23:20:11 +02:00
|
|
|
while (t.duration() < termination_wait_timeout) {
|
2017-07-29 00:22:23 +02:00
|
|
|
ReapAnyOutstandingChildren();
|
2017-03-13 19:54:47 +01:00
|
|
|
|
|
|
|
service_count = 0;
|
2017-07-28 01:20:58 +02:00
|
|
|
for (const auto& s : ServiceList::GetInstance()) {
|
2017-03-13 19:54:47 +01:00
|
|
|
// Count the number of services running except shutdown critical.
|
|
|
|
// Exclude the console as it will ignore the SIGTERM signal
|
|
|
|
// and not exit.
|
|
|
|
// Note: SVC_CONSOLE actually means "requires console" but
|
|
|
|
// it is only used by the shell.
|
|
|
|
if (!s->IsShutdownCritical() && s->pid() != 0 && (s->flags() & SVC_CONSOLE) == 0) {
|
|
|
|
service_count++;
|
|
|
|
}
|
2017-07-28 01:20:58 +02:00
|
|
|
}
|
2017-03-13 19:54:47 +01:00
|
|
|
|
|
|
|
if (service_count == 0) {
|
|
|
|
// All terminable services terminated. We can exit early.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait a bit before recounting the number or running services.
|
|
|
|
std::this_thread::sleep_for(50ms);
|
|
|
|
}
|
|
|
|
LOG(INFO) << "Terminating running services took " << t
|
|
|
|
<< " with remaining services:" << service_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
// minimum safety steps before restarting
|
|
|
|
// 2. kill all services except ones that are necessary for the shutdown sequence.
|
2017-07-28 01:20:58 +02:00
|
|
|
for (const auto& s : ServiceList::GetInstance().services_in_shutdown_order()) {
|
2017-03-29 21:54:40 +02:00
|
|
|
if (!s->IsShutdownCritical()) s->Stop();
|
2017-07-28 01:20:58 +02:00
|
|
|
}
|
2017-07-29 00:22:23 +02:00
|
|
|
ReapAnyOutstandingChildren();
|
2017-03-13 19:54:47 +01:00
|
|
|
|
|
|
|
// 3. send volume shutdown to vold
|
2017-07-28 01:20:58 +02:00
|
|
|
Service* voldService = ServiceList::GetInstance().FindService("vold");
|
2017-03-13 19:54:47 +01:00
|
|
|
if (voldService != nullptr && voldService->IsRunning()) {
|
|
|
|
ShutdownVold();
|
2017-03-29 21:54:40 +02:00
|
|
|
voldService->Stop();
|
2017-03-13 19:54:47 +01:00
|
|
|
} else {
|
|
|
|
LOG(INFO) << "vold not running, skipping vold shutdown";
|
|
|
|
}
|
2017-03-29 21:54:40 +02:00
|
|
|
// logcat stopped here
|
2017-07-28 01:20:58 +02:00
|
|
|
for (const auto& s : ServiceList::GetInstance().services_in_shutdown_order()) {
|
2017-03-29 21:54:40 +02:00
|
|
|
if (kill_after_apps.count(s->name())) s->Stop();
|
2017-07-28 01:20:58 +02:00
|
|
|
}
|
2017-03-13 19:54:47 +01:00
|
|
|
// 4. sync, try umount, and optionally run fsck for user shutdown
|
2018-03-15 18:22:40 +01:00
|
|
|
{
|
|
|
|
Timer sync_timer;
|
|
|
|
LOG(INFO) << "sync() before umount...";
|
|
|
|
sync();
|
|
|
|
LOG(INFO) << "sync() before umount took" << sync_timer;
|
|
|
|
}
|
2017-07-06 23:20:11 +02:00
|
|
|
UmountStat stat = TryUmountAndFsck(runFsck, shutdown_timeout - t.duration());
|
2017-03-29 21:54:40 +02:00
|
|
|
// Follow what linux shutdown is doing: one more sync with little bit delay
|
2018-03-15 18:22:40 +01:00
|
|
|
{
|
|
|
|
Timer sync_timer;
|
|
|
|
LOG(INFO) << "sync() after umount...";
|
|
|
|
sync();
|
|
|
|
LOG(INFO) << "sync() after umount took" << sync_timer;
|
|
|
|
}
|
2018-03-20 00:19:01 +01:00
|
|
|
if (!is_thermal_shutdown) std::this_thread::sleep_for(100ms);
|
2017-03-13 19:54:47 +01:00
|
|
|
LogShutdownTime(stat, &t);
|
|
|
|
// Reboot regardless of umount status. If umount fails, fsck after reboot will fix it.
|
|
|
|
RebootSystem(cmd, rebootTarget);
|
|
|
|
abort();
|
|
|
|
}
|
2017-04-18 01:34:20 +02:00
|
|
|
|
|
|
|
bool HandlePowerctlMessage(const std::string& command) {
|
|
|
|
unsigned int cmd = 0;
|
2017-10-09 18:27:16 +02:00
|
|
|
std::vector<std::string> cmd_params = Split(command, ",");
|
2017-04-18 01:34:20 +02:00
|
|
|
std::string reboot_target = "";
|
|
|
|
bool run_fsck = false;
|
|
|
|
bool command_invalid = false;
|
|
|
|
|
|
|
|
if (cmd_params.size() > 3) {
|
|
|
|
command_invalid = true;
|
|
|
|
} else if (cmd_params[0] == "shutdown") {
|
|
|
|
cmd = ANDROID_RB_POWEROFF;
|
2017-08-15 00:56:53 +02:00
|
|
|
if (cmd_params.size() == 2) {
|
|
|
|
if (cmd_params[1] == "userrequested") {
|
|
|
|
// The shutdown reason is PowerManager.SHUTDOWN_USER_REQUESTED.
|
|
|
|
// Run fsck once the file system is remounted in read-only mode.
|
|
|
|
run_fsck = true;
|
|
|
|
} else if (cmd_params[1] == "thermal") {
|
2017-09-26 20:10:12 +02:00
|
|
|
// Turn off sources of heat immediately.
|
|
|
|
TurnOffBacklight();
|
2017-08-15 00:56:53 +02:00
|
|
|
// run_fsck is false to avoid delay
|
|
|
|
cmd = ANDROID_RB_THERMOFF;
|
|
|
|
}
|
2017-04-18 01:34:20 +02:00
|
|
|
}
|
|
|
|
} else if (cmd_params[0] == "reboot") {
|
|
|
|
cmd = ANDROID_RB_RESTART2;
|
|
|
|
if (cmd_params.size() >= 2) {
|
|
|
|
reboot_target = cmd_params[1];
|
|
|
|
// When rebooting to the bootloader notify the bootloader writing
|
|
|
|
// also the BCB.
|
|
|
|
if (reboot_target == "bootloader") {
|
|
|
|
std::string err;
|
|
|
|
if (!write_reboot_bootloader(&err)) {
|
|
|
|
LOG(ERROR) << "reboot-bootloader: Error writing "
|
|
|
|
"bootloader_message: "
|
|
|
|
<< err;
|
|
|
|
}
|
|
|
|
}
|
2017-08-15 00:56:53 +02:00
|
|
|
// If there is an additional parameter, pass it along
|
|
|
|
if ((cmd_params.size() == 3) && cmd_params[2].size()) {
|
2017-04-18 01:34:20 +02:00
|
|
|
reboot_target += "," + cmd_params[2];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
command_invalid = true;
|
|
|
|
}
|
|
|
|
if (command_invalid) {
|
|
|
|
LOG(ERROR) << "powerctl: unrecognized command '" << command << "'";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-06-28 07:08:45 +02:00
|
|
|
LOG(INFO) << "Clear action queue and start shutdown trigger";
|
|
|
|
ActionManager::GetInstance().ClearQueue();
|
|
|
|
// Queue shutdown trigger first
|
|
|
|
ActionManager::GetInstance().QueueEventTrigger("shutdown");
|
|
|
|
// Queue built-in shutdown_done
|
2017-09-13 00:58:47 +02:00
|
|
|
auto shutdown_handler = [cmd, command, reboot_target, run_fsck](const BuiltinArguments&) {
|
2017-06-28 07:08:45 +02:00
|
|
|
DoReboot(cmd, command, reboot_target, run_fsck);
|
2017-08-01 22:50:23 +02:00
|
|
|
return Success();
|
2017-06-28 07:08:45 +02:00
|
|
|
};
|
|
|
|
ActionManager::GetInstance().QueueBuiltinAction(shutdown_handler, "shutdown_done");
|
|
|
|
|
|
|
|
// Skip wait for prop if it is in progress
|
|
|
|
ResetWaitForProp();
|
|
|
|
|
2017-07-28 23:48:41 +02:00
|
|
|
// Clear EXEC flag if there is one pending
|
2017-07-28 01:20:58 +02:00
|
|
|
for (const auto& s : ServiceList::GetInstance()) {
|
|
|
|
s->UnSetExec();
|
|
|
|
}
|
2017-06-28 07:08:45 +02:00
|
|
|
|
2017-04-18 01:34:20 +02:00
|
|
|
return true;
|
|
|
|
}
|
2017-06-22 21:53:17 +02:00
|
|
|
|
|
|
|
} // namespace init
|
|
|
|
} // namespace android
|