1 /**
2 * collectd - src/filter_chain.h
3 * Copyright (C) 2008 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 typedef struct fc_chain_s fc_chain_t; /* {{{ */
107 struct fc_chain_s
108 {
109 char name[DATA_MAX_NAME_LEN];
110 fc_rule_t *rules;
111 fc_target_t *targets;
112 fc_chain_t *next;
113 }; /* }}} */
115 /*
116 * Global variables
117 */
118 static fc_match_t *match_list_head;
119 static fc_target_t *target_list_head;
120 static fc_chain_t *chain_list_head;
122 /*
123 * Private functions
124 */
125 static void fc_free_matches (fc_match_t *m) /* {{{ */
126 {
127 if (m == NULL)
128 return;
130 if (m->proc.destroy != NULL)
131 (*m->proc.destroy) (&m->user_data);
132 else if (m->user_data != NULL)
133 {
134 ERROR ("Filter sybsystem: fc_free_matches: There is user data, but no "
135 "destroy functions has been specified. "
136 "Memory will probably be lost!");
137 }
139 if (m->next != NULL)
140 fc_free_matches (m->next);
142 free (m);
143 } /* }}} void fc_free_matches */
145 static void fc_free_targets (fc_target_t *t) /* {{{ */
146 {
147 if (t == NULL)
148 return;
150 if (t->proc.destroy != NULL)
151 (*t->proc.destroy) (&t->user_data);
152 else if (t->user_data != NULL)
153 {
154 ERROR ("Filter sybsystem: fc_free_targets: There is user data, but no "
155 "destroy functions has been specified. "
156 "Memory will probably be lost!");
157 }
159 if (t->next != NULL)
160 fc_free_targets (t->next);
162 free (t);
163 } /* }}} void fc_free_targets */
165 static void fc_free_rules (fc_rule_t *r) /* {{{ */
166 {
167 if (r == NULL)
168 return;
170 fc_free_matches (r->matches);
171 fc_free_targets (r->targets);
173 if (r->next != NULL)
174 fc_free_rules (r->next);
176 free (r);
177 } /* }}} void fc_free_rules */
179 static void fc_free_chains (fc_chain_t *c) /* {{{ */
180 {
181 if (c == NULL)
182 return;
184 fc_free_rules (c->rules);
185 fc_free_targets (c->targets);
187 if (c->next != NULL)
188 fc_free_chains (c->next);
190 free (c);
191 } /* }}} void fc_free_chains */
193 static char *fc_strdup (const char *orig) /* {{{ */
194 {
195 size_t sz;
196 char *dest;
198 if (orig == NULL)
199 return (NULL);
201 sz = strlen (orig) + 1;
202 dest = (char *) malloc (sz);
203 if (dest == NULL)
204 return (NULL);
206 memcpy (dest, orig, sz);
208 return (dest);
209 } /* }}} char *fc_strdup */
211 /*
212 * Configuration.
213 *
214 * The configuration looks somewhat like this:
215 *
216 * <Chain "main">
217 * <Rule>
218 * <Match "regex">
219 * Plugin "^mysql$"
220 * Type "^mysql_command$"
221 * TypeInstance "^show_"
222 * </Match>
223 * <Target "drop">
224 * </Target>
225 * </Rule>
226 *
227 * <Target "write">
228 * Plugin "rrdtool"
229 * </Target>
230 * </Chain>
231 */
232 static int fc_config_add_match (fc_match_t **matches_head, /* {{{ */
233 oconfig_item_t *ci)
234 {
235 fc_match_t *m;
236 fc_match_t *ptr;
237 int status;
239 if ((ci->values_num != 1)
240 || (ci->values[0].type != OCONFIG_TYPE_STRING))
241 {
242 WARNING ("Filter subsystem: `Match' blocks require "
243 "exactly one string argument.");
244 return (-1);
245 }
247 ptr = match_list_head;
248 while (ptr != NULL)
249 {
250 if (strcasecmp (ptr->name, ci->values[0].value.string) == 0)
251 break;
252 ptr = ptr->next;
253 }
255 if (ptr == NULL)
256 {
257 WARNING ("Filter subsystem: Cannot find a \"%s\" match. "
258 "Did you load the appropriate plugin?",
259 ci->values[0].value.string);
260 return (-1);
261 }
263 m = (fc_match_t *) malloc (sizeof (*m));
264 if (m == NULL)
265 {
266 ERROR ("fc_config_add_match: malloc failed.");
267 return (-1);
268 }
269 memset (m, 0, sizeof (*m));
271 sstrncpy (m->name, ptr->name, sizeof (m->name));
272 memcpy (&m->proc, &ptr->proc, sizeof (m->proc));
273 assert (m->proc.create != NULL);
274 m->user_data = NULL;
275 m->next = NULL;
277 status = (*m->proc.create) (ci, &m->user_data);
278 if (status != 0)
279 {
280 WARNING ("Filter subsystem: Failed to create a %s match.",
281 m->name);
282 fc_free_matches (m);
283 return (-1);
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_match: 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 match.",
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 /* Additional sanity checking. */
520 while (status == 0)
521 {
522 if (chain->targets == NULL)
523 {
524 WARNING ("Filter subsystem: Chain %s: No default target has been "
525 "specified. Please make sure that there is a <Target> block within "
526 "the <Chain> block!", chain->name);
527 status = -1;
528 break;
529 }
531 break;
532 } /* while (status == 0) */
534 if (status != 0)
535 {
536 fc_free_chains (chain);
537 return (-1);
538 }
540 if (chain_list_head != NULL)
541 {
542 fc_chain_t *ptr;
544 ptr = chain_list_head;
545 while (ptr->next != NULL)
546 ptr = ptr->next;
548 ptr->next = chain;
549 }
550 else
551 {
552 chain_list_head = chain;
553 }
555 return (0);
556 } /* }}} int fc_config_add_chain */
558 int fc_process_chain (const data_set_t *ds, value_list_t *vl, /* {{{ */
559 fc_chain_t *chain)
560 {
561 fc_rule_t *rule;
562 fc_target_t *target;
563 int status;
565 if (chain == NULL)
566 return (-1);
568 DEBUG ("fc_process_chain (chain = %s);", chain->name);
570 status = FC_TARGET_CONTINUE;
571 for (rule = chain->rules; rule != NULL; rule = rule->next)
572 {
573 fc_match_t *match;
575 if (rule->name[0] != 0)
576 {
577 DEBUG ("fc_process_chain (%s): Testing the `%s' rule.",
578 chain->name, rule->name);
579 }
581 /* N. B.: rule->matches may be NULL. */
582 for (match = rule->matches; match != NULL; match = match->next)
583 {
584 status = (*match->proc.match) (ds, vl, /* meta = */ NULL,
585 &match->user_data);
586 if (status < 0)
587 {
588 WARNING ("fc_process_chain (%s): A match failed.", chain->name);
589 break;
590 }
591 else if (status != FC_MATCH_MATCHES)
592 break;
593 }
595 /* for-loop has been aborted: Either error or no match. */
596 if (match != NULL)
597 {
598 status = FC_TARGET_CONTINUE;
599 continue;
600 }
602 if (rule->name[0] != 0)
603 {
604 DEBUG ("fc_process_chain (%s): Rule `%s' matches.",
605 chain->name, rule->name);
606 }
608 for (target = rule->targets; target != NULL; target = target->next)
609 {
610 /* If we get here, all matches have matched the value. Execute the
611 * target. */
612 status = (*target->proc.invoke) (ds, vl, /* meta = */ NULL,
613 &target->user_data);
614 if (status < 0)
615 {
616 WARNING ("fc_process_chain (%s): A target failed.", chain->name);
617 continue;
618 }
619 else if (status == FC_TARGET_CONTINUE)
620 continue;
621 else if (status == FC_TARGET_STOP)
622 break;
623 else if (status == FC_TARGET_RETURN)
624 break;
625 else
626 {
627 WARNING ("fc_process_chain (%s): Unknown return value "
628 "from target `%s': %i",
629 chain->name, target->name, status);
630 }
631 }
633 if ((status == FC_TARGET_STOP)
634 || (status == FC_TARGET_RETURN))
635 {
636 if (rule->name[0] != 0)
637 {
638 DEBUG ("fc_process_chain (%s): Rule `%s' signaled "
639 "the %s condition.",
640 chain->name, rule->name,
641 (status == FC_TARGET_STOP) ? "stop" : "return");
642 }
643 break;
644 }
645 else
646 {
647 status = FC_TARGET_CONTINUE;
648 }
649 } /* for (rule) */
651 if (status == FC_TARGET_STOP)
652 return (FC_TARGET_STOP);
653 else if (status == FC_TARGET_RETURN)
654 return (FC_TARGET_CONTINUE);
656 /* for-loop has been aborted: A target returned `FC_TARGET_STOP' */
657 if (rule != NULL)
658 return (FC_TARGET_CONTINUE);
660 DEBUG ("fc_process_chain (%s): Executing the default targets.",
661 chain->name);
663 status = FC_TARGET_CONTINUE;
664 for (target = chain->targets; target != NULL; target = target->next)
665 {
666 /* If we get here, all matches have matched the value. Execute the
667 * target. */
668 status = (*target->proc.invoke) (ds, vl, /* meta = */ NULL,
669 &target->user_data);
670 if (status < 0)
671 {
672 WARNING ("fc_process_chain (%s): The default target failed.",
673 chain->name);
674 }
675 else if (status == FC_TARGET_CONTINUE)
676 continue;
677 else if (status == FC_TARGET_STOP)
678 break;
679 else if (status == FC_TARGET_RETURN)
680 break;
681 else
682 {
683 WARNING ("fc_process_chain (%s): Unknown return value "
684 "from target `%s': %i",
685 chain->name, target->name, status);
686 }
687 }
689 if ((status == FC_TARGET_STOP)
690 || (status == FC_TARGET_RETURN))
691 {
692 assert (target != NULL);
693 DEBUG ("fc_process_chain (%s): Default target `%s' signaled "
694 "the %s condition.",
695 chain->name, target->name,
696 (status == FC_TARGET_STOP) ? "stop" : "return");
697 if (status == FC_TARGET_STOP)
698 return (FC_TARGET_STOP);
699 else
700 return (FC_TARGET_CONTINUE);
701 }
703 DEBUG ("fc_process_chain (%s): Signaling `continue' at end of chain.",
704 chain->name);
706 return (FC_TARGET_CONTINUE);
707 } /* }}} int fc_process_chain */
709 /*
710 * Built-in target "jump"
711 *
712 * Prefix `bit' like `_b_uilt-_i_n _t_arget'
713 */
714 static int fc_bit_jump_create (const oconfig_item_t *ci, /* {{{ */
715 void **user_data)
716 {
717 oconfig_item_t *ci_chain;
719 if (ci->children_num != 1)
720 {
721 ERROR ("Filter subsystem: The built-in target `jump' needs exactly "
722 "one `Chain' argument!");
723 return (-1);
724 }
726 ci_chain = ci->children;
727 if (strcasecmp ("Chain", ci_chain->key) != 0)
728 {
729 ERROR ("Filter subsystem: The built-in target `jump' does not "
730 "support the configuration option `%s'.",
731 ci_chain->key);
732 return (-1);
733 }
735 if ((ci_chain->values_num != 1)
736 || (ci_chain->values[0].type != OCONFIG_TYPE_STRING))
737 {
738 ERROR ("Filter subsystem: Built-in target `jump': The `Chain' option "
739 "needs exactly one string argument.");
740 return (-1);
741 }
743 *user_data = fc_strdup (ci_chain->values[0].value.string);
744 if (*user_data == NULL)
745 {
746 ERROR ("fc_bit_jump_create: fc_strdup failed.");
747 return (-1);
748 }
750 return (0);
751 } /* }}} int fc_bit_jump_create */
753 static int fc_bit_jump_destroy (void **user_data) /* {{{ */
754 {
755 if (user_data != NULL)
756 {
757 free (*user_data);
758 *user_data = NULL;
759 }
761 return (0);
762 } /* }}} int fc_bit_jump_destroy */
764 static int fc_bit_jump_invoke (const data_set_t *ds, /* {{{ */
765 value_list_t *vl, notification_meta_t **meta, void **user_data)
766 {
767 char *chain_name;
768 fc_chain_t *chain;
769 int status;
771 chain_name = *user_data;
773 for (chain = chain_list_head; chain != NULL; chain = chain->next)
774 if (strcasecmp (chain_name, chain->name) == 0)
775 break;
777 if (chain == NULL)
778 {
779 ERROR ("Filter subsystem: Built-in target `jump': There is no chain "
780 "named `%s'.", chain_name);
781 return (-1);
782 }
784 status = fc_process_chain (ds, vl, chain);
785 if (status < 0)
786 return (status);
787 else if (status == FC_TARGET_STOP)
788 return (FC_TARGET_STOP);
789 else
790 return (FC_TARGET_CONTINUE);
791 } /* }}} int fc_bit_jump_invoke */
793 static int fc_bit_stop_invoke (const data_set_t *ds, /* {{{ */
794 value_list_t *vl, notification_meta_t **meta, void **user_data)
795 {
796 return (FC_TARGET_STOP);
797 } /* }}} int fc_bit_stop_invoke */
799 static int fc_bit_return_invoke (const data_set_t *ds, /* {{{ */
800 value_list_t *vl, notification_meta_t **meta, void **user_data)
801 {
802 return (FC_TARGET_RETURN);
803 } /* }}} int fc_bit_return_invoke */
805 static int fc_bit_write_create (const oconfig_item_t *ci, /* {{{ */
806 void **user_data)
807 {
808 int i;
810 char **plugin_list;
811 size_t plugin_list_len;
813 plugin_list = NULL;
814 plugin_list_len = 0;
816 for (i = 0; i < ci->children_num; i++)
817 {
818 oconfig_item_t *child = ci->children + i;
819 char **temp;
820 int j;
822 if (strcasecmp ("Plugin", child->key) != 0)
823 {
824 ERROR ("Filter subsystem: The built-in target `write' does not "
825 "support the configuration option `%s'.",
826 child->key);
827 continue;
828 }
830 for (j = 0; j < child->values_num; j++)
831 {
832 if (child->values[j].type != OCONFIG_TYPE_STRING)
833 {
834 ERROR ("Filter subsystem: Built-in target `write': "
835 "The `Plugin' option accepts only string arguments.");
836 continue;
837 }
839 temp = (char **) realloc (plugin_list, (plugin_list_len + 2)
840 * (sizeof (*plugin_list)));
841 if (temp == NULL)
842 {
843 ERROR ("fc_bit_write_create: realloc failed.");
844 continue;
845 }
846 plugin_list = temp;
848 plugin_list[plugin_list_len] = fc_strdup (child->values[j].value.string);
849 if (plugin_list[plugin_list_len] == NULL)
850 {
851 ERROR ("fc_bit_write_create: fc_strdup failed.");
852 continue;
853 }
854 plugin_list_len++;
855 plugin_list[plugin_list_len] = NULL;
856 } /* for (j = 0; j < child->values_num; j++) */
857 } /* for (i = 0; i < ci->children_num; i++) */
859 *user_data = plugin_list;
861 return (0);
862 } /* }}} int fc_bit_write_create */
864 static int fc_bit_write_destroy (void **user_data) /* {{{ */
865 {
866 char **plugin_list;
867 size_t i;
869 if ((user_data == NULL) || (*user_data == NULL))
870 return (0);
872 plugin_list = *user_data;
874 for (i = 0; plugin_list[i] != NULL; i++)
875 free (plugin_list[i]);
876 free (plugin_list);
878 return (0);
879 } /* }}} int fc_bit_write_destroy */
881 static int fc_bit_write_invoke (const data_set_t *ds, /* {{{ */
882 value_list_t *vl, notification_meta_t **meta, void **user_data)
883 {
884 char **plugin_list;
885 int status;
887 plugin_list = NULL;
888 if (user_data != NULL)
889 plugin_list = *user_data;
891 if ((plugin_list == NULL) || (plugin_list[0] == NULL))
892 {
893 status = plugin_write (/* plugin = */ NULL, ds, vl);
894 if (status != 0)
895 {
896 INFO ("Filter subsystem: Built-in target `write': Dispatching value to "
897 "all write plugins failed with status %i.", status);
898 }
899 }
900 else
901 {
902 size_t i;
904 for (i = 0; plugin_list[i] != NULL; i++)
905 {
906 status = plugin_write (plugin_list[i], ds, vl);
907 if (status != 0)
908 {
909 INFO ("Filter subsystem: Built-in target `write': Dispatching value to "
910 "the `%s' plugin failed with status %i.", plugin_list[i], status);
911 }
912 } /* for (i = 0; plugin_list[i] != NULL; i++) */
913 }
915 return (FC_TARGET_CONTINUE);
916 } /* }}} int fc_bit_write_invoke */
918 static int fc_init_once (void) /* {{{ */
919 {
920 static int done = 0;
921 target_proc_t tproc;
923 if (done != 0)
924 return (0);
926 memset (&tproc, 0, sizeof (tproc));
927 tproc.create = fc_bit_jump_create;
928 tproc.destroy = fc_bit_jump_destroy;
929 tproc.invoke = fc_bit_jump_invoke;
930 fc_register_target ("jump", tproc);
932 memset (&tproc, 0, sizeof (tproc));
933 tproc.create = NULL;
934 tproc.destroy = NULL;
935 tproc.invoke = fc_bit_stop_invoke;
936 fc_register_target ("stop", tproc);
938 memset (&tproc, 0, sizeof (tproc));
939 tproc.create = NULL;
940 tproc.destroy = NULL;
941 tproc.invoke = fc_bit_return_invoke;
942 fc_register_target ("return", tproc);
944 memset (&tproc, 0, sizeof (tproc));
945 tproc.create = fc_bit_write_create;
946 tproc.destroy = fc_bit_write_destroy;
947 tproc.invoke = fc_bit_write_invoke;
948 fc_register_target ("write", tproc);
950 done++;
951 return (0);
952 } /* }}} int fc_init_once */
954 /*
955 * Public functions
956 */
957 /* Add a match to list of available matches. */
958 int fc_register_match (const char *name, match_proc_t proc) /* {{{ */
959 {
960 fc_match_t *m;
962 DEBUG ("fc_register_match (%s);", name);
964 m = (fc_match_t *) malloc (sizeof (*m));
965 if (m == NULL)
966 return (-ENOMEM);
967 memset (m, 0, sizeof (*m));
969 sstrncpy (m->name, name, sizeof (m->name));
970 memcpy (&m->proc, &proc, sizeof (m->proc));
971 m->next = NULL;
973 if (match_list_head == NULL)
974 {
975 match_list_head = m;
976 }
977 else
978 {
979 fc_match_t *ptr;
981 ptr = match_list_head;
982 while (ptr->next != NULL)
983 ptr = ptr->next;
985 ptr->next = m;
986 }
988 return (0);
989 } /* }}} int fc_register_match */
991 /* Add a target to list of available targets. */
992 int fc_register_target (const char *name, target_proc_t proc) /* {{{ */
993 {
994 fc_target_t *t;
996 DEBUG ("fc_register_target (%s);", name);
998 t = (fc_target_t *) malloc (sizeof (*t));
999 if (t == NULL)
1000 return (-ENOMEM);
1001 memset (t, 0, sizeof (*t));
1003 sstrncpy (t->name, name, sizeof (t->name));
1004 memcpy (&t->proc, &proc, sizeof (t->proc));
1005 t->next = NULL;
1007 if (target_list_head == NULL)
1008 {
1009 target_list_head = t;
1010 }
1011 else
1012 {
1013 fc_target_t *ptr;
1015 ptr = target_list_head;
1016 while (ptr->next != NULL)
1017 ptr = ptr->next;
1019 ptr->next = t;
1020 }
1022 return (0);
1023 } /* }}} int fc_register_target */
1025 /* Iterate over all rules in the chain and execute all targets for which all
1026 * matches match. */
1027 int fc_process (const data_set_t *ds, value_list_t *vl) /* {{{ */
1028 {
1029 fc_chain_t *chain;
1031 for (chain = chain_list_head; chain != NULL; chain = chain->next)
1032 if (strcasecmp ("Main", chain->name) == 0)
1033 break;
1035 if (chain != NULL)
1036 return (fc_process_chain (ds, vl, chain));
1038 return (fc_bit_write_invoke (ds, vl,
1039 /* meta = */ NULL, /* user_data = */ NULL));
1040 } /* }}} int fc_process */
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 : */