From f5cddcd300b779131662519d56ae90fcebe459e1 Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Thu, 20 Oct 2016 10:22:40 +0200 Subject: swrap: Make symbol loading thread-safe Signed-off-by: Andreas Schneider Reviewed-by: Michael Adam --- src/socket_wrapper.c | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/socket_wrapper.c b/src/socket_wrapper.c index f7a0d55..e205a36 100644 --- a/src/socket_wrapper.c +++ b/src/socket_wrapper.c @@ -79,6 +79,7 @@ #ifdef HAVE_RPC_RPC_H #include #endif +#include enum swrap_dbglvl_e { SWRAP_LOG_ERROR = 0, @@ -152,6 +153,15 @@ enum swrap_dbglvl_e { # endif #endif +/* Macros for accessing mutexes */ +# define SWRAP_LOCK(m) do { \ + pthread_mutex_lock(&(m ## _mutex)); \ +} while(0) + +# define SWRAP_UNLOCK(m) do { \ + pthread_mutex_unlock(&(m ## _mutex)); \ +} while(0) + #define SWRAP_DLIST_ADD(list,item) do { \ if (!(list)) { \ @@ -305,6 +315,9 @@ static size_t max_sockets = 0; */ static struct socket_info_fd *socket_fds; +/* The mutex for accessing the global libc.symbols */ +static pthread_mutex_t libc_symbol_binding_mutex = PTHREAD_MUTEX_INITIALIZER; + /* Function prototypes */ bool socket_wrapper_enabled(void); @@ -623,22 +636,28 @@ static void *_swrap_bind_symbol(enum swrap_lib lib, const char *fn_name) } #define swrap_bind_symbol_libc(sym_name) \ + SWRAP_LOCK(libc_symbol_binding); \ if (swrap.libc.symbols._libc_##sym_name.obj == NULL) { \ swrap.libc.symbols._libc_##sym_name.obj = \ _swrap_bind_symbol(SWRAP_LIBC, #sym_name); \ - } + } \ + SWRAP_UNLOCK(libc_symbol_binding) #define swrap_bind_symbol_libsocket(sym_name) \ + SWRAP_LOCK(libc_symbol_binding); \ if (swrap.libc.symbols._libc_##sym_name.obj == NULL) { \ swrap.libc.symbols._libc_##sym_name.obj = \ _swrap_bind_symbol(SWRAP_LIBSOCKET, #sym_name); \ - } + } \ + SWRAP_UNLOCK(libc_symbol_binding) #define swrap_bind_symbol_libnsl(sym_name) \ + SWRAP_LOCK(libc_symbol_binding); \ if (swrap.libc.symbols._libc_##sym_name.obj == NULL) { \ swrap.libc.symbols._libc_##sym_name.obj = \ _swrap_bind_symbol(SWRAP_LIBNSL, #sym_name); \ - } + } \ + SWRAP_UNLOCK(libc_symbol_binding) /* * IMPORTANT -- cgit