This is an automated email from the ASF dual-hosted git repository.

bmahler pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mesos.git


The following commit(s) were added to refs/heads/master by this push:
     new 1c6cab2ed [cgroups2] Add cgroups2::path to get the path of a cgroup.
1c6cab2ed is described below

commit 1c6cab2edbac0de3171187baa3d4d33111d0c999
Author: Devin Leamy <[email protected]>
AuthorDate: Thu Mar 21 11:18:15 2024 -0400

    [cgroups2] Add cgroups2::path to get the path of a cgroup.
    
    Introduces a utility to get the absolute path of a cgroup
    and refactors code to use it.
    
    This closes #529
---
 src/linux/cgroups2.cpp                     | 32 +++++++++++++++++-------------
 src/linux/cgroups2.hpp                     |  4 ++++
 src/tests/containerizer/cgroups2_tests.cpp |  8 ++++++++
 3 files changed, 30 insertions(+), 14 deletions(-)

diff --git a/src/linux/cgroups2.cpp b/src/linux/cgroups2.cpp
index ee3013373..7e1a801a3 100644
--- a/src/linux/cgroups2.cpp
+++ b/src/linux/cgroups2.cpp
@@ -147,7 +147,7 @@ std::ostream& operator<<(std::ostream& stream, const State& 
state)
 template <>
 Try<string> read(const string& cgroup, const string& control)
 {
-  return os::read(path::join(cgroups2::MOUNT_POINT, cgroup, control));
+  return os::read(path::join(cgroups2::path(cgroup), control));
 }
 
 
@@ -169,7 +169,7 @@ Try<Nothing> write(
     const string& control,
     const string& value)
 {
-  return os::write(path::join(cgroups2::MOUNT_POINT, cgroup, control), value);
+  return os::write(path::join(cgroups2::path(cgroup), control), value);
 }
 
 
@@ -272,18 +272,17 @@ Try<Nothing> unmount()
 
 bool exists(const string& cgroup)
 {
-  return os::exists(path::join(MOUNT_POINT, cgroup));
+  return os::exists(cgroups2::path(cgroup));
 }
 
 
 Try<Nothing> create(const string& cgroup, bool recursive)
 {
-  const string absolutePath = path::join(MOUNT_POINT, cgroup);
+  const string path = cgroups2::path(cgroup);
 
-  Try<Nothing> mkdir = os::mkdir(absolutePath, recursive);
+  Try<Nothing> mkdir = os::mkdir(path, recursive);
   if (mkdir.isError()) {
-    return Error("Failed to create directory '" + absolutePath + "': "
-                 + mkdir.error());
+    return Error("Failed to create directory '" + path + "': " + 
mkdir.error());
   }
 
   return Nothing();
@@ -296,11 +295,10 @@ Try<Nothing> destroy(const string& cgroup)
     return Error("Cgroup '" + cgroup + "' does not exist");
   }
 
-  const string absolutePath = path::join(MOUNT_POINT, cgroup);
-  Try<Nothing> rmdir = os::rmdir(absolutePath, false);
+  const string path = cgroups2::path(cgroup);
+  Try<Nothing> rmdir = os::rmdir(path, false);
   if (rmdir.isError()) {
-    return Error("Failed to remove directory '" + absolutePath + "': "
-                 + rmdir.error());
+    return Error("Failed to remove directory '" + path + "': " + 
rmdir.error());
   }
 
   return Nothing();
@@ -309,10 +307,10 @@ Try<Nothing> destroy(const string& cgroup)
 
 Try<Nothing> move_process(const string& cgroup, pid_t pid)
 {
-  const string absolutePath = path::join(MOUNT_POINT, cgroup);
+  const string path = cgroups2::path(cgroup);
 
-  if (!os::exists(absolutePath)) {
-    return Error("There does not exist a cgroup at '" + absolutePath + "'");
+  if (!os::exists(path)) {
+    return Error("There does not exist a cgroup at '" + path + "'");
   }
 
   return cgroups2::write(cgroup, control::PROCESSES, stringify(pid));
@@ -351,6 +349,12 @@ Try<string> cgroup(pid_t pid)
   return content;
 }
 
+
+string path(const string& cgroup)
+{
+  return path::join(cgroups2::MOUNT_POINT, cgroup);
+}
+
 namespace controllers {
 
 Try<set<string>> available(const string& cgroup)
diff --git a/src/linux/cgroups2.hpp b/src/linux/cgroups2.hpp
index b24e8c75d..ea3e577f9 100644
--- a/src/linux/cgroups2.hpp
+++ b/src/linux/cgroups2.hpp
@@ -75,6 +75,10 @@ Try<Nothing> move_process(const std::string& cgroup, pid_t 
pid);
 // /sys/fs/cgroup. E.g. For /sys/fs/cgroup/test, this will return "test".
 Try<std::string> cgroup(pid_t pid);
 
+
+// Get the absolute of a cgroup. The cgroup provided should not start with '/'.
+std::string path(const std::string& cgroup);
+
 namespace controllers {
 
 // Gets the controllers that can be controlled by the provided cgroup.
diff --git a/src/tests/containerizer/cgroups2_tests.cpp 
b/src/tests/containerizer/cgroups2_tests.cpp
index 795dc8816..ee3932e0e 100644
--- a/src/tests/containerizer/cgroups2_tests.cpp
+++ b/src/tests/containerizer/cgroups2_tests.cpp
@@ -115,6 +115,14 @@ TEST_F(Cgroups2Test, ROOT_CGROUPS2_AssignProcessToCgroup)
   AWAIT_EXPECT_WTERMSIG_EQ(SIGKILL, process::reap(pid));
 }
 
+
+TEST_F(Cgroups2Test, CGROUPS2_Path)
+{
+  EXPECT_EQ("/sys/fs/cgroup/", cgroups2::path(cgroups2::ROOT_CGROUP));
+  EXPECT_EQ("/sys/fs/cgroup/foo", cgroups2::path("foo"));
+  EXPECT_EQ("/sys/fs/cgroup/foo/bar", cgroups2::path("foo/bar"));
+}
+
 } // namespace tests {
 
 } // namespace internal {

Reply via email to