]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/init.c
01af4c2c4682e5559ef6c034fa5b99e237400850
[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 #include "config.h"
30
31 static const struct bdbi_database {
32         char *file;
33         char *name;
34         int type;
35         int flags;
36 } bdbi_databases[] = {
37         { "id2entry" BDB_SUFFIX, "id2entry", DB_BTREE, 0 },
38         { "dn2id" BDB_SUFFIX, "dn2id", DB_BTREE, 0 },
39         { NULL, NULL, 0, 0 }
40 };
41
42 typedef void * db_malloc(size_t);
43 typedef void * db_realloc(void *, size_t);
44
45 #define bdb_db_init     BDB_SYMBOL(db_init)
46 #define bdb_db_open BDB_SYMBOL(db_open)
47 #define bdb_db_close BDB_SYMBOL(db_close)
48
49 static int
50 bdb_db_init( BackendDB *be, ConfigReply *cr )
51 {
52         struct bdb_info *bdb;
53         int rc;
54
55         Debug( LDAP_DEBUG_TRACE,
56                 LDAP_XSTRING(bdb_db_init) ": Initializing " BDB_UCTYPE " database\n",
57                 0, 0, 0 );
58
59         /* allocate backend-database-specific stuff */
60         bdb = (struct bdb_info *) ch_calloc( 1, sizeof(struct bdb_info) );
61
62         /* DBEnv parameters */
63         bdb->bi_dbenv_home = ch_strdup( SLAPD_DEFAULT_DB_DIR );
64         bdb->bi_dbenv_xflags = 0;
65         bdb->bi_dbenv_mode = SLAPD_DEFAULT_DB_MODE;
66
67         bdb->bi_cache.c_maxsize = DEFAULT_CACHE_SIZE;
68         bdb->bi_cache.c_minfree = 1;
69
70         bdb->bi_lock_detect = DB_LOCK_DEFAULT;
71         bdb->bi_search_stack_depth = DEFAULT_SEARCH_STACK_DEPTH;
72         bdb->bi_search_stack = NULL;
73
74         ldap_pvt_thread_mutex_init( &bdb->bi_database_mutex );
75         ldap_pvt_thread_mutex_init( &bdb->bi_lastid_mutex );
76 #ifdef BDB_HIER
77         ldap_pvt_thread_mutex_init( &bdb->bi_modrdns_mutex );
78 #endif
79         ldap_pvt_thread_mutex_init( &bdb->bi_cache.c_lru_mutex );
80         ldap_pvt_thread_mutex_init( &bdb->bi_cache.c_count_mutex );
81         ldap_pvt_thread_mutex_init( &bdb->bi_cache.c_eifree_mutex );
82         ldap_pvt_thread_mutex_init( &bdb->bi_cache.c_dntree.bei_kids_mutex );
83         ldap_pvt_thread_rdwr_init ( &bdb->bi_cache.c_rwlock );
84         ldap_pvt_thread_rdwr_init( &bdb->bi_idl_tree_rwlock );
85         ldap_pvt_thread_mutex_init( &bdb->bi_idl_tree_lrulock );
86
87         be->be_private = bdb;
88         be->be_cf_ocs = be->bd_info->bi_cf_ocs;
89
90         rc = bdb_monitor_db_init( be );
91
92         return rc;
93 }
94
95 static int
96 bdb_db_close( BackendDB *be, ConfigReply *cr );
97
98 static int
99 bdb_db_open( BackendDB *be, ConfigReply *cr )
100 {
101         int rc, i;
102         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
103         struct stat stat1, stat2;
104         u_int32_t flags;
105         char path[MAXPATHLEN];
106         char *dbhome;
107         Entry *e = NULL;
108         int do_recover = 0, do_alock_recover = 0;
109         int alockt, quick = 0;
110
111         if ( be->be_suffix == NULL ) {
112                 Debug( LDAP_DEBUG_ANY,
113                         LDAP_XSTRING(bdb_db_open) ": need suffix\n",
114                         1, 0, 0 );
115                 return -1;
116         }
117
118         Debug( LDAP_DEBUG_ARGS,
119                 LDAP_XSTRING(bdb_db_open) ": %s\n",
120                 be->be_suffix[0].bv_val, 0, 0 );
121
122 #ifndef BDB_MULTIPLE_SUFFIXES
123         if ( be->be_suffix[1].bv_val ) {
124                 if (cr) {
125                         snprintf(cr->msg, sizeof(cr->msg), "only one suffix allowed");
126                         Debug( LDAP_DEBUG_ANY,
127                                 LDAP_XSTRING(bdb_db_open) ": %s\n", cr->msg, 0, 0 );
128                 }
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                         LDAP_XSTRING(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                         LDAP_XSTRING(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                         LDAP_XSTRING(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                         LDAP_XSTRING(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                                                 LDAP_XSTRING(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                         LDAP_XSTRING(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                         LDAP_XSTRING(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                         LDAP_XSTRING(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                         LDAP_XSTRING(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 shm_retry:
248                 rc = bdb->bi_dbenv->remove( bdb->bi_dbenv, dbhome, DB_FORCE );
249                 if ( rc ) {
250                         Debug( LDAP_DEBUG_ANY,
251                                 LDAP_XSTRING(bdb_db_open) ": dbenv remove failed: %s (%d)\n",
252                                 db_strerror(rc), rc, 0 );
253                         bdb->bi_dbenv = NULL;
254                         goto fail;
255                 }
256                 rc = db_env_create( &bdb->bi_dbenv, 0 );
257                 if( rc != 0 ) {
258                         Debug( LDAP_DEBUG_ANY,
259                                 LDAP_XSTRING(bdb_db_open) ": db_env_create failed: %s (%d)\n",
260                                 db_strerror(rc), rc, 0 );
261                         goto fail;
262                 }
263         }
264
265         bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0].bv_val );
266         bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
267
268         bdb->bi_dbenv->set_lk_detect( bdb->bi_dbenv, bdb->bi_lock_detect );
269
270         /* One long-lived TXN per thread, two TXNs per write op */
271         bdb->bi_dbenv->set_tx_max( bdb->bi_dbenv, connection_pool_max * 3 );
272
273         if( bdb->bi_dbenv_xflags != 0 ) {
274                 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
275                         bdb->bi_dbenv_xflags, 1);
276                 if( rc != 0 ) {
277                         Debug( LDAP_DEBUG_ANY,
278                                 LDAP_XSTRING(bdb_db_open) ": dbenv_set_flags failed: %s (%d)\n",
279                                 db_strerror(rc), rc, 0 );
280                         goto fail;
281                 }
282         }
283
284 #define BDB_TXN_FLAGS   (DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN)
285
286         Debug( LDAP_DEBUG_TRACE,
287                 LDAP_XSTRING(bdb_db_open) ": dbenv_open(%s)\n",
288                 bdb->bi_dbenv_home, 0, 0);
289
290         flags = DB_INIT_MPOOL | DB_CREATE | DB_THREAD;
291
292         if ( !quick )
293                 flags |= BDB_TXN_FLAGS;
294
295         /* If a key was set, use shared memory for the BDB environment */
296         if ( bdb->bi_shm_key ) {
297                 bdb->bi_dbenv->set_shm_key( bdb->bi_dbenv, bdb->bi_shm_key );
298                 flags |= DB_SYSTEM_MEM;
299         }
300         rc = (bdb->bi_dbenv->open)( bdb->bi_dbenv, dbhome,
301                         flags | do_recover, bdb->bi_dbenv_mode );
302
303         if ( rc ) {
304                 /* Regular open failed, probably a missing shm environment.
305                  * Start over, do a recovery.
306                  */
307                 if ( !do_recover && bdb->bi_shm_key ) {
308                         bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
309                         rc = db_env_create( &bdb->bi_dbenv, 0 );
310                         if( rc == 0 ) {
311                                 Debug( LDAP_DEBUG_ANY, LDAP_XSTRING(bdb_db_open)
312                                         ": Shared memory env open failed, assuming stale env\n",
313                                         0, 0, 0 );
314                                 goto shm_retry;
315                         }
316                 }
317                 Debug( LDAP_DEBUG_ANY,
318                         LDAP_XSTRING(bdb_db_open) ": Database cannot be %s, err %d. "
319                         "Restore from backup!\n",
320                                 do_recover ? "recovered" : "opened", rc, 0);
321                 goto fail;
322         }
323
324         if ( do_alock_recover && alock_recover (&bdb->bi_alock_info) != 0 ) {
325                 Debug( LDAP_DEBUG_ANY,
326                         LDAP_XSTRING(bdb_db_open) ": alock_recover failed\n",
327                         0, 0, 0 );
328                 rc = -1;
329                 goto fail;
330         }
331
332 #ifdef SLAP_ZONE_ALLOC
333         if ( bdb->bi_cache.c_maxsize ) {
334                 bdb->bi_cache.c_zctx = slap_zn_mem_create(
335                         SLAP_ZONE_INITSIZE, SLAP_ZONE_MAXSIZE,
336                         SLAP_ZONE_DELTA, SLAP_ZONE_SIZE);
337         }
338 #endif
339
340         /* Default dncache to 2x entrycache */
341         if ( bdb->bi_cache.c_maxsize && !bdb->bi_cache.c_eimax ) {
342                 bdb->bi_cache.c_eimax = bdb->bi_cache.c_maxsize * 2;
343         }
344
345         if ( bdb->bi_idl_cache_max_size ) {
346                 bdb->bi_idl_tree = NULL;
347                 bdb->bi_idl_cache_size = 0;
348         }
349
350         flags = DB_THREAD | bdb->bi_db_opflags;
351
352 #ifdef DB_AUTO_COMMIT
353         if ( !quick )
354                 flags |= DB_AUTO_COMMIT;
355 #endif
356
357         bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
358                 BDB_INDICES * sizeof(struct bdb_db_info *) );
359
360         /* open (and create) main database */
361         for( i = 0; bdbi_databases[i].name; i++ ) {
362                 struct bdb_db_info *db;
363
364                 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
365
366                 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
367                 if( rc != 0 ) {
368                         Debug( LDAP_DEBUG_ANY,
369                                 LDAP_XSTRING(bdb_db_open) ": db_create(%s) failed: %s (%d)\n",
370                                 bdb->bi_dbenv_home, db_strerror(rc), rc );
371                         goto fail;
372                 }
373
374                 if( i == BDB_ID2ENTRY ) {
375                         if ( slapMode & SLAP_TOOL_MODE )
376                                 db->bdi_db->mpf->set_priority( db->bdi_db->mpf,
377                                         DB_PRIORITY_VERY_LOW );
378
379                         rc = db->bdi_db->set_pagesize( db->bdi_db,
380                                 BDB_ID2ENTRY_PAGESIZE );
381                         if ( slapMode & SLAP_TOOL_READMAIN ) {
382                                 flags |= DB_RDONLY;
383                         } else {
384                                 flags |= DB_CREATE;
385                         }
386                 } else {
387                         rc = db->bdi_db->set_flags( db->bdi_db, 
388                                 DB_DUP | DB_DUPSORT );
389 #ifndef BDB_HIER
390                         if ( slapMode & SLAP_TOOL_READONLY ) {
391                                 flags |= DB_RDONLY;
392                         } else {
393                                 flags |= DB_CREATE;
394                         }
395 #else
396                         if ( slapMode & (SLAP_TOOL_READONLY|SLAP_TOOL_READMAIN) ) {
397                                 flags |= DB_RDONLY;
398                         } else {
399                                 flags |= DB_CREATE;
400                         }
401 #endif
402                         rc = db->bdi_db->set_pagesize( db->bdi_db,
403                                 BDB_PAGESIZE );
404                 }
405
406 #ifdef HAVE_EBCDIC
407                 strcpy( path, bdbi_databases[i].file );
408                 __atoe( path );
409                 rc = DB_OPEN( db->bdi_db,
410                         path,
411                 /*      bdbi_databases[i].name, */ NULL,
412                         bdbi_databases[i].type,
413                         bdbi_databases[i].flags | flags,
414                         bdb->bi_dbenv_mode );
415 #else
416                 rc = DB_OPEN( db->bdi_db,
417                         bdbi_databases[i].file,
418                 /*      bdbi_databases[i].name, */ NULL,
419                         bdbi_databases[i].type,
420                         bdbi_databases[i].flags | flags,
421                         bdb->bi_dbenv_mode );
422 #endif
423
424                 if ( rc != 0 ) {
425                         char    buf[SLAP_TEXT_BUFLEN];
426
427                         snprintf( buf, sizeof(buf), "%s/%s", 
428                                 bdb->bi_dbenv_home, bdbi_databases[i].file );
429                         Debug( LDAP_DEBUG_ANY,
430                                 LDAP_XSTRING(bdb_db_open) ": db_open(%s) failed: %s (%d)\n",
431                                 buf, db_strerror(rc), rc );
432                         db->bdi_db->close( db->bdi_db, 0 );
433                         goto fail;
434                 }
435
436                 flags &= ~(DB_CREATE | DB_RDONLY);
437                 db->bdi_name = bdbi_databases[i].name;
438                 bdb->bi_databases[i] = db;
439         }
440
441         bdb->bi_databases[i] = NULL;
442         bdb->bi_ndatabases = i;
443
444         /* get nextid */
445         rc = bdb_last_id( be, NULL );
446         if( rc != 0 ) {
447                 Debug( LDAP_DEBUG_ANY,
448                         LDAP_XSTRING(bdb_db_open) ": last_id(%s) failed: %s (%d)\n",
449                         bdb->bi_dbenv_home, db_strerror(rc), rc );
450                 goto fail;
451         }
452
453         if ( !quick ) {
454 #if DB_VERSION_FULL >= 0x04060012
455                 u_int32_t lid;
456                 XLOCK_ID(bdb->bi_dbenv, &lid);
457                 __lock_getlocker(bdb->bi_dbenv->lk_handle, lid, 0, &bdb->bi_cache.c_locker);
458 #else
459                 XLOCK_ID(bdb->bi_dbenv, &bdb->bi_cache.c_locker);
460 #endif
461         }
462
463         entry_prealloc( bdb->bi_cache.c_maxsize );
464         attr_prealloc( bdb->bi_cache.c_maxsize * 20 );
465
466         /* setup for empty-DN contexts */
467         if ( BER_BVISEMPTY( &be->be_nsuffix[0] )) {
468                 rc = bdb_id2entry( be, NULL, 0, 0, &e );
469         }
470         if ( !e ) {
471                 e = entry_alloc();
472                 e->e_id = 0;
473                 ber_dupbv( &e->e_name, (struct berval *)&slap_empty_bv );
474                 ber_dupbv( &e->e_nname, (struct berval *)&slap_empty_bv );
475         }
476         e->e_ocflags = SLAP_OC_GLUE|SLAP_OC__END;
477         e->e_private = &bdb->bi_cache.c_dntree;
478         bdb->bi_cache.c_dntree.bei_e = e;
479
480         /* monitor setup */
481         rc = bdb_monitor_db_open( be );
482         if ( rc != 0 ) {
483                 goto fail;
484         }
485
486         bdb->bi_flags |= BDB_IS_OPEN;
487
488         return 0;
489
490 fail:
491         bdb_db_close( be, NULL );
492         return rc;
493 }
494
495 static int
496 bdb_db_close( BackendDB *be, ConfigReply *cr )
497 {
498         int rc;
499         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
500         struct bdb_db_info *db;
501         bdb_idl_cache_entry_t *entry, *next_entry;
502
503         /* monitor handling */
504         (void)bdb_monitor_db_close( be );
505
506         {
507                 Entry *e = bdb->bi_cache.c_dntree.bei_e;
508                 if ( e ) {
509                         bdb->bi_cache.c_dntree.bei_e = NULL;
510                         e->e_private = NULL;
511                         bdb_entry_return( e );
512                 }
513         }
514
515         bdb->bi_flags &= ~BDB_IS_OPEN;
516
517         ber_bvarray_free( bdb->bi_db_config );
518         bdb->bi_db_config = NULL;
519
520         while( bdb->bi_databases && bdb->bi_ndatabases-- ) {
521                 db = bdb->bi_databases[bdb->bi_ndatabases];
522                 rc = db->bdi_db->close( db->bdi_db, 0 );
523                 /* Lower numbered names are not strdup'd */
524                 if( bdb->bi_ndatabases >= BDB_NDB )
525                         free( db->bdi_name );
526                 free( db );
527         }
528         free( bdb->bi_databases );
529         bdb->bi_databases = NULL;
530
531         bdb_cache_release_all (&bdb->bi_cache);
532
533         if ( bdb->bi_idl_cache_size ) {
534                 avl_free( bdb->bi_idl_tree, NULL );
535                 bdb->bi_idl_tree = NULL;
536                 entry = bdb->bi_idl_lru_head;
537                 do {
538                         next_entry = entry->idl_lru_next;
539                         if ( entry->idl )
540                                 free( entry->idl );
541                         free( entry->kstr.bv_val );
542                         free( entry );
543                         entry = next_entry;
544                 } while ( entry != bdb->bi_idl_lru_head );
545                 bdb->bi_idl_lru_head = bdb->bi_idl_lru_tail = NULL;
546         }
547
548         /* close db environment */
549         if( bdb->bi_dbenv ) {
550                 /* Free cache locker if we enabled locking */
551                 if ( !( slapMode & SLAP_TOOL_QUICK )) {
552 #if DB_VERSION_FULL >= 0x04060012
553                         XLOCK_ID_FREE(bdb->bi_dbenv, bdb->bi_cache.c_locker->id);
554 #else
555                         XLOCK_ID_FREE(bdb->bi_dbenv, bdb->bi_cache.c_locker);
556 #endif
557                         bdb->bi_cache.c_locker = 0;
558                 }
559 #ifdef BDB_REUSE_LOCKERS
560                 bdb_locker_flush( bdb->bi_dbenv );
561 #endif
562                 /* force a checkpoint, but not if we were ReadOnly,
563                  * and not in Quick mode since there are no transactions there.
564                  */
565                 if ( !( slapMode & ( SLAP_TOOL_QUICK|SLAP_TOOL_READONLY ))) {
566                         rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
567                         if( rc != 0 ) {
568                                 Debug( LDAP_DEBUG_ANY,
569                                         "bdb_db_close: txn_checkpoint failed: %s (%d)\n",
570                                         db_strerror(rc), rc, 0 );
571                         }
572                 }
573
574                 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
575                 bdb->bi_dbenv = NULL;
576                 if( rc != 0 ) {
577                         Debug( LDAP_DEBUG_ANY,
578                                 "bdb_db_close: close failed: %s (%d)\n",
579                                 db_strerror(rc), rc, 0 );
580                         return rc;
581                 }
582         }
583
584         rc = alock_close( &bdb->bi_alock_info );
585         if( rc != 0 ) {
586                 Debug( LDAP_DEBUG_ANY,
587                         "bdb_db_close: alock_close failed\n", 0, 0, 0 );
588                 return -1;
589         }
590
591         return 0;
592 }
593
594 static int
595 bdb_db_destroy( BackendDB *be, ConfigReply *cr )
596 {
597         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
598
599         /* monitor handling */
600         (void)bdb_monitor_db_destroy( be );
601
602         if( bdb->bi_dbenv_home ) ch_free( bdb->bi_dbenv_home );
603         if( bdb->bi_db_config_path ) ch_free( bdb->bi_db_config_path );
604
605         bdb_attr_index_destroy( bdb );
606
607         ldap_pvt_thread_rdwr_destroy ( &bdb->bi_cache.c_rwlock );
608         ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_lru_mutex );
609         ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_count_mutex );
610         ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_eifree_mutex );
611         ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_dntree.bei_kids_mutex );
612 #ifdef BDB_HIER
613         ldap_pvt_thread_mutex_destroy( &bdb->bi_modrdns_mutex );
614 #endif
615         ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
616         ldap_pvt_thread_mutex_destroy( &bdb->bi_database_mutex );
617         ldap_pvt_thread_rdwr_destroy( &bdb->bi_idl_tree_rwlock );
618         ldap_pvt_thread_mutex_destroy( &bdb->bi_idl_tree_lrulock );
619
620         ch_free( bdb );
621         be->be_private = NULL;
622
623         return 0;
624 }
625
626 int
627 bdb_back_initialize(
628         BackendInfo     *bi )
629 {
630         int rc;
631
632         static char *controls[] = {
633                 LDAP_CONTROL_ASSERT,
634                 LDAP_CONTROL_MANAGEDSAIT,
635                 LDAP_CONTROL_NOOP,
636                 LDAP_CONTROL_PAGEDRESULTS,
637                 LDAP_CONTROL_PRE_READ,
638                 LDAP_CONTROL_POST_READ,
639                 LDAP_CONTROL_SUBENTRIES,
640                 LDAP_CONTROL_X_PERMISSIVE_MODIFY,
641 #ifdef LDAP_X_TXN
642                 LDAP_CONTROL_X_TXN_SPEC,
643 #endif
644                 NULL
645         };
646
647         /* initialize the underlying database system */
648         Debug( LDAP_DEBUG_TRACE,
649                 LDAP_XSTRING(bdb_back_initialize) ": initialize " 
650                 BDB_UCTYPE " backend\n", 0, 0, 0 );
651
652         bi->bi_flags |=
653                 SLAP_BFLAG_INCREMENT |
654                 SLAP_BFLAG_SUBENTRIES |
655                 SLAP_BFLAG_ALIASES |
656                 SLAP_BFLAG_REFERRALS;
657
658         bi->bi_controls = controls;
659
660         {       /* version check */
661                 int major, minor, patch, ver;
662                 char *version = db_version( &major, &minor, &patch );
663 #ifdef HAVE_EBCDIC
664                 char v2[1024];
665
666                 /* All our stdio does an ASCII to EBCDIC conversion on
667                  * the output. Strings from the BDB library are already
668                  * in EBCDIC; we have to go back and forth...
669                  */
670                 strcpy( v2, version );
671                 __etoa( v2 );
672                 version = v2;
673 #endif
674
675                 ver = (major << 24) | (minor << 16) | patch;
676                 if( ver != DB_VERSION_FULL ) {
677                         /* fail if a versions don't match */
678                         Debug( LDAP_DEBUG_ANY,
679                                 LDAP_XSTRING(bdb_back_initialize) ": "
680                                 "BDB library version mismatch:"
681                                 " expected " DB_VERSION_STRING ","
682                                 " got %s\n", version, 0, 0 );
683                         return -1;
684                 }
685
686                 Debug( LDAP_DEBUG_TRACE, LDAP_XSTRING(bdb_back_initialize)
687                         ": %s\n", version, 0, 0 );
688         }
689
690         db_env_set_func_free( ber_memfree );
691         db_env_set_func_malloc( (db_malloc *)ber_memalloc );
692         db_env_set_func_realloc( (db_realloc *)ber_memrealloc );
693 #ifndef NO_THREAD
694         /* This is a no-op on a NO_THREAD build. Leave the default
695          * alone so that BDB will sleep on interprocess conflicts.
696          */
697         db_env_set_func_yield( ldap_pvt_thread_yield );
698 #endif
699
700         bi->bi_open = 0;
701         bi->bi_close = 0;
702         bi->bi_config = 0;
703         bi->bi_destroy = 0;
704
705         bi->bi_db_init = bdb_db_init;
706         bi->bi_db_config = config_generic_wrapper;
707         bi->bi_db_open = bdb_db_open;
708         bi->bi_db_close = bdb_db_close;
709         bi->bi_db_destroy = bdb_db_destroy;
710
711         bi->bi_op_add = bdb_add;
712         bi->bi_op_bind = bdb_bind;
713         bi->bi_op_compare = bdb_compare;
714         bi->bi_op_delete = bdb_delete;
715         bi->bi_op_modify = bdb_modify;
716         bi->bi_op_modrdn = bdb_modrdn;
717         bi->bi_op_search = bdb_search;
718
719         bi->bi_op_unbind = 0;
720
721         bi->bi_extended = bdb_extended;
722
723         bi->bi_chk_referrals = bdb_referrals;
724         bi->bi_operational = bdb_operational;
725         bi->bi_has_subordinates = bdb_hasSubordinates;
726         bi->bi_entry_release_rw = bdb_entry_release;
727         bi->bi_entry_get_rw = bdb_entry_get;
728
729         /*
730          * hooks for slap tools
731          */
732         bi->bi_tool_entry_open = bdb_tool_entry_open;
733         bi->bi_tool_entry_close = bdb_tool_entry_close;
734         bi->bi_tool_entry_first = bdb_tool_entry_next;
735         bi->bi_tool_entry_next = bdb_tool_entry_next;
736         bi->bi_tool_entry_get = bdb_tool_entry_get;
737         bi->bi_tool_entry_put = bdb_tool_entry_put;
738         bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
739         bi->bi_tool_sync = 0;
740         bi->bi_tool_dn2id_get = bdb_tool_dn2id_get;
741         bi->bi_tool_entry_modify = bdb_tool_entry_modify;
742
743         bi->bi_connection_init = 0;
744         bi->bi_connection_destroy = 0;
745
746         rc = bdb_back_init_cf( bi );
747
748         return rc;
749 }
750
751 #if     (SLAPD_BDB == SLAPD_MOD_DYNAMIC && !defined(BDB_HIER)) || \
752         (SLAPD_HDB == SLAPD_MOD_DYNAMIC && defined(BDB_HIER))
753
754 /* conditionally define the init_module() function */
755 #ifdef BDB_HIER
756 SLAP_BACKEND_INIT_MODULE( hdb )
757 #else /* !BDB_HIER */
758 SLAP_BACKEND_INIT_MODULE( bdb )
759 #endif /* !BDB_HIER */
760
761 #endif /* SLAPD_[BH]DB == SLAPD_MOD_DYNAMIC */
762