1 /* monitor.c - monitor bdb backend */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2000-2021 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/string.h>
21 #include <ac/unistd.h>
22 #include <ac/stdlib.h>
23 #include <ac/errno.h>
24 #include <sys/stat.h>
25 #include "lutil.h"
26 #include "back-bdb.h"
27 
28 #include "../back-monitor/back-monitor.h"
29 
30 #include "config.h"
31 
32 static ObjectClass		*oc_olmBDBDatabase;
33 
34 static AttributeDescription	*ad_olmBDBEntryCache,
35 	*ad_olmBDBDNCache, *ad_olmBDBIDLCache,
36 	*ad_olmDbDirectory;
37 
38 #ifdef BDB_MONITOR_IDX
39 static int
40 bdb_monitor_idx_entry_add(
41 	struct bdb_info	*bdb,
42 	Entry		*e );
43 
44 static AttributeDescription	*ad_olmDbNotIndexed;
45 #endif /* BDB_MONITOR_IDX */
46 
47 /*
48  * NOTE: there's some confusion in monitor OID arc;
49  * by now, let's consider:
50  *
51  * Subsystems monitor attributes	1.3.6.1.4.1.4203.666.1.55.0
52  * Databases monitor attributes		1.3.6.1.4.1.4203.666.1.55.0.1
53  * BDB database monitor attributes	1.3.6.1.4.1.4203.666.1.55.0.1.1
54  *
55  * Subsystems monitor objectclasses	1.3.6.1.4.1.4203.666.3.16.0
56  * Databases monitor objectclasses	1.3.6.1.4.1.4203.666.3.16.0.1
57  * BDB database monitor objectclasses	1.3.6.1.4.1.4203.666.3.16.0.1.1
58  */
59 
60 static struct {
61 	char			*name;
62 	char			*oid;
63 }		s_oid[] = {
64 	{ "olmBDBAttributes",			"olmDatabaseAttributes:1" },
65 	{ "olmBDBObjectClasses",		"olmDatabaseObjectClasses:1" },
66 
67 	{ NULL }
68 };
69 
70 static struct {
71 	char			*desc;
72 	AttributeDescription	**ad;
73 }		s_at[] = {
74 	{ "( olmBDBAttributes:1 "
75 		"NAME ( 'olmBDBEntryCache' ) "
76 		"DESC 'Number of items in Entry Cache' "
77 		"SUP monitorCounter "
78 		"NO-USER-MODIFICATION "
79 		"USAGE dSAOperation )",
80 		&ad_olmBDBEntryCache },
81 
82 	{ "( olmBDBAttributes:2 "
83 		"NAME ( 'olmBDBDNCache' ) "
84 		"DESC 'Number of items in DN Cache' "
85 		"SUP monitorCounter "
86 		"NO-USER-MODIFICATION "
87 		"USAGE dSAOperation )",
88 		&ad_olmBDBDNCache },
89 
90 	{ "( olmBDBAttributes:3 "
91 		"NAME ( 'olmBDBIDLCache' ) "
92 		"DESC 'Number of items in IDL Cache' "
93 		"SUP monitorCounter "
94 		"NO-USER-MODIFICATION "
95 		"USAGE dSAOperation )",
96 		&ad_olmBDBIDLCache },
97 
98 	{ "( olmDatabaseAttributes:1 "
99 		"NAME ( 'olmDbDirectory' ) "
100 		"DESC 'Path name of the directory "
101 			"where the database environment resides' "
102 		"SUP monitoredInfo "
103 		"NO-USER-MODIFICATION "
104 		"USAGE dSAOperation )",
105 		&ad_olmDbDirectory },
106 
107 #ifdef BDB_MONITOR_IDX
108 	{ "( olmDatabaseAttributes:2 "
109 		"NAME ( 'olmDbNotIndexed' ) "
110 		"DESC 'Missing indexes resulting from candidate selection' "
111 		"SUP monitoredInfo "
112 		"NO-USER-MODIFICATION "
113 		"USAGE dSAOperation )",
114 		&ad_olmDbNotIndexed },
115 #endif /* BDB_MONITOR_IDX */
116 
117 	{ NULL }
118 };
119 
120 static struct {
121 	char		*desc;
122 	ObjectClass	**oc;
123 }		s_oc[] = {
124 	/* augments an existing object, so it must be AUXILIARY
125 	 * FIXME: derive from some ABSTRACT "monitoredEntity"? */
126 	{ "( olmBDBObjectClasses:1 "
127 		"NAME ( 'olmBDBDatabase' ) "
128 		"SUP top AUXILIARY "
129 		"MAY ( "
130 			"olmBDBEntryCache "
131 			"$ olmBDBDNCache "
132 			"$ olmBDBIDLCache "
133 			"$ olmDbDirectory "
134 #ifdef BDB_MONITOR_IDX
135 			"$ olmDbNotIndexed "
136 #endif /* BDB_MONITOR_IDX */
137 			") )",
138 		&oc_olmBDBDatabase },
139 
140 	{ NULL }
141 };
142 
143 static int
bdb_monitor_update(Operation * op,SlapReply * rs,Entry * e,void * priv)144 bdb_monitor_update(
145 	Operation	*op,
146 	SlapReply	*rs,
147 	Entry		*e,
148 	void		*priv )
149 {
150 	struct bdb_info		*bdb = (struct bdb_info *) priv;
151 	Attribute		*a;
152 
153 	char			buf[ BUFSIZ ];
154 	struct berval		bv;
155 
156 	assert( ad_olmBDBEntryCache != NULL );
157 
158 	a = attr_find( e->e_attrs, ad_olmBDBEntryCache );
159 	assert( a != NULL );
160 	bv.bv_val = buf;
161 	bv.bv_len = snprintf( buf, sizeof( buf ), "%lu", bdb->bi_cache.c_cursize );
162 	ber_bvreplace( &a->a_vals[ 0 ], &bv );
163 
164 	a = attr_find( e->e_attrs, ad_olmBDBDNCache );
165 	assert( a != NULL );
166 	bv.bv_len = snprintf( buf, sizeof( buf ), "%lu", bdb->bi_cache.c_eiused );
167 	ber_bvreplace( &a->a_vals[ 0 ], &bv );
168 
169 	a = attr_find( e->e_attrs, ad_olmBDBIDLCache );
170 	assert( a != NULL );
171 	bv.bv_len = snprintf( buf, sizeof( buf ), "%lu", bdb->bi_idl_cache_size );
172 	ber_bvreplace( &a->a_vals[ 0 ], &bv );
173 
174 #ifdef BDB_MONITOR_IDX
175 	bdb_monitor_idx_entry_add( bdb, e );
176 #endif /* BDB_MONITOR_IDX */
177 
178 	return SLAP_CB_CONTINUE;
179 }
180 
181 #if 0	/* uncomment if required */
182 static int
183 bdb_monitor_modify(
184 	Operation	*op,
185 	SlapReply	*rs,
186 	Entry		*e,
187 	void		*priv )
188 {
189 	return SLAP_CB_CONTINUE;
190 }
191 #endif
192 
193 static int
bdb_monitor_free(Entry * e,void ** priv)194 bdb_monitor_free(
195 	Entry		*e,
196 	void		**priv )
197 {
198 	struct berval	values[ 2 ];
199 	Modification	mod = { 0 };
200 
201 	const char	*text;
202 	char		textbuf[ SLAP_TEXT_BUFLEN ];
203 
204 	int		i, rc;
205 
206 	/* NOTE: if slap_shutdown != 0, priv might have already been freed */
207 	*priv = NULL;
208 
209 	/* Remove objectClass */
210 	mod.sm_op = LDAP_MOD_DELETE;
211 	mod.sm_desc = slap_schema.si_ad_objectClass;
212 	mod.sm_values = values;
213 	mod.sm_numvals = 1;
214 	values[ 0 ] = oc_olmBDBDatabase->soc_cname;
215 	BER_BVZERO( &values[ 1 ] );
216 
217 	rc = modify_delete_values( e, &mod, 1, &text,
218 		textbuf, sizeof( textbuf ) );
219 	/* don't care too much about return code... */
220 
221 	/* remove attrs */
222 	mod.sm_values = NULL;
223 	mod.sm_numvals = 0;
224 	for ( i = 0; s_at[ i ].desc != NULL; i++ ) {
225 		mod.sm_desc = *s_at[ i ].ad;
226 		rc = modify_delete_values( e, &mod, 1, &text,
227 			textbuf, sizeof( textbuf ) );
228 		/* don't care too much about return code... */
229 	}
230 
231 	return SLAP_CB_CONTINUE;
232 }
233 
234 #define	bdb_monitor_initialize	BDB_SYMBOL(monitor_initialize)
235 
236 /*
237  * call from within bdb_initialize()
238  */
239 static int
bdb_monitor_initialize(void)240 bdb_monitor_initialize( void )
241 {
242 	int		i, code;
243 	ConfigArgs c;
244 	char	*argv[ 3 ];
245 
246 	static int	bdb_monitor_initialized = 0;
247 
248 	/* set to 0 when successfully initialized; otherwise, remember failure */
249 	static int	bdb_monitor_initialized_failure = 1;
250 
251 	if ( bdb_monitor_initialized++ ) {
252 		return bdb_monitor_initialized_failure;
253 	}
254 
255 	if ( backend_info( "monitor" ) == NULL ) {
256 		return -1;
257 	}
258 
259 	/* register schema here */
260 
261 	argv[ 0 ] = "back-bdb/back-hdb monitor";
262 	c.argv = argv;
263 	c.argc = 3;
264 	c.fname = argv[0];
265 
266 	for ( i = 0; s_oid[ i ].name; i++ ) {
267 		c.lineno = i;
268 		argv[ 1 ] = s_oid[ i ].name;
269 		argv[ 2 ] = s_oid[ i ].oid;
270 
271 		if ( parse_oidm( &c, 0, NULL ) != 0 ) {
272 			Debug( LDAP_DEBUG_ANY, LDAP_XSTRING(bdb_monitor_initialize)
273 				": unable to add "
274 				"objectIdentifier \"%s=%s\"\n",
275 				s_oid[ i ].name, s_oid[ i ].oid, 0 );
276 			return 2;
277 		}
278 	}
279 
280 	for ( i = 0; s_at[ i ].desc != NULL; i++ ) {
281 		code = register_at( s_at[ i ].desc, s_at[ i ].ad, 1 );
282 		if ( code != LDAP_SUCCESS ) {
283 			Debug( LDAP_DEBUG_ANY, LDAP_XSTRING(bdb_monitor_initialize)
284 				": register_at failed for attributeType (%s)\n",
285 				s_at[ i ].desc, 0, 0 );
286 			return 3;
287 
288 		} else {
289 			(*s_at[ i ].ad)->ad_type->sat_flags |= SLAP_AT_HIDE;
290 		}
291 	}
292 
293 	for ( i = 0; s_oc[ i ].desc != NULL; i++ ) {
294 		code = register_oc( s_oc[ i ].desc, s_oc[ i ].oc, 1 );
295 		if ( code != LDAP_SUCCESS ) {
296 			Debug( LDAP_DEBUG_ANY, LDAP_XSTRING(bdb_monitor_initialize)
297 				": register_oc failed for objectClass (%s)\n",
298 				s_oc[ i ].desc, 0, 0 );
299 			return 4;
300 
301 		} else {
302 			(*s_oc[ i ].oc)->soc_flags |= SLAP_OC_HIDE;
303 		}
304 	}
305 
306 	return ( bdb_monitor_initialized_failure = LDAP_SUCCESS );
307 }
308 
309 /*
310  * call from within bdb_db_init()
311  */
312 int
bdb_monitor_db_init(BackendDB * be)313 bdb_monitor_db_init( BackendDB *be )
314 {
315 	struct bdb_info		*bdb = (struct bdb_info *) be->be_private;
316 
317 	if ( bdb_monitor_initialize() == LDAP_SUCCESS ) {
318 		/* monitoring in back-bdb is on by default */
319 		SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_MONITORING;
320 	}
321 
322 #ifdef BDB_MONITOR_IDX
323 	bdb->bi_idx = NULL;
324 	ldap_pvt_thread_mutex_init( &bdb->bi_idx_mutex );
325 #endif /* BDB_MONITOR_IDX */
326 
327 	return 0;
328 }
329 
330 /*
331  * call from within bdb_db_open()
332  */
333 int
bdb_monitor_db_open(BackendDB * be)334 bdb_monitor_db_open( BackendDB *be )
335 {
336 	struct bdb_info		*bdb = (struct bdb_info *) be->be_private;
337 	Attribute		*a, *next;
338 	monitor_callback_t	*cb = NULL;
339 	int			rc = 0;
340 	BackendInfo		*mi;
341 	monitor_extra_t		*mbe;
342 
343 	if ( !SLAP_DBMONITORING( be ) ) {
344 		return 0;
345 	}
346 
347 	mi = backend_info( "monitor" );
348 	if ( !mi || !mi->bi_extra ) {
349 		SLAP_DBFLAGS( be ) ^= SLAP_DBFLAG_MONITORING;
350 		return 0;
351 	}
352 	mbe = mi->bi_extra;
353 
354 	/* don't bother if monitor is not configured */
355 	if ( !mbe->is_configured() ) {
356 		static int warning = 0;
357 
358 		if ( warning++ == 0 ) {
359 			Debug( LDAP_DEBUG_ANY, LDAP_XSTRING(bdb_monitor_db_open)
360 				": monitoring disabled; "
361 				"configure monitor database to enable\n",
362 				0, 0, 0 );
363 		}
364 
365 		return 0;
366 	}
367 
368 	/* alloc as many as required (plus 1 for objectClass) */
369 	a = attrs_alloc( 1 + 4 );
370 	if ( a == NULL ) {
371 		rc = 1;
372 		goto cleanup;
373 	}
374 
375 	a->a_desc = slap_schema.si_ad_objectClass;
376 	attr_valadd( a, &oc_olmBDBDatabase->soc_cname, NULL, 1 );
377 	next = a->a_next;
378 
379 	{
380 		struct berval	bv = BER_BVC( "0" );
381 
382 		next->a_desc = ad_olmBDBEntryCache;
383 		attr_valadd( next, &bv, NULL, 1 );
384 		next = next->a_next;
385 
386 		next->a_desc = ad_olmBDBDNCache;
387 		attr_valadd( next, &bv, NULL, 1 );
388 		next = next->a_next;
389 
390 		next->a_desc = ad_olmBDBIDLCache;
391 		attr_valadd( next, &bv, NULL, 1 );
392 		next = next->a_next;
393 	}
394 
395 	{
396 		struct berval	bv, nbv;
397 		ber_len_t	pathlen = 0, len = 0;
398 		char		path[ MAXPATHLEN ] = { '\0' };
399 		char		*fname = bdb->bi_dbenv_home,
400 				*ptr;
401 
402 		len = strlen( fname );
403 		if ( fname[ 0 ] != '/' ) {
404 			/* get full path name */
405 			getcwd( path, sizeof( path ) );
406 			pathlen = strlen( path );
407 
408 			if ( fname[ 0 ] == '.' && fname[ 1 ] == '/' ) {
409 				fname += 2;
410 				len -= 2;
411 			}
412 		}
413 
414 		bv.bv_len = pathlen + STRLENOF( "/" ) + len;
415 		ptr = bv.bv_val = ch_malloc( bv.bv_len + STRLENOF( "/" ) + 1 );
416 		if ( pathlen ) {
417 			ptr = lutil_strncopy( ptr, path, pathlen );
418 			ptr[ 0 ] = '/';
419 			ptr++;
420 		}
421 		ptr = lutil_strncopy( ptr, fname, len );
422 		if ( ptr[ -1 ] != '/' ) {
423 			ptr[ 0 ] = '/';
424 			ptr++;
425 		}
426 		ptr[ 0 ] = '\0';
427 
428 		attr_normalize_one( ad_olmDbDirectory, &bv, &nbv, NULL );
429 
430 		next->a_desc = ad_olmDbDirectory;
431 		next->a_vals = ch_calloc( sizeof( struct berval ), 2 );
432 		next->a_vals[ 0 ] = bv;
433 		next->a_numvals = 1;
434 
435 		if ( BER_BVISNULL( &nbv ) ) {
436 			next->a_nvals = next->a_vals;
437 
438 		} else {
439 			next->a_nvals = ch_calloc( sizeof( struct berval ), 2 );
440 			next->a_nvals[ 0 ] = nbv;
441 		}
442 
443 		next = next->a_next;
444 	}
445 
446 	cb = ch_calloc( sizeof( monitor_callback_t ), 1 );
447 	cb->mc_update = bdb_monitor_update;
448 #if 0	/* uncomment if required */
449 	cb->mc_modify = bdb_monitor_modify;
450 #endif
451 	cb->mc_free = bdb_monitor_free;
452 	cb->mc_private = (void *)bdb;
453 
454 	/* make sure the database is registered; then add monitor attributes */
455 	rc = mbe->register_database( be, &bdb->bi_monitor.bdm_ndn );
456 	if ( rc == 0 ) {
457 		rc = mbe->register_entry_attrs( &bdb->bi_monitor.bdm_ndn, a, cb,
458 			NULL, 0, NULL );
459 	}
460 
461 cleanup:;
462 	if ( rc != 0 ) {
463 		if ( cb != NULL ) {
464 			ch_free( cb );
465 			cb = NULL;
466 		}
467 
468 		if ( a != NULL ) {
469 			attrs_free( a );
470 			a = NULL;
471 		}
472 	}
473 
474 	/* store for cleanup */
475 	bdb->bi_monitor.bdm_cb = (void *)cb;
476 
477 	/* we don't need to keep track of the attributes, because
478 	 * bdb_monitor_free() takes care of everything */
479 	if ( a != NULL ) {
480 		attrs_free( a );
481 	}
482 
483 	return rc;
484 }
485 
486 /*
487  * call from within bdb_db_close()
488  */
489 int
bdb_monitor_db_close(BackendDB * be)490 bdb_monitor_db_close( BackendDB *be )
491 {
492 	struct bdb_info		*bdb = (struct bdb_info *) be->be_private;
493 
494 	if ( !BER_BVISNULL( &bdb->bi_monitor.bdm_ndn ) ) {
495 		BackendInfo		*mi = backend_info( "monitor" );
496 		monitor_extra_t		*mbe;
497 
498 		if ( mi && &mi->bi_extra ) {
499 			mbe = mi->bi_extra;
500 			mbe->unregister_entry_callback( &bdb->bi_monitor.bdm_ndn,
501 				(monitor_callback_t *)bdb->bi_monitor.bdm_cb,
502 				NULL, 0, NULL );
503 		}
504 
505 		memset( &bdb->bi_monitor, 0, sizeof( bdb->bi_monitor ) );
506 	}
507 
508 	return 0;
509 }
510 
511 /*
512  * call from within bdb_db_destroy()
513  */
514 int
bdb_monitor_db_destroy(BackendDB * be)515 bdb_monitor_db_destroy( BackendDB *be )
516 {
517 #ifdef BDB_MONITOR_IDX
518 	struct bdb_info		*bdb = (struct bdb_info *) be->be_private;
519 
520 	/* TODO: free tree */
521 	ldap_pvt_thread_mutex_destroy( &bdb->bi_idx_mutex );
522 	avl_free( bdb->bi_idx, ch_free );
523 #endif /* BDB_MONITOR_IDX */
524 
525 	return 0;
526 }
527 
528 #ifdef BDB_MONITOR_IDX
529 
530 #define BDB_MONITOR_IDX_TYPES	(4)
531 
532 typedef struct monitor_idx_t monitor_idx_t;
533 
534 struct monitor_idx_t {
535 	AttributeDescription	*idx_ad;
536 	unsigned long		idx_count[BDB_MONITOR_IDX_TYPES];
537 };
538 
539 static int
bdb_monitor_bitmask2key(slap_mask_t bitmask)540 bdb_monitor_bitmask2key( slap_mask_t bitmask )
541 {
542 	int	key;
543 
544 	for ( key = 0; key < 8 * (int)sizeof(slap_mask_t) && !( bitmask & 0x1U );
545 			key++ )
546 		bitmask >>= 1;
547 
548 	return key;
549 }
550 
551 static struct berval idxbv[] = {
552 	BER_BVC( "present=" ),
553 	BER_BVC( "equality=" ),
554 	BER_BVC( "approx=" ),
555 	BER_BVC( "substr=" ),
556 	BER_BVNULL
557 };
558 
559 static ber_len_t
bdb_monitor_idx2len(monitor_idx_t * idx)560 bdb_monitor_idx2len( monitor_idx_t *idx )
561 {
562 	int		i;
563 	ber_len_t	len = 0;
564 
565 	for ( i = 0; i < BDB_MONITOR_IDX_TYPES; i++ ) {
566 		if ( idx->idx_count[ i ] != 0 ) {
567 			len += idxbv[i].bv_len;
568 		}
569 	}
570 
571 	return len;
572 }
573 
574 static int
monitor_idx_cmp(const void * p1,const void * p2)575 monitor_idx_cmp( const void *p1, const void *p2 )
576 {
577 	const monitor_idx_t	*idx1 = (const monitor_idx_t *)p1;
578 	const monitor_idx_t	*idx2 = (const monitor_idx_t *)p2;
579 
580 	return SLAP_PTRCMP( idx1->idx_ad, idx2->idx_ad );
581 }
582 
583 static int
monitor_idx_dup(void * p1,void * p2)584 monitor_idx_dup( void *p1, void *p2 )
585 {
586 	monitor_idx_t	*idx1 = (monitor_idx_t *)p1;
587 	monitor_idx_t	*idx2 = (monitor_idx_t *)p2;
588 
589 	return SLAP_PTRCMP( idx1->idx_ad, idx2->idx_ad ) == 0 ? -1 : 0;
590 }
591 
592 int
bdb_monitor_idx_add(struct bdb_info * bdb,AttributeDescription * desc,slap_mask_t type)593 bdb_monitor_idx_add(
594 	struct bdb_info		*bdb,
595 	AttributeDescription	*desc,
596 	slap_mask_t		type )
597 {
598 	monitor_idx_t		idx_dummy = { 0 },
599 				*idx;
600 	int			rc = 0, key;
601 
602 	idx_dummy.idx_ad = desc;
603 	key = bdb_monitor_bitmask2key( type ) - 1;
604 	if ( key >= BDB_MONITOR_IDX_TYPES ) {
605 		/* invalid index type */
606 		return -1;
607 	}
608 
609 	ldap_pvt_thread_mutex_lock( &bdb->bi_idx_mutex );
610 
611 	idx = (monitor_idx_t *)avl_find( bdb->bi_idx,
612 		(caddr_t)&idx_dummy, monitor_idx_cmp );
613 	if ( idx == NULL ) {
614 		idx = (monitor_idx_t *)ch_calloc( sizeof( monitor_idx_t ), 1 );
615 		idx->idx_ad = desc;
616 		idx->idx_count[ key ] = 1;
617 
618 		switch ( avl_insert( &bdb->bi_idx, (caddr_t)idx,
619 			monitor_idx_cmp, monitor_idx_dup ) )
620 		{
621 		case 0:
622 			break;
623 
624 		default:
625 			ch_free( idx );
626 			rc = -1;
627 		}
628 
629 	} else {
630 		idx->idx_count[ key ]++;
631 	}
632 
633 	ldap_pvt_thread_mutex_unlock( &bdb->bi_idx_mutex );
634 
635 	return rc;
636 }
637 
638 static int
bdb_monitor_idx_apply(void * v_idx,void * v_valp)639 bdb_monitor_idx_apply( void *v_idx, void *v_valp )
640 {
641 	monitor_idx_t	*idx = (monitor_idx_t *)v_idx;
642 	BerVarray	*valp = (BerVarray *)v_valp;
643 
644 	struct berval	bv;
645 	char		*ptr;
646 	char		count_buf[ BDB_MONITOR_IDX_TYPES ][ SLAP_TEXT_BUFLEN ];
647 	ber_len_t	count_len[ BDB_MONITOR_IDX_TYPES ],
648 			idx_len;
649 	int		i, num = 0;
650 
651 	idx_len = bdb_monitor_idx2len( idx );
652 
653 	bv.bv_len = 0;
654 	for ( i = 0; i < BDB_MONITOR_IDX_TYPES; i++ ) {
655 		if ( idx->idx_count[ i ] == 0 ) {
656 			continue;
657 		}
658 
659 		count_len[ i ] = snprintf( count_buf[ i ],
660 			sizeof( count_buf[ i ] ), "%lu", idx->idx_count[ i ] );
661 		bv.bv_len += count_len[ i ];
662 		num++;
663 	}
664 
665 	bv.bv_len += idx->idx_ad->ad_cname.bv_len
666 		+ num
667 		+ idx_len;
668 	ptr = bv.bv_val = ch_malloc( bv.bv_len + 1 );
669 	ptr = lutil_strcopy( ptr, idx->idx_ad->ad_cname.bv_val );
670 	for ( i = 0; i < BDB_MONITOR_IDX_TYPES; i++ ) {
671 		if ( idx->idx_count[ i ] == 0 ) {
672 			continue;
673 		}
674 
675 		ptr[ 0 ] = '#';
676 		++ptr;
677 		ptr = lutil_strcopy( ptr, idxbv[ i ].bv_val );
678 		ptr = lutil_strcopy( ptr, count_buf[ i ] );
679 	}
680 
681 	ber_bvarray_add( valp, &bv );
682 
683 	return 0;
684 }
685 
686 static int
bdb_monitor_idx_entry_add(struct bdb_info * bdb,Entry * e)687 bdb_monitor_idx_entry_add(
688 	struct bdb_info	*bdb,
689 	Entry		*e )
690 {
691 	BerVarray	vals = NULL;
692 	Attribute	*a;
693 
694 	a = attr_find( e->e_attrs, ad_olmDbNotIndexed );
695 
696 	ldap_pvt_thread_mutex_lock( &bdb->bi_idx_mutex );
697 
698 	avl_apply( bdb->bi_idx, bdb_monitor_idx_apply,
699 		&vals, -1, AVL_INORDER );
700 
701 	ldap_pvt_thread_mutex_unlock( &bdb->bi_idx_mutex );
702 
703 	if ( vals != NULL ) {
704 		if ( a != NULL ) {
705 			assert( a->a_nvals == a->a_vals );
706 
707 			ber_bvarray_free( a->a_vals );
708 
709 		} else {
710 			Attribute	**ap;
711 
712 			for ( ap = &e->e_attrs; *ap != NULL; ap = &(*ap)->a_next )
713 				;
714 			*ap = attr_alloc( ad_olmDbNotIndexed );
715 			a = *ap;
716 		}
717 		a->a_vals = vals;
718 		a->a_nvals = a->a_vals;
719 	}
720 
721 	return 0;
722 }
723 
724 #endif /* BDB_MONITOR_IDX */
725