From: Tom Hromatka Date: Tue, 1 Aug 2023 21:17:35 +0000 (-0600) Subject: python: Surround systemd code with m4 conditionals X-Git-Tag: v3.2.0~248 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a9d27eda52f25514688d434c4a13deac51a16cc4;p=thirdparty%2Flibcgroup.git python: Surround systemd code with m4 conditionals Surround the systemd python logic with m4 ifdef(`WITH_SYSTEMD'...). This will conditionally add the systemd code iff systemd is enabled in configure.ac Signed-off-by: Tom Hromatka --- diff --git a/src/python/.gitignore b/src/python/.gitignore index 8862a139..b4367633 100644 --- a/src/python/.gitignore +++ b/src/python/.gitignore @@ -1,4 +1,7 @@ build/ +libcgroup.pyx +cgroup.pxd +setup.py Makefile Makefile.in __init__.py diff --git a/src/python/Makefile.am b/src/python/Makefile.am index 71092d5c..09357a1a 100644 --- a/src/python/Makefile.am +++ b/src/python/Makefile.am @@ -22,12 +22,27 @@ PY_BUILD = ${PY_BUILD_@AM_V@} PY_INSTALL = ${PY_DISTUTILS} install -EXTRA_DIST = cgroup.pxd libcgroup.pyx setup.py +EXTRA_DIST = cgroup.pxd.m4 libcgroup.pyx.m4 setup.py.m4 \ + cgroup.pxd libcgroup.pyx setup.py + +if WITH_SYSTEMD +M4_FLAGS = -D WITH_SYSTEMD +else +M4_FLAGS = -U WITH_SYSTEMD +endif all-local: build -build: ../libcgroup.la cgroup.pxd libcgroup.pyx setup.py - [ ${srcdir} == ${builddir} ] || cp ${srcdir}/libcgroup.pyx ${builddir} +buildm4: + m4 $(M4_FLAGS) ${srcdir}/cgroup.pxd.m4 > ${builddir}/cgroup.pxd + m4 $(M4_FLAGS) ${srcdir}/libcgroup.pyx.m4 > ${builddir}/libcgroup.pyx + m4 $(M4_FLAGS) ${srcdir}/setup.py.m4 > ${builddir}/setup.py + +cgroup.pxd: cgroup.pxd.m4 buildm4 +libcgroup.pyx: libcgroup.pyx.m4 buildm4 +setup.py: setup.py.m4 buildm4 + +build: ../libcgroup.la buildm4 ${PY_BUILD} && touch build install-exec-local: build @@ -39,5 +54,7 @@ uninstall-local: ${RM} -f ${DESTDIR}/${pyexecdir}/install_files.txt clean-local: - [ ${srcdir} == ${builddir} ] || ${RM} -f ${builddir}/libcgroup.pyx + ${RM} -f ${builddir}/libcgroup.pyx + ${RM} -f ${builddir}/cgroup.pxd + ${RM} -f ${builddir}/setup.py ${RM} -rf libcgroup.c build libcgroup.egg-info dist diff --git a/src/python/cgroup.pxd b/src/python/cgroup.pxd.m4 similarity index 96% rename from src/python/cgroup.pxd rename to src/python/cgroup.pxd.m4 index e591876a..78ed5934 100644 --- a/src/python/cgroup.pxd +++ b/src/python/cgroup.pxd.m4 @@ -35,6 +35,8 @@ cdef extern from "libcgroup.h": unsigned int minor unsigned int release +ifdef(`WITH_SYSTEMD', + # comment to appease m4 cdef enum cgroup_systemd_mode_t: CGROUP_SYSTEMD_MODE_FAIL CGROUP_SYSTEMD_MODE_REPLACE @@ -46,6 +48,7 @@ cdef extern from "libcgroup.h": int delegated cgroup_systemd_mode_t mode pid_t pid +) cdef enum cgroup_log_level: CGROUP_LOG_CONT @@ -85,8 +88,11 @@ cdef extern from "libcgroup.h": cg_setup_mode_t cgroup_setup_mode() +ifdef(`WITH_SYSTEMD', + # comment to appease m4 int cgroup_create_scope(const char * const scope_name, const char * const slice_name, const cgroup_systemd_scope_opts * const opts) +) int cgroup_get_cgroup(cgroup *cg) @@ -107,12 +113,15 @@ cdef extern from "libcgroup.h": void cgroup_set_permissions(cgroup *cgroup, mode_t control_dperm, mode_t control_fperm, mode_t task_fperm) +ifdef(`WITH_SYSTEMD', + # comment to appease m4 int cgroup_create_scope2(cgroup *cgroup, int ignore_ownership, const cgroup_systemd_scope_opts * const opts) 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.m4 similarity index 91% rename from src/python/libcgroup.pyx rename to src/python/libcgroup.pyx.m4 index 407963ab..295fc088 100644 --- a/src/python/libcgroup.pyx +++ b/src/python/libcgroup.pyx.m4 @@ -33,12 +33,14 @@ cdef class Mode: CGROUP_MODE_HYBRID = cgroup.CGROUP_MODE_HYBRID CGROUP_MODE_UNIFIED = cgroup.CGROUP_MODE_UNIFIED +ifdef(`WITH_SYSTEMD', 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 +) cdef class LogLevel: CGROUP_LOG_CONT = cgroup.CGROUP_LOG_CONT @@ -65,10 +67,10 @@ class Controller: self.settings = dict() def __str__(self): - out_str = "Controller {}\n".format(self.name) + out_str = "Controller {}\n".`format'(self.name) for setting_key in self.settings: - out_str += indent("{} = {}\n".format(setting_key, + out_str += indent("{} = {}\n".`format'(setting_key, self.settings[setting_key]), 4) return out_str @@ -96,14 +98,14 @@ cdef class Cgroup: def cgroup_init(): ret = cgroup.cgroup_init() if ret != 0: - raise RuntimeError("Failed to initialize libcgroup: {}".format(ret)) + raise RuntimeError("Failed to initialize libcgroup: {}".`format'(ret)) def __cinit__(self, name, version): Cgroup.cgroup_init() self._cgp = cgroup.cgroup_new_cgroup(c_str(name)) if self._cgp == NULL: - raise RuntimeError("Failed to create cgroup {}".format(name)) + raise RuntimeError("Failed to create cgroup {}".`format'(name)) def __init__(self, name, version): """Initialize this cgroup instance @@ -120,7 +122,7 @@ cdef class Cgroup: self.version = version def __str__(self): - out_str = "Cgroup {}\n".format(self.name) + out_str = "Cgroup {}\n".`format'(self.name) for ctrl_key in self.controllers: out_str += indent(str(self.controllers[ctrl_key]), 4) @@ -168,7 +170,7 @@ cdef class Cgroup: cgcp = cgroup.cgroup_add_controller(self._cgp, c_str(ctrl_name)) if cgcp == NULL: - raise RuntimeError("Failed to add controller {} to cgroup".format( + raise RuntimeError("Failed to add controller {} to cgroup".`format'( ctrl_name)) self.controllers[ctrl_name] = Controller(ctrl_name) @@ -220,7 +222,7 @@ cdef class Cgroup: cgcp = cgroup.cgroup_get_controller(self._cgp, c_str(ctrl_name)) if cgcp == NULL: - raise RuntimeError("Failed to get controller {}".format( + raise RuntimeError("Failed to get controller {}".`format'( ctrl_name)) if setting_value is None: @@ -230,7 +232,7 @@ cdef class Cgroup: ret = cgroup.cgroup_add_value_string(cgcp, c_str(setting_name), c_str(setting_value)) if ret != 0: - raise RuntimeError("Failed to add setting {}: {}".format( + raise RuntimeError("Failed to add setting {}: {}".`format'( setting_name, ret)) def _pythonize_cgroup(self): @@ -244,7 +246,7 @@ cdef class Cgroup: cgcp = cgroup.cgroup_get_controller(self._cgp, c_str(self.controllers[ctrlr_key].name)) if cgcp == NULL: - raise RuntimeError("Failed to get controller {}".format( + raise RuntimeError("Failed to get controller {}".`format'( ctrlr_key)) self.controllers[ctrlr_key] = Controller(ctrlr_key) @@ -256,7 +258,7 @@ cdef class Cgroup: ret = cgroup.cgroup_get_value_string(cgcp, setting_name, &setting_value) if ret != 0: - raise RuntimeError("Failed to get value {}: {}".format( + raise RuntimeError("Failed to get value {}: {}".`format'( setting_name, ret)) name = setting_name.decode("ascii") @@ -283,7 +285,7 @@ cdef class Cgroup: ret = cgroup.cgroup_convert_cgroup(out_cgp._cgp, out_version, self._cgp, self.version) if ret != 0: - raise RuntimeError("Failed to convert cgroup: {}".format(ret)) + raise RuntimeError("Failed to convert cgroup: {}".`format'(ret)) for ctrlr_key in self.controllers: out_cgp.controllers[ctrlr_key] = Controller(ctrlr_key) @@ -320,7 +322,7 @@ cdef class Cgroup: ret = cgroup.cgroup_cgxget(&self._cgp, self.version, ignore) if ret != 0: - raise RuntimeError("cgxget failed: {}".format(ret)) + raise RuntimeError("cgxget failed: {}".`format'(ret)) self._pythonize_cgroup() @@ -344,7 +346,7 @@ cdef class Cgroup: ret = cgroup.cgroup_cgxset(self._cgp, self.version, ignore) if ret != 0: - raise RuntimeError("cgxset failed: {}".format(ret)) + raise RuntimeError("cgxset failed: {}".`format'(ret)) def create(self, ignore_ownership=True): """Write this cgroup to the cgroup sysfs @@ -358,7 +360,7 @@ cdef class Cgroup: """ ret = cgroup.cgroup_create_cgroup(self._cgp, ignore_ownership) if ret != 0: - raise RuntimeError("Failed to create cgroup: {}".format(ret)) + raise RuntimeError("Failed to create cgroup: {}".`format'(ret)) @staticmethod def mount_points(version): @@ -381,7 +383,7 @@ cdef class Cgroup: mount_points = [] ret = cgroup.cgroup_list_mount_points(version, &a) if ret is not 0: - raise RuntimeError("cgroup_list_mount_points failed: {}".format(ret)) + raise RuntimeError("cgroup_list_mount_points failed: {}".`format'(ret)) i = 0 while a[i]: @@ -399,6 +401,8 @@ cdef class Cgroup: Cgroup.cgroup_init() return cgroup.cgroup_setup_mode() +ifdef(`WITH_SYSTEMD', + # comment to appease m4 @staticmethod def create_scope(scope_name='libcgroup.scope', slice_name='libcgroup.slice', delegated=True, systemd_mode=SystemdMode.CGROUP_SYSTEMD_MODE_FAIL, pid=None): @@ -407,15 +411,15 @@ cdef class Cgroup: 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 + 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 + 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 + 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 @@ -435,7 +439,8 @@ cdef class Cgroup: 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)) + raise RuntimeError("cgroup_create_scope failed: {}".``format''(ret)) +) def get(self): """Get the cgroup information from the cgroup sysfs @@ -447,7 +452,7 @@ cdef class Cgroup: ret = cgroup.cgroup_get_cgroup(self._cgp) if ret is not 0: - raise RuntimeError("cgroup_get_cgroup failed: {}".format(ret)) + raise RuntimeError("cgroup_get_cgroup failed: {}".`format'(ret)) ctrl_cnt = cgroup.cgroup_get_controller_count(self._cgp) for i in range(0, ctrl_cnt): @@ -468,7 +473,7 @@ cdef class Cgroup: """ ret = cgroup.cgroup_delete_cgroup(self._cgp, ignore_migration) if ret is not 0: - raise RuntimeError("cgroup_delete_cgroup failed: {}".format(ret)) + raise RuntimeError("cgroup_delete_cgroup failed: {}".`format'(ret)) def attach(self, pid=None, root_cgroup=False): """Attach a process to a cgroup @@ -492,7 +497,7 @@ cdef class Cgroup: ret = cgroup.cgroup_attach_task_pid(self._cgp, pid) if ret is not 0: - raise RuntimeError("cgroup_attach_task failed: {}".format(ret)) + raise RuntimeError("cgroup_attach_task failed: {}".`format'(ret)) def set_uid_gid(self, tasks_uid, tasks_gid, ctrl_uid, ctrl_gid): """Set the desired owning uid/gid for the tasks file and the entire cgroup hierarchy @@ -509,7 +514,7 @@ cdef class Cgroup: """ ret = cgroup.cgroup_set_uid_gid(self._cgp, tasks_uid, tasks_gid, ctrl_uid, ctrl_gid) if ret is not 0: - raise RuntimeError("cgroup_set_uid_gid failed: {}".format(ret)) + raise RuntimeError("cgroup_set_uid_gid failed: {}".`format'(ret)) def set_permissions(self, dir_mode, ctrl_mode, task_mode): """Set the permission bits on the cgroup @@ -534,22 +539,24 @@ cdef class Cgroup: cgroup.cgroup_set_permissions(self._cgp, dmode, cmode, tmode) +ifdef(`WITH_SYSTEMD', + # comment to appease m4 def create_scope2(self, ignore_ownership=True, delegated=True, systemd_mode=SystemdMode.CGROUP_SYSTEMD_MODE_FAIL, pid=None): """Create a systemd scope using the cgroup instance Arguments: - ignore_ownership - if true, do not modify the owning user/group for the cgroup directory + ignore_ownership - if true`,' do not modify the owning user/group for the cgroup directory and control files - delegated - if true, then systemd will not manage the cgroup aspects of the scope. It + 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 + 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 using the cgroup instance in this class. If delegated is true, + Create a systemd scope using the cgroup instance in this class. If delegated is true`,' then systemd will not manage the cgroup aspects of the scope. """ cdef cgroup.cgroup_systemd_scope_opts opts @@ -567,7 +574,7 @@ cdef class Cgroup: ret = cgroup.cgroup_create_scope2(self._cgp, ignore_ownership, &opts) if ret is not 0: - raise RuntimeError("cgroup_create_scope2 failed: {}".format(ret)) + raise RuntimeError("cgroup_create_scope2 failed: {}".``format''(ret)) @staticmethod def _set_default_systemd_cgroup(): @@ -577,8 +584,8 @@ cdef class Cgroup: None Description: - Reads /run/libcgroup/systemd and if the file exists, sets the - systemd_default_cgroup. Then on all the paths constructed, has + Reads /run/libcgroup/systemd and if the file exists`,' sets the + systemd_default_cgroup. Then on all the paths constructed`,' has the systemd_default_cgroup appended to it. This is used when cgroup sub-tree is constructed for systemd delegation. """ @@ -593,20 +600,20 @@ cdef class Cgroup: """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 + 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 + 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 + 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 @@ -639,6 +646,7 @@ cdef class Cgroup: Cgroup._set_default_systemd_cgroup() except RuntimeError: pass +) cdef compare(self, Cgroup other): """Compare this cgroup instance with another cgroup instance @@ -680,10 +688,10 @@ cdef class Cgroup: cdef pid_t *pids cdef int size - if len(self.controllers) == 0: + if `len'(self.controllers) == 0: ret = cgroup.cgroup_get_procs(c_str(self.name), NULL, &pids, &size) if ret is not 0: - raise RuntimeError("cgroup_get_procs failed: {}".format(ret)) + raise RuntimeError("cgroup_get_procs failed: {}".`format'(ret)) for i in range(0, size): pid_list.append(int(pids[i])) @@ -692,7 +700,7 @@ cdef class Cgroup: ret = cgroup.cgroup_get_procs(c_str(self.name), c_str(self.controllers[ctrl_key].name), &pids, &size) if ret is not 0: - raise RuntimeError("cgroup_get_procs failed: {}".format(ret)) + raise RuntimeError("cgroup_get_procs failed: {}".`format'(ret)) for i in range(0, size): pid_list.append(int(pids[i])) @@ -756,11 +764,11 @@ cdef class Cgroup: else: raise TypeError("cgroup_get_current_controller_path failed: " "expected controller type string, but passed " - "{}".format(type(controller))) + "{}".`format'(type(controller))) if ret is not 0: raise RuntimeError("cgroup_get_current_controller_path failed :" - "{}".format(ret)) + "{}".`format'(ret)) return current_path.decode('ascii') @@ -797,11 +805,11 @@ cdef class Cgroup: # # In the future we could add support for a list of controllers # - raise TypeError("Unsupported controller type: {}".format(type(controller))) + raise TypeError("Unsupported controller type: {}".`format'(type(controller))) if ret is not 0: raise RuntimeError("cgroup_change_cgroup_path failed :" - "{}".format(ret)) + "{}".`format'(ret)) @staticmethod def log_level(log_level): diff --git a/src/python/setup.py b/src/python/setup.py.m4 similarity index 73% rename from src/python/setup.py rename to src/python/setup.py.m4 index 753ad874..8183622a 100755 --- a/src/python/setup.py +++ b/src/python/setup.py.m4 @@ -22,6 +22,8 @@ setup( maintainer_email='tom.hromatka@oracle.com', license='LGPLv2.1', platforms='Linux', +ifdef(`WITH_SYSTEMD', + # comment to appease m4 ext_modules=cythonize([ Extension( 'libcgroup', ['libcgroup.pyx'], @@ -30,6 +32,15 @@ setup( libraries=['systemd'] ), ]) + , + ext_modules=cythonize([ + Extension( + 'libcgroup', ['libcgroup.pyx'], + # unable to handle libtool libraries directly + extra_objects=['../.libs/libcgroup.a'], + ), + ]) +) ) # vim: set et ts=4 sw=4: