]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
python: systemd: Add python bindings to create a systemd scope
authorTom Hromatka <tom.hromatka@oracle.com>
Wed, 26 Oct 2022 16:17:36 +0000 (10:17 -0600)
committerTom Hromatka <tom.hromatka@oracle.com>
Tue, 1 Nov 2022 21:20:54 +0000 (15:20 -0600)
Add python bindings to create a systemd scope.

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
src/python/setup.py

index 4f85746e660d6de50faee55ef6ac8fab50b996b4..0723e01ab64ce0440c15802eccdb30eef47bd7fb 100644 (file)
@@ -8,6 +8,8 @@
 
 # cython: language_level = 3str
 
+from posix.types cimport pid_t
+
 cdef extern from "libcgroup.h":
     cdef struct cgroup:
         pass
@@ -32,6 +34,18 @@ cdef extern from "libcgroup.h":
         unsigned int minor
         unsigned int release
 
+    cdef enum cgroup_systemd_mode_t:
+        CGROUP_SYSTEMD_MODE_FAIL
+        CGROUP_SYSTEMD_MODE_REPLACE
+        CGROUP_SYSTEMD_MODE_ISOLATE
+        CGROUP_SYSTEMD_MODE_IGNORE_DEPS
+        CGROUP_SYSTEMD_MODE_IGNORE_REQS
+
+    cdef struct cgroup_systemd_scope_opts:
+        int delegated
+        cgroup_systemd_mode_t mode
+        pid_t pid
+
     int cgroup_init()
     const cgroup_library_version * cgroup_version()
 
@@ -62,4 +76,7 @@ cdef extern from "libcgroup.h":
 
     cg_setup_mode_t cgroup_setup_mode()
 
+    int cgroup_create_scope(const char * const scope_name, const char * const slice_name,
+                            const cgroup_systemd_scope_opts * const opts)
+
 # vim: set et ts=4 sw=4:
index 6b4ff386fd2a7410201ea5087b43c747e74e8165..539a6df66b3ee567d8f963a9df8382c57a786d93 100644 (file)
@@ -14,6 +14,7 @@
 __author__ =  'Tom Hromatka <tom.hromatka@oracle.com>'
 __date__ = "25 October 2021"
 
+from posix.types cimport pid_t
 cimport cgroup
 
 cdef class Version:
@@ -28,6 +29,13 @@ cdef class Mode:
     CGROUP_MODE_HYBRID = cgroup.CGROUP_MODE_HYBRID
     CGROUP_MODE_UNIFIED = cgroup.CGROUP_MODE_UNIFIED
 
+cdef class SystemdMode:
+    CGROUP_SYSTEMD_MODE_FAIL = cgroup.CGROUP_SYSTEMD_MODE_FAIL
+    CGROUP_SYSTEMD_MODE_REPLACE = cgroup.CGROUP_SYSTEMD_MODE_REPLACE
+    CGROUP_SYSTEMD_MODE_ISOLATE = cgroup.CGROUP_SYSTEMD_MODE_ISOLATE
+    CGROUP_SYSTEMD_MODE_IGNORE_DEPS = cgroup.CGROUP_SYSTEMD_MODE_IGNORE_DEPS
+    CGROUP_SYSTEMD_MODE_IGNORE_REQS = cgroup.CGROUP_SYSTEMD_MODE_IGNORE_REQS
+
 def c_str(string):
     return bytes(string, "ascii")
 
@@ -326,6 +334,44 @@ cdef class Cgroup:
         Cgroup.cgroup_init()
         return cgroup.cgroup_setup_mode()
 
+    @staticmethod
+    def create_scope(scope_name='libcgroup.scope', slice_name='libcgroup.slice', delegated=True,
+                     systemd_mode=SystemdMode.CGROUP_SYSTEMD_MODE_FAIL, pid=None):
+        """Create a systemd scope
+
+        Arguments:
+        scope_name - name of the scope to be created
+        slice_name - name of the slice where the scope should reside
+        delegated - if true, then systemd will not manage the cgroup aspects of the scope.  It
+                    is up to the user to manage the cgroup settings
+        systemd_mode - setting to tell systemd how to handle creation of this scope and
+                       resolve conflicts if the scope and/or slice exist
+        pid - pid of the process to place in the scope.  If None is provided, libcgroup will
+              place a dummy process in the scope
+
+        Description:
+        Create a systemd scope under slice_name.  If delegated is true, then systemd will
+        not manage the cgroup aspects of the scope.
+        """
+        cdef cgroup.cgroup_systemd_scope_opts opts
+
+        Cgroup.cgroup_init()
+
+        if delegated:
+            opts.delegated = 1
+        else:
+            opts.delegated = 0
+
+        opts.mode = systemd_mode
+        if pid:
+            opts.pid = pid
+        else:
+            opts.pid = -1
+
+        ret = cgroup.cgroup_create_scope(c_str(scope_name), c_str(slice_name), &opts)
+        if ret is not 0:
+            raise RuntimeError("cgroup_create_scope failed: {}".format(ret))
+
     def __dealloc__(self):
         cgroup.cgroup_free(&self._cgp);
 
index 8bb4d6a5082e09a14b735ed37093591574203f82..753ad874bd8c726374d73e0283ea360c0bae81a2 100755 (executable)
@@ -26,7 +26,8 @@ setup(
             Extension(
                       'libcgroup', ['libcgroup.pyx'],
                       # unable to handle libtool libraries directly
-                      extra_objects=['../.libs/libcgroup.a']
+                      extra_objects=['../.libs/libcgroup.a'],
+                      libraries=['systemd']
                      ),
              ])
 )