nsswitch: protect access to wb_global_ctx by a mutex
authorRalph Wuerthner <ralph.wuerthner@de.ibm.com>
Tue, 2 Oct 2018 11:41:00 +0000 (13:41 +0200)
committerJeremy Allison <jra@samba.org>
Thu, 1 Nov 2018 00:59:10 +0000 (01:59 +0100)
This change will make libwbclient thread safe for all API calls not using a
context. Especially there are no more conflicts with threads using nsswitch
and libwbclient in parallel.

Signed-off-by: Ralph Wuerthner <ralph.wuerthner@de.ibm.com>
Reviewed-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
nsswitch/wb_common.c

index 9ba74c3..59370aa 100644 (file)
 #include "system/select.h"
 #include "winbind_client.h"
 
+#if HAVE_PTHREAD_H
+#include <pthread.h>
+#endif
+
 /* Global context */
 
 struct winbindd_context {
@@ -35,6 +39,10 @@ struct winbindd_context {
        pid_t our_pid;          /* calling process pid */
 };
 
+#if HAVE_PTHREAD
+static pthread_mutex_t wb_global_ctx_mutex = PTHREAD_MUTEX_INITIALIZER;
+#endif
+
 static struct winbindd_context *get_wb_global_ctx(void)
 {
        static struct winbindd_context wb_global_ctx = {
@@ -43,12 +51,17 @@ static struct winbindd_context *get_wb_global_ctx(void)
                .our_pid = 0
        };
 
+#if HAVE_PTHREAD
+       pthread_mutex_lock(&wb_global_ctx_mutex);
+#endif
        return &wb_global_ctx;
 }
 
 static void put_wb_global_ctx(void)
 {
-       /* noop for now */
+#if HAVE_PTHREAD
+       pthread_mutex_unlock(&wb_global_ctx_mutex);
+#endif
        return;
 }