]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/init.c
implement search timeout when the remote server does not respond in the specified...
[openldap] / servers / slapd / back-bdb / init.c
1 /* init.c - initialize bdb backend */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2000-2007 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20 #include <ac/string.h>
21 #include <ac/unistd.h>
22 #include <ac/stdlib.h>
23 #include <ac/errno.h>
24 #include <sys/stat.h>
25 #include "back-bdb.h"
26 #include <lutil.h>
27 #include <ldap_rq.h>
28 #include "alock.h"
29
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         int rc;
49
50         Debug( LDAP_DEBUG_TRACE,
51                 LDAP_XSTRING(bdb_db_init) ": Initializing " BDB_UCTYPE " database\n",
52                 0, 0, 0 );
53
54         /* allocate backend-database-specific stuff */
55         bdb = (struct bdb_info *) ch_calloc( 1, sizeof(struct bdb_info) );
56
57         /* DBEnv parameters */
58         bdb->bi_dbenv_home = ch_strdup( SLAPD_DEFAULT_DB_DIR );
59         bdb->bi_dbenv_xflags = 0;
60         bdb->bi_dbenv_mode = SLAPD_DEFAULT_DB_MODE;
61
62         bdb->bi_cache.c_maxsize = DEFAULT_CACHE_SIZE;
63         bdb->bi_cache.c_minfree = 1;
64
65         bdb->bi_lock_detect = DB_LOCK_DEFAULT;
66         bdb->bi_search_stack_depth = DEFAULT_SEARCH_STACK_DEPTH;
67         bdb->bi_search_stack = NULL;
68
69         ldap_pvt_thread_mutex_init( &bdb->bi_database_mutex );
70         ldap_pvt_thread_mutex_init( &bdb->bi_lastid_mutex );
71 #ifdef BDB_HIER
72         ldap_pvt_thread_mutex_init( &bdb->bi_modrdns_mutex );
73 #endif
74         ldap_pvt_thread_mutex_init( &bdb->bi_cache.c_lru_mutex );
75         ldap_pvt_thread_mutex_init( &bdb->bi_cache.c_count_mutex );
76         ldap_pvt_thread_mutex_init( &bdb->bi_cache.c_eifree_mutex );
77         ldap_pvt_thread_mutex_init( &bdb->bi_cache.c_dntree.bei_kids_mutex );
78         ldap_pvt_thread_rdwr_init ( &bdb->bi_cache.c_rwlock );
79         ldap_pvt_thread_rdwr_init( &bdb->bi_idl_tree_rwlock );
80         ldap_pvt_thread_mutex_init( &bdb->bi_idl_tree_lrulock );
81
82         be->be_private = bdb;
83         be->be_cf_ocs = be->bd_info->bi_cf_ocs;
84
85         rc = bdb_monitor_db_init( be );
86
87         return rc;
88 }
89
90 static int
91 bdb_db_close( BackendDB *be );
92
93 static int
94 bdb_db_open( BackendDB *be )
95 {
96         int rc, i;
97         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
98         struct stat stat1, stat2;
99         u_int32_t flags;
100         char path[MAXPATHLEN];
101         char *dbhome;
102         int do_recover = 0, do_alock_recover = 0;
103         int alockt, quick = 0;
104
105         if ( be->be_suffix == NULL ) {
106                 Debug( LDAP_DEBUG_ANY,
107                         "bdb_db_open: need suffix\n",
108                         0, 0, 0 );
109                 return -1;
110         }
111
112         Debug( LDAP_DEBUG_ARGS,
113                 "bdb_db_open: %s\n",
114                 be->be_suffix[0].bv_val, 0, 0 );
115
116 #ifndef BDB_MULTIPLE_SUFFIXES
117         if ( be->be_suffix[1].bv_val ) {
118         Debug( LDAP_DEBUG_ANY,
119                 "bdb_db_open: only one suffix allowed\n", 0, 0, 0 );
120                 return -1;
121         }
122 #endif
123
124         /* Check existence of dbenv_home. Any error means trouble */
125         rc = stat( bdb->bi_dbenv_home, &stat1 );
126         if( rc !=0 ) {
127                 Debug( LDAP_DEBUG_ANY,
128                         "bdb_db_open: Cannot access database directory %s (%d)\n",
129                         bdb->bi_dbenv_home, errno, 0 );
130                         return -1;
131         }
132
133         /* Perform database use arbitration/recovery logic */
134         alockt = (slapMode & SLAP_TOOL_READONLY) ? ALOCK_LOCKED : ALOCK_UNIQUE;
135         if ( slapMode & SLAP_TOOL_QUICK ) {
136                 alockt |= ALOCK_NOSAVE;
137                 quick = 1;
138         }
139
140         rc = alock_open( &bdb->bi_alock_info, 
141                                 "slapd", 
142                                 bdb->bi_dbenv_home, alockt );
143
144         /* alockt is TRUE if the existing environment was created in Quick mode */
145         alockt = (rc & ALOCK_NOSAVE) ? 1 : 0;
146         rc &= ~ALOCK_NOSAVE;
147
148         if( rc == ALOCK_RECOVER ) {
149                 Debug( LDAP_DEBUG_ANY,
150                         "bdb_db_open: unclean shutdown detected;"
151                         " attempting recovery.\n", 
152                         0, 0, 0 );
153                 do_alock_recover = 1;
154                 do_recover = DB_RECOVER;
155         } else if( rc == ALOCK_BUSY ) {
156                 Debug( LDAP_DEBUG_ANY,
157                         "bdb_db_open: database already in use\n", 
158                         0, 0, 0 );
159                 return -1;
160         } else if( rc != ALOCK_CLEAN ) {
161                 Debug( LDAP_DEBUG_ANY,
162                         "bdb_db_open: alock package is unstable\n", 
163                         0, 0, 0 );
164                 return -1;
165         }
166
167         /*
168          * The DB_CONFIG file may have changed. If so, recover the
169          * database so that new settings are put into effect. Also
170          * note the possible absence of DB_CONFIG in the log.
171          */
172         if( stat( bdb->bi_db_config_path, &stat1 ) == 0 ) {
173                 if ( !do_recover ) {
174                         char *ptr = lutil_strcopy(path, bdb->bi_dbenv_home);
175                         *ptr++ = LDAP_DIRSEP[0];
176                         strcpy( ptr, "__db.001" );
177                         if( stat( path, &stat2 ) == 0 ) {
178                                 if( stat2.st_mtime < stat1.st_mtime ) {
179                                         Debug( LDAP_DEBUG_ANY,
180                                                 "bdb_db_open: DB_CONFIG for suffix %s has changed.\n"
181                                                 "Performing database recovery to activate new settings.\n",
182                                                 be->be_suffix[0].bv_val, 0, 0 );
183                                         do_recover = DB_RECOVER;
184                                 }
185                         }
186                 }
187         }
188         else {
189                 Debug( LDAP_DEBUG_ANY,
190                         "bdb_db_open: Warning - No DB_CONFIG file found "
191                         "in directory %s: (%d)\n"
192                         "Expect poor performance for suffix %s.\n",
193                         bdb->bi_dbenv_home, errno, be->be_suffix[0].bv_val );
194         }
195
196         /* Always let slapcat run, regardless of environment state.
197          * This can be used to cause a cache flush after an unclean
198          * shutdown.
199          */
200         if ( do_recover && ( slapMode & SLAP_TOOL_READONLY )) {
201                 Debug( LDAP_DEBUG_ANY,
202                         "bdb_db_open: Recovery skipped in read-only mode. "
203                         "Run manual recovery if errors are encountered.\n",
204                         0, 0, 0 );
205                 do_recover = 0;
206                 quick = alockt;
207         }
208
209         /* An existing environment in Quick mode has nothing to recover. */
210         if ( alockt && do_recover ) {
211                 Debug( LDAP_DEBUG_ANY,
212                         "bdb_db_open: cannot recover, database must be reinitialized.\n", 
213                         0, 0, 0 );
214                 rc = -1;
215                 goto fail;
216         }
217
218         rc = db_env_create( &bdb->bi_dbenv, 0 );
219         if( rc != 0 ) {
220                 Debug( LDAP_DEBUG_ANY,
221                         "bdb_db_open: db_env_create failed: %s (%d)\n",
222                         db_strerror(rc), rc, 0 );
223                 goto fail;
224         }
225
226 #ifdef HAVE_EBCDIC
227         strcpy( path, bdb->bi_dbenv_home );
228         __atoe( path );
229         dbhome = path;
230 #else
231         dbhome = bdb->bi_dbenv_home;
232 #endif
233
234         /* If existing environment is clean but doesn't support
235          * currently requested modes, remove it.
236          */
237         if ( !do_recover && ( alockt ^ quick )) {
238                 rc = bdb->bi_dbenv->remove( bdb->bi_dbenv, dbhome, DB_FORCE );
239                 if ( rc ) {
240                         Debug( LDAP_DEBUG_ANY,
241                                 "bdb_db_open: dbenv remove failed: %s (%d)\n",
242                                 db_strerror(rc), rc, 0 );
243                         bdb->bi_dbenv = NULL;
244                         goto fail;
245                 }
246                 rc = db_env_create( &bdb->bi_dbenv, 0 );
247                 if( rc != 0 ) {
248                         Debug( LDAP_DEBUG_ANY,
249                                 "bdb_db_open: db_env_create failed: %s (%d)\n",
250                                 db_strerror(rc), rc, 0 );
251                         goto fail;
252                 }
253         }
254
255         bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0].bv_val );
256         bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
257
258         bdb->bi_dbenv->set_lk_detect( bdb->bi_dbenv, bdb->bi_lock_detect );
259
260         /* One long-lived TXN per thread, two TXNs per write op */
261         bdb->bi_dbenv->set_tx_max( bdb->bi_dbenv, connection_pool_max * 3 );
262
263         if( bdb->bi_dbenv_xflags != 0 ) {
264                 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
265                         bdb->bi_dbenv_xflags, 1);
266                 if( rc != 0 ) {
267                         Debug( LDAP_DEBUG_ANY,
268                                 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
269                                 db_strerror(rc), rc, 0 );
270                         goto fail;
271                 }
272         }
273
274 #define BDB_TXN_FLAGS   (DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN)
275
276         Debug( LDAP_DEBUG_TRACE,
277                 "bdb_db_open: dbenv_open(%s)\n",
278                 bdb->bi_dbenv_home, 0, 0);
279
280         flags = DB_INIT_MPOOL | DB_CREATE | DB_THREAD;
281
282         if ( !quick )
283                 flags |= BDB_TXN_FLAGS;
284
285         /* If a key was set, use shared memory for the BDB environment */
286         if ( bdb->bi_shm_key ) {
287                 bdb->bi_dbenv->set_shm_key( bdb->bi_dbenv, bdb->bi_shm_key );
288                 flags |= DB_SYSTEM_MEM;
289         }
290         rc = bdb->bi_dbenv->open( bdb->bi_dbenv, dbhome,
291                         flags | do_recover, bdb->bi_dbenv_mode );
292
293         if ( rc ) {
294                 Debug( LDAP_DEBUG_ANY,
295                         "bdb_db_open: Database cannot be %s, err %d. "
296                         "Restore from backup!\n",
297                                 do_recover ? "recovered" : "opened", rc, 0);
298                 goto fail;
299         }
300
301         if ( do_alock_recover && alock_recover (&bdb->bi_alock_info) != 0 ) {
302                 Debug( LDAP_DEBUG_ANY,
303                         "bdb_db_open: alock_recover failed\n",
304                         0, 0, 0 );
305                 rc = -1;
306                 goto fail;
307         }
308
309 #ifdef SLAP_ZONE_ALLOC
310         if ( bdb->bi_cache.c_maxsize ) {
311                 bdb->bi_cache.c_zctx = slap_zn_mem_create(
312                         SLAP_ZONE_INITSIZE, SLAP_ZONE_MAXSIZE,
313                         SLAP_ZONE_DELTA, SLAP_ZONE_SIZE);
314         }
315 #endif
316
317         if ( bdb->bi_idl_cache_max_size ) {
318                 bdb->bi_idl_tree = NULL;
319                 bdb->bi_idl_cache_size = 0;
320         }
321
322         flags = DB_THREAD | bdb->bi_db_opflags;
323
324 #ifdef DB_AUTO_COMMIT
325         if ( !quick )
326                 flags |= DB_AUTO_COMMIT;
327 #endif
328
329         bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
330                 BDB_INDICES * sizeof(struct bdb_db_info *) );
331
332         /* open (and create) main database */
333         for( i = 0; bdbi_databases[i].name; i++ ) {
334                 struct bdb_db_info *db;
335
336                 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
337
338                 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
339                 if( rc != 0 ) {
340                         Debug( LDAP_DEBUG_ANY,
341                                 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
342                                 bdb->bi_dbenv_home, db_strerror(rc), rc );
343                         goto fail;
344                 }
345
346                 if( i == BDB_ID2ENTRY ) {
347                         if ( slapMode & SLAP_TOOL_MODE )
348                                 db->bdi_db->mpf->set_priority( db->bdi_db->mpf,
349                                         DB_PRIORITY_VERY_LOW );
350
351                         rc = db->bdi_db->set_pagesize( db->bdi_db,
352                                 BDB_ID2ENTRY_PAGESIZE );
353                         if ( slapMode & SLAP_TOOL_READMAIN ) {
354                                 flags |= DB_RDONLY;
355                         } else {
356                                 flags |= DB_CREATE;
357                         }
358                 } else {
359                         rc = db->bdi_db->set_flags( db->bdi_db, 
360                                 DB_DUP | DB_DUPSORT );
361 #ifndef BDB_HIER
362                         if ( slapMode & SLAP_TOOL_READONLY ) {
363                                 flags |= DB_RDONLY;
364                         } else {
365                                 flags |= DB_CREATE;
366                         }
367 #else
368                         if ( slapMode & (SLAP_TOOL_READONLY|SLAP_TOOL_READMAIN) ) {
369                                 flags |= DB_RDONLY;
370                         } else {
371                                 flags |= DB_CREATE;
372                         }
373 #endif
374                         rc = db->bdi_db->set_pagesize( db->bdi_db,
375                                 BDB_PAGESIZE );
376                 }
377
378 #ifdef HAVE_EBCDIC
379                 strcpy( path, bdbi_databases[i].file );
380                 __atoe( path );
381                 rc = DB_OPEN( db->bdi_db,
382                         path,
383                 /*      bdbi_databases[i].name, */ NULL,
384                         bdbi_databases[i].type,
385                         bdbi_databases[i].flags | flags,
386                         bdb->bi_dbenv_mode );
387 #else
388                 rc = DB_OPEN( db->bdi_db,
389                         bdbi_databases[i].file,
390                 /*      bdbi_databases[i].name, */ NULL,
391                         bdbi_databases[i].type,
392                         bdbi_databases[i].flags | flags,
393                         bdb->bi_dbenv_mode );
394 #endif
395
396                 if ( rc != 0 ) {
397                         char    buf[SLAP_TEXT_BUFLEN];
398
399                         snprintf( buf, sizeof(buf), "%s/%s", 
400                                 bdb->bi_dbenv_home, bdbi_databases[i].file );
401                         Debug( LDAP_DEBUG_ANY,
402                                 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
403                                 buf, db_strerror(rc), rc );
404                         db->bdi_db->close( db->bdi_db, 0 );
405                         goto fail;
406                 }
407
408                 flags &= ~(DB_CREATE | DB_RDONLY);
409                 db->bdi_name = bdbi_databases[i].name;
410                 bdb->bi_databases[i] = db;
411         }
412
413         bdb->bi_databases[i] = NULL;
414         bdb->bi_ndatabases = i;
415
416         /* get nextid */
417         rc = bdb_last_id( be, NULL );
418         if( rc != 0 ) {
419                 Debug( LDAP_DEBUG_ANY,
420                         "bdb_db_open: last_id(%s) failed: %s (%d)\n",
421                         bdb->bi_dbenv_home, db_strerror(rc), rc );
422                 goto fail;
423         }
424
425         if ( !quick ) {
426                 XLOCK_ID(bdb->bi_dbenv, &bdb->bi_cache.c_locker);
427         }
428
429         /* monitor setup */
430         rc = bdb_monitor_db_open( be );
431         if ( rc != 0 ) {
432                 goto fail;
433         }
434
435         bdb->bi_flags |= BDB_IS_OPEN;
436
437         entry_prealloc( bdb->bi_cache.c_maxsize );
438         attr_prealloc( bdb->bi_cache.c_maxsize * 20 );
439         return 0;
440
441 fail:
442         bdb_db_close( be );
443         return rc;
444 }
445
446 static int
447 bdb_db_close( BackendDB *be )
448 {
449         int rc;
450         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
451         struct bdb_db_info *db;
452         bdb_idl_cache_entry_t *entry, *next_entry;
453
454         /* monitor handling */
455         (void)bdb_monitor_db_close( be );
456
457         bdb->bi_flags &= ~BDB_IS_OPEN;
458
459         ber_bvarray_free( bdb->bi_db_config );
460         bdb->bi_db_config = NULL;
461
462         while( bdb->bi_databases && bdb->bi_ndatabases-- ) {
463                 db = bdb->bi_databases[bdb->bi_ndatabases];
464                 rc = db->bdi_db->close( db->bdi_db, 0 );
465                 /* Lower numbered names are not strdup'd */
466                 if( bdb->bi_ndatabases >= BDB_NDB )
467                         free( db->bdi_name );
468                 free( db );
469         }
470         free( bdb->bi_databases );
471         bdb->bi_databases = NULL;
472
473         bdb_cache_release_all (&bdb->bi_cache);
474
475         if ( bdb->bi_idl_cache_size ) {
476                 avl_free( bdb->bi_idl_tree, NULL );
477                 bdb->bi_idl_tree = NULL;
478                 entry = bdb->bi_idl_lru_head;
479                 do {
480                         next_entry = entry->idl_lru_next;
481                         if ( entry->idl )
482                                 free( entry->idl );
483                         free( entry->kstr.bv_val );
484                         free( entry );
485                         entry = next_entry;
486                 } while ( entry != bdb->bi_idl_lru_head );
487                 bdb->bi_idl_lru_head = bdb->bi_idl_lru_tail = NULL;
488         }
489
490         /* close db environment */
491         if( bdb->bi_dbenv ) {
492                 /* Free cache locker if we enabled locking */
493                 if ( !( slapMode & SLAP_TOOL_QUICK )) {
494                         XLOCK_ID_FREE(bdb->bi_dbenv, bdb->bi_cache.c_locker);
495                         bdb->bi_cache.c_locker = 0;
496                 }
497 #ifdef BDB_REUSE_LOCKERS
498                 bdb_locker_flush( bdb->bi_dbenv );
499 #endif
500                 /* force a checkpoint, but not if we were ReadOnly,
501                  * and not in Quick mode since there are no transactions there.
502                  */
503                 if ( !( slapMode & ( SLAP_TOOL_QUICK|SLAP_TOOL_READONLY ))) {
504                         rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
505                         if( rc != 0 ) {
506                                 Debug( LDAP_DEBUG_ANY,
507                                         "bdb_db_close: txn_checkpoint failed: %s (%d)\n",
508                                         db_strerror(rc), rc, 0 );
509                         }
510                 }
511
512                 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
513                 bdb->bi_dbenv = NULL;
514                 if( rc != 0 ) {
515                         Debug( LDAP_DEBUG_ANY,
516                                 "bdb_db_close: close failed: %s (%d)\n",
517                                 db_strerror(rc), rc, 0 );
518                         return rc;
519                 }
520         }
521
522         rc = alock_close( &bdb->bi_alock_info );
523         if( rc != 0 ) {
524                 Debug( LDAP_DEBUG_ANY,
525                         "bdb_db_close: alock_close failed\n", 0, 0, 0 );
526                 return -1;
527         }
528
529         return 0;
530 }
531
532 static int
533 bdb_db_destroy( BackendDB *be )
534 {
535         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
536
537         /* monitor handling */
538         (void)bdb_monitor_db_destroy( be );
539
540         if( bdb->bi_dbenv_home ) ch_free( bdb->bi_dbenv_home );
541         if( bdb->bi_db_config_path ) ch_free( bdb->bi_db_config_path );
542
543         bdb_attr_index_destroy( bdb );
544
545         ldap_pvt_thread_rdwr_destroy ( &bdb->bi_cache.c_rwlock );
546         ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_lru_mutex );
547         ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_count_mutex );
548         ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_eifree_mutex );
549         ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_dntree.bei_kids_mutex );
550 #ifdef BDB_HIER
551         ldap_pvt_thread_mutex_destroy( &bdb->bi_modrdns_mutex );
552 #endif
553         ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
554         ldap_pvt_thread_mutex_destroy( &bdb->bi_database_mutex );
555         ldap_pvt_thread_rdwr_destroy( &bdb->bi_idl_tree_rwlock );
556         ldap_pvt_thread_mutex_destroy( &bdb->bi_idl_tree_lrulock );
557
558         ch_free( bdb );
559         be->be_private = NULL;
560
561         return 0;
562 }
563
564 int
565 bdb_back_initialize(
566         BackendInfo     *bi )
567 {
568         int rc;
569
570         static char *controls[] = {
571                 LDAP_CONTROL_ASSERT,
572                 LDAP_CONTROL_MANAGEDSAIT,
573                 LDAP_CONTROL_NOOP,
574                 LDAP_CONTROL_PAGEDRESULTS,
575                 LDAP_CONTROL_PRE_READ,
576                 LDAP_CONTROL_POST_READ,
577                 LDAP_CONTROL_SUBENTRIES,
578                 LDAP_CONTROL_X_PERMISSIVE_MODIFY,
579 #ifdef LDAP_X_TXN
580                 LDAP_CONTROL_X_TXN_SPEC,
581 #endif
582                 NULL
583         };
584
585         /* initialize the underlying database system */
586         Debug( LDAP_DEBUG_TRACE,
587                 LDAP_XSTRING(bdb_back_initialize) ": initialize " 
588                 BDB_UCTYPE " backend\n", 0, 0, 0 );
589
590         bi->bi_flags |=
591                 SLAP_BFLAG_INCREMENT |
592                 SLAP_BFLAG_SUBENTRIES |
593                 SLAP_BFLAG_ALIASES |
594                 SLAP_BFLAG_REFERRALS;
595
596         bi->bi_controls = controls;
597
598         {       /* version check */
599                 int major, minor, patch, ver;
600                 char *version = db_version( &major, &minor, &patch );
601 #ifdef HAVE_EBCDIC
602                 char v2[1024];
603
604                 /* All our stdio does an ASCII to EBCDIC conversion on
605                  * the output. Strings from the BDB library are already
606                  * in EBCDIC; we have to go back and forth...
607                  */
608                 strcpy( v2, version );
609                 __etoa( v2 );
610                 version = v2;
611 #endif
612
613                 ver = (major << 24) | (minor << 16) | patch;
614                 if( ver != DB_VERSION_FULL ) {
615                         /* fail if a versions don't match */
616                         Debug( LDAP_DEBUG_ANY,
617                                 LDAP_XSTRING(bdb_back_initialize) ": "
618                                 "BDB library version mismatch:"
619                                 " expected " DB_VERSION_STRING ","
620                                 " got %s\n", version, 0, 0 );
621                         return -1;
622                 }
623
624                 Debug( LDAP_DEBUG_TRACE, LDAP_XSTRING(bdb_back_initialize)
625                         ": %s\n", version, 0, 0 );
626         }
627
628         db_env_set_func_free( ber_memfree );
629         db_env_set_func_malloc( (db_malloc *)ber_memalloc );
630         db_env_set_func_realloc( (db_realloc *)ber_memrealloc );
631 #ifndef NO_THREAD
632         /* This is a no-op on a NO_THREAD build. Leave the default
633          * alone so that BDB will sleep on interprocess conflicts.
634          */
635         db_env_set_func_yield( ldap_pvt_thread_yield );
636 #endif
637
638         bi->bi_open = 0;
639         bi->bi_close = 0;
640         bi->bi_config = 0;
641         bi->bi_destroy = 0;
642
643         bi->bi_db_init = bdb_db_init;
644         bi->bi_db_config = config_generic_wrapper;
645         bi->bi_db_open = bdb_db_open;
646         bi->bi_db_close = bdb_db_close;
647         bi->bi_db_destroy = bdb_db_destroy;
648
649         bi->bi_op_add = bdb_add;
650         bi->bi_op_bind = bdb_bind;
651         bi->bi_op_compare = bdb_compare;
652         bi->bi_op_delete = bdb_delete;
653         bi->bi_op_modify = bdb_modify;
654         bi->bi_op_modrdn = bdb_modrdn;
655         bi->bi_op_search = bdb_search;
656
657         bi->bi_op_unbind = 0;
658
659         bi->bi_extended = bdb_extended;
660
661         bi->bi_chk_referrals = bdb_referrals;
662         bi->bi_operational = bdb_operational;
663         bi->bi_has_subordinates = bdb_hasSubordinates;
664         bi->bi_entry_release_rw = bdb_entry_release;
665         bi->bi_entry_get_rw = bdb_entry_get;
666
667         /*
668          * hooks for slap tools
669          */
670         bi->bi_tool_entry_open = bdb_tool_entry_open;
671         bi->bi_tool_entry_close = bdb_tool_entry_close;
672         bi->bi_tool_entry_first = bdb_tool_entry_next;
673         bi->bi_tool_entry_next = bdb_tool_entry_next;
674         bi->bi_tool_entry_get = bdb_tool_entry_get;
675         bi->bi_tool_entry_put = bdb_tool_entry_put;
676         bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
677         bi->bi_tool_sync = 0;
678         bi->bi_tool_dn2id_get = bdb_tool_dn2id_get;
679         bi->bi_tool_id2entry_get = bdb_tool_id2entry_get;
680         bi->bi_tool_entry_modify = bdb_tool_entry_modify;
681
682         bi->bi_connection_init = 0;
683         bi->bi_connection_destroy = 0;
684
685         rc = bdb_back_init_cf( bi );
686
687         return rc;
688 }
689
690 #if     (SLAPD_BDB == SLAPD_MOD_DYNAMIC && !defined(BDB_HIER)) || \
691         (SLAPD_HDB == SLAPD_MOD_DYNAMIC && defined(BDB_HIER))
692
693 /* conditionally define the init_module() function */
694 #ifdef BDB_HIER
695 SLAP_BACKEND_INIT_MODULE( hdb )
696 #else /* !BDB_HIER */
697 SLAP_BACKEND_INIT_MODULE( bdb )
698 #endif /* !BDB_HIER */
699
700 #endif /* SLAPD_[BH]DB == SLAPD_MOD_DYNAMIC */
701