From 463b64a94c9b76f11bdf49ba97590f8ea65401d0 Mon Sep 17 00:00:00 2001 From: Kurt Zeilenga Date: Thu, 4 Feb 1999 17:41:19 +0000 Subject: [PATCH] Backout recent init/startup changes in preparation for new frontend-backend interface. Includes a number changes to share common routines between database api types. --- libraries/libldbm/ldbm.c | 239 +++++++++++++++++++-------------------- 1 file changed, 117 insertions(+), 122 deletions(-) diff --git a/libraries/libldbm/ldbm.c b/libraries/libldbm/ldbm.c index 8d219701a3..743e9b4cd8 100644 --- a/libraries/libldbm/ldbm.c +++ b/libraries/libldbm/ldbm.c @@ -21,66 +21,130 @@ #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; @@ -128,29 +192,6 @@ ldbm_sync( LDBM ldbm ) 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 ) { @@ -168,6 +209,8 @@ 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 @@ -247,27 +290,29 @@ ldbm_firstkey( LDBM ldbm ) # 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; } @@ -302,14 +347,16 @@ ldbm_nextkey( LDBM ldbm, Datum key ) 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; } @@ -333,12 +380,6 @@ ldbm_errno( LDBM ldbm ) #include -/* 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 * @@ -354,6 +395,7 @@ ldbm_open( char *name, int rw, int mode, int dbcachesize ) 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 ) { @@ -382,37 +424,15 @@ ldbm_sync( LDBM ldbm ) 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; } @@ -447,9 +467,11 @@ Datum ldbm_firstkey( LDBM ldbm ) { Datum d; + LDBM_LOCK; d = gdbm_firstkey( ldbm ); LDBM_UNLOCK; + return d; } @@ -457,9 +479,11 @@ Datum ldbm_nextkey( LDBM ldbm, Datum key ) { Datum d; + LDBM_LOCK; d = gdbm_nextkey( ldbm, key ); LDBM_UNLOCK; + return d; } @@ -467,9 +491,11 @@ int ldbm_errno( LDBM ldbm ) { int err; + LDBM_LOCK; - err = (int) gdbm_errno; + err = gdbm_errno; LDBM_UNLOCK; + return( err ); } @@ -481,12 +507,6 @@ ldbm_errno( LDBM ldbm ) * * *****************************************************************/ -/* 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 ) @@ -515,31 +535,6 @@ ldbm_sync( LDBM ldbm ) 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 ) { -- 2.39.5