X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Futils_cmd_flush.c;h=6fa8b7bf00b5d0b68979b4e828e1f7093e32f5dc;hb=a019b6c8144745db63c599680bd693ac02f11666;hp=6832493c0a0d9b8e2df0d59628eda2b7fae8528d;hpb=a813e8af8379bd0f55a2b46fc7918fa31de9522e;p=collectd.git diff --git a/src/utils_cmd_flush.c b/src/utils_cmd_flush.c index 6832493c..6fa8b7bf 100644 --- a/src/utils_cmd_flush.c +++ b/src/utils_cmd_flush.c @@ -25,108 +25,70 @@ #include "common.h" #include "plugin.h" -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) -{ - char **temp; - - temp = (char **) realloc (fi->plugins, - (fi->plugins_num + 1) * sizeof (char *)); - 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); +#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; \ } - fi->plugins_num++; - return (0); -} /* int parse_option_plugin */ - -static int parse_option_timeout (flush_info_t *fi, const char *option) +int handle_flush (FILE *fh, char **fields, int fields_num) { - const char *value_ptr = option + strlen ("timeout="); - char *endptr = NULL; - int timeout; + int success = 0; + int error = 0; - timeout = strtol (value_ptr, &endptr, 0); - if (value_ptr == endptr) - return (-1); + int timeout = -1; - fi->timeout = (timeout <= 0) ? (-1) : timeout; + int i; - return (0); -} /* int parse_option_timeout */ + for (i = 1; i < fields_num; i++) + { + char *option = fields[i]; + int status = 0; -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 - return (-1); -} /* int parse_option */ + if (strncasecmp ("plugin=", option, strlen ("plugin=")) == 0) + { + char *plugin = option + strlen ("plugin="); -int handle_flush (FILE *fh, char **fields, int fields_num) -{ - flush_info_t fi; - int status; - int i; + if (0 == plugin_flush_one (timeout, plugin)) + ++success; + else + ++error; + } + else if (strncasecmp ("timeout=", option, strlen ("timeout=")) == 0) + { + char *endptr = NULL; + char *value = option + strlen ("timeout="); - memset (&fi, '\0', sizeof (fi)); - fi.timeout = -1; + errno = 0; + timeout = strtol (value, &endptr, 0); + + if ((endptr == value) || (0 != errno)) + status = -1; + else if (0 >= timeout) + timeout = -1; + } + else + status = -1; - for (i = 1; i < fields_num; i++) - { - status = parse_option (&fi, fields[i]); if (status != 0) { - fprintf (fh, "-1 Cannot parse option %s\n", fields[i]); - fflush (fh); + print_to_socket (fh, "-1 Cannot parse option %s\n", option); return (-1); } } - if (fi.plugins_num > 0) + if ((success + error) > 0) { - int success = 0; - for (i = 0; i < fi.plugins_num; i++) - { - status = plugin_flush_one (fi.timeout, fi.plugins[i]); - if (status == 0) - success++; - } - fprintf (fh, "0 Done: %i successful, %i errors\n", - success, fi.plugins_num - success); + print_to_socket (fh, "0 Done: %i successful, %i errors\n", + success, error); } else { - plugin_flush_all (fi.timeout); - fprintf (fh, "0 Done"); - } - fflush (fh); - - for (i = 0; i < fi.plugins_num; i++) - { - sfree (fi.plugins[i]); + plugin_flush_all (timeout); + print_to_socket (fh, "0 Done\n"); } - sfree (fi.plugins); return (0); } /* int handle_flush */