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