]> 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-2006 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 #include <ac/errno.h>
24 #include <sys/stat.h>
25 #include "back-bdb.h"
26 #include <lutil.h>
27 #include <ldap_rq.h>
28 #include "alock.h"
29
30 static const struct bdbi_database {
31         char *file;
32         char *name;
33         int type;
34         int flags;
35 } bdbi_databases[] = {
36         { "id2entry" BDB_SUFFIX, "id2entry", DB_BTREE, 0 },
37         { "dn2id" BDB_SUFFIX, "dn2id", DB_BTREE, 0 },
38         { NULL, NULL, 0, 0 }
39 };
40
41 typedef void * db_malloc(size_t);
42 typedef void * db_realloc(void *, size_t);
43
44 static int
45 bdb_db_init( BackendDB *be )
46 {
47         struct bdb_info *bdb;
48
49         Debug( LDAP_DEBUG_TRACE,
50                 LDAP_XSTRING(bdb_db_init) ": Initializing " BDB_UCTYPE " database\n",
51                 0, 0, 0 );
52
53         /* allocate backend-database-specific stuff */
54         bdb = (struct bdb_info *) ch_calloc( 1, sizeof(struct bdb_info) );
55
56         /* DBEnv parameters */
57         bdb->bi_dbenv_home = ch_strdup( SLAPD_DEFAULT_DB_DIR );
58         bdb->bi_dbenv_xflags = 0;
59         bdb->bi_dbenv_mode = SLAPD_DEFAULT_DB_MODE;
60
61         bdb->bi_cache.c_maxsize = DEFAULT_CACHE_SIZE;
62         bdb->bi_cache.c_minfree = 1;
63
64         bdb->bi_lock_detect = DB_LOCK_DEFAULT;
65         bdb->bi_search_stack_depth = DEFAULT_SEARCH_STACK_DEPTH;
66         bdb->bi_search_stack = NULL;
67
68         ldap_pvt_thread_mutex_init( &bdb->bi_database_mutex );
69         ldap_pvt_thread_mutex_init( &bdb->bi_lastid_mutex );
70 #ifdef BDB_HIER
71         ldap_pvt_thread_mutex_init( &bdb->bi_modrdns_mutex );
72 #endif
73         ldap_pvt_thread_mutex_init( &bdb->bi_cache.lru_head_mutex );
74         ldap_pvt_thread_mutex_init( &bdb->bi_cache.lru_tail_mutex );
75         ldap_pvt_thread_mutex_init( &bdb->bi_cache.c_dntree.bei_kids_mutex );
76         ldap_pvt_thread_rdwr_init ( &bdb->bi_cache.c_rwlock );
77         ldap_pvt_thread_rdwr_init( &bdb->bi_idl_tree_rwlock );
78         ldap_pvt_thread_mutex_init( &bdb->bi_idl_tree_lrulock );
79
80         be->be_private = bdb;
81         be->be_cf_ocs = be->bd_info->bi_cf_ocs;
82
83         return 0;
84 }
85
86 static int
87 bdb_db_close( BackendDB *be );
88
89 static int
90 bdb_db_open( BackendDB *be )
91 {
92         int rc, i;
93         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
94         struct stat stat1, stat2;
95         u_int32_t flags;
96         char path[MAXPATHLEN];
97         char *dbhome;
98         int do_recover = 0, do_alock_recover = 0, open_env = 1;
99         int alockt, quick = 0;
100
101         if ( be->be_suffix == NULL ) {
102                 Debug( LDAP_DEBUG_ANY,
103                         "bdb_db_open: need suffix\n",
104                         0, 0, 0 );
105                 return -1;
106         }
107
108         Debug( LDAP_DEBUG_ARGS,
109                 "bdb_db_open: %s\n",
110                 be->be_suffix[0].bv_val, 0, 0 );
111
112 #ifndef BDB_MULTIPLE_SUFFIXES
113         if ( be->be_suffix[1].bv_val ) {
114         Debug( LDAP_DEBUG_ANY,
115                 "bdb_db_open: only one suffix allowed\n", 0, 0, 0 );
116                 return -1;
117         }
118 #endif
119
120         /* Check existence of dbenv_home. Any error means trouble */
121         rc = stat( bdb->bi_dbenv_home, &stat1 );
122         if( rc !=0 ) {
123                 Debug( LDAP_DEBUG_ANY,
124                         "bdb_db_open: Cannot access database directory %s (%d)\n",
125                         bdb->bi_dbenv_home, errno, 0 );
126                         return -1;
127         }
128
129         /* Perform database use arbitration/recovery logic */
130         alockt = (slapMode & SLAP_TOOL_READONLY) ? ALOCK_LOCKED : ALOCK_UNIQUE;
131         if ( slapMode & SLAP_TOOL_QUICK ) {
132                 alockt |= ALOCK_NOSAVE;
133                 quick = 1;
134         }
135
136         rc = alock_open( &bdb->bi_alock_info, 
137                                 "slapd", 
138                                 bdb->bi_dbenv_home, alockt );
139
140         /* alockt is TRUE if the existing environment was created in Quick mode */
141         alockt = (rc & ALOCK_NOSAVE) ? 1 : 0;
142         rc &= ~ALOCK_NOSAVE;
143
144         if( rc == ALOCK_RECOVER ) {
145                 Debug( LDAP_DEBUG_ANY,
146                         "bdb_db_open: unclean shutdown detected;"
147                         " attempting recovery.\n", 
148                         0, 0, 0 );
149                 do_alock_recover = 1;
150                 do_recover = DB_RECOVER;
151         } else if( rc == ALOCK_BUSY ) {
152                 Debug( LDAP_DEBUG_ANY,
153                         "bdb_db_open: database already in use\n", 
154                         0, 0, 0 );
155                 return -1;
156         } else if( rc != ALOCK_CLEAN ) {
157                 Debug( LDAP_DEBUG_ANY,
158                         "bdb_db_open: alock package is unstable\n", 
159                         0, 0, 0 );
160                 return -1;
161         }
162
163         /*
164          * The DB_CONFIG file may have changed. If so, recover the
165          * database so that new settings are put into effect. Also
166          * note the possible absence of DB_CONFIG in the log.
167          */
168         if( stat( bdb->bi_db_config_path, &stat1 ) == 0 ) {
169                 if ( !do_recover ) {
170                         char *ptr = lutil_strcopy(path, bdb->bi_dbenv_home);
171                         *ptr++ = LDAP_DIRSEP[0];
172                         strcpy( ptr, "__db.001" );
173                         if( stat( path, &stat2 ) == 0 ) {
174                                 if( stat2.st_mtime < stat1.st_mtime ) {
175                                         Debug( LDAP_DEBUG_ANY,
176                                                 "bdb_db_open: DB_CONFIG for suffix %s has changed.\n"
177                                                 "Performing database recovery to activate new settings.\n",
178                                                 be->be_suffix[0].bv_val, 0, 0 );
179                                         do_recover = DB_RECOVER;
180                                 }
181                         }
182                 }
183         }
184         else {
185                 Debug( LDAP_DEBUG_ANY,
186                         "bdb_db_open: Warning - No DB_CONFIG file found "
187                         "in directory %s: (%d)\n"
188                         "Expect poor performance for suffix %s.\n",
189                         bdb->bi_dbenv_home, errno, be->be_suffix[0].bv_val );
190         }
191
192         /* Always let slapcat run, regardless of environment state.
193          * This can be used to cause a cache flush after an unclean
194          * shutdown.
195          */
196         if ( do_recover && ( slapMode & SLAP_TOOL_READONLY )) {
197                 Debug( LDAP_DEBUG_ANY,
198                         "bdb_db_open: Recovery skipped in read-only mode. "
199                         "Run manual recovery if errors are encountered.\n",
200                         0, 0, 0 );
201                 do_recover = 0;
202                 quick = alockt;
203         }
204
205         /* An existing environment in Quick mode has nothing to recover. */
206         if ( alockt && do_recover ) {
207                 Debug( LDAP_DEBUG_ANY,
208                         "bdb_db_open: cannot recover, database must be reinitialized.\n", 
209                         0, 0, 0 );
210                 rc = -1;
211                 goto fail;
212         }
213
214         rc = db_env_create( &bdb->bi_dbenv, 0 );
215         if( rc != 0 ) {
216                 Debug( LDAP_DEBUG_ANY,
217                         "bdb_db_open: db_env_create failed: %s (%d)\n",
218                         db_strerror(rc), rc, 0 );
219                 goto fail;
220         }
221
222         /* If existing environment is clean but doesn't support
223          * currently requested modes, remove it.
224          */
225         if ( !do_recover && ( alockt ^ quick )) {
226                 rc = bdb->bi_dbenv->remove( bdb->bi_dbenv, dbhome, DB_FORCE );
227                 if ( rc ) {
228                         Debug( LDAP_DEBUG_ANY,
229                                 "bdb_db_open: dbenv remove failed: %s (%d)\n",
230                                 db_strerror(rc), rc, 0 );
231                         bdb->bi_dbenv = NULL;
232                         goto fail;
233                 }
234                 rc = db_env_create( &bdb->bi_dbenv, 0 );
235                 if( rc != 0 ) {
236                         Debug( LDAP_DEBUG_ANY,
237                                 "bdb_db_open: db_env_create failed: %s (%d)\n",
238                                 db_strerror(rc), rc, 0 );
239                         goto fail;
240                 }
241         }
242
243         bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0].bv_val );
244         bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
245
246         bdb->bi_dbenv->set_lk_detect( bdb->bi_dbenv, bdb->bi_lock_detect );
247
248         /* One long-lived TXN per thread, two TXNs per write op */
249         bdb->bi_dbenv->set_tx_max( bdb->bi_dbenv, connection_pool_max * 3 );
250
251         if( bdb->bi_dbenv_xflags != 0 ) {
252                 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
253                         bdb->bi_dbenv_xflags, 1);
254                 if( rc != 0 ) {
255                         Debug( LDAP_DEBUG_ANY,
256                                 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
257                                 db_strerror(rc), rc, 0 );
258                         goto fail;
259                 }
260         }
261
262 #define BDB_TXN_FLAGS   (DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN)
263
264 #ifdef HAVE_EBCDIC
265         strcpy( path, bdb->bi_dbenv_home );
266         __atoe( path );
267         dbhome = path;
268 #else
269         dbhome = bdb->bi_dbenv_home;
270 #endif
271
272         Debug( LDAP_DEBUG_TRACE,
273                 "bdb_db_open: dbenv_open(%s)\n",
274                 bdb->bi_dbenv_home, 0, 0);
275
276         flags = DB_INIT_MPOOL | DB_CREATE | DB_THREAD;
277
278         if ( !quick )
279                 flags |= BDB_TXN_FLAGS;
280
281         /* If a key was set, use shared memory for the BDB environment */
282         if ( bdb->bi_shm_key ) {
283                 bdb->bi_dbenv->set_shm_key( bdb->bi_dbenv, bdb->bi_shm_key );
284                 flags |= DB_SYSTEM_MEM;
285         }
286         rc = bdb->bi_dbenv->open( bdb->bi_dbenv, dbhome,
287                         flags | do_recover, bdb->bi_dbenv_mode );
288
289         if ( rc ) {
290                 Debug( LDAP_DEBUG_ANY,
291                         "bdb_db_open: Database cannot be %s, err %d. "
292                         "Restore from backup!\n",
293                                 do_recover ? "recovered" : "opened", rc, 0);
294                 goto fail;
295         }
296
297         if ( do_alock_recover && alock_recover (&bdb->bi_alock_info) != 0 ) {
298                 Debug( LDAP_DEBUG_ANY,
299                         "bdb_db_open: alock_recover failed\n",
300                         0, 0, 0 );
301                 rc = -1;
302                 goto fail;
303         }
304
305 #ifdef SLAP_ZONE_ALLOC
306         if ( bdb->bi_cache.c_maxsize ) {
307                 bdb->bi_cache.c_zctx = slap_zn_mem_create(
308                                                                 SLAP_ZONE_INITSIZE,
309                                                                 SLAP_ZONE_MAXSIZE,
310                                                                 SLAP_ZONE_DELTA,
311                                                                 SLAP_ZONE_SIZE);
312         }
313 #endif
314
315         if ( bdb->bi_idl_cache_max_size ) {
316                 bdb->bi_idl_tree = NULL;
317                 bdb->bi_idl_cache_size = 0;
318         }
319
320         flags = DB_THREAD | bdb->bi_db_opflags;
321
322 #ifdef DB_AUTO_COMMIT
323         if ( !quick )
324                 flags |= DB_AUTO_COMMIT;
325 #endif
326
327         bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
328                 BDB_INDICES * sizeof(struct bdb_db_info *) );
329
330         /* open (and create) main database */
331         for( i = 0; bdbi_databases[i].name; i++ ) {
332                 struct bdb_db_info *db;
333
334                 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
335
336                 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
337                 if( rc != 0 ) {
338                         Debug( LDAP_DEBUG_ANY,
339                                 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
340                                 bdb->bi_dbenv_home, db_strerror(rc), rc );
341                         goto fail;
342                 }
343
344                 if( i == BDB_ID2ENTRY ) {
345                         if ( slapMode & SLAP_TOOL_MODE )
346                                 db->bdi_db->mpf->set_priority( db->bdi_db->mpf,
347                                         DB_PRIORITY_VERY_LOW );
348
349                         rc = db->bdi_db->set_pagesize( db->bdi_db,
350                                 BDB_ID2ENTRY_PAGESIZE );
351                         if ( slapMode & SLAP_TOOL_READMAIN ) {
352                                 flags |= DB_RDONLY;
353                         } else {
354                                 flags |= DB_CREATE;
355                         }
356                 } else {
357                         rc = db->bdi_db->set_flags( db->bdi_db, 
358                                 DB_DUP | DB_DUPSORT );
359 #ifndef BDB_HIER
360                         if ( slapMode & SLAP_TOOL_READONLY ) {
361                                 flags |= DB_RDONLY;
362                         } else {
363                                 flags |= DB_CREATE;
364                         }
365 #else
366                         if ( slapMode & (SLAP_TOOL_READONLY|SLAP_TOOL_READMAIN) ) {
367                                 flags |= DB_RDONLY;
368                         } else {
369                                 flags |= DB_CREATE;
370                         }
371 #endif
372                         rc = db->bdi_db->set_pagesize( db->bdi_db,
373                                 BDB_PAGESIZE );
374                 }
375
376 #ifdef HAVE_EBCDIC
377                 strcpy( path, bdbi_databases[i].file );
378                 __atoe( path );
379                 rc = DB_OPEN( db->bdi_db,
380                         path,
381                 /*      bdbi_databases[i].name, */ NULL,
382                         bdbi_databases[i].type,
383                         bdbi_databases[i].flags | flags,
384                         bdb->bi_dbenv_mode );
385 #else
386                 rc = DB_OPEN( db->bdi_db,
387                         bdbi_databases[i].file,
388                 /*      bdbi_databases[i].name, */ NULL,
389                         bdbi_databases[i].type,
390                         bdbi_databases[i].flags | flags,
391                         bdb->bi_dbenv_mode );
392 #endif
393
394                 if ( rc != 0 ) {
395                         char    buf[SLAP_TEXT_BUFLEN];
396
397                         snprintf( buf, sizeof(buf), "%s/%s", 
398                                 bdb->bi_dbenv_home, bdbi_databases[i].file );
399                         Debug( LDAP_DEBUG_ANY,
400                                 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
401                                 buf, db_strerror(rc), rc );
402                         db->bdi_db->close( db->bdi_db, 0 );
403                         goto fail;
404                 }
405
406                 flags &= ~(DB_CREATE | DB_RDONLY);
407                 db->bdi_name = bdbi_databases[i].name;
408                 bdb->bi_databases[i] = db;
409         }
410
411         bdb->bi_databases[i] = NULL;
412         bdb->bi_ndatabases = i;
413
414         /* get nextid */
415         rc = bdb_last_id( be, NULL );
416         if( rc != 0 ) {
417                 Debug( LDAP_DEBUG_ANY,
418                         "bdb_db_open: last_id(%s) failed: %s (%d)\n",
419                         bdb->bi_dbenv_home, db_strerror(rc), rc );
420                 goto fail;
421         }
422
423         if ( !quick ) {
424                 XLOCK_ID(bdb->bi_dbenv, &bdb->bi_cache.c_locker);
425         }
426
427         bdb->bi_flags |= BDB_IS_OPEN;
428
429         return 0;
430
431 fail:
432         bdb_db_close( be );
433         return rc;
434 }
435
436 static int
437 bdb_db_close( BackendDB *be )
438 {
439         int rc;
440         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
441         struct bdb_db_info *db;
442         bdb_idl_cache_entry_t *entry, *next_entry;
443
444         bdb->bi_flags &= ~BDB_IS_OPEN;
445
446         ber_bvarray_free( bdb->bi_db_config );
447         bdb->bi_db_config = NULL;
448
449         while( bdb->bi_databases && bdb->bi_ndatabases-- ) {
450                 db = bdb->bi_databases[bdb->bi_ndatabases];
451                 rc = db->bdi_db->close( db->bdi_db, 0 );
452                 /* Lower numbered names are not strdup'd */
453                 if( bdb->bi_ndatabases >= BDB_NDB )
454                         free( db->bdi_name );
455                 free( db );
456         }
457         free( bdb->bi_databases );
458         bdb->bi_databases = NULL;
459
460         bdb_cache_release_all (&bdb->bi_cache);
461
462         if ( bdb->bi_idl_cache_max_size ) {
463                 avl_free( bdb->bi_idl_tree, NULL );
464                 bdb->bi_idl_tree = NULL;
465                 entry = bdb->bi_idl_lru_head;
466                 while ( entry != NULL ) {
467                         next_entry = entry->idl_lru_next;
468                         if ( entry->idl )
469                                 free( entry->idl );
470                         free( entry->kstr.bv_val );
471                         free( entry );
472                         entry = next_entry;
473                 }
474                 bdb->bi_idl_lru_head = bdb->bi_idl_lru_tail = NULL;
475         }
476
477         /* close db environment */
478         if( bdb->bi_dbenv ) {
479                 /* Free cache locker if we enabled locking */
480                 if ( !( slapMode & SLAP_TOOL_QUICK )) {
481                         XLOCK_ID_FREE(bdb->bi_dbenv, bdb->bi_cache.c_locker);
482                         bdb->bi_cache.c_locker = 0;
483                 }
484
485                 /* force a checkpoint, but not if we were ReadOnly,
486                  * and not in Quick mode since there are no transactions there.
487                  */
488                 if ( !( slapMode & ( SLAP_TOOL_QUICK|SLAP_TOOL_READONLY ))) {
489                         rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
490                         if( rc != 0 ) {
491                                 Debug( LDAP_DEBUG_ANY,
492                                         "bdb_db_close: txn_checkpoint failed: %s (%d)\n",
493                                         db_strerror(rc), rc, 0 );
494                         }
495                 }
496
497                 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
498                 bdb->bi_dbenv = NULL;
499                 if( rc != 0 ) {
500                         Debug( LDAP_DEBUG_ANY,
501                                 "bdb_db_close: close failed: %s (%d)\n",
502                                 db_strerror(rc), rc, 0 );
503                         return rc;
504                 }
505         }
506
507         rc = alock_close( &bdb->bi_alock_info );
508         if( rc != 0 ) {
509                 Debug( LDAP_DEBUG_ANY,
510                         "bdb_db_close: alock_close failed\n", 0, 0, 0 );
511                 return -1;
512         }
513
514         return 0;
515 }
516
517 static int
518 bdb_db_destroy( BackendDB *be )
519 {
520         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
521
522         if( bdb->bi_dbenv_home ) ch_free( bdb->bi_dbenv_home );
523         if( bdb->bi_db_config_path ) ch_free( bdb->bi_db_config_path );
524
525         bdb_attr_index_destroy( bdb );
526
527         ldap_pvt_thread_rdwr_destroy ( &bdb->bi_cache.c_rwlock );
528         ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.lru_head_mutex );
529         ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.lru_tail_mutex );
530         ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_dntree.bei_kids_mutex );
531 #ifdef BDB_HIER
532         ldap_pvt_thread_mutex_destroy( &bdb->bi_modrdns_mutex );
533 #endif
534         ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
535         ldap_pvt_thread_mutex_destroy( &bdb->bi_database_mutex );
536         ldap_pvt_thread_rdwr_destroy( &bdb->bi_idl_tree_rwlock );
537         ldap_pvt_thread_mutex_destroy( &bdb->bi_idl_tree_lrulock );
538
539         ch_free( bdb );
540         be->be_private = NULL;
541
542         return 0;
543 }
544
545 int
546 bdb_back_initialize(
547         BackendInfo     *bi )
548 {
549         int rc;
550
551         static char *controls[] = {
552                 LDAP_CONTROL_ASSERT,
553                 LDAP_CONTROL_MANAGEDSAIT,
554                 LDAP_CONTROL_NOOP,
555                 LDAP_CONTROL_PAGEDRESULTS,
556                 LDAP_CONTROL_SUBENTRIES,
557                 LDAP_CONTROL_X_PERMISSIVE_MODIFY,
558                 NULL
559         };
560
561         /* initialize the underlying database system */
562         Debug( LDAP_DEBUG_TRACE,
563                 LDAP_XSTRING(bdb_back_initialize) ": initialize " 
564                 BDB_UCTYPE " backend\n", 0, 0, 0 );
565
566         bi->bi_flags |=
567                 SLAP_BFLAG_INCREMENT |
568                 SLAP_BFLAG_SUBENTRIES |
569                 SLAP_BFLAG_ALIASES |
570                 SLAP_BFLAG_REFERRALS;
571
572         bi->bi_controls = controls;
573
574         {       /* version check */
575                 int major, minor, patch, ver;
576                 char *version = db_version( &major, &minor, &patch );
577 #ifdef HAVE_EBCDIC
578                 char v2[1024];
579
580                 /* All our stdio does an ASCII to EBCDIC conversion on
581                  * the output. Strings from the BDB library are already
582                  * in EBCDIC; we have to go back and forth...
583                  */
584                 strcpy( v2, version );
585                 __etoa( v2 );
586                 version = v2;
587 #endif
588
589                 ver = (major << 24) | (minor << 16) | patch;
590                 if( ver != DB_VERSION_FULL ) {
591                         /* fail if a versions don't match */
592                         Debug( LDAP_DEBUG_ANY,
593                                 LDAP_XSTRING(bdb_back_initialize) ": "
594                                 "BDB library version mismatch:"
595                                 " expected " DB_VERSION_STRING ","
596                                 " got %s\n", version, 0, 0 );
597                         return -1;
598                 }
599
600                 Debug( LDAP_DEBUG_TRACE, LDAP_XSTRING(bdb_back_initialize)
601                         ": %s\n", version, 0, 0 );
602         }
603
604         db_env_set_func_free( ber_memfree );
605         db_env_set_func_malloc( (db_malloc *)ber_memalloc );
606         db_env_set_func_realloc( (db_realloc *)ber_memrealloc );
607 #ifndef NO_THREAD
608         /* This is a no-op on a NO_THREAD build. Leave the default
609          * alone so that BDB will sleep on interprocess conflicts.
610          */
611         db_env_set_func_yield( ldap_pvt_thread_yield );
612 #endif
613
614         bi->bi_open = 0;
615         bi->bi_close = 0;
616         bi->bi_config = 0;
617         bi->bi_destroy = 0;
618
619         bi->bi_db_init = bdb_db_init;
620         bi->bi_db_config = config_generic_wrapper;
621         bi->bi_db_open = bdb_db_open;
622         bi->bi_db_close = bdb_db_close;
623         bi->bi_db_destroy = bdb_db_destroy;
624
625         bi->bi_op_add = bdb_add;
626         bi->bi_op_bind = bdb_bind;
627         bi->bi_op_compare = bdb_compare;
628         bi->bi_op_delete = bdb_delete;
629         bi->bi_op_modify = bdb_modify;
630         bi->bi_op_modrdn = bdb_modrdn;
631         bi->bi_op_search = bdb_search;
632
633         bi->bi_op_unbind = 0;
634
635         bi->bi_extended = bdb_extended;
636
637         bi->bi_chk_referrals = bdb_referrals;
638         bi->bi_operational = bdb_operational;
639         bi->bi_has_subordinates = bdb_hasSubordinates;
640         bi->bi_entry_release_rw = bdb_entry_release;
641         bi->bi_entry_get_rw = bdb_entry_get;
642
643         /*
644          * hooks for slap tools
645          */
646         bi->bi_tool_entry_open = bdb_tool_entry_open;
647         bi->bi_tool_entry_close = bdb_tool_entry_close;
648         bi->bi_tool_entry_first = bdb_tool_entry_next;
649         bi->bi_tool_entry_next = bdb_tool_entry_next;
650         bi->bi_tool_entry_get = bdb_tool_entry_get;
651         bi->bi_tool_entry_put = bdb_tool_entry_put;
652         bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
653         bi->bi_tool_sync = 0;
654         bi->bi_tool_dn2id_get = bdb_tool_dn2id_get;
655         bi->bi_tool_id2entry_get = bdb_tool_id2entry_get;
656         bi->bi_tool_entry_modify = bdb_tool_entry_modify;
657
658         bi->bi_connection_init = 0;
659         bi->bi_connection_destroy = 0;
660
661         rc = bdb_back_init_cf( bi );
662
663         return rc;
664 }
665
666 #if     (SLAPD_BDB == SLAPD_MOD_DYNAMIC && !defined(BDB_HIER)) || \
667         (SLAPD_HDB == SLAPD_MOD_DYNAMIC && defined(BDB_HIER))
668
669 /* conditionally define the init_module() function */
670 #ifdef BDB_HIER
671 SLAP_BACKEND_INIT_MODULE( hdb )
672 #else /* !BDB_HIER */
673 SLAP_BACKEND_INIT_MODULE( bdb )
674 #endif /* !BDB_HIER */
675
676 #endif /* SLAPD_[BH]DB == SLAPD_MOD_DYNAMIC */
677