From: Sebastian Harl Date: Sun, 8 Aug 2010 19:49:11 +0000 (+0200) Subject: collectd-flush: Moved connection handling out of ‘flush()’. X-Git-Tag: collectd-5.0.0-beta0~66 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=677cdefc79b8317c0013521f7f9e3eff7af7c282;p=collectd.git collectd-flush: Moved connection handling out of ‘flush()’. --- diff --git a/src/collectd-flush.c b/src/collectd-flush.c index 27bb3ca2..b82ec727 100644 --- a/src/collectd-flush.c +++ b/src/collectd-flush.c @@ -94,10 +94,8 @@ static int count_chars (const char *str, char chr) { return count; } /* count_chars */ -static int flush (const char *address, int argc, char **argv) +static int flush (lcc_connection_t *c, int argc, char **argv) { - lcc_connection_t *connection; - lcc_identifier_t ident; lcc_identifier_t *identp = NULL; @@ -109,14 +107,6 @@ static int flush (const char *address, int argc, char **argv) assert (strcasecmp (argv[0], "flush") == 0); - connection = NULL; - status = lcc_connect (address, &connection); - if (status != 0) { - fprintf (stderr, "ERROR: Failed to connect to daemon at %s: %s.\n", - address, strerror (errno)); - return (1); - } - for (i = 1; i < argc; ++i) { char *key, *value; @@ -174,33 +164,31 @@ static int flush (const char *address, int argc, char **argv) ident_str[sizeof (ident_str) - 1] = '\0'; } - status = lcc_string_to_identifier (connection, &ident, ident_str); + status = lcc_string_to_identifier (c, &ident, ident_str); if (status != 0) { fprintf (stderr, "ERROR: Failed to parse identifier ``%s'': %s.\n", - ident_str, lcc_strerror(connection)); - LCC_DESTROY (connection); + ident_str, lcc_strerror(c)); return (-1); } identp = &ident; } } - status = lcc_flush (connection, plugin, identp, timeout); + status = lcc_flush (c, plugin, identp, timeout); if (status != 0) { fprintf (stderr, "ERROR: Flushing failed: %s.\n", - lcc_strerror (connection)); - LCC_DESTROY (connection); + lcc_strerror (c)); return (-1); } - LCC_DESTROY (connection); - return 0; } /* flush */ int main (int argc, char **argv) { char address[1024] = "unix:"DEFAULT_SOCK; + lcc_connection_t *c; + int status; while (42) { @@ -229,13 +217,23 @@ int main (int argc, char **argv) { exit_usage (argv[0], 1); } + c = NULL; + status = lcc_connect (address, &c); + if (status != 0) { + fprintf (stderr, "ERROR: Failed to connect to daemon at %s: %s.\n", + address, strerror (errno)); + return (1); + } + if (strcasecmp (argv[optind], "flush") == 0) - status = flush (address, argc - optind, argv + optind); + status = flush (c, argc - optind, argv + optind); else { fprintf (stderr, "%s: invalid command: %s\n", argv[0], argv[optind]); return (1); } + LCC_DESTROY (c); + if (status != 0) return (status); return (0);