1 /* init.c - initialize bdb backend */
4 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <ac/string.h>
13 #include <ac/socket.h>
17 static char *bdbi_dbnames[BDB_INDICES] = {
18 "nextid", "id2entry", "dn2entry"
22 bi_back_destroy( BackendInfo *bi )
28 bi_back_open( BackendInfo *bi )
30 /* initialize the underlying database system */
35 bi_back_close( BackendInfo *bi )
37 /* terminate the underlying database system */
42 bi_back_db_init( Backend *be )
46 /* allocate backend-database-specific stuff */
47 bdb = (struct bdb_info *) ch_calloc( 1, sizeof(struct bdb_info) );
49 /* DBEnv parameters */
50 bdb->bi_dbenv_home = ch_strdup( BDB_DBENV_HOME );
51 bdb->bi_dbenv_xflags = 0;
52 bdb->bi_dbenv_mode = DEFAULT_MODE;
59 bi_back_db_open( BackendDB *be )
62 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
64 /* we should check existance of dbenv_home and db_directory */
66 rc = db_env_create( &bdb->bi_dbenv, 0 );
68 Debug( LDAP_DEBUG_ANY,
69 "bi_back_db_open: db_env_create failed: %s (%d)\n",
70 db_strerror(rc), rc, 0 );
74 flags = DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN |
75 DB_CREATE | DB_RECOVER | DB_THREAD;
77 #ifdef SLAPD_BDB_PRIVATE
80 flags |= DB_INIT_MPOOL;
83 bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0] );
84 bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
88 size_t len = strlen( bdb->bi_dbenv_home );
90 strcpy( dir, bdb->bi_dbenv_home );
91 strcat( &dir[len], BDB_TMP_SUBDIR );
93 rc = bdb->bi_dbenv->set_tmp_dir( bdb->bi_dbenv, dir );
95 Debug( LDAP_DEBUG_ANY,
96 "bi_back_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
97 dir, db_strerror(rc), rc );
101 strcat( &dir[len], BDB_LG_SUBDIR );
103 rc = bdb->bi_dbenv->set_lg_dir( bdb->bi_dbenv, dir );
105 Debug( LDAP_DEBUG_ANY,
106 "bi_back_db_open: set_lg_dir(%s) failed: %s (%d)\n",
107 dir, db_strerror(rc), rc );
111 strcat( &dir[len], BDB_DATA_SUBDIR );
113 rc = bdb->bi_dbenv->set_data_dir( bdb->bi_dbenv, dir );
115 Debug( LDAP_DEBUG_ANY,
116 "bi_back_db_open: set_data_dir(%s) failed: %s (%d)\n",
117 dir, db_strerror(rc), rc );
122 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
124 flags | bdb->bi_dbenv_xflags,
125 bdb->bi_dbenv_mode );
127 Debug( LDAP_DEBUG_ANY,
128 "bi_back_db_open: dbenv_open(%s) failed: %s (%d)\n",
129 bdb->bi_dbenv_home, db_strerror(rc), rc );
136 if( be->be_read_only ) {
144 /* open (and create) main database */
145 for( i = 0; i < BDB_INDICES; i++ ) {
146 struct bdb_db_info *db;
148 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
150 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
152 Debug( LDAP_DEBUG_ANY,
153 "bi_back_db_open: db_create(%s) failed: %s (%d)\n",
154 bdb->bi_dbenv_home, db_strerror(rc), rc );
158 rc = db->bdi_db->open( db->bdi_db,
163 bdb->bi_dbenv_mode );
166 Debug( LDAP_DEBUG_ANY,
167 "bi_back_db_open: db_open(%s) failed: %s (%d)\n",
168 bdb->bi_dbenv_home, db_strerror(rc), rc );
173 /* <insert> open (and create) index databases */
180 bi_back_db_close( BackendDB *be )
183 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
185 /* force a checkpoint */
186 rc = txn_checkpoint( bdb->bi_dbenv, 0, 0, DB_FORCE );
188 Debug( LDAP_DEBUG_ANY,
189 "bi_back_db_destroy: txn_checkpoint failed: %s (%d)\n",
190 db_strerror(rc), rc, 0 );
194 while( bdb->bi_ndatabases-- ) {
195 rc = bdb->bi_databases[bdb->bi_ndatabases]->bdi_db->close(
196 bdb->bi_databases[bdb->bi_ndatabases]->bdi_db, 0 );
203 bi_back_db_destroy( BackendDB *be )
206 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
208 /* close db environment */
209 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
210 bdb->bi_dbenv = NULL;
212 Debug( LDAP_DEBUG_ANY,
213 "bi_back_db_destroy: close failed: %s (%d)\n",
214 db_strerror(rc), rc, 0 );
221 #ifdef SLAPD_BDB_DYNAMIC
222 int back_bdb_LTX_init_module( int argc, char *argv[] ) {
225 memset( &bi, '\0', sizeof(bi) );
227 bi.bi_init = bi_back_initialize;
232 #endif /* SLAPD_BDB_DYNAMIC */
239 static char *controls[] = {
240 LDAP_CONTROL_MANAGEDSAIT,
244 { /* version check */
245 int major, minor, patch;
246 char *version = db_version( &major, &minor, &patch );
248 if( major != DB_VERSION_MAJOR ||
249 minor != DB_VERSION_MINOR ||
250 patch < DB_VERSION_PATCH )
252 Debug( LDAP_DEBUG_ANY,
253 "bi_back_initialize: version mismatch\n"
254 "\texpected: " DB_VERSION_STRING "\n"
255 "\tgot: %s \n", version, 0, 0 );
258 Debug( LDAP_DEBUG_ANY, "bi_back_initialize: %s\n",
262 bi->bi_controls = controls;
264 bi->bi_open = bi_back_open;
265 bi->bi_close = bi_back_close;
267 bi->bi_destroy = bi_back_destroy;
269 bi->bi_db_init = bi_back_db_init;
270 bi->bi_db_config = 0;
271 bi->bi_db_open = bi_back_db_open;
272 bi->bi_db_close = bi_back_db_close;
273 bi->bi_db_destroy = bi_back_db_destroy;
276 bi->bi_op_bind = bi_back_bind;
277 bi->bi_op_unbind = bi_back_unbind;
278 bi->bi_op_search = bi_back_search;
279 bi->bi_op_compare = bi_back_compare;
280 bi->bi_op_modify = bi_back_modify;
281 bi->bi_op_modrdn = bi_back_modrdn;
282 bi->bi_op_add = bi_back_add;
283 bi->bi_op_delete = bi_back_delete;
284 bi->bi_op_abandon = bi_back_abandon;
286 bi->bi_extended = bi_back_extended;
288 bi->bi_entry_release_rw = bi_back_entry_release_rw;
289 bi->bi_acl_group = bi_back_group;
290 bi->bi_acl_attribute = bi_back_attribute;
291 bi->bi_chk_referrals = bi_back_referrals;
295 * hooks for slap tools
297 bi->bi_tool_entry_open = bdb_tool_entry_open;
298 bi->bi_tool_entry_close = bdb_tool_entry_close;
299 bi->bi_tool_entry_first = bdb_tool_entry_next;
300 bi->bi_tool_entry_next = bdb_tool_entry_next;
301 bi->bi_tool_entry_get = bdb_tool_entry_get;
302 bi->bi_tool_entry_put = bdb_tool_entry_put;
303 bi->bi_tool_entry_reindex = 0;
304 bi->bi_tool_sync = 0;
306 bi->bi_connection_init = 0;
307 bi->bi_connection_destroy = 0;