]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/init.c
ITS#2888 don't return LDAP_SIZELIMIT_EXCEEDED prematurely
[openldap] / servers / slapd / back-ldbm / init.c
1 /* init.c - initialize ldbm backend */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2003 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20
21 #include <ac/string.h>
22 #include <ac/socket.h>
23
24 #include "slap.h"
25 #include "back-ldbm.h"
26
27 #ifdef SLAPD_LDBM_DYNAMIC
28
29 int init_module(int argc, char *argv[]) {
30     BackendInfo bi;
31
32     memset( &bi, '\0', sizeof(bi) );
33     bi.bi_type = "ldbm";
34     bi.bi_init = ldbm_back_initialize;
35
36     backend_add(&bi);
37     return 0;
38 }
39
40 #endif /* SLAPD_LDBM_DYNAMIC */
41
42 int
43 ldbm_back_initialize(
44     BackendInfo *bi
45 )
46 {
47         static char *controls[] = {
48                 LDAP_CONTROL_MANAGEDSAIT,
49                 LDAP_CONTROL_VALUESRETURNFILTER,
50                 NULL
51         };
52
53         bi->bi_controls = controls;
54
55         bi->bi_open = ldbm_back_open;
56         bi->bi_config = NULL;
57         bi->bi_close = ldbm_back_close;
58         bi->bi_destroy = ldbm_back_destroy;
59
60         bi->bi_db_init = ldbm_back_db_init;
61         bi->bi_db_config = ldbm_back_db_config;
62         bi->bi_db_open = ldbm_back_db_open;
63         bi->bi_db_close = ldbm_back_db_close;
64         bi->bi_db_destroy = ldbm_back_db_destroy;
65
66         bi->bi_op_bind = ldbm_back_bind;
67         bi->bi_op_unbind = 0;
68         bi->bi_op_search = ldbm_back_search;
69         bi->bi_op_compare = ldbm_back_compare;
70         bi->bi_op_modify = ldbm_back_modify;
71         bi->bi_op_modrdn = ldbm_back_modrdn;
72         bi->bi_op_add = ldbm_back_add;
73         bi->bi_op_delete = ldbm_back_delete;
74         bi->bi_op_abandon = 0;
75
76         bi->bi_extended = ldbm_back_extended;
77
78         bi->bi_entry_release_rw = ldbm_back_entry_release_rw;
79         bi->bi_entry_get_rw = ldbm_back_entry_get;
80         bi->bi_chk_referrals = ldbm_back_referrals;
81         bi->bi_operational = ldbm_back_operational;
82         bi->bi_has_subordinates = ldbm_back_hasSubordinates;
83
84         /*
85          * hooks for slap tools
86          */
87         bi->bi_tool_entry_open = ldbm_tool_entry_open;
88         bi->bi_tool_entry_close = ldbm_tool_entry_close;
89         bi->bi_tool_entry_first = ldbm_tool_entry_first;
90         bi->bi_tool_entry_next = ldbm_tool_entry_next;
91         bi->bi_tool_entry_get = ldbm_tool_entry_get;
92         bi->bi_tool_entry_put = ldbm_tool_entry_put;
93         bi->bi_tool_entry_reindex = ldbm_tool_entry_reindex;
94         bi->bi_tool_sync = ldbm_tool_sync;
95
96         bi->bi_tool_dn2id_get = 0;
97         bi->bi_tool_id2entry_get = 0;
98         bi->bi_tool_entry_modify = 0;
99
100         bi->bi_connection_init = 0;
101         bi->bi_connection_destroy = 0;
102
103         return 0;
104 }
105
106 int
107 ldbm_back_destroy(
108     BackendInfo *bi
109 )
110 {
111         return 0;
112 }
113
114 int
115 ldbm_back_open(
116     BackendInfo *bi
117 )
118 {
119         int rc;
120
121         /* initialize the underlying database system */
122         rc = ldbm_initialize( NULL );
123         return rc;
124 }
125
126 int
127 ldbm_back_close(
128     BackendInfo *bi
129 )
130 {
131         /* terminate the underlying database system */
132         ldbm_shutdown();
133         return 0;
134 }
135
136 int
137 ldbm_back_db_init(
138     Backend     *be
139 )
140 {
141         struct ldbminfo *li;
142
143         /* indicate system schema supported */
144         be->be_flags |= 
145                 SLAP_BFLAG_INCREMENT |
146 #ifdef LDBM_SUBENTRIES
147                 SLAP_BFLAG_SUBENTRIES |
148 #endif
149                 SLAP_BFLAG_ALIASES |
150                 SLAP_BFLAG_REFERRALS;
151
152         /* allocate backend-database-specific stuff */
153         li = (struct ldbminfo *) ch_calloc( 1, sizeof(struct ldbminfo) );
154
155         /* arrange to read nextid later (on first request for it) */
156         li->li_nextid = NOID;
157
158         /* default cache size */
159         li->li_cache.c_maxsize = DEFAULT_CACHE_SIZE;
160
161         /* default database cache size */
162         li->li_dbcachesize = DEFAULT_DBCACHE_SIZE;
163
164         /* default db mode is with locking */ 
165         li->li_dblocking = 1;
166
167         /* default db mode is with write synchronization */ 
168         li->li_dbwritesync = 1;
169
170         /* default file creation mode */
171         li->li_mode = SLAPD_DEFAULT_DB_MODE;
172
173         /* default database directory */
174         li->li_directory = ch_strdup( SLAPD_DEFAULT_DB_DIR );
175
176         /* DB_ENV environment pointer for DB3 */
177         li->li_dbenv = 0;
178
179         /* envdirok is turned on by ldbm_initialize_env if DB3 */
180         li->li_envdirok = 0;
181
182         /* syncfreq is 0 if disabled, or # seconds */
183         li->li_dbsyncfreq = 0;
184
185         /* wait up to dbsyncwaitn times if server is busy */
186         li->li_dbsyncwaitn = 12;
187
188         /* delay interval */
189         li->li_dbsyncwaitinterval = 5;
190
191         /* flag to notify ldbm_cache_sync_daemon to shut down */
192         li->li_dbshutdown = 0;
193
194         /* initialize various mutex locks & condition variables */
195         ldap_pvt_thread_rdwr_init( &li->li_giant_rwlock );
196         ldap_pvt_thread_mutex_init( &li->li_cache.c_mutex );
197         ldap_pvt_thread_mutex_init( &li->li_dbcache_mutex );
198         ldap_pvt_thread_cond_init( &li->li_dbcache_cv );
199
200         be->be_private = li;
201
202         return 0;
203 }
204
205 int
206 ldbm_back_db_open(
207     BackendDB   *be
208 )
209 {
210         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
211         li->li_dbenv = ldbm_initialize_env( li->li_directory,
212                 li->li_dbcachesize, &li->li_envdirok );
213
214         /* sync thread */
215         if ( li->li_dbsyncfreq > 0 )
216         {
217                 int rc;
218                 rc = ldap_pvt_thread_create( &li->li_dbsynctid,
219                         0, ldbm_cache_sync_daemon, (void*)be );
220
221                 if ( rc != 0 )
222                 {
223 #ifdef NEW_LOGGING
224                         LDAP_LOG ( BACK_LDBM, ERR, "ldbm_back_db_open: sync "
225                                 "ldap_pvt_thread_create failed (%d)\n", rc, 0, 0 );
226 #else   
227                         Debug(  LDAP_DEBUG_ANY,
228                                 "sync ldap_pvt_thread_create failed (%d)\n", rc, 0, 0 );
229 #endif
230                         return 1;
231                 }
232         }
233
234         return 0;
235 }
236
237 int
238 ldbm_back_db_destroy(
239     BackendDB   *be
240 )
241 {
242         /* should free/destroy every in be_private */
243         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
244
245         if (li->li_dbenv)
246             ldbm_shutdown_env(li->li_dbenv);
247
248         free( li->li_directory );
249         attr_index_destroy( li->li_attrs );
250
251         ldap_pvt_thread_rdwr_destroy( &li->li_giant_rwlock );
252         ldap_pvt_thread_mutex_destroy( &li->li_cache.c_mutex );
253         ldap_pvt_thread_mutex_destroy( &li->li_dbcache_mutex );
254         ldap_pvt_thread_cond_destroy( &li->li_dbcache_cv );
255
256         free( be->be_private );
257         be->be_private = NULL;
258
259         return 0;
260 }