From df47c8c9528d25489d54f7fa5c0b4855e474285a Mon Sep 17 00:00:00 2001 From: Tom Hromatka Date: Thu, 16 Jul 2020 13:36:48 -0600 Subject: [PATCH] api.c: Add function to get the cgroup version of a controller This commit adds cgroup_get_controller_version() which returns which version (cgroup v1 or cgroup v2) of a controller is mounted. Signed-off-by: Tom Hromatka --- src/api.c | 19 +++++++++++++++++++ src/libcgroup-internal.h | 10 ++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/api.c b/src/api.c index b786dc61..c4870046 100644 --- a/src/api.c +++ b/src/api.c @@ -5306,4 +5306,23 @@ int cgroup_get_subsys_mount_point_end(void **handle) return 0; } +int cgroup_get_controller_version(const char * const controller, + enum cg_version_t * const version) +{ + int i; + + if (!version) + return ECGINVAL; + + *version = CGROUP_UNK; + + for (i = 0; cg_mount_table[i].name[0] != '\0'; i++) { + if (strncmp(cg_mount_table[i].name, controller, + sizeof(cg_mount_table[i].name)) == 0) { + *version = cg_mount_table[i].version; + return 0; + } + } + return ECGROUPNOTEXIST; +} diff --git a/src/libcgroup-internal.h b/src/libcgroup-internal.h index 8c6b36c2..57c870a8 100644 --- a/src/libcgroup-internal.h +++ b/src/libcgroup-internal.h @@ -114,6 +114,7 @@ struct cg_mount_point { }; enum cg_version_t { + CGROUP_UNK = 0, CGROUP_V1, CGROUP_V2, }; @@ -297,6 +298,15 @@ extern void cgroup_dictionary_iterator_end(void **handle); */ int cg_chmod_path(const char *path, mode_t mode, int owner_is_umask); +/** + * Get the cgroup version of a controller. Version is set to CGROUP_UNK + * if the version cannot be determined. + * + * @param controller The controller of interest + * @param version The version of the controller + */ +int cgroup_get_controller_version(const char * const controller, + enum cg_version_t * const version); /** * Functions that are defined as STATIC can be placed within the UNIT_TEST * ifdef. This will allow them to be included in the unit tests while -- 2.47.2