util_tdb: move timeout chainlock variants from source3/lib/util/util_tdb.c
[ddiss/samba.git] / source3 / lib / util_tdb.c
index ade46bf18ec86b89ebbdb9b1d445c06af76e70e0..c6c6d262772c567260306fb5b2fa07474808e10d 100644 (file)
 #undef calloc
 #undef strdup
 
-#ifdef BUILD_TDB2
-static struct flock flock_struct;
-
-/* Return a value which is none of v1, v2 or v3. */
-static inline short int invalid_value(short int v1, short int v2, short int v3)
-{
-       short int try = (v1+v2+v3)^((v1+v2+v3) << 16);
-       while (try == v1 || try == v2 || try == v3)
-               try++;
-       return try;
-}
-
-/* We invalidate in as many ways as we can, so the OS rejects it */
-static void invalidate_flock_struct(int signum)
-{
-       flock_struct.l_type = invalid_value(F_RDLCK, F_WRLCK, F_UNLCK);
-       flock_struct.l_whence = invalid_value(SEEK_SET, SEEK_CUR, SEEK_END);
-       flock_struct.l_start = -1;
-       /* A large negative. */
-       flock_struct.l_len = (((off_t)1 << (sizeof(off_t)*CHAR_BIT - 1)) + 1);
-}
-
-static int timeout_lock(int fd, int rw, off_t off, off_t len, bool waitflag,
-                       void *_timeout)
-{
-       int ret, saved_errno;
-       unsigned int timeout = *(unsigned int *)_timeout;
-
-       flock_struct.l_type = rw;
-       flock_struct.l_whence = SEEK_SET;
-       flock_struct.l_start = off;
-       flock_struct.l_len = len;
-
-       CatchSignal(SIGALRM, invalidate_flock_struct);
-       alarm(timeout);
-
-       for (;;) {
-               if (waitflag)
-                       ret = fcntl(fd, F_SETLKW, &flock_struct);
-               else
-                       ret = fcntl(fd, F_SETLK, &flock_struct);
-
-               if (ret == 0)
-                       break;
-
-               /* Not signalled?  Something else went wrong. */
-               if (flock_struct.l_len == len) {
-                       if (errno == EAGAIN || errno == EINTR)
-                               continue;
-                       saved_errno = errno;
-                       break;
-               } else {
-                       saved_errno = EINTR;
-                       break;
-               }
-       }
-
-       alarm(0);
-       errno = saved_errno;
-       return ret;
-}
-
-static int tdb_chainlock_with_timeout_internal(struct tdb_context *tdb,
-                                              TDB_DATA key,
-                                              unsigned int timeout,
-                                              int rw_type)
-{
-       union tdb_attribute locking;
-       enum TDB_ERROR ecode;
-
-       if (timeout) {
-               locking.base.attr = TDB_ATTRIBUTE_FLOCK;
-               ecode = tdb_get_attribute(tdb, &locking);
-               if (ecode != TDB_SUCCESS)
-                       return ecode;
-
-               /* Replace locking function with our own. */
-               locking.flock.data = &timeout;
-               locking.flock.lock = timeout_lock;
-
-               ecode = tdb_set_attribute(tdb, &locking);
-               if (ecode != TDB_SUCCESS)
-                       return ecode;
-       }
-       if (rw_type == F_RDLCK)
-               ecode = tdb_chainlock_read(tdb, key);
-       else
-               ecode = tdb_chainlock(tdb, key);
-
-       if (timeout) {
-               tdb_unset_attribute(tdb, TDB_ATTRIBUTE_FLOCK);
-       }
-       return ecode == TDB_SUCCESS ? 0 : -1;
-}
-#else
 /* 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;
-}
-#endif /* TDB1 */
-
-/****************************************************************************
- 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)
 {
@@ -388,7 +211,7 @@ int tdb_unpack(const uint8 *buf, int bufsize, const char *fmt, ...)
                                goto no_space;
                        *w = SVAL(buf, 0);
                        break;
-               case 'd': /* signed 32-bit integer (standard int in most systems) */
+               case 'd': /* unsigned 32-bit integer (standard int in most systems) */
                        len = 4;
                        d = va_arg(ap, uint32 *);
                        if (bufsize < len)
