src/plugin.c: Change the write callbacks to receive a user_data_t pointer.
[collectd.git] / src / plugin.c
index 9d558cb..9bcea3c 100644 (file)
 /*
  * Private structures
  */
+#define RF_SIMPLE  0
+#define RF_COMPLEX 1
 struct read_func_s
 {
        int wait_time;
        int wait_left;
-       int (*callback) (void);
+       int type;
+       union
+       {
+               int (*cb_simple) (void);
+               plugin_read_cb cb_complex;
+       } callback;
        enum { DONE = 0, TODO = 1, ACTIVE = 2 } needs_read;
+       user_data_t udata;
 };
 typedef struct read_func_s read_func_t;
 
+struct write_func_s
+{
+       plugin_write_cb callback;
+       user_data_t udata;
+};
+typedef struct write_func_s write_func_t;
+
 /*
  * Private variables
  */
@@ -118,6 +133,16 @@ static int register_callback (llist_t **list, const char *name, void *callback)
        return (0);
 } /* int register_callback */
 
+static void plugin_user_data_destroy (user_data_t *ud)
+{
+       if ((ud != NULL) && (ud->data != NULL) && (ud->free_func != NULL))
+       {
+               ud->free_func (ud->data);
+               ud->data = NULL;
+               ud->free_func = NULL;
+       }
+} /* void plugin_user_data_destroy */
+
 static int plugin_unregister (llist_t *list, const char *name)
 {
        llentry_t *e;
@@ -202,7 +227,15 @@ static void *plugin_read_thread (void __attribute__((unused)) *args)
                                        (unsigned long int) pthread_self (), le->key);
                        pthread_mutex_unlock (&read_lock);
 
-                       status = rf->callback ();
+                       if (rf->type == RF_SIMPLE)
+                       {
+                               status = rf->callback.cb_simple ();
+                       }
+                       else
+                       {
+                               assert (rf->type == RF_COMPLEX);
+                               status = rf->callback.cb_complex (&rf->udata);
+                       }
                        done++;
 
                        if (status != 0)
@@ -328,6 +361,7 @@ int plugin_load (const char *type)
        int   ret;
        struct stat    statbuf;
        struct dirent *de;
+       int status;
 
        DEBUG ("type = %s", type);
 
@@ -336,8 +370,8 @@ int plugin_load (const char *type)
 
        /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
         * type when matching the filename */
