]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/init.c
- setup framework for monitoring of back-bdb/back-hdb stuff in their
[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;
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 #ifdef HAVE_EBCDIC
223         strcpy( path, bdb->bi_dbenv_home );
224         __atoe( path );
225         dbhome = path;
226 #else
227         dbhome = bdb->bi_dbenv_home;
228 #endif
229
230         /* If existing environment is clean but doesn't support
231          * currently requested modes, remove it.
232          */
233         if ( !do_recover && ( alockt ^ quick )) {
234                 rc = bdb->bi_dbenv->remove( bdb->bi_dbenv, dbhome, DB_FORCE );
235                 if ( rc ) {
236                         Debug( LDAP_DEBUG_ANY,
237                                 "bdb_db_open: dbenv remove failed: %s (%d)\n",
238                                 db_strerror(rc), rc, 0 );
239                         bdb->bi_dbenv = NULL;
240                         goto fail;
241                 }
242                 rc = db_env_create( &bdb->bi_dbenv, 0 );
243                 if( rc != 0 ) {
244                         Debug( LDAP_DEBUG_ANY,
245                                 "bdb_db_open: db_env_create failed: %s (%d)\n",
246                                 db_strerror(rc), rc, 0 );
247                         goto fail;
248                 }
249         }
250
251         bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0].bv_val );
252         bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
253
254         bdb->bi_dbenv->set_lk_detect( bdb->bi_dbenv, bdb->bi_lock_detect );
255
256         /* One long-lived TXN per thread, two TXNs per write op */
257         bdb->bi_dbenv->set_tx_max( bdb->bi_dbenv, connection_pool_max * 3 );
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                         goto fail;
267                 }
268         }
269
270 #define BDB_TXN_FLAGS   (DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN)
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, SLAP_ZONE_MAXSIZE,
309                         SLAP_ZONE_DELTA, SLAP_ZONE_SIZE);
310         }
311 #endif
312
313         if ( bdb->bi_idl_cache_max_size ) {
314                 bdb->bi_idl_tree = NULL;
315                 bdb->bi_idl_cache_size = 0;
316         }
317
318         flags = DB_THREAD | bdb->bi_db_opflags;
319
320 #ifdef DB_AUTO_COMMIT
321         if ( !quick )
322                 flags |= DB_AUTO_COMMIT;
323 #endif
324
325         bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
326                 BDB_INDICES * sizeof(struct bdb_db_info *) );
327
328         /* open (and create) main database */
329         for( i = 0; bdbi_databases[i].name; i++ ) {
330                 struct bdb_db_info *db;
331
332                 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
333
334                 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
335                 if( rc != 0 ) {
336                         Debug( LDAP_DEBUG_ANY,
337                                 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
338                                 bdb->bi_dbenv_home, db_strerror(rc), rc );
339                         goto fail;
340                 }
341
342                 if( i == BDB_ID2ENTRY ) {
343                         if ( slapMode & SLAP_TOOL_MODE )
344                                 db->bdi_db->mpf->set_priority( db->bdi_db->mpf,
345                                         DB_PRIORITY_VERY_LOW );
346
347                         rc = db->bdi_db->set_pagesize( db->bdi_db,
348                                 BDB_ID2ENTRY_PAGESIZE );
349                         if ( slapMode & SLAP_TOOL_READMAIN ) {
350                                 flags |= DB_RDONLY;
351                         } else {
352                                 flags |= DB_CREATE;
353                         }
354                 } else {
355                         rc = db->bdi_db->set_flags( db->bdi_db, 
356                                 DB_DUP | DB_DUPSORT );
357 #ifndef BDB_HIER
358                         if ( slapMode & SLAP_TOOL_READONLY ) {
359                                 flags |= DB_RDONLY;
360                         } else {
361                                 flags |= DB_CREATE;
362                         }
363 #else
364                         if ( slapMode & (SLAP_TOOL_READONLY|SLAP_TOOL_READMAIN) ) {
365                                 flags |= DB_RDONLY;
366                         } else {
367                                 flags |= DB_CREATE;
368                         }
369 #endif
370                         rc = db->bdi_db->set_pagesize( db->bdi_db,
371                                 BDB_PAGESIZE );
372                 }
373
374 #ifdef HAVE_EBCDIC
375                 strcpy( path, bdbi_databases[i].file );
376                 __atoe( path );
377                 rc = DB_OPEN( db->bdi_db,
378                         path,
379                 /*      bdbi_databases[i].name, */ NULL,
380                         bdbi_databases[i].type,
381                         bdbi_databases[i].flags | flags,
382                         bdb->bi_dbenv_mode );
383 #else
384                 rc = DB_OPEN( db->bdi_db,
385                         bdbi_databases[i].file,
386                 /*      bdbi_databases[i].name, */ NULL,
387                         bdbi_databases[i].type,
388                         bdbi_databases[i].flags | flags,
389                         bdb->bi_dbenv_mode );
390 #endif
391
392                 if ( rc != 0 ) {
393                         char    buf[SLAP_TEXT_BUFLEN];
394
395                         snprintf( buf, sizeof(buf), "%s/%s", 
396                                 bdb->bi_dbenv_home, bdbi_databases[i].file );
397                         Debug( LDAP_DEBUG_ANY,
398                                 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
399                                 buf, db_strerror(rc), rc );
400                         db->bdi_db->close( db->bdi_db, 0 );
401                         goto fail;
402                 }
403
404                 flags &= ~(DB_CREATE | DB_RDONLY);
405                 db->bdi_name = bdbi_databases[i].name;
406                 bdb->bi_databases[i] = db;
407         }
408
409         bdb->bi_databases[i] = NULL;
410         bdb->bi_ndatabases = i;
411
412         /* get nextid */
413         rc = bdb_last_id( be, NULL );
414         if( rc != 0 ) {
415                 Debug( LDAP_DEBUG_ANY,
416                         "bdb_db_open: last_id(%s) failed: %s (%d)\n",
417                         bdb->bi_dbenv_home, db_strerror(rc), rc );
418                 goto fail;
419         }
420
421         if ( !quick ) {
422                 XLOCK_ID(bdb->bi_dbenv, &bdb->bi_cache.c_locker);
423         }
424
425         /* monitor setup */
426         rc = bdb_monitor_open( be );
427         if ( rc != 0 ) {
428                 goto fail;
429         }
430
431         bdb->bi_flags |= BDB_IS_OPEN;
432
433         entry_prealloc( bdb->bi_cache.c_maxsize );
434         attr_prealloc( bdb->bi_cache.c_maxsize * 20 );
435         return 0;
436
437 fail:
438         bdb_db_close( be );
439         return rc;
440 }
441
442 static int
443 bdb_db_close( BackendDB *be )
444 {
445         int rc;
446         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
447         struct bdb_db_info *db;
448         bdb_idl_cache_entry_t *entry, *next_entry;
449
450         bdb->bi_flags &= ~BDB_IS_OPEN;
451
452         ber_bvarray_free( bdb->bi_db_config );
453         bdb->bi_db_config = NULL;
454
455         while( bdb->bi_databases && bdb->bi_ndatabases-- ) {
456                 db = bdb->bi_databases[bdb->bi_ndatabases];
457                 rc = db->bdi_db->close( db->bdi_db, 0 );
458                 /* Lower numbered names are not strdup'd */
459                 if( bdb->bi_ndatabases >= BDB_NDB )
460                         free( db->bdi_name );
461                 free( db );
462         }
463         free( bdb->bi_databases );
464         bdb->bi_databases = NULL;
465
466         bdb_cache_release_all (&bdb->bi_cache);
467
468         if ( bdb->bi_idl_cache_max_size ) {
469                 avl_free( bdb->bi_idl_tree, NULL );
470                 bdb->bi_idl_tree = NULL;
471                 entry = bdb->bi_idl_lru_head;
472                 while ( entry != NULL ) {
473                         next_entry = entry->idl_lru_next;
474                         if ( entry->idl )
475                                 free( entry->idl );
476                         free( entry->kstr.bv_val );
477                         free( entry );
478                         entry = next_entry;
479                 }
480                 bdb->bi_idl_lru_head = bdb->bi_idl_lru_tail = NULL;
481         }
482
483         /* close db environment */
484         if( bdb->bi_dbenv ) {
485                 /* Free cache locker if we enabled locking */
486                 if ( !( slapMode & SLAP_TOOL_QUICK )) {
487                         XLOCK_ID_FREE(bdb->bi_dbenv, bdb->bi_cache.c_locker);
488                         bdb->bi_cache.c_locker = 0;
489                 }
490
491                 /* force a checkpoint, but not if we were ReadOnly,
492                  * and not in Quick mode since there are no transactions there.
493                  */
494                 if ( !( slapMode & ( SLAP_TOOL_QUICK|SLAP_TOOL_READONLY ))) {
495                         rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
496                         if( rc != 0 ) {
497                                 Debug( LDAP_DEBUG_ANY,
498                                         "bdb_db_close: txn_checkpoint failed: %s (%d)\n",
499                                         db_strerror(rc), rc, 0 );
500                         }
501                 }
502
503                 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
504                 bdb->bi_dbenv = NULL;
505                 if( rc != 0 ) {
506                         Debug( LDAP_DEBUG_ANY,
507                                 "bdb_db_close: close failed: %s (%d)\n",
508                                 db_strerror(rc), rc, 0 );
509                         return rc;
510                 }
511         }
512
513         rc = alock_close( &bdb->bi_alock_info );
514         if( rc != 0 ) {
515                 Debug( LDAP_DEBUG_ANY,
516                         "bdb_db_close: alock_close failed\n", 0, 0, 0 );
517                 return -1;
518         }
519
520         return 0;
521 }
522
523 static int
524 bdb_db_destroy( BackendDB *be )
525 {
526         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
527
528         if( bdb->bi_dbenv_home ) ch_free( bdb->bi_dbenv_home );
529         if( bdb->bi_db_config_path ) ch_free( bdb->bi_db_config_path );
530
531         bdb_attr_index_destroy( bdb );
532
533         ldap_pvt_thread_rdwr_destroy ( &bdb->bi_cache.c_rwlock );
534         ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.lru_head_mutex );
535         ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.lru_tail_mutex );
536         ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_dntree.bei_kids_mutex );
537 #ifdef BDB_HIER
538         ldap_pvt_thread_mutex_destroy( &bdb->bi_modrdns_mutex );
539 #endif
540         ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
541         ldap_pvt_thread_mutex_destroy( &bdb->bi_database_mutex );
542         ldap_pvt_thread_rdwr_destroy( &bdb->bi_idl_tree_rwlock );
543         ldap_pvt_thread_mutex_destroy( &bdb->bi_idl_tree_lrulock );
544
545         ch_free( bdb );
546         be->be_private = NULL;
547
548         return 0;
549 }
550
551 int
552 bdb_back_initialize(
553         BackendInfo     *bi )
554 {
555         int rc;
556
557         static char *controls[] = {
558                 LDAP_CONTROL_ASSERT,
559                 LDAP_CONTROL_MANAGEDSAIT,
560                 LDAP_CONTROL_NOOP,
561                 LDAP_CONTROL_PAGEDRESULTS,
562                 LDAP_CONTROL_PRE_READ,
563                 LDAP_CONTROL_POST_READ,
564                 LDAP_CONTROL_SUBENTRIES,
565                 LDAP_CONTROL_X_PERMISSIVE_MODIFY,
566 #ifdef LDAP_X_TXN
567                 LDAP_CONTROL_X_TXN_SPEC,
568 #endif
569                 NULL
570         };
571
572         /* initialize the underlying database system */
573         Debug( LDAP_DEBUG_TRACE,
574                 LDAP_XSTRING(bdb_back_initialize) ": initialize " 
575                 BDB_UCTYPE " backend\n", 0, 0, 0 );
576
577         bi->bi_flags |=
578                 SLAP_BFLAG_INCREMENT |
579                 SLAP_BFLAG_SUBENTRIES |
580                 SLAP_BFLAG_ALIASES |
581                 SLAP_BFLAG_REFERRALS;
582
583         bi->bi_controls = controls;
584
585         {       /* version check */
586                 int major, minor, patch, ver;
587                 char *version = db_version( &major, &minor, &patch );
588 #ifdef HAVE_EBCDIC
589                 char v2[1024];
590
591                 /* All our stdio does an ASCII to EBCDIC conversion on
592                  * the output. Strings from the BDB library are already
593                  * in EBCDIC; we have to go back and forth...
594                  */
595                 strcpy( v2, version );
596                 __etoa( v2 );
597                 version = v2;
598 #endif
599
600                 ver = (major << 24) | (minor << 16) | patch;
601                 if( ver != DB_VERSION_FULL ) {
602                         /* fail if a versions don't match */
603                         Debug( LDAP_DEBUG_ANY,
604                                 LDAP_XSTRING(bdb_back_initialize) ": "
605                                 "BDB library version mismatch:"
606                                 " expected " DB_VERSION_STRING ","
607                                 " got %s\n", version, 0, 0 );
608                         return -1;
609                 }
610
611                 Debug( LDAP_DEBUG_TRACE, LDAP_XSTRING(bdb_back_initialize)
612                         ": %s\n", version, 0, 0 );
613         }
614
615         db_env_set_func_free( ber_memfree );
616         db_env_set_func_malloc( (db_malloc *)ber_memalloc );
617         db_env_set_func_realloc( (db_realloc *)ber_memrealloc );
618 #ifndef NO_THREAD
619         /* This is a no-op on a NO_THREAD build. Leave the default
620          * alone so that BDB will sleep on interprocess conflicts.
621          */
622         db_env_set_func_yield( ldap_pvt_thread_yield );
623 #endif
624
625         bi->bi_open = 0;
626         bi->bi_close = 0;
627         bi->bi_config = 0;
628         bi->bi_destroy = 0;
629
630         bi->bi_db_init = bdb_db_init;
631         bi->bi_db_config = config_generic_wrapper;
632         bi->bi_db_open = bdb_db_open;
633         bi->bi_db_close = bdb_db_close;
634         bi->bi_db_destroy = bdb_db_destroy;
635
636         bi->bi_op_add = bdb_add;
637         bi->bi_op_bind = bdb_bind;
638         bi->bi_op_compare = bdb_compare;
639         bi->bi_op_delete = bdb_delete;
640         bi->bi_op_modify = bdb_modify;
641         bi->bi_op_modrdn = bdb_modrdn;
642         bi->bi_op_search = bdb_search;
643
644         bi->bi_op_unbind = 0;
645
646         bi->bi_extended = bdb_extended;
647
648         bi->bi_chk_referrals = bdb_referrals;
649         bi->bi_operational = bdb_operational;
650         bi->bi_has_subordinates = bdb_hasSubordinates;
651         bi->bi_entry_release_rw = bdb_entry_release;
652         bi->bi_entry_get_rw = bdb_entry_get;
653
654         /*
655          * hooks for slap tools
656          */
657         bi->bi_tool_entry_open = bdb_tool_entry_open;
658         bi->bi_tool_entry_close = bdb_tool_entry_close;
659         bi->bi_tool_entry_first = bdb_tool_entry_next;
660         bi->bi_tool_entry_next = bdb_tool_entry_next;
661         bi->bi_tool_entry_get = bdb_tool_entry_get;
662         bi->bi_tool_entry_put = bdb_tool_entry_put;
663         bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
664         bi->bi_tool_sync = 0;
665         bi->bi_tool_dn2id_get = bdb_tool_dn2id_get;
666         bi->bi_tool_id2entry_get = bdb_tool_id2entry_get;
667         bi->bi_tool_entry_modify = bdb_tool_entry_modify;
668
669         bi->bi_connection_init = 0;
670         bi->bi_connection_destroy = 0;
671
672         /*
673          * initialize monitor stuff
674          */
675         rc = bdb_monitor_initialize();
676         if ( rc ) {
677                 return rc;
678         }
679
680         rc = bdb_back_init_cf( bi );
681
682         return rc;
683 }
684
685 #if     (SLAPD_BDB == SLAPD_MOD_DYNAMIC && !defined(BDB_HIER)) || \
686         (SLAPD_HDB == SLAPD_MOD_DYNAMIC && defined(BDB_HIER))
687
688 /* conditionally define the init_module() function */
689 #ifdef BDB_HIER
690 SLAP_BACKEND_INIT_MODULE( hdb )
691 #else /* !BDB_HIER */
692 SLAP_BACKEND_INIT_MODULE( bdb )
693 #endif /* !BDB_HIER */
694
695 #endif /* SLAPD_[BH]DB == SLAPD_MOD_DYNAMIC */
696