From 1ebfc66b2c145289d1e1314e8415d9e3c6f405ae Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Mon, 10 Mar 2008 21:08:29 +0100 Subject: Use a separate tdb for mutexes Another preparation to convert secrets.c to dbwrap: The dbwrap API does not provide a sane tdb_lock_with_timeout abstraction. In the clustered case the DC mutex is needed per-node anyway, so it is perfectly fine to use a local mutex only. (This used to be commit f94a63cd8f94490780ad9331da229c0bcb2ca5d6) --- source3/lib/server_mutex.c | 57 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 40 insertions(+), 17 deletions(-) (limited to 'source3/lib/server_mutex.c') diff --git a/source3/lib/server_mutex.c b/source3/lib/server_mutex.c index 2700aa103b..43c0de1975 100644 --- a/source3/lib/server_mutex.c +++ b/source3/lib/server_mutex.c @@ -28,28 +28,51 @@ This locking allows smbd's mutlithread architecture to look like the single-connection that NT makes. */ -static char *mutex_server_name; +struct named_mutex { + struct tdb_wrap *tdb; + char *name; +}; -bool grab_server_mutex(const char *name) +static int unlock_named_mutex(struct named_mutex *mutex) { - mutex_server_name = SMB_STRDUP(name); - if (!mutex_server_name) { - DEBUG(0,("grab_server_mutex: malloc failed for %s\n", name)); - return False; + tdb_unlock_bystring(mutex->tdb->tdb, mutex->name); + return 0; +} + +struct named_mutex *grab_named_mutex(TALLOC_CTX *mem_ctx, const char *name, + int timeout) +{ + struct named_mutex *result; + + result = talloc(mem_ctx, struct named_mutex); + if (result == NULL) { + DEBUG(0, ("talloc failed\n")); + return NULL; } - if (!secrets_named_mutex(mutex_server_name, 10)) { - DEBUG(10,("grab_server_mutex: failed for %s\n", name)); - SAFE_FREE(mutex_server_name); - return False; + + result->name = talloc_strdup(result, name); + if (result->name == NULL) { + DEBUG(0, ("talloc failed\n")); + TALLOC_FREE(result); + return NULL; } - return True; -} + result->tdb = tdb_wrap_open(result, lock_path("mutex.tdb"), 0, + TDB_DEFAULT, O_RDWR|O_CREAT, 0600); + if (result->tdb == NULL) { + DEBUG(1, ("Could not open mutex.tdb: %s\n", + strerror(errno))); + TALLOC_FREE(result); + return NULL; + } -void release_server_mutex(void) -{ - if (mutex_server_name) { - secrets_named_mutex_release(mutex_server_name); - SAFE_FREE(mutex_server_name); + if (tdb_lock_bystring_with_timeout(result->tdb->tdb, name, + timeout) == -1) { + DEBUG(1, ("Could not get the lock for %s\n", name)); + TALLOC_FREE(result); + return NULL; } + + talloc_set_destructor(result, unlock_named_mutex); + return result; } -- cgit