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