From: Kamalesh Babulal Date: Thu, 27 Apr 2023 11:09:05 +0000 (+0000) Subject: python: Add python bindings for get_current_controller_path() X-Git-Tag: v3.1.0~85 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=73ff9397600d24b1ae04cbf0f7d0487adc1e0d4b;p=thirdparty%2Flibcgroup.git python: Add python bindings for get_current_controller_path() Add python bindings for get_current_controller_path() Signed-off-by: Kamalesh Babulal Signed-off-by: Tom Hromatka --- diff --git a/src/python/cgroup.pxd b/src/python/cgroup.pxd index a27088e5..7f011de2 100644 --- a/src/python/cgroup.pxd +++ b/src/python/cgroup.pxd @@ -115,4 +115,6 @@ cdef extern from "libcgroup.h": bool is_cgroup_mode_unified() + int cgroup_get_current_controller_path(pid_t pid, const char *controller, + char **current_path) # vim: set et ts=4 sw=4: diff --git a/src/python/libcgroup.pyx b/src/python/libcgroup.pyx index d7b0d42f..bab157e2 100644 --- a/src/python/libcgroup.pyx +++ b/src/python/libcgroup.pyx @@ -654,6 +654,41 @@ cdef class Cgroup: """ return cgroup.is_cgroup_mode_unified() + @staticmethod + def get_current_controller_path(pid, controller=None): + """Get the cgroup path of pid, under controller hierarchy + + Return: + Return cgroup path relative to mount point + + Description: + Invokes the libcgroup C function, cgroup_get_current_controller_path(). + It parses the /proc//cgroup file and returns the cgroup path, if + the controller matches in the output for cgroup v1 controllers and for + the cgroup v2 controllers, checks if the cgroup.controllers file has + the controller enabled. + """ + cdef char *current_path + + Cgroup.cgroup_init() + + if controller is None: + ret = cgroup.cgroup_get_current_controller_path(pid, NULL, + ¤t_path) + elif isinstance(controller, str): + ret = cgroup.cgroup_get_current_controller_path(pid, + c_str(controller), ¤t_path) + else: + raise TypeError("cgroup_get_current_controller_path failed: " + "expected controller type string, but passed " + "{}".format(type(controller))) + + if ret is not 0: + raise RuntimeError("cgroup_get_current_controller_path failed :" + "{}".format(ret)) + + return current_path.decode('ascii') + def __dealloc__(self): cgroup.cgroup_free(&self._cgp);