From: Tom Hromatka Date: Tue, 7 Mar 2023 22:34:34 +0000 (-0700) Subject: wrapper: Make cgroup_compare_cgroup() order agnostic X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b722ac6e0cd61d0f4c31c5318b5f215be9787f6b;p=thirdparty%2Flibcgroup.git wrapper: Make cgroup_compare_cgroup() order agnostic 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 (cherry picked from commit 50222392d930e7be15231d0cfde9072eea172242) --- diff --git a/src/wrapper.c b/src/wrapper.c index a20bf361..7301ab86 100644 --- a/src/wrapper.c +++ b/src/wrapper.c @@ -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; }