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