From 74707e8e8e1be2cbaa71fb0442957be2e663cbde Mon Sep 17 00:00:00 2001
From: Stefan Metzmacher <metze@samba.org>
Date: Tue, 14 Apr 2009 12:45:48 +0200
Subject: socket_wrapper: fix the build on systems without ipv6 support

metze
---
 lib/socket_wrapper/socket_wrapper.c | 2 ++
 1 file changed, 2 insertions(+)

(limited to 'lib')

diff --git a/lib/socket_wrapper/socket_wrapper.c b/lib/socket_wrapper/socket_wrapper.c
index d3853de50d..d809d8a500 100644
--- a/lib/socket_wrapper/socket_wrapper.c
+++ b/lib/socket_wrapper/socket_wrapper.c
@@ -1102,8 +1102,10 @@ static uint8_t *swrap_marshall_packet(struct socket_info *si,
 	switch (si->family) {
 	case AF_INET:
 		break;
+#ifdef HAVE_IPV6
 	case AF_INET6:
 		break;
+#endif
 	default:
 		return NULL;
 	}
-- 
cgit 


From 27a3118bcf6105848e8200c15ebab910831f73c6 Mon Sep 17 00:00:00 2001
From: Jeremy Allison <jra@samba.org>
Date: Tue, 14 Apr 2009 12:23:22 -0700
Subject: Make talloc_stack threadsafe using TLS. Volker please check. Passes
 make test and basic valgrind testing. Jeremy.

---
 lib/util/config.mk              |  1 +
 lib/util/smb_threads.c          | 19 +++++++--
 lib/util/smb_threads_internal.h |  5 ++-
 lib/util/talloc_stack.c         | 93 ++++++++++++++++++++++++++++++++---------
 4 files changed, 93 insertions(+), 25 deletions(-)

(limited to 'lib')

diff --git a/lib/util/config.mk b/lib/util/config.mk
index 7835fed911..1b620d1464 100644
--- a/lib/util/config.mk
+++ b/lib/util/config.mk
@@ -27,6 +27,7 @@ LIBSAMBA-UTIL_OBJ_FILES = $(addprefix $(libutilsrcdir)/, \
 		become_daemon.o \
 		rbtree.o \
 		talloc_stack.o \
+		smb_threads.o \
 		params.o)
 
 PUBLIC_HEADERS += $(addprefix $(libutilsrcdir)/, util.h \
diff --git a/lib/util/smb_threads.c b/lib/util/smb_threads.c
index 84dec4d874..8e0e1cd915 100644
--- a/lib/util/smb_threads.c
+++ b/lib/util/smb_threads.c
@@ -50,8 +50,19 @@ int smb_thread_set_functions(const struct smb_thread_functions *tf)
 
 	global_tfp = tf;
 
+#if defined(PARANOID_MALLOC_CHECKER)
+#ifdef malloc
+#undef malloc
+#endif
+#endif
+
 	/* Here we initialize any static locks we're using. */
-	global_lock_array = (void **)SMB_MALLOC_ARRAY(void *, NUM_GLOBAL_LOCKS);
+	global_lock_array = (void **)malloc(sizeof(void *) *NUM_GLOBAL_LOCKS);
+
+#if defined(PARANOID_MALLOC_CHECKER)
+#define malloc(s) __ERROR_DONT_USE_MALLOC_DIRECTLY
+#endif
+
 	if (global_lock_array == NULL) {
 		return ENOMEM;
 	}
@@ -62,9 +73,11 @@ int smb_thread_set_functions(const struct smb_thread_functions *tf)
 			SAFE_FREE(global_lock_array);
 			return ENOMEM;
 		}
-		global_tfp->create_mutex(name,
+		if (global_tfp->create_mutex(name,
 				&global_lock_array[i],
-				__location__);
+				__location__)) {
+			smb_panic("smb_thread_set_functions: create mutexes failed");
+		}
 		SAFE_FREE(name);
 	}
 
