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