From c975f53276eb34b4ea0e9db626d98bbd605531d5 Mon Sep 17 00:00:00 2001 From: Dmitry Kurochkin Date: Sun, 28 Aug 2011 21:11:19 +0400 Subject: [PATCH] Disable Rock store and shared memory cache if shared memory is not available. The patch adds support for building on systems without POSIX shared memory (e.g. OpenBSD). IpcIo DiskIO module and Rock store is not enabled by default if shared memory support is not available. If IpcIo or Rock store is explicitly enabled, but no shared memory support is available, fail with error during configure. Fail with error at runtime if shared memory cache is enabled but no shared memory support is detected. --- compat/Makefile.am | 1 + compat/shm.h | 45 ++++++++++++++++++++++++++++++++++++++++++ configure.ac | 22 ++++++++++++++++++++- src/MemStore.cc | 6 +++++- src/ipc/mem/Segment.cc | 10 ++++++++++ src/ipc/mem/Segment.h | 3 +++ 6 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 compat/shm.h diff --git a/compat/Makefile.am b/compat/Makefile.am index cb60e69580..8667bb0c65 100644 --- a/compat/Makefile.am +++ b/compat/Makefile.am @@ -32,6 +32,7 @@ libcompat_squid_a_SOURCES = \ initgroups.h \ osdetect.h \ psignal.h \ + shm.h \ stdio.h \ stdvarargs.h \ strnstr.cc \ diff --git a/compat/shm.h b/compat/shm.h new file mode 100644 index 0000000000..d1b66bd426 --- /dev/null +++ b/compat/shm.h @@ -0,0 +1,45 @@ +/* + * $Id$ + */ +#ifndef SQUID_COMPAT_SHM_H +#define SQUID_COMPAT_SHM_H + +#if HAVE_SHM + +#if HAVE_SYS_STAT_H +#include /* for mode constants */ +#endif + +#if HAVE_FCNTL_H +#include /* for O_* constants */ +#endif + +#if HAVE_SYS_MMAN_H +#include +#endif + +#else /* HAVE_SHM */ + +#if HAVE_ERRNO_H +#include +#endif + +extern "C" { + +inline int +shm_open(const char *, int, mode_t) { + errno = ENOTSUP; + return -1; +} + +inline int +shm_unlink(const char *) { + errno = ENOTSUP; + return -1; +} + +} /* extern "C" */ + +#endif /* HAVE_SHM */ + +#endif /* SQUID_COMPAT_CPU_H */ diff --git a/configure.ac b/configure.ac index 2bc44b31cd..a21ab99686 100644 --- a/configure.ac +++ b/configure.ac @@ -470,6 +470,11 @@ if test "x$with_dl" = "xyes"; then AC_MSG_NOTICE([With dl]) fi +AC_SEARCH_LIBS([shm_open], [rt]) +if test "x$ac_cv_search_shm_open" != "xno" ; then + AC_DEFINE(HAVE_SHM,1,[Support shared memory features]) +fi + AC_MSG_CHECKING([for DiskIO modules to be enabled]) squid_disk_module_candidates="" squid_opt_enable_diskio="auto" #values: no, yes, "auto"(=yes+detect modules) @@ -500,6 +505,10 @@ case $enableval in if test "x$squid_opt_enable_diskio" = "xauto"; then squid_opt_enable_diskio="yes" SQUID_LOOK_FOR_MODULES([$srcdir/src/DiskIO],[squid_disk_module_candidates]) + if test "x$ac_cv_search_shm_open" = "xno" ; then + # disable IpcIo + squid_disk_module_candidates=`echo $squid_disk_module_candidates|sed 's/IpcIo//'` + fi fi AC_MSG_RESULT([${squid_disk_module_candidates:-none}]) @@ -675,6 +684,9 @@ for module in $squid_disk_module_candidates none; do IpcIo) AC_MSG_NOTICE([Enabling IpcIo DiskIO module]) + if test "x$ac_cv_search_shm_open" = "xno" ; then + AC_MSG_ERROR([DiskIO IpcIo module requires shared memory support]) + fi DISK_LIBS="$DISK_LIBS libIpcIo.a" DISK_MODULES="$DISK_MODULES IpcIo" DISK_LINKOBJS="$DISK_LINKOBJS DiskIO/IpcIo/IpcIoDiskIOModule.o" @@ -738,6 +750,10 @@ if test "x$squid_opt_enable_storeio" = "xauto"; then SQUID_LOOK_FOR_MODULES([$srcdir/src/fs],[squid_storeio_module_candidates]) # disable coss squid_storeio_module_candidates=`echo $squid_storeio_module_candidates|sed 's/coss//'` + if test "x$ac_cv_search_shm_open" = "xno" ; then + # disable rock + squid_storeio_module_candidates=`echo $squid_storeio_module_candidates|sed 's/rock//'` + fi AC_MSG_RESULT([$squid_storeio_module_candidates]) fi @@ -773,6 +789,9 @@ for fs in $squid_storeio_module_candidates none; do "x$squid_disk_module_candidates_Blocking" = "xyes"; then AC_MSG_ERROR([Storage module Rock requires IpcIo and Blocking DiskIO modules]) fi + if test "x$ac_cv_search_shm_open" = "xno" ; then + AC_MSG_ERROR([Storage module Rock requires shared memory support]) + fi STORE_TESTS="$STORE_TESTS tests/testRock$EXEEXT" ;; ufs) @@ -2351,6 +2370,7 @@ AC_CHECK_HEADERS( \ sys/param.h \ sys/prctl.h \ sys/md5.h \ + sys/mman.h \ sys/msg.h \ sys/resource.h \ sys/select.h \ @@ -2571,7 +2591,7 @@ AC_SEARCH_LIBS([bind],[socket]) AC_SEARCH_LIBS([opcom_stack_trace],[opcom_stack]) AC_SEARCH_LIBS([strlcpy], [bsd]) AC_SEARCH_LIBS([yp_match], [nsl nss_nis nss_nisplus]) -AC_SEARCH_LIBS([shm_open], [rt]) + dnl Check for Winsock only on MinGW, on Cygwin we must use emulated BSD socket API if test "x$squid_host_os" = "xmingw" ; then SQUID_CHECK_WINSOCK_LIB diff --git a/src/MemStore.cc b/src/MemStore.cc index e7621689d4..c4109ea931 100644 --- a/src/MemStore.cc +++ b/src/MemStore.cc @@ -362,11 +362,15 @@ void MemStoreRr::run(const RunnerRegistry &) // decide whether to use a shared memory cache if the user did not specify if (!Config.memShared.configured()) { Config.memShared.configure(AtomicOperationsSupported && - UsingSmp() && Config.memMaxSize > 0); + Ipc::Mem::Segment::Enabled() && UsingSmp() && + Config.memMaxSize > 0); } else if (Config.memShared && !AtomicOperationsSupported) { // bail if the user wants shared memory cache but we cannot support it fatal("memory_cache_shared is on, but no support for atomic operations detected"); + } else + if (Config.memShared && !Ipc::Mem::Segment::Enabled()) { + fatal("memory_cache_shared is on, but no support for shared memory detected"); } if (!Config.memShared) diff --git a/src/ipc/mem/Segment.cc b/src/ipc/mem/Segment.cc index 470c3b9002..545a158607 100644 --- a/src/ipc/mem/Segment.cc +++ b/src/ipc/mem/Segment.cc @@ -7,6 +7,7 @@ #include "config.h" #include "base/TextException.h" +#include "compat/shm.h" #include "ipc/mem/Segment.h" #include "protos.h" @@ -32,6 +33,15 @@ Ipc::Mem::Segment::~Segment() { unlink(); } +bool +Ipc::Mem::Segment::Enabled() { +#if HAVE_SHM + return true; +#else + return false; +#endif +} + void Ipc::Mem::Segment::create(const off_t aSize) { diff --git a/src/ipc/mem/Segment.h b/src/ipc/mem/Segment.h index b45a99fba7..792ab9f75d 100644 --- a/src/ipc/mem/Segment.h +++ b/src/ipc/mem/Segment.h @@ -19,6 +19,9 @@ public: Segment(const char *const id); ~Segment(); + /// Whether shared memory support is available + static bool Enabled(); + /// Create a new shared memory segment. Fails if a segment with /// the same name already exists. Unlinks the segment on destruction. void create(const off_t aSize); -- 2.47.3