[collectd] [PATCH] perl plugin: Unregister all callbacks from collectd when shutting down.

Sebastian Harl sh at tokkee.org
Sun May 20 22:58:03 CEST 2007


If we do not unregister the callbacks there is still an entry point into the
plugin after Perl has been shut down and its memory has been freed. This will
cause segmentation faults if collectd tries to call any of the registered
callbacks, e.g. by some plugin calling plugin_log () during shutdown.

Important note: Unregistering the shutdown callback during shutdown is
somewhat tricky. Doing that frees the llist_t entry in plugin.c:list_shutdown.
This might cause a segfault in plugin_shutdown_all () when it tries to access
the next pointer of that entry. If plugin_unregister_shutdown () is the last
statement before the return from the shutdown callback this should be save in
about 99% of all cases. Still there should be some better way to handle this.

Signed-off-by: Sebastian Harl <sh at tokkee.org>
---
 src/perl.c |   21 +++++++++++++++++++++
 1 files changed, 21 insertions(+), 0 deletions(-)

diff --git a/src/perl.c b/src/perl.c
index 3be3762..bcf7bb1 100644
--- a/src/perl.c
+++ b/src/perl.c
@@ -959,6 +959,8 @@ static XS (boot_Collectd)
 
 static int perl_config (const char *key, const char *value)
 {
+	assert (NULL != perl);
+
 	log_debug ("perl_config: key = \"%s\", value=\"%s\"", key, value);
 
 	if (0 == strcasecmp (key, "LoadPlugin")) {
@@ -993,24 +995,32 @@ static int perl_config (const char *key, const char *value)
 
 static int perl_init (void)
 {
+	assert (NULL != perl);
+
 	PERL_SET_CONTEXT (perl);
 	return pplugin_call_all (PLUGIN_INIT);
 } /* static int perl_init (void) */
 
 static int perl_read (void)
 {
+	assert (NULL != perl);
+
 	PERL_SET_CONTEXT (perl);
 	return pplugin_call_all (PLUGIN_READ);
 } /* static int perl_read (void) */
 
 static int perl_write (const data_set_t *ds, const value_list_t *vl)
 {
+	assert (NULL != perl);
+
 	PERL_SET_CONTEXT (perl);
 	return pplugin_call_all (PLUGIN_WRITE, ds, vl);
 } /* static int perl_write (const data_set_t *, const value_list_t *) */
 
 static void perl_log (int level, const char *msg)
 {
+	assert (NULL != perl);
+
 	PERL_SET_CONTEXT (perl);
 	pplugin_call_all (PLUGIN_LOG, level, msg);
 	return;
@@ -1021,6 +1031,14 @@ static int perl_shutdown (void)
 	int i   = 0;
 	int ret = 0;
 
+	plugin_unregister_log ("perl");
+	plugin_unregister_config ("perl");
+	plugin_unregister_init ("perl");
+	plugin_unregister_read ("perl");
+	plugin_unregister_write ("perl");
+
+	assert (NULL != perl);
+
 	PERL_SET_CONTEXT (perl);
 	ret = pplugin_call_all (PLUGIN_SHUTDOWN);
 
@@ -1054,8 +1072,11 @@ static int perl_shutdown (void)
 
 	perl_destruct (perl);
 	perl_free (perl);
+	perl = NULL;
 
 	PERL_SYS_TERM ();
+
+	plugin_unregister_shutdown ("perl");
 	return ret;
 } /* static void perl_shutdown (void) */
 
-- 
1.4.4.4

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://mailman.verplant.org/pipermail/collectd/attachments/20070520/81f64765/attachment.pgp 


More information about the collectd mailing list