]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
python: Add python bindings for cgroup_attach_task*()
authorTom Hromatka <tom.hromatka@oracle.com>
Wed, 4 Jan 2023 21:06:45 +0000 (21:06 +0000)
committerTom Hromatka <tom.hromatka@oracle.com>
Fri, 6 Jan 2023 15:28:48 +0000 (08:28 -0700)
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 <tom.hromatka@oracle.com>
Reviewed-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
src/python/cgroup.pxd
src/python/libcgroup.pyx

index 7c71607b4fdc22247b782f30d0f59a74f7382e78..ed7ad69fc889aba6998e1a005fbacdc3fa6b3fc3 100644 (file)
@@ -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:
index ea3203d26859232bf3d410ce21d8beeadcfa8629..6edf71fdf47735abf17d5820692759e90e8f296e 100644 (file)
@@ -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);