swrap: Add a mutex for resetting socket index
authorAndreas Schneider <asn@samba.org>
Fri, 28 Sep 2018 10:46:30 +0000 (12:46 +0200)
committerAndreas Schneider <asn@samba.org>
Tue, 23 Oct 2018 13:39:14 +0000 (15:39 +0200)
Fixes a helgrind error.

Signed-off-by: Andreas Schneider <asn@samba.org>
Reviewed-by: Ralph Boehme <slow@samba.org>
src/socket_wrapper.c

index 4f8d218c3b949c7c0888634cfe67ca04e8877cfa..3f027dce5420085e5adfe5040ac9f0334e7ff63e 100644 (file)
@@ -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,