[LTP] [PATCH v3 4/5] API/cgroup: Make tst_cgroup_group_mk sprintf like

Richard Palethorpe rpalethorpe@suse.com
Mon Feb 7 15:16:17 CET 2022


Allows the name to be formatted which is trivial because we already
copy it into a buffer. Also this removes the init function which is
now just unnecessary verbiage.

Signed-off-by: Richard Palethorpe <rpalethorpe@suse.com>
Suggested-by: Cyril Hrubis <chrubis@suse.cz>
---
 include/tst_cgroup.h                          |  5 +--
 lib/tst_cgroup.c                              | 31 ++++++++-----------
 .../sched/cfs-scheduler/cfs_bandwidth01.c     |  9 +++---
 3 files changed, 21 insertions(+), 24 deletions(-)

diff --git a/include/tst_cgroup.h b/include/tst_cgroup.h
index 83bdb4b0d..39aa95a88 100644
--- a/include/tst_cgroup.h
+++ b/include/tst_cgroup.h
@@ -132,8 +132,9 @@ void tst_cgroup_init(void);
 /* Create a descendant CGroup */
 struct tst_cgroup_group *
 tst_cgroup_group_mk(const struct tst_cgroup_group *const parent,
-		    const char *const group_name)
-		    __attribute__ ((nonnull, warn_unused_result));
+		    const char *const group_name_fmt, ...)
+	    __attribute__ ((nonnull, warn_unused_result, format (printf, 2, 3)));
+
 const char *
 tst_cgroup_group_name(const struct tst_cgroup_group *const cg)
 		      __attribute__ ((nonnull, warn_unused_result));
diff --git a/lib/tst_cgroup.c b/lib/tst_cgroup.c
index bb379caf5..93d374c56 100644
--- a/lib/tst_cgroup.c
+++ b/lib/tst_cgroup.c
@@ -834,21 +834,6 @@ clear_data:
 	memset(roots, 0, sizeof(roots));
 }
 
-__attribute__((nonnull(1)))
-static void cgroup_group_init(struct tst_cgroup_group *const cg,
-			      const char *const group_name)
-{
-	memset(cg, 0, sizeof(*cg));
-
-	if (!group_name)
-		return;
-
-	if (strlen(group_name) > NAME_MAX)
-		tst_brk(TBROK, "Group name is too long");
-
-	strcpy(cg->group_name, group_name);
-}
-
 __attribute__((nonnull(2, 3)))
 static void cgroup_group_add_dir(const struct tst_cgroup_group *const parent,
 				 struct tst_cgroup_group *const cg,
@@ -880,18 +865,28 @@ static void cgroup_group_add_dir(const struct tst_cgroup_group *const parent,
 
 struct tst_cgroup_group *
 tst_cgroup_group_mk(const struct tst_cgroup_group *const parent,
-		    const char *const group_name)
+		    const char *const group_name_fmt, ...)
 {
 	struct tst_cgroup_group *cg;
 	struct cgroup_dir *const *dir;
 	struct cgroup_dir *new_dir;
+	va_list ap;
+	size_t name_len;
 
 	cg = SAFE_MALLOC(sizeof(*cg));
-	cgroup_group_init(cg, group_name);
+	memset(cg, 0, sizeof(*cg));
+
+	va_start(ap, group_name_fmt);
+	name_len = vsnprintf(cg->group_name, NAME_MAX,
+			     group_name_fmt, ap);
+	va_end(ap);
+
+	if (name_len >= NAME_MAX)
+		tst_brk(TBROK, "CGroup name is too long");
 
 	for_each_dir(parent, 0, dir) {
 		new_dir = SAFE_MALLOC(sizeof(*new_dir));
-		cgroup_dir_mk(*dir, group_name, new_dir);
+		cgroup_dir_mk(*dir, cg->group_name, new_dir);
 		cgroup_group_add_dir(parent, cg, new_dir);
 	}
 
diff --git a/testcases/kernel/sched/cfs-scheduler/cfs_bandwidth01.c b/testcases/kernel/sched/cfs-scheduler/cfs_bandwidth01.c
index aae1e321b..2c28b83a2 100644
--- a/testcases/kernel/sched/cfs-scheduler/cfs_bandwidth01.c
+++ b/testcases/kernel/sched/cfs-scheduler/cfs_bandwidth01.c
@@ -56,11 +56,12 @@ static void set_cpu_quota(const struct tst_cgroup_group *const cg,
 }
 
 static void mk_cpu_cgroup(struct tst_cgroup_group **cg,
-		const struct tst_cgroup_group *const cg_parent,
-		const char *const cg_child_name,
-		const float quota_percent)
+			  const struct tst_cgroup_group *const cg_parent,
+			  const char *const cg_child_name,
+			  const float quota_percent)
+
 {
-	*cg = tst_cgroup_group_mk(cg_parent, cg_child_name);
+	*cg = tst_cgroup_group_mk(cg_parent, "%s", cg_child_name);
 
 	set_cpu_quota(*cg, quota_percent);
 }
-- 
2.34.1



More information about the ltp mailing list