config.c 16.7 KB
Newer Older
1
/* config.c - bdb backend configuration file routine */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
2
/* $OpenLDAP$ */
Kurt Zeilenga's avatar
Notices    
Kurt Zeilenga committed
3
4
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
 *
Kurt Zeilenga's avatar
Kurt Zeilenga committed
5
 * Copyright 2000-2005 The OpenLDAP Foundation.
Kurt Zeilenga's avatar
Notices    
Kurt Zeilenga committed
6
7
8
9
10
11
12
13
14
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted only as authorized by the OpenLDAP
 * Public License.
 *
 * A copy of this license is available in the file LICENSE in the
 * top-level directory of the distribution or, alternatively, at
 * <http://www.OpenLDAP.org/license.html>.
Kurt Zeilenga's avatar
Kurt Zeilenga committed
15
16
17
18
19
 */

#include "portable.h"

#include <stdio.h>
20
#include <ac/ctype.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
21
22
23
24
#include <ac/string.h>

#include "back-bdb.h"

25
26
#include "config.h"

27
#include "lutil.h"
Howard Chu's avatar
Howard Chu committed
28
#include "ldap_rq.h"
29

30
31
32
#ifdef DB_DIRTY_READ
#	define	SLAP_BDB_ALLOW_DIRTY_READ
#endif
33

34
35
36
37
38
#define bdb_cf_gen			BDB_SYMBOL(cf_gen)
#define	bdb_cf_cleanup		BDB_SYMBOL(cf_cleanup)
#define bdb_checkpoint		BDB_SYMBOL(checkpoint)
#define bdb_online_index	BDB_SYMBOL(online_index)

39
static ConfigDriver bdb_cf_gen;
40
41
42

enum {
	BDB_CHKPT = 1,
43
44
	BDB_CONFIG,
	BDB_DIRECTORY,
45
46
47
48
49
50
51
52
	BDB_NOSYNC,
	BDB_DIRTYR,
	BDB_INDEX,
	BDB_LOCKD,
	BDB_SSTACK
};

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
56
			"DESC 'Directory for database content' "
			"EQUALITY caseIgnoreMatch "
57
			"SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
