From: Tom Hromatka Date: Tue, 20 Dec 2022 22:35:54 +0000 (+0000) Subject: api: Add functions for iterating through the cgroup struct X-Git-Tag: v3.1.0~253 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9ddd11f745763de99e6385044c01d5065fd969a5;p=thirdparty%2Flibcgroup.git api: Add functions for iterating through the cgroup struct Add functions to iterate through the cgroup structure: cgroup_get_controller_count() - returns the number of controllers within the cgroup struct cgroup_get_controller_by_index() - returns the cgroup_controller pointer corresponding with the requested index cgroup_get_controller_name() - given a cgroup_controller pointer, get the controller's name Signed-off-by: Tom Hromatka Reviewed-by: Kamalesh Babulal --- diff --git a/include/libcgroup/groups.h b/include/libcgroup/groups.h index dc8479a6..779d21a1 100644 --- a/include/libcgroup/groups.h +++ b/include/libcgroup/groups.h @@ -650,6 +650,31 @@ int cgroup_get_controller_version(const char * const controller, */ enum cg_setup_mode_t cgroup_setup_mode(void); +/** + * Return the number of controllers for the specified cgroup in libcgroup + * internal structures. + * + * @param cgroup + * @return Count of the controllers or -1 on error. + */ +int cgroup_get_controller_count(struct cgroup *cgroup); + +/** + * Return requested controller from given group + * + * @param cgroup + * @param index The index into the cgroup controller list + */ +struct cgroup_controller *cgroup_get_controller_by_index(struct cgroup *cgroup, int index); + +/** + * Given a controller pointer, get the name of the controller + * + * @param controller + * @return controller name string, NULL if there's an error + */ +char *cgroup_get_controller_name(struct cgroup_controller *controller); + /** * @} * @} diff --git a/src/api.c b/src/api.c index 1932832c..484b46ab 100644 --- a/src/api.c +++ b/src/api.c @@ -6177,3 +6177,30 @@ out: pthread_rwlock_unlock(&cg_mount_table_lock); return setup_mode; } + +int cgroup_get_controller_count(struct cgroup *cgroup) +{ + if (!cgroup) + return -1; + + return cgroup->index; +} + +struct cgroup_controller *cgroup_get_controller_by_index(struct cgroup *cgroup, int index) +{ + if (!cgroup) + return NULL; + + if (index >= cgroup->index) + return NULL; + + return cgroup->controller[index]; +} + +char *cgroup_get_controller_name(struct cgroup_controller *controller) +{ + if (!controller) + return NULL; + + return controller->name; +} diff --git a/src/libcgroup.map b/src/libcgroup.map index 0d582982..ce6c0546 100644 --- a/src/libcgroup.map +++ b/src/libcgroup.map @@ -151,4 +151,7 @@ CGROUP_3.0 { cgroup_setup_mode; cgroup_create_scope; cgroup_set_default_scope_opts; + cgroup_get_controller_count; + cgroup_get_controller_by_index; + cgroup_get_controller_name; } CGROUP_2.0;