[4/6] tests: use *BSD replacement for /proc

Submitted by Leonid Bobrov on Feb. 8, 2019, 5:28 p.m.

Details

Message ID 20190208172817.16445-4-mazocomp@disroot.org
State Superseded
Headers show
Series "Series without cover letter" ( rev: 1 ) in Wayland

Not browsing as part of any series.

Commit Message

Leonid Bobrov Feb. 8, 2019, 5:28 p.m.
Signed-off-by: Leonid Bobrov <mazocomp@disroot.org>
---
 Makefile.am          |  3 ++-
 configure.ac         | 12 ++++++++++++
 tests/test-helpers.c | 31 ++++++++++++++++++++++++++++++-
 3 files changed, 44 insertions(+), 2 deletions(-)

Patch hide | download patch | download mbox

diff --git a/Makefile.am b/Makefile.am
index 489f581..8a6fa6e 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -227,7 +227,8 @@  libtest_runner_la_LIBADD =			\
 	libwayland-client.la			\
 	libwayland-server.la			\
 	libtest-helpers.la			\
-	$(RT_LIBS) $(DL_LIBS) $(FFI_LIBS)
+	$(RT_LIBS) $(DL_LIBS)			\
+	$(FFI_LIBS) $(KVM_LIBS)
 
 array_test_SOURCES = tests/array-test.c
 array_test_LDADD = libtest-runner.la
diff --git a/configure.ac b/configure.ac
index f53408a..9ca2331 100644
--- a/configure.ac
+++ b/configure.ac
@@ -65,6 +65,18 @@  AC_SUBST(GCC_CFLAGS)
 AC_CHECK_HEADERS([sys/prctl.h])
 AC_CHECK_FUNCS([accept4 mkostemp posix_fallocate prctl])
 
+# Replacement for /proc on BSD
+AC_CHECK_HEADERS([kvm.h])
+SAVE_LIBS="$LIBS"
+LIBS=
+AC_CHECK_LIB([kvm], [kvm_getfiles])
+KVM_LIBS="$LIBS"
+LIBS="$SAVE_LIBS"
+AC_SUBST(KVM_LIBS)
+if test "x$ac_cv_header_kvm_h" != "x" && test "x$ac_cv_lib_kvm_kvm_getfiles" != "x"; then
+	AC_DEFINE(USE_LIBKVM, 1, [use libkvm on BSD])
+fi
+
 # *BSD don't have libdl, but they have its functions
 SAVE_LIBS="$LIBS"
 LIBS=
diff --git a/tests/test-helpers.c b/tests/test-helpers.c
index b2189d8..4b90a10 100644
--- a/tests/test-helpers.c
+++ b/tests/test-helpers.c
@@ -25,9 +25,18 @@ 
 
 #include "config.h"
 
-#include <assert.h>
+#ifdef USE_LIBKVM
+#include <sys/types.h>
+#include <sys/sysctl.h>
+#include <fcntl.h>
+#include <kvm.h>
+#include <limits.h>
+#else
 #include <errno.h>
 #include <dirent.h>
+#endif
+
+#include <assert.h>
 #include <stdio.h>
 #include <unistd.h>
 #include <time.h>
@@ -43,6 +52,25 @@ 
 int
 count_open_fds(void)
 {
+#ifdef USE_LIBKVM
+	/* Use BSD-specific kernel memory interface */
+
+	struct kinfo_file *kif;
+	kvm_t *kd;
+	int count;
+	char errstr[_POSIX2_LINE_MAX];
+
+	kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY|KVM_NO_FILES, errstr);
+	assert(kd != NULL);
+	kif = kvm_getfiles(kd, KERN_FILE_BYPID, getpid(), sizeof(struct kinfo_file), &count);
+	assert(kif != NULL);
+
+	/* KVM library frees memory on itself */
+	kvm_close(kd);
+	return count;
+#else
+	/* Use /proc filesystem. */
+
 	DIR *dir;
 	struct dirent *ent;
 	int count = 0;
@@ -62,6 +90,7 @@  count_open_fds(void)
 	closedir(dir);
 
 	return count;
+#endif
 }
 
 void