[LTP] [PATCH] accept02: Add SAFE_FORK to clean CLOSE_WAIT fds

Zhao Gongyi zhaogongyi@huawei.com
Wed Mar 23 10:49:26 CET 2022


If we run the test with option -i 1000, and the ulimit
of open files little than 1000, the test would fail and
report the error of EMFILE. We can see that the socket
fds are in the status of CLOSE_WAIT.

Signed-off-by: Zhao Gongyi <zhaogongyi@huawei.com>
---
 testcases/kernel/syscalls/accept/accept02.c | 25 ++++++++++++++-------
 1 file changed, 17 insertions(+), 8 deletions(-)

diff --git a/testcases/kernel/syscalls/accept/accept02.c b/testcases/kernel/syscalls/accept/accept02.c
index 12a1e3ca3..d46293386 100644
--- a/testcases/kernel/syscalls/accept/accept02.c
+++ b/testcases/kernel/syscalls/accept/accept02.c
@@ -23,6 +23,8 @@

 #include <errno.h>
 #include <sys/socket.h>
+#include <sys/wait.h>
+#include <stdlib.h>
 #include "tst_test.h"
 #include "tst_safe_net.h"
 #include "tst_safe_pthread.h"
@@ -92,17 +94,23 @@ static void *client_thread(void *arg)

 static void run(void)
 {
-	pthread_t server_thr, client_thr;
+	pid_t child = SAFE_FORK();
+	if (!child) {
+		pthread_t server_thr, client_thr;

-	server_addr->sin_port = server_port;
-	client_addr->sin_port = htons(0);
+		server_addr->sin_port = server_port;
+		client_addr->sin_port = htons(0);

-	SAFE_PTHREAD_CREATE(&server_thr, NULL, server_thread, NULL);
-	TST_CHECKPOINT_WAIT(0);
-	SAFE_PTHREAD_CREATE(&client_thr, NULL, client_thread, NULL);
+		SAFE_PTHREAD_CREATE(&server_thr, NULL, server_thread, NULL);
+		TST_CHECKPOINT_WAIT(0);
+		SAFE_PTHREAD_CREATE(&client_thr, NULL, client_thread, NULL);

-	SAFE_PTHREAD_JOIN(server_thr, NULL);
-	SAFE_PTHREAD_JOIN(client_thr, NULL);
+		SAFE_PTHREAD_JOIN(server_thr, NULL);
+		SAFE_PTHREAD_JOIN(client_thr, NULL);
+		exit(0);
+	}
+
+	SAFE_WAIT(NULL);
 }

 static void setup(void)
@@ -145,6 +153,7 @@ static struct tst_test test = {
 	.setup = setup,
 	.cleanup = cleanup,
 	.needs_checkpoints = 1,
+	.forks_child = 1,
 	.tags = (const struct tst_tag[]) {
 		{"CVE", "2017-8890"},
 		{"linux-git", "657831ff"},
--
2.17.1



More information about the ltp mailing list