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