From 9769c37cbb0e2bcf56c6e5ad8a065e5d166781f9 Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Mon, 4 Dec 2006 11:30:29 +0100 Subject: [PATCH] plugin.[ch]: Added support for an optional plugin shutdown function. A plugin may register a shutdown function using plugin_register_shutdown (). This function is called when collectd terminates either during normal termination or after SIGINT or SIGTERM have been received. Signed-off-by: Sebastian Harl --- src/collectd.c | 2 ++ src/plugin.c | 30 ++++++++++++++++++++++++++++++ src/plugin.h | 4 ++++ 3 files changed, 36 insertions(+) diff --git a/src/collectd.c b/src/collectd.c index 64da5765..a1e15bf1 100644 --- a/src/collectd.c +++ b/src/collectd.c @@ -444,6 +444,8 @@ int main (int argc, char **argv) #endif start_client (); + plugin_shutdown_all (); + #if COLLECT_DEBUG if (logfile != NULL) DBG_STOPFILE("debug file closed."); diff --git a/src/plugin.c b/src/plugin.c index 697449e2..46a1c617 100644 --- a/src/plugin.c +++ b/src/plugin.c @@ -34,6 +34,7 @@ typedef struct plugin void (*init) (void); void (*read) (void); void (*write) (char *host, char *inst, char *val); + void (*shutdown) (void); struct plugin *next; } plugin_t; @@ -285,6 +286,19 @@ void plugin_read_all (const int *loop) (*p->read) (); } +/* + * Call `shutdown' on all plugins (if given) + */ +void plugin_shutdown_all (void) +{ + plugin_t *p; + + for (p = first_plugin; NULL != p; p = p->next) + if (NULL != p->shutdown) + (*p->shutdown) (); + return; +} + /* * Add plugin to the linked list of registered plugins. */ @@ -317,10 +331,26 @@ void plugin_register (char *type, p->read = read; p->write = write; + p->shutdown = NULL; + p->next = first_plugin; first_plugin = p; } +/* + * Register the shutdown function (optional). + */ +int plugin_register_shutdown (char *type, void (*shutdown) (void)) +{ + plugin_t *p = plugin_search (type); + + if (NULL == p) + return -1; + + p->shutdown = shutdown; + return 0; +} + /* * Send received data back to the plugin/module which will append DS * definitions and pass it on to ``rrd_update_file''. diff --git a/src/plugin.h b/src/plugin.h index a77c87fb..45e9b494 100644 --- a/src/plugin.h +++ b/src/plugin.h @@ -101,11 +101,15 @@ int plugin_load_all (char *dir); void plugin_init_all (void); void plugin_read_all (const int *loop); +void plugin_shutdown_all (void); + void plugin_register (char *type, void (*init) (void), void (*read) (void), void (*write) (char *, char *, char *)); +int plugin_register_shutdown (char *, void (*) (void)); + /* * NAME * plugin_write -- 2.30.2