]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/config.c
Give OperationBuffer normal struct members instead of using
[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;
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, &opbuf, ctx );
195         op = &opbuf.ob_op;
196
197         op->o_bd = be;
198
199         DBTzero( &key );
200         DBTzero( &data );
201         
202         id = 1;
203         key.data = &nid;
204         key.size = key.ulen = sizeof(ID);
205         key.flags = DB_DBT_USERMEM;
206
207         data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
208         data.dlen = data.ulen = 0;
209
210         while ( 1 ) {
211                 if ( slapd_shutdown )
212                         break;
213
214                 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &txn, bdb->bi_db_opflags );
215                 if ( rc ) 
216                         break;
217                 locker = TXN_ID( txn );
218                 if ( getnext ) {
219                         getnext = 0;
220                         BDB_ID2DISK( id, &nid );
221                         rc = bdb->bi_id2entry->bdi_db->cursor(
222                                 bdb->bi_id2entry->bdi_db, txn, &curs, bdb->bi_db_opflags );
223                         if ( rc ) {
224                                 TXN_ABORT( txn );
225                                 break;
226                         }
227                         rc = curs->c_get( curs, &key, &data, DB_SET_RANGE );
228                         curs->c_close( curs );
229                         if ( rc ) {
230                                 TXN_ABORT( txn );
231                                 if ( rc == DB_NOTFOUND )
232                                         rc = 0;
233                                 if ( rc == DB_LOCK_DEADLOCK ) {
234                                         ldap_pvt_thread_yield();
235                                         continue;
236                                 }
237                                 break;
238                         }
239                         BDB_DISK2ID( &nid, &id );
240                 }
241
242                 ei = NULL;
243                 rc = bdb_cache_find_id( op, txn, id, &ei, 0, locker, &lock );
244                 if ( rc ) {
245                         TXN_ABORT( txn );
246                         if ( rc == DB_LOCK_DEADLOCK ) {
247                                 ldap_pvt_thread_yield();
248                                 continue;
249                         }
250                         if ( rc == DB_NOTFOUND ) {
251                                 id++;
252                                 getnext = 1;
253                                 continue;
254                         }
255                         break;
256                 }
257                 if ( ei->bei_e ) {
258                         rc = bdb_index_entry( op, txn, BDB_INDEX_UPDATE_OP, ei->bei_e );
259                         if ( rc == DB_LOCK_DEADLOCK ) {
260                                 TXN_ABORT( txn );
261                                 ldap_pvt_thread_yield();
262                                 continue;
263                         }
264                         if ( rc == 0 ) {
265                                 rc = TXN_COMMIT( txn, 0 );
266                                 txn = NULL;
267                         }
268                         if ( rc )
269                                 break;
270                 }
271                 id++;
272                 getnext = 1;
273         }
274
275         for ( i = 0; i < bdb->bi_nattrs; i++ ) {
276                 if ( bdb->bi_attrs[ i ]->ai_indexmask & BDB_INDEX_DELETING
277                         || bdb->bi_attrs[ i ]->ai_newmask == 0 )
278                 {
279                         continue;
280                 }
281                 bdb->bi_attrs[ i ]->ai_indexmask = bdb->bi_attrs[ i ]->ai_newmask;
282                 bdb->bi_attrs[ i ]->ai_newmask = 0;
283         }
284
285         ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
286         ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
287         bdb->bi_index_task = NULL;
288         ldap_pvt_runqueue_remove( &slapd_rq, rtask );
289         ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
290
291         return NULL;
292 }
293
294 /* Cleanup loose ends after Modify completes */
295 static int
296 bdb_cf_cleanup( ConfigArgs *c )
297 {
298         struct bdb_info *bdb = c->be->be_private;
299         int rc = 0;
300
301         if ( bdb->bi_flags & BDB_UPD_CONFIG ) {
302                 if ( bdb->bi_db_config ) {
303                         int i;
304                         FILE *f = fopen( bdb->bi_db_config_path, "w" );
305                         if ( f ) {
306                                 for (i=0; bdb->bi_db_config[i].bv_val; i++)
307                                         fprintf( f, "%s\n", bdb->bi_db_config[i].bv_val );
308                                 fclose( f );
309                         }
310                 } else {
311                         unlink( bdb->bi_db_config_path );
312                 }
313                 bdb->bi_flags ^= BDB_UPD_CONFIG;
314         }
315
316         if ( bdb->bi_flags & BDB_DEL_INDEX ) {
317                 bdb_attr_flush( bdb );
318                 bdb->bi_flags ^= BDB_DEL_INDEX;
319         }
320         
321         if ( bdb->bi_flags & BDB_RE_OPEN ) {
322                 bdb->bi_flags ^= BDB_RE_OPEN;
323                 rc = c->be->bd_info->bi_db_close( c->be );
324                 if ( rc == 0 )
325                         rc = c->be->bd_info->bi_db_open( c->be );
326                 /* If this fails, we need to restart */
327                 if ( rc ) {
328                         slapd_shutdown = 2;
329                         Debug( LDAP_DEBUG_ANY, LDAP_XSTRING(bdb_cf_cleanup)
330                                 ": failed to reopen database, rc=%d", rc, 0, 0 );
331                 }
332         }
333         return rc;
334 }
335
336 static int
337 bdb_cf_gen( ConfigArgs *c )
338 {
339         struct bdb_info *bdb = c->be->be_private;
340         int rc;
341
342         if ( c->op == SLAP_CONFIG_EMIT ) {
343                 rc = 0;
344                 switch( c->type ) {
345                 case BDB_CHKPT:
346                         if ( bdb->bi_txn_cp ) {
347                                 char buf[64];
348                                 struct berval bv;
349                                 bv.bv_len = sprintf( buf, "%d %d", bdb->bi_txn_cp_kbyte,
350                                         bdb->bi_txn_cp_min );
351                                 bv.bv_val = buf;
352                                 value_add_one( &c->rvalue_vals, &bv );
353                         } else{
354                                 rc = 1;
355                         }
356                         break;
357
358                 case BDB_DIRECTORY:
359                         if ( bdb->bi_dbenv_home ) {
360                                 c->value_string = ch_strdup( bdb->bi_dbenv_home );
361                         } else {
362                                 rc = 1;
363                         }
364                         break;
365
366                 case BDB_CONFIG:
367                         if ( ( slapMode & SLAP_SERVER_MODE )
368                                 && !( bdb->bi_flags & BDB_IS_OPEN )
369                                 && !bdb->bi_db_config )
370                         {
371                                 char    buf[SLAP_TEXT_BUFLEN];
372                                 FILE *f = fopen( bdb->bi_db_config_path, "r" );
373                                 struct berval bv;
374
375                                 if ( f ) {
376                                         bdb->bi_flags |= BDB_HAS_CONFIG;
377                                         while ( fgets( buf, sizeof(buf), f )) {
378                                                 ber_str2bv( buf, 0, 1, &bv );
379                                                 if ( bv.bv_len > 0 && bv.bv_val[bv.bv_len-1] == '\n' ) {
380                                                         bv.bv_len--;
381                                                         bv.bv_val[bv.bv_len] = '\0';
382                                                 }
383                                                 /* shouldn't need this, but ... */
384                                                 if ( bv.bv_len > 0 && bv.bv_val[bv.bv_len-1] == '\r' ) {
385                                                         bv.bv_len--;
386                                                         bv.bv_val[bv.bv_len] = '\0';
387                                                 }
388                                                 ber_bvarray_add( &bdb->bi_db_config, &bv );
389                                         }
390                                         fclose( f );
391                                 }
392                         }
393                         if ( bdb->bi_db_config ) {
394                                 int i;
395                                 struct berval bv;
396
397                                 bv.bv_val = c->log;
398                                 for (i=0; !BER_BVISNULL(&bdb->bi_db_config[i]); i++) {
399                                         bv.bv_len = sprintf( bv.bv_val, "{%d}%s", i,
400                                                 bdb->bi_db_config[i].bv_val );
401                                         value_add_one( &c->rvalue_vals, &bv );
402                                 }
403                         }
404                         if ( !c->rvalue_vals ) rc = 1;
405                         break;
406
407                 case BDB_NOSYNC:
408                         if ( bdb->bi_dbenv_xflags & DB_TXN_NOSYNC )
409                                 c->value_int = 1;
410                         break;
411                         
412                 case BDB_INDEX:
413                         bdb_attr_index_unparse( bdb, &c->rvalue_vals );
414                         if ( !c->rvalue_vals ) rc = 1;
415                         break;
416
417                 case BDB_LOCKD:
418                         rc = 1;
419                         if ( bdb->bi_lock_detect != DB_LOCK_DEFAULT ) {
420                                 int i;
421                                 for (i=0; !BER_BVISNULL(&bdb_lockd[i].word); i++) {
422                                         if ( bdb->bi_lock_detect == bdb_lockd[i].mask ) {
423                                                 value_add_one( &c->rvalue_vals, &bdb_lockd[i].word );
424                                                 rc = 0;
425                                                 break;
426                                         }
427                                 }
428                         }
429                         break;
430
431                 case BDB_SSTACK:
432                         c->value_int = bdb->bi_search_stack_depth;
433                         break;
434                 }
435                 return rc;
436         } else if ( c->op == LDAP_MOD_DELETE ) {
437                 rc = 0;
438                 switch( c->type ) {
439                 /* single-valued no-ops */
440                 case BDB_LOCKD:
441                 case BDB_SSTACK:
442                         break;
443
444                 case BDB_CHKPT:
445                         if ( bdb->bi_txn_cp_task ) {
446                                 struct re_s *re = bdb->bi_txn_cp_task;
447                                 bdb->bi_txn_cp_task = NULL;
448                                 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ) )
449                                         ldap_pvt_runqueue_stoptask( &slapd_rq, re );
450                                 ldap_pvt_runqueue_remove( &slapd_rq, re );
451                         }
452                         bdb->bi_txn_cp = 0;
453                         break;
454                 case BDB_CONFIG:
455                         if ( c->valx < 0 ) {
456                                 ber_bvarray_free( bdb->bi_db_config );
457                                 bdb->bi_db_config = NULL;
458                         } else {
459                                 int i = c->valx;
460                                 ch_free( bdb->bi_db_config[i].bv_val );
461                                 for (; bdb->bi_db_config[i].bv_val; i++)
462                                         bdb->bi_db_config[i] = bdb->bi_db_config[i+1];
463                         }
464                         bdb->bi_flags |= BDB_UPD_CONFIG;
465                         c->cleanup = bdb_cf_cleanup;
466                         break;
467                 case BDB_DIRECTORY:
468                         bdb->bi_flags |= BDB_RE_OPEN;
469                         bdb->bi_flags ^= BDB_HAS_CONFIG;
470                         ch_free( bdb->bi_dbenv_home );
471                         bdb->bi_dbenv_home = NULL;
472                         ch_free( bdb->bi_db_config_path );
473                         bdb->bi_db_config_path = NULL;
474                         c->cleanup = bdb_cf_cleanup;
475                         ldap_pvt_thread_pool_purgekey( bdb->bi_dbenv );
476                         break;
477                 case BDB_NOSYNC:
478                         bdb->bi_dbenv->set_flags( bdb->bi_dbenv, DB_TXN_NOSYNC, 0 );
479                         break;
480                 case BDB_INDEX:
481                         if ( c->valx == -1 ) {
482                                 int i;
483
484                                 /* delete all (FIXME) */
485                                 for ( i = 0; i < bdb->bi_nattrs; i++ ) {
486                                         bdb->bi_attrs[i]->ai_indexmask |= BDB_INDEX_DELETING;
487                                 }
488                                 bdb->bi_flags |= BDB_DEL_INDEX;
489                                 c->cleanup = bdb_cf_cleanup;
490
491                         } else {
492                                 struct berval bv, def = BER_BVC("default");
493                                 char *ptr;
494
495                                 for (ptr = c->line; !isspace( (unsigned char) *ptr ); ptr++);
496
497                                 bv.bv_val = c->line;
498                                 bv.bv_len = ptr - bv.bv_val;
499                                 if ( bvmatch( &bv, &def )) {
500                                         bdb->bi_defaultmask = 0;
501
502                                 } else {
503                                         int i;
504                                         char **attrs;
505                                         char sep;
506
507                                         sep = bv.bv_val[ bv.bv_len ];
508                                         bv.bv_val[ bv.bv_len ] = '\0';
509                                         attrs = ldap_str2charray( bv.bv_val, "," );
510
511                                         for ( i = 0; attrs[ i ]; i++ ) {
512                                                 AttributeDescription *ad = NULL;
513                                                 const char *text;
514                                                 AttrInfo *ai;
515
516                                                 slap_str2ad( attrs[ i ], &ad, &text );
517                                                 /* if we got here... */
518                                                 assert( ad != NULL );
519
520                                                 ai = bdb_attr_mask( bdb, ad );
521                                                 /* if we got here... */
522                                                 assert( ai != NULL );
523
524                                                 ai->ai_indexmask |= BDB_INDEX_DELETING;
525                                                 bdb->bi_flags |= BDB_DEL_INDEX;
526                                                 c->cleanup = bdb_cf_cleanup;
527                                         }
528
529                                         bv.bv_val[ bv.bv_len ] = sep;
530                                         ldap_charray_free( attrs );
531                                 }
532                         }
533                         break;
534                 }
535                 return rc;
536         }
537
538         switch( c->type ) {
539         case BDB_CHKPT: {
540                 long    l;
541                 bdb->bi_txn_cp = 1;
542                 if ( lutil_atolx( &l, c->argv[1], 0 ) != 0 ) {
543                         fprintf( stderr, "%s: "
544                                 "invalid kbyte \"%s\" in \"checkpoint\".\n",
545                                 c->log, c->argv[1] );
546                         return 1;
547                 }
548                 bdb->bi_txn_cp_kbyte = l;
549                 if ( lutil_atolx( &l, c->argv[2], 0 ) != 0 ) {
550                         fprintf( stderr, "%s: "
551                                 "invalid minutes \"%s\" in \"checkpoint\".\n",
552                                 c->log, c->argv[2] );
553                         return 1;
554                 }
555                 bdb->bi_txn_cp_min = l;
556                 /* If we're in server mode and time-based checkpointing is enabled,
557                  * submit a task to perform periodic checkpoints.
558                  */
559                 if ((slapMode & SLAP_SERVER_MODE) && bdb->bi_txn_cp_min ) {
560                         struct re_s *re = bdb->bi_txn_cp_task;
561                         if ( re ) {
562                                 re->interval.tv_sec = bdb->bi_txn_cp_min * 60;
563                         } else {
564                                 if ( c->be->be_suffix == NULL || BER_BVISNULL( &c->be->be_suffix[0] ) ) {
565                                         fprintf( stderr, "%s: "
566                                                 "\"checkpoint\" must occur after \"suffix\".\n",
567                                                 c->log );
568                                         return 1;
569                                 }
570                                 bdb->bi_txn_cp_task = ldap_pvt_runqueue_insert( &slapd_rq,
571                                         bdb->bi_txn_cp_min * 60, bdb_checkpoint, bdb,
572                                         LDAP_XSTRING(bdb_checkpoint), c->be->be_suffix[0].bv_val );
573                         }
574                 }
575                 } break;
576
577         case BDB_CONFIG: {
578                 char *ptr = c->line;
579                 struct berval bv;
580
581                 if ( c->op == SLAP_CONFIG_ADD ) {
582                         ptr += STRLENOF("dbconfig");
583                         while (!isspace((unsigned char)*ptr)) ptr++;
584                         while (isspace((unsigned char)*ptr)) ptr++;
585                 }
586
587                 if ( bdb->bi_flags & BDB_IS_OPEN ) {
588                         bdb->bi_flags |= BDB_UPD_CONFIG;
589                         c->cleanup = bdb_cf_cleanup;
590                 } else {
591                 /* If we're just starting up...
592                  */
593                         FILE *f;
594                         /* If a DB_CONFIG file exists, or we don't know the path
595                          * to the DB_CONFIG file, ignore these directives
596                          */
597                         if (( bdb->bi_flags & BDB_HAS_CONFIG ) || !bdb->bi_db_config_path )
598                                 break;
599                         f = fopen( bdb->bi_db_config_path, "a" );
600                         if ( f ) {
601                                 /* FIXME: EBCDIC probably needs special handling */
602                                 fprintf( f, "%s\n", ptr );
603                                 fclose( f );
604                         }
605                 }
606                 ber_str2bv( ptr, 0, 1, &bv );
607                 ber_bvarray_add( &bdb->bi_db_config, &bv );
608                 }
609                 break;
610
611         case BDB_DIRECTORY: {
612                 FILE *f;
613                 char *ptr;
614
615                 if ( bdb->bi_dbenv_home )
616                         ch_free( bdb->bi_dbenv_home );
617                 bdb->bi_dbenv_home = c->value_string;
618
619                 /* See if a DB_CONFIG file already exists here */
620                 if ( bdb->bi_db_config_path )
621                         ch_free( bdb->bi_db_config_path );
622                 bdb->bi_db_config_path = ch_malloc( strlen( bdb->bi_dbenv_home ) +
623                         STRLENOF(LDAP_DIRSEP) + STRLENOF("DB_CONFIG") + 1 );
624                 ptr = lutil_strcopy( bdb->bi_db_config_path, bdb->bi_dbenv_home );
625                 *ptr++ = LDAP_DIRSEP[0];
626                 strcpy( ptr, "DB_CONFIG" );
627
628                 f = fopen( bdb->bi_db_config_path, "r" );
629                 if ( f ) {
630                         bdb->bi_flags |= BDB_HAS_CONFIG;
631                         fclose(f);
632                 }
633                 }
634                 break;
635
636         case BDB_NOSYNC:
637                 if ( c->value_int )
638                         bdb->bi_dbenv_xflags |= DB_TXN_NOSYNC;
639                 else
640                         bdb->bi_dbenv_xflags &= ~DB_TXN_NOSYNC;
641                 if ( bdb->bi_flags & BDB_IS_OPEN ) {
642                         bdb->bi_dbenv->set_flags( bdb->bi_dbenv, DB_TXN_NOSYNC,
643                                 c->value_int );
644                 }
645                 break;
646
647         case BDB_INDEX:
648                 rc = bdb_attr_index_config( bdb, c->fname, c->lineno,
649                         c->argc - 1, &c->argv[1] );
650
651                 if( rc != LDAP_SUCCESS ) return 1;
652                 if (( bdb->bi_flags & BDB_IS_OPEN ) && !bdb->bi_index_task ) {
653                         /* Start the task as soon as we finish here. Set a long
654                          * interval (10 hours) so that it only gets scheduled once.
655                          */
656                         if ( c->be->be_suffix == NULL || BER_BVISNULL( &c->be->be_suffix[0] ) ) {
657                                 fprintf( stderr, "%s: "
658                                         "\"index\" must occur after \"suffix\".\n",
659                                         c->log );
660                                 return 1;
661                         }
662                         bdb->bi_index_task = ldap_pvt_runqueue_insert( &slapd_rq, 36000,
663                                 bdb_online_index, c->be,
664                                 LDAP_XSTRING(bdb_online_index), c->be->be_suffix[0].bv_val );
665                 }
666                 break;
667
668         case BDB_LOCKD:
669                 rc = verb_to_mask( c->argv[1], bdb_lockd );
670                 if ( BER_BVISNULL(&bdb_lockd[rc].word) ) {
671                         fprintf( stderr, "%s: "
672                                 "bad policy (%s) in \"lockDetect <policy>\" line\n",
673                                 c->log, c->argv[1] );
674                         return 1;
675                 }
676                 bdb->bi_lock_detect = rc;
677                 break;
678
679         case BDB_SSTACK:
680                 if ( c->value_int < MINIMUM_SEARCH_STACK_DEPTH ) {
681                         fprintf( stderr,
682                 "%s: depth %d too small, using %d\n",
683                         c->log, c->value_int, MINIMUM_SEARCH_STACK_DEPTH );
684                         c->value_int = MINIMUM_SEARCH_STACK_DEPTH;
685                 }
686                 bdb->bi_search_stack_depth = c->value_int;
687                 break;
688         }
689         return 0;
690 }
691
692 int bdb_back_init_cf( BackendInfo *bi )
693 {
694         int rc;
695         bi->bi_cf_ocs = bdbocs;
696
697         rc = config_register_schema( bdbcfg, bdbocs );
698         if ( rc ) return rc;
699         return 0;
700 }