/* ldbm.c - ldap dbm compatibility routines */
+/* $OpenLDAP$ */
+/*
+ * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
+ * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
+ */
/* Patched for Berkeley DB version 2.0; /KSp; 98/02/23
*
#include "portable.h"
-#include "syslog.h"
-
#ifdef SLAPD_LDBM
#include <stdio.h>
-#include <stdlib.h>
+
+#include <ac/stdlib.h>
#include <ac/string.h>
#include <ac/errno.h>
void
ldbm_datum_free( LDBM ldbm, Datum data )
{
- free( data.dptr );
- data.dptr = NULL;
+ if ( data.dptr ) {
+ free( data.dptr );
+ memset( &data, 0, sizeof( Datum ));
+ data.dptr = NULL;
+ }
}
{
Datum dup;
+ ldbm_datum_init( dup );
+
if ( data.dsize == 0 ) {
dup.dsize = 0;
dup.dptr = NULL;
static int ldbm_initialized = 0;
-#ifndef HAVE_BERKELEY_DB2
-/* Everything but DB2 is non-reentrant */
-
+#ifndef HAVE_BERKELEY_DB2_DB_THREAD
+/* Only DB2 with DB_THREAD is thread-free */
static ldap_pvt_thread_mutex_t ldbm_big_mutex;
#define LDBM_LOCK (ldap_pvt_thread_mutex_lock(&ldbm_big_mutex))
#define LDBM_UNLOCK (ldap_pvt_thread_mutex_unlock(&ldbm_big_mutex))
+#else
+#define LDBM_LOCK ((void)0)
+#define LDBM_UNLOCK ((void)0)
+#endif
+
+#ifndef HAVE_BERKELEY_DB2
+
int ldbm_initialize( void )
{
if(ldbm_initialized++) return 1;
return( calloc( 1, size ));
}
+#ifdef LDAP_SYSLOG
+#include <ac/syslog.h>
+#endif
+
static void
ldbm_db_errcall( const char *prefix, char *message )
{
-
+#ifdef LDAP_SYSLOG
syslog( LOG_INFO, "ldbm_db_errcall(): %s %s", prefix, message );
-
+#endif
}
/* a dbEnv for BERKELEYv2 */
-DB_ENV ldbm_Env;
-
-/* Berkeley DB 2.x is reentrant */
-#define LDBM_LOCK ((void)0)
-#define LDBM_UNLOCK ((void)0)
+static DB_ENV ldbm_Env_internal;
+DB_ENV *ldbm_Env = NULL;
int ldbm_initialize( void )
{
int err;
- int envFlags;
+ u_int32_t envFlags;
if(ldbm_initialized++) return 1;
- memset( &ldbm_Env, 0, sizeof( ldbm_Env ));
+ memset( &ldbm_Env_internal, 0, sizeof( DB_ENV ));
+ ldbm_Env = &ldbm_Env_internal;
- ldbm_Env.db_errcall = ldbm_db_errcall;
- ldbm_Env.db_errpfx = "==>";
+ ldbm_Env->db_errcall = ldbm_db_errcall;
+ ldbm_Env->db_errpfx = "==>";
- envFlags = DB_CREATE | DB_THREAD;
+ envFlags = DB_CREATE
+#ifdef HAVE_BERKELEY_DB2_DB_THREAD
+ | DB_THREAD
+#endif
+ ;
- if ( ( err = db_appinit( NULL, NULL, &ldbm_Env, envFlags )) ) {
+ if ( ( err = db_appinit( NULL, NULL, ldbm_Env, envFlags )) ) {
char error[BUFSIZ];
if ( err < 0 ) {
sprintf( error, "%s\n", strerror( err ));
}
+#ifdef LDAP_SYSLOG
syslog( LOG_INFO,
"ldbm_initialize(): FATAL error in db_appinit() : %s\n",
error );
+#endif
return( 1 );
}
{
if( !ldbm_initialized ) return 1;
- db_appexit( &ldbm_Env );
+ db_appexit( ldbm_Env );
return 0;
}
DB_INFO dbinfo;
memset( &dbinfo, 0, sizeof( dbinfo ));
- if ( ldbm_Env.mp_info == NULL )
+
+#if defined( DB_VERSION_MAJOR ) && defined( DB_VERSION_MINOR ) && \
+ DB_VERSION_MAJOR == 2 && DB_VERSION_MINOR == 4
+ /*
+ * BerkeleyDB 2.4 do not allow db_cachesize
+ * to be specified if an DB_ENV is.
+ */
+#else
+ /* set db_cachesize of MPOOL is NOT being used. */
+ if (( ldbm_Env == NULL ) || ( ldbm_Env->mp_info == NULL ))
dbinfo.db_cachesize = dbcachesize;
+#endif
+
dbinfo.db_pagesize = DEFAULT_DB_PAGE_SIZE;
dbinfo.db_malloc = ldbm_malloc;
LDBM_LOCK;
- (void) db_open( name, DB_TYPE, rw, mode, &ldbm_Env, &dbinfo, &ret );
+ (void) db_open( name, DB_TYPE, rw, mode, ldbm_Env, &dbinfo, &ret );
LDBM_UNLOCK;
#else
data.flags = DB_DBT_MALLOC;
if ( (rc = (*ldbm->get)( ldbm, NULL, &key, &data, 0 )) != 0 ) {
- if ( data.dptr ) free( data.dptr );
+ ldbm_datum_free( ldbm, data );
#else
if ( (rc = (*ldbm->get)( ldbm, &key, &data, 0 )) == 0 ) {
/* Berkeley DB 1.85 don't malloc the data for us */
}
Datum
-#ifdef HAVE_BERKELEY_DB2
-ldbm_firstkey( LDBM ldbm, DBC **dbch )
-#else
-ldbm_firstkey( LDBM ldbm )
-#endif
+ldbm_firstkey( LDBM ldbm, LDBMCursor **dbch )
{
Datum key, data;
- int rc;
#ifdef HAVE_BERKELEY_DB2
- DBC *dbci;
+ LDBMCursor *dbci;
ldbm_datum_init( key );
ldbm_datum_init( data );
if ( (*ldbm->cursor)( ldbm, NULL, &dbci, 0 ))
# endif
{
+ key.dptr = NULL;
return( key );
} else {
*dbch = dbci;
if ( (*dbci->c_get)( dbci, &key, &data, DB_NEXT ) == 0 ) {
- if ( data.dptr ) {
- free( data.dptr );
- }
+ ldbm_datum_free( ldbm, data );
}
+ else {
#else
+ int rc;
LDBM_LOCK;
if ( (rc = (*ldbm->seq)( ldbm, &key, &data, R_FIRST )) == 0 ) {
key = ldbm_datum_dup( ldbm, key );
}
-#endif
else {
+#endif
key.dptr = NULL;
key.dsize = 0;
}
}
Datum
-#ifdef HAVE_BERKELEY_DB2
-ldbm_nextkey( LDBM ldbm, Datum key, DBC *dbcp )
-#else
-ldbm_nextkey( LDBM ldbm, Datum key )
-#endif
+ldbm_nextkey( LDBM ldbm, Datum key, LDBMCursor *dbcp )
{
Datum data;
- int rc;
#ifdef HAVE_BERKELEY_DB2
- void *oldKey = key.dptr;
-
ldbm_datum_init( data );
- data.flags = DB_DBT_MALLOC;
+ ldbm_datum_free( ldbm, key );
+ key.flags = data.flags = DB_DBT_MALLOC;
LDBM_LOCK;
if ( (*dbcp->c_get)( dbcp, &key, &data, DB_NEXT ) == 0 ) {
- if ( data.dptr ) free( data.dptr );
+ ldbm_datum_free( ldbm, data );
}
+ else {
#else
+ int rc;
LDBM_LOCK;
if ( (rc = (*ldbm->seq)( ldbm, &key, &data, R_NEXT )) == 0 ) {
key = ldbm_datum_dup( ldbm, key );
}
-#endif
else {
+#endif
key.dptr = NULL;
key.dsize = 0;
}
LDBM_UNLOCK;
-#ifdef HAVE_BERKELEY_DB2
- if ( oldKey ) free( oldKey );
-#endif
-
return( key );
}
#elif defined( HAVE_GDBM )
+#ifdef HAVE_ST_BLKSIZE
#include <sys/stat.h>
+#endif
/*****************************************************************
* *
ldbm_open( char *name, int rw, int mode, int dbcachesize )
{
LDBM db;
- struct stat st;
+#ifdef HAVE_ST_BLKSIZE
+ struct stat st;
+#endif
LDBM_LOCK;
LDBM_UNLOCK;
return( NULL );
}
+
+#ifdef HAVE_ST_BLKSIZE
if ( dbcachesize > 0 && stat( name, &st ) == 0 ) {
- dbcachesize = (dbcachesize / st.st_blksize);
+ dbcachesize /= st.st_blksize;
+ if( dbcachesize == 0 ) dbcachesize = 1;
+ gdbm_setopt( db, GDBM_CACHESIZE, &dbcachesize, sizeof(int) );
+ }
+#else
+ if ( dbcachesize > 0 ) {
+ dbcachesize /= 4096;
+ if( dbcachesize == 0 ) dbcachesize = 1;
gdbm_setopt( db, GDBM_CACHESIZE, &dbcachesize, sizeof(int) );
}
+#endif
LDBM_UNLOCK;
}
Datum
-ldbm_firstkey( LDBM ldbm )
+ldbm_firstkey( LDBM ldbm, LDBMCursor **dbcp )
{
Datum d;
}
Datum
-ldbm_nextkey( LDBM ldbm, Datum key )
+ldbm_nextkey( LDBM ldbm, Datum key, LDBMCursor *dbcp )
{
Datum d;
return( err );
}
+#elif HAVE_MDBM
+
+/* MMAPED DBM HASHING DATABASE */
+
+#include <ac/string.h>
+
+/* #define MDBM_DEBUG */
+
+#ifdef MDBM_DEBUG
+#include <stdio.h>
+#endif
+
+#define NO_NULL_KEY
+/* #define MDBM_CHAIN */
+
+#ifdef MDBM_CHAIN
+
+/* Use chaining */
+
+
+#define mdbm_store mdbm_chain_store
+#define mdbm_fetch mdbm_chain_fetch
+#define mdbm_delete mdbm_chain_delete
+#define mdbm_first mdbm_chain_first
+#define mdbm_next mdbm_chain_next
+
+#endif
+
+#define MDBM_PG_SZ (4*1024)
+
+/*****************************************************************
+ * *
+ * use mdbm *
+ * *
+ *****************************************************************/
+
+LDBM
+ldbm_open( char *name, int rw, int mode, int dbcachesize )
+{
+ LDBM db;
+
+#ifdef MDBM_DEBUG
+ fprintf( stdout,
+ "==>(mdbm)ldbm_open(name=%s,rw=%x,mode=%x,cachesize=%d)\n",
+ name ? name : "NULL", rw, mode, dbcachesize );
+ fflush( stdout );
+#endif
+
+ LDBM_LOCK; /* We need locking here, this is the only non-thread
+ * safe function we have.
+ */
+
+ if ( (db = mdbm_open( name, rw, mode, MDBM_PG_SZ )) == NULL ) {
+
+ LDBM_UNLOCK;
+#ifdef MDBM_DEBUG
+ fprintf( stdout, "<==(mdbm)ldbm_open(db=NULL)\n" );
+ fflush( stdout );
+#endif
+ return( NULL );
+
+ }
+
+#ifdef MDBM_CHAIN
+ (void)mdbm_set_chain(db);
+#endif
+
+ LDBM_UNLOCK;
+
+#ifdef MDBM_DEBUG
+ fprintf( stdout, "<==(mdbm)ldbm_open(db=%p)\n", db );
+ fflush( stdout );
+#endif
+
+ return( db );
+
+}
+
+
+
+
+void
+ldbm_close( LDBM ldbm )
+{
+
+ /* Open and close are not reentrant so we need to use locks here */
+
+#ifdef MDBM_DEBUG
+ fprintf( stdout,
+ "==>(mdbm)ldbm_close(db=%p)\n", ldbm );
+ fflush( stdout );
+#endif
+
+ LDBM_LOCK;
+ mdbm_close( ldbm );
+ LDBM_UNLOCK;
+
+#ifdef MDBM_DEBUG
+ fprintf( stdout, "<==(mdbm)ldbm_close()\n" );
+ fflush( stdout );
+#endif
+
+}
+
+
+
+
+void
+ldbm_sync( LDBM ldbm )
+{
+
+ /* XXX: Not sure if this is re-entrant need to check code, if so
+ * you can leave LOCKS out.
+ */
+
+ LDBM_LOCK;
+ mdbm_sync( ldbm );
+ LDBM_UNLOCK;
+
+}
+
+
+#define MAX_MDBM_RETRY 5
+
+Datum
+ldbm_fetch( LDBM ldbm, Datum key )
+{
+ Datum d;
+ kvpair k;
+ int retry = 0;
+
+ /* This hack is needed because MDBM does not take keys
+ * which begin with NULL when working in the chaining
+ * mode.
+ */
+
+ /* LDBM_LOCK; */
+
+#ifdef NO_NULL_KEY
+ k.key.dsize = key.dsize + 1;
+ k.key.dptr = malloc(k.key.dsize);
+ *(k.key.dptr) = 'l';
+ memcpy( (void *)(k.key.dptr + 1), key.dptr, key.dsize );
+#else
+ k.key = key;
+#endif
+
+ k.val.dptr = NULL;
+ k.val.dsize = 0;
+
+ do {
+
+ d = mdbm_fetch( ldbm, k );
+
+ if ( d.dsize > 0 ) {
+
+ if ( k.val.dptr != NULL ) {
+
+ free( k.val.dptr );
+
+ }
+
+ if ( (k.val.dptr = malloc( d.dsize )) != NULL ) {
+
+ k.val.dsize = d.dsize;
+ d = mdbm_fetch( ldbm, k );
+
+ } else {
+
+ d.dsize = 0;
+ break;
+
+ }
+
+ }/* if ( d.dsize > 0 ) */
+
+ } while ((d.dsize > k.val.dsize) && (++retry < MAX_MDBM_RETRY));
+
+ /* LDBM_UNLOCK; */
+
+#ifdef NO_NULL_KEY
+ free(k.key.dptr);
+#endif
+
+ return d;
+
+}
+
+
+
+
+int
+ldbm_store( LDBM ldbm, Datum key, Datum data, int flags )
+{
+ int rc;
+ Datum int_key; /* Internal key */
+
+#ifdef MDBM_DEBUG
+ fprintf( stdout,
+ "==>(mdbm)ldbm_store(db=%p, key(dptr=%p,sz=%d), data(dptr=%p,sz=%d), flags=%x)\n",
+ ldbm, key.dptr, key.dsize, data.dptr, data.dsize, flags );
+ fflush( stdout );
+#endif
+
+ /* LDBM_LOCK; */
+
+#ifdef NO_NULL_KEY
+ int_key.dsize = key.dsize + 1;
+ int_key.dptr = malloc( int_key.dsize );
+ *(int_key.dptr) = 'l'; /* Must not be NULL !*/
+ memcpy( (void *)(int_key.dptr + 1), key.dptr, key.dsize );
+#else
+ int_key = key;
+#endif
+
+ rc = mdbm_store( ldbm, int_key, data, flags );
+ if ( flags & LDBM_SYNC ) {
+ mdbm_sync( ldbm );
+ }
+
+ /* LDBM_UNLOCK; */
+
+#ifdef MDBM_DEBUG
+ fprintf( stdout, "<==(mdbm)ldbm_store(rc=%d)\n", rc );
+ fflush( stdout );
+#endif
+
+#ifdef NO_NULL_KEY
+ free(int_key.dptr);
+#endif
+
+ return( rc );
+
+}
+
+
+
+int
+ldbm_delete( LDBM ldbm, Datum key )
+{
+ int rc;
+ Datum int_key;
+
+ /* LDBM_LOCK; */
+
+#ifdef NO_NULL_KEY
+ int_key.dsize = key.dsize + 1;
+ int_key.dptr = malloc(int_key.dsize);
+ *(int_key.dptr) = 'l';
+ memcpy( (void *)(int_key.dptr + 1), key.dptr, key.dsize );
+#else
+ int_key = key;
+#endif
+
+ rc = mdbm_delete( ldbm, int_key );
+
+ /* LDBM_UNLOCK; */
+#ifdef NO_NULL_KEY
+ free(int_key.dptr);
+#endif
+
+ return( rc );
+
+}
+
+
+
+
+static Datum
+ldbm_get_next( LDBM ldbm, kvpair (*fptr)(MDBM *, kvpair) )
+{
+
+ kvpair out;
+ kvpair in;
+ Datum ret;
+ size_t sz = MDBM_PAGE_SIZE(ldbm);
+#ifdef NO_NULL_KEY
+ int delta = 1;
+#else
+ int delta = 0;
+#endif
+
+ /* LDBM_LOCK; */
+
+ in.key.dsize = sz; /* Assume first key in one pg */
+ in.key.dptr = malloc(sz);
+
+ in.val.dptr = NULL; /* Don't need data just key */
+ in.val.dsize = 0;
+
+ ret.dptr = NULL;
+ ret.dsize = NULL;
+
+ out = fptr( ldbm, in );
+
+ if (out.key.dsize > 0) {
+
+ ret.dsize = out.key.dsize - delta;
+ if ((ret.dptr = (char *)malloc(ret.dsize)) == NULL) {
+
+ ret.dsize = 0;
+ ret.dptr = NULL;
+
+ } else {
+
+ memcpy(ret.dptr, (void *)(out.key.dptr + delta),
+ ret.dsize );
+
+ }
+
+ }
+
+ /* LDBM_UNLOCK; */
+
+ free(in.key.dptr);
+
+ return ret;
+
+}
+
+
+
+
+Datum
+ldbm_firstkey( LDBM ldbm, LDBMCursor **dbcp )
+{
+
+ return ldbm_get_next( ldbm, mdbm_first );
+
+}
+
+
+
+
+Datum
+ldbm_nextkey( LDBM ldbm, Datum key, LDBMCursor *dbcp )
+{
+
+ /* XXX:
+ * don't know if this will affect the LDAP server opertaion
+ * but mdbm cannot take and input key.
+ */
+
+ return ldbm_get_next( ldbm, mdbm_next );
+
+}
+
+int
+ldbm_errno( LDBM ldbm )
+{
+ /* XXX: best we can do with current mdbm interface */
+ return( errno );
+
+}
+
+
+
+
#elif defined( HAVE_NDBM )
/*****************************************************************
* *
- * if no gdbm, fall back to using ndbm, the standard unix thing *
+ * if no gdbm or mdbm, fall back to using ndbm, the standard unix thing *
* *
*****************************************************************/
}
Datum
-ldbm_firstkey( LDBM ldbm )
+ldbm_firstkey( LDBM ldbm, LDBMCursor **dbcp )
{
Datum d;
}
Datum
-ldbm_nextkey( LDBM ldbm, Datum key )
+ldbm_nextkey( LDBM ldbm, Datum key, LDBMCursor *dbcp )
{
Datum d;