]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/init.c
From 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-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 #ifdef SLAP_IDL_CACHE
200         if ( bdb->bi_idl_cache_max_size ) {
201                 bdb->bi_idl_tree = NULL;
202                 ldap_pvt_thread_rdwr_init( &bdb->bi_idl_tree_rwlock );
203                 ldap_pvt_thread_mutex_init( &bdb->bi_idl_tree_lrulock );
204                 bdb->bi_idl_cache_size = 0;
205         }
206 #endif
207
208 #ifdef BDB_SUBDIRS
209         {
210                 char dir[MAXPATHLEN], *ptr;
211                 
212                 if (bdb->bi_dbenv_home[0] == '.') {
213                         /* If home is a relative path, relative subdirs
214                          * are just concat'd by BDB. We don't want the
215                          * path to be concat'd twice, e.g.
216                          * ./test-db/./test-db/tmp
217                          */
218                         ptr = dir;
219                 } else {
220                         ptr = lutil_strcopy( dir, bdb->bi_dbenv_home );
221                         *ptr++ = LDAP_DIRSEP[0];
222 #ifdef HAVE_EBCDIC
223                         __atoe( dir );
224 #endif
225                 }
226
227                 strcpy( ptr, BDB_TMP_SUBDIR );
228 #ifdef HAVE_EBCDIC
229                 __atoe( ptr );
230 #endif
231                 rc = bdb->bi_dbenv->set_tmp_dir( bdb->bi_dbenv, dir );
232                 if( rc != 0 ) {
233 #ifdef NEW_LOGGING
234                         LDAP_LOG( BACK_BDB, ERR, 
235                                 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n", 
236                                 dir, db_strerror(rc), rc );
237 #else
238                         Debug( LDAP_DEBUG_ANY,
239                                 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
240                                 dir, db_strerror(rc), rc );
241 #endif
242                         return rc;
243                 }
244
245                 strcpy( ptr, BDB_LG_SUBDIR );
246 #ifdef HAVE_EBCDIC
247                 __atoe( ptr );
248 #endif
249                 rc = bdb->bi_dbenv->set_lg_dir( bdb->bi_dbenv, dir );
250                 if( rc != 0 ) {
251 #ifdef NEW_LOGGING
252                         LDAP_LOG( BACK_BDB, ERR, 
253                                 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n", 
254                                 dir, db_strerror(rc), rc );
255 #else
256                         Debug( LDAP_DEBUG_ANY,
257                                 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
258                                 dir, db_strerror(rc), rc );
259 #endif
260                         return rc;
261                 }
262
263                 strcpy( ptr, BDB_DATA_SUBDIR );
264 #ifdef HAVE_EBCDIC
265                 __atoe( ptr );
266 #endif
267                 rc = bdb->bi_dbenv->set_data_dir( bdb->bi_dbenv, dir );
268                 if( rc != 0 ) {
269 #ifdef NEW_LOGGING
270                         LDAP_LOG( BACK_BDB, ERR, 
271                                 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
272                                 dir, db_strerror(rc), rc );
273 #else
274                         Debug( LDAP_DEBUG_ANY,
275                                 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
276                                 dir, db_strerror(rc), rc );
277 #endif
278                         return rc;
279                 }
280         }
281 #endif
282
283 #ifdef NEW_LOGGING
284         LDAP_LOG( BACK_BDB, DETAIL1, 
285                 "bdb_db_open: dbenv_open %s\n", bdb->bi_dbenv_home, 0, 0 );
286 #else
287         Debug( LDAP_DEBUG_TRACE,
288                 "bdb_db_open: dbenv_open(%s)\n",
289                 bdb->bi_dbenv_home, 0, 0);
290 #endif
291
292 #ifdef HAVE_EBCDIC
293         strcpy( path, bdb->bi_dbenv_home );
294         __atoe( path );
295         rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
296                 path,
297                 flags,
298                 bdb->bi_dbenv_mode );
299 #else
300         rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
301                 bdb->bi_dbenv_home,
302                 flags,
303                 bdb->bi_dbenv_mode );
304 #endif
305         if( rc != 0 ) {
306 #ifdef NEW_LOGGING
307                 LDAP_LOG( BACK_BDB, ERR, 
308                         "bdb_db_open: dbenv_open failed: %s (%d)\n", 
309                         db_strerror(rc), rc, 0 );
310 #else
311                 Debug( LDAP_DEBUG_ANY,
312                         "bdb_db_open: dbenv_open failed: %s (%d)\n",
313                         db_strerror(rc), rc, 0 );
314 #endif
315                 return rc;
316         }
317
318         if( bdb->bi_dbenv_xflags != 0 ) {
319                 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
320                         bdb->bi_dbenv_xflags, 1);
321                 if( rc != 0 ) {
322 #ifdef NEW_LOGGING
323                         LDAP_LOG( BACK_BDB, ERR, 
324                                 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n", 
325                                 db_strerror(rc), rc, 0 );
326 #else
327                         Debug( LDAP_DEBUG_ANY,
328                                 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
329                                 db_strerror(rc), rc, 0 );
330 #endif
331                         return rc;
332                 }
333         }
334
335         flags = DB_THREAD | bdb->bi_db_opflags;
336
337         bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
338                 BDB_INDICES * sizeof(struct bdb_db_info *) );
339
340         /* open (and create) main database */
341         for( i = 0; bdbi_databases[i].name; i++ ) {
342                 struct bdb_db_info *db;
343
344                 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
345
346                 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
347                 if( rc != 0 ) {
348 #ifdef NEW_LOGGING
349                         LDAP_LOG( BACK_BDB, ERR, 
350                                 "bdb_db_open: db_create(%s) failed: %s (%d)\n", 
351                                 bdb->bi_dbenv_home, db_strerror(rc), rc );
352 #else
353                         Debug( LDAP_DEBUG_ANY,
354                                 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
355                                 bdb->bi_dbenv_home, db_strerror(rc), rc );
356 #endif
357                         return rc;
358                 }
359
360                 if( i == BDB_ID2ENTRY ) {
361                         rc = db->bdi_db->set_bt_compare( db->bdi_db,
362                                 bdb_bt_compare );
363                         rc = db->bdi_db->set_pagesize( db->bdi_db,
364                                 BDB_ID2ENTRY_PAGESIZE );
365                         if ( slapMode & SLAP_TOOL_READMAIN ) {
366                                 flags |= DB_RDONLY;
367                         } else {
368                                 flags |= DB_CREATE;
369                         }
370                 } else {
371                         rc = db->bdi_db->set_flags( db->bdi_db, 
372                                 DB_DUP | DB_DUPSORT );
373 #ifndef BDB_HIER
374                         rc = db->bdi_db->set_dup_compare( db->bdi_db,
375                                 bdb_bt_compare );
376                         if ( slapMode & SLAP_TOOL_READONLY ) {
377                                 flags |= DB_RDONLY;
378                         } else {
379                                 flags |= DB_CREATE;
380                         }
381 #else
382                         rc = db->bdi_db->set_dup_compare( db->bdi_db,
383                                 bdb_dup_compare );
384                         rc = db->bdi_db->set_bt_compare( db->bdi_db,
385                                 bdb_bt_compare );
386                         if ( slapMode & (SLAP_TOOL_READONLY|SLAP_TOOL_READMAIN) ) {
387                                 flags |= DB_RDONLY;
388                         } else {
389                                 flags |= DB_CREATE;
390                         }
391 #endif
392                         rc = db->bdi_db->set_pagesize( db->bdi_db,
393                                 BDB_PAGESIZE );
394                 }
395
396 #ifdef HAVE_EBCDIC
397                 strcpy( path, bdbi_databases[i].file );
398                 __atoe( path );
399                 rc = DB_OPEN( db->bdi_db,
400                         path,
401                 /*      bdbi_databases[i].name, */ NULL,
402                         bdbi_databases[i].type,
403                         bdbi_databases[i].flags | flags,
404                         bdb->bi_dbenv_mode );
405 #else
406                 rc = DB_OPEN( db->bdi_db,
407                         bdbi_databases[i].file,
408                 /*      bdbi_databases[i].name, */ NULL,
409                         bdbi_databases[i].type,
410                         bdbi_databases[i].flags | flags,
411                         bdb->bi_dbenv_mode );
412 #endif
413
414                 if( rc != 0 ) {
415 #ifdef NEW_LOGGING
416                         LDAP_LOG( BACK_BDB, ERR, 
417                                 "bdb_db_open: db_create(%s) failed: %s (%d)\n", 
418                                 bdb->bi_dbenv_home, db_strerror(rc), rc );
419 #else
420                         Debug( LDAP_DEBUG_ANY,
421                                 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
422                                 bdb->bi_dbenv_home, db_strerror(rc), rc );
423 #endif
424                         return rc;
425                 }
426
427                 flags &= ~(DB_CREATE | DB_RDONLY);
428                 db->bdi_name = bdbi_databases[i].name;
429                 bdb->bi_databases[i] = db;
430         }
431
432         bdb->bi_databases[i] = NULL;
433         bdb->bi_ndatabases = i;
434
435         /* get nextid */
436         rc = bdb_last_id( be, NULL );
437         if( rc != 0 ) {
438 #ifdef NEW_LOGGING
439                         LDAP_LOG( BACK_BDB, ERR, 
440                                 "bdb_db_open: last_id(%s) failed: %s (%d)\n", 
441                                 bdb->bi_dbenv_home, db_strerror(rc), rc );
442 #else
443                 Debug( LDAP_DEBUG_ANY,
444                         "bdb_db_open: last_id(%s) failed: %s (%d)\n",
445                         bdb->bi_dbenv_home, db_strerror(rc), rc );
446 #endif
447                 return rc;
448         }
449
450         /* <insert> open (and create) index databases */
451         return 0;
452 }
453
454 static int
455 bdb_db_close( BackendDB *be )
456 {
457         int rc;
458         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
459         struct bdb_db_info *db;
460 #ifdef SLAP_IDL_CACHE
461         bdb_idl_cache_entry_t *entry, *next_entry;
462 #endif
463
464         while( bdb->bi_ndatabases-- ) {
465                 db = bdb->bi_databases[bdb->bi_ndatabases];
466                 rc = db->bdi_db->close( db->bdi_db, 0 );
467                 /* Lower numbered names are not strdup'd */
468                 if( bdb->bi_ndatabases >= BDB_NDB )
469                         free( db->bdi_name );
470                 free( db );
471         }
472         free( bdb->bi_databases );
473         bdb_attr_index_destroy( bdb->bi_attrs );
474
475         bdb_cache_release_all (&bdb->bi_cache);
476
477 #ifdef SLAP_IDL_CACHE
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 #endif
493
494         return 0;
495 }
496
497 static int
498 bdb_db_destroy( BackendDB *be )
499 {
500         int rc;
501         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
502
503         /* close db environment */
504         if( bdb->bi_dbenv ) {
505                 /* force a checkpoint */
506                 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
507                 if( rc != 0 ) {
508 #ifdef NEW_LOGGING
509                         LDAP_LOG( BACK_BDB, ERR, 
510                                 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
511                                 db_strerror(rc), rc, 0 );
512 #else
513                         Debug( LDAP_DEBUG_ANY,
514                                 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
515                                 db_strerror(rc), rc, 0 );
516 #endif
517                 }
518
519                 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
520                 bdb->bi_dbenv = NULL;
521                 if( rc != 0 ) {
522 #ifdef NEW_LOGGING
523                         LDAP_LOG( BACK_BDB, ERR, 
524                                 "bdb_db_destroy: close failed: %s (%d)\n", 
525                                 db_strerror(rc), rc, 0 );
526 #else
527                         Debug( LDAP_DEBUG_ANY,
528                                 "bdb_db_destroy: close failed: %s (%d)\n",
529                                 db_strerror(rc), rc, 0 );
530 #endif
531                         return rc;
532                 }
533         }
534
535         if( bdb->bi_dbenv_home ) ch_free( bdb->bi_dbenv_home );
536
537         ldap_pvt_thread_rdwr_destroy ( &bdb->bi_cache.c_rwlock );
538         ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.lru_mutex );
539         ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_dntree.bei_kids_mutex );
540         ldap_pvt_thread_rdwr_destroy ( &bdb->bi_pslist_rwlock );
541         ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
542         ldap_pvt_thread_mutex_destroy( &bdb->bi_database_mutex );
543 #ifdef SLAP_IDL_CACHE
544         if ( bdb->bi_idl_cache_max_size ) {
545                 ldap_pvt_thread_rdwr_destroy( &bdb->bi_idl_tree_rwlock );
546                 ldap_pvt_thread_mutex_destroy( &bdb->bi_idl_tree_lrulock );
547         }
548 #endif
549
550         ch_free( bdb );
551         be->be_private = NULL;
552
553         return 0;
554 }
555
556 #if     (SLAPD_BDB == SLAPD_MOD_DYNAMIC && !defined(BDB_HIER)) || \
557         (SLAPD_HDB == SLAPD_MOD_DYNAMIC && defined(BDB_HIER))
558 int init_module( int argc, char *argv[] ) {
559         BackendInfo bi;
560
561         memset( &bi, '\0', sizeof(bi) );
562 #ifdef BDB_HIER
563         bi.bi_type = "hdb";
564 #else
565         bi.bi_type = "bdb";
566 #endif
567         bi.bi_init = bdb_initialize;
568
569         backend_add( &bi );
570         return 0;
571 }
572 #endif /* SLAPD_BDB */
573
574 int
575 bdb_initialize(
576         BackendInfo     *bi )
577 {
578         static char *controls[] = {
579                 LDAP_CONTROL_ASSERT,
580                 LDAP_CONTROL_MANAGEDSAIT,
581                 LDAP_CONTROL_NOOP,
582                 LDAP_CONTROL_PAGEDRESULTS,
583 #ifdef LDAP_CONTROL_SUBENTRIES
584                 LDAP_CONTROL_SUBENTRIES,
585 #endif
586                 LDAP_CONTROL_VALUESRETURNFILTER,
587                 NULL
588         };
589
590         /* initialize the underlying database system */
591 #ifdef NEW_LOGGING
592         LDAP_LOG( BACK_BDB, ENTRY, "bdb_db_initialize\n", 0, 0, 0 );
593 #else
594         Debug( LDAP_DEBUG_TRACE, "bdb_initialize: initialize BDB backend\n",
595                 0, 0, 0 );
596 #endif
597
598         bi->bi_flags |=
599                 SLAP_BFLAG_INCREMENT |
600 #ifdef BDB_SUBENTRIES
601                 SLAP_BFLAG_SUBENTRIES |
602 #endif
603 #ifdef BDB_ALIASES
604                 SLAP_BFLAG_ALIASES |
605 #endif
606                 SLAP_BFLAG_REFERRALS;
607
608         bi->bi_controls = controls;
609
610         {       /* version check */
611                 int major, minor, patch;
612                 char *version = db_version( &major, &minor, &patch );
613 #ifdef HAVE_EBCDIC
614                 char v2[1024];
615
616                 /* All our stdio does an ASCII to EBCDIC conversion on
617                  * the output. Strings from the BDB library are already
618                  * in EBCDIC; we have to go back and forth...
619                  */
620                 strcpy( v2, version );
621                 __etoa( v2 );
622                 version = v2;
623 #endif
624
625                 if( major != DB_VERSION_MAJOR ||
626                         minor != DB_VERSION_MINOR ||
627                         patch < DB_VERSION_PATCH )
628                 {
629 #ifdef NEW_LOGGING
630                         LDAP_LOG( BACK_BDB, ERR, 
631                                 "bdb_initialize: BDB library version mismatch:"
632                                 " expected " DB_VERSION_STRING ","
633                                 " got %s\n", version, 0, 0 );
634 #else
635                         Debug( LDAP_DEBUG_ANY,
636                                 "bdb_initialize: BDB library version mismatch:"
637                                 " expected " DB_VERSION_STRING ","
638                                 " got %s\n", version, 0, 0 );
639 #endif
640                 }
641
642 #ifdef NEW_LOGGING
643                 LDAP_LOG( BACK_BDB, DETAIL1, 
644                         "bdb_db_initialize: %s\n", version, 0, 0 );
645 #else
646                 Debug( LDAP_DEBUG_ANY, "bdb_initialize: %s\n",
647                         version, 0, 0 );
648 #endif
649         }
650
651         db_env_set_func_free( ber_memfree );
652         db_env_set_func_malloc( (db_malloc *)ber_memalloc );
653         db_env_set_func_realloc( (db_realloc *)ber_memrealloc );
654 #ifndef NO_THREAD
655         /* This is a no-op on a NO_THREAD build. Leave the default
656          * alone so that BDB will sleep on interprocess conflicts.
657          */
658         db_env_set_func_yield( ldap_pvt_thread_yield );
659 #endif
660
661         {
662                 static char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
663
664                 bdb_uuid.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ));
665                 bdb_uuid.bv_val = uuidbuf;
666         }
667
668         bi->bi_open = 0;
669         bi->bi_close = 0;
670         bi->bi_config = 0;
671         bi->bi_destroy = 0;
672
673         bi->bi_db_init = bdb_db_init;
674         bi->bi_db_config = bdb_db_config;
675         bi->bi_db_open = bdb_db_open;
676         bi->bi_db_close = bdb_db_close;
677         bi->bi_db_destroy = bdb_db_destroy;
678
679         bi->bi_op_add = bdb_add;
680         bi->bi_op_bind = bdb_bind;
681         bi->bi_op_compare = bdb_compare;
682         bi->bi_op_delete = bdb_delete;
683         bi->bi_op_modify = bdb_modify;
684         bi->bi_op_modrdn = bdb_modrdn;
685         bi->bi_op_search = bdb_search;
686
687         bi->bi_op_unbind = 0;
688
689         bi->bi_op_abandon = bdb_abandon;
690         bi->bi_op_cancel = bdb_cancel;
691
692         bi->bi_extended = bdb_extended;
693
694         bi->bi_chk_referrals = bdb_referrals;
695         bi->bi_operational = bdb_operational;
696         bi->bi_has_subordinates = bdb_hasSubordinates;
697         bi->bi_entry_release_rw = bdb_entry_release;
698         bi->bi_entry_get_rw = bdb_entry_get;
699
700         /*
701          * hooks for slap tools
702          */
703         bi->bi_tool_entry_open = bdb_tool_entry_open;
704         bi->bi_tool_entry_close = bdb_tool_entry_close;
705         bi->bi_tool_entry_first = bdb_tool_entry_next;
706         bi->bi_tool_entry_next = bdb_tool_entry_next;
707         bi->bi_tool_entry_get = bdb_tool_entry_get;
708         bi->bi_tool_entry_put = bdb_tool_entry_put;
709         bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
710         bi->bi_tool_sync = 0;
711         bi->bi_tool_dn2id_get = bdb_tool_dn2id_get;
712         bi->bi_tool_id2entry_get = bdb_tool_id2entry_get;
713         bi->bi_tool_entry_modify = bdb_tool_entry_modify;
714
715         bi->bi_connection_init = 0;
716         bi->bi_connection_destroy = 0;
717
718         return 0;
719 }