Skip to contentSkip to conversation

Make use of SAFE_CHDIR()

PATCH New Cyril Hrubis 1 participant 40 messages Oct 3, 2017 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/40 Make use of SAFE_CHDIR()

---
 lib/self_exec.c                           |  7 ++-----
 testcases/kernel/fs/ftest/ftest01.c       |  4 ++--
 testcases/kernel/fs/ftest/ftest03.c       |  6 ++----
 testcases/kernel/fs/ftest/ftest05.c       |  5 ++---
 testcases/kernel/fs/ftest/ftest07.c       |  6 ++----
 testcases/kernel/syscalls/clone/clone02.c | 11 ++---------
 testcases/kernel/syscalls/mknod/mknod02.c |  6 ++----
 testcases/kernel/syscalls/mknod/mknod03.c |  6 ++----
 testcases/kernel/syscalls/mknod/mknod04.c |  6 ++----
 testcases/kernel/syscalls/mknod/mknod05.c |  6 ++----
 testcases/kernel/syscalls/mknod/mknod08.c |  6 ++----
 11 files changed, 22 insertions(+), 47 deletions(-)
lib/self_exec.c +2 -5
index c287ce913..de7d09517 100644
@@ -32,6 +32,7 @@
 #include <string.h>
 #include <stdio.h>
 #include "test.h"
+#include "safe_macros.h"
 
 /* Set from parse_opts.c: */
 char *child_args;		/* Arguments to child when -C is used */
@@ -152,11 +153,7 @@ void maybe_run_child(void (*child) (), const char *fmt, ...)
 
 		va_end(ap);
 		free(args);
-		if (chdir(child_dir) < 0) {
-			tst_brkm(TBROK, NULL,
-				 "Could not change to %s for child", child_dir);
-			return;
-		}
+		SAFE_CHDIR(NULL, child_dir);
 
 		(*child) ();
 		tst_resm(TWARN, "Child function returned unexpectedly");
testcases/kernel/fs/ftest/ftest01.c +2 -2
index 2dfc92a0a..f7e320cd9 100644
@@ -59,6 +59,7 @@
 #include <unistd.h>
 #include <inttypes.h>
 #include "test.h"
+#include "safe_macros.h"
 #include "libftest.h"
 
 char *TCID = "ftest01";
@@ -129,8 +130,7 @@ static void setup(void)
 
 	mkdir(fuss, 0755);
 
-	if (chdir(fuss) < 0)
-		tst_brkm(TBROK | TERRNO, NULL, "chdir failed");
+	SAFE_CHDIR(NULL, fuss);
 
 	/*
 	 * Default values for run conditions.
testcases/kernel/fs/ftest/ftest03.c +2 -4
index 9dc47c942..4d0fc803a 100644
@@ -64,6 +64,7 @@
 #include <stdio.h>
 #include <inttypes.h>
 #include "test.h"
+#include "safe_macros.h"
 #include "libftest.h"
 
 char *TCID = "ftest03";
@@ -149,10 +150,7 @@ static void setup(void)
 
 	mkdir(fuss, 0755);
 
-	if (chdir(fuss) < 0) {
-		tst_brkm(TBROK, NULL, "\tCan't chdir(%s), error %d.", fuss,
-			 errno);
-	}
+	SAFE_CHDIR(NULL, fuss);
 
 	/*
 	 * Default values for run conditions.
testcases/kernel/fs/ftest/ftest05.c +2 -3
index da15c971a..b647f8b06 100644
@@ -63,6 +63,7 @@
 #include <inttypes.h>
 #include <sys/param.h>
 #include "test.h"
+#include "safe_macros.h"
 #include "libftest.h"
 
 char *TCID = "ftest05";
@@ -137,9 +138,7 @@ static void setup(void)
 
 	mkdir(fuss, 0755);
 
-	if (chdir(fuss) < 0) {
-		tst_brkm(TBROK | TERRNO, NULL, "\tCan't chdir(%s)", fuss);
-	}
+	SAFE_CHDIR(NULL, fuss);
 
 	/*
 	 * Default values for run conditions.
testcases/kernel/fs/ftest/ftest07.c +2 -4
index 915d01540..f80f1922f 100644
@@ -70,6 +70,7 @@
 #include <unistd.h>
 #include <inttypes.h>
 #include "test.h"
+#include "safe_macros.h"
 #include "libftest.h"
 
 char *TCID = "ftest07";
@@ -152,10 +153,7 @@ static void setup(void)
 
 	mkdir(fuss, 0755);
 
-	if (chdir(fuss) < 0) {
-		tst_brkm(TBROK, NULL, "\tCan't chdir(%s), error %d.", fuss,
-			 errno);
-	}
+	SAFE_CHDIR(NULL, fuss);
 
 	/*
 	 * Default values for run conditions.
testcases/kernel/syscalls/clone/clone02.c +2 -9
index 3f61d2297..d864aaea5 100644
@@ -63,6 +63,7 @@
 #include <sys/syscall.h>
 #include <sched.h>
 #include "test.h"
+#include "safe_macros.h"
 
 #define FLAG_ALL (CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|SIGCHLD)
 #define FLAG_NONE SIGCHLD
@@ -250,15 +251,7 @@ static void test_cleanup(void)
 {
 
 	/* Restore parent's working directory */
-	if (chdir(cwd_parent) == -1) {
-		/*
-		 * we have to exit here
-		 *
-		 * XXX (garrcoop): why???
-		 */
-		tst_brkm(TBROK | TERRNO, cleanup,
-			 "chdir() failed in test_cleanup()");
-	}
+	SAFE_CHDIR(cleanup, cwd_parent);
 
 }
 
testcases/kernel/syscalls/mknod/mknod02.c +2 -4
index 07c204841..85ae1409b 100644
@@ -80,6 +80,7 @@
 #include <sys/stat.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 #define LTPUSER		"nobody"
 #define MODE_RWX	S_IFIFO | S_IRWXU | S_IRWXG | S_IRWXO
@@ -279,10 +280,7 @@ void setup(void)
 	mygid = getgid();
 
 	/* Change directory to DIR_TEMP */
-	if (chdir(DIR_TEMP) < 0) {
-		tst_brkm(TBROK, cleanup,
-			 "Unable to change to %s directory", DIR_TEMP);
-	}
+	SAFE_CHDIR(cleanup, DIR_TEMP);
 }
 
 /*
testcases/kernel/syscalls/mknod/mknod03.c +2 -4
index 4318bb9b6..3d4e3c37f 100644
@@ -80,6 +80,7 @@
 #include <sys/stat.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 #define LTPUSER		"nobody"
 #define MODE_RWX	S_IFIFO | S_IRWXU | S_IRWXG | S_IRWXO
@@ -279,10 +280,7 @@ void setup(void)
 	mygid = getgid();
 
 	/* Change directory to DIR_TEMP */
-	if (chdir(DIR_TEMP) < 0) {
-		tst_brkm(TBROK, cleanup,
-			 "Unable to change to %s directory", DIR_TEMP);
-	}
+	SAFE_CHDIR(cleanup, DIR_TEMP);
 }
 
 /*
testcases/kernel/syscalls/mknod/mknod04.c +2 -4
index 4b7f42f51..67c31fe58 100644
@@ -80,6 +80,7 @@
 #include <sys/stat.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 #define LTPUSER		"nobody"
 #define MODE_RWX	S_IFIFO | S_IRWXU | S_IRWXG | S_IRWXO
@@ -281,10 +282,7 @@ void setup(void)
 	mygid = getgid();
 
 	/* Change directory to DIR_TEMP */
-	if (chdir(DIR_TEMP) < 0) {
-		tst_brkm(TBROK, cleanup,
-			 "Unable to change to %s directory", DIR_TEMP);
-	}
+	SAFE_CHDIR(cleanup, DIR_TEMP);
 }
 
 /*
testcases/kernel/syscalls/mknod/mknod05.c +2 -4
index a9b395fe1..0808971ed 100644
@@ -80,6 +80,7 @@
 #include <sys/stat.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 #define LTPUSER		"nobody"
 #define MODE_RWX	S_IFIFO | S_IRWXU | S_IRWXG | S_IRWXO
@@ -256,10 +257,7 @@ void setup(void)
 	}
 
 	/* Change directory to DIR_TEMP */
-	if (chdir(DIR_TEMP) < 0) {
-		tst_brkm(TBROK, cleanup,
-			 "Unable to change to %s directory", DIR_TEMP);
-	}
+	SAFE_CHDIR(cleanup, DIR_TEMP);
 }
 
 /*
testcases/kernel/syscalls/mknod/mknod08.c +2 -4
index 5d3f6872a..6937da532 100644
@@ -80,6 +80,7 @@
 #include <sys/stat.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 #define LTPUSER		"nobody"
 #define MODE_RWX	S_IFIFO | S_IRWXU | S_IRWXG | S_IRWXO
@@ -274,10 +275,7 @@ void setup(void)
 	mygid = getgid();
 
 	/* Change directory to DIR_TEMP */
-	if (chdir(DIR_TEMP) < 0) {
-		tst_brkm(TBROK, cleanup,
-			 "Unable to change to %s directory", DIR_TEMP);
-	}
+	SAFE_CHDIR(cleanup, DIR_TEMP);
 }
 
 /*
Signature · 1 line
2.13.5
#1 lore raw

PATCH 2/40 Make use of SAFE_CLOSE()

---
 testcases/kernel/io/direct_io/diotest4.c               |  6 ++----
 .../kernel/security/prot_hsymlinks/prot_hsymlinks.c    |  3 +--
 testcases/kernel/syscalls/asyncio/asyncio02.c          |  5 ++---
 testcases/kernel/syscalls/chmod/chmod01.c              |  4 ++--
 testcases/kernel/syscalls/chmod/chmod03.c              |  5 ++---
 testcases/kernel/syscalls/chmod/chmod07.c              |  4 ++--
 testcases/kernel/syscalls/chown/chown02.c              |  9 +++------
 testcases/kernel/syscalls/chown/chown03.c              |  5 ++---
 testcases/kernel/syscalls/chown/chown05.c              |  4 ++--
 testcases/kernel/syscalls/dup/dup01.c                  |  6 ++----
 testcases/kernel/syscalls/dup/dup02.c                  |  5 ++---
 testcases/kernel/syscalls/dup/dup03.c                  |  5 ++---
 testcases/kernel/syscalls/dup/dup04.c                  |  8 +++-----
 testcases/kernel/syscalls/dup/dup05.c                  |  5 ++---
 testcases/kernel/syscalls/dup2/dup203.c                | 11 ++++-------
 testcases/kernel/syscalls/dup2/dup204.c                |  5 ++---
 testcases/kernel/syscalls/fchmod/fchmod02.c            |  6 ++----
 testcases/kernel/syscalls/fchmod/fchmod04.c            |  7 ++-----
 testcases/kernel/syscalls/fchmod/fchmod05.c            |  7 ++-----
 testcases/kernel/syscalls/fchmod/fchmod07.c            |  7 ++-----
 testcases/kernel/syscalls/fdatasync/fdatasync02.c      |  5 ++---
 testcases/kernel/syscalls/fstat/fstat03.c              |  5 ++---
 testcases/kernel/syscalls/fstat/fstat05.c              |  5 ++---
 testcases/kernel/syscalls/getdents/getdents01.c        |  3 +--
 testcases/kernel/syscalls/inotify/inotify01.c          | 13 ++++---------
 testcases/kernel/syscalls/inotify/inotify02.c          |  6 ++----
 testcases/kernel/syscalls/inotify/inotify03.c          |  4 ++--
 testcases/kernel/syscalls/ioctl/ioctl02.c              |  7 +++----
 testcases/kernel/syscalls/lchown/lchown01.c            |  5 ++---
 testcases/kernel/syscalls/lchown/lchown02.c            | 11 ++++-------
 testcases/kernel/syscalls/llseek/llseek02.c            |  6 ++----
 testcases/kernel/syscalls/mprotect/mprotect03.c        |  4 +---
 testcases/kernel/syscalls/mremap/mremap01.c            |  5 ++---
 testcases/kernel/syscalls/munmap/munmap02.c            |  6 ++----
 testcases/kernel/syscalls/pread/pread01.c              |  6 ++----
 testcases/kernel/syscalls/pread/pread02.c              |  6 ++----
 testcases/kernel/syscalls/pwrite/pwrite01.c            |  6 ++----
 testcases/kernel/syscalls/read/read04.c                |  5 ++---
 testcases/kernel/syscalls/readahead/readahead02.c      |  3 +--
 testcases/kernel/syscalls/readv/readv02.c              |  5 ++---
 testcases/kernel/syscalls/readv/readv03.c              |  5 ++---
 testcases/kernel/syscalls/stat/stat03.c                | 13 +++----------
 testcases/kernel/syscalls/symlink/symlink03.c          | 18 ++++--------------
 testcases/kernel/syscalls/truncate/truncate01.c        |  7 ++-----
 44 files changed, 96 insertions(+), 180 deletions(-)
testcases/kernel/io/direct_io/diotest4.c +2 -4
index f3b7d3ba8..dbcacf1ae 100644
@@ -70,6 +70,7 @@
 #include "diotest_routines.h"
 
 #include "test.h"
+#include "safe_macros.h"
 
 char *TCID = "diotest4";	/* Test program identifier.    */
 int TST_TOTAL = 17;		/* Total number of test conditions */
@@ -329,10 +330,7 @@ int main(int argc, char *argv[])
 	/* Test-7: Closed file descriptor */
 	offset = 4096;
 	count = bufsize;
-	if (close(fd) < 0) {
-		tst_brkm(TBROK, cleanup, "can't close fd %d: %s", fd,
-			 strerror(errno));
-	}
+	SAFE_CLOSE(cleanup, fd);
 	ret = runtest_f(fd, buf2, offset, count, EBADF, 7, "closed fd");
 	testcheck_end(ret, &failed, &fail_count, "Closed file descriptor");
 	total++;
testcases/kernel/security/prot_hsymlinks/prot_hsymlinks.c +1 -2
index 1d615ae5b..ecfbd1533 100644
@@ -554,8 +554,7 @@ static int try_open(const char *name, int mode)
 	if (fd == -1)
 		return fd;
 
-	if (close(fd) == -1)
-		tst_brkm(TBROK, cleanup, "Can't close file: %s", name);
+	SAFE_CLOSE(cleanup, fd);
 
 	return 0;
 }
testcases/kernel/syscalls/asyncio/asyncio02.c +2 -3
index e97cc4932..9e3ca7ad4 100644
@@ -97,6 +97,7 @@
 #include <errno.h>
 #include <stdlib.h>
 #include "test.h"
+#include "safe_macros.h"
 
 #define FLAG O_RDWR | O_CREAT | O_TRUNC	/* Flags used when opening temp tile */
 #define MODE  0777		/* Mode to open file with */
@@ -238,9 +239,7 @@ int testrun(int flag, int bytes, int ti)
 	 *      Attempt to close the file which also flushes the buffers.
 	 */
 
-	if (close(fildes) == -1) {
-		tst_brkm(TBROK | TERRNO, cleanup, "close() failed");
-	}
+	SAFE_CLOSE(cleanup, fildes);
 
 	ret = OK;
 
testcases/kernel/syscalls/chmod/chmod01.c +2 -2
index 9631c92a1..4428c9cd1 100644
@@ -76,6 +76,7 @@
 #include <signal.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 #define FILE_MODE	S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
 #define TESTFILE	"testfile"
@@ -152,8 +153,7 @@ void setup(void)
 		tst_brkm(TBROK | TERRNO, cleanup,
 			 "open(%s, O_RDWR|O_CREAT, %o) failed",
 			 TESTFILE, FILE_MODE);
-	if (close(fd) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "close(%s) failed", TESTFILE);
+	SAFE_CLOSE(cleanup, fd);
 
 }
 
testcases/kernel/syscalls/chmod/chmod03.c +2 -3
index 47c5c97f9..1ea242396 100644
@@ -80,6 +80,7 @@
 #include <pwd.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 #define FILE_MODE       S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
 #define PERMS		01777	/*
@@ -167,9 +168,7 @@ void setup(void)
 			 TESTFILE, FILE_MODE);
 	}
 
-	if (close(fd) == -1) {
-		tst_brkm(TBROK | TERRNO, cleanup, "close(%s) failed", TESTFILE);
-	}
+	SAFE_CLOSE(cleanup, fd);
 }
 
 /*
testcases/kernel/syscalls/chmod/chmod07.c +2 -2
index c7508ff9c..1cfec3bd0 100644
@@ -81,6 +81,7 @@
 #include <pwd.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 #define LTPUSER		"nobody"
 #define LTPGRP		"users"
@@ -182,8 +183,7 @@ void setup(void)
 		tst_brkm(TBROK | TERRNO, cleanup,
 			 "open(%s, O_RDWR|O_CREAT, %#o) failed",
 			 TESTFILE, FILE_MODE);
-	if (close(fd) == -1)
-		tst_brkm(TBROK, cleanup, "close(%s) failed", TESTFILE);
+	SAFE_CLOSE(cleanup, fd);
 	if (chown(TESTFILE, user1_uid, group1_gid) == -1)
 		tst_brkm(TBROK | TERRNO, cleanup, "chown(%s) failed", TESTFILE);
 
testcases/kernel/syscalls/chown/chown02.c +3 -6
index 227986eff..f1d905e22 100644
@@ -78,6 +78,7 @@
 #include <signal.h>
 
 #include "test.h"
+#include "safe_macros.h"
 #include "compat_16.h"
 
 #define FILE_MODE	(S_IFREG|S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
@@ -238,9 +239,7 @@ int setup1(void)
 		tst_brkm(TBROK | TERRNO, cleanup,
 			 "open(%s, O_RDWR|O_CREAT, %o) failed",
 			 TESTFILE1, FILE_MODE);
-	if (close(fd) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "close(%s) failed",
-			 TESTFILE1);
+	SAFE_CLOSE(cleanup, fd);
 
 	/* Set setuid/setgid bits on the test file created */
 	if (chmod(TESTFILE1, NEW_PERMS1) == -1)
@@ -268,9 +267,7 @@ int setup2(void)
 	/* Set setgid bit on the test file created */
 	if (fchmod(fd, NEW_PERMS2) != 0)
 		tst_brkm(TBROK | TERRNO, cleanup, "fchmod failed");
-	if (close(fd) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "close(%s) failed",
-			 TESTFILE2);
+	SAFE_CLOSE(cleanup, fd);
 	return 0;
 }
 
testcases/kernel/syscalls/chown/chown03.c +2 -3
index fdf40014f..6188315df 100644
@@ -81,6 +81,7 @@
 #include <pwd.h>
 
 #include "test.h"
+#include "safe_macros.h"
 #include "compat_16.h"
 
 #define FILE_MODE	(S_IFREG|S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
@@ -194,9 +195,7 @@ void setup(void)
 	if (seteuid(ltpuser->pw_uid) == -1)
 		tst_brkm(TBROK | TERRNO, cleanup, "seteuid to nobody failed");
 
-	if (close(fd) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "closing %s failed",
-			 TESTFILE);
+	SAFE_CLOSE(cleanup, fd);
 }
 
 void cleanup(void)
testcases/kernel/syscalls/chown/chown05.c +2 -2
index 51a7fc06c..840558c08 100644
@@ -76,6 +76,7 @@
 #include <signal.h>
 
 #include "test.h"
+#include "safe_macros.h"
 #include "compat_16.h"
 
 #define FILE_MODE	(S_IFREG|S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
@@ -162,8 +163,7 @@ void setup(void)
 	if ((fd = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE)) == -1)
 		tst_brkm(TBROK | TERRNO, cleanup, "opening %s failed",
 			 TESTFILE);
-	if (close(fd) == -1)
-		tst_brkm(TBROK, cleanup, "closing %s failed", TESTFILE);
+	SAFE_CLOSE(cleanup, fd);
 
 }
 
testcases/kernel/syscalls/dup/dup01.c +2 -4
index 231958d63..5c82ebefc 100644
@@ -115,6 +115,7 @@
 #include <string.h>
 #include <signal.h>
 #include "test.h"
+#include "safe_macros.h"
 
 void setup();
 void cleanup();
@@ -151,10 +152,7 @@ int main(int ac, char **av)
 				 filename, TEST_RETURN);
 
 			/* close the new file so loops do not open too many files */
-			if (close(TEST_RETURN) == -1) {
-				tst_brkm(TBROK | TERRNO, cleanup,
-					 "closing %s failed", filename);
-			}
+			SAFE_CLOSE(cleanup, TEST_RETURN);
 		}
 
 	}
testcases/kernel/syscalls/dup/dup02.c +2 -3
index 36ac76041..c0919ef09 100644
@@ -114,6 +114,7 @@
 #include <string.h>
 #include <signal.h>
 #include "test.h"
+#include "safe_macros.h"
 
 void setup();
 void cleanup();
@@ -152,9 +153,7 @@ int main(int ac, char **av)
 			} else {
 				tst_resm(TFAIL, "dup succeeded unexpectedly");
 
-				if (close(TEST_RETURN) == -1)
-					tst_brkm(TBROK | TERRNO, cleanup,
-						 "close failed");
+				SAFE_CLOSE(cleanup, TEST_RETURN);
 			}
 		}
 	}
testcases/kernel/syscalls/dup/dup03.c +2 -3
index 8b4c72635..e41c9e445 100644
@@ -112,6 +112,7 @@
 #include <signal.h>
 #include <stdlib.h>
 #include "test.h"
+#include "safe_macros.h"
 
 void setup();
 void cleanup();
@@ -148,9 +149,7 @@ int main(int ac, char **av)
 		} else {
 			tst_resm(TFAIL, "dup succeeded unexpectedly");
 
-			if (close(TEST_RETURN) == -1)
-				tst_brkm(TBROK | TERRNO, cleanup,
-					 "close failed");
+			SAFE_CLOSE(cleanup, TEST_RETURN);
 		}
 
 	}
testcases/kernel/syscalls/dup/dup04.c +3 -5
index ec050fd17..4abe451c1 100644
@@ -119,6 +119,7 @@
 #include <string.h>
 #include <signal.h>
 #include "test.h"
+#include "safe_macros.h"
 
 void setup();
 void cleanup();
@@ -150,8 +151,7 @@ int main(int ac, char **av)
 				 "dup(%d) read side of syspipe returned %ld",
 				 fd[0], TEST_RETURN);
 
-			if (close(TEST_RETURN) == -1)
-				tst_brkm(TBROK, cleanup, "close failed");
+			SAFE_CLOSE(cleanup, TEST_RETURN);
 		}
 
 		TEST(dup(fd[1]));
@@ -165,9 +165,7 @@ int main(int ac, char **av)
 				 fd[1], TEST_RETURN);
 
 
-			if (close(TEST_RETURN) == -1)
-				tst_brkm(TBROK | TTERRNO, cleanup,
-					 "close failed");
+			SAFE_CLOSE(cleanup, TEST_RETURN);
 		}
 
 	}
testcases/kernel/syscalls/dup/dup05.c +2 -3
index 6d4623631..3ef61cfa0 100644
@@ -116,6 +116,7 @@
 #include <string.h>
 #include <signal.h>
 #include "test.h"
+#include "safe_macros.h"
 
 void setup();
 void cleanup();
@@ -146,9 +147,7 @@ int main(int ac, char **av)
 			tst_resm(TPASS, "dup returned %ld",
 				 TEST_RETURN);
 
-			if (close(TEST_RETURN) == -1)
-				tst_brkm(TBROK | TERRNO, cleanup,
-					 "close failed");
+			SAFE_CLOSE(cleanup, TEST_RETURN);
 		}
 
 	}
testcases/kernel/syscalls/dup2/dup203.c +4 -7
index 4fdc9f4d0..d3c228362 100644
@@ -49,6 +49,7 @@
 #include <errno.h>
 #include <string.h>
 #include "test.h"
+#include "safe_macros.h"
 
 void setup(void);
 void cleanup(void);
@@ -90,13 +91,11 @@ int main(int ac, char **av)
 		if (write(fd1, filename1, strlen(filename1)) == -1)
 			tst_brkm(TBROK, cleanup, "filename1: write(2) failed");
 
-		if (close(fd0) == -1)
-			tst_brkm(TBROK, cleanup, "close(2) fd0 failed");
+		SAFE_CLOSE(cleanup, fd0);
 		if ((fd0 = open(filename0, O_RDONLY)) == -1)
 			tst_brkm(TBROK, cleanup, "open(2) on filename0 failed");
 
-		if (close(fd1) == -1)
-			tst_brkm(TBROK, cleanup, "close(2) fd1 failed");
+		SAFE_CLOSE(cleanup, fd1);
 		if ((fd1 = open(filename1, O_RDONLY)) == -1)
 			tst_brkm(TBROK, cleanup, "open(2) on filename1 failed");
 
@@ -145,9 +144,7 @@ int main(int ac, char **av)
 			tst_brkm(TBROK, cleanup, "Cannot create second file");
 		}
 
-		if (close(fd2) == -1) {
-			tst_brkm(TBROK, cleanup, "close(2) fd_closed failed");
-		}
+		SAFE_CLOSE(cleanup, fd2);
 
 		TEST(dup2(fd0, fd2));
 
testcases/kernel/syscalls/dup2/dup204.c +2 -3
index 083968272..ab8de5d08 100644
@@ -50,6 +50,7 @@
 #include <signal.h>
 #include <string.h>
 #include "test.h"
+#include "safe_macros.h"
 
 void setup();
 void cleanup();
@@ -97,9 +98,7 @@ int main(int ac, char **av)
 				tst_resm(TPASS, "original and duped "
 					 "inodes are the same");
 
-			if (close(TEST_RETURN) == -1)
-				tst_brkm(TBROK | TERRNO, cleanup,
-					 "close failed");
+			SAFE_CLOSE(cleanup, TEST_RETURN);
 		}
 	}
 
testcases/kernel/syscalls/fchmod/fchmod02.c +2 -4
index 6abb04d68..207bbab8c 100644
@@ -81,6 +81,7 @@
 #include <pwd.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 #define LTPUSER		"nobody"
 #define LTPGRP		"users"
@@ -213,10 +214,7 @@ void cleanup(void)
 {
 
 	/* Close the testfile created in the setup() */
-	if (close(fd) == -1) {
-		tst_brkm(TBROK, NULL, "close(%s) Failed, errno=%d : %s",
-			 TESTFILE, errno, strerror(errno));
-	}
+	SAFE_CLOSE(NULL, fd);
 
 	tst_rmdir();
 
testcases/kernel/syscalls/fchmod/fchmod04.c +2 -5
index 77948fd35..dd79b6cb3 100644
@@ -80,6 +80,7 @@
 #include <pwd.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 #define DIR_MODE 	S_IRWXU | S_IRWXG | S_IRWXO
 #define PERMS		01777	/*
@@ -190,11 +191,7 @@ void cleanup(void)
 {
 
 	/* Close the test directory opened during setup() */
-	if (close(fd) == -1) {
-		tst_brkm(TBROK, NULL,
-			 "close(%s) Failed, errno=%d : %s",
-			 TESTDIR, errno, strerror(errno));
-	}
+	SAFE_CLOSE(NULL, fd);
 
 	tst_rmdir();
 
testcases/kernel/syscalls/fchmod/fchmod05.c +2 -5
index 9b32ebe66..4ef62d4cc 100644
@@ -87,6 +87,7 @@
 #include <pwd.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 #define MODE_RWX	(S_IRWXU | S_IRWXG | S_IRWXO)
 #define PERMS		043777
@@ -226,11 +227,7 @@ void cleanup(void)
 {
 
 	/* Close the test directory opened in the setup() */
-	if (close(fd) == -1) {
-		tst_brkm(TBROK, NULL,
-			 "close(%s) Failed, errno=%d : %s",
-			 TESTDIR, errno, strerror(errno));
-	}
+	SAFE_CLOSE(NULL, fd);
 
 	setegid(0);
 	seteuid(0);
testcases/kernel/syscalls/fchmod/fchmod07.c +2 -5
index ab03721a1..66032e486 100644
@@ -74,6 +74,7 @@
 #include <signal.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 #define FILE_MODE	S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
 #define TESTFILE	"testfile"
@@ -186,11 +187,7 @@ void cleanup(void)
 {
 
 	/* Close the TESTFILE opened in the setup() */
-	if (close(fd) == -1) {
-		tst_brkm(TBROK, NULL,
-			 "close(%s) Failed, errno=%d : %s",
-			 TESTFILE, errno, strerror(errno));
-	}
+	SAFE_CLOSE(NULL, fd);
 
 	tst_rmdir();
 
testcases/kernel/syscalls/fdatasync/fdatasync02.c +2 -3
index 5f0d492eb..9ce4fc7bb 100644
@@ -76,6 +76,7 @@
 #include <fcntl.h>
 #include <unistd.h>
 #include "test.h"
+#include "safe_macros.h"
 
 #define EXP_RET_VAL	-1
 #define SPL_FILE	"/dev/null"
@@ -166,9 +167,7 @@ int setup2(void)
 void cleanup2(void)
 {
 	/* close special file */
-	if (close(fd) == -1) {
-		tst_brkm(TBROK, NULL, "Failed to close fd of %s", SPL_FILE);
-	}
+	SAFE_CLOSE(NULL, fd);
 }
 
 /*
testcases/kernel/syscalls/fstat/fstat03.c +2 -3
index 45e27b49d..1ff61c7ca 100644
@@ -57,6 +57,7 @@
 #include <sys/stat.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 #define FILE_MODE	S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
 #define TEST_FILE	"testfile"
@@ -141,9 +142,7 @@ void setup(void)
 		tst_brkm(TBROK | TERRNO, cleanup,
 			 "open(%s, O_RDWR|O_CREAT, 0666) failed", TEST_FILE);
 
-	if (close(fildes) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "close(%s) failed",
-			 TEST_FILE);
+	SAFE_CLOSE(cleanup, fildes);
 }
 
 /*
testcases/kernel/syscalls/fstat/fstat05.c +2 -3
index 7e4a4f1c2..69f746c73 100644
@@ -93,6 +93,7 @@
 #include <pwd.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 #define TEST_FILE       "testfile"
 
@@ -232,9 +233,7 @@ void setup(void)
 void cleanup(void)
 {
 
-	if (close(fildes) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "close(%s) failed",
-			 TEST_FILE);
+	SAFE_CLOSE(cleanup, fildes);
 
 	tst_rmdir();
 
testcases/kernel/syscalls/getdents/getdents01.c +1 -2
index 4dadd4bdc..3962d960b 100644
@@ -154,8 +154,7 @@ int main(int ac, char **av)
 
 		} while (rval > 0);
 
-		if (close(fd) == -1)
-			tst_brkm(TBROK | TERRNO, cleanup, "Directory close failed");
+		SAFE_CLOSE(cleanup, fd);
 	
 		check_flags();
 	}
testcases/kernel/syscalls/inotify/inotify01.c +4 -9
index 7f08a8f95..753154076 100644
@@ -38,6 +38,7 @@
 #include <string.h>
 #include <sys/syscall.h>
 #include "test.h"
+#include "safe_macros.h"
 #include "lapi/syscalls.h"
 #include "inotify.h"
 
@@ -101,9 +102,7 @@ int main(int ac, char **av)
 		event_set[tst_count] = IN_ACCESS;
 		tst_count++;
 
-		if (close(fd) == -1) {
-			tst_brkm(TBROK, cleanup, "close(%s) failed", fname);
-		}
+		SAFE_CLOSE(cleanup, fd);
 		event_set[tst_count] = IN_CLOSE_NOWRITE;
 		tst_count++;
 
@@ -121,9 +120,7 @@ int main(int ac, char **av)
 		event_set[tst_count] = IN_MODIFY;
 		tst_count++;
 
-		if (close(fd) == -1) {
-			tst_brkm(TBROK, cleanup, "close(%s) failed", fname);
-		}
+		SAFE_CLOSE(cleanup, fd);
 		event_set[tst_count] = IN_CLOSE_WRITE;
 		tst_count++;
 
@@ -212,9 +209,7 @@ static void setup(void)
 	}
 
 	/* close the file we have open */
-	if (close(fd) == -1) {
-		tst_brkm(TBROK, cleanup, "close(%s) failed", fname);
-	}
+	SAFE_CLOSE(cleanup, fd);
 	if ((fd_notify = myinotify_init()) < 0) {
 		if (errno == ENOSYS) {
 			tst_brkm(TCONF, cleanup,
testcases/kernel/syscalls/inotify/inotify02.c +2 -4
index 991342656..cb4b94e55 100644
@@ -40,6 +40,7 @@
 #include <sys/syscall.h>
 #include <limits.h>
 #include "test.h"
+#include "safe_macros.h"
 #include "lapi/syscalls.h"
 #include "inotify.h"
 
@@ -114,10 +115,7 @@ int main(int ac, char **av)
 		strcpy(event_set[tst_count].name, FILE_NAME1);
 		tst_count++;
 
-		if (close(fd) == -1) {
-			tst_brkm(TBROK | TERRNO, cleanup,
-				 "close(%s) failed", FILE_NAME1);
-		}
+		SAFE_CLOSE(cleanup, fd);
 		event_set[tst_count].mask = IN_CLOSE_WRITE;
 		strcpy(event_set[tst_count].name, FILE_NAME1);
 		tst_count++;
testcases/kernel/syscalls/inotify/inotify03.c +2 -2
index ec2f5d2bc..9393f1dbb 100644
@@ -42,6 +42,7 @@
 #include <sys/syscall.h>
 #include <signal.h>
 #include "test.h"
+#include "safe_macros.h"
 #include "lapi/syscalls.h"
 #include "inotify.h"
 
@@ -211,8 +212,7 @@ static void setup(void)
 	}
 
 	/* close the file we have open */
-	if (close(fd) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "close(%s) failed", fname);
+	SAFE_CLOSE(cleanup, fd);
 
 	fd_notify = myinotify_init();
 
testcases/kernel/syscalls/ioctl/ioctl02.c +3 -4
index c5648234b..9201ce205 100644
@@ -67,6 +67,7 @@
 #include <sys/ioctl.h>
 #include <sys/termios.h>
 #include "test.h"
+#include "safe_macros.h"
 
 #define	CNUL	0
 
@@ -183,8 +184,7 @@ int main(int ac, char **av)
 		 */
 		if (ioctl(parentfd, TCSETA, &save_io) == -1)
 			tst_resm(TINFO, "ioctl restore failed in main");
-		if (close(parentfd) == -1)
-			tst_brkm(TBROK, cleanup, "close() failed in main");
+		SAFE_CLOSE(cleanup, parentfd);
 
 		closed = 1;
 	}
@@ -443,8 +443,7 @@ static void setup(void)
 			 "do_parent_setup");
 
 	/* Close the device */
-	if (close(fd) == -1)
-		tst_brkm(TBROK, cleanup, "close failed in setup");
+	SAFE_CLOSE(cleanup, fd);
 
 	/* Set up the signal handlers */
 	act.sa_handler = (void *)sigterm_handler;
testcases/kernel/syscalls/lchown/lchown01.c +2 -3
index eaf71c7eb..06ebd0c07 100644
@@ -40,6 +40,7 @@
 #include <signal.h>
 
 #include "test.h"
+#include "safe_macros.h"
 #include "compat_16.h"
 
 #define FILE_MODE	(S_IFREG|S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
@@ -158,9 +159,7 @@ static void setup(void)
 	if ((fd = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE)) == -1) {
 		tst_brkm(TBROK, cleanup, "open failed");
 	}
-	if (close(fd) == -1) {
-		tst_brkm(TBROK | TERRNO, cleanup, "close failed");
-	}
+	SAFE_CLOSE(cleanup, fd);
 
 	if (symlink(TESTFILE, SFILE) < 0) {
 		tst_brkm(TBROK | TERRNO, cleanup, "symlink failed");
testcases/kernel/syscalls/lchown/lchown02.c +4 -7
index 765ae7a9f..5031c0b2f 100644
@@ -57,6 +57,7 @@
 #include <sys/mman.h>
 
 #include "test.h"
+#include "safe_macros.h"
 #include "compat_16.h"
 
 #define TEST_USER       "nobody"
@@ -199,8 +200,7 @@ static void setup_eperm(int pos LTP_ATTRIBUTE_UNUSED)
 	if ((fd = open(TEST_FILE1, O_RDWR | O_CREAT, 0666)) == -1)
 		tst_brkm(TBROK | TERRNO, cleanup, "open failed");
 
-	if (close(fd) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "close failed");
+	SAFE_CLOSE(cleanup, fd);
 
 	/* become root once more */
 	if (seteuid(0) == -1)
@@ -236,8 +236,7 @@ static void setup_eacces(int pos LTP_ATTRIBUTE_UNUSED)
 	if ((fd = open(TEST_FILE2, O_RDWR | O_CREAT, 0666)) == -1)
 		tst_brkm(TBROK | TERRNO, cleanup, "open failed");
 
-	if (close(fd) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "close failed");
+	SAFE_CLOSE(cleanup, fd);
 
 	/* create a symlink of testfile */
 	if (symlink(TEST_FILE2, SFILE2) < 0) {
@@ -298,9 +297,7 @@ static void setup_enotdir(int pos LTP_ATTRIBUTE_UNUSED)
 		tst_brkm(TBROK | TERRNO, cleanup, "open(2) %s failed", TFILE3);
 	}
 
-	if (close(fd) == -1) {
-		tst_brkm(TBROK | TERRNO, cleanup, "close(2) %s failed", TFILE3);
-	}
+	SAFE_CLOSE(cleanup, fd);
 }
 
 /*
testcases/kernel/syscalls/llseek/llseek02.c +2 -4
index 9227f70ed..5f2e398e1 100644
@@ -82,6 +82,7 @@
 #include <signal.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 #define TEMP_FILE1	"tmp_file1"
 #define TEMP_FILE2	"tmp_file2"
@@ -249,10 +250,7 @@ int setup2(void)
 	}
 
 	/* Close the temporary file created above */
-	if (close(fd2) < 0) {
-		tst_brkm(TBROK, cleanup, "close(%s) Failed, errno=%d : %s:",
-			 TEMP_FILE2, errno, strerror(errno));
-	}
+	SAFE_CLOSE(cleanup, fd2);
 
 	return 0;
 }
testcases/kernel/syscalls/mprotect/mprotect03.c +1 -3
index 4bda261d3..099afb6bc 100644
@@ -123,9 +123,7 @@ int main(int ac, char **av)
 		if (munmap(addr, strlen(buf)) == -1) {
 			tst_brkm(TBROK, cleanup, "munamp failed");
 		}
-		if (close(fd) == -1) {
-			tst_brkm(TBROK, cleanup, "close failed");
-		}
+		SAFE_CLOSE(cleanup, fd);
 		if (unlink(file1) == -1) {
 			tst_brkm(TBROK, cleanup, "unlink failed");
 		}
testcases/kernel/syscalls/mremap/mremap01.c +2 -3
index d63d1e456..16519a23c 100644
@@ -82,6 +82,7 @@
 #include <fcntl.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 #define TEMPFILE	"mremapfile"
 
@@ -230,9 +231,7 @@ void cleanup(void)
 		tst_brkm(TBROK | TERRNO, NULL, "munmap failed");
 
 	/* Close the temporary file */
-	if (close(fildes) < 0) {
-		tst_brkm(TBROK, NULL, "closing %s failed", TEMPFILE);
-	}
+	SAFE_CLOSE(NULL, fildes);
 
 	tst_rmdir();
 }
testcases/kernel/syscalls/munmap/munmap02.c +2 -4
index 7f1d9f4c2..3997b445c 100644
@@ -76,6 +76,7 @@
 #include <sys/mman.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 #define TEMPFILE	"mmapfile"
 
@@ -269,10 +270,7 @@ void cleanup(void)
 	}
 
 	/* Close the temporary file */
