]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
wrapper: Make cgroup_compare_cgroup() order agnostic
authorTom Hromatka <tom.hromatka@oracle.com>
Tue, 7 Mar 2023 22:34:34 +0000 (15:34 -0700)
committerTom Hromatka <tom.hromatka@oracle.com>
Mon, 3 Apr 2023 14:53:01 +0000 (08:53 -0600)
cgroup_compare_cgroup() can be used to compare two struct cgroup
instances.  Improve its comparison algorithm so that it can successfully
identify cgroups as the same if the only difference is the order of the
controllers stored within them.

With this change, the following two cgroups would be identified as
equal.

cgroup_a
name = foo
controller[0] = memory
controller[1] = cpu

cgroup_b
name = foo
controller[0] = cpu
controller[1] = memory

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
(cherry picked from commit 50222392d930e7be15231d0cfde9072eea172242)

src/wrapper.c

index a20bf361a5f598987ceb397e068ed15a8ea7241c..7301ab8664d93e754941880acccd6dcbb4040963 100644 (file)
@@ -415,7 +415,7 @@ int cgroup_compare_controllers(struct cgroup_controller *cgca, struct cgroup_con
 
 int cgroup_compare_cgroup(struct cgroup *cgroup_a, struct cgroup *cgroup_b)
 {
-       int i;
+       int i, j;
 
        if (!cgroup_a || !cgroup_b)
                return ECGINVAL;
@@ -440,9 +440,22 @@ int cgroup_compare_cgroup(struct cgroup *cgroup_a, struct cgroup *cgroup_b)
 
        for (i = 0; i < cgroup_a->index; i++) {
                struct cgroup_controller *cgca = cgroup_a->controller[i];
-               struct cgroup_controller *cgcb = cgroup_b->controller[i];
+               bool found_match = false;
+
+               /*
+                * Don't penalize the user if the controllers are in different order
+                * from cgroup_a to cgroup_b
+                */
+               for (j = 0; j < cgroup_b->index; j++) {
+                       struct cgroup_controller *cgcb = cgroup_b->controller[j];
+
+                       if (cgroup_compare_controllers(cgca, cgcb) == 0) {
+                               found_match = true;
+                               break;
+                       }
+               }
 
-               if (cgroup_compare_controllers(cgca, cgcb))
+               if (!found_match)
                        return ECGCONTROLLERNOTEQUAL;
        }