From f32c4ecebb323b24638de9a8100846cbffaff30a Mon Sep 17 00:00:00 2001 From: Bart Van Assche Date: Tue, 2 Aug 2022 13:18:12 -0700 Subject: [PATCH] libprocessgroup: Provide SetProcessProfiles() and SetTaskProfiles() alternatives Provide alternative versions that do not force callers to create std::string objects. This patch has the intended side-effect that all callers that pass a {string} initializer list to the 'profiles' argument now call an std::initializer_list<> overload instead of the const std::vector& overload. Additionally, add std::function<> arguments instead of calling ExecuteForProcess() or ExecuteForTask() directly to make it easier to write unit tests for SetTaskProfiles() and SetProcessProfiles(). Bug: 213617178 Change-Id: Ica61e944a66a17178ee43a113b8ca082f7eb834b Signed-off-by: Bart Van Assche --- .../include/processgroup/processgroup.h | 16 +++++++++++ libprocessgroup/processgroup.cpp | 27 ++++++++++++++++--- libprocessgroup/task_profiles.cpp | 20 +++++++++++--- libprocessgroup/task_profiles.h | 9 ++++--- 4 files changed, 62 insertions(+), 10 deletions(-) diff --git a/libprocessgroup/include/processgroup/processgroup.h b/libprocessgroup/include/processgroup/processgroup.h index 39b9f3fc0..45a723f74 100644 --- a/libprocessgroup/include/processgroup/processgroup.h +++ b/libprocessgroup/include/processgroup/processgroup.h @@ -18,7 +18,10 @@ #include #include +#include +#include #include +#include #include __BEGIN_DECLS @@ -33,6 +36,19 @@ bool CgroupGetAttributePathForTask(const std::string& attr_name, int tid, std::s bool SetTaskProfiles(int tid, const std::vector& profiles, bool use_fd_cache = false); bool SetProcessProfiles(uid_t uid, pid_t pid, const std::vector& profiles); +__END_DECLS + +bool SetTaskProfiles(int 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 use_fd_cache = false); +bool SetProcessProfiles(uid_t uid, pid_t pid, std::span profiles); +#endif + +__BEGIN_DECLS + #ifndef __ANDROID_VNDK__ bool SetProcessProfilesCached(uid_t uid, pid_t pid, const std::vector& profiles); diff --git a/libprocessgroup/processgroup.cpp b/libprocessgroup/processgroup.cpp index 51c810e98..bdda1020c 100644 --- a/libprocessgroup/processgroup.cpp +++ b/libprocessgroup/processgroup.cpp @@ -148,14 +148,35 @@ void DropTaskProfilesResourceCaching() { } bool SetProcessProfiles(uid_t uid, pid_t pid, const std::vector& profiles) { + return TaskProfiles::GetInstance().SetProcessProfiles( + uid, pid, std::span(profiles), false); +} + +bool SetProcessProfiles(uid_t uid, pid_t pid, std::initializer_list profiles) { + return TaskProfiles::GetInstance().SetProcessProfiles( + uid, pid, std::span(profiles), false); +} + +bool SetProcessProfiles(uid_t uid, pid_t pid, std::span profiles) { return TaskProfiles::GetInstance().SetProcessProfiles(uid, pid, profiles, false); } bool SetProcessProfilesCached(uid_t uid, pid_t pid, const std::vector& profiles) { - return TaskProfiles::GetInstance().SetProcessProfiles(uid, pid, profiles, true); + return TaskProfiles::GetInstance().SetProcessProfiles( + uid, pid, std::span(profiles), true); } bool SetTaskProfiles(int 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) { + return TaskProfiles::GetInstance().SetTaskProfiles( + tid, std::span(profiles), use_fd_cache); +} + +bool SetTaskProfiles(int tid, std::span profiles, bool use_fd_cache) { return TaskProfiles::GetInstance().SetTaskProfiles(tid, profiles, use_fd_cache); } @@ -166,12 +187,12 @@ bool SetTaskProfiles(int tid, const std::vector& profiles, bool use // https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3574427/5/src/linux/android.rs#12 extern "C" bool android_set_process_profiles(uid_t uid, pid_t pid, size_t num_profiles, const char* profiles[]) { - std::vector profiles_; + std::vector profiles_; profiles_.reserve(num_profiles); for (size_t i = 0; i < num_profiles; i++) { profiles_.emplace_back(profiles[i]); } - return SetProcessProfiles(uid, pid, profiles_); + return SetProcessProfiles(uid, pid, std::span(profiles_)); } static std::string ConvertUidToPath(const char* cgroup, uid_t uid) { diff --git a/libprocessgroup/task_profiles.cpp b/libprocessgroup/task_profiles.cpp index dbab2d47f..744710f3b 100644 --- a/libprocessgroup/task_profiles.cpp +++ b/libprocessgroup/task_profiles.cpp @@ -804,8 +804,9 @@ const IProfileAttribute* TaskProfiles::GetAttribute(std::string_view name) const return nullptr; } -bool TaskProfiles::SetProcessProfiles(uid_t uid, pid_t pid, - const std::vector& profiles, bool use_fd_cache) { +template +bool TaskProfiles::SetProcessProfiles(uid_t uid, pid_t pid, std::span profiles, + bool use_fd_cache) { bool success = true; for (const auto& name : profiles) { TaskProfile* profile = GetProfile(name); @@ -825,8 +826,8 @@ bool TaskProfiles::SetProcessProfiles(uid_t uid, pid_t pid, return success; } -bool TaskProfiles::SetTaskProfiles(int tid, const std::vector& profiles, - bool use_fd_cache) { +template +bool TaskProfiles::SetTaskProfiles(int tid, std::span profiles, bool use_fd_cache) { bool success = true; for (const auto& name : profiles) { TaskProfile* profile = GetProfile(name); @@ -845,3 +846,14 @@ bool TaskProfiles::SetTaskProfiles(int tid, const std::vector& prof } return success; } + +template bool TaskProfiles::SetProcessProfiles(uid_t uid, pid_t pid, + std::span profiles, + bool use_fd_cache); +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, + bool use_fd_cache); +template bool TaskProfiles::SetTaskProfiles(int tid, std::span profiles, + bool use_fd_cache); diff --git a/libprocessgroup/task_profiles.h b/libprocessgroup/task_profiles.h index b2b4f5488..85b3f9162 100644 --- a/libprocessgroup/task_profiles.h +++ b/libprocessgroup/task_profiles.h @@ -18,8 +18,10 @@ #include #include +#include #include #include +#include #include #include #include @@ -210,9 +212,10 @@ class TaskProfiles { TaskProfile* GetProfile(std::string_view name) const; const IProfileAttribute* GetAttribute(std::string_view name) const; void DropResourceCaching(ProfileAction::ResourceCacheType cache_type) const; - bool SetProcessProfiles(uid_t uid, pid_t pid, const std::vector& profiles, - bool use_fd_cache); - bool SetTaskProfiles(int tid, const std::vector& profiles, bool use_fd_cache); + 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); private: TaskProfiles();