From 787ddbc8a57b34d8d74dfd19b47ab5c816ae2af3 Mon Sep 17 00:00:00 2001 From: "T.J. Mercier" Date: Tue, 8 Oct 2024 23:41:34 +0000 Subject: [PATCH] Reapply "libprocessgroup: Combine all 3 ActivateControllers imple..." This reverts commit 0fa49253a484fc0c99d067ade57a4978059b3a95. Change-Id: I83121ff295caaabc0a2fd8a606ee2d52dacb0174 --- libprocessgroup/cgroup_map.cpp | 22 +--------- libprocessgroup/cgroup_map.h | 2 +- libprocessgroup/processgroup.cpp | 7 ++-- libprocessgroup/setup/cgroup_map_write.cpp | 42 +------------------ .../util/include/processgroup/util.h | 2 + libprocessgroup/util/util.cpp | 23 ++++++++++ 6 files changed, 33 insertions(+), 65 deletions(-) diff --git a/libprocessgroup/cgroup_map.cpp b/libprocessgroup/cgroup_map.cpp index 8180ccf3f..32bef13a1 100644 --- a/libprocessgroup/cgroup_map.cpp +++ b/libprocessgroup/cgroup_map.cpp @@ -194,24 +194,6 @@ CgroupControllerWrapper CgroupMap::FindControllerByPath(const std::string& path) return CgroupControllerWrapper(nullptr); } -int CgroupMap::ActivateControllers(const std::string& path) const { - for (const auto& [name, descriptor] : descriptors_) { - const uint32_t flags = descriptor.controller()->flags(); - const uint32_t max_activation_depth = descriptor.controller()->max_activation_depth(); - const int depth = GetCgroupDepth(descriptor.controller()->path(), path); - - if (flags & CGROUPRC_CONTROLLER_FLAG_NEEDS_ACTIVATION && depth < max_activation_depth) { - std::string str("+"); - str.append(descriptor.controller()->name()); - if (!WriteStringToFile(str, path + "/cgroup.subtree_control")) { - if (flags & CGROUPRC_CONTROLLER_FLAG_OPTIONAL) { - PLOG(WARNING) << "Activation of cgroup controller " << str - << " failed in path " << path; - } else { - return -errno; - } - } - } - } - return 0; +bool CgroupMap::ActivateControllers(const std::string& path) const { + return ::ActivateControllers(path, descriptors_); } diff --git a/libprocessgroup/cgroup_map.h b/libprocessgroup/cgroup_map.h index 5ad59bddf..fb9907645 100644 --- a/libprocessgroup/cgroup_map.h +++ b/libprocessgroup/cgroup_map.h @@ -58,7 +58,7 @@ class CgroupMap { static CgroupMap& GetInstance(); CgroupControllerWrapper FindController(const std::string& name) const; CgroupControllerWrapper FindControllerByPath(const std::string& path) const; - int ActivateControllers(const std::string& path) const; + bool ActivateControllers(const std::string& path) const; private: bool loaded_ = false; diff --git a/libprocessgroup/processgroup.cpp b/libprocessgroup/processgroup.cpp index 83a2258bf..d3719eea3 100644 --- a/libprocessgroup/processgroup.cpp +++ b/libprocessgroup/processgroup.cpp @@ -662,10 +662,9 @@ static int createProcessGroupInternal(uid_t uid, pid_t initialPid, std::string c return -errno; } if (activate_controllers) { - ret = CgroupMap::GetInstance().ActivateControllers(uid_path); - if (ret) { - LOG(ERROR) << "Failed to activate controllers in " << uid_path; - return ret; + if (!CgroupMap::GetInstance().ActivateControllers(uid_path)) { + PLOG(ERROR) << "Failed to activate controllers in " << uid_path; + return -errno; } } diff --git a/libprocessgroup/setup/cgroup_map_write.cpp b/libprocessgroup/setup/cgroup_map_write.cpp index 821168008..d05bf2408 100644 --- a/libprocessgroup/setup/cgroup_map_write.cpp +++ b/libprocessgroup/setup/cgroup_map_write.cpp @@ -180,25 +180,7 @@ static bool ActivateV2CgroupController(const CgroupDescriptor& descriptor) { return false; } - if (controller->flags() & CGROUPRC_CONTROLLER_FLAG_NEEDS_ACTIVATION && - controller->max_activation_depth() > 0) { - std::string str = "+"; - str += controller->name(); - std::string path = controller->path(); - path += "/cgroup.subtree_control"; - - if (!android::base::WriteStringToFile(str, path)) { - if (IsOptionalController(controller)) { - PLOG(INFO) << "Failed to activate optional controller " << controller->name() - << " at " << path; - return true; - } - PLOG(ERROR) << "Failed to activate controller " << controller->name(); - return false; - } - } - - return true; + return ::ActivateControllers(controller->path(), {{controller->name(), descriptor}}); } static bool MountV1CgroupController(const CgroupDescriptor& descriptor) { @@ -323,27 +305,7 @@ static bool CreateV2SubHierarchy(const std::string& path, const CgroupDescriptor // Activate all v2 controllers in path so they can be activated in // children as they are created. - for (const auto& [name, descriptor] : descriptors) { - const CgroupController* controller = descriptor.controller(); - std::uint32_t flags = controller->flags(); - std::uint32_t max_activation_depth = controller->max_activation_depth(); - const int depth = GetCgroupDepth(controller->path(), path); - - if (controller->version() == 2 && name != CGROUPV2_HIERARCHY_NAME && - flags & CGROUPRC_CONTROLLER_FLAG_NEEDS_ACTIVATION && depth < max_activation_depth) { - std::string str("+"); - str += controller->name(); - if (!android::base::WriteStringToFile(str, path + "/cgroup.subtree_control")) { - if (flags & CGROUPRC_CONTROLLER_FLAG_OPTIONAL) { - PLOG(WARNING) << "Activation of cgroup controller " << str << " failed in path " - << path; - } else { - return false; - } - } - } - } - return true; + return ::ActivateControllers(path, descriptors); } bool CgroupSetup() { diff --git a/libprocessgroup/util/include/processgroup/util.h b/libprocessgroup/util/include/processgroup/util.h index d592a6347..2c7b32926 100644 --- a/libprocessgroup/util/include/processgroup/util.h +++ b/libprocessgroup/util/include/processgroup/util.h @@ -31,3 +31,5 @@ unsigned int GetCgroupDepth(const std::string& controller_root, const std::strin using CgroupControllerName = std::string; using CgroupDescriptorMap = std::map; bool ReadDescriptors(CgroupDescriptorMap* descriptors); + +bool ActivateControllers(const std::string& path, const CgroupDescriptorMap& descriptors); diff --git a/libprocessgroup/util/util.cpp b/libprocessgroup/util/util.cpp index bff4c6f1a..14016751c 100644 --- a/libprocessgroup/util/util.cpp +++ b/libprocessgroup/util/util.cpp @@ -250,3 +250,26 @@ bool ReadDescriptors(CgroupDescriptorMap* descriptors) { return true; } + +bool ActivateControllers(const std::string& path, const CgroupDescriptorMap& descriptors) { + for (const auto& [name, descriptor] : descriptors) { + const uint32_t flags = descriptor.controller()->flags(); + const uint32_t max_activation_depth = descriptor.controller()->max_activation_depth(); + const unsigned int depth = GetCgroupDepth(descriptor.controller()->path(), path); + + if (flags & CGROUPRC_CONTROLLER_FLAG_NEEDS_ACTIVATION && depth < max_activation_depth) { + std::string str("+"); + str.append(descriptor.controller()->name()); + if (!android::base::WriteStringToFile(str, path + "/cgroup.subtree_control")) { + if (flags & CGROUPRC_CONTROLLER_FLAG_OPTIONAL) { + PLOG(WARNING) << "Activation of cgroup controller " << str + << " failed in path " << path; + } else { + return false; + } + } + } + } + return true; +} +