1 /* init.c - initialize bdb backend */
4 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/string.h>
12 #include <ac/unistd.h>
13 #include <ac/stdlib.h>
18 static struct bdbi_database {
23 } bdbi_databases[] = {
24 { "id2entry" BDB_SUFFIX, "id2entry", DB_BTREE, 0 },
26 { "id2parent" BDB_SUFFIX, "id2parent", DB_BTREE, 0 },
28 { "dn2id" BDB_SUFFIX, "dn2id", DB_BTREE, 0 },
35 bdb_destroy( BackendInfo *bi )
41 bdb_open( BackendInfo *bi )
43 /* initialize the underlying database system */
44 Debug( LDAP_DEBUG_TRACE, "bdb_open: initialize BDB backend\n",
51 bdb_close( BackendInfo *bi )
53 /* terminate the underlying database system */
59 bdb_db_init( BackendDB *be )
63 Debug( LDAP_DEBUG_ANY,
64 "bdb_db_init: Initializing BDB database\n",
67 /* indicate system schema supported */
68 be->be_flags |= SLAP_BFLAG_ALIASES
69 | SLAP_BFLAG_REFERRALS
70 | SLAP_BFLAG_SUBENTRIES;
72 /* allocate backend-database-specific stuff */
73 bdb = (struct bdb_info *) ch_calloc( 1, sizeof(struct bdb_info) );
75 /* DBEnv parameters */
76 bdb->bi_dbenv_home = ch_strdup( BDB_DBENV_HOME );
77 bdb->bi_dbenv_xflags = 0;
78 bdb->bi_dbenv_mode = DEFAULT_MODE;
79 bdb->bi_txn = 1; /* default to using transactions */
82 bdb->bi_lock_detect = DB_LOCK_NORUN;
85 ldap_pvt_thread_mutex_init( &bdb->bi_database_mutex );
86 ldap_pvt_thread_mutex_init( &bdb->bi_lastid_mutex );
88 ldap_pvt_thread_rdwr_init( &bdb->bi_tree_rdwr );
95 #if 0 /* ifndef NO_THREADS */
96 static void *lock_detect_task( void *arg )
98 struct bdb_info *bdb = (struct bdb_info *) arg;
100 while( bdb->bi_dbenv != NULL ) {
103 sleep( bdb->bi_lock_detect_seconds );
105 rc = LOCK_DETECT( bdb->bi_dbenv, 0,
106 bdb->bi_lock_detect, &aborted );
112 Debug( LDAP_DEBUG_ANY,
113 "bdb_lock_detect: aborted %d locks\n",
128 unsigned char *u, *c;
134 #ifdef WORDS_BIGENDIAN
135 for( i = 0; i < sizeof(ID); i++)
137 for( i = sizeof(ID)-1; i >= 0; i--)
147 bdb_db_open( BackendDB *be )
150 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
153 Debug( LDAP_DEBUG_ARGS,
155 be->be_suffix[0]->bv_val, 0, 0 );
157 /* we should check existance of dbenv_home and db_directory */
159 rc = db_env_create( &bdb->bi_dbenv, 0 );
161 Debug( LDAP_DEBUG_ANY,
162 "bdb_db_open: db_env_create failed: %s (%d)\n",
163 db_strerror(rc), rc, 0 );
167 flags = DB_INIT_MPOOL | DB_THREAD | DB_CREATE;
170 flags |= DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN | DB_RECOVER;
173 flags |= DB_INIT_CDB;
177 bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0]->bv_val );
178 bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
180 bdb->bi_dbenv->set_lk_detect( bdb->bi_dbenv, bdb->bi_lock_detect );
185 char dir[MAXPATHLEN];
186 size_t len = strlen( bdb->bi_dbenv_home );
188 strcpy( dir, bdb->bi_dbenv_home );
189 strcat( &dir[len], BDB_TMP_SUBDIR );
191 rc = bdb->bi_dbenv->set_tmp_dir( bdb->bi_dbenv, dir );
193 Debug( LDAP_DEBUG_ANY,
194 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
195 dir, db_strerror(rc), rc );
199 strcat( &dir[len], BDB_LG_SUBDIR );
201 rc = bdb->bi_dbenv->set_lg_dir( bdb->bi_dbenv, dir );
203 Debug( LDAP_DEBUG_ANY,
204 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
205 dir, db_strerror(rc), rc );
209 strcat( &dir[len], BDB_DATA_SUBDIR );
211 rc = bdb->bi_dbenv->set_data_dir( bdb->bi_dbenv, dir );
213 Debug( LDAP_DEBUG_ANY,
214 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
215 dir, db_strerror(rc), rc );
221 Debug( LDAP_DEBUG_TRACE,
222 "bdb_db_open: dbenv_open(%s)\n",
223 bdb->bi_dbenv_home, 0, 0);
225 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
228 bdb->bi_dbenv_mode );
230 Debug( LDAP_DEBUG_ANY,
231 "bdb_db_open: dbenv_open failed: %s (%d)\n",
232 db_strerror(rc), rc, 0 );
236 if( bdb->bi_dbenv_xflags != 0 ) {
237 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
238 bdb->bi_dbenv_xflags, 1);
240 Debug( LDAP_DEBUG_ANY,
241 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
242 db_strerror(rc), rc, 0 );
247 flags = DB_THREAD | DB_CREATE | bdb->bi_db_opflags;
249 bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
250 BDB_INDICES * sizeof(struct bdb_db_info *) );
252 /* open (and create) main database */
253 for( i = 0; bdbi_databases[i].name; i++ ) {
254 struct bdb_db_info *db;
256 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
258 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
260 Debug( LDAP_DEBUG_ANY,
261 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
262 bdb->bi_dbenv_home, db_strerror(rc), rc );
266 if( i == BDB_ID2ENTRY ) {
267 rc = db->bdi_db->set_bt_compare( db->bdi_db,
269 rc = db->bdi_db->set_pagesize( db->bdi_db,
270 BDB_ID2ENTRY_PAGESIZE );
273 rc = db->bdi_db->set_bt_compare( db->bdi_db,
275 #elif defined(BDB_IDL_MULTI)
276 rc = db->bdi_db->set_flags( db->bdi_db,
277 DB_DUP | DB_DUPSORT );
278 rc = db->bdi_db->set_dup_compare( db->bdi_db,
281 rc = db->bdi_db->set_pagesize( db->bdi_db,
285 rc = db->bdi_db->open( db->bdi_db,
286 bdbi_databases[i].file,
287 /* bdbi_databases[i].name, */ NULL,
288 bdbi_databases[i].type,
289 bdbi_databases[i].flags | flags,
290 bdb->bi_dbenv_mode );
293 Debug( LDAP_DEBUG_ANY,
294 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
295 bdb->bi_dbenv_home, db_strerror(rc), rc );
299 db->bdi_name = bdbi_databases[i].name;
300 bdb->bi_databases[i] = db;
303 bdb->bi_databases[i] = NULL;
304 bdb->bi_ndatabases = i;
307 rc = bdb_last_id( be, NULL );
309 Debug( LDAP_DEBUG_ANY,
310 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
311 bdb->bi_dbenv_home, db_strerror(rc), rc );
315 /* <insert> open (and create) index databases */
317 rc = bdb_build_tree( be );
320 #if 0 /* ifndef NO_THREADS */
321 if( bdb->bi_lock_detect != DB_LOCK_NORUN ) {
322 /* listener as a separate THREAD */
323 rc = ldap_pvt_thread_create( &bdb->bi_lock_detect_tid,
324 1, lock_detect_task, bdb );
331 bdb_db_close( BackendDB *be )
334 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
335 struct bdb_db_info *db;
337 while( bdb->bi_ndatabases-- ) {
338 db = bdb->bi_databases[bdb->bi_ndatabases];
339 rc = db->bdi_db->close( db->bdi_db, 0 );
340 /* Lower numbered names are not strdup'd */
341 if( bdb->bi_ndatabases >= BDB_NDB )
342 free( db->bdi_name );
345 free( bdb->bi_databases );
346 bdb_attr_index_destroy( bdb->bi_attrs );
352 bdb_db_destroy( BackendDB *be )
355 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
357 /* close db environment */
358 if( bdb->bi_dbenv ) {
360 /* force a checkpoint */
362 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
364 Debug( LDAP_DEBUG_ANY,
365 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
366 db_strerror(rc), rc, 0 );
370 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
371 bdb->bi_dbenv = NULL;
373 Debug( LDAP_DEBUG_ANY,
374 "bdb_db_destroy: close failed: %s (%d)\n",
375 db_strerror(rc), rc, 0 );
383 #ifdef SLAPD_BDB_DYNAMIC
384 int back_bdb_LTX_init_module( int argc, char *argv[] ) {
387 memset( &bi, '\0', sizeof(bi) );
389 bi.bi_init = bdb_initialize;
394 #endif /* SLAPD_BDB_DYNAMIC */
401 static char *controls[] = {
402 LDAP_CONTROL_MANAGEDSAIT,
403 LDAP_CONTROL_SUBENTRIES,
407 { /* version check */
408 int major, minor, patch;
409 char *version = db_version( &major, &minor, &patch );
411 if( major != DB_VERSION_MAJOR ||
412 minor != DB_VERSION_MINOR ||
413 patch < DB_VERSION_PATCH )
415 Debug( LDAP_DEBUG_ANY,
416 "bi_back_initialize: version mismatch\n"
417 "\texpected: " DB_VERSION_STRING "\n"
418 "\tgot: %s \n", version, 0, 0 );
421 Debug( LDAP_DEBUG_ANY, "bdb_initialize: %s\n",
426 db_env_set_func_malloc( ch_malloc );
427 db_env_set_func_realloc( ch_realloc );
428 db_env_set_func_free( ch_free );
431 db_env_set_func_yield( ldap_pvt_thread_yield );
433 bi->bi_controls = controls;
440 bi->bi_db_init = bdb_db_init;
441 bi->bi_db_config = bdb_db_config;
442 bi->bi_db_open = bdb_db_open;
443 bi->bi_db_close = bdb_db_close;
444 bi->bi_db_destroy = bdb_db_destroy;
446 bi->bi_op_add = bdb_add;
447 bi->bi_op_bind = bdb_bind;
448 bi->bi_op_compare = bdb_compare;
449 bi->bi_op_delete = bdb_delete;
450 bi->bi_op_modify = bdb_modify;
451 bi->bi_op_modrdn = bdb_modrdn;
452 bi->bi_op_search = bdb_search;
454 bi->bi_op_unbind = 0;
455 bi->bi_op_abandon = 0;
457 bi->bi_extended = bdb_extended;
461 * these routines (and their callers) are not yet designed
462 * to work with transaction. Using them may cause deadlock.
464 bi->bi_acl_group = bdb_group;
465 bi->bi_acl_attribute = bdb_attribute;
467 bi->bi_acl_group = 0;
468 bi->bi_acl_attribute = 0;
471 bi->bi_chk_referrals = bdb_referrals;
472 bi->bi_entry_release_rw = bdb_entry_release;
475 * hooks for slap tools
477 bi->bi_tool_entry_open = bdb_tool_entry_open;
478 bi->bi_tool_entry_close = bdb_tool_entry_close;
479 bi->bi_tool_entry_first = bdb_tool_entry_next;
480 bi->bi_tool_entry_next = bdb_tool_entry_next;
481 bi->bi_tool_entry_get = bdb_tool_entry_get;
482 bi->bi_tool_entry_put = bdb_tool_entry_put;
483 bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
484 bi->bi_tool_sync = 0;
486 bi->bi_connection_init = 0;
487 bi->bi_connection_destroy = 0;