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;
74 ldap_pvt_thread_mutex_init( &bdb->bi_database_mutex );
81 static void *lock_detect_task( void *arg )
83 struct bdb_info *bdb = (struct bdb_info *) arg;
85 while( bdb->bi_dbenv != NULL ) {
87 sleep( bdb->bi_lock_detect_seconds );
89 rc = lock_detect( bdb->bi_dbenv, 0,
90 bdb->bi_lock_detect, NULL );
102 bdb_db_open( BackendDB *be )
105 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
108 Debug( LDAP_DEBUG_ARGS,
110 be->be_suffix[0], 0, 0 );
112 /* we should check existance of dbenv_home and db_directory */
114 rc = db_env_create( &bdb->bi_dbenv, 0 );
116 Debug( LDAP_DEBUG_ANY,
117 "bdb_db_open: db_env_create failed: %s (%d)\n",
118 db_strerror(rc), rc, 0 );
122 flags = DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN |
123 DB_THREAD | DB_CREATE | DB_RECOVER;
125 bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0] );
126 bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
130 char dir[MAXPATHLEN];
131 size_t len = strlen( bdb->bi_dbenv_home );
133 strcpy( dir, bdb->bi_dbenv_home );
134 strcat( &dir[len], BDB_TMP_SUBDIR );
136 rc = bdb->bi_dbenv->set_tmp_dir( bdb->bi_dbenv, dir );
138 Debug( LDAP_DEBUG_ANY,
139 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
140 dir, db_strerror(rc), rc );
144 strcat( &dir[len], BDB_LG_SUBDIR );
146 rc = bdb->bi_dbenv->set_lg_dir( bdb->bi_dbenv, dir );
148 Debug( LDAP_DEBUG_ANY,
149 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
150 dir, db_strerror(rc), rc );
154 strcat( &dir[len], BDB_DATA_SUBDIR );
156 rc = bdb->bi_dbenv->set_data_dir( bdb->bi_dbenv, dir );
158 Debug( LDAP_DEBUG_ANY,
159 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
160 dir, db_strerror(rc), rc );
166 Debug( LDAP_DEBUG_TRACE,
167 "bdb_db_open: dbenv_open(%s)\n",
168 bdb->bi_dbenv_home, 0, 0);
170 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
172 flags | bdb->bi_dbenv_xflags,
173 bdb->bi_dbenv_mode );
175 Debug( LDAP_DEBUG_ANY,
176 "bdb_db_open: dbenv_open failed: %s (%d)\n",
177 db_strerror(rc), rc, 0 );
181 flags = DB_THREAD | DB_CREATE;
183 bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
184 BDB_INDICES * sizeof(struct bdb_db_info *) );
186 /* open (and create) main database */
187 for( i = 0; bdbi_databases[i].name; i++ ) {
188 struct bdb_db_info *db;
190 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
192 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
194 Debug( LDAP_DEBUG_ANY,
195 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
196 bdb->bi_dbenv_home, db_strerror(rc), rc );
200 rc = db->bdi_db->open( db->bdi_db,
201 bdbi_databases[i].file,
202 bdbi_databases[i].name,
203 bdbi_databases[i].type,
204 bdbi_databases[i].flags | flags,
205 bdb->bi_dbenv_mode );
208 Debug( LDAP_DEBUG_ANY,
209 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
210 bdb->bi_dbenv_home, db_strerror(rc), rc );
214 db->bdi_name = bdbi_databases[i].name;
215 bdb->bi_databases[i] = db;
218 bdb->bi_databases[i] = NULL;
219 bdb->bi_ndatabases = i;
222 rc = bdb_last_id( be, NULL );
224 Debug( LDAP_DEBUG_ANY,
225 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
226 bdb->bi_dbenv_home, db_strerror(rc), rc );
230 /* <insert> open (and create) index databases */
233 if( bdb->bi_lock_detect != DB_LOCK_NORUN ) {
234 /* listener as a separate THREAD */
235 rc = ldap_pvt_thread_create( &bdb->bi_lock_detect_tid,
236 1, lock_detect_task, bdb );
243 bdb_db_close( BackendDB *be )
246 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
248 /* force a checkpoint */
249 rc = txn_checkpoint( bdb->bi_dbenv, 0, 0, DB_FORCE );
251 Debug( LDAP_DEBUG_ANY,
252 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
253 db_strerror(rc), rc, 0 );
257 while( bdb->bi_ndatabases-- ) {
258 rc = bdb->bi_databases[bdb->bi_ndatabases]->bdi_db->close(
259 bdb->bi_databases[bdb->bi_ndatabases]->bdi_db, 0 );
266 bdb_db_destroy( BackendDB *be )
269 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
271 /* close db environment */
272 if( bdb->bi_dbenv ) {
273 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
274 bdb->bi_dbenv = NULL;
276 Debug( LDAP_DEBUG_ANY,
277 "bdb_db_destroy: close failed: %s (%d)\n",
278 db_strerror(rc), rc, 0 );
286 #ifdef SLAPD_BDB_DYNAMIC
287 int back_bdb_LTX_init_module( int argc, char *argv[] ) {
290 memset( &bi, '\0', sizeof(bi) );
292 bi.bi_init = bdb_initialize;
297 #endif /* SLAPD_BDB_DYNAMIC */
304 static char *controls[] = {
305 LDAP_CONTROL_MANAGEDSAIT,
309 { /* version check */
310 int major, minor, patch;
311 char *version = db_version( &major, &minor, &patch );
313 if( major != DB_VERSION_MAJOR ||
314 minor != DB_VERSION_MINOR ||
315 patch < DB_VERSION_PATCH )
317 Debug( LDAP_DEBUG_ANY,
318 "bi_back_initialize: version mismatch\n"
319 "\texpected: " DB_VERSION_STRING "\n"
320 "\tgot: %s \n", version, 0, 0 );
323 Debug( LDAP_DEBUG_ANY, "bdb_initialize: %s\n",
328 db_env_set_func_malloc( ch_malloc );
329 db_env_set_func_realloc( ch_realloc );
330 db_env_set_func_free( ch_free );
333 db_env_set_func_yield( ldap_pvt_thread_yield );
335 bi->bi_controls = controls;
342 bi->bi_db_init = bdb_db_init;
343 bi->bi_db_config = bdb_db_config;
344 bi->bi_db_open = bdb_db_open;
345 bi->bi_db_close = bdb_db_close;
346 bi->bi_db_destroy = bdb_db_destroy;
348 bi->bi_op_add = bdb_add;
349 bi->bi_op_bind = bdb_bind;
350 bi->bi_op_compare = bdb_compare;
351 bi->bi_op_delete = bdb_delete;
352 bi->bi_op_modify = bdb_modify;
353 bi->bi_op_modrdn = bdb_modrdn;
354 bi->bi_op_search = bdb_search;
357 bi->bi_op_unbind = bdb_unbind;
358 bi->bi_op_abandon = bdb_abandon;
360 bi->bi_extended = bdb_extended;
362 bi->bi_acl_group = bdb_group;
363 bi->bi_acl_attribute = bdb_attribute;
365 bi->bi_chk_referrals = bdb_referrals;
367 bi->bi_entry_release_rw = 0;
370 * hooks for slap tools
372 bi->bi_tool_entry_open = bdb_tool_entry_open;
373 bi->bi_tool_entry_close = bdb_tool_entry_close;
374 bi->bi_tool_entry_first = bdb_tool_entry_next;
375 bi->bi_tool_entry_next = bdb_tool_entry_next;
376 bi->bi_tool_entry_get = bdb_tool_entry_get;
377 bi->bi_tool_entry_put = bdb_tool_entry_put;
378 bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
379 bi->bi_tool_sync = 0;
381 bi->bi_connection_init = 0;
382 bi->bi_connection_destroy = 0;