#include "ldbm.h"
#include "ldap_pvt_thread.h"
-#if defined( LDBM_USE_DBHASH ) || defined( LDBM_USE_DBBTREE )
-/*****************************************************************
- * *
- * use berkeley db hash or btree package *
- * *
- *****************************************************************/
+void
+ldbm_datum_free( LDBM ldbm, Datum data )
+{
+ free( data.dptr );
+ data.dptr = NULL;
+}
-#ifdef HAVE_BERKELEY_DB2
-/* A malloc routine for use with DB_DBT_MALLOC */
+Datum
+ldbm_datum_dup( LDBM ldbm, Datum data )
+{
+ Datum dup;
+
+ if ( data.dsize == 0 ) {
+ dup.dsize = 0;
+ dup.dptr = NULL;
+
+ return( dup );
+ }
+ dup.dsize = data.dsize;
+ if ( (dup.dptr = (char *) malloc( data.dsize )) != NULL )
+ memcpy( dup.dptr, data.dptr, data.dsize );
+
+ return( dup );
+}
+
+#ifndef HAVE_BERKELEY_DB2
+/* Everything but DB2 is non-reentrant */
+
+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))
+
+void ldbm_initialize( void )
+{
+ static int initialized = 0;
+
+ if(initialized++) return;
+
+ ldap_pvt_thread_mutex_init( &ldbm_big_mutex );
+}
+
+#else
+
void *
ldbm_malloc( size_t size )
{
return( calloc( 1, size ));
}
+static void
+ldbm_db_errcall( const char *prefix, char *message )
+{
+
+ syslog( LOG_INFO, "ldbm_db_errcall(): %s %s", prefix, message );
+
+}
+
+/* a dbEnv for BERKELEYv2 */
+static DB_ENV ldbm_Env;
+
/* Berkeley DB 2.x is reentrant */
-#define LDBM_LOCK ((void)0)
-#define LDBM_UNLOCK ((void)0)
+#define LDBM_LOCK ((void)0)
+#define LDBM_UNLOCK ((void)0)
-#else
+void ldbm_initialize( void )
+{
+ static int initialized = 0;
-/* DB 1.85 is non-reentrant */
-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))
+ int err;
+ int envFlags;
+ if(initialized++) return;
-/* we need a dummy definition for pre-2.0 DB */
-typedef void DB_ENV;
+ memset( &ldbm_Env, 0, sizeof( ldbm_Env ));
-#endif
+ ldbm_Env.db_errcall = ldbm_db_errcall;
+ ldbm_Env.db_errpfx = "==>";
+ envFlags = DB_CREATE | DB_THREAD;
-/* the old interface for tools and pre-2.0 DB */
-LDBM
-ldbm_open( char *name, int rw, int mode, int dbcachesize )
-{
- return( ldbm_open_env( name, rw, mode, dbcachesize, NULL ));
+ if ( ( err = db_appinit( NULL, NULL, &ldbm_Env, envFlags )) ) {
+ char error[BUFSIZ];
+
+ if ( err < 0 ) {
+ sprintf( error, "%ld\n", (long) err );
+ } else {
+ sprintf( error, "%s\n", strerror( err ));
+ }
+
+ syslog( LOG_INFO,
+ "ldbm_initialize(): FATAL error in db_appinit() : %s\n",
+ error );
+ exit( 1 );
+ }
}
+#endif
+
+#if defined( LDBM_USE_DBHASH ) || defined( LDBM_USE_DBBTREE )
+
+/*****************************************************************
+ * *
+ * use berkeley db hash or btree package *
+ * *
+ *****************************************************************/
-/* an enhanced interface for DB 2.0-slapd */
LDBM
-ldbm_open_env( char *name, int rw, int mode, int dbcachesize, DB_ENV *dbEnv )
+ldbm_open( char *name, int rw, int mode, int dbcachesize )
{
LDBM ret = NULL;
#ifdef HAVE_BERKELEY_DB2
- DB_INFO dbinfo;
+ DB_INFO dbinfo;
memset( &dbinfo, 0, sizeof( dbinfo ));
dbinfo.db_cachesize = dbcachesize;
dbinfo.db_pagesize = DEFAULT_DB_PAGE_SIZE;
dbinfo.db_malloc = ldbm_malloc;
- /* use the environment, but only if initialized */
- (void) db_open( name, DB_TYPE, rw, mode,
- dbEnv->db_errcall ? dbEnv : NULL, &dbinfo, &ret );
+ LDBM_LOCK;
+ (void) db_open( name, DB_TYPE, rw, mode, &ldbm_Env, &dbinfo, &ret );
+ LDBM_UNLOCK;
#else
void *info;
LDBM_UNLOCK;
}
-void
-ldbm_datum_free( LDBM ldbm, Datum data )
-{
- free( data.dptr );
-}
-
-Datum
-ldbm_datum_dup( LDBM ldbm, Datum data )
-{
- Datum dup;
-
- ldbm_datum_init( dup );
-
- if ( data.dsize == 0 ) {
- return( dup );
- }
- dup.dsize = data.dsize;
- if ( dup.dptr = (char *) malloc( data.dsize ) )
- memcpy( dup.dptr, data.dptr, data.dsize );
-
- return( dup );
-}
-
Datum
ldbm_fetch( LDBM ldbm, Datum key )
{
if ( data.dptr ) free( data.dptr );
#else
if ( (rc = (*ldbm->get)( ldbm, &key, &data, 0 )) == 0 ) {
+ /* Berkeley DB 1.85 don't malloc the data for us */
+ /* duplicate it for to ensure reentrancy */
data = ldbm_datum_dup( ldbm, data );
} else {
#endif
# if defined( DB_VERSION_MAJOR ) && defined( DB_VERSION_MINOR ) && \
DB_VERSION_MAJOR == 2 && DB_VERSION_MINOR < 6
- if ( (*ldbm->cursor)( ldbm, NULL, &dbci )) {
+ if ( (*ldbm->cursor)( ldbm, NULL, &dbci ))
# else
-
- if ( (*ldbm->cursor)( ldbm, NULL, &dbci, 0 )) {
-
+ if ( (*ldbm->cursor)( ldbm, NULL, &dbci, 0 ))
# endif
-
+ {
return( key );
} else {
*dbch = dbci;
if ( (*dbci->c_get)( dbci, &key, &data, DB_NEXT ) == 0 ) {
- if ( data.dptr ) free( data.dptr );
+ if ( data.dptr ) {
+ free( data.dptr );
+ }
+ }
#else
LDBM_LOCK;
if ( (rc = (*ldbm->seq)( ldbm, &key, &data, R_FIRST )) == 0 ) {
key = ldbm_datum_dup( ldbm, key );
+ }
#endif
- } else {
+ else {
key.dptr = NULL;
key.dsize = 0;
}
if ( (*dbcp->c_get)( dbcp, &key, &data, DB_NEXT ) == 0 ) {
if ( data.dptr ) free( data.dptr );
+ }
#else
LDBM_LOCK;
if ( (rc = (*ldbm->seq)( ldbm, &key, &data, R_NEXT )) == 0 ) {
key = ldbm_datum_dup( ldbm, key );
+ }
#endif
- } else {
+ else {
key.dptr = NULL;
key.dsize = 0;
}
#include <sys/stat.h>
-/* GDBM is non-reentrant */
-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))
-
-
/*****************************************************************
* *
* use gdbm *
LDBM_LOCK;
if ( (db = gdbm_open( name, 0, rw | GDBM_FAST, mode, 0 )) == NULL ) {
+ LDBM_UNLOCK;
return( NULL );
}
if ( dbcachesize > 0 && stat( name, &st ) == 0 ) {
LDBM_UNLOCK;
}
-void
-ldbm_datum_free( LDBM ldbm, Datum data )
-{
- free( data.dptr );
-}
-
-Datum
-ldbm_datum_dup( LDBM ldbm, Datum data )
-{
- Datum dup;
-
- if ( data.dsize == 0 ) {
- dup.dsize = 0;
- dup.dptr = NULL;
-
- return( dup );
- }
- dup.dsize = data.dsize;
- if ( (dup.dptr = (char *) malloc( data.dsize )) != NULL )
- memcpy( dup.dptr, data.dptr, data.dsize );
-
- return( dup );
-}
-
Datum
ldbm_fetch( LDBM ldbm, Datum key )
{
Datum d;
+
LDBM_LOCK;
d = gdbm_fetch( ldbm, key );
LDBM_UNLOCK;
+
return d;
}
ldbm_firstkey( LDBM ldbm )
{
Datum d;
+
LDBM_LOCK;
d = gdbm_firstkey( ldbm );
LDBM_UNLOCK;
+
return d;
}
ldbm_nextkey( LDBM ldbm, Datum key )
{
Datum d;
+
LDBM_LOCK;
d = gdbm_nextkey( ldbm, key );
LDBM_UNLOCK;
+
return d;
}
ldbm_errno( LDBM ldbm )
{
int err;
+
LDBM_LOCK;
- err = (int) gdbm_errno;
+ err = gdbm_errno;
LDBM_UNLOCK;
+
return( err );
}
* *
*****************************************************************/
-/* NDBM is non-reentrant */
-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))
-
-
/* ARGSUSED */
LDBM
ldbm_open( char *name, int rw, int mode, int dbcachesize )
return;
}
-void
-ldbm_datum_free( LDBM ldbm, Datum data )
-{
- return;
-}
-
-Datum
-ldbm_datum_dup( LDBM ldbm, Datum data )
-{
- Datum dup;
-
- if ( data.dsize == 0 ) {
- dup.dsize = 0;
- dup.dptr = NULL;
-
- return( dup );
- }
- dup.dsize = data.dsize;
- dup.dptr = (char *) malloc( data.dsize );
- if ( dup.dptr )
- memcpy( dup.dptr, data.dptr, data.dsize );
-
- return( dup );
-}
-
Datum
ldbm_fetch( LDBM ldbm, Datum key )
{