summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorT.J. Mercier <tjmercier@google.com>2024-03-28 00:33:44 +0000
committerT.J. Mercier <tjmercier@google.com>2024-04-08 21:15:48 +0000
commitd1e048f95618b6fb7f66c81e1c650482d95cd452 (patch)
treecb60b87f632f9993b4e7a52b8d2204c6d243426c
parent44eb705480fe7b134d6178746d5430ced247bc74 (diff)
downloadcore-d1e048f95618b6fb7f66c81e1c650482d95cd452.tar.gz
Use ConvertUid{Pid}ToPath for all path generation
Consolidate into a single implementation. Test: m Change-Id: I0fc52db2d4b2973a74bad24c0a5f77384a559cee
-rw-r--r--libprocessgroup/processgroup.cpp8
-rw-r--r--libprocessgroup/task_profiles.cpp18
-rw-r--r--libprocessgroup/task_profiles.h3
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::string, std::shared_ptr<TaskProfile>, std::less<>> profiles_;
std::map<std::string, std::unique_ptr<IProfileAttribute>, 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);