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