Merge changes I9f850003,Idb270068,I39df9814 into main

* changes:
  libprocessgroup: Remove unused CgroupMap::SetupCgroups declaration
  libprocessgroup: Rename files a_cgroup_controller.cpp, a_cgroup_file.cpp
  libprocessgroup: Rename CgroupController -> CgroupControllerWrapper
This commit is contained in:
T.J. Mercier 2024-08-09 23:26:11 +00:00 committed by Gerrit Code Review
commit f9f144363b
9 changed files with 38 additions and 41 deletions

View file

@ -38,26 +38,26 @@ static constexpr const char* CGROUP_PROCS_FILE = "/cgroup.procs";
static constexpr const char* CGROUP_TASKS_FILE = "/tasks";
static constexpr const char* CGROUP_TASKS_FILE_V2 = "/cgroup.threads";
uint32_t CgroupController::version() const {
uint32_t CgroupControllerWrapper::version() const {
CHECK(HasValue());
return ACgroupController_getVersion(controller_);
}
const char* CgroupController::name() const {
const char* CgroupControllerWrapper::name() const {
CHECK(HasValue());
return ACgroupController_getName(controller_);
}
const char* CgroupController::path() const {
const char* CgroupControllerWrapper::path() const {
CHECK(HasValue());
return ACgroupController_getPath(controller_);
}
bool CgroupController::HasValue() const {
bool CgroupControllerWrapper::HasValue() const {
return controller_ != nullptr;
}
bool CgroupController::IsUsable() {
bool CgroupControllerWrapper::IsUsable() {
if (!HasValue()) return false;
if (state_ == UNKNOWN) {
@ -72,7 +72,7 @@ bool CgroupController::IsUsable() {
return state_ == USABLE;
}
std::string CgroupController::GetTasksFilePath(const std::string& rel_path) const {
std::string CgroupControllerWrapper::GetTasksFilePath(const std::string& rel_path) const {
std::string tasks_path = path();
if (!rel_path.empty()) {
@ -81,8 +81,8 @@ std::string CgroupController::GetTasksFilePath(const std::string& rel_path) cons
return (version() == 1) ? tasks_path + CGROUP_TASKS_FILE : tasks_path + CGROUP_TASKS_FILE_V2;
}
std::string CgroupController::GetProcsFilePath(const std::string& rel_path, uid_t uid,
pid_t pid) const {
std::string CgroupControllerWrapper::GetProcsFilePath(const std::string& rel_path, uid_t uid,
pid_t pid) const {
std::string proc_path(path());
proc_path.append("/").append(rel_path);
proc_path = regex_replace(proc_path, std::regex("<uid>"), std::to_string(uid));
@ -91,7 +91,7 @@ std::string CgroupController::GetProcsFilePath(const std::string& rel_path, uid_
return proc_path.append(CGROUP_PROCS_FILE);
}
bool CgroupController::GetTaskGroup(pid_t tid, std::string* group) const {
bool CgroupControllerWrapper::GetTaskGroup(pid_t tid, std::string* group) const {
std::string file_name = StringPrintf("/proc/%d/cgroup", tid);
std::string content;
if (!android::base::ReadFileToString(file_name, &content)) {
@ -175,40 +175,40 @@ void CgroupMap::Print() const {
}
}
CgroupController CgroupMap::FindController(const std::string& name) const {
CgroupControllerWrapper CgroupMap::FindController(const std::string& name) const {
if (!loaded_) {
LOG(ERROR) << "CgroupMap::FindController called for [" << getpid()
<< "] failed, RC file was not initialized properly";
return CgroupController(nullptr);
return CgroupControllerWrapper(nullptr);
}
auto controller_count = ACgroupFile_getControllerCount();
for (uint32_t i = 0; i < controller_count; ++i) {
const ACgroupController* controller = ACgroupFile_getController(i);
if (name == ACgroupController_getName(controller)) {
return CgroupController(controller);
return CgroupControllerWrapper(controller);
}
}
return CgroupController(nullptr);
return CgroupControllerWrapper(nullptr);
}
CgroupController CgroupMap::FindControllerByPath(const std::string& path) const {
CgroupControllerWrapper CgroupMap::FindControllerByPath(const std::string& path) const {
if (!loaded_) {
LOG(ERROR) << "CgroupMap::FindControllerByPath called for [" << getpid()
<< "] failed, RC file was not initialized properly";
return CgroupController(nullptr);
return CgroupControllerWrapper(nullptr);
}
auto controller_count = ACgroupFile_getControllerCount();
for (uint32_t i = 0; i < controller_count; ++i) {
const ACgroupController* controller = ACgroupFile_getController(i);
if (StartsWith(path, ACgroupController_getPath(controller))) {
return CgroupController(controller);
return CgroupControllerWrapper(controller);
}
}
return CgroupController(nullptr);
return CgroupControllerWrapper(nullptr);
}
int CgroupMap::ActivateControllers(const std::string& path) const {

View file

@ -23,10 +23,10 @@
#include <android/cgrouprc.h>
// Convenient wrapper of an ACgroupController pointer.
class CgroupController {
class CgroupControllerWrapper {
public:
// Does not own controller
explicit CgroupController(const ACgroupController* controller)
explicit CgroupControllerWrapper(const ACgroupController* controller)
: controller_(controller) {}
uint32_t version() const;
@ -53,12 +53,9 @@ class CgroupController {
class CgroupMap {
public:
// Selinux policy ensures only init process can successfully use this function
static bool SetupCgroups();
static CgroupMap& GetInstance();
CgroupController FindController(const std::string& name) const;
CgroupController FindControllerByPath(const std::string& path) const;
CgroupControllerWrapper FindController(const std::string& name) const;
CgroupControllerWrapper FindControllerByPath(const std::string& path) const;
int ActivateControllers(const std::string& path) const;
private:

View file

@ -32,8 +32,8 @@ cc_library {
symbol_file: "libcgrouprc.map.txt",
},
srcs: [
"cgroup_controller.cpp",
"cgroup_file.cpp",
"a_cgroup_controller.cpp",
"a_cgroup_file.cpp",
],
cflags: [
"-Wall",

View file

@ -94,7 +94,7 @@ static bool CgroupKillAvailable() {
}
static bool CgroupGetMemcgAppsPath(std::string* path) {
CgroupController controller = CgroupMap::GetInstance().FindController("memory");
CgroupControllerWrapper controller = CgroupMap::GetInstance().FindController("memory");
if (!controller.HasValue()) {
return false;

View file

@ -123,8 +123,8 @@ const std::string& ProfileAttribute::file_name() const {
return file_name_;
}
void ProfileAttribute::Reset(const CgroupController& controller, const std::string& file_name,
const std::string& file_v2_name) {
void ProfileAttribute::Reset(const CgroupControllerWrapper& controller,
const std::string& file_name, const std::string& file_v2_name) {
controller_ = controller;
file_name_ = file_name;
file_v2_name_ = file_v2_name;
@ -333,7 +333,7 @@ bool SetAttributeAction::IsValidForTask(pid_t tid) const {
return optional_;
}
SetCgroupAction::SetCgroupAction(const CgroupController& c, const std::string& p)
SetCgroupAction::SetCgroupAction(const CgroupControllerWrapper& c, const std::string& p)
: controller_(c), path_(p) {
FdCacheHelper::Init(controller_.GetTasksFilePath(path_), fd_[ProfileAction::RCT_TASK]);
// uid and pid don't matter because IsAppDependentPath ensures the path doesn't use them

View file

@ -32,9 +32,9 @@
class IProfileAttribute {
public:
virtual ~IProfileAttribute() = 0;
virtual void Reset(const CgroupController& controller, const std::string& file_name,
virtual void Reset(const CgroupControllerWrapper& controller, const std::string& file_name,
const std::string& file_v2_name) = 0;
virtual const CgroupController* controller() const = 0;
virtual const CgroupControllerWrapper* controller() const = 0;
virtual const std::string& file_name() const = 0;
virtual bool GetPathForProcess(uid_t uid, pid_t pid, std::string* path) const = 0;
virtual bool GetPathForTask(pid_t tid, std::string* path) const = 0;
@ -46,14 +46,14 @@ class ProfileAttribute : public IProfileAttribute {
// Cgroup attributes may have different names in the v1 and v2 hierarchies. If `file_v2_name` is
// not empty, `file_name` is the name for the v1 hierarchy and `file_v2_name` is the name for
// the v2 hierarchy. If `file_v2_name` is empty, `file_name` is used for both hierarchies.
ProfileAttribute(const CgroupController& controller, const std::string& file_name,
ProfileAttribute(const CgroupControllerWrapper& controller, const std::string& file_name,
const std::string& file_v2_name)
: controller_(controller), file_name_(file_name), file_v2_name_(file_v2_name) {}
~ProfileAttribute() = default;
const CgroupController* controller() const override { return &controller_; }
const CgroupControllerWrapper* controller() const override { return &controller_; }
const std::string& file_name() const override;
void Reset(const CgroupController& controller, const std::string& file_name,
void Reset(const CgroupControllerWrapper& controller, const std::string& file_name,
const std::string& file_v2_name) override;
bool GetPathForProcess(uid_t uid, pid_t pid, std::string* path) const override;
@ -61,7 +61,7 @@ class ProfileAttribute : public IProfileAttribute {
bool GetPathForUID(uid_t uid, std::string* path) const override;
private:
CgroupController controller_;
CgroupControllerWrapper controller_;
std::string file_name_;
std::string file_v2_name_;
};
@ -142,7 +142,7 @@ class SetAttributeAction : public ProfileAction {
// Set cgroup profile element
class SetCgroupAction : public ProfileAction {
public:
SetCgroupAction(const CgroupController& c, const std::string& p);
SetCgroupAction(const CgroupControllerWrapper& c, const std::string& p);
const char* Name() const override { return "SetCgroup"; }
bool ExecuteForProcess(uid_t uid, pid_t pid) const override;
@ -152,10 +152,10 @@ class SetCgroupAction : public ProfileAction {
bool IsValidForProcess(uid_t uid, pid_t pid) const override;
bool IsValidForTask(pid_t tid) const override;
const CgroupController* controller() const { return &controller_; }
const CgroupControllerWrapper* controller() const { return &controller_; }
private:
CgroupController controller_;
CgroupControllerWrapper controller_;
std::string path_;
android::base::unique_fd fd_[ProfileAction::RCT_COUNT];
mutable std::mutex fd_mutex_;

View file

@ -102,10 +102,10 @@ class ProfileAttributeMock : public IProfileAttribute {
public:
ProfileAttributeMock(const std::string& file_name) : file_name_(file_name) {}
~ProfileAttributeMock() override = default;
void Reset(const CgroupController&, const std::string&, const std::string&) override {
void Reset(const CgroupControllerWrapper&, const std::string&, const std::string&) override {
CHECK(false);
}
const CgroupController* controller() const override {
const CgroupControllerWrapper* controller() const override {
CHECK(false);
return {};
}