Merge branch 'pull/collectd-4.3' into collectd-4.3
authorFlorian Forster <octo@huhu.verplant.org>
Wed, 26 Mar 2008 08:56:22 +0000 (09:56 +0100)
committerFlorian Forster <octo@huhu.verplant.org>
Wed, 26 Mar 2008 08:56:22 +0000 (09:56 +0100)
bindings/perl/Collectd/Unixsock.pm
src/collectd-unixsock.pod
src/unixsock.c
src/utils_cmd_putnotif.c
src/utils_cmd_putval.c

index d3b77d0..8749c1a 100644 (file)
@@ -73,7 +73,7 @@ sub _create_socket
 
 =head1 VALUE IDENTIFIER
 
-The values in the collectd are identified using an five-tupel (host, plugin,
+The values in the collectd are identified using an five-tuple (host, plugin,
 plugin-instance, type, type-instance) where only plugin-instance and
 type-instance may be NULL (or undefined). Many functions expect an
 I<%identifier> hash that has at least the members B<host>, B<plugin>, and
@@ -217,7 +217,7 @@ sub getval
 =item I<$obj>-E<gt>B<putval> (I<%identifier>, B<time> =E<gt> I<$time>, B<values> =E<gt> [...]);
 
 Submits a value-list to the daemon. If the B<time> argument is omitted
-C<time()> is used. The requierd argument B<values> is a reference to an array
+C<time()> is used. The required argument B<values> is a reference to an array
 of values that is to be submitted. The number of values must match the number
 of values expected for the given B<type> (see L<VALUE IDENTIFIER>), though this
 is checked by the daemon, not the Perl module. Also, gauge data-sources
index 2339625..3ef2438 100644 (file)
@@ -207,10 +207,12 @@ value on failure and never return zero.
 
 =head1 ABSTRACTION LAYER
 
-Shipped with the sourcecode comes the Perl-Module L<Collectd::Unixsock> which
+B<collectd> ships the Perl-Module L<Collectd::Unixsock> which
 provides an abstraction layer over the actual socket connection. It can be
-found in the directory F<contrib/PerlLib>. If you want to use Perl to
-communicate with the daemon, you're encouraged to use and expand this module.
+found in the directory F<bindings/perl/> in the source distribution or
+(usually) somewhere near F</usr/share/perl5/> if you're using a package. If
+you want to use Perl to communicate with the daemon, you're encouraged to use
+and expand this module.
 
 =head1 SEE ALSO
 
index 837a902..9fa0259 100644 (file)
@@ -547,7 +547,7 @@ static int us_handle_listval (FILE *fh, char **fields, int fields_num)
 static void *us_handle_client (void *arg)
 {
        int fd;
-       FILE *fh;
+       FILE *fhin, *fhout;
        char buffer[1024];
        char *fields[128];
        int   fields_num;
@@ -558,8 +558,8 @@ static void *us_handle_client (void *arg)
 
        DEBUG ("Reading from fd #%i", fd);
 
-       fh = fdopen (fd, "r+");
-       if (fh == NULL)
+       fhin  = fdopen (fd, "r");
+       if (fhin == NULL)
        {
                char errbuf[1024];
                ERROR ("unixsock plugin: fdopen failed: %s",
@@ -568,7 +568,17 @@ static void *us_handle_client (void *arg)
                pthread_exit ((void *) 1);
        }
 
-       while (fgets (buffer, sizeof (buffer), fh) != NULL)
+       fhout = fdopen (fd, "w");
+       if (fhout == NULL)
+       {
+               char errbuf[1024];
+               ERROR ("unixsock plugin: fdopen failed: %s",
+                               sstrerror (errno, errbuf, sizeof (errbuf)));
+               fclose (fhin); /* this closes fd as well */
+               pthread_exit ((void *) 1);
+       }
+
+       while (fgets (buffer, sizeof (buffer), fhin) != NULL)
        {
                int len;
 
@@ -593,29 +603,30 @@ static void *us_handle_client (void *arg)
 
                if (strcasecmp (fields[0], "getval") == 0)
                {
-                       us_handle_getval (fh, fields, fields_num);
+                       us_handle_getval (fhout, fields, fields_num);
                }
                else if (strcasecmp (fields[0], "putval") == 0)
                {
-                       handle_putval (fh, fields, fields_num);
+                       handle_putval (fhout, fields, fields_num);
                }
                else if (strcasecmp (fields[0], "listval") == 0)
                {
-                       us_handle_listval (fh, fields, fields_num);
+                       us_handle_listval (fhout, fields, fields_num);
                }
                else if (strcasecmp (fields[0], "putnotif") == 0)
                {
-                       handle_putnotif (fh, fields, fields_num);
+                       handle_putnotif (fhout, fields, fields_num);
                }
                else
                {
-                       fprintf (fh, "-1 Unknown command: %s\n", fields[0]);
-                       fflush (fh);
+                       fprintf (fhout, "-1 Unknown command: %s\n", fields[0]);
+                       fflush (fhout);
                }
        } /* while (fgets) */
 
        DEBUG ("Exiting..");
-       close (fd);
+       fclose (fhin);
+       fclose (fhout);
 
        pthread_exit ((void *) 0);
        return ((void *) 0);
index 0282e4e..18c1ece 100644 (file)
@@ -133,7 +133,7 @@ int handle_putnotif (FILE *fh, char **fields, int fields_num)
       status = parse_option (&n, fields[i]);
       if (status != 0)
       {
-       fprintf (fh, "-1 Error parsing option `%s'", fields[i]);
+       fprintf (fh, "-1 Error parsing option `%s'\n", fields[i]);
        break;
       }
     }
index 7cf0b6d..98b5043 100644 (file)
@@ -36,7 +36,7 @@ static int parse_value (const data_set_t *ds, value_list_t *vl,
        char *value_str = strchr (time_str, ':');
        if (value_str == NULL)
        {
-               fprintf (fh, "-1 No time found.");
+               fprintf (fh, "-1 No time found.\n");
                return (-1);
        }
        *value_str = '\0'; value_str++;
@@ -121,6 +121,8 @@ int handle_putval (FILE *fh, char **fields, int fields_num)
        int   status;
        int   i;
 
+       char *identifier_copy;
+
        const data_set_t *ds;
        value_list_t vl = VALUE_LIST_INIT;
 
@@ -135,7 +137,11 @@ int handle_putval (FILE *fh, char **fields, int fields_num)
                return (-1);
        }
 
-       status = parse_identifier (fields[1], &hostname,
+       /* parse_identifier() modifies its first argument,
+        * returning pointers into it */
+       identifier_copy = sstrdup (fields[1]);
+
+       status = parse_identifier (identifier_copy, &hostname,
                        &plugin, &plugin_instance,
                        &type, &type_instance);
        if (status != 0)
@@ -143,6 +149,7 @@ int handle_putval (FILE *fh, char **fields, int fields_num)
                DEBUG ("cmd putval: Cannot parse `%s'", fields[1]);
                fprintf (fh, "-1 Cannot parse identifier.\n");
                fflush (fh);
+               sfree (identifier_copy);
                return (-1);
        }
 
@@ -153,7 +160,9 @@ int handle_putval (FILE *fh, char **fields, int fields_num)
                        || ((type_instance != NULL)
                                && (strlen (type_instance) >= sizeof (vl.type_instance))))
        {
-               fprintf (fh, "-1 Identifier too long.");
+               fprintf (fh, "-1 Identifier too long.\n");
+               fflush (fh);
+               sfree (identifier_copy);
                return (-1);
        }
 
@@ -165,14 +174,18 @@ int handle_putval (FILE *fh, char **fields, int fields_num)
                strcpy (vl.type_instance, type_instance);
 
        ds = plugin_get_ds (type);
-       if (ds == NULL)
+       if (ds == NULL) {
+               sfree (identifier_copy);
                return (-1);
+       }
 
        vl.values_len = ds->ds_num;
        vl.values = (value_t *) malloc (vl.values_len * sizeof (value_t));
        if (vl.values == NULL)
        {
-               fprintf (fh, "-1 malloc failed.");
+               fprintf (fh, "-1 malloc failed.\n");
+               fflush (fh);
+               sfree (identifier_copy);
                return (-1);
        }
 
@@ -191,7 +204,7 @@ int handle_putval (FILE *fh, char **fields, int fields_num)
                {
                        if (parse_option (&vl, fields[i]) != 0)
                        {
-                               fprintf (fh, "-1 Error parsing option `%s'",
+                               fprintf (fh, "-1 Error parsing option `%s'\n",
                                                fields[i]);
                                break;
                        }
@@ -211,6 +224,7 @@ int handle_putval (FILE *fh, char **fields, int fields_num)
        fflush (fh);
 
        sfree (vl.values); 
+       sfree (identifier_copy);
 
        return (0);
 } /* int handle_putval */