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