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