From: Sebastian Harl Date: Mon, 4 Dec 2006 10:30:29 +0000 (+0100) Subject: plugin.[ch]: Added support for an optional plugin shutdown function. X-Git-Tag: collectd-3.11.0~32 X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=9769c37cbb0e2bcf56c6e5ad8a065e5d166781f9;p=collectd.git 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 --- 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; @@ -286,6 +287,19 @@ void plugin_read_all (const int *loop) } /* + * 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. */ void plugin_register (char *type, @@ -317,11 +331,27 @@ 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