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 int db_env_set_func_malloc( ch_malloc );
39 int db_env_set_func_realloc( ch_realloc );
40 int db_env_set_func_free( ch_free );
41 int 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 );
99 size_t len = strlen( bdb->bi_dbenv_home );
101 strcpy( dir, bdb->bi_dbenv_home );
102 strcat( &dir[len], BDB_TMP_SUBDIR );
104 rc = bdb->bi_dbenv->set_tmp_dir( bdb->bi_dbenv, dir );
106 Debug( LDAP_DEBUG_ANY,
107 "bi_back_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
108 dir, db_strerror(rc), rc );
112 strcat( &dir[len], BDB_LG_SUBDIR );
114 rc = bdb->bi_dbenv->set_lg_dir( bdb->bi_dbenv, dir );
116 Debug( LDAP_DEBUG_ANY,
117 "bi_back_db_open: set_lg_dir(%s) failed: %s (%d)\n",
118 dir, db_strerror(rc), rc );
122 strcat( &dir[len], BDB_DATA_SUBDIR );
124 rc = bdb->bi_dbenv->set_data_dir( bdb->bi_dbenv, dir );
126 Debug( LDAP_DEBUG_ANY,
127 "bi_back_db_open: set_data_dir(%s) failed: %s (%d)\n",
128 dir, db_strerror(rc), rc );
133 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
135 flags | bdb->bi_dbenv_xflags,
136 bdb->bi_dbenv_mode );
138 Debug( LDAP_DEBUG_ANY,
139 "bi_back_db_open: dbenv_open(%s) failed: %s (%d)\n",
140 bdb->bi_dbenv_home, db_strerror(rc), rc );
144 flags = DB_THREAD | DB_CREATE;
146 /* open (and create) main database */
147 for( i = 0; i < BDB_INDICES; i++ ) {
148 struct bdb_db_info *db;
150 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
152 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
154 Debug( LDAP_DEBUG_ANY,
155 "bi_back_db_open: db_create(%s) failed: %s (%d)\n",
156 bdb->bi_dbenv_home, db_strerror(rc), rc );
160 rc = db->bdi_db->open( db->bdi_db,
161 bdbi_databases[i].file,
162 bdbi_databases[i].name,
163 bdbi_databases[i].type,
164 bdbi_databases[i].flags | flags,
165 bdb->bi_dbenv_mode );
168 Debug( LDAP_DEBUG_ANY,
169 "bi_back_db_open: db_open(%s) failed: %s (%d)\n",
170 bdb->bi_dbenv_home, db_strerror(rc), rc );
175 /* <insert> open (and create) index databases */
182 bdb_db_close( BackendDB *be )
185 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
187 /* force a checkpoint */
188 rc = txn_checkpoint( bdb->bi_dbenv, 0, 0, DB_FORCE );
190 Debug( LDAP_DEBUG_ANY,
191 "bi_back_db_destroy: txn_checkpoint failed: %s (%d)\n",
192 db_strerror(rc), rc, 0 );
196 while( bdb->bi_ndatabases-- ) {
197 rc = bdb->bi_databases[bdb->bi_ndatabases]->bdi_db->close(
198 bdb->bi_databases[bdb->bi_ndatabases]->bdi_db, 0 );
205 bdb_db_destroy( BackendDB *be )
208 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
210 /* close db environment */
211 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
212 bdb->bi_dbenv = NULL;
214 Debug( LDAP_DEBUG_ANY,
215 "bi_back_db_destroy: close failed: %s (%d)\n",
216 db_strerror(rc), rc, 0 );
223 #ifdef SLAPD_BDB_DYNAMIC
224 int back_bdb_LTX_init_module( int argc, char *argv[] ) {
227 memset( &bi, '\0', sizeof(bi) );
229 bi.bi_init = bdb_initialize;
234 #endif /* SLAPD_BDB_DYNAMIC */
241 static char *controls[] = {
242 LDAP_CONTROL_MANAGEDSAIT,
246 { /* version check */
247 int major, minor, patch;
248 char *version = db_version( &major, &minor, &patch );
250 if( major != DB_VERSION_MAJOR ||
251 minor != DB_VERSION_MINOR ||
252 patch < DB_VERSION_PATCH )
254 Debug( LDAP_DEBUG_ANY,
255 "bi_back_initialize: version mismatch\n"
256 "\texpected: " DB_VERSION_STRING "\n"
257 "\tgot: %s \n", version, 0, 0 );
260 Debug( LDAP_DEBUG_ANY, "bdb_initialize: %s\n",
264 bi->bi_controls = controls;
266 bi->bi_open = bdb_open;
267 bi->bi_close = bdb_close;
269 bi->bi_destroy = bdb_destroy;
271 bi->bi_db_init = bdb_db_init;
272 bi->bi_db_config = 0;
273 bi->bi_db_open = bdb_db_open;
274 bi->bi_db_close = bdb_db_close;
275 bi->bi_db_destroy = bdb_db_destroy;
277 bi->bi_op_add = bdb_add;
278 bi->bi_op_compare = bdb_compare;
279 bi->bi_op_delete = bdb_delete;
281 bi->bi_op_bind = bi_back_bind;
282 bi->bi_op_unbind = bi_back_unbind;
283 bi->bi_op_search = bi_back_search;
284 bi->bi_op_modify = bi_back_modify;
285 bi->bi_op_modrdn = bi_back_modrdn;
286 bi->bi_op_abandon = bi_back_abandon;
288 bi->bi_extended = bi_back_extended;
290 bi->bi_acl_group = bi_back_group;
291 bi->bi_acl_attribute = bi_back_attribute;
292 bi->bi_chk_referrals = bi_back_referrals;
294 bi->bi_entry_release_rw = 0;
297 * hooks for slap tools
299 bi->bi_tool_entry_open = bdb_tool_entry_open;
300 bi->bi_tool_entry_close = bdb_tool_entry_close;
301 bi->bi_tool_entry_first = bdb_tool_entry_next;
302 bi->bi_tool_entry_next = bdb_tool_entry_next;
303 bi->bi_tool_entry_get = bdb_tool_entry_get;
304 bi->bi_tool_entry_put = bdb_tool_entry_put;
305 bi->bi_tool_entry_reindex = 0;
306 bi->bi_tool_sync = 0;
308 bi->bi_connection_init = 0;
309 bi->bi_connection_destroy = 0;