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 */
37 Debug( LDAP_DEBUG_TRACE, "bdb_open: initialize BDB backend\n",
44 bdb_close( BackendInfo *bi )
46 /* terminate the underlying database system */
51 bdb_db_init( Backend *be )
55 Debug( LDAP_DEBUG_ANY,
56 "bdb_db_init: Initializing BDB database\n",
59 /* allocate backend-database-specific stuff */
60 bdb = (struct bdb_info *) ch_calloc( 1, sizeof(struct bdb_info) );
62 /* DBEnv parameters */
63 bdb->bi_dbenv_home = ch_strdup( BDB_DBENV_HOME );
64 bdb->bi_dbenv_xflags = 0;
65 bdb->bi_dbenv_mode = DEFAULT_MODE;
72 bdb_db_open( BackendDB *be )
75 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
78 Debug( LDAP_DEBUG_ANY,
79 "bdb_db_open: opening database for %s\n",
80 be->be_suffix[0], 0, 0 );
82 /* we should check existance of dbenv_home and db_directory */
84 rc = db_env_create( &bdb->bi_dbenv, 0 );
86 Debug( LDAP_DEBUG_ANY,
87 "bdb_db_open: db_env_create failed: %s (%d)\n",
88 db_strerror(rc), rc, 0 );
92 flags = DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN |
93 DB_CREATE | DB_RECOVER | DB_THREAD;
95 #ifdef SLAPD_BDB_PRIVATE
98 flags |= DB_INIT_MPOOL;
101 bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0] );
102 bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
106 char dir[MAXPATHLEN];
107 size_t len = strlen( bdb->bi_dbenv_home );
109 strcpy( dir, bdb->bi_dbenv_home );
110 strcat( &dir[len], BDB_TMP_SUBDIR );
112 rc = bdb->bi_dbenv->set_tmp_dir( bdb->bi_dbenv, dir );
114 Debug( LDAP_DEBUG_ANY,
115 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
116 dir, db_strerror(rc), rc );
120 strcat( &dir[len], BDB_LG_SUBDIR );
122 rc = bdb->bi_dbenv->set_lg_dir( bdb->bi_dbenv, dir );
124 Debug( LDAP_DEBUG_ANY,
125 "bi_back_db_open: set_lg_dir(%s) failed: %s (%d)\n",
126 dir, db_strerror(rc), rc );
130 strcat( &dir[len], BDB_DATA_SUBDIR );
132 rc = bdb->bi_dbenv->set_data_dir( bdb->bi_dbenv, dir );
134 Debug( LDAP_DEBUG_ANY,
135 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
136 dir, db_strerror(rc), rc );
142 Debug( LDAP_DEBUG_TRACE,
143 "bi_back_db_open: dbenv_open(%s)\n",
144 bdb->bi_dbenv_home, 0, 0);
146 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
148 flags | bdb->bi_dbenv_xflags,
149 bdb->bi_dbenv_mode );
151 Debug( LDAP_DEBUG_ANY,
152 "bdb_db_open: dbenv_open failed: %s (%d)\n",
153 db_strerror(rc), rc, 0 );
157 flags = DB_THREAD | DB_CREATE;
159 /* open (and create) main database */
160 for( i = 0; i < BDB_INDICES; i++ ) {
161 struct bdb_db_info *db;
163 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
165 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
167 Debug( LDAP_DEBUG_ANY,
168 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
169 bdb->bi_dbenv_home, db_strerror(rc), rc );
173 rc = db->bdi_db->open( db->bdi_db,
174 bdbi_databases[i].file,
175 bdbi_databases[i].name,
176 bdbi_databases[i].type,
177 bdbi_databases[i].flags | flags,
178 bdb->bi_dbenv_mode );
181 Debug( LDAP_DEBUG_ANY,
182 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
183 bdb->bi_dbenv_home, db_strerror(rc), rc );
188 /* <insert> open (and create) index databases */
195 bdb_db_close( BackendDB *be )
198 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
200 /* force a checkpoint */
201 rc = txn_checkpoint( bdb->bi_dbenv, 0, 0, DB_FORCE );
203 Debug( LDAP_DEBUG_ANY,
204 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
205 db_strerror(rc), rc, 0 );
209 while( bdb->bi_ndatabases-- ) {
210 rc = bdb->bi_databases[bdb->bi_ndatabases]->bdi_db->close(
211 bdb->bi_databases[bdb->bi_ndatabases]->bdi_db, 0 );
218 bdb_db_destroy( BackendDB *be )
221 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
223 /* close db environment */
224 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
225 bdb->bi_dbenv = NULL;
227 Debug( LDAP_DEBUG_ANY,
228 "bdb_db_destroy: close failed: %s (%d)\n",
229 db_strerror(rc), rc, 0 );
236 #ifdef SLAPD_BDB_DYNAMIC
237 int back_bdb_LTX_init_module( int argc, char *argv[] ) {
240 memset( &bi, '\0', sizeof(bi) );
242 bi.bi_init = bdb_initialize;
247 #endif /* SLAPD_BDB_DYNAMIC */
254 static char *controls[] = {
255 LDAP_CONTROL_MANAGEDSAIT,
259 { /* version check */
260 int major, minor, patch;
261 char *version = db_version( &major, &minor, &patch );
263 if( major != DB_VERSION_MAJOR ||
264 minor != DB_VERSION_MINOR ||
265 patch < DB_VERSION_PATCH )
267 Debug( LDAP_DEBUG_ANY,
268 "bi_back_initialize: version mismatch\n"
269 "\texpected: " DB_VERSION_STRING "\n"
270 "\tgot: %s \n", version, 0, 0 );
273 Debug( LDAP_DEBUG_ANY, "bdb_initialize: %s\n",
278 db_env_set_func_malloc( ch_malloc );
279 db_env_set_func_realloc( ch_realloc );
280 db_env_set_func_free( ch_free );
281 db_env_set_func_yield( ldap_pvt_thread_yield );
284 bi->bi_controls = controls;
286 bi->bi_open = bdb_open;
287 bi->bi_close = bdb_close;
289 bi->bi_destroy = bdb_destroy;
291 bi->bi_db_init = bdb_db_init;
292 bi->bi_db_config = bdb_db_config;
293 bi->bi_db_open = bdb_db_open;
294 bi->bi_db_close = bdb_db_close;
295 bi->bi_db_destroy = bdb_db_destroy;
297 bi->bi_op_add = bdb_add;
298 bi->bi_op_compare = bdb_compare;
299 bi->bi_op_delete = bdb_delete;
300 bi->bi_op_search = bdb_search;
303 bi->bi_op_bind = bdb_bind;
304 bi->bi_op_unbind = bdb_unbind;
305 bi->bi_op_modify = bdb_modify;
306 bi->bi_op_modrdn = bdb_modrdn;
307 bi->bi_op_abandon = bdb_abandon;
309 bi->bi_extended = bdb_extended;
311 bi->bi_acl_group = bdb_group;
312 bi->bi_acl_attribute = bdb_attribute;
313 bi->bi_chk_referrals = bdb_referrals;
316 bi->bi_entry_release_rw = 0;
319 * hooks for slap tools
321 bi->bi_tool_entry_open = bdb_tool_entry_open;
322 bi->bi_tool_entry_close = bdb_tool_entry_close;
323 bi->bi_tool_entry_first = bdb_tool_entry_next;
324 bi->bi_tool_entry_next = bdb_tool_entry_next;
325 bi->bi_tool_entry_get = bdb_tool_entry_get;
326 bi->bi_tool_entry_put = bdb_tool_entry_put;
327 bi->bi_tool_entry_reindex = 0;
328 bi->bi_tool_sync = 0;
330 bi->bi_connection_init = 0;
331 bi->bi_connection_destroy = 0;