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