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