-	if (close(fildes) < 0) {
-		tst_brkm(TBROK, NULL, "close() on %s Failed, errno=%d : %s",
-			 TEMPFILE, errno, strerror(errno));
-	}
+	SAFE_CLOSE(NULL, fildes);
 
 	tst_rmdir();
 }
testcases/kernel/syscalls/pread/pread01.c +2 -4
index 94c9d9461..607fc33b3 100644
@@ -76,6 +76,7 @@
 #include <inttypes.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 #define TEMPFILE	"pread_file"
 #define K1              1024
@@ -340,10 +341,7 @@ void cleanup(void)
 	}
 
 	/* Close the temporary file */
-	if (close(fildes) < 0) {
-		tst_brkm(TBROK, NULL, "close() on %s Failed, errno=%d : %s",
-			 TEMPFILE, errno, strerror(errno));
-	}
+	SAFE_CLOSE(NULL, fildes);
 
 	tst_rmdir();
 
testcases/kernel/syscalls/pread/pread02.c +2 -4
index b9739775e..d2da21f6c 100644
@@ -72,6 +72,7 @@
 #include <fcntl.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 #define TEMPFILE	"pread_file"
 #define K1              1024
@@ -290,10 +291,7 @@ void cleanup(void)
 	}
 
 	/* Close the temporary file created in setup2 */
-	if (close(fd1) < 0) {
-		tst_brkm(TBROK, NULL, "close() on %s Failed, errno=%d : %s",
-			 TEMPFILE, errno, strerror(errno));
-	}
+	SAFE_CLOSE(NULL, fd1);
 
 	tst_rmdir();
 
testcases/kernel/syscalls/pwrite/pwrite01.c +2 -4
index 2d00fb11b..937160ae5 100644
@@ -78,6 +78,7 @@
 #include <inttypes.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 #define _XOPEN_SOURCE 500
 #define TEMPFILE	"pwrite_file"
@@ -328,10 +329,7 @@ void cleanup(void)
 	}
 
 	/* Close the temporary file */
-	if (close(fildes) < 0) {
-		tst_brkm(TBROK, NULL, "close() on %s Failed, errno=%d : %s",
-			 TEMPFILE, errno, strerror(errno));
-	}
+	SAFE_CLOSE(NULL, fildes);
 
 	tst_rmdir();
 
testcases/kernel/syscalls/read/read04.c +2 -3
index aa9a7bf89..11469ad29 100644
@@ -51,6 +51,7 @@
 #include <fcntl.h>
 #include <errno.h>
 #include "test.h"
+#include "safe_macros.h"
 
 void cleanup(void);
 void setup(void);
@@ -103,9 +104,7 @@ int main(int ac, char **av)
 		}
 		tst_resm(TPASS, "functionality of read() is correct");
 
-		if (close(rfild) == -1) {
-			tst_brkm(TBROK, cleanup, "close() failed");
-		}
+		SAFE_CLOSE(cleanup, rfild);
 	}
 
 	cleanup();
testcases/kernel/syscalls/readahead/readahead02.c +1 -2
index 7e51cd314..a5b01c756 100644
@@ -282,8 +282,7 @@ static void read_testfile(int do_readahead, const char *fname, size_t fsize,
 	time_end_usec = now.tv_sec * 1000000 + now.tv_usec;
 	*usec = time_end_usec - time_start_usec;
 
-	if (close(fd) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "close failed");
+	SAFE_CLOSE(cleanup, fd);
 }
 
 static void test_readahead(void)
testcases/kernel/syscalls/readv/readv02.c +2 -3
index d8b198182..c37e629b8 100644
@@ -47,6 +47,7 @@
 #include <errno.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 #define	K_1	1024
 #define	M_1	K_1 * K_1
@@ -212,9 +213,7 @@ void setup(void)
 		}
 	}
 
-	if (close(fd[0]) < 0) {
-		tst_brkm(TBROK, cleanup, "close failed: errno = %d", errno);
-	}
+	SAFE_CLOSE(cleanup, fd[0]);
 
 	if ((fd[0] = open(f_name, O_RDONLY, 0666)) < 0) {
 		tst_brkm(TBROK, cleanup, "open failed: fname = %s, "
testcases/kernel/syscalls/readv/readv03.c +2 -3
index 7896d9144..09a2ce4d6 100644
@@ -55,6 +55,7 @@
 #include <sys/stat.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 #define	K_1	1024
 #define MODES   S_IRWXU
@@ -141,9 +142,7 @@ void setup(void)
  */
 void cleanup(void)
 {
-	if (close(fd) < 0) {
-		tst_brkm(TBROK, cleanup, "close failed: errno = %d", errno);
-	}
+	SAFE_CLOSE(cleanup, fd);
 	tst_rmdir();
 
 }
testcases/kernel/syscalls/stat/stat03.c +3 -10
index 52db14b04..ef3991f26 100644
@@ -87,6 +87,7 @@
 #include <pwd.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 #define MODE_RWX	S_IRWXU | S_IRWXG | S_IRWXO
 #define FILE_MODE	S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
@@ -289,11 +290,7 @@ int setup1(void)
 			 TEST_FILE1, errno, strerror(errno));
 	}
 	/* Close the test file */
-	if (close(fd) == -1) {
-		tst_brkm(TBROK, cleanup,
-			 "close(%s) Failed, errno=%d : %s",
-			 TEST_FILE1, errno, strerror(errno));
-	}
+	SAFE_CLOSE(cleanup, fd);
 
 	/* Modify mode permissions on test directory */
 	if (chmod(DIR_TEMP, FILE_MODE) < 0) {
@@ -322,11 +319,7 @@ int setup2(void)
 			 errno, strerror(errno));
 	}
 	/* Close the test file created above */
-	if (close(fd) == -1) {
-		tst_brkm(TBROK, cleanup,
-			 "close(t_file) Failed, errno=%d : %s",
-			 errno, strerror(errno));
-	}
+	SAFE_CLOSE(cleanup, fd);
 	return 0;
 }
 
testcases/kernel/syscalls/symlink/symlink03.c +4 -14
index 5fa985d68..c8b178a04 100644
@@ -87,6 +87,7 @@
 #include <pwd.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 #define MODE_RWX        S_IRWXU | S_IRWXG | S_IRWXO
 #define FILE_MODE       S_IRUSR | S_IRGRP | S_IROTH
@@ -290,11 +291,7 @@ int setup1(void)
 			 "open(%s, O_RDWR|O_CREAT, 0666) failed, errno=%d : %s",
 			 TEST_FILE1, errno, strerror(errno));
 	}
-	if (close(fd) == -1) {
-		tst_brkm(TBROK, cleanup,
-			 "close(%s) Failed, errno=%d : %s",
-			 TEST_FILE1, errno, strerror(errno));
-	}
+	SAFE_CLOSE(cleanup, fd);
 
 	/* Modify mode permissions on test directory */
 	if (chmod(DIR_TEMP, FILE_MODE) < 0) {
@@ -316,11 +313,7 @@ int setup2(void)
 			 "open(%s, O_RDWR|O_CREAT, 0666) failed, errno=%d : %s",
 			 TEST_FILE1, errno, strerror(errno));
 	}
-	if (close(fd) == -1) {
-		tst_brkm(TBROK, cleanup,
-			 "close(%s) Failed, errno=%d : %s",
-			 TEST_FILE2, errno, strerror(errno));
-	}
+	SAFE_CLOSE(cleanup, fd);
 
 	if (symlink(TEST_FILE2, SYM_FILE2) < 0) {
 		tst_brkm(TBROK, cleanup,
@@ -365,10 +358,7 @@ int setup3(void)
 			 "open(2) on t_file failed, errno=%d : %s",
 			 errno, strerror(errno));
 	}
-	if (close(fd) == -1) {
-		tst_brkm(TBROK, cleanup, "close(t_file) Failed, errno=%d : %s",
-			 errno, strerror(errno));
-	}
+	SAFE_CLOSE(cleanup, fd);
 	return 0;
 }
 
testcases/kernel/syscalls/truncate/truncate01.c +2 -5
index b6782ff1f..b3a535f94 100644
@@ -77,6 +77,7 @@
 #include <inttypes.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 #define TESTFILE	"testfile"	/* file under test */
 #define FILE_MODE	S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
@@ -200,11 +201,7 @@ void setup(void)
 	}
 
 	/* Close the testfile after writing data into it */
-	if (close(fd) == -1) {
-		tst_brkm(TBROK, cleanup,
-			 "close(%s) Failed, errno=%d : %s",
-			 TESTFILE, errno, strerror(errno));
-	}
+	SAFE_CLOSE(cleanup, fd);
 }
 
 /*
Signature · 1 line
2.13.5
#2 lore raw

PATCH 3/40 Make use of SAFE_MKDIR()

In reply to Cyril Hrubis
---
 testcases/kernel/syscalls/chmod/chmod04.c       |  5 ++---
 testcases/kernel/syscalls/chmod/chmod05.c       |  4 ++--
 testcases/kernel/syscalls/fchmod/fchmod04.c     |  4 +---
 testcases/kernel/syscalls/fchmod/fchmod05.c     |  4 +---
 testcases/kernel/syscalls/inotify/inotify03.c   |  5 +----
 testcases/kernel/syscalls/lchown/lchown02.c     |  3 +--
 testcases/kernel/syscalls/mknod/mknod02.c       |  4 +---
 testcases/kernel/syscalls/mknod/mknod03.c       |  4 +---
 testcases/kernel/syscalls/mknod/mknod04.c       |  4 +---
 testcases/kernel/syscalls/mknod/mknod05.c       |  4 +---
 testcases/kernel/syscalls/mknod/mknod08.c       |  4 +---
 testcases/kernel/syscalls/open/open07.c         | 10 ++++------
 testcases/kernel/syscalls/rename/rename01.c     |  5 ++---
 testcases/kernel/syscalls/rename/rename03.c     |  9 +++------
 testcases/kernel/syscalls/rename/rename04.c     |  9 +++------
 testcases/kernel/syscalls/rename/rename05.c     |  5 ++---
 testcases/kernel/syscalls/rename/rename06.c     |  9 +++------
 testcases/kernel/syscalls/rename/rename07.c     |  5 ++---
 testcases/kernel/syscalls/rename/rename12.c     |  4 +---
 testcases/kernel/syscalls/rmdir/rmdir01.c       |  6 ++----
 testcases/kernel/syscalls/rmdir/rmdir03.c       | 20 ++++----------------
 testcases/kernel/syscalls/rmdir/rmdir04.c       |  7 ++-----
 testcases/kernel/syscalls/rmdir/rmdir05.c       |  5 ++---
 testcases/kernel/syscalls/setxattr/setxattr02.c |  5 ++---
 testcases/kernel/syscalls/stat/stat03.c         |  4 +---
 testcases/kernel/syscalls/symlink/symlink03.c   |  4 +---
 testcases/kernel/syscalls/unlink/unlink08.c     | 19 ++++---------------
 27 files changed, 51 insertions(+), 120 deletions(-)
testcases/kernel/syscalls/chmod/chmod04.c +2 -3
index 0392eac1d..cbc13cf61 100644
@@ -80,6 +80,7 @@
 #include <pwd.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 #define DIR_MODE 	S_IRWXU | S_IRWXG | S_IRWXO
 #define PERMS		01777	/*
@@ -173,9 +174,7 @@ void setup(void)
 	 * Create a test directory under temporary directory with specified
 	 * mode permissios.
 	 */
-	if (mkdir(TESTDIR, DIR_MODE) < 0) {
-		tst_brkm(TBROK, cleanup, "mkdir(2) of %s failed", TESTDIR);
-	}
+	SAFE_MKDIR(cleanup, TESTDIR, DIR_MODE);
 }
 
 /*
testcases/kernel/syscalls/chmod/chmod05.c +2 -2
index 4b0126569..028aa5d6d 100644
@@ -97,6 +97,7 @@
 #include <pwd.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 #define DEBUG 0
 
@@ -195,8 +196,7 @@ void setup(void)
 	 * mode permissions and change the gid of test directory to nobody's
 	 * gid.
 	 */
