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 */
83 bdb->bi_lock_detect = DB_LOCK_NORUN;
85 bdb->bi_lock_detect = DB_LOCK_DEFAULT;
89 ldap_pvt_thread_mutex_init( &bdb->bi_database_mutex );
90 ldap_pvt_thread_mutex_init( &bdb->bi_lastid_mutex );
92 ldap_pvt_thread_rdwr_init( &bdb->bi_tree_rdwr );
99 #if 0 /* ifndef NO_THREADS */
100 static void *lock_detect_task( void *arg )
102 struct bdb_info *bdb = (struct bdb_info *) arg;
104 while( bdb->bi_dbenv != NULL ) {
107 sleep( bdb->bi_lock_detect_seconds );
109 rc = LOCK_DETECT( bdb->bi_dbenv, 0,
110 bdb->bi_lock_detect, &aborted );
116 Debug( LDAP_DEBUG_ANY,
117 "bdb_lock_detect: aborted %d locks\n",
132 unsigned char *u, *c;
138 #ifdef WORDS_BIGENDIAN
139 for( i = 0; i < sizeof(ID); i++)
141 for( i = sizeof(ID)-1; i >= 0; i--)
151 bdb_db_open( BackendDB *be )
154 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
157 Debug( LDAP_DEBUG_ARGS,
159 be->be_suffix[0]->bv_val, 0, 0 );
161 /* we should check existance of dbenv_home and db_directory */
163 rc = db_env_create( &bdb->bi_dbenv, 0 );
165 Debug( LDAP_DEBUG_ANY,
166 "bdb_db_open: db_env_create failed: %s (%d)\n",
167 db_strerror(rc), rc, 0 );
171 flags = DB_INIT_MPOOL | DB_THREAD | DB_CREATE;
174 flags |= DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN | DB_RECOVER;
177 flags |= DB_INIT_CDB;
181 bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0]->bv_val );
182 bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
184 bdb->bi_dbenv->set_lk_detect( bdb->bi_dbenv, bdb->bi_lock_detect );
189 char dir[MAXPATHLEN];
190 size_t len = strlen( bdb->bi_dbenv_home );
192 strcpy( dir, bdb->bi_dbenv_home );
193 strcat( &dir[len], BDB_TMP_SUBDIR );
195 rc = bdb->bi_dbenv->set_tmp_dir( bdb->bi_dbenv, dir );
197 Debug( LDAP_DEBUG_ANY,
198 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
199 dir, db_strerror(rc), rc );
203 strcat( &dir[len], BDB_LG_SUBDIR );
205 rc = bdb->bi_dbenv->set_lg_dir( bdb->bi_dbenv, dir );
207 Debug( LDAP_DEBUG_ANY,
208 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
209 dir, db_strerror(rc), rc );
213 strcat( &dir[len], BDB_DATA_SUBDIR );
215 rc = bdb->bi_dbenv->set_data_dir( bdb->bi_dbenv, dir );
217 Debug( LDAP_DEBUG_ANY,
218 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
219 dir, db_strerror(rc), rc );
225 Debug( LDAP_DEBUG_TRACE,
226 "bdb_db_open: dbenv_open(%s)\n",
227 bdb->bi_dbenv_home, 0, 0);
229 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
232 bdb->bi_dbenv_mode );
234 Debug( LDAP_DEBUG_ANY,
235 "bdb_db_open: dbenv_open failed: %s (%d)\n",
236 db_strerror(rc), rc, 0 );
240 if( bdb->bi_dbenv_xflags != 0 ) {
241 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
242 bdb->bi_dbenv_xflags, 1);
244 Debug( LDAP_DEBUG_ANY,
245 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
246 db_strerror(rc), rc, 0 );
251 flags = DB_THREAD | DB_CREATE | bdb->bi_db_opflags;
253 bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
254 BDB_INDICES * sizeof(struct bdb_db_info *) );
256 /* open (and create) main database */
257 for( i = 0; bdbi_databases[i].name; i++ ) {
258 struct bdb_db_info *db;
260 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
262 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
264 Debug( LDAP_DEBUG_ANY,
265 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
266 bdb->bi_dbenv_home, db_strerror(rc), rc );
270 if( i == BDB_ID2ENTRY ) {
271 rc = db->bdi_db->set_bt_compare( db->bdi_db,
273 rc = db->bdi_db->set_pagesize( db->bdi_db,
274 BDB_ID2ENTRY_PAGESIZE );
277 rc = db->bdi_db->set_bt_compare( db->bdi_db,
279 #elif defined(BDB_IDL_MULTI)
280 rc = db->bdi_db->set_flags( db->bdi_db,
281 DB_DUP | DB_DUPSORT );
282 rc = db->bdi_db->set_dup_compare( db->bdi_db,
285 rc = db->bdi_db->set_pagesize( db->bdi_db,
289 rc = db->bdi_db->open( db->bdi_db,
290 bdbi_databases[i].file,
291 /* bdbi_databases[i].name, */ NULL,
292 bdbi_databases[i].type,
293 bdbi_databases[i].flags | flags,
294 bdb->bi_dbenv_mode );
297 Debug( LDAP_DEBUG_ANY,
298 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
299 bdb->bi_dbenv_home, db_strerror(rc), rc );
303 db->bdi_name = bdbi_databases[i].name;
304 bdb->bi_databases[i] = db;
307 bdb->bi_databases[i] = NULL;
308 bdb->bi_ndatabases = i;
311 rc = bdb_last_id( be, NULL );
313 Debug( LDAP_DEBUG_ANY,
314 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
315 bdb->bi_dbenv_home, db_strerror(rc), rc );
319 /* <insert> open (and create) index databases */
321 rc = bdb_build_tree( be );
324 #if 0 /* ifndef NO_THREADS */
325 if( bdb->bi_lock_detect != DB_LOCK_NORUN ) {
326 /* listener as a separate THREAD */
327 rc = ldap_pvt_thread_create( &bdb->bi_lock_detect_tid,
328 1, lock_detect_task, bdb );
335 bdb_db_close( BackendDB *be )
338 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
339 struct bdb_db_info *db;
341 while( bdb->bi_ndatabases-- ) {
342 db = bdb->bi_databases[bdb->bi_ndatabases];
343 rc = db->bdi_db->close( db->bdi_db, 0 );
344 /* Lower numbered names are not strdup'd */
345 if( bdb->bi_ndatabases >= BDB_NDB )
346 free( db->bdi_name );
349 free( bdb->bi_databases );
350 bdb_attr_index_destroy( bdb->bi_attrs );
356 bdb_db_destroy( BackendDB *be )
359 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
361 /* close db environment */
362 if( bdb->bi_dbenv ) {
363 /* force a checkpoint */
365 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
367 Debug( LDAP_DEBUG_ANY,
368 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
369 db_strerror(rc), rc, 0 );
373 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
374 bdb->bi_dbenv = NULL;
376 Debug( LDAP_DEBUG_ANY,
377 "bdb_db_destroy: close failed: %s (%d)\n",
378 db_strerror(rc), rc, 0 );
386 #ifdef SLAPD_BDB_DYNAMIC
387 int back_bdb_LTX_init_module( int argc, char *argv[] ) {
390 memset( &bi, '\0', sizeof(bi) );
392 bi.bi_init = bdb_initialize;
397 #endif /* SLAPD_BDB_DYNAMIC */
404 static char *controls[] = {
405 LDAP_CONTROL_MANAGEDSAIT,
406 LDAP_CONTROL_SUBENTRIES,
410 { /* version check */
411 int major, minor, patch;
412 char *version = db_version( &major, &minor, &patch );
414 if( major != DB_VERSION_MAJOR ||
415 minor != DB_VERSION_MINOR ||
416 patch < DB_VERSION_PATCH )
418 Debug( LDAP_DEBUG_ANY,
419 "bi_back_initialize: version mismatch\n"
420 "\texpected: " DB_VERSION_STRING "\n"
421 "\tgot: %s \n", version, 0, 0 );
424 Debug( LDAP_DEBUG_ANY, "bdb_initialize: %s\n",
429 db_env_set_func_malloc( ch_malloc );
430 db_env_set_func_realloc( ch_realloc );
431 db_env_set_func_free( ch_free );
434 db_env_set_func_yield( ldap_pvt_thread_yield );
436 bi->bi_controls = controls;
443 bi->bi_db_init = bdb_db_init;
444 bi->bi_db_config = bdb_db_config;
445 bi->bi_db_open = bdb_db_open;
446 bi->bi_db_close = bdb_db_close;
447 bi->bi_db_destroy = bdb_db_destroy;
449 bi->bi_op_add = bdb_add;
450 bi->bi_op_bind = bdb_bind;
451 bi->bi_op_compare = bdb_compare;
452 bi->bi_op_delete = bdb_delete;
453 bi->bi_op_modify = bdb_modify;
454 bi->bi_op_modrdn = bdb_modrdn;
455 bi->bi_op_search = bdb_search;
457 bi->bi_op_unbind = 0;
458 bi->bi_op_abandon = 0;
460 bi->bi_extended = bdb_extended;
464 * these routines (and their callers) are not yet designed
465 * to work with transaction. Using them may cause deadlock.
467 bi->bi_acl_group = bdb_group;
468 bi->bi_acl_attribute = bdb_attribute;
470 bi->bi_acl_group = 0;
471 bi->bi_acl_attribute = 0;
474 bi->bi_chk_referrals = bdb_referrals;
475 bi->bi_entry_release_rw = bdb_entry_release;
478 * hooks for slap tools
480 bi->bi_tool_entry_open = bdb_tool_entry_open;
481 bi->bi_tool_entry_close = bdb_tool_entry_close;
482 bi->bi_tool_entry_first = bdb_tool_entry_next;
483 bi->bi_tool_entry_next = bdb_tool_entry_next;
484 bi->bi_tool_entry_get = bdb_tool_entry_get;
485 bi->bi_tool_entry_put = bdb_tool_entry_put;
486 bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
487 bi->bi_tool_sync = 0;
489 bi->bi_connection_init = 0;
490 bi->bi_connection_destroy = 0;