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 struct bdbi_database {
22 } bdbi_databases[BDB_INDICES] = {
23 { "nextid", "nextid", DB_BTREE, 0 },
24 { "id2entry", "id2entry", DB_BTREE, 0 },
25 { "dn2entry", "dn2entry", DB_BTREE, 0 }
29 bdb_destroy( BackendInfo *bi )
35 bdb_open( BackendInfo *bi )
37 /* initialize the underlying database system */
42 bdb_close( BackendInfo *bi )
44 /* terminate the underlying database system */
49 bdb_db_init( Backend *be )
53 /* allocate backend-database-specific stuff */
54 bdb = (struct bdb_info *) ch_calloc( 1, sizeof(struct bdb_info) );
56 /* DBEnv parameters */
57 bdb->bi_dbenv_home = ch_strdup( BDB_DBENV_HOME );
58 bdb->bi_dbenv_xflags = 0;
59 bdb->bi_dbenv_mode = DEFAULT_MODE;
66 bdb_db_open( BackendDB *be )
69 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
71 /* we should check existance of dbenv_home and db_directory */
73 rc = db_env_create( &bdb->bi_dbenv, 0 );
75 Debug( LDAP_DEBUG_ANY,
76 "bi_back_db_open: db_env_create failed: %s (%d)\n",
77 db_strerror(rc), rc, 0 );
81 flags = DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN |
82 DB_CREATE | DB_RECOVER | DB_THREAD;
84 #ifdef SLAPD_BDB_PRIVATE
87 flags |= DB_INIT_MPOOL;
90 bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0] );
91 bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
95 size_t len = strlen( bdb->bi_dbenv_home );
97 strcpy( dir, bdb->bi_dbenv_home );
98 strcat( &dir[len], BDB_TMP_SUBDIR );
100 rc = bdb->bi_dbenv->set_tmp_dir( bdb->bi_dbenv, dir );
102 Debug( LDAP_DEBUG_ANY,
103 "bi_back_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
104 dir, db_strerror(rc), rc );
108 strcat( &dir[len], BDB_LG_SUBDIR );
110 rc = bdb->bi_dbenv->set_lg_dir( bdb->bi_dbenv, dir );
112 Debug( LDAP_DEBUG_ANY,
113 "bi_back_db_open: set_lg_dir(%s) failed: %s (%d)\n",
114 dir, db_strerror(rc), rc );
118 strcat( &dir[len], BDB_DATA_SUBDIR );
120 rc = bdb->bi_dbenv->set_data_dir( bdb->bi_dbenv, dir );
122 Debug( LDAP_DEBUG_ANY,
123 "bi_back_db_open: set_data_dir(%s) failed: %s (%d)\n",
124 dir, db_strerror(rc), rc );
129 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
131 flags | bdb->bi_dbenv_xflags,
132 bdb->bi_dbenv_mode );
134 Debug( LDAP_DEBUG_ANY,
135 "bi_back_db_open: dbenv_open(%s) failed: %s (%d)\n",
136 bdb->bi_dbenv_home, db_strerror(rc), rc );
140 flags = DB_THREAD | DB_CREATE;
142 /* open (and create) main database */
143 for( i = 0; i < BDB_INDICES; i++ ) {
144 struct bdb_db_info *db;
146 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
148 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
150 Debug( LDAP_DEBUG_ANY,
151 "bi_back_db_open: db_create(%s) failed: %s (%d)\n",
152 bdb->bi_dbenv_home, db_strerror(rc), rc );
156 rc = db->bdi_db->open( db->bdi_db,
157 bdbi_databases[i].file,
158 bdbi_databases[i].name,
159 bdbi_databases[i].type,
160 bdbi_databases[i].flags | flags,
161 bdb->bi_dbenv_mode );
164 Debug( LDAP_DEBUG_ANY,
165 "bi_back_db_open: db_open(%s) failed: %s (%d)\n",
166 bdb->bi_dbenv_home, db_strerror(rc), rc );
171 /* <insert> open (and create) index databases */
178 bdb_db_close( BackendDB *be )
181 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
183 /* force a checkpoint */
184 rc = txn_checkpoint( bdb->bi_dbenv, 0, 0, DB_FORCE );
186 Debug( LDAP_DEBUG_ANY,
187 "bi_back_db_destroy: txn_checkpoint failed: %s (%d)\n",
188 db_strerror(rc), rc, 0 );
192 while( bdb->bi_ndatabases-- ) {
193 rc = bdb->bi_databases[bdb->bi_ndatabases]->bdi_db->close(
194 bdb->bi_databases[bdb->bi_ndatabases]->bdi_db, 0 );
201 bdb_db_destroy( BackendDB *be )
204 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
206 /* close db environment */
207 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
208 bdb->bi_dbenv = NULL;
210 Debug( LDAP_DEBUG_ANY,
211 "bi_back_db_destroy: close failed: %s (%d)\n",
212 db_strerror(rc), rc, 0 );
219 #ifdef SLAPD_BDB_DYNAMIC
220 int back_bdb_LTX_init_module( int argc, char *argv[] ) {
223 memset( &bi, '\0', sizeof(bi) );
225 bi.bi_init = bdb_initialize;
230 #endif /* SLAPD_BDB_DYNAMIC */
237 static char *controls[] = {
238 LDAP_CONTROL_MANAGEDSAIT,
242 { /* version check */
243 int major, minor, patch;
244 char *version = db_version( &major, &minor, &patch );
246 if( major != DB_VERSION_MAJOR ||
247 minor != DB_VERSION_MINOR ||
248 patch < DB_VERSION_PATCH )
250 Debug( LDAP_DEBUG_ANY,
251 "bi_back_initialize: version mismatch\n"
252 "\texpected: " DB_VERSION_STRING "\n"
253 "\tgot: %s \n", version, 0, 0 );
256 Debug( LDAP_DEBUG_ANY, "bdb_initialize: %s\n",
260 bi->bi_controls = controls;
262 bi->bi_open = bdb_open;
263 bi->bi_close = bdb_close;
265 bi->bi_destroy = bdb_destroy;
267 bi->bi_db_init = bdb_db_init;
268 bi->bi_db_config = 0;
269 bi->bi_db_open = bdb_db_open;
270 bi->bi_db_close = bdb_db_close;
271 bi->bi_db_destroy = bdb_db_destroy;
274 bi->bi_op_bind = bi_back_bind;
275 bi->bi_op_unbind = bi_back_unbind;
276 bi->bi_op_search = bi_back_search;
277 bi->bi_op_compare = bi_back_compare;
278 bi->bi_op_modify = bi_back_modify;
279 bi->bi_op_modrdn = bi_back_modrdn;
280 bi->bi_op_add = bi_back_add;
281 bi->bi_op_delete = bi_back_delete;
282 bi->bi_op_abandon = bi_back_abandon;
284 bi->bi_extended = bi_back_extended;
286 bi->bi_entry_release_rw = bi_back_entry_release_rw;
287 bi->bi_acl_group = bi_back_group;
288 bi->bi_acl_attribute = bi_back_attribute;
289 bi->bi_chk_referrals = bi_back_referrals;
293 * hooks for slap tools
295 bi->bi_tool_entry_open = bdb_tool_entry_open;
296 bi->bi_tool_entry_close = bdb_tool_entry_close;
297 bi->bi_tool_entry_first = bdb_tool_entry_next;
298 bi->bi_tool_entry_next = bdb_tool_entry_next;
299 bi->bi_tool_entry_get = bdb_tool_entry_get;
300 bi->bi_tool_entry_put = bdb_tool_entry_put;
301 bi->bi_tool_entry_reindex = 0;
302 bi->bi_tool_sync = 0;
304 bi->bi_connection_init = 0;
305 bi->bi_connection_destroy = 0;