]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/config.c
apply naming scheme to global/database schema
[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, "( OLcfgDbAt:0.1 NAME 'olcDbDirectory' "
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                 "( OLcfgDbAt:1.1 NAME 'olcDbCacheSize' "
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, "( OLcfgDbAt:1.2 NAME 'olcDbCheckpoint' "
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, "( OLcfgDbAt:1.3 NAME 'olcDbConfig' "
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, "( OLcfgDbAt:1.4 NAME 'olcDbNoSync' "
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                 "( OLcfgDbAt:1.5 NAME 'olcDbDirtyRead' "
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                 "( OLcfgDbAt:1.6 NAME 'olcDbIDLcacheSize' "
84                 "DESC 'IDL cache size in IDLs' "
85                 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
86         { "index", "attr> <[pres,eq,approx,sub]", 2, 3, 0, ARG_MAGIC|BDB_INDEX,
87                 bdb_cf_gen, "( OLcfgDbAt:0.2 NAME 'olcDbIndex' "
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                 "( OLcfgDbAt:1.7 NAME 'olcDbLinearIndex' "
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, "( OLcfgDbAt:1.8 NAME 'olcDbLockDetect' "
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                 "( OLcfgDbAt:0.3 NAME 'olcDbMode' "
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, "( OLcfgDbAt:1.9 NAME 'olcDbSearchStack' "
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                 "( OLcfgDbAt:1.10 NAME 'olcDbShmKey' "
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         { "( OLcfgDbOc:1.1 "
119                 "NAME 'olcBdbConfig' "
120                 "DESC 'BDB backend configuration' "
121                 "SUP olcDatabaseConfig "
122                 "MUST olcDbDirectory "
123                 "MAY ( olcDbCacheSize $ olcDbCheckpoint $ olcDbConfig $ "
124                 "olcDbNoSync $ olcDbDirtyRead $ olcDbIDLcacheSize $ "
125                 "olcDbIndex $ olcDbLinearIndex $ olcDbLockDetect $ "
126                 "olcDbMode $ olcDbSearchStack $ olcDbShmKey ) )",
127                         Cft_Database, &bdb_oc },
128         { NULL, 0, NULL }
129 };
130
131 static int
132 bdb_cf_oc(ConfigArgs *c)
133 {
134         if ( c->op == SLAP_CONFIG_EMIT ) {
135                 value_add_one( &c->rvalue_vals, &bdb_oc->soc_cname );
136                 return 0;
137         }
138         return 1;
139 }
140
141 static slap_verbmasks bdb_lockd[] = {
142         { BER_BVC("default"), DB_LOCK_DEFAULT },
143         { BER_BVC("oldest"), DB_LOCK_OLDEST },
144         { BER_BVC("random"), DB_LOCK_RANDOM },
145         { BER_BVC("youngest"), DB_LOCK_YOUNGEST },
146         { BER_BVC("fewest"), DB_LOCK_MINLOCKS },
147         { BER_BVNULL, 0 }
148 };
149
150 static int
151 bdb_cf_gen(ConfigArgs *c)
152 {
153         struct bdb_info *bdb = c->be->be_private;
154         int rc;
155
156         if ( c->op == SLAP_CONFIG_EMIT ) {
157                 rc = 0;
158                 switch( c->type ) {
159                 case BDB_CHKPT:
160                         if (bdb->bi_txn_cp ) {
161                                 char buf[64];
162                                 struct berval bv;
163                                 bv.bv_len = sprintf( buf, "%d %d", bdb->bi_txn_cp_kbyte,
164                                         bdb->bi_txn_cp_min );
165                                 bv.bv_val = buf;
166                                 value_add_one( &c->rvalue_vals, &bv );
167                         } else{
168                                 rc = 1;
169                         }
170                         break;
171
172                 case BDB_DIRECTORY:
173                         if ( bdb->bi_dbenv_home ) {
174                                 c->value_string = ch_strdup( bdb->bi_dbenv_home );
175                         } else {
176                                 rc = 1;
177                         }
178                         break;
179
180                 case BDB_CONFIG:
181                         if ( bdb->bi_db_config ) {
182                                 int i;
183                                 struct berval bv;
184
185                                 bv.bv_val = c->log;
186                                 for (i=0; !BER_BVISNULL(&bdb->bi_db_config[i]); i++) {
187                                         bv.bv_len = sprintf( bv.bv_val, "{%d}%s", i,
188                                                 bdb->bi_db_config[i].bv_val );
189                                         value_add_one( &c->rvalue_vals, &bv );
190                                 }
191                         }
192                         if ( !c->rvalue_vals ) rc = 1;
193                         break;
194
195                 case BDB_NOSYNC:
196                         if ( bdb->bi_dbenv_xflags & DB_TXN_NOSYNC )
197                                 c->value_int = 1;
198                         break;
199                         
200                 case BDB_INDEX:
201                         bdb_attr_index_unparse( bdb, &c->rvalue_vals );
202                         if ( !c->rvalue_vals ) rc = 1;
203                         break;
204
205                 case BDB_LOCKD:
206                         rc = 1;
207                         if ( bdb->bi_lock_detect != DB_LOCK_DEFAULT ) {
208                                 int i;
209                                 for (i=0; !BER_BVISNULL(&bdb_lockd[i].word); i++) {
210                                         if ( bdb->bi_lock_detect == bdb_lockd[i].mask ) {
211                                                 value_add_one( &c->rvalue_vals, &bdb_lockd[i].word );
212                                                 rc = 0;
213                                                 break;
214                                         }
215                                 }
216                         }
217                         break;
218
219                 case BDB_SSTACK:
220                         c->value_int = bdb->bi_search_stack_depth;
221                         break;
222                 }
223                 return rc;
224         }
225         switch( c->type ) {
226         case BDB_CHKPT:
227                 bdb->bi_txn_cp = 1;
228                 bdb->bi_txn_cp_kbyte = strtol( c->argv[1], NULL, 0 );
229                 bdb->bi_txn_cp_min = strtol( c->argv[2], NULL, 0 );
230                 break;
231
232         case BDB_CONFIG: {
233                 char *ptr = c->line + STRLENOF("dbconfig");
234                 struct berval bv;
235                 while (!isspace(*ptr)) ptr++;
236                 while (isspace(*ptr)) ptr++;
237                 
238                 /* If we're just starting up...
239                  */
240                 if ( !bdb->bi_db_is_open ) {
241                         FILE *f;
242                         /* If a DB_CONFIG file exists, or we don't know the path
243                          * to the DB_CONFIG file, ignore these directives
244                          */
245                         if ( bdb->bi_db_has_config || !bdb->bi_db_config_path )
246                                 break;
247                         f = fopen( bdb->bi_db_config_path, "a" );
248                         if ( f ) {
249                                 /* FIXME: EBCDIC probably needs special handling */
250                                 fprintf( f, "%s\n", ptr );
251                                 fclose( f );
252                         }
253                 }
254                 ber_str2bv( ptr, 0, 1, &bv );
255                 ber_bvarray_add( &bdb->bi_db_config, &bv );
256                 }
257                 break;
258
259         case BDB_DIRECTORY: {
260                 FILE *f;
261                 char *ptr;
262
263                 bdb->bi_dbenv_home = c->value_string;
264
265                 /* See if a DB_CONFIG file already exists here */
266                 bdb->bi_db_config_path = ch_malloc( strlen( bdb->bi_dbenv_home ) +
267                         STRLENOF(LDAP_DIRSEP) + STRLENOF("DB_CONFIG") + 1 );
268                 ptr = lutil_strcopy( bdb->bi_db_config_path, bdb->bi_dbenv_home );
269                 *ptr++ = LDAP_DIRSEP[0];
270                 strcpy( ptr, "DB_CONFIG" );
271
272                 f = fopen( bdb->bi_db_config_path, "r" );
273                 if ( f ) {
274                         bdb->bi_db_has_config = 1;
275                         fclose(f);
276                 }
277                 }
278                 break;
279
280         case BDB_NOSYNC:
281                 if ( c->value_int )
282                         bdb->bi_dbenv_xflags |= DB_TXN_NOSYNC;
283                 else
284                         bdb->bi_dbenv_xflags &= ~DB_TXN_NOSYNC;
285                 break;
286
287         case BDB_INDEX:
288                 rc = bdb_attr_index_config( bdb, c->fname, c->lineno,
289                         c->argc - 1, &c->argv[1] );
290
291                 if( rc != LDAP_SUCCESS ) return 1;
292                 break;
293
294         case BDB_LOCKD:
295                 rc = verb_to_mask( c->argv[1], bdb_lockd );
296                 if ( BER_BVISNULL(&bdb_lockd[rc].word) ) {
297                         fprintf( stderr, "%s: "
298                                 "bad policy (%s) in \"lockDetect <policy>\" line\n",
299                                 c->log, c->argv[1] );
300                         return 1;
301                 }
302                 bdb->bi_lock_detect = rc;
303                 break;
304
305         case BDB_SSTACK:
306                 if ( c->value_int < MINIMUM_SEARCH_STACK_DEPTH ) {
307                         fprintf( stderr,
308                 "%s: depth %d too small, using %d\n",
309                         c->log, c->value_int, MINIMUM_SEARCH_STACK_DEPTH );
310                         c->value_int = MINIMUM_SEARCH_STACK_DEPTH;
311                 }
312                 bdb->bi_search_stack_depth = c->value_int;
313                 break;
314         }
315         return 0;
316 }
317
318 int bdb_back_init_cf( BackendInfo *bi )
319 {
320         int rc;
321         bi->bi_cf_table = bdbcfg;
322
323         rc = config_register_schema( bdbcfg, bdbocs );
324         if ( rc ) return rc;
325         bdbcfg[0].ad = slap_schema.si_ad_objectClass;
326         return 0;
327 }