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;
70 bdb->bi_lock_detect = DB_LOCK_NORUN;
78 static void *lock_detect_task( void *arg )
80 struct bdb_info *bdb = (struct bdb_info *) arg;
82 while( bdb->bi_dbenv != NULL ) {
84 sleep( bdb->bi_lock_detect_seconds );
86 rc = lock_detect( bdb->bi_dbenv, DB_LOCK_CONFLICT, bdb->bi_lock_detect, NULL );
97 bdb_db_open( BackendDB *be )
100 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
103 Debug( LDAP_DEBUG_ARGS,
105 be->be_suffix[0], 0, 0 );
107 /* we should check existance of dbenv_home and db_directory */
109 rc = db_env_create( &bdb->bi_dbenv, 0 );
111 Debug( LDAP_DEBUG_ANY,
112 "bdb_db_open: db_env_create failed: %s (%d)\n",
113 db_strerror(rc), rc, 0 );
117 flags = DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN |
118 DB_THREAD | DB_CREATE | DB_RECOVER;
120 bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0] );
121 bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
125 char dir[MAXPATHLEN];
126 size_t len = strlen( bdb->bi_dbenv_home );
128 strcpy( dir, bdb->bi_dbenv_home );
129 strcat( &dir[len], BDB_TMP_SUBDIR );
131 rc = bdb->bi_dbenv->set_tmp_dir( bdb->bi_dbenv, dir );
133 Debug( LDAP_DEBUG_ANY,
134 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
135 dir, db_strerror(rc), rc );
139 strcat( &dir[len], BDB_LG_SUBDIR );
141 rc = bdb->bi_dbenv->set_lg_dir( bdb->bi_dbenv, dir );
143 Debug( LDAP_DEBUG_ANY,
144 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
145 dir, db_strerror(rc), rc );
149 strcat( &dir[len], BDB_DATA_SUBDIR );
151 rc = bdb->bi_dbenv->set_data_dir( bdb->bi_dbenv, dir );
153 Debug( LDAP_DEBUG_ANY,
154 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
155 dir, db_strerror(rc), rc );
161 Debug( LDAP_DEBUG_TRACE,
162 "bdb_db_open: dbenv_open(%s)\n",
163 bdb->bi_dbenv_home, 0, 0);
165 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
167 flags | bdb->bi_dbenv_xflags,
168 bdb->bi_dbenv_mode );
170 Debug( LDAP_DEBUG_ANY,
171 "bdb_db_open: dbenv_open failed: %s (%d)\n",
172 db_strerror(rc), rc, 0 );
176 flags = DB_THREAD | DB_CREATE;
178 bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
179 BDB_INDICES * sizeof(struct bdb_db_info *) );
181 /* open (and create) main database */
182 for( i = 0; i < BDB_INDICES; i++ ) {
183 struct bdb_db_info *db;
185 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
187 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
189 Debug( LDAP_DEBUG_ANY,
190 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
191 bdb->bi_dbenv_home, db_strerror(rc), rc );
195 rc = db->bdi_db->open( db->bdi_db,
196 bdbi_databases[i].file,
197 bdbi_databases[i].name,
198 bdbi_databases[i].type,
199 bdbi_databases[i].flags | flags,
200 bdb->bi_dbenv_mode );
203 Debug( LDAP_DEBUG_ANY,
204 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
205 bdb->bi_dbenv_home, db_strerror(rc), rc );
209 bdb->bi_databases[i] = db;
212 /* <insert> open (and create) index databases */
216 if( bdb->bi_lock_detect != DB_LOCK_NORUN ) {
217 /* listener as a separate THREAD */
218 rc = ldap_pvt_thread_create( &bdb->bi_lock_detect_tid,
219 1, lock_detect_task, bdb );
226 bdb_db_close( BackendDB *be )
229 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
231 /* force a checkpoint */
232 rc = txn_checkpoint( bdb->bi_dbenv, 0, 0, DB_FORCE );
234 Debug( LDAP_DEBUG_ANY,
235 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
236 db_strerror(rc), rc, 0 );
240 while( bdb->bi_ndatabases-- ) {
241 rc = bdb->bi_databases[bdb->bi_ndatabases]->bdi_db->close(
242 bdb->bi_databases[bdb->bi_ndatabases]->bdi_db, 0 );
249 bdb_db_destroy( BackendDB *be )
252 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
254 /* close db environment */
255 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
256 bdb->bi_dbenv = NULL;
258 Debug( LDAP_DEBUG_ANY,
259 "bdb_db_destroy: close failed: %s (%d)\n",
260 db_strerror(rc), rc, 0 );
267 #ifdef SLAPD_BDB_DYNAMIC
268 int back_bdb_LTX_init_module( int argc, char *argv[] ) {
271 memset( &bi, '\0', sizeof(bi) );
273 bi.bi_init = bdb_initialize;
278 #endif /* SLAPD_BDB_DYNAMIC */
285 static char *controls[] = {
286 LDAP_CONTROL_MANAGEDSAIT,
290 { /* version check */
291 int major, minor, patch;
292 char *version = db_version( &major, &minor, &patch );
294 if( major != DB_VERSION_MAJOR ||
295 minor != DB_VERSION_MINOR ||
296 patch < DB_VERSION_PATCH )
298 Debug( LDAP_DEBUG_ANY,
299 "bi_back_initialize: version mismatch\n"
300 "\texpected: " DB_VERSION_STRING "\n"
301 "\tgot: %s \n", version, 0, 0 );
304 Debug( LDAP_DEBUG_ANY, "bdb_initialize: %s\n",
309 db_env_set_func_malloc( ch_malloc );
310 db_env_set_func_realloc( ch_realloc );
311 db_env_set_func_free( ch_free );
313 db_env_set_func_yield( ldap_pvt_thread_yield );
315 bi->bi_controls = controls;
322 bi->bi_db_init = bdb_db_init;
323 bi->bi_db_config = bdb_db_config;
324 bi->bi_db_open = bdb_db_open;
325 bi->bi_db_close = bdb_db_close;
326 bi->bi_db_destroy = bdb_db_destroy;
328 bi->bi_op_add = bdb_add;
329 bi->bi_op_bind = bdb_bind;
330 bi->bi_op_compare = bdb_compare;
331 bi->bi_op_delete = bdb_delete;
332 bi->bi_op_modify = bdb_modify;
333 bi->bi_op_modrdn = bdb_modrdn;
334 bi->bi_op_search = bdb_search;
337 bi->bi_op_unbind = bdb_unbind;
338 bi->bi_op_abandon = bdb_abandon;
340 bi->bi_extended = bdb_extended;
342 bi->bi_acl_group = bdb_group;
343 bi->bi_acl_attribute = bdb_attribute;
345 bi->bi_chk_referrals = bdb_referrals;
347 bi->bi_entry_release_rw = 0;
350 * hooks for slap tools
352 bi->bi_tool_entry_open = bdb_tool_entry_open;
353 bi->bi_tool_entry_close = bdb_tool_entry_close;
354 bi->bi_tool_entry_first = bdb_tool_entry_next;
355 bi->bi_tool_entry_next = bdb_tool_entry_next;
356 bi->bi_tool_entry_get = bdb_tool_entry_get;
357 bi->bi_tool_entry_put = bdb_tool_entry_put;
358 bi->bi_tool_entry_reindex = 0;
359 bi->bi_tool_sync = 0;
361 bi->bi_connection_init = 0;
362 bi->bi_connection_destroy = 0;