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