]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
tools/cgsnapshot: limit the controller to search to user lists
authorKamalesh Babulal <kamalesh.babulal@oracle.com>
Wed, 21 Dec 2022 15:46:26 +0000 (21:16 +0530)
committerTom Hromatka <tom.hromatka@oracle.com>
Fri, 6 Jan 2023 15:07:37 +0000 (08:07 -0700)
is_ctlr_on_list(), returns on the first match of the list of controllers
the user has passed on the command line, this might serve as an
optimization but as a side effect we search through the whole list of
controllers, those the user has not asked for. Let's reset the list of
controllers to controllers available on wanted_consts array (list of
controllers required by the user) and controllers array (list of
available controllers).

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
src/tools/cgsnapshot.c

index 6d2e8c80c75d0806b047b355763b64d4da9f983d..79183db2a2d3cc17c9ce1622e1d04f4b6656549b 100644 (file)
@@ -472,13 +472,22 @@ static int display_controller_data(char controller[CG_CONTROLLER_MAX][FILENAME_M
 
                        ret = cgroup_get_cgroup(group);
                        if (ret != 0) {
-                               info("cannot read group '%s': %s\n", cgroup_name,
-                                    cgroup_strerror(ret));
-                               goto err;
+                               /*
+                                * We know for sure that the cgroup exists
+                                * but just that the cgroup v2 controller
+                                * is not enabled in the cgroup.subtree_control
+                                * file.
+                                */
+                               if (ret != ECGROUPNOTEXIST) {
+                                       info("cannot read group '%s': %s %d\n", cgroup_name,
+                                            cgroup_strerror(ret), ret);
+                                       goto err;
+                               }
                        }
 
-                       display_cgroup_data(group, controller, info.full_path, prefix_len, first,
-                                           program_name);
+                       if (ret == 0)
+                               display_cgroup_data(group, controller, info.full_path, prefix_len,
+                                                   first, program_name);
                        first = 0;
                        cgroup_free(&group);
                }
@@ -495,20 +504,34 @@ err:
 static int is_ctlr_on_list(char controllers[CG_CONTROLLER_MAX][FILENAME_MAX],
                           cont_name_t wanted_conts[CG_CONTROLLER_MAX])
 {
-       int i = 0;
-       int j = 0;
+       char tmp_controllers[CG_CONTROLLER_MAX][FILENAME_MAX];
+       int i = 0, j = 0, k = 0;
+       int ret = 0;
 
        while (controllers[i][0] != '\0') {
                while (wanted_conts[j][0] != '\0') {
-                       if (strcmp(controllers[i], wanted_conts[j]) == 0)
-                               return 1;
+                       if (strcmp(controllers[i], wanted_conts[j]) == 0) {
+                               strncpy(tmp_controllers[k], wanted_conts[j], FILENAME_MAX - 1);
+                               (tmp_controllers[k])[FILENAME_MAX - 1] = '\0';
+                               k++;
+                       }
                        j++;
                }
                j = 0;
                i++;
        }
 
-       return 0;
+       (tmp_controllers[k])[0] = '\0';
+
+       /* Lets reset the controllers to intersection of controller ∩ wanted_conts */
+       for (i = 0; tmp_controllers[i][0] != '\0'; i++) {
+               strncpy(controllers[i], tmp_controllers[i], FILENAME_MAX - 1);
+               (controllers[i])[FILENAME_MAX - 1] = '\0';
+               ret = 1;
+       }
+       (controllers[i])[0] = '\0';
+
+       return ret;
 }