diff --git a/lib/util/smb_threads_internal.h b/lib/util/smb_threads_internal.h
index 3208bc27e1..ad05aae9a5 100644
--- a/lib/util/smb_threads_internal.h
+++ b/lib/util/smb_threads_internal.h
@@ -44,9 +44,10 @@
 	} while (0)
 
 #define SMB_THREAD_SET_TLS(key, val) \
-	(global_tfp ? global_tfp->set_tls((key),(val),__location__) : 0)
+	(global_tfp ? global_tfp->set_tls((key),(val),__location__) : \
+		((key) = (val), 0))
 
 #define SMB_THREAD_GET_TLS(key) \
-	(global_tfp ? global_tfp->get_tls((key), __location__) : NULL)
+	(global_tfp ? global_tfp->get_tls((key), __location__) : (key))
 
 #endif
diff --git a/lib/util/talloc_stack.c b/lib/util/talloc_stack.c
index 2f3ea11377..a9b6e033ce 100644
--- a/lib/util/talloc_stack.c
+++ b/lib/util/talloc_stack.c
@@ -2,6 +2,7 @@
    Unix SMB/CIFS implementation.
    Implement a stack of talloc contexts
    Copyright (C) Volker Lendecke 2007
+   Copyright (C) Jeremy Allison 2009 - made thread safe.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -38,22 +39,65 @@
 
 #include "includes.h"
 
