Skip to contentSkip to conversation

mem/thp04: rebuild the cleanup without using safe macros

PATCH In review Li Wang 2 participants 2 messages Oct 23, 2014 – Nov 6, 2014

lore
Share & tools
lore

Li Wang

author
#0 lore raw

PATCH 1/1 mem/thp04: rebuild the cleanup without using safe macros

mem/lib: recall write_file & read_file functions

mem/thp04: rebuild the cleanup() by those two functions to avoid safe macros being abused.

---
 testcases/kernel/mem/include/mem.h |  2 ++
 testcases/kernel/mem/lib/mem.c     | 25 +++++++++++++++++++++++++
 testcases/kernel/mem/thp/thp04.c   | 27 +++++++++++++++++----------
 3 files changed, 44 insertions(+), 10 deletions(-)
testcases/kernel/mem/include/mem.h +2 -0
index 8fa2094..3cb3580 100644
@@ -96,6 +96,8 @@ int  path_exist(const char *path, ...);
 long read_meminfo(char *item);
 void set_sys_tune(char *sys_file, long tune, int check);
 long get_sys_tune(char *sys_file);
+void write_file(char *filename, char *buf);
+void read_file(char *filename, char *retbuf);
 void cleanup(void);
 void setup(void);
 
testcases/kernel/mem/lib/mem.c +25 -0
index d3fc19d..3bd9f65 100644
@@ -1065,6 +1065,31 @@ long get_sys_tune(char *sys_file)
 	return tune;
 }
 