-	if (mkdir(TESTDIR, MODE_RWX) < 0)
-		tst_brkm(TBROK | TERRNO, cleanup, "mkdir(%s) failed", TESTDIR);
+	SAFE_MKDIR(cleanup, TESTDIR, MODE_RWX);
 
 	if (setgroups(1, &nobody_u->pw_gid) == -1)
 		tst_brkm(TBROK | TERRNO, cleanup,
testcases/kernel/syscalls/fchmod/fchmod04.c +1 -3
index dd79b6cb3..27e3bdcd1 100644
@@ -170,9 +170,7 @@ void setup(void)
 	 * Create a test directory under temporary directory with specified
 	 * mode permissios and open it for reading/writing.
 	 */
-	if (mkdir(TESTDIR, DIR_MODE) < 0) {
-		tst_brkm(TBROK, cleanup, "mkdir(2) of %s failed", TESTDIR);
-	}
+	SAFE_MKDIR(cleanup, TESTDIR, DIR_MODE);
 	if ((fd = open(TESTDIR, O_RDONLY)) == -1) {
 		tst_brkm(TBROK, cleanup,
 			 "open(%s, O_RDONLY) failed, errno=%d : %s",
testcases/kernel/syscalls/fchmod/fchmod05.c +1 -3
index 4ef62d4cc..290e2560a 100644
@@ -188,9 +188,7 @@ void setup(void)
 	 * mode permissions and change the gid of test directory to that of
 	 * guest user.
 	 */
-	if (mkdir(TESTDIR, MODE_RWX) < 0) {
-		tst_brkm(TBROK, cleanup, "mkdir(2) of %s failed", TESTDIR);
-	}
+	SAFE_MKDIR(cleanup, TESTDIR, MODE_RWX);
 
 	if (setgroups(1, &nobody_u->pw_gid) == -1)
 		tst_brkm(TBROK, cleanup,
testcases/kernel/syscalls/inotify/inotify03.c +1 -4
index 9393f1dbb..ae0c37e84 100644
@@ -183,10 +183,7 @@ static void setup(void)
 
 	tst_mkfs(cleanup, device, fs_type, NULL, NULL);
 
-	if (mkdir(mntpoint, DIR_MODE) < 0) {
-		tst_brkm(TBROK | TERRNO, cleanup, "mkdir(%s, %#o) failed",
-			 mntpoint, DIR_MODE);
-	}
+	SAFE_MKDIR(cleanup, mntpoint, DIR_MODE);
 
 	/* Call mount(2) */
 	tst_resm(TINFO, "mount %s to %s fs_type=%s", device, mntpoint, fs_type);
testcases/kernel/syscalls/lchown/lchown02.c +1 -2
index 5031c0b2f..245c86fa1 100644
@@ -229,8 +229,7 @@ static void setup_eacces(int pos LTP_ATTRIBUTE_UNUSED)
 	int fd;
 
 	/* create a test directory */
-	if (mkdir(DIR_TEMP, MODE_RWX) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "mkdir failed");
+	SAFE_MKDIR(cleanup, DIR_TEMP, MODE_RWX);
 
 	/* create a file under test directory */
 	if ((fd = open(TEST_FILE2, O_RDWR | O_CREAT, 0666)) == -1)
testcases/kernel/syscalls/mknod/mknod02.c +1 -3
index 85ae1409b..a0b0937a0 100644
@@ -237,9 +237,7 @@ void setup(void)
 	 * specified mode permissions, with uid/gid set to that of guest
 	 * user and the test process.
 	 */
-	if (mkdir(DIR_TEMP, MODE_RWX) < 0) {
-		tst_brkm(TBROK, cleanup, "mkdir(2) of %s failed", DIR_TEMP);
-	}
+	SAFE_MKDIR(cleanup, DIR_TEMP, MODE_RWX);
 	if (chown(DIR_TEMP, user1_uid, group2_gid) < 0) {
 		tst_brkm(TBROK, cleanup, "chown(2) of %s failed", DIR_TEMP);
 	}
testcases/kernel/syscalls/mknod/mknod03.c +1 -3
index 3d4e3c37f..3cf0af398 100644
@@ -234,9 +234,7 @@ void setup(void)
 	 * specified mode permissions, with uid/gid set to that of guest
 	 * user and the test process.
 	 */
-	if (mkdir(DIR_TEMP, MODE_RWX) < 0) {
-		tst_brkm(TBROK, cleanup, "mkdir(2) of %s failed", DIR_TEMP);
-	}
+	SAFE_MKDIR(cleanup, DIR_TEMP, MODE_RWX);
 	if (chown(DIR_TEMP, user1_uid, group2_gid) < 0) {
 		tst_brkm(TBROK, cleanup, "chown(2) of %s failed", DIR_TEMP);
 	}
testcases/kernel/syscalls/mknod/mknod04.c +1 -3
index 67c31fe58..46273bb3f 100644
@@ -236,9 +236,7 @@ void setup(void)
 	 * specified mode permissions, with uid/gid set to that of guest
 	 * user and the test process.
 	 */
-	if (mkdir(DIR_TEMP, MODE_RWX) < 0) {
-		tst_brkm(TBROK, cleanup, "mkdir(2) of %s failed", DIR_TEMP);
-	}
+	SAFE_MKDIR(cleanup, DIR_TEMP, MODE_RWX);
 	if (chown(DIR_TEMP, user1_uid, group2_gid) < 0) {
 		tst_brkm(TBROK, cleanup, "chown(2) of %s failed", DIR_TEMP);
 	}
testcases/kernel/syscalls/mknod/mknod05.c +1 -3
index 0808971ed..4edcb9394 100644
@@ -228,9 +228,7 @@ void setup(void)
 	 * specified mode permissions, with uid/gid set to that of guest
 	 * user and the test process.
 	 */
-	if (mkdir(DIR_TEMP, MODE_RWX) < 0) {
-		tst_brkm(TBROK, cleanup, "mkdir(2) of %s failed", DIR_TEMP);
-	}
+	SAFE_MKDIR(cleanup, DIR_TEMP, MODE_RWX);
 	if (chown(DIR_TEMP, user1_uid, group2_gid) < 0) {
 		tst_brkm(TBROK, cleanup, "chown(2) of %s failed", DIR_TEMP);
 	}
testcases/kernel/syscalls/mknod/mknod08.c +1 -3
index 6937da532..76bb27f17 100644
@@ -232,9 +232,7 @@ void setup(void)
 	 * specified mode permissions, with uid/gid set to that of guest
 	 * user and the test process.
 	 */
-	if (mkdir(DIR_TEMP, MODE_RWX) < 0) {
-		tst_brkm(TBROK, cleanup, "mkdir(2) of %s failed", DIR_TEMP);
-	}
+	SAFE_MKDIR(cleanup, DIR_TEMP, MODE_RWX);
 	if (chown(DIR_TEMP, user1_uid, group2_gid) < 0) {
 		tst_brkm(TBROK, cleanup, "chown(2) of %s failed", DIR_TEMP);
 	}
testcases/kernel/syscalls/open/open07.c +4 -6
index 8d99ae5c2..934d6f512 100644
@@ -65,6 +65,7 @@
 #include <sys/stat.h>
 #include <fcntl.h>
 #include "test.h"
+#include "safe_macros.h"
 
 static void setup(void);
 static void cleanup(void);
@@ -178,8 +179,7 @@ static void setupfunc_test2(void)
 
 	sprintf(file1, "open03.3.%d", getpid());
 	sprintf(file2, "open03.4.%d", getpid());
-	if (mkdir(file1, 00700) < 0)
-		tst_brkm(TBROK, cleanup, "mkdir(2) failed: errno: %d", errno);
+	SAFE_MKDIR(cleanup, file1, 00700);
 
 	if (symlink(file1, file2) < 0)
 		tst_brkm(TBROK, cleanup, "symlink(2) failed: errno: %d", errno);
@@ -214,8 +214,7 @@ static void setupfunc_test4(void)
 	sprintf(file1, "open03.8.%d", getpid());
 	sprintf(file2, "open03.9.%d", getpid());
 	sprintf(file3, "open03.10.%d", getpid());
-	if (mkdir(file1, 00700) < 0)
-		tst_brkm(TBROK, cleanup, "mkdir(2) failed: errno: %d", errno);
+	SAFE_MKDIR(cleanup, file1, 00700);
 
 	if (symlink(file1, file2) < 0)
 		tst_brkm(TBROK, cleanup, "symlink(2) failed: errno: %d", errno);
@@ -232,8 +231,7 @@ static void setupfunc_test5(void)
 
 	sprintf(file1, "open11.3.%d", getpid());
 	sprintf(file2, "open12.4.%d", getpid());
-	if (mkdir(file1, 00700) < 0)
-		tst_brkm(TBROK, cleanup, "mkdir(2) failed: errno: %d", errno);
+	SAFE_MKDIR(cleanup, file1, 00700);
 
 	if (symlink(file1, file2) < 0)
 		tst_brkm(TBROK, cleanup, "symlink(2) failed: errno: %d", errno);
testcases/kernel/syscalls/rename/rename01.c +2 -3
index f61973212..2474ab95e 100644
@@ -72,6 +72,7 @@
 #include <errno.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 void setup();
 void cleanup();
@@ -207,9 +208,7 @@ void setup(void)
 	f_oldino = buf1.st_ino;
 
 	/* create "old" directory */
-	if (mkdir(fdir, 00770) == -1) {
-		tst_brkm(TBROK, cleanup, "Could not create directory %s", fdir);
-	}
+	SAFE_MKDIR(cleanup, fdir, 00770);
 
 	if (stat(fdir, &buf1) == -1) {
 		tst_brkm(TBROK, cleanup, "failed to stat directory %s"
testcases/kernel/syscalls/rename/rename03.c +3 -6
index 1e89fea8c..b15e8fe95 100644
@@ -69,6 +69,7 @@
 #include <errno.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 void setup();
 void setup2();
@@ -218,9 +219,7 @@ void setup2(void)
 	SAFE_TOUCH(cleanup, mname, 0700, NULL);
 
 	/* create "old" directory */
-	if (mkdir(fdir, 00770) == -1) {
-		tst_brkm(TBROK, cleanup, "Could not create directory %s", fdir);
-	}
+	SAFE_MKDIR(cleanup, fdir, 00770);
 	if (stat(fdir, &buf1) == -1) {
 		tst_brkm(TBROK, cleanup, "failed to stat directory %s"
 			 "in rename()", fdir);
@@ -231,9 +230,7 @@ void setup2(void)
 	d_oldino = buf1.st_ino;
 
 	/* create another directory */
-	if (mkdir(mdir, 00770) == -1) {
-		tst_brkm(TBROK, cleanup, "Could not create directory %s", mdir);
-	}
+	SAFE_MKDIR(cleanup, mdir, 00770);
 }
 
 /*
testcases/kernel/syscalls/rename/rename04.c +3 -6
index d9c373ee3..5c6d4fda8 100644
@@ -64,6 +64,7 @@
 #include <errno.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 void setup();
 void cleanup();
@@ -147,9 +148,7 @@ void setup(void)
 	sprintf(tstfile, "%s/tstfile_%d", mdir, getpid());
 
 	/* create "old" directory */
-	if (mkdir(fdir, 00770) == -1) {
-		tst_brkm(TBROK, cleanup, "Could not create directory %s", fdir);
-	}
+	SAFE_MKDIR(cleanup, fdir, 00770);
 
 	if (stat(fdir, &buf1) == -1) {
 		tst_brkm(TBROK, cleanup, "failed to stat directory %s"
@@ -162,9 +161,7 @@ void setup(void)
 	oldino = buf1.st_ino;
 
 	/* create another directory */
-	if (mkdir(mdir, 00770) == -1) {
-		tst_brkm(TBROK, cleanup, "Could not create directory %s", mdir);
-	}
+	SAFE_MKDIR(cleanup, mdir, 00770);
 
 	SAFE_TOUCH(cleanup, tstfile, 0700, NULL);
 
testcases/kernel/syscalls/rename/rename05.c +2 -3
index 10825733f..b60da57f8 100644
@@ -62,6 +62,7 @@
 #include <errno.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 void setup();
 void cleanup();
@@ -154,9 +155,7 @@ void setup(void)
 		tst_brkm(TBROK, cleanup, "tmp directory %s found!", mdir);
 	}
 
-	if (mkdir(mdir, 00770) == -1) {
-		tst_brkm(TBROK, cleanup, "Could not create directory %s", mdir);
-	}
+	SAFE_MKDIR(cleanup, mdir, 00770);
 
 	if (stat(mdir, &buf2) == -1) {
 		tst_brkm(TBROK, cleanup, "failed to stat directory %s "
testcases/kernel/syscalls/rename/rename06.c +3 -6
index b4bdda648..8ea96fd70 100644
@@ -63,6 +63,7 @@
 #include <errno.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 void setup();
 void cleanup();
@@ -139,9 +140,7 @@ void setup(void)
 	if (stat(fdir, &buf1) != -1) {
 		tst_brkm(TBROK, cleanup, "tmp directory %s found!", fdir);
 	}
-	if (mkdir(fdir, 00770) == -1) {
-		tst_brkm(TBROK, cleanup, "Could not create directory %s", fdir);
-	}
+	SAFE_MKDIR(cleanup, fdir, 00770);
 	if (stat(fdir, &buf1) == -1) {
 		tst_brkm(TBROK, cleanup, "failed to stat directory %s "
 			 "in rename()", fdir);
@@ -155,9 +154,7 @@ void setup(void)
 	if (stat(mdir, &buf2) != -1) {
 		tst_brkm(TBROK, cleanup, "tmp directory %s found!", mdir);
 	}
-	if (mkdir(mdir, 00770) == -1) {
-		tst_brkm(TBROK, cleanup, "Could not create directory %s", mdir);
-	}
+	SAFE_MKDIR(cleanup, mdir, 00770);
 
 	if (stat(mdir, &buf2) == -1) {
 		tst_brkm(TBROK, cleanup, "failed to stat directory %s "
testcases/kernel/syscalls/rename/rename07.c +2 -3
index b483a1c62..061d73556 100644
@@ -45,6 +45,7 @@
 #include <errno.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 void setup();
 void cleanup();
@@ -122,9 +123,7 @@ void setup(void)
 		tst_brkm(TBROK, cleanup, "tmp directory %s found!", fdir);
 	}
 
-	if (mkdir(fdir, 00770) == -1) {
-		tst_brkm(TBROK, cleanup, "Could not create directory %s", fdir);
-	}
+	SAFE_MKDIR(cleanup, fdir, 00770);
 
 	if (stat(fdir, &buf1) == -1) {
 		tst_brkm(TBROK, cleanup, "failed to stat directory %s "
testcases/kernel/syscalls/rename/rename12.c +1 -3
index c620a463b..3555b41c3 100644
@@ -194,9 +194,7 @@ void setup(void)
 	sprintf(mname, "%s/rnfile_%d", fdir, getpid());
 
 	/* create a directory */
-	if (mkdir(fdir, PERMS) == -1) {
-		tst_brkm(TBROK, cleanup, "Could not create directory %s", fdir);
-	}
+	SAFE_MKDIR(cleanup, fdir, PERMS);
 
 	if (stat(fdir, &buf1) == -1) {
 		tst_brkm(TBROK, cleanup, "failed to stat directory %s", fdir);
testcases/kernel/syscalls/rmdir/rmdir01.c +2 -4
index f7e176a6e..370e60726 100644
@@ -63,6 +63,7 @@
 #include <fcntl.h>
 #include <unistd.h>
 #include "test.h"
+#include "safe_macros.h"
 
 void setup();
 void cleanup();
@@ -103,10 +104,7 @@ int main(int ac, char **av)
 		/* Initialize the test directory name */
 
 		/* create a directory */
-		if (mkdir(tstdir, PERMS) == -1) {
-			tst_brkm(TBROK, cleanup, "mkdir(%s, %#o) Failed",
-				 tstdir, PERMS);
-		}
+		SAFE_MKDIR(cleanup, tstdir, PERMS);
 		/* call rmdir using TEST macro */
 
 		TEST(rmdir(tstdir));
testcases/kernel/syscalls/rmdir/rmdir03.c +4 -16
index 8a42058f4..2bdc8bae1 100644
@@ -132,10 +132,7 @@ int main(int ac, char **av)
 				 "tmp directory %s found!", tstdir1);
 		}
 		/* create a directory */
-		if (mkdir(tstdir1, PERMS) == -1) {
-			tst_brkm(TBROK, cleanup,
-				 "Couldnot create directory %s", tstdir1);
-		}
+		SAFE_MKDIR(cleanup, tstdir1, PERMS);
 		if (stat(tstdir1, &buf1) == -1) {
 			perror("stat");
 			tst_brkm(TBROK, cleanup, "failed to stat directory %s "
@@ -150,10 +147,7 @@ int main(int ac, char **av)
 
 		}
 		/* create a sub directory under tstdir1 */
-		if (mkdir(tstdir2, PERMS) == -1) {
-			tst_brkm(TBROK, cleanup,
-				 "Could not create directory %s", tstdir2);
-		}
+		SAFE_MKDIR(cleanup, tstdir2, PERMS);
 
 		if ((pid = FORK_OR_VFORK()) == -1) {
 			tst_brkm(TBROK, cleanup, "fork() failed");
@@ -172,15 +166,9 @@ int main(int ac, char **av)
 
 //test2:       $
 		/* create the a directory with 0700 permits */
-		if (mkdir(tstdir3, 0700) == -1) {
-			tst_brkm(TBROK, cleanup, "mkdir(%s, %#o) Failed",
-				 tstdir3, PERMS);
-		}
+		SAFE_MKDIR(cleanup, tstdir3, 0700);
 		/* create the a directory with 0700 permits */
-		if (mkdir(tstdir4, 0777) == -1) {
-			tst_brkm(TBROK, cleanup, "mkdir(%s, %#o) Failed",
-				 tstdir4, PERMS);
-		}
+		SAFE_MKDIR(cleanup, tstdir4, 0777);
 
 		if ((pid = FORK_OR_VFORK()) == -1) {
 			tst_brkm(TBROK, cleanup, "fork() failed");
testcases/kernel/syscalls/rmdir/rmdir04.c +2 -5
index 1b32dafba..fe5e320ca 100644
@@ -118,6 +118,7 @@
 #include <string.h>
 #include <signal.h>
 #include "test.h"
+#include "safe_macros.h"
 
 void setup();
 void cleanup();
@@ -149,11 +150,7 @@ int main(int ac, char **av)
 
 		tst_count = 0;
 
-		if (mkdir(fname, 0777) == -1) {
-			tst_brkm(TBROK, cleanup,
-				 "mkdir(%s) Failure. errno=%d : %s", fname,
-				 errno, strerror(errno));
-		}
+		SAFE_MKDIR(cleanup, fname, 0777);
 		/*
 		 * Call rmdir(2)
 		 */
testcases/kernel/syscalls/rmdir/rmdir05.c +2 -3
index 2f0f10253..bef3b4702 100644
@@ -45,6 +45,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include "test.h"
+#include "safe_macros.h"
 
 static void setup(void);
 static void cleanup(void);
@@ -214,9 +215,7 @@ void setup(void)
 	tst_tmpdir();
 
 	/* Create a directory. */
-	if (mkdir("dir1", 0777) == -1) {
-		tst_brkm(TBROK, cleanup, "mkdir() failed to create dir1.");
-	}
+	SAFE_MKDIR(cleanup, "dir1", 0777);
 
 	/* Create a unique directory name. */
 	sprintf(dir_name, "./dir_%d", getpid());
testcases/kernel/syscalls/setxattr/setxattr02.c +2 -3
index 0ae452bc2..6b9c73213 100644
@@ -58,6 +58,7 @@
 # include <sys/xattr.h>
 #endif
 #include "test.h"
+#include "safe_macros.h"
 
 char *TCID = "setxattr02";
 
@@ -203,9 +204,7 @@ static void setup(void)
 			 FILENAME);
 	close(fd);
 
-	if (mkdir(DIRNAME, 0644) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "Create test dir(%s) failed",
-			 DIRNAME);
+	SAFE_MKDIR(cleanup, DIRNAME, 0644);
 
 	if (symlink(FILENAME, SYMLINK) == -1)
 		tst_brkm(TBROK | TERRNO, cleanup, "Create symlink(%s->%s)"
testcases/kernel/syscalls/stat/stat03.c +1 -3
index ef3991f26..0edf38646 100644
@@ -279,9 +279,7 @@ int setup1(void)
 	int fd;			/* file handle for testfile */
 
 	/* Creat a test directory */
-	if (mkdir(DIR_TEMP, MODE_RWX) < 0) {
-		tst_brkm(TBROK, cleanup, "mkdir(2) of %s failed", DIR_TEMP);
-	}
+	SAFE_MKDIR(cleanup, DIR_TEMP, MODE_RWX);
 
 	/* Creat a test file under above test directory */
 	if ((fd = open(TEST_FILE1, O_RDWR | O_CREAT, 0666)) == -1) {
testcases/kernel/syscalls/symlink/symlink03.c +1 -3
index c8b178a04..0ed4a349f 100644
@@ -282,9 +282,7 @@ int setup1(void)
 {
 	int fd;			/* file handle for testfile */
 
-	if (mkdir(DIR_TEMP, MODE_RWX) < 0) {
-		tst_brkm(TBROK, cleanup, "mkdir(2) of %s failed", DIR_TEMP);
-	}
+	SAFE_MKDIR(cleanup, DIR_TEMP, MODE_RWX);
 
 	if ((fd = open(TEST_FILE1, O_RDWR | O_CREAT, 0666)) == -1) {
 		tst_brkm(TBROK, cleanup,
testcases/kernel/syscalls/unlink/unlink08.c +4 -15
index 8e471e28b..fbe624146 100644
@@ -117,6 +117,7 @@
 #include <unistd.h>
 #include <string.h>
 #include "test.h"
+#include "safe_macros.h"
 
 void setup(void);
 void cleanup(void);
@@ -278,11 +279,7 @@ int unwrite_dir_setup(int flag)
 	int fd;
 
 	if (!flag) {		/* initial setup */
-		if (mkdir("unwrite_dir", 0777) == -1) {
-			tst_brkm(TBROK, cleanup,
-				 "mkdir(unwrite_dir, 0777) failed, errno:%d %s",
-				 errno, strerror(errno));
-		}
+		SAFE_MKDIR(cleanup, "unwrite_dir", 0777);
 
 		if ((fd = creat("unwrite_dir/file", 0777)) == -1) {
 			tst_brkm(TBROK, cleanup,
@@ -327,11 +324,7 @@ int unsearch_dir_setup(int flag)
 	int fd;
 
 	if (!flag) {		/* initial setup */
-		if (mkdir("unsearch_dir", 0777) == -1) {
-			tst_brkm(TBROK, cleanup,
-				 "mkdir(unsearch_dir, 0777) failed, errno:%d %s",
-				 errno, strerror(errno));
-		}
+		SAFE_MKDIR(cleanup, "unsearch_dir", 0777);
 
 		if ((fd = creat("unsearch_dir/file", 0777)) == -1) {
 			tst_brkm(TBROK, cleanup,
@@ -373,11 +366,7 @@ int unsearch_dir_setup(int flag)
  ******************************************************************/
 int dir_setup(int flag)
 {
-	if (mkdir("regdir", 0777) == -1) {
-		tst_brkm(TBROK, cleanup,
-			 "mkdir(unwrite_dir, 0777) failed, errno:%d %s",
-			 errno, strerror(errno));
-	}
+	SAFE_MKDIR(cleanup, "regdir", 0777);
 	return 0;
 }
 
Signature · 1 line
2.13.5
#3 lore raw

PATCH 4/40 Make use of SAFE_RMDIR()

In reply to Cyril Hrubis
---
 testcases/kernel/controllers/cgroup_xattr/cgroup_xattr.c | 5 +----
 testcases/kernel/syscalls/creat/creat08.c                | 9 +++------
 testcases/kernel/syscalls/getdents/getdents02.c          | 5 +----
 testcases/kernel/syscalls/mkdir/mkdir05.c                | 5 ++---
 testcases/kernel/syscalls/mkdir/mkdir08.c                | 7 ++-----
 testcases/kernel/syscalls/rename/rename03.c              | 5 +----
 testcases/kernel/syscalls/rename/rename09.c              | 8 ++------
 7 files changed, 12 insertions(+), 32 deletions(-)
testcases/kernel/controllers/cgroup_xattr/cgroup_xattr.c +1 -4
index a7c131f49..a0e28aa30 100644
@@ -237,10 +237,7 @@ static void cleanup(void)
 	for (i = 0; i < cgrp_opt_num; ++i) {
 		if (cgrp_opt[i].subdir) {
 			SAFE_CHDIR(NULL, cgrp_opt[i].dir);
-			if (rmdir(subdir_name) == -1) {
-				tst_brkm(TBROK | TERRNO, NULL,
-					"Can't remove dir");
-			}
+			SAFE_RMDIR(NULL, subdir_name);
 			SAFE_CHDIR(NULL, "..");
 		}
 		if (cgrp_opt[i].mounted) {
testcases/kernel/syscalls/creat/creat08.c +3 -6
index b95404c42..5f564273e 100644
@@ -52,6 +52,7 @@
 #include <grp.h>
 #include <pwd.h>
 #include "test.h"
+#include "safe_macros.h"
 
 char *TCID = "creat08";
 int TST_TOTAL = 1;
@@ -441,9 +442,7 @@ static void cleanup(void)
 	if (unlink(nosetgid_A) == -1) {
 		tst_resm(TBROK, "unlink %s failed", nosetgid_A);
 	}
-	if (rmdir(DIR_A) == -1) {
-		tst_brkm(TBROK | TERRNO, NULL, "rmdir %s failed", DIR_A);
-	}
+	SAFE_RMDIR(NULL, DIR_A);
 	if (unlink(setgid_B) == -1) {
 		tst_brkm(TBROK | TERRNO, NULL, "unlink %s failed", setgid_B);
 	}
@@ -454,9 +453,7 @@ static void cleanup(void)
 	if (unlink(nosetgid_B) == -1) {
 		tst_brkm(TBROK | TERRNO, NULL, "unlink %s failed", nosetgid_B);
 	}
-	if (rmdir(DIR_B) == -1) {
-		tst_brkm(TBROK | TERRNO, NULL, "rmdir %s failed", DIR_B);
-	}
+	SAFE_RMDIR(NULL, DIR_B);
 
 	tst_rmdir();
 }
testcases/kernel/syscalls/getdents/getdents02.c +1 -4
index 2c019e5db..7b023c53f 100644
@@ -179,10 +179,7 @@ static void test_enoent(void)
 	SAFE_MKDIR(cleanup, TEST_DIR, DIR_MODE);
 
 	fd = SAFE_OPEN(cleanup, TEST_DIR, O_DIRECTORY);
-	if (rmdir(TEST_DIR) == -1) {
-		tst_brkm(TBROK | TERRNO, cleanup,
-			 "rmdir(%s) failed", TEST_DIR);
-	}
+	SAFE_RMDIR(cleanup, TEST_DIR);
 
 	if (longsyscall)
 		getdents64(fd, &dir64, sizeof(dir64));
testcases/kernel/syscalls/mkdir/mkdir05.c +2 -3
index 6397ba56e..ee52771c8 100644
@@ -31,6 +31,7 @@
 #include <unistd.h>
 #include <pwd.h>
 #include "test.h"
+#include "safe_macros.h"
 
 void setup();
 void cleanup();
@@ -102,9 +103,7 @@ int main(int ac, char **av)
 		tst_resm(TPASS, "mkdir() functionality is correct");
 
 		/* clean up things in case we are looping */
-		if (rmdir(tstdir1) == -1) {
-			tst_brkm(TBROK, cleanup, "could not remove directory");
-		}
+		SAFE_RMDIR(cleanup, tstdir1);
 
 	}
 
testcases/kernel/syscalls/mkdir/mkdir08.c +2 -5
index 752763a47..ead218a63 100644
@@ -117,6 +117,7 @@
 #include <fcntl.h>
 #include <unistd.h>
 #include "test.h"
+#include "safe_macros.h"
 
 void setup();
 void cleanup();
@@ -159,11 +160,7 @@ int main(int ac, char **av)
 		} else {
 			tst_resm(TPASS, "mkdir(%s) returned %ld", fname,
 				 TEST_RETURN);
-			if (rmdir(fname) == -1) {
-				tst_brkm(TBROK, cleanup,
-					 "rmdir(%s) Failed, errno=%d : %s",
-					 fname, errno, strerror(errno));
-			}
+			SAFE_RMDIR(cleanup, fname);
 		}
 
 	}
testcases/kernel/syscalls/rename/rename03.c +1 -4
index b15e8fe95..4d4f78d60 100644
@@ -169,10 +169,7 @@ int main(int ac, char **av)
 		}
 
 		/* remove the new directory */
-		if (rmdir(mdir) == -1) {
-			tst_brkm(TBROK, cleanup, "Couldn't remove directory %s",
-				 mdir);
-		}
+		SAFE_RMDIR(cleanup, mdir);
 	}
 
 	cleanup();
testcases/kernel/syscalls/rename/rename09.c +2 -6
index f1a8a20e7..15e607981 100644
@@ -201,12 +201,8 @@ int main(int ac, char **av)
 			if (unlink(mname) == -1) {
 				tst_brkm(TBROK, cleanup, "unlink() #2 failed");
 			}
-			if (rmdir(fdir) == -1) {
-				tst_brkm(TBROK, cleanup, "rmdir() #1 failed");
-			}
-			if (rmdir(mdir) == -1) {
-				tst_brkm(TBROK, cleanup, "rmdir() #2 failed");
-			}
+			SAFE_RMDIR(cleanup, fdir);
+			SAFE_RMDIR(cleanup, mdir);
 		} else {
 			/* parent - let the second child carry on */
 			waitpid(pid1, &status, 0);
Signature · 1 line
2.13.5
#4 lore raw

PATCH 5/40 Make use of SAFE_MUNMAP()

In reply to Cyril Hrubis
---
 testcases/kernel/mem/mtest06/mmap1.c                      | 4 ++--
 testcases/kernel/mem/vma/vma01.c                          | 4 ++--
 testcases/kernel/syscalls/cma/process_vm01.c              | 3 +--
 testcases/kernel/syscalls/migrate_pages/migrate_pages01.c | 3 +--
 testcases/kernel/syscalls/mlock/mlock04.c                 | 4 ++--
 testcases/kernel/syscalls/mprotect/mprotect03.c           | 4 +---
 testcases/kernel/syscalls/mremap/mremap05.c               | 4 ++--
 testcases/kernel/syscalls/munmap/munmap02.c               | 5 +----
 testcases/kernel/syscalls/readahead/readahead02.c         | 3 +--
 testcases/kernel/syscalls/sendfile/sendfile04.c           | 6 +++---
 10 files changed, 16 insertions(+), 24 deletions(-)
testcases/kernel/mem/mtest06/mmap1.c +2 -2
index f3eae9994..8894b0dbf 100644
@@ -53,6 +53,7 @@
 #include <signal.h>
 #include <string.h>
 #include "test.h"
+#include "safe_macros.h"
 
 #define DISTANT_MMAP_SIZE (64*1024*1024)
 #define OPT_MISSING(prog, opt) do { \
@@ -347,8 +348,7 @@ int main(int argc, char **argv)
 		MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
 	if (distant_area == (void *)-1)
 		tst_brkm(TBROK | TERRNO, NULL, "distant_area: mmap()");
-	if (munmap(distant_area, (size_t) DISTANT_MMAP_SIZE) == -1)
-		tst_brkm(TBROK | TERRNO, NULL, "distant_area: munmap()");
+	SAFE_MUNMAP(NULL, distant_area, (size_t)DISTANT_MMAP_SIZE);
 	distant_area += DISTANT_MMAP_SIZE / 2;
 
 	if (verbose_print)
testcases/kernel/mem/vma/vma01.c +2 -2
index f800ffaf8..bafbfff46 100644
@@ -51,6 +51,7 @@
 #include <string.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 #define MAPS_FILE "/proc/self/maps"
 
@@ -155,8 +156,7 @@ static void create_bighole(void)
 		tst_brkm(TBROK | TERRNO, cleanup, "mmap");
 	memset(t, 'a', ps);
 	p = t + 3 * ps;
-	if (munmap(t, 9 * ps) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "munmap");
+	SAFE_MUNMAP(cleanup, t, 9 * ps);
 }
 
 static void *get_end_addr(void *addr_s)
testcases/kernel/syscalls/cma/process_vm01.c +1 -2
index 110db6e76..8ae16c41d 100644
@@ -396,8 +396,7 @@ static void cma_test_invalid_protection(void)
 	cma_check_ret(-1, TEST_RETURN);
 	cma_check_errno(EFAULT);
 
-	if (munmap(p, getpagesize()) < 0)
-		tst_brkm(TBROK | TERRNO, cleanup, "munmap");
+	SAFE_MUNMAP(cleanup, p, getpagesize());
 
 	cma_free_params(sane_params);
 }
testcases/kernel/syscalls/migrate_pages/migrate_pages01.c +1 -2
index 88a37219c..591ff3365 100644
@@ -117,8 +117,7 @@ static void test_invalid_mem(void)
 	check_ret(-1);
 	check_errno(EFAULT);
 
-	if (munmap(p, getpagesize()) < 0)
-		tst_brkm(TBROK | TERRNO, cleanup, "munmap");
+	SAFE_MUNMAP(cleanup, p, getpagesize());
 	tst_resm(TINFO, "test_invalid_mem unmmaped");
 	TEST(ltp_syscall(__NR_migrate_pages, 0, sane_max_node, p, p));
 	check_ret(-1);
testcases/kernel/syscalls/mlock/mlock04.c +2 -2
index a97bf491c..d1f3c9d0d 100644
@@ -31,6 +31,7 @@
  * 02110-1301, USA.
  */
 #include "test.h"
+#include "safe_macros.h"
 #include "config.h"
 
 char *TCID = "mlock04";
@@ -72,8 +73,7 @@ int main(void)
 		if (munlock(buf, file_len) == -1)
 			tst_brkm(TBROK | TERRNO, cleanup, "munlock");
 
-		if (munmap(buf, file_len) == -1)
-			tst_brkm(TBROK | TERRNO, cleanup, "munmap");
+		SAFE_MUNMAP(cleanup, buf, file_len);
 	}
 
 	tst_resm(TPASS, "test succeeded.");
testcases/kernel/syscalls/mprotect/mprotect03.c +1 -3
index 099afb6bc..944b45e87 100644
@@ -120,9 +120,7 @@ int main(int ac, char **av)
 		}
 
 		/* clean up things in case we are looping */
-		if (munmap(addr, strlen(buf)) == -1) {
-			tst_brkm(TBROK, cleanup, "munamp failed");
-		}
+		SAFE_MUNMAP(cleanup, addr, strlen(buf));
 		SAFE_CLOSE(cleanup, fd);
 		if (unlink(file1) == -1) {
 			tst_brkm(TBROK, cleanup, "unlink failed");
testcases/kernel/syscalls/mremap/mremap05.c +2 -2
index 079f281e5..d7e4e63e7 100644
@@ -41,6 +41,7 @@
 #include <errno.h>
 #include <unistd.h>
 #include "test.h"
+#include "safe_macros.h"
 
 char *TCID = "mremap05";
 
@@ -119,8 +120,7 @@ static int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]);
 
 static void free_test_area(void *p, int size)
 {
-	if (munmap(p, size) < 0)
-		tst_brkm(TBROK | TERRNO, cleanup, "free_test_area munmap");
+	SAFE_MUNMAP(cleanup, p, size);
 }
 
 static void *get_test_area(int size, int free_area)
testcases/kernel/syscalls/munmap/munmap02.c +1 -4
index 3997b445c..a85c079c3 100644
@@ -264,10 +264,7 @@ void cleanup(void)
 	map_len = map_len - page_sz;
 
 	/* unmap the portion of the region of the file left unmapped */
-	if (munmap(addr, map_len) < 0) {
-		tst_brkm(TBROK, NULL,
-			 "munmap() fails to unmap portion of mapped region");
-	}
+	SAFE_MUNMAP(NULL, addr, map_len);
 
 	/* Close the temporary file */
 	SAFE_CLOSE(NULL, fildes);
testcases/kernel/syscalls/readahead/readahead02.c +1 -2
index a5b01c756..d21bb9c45 100644
@@ -273,8 +273,7 @@ static void read_testfile(int do_readahead, const char *fname, size_t fsize,
 	if (!do_readahead)
 		*cached = get_cached_size();
 
-	if (munmap(p, fsize) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "munmap failed");
+	SAFE_MUNMAP(cleanup, p, fsize);
 
 	*read_bytes = get_bytes_read() - read_bytes_start;
 	if (gettimeofday(&now, NULL) == -1)
testcases/kernel/syscalls/sendfile/sendfile04.c +3 -3
index 822c4688c..779e7dc70 100644
@@ -57,6 +57,7 @@
 #include <netinet/in.h>
 #include <arpa/inet.h>
 #include "test.h"
+#include "safe_macros.h"
 
 #ifndef OFF_T
 #define OFF_T off_t
@@ -119,9 +120,8 @@ void do_sendfile(int prot, int pass_unmapped_buffer)
 	}
 
 	if (pass_unmapped_buffer) {
-		if (munmap(protected_buffer, sizeof(*protected_buffer)) < 0) {
-			tst_brkm(TBROK, cleanup, "munmap failed: %d", errno);
-		}
+		SAFE_MUNMAP(cleanup, protected_buffer,
+			    sizeof(*protected_buffer));
 	}
 
 	TEST(sendfile(out_fd, in_fd, protected_buffer, sb.st_size));
Signature · 1 line
2.13.5
#5 lore raw

PATCH 6/40 Make use of SAFE_PIPE()

In reply to Cyril Hrubis
---
 testcases/kernel/containers/sysvipc/shmem_2nstest.c | 7 +++----
 testcases/kernel/syscalls/close/close01.c           | 5 ++---
 testcases/kernel/syscalls/cma/process_vm_readv02.c  | 3 +--
 testcases/kernel/syscalls/cma/process_vm_readv03.c  | 3 +--
 testcases/kernel/syscalls/cma/process_vm_writev02.c | 3 +--
 testcases/kernel/syscalls/dup/dup04.c               | 3 +--
 testcases/kernel/syscalls/dup2/dup204.c             | 3 +--
 testcases/kernel/syscalls/fsync/fsync03.c           | 5 ++---
 testcases/kernel/syscalls/pipe/pipe04.c             | 5 ++---
 testcases/kernel/syscalls/pread/pread02.c           | 5 +----
 testcases/kernel/syscalls/select/select02.c         | 5 ++---
 testcases/kernel/syscalls/setrlimit/setrlimit01.c   | 4 ++--
 12 files changed, 19 insertions(+), 32 deletions(-)
testcases/kernel/containers/sysvipc/shmem_2nstest.c +3 -4
index 49fe19b6f..b172ee07c 100644
@@ -45,6 +45,7 @@
 #include <sys/shm.h>
 #include <libclone.h>
 #include "test.h"
+#include "safe_macros.h"
 #include "ipcns_helper.h"
 
 #define TESTKEY    124426L
@@ -132,10 +133,8 @@ int main(int argc, char *argv[])
 	}
 
 	/* Using PIPE's to sync between containers and Parent */
-	if (pipe(p1) == -1)
-		tst_brkm(TBROK | TERRNO, NULL, "pipe1 error");
-	if (pipe(p2) == -1)
-		tst_brkm(TBROK | TERRNO, NULL, "pipe2 error");
+	SAFE_PIPE(NULL, p1);
+	SAFE_PIPE(NULL, p2);
 
 	if (strcmp(argv[1], "clone") == 0) {
 		use_clone = T_CLONE;
testcases/kernel/syscalls/close/close01.c +2 -3
index 7b119ce44..c734ff7d2 100644
@@ -27,6 +27,7 @@
 #include <fcntl.h>
 #include <sys/stat.h>
 #include "test.h"
+#include "safe_macros.h"
 
 void cleanup(void);
 void setup(void);
@@ -72,9 +73,7 @@ int main(int ac, char **av)
 			tst_brkm(TBROK | TERRNO, cleanup,
 				 "can't dup the file des");
 
-		if (pipe(pipefildes) == -1) {
-			tst_brkm(TBROK | TERRNO, cleanup, "can't open pipe");
-		}
+		SAFE_PIPE(cleanup, pipefildes);
 
 		for (i = 0; i < TST_TOTAL; i++) {
 
testcases/kernel/syscalls/cma/process_vm_readv02.c +1 -2
index b7dd9b2e5..6bbfb62b0 100644
@@ -56,8 +56,7 @@ int main(int argc, char **argv)
 		tst_count = 0;
 		len = strlen(tst_string);
 
-		if (pipe(pipe_fd) < 0)
-			tst_brkm(TBROK | TERRNO, cleanup, "pipe");
+		SAFE_PIPE(cleanup, pipe_fd);
 
 		/* the start of child_alloc and child_invoke is already
 		 * synchronized via pipe */
testcases/kernel/syscalls/cma/process_vm_readv03.c +1 -2
index 17f0a5dcc..07a72d97d 100644
@@ -72,8 +72,7 @@ int main(int argc, char **argv)
 	for (lc = 0; TEST_LOOPING(lc); lc++) {
 		tst_count = 0;
 
-		if (pipe(pipe_fd) < 0)
-			tst_brkm(TBROK | TERRNO, cleanup, "pipe");
+		SAFE_PIPE(cleanup, pipe_fd);
 
 		bufsz_arr = SAFE_MALLOC(cleanup, nr_iovecs * sizeof(int));
 		gen_random_arr(bufsz_arr, nr_iovecs);
testcases/kernel/syscalls/cma/process_vm_writev02.c +1 -2
index 04cf6d149..c681f9f8b 100644
@@ -66,8 +66,7 @@ int main(int argc, char **argv)
 	for (lc = 0; TEST_LOOPING(lc); lc++) {
 		tst_count = 0;
 
-		if (pipe(pipe_fd) < 0)
-			tst_brkm(TBROK | TERRNO, cleanup, "pipe");
+		SAFE_PIPE(cleanup, pipe_fd);
 
 		/* the start of child_init_and_verify and child_write is
 		 * already synchronized via pipe */
testcases/kernel/syscalls/dup/dup04.c +1 -2
index 4abe451c1..1414b2a9e 100644
@@ -184,8 +184,7 @@ void setup(void)
 
 	tst_tmpdir();
 
-	if (pipe(fd) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "pipe failed");
+	SAFE_PIPE(cleanup, fd);
 }
 
 void cleanup(void)
testcases/kernel/syscalls/dup2/dup204.c +1 -2
index ab8de5d08..634bde8f2 100644
@@ -116,8 +116,7 @@ void setup(void)
 
 	tst_tmpdir();
 
-	if (pipe(fd) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "pipe failed");
+	SAFE_PIPE(cleanup, fd);
 }
 
 void cleanup(void)
testcases/kernel/syscalls/fsync/fsync03.c +2 -3
index 87c4e5ab4..60d15f429 100644
@@ -47,6 +47,7 @@
 #include <unistd.h>
 #include <errno.h>
 #include "test.h"
+#include "safe_macros.h"
 
 void setup(void);
 void cleanup(void);
@@ -122,9 +123,7 @@ void setup(void)
 	/* make a temporary directory and cd to it */
 	tst_tmpdir();
 
-	if (pipe(fd) < 0) {
-		tst_brkm(TBROK, cleanup, "pipe call failed");
-	}
+	SAFE_PIPE(cleanup, fd);
 
 	pfd = fd[1];
 }
testcases/kernel/syscalls/pipe/pipe04.c +2 -3
index dc3deade8..b3d255b33 100644
@@ -49,6 +49,7 @@
 #include <sys/types.h>
 #include <sys/wait.h>
 #include "test.h"
+#include "safe_macros.h"
 
 char *TCID = "pipe04";
 int TST_TOTAL = 1;
@@ -95,9 +96,7 @@ int main(int ac, char **av)
 		/* reset tst_count in case we are looping */
 		tst_count = 0;
 
-		if (pipe(fildes) == -1)
-			tst_brkm(TBROK, cleanup, "pipe() failed - errno %d",
-				 errno);
+		SAFE_PIPE(cleanup, fildes);
 
 		if ((c1pid = FORK_OR_VFORK()) == -1)
 			tst_brkm(TBROK, cleanup, "fork() failed - "
testcases/kernel/syscalls/pread/pread02.c +1 -4
index d2da21f6c..aa194f607 100644
@@ -212,10 +212,7 @@ int no_setup(void)
 int setup1(void)
 {
 	/* Create a pair of unnamed pipe */
-	if (pipe(pfd) < 0) {
-		tst_brkm(TBROK, cleanup, "pipe() failed to create pair of "
-			 "pipe, error:%d", errno);
-	}
+	SAFE_PIPE(cleanup, pfd);
 
 	/* Write known data (0's) of K1 bytes */
 	if (write(pfd[1], write_buf[0], K1) != K1) {
testcases/kernel/syscalls/select/select02.c +2 -3
index 3c9742777..7aa0107c0 100644
@@ -47,6 +47,7 @@
 #include <sys/time.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 static void setup(void);
 
@@ -104,9 +105,7 @@ static void setup(void)
 
 	TEST_PAUSE;
 
-	if (pipe(Fd) == -1) {
-		tst_brkm(TBROK, NULL, "pipe(&Fd) failed, errno=%d", errno);
-	}
+	SAFE_PIPE(NULL, Fd);
 
 	FD_ZERO(&saved_Readfds);
 	FD_ZERO(&saved_Writefds);
testcases/kernel/syscalls/setrlimit/setrlimit01.c +2 -2
index d0eb1b9d4..3118782f3 100644
@@ -36,6 +36,7 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include "test.h"
+#include "safe_macros.h"
 
 char *TCID = "setrlimit01";
 int TST_TOTAL = 1;
@@ -124,8 +125,7 @@ static void test2(void)
 	 */
 	int pipefd[2];
 	fflush(stdout);
-	if (pipe(pipefd) == -1)
-		tst_brkm(TBROK | TERRNO, NULL, "pipe creation failed");
+	SAFE_PIPE(NULL, pipefd);
 
 	/*
 	 * Spawn a child process, and reduce the filesize to
Signature · 1 line
2.13.5
#6 lore raw

PATCH 7/40 Make use of SAFE_SETEGID()

In reply to Cyril Hrubis
---
 testcases/kernel/syscalls/chown/chown03.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
testcases/kernel/syscalls/chown/chown03.c +1 -3
index 6188315df..4dba6c6f3 100644
@@ -170,9 +170,7 @@ void setup(void)
 	ltpuser = getpwnam(nobody_uid);
 	if (ltpuser == NULL)
 		tst_brkm(TBROK | TERRNO, NULL, "getpwnam(\"nobody\") failed");
-	if (setegid(ltpuser->pw_gid) == -1)
-		tst_brkm(TBROK | TERRNO, NULL, "setegid(%d) failed",
-			 ltpuser->pw_gid);
+	SAFE_SETEGID(NULL, ltpuser->pw_gid);
 	if (seteuid(ltpuser->pw_uid) == -1)
 		tst_brkm(TBROK | TERRNO, NULL, "seteuid(%d) failed",
 			 ltpuser->pw_uid);
Signature · 1 line
2.13.5
#7 lore raw

PATCH 8/40 Make use of SAFE_SETEUID()

In reply to Cyril Hrubis
---
 testcases/kernel/module/create_module/create_module02.c   |  6 ++----
 testcases/kernel/syscalls/adjtimex/adjtimex02.c           |  6 ++----
 testcases/kernel/syscalls/capset/capset02.c               |  5 ++---
 testcases/kernel/syscalls/chown/chown03.c                 | 10 +++-------
 testcases/kernel/syscalls/chroot/chroot01.c               |  7 +++----
 testcases/kernel/syscalls/chroot/chroot04.c               |  5 ++---
 testcases/kernel/syscalls/fchmod/fchmod03.c               |  4 ++--
 testcases/kernel/syscalls/ioperm/ioperm02.c               |  6 +++---
 testcases/kernel/syscalls/iopl/iopl02.c                   |  6 +++---
 testcases/kernel/syscalls/migrate_pages/migrate_pages02.c | 15 ++++-----------
 testcases/kernel/syscalls/mlockall/mlockall02.c           |  7 ++-----
 testcases/kernel/syscalls/mlockall/mlockall03.c           | 13 +++----------
 testcases/kernel/syscalls/move_pages/move_pages11.c       |  6 ++----
 testcases/kernel/syscalls/munlockall/munlockall02.c       |  6 ++----
 testcases/kernel/syscalls/readlink/readlink01.c           |  5 ++---
 testcases/kernel/syscalls/readlink/readlink04.c           |  7 +++----
 .../syscalls/sched_setscheduler/sched_setscheduler02.c    |  8 ++------
 testcases/kernel/syscalls/setns/setns01.c                 |  6 ++----
 testcases/kernel/syscalls/setregid/setregid03.c           |  4 ++--
 testcases/kernel/syscalls/sysctl/sysctl03.c               | 10 +++-------
 testcases/kernel/syscalls/syslog/syslog11.c               |  6 ++----
 testcases/kernel/syscalls/syslog/syslog12.c               |  6 ++----
 testcases/kernel/timers/clock_settime/clock_settime03.c   |  7 ++-----
 23 files changed, 55 insertions(+), 106 deletions(-)
testcases/kernel/module/create_module/create_module02.c +2 -4
index ff28bf2b7..f275adfb6 100644
@@ -91,6 +91,7 @@
 #include <asm/atomic.h>
 #include <linux/module.h>
 #include "test.h"
+#include "safe_macros.h"
 
 #ifndef PAGE_SIZE
 #define PAGE_SIZE sysconf(_SC_PAGE_SIZE)
@@ -210,10 +211,7 @@ int setup1(void)
 void cleanup1(void)
 {
 	/* Change effective user id to root */
-	if (seteuid(0) == -1) {
-		tst_brkm(TBROK, NULL, "seteuid failed to set the effective"
-			 " uid to root");
-	}
+	SAFE_SETEUID(NULL, 0);
 }
 
 int setup2(void)
testcases/kernel/syscalls/adjtimex/adjtimex02.c +2 -4
index 493fe55c4..577387d92 100644
@@ -91,6 +91,7 @@
 #include <unistd.h>
 #include <pwd.h>
 #include "test.h"
+#include "safe_macros.h"
 
 #define SET_MODE ( ADJ_OFFSET | ADJ_FREQUENCY | ADJ_MAXERROR | ADJ_ESTERROR | \
 	ADJ_STATUS | ADJ_TIMECONST | ADJ_TICK )
@@ -272,8 +273,5 @@ int setup6(void)
 void cleanup6(void)
 {
 	/* Set effective user id back to root */
-	if (seteuid(0) == -1) {
-		tst_brkm(TBROK | TERRNO, cleanup, "seteuid failed to set the"
-			 " effective uid to root");
-	}
+	SAFE_SETEUID(cleanup, 0);
 }
testcases/kernel/syscalls/capset/capset02.c +2 -3
index 381ab75f8..aece29511 100644
@@ -81,6 +81,7 @@
 #include <string.h>
 #include <unistd.h>
 #include "test.h"
+#include "safe_macros.h"
 #include "lapi/syscalls.h"
 
 /**************************************************************************/
@@ -251,9 +252,7 @@ void test_setup(int i, char *argv0)
 			if (ltpuser == NULL)
 				tst_brkm(TBROK | TERRNO, cleanup,
 					 "getpwnam failed");
-			if (seteuid(ltpuser->pw_uid) == -1)
-				tst_brkm(TBROK | TERRNO, cleanup,
-					 "seteuid failed");
+			SAFE_SETEUID(cleanup, ltpuser->pw_uid);
 
 		}
 		break;
testcases/kernel/syscalls/chown/chown03.c +3 -7
index 4dba6c6f3..0ca1428b2 100644
@@ -171,9 +171,7 @@ void setup(void)
 	if (ltpuser == NULL)
 		tst_brkm(TBROK | TERRNO, NULL, "getpwnam(\"nobody\") failed");
 	SAFE_SETEGID(NULL, ltpuser->pw_gid);
-	if (seteuid(ltpuser->pw_uid) == -1)
-		tst_brkm(TBROK | TERRNO, NULL, "seteuid(%d) failed",
-			 ltpuser->pw_uid);
+	SAFE_SETEUID(NULL, ltpuser->pw_uid);
 
 	/* Create a test file under temporary directory */
 	if ((fd = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE)) == -1)
@@ -181,8 +179,7 @@ void setup(void)
 			 "open(%s, O_RDWR|O_CREAT, %o) failed", TESTFILE,
 			 FILE_MODE);
 
-	if (seteuid(0) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "seteuid(0) failed");
+	SAFE_SETEUID(cleanup, 0);
 
 	if (fchown(fd, -1, 0) < 0)
 		tst_brkm(TBROK | TERRNO, cleanup, "fchown failed");
@@ -190,8 +187,7 @@ void setup(void)
 	if (fchmod(fd, NEW_PERMS) < 0)
 		tst_brkm(TBROK | TERRNO, cleanup, "fchmod failed");
 
-	if (seteuid(ltpuser->pw_uid) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "seteuid to nobody failed");
+	SAFE_SETEUID(cleanup, ltpuser->pw_uid);
 
 	SAFE_CLOSE(cleanup, fd);
 }
testcases/kernel/syscalls/chroot/chroot01.c +3 -4
index 875c8c1cf..9b0c30cf1 100644
@@ -47,6 +47,7 @@
 #include <stdio.h>
 #include <errno.h>
 #include "test.h"
+#include "safe_macros.h"
 #include <pwd.h>
 
 char *TCID = "chroot01";
@@ -98,8 +99,7 @@ void setup(void)
 		tst_brkm(TBROK | TERRNO, cleanup,
 			 "getpwnam(\"nobody\") failed");
 
-	if (seteuid(ltpuser->pw_uid) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "seteuid to nobody failed");
+	SAFE_SETEUID(cleanup, ltpuser->pw_uid);
 
 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
 
@@ -108,8 +108,7 @@ void setup(void)
 
 void cleanup(void)
 {
-	if (seteuid(0) == -1)
-		tst_brkm(TBROK | TERRNO, NULL, "setuid(0) failed");
+	SAFE_SETEUID(NULL, 0);
 
 	tst_rmdir();
 }
testcases/kernel/syscalls/chroot/chroot04.c +2 -3
index 254db0ca0..4bbdcd231 100644
@@ -48,6 +48,7 @@
 #include <errno.h>
 #include <sys/stat.h>
 #include "test.h"
+#include "safe_macros.h"
 #include <pwd.h>
 
 char *TCID = "chroot04";
@@ -114,9 +115,7 @@ void setup(void)
 	}
 
 	ltpuser = getpwnam(nobody_uid);
-	if (seteuid(ltpuser->pw_uid) == -1) {
-		tst_brkm(TBROK, cleanup, "seteuid to nobody failed");
-	}
+	SAFE_SETEUID(cleanup, ltpuser->pw_uid);
 
 }
 
testcases/kernel/syscalls/fchmod/fchmod03.c +2 -2
index a3e79b3e6..8d35bc1ef 100644
@@ -80,6 +80,7 @@
 #include <pwd.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 #define FILE_MODE       (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
 #define PERMS		01777
@@ -147,8 +148,7 @@ void setup(void)
 	ltpuser = getpwnam(nobody_uid);
 	if (ltpuser == NULL)
 		tst_brkm(TBROK | TERRNO, NULL, "getpwnam failed");
-	if (seteuid(ltpuser->pw_uid) == -1)
-		tst_brkm(TBROK | TERRNO, NULL, "seteuid failed");
+	SAFE_SETEUID(NULL, ltpuser->pw_uid);
 
 	TEST_PAUSE;
 
testcases/kernel/syscalls/ioperm/ioperm02.c +3 -3
index a25531ec3..3d9b1445f 100644
@@ -77,6 +77,7 @@ char *TCID = "ioperm02";
 #include <sys/io.h>
 #include <pwd.h>
 #include "test.h"
+#include "safe_macros.h"
 
 #define NUM_BYTES 3
 #define TURN_ON 1
@@ -179,9 +180,7 @@ int setup1(void)
 void cleanup1(void)
 {
 	/* reset user as root */
-	if (seteuid(0) == -1) {
-		tst_brkm(TBROK, NULL, "Failed to set uid as root");
-	}
+	SAFE_SETEUID(NULL, 0);
 }
 
 /* setup() - performs all ONE TIME setup for this test */
@@ -237,6 +236,7 @@ void cleanup(void)
 #else /* __i386__ */
 
 #include "test.h"
+#include "safe_macros.h"
 
 int TST_TOTAL = 0;
 
testcases/kernel/syscalls/iopl/iopl02.c +3 -3
index be3be1b25..35d239268 100644
@@ -78,6 +78,7 @@ char *TCID = "iopl02";
 #include <sys/io.h>
 #include <pwd.h>
 #include "test.h"
+#include "safe_macros.h"
 
 #define INVALID_LEVEL 4		/* Invalid privilege level */
 #define EXP_RET_VAL -1
@@ -175,9 +176,7 @@ int setup1(void)
 void cleanup1(void)
 {
 	/* reset user as root */
-	if (seteuid(0) == -1) {
-		tst_brkm(TBROK, NULL, "Failed to set uid as root");
-	}
+	SAFE_SETEUID(NULL, 0);
 }
 
 /* setup() - performs all ONE TIME setup for this test */
@@ -208,6 +207,7 @@ void cleanup(void)
 #else /* __i386__ */
 
 #include "test.h"
+#include "safe_macros.h"
 
 int TST_TOTAL = 0;
 
testcases/kernel/syscalls/migrate_pages/migrate_pages02.c +4 -11
index 4b1126eaf..d18458972 100644
@@ -207,9 +207,7 @@ static void test_migrate_current_process(int node1, int node2, int cap_sys_nice)
 		testp[0] = 1;
 		testp2[0] = 1;
 		if (!cap_sys_nice)
-			if (seteuid(ltpuser->pw_uid) == -1)
-				tst_brkm(TBROK | TERRNO, NULL,
-					 "seteuid failed");
+			SAFE_SETEUID(NULL, ltpuser->pw_uid);
 
 		migrate_to_node(0, node1);
 		/* child can migrate non-shared memory */
@@ -266,8 +264,7 @@ static void test_migrate_other_process(int node1, int node2, int cap_sys_nice)
 		migrate_to_node(0, node1);
 		check_addr_on_node(testp, node1);
 
-		if (seteuid(ltpuser->pw_uid) == -1)
-			tst_brkm(TBROK | TERRNO, NULL, "seteuid failed");
+		SAFE_SETEUID(NULL, ltpuser->pw_uid);
 
 		/* signal parent it's OK to migrate child and wait */
 		if (write(child_ready[1], &tmp, 1) != 1)
@@ -288,9 +285,7 @@ static void test_migrate_other_process(int node1, int node2, int cap_sys_nice)
 		close(pages_migrated[0]);
 
 		if (!cap_sys_nice)
-			if (seteuid(ltpuser->pw_uid) == -1)
-				tst_brkm(TBROK | TERRNO, NULL,
-					 "seteuid failed");
+			SAFE_SETEUID(NULL, ltpuser->pw_uid);
 
 		/* wait until child is ready on node1, then migrate and
 		 * signal to check current node */
@@ -309,9 +304,7 @@ static void test_migrate_other_process(int node1, int node2, int cap_sys_nice)
 
 		/* reset euid, so this testcase can be used in loop */
 		if (!cap_sys_nice)
-			if (seteuid(0) == -1)
-				tst_brkm(TBROK | TERRNO, NULL,
-					 "seteuid failed");
+			SAFE_SETEUID(NULL, 0);
 	}
 }
 
testcases/kernel/syscalls/mlockall/mlockall02.c +2 -5
index f1ec962d1..f2eca09c0 100644
@@ -77,6 +77,7 @@
 #include <pwd.h>
 #include <sys/mman.h>
 #include "test.h"
+#include "safe_macros.h"
 #include <sys/resource.h>
 
 void setup();
@@ -248,11 +249,7 @@ void cleanup_test(int i)
 		}
 		return;
 	case 1:
-		if (seteuid(0) == -1) {
-			tst_brkm(TBROK, cleanup, "seteuid() failed to "
-				 "change euid to %d errno = %d : %s",
-				 0, TEST_ERRNO, strerror(TEST_ERRNO));
-		}
+		SAFE_SETEUID(cleanup, 0);
 		return;
 
 	}
testcases/kernel/syscalls/mlockall/mlockall03.c +3 -10
index 5bdbdd051..5e8e55648 100644
@@ -79,6 +79,7 @@
 #include <ctype.h>
 #include <sys/mman.h>
 #include "test.h"
+#include "safe_macros.h"
 #include <sys/resource.h>
 #include <sys/utsname.h>
 
@@ -267,11 +268,7 @@ void cleanup_test(int i)
 
 	switch (i) {
 	case 0:
-		if (seteuid(0) == -1) {
-			tst_brkm(TBROK, cleanup, "seteuid() failed to "
-				 "change euid to %d errno = %d : %s",
-				 0, TEST_ERRNO, strerror(TEST_ERRNO));
-		}
+		SAFE_SETEUID(cleanup, 0);
 
 		rl.rlim_max = -1;
 		rl.rlim_cur = -1;
@@ -287,11 +284,7 @@ void cleanup_test(int i)
 		return;
 
 	case 1:
-		if (seteuid(0) == -1) {
-			tst_brkm(TBROK, cleanup, "seteuid() failed to "
-				 "change euid to %d errno = %d : %s",
-				 0, TEST_ERRNO, strerror(TEST_ERRNO));
-		}
+		SAFE_SETEUID(cleanup, 0);
 		return;
 
 	}
testcases/kernel/syscalls/move_pages/move_pages11.c +2 -4
index 440e11621..773e9c569 100644
@@ -59,6 +59,7 @@
 #include <errno.h>
 #include <pwd.h>
 #include "test.h"
+#include "safe_macros.h"
 #include "move_pages_support.h"
 
 #define TEST_PAGES 2
@@ -210,10 +211,7 @@ void setup(void)
 		tst_brkm(TBROK, NULL, "'nobody' user not present");
 	}
 
-	if (seteuid(ltpuser->pw_uid) == -1) {
-		tst_brkm(TBROK, NULL, "setting uid to %d failed",
-			 ltpuser->pw_uid);
-	}
+	SAFE_SETEUID(NULL, ltpuser->pw_uid);
 
 	/* Pause if that option was specified
 	 * TEST_PAUSE contains the code to fork the test with the -c option.
testcases/kernel/syscalls/munlockall/munlockall02.c +2 -4
index 648ab785d..f97905473 100644
@@ -70,6 +70,7 @@
 #include <pwd.h>
 #include <sys/mman.h>
 #include "test.h"
+#include "safe_macros.h"
 
 void setup();
 void cleanup();
@@ -126,10 +127,7 @@ void setup(void)
 		tst_brkm(TBROK, NULL, "\"nobody\"user not present");
 	}
 
-	if (seteuid(ltpuser->pw_uid) == -1) {
-		tst_brkm(TBROK, NULL, "seteuid failed to "
-			 "to set the effective uid to %d", ltpuser->pw_uid);
-	}
+	SAFE_SETEUID(NULL, ltpuser->pw_uid);
 
 	TEST_PAUSE;
 }
testcases/kernel/syscalls/readlink/readlink01.c +2 -3
index 068227551..3999a3a48 100644
@@ -72,6 +72,7 @@
 #include <pwd.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 #define TESTFILE	"testfile"
 #define SYMFILE		"slink_file"
@@ -158,9 +159,7 @@ void setup(void)
 	if ((ltpuser = getpwnam(nobody_uid)) == NULL) {
 		tst_brkm(TBROK, cleanup, "getpwname(nobody_uid) failed ");
 	}
-	if (seteuid(ltpuser->pw_uid) == -1) {
-		tst_brkm(TBROK | TERRNO, cleanup, "seteuid to nobody failed");
-	}
+	SAFE_SETEUID(cleanup, ltpuser->pw_uid);
 
 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
 
testcases/kernel/syscalls/readlink/readlink04.c +3 -4
index 62cc9e492..13175145d 100644
@@ -39,6 +39,7 @@
 #include <errno.h>
 #include <string.h>
 #include "test.h"
+#include "safe_macros.h"
 
 char *TCID = "readlink04";
 int TST_TOTAL = 1;
@@ -150,14 +151,12 @@ static void setup(void)
 	if ((pwent = getpwnam("nobody")) == NULL)
 		tst_brkm(TBROK, cleanup, "getpwname() failed for nobody");
 
-	if (seteuid(pwent->pw_uid) == -1)
-		tst_brkm(TBROK, cleanup, "seteuid() failed for nobody");
+	SAFE_SETEUID(cleanup, pwent->pw_uid);
 }
 
 static void cleanup(void)
 {
-	if (seteuid(0) == -1)
-		tst_brkm(TBROK, NULL, "failed to set process id to root");
+	SAFE_SETEUID(NULL, 0);
 
 	tst_rmdir();
 }
testcases/kernel/syscalls/sched_setscheduler/sched_setscheduler02.c +2 -6
index ba0e89428..36952d9cb 100644
@@ -88,9 +88,7 @@ int main(int ac, char **av)
 		if (pid == 0) {	/* child */
 			param.sched_priority = 1;
 
-			if (seteuid(nobody_uid) == -1) {
-				tst_brkm(TBROK, cleanup, "seteuid() failed");
-			}
+			SAFE_SETEUID(cleanup, nobody_uid);
 
 			TEST(sched_setscheduler(pid, SCHED_FIFO, &param));
 
@@ -116,9 +114,7 @@ int main(int ac, char **av)
 			}
 		}
 
-		if (seteuid(0) == -1) {
-			tst_brkm(TBROK, cleanup, "seteuid(0) failed");
-		}
+		SAFE_SETEUID(cleanup, 0);
 	}
 	cleanup();
 	tst_exit();
testcases/kernel/syscalls/setns/setns01.c +2 -4
index 5d8a49117..864899205 100644
@@ -141,8 +141,7 @@ static void setup3(struct testcase_t *t, int i)
 
 static void setup4(struct testcase_t *t, int i)
 {
-	if (seteuid(ltpuser->pw_uid) == -1)
-		tst_brkm(TBROK | TERRNO, NULL, "seteuid failed");
+	SAFE_SETEUID(NULL, ltpuser->pw_uid);
 
 	t->fd = ns_fds[i];
 	t->ns_type = ns_types[i];
@@ -150,8 +149,7 @@ static void setup4(struct testcase_t *t, int i)
 
 static void cleanup4(struct testcase_t *t)
 {
-	if (seteuid(0) == -1)
-		tst_brkm(TBROK | TERRNO, NULL, "seteuid restore failed");
+	SAFE_SETEUID(NULL, 0);
 }
 
 static void test_setns(struct testcase_t *t)
testcases/kernel/syscalls/setregid/setregid03.c +2 -2
index 2a4852288..a51719e80 100644
@@ -30,6 +30,7 @@
 #include <sys/wait.h>
 
 #include "test.h"
+#include "safe_macros.h"
 #include "compat_16.h"
 
 TCID_DEFINE(setregid03);
@@ -104,8 +105,7 @@ int main(int ac, char **av)
 		if (SETREGID(NULL, sys.gr_gid, bin.gr_gid) == -1)
 			tst_brkm(TBROK, NULL, "Initial setregid failed");
 
-		if (seteuid(nobody.pw_uid) == -1)
-			tst_brkm(TBROK, NULL, "Initial seteuid failed");
+		SAFE_SETEUID(NULL, nobody.pw_uid);
 
 		if ((pid = FORK_OR_VFORK()) == -1) {
 			tst_brkm(TBROK, NULL, "fork failed");
testcases/kernel/syscalls/sysctl/sysctl03.c +3 -7
index 68e4f86c2..0c8e691d8 100644
@@ -61,6 +61,7 @@
  *	Test must be run as root.
  */
 #include "test.h"
+#include "safe_macros.h"
 #include <sys/types.h>
 #include <sys/wait.h>
 #include <stdio.h>
@@ -146,10 +147,7 @@ int main(int ac, char **av)
 		}
 
 		/* set process ID to "ltpuser1" */
-		if (seteuid(ltpuser->pw_uid) == -1) {
-			tst_brkm(TBROK, cleanup,
-				 "seteuid() failed, errno %d", errno);
-		}
+		SAFE_SETEUID(cleanup, ltpuser->pw_uid);
 
 		if ((pid = FORK_OR_VFORK()) == -1) {
 			tst_brkm(TBROK, cleanup, "fork() failed");
@@ -182,9 +180,7 @@ int main(int ac, char **av)
 		}
 
 		/* set process ID back to root */
-		if (seteuid(0) == -1) {
-			tst_brkm(TBROK, cleanup, "seteuid() failed");
-		}
+		SAFE_SETEUID(cleanup, 0);
 	}
 	cleanup();
 	tst_exit();
testcases/kernel/syscalls/syslog/syslog11.c +2 -4
index 8d1f2c837..b9540ef47 100644
@@ -71,6 +71,7 @@
 #include <linux/unistd.h>
 #include <sys/syscall.h>
 #include "test.h"
+#include "safe_macros.h"
 
 #define UNEXP_RET_VAL	-1
 
@@ -176,10 +177,7 @@ int setup1(void)
 void cleanup1(void)
 {
 	/* Change effective user id to root */
-	if (seteuid(0) == -1) {
-		tst_brkm(TBROK, NULL, "seteuid failed to set the effective"
-			 " uid to root");
-	}
+	SAFE_SETEUID(NULL, 0);
 }
 
 /*
testcases/kernel/syscalls/syslog/syslog12.c +2 -4
index 883c3eb37..ac28d43c9 100644
@@ -81,6 +81,7 @@
 #include <linux/unistd.h>
 #include <sys/syscall.h>
 #include "test.h"
+#include "safe_macros.h"
 
 #define EXP_RET_VAL	-1
 
@@ -197,10 +198,7 @@ int setup1(void)
 void cleanup1(void)
 {
 	/* Change effective user id to root */
-	if (seteuid(0) == -1) {
-		tst_brkm(TBROK, NULL, "seteuid failed to set the effective"
-			 " uid to root");
-	}
+	SAFE_SETEUID(NULL, 0);
 }
 
 /*
testcases/kernel/timers/clock_settime/clock_settime03.c +2 -5
index b4c10de14..38b41d025 100644
@@ -22,6 +22,7 @@
 #include <unistd.h>
 
 #include "test.h"
+#include "safe_macros.h"
 #include "common_timers.h"
 
 static void setup(void);
@@ -80,11 +81,7 @@ int main(int ac, char **av)
 
 			/* Change the UID back to root */
 			if (i == TST_TOTAL - 1) {
-				if (seteuid(0) == -1) {
-					tst_brkm(TBROK | TERRNO, cleanup,
-						 "Failed to set the effective "
-						 "uid to root");
-				}
+				SAFE_SETEUID(cleanup, 0);
 			}
 
 			/* check return code */
Signature · 1 line
2.13.5
#8 lore raw

PATCH 9/40 Make use of SAFE_SETGID()

In reply to Cyril Hrubis
---
 testcases/kernel/syscalls/chmod/chmod07.c       | 4 +---
 testcases/kernel/syscalls/fchmod/fchmod02.c     | 4 +---
 testcases/kernel/syscalls/mknod/mknod02.c       | 5 +----
 testcases/kernel/syscalls/mknod/mknod03.c       | 5 +----
 testcases/kernel/syscalls/mknod/mknod04.c       | 5 +----
 testcases/kernel/syscalls/mknod/mknod08.c       | 5 +----
 testcases/kernel/syscalls/setregid/setregid02.c | 7 ++-----
 7 files changed, 8 insertions(+), 27 deletions(-)
testcases/kernel/syscalls/chmod/chmod07.c +1 -3
index 1cfec3bd0..340bdba3e 100644
@@ -187,9 +187,7 @@ void setup(void)
 	if (chown(TESTFILE, user1_uid, group1_gid) == -1)
 		tst_brkm(TBROK | TERRNO, cleanup, "chown(%s) failed", TESTFILE);
 
-	if (setgid(group1_gid) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "setgid(%d) failed",
-			 group1_gid);
+	SAFE_SETGID(cleanup, group1_gid);
 }
 
 void cleanup(void)
testcases/kernel/syscalls/fchmod/fchmod02.c +1 -3
index 207bbab8c..39519c872 100644
@@ -198,9 +198,7 @@ void setup(void)
 	}
 
 	/* Set the effective gid of the process to that of user */
