]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/init.c
Hack to allow --enable-bdb --disable-ldbm, needs more work.
[openldap] / servers / slapd / back-bdb / init.c
1 /* init.c - initialize bdb backend */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11 #include <ac/string.h>
12
13 #include "back-bdb.h"
14 #include "external.h"
15
16 static struct bdbi_database {
17         char *file;
18         char *name;
19         int type;
20         int flags;
21 } bdbi_databases[BDB_INDICES] = {
22         { "nextid" BDB_SUFFIX, "nextid", DB_BTREE, 0 },
23         { "dn2entry" BDB_SUFFIX, "dn2entry", DB_BTREE, 0 },
24         { "id2entry" BDB_SUFFIX, "id2entry", DB_BTREE, 0 },
25 };
26
27 #if 0
28 static int
29 bdb_destroy( BackendInfo *bi )
30 {
31         return 0;
32 }
33
34 static int
35 bdb_open( BackendInfo *bi )
36 {
37         /* initialize the underlying database system */
38         Debug( LDAP_DEBUG_TRACE, "bdb_open: initialize BDB backend\n",
39                 0, 0, 0 );
40
41         return 0;
42 }
43
44 static int
45 bdb_close( BackendInfo *bi )
46 {
47         /* terminate the underlying database system */
48         return 0;
49 }
50 #endif
51
52 static int
53 bdb_db_init( BackendDB *be )
54 {
55         struct bdb_info *bdb;
56
57         Debug( LDAP_DEBUG_ANY,
58                 "bdb_db_init: Initializing BDB database\n",
59                 0, 0, 0 );
60
61         /* allocate backend-database-specific stuff */
62         bdb = (struct bdb_info *) ch_calloc( 1, sizeof(struct bdb_info) );
63
64         /* DBEnv parameters */
65         bdb->bi_dbenv_home = ch_strdup( BDB_DBENV_HOME );
66         bdb->bi_dbenv_xflags = 0;
67         bdb->bi_dbenv_mode = DEFAULT_MODE;
68
69 #ifndef NO_THREADS
70         bdb->bi_lock_detect = DB_LOCK_NORUN;
71 #endif
72
73         be->be_private = bdb;
74         return 0;
75 }
76
77 #ifndef NO_THREADS
78 static void *lock_detect_task( void *arg )
79 {
80         struct bdb_info *bdb = (struct bdb_info *) arg;
81
82         while( bdb->bi_dbenv != NULL ) {
83                 int rc;
84                 sleep( bdb->bi_lock_detect_seconds );
85
86                 rc = lock_detect( bdb->bi_dbenv, 0,
87                         bdb->bi_lock_detect, NULL );
88
89                 if( rc != 0 ) {
90                         break;
91                 }
92         }
93
94         return NULL;
95 }
96 #endif
97
98 static int
99 bdb_db_open( BackendDB *be )
100 {
101         int rc, i;
102         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
103         u_int32_t flags;
104
105         Debug( LDAP_DEBUG_ARGS,
106                 "bdb_db_open: %s\n",
107                 be->be_suffix[0], 0, 0 );
108
109         /* we should check existance of dbenv_home and db_directory */
110
111         rc = db_env_create( &bdb->bi_dbenv, 0 );
112         if( rc != 0 ) {
113                 Debug( LDAP_DEBUG_ANY,
114                         "bdb_db_open: db_env_create failed: %s (%d)\n",
115                         db_strerror(rc), rc, 0 );
116                 return rc;
117         }
118
119         flags = DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN | 
120                 DB_THREAD | DB_CREATE | DB_RECOVER;
121
122         bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0] );
123         bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
124
125 #ifdef BDB_SUBDIRS
126         {
127                 char dir[MAXPATHLEN];
128                 size_t len = strlen( bdb->bi_dbenv_home );
129
130                 strcpy( dir, bdb->bi_dbenv_home );
131                 strcat( &dir[len], BDB_TMP_SUBDIR );
132                 
133                 rc = bdb->bi_dbenv->set_tmp_dir( bdb->bi_dbenv, dir );
134                 if( rc != 0 ) {
135                         Debug( LDAP_DEBUG_ANY,
136                                 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
137                                 dir, db_strerror(rc), rc );
138                         return rc;
139                 }
140
141                 strcat( &dir[len], BDB_LG_SUBDIR );
142
143                 rc = bdb->bi_dbenv->set_lg_dir( bdb->bi_dbenv, dir );
144                 if( rc != 0 ) {
145                         Debug( LDAP_DEBUG_ANY,
146                                 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
147                                 dir, db_strerror(rc), rc );
148                         return rc;
149                 }
150
151                 strcat( &dir[len], BDB_DATA_SUBDIR );
152
153                 rc = bdb->bi_dbenv->set_data_dir( bdb->bi_dbenv, dir );
154                 if( rc != 0 ) {
155                         Debug( LDAP_DEBUG_ANY,
156                                 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
157                                 dir, db_strerror(rc), rc );
158                         return rc;
159                 }
160         }
161 #endif
162
163         Debug( LDAP_DEBUG_TRACE,
164                 "bdb_db_open: dbenv_open(%s)\n",
165                 bdb->bi_dbenv_home, 0, 0);
166
167         rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
168                 bdb->bi_dbenv_home,
169                 flags | bdb->bi_dbenv_xflags,
170                 bdb->bi_dbenv_mode );
171         if( rc != 0 ) {
172                 Debug( LDAP_DEBUG_ANY,
173                         "bdb_db_open: dbenv_open failed: %s (%d)\n",
174                         db_strerror(rc), rc, 0 );
175                 return rc;
176         }
177
178         flags = DB_THREAD | DB_CREATE;
179
180         bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
181                 BDB_INDICES * sizeof(struct bdb_db_info *) );
182
183         /* open (and create) main database */
184         for( i = 0; i < BDB_INDICES; i++ ) {
185                 struct bdb_db_info *db;
186
187                 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
188
189                 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
190                 if( rc != 0 ) {
191                         Debug( LDAP_DEBUG_ANY,
192                                 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
193                                 bdb->bi_dbenv_home, db_strerror(rc), rc );
194                         return rc;
195                 }
196
197                 rc = db->bdi_db->open( db->bdi_db,
198                         bdbi_databases[i].file,
199                         bdbi_databases[i].name,
200                         bdbi_databases[i].type,
201                         bdbi_databases[i].flags | flags,
202                         bdb->bi_dbenv_mode );
203
204                 if( rc != 0 ) {
205                         Debug( LDAP_DEBUG_ANY,
206                                 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
207                                 bdb->bi_dbenv_home, db_strerror(rc), rc );
208                         return rc;
209                 }
210
211                 bdb->bi_databases[i] = db;
212         }
213
214         /* get nextid */
215         rc = bdb_last_id( be, NULL );
216         if( rc != 0 ) {
217                 Debug( LDAP_DEBUG_ANY,
218                         "bdb_db_open: last_id(%s) failed: %s (%d)\n",
219                         bdb->bi_dbenv_home, db_strerror(rc), rc );
220                 return rc;
221         }
222
223
224         /* <insert> open (and create) index databases */
225
226
227 #ifndef NO_THREADS
228         if( bdb->bi_lock_detect != DB_LOCK_NORUN ) {
229                 /* listener as a separate THREAD */
230                 rc = ldap_pvt_thread_create( &bdb->bi_lock_detect_tid,
231                         1, lock_detect_task, bdb );
232         }
233 #endif
234         return 0;
235 }
236
237 static int
238 bdb_db_close( BackendDB *be )
239 {
240         int rc;
241         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
242
243         /* force a checkpoint */
244         rc = txn_checkpoint( bdb->bi_dbenv, 0, 0, DB_FORCE );
245         if( rc != 0 ) {
246                 Debug( LDAP_DEBUG_ANY,
247                         "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
248                         db_strerror(rc), rc, 0 );
249                 return rc;
250         }
251
252         while( bdb->bi_ndatabases-- ) {
253                 rc = bdb->bi_databases[bdb->bi_ndatabases]->bdi_db->close(
254                         bdb->bi_databases[bdb->bi_ndatabases]->bdi_db, 0 );
255         }
256
257         return 0;
258 }
259
260 static int
261 bdb_db_destroy( BackendDB *be )
262 {
263         int rc;
264         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
265
266         /* close db environment */
267         if( bdb->bi_dbenv ) {
268                 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
269                 bdb->bi_dbenv = NULL;
270                 if( rc != 0 ) {
271                         Debug( LDAP_DEBUG_ANY,
272                                 "bdb_db_destroy: close failed: %s (%d)\n",
273                                 db_strerror(rc), rc, 0 );
274                         return rc;
275                 }
276         }
277
278         return 0;
279 }
280
281 #ifdef SLAPD_BDB_DYNAMIC
282 int back_bdb_LTX_init_module( int argc, char *argv[] ) {
283         BackendInfo bi;
284
285         memset( &bi, '\0', sizeof(bi) );
286         bi.bi_type = "bdb";
287         bi.bi_init = bdb_initialize;
288
289         backend_add( &bi );
290         return 0;
291 }
292 #endif /* SLAPD_BDB_DYNAMIC */
293
294 int
295 bdb_initialize(
296         BackendInfo     *bi
297 )
298 {
299         static char *controls[] = {
300                 LDAP_CONTROL_MANAGEDSAIT,
301                 NULL
302         };
303
304         {       /* version check */
305                 int major, minor, patch;
306                 char *version = db_version( &major, &minor, &patch );
307
308                 if( major != DB_VERSION_MAJOR ||
309                         minor != DB_VERSION_MINOR ||
310                         patch < DB_VERSION_PATCH )
311                 {
312                         Debug( LDAP_DEBUG_ANY,
313                                 "bi_back_initialize: version mismatch\n"
314                                 "\texpected: " DB_VERSION_STRING "\n"
315                                 "\tgot: %s \n", version, 0, 0 );
316                 }
317
318                 Debug( LDAP_DEBUG_ANY, "bdb_initialize: %s\n",
319                         version, 0, 0 );
320         }
321
322 #if 0
323         db_env_set_func_malloc( ch_malloc );
324         db_env_set_func_realloc( ch_realloc );
325         db_env_set_func_free( ch_free );
326 #endif
327         db_env_set_func_yield( ldap_pvt_thread_yield );
328
329         bi->bi_controls = controls;
330
331         bi->bi_open = 0;
332         bi->bi_close = 0;
333         bi->bi_config = 0;
334         bi->bi_destroy = 0;
335
336         bi->bi_db_init = bdb_db_init;
337         bi->bi_db_config = bdb_db_config;
338         bi->bi_db_open = bdb_db_open;
339         bi->bi_db_close = bdb_db_close;
340         bi->bi_db_destroy = bdb_db_destroy;
341
342         bi->bi_op_add = bdb_add;
343         bi->bi_op_bind = bdb_bind;
344         bi->bi_op_compare = bdb_compare;
345         bi->bi_op_delete = bdb_delete;
346         bi->bi_op_modify = bdb_modify;
347         bi->bi_op_modrdn = bdb_modrdn;
348         bi->bi_op_search = bdb_search;
349
350 #if 0
351         bi->bi_op_unbind = bdb_unbind;
352         bi->bi_op_abandon = bdb_abandon;
353
354         bi->bi_extended = bdb_extended;
355
356         bi->bi_acl_group = bdb_group;
357         bi->bi_acl_attribute = bdb_attribute;
358 #endif
359         bi->bi_chk_referrals = bdb_referrals;
360
361         bi->bi_entry_release_rw = 0;
362
363         /*
364          * hooks for slap tools
365          */
366         bi->bi_tool_entry_open = bdb_tool_entry_open;
367         bi->bi_tool_entry_close = bdb_tool_entry_close;
368         bi->bi_tool_entry_first = bdb_tool_entry_next;
369         bi->bi_tool_entry_next = bdb_tool_entry_next;
370         bi->bi_tool_entry_get = bdb_tool_entry_get;
371         bi->bi_tool_entry_put = bdb_tool_entry_put;
372 #if BDB_REINDEX
373         bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
374 #else
375         bi->bi_tool_entry_reindex = 0;
376 #endif
377         bi->bi_tool_sync = 0;
378
379         bi->bi_connection_init = 0;
380         bi->bi_connection_destroy = 0;
381
382         return 0;
383 }