Home | History | Annotate | Line # | Download | only in back-monitor
init.c revision 1.1.1.1
      1 /* init.c - initialize monitor backend */
      2 /* $OpenLDAP: pkg/ldap/servers/slapd/back-monitor/init.c,v 1.125.2.6 2008/04/24 08:13:39 hyc Exp $ */
      3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
      4  *
      5  * Copyright 2001-2008 The OpenLDAP Foundation.
      6  * Portions Copyright 2001-2003 Pierangelo Masarati.
      7  * All rights reserved.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted only as authorized by the OpenLDAP
     11  * Public License.
     12  *
     13  * A copy of this license is available in file LICENSE in the
     14  * top-level directory of the distribution or, alternatively, at
     15  * <http://www.OpenLDAP.org/license.html>.
     16  */
     17 /* ACKNOWLEDGEMENTS:
     18  * This work was initially developed by Pierangelo Masarati for inclusion
     19  * in OpenLDAP Software.
     20  */
     21 
     22 #include "portable.h"
     23 
     24 #include <stdio.h>
     25 #include <ac/string.h>
     26 
     27 #include <lutil.h>
     28 #include "slap.h"
     29 #include "config.h"
     30 #include "lber_pvt.h"
     31 #include "back-monitor.h"
     32 
     33 #include "config.h"
     34 
     35 #undef INTEGRATE_CORE_SCHEMA
     36 
     37 /*
     38  * used by many functions to add description to entries
     39  *
     40  * WARNING: be_monitor may change as new databases are added,
     41  * so it should not be used outside monitor_back_db_init()
     42  * until monitor_back_db_open is called.
     43  */
     44 BackendDB			*be_monitor;
     45 
     46 static struct monitor_subsys_t	**monitor_subsys;
     47 static int			monitor_subsys_opened;
     48 static monitor_info_t		monitor_info;
     49 static const monitor_extra_t monitor_extra = {
     50 	monitor_back_is_configured,
     51 	monitor_back_get_subsys,
     52 	monitor_back_get_subsys_by_dn,
     53 
     54 	monitor_back_register_subsys,
     55 	monitor_back_register_backend,
     56 	monitor_back_register_database,
     57 	monitor_back_register_overlay_info,
     58 	monitor_back_register_overlay,
     59 	monitor_back_register_entry,
     60 	monitor_back_register_entry_parent,
     61 	monitor_back_register_entry_attrs,
     62 	monitor_back_register_entry_callback,
     63 
     64 	monitor_back_unregister_entry,
     65 	monitor_back_unregister_entry_parent,
     66 	monitor_back_unregister_entry_attrs,
     67 	monitor_back_unregister_entry_callback
     68 };
     69 
     70 
     71 /*
     72  * subsystem data
     73  *
     74  * the known subsystems are added to the subsystems
     75  * array at backend initialization; other subsystems
     76  * may be added by calling monitor_back_register_subsys()
     77  * before the database is opened (e.g. by other backends
     78  * or by overlays or modules).
     79  */
     80 static struct monitor_subsys_t known_monitor_subsys[] = {
     81 	{
     82 		SLAPD_MONITOR_BACKEND_NAME,
     83 		BER_BVNULL, BER_BVNULL, BER_BVNULL,
     84 		{ BER_BVC( "This subsystem contains information about available backends." ),
     85 			BER_BVNULL },
     86 		MONITOR_F_PERSISTENT_CH,
     87 		monitor_subsys_backend_init,
     88 		NULL,	/* destroy */
     89 		NULL,   /* update */
     90 		NULL,   /* create */
     91 		NULL	/* modify */
     92        	}, {
     93 		SLAPD_MONITOR_CONN_NAME,
     94 		BER_BVNULL, BER_BVNULL, BER_BVNULL,
     95 		{ BER_BVC( "This subsystem contains information about connections." ),
     96 			BER_BVNULL },
     97 		MONITOR_F_VOLATILE_CH,
     98 		monitor_subsys_conn_init,
     99 		NULL,	/* destroy */
    100 		NULL,   /* update */
    101 		NULL,   /* create */
    102 		NULL	/* modify */
    103        	}, {
    104 		SLAPD_MONITOR_DATABASE_NAME,
    105 		BER_BVNULL, BER_BVNULL, BER_BVNULL,
    106 		{ BER_BVC( "This subsystem contains information about configured databases." ),
    107 			BER_BVNULL },
    108 		MONITOR_F_PERSISTENT_CH,
    109 		monitor_subsys_database_init,
    110 		NULL,	/* destroy */
    111 		NULL,   /* update */
    112 		NULL,   /* create */
    113 		NULL	/* modify */
    114        	}, {
    115 		SLAPD_MONITOR_LISTENER_NAME,
    116 		BER_BVNULL, BER_BVNULL, BER_BVNULL,
    117 		{ BER_BVC( "This subsystem contains information about active listeners." ),
    118 			BER_BVNULL },
    119 		MONITOR_F_PERSISTENT_CH,
    120 		monitor_subsys_listener_init,
    121 		NULL,	/* destroy */
    122 		NULL,	/* update */
    123 		NULL,	/* create */
    124 		NULL	/* modify */
    125        	}, {
    126 		SLAPD_MONITOR_LOG_NAME,
    127 		BER_BVNULL, BER_BVNULL, BER_BVNULL,
    128 		{ BER_BVC( "This subsystem contains information about logging." ),
    129 		  	BER_BVC( "Set the attribute \"managedInfo\" to the desired log levels." ),
    130 			BER_BVNULL },
    131 		MONITOR_F_NONE,
    132 		monitor_subsys_log_init,
    133 		NULL,	/* destroy */
    134 		NULL,	/* update */
    135 		NULL,   /* create */
    136 		NULL,	/* modify */
    137        	}, {
    138 		SLAPD_MONITOR_OPS_NAME,
    139 		BER_BVNULL, BER_BVNULL, BER_BVNULL,
    140 		{ BER_BVC( "This subsystem contains information about performed operations." ),
    141 			BER_BVNULL },
    142 		MONITOR_F_PERSISTENT_CH,
    143 		monitor_subsys_ops_init,
    144 		NULL,	/* destroy */
    145 		NULL,	/* update */
    146 		NULL,   /* create */
    147 		NULL,	/* modify */
    148        	}, {
    149 		SLAPD_MONITOR_OVERLAY_NAME,
    150 		BER_BVNULL, BER_BVNULL, BER_BVNULL,
    151 		{ BER_BVC( "This subsystem contains information about available overlays." ),
    152 			BER_BVNULL },
    153 		MONITOR_F_PERSISTENT_CH,
    154 		monitor_subsys_overlay_init,
    155 		NULL,	/* destroy */
    156 		NULL,	/* update */
    157 		NULL,   /* create */
    158 		NULL,	/* modify */
    159 	}, {
    160 		SLAPD_MONITOR_SASL_NAME,
    161 		BER_BVNULL, BER_BVNULL, BER_BVNULL,
    162 		{ BER_BVC( "This subsystem contains information about SASL." ),
    163 			BER_BVNULL },
    164 		MONITOR_F_NONE,
    165 		NULL,   /* init */
    166 		NULL,	/* destroy */
    167 		NULL,   /* update */
    168 		NULL,   /* create */
    169 		NULL	/* modify */
    170        	}, {
    171 		SLAPD_MONITOR_SENT_NAME,
    172 		BER_BVNULL, BER_BVNULL, BER_BVNULL,
    173 		{ BER_BVC( "This subsystem contains statistics." ),
    174 			BER_BVNULL },
    175 		MONITOR_F_PERSISTENT_CH,
    176 		monitor_subsys_sent_init,
    177 		NULL,	/* destroy */
    178 		NULL,   /* update */
    179 		NULL,   /* create */
    180 		NULL,	/* modify */
    181        	}, {
    182 		SLAPD_MONITOR_THREAD_NAME,
    183 		BER_BVNULL, BER_BVNULL, BER_BVNULL,
    184 		{ BER_BVC( "This subsystem contains information about threads." ),
    185 			BER_BVNULL },
    186 		MONITOR_F_PERSISTENT_CH,
    187 		monitor_subsys_thread_init,
    188 		NULL,	/* destroy */
    189 		NULL,   /* update */
    190 		NULL,   /* create */
    191 		NULL	/* modify */
    192        	}, {
    193 		SLAPD_MONITOR_TIME_NAME,
    194 		BER_BVNULL, BER_BVNULL, BER_BVNULL,
    195 		{ BER_BVC( "This subsystem contains information about time." ),
    196 			BER_BVNULL },
    197 		MONITOR_F_PERSISTENT_CH,
    198 		monitor_subsys_time_init,
    199 		NULL,	/* destroy */
    200 		NULL,   /* update */
    201 		NULL,   /* create */
    202 		NULL,	/* modify */
    203        	}, {
    204 		SLAPD_MONITOR_TLS_NAME,
    205 		BER_BVNULL, BER_BVNULL, BER_BVNULL,
    206 		{ BER_BVC( "This subsystem contains information about TLS." ),
    207 			BER_BVNULL },
    208 		MONITOR_F_NONE,
    209 		NULL,   /* init */
    210 		NULL,	/* destroy */
    211 		NULL,   /* update */
    212 		NULL,   /* create */
    213 		NULL	/* modify */
    214        	}, {
    215 		SLAPD_MONITOR_RWW_NAME,
    216 		BER_BVNULL, BER_BVNULL, BER_BVNULL,
    217 		{ BER_BVC( "This subsystem contains information about read/write waiters." ),
    218 			BER_BVNULL },
    219 		MONITOR_F_PERSISTENT_CH,
    220 		monitor_subsys_rww_init,
    221 		NULL,	/* destroy */
    222 		NULL,   /* update */
    223 		NULL, 	/* create */
    224 		NULL	/* modify */
    225        	}, { NULL }
    226 };
    227 
    228 int
    229 monitor_subsys_is_opened( void )
    230 {
    231 	return monitor_subsys_opened;
    232 }
    233 
    234 int
    235 monitor_back_register_subsys(
    236 	monitor_subsys_t	*ms )
    237 {
    238 	int	i = 0;
    239 
    240 	if ( monitor_subsys ) {
    241 		for ( ; monitor_subsys[ i ] != NULL; i++ )
    242 			/* just count'em */ ;
    243 	}
    244 
    245 	monitor_subsys = ch_realloc( monitor_subsys,
    246 			( 2 + i ) * sizeof( monitor_subsys_t * ) );
    247 
    248 	if ( monitor_subsys == NULL ) {
    249 		return -1;
    250 	}
    251 
    252 	monitor_subsys[ i ] = ms;
    253 	monitor_subsys[ i + 1 ] = NULL;
    254 
    255 	/* if a subsystem is registered __AFTER__ subsystem
    256 	 * initialization (depending on the sequence the databases
    257 	 * are listed in slapd.conf), init it */
    258 	if ( monitor_subsys_is_opened() ) {
    259 
    260 		/* FIXME: this should only be possible
    261 		 * if be_monitor is already initialized */
    262 		assert( be_monitor != NULL );
    263 
    264 		if ( ms->mss_open && ( *ms->mss_open )( be_monitor, ms ) ) {
    265 			return -1;
    266 		}
    267 
    268 		ms->mss_flags |= MONITOR_F_OPENED;
    269 	}
    270 
    271 	return 0;
    272 }
    273 
    274 enum {
    275 	LIMBO_ENTRY,
    276 	LIMBO_ENTRY_PARENT,
    277 	LIMBO_ATTRS,
    278 	LIMBO_CB,
    279 	LIMBO_BACKEND,
    280 	LIMBO_DATABASE,
    281 	LIMBO_OVERLAY_INFO,
    282 	LIMBO_OVERLAY,
    283 
    284 	LIMBO_LAST
    285 };
    286 
    287 typedef struct entry_limbo_t {
    288 	int			el_type;
    289 	BackendInfo		*el_bi;
    290 	BackendDB		*el_be;
    291 	slap_overinst		*el_on;
    292 	Entry			*el_e;
    293 	Attribute		*el_a;
    294 	struct berval		*el_ndn;
    295 	struct berval		el_nbase;
    296 	int			el_scope;
    297 	struct berval		el_filter;
    298 	monitor_callback_t	*el_cb;
    299 	monitor_subsys_t	*el_mss;
    300 	unsigned long		el_flags;
    301 	struct entry_limbo_t	*el_next;
    302 } entry_limbo_t;
    303 
    304 int
    305 monitor_back_is_configured( void )
    306 {
    307 	return be_monitor != NULL;
    308 }
    309 
    310 int
    311 monitor_back_register_backend(
    312 	BackendInfo		*bi )
    313 {
    314 	return -1;
    315 }
    316 
    317 int
    318 monitor_back_register_overlay_info(
    319 	slap_overinst		*on )
    320 {
    321 	return -1;
    322 }
    323 
    324 int
    325 monitor_back_register_overlay(
    326 	BackendDB		*be )
    327 {
    328 	return -1;
    329 }
    330 
    331 int
    332 monitor_back_register_backend_limbo(
    333 	BackendInfo		*bi )
    334 {
    335 	return -1;
    336 }
    337 
    338 int
    339 monitor_back_register_database_limbo(
    340 	BackendDB		*be,
    341 	struct berval	*ndn )
    342 {
    343 	entry_limbo_t	**elpp, el = { 0 };
    344 	monitor_info_t 	*mi;
    345 
    346 	if ( be_monitor == NULL ) {
    347 		Debug( LDAP_DEBUG_ANY,
    348 			"monitor_back_register_database_limbo: "
    349 			"monitor database not configured.\n",
    350 			0, 0, 0 );
    351 		return -1;
    352 	}
    353 
    354 	mi = ( monitor_info_t * )be_monitor->be_private;
    355 
    356 
    357 	el.el_type = LIMBO_DATABASE;
    358 
    359 	el.el_be = be->bd_self;
    360 	el.el_ndn = ndn;
    361 
    362 	for ( elpp = &mi->mi_entry_limbo;
    363 			*elpp;
    364 			elpp = &(*elpp)->el_next )
    365 		/* go to last */;
    366 
    367 	*elpp = (entry_limbo_t *)ch_malloc( sizeof( entry_limbo_t ) );
    368 
    369 	el.el_next = NULL;
    370 	**elpp = el;
    371 
    372 	return 0;
    373 }
    374 
    375 int
    376 monitor_back_register_overlay_info_limbo(
    377 	slap_overinst		*on )
    378 {
    379 	return -1;
    380 }
    381 
    382 int
    383 monitor_back_register_overlay_limbo(
    384 	BackendDB		*be )
    385 {
    386 	return -1;
    387 }
    388 
    389 int
    390 monitor_back_register_entry(
    391 	Entry			*e,
    392 	monitor_callback_t	*cb,
    393 	monitor_subsys_t	*mss,
    394 	unsigned long		flags )
    395 {
    396 	monitor_info_t 	*mi;
    397 
    398 	if ( be_monitor == NULL ) {
    399 		Debug( LDAP_DEBUG_ANY,
    400 			"monitor_back_register_entry(\"%s\"): "
    401 			"monitor database not configured.\n",
    402 			e->e_name.bv_val, 0, 0 );
    403 		return -1;
    404 	}
    405 
    406 	mi = ( monitor_info_t * )be_monitor->be_private;
    407 
    408 	assert( mi != NULL );
    409 	assert( e != NULL );
    410 	assert( e->e_private == NULL );
    411 
    412 	if ( monitor_subsys_is_opened() ) {
    413 		Entry		*e_parent = NULL,
    414 				*e_new = NULL,
    415 				**ep = NULL;
    416 		struct berval	pdn = BER_BVNULL;
    417 		monitor_entry_t *mp = NULL,
    418 				*mp_parent = NULL;
    419 		int		rc = 0;
    420 
    421 		if ( monitor_cache_get( mi, &e->e_nname, &e_parent ) == 0 ) {
    422 			/* entry exists */
    423 			Debug( LDAP_DEBUG_ANY,
    424 				"monitor_back_register_entry(\"%s\"): "
    425 				"entry exists\n",
    426 				e->e_name.bv_val, 0, 0 );
    427 			monitor_cache_release( mi, e_parent );
    428 			return -1;
    429 		}
    430 
    431 		dnParent( &e->e_nname, &pdn );
    432 		if ( monitor_cache_get( mi, &pdn, &e_parent ) != 0 ) {
    433 			/* parent does not exist */
    434 			Debug( LDAP_DEBUG_ANY,
    435 				"monitor_back_register_entry(\"%s\"): "
    436 				"parent \"%s\" not found\n",
    437 				e->e_name.bv_val, pdn.bv_val, 0 );
    438 			return -1;
    439 		}
    440 
    441 		assert( e_parent->e_private != NULL );
    442 		mp_parent = ( monitor_entry_t * )e_parent->e_private;
    443 
    444 		if ( mp_parent->mp_flags & MONITOR_F_VOLATILE ) {
    445 			/* entry is volatile; cannot append children */
    446 			Debug( LDAP_DEBUG_ANY,
    447 				"monitor_back_register_entry(\"%s\"): "
    448 				"parent \"%s\" is volatile\n",
    449 				e->e_name.bv_val, e_parent->e_name.bv_val, 0 );
    450 			rc = -1;
    451 			goto done;
    452 		}
    453 
    454 		mp = monitor_entrypriv_create();
    455 		if ( mp == NULL ) {
    456 			Debug( LDAP_DEBUG_ANY,
    457 				"monitor_back_register_entry(\"%s\"): "
    458 				"monitor_entrypriv_create() failed\n",
    459 				e->e_name.bv_val, 0, 0 );
    460 			rc = -1;
    461 			goto done;
    462 		}
    463 
    464 		e_new = entry_dup( e );
    465 		if ( e_new == NULL ) {
    466 			Debug( LDAP_DEBUG_ANY,
    467 				"monitor_back_register_entry(\"%s\"): "
    468 				"entry_dup() failed\n",
    469 				e->e_name.bv_val, 0, 0 );
    470 			rc = -1;
    471 			goto done;
    472 		}
    473 
    474 		e_new->e_private = ( void * )mp;
    475 		if ( mss != NULL ) {
    476 			mp->mp_info = mss;
    477 			mp->mp_flags = flags;
    478 
    479 		} else {
    480 			mp->mp_info = mp_parent->mp_info;
    481 			mp->mp_flags = mp_parent->mp_flags | MONITOR_F_SUB;
    482 		}
    483 		mp->mp_cb = cb;
    484 
    485 		ep = &mp_parent->mp_children;
    486 		for ( ; *ep; ) {
    487 			mp_parent = ( monitor_entry_t * )(*ep)->e_private;
    488 			ep = &mp_parent->mp_next;
    489 		}
    490 		*ep = e_new;
    491 
    492 		if ( monitor_cache_add( mi, e_new ) ) {
    493 			Debug( LDAP_DEBUG_ANY,
    494 				"monitor_back_register_entry(\"%s\"): "
    495 				"unable to add entry\n",
    496 				e->e_name.bv_val, 0, 0 );
    497 			rc = -1;
    498 			goto done;
    499 		}
    500 
    501 done:;
    502 		if ( rc ) {
    503 			if ( mp ) {
    504 				ch_free( mp );
    505 			}
    506 			if ( e_new ) {
    507 				e_new->e_private = NULL;
    508 				entry_free( e_new );
    509 			}
    510 		}
    511 
    512 		if ( e_parent ) {
    513 			monitor_cache_release( mi, e_parent );
    514 		}
    515 
    516 	} else {
    517 		entry_limbo_t	**elpp, el = { 0 };
    518 
    519 		el.el_type = LIMBO_ENTRY;
    520 
    521 		el.el_e = entry_dup( e );
    522 		if ( el.el_e == NULL ) {
    523 			Debug( LDAP_DEBUG_ANY,
    524 				"monitor_back_register_entry(\"%s\"): "
    525 				"entry_dup() failed\n",
    526 				e->e_name.bv_val, 0, 0 );
    527 			return -1;
    528 		}
    529 
    530 		el.el_cb = cb;
    531 		el.el_mss = mss;
    532 		el.el_flags = flags;
    533 
    534 		for ( elpp = &mi->mi_entry_limbo;
    535 				*elpp;
    536 				elpp = &(*elpp)->el_next )
    537 			/* go to last */;
    538 
    539 		*elpp = (entry_limbo_t *)ch_malloc( sizeof( entry_limbo_t ) );
    540 		if ( *elpp == NULL ) {
    541 			el.el_e->e_private = NULL;
    542 			entry_free( el.el_e );
    543 			return -1;
    544 		}
    545 
    546 		el.el_next = NULL;
    547 		**elpp = el;
    548 	}
    549 
    550 	return 0;
    551 }
    552 
    553 int
    554 monitor_back_register_entry_parent(
    555 	Entry			*e,
    556 	monitor_callback_t	*cb,
    557 	monitor_subsys_t	*mss,
    558 	unsigned long		flags,
    559 	struct berval		*nbase,
    560 	int			scope,
    561 	struct berval		*filter )
    562 {
    563 	monitor_info_t 	*mi;
    564 	struct berval	ndn = BER_BVNULL;
    565 
    566 	if ( be_monitor == NULL ) {
    567 		Debug( LDAP_DEBUG_ANY,
    568 			"monitor_back_register_entry_parent(base=\"%s\" scope=%s filter=\"%s\"): "
    569 			"monitor database not configured.\n",
    570 			BER_BVISNULL( nbase ) ? "" : nbase->bv_val,
    571 			ldap_pvt_scope2str( scope ),
    572 			BER_BVISNULL( filter ) ? "" : filter->bv_val );
    573 		return -1;
    574 	}
    575 
    576 	mi = ( monitor_info_t * )be_monitor->be_private;
    577 
    578 	assert( mi != NULL );
    579 	assert( e != NULL );
    580 	assert( e->e_private == NULL );
    581 
    582 	if ( BER_BVISNULL( filter ) ) {
    583 		/* need a filter */
    584 		Debug( LDAP_DEBUG_ANY,
    585 			"monitor_back_register_entry_parent(\"\"): "
    586 			"need a valid filter\n",
    587 			0, 0, 0 );
    588 		return -1;
    589 	}
    590 
    591 	if ( monitor_subsys_is_opened() ) {
    592 		Entry		*e_parent = NULL,
    593 				*e_new = NULL,
    594 				**ep = NULL;
    595 		struct berval	e_name = BER_BVNULL,
    596 				e_nname = BER_BVNULL;
    597 		monitor_entry_t *mp = NULL,
    598 				*mp_parent = NULL;
    599 		int		rc = 0;
    600 
    601 		if ( monitor_search2ndn( nbase, scope, filter, &ndn ) ) {
    602 			/* entry does not exist */
    603 			Debug( LDAP_DEBUG_ANY,
    604 				"monitor_back_register_entry_parent(\"\"): "
    605 				"base=\"%s\" scope=%s filter=\"%s\": "
    606 				"unable to find entry\n",
    607 				nbase->bv_val ? nbase->bv_val : "\"\"",
    608 				ldap_pvt_scope2str( scope ),
    609 				filter->bv_val );
    610 			return -1;
    611 		}
    612 
    613 		if ( monitor_cache_get( mi, &ndn, &e_parent ) != 0 ) {
    614 			/* entry does not exist */
    615 			Debug( LDAP_DEBUG_ANY,
    616 				"monitor_back_register_entry_parent(\"%s\"): "
    617 				"parent entry does not exist\n",
    618 				ndn.bv_val, 0, 0 );
    619 			rc = -1;
    620 			goto done;
    621 		}
    622 
    623 		assert( e_parent->e_private != NULL );
    624 		mp_parent = ( monitor_entry_t * )e_parent->e_private;
    625 
    626 		if ( mp_parent->mp_flags & MONITOR_F_VOLATILE ) {
    627 			/* entry is volatile; cannot append callback */
    628 			Debug( LDAP_DEBUG_ANY,
    629 				"monitor_back_register_entry_parent(\"%s\"): "
    630 				"entry is volatile\n",
    631 				e_parent->e_name.bv_val, 0, 0 );
    632 			rc = -1;
    633 			goto done;
    634 		}
    635 
    636 		build_new_dn( &e_name, &e_parent->e_name, &e->e_name, NULL );
    637 		build_new_dn( &e_nname, &e_parent->e_nname, &e->e_nname, NULL );
    638 
    639 		if ( monitor_cache_get( mi, &e_nname, &e_new ) == 0 ) {
    640 			/* entry already exists */
    641 			Debug( LDAP_DEBUG_ANY,
    642 				"monitor_back_register_entry_parent(\"%s\"): "
    643 				"entry already exists\n",
    644 				e_name.bv_val, 0, 0 );
    645 			monitor_cache_release( mi, e_new );
    646 			e_new = NULL;
    647 			rc = -1;
    648 			goto done;
    649 		}
    650 
    651 		mp = monitor_entrypriv_create();
    652 		if ( mp == NULL ) {
    653 			Debug( LDAP_DEBUG_ANY,
    654 				"monitor_back_register_entry_parent(\"%s\"): "
    655 				"monitor_entrypriv_create() failed\n",
    656 				e->e_name.bv_val, 0, 0 );
    657 			rc = -1;
    658 			goto done;
    659 		}
    660 
    661 		e_new = entry_dup( e );
    662 		if ( e_new == NULL ) {
    663 			Debug( LDAP_DEBUG_ANY,
    664 				"monitor_back_register_entry(\"%s\"): "
    665 				"entry_dup() failed\n",
    666 				e->e_name.bv_val, 0, 0 );
    667 			rc = -1;
    668 			goto done;
    669 		}
    670 		ch_free( e_new->e_name.bv_val );
    671 		ch_free( e_new->e_nname.bv_val );
    672 		e_new->e_name = e_name;
    673 		e_new->e_nname = e_nname;
    674 
    675 		e_new->e_private = ( void * )mp;
    676 		if ( mss != NULL ) {
    677 			mp->mp_info = mss;
    678 			mp->mp_flags = flags;
    679 
    680 		} else {
    681 			mp->mp_info = mp_parent->mp_info;
    682 			mp->mp_flags = mp_parent->mp_flags | MONITOR_F_SUB;
    683 		}
    684 		mp->mp_cb = cb;
    685 
    686 		ep = &mp_parent->mp_children;
    687 		for ( ; *ep; ) {
    688 			mp_parent = ( monitor_entry_t * )(*ep)->e_private;
    689 			ep = &mp_parent->mp_next;
    690 		}
    691 		*ep = e_new;
    692 
    693 		if ( monitor_cache_add( mi, e_new ) ) {
    694 			Debug( LDAP_DEBUG_ANY,
    695 				"monitor_back_register_entry(\"%s\"): "
    696 				"unable to add entry\n",
    697 				e->e_name.bv_val, 0, 0 );
    698 			rc = -1;
    699 			goto done;
    700 		}
    701 
    702 done:;
    703 		if ( !BER_BVISNULL( &ndn ) ) {
    704 			ch_free( ndn.bv_val );
    705 		}
    706 
    707 		if ( rc ) {
    708 			if ( mp ) {
    709 				ch_free( mp );
    710 			}
    711 			if ( e_new ) {
    712 				e_new->e_private = NULL;
    713 				entry_free( e_new );
    714 			}
    715 		}
    716 
    717 		if ( e_parent ) {
    718 			monitor_cache_release( mi, e_parent );
    719 		}
    720 
    721 	} else {
    722 		entry_limbo_t	**elpp = NULL, el = { 0 };
    723 
    724 		el.el_type = LIMBO_ENTRY_PARENT;
    725 
    726 		el.el_e = entry_dup( e );
    727 		if ( el.el_e == NULL ) {
    728 			Debug( LDAP_DEBUG_ANY,
    729 				"monitor_back_register_entry(\"%s\"): "
    730 				"entry_dup() failed\n",
    731 				e->e_name.bv_val, 0, 0 );
    732 			goto done_limbo;
    733 		}
    734 
    735 		if ( !BER_BVISNULL( nbase ) ) {
    736 			ber_dupbv( &el.el_nbase, nbase );
    737 		}
    738 
    739 		el.el_scope = scope;
    740 		if ( !BER_BVISNULL( filter ) ) {
    741 			ber_dupbv( &el.el_filter, filter  );
    742 		}
    743 
    744 		el.el_cb = cb;
    745 		el.el_mss = mss;
    746 		el.el_flags = flags;
    747 
    748 		for ( elpp = &mi->mi_entry_limbo;
    749 				*elpp;
    750 				elpp = &(*elpp)->el_next )
    751 			/* go to last */;
    752 
    753 		*elpp = (entry_limbo_t *)ch_malloc( sizeof( entry_limbo_t ) );
    754 		if ( *elpp == NULL ) {
    755 			goto done_limbo;
    756 		}
    757 
    758 done_limbo:;
    759 		if ( *elpp != NULL ) {
    760 			el.el_next = NULL;
    761 			**elpp = el;
    762 
    763 		} else {
    764 			if ( !BER_BVISNULL( &el.el_filter ) ) {
    765 				ch_free( el.el_filter.bv_val );
    766 			}
    767 			if ( !BER_BVISNULL( &el.el_nbase ) ) {
    768 				ch_free( el.el_nbase.bv_val );
    769 			}
    770 			entry_free( el.el_e );
    771 			return -1;
    772 		}
    773 	}
    774 
    775 	return 0;
    776 }
    777 
    778 static int
    779 monitor_search2ndn_cb( Operation *op, SlapReply *rs )
    780 {
    781 	if ( rs->sr_type == REP_SEARCH ) {
    782 		struct berval	*ndn = op->o_callback->sc_private;
    783 
    784 		if ( !BER_BVISNULL( ndn ) ) {
    785 			rs->sr_err = LDAP_SIZELIMIT_EXCEEDED;
    786 			ch_free( ndn->bv_val );
    787 			BER_BVZERO( ndn );
    788 			return rs->sr_err;
    789 		}
    790 
    791 		ber_dupbv( ndn, &rs->sr_entry->e_nname );
    792 	}
    793 
    794 	return 0;
    795 }
    796 
    797 int
    798 monitor_search2ndn(
    799 	struct berval	*nbase,
    800 	int		scope,
    801 	struct berval	*filter,
    802 	struct berval	*ndn )
    803 {
    804 	Connection	conn = { 0 };
    805 	OperationBuffer	opbuf;
    806 	Operation	*op;
    807 	void	*thrctx;
    808 	SlapReply	rs = { 0 };
    809 	slap_callback	cb = { NULL, monitor_search2ndn_cb, NULL, NULL };
    810 	int		rc;
    811 
    812 	BER_BVZERO( ndn );
    813 
    814 	if ( be_monitor == NULL ) {
    815 		return -1;
    816 	}
    817 
    818 	thrctx = ldap_pvt_thread_pool_context();
    819 	connection_fake_init( &conn, &opbuf, thrctx );
    820 	op = &opbuf.ob_op;
    821 
    822 	op->o_tag = LDAP_REQ_SEARCH;
    823 
    824 	/* use global malloc for now */
    825 	if ( op->o_tmpmemctx ) {
    826 		op->o_tmpmemctx = NULL;
    827 	}
    828 	op->o_tmpmfuncs = &ch_mfuncs;
    829 
    830 	op->o_bd = be_monitor;
    831 	if ( nbase == NULL || BER_BVISNULL( nbase ) ) {
    832 		ber_dupbv_x( &op->o_req_dn, &op->o_bd->be_suffix[ 0 ],
    833 				op->o_tmpmemctx );
    834 		ber_dupbv_x( &op->o_req_ndn, &op->o_bd->be_nsuffix[ 0 ],
    835 				op->o_tmpmemctx );
    836 
    837 	} else {
    838 		if ( dnPrettyNormal( NULL, nbase, &op->o_req_dn, &op->o_req_ndn,
    839 					op->o_tmpmemctx ) ) {
    840 			return -1;
    841 		}
    842 	}
    843 
    844 	op->o_callback = &cb;
    845 	cb.sc_private = (void *)ndn;
    846 
    847 	op->ors_scope = scope;
    848 	op->ors_filter = str2filter_x( op, filter->bv_val );
    849 	if ( op->ors_filter == NULL ) {
    850 		rc = LDAP_OTHER;
    851 		goto cleanup;
    852 	}
    853 	ber_dupbv_x( &op->ors_filterstr, filter, op->o_tmpmemctx );
    854 	op->ors_attrs = slap_anlist_no_attrs;
    855 	op->ors_attrsonly = 0;
    856 	op->ors_tlimit = SLAP_NO_LIMIT;
    857 	op->ors_slimit = 1;
    858 	op->ors_limit = NULL;
    859 	op->ors_deref = LDAP_DEREF_NEVER;
    860 
    861 	op->o_nocaching = 1;
    862 	op->o_managedsait = SLAP_CONTROL_NONCRITICAL;
    863 
    864 	op->o_dn = be_monitor->be_rootdn;
    865 	op->o_ndn = be_monitor->be_rootndn;
    866 
    867 	rc = op->o_bd->be_search( op, &rs );
    868 
    869 cleanup:;
    870 	if ( op->ors_filter != NULL ) {
    871 		filter_free_x( op, op->ors_filter );
    872 	}
    873 	if ( !BER_BVISNULL( &op->ors_filterstr ) ) {
    874 		op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
    875 	}
    876 	if ( !BER_BVISNULL( &op->o_req_dn ) ) {
    877 		op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
    878 	}
    879 	if ( !BER_BVISNULL( &op->o_req_ndn ) ) {
    880 		op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
    881 	}
    882 
    883 	if ( rc != 0 ) {
    884 		return rc;
    885 	}
    886 
    887 	switch ( rs.sr_err ) {
    888 	case LDAP_SUCCESS:
    889 		if ( BER_BVISNULL( ndn ) ) {
    890 			rc = -1;
    891 		}
    892 		break;
    893 
    894 	case LDAP_SIZELIMIT_EXCEEDED:
    895 	default:
    896 		if ( !BER_BVISNULL( ndn ) ) {
    897 			ber_memfree( ndn->bv_val );
    898 			BER_BVZERO( ndn );
    899 		}
    900 		rc = -1;
    901 		break;
    902 	}
    903 
    904 	return rc;
    905 }
    906 
    907 int
    908 monitor_back_register_entry_attrs(
    909 	struct berval		*ndn_in,
    910 	Attribute		*a,
    911 	monitor_callback_t	*cb,
    912 	struct berval		*nbase,
    913 	int			scope,
    914 	struct berval		*filter )
    915 {
    916 	monitor_info_t 	*mi;
    917 	struct berval	ndn = BER_BVNULL;
    918 	char		*fname = ( a == NULL ? "callback" : "attrs" );
    919 
    920 	if ( be_monitor == NULL ) {
    921 		char		buf[ SLAP_TEXT_BUFLEN ];
    922 
    923 		snprintf( buf, sizeof( buf ),
    924 			"monitor_back_register_entry_%s(base=\"%s\" scope=%s filter=\"%s\"): "
    925 			"monitor database not configured.\n",
    926 			fname,
    927 			BER_BVISNULL( nbase ) ? "" : nbase->bv_val,
    928 			ldap_pvt_scope2str( scope ),
    929 			BER_BVISNULL( filter ) ? "" : filter->bv_val );
    930 		Debug( LDAP_DEBUG_ANY, "%s\n", buf, 0, 0 );
    931 
    932 		return -1;
    933 	}
    934 
    935 	mi = ( monitor_info_t * )be_monitor->be_private;
    936 
    937 	assert( mi != NULL );
    938 
    939 	if ( ndn_in != NULL ) {
    940 		ndn = *ndn_in;
    941 	}
    942 
    943 	if ( a == NULL && cb == NULL ) {
    944 		/* nothing to do */
    945 		return -1;
    946 	}
    947 
    948 	if ( ( ndn_in == NULL || BER_BVISNULL( &ndn ) )
    949 			&& BER_BVISNULL( filter ) )
    950 	{
    951 		/* need a filter */
    952 		Debug( LDAP_DEBUG_ANY,
    953 			"monitor_back_register_entry_%s(\"\"): "
    954 			"need a valid filter\n",
    955 			fname, 0, 0 );
    956 		return -1;
    957 	}
    958 
    959 	if ( monitor_subsys_is_opened() ) {
    960 		Entry			*e = NULL;
    961 		Attribute		**atp = NULL;
    962 		monitor_entry_t 	*mp = NULL;
    963 		monitor_callback_t	**mcp = NULL;
    964 		int			rc = 0;
    965 		int			freeit = 0;
    966 
    967 		if ( BER_BVISNULL( &ndn ) ) {
    968 			if ( monitor_search2ndn( nbase, scope, filter, &ndn ) ) {
    969 				char		buf[ SLAP_TEXT_BUFLEN ];
    970 
    971 				snprintf( buf, sizeof( buf ),
    972 					"monitor_back_register_entry_%s(\"\"): "
    973 					"base=\"%s\" scope=%s filter=\"%s\": "
    974 					"unable to find entry\n",
    975 					fname,
    976 					nbase->bv_val ? nbase->bv_val : "\"\"",
    977 					ldap_pvt_scope2str( scope ),
    978 					filter->bv_val );
    979 
    980 				/* entry does not exist */
    981 				Debug( LDAP_DEBUG_ANY, "%s\n", buf, 0, 0 );
    982 				return -1;
    983 			}
    984 
    985 			freeit = 1;
    986 		}
    987 
    988 		if ( monitor_cache_get( mi, &ndn, &e ) != 0 ) {
    989 			/* entry does not exist */
    990 			Debug( LDAP_DEBUG_ANY,
    991 				"monitor_back_register_entry_%s(\"%s\"): "
    992 				"entry does not exist\n",
    993 				fname, ndn.bv_val, 0 );
    994 			rc = -1;
    995 			goto done;
    996 		}
    997 
    998 		assert( e->e_private != NULL );
    999 		mp = ( monitor_entry_t * )e->e_private;
   1000 
   1001 		if ( mp->mp_flags & MONITOR_F_VOLATILE ) {
   1002 			/* entry is volatile; cannot append callback */
   1003 			Debug( LDAP_DEBUG_ANY,
   1004 				"monitor_back_register_entry_%s(\"%s\"): "
   1005 				"entry is volatile\n",
   1006 				fname, e->e_name.bv_val, 0 );
   1007 			rc = -1;
   1008 			goto done;
   1009 		}
   1010 
   1011 		if ( a ) {
   1012 			for ( atp = &e->e_attrs; *atp; atp = &(*atp)->a_next )
   1013 				/* just get to last */ ;
   1014 
   1015 			for ( ; a != NULL; a = a->a_next ) {
   1016 				assert( a->a_desc != NULL );
   1017 				assert( a->a_vals != NULL );
   1018 
   1019 				if ( attr_find( e->e_attrs, a->a_desc ) ) {
   1020 					attr_merge( e, a->a_desc, a->a_vals,
   1021 						a->a_nvals == a->a_vals ? NULL : a->a_nvals );
   1022 
   1023 				} else {
   1024 					*atp = attr_dup( a );
   1025 					if ( *atp == NULL ) {
   1026 						Debug( LDAP_DEBUG_ANY,
   1027 							"monitor_back_register_entry_%s(\"%s\"): "
   1028 							"attr_dup() failed\n",
   1029 							fname, e->e_name.bv_val, 0 );
   1030 						rc = -1;
   1031 						goto done;
   1032 					}
   1033 					atp = &(*atp)->a_next;
   1034 				}
   1035 			}
   1036 		}
   1037 
   1038 		if ( cb ) {
   1039 			for ( mcp = &mp->mp_cb; *mcp; mcp = &(*mcp)->mc_next )
   1040 				/* go to tail */ ;
   1041 
   1042 			/* NOTE: we do not clear cb->mc_next, so this function
   1043 			 * can be used to append a list of callbacks */
   1044 			(*mcp) = cb;
   1045 		}
   1046 
   1047 done:;
   1048 		if ( rc ) {
   1049 			if ( atp && *atp ) {
   1050 				attrs_free( *atp );
   1051 				*atp = NULL;
   1052 			}
   1053 		}
   1054 
   1055 		if ( freeit ) {
   1056 			ber_memfree( ndn.bv_val );
   1057 		}
   1058 
   1059 		if ( e ) {
   1060 			monitor_cache_release( mi, e );
   1061 		}
   1062 
   1063 	} else {
   1064 		entry_limbo_t	**elpp, el = { 0 };
   1065 
   1066 		el.el_type = LIMBO_ATTRS;
   1067 		el.el_ndn = ndn_in;
   1068 		if ( !BER_BVISNULL( nbase ) ) {
   1069 			ber_dupbv( &el.el_nbase, nbase);
   1070 		}
   1071 		el.el_scope = scope;
   1072 		if ( !BER_BVISNULL( filter ) ) {
   1073 			ber_dupbv( &el.el_filter, filter  );
   1074 		}
   1075 
   1076 		el.el_a = attrs_dup( a );
   1077 		el.el_cb = cb;
   1078 
   1079 		for ( elpp = &mi->mi_entry_limbo;
   1080 				*elpp;
   1081 				elpp = &(*elpp)->el_next )
   1082 			/* go to last */;
   1083 
   1084 		*elpp = (entry_limbo_t *)ch_malloc( sizeof( entry_limbo_t ) );
   1085 		if ( *elpp == NULL ) {
   1086 			el.el_e->e_private = NULL;
   1087 			entry_free( el.el_e );
   1088 			return -1;
   1089 		}
   1090 
   1091 		if ( *elpp != NULL ) {
   1092 			el.el_next = NULL;
   1093 			**elpp = el;
   1094 
   1095 		} else {
   1096 			if ( !BER_BVISNULL( &el.el_filter ) ) {
   1097 				ch_free( el.el_filter.bv_val );
   1098 			}
   1099 			if ( el.el_a != NULL ) {
   1100 				attrs_free( el.el_a );
   1101 			}
   1102 			if ( !BER_BVISNULL( &el.el_nbase ) ) {
   1103 				ch_free( &el.el_nbase.bv_val );
   1104 			}
   1105 			return -1;
   1106 		}
   1107 	}
   1108 
   1109 	return 0;
   1110 }
   1111 
   1112 int
   1113 monitor_back_register_entry_callback(
   1114 	struct berval		*ndn,
   1115 	monitor_callback_t	*cb,
   1116 	struct berval		*nbase,
   1117 	int			scope,
   1118 	struct berval		*filter )
   1119 {
   1120 	return monitor_back_register_entry_attrs( ndn, NULL, cb,
   1121 			nbase, scope, filter );
   1122 }
   1123 
   1124 /*
   1125  * TODO: add corresponding calls to remove installed callbacks, entries
   1126  * and so, in case the entity that installed them is removed (e.g. a
   1127  * database, via back-config)
   1128  */
   1129 int
   1130 monitor_back_unregister_entry(
   1131 	struct berval	*ndn )
   1132 {
   1133 	monitor_info_t 	*mi;
   1134 
   1135 	if ( be_monitor == NULL ) {
   1136 		Debug( LDAP_DEBUG_ANY,
   1137 			"monitor_back_unregister_entry(\"%s\"): "
   1138 			"monitor database not configured.\n",
   1139 			ndn->bv_val, 0, 0 );
   1140 
   1141 		return -1;
   1142 	}
   1143 
   1144 	/* entry will be regularly freed, and resources released
   1145 	 * according to callbacks */
   1146 	if ( slapd_shutdown ) {
   1147 		return 0;
   1148 	}
   1149 
   1150 	mi = ( monitor_info_t * )be_monitor->be_private;
   1151 
   1152 	assert( mi != NULL );
   1153 
   1154 	if ( monitor_subsys_is_opened() ) {
   1155 		Entry			*e = NULL;
   1156 		monitor_entry_t 	*mp = NULL;
   1157 		monitor_callback_t	*cb = NULL;
   1158 
   1159 		if ( monitor_cache_remove( mi, ndn, &e ) != 0 ) {
   1160 			/* entry does not exist */
   1161 			Debug( LDAP_DEBUG_ANY,
   1162 				"monitor_back_unregister_entry(\"%s\"): "
   1163 				"entry removal failed.\n",
   1164 				ndn->bv_val, 0, 0 );
   1165 			return -1;
   1166 		}
   1167 
   1168 		mp = (monitor_entry_t *)e->e_private;
   1169 		assert( mp != NULL );
   1170 
   1171 		for ( cb = mp->mp_cb; cb != NULL; ) {
   1172 			monitor_callback_t	*next = cb->mc_next;
   1173 
   1174 			if ( cb->mc_free ) {
   1175 				(void)cb->mc_free( e, &cb->mc_private );
   1176 			}
   1177 			ch_free( cb );
   1178 
   1179 			cb = next;
   1180 		}
   1181 
   1182 		ch_free( mp );
   1183 		e->e_private = NULL;
   1184 		entry_free( e );
   1185 
   1186 	} else {
   1187 		entry_limbo_t	**elpp;
   1188 
   1189 		for ( elpp = &mi->mi_entry_limbo;
   1190 			*elpp;
   1191 			elpp = &(*elpp)->el_next )
   1192 		{
   1193 			entry_limbo_t	*elp = *elpp;
   1194 
   1195 			if ( elp->el_type == LIMBO_ENTRY
   1196 				&& dn_match( ndn, &elp->el_e->e_nname ) )
   1197 			{
   1198 				monitor_callback_t	*cb, *next;
   1199 
   1200 				for ( cb = elp->el_cb; cb; cb = next ) {
   1201 					/* FIXME: call callbacks? */
   1202 					next = cb->mc_next;
   1203 					if ( cb->mc_dispose ) {
   1204 						cb->mc_dispose( &cb->mc_private );
   1205 					}
   1206 					ch_free( cb );
   1207 				}
   1208 				assert( elp->el_e != NULL );
   1209 				elp->el_e->e_private = NULL;
   1210 				entry_free( elp->el_e );
   1211 				*elpp = elp->el_next;
   1212 				ch_free( elp );
   1213 				elpp = NULL;
   1214 				break;
   1215 			}
   1216 		}
   1217 
   1218 		if ( elpp != NULL ) {
   1219 			/* not found!  where did it go? */
   1220 			return 1;
   1221 		}
   1222 	}
   1223 
   1224 	return 0;
   1225 }
   1226 
   1227 int
   1228 monitor_back_unregister_entry_parent(
   1229 	struct berval		*nrdn,
   1230 	monitor_callback_t	*target_cb,
   1231 	struct berval		*nbase,
   1232 	int			scope,
   1233 	struct berval		*filter )
   1234 {
   1235 	monitor_info_t 	*mi;
   1236 	struct berval	ndn = BER_BVNULL;
   1237 
   1238 	if ( be_monitor == NULL ) {
   1239 		Debug( LDAP_DEBUG_ANY,
   1240 			"monitor_back_unregister_entry_parent(base=\"%s\" scope=%s filter=\"%s\"): "
   1241 			"monitor database not configured.\n",
   1242 			BER_BVISNULL( nbase ) ? "" : nbase->bv_val,
   1243 			ldap_pvt_scope2str( scope ),
   1244 			BER_BVISNULL( filter ) ? "" : filter->bv_val );
   1245 
   1246 		return -1;
   1247 	}
   1248 
   1249 	/* entry will be regularly freed, and resources released
   1250 	 * according to callbacks */
   1251 	if ( slapd_shutdown ) {
   1252 		return 0;
   1253 	}
   1254 
   1255 	mi = ( monitor_info_t * )be_monitor->be_private;
   1256 
   1257 	assert( mi != NULL );
   1258 
   1259 	if ( ( nrdn == NULL || BER_BVISNULL( nrdn ) )
   1260 			&& BER_BVISNULL( filter ) )
   1261 	{
   1262 		/* need a filter */
   1263 		Debug( LDAP_DEBUG_ANY,
   1264 			"monitor_back_unregister_entry_parent(\"\"): "
   1265 			"need a valid filter\n",
   1266 			0, 0, 0 );
   1267 		return -1;
   1268 	}
   1269 
   1270 	if ( monitor_subsys_is_opened() ) {
   1271 		Entry			*e = NULL;
   1272 		monitor_entry_t 	*mp = NULL;
   1273 
   1274 		if ( monitor_search2ndn( nbase, scope, filter, &ndn ) ) {
   1275 			/* entry does not exist */
   1276 			Debug( LDAP_DEBUG_ANY,
   1277 				"monitor_back_unregister_entry_parent(\"\"): "
   1278 				"base=\"%s\" scope=%s filter=\"%s\": "
   1279 				"unable to find entry\n",
   1280 				nbase->bv_val ? nbase->bv_val : "\"\"",
   1281 				ldap_pvt_scope2str( scope ),
   1282 				filter->bv_val );
   1283 			return -1;
   1284 		}
   1285 
   1286 		if ( monitor_cache_remove( mi, &ndn, &e ) != 0 ) {
   1287 			/* entry does not exist */
   1288 			Debug( LDAP_DEBUG_ANY,
   1289 				"monitor_back_unregister_entry(\"%s\"): "
   1290 				"entry removal failed.\n",
   1291 				ndn.bv_val, 0, 0 );
   1292 			ber_memfree( ndn.bv_val );
   1293 			return -1;
   1294 		}
   1295 		ber_memfree( ndn.bv_val );
   1296 
   1297 		mp = (monitor_entry_t *)e->e_private;
   1298 		assert( mp != NULL );
   1299 
   1300 		if ( target_cb != NULL ) {
   1301 			monitor_callback_t	**cbp;
   1302 
   1303 			for ( cbp = &mp->mp_cb; *cbp != NULL; cbp = &(*cbp)->mc_next ) {
   1304 				if ( *cbp == target_cb ) {
   1305 					if ( (*cbp)->mc_free ) {
   1306 						(void)(*cbp)->mc_free( e, &(*cbp)->mc_private );
   1307 					}
   1308 					*cbp = (*cbp)->mc_next;
   1309 					ch_free( target_cb );
   1310 					break;
   1311 				}
   1312 			}
   1313 		}
   1314 
   1315 
   1316 		ch_free( mp );
   1317 		e->e_private = NULL;
   1318 		entry_free( e );
   1319 
   1320 	} else {
   1321 		entry_limbo_t	**elpp;
   1322 
   1323 		for ( elpp = &mi->mi_entry_limbo;
   1324 			*elpp;
   1325 			elpp = &(*elpp)->el_next )
   1326 		{
   1327 			entry_limbo_t	*elp = *elpp;
   1328 
   1329 			if ( elp->el_type == LIMBO_ENTRY_PARENT
   1330 				&& dn_match( nrdn, &elp->el_e->e_nname )
   1331 				&& dn_match( nbase, &elp->el_nbase )
   1332 				&& scope == elp->el_scope
   1333 				&& bvmatch( filter, &elp->el_filter ) )
   1334 			{
   1335 				monitor_callback_t	*cb, *next;
   1336 
   1337 				for ( cb = elp->el_cb; cb; cb = next ) {
   1338 					/* FIXME: call callbacks? */
   1339 					next = cb->mc_next;
   1340 					if ( cb->mc_dispose ) {
   1341 						cb->mc_dispose( &cb->mc_private );
   1342 					}
   1343 					ch_free( cb );
   1344 				}
   1345 				assert( elp->el_e != NULL );
   1346 				elp->el_e->e_private = NULL;
   1347 				entry_free( elp->el_e );
   1348 				if ( !BER_BVISNULL( &elp->el_nbase ) ) {
   1349 					ch_free( elp->el_nbase.bv_val );
   1350 				}
   1351 				if ( !BER_BVISNULL( &elp->el_filter ) ) {
   1352 					ch_free( elp->el_filter.bv_val );
   1353 				}
   1354 				*elpp = elp->el_next;
   1355 				ch_free( elp );
   1356 				elpp = NULL;
   1357 				break;
   1358 			}
   1359 		}
   1360 
   1361 		if ( elpp != NULL ) {
   1362 			/* not found!  where did it go? */
   1363 			return 1;
   1364 		}
   1365 	}
   1366 
   1367 	return 0;
   1368 }
   1369 
   1370 int
   1371 monitor_back_unregister_entry_attrs(
   1372 	struct berval		*ndn_in,
   1373 	Attribute		*target_a,
   1374 	monitor_callback_t	*target_cb,
   1375 	struct berval		*nbase,
   1376 	int			scope,
   1377 	struct berval		*filter )
   1378 {
   1379 	monitor_info_t 	*mi;
   1380 	struct berval	ndn = BER_BVNULL;
   1381 	char		*fname = ( target_a == NULL ? "callback" : "attrs" );
   1382 
   1383 	if ( be_monitor == NULL ) {
   1384 		char		buf[ SLAP_TEXT_BUFLEN ];
   1385 
   1386 		snprintf( buf, sizeof( buf ),
   1387 			"monitor_back_unregister_entry_%s(base=\"%s\" scope=%s filter=\"%s\"): "
   1388 			"monitor database not configured.\n",
   1389 			fname,
   1390 			BER_BVISNULL( nbase ) ? "" : nbase->bv_val,
   1391 			ldap_pvt_scope2str( scope ),
   1392 			BER_BVISNULL( filter ) ? "" : filter->bv_val );
   1393 		Debug( LDAP_DEBUG_ANY, "%s\n", buf, 0, 0 );
   1394 
   1395 		return -1;
   1396 	}
   1397 
   1398 	/* entry will be regularly freed, and resources released
   1399 	 * according to callbacks */
   1400 	if ( slapd_shutdown ) {
   1401 		return 0;
   1402 	}
   1403 
   1404 	mi = ( monitor_info_t * )be_monitor->be_private;
   1405 
   1406 	assert( mi != NULL );
   1407 
   1408 	if ( ndn_in != NULL ) {
   1409 		ndn = *ndn_in;
   1410 	}
   1411 
   1412 	if ( target_a == NULL && target_cb == NULL ) {
   1413 		/* nothing to do */
   1414 		return -1;
   1415 	}
   1416 
   1417 	if ( ( ndn_in == NULL || BER_BVISNULL( &ndn ) )
   1418 			&& BER_BVISNULL( filter ) )
   1419 	{
   1420 		/* need a filter */
   1421 		Debug( LDAP_DEBUG_ANY,
   1422 			"monitor_back_unregister_entry_%s(\"\"): "
   1423 			"need a valid filter\n",
   1424 			fname, 0, 0 );
   1425 		return -1;
   1426 	}
   1427 
   1428 	if ( monitor_subsys_is_opened() ) {
   1429 		Entry			*e = NULL;
   1430 		monitor_entry_t 	*mp = NULL;
   1431 		int			freeit = 0;
   1432 
   1433 		if ( BER_BVISNULL( &ndn ) ) {
   1434 			if ( monitor_search2ndn( nbase, scope, filter, &ndn ) ) {
   1435 				char		buf[ SLAP_TEXT_BUFLEN ];
   1436 
   1437 				snprintf( buf, sizeof( buf ),
   1438 					"monitor_back_unregister_entry_%s(\"\"): "
   1439 					"base=\"%s\" scope=%d filter=\"%s\": "
   1440 					"unable to find entry\n",
   1441 					fname,
   1442 					nbase->bv_val ? nbase->bv_val : "\"\"",
   1443 					scope, filter->bv_val );
   1444 
   1445 				/* entry does not exist */
   1446 				Debug( LDAP_DEBUG_ANY, "%s\n", buf, 0, 0 );
   1447 				return -1;
   1448 			}
   1449 
   1450 			freeit = 1;
   1451 		}
   1452 
   1453 		if ( monitor_cache_get( mi, &ndn, &e ) != 0 ) {
   1454 			/* entry does not exist */
   1455 			Debug( LDAP_DEBUG_ANY,
   1456 				"monitor_back_unregister_entry(\"%s\"): "
   1457 				"entry removal failed.\n",
   1458 				ndn.bv_val, 0, 0 );
   1459 			return -1;
   1460 		}
   1461 
   1462 		mp = (monitor_entry_t *)e->e_private;
   1463 		assert( mp != NULL );
   1464 
   1465 		if ( target_cb != NULL ) {
   1466 			monitor_callback_t	**cbp;
   1467 
   1468 			for ( cbp = &mp->mp_cb; *cbp != NULL; cbp = &(*cbp)->mc_next ) {
   1469 				if ( *cbp == target_cb ) {
   1470 					if ( (*cbp)->mc_free ) {
   1471 						(void)(*cbp)->mc_free( e, &(*cbp)->mc_private );
   1472 					}
   1473 					*cbp = (*cbp)->mc_next;
   1474 					ch_free( target_cb );
   1475 					break;
   1476 				}
   1477 			}
   1478 		}
   1479 
   1480 		if ( target_a != NULL ) {
   1481 			Attribute	*a;
   1482 
   1483 			for ( a = target_a; a != NULL; a = a->a_next ) {
   1484 				Modification	mod = { 0 };
   1485 				const char	*text;
   1486 				char		textbuf[ SLAP_TEXT_BUFLEN ];
   1487 
   1488 				mod.sm_op = LDAP_MOD_DELETE;
   1489 				mod.sm_desc = a->a_desc;
   1490 				mod.sm_values = a->a_vals;
   1491 				mod.sm_nvalues = a->a_nvals;
   1492 
   1493 				(void)modify_delete_values( e, &mod, 1,
   1494 					&text, textbuf, sizeof( textbuf ) );
   1495 			}
   1496 		}
   1497 
   1498 		if ( freeit ) {
   1499 			ber_memfree( ndn.bv_val );
   1500 		}
   1501 
   1502 		monitor_cache_release( mi, e );
   1503 
   1504 	} else {
   1505 		entry_limbo_t	**elpp;
   1506 
   1507 		for ( elpp = &mi->mi_entry_limbo;
   1508 			*elpp;
   1509 			elpp = &(*elpp)->el_next )
   1510 		{
   1511 			entry_limbo_t	*elp = *elpp;
   1512 
   1513 			if ( elp->el_type == LIMBO_ATTRS
   1514 				&& dn_match( nbase, &elp->el_nbase )
   1515 				&& scope == elp->el_scope
   1516 				&& bvmatch( filter, &elp->el_filter ) )
   1517 			{
   1518 				monitor_callback_t	*cb, *next;
   1519 
   1520 				for ( cb = elp->el_cb; cb; cb = next ) {
   1521 					/* FIXME: call callbacks? */
   1522 					next = cb->mc_next;
   1523 					if ( cb->mc_dispose ) {
   1524 						cb->mc_dispose( &cb->mc_private );
   1525 					}
   1526 					ch_free( cb );
   1527 				}
   1528 				assert( elp->el_e == NULL );
   1529 				if ( elp->el_a != NULL ) {
   1530 					attrs_free( elp->el_a );
   1531 				}
   1532 				if ( !BER_BVISNULL( &elp->el_nbase ) ) {
   1533 					ch_free( elp->el_nbase.bv_val );
   1534 				}
   1535 				if ( !BER_BVISNULL( &elp->el_filter ) ) {
   1536 					ch_free( elp->el_filter.bv_val );
   1537 				}
   1538 				*elpp = elp->el_next;
   1539 				ch_free( elp );
   1540 				elpp = NULL;
   1541 				break;
   1542 			}
   1543 		}
   1544 
   1545 		if ( elpp != NULL ) {
   1546 			/* not found!  where did it go? */
   1547 			return 1;
   1548 		}
   1549 	}
   1550 
   1551 	return 0;
   1552 }
   1553 
   1554 int
   1555 monitor_back_unregister_entry_callback(
   1556 	struct berval		*ndn,
   1557 	monitor_callback_t	*cb,
   1558 	struct berval		*nbase,
   1559 	int			scope,
   1560 	struct berval		*filter )
   1561 {
   1562 	/* TODO: lookup entry (by ndn, if not NULL, and/or by callback);
   1563 	 * unregister the callback; if a is not null, unregister the
   1564 	 * given attrs.  In any case, call cb->cb_free */
   1565 	return monitor_back_unregister_entry_attrs( ndn,
   1566 		NULL, cb, nbase, scope, filter );
   1567 }
   1568 
   1569 monitor_subsys_t *
   1570 monitor_back_get_subsys( const char *name )
   1571 {
   1572 	if ( monitor_subsys != NULL ) {
   1573 		int	i;
   1574 
   1575 		for ( i = 0; monitor_subsys[ i ] != NULL; i++ ) {
   1576 			if ( strcasecmp( monitor_subsys[ i ]->mss_name, name ) == 0 ) {
   1577 				return monitor_subsys[ i ];
   1578 			}
   1579 		}
   1580 	}
   1581 
   1582 	return NULL;
   1583 }
   1584 
   1585 monitor_subsys_t *
   1586 monitor_back_get_subsys_by_dn(
   1587 	struct berval	*ndn,
   1588 	int		sub )
   1589 {
   1590 	if ( monitor_subsys != NULL ) {
   1591 		int	i;
   1592 
   1593 		if ( sub ) {
   1594 			for ( i = 0; monitor_subsys[ i ] != NULL; i++ ) {
   1595 				if ( dnIsSuffix( ndn, &monitor_subsys[ i ]->mss_ndn ) ) {
   1596 					return monitor_subsys[ i ];
   1597 				}
   1598 			}
   1599 
   1600 		} else {
   1601 			for ( i = 0; monitor_subsys[ i ] != NULL; i++ ) {
   1602 				if ( dn_match( ndn, &monitor_subsys[ i ]->mss_ndn ) ) {
   1603 					return monitor_subsys[ i ];
   1604 				}
   1605 			}
   1606 		}
   1607 	}
   1608 
   1609 	return NULL;
   1610 }
   1611 
   1612 int
   1613 monitor_back_initialize(
   1614 	BackendInfo	*bi )
   1615 {
   1616 	static char		*controls[] = {
   1617 		LDAP_CONTROL_MANAGEDSAIT,
   1618 		NULL
   1619 	};
   1620 
   1621 	static ConfigTable monitorcfg[] = {
   1622 		{ NULL, NULL, 0, 0, 0, ARG_IGNORED,
   1623 			NULL, NULL, NULL, NULL }
   1624 	};
   1625 
   1626 	static ConfigOCs monitorocs[] = {
   1627 		{ "( OLcfgDbOc:4.1 "
   1628 			"NAME 'olcMonitorConfig' "
   1629 			"DESC 'Monitor backend configuration' "
   1630 			"SUP olcDatabaseConfig "
   1631 			")",
   1632 			 	Cft_Database, monitorcfg },
   1633 		{ NULL, 0, NULL }
   1634 	};
   1635 
   1636 	struct m_s {
   1637 		char	*schema;
   1638 		slap_mask_t flags;
   1639 		int	offset;
   1640 	} moc[] = {
   1641 		{ "( 1.3.6.1.4.1.4203.666.3.16.1 "
   1642 			"NAME 'monitor' "
   1643 			"DESC 'OpenLDAP system monitoring' "
   1644 			"SUP top STRUCTURAL "
   1645 			"MUST cn "
   1646 			"MAY ( "
   1647 				"description "
   1648 				"$ seeAlso "
   1649 				"$ labeledURI "
   1650 				"$ monitoredInfo "
   1651 				"$ managedInfo "
   1652 				"$ monitorOverlay "
   1653 			") )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
   1654 			offsetof(monitor_info_t, mi_oc_monitor) },
   1655 		{ "( 1.3.6.1.4.1.4203.666.3.16.2 "
   1656 			"NAME 'monitorServer' "
   1657 			"DESC 'Server monitoring root entry' "
   1658 			"SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
   1659 			offsetof(monitor_info_t, mi_oc_monitorServer) },
   1660 		{ "( 1.3.6.1.4.1.4203.666.3.16.3 "
   1661 			"NAME 'monitorContainer' "
   1662 			"DESC 'monitor container class' "
   1663 			"SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
   1664 			offsetof(monitor_info_t, mi_oc_monitorContainer) },
   1665 		{ "( 1.3.6.1.4.1.4203.666.3.16.4 "
   1666 			"NAME 'monitorCounterObject' "
   1667 			"DESC 'monitor counter class' "
   1668 			"SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
   1669 			offsetof(monitor_info_t, mi_oc_monitorCounterObject) },
   1670 		{ "( 1.3.6.1.4.1.4203.666.3.16.5 "
   1671 			"NAME 'monitorOperation' "
   1672 			"DESC 'monitor operation class' "
   1673 			"SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
   1674 			offsetof(monitor_info_t, mi_oc_monitorOperation) },
   1675 		{ "( 1.3.6.1.4.1.4203.666.3.16.6 "
   1676 			"NAME 'monitorConnection' "
   1677 			"DESC 'monitor connection class' "
   1678 			"SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
   1679 			offsetof(monitor_info_t, mi_oc_monitorConnection) },
   1680 		{ "( 1.3.6.1.4.1.4203.666.3.16.7 "
   1681 			"NAME 'managedObject' "
   1682 			"DESC 'monitor managed entity class' "
   1683 			"SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
   1684 			offsetof(monitor_info_t, mi_oc_managedObject) },
   1685 		{ "( 1.3.6.1.4.1.4203.666.3.16.8 "
   1686 			"NAME 'monitoredObject' "
   1687 			"DESC 'monitor monitored entity class' "
   1688 			"SUP monitor STRUCTURAL )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
   1689 			offsetof(monitor_info_t, mi_oc_monitoredObject) },
   1690 		{ NULL, 0, -1 }
   1691 	}, mat[] = {
   1692 		{ "( 1.3.6.1.4.1.4203.666.1.55.1 "
   1693 			"NAME 'monitoredInfo' "
   1694 			"DESC 'monitored info' "
   1695 			/* "SUP name " */
   1696 			"EQUALITY caseIgnoreMatch "
   1697 			"SUBSTR caseIgnoreSubstringsMatch "
   1698 			"SYNTAX 1.3.6.1.4.1.1466.115.121.1.15{32768} "
   1699 			"NO-USER-MODIFICATION "
   1700 			"USAGE dSAOperation )", SLAP_AT_HIDE,
   1701 			offsetof(monitor_info_t, mi_ad_monitoredInfo) },
   1702 		{ "( 1.3.6.1.4.1.4203.666.1.55.2 "
   1703 			"NAME 'managedInfo' "
   1704 			"DESC 'monitor managed info' "
   1705 			"SUP name )", SLAP_AT_HIDE,
   1706 			offsetof(monitor_info_t, mi_ad_managedInfo) },
   1707 		{ "( 1.3.6.1.4.1.4203.666.1.55.3 "
   1708 			"NAME 'monitorCounter' "
   1709 			"DESC 'monitor counter' "
   1710 			"EQUALITY integerMatch "
   1711 			"ORDERING integerOrderingMatch "
   1712 			"SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 "
   1713 			"NO-USER-MODIFICATION "
   1714 			"USAGE dSAOperation )", SLAP_AT_HIDE,
   1715 			offsetof(monitor_info_t, mi_ad_monitorCounter) },
   1716 		{ "( 1.3.6.1.4.1.4203.666.1.55.4 "
   1717 			"NAME 'monitorOpCompleted' "
   1718 			"DESC 'monitor completed operations' "
   1719 			"SUP monitorCounter "
   1720 			"NO-USER-MODIFICATION "
   1721 			"USAGE dSAOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
   1722 			offsetof(monitor_info_t, mi_ad_monitorOpCompleted) },
   1723 		{ "( 1.3.6.1.4.1.4203.666.1.55.5 "
   1724 			"NAME 'monitorOpInitiated' "
   1725 			"DESC 'monitor initiated operations' "
   1726 			"SUP monitorCounter "
   1727 			"NO-USER-MODIFICATION "
   1728 			"USAGE dSAOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
   1729 			offsetof(monitor_info_t, mi_ad_monitorOpInitiated) },
   1730 		{ "( 1.3.6.1.4.1.4203.666.1.55.6 "
   1731 			"NAME 'monitorConnectionNumber' "
   1732 			"DESC 'monitor connection number' "
   1733 			"SUP monitorCounter "
   1734 			"NO-USER-MODIFICATION "
   1735 			"USAGE dSAOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
   1736 			offsetof(monitor_info_t, mi_ad_monitorConnectionNumber) },
   1737 		{ "( 1.3.6.1.4.1.4203.666.1.55.7 "
   1738 			"NAME 'monitorConnectionAuthzDN' "
   1739 			"DESC 'monitor connection authorization DN' "
   1740 			/* "SUP distinguishedName " */
   1741 			"EQUALITY distinguishedNameMatch "
   1742 			"SYNTAX 1.3.6.1.4.1.1466.115.121.1.12 "
   1743 			"NO-USER-MODIFICATION "
   1744 			"USAGE dSAOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
   1745 			offsetof(monitor_info_t, mi_ad_monitorConnectionAuthzDN) },
   1746 		{ "( 1.3.6.1.4.1.4203.666.1.55.8 "
   1747 			"NAME 'monitorConnectionLocalAddress' "
   1748 			"DESC 'monitor connection local address' "
   1749 			"SUP monitoredInfo "
   1750 			"NO-USER-MODIFICATION "
   1751 			"USAGE dSAOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
   1752 			offsetof(monitor_info_t, mi_ad_monitorConnectionLocalAddress) },
   1753 		{ "( 1.3.6.1.4.1.4203.666.1.55.9 "
   1754 			"NAME 'monitorConnectionPeerAddress' "
   1755 			"DESC 'monitor connection peer address' "
   1756 			"SUP monitoredInfo "
   1757 			"NO-USER-MODIFICATION "
   1758 			"USAGE dSAOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
   1759 			offsetof(monitor_info_t, mi_ad_monitorConnectionPeerAddress) },
   1760 		{ "( 1.3.6.1.4.1.4203.666.1.55.10 "
   1761 			"NAME 'monitorTimestamp' "
   1762 			"DESC 'monitor timestamp' "
   1763 			"EQUALITY generalizedTimeMatch "
   1764 			"ORDERING generalizedTimeOrderingMatch "
   1765 			"SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
   1766 			"SINGLE-VALUE "
   1767 			"NO-USER-MODIFICATION "
   1768 			"USAGE dSAOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
   1769 			offsetof(monitor_info_t, mi_ad_monitorTimestamp) },
   1770 		{ "( 1.3.6.1.4.1.4203.666.1.55.11 "
   1771 			"NAME 'monitorOverlay' "
   1772 			"DESC 'name of overlays defined for a given database' "
   1773 			"SUP monitoredInfo "
   1774 			"NO-USER-MODIFICATION "
   1775 			"USAGE dSAOperation )", SLAP_AT_HIDE,
   1776 			offsetof(monitor_info_t, mi_ad_monitorOverlay) },
   1777 		{ "( 1.3.6.1.4.1.4203.666.1.55.12 "
   1778 			"NAME 'readOnly' "
   1779 			"DESC 'read/write status of a given database' "
   1780 			"EQUALITY booleanMatch "
   1781 			"SYNTAX 1.3.6.1.4.1.1466.115.121.1.7 "
   1782 			"SINGLE-VALUE "
   1783 			"USAGE dSAOperation )", SLAP_AT_HIDE,
   1784 			offsetof(monitor_info_t, mi_ad_readOnly) },
   1785 		{ "( 1.3.6.1.4.1.4203.666.1.55.13 "
   1786 			"NAME 'restrictedOperation' "
   1787 			"DESC 'name of restricted operation for a given database' "
   1788 			"SUP managedInfo )", SLAP_AT_HIDE,
   1789 			offsetof(monitor_info_t, mi_ad_restrictedOperation ) },
   1790 		{ "( 1.3.6.1.4.1.4203.666.1.55.14 "
   1791 			"NAME 'monitorConnectionProtocol' "
   1792 			"DESC 'monitor connection protocol' "
   1793 			"SUP monitoredInfo "
   1794 			"NO-USER-MODIFICATION "
   1795 			"USAGE dSAOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
   1796 			offsetof(monitor_info_t, mi_ad_monitorConnectionProtocol) },
   1797 		{ "( 1.3.6.1.4.1.4203.666.1.55.15 "
   1798 			"NAME 'monitorConnectionOpsReceived' "
   1799 			"DESC 'monitor number of operations received by the connection' "
   1800 			"SUP monitorCounter "
   1801 			"NO-USER-MODIFICATION "
   1802 			"USAGE dSAOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
   1803 			offsetof(monitor_info_t, mi_ad_monitorConnectionOpsReceived) },
   1804 		{ "( 1.3.6.1.4.1.4203.666.1.55.16 "
   1805 			"NAME 'monitorConnectionOpsExecuting' "
   1806 			"DESC 'monitor number of operations in execution within the connection' "
   1807 			"SUP monitorCounter "
   1808 			"NO-USER-MODIFICATION "
   1809 			"USAGE dSAOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
   1810 			offsetof(monitor_info_t, mi_ad_monitorConnectionOpsExecuting) },
   1811 		{ "( 1.3.6.1.4.1.4203.666.1.55.17 "
   1812 			"NAME 'monitorConnectionOpsPending' "
   1813 			"DESC 'monitor number of pending operations within the connection' "
   1814 			"SUP monitorCounter "
   1815 			"NO-USER-MODIFICATION "
   1816 			"USAGE dSAOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
   1817 			offsetof(monitor_info_t, mi_ad_monitorConnectionOpsPending) },
   1818 		{ "( 1.3.6.1.4.1.4203.666.1.55.18 "
   1819 			"NAME 'monitorConnectionOpsCompleted' "
   1820 			"DESC 'monitor number of operations completed within the connection' "
   1821 			"SUP monitorCounter "
   1822 			"NO-USER-MODIFICATION "
   1823 			"USAGE dSAOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
   1824 			offsetof(monitor_info_t, mi_ad_monitorConnectionOpsCompleted) },
   1825 		{ "( 1.3.6.1.4.1.4203.666.1.55.19 "
   1826 			"NAME 'monitorConnectionGet' "
   1827 			"DESC 'number of times connection_get() was called so far' "
   1828 			"SUP monitorCounter "
   1829 			"NO-USER-MODIFICATION "
   1830 			"USAGE dSAOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
   1831 			offsetof(monitor_info_t, mi_ad_monitorConnectionGet) },
   1832 		{ "( 1.3.6.1.4.1.4203.666.1.55.20 "
   1833 			"NAME 'monitorConnectionRead' "
   1834 			"DESC 'number of times connection_read() was called so far' "
   1835 			"SUP monitorCounter "
   1836 			"NO-USER-MODIFICATION "
   1837 			"USAGE dSAOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
   1838 			offsetof(monitor_info_t, mi_ad_monitorConnectionRead) },
   1839 		{ "( 1.3.6.1.4.1.4203.666.1.55.21 "
   1840 			"NAME 'monitorConnectionWrite' "
   1841 			"DESC 'number of times connection_write() was called so far' "
   1842 			"SUP monitorCounter "
   1843 			"NO-USER-MODIFICATION "
   1844 			"USAGE dSAOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
   1845 			offsetof(monitor_info_t, mi_ad_monitorConnectionWrite) },
   1846 		{ "( 1.3.6.1.4.1.4203.666.1.55.22 "
   1847 			"NAME 'monitorConnectionMask' "
   1848 			"DESC 'monitor connection mask' "
   1849 			"SUP monitoredInfo "
   1850 			"NO-USER-MODIFICATION "
   1851 			"USAGE dSAOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
   1852 			offsetof(monitor_info_t, mi_ad_monitorConnectionMask) },
   1853 		{ "( 1.3.6.1.4.1.4203.666.1.55.23 "
   1854 			"NAME 'monitorConnectionListener' "
   1855 			"DESC 'monitor connection listener' "
   1856 			"SUP monitoredInfo "
   1857 			"NO-USER-MODIFICATION "
   1858 			"USAGE dSAOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
   1859 			offsetof(monitor_info_t, mi_ad_monitorConnectionListener) },
   1860 		{ "( 1.3.6.1.4.1.4203.666.1.55.24 "
   1861 			"NAME 'monitorConnectionPeerDomain' "
   1862 			"DESC 'monitor connection peer domain' "
   1863 			"SUP monitoredInfo "
   1864 			"NO-USER-MODIFICATION "
   1865 			"USAGE dSAOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
   1866 			offsetof(monitor_info_t, mi_ad_monitorConnectionPeerDomain) },
   1867 		{ "( 1.3.6.1.4.1.4203.666.1.55.25 "
   1868 			"NAME 'monitorConnectionStartTime' "
   1869 			"DESC 'monitor connection start time' "
   1870 			"SUP monitorTimestamp "
   1871 			"SINGLE-VALUE "
   1872 			"NO-USER-MODIFICATION "
   1873 			"USAGE dSAOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
   1874 			offsetof(monitor_info_t, mi_ad_monitorConnectionStartTime) },
   1875 		{ "( 1.3.6.1.4.1.4203.666.1.55.26 "
   1876 			"NAME 'monitorConnectionActivityTime' "
   1877 			"DESC 'monitor connection activity time' "
   1878 			"SUP monitorTimestamp "
   1879 			"SINGLE-VALUE "
   1880 			"NO-USER-MODIFICATION "
   1881 			"USAGE dSAOperation )", SLAP_AT_FINAL|SLAP_AT_HIDE,
   1882 			offsetof(monitor_info_t, mi_ad_monitorConnectionActivityTime) },
   1883 		{ "( 1.3.6.1.4.1.4203.666.1.55.27 "
   1884 			"NAME 'monitorIsShadow' "
   1885 			"DESC 'TRUE if the database is shadow' "
   1886 			"EQUALITY booleanMatch "
   1887 			"SYNTAX 1.3.6.1.4.1.1466.115.121.1.7 "
   1888 			"SINGLE-VALUE "
   1889 			"USAGE dSAOperation )", SLAP_AT_HIDE,
   1890 			offsetof(monitor_info_t, mi_ad_monitorIsShadow) },
   1891 		{ "( 1.3.6.1.4.1.4203.666.1.55.28 "
   1892 			"NAME 'monitorUpdateRef' "
   1893 			"DESC 'update referral for shadow databases' "
   1894 			"SUP monitoredInfo "
   1895 			"SINGLE-VALUE "
   1896 			"USAGE dSAOperation )", SLAP_AT_HIDE,
   1897 			offsetof(monitor_info_t, mi_ad_monitorUpdateRef) },
   1898 		{ "( 1.3.6.1.4.1.4203.666.1.55.29 "
   1899 			"NAME 'monitorRuntimeConfig' "
   1900 			"DESC 'TRUE if component allows runtime configuration' "
   1901 			"EQUALITY booleanMatch "
   1902 			"SYNTAX 1.3.6.1.4.1.1466.115.121.1.7 "
   1903 			"SINGLE-VALUE "
   1904 			"USAGE dSAOperation )", SLAP_AT_HIDE,
   1905 			offsetof(monitor_info_t, mi_ad_monitorRuntimeConfig) },
   1906 		{ NULL, 0, -1 }
   1907 	};
   1908 
   1909 	static struct {
   1910 		char			*name;
   1911 		char			*oid;
   1912 	}		s_oid[] = {
   1913 		{ "olmAttributes",			"1.3.6.1.4.1.4203.666.1.55" },
   1914 		{ "olmSubSystemAttributes",		"olmAttributes:0" },
   1915 		{ "olmGenericAttributes",		"olmSubSystemAttributes:0" },
   1916 		{ "olmDatabaseAttributes",		"olmSubSystemAttributes:1" },
   1917 
   1918 		/* for example, back-bdb specific attrs
   1919 		 * are in "olmDatabaseAttributes:1"
   1920 		 *
   1921 		 * NOTE: developers, please record here OID assignments
   1922 		 * for other modules */
   1923 
   1924 		{ "olmObjectClasses",			"1.3.6.1.4.1.4203.666.3.16" },
   1925 		{ "olmSubSystemObjectClasses",		"olmObjectClasses:0" },
   1926 		{ "olmGenericObjectClasses",		"olmSubSystemObjectClasses:0" },
   1927 		{ "olmDatabaseObjectClasses",		"olmSubSystemObjectClasses:1" },
   1928 
   1929 		/* for example, back-bdb specific objectClasses
   1930 		 * are in "olmDatabaseObjectClasses:1"
   1931 		 *
   1932 		 * NOTE: developers, please record here OID assignments
   1933 		 * for other modules */
   1934 
   1935 		{ NULL }
   1936 	};
   1937 
   1938 	int			i, rc;
   1939 	monitor_info_t		*mi = &monitor_info;
   1940 	ConfigArgs c;
   1941 	char	*argv[ 3 ];
   1942 
   1943 	argv[ 0 ] = "monitor";
   1944 	c.argv = argv;
   1945 	c.argc = 3;
   1946 	c.fname = argv[0];
   1947 
   1948 	for ( i = 0; s_oid[ i ].name; i++ ) {
   1949 		argv[ 1 ] = s_oid[ i ].name;
   1950 		argv[ 2 ] = s_oid[ i ].oid;
   1951 
   1952 		if ( parse_oidm( &c, 0, NULL ) != 0 ) {
   1953 			Debug( LDAP_DEBUG_ANY,
   1954 				"monitor_back_initialize: unable to add "
   1955 				"objectIdentifier \"%s=%s\"\n",
   1956 				s_oid[ i ].name, s_oid[ i ].oid, 0 );
   1957 			return 1;
   1958 		}
   1959 	}
   1960 
   1961 	/* schema integration */
   1962 	for ( i = 0; mat[ i ].schema; i++ ) {
   1963 		int			code;
   1964 		AttributeDescription **ad =
   1965 			((AttributeDescription **)&(((char *)mi)[ mat[ i ].offset ]));
   1966 
   1967 		*ad = NULL;
   1968 		code = register_at( mat[ i ].schema, ad, 0 );
   1969 
   1970 		if ( code ) {
   1971 			Debug( LDAP_DEBUG_ANY,
   1972 				"monitor_back_db_init: register_at failed\n", 0, 0, 0 );
   1973 			return -1;
   1974 		}
   1975 		(*ad)->ad_type->sat_flags |= mat[ i ].flags;
   1976 	}
   1977 
   1978 	for ( i = 0; moc[ i ].schema; i++ ) {
   1979 		int			code;
   1980 		ObjectClass		**Oc =
   1981 			((ObjectClass **)&(((char *)mi)[ moc[ i ].offset ]));
   1982 
   1983 		code = register_oc( moc[ i ].schema, Oc, 0 );
   1984 		if ( code ) {
   1985 			Debug( LDAP_DEBUG_ANY,
   1986 				"monitor_back_db_init: register_oc failed\n", 0, 0, 0 );
   1987 			return -1;
   1988 		}
   1989 		(*Oc)->soc_flags |= moc[ i ].flags;
   1990 	}
   1991 
   1992 	bi->bi_controls = controls;
   1993 
   1994 	bi->bi_init = 0;
   1995 	bi->bi_open = 0;
   1996 	bi->bi_config = monitor_back_config;
   1997 	bi->bi_close = 0;
   1998 	bi->bi_destroy = 0;
   1999 
   2000 	bi->bi_db_init = monitor_back_db_init;
   2001 #if 0
   2002 	bi->bi_db_config = monitor_back_db_config;
   2003 #endif
   2004 	bi->bi_db_open = monitor_back_db_open;
   2005 	bi->bi_db_close = 0;
   2006 	bi->bi_db_destroy = monitor_back_db_destroy;
   2007 
   2008 	bi->bi_op_bind = monitor_back_bind;
   2009 	bi->bi_op_unbind = 0;
   2010 	bi->bi_op_search = monitor_back_search;
   2011 	bi->bi_op_compare = monitor_back_compare;
   2012 	bi->bi_op_modify = monitor_back_modify;
   2013 	bi->bi_op_modrdn = 0;
   2014 	bi->bi_op_add = 0;
   2015 	bi->bi_op_delete = 0;
   2016 	bi->bi_op_abandon = 0;
   2017 
   2018 	bi->bi_extended = 0;
   2019 
   2020 	bi->bi_entry_release_rw = 0;
   2021 	bi->bi_chk_referrals = 0;
   2022 	bi->bi_operational = monitor_back_operational;
   2023 
   2024 	/*
   2025 	 * hooks for slap tools
   2026 	 */
   2027 	bi->bi_tool_entry_open = 0;
   2028 	bi->bi_tool_entry_close = 0;
   2029 	bi->bi_tool_entry_first = 0;
   2030 	bi->bi_tool_entry_next = 0;
   2031 	bi->bi_tool_entry_get = 0;
   2032 	bi->bi_tool_entry_put = 0;
   2033 	bi->bi_tool_entry_reindex = 0;
   2034 	bi->bi_tool_sync = 0;
   2035 	bi->bi_tool_dn2id_get = 0;
   2036 	bi->bi_tool_entry_modify = 0;
   2037 
   2038 	bi->bi_connection_init = 0;
   2039 	bi->bi_connection_destroy = 0;
   2040 
   2041 	bi->bi_extra = (void *)&monitor_extra;
   2042 
   2043 	/*
   2044 	 * configuration objectClasses (fake)
   2045 	 */
   2046 	bi->bi_cf_ocs = monitorocs;
   2047 
   2048 	rc = config_register_schema( monitorcfg, monitorocs );
   2049 	if ( rc ) {
   2050 		return rc;
   2051 	}
   2052 
   2053 	return 0;
   2054 }
   2055 
   2056 int
   2057 monitor_back_db_init(
   2058 	BackendDB	*be,
   2059 	ConfigReply	*c)
   2060 {
   2061 	int			rc;
   2062 	struct berval		dn = BER_BVC( SLAPD_MONITOR_DN ),
   2063 				pdn,
   2064 				ndn;
   2065 	BackendDB		*be2;
   2066 
   2067 	monitor_subsys_t	*ms;
   2068 
   2069 	/*
   2070 	 * database monitor can be defined once only
   2071 	 */
   2072 	if ( be_monitor != NULL ) {
   2073 		if (c) {
   2074 			snprintf(c->msg, sizeof(c->msg),"only one monitor database allowed");
   2075 		}
   2076 		return( -1 );
   2077 	}
   2078 	be_monitor = be;
   2079 
   2080 	/*
   2081 	 * register subsys
   2082 	 */
   2083 	for ( ms = known_monitor_subsys; ms->mss_name != NULL; ms++ ) {
   2084 		if ( monitor_back_register_subsys( ms ) ) {
   2085 			return -1;
   2086 		}
   2087 	}
   2088 
   2089 	/* indicate system schema supported */
   2090 	SLAP_BFLAGS(be) |= SLAP_BFLAG_MONITOR;
   2091 
   2092 	rc = dnPrettyNormal( NULL, &dn, &pdn, &ndn, NULL );
   2093 	if( rc != LDAP_SUCCESS ) {
   2094 		Debug( LDAP_DEBUG_ANY,
   2095 			"unable to normalize/pretty monitor DN \"%s\" (%d)\n",
   2096 			dn.bv_val, rc, 0 );
   2097 		return -1;
   2098 	}
   2099 
   2100 	ber_bvarray_add( &be->be_suffix, &pdn );
   2101 	ber_bvarray_add( &be->be_nsuffix, &ndn );
   2102 
   2103 	/* NOTE: only one monitor database is allowed,
   2104 	 * so we use static storage */
   2105 	ldap_pvt_thread_mutex_init( &monitor_info.mi_cache_mutex );
   2106 
   2107 	be->be_private = &monitor_info;
   2108 
   2109 	be2 = select_backend( &ndn, 0 );
   2110 	if ( be2 != be ) {
   2111 		char	*type = be2->bd_info->bi_type;
   2112 
   2113 		if ( overlay_is_over( be2 ) ) {
   2114 			slap_overinfo	*oi = (slap_overinfo *)be2->bd_info->bi_private;
   2115 			type = oi->oi_orig->bi_type;
   2116 		}
   2117 
   2118 		if (c) {
   2119 			snprintf(c->msg, sizeof(c->msg),
   2120 					"\"monitor\" database serving namingContext \"%s\" "
   2121 					"is hidden by \"%s\" database serving namingContext \"%s\".\n",
   2122 					pdn.bv_val, type, be2->be_nsuffix[ 0 ].bv_val );
   2123 		}
   2124 		return -1;
   2125 	}
   2126 
   2127 	return 0;
   2128 }
   2129 
   2130 static void
   2131 monitor_back_destroy_limbo_entry(
   2132 	entry_limbo_t	*el,
   2133 	int		dispose )
   2134 {
   2135 	if ( el->el_e ) {
   2136 		entry_free( el->el_e );
   2137 	}
   2138 	if ( el->el_a ) {
   2139 		attrs_free( el->el_a );
   2140 	}
   2141 	if ( !BER_BVISNULL( &el->el_nbase ) ) {
   2142 		ber_memfree( el->el_nbase.bv_val );
   2143 	}
   2144 	if ( !BER_BVISNULL( &el->el_filter ) ) {
   2145 		ber_memfree( el->el_filter.bv_val );
   2146 	}
   2147 
   2148 	/* NOTE: callbacks are not copied; so only free them
   2149 	 * if disposing of */
   2150 	if ( el->el_cb && dispose != 0 ) {
   2151 		monitor_callback_t *next;
   2152 
   2153 		for ( ; el->el_cb; el->el_cb = next ) {
   2154 			next = el->el_cb->mc_next;
   2155 			if ( el->el_cb->mc_dispose ) {
   2156 				el->el_cb->mc_dispose( &el->el_cb->mc_private );
   2157 			}
   2158 			ch_free( el->el_cb );
   2159 		}
   2160 	}
   2161 
   2162 	ch_free( el );
   2163 }
   2164 
   2165 int
   2166 monitor_back_db_open(
   2167 	BackendDB	*be,
   2168 	ConfigReply	*cr)
   2169 {
   2170 	monitor_info_t 		*mi = (monitor_info_t *)be->be_private;
   2171 	struct monitor_subsys_t	**ms;
   2172 	Entry 			*e, **ep, *root;
   2173 	monitor_entry_t		*mp;
   2174 	int			i;
   2175 	struct berval		bv, rdn = BER_BVC(SLAPD_MONITOR_DN);
   2176 	struct tm		*tms;
   2177 #ifdef HAVE_GMTIME_R
   2178 	struct tm		tm_buf;
   2179 #endif
   2180 	static char		tmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
   2181 	struct berval	desc[] = {
   2182 		BER_BVC("This subtree contains monitoring/managing objects."),
   2183 		BER_BVC("This object contains information about this server."),
   2184 		BER_BVC("Most of the information is held in operational"
   2185 		" attributes, which must be explicitly requested."),
   2186 		BER_BVNULL };
   2187 
   2188 	int			retcode = 0;
   2189 
   2190 	assert( be_monitor != NULL );
   2191 	if ( be != be_monitor ) {
   2192 		be_monitor = be;
   2193 	}
   2194 
   2195 	/*
   2196 	 * Start
   2197 	 */
   2198 #ifndef HAVE_GMTIME_R
   2199 	ldap_pvt_thread_mutex_lock( &gmtime_mutex );
   2200 #endif
   2201 #ifdef HACK_LOCAL_TIME
   2202 # ifdef HAVE_LOCALTIME_R
   2203 	tms = localtime_r( &starttime, &tm_buf );
   2204 # else
   2205 	tms = localtime( &starttime );
   2206 # endif /* HAVE_LOCALTIME_R */
   2207 	lutil_localtime( tmbuf, sizeof(tmbuf), tms, -timezone );
   2208 #else /* !HACK_LOCAL_TIME */
   2209 # ifdef HAVE_GMTIME_R
   2210 	tms = gmtime_r( &starttime, &tm_buf );
   2211 # else
   2212 	tms = gmtime( &starttime );
   2213 # endif /* HAVE_GMTIME_R */
   2214 	lutil_gentime( tmbuf, sizeof(tmbuf), tms );
   2215 #endif /* !HACK_LOCAL_TIME */
   2216 #ifndef HAVE_GMTIME_R
   2217 	ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
   2218 #endif
   2219 
   2220 	mi->mi_startTime.bv_val = tmbuf;
   2221 	mi->mi_startTime.bv_len = strlen( tmbuf );
   2222 
   2223 	if ( BER_BVISEMPTY( &be->be_rootdn ) ) {
   2224 		BER_BVSTR( &mi->mi_creatorsName, SLAPD_ANONYMOUS );
   2225 		BER_BVSTR( &mi->mi_ncreatorsName, SLAPD_ANONYMOUS );
   2226 	} else {
   2227 		mi->mi_creatorsName = be->be_rootdn;
   2228 		mi->mi_ncreatorsName = be->be_rootndn;
   2229 	}
   2230 
   2231 	/*
   2232 	 * creates the "cn=Monitor" entry
   2233 	 */
   2234 	e = monitor_entry_stub( NULL, NULL, &rdn, mi->mi_oc_monitorServer, mi,
   2235 		NULL, NULL );
   2236 
   2237 	if ( e == NULL) {
   2238 		Debug( LDAP_DEBUG_ANY,
   2239 			"unable to create \"%s\" entry\n",
   2240 			SLAPD_MONITOR_DN, 0, 0 );
   2241 		return( -1 );
   2242 	}
   2243 
   2244 	attr_merge_normalize( e, slap_schema.si_ad_description, desc, NULL );
   2245 
   2246 	bv.bv_val = strchr( (char *) Versionstr, '$' );
   2247 	if ( bv.bv_val != NULL ) {
   2248 		char	*end;
   2249 
   2250 		bv.bv_val++;
   2251 		for ( ; bv.bv_val[ 0 ] == ' '; bv.bv_val++ )
   2252 			;
   2253 
   2254 		end = strchr( bv.bv_val, '$' );
   2255 		if ( end != NULL ) {
   2256 			end--;
   2257 
   2258 			for ( ; end > bv.bv_val && end[ 0 ] == ' '; end-- )
   2259 				;
   2260 
   2261 			end++;
   2262 
   2263 			bv.bv_len = end - bv.bv_val;
   2264 
   2265 		} else {
   2266 			bv.bv_len = strlen( bv.bv_val );
   2267 		}
   2268 
   2269 		if ( attr_merge_normalize_one( e, mi->mi_ad_monitoredInfo,
   2270 					&bv, NULL ) ) {
   2271 			Debug( LDAP_DEBUG_ANY,
   2272 				"unable to add monitoredInfo to \"%s\" entry\n",
   2273 				SLAPD_MONITOR_DN, 0, 0 );
   2274 			return( -1 );
   2275 		}
   2276 	}
   2277 
   2278 	mp = monitor_entrypriv_create();
   2279 	if ( mp == NULL ) {
   2280 		return -1;
   2281 	}
   2282 	e->e_private = ( void * )mp;
   2283 	ep = &mp->mp_children;
   2284 
   2285 	if ( monitor_cache_add( mi, e ) ) {
   2286 		Debug( LDAP_DEBUG_ANY,
   2287 			"unable to add entry \"%s\" to cache\n",
   2288 			SLAPD_MONITOR_DN, 0, 0 );
   2289 		return -1;
   2290 	}
   2291 	root = e;
   2292 
   2293 	/*
   2294 	 * Create all the subsystem specific entries
   2295 	 */
   2296 	for ( i = 0; monitor_subsys[ i ] != NULL; i++ ) {
   2297 		int 		len = strlen( monitor_subsys[ i ]->mss_name );
   2298 		struct berval	dn;
   2299 		int		rc;
   2300 
   2301 		dn.bv_len = len + sizeof( "cn=" ) - 1;
   2302 		dn.bv_val = ch_calloc( sizeof( char ), dn.bv_len + 1 );
   2303 		strcpy( dn.bv_val, "cn=" );
   2304 		strcat( dn.bv_val, monitor_subsys[ i ]->mss_name );
   2305 		rc = dnPretty( NULL, &dn, &monitor_subsys[ i ]->mss_rdn, NULL );
   2306 		free( dn.bv_val );
   2307 		if ( rc != LDAP_SUCCESS ) {
   2308 			Debug( LDAP_DEBUG_ANY,
   2309 				"monitor RDN \"%s\" is invalid\n",
   2310 				dn.bv_val, 0, 0 );
   2311 			return( -1 );
   2312 		}
   2313 
   2314 		e = monitor_entry_stub( &root->e_name, &root->e_nname,
   2315 			&monitor_subsys[ i ]->mss_rdn, mi->mi_oc_monitorContainer, mi,
   2316 			NULL, NULL );
   2317 
   2318 		if ( e == NULL) {
   2319 			Debug( LDAP_DEBUG_ANY,
   2320 				"unable to create \"%s\" entry\n",
   2321 				monitor_subsys[ i ]->mss_dn.bv_val, 0, 0 );
   2322 			return( -1 );
   2323 		}
   2324 		monitor_subsys[i]->mss_dn = e->e_name;
   2325 		monitor_subsys[i]->mss_ndn = e->e_nname;
   2326 
   2327 		if ( !BER_BVISNULL( &monitor_subsys[ i ]->mss_desc[ 0 ] ) ) {
   2328 			attr_merge_normalize( e, slap_schema.si_ad_description,
   2329 					monitor_subsys[ i ]->mss_desc, NULL );
   2330 		}
   2331 
   2332 		mp = monitor_entrypriv_create();
   2333 		if ( mp == NULL ) {
   2334 			return -1;
   2335 		}
   2336 		e->e_private = ( void * )mp;
   2337 		mp->mp_info = monitor_subsys[ i ];
   2338 		mp->mp_flags = monitor_subsys[ i ]->mss_flags;
   2339 
   2340 		if ( monitor_cache_add( mi, e ) ) {
   2341 			Debug( LDAP_DEBUG_ANY,
   2342 				"unable to add entry \"%s\" to cache\n",
   2343 				monitor_subsys[ i ]->mss_dn.bv_val, 0, 0 );
   2344 			return -1;
   2345 		}
   2346 
   2347 		*ep = e;
   2348 		ep = &mp->mp_next;
   2349 	}
   2350 
   2351 	assert( be != NULL );
   2352 
   2353 	be->be_private = mi;
   2354 
   2355 	/*
   2356 	 * opens the monitor backend subsystems
   2357 	 */
   2358 	for ( ms = monitor_subsys; ms[ 0 ] != NULL; ms++ ) {
   2359 		if ( ms[ 0 ]->mss_open && ( *ms[ 0 ]->mss_open )( be, ms[ 0 ] ) )
   2360 		{
   2361 			return( -1 );
   2362 		}
   2363 		ms[ 0 ]->mss_flags |= MONITOR_F_OPENED;
   2364 	}
   2365 
   2366 	monitor_subsys_opened = 1;
   2367 
   2368 	if ( mi->mi_entry_limbo ) {
   2369 		entry_limbo_t	*el = mi->mi_entry_limbo;
   2370 
   2371 		for ( ; el; ) {
   2372 			entry_limbo_t	*tmp;
   2373 			int		rc;
   2374 
   2375 			switch ( el->el_type ) {
   2376 			case LIMBO_ENTRY:
   2377 				rc = monitor_back_register_entry(
   2378 						el->el_e,
   2379 						el->el_cb,
   2380 						el->el_mss,
   2381 						el->el_flags );
   2382 				break;
   2383 
   2384 			case LIMBO_ENTRY_PARENT:
   2385 				rc = monitor_back_register_entry_parent(
   2386 						el->el_e,
   2387 						el->el_cb,
   2388 						el->el_mss,
   2389 						el->el_flags,
   2390 						&el->el_nbase,
   2391 						el->el_scope,
   2392 						&el->el_filter );
   2393 				break;
   2394 
   2395 
   2396 			case LIMBO_ATTRS:
   2397 				rc = monitor_back_register_entry_attrs(
   2398 						el->el_ndn,
   2399 						el->el_a,
   2400 						el->el_cb,
   2401 						&el->el_nbase,
   2402 						el->el_scope,
   2403 						&el->el_filter );
   2404 				break;
   2405 
   2406 			case LIMBO_CB:
   2407 				rc = monitor_back_register_entry_callback(
   2408 						el->el_ndn,
   2409 						el->el_cb,
   2410 						&el->el_nbase,
   2411 						el->el_scope,
   2412 						&el->el_filter );
   2413 				break;
   2414 
   2415 			case LIMBO_BACKEND:
   2416 				rc = monitor_back_register_backend( el->el_bi );
   2417 				break;
   2418 
   2419 			case LIMBO_DATABASE:
   2420 				rc = monitor_back_register_database( el->el_be, el->el_ndn );
   2421 				break;
   2422 
   2423 			case LIMBO_OVERLAY_INFO:
   2424 				rc = monitor_back_register_overlay_info( el->el_on );
   2425 				break;
   2426 
   2427 			case LIMBO_OVERLAY:
   2428 				rc = monitor_back_register_overlay( el->el_be );
   2429 				break;
   2430 
   2431 			default:
   2432 				assert( 0 );
   2433 			}
   2434 
   2435 			tmp = el;
   2436 			el = el->el_next;
   2437 			monitor_back_destroy_limbo_entry( tmp, rc );
   2438 
   2439 			if ( rc != 0 ) {
   2440 				/* try all, but report error at end */
   2441 				retcode = 1;
   2442 			}
   2443 		}
   2444 
   2445 		mi->mi_entry_limbo = NULL;
   2446 	}
   2447 
   2448 	return retcode;
   2449 }
   2450 
   2451 int
   2452 monitor_back_config(
   2453 	BackendInfo	*bi,
   2454 	const char	*fname,
   2455 	int		lineno,
   2456 	int		argc,
   2457 	char		**argv )
   2458 {
   2459 	/*
   2460 	 * eventually, will hold backend specific configuration parameters
   2461 	 */
   2462 	return SLAP_CONF_UNKNOWN;
   2463 }
   2464 
   2465 #if 0
   2466 int
   2467 monitor_back_db_config(
   2468 	Backend     *be,
   2469 	const char  *fname,
   2470 	int         lineno,
   2471 	int         argc,
   2472 	char        **argv )
   2473 {
   2474 	monitor_info_t	*mi = ( monitor_info_t * )be->be_private;
   2475 
   2476 	/*
   2477 	 * eventually, will hold database specific configuration parameters
   2478 	 */
   2479 	return SLAP_CONF_UNKNOWN;
   2480 }
   2481 #endif
   2482 
   2483 int
   2484 monitor_back_db_destroy(
   2485 	BackendDB	*be,
   2486 	ConfigReply	*cr)
   2487 {
   2488 	monitor_info_t	*mi = ( monitor_info_t * )be->be_private;
   2489 
   2490 	if ( mi == NULL ) {
   2491 		return -1;
   2492 	}
   2493 
   2494 	/*
   2495 	 * FIXME: destroys all the data
   2496 	 */
   2497 	/* NOTE: mi points to static storage; don't free it */
   2498 
   2499 	(void)monitor_cache_destroy( mi );
   2500 
   2501 	if ( monitor_subsys ) {
   2502 		int	i;
   2503 
   2504 		for ( i = 0; monitor_subsys[ i ] != NULL; i++ ) {
   2505 			if ( monitor_subsys[ i ]->mss_destroy ) {
   2506 				monitor_subsys[ i ]->mss_destroy( be, monitor_subsys[ i ] );
   2507 			}
   2508 
   2509 			if ( !BER_BVISNULL( &monitor_subsys[ i ]->mss_rdn ) ) {
   2510 				ch_free( monitor_subsys[ i ]->mss_rdn.bv_val );
   2511 			}
   2512 		}
   2513 
   2514 		ch_free( monitor_subsys );
   2515 	}
   2516 
   2517 	if ( mi->mi_entry_limbo ) {
   2518 		entry_limbo_t	*el = mi->mi_entry_limbo;
   2519 
   2520 		for ( ; el; ) {
   2521 			entry_limbo_t *tmp = el;
   2522 			el = el->el_next;
   2523 			monitor_back_destroy_limbo_entry( tmp, 1 );
   2524 		}
   2525 	}
   2526 
   2527 	ldap_pvt_thread_mutex_destroy( &monitor_info.mi_cache_mutex );
   2528 
   2529 	be->be_private = NULL;
   2530 
   2531 	return 0;
   2532 }
   2533 
   2534 #if SLAPD_MONITOR == SLAPD_MOD_DYNAMIC
   2535 
   2536 /* conditionally define the init_module() function */
   2537 SLAP_BACKEND_INIT_MODULE( monitor )
   2538 
   2539 #endif /* SLAPD_MONITOR == SLAPD_MOD_DYNAMIC */
   2540 
   2541