2018-12-21 20:41:50 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2019 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_NDEBUG 0
|
|
|
|
#define LOG_TAG "libprocessgroup"
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <task_profiles.h>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include <android-base/file.h>
|
|
|
|
#include <android-base/logging.h>
|
2020-11-21 02:08:51 +01:00
|
|
|
#include <android-base/properties.h>
|
2018-12-21 20:41:50 +01:00
|
|
|
#include <android-base/stringprintf.h>
|
2020-10-26 13:32:52 +01:00
|
|
|
#include <android-base/strings.h>
|
2018-12-21 20:41:50 +01:00
|
|
|
#include <android-base/threads.h>
|
|
|
|
|
|
|
|
#include <cutils/android_filesystem_config.h>
|
|
|
|
|
|
|
|
#include <json/reader.h>
|
|
|
|
#include <json/value.h>
|
|
|
|
|
2019-02-02 23:19:41 +01:00
|
|
|
// To avoid issues in sdk_mac build
|
|
|
|
#if defined(__ANDROID__)
|
|
|
|
#include <sys/prctl.h>
|
|
|
|
#endif
|
|
|
|
|
2018-12-21 20:41:50 +01:00
|
|
|
using android::base::GetThreadId;
|
2020-11-21 02:08:51 +01:00
|
|
|
using android::base::GetUintProperty;
|
2018-12-21 20:41:50 +01:00
|
|
|
using android::base::StringPrintf;
|
2020-10-26 13:32:52 +01:00
|
|
|
using android::base::StringReplace;
|
2018-12-21 20:41:50 +01:00
|
|
|
using android::base::unique_fd;
|
|
|
|
using android::base::WriteStringToFile;
|
|
|
|
|
2020-11-21 02:08:51 +01:00
|
|
|
static constexpr const char* TASK_PROFILE_DB_FILE = "/etc/task_profiles.json";
|
|
|
|
static constexpr const char* TASK_PROFILE_DB_VENDOR_FILE = "/vendor/etc/task_profiles.json";
|
|
|
|
|
|
|
|
static constexpr const char* TEMPLATE_TASK_PROFILE_API_FILE =
|
|
|
|
"/etc/task_profiles/task_profiles_%u.json";
|
2018-12-21 20:41:50 +01:00
|
|
|
|
2022-01-20 19:58:43 +01:00
|
|
|
class FdCacheHelper {
|
|
|
|
public:
|
|
|
|
enum FdState {
|
|
|
|
FDS_INACCESSIBLE = -1,
|
|
|
|
FDS_APP_DEPENDENT = -2,
|
|
|
|
FDS_NOT_CACHED = -3,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void Cache(const std::string& path, android::base::unique_fd& fd);
|
|
|
|
static void Drop(android::base::unique_fd& fd);
|
|
|
|
static void Init(const std::string& path, android::base::unique_fd& fd);
|
|
|
|
static bool IsCached(const android::base::unique_fd& fd) { return fd > FDS_INACCESSIBLE; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
static bool IsAppDependentPath(const std::string& path);
|
|
|
|
};
|
|
|
|
|
|
|
|
void FdCacheHelper::Init(const std::string& path, android::base::unique_fd& fd) {
|
|
|
|
// file descriptors for app-dependent paths can't be cached
|
|
|
|
if (IsAppDependentPath(path)) {
|
|
|
|
// file descriptor is not cached
|
|
|
|
fd.reset(FDS_APP_DEPENDENT);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// file descriptor can be cached later on request
|
|
|
|
fd.reset(FDS_NOT_CACHED);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FdCacheHelper::Cache(const std::string& path, android::base::unique_fd& fd) {
|
|
|
|
if (fd != FDS_NOT_CACHED) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (access(path.c_str(), W_OK) != 0) {
|
|
|
|
// file is not accessible
|
|
|
|
fd.reset(FDS_INACCESSIBLE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
unique_fd tmp_fd(TEMP_FAILURE_RETRY(open(path.c_str(), O_WRONLY | O_CLOEXEC)));
|
|
|
|
if (tmp_fd < 0) {
|
|
|
|
PLOG(ERROR) << "Failed to cache fd '" << path << "'";
|
|
|
|
fd.reset(FDS_INACCESSIBLE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fd = std::move(tmp_fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FdCacheHelper::Drop(android::base::unique_fd& fd) {
|
|
|
|
if (fd == FDS_NOT_CACHED) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fd.reset(FDS_NOT_CACHED);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FdCacheHelper::IsAppDependentPath(const std::string& path) {
|
|
|
|
return path.find("<uid>", 0) != std::string::npos || path.find("<pid>", 0) != std::string::npos;
|
|
|
|
}
|
|
|
|
|
2022-02-03 20:50:16 +01:00
|
|
|
IProfileAttribute::~IProfileAttribute() = default;
|
|
|
|
|
2020-07-01 21:34:17 +02:00
|
|
|
void ProfileAttribute::Reset(const CgroupController& controller, const std::string& file_name) {
|
|
|
|
controller_ = controller;
|
|
|
|
file_name_ = file_name;
|
|
|
|
}
|
|
|
|
|
2018-12-21 20:41:50 +01:00
|
|
|
bool ProfileAttribute::GetPathForTask(int tid, std::string* path) const {
|
|
|
|
std::string subgroup;
|
2019-03-23 01:01:08 +01:00
|
|
|
if (!controller()->GetTaskGroup(tid, &subgroup)) {
|
2018-12-21 20:41:50 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (path == nullptr) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-02-17 02:26:44 +01:00
|
|
|
const std::string& file_name =
|
|
|
|
controller()->version() == 2 && !file_v2_name_.empty() ? file_v2_name_ : file_name_;
|
2018-12-21 20:41:50 +01:00
|
|
|
if (subgroup.empty()) {
|
2022-02-17 02:26:44 +01:00
|
|
|
*path = StringPrintf("%s/%s", controller()->path(), file_name.c_str());
|
2018-12-21 20:41:50 +01:00
|
|
|
} else {
|
2022-02-17 02:26:44 +01:00
|
|
|
*path = StringPrintf("%s/%s/%s", controller()->path(), subgroup.c_str(), file_name.c_str());
|
2018-12-21 20:41:50 +01:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SetClampsAction::ExecuteForProcess(uid_t, pid_t) const {
|
|
|
|
// TODO: add support when kernel supports util_clamp
|
|
|
|
LOG(WARNING) << "SetClampsAction::ExecuteForProcess is not supported";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SetClampsAction::ExecuteForTask(int) const {
|
|
|
|
// TODO: add support when kernel supports util_clamp
|
|
|
|
LOG(WARNING) << "SetClampsAction::ExecuteForTask is not supported";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-02-02 23:19:41 +01:00
|
|
|
// To avoid issues in sdk_mac build
|
|
|
|
#if defined(__ANDROID__)
|
|
|
|
|
2018-12-21 20:41:50 +01:00
|
|
|
bool SetTimerSlackAction::IsTimerSlackSupported(int tid) {
|
|
|
|
auto file = StringPrintf("/proc/%d/timerslack_ns", tid);
|
|
|
|
|
|
|
|
return (access(file.c_str(), W_OK) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SetTimerSlackAction::ExecuteForTask(int tid) const {
|
|
|
|
static bool sys_supports_timerslack = IsTimerSlackSupported(tid);
|
|
|
|
|
|
|
|
// v4.6+ kernels support the /proc/<tid>/timerslack_ns interface.
|
|
|
|
// TODO: once we've backported this, log if the open(2) fails.
|
|
|
|
if (sys_supports_timerslack) {
|
|
|
|
auto file = StringPrintf("/proc/%d/timerslack_ns", tid);
|
|
|
|
if (!WriteStringToFile(std::to_string(slack_), file)) {
|
2019-02-13 02:30:26 +01:00
|
|
|
if (errno == ENOENT) {
|
|
|
|
// This happens when process is already dead
|
|
|
|
return true;
|
|
|
|
}
|
2018-12-21 20:41:50 +01:00
|
|
|
PLOG(ERROR) << "set_timerslack_ns write failed";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Remove when /proc/<tid>/timerslack_ns interface is backported.
|
|
|
|
if (tid == 0 || tid == GetThreadId()) {
|
|
|
|
if (prctl(PR_SET_TIMERSLACK, slack_) == -1) {
|
|
|
|
PLOG(ERROR) << "set_timerslack_ns prctl failed";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-01-24 20:45:59 +01:00
|
|
|
#else
|
|
|
|
|
|
|
|
bool SetTimerSlackAction::ExecuteForTask(int) const {
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2019-02-02 23:19:41 +01:00
|
|
|
#endif
|
|
|
|
|
2018-12-21 20:41:50 +01:00
|
|
|
bool SetAttributeAction::ExecuteForProcess(uid_t, pid_t pid) const {
|
|
|
|
return ExecuteForTask(pid);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SetAttributeAction::ExecuteForTask(int tid) const {
|
|
|
|
std::string path;
|
|
|
|
|
|
|
|
if (!attribute_->GetPathForTask(tid, &path)) {
|
2019-03-07 20:59:12 +01:00
|
|
|
LOG(ERROR) << "Failed to find cgroup for tid " << tid;
|
2018-12-21 20:41:50 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!WriteStringToFile(value_, path)) {
|
2022-03-23 00:15:00 +01:00
|
|
|
if (access(path.c_str(), F_OK) < 0) {
|
2022-01-24 22:08:57 +01:00
|
|
|
if (optional_) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
LOG(ERROR) << "No such cgroup attribute: " << path;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2022-03-31 20:26:42 +02:00
|
|
|
// The PLOG() statement below uses the error code stored in `errno` by
|
|
|
|
// WriteStringToFile() because access() only overwrites `errno` if it fails
|
|
|
|
// and because this code is only reached if the access() function returns 0.
|
2018-12-21 20:41:50 +01:00
|
|
|
PLOG(ERROR) << "Failed to write '" << value_ << "' to " << path;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-11-21 08:57:36 +01:00
|
|
|
SetCgroupAction::SetCgroupAction(const CgroupController& c, const std::string& p)
|
|
|
|
: controller_(c), path_(p) {
|
2022-01-21 00:41:28 +01:00
|
|
|
FdCacheHelper::Init(controller_.GetTasksFilePath(path_), fd_[ProfileAction::RCT_TASK]);
|
|
|
|
// uid and pid don't matter because IsAppDependentPath ensures the path doesn't use them
|
|
|
|
FdCacheHelper::Init(controller_.GetProcsFilePath(path_, 0, 0), fd_[ProfileAction::RCT_PROCESS]);
|
2021-11-21 08:57:36 +01:00
|
|
|
}
|
|
|
|
|
2021-09-03 04:47:12 +02:00
|
|
|
bool SetCgroupAction::AddTidToCgroup(int tid, int fd, const char* controller_name) {
|
2018-12-21 20:41:50 +01:00
|
|
|
if (tid <= 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string value = std::to_string(tid);
|
|
|
|
|
2021-09-03 04:47:12 +02:00
|
|
|
if (TEMP_FAILURE_RETRY(write(fd, value.c_str(), value.length())) == value.length()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the thread is in the process of exiting, don't flag an error
|
|
|
|
if (errno == ESRCH) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ENOSPC is returned when cpuset cgroup that we are joining has no online cpus
|
|
|
|
if (errno == ENOSPC && !strcmp(controller_name, "cpuset")) {
|
|
|
|
// This is an abnormal case happening only in testing, so report it only once
|
|
|
|
static bool empty_cpuset_reported = false;
|
|
|
|
|
|
|
|
if (empty_cpuset_reported) {
|
|
|
|
return true;
|
2018-12-21 20:41:50 +01:00
|
|
|
}
|
2021-09-03 04:47:12 +02:00
|
|
|
|
|
|
|
LOG(ERROR) << "Failed to add task '" << value
|
|
|
|
<< "' into cpuset because all cpus in that cpuset are offline";
|
|
|
|
empty_cpuset_reported = true;
|
|
|
|
} else {
|
|
|
|
PLOG(ERROR) << "AddTidToCgroup failed to write '" << value << "'; fd=" << fd;
|
2018-12-21 20:41:50 +01:00
|
|
|
}
|
|
|
|
|
2021-09-03 04:47:12 +02:00
|
|
|
return false;
|
2018-12-21 20:41:50 +01:00
|
|
|
}
|
|
|
|
|
2022-01-21 00:41:28 +01:00
|
|
|
ProfileAction::CacheUseResult SetCgroupAction::UseCachedFd(ResourceCacheType cache_type,
|
|
|
|
int id) const {
|
|
|
|
std::lock_guard<std::mutex> lock(fd_mutex_);
|
|
|
|
if (FdCacheHelper::IsCached(fd_[cache_type])) {
|
|
|
|
// fd is cached, reuse it
|
|
|
|
if (!AddTidToCgroup(id, fd_[cache_type], controller()->name())) {
|
|
|
|
LOG(ERROR) << "Failed to add task into cgroup";
|
|
|
|
return ProfileAction::FAIL;
|
|
|
|
}
|
|
|
|
return ProfileAction::SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fd_[cache_type] == FdCacheHelper::FDS_INACCESSIBLE) {
|
|
|
|
// no permissions to access the file, ignore
|
|
|
|
return ProfileAction::SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cache_type == ResourceCacheType::RCT_TASK &&
|
|
|
|
fd_[cache_type] == FdCacheHelper::FDS_APP_DEPENDENT) {
|
|
|
|
// application-dependent path can't be used with tid
|
|
|
|
PLOG(ERROR) << "Application profile can't be applied to a thread";
|
|
|
|
return ProfileAction::FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ProfileAction::UNUSED;
|
|
|
|
}
|
|
|
|
|
2018-12-21 20:41:50 +01:00
|
|
|
bool SetCgroupAction::ExecuteForProcess(uid_t uid, pid_t pid) const {
|
2022-01-21 00:41:28 +01:00
|
|
|
CacheUseResult result = UseCachedFd(ProfileAction::RCT_PROCESS, pid);
|
|
|
|
if (result != ProfileAction::UNUSED) {
|
|
|
|
return result == ProfileAction::SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
// fd was not cached or cached fd can't be used
|
2019-03-23 01:01:08 +01:00
|
|
|
std::string procs_path = controller()->GetProcsFilePath(path_, uid, pid);
|
2018-12-21 20:41:50 +01:00
|
|
|
unique_fd tmp_fd(TEMP_FAILURE_RETRY(open(procs_path.c_str(), O_WRONLY | O_CLOEXEC)));
|
|
|
|
if (tmp_fd < 0) {
|
2019-03-15 04:06:36 +01:00
|
|
|
PLOG(WARNING) << "Failed to open " << procs_path;
|
2018-12-21 20:41:50 +01:00
|
|
|
return false;
|
|
|
|
}
|
2021-09-03 04:47:12 +02:00
|
|
|
if (!AddTidToCgroup(pid, tmp_fd, controller()->name())) {
|
2019-03-07 20:59:12 +01:00
|
|
|
LOG(ERROR) << "Failed to add task into cgroup";
|
2018-12-21 20:41:50 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SetCgroupAction::ExecuteForTask(int tid) const {
|
2022-01-21 00:41:28 +01:00
|
|
|
CacheUseResult result = UseCachedFd(ProfileAction::RCT_TASK, tid);
|
|
|
|
if (result != ProfileAction::UNUSED) {
|
|
|
|
return result == ProfileAction::SUCCESS;
|
2019-02-14 23:40:41 +01:00
|
|
|
}
|
|
|
|
|
2022-01-21 00:41:28 +01:00
|
|
|
// fd was not cached or cached fd can't be used
|
2019-03-23 01:01:08 +01:00
|
|
|
std::string tasks_path = controller()->GetTasksFilePath(path_);
|
2019-02-06 01:44:22 +01:00
|
|
|
unique_fd tmp_fd(TEMP_FAILURE_RETRY(open(tasks_path.c_str(), O_WRONLY | O_CLOEXEC)));
|
|
|
|
if (tmp_fd < 0) {
|
2021-11-21 08:57:36 +01:00
|
|
|
PLOG(WARNING) << "Failed to open " << tasks_path;
|
2019-02-14 23:40:41 +01:00
|
|
|
return false;
|
2019-02-06 01:44:22 +01:00
|
|
|
}
|
2021-09-03 04:47:12 +02:00
|
|
|
if (!AddTidToCgroup(tid, tmp_fd, controller()->name())) {
|
2019-03-07 20:59:12 +01:00
|
|
|
LOG(ERROR) << "Failed to add task into cgroup";
|
2019-02-06 01:44:22 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2018-12-21 20:41:50 +01:00
|
|
|
}
|
|
|
|
|
2022-01-21 00:41:28 +01:00
|
|
|
void SetCgroupAction::EnableResourceCaching(ResourceCacheType cache_type) {
|
2022-01-20 19:58:43 +01:00
|
|
|
std::lock_guard<std::mutex> lock(fd_mutex_);
|
2022-01-21 00:41:28 +01:00
|
|
|
// Return early to prevent unnecessary calls to controller_.Get{Tasks|Procs}FilePath() which
|
|
|
|
// include regex evaluations
|
|
|
|
if (fd_[cache_type] != FdCacheHelper::FDS_NOT_CACHED) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
switch (cache_type) {
|
|
|
|
case (ProfileAction::RCT_TASK):
|
|
|
|
FdCacheHelper::Cache(controller_.GetTasksFilePath(path_), fd_[cache_type]);
|
|
|
|
break;
|
|
|
|
case (ProfileAction::RCT_PROCESS):
|
|
|
|
// uid and pid don't matter because IsAppDependentPath ensures the path doesn't use them
|
|
|
|
FdCacheHelper::Cache(controller_.GetProcsFilePath(path_, 0, 0), fd_[cache_type]);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LOG(ERROR) << "Invalid cache type is specified!";
|
|
|
|
break;
|
|
|
|
}
|
2022-01-20 19:58:43 +01:00
|
|
|
}
|
|
|
|
|
2022-01-21 00:41:28 +01:00
|
|
|
void SetCgroupAction::DropResourceCaching(ResourceCacheType cache_type) {
|
2022-01-20 19:58:43 +01:00
|
|
|
std::lock_guard<std::mutex> lock(fd_mutex_);
|
2022-01-21 00:41:28 +01:00
|
|
|
FdCacheHelper::Drop(fd_[cache_type]);
|
2022-01-20 19:58:43 +01:00
|
|
|
}
|
|
|
|
|
2022-02-10 09:44:43 +01:00
|
|
|
WriteFileAction::WriteFileAction(const std::string& task_path, const std::string& proc_path,
|
|
|
|
const std::string& value, bool logfailures)
|
|
|
|
: task_path_(task_path), proc_path_(proc_path), value_(value), logfailures_(logfailures) {
|
|
|
|
FdCacheHelper::Init(task_path_, fd_[ProfileAction::RCT_TASK]);
|
|
|
|
if (!proc_path_.empty()) FdCacheHelper::Init(proc_path_, fd_[ProfileAction::RCT_PROCESS]);
|
2021-11-21 08:57:36 +01:00
|
|
|
}
|
2020-10-26 13:32:52 +01:00
|
|
|
|
2022-02-10 09:44:43 +01:00
|
|
|
bool WriteFileAction::WriteValueToFile(const std::string& value_, ResourceCacheType cache_type,
|
|
|
|
int uid, int pid, bool logfailures) const {
|
|
|
|
std::string value(value_);
|
|
|
|
|
|
|
|
value = StringReplace(value, "<uid>", std::to_string(uid), true);
|
|
|
|
value = StringReplace(value, "<pid>", std::to_string(pid), true);
|
|
|
|
|
|
|
|
CacheUseResult result = UseCachedFd(cache_type, value);
|
|
|
|
|
|
|
|
if (result != ProfileAction::UNUSED) {
|
|
|
|
return result == ProfileAction::SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string path;
|
|
|
|
if (cache_type == ProfileAction::RCT_TASK || proc_path_.empty()) {
|
|
|
|
path = task_path_;
|
|
|
|
} else {
|
|
|
|
path = proc_path_;
|
|
|
|
}
|
|
|
|
|
2021-11-21 08:57:36 +01:00
|
|
|
// Use WriteStringToFd instead of WriteStringToFile because the latter will open file with
|
|
|
|
// O_TRUNC which causes kernfs_mutex contention
|
|
|
|
unique_fd tmp_fd(TEMP_FAILURE_RETRY(open(path.c_str(), O_WRONLY | O_CLOEXEC)));
|
|
|
|
|
|
|
|
if (tmp_fd < 0) {
|
|
|
|
if (logfailures) PLOG(WARNING) << "Failed to open " << path;
|
|
|
|
return false;
|
|
|
|
}
|
2020-10-26 13:32:52 +01:00
|
|
|
|
2021-11-21 08:57:36 +01:00
|
|
|
if (!WriteStringToFd(value, tmp_fd)) {
|
|
|
|
if (logfailures) PLOG(ERROR) << "Failed to write '" << value << "' to " << path;
|
2020-10-26 13:32:52 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-01-21 00:41:28 +01:00
|
|
|
ProfileAction::CacheUseResult WriteFileAction::UseCachedFd(ResourceCacheType cache_type,
|
|
|
|
const std::string& value) const {
|
2021-11-21 08:57:36 +01:00
|
|
|
std::lock_guard<std::mutex> lock(fd_mutex_);
|
2022-02-10 09:44:43 +01:00
|
|
|
if (FdCacheHelper::IsCached(fd_[cache_type])) {
|
2022-01-21 00:41:28 +01:00
|
|
|
// fd is cached, reuse it
|
2022-02-10 09:44:43 +01:00
|
|
|
bool ret = WriteStringToFd(value, fd_[cache_type]);
|
|
|
|
|
|
|
|
if (!ret && logfailures_) {
|
|
|
|
if (cache_type == ProfileAction::RCT_TASK || proc_path_.empty()) {
|
|
|
|
PLOG(ERROR) << "Failed to write '" << value << "' to " << task_path_;
|
|
|
|
} else {
|
|
|
|
PLOG(ERROR) << "Failed to write '" << value << "' to " << proc_path_;
|
|
|
|
}
|
2022-01-21 00:41:28 +01:00
|
|
|
}
|
2022-02-10 09:44:43 +01:00
|
|
|
return ret ? ProfileAction::SUCCESS : ProfileAction::FAIL;
|
2022-01-21 00:41:28 +01:00
|
|
|
}
|
|
|
|
|
2022-02-10 09:44:43 +01:00
|
|
|
if (fd_[cache_type] == FdCacheHelper::FDS_INACCESSIBLE) {
|
2022-01-21 00:41:28 +01:00
|
|
|
// no permissions to access the file, ignore
|
|
|
|
return ProfileAction::SUCCESS;
|
|
|
|
}
|
|
|
|
|
2022-02-10 09:44:43 +01:00
|
|
|
if (cache_type == ResourceCacheType::RCT_TASK &&
|
|
|
|
fd_[cache_type] == FdCacheHelper::FDS_APP_DEPENDENT) {
|
2022-01-21 00:41:28 +01:00
|
|
|
// application-dependent path can't be used with tid
|
|
|
|
PLOG(ERROR) << "Application profile can't be applied to a thread";
|
|
|
|
return ProfileAction::FAIL;
|
|
|
|
}
|
|
|
|
return ProfileAction::UNUSED;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WriteFileAction::ExecuteForProcess(uid_t uid, pid_t pid) const {
|
2022-02-10 09:44:43 +01:00
|
|
|
if (!proc_path_.empty()) {
|
|
|
|
return WriteValueToFile(value_, ProfileAction::RCT_PROCESS, uid, pid, logfailures_);
|
|
|
|
}
|
2021-11-21 08:57:36 +01:00
|
|
|
|
2022-02-10 09:44:43 +01:00
|
|
|
DIR* d;
|
|
|
|
struct dirent* de;
|
|
|
|
char proc_path[255];
|
|
|
|
int t_pid;
|
2022-01-21 00:41:28 +01:00
|
|
|
|
2022-02-10 09:44:43 +01:00
|
|
|
sprintf(proc_path, "/proc/%d/task", pid);
|
|
|
|
if (!(d = opendir(proc_path))) {
|
|
|
|
return false;
|
2022-01-21 00:41:28 +01:00
|
|
|
}
|
|
|
|
|
2022-02-10 09:44:43 +01:00
|
|
|
while ((de = readdir(d))) {
|
|
|
|
if (de->d_name[0] == '.') {
|
|
|
|
continue;
|
|
|
|
}
|
2021-11-21 08:57:36 +01:00
|
|
|
|
2022-02-10 09:44:43 +01:00
|
|
|
t_pid = atoi(de->d_name);
|
2021-11-21 08:57:36 +01:00
|
|
|
|
2022-02-10 09:44:43 +01:00
|
|
|
if (!t_pid) {
|
|
|
|
continue;
|
|
|
|
}
|
2020-10-26 13:32:52 +01:00
|
|
|
|
2022-02-10 09:44:43 +01:00
|
|
|
WriteValueToFile(value_, ProfileAction::RCT_TASK, uid, t_pid, logfailures_);
|
2020-10-26 13:32:52 +01:00
|
|
|
}
|
|
|
|
|
2022-02-10 09:44:43 +01:00
|
|
|
closedir(d);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WriteFileAction::ExecuteForTask(int tid) const {
|
|
|
|
return WriteValueToFile(value_, ProfileAction::RCT_TASK, getuid(), tid, logfailures_);
|
2020-10-26 13:32:52 +01:00
|
|
|
}
|
|
|
|
|
2022-02-10 09:44:43 +01:00
|
|
|
void WriteFileAction::EnableResourceCaching(ResourceCacheType cache_type) {
|
2022-01-20 19:58:43 +01:00
|
|
|
std::lock_guard<std::mutex> lock(fd_mutex_);
|
2022-02-10 09:44:43 +01:00
|
|
|
if (fd_[cache_type] != FdCacheHelper::FDS_NOT_CACHED) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
switch (cache_type) {
|
|
|
|
case (ProfileAction::RCT_TASK):
|
|
|
|
FdCacheHelper::Cache(task_path_, fd_[cache_type]);
|
|
|
|
break;
|
|
|
|
case (ProfileAction::RCT_PROCESS):
|
|
|
|
if (!proc_path_.empty()) FdCacheHelper::Cache(proc_path_, fd_[cache_type]);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LOG(ERROR) << "Invalid cache type is specified!";
|
|
|
|
break;
|
|
|
|
}
|
2022-01-20 19:58:43 +01:00
|
|
|
}
|
|
|
|
|
2022-02-10 09:44:43 +01:00
|
|
|
void WriteFileAction::DropResourceCaching(ResourceCacheType cache_type) {
|
2022-01-20 19:58:43 +01:00
|
|
|
std::lock_guard<std::mutex> lock(fd_mutex_);
|
2022-02-10 09:44:43 +01:00
|
|
|
FdCacheHelper::Drop(fd_[cache_type]);
|
2022-01-20 19:58:43 +01:00
|
|
|
}
|
|
|
|
|
2019-09-16 13:07:17 +02:00
|
|
|
bool ApplyProfileAction::ExecuteForProcess(uid_t uid, pid_t pid) const {
|
|
|
|
for (const auto& profile : profiles_) {
|
2022-01-21 00:41:28 +01:00
|
|
|
profile->ExecuteForProcess(uid, pid);
|
2019-09-16 13:07:17 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ApplyProfileAction::ExecuteForTask(int tid) const {
|
|
|
|
for (const auto& profile : profiles_) {
|
2021-05-14 21:34:54 +02:00
|
|
|
profile->ExecuteForTask(tid);
|
2019-09-16 13:07:17 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-01-21 00:41:28 +01:00
|
|
|
void ApplyProfileAction::EnableResourceCaching(ResourceCacheType cache_type) {
|
2020-02-14 02:28:00 +01:00
|
|
|
for (const auto& profile : profiles_) {
|
2022-01-21 00:41:28 +01:00
|
|
|
profile->EnableResourceCaching(cache_type);
|
2020-02-14 02:28:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-21 00:41:28 +01:00
|
|
|
void ApplyProfileAction::DropResourceCaching(ResourceCacheType cache_type) {
|
2020-02-14 02:28:00 +01:00
|
|
|
for (const auto& profile : profiles_) {
|
2022-01-21 00:41:28 +01:00
|
|
|
profile->DropResourceCaching(cache_type);
|
2020-02-14 02:28:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-25 01:36:10 +01:00
|
|
|
void TaskProfile::MoveTo(TaskProfile* profile) {
|
|
|
|
profile->elements_ = std::move(elements_);
|
|
|
|
profile->res_cached_ = res_cached_;
|
|
|
|
}
|
|
|
|
|
2018-12-21 20:41:50 +01:00
|
|
|
bool TaskProfile::ExecuteForProcess(uid_t uid, pid_t pid) const {
|
|
|
|
for (const auto& element : elements_) {
|
|
|
|
if (!element->ExecuteForProcess(uid, pid)) {
|
2022-01-24 20:59:13 +01:00
|
|
|
LOG(VERBOSE) << "Applying profile action " << element->Name() << " failed";
|
2018-12-21 20:41:50 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TaskProfile::ExecuteForTask(int tid) const {
|
|
|
|
if (tid == 0) {
|
|
|
|
tid = GetThreadId();
|
|
|
|
}
|
|
|
|
for (const auto& element : elements_) {
|
|
|
|
if (!element->ExecuteForTask(tid)) {
|
2022-01-24 20:59:13 +01:00
|
|
|
LOG(VERBOSE) << "Applying profile action " << element->Name() << " failed";
|
2018-12-21 20:41:50 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-01-21 00:41:28 +01:00
|
|
|
void TaskProfile::EnableResourceCaching(ProfileAction::ResourceCacheType cache_type) {
|
2019-02-14 23:40:41 +01:00
|
|
|
if (res_cached_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto& element : elements_) {
|
2022-01-21 00:41:28 +01:00
|
|
|
element->EnableResourceCaching(cache_type);
|
2019-02-14 23:40:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
res_cached_ = true;
|
|
|
|
}
|
|
|
|
|
2022-01-21 00:41:28 +01:00
|
|
|
void TaskProfile::DropResourceCaching(ProfileAction::ResourceCacheType cache_type) {
|
2019-06-18 23:53:53 +02:00
|
|
|
if (!res_cached_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto& element : elements_) {
|
2022-01-21 00:41:28 +01:00
|
|
|
element->DropResourceCaching(cache_type);
|
2019-06-18 23:53:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
res_cached_ = false;
|
|
|
|
}
|
|
|
|
|
2022-01-21 00:41:28 +01:00
|
|
|
void TaskProfiles::DropResourceCaching(ProfileAction::ResourceCacheType cache_type) const {
|
2019-06-18 23:53:53 +02:00
|
|
|
for (auto& iter : profiles_) {
|
2022-01-21 00:41:28 +01:00
|
|
|
iter.second->DropResourceCaching(cache_type);
|
2019-06-18 23:53:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-21 20:41:50 +01:00
|
|
|
TaskProfiles& TaskProfiles::GetInstance() {
|
2019-03-21 05:09:46 +01:00
|
|
|
// Deliberately leak this object to avoid a race between destruction on
|
|
|
|
// process exit and concurrent access from another thread.
|
|
|
|
static auto* instance = new TaskProfiles;
|
|
|
|
return *instance;
|
2018-12-21 20:41:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TaskProfiles::TaskProfiles() {
|
2020-12-03 20:38:42 +01:00
|
|
|
// load system task profiles
|
|
|
|
if (!Load(CgroupMap::GetInstance(), TASK_PROFILE_DB_FILE)) {
|
|
|
|
LOG(ERROR) << "Loading " << TASK_PROFILE_DB_FILE << " for [" << getpid() << "] failed";
|
|
|
|
}
|
2020-11-21 02:08:51 +01:00
|
|
|
|
|
|
|
// load API-level specific system task profiles if available
|
2020-12-03 20:38:42 +01:00
|
|
|
unsigned int api_level = GetUintProperty<unsigned int>("ro.product.first_api_level", 0);
|
2020-11-21 02:08:51 +01:00
|
|
|
if (api_level > 0) {
|
|
|
|
std::string api_profiles_path =
|
|
|
|
android::base::StringPrintf(TEMPLATE_TASK_PROFILE_API_FILE, api_level);
|
|
|
|
if (!access(api_profiles_path.c_str(), F_OK) || errno != ENOENT) {
|
2020-12-03 20:38:42 +01:00
|
|
|
if (!Load(CgroupMap::GetInstance(), api_profiles_path)) {
|
2022-01-20 19:58:43 +01:00
|
|
|
LOG(ERROR) << "Loading " << api_profiles_path << " for [" << getpid() << "] failed";
|
2020-12-03 20:38:42 +01:00
|
|
|
}
|
2020-11-21 02:08:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-20 00:01:28 +01:00
|
|
|
// load vendor task profiles if the file exists
|
|
|
|
if (!access(TASK_PROFILE_DB_VENDOR_FILE, F_OK) &&
|
|
|
|
!Load(CgroupMap::GetInstance(), TASK_PROFILE_DB_VENDOR_FILE)) {
|
|
|
|
LOG(ERROR) << "Loading " << TASK_PROFILE_DB_VENDOR_FILE << " for [" << getpid()
|
|
|
|
<< "] failed";
|
2018-12-21 20:41:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-20 00:01:28 +01:00
|
|
|
bool TaskProfiles::Load(const CgroupMap& cg_map, const std::string& file_name) {
|
2018-12-21 20:41:50 +01:00
|
|
|
std::string json_doc;
|
|
|
|
|
2019-02-20 00:01:28 +01:00
|
|
|
if (!android::base::ReadFileToString(file_name, &json_doc)) {
|
|
|
|
LOG(ERROR) << "Failed to read task profiles from " << file_name;
|
2018-12-21 20:41:50 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-02-25 02:34:50 +01:00
|
|
|
Json::CharReaderBuilder builder;
|
|
|
|
std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
|
2018-12-21 20:41:50 +01:00
|
|
|
Json::Value root;
|
2021-02-25 02:34:50 +01:00
|
|
|
std::string errorMessage;
|
|
|
|
if (!reader->parse(&*json_doc.begin(), &*json_doc.end(), &root, &errorMessage)) {
|
|
|
|
LOG(ERROR) << "Failed to parse task profiles: " << errorMessage;
|
2018-12-21 20:41:50 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-02-21 01:17:22 +01:00
|
|
|
const Json::Value& attr = root["Attributes"];
|
2018-12-21 20:41:50 +01:00
|
|
|
for (Json::Value::ArrayIndex i = 0; i < attr.size(); ++i) {
|
|
|
|
std::string name = attr[i]["Name"].asString();
|
2019-02-20 00:01:28 +01:00
|
|
|
std::string controller_name = attr[i]["Controller"].asString();
|
|
|
|
std::string file_attr = attr[i]["File"].asString();
|
2022-02-17 02:26:44 +01:00
|
|
|
std::string file_v2_attr = attr[i]["FileV2"].asString();
|
|
|
|
|
|
|
|
if (!file_v2_attr.empty() && file_attr.empty()) {
|
|
|
|
LOG(ERROR) << "Attribute " << name << " has FileV2 but no File property";
|
|
|
|
return false;
|
|
|
|
}
|
2018-12-21 20:41:50 +01:00
|
|
|
|
2020-07-01 21:34:17 +02:00
|
|
|
auto controller = cg_map.FindController(controller_name);
|
|
|
|
if (controller.HasValue()) {
|
|
|
|
auto iter = attributes_.find(name);
|
|
|
|
if (iter == attributes_.end()) {
|
2022-02-17 02:26:44 +01:00
|
|
|
attributes_[name] =
|
|
|
|
std::make_unique<ProfileAttribute>(controller, file_attr, file_v2_attr);
|
2018-12-21 20:41:50 +01:00
|
|
|
} else {
|
2020-07-01 21:34:17 +02:00
|
|
|
iter->second->Reset(controller, file_attr);
|
2018-12-21 20:41:50 +01:00
|
|
|
}
|
|
|
|
} else {
|
2020-07-01 21:34:17 +02:00
|
|
|
LOG(WARNING) << "Controller " << controller_name << " is not found";
|
2018-12-21 20:41:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-21 01:17:22 +01:00
|
|
|
const Json::Value& profiles_val = root["Profiles"];
|
|
|
|
for (Json::Value::ArrayIndex i = 0; i < profiles_val.size(); ++i) {
|
|
|
|
const Json::Value& profile_val = profiles_val[i];
|
2018-12-21 20:41:50 +01:00
|
|
|
|
2019-02-21 01:17:22 +01:00
|
|
|
std::string profile_name = profile_val["Name"].asString();
|
|
|
|
const Json::Value& actions = profile_val["Actions"];
|
2022-01-24 20:59:13 +01:00
|
|
|
auto profile = std::make_shared<TaskProfile>(profile_name);
|
2018-12-21 20:41:50 +01:00
|
|
|
|
2019-02-21 01:17:22 +01:00
|
|
|
for (Json::Value::ArrayIndex act_idx = 0; act_idx < actions.size(); ++act_idx) {
|
|
|
|
const Json::Value& action_val = actions[act_idx];
|
|
|
|
std::string action_name = action_val["Name"].asString();
|
|
|
|
const Json::Value& params_val = action_val["Params"];
|
|
|
|
if (action_name == "JoinCgroup") {
|
|
|
|
std::string controller_name = params_val["Controller"].asString();
|
|
|
|
std::string path = params_val["Path"].asString();
|
2018-12-21 20:41:50 +01:00
|
|
|
|
2019-03-23 01:01:08 +01:00
|
|
|
auto controller = cg_map.FindController(controller_name);
|
|
|
|
if (controller.HasValue()) {
|
2018-12-21 20:41:50 +01:00
|
|
|
profile->Add(std::make_unique<SetCgroupAction>(controller, path));
|
|
|
|
} else {
|
2019-02-20 00:01:28 +01:00
|
|
|
LOG(WARNING) << "JoinCgroup: controller " << controller_name << " is not found";
|
2018-12-21 20:41:50 +01:00
|
|
|
}
|
2019-02-21 01:17:22 +01:00
|
|
|
} else if (action_name == "SetTimerSlack") {
|
|
|
|
std::string slack_value = params_val["Slack"].asString();
|
2018-12-21 20:41:50 +01:00
|
|
|
char* end;
|
|
|
|
unsigned long slack;
|
|
|
|
|
2019-02-21 01:17:22 +01:00
|
|
|
slack = strtoul(slack_value.c_str(), &end, 10);
|
|
|
|
if (end > slack_value.c_str()) {
|
2018-12-21 20:41:50 +01:00
|
|
|
profile->Add(std::make_unique<SetTimerSlackAction>(slack));
|
|
|
|
} else {
|
2019-02-21 01:17:22 +01:00
|
|
|
LOG(WARNING) << "SetTimerSlack: invalid parameter: " << slack_value;
|
2018-12-21 20:41:50 +01:00
|
|
|
}
|
2019-02-21 01:17:22 +01:00
|
|
|
} else if (action_name == "SetAttribute") {
|
|
|
|
std::string attr_name = params_val["Name"].asString();
|
|
|
|
std::string attr_value = params_val["Value"].asString();
|
2022-01-24 22:08:57 +01:00
|
|
|
bool optional = strcmp(params_val["Optional"].asString().c_str(), "true") == 0;
|
2018-12-21 20:41:50 +01:00
|
|
|
|
2019-02-21 01:17:22 +01:00
|
|
|
auto iter = attributes_.find(attr_name);
|
2018-12-21 20:41:50 +01:00
|
|
|
if (iter != attributes_.end()) {
|
2022-01-24 22:08:57 +01:00
|
|
|
profile->Add(std::make_unique<SetAttributeAction>(iter->second.get(),
|
|
|
|
attr_value, optional));
|
2018-12-21 20:41:50 +01:00
|
|
|
} else {
|
2019-02-21 01:17:22 +01:00
|
|
|
LOG(WARNING) << "SetAttribute: unknown attribute: " << attr_name;
|
2018-12-21 20:41:50 +01:00
|
|
|
}
|
2019-02-21 01:17:22 +01:00
|
|
|
} else if (action_name == "SetClamps") {
|
|
|
|
std::string boost_value = params_val["Boost"].asString();
|
|
|
|
std::string clamp_value = params_val["Clamp"].asString();
|
2018-12-21 20:41:50 +01:00
|
|
|
char* end;
|
|
|
|
unsigned long boost;
|
|
|
|
|
2019-02-21 01:17:22 +01:00
|
|
|
boost = strtoul(boost_value.c_str(), &end, 10);
|
|
|
|
if (end > boost_value.c_str()) {
|
|
|
|
unsigned long clamp = strtoul(clamp_value.c_str(), &end, 10);
|
|
|
|
if (end > clamp_value.c_str()) {
|
2018-12-21 20:41:50 +01:00
|
|
|
profile->Add(std::make_unique<SetClampsAction>(boost, clamp));
|
|
|
|
} else {
|
2019-02-21 01:17:22 +01:00
|
|
|
LOG(WARNING) << "SetClamps: invalid parameter " << clamp_value;
|
2018-12-21 20:41:50 +01:00
|
|
|
}
|
|
|
|
} else {
|
2019-02-21 01:17:22 +01:00
|
|
|
LOG(WARNING) << "SetClamps: invalid parameter: " << boost_value;
|
2018-12-21 20:41:50 +01:00
|
|
|
}
|
2020-10-26 13:32:52 +01:00
|
|
|
} else if (action_name == "WriteFile") {
|
|
|
|
std::string attr_filepath = params_val["FilePath"].asString();
|
2022-02-10 09:44:43 +01:00
|
|
|
std::string attr_procfilepath = params_val["ProcFilePath"].asString();
|
2020-10-26 13:32:52 +01:00
|
|
|
std::string attr_value = params_val["Value"].asString();
|
2022-02-10 09:44:43 +01:00
|
|
|
// FilePath and Value are mandatory
|
2020-10-26 13:32:52 +01:00
|
|
|
if (!attr_filepath.empty() && !attr_value.empty()) {
|
2021-04-08 16:10:06 +02:00
|
|
|
std::string attr_logfailures = params_val["LogFailures"].asString();
|
|
|
|
bool logfailures = attr_logfailures.empty() || attr_logfailures == "true";
|
2022-02-10 09:44:43 +01:00
|
|
|
profile->Add(std::make_unique<WriteFileAction>(attr_filepath, attr_procfilepath,
|
|
|
|
attr_value, logfailures));
|
2020-10-26 13:32:52 +01:00
|
|
|
} else if (attr_filepath.empty()) {
|
|
|
|
LOG(WARNING) << "WriteFile: invalid parameter: "
|
|
|
|
<< "empty filepath";
|
|
|
|
} else if (attr_value.empty()) {
|
|
|
|
LOG(WARNING) << "WriteFile: invalid parameter: "
|
|
|
|
<< "empty value";
|
|
|
|
}
|
2018-12-21 20:41:50 +01:00
|
|
|
} else {
|
2019-02-21 01:17:22 +01:00
|
|
|
LOG(WARNING) << "Unknown profile action: " << action_name;
|
2018-12-21 20:41:50 +01:00
|
|
|
}
|
|
|
|
}
|
2020-01-25 01:36:10 +01:00
|
|
|
auto iter = profiles_.find(profile_name);
|
|
|
|
if (iter == profiles_.end()) {
|
|
|
|
profiles_[profile_name] = profile;
|
|
|
|
} else {
|
|
|
|
// Move the content rather that replace the profile because old profile might be
|
|
|
|
// referenced from an aggregate profile if vendor overrides task profiles
|
|
|
|
profile->MoveTo(iter->second.get());
|
|
|
|
profile.reset();
|
|
|
|
}
|
2019-09-16 13:07:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const Json::Value& aggregateprofiles_val = root["AggregateProfiles"];
|
|
|
|
for (Json::Value::ArrayIndex i = 0; i < aggregateprofiles_val.size(); ++i) {
|
|
|
|
const Json::Value& aggregateprofile_val = aggregateprofiles_val[i];
|
|
|
|
|
|
|
|
std::string aggregateprofile_name = aggregateprofile_val["Name"].asString();
|
|
|
|
const Json::Value& aggregateprofiles = aggregateprofile_val["Profiles"];
|
|
|
|
std::vector<std::shared_ptr<TaskProfile>> profiles;
|
|
|
|
bool ret = true;
|
|
|
|
|
|
|
|
for (Json::Value::ArrayIndex pf_idx = 0; pf_idx < aggregateprofiles.size(); ++pf_idx) {
|
|
|
|
std::string profile_name = aggregateprofiles[pf_idx].asString();
|
|
|
|
|
|
|
|
if (profile_name == aggregateprofile_name) {
|
|
|
|
LOG(WARNING) << "AggregateProfiles: recursive profile name: " << profile_name;
|
|
|
|
ret = false;
|
|
|
|
break;
|
|
|
|
} else if (profiles_.find(profile_name) == profiles_.end()) {
|
|
|
|
LOG(WARNING) << "AggregateProfiles: undefined profile name: " << profile_name;
|
|
|
|
ret = false;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
profiles.push_back(profiles_[profile_name]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ret) {
|
2022-01-24 20:59:13 +01:00
|
|
|
auto profile = std::make_shared<TaskProfile>(aggregateprofile_name);
|
2019-09-16 13:07:17 +02:00
|
|
|
profile->Add(std::make_unique<ApplyProfileAction>(profiles));
|
|
|
|
profiles_[aggregateprofile_name] = profile;
|
|
|
|
}
|
2018-12-21 20:41:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-02-14 23:40:41 +01:00
|
|
|
TaskProfile* TaskProfiles::GetProfile(const std::string& name) const {
|
2018-12-21 20:41:50 +01:00
|
|
|
auto iter = profiles_.find(name);
|
|
|
|
|
|
|
|
if (iter != profiles_.end()) {
|
|
|
|
return iter->second.get();
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2022-02-03 20:50:16 +01:00
|
|
|
const IProfileAttribute* TaskProfiles::GetAttribute(const std::string& name) const {
|
2018-12-21 20:41:50 +01:00
|
|
|
auto iter = attributes_.find(name);
|
|
|
|
|
|
|
|
if (iter != attributes_.end()) {
|
|
|
|
return iter->second.get();
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
2019-09-16 13:07:17 +02:00
|
|
|
|
|
|
|
bool TaskProfiles::SetProcessProfiles(uid_t uid, pid_t pid,
|
2022-01-21 00:41:28 +01:00
|
|
|
const std::vector<std::string>& profiles, bool use_fd_cache) {
|
2022-04-13 20:50:12 +02:00
|
|
|
bool success = true;
|
2019-09-16 13:07:17 +02:00
|
|
|
for (const auto& name : profiles) {
|
|
|
|
TaskProfile* profile = GetProfile(name);
|
|
|
|
if (profile != nullptr) {
|
2022-01-21 00:41:28 +01:00
|
|
|
if (use_fd_cache) {
|
|
|
|
profile->EnableResourceCaching(ProfileAction::RCT_PROCESS);
|
|
|
|
}
|
2019-09-16 13:07:17 +02:00
|
|
|
if (!profile->ExecuteForProcess(uid, pid)) {
|
|
|
|
PLOG(WARNING) << "Failed to apply " << name << " process profile";
|
2022-04-13 20:50:12 +02:00
|
|
|
success = false;
|
2019-09-16 13:07:17 +02:00
|
|
|
}
|
|
|
|
} else {
|
2022-04-13 20:50:12 +02:00
|
|
|
PLOG(WARNING) << "Failed to find " << name << " process profile";
|
|
|
|
success = false;
|
2019-09-16 13:07:17 +02:00
|
|
|
}
|
|
|
|
}
|
2022-04-13 20:50:12 +02:00
|
|
|
return success;
|
2019-09-16 13:07:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool TaskProfiles::SetTaskProfiles(int tid, const std::vector<std::string>& profiles,
|
|
|
|
bool use_fd_cache) {
|
2022-04-13 20:50:12 +02:00
|
|
|
bool success = true;
|
2019-09-16 13:07:17 +02:00
|
|
|
for (const auto& name : profiles) {
|
|
|
|
TaskProfile* profile = GetProfile(name);
|
|
|
|
if (profile != nullptr) {
|
|
|
|
if (use_fd_cache) {
|
2022-01-21 00:41:28 +01:00
|
|
|
profile->EnableResourceCaching(ProfileAction::RCT_TASK);
|
2019-09-16 13:07:17 +02:00
|
|
|
}
|
|
|
|
if (!profile->ExecuteForTask(tid)) {
|
|
|
|
PLOG(WARNING) << "Failed to apply " << name << " task profile";
|
2022-04-13 20:50:12 +02:00
|
|
|
success = false;
|
2019-09-16 13:07:17 +02:00
|
|
|
}
|
|
|
|
} else {
|
2022-04-13 20:50:12 +02:00
|
|
|
PLOG(WARNING) << "Failed to find " << name << " task profile";
|
|
|
|
success = false;
|
2019-09-16 13:07:17 +02:00
|
|
|
}
|
|
|
|
}
|
2022-04-13 20:50:12 +02:00
|
|
|
return success;
|
2019-09-16 13:07:17 +02:00
|
|
|
}
|