diff --git a/libprocessgroup/cgroup_map.cpp b/libprocessgroup/cgroup_map.cpp index ce7f10b00..c8ae2161a 100644 --- a/libprocessgroup/cgroup_map.cpp +++ b/libprocessgroup/cgroup_map.cpp @@ -104,7 +104,7 @@ std::string CgroupController::GetProcsFilePath(const std::string& rel_path, uid_ return proc_path.append(CGROUP_PROCS_FILE); } -bool CgroupController::GetTaskGroup(int tid, std::string* group) const { +bool CgroupController::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)) { diff --git a/libprocessgroup/cgroup_map.h b/libprocessgroup/cgroup_map.h index 5cdf8b28c..5c6d3e21c 100644 --- a/libprocessgroup/cgroup_map.h +++ b/libprocessgroup/cgroup_map.h @@ -43,7 +43,8 @@ class CgroupController { std::string GetTasksFilePath(const std::string& path) const; std::string GetProcsFilePath(const std::string& path, uid_t uid, pid_t pid) const; - bool GetTaskGroup(int tid, std::string* group) const; + bool GetTaskGroup(pid_t tid, std::string* group) const; + private: enum ControllerState { UNKNOWN = 0, diff --git a/libprocessgroup/include/processgroup/processgroup.h b/libprocessgroup/include/processgroup/processgroup.h index c67fd9ecb..0f4c97f3b 100644 --- a/libprocessgroup/include/processgroup/processgroup.h +++ b/libprocessgroup/include/processgroup/processgroup.h @@ -32,19 +32,20 @@ bool CgroupsAvailable(); 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); +bool CgroupGetAttributePathForTask(const std::string& attr_name, pid_t tid, std::string* path); -bool SetTaskProfiles(int tid, const std::vector& profiles, bool use_fd_cache = false); +bool SetTaskProfiles(pid_t tid, const std::vector& profiles, + bool use_fd_cache = false); bool SetProcessProfiles(uid_t uid, pid_t pid, const std::vector& profiles); bool SetUserProfiles(uid_t uid, const std::vector& profiles); __END_DECLS -bool SetTaskProfiles(int tid, std::initializer_list profiles, +bool SetTaskProfiles(pid_t tid, std::initializer_list profiles, bool use_fd_cache = false); bool SetProcessProfiles(uid_t uid, pid_t pid, std::initializer_list profiles); #if _LIBCPP_STD_VER > 17 -bool SetTaskProfiles(int tid, std::span profiles, +bool SetTaskProfiles(pid_t tid, std::span profiles, bool use_fd_cache = false); bool SetProcessProfiles(uid_t uid, pid_t pid, std::span profiles); #endif @@ -94,7 +95,7 @@ void removeAllEmptyProcessGroups(void); // Provides the path for an attribute in a specific process group // Returns false in case of error, true in case of success -bool getAttributePathForTask(const std::string& attr_name, int tid, std::string* path); +bool getAttributePathForTask(const std::string& attr_name, pid_t tid, std::string* path); // Check if a profile can be applied without failing. // Returns true if it can be applied without failing, false otherwise diff --git a/libprocessgroup/processgroup.cpp b/libprocessgroup/processgroup.cpp index c6646eb40..f8ed43054 100644 --- a/libprocessgroup/processgroup.cpp +++ b/libprocessgroup/processgroup.cpp @@ -120,7 +120,7 @@ bool CgroupGetAttributePath(const std::string& attr_name, std::string* path) { return true; } -bool CgroupGetAttributePathForTask(const std::string& attr_name, int tid, std::string* path) { +bool CgroupGetAttributePathForTask(const std::string& attr_name, pid_t tid, std::string* path) { const TaskProfiles& tp = TaskProfiles::GetInstance(); const IProfileAttribute* attr = tp.GetAttribute(attr_name); @@ -171,17 +171,18 @@ bool SetProcessProfilesCached(uid_t uid, pid_t pid, const std::vector(profiles), true); } -bool SetTaskProfiles(int tid, const std::vector& profiles, bool use_fd_cache) { +bool SetTaskProfiles(pid_t tid, const std::vector& profiles, bool use_fd_cache) { return TaskProfiles::GetInstance().SetTaskProfiles(tid, std::span(profiles), use_fd_cache); } -bool SetTaskProfiles(int tid, std::initializer_list profiles, bool use_fd_cache) { +bool SetTaskProfiles(pid_t tid, std::initializer_list profiles, + bool use_fd_cache) { return TaskProfiles::GetInstance().SetTaskProfiles( tid, std::span(profiles), use_fd_cache); } -bool SetTaskProfiles(int tid, std::span profiles, bool use_fd_cache) { +bool SetTaskProfiles(pid_t tid, std::span profiles, bool use_fd_cache) { return TaskProfiles::GetInstance().SetTaskProfiles(tid, profiles, use_fd_cache); } @@ -623,7 +624,7 @@ int createProcessGroup(uid_t uid, int initialPid, bool memControl) { return createProcessGroupInternal(uid, initialPid, cgroup, true); } -static bool SetProcessGroupValue(int tid, const std::string& attr_name, int64_t value) { +static bool SetProcessGroupValue(pid_t tid, const std::string& attr_name, int64_t value) { if (!isMemoryCgroupSupported()) { PLOG(ERROR) << "Memcg is not mounted."; return false; @@ -654,7 +655,7 @@ bool setProcessGroupLimit(uid_t, int pid, int64_t limit_in_bytes) { return SetProcessGroupValue(pid, "MemLimit", limit_in_bytes); } -bool getAttributePathForTask(const std::string& attr_name, int tid, std::string* path) { +bool getAttributePathForTask(const std::string& attr_name, pid_t tid, std::string* path) { return CgroupGetAttributePathForTask(attr_name, tid, path); } diff --git a/libprocessgroup/sched_policy.cpp b/libprocessgroup/sched_policy.cpp index f743ed81b..d7e66c4ad 100644 --- a/libprocessgroup/sched_policy.cpp +++ b/libprocessgroup/sched_policy.cpp @@ -38,7 +38,7 @@ static inline SchedPolicy _policy(SchedPolicy p) { #if defined(__ANDROID__) -int set_cpuset_policy(int tid, SchedPolicy policy) { +int set_cpuset_policy(pid_t tid, SchedPolicy policy) { if (tid == 0) { tid = GetThreadId(); } @@ -65,7 +65,7 @@ int set_cpuset_policy(int tid, SchedPolicy policy) { return 0; } -int set_sched_policy(int tid, SchedPolicy policy) { +int set_sched_policy(pid_t tid, SchedPolicy policy) { if (tid == 0) { tid = GetThreadId(); } @@ -155,7 +155,7 @@ bool schedboost_enabled() { return enabled; } -static int getCGroupSubsys(int tid, const char* subsys, std::string& subgroup) { +static int getCGroupSubsys(pid_t tid, const char* subsys, std::string& subgroup) { auto controller = CgroupMap::GetInstance().FindController(subsys); if (!controller.IsUsable()) return -1; @@ -188,7 +188,7 @@ static int get_sched_policy_from_group(const std::string& group, SchedPolicy* po return 0; } -int get_sched_policy(int tid, SchedPolicy* policy) { +int get_sched_policy(pid_t tid, SchedPolicy* policy) { if (tid == 0) { tid = GetThreadId(); } diff --git a/libprocessgroup/task_profiles.cpp b/libprocessgroup/task_profiles.cpp index 4aa88a475..df5206a38 100644 --- a/libprocessgroup/task_profiles.cpp +++ b/libprocessgroup/task_profiles.cpp @@ -136,7 +136,7 @@ bool ProfileAttribute::GetPathForProcess(uid_t uid, pid_t pid, std::string* path return GetPathForTask(pid, path); } -bool ProfileAttribute::GetPathForTask(int tid, std::string* path) const { +bool ProfileAttribute::GetPathForTask(pid_t tid, std::string* path) const { std::string subgroup; if (!controller()->GetTaskGroup(tid, &subgroup)) { return false; @@ -179,13 +179,13 @@ bool SetClampsAction::ExecuteForTask(int) const { // To avoid issues in sdk_mac build #if defined(__ANDROID__) -bool SetTimerSlackAction::IsTimerSlackSupported(int tid) { +bool SetTimerSlackAction::IsTimerSlackSupported(pid_t tid) { auto file = StringPrintf("/proc/%d/timerslack_ns", tid); return (access(file.c_str(), W_OK) == 0); } -bool SetTimerSlackAction::ExecuteForTask(int tid) const { +bool SetTimerSlackAction::ExecuteForTask(pid_t tid) const { static bool sys_supports_timerslack = IsTimerSlackSupported(tid); // v4.6+ kernels support the /proc//timerslack_ns interface. @@ -250,7 +250,7 @@ bool SetAttributeAction::ExecuteForProcess(uid_t uid, pid_t pid) const { return WriteValueToFile(path); } -bool SetAttributeAction::ExecuteForTask(int tid) const { +bool SetAttributeAction::ExecuteForTask(pid_t tid) const { std::string path; if (!attribute_->GetPathForTask(tid, &path)) { @@ -288,7 +288,7 @@ bool SetAttributeAction::IsValidForProcess(uid_t, pid_t pid) const { return IsValidForTask(pid); } -bool SetAttributeAction::IsValidForTask(int tid) const { +bool SetAttributeAction::IsValidForTask(pid_t tid) const { std::string path; if (!attribute_->GetPathForTask(tid, &path)) { @@ -316,7 +316,7 @@ SetCgroupAction::SetCgroupAction(const CgroupController& c, const std::string& p FdCacheHelper::Init(controller_.GetProcsFilePath(path_, 0, 0), fd_[ProfileAction::RCT_PROCESS]); } -bool SetCgroupAction::AddTidToCgroup(int tid, int fd, const char* controller_name) { +bool SetCgroupAction::AddTidToCgroup(pid_t tid, int fd, const char* controller_name) { if (tid <= 0) { return true; } @@ -399,7 +399,7 @@ bool SetCgroupAction::ExecuteForProcess(uid_t uid, pid_t pid) const { return true; } -bool SetCgroupAction::ExecuteForTask(int tid) const { +bool SetCgroupAction::ExecuteForTask(pid_t tid) const { CacheUseResult result = UseCachedFd(ProfileAction::RCT_TASK, tid); if (result != ProfileAction::UNUSED) { return result == ProfileAction::SUCCESS; @@ -588,7 +588,7 @@ bool WriteFileAction::ExecuteForProcess(uid_t uid, pid_t pid) const { return true; } -bool WriteFileAction::ExecuteForTask(int tid) const { +bool WriteFileAction::ExecuteForTask(pid_t tid) const { return WriteValueToFile(value_, ProfileAction::RCT_TASK, getuid(), tid, logfailures_); } @@ -653,7 +653,7 @@ bool ApplyProfileAction::ExecuteForProcess(uid_t uid, pid_t pid) const { return true; } -bool ApplyProfileAction::ExecuteForTask(int tid) const { +bool ApplyProfileAction::ExecuteForTask(pid_t tid) const { for (const auto& profile : profiles_) { profile->ExecuteForTask(tid); } @@ -681,7 +681,7 @@ bool ApplyProfileAction::IsValidForProcess(uid_t uid, pid_t pid) const { return true; } -bool ApplyProfileAction::IsValidForTask(int tid) const { +bool ApplyProfileAction::IsValidForTask(pid_t tid) const { for (const auto& profile : profiles_) { if (!profile->IsValidForTask(tid)) { return false; @@ -705,7 +705,7 @@ bool TaskProfile::ExecuteForProcess(uid_t uid, pid_t pid) const { return true; } -bool TaskProfile::ExecuteForTask(int tid) const { +bool TaskProfile::ExecuteForTask(pid_t tid) const { if (tid == 0) { tid = GetThreadId(); } @@ -759,7 +759,7 @@ bool TaskProfile::IsValidForProcess(uid_t uid, pid_t pid) const { return true; } -bool TaskProfile::IsValidForTask(int tid) const { +bool TaskProfile::IsValidForTask(pid_t tid) const { for (const auto& element : elements_) { if (!element->IsValidForTask(tid)) return false; } @@ -1035,7 +1035,7 @@ bool TaskProfiles::SetProcessProfiles(uid_t uid, pid_t pid, std::span p } template -bool TaskProfiles::SetTaskProfiles(int tid, std::span profiles, bool use_fd_cache) { +bool TaskProfiles::SetTaskProfiles(pid_t tid, std::span profiles, bool use_fd_cache) { bool success = true; for (const auto& name : profiles) { TaskProfile* profile = GetProfile(name); @@ -1061,9 +1061,9 @@ template bool TaskProfiles::SetProcessProfiles(uid_t uid, pid_t pid, template bool TaskProfiles::SetProcessProfiles(uid_t uid, pid_t pid, std::span profiles, bool use_fd_cache); -template bool TaskProfiles::SetTaskProfiles(int tid, std::span profiles, +template bool TaskProfiles::SetTaskProfiles(pid_t tid, std::span profiles, bool use_fd_cache); -template bool TaskProfiles::SetTaskProfiles(int tid, std::span profiles, +template bool TaskProfiles::SetTaskProfiles(pid_t tid, std::span profiles, bool use_fd_cache); template bool TaskProfiles::SetUserProfiles(uid_t uid, std::span profiles, bool use_fd_cache); diff --git a/libprocessgroup/task_profiles.h b/libprocessgroup/task_profiles.h index b16224b1c..1e1acc5b7 100644 --- a/libprocessgroup/task_profiles.h +++ b/libprocessgroup/task_profiles.h @@ -37,7 +37,7 @@ class IProfileAttribute { virtual const CgroupController* 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(int tid, std::string* path) const = 0; + virtual bool GetPathForTask(pid_t tid, std::string* path) const = 0; virtual bool GetPathForUID(uid_t uid, std::string* path) const = 0; }; @@ -57,7 +57,7 @@ class ProfileAttribute : public IProfileAttribute { const std::string& file_v2_name) override; bool GetPathForProcess(uid_t uid, pid_t pid, std::string* path) const override; - bool GetPathForTask(int tid, std::string* path) const override; + bool GetPathForTask(pid_t tid, std::string* path) const override; bool GetPathForUID(uid_t uid, std::string* path) const override; private: @@ -83,7 +83,7 @@ class ProfileAction { virtual void EnableResourceCaching(ResourceCacheType) {} virtual void DropResourceCaching(ResourceCacheType) {} virtual bool IsValidForProcess(uid_t uid, pid_t pid) const { return false; } - virtual bool IsValidForTask(int tid) const { return false; } + virtual bool IsValidForTask(pid_t tid) const { return false; } protected: enum CacheUseResult { SUCCESS, FAIL, UNUSED }; @@ -96,7 +96,7 @@ class SetClampsAction : public ProfileAction { const char* Name() const override { return "SetClamps"; } bool ExecuteForProcess(uid_t uid, pid_t pid) const override; - bool ExecuteForTask(int tid) const override; + bool ExecuteForTask(pid_t tid) const override; protected: int boost_; @@ -108,14 +108,14 @@ class SetTimerSlackAction : public ProfileAction { SetTimerSlackAction(unsigned long slack) noexcept : slack_(slack) {} const char* Name() const override { return "SetTimerSlack"; } - bool ExecuteForTask(int tid) const override; + bool ExecuteForTask(pid_t tid) const override; bool IsValidForProcess(uid_t uid, pid_t pid) const override { return true; } - bool IsValidForTask(int tid) const override { return true; } + bool IsValidForTask(pid_t tid) const override { return true; } private: unsigned long slack_; - static bool IsTimerSlackSupported(int tid); + static bool IsTimerSlackSupported(pid_t tid); }; // Set attribute profile element @@ -126,10 +126,10 @@ class SetAttributeAction : public ProfileAction { const char* Name() const override { return "SetAttribute"; } bool ExecuteForProcess(uid_t uid, pid_t pid) const override; - bool ExecuteForTask(int tid) const override; + bool ExecuteForTask(pid_t tid) const override; bool ExecuteForUID(uid_t uid) const override; bool IsValidForProcess(uid_t uid, pid_t pid) const override; - bool IsValidForTask(int tid) const override; + bool IsValidForTask(pid_t tid) const override; private: const IProfileAttribute* attribute_; @@ -146,11 +146,11 @@ class SetCgroupAction : public ProfileAction { const char* Name() const override { return "SetCgroup"; } bool ExecuteForProcess(uid_t uid, pid_t pid) const override; - bool ExecuteForTask(int tid) const override; + bool ExecuteForTask(pid_t tid) const override; void EnableResourceCaching(ResourceCacheType cache_type) override; void DropResourceCaching(ResourceCacheType cache_type) override; bool IsValidForProcess(uid_t uid, pid_t pid) const override; - bool IsValidForTask(int tid) const override; + bool IsValidForTask(pid_t tid) const override; const CgroupController* controller() const { return &controller_; } @@ -160,7 +160,7 @@ class SetCgroupAction : public ProfileAction { android::base::unique_fd fd_[ProfileAction::RCT_COUNT]; mutable std::mutex fd_mutex_; - static bool AddTidToCgroup(int tid, int fd, const char* controller_name); + static bool AddTidToCgroup(pid_t tid, int fd, const char* controller_name); CacheUseResult UseCachedFd(ResourceCacheType cache_type, int id) const; }; @@ -172,11 +172,11 @@ class WriteFileAction : public ProfileAction { const char* Name() const override { return "WriteFile"; } bool ExecuteForProcess(uid_t uid, pid_t pid) const override; - bool ExecuteForTask(int tid) const override; + bool ExecuteForTask(pid_t tid) const override; void EnableResourceCaching(ResourceCacheType cache_type) override; void DropResourceCaching(ResourceCacheType cache_type) override; bool IsValidForProcess(uid_t uid, pid_t pid) const override; - bool IsValidForTask(int tid) const override; + bool IsValidForTask(pid_t tid) const override; private: std::string task_path_, proc_path_, value_; @@ -198,12 +198,12 @@ class TaskProfile { void MoveTo(TaskProfile* profile); bool ExecuteForProcess(uid_t uid, pid_t pid) const; - bool ExecuteForTask(int tid) const; + bool ExecuteForTask(pid_t tid) const; bool ExecuteForUID(uid_t uid) const; void EnableResourceCaching(ProfileAction::ResourceCacheType cache_type); void DropResourceCaching(ProfileAction::ResourceCacheType cache_type); bool IsValidForProcess(uid_t uid, pid_t pid) const; - bool IsValidForTask(int tid) const; + bool IsValidForTask(pid_t tid) const; private: const std::string name_; @@ -219,11 +219,11 @@ class ApplyProfileAction : public ProfileAction { const char* Name() const override { return "ApplyProfileAction"; } bool ExecuteForProcess(uid_t uid, pid_t pid) const override; - bool ExecuteForTask(int tid) const override; + bool ExecuteForTask(pid_t tid) const override; void EnableResourceCaching(ProfileAction::ResourceCacheType cache_type) override; void DropResourceCaching(ProfileAction::ResourceCacheType cache_type) override; bool IsValidForProcess(uid_t uid, pid_t pid) const override; - bool IsValidForTask(int tid) const override; + bool IsValidForTask(pid_t tid) const override; private: std::vector> profiles_; @@ -240,7 +240,7 @@ class TaskProfiles { template bool SetProcessProfiles(uid_t uid, pid_t pid, std::span profiles, bool use_fd_cache); template - bool SetTaskProfiles(int tid, std::span profiles, bool use_fd_cache); + bool SetTaskProfiles(pid_t tid, std::span profiles, bool use_fd_cache); template bool SetUserProfiles(uid_t uid, std::span profiles, bool use_fd_cache);