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