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