diff options
| author | Mark Johnston <markj@FreeBSD.org> | 2026-05-22 14:56:47 +0000 |
|---|---|---|
| committer | Mark Johnston <markj@FreeBSD.org> | 2026-05-22 14:56:47 +0000 |
| commit | c9546bb61910d40f4cb0dfb9716ba6eba44d1a0d (patch) | |
| tree | 2ab2121ce98cc08171b985c97cb26e476ea2773c | |
| parent | bc041630fae2ea89bf041bf5d13df220aae4b2bf (diff) | |
tests/procdesc: Use a more efficient mechanism to block
Reviewed by: kib
MFC after: 2 weeks
Differential Revision: https://reviews.freebsd.org/D57149
| -rw-r--r-- | tests/sys/kern/Makefile | 2 | ||||
| -rw-r--r-- | tests/sys/kern/procdesc.c | 41 |
2 files changed, 38 insertions, 5 deletions
diff --git a/tests/sys/kern/Makefile b/tests/sys/kern/Makefile index d7a6af905b7e..dcaeb8d2f1fa 100644 --- a/tests/sys/kern/Makefile +++ b/tests/sys/kern/Makefile @@ -106,7 +106,7 @@ LIBADD.kcov+= pthread CFLAGS.ktls_test+= -DOPENSSL_API_COMPAT=0x10100000L LIBADD.ktls_test+= crypto util LIBADD.listener_wakeup+= pthread -LIBADD.procdesc+= pthread +LIBADD.procdesc+= kvm pthread LIBADD.shutdown_dgram+= pthread LIBADD.socket_msg_waitall+= pthread LIBADD.socket_splice+= pthread diff --git a/tests/sys/kern/procdesc.c b/tests/sys/kern/procdesc.c index 2d710ef057f2..2fe50be45bf7 100644 --- a/tests/sys/kern/procdesc.c +++ b/tests/sys/kern/procdesc.c @@ -33,16 +33,51 @@ #include <sys/sysctl.h> #include <sys/wait.h> +#include <fcntl.h> #include <poll.h> #include <pthread.h> #include <stdio.h> #include <unistd.h> #include <atf-c.h> +#include <kvm.h> /* Tests for procdesc(4) that aren't specific to any one syscall */ /* + * Block until a thread in the specified process is sleeping in the specified + * wait message. + */ +static void +wait_for_naptime(pid_t pid, const char *wmesg) +{ + kvm_t *kd; + int count; + + kd = kvm_openfiles(NULL, "/dev/null", NULL, O_RDONLY, NULL); + ATF_REQUIRE(kd != NULL); + for (;;) { + struct kinfo_proc *kip; + int i; + + usleep(1000); + kip = kvm_getprocs(kd, KERN_PROC_PID | KERN_PROC_INC_THREAD, + pid, &count); + ATF_REQUIRE(kip != NULL); + for (i = 0; i < count; i++) { + ATF_REQUIRE(kip[i].ki_stat != SZOMB); + if (kip[i].ki_stat == SSLEEP && + strcmp(kip[i].ki_wmesg, wmesg) == 0) + break; + } + if (i < count) + break; + } + + kvm_close(kd); +} + +/* * Even after waiting on a process descriptor with waitpid(2), the kernel will * not recycle the pid until after the process descriptor is closed. That is * important to prevent users from trying to wait() twice, the second time @@ -128,8 +163,7 @@ ATF_TC_BODY(poll_close_race, tc) error = pthread_create(&thr, NULL, poll_procdesc, &pd); ATF_REQUIRE_MSG(error == 0, "pthread_create: %s", strerror(error)); - /* Wait for the thread to block in poll(2). */ - usleep(250000); + wait_for_naptime(getpid(), "select"); ATF_REQUIRE_MSG(close(pd) == 0, "close: %s", strerror(errno)); @@ -159,8 +193,7 @@ ATF_TC_BODY(poll_exit_wakeup, tc) error = pthread_create(&thr, NULL, poll_procdesc, &pd); ATF_REQUIRE_MSG(error == 0, "pthread_create: %s", strerror(error)); - /* Wait for the thread to block in poll(2). */ - usleep(250000); + wait_for_naptime(getpid(), "select"); ATF_REQUIRE_MSG(pdkill(pd, SIGKILL) == 0, "pdkill: %s", strerror(errno)); |
