From 49032fc2895f3b7b772054eb222cda8f4f9d29bf Mon Sep 17 00:00:00 2001 From: Tom Hromatka Date: Fri, 19 May 2023 16:05:25 -0600 Subject: [PATCH] python: Add bindings for cgroup_write_systemd_default_cgroup() 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 Reviewed-by: Kamalesh Babulal --- src/python/cgroup.pxd | 4 +- src/python/libcgroup.pyx | 40 ++++++++++++++++++- .../071-sudo-set_default_systemd_cgroup.py | 6 +-- 3 files changed, 44 insertions(+), 6 deletions(-) diff --git a/src/python/cgroup.pxd b/src/python/cgroup.pxd index 31d9ad4a..e591876a 100644 --- a/src/python/cgroup.pxd +++ b/src/python/cgroup.pxd @@ -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) diff --git a/src/python/libcgroup.pyx b/src/python/libcgroup.pyx index ded55a3f..e84df01f 100644 --- a/src/python/libcgroup.pyx +++ b/src/python/libcgroup.pyx @@ -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 diff --git a/tests/ftests/071-sudo-set_default_systemd_cgroup.py b/tests/ftests/071-sudo-set_default_systemd_cgroup.py index 4d67d9ba..29c822e6 100755 --- a/tests/ftests/071-sudo-set_default_systemd_cgroup.py +++ b/tests/ftests/071-sudo-set_default_systemd_cgroup.py @@ -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() -- 2.47.2