util_tdb: move timeout chainlock variants from source3/lib/util/util_tdb.c
authorRusty Russell <rusty@rustcorp.com.au>
Fri, 22 Jun 2012 05:37:36 +0000 (15:07 +0930)
committerRusty Russell <rusty@rustcorp.com.au>
Fri, 22 Jun 2012 05:35:17 +0000 (07:35 +0200)
We're about to use them for dbwrap.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
lib/util/util_tdb.c
lib/util/util_tdb.h
lib/util/wscript_build
source3/Makefile.in
source3/include/util_tdb.h
source3/lib/util_tdb.c

index 41c9b50251c31d47b62ecc154c1222c1fe0fab30..c005c827919b77cc59edf781cc8fe92cd92620b0 100644 (file)
@@ -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. 
 ****************************************************************************/
index 0b6f3f18a42debd2e673a0624b0f7cc098ace837..d8a23330353127dd462e314cbad3385cabe79c3b 100644 (file)
@@ -51,6 +51,24 @@ int tdb_read_lock_bystring(struct tdb_context *tdb, const char *keyval);
 ****************************************************************************/
 void tdb_read_unlock_bystring(struct tdb_context *tdb, const char *keyval);
 
+/****************************************************************************
+ Lock a chain, with timeout.
+****************************************************************************/
+int tdb_chainlock_with_timeout( struct tdb_context *tdb, TDB_DATA key,
+                               unsigned int timeout);
+
+/****************************************************************************
+ Lock a chain by string, with timeout Return non-zero if lock failed.
+****************************************************************************/
+int tdb_lock_bystring_with_timeout(struct tdb_context *tdb, const char *keyval,
+                                  int timeout);
+
+/****************************************************************************
+ Readlock a chain by string, with timeout Return non-zero if lock failed.
+****************************************************************************/
+int tdb_read_lock_bystring_with_timeout(TDB_CONTEXT *tdb, const char *keyval,
+                                       unsigned int timeout);
+
 /****************************************************************************
  Fetch a int32_t value by a arbitrary blob key, return -1 if not found.
  Output is int32_t in native byte order.
index a955ab6a481f2efca616a003c1015fbb520e15ca..82943a08f23609422f5ae149e6f648c541a552de 100755 (executable)
@@ -41,7 +41,7 @@ bld.SAMBA_SUBSYSTEM('UNIX_PRIVS',
 bld.SAMBA_LIBRARY('util_tdb',
        source='util_tdb.c',
        local_include=False,
-       public_deps='tdb talloc',
+       public_deps='tdb talloc samba-util',
        private_library=True
        )
 
index f4b00a7ff035617e0f8dc5c6d539e104ab9b8781..d66f6bfd99e66602683a5a1485ebe500763a7716 100644 (file)
@@ -1567,7 +1567,30 @@ NTLM_AUTH_OBJ = ${NTLM_AUTH_OBJ1} \
 
 
 VLP_OBJ = printing/tests/vlp.o \
-         ../lib/util/util_tdb.o \
+          ../lib/util/util_tdb.o \
+          ../lib/util/signal.o \
+          ../lib/util/debug.o \
+          ../lib/util/util_strlist.o \
+          ../lib/util/fault.o \
+          ../lib/util/become_daemon.o \
+          ../lib/util/substitute.o \
+          lib/util_sec.o \
+          ../lib/util/time.o \
+          ../lib/util/charset/util_str.o \
+          ../lib/util/charset/pull_push.o \
+          ../lib/util/charset/util_unistr_w.o \
+          ../lib/util/charset/codepoints.o \
+          ../lib/util/charset/iconv.o \
+          ../lib/util/charset/weird.o \
+          ../lib/util/charset/convert_string.o \
+          ../lib/util/talloc_stack.o \
+          ../lib/util/smb_threads.o \
+          ../lib/util/xfile.o \
+          ../lib/util/util_file.o \
+          ../lib/util/util.o \
+          ../lib/util/util_str_common.o \
+          ../lib/util/data_blob.o \
+          ../dynconfig/dynconfig.o \
          $(LIBSAMBAUTIL_OBJ) \
          param/util.o
 
index f127cea1ea5b7d7cd49e1d3149911040b3cf622a..e350413cf9f05d62dfe6f2ce845b1425fed2b205 100644 (file)
 #include "../libcli/util/ntstatus.h" /* for map_nt_error_from_tdb() */
 #include "../../lib/util/util_tdb.h"
 
-int tdb_chainlock_with_timeout( struct tdb_context *tdb, TDB_DATA key,
-                               unsigned int timeout);
-int tdb_lock_bystring_with_timeout(struct tdb_context *tdb, const char *keyval,
-                                  int timeout);
-int tdb_read_lock_bystring_with_timeout(TDB_CONTEXT *tdb, const char *keyval,
-                                       unsigned int timeout);
-
 int tdb_trans_store_bystring(TDB_CONTEXT *tdb, const char *keystr,
                             TDB_DATA data, int flags);
 int tdb_trans_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
index 552a914eb4123d39ae29278b5d0d39dcdf679716..c6c6d262772c567260306fb5b2fa07474808e10d 100644 (file)
 /* these are little tdb utility functions that are meant to make
    dealing with a tdb database a little less cumbersome in Samba */
 
-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);
-}
-
-
-
-
 int tdb_trans_store_bystring(TDB_CONTEXT *tdb, const char *keystr,
                             TDB_DATA data, int flags)
 {