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