Set OpenSSL locking_function.

This commit is contained in:
Tatsuhiro Tsujikawa 2012-06-08 22:41:24 +09:00
parent 1199db690e
commit f2a6b3c9d6
3 changed files with 49 additions and 0 deletions

View File

@ -47,6 +47,7 @@
#include "shrpx_config.h"
#include "shrpx_listen_handler.h"
#include "shrpx_ssl.h"
namespace shrpx {
@ -446,8 +447,12 @@ int main(int argc, char **argv)
OpenSSL_add_all_algorithms();
SSL_load_error_strings();
SSL_library_init();
ssl::setup_ssl_lock();
event_loop();
ssl::teardown_ssl_lock();
return 0;
}

View File

@ -27,6 +27,9 @@
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/tcp.h>
#include <pthread.h>
#include <openssl/crypto.h>
#include <event2/bufferevent.h>
#include <event2/bufferevent_ssl.h>
@ -146,6 +149,43 @@ ClientHandler* accept_ssl_connection(event_base *evbase, SSL_CTX *ssl_ctx,
}
}
namespace {
pthread_mutex_t *ssl_locks;
} // namespace
namespace {
void ssl_locking_cb(int mode, int type, const char *file, int line)
{
if(mode & CRYPTO_LOCK) {
pthread_mutex_lock(&(ssl_locks[type]));
} else {
pthread_mutex_unlock(&(ssl_locks[type]));
}
}
} // namespace
void setup_ssl_lock()
{
ssl_locks = new pthread_mutex_t[CRYPTO_num_locks()];
for(int i = 0; i < CRYPTO_num_locks(); ++i) {
// Always returns 0
pthread_mutex_init(&(ssl_locks[i]), 0);
}
//CRYPTO_set_id_callback(ssl_thread_id); OpenSSL manual says that if
// threadid_func is not specified using
// CRYPTO_THREADID_set_callback(), then default implementation is
// used. We use this default one.
CRYPTO_set_locking_callback(ssl_locking_cb);
}
void teardown_ssl_lock()
{
for(int i = 0; i < CRYPTO_num_locks(); ++i) {
pthread_mutex_destroy(&(ssl_locks[i]));
}
delete [] ssl_locks;
}
} // namespace ssl
} // namespace shrpx

View File

@ -44,6 +44,10 @@ ClientHandler* accept_ssl_connection(event_base *evbase, SSL_CTX *ssl_ctx,
evutil_socket_t fd,
sockaddr *addr, int addrlen);
void setup_ssl_lock();
void teardown_ssl_lock();
} // namespace ssl
} // namespace shrpx