Skip to content

Commit be0abe9

Browse files
committed
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.
1 parent a4bd53a commit be0abe9

File tree

5 files changed

+60
-2
lines changed

5 files changed

+60
-2
lines changed

configure.ac

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ AC_TYPE_UINT32_T
135135
AC_TYPE_UINT64_T
136136
AC_TYPE_UINT8_T
137137

138+
CT_CHECK_TLS
139+
AC_CHECK_DECLS([INADDR_LOOPBACK], [], [], [#include <netinet/in.h>])
140+
138141
AC_MSG_CHECKING([whether pthread_t is a pointer])
139142
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([
140143
#include <pthread.h>

cpp/net/url_fetcher_test.cc

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#include "config.h"
2+
13
#include <csignal>
24
#include <fcntl.h>
35
#include <gflags/gflags.h>
@@ -7,6 +9,9 @@
79
#include <sys/stat.h>
810
#include <sys/wait.h>
911
#include <unistd.h>
12+
#ifdef HAVE_VFORK_H
13+
#include <vfork.h>
14+
#endif
1015

1116
#include "net/connection_pool.h"
1217
#include "net/url_fetcher.h"
@@ -57,7 +62,12 @@ class LocalhostResolver : public libevent::Base::Resolver {
5762
pid_t RunOpenSSLServer(uint16_t port, const std::string& cert_file,
5863
const std::string& key_file,
5964
const std::string& mode = "-www") {
65+
#ifdef HAVE_WORKING_VFORK
66+
pid_t pid(vfork());
67+
#else
6068
pid_t pid(fork());
69+
#endif
70+
6171
if (pid == -1) {
6272
LOG(INFO) << "fork() failed: " << pid;
6373
} else if (pid == 0) {
@@ -315,7 +325,13 @@ int main(int argc, char** argv) {
315325
struct sockaddr_in hang_addr;
316326
bzero((char*)&hang_addr, sizeof(hang_addr));
317327
hang_addr.sin_family = AF_INET;
318-
hang_addr.sin_addr.s_addr = INADDR_ANY;
328+
// Prefer to use INADDR_LOOPBACK if available as it avoids the firewall
329+
// triggering on some platforms if we bind a non-local address.
330+
#ifdef HAVE_INADDR_LOOPBACK
331+
hang_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
332+
#else
333+
hang_addr.sin_addr.s_addr = htonl(INADDR_ANY);
334+
#endif
319335
hang_addr.sin_port = htons(cert_trans::kHangPort);
320336
CHECK_EQ(0, bind(hang_fd, reinterpret_cast<struct sockaddr*>(&hang_addr),
321337
sizeof(hang_addr)));

cpp/server/event.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ bool Services::InitServer(int* sock, int port, const char* ip, int type) {
251251
server.sin_family = AF_INET;
252252
server.sin_port = htons((unsigned short)port);
253253
if (ip == NULL)
254-
server.sin_addr.s_addr = INADDR_ANY;
254+
server.sin_addr.s_addr = htonl(INADDR_ANY);
255255
else
256256
memcpy(&server.sin_addr.s_addr, ip, 4);
257257

cpp/server/server.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
#include "util/thread_pool.h"
3737
#include "util/uuid.h"
3838

39+
using std::bind;
40+
3941
DEFINE_int32(node_state_refresh_seconds, 10,
4042
"How often to refresh the ClusterNodeState entry for this node.");
4143
DEFINE_int32(watchdog_seconds, 120,

m4/ct_check_tls.m4

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
dnl Checks for thread-local storage support.
2+
dnl
3+
dnl Taken from the openvswitch config code (Apache 2.0 License)
4+
dnl with some local modifications. Does not include <threads.h>
5+
dnl as this does not currently exist on GCC.
6+
dnl Checks whether the compiler and linker support the C11
7+
dnl thread_local macro from <threads.h>, and if so defines
8+
dnl HAVE_THREAD_LOCAL. If not, checks whether the compiler and linker
9+
dnl support the GCC __thread extension, and if so defines
10+
dnl HAVE___THREAD.
11+
AC_DEFUN([CT_CHECK_TLS],
12+
[AC_CACHE_CHECK(
13+
[whether $CC has <threads.h> that supports thread_local],
14+
[ct_cv_thread_local],
15+
[AC_LINK_IFELSE(
16+
[AC_LANG_PROGRAM([ static thread_local int var;], [return var;])],
17+
[ct_cv_thread_local=yes],
18+
[ct_cv_thread_local=no])])
19+
if test $ct_cv_thread_local = yes; then
20+
AC_DEFINE([HAVE_THREAD_LOCAL], [1],
21+
[Define to 1 if the C compiler and linker supports the C11
22+
thread_local macro defined in <threads.h>.])
23+
else
24+
AC_CACHE_CHECK(
25+
[whether $CC supports __thread],
26+
[ct_cv___thread],
27+
[AC_LINK_IFELSE(
28+
[AC_LANG_PROGRAM([static __thread int var;], [return var;])],
29+
[ct_cv___thread=yes],
30+
[ct_cv___thread=no])])
31+
if test $ct_cv___thread = yes; then
32+
AC_DEFINE([HAVE___THREAD], [1],
33+
[Define to 1 if the C compiler and linker supports the
34+
GCC __thread extensions.])
35+
fi
36+
fi])
37+

0 commit comments

Comments
 (0)