diff options
author | Alan Antonuk <alan.antonuk@gmail.com> | 2017-10-07 21:37:05 -0700 |
---|---|---|
committer | Alan Antonuk <alan.antonuk@gmail.com> | 2017-12-10 22:38:25 -0800 |
commit | cdfce5a328a59bc9bb51428be0585715546c4634 (patch) | |
tree | 390f7c7e36fbdfae53f4349bb647c64aa783aae8 | |
parent | 9cb126c64759fd1294df5cfd4e16dcf938d40743 (diff) | |
download | rabbitmq-c-cdfce5a328a59bc9bb51428be0585715546c4634.tar.gz |
ssl: Make thread-safety unconditional in rabbitmq-c
Remove the option to disable thread-safety in the parts of rabbitmq-c
that use OpenSSL, making it a requirement for thread-safety to use SSL
with rabbitmq-c.
The existing #ifdef's are a source of additional complexity and the
non-thread-safe version is not well tested.
-rw-r--r-- | CMakeLists.txt | 28 | ||||
-rw-r--r-- | README.md | 2 | ||||
-rw-r--r-- | librabbitmq/CMakeLists.txt | 21 | ||||
-rw-r--r-- | librabbitmq/amqp_openssl.c | 14 | ||||
-rw-r--r-- | librabbitmq/amqp_openssl_bio.c | 6 |
5 files changed, 26 insertions, 45 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 5aea03a..fc07473 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 2.6) +cmake_minimum_required(VERSION 2.8.12) project(rabbitmq-c "C") # Enable MACOSX_RPATH by default. See: cmake --help-policy CMP0042 @@ -116,6 +116,17 @@ if (CLOCK_GETTIME_NEEDS_LIBRT OR POSIX_SPAWNP_NEEDS_LIBRT) set(LIBRT rt) endif() +option(ENABLE_SSL_SUPPORT "Enable SSL support" ON) + +if (ENABLE_SSL_SUPPORT) + find_package(OpenSSL 0.9.8 REQUIRED) + + cmake_push_check_state() + set(THREADS_PREFER_PTHREAD_FLAG ON) + find_package(Threads REQUIRED) + cmake_pop_check_state() +endif() + if (MSVC) set(CMAKE_C_FLAGS "/W4 /nologo ${CMAKE_C_FLAGS}") elseif (CMAKE_C_COMPILER_ID MATCHES ".*Clang") @@ -246,7 +257,6 @@ if (POPT_FOUND AND XmlTo_FOUND) set(DO_DOCS ON) endif() -find_package(Threads) option(BUILD_SHARED_LIBS "Build rabbitmq-c as a shared library" ON) option(BUILD_STATIC_LIBS "Build rabbitmq-c as a static library" ON) @@ -256,12 +266,6 @@ option(BUILD_TOOLS "Build Tools (requires POPT Library)" ${POPT_FOUND}) option(BUILD_TOOLS_DOCS "Build man pages for Tools (requires xmlto)" ${DO_DOCS}) option(BUILD_TESTS "Build tests (run tests with make test)" ON) option(BUILD_API_DOCS "Build Doxygen API docs" ${DOXYGEN_FOUND}) -option(ENABLE_SSL_SUPPORT "Enable SSL support" ON) -option(ENABLE_THREAD_SAFETY "Enable thread safety when using OpenSSL" ${Threads_FOUND}) - -if (ENABLE_SSL_SUPPORT) - find_package(OpenSSL 0.9.8 REQUIRED) -endif() if (NOT BUILD_SHARED_LIBS AND NOT BUILD_STATIC_LIBS) message(FATAL_ERROR "One or both of BUILD_SHARED_LIBS or BUILD_STATIC_LIBS must be set to ON to build") @@ -309,12 +313,8 @@ endif () set(libs_private ${SOCKET_LIBRARIES} ${LIBRT}) if (ENABLE_SSL_SUPPORT) - if (SSL_ENGINE STREQUAL "OpenSSL") - set(requires_private "openssl") - endif() - if (ENABLE_THREAD_SAFETY) - set(libs_private ${libs_private} ${CMAKE_THREAD_LIBS_INIT}) - endif() + set(requires_private "openssl") + set(libs_private ${libs_private} ${CMAKE_THREAD_LIBS_INIT}) endif() set(prefix ${CMAKE_INSTALL_PREFIX}) @@ -82,8 +82,6 @@ Other interesting flags that can be passed to CMake: find the XmlTo utility. * `ENABLE_SSL_SUPPORT=ON/OFF` toggles building rabbitmq-c with SSL support. By default this is ON if the OpenSSL headers and library can be found. -* `ENABLE_THREAD_SAFETY=ON/OFF` toggles OpenSSL thread-safety. By default this - is ON * `BUILD_API_DOCS=ON/OFF` - toggles building the Doxygen API documentation, by default this is OFF diff --git a/librabbitmq/CMakeLists.txt b/librabbitmq/CMakeLists.txt index c5dc663..bd5369a 100644 --- a/librabbitmq/CMakeLists.txt +++ b/librabbitmq/CMakeLists.txt @@ -96,13 +96,10 @@ if (ENABLE_SSL_SUPPORT) PROPERTIES COMPILE_FLAGS -Wno-deprecated-declarations) endif() - if (ENABLE_THREAD_SAFETY) - add_definitions(-DENABLE_THREAD_SAFETY) - if (WIN32) - set(AMQP_SSL_SRCS ${AMQP_SSL_SRCS} win32/threads.h win32/threads.c) - else() - set(AMQP_SSL_SRCS ${AMQP_SSL_SRCS} unix/threads.h) - endif() + if (WIN32) + set(AMQP_SSL_SRCS ${AMQP_SSL_SRCS} win32/threads.h win32/threads.c) + else() + set(AMQP_SSL_SRCS ${AMQP_SSL_SRCS} unix/threads.h) endif() endif() @@ -122,6 +119,9 @@ set(RMQ_LIBRARIES ${AMQP_SSL_LIBS} ${SOCKET_LIBRARIES} ${LIBRT} ${CMAKE_THREAD_L if (BUILD_SHARED_LIBS) add_library(rabbitmq SHARED ${RABBITMQ_SOURCES}) + if (THREADS_HAVE_PTHREAD_ARG) + target_compile_options(rabbitmq PUBLIC "-pthread") + endif() target_link_libraries(rabbitmq ${RMQ_LIBRARIES}) @@ -142,6 +142,9 @@ endif (BUILD_SHARED_LIBS) if (BUILD_STATIC_LIBS) add_library(rabbitmq-static STATIC ${RABBITMQ_SOURCES}) + if (THREADS_HAVE_PTHREAD_ARG) + target_compile_options(rabbitmq-static PUBLIC "-pthread") + endif() target_link_libraries(rabbitmq-static ${RMQ_LIBRARIES}) @@ -150,14 +153,14 @@ if (BUILD_STATIC_LIBS) set_target_properties(rabbitmq-static PROPERTIES VERSION ${RMQ_VERSION} OUTPUT_NAME librabbitmq.${RMQ_SOVERSION}) - + if(MSVC) set_target_properties(rabbitmq-static PROPERTIES # Embed debugging info in the library itself instead of generating # a .pdb file. COMPILE_OPTIONS "/Z7") endif(MSVC) - + else (WIN32) set_target_properties(rabbitmq-static PROPERTIES VERSION ${RMQ_VERSION} SOVERSION ${RMQ_SOVERSION} OUTPUT_NAME rabbitmq) endif (WIN32) diff --git a/librabbitmq/amqp_openssl.c b/librabbitmq/amqp_openssl.c index 417203a..a73cd06 100644 --- a/librabbitmq/amqp_openssl.c +++ b/librabbitmq/amqp_openssl.c @@ -54,7 +54,6 @@ static int open_ssl_connections = 0; static amqp_boolean_t do_initialize_openssl = 1; static amqp_boolean_t openssl_initialized = 0; -#ifdef ENABLE_THREAD_SAFETY static unsigned long amqp_ssl_threadid_callback(void); static void amqp_ssl_locking_callback(int mode, int n, const char *file, int line); @@ -62,7 +61,6 @@ static void amqp_ssl_locking_callback(int mode, int n, const char *file, static pthread_mutex_t openssl_init_mutex = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t *amqp_openssl_lockarray = NULL; -#endif /* ENABLE_THREAD_SAFETY */ struct amqp_ssl_socket_t { const struct amqp_socket_class_t *klass; @@ -532,7 +530,6 @@ void amqp_set_initialize_ssl_library(amqp_boolean_t do_initialize) { } } -#ifdef ENABLE_THREAD_SAFETY unsigned long amqp_ssl_threadid_callback(void) { return (unsigned long)pthread_self(); } @@ -549,16 +546,12 @@ void amqp_ssl_locking_callback(int mode, int n, AMQP_UNUSED const char *file, } } } -#endif /* ENABLE_THREAD_SAFETY */ static int initialize_openssl(void) { -#ifdef ENABLE_THREAD_SAFETY if (pthread_mutex_lock(&openssl_init_mutex)) { return -1; } -#endif /* ENABLE_THREAD_SAFETY */ if (do_initialize_openssl) { -#ifdef ENABLE_THREAD_SAFETY if (NULL == amqp_openssl_lockarray) { int i = 0; amqp_openssl_lockarray = @@ -581,7 +574,6 @@ static int initialize_openssl(void) { CRYPTO_set_id_callback(amqp_ssl_threadid_callback); CRYPTO_set_locking_callback(amqp_ssl_locking_callback); } -#endif /* ENABLE_THREAD_SAFETY */ if (!openssl_initialized) { OPENSSL_config(NULL); @@ -595,24 +587,19 @@ static int initialize_openssl(void) { ++open_ssl_connections; -#ifdef ENABLE_THREAD_SAFETY pthread_mutex_unlock(&openssl_init_mutex); -#endif /* ENABLE_THREAD_SAFETY */ return 0; } static int destroy_openssl(void) { -#ifdef ENABLE_THREAD_SAFETY if (pthread_mutex_lock(&openssl_init_mutex)) { return -1; } -#endif /* ENABLE_THREAD_SAFETY */ if (open_ssl_connections > 0) { --open_ssl_connections; } -#ifdef ENABLE_THREAD_SAFETY if (0 == open_ssl_connections && do_initialize_openssl) { /* Unsetting these allows the rabbitmq-c library to be unloaded * safely. We do leak the amqp_openssl_lockarray. Which is only @@ -634,6 +621,5 @@ static int destroy_openssl(void) { } pthread_mutex_unlock(&openssl_init_mutex); -#endif /* ENABLE_THREAD_SAFETY */ return 0; } diff --git a/librabbitmq/amqp_openssl_bio.c b/librabbitmq/amqp_openssl_bio.c index 5279620..df0ce2d 100644 --- a/librabbitmq/amqp_openssl_bio.c +++ b/librabbitmq/amqp_openssl_bio.c @@ -42,9 +42,7 @@ #ifdef AMQP_USE_AMQP_BIO -#ifdef ENABLE_THREAD_SAFETY static pthread_once_t bio_init_once = PTHREAD_ONCE_INIT; -#endif static int bio_initialized = 0; static BIO_METHOD amqp_bio_method; @@ -150,11 +148,7 @@ static void amqp_openssl_bio_init(void) { BIO_METHOD *amqp_openssl_bio(void) { #ifdef AMQP_USE_AMQP_BIO if (!bio_initialized) { -#ifdef ENABLE_THREAD_SAFETY pthread_once(&bio_init_once, amqp_openssl_bio_init); -#else - amqp_openssl_bio_init(); -#endif /* ifndef ENABLE_THREAD_SAFETY */ } return &amqp_bio_method; |