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