[LTP] [PATCH v2 4/4] mmapstress01: use safe macros

Edward Liaw edliaw@google.com
Fri Sep 30 19:59:59 CEST 2022


Signed-off-by: Edward Liaw <edliaw@google.com>
---
 .../kernel/mem/mmapstress/mmapstress01.c      | 85 ++++++++-----------
 1 file changed, 34 insertions(+), 51 deletions(-)

diff --git a/testcases/kernel/mem/mmapstress/mmapstress01.c b/testcases/kernel/mem/mmapstress/mmapstress01.c
index 0f5071a20..955d6ac3f 100644
--- a/testcases/kernel/mem/mmapstress/mmapstress01.c
+++ b/testcases/kernel/mem/mmapstress/mmapstress01.c
@@ -193,6 +193,7 @@ static void run(void)
 	int write_cnt;
 	unsigned char data;
 	off_t bytes_left;
+	sigset_t set_mask;
 
 	seed = initrand();
 	pattern = seed & 0xff;
@@ -204,29 +205,19 @@ static void run(void)
 	 */
 	sa.sa_handler = finish;
 	sa.sa_flags = 0;
-	if (sigemptyset(&sa.sa_mask))
-		tst_brk(TFAIL, "sigemptyset error");
-
-	if (sigaction(SIGINT, &sa, 0) == -1)
-		tst_brk(TFAIL, "sigaction error SIGINT");
-	if (sigaction(SIGQUIT, &sa, 0) == -1)
-		tst_brk(TFAIL, "sigaction error SIGQUIT");
-	if (sigaction(SIGTERM, &sa, 0) == -1)
-		tst_brk(TFAIL, "sigaction error SIGTERM");
+	SAFE_SIGEMPTYSET(&sa.sa_mask);
+	SAFE_SIGACTION(SIGINT, &sa, 0);
+	SAFE_SIGACTION(SIGQUIT, &sa, 0);
+	SAFE_SIGACTION(SIGTERM, &sa, 0);
 
 	if (alarmtime) {
-		if (sigaction(SIGALRM, &sa, 0) == -1)
-			tst_brk(TFAIL, "sigaction error");
+		SAFE_SIGACTION(SIGALRM, &sa, 0);
 		(void)alarm(alarmtime);
 	}
-	if ((fd = open(TEST_FILE, O_CREAT | O_TRUNC | O_RDWR, 0664)) == -1) {
-		tst_brk(TFAIL, "open error");
-	}
+	fd = SAFE_OPEN(TEST_FILE, O_CREAT | O_TRUNC | O_RDWR, 0664);
 
-	if ((buf = malloc(pagesize)) == NULL
-	    || (pidarray = malloc(nprocs * sizeof(pid_t))) == NULL) {
-		tst_brk(TFAIL, "malloc error");
-	}
+	buf = SAFE_MALLOC(pagesize);
+	pidarray = SAFE_MALLOC(nprocs * sizeof(pid_t));
 
 	for (i = 0; i < nprocs; i++)
 		*(pidarray + i) = 0;
@@ -236,9 +227,7 @@ static void run(void)
 		if (++data == nprocs)
 			data = 0;
 	}
-	if (lseek(fd, (off_t)sparseoffset, SEEK_SET) < 0) {
-		tst_brk(TFAIL, "lseek");
-	}
+	SAFE_LSEEK(fd, (off_t)sparseoffset, SEEK_SET);
 	for (bytes_left = filesize; bytes_left; bytes_left -= c) {
 		write_cnt = MIN(pagesize, (int)bytes_left);
 		if ((c = write(fd, buf, write_cnt)) != write_cnt) {
@@ -247,13 +236,13 @@ static void run(void)
 			else
 				tst_res(TINFO, "write: wrote %d of %d bytes",
 				        c, write_cnt);
-			(void)close(fd);
-			(void)unlink(TEST_FILE);
+			SAFE_CLOSE(fd);
+			SAFE_UNLINK(TEST_FILE);
 			tst_brk(TFAIL, "write error");
 		}
 	}
 
-	(void)close(fd);
+	SAFE_CLOSE(fd);
 
 	/*
 	 *  Fork off mmap children.
@@ -278,14 +267,16 @@ static void run(void)
 	 *  Now wait for children and refork them as needed.
 	 */
 
