]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/init.c
Fix NO_THREADS issues
[openldap] / servers / slapd / back-bdb / init.c
1 /* init.c - initialize bdb backend */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11 #include <ac/string.h>
12 #include <ac/unistd.h>
13 #include <ac/stdlib.h>
14
15 #include <lutil.h>
16
17 #include "back-bdb.h"
18 #include "external.h"
19
20 static struct bdbi_database {
21         char *file;
22         char *name;
23         int type;
24         int flags;
25 } bdbi_databases[] = {
26         { "id2entry" BDB_SUFFIX, "id2entry", DB_BTREE, 0 },
27 #ifdef BDB_HIER
28         { "id2parent" BDB_SUFFIX, "id2parent", DB_BTREE, 0 },
29 #else
30         { "dn2id" BDB_SUFFIX, "dn2id", DB_BTREE, 0 },
31 #endif
32         { NULL, NULL, 0, 0 }
33 };
34
35 struct berval bdb_uuid = { 0, NULL };
36
37 typedef void * db_malloc(size_t);
38 typedef void * db_realloc(void *, size_t);
39
40 static int
41 bdb_open( BackendInfo *bi )
42 {
43         return 0;
44 }
45
46 #if 0
47 static int
48 bdb_destroy( BackendInfo *bi )
49 {
50         return 0;
51 }
52
53 static int
54 bdb_close( BackendInfo *bi )
55 {
56         /* terminate the underlying database system */
57         return 0;
58 }
59 #endif
60
61 static int
62 bdb_db_init( BackendDB *be )
63 {
64         struct bdb_info *bdb;
65
66 #ifdef NEW_LOGGING
67         LDAP_LOG( BACK_BDB, ENTRY, "bdb_db_init", 0, 0, 0 );
68 #else
69         Debug( LDAP_DEBUG_ANY,
70                 "bdb_db_init: Initializing BDB database\n",
71                 0, 0, 0 );
72 #endif
73
74         /* indicate system schema supported */
75         be->be_flags |=
76 #ifdef BDB_SUBENTRIES
77                 SLAP_BFLAG_SUBENTRIES |
78 #endif
79 #ifdef BDB_ALIASES
80                 SLAP_BFLAG_ALIASES |
81 #endif
82                 SLAP_BFLAG_REFERRALS;
83
84         /* allocate backend-database-specific stuff */
85         bdb = (struct bdb_info *) ch_calloc( 1, sizeof(struct bdb_info) );
86
87         /* DBEnv parameters */
88         bdb->bi_dbenv_home = ch_strdup( SLAPD_DEFAULT_DB_DIR );
89         bdb->bi_dbenv_xflags = 0;
90         bdb->bi_dbenv_mode = SLAPD_DEFAULT_DB_MODE;
91
92         bdb->bi_cache.c_maxsize = DEFAULT_CACHE_SIZE;
93
94         bdb->bi_lock_detect = DB_LOCK_DEFAULT;
95
96         ldap_pvt_thread_mutex_init( &bdb->bi_database_mutex );
97         ldap_pvt_thread_mutex_init( &bdb->bi_lastid_mutex );
98         ldap_pvt_thread_mutex_init( &bdb->bi_cache.lru_mutex );
99         ldap_pvt_thread_rdwr_init ( &bdb->bi_cache.c_rwlock );
100 #ifdef BDB_HIER
101         ldap_pvt_thread_rdwr_init( &bdb->bi_tree_rdwr );
102 #endif
103
104         be->be_private = bdb;
105         return 0;
106 }
107
108 int
109 bdb_bt_compare(
110         DB *db, 
111         const DBT *usrkey,
112         const DBT *curkey
113 )
114 {
115         unsigned char *u, *c;
116         int i;
117
118         u = usrkey->data;
119         c = curkey->data;
120
121 #ifdef WORDS_BIGENDIAN
122         for( i = 0; i < sizeof(ID); i++)
123 #else
124         for( i = sizeof(ID)-1; i >= 0; i--)
125 #endif
126         {
127                 if( u[i] - c[i] )
128                         return u[i] - c[i];
129         }
130         return 0;
131 }
132
133 static int
134 bdb_db_open( BackendDB *be )
135 {
136         int rc, i;
137         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
138         u_int32_t flags;
139 #ifdef HAVE_EBCDIC
140         char path[MAXPATHLEN];
141 #endif
142
143 #ifdef NEW_LOGGING
144         LDAP_LOG( BACK_BDB, ARGS, 
145                 "bdb_db_open: %s\n", be->be_suffix[0].bv_val, 0, 0 );
146 #else
147         Debug( LDAP_DEBUG_ARGS,
148                 "bdb_db_open: %s\n",
149                 be->be_suffix[0].bv_val, 0, 0 );
150 #endif
151
152         /* we should check existance of dbenv_home and db_directory */
153
154         rc = db_env_create( &bdb->bi_dbenv, 0 );
155         if( rc != 0 ) {
156 #ifdef NEW_LOGGING
157                 LDAP_LOG( BACK_BDB, ERR, 
158                         "bdb_db_open: db_env_create failed: %s (%d)\n", 
159                         db_strerror(rc), rc, 0 );
160 #else
161                 Debug( LDAP_DEBUG_ANY,
162                         "bdb_db_open: db_env_create failed: %s (%d)\n",
163                         db_strerror(rc), rc, 0 );
164 #endif
165                 return rc;
166         }
167
168         flags = DB_INIT_MPOOL | DB_THREAD | DB_CREATE
169                 | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN;
170         
171 #if 0
172         /* Never do automatic recovery, must perform it manually.
173          * Otherwise restarting with gentlehup will corrupt the
174          * database.
175          */
176         if( !(slapMode & SLAP_TOOL_MODE) ) flags |= DB_RECOVER;
177 #endif
178
179         bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0].bv_val );
180         bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
181         bdb->bi_dbenv->set_lk_detect( bdb->bi_dbenv, bdb->bi_lock_detect );
182
183 #ifdef BDB_SUBDIRS
184         {
185                 char dir[MAXPATHLEN];
186                 size_t len = strlen( bdb->bi_dbenv_home );
187
188                 strcpy( dir, bdb->bi_dbenv_home );
189                 strcat( &dir[len], BDB_TMP_SUBDIR );
190                 
191                 rc = bdb->bi_dbenv->set_tmp_dir( bdb->bi_dbenv, dir );
192                 if( rc != 0 ) {
193 #ifdef NEW_LOGGING
194                         LDAP_LOG( BACK_BDB, ERR, 
195                                 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n", 
196                                 dir, db_strerror(rc), rc );
197 #else
198                         Debug( LDAP_DEBUG_ANY,
199                                 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
200                                 dir, db_strerror(rc), rc );
201 #endif
202                         return rc;
203                 }
204
205                 strcat( &dir[len], BDB_LG_SUBDIR );
206
207                 rc = bdb->bi_dbenv->set_lg_dir( bdb->bi_dbenv, dir );
208                 if( rc != 0 ) {
209 #ifdef NEW_LOGGING
210                         LDAP_LOG( BACK_BDB, ERR, 
211                                 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n", 
212                                 dir, db_strerror(rc), rc );
213 #else
214                         Debug( LDAP_DEBUG_ANY,
215                                 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
216                                 dir, db_strerror(rc), rc );
217 #endif
218                         return rc;
219                 }
220
221                 strcat( &dir[len], BDB_DATA_SUBDIR );
222
223                 rc = bdb->bi_dbenv->set_data_dir( bdb->bi_dbenv, dir );
224                 if( rc != 0 ) {
225 #ifdef NEW_LOGGING
226                         LDAP_LOG( BACK_BDB, ERR, 
227                                 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
228                                 dir, db_strerror(rc), rc );
229 #else
230                         Debug( LDAP_DEBUG_ANY,
231                                 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
232                                 dir, db_strerror(rc), rc );
233 #endif
234                         return rc;
235                 }
236         }
237 #endif
238
239 #ifdef NEW_LOGGING
240         LDAP_LOG( BACK_BDB, DETAIL1, 
241                 "bdb_db_open: dbenv_open %s\n", bdb->bi_dbenv_home, 0, 0 );
242 #else
243         Debug( LDAP_DEBUG_TRACE,
244                 "bdb_db_open: dbenv_open(%s)\n",
245                 bdb->bi_dbenv_home, 0, 0);
246 #endif
247
248 #ifdef HAVE_EBCDIC
249         strcpy( path, bdb->bi_dbenv_home );
250         __atoe( path );
251         rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
252                 path,
253                 flags,
254                 bdb->bi_dbenv_mode );
255 #else
256         rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
257                 bdb->bi_dbenv_home,
258                 flags,
259                 bdb->bi_dbenv_mode );
260 #endif
261         if( rc != 0 ) {
262 #ifdef NEW_LOGGING
263                 LDAP_LOG( BACK_BDB, ERR, 
264                         "bdb_db_open: dbenv_open failed: %s (%d)\n", 
265                         db_strerror(rc), rc, 0 );
266 #else
267                 Debug( LDAP_DEBUG_ANY,
268                         "bdb_db_open: dbenv_open failed: %s (%d)\n",
269                         db_strerror(rc), rc, 0 );
270 #endif
271                 return rc;
272         }
273
274         if( bdb->bi_dbenv_xflags != 0 ) {
275                 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
276                         bdb->bi_dbenv_xflags, 1);
277                 if( rc != 0 ) {
278 #ifdef NEW_LOGGING
279                         LDAP_LOG( BACK_BDB, ERR, 
280                                 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n", 
281                                 db_strerror(rc), rc, 0 );
282 #else
283                         Debug( LDAP_DEBUG_ANY,
284                                 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
285                                 db_strerror(rc), rc, 0 );
286 #endif
287                         return rc;
288                 }
289         }
290
291         flags = DB_THREAD | DB_CREATE | bdb->bi_db_opflags;
292
293         bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
294                 BDB_INDICES * sizeof(struct bdb_db_info *) );
295
296         /* open (and create) main database */
297         for( i = 0; bdbi_databases[i].name; i++ ) {
298                 struct bdb_db_info *db;
299
300                 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
301
302                 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
303                 if( rc != 0 ) {
304 #ifdef NEW_LOGGING
305                         LDAP_LOG( BACK_BDB, ERR, 
306                                 "bdb_db_open: db_create(%s) failed: %s (%d)\n", 
307                                 bdb->bi_dbenv_home, db_strerror(rc), rc );
308 #else
309                         Debug( LDAP_DEBUG_ANY,
310                                 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
311                                 bdb->bi_dbenv_home, db_strerror(rc), rc );
312 #endif
313                         return rc;
314                 }
315
316                 if( i == BDB_ID2ENTRY ) {
317                         rc = db->bdi_db->set_bt_compare( db->bdi_db,
318                                 bdb_bt_compare );
319                         rc = db->bdi_db->set_pagesize( db->bdi_db,
320                                 BDB_ID2ENTRY_PAGESIZE );
321                 } else {
322 #ifdef BDB_HIER
323                         rc = db->bdi_db->set_bt_compare( db->bdi_db,
324                                 bdb_bt_compare );
325 #else
326                         rc = db->bdi_db->set_flags( db->bdi_db, 
327                                 DB_DUP | DB_DUPSORT );
328                         rc = db->bdi_db->set_dup_compare( db->bdi_db,
329                                 bdb_bt_compare );
330 #endif
331                         rc = db->bdi_db->set_pagesize( db->bdi_db,
332                                 BDB_PAGESIZE );
333                 }
334
335 #ifdef HAVE_EBCDIC
336                 strcpy( path, bdbi_databases[i].file );
337                 __atoe( path );
338                 rc = DB_OPEN( db->bdi_db, 
339                         path,
340                 /*      bdbi_databases[i].name, */ NULL,
341                         bdbi_databases[i].type,
342                         bdbi_databases[i].flags | flags,
343                         bdb->bi_dbenv_mode );
344 #else
345                 rc = DB_OPEN( db->bdi_db, 
346                         bdbi_databases[i].file,
347                 /*      bdbi_databases[i].name, */ NULL,
348                         bdbi_databases[i].type,
349                         bdbi_databases[i].flags | flags,
350                         bdb->bi_dbenv_mode );
351 #endif
352
353                 if( rc != 0 ) {
354 #ifdef NEW_LOGGING
355                         LDAP_LOG( BACK_BDB, ERR, 
356                                 "bdb_db_open: db_create(%s) failed: %s (%d)\n", 
357                                 bdb->bi_dbenv_home, db_strerror(rc), rc );
358 #else
359                         Debug( LDAP_DEBUG_ANY,
360                                 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
361                                 bdb->bi_dbenv_home, db_strerror(rc), rc );
362 #endif
363                         return rc;
364                 }
365
366                 db->bdi_name = bdbi_databases[i].name;
367                 bdb->bi_databases[i] = db;
368         }
369
370         bdb->bi_databases[i] = NULL;
371         bdb->bi_ndatabases = i;
372
373         /* get nextid */
374         rc = bdb_last_id( be, NULL );
375         if( rc != 0 ) {
376 #ifdef NEW_LOGGING
377                         LDAP_LOG( BACK_BDB, ERR, 
378                                 "bdb_db_open: last_id(%s) failed: %s (%d)\n", 
379                                 bdb->bi_dbenv_home, db_strerror(rc), rc );
380 #else
381                 Debug( LDAP_DEBUG_ANY,
382                         "bdb_db_open: last_id(%s) failed: %s (%d)\n",
383                         bdb->bi_dbenv_home, db_strerror(rc), rc );
384 #endif
385                 return rc;
386         }
387
388         /* <insert> open (and create) index databases */
389 #ifdef BDB_HIER
390         rc = bdb_build_tree( be );
391 #endif
392         return 0;
393 }
394
395 static int
396 bdb_db_close( BackendDB *be )
397 {
398         int rc;
399         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
400         struct bdb_db_info *db;
401
402         while( bdb->bi_ndatabases-- ) {
403                 db = bdb->bi_databases[bdb->bi_ndatabases];
404                 rc = db->bdi_db->close( db->bdi_db, 0 );
405                 /* Lower numbered names are not strdup'd */
406                 if( bdb->bi_ndatabases >= BDB_NDB )
407                         free( db->bdi_name );
408                 free( db );
409         }
410         free( bdb->bi_databases );
411         bdb_attr_index_destroy( bdb->bi_attrs );
412
413         bdb_cache_release_all (&bdb->bi_cache);
414
415         return 0;
416 }
417
418 static int
419 bdb_db_destroy( BackendDB *be )
420 {
421         int rc;
422         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
423
424         /* close db environment */
425         if( bdb->bi_dbenv ) {
426                 /* force a checkpoint */
427                 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
428                 if( rc != 0 ) {
429 #ifdef NEW_LOGGING
430                         LDAP_LOG( BACK_BDB, ERR, 
431                                 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
432                                 db_strerror(rc), rc, 0 );
433 #else
434                         Debug( LDAP_DEBUG_ANY,
435                                 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
436                                 db_strerror(rc), rc, 0 );
437 #endif
438                 }
439
440                 bdb_cache_release_all (&bdb->bi_cache);
441
442                 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
443                 bdb->bi_dbenv = NULL;
444                 if( rc != 0 ) {
445 #ifdef NEW_LOGGING
446                         LDAP_LOG( BACK_BDB, ERR, 
447                                 "bdb_db_destroy: close failed: %s (%d)\n", 
448                                 db_strerror(rc), rc, 0 );
449 #else
450                         Debug( LDAP_DEBUG_ANY,
451                                 "bdb_db_destroy: close failed: %s (%d)\n",
452                                 db_strerror(rc), rc, 0 );
453 #endif
454                         return rc;
455                 }
456         }
457
458         if( bdb->bi_dbenv_home ) ch_free( bdb->bi_dbenv_home );
459
460 #ifdef BDB_HIER
461         ldap_pvt_thread_rdwr_destroy( &bdb->bi_tree_rdwr );
462 #endif
463         ldap_pvt_thread_rdwr_destroy ( &bdb->bi_cache.c_rwlock );
464         ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.lru_mutex );
465         ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
466         ldap_pvt_thread_mutex_destroy( &bdb->bi_database_mutex );
467
468         ch_free( bdb );
469         be->be_private = NULL;
470
471         return 0;
472 }
473
474 #ifdef SLAPD_BDB_DYNAMIC
475 int back_bdb_LTX_init_module( int argc, char *argv[] ) {
476         BackendInfo bi;
477
478         memset( &bi, '\0', sizeof(bi) );
479         bi.bi_type = "bdb";
480         bi.bi_init = bdb_initialize;
481
482         backend_add( &bi );
483         return 0;
484 }
485 #endif /* SLAPD_BDB_DYNAMIC */
486
487 int
488 bdb_initialize(
489         BackendInfo     *bi
490 )
491 {
492         static char *controls[] = {
493                 LDAP_CONTROL_MANAGEDSAIT,
494 #ifdef LDAP_CONTROL_SUBENTRIES
495                 LDAP_CONTROL_SUBENTRIES,
496 #endif
497 #ifdef LDAP_CONTROL_NOOP
498                 LDAP_CONTROL_NOOP,
499 #endif
500 #ifdef LDAP_CONTROL_VALUESRETURNFILTER
501                 LDAP_CONTROL_VALUESRETURNFILTER,
502 #endif
503 #ifdef LDAP_CLIENT_UPDATE
504                 LDAP_CONTROL_CLIENT_UPDATE,
505 #endif
506                 NULL
507         };
508
509         bi->bi_controls = controls;
510
511         /* initialize the underlying database system */
512 #ifdef NEW_LOGGING
513         LDAP_LOG( BACK_BDB, ENTRY, "bdb_db_initialize\n", 0, 0, 0 );
514 #else
515         Debug( LDAP_DEBUG_TRACE, "bdb_open: initialize BDB backend\n",
516                 0, 0, 0 );
517 #endif
518
519         {       /* version check */
520                 int major, minor, patch;
521                 char *version = db_version( &major, &minor, &patch );
522 #ifdef HAVE_EBCDIC
523                 char v2[1024];
524
525                 /* All our stdio does an ASCII to EBCDIC conversion on
526                  * the output. Strings from the BDB library are already
527                  * in EBCDIC; we have to go back and forth...
528                  */
529                 strcpy( v2, version );
530                 __etoa( v2 );
531                 version = v2;
532 #endif
533
534                 if( major != DB_VERSION_MAJOR ||
535                         minor != DB_VERSION_MINOR ||
536                         patch < DB_VERSION_PATCH )
537                 {
538 #ifdef NEW_LOGGING
539                         LDAP_LOG( BACK_BDB, ERR, 
540                                 "bdb_db_initialize: version mismatch: "
541                                 "\texpected: %s \tgot: %s\n", DB_VERSION_STRING, version, 0 );
542 #else
543                         Debug( LDAP_DEBUG_ANY,
544                                 "bdb_open: version mismatch\n"
545                                 "\texpected: " DB_VERSION_STRING "\n"
546                                 "\tgot: %s \n", version, 0, 0 );
547 #endif
548                 }
549
550 #ifdef NEW_LOGGING
551                 LDAP_LOG( BACK_BDB, DETAIL1, 
552                         "bdb_db_initialize: bdb_open: %s\n", version, 0, 0 );
553 #else
554                 Debug( LDAP_DEBUG_ANY, "bdb_open: %s\n",
555                         version, 0, 0 );
556 #endif
557         }
558
559         db_env_set_func_free( ber_memfree );
560         db_env_set_func_malloc( (db_malloc *)ber_memalloc );
561         db_env_set_func_realloc( (db_realloc *)ber_memrealloc );
562 #ifndef NO_THREAD
563         /* This is a no-op on a NO_THREAD build. Leave the default
564          * alone so that BDB will sleep on interprocess conflicts.
565          */
566         db_env_set_func_yield( ldap_pvt_thread_yield );
567 #endif
568
569         {
570                 static char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
571
572                 bdb_uuid.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ));
573                 bdb_uuid.bv_val = uuidbuf;
574         }
575
576         bi->bi_open = 0;
577         bi->bi_close = 0;
578         bi->bi_config = 0;
579         bi->bi_destroy = 0;
580
581         bi->bi_db_init = bdb_db_init;
582         bi->bi_db_config = bdb_db_config;
583         bi->bi_db_open = bdb_db_open;
584         bi->bi_db_close = bdb_db_close;
585         bi->bi_db_destroy = bdb_db_destroy;
586
587         bi->bi_op_add = bdb_add;
588         bi->bi_op_bind = bdb_bind;
589         bi->bi_op_compare = bdb_compare;
590         bi->bi_op_delete = bdb_delete;
591         bi->bi_op_modify = bdb_modify;
592         bi->bi_op_modrdn = bdb_modrdn;
593         bi->bi_op_search = bdb_search;
594
595         bi->bi_op_unbind = 0;
596         bi->bi_op_abandon = 0;
597
598         bi->bi_extended = bdb_extended;
599
600 #if 1
601         /*
602          * these routines (and their callers) are not yet designed
603          * to work with transaction.  Using them may cause deadlock.
604          */
605         bi->bi_acl_group = bdb_group;
606         bi->bi_acl_attribute = bdb_attribute;
607 #else
608         bi->bi_acl_group = 0;
609         bi->bi_acl_attribute = 0;
610 #endif
611
612         bi->bi_chk_referrals = bdb_referrals;
613         bi->bi_operational = bdb_operational;
614         bi->bi_entry_release_rw = bdb_entry_release;
615
616         /*
617          * hooks for slap tools
618          */
619         bi->bi_tool_entry_open = bdb_tool_entry_open;
620         bi->bi_tool_entry_close = bdb_tool_entry_close;
621         bi->bi_tool_entry_first = bdb_tool_entry_next;
622         bi->bi_tool_entry_next = bdb_tool_entry_next;
623         bi->bi_tool_entry_get = bdb_tool_entry_get;
624         bi->bi_tool_entry_put = bdb_tool_entry_put;
625         bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
626         bi->bi_tool_sync = 0;
627
628         bi->bi_connection_init = 0;
629         bi->bi_connection_destroy = 0;
630
631         return 0;
632 }