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