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>
|
zram: support zram_writeback
This patch supports zram_writeback enabled by fstab in two ways.
e.g.,
/dev/block/zram0 none swap defaults
zramsize=1073741824,max_comp_streams=8,zram_loopback_path=/data/unencrypted/zram_swap
==> loopback size is 512MB by default.
Or,
/dev/block/zram0 none swap defaults
zramsize=1073741824,max_comp_streams=8,zram_loopback_path=/data/unencrypted/zram_swap,zram_loopback_size=1G
==> loopback size can be specified by "zram_loopback_size=%s" with "GB" or "MB".
Or,
/dev/block/zram0 none swap defaults
zramsize=1073741824,max_comp_streams=8,zram_backing_dev_path=/dev/block/partition
Bug: 74582279
Bug: 122659265
Change-Id: I66a2e6953b4743a34cf732dd0f5b5256c901f247
Signed-off-by: Jaegeuk Kim <jaegeuk@google.com>
2018-11-20 22:27:06 +01:00
|
|
|
#include <linux/loop.h>
|
2019-04-16 12:46:24 +02:00
|
|
|
#include <mntent.h>
|
|
|
|
#include <semaphore.h>
|
2019-10-09 16:23:02 +02:00
|
|
|
#include <stdlib.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/stat.h>
|
2019-04-16 12:46:24 +02:00
|
|
|
#include <sys/swap.h>
|
2017-03-13 19:54:47 +01:00
|
|
|
#include <sys/syscall.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
2019-10-09 16:23:02 +02:00
|
|
|
#include <chrono>
|
2017-03-13 19:54:47 +01:00
|
|
|
#include <memory>
|
2017-03-28 01:07:02 +02:00
|
|
|
#include <set>
|
2017-03-13 19:54:47 +01:00
|
|
|
#include <thread>
|
|
|
|
#include <vector>
|
|
|
|
|
2019-11-14 02:21:24 +01:00
|
|
|
#include <InitProperties.sysprop.h>
|
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>
|
2019-10-09 16:23:02 +02:00
|
|
|
#include <android-base/scopeguard.h>
|
2017-03-13 19:54:47 +01:00
|
|
|
#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
|
|
|
|
2019-10-07 17:26:33 +02:00
|
|
|
#include "action.h"
|
2018-02-14 00:36:14 +01:00
|
|
|
#include "action_manager.h"
|
2019-10-07 17:26:33 +02:00
|
|
|
#include "builtin_arguments.h"
|
2017-06-28 07:08:45 +02:00
|
|
|
#include "init.h"
|
2019-11-06 22:40:31 +01:00
|
|
|
#include "mount_namespace.h"
|
init: handle property messages asynchronously #2
A previous change moved property_service into its own thread, since
there was otherwise a deadlock whenever a process called by init would
try to set a property. This new thread, however, would send a message
via a blocking socket to init for each property that it received,
since init may need to take action depending on which property it is.
Unfortunately, this means that the deadlock is still possible, the
only difference is the socket's buffer must be filled before init deadlocks.
This change, therefore, adds the following:
1) A lock for instructing init to reboot
2) A lock for waiting on properties
3) A lock for queueing new properties
A previous version of this change was reverted and added locks around
all service operations and allowed the property thread to spawn
services directly. This was complex due to the fact that this code
was not designed to be multi-threaded. It was reverted due to
apparent issues during reboot. This change keeps a queue of processes
pending control messages, which it will then handle in the future. It
is less flexible but safer.
Bug: 146877356
Bug: 148236233
Bug: 150863651
Bug: 151251827
Test: multiple reboot tests, safely restarting hwservicemanager
Change-Id: Ice773436e85d3bf636bb0a892f3f6002bdf996b6
2020-03-12 22:29:25 +01:00
|
|
|
#include "property_service.h"
|
2018-08-03 22:36:18 +02:00
|
|
|
#include "reboot_utils.h"
|
2017-03-13 19:54:47 +01:00
|
|
|
#include "service.h"
|
2019-06-26 19:46:20 +02:00
|
|
|
#include "service_list.h"
|
2017-09-06 22:43:57 +02:00
|
|
|
#include "sigchld_handler.h"
|
2019-10-09 16:23:02 +02:00
|
|
|
#include "util.h"
|
2017-03-13 19:54:47 +01:00
|
|
|
|
2019-09-19 20:16:19 +02:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2019-11-13 22:47:06 +01:00
|
|
|
using android::base::boot_clock;
|
2019-01-08 19:39:00 +01:00
|
|
|
using android::base::GetBoolProperty;
|
2020-03-25 01:06:51 +01:00
|
|
|
using android::base::GetUintProperty;
|
2019-11-13 22:47:06 +01:00
|
|
|
using android::base::SetProperty;
|
2017-10-09 18:27:16 +02:00
|
|
|
using android::base::Split;
|
2017-07-06 23:20:11 +02:00
|
|
|
using android::base::Timer;
|
zram: support zram_writeback
This patch supports zram_writeback enabled by fstab in two ways.
e.g.,
/dev/block/zram0 none swap defaults
zramsize=1073741824,max_comp_streams=8,zram_loopback_path=/data/unencrypted/zram_swap
==> loopback size is 512MB by default.
Or,
/dev/block/zram0 none swap defaults
zramsize=1073741824,max_comp_streams=8,zram_loopback_path=/data/unencrypted/zram_swap,zram_loopback_size=1G
==> loopback size can be specified by "zram_loopback_size=%s" with "GB" or "MB".
Or,
/dev/block/zram0 none swap defaults
zramsize=1073741824,max_comp_streams=8,zram_backing_dev_path=/dev/block/partition
Bug: 74582279
Bug: 122659265
Change-Id: I66a2e6953b4743a34cf732dd0f5b5256c901f247
Signed-off-by: Jaegeuk Kim <jaegeuk@google.com>
2018-11-20 22:27:06 +01:00
|
|
|
using android::base::unique_fd;
|
2019-11-13 22:47:06 +01:00
|
|
|
using android::base::WaitForProperty;
|
2019-04-16 12:46:24 +02:00
|
|
|
using android::base::WriteStringToFile;
|
2017-03-13 19:54:47 +01:00
|
|
|
|
2017-06-22 21:53:17 +02:00
|
|
|
namespace android {
|
|
|
|
namespace init {
|
|
|
|
|
2019-10-07 17:26:33 +02:00
|
|
|
static bool shutting_down = false;
|
|
|
|
|
2019-10-09 16:23:02 +02:00
|
|
|
static const std::set<std::string> kDebuggingServices{"tombstoned", "logd", "adbd", "console"};
|
|
|
|
|
2020-11-27 19:57:44 +01:00
|
|
|
static std::set<std::string> GetPostDataDebuggingServices() {
|
|
|
|
std::set<std::string> ret;
|
2019-10-09 16:23:02 +02:00
|
|
|
for (const auto& s : ServiceList::GetInstance()) {
|
2020-11-27 19:57:44 +01:00
|
|
|
if (kDebuggingServices.count(s->name()) && s->is_post_data()) {
|
|
|
|
ret.insert(s->name());
|
2019-10-09 16:23:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-02-26 16:18:56 +01:00
|
|
|
static void PersistRebootReason(const char* reason, bool write_to_property) {
|
|
|
|
if (write_to_property) {
|
|
|
|
SetProperty(LAST_REBOOT_REASON_PROPERTY, reason);
|
|
|
|
}
|
2020-04-01 01:31:37 +02:00
|
|
|
auto fd = unique_fd(TEMP_FAILURE_RETRY(open(
|
|
|
|
LAST_REBOOT_REASON_FILE, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_BINARY, 0666)));
|
|
|
|
if (!fd.ok()) {
|
|
|
|
PLOG(ERROR) << "Could not open '" << LAST_REBOOT_REASON_FILE
|
|
|
|
<< "' to persist reboot reason";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
WriteStringToFd(reason, fd);
|
|
|
|
fsync(fd.get());
|
2020-02-19 17:21:11 +01:00
|
|
|
}
|
|
|
|
|
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[] = {
|
2019-01-15 08:00:56 +01:00
|
|
|
"/system/bin/fsck.f2fs",
|
|
|
|
"-a",
|
|
|
|
mnt_fsname_.c_str(),
|
2017-03-29 21:54:40 +02:00
|
|
|
};
|
2019-09-26 01:23:50 +02:00
|
|
|
logwrap_fork_execvp(arraysize(f2fs_argv), f2fs_argv, &st, false, LOG_KLOG, true,
|
|
|
|
nullptr);
|
2017-03-29 21:54:40 +02:00
|
|
|
} else if (IsExt4()) {
|
|
|
|
const char* ext4_argv[] = {
|
2019-01-15 08:00:56 +01:00
|
|
|
"/system/bin/e2fsck",
|
|
|
|
"-y",
|
|
|
|
mnt_fsname_.c_str(),
|
2017-03-29 21:54:40 +02:00
|
|
|
};
|
2019-09-26 01:23:50 +02:00
|
|
|
logwrap_fork_execvp(arraysize(ext4_argv), ext4_argv, &st, false, LOG_KLOG, true,
|
|
|
|
nullptr);
|
2017-03-29 21:54:40 +02:00
|
|
|
}
|
|
|
|
}
|
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.
|
2020-03-10 19:47:24 +01:00
|
|
|
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;
|
|
|
|
}
|
2020-02-05 19:49:33 +01:00
|
|
|
if (auto result = service->Start(); !result.ok()) {
|
2018-10-11 19:38:05 +02:00
|
|
|
LOG(WARNING) << "Could not start blank_screen service: " << result.error();
|
|
|
|
}
|
2017-03-13 19:54:47 +01:00
|
|
|
}
|
|
|
|
|
2019-12-02 12:51:39 +01:00
|
|
|
static Result<void> CallVdc(const std::string& system, const std::string& cmd) {
|
2020-04-16 22:37:03 +02:00
|
|
|
LOG(INFO) << "Calling /system/bin/vdc " << system << " " << cmd;
|
2019-12-02 12:51:39 +01:00
|
|
|
const char* vdc_argv[] = {"/system/bin/vdc", system.c_str(), cmd.c_str()};
|
2017-03-13 19:54:47 +01:00
|
|
|
int status;
|
2019-10-23 21:11:32 +02:00
|
|
|
if (logwrap_fork_execvp(arraysize(vdc_argv), vdc_argv, &status, false, LOG_KLOG, true,
|
|
|
|
nullptr) != 0) {
|
2019-12-02 12:51:39 +01:00
|
|
|
return ErrnoError() << "Failed to call '/system/bin/vdc " << system << " " << cmd << "'";
|
2019-10-23 21:11:32 +02:00
|
|
|
}
|
|
|
|
if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
|
|
|
|
return {};
|
|
|
|
}
|
2019-12-02 12:51:39 +01:00
|
|
|
return Error() << "'/system/bin/vdc " << system << " " << cmd << "' failed : " << status;
|
2017-03-13 19:54:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-09-19 20:16:19 +02:00
|
|
|
static bool IsDataMounted() {
|
|
|
|
std::unique_ptr<std::FILE, int (*)(std::FILE*)> fp(setmntent("/proc/mounts", "re"), endmntent);
|
|
|
|
if (fp == nullptr) {
|
|
|
|
PLOG(ERROR) << "Failed to open /proc/mounts";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
mntent* mentry;
|
|
|
|
while ((mentry = getmntent(fp.get())) != nullptr) {
|
|
|
|
if (mentry->mnt_dir == "/data"s) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find all read+write block devices and emulated devices in /proc/mounts and add them to
|
|
|
|
// the correpsponding list.
|
2019-10-25 00:18:39 +02:00
|
|
|
static bool FindPartitionsToUmount(std::vector<MountEntry>* block_dev_partitions,
|
|
|
|
std::vector<MountEntry>* emulated_partitions, bool dump) {
|
2018-10-03 22:13:41 +02:00
|
|
|
std::unique_ptr<std::FILE, int (*)(std::FILE*)> fp(setmntent("/proc/mounts", "re"), endmntent);
|
2017-03-13 19:54:47 +01:00
|
|
|
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") {
|
2019-10-25 00:18:39 +02:00
|
|
|
block_dev_partitions->emplace(block_dev_partitions->begin(), *mentry);
|
2017-07-17 21:20:33 +02:00
|
|
|
}
|
2017-03-13 19:54:47 +01:00
|
|
|
} else if (MountEntry::IsEmulatedDevice(*mentry)) {
|
2019-10-25 00:18:39 +02:00
|
|
|
emulated_partitions->emplace(emulated_partitions->begin(), *mentry);
|
2017-03-13 19:54:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-01-16 01:38:44 +01:00
|
|
|
static void DumpUmountDebuggingInfo() {
|
2017-03-29 21:54:40 +02:00
|
|
|
int status;
|
|
|
|
if (!security_getenforce()) {
|
|
|
|
LOG(INFO) << "Run lsof";
|
|
|
|
const char* lsof_argv[] = {"/system/bin/lsof"};
|
2019-09-26 01:23:50 +02:00
|
|
|
logwrap_fork_execvp(arraysize(lsof_argv), lsof_argv, &status, false, LOG_KLOG, true,
|
|
|
|
nullptr);
|
2017-03-29 21:54:40 +02:00
|
|
|
}
|
|
|
|
FindPartitionsToUmount(nullptr, nullptr, true);
|
2019-01-16 01:38:44 +01:00
|
|
|
// dump current CPU stack traces and uninterruptible tasks
|
2019-04-16 12:46:24 +02:00
|
|
|
WriteStringToFile("l", PROC_SYSRQ);
|
|
|
|
WriteStringToFile("w", PROC_SYSRQ);
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-16 12:46:24 +02:00
|
|
|
static void KillAllProcesses() {
|
|
|
|
WriteStringToFile("i", PROC_SYSRQ);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create reboot/shutdwon monitor thread
|
2019-10-25 00:18:39 +02:00
|
|
|
void RebootMonitorThread(unsigned int cmd, const std::string& reboot_target,
|
|
|
|
sem_t* reboot_semaphore, std::chrono::milliseconds shutdown_timeout,
|
|
|
|
bool* reboot_monitor_run) {
|
2019-04-16 12:46:24 +02:00
|
|
|
unsigned int remaining_shutdown_time = 0;
|
|
|
|
|
2020-04-01 01:31:37 +02:00
|
|
|
// 300 seconds more than the timeout passed to the thread as there is a final Umount pass
|
2019-04-16 12:46:24 +02:00
|
|
|
// after the timeout is reached.
|
2020-04-01 01:31:37 +02:00
|
|
|
constexpr unsigned int shutdown_watchdog_timeout_default = 300;
|
2019-04-16 12:46:24 +02:00
|
|
|
auto shutdown_watchdog_timeout = android::base::GetUintProperty(
|
|
|
|
"ro.build.shutdown.watchdog.timeout", shutdown_watchdog_timeout_default);
|
|
|
|
remaining_shutdown_time = shutdown_watchdog_timeout + shutdown_timeout.count() / 1000;
|
|
|
|
|
|
|
|
while (*reboot_monitor_run == true) {
|
|
|
|
if (TEMP_FAILURE_RETRY(sem_wait(reboot_semaphore)) == -1) {
|
|
|
|
LOG(ERROR) << "sem_wait failed and exit RebootMonitorThread()";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
timespec shutdown_timeout_timespec;
|
|
|
|
if (clock_gettime(CLOCK_MONOTONIC, &shutdown_timeout_timespec) == -1) {
|
|
|
|
LOG(ERROR) << "clock_gettime() fail! exit RebootMonitorThread()";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there are some remaining shutdown time left from previous round, we use
|
|
|
|
// remaining time here.
|
|
|
|
shutdown_timeout_timespec.tv_sec += remaining_shutdown_time;
|
|
|
|
|
|
|
|
LOG(INFO) << "shutdown_timeout_timespec.tv_sec: " << shutdown_timeout_timespec.tv_sec;
|
|
|
|
|
|
|
|
int sem_return = 0;
|
|
|
|
while ((sem_return = sem_timedwait_monotonic_np(reboot_semaphore,
|
|
|
|
&shutdown_timeout_timespec)) == -1 &&
|
|
|
|
errno == EINTR) {
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sem_return == -1) {
|
|
|
|
LOG(ERROR) << "Reboot thread timed out";
|
|
|
|
|
|
|
|
if (android::base::GetBoolProperty("ro.debuggable", false) == true) {
|
2019-09-19 20:16:19 +02:00
|
|
|
if (false) {
|
|
|
|
// SEPolicy will block debuggerd from running and this is intentional.
|
|
|
|
// But these lines are left to be enabled during debugging.
|
|
|
|
LOG(INFO) << "Try to dump init process call trace:";
|
|
|
|
const char* vdc_argv[] = {"/system/bin/debuggerd", "-b", "1"};
|
|
|
|
int status;
|
2019-09-26 01:23:50 +02:00
|
|
|
logwrap_fork_execvp(arraysize(vdc_argv), vdc_argv, &status, false, LOG_KLOG,
|
|
|
|
true, nullptr);
|
2019-09-19 20:16:19 +02:00
|
|
|
}
|
2019-04-16 12:46:24 +02:00
|
|
|
LOG(INFO) << "Show stack for all active CPU:";
|
|
|
|
WriteStringToFile("l", PROC_SYSRQ);
|
|
|
|
|
|
|
|
LOG(INFO) << "Show tasks that are in disk sleep(uninterruptable sleep), which are "
|
|
|
|
"like "
|
|
|
|
"blocked in mutex or hardware register access:";
|
|
|
|
WriteStringToFile("w", PROC_SYSRQ);
|
|
|
|
}
|
|
|
|
|
|
|
|
// In shutdown case,notify kernel to sync and umount fs to read-only before shutdown.
|
|
|
|
if (cmd == ANDROID_RB_POWEROFF || cmd == ANDROID_RB_THERMOFF) {
|
|
|
|
WriteStringToFile("s", PROC_SYSRQ);
|
|
|
|
|
|
|
|
WriteStringToFile("u", PROC_SYSRQ);
|
|
|
|
|
2019-10-25 00:18:39 +02:00
|
|
|
RebootSystem(cmd, reboot_target);
|
2019-04-16 12:46:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
LOG(ERROR) << "Trigger crash at last!";
|
|
|
|
WriteStringToFile("c", PROC_SYSRQ);
|
|
|
|
} else {
|
|
|
|
timespec current_time_timespec;
|
|
|
|
|
|
|
|
if (clock_gettime(CLOCK_MONOTONIC, ¤t_time_timespec) == -1) {
|
|
|
|
LOG(ERROR) << "clock_gettime() fail! exit RebootMonitorThread()";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
remaining_shutdown_time =
|
|
|
|
shutdown_timeout_timespec.tv_sec - current_time_timespec.tv_sec;
|
|
|
|
|
|
|
|
LOG(INFO) << "remaining_shutdown_time: " << remaining_shutdown_time;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-03-27 20:21:09 +02:00
|
|
|
|
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.
|
|
|
|
*/
|
2019-10-25 00:18:39 +02:00
|
|
|
static UmountStat TryUmountAndFsck(unsigned int cmd, bool run_fsck,
|
2019-04-16 12:46:24 +02:00
|
|
|
std::chrono::milliseconds timeout, sem_t* reboot_semaphore) {
|
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
|
|
|
|
2019-10-25 00:18:39 +02:00
|
|
|
if (run_fsck && !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";
|
2019-01-16 01:38:44 +01:00
|
|
|
if (DUMP_ON_UMOUNT_FAILURE) DumpUmountDebuggingInfo();
|
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);
|
2019-01-16 01:38:44 +01:00
|
|
|
if ((st != UMOUNT_STAT_SUCCESS) && DUMP_ON_UMOUNT_FAILURE) DumpUmountDebuggingInfo();
|
2017-03-13 19:54:47 +01:00
|
|
|
}
|
2017-03-29 21:54:40 +02:00
|
|
|
|
2019-10-25 00:18:39 +02:00
|
|
|
if (stat == UMOUNT_STAT_SUCCESS && run_fsck) {
|
2019-04-16 12:46:24 +02:00
|
|
|
LOG(INFO) << "Pause reboot monitor thread before fsck";
|
|
|
|
sem_post(reboot_semaphore);
|
|
|
|
|
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
|
|
|
}
|
2019-04-16 12:46:24 +02:00
|
|
|
|
|
|
|
LOG(INFO) << "Resume reboot monitor thread after fsck";
|
|
|
|
sem_post(reboot_semaphore);
|
2017-03-13 19:54:47 +01:00
|
|
|
}
|
|
|
|
return stat;
|
|
|
|
}
|
|
|
|
|
zram: support zram_writeback
This patch supports zram_writeback enabled by fstab in two ways.
e.g.,
/dev/block/zram0 none swap defaults
zramsize=1073741824,max_comp_streams=8,zram_loopback_path=/data/unencrypted/zram_swap
==> loopback size is 512MB by default.
Or,
/dev/block/zram0 none swap defaults
zramsize=1073741824,max_comp_streams=8,zram_loopback_path=/data/unencrypted/zram_swap,zram_loopback_size=1G
==> loopback size can be specified by "zram_loopback_size=%s" with "GB" or "MB".
Or,
/dev/block/zram0 none swap defaults
zramsize=1073741824,max_comp_streams=8,zram_backing_dev_path=/dev/block/partition
Bug: 74582279
Bug: 122659265
Change-Id: I66a2e6953b4743a34cf732dd0f5b5256c901f247
Signed-off-by: Jaegeuk Kim <jaegeuk@google.com>
2018-11-20 22:27:06 +01:00
|
|
|
// zram is able to use backing device on top of a loopback device.
|
|
|
|
// In order to unmount /data successfully, we have to kill the loopback device first
|
2021-04-14 08:34:59 +02:00
|
|
|
#define ZRAM_DEVICE "/dev/block/zram0"
|
|
|
|
#define ZRAM_RESET "/sys/block/zram0/reset"
|
|
|
|
#define ZRAM_BACK_DEV "/sys/block/zram0/backing_dev"
|
|
|
|
#define ZRAM_INITSTATE "/sys/block/zram0/initstate"
|
2019-10-23 21:11:32 +02:00
|
|
|
static Result<void> KillZramBackingDevice() {
|
2021-04-14 08:34:59 +02:00
|
|
|
std::string zram_initstate;
|
|
|
|
if (!android::base::ReadFileToString(ZRAM_INITSTATE, &zram_initstate)) {
|
|
|
|
return ErrnoError() << "Failed to read " << ZRAM_INITSTATE;
|
|
|
|
}
|
|
|
|
|
|
|
|
zram_initstate.erase(zram_initstate.length() - 1);
|
|
|
|
if (zram_initstate == "0") {
|
|
|
|
LOG(INFO) << "Zram has not been swapped on";
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2020-04-16 22:37:03 +02:00
|
|
|
if (access(ZRAM_BACK_DEV, F_OK) != 0 && errno == ENOENT) {
|
|
|
|
LOG(INFO) << "No zram backing device configured";
|
|
|
|
return {};
|
|
|
|
}
|
zram: support zram_writeback
This patch supports zram_writeback enabled by fstab in two ways.
e.g.,
/dev/block/zram0 none swap defaults
zramsize=1073741824,max_comp_streams=8,zram_loopback_path=/data/unencrypted/zram_swap
==> loopback size is 512MB by default.
Or,
/dev/block/zram0 none swap defaults
zramsize=1073741824,max_comp_streams=8,zram_loopback_path=/data/unencrypted/zram_swap,zram_loopback_size=1G
==> loopback size can be specified by "zram_loopback_size=%s" with "GB" or "MB".
Or,
/dev/block/zram0 none swap defaults
zramsize=1073741824,max_comp_streams=8,zram_backing_dev_path=/dev/block/partition
Bug: 74582279
Bug: 122659265
Change-Id: I66a2e6953b4743a34cf732dd0f5b5256c901f247
Signed-off-by: Jaegeuk Kim <jaegeuk@google.com>
2018-11-20 22:27:06 +01:00
|
|
|
std::string backing_dev;
|
2020-04-16 22:37:03 +02:00
|
|
|
if (!android::base::ReadFileToString(ZRAM_BACK_DEV, &backing_dev)) {
|
|
|
|
return ErrnoError() << "Failed to read " << ZRAM_BACK_DEV;
|
|
|
|
}
|
zram: support zram_writeback
This patch supports zram_writeback enabled by fstab in two ways.
e.g.,
/dev/block/zram0 none swap defaults
zramsize=1073741824,max_comp_streams=8,zram_loopback_path=/data/unencrypted/zram_swap
==> loopback size is 512MB by default.
Or,
/dev/block/zram0 none swap defaults
zramsize=1073741824,max_comp_streams=8,zram_loopback_path=/data/unencrypted/zram_swap,zram_loopback_size=1G
==> loopback size can be specified by "zram_loopback_size=%s" with "GB" or "MB".
Or,
/dev/block/zram0 none swap defaults
zramsize=1073741824,max_comp_streams=8,zram_backing_dev_path=/dev/block/partition
Bug: 74582279
Bug: 122659265
Change-Id: I66a2e6953b4743a34cf732dd0f5b5256c901f247
Signed-off-by: Jaegeuk Kim <jaegeuk@google.com>
2018-11-20 22:27:06 +01:00
|
|
|
|
|
|
|
// cut the last "\n"
|
|
|
|
backing_dev.erase(backing_dev.length() - 1);
|
|
|
|
|
2021-05-18 05:47:43 +02:00
|
|
|
if (android::base::StartsWith(backing_dev, "none")) {
|
|
|
|
LOG(INFO) << "No zram backing device configured";
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
zram: support zram_writeback
This patch supports zram_writeback enabled by fstab in two ways.
e.g.,
/dev/block/zram0 none swap defaults
zramsize=1073741824,max_comp_streams=8,zram_loopback_path=/data/unencrypted/zram_swap
==> loopback size is 512MB by default.
Or,
/dev/block/zram0 none swap defaults
zramsize=1073741824,max_comp_streams=8,zram_loopback_path=/data/unencrypted/zram_swap,zram_loopback_size=1G
==> loopback size can be specified by "zram_loopback_size=%s" with "GB" or "MB".
Or,
/dev/block/zram0 none swap defaults
zramsize=1073741824,max_comp_streams=8,zram_backing_dev_path=/dev/block/partition
Bug: 74582279
Bug: 122659265
Change-Id: I66a2e6953b4743a34cf732dd0f5b5256c901f247
Signed-off-by: Jaegeuk Kim <jaegeuk@google.com>
2018-11-20 22:27:06 +01:00
|
|
|
// shutdown zram handle
|
|
|
|
Timer swap_timer;
|
|
|
|
LOG(INFO) << "swapoff() start...";
|
|
|
|
if (swapoff(ZRAM_DEVICE) == -1) {
|
2019-10-23 21:11:32 +02:00
|
|
|
return ErrnoError() << "zram_backing_dev: swapoff (" << backing_dev << ")"
|
|
|
|
<< " failed";
|
zram: support zram_writeback
This patch supports zram_writeback enabled by fstab in two ways.
e.g.,
/dev/block/zram0 none swap defaults
zramsize=1073741824,max_comp_streams=8,zram_loopback_path=/data/unencrypted/zram_swap
==> loopback size is 512MB by default.
Or,
/dev/block/zram0 none swap defaults
zramsize=1073741824,max_comp_streams=8,zram_loopback_path=/data/unencrypted/zram_swap,zram_loopback_size=1G
==> loopback size can be specified by "zram_loopback_size=%s" with "GB" or "MB".
Or,
/dev/block/zram0 none swap defaults
zramsize=1073741824,max_comp_streams=8,zram_backing_dev_path=/dev/block/partition
Bug: 74582279
Bug: 122659265
Change-Id: I66a2e6953b4743a34cf732dd0f5b5256c901f247
Signed-off-by: Jaegeuk Kim <jaegeuk@google.com>
2018-11-20 22:27:06 +01:00
|
|
|
}
|
|
|
|
LOG(INFO) << "swapoff() took " << swap_timer;;
|
|
|
|
|
2019-04-16 12:46:24 +02:00
|
|
|
if (!WriteStringToFile("1", ZRAM_RESET)) {
|
2019-10-23 21:11:32 +02:00
|
|
|
return Error() << "zram_backing_dev: reset (" << backing_dev << ")"
|
|
|
|
<< " failed";
|
zram: support zram_writeback
This patch supports zram_writeback enabled by fstab in two ways.
e.g.,
/dev/block/zram0 none swap defaults
zramsize=1073741824,max_comp_streams=8,zram_loopback_path=/data/unencrypted/zram_swap
==> loopback size is 512MB by default.
Or,
/dev/block/zram0 none swap defaults
zramsize=1073741824,max_comp_streams=8,zram_loopback_path=/data/unencrypted/zram_swap,zram_loopback_size=1G
==> loopback size can be specified by "zram_loopback_size=%s" with "GB" or "MB".
Or,
/dev/block/zram0 none swap defaults
zramsize=1073741824,max_comp_streams=8,zram_backing_dev_path=/dev/block/partition
Bug: 74582279
Bug: 122659265
Change-Id: I66a2e6953b4743a34cf732dd0f5b5256c901f247
Signed-off-by: Jaegeuk Kim <jaegeuk@google.com>
2018-11-20 22:27:06 +01:00
|
|
|
}
|
|
|
|
|
2020-04-16 22:37:03 +02:00
|
|
|
if (!android::base::StartsWith(backing_dev, "/dev/block/loop")) {
|
|
|
|
LOG(INFO) << backing_dev << " is not a loop device. Exiting early";
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
zram: support zram_writeback
This patch supports zram_writeback enabled by fstab in two ways.
e.g.,
/dev/block/zram0 none swap defaults
zramsize=1073741824,max_comp_streams=8,zram_loopback_path=/data/unencrypted/zram_swap
==> loopback size is 512MB by default.
Or,
/dev/block/zram0 none swap defaults
zramsize=1073741824,max_comp_streams=8,zram_loopback_path=/data/unencrypted/zram_swap,zram_loopback_size=1G
==> loopback size can be specified by "zram_loopback_size=%s" with "GB" or "MB".
Or,
/dev/block/zram0 none swap defaults
zramsize=1073741824,max_comp_streams=8,zram_backing_dev_path=/dev/block/partition
Bug: 74582279
Bug: 122659265
Change-Id: I66a2e6953b4743a34cf732dd0f5b5256c901f247
Signed-off-by: Jaegeuk Kim <jaegeuk@google.com>
2018-11-20 22:27:06 +01:00
|
|
|
// clear loopback device
|
|
|
|
unique_fd loop(TEMP_FAILURE_RETRY(open(backing_dev.c_str(), O_RDWR | O_CLOEXEC)));
|
|
|
|
if (loop.get() < 0) {
|
2019-10-23 21:11:32 +02:00
|
|
|
return ErrnoError() << "zram_backing_dev: open(" << backing_dev << ")"
|
|
|
|
<< " failed";
|
zram: support zram_writeback
This patch supports zram_writeback enabled by fstab in two ways.
e.g.,
/dev/block/zram0 none swap defaults
zramsize=1073741824,max_comp_streams=8,zram_loopback_path=/data/unencrypted/zram_swap
==> loopback size is 512MB by default.
Or,
/dev/block/zram0 none swap defaults
zramsize=1073741824,max_comp_streams=8,zram_loopback_path=/data/unencrypted/zram_swap,zram_loopback_size=1G
==> loopback size can be specified by "zram_loopback_size=%s" with "GB" or "MB".
Or,
/dev/block/zram0 none swap defaults
zramsize=1073741824,max_comp_streams=8,zram_backing_dev_path=/dev/block/partition
Bug: 74582279
Bug: 122659265
Change-Id: I66a2e6953b4743a34cf732dd0f5b5256c901f247
Signed-off-by: Jaegeuk Kim <jaegeuk@google.com>
2018-11-20 22:27:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ioctl(loop.get(), LOOP_CLR_FD, 0) < 0) {
|
2019-10-23 21:11:32 +02:00
|
|
|
return ErrnoError() << "zram_backing_dev: loop_clear (" << backing_dev << ")"
|
|
|
|
<< " failed";
|
zram: support zram_writeback
This patch supports zram_writeback enabled by fstab in two ways.
e.g.,
/dev/block/zram0 none swap defaults
zramsize=1073741824,max_comp_streams=8,zram_loopback_path=/data/unencrypted/zram_swap
==> loopback size is 512MB by default.
Or,
/dev/block/zram0 none swap defaults
zramsize=1073741824,max_comp_streams=8,zram_loopback_path=/data/unencrypted/zram_swap,zram_loopback_size=1G
==> loopback size can be specified by "zram_loopback_size=%s" with "GB" or "MB".
Or,
/dev/block/zram0 none swap defaults
zramsize=1073741824,max_comp_streams=8,zram_backing_dev_path=/dev/block/partition
Bug: 74582279
Bug: 122659265
Change-Id: I66a2e6953b4743a34cf732dd0f5b5256c901f247
Signed-off-by: Jaegeuk Kim <jaegeuk@google.com>
2018-11-20 22:27:06 +01:00
|
|
|
}
|
|
|
|
LOG(INFO) << "zram_backing_dev: `" << backing_dev << "` is cleared successfully.";
|
2019-10-23 21:11:32 +02:00
|
|
|
return {};
|
zram: support zram_writeback
This patch supports zram_writeback enabled by fstab in two ways.
e.g.,
/dev/block/zram0 none swap defaults
zramsize=1073741824,max_comp_streams=8,zram_loopback_path=/data/unencrypted/zram_swap
==> loopback size is 512MB by default.
Or,
/dev/block/zram0 none swap defaults
zramsize=1073741824,max_comp_streams=8,zram_loopback_path=/data/unencrypted/zram_swap,zram_loopback_size=1G
==> loopback size can be specified by "zram_loopback_size=%s" with "GB" or "MB".
Or,
/dev/block/zram0 none swap defaults
zramsize=1073741824,max_comp_streams=8,zram_backing_dev_path=/dev/block/partition
Bug: 74582279
Bug: 122659265
Change-Id: I66a2e6953b4743a34cf732dd0f5b5256c901f247
Signed-off-by: Jaegeuk Kim <jaegeuk@google.com>
2018-11-20 22:27:06 +01:00
|
|
|
}
|
|
|
|
|
2019-10-09 16:23:02 +02:00
|
|
|
// Stops given services, waits for them to be stopped for |timeout| ms.
|
|
|
|
// If terminate is true, then SIGTERM is sent to services, otherwise SIGKILL is sent.
|
2020-11-27 19:57:44 +01:00
|
|
|
// Note that services are stopped in order given by |ServiceList::services_in_shutdown_order|
|
|
|
|
// function.
|
|
|
|
static void StopServices(const std::set<std::string>& services, std::chrono::milliseconds timeout,
|
2019-10-09 16:23:02 +02:00
|
|
|
bool terminate) {
|
|
|
|
LOG(INFO) << "Stopping " << services.size() << " services by sending "
|
|
|
|
<< (terminate ? "SIGTERM" : "SIGKILL");
|
|
|
|
std::vector<pid_t> pids;
|
|
|
|
pids.reserve(services.size());
|
2020-11-27 19:57:44 +01:00
|
|
|
for (const auto& s : ServiceList::GetInstance().services_in_shutdown_order()) {
|
|
|
|
if (services.count(s->name()) == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-10-09 16:23:02 +02:00
|
|
|
if (s->pid() > 0) {
|
|
|
|
pids.push_back(s->pid());
|
|
|
|
}
|
|
|
|
if (terminate) {
|
|
|
|
s->Terminate();
|
|
|
|
} else {
|
|
|
|
s->Stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (timeout > 0ms) {
|
|
|
|
WaitToBeReaped(pids, timeout);
|
|
|
|
} else {
|
|
|
|
// Even if we don't to wait for services to stop, we still optimistically reap zombies.
|
|
|
|
ReapAnyOutstandingChildren();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Like StopServices, but also logs all the services that failed to stop after the provided timeout.
|
|
|
|
// Returns number of violators.
|
2020-12-10 17:52:35 +01:00
|
|
|
int StopServicesAndLogViolations(const std::set<std::string>& services,
|
|
|
|
std::chrono::milliseconds timeout, bool terminate) {
|
2019-10-09 16:23:02 +02:00
|
|
|
StopServices(services, timeout, terminate);
|
|
|
|
int still_running = 0;
|
2020-11-27 19:57:44 +01:00
|
|
|
for (const auto& s : ServiceList::GetInstance()) {
|
|
|
|
if (s->IsRunning() && services.count(s->name())) {
|
2019-10-09 16:23:02 +02:00
|
|
|
LOG(ERROR) << "[service-misbehaving] : service '" << s->name() << "' is still running "
|
|
|
|
<< timeout.count() << "ms after receiving "
|
|
|
|
<< (terminate ? "SIGTERM" : "SIGKILL");
|
|
|
|
still_running++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return still_running;
|
|
|
|
}
|
|
|
|
|
2020-06-10 01:53:59 +02:00
|
|
|
static Result<void> UnmountAllApexes() {
|
|
|
|
const char* args[] = {"/system/bin/apexd", "--unmount-all"};
|
|
|
|
int status;
|
|
|
|
if (logwrap_fork_execvp(arraysize(args), args, &status, false, LOG_KLOG, true, nullptr) != 0) {
|
|
|
|
return ErrnoError() << "Failed to call '/system/bin/apexd --unmount-all'";
|
|
|
|
}
|
|
|
|
if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
return Error() << "'/system/bin/apexd --unmount-all' failed : " << status;
|
|
|
|
}
|
|
|
|
|
2018-08-03 22:36:18 +02:00
|
|
|
//* Reboot / shutdown the system.
|
|
|
|
// cmd ANDROID_RB_* as defined in android_reboot.h
|
|
|
|
// reason Reason string like "reboot", "shutdown,userrequested"
|
2019-10-25 00:18:39 +02:00
|
|
|
// reboot_target Reboot target string like "bootloader". Otherwise, it should be an empty string.
|
|
|
|
// run_fsck Whether to run fsck after umount is done.
|
2018-08-03 22:36:18 +02:00
|
|
|
//
|
2019-10-25 00:18:39 +02:00
|
|
|
static void DoReboot(unsigned int cmd, const std::string& reason, const std::string& reboot_target,
|
|
|
|
bool run_fsck) {
|
2017-03-13 19:54:47 +01:00
|
|
|
Timer t;
|
2019-10-25 00:18:39 +02:00
|
|
|
LOG(INFO) << "Reboot start, reason: " << reason << ", reboot_target: " << reboot_target;
|
2018-03-20 00:19:01 +01:00
|
|
|
|
|
|
|
bool is_thermal_shutdown = cmd == ANDROID_RB_THERMOFF;
|
|
|
|
|
|
|
|
auto shutdown_timeout = 0ms;
|
|
|
|
if (!SHUTDOWN_ZERO_TIMEOUT) {
|
2018-10-09 21:42:06 +02:00
|
|
|
constexpr unsigned int shutdown_timeout_default = 6;
|
|
|
|
constexpr unsigned int max_thermal_shutdown_timeout = 3;
|
|
|
|
auto shutdown_timeout_final = android::base::GetUintProperty("ro.build.shutdown_timeout",
|
|
|
|
shutdown_timeout_default);
|
|
|
|
if (is_thermal_shutdown && shutdown_timeout_final > max_thermal_shutdown_timeout) {
|
|
|
|
shutdown_timeout_final = max_thermal_shutdown_timeout;
|
2018-03-20 00:19:01 +01:00
|
|
|
}
|
2018-10-09 21:42:06 +02:00
|
|
|
shutdown_timeout = std::chrono::seconds(shutdown_timeout_final);
|
2018-03-20 00:19:01 +01:00
|
|
|
}
|
|
|
|
LOG(INFO) << "Shutdown timeout: " << shutdown_timeout.count() << " ms";
|
|
|
|
|
2019-04-16 12:46:24 +02:00
|
|
|
sem_t reboot_semaphore;
|
|
|
|
if (sem_init(&reboot_semaphore, false, 0) == -1) {
|
|
|
|
// These should never fail, but if they do, skip the graceful reboot and reboot immediately.
|
|
|
|
LOG(ERROR) << "sem_init() fail and RebootSystem() return!";
|
2019-10-25 00:18:39 +02:00
|
|
|
RebootSystem(cmd, reboot_target);
|
2019-04-16 12:46:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start a thread to monitor init shutdown process
|
|
|
|
LOG(INFO) << "Create reboot monitor thread.";
|
|
|
|
bool reboot_monitor_run = true;
|
2019-10-25 00:18:39 +02:00
|
|
|
std::thread reboot_monitor_thread(&RebootMonitorThread, cmd, reboot_target, &reboot_semaphore,
|
2019-04-16 12:46:24 +02:00
|
|
|
shutdown_timeout, &reboot_monitor_run);
|
|
|
|
reboot_monitor_thread.detach();
|
|
|
|
|
|
|
|
// Start reboot monitor thread
|
|
|
|
sem_post(&reboot_semaphore);
|
|
|
|
|
2020-04-01 01:31:37 +02:00
|
|
|
// 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,");
|
|
|
|
}
|
|
|
|
PersistRebootReason(reason.c_str() + skip, true);
|
|
|
|
|
|
|
|
// If /data isn't mounted then we can skip the extra reboot steps below, since we don't need to
|
|
|
|
// worry about unmounting it.
|
|
|
|
if (!IsDataMounted()) {
|
|
|
|
sync();
|
|
|
|
RebootSystem(cmd, reboot_target);
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2021-08-10 08:04:34 +02:00
|
|
|
bool do_shutdown_animation = GetBoolProperty("ro.init.shutdown_animation", false);
|
2017-03-28 01:07:02 +02:00
|
|
|
// 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"};
|
2020-11-27 19:57:44 +01:00
|
|
|
std::set<std::string> stop_first;
|
2017-07-28 01:20:58 +02:00
|
|
|
for (const auto& s : ServiceList::GetInstance()) {
|
2019-10-09 16:23:02 +02:00
|
|
|
if (kDebuggingServices.count(s->name())) {
|
|
|
|
// keep debugging tools until non critical ones are all gone.
|
2017-03-28 01:07:02 +02:00
|
|
|
s->SetShutdownCritical();
|
|
|
|
} else if (to_starts.count(s->name())) {
|
2020-02-05 19:49:33 +01:00
|
|
|
if (auto result = s->Start(); !result.ok()) {
|
2017-08-25 19:36:52 +02:00
|
|
|
LOG(ERROR) << "Could not start shutdown 'to_start' service '" << s->name()
|
|
|
|
<< "': " << result.error();
|
|
|
|
}
|
2017-03-28 01:07:02 +02:00
|
|
|
s->SetShutdownCritical();
|
2021-08-10 08:04:34 +02:00
|
|
|
} else if (do_shutdown_animation) {
|
|
|
|
continue;
|
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.
|
2020-02-05 19:49:33 +01:00
|
|
|
if (auto result = s->Start(); !result.ok()) {
|
2017-08-25 19:36:52 +02:00
|
|
|
LOG(ERROR) << "Could not start shutdown critical service '" << s->name()
|
|
|
|
<< "': " << result.error();
|
|
|
|
}
|
2019-10-09 16:23:02 +02:00
|
|
|
} else {
|
2020-11-27 19:57:44 +01:00
|
|
|
stop_first.insert(s->name());
|
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
|
2021-08-10 08:04:34 +02:00
|
|
|
if (!do_shutdown_animation && (cmd == ANDROID_RB_POWEROFF || is_thermal_shutdown)) {
|
2018-01-19 22:01:53 +01:00
|
|
|
TurnOffBacklight();
|
|
|
|
}
|
|
|
|
|
2019-10-25 00:18:39 +02:00
|
|
|
Service* boot_anim = ServiceList::GetInstance().FindService("bootanim");
|
|
|
|
Service* surface_flinger = ServiceList::GetInstance().FindService("surfaceflinger");
|
|
|
|
if (boot_anim != nullptr && surface_flinger != nullptr && surface_flinger->IsRunning()) {
|
2019-01-08 19:39:00 +01:00
|
|
|
|
|
|
|
if (do_shutdown_animation) {
|
2019-08-20 00:21:25 +02:00
|
|
|
SetProperty("service.bootanim.exit", "0");
|
2020-12-15 19:34:47 +01:00
|
|
|
SetProperty("service.bootanim.progress", "0");
|
2019-01-08 19:39:00 +01:00
|
|
|
// Could be in the middle of animation. Stop and start so that it can pick
|
|
|
|
// up the right mode.
|
2019-10-25 00:18:39 +02:00
|
|
|
boot_anim->Stop();
|
2019-01-08 19:39:00 +01:00
|
|
|
}
|
|
|
|
|
2017-07-28 01:20:58 +02:00
|
|
|
for (const auto& service : ServiceList::GetInstance()) {
|
2019-01-08 19:39:00 +01:00
|
|
|
if (service->classnames().count("animation") == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// start all animation classes if stopped.
|
|
|
|
if (do_shutdown_animation) {
|
2019-05-17 01:54:49 +02:00
|
|
|
service->Start();
|
2019-01-08 19:39:00 +01:00
|
|
|
}
|
|
|
|
service->SetShutdownCritical(); // will not check animation class separately
|
|
|
|
}
|
|
|
|
|
|
|
|
if (do_shutdown_animation) {
|
2019-10-25 00:18:39 +02:00
|
|
|
boot_anim->Start();
|
|
|
|
surface_flinger->SetShutdownCritical();
|
|
|
|
boot_anim->SetShutdownCritical();
|
2017-07-28 01:20:58 +02:00
|
|
|
}
|
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) {
|
2019-10-09 16:23:02 +02:00
|
|
|
StopServicesAndLogViolations(stop_first, shutdown_timeout / 2, true /* SIGTERM */);
|
2017-07-28 01:20:58 +02:00
|
|
|
}
|
2019-10-09 16:23:02 +02:00
|
|
|
// Send SIGKILL to ones that didn't terminate cleanly.
|
|
|
|
StopServicesAndLogViolations(stop_first, 0ms, false /* SIGKILL */);
|
2018-07-27 20:19:25 +02:00
|
|
|
SubcontextTerminate();
|
2019-10-09 16:23:02 +02:00
|
|
|
// Reap subcontext pids.
|
2017-07-29 00:22:23 +02:00
|
|
|
ReapAnyOutstandingChildren();
|
2017-03-13 19:54:47 +01:00
|
|
|
|
2020-05-28 16:42:10 +02:00
|
|
|
// 3. send volume abort_fuse and volume shutdown to vold
|
2019-10-25 00:18:39 +02:00
|
|
|
Service* vold_service = ServiceList::GetInstance().FindService("vold");
|
|
|
|
if (vold_service != nullptr && vold_service->IsRunning()) {
|
2020-05-28 16:42:10 +02:00
|
|
|
// Manually abort FUSE connections, since the FUSE daemon is already dead
|
|
|
|
// at this point, and unmounting it might hang.
|
|
|
|
CallVdc("volume", "abort_fuse");
|
2019-12-02 12:51:39 +01:00
|
|
|
CallVdc("volume", "shutdown");
|
2019-10-25 00:18:39 +02:00
|
|
|
vold_service->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
|
2020-11-27 19:57:44 +01:00
|
|
|
StopServices(kDebuggingServices, 0ms, false /* SIGKILL */);
|
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;
|
|
|
|
}
|
zram: support zram_writeback
This patch supports zram_writeback enabled by fstab in two ways.
e.g.,
/dev/block/zram0 none swap defaults
zramsize=1073741824,max_comp_streams=8,zram_loopback_path=/data/unencrypted/zram_swap
==> loopback size is 512MB by default.
Or,
/dev/block/zram0 none swap defaults
zramsize=1073741824,max_comp_streams=8,zram_loopback_path=/data/unencrypted/zram_swap,zram_loopback_size=1G
==> loopback size can be specified by "zram_loopback_size=%s" with "GB" or "MB".
Or,
/dev/block/zram0 none swap defaults
zramsize=1073741824,max_comp_streams=8,zram_backing_dev_path=/dev/block/partition
Bug: 74582279
Bug: 122659265
Change-Id: I66a2e6953b4743a34cf732dd0f5b5256c901f247
Signed-off-by: Jaegeuk Kim <jaegeuk@google.com>
2018-11-20 22:27:06 +01:00
|
|
|
// 5. drop caches and disable zram backing device, if exist
|
|
|
|
KillZramBackingDevice();
|
|
|
|
|
2020-06-10 01:53:59 +02:00
|
|
|
LOG(INFO) << "Ready to unmount apexes. So far shutdown sequence took " << t;
|
|
|
|
// 6. unmount active apexes, otherwise they might prevent clean unmount of /data.
|
|
|
|
if (auto ret = UnmountAllApexes(); !ret.ok()) {
|
|
|
|
LOG(ERROR) << ret.error();
|
|
|
|
}
|
2019-10-25 00:18:39 +02:00
|
|
|
UmountStat stat =
|
|
|
|
TryUmountAndFsck(cmd, run_fsck, shutdown_timeout - t.duration(), &reboot_semaphore);
|
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);
|
2019-04-16 12:46:24 +02:00
|
|
|
|
|
|
|
// Send signal to terminate reboot monitor thread.
|
|
|
|
reboot_monitor_run = false;
|
|
|
|
sem_post(&reboot_semaphore);
|
|
|
|
|
2017-03-13 19:54:47 +01:00
|
|
|
// Reboot regardless of umount status. If umount fails, fsck after reboot will fix it.
|
2019-10-25 00:18:39 +02:00
|
|
|
RebootSystem(cmd, reboot_target);
|
2017-03-13 19:54:47 +01:00
|
|
|
abort();
|
|
|
|
}
|
2017-04-18 01:34:20 +02:00
|
|
|
|
2019-10-07 17:26:33 +02:00
|
|
|
static void EnterShutdown() {
|
2019-10-09 16:23:02 +02:00
|
|
|
LOG(INFO) << "Entering shutdown mode";
|
2019-10-07 17:26:33 +02:00
|
|
|
shutting_down = true;
|
|
|
|
// Skip wait for prop if it is in progress
|
|
|
|
ResetWaitForProp();
|
|
|
|
// Clear EXEC flag if there is one pending
|
|
|
|
for (const auto& s : ServiceList::GetInstance()) {
|
|
|
|
s->UnSetExec();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void LeaveShutdown() {
|
2019-10-09 16:23:02 +02:00
|
|
|
LOG(INFO) << "Leaving shutdown mode";
|
2019-10-07 17:26:33 +02:00
|
|
|
shutting_down = false;
|
init: handle property messages asynchronously #2
A previous change moved property_service into its own thread, since
there was otherwise a deadlock whenever a process called by init would
try to set a property. This new thread, however, would send a message
via a blocking socket to init for each property that it received,
since init may need to take action depending on which property it is.
Unfortunately, this means that the deadlock is still possible, the
only difference is the socket's buffer must be filled before init deadlocks.
This change, therefore, adds the following:
1) A lock for instructing init to reboot
2) A lock for waiting on properties
3) A lock for queueing new properties
A previous version of this change was reverted and added locks around
all service operations and allowed the property thread to spawn
services directly. This was complex due to the fact that this code
was not designed to be multi-threaded. It was reverted due to
apparent issues during reboot. This change keeps a queue of processes
pending control messages, which it will then handle in the future. It
is less flexible but safer.
Bug: 146877356
Bug: 148236233
Bug: 150863651
Bug: 151251827
Test: multiple reboot tests, safely restarting hwservicemanager
Change-Id: Ice773436e85d3bf636bb0a892f3f6002bdf996b6
2020-03-12 22:29:25 +01:00
|
|
|
StartSendingMessages();
|
2019-10-07 17:26:33 +02:00
|
|
|
}
|
|
|
|
|
2020-03-25 01:06:51 +01:00
|
|
|
static std::chrono::milliseconds GetMillisProperty(const std::string& name,
|
|
|
|
std::chrono::milliseconds default_value) {
|
|
|
|
auto value = GetUintProperty(name, static_cast<uint64_t>(default_value.count()));
|
|
|
|
return std::chrono::milliseconds(std::move(value));
|
|
|
|
}
|
|
|
|
|
2019-10-09 16:23:02 +02:00
|
|
|
static Result<void> DoUserspaceReboot() {
|
|
|
|
LOG(INFO) << "Userspace reboot initiated";
|
2020-04-30 23:37:15 +02:00
|
|
|
// An ugly way to pass a more precise reason on why fallback to hard reboot was triggered.
|
|
|
|
std::string sub_reason = "";
|
|
|
|
auto guard = android::base::make_scope_guard([&sub_reason] {
|
2019-10-09 16:23:02 +02:00
|
|
|
// Leave shutdown so that we can handle a full reboot.
|
|
|
|
LeaveShutdown();
|
2020-04-30 23:37:15 +02:00
|
|
|
trigger_shutdown("reboot,userspace_failed,shutdown_aborted," + sub_reason);
|
2019-10-09 16:23:02 +02:00
|
|
|
});
|
2019-11-13 22:47:06 +01:00
|
|
|
// Triggering userspace-reboot-requested will result in a bunch of setprop
|
2019-10-07 17:26:33 +02:00
|
|
|
// actions. We should make sure, that all of them are propagated before
|
2020-01-27 18:14:46 +01:00
|
|
|
// proceeding with userspace reboot. Synchronously setting sys.init.userspace_reboot.in_progress
|
|
|
|
// property is not perfect, but it should do the trick.
|
2019-11-14 02:21:24 +01:00
|
|
|
if (!android::sysprop::InitProperties::userspace_reboot_in_progress(true)) {
|
2020-04-30 23:37:15 +02:00
|
|
|
sub_reason = "setprop";
|
2019-11-14 02:21:24 +01:00
|
|
|
return Error() << "Failed to set sys.init.userspace_reboot.in_progress property";
|
2019-11-13 22:47:06 +01:00
|
|
|
}
|
2019-10-07 17:26:33 +02:00
|
|
|
EnterShutdown();
|
2020-01-27 18:14:46 +01:00
|
|
|
if (!SetProperty("sys.powerctl", "")) {
|
2020-04-30 23:37:15 +02:00
|
|
|
sub_reason = "resetprop";
|
2020-01-27 18:14:46 +01:00
|
|
|
return Error() << "Failed to reset sys.powerctl property";
|
|
|
|
}
|
2020-11-27 19:57:44 +01:00
|
|
|
std::set<std::string> stop_first;
|
2019-10-09 16:23:02 +02:00
|
|
|
// Remember the services that were enabled. We will need to manually enable them again otherwise
|
|
|
|
// triggers like class_start won't restart them.
|
2020-11-27 19:57:44 +01:00
|
|
|
std::set<std::string> were_enabled;
|
2019-10-09 16:23:02 +02:00
|
|
|
for (const auto& s : ServiceList::GetInstance().services_in_shutdown_order()) {
|
|
|
|
if (s->is_post_data() && !kDebuggingServices.count(s->name())) {
|
2020-11-27 19:57:44 +01:00
|
|
|
stop_first.insert(s->name());
|
2019-10-09 16:23:02 +02:00
|
|
|
}
|
2020-11-27 19:57:44 +01:00
|
|
|
// TODO(ioffe): we should also filter out temporary services here.
|
2019-10-09 16:23:02 +02:00
|
|
|
if (s->is_post_data() && s->IsEnabled()) {
|
2020-11-27 19:57:44 +01:00
|
|
|
were_enabled.insert(s->name());
|
2019-10-09 16:23:02 +02:00
|
|
|
}
|
|
|
|
}
|
2019-11-07 16:37:38 +01:00
|
|
|
{
|
|
|
|
Timer sync_timer;
|
|
|
|
LOG(INFO) << "sync() before terminating services...";
|
|
|
|
sync();
|
|
|
|
LOG(INFO) << "sync() took " << sync_timer;
|
|
|
|
}
|
2020-03-25 01:06:51 +01:00
|
|
|
auto sigterm_timeout = GetMillisProperty("init.userspace_reboot.sigterm.timeoutmillis", 5s);
|
|
|
|
auto sigkill_timeout = GetMillisProperty("init.userspace_reboot.sigkill.timeoutmillis", 10s);
|
2020-04-16 22:37:03 +02:00
|
|
|
LOG(INFO) << "Timeout to terminate services: " << sigterm_timeout.count() << "ms "
|
2020-03-25 01:06:51 +01:00
|
|
|
<< "Timeout to kill services: " << sigkill_timeout.count() << "ms";
|
2020-05-11 15:10:09 +02:00
|
|
|
std::string services_file_name = "/metadata/userspacereboot/services.txt";
|
|
|
|
const int flags = O_RDWR | O_CREAT | O_SYNC | O_APPEND | O_CLOEXEC;
|
2020-03-25 01:06:51 +01:00
|
|
|
StopServicesAndLogViolations(stop_first, sigterm_timeout, true /* SIGTERM */);
|
|
|
|
if (int r = StopServicesAndLogViolations(stop_first, sigkill_timeout, false /* SIGKILL */);
|
|
|
|
r > 0) {
|
2020-05-11 15:10:09 +02:00
|
|
|
auto fd = unique_fd(TEMP_FAILURE_RETRY(open(services_file_name.c_str(), flags, 0666)));
|
|
|
|
android::base::WriteStringToFd("Post-data services still running: \n", fd);
|
2020-11-27 19:57:44 +01:00
|
|
|
for (const auto& s : ServiceList::GetInstance()) {
|
|
|
|
if (s->IsRunning() && stop_first.count(s->name())) {
|
2020-05-11 15:10:09 +02:00
|
|
|
android::base::WriteStringToFd(s->name() + "\n", fd);
|
|
|
|
}
|
|
|
|
}
|
2020-04-30 23:37:15 +02:00
|
|
|
sub_reason = "sigkill";
|
2019-10-09 16:23:02 +02:00
|
|
|
return Error() << r << " post-data services are still running";
|
|
|
|
}
|
2020-02-05 19:49:33 +01:00
|
|
|
if (auto result = KillZramBackingDevice(); !result.ok()) {
|
2020-04-30 23:37:15 +02:00
|
|
|
sub_reason = "zram";
|
2019-12-02 12:51:39 +01:00
|
|
|
return result;
|
|
|
|
}
|
2020-02-05 19:49:33 +01:00
|
|
|
if (auto result = CallVdc("volume", "reset"); !result.ok()) {
|
2020-04-30 23:37:15 +02:00
|
|
|
sub_reason = "vold_reset";
|
2019-12-02 12:51:39 +01:00
|
|
|
return result;
|
|
|
|
}
|
2020-11-27 19:57:44 +01:00
|
|
|
const auto& debugging_services = GetPostDataDebuggingServices();
|
|
|
|
if (int r = StopServicesAndLogViolations(debugging_services, sigkill_timeout,
|
|
|
|
false /* SIGKILL */);
|
2019-10-09 16:23:02 +02:00
|
|
|
r > 0) {
|
2020-05-11 15:10:09 +02:00
|
|
|
auto fd = unique_fd(TEMP_FAILURE_RETRY(open(services_file_name.c_str(), flags, 0666)));
|
|
|
|
android::base::WriteStringToFd("Debugging services still running: \n", fd);
|
2020-11-27 19:57:44 +01:00
|
|
|
for (const auto& s : ServiceList::GetInstance()) {
|
|
|
|
if (s->IsRunning() && debugging_services.count(s->name())) {
|
2020-05-11 15:10:09 +02:00
|
|
|
android::base::WriteStringToFd(s->name() + "\n", fd);
|
|
|
|
}
|
|
|
|
}
|
2020-04-30 23:37:15 +02:00
|
|
|
sub_reason = "sigkill_debug";
|
2019-10-09 16:23:02 +02:00
|
|
|
return Error() << r << " debugging services are still running";
|
|
|
|
}
|
2019-11-07 16:37:38 +01:00
|
|
|
{
|
|
|
|
Timer sync_timer;
|
|
|
|
LOG(INFO) << "sync() after stopping services...";
|
|
|
|
sync();
|
|
|
|
LOG(INFO) << "sync() took " << sync_timer;
|
|
|
|
}
|
2020-02-05 19:49:33 +01:00
|
|
|
if (auto result = UnmountAllApexes(); !result.ok()) {
|
2020-04-30 23:37:15 +02:00
|
|
|
sub_reason = "apex";
|
2019-11-06 22:40:31 +01:00
|
|
|
return result;
|
|
|
|
}
|
2020-02-07 15:20:54 +01:00
|
|
|
if (!SwitchToMountNamespaceIfNeeded(NS_BOOTSTRAP).ok()) {
|
2020-04-30 23:37:15 +02:00
|
|
|
sub_reason = "ns_switch";
|
2019-11-06 22:40:31 +01:00
|
|
|
return Error() << "Failed to switch to bootstrap namespace";
|
|
|
|
}
|
2019-12-05 13:35:19 +01:00
|
|
|
// Remove services that were defined in an APEX.
|
|
|
|
ServiceList::GetInstance().RemoveServiceIf([](const std::unique_ptr<Service>& s) -> bool {
|
|
|
|
if (s->is_from_apex()) {
|
|
|
|
LOG(INFO) << "Removing service '" << s->name() << "' because it's defined in an APEX";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
2019-10-09 16:23:02 +02:00
|
|
|
// Re-enable services
|
2020-11-27 19:57:44 +01:00
|
|
|
for (const auto& s : ServiceList::GetInstance()) {
|
|
|
|
if (were_enabled.count(s->name())) {
|
|
|
|
LOG(INFO) << "Re-enabling service '" << s->name() << "'";
|
|
|
|
s->Enable();
|
|
|
|
}
|
2019-10-09 16:23:02 +02:00
|
|
|
}
|
2020-02-27 21:46:27 +01:00
|
|
|
ServiceList::GetInstance().ResetState();
|
2019-10-09 16:23:02 +02:00
|
|
|
LeaveShutdown();
|
2019-10-07 17:26:33 +02:00
|
|
|
ActionManager::GetInstance().QueueEventTrigger("userspace-reboot-resume");
|
2019-10-09 16:23:02 +02:00
|
|
|
guard.Disable(); // Go on with userspace reboot.
|
|
|
|
return {};
|
2019-10-07 17:26:33 +02:00
|
|
|
}
|
|
|
|
|
2019-11-07 16:37:38 +01:00
|
|
|
static void UserspaceRebootWatchdogThread() {
|
2020-04-11 02:55:24 +02:00
|
|
|
auto started_timeout = GetMillisProperty("init.userspace_reboot.started.timeoutmillis", 10s);
|
|
|
|
if (!WaitForProperty("sys.init.userspace_reboot.in_progress", "1", started_timeout)) {
|
|
|
|
LOG(ERROR) << "Userspace reboot didn't start in " << started_timeout.count()
|
|
|
|
<< "ms. Switching to full reboot";
|
|
|
|
// Init might be wedged, don't try to write reboot reason into a persistent property and do
|
|
|
|
// a dirty reboot.
|
|
|
|
PersistRebootReason("userspace_failed,watchdog_triggered,failed_to_start", false);
|
|
|
|
RebootSystem(ANDROID_RB_RESTART2, "userspace_failed,watchdog_triggered,failed_to_start");
|
2019-11-07 16:37:38 +01:00
|
|
|
}
|
|
|
|
LOG(INFO) << "Starting userspace reboot watchdog";
|
2020-04-11 02:55:24 +02:00
|
|
|
auto watchdog_timeout = GetMillisProperty("init.userspace_reboot.watchdog.timeoutmillis", 5min);
|
|
|
|
LOG(INFO) << "UserspaceRebootWatchdog timeout: " << watchdog_timeout.count() << "ms";
|
|
|
|
if (!WaitForProperty("sys.boot_completed", "1", watchdog_timeout)) {
|
|
|
|
LOG(ERROR) << "Failed to boot in " << watchdog_timeout.count()
|
|
|
|
<< "ms. Switching to full reboot";
|
2019-11-07 16:37:38 +01:00
|
|
|
// In this case device is in a boot loop. Only way to recover is to do dirty reboot.
|
2020-02-26 16:18:56 +01:00
|
|
|
// Since init might be wedged, don't try to write reboot reason into a persistent property.
|
2020-04-11 02:55:24 +02:00
|
|
|
PersistRebootReason("userspace_failed,watchdog_triggered,failed_to_boot", false);
|
|
|
|
RebootSystem(ANDROID_RB_RESTART2, "userspace_failed,watchdog_triggered,failed_to_boot");
|
2019-11-07 16:37:38 +01:00
|
|
|
}
|
|
|
|
LOG(INFO) << "Device booted, stopping userspace reboot watchdog";
|
|
|
|
}
|
|
|
|
|
2019-10-07 17:26:33 +02:00
|
|
|
static void HandleUserspaceReboot() {
|
2019-12-20 17:34:48 +01:00
|
|
|
if (!android::sysprop::InitProperties::is_userspace_reboot_supported().value_or(false)) {
|
2019-12-18 14:31:42 +01:00
|
|
|
LOG(ERROR) << "Attempted a userspace reboot on a device that doesn't support it";
|
|
|
|
return;
|
|
|
|
}
|
2019-11-07 16:37:38 +01:00
|
|
|
// Spinnig up a separate thread will fail the setns call later in the boot sequence.
|
|
|
|
// Fork a new process to monitor userspace reboot while we are investigating a better solution.
|
|
|
|
pid_t pid = fork();
|
|
|
|
if (pid < 0) {
|
|
|
|
PLOG(ERROR) << "Failed to fork process for userspace reboot watchdog. Switching to full "
|
|
|
|
<< "reboot";
|
2020-01-16 00:23:13 +01:00
|
|
|
trigger_shutdown("reboot,userspace_failed,watchdog_fork");
|
2019-11-07 16:37:38 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (pid == 0) {
|
|
|
|
// Child
|
|
|
|
UserspaceRebootWatchdogThread();
|
|
|
|
_exit(EXIT_SUCCESS);
|
|
|
|
}
|
2019-10-07 17:26:33 +02:00
|
|
|
LOG(INFO) << "Clearing queue and starting userspace-reboot-requested trigger";
|
|
|
|
auto& am = ActionManager::GetInstance();
|
|
|
|
am.ClearQueue();
|
|
|
|
am.QueueEventTrigger("userspace-reboot-requested");
|
2019-10-09 16:23:02 +02:00
|
|
|
auto handler = [](const BuiltinArguments&) { return DoUserspaceReboot(); };
|
2019-10-07 17:26:33 +02:00
|
|
|
am.QueueBuiltinAction(handler, "userspace-reboot");
|
|
|
|
}
|
|
|
|
|
2019-12-26 20:55:08 +01:00
|
|
|
/**
|
|
|
|
* Check if "command" field is set in bootloader message.
|
|
|
|
*
|
|
|
|
* If "command" field is broken (contains non-printable characters prior to
|
|
|
|
* terminating zero), it will be zeroed.
|
|
|
|
*
|
|
|
|
* @param[in,out] boot Bootloader message (BCB) structure
|
|
|
|
* @return true if "command" field is already set, and false if it's empty
|
|
|
|
*/
|
|
|
|
static bool CommandIsPresent(bootloader_message* boot) {
|
|
|
|
if (boot->command[0] == '\0')
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < arraysize(boot->command); ++i) {
|
|
|
|
if (boot->command[i] == '\0')
|
|
|
|
return true;
|
|
|
|
if (!isprint(boot->command[i]))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(boot->command, 0, sizeof(boot->command));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-10-07 17:26:33 +02:00
|
|
|
void HandlePowerctlMessage(const std::string& command) {
|
2017-04-18 01:34:20 +02:00
|
|
|
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;
|
2019-10-07 17:26:33 +02:00
|
|
|
bool userspace_reboot = false;
|
2017-04-18 01:34:20 +02:00
|
|
|
|
2019-07-10 19:33:09 +02:00
|
|
|
if (cmd_params[0] == "shutdown") {
|
2017-04-18 01:34:20 +02:00
|
|
|
cmd = ANDROID_RB_POWEROFF;
|
2019-07-10 19:33:09 +02:00
|
|
|
if (cmd_params.size() >= 2) {
|
2017-08-15 00:56:53 +02:00
|
|
|
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];
|
2019-10-07 17:26:33 +02:00
|
|
|
if (reboot_target == "userspace") {
|
|
|
|
LOG(INFO) << "Userspace reboot requested";
|
|
|
|
userspace_reboot = true;
|
|
|
|
}
|
2018-09-20 06:14:18 +02:00
|
|
|
// adb reboot fastboot should boot into bootloader for devices not
|
|
|
|
// supporting logical partitions.
|
|
|
|
if (reboot_target == "fastboot" &&
|
2018-11-16 21:49:06 +01:00
|
|
|
!android::base::GetBoolProperty("ro.boot.dynamic_partitions", false)) {
|
2018-09-20 06:14:18 +02:00
|
|
|
reboot_target = "bootloader";
|
|
|
|
}
|
2017-04-18 01:34:20 +02:00
|
|
|
// 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;
|
|
|
|
}
|
2019-07-18 21:48:28 +02:00
|
|
|
} else if (reboot_target == "recovery") {
|
|
|
|
bootloader_message boot = {};
|
|
|
|
if (std::string err; !read_bootloader_message(&boot, &err)) {
|
|
|
|
LOG(ERROR) << "Failed to read bootloader message: " << err;
|
|
|
|
}
|
|
|
|
// Update the boot command field if it's empty, and preserve
|
|
|
|
// the other arguments in the bootloader message.
|
2019-12-26 20:55:08 +01:00
|
|
|
if (!CommandIsPresent(&boot)) {
|
2019-07-18 21:48:28 +02:00
|
|
|
strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
|
|
|
|
if (std::string err; !write_bootloader_message(boot, &err)) {
|
|
|
|
LOG(ERROR) << "Failed to set bootloader message: " << err;
|
2019-10-07 17:26:33 +02:00
|
|
|
return;
|
2019-07-18 21:48:28 +02:00
|
|
|
}
|
|
|
|
}
|
2021-06-29 09:41:22 +02:00
|
|
|
} else if (reboot_target == "quiescent") {
|
|
|
|
bootloader_message boot = {};
|
|
|
|
if (std::string err; !read_bootloader_message(&boot, &err)) {
|
|
|
|
LOG(ERROR) << "Failed to read bootloader message: " << err;
|
|
|
|
}
|
|
|
|
// Update the boot command field if it's empty, and preserve
|
|
|
|
// the other arguments in the bootloader message.
|
|
|
|
if (!CommandIsPresent(&boot)) {
|
|
|
|
strlcpy(boot.command, "boot-quiescent", sizeof(boot.command));
|
|
|
|
if (std::string err; !write_bootloader_message(boot, &err)) {
|
|
|
|
LOG(ERROR) << "Failed to set bootloader message: " << err;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2018-08-03 00:02:06 +02:00
|
|
|
} else if (reboot_target == "sideload" || reboot_target == "sideload-auto-reboot" ||
|
|
|
|
reboot_target == "fastboot") {
|
|
|
|
std::string arg = reboot_target == "sideload-auto-reboot" ? "sideload_auto_reboot"
|
|
|
|
: reboot_target;
|
|
|
|
const std::vector<std::string> options = {
|
|
|
|
"--" + arg,
|
|
|
|
};
|
|
|
|
std::string err;
|
|
|
|
if (!write_bootloader_message(options, &err)) {
|
|
|
|
LOG(ERROR) << "Failed to set bootloader message: " << err;
|
2019-10-07 17:26:33 +02:00
|
|
|
return;
|
2018-08-03 00:02:06 +02:00
|
|
|
}
|
|
|
|
reboot_target = "recovery";
|
2017-04-18 01:34:20 +02:00
|
|
|
}
|
2018-08-03 00:02:06 +02:00
|
|
|
|
2019-07-10 19:33:09 +02:00
|
|
|
// If there are additional parameter, pass them along
|
|
|
|
for (size_t i = 2; (cmd_params.size() > i) && cmd_params[i].size(); ++i) {
|
|
|
|
reboot_target += "," + cmd_params[i];
|
2017-04-18 01:34:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
command_invalid = true;
|
|
|
|
}
|
|
|
|
if (command_invalid) {
|
|
|
|
LOG(ERROR) << "powerctl: unrecognized command '" << command << "'";
|
2019-10-07 17:26:33 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
init: handle property messages asynchronously #2
A previous change moved property_service into its own thread, since
there was otherwise a deadlock whenever a process called by init would
try to set a property. This new thread, however, would send a message
via a blocking socket to init for each property that it received,
since init may need to take action depending on which property it is.
Unfortunately, this means that the deadlock is still possible, the
only difference is the socket's buffer must be filled before init deadlocks.
This change, therefore, adds the following:
1) A lock for instructing init to reboot
2) A lock for waiting on properties
3) A lock for queueing new properties
A previous version of this change was reverted and added locks around
all service operations and allowed the property thread to spawn
services directly. This was complex due to the fact that this code
was not designed to be multi-threaded. It was reverted due to
apparent issues during reboot. This change keeps a queue of processes
pending control messages, which it will then handle in the future. It
is less flexible but safer.
Bug: 146877356
Bug: 148236233
Bug: 150863651
Bug: 151251827
Test: multiple reboot tests, safely restarting hwservicemanager
Change-Id: Ice773436e85d3bf636bb0a892f3f6002bdf996b6
2020-03-12 22:29:25 +01:00
|
|
|
// We do not want to process any messages (queue'ing triggers, shutdown messages, control
|
|
|
|
// messages, etc) from properties during reboot.
|
|
|
|
StopSendingMessages();
|
|
|
|
|
2019-10-07 17:26:33 +02:00
|
|
|
if (userspace_reboot) {
|
|
|
|
HandleUserspaceReboot();
|
|
|
|
return;
|
2017-04-18 01:34:20 +02:00
|
|
|
}
|
|
|
|
|
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);
|
2019-06-10 20:08:01 +02:00
|
|
|
return Result<void>{};
|
2017-06-28 07:08:45 +02:00
|
|
|
};
|
|
|
|
ActionManager::GetInstance().QueueBuiltinAction(shutdown_handler, "shutdown_done");
|
|
|
|
|
2019-10-07 17:26:33 +02:00
|
|
|
EnterShutdown();
|
|
|
|
}
|
2019-04-23 02:46:37 +02:00
|
|
|
|
2019-10-07 17:26:33 +02:00
|
|
|
bool IsShuttingDown() {
|
|
|
|
return shutting_down;
|
2017-04-18 01:34:20 +02:00
|
|
|
}
|
2017-06-22 21:53:17 +02:00
|
|
|
|
|
|
|
} // namespace init
|
|
|
|
} // namespace android
|