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