[PATCH 2/8] tests: use *BSD replacement for /proc

Leonid Bobrov mazocomp at disroot.org
Fri Feb 8 13:13:35 UTC 2019


Signed-off-by: Leonid Bobrov <mazocomp at disroot.org>
---
 Makefile.am          |  2 +-
 configure.ac         | 12 ++++++++++++
 tests/test-helpers.c | 33 ++++++++++++++++++++++++++++++++-
 3 files changed, 45 insertions(+), 2 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index cce4d73..52c7895 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -215,7 +215,7 @@ noinst_LTLIBRARIES +=				\
 	libtest-helpers.la
 
 libtest_helpers_la_SOURCES = tests/test-helpers.c
-libtest_helpers_la_LIBADD = -lrt -ldl $(FFI_LIBS)
+libtest_helpers_la_LIBADD = -lrt -ldl $(FFI_LIBS) $(KVM_LIBS)
 
 libtest_runner_la_SOURCES =			\
 	tests/test-runner.c			\
diff --git a/configure.ac b/configure.ac
index 18fb649..912330e 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
+
 AC_ARG_ENABLE([libraries],
 	      [AC_HELP_STRING([--disable-libraries],
 			      [Disable compilation of wayland libraries])],
diff --git a/tests/test-helpers.c b/tests/test-helpers.c
index b2189d8..1c83e00 100644
--- a/tests/test-helpers.c
+++ b/tests/test-helpers.c
@@ -25,9 +25,20 @@
 
 #include "config.h"
 
-#include <assert.h>
+#include "../config.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 +54,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 +92,7 @@ count_open_fds(void)
 	closedir(dir);
 
 	return count;
+#endif
 }
 
 void
-- 
2.20.1



More information about the wayland-devel mailing list