]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/init.c
Sync with HEAD
[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-2003 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_mutex_init( &bdb->bi_cache.lru_mutex );
108         ldap_pvt_thread_mutex_init( &bdb->bi_cache.c_dntree.bei_kids_mutex );
109         ldap_pvt_thread_rdwr_init ( &bdb->bi_cache.c_rwlock );
110
111         be->be_private = bdb;
112
113         return 0;
114 }
115
116 int
117 bdb_bt_compare(
118         DB *db, 
119         const DBT *usrkey,
120         const DBT *curkey
121 )
122 {
123         unsigned char *u, *c;
124         int i, x;
125
126         u = usrkey->data;
127         c = curkey->data;
128
129 #ifdef WORDS_BIGENDIAN
130         for( i = 0; i < (int)sizeof(ID); i++)
131 #else
132         for( i = sizeof(ID)-1; i >= 0; i--)
133 #endif
134         {
135                 x = u[i] - c[i];
136                 if( x ) return x;
137         }
138
139         return 0;
140 }
141
142 static int
143 bdb_db_open( BackendDB *be )
144 {
145         int rc, i;
146         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
147         u_int32_t flags;
148 #ifdef HAVE_EBCDIC
149         char path[MAXPATHLEN];
150 #endif
151
152 #ifdef NEW_LOGGING
153         LDAP_LOG( BACK_BDB, ARGS, 
154                 "bdb_db_open: %s\n", be->be_suffix[0].bv_val, 0, 0 );
155 #else
156         Debug( LDAP_DEBUG_ARGS,
157                 "bdb_db_open: %s\n",
158                 be->be_suffix[0].bv_val, 0, 0 );
159 #endif
160
161 #ifndef BDB_MULTIPLE_SUFFIXES
162         if ( be->be_suffix[1].bv_val ) {
163 #ifdef NEW_LOGGING
164         LDAP_LOG( BACK_BDB, ERR, 
165                 "bdb_db_open: only one suffix allowed\n", 0, 0, 0 );
166 #else
167         Debug( LDAP_DEBUG_ANY,
168                 "bdb_db_open: only one suffix allowed\n", 0, 0, 0 );
169 #endif
170                 return -1;
171         }
172 #endif
173         /* we should check existance of dbenv_home and db_directory */
174
175         rc = db_env_create( &bdb->bi_dbenv, 0 );
176         if( rc != 0 ) {
177 #ifdef NEW_LOGGING
178                 LDAP_LOG( BACK_BDB, ERR, 
179                         "bdb_db_open: db_env_create failed: %s (%d)\n", 
180                         db_strerror(rc), rc, 0 );
181 #else
182                 Debug( LDAP_DEBUG_ANY,
183                         "bdb_db_open: db_env_create failed: %s (%d)\n",
184                         db_strerror(rc), rc, 0 );
185 #endif
186                 return rc;
187         }
188
189         flags = DB_INIT_MPOOL | DB_THREAD | DB_CREATE
190                 | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN;
191         
192 #if 0
193         /* Never do automatic recovery, must perform it manually.
194          * Otherwise restarting with gentlehup will corrupt the
195          * database.
196          */
197         if( !(slapMode & SLAP_TOOL_MODE) ) flags |= DB_RECOVER;
198 #endif
199
200         /* If a key was set, use shared memory for the BDB environment */
201         if ( bdb->bi_shm_key ) {
202                 bdb->bi_dbenv->set_shm_key( bdb->bi_dbenv, bdb->bi_shm_key );
203                 flags |= DB_SYSTEM_MEM;
204         }
205
206         bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0].bv_val );
207         bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
208         bdb->bi_dbenv->set_lk_detect( bdb->bi_dbenv, bdb->bi_lock_detect );
209
210 #ifdef SLAP_IDL_CACHE
211         if ( bdb->bi_idl_cache_max_size ) {
212                 bdb->bi_idl_tree = NULL;
213                 ldap_pvt_thread_rdwr_init( &bdb->bi_idl_tree_rwlock );
214                 ldap_pvt_thread_mutex_init( &bdb->bi_idl_tree_lrulock );
215                 bdb->bi_idl_cache_size = 0;
216         }
217 #endif
218
219 #ifdef BDB_SUBDIRS
220         {
221                 char dir[MAXPATHLEN], *ptr;
222                 
223                 if (bdb->bi_dbenv_home[0] == '.') {
224                         /* If home is a relative path, relative subdirs
225                          * are just concat'd by BDB. We don't want the
226                          * path to be concat'd twice, e.g.
227                          * ./test-db/./test-db/tmp
228                          */
229                         ptr = dir;
230                 } else {
231                         ptr = lutil_strcopy( dir, bdb->bi_dbenv_home );
232                         *ptr++ = LDAP_DIRSEP[0];
233 #ifdef HAVE_EBCDIC
234                         __atoe( dir );
235 #endif
236                 }
237
238                 strcpy( ptr, BDB_TMP_SUBDIR );
239 #ifdef HAVE_EBCDIC
240                 __atoe( ptr );
241 #endif
242                 rc = bdb->bi_dbenv->set_tmp_dir( bdb->bi_dbenv, dir );
243                 if( rc != 0 ) {
244 #ifdef NEW_LOGGING
245                         LDAP_LOG( BACK_BDB, ERR, 
246                                 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n", 
247                                 dir, db_strerror(rc), rc );
248 #else
249                         Debug( LDAP_DEBUG_ANY,
250                                 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
251                                 dir, db_strerror(rc), rc );
252 #endif
253                         return rc;
254                 }
255
256                 strcpy( ptr, BDB_LG_SUBDIR );
257 #ifdef HAVE_EBCDIC
258                 __atoe( ptr );
259 #endif
260                 rc = bdb->bi_dbenv->set_lg_dir( bdb->bi_dbenv, dir );
261                 if( rc != 0 ) {
262 #ifdef NEW_LOGGING
263                         LDAP_LOG( BACK_BDB, ERR, 
264                                 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n", 
265                                 dir, db_strerror(rc), rc );
266 #else
267                         Debug( LDAP_DEBUG_ANY,
268                                 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
269                                 dir, db_strerror(rc), rc );
270 #endif
271                         return rc;
272                 }
273
274                 strcpy( ptr, BDB_DATA_SUBDIR );
275 #ifdef HAVE_EBCDIC
276                 __atoe( ptr );
277 #endif
278                 rc = bdb->bi_dbenv->set_data_dir( bdb->bi_dbenv, dir );
279                 if( rc != 0 ) {
280 #ifdef NEW_LOGGING
281                         LDAP_LOG( BACK_BDB, ERR, 
282                                 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
283                                 dir, db_strerror(rc), rc );
284 #else
285                         Debug( LDAP_DEBUG_ANY,
286                                 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
287                                 dir, db_strerror(rc), rc );
288 #endif
289                         return rc;
290                 }
291         }
292 #endif
293
294 #ifdef NEW_LOGGING
295         LDAP_LOG( BACK_BDB, DETAIL1, 
296                 "bdb_db_open: dbenv_open %s\n", bdb->bi_dbenv_home, 0, 0 );
297 #else
298         Debug( LDAP_DEBUG_TRACE,
299                 "bdb_db_open: dbenv_open(%s)\n",
300                 bdb->bi_dbenv_home, 0, 0);
301 #endif
302
303 #ifdef HAVE_EBCDIC
304         strcpy( path, bdb->bi_dbenv_home );
305         __atoe( path );
306         rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
307                 path,
308                 flags,
309                 bdb->bi_dbenv_mode );
310 #else
311         rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
312                 bdb->bi_dbenv_home,
313                 flags,
314                 bdb->bi_dbenv_mode );
315 #endif
316         if( rc != 0 ) {
317 #ifdef NEW_LOGGING
318                 LDAP_LOG( BACK_BDB, ERR, 
319                         "bdb_db_open: dbenv_open failed: %s (%d)\n", 
320                         db_strerror(rc), rc, 0 );
321 #else
322                 Debug( LDAP_DEBUG_ANY,
323                         "bdb_db_open: dbenv_open failed: %s (%d)\n",
324                         db_strerror(rc), rc, 0 );
325 #endif
326                 return rc;
327         }
328
329         if( bdb->bi_dbenv_xflags != 0 ) {
330                 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
331                         bdb->bi_dbenv_xflags, 1);
332                 if( rc != 0 ) {
333 #ifdef NEW_LOGGING
334                         LDAP_LOG( BACK_BDB, ERR, 
335                                 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n", 
336                                 db_strerror(rc), rc, 0 );
337 #else
338                         Debug( LDAP_DEBUG_ANY,
339                                 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
340                                 db_strerror(rc), rc, 0 );
341 #endif
342                         return rc;
343                 }
344         }
345
346         flags = DB_THREAD | bdb->bi_db_opflags;
347
348         bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
349                 BDB_INDICES * sizeof(struct bdb_db_info *) );
350
351         /* open (and create) main database */
352         for( i = 0; bdbi_databases[i].name; i++ ) {
353                 struct bdb_db_info *db;
354
355                 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
356
357                 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
358                 if( rc != 0 ) {
359 #ifdef NEW_LOGGING
360                         LDAP_LOG( BACK_BDB, ERR, 
361                                 "bdb_db_open: db_create(%s) failed: %s (%d)\n", 
362                                 bdb->bi_dbenv_home, db_strerror(rc), rc );
363 #else
364                         Debug( LDAP_DEBUG_ANY,
365                                 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
366                                 bdb->bi_dbenv_home, db_strerror(rc), rc );
367 #endif
368                         return rc;
369                 }
370
371                 if( i == BDB_ID2ENTRY ) {
372                         rc = db->bdi_db->set_bt_compare( db->bdi_db,
373                                 bdb_bt_compare );
374                         rc = db->bdi_db->set_pagesize( db->bdi_db,
375                                 BDB_ID2ENTRY_PAGESIZE );
376                         if ( slapMode & SLAP_TOOL_READMAIN ) {
377                                 flags |= DB_RDONLY;
378                         } else {
379                                 flags |= DB_CREATE;
380                         }
381                 } else {
382                         rc = db->bdi_db->set_flags( db->bdi_db, 
383                                 DB_DUP | DB_DUPSORT );
384 #ifndef BDB_HIER
385                         rc = db->bdi_db->set_dup_compare( db->bdi_db,
386                                 bdb_bt_compare );
387                         if ( slapMode & SLAP_TOOL_READONLY ) {
388                                 flags |= DB_RDONLY;
389                         } else {
390                                 flags |= DB_CREATE;
391                         }
392 #else
393                         rc = db->bdi_db->set_dup_compare( db->bdi_db,
394                                 bdb_dup_compare );
395                         rc = db->bdi_db->set_bt_compare( db->bdi_db,
396                                 bdb_bt_compare );
397                         if ( slapMode & (SLAP_TOOL_READONLY|SLAP_TOOL_READMAIN) ) {
398                                 flags |= DB_RDONLY;
399                         } else {
400                                 flags |= DB_CREATE;
401                         }
402 #endif
403                         rc = db->bdi_db->set_pagesize( db->bdi_db,
404                                 BDB_PAGESIZE );
405                 }
406
407 #ifdef HAVE_EBCDIC
408                 strcpy( path, bdbi_databases[i].file );
409                 __atoe( path );
410                 rc = DB_OPEN( db->bdi_db,
411                         path,
412                 /*      bdbi_databases[i].name, */ NULL,
413                         bdbi_databases[i].type,
414                         bdbi_databases[i].flags | flags | DB_AUTO_COMMIT,
415                         bdb->bi_dbenv_mode );
416 #else
417                 rc = DB_OPEN( db->bdi_db,
418                         bdbi_databases[i].file,
419                 /*      bdbi_databases[i].name, */ NULL,
420                         bdbi_databases[i].type,
421                         bdbi_databases[i].flags | flags | DB_AUTO_COMMIT,
422                         bdb->bi_dbenv_mode );
423 #endif
424
425                 if( rc != 0 ) {
426 #ifdef NEW_LOGGING
427                         LDAP_LOG( BACK_BDB, ERR, 
428                                 "bdb_db_open: db_create(%s) failed: %s (%d)\n", 
429                                 bdb->bi_dbenv_home, db_strerror(rc), rc );
430 #else
431                         Debug( LDAP_DEBUG_ANY,
432                                 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
433                                 bdb->bi_dbenv_home, db_strerror(rc), rc );
434 #endif
435                         return rc;
436                 }
437
438                 flags &= ~(DB_CREATE | DB_RDONLY);
439                 db->bdi_name = bdbi_databases[i].name;
440                 bdb->bi_databases[i] = db;
441         }
442
443         bdb->bi_databases[i] = NULL;
444         bdb->bi_ndatabases = i;
445
446         /* get nextid */
447         rc = bdb_last_id( be, NULL );
448         if( rc != 0 ) {
449 #ifdef NEW_LOGGING
450                         LDAP_LOG( BACK_BDB, ERR, 
451                                 "bdb_db_open: last_id(%s) failed: %s (%d)\n", 
452                                 bdb->bi_dbenv_home, db_strerror(rc), rc );
453 #else
454                 Debug( LDAP_DEBUG_ANY,
455                         "bdb_db_open: last_id(%s) failed: %s (%d)\n",
456                         bdb->bi_dbenv_home, db_strerror(rc), rc );
457 #endif
458                 return rc;
459         }
460
461         /* <insert> open (and create) index databases */
462         return 0;
463 }
464
465 static int
466 bdb_db_close( BackendDB *be )
467 {
468         int rc;
469         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
470         struct bdb_db_info *db;
471 #ifdef SLAP_IDL_CACHE
472         bdb_idl_cache_entry_t *entry, *next_entry;
473 #endif
474
475         while( bdb->bi_ndatabases-- ) {
476                 db = bdb->bi_databases[bdb->bi_ndatabases];
477                 rc = db->bdi_db->close( db->bdi_db, 0 );
478                 /* Lower numbered names are not strdup'd */
479                 if( bdb->bi_ndatabases >= BDB_NDB )
480                         free( db->bdi_name );
481                 free( db );
482         }
483         free( bdb->bi_databases );
484         bdb_attr_index_destroy( bdb->bi_attrs );
485
486         bdb_cache_release_all (&bdb->bi_cache);
487
488 #ifdef SLAP_IDL_CACHE
489         if ( bdb->bi_idl_cache_max_size ) {
490                 ldap_pvt_thread_rdwr_wlock ( &bdb->bi_idl_tree_rwlock );
491                 avl_free( bdb->bi_idl_tree, NULL );
492                 entry = bdb->bi_idl_lru_head;
493                 while ( entry != NULL ) {
494                         next_entry = entry->idl_lru_next;
495                         if ( entry->idl )
496                                 free( entry->idl );
497                         free( entry->kstr.bv_val );
498                         free( entry );
499                         entry = next_entry;
500                 }
501                 ldap_pvt_thread_rdwr_wunlock ( &bdb->bi_idl_tree_rwlock );
502         }
503 #endif
504
505         return 0;
506 }
507
508 static int
509 bdb_db_destroy( BackendDB *be )
510 {
511         int rc;
512         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
513
514         /* close db environment */
515         if( bdb->bi_dbenv ) {
516                 /* force a checkpoint */
517                 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
518                 if( rc != 0 ) {
519 #ifdef NEW_LOGGING
520                         LDAP_LOG( BACK_BDB, ERR, 
521                                 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
522                                 db_strerror(rc), rc, 0 );
523 #else
524                         Debug( LDAP_DEBUG_ANY,
525                                 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
526                                 db_strerror(rc), rc, 0 );
527 #endif
528                 }
529
530                 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
531                 bdb->bi_dbenv = NULL;
532                 if( rc != 0 ) {
533 #ifdef NEW_LOGGING
534                         LDAP_LOG( BACK_BDB, ERR, 
535                                 "bdb_db_destroy: close failed: %s (%d)\n", 
536                                 db_strerror(rc), rc, 0 );
537 #else
538                         Debug( LDAP_DEBUG_ANY,
539                                 "bdb_db_destroy: close failed: %s (%d)\n",
540                                 db_strerror(rc), rc, 0 );
541 #endif
542                         return rc;
543                 }
544         }
545
546         if( bdb->bi_dbenv_home ) ch_free( bdb->bi_dbenv_home );
547
548         ldap_pvt_thread_rdwr_destroy ( &bdb->bi_cache.c_rwlock );
549         ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.lru_mutex );
550         ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_dntree.bei_kids_mutex );
551         ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
552         ldap_pvt_thread_mutex_destroy( &bdb->bi_database_mutex );
553 #ifdef SLAP_IDL_CACHE
554         if ( bdb->bi_idl_cache_max_size ) {
555                 ldap_pvt_thread_rdwr_destroy( &bdb->bi_idl_tree_rwlock );
556                 ldap_pvt_thread_mutex_destroy( &bdb->bi_idl_tree_lrulock );
557         }
558 #endif
559
560         ch_free( bdb );
561         be->be_private = NULL;
562
563         return 0;
564 }
565
566 #if     (defined(SLAPD_BDB_DYNAMIC) && !defined(BDB_HIER)) || \
567         (defined(SLAPD_HDB_DYNAMIC) && defined(BDB_HIER))
568 int init_module( int argc, char *argv[] ) {
569         BackendInfo bi;
570
571         memset( &bi, '\0', sizeof(bi) );
572 #ifdef BDB_HIER
573         bi.bi_type = "hdb";
574 #else
575         bi.bi_type = "bdb";
576 #endif
577         bi.bi_init = bdb_initialize;
578
579         backend_add( &bi );
580         return 0;
581 }
582 #endif /* SLAPD_BDB_DYNAMIC */
583
584 int
585 bdb_initialize(
586         BackendInfo     *bi
587 )
588 {
589         static char *controls[] = {
590                 LDAP_CONTROL_ASSERT,
591                 LDAP_CONTROL_MANAGEDSAIT,
592                 LDAP_CONTROL_NOOP,
593 #ifdef LDAP_CONTROL_PAGEDRESULTS
594                 LDAP_CONTROL_PAGEDRESULTS,
595 #endif
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 }