test-helpers: use sysctl() to count open fds on FreeBSD

This allows running the tests on FreeBSD without mounting fdescfs.
Previously you had to run `mount -t fdescfs -o linrdlnk null /dev/fd` to
get file descriptors >=3 listed in /dev/fd.

Signed-off-by: Alex Richardson <Alexander.Richardson@cl.cam.ac.uk>
This commit is contained in:
Alex Richardson 2021-06-07 11:53:46 +01:00 committed by Alexander Richardson
parent bb92828807
commit 42bf011f65

View File

@ -41,6 +41,27 @@
#include "test-runner.h" #include "test-runner.h"
#if defined(__FreeBSD__)
#include <sys/sysctl.h>
/*
* On FreeBSD, get file descriptor information using sysctl() since that does
* not depend on a mounted fdescfs (which provides /dev/fd/N for N > 2).
*/
int
count_open_fds(void)
{
int error;
int nfds;
int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_NFDS, 0 };
size_t len;
len = sizeof(nfds);
error = sysctl(mib, 4, &nfds, &len, NULL, 0);
assert(error == 0 && "sysctl KERN_PROC_NFDS failed.");
return nfds;
}
#else
int int
count_open_fds(void) count_open_fds(void)
{ {
@ -68,6 +89,7 @@ count_open_fds(void)
return count; return count;
} }
#endif
void void
exec_fd_leak_check(int nr_expected_fds) exec_fd_leak_check(int nr_expected_fds)