f5606e90145af0d166af3678794fdfec6091394b
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 subsystem: 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 subsystem: 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 = NULL;
439 int status = 0;
440 int i;
441 int new_chain = 1;
443 if ((ci->values_num != 1)
444 || (ci->values[0].type != OCONFIG_TYPE_STRING))
445 {
446 WARNING ("Filter subsystem: <Chain> blocks require exactly one "
447 "string argument.");
448 return (-1);
449 }
451 if (chain_list_head != NULL)
452 {
453 if ((chain = fc_chain_get_by_name (ci->values[0].value.string)) != NULL)
454 new_chain = 0;
455 }
457 if (chain == NULL)
458 {
459 chain = (fc_chain_t *) malloc (sizeof (*chain));
460 if (chain == NULL)
461 {
462 ERROR ("fc_config_add_chain: malloc failed.");
463 return (-1);
464 }
465 memset (chain, 0, sizeof (*chain));
466 sstrncpy (chain->name, ci->values[0].value.string, sizeof (chain->name));
467 chain->rules = NULL;
468 chain->targets = NULL;
469 chain->next = NULL;
470 }
472 for (i = 0; i < ci->children_num; i++)
473 {
474 oconfig_item_t *option = ci->children + i;
475 status = 0;
477 if (strcasecmp ("Rule", option->key) == 0)
478 status = fc_config_add_rule (chain, option);
479 else if (strcasecmp ("Target", option->key) == 0)
480 status = fc_config_add_target (&chain->targets, option);
481 else
482 {
483 WARNING ("Filter subsystem: Chain %s: Option `%s' not allowed "
484 "inside a <Chain> block.", chain->name, option->key);
485 status = -1;
486 }
488 if (status != 0)
489 break;
490 } /* for (ci->children) */
492 if (status != 0)
493 {
494 fc_free_chains (chain);
495 return (-1);
496 }
498 if (chain_list_head != NULL)
499 {
500 if (!new_chain)
501 return (0);
503 fc_chain_t *ptr;
505 ptr = chain_list_head;
506 while (ptr->next != NULL)
507 ptr = ptr->next;
509 ptr->next = chain;
510 }
511 else
512 {
513 chain_list_head = chain;
514 }
516 return (0);
517 } /* }}} int fc_config_add_chain */
519 /*
520 * Built-in target "jump"
521 *
522 * Prefix `bit' like `_b_uilt-_i_n _t_arget'
523 */
524 static int fc_bit_jump_create (const oconfig_item_t *ci, /* {{{ */
525 void **user_data)
526 {
527 oconfig_item_t *ci_chain;
529 if (ci->children_num != 1)
530 {
531 ERROR ("Filter subsystem: The built-in target `jump' needs exactly "
532 "one `Chain' argument!");
533 return (-1);
534 }
536 ci_chain = ci->children;
537 if (strcasecmp ("Chain", ci_chain->key) != 0)
538 {
539 ERROR ("Filter subsystem: The built-in target `jump' does not "
540 "support the configuration option `%s'.",
541 ci_chain->key);
542 return (-1);
543 }
545 if ((ci_chain->values_num != 1)
546 || (ci_chain->values[0].type != OCONFIG_TYPE_STRING))
547 {
548 ERROR ("Filter subsystem: Built-in target `jump': The `Chain' option "
549 "needs exactly one string argument.");
550 return (-1);
551 }
553 *user_data = fc_strdup (ci_chain->values[0].value.string);
554 if (*user_data == NULL)
555 {
556 ERROR ("fc_bit_jump_create: fc_strdup failed.");
557 return (-1);
558 }
560 return (0);
561 } /* }}} int fc_bit_jump_create */
563 static int fc_bit_jump_destroy (void **user_data) /* {{{ */
564 {
565 if (user_data != NULL)
566 {
567 free (*user_data);
568 *user_data = NULL;
569 }
571 return (0);
572 } /* }}} int fc_bit_jump_destroy */
574 static int fc_bit_jump_invoke (const data_set_t *ds, /* {{{ */
575 value_list_t *vl, notification_meta_t __attribute__((unused)) **meta,
576 void **user_data)
577 {
578 char *chain_name;
579 fc_chain_t *chain;
580 int status;
582 chain_name = *user_data;
584 for (chain = chain_list_head; chain != NULL; chain = chain->next)
585 if (strcasecmp (chain_name, chain->name) == 0)
586 break;
588 if (chain == NULL)
589 {
590 ERROR ("Filter subsystem: Built-in target `jump': There is no chain "
591 "named `%s'.", chain_name);
592 return (-1);
593 }
595 status = fc_process_chain (ds, vl, chain);
596 if (status < 0)
597 return (status);
598 else if (status == FC_TARGET_STOP)
599 return (FC_TARGET_STOP);
600 else
601 return (FC_TARGET_CONTINUE);
602 } /* }}} int fc_bit_jump_invoke */
604 static int fc_bit_stop_invoke (const data_set_t __attribute__((unused)) *ds, /* {{{ */
605 value_list_t __attribute__((unused)) *vl,
606 notification_meta_t __attribute__((unused)) **meta,
607 void __attribute__((unused)) **user_data)
608 {
609 return (FC_TARGET_STOP);
610 } /* }}} int fc_bit_stop_invoke */
612 static int fc_bit_return_invoke (const data_set_t __attribute__((unused)) *ds, /* {{{ */
613 value_list_t __attribute__((unused)) *vl,
614 notification_meta_t __attribute__((unused)) **meta,
615 void __attribute__((unused)) **user_data)
616 {
617 return (FC_TARGET_RETURN);
618 } /* }}} int fc_bit_return_invoke */
620 static int fc_bit_write_create (const oconfig_item_t *ci, /* {{{ */
621 void **user_data)
622 {
623 int i;
625 char **plugin_list;
626 size_t plugin_list_len;
628 plugin_list = NULL;
629 plugin_list_len = 0;
631 for (i = 0; i < ci->children_num; i++)
632 {
633 oconfig_item_t *child = ci->children + i;
634 char **temp;
635 int j;
637 if (strcasecmp ("Plugin", child->key) != 0)
638 {
639 ERROR ("Filter subsystem: The built-in target `write' does not "
640 "support the configuration option `%s'.",
641 child->key);
642 continue;
643 }
645 for (j = 0; j < child->values_num; j++)
646 {
647 if (child->values[j].type != OCONFIG_TYPE_STRING)
648 {
649 ERROR ("Filter subsystem: Built-in target `write': "
650 "The `Plugin' option accepts only string arguments.");
651 continue;
652 }
654 temp = (char **) realloc (plugin_list, (plugin_list_len + 2)
655 * (sizeof (*plugin_list)));
656 if (temp == NULL)
657 {
658 ERROR ("fc_bit_write_create: realloc failed.");
659 continue;
660 }
661 plugin_list = temp;
663 plugin_list[plugin_list_len] = fc_strdup (child->values[j].value.string);
664 if (plugin_list[plugin_list_len] == NULL)
665 {
666 ERROR ("fc_bit_write_create: fc_strdup failed.");
667 continue;
668 }
669 plugin_list_len++;
670 plugin_list[plugin_list_len] = NULL;
671 } /* for (j = 0; j < child->values_num; j++) */
672 } /* for (i = 0; i < ci->children_num; i++) */
674 *user_data = plugin_list;
676 return (0);
677 } /* }}} int fc_bit_write_create */
679 static int fc_bit_write_destroy (void **user_data) /* {{{ */
680 {
681 char **plugin_list;
682 size_t i;
684 if ((user_data == NULL) || (*user_data == NULL))
685 return (0);
687 plugin_list = *user_data;
689 for (i = 0; plugin_list[i] != NULL; i++)
690 free (plugin_list[i]);
691 free (plugin_list);
693 return (0);
694 } /* }}} int fc_bit_write_destroy */
696 static int fc_bit_write_invoke (const data_set_t *ds, /* {{{ */
697 value_list_t *vl, notification_meta_t __attribute__((unused)) **meta,
698 void **user_data)
699 {
700 char **plugin_list;
701 int status;
703 plugin_list = NULL;
704 if (user_data != NULL)
705 plugin_list = *user_data;
707 if ((plugin_list == NULL) || (plugin_list[0] == NULL))
708 {
709 static c_complain_t enoent_complaint = C_COMPLAIN_INIT_STATIC;
711 status = plugin_write (/* plugin = */ NULL, ds, vl);
712 if (status == ENOENT)
713 {
714 /* in most cases this is a permanent error, so use the complain
715 * mechanism rather than spamming the logs */
716 c_complain (LOG_INFO, &enoent_complaint,
717 "Filter subsystem: Built-in target `write': Dispatching value to "
718 "all write plugins failed with status %i (ENOENT). "
719 "Most likely this means you didn't load any write plugins.",
720 status);
721 }
722 else if (status != 0)
723 {
724 INFO ("Filter subsystem: Built-in target `write': Dispatching value to "
725 "all write plugins failed with status %i.", status);
726 }
727 else
728 {
729 assert (status == 0);
730 c_release (LOG_INFO, &enoent_complaint, "Filter subsystem: "
731 "Built-in target `write': Some write plugin is back to normal "
732 "operation. `write' succeeded.");
733 }
734 }
735 else
736 {
737 size_t i;
739 for (i = 0; plugin_list[i] != NULL; i++)
740 {
741 status = plugin_write (plugin_list[i], ds, vl);
742 if (status != 0)
743 {
744 INFO ("Filter subsystem: Built-in target `write': Dispatching value to "
745 "the `%s' plugin failed with status %i.", plugin_list[i], status);
746 }
747 } /* for (i = 0; plugin_list[i] != NULL; i++) */
748 }
750 return (FC_TARGET_CONTINUE);
751 } /* }}} int fc_bit_write_invoke */
753 static int fc_init_once (void) /* {{{ */
754 {
755 static int done = 0;
756 target_proc_t tproc;
758 if (done != 0)
759 return (0);
761 memset (&tproc, 0, sizeof (tproc));
762 tproc.create = fc_bit_jump_create;
763 tproc.destroy = fc_bit_jump_destroy;
764 tproc.invoke = fc_bit_jump_invoke;
765 fc_register_target ("jump", tproc);
767 memset (&tproc, 0, sizeof (tproc));
768 tproc.create = NULL;
769 tproc.destroy = NULL;
770 tproc.invoke = fc_bit_stop_invoke;
771 fc_register_target ("stop", tproc);
773 memset (&tproc, 0, sizeof (tproc));
774 tproc.create = NULL;
775 tproc.destroy = NULL;
776 tproc.invoke = fc_bit_return_invoke;
777 fc_register_target ("return", tproc);
779 memset (&tproc, 0, sizeof (tproc));
780 tproc.create = fc_bit_write_create;
781 tproc.destroy = fc_bit_write_destroy;
782 tproc.invoke = fc_bit_write_invoke;
783 fc_register_target ("write", tproc);
785 done++;
786 return (0);
787 } /* }}} int fc_init_once */
789 /*
790 * Public functions
791 */
792 /* Add a match to list of available matches. */
793 int fc_register_match (const char *name, match_proc_t proc) /* {{{ */
794 {
795 fc_match_t *m;
797 DEBUG ("fc_register_match (%s);", name);
799 m = (fc_match_t *) malloc (sizeof (*m));
800 if (m == NULL)
801 return (-ENOMEM);
802 memset (m, 0, sizeof (*m));
804 sstrncpy (m->name, name, sizeof (m->name));
805 memcpy (&m->proc, &proc, sizeof (m->proc));
806 m->next = NULL;
808 if (match_list_head == NULL)
809 {
810 match_list_head = m;
811 }
812 else
813 {
814 fc_match_t *ptr;
816 ptr = match_list_head;
817 while (ptr->next != NULL)
818 ptr = ptr->next;
820 ptr->next = m;
821 }
823 return (0);
824 } /* }}} int fc_register_match */
826 /* Add a target to list of available targets. */
827 int fc_register_target (const char *name, target_proc_t proc) /* {{{ */
828 {
829 fc_target_t *t;
831 DEBUG ("fc_register_target (%s);", name);
833 t = (fc_target_t *) malloc (sizeof (*t));
834 if (t == NULL)
835 return (-ENOMEM);
836 memset (t, 0, sizeof (*t));
838 sstrncpy (t->name, name, sizeof (t->name));
839 memcpy (&t->proc, &proc, sizeof (t->proc));
840 t->next = NULL;
842 if (target_list_head == NULL)
843 {
844 target_list_head = t;
845 }
846 else
847 {
848 fc_target_t *ptr;
850 ptr = target_list_head;
851 while (ptr->next != NULL)
852 ptr = ptr->next;
854 ptr->next = t;
855 }
857 return (0);
858 } /* }}} int fc_register_target */
860 fc_chain_t *fc_chain_get_by_name (const char *chain_name) /* {{{ */
861 {
862 fc_chain_t *chain;
864 if (chain_name == NULL)
865 return (NULL);
867 for (chain = chain_list_head; chain != NULL; chain = chain->next)
868 if (strcasecmp (chain_name, chain->name) == 0)
869 return (chain);
871 return (NULL);
872 } /* }}} int fc_chain_get_by_name */
874 int fc_process_chain (const data_set_t *ds, value_list_t *vl, /* {{{ */
875 fc_chain_t *chain)
876 {
877 fc_rule_t *rule;
878 fc_target_t *target;
879 int status;
881 if (chain == NULL)
882 return (-1);
884 DEBUG ("fc_process_chain (chain = %s);", chain->name);
886 status = FC_TARGET_CONTINUE;
887 for (rule = chain->rules; rule != NULL; rule = rule->next)
888 {
889 fc_match_t *match;
891 if (rule->name[0] != 0)
892 {
893 DEBUG ("fc_process_chain (%s): Testing the `%s' rule.",
894 chain->name, rule->name);
895 }
897 /* N. B.: rule->matches may be NULL. */
898 for (match = rule->matches; match != NULL; match = match->next)
899 {
900 /* FIXME: Pass the meta-data to match targets here (when implemented). */
901 status = (*match->proc.match) (ds, vl, /* meta = */ NULL,
902 &match->user_data);
903 if (status < 0)
904 {
905 WARNING ("fc_process_chain (%s): A match failed.", chain->name);
906 break;
907 }
908 else if (status != FC_MATCH_MATCHES)
909 break;
910 }
912 /* for-loop has been aborted: Either error or no match. */
913 if (match != NULL)
914 {
915 status = FC_TARGET_CONTINUE;
916 continue;
917 }
919 if (rule->name[0] != 0)
920 {
921 DEBUG ("fc_process_chain (%s): Rule `%s' matches.",
922 chain->name, rule->name);
923 }
925 for (target = rule->targets; target != NULL; target = target->next)
926 {
927 /* If we get here, all matches have matched the value. Execute the
928 * target. */
929 /* FIXME: Pass the meta-data to match targets here (when implemented). */
930 status = (*target->proc.invoke) (ds, vl, /* meta = */ NULL,
931 &target->user_data);
932 if (status < 0)
933 {
934 WARNING ("fc_process_chain (%s): A target failed.", chain->name);
935 continue;
936 }
937 else if (status == FC_TARGET_CONTINUE)
938 continue;
939 else if (status == FC_TARGET_STOP)
940 break;
941 else if (status == FC_TARGET_RETURN)
942 break;
943 else
944 {
945 WARNING ("fc_process_chain (%s): Unknown return value "
946 "from target `%s': %i",
947 chain->name, target->name, status);
948 }
949 }
951 if ((status == FC_TARGET_STOP)
952 || (status == FC_TARGET_RETURN))
953 {
954 if (rule->name[0] != 0)
955 {
956 DEBUG ("fc_process_chain (%s): Rule `%s' signaled "
957 "the %s condition.",
958 chain->name, rule->name,
959 (status == FC_TARGET_STOP) ? "stop" : "return");
960 }
961 break;
962 }
963 else
964 {
965 status = FC_TARGET_CONTINUE;
966 }
967 } /* for (rule) */
969 if (status == FC_TARGET_STOP)
970 return (FC_TARGET_STOP);
971 else if (status == FC_TARGET_RETURN)
972 return (FC_TARGET_CONTINUE);
974 /* for-loop has been aborted: A target returned `FC_TARGET_STOP' */
975 if (rule != NULL)
976 return (FC_TARGET_CONTINUE);
978 DEBUG ("fc_process_chain (%s): Executing the default targets.",
979 chain->name);
981 status = FC_TARGET_CONTINUE;
982 for (target = chain->targets; target != NULL; target = target->next)
983 {
984 /* If we get here, all matches have matched the value. Execute the
985 * target. */
986 /* FIXME: Pass the meta-data to match targets here (when implemented). */
987 status = (*target->proc.invoke) (ds, vl, /* meta = */ NULL,
988 &target->user_data);
989 if (status < 0)
990 {
991 WARNING ("fc_process_chain (%s): The default target failed.",
992 chain->name);
993 }
994 else if (status == FC_TARGET_CONTINUE)
995 continue;
996 else if (status == FC_TARGET_STOP)
997 break;
998 else if (status == FC_TARGET_RETURN)
999 break;
1000 else
1001 {
1002 WARNING ("fc_process_chain (%s): Unknown return value "
1003 "from target `%s': %i",
1004 chain->name, target->name, status);
1005 }
1006 }
1008 if ((status == FC_TARGET_STOP)
1009 || (status == FC_TARGET_RETURN))
1010 {
1011 assert (target != NULL);
1012 DEBUG ("fc_process_chain (%s): Default target `%s' signaled "
1013 "the %s condition.",
1014 chain->name, target->name,
1015 (status == FC_TARGET_STOP) ? "stop" : "return");
1016 if (status == FC_TARGET_STOP)
1017 return (FC_TARGET_STOP);
1018 else
1019 return (FC_TARGET_CONTINUE);
1020 }
1022 DEBUG ("fc_process_chain (%s): Signaling `continue' at end of chain.",
1023 chain->name);
1025 return (FC_TARGET_CONTINUE);
1026 } /* }}} int fc_process_chain */
1028 /* Iterate over all rules in the chain and execute all targets for which all
1029 * matches match. */
1030 int fc_default_action (const data_set_t *ds, value_list_t *vl) /* {{{ */
1031 {
1032 /* FIXME: Pass the meta-data to match targets here (when implemented). */
1033 return (fc_bit_write_invoke (ds, vl,
1034 /* meta = */ NULL, /* user_data = */ NULL));
1035 } /* }}} int fc_default_action */
1037 int fc_configure (const oconfig_item_t *ci) /* {{{ */
1038 {
1039 fc_init_once ();
1041 if (ci == NULL)
1042 return (-EINVAL);
1044 if (strcasecmp ("Chain", ci->key) == 0)
1045 return (fc_config_add_chain (ci));
1047 WARNING ("Filter subsystem: Unknown top level config option `%s'.",
1048 ci->key);
1050 return (-1);
1051 } /* }}} int fc_configure */
1053 /* vim: set sw=2 sts=2 et fdm=marker : */