-	if (setgid(group1_gid) < 0) {
-		tst_brkm(TBROK, cleanup, "setgid(2) to %d failed", group1_gid);
-	}
+	SAFE_SETGID(cleanup, group1_gid);
 }
 
 /*
testcases/kernel/syscalls/mknod/mknod02.c +1 -4
index a0b0937a0..d1b166cdb 100644
@@ -265,10 +265,7 @@ void setup(void)
 	 * Set the effective group id and user id of the test process
 	 * to that of guest user.
 	 */
-	if (setgid(group1_gid) < 0) {
-		tst_brkm(TBROK, cleanup,
-			 "Unable to set process gid to that of ltpuser");
-	}
+	SAFE_SETGID(cleanup, group1_gid);
 	if (setreuid(-1, user1_uid) < 0) {
 		tst_brkm(TBROK, cleanup,
 			 "Unable to set process uid to that of ltp user");
testcases/kernel/syscalls/mknod/mknod03.c +1 -4
index 3cf0af398..4541b93e5 100644
@@ -265,10 +265,7 @@ void setup(void)
 	 * Set the effective group id and user id of the test process
 	 * to that of guest user (nobody)
 	 */
-	if (setgid(group1_gid) < 0) {
-		tst_brkm(TBROK, cleanup,
-			 "Unable to set process gid to that of ltp user");
-	}
+	SAFE_SETGID(cleanup, group1_gid);
 	if (setreuid(-1, user1_uid) < 0) {
 		tst_brkm(TBROK, cleanup,
 			 "Unable to set process uid to that of ltp user");
testcases/kernel/syscalls/mknod/mknod04.c +1 -4
index 46273bb3f..8cbc5401b 100644
@@ -267,10 +267,7 @@ void setup(void)
 	 * Set the effective group id and user id of the test process
 	 * to that of guest user (nobody)
 	 */
-	if (setgid(group1_gid) < 0) {
-		tst_brkm(TBROK, cleanup,
-			 "Unable to set process gid to that of ltp user");
-	}
+	SAFE_SETGID(cleanup, group1_gid);
 	if (setreuid(-1, user1_uid) < 0) {
 		tst_brkm(TBROK, cleanup,
 			 "Unable to set process uid to that of ltp user");
testcases/kernel/syscalls/mknod/mknod08.c +1 -4
index 76bb27f17..97d4fd0b6 100644
@@ -260,10 +260,7 @@ void setup(void)
 	 * Set the effective group id and user id of the test process
 	 * to that of guest user.
 	 */
-	if (setgid(group1_gid) < 0) {
-		tst_brkm(TBROK, cleanup,
-			 "Unable to set process gid to that of ltp user");
-	}
+	SAFE_SETGID(cleanup, group1_gid);
 	if (setreuid(-1, user1_uid) < 0) {
 		tst_brkm(TBROK, cleanup,
 			 "Unable to set process uid to that of ltp user");
testcases/kernel/syscalls/setregid/setregid02.c +2 -5
index 9359fcb0a..0eb3c408c 100644
@@ -32,6 +32,7 @@
 #include <string.h>
 
 #include "test.h"
+#include "safe_macros.h"
 #include "compat_16.h"
 
 TCID_DEFINE(setregid02);
@@ -135,11 +136,7 @@ static void setup(void)
 	if (ltpuser == NULL)
 		tst_brkm(TBROK, NULL, "getpwnam(\"nobody\") failed");
 
-	if (setgid(ltpuser->pw_gid) == -1) {
-		tst_brkm(TBROK | TERRNO, NULL,
-			 "setgid failed to set the effective gid to %d",
-			 ltpuser->pw_gid);
-	}
+	SAFE_SETGID(NULL, ltpuser->pw_gid);
 	if (setuid(ltpuser->pw_uid) == -1) {
 		tst_brkm(TBROK | TERRNO, NULL,
 			 "setuid failed to to set the effective uid to %d",
Signature · 1 line
2.13.5
#9 lore raw

PATCH 10/40 Make use of SAFE_SETUID()

In reply to Cyril Hrubis
---
 testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat03.c          | 3 +--
 testcases/kernel/mem/hugetlb/hugeshmctl/hugeshmctl03.c        | 3 +--
 testcases/kernel/mem/hugetlb/hugeshmget/hugeshmget05.c        | 3 +--
 testcases/kernel/syscalls/chmod/chmod03.c                     | 4 +---
 testcases/kernel/syscalls/chroot/chroot04.c                   | 4 +---
 testcases/kernel/syscalls/cma/process_vm01.c                  | 4 +---
 testcases/kernel/syscalls/fstat/fstat05.c                     | 4 +---
 testcases/kernel/syscalls/get_robust_list/get_robust_list01.c | 4 ++--
 testcases/kernel/syscalls/migrate_pages/migrate_pages01.c     | 4 +---
 testcases/kernel/syscalls/open/open08.c                       | 6 ++----
 testcases/kernel/syscalls/setregid/setregid02.c               | 6 +-----
 testcases/kernel/syscalls/setresgid/setresgid03.c             | 7 ++-----
 testcases/kernel/syscalls/setreuid/setreuid03.c               | 4 ++--
 testcases/kernel/syscalls/setreuid/setreuid06.c               | 5 ++---
 14 files changed, 19 insertions(+), 42 deletions(-)
testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat03.c +1 -2
index f628710ae..960bec184 100644
@@ -66,8 +66,7 @@ static void verify_hugeshmat(void)
 
 	switch (pid = SAFE_FORK()) {
 	case 0:
-		if (setuid(ltp_uid) == -1)
-			tst_brk(TBROK | TERRNO, "setuid");
+		SAFE_SETUID(ltp_uid);
 
 		addr = shmat(shm_id_1, NULL, 0);
 		if (addr != (void *)-1) {
testcases/kernel/mem/hugetlb/hugeshmctl/hugeshmctl03.c +1 -2
index 19df79f48..f58946fa0 100644
@@ -85,8 +85,7 @@ static void test_hugeshmctl(void)
 	switch (pid = SAFE_FORK()) {
 	case 0:
 		/* set the user ID of the child to the non root user */
-		if (setuid(ltp_uid) == -1)
-			tst_brk(TBROK | TERRNO, "setuid");
+		SAFE_SETUID(ltp_uid);
 		do_child();
 		exit(0);
 	default:
testcases/kernel/mem/hugetlb/hugeshmget/hugeshmget05.c +1 -2
index b8aee0774..22138e354 100644
@@ -52,8 +52,7 @@ static void test_hugeshmget(void)
 		tst_brk(TBROK | TERRNO, "fork");
 	case 0:
 		/* set the user ID of the child to the non root user */
-		if (setuid(ltp_uid) == -1)
-			tst_brk(TBROK | TERRNO, "setuid");
+		SAFE_SETUID(ltp_uid);
 		do_child();
 		exit(0);
 	default:
testcases/kernel/syscalls/chmod/chmod03.c +1 -3
index 1ea242396..f53e437e4 100644
@@ -149,9 +149,7 @@ void setup(void)
 	ltpuser = getpwnam(nobody_uid);
 	if (ltpuser == NULL)
 		tst_brkm(TBROK | TERRNO, NULL, "getpwnam failed");
-	if (setuid(ltpuser->pw_uid) == -1)
-		tst_brkm(TBROK | TERRNO, NULL, "setuid(%u) failed",
-			 ltpuser->pw_uid);
+	SAFE_SETUID(NULL, ltpuser->pw_uid);
 
 	TEST_PAUSE;
 
testcases/kernel/syscalls/chroot/chroot04.c +1 -3
index 4bbdcd231..69fd213c8 100644
@@ -126,9 +126,7 @@ void setup(void)
 void cleanup(void)
 {
 	/* reset the process ID to the saved ID (root) */
-	if (setuid(0) == -1) {
-		tst_brkm(TBROK | TERRNO, NULL, "setuid(0) failed");
-	}
+	SAFE_SETUID(NULL, 0);
 	if (rmdir(TEST_TMPDIR) != 0) {
 		tst_brkm(TFAIL | TERRNO, NULL, "rmdir(%s) failed", TEST_TMPDIR);
 	}
testcases/kernel/syscalls/cma/process_vm01.c +1 -3
index 8ae16c41d..16a27e5b0 100644
@@ -349,9 +349,7 @@ static void cma_test_invalid_perm(void)
 		ltpuser = getpwnam(nobody_uid);
 		if (ltpuser == NULL)
 			tst_brkm(TBROK | TERRNO, NULL, "getpwnam failed");
-		if (setuid(ltpuser->pw_uid) == -1)
-			tst_brkm(TBROK | TERRNO, NULL,
-				 "setuid(%u) failed", ltpuser->pw_uid);
+		SAFE_SETUID(NULL, ltpuser->pw_uid);
 
 		params = cma_alloc_sane_params();
 		params->pid = parent_pid;
testcases/kernel/syscalls/fstat/fstat05.c +1 -3
index 69f746c73..29c253f48 100644
@@ -206,9 +206,7 @@ void setup(void)
 		signal(siglist[i], &sighandler);
 
 	ltpuser = getpwnam(nobody_uid);
-	if (setuid(ltpuser->pw_uid) == -1)
-		tst_brkm(TBROK | TERRNO, NULL, "setuid(%d) failed",
-			 ltpuser->pw_uid);
+	SAFE_SETUID(NULL, ltpuser->pw_uid);
 
 	tst_tmpdir();
 
testcases/kernel/syscalls/get_robust_list/get_robust_list01.c +2 -2
index 6aa776f6a..222dc6f2e 100644
@@ -51,6 +51,7 @@
 #include <stdlib.h>
 
 #include "test.h"
+#include "safe_macros.h"
 #include "lapi/syscalls.h"
 
 char *TCID = "get_robust_list01";
@@ -156,8 +157,7 @@ int main(int argc, char **argv)
 			tst_resm(TFAIL | TTERRNO,
 				 "get_robust_list failed unexpectedly");
 
-		if (setuid(1) == -1)
-			tst_brkm(TBROK | TERRNO, cleanup, "setuid(1) failed");
+		SAFE_SETUID(cleanup, 1);
 
 		TEST(ltp_syscall(__NR_get_robust_list, 1,
 				      (struct robust_list_head *)&head,
testcases/kernel/syscalls/migrate_pages/migrate_pages01.c +1 -3
index 591ff3365..c68f95d83 100644
@@ -182,9 +182,7 @@ static void test_invalid_perm(void)
 		ltpuser = getpwnam(nobody_uid);
 		if (ltpuser == NULL)
 			tst_brkm(TBROK | TERRNO, NULL, "getpwnam failed");
-		if (setuid(ltpuser->pw_uid) == -1)
-			tst_brkm(TBROK | TERRNO, NULL,
-				 "setuid(%u) failed", ltpuser->pw_uid);
+		SAFE_SETUID(NULL, ltpuser->pw_uid);
 		TEST(ltp_syscall(__NR_migrate_pages, parent_pid,
 			     sane_max_node, sane_old_nodes, sane_new_nodes));
 		ret |= check_ret(-1);
testcases/kernel/syscalls/open/open08.c +2 -4
index 6580b0cff..742771eef 100644
@@ -57,6 +57,7 @@
 #include <signal.h>
 #include <pwd.h>
 #include "test.h"
+#include "safe_macros.h"
 
 static void setup(void);
 static void cleanup(void);
@@ -143,10 +144,7 @@ static void setup(void)
 	if (setgid(ltpuser->pw_gid) == -1) {
 		tst_brkm(TBROK | TERRNO, NULL, "setgid(%d) failed",
 			 ltpuser->pw_gid);
-	} else if (setuid(ltpuser->pw_uid) == -1) {
-		tst_brkm(TBROK | TERRNO, NULL, "setuid(%d) failed",
-			 ltpuser->pw_uid);
-	}
+	} else SAFE_SETUID(NULL, ltpuser->pw_uid);
 
 	tst_tmpdir();
 
testcases/kernel/syscalls/setregid/setregid02.c +1 -5
index 0eb3c408c..21d1c823a 100644
@@ -137,11 +137,7 @@ static void setup(void)
 		tst_brkm(TBROK, NULL, "getpwnam(\"nobody\") failed");
 
 	SAFE_SETGID(NULL, ltpuser->pw_gid);
-	if (setuid(ltpuser->pw_uid) == -1) {
-		tst_brkm(TBROK | TERRNO, NULL,
-			 "setuid failed to to set the effective uid to %d",
-			 ltpuser->pw_uid);
-	}
+	SAFE_SETUID(NULL, ltpuser->pw_uid);
 
 	root = get_group_by_name("root");
 	ltpgroup = get_group_by_gid(ltpuser->pw_gid);
testcases/kernel/syscalls/setresgid/setresgid03.c +2 -5
index 199c1b127..b669feef7 100644
@@ -80,6 +80,7 @@
 #include <sys/types.h>
 #include <unistd.h>
 #include "test.h"
+#include "safe_macros.h"
 #include "compat_16.h"
 
 #define EXP_RET_VAL	-1
@@ -217,11 +218,7 @@ void setup(void)
 			 " setting real/effective/saved gid");
 	}
 	/* Set euid to nobody */
-	if (setuid(nobody.pw_uid) == -1) {
-		tst_brkm(TBROK, NULL, "setuid failed to "
-			 "to set the effective uid to nodody");
-
-	}
+	SAFE_SETUID(NULL, nobody.pw_uid);
 	/* Pause if that option was specified
 	 * TEST_PAUSE contains the code to fork the test with the -c option.
 	 */
testcases/kernel/syscalls/setreuid/setreuid03.c +2 -2
index b62098eb7..ecf30d6ec 100644
@@ -28,6 +28,7 @@
 #include <stdlib.h>
 
 #include "test.h"
+#include "safe_macros.h"
 #include "compat_16.h"
 
 #define FAILED  1
@@ -169,8 +170,7 @@ static void setup(void)
 	bin = *(getpwnam("bin"));
 	UID16_CHECK(bin.pw_uid, setreuid, cleanup);
 
-	if (setuid(nobody.pw_uid) < 0)
-		tst_brkm(TBROK | TERRNO, NULL, "setuid() to nobody failed");
+	SAFE_SETUID(NULL, nobody.pw_uid);
 
 	TEST_PAUSE;
 }
testcases/kernel/syscalls/setreuid/setreuid06.c +2 -3
index 8dbeb2005..22acd996f 100644
@@ -33,6 +33,7 @@
 #include <sys/stat.h>
 
 #include "test.h"
+#include "safe_macros.h"
 #include "compat_16.h"
 
 #define INVAL_USER		 (USHRT_MAX-2)
@@ -84,9 +85,7 @@ static void setup(void)
 	if (ltpuser == NULL)
 		tst_brkm(TBROK, NULL, "nobody must be a valid user.");
 
-	if (setuid(ltpuser->pw_uid) == -1)
-		tst_brkm(TBROK | TERRNO, NULL, "setuid failed to "
-			 "to set the effective uid to %d", ltpuser->pw_uid);
+	SAFE_SETUID(NULL, ltpuser->pw_uid);
 
 	TEST_PAUSE;
 }
Signature · 1 line
2.13.5
#10 lore raw

PATCH 11/40 Make use of SAFE_GETRESGID()

In reply to Cyril Hrubis
---
 testcases/kernel/syscalls/setresgid/setresgid01.c | 6 ++----
 testcases/kernel/syscalls/setresgid/setresgid02.c | 6 ++----
 testcases/kernel/syscalls/setresgid/setresgid03.c | 5 +----
 3 files changed, 5 insertions(+), 12 deletions(-)
testcases/kernel/syscalls/setresgid/setresgid01.c +2 -4
index 173ee35c3..d66c42261 100644
@@ -75,6 +75,7 @@
 #include <sys/types.h>
 #include <unistd.h>
 #include "test.h"
+#include "safe_macros.h"
 #include "compat_16.h"
 
 #define EXP_RET_VAL	0
@@ -163,10 +164,7 @@ static int test_functionality(uid_t exp_rgid, uid_t exp_egid, uid_t exp_sgid)
 	uid_t cur_rgid, cur_egid, cur_sgid;
 
 	/* Get current real, effective and saved group id's */
-	if (getresgid(&cur_rgid, &cur_egid, &cur_sgid) == -1) {
-		tst_brkm(TBROK, cleanup, "getresgid() failed");
-
-	}
+	SAFE_GETRESGID(cleanup, &cur_rgid, &cur_egid, &cur_sgid);
 
 	if ((cur_rgid == exp_rgid) && (cur_egid == exp_egid)
 	    && (cur_sgid == exp_sgid)) {
testcases/kernel/syscalls/setresgid/setresgid02.c +2 -4
index ebdaec936..2c45fc27a 100644
@@ -80,6 +80,7 @@
 #include <sys/types.h>
 #include <unistd.h>
 #include "test.h"
+#include "safe_macros.h"
 #include "compat_16.h"
 
 #define EXP_RET_VAL	0
@@ -170,10 +171,7 @@ static int test_functionality(uid_t exp_rgid, uid_t exp_egid, uid_t exp_sgid)
 	uid_t cur_rgid, cur_egid, cur_sgid;
 
 	/* Get current real, effective and saved group id */
-	if (getresgid(&cur_rgid, &cur_egid, &cur_sgid) == -1) {
-		tst_brkm(TBROK, cleanup, "getresgid() failed");
-
-	}
+	SAFE_GETRESGID(cleanup, &cur_rgid, &cur_egid, &cur_sgid);
 
 	if ((cur_rgid == exp_rgid) && (cur_egid == exp_egid)
 	    && (cur_sgid == exp_sgid)) {
testcases/kernel/syscalls/setresgid/setresgid03.c +1 -4
index b669feef7..78c89487b 100644
@@ -168,10 +168,7 @@ static int test_functionality(uid_t exp_rgid, uid_t exp_egid, uid_t exp_sgid)
 	uid_t cur_rgid, cur_egid, cur_sgid;
 
 	/* Get current real, effective and saved group id */
-	if (getresgid(&cur_rgid, &cur_egid, &cur_sgid) == -1) {
-		tst_brkm(TBROK, cleanup, "getresgid() failed");
-
-	}
+	SAFE_GETRESGID(cleanup, &cur_rgid, &cur_egid, &cur_sgid);
 
 	if ((cur_rgid == exp_rgid) && (cur_egid == exp_egid)
 	    && (cur_sgid == exp_sgid)) {
Signature · 1 line
2.13.5
#11 lore raw

PATCH 12/40 Make use of SAFE_UNLINK()

In reply to Cyril Hrubis
---
 testcases/kernel/syscalls/asyncio/asyncio02.c   |  5 +----
 testcases/kernel/syscalls/chdir/chdir01.c       |  4 +---
 testcases/kernel/syscalls/close/close08.c       |  6 ++----
 testcases/kernel/syscalls/creat/creat08.c       | 13 +++----------
 testcases/kernel/syscalls/fork/fork09.c         |  5 ++---
 testcases/kernel/syscalls/inotify/inotify02.c   |  5 +----
 testcases/kernel/syscalls/mprotect/mprotect03.c |  4 +---
 testcases/kernel/syscalls/open/open03.c         |  5 ++---
 testcases/kernel/syscalls/readv/readv02.c       |  5 +----
 testcases/kernel/syscalls/rename/rename03.c     |  4 +---
 testcases/kernel/syscalls/rename/rename09.c     |  8 ++------
 testcases/kernel/syscalls/symlink/symlink02.c   |  7 ++-----
 testcases/kernel/syscalls/symlink/symlink04.c   |  7 ++-----
 testcases/kernel/syscalls/symlink/symlink05.c   |  7 ++-----
 14 files changed, 23 insertions(+), 62 deletions(-)
testcases/kernel/syscalls/asyncio/asyncio02.c +1 -4
index 9e3ca7ad4..6f7ef9110 100644
@@ -256,10 +256,7 @@ int testrun(int flag, int bytes, int ti)
 		ret = (int)buffer.st_size;
 	}
 
-	if (unlink(filename) == -1) {
-		tst_brkm(TBROK | TERRNO, cleanup, "unlink(%s) failed",
-			 filename);
-	}
+	SAFE_UNLINK(cleanup, filename);
 
 	return ret;
 
testcases/kernel/syscalls/chdir/chdir01.c +1 -3
index 1933377a4..81cf7c564 100644
@@ -107,9 +107,7 @@ int main(int ac, char **av)
 		else
 			tst_resm(TPASS, "failed as expected with ENOTDIR");
 
-		if (unlink(filname) == -1)
-			tst_brkm(TBROK | TERRNO, cleanup,
-				 "Couldn't remove file");
+		SAFE_UNLINK(cleanup, filname);
 
 		SAFE_CHDIR(cleanup, "..");
 
testcases/kernel/syscalls/close/close08.c +2 -4
index b8e899095..ccdefa173 100644
@@ -115,6 +115,7 @@
 #include <string.h>
 #include <signal.h>
 #include "test.h"
+#include "safe_macros.h"
 
 void setup();
 void cleanup();
@@ -150,10 +151,7 @@ int main(int ac, char **av)
 				 TEST_RETURN);
 		}
 
-		if (unlink(fname) == -1) {
-			tst_brkm(TBROK | TERRNO, cleanup,
-				 "unlink(%s) failed", fname);
-		}
+		SAFE_UNLINK(cleanup, fname);
 	}
 
 	cleanup();
testcases/kernel/syscalls/creat/creat08.c +3 -10
index 5f564273e..284ae5f1a 100644
@@ -443,16 +443,9 @@ static void cleanup(void)
 		tst_resm(TBROK, "unlink %s failed", nosetgid_A);
 	}
 	SAFE_RMDIR(NULL, DIR_A);
-	if (unlink(setgid_B) == -1) {
-		tst_brkm(TBROK | TERRNO, NULL, "unlink %s failed", setgid_B);
-	}
-	if (unlink(root_setgid_B) == -1) {
-		tst_brkm(TBROK | TERRNO, NULL, "unlink %s failed",
-			 root_setgid_B);
-	}
-	if (unlink(nosetgid_B) == -1) {
-		tst_brkm(TBROK | TERRNO, NULL, "unlink %s failed", nosetgid_B);
-	}
+	SAFE_UNLINK(NULL, setgid_B);
+	SAFE_UNLINK(NULL, root_setgid_B);
+	SAFE_UNLINK(NULL, nosetgid_B);
 	SAFE_RMDIR(NULL, DIR_B);
 
 	tst_rmdir();
testcases/kernel/syscalls/fork/fork09.c +2 -3
index 35ba01507..dedc8a41c 100644
@@ -47,6 +47,7 @@
 #include <errno.h>
 #include <unistd.h>		/* for _SC_OPEN_MAX */
 #include "test.h"
+#include "safe_macros.h"
 
 char *TCID = "fork09";
 int TST_TOTAL = 1;
@@ -91,9 +92,7 @@ int main(int ac, char **av)
 
 		tst_resm(TINFO, "first file descriptor is %d ", first);
 
-		if (unlink(filname) == -1)
-			tst_brkm(TBROK, cleanup, "Cannot unlink file %s, "
-				 "errno = %d", filname, errno);
+		SAFE_UNLINK(cleanup, filname);
 
 		/*
 		 * now open all the files for the test
testcases/kernel/syscalls/inotify/inotify02.c +1 -4
index cb4b94e55..ff5be64ae 100644
@@ -146,10 +146,7 @@ int main(int ac, char **av)
 		strcpy(event_set[tst_count].name, "");
 		tst_count++;
 
-		if (unlink(FILE_NAME2) == -1) {
-			tst_brkm(TBROK | TERRNO, cleanup,
-				 "unlink(%s) failed", FILE_NAME2);
-		}
+		SAFE_UNLINK(cleanup, FILE_NAME2);
 		event_set[tst_count].mask = IN_DELETE;
 		strcpy(event_set[tst_count].name, FILE_NAME2);
 		tst_count++;
testcases/kernel/syscalls/mprotect/mprotect03.c +1 -3
index 944b45e87..28f54c9ae 100644
@@ -122,9 +122,7 @@ int main(int ac, char **av)
 		/* clean up things in case we are looping */
 		SAFE_MUNMAP(cleanup, addr, strlen(buf));
 		SAFE_CLOSE(cleanup, fd);
-		if (unlink(file1) == -1) {
-			tst_brkm(TBROK, cleanup, "unlink failed");
-		}
+		SAFE_UNLINK(cleanup, file1);
 	}
 
 	cleanup();
testcases/kernel/syscalls/open/open03.c +2 -3
index 7e82a9ae7..80e43c6e9 100644
@@ -31,6 +31,7 @@
 #include <string.h>
 #include <signal.h>
 #include "test.h"
+#include "safe_macros.h"
 
 static void setup(void);
 static void cleanup(void);
@@ -66,9 +67,7 @@ int main(int ac, char **av)
 			if (close(fd) == -1)
 				tst_brkm(TBROK | TERRNO, cleanup,
 					 "close(%s) failed", fname);
-			else if (unlink(fname) == -1)
-				tst_brkm(TBROK | TERRNO, cleanup,
-					 "unlink(%s) failed", fname);
+			else SAFE_UNLINK(cleanup, fname);
 		}
 	}
 
testcases/kernel/syscalls/readv/readv02.c +1 -4
index c37e629b8..be67c4ece 100644
@@ -236,10 +236,7 @@ void setup(void)
  */
 void cleanup(void)
 {
-	if (unlink(f_name) < 0) {
-		tst_brkm(TBROK, NULL, "unlink FAILED: file %s, errno %d",
-			 f_name, errno);
-	}
+	SAFE_UNLINK(NULL, f_name);
 	tst_rmdir();
 
 }
testcases/kernel/syscalls/rename/rename03.c +1 -3
index 4d4f78d60..6ffbe5628 100644
@@ -164,9 +164,7 @@ int main(int ac, char **av)
 		/* reset things in case we are looping */
 
 		/* unlink the new file */
-		if (unlink(mname) == -1) {
-			tst_brkm(TBROK, cleanup, "unlink() failed");
-		}
+		SAFE_UNLINK(cleanup, mname);
 
 		/* remove the new directory */
 		SAFE_RMDIR(cleanup, mdir);
testcases/kernel/syscalls/rename/rename09.c +2 -6
index 15e607981..9b2022592 100644
@@ -195,12 +195,8 @@ int main(int ac, char **av)
 			}
 
 			/* clean up things in case we are looping */
-			if (unlink(fname) == -1) {
-				tst_brkm(TBROK, cleanup, "unlink() #1 failed");
-			}
-			if (unlink(mname) == -1) {
-				tst_brkm(TBROK, cleanup, "unlink() #2 failed");
-			}
+			SAFE_UNLINK(cleanup, fname);
+			SAFE_UNLINK(cleanup, mname);
 			SAFE_RMDIR(cleanup, fdir);
 			SAFE_RMDIR(cleanup, mdir);
 		} else {
testcases/kernel/syscalls/symlink/symlink02.c +2 -5
index fdf8e609c..c18db2b37 100644
@@ -115,6 +115,7 @@
 #include <string.h>
 #include <signal.h>
 #include "test.h"
+#include "safe_macros.h"
 
 void setup();
 void cleanup();
@@ -157,11 +158,7 @@ int main(int ac, char **av)
 				 fname, symlnk, TEST_ERRNO,
 				 strerror(TEST_ERRNO));
 		} else {
-			if (unlink(symlnk) == -1) {
-				tst_brkm(TBROK, cleanup,
-					 "unlink(%s) Failed, errno=%d : %s",
-					 symlnk, errno, strerror(errno));
-			}
+			SAFE_UNLINK(cleanup, symlnk);
 		}
 	}
 
testcases/kernel/syscalls/symlink/symlink04.c +2 -5
index ef284007c..2190b3b1b 100644
@@ -76,6 +76,7 @@
 #include <signal.h>
 #include <sys/stat.h>
 #include "test.h"
+#include "safe_macros.h"
 
 #define  TESTFILE	"testfile"
 #define  SYMFILE	"slink_file"
@@ -134,11 +135,7 @@ int main(int ac, char **av)
 		}
 
 		/* Unlink the symlink file for next loop */
-		if (unlink(SYMFILE) == -1) {
-			tst_brkm(TBROK, cleanup,
-				 "unlink(%s) Failed, errno=%d : %s",
-				 SYMFILE, errno, strerror(errno));
-		}
+		SAFE_UNLINK(cleanup, SYMFILE);
 		tst_count++;	/* incr TEST_LOOP counter */
 	}
 
testcases/kernel/syscalls/symlink/symlink05.c +2 -5
index 89f8c8877..83b151f5d 100644
@@ -77,6 +77,7 @@
 #include <sys/stat.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 #define  TESTFILE	"testfile"
 #define  SYMFILE	"slink_file"
@@ -135,11 +136,7 @@ int main(int ac, char **av)
 		}
 
 		/* Unlink the symlink file for next loop */
-		if (unlink(SYMFILE) == -1) {
-			tst_brkm(TBROK, cleanup,
-				 "unlink(%s) Failed, errno=%d : %s",
-				 SYMFILE, errno, strerror(errno));
-		}
+		SAFE_UNLINK(cleanup, SYMFILE);
 		tst_count++;	/* incr TEST_LOOP counter */
 	}
 
Signature · 1 line
2.13.5
#12 lore raw

PATCH 13/40 Make use of SAFE_LINK()

In reply to Cyril Hrubis
---
 testcases/kernel/syscalls/rename/rename13.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
testcases/kernel/syscalls/rename/rename13.c +2 -4
index a42beba35..72394a2d7 100644
@@ -65,6 +65,7 @@
 #include <errno.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 void setup();
 void cleanup();
@@ -179,10 +180,7 @@ void setup(void)
 	oldino = buf1.st_ino;
 
 	/* link the "new" file to the "old" file */
-	if (link(fname, mname) == -1) {
-		tst_brkm(TBROK, cleanup,
-			 "link from %s to %s failed!", fname, mname);
-	}
+	SAFE_LINK(cleanup, fname, mname);
 }
 
 /*
Signature · 1 line
2.13.5
#13 lore raw

PATCH 14/40 Make use of SAFE_SYMLINK()

In reply to Cyril Hrubis
---
 .../kernel/security/prot_hsymlinks/prot_hsymlinks.c |  6 +-----
 testcases/kernel/syscalls/lchown/lchown01.c         |  4 +---
 testcases/kernel/syscalls/lchown/lchown02.c         |  8 ++------
 testcases/kernel/syscalls/open/open07.c             | 21 +++++++--------------
 testcases/kernel/syscalls/readlink/readlink01.c     |  5 +----
 testcases/kernel/syscalls/readlink/readlink04.c     |  5 +----
 testcases/kernel/syscalls/setxattr/setxattr02.c     |  4 +---
 testcases/kernel/syscalls/symlink/symlink03.c       |  6 +-----
 8 files changed, 15 insertions(+), 44 deletions(-)
testcases/kernel/security/prot_hsymlinks/prot_hsymlinks.c +1 -5
index ecfbd1533..058d7f982 100644
@@ -453,11 +453,7 @@ static int create_check_slinks(const struct user_file *ufile, int owner)
 			slink_info.in_sticky = bdirs[dir].sticky;
 			slink_info.dir_owner = bdirs[dir].owner;
 
-			if (symlink(ufile->path, slink_info.path) == -1) {
-				tst_brkm(TBROK, cleanup,
-					"Can't create symlink: %s",
-					slink_info.path);
-			}
+			SAFE_SYMLINK(cleanup, ufile->path, slink_info.path);
 			result |= check_symlink(&slink_info);
 		}
 	}
testcases/kernel/syscalls/lchown/lchown01.c +1 -3
index 06ebd0c07..89c85e987 100644
@@ -161,9 +161,7 @@ static void setup(void)
 	}
 	SAFE_CLOSE(cleanup, fd);
 
-	if (symlink(TESTFILE, SFILE) < 0) {
-		tst_brkm(TBROK | TERRNO, cleanup, "symlink failed");
-	}
+	SAFE_SYMLINK(cleanup, TESTFILE, SFILE);
 }
 
 static void cleanup(void)
testcases/kernel/syscalls/lchown/lchown02.c +2 -6
index 245c86fa1..b5992a105 100644
@@ -207,8 +207,7 @@ static void setup_eperm(int pos LTP_ATTRIBUTE_UNUSED)
 		tst_resm(TBROK | TERRNO, "setuid(0) failed");
 
 	/* create symling to testfile */
-	if (symlink(TEST_FILE1, SFILE1) < 0)
-		tst_brkm(TBROK | TERRNO, cleanup, "symlink failed");
+	SAFE_SYMLINK(cleanup, TEST_FILE1, SFILE1);
 
 	/* back to the user nobody */
 	if (seteuid(ltpuser->pw_uid) == -1)
@@ -238,10 +237,7 @@ static void setup_eacces(int pos LTP_ATTRIBUTE_UNUSED)
 	SAFE_CLOSE(cleanup, fd);
 
 	/* create a symlink of testfile */
-	if (symlink(TEST_FILE2, SFILE2) < 0) {
-		tst_brkm(TBROK | TERRNO, cleanup, "symlink(2) %s to %s failed",
-			 TEST_FILE2, SFILE2);
-	}
+	SAFE_SYMLINK(cleanup, TEST_FILE2, SFILE2);
 
 	/* modify mode permissions on test directory */
 	if (chmod(DIR_TEMP, FILE_MODE) < 0) {
testcases/kernel/syscalls/open/open07.c +7 -14
index 934d6f512..b941f4b81 100644
@@ -167,8 +167,7 @@ static void setupfunc_test1(void)
 	if (fd1 < 0)
 		tst_brkm(TBROK, cleanup, "creat(2) failed: errno: %d", errno);
 
-	if (symlink(file1, file2) < 0)
-		tst_brkm(TBROK, cleanup, "symlink(2) failed: errno: %d", errno);
+	SAFE_SYMLINK(cleanup, file1, file2);
 
 	strcpy(TC[0].filename, file2);
 }
@@ -181,8 +180,7 @@ static void setupfunc_test2(void)
 	sprintf(file2, "open03.4.%d", getpid());
 	SAFE_MKDIR(cleanup, file1, 00700);
 
-	if (symlink(file1, file2) < 0)
-		tst_brkm(TBROK, cleanup, "symlink(2) failed: errno: %d", errno);
+	SAFE_SYMLINK(cleanup, file1, file2);
 
 	strcpy(TC[1].filename, file2);
 }
