From 42bf011f65a1aa5b7dcb664026b02c48170d07a5 Mon Sep 17 00:00:00 2001 From: Alex Richardson Date: Mon, 7 Jun 2021 11:53:46 +0100 Subject: [PATCH] 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 --- tests/test-helpers.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/test-helpers.c b/tests/test-helpers.c index 4cdd1fc..3535744 100644 --- a/tests/test-helpers.c +++ b/tests/test-helpers.c @@ -41,6 +41,27 @@ #include "test-runner.h" +#if defined(__FreeBSD__) +#include + +/* + * 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 count_open_fds(void) { @@ -68,6 +89,7 @@ count_open_fds(void) return count; } +#endif void exec_fd_leak_check(int nr_expected_fds)