]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
python: Add python bindings for get_current_controller_path()
authorKamalesh Babulal <kamalesh.babulal@oracle.com>
Thu, 27 Apr 2023 11:09:05 +0000 (11:09 +0000)
committerTom Hromatka <tom.hromatka@oracle.com>
Thu, 27 Apr 2023 14:24:06 +0000 (08:24 -0600)
Add python bindings for get_current_controller_path()

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
src/python/cgroup.pxd
src/python/libcgroup.pyx

index a27088e5df04ed0aaca04e50d4bc61131e9a0731..7f011de26ec2687e2b215d1f46fcacc40f9d2df7 100644 (file)
@@ -115,4 +115,6 @@ cdef extern from "libcgroup.h":
 
     bool is_cgroup_mode_unified()
 
+    int cgroup_get_current_controller_path(pid_t pid, const char *controller,
+                                           char **current_path)
 # vim: set et ts=4 sw=4:
index d7b0d42fe98a1ab47dcd078f3e4c8c9bccba0d6d..bab157e295d274dff58676cd8903a3bb8fd4f81f 100644 (file)
@@ -654,6 +654,41 @@ cdef class Cgroup:
         """
         return cgroup.is_cgroup_mode_unified()
 
+    @staticmethod
+    def get_current_controller_path(pid, controller=None):
+        """Get the cgroup path of pid, under controller hierarchy
+
+        Return:
+        Return cgroup path relative to mount point
+
+        Description:
+        Invokes the libcgroup C function, cgroup_get_current_controller_path().
+        It parses the /proc/<pid>/cgroup file and returns the cgroup path, if
+        the controller matches in the output for cgroup v1 controllers and for
+        the cgroup v2 controllers, checks if the cgroup.controllers file has
+        the controller enabled.
+        """
+        cdef char *current_path
+
+        Cgroup.cgroup_init()
+
+        if controller is None:
+            ret = cgroup.cgroup_get_current_controller_path(pid, NULL,
+                        &current_path)
+        elif isinstance(controller, str):
+            ret = cgroup.cgroup_get_current_controller_path(pid,
+                        c_str(controller), &current_path)
+        else:
+            raise TypeError("cgroup_get_current_controller_path failed: "
+                            "expected controller type string, but passed "
+                            "{}".format(type(controller)))
+
+        if ret is not 0:
+            raise RuntimeError("cgroup_get_current_controller_path failed :"
+                               "{}".format(ret))
+
+        return current_path.decode('ascii')
+
     def __dealloc__(self):
         cgroup.cgroup_free(&self._cgp);