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