| 1 |
/** |
| 2 |
* collectd - src/common.h |
| 3 |
* Copyright (C) 2005-2014 Florian octo Forster |
| 4 |
* |
| 5 |
* Permission is hereby granted, free of charge, to any person obtaining a |
| 6 |
* copy of this software and associated documentation files (the "Software"), |
| 7 |
* to deal in the Software without restriction, including without limitation |
| 8 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 9 |
* and/or sell copies of the Software, and to permit persons to whom the |
| 10 |
* Software is furnished to do so, subject to the following conditions: |
| 11 |
* |
| 12 |
* The above copyright notice and this permission notice shall be included in |
| 13 |
* all copies or substantial portions of the Software. |
| 14 |
* |
| 15 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 21 |
* DEALINGS IN THE SOFTWARE. |
| 22 |
* |
| 23 |
* Authors: |
| 24 |
* Florian octo Forster <octo at collectd.org> |
| 25 |
* Niki W. Waibel <[email protected]> |
| 26 |
**/ |
| 27 |
|
| 28 |
#ifndef COMMON_H |
| 29 |
#define COMMON_H |
| 30 |
|
| 31 |
#include "collectd.h" |
| 32 |
|
| 33 |
#include "plugin.h" |
| 34 |
|
| 35 |
#if HAVE_PWD_H |
| 36 |
#include <pwd.h> |
| 37 |
#endif |
| 38 |
|
| 39 |
#define sfree(ptr) \ |
| 40 |
do { \ |
| 41 |
free(ptr); \ |
| 42 |
(ptr) = NULL; \ |
| 43 |
} while (0) |
| 44 |
|
| 45 |
#define STATIC_ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a))) |
| 46 |
|
| 47 |
#define IS_TRUE(s) \ |
| 48 |
((strcasecmp("true", (s)) == 0) || (strcasecmp("yes", (s)) == 0) || \ |
| 49 |
(strcasecmp("on", (s)) == 0)) |
| 50 |
#define IS_FALSE(s) \ |
| 51 |
((strcasecmp("false", (s)) == 0) || (strcasecmp("no", (s)) == 0) || \ |
| 52 |
(strcasecmp("off", (s)) == 0)) |
| 53 |
|
| 54 |
struct rate_to_value_state_s { |
| 55 |
value_t last_value; |
| 56 |
cdtime_t last_time; |
| 57 |
gauge_t residual; |
| 58 |
}; |
| 59 |
typedef struct rate_to_value_state_s rate_to_value_state_t; |
| 60 |
|
| 61 |
struct value_to_rate_state_s { |
| 62 |
value_t last_value; |
| 63 |
cdtime_t last_time; |
| 64 |
}; |
| 65 |
typedef struct value_to_rate_state_s value_to_rate_state_t; |
| 66 |
|
| 67 |
char *sstrncpy(char *dest, const char *src, size_t n); |
| 68 |
|
| 69 |
__attribute__((format(printf, 3, 4))) int ssnprintf(char *str, size_t size, |
| 70 |
char const *format, ...); |
| 71 |
|
| 72 |
__attribute__((format(printf, 1, 2))) char *ssnprintf_alloc(char const *format, |
| 73 |
...); |
| 74 |
|
| 75 |
char *sstrdup(const char *s); |
| 76 |
size_t sstrnlen(const char *s, size_t n); |
| 77 |
char *sstrndup(const char *s, size_t n); |
| 78 |
void *smalloc(size_t size); |
| 79 |
char *sstrerror(int errnum, char *buf, size_t buflen); |
| 80 |
|
| 81 |
#ifndef ERRBUF_SIZE |
| 82 |
#define ERRBUF_SIZE 256 |
| 83 |
#endif |
| 84 |
|
| 85 |
#define STRERROR(e) sstrerror((e), (char[ERRBUF_SIZE]){0}, ERRBUF_SIZE) |
| 86 |
#define STRERRNO STRERROR(errno) |
| 87 |
|
| 88 |
/* |
| 89 |
* NAME |
| 90 |
* sread |
| 91 |
* |
| 92 |
* DESCRIPTION |
| 93 |
* Reads exactly `n' bytes or fails. Syntax and other behavior is analogous |
| 94 |
* to `read(2)'. |
| 95 |
* |
| 96 |
* PARAMETERS |
| 97 |
* `fd' File descriptor to write to. |
| 98 |
* `buf' Buffer that is to be written. |
| 99 |
* `count' Number of bytes in the buffer. |
| 100 |
* |
| 101 |
* RETURN VALUE |
| 102 |
* Zero upon success or non-zero if an error occurred. `errno' is set in this |
| 103 |
* case. |
| 104 |
*/ |
| 105 |
int sread(int fd, void *buf, size_t count); |
| 106 |
|
| 107 |
/* |
| 108 |
* NAME |
| 109 |
* swrite |
| 110 |
* |
| 111 |
* DESCRIPTION |
| 112 |
* Writes exactly `n' bytes or fails. Syntax and other behavior is analogous |
| 113 |
* to `write(2)'. |
| 114 |
* |
| 115 |
* PARAMETERS |
| 116 |
* `fd' File descriptor to write to. |
| 117 |
* `buf' Buffer that is to be written. |
| 118 |
* `count' Number of bytes in the buffer. |
| 119 |
* |
| 120 |
* RETURN VALUE |
| 121 |
* Zero upon success or non-zero if an error occurred. `errno' is set in this |
| 122 |
* case. |
| 123 |
*/ |
| 124 |
int swrite(int fd, const void *buf, size_t count); |
| 125 |
|
| 126 |
/* |
| 127 |
* NAME |
| 128 |
* strsplit |
| 129 |
* |
| 130 |
* DESCRIPTION |
| 131 |
* Splits a string into parts and stores pointers to the parts in `fields'. |
| 132 |
* The characters split at are: " ", "\t", "\r", and "\n". |
| 133 |
* |
| 134 |
* PARAMETERS |
| 135 |
* `string' String to split. This string will be modified. `fields' will |
| 136 |
* contain pointers to parts of this string, so free'ing it |
| 137 |
* will destroy `fields' as well. |
| 138 |
* `fields' Array of strings where pointers to the parts will be stored. |
| 139 |
* `size' Number of elements in the array. No more than `size' |
| 140 |
* pointers will be stored in `fields'. |
| 141 |
* |
| 142 |
* RETURN VALUE |
| 143 |
* Returns the number of parts stored in `fields'. |
| 144 |
*/ |
| 145 |
int strsplit(char *string, char **fields, size_t size); |
| 146 |
|
| 147 |
/* |
| 148 |
* NAME |
| 149 |
* strjoin |
| 150 |
* |
| 151 |
* DESCRIPTION |
| 152 |
* Joins together several parts of a string using `sep' as a separator. This |
| 153 |
* is equivalent to the Perl built-in `join'. |
| 154 |
* |
| 155 |
* PARAMETERS |
| 156 |
* `dst' Buffer where the result is stored. Can be NULL if you need to |
| 157 |
* determine the required buffer size only. |
| 158 |
* `dst_len' Length of the destination buffer. No more than this many |
| 159 |
* bytes will be written to the memory pointed to by `dst', |
| 160 |
* including the trailing null-byte. Must be zero if dst is |
| 161 |
* NULL. |
| 162 |
* `fields' Array of strings to be joined. |
| 163 |
* `fields_num' Number of elements in the `fields' array. |
| 164 |
* `sep' String to be inserted between any two elements of `fields'. |
| 165 |
* This string is neither prepended nor appended to the result. |
| 166 |
* Instead of passing "" (empty string) one can pass NULL. |
| 167 |
* |
| 168 |
* RETURN VALUE |
| 169 |
* Returns the number of characters in the resulting string, excluding a |
| 170 |
* tailing null byte. If this value is greater than or equal to "dst_len", the |
| 171 |
* result in "dst" is truncated (but still null terminated). On error a |
| 172 |
* negative value is returned. |
| 173 |
*/ |
| 174 |
int strjoin(char *dst, size_t dst_len, char **fields, size_t fields_num, |
| 175 |
const char *sep); |
| 176 |
|
| 177 |
/* |
| 178 |
* NAME |
| 179 |
* escape_slashes |
| 180 |
* |
| 181 |
* DESCRIPTION |
| 182 |
* Removes slashes ("/") from "buffer". If buffer contains a single slash, |
| 183 |
* the result will be "root". Leading slashes are removed. All other slashes |
| 184 |
* are replaced with underscores ("_"). |
| 185 |
* This function is used by plugin_dispatch_values() to escape all parts of |
| 186 |
* the identifier. |
| 187 |
* |
| 188 |
* PARAMETERS |
| 189 |
* `buffer' String to be escaped. |
| 190 |
* `buffer_size' Size of the buffer. No more then this many bytes will be |
| 191 |
* written to `buffer', including the trailing null-byte. |
| 192 |
* |
| 193 |
* RETURN VALUE |
| 194 |
* Returns zero upon success and a value smaller than zero upon failure. |
| 195 |
*/ |
| 196 |
int escape_slashes(char *buffer, size_t buffer_size); |
| 197 |
|
| 198 |
/** |
| 199 |
* NAME |
| 200 |
* escape_string |
| 201 |
* |
| 202 |
* DESCRIPTION |
| 203 |
* escape_string quotes and escapes a string to be usable with collectd's |
| 204 |
* plain text protocol. "simple" strings are left as they are, for example if |
| 205 |
* buffer is 'simple' before the call, it will remain 'simple'. However, if |
| 206 |
* buffer contains 'more "complex"' before the call, the returned buffer will |
| 207 |
* contain '"more \"complex\""'. |
| 208 |
* |
| 209 |
* If the buffer is too small to contain the escaped string, the string will |
| 210 |
* be truncated. However, leading and trailing double quotes, as well as an |
| 211 |
* ending null byte are guaranteed. |
| 212 |
* |
| 213 |
* RETURN VALUE |
| 214 |
* Returns zero on success, even if the string was truncated. Non-zero on |
| 215 |
* failure. |
| 216 |
*/ |
| 217 |
int escape_string(char *buffer, size_t buffer_size); |
| 218 |
|
| 219 |
/* |
| 220 |
* NAME |
| 221 |
* replace_special |
| 222 |
* |
| 223 |
* DESCRIPTION |
| 224 |
* Replaces any special characters (anything that's not alpha-numeric or a |
| 225 |
* dash) with an underscore. |
| 226 |
* |
| 227 |
* E.g. "foo$bar&" would become "foo_bar_". |
| 228 |
* |
| 229 |
* PARAMETERS |
| 230 |
* `buffer' String to be handled. |
| 231 |
* `buffer_size' Length of the string. The function returns after |
| 232 |
* encountering a null-byte or reading this many bytes. |
| 233 |
*/ |
| 234 |
void replace_special(char *buffer, size_t buffer_size); |
| 235 |
|
| 236 |
/* |
| 237 |
* NAME |
| 238 |
* strunescape |
| 239 |
* |
| 240 |
* DESCRIPTION |
| 241 |
* Replaces any escaped characters in a string with the appropriate special |
| 242 |
* characters. The following escaped characters are recognized: |
| 243 |
* |
| 244 |
* \t -> <tab> |
| 245 |
* \n -> <newline> |
| 246 |
* \r -> <carriage return> |
| 247 |
* |
| 248 |
* For all other escacped characters only the backslash will be removed. |
| 249 |
* |
| 250 |
* PARAMETERS |
| 251 |
* `buf' String to be unescaped. |
| 252 |
* `buf_len' Length of the string, including the terminating null-byte. |
| 253 |
* |
| 254 |
* RETURN VALUE |
| 255 |
* Returns zero upon success, a value less than zero else. |
| 256 |
*/ |
| 257 |
int strunescape(char *buf, size_t buf_len); |
| 258 |
|
| 259 |
/** |
| 260 |
* Removed trailing newline characters (CR and LF) from buffer, which must be |
| 261 |
* null terminated. Returns the length of the resulting string. |
| 262 |
*/ |
| 263 |
__attribute__((nonnull(1))) size_t strstripnewline(char *buffer); |
| 264 |
|
| 265 |
/* |
| 266 |
* NAME |
| 267 |
* timeval_cmp |
| 268 |
* |
| 269 |
* DESCRIPTION |
| 270 |
* Compare the two time values `tv0' and `tv1' and store the absolut value |
| 271 |
* of the difference in the time value pointed to by `delta' if it does not |
| 272 |
* equal NULL. |
| 273 |
* |
| 274 |
* RETURN VALUE |
| 275 |
* Returns an integer less than, equal to, or greater than zero if `tv0' is |
| 276 |
* less than, equal to, or greater than `tv1' respectively. |
| 277 |
*/ |
| 278 |
int timeval_cmp(struct timeval tv0, struct timeval tv1, struct timeval *delta); |
| 279 |
|
| 280 |
/* make sure tv_usec stores less than a second */ |
| 281 |
#define NORMALIZE_TIMEVAL(tv) \ |
| 282 |
do { \ |
| 283 |
(tv).tv_sec += (tv).tv_usec / 1000000; \ |
| 284 |
(tv).tv_usec = (tv).tv_usec % 1000000; \ |
| 285 |
} while (0) |
| 286 |
|
| 287 |
/* make sure tv_sec stores less than a second */ |
| 288 |
#define NORMALIZE_TIMESPEC(tv) \ |
| 289 |
do { \ |
| 290 |
(tv).tv_sec += (tv).tv_nsec / 1000000000; \ |
| 291 |
(tv).tv_nsec = (tv).tv_nsec % 1000000000; \ |
| 292 |
} while (0) |
| 293 |
|
| 294 |
int check_create_dir(const char *file_orig); |
| 295 |
|
| 296 |
#ifdef HAVE_LIBKSTAT |
| 297 |
#if HAVE_KSTAT_H |
| 298 |
#include <kstat.h> |
| 299 |
#endif |
| 300 |
int get_kstat(kstat_t **ksp_ptr, char *module, int instance, char *name); |
| 301 |
long long get_kstat_value(kstat_t *ksp, char *name); |
| 302 |
#endif |
| 303 |
|
| 304 |
#ifndef HAVE_HTONLL |
| 305 |
unsigned long long ntohll(unsigned long long n); |
| 306 |
unsigned long long htonll(unsigned long long n); |
| 307 |
#endif |
| 308 |
|
| 309 |
#if FP_LAYOUT_NEED_NOTHING |
| 310 |
#define ntohd(d) (d) |
| 311 |
#define htond(d) (d) |
| 312 |
#elif FP_LAYOUT_NEED_ENDIANFLIP || FP_LAYOUT_NEED_INTSWAP |
| 313 |
double ntohd(double d); |
| 314 |
double htond(double d); |
| 315 |
#else |
| 316 |
#error \ |
| 317 |
"Don't know how to convert between host and network representation of doubles." |
| 318 |
#endif |
| 319 |
|
| 320 |
int format_name(char *ret, int ret_len, const char *hostname, |
| 321 |
const char *plugin, const char *plugin_instance, |
| 322 |
const char *type, const char *type_instance); |
| 323 |
#define FORMAT_VL(ret, ret_len, vl) \ |
| 324 |
format_name(ret, ret_len, (vl)->host, (vl)->plugin, (vl)->plugin_instance, \ |
| 325 |
(vl)->type, (vl)->type_instance) |
| 326 |
int format_values(char *ret, size_t ret_len, const data_set_t *ds, |
| 327 |
const value_list_t *vl, bool store_rates); |
| 328 |
|
| 329 |
int parse_identifier(char *str, char **ret_host, char **ret_plugin, |
| 330 |
char **ret_plugin_instance, char **ret_type, |
| 331 |
char **ret_type_instance, char *default_host); |
| 332 |
int parse_identifier_vl(const char *str, value_list_t *vl); |
| 333 |
int parse_value(const char *value, value_t *ret_value, int ds_type); |
| 334 |
int parse_values(char *buffer, value_list_t *vl, const data_set_t *ds); |
| 335 |
|
| 336 |
/* parse_value_file reads "path" and parses its content as an integer or |
| 337 |
* floating point, depending on "ds_type". On success, the value is stored in |
| 338 |
* "ret_value" and zero is returned. On failure, a non-zero value is returned. |
| 339 |
*/ |
| 340 |
int parse_value_file(char const *path, value_t *ret_value, int ds_type); |
| 341 |
|
| 342 |
#if !HAVE_GETPWNAM_R |
| 343 |
struct passwd; |
| 344 |
int getpwnam_r(const char *name, struct passwd *pwbuf, char *buf, size_t buflen, |
| 345 |
struct passwd **pwbufp); |
| 346 |
#endif |
| 347 |
|
| 348 |
int notification_init(notification_t *n, int severity, const char *message, |
| 349 |
const char *host, const char *plugin, |
| 350 |
const char *plugin_instance, const char *type, |
| 351 |
const char *type_instance); |
| 352 |
#define NOTIFICATION_INIT_VL(n, vl) \ |
| 353 |
notification_init(n, NOTIF_FAILURE, NULL, (vl)->host, (vl)->plugin, \ |
| 354 |
(vl)->plugin_instance, (vl)->type, (vl)->type_instance) |
| 355 |
|
| 356 |
typedef int (*dirwalk_callback_f)(const char *dirname, const char *filename, |
| 357 |
void *user_data); |
| 358 |
int walk_directory(const char *dir, dirwalk_callback_f callback, |
| 359 |
void *user_data, int hidden); |
| 360 |
/* Returns the number of bytes read or negative on error. */ |
| 361 |
ssize_t read_file_contents(char const *filename, void *buf, size_t bufsize); |
| 362 |
/* Writes the contents of the file into the buffer with a trailing NUL. |
| 363 |
* Returns the number of bytes written to the buffer or negative on error. */ |
| 364 |
ssize_t read_text_file_contents(char const *filename, char *buf, |
| 365 |
size_t bufsize); |
| 366 |
|
| 367 |
counter_t counter_diff(counter_t old_value, counter_t new_value); |
| 368 |
|
| 369 |
/* Convert a rate back to a value_t. When converting to a derive_t, counter_t |
| 370 |
* or absolute_t, take fractional residuals into account. This is important |
| 371 |
* when scaling counters, for example. |
| 372 |
* Returns zero on success. Returns EAGAIN when called for the first time; in |
| 373 |
* this case the value_t is invalid and the next call should succeed. Other |
| 374 |
* return values indicate an error. */ |
| 375 |
int rate_to_value(value_t *ret_value, gauge_t rate, |
| 376 |
rate_to_value_state_t *state, int ds_type, cdtime_t t); |
| 377 |
|
| 378 |
int value_to_rate(gauge_t *ret_rate, value_t value, int ds_type, cdtime_t t, |
| 379 |
value_to_rate_state_t *state); |
| 380 |
|
| 381 |
/* Converts a service name (a string) to a port number |
| 382 |
* (in the range [1-65535]). Returns less than zero on error. */ |
| 383 |
int service_name_to_port_number(const char *service_name); |
| 384 |
|
| 385 |
/* Sets various, non-default, socket options */ |
| 386 |
void set_sock_opts(int sockfd); |
| 387 |
|
| 388 |
/** Parse a string to a derive_t value. Returns zero on success or non-zero on |
| 389 |
* failure. If failure is returned, ret_value is not touched. */ |
| 390 |
int strtoderive(const char *string, derive_t *ret_value); |
| 391 |
|
| 392 |
/** Parse a string to a gauge_t value. Returns zero on success or non-zero on |
| 393 |
* failure. If failure is returned, ret_value is not touched. */ |
| 394 |
int strtogauge(const char *string, gauge_t *ret_value); |
| 395 |
|
| 396 |
int strarray_add(char ***ret_array, size_t *ret_array_len, char const *str); |
| 397 |
void strarray_free(char **array, size_t array_len); |
| 398 |
|
| 399 |
/** Check if the current process benefits from the capability passed in |
| 400 |
* argument. Returns zero if it does, less than zero if it doesn't or on error. |
| 401 |
* See capabilities(7) for the list of possible capabilities. |
| 402 |
* */ |
| 403 |
int check_capability(int arg); |
| 404 |
|
| 405 |
#endif /* COMMON_H */ |