X-Git-Url: https://git.octo.it/?a=blobdiff_plain;ds=sidebyside;f=src%2Futils_cmd_putval.c;h=16a09b1b71093c7eb5752e4c28772fc34800a667;hb=6acb19befc321c501e4d8b79f403ebda0c021acc;hp=98b5043f33d1a94156fa509404a840af76dc69bb;hpb=3da24ea8c48268a64025dc4aae5bdf223d7dad0c;p=collectd.git diff --git a/src/utils_cmd_putval.c b/src/utils_cmd_putval.c index 98b5043f..16a09b1b 100644 --- a/src/utils_cmd_putval.c +++ b/src/utils_cmd_putval.c @@ -23,6 +23,14 @@ #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) @@ -36,7 +44,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,7 +84,7 @@ 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); @@ -130,10 +138,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 +154,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 +166,17 @@ 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)); 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); } @@ -204,7 +208,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'\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);