git: 4c6ceca9cc44 - main - tests/fdgrowtable: perform the threaded test in a child process

From: Gleb Smirnoff <glebius_at_FreeBSD.org>
Date: Sat, 24 Feb 2024 01:54:07 UTC
The branch main has been updated by glebius:

URL: https://cgit.FreeBSD.org/src/commit/?id=4c6ceca9cc44fc9b02dc39e80713f2ad3ab8eeb6

commit 4c6ceca9cc44fc9b02dc39e80713f2ad3ab8eeb6
Author:     Gleb Smirnoff <glebius@FreeBSD.org>
AuthorDate: 2024-02-24 01:49:53 +0000
Commit:     Gleb Smirnoff <glebius@FreeBSD.org>
CommitDate: 2024-02-24 01:49:53 +0000

    tests/fdgrowtable: perform the threaded test in a child process
    
    The test needs to be performed in a new process that was forked with
    RFCFDG flag.  The will guarantee that the table will start to grow from 20
    file descriptors, no matter what kyua(1) or a bare shell was doing before
    executing this test.  This should fix  repetitive test runs from a shell
    as well as failures with kyua(1) in some environments.
---
 tests/sys/kern/fdgrowtable_test.c | 34 ++++++++++++++++++++++++++++------
 1 file changed, 28 insertions(+), 6 deletions(-)

diff --git a/tests/sys/kern/fdgrowtable_test.c b/tests/sys/kern/fdgrowtable_test.c
index 826599ecf836..ecab72ff09aa 100644
--- a/tests/sys/kern/fdgrowtable_test.c
+++ b/tests/sys/kern/fdgrowtable_test.c
@@ -167,21 +167,43 @@ ATF_TC_HEAD(oldtables_shared_via_threads, tc)
 
 ATF_TC_BODY(oldtables_shared_via_threads, tc)
 {
+	pid_t child;
 	kvm_t *kd;
 	struct kinfo_proc *kp;
 	pthread_t thread;
 
-	ATF_REQUIRE((kd = kvm_open(NULL, NULL, NULL, O_RDONLY, NULL)) != NULL);
-	ATF_REQUIRE(pthread_create(&thread, NULL, exec_thread, NULL) == 0);
+	if ((child = rfork(RFPROC | RFCFDG)) > 0) {
+		pid_t wpid;
+		int status;
+
+		wpid = waitpid(child, &status, 0);
+		ATF_REQUIRE(wpid == child);
+		ATF_REQUIRE(WIFEXITED(status));
+		ATF_REQUIRE(WEXITSTATUS(status) == EXIT_SUCCESS);
+		return;
+	}
+
+#define	REQUIRE(expression)	do {					\
+		if (!(expression))					\
+			exit(EXIT_FAILURE);				\
+	} while (0)
+
+	REQUIRE(child == 0);
+
+	REQUIRE((kd = kvm_open(NULL, NULL, NULL, O_RDONLY, NULL)) != NULL);
+	REQUIRE(pthread_create(&thread, NULL, exec_thread, NULL) == 0);
 
 	openfiles(128);
 
 	kp = read_kinfo(kd);
-	ATF_CHECK(kp->ki_numthreads > 1);
-	ATF_CHECK(old_tables(kd,kp) > 1);
+	REQUIRE(kp->ki_numthreads > 1);
+	REQUIRE(old_tables(kd,kp) > 1);
+
+	REQUIRE(pthread_cancel(thread) == 0);
+	REQUIRE(pthread_join(thread, NULL) == 0);
+#undef REQUIRE
 
-	ATF_REQUIRE(pthread_cancel(thread) == 0);
-	ATF_REQUIRE(pthread_join(thread, NULL) == 0);
+	exit(EXIT_SUCCESS);
 }
 
 /*