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 #ifdef _XOPEN_SOURCE
40 # undef _XOPEN_SOURCE
41 #endif
42 #define _XOPEN_SOURCE 600
44 #ifndef _REENTRANT
45 # define _REENTRANT
46 #endif
48 #ifndef _THREAD_SAFE
49 # define _THREAD_SAFE
50 #endif
52 #ifdef _GNU_SOURCE
53 # undef _GNU_SOURCE
54 #endif
55 /* }}} */
57 #include "collectd.h"
58 #include "configfile.h"
59 #include "plugin.h"
60 #include "common.h"
61 #include "filter_chain.h"
63 /*
64 * Data types
65 */
66 /* List of matches, used in fc_rule_t and for the global `match_list_head'
67 * variable. */
68 struct fc_match_s;
69 typedef struct fc_match_s fc_match_t; /* {{{ */
70 struct fc_match_s
71 {
72 char name[DATA_MAX_NAME_LEN];
73 match_proc_t proc;
74 void *user_data;
75 fc_match_t *next;
76 }; /* }}} */
78 /* List of targets, used in fc_rule_t and for the global `target_list_head'
79 * variable. */
80 struct fc_target_s;
81 typedef struct fc_target_s fc_target_t; /* {{{ */
82 struct fc_target_s
83 {
84 char name[DATA_MAX_NAME_LEN];
85 void *user_data;
86 target_proc_t proc;
87 fc_target_t *next;
88 }; /* }}} */
90 /* List of rules, used in fc_chain_t */
91 struct fc_rule_s;
92 typedef struct fc_rule_s fc_rule_t; /* {{{ */
93 struct fc_rule_s
94 {
95 char name[DATA_MAX_NAME_LEN];
96 fc_match_t *matches;
97 fc_target_t *targets;
98 fc_rule_t *next;
99 }; /* }}} */
101 /* List of chains, used for `chain_list_head' */
102 struct fc_chain_s /* {{{ */
103 {
104 char name[DATA_MAX_NAME_LEN];
105 fc_rule_t *rules;
106 fc_target_t *targets;
107 fc_chain_t *next;
108 }; /* }}} */
110 /*
111 * Global variables
112 */
113 static fc_match_t *match_list_head;
114 static fc_target_t *target_list_head;
115 static fc_chain_t *chain_list_head;
117 /*
118 * Private functions
119 */
120 static void fc_free_matches (fc_match_t *m) /* {{{ */
121 {
122 if (m == NULL)
123 return;
125 if (m->proc.destroy != NULL)
126 (*m->proc.destroy) (&m->user_data);
127 else if (m->user_data != NULL)
128 {
129 ERROR ("Filter sybsystem: fc_free_matches: There is user data, but no "
130 "destroy functions has been specified. "
131 "Memory will probably be lost!");
132 }
134 if (m->next != NULL)
135 fc_free_matches (m->next);
137 free (m);
138 } /* }}} void fc_free_matches */
140 static void fc_free_targets (fc_target_t *t) /* {{{ */
141 {
142 if (t == NULL)
143 return;
145 if (t->proc.destroy != NULL)
146 (*t->proc.destroy) (&t->user_data);
147 else if (t->user_data != NULL)
148 {
149 ERROR ("Filter sybsystem: fc_free_targets: There is user data, but no "
150 "destroy functions has been specified. "
151 "Memory will probably be lost!");
152 }
154 if (t->next != NULL)
155 fc_free_targets (t->next);
157 free (t);
158 } /* }}} void fc_free_targets */
160 static void fc_free_rules (fc_rule_t *r) /* {{{ */
161 {
162 if (r == NULL)
163 return;
165 fc_free_matches (r->matches);
166 fc_free_targets (r->targets);
168 if (r->next != NULL)
169 fc_free_rules (r->next);
171 free (r);
172 } /* }}} void fc_free_rules */
174 static void fc_free_chains (fc_chain_t *c) /* {{{ */
175 {
176 if (c == NULL)
177 return;
179 fc_free_rules (c->rules);
180 fc_free_targets (c->targets);
182 if (c->next != NULL)
183 fc_free_chains (c->next);
185 free (c);
186 } /* }}} void fc_free_chains */
188 static char *fc_strdup (const char *orig) /* {{{ */
189 {
190 size_t sz;
191 char *dest;
193 if (orig == NULL)
194 return (NULL);
196 sz = strlen (orig) + 1;
197 dest = (char *) malloc (sz);
198 if (dest == NULL)
199 return (NULL);
201 memcpy (dest, orig, sz);
203 return (dest);
204 } /* }}} char *fc_strdup */
206 /*
207 * Configuration.
208 *
209 * The configuration looks somewhat like this:
210 *
211 * <Chain "main">
212 * <Rule>
213 * <Match "regex">
214 * Plugin "^mysql$"
215 * Type "^mysql_command$"
216 * TypeInstance "^show_"
217 * </Match>
218 * <Target "drop">
219 * </Target>
220 * </Rule>
221 *
222 * <Target "write">
223 * Plugin "rrdtool"
224 * </Target>
225 * </Chain>
226 */
227 static int fc_config_add_match (fc_match_t **matches_head, /* {{{ */
228 oconfig_item_t *ci)
229 {
230 fc_match_t *m;
231 fc_match_t *ptr;
232 int status;
234 if ((ci->values_num != 1)
235 || (ci->values[0].type != OCONFIG_TYPE_STRING))
236 {
237 WARNING ("Filter subsystem: `Match' blocks require "
238 "exactly one string argument.");
239 return (-1);
240 }
242 ptr = match_list_head;
243 while (ptr != NULL)
244 {
245 if (strcasecmp (ptr->name, ci->values[0].value.string) == 0)
246 break;
247 ptr = ptr->next;
248 }
250 if (ptr == NULL)
251 {
252 WARNING ("Filter subsystem: Cannot find a \"%s\" match. "
253 "Did you load the appropriate plugin?",
254 ci->values[0].value.string);
255 return (-1);
256 }
258 m = (fc_match_t *) malloc (sizeof (*m));
259 if (m == NULL)
260 {
261 ERROR ("fc_config_add_match: malloc failed.");
262 return (-1);
263 }
264 memset (m, 0, sizeof (*m));
266 sstrncpy (m->name, ptr->name, sizeof (m->name));
267 memcpy (&m->proc, &ptr->proc, sizeof (m->proc));
268 m->user_data = NULL;
269 m->next = NULL;
271 if (m->proc.create != NULL)
272 {
273 status = (*m->proc.create) (ci, &m->user_data);
274 if (status != 0)
275 {
276 WARNING ("Filter subsystem: Failed to create a %s match.",
277 m->name);
278 fc_free_matches (m);
279 return (-1);
280 }
281 }
283 if (*matches_head != NULL)
284 {
285 ptr = *matches_head;
286 while (ptr->next != NULL)
287 ptr = ptr->next;
289 ptr->next = m;
290 }
291 else
292 {
293 *matches_head = m;
294 }
296 return (0);
297 } /* }}} int fc_config_add_match */
299 static int fc_config_add_target (fc_target_t **targets_head, /* {{{ */
300 oconfig_item_t *ci)
301 {
302 fc_target_t *t;
303 fc_target_t *ptr;
304 int status;
306 if ((ci->values_num != 1)
307 || (ci->values[0].type != OCONFIG_TYPE_STRING))
308 {
309 WARNING ("Filter subsystem: `Target' blocks require "
310 "exactly one string argument.");
311 return (-1);
312 }
314 ptr = target_list_head;
315 while (ptr != NULL)
316 {
317 if (strcasecmp (ptr->name, ci->values[0].value.string) == 0)
318 break;
319 ptr = ptr->next;
320 }
322 if (ptr == NULL)
323 {
324 WARNING ("Filter subsystem: Cannot find a \"%s\" target. "
325 "Did you load the appropriate plugin?",
326 ci->values[0].value.string);
327 return (-1);
328 }
330 t = (fc_target_t *) malloc (sizeof (*t));
331 if (t == NULL)
332 {
333 ERROR ("fc_config_add_target: malloc failed.");
334 return (-1);
335 }
336 memset (t, 0, sizeof (*t));
338 sstrncpy (t->name, ptr->name, sizeof (t->name));
339 memcpy (&t->proc, &ptr->proc, sizeof (t->proc));
340 t->user_data = NULL;
341 t->next = NULL;
343 if (t->proc.create != NULL)
344 {
345 status = (*t->proc.create) (ci, &t->user_data);
346 if (status != 0)
347 {
348 WARNING ("Filter subsystem: Failed to create a %s target.",
349 t->name);
350 fc_free_targets (t);
351 return (-1);
352 }
353 }
354 else
355 {
356 t->user_data = NULL;
357 }
359 if (*targets_head != NULL)
360 {
361 ptr = *targets_head;
362 while (ptr->next != NULL)
363 ptr = ptr->next;
365 ptr->next = t;
366 }
367 else
368 {
369 *targets_head = t;
370 }
372 return (0);
373 } /* }}} int fc_config_add_target */
375 static int fc_config_add_rule (fc_chain_t *chain, /* {{{ */
376 oconfig_item_t *ci)
377 {
378 fc_rule_t *rule;
379 char rule_name[2*DATA_MAX_NAME_LEN] = "Unnamed rule";
380 int status = 0;
381 int i;
383 if (ci->values_num > 1)
384 {
385 WARNING ("Filter subsystem: `Rule' blocks have at most one argument.");
386 return (-1);
387 }
388 else if ((ci->values_num == 1)
389 && (ci->values[0].type != OCONFIG_TYPE_STRING))
390 {
391 WARNING ("Filter subsystem: `Rule' blocks expect one string argument "
392 "or no argument at all.");
393 return (-1);
394 }
396 rule = (fc_rule_t *) malloc (sizeof (*rule));
397 if (rule == NULL)
398 {
399 ERROR ("fc_config_add_rule: malloc failed.");
400 return (-1);
401 }
402 memset (rule, 0, sizeof (*rule));
403 rule->next = NULL;
405 if (ci->values_num == 1)
406 {
407 sstrncpy (rule->name, ci->values[0].value.string, sizeof (rule->name));
408 ssnprintf (rule_name, sizeof (rule_name), "Rule \"%s\"",
409 ci->values[0].value.string);
410 }
412 for (i = 0; i < ci->children_num; i++)
413 {
414 oconfig_item_t *option = ci->children + i;
415 status = 0;
417 if (strcasecmp ("Match", option->key) == 0)
418 status = fc_config_add_match (&rule->matches, option);
419 else if (strcasecmp ("Target", option->key) == 0)
420 status = fc_config_add_target (&rule->targets, option);
421 else
422 {
423 WARNING ("Filter subsystem: %s: Option `%s' not allowed "
424 "inside a <Rule> block.", rule_name, option->key);
425 status = -1;
426 }
428 if (status != 0)
429 break;
430 } /* for (ci->children) */
432 /* Additional sanity checking. */
433 while (status == 0)
434 {
435 if (rule->targets == NULL)
436 {
437 WARNING ("Filter subsystem: %s: No target has been specified.",
438 rule_name);
439 status = -1;
440 break;
441 }
443 break;
444 } /* while (status == 0) */
446 if (status != 0)
447 {
448 fc_free_rules (rule);
449 return (-1);
450 }
452 if (chain->rules != NULL)
453 {
454 fc_rule_t *ptr;
456 ptr = chain->rules;
457 while (ptr->next != NULL)
458 ptr = ptr->next;
460 ptr->next = rule;
461 }
462 else
463 {
464 chain->rules = rule;
465 }
467 return (0);
468 } /* }}} int fc_config_add_rule */
470 static int fc_config_add_chain (const oconfig_item_t *ci) /* {{{ */
471 {
472 fc_chain_t *chain;
473 int status = 0;
474 int i;
476 if ((ci->values_num != 1)
477 || (ci->values[0].type != OCONFIG_TYPE_STRING))
478 {
479 WARNING ("Filter subsystem: <Chain> blocks require exactly one "
480 "string argument.");
481 return (-1);
482 }
484 chain = (fc_chain_t *) malloc (sizeof (*chain));
485 if (chain == NULL)
486 {
487 ERROR ("fc_config_add_chain: malloc failed.");
488 return (-1);
489 }
490 memset (chain, 0, sizeof (*chain));
491 sstrncpy (chain->name, ci->values[0].value.string, sizeof (chain->name));
492 chain->rules = NULL;
493 chain->targets = NULL;
494 chain->next = NULL;
496 for (i = 0; i < ci->children_num; i++)
497 {
498 oconfig_item_t *option = ci->children + i;
499 status = 0;
501 if (strcasecmp ("Rule", option->key) == 0)
502 status = fc_config_add_rule (chain, option);
503 else if (strcasecmp ("Target", option->key) == 0)
504 status = fc_config_add_target (&chain->targets, option);
505 else
506 {
507 WARNING ("Filter subsystem: Chain %s: Option `%s' not allowed "
508 "inside a <Chain> block.", chain->name, option->key);
509 status = -1;
510 }
512 if (status != 0)
513 break;
514 } /* for (ci->children) */
516 if (status != 0)
517 {
518 fc_free_chains (chain);
519 return (-1);
520 }
522 if (chain_list_head != NULL)
523 {
524 fc_chain_t *ptr;
526 ptr = chain_list_head;
527 while (ptr->next != NULL)
528 ptr = ptr->next;
530 ptr->next = chain;
531 }
532 else
533 {
534 chain_list_head = chain;
535 }
537 return (0);
538 } /* }}} int fc_config_add_chain */
540 /*
541 * Built-in target "jump"
542 *
543 * Prefix `bit' like `_b_uilt-_i_n _t_arget'
544 */
545 static int fc_bit_jump_create (const oconfig_item_t *ci, /* {{{ */
546 void **user_data)
547 {
548 oconfig_item_t *ci_chain;
550 if (ci->children_num != 1)
551 {
552 ERROR ("Filter subsystem: The built-in target `jump' needs exactly "
553 "one `Chain' argument!");
554 return (-1);
555 }
557 ci_chain = ci->children;
558 if (strcasecmp ("Chain", ci_chain->key) != 0)
559 {
560 ERROR ("Filter subsystem: The built-in target `jump' does not "
561 "support the configuration option `%s'.",
562 ci_chain->key);
563 return (-1);
564 }
566 if ((ci_chain->values_num != 1)
567 || (ci_chain->values[0].type != OCONFIG_TYPE_STRING))
568 {
569 ERROR ("Filter subsystem: Built-in target `jump': The `Chain' option "
570 "needs exactly one string argument.");
571 return (-1);
572 }
574 *user_data = fc_strdup (ci_chain->values[0].value.string);
575 if (*user_data == NULL)
576 {
577 ERROR ("fc_bit_jump_create: fc_strdup failed.");
578 return (-1);
579 }
581 return (0);
582 } /* }}} int fc_bit_jump_create */
584 static int fc_bit_jump_destroy (void **user_data) /* {{{ */
585 {
586 if (user_data != NULL)
587 {
588 free (*user_data);
589 *user_data = NULL;
590 }
592 return (0);
593 } /* }}} int fc_bit_jump_destroy */
595 static int fc_bit_jump_invoke (const data_set_t *ds, /* {{{ */
596 value_list_t *vl, notification_meta_t __attribute__((unused)) **meta,
597 void **user_data)
598 {
599 char *chain_name;
600 fc_chain_t *chain;
601 int status;
603 chain_name = *user_data;
605 for (chain = chain_list_head; chain != NULL; chain = chain->next)
606 if (strcasecmp (chain_name, chain->name) == 0)
607 break;
609 if (chain == NULL)
610 {
611 ERROR ("Filter subsystem: Built-in target `jump': There is no chain "
612 "named `%s'.", chain_name);
613 return (-1);
614 }
616 status = fc_process_chain (ds, vl, chain);
617 if (status < 0)
618 return (status);
619 else if (status == FC_TARGET_STOP)
620 return (FC_TARGET_STOP);
621 else
622 return (FC_TARGET_CONTINUE);
623 } /* }}} int fc_bit_jump_invoke */
625 static int fc_bit_stop_invoke (const data_set_t __attribute__((unused)) *ds, /* {{{ */
626 value_list_t __attribute__((unused)) *vl,
627 notification_meta_t __attribute__((unused)) **meta,
628 void __attribute__((unused)) **user_data)
629 {
630 return (FC_TARGET_STOP);
631 } /* }}} int fc_bit_stop_invoke */
633 static int fc_bit_return_invoke (const data_set_t __attribute__((unused)) *ds, /* {{{ */
634 value_list_t __attribute__((unused)) *vl,
635 notification_meta_t __attribute__((unused)) **meta,
636 void __attribute__((unused)) **user_data)
637 {
638 return (FC_TARGET_RETURN);
639 } /* }}} int fc_bit_return_invoke */
641 static int fc_bit_write_create (const oconfig_item_t *ci, /* {{{ */
642 void **user_data)
643 {
644 int i;
646 char **plugin_list;
647 size_t plugin_list_len;
649 plugin_list = NULL;
650 plugin_list_len = 0;
652 for (i = 0; i < ci->children_num; i++)
653 {
654 oconfig_item_t *child = ci->children + i;
655 char **temp;
656 int j;
658 if (strcasecmp ("Plugin", child->key) != 0)
659 {
660 ERROR ("Filter subsystem: The built-in target `write' does not "
661 "support the configuration option `%s'.",
662 child->key);
663 continue;
664 }
666 for (j = 0; j < child->values_num; j++)
667 {
668 if (child->values[j].type != OCONFIG_TYPE_STRING)
669 {
670 ERROR ("Filter subsystem: Built-in target `write': "
671 "The `Plugin' option accepts only string arguments.");
672 continue;
673 }
675 temp = (char **) realloc (plugin_list, (plugin_list_len + 2)
676 * (sizeof (*plugin_list)));
677 if (temp == NULL)
678 {
679 ERROR ("fc_bit_write_create: realloc failed.");
680 continue;
681 }
682 plugin_list = temp;
684 plugin_list[plugin_list_len] = fc_strdup (child->values[j].value.string);
685 if (plugin_list[plugin_list_len] == NULL)
686 {
687 ERROR ("fc_bit_write_create: fc_strdup failed.");
688 continue;
689 }
690 plugin_list_len++;
691 plugin_list[plugin_list_len] = NULL;
692 } /* for (j = 0; j < child->values_num; j++) */
693 } /* for (i = 0; i < ci->children_num; i++) */
695 *user_data = plugin_list;
697 return (0);
698 } /* }}} int fc_bit_write_create */
700 static int fc_bit_write_destroy (void **user_data) /* {{{ */
701 {
702 char **plugin_list;
703 size_t i;
705 if ((user_data == NULL) || (*user_data == NULL))
706 return (0);
708 plugin_list = *user_data;
710 for (i = 0; plugin_list[i] != NULL; i++)
711 free (plugin_list[i]);
712 free (plugin_list);
714 return (0);
715 } /* }}} int fc_bit_write_destroy */
717 static int fc_bit_write_invoke (const data_set_t *ds, /* {{{ */
718 value_list_t *vl, notification_meta_t __attribute__((unused)) **meta,
719 void **user_data)
720 {
721 char **plugin_list;
722 int status;
724 plugin_list = NULL;
725 if (user_data != NULL)
726 plugin_list = *user_data;
728 if ((plugin_list == NULL) || (plugin_list[0] == NULL))
729 {
730 status = plugin_write (/* plugin = */ NULL, ds, vl);
731 if (status != 0)
732 {
733 INFO ("Filter subsystem: Built-in target `write': Dispatching value to "
734 "all write plugins failed with status %i.", status);
735 }
736 }
737 else
738 {
739 size_t i;
741 for (i = 0; plugin_list[i] != NULL; i++)
742 {
743 status = plugin_write (plugin_list[i], ds, vl);
744 if (status != 0)
745 {
746 INFO ("Filter subsystem: Built-in target `write': Dispatching value to "
747 "the `%s' plugin failed with status %i.", plugin_list[i], status);
748 }
749 } /* for (i = 0; plugin_list[i] != NULL; i++) */
750 }
752 return (FC_TARGET_CONTINUE);
753 } /* }}} int fc_bit_write_invoke */
755 static int fc_init_once (void) /* {{{ */
756 {
757 static int done = 0;
758 target_proc_t tproc;
760 if (done != 0)
761 return (0);
763 memset (&tproc, 0, sizeof (tproc));
764 tproc.create = fc_bit_jump_create;
765 tproc.destroy = fc_bit_jump_destroy;
766 tproc.invoke = fc_bit_jump_invoke;
767 fc_register_target ("jump", tproc);
769 memset (&tproc, 0, sizeof (tproc));
770 tproc.create = NULL;
771 tproc.destroy = NULL;
772 tproc.invoke = fc_bit_stop_invoke;
773 fc_register_target ("stop", tproc);
775 memset (&tproc, 0, sizeof (tproc));
776 tproc.create = NULL;
777 tproc.destroy = NULL;
778 tproc.invoke = fc_bit_return_invoke;
779 fc_register_target ("return", tproc);
781 memset (&tproc, 0, sizeof (tproc));
782 tproc.create = fc_bit_write_create;
783 tproc.destroy = fc_bit_write_destroy;
784 tproc.invoke = fc_bit_write_invoke;
785 fc_register_target ("write", tproc);
787 done++;
788 return (0);
789 } /* }}} int fc_init_once */
791 /*
792 * Public functions
793 */
794 /* Add a match to list of available matches. */
795 int fc_register_match (const char *name, match_proc_t proc) /* {{{ */
796 {
797 fc_match_t *m;
799 DEBUG ("fc_register_match (%s);", name);
801 m = (fc_match_t *) malloc (sizeof (*m));
802 if (m == NULL)
803 return (-ENOMEM);
804 memset (m, 0, sizeof (*m));
806 sstrncpy (m->name, name, sizeof (m->name));
807 memcpy (&m->proc, &proc, sizeof (m->proc));
808 m->next = NULL;
810 if (match_list_head == NULL)
811 {
812 match_list_head = m;
813 }
814 else
815 {
816 fc_match_t *ptr;
818 ptr = match_list_head;
819 while (ptr->next != NULL)
820 ptr = ptr->next;
822 ptr->next = m;
823 }
825 return (0);
826 } /* }}} int fc_register_match */
828 /* Add a target to list of available targets. */
829 int fc_register_target (const char *name, target_proc_t proc) /* {{{ */
830 {
831 fc_target_t *t;
833 DEBUG ("fc_register_target (%s);", name);
835 t = (fc_target_t *) malloc (sizeof (*t));
836 if (t == NULL)
837 return (-ENOMEM);
838 memset (t, 0, sizeof (*t));
840 sstrncpy (t->name, name, sizeof (t->name));
841 memcpy (&t->proc, &proc, sizeof (t->proc));
842 t->next = NULL;
844 if (target_list_head == NULL)
845 {
846 target_list_head = t;
847 }
848 else
849 {
850 fc_target_t *ptr;
852 ptr = target_list_head;
853 while (ptr->next != NULL)
854 ptr = ptr->next;
856 ptr->next = t;
857 }
859 return (0);
860 } /* }}} int fc_register_target */
862 fc_chain_t *fc_chain_get_by_name (const char *chain_name) /* {{{ */
863 {
864 fc_chain_t *chain;
866 if (chain_name == NULL)
867 return (NULL);
869 for (chain = chain_list_head; chain != NULL; chain = chain->next)
870 if (strcasecmp (chain_name, chain->name) == 0)
871 return (chain);
873 return (NULL);
874 } /* }}} int fc_chain_get_by_name */
876 int fc_process_chain (const data_set_t *ds, value_list_t *vl, /* {{{ */
877 fc_chain_t *chain)
878 {
879 fc_rule_t *rule;
880 fc_target_t *target;
881 int status;
883 if (chain == NULL)
884 return (-1);
886 DEBUG ("fc_process_chain (chain = %s);", chain->name);
888 status = FC_TARGET_CONTINUE;
889 for (rule = chain->rules; rule != NULL; rule = rule->next)
890 {
891 fc_match_t *match;
893 if (rule->name[0] != 0)
894 {
895 DEBUG ("fc_process_chain (%s): Testing the `%s' rule.",
896 chain->name, rule->name);
897 }
899 /* N. B.: rule->matches may be NULL. */
900 for (match = rule->matches; match != NULL; match = match->next)
901 {
902 /* FIXME: Pass the meta-data to match targets here (when implemented). */
903 status = (*match->proc.match) (ds, vl, /* meta = */ NULL,
904 &match->user_data);
905 if (status < 0)
906 {
907 WARNING ("fc_process_chain (%s): A match failed.", chain->name);
908 break;
909 }
910 else if (status != FC_MATCH_MATCHES)
911 break;
912 }
914 /* for-loop has been aborted: Either error or no match. */
915 if (match != NULL)
916 {
917 status = FC_TARGET_CONTINUE;
918 continue;
919 }
921 if (rule->name[0] != 0)
922 {
923 DEBUG ("fc_process_chain (%s): Rule `%s' matches.",
924 chain->name, rule->name);
925 }
927 for (target = rule->targets; target != NULL; target = target->next)
928 {
929 /* If we get here, all matches have matched the value. Execute the
930 * target. */
931 /* FIXME: Pass the meta-data to match targets here (when implemented). */
932 status = (*target->proc.invoke) (ds, vl, /* meta = */ NULL,
933 &target->user_data);
934 if (status < 0)
935 {
936 WARNING ("fc_process_chain (%s): A target failed.", chain->name);
937 continue;
938 }
939 else if (status == FC_TARGET_CONTINUE)
940 continue;
941 else if (status == FC_TARGET_STOP)
942 break;
943 else if (status == FC_TARGET_RETURN)
944 break;
945 else
946 {
947 WARNING ("fc_process_chain (%s): Unknown return value "
948 "from target `%s': %i",
949 chain->name, target->name, status);
950 }
951 }
953 if ((status == FC_TARGET_STOP)
954 || (status == FC_TARGET_RETURN))
955 {
956 if (rule->name[0] != 0)
957 {
958 DEBUG ("fc_process_chain (%s): Rule `%s' signaled "
959 "the %s condition.",
960 chain->name, rule->name,
961 (status == FC_TARGET_STOP) ? "stop" : "return");
962 }
963 break;
964 }
965 else
966 {
967 status = FC_TARGET_CONTINUE;
968 }
969 } /* for (rule) */
971 if (status == FC_TARGET_STOP)
972 return (FC_TARGET_STOP);
973 else if (status == FC_TARGET_RETURN)
974 return (FC_TARGET_CONTINUE);
976 /* for-loop has been aborted: A target returned `FC_TARGET_STOP' */
977 if (rule != NULL)
978 return (FC_TARGET_CONTINUE);
980 DEBUG ("fc_process_chain (%s): Executing the default targets.",
981 chain->name);
983 status = FC_TARGET_CONTINUE;
984 for (target = chain->targets; target != NULL; target = target->next)
985 {
986 /* If we get here, all matches have matched the value. Execute the
987 * target. */
988 /* FIXME: Pass the meta-data to match targets here (when implemented). */
989 status = (*target->proc.invoke) (ds, vl, /* meta = */ NULL,
990 &target->user_data);
991 if (status < 0)
992 {
993 WARNING ("fc_process_chain (%s): The default target failed.",
994 chain->name);
995 }
996 else if (status == FC_TARGET_CONTINUE)
997 continue;
998 else if (status == FC_TARGET_STOP)
999 break;
1000 else if (status == FC_TARGET_RETURN)
1001 break;
1002 else
1003 {
1004 WARNING ("fc_process_chain (%s): Unknown return value "
1005 "from target `%s': %i",
1006 chain->name, target->name, status);
1007 }
1008 }
1010 if ((status == FC_TARGET_STOP)
1011 || (status == FC_TARGET_RETURN))
1012 {
1013 assert (target != NULL);
1014 DEBUG ("fc_process_chain (%s): Default target `%s' signaled "
1015 "the %s condition.",
1016 chain->name, target->name,
1017 (status == FC_TARGET_STOP) ? "stop" : "return");
1018 if (status == FC_TARGET_STOP)
1019 return (FC_TARGET_STOP);
1020 else
1021 return (FC_TARGET_CONTINUE);
1022 }
1024 DEBUG ("fc_process_chain (%s): Signaling `continue' at end of chain.",
1025 chain->name);
1027 return (FC_TARGET_CONTINUE);
1028 } /* }}} int fc_process_chain */
1030 /* Iterate over all rules in the chain and execute all targets for which all
1031 * matches match. */
1032 int fc_default_action (const data_set_t *ds, value_list_t *vl) /* {{{ */
1033 {
1034 /* FIXME: Pass the meta-data to match targets here (when implemented). */
1035 return (fc_bit_write_invoke (ds, vl,
1036 /* meta = */ NULL, /* user_data = */ NULL));
1037 } /* }}} int fc_default_action */
1039 int fc_configure (const oconfig_item_t *ci) /* {{{ */
1040 {
1041 fc_init_once ();
1043 if (ci == NULL)
1044 return (-EINVAL);
1046 if (strcasecmp ("Chain", ci->key) == 0)
1047 return (fc_config_add_chain (ci));
1049 WARNING ("Filter subsystem: Unknown top level config option `%s'.",
1050 ci->key);
1052 return (-1);
1053 } /* }}} int fc_configure */
1055 /* vim: set sw=2 sts=2 et fdm=marker : */