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