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 );
110 memcpy(&usr, usrkey->data, sizeof(ID));
111 memcpy(&cur, curkey->data, sizeof(ID));
116 bdb_db_open( BackendDB *be )
119 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
122 Debug( LDAP_DEBUG_ARGS,
124 be->be_suffix[0], 0, 0 );
126 /* we should check existance of dbenv_home and db_directory */
128 rc = db_env_create( &bdb->bi_dbenv, 0 );
130 Debug( LDAP_DEBUG_ANY,
131 "bdb_db_open: db_env_create failed: %s (%d)\n",
132 db_strerror(rc), rc, 0 );
136 flags = DB_INIT_MPOOL | DB_THREAD | DB_CREATE;
139 flags |= DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN | DB_RECOVER;
142 flags |= DB_INIT_CDB;
146 bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0] );
147 bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
151 char dir[MAXPATHLEN];
152 size_t len = strlen( bdb->bi_dbenv_home );
154 strcpy( dir, bdb->bi_dbenv_home );
155 strcat( &dir[len], BDB_TMP_SUBDIR );
157 rc = bdb->bi_dbenv->set_tmp_dir( bdb->bi_dbenv, dir );
159 Debug( LDAP_DEBUG_ANY,
160 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
161 dir, db_strerror(rc), rc );
165 strcat( &dir[len], BDB_LG_SUBDIR );
167 rc = bdb->bi_dbenv->set_lg_dir( bdb->bi_dbenv, dir );
169 Debug( LDAP_DEBUG_ANY,
170 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
171 dir, db_strerror(rc), rc );
175 strcat( &dir[len], BDB_DATA_SUBDIR );
177 rc = bdb->bi_dbenv->set_data_dir( bdb->bi_dbenv, dir );
179 Debug( LDAP_DEBUG_ANY,
180 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
181 dir, db_strerror(rc), rc );
187 Debug( LDAP_DEBUG_TRACE,
188 "bdb_db_open: dbenv_open(%s)\n",
189 bdb->bi_dbenv_home, 0, 0);
191 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
194 bdb->bi_dbenv_mode );
196 Debug( LDAP_DEBUG_ANY,
197 "bdb_db_open: dbenv_open failed: %s (%d)\n",
198 db_strerror(rc), rc, 0 );
202 if( bdb->bi_dbenv_xflags != 0 ) {
203 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
204 bdb->bi_dbenv_xflags, 1);
206 Debug( LDAP_DEBUG_ANY,
207 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
208 db_strerror(rc), rc, 0 );
213 flags = DB_THREAD | DB_CREATE | bdb->bi_db_opflags;
215 bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
216 BDB_INDICES * sizeof(struct bdb_db_info *) );
218 /* open (and create) main database */
219 for( i = 0; bdbi_databases[i].name; i++ ) {
220 struct bdb_db_info *db;
222 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
224 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
226 Debug( LDAP_DEBUG_ANY,
227 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
228 bdb->bi_dbenv_home, db_strerror(rc), rc );
232 if( i == BDB_ID2ENTRY ) {
233 rc = db->bdi_db->set_bt_compare( db->bdi_db,
235 rc = db->bdi_db->set_pagesize( db->bdi_db,
236 BDB_ID2ENTRY_PAGESIZE );
238 rc = db->bdi_db->open( db->bdi_db,
239 bdbi_databases[i].file,
240 /* bdbi_databases[i].name, */ NULL,
241 bdbi_databases[i].type,
242 bdbi_databases[i].flags | flags,
243 bdb->bi_dbenv_mode );
246 Debug( LDAP_DEBUG_ANY,
247 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
248 bdb->bi_dbenv_home, db_strerror(rc), rc );
252 db->bdi_name = bdbi_databases[i].name;
253 bdb->bi_databases[i] = db;
256 bdb->bi_databases[i] = NULL;
257 bdb->bi_ndatabases = i;
260 rc = bdb_last_id( be, NULL );
262 Debug( LDAP_DEBUG_ANY,
263 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
264 bdb->bi_dbenv_home, db_strerror(rc), rc );
268 /* <insert> open (and create) index databases */
271 if( bdb->bi_lock_detect != DB_LOCK_NORUN ) {
272 /* listener as a separate THREAD */
273 rc = ldap_pvt_thread_create( &bdb->bi_lock_detect_tid,
274 1, lock_detect_task, bdb );
281 bdb_db_close( BackendDB *be )
284 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
286 /* force a checkpoint */
288 rc = txn_checkpoint( bdb->bi_dbenv, 0, 0, DB_FORCE );
290 Debug( LDAP_DEBUG_ANY,
291 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
292 db_strerror(rc), rc, 0 );
297 while( bdb->bi_ndatabases-- ) {
298 rc = bdb->bi_databases[bdb->bi_ndatabases]->bdi_db->close(
299 bdb->bi_databases[bdb->bi_ndatabases]->bdi_db, 0 );
306 bdb_db_destroy( BackendDB *be )
309 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
311 /* close db environment */
312 if( bdb->bi_dbenv ) {
313 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
314 bdb->bi_dbenv = NULL;
316 Debug( LDAP_DEBUG_ANY,
317 "bdb_db_destroy: close failed: %s (%d)\n",
318 db_strerror(rc), rc, 0 );
326 #ifdef SLAPD_BDB_DYNAMIC
327 int back_bdb_LTX_init_module( int argc, char *argv[] ) {
330 memset( &bi, '\0', sizeof(bi) );
332 bi.bi_init = bdb_initialize;
337 #endif /* SLAPD_BDB_DYNAMIC */
344 static char *controls[] = {
345 LDAP_CONTROL_MANAGEDSAIT,
349 { /* version check */
350 int major, minor, patch;
351 char *version = db_version( &major, &minor, &patch );
353 if( major != DB_VERSION_MAJOR ||
354 minor != DB_VERSION_MINOR ||
355 patch < DB_VERSION_PATCH )
357 Debug( LDAP_DEBUG_ANY,
358 "bi_back_initialize: version mismatch\n"
359 "\texpected: " DB_VERSION_STRING "\n"
360 "\tgot: %s \n", version, 0, 0 );
363 Debug( LDAP_DEBUG_ANY, "bdb_initialize: %s\n",
368 db_env_set_func_malloc( ch_malloc );
369 db_env_set_func_realloc( ch_realloc );
370 db_env_set_func_free( ch_free );
373 db_env_set_func_yield( ldap_pvt_thread_yield );
375 bi->bi_controls = controls;
382 bi->bi_db_init = bdb_db_init;
383 bi->bi_db_config = bdb_db_config;
384 bi->bi_db_open = bdb_db_open;
385 bi->bi_db_close = bdb_db_close;
386 bi->bi_db_destroy = bdb_db_destroy;
388 bi->bi_op_add = bdb_add;
389 bi->bi_op_bind = bdb_bind;
390 bi->bi_op_compare = bdb_compare;
391 bi->bi_op_delete = bdb_delete;
392 bi->bi_op_modify = bdb_modify;
393 bi->bi_op_modrdn = bdb_modrdn;
394 bi->bi_op_search = bdb_search;
397 bi->bi_op_unbind = bdb_unbind;
398 bi->bi_op_abandon = bdb_abandon;
400 bi->bi_extended = bdb_extended;
402 bi->bi_acl_group = bdb_group;
403 bi->bi_acl_attribute = bdb_attribute;
404 bi->bi_chk_referrals = bdb_referrals;
406 bi->bi_entry_release_rw = bdb_entry_release;
409 * hooks for slap tools
411 bi->bi_tool_entry_open = bdb_tool_entry_open;
412 bi->bi_tool_entry_close = bdb_tool_entry_close;
413 bi->bi_tool_entry_first = bdb_tool_entry_next;
414 bi->bi_tool_entry_next = bdb_tool_entry_next;
415 bi->bi_tool_entry_get = bdb_tool_entry_get;
416 bi->bi_tool_entry_put = bdb_tool_entry_put;
417 bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
418 bi->bi_tool_sync = 0;
420 bi->bi_connection_init = 0;
421 bi->bi_connection_destroy = 0;