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