@@ -198,11 +196,9 @@ static void setupfunc_test3(void)
 	if (fd2 < 0)
 		tst_brkm(TBROK, cleanup, "creat(2) failed: errno: %d", errno);
 
-	if (symlink(file1, file2) < 0)
-		tst_brkm(TBROK, cleanup, "symlink(2) failed: errno: %d", errno);
+	SAFE_SYMLINK(cleanup, file1, file2);
 
-	if (symlink(file2, file3) < 0)
-		tst_brkm(TBROK, cleanup, "symlink(2) failed: errno: %d", errno);
+	SAFE_SYMLINK(cleanup, file2, file3);
 
 	strcpy(TC[2].filename, file3);
 }
@@ -216,11 +212,9 @@ static void setupfunc_test4(void)
 	sprintf(file3, "open03.10.%d", getpid());
 	SAFE_MKDIR(cleanup, file1, 00700);
 
-	if (symlink(file1, file2) < 0)
-		tst_brkm(TBROK, cleanup, "symlink(2) failed: errno: %d", errno);
+	SAFE_SYMLINK(cleanup, file1, file2);
 
-	if (symlink(file2, file3) < 0)
-		tst_brkm(TBROK, cleanup, "symlink(2) failed: errno: %d", errno);
+	SAFE_SYMLINK(cleanup, file2, file3);
 
 	strcpy(TC[3].filename, file3);
 }
@@ -233,8 +227,7 @@ static void setupfunc_test5(void)
 	sprintf(file2, "open12.4.%d", getpid());
 	SAFE_MKDIR(cleanup, file1, 00700);
 
-	if (symlink(file1, file2) < 0)
-		tst_brkm(TBROK, cleanup, "symlink(2) failed: errno: %d", errno);
+	SAFE_SYMLINK(cleanup, file1, file2);
 
 	strcpy(TC[4].filename, file2);
 	strcat(TC[4].filename, "/");
testcases/kernel/syscalls/readlink/readlink01.c +1 -4
index 3999a3a48..5f6448a2b 100644
@@ -178,10 +178,7 @@ void setup(void)
 	}
 
 	/* Create a symlink of testfile under temporary directory */
-	if (symlink(TESTFILE, SYMFILE) < 0) {
-		tst_brkm(TBROK | TERRNO, cleanup, "symlink(%s, %s) failed",
-			 TESTFILE, SYMFILE);
-	}
+	SAFE_SYMLINK(cleanup, TESTFILE, SYMFILE);
 }
 
 /*
testcases/kernel/syscalls/readlink/readlink04.c +1 -4
index 13175145d..8e1582142 100644
@@ -136,10 +136,7 @@ static void setup(void)
 	if (close(fd))
 		tst_brkm(TBROK|TERRNO, cleanup, "close(%s) failed", TESTFILE);
 
-	if (symlink(TESTFILE, SYMFILE) < 0) {
-		tst_brkm(TBROK|TERRNO, cleanup, "symlink(%s, %s) failed",
-		         TESTFILE, SYMFILE);
-	}
+	SAFE_SYMLINK(cleanup, TESTFILE, SYMFILE);
 
 	/* set up the expected return value from the readlink() call */
 	exp_val = strlen(TESTFILE);
testcases/kernel/syscalls/setxattr/setxattr02.c +1 -3
index 6b9c73213..b16ddce94 100644
@@ -206,9 +206,7 @@ static void setup(void)
 
 	SAFE_MKDIR(cleanup, DIRNAME, 0644);
 
-	if (symlink(FILENAME, SYMLINK) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "Create symlink(%s->%s)"
-			 " failed", SYMLINK, FILENAME);
+	SAFE_SYMLINK(cleanup, FILENAME, SYMLINK);
 
 	if (mknod(FIFO, S_IFIFO | 0777, 0) == -1)
 		tst_brkm(TBROK | TERRNO, cleanup, "Create FIFO(%s) failed",
testcases/kernel/syscalls/symlink/symlink03.c +1 -5
index 0ed4a349f..dc6d12262 100644
@@ -313,11 +313,7 @@ int setup2(void)
 	}
 	SAFE_CLOSE(cleanup, fd);
 
-	if (symlink(TEST_FILE2, SYM_FILE2) < 0) {
-		tst_brkm(TBROK, cleanup,
-			 "symlink() Fails to create %s in setup2, error=%d",
-			 SYM_FILE2, errno);
-	}
+	SAFE_SYMLINK(cleanup, TEST_FILE2, SYM_FILE2);
 	return 0;
 }
 
Signature · 1 line
2.13.5
#14 lore raw

PATCH 15/40 Make use of SAFE_CHMOD()

In reply to Cyril Hrubis
---
 lib/tst_tmpdir.c                              |  7 ++----
 testcases/kernel/syscalls/chown/chown02.c     |  4 +--
 testcases/kernel/syscalls/inotify/inotify01.c |  5 +---
 testcases/kernel/syscalls/inotify/inotify02.c |  5 +---
 testcases/kernel/syscalls/lchown/lchown02.c   |  5 +---
 testcases/kernel/syscalls/lstat/lstat02.c     |  3 +--
 testcases/kernel/syscalls/mknod/mknod03.c     |  4 +--
 testcases/kernel/syscalls/mknod/mknod04.c     |  4 +--
 testcases/kernel/syscalls/mknod/mknod05.c     |  4 +--
 testcases/kernel/syscalls/stat/stat02.c       |  6 ++---
 testcases/kernel/syscalls/stat/stat03.c       |  4 +--
 testcases/kernel/syscalls/statfs/statfs03.c   |  5 ++--
 testcases/kernel/syscalls/symlink/symlink03.c |  8 ++----
 testcases/kernel/syscalls/unlink/unlink08.c   | 36 +++++----------------------
 14 files changed, 23 insertions(+), 77 deletions(-)
lib/tst_tmpdir.c +2 -5
index 824caded5..5cd554361 100644
@@ -71,6 +71,7 @@
 #include <fcntl.h>
 
 #include "test.h"
+#include "safe_macros.h"
 #include "ltp_priv.h"
 #include "lapi/futex.h"
 
@@ -276,11 +277,7 @@ void tst_tmpdir(void)
 		return;
 	}
 
-	if (chmod(TESTDIR, DIR_MODE) == -1) {
-		tst_brkm(TBROK | TERRNO, NULL,
-			 "chmod(%s, %#o) failed", TESTDIR, DIR_MODE);
-		return;
-	}
+	SAFE_CHMOD(NULL, TESTDIR, DIR_MODE);
 
 	if (getcwd(test_start_work_dir, sizeof(test_start_work_dir)) == NULL) {
 		tst_resm(TINFO, "Failed to record test working dir");
testcases/kernel/syscalls/chown/chown02.c +1 -3
index f1d905e22..a459f092b 100644
@@ -242,9 +242,7 @@ int setup1(void)
 	SAFE_CLOSE(cleanup, fd);
 
 	/* Set setuid/setgid bits on the test file created */
-	if (chmod(TESTFILE1, NEW_PERMS1) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "chmod(%s, ..) failed",
-			 TESTFILE1);
+	SAFE_CHMOD(cleanup, TESTFILE1, NEW_PERMS1);
 	return 0;
 }
 
testcases/kernel/syscalls/inotify/inotify01.c +1 -4
index 753154076..8a471ae5f 100644
@@ -81,10 +81,7 @@ int main(int ac, char **av)
 		/*
 		 * generate sequence of events
 		 */
-		if (chmod(fname, 0755) < 0) {
-			tst_brkm(TBROK | TERRNO, cleanup,
-				 "chmod(%s, 0755) failed", fname);
-		}
+		SAFE_CHMOD(cleanup, fname, 0755);
 		event_set[tst_count] = IN_ATTRIB;
 		tst_count++;
 
testcases/kernel/syscalls/inotify/inotify02.c +1 -4
index ff5be64ae..2c92f1616 100644
@@ -95,10 +95,7 @@ int main(int ac, char **av)
 		/*
 		 * generate sequence of events
 		 */
-		if (chmod(".", 0755) < 0) {
-			tst_brkm(TBROK | TERRNO, cleanup,
-				 "chmod(\".\", 0755) failed");
-		}
+		SAFE_CHMOD(cleanup, ".", 0755);
 		event_set[tst_count].mask = IN_ISDIR | IN_ATTRIB;
 		strcpy(event_set[tst_count].name, "");
 		tst_count++;
testcases/kernel/syscalls/lchown/lchown02.c +1 -4
index b5992a105..326e584c9 100644
@@ -240,10 +240,7 @@ static void setup_eacces(int pos LTP_ATTRIBUTE_UNUSED)
 	SAFE_SYMLINK(cleanup, TEST_FILE2, SFILE2);
 
 	/* modify mode permissions on test directory */
-	if (chmod(DIR_TEMP, FILE_MODE) < 0) {
-		tst_brkm(TBROK | TERRNO, cleanup, "chmod(2) %s failed",
-			 DIR_TEMP);
-	}
+	SAFE_CHMOD(cleanup, DIR_TEMP, FILE_MODE);
 }
 
 /*
testcases/kernel/syscalls/lstat/lstat02.c +1 -2
index afc2c7c3c..ab4678704 100644
@@ -124,8 +124,7 @@ static void setup(void)
 
 	SAFE_MKDIR(cleanup, TEST_DIR, MODE_RWX);
 	SAFE_TOUCH(cleanup, TEST_EACCES, 0666, NULL);
-	if (chmod(TEST_DIR, FILE_MODE) < 0)
-		tst_brkm(TBROK, cleanup, "chmod(2) of %s failed", TEST_DIR);
+	SAFE_CHMOD(cleanup, TEST_DIR, FILE_MODE);
 
 	SAFE_TOUCH(cleanup, "test_file", MODE_RWX, NULL);
 
testcases/kernel/syscalls/mknod/mknod03.c +1 -3
index 4541b93e5..919327a65 100644
@@ -238,9 +238,7 @@ void setup(void)
 	if (chown(DIR_TEMP, user1_uid, group2_gid) < 0) {
 		tst_brkm(TBROK, cleanup, "chown(2) of %s failed", DIR_TEMP);
 	}
-	if (chmod(DIR_TEMP, MODE_SGID) < 0) {
-		tst_brkm(TBROK, cleanup, "chmod(2) of %s failed", DIR_TEMP);
-	}
+	SAFE_CHMOD(cleanup, DIR_TEMP, MODE_SGID);
 
 	/*
 	 * Verify that test directory created with expected permission modes
testcases/kernel/syscalls/mknod/mknod04.c +1 -3
index 8cbc5401b..205bdcb36 100644
@@ -240,9 +240,7 @@ void setup(void)
 	if (chown(DIR_TEMP, user1_uid, group2_gid) < 0) {
 		tst_brkm(TBROK, cleanup, "chown(2) of %s failed", DIR_TEMP);
 	}
-	if (chmod(DIR_TEMP, MODE_SGID) < 0) {
-		tst_brkm(TBROK, cleanup, "chmod(2) of %s failed", DIR_TEMP);
-	}
+	SAFE_CHMOD(cleanup, DIR_TEMP, MODE_SGID);
 
 	/*
 	 * Verify that test directory created with expected permission modes
testcases/kernel/syscalls/mknod/mknod05.c +1 -3
index 4edcb9394..a7a5ea8ee 100644
@@ -232,9 +232,7 @@ void setup(void)
 	if (chown(DIR_TEMP, user1_uid, group2_gid) < 0) {
 		tst_brkm(TBROK, cleanup, "chown(2) of %s failed", DIR_TEMP);
 	}
-	if (chmod(DIR_TEMP, MODE_SGID) < 0) {
-		tst_brkm(TBROK, cleanup, "chmod(2) of %s failed", DIR_TEMP);
-	}
+	SAFE_CHMOD(cleanup, DIR_TEMP, MODE_SGID);
 
 	/*
 	 * Verify that test directory created with expected permission modes
testcases/kernel/syscalls/stat/stat02.c +2 -4
index 1b7d60f53..aeaa7ab82 100644
@@ -77,6 +77,7 @@
 #include <pwd.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 #define FILE_MODE	S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
 #define TESTFILE	"testfile"
@@ -207,10 +208,7 @@ void setup(void)
 	}
 
 	/* Modify mode permissions on the testfile */
-	if (chmod(TESTFILE, NEW_MODE) < 0) {
-		tst_brkm(TBROK | TERRNO, cleanup, "chmodding %s failed",
-			 TESTFILE);
-	}
+	SAFE_CHMOD(cleanup, TESTFILE, NEW_MODE);
 
 	/* Get the uid/gid of the process */
 	user_id = getuid();
testcases/kernel/syscalls/stat/stat03.c +1 -3
index 0edf38646..ef3d93742 100644
@@ -291,9 +291,7 @@ int setup1(void)
 	SAFE_CLOSE(cleanup, fd);
 
 	/* Modify mode permissions on test directory */
-	if (chmod(DIR_TEMP, FILE_MODE) < 0) {
-		tst_brkm(TBROK, cleanup, "chmod(2) of %s failed", DIR_TEMP);
-	}
+	SAFE_CHMOD(cleanup, DIR_TEMP, FILE_MODE);
 	return 0;
 }
 
testcases/kernel/syscalls/statfs/statfs03.c +2 -3
index ab9720885..32aaf830e 100644
@@ -54,6 +54,7 @@
 #include <errno.h>
 #include <stdio.h>
 #include "test.h"
+#include "safe_macros.h"
 #include <pwd.h>
 
 char *TCID = "statfs03";
@@ -119,9 +120,7 @@ void setup(void)
 
 	/* make a temporary directory and cd to it */
 	tst_tmpdir();
-	if (chmod(tst_get_tmpdir(), S_IRWXU) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "chmod(%s, 700) failed",
-			 tst_get_tmpdir());
+	SAFE_CHMOD(cleanup, tst_get_tmpdir(), S_IRWXU);
 
 	/* create a test file */
 	sprintf(fname, "%s.%d", fname, getpid());
testcases/kernel/syscalls/symlink/symlink03.c +2 -6
index dc6d12262..89f23c952 100644
@@ -292,9 +292,7 @@ int setup1(void)
 	SAFE_CLOSE(cleanup, fd);
 
 	/* Modify mode permissions on test directory */
-	if (chmod(DIR_TEMP, FILE_MODE) < 0) {
-		tst_brkm(TBROK, cleanup, "chmod(2) of %s failed", DIR_TEMP);
-	}
+	SAFE_CHMOD(cleanup, DIR_TEMP, FILE_MODE);
 	return 0;
 }
 
@@ -367,9 +365,7 @@ void cleanup(void)
 {
 
 	/* Restore mode permissions on test directory created in setup2() */
-	if (chmod(DIR_TEMP, MODE_RWX) < 0) {
-		tst_brkm(TBROK, NULL, "chmod(2) of %s failed", DIR_TEMP);
-	}
+	SAFE_CHMOD(NULL, DIR_TEMP, MODE_RWX);
 
 	tst_rmdir();
 
testcases/kernel/syscalls/unlink/unlink08.c +6 -30
index fbe624146..9829a7c11 100644
@@ -288,17 +288,9 @@ int unwrite_dir_setup(int flag)
 		}
 		close(fd);
 
-		if (chmod("unwrite_dir", 0555) == -1) {
-			tst_brkm(TBROK, cleanup,
-				 "chmod(unwrite_dir, 0555) failed, errno:%d %s",
-				 errno, strerror(errno));
-		}
+		SAFE_CHMOD(cleanup, "unwrite_dir", 0555);
 	} else {		/* resetup */
-		if (chmod("unwrite_dir", 0777) == -1) {
-			tst_brkm(TBROK, cleanup,
-				 "chmod(unwrite_dir, 0777) failed, errno:%d %s",
-				 errno, strerror(errno));
-		}
+		SAFE_CHMOD(cleanup, "unwrite_dir", 0777);
 
 		if ((fd = creat("unwrite_dir/file", 0777)) == -1) {
 			tst_brkm(TBROK, cleanup,
@@ -307,11 +299,7 @@ int unwrite_dir_setup(int flag)
 		}
 		close(fd);
 
-		if (chmod("unwrite_dir", 0555) == -1) {
-			tst_brkm(TBROK, cleanup,
-				 "chmod(unwrite_dir, 0555) failed, errno:%d %s",
-				 errno, strerror(errno));
-		}
+		SAFE_CHMOD(cleanup, "unwrite_dir", 0555);
 	}
 	return 0;
 }
@@ -333,17 +321,9 @@ int unsearch_dir_setup(int flag)
 		}
 		close(fd);
 
-		if (chmod("unsearch_dir", 0666) == -1) {
-			tst_brkm(TBROK, cleanup,
-				 "chmod(unsearch_dir, 0666) failed, errno:%d %s",
-				 errno, strerror(errno));
-		}
+		SAFE_CHMOD(cleanup, "unsearch_dir", 0666);
 	} else {		/* resetup */
-		if (chmod("unsearch_dir", 0777) == -1) {
-			tst_brkm(TBROK, cleanup,
-				 "chmod(unsearch_dir, 0777) failed, errno:%d %s",
-				 errno, strerror(errno));
-		}
+		SAFE_CHMOD(cleanup, "unsearch_dir", 0777);
 
 		if ((fd = creat("unsearch_dir/file", 0777)) == -1) {
 			tst_brkm(TBROK, cleanup,
@@ -352,11 +332,7 @@ int unsearch_dir_setup(int flag)
 		}
 		close(fd);
 
-		if (chmod("unsearch_dir", 0666) == -1) {
-			tst_brkm(TBROK, cleanup,
-				 "chmod(unsearch_dir, 0666) failed, errno:%d %s",
-				 errno, strerror(errno));
-		}
+		SAFE_CHMOD(cleanup, "unsearch_dir", 0666);
 	}
 	return 0;
 }
Signature · 1 line
2.13.5
#15 lore raw

PATCH 16/40 Make use of SAFE_FCHMOD()

In reply to Cyril Hrubis
---
 testcases/kernel/syscalls/chown/chown03.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
testcases/kernel/syscalls/chown/chown03.c +1 -2
index 0ca1428b2..b88b6699a 100644
@@ -184,8 +184,7 @@ void setup(void)
 	if (fchown(fd, -1, 0) < 0)
 		tst_brkm(TBROK | TERRNO, cleanup, "fchown failed");
 
-	if (fchmod(fd, NEW_PERMS) < 0)
-		tst_brkm(TBROK | TERRNO, cleanup, "fchmod failed");
+	SAFE_FCHMOD(cleanup, fd, NEW_PERMS);
 
 	SAFE_SETEUID(cleanup, ltpuser->pw_uid);
 
Signature · 1 line
2.13.5
#16 lore raw

PATCH 17/40 Make use of SAFE_CHOWN()

In reply to Cyril Hrubis
---
 lib/tst_tmpdir.c                                | 6 +-----
 testcases/kernel/syscalls/chmod/chmod05.c       | 4 +---
 testcases/kernel/syscalls/chmod/chmod07.c       | 3 +--
 testcases/kernel/syscalls/fchmod/fchmod02.c     | 4 +---
 testcases/kernel/syscalls/fchmod/fchmod05.c     | 4 +---
 testcases/kernel/syscalls/mknod/mknod02.c       | 4 +---
 testcases/kernel/syscalls/mknod/mknod03.c       | 4 +---
 testcases/kernel/syscalls/mknod/mknod04.c       | 4 +---
 testcases/kernel/syscalls/mknod/mknod05.c       | 4 +---
 testcases/kernel/syscalls/mknod/mknod08.c       | 4 +---
 testcases/kernel/syscalls/readlink/readlink04.c | 3 +--
 11 files changed, 11 insertions(+), 33 deletions(-)
lib/tst_tmpdir.c +1 -5
index 5cd554361..09b7b6e22 100644
@@ -271,11 +271,7 @@ void tst_tmpdir(void)
 		return;
 	}
 
-	if (chown(TESTDIR, -1, getgid()) == -1) {
-		tst_brkm(TBROK | TERRNO, NULL,
-			 "chown(%s, -1, %d) failed", TESTDIR, getgid());
-		return;
-	}
+	SAFE_CHOWN(NULL, TESTDIR, -1, getgid());
 
 	SAFE_CHMOD(NULL, TESTDIR, DIR_MODE);
 
testcases/kernel/syscalls/chmod/chmod05.c +1 -3
index 028aa5d6d..3cf4db5d0 100644
@@ -202,9 +202,7 @@ void setup(void)
 		tst_brkm(TBROK | TERRNO, cleanup,
 			 "setgroups to nobody's gid failed");
 
-	if (chown(TESTDIR, nobody_u->pw_uid, bin_group->gr_gid) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup,
-			 "chowning testdir to nobody:bin failed");
+	SAFE_CHOWN(cleanup, TESTDIR, nobody_u->pw_uid, bin_group->gr_gid);
 
 	/* change to nobody:nobody */
 	if (setegid(nobody_u->pw_gid) == -1 || seteuid(nobody_u->pw_uid) == -1)
testcases/kernel/syscalls/chmod/chmod07.c +1 -2
index 340bdba3e..42272414a 100644
@@ -184,8 +184,7 @@ void setup(void)
 			 "open(%s, O_RDWR|O_CREAT, %#o) failed",
 			 TESTFILE, FILE_MODE);
 	SAFE_CLOSE(cleanup, fd);
-	if (chown(TESTFILE, user1_uid, group1_gid) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "chown(%s) failed", TESTFILE);
+	SAFE_CHOWN(cleanup, TESTFILE, user1_uid, group1_gid);
 
 	SAFE_SETGID(cleanup, group1_gid);
 }
testcases/kernel/syscalls/fchmod/fchmod02.c +1 -3
index 39519c872..ea2bdb4b8 100644
@@ -193,9 +193,7 @@ void setup(void)
 			 TESTFILE, FILE_MODE, errno, strerror(errno));
 	}
 
-	if (chown(TESTFILE, user1_uid, group1_gid) < 0) {
-		tst_brkm(TBROK, cleanup, "chown(2) of %s failed", TESTFILE);
-	}
+	SAFE_CHOWN(cleanup, TESTFILE, user1_uid, group1_gid);
 
 	/* Set the effective gid of the process to that of user */
 	SAFE_SETGID(cleanup, group1_gid);
testcases/kernel/syscalls/fchmod/fchmod05.c +1 -3
index 290e2560a..0ab2f935d 100644
@@ -195,9 +195,7 @@ void setup(void)
 			 "Couldn't change supplementary group Id: %s",
 			 strerror(errno));
 
-	if (chown(TESTDIR, nobody_u->pw_uid, bin_group->gr_gid) == -1)
-		tst_brkm(TBROK, cleanup, "Couldn't change owner of testdir: %s",
-			 strerror(errno));
+	SAFE_CHOWN(cleanup, TESTDIR, nobody_u->pw_uid, bin_group->gr_gid);
 
 	/* change to nobody:nobody */
 	if (setegid(nobody_u->pw_gid) == -1 || seteuid(nobody_u->pw_uid) == -1)
testcases/kernel/syscalls/mknod/mknod02.c +1 -3
index d1b166cdb..7ac8db5ef 100644
@@ -238,9 +238,7 @@ void setup(void)
 	 * user and the test process.
 	 */
 	SAFE_MKDIR(cleanup, DIR_TEMP, MODE_RWX);
-	if (chown(DIR_TEMP, user1_uid, group2_gid) < 0) {
-		tst_brkm(TBROK, cleanup, "chown(2) of %s failed", DIR_TEMP);
-	}
+	SAFE_CHOWN(cleanup, DIR_TEMP, user1_uid, group2_gid);
 
 	/*
 	 * Verify that test directory created with expected permission modes
testcases/kernel/syscalls/mknod/mknod03.c +1 -3
index 919327a65..8fd58a732 100644
@@ -235,9 +235,7 @@ void setup(void)
 	 * user and the test process.
 	 */
 	SAFE_MKDIR(cleanup, DIR_TEMP, MODE_RWX);
-	if (chown(DIR_TEMP, user1_uid, group2_gid) < 0) {
-		tst_brkm(TBROK, cleanup, "chown(2) of %s failed", DIR_TEMP);
-	}
+	SAFE_CHOWN(cleanup, DIR_TEMP, user1_uid, group2_gid);
 	SAFE_CHMOD(cleanup, DIR_TEMP, MODE_SGID);
 
 	/*
testcases/kernel/syscalls/mknod/mknod04.c +1 -3
index 205bdcb36..9615c15a6 100644
@@ -237,9 +237,7 @@ void setup(void)
 	 * user and the test process.
 	 */
 	SAFE_MKDIR(cleanup, DIR_TEMP, MODE_RWX);
-	if (chown(DIR_TEMP, user1_uid, group2_gid) < 0) {
-		tst_brkm(TBROK, cleanup, "chown(2) of %s failed", DIR_TEMP);
-	}
+	SAFE_CHOWN(cleanup, DIR_TEMP, user1_uid, group2_gid);
 	SAFE_CHMOD(cleanup, DIR_TEMP, MODE_SGID);
 
 	/*
testcases/kernel/syscalls/mknod/mknod05.c +1 -3
index a7a5ea8ee..57f097b9e 100644
@@ -229,9 +229,7 @@ void setup(void)
 	 * user and the test process.
 	 */
 	SAFE_MKDIR(cleanup, DIR_TEMP, MODE_RWX);
-	if (chown(DIR_TEMP, user1_uid, group2_gid) < 0) {
-		tst_brkm(TBROK, cleanup, "chown(2) of %s failed", DIR_TEMP);
-	}
+	SAFE_CHOWN(cleanup, DIR_TEMP, user1_uid, group2_gid);
 	SAFE_CHMOD(cleanup, DIR_TEMP, MODE_SGID);
 
 	/*
testcases/kernel/syscalls/mknod/mknod08.c +1 -3
index 97d4fd0b6..39c024833 100644
@@ -233,9 +233,7 @@ void setup(void)
 	 * user and the test process.
 	 */
 	SAFE_MKDIR(cleanup, DIR_TEMP, MODE_RWX);
-	if (chown(DIR_TEMP, user1_uid, group2_gid) < 0) {
-		tst_brkm(TBROK, cleanup, "chown(2) of %s failed", DIR_TEMP);
-	}
+	SAFE_CHOWN(cleanup, DIR_TEMP, user1_uid, group2_gid);
 
 	/*
 	 * Verify that test directory created with expected permission modes
testcases/kernel/syscalls/readlink/readlink04.c +1 -2
index 8e1582142..9c6c58914 100644
@@ -123,8 +123,7 @@ static void setup(void)
 		tst_brkm(TBROK, cleanup, "getpwname() failed");
 
 	/* make the tmp directory belong to bin */
-	if (chown(tmp_dir, pwent->pw_uid, pwent->pw_gid) == -1)
-		tst_brkm(TBROK, cleanup, "chown() failed");
+	SAFE_CHOWN(cleanup, tmp_dir, pwent->pw_uid, pwent->pw_gid);
 
 	if (chmod(tmp_dir, 0711) != 0)
 		tst_brkm(TBROK|TERRNO, cleanup, "chmod(%s) failed", tmp_dir);
Signature · 1 line
2.13.5
#17 lore raw

PATCH 18/40 Make use of SAFE_FCHOWN()

In reply to Cyril Hrubis
---
 testcases/kernel/syscalls/chown/chown03.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
testcases/kernel/syscalls/chown/chown03.c +1 -2
index b88b6699a..2c7bcfe7d 100644
@@ -181,8 +181,7 @@ void setup(void)
 
 	SAFE_SETEUID(cleanup, 0);
 
-	if (fchown(fd, -1, 0) < 0)
-		tst_brkm(TBROK | TERRNO, cleanup, "fchown failed");
+	SAFE_FCHOWN(cleanup, fd, -1, 0);
 
 	SAFE_FCHMOD(cleanup, fd, NEW_PERMS);
 
Signature · 1 line
2.13.5
#18 lore raw

PATCH 19/40 Make use of SAFE_WAIT()

In reply to Cyril Hrubis
---
 testcases/kernel/containers/pidns/pidns20.c     |  5 ++---
 testcases/kernel/syscalls/alarm/alarm03.c       |  5 ++---
 testcases/kernel/syscalls/alarm/alarm07.c       |  5 ++---
 testcases/kernel/syscalls/execve/execve05.c     |  9 +++------
 testcases/kernel/syscalls/flock/flock04.c       |  5 ++---
 testcases/kernel/syscalls/getpid/getpid02.c     |  5 ++---
 testcases/kernel/syscalls/getppid/getppid02.c   |  5 ++---
 testcases/kernel/syscalls/ipc/semctl/semctl01.c |  4 ++--
 testcases/kernel/syscalls/openat/openat02.c     |  3 +--
 testcases/kernel/syscalls/sigrelse/sigrelse01.c |  3 +--
 testcases/kernel/syscalls/unshare/unshare01.c   | 13 ++++---------
 testcases/kernel/syscalls/unshare/unshare02.c   | 11 +++--------
 12 files changed, 26 insertions(+), 47 deletions(-)
testcases/kernel/containers/pidns/pidns20.c +2 -3
index 630e6c393..70e9356fd 100644
@@ -48,6 +48,7 @@
 #include <stdio.h>
 #include "pidns_helper.h"
 #include "test.h"
+#include "safe_macros.h"
 
 char *TCID = "pidns20";
 int TST_TOTAL = 1;
@@ -189,9 +190,7 @@ int main(int argc, char *argv[])
 	}
 
 	/* collect exit status of child */
-	if (wait(&status) == -1) {
-		tst_brkm(TBROK | TERRNO, NULL, "wait failed");
-	}
+	SAFE_WAIT(NULL, &status);
 
 	if (WIFSIGNALED(status)) {
 		if (WTERMSIG(status) == SIGUSR1)
testcases/kernel/syscalls/alarm/alarm03.c +2 -3
index 5853a29e6..60141f1ad 100644
@@ -98,6 +98,7 @@
 #include <stdlib.h>
 #include <sys/wait.h>
 #include "test.h"
+#include "safe_macros.h"
 
 void setup();
 void cleanup();
@@ -160,9 +161,7 @@ int main(int ac, char **av)
 					 "alarm(100), fork, alarm(0) parent's alarm returned %ld",
 					 TEST_RETURN);
 			}
-			if (wait(&status) == -1)
-				tst_brkm(TBROK | TERRNO, cleanup,
-					 "wait failed");
+			SAFE_WAIT(cleanup, &status);
 			if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
 				tst_resm(TFAIL, "see failures reported above");
 
testcases/kernel/syscalls/alarm/alarm07.c +2 -3
index b270309f6..5f4d6e44e 100644
@@ -71,6 +71,7 @@
 #include <sys/wait.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 char *TCID = "alarm07";
 int TST_TOTAL = 1;
@@ -121,9 +122,7 @@ int main(int ac, char **av)
 			}
 		} else {
 			/* Wait for child to complete execution */
-			if (wait(&status) == -1)
-				tst_brkm(TBROK | TERRNO, cleanup,
-					 "wait failed");
+			SAFE_WAIT(cleanup, &status);
 			if (!WIFEXITED(status) ||
 			    WEXITSTATUS(status) != 0)
 				tst_brkm(TBROK | TERRNO, cleanup,
testcases/kernel/syscalls/execve/execve05.c +3 -6
index 444bead23..66290f63d 100644
@@ -44,6 +44,7 @@
 #include <sys/stat.h>
 #include <sys/wait.h>
 #include "test.h"
+#include "safe_macros.h"
 
 #undef DEBUG			/* change this to #define if needed */
 
@@ -144,9 +145,7 @@ int main(int ac, char **av)
 #ifdef DEBUG
 			tst_resm(TINFO, "Main - started pid %d", pid);
 #endif
-			if (wait(&status) == -1)
-				tst_brkm(TBROK | TERRNO, cleanup,
-					 "wait failed");
+			SAFE_WAIT(cleanup, &status);
 			if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
 				tst_resm(TFAIL, "child exited abnormally");
 
@@ -169,9 +168,7 @@ int main(int ac, char **av)
 #ifdef DEBUG
 			tst_resm(TINFO, "Main - started pid %d", pid);
 #endif
-			if (wait(&status) == -1)
-				tst_brkm(TBROK | TERRNO, cleanup,
-					 "wait failed");
+			SAFE_WAIT(cleanup, &status);
 			if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
 				tst_resm(TFAIL, "child exited abnormally");
 
testcases/kernel/syscalls/flock/flock04.c +2 -3
index 87f1dfbda..87f8fc0ad 100644
@@ -76,6 +76,7 @@
 #include <sys/stat.h>
 #include <fcntl.h>
 #include "test.h"
+#include "safe_macros.h"
 
 void setup(void);
 void cleanup(void);
@@ -117,9 +118,7 @@ int main(int argc, char **argv)
 						 "flock() PASSED in acquiring shared lock on "
 						 "Share Locked file");
 				exit(0);
-			} else if (wait(&status) == -1)
-				tst_brkm(TBROK | TERRNO, cleanup,
-					 "wait failed");
+			} else SAFE_WAIT(cleanup, &status);
 
 			pid = FORK_OR_VFORK();
 			if (pid == -1)
testcases/kernel/syscalls/getpid/getpid02.c +2 -3
index 8a4197b8c..6c44ea96a 100644
@@ -71,6 +71,7 @@
 #include <sys/wait.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 void setup();			/* Main setup function of test */
 void cleanup();			/* cleanup function for the test */
@@ -107,9 +108,7 @@ int main(int ac, char **av)
 				exit(1);
 			exit(0);
 		} else {
-			if (wait(&status) == -1)
-				tst_brkm(TBROK | TERRNO, cleanup,
-					 "wait failed");
+			SAFE_WAIT(cleanup, &status);
 			if (!WIFEXITED(status) ||
 			    WEXITSTATUS(status) != 0)
 				tst_resm(TFAIL, "getpid() returned "
testcases/kernel/syscalls/getppid/getppid02.c +2 -3
index 60181e9e0..ae3c8eb26 100644
@@ -44,6 +44,7 @@
 #include <err.h>
 #include <errno.h>
 #include "test.h"
+#include "safe_macros.h"
 
 char *TCID = "getppid02";
 int TST_TOTAL = 1;
@@ -81,9 +82,7 @@ int main(int ac, char **av)
 				       "value match\n");
 			exit(0);
 		} else {
-			if (wait(&status) == -1)
-				tst_brkm(TBROK | TERRNO, cleanup,
-					 "wait failed");
+			SAFE_WAIT(cleanup, &status);
 			if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
 				tst_resm(TFAIL,
 					 "getppid functionality incorrect");
testcases/kernel/syscalls/ipc/semctl/semctl01.c +2 -2
index 39cf64e11..739eb5e4e 100644
@@ -47,6 +47,7 @@
 #endif
 #include <sys/wait.h>
 #include "ipcsem.h"
+#include "safe_macros.h"
 
 char *TCID = "semctl01";
 
@@ -135,8 +136,7 @@ static void kill_all_children(void)
 	 * make sure children finished before we proceed with next testcase
 	 */
 	for (j = 0; j < NCHILD; j++) {
-		if (wait(&status) == -1)
-			tst_brkm(TBROK | TERRNO, cleanup, "wait() failed");
+		SAFE_WAIT(cleanup, &status);
 	}
 }
 
testcases/kernel/syscalls/openat/openat02.c +1 -2
index 580b08636..e2eefda04 100644
@@ -172,8 +172,7 @@ void testfunc_cloexec(void)
 
 	SAFE_CLOSE(cleanup, TEST_RETURN);
 
