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