]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/config.c
ITS#5122 add ARG_UINT config type for unsigned ints
[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-2007 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/ctype.h>
21 #include <ac/string.h>
22
23 #include "back-bdb.h"
24
25 #include "config.h"
26
27 #include "lutil.h"
28 #include "ldap_rq.h"
29
30 #ifdef DB_DIRTY_READ
31 #       define  SLAP_BDB_ALLOW_DIRTY_READ
32 #endif
33
34 #define bdb_cf_gen              BDB_SYMBOL(cf_gen)
35 #define bdb_cf_cleanup          BDB_SYMBOL(cf_cleanup)
36 #define bdb_checkpoint          BDB_SYMBOL(checkpoint)
37 #define bdb_online_index        BDB_SYMBOL(online_index)
38
39 static ConfigDriver bdb_cf_gen;
40
41 enum {
42         BDB_CHKPT = 1,
43         BDB_CONFIG,
44         BDB_DIRECTORY,
45         BDB_NOSYNC,
46         BDB_DIRTYR,
47         BDB_INDEX,
48         BDB_LOCKD,
49         BDB_SSTACK
50 };
51
52 static ConfigTable bdbcfg[] = {
53         { "directory", "dir", 2, 2, 0, ARG_STRING|ARG_MAGIC|BDB_DIRECTORY,
54                 bdb_cf_gen, "( OLcfgDbAt:0.1 NAME 'olcDbDirectory' "
55                         "DESC 'Directory for database content' "
56                         "EQUALITY caseIgnoreMatch "
57                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
58         { "cachefree", "size", 2, 2, 0, ARG_UINT|ARG_OFFSET,
59                 (void *)offsetof(struct bdb_info, bi_cache.c_minfree),
60                 "( OLcfgDbAt:1.11 NAME 'olcDbCacheFree' "
61                         "DESC 'Number of extra entries to free when max is reached' "
62                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
63         { "cachesize", "size", 2, 2, 0, ARG_UINT|ARG_OFFSET,
64                 (void *)offsetof(struct bdb_info, bi_cache.c_maxsize),
65                 "( OLcfgDbAt:1.1 NAME 'olcDbCacheSize' "
66                         "DESC 'Entry cache size in entries' "
67                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
68         { "checkpoint", "kbyte> <min", 3, 3, 0, ARG_MAGIC|BDB_CHKPT,
69                 bdb_cf_gen, "( OLcfgDbAt:1.2 NAME 'olcDbCheckpoint' "
70                         "DESC 'Database checkpoint interval in kbytes and minutes' "
71                         "SYNTAX OMsDirectoryString SINGLE-VALUE )",NULL, NULL },
72         { "dbconfig", "DB_CONFIG setting", 1, 0, 0, ARG_MAGIC|BDB_CONFIG,
73                 bdb_cf_gen, "( OLcfgDbAt:1.3 NAME 'olcDbConfig' "
74                         "DESC 'BerkeleyDB DB_CONFIG configuration directives' "
75                         "SYNTAX OMsIA5String X-ORDERED 'VALUES' )", NULL, NULL },
76         { "dbnosync", NULL, 1, 2, 0, ARG_ON_OFF|ARG_MAGIC|BDB_NOSYNC,
77                 bdb_cf_gen, "( OLcfgDbAt:1.4 NAME 'olcDbNoSync' "
78                         "DESC 'Disable synchronous database writes' "
79                         "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
80         { "dirtyread", NULL, 1, 2, 0,
81 #ifdef SLAP_BDB_ALLOW_DIRTY_READ
82                 ARG_ON_OFF|ARG_MAGIC|BDB_DIRTYR, bdb_cf_gen,
83 #else
84                 ARG_IGNORED, NULL,
85 #endif
86                 "( OLcfgDbAt:1.5 NAME 'olcDbDirtyRead' "
87                 "DESC 'Allow reads of uncommitted data' "
88                 "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
89         { "dncachesize", "size", 2, 2, 0, ARG_UINT|ARG_OFFSET,
90                 (void *)offsetof(struct bdb_info, bi_cache.c_eimax),
91                 "( OLcfgDbAt:1.12 NAME 'olcDbDNcacheSize' "
92                         "DESC 'DN cache size' "
93                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
94         { "idlcachesize", "size", 2, 2, 0, ARG_UINT|ARG_OFFSET,
95                 (void *)offsetof(struct bdb_info, bi_idl_cache_max_size),
96                 "( OLcfgDbAt:1.6 NAME 'olcDbIDLcacheSize' "
97                 "DESC 'IDL cache size in IDLs' "
98                 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
99         { "index", "attr> <[pres,eq,approx,sub]", 2, 3, 0, ARG_MAGIC|BDB_INDEX,
100                 bdb_cf_gen, "( OLcfgDbAt:0.2 NAME 'olcDbIndex' "
101                 "DESC 'Attribute index parameters' "
102                 "EQUALITY caseIgnoreMatch "
103                 "SYNTAX OMsDirectoryString )", NULL, NULL },
104         { "linearindex", NULL, 1, 2, 0, ARG_ON_OFF|ARG_OFFSET,
105                 (void *)offsetof(struct bdb_info, bi_linear_index), 
106                 "( OLcfgDbAt:1.7 NAME 'olcDbLinearIndex' "
107                 "DESC 'Index attributes one at a time' "
108                 "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
109         { "lockdetect", "policy", 2, 2, 0, ARG_MAGIC|BDB_LOCKD,
110                 bdb_cf_gen, "( OLcfgDbAt:1.8 NAME 'olcDbLockDetect' "
111                 "DESC 'Deadlock detection algorithm' "
112                 "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
113         { "mode", "mode", 2, 2, 0, ARG_INT|ARG_OFFSET,
114                 (void *)offsetof(struct bdb_info, bi_dbenv_mode),
115                 "( OLcfgDbAt:0.3 NAME 'olcDbMode' "
116                 "DESC 'Unix permissions of database files' "
117                 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
118         { "searchstack", "depth", 2, 2, 0, ARG_INT|ARG_MAGIC|BDB_SSTACK,
119                 bdb_cf_gen, "( OLcfgDbAt:1.9 NAME 'olcDbSearchStack' "
120                 "DESC 'Depth of search stack in IDLs' "
121                 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
122         { "shm_key", "key", 2, 2, 0, ARG_LONG|ARG_OFFSET,
123                 (void *)offsetof(struct bdb_info, bi_shm_key), 
124                 "( OLcfgDbAt:1.10 NAME 'olcDbShmKey' "
125                 "DESC 'Key for shared memory region' "
126                 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
127         { NULL, NULL, 0, 0, 0, ARG_IGNORED,
128                 NULL, NULL, NULL, NULL }
129 };
130
131 static ConfigOCs bdbocs[] = {
132         {
133 #ifdef BDB_HIER
134                 "( OLcfgDbOc:1.2 "
135                 "NAME 'olcHdbConfig' "
136                 "DESC 'HDB backend configuration' "
137 #else
138                 "( OLcfgDbOc:1.1 "
139                 "NAME 'olcBdbConfig' "
140                 "DESC 'BDB backend configuration' "
141 #endif
142                 "SUP olcDatabaseConfig "
143                 "MUST olcDbDirectory "
144                 "MAY ( olcDbCacheSize $ olcDbCheckpoint $ olcDbConfig $ "
145                 "olcDbNoSync $ olcDbDirtyRead $ olcDbIDLcacheSize $ "
146                 "olcDbIndex $ olcDbLinearIndex $ olcDbLockDetect $ "
147                 "olcDbMode $ olcDbSearchStack $ olcDbShmKey $ "
148                 "olcDbCacheFree $ olcDbDNcacheSize ) )",
149                         Cft_Database, bdbcfg },
150         { NULL, 0, NULL }
151 };
152
153 static slap_verbmasks bdb_lockd[] = {
154         { BER_BVC("default"), DB_LOCK_DEFAULT },
155         { BER_BVC("oldest"), DB_LOCK_OLDEST },
156         { BER_BVC("random"), DB_LOCK_RANDOM },
157         { BER_BVC("youngest"), DB_LOCK_YOUNGEST },
158         { BER_BVC("fewest"), DB_LOCK_MINLOCKS },
159         { BER_BVNULL, 0 }
160 };
161
162 /* perform periodic checkpoints */
163 static void *
164 bdb_checkpoint( void *ctx, void *arg )
165 {
166         struct re_s *rtask = arg;
167         struct bdb_info *bdb = rtask->arg;
168         
169         TXN_CHECKPOINT( bdb->bi_dbenv, bdb->bi_txn_cp_kbyte,
170                 bdb->bi_txn_cp_min, 0 );
171         ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
172         ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
173         ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
174         return NULL;
175 }
176
177 /* reindex entries on the fly */
178 static void *
179 bdb_online_index( void *ctx, void *arg )
180 {
181         struct re_s *rtask = arg;
182         BackendDB *be = rtask->arg;
183         struct bdb_info *bdb = be->be_private;
184
185         Connection conn = {0};
186         OperationBuffer opbuf;
187         Operation *op;
188
189         DBC *curs;
190         DBT key, data;
191         DB_TXN *txn;
192         DB_LOCK lock;
193         BDB_LOCKER locker;
194         ID id, nid;
195         EntryInfo *ei;
196         int rc, getnext = 1;
197         int i;
198
199         connection_fake_init( &conn, &opbuf, ctx );
200         op = &opbuf.ob_op;
201
202         op->o_bd = be;
203
204         DBTzero( &key );
205         DBTzero( &data );
206         
207         id = 1;
208         key.data = &nid;
209         key.size = key.ulen = sizeof(ID);
210         key.flags = DB_DBT_USERMEM;
211
212         data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
213         data.dlen = data.ulen = 0;
214
215         while ( 1 ) {
216                 if ( slapd_shutdown )
217                         break;
218
219                 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &txn, bdb->bi_db_opflags );
220                 if ( rc ) 
221                         break;
222                 locker = TXN_ID( txn );
223                 if ( getnext ) {
224                         getnext = 0;
225                         BDB_ID2DISK( id, &nid );
226                         rc = bdb->bi_id2entry->bdi_db->cursor(
227                                 bdb->bi_id2entry->bdi_db, txn, &curs, bdb->bi_db_opflags );
228                         if ( rc ) {
229                                 TXN_ABORT( txn );
230                                 break;
231                         }
232                         rc = curs->c_get( curs, &key, &data, DB_SET_RANGE );
233                         curs->c_close( curs );
234                         if ( rc ) {
235                                 TXN_ABORT( txn );
236                                 if ( rc == DB_NOTFOUND )
237                                         rc = 0;
238                                 if ( rc == DB_LOCK_DEADLOCK ) {
239                                         ldap_pvt_thread_yield();
240                                         continue;
241                                 }
242                                 break;
243                         }
244                         BDB_DISK2ID( &nid, &id );
245                 }
246
247                 ei = NULL;
248                 rc = bdb_cache_find_id( op, txn, id, &ei, 0, locker, &lock );
249                 if ( rc ) {
250                         TXN_ABORT( txn );
251                         if ( rc == DB_LOCK_DEADLOCK ) {
252                                 ldap_pvt_thread_yield();
253                                 continue;
254                         }
255                         if ( rc == DB_NOTFOUND ) {
256                                 id++;
257                                 getnext = 1;
258                                 continue;
259                         }
260                         break;
261                 }
262                 if ( ei->bei_e ) {
263                         rc = bdb_index_entry( op, txn, BDB_INDEX_UPDATE_OP, ei->bei_e );
264                         if ( rc == DB_LOCK_DEADLOCK ) {
265                                 TXN_ABORT( txn );
266                                 ldap_pvt_thread_yield();
267                                 continue;
268                         }
269                         if ( rc == 0 ) {
270                                 rc = TXN_COMMIT( txn, 0 );
271                                 txn = NULL;
272                         }
273                         if ( rc )
274                                 break;
275                 }
276                 id++;
277                 getnext = 1;
278         }
279
280         for ( i = 0; i < bdb->bi_nattrs; i++ ) {
281                 if ( bdb->bi_attrs[ i ]->ai_indexmask & BDB_INDEX_DELETING
282                         || bdb->bi_attrs[ i ]->ai_newmask == 0 )
283                 {
284                         continue;
285                 }
286                 bdb->bi_attrs[ i ]->ai_indexmask = bdb->bi_attrs[ i ]->ai_newmask;
287                 bdb->bi_attrs[ i ]->ai_newmask = 0;
288         }
289
290         ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
291         ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
292         bdb->bi_index_task = NULL;
293         ldap_pvt_runqueue_remove( &slapd_rq, rtask );
294         ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
295
296         return NULL;
297 }
298
299 /* Cleanup loose ends after Modify completes */
300 static int
301 bdb_cf_cleanup( ConfigArgs *c )
302 {
303         struct bdb_info *bdb = c->be->be_private;
304         int rc = 0;
305
306         if ( bdb->bi_flags & BDB_UPD_CONFIG ) {
307                 if ( bdb->bi_db_config ) {
308                         int i;
309                         FILE *f = fopen( bdb->bi_db_config_path, "w" );
310                         if ( f ) {
311                                 for (i=0; bdb->bi_db_config[i].bv_val; i++)
312                                         fprintf( f, "%s\n", bdb->bi_db_config[i].bv_val );
313                                 fclose( f );
314                         }
315                 } else {
316                         unlink( bdb->bi_db_config_path );
317                 }
318                 bdb->bi_flags ^= BDB_UPD_CONFIG;
319         }
320
321         if ( bdb->bi_flags & BDB_DEL_INDEX ) {
322                 bdb_attr_flush( bdb );
323                 bdb->bi_flags ^= BDB_DEL_INDEX;
324         }
325         
326         if ( bdb->bi_flags & BDB_RE_OPEN ) {
327                 bdb->bi_flags ^= BDB_RE_OPEN;
328                 rc = c->be->bd_info->bi_db_close( c->be, NULL );
329                 if ( rc == 0 )
330                         rc = c->be->bd_info->bi_db_open( c->be, NULL );
331                 /* If this fails, we need to restart */
332                 if ( rc ) {
333                         slapd_shutdown = 2;
334                         Debug( LDAP_DEBUG_ANY, LDAP_XSTRING(bdb_cf_cleanup)
335                                 ": failed to reopen database, rc=%d", rc, 0, 0 );
336                 }
337         }
338         return rc;
339 }
340
341 static int
342 bdb_cf_gen( ConfigArgs *c )
343 {
344         struct bdb_info *bdb = c->be->be_private;
345         int rc;
346
347         if ( c->op == SLAP_CONFIG_EMIT ) {
348                 rc = 0;
349                 switch( c->type ) {
350                 case BDB_CHKPT:
351                         if ( bdb->bi_txn_cp ) {
352                                 char buf[64];
353                                 struct berval bv;
354                                 bv.bv_len = sprintf( buf, "%d %d", bdb->bi_txn_cp_kbyte,
355                                         bdb->bi_txn_cp_min );
356                                 bv.bv_val = buf;
357                                 value_add_one( &c->rvalue_vals, &bv );
358                         } else{
359                                 rc = 1;
360                         }
361                         break;
362
363                 case BDB_DIRECTORY:
364                         if ( bdb->bi_dbenv_home ) {
365                                 c->value_string = ch_strdup( bdb->bi_dbenv_home );
366                         } else {
367                                 rc = 1;
368                         }
369                         break;
370
371                 case BDB_CONFIG:
372                         if ( !( bdb->bi_flags & BDB_IS_OPEN )
373                                 && !bdb->bi_db_config )
374                         {
375                                 char    buf[SLAP_TEXT_BUFLEN];
376                                 FILE *f = fopen( bdb->bi_db_config_path, "r" );
377                                 struct berval bv;
378
379                                 if ( f ) {
380                                         bdb->bi_flags |= BDB_HAS_CONFIG;
381                                         while ( fgets( buf, sizeof(buf), f )) {
382                                                 ber_str2bv( buf, 0, 1, &bv );
383                                                 if ( bv.bv_len > 0 && bv.bv_val[bv.bv_len-1] == '\n' ) {
384                                                         bv.bv_len--;
385                                                         bv.bv_val[bv.bv_len] = '\0';
386                                                 }
387                                                 /* shouldn't need this, but ... */
388                                                 if ( bv.bv_len > 0 && bv.bv_val[bv.bv_len-1] == '\r' ) {
389                                                         bv.bv_len--;
390                                                         bv.bv_val[bv.bv_len] = '\0';
391                                                 }
392                                                 ber_bvarray_add( &bdb->bi_db_config, &bv );
393                                         }
394                                         fclose( f );
395                                 }
396                         }
397                         if ( bdb->bi_db_config ) {
398                                 int i;
399                                 struct berval bv;
400
401                                 bv.bv_val = c->log;
402                                 for (i=0; !BER_BVISNULL(&bdb->bi_db_config[i]); i++) {
403                                         bv.bv_len = sprintf( bv.bv_val, "{%d}%s", i,
404                                                 bdb->bi_db_config[i].bv_val );
405                                         value_add_one( &c->rvalue_vals, &bv );
406                                 }
407                         }
408                         if ( !c->rvalue_vals ) rc = 1;
409                         break;
410
411                 case BDB_NOSYNC:
412                         if ( bdb->bi_dbenv_xflags & DB_TXN_NOSYNC )
413                                 c->value_int = 1;
414                         break;
415                         
416                 case BDB_INDEX:
417                         bdb_attr_index_unparse( bdb, &c->rvalue_vals );
418                         if ( !c->rvalue_vals ) rc = 1;
419                         break;
420
421                 case BDB_LOCKD:
422                         rc = 1;
423                         if ( bdb->bi_lock_detect != DB_LOCK_DEFAULT ) {
424                                 int i;
425                                 for (i=0; !BER_BVISNULL(&bdb_lockd[i].word); i++) {
426                                         if ( bdb->bi_lock_detect == bdb_lockd[i].mask ) {
427                                                 value_add_one( &c->rvalue_vals, &bdb_lockd[i].word );
428                                                 rc = 0;
429                                                 break;
430                                         }
431                                 }
432                         }
433                         break;
434
435                 case BDB_SSTACK:
436                         c->value_int = bdb->bi_search_stack_depth;
437                         break;
438                 }
439                 return rc;
440         } else if ( c->op == LDAP_MOD_DELETE ) {
441                 rc = 0;
442                 switch( c->type ) {
443                 /* single-valued no-ops */
444                 case BDB_LOCKD:
445                 case BDB_SSTACK:
446                         break;
447
448                 case BDB_CHKPT:
449                         if ( bdb->bi_txn_cp_task ) {
450                                 struct re_s *re = bdb->bi_txn_cp_task;
451                                 bdb->bi_txn_cp_task = NULL;
452                                 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ) )
453                                         ldap_pvt_runqueue_stoptask( &slapd_rq, re );
454                                 ldap_pvt_runqueue_remove( &slapd_rq, re );
455                         }
456                         bdb->bi_txn_cp = 0;
457                         break;
458                 case BDB_CONFIG:
459                         if ( c->valx < 0 ) {
460                                 ber_bvarray_free( bdb->bi_db_config );
461                                 bdb->bi_db_config = NULL;
462                         } else {
463                                 int i = c->valx;
464                                 ch_free( bdb->bi_db_config[i].bv_val );
465                                 for (; bdb->bi_db_config[i].bv_val; i++)
466                                         bdb->bi_db_config[i] = bdb->bi_db_config[i+1];
467                         }
468                         bdb->bi_flags |= BDB_UPD_CONFIG;
469                         c->cleanup = bdb_cf_cleanup;
470                         break;
471                 case BDB_DIRECTORY:
472                         bdb->bi_flags |= BDB_RE_OPEN;
473                         bdb->bi_flags ^= BDB_HAS_CONFIG;
474                         ch_free( bdb->bi_dbenv_home );
475                         bdb->bi_dbenv_home = NULL;
476                         ch_free( bdb->bi_db_config_path );
477                         bdb->bi_db_config_path = NULL;
478                         c->cleanup = bdb_cf_cleanup;
479                         ldap_pvt_thread_pool_purgekey( bdb->bi_dbenv );
480                         break;
481                 case BDB_NOSYNC:
482                         bdb->bi_dbenv->set_flags( bdb->bi_dbenv, DB_TXN_NOSYNC, 0 );
483                         break;
484                 case BDB_INDEX:
485                         if ( c->valx == -1 ) {
486                                 int i;
487
488                                 /* delete all (FIXME) */
489                                 for ( i = 0; i < bdb->bi_nattrs; i++ ) {
490                                         bdb->bi_attrs[i]->ai_indexmask |= BDB_INDEX_DELETING;
491                                 }
492                                 bdb->bi_flags |= BDB_DEL_INDEX;
493                                 c->cleanup = bdb_cf_cleanup;
494
495                         } else {
496                                 struct berval bv, def = BER_BVC("default");
497                                 char *ptr;
498
499                                 for (ptr = c->line; !isspace( (unsigned char) *ptr ); ptr++);
500
501                                 bv.bv_val = c->line;
502                                 bv.bv_len = ptr - bv.bv_val;
503                                 if ( bvmatch( &bv, &def )) {
504                                         bdb->bi_defaultmask = 0;
505
506                                 } else {
507                                         int i;
508                                         char **attrs;
509                                         char sep;
510
511                                         sep = bv.bv_val[ bv.bv_len ];
512                                         bv.bv_val[ bv.bv_len ] = '\0';
513                                         attrs = ldap_str2charray( bv.bv_val, "," );
514
515                                         for ( i = 0; attrs[ i ]; i++ ) {
516                                                 AttributeDescription *ad = NULL;
517                                                 const char *text;
518                                                 AttrInfo *ai;
519
520                                                 slap_str2ad( attrs[ i ], &ad, &text );
521                                                 /* if we got here... */
522                                                 assert( ad != NULL );
523
524                                                 ai = bdb_attr_mask( bdb, ad );
525                                                 /* if we got here... */
526                                                 assert( ai != NULL );
527
528                                                 ai->ai_indexmask |= BDB_INDEX_DELETING;
529                                                 bdb->bi_flags |= BDB_DEL_INDEX;
530                                                 c->cleanup = bdb_cf_cleanup;
531                                         }
532
533                                         bv.bv_val[ bv.bv_len ] = sep;
534                                         ldap_charray_free( attrs );
535                                 }
536                         }
537                         break;
538                 }
539                 return rc;
540         }
541
542         switch( c->type ) {
543         case BDB_CHKPT: {
544                 long    l;
545                 bdb->bi_txn_cp = 1;
546                 if ( lutil_atolx( &l, c->argv[1], 0 ) != 0 ) {
547                         fprintf( stderr, "%s: "
548                                 "invalid kbyte \"%s\" in \"checkpoint\".\n",
549                                 c->log, c->argv[1] );
550                         return 1;
551                 }
552                 bdb->bi_txn_cp_kbyte = l;
553                 if ( lutil_atolx( &l, c->argv[2], 0 ) != 0 ) {
554                         fprintf( stderr, "%s: "
555                                 "invalid minutes \"%s\" in \"checkpoint\".\n",
556                                 c->log, c->argv[2] );
557                         return 1;
558                 }
559                 bdb->bi_txn_cp_min = l;
560                 /* If we're in server mode and time-based checkpointing is enabled,
561                  * submit a task to perform periodic checkpoints.
562                  */
563                 if ((slapMode & SLAP_SERVER_MODE) && bdb->bi_txn_cp_min ) {
564                         struct re_s *re = bdb->bi_txn_cp_task;
565                         if ( re ) {
566                                 re->interval.tv_sec = bdb->bi_txn_cp_min * 60;
567                         } else {
568                                 if ( c->be->be_suffix == NULL || BER_BVISNULL( &c->be->be_suffix[0] ) ) {
569                                         fprintf( stderr, "%s: "
570                                                 "\"checkpoint\" must occur after \"suffix\".\n",
571                                                 c->log );
572                                         return 1;
573                                 }
574                                 bdb->bi_txn_cp_task = ldap_pvt_runqueue_insert( &slapd_rq,
575                                         bdb->bi_txn_cp_min * 60, bdb_checkpoint, bdb,
576                                         LDAP_XSTRING(bdb_checkpoint), c->be->be_suffix[0].bv_val );
577                         }
578                 }
579                 } break;
580
581         case BDB_CONFIG: {
582                 char *ptr = c->line;
583                 struct berval bv;
584
585                 if ( c->op == SLAP_CONFIG_ADD ) {
586                         ptr += STRLENOF("dbconfig");
587                         while (!isspace((unsigned char)*ptr)) ptr++;
588                         while (isspace((unsigned char)*ptr)) ptr++;
589                 }
590
591                 if ( bdb->bi_flags & BDB_IS_OPEN ) {
592                         bdb->bi_flags |= BDB_UPD_CONFIG;
593                         c->cleanup = bdb_cf_cleanup;
594                 } else {
595                 /* If we're just starting up...
596                  */
597                         FILE *f;
598                         /* If a DB_CONFIG file exists, or we don't know the path
599                          * to the DB_CONFIG file, ignore these directives
600                          */
601                         if (( bdb->bi_flags & BDB_HAS_CONFIG ) || !bdb->bi_db_config_path )
602                                 break;
603                         f = fopen( bdb->bi_db_config_path, "a" );
604                         if ( f ) {
605                                 /* FIXME: EBCDIC probably needs special handling */
606                                 fprintf( f, "%s\n", ptr );
607                                 fclose( f );
608                         }
609                 }
610                 ber_str2bv( ptr, 0, 1, &bv );
611                 ber_bvarray_add( &bdb->bi_db_config, &bv );
612                 }
613                 break;
614
615         case BDB_DIRECTORY: {
616                 FILE *f;
617                 char *ptr;
618
619                 if ( bdb->bi_dbenv_home )
620                         ch_free( bdb->bi_dbenv_home );
621                 bdb->bi_dbenv_home = c->value_string;
622
623                 /* See if a DB_CONFIG file already exists here */
624                 if ( bdb->bi_db_config_path )
625                         ch_free( bdb->bi_db_config_path );
626                 bdb->bi_db_config_path = ch_malloc( strlen( bdb->bi_dbenv_home ) +
627                         STRLENOF(LDAP_DIRSEP) + STRLENOF("DB_CONFIG") + 1 );
628                 ptr = lutil_strcopy( bdb->bi_db_config_path, bdb->bi_dbenv_home );
629                 *ptr++ = LDAP_DIRSEP[0];
630                 strcpy( ptr, "DB_CONFIG" );
631
632                 f = fopen( bdb->bi_db_config_path, "r" );
633                 if ( f ) {
634                         bdb->bi_flags |= BDB_HAS_CONFIG;
635                         fclose(f);
636                 }
637                 }
638                 break;
639
640         case BDB_NOSYNC:
641                 if ( c->value_int )
642                         bdb->bi_dbenv_xflags |= DB_TXN_NOSYNC;
643                 else
644                         bdb->bi_dbenv_xflags &= ~DB_TXN_NOSYNC;
645                 if ( bdb->bi_flags & BDB_IS_OPEN ) {
646                         bdb->bi_dbenv->set_flags( bdb->bi_dbenv, DB_TXN_NOSYNC,
647                                 c->value_int );
648                 }
649                 break;
650
651         case BDB_INDEX:
652                 rc = bdb_attr_index_config( bdb, c->fname, c->lineno,
653                         c->argc - 1, &c->argv[1] );
654
655                 if( rc != LDAP_SUCCESS ) return 1;
656                 if (( bdb->bi_flags & BDB_IS_OPEN ) && !bdb->bi_index_task ) {
657                         /* Start the task as soon as we finish here. Set a long
658                          * interval (10 hours) so that it only gets scheduled once.
659                          */
660                         if ( c->be->be_suffix == NULL || BER_BVISNULL( &c->be->be_suffix[0] ) ) {
661                                 fprintf( stderr, "%s: "
662                                         "\"index\" must occur after \"suffix\".\n",
663                                         c->log );
664                                 return 1;
665                         }
666                         bdb->bi_index_task = ldap_pvt_runqueue_insert( &slapd_rq, 36000,
667                                 bdb_online_index, c->be,
668                                 LDAP_XSTRING(bdb_online_index), c->be->be_suffix[0].bv_val );
669                 }
670                 break;
671
672         case BDB_LOCKD:
673                 rc = verb_to_mask( c->argv[1], bdb_lockd );
674                 if ( BER_BVISNULL(&bdb_lockd[rc].word) ) {
675                         fprintf( stderr, "%s: "
676                                 "bad policy (%s) in \"lockDetect <policy>\" line\n",
677                                 c->log, c->argv[1] );
678                         return 1;
679                 }
680                 bdb->bi_lock_detect = rc;
681                 break;
682
683         case BDB_SSTACK:
684                 if ( c->value_int < MINIMUM_SEARCH_STACK_DEPTH ) {
685                         fprintf( stderr,
686                 "%s: depth %d too small, using %d\n",
687                         c->log, c->value_int, MINIMUM_SEARCH_STACK_DEPTH );
688                         c->value_int = MINIMUM_SEARCH_STACK_DEPTH;
689                 }
690                 bdb->bi_search_stack_depth = c->value_int;
691                 break;
692         }
693         return 0;
694 }
695
696 int bdb_back_init_cf( BackendInfo *bi )
697 {
698         int rc;
699         bi->bi_cf_ocs = bdbocs;
700
701         rc = config_register_schema( bdbcfg, bdbocs );
702         if ( rc ) return rc;
703         return 0;
704 }