From: Tom Hromatka Date: Wed, 4 Jan 2023 21:06:45 +0000 (+0000) Subject: python: Add python bindings for cgroup_attach_task*() X-Git-Tag: v3.1.0~250 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=95c943ed0f973971c64a3771481481b0a801432d;p=thirdparty%2Flibcgroup.git python: Add python bindings for cgroup_attach_task*() Add python bindings for cgroup_attach_task() and cgroup_attach_task_pid(). Add a convenience wrapper around them in the Cgroup() class. Signed-off-by: Tom Hromatka Reviewed-by: Kamalesh Babulal --- diff --git a/src/python/cgroup.pxd b/src/python/cgroup.pxd index 7c71607b..ed7ad69f 100644 --- a/src/python/cgroup.pxd +++ b/src/python/cgroup.pxd @@ -89,4 +89,7 @@ cdef extern from "libcgroup.h": char *cgroup_get_controller_name(cgroup_controller *controller) + int cgroup_attach_task(cgroup * cgroup) + int cgroup_attach_task_pid(cgroup * cgroup, pid_t pid) + # vim: set et ts=4 sw=4: diff --git a/src/python/libcgroup.pyx b/src/python/libcgroup.pyx index ea3203d2..6edf71fd 100644 --- a/src/python/libcgroup.pyx +++ b/src/python/libcgroup.pyx @@ -16,6 +16,7 @@ __date__ = "25 October 2021" from posix.types cimport pid_t cimport cgroup +import os cdef class Version: CGROUP_UNK = cgroup.CGROUP_UNK @@ -405,6 +406,30 @@ cdef class Cgroup: if ret is not 0: raise RuntimeError("cgroup_delete_cgroup failed: {}".format(ret)) + def attach(self, pid=None, root_cgroup=False): + """Attach a process to a cgroup + + Arguments: + pid - pid to be attached. If none, then the current pid is attached + root_cgroup - if True, then the pid will be attached to the root cgroup + + Description: + Attach a process to a cgroup + """ + if pid is None: + if root_cgroup: + ret = cgroup.cgroup_attach_task(NULL) + else: + ret = cgroup.cgroup_attach_task(self._cgp) + else: + if root_cgroup: + ret = cgroup.cgroup_attach_task_pid(NULL, pid) + else: + ret = cgroup.cgroup_attach_task_pid(self._cgp, pid) + + if ret is not 0: + raise RuntimeError("cgroup_attach_task failed: {}".format(ret)) + def __dealloc__(self): cgroup.cgroup_free(&self._cgp);