]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/init.c
psearch consistency patch for releng
[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-2004 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
24 #include "back-bdb.h"
25 #include "external.h"
26 #include <lutil.h>
27
28 static const struct bdbi_database {
29         char *file;
30         char *name;
31         int type;
32         int flags;
33 } bdbi_databases[] = {
34         { "id2entry" BDB_SUFFIX, "id2entry", DB_BTREE, 0 },
35         { "dn2id" BDB_SUFFIX, "dn2id", DB_BTREE, 0 },
36         { NULL, NULL, 0, 0 }
37 };
38
39 struct berval bdb_uuid = BER_BVNULL;
40
41 typedef void * db_malloc(size_t);
42 typedef void * db_realloc(void *, size_t);
43
44 #if 0
45 static int
46 bdb_open( BackendInfo *bi )
47 {
48         return 0;
49 }
50
51 static int
52 bdb_destroy( BackendInfo *bi )
53 {
54         return 0;
55 }
56
57 static int
58 bdb_close( BackendInfo *bi )
59 {
60         /* terminate the underlying database system */
61         return 0;
62 }
63 #endif
64
65 static int
66 bdb_db_init( BackendDB *be )
67 {
68         struct bdb_info *bdb;
69
70 #ifdef NEW_LOGGING
71         LDAP_LOG( BACK_BDB, ENTRY, "bdb_db_init", 0, 0, 0 );
72 #else
73         Debug( LDAP_DEBUG_ANY,
74                 "bdb_db_init: Initializing %s database\n",
75                 be->bd_info->bi_type, 0, 0 );
76 #endif
77
78         /* allocate backend-database-specific stuff */
79         bdb = (struct bdb_info *) ch_calloc( 1, sizeof(struct bdb_info) );
80
81         /* DBEnv parameters */
82         bdb->bi_dbenv_home = ch_strdup( SLAPD_DEFAULT_DB_DIR );
83         bdb->bi_dbenv_xflags = 0;
84         bdb->bi_dbenv_mode = SLAPD_DEFAULT_DB_MODE;
85
86         bdb->bi_cache.c_maxsize = DEFAULT_CACHE_SIZE;
87
88         bdb->bi_lock_detect = DB_LOCK_DEFAULT;
89         bdb->bi_search_stack_depth = DEFAULT_SEARCH_STACK_DEPTH;
90         bdb->bi_search_stack = NULL;
91
92         LDAP_LIST_INIT (&bdb->bi_psearch_list);
93
94         ldap_pvt_thread_mutex_init( &bdb->bi_database_mutex );
95         ldap_pvt_thread_mutex_init( &bdb->bi_lastid_mutex );
96         ldap_pvt_thread_rdwr_init ( &bdb->bi_pslist_rwlock );
97         ldap_pvt_thread_mutex_init( &bdb->bi_cache.lru_mutex );
98         ldap_pvt_thread_mutex_init( &bdb->bi_cache.c_dntree.bei_kids_mutex );
99         ldap_pvt_thread_rdwr_init ( &bdb->bi_cache.c_rwlock );
100
101         be->be_private = bdb;
102
103         return 0;
104 }
105
106 int
107 bdb_bt_compare(
108         DB *db, 
109         const DBT *usrkey,
110         const DBT *curkey )
111 {
112         unsigned char *u, *c;
113         int i, x;
114
115         u = usrkey->data;
116         c = curkey->data;
117
118 #ifdef WORDS_BIGENDIAN
119         for( i = 0; i < (int)sizeof(ID); i++)
120 #else
121         for( i = sizeof(ID)-1; i >= 0; i--)
122 #endif
123         {
124                 x = u[i] - c[i];
125                 if( x ) return x;
126         }
127
128         return 0;
129 }
130
131 static int
132 bdb_db_open( BackendDB *be )
133 {
134         int rc, i;
135         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
136         u_int32_t flags;
137 #ifdef HAVE_EBCDIC
138         char path[MAXPATHLEN];
139 #endif
140
141 #ifdef NEW_LOGGING
142         LDAP_LOG( BACK_BDB, ARGS, 
143                 "bdb_db_open: %s\n", be->be_suffix[0].bv_val, 0, 0 );
144 #else
145         Debug( LDAP_DEBUG_ARGS,
146                 "bdb_db_open: %s\n",
147                 be->be_suffix[0].bv_val, 0, 0 );
148 #endif
149
150 #ifndef BDB_MULTIPLE_SUFFIXES
151         if ( be->be_suffix[1].bv_val ) {
152 #ifdef NEW_LOGGING
153         LDAP_LOG( BACK_BDB, ERR, 
154                 "bdb_db_open: only one suffix allowed\n", 0, 0, 0 );
155 #else
156         Debug( LDAP_DEBUG_ANY,
157                 "bdb_db_open: only one suffix allowed\n", 0, 0, 0 );
158 #endif
159                 return -1;
160         }
161 #endif
162         /* we should check existance of dbenv_home and db_directory */
163
164         rc = db_env_create( &bdb->bi_dbenv, 0 );
165         if( rc != 0 ) {
166 #ifdef NEW_LOGGING
167                 LDAP_LOG( BACK_BDB, ERR, 
168                         "bdb_db_open: db_env_create failed: %s (%d)\n", 
169                         db_strerror(rc), rc, 0 );
170 #else
171                 Debug( LDAP_DEBUG_ANY,
172                         "bdb_db_open: db_env_create failed: %s (%d)\n",
173                         db_strerror(rc), rc, 0 );
174 #endif
175                 return rc;
176         }
177
178         flags = DB_INIT_MPOOL | DB_THREAD | DB_CREATE
179                 | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN;
180         
181 #if 0
182         /* Never do automatic recovery, must perform it manually.
183          * Otherwise restarting with gentlehup will corrupt the
184          * database.
185          */
186         if( !(slapMode & SLAP_TOOL_MODE) ) flags |= DB_RECOVER;
187 #endif
188
189         /* If a key was set, use shared memory for the BDB environment */
190         if ( bdb->bi_shm_key ) {
191                 bdb->bi_dbenv->set_shm_key( bdb->bi_dbenv, bdb->bi_shm_key );
192                 flags |= DB_SYSTEM_MEM;
193         }
194
195         bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0].bv_val );
196         bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
197         bdb->bi_dbenv->set_lk_detect( bdb->bi_dbenv, bdb->bi_lock_detect );
198
199         /* One long-lived TXN per thread, two TXNs per write op */
200         bdb->bi_dbenv->set_tx_max( bdb->bi_dbenv, connection_pool_max * 3 );
201
202         if ( bdb->bi_idl_cache_max_size ) {
203                 bdb->bi_idl_tree = NULL;
204                 ldap_pvt_thread_rdwr_init( &bdb->bi_idl_tree_rwlock );
205                 ldap_pvt_thread_mutex_init( &bdb->bi_idl_tree_lrulock );
206                 bdb->bi_idl_cache_size = 0;
207         }
208
209 #ifdef BDB_SUBDIRS
210         {
211                 char dir[MAXPATHLEN], *ptr;
212                 
213                 if (bdb->bi_dbenv_home[0] == '.') {
214                         /* If home is a relative path, relative subdirs
215                          * are just concat'd by BDB. We don't want the
216                          * path to be concat'd twice, e.g.
217                          * ./test-db/./test-db/tmp
218                          */
219                         ptr = dir;
220                 } else {
221                         ptr = lutil_strcopy( dir, bdb->bi_dbenv_home );
222                         *ptr++ = LDAP_DIRSEP[0];
223 #ifdef HAVE_EBCDIC
224                         __atoe( dir );
225 #endif
226                 }
227
228                 strcpy( ptr, BDB_TMP_SUBDIR );
229 #ifdef HAVE_EBCDIC
230                 __atoe( ptr );
231 #endif
232                 rc = bdb->bi_dbenv->set_tmp_dir( bdb->bi_dbenv, dir );
233                 if( rc != 0 ) {
234 #ifdef NEW_LOGGING
235                         LDAP_LOG( BACK_BDB, ERR, 
236                                 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n", 
237                                 dir, db_strerror(rc), rc );
238 #else
239                         Debug( LDAP_DEBUG_ANY,
240                                 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
241                                 dir, db_strerror(rc), rc );
242 #endif
243                         return rc;
244                 }
245
246                 strcpy( ptr, BDB_LG_SUBDIR );
247 #ifdef HAVE_EBCDIC
248                 __atoe( ptr );
249 #endif
250                 rc = bdb->bi_dbenv->set_lg_dir( bdb->bi_dbenv, dir );
251                 if( rc != 0 ) {
252 #ifdef NEW_LOGGING
253                         LDAP_LOG( BACK_BDB, ERR, 
254                                 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n", 
255                                 dir, db_strerror(rc), rc );
256 #else
257                         Debug( LDAP_DEBUG_ANY,
258                                 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
259                                 dir, db_strerror(rc), rc );
260 #endif
261                         return rc;
262                 }
263
264                 strcpy( ptr, BDB_DATA_SUBDIR );
265 #ifdef HAVE_EBCDIC
266                 __atoe( ptr );
267 #endif
268                 rc = bdb->bi_dbenv->set_data_dir( bdb->bi_dbenv, dir );
269                 if( rc != 0 ) {
270 #ifdef NEW_LOGGING
271                         LDAP_LOG( BACK_BDB, ERR, 
272                                 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
273                                 dir, db_strerror(rc), rc );
274 #else
275                         Debug( LDAP_DEBUG_ANY,
276                                 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
277                                 dir, db_strerror(rc), rc );
278 #endif
279                         return rc;
280                 }
281         }
282 #endif
283
284 #ifdef NEW_LOGGING
285         LDAP_LOG( BACK_BDB, DETAIL1, 
286                 "bdb_db_open: dbenv_open %s\n", bdb->bi_dbenv_home, 0, 0 );
287 #else
288         Debug( LDAP_DEBUG_TRACE,
289                 "bdb_db_open: dbenv_open(%s)\n",
290                 bdb->bi_dbenv_home, 0, 0);
291 #endif
292
293 #ifdef HAVE_EBCDIC
294         strcpy( path, bdb->bi_dbenv_home );
295         __atoe( path );
296         rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
297                 path,
298                 flags,
299                 bdb->bi_dbenv_mode );
300 #else
301         rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
302                 bdb->bi_dbenv_home,
303                 flags,
304                 bdb->bi_dbenv_mode );
305 #endif
306         if( rc != 0 ) {
307 #ifdef NEW_LOGGING
308                 LDAP_LOG( BACK_BDB, ERR, 
309                         "bdb_db_open: dbenv_open failed: %s (%d)\n", 
310                         db_strerror(rc), rc, 0 );
311 #else
312                 Debug( LDAP_DEBUG_ANY,
313                         "bdb_db_open: dbenv_open failed: %s (%d)\n",
314                         db_strerror(rc), rc, 0 );
315 #endif
316                 return rc;
317         }
318
319         if( bdb->bi_dbenv_xflags != 0 ) {
320                 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
321                         bdb->bi_dbenv_xflags, 1);
322                 if( rc != 0 ) {
323 #ifdef NEW_LOGGING
324                         LDAP_LOG( BACK_BDB, ERR, 
325                                 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n", 
326                                 db_strerror(rc), rc, 0 );
327 #else
328                         Debug( LDAP_DEBUG_ANY,
329                                 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
330                                 db_strerror(rc), rc, 0 );
331 #endif
332                         return rc;
333                 }
334         }
335
336         flags = DB_THREAD | bdb->bi_db_opflags;
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 #ifdef NEW_LOGGING
350                         LDAP_LOG( BACK_BDB, ERR, 
351                                 "bdb_db_open: db_create(%s) failed: %s (%d)\n", 
352                                 bdb->bi_dbenv_home, db_strerror(rc), rc );
353 #else
354                         Debug( LDAP_DEBUG_ANY,
355                                 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
356                                 bdb->bi_dbenv_home, db_strerror(rc), rc );
357 #endif
358                         return rc;
359                 }
360
361                 if( i == BDB_ID2ENTRY ) {
362                         rc = db->bdi_db->set_bt_compare( db->bdi_db,
363                                 bdb_bt_compare );
364                         rc = db->bdi_db->set_pagesize( db->bdi_db,
365                                 BDB_ID2ENTRY_PAGESIZE );
366                         if ( slapMode & SLAP_TOOL_READMAIN ) {
367                                 flags |= DB_RDONLY;
368                         } else {
369                                 flags |= DB_CREATE;
370                         }
371                 } else {
372                         rc = db->bdi_db->set_flags( db->bdi_db, 
373                                 DB_DUP | DB_DUPSORT );
374 #ifndef BDB_HIER
375                         rc = db->bdi_db->set_dup_compare( db->bdi_db,
376                                 bdb_bt_compare );
377                         if ( slapMode & SLAP_TOOL_READONLY ) {
378                                 flags |= DB_RDONLY;
379                         } else {
380                                 flags |= DB_CREATE;
381                         }
382 #else
383                         rc = db->bdi_db->set_dup_compare( db->bdi_db,
384                                 bdb_dup_compare );
385                         rc = db->bdi_db->set_bt_compare( db->bdi_db,
386                                 bdb_bt_compare );
387                         if ( slapMode & (SLAP_TOOL_READONLY|SLAP_TOOL_READMAIN) ) {
388                                 flags |= DB_RDONLY;
389                         } else {
390                                 flags |= DB_CREATE;
391                         }
392 #endif
393                         rc = db->bdi_db->set_pagesize( db->bdi_db,
394                                 BDB_PAGESIZE );
395                 }
396
397 #ifdef HAVE_EBCDIC
398                 strcpy( path, bdbi_databases[i].file );
399                 __atoe( path );
400                 rc = DB_OPEN( db->bdi_db,
401                         path,
402                 /*      bdbi_databases[i].name, */ NULL,
403                         bdbi_databases[i].type,
404                         bdbi_databases[i].flags | flags,
405                         bdb->bi_dbenv_mode );
406 #else
407                 rc = DB_OPEN( db->bdi_db,
408                         bdbi_databases[i].file,
409                 /*      bdbi_databases[i].name, */ NULL,
410                         bdbi_databases[i].type,
411                         bdbi_databases[i].flags | flags,
412                         bdb->bi_dbenv_mode );
413 #endif
414
415                 if( rc != 0 ) {
416 #ifdef NEW_LOGGING
417                         LDAP_LOG( BACK_BDB, ERR, 
418                                 "bdb_db_open: db_create(%s) failed: %s (%d)\n", 
419                                 bdb->bi_dbenv_home, db_strerror(rc), rc );
420 #else
421                         Debug( LDAP_DEBUG_ANY,
422                                 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
423                                 bdb->bi_dbenv_home, db_strerror(rc), rc );
424 #endif
425                         return rc;
426                 }
427
428                 flags &= ~(DB_CREATE | DB_RDONLY);
429                 db->bdi_name = bdbi_databases[i].name;
430                 bdb->bi_databases[i] = db;
431         }
432
433         bdb->bi_databases[i] = NULL;
434         bdb->bi_ndatabases = i;
435
436         /* get nextid */
437         rc = bdb_last_id( be, NULL );
438         if( rc != 0 ) {
439 #ifdef NEW_LOGGING
440                         LDAP_LOG( BACK_BDB, ERR, 
441                                 "bdb_db_open: last_id(%s) failed: %s (%d)\n", 
442                                 bdb->bi_dbenv_home, db_strerror(rc), rc );
443 #else
444                 Debug( LDAP_DEBUG_ANY,
445                         "bdb_db_open: last_id(%s) failed: %s (%d)\n",
446                         bdb->bi_dbenv_home, db_strerror(rc), rc );
447 #endif
448                 return rc;
449         }
450
451         XLOCK_ID(bdb->bi_dbenv, &bdb->bi_cache.c_locker);
452
453         /* <insert> open (and create) index databases */
454         return 0;
455 }
456
457 static int
458 bdb_db_close( BackendDB *be )
459 {
460         int rc;
461         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
462         struct bdb_db_info *db;
463         bdb_idl_cache_entry_t *entry, *next_entry;
464
465         while( bdb->bi_ndatabases-- ) {
466                 db = bdb->bi_databases[bdb->bi_ndatabases];
467                 rc = db->bdi_db->close( db->bdi_db, 0 );
468                 /* Lower numbered names are not strdup'd */
469                 if( bdb->bi_ndatabases >= BDB_NDB )
470                         free( db->bdi_name );
471                 free( db );
472         }
473         free( bdb->bi_databases );
474         bdb_attr_index_destroy( bdb->bi_attrs );
475
476         bdb_cache_release_all (&bdb->bi_cache);
477
478         if ( bdb->bi_idl_cache_max_size ) {
479                 ldap_pvt_thread_rdwr_wlock ( &bdb->bi_idl_tree_rwlock );
480                 avl_free( bdb->bi_idl_tree, NULL );
481                 entry = bdb->bi_idl_lru_head;
482                 while ( entry != NULL ) {
483                         next_entry = entry->idl_lru_next;
484                         if ( entry->idl )
485                                 free( entry->idl );
486                         free( entry->kstr.bv_val );
487                         free( entry );
488                         entry = next_entry;
489                 }
490                 ldap_pvt_thread_rdwr_wunlock ( &bdb->bi_idl_tree_rwlock );
491         }
492
493         XLOCK_ID_FREE(bdb->bi_dbenv, bdb->bi_cache.c_locker);
494
495         return 0;
496 }
497
498 static int
499 bdb_db_destroy( BackendDB *be )
500 {
501         int rc;
502         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
503         Operation *ps = NULL;
504         Operation *psn = NULL;
505         void *saved_tmpmemctx = NULL;
506
507         /* close db environment */
508         if( bdb->bi_dbenv ) {
509                 /* force a checkpoint */
510                 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
511                 if( rc != 0 ) {
512 #ifdef NEW_LOGGING
513                         LDAP_LOG( BACK_BDB, ERR, 
514                                 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
515                                 db_strerror(rc), rc, 0 );
516 #else
517                         Debug( LDAP_DEBUG_ANY,
518                                 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
519                                 db_strerror(rc), rc, 0 );
520 #endif
521                 }
522
523                 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
524                 bdb->bi_dbenv = NULL;
525                 if( rc != 0 ) {
526 #ifdef NEW_LOGGING
527                         LDAP_LOG( BACK_BDB, ERR, 
528                                 "bdb_db_destroy: close failed: %s (%d)\n", 
529                                 db_strerror(rc), rc, 0 );
530 #else
531                         Debug( LDAP_DEBUG_ANY,
532                                 "bdb_db_destroy: close failed: %s (%d)\n",
533                                 db_strerror(rc), rc, 0 );
534 #endif
535                         return rc;
536                 }
537         }
538
539         if( bdb->bi_dbenv_home ) ch_free( bdb->bi_dbenv_home );
540
541         ldap_pvt_thread_rdwr_destroy ( &bdb->bi_cache.c_rwlock );
542         ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.lru_mutex );
543         ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_dntree.bei_kids_mutex );
544         ldap_pvt_thread_rdwr_destroy ( &bdb->bi_pslist_rwlock );
545         ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
546         ldap_pvt_thread_mutex_destroy( &bdb->bi_database_mutex );
547         if ( bdb->bi_idl_cache_max_size ) {
548                 ldap_pvt_thread_rdwr_destroy( &bdb->bi_idl_tree_rwlock );
549                 ldap_pvt_thread_mutex_destroy( &bdb->bi_idl_tree_lrulock );
550         }
551
552         ps = LDAP_LIST_FIRST( &bdb->bi_psearch_list );
553
554         if ( ps ) {
555                 psn = LDAP_LIST_NEXT( ps, o_ps_link );
556                 if ( ps->o_savmemctx ) {
557                         ps->o_tmpmemctx = ps->o_savmemctx;
558                         ps->o_tmpmfuncs = &sl_mfuncs;
559                         ber_set_option(ps->o_ber, LBER_OPT_BER_MEMCTX, &ps->o_savmemctx);
560                 }
561                 saved_tmpmemctx = ps->o_tmpmemctx;
562
563                 if (!BER_BVISNULL(&ps->o_req_dn)) {
564                         slap_sl_free( ps->o_req_dn.bv_val, ps->o_tmpmemctx);
565                 }
566                 if (!BER_BVISNULL(&ps->o_req_ndn)) {
567                         slap_sl_free( ps->o_req_ndn.bv_val, ps->o_tmpmemctx);
568                 }
569                 if (!BER_BVISNULL(&ps->ors_filterstr)) {
570                         slap_sl_free( ps->ors_filterstr.bv_val, ps->o_tmpmemctx);
571                 }
572                 if (ps->ors_filter != NULL) {
573                         filter_free_x( ps, ps->ors_filter );
574                 }
575                 if (ps->ors_attrs != NULL) {
576                         ps->o_tmpfree(ps->ors_attrs, ps->o_tmpmemctx);
577                 }
578
579                 slap_op_free( ps );
580
581                 if ( saved_tmpmemctx ) {
582                         sl_mem_destroy( NULL, saved_tmpmemctx );
583                 }
584         }
585
586         while ( psn ) {
587                 ps = psn;
588                 psn = LDAP_LIST_NEXT( ps, o_ps_link );
589
590                 if ( ps->o_savmemctx ) {
591                         ps->o_tmpmemctx = ps->o_savmemctx;
592                         ps->o_tmpmfuncs = &sl_mfuncs;
593                         ber_set_option(ps->o_ber, LBER_OPT_BER_MEMCTX, &ps->o_savmemctx);
594                 }
595                 saved_tmpmemctx = ps->o_tmpmemctx;
596
597                 if (!BER_BVISNULL(&ps->o_req_dn)) {
598                         slap_sl_free( ps->o_req_dn.bv_val, ps->o_tmpmemctx);
599                 }
600                 if (!BER_BVISNULL(&ps->o_req_ndn)) {
601                         slap_sl_free( ps->o_req_ndn.bv_val, ps->o_tmpmemctx);
602                 }
603                 if (!BER_BVISNULL(&ps->ors_filterstr)) {
604                         slap_sl_free( ps->ors_filterstr.bv_val, ps->o_tmpmemctx);
605                 }
606                 if (ps->ors_filter != NULL) {
607                         filter_free_x( ps, ps->ors_filter );
608                 }
609                 if (ps->ors_attrs != NULL) {
610                         ps->o_tmpfree(ps->ors_attrs, ps->o_tmpmemctx);
611                 }
612
613                 slap_op_free( ps );
614
615                 if ( saved_tmpmemctx ) {
616                         sl_mem_destroy( NULL, saved_tmpmemctx );
617                 }
618         }
619
620         ch_free( bdb );
621         be->be_private = NULL;
622
623         return 0;
624 }
625
626 #if     (SLAPD_BDB == SLAPD_MOD_DYNAMIC && !defined(BDB_HIER)) || \
627         (SLAPD_HDB == SLAPD_MOD_DYNAMIC && defined(BDB_HIER))
628 int init_module( int argc, char *argv[] ) {
629         BackendInfo bi;
630
631         memset( &bi, '\0', sizeof(bi) );
632 #ifdef BDB_HIER
633         bi.bi_type = "hdb";
634 #else
635         bi.bi_type = "bdb";
636 #endif
637         bi.bi_init = bdb_initialize;
638
639         backend_add( &bi );
640         return 0;
641 }
642 #endif /* SLAPD_BDB */
643
644 int
645 bdb_initialize(
646         BackendInfo     *bi )
647 {
648         static char *controls[] = {
649                 LDAP_CONTROL_ASSERT,
650                 LDAP_CONTROL_MANAGEDSAIT,
651                 LDAP_CONTROL_NOOP,
652                 LDAP_CONTROL_PAGEDRESULTS,
653 #ifdef LDAP_CONTROL_SUBENTRIES
654                 LDAP_CONTROL_SUBENTRIES,
655 #endif
656                 LDAP_CONTROL_VALUESRETURNFILTER,
657                 NULL
658         };
659
660         /* initialize the underlying database system */
661 #ifdef NEW_LOGGING
662         LDAP_LOG( BACK_BDB, ENTRY, "bdb_db_initialize\n", 0, 0, 0 );
663 #else
664         Debug( LDAP_DEBUG_TRACE, "bdb_initialize: initialize BDB backend\n",
665                 0, 0, 0 );
666 #endif
667
668         bi->bi_flags |=
669                 SLAP_BFLAG_INCREMENT |
670 #ifdef BDB_SUBENTRIES
671                 SLAP_BFLAG_SUBENTRIES |
672 #endif
673 #ifdef BDB_ALIASES
674                 SLAP_BFLAG_ALIASES |
675 #endif
676                 SLAP_BFLAG_REFERRALS;
677
678         bi->bi_controls = controls;
679
680         {       /* version check */
681                 int major, minor, patch;
682                 char *version = db_version( &major, &minor, &patch );
683 #ifdef HAVE_EBCDIC
684                 char v2[1024];
685
686                 /* All our stdio does an ASCII to EBCDIC conversion on
687                  * the output. Strings from the BDB library are already
688                  * in EBCDIC; we have to go back and forth...
689                  */
690                 strcpy( v2, version );
691                 __etoa( v2 );
692                 version = v2;
693 #endif
694
695                 if( major != DB_VERSION_MAJOR ||
696                         minor != DB_VERSION_MINOR ||
697                         patch < DB_VERSION_PATCH )
698                 {
699 #ifdef NEW_LOGGING
700                         LDAP_LOG( BACK_BDB, ERR, 
701                                 "bdb_initialize: BDB library version mismatch:"
702                                 " expected " DB_VERSION_STRING ","
703                                 " got %s\n", version, 0, 0 );
704 #else
705                         Debug( LDAP_DEBUG_ANY,
706                                 "bdb_initialize: BDB library version mismatch:"
707                                 " expected " DB_VERSION_STRING ","
708                                 " got %s\n", version, 0, 0 );
709 #endif
710                 }
711
712 #ifdef NEW_LOGGING
713                 LDAP_LOG( BACK_BDB, DETAIL1, 
714                         "bdb_db_initialize: %s\n", version, 0, 0 );
715 #else
716                 Debug( LDAP_DEBUG_ANY, "bdb_initialize: %s\n",
717                         version, 0, 0 );
718 #endif
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         {
732                 static char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
733
734                 bdb_uuid.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ));
735                 bdb_uuid.bv_val = uuidbuf;
736         }
737
738         bi->bi_open = 0;
739         bi->bi_close = 0;
740         bi->bi_config = 0;
741         bi->bi_destroy = 0;
742
743         bi->bi_db_init = bdb_db_init;
744         bi->bi_db_config = bdb_db_config;
745         bi->bi_db_open = bdb_db_open;
746         bi->bi_db_close = bdb_db_close;
747         bi->bi_db_destroy = bdb_db_destroy;
748
749         bi->bi_op_add = bdb_add;
750         bi->bi_op_bind = bdb_bind;
751         bi->bi_op_compare = bdb_compare;
752         bi->bi_op_delete = bdb_delete;
753         bi->bi_op_modify = bdb_modify;
754         bi->bi_op_modrdn = bdb_modrdn;
755         bi->bi_op_search = bdb_search;
756
757         bi->bi_op_unbind = 0;
758
759         bi->bi_op_abandon = bdb_abandon;
760         bi->bi_op_cancel = bdb_cancel;
761
762         bi->bi_extended = bdb_extended;
763
764         bi->bi_chk_referrals = bdb_referrals;
765         bi->bi_operational = bdb_operational;
766         bi->bi_has_subordinates = bdb_hasSubordinates;
767         bi->bi_entry_release_rw = bdb_entry_release;
768         bi->bi_entry_get_rw = bdb_entry_get;
769
770         /*
771          * hooks for slap tools
772          */
773         bi->bi_tool_entry_open = bdb_tool_entry_open;
774         bi->bi_tool_entry_close = bdb_tool_entry_close;
775         bi->bi_tool_entry_first = bdb_tool_entry_next;
776         bi->bi_tool_entry_next = bdb_tool_entry_next;
777         bi->bi_tool_entry_get = bdb_tool_entry_get;
778         bi->bi_tool_entry_put = bdb_tool_entry_put;
779         bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
780         bi->bi_tool_sync = 0;
781         bi->bi_tool_dn2id_get = bdb_tool_dn2id_get;
782         bi->bi_tool_id2entry_get = bdb_tool_id2entry_get;
783         bi->bi_tool_entry_modify = bdb_tool_entry_modify;
784
785         bi->bi_connection_init = 0;
786         bi->bi_connection_destroy = 0;
787
788         return 0;
789 }