]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
api.c: Add function to get the cgroup version of a controller
authorTom Hromatka <tom.hromatka@oracle.com>
Thu, 16 Jul 2020 19:36:48 +0000 (13:36 -0600)
committerTom Hromatka <tom.hromatka@oracle.com>
Thu, 16 Jul 2020 20:54:02 +0000 (14:54 -0600)
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 <tom.hromatka@oracle.com>
src/api.c
src/libcgroup-internal.h

index b786dc61400546c49d64b1f0f5d21b2457873a3d..c4870046e8c24e4d4fd27e7819d874c444256f5f 100644 (file)
--- 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;
+}
index 8c6b36c299662b6cab99bdae24eca89fa39f2a55..57c870a8c41f91cd89e1a22c4c4febf3c179b63d 100644 (file)
@@ -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