src/utils_cmd_getval.[ch]: Fix handling of identifiers with spaces.
authorFlorian Forster <octo@noris.net>
Wed, 27 Aug 2008 09:59:53 +0000 (11:59 +0200)
committerFlorian Forster <octo@noris.net>
Wed, 27 Aug 2008 09:59:53 +0000 (11:59 +0200)
The getval handler now uses the `parse_string' function in
`utils_parse_option' to handle quoted strings correctly.

bindings/perl/Collectd/Unixsock.pm
src/unixsock.c
src/utils_cmd_getval.c
src/utils_cmd_getval.h

index cd910ed..d449924 100644 (file)
@@ -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;
index c2e1f30..db75809 100644 (file)
@@ -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)
                {
index 470d302..33b40e0 100644 (file)
@@ -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) { \
     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");
index d7bd115..86134cd 100644 (file)
@@ -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 */