]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
api.c: add support to attach tid to an empty cgroup v2
authorKamalesh Babulal <kamalesh.babulal@oracle.com>
Mon, 4 Apr 2022 06:48:46 +0000 (12:18 +0530)
committerTom Hromatka <tom.hromatka@oracle.com>
Wed, 20 Apr 2022 15:23:33 +0000 (09:23 -0600)
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 <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
src/api.c

index f5b6d303bf955b19a6e272eab8c9968652c54cf5..7f2443156204ebb837cdb61c43a9a25e4065f1c1 100644 (file)
--- 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;