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 );
96 static void *lock_detect_task( void *arg )
98 struct bdb_info *bdb = (struct bdb_info *) arg;
100 while( bdb->bi_dbenv != NULL ) {
102 sleep( bdb->bi_lock_detect_seconds );
104 rc = LOCK_DETECT( bdb->bi_dbenv, 0,
105 bdb->bi_lock_detect, NULL );
123 unsigned char *u, *c;
129 #ifdef WORDS_BIGENDIAN
130 for( i = 0; i < sizeof(ID); i++)
132 for( i = sizeof(ID)-1; i >= 0; i--)
142 bdb_db_open( BackendDB *be )
145 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
148 Debug( LDAP_DEBUG_ARGS,
150 be->be_suffix[0]->bv_val, 0, 0 );
152 /* we should check existance of dbenv_home and db_directory */
154 rc = db_env_create( &bdb->bi_dbenv, 0 );
156 Debug( LDAP_DEBUG_ANY,
157 "bdb_db_open: db_env_create failed: %s (%d)\n",
158 db_strerror(rc), rc, 0 );
162 flags = DB_INIT_MPOOL | DB_THREAD | DB_CREATE;
165 flags |= DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN | DB_RECOVER;
168 flags |= DB_INIT_CDB;
172 bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0]->bv_val );
173 bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
177 char dir[MAXPATHLEN];
178 size_t len = strlen( bdb->bi_dbenv_home );
180 strcpy( dir, bdb->bi_dbenv_home );
181 strcat( &dir[len], BDB_TMP_SUBDIR );
183 rc = bdb->bi_dbenv->set_tmp_dir( bdb->bi_dbenv, dir );
185 Debug( LDAP_DEBUG_ANY,
186 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
187 dir, db_strerror(rc), rc );
191 strcat( &dir[len], BDB_LG_SUBDIR );
193 rc = bdb->bi_dbenv->set_lg_dir( bdb->bi_dbenv, dir );
195 Debug( LDAP_DEBUG_ANY,
196 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
197 dir, db_strerror(rc), rc );
201 strcat( &dir[len], BDB_DATA_SUBDIR );
203 rc = bdb->bi_dbenv->set_data_dir( bdb->bi_dbenv, dir );
205 Debug( LDAP_DEBUG_ANY,
206 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
207 dir, db_strerror(rc), rc );
213 Debug( LDAP_DEBUG_TRACE,
214 "bdb_db_open: dbenv_open(%s)\n",
215 bdb->bi_dbenv_home, 0, 0);
217 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
220 bdb->bi_dbenv_mode );
222 Debug( LDAP_DEBUG_ANY,
223 "bdb_db_open: dbenv_open failed: %s (%d)\n",
224 db_strerror(rc), rc, 0 );
228 if( bdb->bi_dbenv_xflags != 0 ) {
229 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
230 bdb->bi_dbenv_xflags, 1);
232 Debug( LDAP_DEBUG_ANY,
233 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
234 db_strerror(rc), rc, 0 );
239 flags = DB_THREAD | DB_CREATE | bdb->bi_db_opflags;
241 bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
242 BDB_INDICES * sizeof(struct bdb_db_info *) );
244 /* open (and create) main database */
245 for( i = 0; bdbi_databases[i].name; i++ ) {
246 struct bdb_db_info *db;
248 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
250 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
252 Debug( LDAP_DEBUG_ANY,
253 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
254 bdb->bi_dbenv_home, db_strerror(rc), rc );
258 if( i == BDB_ID2ENTRY ) {
259 rc = db->bdi_db->set_bt_compare( db->bdi_db,
261 rc = db->bdi_db->set_pagesize( db->bdi_db,
262 BDB_ID2ENTRY_PAGESIZE );
265 rc = db->bdi_db->set_bt_compare( db->bdi_db,
267 #elif defined(BDB_IDL_MULTI)
268 rc = db->bdi_db->set_flags( db->bdi_db,
269 DB_DUP | DB_DUPSORT | DB_NODUPDATA );
270 rc = db->bdi_db->set_dup_compare( db->bdi_db,
273 rc = db->bdi_db->set_pagesize( db->bdi_db,
277 rc = db->bdi_db->open( db->bdi_db,
278 bdbi_databases[i].file,
279 /* bdbi_databases[i].name, */ NULL,
280 bdbi_databases[i].type,
281 bdbi_databases[i].flags | flags,
282 bdb->bi_dbenv_mode );
285 Debug( LDAP_DEBUG_ANY,
286 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
287 bdb->bi_dbenv_home, db_strerror(rc), rc );
291 db->bdi_name = bdbi_databases[i].name;
292 bdb->bi_databases[i] = db;
295 bdb->bi_databases[i] = NULL;
296 bdb->bi_ndatabases = i;
299 rc = bdb_last_id( be, NULL );
301 Debug( LDAP_DEBUG_ANY,
302 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
303 bdb->bi_dbenv_home, db_strerror(rc), rc );
307 /* <insert> open (and create) index databases */
309 rc = bdb_build_tree( be );
313 if( bdb->bi_lock_detect != DB_LOCK_NORUN ) {
314 /* listener as a separate THREAD */
315 rc = ldap_pvt_thread_create( &bdb->bi_lock_detect_tid,
316 1, lock_detect_task, bdb );
323 bdb_db_close( BackendDB *be )
326 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
327 struct bdb_db_info *db;
329 while( bdb->bi_ndatabases-- ) {
330 db = bdb->bi_databases[bdb->bi_ndatabases];
331 rc = db->bdi_db->close( db->bdi_db, 0 );
332 /* Lower numbered names are not strdup'd */
333 if( bdb->bi_ndatabases >= BDB_NDB )
334 free( db->bdi_name );
337 free( bdb->bi_databases );
338 bdb_attr_index_destroy( bdb->bi_attrs );
344 bdb_db_destroy( BackendDB *be )
347 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
349 /* force a checkpoint */
351 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
353 Debug( LDAP_DEBUG_ANY,
354 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
355 db_strerror(rc), rc, 0 );
359 /* close db environment */
360 if( bdb->bi_dbenv ) {
361 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
362 bdb->bi_dbenv = NULL;
364 Debug( LDAP_DEBUG_ANY,
365 "bdb_db_destroy: close failed: %s (%d)\n",
366 db_strerror(rc), rc, 0 );
374 #ifdef SLAPD_BDB_DYNAMIC
375 int back_bdb_LTX_init_module( int argc, char *argv[] ) {
378 memset( &bi, '\0', sizeof(bi) );
380 bi.bi_init = bdb_initialize;
385 #endif /* SLAPD_BDB_DYNAMIC */
392 static char *controls[] = {
393 LDAP_CONTROL_MANAGEDSAIT,
397 { /* version check */
398 int major, minor, patch;
399 char *version = db_version( &major, &minor, &patch );
401 if( major != DB_VERSION_MAJOR ||
402 minor != DB_VERSION_MINOR ||
403 patch < DB_VERSION_PATCH )
405 Debug( LDAP_DEBUG_ANY,
406 "bi_back_initialize: version mismatch\n"
407 "\texpected: " DB_VERSION_STRING "\n"
408 "\tgot: %s \n", version, 0, 0 );
411 Debug( LDAP_DEBUG_ANY, "bdb_initialize: %s\n",
416 db_env_set_func_malloc( ch_malloc );
417 db_env_set_func_realloc( ch_realloc );
418 db_env_set_func_free( ch_free );
421 db_env_set_func_yield( ldap_pvt_thread_yield );
423 bi->bi_controls = controls;
430 bi->bi_db_init = bdb_db_init;
431 bi->bi_db_config = bdb_db_config;
432 bi->bi_db_open = bdb_db_open;
433 bi->bi_db_close = bdb_db_close;
434 bi->bi_db_destroy = bdb_db_destroy;
436 bi->bi_op_add = bdb_add;
437 bi->bi_op_bind = bdb_bind;
438 bi->bi_op_compare = bdb_compare;
439 bi->bi_op_delete = bdb_delete;
440 bi->bi_op_modify = bdb_modify;
441 bi->bi_op_modrdn = bdb_modrdn;
442 bi->bi_op_search = bdb_search;
444 bi->bi_op_unbind = 0;
445 bi->bi_op_abandon = 0;
447 bi->bi_extended = bdb_extended;
448 bi->bi_acl_group = bdb_group;
450 bi->bi_acl_attribute = bdb_attribute;
451 bi->bi_chk_referrals = bdb_referrals;
453 bi->bi_entry_release_rw = bdb_entry_release;
456 * hooks for slap tools
458 bi->bi_tool_entry_open = bdb_tool_entry_open;
459 bi->bi_tool_entry_close = bdb_tool_entry_close;
460 bi->bi_tool_entry_first = bdb_tool_entry_next;
461 bi->bi_tool_entry_next = bdb_tool_entry_next;
462 bi->bi_tool_entry_get = bdb_tool_entry_get;
463 bi->bi_tool_entry_put = bdb_tool_entry_put;
464 bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
465 bi->bi_tool_sync = 0;
467 bi->bi_connection_init = 0;
468 bi->bi_connection_destroy = 0;