]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
api: cgroup_get_cgroup() populate the controller list based on the subtree_control...
authorTom Hromatka <tom.hromatka@oracle.com>
Tue, 20 Dec 2022 22:33:23 +0000 (22:33 +0000)
committerTom Hromatka <tom.hromatka@oracle.com>
Fri, 6 Jan 2023 15:07:34 +0000 (08:07 -0700)
In cgroup_get_cgroup(), populate the controller list in the cgroup
only with controllers that are enabled in cgroup.subtree_control.

As part of this change update the error codes from
cgroupv2_get_subtree_control() to better reflect memory allocation
errors

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

index 5480c7a8fb748ded667a61d1bacbb757eb9c21af..1932832ce8640abee636daa9e2251b32e26c1716 100644 (file)
--- a/src/api.c
+++ b/src/api.c
@@ -2277,17 +2277,22 @@ STATIC int cgroupv2_get_subtree_control(const char *path, const char *ctrl_name,
        *enabled = false;
 
        path_copy = (char *)malloc(FILENAME_MAX);
-       if (!path_copy)
+       if (!path_copy) {
+               error = ECGOTHER;
                goto out;
+       }
 
        ret = snprintf(path_copy, FILENAME_MAX, "%s/%s", path, CGV2_SUBTREE_CTRL_FILE);
-       if (ret < 0)
+       if (ret < 0) {
+               error = ECGOTHER;
                goto out;
+       }
 
        fp = fopen(path_copy, "re");
        if (!fp) {
                cgroup_warn("fopen failed\n");
                last_errno = errno;
+               error = ECGOTHER;
                goto out;
        }
 
@@ -3506,6 +3511,18 @@ int cgroup_get_cgroup(struct cgroup *cgroup)
                        cgroup->tasks_gid = stat_buffer.st_gid;
 
                        free(control_path);
+               } else { /* cgroup v2 */
+                       bool enabled;
+
+                       error = cgroupv2_get_subtree_control(path, cg_mount_table[i].name,
+                                                            &enabled);
+                       if (error == ECGROUPNOTMOUNTED)
+                               continue;
+                       if (error)
+                               goto unlock_error;
+
+                       if (!enabled)
+                               continue;
                }
 
                cgc = cgroup_add_controller(cgroup, cg_mount_table[i].name);