Reapply "libprocessgroup: Combine all 3 ActivateControllers imple..."

This reverts commit 0fa49253a4.

Change-Id: I83121ff295caaabc0a2fd8a606ee2d52dacb0174
This commit is contained in:
T.J. Mercier 2024-10-08 23:41:34 +00:00
parent 47580ff76d
commit 787ddbc8a5
6 changed files with 33 additions and 65 deletions

View file

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

View file

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

View file

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

View file

@ -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() {

View file

@ -31,3 +31,5 @@ unsigned int GetCgroupDepth(const std::string& controller_root, const std::strin
using CgroupControllerName = std::string;
using CgroupDescriptorMap = std::map<CgroupControllerName, CgroupDescriptor>;
bool ReadDescriptors(CgroupDescriptorMap* descriptors);
bool ActivateControllers(const std::string& path, const CgroupDescriptorMap& descriptors);

View file

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