From 677cdefc79b8317c0013521f7f9e3eff7af7c282 Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Sun, 8 Aug 2010 21:49:11 +0200 Subject: [PATCH] =?utf8?q?collectd-flush:=20Moved=20connection=20handling?= =?utf8?q?=20out=20of=20=E2=80=98flush()=E2=80=99.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- src/collectd-flush.c | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) 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); -- 2.11.0