]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/init.c
e08f0547d32063be73f2836715bc5271bbf4338c
[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 "back-bdb.h"
16 #include "external.h"
17
18 static struct bdbi_database {
19         char *file;
20         char *name;
21         int type;
22         int flags;
23 } bdbi_databases[] = {
24         { "id2entry" BDB_SUFFIX, "id2entry", DB_BTREE, 0 },
25 #ifdef BDB_HIER
26         { "id2parent" BDB_SUFFIX, "id2parent", DB_BTREE, 0 },
27 #else
28         { "dn2id" BDB_SUFFIX, "dn2id", DB_BTREE, 0 },
29 #endif
30         { NULL, NULL, 0, 0 }
31 };
32
33 #if 0
34 static int
35 bdb_destroy( BackendInfo *bi )
36 {
37         return 0;
38 }
39
40 static int
41 bdb_open( BackendInfo *bi )
42 {
43         /* initialize the underlying database system */
44         Debug( LDAP_DEBUG_TRACE, "bdb_open: initialize BDB backend\n",
45                 0, 0, 0 );
46
47         return 0;
48 }
49
50 static int
51 bdb_close( BackendInfo *bi )
52 {
53         /* terminate the underlying database system */
54         return 0;
55 }
56 #endif
57
58 static int
59 bdb_db_init( BackendDB *be )
60 {
61         struct bdb_info *bdb;
62
63         Debug( LDAP_DEBUG_ANY,
64                 "bdb_db_init: Initializing BDB database\n",
65                 0, 0, 0 );
66
67         /* indicate system schema supported */
68         be->be_flags |= SLAP_BFLAG_ALIASES
69                 | SLAP_BFLAG_REFERRALS
70                 | SLAP_BFLAG_SUBENTRIES;
71
72         /* allocate backend-database-specific stuff */
73         bdb = (struct bdb_info *) ch_calloc( 1, sizeof(struct bdb_info) );
74
75         /* DBEnv parameters */
76         bdb->bi_dbenv_home = ch_strdup( BDB_DBENV_HOME );
77         bdb->bi_dbenv_xflags = 0;
78         bdb->bi_dbenv_mode = DEFAULT_MODE;
79         bdb->bi_txn = 1;        /* default to using transactions */
80
81 #ifndef NO_THREADS
82         bdb->bi_lock_detect = DB_LOCK_NORUN;
83 #endif
84
85         ldap_pvt_thread_mutex_init( &bdb->bi_database_mutex );
86         ldap_pvt_thread_mutex_init( &bdb->bi_lastid_mutex );
87 #ifdef BDB_HIER
88         ldap_pvt_thread_rdwr_init( &bdb->bi_tree_rdwr );
89 #endif
90
91         be->be_private = bdb;
92         return 0;
93 }
94
95 #ifndef NO_THREADS
96 static void *lock_detect_task( void *arg )
97 {
98         struct bdb_info *bdb = (struct bdb_info *) arg;
99
100         while( bdb->bi_dbenv != NULL ) {
101                 int rc;
102                 sleep( bdb->bi_lock_detect_seconds );
103
104                 rc = LOCK_DETECT( bdb->bi_dbenv, 0,
105                         bdb->bi_lock_detect, NULL );
106
107                 if( rc != 0 ) {
108                         break;
109                 }
110         }
111
112         return NULL;
113 }
114 #endif
115
116 int
117 bdb_bt_compare(
118         DB *db, 
119         const DBT *usrkey,
120         const DBT *curkey
121 )
122 {
123         unsigned char *u, *c;
124         int i;
125
126         u = usrkey->data;
127         c = curkey->data;
128
129 #ifdef WORDS_BIGENDIAN
130         for( i = 0; i < sizeof(ID); i++)
131 #else
132         for( i = sizeof(ID)-1; i >= 0; i--)
133 #endif
134         {
135                 if( u[i] - c[i] )
136                         return u[i] - c[i];
137         }
138         return 0;
139 }
140
141 static int
142 bdb_db_open( BackendDB *be )
143 {
144         int rc, i;
145         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
146         u_int32_t flags;
147
148         Debug( LDAP_DEBUG_ARGS,
149                 "bdb_db_open: %s\n",
150                 be->be_suffix[0]->bv_val, 0, 0 );
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                 Debug( LDAP_DEBUG_ANY,
157                         "bdb_db_open: db_env_create failed: %s (%d)\n",
158                         db_strerror(rc), rc, 0 );
159                 return rc;
160         }
161
162         flags = DB_INIT_MPOOL | DB_THREAD | DB_CREATE;
163
164         if( bdb->bi_txn ) {
165                 flags |= DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN | DB_RECOVER;
166
167         } else {
168                 flags |= DB_INIT_CDB;
169                 bdb->bi_txn_cp = 0;
170         }
171
172         bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0]->bv_val );
173         bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
174
175 #ifdef BDB_SUBDIRS
176         {
177                 char dir[MAXPATHLEN];
178                 size_t len = strlen( bdb->bi_dbenv_home );
179
180                 strcpy( dir, bdb->bi_dbenv_home );
181                 strcat( &dir[len], BDB_TMP_SUBDIR );
182                 
183                 rc = bdb->bi_dbenv->set_tmp_dir( bdb->bi_dbenv, dir );
184                 if( rc != 0 ) {
185                         Debug( LDAP_DEBUG_ANY,
186                                 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
187                                 dir, db_strerror(rc), rc );
188                         return rc;
189                 }
190
191                 strcat( &dir[len], BDB_LG_SUBDIR );
192
193                 rc = bdb->bi_dbenv->set_lg_dir( bdb->bi_dbenv, dir );
194                 if( rc != 0 ) {
195                         Debug( LDAP_DEBUG_ANY,
196                                 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
197                                 dir, db_strerror(rc), rc );
198                         return rc;
199                 }
200
201                 strcat( &dir[len], BDB_DATA_SUBDIR );
202
203                 rc = bdb->bi_dbenv->set_data_dir( bdb->bi_dbenv, dir );
204                 if( rc != 0 ) {
205                         Debug( LDAP_DEBUG_ANY,
206                                 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
207                                 dir, db_strerror(rc), rc );
208                         return rc;
209                 }
210         }
211 #endif
212
213         Debug( LDAP_DEBUG_TRACE,
214                 "bdb_db_open: dbenv_open(%s)\n",
215                 bdb->bi_dbenv_home, 0, 0);
216
217         rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
218                 bdb->bi_dbenv_home,
219                 flags,
220                 bdb->bi_dbenv_mode );
221         if( rc != 0 ) {
222                 Debug( LDAP_DEBUG_ANY,
223                         "bdb_db_open: dbenv_open failed: %s (%d)\n",
224                         db_strerror(rc), rc, 0 );
225                 return rc;
226         }
227
228         if( bdb->bi_dbenv_xflags != 0 ) {
229                 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
230                         bdb->bi_dbenv_xflags, 1);
231                 if( rc != 0 ) {
232                         Debug( LDAP_DEBUG_ANY,
233                                 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
234                                 db_strerror(rc), rc, 0 );
235                         return rc;
236                 }
237         }
238
239         flags = DB_THREAD | DB_CREATE | bdb->bi_db_opflags;
240
241         bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
242                 BDB_INDICES * sizeof(struct bdb_db_info *) );
243
244         /* open (and create) main database */
245         for( i = 0; bdbi_databases[i].name; i++ ) {
246                 struct bdb_db_info *db;
247
248                 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
249
250                 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
251                 if( rc != 0 ) {
252                         Debug( LDAP_DEBUG_ANY,
253                                 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
254                                 bdb->bi_dbenv_home, db_strerror(rc), rc );
255                         return rc;
256                 }
257
258                 if( i == BDB_ID2ENTRY ) {
259                         rc = db->bdi_db->set_bt_compare( db->bdi_db,
260                                 bdb_bt_compare );
261                         rc = db->bdi_db->set_pagesize( db->bdi_db,
262                                 BDB_ID2ENTRY_PAGESIZE );
263                 } else {
264 #ifdef BDB_HIER
265                         rc = db->bdi_db->set_bt_compare( db->bdi_db,
266                                 bdb_bt_compare );
267 #elif defined(BDB_IDL_MULTI)
268                         rc = db->bdi_db->set_flags( db->bdi_db, 
269                                 DB_DUP | DB_DUPSORT );
270                         rc = db->bdi_db->set_dup_compare( db->bdi_db,
271                                 bdb_bt_compare );
272 #endif
273                         rc = db->bdi_db->set_pagesize( db->bdi_db,
274                                 BDB_PAGESIZE );
275                 }
276
277                 rc = db->bdi_db->open( db->bdi_db,
278                         bdbi_databases[i].file,
279                 /*      bdbi_databases[i].name, */ NULL,
280                         bdbi_databases[i].type,
281                         bdbi_databases[i].flags | flags,
282                         bdb->bi_dbenv_mode );
283
284                 if( rc != 0 ) {
285                         Debug( LDAP_DEBUG_ANY,
286                                 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
287                                 bdb->bi_dbenv_home, db_strerror(rc), rc );
288                         return rc;
289                 }
290
291                 db->bdi_name = bdbi_databases[i].name;
292                 bdb->bi_databases[i] = db;
293         }
294
295         bdb->bi_databases[i] = NULL;
296         bdb->bi_ndatabases = i;
297
298         /* get nextid */
299         rc = bdb_last_id( be, NULL );
300         if( rc != 0 ) {
301                 Debug( LDAP_DEBUG_ANY,
302                         "bdb_db_open: last_id(%s) failed: %s (%d)\n",
303                         bdb->bi_dbenv_home, db_strerror(rc), rc );
304                 return rc;
305         }
306
307         /* <insert> open (and create) index databases */
308 #ifdef BDB_HIER
309         rc = bdb_build_tree( be );
310 #endif
311
312 #ifndef NO_THREADS
313         if( bdb->bi_lock_detect != DB_LOCK_NORUN ) {
314                 /* listener as a separate THREAD */
315                 rc = ldap_pvt_thread_create( &bdb->bi_lock_detect_tid,
316                         1, lock_detect_task, bdb );
317         }
318 #endif
319         return 0;
320 }
321
322 static int
323 bdb_db_close( BackendDB *be )
324 {
325         int rc;
326         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
327         struct bdb_db_info *db;
328
329         while( bdb->bi_ndatabases-- ) {
330                 db = bdb->bi_databases[bdb->bi_ndatabases];
331                 rc = db->bdi_db->close( db->bdi_db, 0 );
332                 /* Lower numbered names are not strdup'd */
333                 if( bdb->bi_ndatabases >= BDB_NDB )
334                         free( db->bdi_name );
335                 free( db );
336         }
337         free( bdb->bi_databases );
338         bdb_attr_index_destroy( bdb->bi_attrs );
339
340         return 0;
341 }
342
343 static int
344 bdb_db_destroy( BackendDB *be )
345 {
346         int rc;
347         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
348
349         /* force a checkpoint */
350         if( bdb->bi_txn ) {
351                 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
352                 if( rc != 0 ) {
353                         Debug( LDAP_DEBUG_ANY,
354                                 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
355                                 db_strerror(rc), rc, 0 );
356                 }
357         }
358
359         /* close db environment */
360         if( bdb->bi_dbenv ) {
361                 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
362                 bdb->bi_dbenv = NULL;
363                 if( rc != 0 ) {
364                         Debug( LDAP_DEBUG_ANY,
365                                 "bdb_db_destroy: close failed: %s (%d)\n",
366                                 db_strerror(rc), rc, 0 );
367                         return rc;
368                 }
369         }
370
371         return 0;
372 }
373
374 #ifdef SLAPD_BDB_DYNAMIC
375 int back_bdb_LTX_init_module( int argc, char *argv[] ) {
376         BackendInfo bi;
377
378         memset( &bi, '\0', sizeof(bi) );
379         bi.bi_type = "bdb";
380         bi.bi_init = bdb_initialize;
381
382         backend_add( &bi );
383         return 0;
384 }
385 #endif /* SLAPD_BDB_DYNAMIC */
386
387 int
388 bdb_initialize(
389         BackendInfo     *bi
390 )
391 {
392         static char *controls[] = {
393                 LDAP_CONTROL_MANAGEDSAIT,
394                 LDAP_CONTROL_SUBENTRIES,
395                 NULL
396         };
397
398         {       /* version check */
399                 int major, minor, patch;
400                 char *version = db_version( &major, &minor, &patch );
401
402                 if( major != DB_VERSION_MAJOR ||
403                         minor != DB_VERSION_MINOR ||
404                         patch < DB_VERSION_PATCH )
405                 {
406                         Debug( LDAP_DEBUG_ANY,
407                                 "bi_back_initialize: version mismatch\n"
408                                 "\texpected: " DB_VERSION_STRING "\n"
409                                 "\tgot: %s \n", version, 0, 0 );
410                 }
411
412                 Debug( LDAP_DEBUG_ANY, "bdb_initialize: %s\n",
413                         version, 0, 0 );
414         }
415
416 #if 0
417         db_env_set_func_malloc( ch_malloc );
418         db_env_set_func_realloc( ch_realloc );
419         db_env_set_func_free( ch_free );
420 #endif
421
422         db_env_set_func_yield( ldap_pvt_thread_yield );
423
424         bi->bi_controls = controls;
425
426         bi->bi_open = 0;
427         bi->bi_close = 0;
428         bi->bi_config = 0;
429         bi->bi_destroy = 0;
430
431         bi->bi_db_init = bdb_db_init;
432         bi->bi_db_config = bdb_db_config;
433         bi->bi_db_open = bdb_db_open;
434         bi->bi_db_close = bdb_db_close;
435         bi->bi_db_destroy = bdb_db_destroy;
436
437         bi->bi_op_add = bdb_add;
438         bi->bi_op_bind = bdb_bind;
439         bi->bi_op_compare = bdb_compare;
440         bi->bi_op_delete = bdb_delete;
441         bi->bi_op_modify = bdb_modify;
442         bi->bi_op_modrdn = bdb_modrdn;
443         bi->bi_op_search = bdb_search;
444
445         bi->bi_op_unbind = 0;
446         bi->bi_op_abandon = 0;
447
448         bi->bi_extended = bdb_extended;
449
450 #if 0
451         /*
452          * these routines (and their callers) are not yet designed
453          * to work with transaction.  Using them may cause deadlock.
454          */
455         bi->bi_acl_group = bdb_group;
456         bi->bi_acl_attribute = bdb_attribute;
457 #else
458         bi->bi_acl_group = 0;
459         bi->bi_acl_attribute = 0;
460 #endif
461
462         bi->bi_chk_referrals = bdb_referrals;
463         bi->bi_entry_release_rw = bdb_entry_release;
464
465         /*
466          * hooks for slap tools
467          */
468         bi->bi_tool_entry_open = bdb_tool_entry_open;
469         bi->bi_tool_entry_close = bdb_tool_entry_close;
470         bi->bi_tool_entry_first = bdb_tool_entry_next;
471         bi->bi_tool_entry_next = bdb_tool_entry_next;
472         bi->bi_tool_entry_get = bdb_tool_entry_get;
473         bi->bi_tool_entry_put = bdb_tool_entry_put;
474         bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
475         bi->bi_tool_sync = 0;
476
477         bi->bi_connection_init = 0;
478         bi->bi_connection_destroy = 0;
479
480         return 0;
481 }