From be0abe9be8408745ce2d0f6a1c0f4dacc4fe25ca Mon Sep 17 00:00:00 2001 From: Martin2112 Date: Fri, 14 Aug 2015 14:58:11 +0100 Subject: [PATCH] Add support to configure.ac for checking thread local support and also check IPADDR_LOOPBACK is defined. In test use loopback if we can to prevent firewall issues and use correct thread local qualifier in sever. Plus ensure server.h qualifies calls to std::bind to prevent clash with C socket API. --- configure.ac | 3 +++ cpp/net/url_fetcher_test.cc | 18 +++++++++++++++++- cpp/server/event.cc | 2 +- cpp/server/server.h | 2 ++ m4/ct_check_tls.m4 | 37 +++++++++++++++++++++++++++++++++++++ 5 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 m4/ct_check_tls.m4 diff --git a/configure.ac b/configure.ac index 039d92874..7ce4fd59b 100644 --- a/configure.ac +++ b/configure.ac @@ -135,6 +135,9 @@ AC_TYPE_UINT32_T AC_TYPE_UINT64_T AC_TYPE_UINT8_T +CT_CHECK_TLS +AC_CHECK_DECLS([INADDR_LOOPBACK], [], [], [#include ]) + AC_MSG_CHECKING([whether pthread_t is a pointer]) AC_COMPILE_IFELSE([AC_LANG_PROGRAM([ #include diff --git a/cpp/net/url_fetcher_test.cc b/cpp/net/url_fetcher_test.cc index d387044c4..6d6333b74 100644 --- a/cpp/net/url_fetcher_test.cc +++ b/cpp/net/url_fetcher_test.cc @@ -1,3 +1,5 @@ +#include "config.h" + #include #include #include @@ -7,6 +9,9 @@ #include #include #include +#ifdef HAVE_VFORK_H +#include +#endif #include "net/connection_pool.h" #include "net/url_fetcher.h" @@ -57,7 +62,12 @@ class LocalhostResolver : public libevent::Base::Resolver { pid_t RunOpenSSLServer(uint16_t port, const std::string& cert_file, const std::string& key_file, const std::string& mode = "-www") { +#ifdef HAVE_WORKING_VFORK + pid_t pid(vfork()); +#else pid_t pid(fork()); +#endif + if (pid == -1) { LOG(INFO) << "fork() failed: " << pid; } else if (pid == 0) { @@ -315,7 +325,13 @@ int main(int argc, char** argv) { struct sockaddr_in hang_addr; bzero((char*)&hang_addr, sizeof(hang_addr)); hang_addr.sin_family = AF_INET; - hang_addr.sin_addr.s_addr = INADDR_ANY; + // Prefer to use INADDR_LOOPBACK if available as it avoids the firewall + // triggering on some platforms if we bind a non-local address. +#ifdef HAVE_INADDR_LOOPBACK + hang_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); +#else + hang_addr.sin_addr.s_addr = htonl(INADDR_ANY); +#endif hang_addr.sin_port = htons(cert_trans::kHangPort); CHECK_EQ(0, bind(hang_fd, reinterpret_cast(&hang_addr), sizeof(hang_addr))); diff --git a/cpp/server/event.cc b/cpp/server/event.cc index 430919d90..6752487ca 100644 --- a/cpp/server/event.cc +++ b/cpp/server/event.cc @@ -251,7 +251,7 @@ bool Services::InitServer(int* sock, int port, const char* ip, int type) { server.sin_family = AF_INET; server.sin_port = htons((unsigned short)port); if (ip == NULL) - server.sin_addr.s_addr = INADDR_ANY; + server.sin_addr.s_addr = htonl(INADDR_ANY); else memcpy(&server.sin_addr.s_addr, ip, 4); diff --git a/cpp/server/server.h b/cpp/server/server.h index 580f03f01..ed140f208 100644 --- a/cpp/server/server.h +++ b/cpp/server/server.h @@ -36,6 +36,8 @@ #include "util/thread_pool.h" #include "util/uuid.h" +using std::bind; + DEFINE_int32(node_state_refresh_seconds, 10, "How often to refresh the ClusterNodeState entry for this node."); DEFINE_int32(watchdog_seconds, 120, diff --git a/m4/ct_check_tls.m4 b/m4/ct_check_tls.m4 new file mode 100644 index 000000000..d16f066ca --- /dev/null +++ b/m4/ct_check_tls.m4 @@ -0,0 +1,37 @@ +dnl Checks for thread-local storage support. +dnl +dnl Taken from the openvswitch config code (Apache 2.0 License) +dnl with some local modifications. Does not include +dnl as this does not currently exist on GCC. +dnl Checks whether the compiler and linker support the C11 +dnl thread_local macro from , and if so defines +dnl HAVE_THREAD_LOCAL. If not, checks whether the compiler and linker +dnl support the GCC __thread extension, and if so defines +dnl HAVE___THREAD. +AC_DEFUN([CT_CHECK_TLS], + [AC_CACHE_CHECK( + [whether $CC has that supports thread_local], + [ct_cv_thread_local], + [AC_LINK_IFELSE( + [AC_LANG_PROGRAM([ static thread_local int var;], [return var;])], + [ct_cv_thread_local=yes], + [ct_cv_thread_local=no])]) + if test $ct_cv_thread_local = yes; then + AC_DEFINE([HAVE_THREAD_LOCAL], [1], + [Define to 1 if the C compiler and linker supports the C11 + thread_local macro defined in .]) + else + AC_CACHE_CHECK( + [whether $CC supports __thread], + [ct_cv___thread], + [AC_LINK_IFELSE( + [AC_LANG_PROGRAM([static __thread int var;], [return var;])], + [ct_cv___thread=yes], + [ct_cv___thread=no])]) + if test $ct_cv___thread = yes; then + AC_DEFINE([HAVE___THREAD], [1], + [Define to 1 if the C compiler and linker supports the + GCC __thread extensions.]) + fi + fi]) +