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