From 9e3ace52bba69b3fb9b6700e02c9380bd06b0351 Mon Sep 17 00:00:00 2001 From: Suren Baghdasaryan Date: Wed, 16 Jun 2021 17:01:19 -0700 Subject: [PATCH] libprocessgroup: Add CgroupGetControllerFromPath API function Add new CgroupGetControllerFromPath function to retrieve the name of the cgroup using a file path. If the file does not belong to any cgroup, the function returns false. Bug: 191283136 Test: build and boot Signed-off-by: Suren Baghdasaryan Change-Id: Ic17a474cb25a80a3339b33ed8bc27b07af053abb --- libprocessgroup/cgroup_map.cpp | 20 +++++++++++++++++++ libprocessgroup/cgroup_map.h | 1 + .../include/processgroup/processgroup.h | 1 + libprocessgroup/processgroup.cpp | 14 +++++++++++++ 4 files changed, 36 insertions(+) diff --git a/libprocessgroup/cgroup_map.cpp b/libprocessgroup/cgroup_map.cpp index 0734f2565..352847a1c 100644 --- a/libprocessgroup/cgroup_map.cpp +++ b/libprocessgroup/cgroup_map.cpp @@ -34,6 +34,7 @@ #include #include #include +#include #include #include #include @@ -41,6 +42,7 @@ #include using android::base::GetBoolProperty; +using android::base::StartsWith; using android::base::StringPrintf; using android::base::unique_fd; using android::base::WriteStringToFile; @@ -204,6 +206,24 @@ CgroupController CgroupMap::FindController(const std::string& name) const { return CgroupController(nullptr); } +CgroupController 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); + } + + 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 CgroupController(nullptr); +} + int CgroupMap::ActivateControllers(const std::string& path) const { if (__builtin_available(android 30, *)) { auto controller_count = ACgroupFile_getControllerCount(); diff --git a/libprocessgroup/cgroup_map.h b/libprocessgroup/cgroup_map.h index 22d717b32..5cdf8b28c 100644 --- a/libprocessgroup/cgroup_map.h +++ b/libprocessgroup/cgroup_map.h @@ -62,6 +62,7 @@ class CgroupMap { static CgroupMap& GetInstance(); CgroupController FindController(const std::string& name) const; + CgroupController FindControllerByPath(const std::string& path) const; int ActivateControllers(const std::string& path) const; private: diff --git a/libprocessgroup/include/processgroup/processgroup.h b/libprocessgroup/include/processgroup/processgroup.h index fa2642d86..be34f9513 100644 --- a/libprocessgroup/include/processgroup/processgroup.h +++ b/libprocessgroup/include/processgroup/processgroup.h @@ -26,6 +26,7 @@ __BEGIN_DECLS static constexpr const char* CGROUPV2_CONTROLLER_NAME = "cgroup2"; bool CgroupGetControllerPath(const std::string& cgroup_name, std::string* path); +bool CgroupGetControllerFromPath(const std::string& path, std::string* cgroup_name); bool CgroupGetAttributePath(const std::string& attr_name, std::string* path); bool CgroupGetAttributePathForTask(const std::string& attr_name, int tid, std::string* path); diff --git a/libprocessgroup/processgroup.cpp b/libprocessgroup/processgroup.cpp index faf945cfc..0320b0263 100644 --- a/libprocessgroup/processgroup.cpp +++ b/libprocessgroup/processgroup.cpp @@ -69,6 +69,20 @@ bool CgroupGetControllerPath(const std::string& cgroup_name, std::string* path) return true; } +bool CgroupGetControllerFromPath(const std::string& path, std::string* cgroup_name) { + auto controller = CgroupMap::GetInstance().FindControllerByPath(path); + + if (!controller.HasValue()) { + return false; + } + + if (cgroup_name) { + *cgroup_name = controller.name(); + } + + return true; +} + bool CgroupGetAttributePath(const std::string& attr_name, std::string* path) { const TaskProfiles& tp = TaskProfiles::GetInstance(); const ProfileAttribute* attr = tp.GetAttribute(attr_name);