summaryrefslogtreecommitdiff
path: root/source3/lib/util_tdb.c
diff options
context:
space:
mode:
Diffstat (limited to 'source3/lib/util_tdb.c')
-rw-r--r--source3/lib/util_tdb.c164
1 files changed, 13 insertions, 151 deletions
diff --git a/source3/lib/util_tdb.c b/source3/lib/util_tdb.c
index 654a538a88..8bfc75f18b 100644
--- a/source3/lib/util_tdb.c
+++ b/source3/lib/util_tdb.c
@@ -31,87 +31,6 @@
/* 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;
-}
-
-/****************************************************************************
- 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)
{
@@ -231,7 +150,7 @@ bool tdb_pack_append(TALLOC_CTX *mem_ctx, uint8 **buf, size_t *len,
va_end(ap);
if (mem_ctx != NULL) {
- *buf = TALLOC_REALLOC_ARRAY(mem_ctx, *buf, uint8,
+ *buf = talloc_realloc(mem_ctx, *buf, uint8,
(*len) + len1);
} else {
*buf = SMB_REALLOC_ARRAY(*buf, uint8, (*len) + len1);
@@ -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,17 @@ 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);
+ if (*ps == NULL) {
+ goto no_space;
+ }
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 +322,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 +337,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;
@@ -442,9 +362,7 @@ int tdb_trans_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
if ((res = tdb_store(tdb, key, dbuf, flag)) != 0) {
DEBUG(10, ("tdb_store failed\n"));
- if (tdb_transaction_cancel(tdb) != 0) {
- smb_panic("Cancelling transaction failed");
- }
+ tdb_transaction_cancel(tdb);
return res;
}
@@ -471,9 +389,7 @@ int tdb_trans_delete(struct tdb_context *tdb, TDB_DATA key)
if ((res = tdb_delete(tdb, key)) != 0) {
DEBUG(10, ("tdb_delete failed\n"));
- if (tdb_transaction_cancel(tdb) != 0) {
- smb_panic("Cancelling transaction failed");
- }
+ tdb_transaction_cancel(tdb);
return res;
}
@@ -484,60 +400,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;