blob: 26232524575ed5bfbb81d702dad0feaefab4df93 [file]
/*
* Copyright (C) 2025 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.
*/
#define LOG_TAG "ueventd"
#include "coldboot.h"
#include "coldboot_runner.h"
#include "util.h"
#include <android-base/chrono_utils.h>
#include <android-base/logging.h>
#include <android-base/properties.h>
#include <cutils/klog.h>
#include <selinux/android.h>
#include <selinux/selinux.h>
#include <sys/stat.h>
#include <algorithm>
namespace android {
namespace init {
void RestoreconRecurse(const std::string& dir) {
android::base::Timer t;
selinux_android_restorecon(dir.c_str(), SELINUX_ANDROID_RESTORECON_RECURSE);
// Mark a dir restorecon operation for 50ms,
// Maybe you can add this dir to the ueventd.rc script to parallel processing
if (t.duration() > 50ms) {
KLOG_INFO(LOG_TAG, "took %lldms restorecon '%s'\n",
static_cast<long long>(t.duration().count()), dir.c_str());
}
}
void ColdBoot::RegenerateUevents() {
if (priority_subsystems_.empty()) {
uevent_listener_.RegenerateUevents([this](const Uevent& uevent) {
uevent_queue_.emplace_back(uevent);
return ListenerAction::kContinue;
});
return;
}
std::vector<Uevent> priority_uevents;
std::vector<Uevent> normal_uevents;
uevent_listener_.RegenerateUevents(
[this, &priority_uevents, &normal_uevents](const Uevent& uevent) {
if (priority_subsystems_.count(uevent.subsystem) > 0) {
priority_uevents.emplace_back(uevent);
} else {
normal_uevents.emplace_back(uevent);
}
return ListenerAction::kContinue;
});
uevent_queue_ = std::move(priority_uevents);
uevent_queue_.reserve(uevent_queue_.size() + normal_uevents.size());
uevent_queue_.insert(uevent_queue_.end(), std::make_move_iterator(normal_uevents.begin()),
std::make_move_iterator(normal_uevents.end()));
}
void ColdBoot::GenerateRestoreCon(const std::string& directory) {
std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(directory.c_str()), &closedir);
if (!dir) {
KLOG_WARNING(LOG_TAG, "opendir %s: %s\n", directory.c_str(), strerror(errno));
return;
}
struct dirent* dent;
while ((dent = readdir(dir.get())) != NULL) {
if (strcmp(dent->d_name, ".") == 0 || strcmp(dent->d_name, "..") == 0) continue;
struct stat st;
if (fstatat(dirfd(dir.get()), dent->d_name, &st, 0) == -1) continue;
if (S_ISDIR(st.st_mode)) {
std::string fullpath = directory + "/" + dent->d_name;
auto parallel_restorecon = std::find(parallel_restorecon_queue_.begin(),
parallel_restorecon_queue_.end(), fullpath);
if (parallel_restorecon == parallel_restorecon_queue_.end()) {
restorecon_queue_.push_back(fullpath);
}
}
}
}
void ColdBoot::Run() {
android::base::Timer cold_boot_timer;
RegenerateUevents();
if (enable_parallel_restorecon_) {
if (parallel_restorecon_queue_.empty()) {
parallel_restorecon_queue_.emplace_back("/sys");
// takes long time for /sys/devices, parallelize it
parallel_restorecon_queue_.emplace_back("/sys/devices");
KLOG_INFO(LOG_TAG, "Parallel processing directory is not set, set the default\n");
}
for (const auto& dir : parallel_restorecon_queue_) {
selinux_android_restorecon(dir.c_str(), 0);
GenerateRestoreCon(dir);
}
}
unsigned int parallelism = std::thread::hardware_concurrency() ?: 4;
ColdbootRunner runner(parallelism, uevent_queue_, uevent_handlers_, enable_parallel_restorecon_,
restorecon_queue_);
runner.StartInBackground();
if (!enable_parallel_restorecon_) {
selinux_android_restorecon("/sys", SELINUX_ANDROID_RESTORECON_RECURSE);
}
runner.Wait();
android::base::SetProperty(kColdBootDoneProp, "true");
// When we handle the uevents in the thread pool, the unused memory is not immediately unmapped
// and brought back to the kernel, even after joining the threads. Therefore, we explicitly
// purge all the unused memory here when the coldboot is done.
mallopt(M_PURGE_ALL, 0);
KLOG_INFO(LOG_TAG, "Coldboot took %.3f seconds\n",
cold_boot_timer.duration().count() / 1000.0f);
}
} // namespace init
} // namespace android