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