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

Submitted by Leonid Bobrov on Feb. 8, 2019, 1:13 p.m.

Details

Message ID 20190208131341.15792-2-mazocomp@disroot.org
State New
Series "Series without cover letter"
Headers show

Commit Message

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

Patch hide | download patch | download mbox

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

Comments

Pekka Paalanen Feb. 8, 2019, 2:03 p.m.
On Fri,  8 Feb 2019 15:13:35 +0200
Leonid Bobrov <mazocomp@disroot.org> wrote:

> Signed-off-by: Leonid Bobrov <mazocomp@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

Hi Leonid,

My autoconf is getting rusty, but this looks ok to me.

> +
>  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"

Why is this adding a second config.h include?

> +
> +#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

I suppose this patch is fine. It's such a small chunk and there does
not seem to be anything else to special-case that would warrant a
new .c file or anything.

At the moment I do think this one needs to wait to see the CI support
materialize.


Thanks,
pq