[LTP] [PATCH] lib: rewrite cgroup_find_ctrl with using for_each_ctrl

Li Wang liwang@redhat.com
Thu Jan 13 13:34:18 CET 2022


It is safe to start from controllers[0] to traverse each of
the controller whatever V2 or V1, then we can make use of it
in the cgroup_find_ctrl() function.

Signed-off-by: Li Wang <liwang@redhat.com>
---
 lib/tst_cgroup.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)

diff --git a/lib/tst_cgroup.c b/lib/tst_cgroup.c
index 2ef599d9e..10b65364b 100644
--- a/lib/tst_cgroup.c
+++ b/lib/tst_cgroup.c
@@ -214,7 +214,7 @@ static const char *cgroup_v2_ltp_mount = "unified";
 #define for_each_v1_root(r)			\
 	for ((r) = roots + 1; (r)->ver; (r)++)
 #define for_each_ctrl(ctrl)			\
-	for ((ctrl) = controllers + 1; (ctrl)->ctrl_name; (ctrl)++)
+	for ((ctrl) = controllers; (ctrl)->ctrl_name; (ctrl)++)
 
 /* In all cases except one, this only loops once.
  *
@@ -325,15 +325,14 @@ void tst_cgroup_print_config(void)
 __attribute__ ((nonnull, warn_unused_result))
 static struct cgroup_ctrl *cgroup_find_ctrl(const char *const ctrl_name)
 {
-	struct cgroup_ctrl *ctrl = controllers;
-
-	while (ctrl->ctrl_name && strcmp(ctrl_name, ctrl->ctrl_name))
-		ctrl++;
+	struct cgroup_ctrl *ctrl;
 
-	if (!ctrl->ctrl_name)
-		ctrl = NULL;
+	for_each_ctrl(ctrl) {
+		if (!strcmp(ctrl_name, ctrl->ctrl_name))
+			return ctrl;
+	}
 
-	return ctrl;
+	return NULL;
 }
 
 /* Determine if a mounted cgroup hierarchy is unique and record it if so.
-- 
2.31.1



More information about the ltp mailing list