]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/init.c
66eb7ce861224179808d6f7940a90b224a83dd4a
[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                 }
248 #ifdef BDB_IDL_MULTI
249                 if( i == BDB_DN2ID ) {
250                         rc = db->bdi_db->set_flags( db->bdi_db, DB_DUPSORT );
251                         rc = db->bdi_db->set_dup_compare( db->bdi_db,
252                                 bdb_bt_compare );
253                 }
254 #endif
255                 rc = db->bdi_db->set_pagesize( db->bdi_db, BDB_PAGESIZE );
256
257                 rc = db->bdi_db->open( db->bdi_db,
258                         bdbi_databases[i].file,
259                 /*      bdbi_databases[i].name, */ NULL,
260                         bdbi_databases[i].type,
261                         bdbi_databases[i].flags | flags,
262                         bdb->bi_dbenv_mode );
263
264                 if( rc != 0 ) {
265                         Debug( LDAP_DEBUG_ANY,
266                                 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
267                                 bdb->bi_dbenv_home, db_strerror(rc), rc );
268                         return rc;
269                 }
270
271                 db->bdi_name = bdbi_databases[i].name;
272                 bdb->bi_databases[i] = db;
273         }
274
275         bdb->bi_databases[i] = NULL;
276         bdb->bi_ndatabases = i;
277
278         /* get nextid */
279         rc = bdb_last_id( be, NULL );
280         if( rc != 0 ) {
281                 Debug( LDAP_DEBUG_ANY,
282                         "bdb_db_open: last_id(%s) failed: %s (%d)\n",
283                         bdb->bi_dbenv_home, db_strerror(rc), rc );
284                 return rc;
285         }
286
287         /* <insert> open (and create) index databases */
288
289 #ifndef NO_THREADS
290         if( bdb->bi_lock_detect != DB_LOCK_NORUN ) {
291                 /* listener as a separate THREAD */
292                 rc = ldap_pvt_thread_create( &bdb->bi_lock_detect_tid,
293                         1, lock_detect_task, bdb );
294         }
295 #endif
296         return 0;
297 }
298
299 static int
300 bdb_db_close( BackendDB *be )
301 {
302         int rc;
303         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
304
305         while( bdb->bi_ndatabases-- ) {
306                 rc = bdb->bi_databases[bdb->bi_ndatabases]->bdi_db->close(
307                         bdb->bi_databases[bdb->bi_ndatabases]->bdi_db, 0 );
308         }
309
310         return 0;
311 }
312
313 static int
314 bdb_db_destroy( BackendDB *be )
315 {
316         int rc;
317         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
318
319         /* force a checkpoint */
320         if( bdb->bi_txn ) {
321                 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
322                 if( rc != 0 ) {
323                         Debug( LDAP_DEBUG_ANY,
324                                 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
325                                 db_strerror(rc), rc, 0 );
326                 }
327         }
328
329         /* close db environment */
330         if( bdb->bi_dbenv ) {
331                 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
332                 bdb->bi_dbenv = NULL;
333                 if( rc != 0 ) {
334                         Debug( LDAP_DEBUG_ANY,
335                                 "bdb_db_destroy: close failed: %s (%d)\n",
336                                 db_strerror(rc), rc, 0 );
337                         return rc;
338                 }
339         }
340
341         return 0;
342 }
343
344 #ifdef SLAPD_BDB_DYNAMIC
345 int back_bdb_LTX_init_module( int argc, char *argv[] ) {
346         BackendInfo bi;
347
348         memset( &bi, '\0', sizeof(bi) );
349         bi.bi_type = "bdb";
350         bi.bi_init = bdb_initialize;
351
352         backend_add( &bi );
353         return 0;
354 }
355 #endif /* SLAPD_BDB_DYNAMIC */
356
357 int
358 bdb_initialize(
359         BackendInfo     *bi
360 )
361 {
362         static char *controls[] = {
363                 LDAP_CONTROL_MANAGEDSAIT,
364                 NULL
365         };
366
367         {       /* version check */
368                 int major, minor, patch;
369                 char *version = db_version( &major, &minor, &patch );
370
371                 if( major != DB_VERSION_MAJOR ||
372                         minor != DB_VERSION_MINOR ||
373                         patch < DB_VERSION_PATCH )
374                 {
375                         Debug( LDAP_DEBUG_ANY,
376                                 "bi_back_initialize: version mismatch\n"
377                                 "\texpected: " DB_VERSION_STRING "\n"
378                                 "\tgot: %s \n", version, 0, 0 );
379                 }
380
381                 Debug( LDAP_DEBUG_ANY, "bdb_initialize: %s\n",
382                         version, 0, 0 );
383         }
384
385 #if 0
386         db_env_set_func_malloc( ch_malloc );
387         db_env_set_func_realloc( ch_realloc );
388         db_env_set_func_free( ch_free );
389 #endif
390
391         db_env_set_func_yield( ldap_pvt_thread_yield );
392
393         bi->bi_controls = controls;
394
395         bi->bi_open = 0;
396         bi->bi_close = 0;
397         bi->bi_config = 0;
398         bi->bi_destroy = 0;
399
400         bi->bi_db_init = bdb_db_init;
401         bi->bi_db_config = bdb_db_config;
402         bi->bi_db_open = bdb_db_open;
403         bi->bi_db_close = bdb_db_close;
404         bi->bi_db_destroy = bdb_db_destroy;
405
406         bi->bi_op_add = bdb_add;
407         bi->bi_op_bind = bdb_bind;
408         bi->bi_op_compare = bdb_compare;
409         bi->bi_op_delete = bdb_delete;
410         bi->bi_op_modify = bdb_modify;
411         bi->bi_op_modrdn = bdb_modrdn;
412         bi->bi_op_search = bdb_search;
413
414 #if 0
415         bi->bi_op_unbind = bdb_unbind;
416         bi->bi_op_abandon = bdb_abandon;
417
418         bi->bi_extended = bdb_extended;
419 #endif
420         bi->bi_acl_group = bdb_group;
421         bi->bi_acl_attribute = bdb_attribute;
422         bi->bi_chk_referrals = bdb_referrals;
423
424         bi->bi_entry_release_rw = bdb_entry_release;
425
426         /*
427          * hooks for slap tools
428          */
429         bi->bi_tool_entry_open = bdb_tool_entry_open;
430         bi->bi_tool_entry_close = bdb_tool_entry_close;
431         bi->bi_tool_entry_first = bdb_tool_entry_next;
432         bi->bi_tool_entry_next = bdb_tool_entry_next;
433         bi->bi_tool_entry_get = bdb_tool_entry_get;
434         bi->bi_tool_entry_put = bdb_tool_entry_put;
435         bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
436         bi->bi_tool_sync = 0;
437
438         bi->bi_connection_init = 0;
439         bi->bi_connection_destroy = 0;
440
441         return 0;
442 }