[PATCH v2 4/9] tests: use *BSD replacement for /proc

Leonid Bobrov mazocomp at disroot.org
Sat Feb 9 05:49:41 UTC 2019


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

diff --git a/Makefile.am b/Makefile.am
index 489f581..435957f 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -215,6 +215,7 @@ noinst_LTLIBRARIES +=				\
 	libtest-helpers.la
 
 libtest_helpers_la_SOURCES = tests/test-helpers.c
+libtest_helpers_la_LIBADD = $(KVM_LIBS)
 
 libtest_runner_la_SOURCES =			\
 	tests/test-runner.c			\
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
-- 
2.20.1



More information about the wayland-devel mailing list