lib: Move tdb lock timeout fns to source3
authorVolker Lendecke <vl@samba.org>
Sun, 14 Sep 2014 09:15:34 +0000 (11:15 +0200)
committerVolker Lendecke <vl@samba.org>
Thu, 18 Sep 2014 18:36:11 +0000 (20:36 +0200)
This is not the nicest code and needs to be replaced. Remove it from
common.

Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Martin Schwenke <martin@meltin.net>
lib/util/util_tdb.c
lib/util/util_tdb.h
source3/include/util_tdb.h
source3/lib/util_tdb.c

index 93df958f1ae360ba6dd55b53b8adb930fa036faf..811c2a40f79527893170ec132dedf34de46aa185 100644 (file)
@@ -317,84 +317,6 @@ 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 630bdf669bef2b0fcd3c8a16b1903181d0eaf584..12c472c36d637eaa3367f31a4799f0b4e9b966ff 100644 (file)
@@ -52,24 +52,6 @@ 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 c9e9e409c48f7d51e7e33a342747b8475ab0ba08..5a58a6b67b7952aea27688d43a469480292d29b4 100644 (file)
@@ -46,4 +46,23 @@ int tdb_data_cmp(TDB_DATA t1, TDB_DATA t2);
 
 char *tdb_data_string(TALLOC_CTX *mem_ctx, TDB_DATA d);
 
+/****************************************************************************
+ 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);
+
+
 #endif /* __TDBUTIL_H__ */
index 440c28b98d249140e6434adab7487df3b0bbac65..38c5fa0acd9cc71ee66ee6e3db4cae1ba9eb89bb 100644 (file)
@@ -451,3 +451,81 @@ done:
        talloc_free(ost);
        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);
+}