Skip to contentSkip to conversation

tst_cgroup: use tmpdir

PATCH In review Edward Liaw via ltp 3 participants 3 messages Oct 27, 2022 – Oct 31, 2022 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tst_cgroup: use tmpdir

Use tmpdir instead of hardcoded /tmp path.

---
 lib/tst_cgroup.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
lib/tst_cgroup.c +11 -3
index 6c015e4f8..458b823f2 100644
@@ -290,7 +290,7 @@ static struct cgroup_ctrl controllers[] = {
 static const char *cgroup_ltp_dir = "ltp";
 static const char *cgroup_ltp_drain_dir = "drain";
 static char cgroup_test_dir[NAME_MAX + 1];
-static const char *cgroup_mount_ltp_prefix = "/tmp/cgroup_";
+static const char *cgroup_mount_ltp_prefix = "cgroup_";
 static const char *cgroup_v2_ltp_mount = "unified";
 
 #define first_root				\
@@ -645,8 +645,12 @@ static void cgroup_mount_v2(void)
 {
 	int ret;
 	char mnt_path[PATH_MAX];
+	const char *tmpdir = getenv("TMPDIR");
+	if (!tmpdir)
+		tmpdir = "/tmp";
 
-	sprintf(mnt_path, "%s%s", cgroup_mount_ltp_prefix, cgroup_v2_ltp_mount);
+	sprintf(mnt_path, "%s/%s%s",
+		tmpdir, cgroup_mount_ltp_prefix, cgroup_v2_ltp_mount);
 
 	if (!mkdir(mnt_path, 0777)) {
 		roots[0].mnt_dir.we_created_it = 1;
@@ -693,6 +697,9 @@ static void cgroup_mount_v1(struct cgroup_ctrl *const ctrl)
 {
 	char mnt_path[PATH_MAX];
 	int made_dir = 0;
+	const char *tmpdir = getenv("TMPDIR");
+	if (!tmpdir)
+		tmpdir = "/tmp";
 
 	if (ctrl->ctrl_indx == CTRL_BLKIO && controllers[CTRL_IO].ctrl_root) {
 		tst_res(TCONF,
@@ -700,7 +707,8 @@ static void cgroup_mount_v1(struct cgroup_ctrl *const ctrl)
 		return;
 	}
 
-	sprintf(mnt_path, "%s%s", cgroup_mount_ltp_prefix, ctrl->ctrl_name);
+	sprintf(mnt_path, "%s/%s%s",
+		tmpdir, cgroup_mount_ltp_prefix, ctrl->ctrl_name);
 
 	if (!mkdir(mnt_path, 0777)) {
 		made_dir = 1;
Signature · 5 lines
2.38.1.273.g43a17bfeac-goog


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by on v1 1/1
#1 lore raw

Hi Edward,

Although it's oke to have 3 lines duplicate, it'd be better to put it into function, because we already have few places with this code (_supported_fs_types.c, getcwd02.c, filecaps_common.h, openposix: tempfile.h).

I'm not sure myself for a correct place, maybe some file in lib/*.c, which uses tst_fs.h (maybe we could merge some of these files which contain single function into tst_fs.c and put new function here).

I'm ok to merge it as is and find correct place as another effort.

Kind regards,
Petr

Quoted 54 lines

Use tmpdir instead of hardcoded /tmp path.

---
 lib/tst_cgroup.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
lib/tst_cgroup.c +11 -3
index 6c015e4f8..458b823f2 100644
@@ -290,7 +290,7 @@ static struct cgroup_ctrl controllers[] = {
 static const char *cgroup_ltp_dir = "ltp";
 static const char *cgroup_ltp_drain_dir = "drain";
 static char cgroup_test_dir[NAME_MAX + 1];
-static const char *cgroup_mount_ltp_prefix = "/tmp/cgroup_";
+static const char *cgroup_mount_ltp_prefix = "cgroup_";
 static const char *cgroup_v2_ltp_mount = "unified";
 
 #define first_root				\
@@ -645,8 +645,12 @@ static void cgroup_mount_v2(void)
 {
 	int ret;
 	char mnt_path[PATH_MAX];
+	const char *tmpdir = getenv("TMPDIR");
+	if (!tmpdir)
+		tmpdir = "/tmp";
 
-	sprintf(mnt_path, "%s%s", cgroup_mount_ltp_prefix, cgroup_v2_ltp_mount);
+	sprintf(mnt_path, "%s/%s%s",
+		tmpdir, cgroup_mount_ltp_prefix, cgroup_v2_ltp_mount);
 
 	if (!mkdir(mnt_path, 0777)) {
 		roots[0].mnt_dir.we_created_it = 1;
@@ -693,6 +697,9 @@ static void cgroup_mount_v1(struct cgroup_ctrl *const ctrl)
 {
 	char mnt_path[PATH_MAX];
 	int made_dir = 0;
+	const char *tmpdir = getenv("TMPDIR");
+	if (!tmpdir)
+		tmpdir = "/tmp";
 
 	if (ctrl->ctrl_indx == CTRL_BLKIO && controllers[CTRL_IO].ctrl_root) {
 		tst_res(TCONF,
@@ -700,7 +707,8 @@ static void cgroup_mount_v1(struct cgroup_ctrl *const ctrl)
 		return;
 	}
 
-	sprintf(mnt_path, "%s%s", cgroup_mount_ltp_prefix, ctrl->ctrl_name);
+	sprintf(mnt_path, "%s/%s%s",
+		tmpdir, cgroup_mount_ltp_prefix, ctrl->ctrl_name);
 
 	if (!mkdir(mnt_path, 0777)) {
 		made_dir = 1;
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 lore raw

Hello,

Quoted 8 lines · Petr Vorel

Hi Edward,

Although it's oke to have 3 lines duplicate, it'd be better to put it into function, because we already have few places with this code (_supported_fs_types.c, getcwd02.c, filecaps_common.h, openposix: tempfile.h).

Quoted 5 lines

I'm not sure myself for a correct place, maybe some file in lib/*.c, which uses tst_fs.h (maybe we could merge some of these files which contain single function into tst_fs.c and put new function here).

I'm ok to merge it as is and find correct place as another effort.

OK, pushed with added whitespace, thanks!

Quoted 58 lines

Kind regards,
Petr

Quoted 54 lines

Use tmpdir instead of hardcoded /tmp path.

---
 lib/tst_cgroup.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
lib/tst_cgroup.c +11 -3
index 6c015e4f8..458b823f2 100644
@@ -290,7 +290,7 @@ static struct cgroup_ctrl controllers[] = {
 static const char *cgroup_ltp_dir = "ltp";
 static const char *cgroup_ltp_drain_dir = "drain";
 static char cgroup_test_dir[NAME_MAX + 1];
-static const char *cgroup_mount_ltp_prefix = "/tmp/cgroup_";
+static const char *cgroup_mount_ltp_prefix = "cgroup_";
 static const char *cgroup_v2_ltp_mount = "unified";
 
 #define first_root				\
@@ -645,8 +645,12 @@ static void cgroup_mount_v2(void)
 {
 	int ret;
 	char mnt_path[PATH_MAX];
+	const char *tmpdir = getenv("TMPDIR");
+	if (!tmpdir)
+		tmpdir = "/tmp";
 
-	sprintf(mnt_path, "%s%s", cgroup_mount_ltp_prefix, cgroup_v2_ltp_mount);
+	sprintf(mnt_path, "%s/%s%s",
+		tmpdir, cgroup_mount_ltp_prefix, cgroup_v2_ltp_mount);
 
 	if (!mkdir(mnt_path, 0777)) {
 		roots[0].mnt_dir.we_created_it = 1;
@@ -693,6 +697,9 @@ static void cgroup_mount_v1(struct cgroup_ctrl *const ctrl)
 {
 	char mnt_path[PATH_MAX];
 	int made_dir = 0;
+	const char *tmpdir = getenv("TMPDIR");
+	if (!tmpdir)
+		tmpdir = "/tmp";
 
 	if (ctrl->ctrl_indx == CTRL_BLKIO && controllers[CTRL_IO].ctrl_root) {
 		tst_res(TCONF,
@@ -700,7 +707,8 @@ static void cgroup_mount_v1(struct cgroup_ctrl *const ctrl)
 		return;
 	}
 
-	sprintf(mnt_path, "%s%s", cgroup_mount_ltp_prefix, ctrl->ctrl_name);
+	sprintf(mnt_path, "%s/%s%s",
+		tmpdir, cgroup_mount_ltp_prefix, ctrl->ctrl_name);
 
 	if (!mkdir(mnt_path, 0777)) {
 		made_dir = 1;
Signature · 5 lines
Thank you,
Richard.

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp