+ nbase, scope, filter );
+}
+
+/*
+ * TODO: add corresponding calls to remove installed callbacks, entries
+ * and so, in case the entity that installed them is removed (e.g. a
+ * database, via back-config)
+ */
+int
+monitor_back_unregister_entry(
+ struct berval *ndn )
+{
+ monitor_info_t *mi;
+
+ if ( be_monitor == NULL ) {
+ Debug( LDAP_DEBUG_ANY,
+ "monitor_back_unregister_entry(\"%s\"): "
+ "monitor database not configured.\n",
+ ndn->bv_val, 0, 0 );
+
+ return -1;
+ }
+
+ /* entry will be regularly freed, and resources released
+ * according to callbacks */
+ if ( slapd_shutdown ) {
+ return 0;
+ }
+
+ mi = ( monitor_info_t * )be_monitor->be_private;
+
+ assert( mi != NULL );
+
+ if ( monitor_subsys_is_opened() ) {
+ Entry *e = NULL;
+ monitor_entry_t *mp = NULL;
+ monitor_callback_t *cb = NULL;
+
+ if ( monitor_cache_remove( mi, ndn, &e ) != 0 ) {
+ /* entry does not exist */
+ Debug( LDAP_DEBUG_ANY,
+ "monitor_back_unregister_entry(\"%s\"): "
+ "entry removal failed.\n",
+ ndn->bv_val, 0, 0 );
+ return -1;
+ }
+
+ mp = (monitor_entry_t *)e->e_private;
+ assert( mp != NULL );
+
+ for ( cb = mp->mp_cb; cb != NULL; ) {
+ monitor_callback_t *next = cb->mc_next;
+
+ if ( cb->mc_free ) {
+ (void)cb->mc_free( e, &cb->mc_private );
+ }
+ ch_free( cb );
+
+ cb = next;
+ }
+
+ ch_free( mp );
+ e->e_private = NULL;
+ entry_free( e );
+
+ } else {
+ entry_limbo_t **elpp;
+
+ for ( elpp = &mi->mi_entry_limbo;
+ *elpp;
+ elpp = &(*elpp)->el_next )
+ {
+ entry_limbo_t *elp = *elpp;
+
+ if ( elp->el_type == LIMBO_ENTRY
+ && dn_match( ndn, &elp->el_e->e_nname ) )
+ {
+ monitor_callback_t *cb, *next;
+
+ for ( cb = elp->el_cb; cb; cb = next ) {
+ /* FIXME: call callbacks? */
+ next = cb->mc_next;
+ if ( cb->mc_dispose ) {
+ cb->mc_dispose( &cb->mc_private );
+ }
+ ch_free( cb );
+ }
+ assert( elp->el_e != NULL );
+ elp->el_e->e_private = NULL;
+ entry_free( elp->el_e );
+ *elpp = elp->el_next;
+ ch_free( elp );
+ elpp = NULL;
+ break;
+ }
+ }
+
+ if ( elpp != NULL ) {
+ /* not found! where did it go? */
+ return 1;
+ }
+ }
+
+ return 0;
+}
+
+int
+monitor_back_unregister_entry_parent(
+ struct berval *nrdn,
+ monitor_callback_t *target_cb,
+ struct berval *nbase,
+ int scope,
+ struct berval *filter )
+{
+ monitor_info_t *mi;
+ struct berval ndn = BER_BVNULL;
+
+ if ( be_monitor == NULL ) {
+ Debug( LDAP_DEBUG_ANY,
+ "monitor_back_unregister_entry_parent(base=\"%s\" scope=%s filter=\"%s\"): "
+ "monitor database not configured.\n",
+ BER_BVISNULL( nbase ) ? "" : nbase->bv_val,
+ ldap_pvt_scope2str( scope ),
+ BER_BVISNULL( filter ) ? "" : filter->bv_val );
+
+ return -1;
+ }
+
+ /* entry will be regularly freed, and resources released
+ * according to callbacks */
+ if ( slapd_shutdown ) {
+ return 0;
+ }
+
+ mi = ( monitor_info_t * )be_monitor->be_private;
+
+ assert( mi != NULL );
+
+ if ( ( nrdn == NULL || BER_BVISNULL( nrdn ) )
+ && BER_BVISNULL( filter ) )
+ {
+ /* need a filter */
+ Debug( LDAP_DEBUG_ANY,
+ "monitor_back_unregister_entry_parent(\"\"): "
+ "need a valid filter\n",
+ 0, 0, 0 );
+ return -1;
+ }
+
+ if ( monitor_subsys_is_opened() ) {
+ Entry *e = NULL;
+ monitor_entry_t *mp = NULL;
+
+ if ( monitor_search2ndn( nbase, scope, filter, &ndn ) ) {
+ /* entry does not exist */
+ Debug( LDAP_DEBUG_ANY,
+ "monitor_back_unregister_entry_parent(\"\"): "
+ "base=\"%s\" scope=%s filter=\"%s\": "
+ "unable to find entry\n",
+ nbase->bv_val ? nbase->bv_val : "\"\"",
+ ldap_pvt_scope2str( scope ),
+ filter->bv_val );
+ return -1;
+ }
+
+ if ( monitor_cache_remove( mi, &ndn, &e ) != 0 ) {
+ /* entry does not exist */
+ Debug( LDAP_DEBUG_ANY,
+ "monitor_back_unregister_entry(\"%s\"): "
+ "entry removal failed.\n",
+ ndn.bv_val, 0, 0 );
+ ber_memfree( ndn.bv_val );
+ return -1;
+ }
+ ber_memfree( ndn.bv_val );
+
+ mp = (monitor_entry_t *)e->e_private;
+ assert( mp != NULL );
+
+ if ( target_cb != NULL ) {
+ monitor_callback_t **cbp;
+
+ for ( cbp = &mp->mp_cb; *cbp != NULL; cbp = &(*cbp)->mc_next ) {
+ if ( *cbp == target_cb ) {
+ if ( (*cbp)->mc_free ) {
+ (void)(*cbp)->mc_free( e, &(*cbp)->mc_private );
+ }
+ *cbp = (*cbp)->mc_next;
+ ch_free( target_cb );
+ break;
+ }
+ }
+ }
+
+
+ ch_free( mp );
+ e->e_private = NULL;
+ entry_free( e );
+
+ } else {
+ entry_limbo_t **elpp;
+
+ for ( elpp = &mi->mi_entry_limbo;
+ *elpp;
+ elpp = &(*elpp)->el_next )
+ {
+ entry_limbo_t *elp = *elpp;
+
+ if ( elp->el_type == LIMBO_ENTRY_PARENT
+ && dn_match( nrdn, &elp->el_e->e_nname )
+ && dn_match( nbase, &elp->el_nbase )
+ && scope == elp->el_scope
+ && bvmatch( filter, &elp->el_filter ) )
+ {
+ monitor_callback_t *cb, *next;
+
+ for ( cb = elp->el_cb; cb; cb = next ) {
+ /* FIXME: call callbacks? */
+ next = cb->mc_next;
+ if ( cb->mc_dispose ) {
+ cb->mc_dispose( &cb->mc_private );
+ }
+ ch_free( cb );
+ }
+ assert( elp->el_e != NULL );
+ elp->el_e->e_private = NULL;
+ entry_free( elp->el_e );
+ if ( !BER_BVISNULL( &elp->el_nbase ) ) {
+ ch_free( elp->el_nbase.bv_val );
+ }
+ if ( !BER_BVISNULL( &elp->el_filter ) ) {
+ ch_free( elp->el_filter.bv_val );
+ }
+ *elpp = elp->el_next;
+ ch_free( elp );
+ elpp = NULL;
+ break;
+ }
+ }
+
+ if ( elpp != NULL ) {
+ /* not found! where did it go? */
+ return 1;
+ }
+ }
+
+ return 0;
+}
+
+int
+monitor_back_unregister_entry_attrs(
+ struct berval *ndn_in,
+ Attribute *target_a,
+ monitor_callback_t *target_cb,
+ struct berval *nbase,
+ int scope,
+ struct berval *filter )
+{
+ monitor_info_t *mi;
+ struct berval ndn = BER_BVNULL;
+ char *fname = ( target_a == NULL ? "callback" : "attrs" );
+
+ if ( be_monitor == NULL ) {
+ char buf[ SLAP_TEXT_BUFLEN ];
+
+ snprintf( buf, sizeof( buf ),
+ "monitor_back_unregister_entry_%s(base=\"%s\" scope=%s filter=\"%s\"): "
+ "monitor database not configured.\n",
+ fname,
+ BER_BVISNULL( nbase ) ? "" : nbase->bv_val,
+ ldap_pvt_scope2str( scope ),
+ BER_BVISNULL( filter ) ? "" : filter->bv_val );
+ Debug( LDAP_DEBUG_ANY, "%s\n", buf, 0, 0 );
+
+ return -1;
+ }
+
+ /* entry will be regularly freed, and resources released
+ * according to callbacks */
+ if ( slapd_shutdown ) {
+ return 0;
+ }
+
+ mi = ( monitor_info_t * )be_monitor->be_private;
+
+ assert( mi != NULL );
+
+ if ( ndn_in != NULL ) {
+ ndn = *ndn_in;
+ }
+
+ if ( target_a == NULL && target_cb == NULL ) {
+ /* nothing to do */
+ return -1;
+ }
+
+ if ( ( ndn_in == NULL || BER_BVISNULL( &ndn ) )
+ && BER_BVISNULL( filter ) )
+ {
+ /* need a filter */
+ Debug( LDAP_DEBUG_ANY,
+ "monitor_back_unregister_entry_%s(\"\"): "
+ "need a valid filter\n",
+ fname, 0, 0 );
+ return -1;
+ }
+
+ if ( monitor_subsys_is_opened() ) {
+ Entry *e = NULL;
+ monitor_entry_t *mp = NULL;
+ int freeit = 0;
+
+ if ( BER_BVISNULL( &ndn ) ) {
+ if ( monitor_search2ndn( nbase, scope, filter, &ndn ) ) {
+ char buf[ SLAP_TEXT_BUFLEN ];
+
+ snprintf( buf, sizeof( buf ),
+ "monitor_back_unregister_entry_%s(\"\"): "
+ "base=\"%s\" scope=%d filter=\"%s\": "
+ "unable to find entry\n",
+ fname,
+ nbase->bv_val ? nbase->bv_val : "\"\"",
+ scope, filter->bv_val );
+
+ /* entry does not exist */
+ Debug( LDAP_DEBUG_ANY, "%s\n", buf, 0, 0 );
+ return -1;
+ }
+
+ freeit = 1;
+ }
+
+ if ( monitor_cache_get( mi, &ndn, &e ) != 0 ) {
+ /* entry does not exist */
+ Debug( LDAP_DEBUG_ANY,
+ "monitor_back_unregister_entry(\"%s\"): "
+ "entry removal failed.\n",
+ ndn.bv_val, 0, 0 );
+ return -1;
+ }
+
+ mp = (monitor_entry_t *)e->e_private;
+ assert( mp != NULL );
+
+ if ( target_cb != NULL ) {
+ monitor_callback_t **cbp;
+
+ for ( cbp = &mp->mp_cb; *cbp != NULL; cbp = &(*cbp)->mc_next ) {
+ if ( *cbp == target_cb ) {
+ if ( (*cbp)->mc_free ) {
+ (void)(*cbp)->mc_free( e, &(*cbp)->mc_private );
+ }
+ *cbp = (*cbp)->mc_next;
+ ch_free( target_cb );
+ break;
+ }
+ }
+ }
+
+ if ( target_a != NULL ) {
+ Attribute *a;
+
+ for ( a = target_a; a != NULL; a = a->a_next ) {
+ Modification mod = { 0 };
+ const char *text;
+ char textbuf[ SLAP_TEXT_BUFLEN ];
+
+ mod.sm_op = LDAP_MOD_DELETE;
+ mod.sm_desc = a->a_desc;
+ mod.sm_values = a->a_vals;
+ mod.sm_nvalues = a->a_nvals;
+
+ (void)modify_delete_values( e, &mod, 1,
+ &text, textbuf, sizeof( textbuf ) );
+ }
+ }
+
+ if ( freeit ) {
+ ber_memfree( ndn.bv_val );
+ }
+
+ monitor_cache_release( mi, e );
+
+ } else {
+ entry_limbo_t **elpp;
+
+ for ( elpp = &mi->mi_entry_limbo;
+ *elpp;
+ elpp = &(*elpp)->el_next )
+ {
+ entry_limbo_t *elp = *elpp;
+
+ if ( elp->el_type == LIMBO_ATTRS
+ && dn_match( nbase, &elp->el_nbase )
+ && scope == elp->el_scope
+ && bvmatch( filter, &elp->el_filter ) )
+ {
+ monitor_callback_t *cb, *next;
+
+ for ( cb = elp->el_cb; cb; cb = next ) {
+ /* FIXME: call callbacks? */
+ next = cb->mc_next;
+ if ( cb->mc_dispose ) {
+ cb->mc_dispose( &cb->mc_private );
+ }
+ ch_free( cb );
+ }
+ assert( elp->el_e == NULL );
+ if ( elp->el_a != NULL ) {
+ attrs_free( elp->el_a );
+ }
+ if ( !BER_BVISNULL( &elp->el_nbase ) ) {
+ ch_free( elp->el_nbase.bv_val );
+ }
+ if ( !BER_BVISNULL( &elp->el_filter ) ) {
+ ch_free( elp->el_filter.bv_val );
+ }
+ *elpp = elp->el_next;
+ ch_free( elp );
+ elpp = NULL;
+ break;
+ }
+ }
+
+ if ( elpp != NULL ) {
+ /* not found! where did it go? */
+ return 1;
+ }
+ }
+
+ return 0;
+}
+
+int
+monitor_back_unregister_entry_callback(
+ struct berval *ndn,
+ monitor_callback_t *cb,
+ struct berval *nbase,
+ int scope,
+ struct berval *filter )
+{
+ /* TODO: lookup entry (by ndn, if not NULL, and/or by callback);
+ * unregister the callback; if a is not null, unregister the
+ * given attrs. In any case, call cb->cb_free */
+ return monitor_back_unregister_entry_attrs( ndn,
+ NULL, cb, nbase, scope, filter );