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