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