diff mbox series

accept02: Add SAFE_FORK to clean CLOSE_WAIT fds

Message ID 20220323094926.65653-1-zhaogongyi@huawei.com
State Changes Requested
Headers show
Series accept02: Add SAFE_FORK to clean CLOSE_WAIT fds | expand

Commit Message

Zhao Gongyi March 23, 2022, 9:49 a.m. UTC
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(-)

--
2.17.1

Comments

Cyril Hrubis March 23, 2022, 3:06 p.m. UTC | #1
Hi!
As far as I can tell we should rather close the fd returned from the
accept() call like this:

diff --git a/testcases/kernel/syscalls/accept/accept02.c b/testcases/kernel/syscalls/accept/accept02.c
index 12a1e3ca3..b2d27a260 100644
--- a/testcases/kernel/syscalls/accept/accept02.c
+++ b/testcases/kernel/syscalls/accept/accept02.c
@@ -68,6 +68,8 @@ static void *server_thread(void *arg)
        TEST(setsockopt(clone_server_sockfd, SOL_IP, MCAST_LEAVE_GROUP,
                        mc_group, mc_group_len));

+       SAFE_CLOSE(clone_server_sockfd);
+
        if (TST_RET != -1)
                tst_res(TFAIL, "Multicast group was copied!");
        else if (TST_ERR == EADDRNOTAVAIL)
diff mbox series

Patch

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"},