Many build fixes that turned up with GCC 4.6.
[collectd.git] / src / utils_cmd_flush.c
index c214d07..0e7b350 100644 (file)
 #include "collectd.h"
 #include "common.h"
 #include "plugin.h"
+#include "utils_parse_option.h"
+
+#define print_to_socket(fh, ...) \
+       if (fprintf (fh, __VA_ARGS__) < 0) { \
+               char errbuf[1024]; \
+               WARNING ("handle_flush: failed to write to socket #%i: %s", \
+                               fileno (fh), sstrerror (errno, errbuf, sizeof (errbuf))); \
+               return -1; \
+       }
 
-struct flush_info_s
-{
-       char **plugins;
-       int plugins_num;
-       int timeout;
-};
-typedef struct flush_info_s flush_info_t;
-
-static int parse_option_plugin (flush_info_t *fi, const char *option)
+static int add_to_array (char ***array, int *array_num, char *value)
 {
        char **temp;
 
-       temp = (char **) realloc (fi->plugins,
-                       (fi->plugins_num + 1) * sizeof (char *));
+       temp = (char **) realloc (*array, sizeof (char *) * (*array_num + 1));
        if (temp == NULL)
-       {
-               ERROR ("utils_cmd_flush: parse_option_plugin: realloc failed.");
                return (-1);
-       }
-       fi->plugins = temp;
 
-       fi->plugins[fi->plugins_num] = strdup (option + strlen ("plugin="));
-       if (fi->plugins[fi->plugins_num] == NULL)
-       {
-               /* fi->plugins is freed in handle_flush in this case */
-               ERROR ("utils_cmd_flush: parse_option_plugin: strdup failed.");
-               return (-1);
-       }
-       fi->plugins_num++;
+       *array = temp;
+       (*array)[*array_num] = value;
+       (*array_num)++;
 
        return (0);
-} /* int parse_option_plugin */
+} /* int add_to_array */
 
-static int parse_option_timeout (flush_info_t *fi, const char *option)
+int handle_flush (FILE *fh, char *buffer)
 {
-       const char *value_ptr = option + strlen ("timeout=");
-       char *endptr = NULL;
-       int timeout;
-
-       timeout = strtol (value_ptr, &endptr, 0);
-       if (value_ptr == endptr)
-               return (-1);
+       int success = 0;
+       int error   = 0;
 
-       fi->timeout = (timeout <= 0) ? (-1) : timeout;
+       int timeout = -1;
+       char **plugins = NULL;
+       int plugins_num = 0;
+       char **identifiers = NULL;
+       int identifiers_num = 0;
 
-       return (0);
-} /* int parse_option_timeout */
+       int i;
 
-static int parse_option (flush_info_t *fi, const char *option)
-{
-       if (strncasecmp ("plugin=", option, strlen ("plugin=")) == 0)
-               return (parse_option_plugin (fi, option));
-       else if (strncasecmp ("timeout=", option, strlen ("timeout=")) == 0)
-               return (parse_option_timeout (fi, option));
-       else
+       if ((fh == NULL) || (buffer == NULL))
                return (-1);
-} /* int parse_option */
 
-int handle_flush (FILE *fh, char **fields, int fields_num)
-{
-       flush_info_t fi;
-       int status;
-       int i;
+       DEBUG ("utils_cmd_flush: handle_flush (fh = %p, buffer = %s);",
+                       (void *) fh, buffer);
 
-       memset (&fi, '\0', sizeof (fi));
-       fi.timeout = -1;
+       if (strncasecmp ("FLUSH", buffer, strlen ("FLUSH")) != 0)
+       {
+               print_to_socket (fh, "-1 Cannot parse command.\n");
+               return (-1);
+       }
+       buffer += strlen ("FLUSH");
 
-       for (i = 1; i < fields_num; i++)
+       while (*buffer != 0)
        {
-               status = parse_option (&fi, fields[i]);
+               char *opt_key;
+               char *opt_value;
+               int status;
+
+               opt_key = NULL;
+               opt_value = NULL;
+               status = parse_option (&buffer, &opt_key, &opt_value);
                if (status != 0)
                {
-                       fprintf (fh, "-1 Cannot parse option %s\n", fields[i]);
-                       fflush (fh);
+                       print_to_socket (fh, "-1 Parsing options failed.\n");
+                       sfree (plugins);
+                       sfree (identifiers);
                        return (-1);
                }
-       }
 
-       if (fi.plugins_num > 0)
+               if (strcasecmp ("plugin", opt_key) == 0)
+               {
+                       add_to_array (&plugins, &plugins_num, opt_value);
+               }
+               else if (strcasecmp ("identifier", opt_key) == 0)
+               {
+                       add_to_array (&identifiers, &identifiers_num, opt_value);
+               }
+               else if (strcasecmp ("timeout", opt_key) == 0)
+               {
+                       char *endptr;
+                       
+                       errno = 0;
+                       endptr = NULL;
+                       timeout = strtol (opt_value, &endptr, 0);
+
+                       if ((endptr == opt_value) || (errno != 0))
+                       {
+                               print_to_socket (fh, "-1 Invalid value for option `timeout': "
+                                               "%s\n", opt_value);
+                               sfree (plugins);
+                               sfree (identifiers);
+                               return (-1);
+                       }
+                       else if (timeout <= 0)
+                               timeout = -1;
+               }
+               else
+               {
+                       print_to_socket (fh, "-1 Cannot parse option %s\n", opt_key);
+                       sfree (plugins);
+                       sfree (identifiers);
+                       return (-1);
+               }
+       } /* while (*buffer != 0) */
+
+       /* Add NULL entries for `any plugin' and/or `any value' if nothing was
+        * specified. */
+       if (plugins_num == 0)
+               add_to_array (&plugins, &plugins_num, NULL);
+
+       if (identifiers_num == 0)
+               add_to_array (&identifiers, &identifiers_num, NULL);
+
+       for (i = 0; i < plugins_num; i++)
        {
-               int success = 0;
-               for (i = 0; i < fi.plugins_num; i++)
+               char *plugin;
+               int j;
+
+               plugin = plugins[i];
+
+               for (j = 0; j < identifiers_num; j++)
                {
-                       status = plugin_flush_one (fi.timeout, fi.plugins[i]);
+                       char *identifier;
+                       int status;
+
+                       identifier = identifiers[j];
+                       status = plugin_flush (plugin, timeout, identifier);
                        if (status == 0)
                                success++;
+                       else
+                               error++;
                }
-               fprintf (fh, "0 Done: %i successful, %i errors\n",
-                               success, fi.plugins_num - success);
        }
-       else
+
+       if ((success + error) > 0)
        {
-               plugin_flush_all (fi.timeout);
-               fprintf (fh, "0 Done\n");
+               print_to_socket (fh, "0 Done: %i successful, %i errors\n",
+                               success, error);
        }
-       fflush (fh);
-
-       for (i = 0; i < fi.plugins_num; i++)
+       else
        {
-               sfree (fi.plugins[i]);
+               plugin_flush (NULL, timeout, NULL);
+               print_to_socket (fh, "0 Done\n");
        }
-       sfree (fi.plugins);
 
+       sfree (plugins);
+       sfree (identifiers);
        return (0);
 } /* int handle_flush */