From aec20e4438a754fe2df160e01adea9f1285fe8af Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Wed, 27 Aug 2008 11:59:53 +0200 Subject: [PATCH] src/utils_cmd_getval.[ch]: Fix handling of identifiers with spaces. The getval handler now uses the `parse_string' function in `utils_parse_option' to handle quoted strings correctly. --- bindings/perl/Collectd/Unixsock.pm | 10 +++++-- src/unixsock.c | 2 +- src/utils_cmd_getval.c | 46 +++++++++++++++++++++--------- src/utils_cmd_getval.h | 2 +- 4 files changed, 42 insertions(+), 18 deletions(-) diff --git a/bindings/perl/Collectd/Unixsock.pm b/bindings/perl/Collectd/Unixsock.pm index cd910ed0..d4499246 100644 --- a/bindings/perl/Collectd/Unixsock.pm +++ b/bindings/perl/Collectd/Unixsock.pm @@ -175,13 +175,19 @@ sub getval my %args = @_; my $status; - my $fh = $obj->{'sock'} or confess; + my $fh = $obj->{'sock'} or confess ('object has no filehandle'); my $msg; my $identifier; my $ret = {}; $identifier = _create_identifier (\%args) or return; + if ($identifier =~ m/[\s"]/) + { + $identifier =~ s#\\#\\\\#g; + $identifier =~ s#"#\\"#g; + $identifier = "\"$identifier\""; + } $msg = "GETVAL $identifier\n"; #print "-> $msg"; @@ -477,7 +483,7 @@ sub flush { return; } - if ($ident_str =~ m/ /) + if ($ident_str =~ m/[\s"]/) { $ident_str =~ s#\\#\\\\#g; $ident_str =~ s#"#\\"#g; diff --git a/src/unixsock.c b/src/unixsock.c index c2e1f302..db758094 100644 --- a/src/unixsock.c +++ b/src/unixsock.c @@ -239,7 +239,7 @@ static void *us_handle_client (void *arg) if (strcasecmp (fields[0], "getval") == 0) { - handle_getval (fhout, fields, fields_num); + handle_getval (fhout, buffer); } else if (strcasecmp (fields[0], "putval") == 0) { diff --git a/src/utils_cmd_getval.c b/src/utils_cmd_getval.c index 470d3022..33b40e09 100644 --- a/src/utils_cmd_getval.c +++ b/src/utils_cmd_getval.c @@ -24,6 +24,7 @@ #include "plugin.h" #include "utils_cache.h" +#include "utils_parse_option.h" #define print_to_socket(fh, ...) \ if (fprintf (fh, __VA_ARGS__) < 0) { \ @@ -33,8 +34,12 @@ return -1; \ } -int handle_getval (FILE *fh, char **fields, int fields_num) +int handle_getval (FILE *fh, char *buffer) { + char *command; + char *identifier; + char *identifier_copy; + char *hostname; char *plugin; char *plugin_instance; @@ -43,39 +48,52 @@ int handle_getval (FILE *fh, char **fields, int fields_num) gauge_t *values; size_t values_num; - char *identifier_copy; - const data_set_t *ds; int status; int i; - if (fields_num != 2) + if ((fh == NULL) || (buffer == NULL)) + return (-1); + + DEBUG ("utils_cmd_getval: handle_getval (fh = %p, buffer = %s);", + (void *) fh, buffer); + + command = NULL; + status = parse_string (&buffer, &command); + if (status != 0) { - DEBUG ("unixsock plugin: Wrong number of fields: %i", fields_num); - print_to_socket (fh, "-1 Wrong number of fields: Got %i, expected 2.\n", - fields_num); + print_to_socket (fh, "-1 Cannot parse command.\n"); return (-1); } - DEBUG ("unixsock plugin: Got query for `%s'", fields[1]); + assert (command != NULL); - if (strlen (fields[1]) < strlen ("h/p/t")) + if (strcasecmp ("GETVAL", command) != 0) { - print_to_socket (fh, "-1 Invalied identifier, %s\n", fields[1]); + print_to_socket (fh, "-1 Unexpected command: `%s'.\n", command); return (-1); } + identifier = NULL; + status = parse_string (&buffer, &identifier); + if (status != 0) + { + print_to_socket (fh, "-1 Cannot parse identifier.\n"); + return (-1); + } + assert (identifier != NULL); + /* parse_identifier() modifies its first argument, * returning pointers into it */ - identifier_copy = sstrdup (fields[1]); + identifier_copy = sstrdup (identifier); status = parse_identifier (identifier_copy, &hostname, &plugin, &plugin_instance, &type, &type_instance); if (status != 0) { - DEBUG ("unixsock plugin: Cannot parse `%s'", fields[1]); - print_to_socket (fh, "-1 Cannot parse identifier.\n"); + DEBUG ("unixsock plugin: Cannot parse identifier `%s'.", identifier); + print_to_socket (fh, "-1 Cannot parse identifier `%s'.\n", identifier); sfree (identifier_copy); return (-1); } @@ -91,7 +109,7 @@ int handle_getval (FILE *fh, char **fields, int fields_num) values = NULL; values_num = 0; - status = uc_get_rate_by_name (fields[1], &values, &values_num); + status = uc_get_rate_by_name (identifier, &values, &values_num); if (status != 0) { print_to_socket (fh, "-1 No such value\n"); diff --git a/src/utils_cmd_getval.h b/src/utils_cmd_getval.h index d7bd1151..86134cdf 100644 --- a/src/utils_cmd_getval.h +++ b/src/utils_cmd_getval.h @@ -22,7 +22,7 @@ #ifndef UTILS_CMD_GETVAL_H #define UTILS_CMD_GETVAL_H 1 -int handle_getval (FILE *fh, char **fields, int fields_num); +int handle_getval (FILE *fh, char *buffer); #endif /* UTILS_CMD_GETVAL_H */ -- 2.30.2