1 /**
2 * collectd - src/filter_chain.h
3 * Copyright (C) 2008-2010 Florian octo Forster
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 *
18 * Authors:
19 * Florian octo Forster <octo at verplant.org>
20 **/
22 #include "collectd.h"
23 #include "configfile.h"
24 #include "plugin.h"
25 #include "utils_complain.h"
26 #include "common.h"
27 #include "filter_chain.h"
29 /*
30 * Data types
31 */
32 /* List of matches, used in fc_rule_t and for the global `match_list_head'
33 * variable. */
34 struct fc_match_s;
35 typedef struct fc_match_s fc_match_t; /* {{{ */
36 struct fc_match_s
37 {
38 char name[DATA_MAX_NAME_LEN];
39 match_proc_t proc;
40 void *user_data;
41 fc_match_t *next;
42 }; /* }}} */
44 /* List of targets, used in fc_rule_t and for the global `target_list_head'
45 * variable. */
46 struct fc_target_s;
47 typedef struct fc_target_s fc_target_t; /* {{{ */
48 struct fc_target_s
49 {
50 char name[DATA_MAX_NAME_LEN];
51 void *user_data;
52 target_proc_t proc;
53 fc_target_t *next;
54 }; /* }}} */
56 /* List of rules, used in fc_chain_t */
57 struct fc_rule_s;
58 typedef struct fc_rule_s fc_rule_t; /* {{{ */
59 struct fc_rule_s
60 {
61 char name[DATA_MAX_NAME_LEN];
62 fc_match_t *matches;
63 fc_target_t *targets;
64 fc_rule_t *next;
65 }; /* }}} */
67 /* List of chains, used for `chain_list_head' */
68 struct fc_chain_s /* {{{ */
69 {
70 char name[DATA_MAX_NAME_LEN];
71 fc_rule_t *rules;
72 fc_target_t *targets;
73 fc_chain_t *next;
74 }; /* }}} */
76 /* Writer configuration. */
77 struct fc_writer_s;
78 typedef struct fc_writer_s fc_writer_t; /* {{{ */
79 struct fc_writer_s
80 {
81 char *plugin;
82 c_complain_t complaint;
83 }; /* }}} */
85 /*
86 * Global variables
87 */
88 static fc_match_t *match_list_head;
89 static fc_target_t *target_list_head;
90 static fc_chain_t *chain_list_head;
92 /*
93 * Private functions
94 */
95 static void fc_free_matches (fc_match_t *m) /* {{{ */
96 {
97 if (m == NULL)
98 return;
100 if (m->proc.destroy != NULL)
101 (*m->proc.destroy) (&m->user_data);
102 else if (m->user_data != NULL)
103 {
104 ERROR ("Filter subsystem: fc_free_matches: There is user data, but no "
105 "destroy functions has been specified. "
106 "Memory will probably be lost!");
107 }
109 if (m->next != NULL)
110 fc_free_matches (m->next);
112 free (m);
113 } /* }}} void fc_free_matches */
115 static void fc_free_targets (fc_target_t *t) /* {{{ */
116 {
117 if (t == NULL)
118 return;
120 if (t->proc.destroy != NULL)
121 (*t->proc.destroy) (&t->user_data);
122 else if (t->user_data != NULL)
123 {
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 }
129 if (t->next != NULL)
130 fc_free_targets (t->next);
132 free (t);
133 } /* }}} void fc_free_targets */
135 static void fc_free_rules (fc_rule_t *r) /* {{{ */
136 {
137 if (r == NULL)
138 return;
140 fc_free_matches (r->matches);
141 fc_free_targets (r->targets);
143 if (r->next != NULL)
144 fc_free_rules (r->next);
146 free (r);
147 } /* }}} void fc_free_rules */
149 static void fc_free_chains (fc_chain_t *c) /* {{{ */
150 {
151 if (c == NULL)
152 return;
154 fc_free_rules (c->rules);
155 fc_free_targets (c->targets);
157 if (c->next != NULL)
158 fc_free_chains (c->next);
160 free (c);
161 } /* }}} void fc_free_chains */
163 static char *fc_strdup (const char *orig) /* {{{ */
164 {
165 size_t sz;
166 char *dest;
168 if (orig == NULL)
169 return (NULL);
171 sz = strlen (orig) + 1;
172 dest = (char *) malloc (sz);
173 if (dest == NULL)
174 return (NULL);
176 memcpy (dest, orig, sz);
178 return (dest);
179 } /* }}} char *fc_strdup */
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 {
205 fc_match_t *m;
206 fc_match_t *ptr;
207 int status;
209 if ((ci->values_num != 1)
210 || (ci->values[0].type != OCONFIG_TYPE_STRING))
211 {
212 WARNING ("Filter subsystem: `Match' blocks require "
213 "exactly one string argument.");
214 return (-1);
215 }
217 ptr = match_list_head;
218 while (ptr != NULL)
219 {
220 if (strcasecmp (ptr->name, ci->values[0].value.string) == 0)
221 break;
222 ptr = ptr->next;
223 }
225 if (ptr == NULL)
226 {
227 WARNING ("Filter subsystem: Cannot find a \"%s\" match. "
228 "Did you load the appropriate plugin?",
229 ci->values[0].value.string);
230 return (-1);
231 }
233 m = (fc_match_t *) malloc (sizeof (*m));
234 if (m == NULL)
235 {
236 ERROR ("fc_config_add_match: malloc failed.");
237 return (-1);
238 }
239 memset (m, 0, sizeof (*m));
241 sstrncpy (m->name, ptr->name, sizeof (m->name));
242 memcpy (&m->proc, &ptr->proc, sizeof (m->proc));
243 m->user_data = NULL;
244 m->next = NULL;
246 if (m->proc.create != NULL)
247 {
248 status = (*m->proc.create) (ci, &m->user_data);
249 if (status != 0)
250 {
251 WARNING ("Filter subsystem: Failed to create a %s match.",
252 m->name);
253 fc_free_matches (m);
254 return (-1);
255 }
256 }
258 if (*matches_head != NULL)
259 {
260 ptr = *matches_head;
261 while (ptr->next != NULL)
262 ptr = ptr->next;
264 ptr->next = m;
265 }
266 else
267 {
268 *matches_head = m;
269 }
271 return (0);
272 } /* }}} int fc_config_add_match */
274 static int fc_config_add_target (fc_target_t **targets_head, /* {{{ */
275 oconfig_item_t *ci)
276 {
277 fc_target_t *t;
278 fc_target_t *ptr;
279 int status;
281 if ((ci->values_num != 1)
282 || (ci->values[0].type != OCONFIG_TYPE_STRING))
283 {
284 WARNING ("Filter subsystem: `Target' blocks require "
285 "exactly one string argument.");
286 return (-1);
287 }
289 ptr = target_list_head;
290 while (ptr != NULL)
291 {
292 if (strcasecmp (ptr->name, ci->values[0].value.string) == 0)
293 break;
294 ptr = ptr->next;
295 }
297 if (ptr == NULL)
298 {
299 WARNING ("Filter subsystem: Cannot find a \"%s\" target. "
300 "Did you load the appropriate plugin?",
301 ci->values[0].value.string);
302 return (-1);
303 }
305 t = (fc_target_t *) malloc (sizeof (*t));
306 if (t == NULL)
307 {
308 ERROR ("fc_config_add_target: malloc failed.");
309 return (-1);
310 }
311 memset (t, 0, sizeof (*t));
313 sstrncpy (t->name, ptr->name, sizeof (t->name));
314 memcpy (&t->proc, &ptr->proc, sizeof (t->proc));
315 t->user_data = NULL;
316 t->next = NULL;
318 if (t->proc.create != NULL)
319 {
320 status = (*t->proc.create) (ci, &t->user_data);
321 if (status != 0)
322 {
323 WARNING ("Filter subsystem: Failed to create a %s target.",
324 t->name);
325 fc_free_targets (t);
326 return (-1);
327 }
328 }
329 else
330 {
331 t->user_data = NULL;
332 }
334 if (*targets_head != NULL)
335 {
336 ptr = *targets_head;
337 while (ptr->next != NULL)
338 ptr = ptr->next;
340 ptr->next = t;
341 }
342 else
343 {
344 *targets_head = t;
345 }
347 return (0);
348 } /* }}} int fc_config_add_target */
350 static int fc_config_add_rule (fc_chain_t *chain, /* {{{ */
351 oconfig_item_t *ci)
352 {
353 fc_rule_t *rule;
354 char rule_name[2*DATA_MAX_NAME_LEN] = "Unnamed rule";
355 int status = 0;
356 int i;
358 if (ci->values_num > 1)
359 {
360 WARNING ("Filter subsystem: `Rule' blocks have at most one argument.");
361 return (-1);
362 }
363 else if ((ci->values_num == 1)
364 && (ci->values[0].type != OCONFIG_TYPE_STRING))
365 {
366 WARNING ("Filter subsystem: `Rule' blocks expect one string argument "
367 "or no argument at all.");
368 return (-1);
369 }
371 rule = (fc_rule_t *) malloc (sizeof (*rule));
372 if (rule == NULL)
373 {
374 ERROR ("fc_config_add_rule: malloc failed.");
375 return (-1);
376 }
377 memset (rule, 0, sizeof (*rule));
378 rule->next = NULL;
380 if (ci->values_num == 1)
381 {
382 sstrncpy (rule->name, ci->values[0].value.string, sizeof (rule->name));
383 ssnprintf (rule_name, sizeof (rule_name), "Rule \"%s\"",
384 ci->values[0].value.string);
385 }
387 for (i = 0; i < ci->children_num; i++)
388 {
389 oconfig_item_t *option = ci->children + i;
391 if (strcasecmp ("Match", option->key) == 0)
392 status = fc_config_add_match (&rule->matches, option);
393 else if (strcasecmp ("Target", option->key) == 0)
394 status = fc_config_add_target (&rule->targets, option);
395 else
396 {
397 WARNING ("Filter subsystem: %s: Option `%s' not allowed "
398 "inside a <Rule> block.", rule_name, option->key);
399 status = -1;
400 }
402 if (status != 0)
403 break;
404 } /* for (ci->children) */
406 /* Additional sanity checking. */
407 while (status == 0)
408 {
409 if (rule->targets == NULL)
410 {
411 WARNING ("Filter subsystem: %s: No target has been specified.",
412 rule_name);
413 status = -1;
414 break;
415 }
417 break;
418 } /* while (status == 0) */
420 if (status != 0)
421 {
422 fc_free_rules (rule);
423 return (-1);
424 }
426 if (chain->rules != NULL)
427 {
428 fc_rule_t *ptr;
430 ptr = chain->rules;
431 while (ptr->next != NULL)
432 ptr = ptr->next;
434 ptr->next = rule;
435 }
436 else
437 {
438 chain->rules = rule;
439 }
441 return (0);
442 } /* }}} int fc_config_add_rule */
444 static int fc_config_add_chain (const oconfig_item_t *ci) /* {{{ */
445 {
446 fc_chain_t *chain;
447 int status = 0;
448 int i;
450 if ((ci->values_num != 1)
451 || (ci->values[0].type != OCONFIG_TYPE_STRING))
452 {
453 WARNING ("Filter subsystem: <Chain> blocks require exactly one "
454 "string argument.");
455 return (-1);
456 }
458 chain = (fc_chain_t *) malloc (sizeof (*chain));
459 if (chain == NULL)
460 {
461 ERROR ("fc_config_add_chain: malloc failed.");
462 return (-1);
463 }
464 memset (chain, 0, sizeof (*chain));
465 sstrncpy (chain->name, ci->values[0].value.string, sizeof (chain->name));
466 chain->rules = NULL;
467 chain->targets = NULL;
468 chain->next = NULL;
470 for (i = 0; i < ci->children_num; i++)
471 {
472 oconfig_item_t *option = ci->children + i;
474 if (strcasecmp ("Rule", option->key) == 0)
475 status = fc_config_add_rule (chain, option);
476 else if (strcasecmp ("Target", option->key) == 0)
477 status = fc_config_add_target (&chain->targets, option);
478 else
479 {
480 WARNING ("Filter subsystem: Chain %s: Option `%s' not allowed "
481 "inside a <Chain> block.", chain->name, option->key);
482 status = -1;
483 }
485 if (status != 0)
486 break;
487 } /* for (ci->children) */
489 if (status != 0)
490 {
491 fc_free_chains (chain);
492 return (-1);
493 }
495 if (chain_list_head != NULL)
496 {
497 fc_chain_t *ptr;
499 ptr = chain_list_head;
500 while (ptr->next != NULL)
501 ptr = ptr->next;
503 ptr->next = chain;
504 }
505 else
506 {
507 chain_list_head = chain;
508 }
510 return (0);
511 } /* }}} int fc_config_add_chain */
513 /*
514 * Built-in target "jump"
515 *
516 * Prefix `bit' like `_b_uilt-_i_n _t_arget'
517 */
518 static int fc_bit_jump_create (const oconfig_item_t *ci, /* {{{ */
519 void **user_data)
520 {
521 oconfig_item_t *ci_chain;
523 if (ci->children_num != 1)
524 {
525 ERROR ("Filter subsystem: The built-in target `jump' needs exactly "
526 "one `Chain' argument!");
527 return (-1);
528 }
530 ci_chain = ci->children;
531 if (strcasecmp ("Chain", ci_chain->key) != 0)
532 {
533 ERROR ("Filter subsystem: The built-in target `jump' does not "
534 "support the configuration option `%s'.",
535 ci_chain->key);
536 return (-1);
537 }
539 if ((ci_chain->values_num != 1)
540 || (ci_chain->values[0].type != OCONFIG_TYPE_STRING))
541 {
542 ERROR ("Filter subsystem: Built-in target `jump': The `Chain' option "
543 "needs exactly one string argument.");
544 return (-1);
545 }
547 *user_data = fc_strdup (ci_chain->values[0].value.string);
548 if (*user_data == NULL)
549 {
550 ERROR ("fc_bit_jump_create: fc_strdup failed.");
551 return (-1);
552 }
554 return (0);
555 } /* }}} int fc_bit_jump_create */
557 static int fc_bit_jump_destroy (void **user_data) /* {{{ */
558 {
559 if (user_data != NULL)
560 {
561 free (*user_data);
562 *user_data = NULL;
563 }
565 return (0);
566 } /* }}} int fc_bit_jump_destroy */
568 static int fc_bit_jump_invoke (const data_set_t *ds, /* {{{ */
569 value_list_t *vl, notification_meta_t __attribute__((unused)) **meta,
570 void **user_data)
571 {
572 char *chain_name;
573 fc_chain_t *chain;
574 int status;
576 chain_name = *user_data;
578 for (chain = chain_list_head; chain != NULL; chain = chain->next)
579 if (strcasecmp (chain_name, chain->name) == 0)
580 break;
582 if (chain == NULL)
583 {
584 ERROR ("Filter subsystem: Built-in target `jump': There is no chain "
585 "named `%s'.", chain_name);
586 return (-1);
587 }
589 status = fc_process_chain (ds, vl, chain);
590 if (status < 0)
591 return (status);
592 else if (status == FC_TARGET_STOP)
593 return (FC_TARGET_STOP);
594 else
595 return (FC_TARGET_CONTINUE);
596 } /* }}} int fc_bit_jump_invoke */
598 static int fc_bit_stop_invoke (const data_set_t __attribute__((unused)) *ds, /* {{{ */
599 value_list_t __attribute__((unused)) *vl,
600 notification_meta_t __attribute__((unused)) **meta,
601 void __attribute__((unused)) **user_data)
602 {
603 return (FC_TARGET_STOP);
604 } /* }}} int fc_bit_stop_invoke */
606 static int fc_bit_return_invoke (const data_set_t __attribute__((unused)) *ds, /* {{{ */
607 value_list_t __attribute__((unused)) *vl,
608 notification_meta_t __attribute__((unused)) **meta,
609 void __attribute__((unused)) **user_data)
610 {
611 return (FC_TARGET_RETURN);
612 } /* }}} int fc_bit_return_invoke */
614 static int fc_bit_write_create (const oconfig_item_t *ci, /* {{{ */
615 void **user_data)
616 {
617 int i;
619 fc_writer_t *plugin_list = NULL;
620 size_t plugin_list_len = 0;
622 for (i = 0; i < ci->children_num; i++)
623 {
624 oconfig_item_t *child = ci->children + i;
625 fc_writer_t *temp;
626 int j;
628 if (strcasecmp ("Plugin", child->key) != 0)
629 {
630 ERROR ("Filter subsystem: The built-in target `write' does not "
631 "support the configuration option `%s'.",
632 child->key);
633 continue;
634 }
636 for (j = 0; j < child->values_num; j++)
637 {
638 char *plugin;
640 if (child->values[j].type != OCONFIG_TYPE_STRING)
641 {
642 ERROR ("Filter subsystem: Built-in target `write': "
643 "The `Plugin' option accepts only string arguments.");
644 continue;
645 }
646 plugin = child->values[j].value.string;
648 temp = (fc_writer_t *) realloc (plugin_list, (plugin_list_len + 2)
649 * (sizeof (*plugin_list)));
650 if (temp == NULL)
651 {
652 ERROR ("fc_bit_write_create: realloc failed.");
653 continue;
654 }
655 plugin_list = temp;
657 plugin_list[plugin_list_len].plugin = fc_strdup (plugin);
658 if (plugin_list[plugin_list_len].plugin == NULL)
659 {
660 ERROR ("fc_bit_write_create: fc_strdup failed.");
661 continue;
662 }
663 C_COMPLAIN_INIT (&plugin_list[plugin_list_len].complaint);
664 plugin_list_len++;
665 plugin_list[plugin_list_len].plugin = NULL;
666 } /* for (j = 0; j < child->values_num; j++) */
667 } /* for (i = 0; i < ci->children_num; i++) */
669 *user_data = plugin_list;
671 return (0);
672 } /* }}} int fc_bit_write_create */
674 static int fc_bit_write_destroy (void **user_data) /* {{{ */
675 {
676 fc_writer_t *plugin_list;
677 size_t i;
679 if ((user_data == NULL) || (*user_data == NULL))
680 return (0);
682 plugin_list = *user_data;
684 for (i = 0; plugin_list[i].plugin != NULL; i++)
685 free (plugin_list[i].plugin);
686 free (plugin_list);
688 return (0);
689 } /* }}} int fc_bit_write_destroy */
691 static int fc_bit_write_invoke (const data_set_t *ds, /* {{{ */
692 value_list_t *vl, notification_meta_t __attribute__((unused)) **meta,
693 void **user_data)
694 {
695 fc_writer_t *plugin_list;
696 int status;
698 plugin_list = NULL;
699 if (user_data != NULL)
700 plugin_list = *user_data;
702 if ((plugin_list == NULL) || (plugin_list[0].plugin == NULL))
703 {
704 static c_complain_t write_complaint = C_COMPLAIN_INIT_STATIC;
706 status = plugin_write (/* plugin = */ NULL, ds, vl);
707 if (status == ENOENT)
708 {
709 /* in most cases this is a permanent error, so use the complain
710 * mechanism rather than spamming the logs */
711 c_complain (LOG_INFO, &write_complaint,
712 "Filter subsystem: Built-in target `write': Dispatching value to "
713 "all write plugins failed with status %i (ENOENT). "
714 "Most likely this means you didn't load any write plugins.",
715 status);
716 }
717 else if (status != 0)
718 {
719 /* often, this is a permanent error (e.g. target system unavailable),
720 * so use the complain mechanism rather than spamming the logs */
721 c_complain (LOG_INFO, &write_complaint,
722 "Filter subsystem: Built-in target `write': Dispatching value to "
723 "all write plugins failed with status %i.", status);
724 }
725 else
726 {
727 assert (status == 0);
728 c_release (LOG_INFO, &write_complaint, "Filter subsystem: "
729 "Built-in target `write': Some write plugin is back to normal "
730 "operation. `write' succeeded.");
731 }
732 }
733 else
734 {
735 size_t i;
737 for (i = 0; plugin_list[i].plugin != NULL; i++)
738 {
739 status = plugin_write (plugin_list[i].plugin, ds, vl);
740 if (status != 0)
741 {
742 c_complain (LOG_INFO, &plugin_list[i].complaint,
743 "Filter subsystem: Built-in target `write': Dispatching value to "
744 "the `%s' plugin failed with status %i.",
745 plugin_list[i].plugin, status);
746 }
747 else
748 {
749 c_release (LOG_INFO, &plugin_list[i].complaint,
750 "Filter subsystem: Built-in target `write': Plugin `%s' is back "
751 "to normal operation. `write' succeeded.", plugin_list[i].plugin);
752 }
753 } /* for (i = 0; plugin_list[i] != NULL; i++) */
754 }
756 return (FC_TARGET_CONTINUE);
757 } /* }}} int fc_bit_write_invoke */
759 static int fc_init_once (void) /* {{{ */
760 {
761 static int done = 0;
762 target_proc_t tproc;
764 if (done != 0)
765 return (0);
767 memset (&tproc, 0, sizeof (tproc));
768 tproc.create = fc_bit_jump_create;
769 tproc.destroy = fc_bit_jump_destroy;
770 tproc.invoke = fc_bit_jump_invoke;
771 fc_register_target ("jump", tproc);
773 memset (&tproc, 0, sizeof (tproc));
774 tproc.create = NULL;
775 tproc.destroy = NULL;
776 tproc.invoke = fc_bit_stop_invoke;
777 fc_register_target ("stop", tproc);
779 memset (&tproc, 0, sizeof (tproc));
780 tproc.create = NULL;
781 tproc.destroy = NULL;
782 tproc.invoke = fc_bit_return_invoke;
783 fc_register_target ("return", tproc);
785 memset (&tproc, 0, sizeof (tproc));
786 tproc.create = fc_bit_write_create;
787 tproc.destroy = fc_bit_write_destroy;
788 tproc.invoke = fc_bit_write_invoke;
789 fc_register_target ("write", tproc);
791 done++;
792 return (0);
793 } /* }}} int fc_init_once */
795 /*
796 * Public functions
797 */
798 /* Add a match to list of available matches. */
799 int fc_register_match (const char *name, match_proc_t proc) /* {{{ */
800 {
801 fc_match_t *m;
803 DEBUG ("fc_register_match (%s);", name);
805 m = (fc_match_t *) malloc (sizeof (*m));
806 if (m == NULL)
807 return (-ENOMEM);
808 memset (m, 0, sizeof (*m));
810 sstrncpy (m->name, name, sizeof (m->name));
811 memcpy (&m->proc, &proc, sizeof (m->proc));
812 m->next = NULL;
814 if (match_list_head == NULL)
815 {
816 match_list_head = m;
817 }
818 else
819 {
820 fc_match_t *ptr;
822 ptr = match_list_head;
823 while (ptr->next != NULL)
824 ptr = ptr->next;
826 ptr->next = m;
827 }
829 return (0);
830 } /* }}} int fc_register_match */
832 /* Add a target to list of available targets. */
833 int fc_register_target (const char *name, target_proc_t proc) /* {{{ */
834 {
835 fc_target_t *t;
837 DEBUG ("fc_register_target (%s);", name);
839 t = (fc_target_t *) malloc (sizeof (*t));
840 if (t == NULL)
841 return (-ENOMEM);
842 memset (t, 0, sizeof (*t));
844 sstrncpy (t->name, name, sizeof (t->name));
845 memcpy (&t->proc, &proc, sizeof (t->proc));
846 t->next = NULL;
848 if (target_list_head == NULL)
849 {
850 target_list_head = t;
851 }
852 else
853 {
854 fc_target_t *ptr;
856 ptr = target_list_head;
857 while (ptr->next != NULL)
858 ptr = ptr->next;
860 ptr->next = t;
861 }
863 return (0);
864 } /* }}} int fc_register_target */
866 fc_chain_t *fc_chain_get_by_name (const char *chain_name) /* {{{ */
867 {
868 fc_chain_t *chain;
870 if (chain_name == NULL)
871 return (NULL);
873 for (chain = chain_list_head; chain != NULL; chain = chain->next)
874 if (strcasecmp (chain_name, chain->name) == 0)
875 return (chain);
877 return (NULL);
878 } /* }}} int fc_chain_get_by_name */
880 int fc_process_chain (const data_set_t *ds, value_list_t *vl, /* {{{ */
881 fc_chain_t *chain)
882 {
883 fc_rule_t *rule;
884 fc_target_t *target;
885 int status = FC_TARGET_CONTINUE;
887 if (chain == NULL)
888 return (-1);
890 DEBUG ("fc_process_chain (chain = %s);", chain->name);
892 for (rule = chain->rules; rule != NULL; rule = rule->next)
893 {
894 fc_match_t *match;
895 status = FC_TARGET_CONTINUE;
897 if (rule->name[0] != 0)
898 {
899 DEBUG ("fc_process_chain (%s): Testing the `%s' rule.",
900 chain->name, rule->name);
901 }
903 /* N. B.: rule->matches may be NULL. */
904 for (match = rule->matches; match != NULL; match = match->next)
905 {
906 /* FIXME: Pass the meta-data to match targets here (when implemented). */
907 status = (*match->proc.match) (ds, vl, /* meta = */ NULL,
908 &match->user_data);
909 if (status < 0)
910 {
911 WARNING ("fc_process_chain (%s): A match failed.", chain->name);
912 break;
913 }
914 else if (status != FC_MATCH_MATCHES)
915 break;
916 }
918 /* for-loop has been aborted: Either error or no match. */
919 if (match != NULL)
920 {
921 status = FC_TARGET_CONTINUE;
922 continue;
923 }
925 if (rule->name[0] != 0)
926 {
927 DEBUG ("fc_process_chain (%s): Rule `%s' matches.",
928 chain->name, rule->name);
929 }
931 for (target = rule->targets; target != NULL; target = target->next)
932 {
933 /* If we get here, all matches have matched the value. Execute the
934 * target. */
935 /* FIXME: Pass the meta-data to match targets here (when implemented). */
936 status = (*target->proc.invoke) (ds, vl, /* meta = */ NULL,
937 &target->user_data);
938 if (status < 0)
939 {
940 WARNING ("fc_process_chain (%s): A target failed.", chain->name);
941 continue;
942 }
943 else if (status == FC_TARGET_CONTINUE)
944 continue;
945 else if (status == FC_TARGET_STOP)
946 break;
947 else if (status == FC_TARGET_RETURN)
948 break;
949 else
950 {
951 WARNING ("fc_process_chain (%s): Unknown return value "
952 "from target `%s': %i",
953 chain->name, target->name, status);
954 }
955 }
957 if ((status == FC_TARGET_STOP) || (status == FC_TARGET_RETURN))
958 {
959 if (rule->name[0] != 0)
960 {
961 DEBUG ("fc_process_chain (%s): Rule `%s' signaled "
962 "the %s condition.",
963 chain->name, rule->name,
964 (status == FC_TARGET_STOP) ? "stop" : "return");
965 }
966 break;
967 }
968 } /* for (rule) */
970 if ((status == FC_TARGET_STOP) || (status == FC_TARGET_RETURN))
971 return (status);
973 DEBUG ("fc_process_chain (%s): Executing the default targets.",
974 chain->name);
976 status = FC_TARGET_CONTINUE;
977 for (target = chain->targets; target != NULL; target = target->next)
978 {
979 /* If we get here, all matches have matched the value. Execute the
980 * target. */
981 /* FIXME: Pass the meta-data to match targets here (when implemented). */
982 status = (*target->proc.invoke) (ds, vl, /* meta = */ NULL,
983 &target->user_data);
984 if (status < 0)
985 {
986 WARNING ("fc_process_chain (%s): The default target failed.",
987 chain->name);
988 }
989 else if (status == FC_TARGET_CONTINUE)
990 continue;
991 else if (status == FC_TARGET_STOP)
992 break;
993 else if (status == FC_TARGET_RETURN)
994 break;
995 else
996 {
997 WARNING ("fc_process_chain (%s): Unknown return value "
998 "from target `%s': %i",
999 chain->name, target->name, status);
1000 }
1001 }
1003 if ((status == FC_TARGET_STOP)
1004 || (status == FC_TARGET_RETURN))
1005 {
1006 assert (target != NULL);
1007 DEBUG ("fc_process_chain (%s): Default target `%s' signaled "
1008 "the %s condition.",
1009 chain->name, target->name,
1010 (status == FC_TARGET_STOP) ? "stop" : "return");
1011 if (status == FC_TARGET_STOP)
1012 return (FC_TARGET_STOP);
1013 else
1014 return (FC_TARGET_CONTINUE);
1015 }
1017 DEBUG ("fc_process_chain (%s): Signaling `continue' at end of chain.",
1018 chain->name);
1020 return (FC_TARGET_CONTINUE);
1021 } /* }}} int fc_process_chain */
1023 /* Iterate over all rules in the chain and execute all targets for which all
1024 * matches match. */
1025 int fc_default_action (const data_set_t *ds, value_list_t *vl) /* {{{ */
1026 {
1027 /* FIXME: Pass the meta-data to match targets here (when implemented). */
1028 return (fc_bit_write_invoke (ds, vl,
1029 /* meta = */ NULL, /* user_data = */ NULL));
1030 } /* }}} int fc_default_action */
1032 int fc_configure (const oconfig_item_t *ci) /* {{{ */
1033 {
1034 fc_init_once ();
1036 if (ci == NULL)
1037 return (-EINVAL);
1039 if (strcasecmp ("Chain", ci->key) == 0)
1040 return (fc_config_add_chain (ci));
1042 WARNING ("Filter subsystem: Unknown top level config option `%s'.",
1043 ci->key);
1045 return (-1);
1046 } /* }}} int fc_configure */
1048 /* vim: set sw=2 sts=2 et fdm=marker : */