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>
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 /* allocate backend-database-specific stuff */
68 bdb = (struct bdb_info *) ch_calloc( 1, sizeof(struct bdb_info) );
70 /* DBEnv parameters */
71 bdb->bi_dbenv_home = ch_strdup( BDB_DBENV_HOME );
72 bdb->bi_dbenv_xflags = 0;
73 bdb->bi_dbenv_mode = DEFAULT_MODE;
74 bdb->bi_txn = 1; /* default to using transactions */
77 bdb->bi_lock_detect = DB_LOCK_NORUN;
80 ldap_pvt_thread_mutex_init( &bdb->bi_database_mutex );
81 ldap_pvt_thread_mutex_init( &bdb->bi_lastid_mutex );
83 ldap_pvt_thread_rdwr_init( &bdb->bi_tree_rdwr );
91 static void *lock_detect_task( void *arg )
93 struct bdb_info *bdb = (struct bdb_info *) arg;
95 while( bdb->bi_dbenv != NULL ) {
97 sleep( bdb->bi_lock_detect_seconds );
99 rc = LOCK_DETECT( bdb->bi_dbenv, 0,
100 bdb->bi_lock_detect, NULL );
118 unsigned char *u, *c;
124 #ifdef WORDS_BIGENDIAN
125 for( i = 0; i < sizeof(ID); i++)
127 for( i = sizeof(ID)-1; i >= 0; i--)
137 bdb_db_open( BackendDB *be )
140 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
143 Debug( LDAP_DEBUG_ARGS,
145 be->be_suffix[0]->bv_val, 0, 0 );
147 /* we should check existance of dbenv_home and db_directory */
149 rc = db_env_create( &bdb->bi_dbenv, 0 );
151 Debug( LDAP_DEBUG_ANY,
152 "bdb_db_open: db_env_create failed: %s (%d)\n",
153 db_strerror(rc), rc, 0 );
157 flags = DB_INIT_MPOOL | DB_THREAD | DB_CREATE;
160 flags |= DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN | DB_RECOVER;
163 flags |= DB_INIT_CDB;
167 bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0]->bv_val );
168 bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
172 char dir[MAXPATHLEN];
173 size_t len = strlen( bdb->bi_dbenv_home );
175 strcpy( dir, bdb->bi_dbenv_home );
176 strcat( &dir[len], BDB_TMP_SUBDIR );
178 rc = bdb->bi_dbenv->set_tmp_dir( bdb->bi_dbenv, dir );
180 Debug( LDAP_DEBUG_ANY,
181 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
182 dir, db_strerror(rc), rc );
186 strcat( &dir[len], BDB_LG_SUBDIR );
188 rc = bdb->bi_dbenv->set_lg_dir( bdb->bi_dbenv, dir );
190 Debug( LDAP_DEBUG_ANY,
191 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
192 dir, db_strerror(rc), rc );
196 strcat( &dir[len], BDB_DATA_SUBDIR );
198 rc = bdb->bi_dbenv->set_data_dir( bdb->bi_dbenv, dir );
200 Debug( LDAP_DEBUG_ANY,
201 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
202 dir, db_strerror(rc), rc );
208 Debug( LDAP_DEBUG_TRACE,
209 "bdb_db_open: dbenv_open(%s)\n",
210 bdb->bi_dbenv_home, 0, 0);
212 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
215 bdb->bi_dbenv_mode );
217 Debug( LDAP_DEBUG_ANY,
218 "bdb_db_open: dbenv_open failed: %s (%d)\n",
219 db_strerror(rc), rc, 0 );
223 if( bdb->bi_dbenv_xflags != 0 ) {
224 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
225 bdb->bi_dbenv_xflags, 1);
227 Debug( LDAP_DEBUG_ANY,
228 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
229 db_strerror(rc), rc, 0 );
234 flags = DB_THREAD | DB_CREATE | bdb->bi_db_opflags;
236 bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
237 BDB_INDICES * sizeof(struct bdb_db_info *) );
239 /* open (and create) main database */
240 for( i = 0; bdbi_databases[i].name; i++ ) {
241 struct bdb_db_info *db;
243 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
245 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
247 Debug( LDAP_DEBUG_ANY,
248 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
249 bdb->bi_dbenv_home, db_strerror(rc), rc );
253 if( i == BDB_ID2ENTRY ) {
254 rc = db->bdi_db->set_bt_compare( db->bdi_db,
256 rc = db->bdi_db->set_pagesize( db->bdi_db,
257 BDB_ID2ENTRY_PAGESIZE );
260 rc = db->bdi_db->set_bt_compare( db->bdi_db,
262 #elif defined(BDB_IDL_MULTI)
263 rc = db->bdi_db->set_flags( db->bdi_db,
264 DB_DUP | DB_DUPSORT | DB_NODUPDATA );
265 rc = db->bdi_db->set_dup_compare( db->bdi_db,
268 rc = db->bdi_db->set_pagesize( db->bdi_db,
272 rc = db->bdi_db->open( db->bdi_db,
273 bdbi_databases[i].file,
274 /* bdbi_databases[i].name, */ NULL,
275 bdbi_databases[i].type,
276 bdbi_databases[i].flags | flags,
277 bdb->bi_dbenv_mode );
280 Debug( LDAP_DEBUG_ANY,
281 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
282 bdb->bi_dbenv_home, db_strerror(rc), rc );
286 db->bdi_name = bdbi_databases[i].name;
287 bdb->bi_databases[i] = db;
290 bdb->bi_databases[i] = NULL;
291 bdb->bi_ndatabases = i;
294 rc = bdb_last_id( be, NULL );
296 Debug( LDAP_DEBUG_ANY,
297 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
298 bdb->bi_dbenv_home, db_strerror(rc), rc );
302 /* <insert> open (and create) index databases */
304 rc = bdb_build_tree( be );
308 if( bdb->bi_lock_detect != DB_LOCK_NORUN ) {
309 /* listener as a separate THREAD */
310 rc = ldap_pvt_thread_create( &bdb->bi_lock_detect_tid,
311 1, lock_detect_task, bdb );
318 bdb_db_close( BackendDB *be )
321 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
322 struct bdb_db_info *db;
324 while( bdb->bi_ndatabases-- ) {
325 db = bdb->bi_databases[bdb->bi_ndatabases];
326 rc = db->bdi_db->close( db->bdi_db, 0 );
327 /* Lower numbered names are not strdup'd */
328 if( bdb->bi_ndatabases >= BDB_NDB )
329 free( db->bdi_name );
332 free( bdb->bi_databases );
333 bdb_attr_index_destroy( bdb->bi_attrs );
339 bdb_db_destroy( BackendDB *be )
342 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
344 /* force a checkpoint */
346 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
348 Debug( LDAP_DEBUG_ANY,
349 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
350 db_strerror(rc), rc, 0 );
354 /* close db environment */
355 if( bdb->bi_dbenv ) {
356 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
357 bdb->bi_dbenv = NULL;
359 Debug( LDAP_DEBUG_ANY,
360 "bdb_db_destroy: close failed: %s (%d)\n",
361 db_strerror(rc), rc, 0 );
369 #ifdef SLAPD_BDB_DYNAMIC
370 int back_bdb_LTX_init_module( int argc, char *argv[] ) {
373 memset( &bi, '\0', sizeof(bi) );
375 bi.bi_init = bdb_initialize;
380 #endif /* SLAPD_BDB_DYNAMIC */
387 static char *controls[] = {
388 LDAP_CONTROL_MANAGEDSAIT,
392 { /* version check */
393 int major, minor, patch;
394 char *version = db_version( &major, &minor, &patch );
396 if( major != DB_VERSION_MAJOR ||
397 minor != DB_VERSION_MINOR ||
398 patch < DB_VERSION_PATCH )
400 Debug( LDAP_DEBUG_ANY,
401 "bi_back_initialize: version mismatch\n"
402 "\texpected: " DB_VERSION_STRING "\n"
403 "\tgot: %s \n", version, 0, 0 );
406 Debug( LDAP_DEBUG_ANY, "bdb_initialize: %s\n",
411 db_env_set_func_malloc( ch_malloc );
412 db_env_set_func_realloc( ch_realloc );
413 db_env_set_func_free( ch_free );
416 db_env_set_func_yield( ldap_pvt_thread_yield );
418 bi->bi_controls = controls;
425 bi->bi_db_init = bdb_db_init;
426 bi->bi_db_config = bdb_db_config;
427 bi->bi_db_open = bdb_db_open;
428 bi->bi_db_close = bdb_db_close;
429 bi->bi_db_destroy = bdb_db_destroy;
431 bi->bi_op_add = bdb_add;
432 bi->bi_op_bind = bdb_bind;
433 bi->bi_op_compare = bdb_compare;
434 bi->bi_op_delete = bdb_delete;
435 bi->bi_op_modify = bdb_modify;
436 bi->bi_op_modrdn = bdb_modrdn;
437 bi->bi_op_search = bdb_search;
440 bi->bi_op_unbind = bdb_unbind;
441 bi->bi_op_abandon = bdb_abandon;
443 bi->bi_extended = bdb_extended;
444 bi->bi_acl_group = bdb_group;
446 bi->bi_acl_attribute = bdb_attribute;
447 bi->bi_chk_referrals = bdb_referrals;
449 bi->bi_entry_release_rw = bdb_entry_release;
452 * hooks for slap tools
454 bi->bi_tool_entry_open = bdb_tool_entry_open;
455 bi->bi_tool_entry_close = bdb_tool_entry_close;
456 bi->bi_tool_entry_first = bdb_tool_entry_next;
457 bi->bi_tool_entry_next = bdb_tool_entry_next;
458 bi->bi_tool_entry_get = bdb_tool_entry_get;
459 bi->bi_tool_entry_put = bdb_tool_entry_put;
460 bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
461 bi->bi_tool_sync = 0;
463 bi->bi_connection_init = 0;
464 bi->bi_connection_destroy = 0;