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 /*
77 * Global variables
78 */
79 static fc_match_t *match_list_head;
80 static fc_target_t *target_list_head;
81 static fc_chain_t *chain_list_head;
83 /*
84 * Private functions
85 */
86 static void fc_free_matches (fc_match_t *m) /* {{{ */
87 {
88 if (m == NULL)
89 return;
91 if (m->proc.destroy != NULL)
92 (*m->proc.destroy) (&m->user_data);
93 else if (m->user_data != NULL)
94 {
95 ERROR ("Filter sybsystem: fc_free_matches: There is user data, but no "
96 "destroy functions has been specified. "
97 "Memory will probably be lost!");
98 }
100 if (m->next != NULL)
101 fc_free_matches (m->next);
103 free (m);
104 } /* }}} void fc_free_matches */
106 static void fc_free_targets (fc_target_t *t) /* {{{ */
107 {
108 if (t == NULL)
109 return;
111 if (t->proc.destroy != NULL)
112 (*t->proc.destroy) (&t->user_data);
113 else if (t->user_data != NULL)
114 {
115 ERROR ("Filter sybsystem: fc_free_targets: There is user data, but no "
116 "destroy functions has been specified. "
117 "Memory will probably be lost!");
118 }
120 if (t->next != NULL)
121 fc_free_targets (t->next);
123 free (t);
124 } /* }}} void fc_free_targets */
126 static void fc_free_rules (fc_rule_t *r) /* {{{ */
127 {
128 if (r == NULL)
129 return;
131 fc_free_matches (r->matches);
132 fc_free_targets (r->targets);
134 if (r->next != NULL)
135 fc_free_rules (r->next);
137 free (r);
138 } /* }}} void fc_free_rules */
140 static void fc_free_chains (fc_chain_t *c) /* {{{ */
141 {
142 if (c == NULL)
143 return;
145 fc_free_rules (c->rules);
146 fc_free_targets (c->targets);
148 if (c->next != NULL)
149 fc_free_chains (c->next);
151 free (c);
152 } /* }}} void fc_free_chains */
154 static char *fc_strdup (const char *orig) /* {{{ */
155 {
156 size_t sz;
157 char *dest;
159 if (orig == NULL)
160 return (NULL);
162 sz = strlen (orig) + 1;
163 dest = (char *) malloc (sz);
164 if (dest == NULL)
165 return (NULL);
167 memcpy (dest, orig, sz);
169 return (dest);
170 } /* }}} char *fc_strdup */
172 /*
173 * Configuration.
174 *
175 * The configuration looks somewhat like this:
176 *
177 * <Chain "PreCache">
178 * <Rule>
179 * <Match "regex">
180 * Plugin "^mysql$"
181 * Type "^mysql_command$"
182 * TypeInstance "^show_"
183 * </Match>
184 * <Target "drop">
185 * </Target>
186 * </Rule>
187 *
188 * <Target "write">
189 * Plugin "rrdtool"
190 * </Target>
191 * </Chain>
192 */
193 static int fc_config_add_match (fc_match_t **matches_head, /* {{{ */
194 oconfig_item_t *ci)
195 {
196 fc_match_t *m;
197 fc_match_t *ptr;
198 int status;
200 if ((ci->values_num != 1)
201 || (ci->values[0].type != OCONFIG_TYPE_STRING))
202 {
203 WARNING ("Filter subsystem: `Match' blocks require "
204 "exactly one string argument.");
205 return (-1);
206 }
208 ptr = match_list_head;
209 while (ptr != NULL)
210 {
211 if (strcasecmp (ptr->name, ci->values[0].value.string) == 0)
212 break;
213 ptr = ptr->next;
214 }
216 if (ptr == NULL)
217 {
218 WARNING ("Filter subsystem: Cannot find a \"%s\" match. "
219 "Did you load the appropriate plugin?",
220 ci->values[0].value.string);
221 return (-1);
222 }
224 m = (fc_match_t *) malloc (sizeof (*m));
225 if (m == NULL)
226 {
227 ERROR ("fc_config_add_match: malloc failed.");
228 return (-1);
229 }
230 memset (m, 0, sizeof (*m));
232 sstrncpy (m->name, ptr->name, sizeof (m->name));
233 memcpy (&m->proc, &ptr->proc, sizeof (m->proc));
234 m->user_data = NULL;
235 m->next = NULL;
237 if (m->proc.create != NULL)
238 {
239 status = (*m->proc.create) (ci, &m->user_data);
240 if (status != 0)
241 {
242 WARNING ("Filter subsystem: Failed to create a %s match.",
243 m->name);
244 fc_free_matches (m);
245 return (-1);
246 }
247 }
249 if (*matches_head != NULL)
250 {
251 ptr = *matches_head;
252 while (ptr->next != NULL)
253 ptr = ptr->next;
255 ptr->next = m;
256 }
257 else
258 {
259 *matches_head = m;
260 }
262 return (0);
263 } /* }}} int fc_config_add_match */
265 static int fc_config_add_target (fc_target_t **targets_head, /* {{{ */
266 oconfig_item_t *ci)
267 {
268 fc_target_t *t;
269 fc_target_t *ptr;
270 int status;
272 if ((ci->values_num != 1)
273 || (ci->values[0].type != OCONFIG_TYPE_STRING))
274 {
275 WARNING ("Filter subsystem: `Target' blocks require "
276 "exactly one string argument.");
277 return (-1);
278 }
280 ptr = target_list_head;
281 while (ptr != NULL)
282 {
283 if (strcasecmp (ptr->name, ci->values[0].value.string) == 0)
284 break;
285 ptr = ptr->next;
286 }
288 if (ptr == NULL)
289 {
290 WARNING ("Filter subsystem: Cannot find a \"%s\" target. "
291 "Did you load the appropriate plugin?",
292 ci->values[0].value.string);
293 return (-1);
294 }
296 t = (fc_target_t *) malloc (sizeof (*t));
297 if (t == NULL)
298 {
299 ERROR ("fc_config_add_target: malloc failed.");
300 return (-1);
301 }
302 memset (t, 0, sizeof (*t));
304 sstrncpy (t->name, ptr->name, sizeof (t->name));
305 memcpy (&t->proc, &ptr->proc, sizeof (t->proc));
306 t->user_data = NULL;
307 t->next = NULL;
309 if (t->proc.create != NULL)
310 {
311 status = (*t->proc.create) (ci, &t->user_data);
312 if (status != 0)
313 {
314 WARNING ("Filter subsystem: Failed to create a %s target.",
315 t->name);
316 fc_free_targets (t);
317 return (-1);
318 }
319 }
320 else
321 {
322 t->user_data = NULL;
323 }
325 if (*targets_head != NULL)
326 {
327 ptr = *targets_head;
328 while (ptr->next != NULL)
329 ptr = ptr->next;
331 ptr->next = t;
332 }
333 else
334 {
335 *targets_head = t;
336 }
338 return (0);
339 } /* }}} int fc_config_add_target */
341 static int fc_config_add_rule (fc_chain_t *chain, /* {{{ */
342 oconfig_item_t *ci)
343 {
344 fc_rule_t *rule;
345 char rule_name[2*DATA_MAX_NAME_LEN] = "Unnamed rule";
346 int status = 0;
347 int i;
349 if (ci->values_num > 1)
350 {
351 WARNING ("Filter subsystem: `Rule' blocks have at most one argument.");
352 return (-1);
353 }
354 else if ((ci->values_num == 1)
355 && (ci->values[0].type != OCONFIG_TYPE_STRING))
356 {
357 WARNING ("Filter subsystem: `Rule' blocks expect one string argument "
358 "or no argument at all.");
359 return (-1);
360 }
362 rule = (fc_rule_t *) malloc (sizeof (*rule));
363 if (rule == NULL)
364 {
365 ERROR ("fc_config_add_rule: malloc failed.");
366 return (-1);
367 }
368 memset (rule, 0, sizeof (*rule));
369 rule->next = NULL;
371 if (ci->values_num == 1)
372 {
373 sstrncpy (rule->name, ci->values[0].value.string, sizeof (rule->name));
374 ssnprintf (rule_name, sizeof (rule_name), "Rule \"%s\"",
375 ci->values[0].value.string);
376 }
378 for (i = 0; i < ci->children_num; i++)
379 {
380 oconfig_item_t *option = ci->children + i;
381 status = 0;
383 if (strcasecmp ("Match", option->key) == 0)
384 status = fc_config_add_match (&rule->matches, option);
385 else if (strcasecmp ("Target", option->key) == 0)
386 status = fc_config_add_target (&rule->targets, option);
387 else
388 {
389 WARNING ("Filter subsystem: %s: Option `%s' not allowed "
390 "inside a <Rule> block.", rule_name, option->key);
391 status = -1;
392 }
394 if (status != 0)
395 break;
396 } /* for (ci->children) */
398 /* Additional sanity checking. */
399 while (status == 0)
400 {
401 if (rule->targets == NULL)
402 {
403 WARNING ("Filter subsystem: %s: No target has been specified.",
404 rule_name);
405 status = -1;
406 break;
407 }
409 break;
410 } /* while (status == 0) */
412 if (status != 0)
413 {
414 fc_free_rules (rule);
415 return (-1);
416 }
418 if (chain->rules != NULL)
419 {
420 fc_rule_t *ptr;
422 ptr = chain->rules;
423 while (ptr->next != NULL)
424 ptr = ptr->next;
426 ptr->next = rule;
427 }
428 else
429 {
430 chain->rules = rule;
431 }
433 return (0);
434 } /* }}} int fc_config_add_rule */
436 static int fc_config_add_chain (const oconfig_item_t *ci) /* {{{ */
437 {
438 fc_chain_t *chain;
439 int status = 0;
440 int i;
442 if ((ci->values_num != 1)
443 || (ci->values[0].type != OCONFIG_TYPE_STRING))
444 {
445 WARNING ("Filter subsystem: <Chain> blocks require exactly one "
446 "string argument.");
447 return (-1);
448 }
450 chain = (fc_chain_t *) malloc (sizeof (*chain));
451 if (chain == NULL)
452 {
453 ERROR ("fc_config_add_chain: malloc failed.");
454 return (-1);
455 }
456 memset (chain, 0, sizeof (*chain));
457 sstrncpy (chain->name, ci->values[0].value.string, sizeof (chain->name));
458 chain->rules = NULL;
459 chain->targets = NULL;
460 chain->next = NULL;
462 for (i = 0; i < ci->children_num; i++)
463 {
464 oconfig_item_t *option = ci->children + i;
465 status = 0;
467 if (strcasecmp ("Rule", option->key) == 0)
468 status = fc_config_add_rule (chain, option);
469 else if (strcasecmp ("Target", option->key) == 0)
470 status = fc_config_add_target (&chain->targets, option);
471 else
472 {
473 WARNING ("Filter subsystem: Chain %s: Option `%s' not allowed "
474 "inside a <Chain> block.", chain->name, option->key);
475 status = -1;
476 }
478 if (status != 0)
479 break;
480 } /* for (ci->children) */
482 if (status != 0)
483 {
484 fc_free_chains (chain);
485 return (-1);
486 }
488 if (chain_list_head != NULL)
489 {
490 fc_chain_t *ptr;
492 ptr = chain_list_head;
493 while (ptr->next != NULL)
494 ptr = ptr->next;
496 ptr->next = chain;
497 }
498 else
499 {
500 chain_list_head = chain;
501 }
503 return (0);
504 } /* }}} int fc_config_add_chain */
506 /*
507 * Built-in target "jump"
508 *
509 * Prefix `bit' like `_b_uilt-_i_n _t_arget'
510 */
511 static int fc_bit_jump_create (const oconfig_item_t *ci, /* {{{ */
512 void **user_data)
513 {
514 oconfig_item_t *ci_chain;
516 if (ci->children_num != 1)
517 {
518 ERROR ("Filter subsystem: The built-in target `jump' needs exactly "
519 "one `Chain' argument!");
520 return (-1);
521 }
523 ci_chain = ci->children;
524 if (strcasecmp ("Chain", ci_chain->key) != 0)
525 {
526 ERROR ("Filter subsystem: The built-in target `jump' does not "
527 "support the configuration option `%s'.",
528 ci_chain->key);
529 return (-1);
530 }
532 if ((ci_chain->values_num != 1)
533 || (ci_chain->values[0].type != OCONFIG_TYPE_STRING))
534 {
535 ERROR ("Filter subsystem: Built-in target `jump': The `Chain' option "
536 "needs exactly one string argument.");
537 return (-1);
538 }
540 *user_data = fc_strdup (ci_chain->values[0].value.string);
541 if (*user_data == NULL)
542 {
543 ERROR ("fc_bit_jump_create: fc_strdup failed.");
544 return (-1);
545 }
547 return (0);
548 } /* }}} int fc_bit_jump_create */
550 static int fc_bit_jump_destroy (void **user_data) /* {{{ */
551 {
552 if (user_data != NULL)
553 {
554 free (*user_data);
555 *user_data = NULL;
556 }
558 return (0);
559 } /* }}} int fc_bit_jump_destroy */
561 static int fc_bit_jump_invoke (const data_set_t *ds, /* {{{ */
562 value_list_t *vl, notification_meta_t __attribute__((unused)) **meta,
563 void **user_data)
564 {
565 char *chain_name;
566 fc_chain_t *chain;
567 int status;
569 chain_name = *user_data;
571 for (chain = chain_list_head; chain != NULL; chain = chain->next)
572 if (strcasecmp (chain_name, chain->name) == 0)
573 break;
575 if (chain == NULL)
576 {
577 ERROR ("Filter subsystem: Built-in target `jump': There is no chain "
578 "named `%s'.", chain_name);
579 return (-1);
580 }
582 status = fc_process_chain (ds, vl, chain);
583 if (status < 0)
584 return (status);
585 else if (status == FC_TARGET_STOP)
586 return (FC_TARGET_STOP);
587 else
588 return (FC_TARGET_CONTINUE);
589 } /* }}} int fc_bit_jump_invoke */
591 static int fc_bit_stop_invoke (const data_set_t __attribute__((unused)) *ds, /* {{{ */
592 value_list_t __attribute__((unused)) *vl,
593 notification_meta_t __attribute__((unused)) **meta,
594 void __attribute__((unused)) **user_data)
595 {
596 return (FC_TARGET_STOP);
597 } /* }}} int fc_bit_stop_invoke */
599 static int fc_bit_return_invoke (const data_set_t __attribute__((unused)) *ds, /* {{{ */
600 value_list_t __attribute__((unused)) *vl,
601 notification_meta_t __attribute__((unused)) **meta,
602 void __attribute__((unused)) **user_data)
603 {
604 return (FC_TARGET_RETURN);
605 } /* }}} int fc_bit_return_invoke */
607 static int fc_bit_write_create (const oconfig_item_t *ci, /* {{{ */
608 void **user_data)
609 {
610 int i;
612 char **plugin_list;
613 size_t plugin_list_len;
615 plugin_list = NULL;
616 plugin_list_len = 0;
618 for (i = 0; i < ci->children_num; i++)
619 {
620 oconfig_item_t *child = ci->children + i;
621 char **temp;
622 int j;
624 if (strcasecmp ("Plugin", child->key) != 0)
625 {
626 ERROR ("Filter subsystem: The built-in target `write' does not "
627 "support the configuration option `%s'.",
628 child->key);
629 continue;
630 }
632 for (j = 0; j < child->values_num; j++)
633 {
634 if (child->values[j].type != OCONFIG_TYPE_STRING)
635 {
636 ERROR ("Filter subsystem: Built-in target `write': "
637 "The `Plugin' option accepts only string arguments.");
638 continue;
639 }
641 temp = (char **) realloc (plugin_list, (plugin_list_len + 2)
642 * (sizeof (*plugin_list)));
643 if (temp == NULL)
644 {
645 ERROR ("fc_bit_write_create: realloc failed.");
646 continue;
647 }
648 plugin_list = temp;
650 plugin_list[plugin_list_len] = fc_strdup (child->values[j].value.string);
651 if (plugin_list[plugin_list_len] == NULL)
652 {
653 ERROR ("fc_bit_write_create: fc_strdup failed.");
654 continue;
655 }
656 plugin_list_len++;
657 plugin_list[plugin_list_len] = NULL;
658 } /* for (j = 0; j < child->values_num; j++) */
659 } /* for (i = 0; i < ci->children_num; i++) */
661 *user_data = plugin_list;
663 return (0);
664 } /* }}} int fc_bit_write_create */
666 static int fc_bit_write_destroy (void **user_data) /* {{{ */
667 {
668 char **plugin_list;
669 size_t i;
671 if ((user_data == NULL) || (*user_data == NULL))
672 return (0);
674 plugin_list = *user_data;
676 for (i = 0; plugin_list[i] != NULL; i++)
677 free (plugin_list[i]);
678 free (plugin_list);
680 return (0);
681 } /* }}} int fc_bit_write_destroy */
683 static int fc_bit_write_invoke (const data_set_t *ds, /* {{{ */
684 value_list_t *vl, notification_meta_t __attribute__((unused)) **meta,
685 void **user_data)
686 {
687 char **plugin_list;
688 int status;
690 plugin_list = NULL;
691 if (user_data != NULL)
692 plugin_list = *user_data;
694 if ((plugin_list == NULL) || (plugin_list[0] == NULL))
695 {
696 static c_complain_t enoent_complaint = C_COMPLAIN_INIT_STATIC;
698 status = plugin_write (/* plugin = */ NULL, ds, vl);
699 if (status == ENOENT)
700 {
701 /* in most cases this is a permanent error, so use the complain
702 * mechanism rather than spamming the logs */
703 c_complain (LOG_INFO, &enoent_complaint,
704 "Filter subsystem: Built-in target `write': Dispatching value to "
705 "all write plugins failed with status %i (ENOENT). "
706 "Most likely this means you didn't load any write plugins.",
707 status);
708 }
709 else if (status != 0)
710 {
711 INFO ("Filter subsystem: Built-in target `write': Dispatching value to "
712 "all write plugins failed with status %i.", status);
713 }
714 else
715 {
716 assert (status == 0);
717 c_release (LOG_INFO, &enoent_complaint, "Filter subsystem: "
718 "Built-in target `write': Some write plugin is back to normal "
719 "operation. `write' succeeded.");
720 }
721 }
722 else
723 {
724 size_t i;
726 for (i = 0; plugin_list[i] != NULL; i++)
727 {
728 status = plugin_write (plugin_list[i], ds, vl);
729 if (status != 0)
730 {
731 INFO ("Filter subsystem: Built-in target `write': Dispatching value to "
732 "the `%s' plugin failed with status %i.", plugin_list[i], status);
733 }
734 } /* for (i = 0; plugin_list[i] != NULL; i++) */
735 }
737 return (FC_TARGET_CONTINUE);
738 } /* }}} int fc_bit_write_invoke */
740 static int fc_init_once (void) /* {{{ */
741 {
742 static int done = 0;
743 target_proc_t tproc;
745 if (done != 0)
746 return (0);
748 memset (&tproc, 0, sizeof (tproc));
749 tproc.create = fc_bit_jump_create;
750 tproc.destroy = fc_bit_jump_destroy;
751 tproc.invoke = fc_bit_jump_invoke;
752 fc_register_target ("jump", tproc);
754 memset (&tproc, 0, sizeof (tproc));
755 tproc.create = NULL;
756 tproc.destroy = NULL;
757 tproc.invoke = fc_bit_stop_invoke;
758 fc_register_target ("stop", tproc);
760 memset (&tproc, 0, sizeof (tproc));
761 tproc.create = NULL;
762 tproc.destroy = NULL;
763 tproc.invoke = fc_bit_return_invoke;
764 fc_register_target ("return", tproc);
766 memset (&tproc, 0, sizeof (tproc));
767 tproc.create = fc_bit_write_create;
768 tproc.destroy = fc_bit_write_destroy;
769 tproc.invoke = fc_bit_write_invoke;
770 fc_register_target ("write", tproc);
772 done++;
773 return (0);
774 } /* }}} int fc_init_once */
776 /*
777 * Public functions
778 */
779 /* Add a match to list of available matches. */
780 int fc_register_match (const char *name, match_proc_t proc) /* {{{ */
781 {
782 fc_match_t *m;
784 DEBUG ("fc_register_match (%s);", name);
786 m = (fc_match_t *) malloc (sizeof (*m));
787 if (m == NULL)
788 return (-ENOMEM);
789 memset (m, 0, sizeof (*m));
791 sstrncpy (m->name, name, sizeof (m->name));
792 memcpy (&m->proc, &proc, sizeof (m->proc));
793 m->next = NULL;
795 if (match_list_head == NULL)
796 {
797 match_list_head = m;
798 }
799 else
800 {
801 fc_match_t *ptr;
803 ptr = match_list_head;
804 while (ptr->next != NULL)
805 ptr = ptr->next;
807 ptr->next = m;
808 }
810 return (0);
811 } /* }}} int fc_register_match */
813 /* Add a target to list of available targets. */
814 int fc_register_target (const char *name, target_proc_t proc) /* {{{ */
815 {
816 fc_target_t *t;
818 DEBUG ("fc_register_target (%s);", name);
820 t = (fc_target_t *) malloc (sizeof (*t));
821 if (t == NULL)
822 return (-ENOMEM);
823 memset (t, 0, sizeof (*t));
825 sstrncpy (t->name, name, sizeof (t->name));
826 memcpy (&t->proc, &proc, sizeof (t->proc));
827 t->next = NULL;
829 if (target_list_head == NULL)
830 {
831 target_list_head = t;
832 }
833 else
834 {
835 fc_target_t *ptr;
837 ptr = target_list_head;
838 while (ptr->next != NULL)
839 ptr = ptr->next;
841 ptr->next = t;
842 }
844 return (0);
845 } /* }}} int fc_register_target */
847 fc_chain_t *fc_chain_get_by_name (const char *chain_name) /* {{{ */
848 {
849 fc_chain_t *chain;
851 if (chain_name == NULL)
852 return (NULL);
854 for (chain = chain_list_head; chain != NULL; chain = chain->next)
855 if (strcasecmp (chain_name, chain->name) == 0)
856 return (chain);
858 return (NULL);
859 } /* }}} int fc_chain_get_by_name */
861 int fc_process_chain (const data_set_t *ds, value_list_t *vl, /* {{{ */
862 fc_chain_t *chain)
863 {
864 fc_rule_t *rule;
865 fc_target_t *target;
866 int status;
868 if (chain == NULL)
869 return (-1);
871 DEBUG ("fc_process_chain (chain = %s);", chain->name);
873 status = FC_TARGET_CONTINUE;
874 for (rule = chain->rules; rule != NULL; rule = rule->next)
875 {
876 fc_match_t *match;
878 if (rule->name[0] != 0)
879 {
880 DEBUG ("fc_process_chain (%s): Testing the `%s' rule.",
881 chain->name, rule->name);
882 }
884 /* N. B.: rule->matches may be NULL. */
885 for (match = rule->matches; match != NULL; match = match->next)
886 {
887 /* FIXME: Pass the meta-data to match targets here (when implemented). */
888 status = (*match->proc.match) (ds, vl, /* meta = */ NULL,
889 &match->user_data);
890 if (status < 0)
891 {
892 WARNING ("fc_process_chain (%s): A match failed.", chain->name);
893 break;
894 }
895 else if (status != FC_MATCH_MATCHES)
896 break;
897 }
899 /* for-loop has been aborted: Either error or no match. */
900 if (match != NULL)
901 {
902 status = FC_TARGET_CONTINUE;
903 continue;
904 }
906 if (rule->name[0] != 0)
907 {
908 DEBUG ("fc_process_chain (%s): Rule `%s' matches.",
909 chain->name, rule->name);
910 }
912 for (target = rule->targets; target != NULL; target = target->next)
913 {
914 /* If we get here, all matches have matched the value. Execute the
915 * target. */
916 /* FIXME: Pass the meta-data to match targets here (when implemented). */
917 status = (*target->proc.invoke) (ds, vl, /* meta = */ NULL,
918 &target->user_data);
919 if (status < 0)
920 {
921 WARNING ("fc_process_chain (%s): A target failed.", chain->name);
922 continue;
923 }
924 else if (status == FC_TARGET_CONTINUE)
925 continue;
926 else if (status == FC_TARGET_STOP)
927 break;
928 else if (status == FC_TARGET_RETURN)
929 break;
930 else
931 {
932 WARNING ("fc_process_chain (%s): Unknown return value "
933 "from target `%s': %i",
934 chain->name, target->name, status);
935 }
936 }
938 if ((status == FC_TARGET_STOP)
939 || (status == FC_TARGET_RETURN))
940 {
941 if (rule->name[0] != 0)
942 {
943 DEBUG ("fc_process_chain (%s): Rule `%s' signaled "
944 "the %s condition.",
945 chain->name, rule->name,
946 (status == FC_TARGET_STOP) ? "stop" : "return");
947 }
948 break;
949 }
950 else
951 {
952 status = FC_TARGET_CONTINUE;
953 }
954 } /* for (rule) */
956 if (status == FC_TARGET_STOP)
957 return (FC_TARGET_STOP);
958 else if (status == FC_TARGET_RETURN)
959 return (FC_TARGET_CONTINUE);
961 /* for-loop has been aborted: A target returned `FC_TARGET_STOP' */
962 if (rule != NULL)
963 return (FC_TARGET_CONTINUE);
965 DEBUG ("fc_process_chain (%s): Executing the default targets.",
966 chain->name);
968 status = FC_TARGET_CONTINUE;
969 for (target = chain->targets; target != NULL; target = target->next)
970 {
971 /* If we get here, all matches have matched the value. Execute the
972 * target. */
973 /* FIXME: Pass the meta-data to match targets here (when implemented). */
974 status = (*target->proc.invoke) (ds, vl, /* meta = */ NULL,
975 &target->user_data);
976 if (status < 0)
977 {
978 WARNING ("fc_process_chain (%s): The default target failed.",
979 chain->name);
980 }
981 else if (status == FC_TARGET_CONTINUE)
982 continue;
983 else if (status == FC_TARGET_STOP)
984 break;
985 else if (status == FC_TARGET_RETURN)
986 break;
987 else
988 {
989 WARNING ("fc_process_chain (%s): Unknown return value "
990 "from target `%s': %i",
991 chain->name, target->name, status);
992 }
993 }
995 if ((status == FC_TARGET_STOP)
996 || (status == FC_TARGET_RETURN))
997 {
998 assert (target != NULL);
999 DEBUG ("fc_process_chain (%s): Default target `%s' signaled "
1000 "the %s condition.",
1001 chain->name, target->name,
1002 (status == FC_TARGET_STOP) ? "stop" : "return");
1003 if (status == FC_TARGET_STOP)
1004 return (FC_TARGET_STOP);
1005 else
1006 return (FC_TARGET_CONTINUE);
1007 }
1009 DEBUG ("fc_process_chain (%s): Signaling `continue' at end of chain.",
1010 chain->name);
1012 return (FC_TARGET_CONTINUE);
1013 } /* }}} int fc_process_chain */
1015 /* Iterate over all rules in the chain and execute all targets for which all
1016 * matches match. */
1017 int fc_default_action (const data_set_t *ds, value_list_t *vl) /* {{{ */
1018 {
1019 /* FIXME: Pass the meta-data to match targets here (when implemented). */
1020 return (fc_bit_write_invoke (ds, vl,
1021 /* meta = */ NULL, /* user_data = */ NULL));
1022 } /* }}} int fc_default_action */
1024 int fc_configure (const oconfig_item_t *ci) /* {{{ */
1025 {
1026 fc_init_once ();
1028 if (ci == NULL)
1029 return (-EINVAL);
1031 if (strcasecmp ("Chain", ci->key) == 0)
1032 return (fc_config_add_chain (ci));
1034 WARNING ("Filter subsystem: Unknown top level config option `%s'.",
1035 ci->key);
1037 return (-1);
1038 } /* }}} int fc_configure */
1040 /* vim: set sw=2 sts=2 et fdm=marker : */