From d1e048f95618b6fb7f66c81e1c650482d95cd452 Mon Sep 17 00:00:00 2001 From: "T.J. Mercier" Date: Thu, 28 Mar 2024 00:33:44 +0000 Subject: [PATCH] Use ConvertUid{Pid}ToPath for all path generation Consolidate into a single implementation. Test: m Change-Id: I0fc52db2d4b2973a74bad24c0a5f77384a559cee --- libprocessgroup/processgroup.cpp | 8 -------- libprocessgroup/task_profiles.cpp | 18 ++++++++++++++---- libprocessgroup/task_profiles.h | 3 +++ 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/libprocessgroup/processgroup.cpp b/libprocessgroup/processgroup.cpp index 94d950209..8df2805d9 100644 --- a/libprocessgroup/processgroup.cpp +++ b/libprocessgroup/processgroup.cpp @@ -78,14 +78,6 @@ bool CgroupGetControllerPath(const std::string& cgroup_name, std::string* path) return true; } -static std::string ConvertUidToPath(const char* cgroup, uid_t uid) { - return StringPrintf("%s/uid_%u", cgroup, uid); -} - -static std::string ConvertUidPidToPath(const char* cgroup, uid_t uid, pid_t pid) { - return StringPrintf("%s/uid_%u/pid_%d", cgroup, uid, pid); -} - static bool CgroupKillAvailable() { static std::once_flag f; static bool cgroup_kill_available = false; diff --git a/libprocessgroup/task_profiles.cpp b/libprocessgroup/task_profiles.cpp index 2353cf18a..c376a0fe6 100644 --- a/libprocessgroup/task_profiles.cpp +++ b/libprocessgroup/task_profiles.cpp @@ -126,11 +126,19 @@ void ProfileAttribute::Reset(const CgroupController& controller, const std::stri file_v2_name_ = file_v2_name; } +std::string ConvertUidToPath(const char* root_cgroup_path, uid_t uid) { + return StringPrintf("%s/uid_%u", root_cgroup_path, uid); +} + +std::string ConvertUidPidToPath(const char* root_cgroup_path, uid_t uid, pid_t pid) { + const std::string uid_path = ConvertUidToPath(root_cgroup_path, uid); + return StringPrintf("%s/pid_%d", uid_path.c_str(), pid); +} + bool ProfileAttribute::GetPathForProcess(uid_t uid, pid_t pid, std::string* path) const { if (controller()->version() == 2) { - // all cgroup v2 attributes use the same process group hierarchy - *path = StringPrintf("%s/uid_%u/pid_%d/%s", controller()->path(), uid, pid, - file_name().c_str()); + const std::string cgroup_path = ConvertUidPidToPath(controller()->path(), uid, pid); + *path = cgroup_path + "/" + file_name(); return true; } return GetPathForTask(pid, path); @@ -155,12 +163,14 @@ bool ProfileAttribute::GetPathForTask(pid_t tid, std::string* path) const { return true; } +// NOTE: This function is for cgroup v2 only bool ProfileAttribute::GetPathForUID(uid_t uid, std::string* path) const { if (path == nullptr) { return true; } - *path = StringPrintf("%s/uid_%u/%s", controller()->path(), uid, file_name().c_str()); + const std::string cgroup_path = ConvertUidToPath(controller()->path(), uid); + *path = cgroup_path + "/" + file_name(); return true; } diff --git a/libprocessgroup/task_profiles.h b/libprocessgroup/task_profiles.h index 428447106..7e3c50d9f 100644 --- a/libprocessgroup/task_profiles.h +++ b/libprocessgroup/task_profiles.h @@ -252,3 +252,6 @@ class TaskProfiles { std::map, std::less<>> profiles_; std::map, std::less<>> attributes_; }; + +std::string ConvertUidToPath(const char* root_cgroup_path, uid_t uid); +std::string ConvertUidPidToPath(const char* root_cgroup_path, uid_t uid, pid_t pid);