Skip to content

Commit

Permalink
tdb_compat: use tdb_open_compat.
Browse files Browse the repository at this point in the history
This is a helper for the common case of opening a tdb with a logging
function, but it doesn't do all the work, since TDB1 and TDB2's log
functions are different types.

Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell committed Jun 20, 2011
1 parent 9eaaf1f commit ca1936f
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 49 deletions.
33 changes: 28 additions & 5 deletions lib/util/tdb_wrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,31 @@
#include "lib/util/dlinklist.h"
#include "lib/util/tdb_wrap.h"

/* FIXME: TDB2 does this internally, so no need to wrap multiple opens! */
#if BUILD_TDB2
static void tdb_wrap_log(struct tdb_context *tdb,
enum tdb_log_level level,
const char *message,
void *unused)
{
int dl;
const char *name = tdb_name(tdb);

switch (level) {
case TDB_LOG_USE_ERROR:
case TDB_LOG_ERROR:
dl = 0;
break;
case TDB_LOG_WARNING:
dl = 2;
break;
default:
dl = 0;
}

DEBUG(dl, ("tdb(%s): %s", name ? name : "unnamed", message));
}
#else
/*
Log tdb messages via DEBUG().
*/
Expand Down Expand Up @@ -64,6 +89,7 @@ static void tdb_wrap_log(TDB_CONTEXT *tdb, enum tdb_debug_level level,
free(ptr);
}
}
#endif