-       if (ssnprintf (typename, sizeof (typename),
-                       "%s.so", type) >= sizeof (typename))
+       status = ssnprintf (typename, sizeof (typename), "%s.so", type);
+       if ((status < 0) || ((size_t) status >= sizeof (typename)))
        {
                WARNING ("snprintf: truncated: `%s.so'", type);
                return (-1);
@@ -357,8 +391,9 @@ int plugin_load (const char *type)
                if (strncasecmp (de->d_name, typename, typename_len))
                        continue;
 
-               if (ssnprintf (filename, sizeof (filename),
-                               "%s/%s", dir, de->d_name) >= sizeof (filename))
+               status = ssnprintf (filename, sizeof (filename),
+                               "%s/%s", dir, de->d_name);
+               if ((status < 0) || ((size_t) status >= sizeof (filename)))
                {
                        WARNING ("snprintf: truncated: `%s/%s'", dir, de->d_name);
                        continue;
@@ -434,19 +469,76 @@ int plugin_register_read (const char *name,
                return (-1);
        }
 
-       memset (rf, '\0', sizeof (read_func_t));
+       memset (rf, 0, sizeof (read_func_t));
        rf->wait_time = interval_g;
        rf->wait_left = 0;
-       rf->callback = callback;
+       rf->type = RF_SIMPLE;
+       rf->callback.cb_simple = callback;
        rf->needs_read = DONE;
+       rf->udata.data = NULL;
+       rf->udata.free_func = NULL;
 
        return (register_callback (&list_read, name, (void *) rf));
 } /* int plugin_register_read */
 
+int plugin_register_complex_read (const char *name,
+               plugin_read_cb callback, user_data_t *user_data)
+{
+       read_func_t *rf;
+
+       rf = (read_func_t *) malloc (sizeof (read_func_t));
+       if (rf == NULL)
+       {
+               ERROR ("plugin_register_complex_read: malloc failed.");
+               return (-1);
+       }
+
+       memset (rf, 0, sizeof (read_func_t));
+       rf->wait_time = interval_g;
+       rf->wait_left = 0;
+       rf->type = RF_COMPLEX;
+       rf->callback.cb_complex = callback;
+       rf->needs_read = DONE;
+
+       /* Set user data */
+       if (user_data == NULL)
+       {
+               rf->udata.data = NULL;
+               rf->udata.free_func = NULL;
+       }
+       else
+       {
+               rf->udata = *user_data;
+       }
+
+       return (register_callback (&list_read, name, (void *) rf));
+} /* int plugin_register_complex_read */
+
 int plugin_register_write (const char *name,
-               int (*callback) (const data_set_t *ds, const value_list_t *vl))
+               plugin_write_cb callback, user_data_t *user_data)
 {
-       return (register_callback (&list_write, name, (void *) callback));
+       write_func_t *wr;
+
+       wr = (write_func_t *) malloc (sizeof (*wr));
+       if (wr == NULL)
+       {
+               ERROR ("plugin_register_write: malloc failed.");
+               return (-1);
+       }
+       memset (wr, 0, sizeof (*wr));
+
+       wr->callback = callback;
+       if (user_data == NULL)
+       {
+               wr->udata.data = NULL;
+               wr->udata.free_func = NULL;
+       }
+       else
+       {
+               wr->udata = *user_data;
+       }
+
+       return (register_callback (&list_write, name, (void *) wr));
 } /* int plugin_register_write */
 
 int plugin_register_flush (const char *name,
@@ -530,6 +622,7 @@ int plugin_unregister_init (const char *name)
 int plugin_unregister_read (const char *name)
 {
        llentry_t *e;
+       read_func_t *rf;
 
        e = llist_search (list_read, name);
 
@@ -537,8 +630,12 @@ int plugin_unregister_read (const char *name)
                return (-1);
 
        llist_remove (list_read, e);
-       free (e->value);
+
+       rf = (read_func_t *) e->value;
+       plugin_user_data_destroy (&rf->udata);
+       free (rf);
        free (e->key);
+
        llentry_destroy (e);
 
        return (0);
@@ -546,7 +643,24 @@ int plugin_unregister_read (const char *name)
 
 int plugin_unregister_write (const char *name)
 {
-       return (plugin_unregister (list_write, name));
+       llentry_t *e;
+       write_func_t *wf;
+
+       e = llist_search (list_write, name);
+
+       if (e == NULL)
+               return (-1);
+
+       llist_remove (list_write, e);
+
+       wf = (write_func_t *) e->value;
+       plugin_user_data_destroy (&wf->udata);
+       free (wf);
+       free (e->key);
+
+       llentry_destroy (e);
+
+       return (0);
 }
 
 int plugin_unregister_flush (const char *name)
@@ -700,7 +814,17 @@ int plugin_read_all_once (void)
             le = le->next)
        {
                rf = (read_func_t *) le->value;
-               status = rf->callback ();
+
+               if (rf->type == RF_SIMPLE)
+               {
+                       status = rf->callback.cb_simple ();
+               }
+               else
+               {
+                       assert (rf->type == RF_COMPLEX);
+                       status = rf->callback.cb_complex (&rf->udata);
+               }
+
                if (status != 0)
                {
                        NOTICE ("read-function of plugin `%s' failed.",
@@ -715,7 +839,6 @@ int plugin_read_all_once (void)
 int plugin_write (const char *plugin, /* {{{ */
                const data_set_t *ds, const value_list_t *vl)
 {
-  int (*callback) (const data_set_t *ds, const value_list_t *vl);
   llentry_t *le;
   int status;
 
@@ -743,8 +866,10 @@ int plugin_write (const char *plugin, /* {{{ */
     le = llist_head (list_write);
     while (le != NULL)
     {
-      callback = le->value;
-      status = (*callback) (ds, vl);
+      write_func_t *wf = le->value;
+
+      DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
+      status = wf->callback (ds, vl, &wf->udata);
       if (status != 0)
         failure++;
       else
@@ -760,6 +885,7 @@ int plugin_write (const char *plugin, /* {{{ */
   }
   else /* plugin != NULL */
   {
+    write_func_t *wf;
     le = llist_head (list_write);
     while (le != NULL)
     {
@@ -772,8 +898,10 @@ int plugin_write (const char *plugin, /* {{{ */
     if (le == NULL)
       return (ENOENT);
 
-    callback = le->value;
-    status = (*callback) (ds, vl);
+    wf = le->value;
+
+    DEBUG ("plugin: plugin_write: Writing values via %s.", le->key);
+    status = wf->callback (ds, vl, &wf->udata);
   }
 
   return (status);
@@ -1152,7 +1280,7 @@ int plugin_notification_meta_copy (notification_t *dst,
   return (0);
 } /* int plugin_notification_meta_copy */
 
-int plugin_notification_meta_free (notification_t *n)
+int plugin_notification_meta_free (notification_meta_t *n)
 {
   notification_meta_t *this;
   notification_meta_t *next;
@@ -1163,8 +1291,7 @@ int plugin_notification_meta_free (notification_t *n)
     return (-1);
   }
 
-  this = n->meta;
-  n->meta = NULL;
+  this = n;
   while (this != NULL)
   {
     next = this->next;