From: Tom Hromatka Date: Wed, 26 Oct 2022 16:17:36 +0000 (-0600) Subject: python: systemd: Add python bindings to create a systemd scope X-Git-Tag: v3.1.0~272 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=08b940e3e8cfb0f0d758ec3114d5bd72df976432;p=thirdparty%2Flibcgroup.git python: systemd: Add python bindings to create a systemd scope Add python bindings to create a systemd scope. Signed-off-by: Tom Hromatka Reviewed-by: Kamalesh Babulal --- diff --git a/src/python/cgroup.pxd b/src/python/cgroup.pxd index 4f85746e..0723e01a 100644 --- a/src/python/cgroup.pxd +++ b/src/python/cgroup.pxd @@ -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: diff --git a/src/python/libcgroup.pyx b/src/python/libcgroup.pyx index 6b4ff386..539a6df6 100644 --- a/src/python/libcgroup.pyx +++ b/src/python/libcgroup.pyx @@ -14,6 +14,7 @@ __author__ = 'Tom Hromatka ' __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); diff --git a/src/python/setup.py b/src/python/setup.py index 8bb4d6a5..753ad874 100755 --- a/src/python/setup.py +++ b/src/python/setup.py @@ -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'] ), ]) )