-	if (wait(&status) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "wait() failed");
+	SAFE_WAIT(cleanup, &status);
 
 	if (WIFEXITED(status)) {
 		switch ((int8_t)WEXITSTATUS(status)) {
testcases/kernel/syscalls/sigrelse/sigrelse01.c +1 -2
index f94ba3534..38086caa7 100644
@@ -291,8 +291,7 @@ static void parent(void)
 	/*
 	 * child is now releasing signals, wait and check exit value
 	 */
-	if (wait(&term_stat) < 0)
-		tst_brkm(TBROK | TERRNO, getout, "wait() failed");
+	SAFE_WAIT(getout, &term_stat);
 
 	/* check child's signal exit value */
 	if ((sig = CHILD_SIG(term_stat)) != 0)
testcases/kernel/syscalls/unshare/unshare01.c +4 -9
index 84d18926e..52c774530 100644
@@ -92,6 +92,7 @@
 #include <stdio.h>
 
 #include "test.h"
+#include "safe_macros.h"
 #include "config.h"
 
 char *TCID = "unshare01";
@@ -187,9 +188,7 @@ int main(int ac, char **av)
 				}
 				exit(rval);
 			} else {
-				if (wait(&rval) == -1)
-					tst_brkm(TBROK | TERRNO, cleanup,
-						 "wait failed");
+				SAFE_WAIT(cleanup, &rval);
 				if (rval != 0 && WIFEXITED(rval)) {
 					switch (WEXITSTATUS(rval)) {
 					case 1:
@@ -225,9 +224,7 @@ int main(int ac, char **av)
 				}
 				exit(rval);
 			} else {
-				if (wait(&rval) == -1)
-					tst_brkm(TBROK | TERRNO, cleanup,
-						 "wait failed");
+				SAFE_WAIT(cleanup, &rval);
 				if (rval != 0 && WIFEXITED(rval)) {
 					switch (WEXITSTATUS(rval)) {
 					case 1:
@@ -263,9 +260,7 @@ int main(int ac, char **av)
 				}
 				exit(rval);
 			} else {
-				if (wait(&rval) == -1)
-					tst_brkm(TBROK | TERRNO, cleanup,
-						 "wait failed");
+				SAFE_WAIT(cleanup, &rval);
 				if (rval != 0 && WIFEXITED(rval)) {
 					switch (WEXITSTATUS(rval)) {
 					case 1:
testcases/kernel/syscalls/unshare/unshare02.c +3 -8
index 9221f9299..18b8bf522 100644
@@ -56,6 +56,7 @@
 #include <stdio.h>
 
 #include "test.h"
+#include "safe_macros.h"
 #include "config.h"
 
 char *TCID = "unshare02";
@@ -153,10 +154,7 @@ int main(int ac, char **av)
 				}
 				exit(rval);
 			} else {
-				if (wait(&rval) == -1) {
-					tst_brkm(TBROK | TERRNO, cleanup,
-						 "wait failed");
-				}
+				SAFE_WAIT(cleanup, &rval);
 				if (rval != 0 && WIFEXITED(rval)) {
 					switch (WEXITSTATUS(rval)) {
 					case 1:
@@ -191,10 +189,7 @@ int main(int ac, char **av)
 				}
 				exit(rval);
 			} else {
-				if (wait(&rval) == -1) {
-					tst_brkm(TBROK | TERRNO, cleanup,
-						 "wait failed");
-				}
+				SAFE_WAIT(cleanup, &rval);
 				if (rval != 0 && WIFEXITED(rval)) {
 					switch (WEXITSTATUS(rval)) {
 					case 1:
Signature · 1 line
2.13.5
#19 lore raw

PATCH 20/40 Make use of SAFE_WAITPID()

In reply to Cyril Hrubis
---
 lib/tst_res.c                                             |  4 ++--
 testcases/kernel/logging/kmsg/kmsg01.c                    |  3 +--
 testcases/kernel/mem/cpuset/cpuset01.c                    |  3 +--
 testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat03.c      |  3 +--
 testcases/kernel/mem/hugetlb/hugeshmctl/hugeshmctl03.c    |  3 +--
 testcases/kernel/mem/hugetlb/hugeshmget/hugeshmget05.c    |  3 +--
 testcases/kernel/mem/swapping/swapping01.c                |  6 ++----
 testcases/kernel/mem/vma/vma01.c                          |  3 +--
 testcases/kernel/syscalls/cma/process_vm01.c              |  3 +--
 testcases/kernel/syscalls/cma/process_vm_readv02.c        |  6 ++----
 testcases/kernel/syscalls/cma/process_vm_readv03.c        |  6 ++----
 testcases/kernel/syscalls/cma/process_vm_writev02.c       |  6 ++----
 testcases/kernel/syscalls/fanotify/fanotify03.c           |  5 +----
 testcases/kernel/syscalls/fork/fork14.c                   |  3 +--
 testcases/kernel/syscalls/getrusage/getrusage03.c         | 15 +++++----------
 testcases/kernel/syscalls/ipc/shmctl/shmctl01.c           |  4 ++--
 testcases/kernel/syscalls/ipc/shmctl/shmctl03.c           |  5 ++---
 testcases/kernel/syscalls/ipc/shmget/shmget05.c           |  5 ++---
 testcases/kernel/syscalls/migrate_pages/migrate_pages01.c |  3 +--
 testcases/kernel/syscalls/migrate_pages/migrate_pages02.c |  6 ++----
 testcases/kernel/syscalls/mprotect/mprotect02.c           |  7 ++-----
 testcases/kernel/syscalls/pipe/pipe09.c                   |  7 +++----
 testcases/kernel/syscalls/setns/setns02.c                 |  3 +--
 testcases/kernel/syscalls/setrlimit/setrlimit01.c         |  3 +--
 testcases/kernel/syscalls/waitid/waitid02.c               |  4 ++--
 25 files changed, 42 insertions(+), 77 deletions(-)
lib/tst_res.c +2 -2
index 8cf5f0d4a..9e9aa2109 100644
@@ -48,6 +48,7 @@
 #include <sys/wait.h>
 
 #include "test.h"
+#include "safe_macros.h"
 #include "usctest.h"
 #include "ltp_priv.h"
 #include "tst_ansi_color.h"
@@ -423,8 +424,7 @@ void tst_record_childstatus(void (*cleanup)(void), pid_t child)
 
 	NO_NEWLIB_ASSERT("Unknown", 0);
 
-	if (waitpid(child, &status, 0) < 0)
-		tst_brkm(TBROK | TERRNO, cleanup, "waitpid(%d) failed", child);
+	SAFE_WAITPID(cleanup, child, &status, 0);
 
 	if (WIFEXITED(status)) {
 		ttype_result = WEXITSTATUS(status);
testcases/kernel/logging/kmsg/kmsg01.c +1 -2
index b20704551..58b62d517 100644
@@ -234,8 +234,7 @@ static int timed_read_kmsg(int fd, int timeout_sec)
 	/* child is blocked, kill it */
 	if (TEST_RETURN == -2)
 		kill(child, SIGTERM);
-	if (waitpid(child, &status, 0) == -1)
-		tst_brk(TBROK | TERRNO, "waitpid");
+	SAFE_WAITPID(child, &status, 0);
 	if (WIFEXITED(status)) {
 		if (WEXITSTATUS(status) == 0) {
 			return 0;
testcases/kernel/mem/cpuset/cpuset01.c +1 -2
index e10124c17..3d68cc07a 100644
@@ -71,8 +71,7 @@ static void test_cpuset(void)
 	snprintf(buf, BUFSIZ, "%d", nodes[1]);
 	write_cpuset_files(CPATH_NEW, "mems", buf);
 
-	if (waitpid(child, &status, WUNTRACED | WCONTINUED) == -1)
-		tst_brk(TBROK | TERRNO, "waitpid");
+	SAFE_WAITPID(child, &status, WUNTRACED | WCONTINUED);
 	if (WEXITSTATUS(status) != 0) {
 		tst_res(TFAIL, "child exit status is %d", WEXITSTATUS(status));
 		return;
testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat03.c +1 -2
index 960bec184..83534e585 100644
@@ -81,8 +81,7 @@ static void verify_hugeshmat(void)
 		}
 		break;
 	default:
-		if (waitpid(pid, &status, 0) == -1)
-			tst_brk(TBROK | TERRNO, "waitpid");
+		SAFE_WAITPID(pid, &status, 0);
 	}
 }
 
testcases/kernel/mem/hugetlb/hugeshmctl/hugeshmctl03.c +1 -2
index f58946fa0..0474b31e0 100644
@@ -89,8 +89,7 @@ static void test_hugeshmctl(void)
 		do_child();
 		exit(0);
 	default:
-		if (waitpid(pid, &status, 0) == -1)
-			tst_brk(TBROK | TERRNO, "waitpid");
+		SAFE_WAITPID(pid, &status, 0);
 	}
 }
 
testcases/kernel/mem/hugetlb/hugeshmget/hugeshmget05.c +1 -2
index 22138e354..790d73603 100644
@@ -57,8 +57,7 @@ static void test_hugeshmget(void)
 		exit(0);
 	default:
 		/* wait for the child to return */
-		if (waitpid(pid, &status, 0) == -1)
-			tst_brk(TBROK | TERRNO, "waitpid");
+		SAFE_WAITPID(pid, &status, 0);
 	}
 }
 
testcases/kernel/mem/swapping/swapping01.c +2 -4
index 561080691..6ac802b25 100644
@@ -120,8 +120,7 @@ static void check_swapping(void)
 	long swap_free_now, swapped;
 
 	/* wait child stop */
-	if (waitpid(pid, &status, WUNTRACED) == -1)
-		tst_brk(TBROK | TERRNO, "waitpid");
+	SAFE_WAITPID(pid, &status, WUNTRACED);
 	if (!WIFSTOPPED(status))
 		tst_brk(TBROK, "child was not stopped.");
 
@@ -148,8 +147,7 @@ static void check_swapping(void)
 		 swapped / 1024);
 	kill(pid, SIGCONT);
 	/* wait child exit */
-	if (waitpid(pid, &status, 0) == -1)
-		tst_brk(TBROK | TERRNO, "waitpid");
+	SAFE_WAITPID(pid, &status, 0);
 }
 
 static struct tst_test test = {
testcases/kernel/mem/vma/vma01.c +1 -2
index bafbfff46..31322918c 100644
@@ -131,8 +131,7 @@ static void check_vma(void)
 		}
 		exit(255);
 	default:
-		if (waitpid(-1, &status, 0) == -1)
-			tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
+		SAFE_WAITPID(cleanup, -1, &status, 0);
 		if (!WIFEXITED(status))
 			tst_brkm(TBROK, cleanup, "child exited abnormally.");
 		check_status(WEXITSTATUS(status));
testcases/kernel/syscalls/cma/process_vm01.c +1 -2
index 16a27e5b0..bc6b04f24 100644
@@ -359,8 +359,7 @@ static void cma_test_invalid_perm(void)
 		cma_free_params(params);
 		exit(ret);
 	default:
-		if (waitpid(child_pid, &status, 0) == -1)
-			tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
+		SAFE_WAITPID(cleanup, child_pid, &status, 0);
 		if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
 			tst_resm(TFAIL, "child returns %d", status);
 	}
testcases/kernel/syscalls/cma/process_vm_readv02.c +2 -4
index 6bbfb62b0..f84f79ef7 100644
@@ -79,16 +79,14 @@ int main(int argc, char **argv)
 		}
 
 		/* wait until child_invoke reads from child_alloc's VM */
-		if (waitpid(pids[1], &status, 0) == -1)
-			tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
+		SAFE_WAITPID(cleanup, pids[1], &status, 0);
 		if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
 			tst_resm(TFAIL, "child 1 returns %d", status);
 
 		/* child_alloc is free to exit now */
 		safe_semop(semid, 0, 1);
 
-		if (waitpid(pids[0], &status, 0) == -1)
-			tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
+		SAFE_WAITPID(cleanup, pids[0], &status, 0);
 		if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
 			tst_resm(TFAIL, "child 0 returns %d", status);
 	}
testcases/kernel/syscalls/cma/process_vm_readv03.c +2 -4
index 07a72d97d..2e69f855f 100644
@@ -98,16 +98,14 @@ int main(int argc, char **argv)
 		}
 
 		/* wait until child_invoke reads from child_alloc's VM */
-		if (waitpid(pids[1], &status, 0) == -1)
-			tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
+		SAFE_WAITPID(cleanup, pids[1], &status, 0);
 		if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
 			tst_resm(TFAIL, "child 1 returns %d", status);
 
 		/* child_alloc is free to exit now */
 		safe_semop(semid, 0, 1);
 
-		if (waitpid(pids[0], &status, 0) == -1)
-			tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
+		SAFE_WAITPID(cleanup, pids[0], &status, 0);
 		if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
 			tst_resm(TFAIL, "child 0 returns %d", status);
 
testcases/kernel/syscalls/cma/process_vm_writev02.c +2 -4
index c681f9f8b..952081811 100644
@@ -92,16 +92,14 @@ int main(int argc, char **argv)
 
 		/* wait until child_write writes into
 		 * child_init_and_verify's VM */
-		if (waitpid(pids[1], &status, 0) == -1)
-			tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
+		SAFE_WAITPID(cleanup, pids[1], &status, 0);
 		if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
 			tst_resm(TFAIL, "child 1 returns %d", status);
 
 		/* signal child_init_and_verify to verify its VM now */
 		safe_semop(semid, 0, 1);
 
-		if (waitpid(pids[0], &status, 0) == -1)
-			tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
+		SAFE_WAITPID(cleanup, pids[0], &status, 0);
 		if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
 			tst_resm(TFAIL, "child 0 returns %d", status);
 	}
testcases/kernel/syscalls/fanotify/fanotify03.c +1 -4
index 3a75ac252..1ab879843 100644
@@ -130,10 +130,7 @@ static void check_child(void)
 		tst_brk(TBROK | TERRNO,
 			 "sigaction(SIGCHLD, &child_action, NULL) failed");
 	}
-	if (waitpid(-1, &child_ret, 0) < 0) {
-		tst_brk(TBROK | TERRNO,
-				 "waitpid(-1, &child_ret, 0) failed");
-	}
+	SAFE_WAITPID(-1, &child_ret, 0);
 
 	if (WIFSIGNALED(child_ret)) {
 		tst_res(TFAIL, "child exited due to signal %d",
testcases/kernel/syscalls/fork/fork14.c +1 -2
index bbc1d3d74..6ea92e0c2 100644
@@ -121,8 +121,7 @@ static int fork_test(void)
 		case 0:
 			exit(0);
 		default:
-			if (waitpid(-1, NULL, 0) == -1)
-				tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
+			SAFE_WAITPID(cleanup, -1, NULL, 0);
 
 			if (prev_failed > 0 && i >= LARGE) {
 				tst_resm(TFAIL, "Fork succeeds incorrectly");
testcases/kernel/syscalls/getrusage/getrusage03.c +5 -10
index 54cdc8322..125acfc64 100644
@@ -112,8 +112,7 @@ static void inherit_fork(void)
 		break;
 	}
 
-	if (waitpid(pid, &status, WUNTRACED | WCONTINUED) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
+	SAFE_WAITPID(cleanup, pid, &status, WUNTRACED | WCONTINUED);
 	check_return(WEXITSTATUS(status), "initial.self ~= child.self",
 		     "initial.self !~= child.self");
 }
@@ -142,8 +141,7 @@ static void inherit_fork2(void)
 		break;
 	}
 
-	if (waitpid(pid, &status, WUNTRACED | WCONTINUED) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
+	SAFE_WAITPID(cleanup, pid, &status, WUNTRACED | WCONTINUED);
 	check_return(WEXITSTATUS(status), "child.children == 0",
 		     "child.children != 0");
 }
@@ -171,8 +169,7 @@ static void fork_malloc(void)
 		break;
 	}
 
-	if (waitpid(pid, &status, WUNTRACED | WCONTINUED) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
+	SAFE_WAITPID(cleanup, pid, &status, WUNTRACED | WCONTINUED);
 	check_return(WEXITSTATUS(status), "initial.self + 50MB ~= child.self",
 		     "initial.self + 50MB !~= child.self");
 }
@@ -198,8 +195,7 @@ static void grandchild_maxrss(void)
 		break;
 	}
 
-	if (waitpid(pid, &status, WUNTRACED | WCONTINUED) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
+	SAFE_WAITPID(cleanup, pid, &status, WUNTRACED | WCONTINUED);
 	if (WEXITSTATUS(status) != 0)
 		tst_brkm(TBROK | TERRNO, cleanup, "child exit status is not 0");
 
@@ -241,8 +237,7 @@ static void zombie(void)
 	else
 		tst_resm(TFAIL, "initial.children !~= pre_wait.children");
 
-	if (waitpid(pid, &status, WUNTRACED | WCONTINUED) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
+	SAFE_WAITPID(cleanup, pid, &status, WUNTRACED | WCONTINUED);
 	if (WEXITSTATUS(status) != 0)
 		tst_brkm(TBROK | TERRNO, cleanup, "child exit status is not 0");
 
testcases/kernel/syscalls/ipc/shmctl/shmctl01.c +2 -2
index ac66ceb83..ac3660f4a 100644
@@ -47,6 +47,7 @@
 #define _GNU_SOURCE
 #endif
 #include "ipcshm.h"
+#include "safe_macros.h"
 
 char *TCID = "shmctl01";
 
@@ -348,8 +349,7 @@ void stat_cleanup(void)
 	}
 
 	for (i = 0; i < N_ATTACH; i++) {
-		if (waitpid(pid_arr[i], NULL, 0) == -1)
-			tst_brkm(TBROK, cleanup, "waitpid failed");
+		SAFE_WAITPID(cleanup, pid_arr[i], NULL, 0);
 	}
 
 	stat_time++;
testcases/kernel/syscalls/ipc/shmctl/shmctl03.c +2 -3
index a66db3baf..97cf27baf 100644
@@ -60,6 +60,7 @@
 #include "ipcshm.h"
 #include <sys/types.h>
 #include <sys/wait.h>
+#include "safe_macros.h"
 
 char *TCID = "shmctl03";
 int shm_id_1 = -1;
@@ -110,9 +111,7 @@ int main(int ac, char **av)
 		do_child();
 	} else {
 		/* wait for the child to return */
-		if (waitpid(pid, NULL, 0) == -1) {
-			tst_brkm(TBROK, cleanup, "waitpid failed");
-		}
+		SAFE_WAITPID(cleanup, pid, NULL, 0);
 
 		/* if it exists, remove the shared memory resource */
 		rm_shm(shm_id_1);
testcases/kernel/syscalls/ipc/shmget/shmget05.c +2 -3
index 09ec5b190..01770ba7d 100644
@@ -59,6 +59,7 @@
 #include "ipcshm.h"
 #include <sys/types.h>
 #include <sys/wait.h>
+#include "safe_macros.h"
 
 char *TCID = "shmget05";
 int TST_TOTAL = 1;
@@ -94,9 +95,7 @@ int main(int ac, char **av)
 
 	} else {		/* parent */
 		/* wait for the child to return */
-		if (waitpid(pid, NULL, 0) == -1) {
-			tst_brkm(TBROK, cleanup, "waitpid failed");
-		}
+		SAFE_WAITPID(cleanup, pid, NULL, 0);
 
 		/* if it exists, remove the shared memory resource */
 		rm_shm(shm_id_1);
testcases/kernel/syscalls/migrate_pages/migrate_pages01.c +1 -2
index c68f95d83..32c78b29a 100644
@@ -189,8 +189,7 @@ static void test_invalid_perm(void)
 		ret |= check_errno(EPERM);
 		exit(ret);
 	default:
-		if (waitpid(child_pid, &status, 0) == -1)
-			tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
+		SAFE_WAITPID(cleanup, child_pid, &status, 0);
 		if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
 			tst_resm(TFAIL, "child returns %d", status);
 	}
testcases/kernel/syscalls/migrate_pages/migrate_pages02.c +2 -4
index d18458972..6755a384d 100644
@@ -217,8 +217,7 @@ static void test_migrate_current_process(int node1, int node2, int cap_sys_nice)
 		munmap(testp2, getpagesize());
 		exit(ret);
 	default:
-		if (waitpid(child, &status, 0) == -1)
-			tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
+		SAFE_WAITPID(cleanup, child, &status, 0);
 		if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
 			tst_resm(TFAIL, "child returns %d", status);
 		if (cap_sys_nice)
@@ -295,8 +294,7 @@ static void test_migrate_other_process(int node1, int node2, int cap_sys_nice)
 		if (write(pages_migrated[1], &tmp, 1) != 1)
 			tst_brkm(TBROK | TERRNO, NULL, "write #2 failed");
 
-		if (waitpid(child, &status, 0) == -1)
-			tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
+		SAFE_WAITPID(cleanup, child, &status, 0);
 		if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
 			tst_resm(TFAIL, "child returns %d", status);
 		close(child_ready[0]);
testcases/kernel/syscalls/mprotect/mprotect02.c +2 -5
index 31455c7b9..de9b4ea00 100644
@@ -96,8 +96,7 @@ int main(int ac, char **av)
 			exit(255);
 		}
 
-		if (waitpid(pid, &status, 0) == -1)
-			tst_brkm(TBROK | TERRNO, cleanup, "waitpid failed");
+		SAFE_WAITPID(cleanup, pid, &status, 0);
 		if (!WIFEXITED(status))
 			tst_brkm(TBROK, cleanup, "child exited abnormally "
 				 "with status: %d", status);
@@ -127,9 +126,7 @@ int main(int ac, char **av)
 				exit(0);
 			}
 
-			if (waitpid(pid, &status, 0) == -1)
-				tst_brkm(TBROK | TERRNO, cleanup,
-					 "waitpid failed");
+			SAFE_WAITPID(cleanup, pid, &status, 0);
 
 			if (WIFEXITED(status) &&
 			    WEXITSTATUS(status) == 0)
testcases/kernel/syscalls/pipe/pipe09.c +3 -4
index 226083b31..a4b2f8231 100644
@@ -51,6 +51,7 @@
 #include <sys/wait.h>
 #include <errno.h>
 #include "test.h"
+#include "safe_macros.h"
 
 #define	PIPEWRTCNT	100	/* must be an even number */
 
@@ -119,8 +120,7 @@ int main(int ac, char **av)
 
 		/* parent */
 
-		if (waitpid(fork_1, &wtstatus, 0) == -1)
-			tst_brkm(TBROK, cleanup, "waitpid failed");
+		SAFE_WAITPID(cleanup, fork_1, &wtstatus, 0);
 		if (WIFEXITED(wtstatus) && WEXITSTATUS(wtstatus) != 0) {
 			tst_brkm(TBROK, cleanup, "child exited abnormally");
 		}
@@ -146,8 +146,7 @@ int main(int ac, char **av)
 
 		/* parent */
 
-		if (waitpid(fork_2, &wtstatus, 0) == -1)
-			tst_brkm(TBROK, cleanup, "waitpid failed");
+		SAFE_WAITPID(cleanup, fork_2, &wtstatus, 0);
 		if (WEXITSTATUS(wtstatus) != 0) {
 			tst_brkm(TBROK, cleanup, "problem detected in child, "
 				 "wait status %d, errno = %d", wtstatus, errno);
testcases/kernel/syscalls/setns/setns02.c +1 -2
index 58a02a65b..0f4902845 100644
@@ -150,8 +150,7 @@ static void test_flag(int clone_flag, int ns_flag, int (*fn) (void *arg))
 	if (ret == -1)
 		tst_brkm(TBROK|TERRNO, cleanup, "ltp_clone");
 
-	if (waitpid(ret, &status, 0) == -1)
-		tst_brkm(TBROK|TERRNO, cleanup, "waitpid");
+	SAFE_WAITPID(cleanup, ret, &status, 0);
 	if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
 		tst_resm(TFAIL, "child returns %d", status);
 	else
testcases/kernel/syscalls/setrlimit/setrlimit01.c +1 -2
index 3118782f3..05b4cf948 100644
@@ -162,8 +162,7 @@ static void test2(void)
 	}
 
 	/* parent */
-	if (waitpid(pid, &status, 0) == -1)
-		tst_brkm(TBROK, cleanup, "waitpid() failed");
+	SAFE_WAITPID(cleanup, pid, &status, 0);
 
 	switch (WEXITSTATUS(status)) {
 	case 0:
testcases/kernel/syscalls/waitid/waitid02.c +2 -2
index 431e91060..dced6fadc 100644
@@ -48,6 +48,7 @@
 #include <sys/stat.h>
 
 #include "test.h"
+#include "safe_macros.h"
 #include "lapi/syscalls.h"
 
 struct testcase_t {
@@ -162,8 +163,7 @@ static void makechild(struct testcase_t *t, void (*childfn)(void))
 static void wait4child(pid_t pid)
 {
 	int status;
-	if (waitpid(pid, &status, 0) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
+	SAFE_WAITPID(cleanup, pid, &status, 0);
 	if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
 		tst_resm(TFAIL, "child returns %d", status);
 }
Signature · 1 line
2.13.5
#20 lore raw

PATCH 21/40 Make use of SAFE_KILL()

In reply to Cyril Hrubis
---
 testcases/kernel/containers/pidns/pidns20.c            | 4 +---
 testcases/kernel/mem/hugetlb/hugeshmctl/hugeshmctl01.c | 3 +--
 testcases/kernel/syscalls/ipc/msgrcv/msgrcv05.c        | 4 ++--
 testcases/kernel/syscalls/ipc/semctl/semctl01.c        | 3 +--
 testcases/kernel/syscalls/ipc/semop/semop05.c          | 6 ++----
 testcases/kernel/syscalls/ipc/shmctl/shmctl01.c        | 3 +--
 testcases/kernel/syscalls/nanosleep/nanosleep02.c      | 6 ++----
 testcases/kernel/syscalls/nanosleep/nanosleep03.c      | 6 ++----
 testcases/kernel/syscalls/sighold/sighold02.c          | 7 ++-----
 testcases/kernel/syscalls/vfork/vfork02.c              | 6 ++----
 10 files changed, 16 insertions(+), 32 deletions(-)
testcases/kernel/containers/pidns/pidns20.c +1 -3
index 70e9356fd..e28bcbdd6 100644
@@ -180,9 +180,7 @@ int main(int argc, char *argv[])
 	}
 
 	/* Enqueue SIGUSR1 in pending signal queue of container */
-	if (kill(cpid, SIGUSR1) == -1) {
-		tst_brkm(TBROK | TERRNO, NULL, "kill() failed");
-	}
+	SAFE_KILL(NULL, cpid, SIGUSR1);
 
 	tst_resm(TINFO, "parent: signalled SIGUSR1 to container");
 	if (write(parent_cinit[1], "p:go", 5) != 5) {
testcases/kernel/mem/hugetlb/hugeshmctl/hugeshmctl01.c +1 -2
index 0e37b8f96..ad7a80d70 100644
@@ -287,8 +287,7 @@ static void stat_cleanup(void)
 
 	/* wake up the childern so they can detach the memory and exit */
 	for (i = 0; i < N_ATTACH; i++)
-		if (kill(pid_arr[i], SIGUSR1) == -1)
-			tst_brk(TBROK | TERRNO, "kill with SIGUSR1");
+		SAFE_KILL(pid_arr[i], SIGUSR1);
 
 	/* remove the parent's shared memory the second time through */
 	if (stat_time == SECOND)
testcases/kernel/syscalls/ipc/msgrcv/msgrcv05.c +2 -2
index 7a8b680b9..66c4341f1 100644
@@ -56,6 +56,7 @@
  */
 
 #include "test.h"
+#include "safe_macros.h"
 
 #include "ipcmsg.h"
 
@@ -116,8 +117,7 @@ int main(int ac, char **av)
 			TST_PROCESS_STATE_WAIT(cleanup, c_pid, 'S');
 
 			/* send a signal that must be caught to the child */
-			if (kill(c_pid, SIGHUP) == -1)
-				tst_brkm(TBROK, cleanup, "kill failed");
+			SAFE_KILL(cleanup, c_pid, SIGHUP);
 
 			waitpid(c_pid, NULL, 0);
 		}
testcases/kernel/syscalls/ipc/semctl/semctl01.c +1 -2
index 739eb5e4e..2f443db89 100644
@@ -128,8 +128,7 @@ static void kill_all_children(void)
 	int j, status;
 
 	for (j = 0; j < NCHILD; j++) {
-		if (kill(pid_arr[j], SIGKILL) == -1)
-			tst_brkm(TBROK | TERRNO, cleanup, "child kill failed");
+		SAFE_KILL(cleanup, pid_arr[j], SIGKILL);
 	}
 
 	/*
testcases/kernel/syscalls/ipc/semop/semop05.c +2 -4
index 34bb4da35..6ed25014c 100644
@@ -64,6 +64,7 @@
 
 #include <sys/types.h>
 #include <sys/wait.h>
+#include "safe_macros.h"
 
 char *TCID = "semop05";
 int TST_TOTAL = 4;
@@ -160,10 +161,7 @@ int main(int ac, char **av)
 					/* remove the semaphore resource */
 					rm_sema(sem_id_1);
 				} else {
-					if (kill(pid, SIGHUP) == -1) {
-						tst_brkm(TBROK, cleanup,
-							 "kill failed");
-					}
+					SAFE_KILL(cleanup, pid, SIGHUP);
 				}
 
 				/* let the child carry on */
testcases/kernel/syscalls/ipc/shmctl/shmctl01.c +1 -2
index ac3660f4a..ddf6b86d1 100644
@@ -338,8 +338,7 @@ void stat_cleanup(void)
 
 	/* wake up the childern so they can detach the memory and exit */
 	for (i = 0; i < N_ATTACH; i++) {
-		if (kill(pid_arr[i], SIGUSR1) == -1)
-			tst_brkm(TBROK, cleanup, "kill failed");
+		SAFE_KILL(cleanup, pid_arr[i], SIGUSR1);
 	}
 
 	/* remove the parent's shared memory the second time through */
testcases/kernel/syscalls/nanosleep/nanosleep02.c +2 -4
index 8ee22b49e..42c825190 100644
@@ -36,6 +36,7 @@
 #include <inttypes.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 char *TCID = "nanosleep02";
 int TST_TOTAL = 1;
@@ -92,10 +93,7 @@ int main(int ac, char **av)
 		sleep(1);
 
 		/* Now send signal to child */
-		if (kill(cpid, SIGINT) < 0) {
-			tst_brkm(TBROK, NULL,
-				 "kill() fails send signal to child");
-		}
+		SAFE_KILL(NULL, cpid, SIGINT);
 
 		tst_record_childstatus(NULL, cpid);
 	}
testcases/kernel/syscalls/nanosleep/nanosleep03.c +2 -4
index 291396b43..456e140e9 100644
@@ -33,6 +33,7 @@
 #include <sys/wait.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 char *TCID = "nanosleep03";
 int TST_TOTAL = 1;
@@ -81,10 +82,7 @@ int main(int ac, char **av)
 		sleep(1);
 
 		/* Now send signal to child */
-		if (kill(cpid, SIGINT) < 0) {
-			tst_brkm(TBROK, NULL,
-				 "kill() fails send signal to child");
-		}
+		SAFE_KILL(NULL, cpid, SIGINT);
 
 		/* Wait for child to execute */
 		wait(&status);
testcases/kernel/syscalls/sighold/sighold02.c +2 -5
index be018748b..8df5fa7db 100644
@@ -48,6 +48,7 @@
 #include <sys/types.h>
 #include <sys/wait.h>
 #include "test.h"
+#include "safe_macros.h"
 
 /* _XOPEN_SOURCE disables NSIG */
 #ifndef NSIG
@@ -111,11 +112,7 @@ int main(int ac, char **av)
 			for (sig = 1; sig < NUMSIGS; sig++) {
 				if (skip_sig(sig))
 					continue;
-				if (kill(pid, sig) < 0) {
-					tst_brkm(TBROK | TERRNO, NULL,
-						 "kill(%d, %d(%s)) failed",
-						 pid, sig, tst_strsig(sig));
-				}
+				SAFE_KILL(NULL, pid, sig);
 			}
 
 			TST_SAFE_CHECKPOINT_WAKE(NULL, 0);
testcases/kernel/syscalls/vfork/vfork02.c +2 -4
index aadc5993f..efa70d0be 100644
@@ -78,6 +78,7 @@
 #include <sys/wait.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 char *TCID = "vfork02";
 int TST_TOTAL = 1;
@@ -185,10 +186,7 @@ void setup(void)
 	}
 
 	/* Send the signal SIGUSR1 to itself so that SIGUSR1 is pending */
-	if (kill(getpid(), SIGUSR1) == -1) {
-		tst_brkm(TBROK, cleanup,
-			 "Fails to send the signal to the parent process");
-	}
+	SAFE_KILL(cleanup, getpid(), SIGUSR1);
 
 	/* If SIGUSR1 is not pending in the parent, fail */
 	if (sigpending(&PendSig) == -1) {
Signature · 1 line
2.13.5
#21 lore raw

PATCH 22/40 Make use of SAFE_MKFIFO()

In reply to Cyril Hrubis
---
 testcases/kernel/ipc/pipeio/pipeio.c        | 5 +----
 testcases/kernel/syscalls/dup/dup05.c       | 3 +--
 testcases/kernel/syscalls/select/select03.c | 6 ++----
 testcases/kernel/syscalls/unlink/unlink06.c | 7 ++-----
 4 files changed, 6 insertions(+), 15 deletions(-)
testcases/kernel/ipc/pipeio/pipeio.c +1 -4
index 5aa082fd5..b9421cbea 100644
@@ -478,10 +478,7 @@ static void setup(int argc, char *argv[])
 		pipe_type = PIPE_UNNAMED;
 		blk_type = UNNAMED_IO;
 	} else {
-		if (mkfifo(pname, 0777) == -1) {
-			tst_brkm(TBROK | TERRNO, cleanup,
-				"mkfifo(%s, 0777) failed", pname);
-		}
+		SAFE_MKFIFO(cleanup, pname, 0777);
 		pipe_type = PIPE_NAMED;
 	}
 }
testcases/kernel/syscalls/dup/dup05.c +1 -2
index 3ef61cfa0..fc0e1f1d4 100644
@@ -167,8 +167,7 @@ void setup(void)
 	tst_tmpdir();
 
 	sprintf(Fname, "dupfile");
-	if (mkfifo(Fname, 0777) == -1)
-		tst_brkm(TBROK, cleanup, "mkfifo failed");
+	SAFE_MKFIFO(cleanup, Fname, 0777);
 	if ((fd = open(Fname, O_RDWR, 0700)) == -1)
 		tst_brkm(TBROK, cleanup, "open failed");
 }
testcases/kernel/syscalls/select/select03.c +2 -4
index a9a81d0f9..da7fdb094 100644
@@ -48,6 +48,7 @@
 #include <sys/stat.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 #define FILENAME	"select03"
 
@@ -113,10 +114,7 @@ static void setup(void)
 
 	tst_tmpdir();
 
-	if (mkfifo(FILENAME, 0777) == -1) {
-		tst_brkm(TBROK, cleanup, "mkfifo(%s, 0777) failed, errno=%d",
-			 FILENAME, errno);
-	}
+	SAFE_MKFIFO(cleanup, FILENAME, 0777);
 
 	if ((Fd = open(FILENAME, O_RDWR)) == -1) {
 		tst_brkm(TBROK, cleanup, "open(%s, O_RDWR) failed, errno=%d",
testcases/kernel/syscalls/unlink/unlink06.c +2 -5
index 2f9601214..0e384804d 100644
@@ -117,6 +117,7 @@
 #include <signal.h>
 #include <unistd.h>
 #include "test.h"
+#include "safe_macros.h"
 
 void setup();
 void create_file();
@@ -210,9 +211,5 @@ void cleanup(void)
  ******************************************************************/
 void create_file(void)
 {
-	if (mkfifo(Fname, 0777) == -1) {
-		tst_brkm(TBROK, cleanup,
-			 "mkfifo(%s, 0777) failed errno:%d %s\n", Fname,
-			 errno, strerror(errno));
-	}
+	SAFE_MKFIFO(cleanup, Fname, 0777);
 }
Signature · 1 line
2.13.5
#22 lore raw

PATCH 23/40 Make use of SAFE_RENAME()

In reply to Cyril Hrubis
---
 testcases/kernel/syscalls/inotify/inotify02.c | 21 ++++-----------------
 testcases/kernel/syscalls/rename/rename01.c   |  8 ++------
 2 files changed, 6 insertions(+), 23 deletions(-)
testcases/kernel/syscalls/inotify/inotify02.c +4 -17
index 2c92f1616..4a1036ab4 100644
@@ -117,11 +117,7 @@ int main(int ac, char **av)
 		strcpy(event_set[tst_count].name, FILE_NAME1);
 		tst_count++;
 
-		if (rename(FILE_NAME1, FILE_NAME2) == -1) {
-			tst_brkm(TBROK | TERRNO, cleanup,
-				 "rename(%s, %s) failed",
-				 FILE_NAME1, FILE_NAME2);
-		}
+		SAFE_RENAME(cleanup, FILE_NAME1, FILE_NAME2);
 		event_set[tst_count].mask = IN_MOVED_FROM;
 		strcpy(event_set[tst_count].name, FILE_NAME1);
 		tst_count++;
@@ -135,10 +131,7 @@ int main(int ac, char **av)
 		}
 
 		snprintf(fname2, BUF_SIZE, "%s.rename1", fname1);
-		if (rename(fname1, fname2) == -1) {
-			tst_brkm(TBROK | TERRNO, cleanup,
-				 "rename(%s, %s) failed", fname1, fname2);
-		}
+		SAFE_RENAME(cleanup, fname1, fname2);
 		event_set[tst_count].mask = IN_MOVE_SELF;
 		strcpy(event_set[tst_count].name, "");
 		tst_count++;
@@ -155,15 +148,9 @@ int main(int ac, char **av)
 		 * 2.6.25. See comment below.
 		 */
 		snprintf(fname3, BUF_SIZE, "%s.rename2", fname1);
-		if (rename(fname2, fname3) == -1) {
-			tst_brkm(TBROK | TERRNO, cleanup,
-				 "rename(%s, %s) failed", fname2, fname3);
-		}
+		SAFE_RENAME(cleanup, fname2, fname3);
 
-		if (rename(fname3, fname1) == -1) {
-			tst_brkm(TBROK | TERRNO, cleanup,
-				 "rename(%s, %s) failed", fname3, fname1);
-		}
+		SAFE_RENAME(cleanup, fname3, fname1);
 		event_set[tst_count].mask = IN_MOVE_SELF;
 		strcpy(event_set[tst_count].name, "");
 		tst_count++;
testcases/kernel/syscalls/rename/rename01.c +2 -6
index 2474ab95e..80977d35f 100644
@@ -164,13 +164,9 @@ int main(int ac, char **av)
 				 "for renaming a %s", TC[i].desc);
 		}
 		/* reset things in case we are looping */
-		if (rename(mname, fname) == -1) {
-			tst_brkm(TBROK, cleanup, "file rename failed");
-		}
+		SAFE_RENAME(cleanup, mname, fname);
 
-		if (rename(mdir, fdir) == -1) {
-			tst_brkm(TBROK, cleanup, "directory rename failed");
-		}
+		SAFE_RENAME(cleanup, mdir, fdir);
 	}
 
 	cleanup();
Signature · 1 line
2.13.5
#23 lore raw

PATCH 24/40 Make use of SAFE_MOUNT()

In reply to Cyril Hrubis
---
 testcases/kernel/io/direct_io/dma_thread_diotest.c |  5 +----
 testcases/kernel/mem/hugetlb/hugemmap/hugemmap01.c |  3 +--
 testcases/kernel/mem/hugetlb/hugemmap/hugemmap02.c |  3 +--
 testcases/kernel/mem/hugetlb/hugemmap/hugemmap04.c |  3 +--
 testcases/kernel/syscalls/acct/acct01.c            | 12 +++---------
 testcases/kernel/syscalls/chmod/chmod06.c          |  5 +----
 testcases/kernel/syscalls/chown/chown04.c          |  5 +----
 testcases/kernel/syscalls/fchmod/fchmod06.c        | 12 +++---------
 testcases/kernel/syscalls/fchown/fchown04.c        | 12 +++---------
 testcases/kernel/syscalls/lchown/lchown03.c        |  5 +----
 testcases/kernel/syscalls/link/link08.c            | 12 +++---------
 testcases/kernel/syscalls/linkat/linkat02.c        | 12 +++---------
 testcases/kernel/syscalls/mkdir/mkdir03.c          |  5 +----
 testcases/kernel/syscalls/mkdirat/mkdirat02.c      | 11 +++--------
 testcases/kernel/syscalls/mknod/mknod07.c          |  5 +----
 testcases/kernel/syscalls/mknodat/mknodat02.c      |  5 +----
 testcases/kernel/syscalls/mount/mount05.c          |  3 +--
 testcases/kernel/syscalls/mount/mount06.c          | 10 +++-------
 testcases/kernel/syscalls/rename/rename11.c        | 16 ++++------------
 testcases/kernel/syscalls/renameat/renameat01.c    | 12 +++---------
 testcases/kernel/syscalls/rmdir/rmdir02.c          | 12 +++---------
 testcases/kernel/syscalls/utime/utime06.c          |  5 +----
 testcases/kernel/syscalls/utimes/utimes01.c        | 12 +++---------
 23 files changed, 46 insertions(+), 139 deletions(-)
testcases/kernel/io/direct_io/dma_thread_diotest.c +1 -4
index 5f52dcc9b..aa51bf2fb 100644
@@ -401,10 +401,7 @@ static void setup(void)
 			tst_mkfs(NULL, device, "ext3", NULL, NULL);
 		}
 
-		if (mount(device, MNT_POINT, "ext3", 0, NULL) < 0) {
-			tst_brkm(TBROK | TERRNO, NULL,
-				 "mount device:%s failed", device);
-		}
+		SAFE_MOUNT(NULL, device, MNT_POINT, "ext3", 0, NULL);
 		mount_flag = 1;
 	}
 
testcases/kernel/mem/hugetlb/hugemmap/hugemmap01.c +1 -2
index d56fbfaed..212a8529b 100644
@@ -96,8 +96,7 @@ void setup(void)
 
 	if (!Hopt)
 		Hopt = tst_get_tmpdir();
-	if (mount("none", Hopt, "hugetlbfs", 0, NULL) < 0)
-		tst_brk(TBROK | TERRNO, "mount failed on %s", Hopt);
+	SAFE_MOUNT("none", Hopt, "hugetlbfs", 0, NULL);
 
 	if (nr_opt)
 		hugepages = SAFE_STRTOL(nr_opt, 0, LONG_MAX);
testcases/kernel/mem/hugetlb/hugemmap/hugemmap02.c +1 -2
index 86142484d..52db226fe 100644
@@ -142,8 +142,7 @@ static void setup(void)
 
 	if (!Hopt)
 		Hopt = tst_get_tmpdir();
-	if (mount("none", Hopt, "hugetlbfs", 0, NULL) < 0)
-		tst_brk(TBROK | TERRNO, "mount failed on %s", Hopt);
+	SAFE_MOUNT("none", Hopt, "hugetlbfs", 0, NULL);
 
 	if (nr_opt)
 		hugepages = SAFE_STRTOL(nr_opt, 0, LONG_MAX);
testcases/kernel/mem/hugetlb/hugemmap/hugemmap04.c +1 -2
index f6d51a5b6..2a368a390 100644
@@ -113,8 +113,7 @@ void setup(void)
 
 	if (!Hopt)
 		Hopt = tst_get_tmpdir();
-	if (mount("none", Hopt, "hugetlbfs", 0, NULL) < 0)
-		tst_brk(TBROK | TERRNO, "mount failed on %s", Hopt);
+	SAFE_MOUNT("none", Hopt, "hugetlbfs", 0, NULL);
 
 	if (nr_opt)
 		hugepages = SAFE_STRTOL(nr_opt, 0, LONG_MAX);
