| 1 |
/** |
| 2 |
* collectd - src/filter_chain.c |
| 3 |
* Copyright (C) 2008-2010 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 |
**/ |
| 26 |
|
| 27 |
#include "collectd.h" |
| 28 |
|
| 29 |
#include "configfile.h" |
| 30 |
#include "filter_chain.h" |
| 31 |
#include "plugin.h" |
| 32 |
#include "utils/common/common.h" |
| 33 |
#include "utils_complain.h" |
| 34 |
|
| 35 |
/* |
| 36 |
* Data types |
| 37 |
*/ |
| 38 |
/* List of matches, used in fc_rule_t and for the global `match_list_head' |
| 39 |
* variable. */ |
| 40 |
struct fc_match_s; |
| 41 |
typedef struct fc_match_s fc_match_t; /* {{{ */ |
| 42 |
struct fc_match_s { |
| 43 |
char name[DATA_MAX_NAME_LEN]; |
| 44 |
match_proc_t proc; |
| 45 |
void *user_data; |
| 46 |
fc_match_t *next; |
| 47 |
}; /* }}} */ |
| 48 |
|
| 49 |
/* List of targets, used in fc_rule_t and for the global `target_list_head' |
| 50 |
* variable. */ |
| 51 |
struct fc_target_s; |
| 52 |
typedef struct fc_target_s fc_target_t; /* {{{ */ |
| 53 |
struct fc_target_s { |
| 54 |
char name[DATA_MAX_NAME_LEN]; |
| 55 |
void *user_data; |
| 56 |
target_proc_t proc; |
| 57 |
fc_target_t *next; |
| 58 |
}; /* }}} */ |
| 59 |
|
| 60 |
/* List of rules, used in fc_chain_t */ |
| 61 |
struct fc_rule_s; |
| 62 |
typedef struct fc_rule_s fc_rule_t; /* {{{ */ |
| 63 |
struct fc_rule_s { |
| 64 |
char name[DATA_MAX_NAME_LEN]; |
| 65 |
fc_match_t *matches; |
| 66 |
fc_target_t *targets; |
| 67 |
fc_rule_t *next; |
| 68 |
}; /* }}} */ |
| 69 |
|
| 70 |
/* List of chains, used for `chain_list_head' */ |
| 71 |
struct fc_chain_s /* {{{ */ |
| 72 |
{ |
| 73 |
char name[DATA_MAX_NAME_LEN]; |
| 74 |
fc_rule_t *rules; |
| 75 |
fc_target_t *targets; |
| 76 |
fc_chain_t *next; |
| 77 |
}; /* }}} */ |
| 78 |
|
| 79 |
/* Writer configuration. */ |
| 80 |
struct fc_writer_s; |
| 81 |
typedef struct fc_writer_s fc_writer_t; /* {{{ */ |
| 82 |
struct fc_writer_s { |
| 83 |
char *plugin; |
| 84 |
c_complain_t complaint; |
| 85 |
}; /* }}} */ |
| 86 |
|
| 87 |
/* |
| 88 |
* Global variables |
| 89 |
*/ |
| 90 |
static fc_match_t *match_list_head; |
| 91 |
static fc_target_t *target_list_head; |
| 92 |
static fc_chain_t *chain_list_head; |
| 93 |
|
| 94 |
/* |
| 95 |
* Private functions |
| 96 |
*/ |
| 97 |
static void fc_free_matches(fc_match_t *m) /* {{{ */ |
| 98 |
{ |
| 99 |
if (m == NULL) |
| 100 |
return; |
| 101 |
|
| 102 |
if (m->proc.destroy != NULL) |
| 103 |
(*m->proc.destroy)(&m->user_data); |
| 104 |
else if (m->user_data != NULL) { |
| 105 |
ERROR("Filter subsystem: fc_free_matches: There is user data, but no " |
| 106 |
"destroy functions has been specified. " |
| 107 |
"Memory will probably be lost!"); |
| 108 |
} |
| 109 |
|
| 110 |
if (m->next != NULL) |
| 111 |
fc_free_matches(m->next); |
| 112 |
|
| 113 |
free(m); |
| 114 |
} /* }}} void fc_free_matches */ |
| 115 |
|
| 116 |
static void fc_free_targets(fc_target_t *t) /* {{{ */ |
| 117 |
{ |
| 118 |
if (t == NULL) |
| 119 |
return; |
| 120 |
|
| 121 |
if (t->proc.destroy != NULL) |
| 122 |
(*t->proc.destroy)(&t->user_data); |
| 123 |
else if (t->user_data != NULL) { |
| 124 |
ERROR("Filter subsystem: fc_free_targets: There is user data, but no " |
| 125 |
"destroy functions has been specified. " |
| 126 |
"Memory will probably be lost!"); |
| 127 |
} |
| 128 |
|
| 129 |
if (t->next != NULL) |
| 130 |
fc_free_targets(t->next); |
| 131 |
|
| 132 |
free(t); |
| 133 |
} /* }}} void fc_free_targets */ |
| 134 |
|
| 135 |
static void fc_free_rules(fc_rule_t *r) /* {{{ */ |
| 136 |
{ |
| 137 |
if (r == NULL) |
| 138 |
return; |
| 139 |
|
| 140 |
fc_free_matches(r->matches); |
| 141 |
fc_free_targets(r->targets); |
| 142 |
|
| 143 |
if (r->next != NULL) |
| 144 |
fc_free_rules(r->next); |
| 145 |
|
| 146 |
free(r); |
| 147 |
} /* }}} void fc_free_rules */ |
| 148 |
|
| 149 |
static void fc_free_chains(fc_chain_t *c) /* {{{ */ |
| 150 |
{ |
| 151 |
if (c == NULL) |
| 152 |
return; |
| 153 |
|
| 154 |
fc_free_rules(c->rules); |
| 155 |
fc_free_targets(c->targets); |
| 156 |
|
| 157 |
if (c->next != NULL) |
| 158 |
fc_free_chains(c->next); |
| 159 |
|
| 160 |
free(c); |
| 161 |
} /* }}} void fc_free_chains */ |
| 162 |
|
| 163 |
static char *fc_strdup(const char *orig) /* {{{ */ |
| 164 |
{ |
| 165 |
size_t sz; |
| 166 |
char *dest; |
| 167 |
|
| 168 |
if (orig == NULL) |
| 169 |
return NULL; |
| 170 |
|
| 171 |
sz = strlen(orig) + 1; |
| 172 |
dest = malloc(sz); |
| 173 |
if (dest == NULL) |
| 174 |
return NULL; |
| 175 |
|
| 176 |
memcpy(dest, orig, sz); |
| 177 |
|
| 178 |
return dest; |
| 179 |
} /* }}} char *fc_strdup */ |
| 180 |
|
| 181 |
/* |
| 182 |
* Configuration. |
| 183 |
* |
| 184 |
* The configuration looks somewhat like this: |
| 185 |
* |
| 186 |
* <Chain "PreCache"> |
| 187 |
* <Rule> |
| 188 |
* <Match "regex"> |
| 189 |
* Plugin "^mysql$" |
| 190 |
* Type "^mysql_command$" |
| 191 |
* TypeInstance "^show_" |
| 192 |
* </Match> |
| 193 |
* <Target "drop"> |
| 194 |
* </Target> |
| 195 |
* </Rule> |
| 196 |
* |
| 197 |
* <Target "write"> |
| 198 |
* Plugin "rrdtool" |
| 199 |
* </Target> |
| 200 |
* </Chain> |
| 201 |
*/ |
| 202 |
static int fc_config_add_match(fc_match_t **matches_head, /* {{{ */ |
| 203 |
oconfig_item_t *ci) { |
| 204 |
fc_match_t *m; |
| 205 |
fc_match_t *ptr; |
| 206 |
int status; |
| 207 |
|
| 208 |
if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) { |
| 209 |
WARNING("Filter subsystem: `Match' blocks require " |
| 210 |
"exactly one string argument."); |
| 211 |
return -1; |
| 212 |
} |
| 213 |
|
| 214 |
ptr = match_list_head; |
| 215 |
while (ptr != NULL) { |
| 216 |
if (strcasecmp(ptr->name, ci->values[0].value.string) == 0) |
| 217 |
break; |
| 218 |
ptr = ptr->next; |
| 219 |
} |
| 220 |
|
| 221 |
if (ptr == NULL && IS_TRUE(global_option_get("AutoLoadPlugin"))) { |
| 222 |
char plugin_name[DATA_MAX_NAME_LEN]; |
| 223 |
|
| 224 |
status = ssnprintf(plugin_name, sizeof(plugin_name), "match_%s", |
| 225 |
ci->values[0].value.string); |
| 226 |
if ((status < 0) || ((size_t)status >= sizeof(plugin_name))) { |
| 227 |
ERROR("Automatically loading plugin \"match_%s\" failed:" |
| 228 |
" plugin name would have been truncated.", |
| 229 |
ci->values[0].value.string); |
| 230 |
return -1; |
| 231 |
} |
| 232 |
|
| 233 |
status = plugin_load(plugin_name, /* flags = */ 0); |
| 234 |
if (status != 0) { |
| 235 |
ERROR("Automatically loading plugin \"%s\" failed " |
| 236 |
"with status %i.", |
| 237 |
plugin_name, status); |
| 238 |
return status; |
| 239 |
} |
| 240 |
|
| 241 |
ptr = match_list_head; |
| 242 |
while (ptr != NULL) { |
| 243 |
if (strcasecmp(ptr->name, ci->values[0].value.string) == 0) |
| 244 |
break; |
| 245 |
ptr = ptr->next; |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
if (ptr == NULL) { |
| 250 |
WARNING("Filter subsystem: Cannot find a \"%s\" match. " |
| 251 |
"Did you load the appropriate plugin?", |
| 252 |
ci->values[0].value.string); |
| 253 |
return -1; |
| 254 |
} |
| 255 |
|
| 256 |
m = calloc(1, sizeof(*m)); |
| 257 |
if (m == NULL) { |
| 258 |
ERROR("fc_config_add_match: calloc failed."); |
| 259 |
return -1; |
| 260 |
} |
| 261 |
|
| 262 |
sstrncpy(m->name, ptr->name, sizeof(m->name)); |
| 263 |
memcpy(&m->proc, &ptr->proc, sizeof(m->proc)); |
| 264 |
m->user_data = NULL; |
| 265 |
m->next = NULL; |
| 266 |
|
| 267 |
if (m->proc.create != NULL) { |
| 268 |
status = (*m->proc.create)(ci, &m->user_data); |
| 269 |
if (status != 0) { |
| 270 |
WARNING("Filter subsystem: Failed to create a %s match.", m->name); |
| 271 |
fc_free_matches(m); |
| 272 |
return -1; |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
if (*matches_head != NULL) { |
| 277 |
ptr = *matches_head; |
| 278 |
while (ptr->next != NULL) |
| 279 |
ptr = ptr->next; |
| 280 |
|
| 281 |
ptr->next = m; |
| 282 |
} else { |
| 283 |
*matches_head = m; |
| 284 |
} |
| 285 |
|
| 286 |
return 0; |
| 287 |
} /* }}} int fc_config_add_match */ |
| 288 |
|
| 289 |
static int fc_config_add_target(fc_target_t **targets_head, /* {{{ */ |
| 290 |
oconfig_item_t *ci) { |
| 291 |
fc_target_t *t; |
| 292 |
fc_target_t *ptr; |
| 293 |
int status; |
| 294 |
|
| 295 |
if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) { |
| 296 |
WARNING("Filter subsystem: `Target' blocks require " |
| 297 |
"exactly one string argument."); |
| 298 |
return -1; |
| 299 |
} |
| 300 |
|
| 301 |
ptr = target_list_head; |
| 302 |
while (ptr != NULL) { |
| 303 |
if (strcasecmp(ptr->name, ci->values[0].value.string) == 0) |
| 304 |
break; |
| 305 |
ptr = ptr->next; |
| 306 |
} |
| 307 |
|
| 308 |
if (ptr == NULL && IS_TRUE(global_option_get("AutoLoadPlugin"))) { |
| 309 |
char plugin_name[DATA_MAX_NAME_LEN]; |
| 310 |
|
| 311 |
status = ssnprintf(plugin_name, sizeof(plugin_name), "target_%s", |
| 312 |
ci->values[0].value.string); |
| 313 |
if ((status < 0) || ((size_t)status >= sizeof(plugin_name))) { |
| 314 |
ERROR("Automatically loading plugin \"target_%s\" failed:" |
| 315 |
" plugin name would have been truncated.", |
| 316 |
ci->values[0].value.string); |
| 317 |
return -1; |
| 318 |
} |
| 319 |
|
| 320 |
status = plugin_load(plugin_name, /* flags = */ 0); |
| 321 |
if (status != 0) { |
| 322 |
ERROR("Automatically loading plugin \"%s\" failed " |
| 323 |
"with status %i.", |
| 324 |
plugin_name, status); |
| 325 |
return status; |
| 326 |
} |
| 327 |
|
| 328 |
ptr = target_list_head; |
| 329 |
while (ptr != NULL) { |
| 330 |
if (strcasecmp(ptr->name, ci->values[0].value.string) == 0) |
| 331 |
break; |
| 332 |
ptr = ptr->next; |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
if (ptr == NULL) { |
| 337 |
WARNING("Filter subsystem: Cannot find a \"%s\" target. " |
| 338 |
"Did you load the appropriate plugin?", |
| 339 |
ci->values[0].value.string); |
| 340 |
return -1; |
| 341 |
} |
| 342 |
|
| 343 |
t = calloc(1, sizeof(*t)); |
| 344 |
if (t == NULL) { |
| 345 |
ERROR("fc_config_add_target: calloc failed."); |
| 346 |
return -1; |
| 347 |
} |
| 348 |
|
| 349 |
sstrncpy(t->name, ptr->name, sizeof(t->name)); |
| 350 |
memcpy(&t->proc, &ptr->proc, sizeof(t->proc)); |
| 351 |
t->user_data = NULL; |
| 352 |
t->next = NULL; |
| 353 |
|
| 354 |
if (t->proc.create != NULL) { |
| 355 |
status = (*t->proc.create)(ci, &t->user_data); |
| 356 |
if (status != 0) { |
| 357 |
WARNING("Filter subsystem: Failed to create a %s target.", t->name); |
| 358 |
fc_free_targets(t); |
| 359 |
return -1; |
| 360 |
} |
| 361 |
} else { |
| 362 |
t->user_data = NULL; |
| 363 |
} |
| 364 |
|
| 365 |
if (*targets_head != NULL) { |
| 366 |
ptr = *targets_head; |
| 367 |
while (ptr->next != NULL) |
| 368 |
ptr = ptr->next; |
| 369 |
|
| 370 |
ptr->next = t; |
| 371 |
} else { |
| 372 |
*targets_head = t; |
| 373 |
} |
| 374 |
|
| 375 |
return 0; |
| 376 |
} /* }}} int fc_config_add_target */ |
| 377 |
|
| 378 |
static int fc_config_add_rule(fc_chain_t *chain, /* {{{ */ |
| 379 |
oconfig_item_t *ci) { |
| 380 |
fc_rule_t *rule; |
| 381 |
char rule_name[2 * DATA_MAX_NAME_LEN] = "Unnamed rule"; |
| 382 |
int status = 0; |
| 383 |
|
| 384 |
if (ci->values_num > 1) { |
| 385 |
WARNING("Filter subsystem: `Rule' blocks have at most one argument."); |
| 386 |
return -1; |
| 387 |
} else if ((ci->values_num == 1) && |
| 388 |
(ci->values[0].type != OCONFIG_TYPE_STRING)) { |
| 389 |
WARNING("Filter subsystem: `Rule' blocks expect one string argument " |
| 390 |
"or no argument at all."); |
| 391 |
return -1; |
| 392 |
} |
| 393 |
|
| 394 |
rule = calloc(1, sizeof(*rule)); |
| 395 |
if (rule == NULL) { |
| 396 |
ERROR("fc_config_add_rule: calloc failed."); |
| 397 |
return -1; |
| 398 |
} |
| 399 |
|
| 400 |
if (ci->values_num == 1) { |
| 401 |
sstrncpy(rule->name, ci->values[0].value.string, sizeof(rule->name)); |
| 402 |
snprintf(rule_name, sizeof(rule_name), "Rule \"%s\"", |
| 403 |
ci->values[0].value.string); |
| 404 |
} |
| 405 |
|
| 406 |
for (int i = 0; i < ci->children_num; i++) { |
| 407 |
oconfig_item_t *option = ci->children + i; |
| 408 |
|
| 409 |
if (strcasecmp("Match", option->key) == 0) |
| 410 |
status = fc_config_add_match(&rule->matches, option); |
| 411 |
else if (strcasecmp("Target", option->key) == 0) |
| 412 |
status = fc_config_add_target(&rule->targets, option); |
| 413 |
else { |
| 414 |
WARNING("Filter subsystem: %s: Option `%s' not allowed " |
| 415 |
"inside a <Rule> block.", |
| 416 |
rule_name, option->key); |
| 417 |
status = -1; |
| 418 |
} |
| 419 |
|
| 420 |
if (status != 0) |
| 421 |
break; |
| 422 |
} /* for (ci->children) */ |
| 423 |
|
| 424 |
/* Additional sanity checking. */ |
| 425 |
while (status == 0) { |
| 426 |
if (rule->targets == NULL) { |
| 427 |
WARNING("Filter subsystem: %s: No target has been specified.", rule_name); |
| 428 |
status = -1; |
| 429 |
break; |
| 430 |
} |
| 431 |
|
| 432 |
break; |
| 433 |
} /* while (status == 0) */ |
| 434 |
|
| 435 |
if (status != 0) { |
| 436 |
fc_free_rules(rule); |
| 437 |
return -1; |
| 438 |
} |
| 439 |
|
| 440 |
if (chain->rules != NULL) { |
| 441 |
fc_rule_t *ptr; |
| 442 |
|
| 443 |
ptr = chain->rules; |
| 444 |
while (ptr->next != NULL) |
| 445 |
ptr = ptr->next; |
| 446 |
|
| 447 |
ptr->next = rule; |
| 448 |
} else { |
| 449 |
chain->rules = rule; |
| 450 |
} |
| 451 |
|
| 452 |
return 0; |
| 453 |
} /* }}} int fc_config_add_rule */ |
| 454 |
|
| 455 |
static int fc_config_add_chain(const oconfig_item_t *ci) /* {{{ */ |
| 456 |
{ |
| 457 |
fc_chain_t *chain = NULL; |
| 458 |
int status = 0; |
| 459 |
int new_chain = 1; |
| 460 |
|
| 461 |
if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) { |
| 462 |
WARNING("Filter subsystem: <Chain> blocks require exactly one " |
| 463 |
"string argument."); |
| 464 |
return -1; |
| 465 |
} |
| 466 |
|
| 467 |
if (chain_list_head != NULL) { |
| 468 |
if ((chain = fc_chain_get_by_name(ci->values[0].value.string)) != NULL) |
| 469 |
new_chain = 0; |
| 470 |
} |
| 471 |
|
| 472 |
if (chain == NULL) { |
| 473 |
chain = calloc(1, sizeof(*chain)); |
| 474 |
if (chain == NULL) { |
| 475 |
ERROR("fc_config_add_chain: calloc failed."); |
| 476 |
return -1; |
| 477 |
} |
| 478 |
sstrncpy(chain->name, ci->values[0].value.string, sizeof(chain->name)); |
| 479 |
} |
| 480 |
|
| 481 |
for (int i = 0; i < ci->children_num; i++) { |
| 482 |
oconfig_item_t *option = ci->children + i; |
| 483 |
|
| 484 |
if (strcasecmp("Rule", option->key) == 0) |
| 485 |
status = fc_config_add_rule(chain, option); |
| 486 |
else if (strcasecmp("Target", option->key) == 0) |
| 487 |
status = fc_config_add_target(&chain->targets, option); |
| 488 |
else { |
| 489 |
WARNING("Filter subsystem: Chain %s: Option `%s' not allowed " |
| 490 |
"inside a <Chain> block.", |
| 491 |
chain->name, option->key); |
| 492 |
status = -1; |
| 493 |
} |
| 494 |
|
| 495 |
if (status != 0) |
| 496 |
break; |
| 497 |
} /* for (ci->children) */ |
| 498 |
|
| 499 |
if (status != 0) { |
| 500 |
fc_free_chains(chain); |
| 501 |
return -1; |
| 502 |
} |
| 503 |
|
| 504 |
if (chain_list_head != NULL) { |
| 505 |
if (!new_chain) |
| 506 |
return 0; |
| 507 |
|
| 508 |
fc_chain_t *ptr; |
| 509 |
|
| 510 |
ptr = chain_list_head; |
| 511 |
while (ptr->next != NULL) |
| 512 |
ptr = ptr->next; |
| 513 |
|
| 514 |
ptr->next = chain; |
| 515 |
} else { |
| 516 |
chain_list_head = chain; |
| 517 |
} |
| 518 |
|
| 519 |
return 0; |
| 520 |
} /* }}} int fc_config_add_chain */ |
| 521 |
|
| 522 |
/* |
| 523 |
* Built-in target "jump" |
| 524 |
* |
| 525 |
* Prefix `bit' like `_b_uilt-_i_n _t_arget' |
| 526 |
*/ |
| 527 |
static int fc_bit_jump_create(const oconfig_item_t *ci, /* {{{ */ |
| 528 |
void **user_data) { |
| 529 |
oconfig_item_t *ci_chain; |
| 530 |
|
| 531 |
if (ci->children_num != 1) { |
| 532 |
ERROR("Filter subsystem: The built-in target `jump' needs exactly " |
| 533 |
"one `Chain' argument!"); |
| 534 |
return -1; |
| 535 |
} |
| 536 |
|
| 537 |
ci_chain = ci->children; |
| 538 |
if (strcasecmp("Chain", ci_chain->key) != 0) { |
| 539 |
ERROR("Filter subsystem: The built-in target `jump' does not " |
| 540 |
"support the configuration option `%s'.", |
| 541 |
ci_chain->key); |
| 542 |
return -1; |
| 543 |
} |
| 544 |
|
| 545 |
if ((ci_chain->values_num != 1) || |
| 546 |
(ci_chain->values[0].type != OCONFIG_TYPE_STRING)) { |
| 547 |
ERROR("Filter subsystem: Built-in target `jump': The `Chain' option " |
| 548 |
"needs exactly one string argument."); |
| 549 |
return -1; |
| 550 |
} |
| 551 |
|
| 552 |
*user_data = fc_strdup(ci_chain->values[0].value.string); |
| 553 |
if (*user_data == NULL) { |
| 554 |
ERROR("fc_bit_jump_create: fc_strdup failed."); |
| 555 |
return -1; |
| 556 |
} |
| 557 |
|
| 558 |
return 0; |
| 559 |
} /* }}} int fc_bit_jump_create */ |
| 560 |
|
| 561 |
static int fc_bit_jump_destroy(void **user_data) /* {{{ */ |
| 562 |
{ |
| 563 |
if (user_data != NULL) { |
| 564 |
free(*user_data); |
| 565 |
*user_data = NULL; |
| 566 |
} |
| 567 |
|
| 568 |
return 0; |
| 569 |
} /* }}} int fc_bit_jump_destroy */ |
| 570 |
|
| 571 |
static int fc_bit_jump_invoke(const data_set_t *ds, /* {{{ */ |
| 572 |
value_list_t *vl, |
| 573 |
notification_meta_t __attribute__((unused)) * |
| 574 |
*meta, |
| 575 |
void **user_data) { |
| 576 |
char *chain_name; |
| 577 |
fc_chain_t *chain; |
| 578 |
int status; |
| 579 |
|
| 580 |
chain_name = *user_data; |
| 581 |
|
| 582 |
for (chain = chain_list_head; chain != NULL; chain = chain->next) |
| 583 |
if (strcasecmp(chain_name, chain->name) == 0) |
| 584 |
break; |
| 585 |
|
| 586 |
if (chain == NULL) { |
| 587 |
ERROR("Filter subsystem: Built-in target `jump': There is no chain " |
| 588 |
"named `%s'.", |
| 589 |
chain_name); |
| 590 |
return -1; |
| 591 |
} |
| 592 |
|
| 593 |
status = fc_process_chain(ds, vl, chain); |
| 594 |
if (status < 0) |
| 595 |
return status; |
| 596 |
else if (status == FC_TARGET_STOP) |
| 597 |
return FC_TARGET_STOP; |
| 598 |
else |
| 599 |
return FC_TARGET_CONTINUE; |
| 600 |
} /* }}} int fc_bit_jump_invoke */ |
| 601 |
|
| 602 |
static int |
| 603 |
fc_bit_stop_invoke(const data_set_t __attribute__((unused)) * ds, /* {{{ */ |
| 604 |
value_list_t __attribute__((unused)) * vl, |
| 605 |
notification_meta_t __attribute__((unused)) * *meta, |
| 606 |
void __attribute__((unused)) * *user_data) { |
| 607 |
return FC_TARGET_STOP; |
| 608 |
} /* }}} int fc_bit_stop_invoke */ |
| 609 |
|
| 610 |
static int |
| 611 |
fc_bit_return_invoke(const data_set_t __attribute__((unused)) * ds, /* {{{ */ |
| 612 |
value_list_t __attribute__((unused)) * vl, |
| 613 |
notification_meta_t __attribute__((unused)) * *meta, |
| 614 |
void __attribute__((unused)) * *user_data) { |
| 615 |
return FC_TARGET_RETURN; |
| 616 |
} /* }}} int fc_bit_return_invoke */ |
| 617 |
|
| 618 |
static int fc_bit_write_create(const oconfig_item_t *ci, /* {{{ */ |
| 619 |
void **user_data) { |
| 620 |
fc_writer_t *plugin_list = NULL; |
| 621 |
size_t plugin_list_len = 0; |
| 622 |
|
| 623 |
for (int i = 0; i < ci->children_num; i++) { |
| 624 |
oconfig_item_t *child = ci->children + i; |
| 625 |
fc_writer_t *temp; |
| 626 |
|
| 627 |
if (strcasecmp("Plugin", child->key) != 0) { |
| 628 |
ERROR("Filter subsystem: The built-in target `write' does not " |
| 629 |
"support the configuration option `%s'.", |
| 630 |
child->key); |
| 631 |
continue; |
| 632 |
} |
| 633 |
|
| 634 |
for (int j = 0; j < child->values_num; j++) { |
| 635 |
char *plugin; |
| 636 |
|
| 637 |
if (child->values[j].type != OCONFIG_TYPE_STRING) { |
| 638 |
ERROR("Filter subsystem: Built-in target `write': " |
| 639 |
"The `Plugin' option accepts only string arguments."); |
| 640 |
continue; |
| 641 |
} |
| 642 |
plugin = child->values[j].value.string; |
| 643 |
|
| 644 |
temp = |
| 645 |
realloc(plugin_list, (plugin_list_len + 2) * (sizeof(*plugin_list))); |
| 646 |
if (temp == NULL) { |
| 647 |
ERROR("fc_bit_write_create: realloc failed."); |
| 648 |
continue; |
| 649 |
} |
| 650 |
plugin_list = temp; |
| 651 |
|
| 652 |
plugin_list[plugin_list_len].plugin = fc_strdup(plugin); |
| 653 |
if (plugin_list[plugin_list_len].plugin == NULL) { |
| 654 |
ERROR("fc_bit_write_create: fc_strdup failed."); |
| 655 |
continue; |
| 656 |
} |
| 657 |
C_COMPLAIN_INIT(&plugin_list[plugin_list_len].complaint); |
| 658 |
plugin_list_len++; |
| 659 |
plugin_list[plugin_list_len].plugin = NULL; |
| 660 |
} /* for (j = 0; j < child->values_num; j++) */ |
| 661 |
} /* for (i = 0; i < ci->children_num; i++) */ |
| 662 |
|
| 663 |
*user_data = plugin_list; |
| 664 |
|
| 665 |
return 0; |
| 666 |
} /* }}} int fc_bit_write_create */ |
| 667 |
|
| 668 |
static int fc_bit_write_destroy(void **user_data) /* {{{ */ |
| 669 |
{ |
| 670 |
fc_writer_t *plugin_list; |
| 671 |
|
| 672 |
if ((user_data == NULL) || (*user_data == NULL)) |
| 673 |
return 0; |
| 674 |
|
| 675 |
plugin_list = *user_data; |
| 676 |
|
| 677 |
for (size_t i = 0; plugin_list[i].plugin != NULL; i++) |
| 678 |
free(plugin_list[i].plugin); |
| 679 |
free(plugin_list); |
| 680 |
|
| 681 |
return 0; |
| 682 |
} /* }}} int fc_bit_write_destroy */ |
| 683 |
|
| 684 |
static int fc_bit_write_invoke(const data_set_t *ds, /* {{{ */ |
| 685 |
value_list_t *vl, |
| 686 |
notification_meta_t __attribute__((unused)) * |
| 687 |
*meta, |
| 688 |
void **user_data) { |
| 689 |
fc_writer_t *plugin_list; |
| 690 |
int status; |
| 691 |
|
| 692 |
plugin_list = NULL; |
| 693 |
if (user_data != NULL) |
| 694 |
plugin_list = *user_data; |
| 695 |
|
| 696 |
if ((plugin_list == NULL) || (plugin_list[0].plugin == NULL)) { |
| 697 |
static c_complain_t write_complaint = C_COMPLAIN_INIT_STATIC; |
| 698 |
|
| 699 |
status = plugin_write(/* plugin = */ NULL, ds, vl); |
| 700 |
if (status == ENOENT) { |
| 701 |
/* in most cases this is a permanent error, so use the complain |
| 702 |
* mechanism rather than spamming the logs */ |
| 703 |
c_complain( |
| 704 |
LOG_INFO, &write_complaint, |
| 705 |
"Filter subsystem: Built-in target `write': Dispatching value to " |
| 706 |
"all write plugins failed with status %i (ENOENT). " |
| 707 |
"Most likely this means you didn't load any write plugins.", |
| 708 |
status); |
| 709 |
|
| 710 |
plugin_log_available_writers(); |
| 711 |
} else if (status != 0) { |
| 712 |
/* often, this is a permanent error (e.g. target system unavailable), |
| 713 |
* so use the complain mechanism rather than spamming the logs */ |
| 714 |
c_complain( |
| 715 |
LOG_INFO, &write_complaint, |
| 716 |
"Filter subsystem: Built-in target `write': Dispatching value to " |
| 717 |
"all write plugins failed with status %i.", |
| 718 |
status); |
| 719 |
} else { |
| 720 |
assert(status == 0); |
| 721 |
c_release(LOG_INFO, &write_complaint, |
| 722 |
"Filter subsystem: " |
| 723 |
"Built-in target `write': Some write plugin is back to normal " |
| 724 |
"operation. `write' succeeded."); |
| 725 |
} |
| 726 |
} else { |
| 727 |
for (size_t i = 0; plugin_list[i].plugin != NULL; i++) { |
| 728 |
status = plugin_write(plugin_list[i].plugin, ds, vl); |
| 729 |
if (status != 0) { |
| 730 |
c_complain( |
| 731 |
LOG_INFO, &plugin_list[i].complaint, |
| 732 |
"Filter subsystem: Built-in target `write': Dispatching value to " |
| 733 |
"the `%s' plugin failed with status %i.", |
| 734 |
plugin_list[i].plugin, status); |
| 735 |
|
| 736 |
plugin_log_available_writers(); |
| 737 |
} else { |
| 738 |
c_release( |
| 739 |
LOG_INFO, &plugin_list[i].complaint, |
| 740 |
"Filter subsystem: Built-in target `write': Plugin `%s' is back " |
| 741 |
"to normal operation. `write' succeeded.", |
| 742 |
plugin_list[i].plugin); |
| 743 |
} |
| 744 |
} /* for (i = 0; plugin_list[i] != NULL; i++) */ |
| 745 |
} |
| 746 |
|
| 747 |
return FC_TARGET_CONTINUE; |
| 748 |
} /* }}} int fc_bit_write_invoke */ |
| 749 |
|
| 750 |
static int fc_init_once(void) /* {{{ */ |
| 751 |
{ |
| 752 |
static int done; |
| 753 |
target_proc_t tproc = {0}; |
| 754 |
|
| 755 |
if (done != 0) |
| 756 |
return 0; |
| 757 |
|
| 758 |
tproc.create = fc_bit_jump_create; |
| 759 |
tproc.destroy = fc_bit_jump_destroy; |
| 760 |
tproc.invoke = fc_bit_jump_invoke; |
| 761 |
fc_register_target("jump", tproc); |
| 762 |
|
| 763 |
memset(&tproc, 0, sizeof(tproc)); |
| 764 |
tproc.create = NULL; |
| 765 |
tproc.destroy = NULL; |
| 766 |
tproc.invoke = fc_bit_stop_invoke; |
| 767 |
fc_register_target("stop", tproc); |
| 768 |
|
| 769 |
memset(&tproc, 0, sizeof(tproc)); |
| 770 |
tproc.create = NULL; |
| 771 |
tproc.destroy = NULL; |
| 772 |
tproc.invoke = fc_bit_return_invoke; |
| 773 |
fc_register_target("return", tproc); |
| 774 |
|
| 775 |
memset(&tproc, 0, sizeof(tproc)); |
| 776 |
tproc.create = fc_bit_write_create; |
| 777 |
tproc.destroy = fc_bit_write_destroy; |
| 778 |
tproc.invoke = fc_bit_write_invoke; |
| 779 |
fc_register_target("write", tproc); |
| 780 |
|
| 781 |
done++; |
| 782 |
return 0; |
| 783 |
} /* }}} int fc_init_once */ |
| 784 |
|
| 785 |
/* |
| 786 |
* Public functions |
| 787 |
*/ |
| 788 |
/* Add a match to list of available matches. */ |
| 789 |
int fc_register_match(const char *name, match_proc_t proc) /* {{{ */ |
| 790 |
{ |
| 791 |
fc_match_t *m; |
| 792 |
|
| 793 |
DEBUG("fc_register_match (%s);", name); |
| 794 |
|
| 795 |
m = calloc(1, sizeof(*m)); |
| 796 |
if (m == NULL) |
| 797 |
return -ENOMEM; |
| 798 |
|
| 799 |
sstrncpy(m->name, name, sizeof(m->name)); |
| 800 |
memcpy(&m->proc, &proc, sizeof(m->proc)); |
| 801 |
|
| 802 |
if (match_list_head == NULL) { |
| 803 |
match_list_head = m; |
| 804 |
} else { |
| 805 |
fc_match_t *ptr; |
| 806 |
|
| 807 |
ptr = match_list_head; |
| 808 |
while (ptr->next != NULL) |
| 809 |
ptr = ptr->next; |
| 810 |
|
| 811 |
ptr->next = m; |
| 812 |
} |
| 813 |
|
| 814 |
return 0; |
| 815 |
} /* }}} int fc_register_match */ |
| 816 |
|
| 817 |
/* Add a target to list of available targets. */ |
| 818 |
int fc_register_target(const char *name, target_proc_t proc) /* {{{ */ |
| 819 |
{ |
| 820 |
fc_target_t *t; |
| 821 |
|
| 822 |
DEBUG("fc_register_target (%s);", name); |
| 823 |
|
| 824 |
t = calloc(1, sizeof(*t)); |
| 825 |
if (t == NULL) |
| 826 |
return -ENOMEM; |
| 827 |
|
| 828 |
sstrncpy(t->name, name, sizeof(t->name)); |
| 829 |
memcpy(&t->proc, &proc, sizeof(t->proc)); |
| 830 |
|
| 831 |
if (target_list_head == NULL) { |
| 832 |
target_list_head = t; |
| 833 |
} else { |
| 834 |
fc_target_t *ptr; |
| 835 |
|
| 836 |
ptr = target_list_head; |
| 837 |
while (ptr->next != NULL) |
| 838 |
ptr = ptr->next; |
| 839 |
|
| 840 |
ptr->next = t; |
| 841 |
} |
| 842 |
|
| 843 |
return 0; |
| 844 |
} /* }}} int fc_register_target */ |
| 845 |
|
| 846 |
fc_chain_t *fc_chain_get_by_name(const char *chain_name) /* {{{ */ |
| 847 |
{ |
| 848 |
if (chain_name == NULL) |
| 849 |
return NULL; |
| 850 |
|
| 851 |
for (fc_chain_t *chain = chain_list_head; chain != NULL; chain = chain->next) |
| 852 |
if (strcasecmp(chain_name, chain->name) == 0) |
| 853 |
return chain; |
| 854 |
|
| 855 |
return NULL; |
| 856 |
} /* }}} int fc_chain_get_by_name */ |
| 857 |
|
| 858 |
int fc_process_chain(const data_set_t *ds, value_list_t *vl, /* {{{ */ |
| 859 |
fc_chain_t *chain) { |
| 860 |
fc_target_t *target; |
| 861 |
int status = FC_TARGET_CONTINUE; |
| 862 |
|
| 863 |
if (chain == NULL) |
| 864 |
return -1; |
| 865 |
|
| 866 |
DEBUG("fc_process_chain (chain = %s);", chain->name); |
| 867 |
|
| 868 |
for (fc_rule_t *rule = chain->rules; rule != NULL; rule = rule->next) { |
| 869 |
fc_match_t *match; |
| 870 |
status = FC_TARGET_CONTINUE; |
| 871 |
|
| 872 |
if (rule->name[0] != 0) { |
| 873 |
DEBUG("fc_process_chain (%s): Testing the `%s' rule.", chain->name, |
| 874 |
rule->name); |
| 875 |
} |
| 876 |
|
| 877 |
/* N. B.: rule->matches may be NULL. */ |
| 878 |
for (match = rule->matches; match != NULL; match = match->next) { |
| 879 |
/* FIXME: Pass the meta-data to match targets here (when implemented). */ |
| 880 |
status = |
| 881 |
(*match->proc.match)(ds, vl, /* meta = */ NULL, &match->user_data); |
| 882 |
if (status < 0) { |
| 883 |
WARNING("fc_process_chain (%s): A match failed.", chain->name); |
| 884 |
break; |
| 885 |
} else if (status != FC_MATCH_MATCHES) |
| 886 |
break; |
| 887 |
} |
| 888 |
|
| 889 |
/* for-loop has been aborted: Either error or no match. */ |
| 890 |
if (match != NULL) { |
| 891 |
status = FC_TARGET_CONTINUE; |
| 892 |
continue; |
| 893 |
} |
| 894 |
|
| 895 |
if (rule->name[0] != 0) { |
| 896 |
DEBUG("fc_process_chain (%s): Rule `%s' matches.", chain->name, |
| 897 |
rule->name); |
| 898 |
} |
| 899 |
|
| 900 |
for (target = rule->targets; target != NULL; target = target->next) { |
| 901 |
/* If we get here, all matches have matched the value. Execute the |
| 902 |
* target. */ |
| 903 |
/* FIXME: Pass the meta-data to match targets here (when implemented). */ |
| 904 |
status = |
| 905 |
(*target->proc.invoke)(ds, vl, /* meta = */ NULL, &target->user_data); |
| 906 |
if (status < 0) { |
| 907 |
WARNING("fc_process_chain (%s): A target failed.", chain->name); |
| 908 |
continue; |
| 909 |
} else if (status == FC_TARGET_CONTINUE) |
| 910 |
continue; |
| 911 |
else if (status == FC_TARGET_STOP) |
| 912 |
break; |
| 913 |
else if (status == FC_TARGET_RETURN) |
| 914 |
break; |
| 915 |
else { |
| 916 |
WARNING("fc_process_chain (%s): Unknown return value " |
| 917 |
"from target `%s': %i", |
| 918 |
chain->name, target->name, status); |
| 919 |
} |
| 920 |
} |
| 921 |
|
| 922 |
if ((status == FC_TARGET_STOP) || (status == FC_TARGET_RETURN)) { |
| 923 |
if (rule->name[0] != 0) { |
| 924 |
DEBUG("fc_process_chain (%s): Rule `%s' signaled " |
| 925 |
"the %s condition.", |
| 926 |
chain->name, rule->name, |
| 927 |
(status == FC_TARGET_STOP) ? "stop" : "return"); |
| 928 |
} |
| 929 |
break; |
| 930 |
} |
| 931 |
} /* for (rule) */ |
| 932 |
|
| 933 |
if ((status == FC_TARGET_STOP) || (status == FC_TARGET_RETURN)) |
| 934 |
return status; |
| 935 |
|
| 936 |
DEBUG("fc_process_chain (%s): Executing the default targets.", chain->name); |
| 937 |
|
| 938 |
status = FC_TARGET_CONTINUE; |
| 939 |
for (target = chain->targets; target != NULL; target = target->next) { |
| 940 |
/* If we get here, all matches have matched the value. Execute the |
| 941 |
* target. */ |
| 942 |
/* FIXME: Pass the meta-data to match targets here (when implemented). */ |
| 943 |
status = |
| 944 |
(*target->proc.invoke)(ds, vl, /* meta = */ NULL, &target->user_data); |
| 945 |
if (status < 0) { |
| 946 |
WARNING("fc_process_chain (%s): The default target failed.", chain->name); |
| 947 |
} else if (status == FC_TARGET_CONTINUE) |
| 948 |
continue; |
| 949 |
else if (status == FC_TARGET_STOP) |
| 950 |
break; |
| 951 |
else if (status == FC_TARGET_RETURN) |
| 952 |
break; |
| 953 |
else { |
| 954 |
WARNING("fc_process_chain (%s): Unknown return value " |
| 955 |
"from target `%s': %i", |
| 956 |
chain->name, target->name, status); |
| 957 |
} |
| 958 |
} |
| 959 |
|
| 960 |
if ((status == FC_TARGET_STOP) || (status == FC_TARGET_RETURN)) { |
| 961 |
assert(target != NULL); |
| 962 |
DEBUG("fc_process_chain (%s): Default target `%s' signaled " |
| 963 |
"the %s condition.", |
| 964 |
chain->name, target->name, |
| 965 |
(status == FC_TARGET_STOP) ? "stop" : "return"); |
| 966 |
if (status == FC_TARGET_STOP) |
| 967 |
return FC_TARGET_STOP; |
| 968 |
else |
| 969 |
return FC_TARGET_CONTINUE; |
| 970 |
} |
| 971 |
|
| 972 |
DEBUG("fc_process_chain (%s): Signaling `continue' at end of chain.", |
| 973 |
chain->name); |
| 974 |
|
| 975 |
return FC_TARGET_CONTINUE; |
| 976 |
} /* }}} int fc_process_chain */ |
| 977 |
|
| 978 |
/* Iterate over all rules in the chain and execute all targets for which all |
| 979 |
* matches match. */ |
| 980 |
int fc_default_action(const data_set_t *ds, value_list_t *vl) /* {{{ */ |
| 981 |
{ |
| 982 |
/* FIXME: Pass the meta-data to match targets here (when implemented). */ |
| 983 |
return fc_bit_write_invoke(ds, vl, NULL, NULL); |
| 984 |
} /* }}} int fc_default_action */ |
| 985 |
|
| 986 |
int fc_configure(const oconfig_item_t *ci) /* {{{ */ |
| 987 |
{ |
| 988 |
fc_init_once(); |
| 989 |
|
| 990 |
if (ci == NULL) |
| 991 |
return -EINVAL; |
| 992 |
|
| 993 |
if (strcasecmp("Chain", ci->key) == 0) |
| 994 |
return fc_config_add_chain(ci); |
| 995 |
|
| 996 |
WARNING("Filter subsystem: Unknown top level config option `%s'.", ci->key); |
| 997 |
|
| 998 |
return -1; |
| 999 |
} /* }}} int fc_configure */ |