+	SAFE_SIGEMPTYSET(&set_mask);
+	SAFE_SIGADDSET(&set_mask, SIGALRM);
+	SAFE_SIGADDSET(&set_mask, SIGINT);
 	while (!finished) {
 		pid = wait(&wait_stat);
 		/*
 		 *  Block signals while processing child exit.
 		 */
 
-		if (sighold(SIGALRM) || sighold(SIGINT))
-			tst_brk(TFAIL, "sighold error");
+		SAFE_SIGPROCMASK(SIG_BLOCK, &set_mask, NULL);
 
 		if (pid != -1) {
 			/*
@@ -321,8 +312,7 @@ static void run(void)
 			if (errno != EINTR || !finished)
 				tst_brk(TFAIL, "unexpected wait error");
 		}
-		if (sigrelse(SIGALRM) || sigrelse(SIGINT))
-			tst_brk(TFAIL, "sigrelse error");
+		SAFE_SIGPROCMASK(SIG_UNBLOCK, &set_mask, NULL);
 	}
 
 	/*
@@ -330,8 +320,9 @@ static void run(void)
 	 *  the children and done!.
 	 */
 
-	if (sighold(SIGALRM))
-		tst_brk(TFAIL, "sighold error");
+	SAFE_SIGEMPTYSET(&set_mask);
+	SAFE_SIGADDSET(&set_mask, SIGALRM);
+	SAFE_SIGPROCMASK(SIG_BLOCK, &set_mask, NULL);
 	(void)alarm(0);
 	check_for_sanity = 1;
 	tst_res(TPASS, "finished, cleaning up");
@@ -352,7 +343,7 @@ static void cleanup(void)
 		} else {
 			tst_res(TINFO, "file data okay");
 			if (!leavefile)
-				(void)unlink(TEST_FILE);
+				SAFE_UNLINK(TEST_FILE);
 			tst_res(TPASS, "test passed");
 		}
 	} else {
@@ -386,12 +377,10 @@ void child_mapper(char *file, unsigned int procno, unsigned int nprocs)
 
 	seed = initrand();	/* initialize random seed */
 
-	if (stat(file, &statbuf) == -1)
-		tst_brk(TFAIL, "stat error");
+	SAFE_STAT(file, &statbuf);
 	filesize = statbuf.st_size;
 
-	if ((fd = open(file, O_RDWR)) == -1)
-		tst_brk(TFAIL, "open error");
+	fd = SAFE_OPEN(file, O_RDWR);
 
 	if (statbuf.st_size - sparseoffset > UINT_MAX)
 		tst_brk(TFAIL, "size_t overflow when setting up map");
@@ -411,11 +400,10 @@ void child_mapper(char *file, unsigned int procno, unsigned int nprocs)
 		tst_res(TINFO, "child %d (pid %d): seed %d, fsize %lld, mapsize %ld, off %lld, loop %d",
 		        procno, getpid(), seed, (long long)filesize,
 		        (long)mapsize, (long long)offset / pagesize, nloops);
-	if ((maddr = mmap(0, mapsize, PROT_READ | PROT_WRITE, MAP_SHARED,
-	                  fd, offset)) == (caddr_t) - 1)
-		tst_brk(TFAIL, "mmap error");
 
-	(void)close(fd);
+	maddr = SAFE_MMAP(0, mapsize, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
+	                  offset);
+	SAFE_CLOSE(fd);
 
 	/*
 	 *  Now loop read/writing random pages.
@@ -453,8 +441,7 @@ void child_mapper(char *file, unsigned int procno, unsigned int nprocs)
 		          MS_SYNC) == -1)
 			tst_brk(TFAIL, "msync failed");
 	}
-	if (munmap(maddr, mapsize) == -1)
-		tst_brk(TFAIL, "munmap failed");
+	SAFE_MUNMAP(maddr, mapsize);
 	exit(0);
 }
 
@@ -472,14 +459,10 @@ int fileokay(char *file, unsigned char *expbuf)
 	int cnt;
 	unsigned int i, j;
 
-	if ((fd = open(file, O_RDONLY)) == -1)
-		tst_brk(TFAIL, "open error");
-
-	if (fstat(fd, &statbuf) == -1)
-		tst_brk(TFAIL, "stat error");
+	fd = SAFE_OPEN(file, O_RDONLY);
 
-	if (lseek(fd, sparseoffset, SEEK_SET) < 0)
-		tst_brk(TFAIL, "lseek");
+	SAFE_FSTAT(fd, &statbuf);
+	SAFE_LSEEK(fd, sparseoffset, SEEK_SET);
 
 	if (statbuf.st_size - sparseoffset > UINT_MAX)
 		tst_brk(TFAIL, "size_t overflow when setting up map");
@@ -498,7 +481,7 @@ int fileokay(char *file, unsigned char *expbuf)
 			if ((i * pagesize) + cnt != mapsize) {
 				tst_res(TINFO, "read %d of %ld bytes",
 				        (i * pagesize) + cnt, (long)mapsize);
-				close(fd);
+				SAFE_CLOSE(fd);
 				return 0;
 			}
 		}
@@ -511,12 +494,12 @@ int fileokay(char *file, unsigned char *expbuf)
 				        "read bad data: exp %c got %c, pg %d off %d, (fsize %lld)",
 				        expbuf[j], readbuf[j], i, j,
 				        (long long)statbuf.st_size);
-				close(fd);
+				SAFE_CLOSE(fd);
 				return 0;
 			}
 		}
 	}
-	close(fd);
+	SAFE_CLOSE(fd);
 
 	return 1;
 }
-- 
2.38.0.rc1.362.ged0d419d3c-goog



More information about the ltp mailing list