From ea2588c6dc921cfc73fe1aa3af2dd4756ebc39a8 Mon Sep 17 00:00:00 2001 From: Tom Hromatka Date: Wed, 17 May 2023 14:49:31 -0600 Subject: [PATCH] python: Add bindings for cgroup_change_cgroup_path() Add python bindings for cgroup_change_cgroup_path(). Note that the bindings currently only support cgroup v2 (unified) systems. Signed-off-by: Tom Hromatka Reviewed-by: Kamalesh Babulal --- src/python/cgroup.pxd | 2 ++ src/python/libcgroup.pyx | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/python/cgroup.pxd b/src/python/cgroup.pxd index 7f011de2..ec326032 100644 --- a/src/python/cgroup.pxd +++ b/src/python/cgroup.pxd @@ -117,4 +117,6 @@ cdef extern from "libcgroup.h": int cgroup_get_current_controller_path(pid_t pid, const char *controller, char **current_path) + + int cgroup_change_cgroup_path(const char *dest, pid_t pid, const char * const controllers[]) # vim: set et ts=4 sw=4: diff --git a/src/python/libcgroup.pyx b/src/python/libcgroup.pyx index bab157e2..aa5484b8 100644 --- a/src/python/libcgroup.pyx +++ b/src/python/libcgroup.pyx @@ -689,6 +689,27 @@ cdef class Cgroup: return current_path.decode('ascii') + @staticmethod + def move_process(pid, dest_cgname): + """Move a process to the specified cgroup + + Return: + None + + Description: + Invokes the libcgroup C function, cgroup_change_cgroup_path(). + It moves a process to the specified cgroup dest_cgname. + + 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) + + if ret is not 0: + raise RuntimeError("cgroup_change_cgroup_path failed :" + "{}".format(ret)) + def __dealloc__(self): cgroup.cgroup_free(&self._cgp); -- 2.47.2