58
	{ "cachesize", "size", 2, 2, 0, ARG_INT|ARG_OFFSET,
59
		(void *)offsetof(struct bdb_info, bi_cache.c_maxsize),
60
		"( OLcfgDbAt:1.1 NAME 'olcDbCacheSize' "
61
			"DESC 'Entry cache size in entries' "
62
			"SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
63
	{ "checkpoint", "kbyte> <min", 3, 3, 0, ARG_MAGIC|BDB_CHKPT,
64
		bdb_cf_gen, "( OLcfgDbAt:1.2 NAME 'olcDbCheckpoint' "
65
			"DESC 'Database checkpoint interval in kbytes and minutes' "
66
			"SYNTAX OMsDirectoryString SINGLE-VALUE )",NULL, NULL },
67
	{ "dbconfig", "DB_CONFIG setting", 1, 0, 0, ARG_MAGIC|BDB_CONFIG,
68
		bdb_cf_gen, "( OLcfgDbAt:1.3 NAME 'olcDbConfig' "
69
			"DESC 'BerkeleyDB DB_CONFIG configuration directives' "
Pierangelo Masarati's avatar
Pierangelo Masarati committed
70
			"SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )", NULL, NULL },
71
	{ "dbnosync", NULL, 1, 2, 0, ARG_ON_OFF|ARG_MAGIC|BDB_NOSYNC,
72
		bdb_cf_gen, "( OLcfgDbAt:1.4 NAME 'olcDbNoSync' "
73
			"DESC 'Disable synchronous database writes' "
74
			"SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
75
76
77
78
79
80
	{ "dirtyread", NULL, 1, 2, 0,
#ifdef SLAP_BDB_ALLOW_DIRTY_READ
		ARG_ON_OFF|ARG_MAGIC|BDB_DIRTYR, bdb_cf_gen,
#else
		ARG_IGNORED, NULL,
#endif
81
		"( OLcfgDbAt:1.5 NAME 'olcDbDirtyRead' "
82
		"DESC 'Allow reads of uncommitted data' "
83
		"SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
84
	{ "idlcachesize", "size", 2, 2, 0, ARG_INT|ARG_OFFSET,
85
		(void *)offsetof(struct bdb_info,bi_idl_cache_max_size),
86
		"( OLcfgDbAt:1.6 NAME 'olcDbIDLcacheSize' "
87
		"DESC 'IDL cache size in IDLs' "
88
		"SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
Howard Chu's avatar
Howard Chu committed
89
	{ "index", "attr> <[pres,eq,approx,sub]", 2, 3, 0, ARG_MAGIC|BDB_INDEX,
90
		bdb_cf_gen, "( OLcfgDbAt:0.2 NAME 'olcDbIndex' "
91
		"DESC 'Attribute index parameters' "
Howard Chu's avatar
Howard Chu committed
92
		"EQUALITY caseIgnoreMatch "
93
94
95
		"SYNTAX OMsDirectoryString )", NULL, NULL },
	{ "linearindex", NULL, 1, 2, 0, ARG_ON_OFF|ARG_OFFSET,
		(void *)offsetof(struct bdb_info, bi_linear_index), 
96
		"( OLcfgDbAt:1.7 NAME 'olcDbLinearIndex' "
97
		"DESC 'Index attributes one at a time' "
98
		"SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
99
	{ "lockdetect", "policy", 2, 2, 0, ARG_MAGIC|BDB_LOCKD,
100
		bdb_cf_gen, "( OLcfgDbAt:1.8 NAME 'olcDbLockDetect' "
101
		"DESC 'Deadlock detection algorithm' "
102
		"SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
Howard Chu's avatar
Howard Chu committed
103
	{ "mode", "mode", 2, 2, 0, ARG_INT|ARG_OFFSET,
104
		(void *)offsetof(struct bdb_info, bi_dbenv_mode),
105
		"( OLcfgDbAt:0.3 NAME 'olcDbMode' "
106
		"DESC 'Unix permissions of database files' "
107
		"SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
108
	{ "searchstack", "depth", 2, 2, 0, ARG_INT|ARG_MAGIC|BDB_SSTACK,
109
		bdb_cf_gen, "( OLcfgDbAt:1.9 NAME 'olcDbSearchStack' "
110
		"DESC 'Depth of search stack in IDLs' "
111
		"SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
Howard Chu's avatar
Howard Chu committed
112
	{ "shm_key", "key", 2, 2, 0, ARG_INT|ARG_OFFSET,
113
		(void *)offsetof(struct bdb_info, bi_shm_key), 
114
		"( OLcfgDbAt:1.10 NAME 'olcDbShmKey' "
115
		"DESC 'Key for shared memory region' "
116
		"SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
117
118
119
120
121
	{ NULL, NULL, 0, 0, 0, ARG_IGNORED,
		NULL, NULL, NULL, NULL }
};

static ConfigOCs bdbocs[] = {
122
123
124
125
126
127
128
	{
#ifdef BDB_HIER
		"( OLcfgDbOc:1.2 "
		"NAME 'olcHdbConfig' "
		"DESC 'HDB backend configuration' "
#else
		"( OLcfgDbOc:1.1 "
129
		"NAME 'olcBdbConfig' "
130
		"DESC 'BDB backend configuration' "
131
#endif
Howard Chu's avatar
Howard Chu committed
132
		"SUP olcDatabaseConfig "
133
134
135
136
137
		"MUST olcDbDirectory "
		"MAY ( olcDbCacheSize $ olcDbCheckpoint $ olcDbConfig $ "
		"olcDbNoSync $ olcDbDirtyRead $ olcDbIDLcacheSize $ "
		"olcDbIndex $ olcDbLinearIndex $ olcDbLockDetect $ "
		"olcDbMode $ olcDbSearchStack $ olcDbShmKey ) )",
138
		 	Cft_Database, bdbcfg },
Howard Chu's avatar
Howard Chu committed
139
	{ NULL, 0, NULL }
140
141
};

Howard Chu's avatar
Howard Chu committed
142
static slap_verbmasks bdb_lockd[] = {
143
144
145
146
147
148
	{ BER_BVC("default"), DB_LOCK_DEFAULT },
	{ BER_BVC("oldest"), DB_LOCK_OLDEST },
	{ BER_BVC("random"), DB_LOCK_RANDOM },
	{ BER_BVC("youngest"), DB_LOCK_YOUNGEST },
	{ BER_BVC("fewest"), DB_LOCK_MINLOCKS },
	{ BER_BVNULL, 0 }
149
150
};

Howard Chu's avatar
Howard Chu committed
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* perform periodic checkpoints */
static void *
bdb_checkpoint( void *ctx, void *arg )
{
	struct re_s *rtask = arg;
	struct bdb_info *bdb = rtask->arg;
	
	TXN_CHECKPOINT( bdb->bi_dbenv, bdb->bi_txn_cp_kbyte,
		bdb->bi_txn_cp_min, 0 );
	ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
	ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
	ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
	return NULL;
}

Howard Chu's avatar
Howard Chu committed
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/* reindex entries on the fly */
static void *
bdb_online_index( void *ctx, void *arg )
{
	struct re_s *rtask = arg;
	BackendDB *be = rtask->arg;
	struct bdb_info *bdb = be->be_private;

	Connection conn = {0};
	char opbuf[OPERATION_BUFFER_SIZE];
	Operation *op = (Operation *)opbuf;

	DBC *curs;
	DBT key, data;
	DB_TXN *txn;
	DB_LOCK lock;
	u_int32_t locker;
	ID id, nid;
	EntryInfo *ei;
	int rc, getnext = 1;

	connection_fake_init( &conn, op, ctx );

	op->o_bd = be;

	DBTzero( &key );
	DBTzero( &data );
	
	id = 1;
	key.data = &nid;
	key.size = key.ulen = sizeof(ID);
	key.flags = DB_DBT_USERMEM;

	data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
	data.dlen = data.ulen = 0;

	while ( 1 ) {
203
204
205
		if ( slapd_shutdown )
			break;

Howard Chu's avatar
Howard Chu committed
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
		rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &txn, bdb->bi_db_opflags );
		if ( rc ) 
			break;
		locker = TXN_ID( txn );
		if ( getnext ) {
			getnext = 0;
			BDB_ID2DISK( id, &nid );
			rc = bdb->bi_id2entry->bdi_db->cursor(
				bdb->bi_id2entry->bdi_db, txn, &curs, bdb->bi_db_opflags );
			if ( rc ) {
				TXN_ABORT( txn );
				break;
			}
			rc = curs->c_get( curs, &key, &data, DB_SET_RANGE );
			curs->c_close( curs );
			if ( rc ) {
				TXN_ABORT( txn );
				if ( rc == DB_NOTFOUND )
					rc = 0;
				if ( rc == DB_LOCK_DEADLOCK ) {
					ldap_pvt_thread_yield();
					continue;
				}
				break;
			}
			BDB_DISK2ID( &nid, &id );
		}

		ei = NULL;
		rc = bdb_cache_find_id( op, txn, id, &ei, 0, locker, &lock );
		if ( rc ) {
			TXN_ABORT( txn );
			if ( rc == DB_LOCK_DEADLOCK ) {
				ldap_pvt_thread_yield();
				continue;
			}
			if ( rc == DB_NOTFOUND ) {
Howard Chu's avatar
Howard Chu committed
243
				id++;
Howard Chu's avatar
Howard Chu committed
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
				getnext = 1;
				continue;
			}
			break;
		}
		if ( ei->bei_e ) {
			rc = bdb_index_entry( op, txn, BDB_INDEX_UPDATE_OP, ei->bei_e );
			if ( rc == DB_LOCK_DEADLOCK ) {
				TXN_ABORT( txn );
				ldap_pvt_thread_yield();
				continue;
			}
			if ( rc == 0 ) {
				rc = TXN_COMMIT( txn, 0 );
				txn = NULL;
			}
			if ( rc )
				break;
		}
		id++;
		getnext = 1;
	}
266

Howard Chu's avatar
Howard Chu committed
267
268
	ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
	ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
269
	bdb->bi_index_task = NULL;
Howard Chu's avatar
Howard Chu committed
270
271
272
273
274
275
276
277
278
279
280
	ldap_pvt_runqueue_remove( &slapd_rq, rtask );
	ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );

	return NULL;
}

/* Cleanup loose ends after Modify completes */
static int
bdb_cf_cleanup( ConfigArgs *c )
{
	struct bdb_info *bdb = c->be->be_private;
Howard Chu's avatar
Howard Chu committed
281
	int rc = 0;
Howard Chu's avatar
Howard Chu committed
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301

	if ( bdb->bi_flags & BDB_UPD_CONFIG ) {
		if ( bdb->bi_db_config ) {
			int i;
			FILE *f = fopen( bdb->bi_db_config_path, "w" );
			if ( f ) {
				for (i=0; bdb->bi_db_config[i].bv_val; i++)
					fprintf( f, "%s\n", bdb->bi_db_config[i].bv_val );
				fclose( f );
			}
		} else {
			unlink( bdb->bi_db_config_path );
		}
		bdb->bi_flags ^= BDB_UPD_CONFIG;
	}

	if ( bdb->bi_flags & BDB_DEL_INDEX ) {
		bdb_attr_flush( bdb );
		bdb->bi_flags ^= BDB_DEL_INDEX;
	}
302
	
Howard Chu's avatar
Howard Chu committed
303
304
305
306
307
308
	if ( bdb->bi_flags & BDB_RE_OPEN ) {
		bdb->bi_flags ^= BDB_RE_OPEN;
		rc = c->be->bd_info->bi_db_close( c->be );
		if ( rc == 0 )
			rc = c->be->bd_info->bi_db_open( c->be );
		/* If this fails, we need to restart */
309
310
311
312
313
		if ( rc ) {
			slapd_shutdown = 2;
			Debug( LDAP_DEBUG_ANY, LDAP_XSTRING(bdb_cf_cleanup)
				": failed to reopen database, rc=%d", rc, 0, 0 );
		}
Howard Chu's avatar
Howard Chu committed
314
315
	}
	return rc;
Howard Chu's avatar
Howard Chu committed
316
317
}

318
319
320
321
322
323
static int
bdb_cf_gen(ConfigArgs *c)
{
	struct bdb_info *bdb = c->be->be_private;
	int rc;

324
	if ( c->op == SLAP_CONFIG_EMIT ) {
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
		rc = 0;
		switch( c->type ) {
		case BDB_CHKPT:
			if (bdb->bi_txn_cp ) {
				char buf[64];
				struct berval bv;
				bv.bv_len = sprintf( buf, "%d %d", bdb->bi_txn_cp_kbyte,
					bdb->bi_txn_cp_min );
				bv.bv_val = buf;
				value_add_one( &c->rvalue_vals, &bv );
			} else{
				rc = 1;
			}
			break;

340
341
342
343
344
345
346
347
348
		case BDB_DIRECTORY:
			if ( bdb->bi_dbenv_home ) {
				c->value_string = ch_strdup( bdb->bi_dbenv_home );
			} else {
				rc = 1;
			}
			break;

		case BDB_CONFIG:
Howard Chu's avatar
Howard Chu committed
349
350
351
352
353
354
355
356
357
358
			if (( slapMode&SLAP_SERVER_MODE ) && !( bdb->bi_flags&BDB_IS_OPEN )
				&& !bdb->bi_db_config ) {
				char	buf[SLAP_TEXT_BUFLEN];
				FILE *f = fopen( bdb->bi_db_config_path, "r" );
				struct berval bv;

				if ( f ) {
					bdb->bi_flags |= BDB_HAS_CONFIG;
					while ( fgets( buf, sizeof(buf), f )) {
						ber_str2bv( buf, 0, 1, &bv );
Luke Howard's avatar
Luke Howard committed
359
						if ( bv.bv_len > 0 && bv.bv_val[bv.bv_len-1] == '\n' ) {
Howard Chu's avatar
Howard Chu committed
360
361
362
363
							bv.bv_len--;
							bv.bv_val[bv.bv_len] = '\0';
						}
						/* shouldn't need this, but ... */
Luke Howard's avatar
Luke Howard committed
364
						if ( bv.bv_len > 0 && bv.bv_val[bv.bv_len-1] == '\r' ) {
Howard Chu's avatar
Howard Chu committed
365
366
367
368
369
370
371
372
							bv.bv_len--;
							bv.bv_val[bv.bv_len] = '\0';
						}
						ber_bvarray_add( &bdb->bi_db_config, &bv );
					}
					fclose( f );
				}
			}
373
374
375
376
377
378
379
380
381
382
383
384
385
386
			if ( bdb->bi_db_config ) {
				int i;
				struct berval bv;

				bv.bv_val = c->log;
				for (i=0; !BER_BVISNULL(&bdb->bi_db_config[i]); i++) {
					bv.bv_len = sprintf( bv.bv_val, "{%d}%s", i,
						bdb->bi_db_config[i].bv_val );
					value_add_one( &c->rvalue_vals, &bv );
				}
			}
			if ( !c->rvalue_vals ) rc = 1;
			break;

387
388
389
390
391
392
		case BDB_NOSYNC:
			if ( bdb->bi_dbenv_xflags & DB_TXN_NOSYNC )
				c->value_int = 1;
			break;
			
		case BDB_INDEX:
393
394
			bdb_attr_index_unparse( bdb, &c->rvalue_vals );
			if ( !c->rvalue_vals ) rc = 1;
395
396
397
398
399
400
			break;

		case BDB_LOCKD:
			rc = 1;
			if ( bdb->bi_lock_detect != DB_LOCK_DEFAULT ) {
				int i;
401
				for (i=0; !BER_BVISNULL(&bdb_lockd[i].word); i++) {
402
					if ( bdb->bi_lock_detect == bdb_lockd[i].mask ) {
403
						value_add_one( &c->rvalue_vals, &bdb_lockd[i].word );
404
405
406
407
408
409
410
411
412
413
						rc = 0;
						break;
					}
				}
			}
			break;

		case BDB_SSTACK:
			c->value_int = bdb->bi_search_stack_depth;
			break;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
414
		}
415
		return rc;
Howard Chu's avatar
Howard Chu committed
416
417
418
419
420
421
422
423
424
	} else if ( c->op == LDAP_MOD_DELETE ) {
		rc = 0;
		switch( c->type ) {
		/* single-valued no-ops */
		case BDB_LOCKD:
		case BDB_SSTACK:
			break;

		case BDB_CHKPT:
Howard Chu's avatar
Howard Chu committed
425
426
427
428
429
430
431
			if ( bdb->bi_txn_cp_task ) {
				struct re_s *re = bdb->bi_txn_cp_task;
				bdb->bi_txn_cp_task = NULL;
				if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ))
					ldap_pvt_runqueue_stoptask( &slapd_rq, re );
				ldap_pvt_runqueue_remove( &slapd_rq, re );
			}
Howard Chu's avatar
Howard Chu committed
432
433
434
			bdb->bi_txn_cp = 0;
			break;
		case BDB_CONFIG:
Howard Chu's avatar
Howard Chu committed
435
436
437
438
439
440
441
442
443
444
445
			if ( c->valx < 0 ) {
				ber_bvarray_free( bdb->bi_db_config );
				bdb->bi_db_config = NULL;
			} else {
				int i = c->valx;
				ch_free( bdb->bi_db_config[i].bv_val );
				for (; bdb->bi_db_config[i].bv_val; i++)
					bdb->bi_db_config[i] = bdb->bi_db_config[i+1];
			}
			bdb->bi_flags |= BDB_UPD_CONFIG;
			c->cleanup = bdb_cf_cleanup;
Howard Chu's avatar
Howard Chu committed
446
447
			break;
		case BDB_DIRECTORY:
Howard Chu's avatar
Howard Chu committed
448
449
450
451
452
453
454
			bdb->bi_flags |= BDB_RE_OPEN;
			bdb->bi_flags ^= BDB_HAS_CONFIG;
			ch_free( bdb->bi_dbenv_home );
			bdb->bi_dbenv_home = NULL;
			ch_free( bdb->bi_db_config_path );
			bdb->bi_db_config_path = NULL;
			c->cleanup = bdb_cf_cleanup;
Howard Chu's avatar
Howard Chu committed
455
456
			ldap_pvt_thread_pool_purgekey( bdb->bi_dbenv );
			ldap_pvt_thread_pool_purgekey( ((char *)bdb->bi_dbenv) + 1 );
Howard Chu's avatar
Howard Chu committed
457
458
459
460
461
462
463
464
465
466
467
468
			break;
		case BDB_NOSYNC:
			bdb->bi_dbenv->set_flags( bdb->bi_dbenv, DB_TXN_NOSYNC, 0 );
			break;
		case BDB_INDEX: {
			AttributeDescription *ad = NULL;
			struct berval bv, def = BER_BVC("default");
			char *ptr;
			const char *text;
			for (ptr = c->line; !isspace( *ptr ); ptr++);
			bv.bv_val = c->line;
			bv.bv_len = ptr - bv.bv_val;
Howard Chu's avatar
Howard Chu committed
469
			if ( bvmatch( &bv, &def )) {
Howard Chu's avatar
Howard Chu committed
470
471
472
				bdb->bi_defaultmask = 0;
			} else {
				slap_bv2ad( &bv, &ad, &text );
Howard Chu's avatar
Howard Chu committed
473
474
475
476
477
478
				if ( ad ) {
					AttrInfo *ai = bdb_attr_mask( bdb, ad );
					ai->ai_indexmask |= BDB_INDEX_DELETING;
					bdb->bi_flags |= BDB_DEL_INDEX;
					c->cleanup = bdb_cf_cleanup;
				}
Howard Chu's avatar
Howard Chu committed
479
480
481
482
483
			}
			}
			break;
		}
		return rc;
484
	}
Howard Chu's avatar
Howard Chu committed
485

486
487
	switch( c->type ) {
	case BDB_CHKPT:
Kurt Zeilenga's avatar
Kurt Zeilenga committed
488
		bdb->bi_txn_cp = 1;
489
490
		bdb->bi_txn_cp_kbyte = strtol( c->argv[1], NULL, 0 );
		bdb->bi_txn_cp_min = strtol( c->argv[2], NULL, 0 );
Howard Chu's avatar
Howard Chu committed
491
492
493
494
495
		/* If we're in server mode and time-based checkpointing is enabled,
		 * submit a task to perform periodic checkpoints.
		 */
		if ((slapMode & SLAP_SERVER_MODE) && bdb->bi_txn_cp_min ) {
			struct re_s *re = bdb->bi_txn_cp_task;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
496
			if ( re ) {
Howard Chu's avatar
Howard Chu committed
497
				re->interval.tv_sec = bdb->bi_txn_cp_min * 60;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
498
499
500
501
502
503
504
			} else {
				if ( c->be->be_suffix == NULL || BER_BVISNULL( &c->be->be_suffix[0] ) ) {
					fprintf( stderr, "%s: "
						"\"checkpoint\" must occur after \"suffix\".\n",
						c->log );
					return 1;
				}
Howard Chu's avatar
Howard Chu committed
505
				bdb->bi_txn_cp_task = ldap_pvt_runqueue_insert( &slapd_rq,
506
					bdb->bi_txn_cp_min * 60, bdb_checkpoint, bdb,
507
					LDAP_XSTRING(bdb_checkpoint), c->be->be_suffix[0].bv_val );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
508
			}
Howard Chu's avatar
Howard Chu committed
509
		}
510
		break;
511

512
	case BDB_CONFIG: {
Howard Chu's avatar
Howard Chu committed
513
		char *ptr = c->line;
514
		struct berval bv;
Howard Chu's avatar
Howard Chu committed
515
516
517
518
519
520

		if ( c->op == SLAP_CONFIG_ADD ) {
			ptr += STRLENOF("dbconfig");
			while (!isspace(*ptr)) ptr++;
			while (isspace(*ptr)) ptr++;
		}
Howard Chu's avatar
Howard Chu committed
521

Howard Chu's avatar
Howard Chu committed
522
523
524
525
		if ( bdb->bi_flags & BDB_IS_OPEN ) {
			bdb->bi_flags |= BDB_UPD_CONFIG;
			c->cleanup = bdb_cf_cleanup;
		} else {
526
527
528
529
530
531
		/* If we're just starting up...
		 */
			FILE *f;
			/* If a DB_CONFIG file exists, or we don't know the path
			 * to the DB_CONFIG file, ignore these directives
			 */
Howard Chu's avatar
Howard Chu committed
532
			if (( bdb->bi_flags & BDB_HAS_CONFIG ) || !bdb->bi_db_config_path )
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
				break;
			f = fopen( bdb->bi_db_config_path, "a" );
			if ( f ) {
				/* FIXME: EBCDIC probably needs special handling */
				fprintf( f, "%s\n", ptr );
				fclose( f );
			}
		}
		ber_str2bv( ptr, 0, 1, &bv );
		ber_bvarray_add( &bdb->bi_db_config, &bv );
		}
		break;

	case BDB_DIRECTORY: {
		FILE *f;
		char *ptr;

Howard Chu's avatar
Howard Chu committed
550
551
		if ( bdb->bi_dbenv_home )
			ch_free( bdb->bi_dbenv_home );
552
553
554
		bdb->bi_dbenv_home = c->value_string;

		/* See if a DB_CONFIG file already exists here */
Howard Chu's avatar
Howard Chu committed
555
556
		if ( bdb->bi_db_config_path )
			ch_free( bdb->bi_db_config_path );
557
		bdb->bi_db_config_path = ch_malloc( strlen( bdb->bi_dbenv_home ) +
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
558
			STRLENOF(LDAP_DIRSEP) + STRLENOF("DB_CONFIG") + 1 );
559
560
561
562
563
564
		ptr = lutil_strcopy( bdb->bi_db_config_path, bdb->bi_dbenv_home );
		*ptr++ = LDAP_DIRSEP[0];
		strcpy( ptr, "DB_CONFIG" );

		f = fopen( bdb->bi_db_config_path, "r" );
		if ( f ) {
Howard Chu's avatar
Howard Chu committed
565
			bdb->bi_flags |= BDB_HAS_CONFIG;
566
567
568
569
570
			fclose(f);
		}
		}
		break;

571
572
573
574
575
	case BDB_NOSYNC:
		if ( c->value_int )
			bdb->bi_dbenv_xflags |= DB_TXN_NOSYNC;
		else
			bdb->bi_dbenv_xflags &= ~DB_TXN_NOSYNC;
Howard Chu's avatar
Howard Chu committed
576
		if ( bdb->bi_flags & BDB_IS_OPEN ) {
Howard Chu's avatar
Howard Chu committed
577
578
579
			bdb->bi_dbenv->set_flags( bdb->bi_dbenv, DB_TXN_NOSYNC,
				c->value_int );
		}
580
		break;
581

582
583
584
	case BDB_INDEX:
		rc = bdb_attr_index_config( bdb, c->fname, c->lineno,
			c->argc - 1, &c->argv[1] );
585

586
		if( rc != LDAP_SUCCESS ) return 1;
587
		if (( bdb->bi_flags & BDB_IS_OPEN ) && !bdb->bi_index_task ) {
Howard Chu's avatar
Howard Chu committed
588
589
590
			/* Start the task as soon as we finish here. Set a long
			 * interval (10 hours) so that it only gets scheduled once.
			 */
Pierangelo Masarati's avatar
Pierangelo Masarati committed
591
592
593
594
595
596
			if ( c->be->be_suffix == NULL || BER_BVISNULL( &c->be->be_suffix[0] ) ) {
				fprintf( stderr, "%s: "
					"\"index\" must occur after \"suffix\".\n",
					c->log );
				return 1;
			}
Howard Chu's avatar
Howard Chu committed
597
			bdb->bi_index_task = ldap_pvt_runqueue_insert( &slapd_rq, 36000,
598
				bdb_online_index, c->be,
599
				LDAP_XSTRING(bdb_online_index), c->be->be_suffix[0].bv_val );
Howard Chu's avatar
Howard Chu committed
600
		}
601
		break;
602

603
	case BDB_LOCKD:
Howard Chu's avatar
Howard Chu committed
604
		rc = verb_to_mask( c->argv[1], bdb_lockd );
605
		if ( BER_BVISNULL(&bdb_lockd[rc].word) ) {
606
			fprintf( stderr, "%s: "
Howard Chu's avatar
Howard Chu committed
607
				"bad policy (%s) in \"lockDetect <policy>\" line\n",
608
				c->log, c->argv[1] );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
609
			return 1;
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
610
		}
611
612
		bdb->bi_lock_detect = rc;
		break;
613

614
615
	case BDB_SSTACK:
		if ( c->value_int < MINIMUM_SEARCH_STACK_DEPTH ) {
616
			fprintf( stderr,
617
618
619
		"%s: depth %d too small, using %d\n",
			c->log, c->value_int, MINIMUM_SEARCH_STACK_DEPTH );
			c->value_int = MINIMUM_SEARCH_STACK_DEPTH;
620
		}
621
622
		bdb->bi_search_stack_depth = c->value_int;
		break;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
623
624
625
	}
	return 0;
}
626
627
628
629

int bdb_back_init_cf( BackendInfo *bi )
{
	int rc;
630
	bi->bi_cf_ocs = bdbocs;
631

632
	rc = config_register_schema( bdbcfg, bdbocs );
633
	if ( rc ) return rc;
634
	return 0;
635
}