[SCM] Samba Shared Repository - branch master updated -
release-4-0-0alpha7-1604-g636fbd1
Derrell Lipman
derrell at samba.org
Wed May 13 18:37:55 GMT 2009
The branch, master has been updated
via 636fbd1028bf22d9a93ae5029f6881e144f58fe1 (commit)
via 045af600f299f55f5a5b09a23b753ba97880aa06 (commit)
via 088906b0641e48c704c5cd529f620023616f561f (commit)
from b9f3a78169be962c4f1fce625ca3a291d9f93c7c (commit)
http://gitweb.samba.org/?p=samba.git;a=shortlog;h=master
- Log -----------------------------------------------------------------
commit 636fbd1028bf22d9a93ae5029f6881e144f58fe1
Author: Derrell Lipman <derrell at dworkin.(none)>
Date: Wed May 13 14:37:17 2009 -0400
Thread-safe protection: libsmbclient initialization
- Begin converting init functions to use SMB_THREAD_ONCE. libsmbclient
module-wide initialization is now moved into a separate function and called
via SMB_THREAD_ONCE.
- libsmbclient counts users (contexts) so that it can release global resources
when the last context is closed. That count of contexts is now protected by
a mutex.
Derrell
commit 045af600f299f55f5a5b09a23b753ba97880aa06
Author: Derrell Lipman <derrell at dworkin.(none)>
Date: Wed May 13 14:33:21 2009 -0400
Take advantage of the easier-to-use thread macros
- Now that we initialize for the non-thread-safe case in the macro, there's no
need to do it here too.
Derrell
commit 088906b0641e48c704c5cd529f620023616f561f
Author: Derrell Lipman <derrell at dworkin.(none)>
Date: Wed May 13 14:31:40 2009 -0400
Make the thread functions a bit easier to use
- Create separate macros for lock and unlock so that it's easier to identify
which request is being made.
- Initialize *ponce in the SMB_THREAD_ONCE macro in the non-thread-safe case,
rather than requiring each init function to determine if it's in the
non-thread-safe case and manually initialize.
Derrell
-----------------------------------------------------------------------
Summary of changes:
lib/util/smb_threads.c | 6 +-
lib/util/smb_threads.h | 2 -
lib/util/smb_threads_internal.h | 12 ++-
lib/util/talloc_stack.c | 8 --
source3/libsmb/libsmb_context.c | 227 +++++++++++++++++++++++----------------
5 files changed, 145 insertions(+), 110 deletions(-)
Changeset truncated at 500 lines:
diff --git a/lib/util/smb_threads.c b/lib/util/smb_threads.c
index ffe2eb0..58ea2da 100644
--- a/lib/util/smb_threads.c
+++ b/lib/util/smb_threads.c
@@ -43,7 +43,7 @@ void **global_lock_array;
Mutex used for our internal "once" function
*********************************************************/
-void *once_mutex = NULL;
+static void *once_mutex = NULL;
/*********************************************************
@@ -112,7 +112,7 @@ int smb_thread_once(smb_thread_once_t *ponce,
int ret;
/* Lock our "once" mutex in order to test and initialize ponce */
- if (SMB_THREAD_LOCK(once_mutex, SMB_THREAD_LOCK) != 0) {
+ if (SMB_THREAD_LOCK(once_mutex) != 0) {
smb_panic("error locking 'once'");
}
@@ -132,7 +132,7 @@ int smb_thread_once(smb_thread_once_t *ponce,
}
/* Unlock the mutex */
- if (SMB_THREAD_LOCK(once_mutex, SMB_THREAD_UNLOCK) != 0) {
+ if (SMB_THREAD_UNLOCK(once_mutex) != 0) {
smb_panic("error unlocking 'once'");
}
diff --git a/lib/util/smb_threads.h b/lib/util/smb_threads.h
index 809673a..9a09616 100644
--- a/lib/util/smb_threads.h
+++ b/lib/util/smb_threads.h
@@ -22,8 +22,6 @@
typedef bool smb_thread_once_t;
#define SMB_THREAD_ONCE_INIT false
-#define SMB_THREAD_ONCE_IS_INITIALIZED(val) ((val) == true)
-#define SMB_THREAD_ONCE_INITIALIZE(val) ((val) = true)
enum smb_thread_lock_type {
SMB_THREAD_LOCK = 1,
diff --git a/lib/util/smb_threads_internal.h b/lib/util/smb_threads_internal.h
index 038c584..afd7559 100644
--- a/lib/util/smb_threads_internal.h
+++ b/lib/util/smb_threads_internal.h
@@ -30,15 +30,21 @@
}; \
} while (0)
-#define SMB_THREAD_LOCK(plock, type) \
- (global_tfp ? global_tfp->lock_mutex((plock), (type), __location__) : 0)
+#define SMB_THREAD_LOCK_INTERNAL(plock, type, location) \
+ (global_tfp ? global_tfp->lock_mutex((plock), (type), location) : 0)
+
+#define SMB_THREAD_LOCK(plock) \
+ SMB_THREAD_LOCK_INTERNAL(plock, SMB_THREAD_LOCK, __location__)
+
+#define SMB_THREAD_UNLOCK(plock) \
+ SMB_THREAD_LOCK_INTERNAL(plock, SMB_THREAD_UNLOCK, __location__)
#define SMB_THREAD_ONCE(ponce, init_fn, pdata) \
(global_tfp \
? (! *(ponce) \
? smb_thread_once((ponce), (init_fn), (pdata)) \
: 0) \
- : ((init_fn(pdata)), 0))
+ : ((init_fn(pdata)), *(ponce) = true, 1))
#define SMB_THREAD_CREATE_TLS(keyname, key) \
(global_tfp ? global_tfp->create_tls((keyname), &(key), __location__) : 0)
diff --git a/lib/util/talloc_stack.c b/lib/util/talloc_stack.c
index f1727ce..596efbf 100644
--- a/lib/util/talloc_stack.c
+++ b/lib/util/talloc_stack.c
@@ -60,14 +60,6 @@ static smb_thread_once_t ts_initialized = SMB_THREAD_ONCE_INIT;
static void talloc_stackframe_init(void * unused)
{
- if (!global_tfp) {
- /* Non-thread safe init case. */
- if (SMB_THREAD_ONCE_IS_INITIALIZED(ts_initialized)) {
- return;
- }
- SMB_THREAD_ONCE_INITIALIZE(ts_initialized);
- }
-
if (SMB_THREAD_CREATE_TLS("talloc_stackframe", global_ts)) {
smb_panic("talloc_stackframe_init create_tls failed");
}
diff --git a/source3/libsmb/libsmb_context.c b/source3/libsmb/libsmb_context.c
index f09e9c6..03cd00d 100644
--- a/source3/libsmb/libsmb_context.c
+++ b/source3/libsmb/libsmb_context.c
@@ -30,8 +30,112 @@
/*
* Is the logging working / configfile read ?
*/
-static bool SMBC_initialized;
-static unsigned int initialized_ctx_count;
+static bool SMBC_initialized = false;
+static unsigned int initialized_ctx_count = 0;
+static void *initialized_ctx_count_mutex = NULL;
+
+/*
+ * Do some module- and library-wide intializations
+ */
+static void
+SMBC_module_init(void * punused)
+{
+ bool conf_loaded = False;
+ char *home = NULL;
+ TALLOC_CTX *frame = talloc_stackframe();
+
+ load_case_tables();
+
+ setup_logging("libsmbclient", True);
+
+#if 0 /* need a place to put this (thread local storage) */
+ if (context->internal->debug_stderr) {
+ dbf = x_stderr;
+ x_setbuf(x_stderr, NULL);
+ }
+#endif
+
+ /* Here we would open the smb.conf file if needed ... */
+
+ lp_set_in_client(True);
+
+ home = getenv("HOME");
+ if (home) {
+ char *conf = NULL;
+ if (asprintf(&conf, "%s/.smb/smb.conf", home) > 0) {
+ if (lp_load(conf, True, False, False, True)) {
+ conf_loaded = True;
+ } else {
+ DEBUG(5, ("Could not load config file: %s\n",
+ conf));
+ }
+ SAFE_FREE(conf);
+ }
+ }
+
+ if (!conf_loaded) {
+ /*
+ * Well, if that failed, try the get_dyn_CONFIGFILE
+ * Which points to the standard locn, and if that
+ * fails, silently ignore it and use the internal
+ * defaults ...
+ */
+
+ if (!lp_load(get_dyn_CONFIGFILE(), True, False, False, False)) {
+ DEBUG(5, ("Could not load config file: %s\n",
+ get_dyn_CONFIGFILE()));
+ } else if (home) {
+ char *conf;
+ /*
+ * We loaded the global config file. Now lets
+ * load user-specific modifications to the
+ * global config.
+ */
+ if (asprintf(&conf,
+ "%s/.smb/smb.conf.append",
+ home) > 0) {
+ if (!lp_load(conf, True, False, False, False)) {
+ DEBUG(10,
+ ("Could not append config file: "
+ "%s\n",
+ conf));
+ }
+ SAFE_FREE(conf);
+ }
+ }
+ }
+
+ load_interfaces(); /* Load the list of interfaces ... */
+
+ reopen_logs(); /* Get logging working ... */
+
+ /*
+ * Block SIGPIPE (from lib/util_sock.c: write())
+ * It is not needed and should not stop execution
+ */
+ BlockSignals(True, SIGPIPE);
+
+ /* Create the mutex we'll use to protect initialized_ctx_count */
+ if (SMB_THREAD_CREATE_MUTEX("initialized_ctx_count_mutex",
+ initialized_ctx_count_mutex) != 0) {
+ smb_panic("SMBC_module_init: "
+ "failed to create 'initialized_ctx_count' mutex");
+ }
+
+
+ TALLOC_FREE(frame);
+}
+
+
+void
+SMBC_module_terminate(void)
+{
+ gencache_shutdown();
+ secrets_shutdown();
+ gfree_all();
+ SMBC_initialized = false;
+}
+
/*
* Get a new empty handle to fill in with your own info
@@ -41,6 +145,9 @@ smbc_new_context(void)
{
SMBCCTX *context;
+ /* The first call to this function should initialize the module */
+ SMB_THREAD_ONCE(&SMBC_initialized, SMBC_module_init, NULL);
+
/*
* All newly added context fields should be placed in
* SMBC_internal_data, not directly in SMBCCTX.
@@ -209,16 +316,24 @@ smbc_free_context(SMBCCTX *context,
SAFE_FREE(context->internal);
SAFE_FREE(context);
+ /* Protect access to the count of contexts in use */
+ if (SMB_THREAD_LOCK(initialized_ctx_count_mutex) != 0) {
+ smb_panic("error locking 'initialized_ctx_count'");
+ }
+
if (initialized_ctx_count) {
initialized_ctx_count--;
}
- if (initialized_ctx_count == 0 && SMBC_initialized) {
- gencache_shutdown();
- secrets_shutdown();
- gfree_all();
- SMBC_initialized = false;
+ if (initialized_ctx_count == 0) {
+ SMBC_module_terminate();
+ }
+
+ /* Unlock the mutex */
+ if (SMB_THREAD_UNLOCK(initialized_ctx_count_mutex) != 0) {
+ smb_panic("error unlocking 'initialized_ctx_count'");
}
+
return 0;
}
@@ -427,7 +542,6 @@ smbc_init_context(SMBCCTX *context)
{
int pid;
char *user = NULL;
- char *home = NULL;
if (!context) {
errno = EBADF;
@@ -449,88 +563,6 @@ smbc_init_context(SMBCCTX *context)
}
- if (!SMBC_initialized) {
- /*
- * Do some library-wide intializations the first time we get
- * called
- */
- bool conf_loaded = False;
- TALLOC_CTX *frame = talloc_stackframe();
-
- load_case_tables();
-
- setup_logging("libsmbclient", True);
- if (context->internal->debug_stderr) {
- dbf = x_stderr;
- x_setbuf(x_stderr, NULL);
- }
-
- /* Here we would open the smb.conf file if needed ... */
-
- lp_set_in_client(True);
-
- home = getenv("HOME");
- if (home) {
- char *conf = NULL;
- if (asprintf(&conf, "%s/.smb/smb.conf", home) > 0) {
- if (lp_load(conf, True, False, False, True)) {
- conf_loaded = True;
- } else {
- DEBUG(5, ("Could not load config file: %s\n",
- conf));
- }
- SAFE_FREE(conf);
- }
- }
-
- if (!conf_loaded) {
- /*
- * Well, if that failed, try the get_dyn_CONFIGFILE
- * Which points to the standard locn, and if that
- * fails, silently ignore it and use the internal
- * defaults ...
- */
-
- if (!lp_load(get_dyn_CONFIGFILE(), True, False, False, False)) {
- DEBUG(5, ("Could not load config file: %s\n",
- get_dyn_CONFIGFILE()));
- } else if (home) {
- char *conf;
- /*
- * We loaded the global config file. Now lets
- * load user-specific modifications to the
- * global config.
- */
- if (asprintf(&conf,
- "%s/.smb/smb.conf.append",
- home) > 0) {
- if (!lp_load(conf, True, False, False, False)) {
- DEBUG(10,
- ("Could not append config file: "
- "%s\n",
- conf));
- }
- SAFE_FREE(conf);
- }
- }
- }
-
- load_interfaces(); /* Load the list of interfaces ... */
-
- reopen_logs(); /* Get logging working ... */
-
- /*
- * Block SIGPIPE (from lib/util_sock.c: write())
- * It is not needed and should not stop execution
- */
- BlockSignals(True, SIGPIPE);
-
- /* Done with one-time initialisation */
- SMBC_initialized = true;
-
- TALLOC_FREE(frame);
- }
-
if (!smbc_getUser(context)) {
/*
* FIXME: Is this the best way to get the user info?
@@ -610,13 +642,20 @@ smbc_init_context(SMBCCTX *context)
if (smbc_getTimeout(context) > 0 && smbc_getTimeout(context) < 1000)
smbc_setTimeout(context, 1000);
- /*
- * FIXME: Should we check the function pointers here?
- */
-
context->internal->initialized = True;
+
+ /* Protect access to the count of contexts in use */
+ if (SMB_THREAD_LOCK(initialized_ctx_count_mutex) != 0) {
+ smb_panic("error locking 'initialized_ctx_count'");
+ }
+
initialized_ctx_count++;
+ /* Unlock the mutex */
+ if (SMB_THREAD_UNLOCK(initialized_ctx_count_mutex) != 0) {
+ smb_panic("error unlocking 'initialized_ctx_count'");
+ }
+
return context;
}
--
Samba Shared Repository
More information about the samba-cvs
mailing list