diff --git a/src/daemon/common.c b/src/daemon/common.c
index 5386739fbc372e96638ac55c79a91e8ca3af3ca9..8f22011ed57c46c94244f8273461fce3fa7af155 100644 (file)
--- a/src/daemon/common.c
+++ b/src/daemon/common.c
/* for getaddrinfo */
#include <sys/types.h>
-#include <sys/socket.h>
#include <netdb.h>
#if HAVE_NETINET_IN_H
assert ((0 > status) || (nleft >= (size_t)status));
- nleft = nleft - status;
- ptr = ptr + status;
+ nleft = nleft - ((size_t) status);
+ ptr = ptr + ((size_t) status);
}
return (0);
if (status < 0)
return (status);
- nleft = nleft - status;
- ptr = ptr + status;
+ nleft = nleft - ((size_t) status);
+ ptr = ptr + ((size_t) status);
}
return (0);
return ((int) i);
}
-int strjoin (char *dst, size_t dst_len,
+int strjoin (char *buffer, size_t buffer_size,
char **fields, size_t fields_num,
const char *sep)
{
- size_t field_len;
+ size_t avail;
+ char *ptr;
size_t sep_len;
- int i;
-
- memset (dst, '\0', dst_len);
+ size_t i;
- if (fields_num <= 0)
+ if ((buffer_size < 1) || (fields_num <= 0))
return (-1);
+ memset (buffer, 0, buffer_size);
+ ptr = buffer;
+ avail = buffer_size - 1;
+
sep_len = 0;
if (sep != NULL)
sep_len = strlen (sep);
- for (i = 0; i < (int)fields_num; i++)
+ for (i = 0; i < fields_num; i++)
{
+ size_t field_len;
+
if ((i > 0) && (sep_len > 0))
{
- if (dst_len <= sep_len)
+ if (avail < sep_len)
return (-1);
- strncat (dst, sep, dst_len);
- dst_len -= sep_len;
+ memcpy (ptr, sep, sep_len);
+ ptr += sep_len;
+ avail -= sep_len;
}
field_len = strlen (fields[i]);
-
- if (dst_len <= field_len)
+ if (avail < field_len)
return (-1);
- strncat (dst, fields[i], dst_len);
- dst_len -= field_len;
+ memcpy (ptr, fields[i], field_len);
+ ptr += field_len;
+ avail -= field_len;
}
- return (strlen (dst));
+ assert (buffer[buffer_size - 1] == 0);
+ return ((int) strlen (buffer));
}
int strsubstitute (char *str, char c_from, char c_to)
return (ret);
} /* int strsubstitute */
+int escape_string (char *buffer, size_t buffer_size)
+{
+ char *temp;
+ size_t i;
+ size_t j;
+
+ /* Check if we need to escape at all first */
+ temp = strpbrk (buffer, " \t\"\\");
+ if (temp == NULL)
+ return (0);
+
+ if (buffer_size < 3)
+ return (EINVAL);
+
+ temp = (char *) malloc (buffer_size);
+ if (temp == NULL)
+ return (ENOMEM);
+ memset (temp, 0, buffer_size);
+
+ temp[0] = '"';
+ j = 1;
+
+ for (i = 0; i < buffer_size; i++)
+ {
+ if (buffer[i] == 0)
+ {
+ break;
+ }
+ else if ((buffer[i] == '"') || (buffer[i] == '\\'))
+ {
+ if (j > (buffer_size - 4))
+ break;
+ temp[j] = '\\';
+ temp[j + 1] = buffer[i];
+ j += 2;
+ }
+ else
+ {
+ if (j > (buffer_size - 3))
+ break;
+ temp[j] = buffer[i];
+ j++;
+ }
+ }
+
+ assert ((j + 1) < buffer_size);
+ temp[j] = '"';
+ temp[j + 1] = 0;
+
+ sstrncpy (buffer, temp, buffer_size);
+ sfree (temp);
+ return (0);
+} /* int escape_string */
+
int strunescape (char *buf, size_t buf_len)
{
size_t i;
if ((buffer[buffer_len - 1] != '\n')
&& (buffer[buffer_len - 1] != '\r'))
break;
- buffer[buffer_len] = 0;
buffer_len--;
+ buffer[buffer_len] = 0;
}
return (buffer_len);
int escape_slashes (char *buffer, size_t buffer_size)
{
- int i;
size_t buffer_len;
+ size_t i;
buffer_len = strlen (buffer);
buffer_len--;
}
- for (i = 0; i < buffer_len - 1; i++)
+ for (i = 0; i < buffer_len; i++)
{
if (buffer[i] == '/')
buffer[i] = '_';
* Join the components together again
*/
dir[0] = '/';
- if (strjoin (dir + path_is_absolute, dir_len - path_is_absolute,
- fields, i + 1, "/") < 0)
+ if (strjoin (dir + path_is_absolute, (size_t) (dir_len - path_is_absolute),
+ fields, (size_t) (i + 1), "/") < 0)
{
ERROR ("strjoin failed: `%s', component #%i", file_orig, i);
return (-1);
{
size_t offset = 0;
int status;
- int i;
+ size_t i;
gauge_t *rates = NULL;
assert (0 == strcmp (ds->type, vl->type));
for (i = 0; i < ds->ds_num; i++)
{
if (ds->ds[i].type == DS_TYPE_GAUGE)
- BUFFER_ADD (":%f", vl->values[i].gauge);
+ BUFFER_ADD (":"GAUGE_FORMAT, vl->values[i].gauge);
else if (store_rates)
{
if (rates == NULL)
rates = uc_get_rate (ds, vl);
if (rates == NULL)
{
- WARNING ("format_values: "
- "uc_get_rate failed.");
+ WARNING ("format_values: uc_get_rate failed.");
return (-1);
}
- BUFFER_ADD (":%g", rates[i]);
+ BUFFER_ADD (":"GAUGE_FORMAT, rates[i]);
}
else if (ds->ds[i].type == DS_TYPE_COUNTER)
BUFFER_ADD (":%llu", vl->values[i].counter);
BUFFER_ADD (":%"PRIu64, vl->values[i].absolute);
else
{
- ERROR ("format_values plugin: Unknown data source type: %i",
+ ERROR ("format_values: Unknown data source type: %i",
ds->ds[i].type);
sfree (rates);
return (-1);
int parse_values (char *buffer, value_list_t *vl, const data_set_t *ds)
{
- int i;
+ size_t i;
char *dummy;
char *ptr;
char *saveptr;
- i = -1;
+ i = 0;
dummy = buffer;
saveptr = NULL;
+ vl->time = 0;
while ((ptr = strtok_r (dummy, ":", &saveptr)) != NULL)
{
dummy = NULL;
if (i >= vl->values_len)
{
/* Make sure i is invalid. */
- i = vl->values_len + 1;
+ i = 0;
break;
}
- if (i == -1)
+ if (vl->time == 0)
{
if (strcmp ("N", ptr) == 0)
vl->time = cdtime ();
vl->time = DOUBLE_TO_CDTIME_T (tmp);
}
- }
- else
- {
- if ((strcmp ("U", ptr) == 0) && (ds->ds[i].type == DS_TYPE_GAUGE))
- vl->values[i].gauge = NAN;
- else if (0 != parse_value (ptr, &vl->values[i], ds->ds[i].type))
- return -1;
+
+ continue;
}
+ if ((strcmp ("U", ptr) == 0) && (ds->ds[i].type == DS_TYPE_GAUGE))
+ vl->values[i].gauge = NAN;
+ else if (0 != parse_value (ptr, &vl->values[i], ds->ds[i].type))
+ return -1;
+
i++;
} /* while (strtok_r) */
- if ((ptr != NULL) || (i != vl->values_len))
+ if ((ptr != NULL) || (i == 0))
return (-1);
return (0);
} /* int parse_values */
if (old_value > new_value)
{
if (old_value <= 4294967295U)
- diff = (4294967295U - old_value) + new_value;
+ diff = (4294967295U - old_value) + new_value + 1;
else
- diff = (18446744073709551615ULL - old_value)
- + new_value;
+ diff = (18446744073709551615ULL - old_value) + new_value + 1;
}
else
{
return (0);
} /* }}} value_t rate_to_value */
-int value_to_rate (value_t *ret_rate, derive_t value, /* {{{ */
- value_to_rate_state_t *state,
- int ds_type, cdtime_t t)
+int value_to_rate (gauge_t *ret_rate, /* {{{ */
+ value_t value, int ds_type, cdtime_t t, value_to_rate_state_t *state)
{
- double interval;
+ gauge_t interval;
/* Another invalid state: The time is not increasing. */
if (t <= state->last_time)
interval = CDTIME_T_TO_DOUBLE(t - state->last_time);
/* Previous value is invalid. */
- if (state->last_time == 0) /* {{{ */
+ if (state->last_time == 0)
{
- if (ds_type == DS_TYPE_DERIVE)
- {
- state->last_value.derive = value;
- }
- else if (ds_type == DS_TYPE_COUNTER)
- {
- state->last_value.counter = (counter_t) value;
- }
- else if (ds_type == DS_TYPE_ABSOLUTE)
- {
- state->last_value.absolute = (absolute_t) value;
- }
- else
- {
- assert (23 == 42);
- }
-
+ state->last_value = value;
state->last_time = t;
return (EAGAIN);
- } /* }}} */
+ }
- if (ds_type == DS_TYPE_DERIVE)
- {
- ret_rate->gauge = (value - state->last_value.derive) / interval;
- state->last_value.derive = value;
+ switch (ds_type) {
+ case DS_TYPE_DERIVE: {
+ derive_t diff = value.derive - state->last_value.derive;
+ *ret_rate = ((gauge_t) diff) / ((gauge_t) interval);
+ break;
}
- else if (ds_type == DS_TYPE_COUNTER)
- {
- ret_rate->gauge = (((counter_t)value) - state->last_value.counter) / interval;
- state->last_value.counter = (counter_t) value;
+ case DS_TYPE_GAUGE: {
+ *ret_rate = value.gauge;
+ break;
}
- else if (ds_type == DS_TYPE_ABSOLUTE)
- {
- ret_rate->gauge = (((absolute_t)value) - state->last_value.absolute) / interval;
- state->last_value.absolute = (absolute_t) value;
+ case DS_TYPE_COUNTER: {
+ counter_t diff = counter_diff (state->last_value.counter, value.counter);
+ *ret_rate = ((gauge_t) diff) / ((gauge_t) interval);
+ break;
}
- else
- {
- assert (23 == 42);
+ case DS_TYPE_ABSOLUTE: {
+ absolute_t diff = value.absolute;
+ *ret_rate = ((gauge_t) diff) / ((gauge_t) interval);
+ break;
+ }
+ default:
+ return EINVAL;
}
- state->last_time = t;
+ state->last_value = value;
+ state->last_time = t;
return (0);
} /* }}} value_t rate_to_value */