util_tdb: move timeout chainlock variants from source3/lib/util/util_tdb.c
[obnox/samba/samba-obnox.git] / source3 / lib / util_tdb.c
index e78dbec704f38dbb9603af56ff9a17ea6afb3be9..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 == -1)) {
-                       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 -1 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 -1 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)
 {
@@ -292,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)
@@ -314,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);
@@ -398,14 +319,10 @@ TDB_CONTEXT *tdb_open_log(const char *name, int hash_size, int tdb_flags,
                          int open_flags, mode_t mode)
 {
        TDB_CONTEXT *tdb;
-       struct tdb_logging_context log_ctx;
 
        if (!lp_use_mmap())
                tdb_flags |= TDB_NOMMAP;
 
-       log_ctx.log_fn = tdb_log;
-       log_ctx.log_private = NULL;
-
        if ((hash_size == 0) && (name != NULL)) {
                const char *base = strrchr_m(name, '/');
                if (base != NULL) {
@@ -417,8 +334,8 @@ TDB_CONTEXT *tdb_open_log(const char *name, int hash_size, int tdb_flags,
                hash_size = lp_parm_int(-1, "tdb_hashsize", base, 0);
        }
 
-       tdb = tdb_open_ex(name, hash_size, tdb_flags, 
-                         open_flags, mode, &log_ctx, NULL);
+       tdb = tdb_open_compat(name, hash_size, tdb_flags,
+                             open_flags, mode, tdb_log, NULL);
        if (!tdb)
                return NULL;
 
@@ -480,60 +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;
-
-       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;
-       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;
-       case TDB_ERR_NESTING:
-               result = NT_STATUS_INTERNAL_ERROR;
-               break;
-       };
-       return result;
-}
-
 int tdb_data_cmp(TDB_DATA t1, TDB_DATA t2)
 {
        int ret;