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