testcases/kernel/syscalls/acct/acct01.c +3 -9
index 3137c05c1..eb104315b 100644
@@ -152,20 +152,14 @@ static void setup(void)
 
 	tst_mkfs(cleanup, device, fs_type, NULL, NULL);
 	SAFE_MKDIR(cleanup, "mntpoint", DIR_MODE);
-	if (mount(device, "mntpoint", fs_type, 0, NULL) < 0) {
-		tst_brkm(TBROK | TERRNO, cleanup,
-			 "mount device:%s failed", device);
-	}
+	SAFE_MOUNT(cleanup, device, "mntpoint", fs_type, 0, NULL);
 	mount_flag = 1;
 
 	/* Create a file in the file system, then remount it as read-only */
 	fd = SAFE_CREAT(cleanup, TEST_FILE8, 0644);
 	SAFE_CLOSE(cleanup, fd);
-	if (mount(device, "mntpoint", fs_type,
-		  MS_REMOUNT | MS_RDONLY, NULL) < 0) {
-		tst_brkm(TBROK | TERRNO, cleanup,
-			 "mount device:%s failed", device);
-	}
+	SAFE_MOUNT(cleanup, device, "mntpoint", fs_type,
+		   MS_REMOUNT | MS_RDONLY, NULL);
 }
 
 static void acct_verify(int i)
testcases/kernel/syscalls/chmod/chmod06.c +1 -4
index 0cb060e27..acd653f7d 100644
@@ -199,10 +199,7 @@ void setup(void)
 	/*
 	 * mount a read-only file system for test EROFS
 	 */
-	if (mount(device, MNT_POINT, fs_type, MS_RDONLY, NULL) < 0) {
-		tst_brkm(TBROK | TERRNO, cleanup,
-			 "mount device:%s failed", device);
-	}
+	SAFE_MOUNT(cleanup, device, MNT_POINT, fs_type, MS_RDONLY, NULL);
 	mount_flag = 1;
 
 	memset(long_path, 'a', PATH_MAX+1);
testcases/kernel/syscalls/chown/chown04.c +1 -4
index 782706789..1f3ed412b 100644
@@ -172,10 +172,7 @@ static void setup(void)
 	SAFE_TOUCH(cleanup, TEST_FILE2, 0666, NULL);
 
 	SAFE_MKDIR(cleanup, "mntpoint", DIR_MODE);
-	if (mount(device, "mntpoint", fs_type, MS_RDONLY, NULL) < 0) {
-		tst_brkm(TBROK | TERRNO, cleanup,
-			 "mount device:%s failed", device);
-	}
+	SAFE_MOUNT(cleanup, device, "mntpoint", fs_type, MS_RDONLY, NULL);
 	mount_flag = 1;
 
 	SAFE_SETEUID(cleanup, ltpuser->pw_uid);
testcases/kernel/syscalls/fchmod/fchmod06.c +3 -9
index 619d54e3a..a5c6d911c 100644
@@ -131,20 +131,14 @@ static void setup(void)
 
 	SAFE_MKDIR(cleanup, "mntpoint", 0755);
 
-	if (mount(device, "mntpoint", fs_type, 0, NULL) < 0) {
-		tst_brkm(TBROK | TERRNO, cleanup,
-			 "mount device:%s failed", device);
-	}
+	SAFE_MOUNT(cleanup, device, "mntpoint", fs_type, 0, NULL);
 	mount_flag = 1;
 
 	/* Create a file in the file system, then remount it as read-only */
 	SAFE_TOUCH(cleanup, "mntpoint/tfile_3", 0644, NULL);
 
-	if (mount(device, "mntpoint", fs_type,
-		  MS_REMOUNT | MS_RDONLY, NULL) < 0) {
-		tst_brkm(TBROK | TERRNO, cleanup,
-			 "mount device:%s failed", device);
-	}
+	SAFE_MOUNT(cleanup, device, "mntpoint", fs_type,
+		   MS_REMOUNT | MS_RDONLY, NULL);
 
 	fd3 = SAFE_OPEN(cleanup, "mntpoint/tfile_3", O_RDONLY);
 
testcases/kernel/syscalls/fchown/fchown04.c +3 -9
index bae0796b4..12e332671 100644
@@ -115,17 +115,11 @@ static void setup(void)
 
 	tst_mkfs(cleanup, device, fs_type, NULL, NULL);
 	SAFE_MKDIR(cleanup, "mntpoint", DIR_MODE);
-	if (mount(device, "mntpoint", fs_type, 0, NULL) < 0) {
-		tst_brkm(TBROK | TERRNO, cleanup,
-			 "mount device:%s failed", device);
-	}
+	SAFE_MOUNT(cleanup, device, "mntpoint", fs_type, 0, NULL);
 	mount_flag = 1;
 	SAFE_TOUCH(cleanup, "mntpoint/tfile_3", 0644, NULL);
-	if (mount(device, "mntpoint", fs_type,
-		  MS_REMOUNT | MS_RDONLY, NULL) < 0) {
-		tst_brkm(TBROK | TERRNO, cleanup,
-			 "mount device:%s failed", device);
-	}
+	SAFE_MOUNT(cleanup, device, "mntpoint", fs_type,
+		   MS_REMOUNT | MS_RDONLY, NULL);
 	fd3 = SAFE_OPEN(cleanup, "mntpoint/tfile_3", O_RDONLY);
 
 	ltpuser = SAFE_GETPWNAM(cleanup, "nobody");
testcases/kernel/syscalls/lchown/lchown03.c +1 -4
index 3f66a3885..c26f54c21 100644
@@ -109,10 +109,7 @@ static void setup(void)
 
 	tst_mkfs(cleanup, device, fs_type, NULL, NULL);
 	SAFE_MKDIR(cleanup, TEST_EROFS, DIR_MODE);
-	if (mount(device, TEST_EROFS, fs_type, MS_RDONLY, NULL) < 0) {
-		tst_brkm(TBROK | TERRNO, cleanup,
-			 "mount device:%s failed", device);
-	}
+	SAFE_MOUNT(cleanup, device, TEST_EROFS, fs_type, MS_RDONLY, NULL);
 	mount_flag = 1;
 }
 
testcases/kernel/syscalls/link/link08.c +3 -9
index 05662e21d..8cb72d1df 100644
@@ -145,18 +145,12 @@ static void setup(void)
 
 	tst_mkfs(cleanup, device, fs_type, NULL, NULL);
 	SAFE_MKDIR(cleanup, MNT_POINT, DIR_MODE);
-	if (mount(device, MNT_POINT, fs_type, 0, NULL) < 0) {
-		tst_brkm(TBROK | TERRNO, cleanup,
-			 "mount device:%s failed", device);
-	}
+	SAFE_MOUNT(cleanup, device, MNT_POINT, fs_type, 0, NULL);
 	mount_flag = 1;
 
 	SAFE_TOUCH(cleanup, TEST_FILE2, 0644, NULL);
-	if (mount(device, MNT_POINT, fs_type,
-		  MS_REMOUNT | MS_RDONLY, NULL) < 0) {
-		tst_brkm(TBROK | TERRNO, cleanup,
-			 "mount device:%s failed", device);
-	}
+	SAFE_MOUNT(cleanup, device, MNT_POINT, fs_type,
+		   MS_REMOUNT | MS_RDONLY, NULL);
 }
 
 static void cleanup(void)
testcases/kernel/syscalls/linkat/linkat02.c +3 -9
index 292d9f57a..51785af02 100644
@@ -173,10 +173,7 @@ static void setup(void)
 	tst_mkfs(cleanup, device, fs_type, NULL, NULL);
 	SAFE_MKDIR(cleanup, "mntpoint", DIR_MODE);
 
-	if (mount(device, "mntpoint", fs_type, 0, NULL) < 0) {
-		tst_brkm(TBROK | TERRNO, cleanup,
-			 "mount device:%s failed", device);
-	}
+	SAFE_MOUNT(cleanup, device, "mntpoint", fs_type, 0, NULL);
 	mount_flag = 1;
 
 	max_hardlinks = tst_fs_fill_hardlinks(cleanup, "emlink_dir");
@@ -194,11 +191,8 @@ static void cleanup_eacces(void)
 
 static void setup_erofs(void)
 {
-	if (mount(device, "mntpoint", fs_type,
-		  MS_REMOUNT | MS_RDONLY, NULL) < 0) {
-		tst_brkm(TBROK | TERRNO, cleanup, "remount device:%s failed",
-			 device);
-	}
+	SAFE_MOUNT(cleanup, device, "mntpoint", fs_type,
+		   MS_REMOUNT | MS_RDONLY, NULL);
 	mount_flag = 1;
 }
 
testcases/kernel/syscalls/mkdir/mkdir03.c +1 -4
index 5b43ab408..823ee9dd1 100644
@@ -124,10 +124,7 @@ static void setup(void)
 
 	tst_mkfs(cleanup, device, fs_type, NULL, NULL);
 	SAFE_MKDIR(cleanup, MNT_POINT, DIR_MODE);
-	if (mount(device, MNT_POINT, fs_type, MS_RDONLY, NULL) < 0) {
-		tst_brkm(TBROK | TERRNO, cleanup,
-			 "mount device:%s failed", device);
-	}
+	SAFE_MOUNT(cleanup, device, MNT_POINT, fs_type, MS_RDONLY, NULL);
 	mount_flag = 1;
 }
 
testcases/kernel/syscalls/mkdirat/mkdirat02.c +3 -8
index e4dd999b9..9dcf37fb1 100644
@@ -119,17 +119,12 @@ static void setup(void)
 	tst_mkfs(cleanup, device, fs_type, NULL, NULL);
 
 	SAFE_MKDIR(cleanup, "test_dir/mntpoint", DIR_MODE);
-	if (mount(device, "test_dir/mntpoint", fs_type, MS_RDONLY, NULL) < 0) {
-		tst_brkm(TBROK | TERRNO, cleanup,
-			 "mount device:%s failed", device);
-	}
+	SAFE_MOUNT(cleanup, device, "test_dir/mntpoint", fs_type, MS_RDONLY,
+		   NULL);
 	mount_flag_dir = 1;
 
 	SAFE_MKDIR(cleanup, "mntpoint", DIR_MODE);
-	if (mount(device, "mntpoint", fs_type, MS_RDONLY, NULL) < 0) {
-		tst_brkm(TBROK | TERRNO, cleanup,
-			 "mount device:%s failed", device);
-	}
+	SAFE_MOUNT(cleanup, device, "mntpoint", fs_type, MS_RDONLY, NULL);
 	mount_flag_cur = 1;
 }
 
testcases/kernel/syscalls/mknod/mknod07.c +1 -4
index bdb664029..69cff02d9 100644
@@ -129,10 +129,7 @@ static void setup(void)
 
 	/* mount a read-only file system for EROFS test */
 	SAFE_MKDIR(cleanup, MNT_POINT, DIR_MODE);
-	if (mount(device, MNT_POINT, fs_type, MS_RDONLY, NULL) < 0) {
-		tst_brkm(TBROK | TERRNO, cleanup,
-			 "mount device:%s failed", device);
-	}
+	SAFE_MOUNT(cleanup, device, MNT_POINT, fs_type, MS_RDONLY, NULL);
 	mount_flag = 1;
 
 	ltpuser = SAFE_GETPWNAM(cleanup, "nobody");
testcases/kernel/syscalls/mknodat/mknodat02.c +1 -4
index 0216aa2f3..6c5054bbc 100644
@@ -131,10 +131,7 @@ static void setup(void)
 	 * mount a read-only file system for EROFS test
 	 */
 	SAFE_MKDIR(cleanup, MNT_POINT, DIR_MODE);
-	if (mount(device, MNT_POINT, fs_type, MS_RDONLY, NULL) < 0) {
-		tst_brkm(TBROK | TERRNO, cleanup,
-			 "mount device:%s failed", device);
-	}
+	SAFE_MOUNT(cleanup, device, MNT_POINT, fs_type, MS_RDONLY, NULL);
 	mount_flag = 1;
 	dir_fd = SAFE_OPEN(cleanup, MNT_POINT, O_DIRECTORY);
 
testcases/kernel/syscalls/mount/mount05.c +1 -2
index bd664fd39..ca26f9738 100644
@@ -106,8 +106,7 @@ void setup(void)
 	if (dflag) {
 		tst_mkfs(NULL, device, fstype, NULL, NULL);
 
-		if (mount(device, mntpoint_src, fstype, 0, NULL) == -1)
-			tst_brkm(TBROK | TERRNO, cleanup, "mount failed");
+		SAFE_MOUNT(cleanup, device, mntpoint_src, fstype, 0, NULL);
 	}
 
 	SAFE_FILE_PRINTF(cleanup, file_src, "TEST FILE");
testcases/kernel/syscalls/mount/mount06.c +3 -7
index 143c9aff2..857f5f905 100644
@@ -69,9 +69,7 @@ int main(int argc, char *argv[])
 
 		tst_count = 0;
 
-		if (mount(device, mntpoint_src, fs_type, 0, NULL) == -1)
-			tst_brkm(TBROK | TERRNO, cleanup, "mount %s failed",
-				 mntpoint_src);
+		SAFE_MOUNT(cleanup, device, mntpoint_src, fs_type, 0, NULL);
 
 		TEST(mount(mntpoint_src, mntpoint_des, fs_type, MS_MOVE, NULL));
 
@@ -138,13 +136,11 @@ static void setup(void)
 	 * Turn current dir into a private mount point being a parent
 	 * mount which is required by move mount.
 	 */
-	if (mount(path_name, path_name, "none", MS_BIND, NULL) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "bind mount failed");
+	SAFE_MOUNT(cleanup, path_name, path_name, "none", MS_BIND, NULL);
 
 	mount_flag = 1;
 
-	if (mount("none", path_name, "none", MS_PRIVATE, NULL) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "mount private failed");
+	SAFE_MOUNT(cleanup, "none", path_name, "none", MS_PRIVATE, NULL);
 
 	snprintf(mntpoint_src, PATH_MAX, "%s/%s", path_name, MNTPOINT_SRC);
 	snprintf(mntpoint_des, PATH_MAX, "%s/%s", path_name, MNTPOINT_DES);
testcases/kernel/syscalls/rename/rename11.c +4 -12
index 0c3e3b37c..17cf04c85 100644
@@ -107,10 +107,7 @@ static void setup(void)
 	tst_mkfs(cleanup, device, fs_type, NULL, NULL);
 
 	SAFE_MKDIR(cleanup, MNTPOINT, 0755);
-	if (mount(device, MNTPOINT, fs_type, 0, NULL) < 0) {
-		tst_brkm(TBROK | TERRNO, cleanup,
-			 "mount device:%s failed", device);
-	}
+	SAFE_MOUNT(cleanup, device, MNTPOINT, fs_type, 0, NULL);
 	mount_flag = 1;
 	SAFE_TOUCH(cleanup, TEST_EROFS, 0644, NULL);
 
@@ -152,10 +149,8 @@ static void test_eloop(void)
 
 static void test_erofs(void)
 {
-	if (mount(device, MNTPOINT, fs_type, MS_REMOUNT | MS_RDONLY, NULL) < 0) {
-		tst_brkm(TBROK | TERRNO, cleanup,
-			 "mount device:%s failed", device);
-	}
+	SAFE_MOUNT(cleanup, device, MNTPOINT, fs_type, MS_REMOUNT | MS_RDONLY,
+		   NULL);
 
 	TEST(rename(TEST_EROFS, TEST_NEW_EROFS));
 	check_and_print(EROFS);
@@ -163,10 +158,7 @@ static void test_erofs(void)
 	if (TEST_RETURN == 0)
 		SAFE_UNLINK(cleanup, TEST_NEW_EROFS);
 
-	if (mount(device, MNTPOINT, fs_type, MS_REMOUNT, NULL) < 0) {
-		tst_brkm(TBROK | TERRNO, cleanup,
-			 "remount device:%s failed", device);
-	}
+	SAFE_MOUNT(cleanup, device, MNTPOINT, fs_type, MS_REMOUNT, NULL);
 }
 
 static void test_emlink(void)
testcases/kernel/syscalls/renameat/renameat01.c +3 -9
index f6797da31..817e21729 100644
@@ -184,17 +184,11 @@ static void setup(void)
 
 	tst_mkfs(cleanup, device, fs_type, NULL, NULL);
 	SAFE_MKDIR(cleanup, MNTPOINT, DIRMODE);
-	if (mount(device, MNTPOINT, fs_type, 0, NULL) < 0) {
-		tst_brkm(TBROK | TERRNO, cleanup,
-			"mount device:%s failed", device);
-	}
+	SAFE_MOUNT(cleanup, device, MNTPOINT, fs_type, 0, NULL);
 	mount_flag = 1;
 	SAFE_TOUCH(cleanup, TESTFILE5, FILEMODE, NULL);
-	if (mount(device, MNTPOINT, fs_type,
-			MS_REMOUNT | MS_RDONLY, NULL) < 0) {
-		tst_brkm(TBROK | TERRNO, cleanup,
-			"mount device:%s failed", device);
-	}
+	SAFE_MOUNT(cleanup, device, MNTPOINT, fs_type, MS_REMOUNT | MS_RDONLY,
+		   NULL);
 
 	SAFE_MKDIR(cleanup, TESTDIR3, DIRMODE);
 	max_subdirs = tst_fs_fill_subdirs(cleanup, "testemlinkdir");
testcases/kernel/syscalls/rmdir/rmdir02.c +3 -9
index 9e7581830..9f6954ad5 100644
@@ -139,16 +139,10 @@ static void setup(void)
 
 	tst_mkfs(cleanup, device, fs_type, NULL, NULL);
 	SAFE_MKDIR(cleanup, MNTPOINT, DIR_MODE);
-	if (mount(device, MNTPOINT, fs_type, 0, NULL) == -1) {
-		tst_brkm(TBROK | TERRNO, cleanup,
-			"mount device:%s failed", device);
-	}
+	SAFE_MOUNT(cleanup, device, MNTPOINT, fs_type, 0, NULL);
 	SAFE_MKDIR(cleanup, TESTDIR5, DIR_MODE);
-	if (mount(device, MNTPOINT, fs_type, MS_REMOUNT | MS_RDONLY,
-			NULL) == -1) {
-		tst_brkm(TBROK | TERRNO, cleanup,
-			"mount device:%s failed", device);
-	}
+	SAFE_MOUNT(cleanup, device, MNTPOINT, fs_type, MS_REMOUNT | MS_RDONLY,
+		   NULL);
 	mount_flag = 1;
 
 	SAFE_MKDIR(cleanup, TESTDIR, DIR_MODE);
testcases/kernel/syscalls/utime/utime06.c +1 -4
index 2a97cce3b..6d80677e4 100644
@@ -122,10 +122,7 @@ static void setup(void)
 	tst_mkfs(cleanup, dev, fs_type, NULL, NULL);
 
 	SAFE_MKDIR(cleanup, MNT_POINT, 0644);
-	if (mount(dev, MNT_POINT, fs_type, MS_RDONLY, NULL) < 0) {
-		tst_brkm(TBROK | TERRNO, cleanup,
-			 "mount device:%s failed", dev);
-	}
+	SAFE_MOUNT(cleanup, dev, MNT_POINT, fs_type, MS_RDONLY, NULL);
 	mount_flag = 1;
 
 	ltpuser = SAFE_GETPWNAM(cleanup, "nobody");
testcases/kernel/syscalls/utimes/utimes01.c +3 -9
index b2450d2b7..c79bd092c 100644
@@ -141,20 +141,14 @@ static void setup(void)
 
 	tst_mkfs(cleanup, device, fs_type, NULL, NULL);
 	SAFE_MKDIR(cleanup, MNTPOINT, DIR_MODE);
-	if (mount(device, MNTPOINT, fs_type, 0, NULL) == -1) {
-		tst_brkm(TBROK | TERRNO, cleanup,
-			"mount device:%s failed", device);
-	}
+	SAFE_MOUNT(cleanup, device, MNTPOINT, fs_type, 0, NULL);
 	mount_flag = 1;
 	SAFE_TOUCH(cleanup, TESTFILE3, FILE_MODE, NULL);
 	ltpuser = SAFE_GETPWNAM(cleanup, LTPUSER1);
 	SAFE_CHOWN(cleanup, TESTFILE3, ltpuser->pw_uid,
 		ltpuser->pw_gid);
-	if (mount(device, MNTPOINT, fs_type,
-			MS_REMOUNT | MS_RDONLY, NULL) == -1) {
-		tst_brkm(TBROK | TERRNO, cleanup,
-			"mount device:%s failed", device);
-	}
+	SAFE_MOUNT(cleanup, device, MNTPOINT, fs_type, MS_REMOUNT | MS_RDONLY,
+		   NULL);
 
 	ltpuser = SAFE_GETPWNAM(cleanup, LTPUSER1);
 	SAFE_SETEUID(cleanup, ltpuser->pw_uid);
Signature · 1 line
2.13.5
#24 lore raw

PATCH 25/40 Make use of SAFE_UMOUNT()

In reply to Cyril Hrubis
---
 testcases/kernel/controllers/cgroup_xattr/cgroup_xattr.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
testcases/kernel/controllers/cgroup_xattr/cgroup_xattr.c +1 -4
index a0e28aa30..e165f6c9a 100644
@@ -241,10 +241,7 @@ static void cleanup(void)
 			SAFE_CHDIR(NULL, "..");
 		}
 		if (cgrp_opt[i].mounted) {
-			if (umount(cgrp_opt[i].dir) == -1) {
-				tst_brkm(TBROK | TERRNO, NULL,
-					"Can't unmount: %s", cgrp_opt[i].dir);
-			}
+			SAFE_UMOUNT(NULL, cgrp_opt[i].dir);
 		}
 	}
 
Signature · 1 line
2.13.5
#25 lore raw

PATCH 26/40 Make use of SAFE_CLOSEDIR()

In reply to Cyril Hrubis
---
 testcases/kernel/controllers/cgroup_xattr/cgroup_xattr.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
testcases/kernel/controllers/cgroup_xattr/cgroup_xattr.c +1 -2
index e165f6c9a..ad291a1eb 100644
@@ -226,8 +226,7 @@ static void cleanup(void)
 
 	int i;
 	for (i = 0; i < odir_num; ++i) {
-		if (closedir(odir[i]) == -1)
-			tst_brkm(TBROK, NULL, "Failed to close dir\n");
+		SAFE_CLOSEDIR(NULL, odir[i]);
 	}
 
 	char *cwd = tst_get_tmpdir();
Signature · 1 line
2.13.5
#26 lore raw

PATCH 27/40 Make use of SAFE_IOCTL()

In reply to Cyril Hrubis
---
 testcases/kernel/syscalls/ioctl/ioctl02.c         | 8 ++------
 testcases/kernel/syscalls/sockioctl/sockioctl01.c | 6 ++----
 2 files changed, 4 insertions(+), 10 deletions(-)
testcases/kernel/syscalls/ioctl/ioctl02.c +2 -6
index 9201ce205..48e8eaa7f 100644
@@ -370,9 +370,7 @@ static int do_parent_setup(void)
 	closed = 0;
 
 	/* flush tty queues to remove old output */
-	if (ioctl(pfd, TCFLSH, 2) < 0)
-		tst_brkm(TBROK, cleanup, "ioctl TCFLSH failed : "
-			 "errno = %d", errno);
+	SAFE_IOCTL(cleanup, pfd, TCFLSH, 2);
 	return pfd;
 }
 
@@ -438,9 +436,7 @@ static void setup(void)
 			 "setup(), errno = %d", devname, errno);
 
 	/* Save the current device information - to be restored in cleanup() */
-	if (ioctl(fd, TCGETA, &save_io) < 0)
-		tst_brkm(TBROK, cleanup, "TCGETA ioctl failed in "
-			 "do_parent_setup");
+	SAFE_IOCTL(cleanup, fd, TCGETA, &save_io);
 
 	/* Close the device */
 	SAFE_CLOSE(cleanup, fd);
testcases/kernel/syscalls/sockioctl/sockioctl01.c +2 -4
index 50bdc935a..ce1831cc8 100644
@@ -40,6 +40,7 @@
 #include <net/if.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 char *TCID = "sockioctl01";
 int testno;
@@ -235,10 +236,7 @@ static void setup2(void)
 static void setup3(void)
 {
 	setup2();
-	if (ioctl(s, SIOCGIFCONF, &ifc) < 0) {
-		tst_brkm(TBROK, cleanup, "socket setup failed: %s",
-			 strerror(errno));
-	}
+	SAFE_IOCTL(cleanup, s, SIOCGIFCONF, &ifc);
 	ifr = *(struct ifreq *)ifc.ifc_buf;
 }
 
Signature · 1 line
2.13.5
#27 lore raw

PATCH 28/40 Make use of SAFE_FCNTL()

In reply to Cyril Hrubis
---
 testcases/kernel/syscalls/fcntl/fcntl34.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
testcases/kernel/syscalls/fcntl/fcntl34.c +2 -4
index c778db086..aa29cf9ea 100644
@@ -76,15 +76,13 @@ void *thread_fn_01(void *arg)
 
 	for (i = 0; i < writes_num; ++i) {
 		lck.l_type = F_WRLCK;
-		if (fcntl(fd, F_OFD_SETLKW, &lck) == -1)
-			tst_brk(TBROK | TERRNO, "fcntl() failed");
+		SAFE_FCNTL(fd, F_OFD_SETLKW, &lck);
 
 		SAFE_LSEEK(fd, 0, SEEK_END);
 		SAFE_WRITE(1, fd, buf, write_size);
 
 		lck.l_type = F_UNLCK;
-		if (fcntl(fd, F_OFD_SETLKW, &lck) == -1)
-			tst_brk(TBROK | TERRNO, "fcntl() failed");
+		SAFE_FCNTL(fd, F_OFD_SETLKW, &lck);
 
 		sched_yield();
 	}
Signature · 1 line
2.13.5
#28 lore raw

PATCH 29/40 Make use of SAFE_FTRUNCATE()

In reply to Cyril Hrubis
---
 testcases/kernel/syscalls/fsync/fsync02.c | 4 ++--
 testcases/kernel/syscalls/mlock/mlock04.c | 3 +--
 2 files changed, 3 insertions(+), 4 deletions(-)
testcases/kernel/syscalls/fsync/fsync02.c +2 -2
index 2673edc9e..1e5f9ee00 100644
@@ -53,6 +53,7 @@
 #include <errno.h>
 #include <sys/resource.h>
 #include "test.h"
+#include "safe_macros.h"
 #include <time.h>
 
 #define BLOCKSIZE 8192
@@ -138,8 +139,7 @@ int main(int ac, char **av)
 			tst_resm(TPASS, "fsync succeeded in an "
 				 "acceptable amount of time");
 
-		if (ftruncate(fd, 0) == -1)
-			tst_brkm(TBROK, cleanup, "ftruncate failed");
+		SAFE_FTRUNCATE(cleanup, fd, 0);
 	}
 
 	sync();
testcases/kernel/syscalls/mlock/mlock04.c +1 -2
index d1f3c9d0d..fb541ec08 100644
@@ -91,8 +91,7 @@ static void setup(void)
 	if (fd == -1)
 		tst_brkm(TBROK | TERRNO, cleanup, "open");
 
-	if (ftruncate(fd, file_len) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "ftruncate");
+	SAFE_FTRUNCATE(cleanup, fd, file_len);
 
 	TEST_PAUSE;
 }
Signature · 1 line
2.13.5
#29 lore raw

PATCH 30/40 Make use of SAFE_STAT()

In reply to Cyril Hrubis
---
 testcases/kernel/syscalls/asyncio/asyncio02.c   |  4 +---
 testcases/kernel/syscalls/mkdir/mkdir05.c       |  5 +----
 testcases/kernel/syscalls/mknod/mknod02.c       |  4 +---
 testcases/kernel/syscalls/mknod/mknod03.c       |  4 +---
 testcases/kernel/syscalls/mknod/mknod04.c       |  4 +---
 testcases/kernel/syscalls/mknod/mknod05.c       |  4 +---
 testcases/kernel/syscalls/mknod/mknod08.c       |  4 +---
 testcases/kernel/syscalls/mount/mount03.c       |  3 +--
 testcases/kernel/syscalls/rename/rename01.c     | 18 +++---------------
 testcases/kernel/syscalls/rename/rename03.c     | 18 +++---------------
 testcases/kernel/syscalls/rename/rename04.c     | 12 ++----------
 testcases/kernel/syscalls/rename/rename05.c     | 12 ++----------
 testcases/kernel/syscalls/rename/rename06.c     | 12 ++----------
 testcases/kernel/syscalls/rename/rename07.c     | 12 ++----------
 testcases/kernel/syscalls/rename/rename12.c     |  5 +----
 testcases/kernel/syscalls/rename/rename13.c     | 18 +++---------------
 testcases/kernel/syscalls/sendfile/sendfile02.c |  5 ++---
 testcases/kernel/syscalls/sendfile/sendfile04.c |  4 +---
 testcases/kernel/syscalls/sendfile/sendfile05.c |  5 ++---
 19 files changed, 31 insertions(+), 122 deletions(-)
testcases/kernel/syscalls/asyncio/asyncio02.c +1 -3
index 6f7ef9110..e532cc287 100644
@@ -248,9 +248,7 @@ int testrun(int flag, int bytes, int ti)
 	 *  same as the number of bytes in the file.
 	 */
 
-	if (stat(filename, &buffer) == -1) {
-		tst_brkm(TBROK | TERRNO, cleanup, "stat() failed");
-	}
+	SAFE_STAT(cleanup, filename, &buffer);
 
 	if (buffer.st_size != (off_t) (bytes * WRITES)) {
 		ret = (int)buffer.st_size;
testcases/kernel/syscalls/mkdir/mkdir05.c +1 -4
index ee52771c8..3d25c439d 100644
@@ -84,10 +84,7 @@ int main(int ac, char **av)
 			continue;
 		}
 
