From 6e50de5cc715881f736631ed81341fd2ef6e250c Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Sun, 22 Feb 2009 21:48:05 +0100 Subject: [PATCH] src/plugin.c: Add a user_data_t pointer to flush callbacks. --- src/network.c | 6 +++-- src/perl.c | 5 ++-- src/plugin.c | 77 +++++++++++++++++++++++++++++++++++++++++++++++------------ src/plugin.h | 4 +++- src/rrdtool.c | 5 ++-- 5 files changed, 75 insertions(+), 22 deletions(-) diff --git a/src/network.c b/src/network.c index 3b2c25b1..0434b359 100644 --- a/src/network.c +++ b/src/network.c @@ -1812,7 +1812,8 @@ static int network_init (void) * there, good. If not, well, then there is nothing to flush.. -octo */ static int network_flush (int timeout, - const char __attribute__((unused)) *identifier) + const char __attribute__((unused)) *identifier, + user_data_t __attribute__((unused)) *user_data) { pthread_mutex_lock (&send_buffer_lock); @@ -1832,5 +1833,6 @@ void module_register (void) plugin_register_config ("network", network_config, config_keys, config_keys_num); plugin_register_init ("network", network_init); - plugin_register_flush ("network", network_flush); + plugin_register_flush ("network", network_flush, + /* user_data = */ NULL); } /* void module_register */ diff --git a/src/perl.c b/src/perl.c index 00912668..b600ca1d 100644 --- a/src/perl.c +++ b/src/perl.c @@ -1973,7 +1973,8 @@ static int perl_notify (const notification_t *notif) return pplugin_call_all (aTHX_ PLUGIN_NOTIF, notif); } /* static int perl_notify (const notification_t *) */ -static int perl_flush (int timeout, const char *identifier) +static int perl_flush (int timeout, const char *identifier, + user_data_t __attribute__((unused)) *user_data) { dTHX; @@ -2226,7 +2227,7 @@ static int init_pi (int argc, char **argv) plugin_register_read ("perl", perl_read); plugin_register_write ("perl", perl_write, /* user_data = */ NULL); - plugin_register_flush ("perl", perl_flush); + plugin_register_flush ("perl", perl_flush, /* user_data = */ NULL); plugin_register_shutdown ("perl", perl_shutdown); return 0; } /* static int init_pi (const char **, const int) */ diff --git a/src/plugin.c b/src/plugin.c index bba8e54f..49793dca 100644 --- a/src/plugin.c +++ b/src/plugin.c @@ -65,6 +65,13 @@ struct write_func_s }; typedef struct write_func_s write_func_t; +struct flush_func_s +{ + plugin_flush_cb callback; + user_data_t udata; +}; +typedef struct flush_func_s flush_func_t; + /* * Private variables */ @@ -547,34 +554,55 @@ int plugin_register_complex_read (const char *name, int plugin_register_write (const char *name, plugin_write_cb callback, user_data_t *user_data) { - write_func_t *wr; + write_func_t *wf; - wr = (write_func_t *) malloc (sizeof (*wr)); - if (wr == NULL) + wf = (write_func_t *) malloc (sizeof (*wf)); + if (wf == NULL) { ERROR ("plugin_register_write: malloc failed."); return (-1); } - memset (wr, 0, sizeof (*wr)); + memset (wf, 0, sizeof (*wf)); - wr->callback = callback; + wf->callback = callback; if (user_data == NULL) { - wr->udata.data = NULL; - wr->udata.free_func = NULL; + wf->udata.data = NULL; + wf->udata.free_func = NULL; } else { - wr->udata = *user_data; + wf->udata = *user_data; } - return (register_callback (&list_write, name, (void *) wr)); + return (register_callback (&list_write, name, (void *) wf)); } /* int plugin_register_write */ int plugin_register_flush (const char *name, - int (*callback) (const int timeout, const char *identifier)) + plugin_flush_cb callback, user_data_t *user_data) { - return (register_callback (&list_flush, name, (void *) callback)); + flush_func_t *ff; + + ff = (flush_func_t *) malloc (sizeof (*ff)); + if (ff == NULL) + { + ERROR ("plugin_register_flush: malloc failed."); + return (-1); + } + memset (ff, 0, sizeof (*ff)); + + ff->callback = callback; + if (user_data == NULL) + { + ff->udata.data = NULL; + ff->udata.free_func = NULL; + } + else + { + ff->udata = *user_data; + } + + return (register_callback (&list_flush, name, (void *) ff)); } /* int plugin_register_flush */ int plugin_register_shutdown (char *name, @@ -695,7 +723,24 @@ int plugin_unregister_write (const char *name) int plugin_unregister_flush (const char *name) { - return (plugin_unregister (list_flush, name)); + llentry_t *e; + flush_func_t *ff; + + e = llist_search (list_flush, name); + + if (e == NULL) + return (-1); + + llist_remove (list_flush, e); + + ff = (flush_func_t *) e->value; + plugin_user_data_destroy (&ff->udata); + free (ff); + free (e->key); + + llentry_destroy (e); + + return (0); } int plugin_unregister_shutdown (const char *name) @@ -939,7 +984,6 @@ int plugin_write (const char *plugin, /* {{{ */ int plugin_flush (const char *plugin, int timeout, const char *identifier) { - int (*callback) (int timeout, const char *identifier); llentry_t *le; if (list_flush == NULL) @@ -948,6 +992,8 @@ int plugin_flush (const char *plugin, int timeout, const char *identifier) le = llist_head (list_flush); while (le != NULL) { + flush_func_t *ff; + if ((plugin != NULL) && (strcmp (plugin, le->key) != 0)) { @@ -955,8 +1001,9 @@ int plugin_flush (const char *plugin, int timeout, const char *identifier) continue; } - callback = (int (*) (int, const char *)) le->value; - (*callback) (timeout, identifier); + ff = (flush_func_t *) le->value; + + ff->callback (timeout, identifier, &ff->udata); le = le->next; } diff --git a/src/plugin.h b/src/plugin.h index e58444ff..ed9fdab7 100644 --- a/src/plugin.h +++ b/src/plugin.h @@ -149,6 +149,8 @@ typedef struct user_data_s user_data_t; typedef int (*plugin_read_cb) (user_data_t *); typedef int (*plugin_write_cb) (const data_set_t *, const value_list_t *, user_data_t *); +typedef int (*plugin_flush_cb) (int timeout, const char *identifier, + user_data_t *); /* * NAME @@ -244,7 +246,7 @@ int plugin_register_complex_read (const char *name, int plugin_register_write (const char *name, plugin_write_cb callback, user_data_t *user_data); int plugin_register_flush (const char *name, - int (*callback) (const int timeout, const char *identifier)); + plugin_flush_cb callback, user_data_t *user_data); int plugin_register_shutdown (char *name, int (*callback) (void)); int plugin_register_data_set (const data_set_t *ds); diff --git a/src/rrdtool.c b/src/rrdtool.c index 698f89fd..6ddbfc9e 100644 --- a/src/rrdtool.c +++ b/src/rrdtool.c @@ -795,7 +795,8 @@ static int rrd_write (const data_set_t *ds, const value_list_t *vl, return (status); } /* int rrd_write */ -static int rrd_flush (int timeout, const char *identifier) +static int rrd_flush (int timeout, const char *identifier, + user_data_t __attribute__((unused)) *user_data) { pthread_mutex_lock (&cache_lock); @@ -1048,6 +1049,6 @@ void module_register (void) config_keys, config_keys_num); plugin_register_init ("rrdtool", rrd_init); plugin_register_write ("rrdtool", rrd_write, /* user_data = */ NULL); - plugin_register_flush ("rrdtool", rrd_flush); + plugin_register_flush ("rrdtool", rrd_flush, /* user_data = */ NULL); plugin_register_shutdown ("rrdtool", rrd_shutdown); } -- 2.11.0