-static int talloc_stacksize;
-static int talloc_stack_arraysize;
-static TALLOC_CTX **talloc_stack;
+struct talloc_stackframe {
+	int talloc_stacksize;
+	int talloc_stack_arraysize;
+	TALLOC_CTX **talloc_stack;
+};
+
+/*
+ * In the single threaded case this is a pointer
+ * to the global talloc_stackframe. In the MT-case
+ * this is the pointer to the thread-specific key
+ * used to look up the per-thread talloc_stackframe
+ * pointer.
+ */
+
+static void *global_ts;
+
+static struct talloc_stackframe *talloc_stackframe_init(void)
+{
+#if defined(PARANOID_MALLOC_CHECKER)
+#ifdef malloc
+#undef malloc
+#endif
+#endif
+	struct talloc_stackframe *ts =
+		(struct talloc_stackframe *)malloc(sizeof(struct talloc_stackframe));
+#if defined(PARANOID_MALLOC_CHECKER)
+#define malloc(s) __ERROR_DONT_USE_MALLOC_DIRECTLY
+#endif
+
+	if (!ts) {
+		smb_panic("talloc_stackframe_init malloc failed");
+	}
+
+	ZERO_STRUCTP(ts);
+
+	if (SMB_THREAD_CREATE_TLS("talloc_stackframe", global_ts)) {
+		smb_panic("talloc_stackframe_init create_tls failed");
+	}
+
+	if (SMB_THREAD_SET_TLS(global_ts, ts)) {
+		smb_panic("talloc_stackframe_init set_tls failed");
+	}
+	return ts;
+}
 
 static int talloc_pop(TALLOC_CTX *frame)
 {
+	struct talloc_stackframe *ts =
+		(struct talloc_stackframe *)SMB_THREAD_GET_TLS(global_ts);
 	int i;
 
-	for (i=talloc_stacksize-1; i>0; i--) {
-		if (frame == talloc_stack[i]) {
+	for (i=ts->talloc_stacksize-1; i>0; i--) {
+		if (frame == ts->talloc_stack[i]) {
 			break;
 		}
-		talloc_free(talloc_stack[i]);
+		talloc_free(ts->talloc_stack[i]);
 	}
 
-	talloc_stacksize = i;
+	ts->talloc_stacksize = i;
 	return 0;
 }
 
@@ -67,22 +111,27 @@ static int talloc_pop(TALLOC_CTX *frame)
 static TALLOC_CTX *talloc_stackframe_internal(size_t poolsize)
 {
 	TALLOC_CTX **tmp, *top, *parent;
+	struct talloc_stackframe *ts =
+		(struct talloc_stackframe *)SMB_THREAD_GET_TLS(global_ts);
 
-	if (talloc_stack_arraysize < talloc_stacksize + 1) {
-		tmp = talloc_realloc(NULL, talloc_stack, TALLOC_CTX *,
-					   talloc_stacksize + 1);
+	if (ts == NULL) {
+		ts = talloc_stackframe_init();
+	}
+
+	if (ts->talloc_stack_arraysize < ts->talloc_stacksize + 1) {
+		tmp = talloc_realloc(NULL, ts->talloc_stack, TALLOC_CTX *,
+					   ts->talloc_stacksize + 1);
 		if (tmp == NULL) {
 			goto fail;
 		}
-		talloc_stack = tmp;
-		talloc_stack_arraysize = talloc_stacksize + 1;
+		ts->talloc_stack = tmp;
+		ts->talloc_stack_arraysize = ts->talloc_stacksize + 1;
         }
 
-	if (talloc_stacksize == 0) {
-		parent = talloc_stack;
-	}
-	else {
-		parent = talloc_stack[talloc_stacksize-1];
+	if (ts->talloc_stacksize == 0) {
+		parent = ts->talloc_stack;
+	} else {
+		parent = ts->talloc_stack[ts->talloc_stacksize-1];
 	}
 
 	if (poolsize) {
@@ -97,7 +146,7 @@ static TALLOC_CTX *talloc_stackframe_internal(size_t poolsize)
 
 	talloc_set_destructor(top, talloc_pop);
 
-	talloc_stack[talloc_stacksize++] = top;
+	ts->talloc_stack[ts->talloc_stacksize++] = top;
 	return top;
 
  fail:
@@ -121,10 +170,14 @@ TALLOC_CTX *talloc_stackframe_pool(size_t poolsize)
 
 TALLOC_CTX *talloc_tos(void)
 {
-	if (talloc_stacksize == 0) {
+	struct talloc_stackframe *ts =
+		(struct talloc_stackframe *)SMB_THREAD_GET_TLS(global_ts);
+
+	if (ts == NULL) {
 		talloc_stackframe();
+		ts = (struct talloc_stackframe *)SMB_THREAD_GET_TLS(global_ts);
 		DEBUG(0, ("no talloc stackframe around, leaking memory\n"));
 	}
 
-	return talloc_stack[talloc_stacksize-1];
+	return ts->talloc_stack[ts->talloc_stacksize-1];
 }
-- 
cgit 


From 8e06e945b6b03db825862f2f057f0dfe13ade3a2 Mon Sep 17 00:00:00 2001
From: Jeremy Allison <jra@samba.org>
Date: Tue, 14 Apr 2009 15:19:39 -0700
Subject: Ensure a tls key is only generated once - wrap create & destroy in a
 mutex. Change the function names to add _once to the tls_create & tls_destroy
 to make this obvious. Jeremy.

---
 lib/util/smb_threads.c          |  9 +++--
 lib/util/smb_threads.h          | 84 +++++++++++++++++++++++++----------------
 lib/util/smb_threads_internal.h | 16 ++++++--
 lib/util/talloc_stack.c         |  2 +-
 4 files changed, 71 insertions(+), 40 deletions(-)

(limited to 'lib')

diff --git a/lib/util/smb_threads.c b/lib/util/smb_threads.c
index 8e0e1cd915..fa2d8da186 100644
--- a/lib/util/smb_threads.c
+++ b/lib/util/smb_threads.c
@@ -25,8 +25,6 @@
 
 #include "includes.h"
 
-#define NUM_GLOBAL_LOCKS 1
-
 /*********************************************************
  Functions to vector the locking primitives used internally
  by libsmbclient.
@@ -94,14 +92,18 @@ int smb_thread_set_functions(const struct smb_thread_functions *tf)
 
 SMB_THREADS_DEF_PTHREAD_IMPLEMENTATION(tf);
 
+void *pkey = NULL;
+
 /* Test function. */
 int test_threads(void)
 {
 	int ret;
 	void *plock = NULL;
-
 	smb_thread_set_functions(&tf);
 
+	if ((ret = SMB_THREAD_CREATE_TLS_ONCE("test_tls", pkey)) != 0) {
+		printf("Create tls once error: %d\n", ret);
+	}
 	if ((ret = SMB_THREAD_CREATE_MUTEX("test", plock)) != 0) {
 		printf("Create lock error: %d\n", ret);
 	}
@@ -112,6 +114,7 @@ int test_threads(void)
 		printf("unlock error: %d\n", ret);
 	}
 	SMB_THREAD_DESTROY_MUTEX(plock);
+	SMB_THREAD_DESTROY_TLS_ONCE(pkey);
 
 	return 0;
 }
diff --git a/lib/util/smb_threads.h b/lib/util/smb_threads.h
index 2ca163be9a..945e93803a 100644
--- a/lib/util/smb_threads.h
+++ b/lib/util/smb_threads.h
@@ -36,10 +36,10 @@ struct smb_thread_functions {
 			const char *location);
 
 	/* Thread local storage. */
-	int (*create_tls)(const char *keyname,
+	int (*create_tls_once)(const char *keyname,
 			void **ppkey,
 			const char *location);
-	void (*destroy_tls)(void *pkey,
+	void (*destroy_tls_once)(void **pkey,
 			const char *location);
 	int (*set_tls)(void *pkey, const void *pval, const char *location);
 	void *(*get_tls)(void *pkey, const char *location);
@@ -53,49 +53,69 @@ extern const struct smb_thread_functions *global_tfp;
  \
 static int smb_create_mutex_pthread(const char *lockname, void **pplock, const char *location) \
 { \
-        pthread_mutex_t *pmut = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t)); \
-        if (!pmut) { \
-                return ENOMEM; \
-        } \
-        pthread_mutex_init(pmut, NULL); \
-        *pplock = (void *)pmut; \
-        return 0; \
+	pthread_mutex_t *pmut = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t)); \
+	if (!pmut) { \
+		return ENOMEM; \
+	} \
+	pthread_mutex_init(pmut, NULL); \
+	*pplock = (void *)pmut; \
+	return 0; \
 } \
  \
 static void smb_destroy_mutex_pthread(void *plock, const char *location) \
 { \
-        pthread_mutex_destroy((pthread_mutex_t *)plock); \
+	pthread_mutex_destroy((pthread_mutex_t *)plock); \
 	free(plock); \
 } \
  \
 static int smb_lock_pthread(void *plock, enum smb_thread_lock_type lock_type, const char *location) \
 { \
-        if (lock_type == SMB_THREAD_UNLOCK) { \
-                return pthread_mutex_unlock((pthread_mutex_t *)plock); \
-        } else { \
-                return pthread_mutex_lock((pthread_mutex_t *)plock); \
-        } \
+	if (lock_type == SMB_THREAD_UNLOCK) { \
+		return pthread_mutex_unlock((pthread_mutex_t *)plock); \
+	} else { \
+		return pthread_mutex_lock((pthread_mutex_t *)plock); \
+	} \
 } \
  \
-static int smb_create_tls_pthread(const char *keyname, void **ppkey, const char *location) \
+static pthread_mutex_t create_tls_mutex = PTHREAD_MUTEX_INITIALIZER; \
+ \
+static int smb_create_tls_once_pthread(const char *keyname, void **ppkey, const char *location) \
 { \
-        int ret; \
-        pthread_key_t *pkey = (pthread_key_t *)malloc(sizeof(pthread_key_t)); \
-        if (!pkey) { \
-                return ENOMEM; \
-        } \
-        ret = pthread_key_create(pkey, NULL); \
-        if (ret) { \
-                return ret; \
-        } \
-        *ppkey = (void *)pkey; \
-        return 0; \
+	int ret; \
+	pthread_key_t *pkey; \
+	ret = pthread_mutex_lock(&create_tls_mutex); \
+	if (ret) { \
+		return ret; \
+	} \
+	if (*ppkey) { \
+		pthread_mutex_unlock(&create_tls_mutex); \
+		return 0; \
+	} \
+	pkey = (pthread_key_t *)malloc(sizeof(pthread_key_t)); \
+	if (!pkey) { \
+		pthread_mutex_unlock(&create_tls_mutex); \
+		return ENOMEM; \
+	} \
+	ret = pthread_key_create(pkey, NULL); \
+	if (ret) { \
+		free(pkey); \
+		pthread_mutex_unlock(&create_tls_mutex); \
+		return ret; \
+	} \
+	*ppkey = (void *)pkey; \
+	pthread_mutex_unlock(&create_tls_mutex); \
+	return 0; \
 } \
  \
-static void smb_destroy_tls_pthread(void *pkey, const char *location) \
+static void smb_destroy_tls_once_pthread(void **ppkey, const char *location) \
 { \
-        pthread_key_delete(*(pthread_key_t *)pkey); \
-        free(pkey); \
+	pthread_mutex_lock(&create_tls_mutex); \
+	if (*ppkey) { \
+		pthread_key_delete(*(pthread_key_t *)ppkey); \
+		free(*ppkey); \
+		*ppkey = NULL; \
+	} \
+	pthread_mutex_unlock(&create_tls_mutex); \
 } \
  \
 static int smb_set_tls_pthread(void *pkey, const void *pval, const char *location) \
@@ -112,8 +132,8 @@ static const struct smb_thread_functions (tf) = { \
                         smb_create_mutex_pthread, \
                         smb_destroy_mutex_pthread, \
                         smb_lock_pthread, \
-                        smb_create_tls_pthread, \
-                        smb_destroy_tls_pthread, \
+                        smb_create_tls_once_pthread, \
+                        smb_destroy_tls_once_pthread, \
                         smb_set_tls_pthread, \
                         smb_get_tls_pthread }
 
diff --git a/lib/util/smb_threads_internal.h b/lib/util/smb_threads_internal.h
index ad05aae9a5..58c6fe3f99 100644
--- a/lib/util/smb_threads_internal.h
+++ b/lib/util/smb_threads_internal.h
@@ -33,13 +33,13 @@
 #define SMB_THREAD_LOCK(plock, type) \
 	(global_tfp ? global_tfp->lock_mutex((plock), (type), __location__) : 0)
 
-#define SMB_THREAD_CREATE_TLS(keyname, key) \
-	(global_tfp ? global_tfp->create_tls((keyname), &(key), __location__) : 0)
+#define SMB_THREAD_CREATE_TLS_ONCE(keyname, key) \
+	(global_tfp ? global_tfp->create_tls_once((keyname), &(key), __location__) : 0)
 
-#define SMB_THREAD_DESTROY_TLS(key) \
+#define SMB_THREAD_DESTROY_TLS_ONCE(key) \
 	do { \
 		if (global_tfp) { \
-			global_tfp->destroy_tls(key); \
+			global_tfp->destroy_tls_once(&(key), __location__); \
 		}; \
 	} while (0)
 
@@ -50,4 +50,12 @@
 #define SMB_THREAD_GET_TLS(key) \
 	(global_tfp ? global_tfp->get_tls((key), __location__) : (key))
 
+/*
+ * Global thread lock list.
+ */
+
+#define NUM_GLOBAL_LOCKS 1
+
+#define GLOBAL_LOCK(locknum) (global_lock_array ? global_lock_array[(locknum)] : NULL)
+
 #endif
diff --git a/lib/util/talloc_stack.c b/lib/util/talloc_stack.c
index a9b6e033ce..f572dd6c77 100644
--- a/lib/util/talloc_stack.c
+++ b/lib/util/talloc_stack.c
@@ -74,7 +74,7 @@ static struct talloc_stackframe *talloc_stackframe_init(void)
 
 	ZERO_STRUCTP(ts);
 
-	if (SMB_THREAD_CREATE_TLS("talloc_stackframe", global_ts)) {
+	if (SMB_THREAD_CREATE_TLS_ONCE("talloc_stackframe", global_ts)) {
 		smb_panic("talloc_stackframe_init create_tls failed");
 	}
 
-- 
cgit