X-Git-Url: https://git.octo.it/?a=blobdiff_plain;ds=sidebyside;f=src%2Fplugin.c;h=da662040cecedc916b5709fefdeae6b8ae6a5b49;hb=1fdb04e6cb573ad06b914b9371f21ffb255b1d13;hp=7aef8737ebc52ae6f6b1ef5390e2a9ceed352f4b;hpb=69106ccf4f82928b886984fd7e7649c0d2964be7;p=collectd.git diff --git a/src/plugin.c b/src/plugin.c index 7aef8737..da662040 100644 --- a/src/plugin.c +++ b/src/plugin.c @@ -118,10 +118,10 @@ static int plugin_unregister (llist_t *list, const char *name) * object, but it will bitch about a shared object not having a * ``module_register'' symbol.. */ -static int plugin_load_file (char *file, modreg_e load) +static int plugin_load_file (char *file) { lt_dlhandle dlh; - void (*reg_handle) (modreg_e mr); + void (*reg_handle) (void); DEBUG ("file = %s", file); @@ -137,7 +137,7 @@ static int plugin_load_file (char *file, modreg_e load) return (1); } - if ((reg_handle = (void (*) (modreg_e)) lt_dlsym (dlh, "module_register")) == NULL) + if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL) { WARNING ("Couldn't find symbol ``module_register'' in ``%s'': %s\n", file, lt_dlerror ()); @@ -145,7 +145,7 @@ static int plugin_load_file (char *file, modreg_e load) return (-1); } - (*reg_handle) (load); + (*reg_handle) (); return (0); } @@ -186,6 +186,8 @@ static void *plugin_read_thread (void *args) if (status != 0) { + if (rf->wait_time < interval_g) + rf->wait_time = interval_g; rf->wait_left = rf->wait_time; rf->wait_time = rf->wait_time * 2; if (rf->wait_time > 86400) @@ -266,7 +268,7 @@ static void stop_threads (void) { ERROR ("plugin: stop_threads: pthread_join failed."); } - read_threads[i] = -1; + read_threads[i] = (pthread_t) 0; } sfree (read_threads); read_threads_num = 0; @@ -291,7 +293,7 @@ void plugin_set_dir (const char *dir) } #define BUFSIZE 512 -int plugin_load (const char *type, modreg_e mr) +int plugin_load (const char *type) { DIR *dh; const char *dir; @@ -348,7 +350,7 @@ int plugin_load (const char *type, modreg_e mr) continue; } - if (plugin_load_file (filename, mr) == 0) + if (plugin_load_file (filename) == 0) { /* success */ ret = 0; @@ -415,7 +417,33 @@ int plugin_register_shutdown (char *name, int plugin_register_data_set (const data_set_t *ds) { - return (register_callback (&list_data_set, ds->type, (void *) ds)); + data_set_t *ds_copy; + int i; + + if ((list_data_set != NULL) + && (llist_search (list_data_set, ds->type) != NULL)) + { + NOTICE ("Replacing DS `%s' with another version.", ds->type); + plugin_unregister_data_set (ds->type); + } + + ds_copy = (data_set_t *) malloc (sizeof (data_set_t)); + if (ds_copy == NULL) + return (-1); + memcpy(ds_copy, ds, sizeof (data_set_t)); + + ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t) + * ds->ds_num); + if (ds_copy->ds == NULL) + { + free (ds_copy); + return (-1); + } + + for (i = 0; i < ds->ds_num; i++) + memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t)); + + return (register_callback (&list_data_set, ds->type, (void *) ds_copy)); } /* int plugin_register_data_set */ int plugin_register_log (char *name, @@ -437,7 +465,6 @@ int plugin_unregister_init (const char *name) int plugin_unregister_read (const char *name) { - return (plugin_unregister (list_read, name)); llentry_t *e; e = llist_search (list_read, name); @@ -464,8 +491,26 @@ int plugin_unregister_shutdown (const char *name) int plugin_unregister_data_set (const char *name) { - return (plugin_unregister (list_data_set, name)); -} + llentry_t *e; + data_set_t *ds; + + if (list_data_set == NULL) + return (-1); + + e = llist_search (list_data_set, name); + + if (e == NULL) + return (-1); + + llist_remove (list_data_set, e); + ds = (data_set_t *) e->value; + llentry_destroy (e); + + sfree (ds->ds); + sfree (ds); + + return (0); +} /* int plugin_unregister_data_set */ int plugin_unregister_log (const char *name) { @@ -494,15 +539,16 @@ void plugin_init_all (void) le = llist_head (list_init); while (le != NULL) { - callback = le->value; + callback = (int (*) (void)) le->value; status = (*callback) (); if (status != 0) { ERROR ("Initialization of plugin `%s' " "failed with status %i. " - "Plugin will be unloaded. TODO!", + "Plugin will be unloaded.", le->key, status); + /* FIXME: Unload _all_ functions */ plugin_unregister_read (le->key); } @@ -560,20 +606,25 @@ void plugin_shutdown_all (void) le = llist_head (list_shutdown); while (le != NULL) { - callback = le->value; - (*callback) (); + callback = (int (*) (void)) le->value; + /* Advance the pointer before calling the callback allows + * shutdown functions to unregister themselves. If done the + * other way around the memory `le' points to will be freed + * after callback returns. */ le = le->next; + + (*callback) (); } } /* void plugin_shutdown_all */ -int plugin_dispatch_values (const char *name, const value_list_t *vl) +int plugin_dispatch_values (const char *name, value_list_t *vl) { int (*callback) (const data_set_t *, const value_list_t *); data_set_t *ds; llentry_t *le; - if (list_write == NULL) + if ((list_write == NULL) || (list_data_set == NULL)) return (-1); le = llist_search (list_data_set, name); @@ -592,17 +643,34 @@ int plugin_dispatch_values (const char *name, const value_list_t *vl) vl->plugin, vl->plugin_instance, ds->type, vl->type_instance); +#if COLLECT_DEBUG + assert (ds->ds_num == vl->values_len); +#else + if (ds->ds_num != vl->values_len) + { + ERROR ("plugin: ds->type = %s: (ds->ds_num = %i) != " + "(vl->values_len = %i)", + ds->type, ds->ds_num, vl->values_len); + return (-1); + } +#endif + + escape_slashes (vl->host, sizeof (vl->host)); + escape_slashes (vl->plugin, sizeof (vl->plugin)); + escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance)); + escape_slashes (vl->type_instance, sizeof (vl->type_instance)); + le = llist_head (list_write); while (le != NULL) { - callback = le->value; + callback = (int (*) (const data_set_t *, const value_list_t *)) le->value; (*callback) (ds, vl); le = le->next; } return (0); -} +} /* int plugin_dispatch_values */ void plugin_log (int level, const char *format, ...) { @@ -628,7 +696,7 @@ void plugin_log (int level, const char *format, ...) le = llist_head (list_log); while (le != NULL) { - callback = le->value; + callback = (void (*) (int, const char *)) le->value; (*callback) (level, msg); le = le->next;