From: Tom Hromatka Date: Tue, 20 Dec 2022 22:33:23 +0000 (+0000) Subject: api: cgroup_get_cgroup() populate the controller list based on the subtree_control... X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=52f20c4567ea45157112ad765b94f072cf62183f;p=thirdparty%2Flibcgroup.git api: cgroup_get_cgroup() populate the controller list based on the subtree_control file 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 Reviewed-by: Kamalesh Babulal --- diff --git a/src/api.c b/src/api.c index 5480c7a8..1932832c 100644 --- 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);