]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/init.c
Fix ITS#3424
[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-2004 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 int
28 ldbm_back_initialize(
29     BackendInfo *bi
30 )
31 {
32         static char *controls[] = {
33                 LDAP_CONTROL_MANAGEDSAIT,
34                 LDAP_CONTROL_VALUESRETURNFILTER,
35                 NULL
36         };
37
38         bi->bi_controls = controls;
39
40         bi->bi_flags |= 
41                 SLAP_BFLAG_INCREMENT |
42 #ifdef LDBM_SUBENTRIES
43                 SLAP_BFLAG_SUBENTRIES |
44 #endif
45                 SLAP_BFLAG_ALIASES |
46                 SLAP_BFLAG_REFERRALS;
47
48         bi->bi_open = ldbm_back_open;
49         bi->bi_config = NULL;
50         bi->bi_close = ldbm_back_close;
51         bi->bi_destroy = ldbm_back_destroy;
52
53         bi->bi_db_init = ldbm_back_db_init;
54         bi->bi_db_config = ldbm_back_db_config;
55         bi->bi_db_open = ldbm_back_db_open;
56         bi->bi_db_close = ldbm_back_db_close;
57         bi->bi_db_destroy = ldbm_back_db_destroy;
58
59         bi->bi_op_bind = ldbm_back_bind;
60         bi->bi_op_unbind = 0;
61         bi->bi_op_search = ldbm_back_search;
62         bi->bi_op_compare = ldbm_back_compare;
63         bi->bi_op_modify = ldbm_back_modify;
64         bi->bi_op_modrdn = ldbm_back_modrdn;
65         bi->bi_op_add = ldbm_back_add;
66         bi->bi_op_delete = ldbm_back_delete;
67         bi->bi_op_abandon = 0;
68
69         bi->bi_extended = ldbm_back_extended;
70
71         bi->bi_entry_release_rw = ldbm_back_entry_release_rw;
72         bi->bi_entry_get_rw = ldbm_back_entry_get;
73         bi->bi_chk_referrals = ldbm_back_referrals;
74         bi->bi_operational = ldbm_back_operational;
75         bi->bi_has_subordinates = ldbm_back_hasSubordinates;
76
77         /*
78          * hooks for slap tools
79          */
80         bi->bi_tool_entry_open = ldbm_tool_entry_open;
81         bi->bi_tool_entry_close = ldbm_tool_entry_close;
82         bi->bi_tool_entry_first = ldbm_tool_entry_first;
83         bi->bi_tool_entry_next = ldbm_tool_entry_next;
84         bi->bi_tool_entry_get = ldbm_tool_entry_get;
85         bi->bi_tool_entry_put = ldbm_tool_entry_put;
86         bi->bi_tool_entry_reindex = ldbm_tool_entry_reindex;
87         bi->bi_tool_sync = ldbm_tool_sync;
88
89         bi->bi_tool_dn2id_get = 0;
90         bi->bi_tool_id2entry_get = 0;
91         bi->bi_tool_entry_modify = 0;
92
93         bi->bi_connection_init = 0;
94         bi->bi_connection_destroy = 0;
95
96         return 0;
97 }
98
99 int
100 ldbm_back_destroy(
101     BackendInfo *bi
102 )
103 {
104         return 0;
105 }
106
107 int
108 ldbm_back_open(
109     BackendInfo *bi
110 )
111 {
112         int rc;
113
114         /* initialize the underlying database system */
115         rc = ldbm_initialize( NULL );
116         return rc;
117 }
118
119 int
120 ldbm_back_close(
121     BackendInfo *bi
122 )
123 {
124         /* terminate the underlying database system */
125         ldbm_shutdown();
126         return 0;
127 }
128
129 int
130 ldbm_back_db_init(
131     Backend     *be
132 )
133 {
134         struct ldbminfo *li;
135
136         /* allocate backend-database-specific stuff */
137         li = (struct ldbminfo *) ch_calloc( 1, sizeof(struct ldbminfo) );
138
139         /* arrange to read nextid later (on first request for it) */
140         li->li_nextid = NOID;
141
142         /* default cache size */
143         li->li_cache.c_maxsize = DEFAULT_CACHE_SIZE;
144
145         /* default database cache size */
146         li->li_dbcachesize = DEFAULT_DBCACHE_SIZE;
147
148         /* default db mode is with locking */ 
149         li->li_dblocking = 1;
150
151         /* default db mode is with write synchronization */ 
152         li->li_dbwritesync = 1;
153
154         /* default file creation mode */
155         li->li_mode = SLAPD_DEFAULT_DB_MODE;
156
157         /* default database directory */
158         li->li_directory = ch_strdup( SLAPD_DEFAULT_DB_DIR );
159
160         /* DB_ENV environment pointer for DB3 */
161         li->li_dbenv = 0;
162
163         /* envdirok is turned on by ldbm_initialize_env if DB3 */
164         li->li_envdirok = 0;
165
166         /* syncfreq is 0 if disabled, or # seconds */
167         li->li_dbsyncfreq = 0;
168
169         /* wait up to dbsyncwaitn times if server is busy */
170         li->li_dbsyncwaitn = 12;
171
172         /* delay interval */
173         li->li_dbsyncwaitinterval = 5;
174
175         /* flag to notify ldbm_cache_sync_daemon to shut down */
176         li->li_dbshutdown = 0;
177
178         /* initialize various mutex locks & condition variables */
179         ldap_pvt_thread_rdwr_init( &li->li_giant_rwlock );
180         ldap_pvt_thread_mutex_init( &li->li_cache.c_mutex );
181         ldap_pvt_thread_mutex_init( &li->li_dbcache_mutex );
182         ldap_pvt_thread_cond_init( &li->li_dbcache_cv );
183
184         be->be_private = li;
185
186         return 0;
187 }
188
189 int
190 ldbm_back_db_open(
191     BackendDB   *be
192 )
193 {
194         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
195         li->li_dbenv = ldbm_initialize_env( li->li_directory,
196                 li->li_dbcachesize, &li->li_envdirok );
197
198         /* sync thread */
199         if ( li->li_dbsyncfreq > 0 )
200         {
201                 int rc;
202                 rc = ldap_pvt_thread_create( &li->li_dbsynctid,
203                         0, ldbm_cache_sync_daemon, (void*)be );
204
205                 if ( rc != 0 )
206                 {
207                         Debug(  LDAP_DEBUG_ANY,
208                                 "sync ldap_pvt_thread_create failed (%d)\n", rc, 0, 0 );
209                         return 1;
210                 }
211         }
212
213         return 0;
214 }
215
216 int
217 ldbm_back_db_destroy(
218     BackendDB   *be
219 )
220 {
221         /* should free/destroy every in be_private */
222         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
223
224         if (li->li_dbenv)
225             ldbm_shutdown_env(li->li_dbenv);
226
227         free( li->li_directory );
228         attr_index_destroy( li->li_attrs );
229
230         ldap_pvt_thread_rdwr_destroy( &li->li_giant_rwlock );
231         ldap_pvt_thread_mutex_destroy( &li->li_cache.c_mutex );
232         ldap_pvt_thread_mutex_destroy( &li->li_dbcache_mutex );
233         ldap_pvt_thread_cond_destroy( &li->li_dbcache_cv );
234
235         free( be->be_private );
236         be->be_private = NULL;
237
238         return 0;
239 }
240
241 #if SLAPD_LDBM == SLAPD_MOD_DYNAMIC
242
243 /* conditionally define the init_module() function */
244 SLAP_BACKEND_INIT_MODULE( ldbm )
245
246 #endif /* SLAPD_LDBM == SLAPD_MOD_DYNAMIC */
247
248