struct tdb_wrap_private {
struct tdb_context *tdb;
Expand All @@ -89,7 +115,6 @@ static struct tdb_wrap_private *tdb_wrap_private_open(TALLOC_CTX *mem_ctx,
mode_t mode)
{
struct tdb_wrap_private *result;
struct tdb_logging_context log_ctx;

result = talloc(mem_ctx, struct tdb_wrap_private);
if (result == NULL) {
Expand All @@ -100,8 +125,6 @@ static struct tdb_wrap_private *tdb_wrap_private_open(TALLOC_CTX *mem_ctx,
goto fail;
}

log_ctx.log_fn = tdb_wrap_log;

#if _SAMBA_BUILD_ == 3
/* This #if _SAMBA_BUILD == 3 is very unfortunate, as it means
* that in the top level build, these options are not
Expand All @@ -126,8 +149,8 @@ static struct tdb_wrap_private *tdb_wrap_private_open(TALLOC_CTX *mem_ctx,
}
#endif

result->tdb = tdb_open_ex(name, hash_size, tdb_flags,
open_flags, mode, &log_ctx, NULL);
result->tdb = tdb_open_compat(name, hash_size, tdb_flags,
open_flags, mode, tdb_wrap_log, NULL);
if (result->tdb == NULL) {
goto fail;
}
Expand Down
17 changes: 11 additions & 6 deletions source3/lib/util_tdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,14 @@ 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;
Expand All @@ -388,6 +396,7 @@ 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
Expand All @@ -398,14 +407,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) {
Expand All @@ -417,8 +422,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;

Expand Down
10 changes: 6 additions & 4 deletions source3/libsmb/smb_share_modes.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,12 @@ struct smbdb_ctx *smb_share_mode_db_open(const char *db_path)

memset(smb_db, '\0', sizeof(struct smbdb_ctx));

smb_db->smb_tdb = tdb_open(db_path,
0, TDB_DEFAULT|TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH,
O_RDWR|O_CREAT,
0644);
/* FIXME: We should *never* open a tdb without logging! */
smb_db->smb_tdb = tdb_open_compat(db_path,
0, TDB_DEFAULT|TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH,
O_RDWR|O_CREAT,
0644,
NULL, NULL);

if (!smb_db->smb_tdb) {
free(smb_db);
Expand Down
5 changes: 3 additions & 2 deletions source3/printing/tests/vlp.c
Original file line number Diff line number Diff line change
Expand Up @@ -392,8 +392,9 @@ int main(int argc, char **argv)
return 1;
}

if (!(tdb = tdb_open(printdb_path, 0, 0, O_RDWR | O_CREAT,
0666))) {
/* FIXME: We should *never* open a tdb without logging! */
if (!(tdb = tdb_open_compat(printdb_path, 0, 0, O_RDWR | O_CREAT,
0666, NULL, NULL))) {
printf("%s: unable to open %s\n", argv[0], printdb_path);
return 1;
}
Expand Down
5 changes: 5 additions & 0 deletions source3/smbd/mangle_hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -773,8 +773,13 @@ const struct mangle_fns *mangle_hash_init(void)
mangle_reset();

/* Create the in-memory tdb using our custom hash function. */
#ifndef BUILD_TDB2
tdb_mangled_cache = tdb_open_ex("mangled_cache", 1031, TDB_INTERNAL,
(O_RDWR|O_CREAT), 0644, NULL, fast_string_hash);
#else
/* FIXME: We should *never* open a tdb without logging! */
tdb_mangled_cache = tdb_open("mangled_cache", TDB_INTERNAL, 0, 0, NULL);
#endif

return &mangle_hash_fns;
}
2 changes: 1 addition & 1 deletion source3/torture/mangle_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ bool torture_mangle(int dummy)
}

/* we will use an internal tdb to store the names we have used */
tdb = tdb_open(NULL, 100000, TDB_INTERNAL, 0, 0);
tdb = tdb_open_compat(NULL, 100000, TDB_INTERNAL, 0, 0, NULL, NULL);
if (!tdb) {
printf("ERROR: Failed to open tdb\n");
return False;
Expand Down
2 changes: 1 addition & 1 deletion source4/lib/ldb/ldb_tdb/ldb_index.c
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ static int ltdb_dn_list_store(struct ldb_module *module, struct ldb_dn *dn,
}

if (ltdb->idxptr->itdb == NULL) {
ltdb->idxptr->itdb = tdb_open(NULL, 1000, TDB_INTERNAL, O_RDWR, 0);
ltdb->idxptr->itdb = tdb_open_compat(NULL, 1000, TDB_INTERNAL, O_RDWR, 0, NULL, NULL);
if (ltdb->idxptr->itdb == NULL) {
return LDB_ERR_OPERATIONS_ERROR;
}
Expand Down
76 changes: 49 additions & 27 deletions source4/lib/ldb/ldb_tdb/ldb_tdb_wrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,30 @@
#include "ldb_tdb.h"
#include "dlinklist.h"

/*
the purpose of this code is to work around the braindead posix locking
rules, to allow us to have a ldb open more than once while allowing
locking to work
*/

struct ltdb_wrap {
struct ltdb_wrap *next, *prev;
struct tdb_context *tdb;
dev_t device;
ino_t inode;
};

static struct ltdb_wrap *tdb_list;

/* destroy the last connection to a tdb */
static int ltdb_wrap_destructor(struct ltdb_wrap *w)
/* FIXME: TDB2 does this internally, so no need to wrap multiple opens! */
#if BUILD_TDB2
static void ltdb_log_fn(struct tdb_context *tdb,
enum tdb_log_level level,
const char *message,
struct ldb_context *ldb)
{
tdb_close(w->tdb);
DLIST_REMOVE(tdb_list, w);
return 0;
}
enum ldb_debug_level ldb_level;
const char *name = tdb_name(tdb);

switch (level) {
case TDB_LOG_WARNING:
ldb_level = LDB_DEBUG_WARNING;
case TDB_LOG_USE_ERROR:
case TDB_LOG_ERROR:
ldb_level = LDB_DEBUG_FATAL;
break;
default:
ldb_level = LDB_DEBUG_FATAL;
}

ldb_debug(ldb, ldb_level, "ltdb: tdb(%s): %s", name, message);
}
#else /* !TDB2 */
static void ltdb_log_fn(struct tdb_context *tdb, enum tdb_debug_level level, const char *fmt, ...) PRINTF_ATTRIBUTE(3, 4);
static void ltdb_log_fn(struct tdb_context *tdb, enum tdb_debug_level level, const char *fmt, ...)
{
Expand Down Expand Up @@ -83,6 +84,32 @@ static void ltdb_log_fn(struct tdb_context *tdb, enum tdb_debug_level level, con
ldb_debug(ldb, ldb_level, "ltdb: tdb(%s): %s", name, message);
talloc_free(message);
}
#endif

/*
the purpose of this code is to work around the braindead posix locking
rules, to allow us to have a ldb open more than once while allowing
locking to work
TDB2 handles multiple opens, so we don't have this problem there.
*/

struct ltdb_wrap {
struct ltdb_wrap *next, *prev;
struct tdb_context *tdb;
dev_t device;
ino_t inode;
};

static struct ltdb_wrap *tdb_list;

/* destroy the last connection to a tdb */
static int ltdb_wrap_destructor(struct ltdb_wrap *w)
{
tdb_close(w->tdb);
DLIST_REMOVE(tdb_list, w);
return 0;
}

/*
wrapped connection to a tdb database. The caller should _not_ free
Expand All @@ -98,10 +125,6 @@ struct tdb_context *ltdb_wrap_open(TALLOC_CTX *mem_ctx,
{
struct ltdb_wrap *w;
struct stat st;
struct tdb_logging_context log_ctx;

log_ctx.log_fn = ltdb_log_fn;
log_ctx.log_private = ldb;

if (stat(path, &st) == 0) {
for (w=tdb_list;w;w=w->next) {
Expand All @@ -119,7 +142,7 @@ struct tdb_context *ltdb_wrap_open(TALLOC_CTX *mem_ctx,
return NULL;
}

w->tdb = tdb_open_ex(path, hash_size, tdb_flags, open_flags, mode, &log_ctx, NULL);
w->tdb = tdb_open_compat(path, hash_size, tdb_flags, open_flags, mode, ltdb_log_fn, ldb);
if (w->tdb == NULL) {
talloc_free(w);
return NULL;
Expand All @@ -140,4 +163,3 @@ struct tdb_context *ltdb_wrap_open(TALLOC_CTX *mem_ctx,

return w->tdb;
}

2 changes: 1 addition & 1 deletion source4/torture/basic/mangle_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ bool torture_mangle(struct torture_context *torture,
int i;

/* we will use an internal tdb to store the names we have used */
tdb = tdb_open(NULL, 100000, TDB_INTERNAL, 0, 0);
tdb = tdb_open_compat(NULL, 100000, TDB_INTERNAL, 0, 0, NULL, NULL);
if (!tdb) {
printf("ERROR: Failed to open tdb\n");
return false;
Expand Down
4 changes: 2 additions & 2 deletions testsuite/printing/psec.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ int psec_getsec(char *printer)
slprintf(tdb_path, sizeof(tdb_path) - 1, "%s/ntdrivers.tdb",
lp_lockdir());

tdb = tdb_open(tdb_path, 0, 0, O_RDONLY, 0600);
tdb = tdb_open_compat(tdb_path, 0, 0, O_RDONLY, 0600, NULL, NULL);

if (!tdb) {
printf("psec: failed to open nt drivers database: %s\n",
Expand Down Expand Up @@ -275,7 +275,7 @@ int psec_setsec(char *printer)
slprintf(tdb_path, sizeof(tdb_path) - 1, "%s/ntdrivers.tdb",
lp_lockdir());

tdb = tdb_open(tdb_path, 0, 0, O_RDWR, 0600);
tdb = tdb_open_compat(tdb_path, 0, 0, O_RDWR, 0600, NULL, NULL);

if (!tdb) {
printf("psec: failed to open nt drivers database: %s\n",
Expand Down

0 comments on commit ca1936f

Please sign in to comment.