[LTP] [PATCH v3 5/7] syscalls/pidfd_open01, 04: Make use of SAFE_FCNTL

Yang Xu xuyang2018.jy@fujitsu.com
Tue Feb 22 09:41:10 CET 2022


Signed-off-by: Yang Xu <xuyang2018.jy@fujitsu.com>
---
 .../kernel/syscalls/pidfd_open/pidfd_open01.c   | 17 ++++++++++++-----
 .../kernel/syscalls/pidfd_open/pidfd_open04.c   | 15 +++++++++++----
 2 files changed, 23 insertions(+), 9 deletions(-)

diff --git a/testcases/kernel/syscalls/pidfd_open/pidfd_open01.c b/testcases/kernel/syscalls/pidfd_open/pidfd_open01.c
index 6f1e95ba8..ce07e6717 100644
--- a/testcases/kernel/syscalls/pidfd_open/pidfd_open01.c
+++ b/testcases/kernel/syscalls/pidfd_open/pidfd_open01.c
@@ -16,18 +16,18 @@
 #include "tst_test.h"
 #include "lapi/pidfd.h"
 
+static int pidfd = -1;
+
 static void run(void)
 {
 	int flag;
 
 	TST_EXP_FD_SILENT(pidfd_open(getpid(), 0), "pidfd_open(getpid(), 0)");
 
-	flag = fcntl(TST_RET, F_GETFD);
-
-	SAFE_CLOSE(TST_RET);
+	pidfd = TST_RET;
+	flag = SAFE_FCNTL(pidfd, F_GETFD);
 
-	if (flag == -1)
-		tst_brk(TFAIL | TERRNO, "fcntl(F_GETFD) failed");
+	SAFE_CLOSE(pidfd);
 
 	if (!(flag & FD_CLOEXEC))
 		tst_brk(TFAIL, "pidfd_open(getpid(), 0) didn't set close-on-exec flag");
@@ -35,7 +35,14 @@ static void run(void)
 	tst_res(TPASS, "pidfd_open(getpid(), 0) passed");
 }
 
+static void cleanup(void)
+{
+	if (pidfd > -1)
+		SAFE_CLOSE(pidfd);
+}
+
 static struct tst_test test = {
 	.setup = pidfd_open_supported,
+	.cleanup = cleanup,
 	.test_all = run,
 };
diff --git a/testcases/kernel/syscalls/pidfd_open/pidfd_open04.c b/testcases/kernel/syscalls/pidfd_open/pidfd_open04.c
index 60080c0c8..0e8ab6956 100644
--- a/testcases/kernel/syscalls/pidfd_open/pidfd_open04.c
+++ b/testcases/kernel/syscalls/pidfd_open/pidfd_open04.c
@@ -21,9 +21,11 @@
 #define P_PIDFD  3
 #endif
 
+static int pidfd = -1;
+
 static void run(void)
 {
-	int flag, pid, pidfd, ret;
+	int flag, pid, ret;
 	siginfo_t info;
 
 	pid = SAFE_FORK();
@@ -36,9 +38,7 @@ static void run(void)
 				"pidfd_open(%d,  PIDFD_NONBLOCK)", pid);
 
 	pidfd = TST_RET;
-	flag = fcntl(pidfd, F_GETFL);
-	if (flag == -1)
-		tst_brk(TFAIL | TERRNO, "fcntl(F_GETFL) failed");
+	flag = SAFE_FCNTL(pidfd, F_GETFL);
 
 	if (!(flag & O_NONBLOCK))
 		tst_brk(TFAIL, "pidfd_open(%d, O_NONBLOCK) didn't set O_NONBLOCK flag", pid);
@@ -77,10 +77,17 @@ static void setup(void)
 	SAFE_CLOSE(TST_RET);
 }
 
+static void cleanup(void)
+{
+	if (pidfd > -1)
+		SAFE_CLOSE(pidfd);
+}
+
 static struct tst_test test = {
 	.needs_root = 1,
 	.forks_child = 1,
 	.needs_checkpoints = 1,
 	.setup = setup,
+	.cleanup = cleanup,
 	.test_all = run,
 };
-- 
2.31.1



More information about the ltp mailing list