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