]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/init.c
Add start of discussion of strings in LDAP/X.500 and OpenLDAP.
[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 #ifndef NO_THREADS
95 #if 0
96         bdb->bi_lock_detect = DB_LOCK_NORUN;
97 #else
98         bdb->bi_lock_detect = DB_LOCK_DEFAULT;
99 #endif
100 #endif
101
102         ldap_pvt_thread_mutex_init( &bdb->bi_database_mutex );
103         ldap_pvt_thread_mutex_init( &bdb->bi_lastid_mutex );
104         ldap_pvt_thread_mutex_init( &bdb->bi_cache.lru_mutex );
105         ldap_pvt_thread_rdwr_init ( &bdb->bi_cache.c_rwlock );
106 #ifdef BDB_HIER
107         ldap_pvt_thread_rdwr_init( &bdb->bi_tree_rdwr );
108 #endif
109
110         be->be_private = bdb;
111         return 0;
112 }
113
114 #if 0 /* ifndef NO_THREADS */
115 static void *lock_detect_task( void *arg )
116 {
117         struct bdb_info *bdb = (struct bdb_info *) arg;
118
119         while( bdb->bi_dbenv != NULL ) {
120                 int rc;
121                 int aborted;
122                 sleep( bdb->bi_lock_detect_seconds );
123
124                 rc = LOCK_DETECT( bdb->bi_dbenv, 0,
125                         bdb->bi_lock_detect, &aborted );
126
127                 if( rc != 0 ) {
128                         break;
129                 }
130
131 #ifdef NEW_LOGGING
132                 LDAP_LOG( BACK_BDB, ERR, "bdb_db_init: aborted %d locks\n", 
133                         aborted, 0, 0 );
134 #else
135                 Debug( LDAP_DEBUG_ANY,
136                         "bdb_lock_detect: aborted %d locks\n",
137                         aborted, 0, 0 );
138 #endif
139         }
140
141         return NULL;
142 }
143 #endif
144
145 int
146 bdb_bt_compare(
147         DB *db, 
148         const DBT *usrkey,
149         const DBT *curkey
150 )
151 {
152         unsigned char *u, *c;
153         int i;
154
155         u = usrkey->data;
156         c = curkey->data;
157
158 #ifdef WORDS_BIGENDIAN
159         for( i = 0; i < sizeof(ID); i++)
160 #else
161         for( i = sizeof(ID)-1; i >= 0; i--)
162 #endif
163         {
164                 if( u[i] - c[i] )
165                         return u[i] - c[i];
166         }
167         return 0;
168 }
169
170 static int
171 bdb_db_open( BackendDB *be )
172 {
173         int rc, i;
174         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
175         u_int32_t flags;
176
177 #ifdef NEW_LOGGING
178         LDAP_LOG( BACK_BDB, ARGS, 
179                 "bdb_db_open: %s\n", be->be_suffix[0].bv_val, 0, 0 );
180 #else
181         Debug( LDAP_DEBUG_ARGS,
182                 "bdb_db_open: %s\n",
183                 be->be_suffix[0].bv_val, 0, 0 );
184 #endif
185
186         /* we should check existance of dbenv_home and db_directory */
187
188         rc = db_env_create( &bdb->bi_dbenv, 0 );
189         if( rc != 0 ) {
190 #ifdef NEW_LOGGING
191                 LDAP_LOG( BACK_BDB, ERR, 
192                         "bdb_db_open: db_env_create failed: %s (%d)\n", 
193                         db_strerror(rc), rc, 0 );
194 #else
195                 Debug( LDAP_DEBUG_ANY,
196                         "bdb_db_open: db_env_create failed: %s (%d)\n",
197                         db_strerror(rc), rc, 0 );
198 #endif
199                 return rc;
200         }
201
202         bdb->bi_dbenv->set_alloc( bdb->bi_dbenv, (db_malloc *)ber_memalloc,
203                 (db_realloc *)ber_memrealloc, ber_memfree );
204
205         flags = DB_INIT_MPOOL | DB_THREAD | DB_CREATE
206                 | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN;
207         
208         /* Only slapd will do recovery, slap-tools won't. Otherwise
209          * running slapcat while slapd is active corrupts the db.
210          */
211         if( !(slapMode & SLAP_TOOL_MODE) ) flags |= DB_RECOVER;
212
213         bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0].bv_val );
214         bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
215 #ifndef NO_THREADS
216         bdb->bi_dbenv->set_lk_detect( bdb->bi_dbenv, bdb->bi_lock_detect );
217 #endif
218
219 #ifdef BDB_SUBDIRS
220         {
221                 char dir[MAXPATHLEN];
222                 size_t len = strlen( bdb->bi_dbenv_home );
223
224                 strcpy( dir, bdb->bi_dbenv_home );
225                 strcat( &dir[len], BDB_TMP_SUBDIR );
226                 
227                 rc = bdb->bi_dbenv->set_tmp_dir( bdb->bi_dbenv, dir );
228                 if( rc != 0 ) {
229 #ifdef NEW_LOGGING
230                         LDAP_LOG( BACK_BDB, ERR, 
231                                 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n", 
232                                 dir, db_strerror(rc), rc );
233 #else
234                         Debug( LDAP_DEBUG_ANY,
235                                 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
236                                 dir, db_strerror(rc), rc );
237 #endif
238                         return rc;
239                 }
240
241                 strcat( &dir[len], BDB_LG_SUBDIR );
242
243                 rc = bdb->bi_dbenv->set_lg_dir( bdb->bi_dbenv, dir );
244                 if( rc != 0 ) {
245 #ifdef NEW_LOGGING
246                         LDAP_LOG( BACK_BDB, ERR, 
247                                 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n", 
248                                 dir, db_strerror(rc), rc );
249 #else
250                         Debug( LDAP_DEBUG_ANY,
251                                 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
252                                 dir, db_strerror(rc), rc );
253 #endif
254                         return rc;
255                 }
256
257                 strcat( &dir[len], BDB_DATA_SUBDIR );
258
259                 rc = bdb->bi_dbenv->set_data_dir( bdb->bi_dbenv, dir );
260                 if( rc != 0 ) {
261 #ifdef NEW_LOGGING
262                         LDAP_LOG( BACK_BDB, ERR, 
263                                 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
264                                 dir, db_strerror(rc), rc );
265 #else
266                         Debug( LDAP_DEBUG_ANY,
267                                 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
268                                 dir, db_strerror(rc), rc );
269 #endif
270                         return rc;
271                 }
272         }
273 #endif
274
275 #ifdef NEW_LOGGING
276         LDAP_LOG( BACK_BDB, DETAIL1, 
277                 "bdb_db_open: dbenv_open %s\n", bdb->bi_dbenv_home, 0, 0 );
278 #else
279         Debug( LDAP_DEBUG_TRACE,
280                 "bdb_db_open: dbenv_open(%s)\n",
281                 bdb->bi_dbenv_home, 0, 0);
282 #endif
283
284         rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
285                 bdb->bi_dbenv_home,
286                 flags,
287                 bdb->bi_dbenv_mode );
288         if( rc != 0 ) {
289 #ifdef NEW_LOGGING
290                 LDAP_LOG( BACK_BDB, ERR, 
291                         "bdb_db_open: dbenv_open failed: %s (%d)\n", 
292                         db_strerror(rc), rc, 0 );
293 #else
294                 Debug( LDAP_DEBUG_ANY,
295                         "bdb_db_open: dbenv_open failed: %s (%d)\n",
296                         db_strerror(rc), rc, 0 );
297 #endif
298                 return rc;
299         }
300
301         if( bdb->bi_dbenv_xflags != 0 ) {
302                 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
303                         bdb->bi_dbenv_xflags, 1);
304                 if( rc != 0 ) {
305 #ifdef NEW_LOGGING
306                         LDAP_LOG( BACK_BDB, ERR, 
307                                 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n", 
308                                 db_strerror(rc), rc, 0 );
309 #else
310                         Debug( LDAP_DEBUG_ANY,
311                                 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
312                                 db_strerror(rc), rc, 0 );
313 #endif
314                         return rc;
315                 }
316         }
317
318         flags = DB_THREAD | DB_CREATE | bdb->bi_db_opflags;
319
320         bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
321                 BDB_INDICES * sizeof(struct bdb_db_info *) );
322
323         /* open (and create) main database */
324         for( i = 0; bdbi_databases[i].name; i++ ) {
325                 struct bdb_db_info *db;
326
327                 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
328
329                 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
330                 if( rc != 0 ) {
331 #ifdef NEW_LOGGING
332                         LDAP_LOG( BACK_BDB, ERR, 
333                                 "bdb_db_open: db_create(%s) failed: %s (%d)\n", 
334                                 bdb->bi_dbenv_home, db_strerror(rc), rc );
335 #else
336                         Debug( LDAP_DEBUG_ANY,
337                                 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
338                                 bdb->bi_dbenv_home, db_strerror(rc), rc );
339 #endif
340                         return rc;
341                 }
342
343                 if( i == BDB_ID2ENTRY ) {
344                         rc = db->bdi_db->set_bt_compare( db->bdi_db,
345                                 bdb_bt_compare );
346                         rc = db->bdi_db->set_pagesize( db->bdi_db,
347                                 BDB_ID2ENTRY_PAGESIZE );
348                 } else {
349 #ifdef BDB_HIER
350                         rc = db->bdi_db->set_bt_compare( db->bdi_db,
351                                 bdb_bt_compare );
352 #elif defined(BDB_IDL_MULTI)
353                         rc = db->bdi_db->set_flags( db->bdi_db, 
354                                 DB_DUP | DB_DUPSORT );
355                         rc = db->bdi_db->set_dup_compare( db->bdi_db,
356                                 bdb_bt_compare );
357 #endif
358                         rc = db->bdi_db->set_pagesize( db->bdi_db,
359                                 BDB_PAGESIZE );
360                 }
361
362                 rc = db->bdi_db->open( db->bdi_db,
363                         bdbi_databases[i].file,
364                 /*      bdbi_databases[i].name, */ NULL,
365                         bdbi_databases[i].type,
366                         bdbi_databases[i].flags | flags,
367                         bdb->bi_dbenv_mode );
368
369                 if( rc != 0 ) {
370 #ifdef NEW_LOGGING
371                         LDAP_LOG( BACK_BDB, ERR, 
372                                 "bdb_db_open: db_create(%s) failed: %s (%d)\n", 
373                                 bdb->bi_dbenv_home, db_strerror(rc), rc );
374 #else
375                         Debug( LDAP_DEBUG_ANY,
376                                 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
377                                 bdb->bi_dbenv_home, db_strerror(rc), rc );
378 #endif
379                         return rc;
380                 }
381
382                 db->bdi_name = bdbi_databases[i].name;
383                 bdb->bi_databases[i] = db;
384         }
385
386         bdb->bi_databases[i] = NULL;
387         bdb->bi_ndatabases = i;
388
389         /* get nextid */
390         rc = bdb_last_id( be, NULL );
391         if( rc != 0 ) {
392 #ifdef NEW_LOGGING
393                         LDAP_LOG( BACK_BDB, ERR, 
394                                 "bdb_db_open: last_id(%s) failed: %s (%d)\n", 
395                                 bdb->bi_dbenv_home, db_strerror(rc), rc );
396 #else
397                 Debug( LDAP_DEBUG_ANY,
398                         "bdb_db_open: last_id(%s) failed: %s (%d)\n",
399                         bdb->bi_dbenv_home, db_strerror(rc), rc );
400 #endif
401                 return rc;
402         }
403
404         /* <insert> open (and create) index databases */
405 #ifdef BDB_HIER
406         rc = bdb_build_tree( be );
407 #endif
408
409 #if 0 /* ifndef NO_THREADS */
410         if( bdb->bi_lock_detect != DB_LOCK_NORUN ) {
411                 /* listener as a separate THREAD */
412                 rc = ldap_pvt_thread_create( &bdb->bi_lock_detect_tid,
413                         1, lock_detect_task, bdb );
414         }
415 #endif
416         return 0;
417 }
418
419 static int
420 bdb_db_close( BackendDB *be )
421 {
422         int rc;
423         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
424         struct bdb_db_info *db;
425
426         while( bdb->bi_ndatabases-- ) {
427                 db = bdb->bi_databases[bdb->bi_ndatabases];
428                 rc = db->bdi_db->close( db->bdi_db, 0 );
429                 /* Lower numbered names are not strdup'd */
430                 if( bdb->bi_ndatabases >= BDB_NDB )
431                         free( db->bdi_name );
432                 free( db );
433         }
434         free( bdb->bi_databases );
435         bdb_attr_index_destroy( bdb->bi_attrs );
436
437         bdb_cache_release_all (&bdb->bi_cache);
438
439         return 0;
440 }
441
442 static int
443 bdb_db_destroy( BackendDB *be )
444 {
445         int rc;
446         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
447
448         /* close db environment */
449         if( bdb->bi_dbenv ) {
450                 /* force a checkpoint */
451                 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
452                 if( rc != 0 ) {
453 #ifdef NEW_LOGGING
454                         LDAP_LOG( BACK_BDB, ERR, 
455                                 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
456                                 db_strerror(rc), rc, 0 );
457 #else
458                         Debug( LDAP_DEBUG_ANY,
459                                 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
460                                 db_strerror(rc), rc, 0 );
461 #endif
462                 }
463
464                 bdb_cache_release_all (&bdb->bi_cache);
465
466                 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
467                 bdb->bi_dbenv = NULL;
468                 if( rc != 0 ) {
469 #ifdef NEW_LOGGING
470                         LDAP_LOG( BACK_BDB, ERR, 
471                                 "bdb_db_destroy: close failed: %s (%d)\n", 
472                                 db_strerror(rc), rc, 0 );
473 #else
474                         Debug( LDAP_DEBUG_ANY,
475                                 "bdb_db_destroy: close failed: %s (%d)\n",
476                                 db_strerror(rc), rc, 0 );
477 #endif
478                         return rc;
479                 }
480         }
481
482 #ifdef BDB_HIER
483         ldap_pvt_thread_rdwr_destroy( &bdb->bi_tree_rdwr );
484 #endif
485         ldap_pvt_thread_rdwr_destroy ( &bdb->bi_cache.c_rwlock );
486         ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.lru_mutex );
487         ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
488         ldap_pvt_thread_mutex_destroy( &bdb->bi_database_mutex );
489
490         return 0;
491 }
492
493 #ifdef SLAPD_BDB_DYNAMIC
494 int back_bdb_LTX_init_module( int argc, char *argv[] ) {
495         BackendInfo bi;
496
497         memset( &bi, '\0', sizeof(bi) );
498         bi.bi_type = "bdb";
499         bi.bi_init = bdb_initialize;
500
501         backend_add( &bi );
502         return 0;
503 }
504 #endif /* SLAPD_BDB_DYNAMIC */
505
506 int
507 bdb_initialize(
508         BackendInfo     *bi
509 )
510 {
511         static char *controls[] = {
512                 LDAP_CONTROL_MANAGEDSAIT,
513 #ifdef LDAP_CONTROL_SUBENTRIES
514                 LDAP_CONTROL_SUBENTRIES,
515 #endif
516 #ifdef LDAP_CONTROL_NOOP
517                 LDAP_CONTROL_NOOP,
518 #endif
519 #ifdef LDAP_CONTROL_VALUESRETURNFILTER
520                 LDAP_CONTROL_VALUESRETURNFILTER,
521 #endif
522                 NULL
523         };
524
525         bi->bi_controls = controls;
526
527         /* initialize the underlying database system */
528 #ifdef NEW_LOGGING
529         LDAP_LOG( BACK_BDB, ENTRY, "bdb_db_initialize\n", 0, 0, 0 );
530 #else
531         Debug( LDAP_DEBUG_TRACE, "bdb_open: initialize BDB backend\n",
532                 0, 0, 0 );
533 #endif
534
535         {       /* version check */
536                 int major, minor, patch;
537                 char *version = db_version( &major, &minor, &patch );
538
539                 if( major != DB_VERSION_MAJOR ||
540                         minor != DB_VERSION_MINOR ||
541                         patch < DB_VERSION_PATCH )
542                 {
543 #ifdef NEW_LOGGING
544                         LDAP_LOG( BACK_BDB, ERR, 
545                                 "bdb_db_initialize: version mismatch: "
546                                 "\texpected: %s \tgot: %s\n", DB_VERSION_STRING, version, 0 );
547 #else
548                         Debug( LDAP_DEBUG_ANY,
549                                 "bdb_open: version mismatch\n"
550                                 "\texpected: " DB_VERSION_STRING "\n"
551                                 "\tgot: %s \n", version, 0, 0 );
552 #endif
553                 }
554
555 #ifdef NEW_LOGGING
556                 LDAP_LOG( BACK_BDB, DETAIL1, 
557                         "bdb_db_initialize: bdb_open: %s\n", version, 0, 0 );
558 #else
559                 Debug( LDAP_DEBUG_ANY, "bdb_open: %s\n",
560                         version, 0, 0 );
561 #endif
562         }
563
564 #if 0
565         db_env_set_func_malloc( ch_malloc );
566         db_env_set_func_realloc( ch_realloc );
567         db_env_set_func_free( ch_free );
568 #endif
569
570         db_env_set_func_yield( ldap_pvt_thread_yield );
571
572         {
573                 static char uuidbuf[40];
574
575                 bdb_uuid.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ));
576                 bdb_uuid.bv_val = uuidbuf;
577         }
578
579         bi->bi_open = 0;
580         bi->bi_close = 0;
581         bi->bi_config = 0;
582         bi->bi_destroy = 0;
583
584         bi->bi_db_init = bdb_db_init;
585         bi->bi_db_config = bdb_db_config;
586         bi->bi_db_open = bdb_db_open;
587         bi->bi_db_close = bdb_db_close;
588         bi->bi_db_destroy = bdb_db_destroy;
589
590         bi->bi_op_add = bdb_add;
591         bi->bi_op_bind = bdb_bind;
592         bi->bi_op_compare = bdb_compare;
593         bi->bi_op_delete = bdb_delete;
594         bi->bi_op_modify = bdb_modify;
595         bi->bi_op_modrdn = bdb_modrdn;
596         bi->bi_op_search = bdb_search;
597
598         bi->bi_op_unbind = 0;
599         bi->bi_op_abandon = 0;
600
601         bi->bi_extended = bdb_extended;
602
603 #if 1
604         /*
605          * these routines (and their callers) are not yet designed
606          * to work with transaction.  Using them may cause deadlock.
607          */
608         bi->bi_acl_group = bdb_group;
609         bi->bi_acl_attribute = bdb_attribute;
610 #else
611         bi->bi_acl_group = 0;
612         bi->bi_acl_attribute = 0;
613 #endif
614
615         bi->bi_chk_referrals = bdb_referrals;
616         bi->bi_operational = bdb_operational;
617         bi->bi_entry_release_rw = bdb_entry_release;
618
619         /*
620          * hooks for slap tools
621          */
622         bi->bi_tool_entry_open = bdb_tool_entry_open;
623         bi->bi_tool_entry_close = bdb_tool_entry_close;
624         bi->bi_tool_entry_first = bdb_tool_entry_next;
625         bi->bi_tool_entry_next = bdb_tool_entry_next;
626         bi->bi_tool_entry_get = bdb_tool_entry_get;
627         bi->bi_tool_entry_put = bdb_tool_entry_put;
628         bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
629         bi->bi_tool_sync = 0;
630
631         bi->bi_connection_init = 0;
632         bi->bi_connection_destroy = 0;
633
634         return 0;
635 }