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