Remove hardcoded cgroup v2 path

Replace hardcoded cgroup v2 root path with new libprocessgroup API calls.

Bug: 111307099
Test: builds, boots

Change-Id: Ic9c47b52702767f9934f65d04bb91ab303b1d06e
Merged-In: Ic9c47b52702767f9934f65d04bb91ab303b1d06e
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
diff --git a/libbpf_android/Android.bp b/libbpf_android/Android.bp
index 721792f..0bacf7b 100644
--- a/libbpf_android/Android.bp
+++ b/libbpf_android/Android.bp
@@ -45,6 +45,7 @@
     shared_libs: [
         "libbase",
         "libutils",
+        "libprocessgroup",
         "liblog",
         "libnetdutils",
         "libbpf",
diff --git a/libbpf_android/BpfUtils.cpp b/libbpf_android/BpfUtils.cpp
index f5741bb..74585b2 100644
--- a/libbpf_android/BpfUtils.cpp
+++ b/libbpf_android/BpfUtils.cpp
@@ -38,6 +38,7 @@
 #include <log/log.h>
 #include <netdutils/MemBlock.h>
 #include <netdutils/Slice.h>
+#include <processgroup/processgroup.h>
 
 using android::base::GetUintProperty;
 using android::base::unique_fd;
@@ -272,7 +273,13 @@
         return ret;
     }
     if (prog->attachType == BPF_CGROUP_INET_EGRESS || prog->attachType == BPF_CGROUP_INET_INGRESS) {
-        unique_fd cg_fd(open(CGROUP_ROOT_PATH, O_DIRECTORY | O_RDONLY | O_CLOEXEC));
+        std::string cg2_path;
+        if (!CgroupGetControllerPath(CGROUPV2_CONTROLLER_NAME, &cg2_path)) {
+            int ret = -errno;
+            ALOGE("Failed to find cgroup v2 root");
+            return ret;
+        }
+        unique_fd cg_fd(open(cg2_path.c_str(), O_DIRECTORY | O_RDONLY | O_CLOEXEC));
         if (cg_fd < 0) {
             int ret = -errno;
             ALOGE("Failed to open the cgroup directory");
diff --git a/libbpf_android/include/bpf/BpfUtils.h b/libbpf_android/include/bpf/BpfUtils.h
index 426dc02..296b0fd 100644
--- a/libbpf_android/include/bpf/BpfUtils.h
+++ b/libbpf_android/include/bpf/BpfUtils.h
@@ -125,8 +125,6 @@
 #define DEFAULT_OVERFLOWUID 65534
 #endif
 
-constexpr const char* CGROUP_ROOT_PATH = "/dev/cg2_bpf";
-
 constexpr const int OVERFLOW_COUNTERSET = 2;
 
 constexpr const uint64_t NONEXISTENT_COOKIE = 0;