]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
python: Add cgroup v1 support to move_process()
authorTom Hromatka <tom.hromatka@oracle.com>
Wed, 24 May 2023 20:12:45 +0000 (14:12 -0600)
committerTom Hromatka <tom.hromatka@oracle.com>
Mon, 3 Jul 2023 14:50:55 +0000 (08:50 -0600)
Add support for providing a single controller to Cgroup.move_process().
move_process() invokes cgroup_change_cgroup_path() which utilizes an
array of controller strings.

This change only adds support for a single controller via the python
bindings, but a list of controllers could be supported in the future.

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

index 2014815fffb85ae384effb6f862785bdc3a11d40..f23bd6c34b709f411e5bef4aa78b508d83ef96d4 100644 (file)
@@ -15,9 +15,13 @@ __author__ =  'Tom Hromatka <tom.hromatka@oracle.com>'
 __date__ = "25 October 2021"
 
 from posix.types cimport pid_t, mode_t
+from libc.stdlib cimport malloc, free
+from libc.string cimport strcpy
 cimport cgroup
 import os
 
+CONTROL_NAMELEN_MAX = 32
+
 cdef class Version:
     CGROUP_UNK = cgroup.CGROUP_UNK
     CGROUP_V1 = cgroup.CGROUP_V1
@@ -752,7 +756,7 @@ cdef class Cgroup:
         return current_path.decode('ascii')
 
     @staticmethod
-    def move_process(pid, dest_cgname):
+    def move_process(pid, dest_cgname, controller=None):
         """Move a process to the specified cgroup
 
         Return:
@@ -762,11 +766,29 @@ cdef class Cgroup:
         Invokes the libcgroup C function, cgroup_change_cgroup_path().
         It moves a process to the specified cgroup dest_cgname.
 
+        To move the process to a cgroup v1 cgroup, the controller must be
+        provided.  For cgroup v2, the controller is optional
+
         Note:
         * Writes to the cgroup sysfs
-        * Only works on cgroup v2 (unified) hierarchies
         """
-        ret = cgroup.cgroup_change_cgroup_path(c_str(dest_cgname), pid, NULL)
+        cdef char *controllers[2]
+
+        if not controller:
+            ret = cgroup.cgroup_change_cgroup_path(c_str(dest_cgname), pid, NULL)
+        elif isinstance(controller, str):
+            controllers[0] = <char *>malloc(CONTROL_NAMELEN_MAX)
+            strcpy(controllers[0], c_str(controller))
+            controllers[1] = NULL
+
+            ret = cgroup.cgroup_change_cgroup_path(c_str(dest_cgname), pid,
+                                                   <const char * const *>controllers)
+            free(controllers[0])
+        else:
+            #
+            # In the future we could add support for a list of controllers
+            #
+            raise TypeError("Unsupported controller type: {}".format(type(controller)))
 
         if ret is not 0:
             raise RuntimeError("cgroup_change_cgroup_path failed :"