From: Tom Hromatka Date: Wed, 24 May 2023 20:12:45 +0000 (-0600) Subject: python: Add cgroup v1 support to move_process() X-Git-Tag: v3.1.0~34 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=407ba326c0e2ae8d02bbe0113ced0abb761e3493;p=thirdparty%2Flibcgroup.git python: Add cgroup v1 support to move_process() 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 Reviewed-by: Kamalesh Babulal --- diff --git a/src/python/libcgroup.pyx b/src/python/libcgroup.pyx index 2014815f..f23bd6c3 100644 --- a/src/python/libcgroup.pyx +++ b/src/python/libcgroup.pyx @@ -15,9 +15,13 @@ __author__ = 'Tom Hromatka ' __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] = malloc(CONTROL_NAMELEN_MAX) + strcpy(controllers[0], c_str(controller)) + controllers[1] = NULL + + ret = cgroup.cgroup_change_cgroup_path(c_str(dest_cgname), pid, + 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 :"