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[BDB_INDICES] = {
22 { "nextid" BDB_SUFFIX, "nextid", DB_BTREE, 0 },
23 { "dn2entry" BDB_SUFFIX, "dn2entry", DB_BTREE, 0 },
24 { "id2entry" BDB_SUFFIX, "id2entry", 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;
69 bdb->bi_lock_detect = DB_LOCK_NORUN;
76 static void *lock_detect_task( void *arg )
78 struct bdb_info *bdb = (struct bdb_info *) arg;
80 while( bdb->bi_dbenv != NULL ) {
82 sleep( bdb->bi_lock_detect_seconds );
84 rc = lock_detect( bdb->bi_dbenv, DB_LOCK_CONFLICT, bdb->bi_lock_detect, NULL );
95 bdb_db_open( BackendDB *be )
98 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
101 Debug( LDAP_DEBUG_ARGS,
103 be->be_suffix[0], 0, 0 );
105 /* we should check existance of dbenv_home and db_directory */
107 rc = db_env_create( &bdb->bi_dbenv, 0 );
109 Debug( LDAP_DEBUG_ANY,
110 "bdb_db_open: db_env_create failed: %s (%d)\n",
111 db_strerror(rc), rc, 0 );
115 flags = DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN |
116 DB_CREATE | DB_RECOVER;
122 bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0] );
123 bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
127 char dir[MAXPATHLEN];
128 size_t len = strlen( bdb->bi_dbenv_home );
130 strcpy( dir, bdb->bi_dbenv_home );
131 strcat( &dir[len], BDB_TMP_SUBDIR );
133 rc = bdb->bi_dbenv->set_tmp_dir( bdb->bi_dbenv, dir );
135 Debug( LDAP_DEBUG_ANY,
136 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
137 dir, db_strerror(rc), rc );
141 strcat( &dir[len], BDB_LG_SUBDIR );
143 rc = bdb->bi_dbenv->set_lg_dir( bdb->bi_dbenv, dir );
145 Debug( LDAP_DEBUG_ANY,
146 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
147 dir, db_strerror(rc), rc );
151 strcat( &dir[len], BDB_DATA_SUBDIR );
153 rc = bdb->bi_dbenv->set_data_dir( bdb->bi_dbenv, dir );
155 Debug( LDAP_DEBUG_ANY,
156 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
157 dir, db_strerror(rc), rc );
163 Debug( LDAP_DEBUG_TRACE,
164 "bdb_db_open: dbenv_open(%s)\n",
165 bdb->bi_dbenv_home, 0, 0);
167 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
169 flags | bdb->bi_dbenv_xflags,
170 bdb->bi_dbenv_mode );
172 Debug( LDAP_DEBUG_ANY,
173 "bdb_db_open: dbenv_open failed: %s (%d)\n",
174 db_strerror(rc), rc, 0 );
178 flags = DB_THREAD | DB_CREATE;
180 bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
181 BDB_INDICES * sizeof(struct bdb_db_info *) );
183 /* open (and create) main database */
184 for( i = 0; i < BDB_INDICES; i++ ) {
185 struct bdb_db_info *db;
187 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
189 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
191 Debug( LDAP_DEBUG_ANY,
192 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
193 bdb->bi_dbenv_home, db_strerror(rc), rc );
197 rc = db->bdi_db->open( db->bdi_db,
198 bdbi_databases[i].file,
199 bdbi_databases[i].name,
200 bdbi_databases[i].type,
201 bdbi_databases[i].flags | flags,
202 bdb->bi_dbenv_mode );
205 Debug( LDAP_DEBUG_ANY,
206 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
207 bdb->bi_dbenv_home, db_strerror(rc), rc );
211 bdb->bi_databases[i] = db;
214 /* <insert> open (and create) index databases */
218 if( bdb->bi_lock_detect != DB_LOCK_NORUN ) {
219 /* listener as a separate THREAD */
220 rc = ldap_pvt_thread_create( &bdb->bi_lock_detect_tid,
221 1, lock_detect_task, bdb );
228 bdb_db_close( BackendDB *be )
231 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
233 /* force a checkpoint */
234 rc = txn_checkpoint( bdb->bi_dbenv, 0, 0, DB_FORCE );
236 Debug( LDAP_DEBUG_ANY,
237 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
238 db_strerror(rc), rc, 0 );
242 while( bdb->bi_ndatabases-- ) {
243 rc = bdb->bi_databases[bdb->bi_ndatabases]->bdi_db->close(
244 bdb->bi_databases[bdb->bi_ndatabases]->bdi_db, 0 );
251 bdb_db_destroy( BackendDB *be )
254 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
256 /* close db environment */
257 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
258 bdb->bi_dbenv = NULL;
260 Debug( LDAP_DEBUG_ANY,
261 "bdb_db_destroy: close failed: %s (%d)\n",
262 db_strerror(rc), rc, 0 );
269 #ifdef SLAPD_BDB_DYNAMIC
270 int back_bdb_LTX_init_module( int argc, char *argv[] ) {
273 memset( &bi, '\0', sizeof(bi) );
275 bi.bi_init = bdb_initialize;
280 #endif /* SLAPD_BDB_DYNAMIC */
287 static char *controls[] = {
288 LDAP_CONTROL_MANAGEDSAIT,
292 { /* version check */
293 int major, minor, patch;
294 char *version = db_version( &major, &minor, &patch );
296 if( major != DB_VERSION_MAJOR ||
297 minor != DB_VERSION_MINOR ||
298 patch < DB_VERSION_PATCH )
300 Debug( LDAP_DEBUG_ANY,
301 "bi_back_initialize: version mismatch\n"
302 "\texpected: " DB_VERSION_STRING "\n"
303 "\tgot: %s \n", version, 0, 0 );
306 Debug( LDAP_DEBUG_ANY, "bdb_initialize: %s\n",
311 db_env_set_func_malloc( ch_malloc );
312 db_env_set_func_realloc( ch_realloc );
313 db_env_set_func_free( ch_free );
315 db_env_set_func_yield( ldap_pvt_thread_yield );
317 bi->bi_controls = controls;
324 bi->bi_db_init = bdb_db_init;
325 bi->bi_db_config = bdb_db_config;
326 bi->bi_db_open = bdb_db_open;
327 bi->bi_db_close = bdb_db_close;
328 bi->bi_db_destroy = bdb_db_destroy;
330 bi->bi_op_add = bdb_add;
331 bi->bi_op_bind = bdb_bind;
332 bi->bi_op_compare = bdb_compare;
333 bi->bi_op_delete = bdb_delete;
334 bi->bi_op_modify = bdb_modify;
335 bi->bi_op_modrdn = bdb_modrdn;
336 bi->bi_op_search = bdb_search;
339 bi->bi_op_unbind = bdb_unbind;
340 bi->bi_op_abandon = bdb_abandon;
342 bi->bi_extended = bdb_extended;
344 bi->bi_acl_group = bdb_group;
345 bi->bi_acl_attribute = bdb_attribute;
347 bi->bi_chk_referrals = bdb_referrals;
349 bi->bi_entry_release_rw = 0;
352 * hooks for slap tools
354 bi->bi_tool_entry_open = bdb_tool_entry_open;
355 bi->bi_tool_entry_close = bdb_tool_entry_close;
356 bi->bi_tool_entry_first = bdb_tool_entry_next;
357 bi->bi_tool_entry_next = bdb_tool_entry_next;
358 bi->bi_tool_entry_get = bdb_tool_entry_get;
359 bi->bi_tool_entry_put = bdb_tool_entry_put;
360 bi->bi_tool_entry_reindex = 0;
361 bi->bi_tool_sync = 0;
363 bi->bi_connection_init = 0;
364 bi->bi_connection_destroy = 0;