X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Futils_cmd_putval.c;h=9cbba126f48496c5750d623dc6cd5fe8fd0cdccf;hb=10075e6fe3c384b73c2dd398a1435f8d10e56654;hp=98b5043f33d1a94156fa509404a840af76dc69bb;hpb=4a005f01e9ba4322bdb41631928ecc9edf51b189;p=collectd.git diff --git a/src/utils_cmd_putval.c b/src/utils_cmd_putval.c index 98b5043f..9cbba126 100644 --- a/src/utils_cmd_putval.c +++ b/src/utils_cmd_putval.c @@ -23,8 +23,15 @@ #include "common.h" #include "plugin.h" +#define print_to_socket(fh, ...) \ + if (fprintf (fh, __VA_ARGS__) < 0) { \ + char errbuf[1024]; \ + WARNING ("handle_putval: failed to write to socket #%i: %s", \ + fileno (fh), sstrerror (errno, errbuf, sizeof (errbuf))); \ + return -1; \ + } + static int parse_value (const data_set_t *ds, value_list_t *vl, - const char *type, FILE *fh, char *buffer) { char *dummy; @@ -36,7 +43,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.\n"); + print_to_socket (fh, "-1 No time found.\n"); return (-1); } *value_str = '\0'; value_str++; @@ -76,13 +83,13 @@ static int parse_value (const data_set_t *ds, value_list_t *vl, "Number of values incorrect: " "Got %i, expected %i. Identifier is `%s'.", i, vl->values_len, identifier); - fprintf (fh, "-1 Number of values incorrect: " + print_to_socket (fh, "-1 Number of values incorrect: " "Got %i, expected %i.\n", i, vl->values_len); return (-1); } - plugin_dispatch_values (type, vl); + plugin_dispatch_values (vl); return (0); } /* int parse_value */ @@ -130,10 +137,9 @@ int handle_putval (FILE *fh, char **fields, int fields_num) { DEBUG ("cmd putval: Wrong number of fields: %i", fields_num); - fprintf (fh, "-1 Wrong number of fields: Got %i, " + print_to_socket (fh, "-1 Wrong number of fields: Got %i, " "expected at least 3.\n", fields_num); - fflush (fh); return (-1); } @@ -147,8 +153,7 @@ int handle_putval (FILE *fh, char **fields, int fields_num) if (status != 0) { DEBUG ("cmd putval: Cannot parse `%s'", fields[1]); - fprintf (fh, "-1 Cannot parse identifier.\n"); - fflush (fh); + print_to_socket (fh, "-1 Cannot parse identifier.\n"); sfree (identifier_copy); return (-1); } @@ -160,18 +165,18 @@ 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.\n"); - fflush (fh); + print_to_socket (fh, "-1 Identifier too long.\n"); sfree (identifier_copy); return (-1); } - strcpy (vl.host, hostname); - strcpy (vl.plugin, plugin); + sstrncpy (vl.host, hostname, sizeof (vl.host)); + sstrncpy (vl.plugin, plugin, sizeof (vl.plugin)); + sstrncpy (vl.type, type, sizeof (vl.type)); if (plugin_instance != NULL) - strcpy (vl.plugin_instance, plugin_instance); + sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance)); if (type_instance != NULL) - strcpy (vl.type_instance, type_instance); + sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance)); ds = plugin_get_ds (type); if (ds == NULL) { @@ -183,8 +188,7 @@ int handle_putval (FILE *fh, char **fields, int fields_num) vl.values = (value_t *) malloc (vl.values_len * sizeof (value_t)); if (vl.values == NULL) { - fprintf (fh, "-1 malloc failed.\n"); - fflush (fh); + print_to_socket (fh, "-1 malloc failed.\n"); sfree (identifier_copy); return (-1); } @@ -197,14 +201,14 @@ int handle_putval (FILE *fh, char **fields, int fields_num) /* It's parse_value's job to write an error to `fh'. * This is not the case with `parse_option below. * Neither will write an success message. */ - if (parse_value (ds, &vl, type, fh, fields[i]) != 0) + if (parse_value (ds, &vl, fh, fields[i]) != 0) break; } else if (strchr (fields[i], '=') != NULL) { if (parse_option (&vl, fields[i]) != 0) { - fprintf (fh, "-1 Error parsing option `%s'\n", + print_to_socket (fh, "-1 Error parsing option `%s'\n", fields[i]); break; } @@ -220,8 +224,7 @@ int handle_putval (FILE *fh, char **fields, int fields_num) /* Done parsing the options. */ if (i == fields_num) - fprintf (fh, "0 Success\n"); - fflush (fh); + print_to_socket (fh, "0 Success\n"); sfree (vl.values); sfree (identifier_copy);