diff options
author | Rusty Russell <rusty@rustcorp.com.au> | 2012-06-22 15:07:36 +0930 |
---|---|---|
committer | Rusty Russell <rusty@rustcorp.com.au> | 2012-06-22 07:35:17 +0200 |
commit | fc9b2987611d7535b92288b26b09db19a0e2d78e (patch) | |
tree | 432223a076e40ca47465abfb665be5c01069601f /lib/util/util_tdb.c | |
parent | 02bacf1f95046163dfb5afb40f33b37ccdf1f374 (diff) | |
download | samba-fc9b2987611d7535b92288b26b09db19a0e2d78e.tar.gz samba-fc9b2987611d7535b92288b26b09db19a0e2d78e.tar.bz2 samba-fc9b2987611d7535b92288b26b09db19a0e2d78e.zip |
util_tdb: move timeout chainlock variants from source3/lib/util/util_tdb.c
We're about to use them for dbwrap.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Diffstat (limited to 'lib/util/util_tdb.c')
-rw-r--r-- | lib/util/util_tdb.c | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/lib/util/util_tdb.c b/lib/util/util_tdb.c index 41c9b50251..c005c82791 100644 --- a/lib/util/util_tdb.c +++ b/lib/util/util_tdb.c @@ -21,6 +21,7 @@ */ #include "includes.h" +#include "system/filesys.h" #include "../lib/tdb/include/tdb.h" #include "../lib/util/util_tdb.h" @@ -301,6 +302,84 @@ int32_t tdb_change_int32_atomic(struct tdb_context *tdb, const char *keystr, int return ret; } +static sig_atomic_t gotalarm; + +/*************************************************************** + Signal function to tell us we timed out. +****************************************************************/ + +static void gotalarm_sig(int signum) +{ + gotalarm = 1; +} + +/**************************************************************************** + Lock a chain with timeout (in seconds). +****************************************************************************/ + +static int tdb_chainlock_with_timeout_internal( TDB_CONTEXT *tdb, TDB_DATA key, unsigned int timeout, int rw_type) +{ + /* Allow tdb_chainlock to be interrupted by an alarm. */ + int ret; + gotalarm = 0; + + if (timeout) { + CatchSignal(SIGALRM, gotalarm_sig); + tdb_setalarm_sigptr(tdb, &gotalarm); + alarm(timeout); + } + + if (rw_type == F_RDLCK) + ret = tdb_chainlock_read(tdb, key); + else + ret = tdb_chainlock(tdb, key); + + if (timeout) { + alarm(0); + tdb_setalarm_sigptr(tdb, NULL); + CatchSignal(SIGALRM, SIG_IGN); + if (gotalarm && (ret != 0)) { + DEBUG(0,("tdb_chainlock_with_timeout_internal: alarm (%u) timed out for key %s in tdb %s\n", + timeout, key.dptr, tdb_name(tdb))); + /* TODO: If we time out waiting for a lock, it might + * be nice to use F_GETLK to get the pid of the + * process currently holding the lock and print that + * as part of the debugging message. -- mbp */ + return -1; + } + } + + return ret == 0 ? 0 : -1; +} + +/**************************************************************************** + Write lock a chain. Return non-zero if timeout or lock failed. +****************************************************************************/ + +int tdb_chainlock_with_timeout( TDB_CONTEXT *tdb, TDB_DATA key, unsigned int timeout) +{ + return tdb_chainlock_with_timeout_internal(tdb, key, timeout, F_WRLCK); +} + +int tdb_lock_bystring_with_timeout(TDB_CONTEXT *tdb, const char *keyval, + int timeout) +{ + TDB_DATA key = string_term_tdb_data(keyval); + + return tdb_chainlock_with_timeout(tdb, key, timeout); +} + +/**************************************************************************** + Read lock a chain by string. Return non-zero if timeout or lock failed. +****************************************************************************/ + +int tdb_read_lock_bystring_with_timeout(TDB_CONTEXT *tdb, const char *keyval, unsigned int timeout) +{ + TDB_DATA key = string_term_tdb_data(keyval); + + return tdb_chainlock_with_timeout_internal(tdb, key, timeout, F_RDLCK); +} + /**************************************************************************** Atomic unsigned integer change. Returns old value. To create, set initial value in *oldval. ****************************************************************************/ |