From 9730b8fe1c1433ed0c764ccd8bd4fe11c4833b7f Mon Sep 17 00:00:00 2001 From: Jim Garlick Date: Tue, 14 Jan 2025 18:07:41 -0800 Subject: [PATCH 1/6] eliminate src/daemon directory Problem: there is not a great reason to have src/daemon split out from src/cmd and src/libdiod. Eliminate that directory, placing diod.c under src/cmd and its other support modules in src/libdiod. --- configure.ac | 1 - scripts/check-root.sh | 2 +- scripts/check-valgrind.sh | 4 +- src/Makefile.am | 1 - src/cmd/Makefile.am | 5 +- src/{daemon => cmd}/diod.c | 2 +- src/daemon/Makefile.am | 74 -------------------- src/libdiod/Makefile.am | 30 +++++++- src/{daemon/exp.c => libdiod/diod_exp.c} | 6 +- src/{daemon/exp.h => libdiod/diod_exp.h} | 0 src/{daemon/fid.c => libdiod/diod_fid.c} | 11 ++- src/{daemon/fid.h => libdiod/diod_fid.h} | 0 src/{daemon/ioctx.c => libdiod/diod_ioctx.c} | 12 ++-- src/{daemon/ioctx.h => libdiod/diod_ioctx.h} | 0 src/{daemon/ops.c => libdiod/diod_ops.c} | 18 ++--- src/{daemon/ops.h => libdiod/diod_ops.h} | 0 src/{daemon/xattr.c => libdiod/diod_xattr.c} | 11 ++- src/{daemon/xattr.h => libdiod/diod_xattr.h} | 0 src/{daemon => libdiod}/test/directory.c | 11 ++- src/{daemon => libdiod}/test/multiuser.c | 10 ++- src/{daemon => libdiod}/test/read.c | 11 ++- src/{daemon => libdiod}/test/threadwrlock.c | 0 tests/kern/Makefile.am | 2 +- tests/misc/t15 | 2 +- tests/user/Makefile.am | 2 +- 25 files changed, 82 insertions(+), 133 deletions(-) rename src/{daemon => cmd}/diod.c (99%) delete mode 100644 src/daemon/Makefile.am rename src/{daemon/exp.c => libdiod/diod_exp.c} (98%) rename src/{daemon/exp.h => libdiod/diod_exp.h} (100%) rename src/{daemon/fid.c => libdiod/diod_fid.c} (94%) rename src/{daemon/fid.h => libdiod/diod_fid.h} (100%) rename src/{daemon/ioctx.c => libdiod/diod_ioctx.c} (98%) rename src/{daemon/ioctx.h => libdiod/diod_ioctx.h} (100%) rename src/{daemon/ops.c => libdiod/diod_ops.c} (99%) rename src/{daemon/ops.h => libdiod/diod_ops.h} (100%) rename src/{daemon/xattr.c => libdiod/diod_xattr.c} (97%) rename src/{daemon/xattr.h => libdiod/diod_xattr.h} (100%) rename src/{daemon => libdiod}/test/directory.c (97%) rename src/{daemon => libdiod}/test/multiuser.c (98%) rename src/{daemon => libdiod}/test/read.c (97%) rename src/{daemon => libdiod}/test/threadwrlock.c (100%) diff --git a/configure.ac b/configure.ac index 87cc0fd9..a0cd3c0f 100644 --- a/configure.ac +++ b/configure.ac @@ -231,7 +231,6 @@ AC_CONFIG_FILES( \ man/diod.conf.5 \ etc/diod.service \ etc/auto.diod \ - src/daemon/Makefile \ src/cmd/Makefile \ src/Makefile \ tests/Makefile \ diff --git a/scripts/check-root.sh b/scripts/check-root.sh index 397c0a04..7b6eb39e 100755 --- a/scripts/check-root.sh +++ b/scripts/check-root.sh @@ -13,5 +13,5 @@ sudo -n make -C src/libnpfs check TESTS="\ test_setgroups.t \ test_setreuid.t" || die "test failed" -sudo -n make -C src/daemon check TESTS="\ +sudo -n make -C src/libdiod check TESTS="\ test_multiuser.t" || die "test failed" diff --git a/scripts/check-valgrind.sh b/scripts/check-valgrind.sh index fa5033e3..6ebb9bda 100755 --- a/scripts/check-valgrind.sh +++ b/scripts/check-valgrind.sh @@ -11,8 +11,8 @@ TESTS="src/libnpfs/test_encoding.t \ src/libnpfs/test_fidpool.t \ src/libnpclient/test_simple.t \ src/libdiod/test_configfile.t \ - src/daemon/test_read.t \ - src/daemon/test_directory.t" + src/libdiod/test_read.t \ + src/libdiod/test_directory.t" exit_rc=0 diff --git a/src/Makefile.am b/src/Makefile.am index 1a1f7e47..2f0dd756 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -4,5 +4,4 @@ SUBDIRS = \ libnpfs \ libnpclient \ libdiod \ - daemon \ cmd diff --git a/src/cmd/Makefile.am b/src/cmd/Makefile.am index c9bed1cb..f9d942b6 100644 --- a/src/cmd/Makefile.am +++ b/src/cmd/Makefile.am @@ -5,7 +5,7 @@ AM_CPPFLAGS = \ $(LUA_INCLUDE) AM_CPPFLAGS += $(ncurses_CPPFLAGS) -sbin_PROGRAMS = diodcat dtop diodload diodls diodshowmount dioddate +sbin_PROGRAMS = diod diodcat dtop diodload diodls diodshowmount dioddate if ENABLE_DIODMOUNT sbin_PROGRAMS += diodmount @@ -24,6 +24,9 @@ common_sources = \ opt.c \ opt.h +diod_LDADD = $(common_ldadd) +diod_SOURCES = diod.c $(common_sources) + diodmount_LDADD = $(common_ldadd) diodmount_SOURCES = diodmount.c $(common_sources) diff --git a/src/daemon/diod.c b/src/cmd/diod.c similarity index 99% rename from src/daemon/diod.c rename to src/cmd/diod.c index 8ce090a6..66b8ae83 100644 --- a/src/daemon/diod.c +++ b/src/cmd/diod.c @@ -52,7 +52,7 @@ #include "src/libdiod/diod_rdma.h" #endif -#include "ops.h" +#include "src/libdiod/diod_ops.h" #if USE_IMPERSONATION_GANESHA #include "src/libnpfs/ganesha-syscalls.h" diff --git a/src/daemon/Makefile.am b/src/daemon/Makefile.am deleted file mode 100644 index 949b03e3..00000000 --- a/src/daemon/Makefile.am +++ /dev/null @@ -1,74 +0,0 @@ -AM_CFLAGS = @WARNING_CFLAGS@ - -AM_CPPFLAGS = \ - -I$(top_srcdir) \ - $(LUA_INCLUDE) - -sbin_PROGRAMS = diod - -diod_LDADD = \ - $(top_builddir)/src/libdiod/libdiod.a \ - $(top_builddir)/src/libnpclient/libnpclient.a \ - $(top_builddir)/src/libnpfs/libnpfs.a \ - $(top_builddir)/src/liblsd/liblsd.a \ - $(top_builddir)/src/libdiod/libdiod.a \ - $(LIBWRAP) $(LIBPTHREAD) $(LUA_LIB) $(LIBMUNGE) $(LIBCAP) \ - $(LIBIBVERBS) $(LIBRDMACM) $(LIBTCMALLOC) - -diod_SOURCES = \ - diod.c \ - ops.c \ - ops.h \ - exp.c \ - exp.h \ - ioctx.c \ - ioctx.h \ - fid.c \ - fid.h \ - xattr.c \ - xattr.h - -test_ldadd = \ - $(top_builddir)/src/libdiod/libdiod.a \ - $(top_builddir)/src/libnpclient/libnpclient.a \ - $(top_builddir)/src/libnpfs/libnpfs.a \ - $(top_builddir)/src/liblsd/liblsd.a \ - $(top_builddir)/src/libdiod/libdiod.a \ - $(top_builddir)/src/libtap/libtap.a \ - $(LIBPTHREAD) - -xtest_ldadd = \ - $(builddir)/ops.o \ - $(builddir)/fid.o \ - $(builddir)/exp.o \ - $(builddir)/ioctx.o \ - $(builddir)/xattr.o \ - $(test_ldadd) \ - $(LUA_LIB) \ - $(LIBCAP) \ - $(LIBMUNGE) \ - $(LIBWRAP) - -TESTS = \ - test_threadwrlock.t \ - test_read.t \ - test_directory.t \ - test_multiuser.t - -check_PROGRAMS = $(TESTS) - -TEST_EXTENSIONS = .t -T_LOG_DRIVER = env AM_TAP_AWK='$(AWK)' $(SHELL) \ - $(top_srcdir)/config/tap-driver.sh - -test_threadwrlock_t_SOURCES = test/threadwrlock.c -test_threadwrlock_t_LDADD = $(test_ldadd) - -test_read_t_SOURCES = test/read.c -test_read_t_LDADD = $(xtest_ldadd) - -test_directory_t_SOURCES = test/directory.c -test_directory_t_LDADD = $(xtest_ldadd) - -test_multiuser_t_SOURCES = test/multiuser.c -test_multiuser_t_LDADD = $(xtest_ldadd) diff --git a/src/libdiod/Makefile.am b/src/libdiod/Makefile.am index c2691e14..3a969f69 100644 --- a/src/libdiod/Makefile.am +++ b/src/libdiod/Makefile.am @@ -15,6 +15,16 @@ libdiod_a_SOURCES = \ diod_conf.h \ diod_sock.c \ diod_sock.h \ + diod_fid.c \ + diod_fid.h \ + diod_ioctx.c \ + diod_ioctx.h \ + diod_xattr.c \ + diod_xattr.h \ + diod_exp.c \ + diod_exp.h \ + diod_ops.c \ + diod_ops.h \ lsderr.c if RDMATRANS @@ -30,10 +40,16 @@ test_ldadd = \ $(top_builddir)/src/libtap/libtap.a \ $(LUA_LIB) \ $(LIBCAP) \ + $(LIBMUNGE) \ + $(LIBWRAP) \ $(LIBPTHREAD) TESTS = \ - test_configfile.t + test_configfile.t \ + test_threadwrlock.t \ + test_read.t \ + test_directory.t \ + test_multiuser.t check_PROGRAMS = $(TESTS) TEST_EXTENSIONS = .t @@ -42,3 +58,15 @@ T_LOG_DRIVER = env AM_TAP_AWK='$(AWK)' $(SHELL) \ test_configfile_t_SOURCES = test/configfile.c test_configfile_t_LDADD = $(test_ldadd) + +test_threadwrlock_t_SOURCES = test/threadwrlock.c +test_threadwrlock_t_LDADD = $(test_ldadd) + +test_read_t_SOURCES = test/read.c +test_read_t_LDADD = $(test_ldadd) + +test_directory_t_SOURCES = test/directory.c +test_directory_t_LDADD = $(test_ldadd) + +test_multiuser_t_SOURCES = test/multiuser.c +test_multiuser_t_LDADD = $(test_ldadd) diff --git a/src/daemon/exp.c b/src/libdiod/diod_exp.c similarity index 98% rename from src/daemon/exp.c rename to src/libdiod/diod_exp.c index a1eb634a..b60d83c8 100644 --- a/src/daemon/exp.c +++ b/src/libdiod/diod_exp.c @@ -36,9 +36,9 @@ #include "src/liblsd/list.h" #include "src/liblsd/hostlist.h" -#include "src/libdiod/diod_conf.h" -#include "src/libdiod/diod_log.h" -#include "exp.h" +#include "diod_conf.h" +#include "diod_log.h" +#include "diod_exp.h" static int _match_export_users (Export *x, Npuser *user) diff --git a/src/daemon/exp.h b/src/libdiod/diod_exp.h similarity index 100% rename from src/daemon/exp.h rename to src/libdiod/diod_exp.h diff --git a/src/daemon/fid.c b/src/libdiod/diod_fid.c similarity index 94% rename from src/daemon/fid.c rename to src/libdiod/diod_fid.c index 9a0faa13..7457fe02 100644 --- a/src/daemon/fid.c +++ b/src/libdiod/diod_fid.c @@ -39,12 +39,11 @@ #include "src/liblsd/hash.h" #include "src/liblsd/hostlist.h" -#include "src/libdiod/diod_conf.h" -#include "src/libdiod/diod_log.h" - -#include "ioctx.h" -#include "xattr.h" -#include "fid.h" +#include "diod_conf.h" +#include "diod_log.h" +#include "diod_ioctx.h" +#include "diod_xattr.h" +#include "diod_fid.h" /* Allocate local fid struct and attach to fid->aux. */ diff --git a/src/daemon/fid.h b/src/libdiod/diod_fid.h similarity index 100% rename from src/daemon/fid.h rename to src/libdiod/diod_fid.h diff --git a/src/daemon/ioctx.c b/src/libdiod/diod_ioctx.c similarity index 98% rename from src/daemon/ioctx.c rename to src/libdiod/diod_ioctx.c index 09529251..b80a376b 100644 --- a/src/daemon/ioctx.c +++ b/src/libdiod/diod_ioctx.c @@ -38,13 +38,13 @@ #include "src/liblsd/hostlist.h" #include "src/libnpfs/xpthread.h" -#include "src/libdiod/diod_conf.h" -#include "src/libdiod/diod_log.h" +#include "diod_conf.h" +#include "diod_log.h" -#include "ioctx.h" -#include "xattr.h" -#include "fid.h" -#include "ops.h" +#include "diod_ioctx.h" +#include "diod_xattr.h" +#include "diod_fid.h" +#include "diod_ops.h" typedef struct pathpool_struct *PathPool; diff --git a/src/daemon/ioctx.h b/src/libdiod/diod_ioctx.h similarity index 100% rename from src/daemon/ioctx.h rename to src/libdiod/diod_ioctx.h diff --git a/src/daemon/ops.c b/src/libdiod/diod_ops.c similarity index 99% rename from src/daemon/ops.c rename to src/libdiod/diod_ops.c index ce71a299..6524c722 100644 --- a/src/daemon/ops.c +++ b/src/libdiod/diod_ops.c @@ -97,15 +97,15 @@ #include "src/liblsd/hash.h" #include "src/liblsd/hostlist.h" -#include "src/libdiod/diod_conf.h" -#include "src/libdiod/diod_log.h" -#include "src/libdiod/diod_auth.h" - -#include "ops.h" -#include "exp.h" -#include "ioctx.h" -#include "xattr.h" -#include "fid.h" +#include "diod_conf.h" +#include "diod_log.h" +#include "diod_auth.h" + +#include "diod_ops.h" +#include "diod_exp.h" +#include "diod_ioctx.h" +#include "diod_xattr.h" +#include "diod_fid.h" #define DIOD_SRV_MAX_MSIZE 1048576 diff --git a/src/daemon/ops.h b/src/libdiod/diod_ops.h similarity index 100% rename from src/daemon/ops.h rename to src/libdiod/diod_ops.h diff --git a/src/daemon/xattr.c b/src/libdiod/diod_xattr.c similarity index 97% rename from src/daemon/xattr.c rename to src/libdiod/diod_xattr.c index fc5fba85..33df501c 100644 --- a/src/daemon/xattr.c +++ b/src/libdiod/diod_xattr.c @@ -48,12 +48,11 @@ #include "src/liblsd/hostlist.h" #include "src/libnpfs/xpthread.h" -#include "src/libdiod/diod_conf.h" -#include "src/libdiod/diod_log.h" - -#include "ioctx.h" -#include "xattr.h" -#include "fid.h" +#include "diod_conf.h" +#include "diod_log.h" +#include "diod_ioctx.h" +#include "diod_xattr.h" +#include "diod_fid.h" #define XATTR_FLAGS_GET 1 #define XATTR_FLAGS_SET 2 diff --git a/src/daemon/xattr.h b/src/libdiod/diod_xattr.h similarity index 100% rename from src/daemon/xattr.h rename to src/libdiod/diod_xattr.h diff --git a/src/daemon/test/directory.c b/src/libdiod/test/directory.c similarity index 97% rename from src/daemon/test/directory.c rename to src/libdiod/test/directory.c index 95263fb7..628d084e 100644 --- a/src/daemon/test/directory.c +++ b/src/libdiod/test/directory.c @@ -32,15 +32,14 @@ #include "src/libnpfs/9p.h" #include "src/libnpfs/npfs.h" #include "src/libnpclient/npclient.h" - #include "src/liblsd/list.h" -#include "src/libdiod/diod_log.h" -#include "src/libdiod/diod_conf.h" -#include "src/libdiod/diod_sock.h" - -#include "ops.h" #include "src/libtap/tap.h" +#include "diod_log.h" +#include "diod_conf.h" +#include "diod_sock.h" +#include "diod_ops.h" + #define TEST_MSIZE 8192 #define TEST_ITER 64 diff --git a/src/daemon/test/multiuser.c b/src/libdiod/test/multiuser.c similarity index 98% rename from src/daemon/test/multiuser.c rename to src/libdiod/test/multiuser.c index e0116852..3074e74d 100644 --- a/src/daemon/test/multiuser.c +++ b/src/libdiod/test/multiuser.c @@ -32,15 +32,13 @@ #include "src/libnpfs/9p.h" #include "src/libnpfs/npfs.h" #include "src/libnpclient/npclient.h" - #include "src/liblsd/list.h" -#include "src/libdiod/diod_log.h" -#include "src/libdiod/diod_conf.h" -#include "src/libdiod/diod_sock.h" - #include "src/libtap/tap.h" -#include "ops.h" +#include "diod_log.h" +#include "diod_conf.h" +#include "diod_sock.h" +#include "diod_ops.h" #define TEST_MSIZE 8192 diff --git a/src/daemon/test/read.c b/src/libdiod/test/read.c similarity index 97% rename from src/daemon/test/read.c rename to src/libdiod/test/read.c index fc860dff..d93877f5 100644 --- a/src/daemon/test/read.c +++ b/src/libdiod/test/read.c @@ -32,14 +32,13 @@ #include "src/libnpfs/9p.h" #include "src/libnpfs/npfs.h" #include "src/libnpclient/npclient.h" - +#include "src/libtap/tap.h" #include "src/liblsd/list.h" -#include "src/libdiod/diod_log.h" -#include "src/libdiod/diod_conf.h" -#include "src/libdiod/diod_sock.h" -#include "ops.h" -#include "src/libtap/tap.h" +#include "diod_log.h" +#include "diod_conf.h" +#include "diod_sock.h" +#include "diod_ops.h" #define TEST_MSIZE 8192 diff --git a/src/daemon/test/threadwrlock.c b/src/libdiod/test/threadwrlock.c similarity index 100% rename from src/daemon/test/threadwrlock.c rename to src/libdiod/test/threadwrlock.c diff --git a/tests/kern/Makefile.am b/tests/kern/Makefile.am index a670c02e..c03d611f 100644 --- a/tests/kern/Makefile.am +++ b/tests/kern/Makefile.am @@ -16,7 +16,7 @@ check_PROGRAMS = \ SUBDIRS = fstest postmark fsx fsstress AM_TESTS_ENVIRONMENT = \ - PATH_DIOD='$(top_builddir)/src/daemon/diod'; export PATH_DIOD; \ + PATH_DIOD='$(top_builddir)/src/cmd/diod'; export PATH_DIOD; \ PATH_DIODMOUNT='$(top_builddir)/src/cmd/diodmount'; export PATH_DIODMOUNT; \ KERN_SRCDIR='$(top_srcdir)/tests/kern'; export KERN_SRCDIR; \ KERN_BUILDDIR='$(top_builddir)/tests/kern'; export KERN_BUILDDIR; \ diff --git a/tests/misc/t15 b/tests/misc/t15 index 8d31de22..77f0f8c6 100755 --- a/tests/misc/t15 +++ b/tests/misc/t15 @@ -12,7 +12,7 @@ bg_test () TEST=$(basename $0 | cut -d- -f1) sockfile=$(mktemp) bg_test $sockfile & -${MISC_SRCDIR}/memcheck ${TOP_BUILDDIR}/src/daemon/diod -c /dev/null -n -f -e ctl -l $sockfile -s >$TEST.out 2>&1 +${MISC_SRCDIR}/memcheck ${TOP_BUILDDIR}/src/cmd/diod -c /dev/null -n -f -e ctl -l $sockfile -s >$TEST.out 2>&1 diff ${MISC_SRCDIR}/$TEST.exp $TEST.out >$TEST.diff rm -f $sockfile wait %1 diff --git a/tests/user/Makefile.am b/tests/user/Makefile.am index 20b22494..fbb822eb 100644 --- a/tests/user/Makefile.am +++ b/tests/user/Makefile.am @@ -15,7 +15,7 @@ check_PROGRAMS = \ testopenfid AM_TESTS_ENVIRONMENT = \ - PATH_DIOD='$(top_builddir)/src/daemon/diod'; export PATH_DIOD; \ + PATH_DIOD='$(top_builddir)/src/cmd/diod'; export PATH_DIOD; \ PATH_DIODCONF='$(top_builddir)/etc/diod.conf'; export PATH_DIODCONF; \ USER_SRCDIR='$(top_srcdir)/tests/user'; export USER_SRCDIR; \ USER_BUILDDIR='$(top_builddir)/tests/user'; export USER_BUILDDIR; From 5567efc9646554442760e02f43cfb3a4f130547f Mon Sep 17 00:00:00 2001 From: Jim Garlick Date: Wed, 15 Jan 2025 06:29:22 -0800 Subject: [PATCH 2/6] testsuite: factor out duplicated thread code Problem: unit tests duplicate code for synchronizing state between threads. Move this code to a shared location in src/libutil. --- configure.ac | 1 + src/Makefile.am | 1 + src/libdiod/Makefile.am | 1 + src/libdiod/test/threadwrlock.c | 76 +++++-------------------------- src/libnpfs/Makefile.am | 1 + src/libnpfs/test/capability.c | 78 +++++--------------------------- src/libnpfs/test/setfsuid.c | 79 +++++---------------------------- src/libnpfs/test/setgroups.c | 77 +++++--------------------------- src/libnpfs/test/setreuid.c | 77 +++++--------------------------- src/libtest/Makefile.am | 12 +++++ src/libtest/state.c | 54 ++++++++++++++++++++++ src/libtest/state.h | 24 ++++++++++ src/libtest/thread.c | 64 ++++++++++++++++++++++++++ src/libtest/thread.h | 28 ++++++++++++ 14 files changed, 244 insertions(+), 329 deletions(-) create mode 100644 src/libtest/Makefile.am create mode 100644 src/libtest/state.c create mode 100644 src/libtest/state.h create mode 100644 src/libtest/thread.c create mode 100644 src/libtest/thread.h diff --git a/configure.ac b/configure.ac index a0cd3c0f..721935dd 100644 --- a/configure.ac +++ b/configure.ac @@ -215,6 +215,7 @@ AC_CONFIG_FILES( \ Makefile \ diod.spec \ src/libtap/Makefile \ + src/libtest/Makefile \ src/libdiod/Makefile \ src/liblsd/Makefile \ src/libnpfs/Makefile \ diff --git a/src/Makefile.am b/src/Makefile.am index 2f0dd756..4980312b 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,5 +1,6 @@ SUBDIRS = \ libtap \ + libtest \ liblsd \ libnpfs \ libnpclient \ diff --git a/src/libdiod/Makefile.am b/src/libdiod/Makefile.am index 3a969f69..d5ab1851 100644 --- a/src/libdiod/Makefile.am +++ b/src/libdiod/Makefile.am @@ -37,6 +37,7 @@ test_ldadd = \ $(top_builddir)/src/libnpfs/libnpfs.a \ $(top_builddir)/src/liblsd/liblsd.a \ $(builddir)/libdiod.a \ + $(top_builddir)/src/libtest/libtest.a \ $(top_builddir)/src/libtap/libtap.a \ $(LUA_LIB) \ $(LIBCAP) \ diff --git a/src/libdiod/test/threadwrlock.c b/src/libdiod/test/threadwrlock.c index 3ad66e48..244ec233 100644 --- a/src/libdiod/test/threadwrlock.c +++ b/src/libdiod/test/threadwrlock.c @@ -31,6 +31,8 @@ #include #include +#include "src/libtest/state.h" +#include "src/libtest/thread.h" #include "src/libtap/tap.h" #define TEST_UID 100 @@ -38,65 +40,9 @@ typedef enum { S0, S1, S2, S3, S4, S5 } state_t; -static state_t state = S0; -static pthread_mutex_t state_lock = PTHREAD_MUTEX_INITIALIZER; -static pthread_cond_t state_cond = PTHREAD_COND_INITIALIZER; static char path[] = "/tmp/test.fcntl.XXXXXX"; static int fd = -1; -static void test_lock (pthread_mutex_t *l) -{ - int n = pthread_mutex_lock (l); - if (n) - BAIL_OUT ("pthread_mutex_lock: %s", strerror (n)); -} -static void test_unlock (pthread_mutex_t *l) -{ - int n = pthread_mutex_unlock (l); - if (n) - BAIL_OUT ("pthread_mutex_unlock: %s", strerror (n)); -} -static void test_condsig (pthread_cond_t *c) -{ - int n = pthread_cond_signal (c); - if (n) - BAIL_OUT ("pthread_cond_signal: %s", strerror (n)); -} -static void test_condwait (pthread_cond_t *c, pthread_mutex_t *l) -{ - int n = pthread_cond_wait (c, l); - if (n) - BAIL_OUT ("pthread_cond_wait: %s", strerror (n)); -} -static void test_thread_create (pthread_t *t, void *(f)(void *), void *a) -{ - int n = pthread_create (t, NULL, f, a); - if (n) - BAIL_OUT ("pthread_create: %s", strerror (n)); -} -static void test_thread_join (pthread_t t, void **a) -{ - int n = pthread_join (t, a); - if (n) - BAIL_OUT ("pthread_join: %s", strerror (n)); -} - -static void change_state (state_t s) -{ - test_lock (&state_lock); - state = s; - test_condsig (&state_cond); - test_unlock (&state_lock); -} - -static void wait_state (state_t s) -{ - test_lock (&state_lock); - while (state != s) - test_condwait (&state_cond, &state_lock); - test_unlock (&state_lock); -} - static void mkfile (void) { char buf[1024]; @@ -123,11 +69,11 @@ static void *proc1 (void *a) f.l_len = 64; ok (fcntl (fd, F_SETLK, &f) == 0, "proc1: fcntl F_WRLCK 0-63 works"); - change_state (S1); + test_state_change (S1); - wait_state (S2); - change_state (S3); - wait_state (S4); + test_state_wait (S2); + test_state_change (S3); + test_state_wait (S4); return NULL; } @@ -135,7 +81,7 @@ static void *proc2 (void *a) { struct flock f; - wait_state (S1); + test_state_wait (S1); diag ("proc2: locking bytes 0-63 (locked in same process)"); f.l_type = F_WRLCK; f.l_whence = SEEK_SET; @@ -143,10 +89,10 @@ static void *proc2 (void *a) f.l_len = 64; ok (fcntl (fd, F_SETLK, &f) == 0, "proc2: fcntl F_WRLCK 0-63 works"); - change_state (S2); + test_state_change (S2); - wait_state (S3); - change_state (S4); + test_state_wait (S3); + test_state_change (S4); return NULL; } @@ -161,6 +107,8 @@ main (int arg, char *argv[]) plan (NO_PLAN); + test_state_init (S0); + mkfile (); /* same task/thread contending for write lock */ diff --git a/src/libnpfs/Makefile.am b/src/libnpfs/Makefile.am index addb669f..9037ae67 100644 --- a/src/libnpfs/Makefile.am +++ b/src/libnpfs/Makefile.am @@ -40,6 +40,7 @@ endif test_ldadd = \ $(builddir)/libnpfs.a \ $(top_builddir)/src/liblsd/liblsd.a \ + $(top_builddir)/src/libtest/libtest.a \ $(top_builddir)/src/libtap/libtap.a \ $(LIBCAP) \ $(LIBPTHREAD) diff --git a/src/libnpfs/test/capability.c b/src/libnpfs/test/capability.c index b98a569a..b0e5d6bf 100644 --- a/src/libnpfs/test/capability.c +++ b/src/libnpfs/test/capability.c @@ -30,52 +30,13 @@ #include #include "src/libdiod/diod_log.h" +#include "src/libtest/thread.h" +#include "src/libtest/state.h" #include "src/libtap/tap.h" #if HAVE_LIBCAP typedef enum { S0, S1, S2, S3, S4, S5 } state_t; -static state_t state = S0; -static pthread_mutex_t state_lock = PTHREAD_MUTEX_INITIALIZER; -static pthread_cond_t state_cond = PTHREAD_COND_INITIALIZER; - -static void test_lock (pthread_mutex_t *l) -{ - int n = pthread_mutex_lock (l); - if (n) - BAIL_OUT ("pthread_mutex_lock: %s", strerror (n)); -} -static void test_unlock (pthread_mutex_t *l) -{ - int n = pthread_mutex_unlock (l); - if (n) - BAIL_OUT ("pthread_mutex_unlock: %s", strerror (n)); -} -static void test_condsig (pthread_cond_t *c) -{ - int n = pthread_cond_signal (c); - if (n) - BAIL_OUT ("pthread_cond_signal: %s", strerror (n)); -} -static void test_condwait (pthread_cond_t *c, pthread_mutex_t *l) -{ - int n = pthread_cond_wait (c, l); - if (n) - BAIL_OUT ("pthread_cond_wait: %s", strerror (n)); -} -static void test_thread_create (pthread_t *t, void *(f)(void *), void *a) -{ - int n = pthread_create (t, NULL, f, a); - if (n) - BAIL_OUT ("pthread_create: %s", strerror (n)); -} -static void test_thread_join (pthread_t t, void **a) -{ - int n = pthread_join (t, a); - if (n) - BAIL_OUT ("pthread_join: %s", strerror (n)); -} - static void check_capability (const char *who, const char *capname, cap_value_t capflag, @@ -111,45 +72,27 @@ static void set_capability (char *who, BAIL_OUT ("%s: cap_free failed", who); } -static void -change_state (state_t s) -{ - test_lock (&state_lock); - state = s; - test_condsig (&state_cond); - test_unlock (&state_lock); -} - -static void -wait_state (state_t s) -{ - test_lock (&state_lock); - while ((state != s)) - test_condwait (&state_cond, &state_lock); - test_unlock (&state_lock); -} - static void *proc1 (void *a) { /* 1) task 1, expect clr */ check_capability ("task1", "DAC_OVERRIDE", CAP_DAC_OVERRIDE, CAP_CLEAR); check_capability ("task1", "CHOWN", CAP_CHOWN, CAP_CLEAR); - change_state (S1); - wait_state (S2); + test_state_change (S1); + test_state_wait (S2); /* 4) task 1, still clr */ check_capability ("task1", "DAC_OVERRIDE", CAP_DAC_OVERRIDE, CAP_CLEAR); check_capability ("task1", "CHOWN", CAP_CHOWN, CAP_CLEAR); diag ("task1: clr cap"); set_capability ("task1", "DAC_OVERRIDE", CAP_DAC_OVERRIDE, CAP_CLEAR); - change_state (S3); - wait_state (S4); + test_state_change (S3); + test_state_wait (S4); return NULL; } static void *proc2 (void *a) { /* 2) task 2, expect clr */ - wait_state (S1); + test_state_wait (S1); check_capability ("task2", "DAC_OVERRIDE", CAP_DAC_OVERRIDE, CAP_CLEAR); check_capability ("task2", "CHOWN", CAP_CHOWN, CAP_CLEAR); diag ("task2: set cap"); @@ -158,12 +101,12 @@ static void *proc2 (void *a) /* 3) task 2, expect set */ check_capability ("task2", "DAC_OVERRIDE", CAP_DAC_OVERRIDE, CAP_SET); check_capability ("task2", "CHOWN", CAP_CHOWN, CAP_SET); - change_state (S2); - wait_state (S3); + test_state_change (S2); + test_state_wait (S3); /* 5) task 2, expect set */ check_capability ("task2", "DAC_OVERRIDE", CAP_DAC_OVERRIDE, CAP_SET); check_capability ("task2", "CHOWN", CAP_CHOWN, CAP_SET); - change_state (S4); + test_state_change (S4); return NULL; } @@ -233,6 +176,7 @@ int main(int argc, char *argv[]) if (geteuid () != 0 || getenv ("FAKEROOTKEY") != NULL) plan (SKIP_ALL, "this test must run as root"); plan (NO_PLAN); + test_state_init (S0); proc0 (); // spawns proc1 and proc2 #else plan (SKIP_ALL, "libcap2-dev is not installed"); diff --git a/src/libnpfs/test/setfsuid.c b/src/libnpfs/test/setfsuid.c index bceb2f93..27da3df4 100644 --- a/src/libnpfs/test/setfsuid.c +++ b/src/libnpfs/test/setfsuid.c @@ -33,6 +33,8 @@ #include #include +#include "src/libtest/thread.h" +#include "src/libtest/state.h" #include "src/libtap/tap.h" #define TEST_UID 100 @@ -40,47 +42,6 @@ typedef enum { S0, S1, S2, S3, S4, S5 } state_t; -static state_t state = S0; -static pthread_mutex_t state_lock = PTHREAD_MUTEX_INITIALIZER; -static pthread_cond_t state_cond = PTHREAD_COND_INITIALIZER; - -static void test_lock (pthread_mutex_t *l) -{ - int n = pthread_mutex_lock (l); - if (n) - BAIL_OUT ("pthread_mutex_lock: %s", strerror (n)); -} -static void test_unlock (pthread_mutex_t *l) -{ - int n = pthread_mutex_unlock (l); - if (n) - BAIL_OUT ("pthread_mutex_unlock: %s", strerror (n)); -} -static void test_condsig (pthread_cond_t *c) -{ - int n = pthread_cond_signal (c); - if (n) - BAIL_OUT ("pthread_cond_signal: %s", strerror (n)); -} -static void test_condwait (pthread_cond_t *c, pthread_mutex_t *l) -{ - int n = pthread_cond_wait (c, l); - if (n) - BAIL_OUT ("pthread_cond_wait: %s", strerror (n)); -} -static void test_thread_create (pthread_t *t, void *(f)(void *), void *a) -{ - int n = pthread_create (t, NULL, f, a); - if (n) - BAIL_OUT ("pthread_create: %s", strerror (n)); -} -static void test_thread_join (pthread_t t, void **a) -{ - int n = pthread_join (t, a); - if (n) - BAIL_OUT ("pthread_join: %s", strerror (n)); -} - static void check_fsid (char *s, uid_t uid, gid_t gid) { @@ -122,49 +83,31 @@ change_fsid (char *s, uid_t fromuid, gid_t fromgid, uid_t uid, gid_t gid) } } -static void -change_state (state_t s) -{ - test_lock (&state_lock); - state = s; - test_condsig (&state_cond); - test_unlock (&state_lock); -} - -static void -wait_state (state_t s) -{ - test_lock (&state_lock); - while ((state != s)) - test_condwait (&state_cond, &state_lock); - test_unlock (&state_lock); -} - static void *proc1 (void *a) { check_fsid ("task1", 0, 0); - change_state (S1); - wait_state (S2); + test_state_change (S1); + test_state_wait (S2); check_fsid ("task1", 0, 0); change_fsid ("task1", 0, 0, TEST_UID, TEST_GID); check_fsid ("task1", TEST_UID, TEST_GID); - change_state (S3); - wait_state (S4); + test_state_change (S3); + test_state_wait (S4); check_fsid ("task1", TEST_UID, TEST_GID); return NULL; } static void *proc2 (void *a) { - wait_state (S1); + test_state_wait (S1); check_fsid ("task2", 0, 0); change_fsid ("task2", 0, 0, TEST_UID, TEST_GID); check_fsid ("task2", TEST_UID, TEST_GID); - change_state (S2); - wait_state (S3); + test_state_change (S2); + test_state_wait (S3); change_fsid ("task2", TEST_UID, TEST_GID, 0, 0); check_fsid ("task2", 0, 0); - change_state (S4); + test_state_change (S4); return NULL; } @@ -176,6 +119,8 @@ int main(int argc, char *argv[]) plan (SKIP_ALL, "this test must run as root"); plan (NO_PLAN); + test_state_init (S0); + check_fsid ("task0", 0, 0); test_thread_create (&t1, proc1, NULL); diff --git a/src/libnpfs/test/setgroups.c b/src/libnpfs/test/setgroups.c index df4312b0..2464d685 100644 --- a/src/libnpfs/test/setgroups.c +++ b/src/libnpfs/test/setgroups.c @@ -32,6 +32,8 @@ #include #include +#include "src/libtest/thread.h" +#include "src/libtest/state.h" #include "src/libtap/tap.h" static void check_fsid (uid_t uid, gid_t gid) @@ -149,63 +151,6 @@ void test_single_thread (void) typedef enum { S0, S1, S2, S3, S4, S5 } state_t; -static state_t state = S0; -static pthread_mutex_t state_lock = PTHREAD_MUTEX_INITIALIZER; -static pthread_cond_t state_cond = PTHREAD_COND_INITIALIZER; - -static void test_lock (pthread_mutex_t *l) -{ - int n = pthread_mutex_lock (l); - if (n) - BAIL_OUT ("pthread_mutex_lock: %s", strerror (n)); -} -static void test_unlock (pthread_mutex_t *l) -{ - int n = pthread_mutex_unlock (l); - if (n) - BAIL_OUT ("pthread_mutex_unlock: %s", strerror (n)); -} -static void test_condsig (pthread_cond_t *c) -{ - int n = pthread_cond_signal (c); - if (n) - BAIL_OUT ("pthread_cond_signal: %s", strerror (n)); -} -static void test_condwait (pthread_cond_t *c, pthread_mutex_t *l) -{ - int n = pthread_cond_wait (c, l); - if (n) - BAIL_OUT ("pthread_cond_wait: %s", strerror (n)); -} -static void test_thread_create (pthread_t *t, void *(f)(void *), void *a) -{ - int n = pthread_create (t, NULL, f, a); - if (n) - BAIL_OUT ("pthread_create: %s", strerror (n)); -} -static void test_thread_join (pthread_t t, void **a) -{ - int n = pthread_join (t, a); - if (n) - BAIL_OUT ("pthread_join: %s", strerror (n)); -} - -static void change_state (state_t s) -{ - test_lock (&state_lock); - state = s; - test_condsig (&state_cond); - test_unlock (&state_lock); -} - -static void wait_state (state_t s) -{ - test_lock (&state_lock); - while ((state != s)) - test_condwait (&state_cond, &state_lock); - test_unlock (&state_lock); -} - static const char *strgroups (char *buf, size_t size, gid_t *g, size_t len) { buf[0] = '\0'; @@ -234,18 +179,18 @@ static void *proc1 (void *a) gid_t g[] = { 101 }; check_groups ("task1", ""); - change_state (S1); + test_state_change (S1); - wait_state (S2); + test_state_wait (S2); check_groups ("task1", ""); diag ("task1: SYS_setgroups [101]"); if (syscall (SYS_setgroups, 1, g) < 0) BAIL_OUT ("task1: SYS_setgroups: %s", strerror (errno)); check_groups ("task1", "101"); - change_state (S3); + test_state_change (S3); - wait_state (S4); + test_state_wait (S4); return NULL; } @@ -253,18 +198,18 @@ static void *proc2 (void *a) { gid_t g[] = { 100 }; - wait_state (S1); + test_state_wait (S1); check_groups ("task2", ""); diag ("task2: SYS_setgroups [100]"); if (syscall (SYS_setgroups, 1, g) < 0) BAIL_OUT ("task2: SYS_setgroups: %s", strerror (errno)); check_groups ("task2", "100"); - change_state (S2); + test_state_change (S2); - wait_state (S3); + test_state_wait (S3); check_groups ("task2", "100"); - change_state (S4); + test_state_change (S4); return NULL; } @@ -272,6 +217,8 @@ void test_multi_thread (void) { pthread_t t1, t2; + test_state_init (S0); + diag ("task0: SYS_setgroups []"); if (syscall (SYS_setgroups, 0, NULL) < 0) BAIL_OUT ("task0: SYS_setgroups: %s", strerror (errno)); diff --git a/src/libnpfs/test/setreuid.c b/src/libnpfs/test/setreuid.c index cd93e37f..94a4eae2 100644 --- a/src/libnpfs/test/setreuid.c +++ b/src/libnpfs/test/setreuid.c @@ -30,99 +30,44 @@ #include #include +#include "src/libtest/thread.h" +#include "src/libtest/state.h" #include "src/libtap/tap.h" typedef enum { S0, S1, S2, S3, S4, S5 } state_t; -static state_t state = S0; -static pthread_mutex_t state_lock = PTHREAD_MUTEX_INITIALIZER; -static pthread_cond_t state_cond = PTHREAD_COND_INITIALIZER; - -static void test_lock (pthread_mutex_t *l) -{ - int n = pthread_mutex_lock (l); - if (n) - BAIL_OUT ("pthread_mutex_lock: %s", strerror (n)); -} -static void test_unlock (pthread_mutex_t *l) -{ - int n = pthread_mutex_unlock (l); - if (n) - BAIL_OUT ("pthread_mutex_unlock: %s", strerror (n)); -} -static void test_condsig (pthread_cond_t *c) -{ - int n = pthread_cond_signal (c); - if (n) - BAIL_OUT ("pthread_cond_signal: %s", strerror (n)); -} -static void test_condwait (pthread_cond_t *c, pthread_mutex_t *l) -{ - int n = pthread_cond_wait (c, l); - if (n) - BAIL_OUT ("pthread_cond_wait: %s", strerror (n)); -} -static void test_thread_create (pthread_t *t, void *(f)(void *), void *a) -{ - int n = pthread_create (t, NULL, f, a); - if (n) - BAIL_OUT ("pthread_create: %s", strerror (n)); -} -static void test_thread_join (pthread_t t, void **a) -{ - int n = pthread_join (t, a); - if (n) - BAIL_OUT ("pthread_join: %s", strerror (n)); -} - -static void change_state (state_t s) -{ - test_lock (&state_lock); - state = s; - test_condsig (&state_cond); - test_unlock (&state_lock); -} - -static void wait_state (state_t s) -{ - test_lock (&state_lock); - while ((state != s)) - test_condwait (&state_cond, &state_lock); - test_unlock (&state_lock); -} - static void *proc1 (void *a) { ok (geteuid () == 0, "task1: geteuid returned 0"); - change_state (S1); + test_state_change (S1); - wait_state (S2); + test_state_wait (S2); ok (geteuid () == 100, "task1: geteuid returned 100"); ok (setreuid (0, 0) == 0, "task1: setreuid 0 0 worked"); ok (setreuid (-1, 101) == 0, "task1: setreuid -1 %d works", 101); ok (geteuid () == 101, "task1: geteuid returned 101"); - change_state (S3); + test_state_change (S3); - wait_state (S4); + test_state_wait (S4); ok (geteuid () == 102, "task1: geteuid returned 102"); return NULL; } static void *proc2 (void *a) { - wait_state (S1); + test_state_wait (S1); ok (geteuid () == 0, "task2: geteuid returned 0"); ok (setreuid (0, 0) == 0, "setreuid 0 0 worked"); ok (setreuid (-1, 100) == 0, "setreuid -1 %d worked", 100); ok (geteuid () == 100, "task2: geteuid returned 100"); - change_state (S2); + test_state_change (S2); - wait_state (S3); + test_state_wait (S3); ok (geteuid () == 101, "task2: geteuid returned 101"); ok (setreuid (0, 0) == 0, "setreuid 0 0 worked"); ok (setreuid (-1, 102) == 0, "setreuid -1 %d worked", 102); ok (geteuid () == 102, "task2: geteuid returned 102"); - change_state (S4); + test_state_change (S4); return NULL; } @@ -135,7 +80,7 @@ int main(int argc, char *argv[]) pthread_t t1, t2; - ok (geteuid () == 0, "task0: geteuid returned 0"); + test_state_init (S0); test_thread_create (&t1, proc1, NULL); test_thread_create (&t2, proc2, NULL); diff --git a/src/libtest/Makefile.am b/src/libtest/Makefile.am new file mode 100644 index 00000000..baec4c78 --- /dev/null +++ b/src/libtest/Makefile.am @@ -0,0 +1,12 @@ +AM_CFLAGS = @WARNING_CFLAGS@ + +AM_CPPFLAGS = \ + -I$(top_srcdir) + +check_LIBRARIES = libtest.a + +libtest_a_SOURCES = \ + thread.c \ + thread.h \ + state.c \ + state.h diff --git a/src/libtest/state.c b/src/libtest/state.c new file mode 100644 index 00000000..03d959b9 --- /dev/null +++ b/src/libtest/state.c @@ -0,0 +1,54 @@ +/************************************************************\ + * Copyright 2010 Lawrence Livermore National Security, LLC + * (c.f. AUTHORS, NOTICE.LLNS, COPYING) + * + * This file is part of the diod 9P server project. + * For details, see https://github.com/chaos/diod. + * + * SPDX-License-Identifier: GPL-2.0-or-later +\************************************************************/ + +#if HAVE_CONFIG_H +#include "config.h" +#endif +#include + +#include "src/libtap/tap.h" + +#include "thread.h" +#include "state.h" + +static int state; +static pthread_mutex_t state_lock; +static pthread_cond_t state_cond; + +void test_state_init (int s) +{ + int e; + + if ((e = pthread_mutex_init (&state_lock, NULL))) + BAIL_OUT ("pthread_mutex_init: %s", strerror (e)); + if ((e = pthread_cond_init (&state_cond, NULL))) + BAIL_OUT ("pthread_cond_init: %s", strerror (e)); + state = s; +} + +void test_state_change (int s) +{ + test_lock (&state_lock); + state = s; + test_condsig (&state_cond); + test_unlock (&state_lock); +} + +void test_state_wait (int s) +{ + test_lock (&state_lock); + while ((state != s)) + test_condwait (&state_cond, &state_lock); + test_unlock (&state_lock); +} + +/* + * vi:tabstop=4 shiftwidth=4 expandtab + */ diff --git a/src/libtest/state.h b/src/libtest/state.h new file mode 100644 index 00000000..3f2b6b0c --- /dev/null +++ b/src/libtest/state.h @@ -0,0 +1,24 @@ +/************************************************************\ + * Copyright 2010 Lawrence Livermore National Security, LLC + * (c.f. AUTHORS, NOTICE.LLNS, COPYING) + * + * This file is part of the diod 9P server project. + * For details, see https://github.com/chaos/diod. + * + * SPDX-License-Identifier: GPL-2.0-or-later +\************************************************************/ + +#ifndef _LIBTEST_STATE_H +#define _LIBTEST_STATE_H + +#include + +void test_state_init (int s); +void test_state_change (int s); +void test_state_wait (int s); + +#endif + +/* + * vi:tabstop=4 shiftwidth=4 expandtab + */ diff --git a/src/libtest/thread.c b/src/libtest/thread.c new file mode 100644 index 00000000..cefaff8d --- /dev/null +++ b/src/libtest/thread.c @@ -0,0 +1,64 @@ +/************************************************************\ + * Copyright 2010 Lawrence Livermore National Security, LLC + * (c.f. AUTHORS, NOTICE.LLNS, COPYING) + * + * This file is part of the diod 9P server project. + * For details, see https://github.com/chaos/diod. + * + * SPDX-License-Identifier: GPL-2.0-or-later +\************************************************************/ + +#if HAVE_CONFIG_H +#include "config.h" +#endif +#include +#include + +#include "thread.h" +#include "src/libtap/tap.h" + +void test_lock (pthread_mutex_t *l) +{ + int n = pthread_mutex_lock (l); + if (n) + BAIL_OUT ("pthread_mutex_lock: %s", strerror (n)); +} + +void test_unlock (pthread_mutex_t *l) +{ + int n = pthread_mutex_unlock (l); + if (n) + BAIL_OUT ("pthread_mutex_unlock: %s", strerror (n)); +} + +void test_condsig (pthread_cond_t *c) +{ + int n = pthread_cond_signal (c); + if (n) + BAIL_OUT ("pthread_cond_signal: %s", strerror (n)); +} + +void test_condwait (pthread_cond_t *c, pthread_mutex_t *l) +{ + int n = pthread_cond_wait (c, l); + if (n) + BAIL_OUT ("pthread_cond_wait: %s", strerror (n)); +} + +void test_thread_create (pthread_t *t, void *(f)(void *), void *a) +{ + int n = pthread_create (t, NULL, f, a); + if (n) + BAIL_OUT ("pthread_create: %s", strerror (n)); +} + +void test_thread_join (pthread_t t, void **a) +{ + int n = pthread_join (t, a); + if (n) + BAIL_OUT ("pthread_join: %s", strerror (n)); +} + +/* + * vi:tabstop=4 shiftwidth=4 expandtab + */ diff --git a/src/libtest/thread.h b/src/libtest/thread.h new file mode 100644 index 00000000..6b1c8a6f --- /dev/null +++ b/src/libtest/thread.h @@ -0,0 +1,28 @@ +/************************************************************\ + * Copyright 2010 Lawrence Livermore National Security, LLC + * (c.f. AUTHORS, NOTICE.LLNS, COPYING) + * + * This file is part of the diod 9P server project. + * For details, see https://github.com/chaos/diod. + * + * SPDX-License-Identifier: GPL-2.0-or-later +\************************************************************/ + +#ifndef LIBTEST_THREAD_H +#define LIBTEST_THREAD_H + +#include + +/* wrappers for pthreads operations that call BAIL_OUT internally on failure */ +void test_lock (pthread_mutex_t *l); +void test_unlock (pthread_mutex_t *l); +void test_condsig (pthread_cond_t *c); +void test_condwait (pthread_cond_t *c, pthread_mutex_t *l); +void test_thread_create (pthread_t *t, void *(f)(void *), void *a); +void test_thread_join (pthread_t t, void **a); + +#endif + +/* + * vi:tabstop=4 shiftwidth=4 expandtab + */ From c992e0e37061206e856dc9ccda8ed72849cc2bec Mon Sep 17 00:00:00 2001 From: Jim Garlick Date: Wed, 15 Jan 2025 08:03:05 -0800 Subject: [PATCH 3/6] testsuite: factor out duplicated server code Problem: code for embedding the diod server in a unit test is repeated in each test. Move this code to a shared location in libtest. --- src/libdiod/Makefile.am | 2 +- src/libdiod/test/directory.c | 58 +++++------------------------- src/libdiod/test/multiuser.c | 70 ++++++++---------------------------- src/libdiod/test/read.c | 61 ++++++------------------------- src/libtest/Makefile.am | 4 ++- src/libtest/server.c | 70 ++++++++++++++++++++++++++++++++++++ src/libtest/server.h | 34 ++++++++++++++++++ 7 files changed, 142 insertions(+), 157 deletions(-) create mode 100644 src/libtest/server.c create mode 100644 src/libtest/server.h diff --git a/src/libdiod/Makefile.am b/src/libdiod/Makefile.am index d5ab1851..6da66df9 100644 --- a/src/libdiod/Makefile.am +++ b/src/libdiod/Makefile.am @@ -32,12 +32,12 @@ libdiod_a_SOURCES += diod_rdma.c diod_rdma.h endif test_ldadd = \ + $(top_builddir)/src/libtest/libtest.a \ $(builddir)/libdiod.a \ $(top_builddir)/src/libnpclient/libnpclient.a \ $(top_builddir)/src/libnpfs/libnpfs.a \ $(top_builddir)/src/liblsd/liblsd.a \ $(builddir)/libdiod.a \ - $(top_builddir)/src/libtest/libtest.a \ $(top_builddir)/src/libtap/libtap.a \ $(LUA_LIB) \ $(LIBCAP) \ diff --git a/src/libdiod/test/directory.c b/src/libdiod/test/directory.c index 628d084e..c51e8b2f 100644 --- a/src/libdiod/test/directory.c +++ b/src/libdiod/test/directory.c @@ -13,32 +13,17 @@ #if HAVE_CONFIG_H #include "config.h" #endif -#include -#include +#include #include -#include -#include #include #include -#include -#include -#include - -#include -#include -#include -#include -#include "src/libnpfs/9p.h" -#include "src/libnpfs/npfs.h" +#include "src/libtest/server.h" #include "src/libnpclient/npclient.h" -#include "src/liblsd/list.h" #include "src/libtap/tap.h" -#include "diod_log.h" +#include "src/liblsd/list.h" #include "diod_conf.h" -#include "diod_sock.h" -#include "diod_ops.h" #define TEST_MSIZE 8192 @@ -48,7 +33,7 @@ int main (int argc, char *argv[]) { Npsrv *srv; - int s[2]; + int client_fd; int flags = 0; int rc; Npcfid *root, *dir, *f[TEST_ITER]; @@ -61,34 +46,15 @@ main (int argc, char *argv[]) plan (NO_PLAN); - diag ("initialize diod logging and configuration"); - diod_log_init (argv[0]); - diod_conf_init (); - diod_conf_set_auth_required (0); - /* export */ if (!mkdtemp (tmpdir)) BAIL_OUT ("mkdtemp: %s", strerror (errno)); - diag ("exporting %s", tmpdir); - diod_conf_add_exports (tmpdir); - diod_conf_set_exportopts ("sharefd"); - - if (socketpair (AF_LOCAL, SOCK_STREAM, 0, s) < 0) - BAIL_OUT ("socketpair: %s", strerror (errno)); - srv = np_srv_create (16, flags); - ok (srv != NULL, "np_srv_create works"); - if (!srv) - BAIL_OUT ("need server to continue"); - rc = diod_init (srv); - ok (rc == 0, "diod_init works"); - if (rc < 0) - BAIL_OUT ("diod_init: %s", strerror (np_rerror ())); + srv = test_server_create (tmpdir, flags, &client_fd); - diag ("connecting server to socketpair"); - diod_sock_startfd (srv, s[1], s[1], "loopback", 0); + diod_conf_set_exportopts ("sharefd"); - root = npc_mount (s[0], s[0], TEST_MSIZE, tmpdir, NULL); + root = npc_mount (client_fd, client_fd, TEST_MSIZE, tmpdir, NULL); ok (root != NULL, "npc_mount on socketpair works"); if (!root) BAIL_OUT ("npc_mount: %s", strerror (np_rerror ())); @@ -165,18 +131,10 @@ main (int argc, char *argv[]) diag ("npc_umount"); npc_umount (root); - diag ("npc_srv_wait_conncount"); - np_srv_wait_conncount (srv, 0); - - diag ("finalzing server"); - diod_fini (srv); - np_srv_destroy (srv); + test_server_destroy (srv); rmdir (tmpdir); - diod_conf_fini (); - diod_log_fini (); - done_testing (); exit (0); diff --git a/src/libdiod/test/multiuser.c b/src/libdiod/test/multiuser.c index 3074e74d..fca68d9c 100644 --- a/src/libdiod/test/multiuser.c +++ b/src/libdiod/test/multiuser.c @@ -13,37 +13,24 @@ #if HAVE_CONFIG_H #include "config.h" #endif -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include #include #include #include +#include +#include +#include +#include -#include "src/libnpfs/9p.h" -#include "src/libnpfs/npfs.h" +#include "src/libtest/server.h" #include "src/libnpclient/npclient.h" -#include "src/liblsd/list.h" #include "src/libtap/tap.h" -#include "diod_log.h" +#include "src/liblsd/list.h" #include "diod_conf.h" -#include "diod_sock.h" -#include "diod_ops.h" #define TEST_MSIZE 8192 -int -file_test (Npcfid *root) +int file_test (Npcfid *root) { int n, len = 4096*100; char *buf = malloc (len); @@ -121,11 +108,10 @@ file_test (Npcfid *root) return rc; } -int -main (int argc, char *argv[]) +int main (int argc, char *argv[]) { Npsrv *srv; - int s[2]; + int client_fd; int flags = 0; int rc; Npcfsys *fs; @@ -135,41 +121,23 @@ main (int argc, char *argv[]) if (geteuid () != 0 || getenv ("FAKEROOTKEY") != NULL) plan (SKIP_ALL, "this test must run as root"); - diag ("initialize diod logging and configuration"); - diod_log_init (argv[0]); - diod_conf_init (); - - /* Allow attach with afid==-1. - */ - diod_conf_set_auth_required (0); + plan (NO_PLAN); /* create export directory: will be owned by root, mode=0700 */ if (!mkdtemp (tmpdir)) BAIL_OUT ("mkdtemp: %s", strerror (errno)); - diod_conf_add_exports (tmpdir); - diod_conf_set_exportopts ("sharefd"); - - if (socketpair (AF_LOCAL, SOCK_STREAM, 0, s) < 0) - BAIL_OUT ("socketpair: %s", strerror (errno)); /* Note: supplementary groups do not work in this mode, however * regular uid:gid switching of fsid works. Enabling DAC_BYPASS * assumes v9fs is enforcing permissions, not the case with npclient. */ flags |= SRV_FLAGS_SETFSID; - srv = np_srv_create (16, flags); - ok (srv != NULL, "np_srv_create works"); - if (!srv) - BAIL_OUT ("need server to continue"); - rc = diod_init (srv); - ok (rc == 0, "diod_init works"); - if (rc < 0) - BAIL_OUT ("diod_init: %s", strerror (np_rerror ())); - diag ("connecting server to socketpair"); - diod_sock_startfd (srv, s[1], s[1], "loopback", 0); + srv = test_server_create (tmpdir, flags, &client_fd); + + diod_conf_set_exportopts ("sharefd"); - fs = npc_start (s[0], s[0], TEST_MSIZE, 0); + fs = npc_start (client_fd, client_fd, TEST_MSIZE, 0); ok (fs != NULL, "npc_start works"); if (!fs) BAIL_OUT ("npc_start: %s", strerror (np_rerror ())); @@ -219,18 +187,10 @@ main (int argc, char *argv[]) diag ("npc_finish"); npc_finish (fs); - diag ("npc_srv_wait_conncount"); - np_srv_wait_conncount (srv, 0); - - diag ("finalizing server"); - diod_fini (srv); - np_srv_destroy (srv); + test_server_destroy (srv); rmdir (tmpdir); - diod_conf_fini (); - diod_log_fini (); - done_testing (); exit (0); diff --git a/src/libdiod/test/read.c b/src/libdiod/test/read.c index d93877f5..25077656 100644 --- a/src/libdiod/test/read.c +++ b/src/libdiod/test/read.c @@ -13,32 +13,20 @@ #if HAVE_CONFIG_H #include "config.h" #endif -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include #include #include #include +#include +#include +#include +#include -#include "src/libnpfs/9p.h" -#include "src/libnpfs/npfs.h" +#include "src/libtest/server.h" #include "src/libnpclient/npclient.h" #include "src/libtap/tap.h" -#include "src/liblsd/list.h" -#include "diod_log.h" +#include "src/liblsd/list.h" #include "diod_conf.h" -#include "diod_sock.h" -#include "diod_ops.h" #define TEST_MSIZE 8192 @@ -48,7 +36,7 @@ int main (int argc, char *argv[]) { Npsrv *srv; - int s[2]; + int client_fd; int flags = 0; int rc; Npcfid *root, *f[TEST_ITER]; @@ -63,34 +51,15 @@ main (int argc, char *argv[]) if (!buf || !buf2) BAIL_OUT ("out of memory"); - diag ("initialize diod logging and configuration"); - diod_log_init (argv[0]); - diod_conf_init (); - diod_conf_set_auth_required (0); - /* export */ if (!mkdtemp (tmpdir)) BAIL_OUT ("mkdtemp: %s", strerror (errno)); - diag ("exporting %s", tmpdir); - diod_conf_add_exports (tmpdir); - diod_conf_set_exportopts ("sharefd"); - if (socketpair (AF_LOCAL, SOCK_STREAM, 0, s) < 0) - BAIL_OUT ("socketpair: %s", strerror (errno)); + srv = test_server_create (tmpdir, flags, &client_fd); - srv = np_srv_create (16, flags); - ok (srv != NULL, "np_srv_create works"); - if (!srv) - BAIL_OUT ("need server to continue"); - rc = diod_init (srv); - ok (rc == 0, "diod_init works"); - if (rc < 0) - BAIL_OUT ("diod_init: %s", strerror (np_rerror ())); - - diag ("connecting server to socketpair"); - diod_sock_startfd (srv, s[1], s[1], "loopback", 0); + diod_conf_set_exportopts ("sharefd"); - root = npc_mount (s[0], s[0], TEST_MSIZE, tmpdir, NULL); + root = npc_mount (client_fd, client_fd, TEST_MSIZE, tmpdir, NULL); ok (root != NULL, "npc_mount on socketpair works"); if (!root) BAIL_OUT ("npc_mount: %s", strerror (np_rerror ())); @@ -184,18 +153,10 @@ main (int argc, char *argv[]) diag ("npc_umount"); npc_umount (root); - diag ("npc_srv_wait_conncount"); - np_srv_wait_conncount (srv, 0); - - diag ("finalizing server"); - diod_fini (srv); - np_srv_destroy (srv); + test_server_destroy (srv); rmdir (tmpdir); - diod_conf_fini (); - diod_log_fini (); - done_testing (); exit (0); diff --git a/src/libtest/Makefile.am b/src/libtest/Makefile.am index baec4c78..17059bf8 100644 --- a/src/libtest/Makefile.am +++ b/src/libtest/Makefile.am @@ -9,4 +9,6 @@ libtest_a_SOURCES = \ thread.c \ thread.h \ state.c \ - state.h + state.h \ + server.c \ + server.h diff --git a/src/libtest/server.c b/src/libtest/server.c new file mode 100644 index 00000000..ceddce6b --- /dev/null +++ b/src/libtest/server.c @@ -0,0 +1,70 @@ +/************************************************************\ + * Copyright 2025 Lawrence Livermore National Security, LLC + * (c.f. AUTHORS, NOTICE.LLNS, COPYING) + * + * This file is part of the diod 9P server project. + * For details, see https://github.com/chaos/diod. + * + * SPDX-License-Identifier: GPL-2.0-or-later +\************************************************************/ + +#if HAVE_CONFIG_H +#include "config.h" +#endif +#include +#include +#include +#include +#include + +#include "server.h" + +#include "src/liblsd/list.h" +#include "src/libdiod/diod_log.h" +#include "src/libdiod/diod_conf.h" +#include "src/libdiod/diod_ops.h" +#include "src/libdiod/diod_sock.h" +#include "src/libtap/tap.h" + +Npsrv *test_server_create (const char *testdir, int flags, int *client_fd) +{ + int s[2]; + Npsrv *srv; + + diod_log_init ("#"); // add TAP compatible prefix on stderr logs + diod_conf_init (); + diod_conf_set_auth_required (0); + + if (testdir) + diod_conf_add_exports ((char *)testdir); + + if (socketpair (AF_LOCAL, SOCK_STREAM, 0, s) < 0) + BAIL_OUT ("socketpair: %s", strerror (errno)); + + if (!(srv = np_srv_create (16, flags))) + BAIL_OUT ("np_srv_create failed"); + + if (diod_init (srv) < 0) + BAIL_OUT ("diod_init: %s", strerror (np_rerror ())); + + diod_sock_startfd (srv, s[1], s[1], "simple-test-client", flags); + + diag ("serving %s on fd %d", testdir, s[0]); + + *client_fd = s[0]; + return srv; +} + +void test_server_destroy (Npsrv *srv) +{ + diag ("waiting for client to finish"); + np_srv_wait_conncount (srv, 0); + + diod_fini (srv); + np_srv_destroy (srv); + + diod_conf_fini (); + diod_log_fini (); +} + +// vi:ts=4 sw=4 expandtab diff --git a/src/libtest/server.h b/src/libtest/server.h new file mode 100644 index 00000000..0e913743 --- /dev/null +++ b/src/libtest/server.h @@ -0,0 +1,34 @@ +/************************************************************\ + * Copyright 2025 Lawrence Livermore National Security, LLC + * (c.f. AUTHORS, NOTICE.LLNS, COPYING) + * + * This file is part of the diod 9P server project. + * For details, see https://github.com/chaos/diod. + * + * SPDX-License-Identifier: GPL-2.0-or-later +\************************************************************/ + +#ifndef LIBTEST_SERVER_H +#define LIBTEST_SERVER_H + +#include +#include "src/libnpfs/9p.h" +#include "src/libnpfs/npfs.h" + +/* Embed a diod server in a test program that uses libtap, + * initializing diod logging and configuration. + * + * 'testdir' is a directory to exported by the server (may be NULL) + * 'client_fd' is set to the client side file descriptor (for libnpfs) + * 'flags' is a mask of SRV_FLAGS_* from libnpfs/npfs.h. + */ +Npsrv *test_server_create (const char *testdir, int flags, int *client_fd); + +/* Wait for clients to finalize, then destroy the server and finalize + * diod logging and configuration. + */ +void test_server_destroy (Npsrv *srv); + +#endif + +// vi:ts=4 sw=4 expandtab From cb0c9dc4de18068947756adfb62f3143b43c3881 Mon Sep 17 00:00:00 2001 From: Jim Garlick Date: Wed, 15 Jan 2025 08:47:36 -0800 Subject: [PATCH 4/6] add header guards Problem: header guards are missing from internal header files, creating an occasional nuisance. Add them. --- src/cmd/opt.h | 4 ++++ src/cmd/sample.h | 5 +++++ src/libdiod/diod_auth.h | 5 +++++ src/libdiod/diod_conf.h | 5 +++++ src/libdiod/diod_exp.h | 5 +++++ src/libdiod/diod_fid.h | 5 +++++ src/libdiod/diod_ioctx.h | 5 +++++ src/libdiod/diod_log.h | 5 +++++ src/libdiod/diod_ops.h | 5 +++++ src/libdiod/diod_rdma.h | 5 +++++ src/libdiod/diod_sock.h | 5 +++++ src/libdiod/diod_xattr.h | 5 ++++- src/libnpclient/npcimpl.h | 5 +++++ src/libnpclient/npclient.h | 5 +++++ src/libnpfs/npfs.h | 5 +++++ src/libnpfs/npfsimpl.h | 4 ++++ src/libnpfs/xpthread.h | 5 +++++ 17 files changed, 82 insertions(+), 1 deletion(-) diff --git a/src/cmd/opt.h b/src/cmd/opt.h index 5923ef7f..9c39bee6 100644 --- a/src/cmd/opt.h +++ b/src/cmd/opt.h @@ -8,6 +8,9 @@ * SPDX-License-Identifier: GPL-2.0-or-later \************************************************************/ +#ifndef CMD_OPT_H +#define CMD_OPT_H + typedef struct opt_struct *Opt; Opt opt_create (void); @@ -28,6 +31,7 @@ int opt_scanf (Opt o, const char *fmt, ...) int opt_check_allowed_csv (Opt o, const char *s); +#endif /* * vi:tabstop=4 shiftwidth=4 expandtab diff --git a/src/cmd/sample.h b/src/cmd/sample.h index 413fbc45..61d08d71 100644 --- a/src/cmd/sample.h +++ b/src/cmd/sample.h @@ -8,6 +8,9 @@ * SPDX-License-Identifier: GPL-2.0-or-later \************************************************************/ +#ifndef CMD_SAMPLE_H +#define CMD_SAMPLE_H + typedef struct sample_struct *sample_t; sample_t sample_create (int stale_secs); @@ -27,6 +30,8 @@ double sample_val (sample_t s, time_t tnow); int sample_val_cmp (sample_t s1, sample_t s2, time_t tnow); int sample_rate_cmp (sample_t s1, sample_t s2, time_t tnow); +#endif + /* * vi:tabstop=4 shiftwidth=4 expandtab */ diff --git a/src/libdiod/diod_auth.h b/src/libdiod/diod_auth.h index 1e9df019..5a78a0c7 100644 --- a/src/libdiod/diod_auth.h +++ b/src/libdiod/diod_auth.h @@ -8,8 +8,13 @@ * SPDX-License-Identifier: GPL-2.0-or-later \************************************************************/ +#ifndef _LIBDIOD_DIOD_AUTH_H +#define _LIBDIOD_DIOD_AUTH_H + extern Npauth *diod_auth_functions; struct Npcfid; int diod_auth (struct Npcfid *afid, u32 uid); + +#endif diff --git a/src/libdiod/diod_conf.h b/src/libdiod/diod_conf.h index 5a7e80b8..4a8ffb7e 100644 --- a/src/libdiod/diod_conf.h +++ b/src/libdiod/diod_conf.h @@ -8,6 +8,9 @@ * SPDX-License-Identifier: GPL-2.0-or-later \************************************************************/ +#ifndef LIBDIOD_DIOD_CONF_H +#define LIBDIOD_DIOD_CONF_H + #define DFLT_DEBUGLEVEL 0 #define DFLT_NWTHREADS 16 #define DFLT_MAXMMAP 0 @@ -111,6 +114,8 @@ char *diod_conf_get_exportopts(void); int diod_conf_opt_exportopts(void); void diod_conf_set_exportopts(char *opts); +#endif + /* * vi:tabstop=4 shiftwidth=4 expandtab */ diff --git a/src/libdiod/diod_exp.h b/src/libdiod/diod_exp.h index fe760f82..a2a3ae8f 100644 --- a/src/libdiod/diod_exp.h +++ b/src/libdiod/diod_exp.h @@ -8,6 +8,11 @@ * SPDX-License-Identifier: GPL-2.0-or-later \************************************************************/ +#ifndef LIBDIOD_DIOD_EXP_H +#define LIBDIOD_DIOD_EXP_H + int diod_fetch_xflags (Npstr *aname, int *xfp); int diod_match_exports (char *path, Npconn *conn, Npuser *user, int *xfp); char *diod_get_exports (char *name, void *a); + +#endif diff --git a/src/libdiod/diod_fid.h b/src/libdiod/diod_fid.h index b436384a..d5d60220 100644 --- a/src/libdiod/diod_fid.h +++ b/src/libdiod/diod_fid.h @@ -8,6 +8,9 @@ * SPDX-License-Identifier: GPL-2.0-or-later \************************************************************/ +#ifndef LIBDIOD_DIOD_FID_H +#define LIBDIOD_DIOD_FID_H + #define DIOD_FID_FLAGS_ROFS 0x01 #define DIOD_FID_FLAGS_MOUNTPT 0x02 #define DIOD_FID_FLAGS_SHAREFD 0x04 @@ -24,6 +27,8 @@ Fid *diod_fidalloc (Npfid *fid, Npstr *ns); Fid *diod_fidclone (Npfid *newfid, Npfid *fid); void diod_fiddestroy (Npfid *fid); +#endif + /* * vi:tabstop=4 shiftwidth=4 expandtab */ diff --git a/src/libdiod/diod_ioctx.h b/src/libdiod/diod_ioctx.h index 74e08ccf..27bcfe4e 100644 --- a/src/libdiod/diod_ioctx.h +++ b/src/libdiod/diod_ioctx.h @@ -8,6 +8,9 @@ * SPDX-License-Identifier: GPL-2.0-or-later \************************************************************/ +#ifndef LIBDIOD_DIOD_IOCTX_H +#define LIBDIOD_DIOD_IOCTX_H + typedef struct path_struct *Path; typedef struct ioctx_struct *IOCtx; @@ -45,6 +48,8 @@ int ioctx_utimes (IOCtx ioctx, const utimbuf *times); u32 ioctx_iounit (IOCtx ioctx); Npqid *ioctx_qid (IOCtx ioctx); +#endif + /* * vi:tabstop=4 shiftwidth=4 expandtab */ diff --git a/src/libdiod/diod_log.h b/src/libdiod/diod_log.h index e33ace75..d2c5b0c6 100644 --- a/src/libdiod/diod_log.h +++ b/src/libdiod/diod_log.h @@ -8,6 +8,9 @@ * SPDX-License-Identifier: GPL-2.0-or-later \************************************************************/ +#ifndef LIBDIOD_DIOD_LOG_H +#define LIBDIOD_DIOD_LOG_H + #include void diod_log_init (char *p); @@ -29,6 +32,8 @@ void msg_exit (const char *fmt, ...) void msg (const char *fmt, ...) __attribute__ ((format (printf, 1, 2))); +#endif + /* * vi:tabstop=4 shiftwidth=4 expandtab */ diff --git a/src/libdiod/diod_ops.h b/src/libdiod/diod_ops.h index 855c9f9d..5cef55d1 100644 --- a/src/libdiod/diod_ops.h +++ b/src/libdiod/diod_ops.h @@ -8,11 +8,16 @@ * SPDX-License-Identifier: GPL-2.0-or-later \************************************************************/ +#ifndef LIBDIOD_DIOD_OPS_H +#define LIBDIOD_DIOD_OPS_H + int diod_init (Npsrv *srv); void diod_fini (Npsrv *srv); void diod_ustat2qid (struct stat *st, Npqid *qid); +#endif + /* * vi:tabstop=4 shiftwidth=4 expandtab */ diff --git a/src/libdiod/diod_rdma.h b/src/libdiod/diod_rdma.h index 627c9731..2a7be569 100644 --- a/src/libdiod/diod_rdma.h +++ b/src/libdiod/diod_rdma.h @@ -8,6 +8,9 @@ * SPDX-License-Identifier: GPL-2.0-or-later \************************************************************/ +#ifndef LIBDIOD_DIOD_RDMA_H +#define LIBDIOD_DIOD_RDMA_H + typedef struct diod_rdma_struct *diod_rdma_t; diod_rdma_t diod_rdma_create (void); @@ -16,6 +19,8 @@ void diod_rdma_accept_one (Npsrv *srv, diod_rdma_t rdma); void diod_rdma_shutdown (diod_rdma_t rdma); void diod_rdma_destroy (diod_rdma_t rdma); +#endif + /* * vi:tabstop=4 shiftwidth=4 expandtab */ diff --git a/src/libdiod/diod_sock.h b/src/libdiod/diod_sock.h index 8f6da7ab..45b5963a 100644 --- a/src/libdiod/diod_sock.h +++ b/src/libdiod/diod_sock.h @@ -8,6 +8,9 @@ * SPDX-License-Identifier: GPL-2.0-or-later \************************************************************/ +#ifndef LIBDIOD_DIOD_SOCK_H +#define LIBDIOD_DIOD_SOCK_H + struct pollfd; void diod_sock_accept_one (Npsrv *srv, int fd, int lookup); @@ -24,6 +27,8 @@ int diod_sock_connect (char *name, int flags); int diod_sock_connect_inet (char *host, char *port, int flags); int diod_sock_connect_unix (char *path, int flags); +#endif + /* * vi:tabstop=4 shiftwidth=4 expandtab */ diff --git a/src/libdiod/diod_xattr.h b/src/libdiod/diod_xattr.h index 8a767a95..22574568 100644 --- a/src/libdiod/diod_xattr.h +++ b/src/libdiod/diod_xattr.h @@ -8,6 +8,9 @@ * SPDX-License-Identifier: GPL-2.0-or-later \************************************************************/ +#ifndef LIBDIOD_DIOD_XATTR_H +#define LIBDIOD_DIOD_XATTR_H + typedef struct xattr_struct *Xattr; int xattr_open (Npfid *fid, Npstr *name, u64 *sizep); @@ -17,7 +20,7 @@ int xattr_close (Npfid *fid); int xattr_pread (Xattr xattr, void *buf, size_t count, off_t offset); int xattr_pwrite (Xattr xattr, void *buf, size_t count, off_t offset); - +#endif /* * vi:tabstop=4 shiftwidth=4 expandtab */ diff --git a/src/libnpclient/npcimpl.h b/src/libnpclient/npcimpl.h index 6a9a1f91..918dda32 100644 --- a/src/libnpclient/npcimpl.h +++ b/src/libnpclient/npcimpl.h @@ -8,6 +8,9 @@ * SPDX-License-Identifier: MIT *************************************************************/ +#ifndef LIBNPCLIENT_NPCIMPL_H +#define LIBNPCLIENT_NPCIMPL_H + typedef int (*RpcFun)(Npcfsys *fs, Npfcall *tc, Npfcall **rc); typedef void (*RefFun)(Npcfsys *fs); typedef void (*DiscFun)(Npcfsys *fs); @@ -80,3 +83,5 @@ void npc_put_id(Npcpool *p, u32 id); Npcfid *npc_fid_alloc(Npcfsys *fs); void npc_fid_free(Npcfid *fid); + +#endif diff --git a/src/libnpclient/npclient.h b/src/libnpclient/npclient.h index 9f98c7b0..73133eb8 100644 --- a/src/libnpclient/npclient.h +++ b/src/libnpclient/npclient.h @@ -8,6 +8,9 @@ * SPDX-License-Identifier: MIT *************************************************************/ +#ifndef LIBNPCLIENT_NPCLIENT_H +#define LIBNPCLIENT_NPCLIENT_H + typedef struct Npcfid Npcfid; typedef struct Npcfsys Npcfsys; @@ -278,3 +281,5 @@ ssize_t npc_getxattr (Npcfid *root, char *path, char *attr, char *buf, size_t size); int npc_setxattr (Npcfid *root, char *path, char *name, char *val, size_t size, int flags); + +#endif diff --git a/src/libnpfs/npfs.h b/src/libnpfs/npfs.h index eb78cc50..85a86abb 100644 --- a/src/libnpfs/npfs.h +++ b/src/libnpfs/npfs.h @@ -8,6 +8,9 @@ * SPDX-License-Identifier: MIT *************************************************************/ +#ifndef LIBNPFS_NPFS_H +#define LIBNPFS_NPFS_H + #include #include @@ -540,3 +543,5 @@ Npfile *np_ctl_addfile (Npfile *parent, char *name, SynGetF getf, void *arg, int flags); Npfile *np_ctl_adddir (Npfile *parent, char *name); void np_ctl_delfile (Npfile *file); + +#endif diff --git a/src/libnpfs/npfsimpl.h b/src/libnpfs/npfsimpl.h index b01d719f..ea166578 100644 --- a/src/libnpfs/npfsimpl.h +++ b/src/libnpfs/npfsimpl.h @@ -8,6 +8,9 @@ * SPDX-License-Identifier: MIT *************************************************************/ +#ifndef LIBNPFS_NPFSIMPL_H +#define LIBNPFS_NPFSIMPL_H + /* fcall.c */ Npfcall *np_version(Npreq *req, Npfcall *tc); Npfcall *np_auth(Npreq *req, Npfcall *tc); @@ -47,3 +50,4 @@ Npreq *np_req_alloc(Npconn *conn, Npfcall *tc); Npreq *np_req_ref(Npreq*); void np_req_unref(Npreq*); +#endif diff --git a/src/libnpfs/xpthread.h b/src/libnpfs/xpthread.h index 0027beee..9fd5489e 100644 --- a/src/libnpfs/xpthread.h +++ b/src/libnpfs/xpthread.h @@ -8,6 +8,9 @@ * SPDX-License-Identifier: MIT *************************************************************/ +#ifndef LIBNPFS_XPTHREAD_H +#define LIBNPFS_XPTHREAD_H + /* pthread wrappers */ #define xpthread_mutex_lock(a) do { \ int pthread_mutex_lock_result = pthread_mutex_lock(a); \ @@ -33,3 +36,5 @@ int pthread_cond_signal_result = pthread_cond_signal(a); \ NP_ASSERT (pthread_cond_signal_result == 0); \ } while (0) + +#endif From e057687122f63d82d233b62bf97efc919277b504 Mon Sep 17 00:00:00 2001 From: Jim Garlick Date: Wed, 15 Jan 2025 09:39:09 -0800 Subject: [PATCH 5/6] headers include the headers they depend on Problem: diod headers don't include their dependencies, so each source module requires a plethora of them. Now that there are header guards, headers may include their dependencies. --- src/libdiod/diod_auth.h | 2 ++ src/libdiod/diod_conf.h | 2 ++ src/libdiod/diod_exp.h | 2 ++ src/libdiod/diod_fid.h | 6 ++++++ src/libdiod/diod_ioctx.h | 3 +++ src/libdiod/diod_ops.h | 5 +++++ src/libdiod/diod_rdma.h | 2 ++ src/libdiod/diod_sock.h | 4 ++++ src/libdiod/diod_xattr.h | 2 ++ src/libnpclient/npcimpl.h | 2 ++ src/libnpclient/npclient.h | 2 ++ src/libnpfs/9p.h | 2 ++ src/libnpfs/npfs.h | 1 + src/libnpfs/npfsimpl.h | 2 ++ src/libnpfs/xpthread.h | 2 ++ src/libtest/server.h | 2 -- src/libtest/state.h | 2 -- 17 files changed, 39 insertions(+), 4 deletions(-) diff --git a/src/libdiod/diod_auth.h b/src/libdiod/diod_auth.h index 5a78a0c7..939a36f4 100644 --- a/src/libdiod/diod_auth.h +++ b/src/libdiod/diod_auth.h @@ -11,6 +11,8 @@ #ifndef _LIBDIOD_DIOD_AUTH_H #define _LIBDIOD_DIOD_AUTH_H +#include "src/libnpfs/npfs.h" + extern Npauth *diod_auth_functions; struct Npcfid; diff --git a/src/libdiod/diod_conf.h b/src/libdiod/diod_conf.h index 4a8ffb7e..78106cf1 100644 --- a/src/libdiod/diod_conf.h +++ b/src/libdiod/diod_conf.h @@ -11,6 +11,8 @@ #ifndef LIBDIOD_DIOD_CONF_H #define LIBDIOD_DIOD_CONF_H +#include "src/liblsd/list.h" + #define DFLT_DEBUGLEVEL 0 #define DFLT_NWTHREADS 16 #define DFLT_MAXMMAP 0 diff --git a/src/libdiod/diod_exp.h b/src/libdiod/diod_exp.h index a2a3ae8f..fb03ca2b 100644 --- a/src/libdiod/diod_exp.h +++ b/src/libdiod/diod_exp.h @@ -11,6 +11,8 @@ #ifndef LIBDIOD_DIOD_EXP_H #define LIBDIOD_DIOD_EXP_H +#include "src/libnpfs/npfs.h" + int diod_fetch_xflags (Npstr *aname, int *xfp); int diod_match_exports (char *path, Npconn *conn, Npuser *user, int *xfp); char *diod_get_exports (char *name, void *a); diff --git a/src/libdiod/diod_fid.h b/src/libdiod/diod_fid.h index d5d60220..f064a893 100644 --- a/src/libdiod/diod_fid.h +++ b/src/libdiod/diod_fid.h @@ -11,6 +11,12 @@ #ifndef LIBDIOD_DIOD_FID_H #define LIBDIOD_DIOD_FID_H +#include "src/libnpfs/npfs.h" + +#include "diod_xattr.h" +#include "diod_fid.h" +#include "diod_ioctx.h" + #define DIOD_FID_FLAGS_ROFS 0x01 #define DIOD_FID_FLAGS_MOUNTPT 0x02 #define DIOD_FID_FLAGS_SHAREFD 0x04 diff --git a/src/libdiod/diod_ioctx.h b/src/libdiod/diod_ioctx.h index 27bcfe4e..55ab6177 100644 --- a/src/libdiod/diod_ioctx.h +++ b/src/libdiod/diod_ioctx.h @@ -11,6 +11,9 @@ #ifndef LIBDIOD_DIOD_IOCTX_H #define LIBDIOD_DIOD_IOCTX_H +#include +#include "src/libnpfs/npfs.h" + typedef struct path_struct *Path; typedef struct ioctx_struct *IOCtx; diff --git a/src/libdiod/diod_ops.h b/src/libdiod/diod_ops.h index 5cef55d1..c916ab89 100644 --- a/src/libdiod/diod_ops.h +++ b/src/libdiod/diod_ops.h @@ -11,6 +11,11 @@ #ifndef LIBDIOD_DIOD_OPS_H #define LIBDIOD_DIOD_OPS_H +#include +#include + +#include "src/libnpfs/npfs.h" + int diod_init (Npsrv *srv); void diod_fini (Npsrv *srv); diff --git a/src/libdiod/diod_rdma.h b/src/libdiod/diod_rdma.h index 2a7be569..b781de94 100644 --- a/src/libdiod/diod_rdma.h +++ b/src/libdiod/diod_rdma.h @@ -11,6 +11,8 @@ #ifndef LIBDIOD_DIOD_RDMA_H #define LIBDIOD_DIOD_RDMA_H +#include "src/libnpfs/npfs.h" + typedef struct diod_rdma_struct *diod_rdma_t; diod_rdma_t diod_rdma_create (void); diff --git a/src/libdiod/diod_sock.h b/src/libdiod/diod_sock.h index 45b5963a..c20ea034 100644 --- a/src/libdiod/diod_sock.h +++ b/src/libdiod/diod_sock.h @@ -11,6 +11,10 @@ #ifndef LIBDIOD_DIOD_SOCK_H #define LIBDIOD_DIOD_SOCK_H +#include +#include "src/libnpfs/npfs.h" +#include "src/liblsd/list.h" + struct pollfd; void diod_sock_accept_one (Npsrv *srv, int fd, int lookup); diff --git a/src/libdiod/diod_xattr.h b/src/libdiod/diod_xattr.h index 22574568..556d28bc 100644 --- a/src/libdiod/diod_xattr.h +++ b/src/libdiod/diod_xattr.h @@ -11,6 +11,8 @@ #ifndef LIBDIOD_DIOD_XATTR_H #define LIBDIOD_DIOD_XATTR_H +#include "src/libnpfs/npfs.h" + typedef struct xattr_struct *Xattr; int xattr_open (Npfid *fid, Npstr *name, u64 *sizep); diff --git a/src/libnpclient/npcimpl.h b/src/libnpclient/npcimpl.h index 918dda32..234bd947 100644 --- a/src/libnpclient/npcimpl.h +++ b/src/libnpclient/npcimpl.h @@ -11,6 +11,8 @@ #ifndef LIBNPCLIENT_NPCIMPL_H #define LIBNPCLIENT_NPCIMPL_H +#include "src/libnpfs/npfs.h" + typedef int (*RpcFun)(Npcfsys *fs, Npfcall *tc, Npfcall **rc); typedef void (*RefFun)(Npcfsys *fs); typedef void (*DiscFun)(Npcfsys *fs); diff --git a/src/libnpclient/npclient.h b/src/libnpclient/npclient.h index 73133eb8..40bb7edb 100644 --- a/src/libnpclient/npclient.h +++ b/src/libnpclient/npclient.h @@ -11,6 +11,8 @@ #ifndef LIBNPCLIENT_NPCLIENT_H #define LIBNPCLIENT_NPCLIENT_H +#include "src/libnpfs/9p.h" + typedef struct Npcfid Npcfid; typedef struct Npcfsys Npcfsys; diff --git a/src/libnpfs/9p.h b/src/libnpfs/9p.h index 7912c642..fb014dc5 100644 --- a/src/libnpfs/9p.h +++ b/src/libnpfs/9p.h @@ -16,6 +16,8 @@ #ifndef NET_9P_H #define NET_9P_H +#include + #ifndef V9FS_MAGIC #define V9FS_MAGIC 0x01021997 #endif diff --git a/src/libnpfs/npfs.h b/src/libnpfs/npfs.h index 85a86abb..413472de 100644 --- a/src/libnpfs/npfs.h +++ b/src/libnpfs/npfs.h @@ -13,6 +13,7 @@ #include #include +#include "9p.h" typedef struct p9_str Npstr; typedef struct p9_qid Npqid; diff --git a/src/libnpfs/npfsimpl.h b/src/libnpfs/npfsimpl.h index ea166578..9afd1248 100644 --- a/src/libnpfs/npfsimpl.h +++ b/src/libnpfs/npfsimpl.h @@ -11,6 +11,8 @@ #ifndef LIBNPFS_NPFSIMPL_H #define LIBNPFS_NPFSIMPL_H +#include "npfs.h" + /* fcall.c */ Npfcall *np_version(Npreq *req, Npfcall *tc); Npfcall *np_auth(Npreq *req, Npfcall *tc); diff --git a/src/libnpfs/xpthread.h b/src/libnpfs/xpthread.h index 9fd5489e..b7f61f07 100644 --- a/src/libnpfs/xpthread.h +++ b/src/libnpfs/xpthread.h @@ -11,6 +11,8 @@ #ifndef LIBNPFS_XPTHREAD_H #define LIBNPFS_XPTHREAD_H +#include + /* pthread wrappers */ #define xpthread_mutex_lock(a) do { \ int pthread_mutex_lock_result = pthread_mutex_lock(a); \ diff --git a/src/libtest/server.h b/src/libtest/server.h index 0e913743..c59b9ed4 100644 --- a/src/libtest/server.h +++ b/src/libtest/server.h @@ -11,8 +11,6 @@ #ifndef LIBTEST_SERVER_H #define LIBTEST_SERVER_H -#include -#include "src/libnpfs/9p.h" #include "src/libnpfs/npfs.h" /* Embed a diod server in a test program that uses libtap, diff --git a/src/libtest/state.h b/src/libtest/state.h index 3f2b6b0c..b7541b4f 100644 --- a/src/libtest/state.h +++ b/src/libtest/state.h @@ -11,8 +11,6 @@ #ifndef _LIBTEST_STATE_H #define _LIBTEST_STATE_H -#include - void test_state_init (int s); void test_state_change (int s); void test_state_wait (int s); From b66c8a9e7874183bcacb331ff4c7471b3117c9a2 Mon Sep 17 00:00:00 2001 From: Jim Garlick Date: Wed, 15 Jan 2025 10:01:34 -0800 Subject: [PATCH 6/6] testsuite: reduce unnecessary include directives Problem: tests have a lot of extra includes. Pare these down to the minimum required. --- src/libdiod/test/configfile.c | 6 +----- src/libdiod/test/directory.c | 1 - src/libdiod/test/multiuser.c | 1 - src/libnpfs/test/capability.c | 16 ++-------------- src/libnpfs/test/encoding.c | 15 +-------------- src/libnpfs/test/fidpool.c | 13 +------------ src/libnpfs/test/setfsuid.c | 12 ++---------- src/libnpfs/test/setgroups.c | 4 ---- src/libnpfs/test/setreuid.c | 12 ------------ src/libtest/server.c | 8 +++----- 10 files changed, 10 insertions(+), 78 deletions(-) diff --git a/src/libdiod/test/configfile.c b/src/libdiod/test/configfile.c index 493292ef..2a9b1d43 100644 --- a/src/libdiod/test/configfile.c +++ b/src/libdiod/test/configfile.c @@ -13,12 +13,8 @@ #if HAVE_CONFIG_H #include "config.h" #endif -#include -#include -#include -#include -#include #include +#include #include "src/liblsd/list.h" #include "src/libtap/tap.h" diff --git a/src/libdiod/test/directory.c b/src/libdiod/test/directory.c index c51e8b2f..d5b479de 100644 --- a/src/libdiod/test/directory.c +++ b/src/libdiod/test/directory.c @@ -22,7 +22,6 @@ #include "src/libnpclient/npclient.h" #include "src/libtap/tap.h" -#include "src/liblsd/list.h" #include "diod_conf.h" #define TEST_MSIZE 8192 diff --git a/src/libdiod/test/multiuser.c b/src/libdiod/test/multiuser.c index fca68d9c..79b41989 100644 --- a/src/libdiod/test/multiuser.c +++ b/src/libdiod/test/multiuser.c @@ -25,7 +25,6 @@ #include "src/libnpclient/npclient.h" #include "src/libtap/tap.h" -#include "src/liblsd/list.h" #include "diod_conf.h" #define TEST_MSIZE 8192 diff --git a/src/libnpfs/test/capability.c b/src/libnpfs/test/capability.c index b0e5d6bf..55e643c4 100644 --- a/src/libnpfs/test/capability.c +++ b/src/libnpfs/test/capability.c @@ -12,24 +12,12 @@ #if HAVE_CONFIG_H #include "config.h" #endif -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include #if HAVE_LIBCAP #include #endif -#include +#include +#include -#include "src/libdiod/diod_log.h" #include "src/libtest/thread.h" #include "src/libtest/state.h" #include "src/libtap/tap.h" diff --git a/src/libnpfs/test/encoding.c b/src/libnpfs/test/encoding.c index 313e0908..1ed799e8 100644 --- a/src/libnpfs/test/encoding.c +++ b/src/libnpfs/test/encoding.c @@ -10,21 +10,8 @@ #if HAVE_CONFIG_H #include "config.h" #endif -#include -#include #include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "9p.h" + #include "npfs.h" #include "src/libtap/tap.h" diff --git a/src/libnpfs/test/fidpool.c b/src/libnpfs/test/fidpool.c index d83a2fab..40870a67 100644 --- a/src/libnpfs/test/fidpool.c +++ b/src/libnpfs/test/fidpool.c @@ -10,20 +10,9 @@ #if HAVE_CONFIG_H #include "config.h" #endif -#include -#include -#include -#include -#include #include -#include -#include -#include +#include "npfs.h" -#include "src/libnpfs/9p.h" -#include "src/libnpfs/npfs.h" - -#include "src/liblsd/list.h" #include "src/libtap/tap.h" diff --git a/src/libnpfs/test/setfsuid.c b/src/libnpfs/test/setfsuid.c index 27da3df4..d2100cde 100644 --- a/src/libnpfs/test/setfsuid.c +++ b/src/libnpfs/test/setfsuid.c @@ -19,19 +19,11 @@ #if HAVE_CONFIG_H #include "config.h" #endif -#include -#include #include #include -#include -#include -#include -#include -#include -#include -#include -#include #include +#include +#include #include "src/libtest/thread.h" #include "src/libtest/state.h" diff --git a/src/libnpfs/test/setgroups.c b/src/libnpfs/test/setgroups.c index 2464d685..58004c37 100644 --- a/src/libnpfs/test/setgroups.c +++ b/src/libnpfs/test/setgroups.c @@ -22,13 +22,9 @@ #include #include #include -#include -#include #include #include -#include #include -#include #include #include diff --git a/src/libnpfs/test/setreuid.c b/src/libnpfs/test/setreuid.c index 94a4eae2..cf7d4076 100644 --- a/src/libnpfs/test/setreuid.c +++ b/src/libnpfs/test/setreuid.c @@ -17,18 +17,6 @@ #if HAVE_CONFIG_H #include "config.h" #endif -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include #include "src/libtest/thread.h" #include "src/libtest/state.h" diff --git a/src/libtest/server.c b/src/libtest/server.c index ceddce6b..f28af178 100644 --- a/src/libtest/server.c +++ b/src/libtest/server.c @@ -12,20 +12,18 @@ #include "config.h" #endif #include -#include #include #include -#include -#include "server.h" - -#include "src/liblsd/list.h" #include "src/libdiod/diod_log.h" #include "src/libdiod/diod_conf.h" #include "src/libdiod/diod_ops.h" #include "src/libdiod/diod_sock.h" +#include "src/libnpfs/npfs.h" #include "src/libtap/tap.h" +#include "server.h" + Npsrv *test_server_create (const char *testdir, int flags, int *client_fd) { int s[2];