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

Revert submission 3212512

Reason for revert: Droidmonitor created revert due to b/372273614. Will be verifying through ABTD before submission.

Reverted changes: /q/submissionid:3212512

Change-Id: I3dadc0b7bccfe28bb067a93df2acf2c3ea0f9920
diff --git a/libprocessgroup/cgroup_map.cpp b/libprocessgroup/cgroup_map.cpp
index 32bef13..8180ccf 100644
--- a/libprocessgroup/cgroup_map.cpp
+++ b/libprocessgroup/cgroup_map.cpp
@@ -194,6 +194,24 @@
     return CgroupControllerWrapper(nullptr);
 }
 
-bool CgroupMap::ActivateControllers(const std::string& path) const {
-    return ::ActivateControllers(path, descriptors_);
+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;
 }
diff --git a/libprocessgroup/cgroup_map.h b/libprocessgroup/cgroup_map.h
index fb99076..5ad59bd 100644
--- a/libprocessgroup/cgroup_map.h
+++ b/libprocessgroup/cgroup_map.h
@@ -58,7 +58,7 @@
     static CgroupMap& GetInstance();
     CgroupControllerWrapper FindController(const std::string& name) const;
     CgroupControllerWrapper FindControllerByPath(const std::string& path) const;
-    bool ActivateControllers(const std::string& path) const;
+    int ActivateControllers(const std::string& path) const;
 
   private:
     bool loaded_ = false;
diff --git a/libprocessgroup/processgroup.cpp b/libprocessgroup/processgroup.cpp
index d3719ee..83a2258 100644
--- a/libprocessgroup/processgroup.cpp
+++ b/libprocessgroup/processgroup.cpp
@@ -662,9 +662,10 @@
         return -errno;
     }
     if (activate_controllers) {
-        if (!CgroupMap::GetInstance().ActivateControllers(uid_path)) {
-            PLOG(ERROR) << "Failed to activate controllers in " << uid_path;
-            return -errno;
+        ret = CgroupMap::GetInstance().ActivateControllers(uid_path);
+        if (ret) {
+            LOG(ERROR) << "Failed to activate controllers in " << uid_path;
+            return ret;
         }
     }
 
diff --git a/libprocessgroup/setup/cgroup_map_write.cpp b/libprocessgroup/setup/cgroup_map_write.cpp
index d05bf24..8211680 100644
--- a/libprocessgroup/setup/cgroup_map_write.cpp
+++ b/libprocessgroup/setup/cgroup_map_write.cpp
@@ -180,7 +180,25 @@
         return false;
     }
 
-    return ::ActivateControllers(controller->path(), {{controller->name(), descriptor}});
+    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;
 }
 
 static bool MountV1CgroupController(const CgroupDescriptor& descriptor) {
@@ -305,7 +323,27 @@
 
     // Activate all v2 controllers in path so they can be activated in
     // children as they are created.
-    return ::ActivateControllers(path, descriptors);
+    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;
 }
 
 bool CgroupSetup() {
diff --git a/libprocessgroup/util/include/processgroup/util.h b/libprocessgroup/util/include/processgroup/util.h
index 2c7b329..d592a63 100644
--- a/libprocessgroup/util/include/processgroup/util.h
+++ b/libprocessgroup/util/include/processgroup/util.h
@@ -31,5 +31,3 @@
 using CgroupControllerName = std::string;
 using CgroupDescriptorMap = std::map<CgroupControllerName, CgroupDescriptor>;
 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 1401675..bff4c6f 100644
--- a/libprocessgroup/util/util.cpp
+++ b/libprocessgroup/util/util.cpp
@@ -250,26 +250,3 @@
 
     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;
-}
-