]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/init.c
Sync with HEAD as of 14-March-2004
[openldap] / servers / slapd / back-bdb / init.c
1 /* init.c - initialize bdb backend */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2000-2004 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20 #include <ac/string.h>
21 #include <ac/unistd.h>
22 #include <ac/stdlib.h>
23
24 #include "back-bdb.h"
25 #include "external.h"
26 #include <lutil.h>
27
28 static const struct bdbi_database {
29         char *file;
30         char *name;
31         int type;
32         int flags;
33 } bdbi_databases[] = {
34         { "id2entry" BDB_SUFFIX, "id2entry", DB_BTREE, 0 },
35         { "dn2id" BDB_SUFFIX, "dn2id", DB_BTREE, 0 },
36         { NULL, NULL, 0, 0 }
37 };
38
39 struct berval bdb_uuid = { 0, NULL };
40
41 typedef void * db_malloc(size_t);
42 typedef void * db_realloc(void *, size_t);
43
44 #if 0
45 static int
46 bdb_open( BackendInfo *bi )
47 {
48         return 0;
49 }
50
51 static int
52 bdb_destroy( BackendInfo *bi )
53 {
54         return 0;
55 }
56
57 static int
58 bdb_close( BackendInfo *bi )
59 {
60         /* terminate the underlying database system */
61         return 0;
62 }
63 #endif
64
65 static int
66 bdb_db_init( BackendDB *be )
67 {
68         struct bdb_info *bdb;
69
70 #ifdef NEW_LOGGING
71         LDAP_LOG( BACK_BDB, ENTRY, "bdb_db_init", 0, 0, 0 );
72 #else
73         Debug( LDAP_DEBUG_ANY,
74                 "bdb_db_init: Initializing BDB database\n",
75                 0, 0, 0 );
76 #endif
77
78         /* indicate system schema supported */
79         be->be_flags |=
80                 SLAP_BFLAG_INCREMENT |
81 #ifdef BDB_SUBENTRIES
82                 SLAP_BFLAG_SUBENTRIES |
83 #endif
84 #ifdef BDB_ALIASES
85                 SLAP_BFLAG_ALIASES |
86 #endif
87                 SLAP_BFLAG_REFERRALS;
88
89         /* allocate backend-database-specific stuff */
90         bdb = (struct bdb_info *) ch_calloc( 1, sizeof(struct bdb_info) );
91
92         /* DBEnv parameters */
93         bdb->bi_dbenv_home = ch_strdup( SLAPD_DEFAULT_DB_DIR );
94         bdb->bi_dbenv_xflags = 0;
95         bdb->bi_dbenv_mode = SLAPD_DEFAULT_DB_MODE;
96
97         bdb->bi_cache.c_maxsize = DEFAULT_CACHE_SIZE;
98
99         bdb->bi_lock_detect = DB_LOCK_DEFAULT;
100         bdb->bi_search_stack_depth = DEFAULT_SEARCH_STACK_DEPTH;
101         bdb->bi_search_stack = NULL;
102
103         LDAP_LIST_INIT (&bdb->bi_psearch_list);
104
105         ldap_pvt_thread_mutex_init( &bdb->bi_database_mutex );
106         ldap_pvt_thread_mutex_init( &bdb->bi_lastid_mutex );
107         ldap_pvt_thread_rdwr_init ( &bdb->bi_pslist_rwlock );
108         ldap_pvt_thread_mutex_init( &bdb->bi_cache.lru_mutex );
109         ldap_pvt_thread_mutex_init( &bdb->bi_cache.c_dntree.bei_kids_mutex );
110         ldap_pvt_thread_rdwr_init ( &bdb->bi_cache.c_rwlock );
111
112         be->be_private = bdb;
113
114         return 0;
115 }
116
117 int
118 bdb_bt_compare(
119         DB *db, 
120         const DBT *usrkey,
121         const DBT *curkey
122 )
123 {
124         unsigned char *u, *c;
125         int i, x;
126
127         u = usrkey->data;
128         c = curkey->data;
129
130 #ifdef WORDS_BIGENDIAN
131         for( i = 0; i < (int)sizeof(ID); i++)
132 #else
133         for( i = sizeof(ID)-1; i >= 0; i--)
134 #endif
135         {
136                 x = u[i] - c[i];
137                 if( x ) return x;
138         }
139
140         return 0;
141 }
142
143 static int
144 bdb_db_open( BackendDB *be )
145 {
146         int rc, i;
147         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
148         u_int32_t flags;
149 #ifdef HAVE_EBCDIC
150         char path[MAXPATHLEN];
151 #endif
152
153 #ifdef NEW_LOGGING
154         LDAP_LOG( BACK_BDB, ARGS, 
155                 "bdb_db_open: %s\n", be->be_suffix[0].bv_val, 0, 0 );
156 #else
157         Debug( LDAP_DEBUG_ARGS,
158                 "bdb_db_open: %s\n",
159                 be->be_suffix[0].bv_val, 0, 0 );
160 #endif
161
162 #ifndef BDB_MULTIPLE_SUFFIXES
163         if ( be->be_suffix[1].bv_val ) {
164 #ifdef NEW_LOGGING
165         LDAP_LOG( BACK_BDB, ERR, 
166                 "bdb_db_open: only one suffix allowed\n", 0, 0, 0 );
167 #else
168         Debug( LDAP_DEBUG_ANY,
169                 "bdb_db_open: only one suffix allowed\n", 0, 0, 0 );
170 #endif
171                 return -1;
172         }
173 #endif
174         /* we should check existance of dbenv_home and db_directory */
175
176         rc = db_env_create( &bdb->bi_dbenv, 0 );
177         if( rc != 0 ) {
178 #ifdef NEW_LOGGING
179                 LDAP_LOG( BACK_BDB, ERR, 
180                         "bdb_db_open: db_env_create failed: %s (%d)\n", 
181                         db_strerror(rc), rc, 0 );
182 #else
183                 Debug( LDAP_DEBUG_ANY,
184                         "bdb_db_open: db_env_create failed: %s (%d)\n",
185                         db_strerror(rc), rc, 0 );
186 #endif
187                 return rc;
188         }
189
190         flags = DB_INIT_MPOOL | DB_THREAD | DB_CREATE
191                 | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN;
192         
193 #if 0
194         /* Never do automatic recovery, must perform it manually.
195          * Otherwise restarting with gentlehup will corrupt the
196          * database.
197          */
198         if( !(slapMode & SLAP_TOOL_MODE) ) flags |= DB_RECOVER;
199 #endif
200
201         /* If a key was set, use shared memory for the BDB environment */
202         if ( bdb->bi_shm_key ) {
203                 bdb->bi_dbenv->set_shm_key( bdb->bi_dbenv, bdb->bi_shm_key );
204                 flags |= DB_SYSTEM_MEM;
205         }
206
207         bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0].bv_val );
208         bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
209         bdb->bi_dbenv->set_lk_detect( bdb->bi_dbenv, bdb->bi_lock_detect );
210
211 #ifdef SLAP_IDL_CACHE
212         if ( bdb->bi_idl_cache_max_size ) {
213                 bdb->bi_idl_tree = NULL;
214                 ldap_pvt_thread_rdwr_init( &bdb->bi_idl_tree_rwlock );
215                 ldap_pvt_thread_mutex_init( &bdb->bi_idl_tree_lrulock );
216                 bdb->bi_idl_cache_size = 0;
217         }
218 #endif
219
220 #ifdef BDB_SUBDIRS
221         {
222                 char dir[MAXPATHLEN], *ptr;
223                 
224                 if (bdb->bi_dbenv_home[0] == '.') {
225                         /* If home is a relative path, relative subdirs
226                          * are just concat'd by BDB. We don't want the
227                          * path to be concat'd twice, e.g.
228                          * ./test-db/./test-db/tmp
229                          */
230                         ptr = dir;
231                 } else {
232                         ptr = lutil_strcopy( dir, bdb->bi_dbenv_home );
233                         *ptr++ = LDAP_DIRSEP[0];
234 #ifdef HAVE_EBCDIC
235                         __atoe( dir );
236 #endif
237                 }
238
239                 strcpy( ptr, BDB_TMP_SUBDIR );
240 #ifdef HAVE_EBCDIC
241                 __atoe( ptr );
242 #endif
243                 rc = bdb->bi_dbenv->set_tmp_dir( bdb->bi_dbenv, dir );
244                 if( rc != 0 ) {
245 #ifdef NEW_LOGGING
246                         LDAP_LOG( BACK_BDB, ERR, 
247                                 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n", 
248                                 dir, db_strerror(rc), rc );
249 #else
250                         Debug( LDAP_DEBUG_ANY,
251                                 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
252                                 dir, db_strerror(rc), rc );
253 #endif
254                         return rc;
255                 }
256
257                 strcpy( ptr, BDB_LG_SUBDIR );
258 #ifdef HAVE_EBCDIC
259                 __atoe( ptr );
260 #endif
261                 rc = bdb->bi_dbenv->set_lg_dir( bdb->bi_dbenv, dir );
262                 if( rc != 0 ) {
263 #ifdef NEW_LOGGING
264                         LDAP_LOG( BACK_BDB, ERR, 
265                                 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n", 
266                                 dir, db_strerror(rc), rc );
267 #else
268                         Debug( LDAP_DEBUG_ANY,
269                                 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
270                                 dir, db_strerror(rc), rc );
271 #endif
272                         return rc;
273                 }
274
275                 strcpy( ptr, BDB_DATA_SUBDIR );
276 #ifdef HAVE_EBCDIC
277                 __atoe( ptr );
278 #endif
279                 rc = bdb->bi_dbenv->set_data_dir( bdb->bi_dbenv, dir );
280                 if( rc != 0 ) {
281 #ifdef NEW_LOGGING
282                         LDAP_LOG( BACK_BDB, ERR, 
283                                 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
284                                 dir, db_strerror(rc), rc );
285 #else
286                         Debug( LDAP_DEBUG_ANY,
287                                 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
288                                 dir, db_strerror(rc), rc );
289 #endif
290                         return rc;
291                 }
292         }
293 #endif
294
295 #ifdef NEW_LOGGING
296         LDAP_LOG( BACK_BDB, DETAIL1, 
297                 "bdb_db_open: dbenv_open %s\n", bdb->bi_dbenv_home, 0, 0 );
298 #else
299         Debug( LDAP_DEBUG_TRACE,
300                 "bdb_db_open: dbenv_open(%s)\n",
301                 bdb->bi_dbenv_home, 0, 0);
302 #endif
303
304 #ifdef HAVE_EBCDIC
305         strcpy( path, bdb->bi_dbenv_home );
306         __atoe( path );
307         rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
308                 path,
309                 flags,
310                 bdb->bi_dbenv_mode );
311 #else
312         rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
313                 bdb->bi_dbenv_home,
314                 flags,
315                 bdb->bi_dbenv_mode );
316 #endif
317         if( rc != 0 ) {
318 #ifdef NEW_LOGGING
319                 LDAP_LOG( BACK_BDB, ERR, 
320                         "bdb_db_open: dbenv_open failed: %s (%d)\n", 
321                         db_strerror(rc), rc, 0 );
322 #else
323                 Debug( LDAP_DEBUG_ANY,
324                         "bdb_db_open: dbenv_open failed: %s (%d)\n",
325                         db_strerror(rc), rc, 0 );
326 #endif
327                 return rc;
328         }
329
330         if( bdb->bi_dbenv_xflags != 0 ) {
331                 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
332                         bdb->bi_dbenv_xflags, 1);
333                 if( rc != 0 ) {
334 #ifdef NEW_LOGGING
335                         LDAP_LOG( BACK_BDB, ERR, 
336                                 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n", 
337                                 db_strerror(rc), rc, 0 );
338 #else
339                         Debug( LDAP_DEBUG_ANY,
340                                 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
341                                 db_strerror(rc), rc, 0 );
342 #endif
343                         return rc;
344                 }
345         }
346
347         flags = DB_THREAD | bdb->bi_db_opflags;
348
349         bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
350                 BDB_INDICES * sizeof(struct bdb_db_info *) );
351
352         /* open (and create) main database */
353         for( i = 0; bdbi_databases[i].name; i++ ) {
354                 struct bdb_db_info *db;
355
356                 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
357
358                 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
359                 if( rc != 0 ) {
360 #ifdef NEW_LOGGING
361                         LDAP_LOG( BACK_BDB, ERR, 
362                                 "bdb_db_open: db_create(%s) failed: %s (%d)\n", 
363                                 bdb->bi_dbenv_home, db_strerror(rc), rc );
364 #else
365                         Debug( LDAP_DEBUG_ANY,
366                                 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
367                                 bdb->bi_dbenv_home, db_strerror(rc), rc );
368 #endif
369                         return rc;
370                 }
371
372                 if( i == BDB_ID2ENTRY ) {
373                         rc = db->bdi_db->set_bt_compare( db->bdi_db,
374                                 bdb_bt_compare );
375                         rc = db->bdi_db->set_pagesize( db->bdi_db,
376                                 BDB_ID2ENTRY_PAGESIZE );
377                         if ( slapMode & SLAP_TOOL_READMAIN ) {
378                                 flags |= DB_RDONLY;
379                         } else {
380                                 flags |= DB_CREATE;
381                         }
382                 } else {
383                         rc = db->bdi_db->set_flags( db->bdi_db, 
384                                 DB_DUP | DB_DUPSORT );
385 #ifndef BDB_HIER
386                         rc = db->bdi_db->set_dup_compare( db->bdi_db,
387                                 bdb_bt_compare );
388                         if ( slapMode & SLAP_TOOL_READONLY ) {
389                                 flags |= DB_RDONLY;
390                         } else {
391                                 flags |= DB_CREATE;
392                         }
393 #else
394                         rc = db->bdi_db->set_dup_compare( db->bdi_db,
395                                 bdb_dup_compare );
396                         rc = db->bdi_db->set_bt_compare( db->bdi_db,
397                                 bdb_bt_compare );
398                         if ( slapMode & (SLAP_TOOL_READONLY|SLAP_TOOL_READMAIN) ) {
399                                 flags |= DB_RDONLY;
400                         } else {
401                                 flags |= DB_CREATE;
402                         }
403 #endif
404                         rc = db->bdi_db->set_pagesize( db->bdi_db,
405                                 BDB_PAGESIZE );
406                 }
407
408 #ifdef HAVE_EBCDIC
409                 strcpy( path, bdbi_databases[i].file );
410                 __atoe( path );
411                 rc = DB_OPEN( db->bdi_db,
412                         path,
413                 /*      bdbi_databases[i].name, */ NULL,
414                         bdbi_databases[i].type,
415                         bdbi_databases[i].flags | flags,
416                         bdb->bi_dbenv_mode );
417 #else
418                 rc = DB_OPEN( db->bdi_db,
419                         bdbi_databases[i].file,
420                 /*      bdbi_databases[i].name, */ NULL,
421                         bdbi_databases[i].type,
422                         bdbi_databases[i].flags | flags,
423                         bdb->bi_dbenv_mode );
424 #endif
425
426                 if( rc != 0 ) {
427 #ifdef NEW_LOGGING
428                         LDAP_LOG( BACK_BDB, ERR, 
429                                 "bdb_db_open: db_create(%s) failed: %s (%d)\n", 
430                                 bdb->bi_dbenv_home, db_strerror(rc), rc );
431 #else
432                         Debug( LDAP_DEBUG_ANY,
433                                 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
434                                 bdb->bi_dbenv_home, db_strerror(rc), rc );
435 #endif
436                         return rc;
437                 }
438
439                 flags &= ~(DB_CREATE | DB_RDONLY);
440                 db->bdi_name = bdbi_databases[i].name;
441                 bdb->bi_databases[i] = db;
442         }
443
444         bdb->bi_databases[i] = NULL;
445         bdb->bi_ndatabases = i;
446
447         /* get nextid */
448         rc = bdb_last_id( be, NULL );
449         if( rc != 0 ) {
450 #ifdef NEW_LOGGING
451                         LDAP_LOG( BACK_BDB, ERR, 
452                                 "bdb_db_open: last_id(%s) failed: %s (%d)\n", 
453                                 bdb->bi_dbenv_home, db_strerror(rc), rc );
454 #else
455                 Debug( LDAP_DEBUG_ANY,
456                         "bdb_db_open: last_id(%s) failed: %s (%d)\n",
457                         bdb->bi_dbenv_home, db_strerror(rc), rc );
458 #endif
459                 return rc;
460         }
461
462         /* <insert> open (and create) index databases */
463         return 0;
464 }
465
466 static int
467 bdb_db_close( BackendDB *be )
468 {
469         int rc;
470         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
471         struct bdb_db_info *db;
472 #ifdef SLAP_IDL_CACHE
473         bdb_idl_cache_entry_t *entry, *next_entry;
474 #endif
475
476         while( bdb->bi_ndatabases-- ) {
477                 db = bdb->bi_databases[bdb->bi_ndatabases];
478                 rc = db->bdi_db->close( db->bdi_db, 0 );
479                 /* Lower numbered names are not strdup'd */
480                 if( bdb->bi_ndatabases >= BDB_NDB )
481                         free( db->bdi_name );
482                 free( db );
483         }
484         free( bdb->bi_databases );
485         bdb_attr_index_destroy( bdb->bi_attrs );
486
487         bdb_cache_release_all (&bdb->bi_cache);
488
489 #ifdef SLAP_IDL_CACHE
490         if ( bdb->bi_idl_cache_max_size ) {
491                 ldap_pvt_thread_rdwr_wlock ( &bdb->bi_idl_tree_rwlock );
492                 avl_free( bdb->bi_idl_tree, NULL );
493                 entry = bdb->bi_idl_lru_head;
494                 while ( entry != NULL ) {
495                         next_entry = entry->idl_lru_next;
496                         if ( entry->idl )
497                                 free( entry->idl );
498                         free( entry->kstr.bv_val );
499                         free( entry );
500                         entry = next_entry;
501                 }
502                 ldap_pvt_thread_rdwr_wunlock ( &bdb->bi_idl_tree_rwlock );
503         }
504 #endif
505
506         return 0;
507 }
508
509 static int
510 bdb_db_destroy( BackendDB *be )
511 {
512         int rc;
513         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
514
515         /* close db environment */
516         if( bdb->bi_dbenv ) {
517                 /* force a checkpoint */
518                 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
519                 if( rc != 0 ) {
520 #ifdef NEW_LOGGING
521                         LDAP_LOG( BACK_BDB, ERR, 
522                                 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
523                                 db_strerror(rc), rc, 0 );
524 #else
525                         Debug( LDAP_DEBUG_ANY,
526                                 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
527                                 db_strerror(rc), rc, 0 );
528 #endif
529                 }
530
531                 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
532                 bdb->bi_dbenv = NULL;
533                 if( rc != 0 ) {
534 #ifdef NEW_LOGGING
535                         LDAP_LOG( BACK_BDB, ERR, 
536                                 "bdb_db_destroy: close failed: %s (%d)\n", 
537                                 db_strerror(rc), rc, 0 );
538 #else
539                         Debug( LDAP_DEBUG_ANY,
540                                 "bdb_db_destroy: close failed: %s (%d)\n",
541                                 db_strerror(rc), rc, 0 );
542 #endif
543                         return rc;
544                 }
545         }
546
547         if( bdb->bi_dbenv_home ) ch_free( bdb->bi_dbenv_home );
548
549         ldap_pvt_thread_rdwr_destroy ( &bdb->bi_cache.c_rwlock );
550         ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.lru_mutex );
551         ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_dntree.bei_kids_mutex );
552         ldap_pvt_thread_rdwr_destroy ( &bdb->bi_pslist_rwlock );
553         ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
554         ldap_pvt_thread_mutex_destroy( &bdb->bi_database_mutex );
555 #ifdef SLAP_IDL_CACHE
556         if ( bdb->bi_idl_cache_max_size ) {
557                 ldap_pvt_thread_rdwr_destroy( &bdb->bi_idl_tree_rwlock );
558                 ldap_pvt_thread_mutex_destroy( &bdb->bi_idl_tree_lrulock );
559         }
560 #endif
561
562         ch_free( bdb );
563         be->be_private = NULL;
564
565         return 0;
566 }
567
568 #if     (defined(SLAPD_BDB_DYNAMIC) && !defined(BDB_HIER)) || \
569         (defined(SLAPD_HDB_DYNAMIC) && defined(BDB_HIER))
570 int init_module( int argc, char *argv[] ) {
571         BackendInfo bi;
572
573         memset( &bi, '\0', sizeof(bi) );
574 #ifdef BDB_HIER
575         bi.bi_type = "hdb";
576 #else
577         bi.bi_type = "bdb";
578 #endif
579         bi.bi_init = bdb_initialize;
580
581         backend_add( &bi );
582         return 0;
583 }
584 #endif /* SLAPD_BDB_DYNAMIC */
585
586 int
587 bdb_initialize(
588         BackendInfo     *bi
589 )
590 {
591         static char *controls[] = {
592                 LDAP_CONTROL_ASSERT,
593                 LDAP_CONTROL_MANAGEDSAIT,
594                 LDAP_CONTROL_NOOP,
595                 LDAP_CONTROL_PAGEDRESULTS,
596 #ifdef LDAP_CONTROL_SUBENTRIES
597                 LDAP_CONTROL_SUBENTRIES,
598 #endif
599                 LDAP_CONTROL_VALUESRETURNFILTER,
600                 NULL
601         };
602
603         bi->bi_controls = controls;
604
605         /* initialize the underlying database system */
606 #ifdef NEW_LOGGING
607         LDAP_LOG( BACK_BDB, ENTRY, "bdb_db_initialize\n", 0, 0, 0 );
608 #else
609         Debug( LDAP_DEBUG_TRACE, "bdb_initialize: initialize BDB backend\n",
610                 0, 0, 0 );
611 #endif
612
613         {       /* version check */
614                 int major, minor, patch;
615                 char *version = db_version( &major, &minor, &patch );
616 #ifdef HAVE_EBCDIC
617                 char v2[1024];
618
619                 /* All our stdio does an ASCII to EBCDIC conversion on
620                  * the output. Strings from the BDB library are already
621                  * in EBCDIC; we have to go back and forth...
622                  */
623                 strcpy( v2, version );
624                 __etoa( v2 );
625                 version = v2;
626 #endif
627
628                 if( major != DB_VERSION_MAJOR ||
629                         minor != DB_VERSION_MINOR ||
630                         patch < DB_VERSION_PATCH )
631                 {
632 #ifdef NEW_LOGGING
633                         LDAP_LOG( BACK_BDB, ERR, 
634                                 "bdb_initialize: BDB library version mismatch:"
635                                 " expected " DB_VERSION_STRING ","
636                                 " got %s\n", version, 0, 0 );
637 #else
638                         Debug( LDAP_DEBUG_ANY,
639                                 "bdb_initialize: BDB library version mismatch:"
640                                 " expected " DB_VERSION_STRING ","
641                                 " got %s\n", version, 0, 0 );
642 #endif
643                 }
644
645 #ifdef NEW_LOGGING
646                 LDAP_LOG( BACK_BDB, DETAIL1, 
647                         "bdb_db_initialize: %s\n", version, 0, 0 );
648 #else
649                 Debug( LDAP_DEBUG_ANY, "bdb_initialize: %s\n",
650                         version, 0, 0 );
651 #endif
652         }
653
654         db_env_set_func_free( ber_memfree );
655         db_env_set_func_malloc( (db_malloc *)ber_memalloc );
656         db_env_set_func_realloc( (db_realloc *)ber_memrealloc );
657 #ifndef NO_THREAD
658         /* This is a no-op on a NO_THREAD build. Leave the default
659          * alone so that BDB will sleep on interprocess conflicts.
660          */
661         db_env_set_func_yield( ldap_pvt_thread_yield );
662 #endif
663
664         {
665                 static char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
666
667                 bdb_uuid.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ));
668                 bdb_uuid.bv_val = uuidbuf;
669         }
670
671         bi->bi_open = 0;
672         bi->bi_close = 0;
673         bi->bi_config = 0;
674         bi->bi_destroy = 0;
675
676         bi->bi_db_init = bdb_db_init;
677         bi->bi_db_config = bdb_db_config;
678         bi->bi_db_open = bdb_db_open;
679         bi->bi_db_close = bdb_db_close;
680         bi->bi_db_destroy = bdb_db_destroy;
681
682         bi->bi_op_add = bdb_add;
683         bi->bi_op_bind = bdb_bind;
684         bi->bi_op_compare = bdb_compare;
685         bi->bi_op_delete = bdb_delete;
686         bi->bi_op_modify = bdb_modify;
687         bi->bi_op_modrdn = bdb_modrdn;
688         bi->bi_op_search = bdb_search;
689
690         bi->bi_op_unbind = 0;
691
692         bi->bi_op_abandon = bdb_abandon;
693         bi->bi_op_cancel = bdb_cancel;
694
695         bi->bi_extended = bdb_extended;
696
697         bi->bi_chk_referrals = bdb_referrals;
698         bi->bi_operational = bdb_operational;
699         bi->bi_has_subordinates = bdb_hasSubordinates;
700         bi->bi_entry_release_rw = bdb_entry_release;
701         bi->bi_entry_get_rw = bdb_entry_get;
702
703         /*
704          * hooks for slap tools
705          */
706         bi->bi_tool_entry_open = bdb_tool_entry_open;
707         bi->bi_tool_entry_close = bdb_tool_entry_close;
708         bi->bi_tool_entry_first = bdb_tool_entry_next;
709         bi->bi_tool_entry_next = bdb_tool_entry_next;
710         bi->bi_tool_entry_get = bdb_tool_entry_get;
711         bi->bi_tool_entry_put = bdb_tool_entry_put;
712         bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
713         bi->bi_tool_sync = 0;
714         bi->bi_tool_dn2id_get = bdb_tool_dn2id_get;
715         bi->bi_tool_id2entry_get = bdb_tool_id2entry_get;
716         bi->bi_tool_entry_modify = bdb_tool_entry_modify;
717
718         bi->bi_connection_init = 0;
719         bi->bi_connection_destroy = 0;
720
721         return 0;
722 }