1 /* init.c - initialize bdb backend */
4 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/string.h>
16 static struct bdbi_database {
21 } bdbi_databases[] = {
22 { "id2entry" BDB_SUFFIX, "id2entry", DB_BTREE, 0 },
23 { "dn2id" BDB_SUFFIX, "dn2id", DB_BTREE, 0 },
29 bdb_destroy( BackendInfo *bi )
35 bdb_open( BackendInfo *bi )
37 /* initialize the underlying database system */
38 Debug( LDAP_DEBUG_TRACE, "bdb_open: initialize BDB backend\n",
45 bdb_close( BackendInfo *bi )
47 /* terminate the underlying database system */
53 bdb_db_init( BackendDB *be )
57 Debug( LDAP_DEBUG_ANY,
58 "bdb_db_init: Initializing BDB database\n",
61 /* allocate backend-database-specific stuff */
62 bdb = (struct bdb_info *) ch_calloc( 1, sizeof(struct bdb_info) );
64 /* DBEnv parameters */
65 bdb->bi_dbenv_home = ch_strdup( BDB_DBENV_HOME );
66 bdb->bi_dbenv_xflags = 0;
67 bdb->bi_dbenv_mode = DEFAULT_MODE;
68 bdb->bi_txn = 1; /* default to using transactions */
71 bdb->bi_lock_detect = DB_LOCK_NORUN;
74 ldap_pvt_thread_mutex_init( &bdb->bi_database_mutex );
75 ldap_pvt_thread_mutex_init( &bdb->bi_lastid_mutex );
82 static void *lock_detect_task( void *arg )
84 struct bdb_info *bdb = (struct bdb_info *) arg;
86 while( bdb->bi_dbenv != NULL ) {
88 sleep( bdb->bi_lock_detect_seconds );
90 rc = LOCK_DETECT( bdb->bi_dbenv, 0,
91 bdb->bi_lock_detect, NULL );
109 unsigned char *u, *c;
115 #ifdef WORDS_BIGENDIAN
116 for( i = 0; i < sizeof(ID); i++)
118 for( i = sizeof(ID)-1; i >= 0; i--)
128 bdb_db_open( BackendDB *be )
131 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
134 Debug( LDAP_DEBUG_ARGS,
136 be->be_suffix[0], 0, 0 );
138 /* we should check existance of dbenv_home and db_directory */
140 rc = db_env_create( &bdb->bi_dbenv, 0 );
142 Debug( LDAP_DEBUG_ANY,
143 "bdb_db_open: db_env_create failed: %s (%d)\n",
144 db_strerror(rc), rc, 0 );
148 flags = DB_INIT_MPOOL | DB_THREAD | DB_CREATE;
151 flags |= DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN | DB_RECOVER;
154 flags |= DB_INIT_CDB;
158 bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0] );
159 bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
163 char dir[MAXPATHLEN];
164 size_t len = strlen( bdb->bi_dbenv_home );
166 strcpy( dir, bdb->bi_dbenv_home );
167 strcat( &dir[len], BDB_TMP_SUBDIR );
169 rc = bdb->bi_dbenv->set_tmp_dir( bdb->bi_dbenv, dir );
171 Debug( LDAP_DEBUG_ANY,
172 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
173 dir, db_strerror(rc), rc );
177 strcat( &dir[len], BDB_LG_SUBDIR );
179 rc = bdb->bi_dbenv->set_lg_dir( bdb->bi_dbenv, dir );
181 Debug( LDAP_DEBUG_ANY,
182 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
183 dir, db_strerror(rc), rc );
187 strcat( &dir[len], BDB_DATA_SUBDIR );
189 rc = bdb->bi_dbenv->set_data_dir( bdb->bi_dbenv, dir );
191 Debug( LDAP_DEBUG_ANY,
192 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
193 dir, db_strerror(rc), rc );
199 Debug( LDAP_DEBUG_TRACE,
200 "bdb_db_open: dbenv_open(%s)\n",
201 bdb->bi_dbenv_home, 0, 0);
203 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
206 bdb->bi_dbenv_mode );
208 Debug( LDAP_DEBUG_ANY,
209 "bdb_db_open: dbenv_open failed: %s (%d)\n",
210 db_strerror(rc), rc, 0 );
214 if( bdb->bi_dbenv_xflags != 0 ) {
215 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
216 bdb->bi_dbenv_xflags, 1);
218 Debug( LDAP_DEBUG_ANY,
219 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
220 db_strerror(rc), rc, 0 );
225 flags = DB_THREAD | DB_CREATE | bdb->bi_db_opflags;
227 bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
228 BDB_INDICES * sizeof(struct bdb_db_info *) );
230 /* open (and create) main database */
231 for( i = 0; bdbi_databases[i].name; i++ ) {
232 struct bdb_db_info *db;
234 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
236 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
238 Debug( LDAP_DEBUG_ANY,
239 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
240 bdb->bi_dbenv_home, db_strerror(rc), rc );
244 if( i == BDB_ID2ENTRY ) {
245 rc = db->bdi_db->set_bt_compare( db->bdi_db,
248 rc = db->bdi_db->set_pagesize( db->bdi_db, BDB_PAGESIZE );
250 rc = db->bdi_db->open( db->bdi_db,
251 bdbi_databases[i].file,
252 /* bdbi_databases[i].name, */ NULL,
253 bdbi_databases[i].type,
254 bdbi_databases[i].flags | flags,
255 bdb->bi_dbenv_mode );
258 Debug( LDAP_DEBUG_ANY,
259 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
260 bdb->bi_dbenv_home, db_strerror(rc), rc );
264 db->bdi_name = bdbi_databases[i].name;
265 bdb->bi_databases[i] = db;
268 bdb->bi_databases[i] = NULL;
269 bdb->bi_ndatabases = i;
272 rc = bdb_last_id( be, NULL );
274 Debug( LDAP_DEBUG_ANY,
275 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
276 bdb->bi_dbenv_home, db_strerror(rc), rc );
280 /* <insert> open (and create) index databases */
283 if( bdb->bi_lock_detect != DB_LOCK_NORUN ) {
284 /* listener as a separate THREAD */
285 rc = ldap_pvt_thread_create( &bdb->bi_lock_detect_tid,
286 1, lock_detect_task, bdb );
293 bdb_db_close( BackendDB *be )
296 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
298 while( bdb->bi_ndatabases-- ) {
299 rc = bdb->bi_databases[bdb->bi_ndatabases]->bdi_db->close(
300 bdb->bi_databases[bdb->bi_ndatabases]->bdi_db, 0 );
307 bdb_db_destroy( BackendDB *be )
310 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
312 /* force a checkpoint */
314 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
316 Debug( LDAP_DEBUG_ANY,
317 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
318 db_strerror(rc), rc, 0 );
322 /* close db environment */
323 if( bdb->bi_dbenv ) {
324 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
325 bdb->bi_dbenv = NULL;
327 Debug( LDAP_DEBUG_ANY,
328 "bdb_db_destroy: close failed: %s (%d)\n",
329 db_strerror(rc), rc, 0 );
337 #ifdef SLAPD_BDB_DYNAMIC
338 int back_bdb_LTX_init_module( int argc, char *argv[] ) {
341 memset( &bi, '\0', sizeof(bi) );
343 bi.bi_init = bdb_initialize;
348 #endif /* SLAPD_BDB_DYNAMIC */
355 static char *controls[] = {
356 LDAP_CONTROL_MANAGEDSAIT,
360 { /* version check */
361 int major, minor, patch;
362 char *version = db_version( &major, &minor, &patch );
364 if( major != DB_VERSION_MAJOR ||
365 minor != DB_VERSION_MINOR ||
366 patch < DB_VERSION_PATCH )
368 Debug( LDAP_DEBUG_ANY,
369 "bi_back_initialize: version mismatch\n"
370 "\texpected: " DB_VERSION_STRING "\n"
371 "\tgot: %s \n", version, 0, 0 );
374 Debug( LDAP_DEBUG_ANY, "bdb_initialize: %s\n",
379 db_env_set_func_malloc( ch_malloc );
380 db_env_set_func_realloc( ch_realloc );
381 db_env_set_func_free( ch_free );
384 db_env_set_func_yield( ldap_pvt_thread_yield );
386 bi->bi_controls = controls;
393 bi->bi_db_init = bdb_db_init;
394 bi->bi_db_config = bdb_db_config;
395 bi->bi_db_open = bdb_db_open;
396 bi->bi_db_close = bdb_db_close;
397 bi->bi_db_destroy = bdb_db_destroy;
399 bi->bi_op_add = bdb_add;
400 bi->bi_op_bind = bdb_bind;
401 bi->bi_op_compare = bdb_compare;
402 bi->bi_op_delete = bdb_delete;
403 bi->bi_op_modify = bdb_modify;
404 bi->bi_op_modrdn = bdb_modrdn;
405 bi->bi_op_search = bdb_search;
408 bi->bi_op_unbind = bdb_unbind;
409 bi->bi_op_abandon = bdb_abandon;
411 bi->bi_extended = bdb_extended;
413 bi->bi_acl_group = bdb_group;
414 bi->bi_acl_attribute = bdb_attribute;
415 bi->bi_chk_referrals = bdb_referrals;
417 bi->bi_entry_release_rw = bdb_entry_release;
420 * hooks for slap tools
422 bi->bi_tool_entry_open = bdb_tool_entry_open;
423 bi->bi_tool_entry_close = bdb_tool_entry_close;
424 bi->bi_tool_entry_first = bdb_tool_entry_next;
425 bi->bi_tool_entry_next = bdb_tool_entry_next;
426 bi->bi_tool_entry_get = bdb_tool_entry_get;
427 bi->bi_tool_entry_put = bdb_tool_entry_put;
428 bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
429 bi->bi_tool_sync = 0;
431 bi->bi_connection_init = 0;
432 bi->bi_connection_destroy = 0;