+void write_file(char *filename, char *buf)
+{
+	int fd;
+
+	fd = open(filename, O_WRONLY);
+	if (fd == -1)
+		tst_resm(TWARN | TERRNO, "open");
+	tst_resm(TINFO, "set %s value to %s", filename, buf);
+	if (write(fd, buf, strlen(buf)) != strlen(buf))
+		tst_resm(TWARN | TERRNO, "write");
+	close(fd);
+}
+
+void read_file(char *filename, char *retbuf)
+{
+	int fd;
+
+	fd = open(filename, O_RDONLY);
+	if (fd == -1)
+		tst_resm(TWARN | TERRNO, "open");
+	if (read(fd, retbuf, BUFSIZ) < 0)
+		tst_resm(TWARN | TERRNO, "read");
+	close(fd);
+}
+
 void update_shm_size(size_t * shm_size)
 {
 	size_t shmmax;
testcases/kernel/mem/thp/thp04.c +17 -10
index 0b6baeb..4e9aa87 100644
@@ -61,6 +61,7 @@ option_t thp_options[] = {
 
 static int pre_thp_scan_sleep_millisecs;
 static int pre_thp_alloc_sleep_millisecs;
+static char buf[BUFSIZ], path[BUFSIZ];
 static char pre_thp_enabled[BUFSIZ];
 
 int main(int argc, char *argv[])
@@ -120,23 +121,29 @@ void setup(void)
 
 void cleanup(void)
 {
-	SAFE_FILE_PRINTF(NULL, PATH_KHPD "scan_sleep_millisecs",
-			 "%d", pre_thp_scan_sleep_millisecs);
+	snprintf(buf, BUFSIZ, "%d", pre_thp_scan_sleep_millisecs);
+	write_file(PATH_KHPD "scan_sleep_millisecs", buf);
 
-	SAFE_FILE_PRINTF(NULL, PATH_KHPD "alloc_sleep_millisecs",
-			 "%d", pre_thp_alloc_sleep_millisecs);
+	snprintf(buf, BUFSIZ, "%d", pre_thp_alloc_sleep_millisecs);
+	write_file(PATH_KHPD "alloc_sleep_millisecs", buf);
 
 	/*
 	 * The value of transparent_hugepage/enabled is speical,
 	 * we need to recover the previous value one by one for
 	 * the three mode: always, madvise, never.
 	 */
-	if (strcmp(pre_thp_enabled, "[always] madvise never") == 0)
-		SAFE_FILE_PRINTF(NULL, PATH_THP "enabled", "always");
-	else if (strcmp(pre_thp_enabled, "always [madvise] never") == 0)
-		SAFE_FILE_PRINTF(NULL, PATH_THP "enabled", "madvise");
-	else
-		SAFE_FILE_PRINTF(NULL, PATH_THP "enabled", "never");
+	if (strcmp(pre_thp_enabled, "[always] madvise never") == 0){
+		snprintf(path, BUFSIZ, "always");
+		write_file(PATH_THP "enabled", path);
+	}
+	else if (strcmp(pre_thp_enabled, "always [madvise] never") == 0){
+		snprintf(path, BUFSIZ, "madvise");
+		write_file(PATH_THP "enabled", path);
+	}
+	else{
+		snprintf(path, BUFSIZ, "never");
+		write_file(PATH_THP "enabled", path);
+	}
 
 	TEST_CLEANUP;
 }
Signature · 8 lines
1.9.3


------------------------------------------------------------------------------
_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list
#1 lore raw

Hi!

Quoted 103 lines

mem/lib: recall write_file & read_file functions

mem/thp04: rebuild the cleanup() by those two functions to avoid safe macros being abused.

---
 testcases/kernel/mem/include/mem.h |  2 ++
 testcases/kernel/mem/lib/mem.c     | 25 +++++++++++++++++++++++++
 testcases/kernel/mem/thp/thp04.c   | 27 +++++++++++++++++----------
 3 files changed, 44 insertions(+), 10 deletions(-)
testcases/kernel/mem/include/mem.h +2 -0
index 8fa2094..3cb3580 100644
@@ -96,6 +96,8 @@ int  path_exist(const char *path, ...);
 long read_meminfo(char *item);
 void set_sys_tune(char *sys_file, long tune, int check);
 long get_sys_tune(char *sys_file);
+void write_file(char *filename, char *buf);
+void read_file(char *filename, char *retbuf);
 void cleanup(void);
 void setup(void);
 
testcases/kernel/mem/lib/mem.c +25 -0
index d3fc19d..3bd9f65 100644
@@ -1065,6 +1065,31 @@ long get_sys_tune(char *sys_file)
 	return tune;
 }
 
+void write_file(char *filename, char *buf)
+{
+	int fd;
+
+	fd = open(filename, O_WRONLY);
+	if (fd == -1)
+		tst_resm(TWARN | TERRNO, "open");
+	tst_resm(TINFO, "set %s value to %s", filename, buf);
+	if (write(fd, buf, strlen(buf)) != strlen(buf))
+		tst_resm(TWARN | TERRNO, "write");
+	close(fd);
+}
+
+void read_file(char *filename, char *retbuf)
+{
+	int fd;
+
+	fd = open(filename, O_RDONLY);
+	if (fd == -1)
+		tst_resm(TWARN | TERRNO, "open");
+	if (read(fd, retbuf, BUFSIZ) < 0)
+		tst_resm(TWARN | TERRNO, "read");
+	close(fd);
+}
+
 void update_shm_size(size_t * shm_size)
 {
 	size_t shmmax;
testcases/kernel/mem/thp/thp04.c +10 -10
index 0b6baeb..4e9aa87 100644
@@ -61,6 +61,7 @@ option_t thp_options[] = {
 
 static int pre_thp_scan_sleep_millisecs;
 static int pre_thp_alloc_sleep_millisecs;
+static char buf[BUFSIZ], path[BUFSIZ];
 static char pre_thp_enabled[BUFSIZ];
 
 int main(int argc, char *argv[])
@@ -120,23 +121,29 @@ void setup(void)
 
 void cleanup(void)
 {
-	SAFE_FILE_PRINTF(NULL, PATH_KHPD "scan_sleep_millisecs",
-			 "%d", pre_thp_scan_sleep_millisecs);
+	snprintf(buf, BUFSIZ, "%d", pre_thp_scan_sleep_millisecs);
+	write_file(PATH_KHPD "scan_sleep_millisecs", buf);
 
-	SAFE_FILE_PRINTF(NULL, PATH_KHPD "alloc_sleep_millisecs",
-			 "%d", pre_thp_alloc_sleep_millisecs);
+	snprintf(buf, BUFSIZ, "%d", pre_thp_alloc_sleep_millisecs);
+	write_file(PATH_KHPD "alloc_sleep_millisecs", buf);
 
 	/*
 	 * The value of transparent_hugepage/enabled is speical,
 	 * we need to recover the previous value one by one for
 	 * the three mode: always, madvise, never.
 	 */
-	if (strcmp(pre_thp_enabled, "[always] madvise never") == 0)
-		SAFE_FILE_PRINTF(NULL, PATH_THP "enabled", "always");
-	else if (strcmp(pre_thp_enabled, "always [madvise] never") == 0)
-		SAFE_FILE_PRINTF(NULL, PATH_THP "enabled", "madvise");
-	else
-		SAFE_FILE_PRINTF(NULL, PATH_THP "enabled", "never");
+	if (strcmp(pre_thp_enabled, "[always] madvise never") == 0){
+		snprintf(path, BUFSIZ, "always");
+		write_file(PATH_THP "enabled", path);
+	}
+	else if (strcmp(pre_thp_enabled, "always [madvise] never") == 0){

testcases/kernel/mem/thp/thp04.c void setup(void) · line 149

Quoted 10 lines
testcases/kernel/mem/thp/thp04.c +7 -0
+		snprintf(path, BUFSIZ, "madvise");
+		write_file(PATH_THP "enabled", path);
+	}
+	else{
+		snprintf(path, BUFSIZ, "never");
+		write_file(PATH_THP "enabled", path);
+	}
 
 	TEST_CLEANUP;
 }

This is a bit better but suffers from the same issues as the previous one.

Signature · 8 lines
Cyril Hrubis
[email protected]

------------------------------------------------------------------------------
_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list