-		if (stat(tstdir1, &buf) == -1) {
-			tst_brkm(TBROK, cleanup, "failed to stat the "
-				 "new directory");
-		}
+		SAFE_STAT(cleanup, tstdir1, &buf);
 		/* check the owner */
 		if (buf.st_uid != geteuid()) {
 			tst_resm(TFAIL, "mkdir() FAILED to set owner ID"
testcases/kernel/syscalls/mknod/mknod02.c +1 -3
index 7ac8db5ef..e5741c3d6 100644
@@ -244,9 +244,7 @@ void setup(void)
 	 * Verify that test directory created with expected permission modes
 	 * and ownerships.
 	 */
-	if (stat(DIR_TEMP, &buf) < 0) {
-		tst_brkm(TBROK, cleanup, "stat(2) of %s failed", DIR_TEMP);
-	}
+	SAFE_STAT(cleanup, DIR_TEMP, &buf);
 
 	/* Verify modes of test directory */
 	if (buf.st_mode & S_ISGID) {
testcases/kernel/syscalls/mknod/mknod03.c +1 -3
index 8fd58a732..fc3468673 100644
@@ -242,9 +242,7 @@ void setup(void)
 	 * Verify that test directory created with expected permission modes
 	 * and ownerships.
 	 */
-	if (stat(DIR_TEMP, &buf) < 0) {
-		tst_brkm(TBROK, cleanup, "stat(2) of %s failed", DIR_TEMP);
-	}
+	SAFE_STAT(cleanup, DIR_TEMP, &buf);
 
 	/* Verify modes of test directory */
 	if (!(buf.st_mode & S_ISGID)) {
testcases/kernel/syscalls/mknod/mknod04.c +1 -3
index 9615c15a6..6a8e08e54 100644
@@ -244,9 +244,7 @@ void setup(void)
 	 * Verify that test directory created with expected permission modes
 	 * and ownerships.
 	 */
-	if (stat(DIR_TEMP, &buf) < 0) {
-		tst_brkm(TBROK, cleanup, "stat(2) of %s failed", DIR_TEMP);
-	}
+	SAFE_STAT(cleanup, DIR_TEMP, &buf);
 
 	/* Verify modes of test directory */
 	if (!(buf.st_mode & S_ISGID)) {
testcases/kernel/syscalls/mknod/mknod05.c +1 -3
index 57f097b9e..a00a0e441 100644
@@ -236,9 +236,7 @@ void setup(void)
 	 * Verify that test directory created with expected permission modes
 	 * and ownerships.
 	 */
-	if (stat(DIR_TEMP, &buf) < 0) {
-		tst_brkm(TBROK, cleanup, "stat(2) of %s failed", DIR_TEMP);
-	}
+	SAFE_STAT(cleanup, DIR_TEMP, &buf);
 	/* Verify modes of test directory */
 	if (!(buf.st_mode & S_ISGID)) {
 		tst_brkm(TBROK, cleanup,
testcases/kernel/syscalls/mknod/mknod08.c +1 -3
index 39c024833..3cc24ab65 100644
@@ -239,9 +239,7 @@ void setup(void)
 	 * Verify that test directory created with expected permission modes
 	 * and ownerships.
 	 */
-	if (stat(DIR_TEMP, &buf) < 0) {
-		tst_brkm(TBROK, cleanup, "stat(2) of %s failed", DIR_TEMP);
-	}
+	SAFE_STAT(cleanup, DIR_TEMP, &buf);
 
 	/* Verify modes of test directory */
 	if (buf.st_mode & S_ISGID) {
testcases/kernel/syscalls/mount/mount03.c +1 -2
index a8abbff73..bf78c797f 100644
@@ -264,8 +264,7 @@ int test_rwflag(int i, int cnt)
 		snprintf(file, PATH_MAX, "%ssetuid_test", path_name);
 		SAFE_FILE_PRINTF(cleanup, file, "TEST FILE");
 
-		if (stat(file, &file_stat) < 0)
-			tst_brkm(TBROK, cleanup, "stat for setuid_test failed");
+		SAFE_STAT(cleanup, file, &file_stat);
 
 		if (file_stat.st_mode != SUID_MODE &&
 		    chmod(file, SUID_MODE) < 0)
testcases/kernel/syscalls/rename/rename01.c +3 -15
index 80977d35f..e7de18ff4 100644
@@ -133,11 +133,7 @@ int main(int ac, char **av)
 				continue;
 			}
 
-			if (stat(TC[i].name2, &buf1) == -1) {
-				tst_brkm(TBROK, cleanup, "stat of %s "
-					 "failed", TC[i].desc);
-
-			}
+			SAFE_STAT(cleanup, TC[i].name2, &buf1);
 
 			/*
 			 * verify the new file or directory is the
@@ -194,11 +190,7 @@ void setup(void)
 
 	SAFE_TOUCH(cleanup, fname, 0700, NULL);
 
-	if (stat(fname, &buf1) == -1) {
-		tst_brkm(TBROK, cleanup, "failed to stat file %s"
-			 "in setup()", fname);
-
-	}
+	SAFE_STAT(cleanup, fname, &buf1);
 
 	f_olddev = buf1.st_dev;
 	f_oldino = buf1.st_ino;
@@ -206,11 +198,7 @@ void setup(void)
 	/* create "old" directory */
 	SAFE_MKDIR(cleanup, fdir, 00770);
 
-	if (stat(fdir, &buf1) == -1) {
-		tst_brkm(TBROK, cleanup, "failed to stat directory %s"
-			 "in setup()", fname);
-
-	}
+	SAFE_STAT(cleanup, fdir, &buf1);
 
 	d_olddev = buf1.st_dev;
 	d_oldino = buf1.st_ino;
testcases/kernel/syscalls/rename/rename03.c +3 -15
index 6ffbe5628..396e95c4d 100644
@@ -130,11 +130,7 @@ int main(int ac, char **av)
 				continue;
 			}
 
-			if (stat(TC[i].name2, &buf2) == -1) {
-				tst_brkm(TBROK, cleanup, "stat of %s "
-					 "failed", TC[i].desc);
-
-			}
+			SAFE_STAT(cleanup, TC[i].name2, &buf2);
 
 			/*
 			 * verify the new file or directory is the
@@ -201,11 +197,7 @@ void setup2(void)
 {
 	SAFE_TOUCH(cleanup, fname, 0700, NULL);
 
-	if (stat(fname, &buf1) == -1) {
-		tst_brkm(TBROK, cleanup, "failed to stat file %s"
-			 "in rename()", fname);
-
-	}
+	SAFE_STAT(cleanup, fname, &buf1);
 
 	/* save original file's dev and ino */
 	f_olddev = buf1.st_dev;
@@ -215,11 +207,7 @@ void setup2(void)
 
 	/* create "old" directory */
 	SAFE_MKDIR(cleanup, fdir, 00770);
-	if (stat(fdir, &buf1) == -1) {
-		tst_brkm(TBROK, cleanup, "failed to stat directory %s"
-			 "in rename()", fdir);
-
-	}
+	SAFE_STAT(cleanup, fdir, &buf1);
 
 	d_olddev = buf1.st_dev;
 	d_oldino = buf1.st_ino;
testcases/kernel/syscalls/rename/rename04.c +2 -10
index 5c6d4fda8..32594a776 100644
@@ -150,11 +150,7 @@ void setup(void)
 	/* create "old" directory */
 	SAFE_MKDIR(cleanup, fdir, 00770);
 
-	if (stat(fdir, &buf1) == -1) {
-		tst_brkm(TBROK, cleanup, "failed to stat directory %s"
-			 "in rename()", fdir);
-
-	}
+	SAFE_STAT(cleanup, fdir, &buf1);
 
 	/* save "old"'s dev and ino */
 	olddev = buf1.st_dev;
@@ -165,11 +161,7 @@ void setup(void)
 
 	SAFE_TOUCH(cleanup, tstfile, 0700, NULL);
 
-	if (stat(mdir, &buf2) == -1) {
-		tst_brkm(TBROK, cleanup, "failed to stat directory %s "
-			 "in rename()", mdir);
-
-	}
+	SAFE_STAT(cleanup, mdir, &buf2);
 
 	/* save "new"'s dev and ino */
 	olddev1 = buf2.st_dev;
testcases/kernel/syscalls/rename/rename05.c +2 -10
index b60da57f8..db10720fe 100644
@@ -140,11 +140,7 @@ void setup(void)
 
 	/* create "old" file */
 	SAFE_TOUCH(cleanup, fname, 0700, NULL);
-	if (stat(fname, &buf1) == -1) {
-		tst_brkm(TBROK, cleanup, "failed to stat file %s"
-			 "in rename()", fname);
-
-	}
+	SAFE_STAT(cleanup, fname, &buf1);
 
 	/* save "old"'s dev and ino */
 	olddev = buf1.st_dev;
@@ -157,11 +153,7 @@ void setup(void)
 
 	SAFE_MKDIR(cleanup, mdir, 00770);
 
-	if (stat(mdir, &buf2) == -1) {
-		tst_brkm(TBROK, cleanup, "failed to stat directory %s "
-			 "in rename()", mdir);
-
-	}
+	SAFE_STAT(cleanup, mdir, &buf2);
 
 	/* save "new"'s dev and ino */
 	olddev1 = buf2.st_dev;
testcases/kernel/syscalls/rename/rename06.c +2 -10
index 8ea96fd70..e415b4158 100644
@@ -141,11 +141,7 @@ void setup(void)
 		tst_brkm(TBROK, cleanup, "tmp directory %s found!", fdir);
 	}
 	SAFE_MKDIR(cleanup, fdir, 00770);
-	if (stat(fdir, &buf1) == -1) {
-		tst_brkm(TBROK, cleanup, "failed to stat directory %s "
-			 "in rename()", fdir);
-
-	}
+	SAFE_STAT(cleanup, fdir, &buf1);
 	/* save "old"'s dev and ino */
 	olddev = buf1.st_dev;
 	oldino = buf1.st_ino;
@@ -156,11 +152,7 @@ void setup(void)
 	}
 	SAFE_MKDIR(cleanup, mdir, 00770);
 
-	if (stat(mdir, &buf2) == -1) {
-		tst_brkm(TBROK, cleanup, "failed to stat directory %s "
-			 "in rename()", mdir);
-
-	}
+	SAFE_STAT(cleanup, mdir, &buf2);
 
 	/* save "new"'s dev and ino */
 	olddev1 = buf2.st_dev;
testcases/kernel/syscalls/rename/rename07.c +2 -10
index 061d73556..5b95f84bb 100644
@@ -125,11 +125,7 @@ void setup(void)
 
 	SAFE_MKDIR(cleanup, fdir, 00770);
 
-	if (stat(fdir, &buf1) == -1) {
-		tst_brkm(TBROK, cleanup, "failed to stat directory %s "
-			 "in rename()", fdir);
-
-	}
+	SAFE_STAT(cleanup, fdir, &buf1);
 
 	/* save "old"'s dev and ino */
 	olddev = buf1.st_dev;
@@ -137,11 +133,7 @@ void setup(void)
 
 	SAFE_TOUCH(cleanup, mname, 0700, NULL);
 
-	if (stat(mname, &buf2) == -1) {
-		tst_brkm(TBROK, cleanup, "failed to stat file %s in rename()",
-			 mname);
-
-	}
+	SAFE_STAT(cleanup, mname, &buf2);
 
 	/* save "new"'s dev and ino */
 	olddev1 = buf2.st_dev;
testcases/kernel/syscalls/rename/rename12.c +1 -4
index 3555b41c3..36691783f 100644
@@ -196,10 +196,7 @@ void setup(void)
 	/* create a directory */
 	SAFE_MKDIR(cleanup, fdir, PERMS);
 
-	if (stat(fdir, &buf1) == -1) {
-		tst_brkm(TBROK, cleanup, "failed to stat directory %s", fdir);
-
-	}
+	SAFE_STAT(cleanup, fdir, &buf1);
 
 	/* set the sticky bit */
 	if (chmod(fdir, buf1.st_mode | S_ISVTX) != 0) {
testcases/kernel/syscalls/rename/rename13.c +3 -15
index 72394a2d7..07a90c4d1 100644
@@ -114,18 +114,10 @@ int main(int ac, char **av)
 		}
 
 		/* check the existence of "new", and get the status */
-		if (stat(mname, &buf2) == -1) {
-			tst_brkm(TBROK, cleanup, "failed to stat file "
-				 "%s in rename()", mname);
-
-		}
+		SAFE_STAT(cleanup, mname, &buf2);
 
 		/* check the existence of "old", and get the status */
-		if (stat(fname, &buf1) == -1) {
-			tst_brkm(TBROK, cleanup, "failed to stat file "
-				 "%s in rename()", fname);
-
-		}
+		SAFE_STAT(cleanup, fname, &buf1);
 
 		/* verify the new file is the same as the original */
 		if (buf2.st_dev != olddev || buf2.st_ino != oldino) {
@@ -169,11 +161,7 @@ void setup(void)
 
 	SAFE_TOUCH(cleanup, fname, 0700, NULL);
 
-	if (stat(fname, &buf1) == -1) {
-		tst_brkm(TBROK, cleanup, "failed to stat file %s"
-			 "in rename()", fname);
-
-	}
+	SAFE_STAT(cleanup, fname, &buf1);
 
 	/* save the dev and inode */
 	olddev = buf1.st_dev;
testcases/kernel/syscalls/sendfile/sendfile02.c +2 -3
index ba12308b3..111b2741a 100644
@@ -57,6 +57,7 @@
 #include <unistd.h>
 #include <inttypes.h>
 #include "test.h"
+#include "safe_macros.h"
 
 #ifndef OFF_T
 #define OFF_T off_t
@@ -107,9 +108,7 @@ void do_sendfile(OFF_T offset, int i)
 	if ((in_fd = open(in_file, O_RDONLY)) < 0) {
 		tst_brkm(TBROK, cleanup, "open failed: %d", errno);
 	}
-	if (stat(in_file, &sb) < 0) {
-		tst_brkm(TBROK, cleanup, "stat failed: %d", errno);
-	}
+	SAFE_STAT(cleanup, in_file, &sb);
 
 	if ((before_pos = lseek(in_fd, 0, SEEK_CUR)) < 0) {
 		tst_brkm(TBROK, cleanup,
testcases/kernel/syscalls/sendfile/sendfile04.c +1 -3
index 779e7dc70..13389150c 100644
@@ -115,9 +115,7 @@ void do_sendfile(int prot, int pass_unmapped_buffer)
 	if ((in_fd = open(in_file, O_RDONLY)) < 0) {
 		tst_brkm(TBROK, cleanup, "open failed: %d", errno);
 	}
-	if (stat(in_file, &sb) < 0) {
-		tst_brkm(TBROK, cleanup, "stat failed: %d", errno);
-	}
+	SAFE_STAT(cleanup, in_file, &sb);
 
 	if (pass_unmapped_buffer) {
 		SAFE_MUNMAP(cleanup, protected_buffer,
testcases/kernel/syscalls/sendfile/sendfile05.c +2 -3
index 110194b84..1c700a61f 100644
@@ -52,6 +52,7 @@
 #include <netinet/in.h>
 #include <arpa/inet.h>
 #include "test.h"
+#include "safe_macros.h"
 
 #ifndef OFF_T
 #define OFF_T off_t
@@ -88,9 +89,7 @@ void do_sendfile(void)
 	if ((in_fd = open(in_file, O_RDONLY)) < 0) {
 		tst_brkm(TBROK, cleanup, "open failed: %d", errno);
 	}
-	if (stat(in_file, &sb) < 0) {
-		tst_brkm(TBROK, cleanup, "stat failed: %d", errno);
-	}
+	SAFE_STAT(cleanup, in_file, &sb);
 
 	offset = -1;
 	TEST(sendfile(out_fd, in_fd, &offset, sb.st_size));
Signature · 1 line
2.13.5
#30 lore raw

PATCH 31/40 Make use of SAFE_FSTAT()

In reply to Cyril Hrubis
---
 testcases/kernel/syscalls/dup2/dup202.c | 9 +++------
 testcases/kernel/syscalls/dup2/dup204.c | 8 ++------
 2 files changed, 5 insertions(+), 12 deletions(-)
testcases/kernel/syscalls/dup2/dup202.c +3 -6
index daff81fc4..15443ef3d 100644
@@ -53,6 +53,7 @@
 #include <fcntl.h>
 #include <stdio.h>
 #include "test.h"
+#include "safe_macros.h"
 
 char *TCID = "dup202";
 int TST_TOTAL = 3;
@@ -113,14 +114,10 @@ int main(int ac, char **av)
 			}
 
 			/* stat the original file */
-			if (fstat(ofd, &oldbuf) == -1)
-				tst_brkm(TBROK | TERRNO, cleanup,
-					 "fstat #1 failed");
+			SAFE_FSTAT(cleanup, ofd, &oldbuf);
 
 			/* stat the duped file */
-			if (fstat(*TC[i].nfd, &newbuf) == -1)
-				tst_brkm(TBROK | TERRNO, cleanup,
-					 "fstat #2 failed");
+			SAFE_FSTAT(cleanup, *TC[i].nfd, &newbuf);
 
 			if (oldbuf.st_mode != newbuf.st_mode)
 				tst_resm(TFAIL, "original and dup "
testcases/kernel/syscalls/dup2/dup204.c +2 -6
index 634bde8f2..c864f9866 100644
@@ -84,12 +84,8 @@ int main(int ac, char **av)
 				continue;
 			}
 
-			if (fstat(fd[i], &oldbuf) == -1)
-				tst_brkm(TBROK, cleanup, "fstat() #1 "
-					 "failed");
-			if (fstat(nfd[i], &newbuf) == -1)
-				tst_brkm(TBROK, cleanup, "fstat() #2 "
-					 "failed");
+			SAFE_FSTAT(cleanup, fd[i], &oldbuf);
+			SAFE_FSTAT(cleanup, nfd[i], &newbuf);
 
 			if (oldbuf.st_ino != newbuf.st_ino)
 				tst_resm(TFAIL, "original and duped "
Signature · 1 line
2.13.5
#31 lore raw

PATCH 32/40 Make use of SAFE_LSEEK()

In reply to Cyril Hrubis
---
 testcases/kernel/syscalls/fcntl/fcntl14.c   | 4 ++--
 testcases/kernel/syscalls/munmap/munmap01.c | 6 ++----
 testcases/kernel/syscalls/munmap/munmap02.c | 5 +----
 testcases/kernel/syscalls/readv/readv02.c   | 4 +---
 4 files changed, 6 insertions(+), 13 deletions(-)
testcases/kernel/syscalls/fcntl/fcntl14.c +2 -2
index 179908433..5f99dc21e 100644
@@ -48,6 +48,7 @@
 #include <sys/wait.h>
 #include <inttypes.h>
 #include "test.h"
+#include "safe_macros.h"
 
 #define SKIP 0x0c00
 #if SKIP == F_RDLCK || SKIP== F_WRLCK
@@ -786,8 +787,7 @@ void run_test(int file_flag, int file_mode, int seek, int start, int end)
 			tst_brkm(TBROK, cleanup, "write() failed");
 
 		if (seek) {
-			if (lseek(fd, seek, 0) < 0)
-				tst_brkm(TBROK, cleanup, "lseek() failed");
+			SAFE_LSEEK(cleanup, fd, seek, 0);
 		}
 
 		thiscase = &testcases[test];
testcases/kernel/syscalls/munmap/munmap01.c +2 -4
index 62dbaffbc..2a5cfc90d 100644
@@ -75,6 +75,7 @@
 #include <sys/mman.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 #define TEMPFILE	"mmapfile"
 
@@ -181,10 +182,7 @@ void setup(void)
 	 * move the file pointer to maplength position from the beginning
 	 * of the file.
 	 */
-	if (lseek(fildes, map_len, SEEK_SET) == -1) {
-		tst_brkm(TBROK, cleanup, "lseek() fails on %s, errno=%d : %s",
-			 TEMPFILE, errno, strerror(errno));
-	}
+	SAFE_LSEEK(cleanup, fildes, map_len, SEEK_SET);
 
 	/* Write one byte into temporary file */
 	if (write(fildes, "a", 1) != 1) {
testcases/kernel/syscalls/munmap/munmap02.c +1 -4
index a85c079c3..b43250512 100644
@@ -186,10 +186,7 @@ void setup(void)
 	 * move the file pointer to maplength position from the beginning
 	 * of the file.
 	 */
-	if (lseek(fildes, map_len, SEEK_SET) == -1) {
-		tst_brkm(TBROK, cleanup, "lseek() fails on %s, errno=%d : %s",
-			 TEMPFILE, errno, strerror(errno));
-	}
+	SAFE_LSEEK(cleanup, fildes, map_len, SEEK_SET);
 
 	/* Write one byte into temporary file */
 	if (write(fildes, "a", 1) != 1) {
testcases/kernel/syscalls/readv/readv02.c +1 -3
index be67c4ece..aa40e2c7d 100644
@@ -279,8 +279,6 @@ int fill_mem(char *c_ptr, int c1, int c2)
 
 long l_seek(int fdesc, long offset, int whence)
 {
-	if (lseek(fdesc, offset, whence) < 0) {
-		tst_brkm(TBROK, cleanup, "lseek Failed : errno = %d", errno);
-	}
+	SAFE_LSEEK(cleanup, fdesc, offset, whence);
 	return 0;
 }
Signature · 1 line
2.13.5
#32 lore raw

PATCH 33/40 Make use of SAFE_GETRLIMIT()

In reply to Cyril Hrubis
---
 testcases/kernel/syscalls/brk/brk01.c             | 5 ++---
 testcases/kernel/syscalls/mincore/mincore01.c     | 5 +----
 testcases/kernel/syscalls/msync/msync03.c         | 3 +--
 testcases/kernel/syscalls/setrlimit/setrlimit01.c | 3 +--
 4 files changed, 5 insertions(+), 11 deletions(-)
testcases/kernel/syscalls/brk/brk01.c +2 -3
index 775bc15fe..9a364318c 100644
@@ -43,6 +43,7 @@
 #include <inttypes.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 #ifndef BSIZE
 #define BSIZE  BBSIZE
@@ -159,9 +160,7 @@ void setup(void)
 	/*if ((ulim_sz=ulimit(3,0)) == -1)
 	   tst_brkm(TBROK|TERRNO, cleanup, "ulimit(3,0) failed"); */
 
-	if (getrlimit(RLIMIT_DATA, &lim) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup,
-			 "getrlimit(RLIMIT_DATA,%p) failed", &lim);
+	SAFE_GETRLIMIT(cleanup, RLIMIT_DATA, &lim);
 	ulim_sz = lim.rlim_cur;
 
 	/*
testcases/kernel/syscalls/mincore/mincore01.c +1 -4
index fa426d163..1632c72ab 100644
@@ -140,10 +140,7 @@ static void setup4(struct test_case_t *tc)
 {
 	struct rlimit as_lim;
 
-	if (getrlimit(RLIMIT_AS, &as_lim) == -1) {
-		tst_brkm(TBROK | TERRNO, cleanup,
-			 "getrlimit(RLIMIT_AS) failed");
-	}
+	SAFE_GETRLIMIT(cleanup, RLIMIT_AS, &as_lim);
 
 	tc->addr = global_pointer;
 	tc->len = as_lim.rlim_cur - (rlim_t)global_pointer + pagesize;
testcases/kernel/syscalls/msync/msync03.c +1 -2
index 8bd35be0d..2e072ca92 100644
@@ -132,8 +132,7 @@ static void setup(void)
 	addr2 = addr1 + 1;
 
 	/* addr3 is outside the address space of the process */
-	if (getrlimit(RLIMIT_DATA, &rl) < 0)
-		tst_brkm(TBROK | TERRNO, NULL, "getrlimit failed");
+	SAFE_GETRLIMIT(NULL, RLIMIT_DATA, &rl);
 	addr3 = (char *)rl.rlim_max;
 
 #if !defined(UCLINUX)
testcases/kernel/syscalls/setrlimit/setrlimit01.c +1 -2
index 05b4cf948..f66f99553 100644
@@ -194,8 +194,7 @@ static void test2(void)
  */
 static void test3(void)
 {
-	if (getrlimit(RLIMIT_NPROC, &save_rlim) < 0)
-		tst_brkm(TBROK, cleanup, "getrlimit failed, errno: %d", errno);
+	SAFE_GETRLIMIT(cleanup, RLIMIT_NPROC, &save_rlim);
 
 	rlim.rlim_cur = 10;
 	rlim.rlim_max = 10;
Signature · 1 line
2.13.5
#33 lore raw

PATCH 34/40 Make use of SAFE_SETRLIMIT()

In reply to Cyril Hrubis
---
 testcases/kernel/syscalls/llseek/llseek01.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
testcases/kernel/syscalls/llseek/llseek01.c +1 -3
index 6c98a4441..9ae821899 100644
@@ -191,9 +191,7 @@ void setup(void)
 	/* Set limit low, argument is # bytes */
 	rlp.rlim_cur = rlp.rlim_max = 2 * BUFSIZ;
 
-	if (setrlimit(RLIMIT_FSIZE, &rlp) == -1)
-		tst_brkm(TBROK, cleanup,
-			 "Cannot set max. file size using setrlimit");
+	SAFE_SETRLIMIT(cleanup, RLIMIT_FSIZE, &rlp);
 
 	/* Creat/open a temporary file under above directory */
 	if ((fildes = open(TEMP_FILE, O_RDWR | O_CREAT, FILE_MODE)) == -1) {
Signature · 1 line
2.13.5
#34 lore raw

PATCH 35/40 Make use of SAFE_BIND()

In reply to Cyril Hrubis
---
 testcases/kernel/syscalls/accept/accept01.c           | 5 +----
 testcases/kernel/syscalls/getpeername/getpeername01.c | 7 ++-----
 testcases/kernel/syscalls/getsockname/getsockname01.c | 6 ++----
 testcases/kernel/syscalls/getsockopt/getsockopt01.c   | 6 ++----
 testcases/kernel/syscalls/setsockopt/setsockopt01.c   | 6 ++----
 testcases/kernel/syscalls/sockioctl/sockioctl01.c     | 5 +----
 6 files changed, 10 insertions(+), 25 deletions(-)
testcases/kernel/syscalls/accept/accept01.c +1 -4
index 986415897..5a4966a0c 100644
@@ -161,10 +161,7 @@ static void setup1(void)
 		tst_brkm(TBROK, cleanup, "socket setup failed for accept "
 			 "test %d: %s", testno, strerror(errno));
 	}
-	if (bind(s, (struct sockaddr *)&sin0, sizeof(sin0)) < 0) {
-		tst_brkm(TBROK, cleanup, "socket bind failed for accept "
-			 "test %d: %s", testno, strerror(errno));
-	}
+	SAFE_BIND(cleanup, s, (struct sockaddr *)&sin0, sizeof(sin0));
 	sinlen = sizeof(fsin1);
 }
 
testcases/kernel/syscalls/getpeername/getpeername01.c +2 -5
index fd5d58d57..817cd38ad 100644
@@ -153,11 +153,8 @@ static void setup3(int i)
 		tst_brkm(TBROK | TERRNO, cleanup,
 			 "socket setup failed for getpeername test %d", i);
 	}
-	if (bind(test_cases[i].sockfd, (struct sockaddr *)&server_addr,
-		 sizeof(server_addr)) < 0) {
-		tst_brkm(TBROK | TERRNO, cleanup,
-			 "socket bind failed for getpeername test %d", i);
-	}
+	SAFE_BIND(cleanup, test_cases[i].sockfd,
+		  (struct sockaddr *)&server_addr, sizeof(server_addr));
 }
 
 static void setup4(int i)
testcases/kernel/syscalls/getsockname/getsockname01.c +2 -4
index c4b6e9b1a..c46b1763a 100644
@@ -52,6 +52,7 @@
 #include <netinet/in.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 char *TCID = "getsockname01";
 int testno;
@@ -172,10 +173,7 @@ void setup1(void)
 		tst_brkm(TBROK, cleanup, "socket setup failed for getsockname "
 			 "test %d: %s", testno, strerror(errno));
 	}
-	if (bind(s, (struct sockaddr *)&sin0, sizeof(sin0)) < 0) {
-		tst_brkm(TBROK, cleanup, "socket bind failed for getsockname "
-			 "test %d: %s", testno, strerror(errno));
-	}
+	SAFE_BIND(cleanup, s, (struct sockaddr *)&sin0, sizeof(sin0));
 	sinlen = sizeof(fsin1);
 }
 
testcases/kernel/syscalls/getsockopt/getsockopt01.c +2 -4
index 1195306a0..b39cfefb5 100644
@@ -52,6 +52,7 @@
 #include <netinet/in.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 char *TCID = "getsockopt01";
 int testno;
@@ -198,10 +199,7 @@ void setup1(void)
 		tst_brkm(TBROK, cleanup, "socket setup failed for getsockopt: "
 			 "%s", strerror(errno));
 	}
-	if (bind(s, (struct sockaddr *)&sin0, sizeof(sin0)) < 0) {
-		tst_brkm(TBROK, cleanup, "socket bind failed for getsockopt: "
-			 "%s", strerror(errno));
-	}
+	SAFE_BIND(cleanup, s, (struct sockaddr *)&sin0, sizeof(sin0));
 	sinlen = sizeof(fsin1);
 	optlen = sizeof(optval);
 }
testcases/kernel/syscalls/setsockopt/setsockopt01.c +2 -4
index f25694a96..36988854b 100644
@@ -54,6 +54,7 @@
 #include <netinet/in.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 char *TCID = "setsockopt01";
 int testno;
@@ -205,10 +206,7 @@ void setup1(void)
 		tst_brkm(TBROK, cleanup, "socket setup failed for setsockopt:"
 			 " %s", strerror(errno));
 	}
-	if (bind(s, (struct sockaddr *)&sin0, sizeof(sin0)) < 0) {
-		tst_brkm(TBROK, cleanup, "socket bind failed for setsockopt:"
-			 " %s", strerror(errno));
-	}
+	SAFE_BIND(cleanup, s, (struct sockaddr *)&sin0, sizeof(sin0));
 }
 
 void cleanup1(void)
testcases/kernel/syscalls/sockioctl/sockioctl01.c +1 -4
index ce1831cc8..79c53e042 100644
@@ -210,10 +210,7 @@ static void setup1(void)
 		tst_brkm(TBROK, cleanup, "socket setup failed: %s",
 			 strerror(errno));
 	}
-	if (bind(s, (struct sockaddr *)&sin0, sizeof(sin0)) < 0) {
-		tst_brkm(TBROK, cleanup, "socket bind failed for: %s",
-			 strerror(errno));
-	}
+	SAFE_BIND(cleanup, s, (struct sockaddr *)&sin0, sizeof(sin0));
 	sinlen = sizeof(fsin1);
 
 	if (strncmp(tdat[testno].desc, "ATMARK on UDP", 14) == 0) {
Signature · 1 line
2.13.5
#35 lore raw

PATCH 36/40 Make use of SAFE_CONNECT()

In reply to Cyril Hrubis
---
 testcases/kernel/syscalls/connect/connect01.c   |  5 ++---
 testcases/kernel/syscalls/recvmsg/recvmsg01.c   | 13 +++++--------
 testcases/kernel/syscalls/send/send01.c         |  4 ++--
 testcases/kernel/syscalls/sendfile/sendfile02.c |  5 +----
 testcases/kernel/syscalls/sendfile/sendfile04.c |  5 +----
 testcases/kernel/syscalls/sendfile/sendfile05.c |  5 +----
 testcases/kernel/syscalls/sendfile/sendfile06.c |  5 +----
 testcases/kernel/syscalls/sendmsg/sendmsg01.c   |  4 ++--
 testcases/kernel/syscalls/sendto/sendto01.c     |  4 ++--
 9 files changed, 17 insertions(+), 33 deletions(-)
testcases/kernel/syscalls/connect/connect01.c +2 -3
index af6035435..c0f711b09 100644
@@ -53,6 +53,7 @@
 #include <netinet/in.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 char *TCID = "connect01";
 int testno;
@@ -216,9 +217,7 @@ void cleanup1(void)
 void setup2(void)
 {
 	setup1();		/* get a socket in s */
-	if (connect(s, (const struct sockaddr *)&sin1, sizeof(sin1)) < 0)
-		tst_brkm(TBROK | TERRNO, cleanup,
-			 "socket setup failed connect test %d", testno);
+	SAFE_CONNECT(cleanup, s, (const struct sockaddr *)&sin1, sizeof(sin1));
 }
 
 pid_t start_server(struct sockaddr_in *sin0)
testcases/kernel/syscalls/recvmsg/recvmsg01.c +5 -8
index 13bd58f4b..4e19157e7 100644
@@ -56,6 +56,7 @@
 #include <netinet/in.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 char *TCID = "recvmsg01";
 int testno;
@@ -299,10 +300,8 @@ void setup1(void)
 		tst_brkm(TBROK | TERRNO, cleanup, "socket setup failed");
 	if (tdat[testno].type == SOCK_STREAM) {
 		if (tdat[testno].domain == PF_INET) {
-			if (connect(s, (struct sockaddr *)&sin1, sizeof(sin1)) <
-			    0)
-				tst_brkm(TBROK | TERRNO, cleanup,
-					 "connect failed");
+			SAFE_CONNECT(cleanup, s, (struct sockaddr *)&sin1,
+				     sizeof(sin1));
 			/* Wait for something to be readable, else we won't detect EFAULT on recv */
 			FD_ZERO(&rdfds);
 			FD_SET(s, &rdfds);
@@ -313,10 +312,8 @@ void setup1(void)
 				tst_brkm(TBROK, cleanup,
 					 "client setup1 failed - no message ready in 2 sec");
 		} else if (tdat[testno].domain == PF_UNIX) {
-			if (connect(s, (struct sockaddr *)&sun1, sizeof(sun1)) <
-			    0)
-				tst_brkm(TBROK | TERRNO, cleanup,
-					 "UD connect failed");
+			SAFE_CONNECT(cleanup, s, (struct sockaddr *)&sun1,
+				     sizeof(sun1));
 		}
 	}
 }
testcases/kernel/syscalls/send/send01.c +2 -2
index 6f71300ce..80a20098a 100644
@@ -42,6 +42,7 @@
 #include <netinet/in.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 char *TCID = "send01";
 int testno;
@@ -328,8 +329,7 @@ static void setup1(void)
 	s = socket(tdat[testno].domain, tdat[testno].type, tdat[testno].proto);
 	if (s < 0)
 		tst_brkm(TBROK | TERRNO, cleanup, "socket setup failed");
-	if (connect(s, (const struct sockaddr *)&sin1, sizeof(sin1)) < 0)
-		tst_brkm(TBROK | TERRNO, cleanup, "connect failed");
+	SAFE_CONNECT(cleanup, s, (const struct sockaddr *)&sin1, sizeof(sin1));
 }
 
 static void cleanup1(void)
testcases/kernel/syscalls/sendfile/sendfile02.c +1 -4
index 111b2741a..c2a7e7d0f 100644
@@ -256,10 +256,7 @@ int create_server(void)
 			 strerror(errno));
 		return -1;
 	}
-	if (connect(s, (struct sockaddr *)&sin1, sizeof(sin1)) < 0) {
-		tst_brkm(TBROK, cleanup, "call to connect() failed: %s",
-			 strerror(errno));
-	}
+	SAFE_CONNECT(cleanup, s, (struct sockaddr *)&sin1, sizeof(sin1));
 	return s;
 
 }
testcases/kernel/syscalls/sendfile/sendfile04.c +1 -4
index 13389150c..09869c2c6 100644
@@ -253,10 +253,7 @@ int create_server(void)
 			 strerror(errno));
 		return -1;
 	}
-	if (connect(s, (struct sockaddr *)&sin1, sizeof(sin1)) < 0) {
-		tst_brkm(TBROK, cleanup, "call to connect() failed: %s",
-			 strerror(errno));
-	}
+	SAFE_CONNECT(cleanup, s, (struct sockaddr *)&sin1, sizeof(sin1));
 	return s;
 
 }
testcases/kernel/syscalls/sendfile/sendfile05.c +1 -4
index 1c700a61f..85e296744 100644
@@ -218,10 +218,7 @@ int create_server(void)
 			 strerror(errno));
 		return -1;
 	}
-	if (connect(s, (struct sockaddr *)&sin1, sizeof(sin1)) < 0) {
-		tst_brkm(TBROK, cleanup, "call to connect() failed: %s",
-			 strerror(errno));
-	}
+	SAFE_CONNECT(cleanup, s, (struct sockaddr *)&sin1, sizeof(sin1));
 	return s;
 
 }
testcases/kernel/syscalls/sendfile/sendfile06.c +1 -4
index 794519549..2d8d6d769 100644
@@ -200,10 +200,7 @@ static int create_server(void)
 		return -1;
 	}
 
-	if (connect(s, (struct sockaddr *)&sin1, sizeof(sin1)) < 0) {
-		tst_brkm(TBROK, cleanup, "call to connect() failed: %s",
-			 strerror(errno));
-	}
+	SAFE_CONNECT(cleanup, s, (struct sockaddr *)&sin1, sizeof(sin1));
 
 	return s;
 }
testcases/kernel/syscalls/sendmsg/sendmsg01.c +2 -2
index dcf3ab7e8..b79c6f4ac 100644
@@ -48,6 +48,7 @@
 #include <netinet/in.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 char *TCID = "sendmsg01";
 int testno;
@@ -665,8 +666,7 @@ static void setup5(void)
 			 strerror(errno));
 	}
 
-	if (connect(s, (struct sockaddr *)&sin1, sizeof(sin1)) < 0)
-		tst_brkm(TBROK, cleanup, "connect failed: %s", strerror(errno));
+	SAFE_CONNECT(cleanup, s, (struct sockaddr *)&sin1, sizeof(sin1));
 
 	/* slight change destination (port) so connect() is to different
 	 * 5-tuple than already connected
testcases/kernel/syscalls/sendto/sendto01.c +2 -2
index ec0766197..813c3d7f8 100644
@@ -41,6 +41,7 @@
 #include <netinet/in.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 char *TCID = "sendto01";
 int testno;
@@ -398,8 +399,7 @@ static void setup1(void)
 	s = socket(tdat[testno].domain, tdat[testno].type, tdat[testno].proto);
 	if (s < 0)
 		tst_brkm(TBROK | TERRNO, cleanup, "socket setup failed");
-	if (connect(s, (const struct sockaddr *)&sin1, sizeof(sin1)) < 0)
-		tst_brkm(TBROK | TERRNO, cleanup, "connect failed");
+	SAFE_CONNECT(cleanup, s, (const struct sockaddr *)&sin1, sizeof(sin1));
 }
 
 static void cleanup1(void)
Signature · 1 line
2.13.5
#36 lore raw

PATCH 37/40 Make use of SAFE_GETSOCKNAME()

In reply to Cyril Hrubis
---
 testcases/kernel/syscalls/connect/connect01.c   | 3 +--
 testcases/kernel/syscalls/recv/recv01.c         | 4 ++--
 testcases/kernel/syscalls/recvfrom/recvfrom01.c | 4 ++--
 testcases/kernel/syscalls/recvmsg/recvmsg01.c   | 3 +--
 testcases/kernel/syscalls/send/send01.c         | 3 +--
 testcases/kernel/syscalls/sendfile/sendfile02.c | 3 +--
 testcases/kernel/syscalls/sendfile/sendfile04.c | 3 +--
 testcases/kernel/syscalls/sendfile/sendfile05.c | 3 +--
 testcases/kernel/syscalls/sendfile/sendfile06.c | 3 +--
 testcases/kernel/syscalls/sendmsg/sendmsg01.c   | 3 +--
 testcases/kernel/syscalls/sendto/sendto01.c     | 3 +--
 11 files changed, 13 insertions(+), 22 deletions(-)
testcases/kernel/syscalls/connect/connect01.c +1 -2
index c0f711b09..d65af4ae0 100644
@@ -242,8 +242,7 @@ pid_t start_server(struct sockaddr_in *sin0)
 		tst_brkm(TBROK | TERRNO, cleanup, "server listen failed");
 		return -1;
 	}
-	if (getsockname(sfd, (struct sockaddr *)sin0, &slen) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "getsockname failed");
+	SAFE_GETSOCKNAME(cleanup, sfd, (struct sockaddr *)sin0, &slen);
 
 	switch ((pid = FORK_OR_VFORK())) {
 	case 0:		/* child */
testcases/kernel/syscalls/recv/recv01.c +2 -2
index 9c21dc206..3345e51e6 100644
@@ -53,6 +53,7 @@
 #include <netinet/in.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 char *TCID = "recv01";
 int testno;
@@ -238,8 +239,7 @@ pid_t start_server(struct sockaddr_in *sin0)
 		tst_brkm(TBROK | TERRNO, cleanup, "server listen failed");
 		return -1;
 	}
-	if (getsockname(sfd, (struct sockaddr *)sin0, &slen) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "getsockname failed");
+	SAFE_GETSOCKNAME(cleanup, sfd, (struct sockaddr *)sin0, &slen);
 
 	switch ((pid = FORK_OR_VFORK())) {
 	case 0:		/* child */
testcases/kernel/syscalls/recvfrom/recvfrom01.c +2 -2
index 3bcf2a1fc..e07879b06 100644
@@ -53,6 +53,7 @@
 #include <netinet/in.h>
 
 #include "test.h"
+#include "safe_macros.h"
 
 char *TCID = "recvfrom01";
 int testno;
@@ -267,8 +268,7 @@ pid_t start_server(struct sockaddr_in *sin0)
 		tst_brkm(TBROK | TERRNO, cleanup, "server listen failed");
 		return -1;
 	}
-	if (getsockname(sfd, (struct sockaddr *)sin0, &slen) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "getsockname failed");
+	SAFE_GETSOCKNAME(cleanup, sfd, (struct sockaddr *)sin0, &slen);
 
 	switch ((pid = FORK_OR_VFORK())) {
 	case 0:		/* child */
testcases/kernel/syscalls/recvmsg/recvmsg01.c +1 -2
index 4e19157e7..5bdc60cd9 100644
@@ -384,8 +384,7 @@ pid_t start_server(struct sockaddr_in *ssin, struct sockaddr_un *ssun)
 		tst_brkm(TBROK | TERRNO, cleanup, "server listen failed");
 		return -1;
 	}
-	if (getsockname(sfd, (struct sockaddr *)ssin, &slen) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "getsockname failed");
+	SAFE_GETSOCKNAME(cleanup, sfd, (struct sockaddr *)ssin, &slen);
 
 	/* set up UNIX-domain socket */
 	ufd = socket(PF_UNIX, SOCK_STREAM, 0);
testcases/kernel/syscalls/send/send01.c +1 -2
index 80a20098a..31e8c1758 100644
@@ -183,8 +183,7 @@ static pid_t start_server(struct sockaddr_in *sin0)
 		tst_brkm(TBROK | TERRNO, cleanup, "server listen failed");
 		return -1;
 	}
-	if (getsockname(sfd, (struct sockaddr *)sin0, &slen) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "getsockname failed");
+	SAFE_GETSOCKNAME(cleanup, sfd, (struct sockaddr *)sin0, &slen);
 
 	switch ((pid = FORK_OR_VFORK())) {
 	case 0:
testcases/kernel/syscalls/sendfile/sendfile02.c +1 -2
index c2a7e7d0f..e5f115146 100644
@@ -228,8 +228,7 @@ int create_server(void)
 			 strerror(errno));
 		return -1;
 	}
-	if (getsockname(sockfd, (struct sockaddr *)&sin1, &slen) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "getsockname failed");
+	SAFE_GETSOCKNAME(cleanup, sockfd, (struct sockaddr *)&sin1, &slen);
 
 	child_pid = FORK_OR_VFORK();
 	if (child_pid < 0) {
testcases/kernel/syscalls/sendfile/sendfile04.c +1 -2
index 09869c2c6..0f315abb0 100644
@@ -225,8 +225,7 @@ int create_server(void)
 			 strerror(errno));
 		return -1;
 	}
-	if (getsockname(sockfd, (struct sockaddr *)&sin1, &slen) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "getsockname failed");
+	SAFE_GETSOCKNAME(cleanup, sockfd, (struct sockaddr *)&sin1, &slen);
 
 	child_pid = FORK_OR_VFORK();
 	if (child_pid < 0) {
testcases/kernel/syscalls/sendfile/sendfile05.c +1 -2
index 85e296744..0f268ceb3 100644
@@ -190,8 +190,7 @@ int create_server(void)
 			 strerror(errno));
 		return -1;
 	}
-	if (getsockname(sockfd, (struct sockaddr *)&sin1, &slen) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "getsockname failed");
+	SAFE_GETSOCKNAME(cleanup, sockfd, (struct sockaddr *)&sin1, &slen);
 
 	child_pid = FORK_OR_VFORK();
 	if (child_pid < 0) {
testcases/kernel/syscalls/sendfile/sendfile06.c +1 -2
index 2d8d6d769..abb67604f 100644
@@ -169,8 +169,7 @@ static int create_server(void)
 			 strerror(errno));
 		return -1;
 	}
-	if (getsockname(sockfd, (struct sockaddr *)&sin1, &slen) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "getsockname failed");
+	SAFE_GETSOCKNAME(cleanup, sockfd, (struct sockaddr *)&sin1, &slen);
 
 	child_pid = FORK_OR_VFORK();
 	if (child_pid < 0) {
testcases/kernel/syscalls/sendmsg/sendmsg01.c +1 -2
index b79c6f4ac..d7f7b757c 100644
@@ -442,8 +442,7 @@ static pid_t start_server(struct sockaddr_in *sin0, struct sockaddr_un *sun0)
 			 strerror(errno));
 		return -1;
 	}
-	if (getsockname(sfd, (struct sockaddr *)sin0, &slen) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "getsockname failed");
+	SAFE_GETSOCKNAME(cleanup, sfd, (struct sockaddr *)sin0, &slen);
 
 	/* set up UNIX-domain socket */
 	ufd = socket(PF_UNIX, SOCK_DGRAM, 0);
testcases/kernel/syscalls/sendto/sendto01.c +1 -2
index 813c3d7f8..5304da691 100644
@@ -253,8 +253,7 @@ static pid_t start_server(struct sockaddr_in *sin0)
 		tst_brkm(TBROK | TERRNO, cleanup, "server listen failed");
 		return -1;
 	}
-	if (getsockname(sfd, (struct sockaddr *)sin0, &slen) == -1)
-		tst_brkm(TBROK | TERRNO, cleanup, "getsockname failed");
+	SAFE_GETSOCKNAME(cleanup, sfd, (struct sockaddr *)sin0, &slen);
 
 	switch ((pid = FORK_OR_VFORK())) {
 	case 0:
Signature · 1 line
2.13.5
#37 lore raw

PATCH 38/40 syscalls/open03: Make use of SAFE_CLOSE()

In reply to Cyril Hrubis

Coccinelle haven't caught this one because of the else branch.

---
 testcases/kernel/syscalls/open/open03.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
testcases/kernel/syscalls/open/open03.c +2 -4
index 80e43c6e9..aa15ee9e9 100644
@@ -64,10 +64,8 @@ int main(int ac, char **av)
 				 "open(%s, O_RDWR|O_CREAT,0700) returned %ld",
 				 fname, TEST_RETURN);
 
-			if (close(fd) == -1)
-				tst_brkm(TBROK | TERRNO, cleanup,
-					 "close(%s) failed", fname);
-			else SAFE_UNLINK(cleanup, fname);
+			SAFE_CLOSE(cleanup, fd);
+			SAFE_UNLINK(cleanup, fname);
 		}
 	}
 
Signature · 1 line
2.13.5
#38 lore raw

PATCH 39/40 syscalls/open08: Make use of SAFE_SETGID()

In reply to Cyril Hrubis

Coccinelle haven't caught this one because of the else branch.

---
 testcases/kernel/syscalls/open/open08.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
testcases/kernel/syscalls/open/open08.c +2 -4
index 742771eef..6d1f37de0 100644
@@ -141,10 +141,8 @@ static void setup(void)
 
 	/* Switch to nobody user for correct error code collection */
 	ltpuser = getpwnam(nobody_uid);
-	if (setgid(ltpuser->pw_gid) == -1) {
-		tst_brkm(TBROK | TERRNO, NULL, "setgid(%d) failed",
-			 ltpuser->pw_gid);
-	} else SAFE_SETUID(NULL, ltpuser->pw_uid);
+	SAFE_SETGID(NULL, ltpuser->pw_gid);
+	SAFE_SETUID(NULL, ltpuser->pw_uid);
 
 	tst_tmpdir();
 
Signature · 1 line
2.13.5
#39 lore raw

PATCH 40/40 syscalls/flock04: Use curly brances in the else block

In reply to Cyril Hrubis

The code generated by Coccinelle was correct but a bit confusingly formatted.

---
 testcases/kernel/syscalls/flock/flock04.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
testcases/kernel/syscalls/flock/flock04.c +3 -1
index 87f8fc0ad..8a282b41d 100644
@@ -118,7 +118,9 @@ int main(int argc, char **argv)
 						 "flock() PASSED in acquiring shared lock on "
 						 "Share Locked file");
 				exit(0);
-			} else SAFE_WAIT(cleanup, &status);
+			} else {
+				SAFE_WAIT(cleanup, &status);
+			}
 
 			pid = FORK_OR_VFORK();
 			if (pid == -1)
Signature · 1 line
2.13.5