]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
python: Add bindings for cgroup_write_systemd_default_cgroup()
authorTom Hromatka <tom.hromatka@oracle.com>
Fri, 19 May 2023 22:05:25 +0000 (16:05 -0600)
committerTom Hromatka <tom.hromatka@oracle.com>
Mon, 3 Jul 2023 14:50:44 +0000 (08:50 -0600)
Add python bindings for cgroup_write_systemd_default_cgroup().  As part
of this change, demote cgroup_set_default_systemd_cgroup() to an
internal function - __set_default_systemd_cgroup().  I would expect
users to invoke write_default_systemd_scope() and utilize that to set
the default slice/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
tests/ftests/071-sudo-set_default_systemd_cgroup.py

index 31d9ad4a4631b12a925cb19eb3428917af0f89ae..e591876a94a0c7eadce922aa581f6a9a6013eb52 100644 (file)
@@ -110,7 +110,9 @@ cdef extern from "libcgroup.h":
     int cgroup_create_scope2(cgroup *cgroup, int ignore_ownership,
                              const cgroup_systemd_scope_opts * const opts)
 
-    void cgroup_set_default_systemd_cgroup()
+    int cgroup_set_default_systemd_cgroup()
+    int cgroup_write_systemd_default_cgroup(const char * const slice_name,
+                                            const char * const scope_name)
 
     int cgroup_compare_cgroup(cgroup *cgroup_a, cgroup *cgroup_b)
 
index ded55a3fdb1d01a8b714f4593fa18e1d2529448f..e84df01f01b82466f39f4907819814ffb552b457 100644 (file)
@@ -565,7 +565,7 @@ cdef class Cgroup:
             raise RuntimeError("cgroup_create_scope2 failed: {}".format(ret))
 
     @staticmethod
-    def cgroup_set_default_systemd_cgroup():
+    def __set_default_systemd_cgroup():
         """Set systemd_default_cgroup
 
         Arguments:
@@ -578,7 +578,43 @@ cdef class Cgroup:
         cgroup sub-tree is constructed for systemd delegation.
         """
         Cgroup.cgroup_init()
-        cgroup.cgroup_set_default_systemd_cgroup()
+        ret = cgroup.cgroup_set_default_systemd_cgroup()
+
+        if ret != 1:
+            raise RuntimeError('Failed to set the default systemd cgroup')
+
+    @staticmethod
+    def write_default_systemd_scope(slice_name, scope_name, set_default=True):
+        """Write the provided slice and scope to the libcgroup /var/run file
+
+        Arguments:
+        slice_name - Slice name, e.g. libcgroup.slice
+        scope_name - Scope name, e.g. database.scope
+        set_default - If true, set this as the default path for libcgroup APIs
+                      and tools
+
+        Description:
+        Write the provided slice and scope to the libcgroup var/run file.  This
+        convenience function provides a mechanism for setting a slice/scope as
+        the default path within libcgroup.  Any API or cmdline operation will
+        utilize this path as the "root" cgroup, but it can be overridden on a
+        case-by-case basis.
+
+        So if the default slice/scope is set to "libcgroup.slice/database.scope",
+        and the user wants to access "libcgroup.slice/database.scope/foo", then
+        they can use the following:
+
+            # Within libcgroup, this will expand to
+            # libcgroup.slice/database.scope/foo
+            cg = Cgroup('foo')
+        """
+        ret = cgroup.cgroup_write_systemd_default_cgroup(c_str(slice_name),
+                                                         c_str(scope_name))
+        if ret != 1:
+            raise RuntimeError("Failed to write the default slice/scope")
+
+        if set_default:
+            Cgroup.__set_default_systemd_cgroup()
 
     cdef compare(self, Cgroup other):
         """Compare this cgroup instance with another cgroup instance
index 4d67d9ba0e46da188b39984fa871f83056e7e267..29c822e69fd1c8129b8d44c042a53215ecc551fe 100755 (executable)
@@ -124,7 +124,7 @@ def test(config):
     if result == consts.TEST_FAILED:
         return result, cause
 
-    Cgroup.cgroup_set_default_systemd_cgroup()
+    Cgroup.__set_default_systemd_cgroup()
 
     # Create cgroup after setting the default systemd cgroup
     result, cause = create_cgrp(config, SYSTEMD_CGNAME)
@@ -144,11 +144,11 @@ def teardown(config):
 
     # unset the default systemd cgroup by deleting the
     # /var/run/libcgroup/systemd and calling
-    # cgroup_set_default_systemd_cgroup()
+    # __set_default_systemd_cgroup()
     if os.path.exists(SYSTEMD_DEFAULT_CGROUP_FILE):
         os.unlink(SYSTEMD_DEFAULT_CGROUP_FILE)
 
-    Cgroup.cgroup_set_default_systemd_cgroup()
+    Cgroup.__set_default_systemd_cgroup()
 
     for cgroup in reversed(CGRPS_LIST):
         cgroup.delete()