Use ConvertUid{Pid}ToPath for all path generation

Consolidate into a single implementation.

Test: m
Change-Id: I0fc52db2d4b2973a74bad24c0a5f77384a559cee
This commit is contained in:
T.J. Mercier 2024-03-28 00:33:44 +00:00
parent 44eb705480
commit d1e048f956
3 changed files with 17 additions and 12 deletions

View file

@ -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;

View file

@ -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;
}

View file

@ -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);