diff --git a/configure.ac b/configure.ac index 87cc0fd9..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 \ @@ -231,7 +232,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..4980312b 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,8 +1,8 @@ SUBDIRS = \ libtap \ + libtest \ liblsd \ 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/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/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..6da66df9 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 @@ -22,6 +32,7 @@ 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 \ @@ -30,10 +41,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 +59,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/libdiod/diod_auth.h b/src/libdiod/diod_auth.h index 1e9df019..939a36f4 100644 --- a/src/libdiod/diod_auth.h +++ b/src/libdiod/diod_auth.h @@ -8,8 +8,15 @@ * SPDX-License-Identifier: GPL-2.0-or-later \************************************************************/ +#ifndef _LIBDIOD_DIOD_AUTH_H +#define _LIBDIOD_DIOD_AUTH_H + +#include "src/libnpfs/npfs.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..78106cf1 100644 --- a/src/libdiod/diod_conf.h +++ b/src/libdiod/diod_conf.h @@ -8,6 +8,11 @@ * SPDX-License-Identifier: GPL-2.0-or-later \************************************************************/ +#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 @@ -111,6 +116,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/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 85% rename from src/daemon/exp.h rename to src/libdiod/diod_exp.h index fe760f82..fb03ca2b 100644 --- a/src/daemon/exp.h +++ b/src/libdiod/diod_exp.h @@ -8,6 +8,13 @@ * SPDX-License-Identifier: GPL-2.0-or-later \************************************************************/ +#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); + +#endif 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 83% rename from src/daemon/fid.h rename to src/libdiod/diod_fid.h index b436384a..f064a893 100644 --- a/src/daemon/fid.h +++ b/src/libdiod/diod_fid.h @@ -8,6 +8,15 @@ * SPDX-License-Identifier: GPL-2.0-or-later \************************************************************/ +#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 @@ -24,6 +33,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/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 93% rename from src/daemon/ioctx.h rename to src/libdiod/diod_ioctx.h index 74e08ccf..55ab6177 100644 --- a/src/daemon/ioctx.h +++ b/src/libdiod/diod_ioctx.h @@ -8,6 +8,12 @@ * SPDX-License-Identifier: GPL-2.0-or-later \************************************************************/ +#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; @@ -45,6 +51,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/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 79% rename from src/daemon/ops.h rename to src/libdiod/diod_ops.h index 855c9f9d..c916ab89 100644 --- a/src/daemon/ops.h +++ b/src/libdiod/diod_ops.h @@ -8,11 +8,21 @@ * SPDX-License-Identifier: GPL-2.0-or-later \************************************************************/ +#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); 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..b781de94 100644 --- a/src/libdiod/diod_rdma.h +++ b/src/libdiod/diod_rdma.h @@ -8,6 +8,11 @@ * SPDX-License-Identifier: GPL-2.0-or-later \************************************************************/ +#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); @@ -16,6 +21,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..c20ea034 100644 --- a/src/libdiod/diod_sock.h +++ b/src/libdiod/diod_sock.h @@ -8,6 +8,13 @@ * SPDX-License-Identifier: GPL-2.0-or-later \************************************************************/ +#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); @@ -24,6 +31,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/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 88% rename from src/daemon/xattr.h rename to src/libdiod/diod_xattr.h index 8a767a95..556d28bc 100644 --- a/src/daemon/xattr.h +++ b/src/libdiod/diod_xattr.h @@ -8,6 +8,11 @@ * SPDX-License-Identifier: GPL-2.0-or-later \************************************************************/ +#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); @@ -17,7 +22,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/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/daemon/test/directory.c b/src/libdiod/test/directory.c similarity index 72% rename from src/daemon/test/directory.c rename to src/libdiod/test/directory.c index 95263fb7..d5b479de 100644 --- a/src/daemon/test/directory.c +++ b/src/libdiod/test/directory.c @@ -13,34 +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/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_conf.h" + #define TEST_MSIZE 8192 #define TEST_ITER 64 @@ -49,7 +32,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]; @@ -62,34 +45,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 ())); @@ -166,18 +130,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/daemon/test/multiuser.c b/src/libdiod/test/multiuser.c similarity index 78% rename from src/daemon/test/multiuser.c rename to src/libdiod/test/multiuser.c index e0116852..79b41989 100644 --- a/src/daemon/test/multiuser.c +++ b/src/libdiod/test/multiuser.c @@ -13,39 +13,23 @@ #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/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_conf.h" #define TEST_MSIZE 8192 -int -file_test (Npcfid *root) +int file_test (Npcfid *root) { int n, len = 4096*100; char *buf = malloc (len); @@ -123,11 +107,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; @@ -137,41 +120,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 ())); @@ -221,18 +186,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/daemon/test/read.c b/src/libdiod/test/read.c similarity index 76% rename from src/daemon/test/read.c rename to src/libdiod/test/read.c index fc860dff..25077656 100644 --- a/src/daemon/test/read.c +++ b/src/libdiod/test/read.c @@ -13,33 +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 "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_conf.h" #define TEST_MSIZE 8192 @@ -49,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]; @@ -64,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 ())); @@ -185,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/daemon/test/threadwrlock.c b/src/libdiod/test/threadwrlock.c similarity index 76% rename from src/daemon/test/threadwrlock.c rename to src/libdiod/test/threadwrlock.c index 3ad66e48..244ec233 100644 --- a/src/daemon/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/libnpclient/npcimpl.h b/src/libnpclient/npcimpl.h index 6a9a1f91..234bd947 100644 --- a/src/libnpclient/npcimpl.h +++ b/src/libnpclient/npcimpl.h @@ -8,6 +8,11 @@ * SPDX-License-Identifier: MIT *************************************************************/ +#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); @@ -80,3 +85,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..40bb7edb 100644 --- a/src/libnpclient/npclient.h +++ b/src/libnpclient/npclient.h @@ -8,6 +8,11 @@ * SPDX-License-Identifier: MIT *************************************************************/ +#ifndef LIBNPCLIENT_NPCLIENT_H +#define LIBNPCLIENT_NPCLIENT_H + +#include "src/libnpfs/9p.h" + typedef struct Npcfid Npcfid; typedef struct Npcfsys Npcfsys; @@ -278,3 +283,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/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/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/npfs.h b/src/libnpfs/npfs.h index eb78cc50..413472de 100644 --- a/src/libnpfs/npfs.h +++ b/src/libnpfs/npfs.h @@ -8,8 +8,12 @@ * SPDX-License-Identifier: MIT *************************************************************/ +#ifndef LIBNPFS_NPFS_H +#define LIBNPFS_NPFS_H + #include #include +#include "9p.h" typedef struct p9_str Npstr; typedef struct p9_qid Npqid; @@ -540,3 +544,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..9afd1248 100644 --- a/src/libnpfs/npfsimpl.h +++ b/src/libnpfs/npfsimpl.h @@ -8,6 +8,11 @@ * SPDX-License-Identifier: MIT *************************************************************/ +#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); @@ -47,3 +52,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/test/capability.c b/src/libnpfs/test/capability.c index b98a569a..55e643c4 100644 --- a/src/libnpfs/test/capability.c +++ b/src/libnpfs/test/capability.c @@ -12,70 +12,19 @@ #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" #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 +60,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 +89,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 +164,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/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 bceb2f93..d2100cde 100644 --- a/src/libnpfs/test/setfsuid.c +++ b/src/libnpfs/test/setfsuid.c @@ -19,20 +19,14 @@ #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" #include "src/libtap/tap.h" #define TEST_UID 100 @@ -40,47 +34,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 +75,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 +111,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..58004c37 100644 --- a/src/libnpfs/test/setgroups.c +++ b/src/libnpfs/test/setgroups.c @@ -22,16 +22,14 @@ #include #include #include -#include -#include #include #include -#include #include -#include #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 +147,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 +175,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 +194,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 +213,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..cf7d4076 100644 --- a/src/libnpfs/test/setreuid.c +++ b/src/libnpfs/test/setreuid.c @@ -17,112 +17,45 @@ #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" #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 +68,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/libnpfs/xpthread.h b/src/libnpfs/xpthread.h index 0027beee..b7f61f07 100644 --- a/src/libnpfs/xpthread.h +++ b/src/libnpfs/xpthread.h @@ -8,6 +8,11 @@ * SPDX-License-Identifier: MIT *************************************************************/ +#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); \ @@ -33,3 +38,5 @@ int pthread_cond_signal_result = pthread_cond_signal(a); \ NP_ASSERT (pthread_cond_signal_result == 0); \ } while (0) + +#endif diff --git a/src/libtest/Makefile.am b/src/libtest/Makefile.am new file mode 100644 index 00000000..17059bf8 --- /dev/null +++ b/src/libtest/Makefile.am @@ -0,0 +1,14 @@ +AM_CFLAGS = @WARNING_CFLAGS@ + +AM_CPPFLAGS = \ + -I$(top_srcdir) + +check_LIBRARIES = libtest.a + +libtest_a_SOURCES = \ + thread.c \ + thread.h \ + state.c \ + state.h \ + server.c \ + server.h diff --git a/src/libtest/server.c b/src/libtest/server.c new file mode 100644 index 00000000..f28af178 --- /dev/null +++ b/src/libtest/server.c @@ -0,0 +1,68 @@ +/************************************************************\ + * 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 "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]; + 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..c59b9ed4 --- /dev/null +++ b/src/libtest/server.h @@ -0,0 +1,32 @@ +/************************************************************\ + * 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 "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 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..b7541b4f --- /dev/null +++ b/src/libtest/state.h @@ -0,0 +1,22 @@ +/************************************************************\ + * 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 + +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 + */ 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;