diff options
-rw-r--r-- | src/socket_wrapper.c | 24 |
1 files changed, 22 insertions, 2 deletions
diff --git a/src/socket_wrapper.c b/src/socket_wrapper.c index 4f8d218..3f027dc 100644 --- a/src/socket_wrapper.c +++ b/src/socket_wrapper.c @@ -307,6 +307,9 @@ static pthread_mutex_t autobind_start_mutex; /* Mutex to guard the initialization of array of socket_info structures */ static pthread_mutex_t sockets_mutex; +/* Mutex to guard the socket reset in swrap_close() and swrap_remove_stale() */ +static pthread_mutex_t socket_reset_mutex; + /* Mutex to synchronize access to first free index in socket_info array */ static pthread_mutex_t first_free_mutex; @@ -2034,15 +2037,20 @@ static void swrap_remove_stale(int fd) SWRAP_LOG(SWRAP_LOG_TRACE, "remove stale wrapper for %d", fd); + swrap_mutex_lock(&socket_reset_mutex); + si_index = find_socket_info_index(fd); if (si_index == -1) { + swrap_mutex_unlock(&socket_reset_mutex); return; } - si = swrap_get_socket_info(si_index); - reset_socket_info_index(fd); + swrap_mutex_unlock(&socket_reset_mutex); + + si = swrap_get_socket_info(si_index); + swrap_mutex_lock(&first_free_mutex); SWRAP_LOCK_SI(si); @@ -5909,13 +5917,18 @@ static int swrap_close(int fd) int si_index; int ret; + swrap_mutex_lock(&socket_reset_mutex); + si_index = find_socket_info_index(fd); if (si_index == -1) { + swrap_mutex_unlock(&socket_reset_mutex); return libc_close(fd); } reset_socket_info_index(fd); + swrap_mutex_unlock(&socket_reset_mutex); + si = swrap_get_socket_info(si_index); swrap_mutex_lock(&first_free_mutex); @@ -6202,6 +6215,13 @@ void swrap_constructor(void) exit(-1); } + ret = socket_wrapper_init_mutex(&socket_reset_mutex); + if (ret != 0) { + SWRAP_LOG(SWRAP_LOG_ERROR, + "Failed to initialize pthread mutex"); + exit(-1); + } + ret = socket_wrapper_init_mutex(&first_free_mutex); if (ret != 0) { SWRAP_LOG(SWRAP_LOG_ERROR, |