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 { "nextid" BDB_SUFFIX, "nextid", DB_BTREE, 0 },
23 { "dn2entry" BDB_SUFFIX, "dn2entry", DB_BTREE, 0 },
24 { "id2entry" BDB_SUFFIX, "id2entry", DB_BTREE, 0 },
30 bdb_destroy( BackendInfo *bi )
36 bdb_open( BackendInfo *bi )
38 /* initialize the underlying database system */
39 Debug( LDAP_DEBUG_TRACE, "bdb_open: initialize BDB backend\n",
46 bdb_close( BackendInfo *bi )
48 /* terminate the underlying database system */
54 bdb_db_init( BackendDB *be )
58 Debug( LDAP_DEBUG_ANY,
59 "bdb_db_init: Initializing BDB database\n",
62 /* allocate backend-database-specific stuff */
63 bdb = (struct bdb_info *) ch_calloc( 1, sizeof(struct bdb_info) );
65 /* DBEnv parameters */
66 bdb->bi_dbenv_home = ch_strdup( BDB_DBENV_HOME );
67 bdb->bi_dbenv_xflags = 0;
68 bdb->bi_dbenv_mode = DEFAULT_MODE;
71 bdb->bi_lock_detect = DB_LOCK_NORUN;
79 static void *lock_detect_task( void *arg )
81 struct bdb_info *bdb = (struct bdb_info *) arg;
83 while( bdb->bi_dbenv != NULL ) {
85 sleep( bdb->bi_lock_detect_seconds );
87 rc = lock_detect( bdb->bi_dbenv, 0,
88 bdb->bi_lock_detect, NULL );
100 bdb_db_open( BackendDB *be )
103 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
106 Debug( LDAP_DEBUG_ARGS,
108 be->be_suffix[0], 0, 0 );
110 /* we should check existance of dbenv_home and db_directory */
112 rc = db_env_create( &bdb->bi_dbenv, 0 );
114 Debug( LDAP_DEBUG_ANY,
115 "bdb_db_open: db_env_create failed: %s (%d)\n",
116 db_strerror(rc), rc, 0 );
120 flags = DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN |
121 DB_THREAD | DB_CREATE | DB_RECOVER;
123 bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0] );
124 bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
128 char dir[MAXPATHLEN];
129 size_t len = strlen( bdb->bi_dbenv_home );
131 strcpy( dir, bdb->bi_dbenv_home );
132 strcat( &dir[len], BDB_TMP_SUBDIR );
134 rc = bdb->bi_dbenv->set_tmp_dir( bdb->bi_dbenv, dir );
136 Debug( LDAP_DEBUG_ANY,
137 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
138 dir, db_strerror(rc), rc );
142 strcat( &dir[len], BDB_LG_SUBDIR );
144 rc = bdb->bi_dbenv->set_lg_dir( bdb->bi_dbenv, dir );
146 Debug( LDAP_DEBUG_ANY,
147 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
148 dir, db_strerror(rc), rc );
152 strcat( &dir[len], BDB_DATA_SUBDIR );
154 rc = bdb->bi_dbenv->set_data_dir( bdb->bi_dbenv, dir );
156 Debug( LDAP_DEBUG_ANY,
157 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
158 dir, db_strerror(rc), rc );
164 Debug( LDAP_DEBUG_TRACE,
165 "bdb_db_open: dbenv_open(%s)\n",
166 bdb->bi_dbenv_home, 0, 0);
168 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
170 flags | bdb->bi_dbenv_xflags,
171 bdb->bi_dbenv_mode );
173 Debug( LDAP_DEBUG_ANY,
174 "bdb_db_open: dbenv_open failed: %s (%d)\n",
175 db_strerror(rc), rc, 0 );
179 flags = DB_THREAD | DB_CREATE;
181 bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
182 BDB_INDICES * sizeof(struct bdb_db_info *) );
184 /* open (and create) main database */
185 for( i = 0; bdbi_databases[i].name; i++ ) {
186 struct bdb_db_info *db;
188 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
190 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
192 Debug( LDAP_DEBUG_ANY,
193 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
194 bdb->bi_dbenv_home, db_strerror(rc), rc );
198 rc = db->bdi_db->open( db->bdi_db,
199 bdbi_databases[i].file,
200 bdbi_databases[i].name,
201 bdbi_databases[i].type,
202 bdbi_databases[i].flags | flags,
203 bdb->bi_dbenv_mode );
206 Debug( LDAP_DEBUG_ANY,
207 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
208 bdb->bi_dbenv_home, db_strerror(rc), rc );
212 db->bdi_name = bdbi_databases[i].name;
213 bdb->bi_databases[i] = db;
216 bdb->bi_databases[i] = NULL;
217 bdb->bi_ndatabases = i;
220 rc = bdb_last_id( be, NULL );
222 Debug( LDAP_DEBUG_ANY,
223 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
224 bdb->bi_dbenv_home, db_strerror(rc), rc );
228 /* <insert> open (and create) index databases */
231 if( bdb->bi_lock_detect != DB_LOCK_NORUN ) {
232 /* listener as a separate THREAD */
233 rc = ldap_pvt_thread_create( &bdb->bi_lock_detect_tid,
234 1, lock_detect_task, bdb );
241 bdb_db_close( BackendDB *be )
244 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
246 /* force a checkpoint */
247 rc = txn_checkpoint( bdb->bi_dbenv, 0, 0, DB_FORCE );
249 Debug( LDAP_DEBUG_ANY,
250 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
251 db_strerror(rc), rc, 0 );
255 while( bdb->bi_ndatabases-- ) {
256 rc = bdb->bi_databases[bdb->bi_ndatabases]->bdi_db->close(
257 bdb->bi_databases[bdb->bi_ndatabases]->bdi_db, 0 );
264 bdb_db_destroy( BackendDB *be )
267 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
269 /* close db environment */
270 if( bdb->bi_dbenv ) {
271 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
272 bdb->bi_dbenv = NULL;
274 Debug( LDAP_DEBUG_ANY,
275 "bdb_db_destroy: close failed: %s (%d)\n",
276 db_strerror(rc), rc, 0 );
284 #ifdef SLAPD_BDB_DYNAMIC
285 int back_bdb_LTX_init_module( int argc, char *argv[] ) {
288 memset( &bi, '\0', sizeof(bi) );
290 bi.bi_init = bdb_initialize;
295 #endif /* SLAPD_BDB_DYNAMIC */
302 static char *controls[] = {
303 LDAP_CONTROL_MANAGEDSAIT,
307 { /* version check */
308 int major, minor, patch;
309 char *version = db_version( &major, &minor, &patch );
311 if( major != DB_VERSION_MAJOR ||
312 minor != DB_VERSION_MINOR ||
313 patch < DB_VERSION_PATCH )
315 Debug( LDAP_DEBUG_ANY,
316 "bi_back_initialize: version mismatch\n"
317 "\texpected: " DB_VERSION_STRING "\n"
318 "\tgot: %s \n", version, 0, 0 );
321 Debug( LDAP_DEBUG_ANY, "bdb_initialize: %s\n",
326 db_env_set_func_malloc( ch_malloc );
327 db_env_set_func_realloc( ch_realloc );
328 db_env_set_func_free( ch_free );
331 db_env_set_func_yield( ldap_pvt_thread_yield );
333 bi->bi_controls = controls;
340 bi->bi_db_init = bdb_db_init;
341 bi->bi_db_config = bdb_db_config;
342 bi->bi_db_open = bdb_db_open;
343 bi->bi_db_close = bdb_db_close;
344 bi->bi_db_destroy = bdb_db_destroy;
346 bi->bi_op_add = bdb_add;
347 bi->bi_op_bind = bdb_bind;
348 bi->bi_op_compare = bdb_compare;
349 bi->bi_op_delete = bdb_delete;
350 bi->bi_op_modify = bdb_modify;
351 bi->bi_op_modrdn = bdb_modrdn;
352 bi->bi_op_search = bdb_search;
355 bi->bi_op_unbind = bdb_unbind;
356 bi->bi_op_abandon = bdb_abandon;
358 bi->bi_extended = bdb_extended;
360 bi->bi_acl_group = bdb_group;
361 bi->bi_acl_attribute = bdb_attribute;
363 bi->bi_chk_referrals = bdb_referrals;
365 bi->bi_entry_release_rw = 0;
368 * hooks for slap tools
370 bi->bi_tool_entry_open = bdb_tool_entry_open;
371 bi->bi_tool_entry_close = bdb_tool_entry_close;
372 bi->bi_tool_entry_first = bdb_tool_entry_next;
373 bi->bi_tool_entry_next = bdb_tool_entry_next;
374 bi->bi_tool_entry_get = bdb_tool_entry_get;
375 bi->bi_tool_entry_put = bdb_tool_entry_put;
377 bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
379 bi->bi_tool_entry_reindex = 0;
381 bi->bi_tool_sync = 0;
383 bi->bi_connection_init = 0;
384 bi->bi_connection_destroy = 0;