1 /**
2 * collectd - src/filter_chain.h
3 * Copyright (C) 2008,2009 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 /*
23 * First tell the compiler to stick to the C99 and POSIX standards as close as
24 * possible.
25 */
26 #ifndef __STRICT_ANSI__ /* {{{ */
27 # define __STRICT_ANSI__
28 #endif
30 #ifndef _ISOC99_SOURCE
31 # define _ISOC99_SOURCE
32 #endif
34 #ifdef _POSIX_C_SOURCE
35 # undef _POSIX_C_SOURCE
36 #endif
37 #define _POSIX_C_SOURCE 200112L
39 #if 0
40 /* Single UNIX needed for strdup. */
41 #ifdef _XOPEN_SOURCE
42 # undef _XOPEN_SOURCE
43 #endif
44 #define _XOPEN_SOURCE 500
45 #endif
47 #ifndef _REENTRANT
48 # define _REENTRANT
49 #endif
51 #ifndef _THREAD_SAFE
52 # define _THREAD_SAFE
53 #endif
55 #ifdef _GNU_SOURCE
56 # undef _GNU_SOURCE
57 #endif
58 /* }}} */
60 #include "collectd.h"
61 #include "configfile.h"
62 #include "plugin.h"
63 #include "common.h"
64 #include "filter_chain.h"
66 /*
67 * Data types
68 */
69 /* List of matches, used in fc_rule_t and for the global `match_list_head'
70 * variable. */
71 struct fc_match_s;
72 typedef struct fc_match_s fc_match_t; /* {{{ */
73 struct fc_match_s
74 {
75 char name[DATA_MAX_NAME_LEN];
76 match_proc_t proc;
77 void *user_data;
78 fc_match_t *next;
79 }; /* }}} */
81 /* List of targets, used in fc_rule_t and for the global `target_list_head'
82 * variable. */
83 struct fc_target_s;
84 typedef struct fc_target_s fc_target_t; /* {{{ */
85 struct fc_target_s
86 {
87 char name[DATA_MAX_NAME_LEN];
88 void *user_data;
89 target_proc_t proc;
90 fc_target_t *next;
91 }; /* }}} */
93 /* List of rules, used in fc_chain_t */
94 struct fc_rule_s;
95 typedef struct fc_rule_s fc_rule_t; /* {{{ */
96 struct fc_rule_s
97 {
98 char name[DATA_MAX_NAME_LEN];
99 fc_match_t *matches;
100 fc_target_t *targets;
101 fc_rule_t *next;
102 }; /* }}} */
104 /* List of chains, used for `chain_list_head' */
105 struct fc_chain_s /* {{{ */
106 {
107 char name[DATA_MAX_NAME_LEN];
108 fc_rule_t *rules;
109 fc_target_t *targets;
110 fc_chain_t *next;
111 }; /* }}} */
113 /*
114 * Global variables
115 */
116 static fc_match_t *match_list_head;
117 static fc_target_t *target_list_head;
118 static fc_chain_t *chain_list_head;
120 /*
121 * Private functions
122 */
123 static void fc_free_matches (fc_match_t *m) /* {{{ */
124 {
125 if (m == NULL)
126 return;
128 if (m->proc.destroy != NULL)
129 (*m->proc.destroy) (&m->user_data);
130 else if (m->user_data != NULL)
131 {
132 ERROR ("Filter sybsystem: fc_free_matches: There is user data, but no "
133 "destroy functions has been specified. "
134 "Memory will probably be lost!");
135 }
137 if (m->next != NULL)
138 fc_free_matches (m->next);
140 free (m);
141 } /* }}} void fc_free_matches */
143 static void fc_free_targets (fc_target_t *t) /* {{{ */
144 {
145 if (t == NULL)
146 return;
148 if (t->proc.destroy != NULL)
149 (*t->proc.destroy) (&t->user_data);
150 else if (t->user_data != NULL)
151 {
152 ERROR ("Filter sybsystem: fc_free_targets: There is user data, but no "
153 "destroy functions has been specified. "
154 "Memory will probably be lost!");
155 }
157 if (t->next != NULL)
158 fc_free_targets (t->next);
160 free (t);
161 } /* }}} void fc_free_targets */
163 static void fc_free_rules (fc_rule_t *r) /* {{{ */
164 {
165 if (r == NULL)
166 return;
168 fc_free_matches (r->matches);
169 fc_free_targets (r->targets);
171 if (r->next != NULL)
172 fc_free_rules (r->next);
174 free (r);
175 } /* }}} void fc_free_rules */
177 static void fc_free_chains (fc_chain_t *c) /* {{{ */
178 {
179 if (c == NULL)
180 return;
182 fc_free_rules (c->rules);
183 fc_free_targets (c->targets);
185 if (c->next != NULL)
186 fc_free_chains (c->next);
188 free (c);
189 } /* }}} void fc_free_chains */
191 static char *fc_strdup (const char *orig) /* {{{ */
192 {
193 size_t sz;
194 char *dest;
196 if (orig == NULL)
197 return (NULL);
199 sz = strlen (orig) + 1;
200 dest = (char *) malloc (sz);
201 if (dest == NULL)
202 return (NULL);
204 memcpy (dest, orig, sz);
206 return (dest);
207 } /* }}} char *fc_strdup */
209 /*
210 * Configuration.
211 *
212 * The configuration looks somewhat like this:
213 *
214 * <Chain "main">
215 * <Rule>
216 * <Match "regex">
217 * Plugin "^mysql$"
218 * Type "^mysql_command$"
219 * TypeInstance "^show_"
220 * </Match>
221 * <Target "drop">
222 * </Target>
223 * </Rule>
224 *
225 * <Target "write">
226 * Plugin "rrdtool"
227 * </Target>
228 * </Chain>
229 */
230 static int fc_config_add_match (fc_match_t **matches_head, /* {{{ */
231 oconfig_item_t *ci)
232 {
233 fc_match_t *m;
234 fc_match_t *ptr;
235 int status;
237 if ((ci->values_num != 1)
238 || (ci->values[0].type != OCONFIG_TYPE_STRING))
239 {
240 WARNING ("Filter subsystem: `Match' blocks require "
241 "exactly one string argument.");
242 return (-1);
243 }
245 ptr = match_list_head;
246 while (ptr != NULL)
247 {
248 if (strcasecmp (ptr->name, ci->values[0].value.string) == 0)
249 break;
250 ptr = ptr->next;
251 }
253 if (ptr == NULL)
254 {
255 WARNING ("Filter subsystem: Cannot find a \"%s\" match. "
256 "Did you load the appropriate plugin?",
257 ci->values[0].value.string);
258 return (-1);
259 }
261 m = (fc_match_t *) malloc (sizeof (*m));
262 if (m == NULL)
263 {
264 ERROR ("fc_config_add_match: malloc failed.");
265 return (-1);
266 }
267 memset (m, 0, sizeof (*m));
269 sstrncpy (m->name, ptr->name, sizeof (m->name));
270 memcpy (&m->proc, &ptr->proc, sizeof (m->proc));
271 m->user_data = NULL;
272 m->next = NULL;
274 if (m->proc.create != NULL)
275 {
276 status = (*m->proc.create) (ci, &m->user_data);
277 if (status != 0)
278 {
279 WARNING ("Filter subsystem: Failed to create a %s match.",
280 m->name);
281 fc_free_matches (m);
282 return (-1);
283 }
284 }
286 if (*matches_head != NULL)
287 {
288 ptr = *matches_head;
289 while (ptr->next != NULL)
290 ptr = ptr->next;
292 ptr->next = m;
293 }
294 else
295 {
296 *matches_head = m;
297 }
299 return (0);
300 } /* }}} int fc_config_add_match */
302 static int fc_config_add_target (fc_target_t **targets_head, /* {{{ */
303 oconfig_item_t *ci)
304 {
305 fc_target_t *t;
306 fc_target_t *ptr;
307 int status;
309 if ((ci->values_num != 1)
310 || (ci->values[0].type != OCONFIG_TYPE_STRING))
311 {
312 WARNING ("Filter subsystem: `Target' blocks require "
313 "exactly one string argument.");
314 return (-1);
315 }
317 ptr = target_list_head;
318 while (ptr != NULL)
319 {
320 if (strcasecmp (ptr->name, ci->values[0].value.string) == 0)
321 break;
322 ptr = ptr->next;
323 }
325 if (ptr == NULL)
326 {
327 WARNING ("Filter subsystem: Cannot find a \"%s\" target. "
328 "Did you load the appropriate plugin?",
329 ci->values[0].value.string);
330 return (-1);
331 }
333 t = (fc_target_t *) malloc (sizeof (*t));
334 if (t == NULL)
335 {
336 ERROR ("fc_config_add_target: malloc failed.");
337 return (-1);
338 }
339 memset (t, 0, sizeof (*t));
341 sstrncpy (t->name, ptr->name, sizeof (t->name));
342 memcpy (&t->proc, &ptr->proc, sizeof (t->proc));
343 t->user_data = NULL;
344 t->next = NULL;
346 if (t->proc.create != NULL)
347 {
348 status = (*t->proc.create) (ci, &t->user_data);
349 if (status != 0)
350 {
351 WARNING ("Filter subsystem: Failed to create a %s target.",
352 t->name);
353 fc_free_targets (t);
354 return (-1);
355 }
356 }
357 else
358 {
359 t->user_data = NULL;
360 }
362 if (*targets_head != NULL)
363 {
364 ptr = *targets_head;
365 while (ptr->next != NULL)
366 ptr = ptr->next;
368 ptr->next = t;
369 }
370 else
371 {
372 *targets_head = t;
373 }
375 return (0);
376 } /* }}} int fc_config_add_target */
378 static int fc_config_add_rule (fc_chain_t *chain, /* {{{ */
379 oconfig_item_t *ci)
380 {
381 fc_rule_t *rule;
382 char rule_name[2*DATA_MAX_NAME_LEN] = "Unnamed rule";
383 int status = 0;
384 int i;
386 if (ci->values_num > 1)
387 {
388 WARNING ("Filter subsystem: `Rule' blocks have at most one argument.");
389 return (-1);
390 }
391 else if ((ci->values_num == 1)
392 && (ci->values[0].type != OCONFIG_TYPE_STRING))
393 {
394 WARNING ("Filter subsystem: `Rule' blocks expect one string argument "
395 "or no argument at all.");
396 return (-1);
397 }
399 rule = (fc_rule_t *) malloc (sizeof (*rule));
400 if (rule == NULL)
401 {
402 ERROR ("fc_config_add_rule: malloc failed.");
403 return (-1);
404 }
405 memset (rule, 0, sizeof (*rule));
406 rule->next = NULL;
408 if (ci->values_num == 1)
409 {
410 sstrncpy (rule->name, ci->values[0].value.string, sizeof (rule->name));
411 ssnprintf (rule_name, sizeof (rule_name), "Rule \"%s\"",
412 ci->values[0].value.string);
413 }
415 for (i = 0; i < ci->children_num; i++)
416 {
417 oconfig_item_t *option = ci->children + i;
418 status = 0;
420 if (strcasecmp ("Match", option->key) == 0)
421 status = fc_config_add_match (&rule->matches, option);
422 else if (strcasecmp ("Target", option->key) == 0)
423 status = fc_config_add_target (&rule->targets, option);
424 else
425 {
426 WARNING ("Filter subsystem: %s: Option `%s' not allowed "
427 "inside a <Rule> block.", rule_name, option->key);
428 status = -1;
429 }
431 if (status != 0)
432 break;
433 } /* for (ci->children) */
435 /* Additional sanity checking. */
436 while (status == 0)
437 {
438 if (rule->targets == NULL)
439 {
440 WARNING ("Filter subsystem: %s: No target has been specified.",
441 rule_name);
442 status = -1;
443 break;
444 }
446 break;
447 } /* while (status == 0) */
449 if (status != 0)
450 {
451 fc_free_rules (rule);
452 return (-1);
453 }
455 if (chain->rules != NULL)
456 {
457 fc_rule_t *ptr;
459 ptr = chain->rules;
460 while (ptr->next != NULL)
461 ptr = ptr->next;
463 ptr->next = rule;
464 }
465 else
466 {
467 chain->rules = rule;
468 }
470 return (0);
471 } /* }}} int fc_config_add_rule */
473 static int fc_config_add_chain (const oconfig_item_t *ci) /* {{{ */
474 {
475 fc_chain_t *chain;
476 int status = 0;
477 int i;
479 if ((ci->values_num != 1)
480 || (ci->values[0].type != OCONFIG_TYPE_STRING))
481 {
482 WARNING ("Filter subsystem: <Chain> blocks require exactly one "
483 "string argument.");
484 return (-1);
485 }
487 chain = (fc_chain_t *) malloc (sizeof (*chain));
488 if (chain == NULL)
489 {
490 ERROR ("fc_config_add_chain: malloc failed.");
491 return (-1);
492 }
493 memset (chain, 0, sizeof (*chain));
494 sstrncpy (chain->name, ci->values[0].value.string, sizeof (chain->name));
495 chain->rules = NULL;
496 chain->targets = NULL;
497 chain->next = NULL;
499 for (i = 0; i < ci->children_num; i++)
500 {
501 oconfig_item_t *option = ci->children + i;
502 status = 0;
504 if (strcasecmp ("Rule", option->key) == 0)
505 status = fc_config_add_rule (chain, option);
506 else if (strcasecmp ("Target", option->key) == 0)
507 status = fc_config_add_target (&chain->targets, option);
508 else
509 {
510 WARNING ("Filter subsystem: Chain %s: Option `%s' not allowed "
511 "inside a <Chain> block.", chain->name, option->key);
512 status = -1;
513 }
515 if (status != 0)
516 break;
517 } /* for (ci->children) */
519 if (status != 0)
520 {
521 fc_free_chains (chain);
522 return (-1);
523 }
525 if (chain_list_head != NULL)
526 {
527 fc_chain_t *ptr;
529 ptr = chain_list_head;
530 while (ptr->next != NULL)
531 ptr = ptr->next;
533 ptr->next = chain;
534 }
535 else
536 {
537 chain_list_head = chain;
538 }
540 return (0);
541 } /* }}} int fc_config_add_chain */
543 /*
544 * Built-in target "jump"
545 *
546 * Prefix `bit' like `_b_uilt-_i_n _t_arget'
547 */
548 static int fc_bit_jump_create (const oconfig_item_t *ci, /* {{{ */
549 void **user_data)
550 {
551 oconfig_item_t *ci_chain;
553 if (ci->children_num != 1)
554 {
555 ERROR ("Filter subsystem: The built-in target `jump' needs exactly "
556 "one `Chain' argument!");
557 return (-1);
558 }
560 ci_chain = ci->children;
561 if (strcasecmp ("Chain", ci_chain->key) != 0)
562 {
563 ERROR ("Filter subsystem: The built-in target `jump' does not "
564 "support the configuration option `%s'.",
565 ci_chain->key);
566 return (-1);
567 }
569 if ((ci_chain->values_num != 1)
570 || (ci_chain->values[0].type != OCONFIG_TYPE_STRING))
571 {
572 ERROR ("Filter subsystem: Built-in target `jump': The `Chain' option "
573 "needs exactly one string argument.");
574 return (-1);
575 }
577 *user_data = fc_strdup (ci_chain->values[0].value.string);
578 if (*user_data == NULL)
579 {
580 ERROR ("fc_bit_jump_create: fc_strdup failed.");
581 return (-1);
582 }
584 return (0);
585 } /* }}} int fc_bit_jump_create */
587 static int fc_bit_jump_destroy (void **user_data) /* {{{ */
588 {
589 if (user_data != NULL)
590 {
591 free (*user_data);
592 *user_data = NULL;
593 }
595 return (0);
596 } /* }}} int fc_bit_jump_destroy */
598 static int fc_bit_jump_invoke (const data_set_t *ds, /* {{{ */
599 value_list_t *vl, notification_meta_t __attribute__((unused)) **meta,
600 void **user_data)
601 {
602 char *chain_name;
603 fc_chain_t *chain;
604 int status;
606 chain_name = *user_data;
608 for (chain = chain_list_head; chain != NULL; chain = chain->next)
609 if (strcasecmp (chain_name, chain->name) == 0)
610 break;
612 if (chain == NULL)
613 {
614 ERROR ("Filter subsystem: Built-in target `jump': There is no chain "
615 "named `%s'.", chain_name);
616 return (-1);
617 }
619 status = fc_process_chain (ds, vl, chain);
620 if (status < 0)
621 return (status);
622 else if (status == FC_TARGET_STOP)
623 return (FC_TARGET_STOP);
624 else
625 return (FC_TARGET_CONTINUE);
626 } /* }}} int fc_bit_jump_invoke */
628 static int fc_bit_stop_invoke (const data_set_t __attribute__((unused)) *ds, /* {{{ */
629 value_list_t __attribute__((unused)) *vl,
630 notification_meta_t __attribute__((unused)) **meta,
631 void __attribute__((unused)) **user_data)
632 {
633 return (FC_TARGET_STOP);
634 } /* }}} int fc_bit_stop_invoke */
636 static int fc_bit_return_invoke (const data_set_t __attribute__((unused)) *ds, /* {{{ */
637 value_list_t __attribute__((unused)) *vl,
638 notification_meta_t __attribute__((unused)) **meta,
639 void __attribute__((unused)) **user_data)
640 {
641 return (FC_TARGET_RETURN);
642 } /* }}} int fc_bit_return_invoke */
644 static int fc_bit_write_create (const oconfig_item_t *ci, /* {{{ */
645 void **user_data)
646 {
647 int i;
649 char **plugin_list;
650 size_t plugin_list_len;
652 plugin_list = NULL;
653 plugin_list_len = 0;
655 for (i = 0; i < ci->children_num; i++)
656 {
657 oconfig_item_t *child = ci->children + i;
658 char **temp;
659 int j;
661 if (strcasecmp ("Plugin", child->key) != 0)
662 {
663 ERROR ("Filter subsystem: The built-in target `write' does not "
664 "support the configuration option `%s'.",
665 child->key);
666 continue;
667 }
669 for (j = 0; j < child->values_num; j++)
670 {
671 if (child->values[j].type != OCONFIG_TYPE_STRING)
672 {
673 ERROR ("Filter subsystem: Built-in target `write': "
674 "The `Plugin' option accepts only string arguments.");
675 continue;
676 }
678 temp = (char **) realloc (plugin_list, (plugin_list_len + 2)
679 * (sizeof (*plugin_list)));
680 if (temp == NULL)
681 {
682 ERROR ("fc_bit_write_create: realloc failed.");
683 continue;
684 }
685 plugin_list = temp;
687 plugin_list[plugin_list_len] = fc_strdup (child->values[j].value.string);
688 if (plugin_list[plugin_list_len] == NULL)
689 {
690 ERROR ("fc_bit_write_create: fc_strdup failed.");
691 continue;
692 }
693 plugin_list_len++;
694 plugin_list[plugin_list_len] = NULL;
695 } /* for (j = 0; j < child->values_num; j++) */
696 } /* for (i = 0; i < ci->children_num; i++) */
698 *user_data = plugin_list;
700 return (0);
701 } /* }}} int fc_bit_write_create */
703 static int fc_bit_write_destroy (void **user_data) /* {{{ */
704 {
705 char **plugin_list;
706 size_t i;
708 if ((user_data == NULL) || (*user_data == NULL))
709 return (0);
711 plugin_list = *user_data;
713 for (i = 0; plugin_list[i] != NULL; i++)
714 free (plugin_list[i]);
715 free (plugin_list);
717 return (0);
718 } /* }}} int fc_bit_write_destroy */
720 static int fc_bit_write_invoke (const data_set_t *ds, /* {{{ */
721 value_list_t *vl, notification_meta_t __attribute__((unused)) **meta,
722 void **user_data)
723 {
724 char **plugin_list;
725 int status;
727 plugin_list = NULL;
728 if (user_data != NULL)
729 plugin_list = *user_data;
731 if ((plugin_list == NULL) || (plugin_list[0] == NULL))
732 {
733 status = plugin_write (/* plugin = */ NULL, ds, vl);
734 if (status != 0)
735 {
736 INFO ("Filter subsystem: Built-in target `write': Dispatching value to "
737 "all write plugins failed with status %i.", status);
738 }
739 }
740 else
741 {
742 size_t i;
744 for (i = 0; plugin_list[i] != NULL; i++)
745 {
746 status = plugin_write (plugin_list[i], ds, vl);
747 if (status != 0)
748 {
749 INFO ("Filter subsystem: Built-in target `write': Dispatching value to "
750 "the `%s' plugin failed with status %i.", plugin_list[i], status);
751 }
752 } /* for (i = 0; plugin_list[i] != NULL; i++) */
753 }
755 return (FC_TARGET_CONTINUE);
756 } /* }}} int fc_bit_write_invoke */
758 static int fc_init_once (void) /* {{{ */
759 {
760 static int done = 0;
761 target_proc_t tproc;
763 if (done != 0)
764 return (0);
766 memset (&tproc, 0, sizeof (tproc));
767 tproc.create = fc_bit_jump_create;
768 tproc.destroy = fc_bit_jump_destroy;
769 tproc.invoke = fc_bit_jump_invoke;
770 fc_register_target ("jump", tproc);
772 memset (&tproc, 0, sizeof (tproc));
773 tproc.create = NULL;
774 tproc.destroy = NULL;
775 tproc.invoke = fc_bit_stop_invoke;
776 fc_register_target ("stop", tproc);
778 memset (&tproc, 0, sizeof (tproc));
779 tproc.create = NULL;
780 tproc.destroy = NULL;
781 tproc.invoke = fc_bit_return_invoke;
782 fc_register_target ("return", tproc);
784 memset (&tproc, 0, sizeof (tproc));
785 tproc.create = fc_bit_write_create;
786 tproc.destroy = fc_bit_write_destroy;
787 tproc.invoke = fc_bit_write_invoke;
788 fc_register_target ("write", tproc);
790 done++;
791 return (0);
792 } /* }}} int fc_init_once */
794 /*
795 * Public functions
796 */
797 /* Add a match to list of available matches. */
798 int fc_register_match (const char *name, match_proc_t proc) /* {{{ */
799 {
800 fc_match_t *m;
802 DEBUG ("fc_register_match (%s);", name);
804 m = (fc_match_t *) malloc (sizeof (*m));
805 if (m == NULL)
806 return (-ENOMEM);
807 memset (m, 0, sizeof (*m));
809 sstrncpy (m->name, name, sizeof (m->name));
810 memcpy (&m->proc, &proc, sizeof (m->proc));
811 m->next = NULL;
813 if (match_list_head == NULL)
814 {
815 match_list_head = m;
816 }
817 else
818 {
819 fc_match_t *ptr;
821 ptr = match_list_head;
822 while (ptr->next != NULL)
823 ptr = ptr->next;
825 ptr->next = m;
826 }
828 return (0);
829 } /* }}} int fc_register_match */
831 /* Add a target to list of available targets. */
832 int fc_register_target (const char *name, target_proc_t proc) /* {{{ */
833 {
834 fc_target_t *t;
836 DEBUG ("fc_register_target (%s);", name);
838 t = (fc_target_t *) malloc (sizeof (*t));
839 if (t == NULL)
840 return (-ENOMEM);
841 memset (t, 0, sizeof (*t));
843 sstrncpy (t->name, name, sizeof (t->name));
844 memcpy (&t->proc, &proc, sizeof (t->proc));
845 t->next = NULL;
847 if (target_list_head == NULL)
848 {
849 target_list_head = t;
850 }
851 else
852 {
853 fc_target_t *ptr;
855 ptr = target_list_head;
856 while (ptr->next != NULL)
857 ptr = ptr->next;
859 ptr->next = t;
860 }
862 return (0);
863 } /* }}} int fc_register_target */
865 fc_chain_t *fc_chain_get_by_name (const char *chain_name) /* {{{ */
866 {
867 fc_chain_t *chain;
869 if (chain_name == NULL)
870 return (NULL);
872 for (chain = chain_list_head; chain != NULL; chain = chain->next)
873 if (strcasecmp (chain_name, chain->name) == 0)
874 return (chain);
876 return (NULL);
877 } /* }}} int fc_chain_get_by_name */
879 int fc_process_chain (const data_set_t *ds, value_list_t *vl, /* {{{ */
880 fc_chain_t *chain)
881 {
882 fc_rule_t *rule;
883 fc_target_t *target;
884 int status;
886 if (chain == NULL)
887 return (-1);
889 DEBUG ("fc_process_chain (chain = %s);", chain->name);
891 status = FC_TARGET_CONTINUE;
892 for (rule = chain->rules; rule != NULL; rule = rule->next)
893 {
894 fc_match_t *match;
896 if (rule->name[0] != 0)
897 {
898 DEBUG ("fc_process_chain (%s): Testing the `%s' rule.",
899 chain->name, rule->name);
900 }
902 /* N. B.: rule->matches may be NULL. */
903 for (match = rule->matches; match != NULL; match = match->next)
904 {
905 /* FIXME: Pass the meta-data to match targets here (when implemented). */
906 status = (*match->proc.match) (ds, vl, /* meta = */ NULL,
907 &match->user_data);
908 if (status < 0)
909 {
910 WARNING ("fc_process_chain (%s): A match failed.", chain->name);
911 break;
912 }
913 else if (status != FC_MATCH_MATCHES)
914 break;
915 }
917 /* for-loop has been aborted: Either error or no match. */
918 if (match != NULL)
919 {
920 status = FC_TARGET_CONTINUE;
921 continue;
922 }
924 if (rule->name[0] != 0)
925 {
926 DEBUG ("fc_process_chain (%s): Rule `%s' matches.",
927 chain->name, rule->name);
928 }
930 for (target = rule->targets; target != NULL; target = target->next)
931 {
932 /* If we get here, all matches have matched the value. Execute the
933 * target. */
934 /* FIXME: Pass the meta-data to match targets here (when implemented). */
935 status = (*target->proc.invoke) (ds, vl, /* meta = */ NULL,
936 &target->user_data);
937 if (status < 0)
938 {
939 WARNING ("fc_process_chain (%s): A target failed.", chain->name);
940 continue;
941 }
942 else if (status == FC_TARGET_CONTINUE)
943 continue;
944 else if (status == FC_TARGET_STOP)
945 break;
946 else if (status == FC_TARGET_RETURN)
947 break;
948 else
949 {
950 WARNING ("fc_process_chain (%s): Unknown return value "
951 "from target `%s': %i",
952 chain->name, target->name, status);
953 }
954 }
956 if ((status == FC_TARGET_STOP)
957 || (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 else
969 {
970 status = FC_TARGET_CONTINUE;
971 }
972 } /* for (rule) */
974 if (status == FC_TARGET_STOP)
975 return (FC_TARGET_STOP);
976 else if (status == FC_TARGET_RETURN)
977 return (FC_TARGET_CONTINUE);
979 /* for-loop has been aborted: A target returned `FC_TARGET_STOP' */
980 if (rule != NULL)
981 return (FC_TARGET_CONTINUE);
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 : */