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