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