| 1 |
/** |
| 2 |
* collectd - src/configfile.c |
| 3 |
* Copyright (C) 2005-2011 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 |
* Sebastian tokkee Harl <sh at tokkee.org> |
| 26 |
**/ |
| 27 |
|
| 28 |
#include "collectd.h" |
| 29 |
|
| 30 |
#include "liboconfig/oconfig.h" |
| 31 |
|
| 32 |
#include "configfile.h" |
| 33 |
#include "filter_chain.h" |
| 34 |
#include "plugin.h" |
| 35 |
#include "types_list.h" |
| 36 |
#include "utils/common/common.h" |
| 37 |
|
| 38 |
#if HAVE_WORDEXP_H |
| 39 |
#include <wordexp.h> |
| 40 |
#endif /* HAVE_WORDEXP_H */ |
| 41 |
|
| 42 |
#if HAVE_FNMATCH_H |
| 43 |
#include <fnmatch.h> |
| 44 |
#endif /* HAVE_FNMATCH_H */ |
| 45 |
|
| 46 |
#if HAVE_LIBGEN_H |
| 47 |
#include <libgen.h> |
| 48 |
#endif /* HAVE_LIBGEN_H */ |
| 49 |
|
| 50 |
#define ESCAPE_NULL(str) ((str) == NULL ? "(null)" : (str)) |
| 51 |
|
| 52 |
/* |
| 53 |
* Private types |
| 54 |
*/ |
| 55 |
typedef struct cf_callback { |
| 56 |
const char *type; |
| 57 |
int (*callback)(const char *, const char *); |
| 58 |
const char **keys; |
| 59 |
int keys_num; |
| 60 |
plugin_ctx_t ctx; |
| 61 |
struct cf_callback *next; |
| 62 |
} cf_callback_t; |
| 63 |
|
| 64 |
typedef struct cf_complex_callback_s { |
| 65 |
char *type; |
| 66 |
int (*callback)(oconfig_item_t *); |
| 67 |
plugin_ctx_t ctx; |
| 68 |
struct cf_complex_callback_s *next; |
| 69 |
} cf_complex_callback_t; |
| 70 |
|
| 71 |
typedef struct cf_value_map_s { |
| 72 |
const char *key; |
| 73 |
int (*func)(oconfig_item_t *); |
| 74 |
} cf_value_map_t; |
| 75 |
|
| 76 |
typedef struct cf_global_option_s { |
| 77 |
const char *key; |
| 78 |
char *value; |
| 79 |
bool from_cli; /* value set from CLI */ |
| 80 |
const char *def; |
| 81 |
} cf_global_option_t; |
| 82 |
|
| 83 |
/* |
| 84 |
* Prototypes of callback functions |
| 85 |
*/ |
| 86 |
static int dispatch_value_typesdb(oconfig_item_t *ci); |
| 87 |
static int dispatch_value_plugindir(oconfig_item_t *ci); |
| 88 |
static int dispatch_loadplugin(oconfig_item_t *ci); |
| 89 |
static int dispatch_block_plugin(oconfig_item_t *ci); |
| 90 |
|
| 91 |
/* |
| 92 |
* Private variables |
| 93 |
*/ |
| 94 |
static cf_callback_t *first_callback; |
| 95 |
static cf_complex_callback_t *complex_callback_head; |
| 96 |
|
| 97 |
static cf_value_map_t cf_value_map[] = {{"TypesDB", dispatch_value_typesdb}, |
| 98 |
{"PluginDir", dispatch_value_plugindir}, |
| 99 |
{"LoadPlugin", dispatch_loadplugin}, |
| 100 |
{"Plugin", dispatch_block_plugin}}; |
| 101 |
static int cf_value_map_num = STATIC_ARRAY_SIZE(cf_value_map); |
| 102 |
|
| 103 |
static cf_global_option_t cf_global_options[] = { |
| 104 |
{"BaseDir", NULL, 0, PKGLOCALSTATEDIR}, |
| 105 |
{"PIDFile", NULL, 0, PIDFILE}, |
| 106 |
{"Hostname", NULL, 0, NULL}, |
| 107 |
{"FQDNLookup", NULL, 0, "true"}, |
| 108 |
{"Interval", NULL, 0, NULL}, |
| 109 |
{"ReadThreads", NULL, 0, "5"}, |
| 110 |
{"WriteThreads", NULL, 0, "5"}, |
| 111 |
{"WriteQueueLimitHigh", NULL, 0, NULL}, |
| 112 |
{"WriteQueueLimitLow", NULL, 0, NULL}, |
| 113 |
{"Timeout", NULL, 0, "2"}, |
| 114 |
{"AutoLoadPlugin", NULL, 0, "false"}, |
| 115 |
{"CollectInternalStats", NULL, 0, "false"}, |
| 116 |
{"PreCacheChain", NULL, 0, "PreCache"}, |
| 117 |
{"PostCacheChain", NULL, 0, "PostCache"}, |
| 118 |
{"MaxReadInterval", NULL, 0, "86400"}}; |
| 119 |
static int cf_global_options_num = STATIC_ARRAY_SIZE(cf_global_options); |
| 120 |
|
| 121 |
static int cf_default_typesdb = 1; |
| 122 |
|
| 123 |
/* |
| 124 |
* Functions to handle register/unregister, search, and other plugin related |
| 125 |
* stuff |
| 126 |
*/ |
| 127 |
static cf_callback_t *cf_search(const char *type) { |
| 128 |
cf_callback_t *cf_cb; |
| 129 |
|
| 130 |
if (type == NULL) |
| 131 |
return NULL; |
| 132 |
|
| 133 |
for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next) |
| 134 |
if (strcasecmp(cf_cb->type, type) == 0) |
| 135 |
break; |
| 136 |
|
| 137 |
return cf_cb; |
| 138 |
} |
| 139 |
|
| 140 |
static int cf_dispatch_option(const cf_callback_t *cf_cb, oconfig_item_t *ci) { |
| 141 |
|
| 142 |
const char *plugin = cf_cb->type; |
| 143 |
const char *orig_key = ci->key; |
| 144 |
if (orig_key == NULL) |
| 145 |
return EINVAL; |
| 146 |
|
| 147 |
/* (Re)construct string value for option */ |
| 148 |
char buffer[4096]; |
| 149 |
int buffer_free = sizeof(buffer); |
| 150 |
char *buffer_ptr = buffer; |
| 151 |
|
| 152 |
for (int i = 0; i < ci->values_num; i++) { |
| 153 |
int status = -1; |
| 154 |
|
| 155 |
if (ci->values[i].type == OCONFIG_TYPE_STRING) |
| 156 |
status = |
| 157 |
ssnprintf(buffer_ptr, buffer_free, " %s", ci->values[i].value.string); |
| 158 |
else if (ci->values[i].type == OCONFIG_TYPE_NUMBER) |
| 159 |
status = ssnprintf(buffer_ptr, buffer_free, " %lf", |
| 160 |
ci->values[i].value.number); |
| 161 |
else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN) |
| 162 |
status = ssnprintf(buffer_ptr, buffer_free, " %s", |
| 163 |
ci->values[i].value.boolean ? "true" : "false"); |
| 164 |
|
| 165 |
if ((status < 0) || (status >= buffer_free)) |
| 166 |
return -1; |
| 167 |
buffer_free -= status; |
| 168 |
buffer_ptr += status; |
| 169 |
} |
| 170 |
|
| 171 |
/* skip the initial space */ |
| 172 |
const char *orig_value = buffer + 1; |
| 173 |
|
| 174 |
DEBUG("plugin = %s, key = %s, value = %s", ESCAPE_NULL(plugin), orig_key, |
| 175 |
ESCAPE_NULL(orig_value)); |
| 176 |
|
| 177 |
char *key = strdup(orig_key); |
| 178 |
if (key == NULL) |
| 179 |
return 1; |
| 180 |
|
| 181 |
char *value = strdup(orig_value); |
| 182 |
if (value == NULL) { |
| 183 |
free(key); |
| 184 |
return 2; |
| 185 |
} |
| 186 |
|
| 187 |
int ret = -1; |
| 188 |
|
| 189 |
plugin_ctx_t old_ctx = plugin_set_ctx(cf_cb->ctx); |
| 190 |
|
| 191 |
int i; |
| 192 |
for (i = 0; i < cf_cb->keys_num; i++) { |
| 193 |
if ((cf_cb->keys[i] != NULL) && (strcasecmp(cf_cb->keys[i], key) == 0)) { |
| 194 |
ret = (*cf_cb->callback)(key, value); |
| 195 |
break; |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
plugin_set_ctx(old_ctx); |
| 200 |
|
| 201 |
if (i >= cf_cb->keys_num) |
| 202 |
WARNING("Plugin `%s' did not register for value `%s'.", plugin, key); |
| 203 |
|
| 204 |
free(key); |
| 205 |
free(value); |
| 206 |
|
| 207 |
return ret; |
| 208 |
} /* int cf_dispatch_option */ |
| 209 |
|
| 210 |
static int dispatch_global_option(const oconfig_item_t *ci) { |
| 211 |
if (ci->values_num != 1) { |
| 212 |
ERROR("configfile: Global option `%s' needs exactly one argument.", |
| 213 |
ci->key); |
| 214 |
return -1; |
| 215 |
} |
| 216 |
|
| 217 |
if (ci->values[0].type == OCONFIG_TYPE_STRING) |
| 218 |
return global_option_set(ci->key, ci->values[0].value.string, 0); |
| 219 |
else if (ci->values[0].type == OCONFIG_TYPE_NUMBER) { |
| 220 |
char tmp[128]; |
| 221 |
ssnprintf(tmp, sizeof(tmp), "%lf", ci->values[0].value.number); |
| 222 |
return global_option_set(ci->key, tmp, 0); |
| 223 |
} else if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN) { |
| 224 |
if (ci->values[0].value.boolean) |
| 225 |
return global_option_set(ci->key, "true", 0); |
| 226 |
else |
| 227 |
return global_option_set(ci->key, "false", 0); |
| 228 |
} |
| 229 |
|
| 230 |
ERROR("configfile: Global option `%s' argument has unknown type.", ci->key); |
| 231 |
|
| 232 |
return -1; |
| 233 |
} /* int dispatch_global_option */ |
| 234 |
|
| 235 |
static int dispatch_value_typesdb(oconfig_item_t *ci) { |
| 236 |
assert(strcasecmp(ci->key, "TypesDB") == 0); |
| 237 |
|
| 238 |
cf_default_typesdb = 0; |
| 239 |
|
| 240 |
if (ci->values_num < 1) { |
| 241 |
ERROR("configfile: `TypesDB' needs at least one argument."); |
| 242 |
return -1; |
| 243 |
} |
| 244 |
|
| 245 |
for (int i = 0; i < ci->values_num; ++i) { |
| 246 |
if (OCONFIG_TYPE_STRING != ci->values[i].type) { |
| 247 |
WARNING("configfile: TypesDB: Skipping %i. argument which " |
| 248 |
"is not a string.", |
| 249 |
i + 1); |
| 250 |
continue; |
| 251 |
} |
| 252 |
|
| 253 |
read_types_list(ci->values[i].value.string); |
| 254 |
} |
| 255 |
return 0; |
| 256 |
} /* int dispatch_value_typesdb */ |
| 257 |
|
| 258 |
static int dispatch_value_plugindir(oconfig_item_t *ci) { |
| 259 |
assert(strcasecmp(ci->key, "PluginDir") == 0); |
| 260 |
|
| 261 |
if (ci->values_num != 1 || ci->values[0].type != OCONFIG_TYPE_STRING) { |
| 262 |
ERROR("configfile: The `PluginDir' option needs exactly one string " |
| 263 |
"argument."); |
| 264 |
return -1; |
| 265 |
} |
| 266 |
|
| 267 |
plugin_set_dir(ci->values[0].value.string); |
| 268 |
return 0; |
| 269 |
} |
| 270 |
|
| 271 |
static int dispatch_loadplugin(oconfig_item_t *ci) { |
| 272 |
bool global = false; |
| 273 |
|
| 274 |
assert(strcasecmp(ci->key, "LoadPlugin") == 0); |
| 275 |
|
| 276 |
if (ci->values_num != 1 || ci->values[0].type != OCONFIG_TYPE_STRING) { |
| 277 |
ERROR("configfile: The `LoadPlugin' block needs exactly one string " |
| 278 |
"argument."); |
| 279 |
return -1; |
| 280 |
} |
| 281 |
|
| 282 |
const char *name = ci->values[0].value.string; |
| 283 |
if (strcmp("libvirt", name) == 0) |
| 284 |
name = "virt"; |
| 285 |
|
| 286 |
/* default to the global interval set before loading this plugin */ |
| 287 |
plugin_ctx_t ctx = { |
| 288 |
.interval = cf_get_default_interval(), |
| 289 |
.name = strdup(name), |
| 290 |
}; |
| 291 |
if (ctx.name == NULL) |
| 292 |
return ENOMEM; |
| 293 |
|
| 294 |
for (int i = 0; i < ci->children_num; ++i) { |
| 295 |
oconfig_item_t *child = ci->children + i; |
| 296 |
|
| 297 |
if (strcasecmp("Globals", child->key) == 0) |
| 298 |
cf_util_get_boolean(child, &global); |
| 299 |
else if (strcasecmp("Interval", child->key) == 0) |
| 300 |
cf_util_get_cdtime(child, &ctx.interval); |
| 301 |
else if (strcasecmp("FlushInterval", child->key) == 0) |
| 302 |
cf_util_get_cdtime(child, &ctx.flush_interval); |
| 303 |
else if (strcasecmp("FlushTimeout", child->key) == 0) |
| 304 |
cf_util_get_cdtime(child, &ctx.flush_timeout); |
| 305 |
else { |
| 306 |
WARNING("Ignoring unknown LoadPlugin option \"%s\" " |
| 307 |
"for plugin \"%s\"", |
| 308 |
child->key, name); |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
plugin_ctx_t old_ctx = plugin_set_ctx(ctx); |
| 313 |
int ret_val = plugin_load(name, global); |
| 314 |
/* reset to the "global" context */ |
| 315 |
plugin_set_ctx(old_ctx); |
| 316 |
|
| 317 |
return ret_val; |
| 318 |
} /* int dispatch_value_loadplugin */ |
| 319 |
|
| 320 |
static int dispatch_value(oconfig_item_t *ci) { |
| 321 |
int ret = 0; |
| 322 |
|
| 323 |
for (int i = 0; i < cf_value_map_num; i++) |
| 324 |
if (strcasecmp(cf_value_map[i].key, ci->key) == 0) { |
| 325 |
ret = cf_value_map[i].func(ci); |
| 326 |
break; |
| 327 |
} |
| 328 |
|
| 329 |
if (ret != 0) |
| 330 |
return ret; |
| 331 |
|
| 332 |
for (int i = 0; i < cf_global_options_num; i++) |
| 333 |
if (strcasecmp(cf_global_options[i].key, ci->key) == 0) { |
| 334 |
ret = dispatch_global_option(ci); |
| 335 |
break; |
| 336 |
} |
| 337 |
|
| 338 |
return ret; |
| 339 |
} /* int dispatch_value */ |
| 340 |
|
| 341 |
static int dispatch_block_plugin(oconfig_item_t *ci) { |
| 342 |
assert(strcasecmp(ci->key, "Plugin") == 0); |
| 343 |
|
| 344 |
if (ci->values_num < 1) { |
| 345 |
ERROR("configfile: The `Plugin' block requires arguments."); |
| 346 |
return -1; |
| 347 |
} |
| 348 |
if (ci->values[0].type != OCONFIG_TYPE_STRING) { |
| 349 |
ERROR("configfile: First argument of `Plugin' block should be a string."); |
| 350 |
return -1; |
| 351 |
} |
| 352 |
|
| 353 |
const char *plugin_name = ci->values[0].value.string; |
| 354 |
if (strcmp("libvirt", plugin_name) == 0) { |
| 355 |
/* TODO(octo): Remove this legacy. */ |
| 356 |
WARNING("The \"libvirt\" plugin has been renamed to \"virt\" to avoid " |
| 357 |
"problems with the build system. " |
| 358 |
"Your configuration is still using the old name. " |
| 359 |
"Please change it to use \"virt\" as soon as possible. " |
| 360 |
"This compatibility code will go away eventually."); |
| 361 |
plugin_name = "virt"; |
| 362 |
} |
| 363 |
|
| 364 |
bool plugin_loaded = plugin_is_loaded(plugin_name); |
| 365 |
|
| 366 |
if (!plugin_loaded && IS_TRUE(global_option_get("AutoLoadPlugin"))) { |
| 367 |
plugin_ctx_t ctx = {0}; |
| 368 |
|
| 369 |
/* default to the global interval set before loading this plugin */ |
| 370 |
ctx.interval = cf_get_default_interval(); |
| 371 |
ctx.name = strdup(plugin_name); |
| 372 |
|
| 373 |
plugin_ctx_t old_ctx = plugin_set_ctx(ctx); |
| 374 |
int status = plugin_load(plugin_name, /* flags = */ false); |
| 375 |
/* reset to the "global" context */ |
| 376 |
plugin_set_ctx(old_ctx); |
| 377 |
|
| 378 |
if (status != 0) { |
| 379 |
ERROR("Automatically loading plugin `%s' failed " |
| 380 |
"with status %i.", |
| 381 |
plugin_name, status); |
| 382 |
return status; |
| 383 |
} |
| 384 |
plugin_loaded = true; |
| 385 |
} |
| 386 |
|
| 387 |
if (!plugin_loaded) { |
| 388 |
WARNING("There is configuration for the `%s' plugin, but the plugin isn't " |
| 389 |
"loaded. Please check your configuration.", |
| 390 |
plugin_name); |
| 391 |
|
| 392 |
/* Try to be backward-compatible with previous versions */ |
| 393 |
return 0; |
| 394 |
} |
| 395 |
|
| 396 |
/* Check for a complex callback first */ |
| 397 |
for (cf_complex_callback_t *cb = complex_callback_head; cb != NULL; |
| 398 |
cb = cb->next) { |
| 399 |
if (strcasecmp(plugin_name, cb->type) == 0) { |
| 400 |
plugin_ctx_t old_ctx = plugin_set_ctx(cb->ctx); |
| 401 |
int ret_val = (cb->callback(ci)); |
| 402 |
plugin_set_ctx(old_ctx); |
| 403 |
return ret_val; |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
/* Try to be backward-compatible with previous versions */ |
| 408 |
if (ci->children_num == 0) |
| 409 |
return 0; |
| 410 |
|
| 411 |
/* Hm, no complex plugin found. Dispatch the values one by one */ |
| 412 |
cf_callback_t *cf_cb = cf_search(plugin_name); |
| 413 |
if (cf_cb == NULL) { |
| 414 |
WARNING("Found a configuration for the `%s' plugin, but " |
| 415 |
"the plugin didn't register a configuration callback.", |
| 416 |
plugin_name); |
| 417 |
return -1; |
| 418 |
} |
| 419 |
|
| 420 |
for (int i = 0; i < ci->children_num; i++) { |
| 421 |
if (ci->children[i].children == NULL) { |
| 422 |
oconfig_item_t *child = ci->children + i; |
| 423 |
int ret = cf_dispatch_option(cf_cb, child); |
| 424 |
if (ret != 0) { |
| 425 |
ERROR("Plugin `%s' failed to handle option `%s', return code: %i", |
| 426 |
plugin_name, child->key, ret); |
| 427 |
return ret; |
| 428 |
} |
| 429 |
} else { |
| 430 |
WARNING("There is a `%s' block within the " |
| 431 |
"configuration for the `%s' plugin. " |
| 432 |
"The plugin only expects \"simple\" configuration options. " |
| 433 |
"Blocks are not supported. Please check your configuration.", |
| 434 |
ci->children[i].key, plugin_name); |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
return 0; |
| 439 |
} |
| 440 |
|
| 441 |
static int dispatch_block(oconfig_item_t *ci) { |
| 442 |
if (strcasecmp(ci->key, "LoadPlugin") == 0) |
| 443 |
return dispatch_loadplugin(ci); |
| 444 |
else if (strcasecmp(ci->key, "Plugin") == 0) |
| 445 |
return dispatch_block_plugin(ci); |
| 446 |
else if (strcasecmp(ci->key, "Chain") == 0) |
| 447 |
return fc_configure(ci); |
| 448 |
|
| 449 |
return 0; |
| 450 |
} |
| 451 |
|
| 452 |
static int cf_ci_replace_child(oconfig_item_t *dst, oconfig_item_t *src, |
| 453 |
int offset) { |
| 454 |
oconfig_item_t *temp; |
| 455 |
|
| 456 |
assert(offset >= 0); |
| 457 |
assert(dst->children_num > offset); |
| 458 |
|
| 459 |
/* Free the memory used by the replaced child. Usually that's the |
| 460 |
* `Include "blah"' statement. */ |
| 461 |
temp = dst->children + offset; |
| 462 |
for (int i = 0; i < temp->values_num; i++) { |
| 463 |
if (temp->values[i].type == OCONFIG_TYPE_STRING) { |
| 464 |
sfree(temp->values[i].value.string); |
| 465 |
} |
| 466 |
} |
| 467 |
sfree(temp->values); |
| 468 |
temp = NULL; |
| 469 |
|
| 470 |
/* If (src->children_num == 0) the array size is decreased. If offset |
| 471 |
* is _not_ the last element, (offset < (dst->children_num - 1)), then |
| 472 |
* we need to move the trailing elements before resizing the array. */ |
| 473 |
if ((src->children_num == 0) && (offset < (dst->children_num - 1))) { |
| 474 |
int nmemb = dst->children_num - (offset + 1); |
| 475 |
memmove(dst->children + offset, dst->children + offset + 1, |
| 476 |
sizeof(oconfig_item_t) * nmemb); |
| 477 |
} |
| 478 |
|
| 479 |
/* Resize the memory containing the children to be big enough to hold |
| 480 |
* all children. */ |
| 481 |
if (dst->children_num + src->children_num - 1 == 0) { |
| 482 |
dst->children_num = 0; |
| 483 |
return 0; |
| 484 |
} |
| 485 |
|
| 486 |
temp = |
| 487 |
realloc(dst->children, sizeof(oconfig_item_t) * |
| 488 |
(dst->children_num + src->children_num - 1)); |
| 489 |
if (temp == NULL) { |
| 490 |
ERROR("configfile: realloc failed."); |
| 491 |
return -1; |
| 492 |
} |
| 493 |
dst->children = temp; |
| 494 |
|
| 495 |
/* If there are children behind the include statement, and they have |
| 496 |
* not yet been moved because (src->children_num == 0), then move them |
| 497 |
* to the end of the list, so that the new children have room before |
| 498 |
* them. */ |
| 499 |
if ((src->children_num > 0) && ((dst->children_num - (offset + 1)) > 0)) { |
| 500 |
int nmemb = dst->children_num - (offset + 1); |
| 501 |
int old_offset = offset + 1; |
| 502 |
int new_offset = offset + src->children_num; |
| 503 |
|
| 504 |
memmove(dst->children + new_offset, dst->children + old_offset, |
| 505 |
sizeof(oconfig_item_t) * nmemb); |
| 506 |
} |
| 507 |
|
| 508 |
/* Last but not least: If there are new children, copy them to the |
| 509 |
* memory reserved for them. */ |
| 510 |
if (src->children_num > 0) { |
| 511 |
memcpy(dst->children + offset, src->children, |
| 512 |
sizeof(oconfig_item_t) * src->children_num); |
| 513 |
} |
| 514 |
|
| 515 |
/* Update the number of children. */ |
| 516 |
dst->children_num += (src->children_num - 1); |
| 517 |
|
| 518 |
return 0; |
| 519 |
} /* int cf_ci_replace_child */ |
| 520 |
|
| 521 |
static int cf_ci_append_children(oconfig_item_t *dst, oconfig_item_t *src) { |
| 522 |
oconfig_item_t *temp; |
| 523 |
|
| 524 |
if ((src == NULL) || (src->children_num == 0)) |
| 525 |
return 0; |
| 526 |
|
| 527 |
temp = realloc(dst->children, sizeof(oconfig_item_t) * |
| 528 |
(dst->children_num + src->children_num)); |
| 529 |
if (temp == NULL) { |
| 530 |
ERROR("configfile: realloc failed."); |
| 531 |
return -1; |
| 532 |
} |
| 533 |
dst->children = temp; |
| 534 |
|
| 535 |
memcpy(dst->children + dst->children_num, src->children, |
| 536 |
sizeof(oconfig_item_t) * src->children_num); |
| 537 |
dst->children_num += src->children_num; |
| 538 |
|
| 539 |
return 0; |
| 540 |
} /* int cf_ci_append_children */ |
| 541 |
|
| 542 |
#define CF_MAX_DEPTH 8 |
| 543 |
static oconfig_item_t *cf_read_generic(const char *path, const char *pattern, |
| 544 |
int depth); |
| 545 |
|
| 546 |
static int cf_include_all(oconfig_item_t *root, int depth) { |
| 547 |
for (int i = 0; i < root->children_num; i++) { |
| 548 |
oconfig_item_t *new; |
| 549 |
oconfig_item_t *old; |
| 550 |
|
| 551 |
char *pattern = NULL; |
| 552 |
|
| 553 |
if (strcasecmp(root->children[i].key, "Include") != 0) |
| 554 |
continue; |
| 555 |
|
| 556 |
old = root->children + i; |
| 557 |
|
| 558 |
if ((old->values_num != 1) || |
| 559 |
(old->values[0].type != OCONFIG_TYPE_STRING)) { |
| 560 |
ERROR("configfile: `Include' needs exactly one string argument."); |
| 561 |
continue; |
| 562 |
} |
| 563 |
|
| 564 |
for (int j = 0; j < old->children_num; ++j) { |
| 565 |
oconfig_item_t *child = old->children + j; |
| 566 |
|
| 567 |
if (strcasecmp(child->key, "Filter") == 0) |
| 568 |
cf_util_get_string(child, &pattern); |
| 569 |
else |
| 570 |
ERROR("configfile: Option `%s' not allowed in <Include> block.", |
| 571 |
child->key); |
| 572 |
} |
| 573 |
|
| 574 |
new = cf_read_generic(old->values[0].value.string, pattern, depth + 1); |
| 575 |
sfree(pattern); |
| 576 |
|
| 577 |
if (new == NULL) |
| 578 |
return -1; |
| 579 |
|
| 580 |
/* Now replace the i'th child in `root' with `new'. */ |
| 581 |
if (cf_ci_replace_child(root, new, i) < 0) { |
| 582 |
sfree(new->values); |
| 583 |
sfree(new); |
| 584 |
return -1; |
| 585 |
} |
| 586 |
|
| 587 |
/* ... and go back to the new i'th child. */ |
| 588 |
--i; |
| 589 |
|
| 590 |
sfree(new->values); |
| 591 |
sfree(new); |
| 592 |
} /* for (i = 0; i < root->children_num; i++) */ |
| 593 |
|
| 594 |
return 0; |
| 595 |
} /* int cf_include_all */ |
| 596 |
|
| 597 |
static oconfig_item_t *cf_read_file(const char *file, const char *pattern, |
| 598 |
int depth) { |
| 599 |
oconfig_item_t *root; |
| 600 |
int status; |
| 601 |
|
| 602 |
assert(depth < CF_MAX_DEPTH); |
| 603 |
|
| 604 |
if (pattern != NULL) { |
| 605 |
#if HAVE_FNMATCH_H && HAVE_LIBGEN_H |
| 606 |
char *tmp = sstrdup(file); |
| 607 |
char *filename = basename(tmp); |
| 608 |
|
| 609 |
if ((filename != NULL) && (fnmatch(pattern, filename, 0) != 0)) { |
| 610 |
DEBUG("configfile: Not including `%s' because it " |
| 611 |
"does not match pattern `%s'.", |
| 612 |
filename, pattern); |
| 613 |
free(tmp); |
| 614 |
return NULL; |
| 615 |
} |
| 616 |
|
| 617 |
free(tmp); |
| 618 |
#else |
| 619 |
ERROR("configfile: Cannot apply pattern filter '%s' " |
| 620 |
"to file '%s': functions basename() and / or " |
| 621 |
"fnmatch() not available.", |
| 622 |
pattern, file); |
| 623 |
#endif /* HAVE_FNMATCH_H && HAVE_LIBGEN_H */ |
| 624 |
} |
| 625 |
|
| 626 |
root = oconfig_parse_file(file); |
| 627 |
if (root == NULL) { |
| 628 |
ERROR("configfile: Cannot read file `%s'.", file); |
| 629 |
return NULL; |
| 630 |
} |
| 631 |
|
| 632 |
status = cf_include_all(root, depth); |
| 633 |
if (status != 0) { |
| 634 |
oconfig_free(root); |
| 635 |
return NULL; |
| 636 |
} |
| 637 |
|
| 638 |
return root; |
| 639 |
} /* oconfig_item_t *cf_read_file */ |
| 640 |
|
| 641 |
static int cf_compare_string(const void *p1, const void *p2) { |
| 642 |
return strcmp(*(const char **)p1, *(const char **)p2); |
| 643 |
} |
| 644 |
|
| 645 |
static oconfig_item_t *cf_read_dir(const char *dir, const char *pattern, |
| 646 |
int depth) { |
| 647 |
oconfig_item_t *root = NULL; |
| 648 |
DIR *dh; |
| 649 |
struct dirent *de; |
| 650 |
char **filenames = NULL; |
| 651 |
int filenames_num = 0; |
| 652 |
int status; |
| 653 |
|
| 654 |
assert(depth < CF_MAX_DEPTH); |
| 655 |
|
| 656 |
dh = opendir(dir); |
| 657 |
if (dh == NULL) { |
| 658 |
ERROR("configfile: opendir failed: %s", STRERRNO); |
| 659 |
return NULL; |
| 660 |
} |
| 661 |
|
| 662 |
root = calloc(1, sizeof(*root)); |
| 663 |
if (root == NULL) { |
| 664 |
ERROR("configfile: calloc failed."); |
| 665 |
closedir(dh); |
| 666 |
return NULL; |
| 667 |
} |
| 668 |
|
| 669 |
while ((de = readdir(dh)) != NULL) { |
| 670 |
char name[1024]; |
| 671 |
char **tmp; |
| 672 |
|
| 673 |
if ((de->d_name[0] == '.') || (de->d_name[0] == 0)) |
| 674 |
continue; |
| 675 |
|
| 676 |
status = ssnprintf(name, sizeof(name), "%s/%s", dir, de->d_name); |
| 677 |
if ((status < 0) || ((size_t)status >= sizeof(name))) { |
| 678 |
ERROR("configfile: Not including `%s/%s' because its" |
| 679 |
" name is too long.", |
| 680 |
dir, de->d_name); |
| 681 |
closedir(dh); |
| 682 |
for (int i = 0; i < filenames_num; ++i) |
| 683 |
free(filenames[i]); |
| 684 |
free(filenames); |
| 685 |
free(root); |
| 686 |
return NULL; |
| 687 |
} |
| 688 |
|
| 689 |
++filenames_num; |
| 690 |
tmp = realloc(filenames, filenames_num * sizeof(*filenames)); |
| 691 |
if (tmp == NULL) { |
| 692 |
ERROR("configfile: realloc failed."); |
| 693 |
closedir(dh); |
| 694 |
for (int i = 0; i < filenames_num - 1; ++i) |
| 695 |
free(filenames[i]); |
| 696 |
free(filenames); |
| 697 |
free(root); |
| 698 |
return NULL; |
| 699 |
} |
| 700 |
filenames = tmp; |
| 701 |
|
| 702 |
filenames[filenames_num - 1] = sstrdup(name); |
| 703 |
} |
| 704 |
|
| 705 |
if (filenames == NULL) { |
| 706 |
closedir(dh); |
| 707 |
return root; |
| 708 |
} |
| 709 |
|
| 710 |
qsort((void *)filenames, filenames_num, sizeof(*filenames), |
| 711 |
cf_compare_string); |
| 712 |
|
| 713 |
for (int i = 0; i < filenames_num; ++i) { |
| 714 |
oconfig_item_t *temp; |
| 715 |
char *name = filenames[i]; |
| 716 |
|
| 717 |
temp = cf_read_generic(name, pattern, depth); |
| 718 |
if (temp == NULL) { |
| 719 |
/* An error should already have been reported. */ |
| 720 |
sfree(name); |
| 721 |
continue; |
| 722 |
} |
| 723 |
|
| 724 |
cf_ci_append_children(root, temp); |
| 725 |
sfree(temp->children); |
| 726 |
sfree(temp); |
| 727 |
|
| 728 |
free(name); |
| 729 |
} |
| 730 |
|
| 731 |
closedir(dh); |
| 732 |
free(filenames); |
| 733 |
return root; |
| 734 |
} /* oconfig_item_t *cf_read_dir */ |
| 735 |
|
| 736 |
/* |
| 737 |
* cf_read_generic |
| 738 |
* |
| 739 |
* Path is stat'ed and either cf_read_file or cf_read_dir is called |
| 740 |
* accordingly. |
| 741 |
* |
| 742 |
* There are two versions of this function: If `wordexp' exists shell wildcards |
| 743 |
* will be expanded and the function will include all matches found. If |
| 744 |
* `wordexp' (or, more precisely, its header file) is not available the |
| 745 |
* simpler function is used which does not do any such expansion. |
| 746 |
*/ |
| 747 |
#if HAVE_WORDEXP_H |
| 748 |
static oconfig_item_t *cf_read_generic(const char *path, const char *pattern, |
| 749 |
int depth) { |
| 750 |
oconfig_item_t *root = NULL; |
| 751 |
int status; |
| 752 |
const char *path_ptr; |
| 753 |
wordexp_t we; |
| 754 |
|
| 755 |
if (depth >= CF_MAX_DEPTH) { |
| 756 |
ERROR("configfile: Not including `%s' because the maximum " |
| 757 |
"nesting depth has been reached.", |
| 758 |
path); |
| 759 |
return NULL; |
| 760 |
} |
| 761 |
|
| 762 |
status = wordexp(path, &we, WRDE_NOCMD); |
| 763 |
if (status != 0) { |
| 764 |
ERROR("configfile: wordexp (%s) failed.", path); |
| 765 |
return NULL; |
| 766 |
} |
| 767 |
|
| 768 |
root = calloc(1, sizeof(*root)); |
| 769 |
if (root == NULL) { |
| 770 |
ERROR("configfile: calloc failed."); |
| 771 |
return NULL; |
| 772 |
} |
| 773 |
|
| 774 |
/* wordexp() might return a sorted list already. That's not |
| 775 |
* documented though, so let's make sure we get what we want. */ |
| 776 |
qsort((void *)we.we_wordv, we.we_wordc, sizeof(*we.we_wordv), |
| 777 |
cf_compare_string); |
| 778 |
|
| 779 |
for (size_t i = 0; i < we.we_wordc; i++) { |
| 780 |
oconfig_item_t *temp; |
| 781 |
struct stat statbuf; |
| 782 |
|
| 783 |
path_ptr = we.we_wordv[i]; |
| 784 |
|
| 785 |
status = stat(path_ptr, &statbuf); |
| 786 |
if (status != 0) { |
| 787 |
WARNING("configfile: stat (%s) failed: %s", path_ptr, STRERRNO); |
| 788 |
continue; |
| 789 |
} |
| 790 |
|
| 791 |
if (S_ISREG(statbuf.st_mode)) |
| 792 |
temp = cf_read_file(path_ptr, pattern, depth); |
| 793 |
else if (S_ISDIR(statbuf.st_mode)) |
| 794 |
temp = cf_read_dir(path_ptr, pattern, depth); |
| 795 |
else { |
| 796 |
WARNING("configfile: %s is neither a file nor a " |
| 797 |
"directory.", |
| 798 |
path); |
| 799 |
continue; |
| 800 |
} |
| 801 |
|
| 802 |
if (temp == NULL) { |
| 803 |
oconfig_free(root); |
| 804 |
return NULL; |
| 805 |
} |
| 806 |
|
| 807 |
cf_ci_append_children(root, temp); |
| 808 |
sfree(temp->children); |
| 809 |
sfree(temp); |
| 810 |
} |
| 811 |
|
| 812 |
wordfree(&we); |
| 813 |
|
| 814 |
return root; |
| 815 |
} /* oconfig_item_t *cf_read_generic */ |
| 816 |
/* #endif HAVE_WORDEXP_H */ |
| 817 |
|
| 818 |
#else /* if !HAVE_WORDEXP_H */ |
| 819 |
static oconfig_item_t *cf_read_generic(const char *path, const char *pattern, |
| 820 |
int depth) { |
| 821 |
struct stat statbuf; |
| 822 |
int status; |
| 823 |
|
| 824 |
if (depth >= CF_MAX_DEPTH) { |
| 825 |
ERROR("configfile: Not including `%s' because the maximum " |
| 826 |
"nesting depth has been reached.", |
| 827 |
path); |
| 828 |
return NULL; |
| 829 |
} |
| 830 |
|
| 831 |
status = stat(path, &statbuf); |
| 832 |
if (status != 0) { |
| 833 |
ERROR("configfile: stat (%s) failed: %s", path, STRERRNO); |
| 834 |
return NULL; |
| 835 |
} |
| 836 |
|
| 837 |
if (S_ISREG(statbuf.st_mode)) |
| 838 |
return cf_read_file(path, pattern, depth); |
| 839 |
else if (S_ISDIR(statbuf.st_mode)) |
| 840 |
return cf_read_dir(path, pattern, depth); |
| 841 |
|
| 842 |
ERROR("configfile: %s is neither a file nor a directory.", path); |
| 843 |
return NULL; |
| 844 |
} /* oconfig_item_t *cf_read_generic */ |
| 845 |
#endif /* !HAVE_WORDEXP_H */ |
| 846 |
|
| 847 |
/* |
| 848 |
* Public functions |
| 849 |
*/ |
| 850 |
int global_option_set(const char *option, const char *value, bool from_cli) { |
| 851 |
int i; |
| 852 |
DEBUG("option = %s; value = %s;", option, value); |
| 853 |
|
| 854 |
for (i = 0; i < cf_global_options_num; i++) |
| 855 |
if (strcasecmp(cf_global_options[i].key, option) == 0) |
| 856 |
break; |
| 857 |
|
| 858 |
if (i >= cf_global_options_num) { |
| 859 |
ERROR("configfile: Cannot set unknown global option `%s'.", option); |
| 860 |
return -1; |
| 861 |
} |
| 862 |
|
| 863 |
if (cf_global_options[i].from_cli && (!from_cli)) { |
| 864 |
DEBUG("configfile: Ignoring %s `%s' option because " |
| 865 |
"it was overriden by a command-line option.", |
| 866 |
option, value); |
| 867 |
return 0; |
| 868 |
} |
| 869 |
|
| 870 |
sfree(cf_global_options[i].value); |
| 871 |
|
| 872 |
if (value != NULL) |
| 873 |
cf_global_options[i].value = strdup(value); |
| 874 |
else |
| 875 |
cf_global_options[i].value = NULL; |
| 876 |
|
| 877 |
cf_global_options[i].from_cli = from_cli; |
| 878 |
|
| 879 |
return 0; |
| 880 |
} |
| 881 |
|
| 882 |
const char *global_option_get(const char *option) { |
| 883 |
int i; |
| 884 |
for (i = 0; i < cf_global_options_num; i++) |
| 885 |
if (strcasecmp(cf_global_options[i].key, option) == 0) |
| 886 |
break; |
| 887 |
|
| 888 |
if (i >= cf_global_options_num) { |
| 889 |
ERROR("configfile: Cannot get unknown global option `%s'.", option); |
| 890 |
return NULL; |
| 891 |
} |
| 892 |
|
| 893 |
return (cf_global_options[i].value != NULL) ? cf_global_options[i].value |
| 894 |
: cf_global_options[i].def; |
| 895 |
} /* char *global_option_get */ |
| 896 |
|
| 897 |
long global_option_get_long(const char *option, long default_value) { |
| 898 |
const char *str; |
| 899 |
long value; |
| 900 |
|
| 901 |
str = global_option_get(option); |
| 902 |
if (NULL == str) |
| 903 |
return default_value; |
| 904 |
|
| 905 |
errno = 0; |
| 906 |
value = strtol(str, /* endptr = */ NULL, /* base = */ 0); |
| 907 |
if (errno != 0) |
| 908 |
return default_value; |
| 909 |
|
| 910 |
return value; |
| 911 |
} /* char *global_option_get_long */ |
| 912 |
|
| 913 |
cdtime_t global_option_get_time(const char *name, cdtime_t def) /* {{{ */ |
| 914 |
{ |
| 915 |
char const *optstr; |
| 916 |
char *endptr = NULL; |
| 917 |
double v; |
| 918 |
|
| 919 |
optstr = global_option_get(name); |
| 920 |
if (optstr == NULL) |
| 921 |
return def; |
| 922 |
|
| 923 |
errno = 0; |
| 924 |
v = strtod(optstr, &endptr); |
| 925 |
if ((endptr == NULL) || (*endptr != 0) || (errno != 0)) |
| 926 |
return def; |
| 927 |
else if (v <= 0.0) |
| 928 |
return def; |
| 929 |
|
| 930 |
return DOUBLE_TO_CDTIME_T(v); |
| 931 |
} /* }}} cdtime_t global_option_get_time */ |
| 932 |
|
| 933 |
cdtime_t cf_get_default_interval(void) { |
| 934 |
return global_option_get_time("Interval", |
| 935 |
DOUBLE_TO_CDTIME_T(COLLECTD_DEFAULT_INTERVAL)); |
| 936 |
} |
| 937 |
|
| 938 |
void cf_unregister(const char *type) { |
| 939 |
for (cf_callback_t *prev = NULL, *this = first_callback; this != NULL; |
| 940 |
prev = this, this = this->next) |
| 941 |
if (strcasecmp(this->type, type) == 0) { |
| 942 |
if (prev == NULL) |
| 943 |
first_callback = this->next; |
| 944 |
else |
| 945 |
prev->next = this->next; |
| 946 |
|
| 947 |
free(this); |
| 948 |
break; |
| 949 |
} |
| 950 |
} /* void cf_unregister */ |
| 951 |
|
| 952 |
void cf_unregister_complex(const char *type) { |
| 953 |
for (cf_complex_callback_t *prev = NULL, *this = complex_callback_head; |
| 954 |
this != NULL; prev = this, this = this->next) |
| 955 |
if (strcasecmp(this->type, type) == 0) { |
| 956 |
if (prev == NULL) |
| 957 |
complex_callback_head = this->next; |
| 958 |
else |
| 959 |
prev->next = this->next; |
| 960 |
|
| 961 |
sfree(this->type); |
| 962 |
sfree(this); |
| 963 |
break; |
| 964 |
} |
| 965 |
} /* void cf_unregister */ |
| 966 |
|
| 967 |
void cf_register(const char *type, int (*callback)(const char *, const char *), |
| 968 |
const char **keys, int keys_num) { |
| 969 |
cf_callback_t *cf_cb; |
| 970 |
|
| 971 |
/* Remove this module from the list, if it already exists */ |
| 972 |
cf_unregister(type); |
| 973 |
|
| 974 |
/* This pointer will be free'd in `cf_unregister' */ |
| 975 |
if ((cf_cb = malloc(sizeof(*cf_cb))) == NULL) |
| 976 |
return; |
| 977 |
|
| 978 |
cf_cb->type = type; |
| 979 |
cf_cb->callback = callback; |
| 980 |
cf_cb->keys = keys; |
| 981 |
cf_cb->keys_num = keys_num; |
| 982 |
cf_cb->ctx = plugin_get_ctx(); |
| 983 |
|
| 984 |
cf_cb->next = first_callback; |
| 985 |
first_callback = cf_cb; |
| 986 |
} /* void cf_register */ |
| 987 |
|
| 988 |
int cf_register_complex(const char *type, int (*callback)(oconfig_item_t *)) { |
| 989 |
cf_complex_callback_t *new; |
| 990 |
|
| 991 |
new = malloc(sizeof(*new)); |
| 992 |
if (new == NULL) |
| 993 |
return -1; |
| 994 |
|
| 995 |
new->type = strdup(type); |
| 996 |
if (new->type == NULL) { |
| 997 |
sfree(new); |
| 998 |
return -1; |
| 999 |
} |
| 1000 |
|
| 1001 |
new->callback = callback; |
| 1002 |
new->next = NULL; |
| 1003 |
|
| 1004 |
new->ctx = plugin_get_ctx(); |
| 1005 |
|
| 1006 |
if (complex_callback_head == NULL) { |
| 1007 |
complex_callback_head = new; |
| 1008 |
} else { |
| 1009 |
cf_complex_callback_t *last = complex_callback_head; |
| 1010 |
while (last->next != NULL) |
| 1011 |
last = last->next; |
| 1012 |
last->next = new; |
| 1013 |
} |
| 1014 |
|
| 1015 |
return 0; |
| 1016 |
} /* int cf_register_complex */ |
| 1017 |
|
| 1018 |
int cf_read(const char *filename) { |
| 1019 |
oconfig_item_t *conf; |
| 1020 |
int ret = 0; |
| 1021 |
|
| 1022 |
conf = cf_read_generic(filename, /* pattern = */ NULL, /* depth = */ 0); |
| 1023 |
if (conf == NULL) { |
| 1024 |
ERROR("Unable to read config file %s.", filename); |
| 1025 |
return -1; |
| 1026 |
} else if (conf->children_num == 0) { |
| 1027 |
ERROR("Configuration file %s is empty.", filename); |
| 1028 |
oconfig_free(conf); |
| 1029 |
return -1; |
| 1030 |
} |
| 1031 |
|
| 1032 |
for (int i = 0; i < conf->children_num; i++) { |
| 1033 |
if (conf->children[i].children == NULL) { |
| 1034 |
if (dispatch_value(conf->children + i) != 0) |
| 1035 |
ret = -1; |
| 1036 |
} else { |
| 1037 |
if (dispatch_block(conf->children + i) != 0) |
| 1038 |
ret = -1; |
| 1039 |
} |
| 1040 |
} |
| 1041 |
|
| 1042 |
oconfig_free(conf); |
| 1043 |
|
| 1044 |
/* Read the default types.db if no `TypesDB' option was given. */ |
| 1045 |
if (cf_default_typesdb) { |
| 1046 |
if (read_types_list(PKGDATADIR "/types.db") != 0) |
| 1047 |
ret = -1; |
| 1048 |
} |
| 1049 |
|
| 1050 |
return ret; |
| 1051 |
|
| 1052 |
} /* int cf_read */ |
| 1053 |
|
| 1054 |
/* Assures the config option is a string, duplicates it and returns the copy in |
| 1055 |
* "ret_string". If necessary "*ret_string" is freed first. Returns zero upon |
| 1056 |
* success. */ |
| 1057 |
int cf_util_get_string(const oconfig_item_t *ci, char **ret_string) /* {{{ */ |
| 1058 |
{ |
| 1059 |
if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) { |
| 1060 |
P_ERROR("The `%s' option requires exactly one string argument.", ci->key); |
| 1061 |
return -1; |
| 1062 |
} |
| 1063 |
|
| 1064 |
char *string = strdup(ci->values[0].value.string); |
| 1065 |
if (string == NULL) |
| 1066 |
return -1; |
| 1067 |
|
| 1068 |
if (*ret_string != NULL) |
| 1069 |
sfree(*ret_string); |
| 1070 |
*ret_string = string; |
| 1071 |
|
| 1072 |
return 0; |
| 1073 |
} /* }}} int cf_util_get_string */ |
| 1074 |
|
| 1075 |
/* Assures the config option is a string and copies it to the provided buffer. |
| 1076 |
* Assures NUL-termination. */ |
| 1077 |
int cf_util_get_string_buffer(const oconfig_item_t *ci, char *buffer, /* {{{ */ |
| 1078 |
size_t buffer_size) { |
| 1079 |
if ((ci == NULL) || (buffer == NULL) || (buffer_size < 1)) |
| 1080 |
return EINVAL; |
| 1081 |
|
| 1082 |
if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) { |
| 1083 |
P_ERROR("The `%s' option requires exactly one string argument.", ci->key); |
| 1084 |
return -1; |
| 1085 |
} |
| 1086 |
|
| 1087 |
strncpy(buffer, ci->values[0].value.string, buffer_size); |
| 1088 |
buffer[buffer_size - 1] = '\0'; |
| 1089 |
|
| 1090 |
return 0; |
| 1091 |
} /* }}} int cf_util_get_string_buffer */ |
| 1092 |
|
| 1093 |
/* Assures the config option is a number and returns it as an int. */ |
| 1094 |
int cf_util_get_int(const oconfig_item_t *ci, int *ret_value) /* {{{ */ |
| 1095 |
{ |
| 1096 |
if ((ci == NULL) || (ret_value == NULL)) |
| 1097 |
return EINVAL; |
| 1098 |
|
| 1099 |
if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER)) { |
| 1100 |
P_ERROR("The `%s' option requires exactly one numeric argument.", ci->key); |
| 1101 |
return -1; |
| 1102 |
} |
| 1103 |
|
| 1104 |
*ret_value = (int)ci->values[0].value.number; |
| 1105 |
|
| 1106 |
return 0; |
| 1107 |
} /* }}} int cf_util_get_int */ |
| 1108 |
|
| 1109 |
int cf_util_get_double(const oconfig_item_t *ci, double *ret_value) /* {{{ */ |
| 1110 |
{ |
| 1111 |
if ((ci == NULL) || (ret_value == NULL)) |
| 1112 |
return EINVAL; |
| 1113 |
|
| 1114 |
if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER)) { |
| 1115 |
P_ERROR("The `%s' option requires exactly one numeric argument.", ci->key); |
| 1116 |
return -1; |
| 1117 |
} |
| 1118 |
|
| 1119 |
*ret_value = ci->values[0].value.number; |
| 1120 |
|
| 1121 |
return 0; |
| 1122 |
} /* }}} int cf_util_get_double */ |
| 1123 |
|
| 1124 |
int cf_util_get_boolean(const oconfig_item_t *ci, bool *ret_bool) /* {{{ */ |
| 1125 |
{ |
| 1126 |
if ((ci == NULL) || (ret_bool == NULL)) |
| 1127 |
return EINVAL; |
| 1128 |
|
| 1129 |
if ((ci->values_num != 1) || ((ci->values[0].type != OCONFIG_TYPE_BOOLEAN) && |
| 1130 |
(ci->values[0].type != OCONFIG_TYPE_STRING))) { |
| 1131 |
P_ERROR("The `%s' option requires exactly one boolean argument.", ci->key); |
| 1132 |
return -1; |
| 1133 |
} |
| 1134 |
|
| 1135 |
switch (ci->values[0].type) { |
| 1136 |
case OCONFIG_TYPE_BOOLEAN: |
| 1137 |
*ret_bool = ci->values[0].value.boolean ? true : false; |
| 1138 |
break; |
| 1139 |
case OCONFIG_TYPE_STRING: |
| 1140 |
P_WARNING("Using string value `%s' for boolean option `%s' is deprecated " |
| 1141 |
"and will be removed in future releases. Use unquoted true or " |
| 1142 |
"false instead.", |
| 1143 |
ci->values[0].value.string, ci->key); |
| 1144 |
|
| 1145 |
if (IS_TRUE(ci->values[0].value.string)) |
| 1146 |
*ret_bool = true; |
| 1147 |
else if (IS_FALSE(ci->values[0].value.string)) |
| 1148 |
*ret_bool = false; |
| 1149 |
else { |
| 1150 |
P_ERROR("Cannot parse string value `%s' of the `%s' option as a boolean " |
| 1151 |
"value.", |
| 1152 |
ci->values[0].value.string, ci->key); |
| 1153 |
return -1; |
| 1154 |
} |
| 1155 |
break; |
| 1156 |
} |
| 1157 |
|
| 1158 |
return 0; |
| 1159 |
} /* }}} int cf_util_get_boolean */ |
| 1160 |
|
| 1161 |
int cf_util_get_flag(const oconfig_item_t *ci, /* {{{ */ |
| 1162 |
unsigned int *ret_value, unsigned int flag) { |
| 1163 |
int status; |
| 1164 |
|
| 1165 |
if (ret_value == NULL) |
| 1166 |
return EINVAL; |
| 1167 |
|
| 1168 |
bool b = false; |
| 1169 |
status = cf_util_get_boolean(ci, &b); |
| 1170 |
if (status != 0) |
| 1171 |
return status; |
| 1172 |
|
| 1173 |
if (b) { |
| 1174 |
*ret_value |= flag; |
| 1175 |
} else { |
| 1176 |
*ret_value &= ~flag; |
| 1177 |
} |
| 1178 |
|
| 1179 |
return 0; |
| 1180 |
} /* }}} int cf_util_get_flag */ |
| 1181 |
|
| 1182 |
/* Assures that the config option is a string or a number if the correct range |
| 1183 |
* of 1-65535. The string is then converted to a port number using |
| 1184 |
* `service_name_to_port_number' and returned. |
| 1185 |
* Returns the port number in the range [1-65535] or less than zero upon |
| 1186 |
* failure. */ |
| 1187 |
int cf_util_get_port_number(const oconfig_item_t *ci) /* {{{ */ |
| 1188 |
{ |
| 1189 |
int tmp; |
| 1190 |
|
| 1191 |
if ((ci->values_num != 1) || ((ci->values[0].type != OCONFIG_TYPE_STRING) && |
| 1192 |
(ci->values[0].type != OCONFIG_TYPE_NUMBER))) { |
| 1193 |
P_ERROR("The `%s' option requires exactly one string argument.", ci->key); |
| 1194 |
return -1; |
| 1195 |
} |
| 1196 |
|
| 1197 |
if (ci->values[0].type == OCONFIG_TYPE_STRING) |
| 1198 |
return service_name_to_port_number(ci->values[0].value.string); |
| 1199 |
|
| 1200 |
assert(ci->values[0].type == OCONFIG_TYPE_NUMBER); |
| 1201 |
tmp = (int)(ci->values[0].value.number + 0.5); |
| 1202 |
if ((tmp < 1) || (tmp > 65535)) { |
| 1203 |
P_ERROR("The `%s' option requires a service name or a port number. The " |
| 1204 |
"number you specified, %i, is not in the valid range of 1-65535.", |
| 1205 |
ci->key, tmp); |
| 1206 |
return -1; |
| 1207 |
} |
| 1208 |
|
| 1209 |
return tmp; |
| 1210 |
} /* }}} int cf_util_get_port_number */ |
| 1211 |
|
| 1212 |
int cf_util_get_service(const oconfig_item_t *ci, char **ret_string) /* {{{ */ |
| 1213 |
{ |
| 1214 |
int port; |
| 1215 |
char *service; |
| 1216 |
int status; |
| 1217 |
|
| 1218 |
if (ci->values_num != 1) { |
| 1219 |
P_ERROR("The `%s` option requires exactly one argument.", ci->key); |
| 1220 |
return -1; |
| 1221 |
} |
| 1222 |
|
| 1223 |
if (ci->values[0].type == OCONFIG_TYPE_STRING) |
| 1224 |
return cf_util_get_string(ci, ret_string); |
| 1225 |
if (ci->values[0].type != OCONFIG_TYPE_NUMBER) { |
| 1226 |
P_ERROR("The `%s` option requires exactly one string or numeric argument.", |
| 1227 |
ci->key); |
| 1228 |
} |
| 1229 |
|
| 1230 |
port = 0; |
| 1231 |
status = cf_util_get_int(ci, &port); |
| 1232 |
if (status != 0) |
| 1233 |
return status; |
| 1234 |
else if ((port < 1) || (port > 65535)) { |
| 1235 |
P_ERROR("The port number given for the `%s` option is out of range (%i).", |
| 1236 |
ci->key, port); |
| 1237 |
return -1; |
| 1238 |
} |
| 1239 |
|
| 1240 |
service = malloc(6); |
| 1241 |
if (service == NULL) { |
| 1242 |
P_ERROR("cf_util_get_service: Out of memory."); |
| 1243 |
return -1; |
| 1244 |
} |
| 1245 |
ssnprintf(service, 6, "%i", port); |
| 1246 |
|
| 1247 |
sfree(*ret_string); |
| 1248 |
*ret_string = service; |
| 1249 |
|
| 1250 |
return 0; |
| 1251 |
} /* }}} int cf_util_get_service */ |
| 1252 |
|
| 1253 |
int cf_util_get_cdtime(const oconfig_item_t *ci, cdtime_t *ret_value) /* {{{ */ |
| 1254 |
{ |
| 1255 |
if ((ci == NULL) || (ret_value == NULL)) |
| 1256 |
return EINVAL; |
| 1257 |
|
| 1258 |
if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER)) { |
| 1259 |
P_ERROR("The `%s' option requires exactly one numeric argument.", ci->key); |
| 1260 |
return -1; |
| 1261 |
} |
| 1262 |
|
| 1263 |
if (ci->values[0].value.number < 0.0) { |
| 1264 |
P_ERROR("The numeric argument of the `%s' option must not be negative.", |
| 1265 |
ci->key); |
| 1266 |
return -1; |
| 1267 |
} |
| 1268 |
|
| 1269 |
*ret_value = DOUBLE_TO_CDTIME_T(ci->values[0].value.number); |
| 1270 |
|
| 1271 |
return 0; |
| 1272 |
} /* }}} int cf_util_get_cdtime */ |