@@ -410,12 +233,14 @@ int tdb_unpack(const uint8 *buf, int bufsize, const char *fmt, ...)
                case 'P': /* null-terminated string */
                        /* Return malloc'ed string. */
                        ps = va_arg(ap,char **);
-                       len = strlen((const char *)buf) + 1;
+                       len = strnlen((const char *)buf, bufsize) + 1;
+                       if (bufsize < len)
+                               goto no_space;
                        *ps = SMB_STRDUP((const char *)buf);
                        break;
                case 'f': /* null-terminated string */
                        s = va_arg(ap,char *);
-                       len = strlen((const char *)buf) + 1;
+                       len = strnlen((const char *)buf, bufsize) + 1;
                        if (bufsize < len || len > sizeof(fstring))
                                goto no_space;
                        memcpy(s, buf, len);
@@ -468,14 +293,6 @@ int tdb_unpack(const uint8 *buf, int bufsize, const char *fmt, ...)
  Log tdb messages via DEBUG().
 ****************************************************************************/
 
-#ifdef BUILD_TDB2
-static void tdb_log(TDB_CONTEXT *tdb, enum tdb_log_level level,
-                   const char *message, void *unused)
-{
-       DEBUG((int)level, ("tdb(%s): %s",
-                          tdb_name(tdb) ? tdb_name(tdb) : "unnamed", message));
-}
-#else
 static void tdb_log(TDB_CONTEXT *tdb, enum tdb_debug_level level, const char *format, ...)
 {
        va_list ap;
@@ -492,7 +309,6 @@ static void tdb_log(TDB_CONTEXT *tdb, enum tdb_debug_level level, const char *fo
        DEBUG((int)level, ("tdb(%s): %s", tdb_name(tdb) ? tdb_name(tdb) : "unnamed", ptr));
        SAFE_FREE(ptr);
 }
-#endif /* TDB1 */
 
 /****************************************************************************
  Like tdb_open() but also setup a logging function that redirects to
@@ -581,64 +397,6 @@ int tdb_trans_delete(struct tdb_context *tdb, TDB_DATA key)
        return res;
 }
 
-NTSTATUS map_nt_error_from_tdb(enum TDB_ERROR err)
-{
-       NTSTATUS result = NT_STATUS_INTERNAL_ERROR;
-
-       switch (err) {
-       case TDB_SUCCESS:
-               result = NT_STATUS_OK;
-               break;
-       case TDB_ERR_CORRUPT:
-               result = NT_STATUS_INTERNAL_DB_CORRUPTION;
-               break;
-       case TDB_ERR_IO:
-               result = NT_STATUS_UNEXPECTED_IO_ERROR;
-               break;
-       case TDB_ERR_OOM:
-               result = NT_STATUS_NO_MEMORY;
-               break;
-       case TDB_ERR_EXISTS:
-               result = NT_STATUS_OBJECT_NAME_COLLISION;
-               break;
-
-       case TDB_ERR_LOCK:
-               /*
-                * TDB_ERR_LOCK is very broad, we could for example
-                * distinguish between fcntl locks and invalid lock
-                * sequences. So NT_STATUS_FILE_LOCK_CONFLICT is a
-                * compromise.
-                */
-               result = NT_STATUS_FILE_LOCK_CONFLICT;
-               break;
-
-#ifndef BUILD_TDB2
-       case TDB_ERR_NOLOCK:
-       case TDB_ERR_LOCK_TIMEOUT:
-               /*
-                * These two ones in the enum are not actually used
-                */
-               result = NT_STATUS_FILE_LOCK_CONFLICT;
-               break;
-#endif
-       case TDB_ERR_NOEXIST:
-               result = NT_STATUS_NOT_FOUND;
-               break;
-       case TDB_ERR_EINVAL:
-               result = NT_STATUS_INVALID_PARAMETER;
-               break;
-       case TDB_ERR_RDONLY:
-               result = NT_STATUS_ACCESS_DENIED;
-               break;
-#ifndef BUILD_TDB2
-       case TDB_ERR_NESTING:
-               result = NT_STATUS_INTERNAL_ERROR;
-               break;
-#endif
-       };
-       return result;
-}
-
 int tdb_data_cmp(TDB_DATA t1, TDB_DATA t2)
 {
        int ret;