From 6193ee4003a1aeb8d70b59b90e1ee242100b190e Mon Sep 17 00:00:00 2001 From: Pierangelo Masarati Date: Sun, 24 Dec 2006 15:44:56 +0000 Subject: [PATCH] don't leak callbacks if stuff cannot be registered; provide a means to dispose of private stuff if required --- servers/slapd/back-bdb/monitor.c | 5 ++++- servers/slapd/back-ldap/monitor.c | 7 ++++--- servers/slapd/back-monitor/back-monitor.h | 6 +++++- servers/slapd/back-monitor/cache.c | 2 +- servers/slapd/back-monitor/init.c | 23 ++++++++++++++--------- 5 files changed, 28 insertions(+), 15 deletions(-) diff --git a/servers/slapd/back-bdb/monitor.c b/servers/slapd/back-bdb/monitor.c index 7349796381..e0693f4914 100644 --- a/servers/slapd/back-bdb/monitor.c +++ b/servers/slapd/back-bdb/monitor.c @@ -152,6 +152,7 @@ bdb_monitor_update( return SLAP_CB_CONTINUE; } +#if 0 /* uncomment if required */ static int bdb_monitor_modify( Operation *op, @@ -161,11 +162,12 @@ bdb_monitor_modify( { return SLAP_CB_CONTINUE; } +#endif static int bdb_monitor_free( Entry *e, - void *priv ) + void **priv ) { struct berval values[ 2 ]; Modification mod = { 0 }; @@ -176,6 +178,7 @@ bdb_monitor_free( int i, rc; /* NOTE: if slap_shutdown != 0, priv might have already been freed */ + *priv = NULL; /* Remove objectClass */ mod.sm_op = LDAP_MOD_DELETE; diff --git a/servers/slapd/back-ldap/monitor.c b/servers/slapd/back-ldap/monitor.c index 288ff6a440..b621807f02 100644 --- a/servers/slapd/back-ldap/monitor.c +++ b/servers/slapd/back-ldap/monitor.c @@ -265,9 +265,11 @@ done:; static int ldap_back_monitor_free( Entry *e, - void *priv ) + void **priv ) { - ldapinfo_t *li = (ldapinfo_t *)priv; + ldapinfo_t *li = (ldapinfo_t *)(*priv); + + *priv = NULL; if ( !slapd_shutdown && !BER_BVISNULL( &li->li_monitor_info.lmi_rdn ) ) { ldap_back_monitor_info_destroy( li ); @@ -306,7 +308,6 @@ static int ldap_back_monitor_initialize( void ) { int i, code; - const char *err; ConfigArgs c; char *argv[ 3 ]; diff --git a/servers/slapd/back-monitor/back-monitor.h b/servers/slapd/back-monitor/back-monitor.h index 905bcf73ff..3c8e742b8f 100644 --- a/servers/slapd/back-monitor/back-monitor.h +++ b/servers/slapd/back-monitor/back-monitor.h @@ -39,9 +39,13 @@ typedef struct monitor_callback_t { int (*mc_modify)( Operation *op, SlapReply *rs, Entry *e, void *priv ); /* modify callback for user-defined entries */ - int (*mc_free)( Entry *e, void *priv ); + int (*mc_free)( Entry *e, void **priv ); /* delete callback for user-defined entries */ + void (*mc_dispose)( void **priv ); + /* dispose callback + to dispose of the callback + private data itself */ void *mc_private; /* opaque pointer to private data */ struct monitor_callback_t *mc_next; diff --git a/servers/slapd/back-monitor/cache.c b/servers/slapd/back-monitor/cache.c index 9c96c0c154..5f198080bb 100644 --- a/servers/slapd/back-monitor/cache.c +++ b/servers/slapd/back-monitor/cache.c @@ -405,7 +405,7 @@ monitor_entry_destroy( void *v_mc ) monitor_callback_t *next = cb->mc_next; if ( cb->mc_free ) { - cb->mc_free( mc->mc_e, cb->mc_private ); + (void)cb->mc_free( mc->mc_e, &cb->mc_private ); } ch_free( mp->mp_cb ); diff --git a/servers/slapd/back-monitor/init.c b/servers/slapd/back-monitor/init.c index 4b32e124b0..388488b2ed 100644 --- a/servers/slapd/back-monitor/init.c +++ b/servers/slapd/back-monitor/init.c @@ -992,7 +992,6 @@ done:; return -1; } -done_limbo:; if ( *elpp != NULL ) { el.el_next = NULL; **elpp = el; @@ -1080,7 +1079,7 @@ monitor_back_unregister_entry( monitor_callback_t *next = cb->mc_next; if ( cb->mc_free ) { - (void)cb->mc_free( e, cb->mc_private ); + (void)cb->mc_free( e, &cb->mc_private ); } ch_free( cb ); @@ -1208,7 +1207,7 @@ monitor_back_unregister_entry_parent( 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 ); + (void)(*cbp)->mc_free( e, &(*cbp)->mc_private ); } *cbp = (*cbp)->mc_next; ch_free( target_cb ); @@ -1370,7 +1369,7 @@ monitor_back_unregister_entry_attrs( 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 ); + (void)(*cbp)->mc_free( e, &(*cbp)->mc_private ); } *cbp = (*cbp)->mc_next; ch_free( target_cb ); @@ -1837,7 +1836,6 @@ monitor_back_initialize( }; int i, rc; - const char *text; monitor_info_t *mi = &monitor_info; ConfigArgs c; char *argv[ 3 ]; @@ -2231,10 +2229,11 @@ monitor_back_db_open( for ( ; el; ) { entry_limbo_t *tmp; + int rc; switch ( el->el_type ) { case LIMBO_ENTRY: - monitor_back_register_entry( + rc = monitor_back_register_entry( el->el_e, el->el_cb, el->el_mss, @@ -2242,7 +2241,7 @@ monitor_back_db_open( break; case LIMBO_ENTRY_PARENT: - monitor_back_register_entry_parent( + rc = monitor_back_register_entry_parent( el->el_e, el->el_cb, el->el_mss, @@ -2254,7 +2253,7 @@ monitor_back_db_open( case LIMBO_ATTRS: - monitor_back_register_entry_attrs( + rc = monitor_back_register_entry_attrs( &el->el_ndn, el->el_a, el->el_cb, @@ -2264,7 +2263,7 @@ monitor_back_db_open( break; case LIMBO_CB: - monitor_back_register_entry_callback( + rc = monitor_back_register_entry_callback( &el->el_ndn, el->el_cb, &el->el_nbase, @@ -2291,6 +2290,12 @@ monitor_back_db_open( if ( !BER_BVISNULL( &el->el_filter ) ) { ber_memfree( el->el_filter.bv_val ); } + if ( el->el_cb && rc != 0 ) { + if ( el->el_cb->mc_dispose ) { + el->el_cb->mc_dispose( &el->el_cb->mc_private ); + } + ch_free( el->el_cb ); + } tmp = el; el = el->el_next;