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", "nextid", DB_BTREE, 0 },
23 { "dn2entry", "dn2entry", DB_BTREE, 0 },
24 { "id2entry", "id2entry", DB_BTREE, 0 },
28 bdb_destroy( BackendInfo *bi )
34 bdb_open( BackendInfo *bi )
36 /* initialize the underlying database system */
38 db_env_set_func_malloc( ch_malloc );
39 db_env_set_func_realloc( ch_realloc );
40 db_env_set_func_free( ch_free );
41 db_env_set_func_yield( ldap_pvt_thread_yield );
46 bdb_close( BackendInfo *bi )
48 /* terminate the underlying database system */
53 bdb_db_init( Backend *be )
57 /* allocate backend-database-specific stuff */
58 bdb = (struct bdb_info *) ch_calloc( 1, sizeof(struct bdb_info) );
60 /* DBEnv parameters */
61 bdb->bi_dbenv_home = ch_strdup( BDB_DBENV_HOME );
62 bdb->bi_dbenv_xflags = 0;
63 bdb->bi_dbenv_mode = DEFAULT_MODE;
70 bdb_db_open( BackendDB *be )
73 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
75 /* we should check existance of dbenv_home and db_directory */
77 rc = db_env_create( &bdb->bi_dbenv, 0 );
79 Debug( LDAP_DEBUG_ANY,
80 "bi_back_db_open: db_env_create failed: %s (%d)\n",
81 db_strerror(rc), rc, 0 );
85 flags = DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN |
86 DB_CREATE | DB_RECOVER | DB_THREAD;
88 #ifdef SLAPD_BDB_PRIVATE
91 flags |= DB_INIT_MPOOL;
94 bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0] );
95 bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
100 size_t len = strlen( bdb->bi_dbenv_home );
102 strcpy( dir, bdb->bi_dbenv_home );
103 strcat( &dir[len], BDB_TMP_SUBDIR );
105 rc = bdb->bi_dbenv->set_tmp_dir( bdb->bi_dbenv, dir );
107 Debug( LDAP_DEBUG_ANY,
108 "bi_back_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
109 dir, db_strerror(rc), rc );
113 strcat( &dir[len], BDB_LG_SUBDIR );
115 rc = bdb->bi_dbenv->set_lg_dir( bdb->bi_dbenv, dir );
117 Debug( LDAP_DEBUG_ANY,
118 "bi_back_db_open: set_lg_dir(%s) failed: %s (%d)\n",
119 dir, db_strerror(rc), rc );
123 strcat( &dir[len], BDB_DATA_SUBDIR );
125 rc = bdb->bi_dbenv->set_data_dir( bdb->bi_dbenv, dir );
127 Debug( LDAP_DEBUG_ANY,
128 "bi_back_db_open: set_data_dir(%s) failed: %s (%d)\n",
129 dir, db_strerror(rc), rc );
135 Debug( LDAP_DEBUG_TRACE,
136 "bi_back_db_open: dbenv_open(%s)\n",
137 bdb->bi_dbenv_home, 0, 0);
139 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
141 flags | bdb->bi_dbenv_xflags,
142 bdb->bi_dbenv_mode );
144 Debug( LDAP_DEBUG_ANY,
145 "bi_back_db_open: dbenv_open failed: %s (%d)\n",
146 db_strerror(rc), rc, 0 );
150 flags = DB_THREAD | DB_CREATE;
152 /* open (and create) main database */
153 for( i = 0; i < BDB_INDICES; i++ ) {
154 struct bdb_db_info *db;
156 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
158 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
160 Debug( LDAP_DEBUG_ANY,
161 "bi_back_db_open: db_create(%s) failed: %s (%d)\n",
162 bdb->bi_dbenv_home, db_strerror(rc), rc );
166 rc = db->bdi_db->open( db->bdi_db,
167 bdbi_databases[i].file,
168 bdbi_databases[i].name,
169 bdbi_databases[i].type,
170 bdbi_databases[i].flags | flags,
171 bdb->bi_dbenv_mode );
174 Debug( LDAP_DEBUG_ANY,
175 "bi_back_db_open: db_open(%s) failed: %s (%d)\n",
176 bdb->bi_dbenv_home, db_strerror(rc), rc );
181 /* <insert> open (and create) index databases */
188 bdb_db_close( BackendDB *be )
191 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
193 /* force a checkpoint */
194 rc = txn_checkpoint( bdb->bi_dbenv, 0, 0, DB_FORCE );
196 Debug( LDAP_DEBUG_ANY,
197 "bi_back_db_destroy: txn_checkpoint failed: %s (%d)\n",
198 db_strerror(rc), rc, 0 );
202 while( bdb->bi_ndatabases-- ) {
203 rc = bdb->bi_databases[bdb->bi_ndatabases]->bdi_db->close(
204 bdb->bi_databases[bdb->bi_ndatabases]->bdi_db, 0 );
211 bdb_db_destroy( BackendDB *be )
214 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
216 /* close db environment */
217 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
218 bdb->bi_dbenv = NULL;
220 Debug( LDAP_DEBUG_ANY,
221 "bi_back_db_destroy: close failed: %s (%d)\n",
222 db_strerror(rc), rc, 0 );
229 #ifdef SLAPD_BDB_DYNAMIC
230 int back_bdb_LTX_init_module( int argc, char *argv[] ) {
233 memset( &bi, '\0', sizeof(bi) );
235 bi.bi_init = bdb_initialize;
240 #endif /* SLAPD_BDB_DYNAMIC */
247 static char *controls[] = {
248 LDAP_CONTROL_MANAGEDSAIT,
252 { /* version check */
253 int major, minor, patch;
254 char *version = db_version( &major, &minor, &patch );
256 if( major != DB_VERSION_MAJOR ||
257 minor != DB_VERSION_MINOR ||
258 patch < DB_VERSION_PATCH )
260 Debug( LDAP_DEBUG_ANY,
261 "bi_back_initialize: version mismatch\n"
262 "\texpected: " DB_VERSION_STRING "\n"
263 "\tgot: %s \n", version, 0, 0 );
266 Debug( LDAP_DEBUG_ANY, "bdb_initialize: %s\n",
270 bi->bi_controls = controls;
272 bi->bi_open = bdb_open;
273 bi->bi_close = bdb_close;
275 bi->bi_destroy = bdb_destroy;
277 bi->bi_db_init = bdb_db_init;
278 bi->bi_db_config = bdb_db_config;
279 bi->bi_db_open = bdb_db_open;
280 bi->bi_db_close = bdb_db_close;
281 bi->bi_db_destroy = bdb_db_destroy;
283 bi->bi_op_add = bdb_add;
284 bi->bi_op_compare = bdb_compare;
285 bi->bi_op_delete = bdb_delete;
286 bi->bi_op_search = bdb_search;
289 bi->bi_op_bind = bdb_bind;
290 bi->bi_op_unbind = bdb_unbind;
291 bi->bi_op_modify = bdb_modify;
292 bi->bi_op_modrdn = bdb_modrdn;
293 bi->bi_op_abandon = bdb_abandon;
295 bi->bi_extended = bdb_extended;
297 bi->bi_acl_group = bdb_group;
298 bi->bi_acl_attribute = bdb_attribute;
299 bi->bi_chk_referrals = bdb_referrals;
302 bi->bi_entry_release_rw = 0;
305 * hooks for slap tools
307 bi->bi_tool_entry_open = bdb_tool_entry_open;
308 bi->bi_tool_entry_close = bdb_tool_entry_close;
309 bi->bi_tool_entry_first = bdb_tool_entry_next;
310 bi->bi_tool_entry_next = bdb_tool_entry_next;
311 bi->bi_tool_entry_get = bdb_tool_entry_get;
312 bi->bi_tool_entry_put = bdb_tool_entry_put;
313 bi->bi_tool_entry_reindex = 0;
314 bi->bi_tool_sync = 0;
316 bi->bi_connection_init = 0;
317 bi->bi_connection_destroy = 0;