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