]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/config.c
More for BDB 4.6 compatibility
[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_INT|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_INT|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 OMsDirectoryString 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_INT|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_INT|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_INT|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 ( ( slapMode & SLAP_SERVER_MODE )
373                                 && !( bdb->bi_flags & BDB_IS_OPEN )
374                                 && !bdb->bi_db_config )
375                         {
376                                 char    buf[SLAP_TEXT_BUFLEN];
377                                 FILE *f = fopen( bdb->bi_db_config_path, "r" );
378                                 struct berval bv;
379
380                                 if ( f ) {
381                                         bdb->bi_flags |= BDB_HAS_CONFIG;
382                                         while ( fgets( buf, sizeof(buf), f )) {
383                                                 ber_str2bv( buf, 0, 1, &bv );
384                                                 if ( bv.bv_len > 0 && bv.bv_val[bv.bv_len-1] == '\n' ) {
385                                                         bv.bv_len--;
386                                                         bv.bv_val[bv.bv_len] = '\0';
387                                                 }
388                                                 /* shouldn't need this, but ... */
389                                                 if ( bv.bv_len > 0 && bv.bv_val[bv.bv_len-1] == '\r' ) {
390                                                         bv.bv_len--;
391                                                         bv.bv_val[bv.bv_len] = '\0';
392                                                 }
393                                                 ber_bvarray_add( &bdb->bi_db_config, &bv );
394                                         }
395                                         fclose( f );
396                                 }
397                         }
398                         if ( bdb->bi_db_config ) {
399                                 int i;
400                                 struct berval bv;
401
402                                 bv.bv_val = c->log;
403                                 for (i=0; !BER_BVISNULL(&bdb->bi_db_config[i]); i++) {
404                                         bv.bv_len = sprintf( bv.bv_val, "{%d}%s", i,
405                                                 bdb->bi_db_config[i].bv_val );
406                                         value_add_one( &c->rvalue_vals, &bv );
407                                 }
408                         }
409                         if ( !c->rvalue_vals ) rc = 1;
410                         break;
411
412                 case BDB_NOSYNC:
413                         if ( bdb->bi_dbenv_xflags & DB_TXN_NOSYNC )
414                                 c->value_int = 1;
415                         break;
416                         
417                 case BDB_INDEX:
418                         bdb_attr_index_unparse( bdb, &c->rvalue_vals );
419                         if ( !c->rvalue_vals ) rc = 1;
420                         break;
421
422                 case BDB_LOCKD:
423                         rc = 1;
424                         if ( bdb->bi_lock_detect != DB_LOCK_DEFAULT ) {
425                                 int i;
426                                 for (i=0; !BER_BVISNULL(&bdb_lockd[i].word); i++) {
427                                         if ( bdb->bi_lock_detect == bdb_lockd[i].mask ) {
428                                                 value_add_one( &c->rvalue_vals, &bdb_lockd[i].word );
429                                                 rc = 0;
430                                                 break;
431                                         }
432                                 }
433                         }
434                         break;
435
436                 case BDB_SSTACK:
437                         c->value_int = bdb->bi_search_stack_depth;
438                         break;
439                 }
440                 return rc;
441         } else if ( c->op == LDAP_MOD_DELETE ) {
442                 rc = 0;
443                 switch( c->type ) {
444                 /* single-valued no-ops */
445                 case BDB_LOCKD:
446                 case BDB_SSTACK:
447                         break;
448
449                 case BDB_CHKPT:
450                         if ( bdb->bi_txn_cp_task ) {
451                                 struct re_s *re = bdb->bi_txn_cp_task;
452                                 bdb->bi_txn_cp_task = NULL;
453                                 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ) )
454                                         ldap_pvt_runqueue_stoptask( &slapd_rq, re );
455                                 ldap_pvt_runqueue_remove( &slapd_rq, re );
456                         }
457                         bdb->bi_txn_cp = 0;
458                         break;
459                 case BDB_CONFIG:
460                         if ( c->valx < 0 ) {
461                                 ber_bvarray_free( bdb->bi_db_config );
462                                 bdb->bi_db_config = NULL;
463                         } else {
464                                 int i = c->valx;
465                                 ch_free( bdb->bi_db_config[i].bv_val );
466                                 for (; bdb->bi_db_config[i].bv_val; i++)
467                                         bdb->bi_db_config[i] = bdb->bi_db_config[i+1];
468                         }
469                         bdb->bi_flags |= BDB_UPD_CONFIG;
470                         c->cleanup = bdb_cf_cleanup;
471                         break;
472                 case BDB_DIRECTORY:
473                         bdb->bi_flags |= BDB_RE_OPEN;
474                         bdb->bi_flags ^= BDB_HAS_CONFIG;
475                         ch_free( bdb->bi_dbenv_home );
476                         bdb->bi_dbenv_home = NULL;
477                         ch_free( bdb->bi_db_config_path );
478                         bdb->bi_db_config_path = NULL;
479                         c->cleanup = bdb_cf_cleanup;
480                         ldap_pvt_thread_pool_purgekey( bdb->bi_dbenv );
481                         break;
482                 case BDB_NOSYNC:
483                         bdb->bi_dbenv->set_flags( bdb->bi_dbenv, DB_TXN_NOSYNC, 0 );
484                         break;
485                 case BDB_INDEX:
486                         if ( c->valx == -1 ) {
487                                 int i;
488
489                                 /* delete all (FIXME) */
490                                 for ( i = 0; i < bdb->bi_nattrs; i++ ) {
491                                         bdb->bi_attrs[i]->ai_indexmask |= BDB_INDEX_DELETING;
492                                 }
493                                 bdb->bi_flags |= BDB_DEL_INDEX;
494                                 c->cleanup = bdb_cf_cleanup;
495
496                         } else {
497                                 struct berval bv, def = BER_BVC("default");
498                                 char *ptr;
499
500                                 for (ptr = c->line; !isspace( (unsigned char) *ptr ); ptr++);
501
502                                 bv.bv_val = c->line;
503                                 bv.bv_len = ptr - bv.bv_val;
504                                 if ( bvmatch( &bv, &def )) {
505                                         bdb->bi_defaultmask = 0;
506
507                                 } else {
508                                         int i;
509                                         char **attrs;
510                                         char sep;
511
512                                         sep = bv.bv_val[ bv.bv_len ];
513                                         bv.bv_val[ bv.bv_len ] = '\0';
514                                         attrs = ldap_str2charray( bv.bv_val, "," );
515
516                                         for ( i = 0; attrs[ i ]; i++ ) {
517                                                 AttributeDescription *ad = NULL;
518                                                 const char *text;
519                                                 AttrInfo *ai;
520
521                                                 slap_str2ad( attrs[ i ], &ad, &text );
522                                                 /* if we got here... */
523                                                 assert( ad != NULL );
524
525                                                 ai = bdb_attr_mask( bdb, ad );
526                                                 /* if we got here... */
527                                                 assert( ai != NULL );
528
529                                                 ai->ai_indexmask |= BDB_INDEX_DELETING;
530                                                 bdb->bi_flags |= BDB_DEL_INDEX;
531                                                 c->cleanup = bdb_cf_cleanup;
532                                         }
533
534                                         bv.bv_val[ bv.bv_len ] = sep;
535                                         ldap_charray_free( attrs );
536                                 }
537                         }
538                         break;
539                 }
540                 return rc;
541         }
542
543         switch( c->type ) {
544         case BDB_CHKPT: {
545                 long    l;
546                 bdb->bi_txn_cp = 1;
547                 if ( lutil_atolx( &l, c->argv[1], 0 ) != 0 ) {
548                         fprintf( stderr, "%s: "
549                                 "invalid kbyte \"%s\" in \"checkpoint\".\n",
550                                 c->log, c->argv[1] );
551                         return 1;
552                 }
553                 bdb->bi_txn_cp_kbyte = l;
554                 if ( lutil_atolx( &l, c->argv[2], 0 ) != 0 ) {
555                         fprintf( stderr, "%s: "
556                                 "invalid minutes \"%s\" in \"checkpoint\".\n",
557                                 c->log, c->argv[2] );
558                         return 1;
559                 }
560                 bdb->bi_txn_cp_min = l;
561                 /* If we're in server mode and time-based checkpointing is enabled,
562                  * submit a task to perform periodic checkpoints.
563                  */
564                 if ((slapMode & SLAP_SERVER_MODE) && bdb->bi_txn_cp_min ) {
565                         struct re_s *re = bdb->bi_txn_cp_task;
566                         if ( re ) {
567                                 re->interval.tv_sec = bdb->bi_txn_cp_min * 60;
568                         } else {
569                                 if ( c->be->be_suffix == NULL || BER_BVISNULL( &c->be->be_suffix[0] ) ) {
570                                         fprintf( stderr, "%s: "
571                                                 "\"checkpoint\" must occur after \"suffix\".\n",
572                                                 c->log );
573                                         return 1;
574                                 }
575                                 bdb->bi_txn_cp_task = ldap_pvt_runqueue_insert( &slapd_rq,
576                                         bdb->bi_txn_cp_min * 60, bdb_checkpoint, bdb,
577                                         LDAP_XSTRING(bdb_checkpoint), c->be->be_suffix[0].bv_val );
578                         }
579                 }
580                 } break;
581
582         case BDB_CONFIG: {
583                 char *ptr = c->line;
584                 struct berval bv;
585
586                 if ( c->op == SLAP_CONFIG_ADD ) {
587                         ptr += STRLENOF("dbconfig");
588                         while (!isspace((unsigned char)*ptr)) ptr++;
589                         while (isspace((unsigned char)*ptr)) ptr++;
590                 }
591
592                 if ( bdb->bi_flags & BDB_IS_OPEN ) {
593                         bdb->bi_flags |= BDB_UPD_CONFIG;
594                         c->cleanup = bdb_cf_cleanup;
595                 } else {
596                 /* If we're just starting up...
597                  */
598                         FILE *f;
599                         /* If a DB_CONFIG file exists, or we don't know the path
600                          * to the DB_CONFIG file, ignore these directives
601                          */
602                         if (( bdb->bi_flags & BDB_HAS_CONFIG ) || !bdb->bi_db_config_path )
603                                 break;
604                         f = fopen( bdb->bi_db_config_path, "a" );
605                         if ( f ) {
606                                 /* FIXME: EBCDIC probably needs special handling */
607                                 fprintf( f, "%s\n", ptr );
608                                 fclose( f );
609                         }
610                 }
611                 ber_str2bv( ptr, 0, 1, &bv );
612                 ber_bvarray_add( &bdb->bi_db_config, &bv );
613                 }
614                 break;
615
616         case BDB_DIRECTORY: {
617                 FILE *f;
618                 char *ptr;
619
620                 if ( bdb->bi_dbenv_home )
621                         ch_free( bdb->bi_dbenv_home );
622                 bdb->bi_dbenv_home = c->value_string;
623
624                 /* See if a DB_CONFIG file already exists here */
625                 if ( bdb->bi_db_config_path )
626                         ch_free( bdb->bi_db_config_path );
627                 bdb->bi_db_config_path = ch_malloc( strlen( bdb->bi_dbenv_home ) +
628                         STRLENOF(LDAP_DIRSEP) + STRLENOF("DB_CONFIG") + 1 );
629                 ptr = lutil_strcopy( bdb->bi_db_config_path, bdb->bi_dbenv_home );
630                 *ptr++ = LDAP_DIRSEP[0];
631                 strcpy( ptr, "DB_CONFIG" );
632
633                 f = fopen( bdb->bi_db_config_path, "r" );
634                 if ( f ) {
635                         bdb->bi_flags |= BDB_HAS_CONFIG;
636                         fclose(f);
637                 }
638                 }
639                 break;
640
641         case BDB_NOSYNC:
642                 if ( c->value_int )
643                         bdb->bi_dbenv_xflags |= DB_TXN_NOSYNC;
644                 else
645                         bdb->bi_dbenv_xflags &= ~DB_TXN_NOSYNC;
646                 if ( bdb->bi_flags & BDB_IS_OPEN ) {
647                         bdb->bi_dbenv->set_flags( bdb->bi_dbenv, DB_TXN_NOSYNC,
648                                 c->value_int );
649                 }
650                 break;
651
652         case BDB_INDEX:
653                 rc = bdb_attr_index_config( bdb, c->fname, c->lineno,
654                         c->argc - 1, &c->argv[1] );
655
656                 if( rc != LDAP_SUCCESS ) return 1;
657                 if (( bdb->bi_flags & BDB_IS_OPEN ) && !bdb->bi_index_task ) {
658                         /* Start the task as soon as we finish here. Set a long
659                          * interval (10 hours) so that it only gets scheduled once.
660                          */
661                         if ( c->be->be_suffix == NULL || BER_BVISNULL( &c->be->be_suffix[0] ) ) {
662                                 fprintf( stderr, "%s: "
663                                         "\"index\" must occur after \"suffix\".\n",
664                                         c->log );
665                                 return 1;
666                         }
667                         bdb->bi_index_task = ldap_pvt_runqueue_insert( &slapd_rq, 36000,
668                                 bdb_online_index, c->be,
669                                 LDAP_XSTRING(bdb_online_index), c->be->be_suffix[0].bv_val );
670                 }
671                 break;
672
673         case BDB_LOCKD:
674                 rc = verb_to_mask( c->argv[1], bdb_lockd );
675                 if ( BER_BVISNULL(&bdb_lockd[rc].word) ) {
676                         fprintf( stderr, "%s: "
677                                 "bad policy (%s) in \"lockDetect <policy>\" line\n",
678                                 c->log, c->argv[1] );
679                         return 1;
680                 }
681                 bdb->bi_lock_detect = rc;
682                 break;
683
684         case BDB_SSTACK:
685                 if ( c->value_int < MINIMUM_SEARCH_STACK_DEPTH ) {
686                         fprintf( stderr,
687                 "%s: depth %d too small, using %d\n",
688                         c->log, c->value_int, MINIMUM_SEARCH_STACK_DEPTH );
689                         c->value_int = MINIMUM_SEARCH_STACK_DEPTH;
690                 }
691                 bdb->bi_search_stack_depth = c->value_int;
692                 break;
693         }
694         return 0;
695 }
696
697 int bdb_back_init_cf( BackendInfo *bi )
698 {
699         int rc;
700         bi->bi_cf_ocs = bdbocs;
701
702         rc = config_register_schema( bdbcfg, bdbocs );
703         if ( rc ) return rc;
704         return 0;
705 }