]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/init.c
Fix checkpoint task start/stop
[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-2005 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
49         Debug( LDAP_DEBUG_TRACE,
50                 LDAP_XSTRING(bdb_db_init) ": Initializing " BDB_UCTYPE " database\n",
51                 0, 0, 0 );
52
53         /* allocate backend-database-specific stuff */
54         bdb = (struct bdb_info *) ch_calloc( 1, sizeof(struct bdb_info) );
55
56         /* DBEnv parameters */
57         bdb->bi_dbenv_home = ch_strdup( SLAPD_DEFAULT_DB_DIR );
58         bdb->bi_dbenv_xflags = 0;
59         bdb->bi_dbenv_mode = SLAPD_DEFAULT_DB_MODE;
60
61         bdb->bi_cache.c_maxsize = DEFAULT_CACHE_SIZE;
62
63         bdb->bi_lock_detect = DB_LOCK_DEFAULT;
64         bdb->bi_search_stack_depth = DEFAULT_SEARCH_STACK_DEPTH;
65         bdb->bi_search_stack = NULL;
66
67         ldap_pvt_thread_mutex_init( &bdb->bi_database_mutex );
68         ldap_pvt_thread_mutex_init( &bdb->bi_lastid_mutex );
69         ldap_pvt_thread_mutex_init( &bdb->bi_cache.lru_mutex );
70         ldap_pvt_thread_mutex_init( &bdb->bi_cache.c_dntree.bei_kids_mutex );
71         ldap_pvt_thread_rdwr_init ( &bdb->bi_cache.c_rwlock );
72
73         be->be_private = bdb;
74         be->be_cf_table = be->bd_info->bi_cf_table;
75
76         return 0;
77 }
78
79 /*
80  * Unconditionally perform a database recovery. Only works on
81  * databases that were previously opened with transactions and
82  * logs enabled.
83  */
84 static int
85 bdb_do_recovery( BackendDB *be )
86 {
87         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
88         DB_ENV  *re_dbenv;
89         u_int32_t flags;
90         int             rc;
91         char    path[MAXPATHLEN], *ptr;
92
93         /* Create and init the recovery environment */
94         rc = db_env_create( &re_dbenv, 0 );
95         if( rc != 0 ) {
96                 Debug( LDAP_DEBUG_ANY,
97                         "bdb_do_recovery: db_env_create failed: %s (%d)\n",
98                         db_strerror(rc), rc, 0 );
99                 return rc;
100         }
101         re_dbenv->set_errpfx( re_dbenv, be->be_suffix[0].bv_val );
102         re_dbenv->set_errcall( re_dbenv, bdb_errcall );
103         (void)re_dbenv->set_verbose(re_dbenv, DB_VERB_RECOVERY, 1);
104 #if DB_VERSION_FULL < 0x04030000
105         (void)re_dbenv->set_verbose(re_dbenv, DB_VERB_CHKPOINT, 1);
106 #else
107         re_dbenv->set_msgcall( re_dbenv, bdb_msgcall );
108 #endif
109
110         flags = DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL |
111                 DB_INIT_TXN | DB_USE_ENVIRON | DB_RECOVER;
112
113         /* Open the environment, which will also perform the recovery */
114 #ifdef HAVE_EBCDIC
115         strcpy( path, bdb->bi_dbenv_home );
116         __atoe( path );
117         rc = re_dbenv->open( re_dbenv,
118                 path,
119                 flags,
120                 bdb->bi_dbenv_mode );
121 #else
122         rc = re_dbenv->open( re_dbenv,
123                 bdb->bi_dbenv_home,
124                 flags,
125                 bdb->bi_dbenv_mode );
126 #endif
127         if( rc != 0 ) {
128                 Debug( LDAP_DEBUG_ANY,
129                         "bdb_do_recovery: dbenv_open failed: %s (%d)\n",
130                         db_strerror(rc), rc, 0 );
131                 return rc;
132         }
133         (void) re_dbenv->close( re_dbenv, 0 );
134
135         /* By convention we reset the mtime for id2entry.bdb to the current time */
136         ptr = lutil_strcopy( path, bdb->bi_dbenv_home);
137         *ptr++ = LDAP_DIRSEP[0];
138         strcpy( ptr, bdbi_databases[0].file);
139         (void) utime( path, NULL);
140
141         return 0;
142 }
143
144 /*
145  * Database recovery logic:
146  * This function is called whenever the database appears to have been
147  * shut down uncleanly, as determined by the alock functions. 
148  * Because of the -q function in slapadd, there is also the possibility
149  * that the shutdown happened when transactions weren't being used and
150  * the database is likely to be corrupt. The function checks for this
151  * condition by examining the environment to make sure it had previously
152  * been opened with transactions enabled. If this is the case, the
153  * database is recovered as usual. If transactions were not enabled,
154  * then this function will return a fail.
155  */
156 static int
157 bdb_db_recover( BackendDB *be )
158 {
159         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
160         DB_ENV  *re_dbenv;
161         u_int32_t flags;
162         int             rc;
163 #ifdef HAVE_EBCDIC
164         char    path[MAXPATHLEN];
165 #endif
166
167         /* Create the recovery environment, then open it.
168          * We use the DB_JOIN in combination with a flags value of
169          * zero so we join an existing environment and can read the
170          * value of the flags that were used the last time the 
171          * environment was opened. DB_CREATE is added because the
172          * open would fail if the only thing that had been done
173          * was an open with transactions and logs disabled.
174          */
175         rc = db_env_create( &re_dbenv, 0 );
176         if( rc != 0 ) {
177                 Debug( LDAP_DEBUG_ANY,
178                         "bdb_db_recover: db_env_create failed: %s (%d)\n",
179                         db_strerror(rc), rc, 0 );
180                 return rc;
181         }
182         re_dbenv->set_errpfx( re_dbenv, be->be_suffix[0].bv_val );
183         re_dbenv->set_errcall( re_dbenv, bdb_errcall );
184
185         Debug( LDAP_DEBUG_TRACE,
186                 "bdb_db_recover: dbenv_open(%s)\n",
187                 bdb->bi_dbenv_home, 0, 0);
188
189 #ifdef HAVE_EBCDIC
190         strcpy( path, bdb->bi_dbenv_home );
191         __atoe( path );
192         rc = re_dbenv->open( re_dbenv,
193                 path,
194                 DB_JOINENV,
195                 bdb->bi_dbenv_mode );
196 #else
197         rc = re_dbenv->open( re_dbenv,
198                 bdb->bi_dbenv_home,
199                 DB_JOINENV,
200                 bdb->bi_dbenv_mode );
201 #endif
202
203         if( rc == ENOENT ) {
204                 goto re_exit;
205         }
206         else if( rc != 0 ) {
207                 Debug( LDAP_DEBUG_ANY,
208                         "bdb_db_recover: dbenv_open failed: %s (%d)\n",
209                         db_strerror(rc), rc, 0 );
210                 return rc;
211         }
212
213         /*
214          * Check the flags that had been used in the previous open.
215          * The environment needed to have had both
216          * DB_INIT_LOG and DB_INIT_TXN set for us to be willing to
217          * recover the database. Otherwise the an app failed while running
218          * without transactions and logs enabled and the dn2id and id2entry
219          * mapping is likely to be corrupt.
220          */
221         rc = re_dbenv->get_open_flags( re_dbenv, &flags );
222         if( rc != 0 ) {
223                 Debug( LDAP_DEBUG_ANY,
224                         "bdb_db_recover: get_open_flags failed: %s (%d)\n",
225                         db_strerror(rc), rc, 0 );
226                 return rc;
227         }
228
229         (void) re_dbenv->close( re_dbenv, 0 );
230
231         if( (flags & DB_INIT_LOG) && (flags & DB_INIT_TXN) ) {
232                 return bdb_do_recovery( be );
233         }
234
235 re_exit:
236         Debug( LDAP_DEBUG_ANY,
237                 "bdb_db_recover: Database cannot be recovered. "\
238                 "Restore from backup!\n", 0, 0, 0);
239         return -1;
240
241 }
242
243
244 static int
245 bdb_db_open( BackendDB *be )
246 {
247         int rc, i;
248         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
249         struct stat stat1, stat2;
250         u_int32_t flags;
251         char path[MAXPATHLEN];
252         char *ptr;
253
254         Debug( LDAP_DEBUG_ARGS,
255                 "bdb_db_open: %s\n",
256                 be->be_suffix[0].bv_val, 0, 0 );
257
258 #ifndef BDB_MULTIPLE_SUFFIXES
259         if ( be->be_suffix[1].bv_val ) {
260         Debug( LDAP_DEBUG_ANY,
261                 "bdb_db_open: only one suffix allowed\n", 0, 0, 0 );
262                 return -1;
263         }
264 #endif
265
266         /* Check existence of dbenv_home. Any error means trouble */
267         rc = stat( bdb->bi_dbenv_home, &stat1 );
268         if( rc !=0 ) {
269                 Debug( LDAP_DEBUG_ANY,
270                         "bdb_db_open: Cannot access database directory %s (%d)\n",
271                         bdb->bi_dbenv_home, errno, 0 );
272                         return -1;
273         }
274         
275         /* Perform database use arbitration/recovery logic */
276         rc = alock_open( &bdb->bi_alock_info, 
277                                 "slapd", 
278                                 bdb->bi_dbenv_home,
279                                 slapMode & SLAP_TOOL_READONLY ?
280                                 ALOCK_LOCKED : ALOCK_UNIQUE );
281
282         if( rc == ALOCK_RECOVER ) {
283                 Debug( LDAP_DEBUG_ANY,
284                         "bdb_db_open: unclean shutdown detected;"
285                         " attempting recovery.\n", 
286                         0, 0, 0 );
287                 if( bdb_db_recover( be ) != 0 ) {
288                         Debug( LDAP_DEBUG_ANY,
289                                 "bdb_db_open: DB recovery failed.\n",
290                                 0, 0, 0 );
291                         return -1;
292                 }
293                 if( alock_recover (&bdb->bi_alock_info) != 0 ) {
294                         Debug( LDAP_DEBUG_ANY,
295                                 "bdb_db_open: alock_recover failed\n",
296                                 0, 0, 0 );
297                         return -1;
298                 }
299
300         } else if( rc == ALOCK_BUSY ) {
301                 Debug( LDAP_DEBUG_ANY,
302                         "bdb_db_open: database already in use\n", 
303                         0, 0, 0 );
304                 return -1;
305         } else if( rc != ALOCK_CLEAN ) {
306                 Debug( LDAP_DEBUG_ANY,
307                         "bdb_db_open: alock package is unstable\n", 
308                         0, 0, 0 );
309                 return -1;
310         }
311         
312         /*
313          * The DB_CONFIG file may have changed. If so, recover the
314          * database so that new settings are put into effect. Also
315          * note the possible absence of DB_CONFIG in the log.
316          */
317         if( stat( bdb->bi_db_config_path, &stat1 ) == 0 ) {
318                 ptr = lutil_strcopy(path, bdb->bi_dbenv_home);
319                 *ptr++ = LDAP_DIRSEP[0];
320                 strcpy( ptr, bdbi_databases[0].file);
321                 if( stat( path, &stat2 ) == 0 ) {
322                         if( stat2.st_mtime <= stat1.st_mtime ) {
323                                 Debug( LDAP_DEBUG_ANY,
324                                         "bdb_db_open: DB_CONFIG for suffix %s has changed.\n"
325                                         "Performing database recovery to activate new settings.\n",
326                                         be->be_suffix[0].bv_val, 0, 0 );
327                                 if( bdb_do_recovery( be ) != 0) {
328                                         Debug( LDAP_DEBUG_ANY,
329                                                 "bdb_db_open: db recovery failed.\n",
330                                                 0, 0, 0 );
331                                         return -1;
332                                 }
333                         }
334                                         
335                 }
336         }
337         else {
338                 Debug( LDAP_DEBUG_ANY,
339                         "bdb_db_open: Warning - No DB_CONFIG file found "
340                         "in directory %s: (%d)\n"
341                         "Expect poor performance for suffix %s.\n",
342                         bdb->bi_dbenv_home, errno, be->be_suffix[0].bv_val );
343         }
344                 
345         flags = DB_INIT_MPOOL | DB_THREAD | DB_CREATE;
346         if ( !( slapMode & SLAP_TOOL_QUICK ))
347                 flags |= DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN;
348
349         rc = db_env_create( &bdb->bi_dbenv, 0 );
350         if( rc != 0 ) {
351                 Debug( LDAP_DEBUG_ANY,
352                         "bdb_db_open: db_env_create failed: %s (%d)\n",
353                         db_strerror(rc), rc, 0 );
354                 return rc;
355         }
356
357         /* If a key was set, use shared memory for the BDB environment */
358         if ( bdb->bi_shm_key ) {
359                 bdb->bi_dbenv->set_shm_key( bdb->bi_dbenv, bdb->bi_shm_key );
360                 flags |= DB_SYSTEM_MEM;
361         }
362
363         bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0].bv_val );
364         bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
365         bdb->bi_dbenv->set_lk_detect( bdb->bi_dbenv, bdb->bi_lock_detect );
366
367         /* One long-lived TXN per thread, two TXNs per write op */
368         bdb->bi_dbenv->set_tx_max( bdb->bi_dbenv, connection_pool_max * 3 );
369
370 #ifdef SLAP_ZONE_ALLOC
371         if ( bdb->bi_cache.c_maxsize ) {
372                 bdb->bi_cache.c_zctx = slap_zn_mem_create(
373                                                                 SLAP_ZONE_INITSIZE,
374                                                                 SLAP_ZONE_MAXSIZE,
375                                                                 SLAP_ZONE_DELTA,
376                                                                 SLAP_ZONE_SIZE);
377         }
378 #endif
379
380         if ( bdb->bi_idl_cache_max_size ) {
381                 bdb->bi_idl_tree = NULL;
382                 ldap_pvt_thread_rdwr_init( &bdb->bi_idl_tree_rwlock );
383                 ldap_pvt_thread_mutex_init( &bdb->bi_idl_tree_lrulock );
384                 bdb->bi_idl_cache_size = 0;
385         }
386
387         if( bdb->bi_dbenv_xflags != 0 ) {
388                 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
389                         bdb->bi_dbenv_xflags, 1);
390                 if( rc != 0 ) {
391                         Debug( LDAP_DEBUG_ANY,
392                                 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
393                                 db_strerror(rc), rc, 0 );
394                         return rc;
395                 }
396         }
397
398         Debug( LDAP_DEBUG_TRACE,
399                 "bdb_db_open: dbenv_open(%s)\n",
400                 bdb->bi_dbenv_home, 0, 0);
401
402 #ifdef HAVE_EBCDIC
403         strcpy( path, bdb->bi_dbenv_home );
404         __atoe( path );
405         rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
406                 path,
407                 flags,
408                 bdb->bi_dbenv_mode );
409 #else
410         rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
411                 bdb->bi_dbenv_home,
412                 flags,
413                 bdb->bi_dbenv_mode );
414 #endif
415         if( rc != 0 ) {
416                 Debug( LDAP_DEBUG_ANY,
417                         "bdb_db_open: dbenv_open failed: %s (%d)\n",
418                         db_strerror(rc), rc, 0 );
419                 return rc;
420         }
421
422         flags = DB_THREAD | bdb->bi_db_opflags;
423
424 #ifdef DB_AUTO_COMMIT
425         if ( !( slapMode & SLAP_TOOL_QUICK ))
426                 flags |= DB_AUTO_COMMIT;
427 #endif
428
429         bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
430                 BDB_INDICES * sizeof(struct bdb_db_info *) );
431
432         /* open (and create) main database */
433         for( i = 0; bdbi_databases[i].name; i++ ) {
434                 struct bdb_db_info *db;
435
436                 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
437
438                 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
439                 if( rc != 0 ) {
440                         Debug( LDAP_DEBUG_ANY,
441                                 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
442                                 bdb->bi_dbenv_home, db_strerror(rc), rc );
443                         return rc;
444                 }
445
446                 if( i == BDB_ID2ENTRY ) {
447                         rc = db->bdi_db->set_pagesize( db->bdi_db,
448                                 BDB_ID2ENTRY_PAGESIZE );
449                         if ( slapMode & SLAP_TOOL_READMAIN ) {
450                                 flags |= DB_RDONLY;
451                         } else {
452                                 flags |= DB_CREATE;
453                         }
454                 } else {
455                         rc = db->bdi_db->set_flags( db->bdi_db, 
456                                 DB_DUP | DB_DUPSORT );
457 #ifndef BDB_HIER
458                         if ( slapMode & SLAP_TOOL_READONLY ) {
459                                 flags |= DB_RDONLY;
460                         } else {
461                                 flags |= DB_CREATE;
462                         }
463 #else
464                         if ( slapMode & (SLAP_TOOL_READONLY|SLAP_TOOL_READMAIN) ) {
465                                 flags |= DB_RDONLY;
466                         } else {
467                                 flags |= DB_CREATE;
468                         }
469 #endif
470                         rc = db->bdi_db->set_pagesize( db->bdi_db,
471                                 BDB_PAGESIZE );
472                 }
473
474 #ifdef HAVE_EBCDIC
475                 strcpy( path, bdbi_databases[i].file );
476                 __atoe( path );
477                 rc = DB_OPEN( db->bdi_db,
478                         path,
479                 /*      bdbi_databases[i].name, */ NULL,
480                         bdbi_databases[i].type,
481                         bdbi_databases[i].flags | flags,
482                         bdb->bi_dbenv_mode );
483 #else
484                 rc = DB_OPEN( db->bdi_db,
485                         bdbi_databases[i].file,
486                 /*      bdbi_databases[i].name, */ NULL,
487                         bdbi_databases[i].type,
488                         bdbi_databases[i].flags | flags,
489                         bdb->bi_dbenv_mode );
490 #endif
491
492                 if ( rc != 0 ) {
493                         char    buf[SLAP_TEXT_BUFLEN];
494
495                         snprintf( buf, sizeof(buf), "%s/%s", 
496                                 bdb->bi_dbenv_home, bdbi_databases[i].file );
497                         Debug( LDAP_DEBUG_ANY,
498                                 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
499                                 buf, db_strerror(rc), rc );
500                         return rc;
501                 }
502
503                 flags &= ~(DB_CREATE | DB_RDONLY);
504                 db->bdi_name = bdbi_databases[i].name;
505                 bdb->bi_databases[i] = db;
506         }
507
508         bdb->bi_databases[i] = NULL;
509         bdb->bi_ndatabases = i;
510
511         /* get nextid */
512         rc = bdb_last_id( be, NULL );
513         if( rc != 0 ) {
514                 Debug( LDAP_DEBUG_ANY,
515                         "bdb_db_open: last_id(%s) failed: %s (%d)\n",
516                         bdb->bi_dbenv_home, db_strerror(rc), rc );
517                 return rc;
518         }
519
520         if ( !( slapMode & SLAP_TOOL_QUICK )) {
521                 XLOCK_ID(bdb->bi_dbenv, &bdb->bi_cache.c_locker);
522         }
523
524         if (( slapMode&SLAP_SERVER_MODE ) && ( bdb->bi_flags&BDB_HAS_CONFIG )) {
525                 char    buf[SLAP_TEXT_BUFLEN];
526                 FILE *f = fopen( bdb->bi_db_config_path, "r" );
527                 struct berval bv;
528
529                 if ( f ) {
530                         while ( fgets( buf, sizeof(buf), f )) {
531                                 ber_str2bv( buf, 0, 1, &bv );
532                                 if ( bv.bv_val[bv.bv_len-1] == '\n' ) {
533                                         bv.bv_len--;
534                                         bv.bv_val[bv.bv_len] = '\0';
535                                 }
536                                 /* shouldn't need this, but ... */
537                                 if ( bv.bv_val[bv.bv_len-1] == '\r' ) {
538                                         bv.bv_len--;
539                                         bv.bv_val[bv.bv_len] = '\0';
540                                 }
541                                 ber_bvarray_add( &bdb->bi_db_config, &bv );
542                         }
543                         fclose( f );
544                 } else {
545                         /* Eh? It disappeared between config and open?? */
546                         bdb->bi_flags &= ~BDB_HAS_CONFIG;
547                 }
548
549         }
550         bdb->bi_flags |= BDB_IS_OPEN;
551
552         return 0;
553 }
554
555 static int
556 bdb_db_close( BackendDB *be )
557 {
558         int rc;
559         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
560         struct bdb_db_info *db;
561         bdb_idl_cache_entry_t *entry, *next_entry;
562
563         bdb->bi_flags &= ~BDB_IS_OPEN;
564
565         ber_bvarray_free( bdb->bi_db_config );
566
567         while( bdb->bi_ndatabases-- ) {
568                 db = bdb->bi_databases[bdb->bi_ndatabases];
569                 rc = db->bdi_db->close( db->bdi_db, 0 );
570                 /* Lower numbered names are not strdup'd */
571                 if( bdb->bi_ndatabases >= BDB_NDB )
572                         free( db->bdi_name );
573                 free( db );
574         }
575         free( bdb->bi_databases );
576         bdb_attr_index_destroy( bdb->bi_attrs );
577
578         bdb_cache_release_all (&bdb->bi_cache);
579
580         if ( bdb->bi_idl_cache_max_size ) {
581                 ldap_pvt_thread_rdwr_wlock ( &bdb->bi_idl_tree_rwlock );
582                 avl_free( bdb->bi_idl_tree, NULL );
583                 entry = bdb->bi_idl_lru_head;
584                 while ( entry != NULL ) {
585                         next_entry = entry->idl_lru_next;
586                         if ( entry->idl )
587                                 free( entry->idl );
588                         free( entry->kstr.bv_val );
589                         free( entry );
590                         entry = next_entry;
591                 }
592                 ldap_pvt_thread_rdwr_wunlock ( &bdb->bi_idl_tree_rwlock );
593         }
594
595         if ( !( slapMode & SLAP_TOOL_QUICK ) && bdb->bi_dbenv ) {
596                 XLOCK_ID_FREE(bdb->bi_dbenv, bdb->bi_cache.c_locker);
597         }
598
599         return 0;
600 }
601
602 static int
603 bdb_db_destroy( BackendDB *be )
604 {
605         int rc;
606         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
607
608         /* close db environment */
609         if( bdb->bi_dbenv ) {
610                 /* force a checkpoint */
611                 if ( !( slapMode & SLAP_TOOL_QUICK )) {
612                         rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
613                         if( rc != 0 ) {
614                                 Debug( LDAP_DEBUG_ANY,
615                                         "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
616                                         db_strerror(rc), rc, 0 );
617                         }
618                 }
619
620                 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
621                 bdb->bi_dbenv = NULL;
622                 if( rc != 0 ) {
623                         Debug( LDAP_DEBUG_ANY,
624                                 "bdb_db_destroy: close failed: %s (%d)\n",
625                                 db_strerror(rc), rc, 0 );
626                         return rc;
627                 }
628         }
629
630         rc = alock_close( &bdb->bi_alock_info );
631         if( rc != 0 ) {
632                 Debug( LDAP_DEBUG_ANY,
633                         "bdb_db_destroy: alock_close failed\n", 0, 0, 0 );
634                 return -1;
635         }
636
637         if( bdb->bi_dbenv_home ) ch_free( bdb->bi_dbenv_home );
638         if( bdb->bi_db_config_path ) ch_free( bdb->bi_db_config_path );
639
640         ldap_pvt_thread_rdwr_destroy ( &bdb->bi_cache.c_rwlock );
641         ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.lru_mutex );
642         ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_dntree.bei_kids_mutex );
643         ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
644         ldap_pvt_thread_mutex_destroy( &bdb->bi_database_mutex );
645         if ( bdb->bi_idl_cache_max_size ) {
646                 ldap_pvt_thread_rdwr_destroy( &bdb->bi_idl_tree_rwlock );
647                 ldap_pvt_thread_mutex_destroy( &bdb->bi_idl_tree_lrulock );
648         }
649
650         ch_free( bdb );
651         be->be_private = NULL;
652
653         return 0;
654 }
655
656 int
657 bdb_back_initialize(
658         BackendInfo     *bi )
659 {
660         int rc;
661
662         static char *controls[] = {
663                 LDAP_CONTROL_ASSERT,
664                 LDAP_CONTROL_MANAGEDSAIT,
665                 LDAP_CONTROL_NOOP,
666                 LDAP_CONTROL_PAGEDRESULTS,
667 #ifdef LDAP_CONTROL_SUBENTRIES
668                 LDAP_CONTROL_SUBENTRIES,
669 #endif
670 #ifdef LDAP_CONTROL_X_PERMISSIVE_MODIFY
671                 LDAP_CONTROL_X_PERMISSIVE_MODIFY,
672 #endif
673                 NULL
674         };
675
676         /* initialize the underlying database system */
677         Debug( LDAP_DEBUG_TRACE,
678                 LDAP_XSTRING(bdb_back_initialize) ": initialize " 
679                 BDB_UCTYPE " backend\n", 0, 0, 0 );
680
681         bi->bi_flags |=
682                 SLAP_BFLAG_INCREMENT |
683 #ifdef BDB_SUBENTRIES
684                 SLAP_BFLAG_SUBENTRIES |
685 #endif
686                 SLAP_BFLAG_ALIASES |
687                 SLAP_BFLAG_REFERRALS;
688
689         bi->bi_controls = controls;
690
691         {       /* version check */
692                 int major, minor, patch, ver;
693                 char *version = db_version( &major, &minor, &patch );
694 #ifdef HAVE_EBCDIC
695                 char v2[1024];
696
697                 /* All our stdio does an ASCII to EBCDIC conversion on
698                  * the output. Strings from the BDB library are already
699                  * in EBCDIC; we have to go back and forth...
700                  */
701                 strcpy( v2, version );
702                 __etoa( v2 );
703                 version = v2;
704 #endif
705
706                 ver = (major << 24) | (minor << 16) | patch;
707                 if( ver != DB_VERSION_FULL ) {
708                         /* fail if a versions don't match */
709                         Debug( LDAP_DEBUG_ANY,
710                                 LDAP_XSTRING(bdb_back_initialize) ": "
711                                 "BDB library version mismatch:"
712                                 " expected " DB_VERSION_STRING ","
713                                 " got %s\n", version, 0, 0 );
714                         return -1;
715                 }
716
717                 Debug( LDAP_DEBUG_TRACE, LDAP_XSTRING(bdb_back_initialize)
718                         ": %s\n", version, 0, 0 );
719         }
720
721         db_env_set_func_free( ber_memfree );
722         db_env_set_func_malloc( (db_malloc *)ber_memalloc );
723         db_env_set_func_realloc( (db_realloc *)ber_memrealloc );
724 #ifndef NO_THREAD
725         /* This is a no-op on a NO_THREAD build. Leave the default
726          * alone so that BDB will sleep on interprocess conflicts.
727          */
728         db_env_set_func_yield( ldap_pvt_thread_yield );
729 #endif
730
731         bi->bi_open = 0;
732         bi->bi_close = 0;
733         bi->bi_config = 0;
734         bi->bi_destroy = 0;
735
736         bi->bi_db_init = bdb_db_init;
737         bi->bi_db_config = config_generic_wrapper;
738         bi->bi_db_open = bdb_db_open;
739         bi->bi_db_close = bdb_db_close;
740         bi->bi_db_destroy = bdb_db_destroy;
741
742         bi->bi_op_add = bdb_add;
743         bi->bi_op_bind = bdb_bind;
744         bi->bi_op_compare = bdb_compare;
745         bi->bi_op_delete = bdb_delete;
746         bi->bi_op_modify = bdb_modify;
747         bi->bi_op_modrdn = bdb_modrdn;
748         bi->bi_op_search = bdb_search;
749
750         bi->bi_op_unbind = 0;
751
752         bi->bi_extended = bdb_extended;
753
754         bi->bi_chk_referrals = bdb_referrals;
755         bi->bi_operational = bdb_operational;
756         bi->bi_has_subordinates = bdb_hasSubordinates;
757         bi->bi_entry_release_rw = bdb_entry_release;
758         bi->bi_entry_get_rw = bdb_entry_get;
759
760         /*
761          * hooks for slap tools
762          */
763         bi->bi_tool_entry_open = bdb_tool_entry_open;
764         bi->bi_tool_entry_close = bdb_tool_entry_close;
765         bi->bi_tool_entry_first = bdb_tool_entry_next;
766         bi->bi_tool_entry_next = bdb_tool_entry_next;
767         bi->bi_tool_entry_get = bdb_tool_entry_get;
768         bi->bi_tool_entry_put = bdb_tool_entry_put;
769         bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
770         bi->bi_tool_sync = 0;
771         bi->bi_tool_dn2id_get = bdb_tool_dn2id_get;
772         bi->bi_tool_id2entry_get = bdb_tool_id2entry_get;
773         bi->bi_tool_entry_modify = bdb_tool_entry_modify;
774
775         bi->bi_connection_init = 0;
776         bi->bi_connection_destroy = 0;
777
778         rc = bdb_back_init_cf( bi );
779
780         return rc;
781 }
782
783 #if     (SLAPD_BDB == SLAPD_MOD_DYNAMIC && !defined(BDB_HIER)) || \
784         (SLAPD_HDB == SLAPD_MOD_DYNAMIC && defined(BDB_HIER))
785
786 /* conditionally define the init_module() function */
787 #ifdef BDB_HIER
788 SLAP_BACKEND_INIT_MODULE( hdb )
789 #else /* !BDB_HIER */
790 SLAP_BACKEND_INIT_MODULE( bdb )
791 #endif /* !BDB_HIER */
792
793 #endif /* SLAPD_[BH]DB == SLAPD_MOD_DYNAMIC */
794