From: Kamalesh Babulal Date: Mon, 4 Apr 2022 06:48:46 +0000 (+0530) Subject: api.c: add support to attach tid to an empty cgroup v2 X-Git-Tag: v3.0~90 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fb8a49dc5551165716ff368df593a91b4f422e0d;p=thirdparty%2Flibcgroup.git api.c: add support to attach tid to an empty cgroup v2 Currently, cgroup_attach_task_pid() checks for enabled controllers before attaching the given tid to it, but in the case of empty cgroup, it simply ignores the attaching due to no controllers available, a.k.a. cgroup->index is 0. Add support to recognize empty controller cgroups. Fixes: https://github.com/libcgroup/libcgroup/issues/129 Signed-off-by: Kamalesh Babulal Signed-off-by: Tom Hromatka --- diff --git a/src/api.c b/src/api.c index f5b6d303..7f244315 100644 --- a/src/api.c +++ b/src/api.c @@ -1858,6 +1858,8 @@ err: int cgroup_attach_task_pid(struct cgroup *cgroup, pid_t tid) { char path[FILENAME_MAX] = {0}; + char *controller_name; + int empty_cgroup = 0; int i, ret = 0; if (!cgroup_initialized) { @@ -1896,15 +1898,24 @@ int cgroup_attach_task_pid(struct cgroup *cgroup, pid_t tid) } } - for (i = 0; i < cgroup->index; i++) { + if (cgroup->index == 0) + /* Valid empty cgroup v2 with no controllers added. */ + empty_cgroup = 1; + + for (i = 0, controller_name = NULL; + empty_cgroup > 0 || i < cgroup->index; + i++, empty_cgroup--) { + + if (cgroup->controller[i]) + controller_name = cgroup->controller[i]->name; + ret = cgroupv2_controller_enabled(cgroup->name, - cgroup->controller[i]->name); + controller_name); if (ret) return ret; ret = cgroup_build_tasks_procs_path(path, - sizeof(path), cgroup->name, - cgroup->controller[i]->name); + sizeof(path), cgroup->name, controller_name); if (ret) return ret;