]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
wrapper.c: Store the version in struct cgroup_controller
authorTom Hromatka <tom.hromatka@oracle.com>
Wed, 15 Dec 2021 19:30:30 +0000 (19:30 +0000)
committerTom Hromatka <tom.hromatka@oracle.com>
Thu, 3 Feb 2022 21:41:38 +0000 (14:41 -0700)
Store the cgroup version in struct cgroup_controller.  Each
controller can be mounted separately, and thus the cgroup
version is applicable at the controller level.

Note that this commit requires cgroup_init() to be called by
cgconfigparser.  Storing the version in the cgroup_controller
struct requires the cg_mount_table[] to be populated.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Reviewed-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
src/libcgroup-internal.h
src/tools/cgconfig.c
src/tools/cgget.c
src/wrapper.c

index 160a189fd9cb8b9aed5cd7c5dc996d5adf2aef5e..dd390cc7a551256e37e8e5a88f842531fd717a7e 100644 (file)
@@ -101,6 +101,7 @@ struct cgroup_controller {
        struct control_value *values[CG_NV_MAX];
        struct cgroup *cgroup;
        int index;
+       enum cg_version_t version;
 };
 
 struct cgroup {
index e8bb5a2e553b4c91f70d01be8d0166afb08c7fa1..b5058db42e26a143e4755fc0420831a6aeb8c54b 100644 (file)
@@ -94,6 +94,13 @@ int main(int argc, char *argv[])
                return -1;
        }
 
+       ret = cgroup_init();
+       if (ret) {
+               fprintf(stderr, "%s: libcgroup initialization failed: %s\n",
+                       argv[0], cgroup_strerror(ret));
+               goto err;
+       }
+
        error = cgroup_string_list_init(&cfg_files, argc/2);
        if (error)
                goto err;
index e9e29fd2350e8d01ff52401e7fe44ed1eac533ff..01fae3c8714b2987fd534039533ef5a080cc0c37 100644 (file)
@@ -113,7 +113,9 @@ static int parse_a_flag(struct cgroup **cg_list[], int * const cg_list_len)
                if (!cgc) {
                        cgc = cgroup_add_controller(cg, controller.name);
                        if (!cgc) {
-                               ret = ECGCONTROLLERCREATEFAILED;
+                               fprintf(stderr, "cgget: cannot find controller '%s'\n",
+                                       controller.name);
+                               ret = ECGOTHER;
                                goto out;
                        }
                }
@@ -163,7 +165,9 @@ static int parse_r_flag(struct cgroup **cg_list[], int * const cg_list_len,
        if (!cgc) {
                cgc = cgroup_add_controller(cg, cntl_value_controller);
                if (!cgc) {
-                       ret = ECGCONTROLLERCREATEFAILED;
+                       fprintf(stderr, "cgget: cannot find controller '%s'\n",
+                               cntl_value_controller);
+                       ret = ECGOTHER;
                        goto out;
                }
        }
@@ -204,7 +208,9 @@ static int parse_g_flag_no_colon(struct cgroup **cg_list[],
        if (!cgc) {
                cgc = cgroup_add_controller(cg, ctrl_str);
                if (!cgc) {
-                       ret = ECGCONTROLLERCREATEFAILED;
+                       fprintf(stderr, "cgget: cannot find controller '%s'\n",
+                               ctrl_str);
+                       ret = ECGOTHER;
                        goto out;
                }
        }
@@ -296,7 +302,9 @@ static int parse_g_flag_with_colon(struct cgroup **cg_list[],
                if (!cgc) {
                        cgc = cgroup_add_controller(cg, controllers[i]);
                        if (!cgc) {
-                               ret = ECGCONTROLLERCREATEFAILED;
+                               fprintf(stderr, "cgget: cannot find controller '%s'\n",
+                                       controllers[i]);
+                               ret = ECGOTHER;
                                goto out;
                        }
                }
index 98ebcc266c41f168b6865676d349499c7ff9e652..2b2876565a68b2089d3c89e3093fc68ae6b62880 100644 (file)
@@ -56,7 +56,7 @@ struct cgroup *cgroup_new_cgroup(const char *name)
 struct cgroup_controller *cgroup_add_controller(struct cgroup *cgroup,
                                                        const char *name)
 {
-       int i;
+       int i, ret;
        struct cgroup_controller *controller;
 
        if (!cgroup)
@@ -86,6 +86,23 @@ struct cgroup_controller *cgroup_add_controller(struct cgroup *cgroup,
        controller->cgroup = cgroup;
        controller->index = 0;
 
+       if (strcmp(controller->name, CGROUP_FILE_PREFIX) == 0) {
+               /*
+                * Operating on the "cgroup" controller is only allowed on
+                * cgroup v2 systems
+                */
+               controller->version = CGROUP_V2;
+       } else {
+               ret = cgroup_get_controller_version(controller->name,
+                                                   &controller->version);
+               if (ret) {
+                       cgroup_dbg("failed to get cgroup version for controller %s\n",
+                                  controller->name);
+                       free(controller);
+                       return NULL;
+               }
+       }
+
        cgroup->controller[cgroup->index] = controller;
        cgroup->index++;