]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/config.c
malloc cleanup
[openldap] / servers / slapd / back-bdb / config.c
1 /* config.c - bdb backend configuration file routine */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2000-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 #include <ac/string.h>
21
22 #include "back-bdb.h"
23
24 #include "config.h"
25
26 #include "lutil.h"
27
28 #ifdef DB_DIRTY_READ
29 #       define  SLAP_BDB_ALLOW_DIRTY_READ
30 #endif
31
32 static ObjectClass *bdb_oc;
33
34 static ConfigDriver bdb_cf_oc, bdb_cf_gen;
35
36 enum {
37         BDB_CHKPT = 1,
38         BDB_CONFIG,
39         BDB_DIRECTORY,
40         BDB_NOSYNC,
41         BDB_DIRTYR,
42         BDB_INDEX,
43         BDB_LOCKD,
44         BDB_SSTACK
45 };
46
47 static ConfigTable bdbcfg[] = {
48         { "", "", 0, 0, 0, ARG_MAGIC,
49                 bdb_cf_oc, NULL, NULL, NULL },
50         { "directory", "dir", 2, 2, 0, ARG_STRING|ARG_MAGIC|BDB_DIRECTORY,
51                 bdb_cf_gen, "( OLcfgAt:1.1 NAME 'dbDirectory' "
52                         "DESC 'Directory for database content' "
53                         "EQUALITY caseIgnoreMatch "
54                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
55         { "cachesize", "size", 2, 2, 0, ARG_INT|ARG_OFFSET,
56                 (void *)offsetof(struct bdb_info, bi_cache.c_maxsize),
57                 "( OLcfgAt:1.2 NAME 'dbCacheSize' "
58                         "DESC 'Entry cache size in entries' "
59                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
60         { "checkpoint", "kbyte> <min", 3, 3, 0, ARG_MAGIC|BDB_CHKPT,
61                 bdb_cf_gen, "( OLcfgAt:1.3 NAME 'dbCheckpoint' "
62                         "DESC 'Database checkpoint interval in kbytes and minutes' "
63                         "SYNTAX OMsDirectoryString SINGLE-VALUE )",NULL, NULL },
64         { "dbconfig", "DB_CONFIG setting", 3, 0, 0, ARG_MAGIC|BDB_CONFIG,
65                 bdb_cf_gen, "( OLcfgAt:1.13 NAME 'dbConfig' "
66                         "DESC 'BerkeleyDB DB_CONFIG configuration directives' "
67                         "SYNTAX OMsDirectoryString )",NULL, NULL },
68         { "dbnosync", NULL, 1, 2, 0, ARG_ON_OFF|ARG_MAGIC|BDB_NOSYNC,
69                 bdb_cf_gen, "( OLcfgAt:1.4 NAME 'dbNoSync' "
70                         "DESC 'Disable synchronous database writes' "
71                         "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
72         { "dirtyread", NULL, 1, 2, 0,
73 #ifdef SLAP_BDB_ALLOW_DIRTY_READ
74                 ARG_ON_OFF|ARG_MAGIC|BDB_DIRTYR, bdb_cf_gen,
75 #else
76                 ARG_IGNORED, NULL,
77 #endif
78                 "( OLcfgAt:1.5 NAME 'dbDirtyRead' "
79                 "DESC 'Allow reads of uncommitted data' "
80                 "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
81         { "idlcachesize", "size", 2, 2, 0, ARG_INT|ARG_OFFSET,
82                 (void *)offsetof(struct bdb_info,bi_idl_cache_max_size),
83                 "( OLcfgAt:1.6 NAME 'dbIDLcacheSize' "
84                 "DESC 'IDL cache size in IDLs' "
85                 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
86         { "index", "attr> <[pres,eq,approx,sub]", 3, 3, 0, ARG_MAGIC|BDB_INDEX,
87                 bdb_cf_gen, "( OLcfgAt:1.7 NAME 'dbIndex' "
88                 "DESC 'Attribute index parameters' "
89                 "SYNTAX OMsDirectoryString )", NULL, NULL },
90         { "linearindex", NULL, 1, 2, 0, ARG_ON_OFF|ARG_OFFSET,
91                 (void *)offsetof(struct bdb_info, bi_linear_index), 
92                 "( OLcfgAt:1.8 NAME 'dbLinearIndex' "
93                 "DESC 'Index attributes one at a time' "
94                 "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
95         { "lockdetect", "policy", 2, 2, 0, ARG_MAGIC|BDB_LOCKD,
96                 bdb_cf_gen, "( OLcfgAt:1.9 NAME 'dbLockDetect' "
97                 "DESC 'Deadlock detection algorithm' "
98                 "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
99         { "mode", "mode", 2, 2, 0, ARG_INT|ARG_OFFSET,
100                 (void *)offsetof(struct bdb_info, bi_dbenv_mode),
101                 "( OLcfgAt:1.10 NAME 'dbMode' "
102                 "DESC 'Unix permissions of database files' "
103                 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
104         { "searchstack", "depth", 2, 2, 0, ARG_INT|ARG_MAGIC|BDB_SSTACK,
105                 bdb_cf_gen, "( OLcfgAt:1.11 NAME 'dbSearchStack' "
106                 "DESC 'Depth of search stack in IDLs' "
107                 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
108         { "shm_key", "key", 2, 2, 0, ARG_INT|ARG_OFFSET,
109                 (void *)offsetof(struct bdb_info, bi_shm_key), 
110                 "( OLcfgAt:1.12 NAME 'dbShmKey' "
111                 "DESC 'Key for shared memory region' "
112                 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
113         { NULL, NULL, 0, 0, 0, ARG_IGNORED,
114                 NULL, NULL, NULL, NULL }
115 };
116
117 static ConfigOCs bdbocs[] = {
118         { "( OLcfgOc:1.1 "
119                 "NAME 'bdbConfig' "
120                 "DESC 'BDB backend configuration' "
121                 "SUP olcDatabaseConfig "
122                 "MUST dbDirectory "
123                 "MAY ( dbCacheSize $ dbCheckpoint $ dbConfig $ dbNoSync $ "
124                  "dbDirtyRead $ dbIDLcacheSize $ dbIndex $ dbLinearIndex $ "
125                  "dbLockDetect $ dbMode $ dbSearchStack $ dbShmKey ) )",
126                         Cft_Database, &bdb_oc },
127         { NULL, 0, NULL }
128 };
129
130 static int
131 bdb_cf_oc(ConfigArgs *c)
132 {
133         if ( c->op == SLAP_CONFIG_EMIT ) {
134                 value_add_one( &c->rvalue_vals, &bdb_oc->soc_cname );
135                 return 0;
136         }
137         return 1;
138 }
139
140 static slap_verbmasks bdb_lockd[] = {
141         { BER_BVC("default"), DB_LOCK_DEFAULT },
142         { BER_BVC("oldest"), DB_LOCK_OLDEST },
143         { BER_BVC("random"), DB_LOCK_RANDOM },
144         { BER_BVC("youngest"), DB_LOCK_YOUNGEST },
145         { BER_BVC("fewest"), DB_LOCK_MINLOCKS },
146         { BER_BVNULL, 0 }
147 };
148
149 static int
150 bdb_cf_gen(ConfigArgs *c)
151 {
152         struct bdb_info *bdb = c->be->be_private;
153         int rc;
154
155         if ( c->op == SLAP_CONFIG_EMIT ) {
156                 rc = 0;
157                 switch( c->type ) {
158                 case BDB_CHKPT:
159                         if (bdb->bi_txn_cp ) {
160                                 char buf[64];
161                                 struct berval bv;
162                                 bv.bv_len = sprintf( buf, "%d %d", bdb->bi_txn_cp_kbyte,
163                                         bdb->bi_txn_cp_min );
164                                 bv.bv_val = buf;
165                                 value_add_one( &c->rvalue_vals, &bv );
166                         } else{
167                                 rc = 1;
168                         }
169                         break;
170
171                 case BDB_DIRECTORY:
172                         if ( bdb->bi_dbenv_home ) {
173                                 c->value_string = ch_strdup( bdb->bi_dbenv_home );
174                         } else {
175                                 rc = 1;
176                         }
177                         break;
178
179                 case BDB_CONFIG:
180                         if ( bdb->bi_db_config ) {
181                                 int i;
182                                 struct berval bv;
183
184                                 bv.bv_val = c->log;
185                                 for (i=0; !BER_BVISNULL(&bdb->bi_db_config[i]); i++) {
186                                         bv.bv_len = sprintf( bv.bv_val, "{%d}%s", i,
187                                                 bdb->bi_db_config[i].bv_val );
188                                         value_add_one( &c->rvalue_vals, &bv );
189                                 }
190                         }
191                         if ( !c->rvalue_vals ) rc = 1;
192                         break;
193
194                 case BDB_NOSYNC:
195                         if ( bdb->bi_dbenv_xflags & DB_TXN_NOSYNC )
196                                 c->value_int = 1;
197                         break;
198                         
199                 case BDB_INDEX:
200                         bdb_attr_index_unparse( bdb, &c->rvalue_vals );
201                         if ( !c->rvalue_vals ) rc = 1;
202                         break;
203
204                 case BDB_LOCKD:
205                         rc = 1;
206                         if ( bdb->bi_lock_detect != DB_LOCK_DEFAULT ) {
207                                 int i;
208                                 for (i=0; !BER_BVISNULL(&bdb_lockd[i].word); i++) {
209                                         if ( bdb->bi_lock_detect == bdb_lockd[i].mask ) {
210                                                 value_add_one( &c->rvalue_vals, &bdb_lockd[i].word );
211                                                 rc = 0;
212                                                 break;
213                                         }
214                                 }
215                         }
216                         break;
217
218                 case BDB_SSTACK:
219                         c->value_int = bdb->bi_search_stack_depth;
220                         break;
221                 }
222                 return rc;
223         }
224         switch( c->type ) {
225         case BDB_CHKPT:
226                 bdb->bi_txn_cp = 1;
227                 bdb->bi_txn_cp_kbyte = strtol( c->argv[1], NULL, 0 );
228                 bdb->bi_txn_cp_min = strtol( c->argv[2], NULL, 0 );
229                 break;
230
231         case BDB_CONFIG: {
232                 char *ptr = c->line + STRLENOF("dbconfig");
233                 struct berval bv;
234                 while (!isspace(*ptr)) ptr++;
235                 while (isspace(*ptr)) ptr++;
236                 
237                 /* If we're just starting up...
238                  */
239                 if ( !bdb->bi_db_is_open ) {
240                         FILE *f;
241                         /* If a DB_CONFIG file exists, or we don't know the path
242                          * to the DB_CONFIG file, ignore these directives
243                          */
244                         if ( bdb->bi_db_has_config || !bdb->bi_db_config_path )
245                                 break;
246                         f = fopen( bdb->bi_db_config_path, "a" );
247                         if ( f ) {
248                                 /* FIXME: EBCDIC probably needs special handling */
249                                 fprintf( f, "%s\n", ptr );
250                                 fclose( f );
251                         }
252                 }
253                 ber_str2bv( ptr, 0, 1, &bv );
254                 ber_bvarray_add( &bdb->bi_db_config, &bv );
255                 }
256                 break;
257
258         case BDB_DIRECTORY: {
259                 FILE *f;
260                 char *ptr;
261
262                 bdb->bi_dbenv_home = c->value_string;
263
264                 /* See if a DB_CONFIG file already exists here */
265                 bdb->bi_db_config_path = ch_malloc( strlen( bdb->bi_dbenv_home ) +
266                         STRLENOF(LDAP_DIRSEP) + STRLENOF("DB_CONFIG") + 1 );
267                 ptr = lutil_strcopy( bdb->bi_db_config_path, bdb->bi_dbenv_home );
268                 *ptr++ = LDAP_DIRSEP[0];
269                 strcpy( ptr, "DB_CONFIG" );
270
271                 f = fopen( bdb->bi_db_config_path, "r" );
272                 if ( f ) {
273                         bdb->bi_db_has_config = 1;
274                         fclose(f);
275                 }
276                 }
277                 break;
278
279         case BDB_NOSYNC:
280                 if ( c->value_int )
281                         bdb->bi_dbenv_xflags |= DB_TXN_NOSYNC;
282                 else
283                         bdb->bi_dbenv_xflags &= ~DB_TXN_NOSYNC;
284                 break;
285
286         case BDB_INDEX:
287                 rc = bdb_attr_index_config( bdb, c->fname, c->lineno,
288                         c->argc - 1, &c->argv[1] );
289
290                 if( rc != LDAP_SUCCESS ) return 1;
291                 break;
292
293         case BDB_LOCKD:
294                 rc = verb_to_mask( c->argv[1], bdb_lockd );
295                 if ( BER_BVISNULL(&bdb_lockd[rc].word) ) {
296                         fprintf( stderr, "%s: "
297                                 "bad policy (%s) in \"lockDetect <policy>\" line\n",
298                                 c->log, c->argv[1] );
299                         return 1;
300                 }
301                 bdb->bi_lock_detect = rc;
302                 break;
303
304         case BDB_SSTACK:
305                 if ( c->value_int < MINIMUM_SEARCH_STACK_DEPTH ) {
306                         fprintf( stderr,
307                 "%s: depth %d too small, using %d\n",
308                         c->log, c->value_int, MINIMUM_SEARCH_STACK_DEPTH );
309                         c->value_int = MINIMUM_SEARCH_STACK_DEPTH;
310                 }
311                 bdb->bi_search_stack_depth = c->value_int;
312                 break;
313         }
314         return 0;
315 }
316
317 int bdb_back_init_cf( BackendInfo *bi )
318 {
319         int rc;
320         bi->bi_cf_table = bdbcfg;
321
322         rc = config_register_schema( bdbcfg, bdbocs );
323         if ( rc ) return rc;
324         bdbcfg[0].ad = slap_schema.si_ad_objectClass;
325         return 0;
326 }