v2 -> v3: fix a silly potential loop

current zram test is broken:

* At the end of test, error message printed:
    ERROR: Module zram is in use
  this is because the mapped area `s' is not freed yet. This patch unmaps
  it before calling `rmmod zram'.

* Sometimes `if (access() && errno == ENOENT)' doesn't work due to errno
  not correctly captured.

* argument passing to memset() is incorrect.

The fixes to above issues are all integrated into this patch and the
patch is tested by myself.

Signed-off-by: Caspar Zhang <[email protected]>
---
 testcases/kernel/mem/zram/zram01.c |   30 ++++++++++++++++++++++--------
 1 files changed, 22 insertions(+), 8 deletions(-)

diff --git a/testcases/kernel/mem/zram/zram01.c b/testcases/kernel/mem/zram/zram01.c
index e0a0361..b800243 100644
--- a/testcases/kernel/mem/zram/zram01.c
+++ b/testcases/kernel/mem/zram/zram01.c
@@ -31,12 +31,15 @@
 #include <errno.h>
 #include <unistd.h>
 #include <string.h>
+
 #include "test.h"
 #include "usctest.h"
+#include "safe_macros.h"
 
 char *TCID = "zram01";
 int TST_TOTAL = 1;
 int modprobe = 0;
+void *s;
 
 #define PATH_ZRAM	"/sys/block/zram0"
 #define SIZE		(512 * 1024 * 1024)
@@ -52,7 +55,6 @@ int main(int argc, char *argv[])
 	int lc, fd;
 	char *msg;
 	char size[BUFSIZ];
-	void *s;
 
 	msg = parse_opts(argc, argv, NULL, NULL);
 	if (msg != NULL)
@@ -83,7 +85,7 @@ int main(int argc, char *argv[])
 			tst_brkm(TBROK|TERRNO, cleanup, "mmap");
 
 		tst_resm(TINFO, "write all the memory.");
-		memset(s, SIZE, 'a');
+		memset(s, 'a', SIZE);
 		close(fd);
 
 		dump_info();
@@ -106,22 +108,34 @@ int main(int argc, char *argv[])
 
 void setup(void)
 {
+	int retried = 0;
+
 	tst_require_root(NULL);
 
-	if (access(PATH_ZRAM, R_OK|W_OK|X_OK) == -1 && errno == ENOENT) {
-		system("modprobe zram");
-		modprobe = 1;
-		if (access(PATH_ZRAM, R_OK|W_OK|X_OK) == -1 && errno == ENOENT)
-			tst_brkm(TCONF, NULL, "system has no zram device.");
-		else
+retry:
+	if (access(PATH_ZRAM, R_OK|W_OK|X_OK) == -1) {
+		if (errno == ENOENT) {
+			if (retried)
+				tst_brkm(TCONF, NULL,
+					"system has no zram device.");
+			retried = 1;
+			goto retry;
+		} else
 			tst_brkm(TBROK|TERRNO, NULL, "access");
 	}
+	
+	modprobe = 1;
+
 	tst_sig(FORK, DEF_HANDLER, cleanup);
 	TEST_PAUSE;
 }
 
 void cleanup(void)
 {
+	if (s != MAP_FAILED) {
+		SAFE_MUNMAP(NULL, s, SIZE);
+		sleep(1);
+	}
 	if (modprobe == 1)
 		system("rmmod zram");
 
------------------------------------------------------------------------------
RSA(R) Conference 2012
Save $700 by Nov 18
Register now
http://p.sf.net/sfu/rsa-sfdev2dev1
_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list

Reply via email to