src/filter_chain.c: Add `Target' blocks to `Chain' blocks.
[collectd.git] / src / filter_chain.c
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  **/
21
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
29
30 #ifndef _ISOC99_SOURCE
31 # define _ISOC99_SOURCE
32 #endif
33
34 #ifdef _POSIX_C_SOURCE
35 # undef _POSIX_C_SOURCE
36 #endif
37 #define _POSIX_C_SOURCE 200112L
38
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
46
47 #ifndef _REENTRANT
48 # define _REENTRANT
49 #endif
50
51 #ifndef _THREAD_SAFE
52 # define _THREAD_SAFE
53 #endif
54
55 #ifdef _GNU_SOURCE
56 # undef _GNU_SOURCE
57 #endif
58 /* }}} */
59
60 #include "collectd.h"
61 #include "configfile.h"
62 #include "plugin.h"
63 #include "common.h"
64 #include "filter_chain.h"
65
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 }; /* }}} */
80
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 }; /* }}} */
92
93 /* List of rules, used in fc_chain_t */
94 struct fc_fule_s;
95 typedef struct fc_fule_s fc_rule_t; /* {{{ */
96 struct fc_fule_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 }; /* }}} */
103
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 }; /* }}} */
114
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;
121
122 /*
123  * Private functions
124  */
125 static void fc_free_matches (fc_match_t *m) /* {{{ */
126 {
127   if (m == NULL)
128     return;
129
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   }
138
139   if (m->next != NULL)
140     fc_free_matches (m->next);
141
142   free (m);
143 } /* }}} void fc_free_matches */
144
145 static void fc_free_targets (fc_target_t *t) /* {{{ */
146 {
147   if (t == NULL)
148     return;
149
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   }
158
159   if (t->next != NULL)
160     fc_free_targets (t->next);
161
162   free (t);
163 } /* }}} void fc_free_targets */
164
165 static void fc_free_rules (fc_rule_t *r) /* {{{ */
166 {
167   if (r == NULL)
168     return;
169
170   fc_free_matches (r->matches);
171   fc_free_targets (r->targets);
172
173   if (r->next != NULL)
174     fc_free_rules (r->next);
175
176   free (r);
177 } /* }}} void fc_free_rules */
178
179 static void fc_free_chains (fc_chain_t *c) /* {{{ */
180 {
181   if (c == NULL)
182     return;
183
184   fc_free_rules (c->rules);
185   fc_free_targets (c->targets);
186
187   if (c->next != NULL)
188     fc_free_chains (c->next);
189
190   free (c);
191 } /* }}} void fc_free_chains */
192
193 static char *fc_strdup (const char *orig) /* {{{ */
194 {
195   size_t sz;
196   char *dest;
197
198   if (orig == NULL)
199     return (NULL);
200
201   sz = strlen (orig) + 1;
202   dest = (char *) malloc (sz);
203   if (dest == NULL)
204     return (NULL);
205
206   memcpy (dest, orig, sz);
207
208   return (dest);
209 } /* }}} char *fc_strdup */
210
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;
238
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   }
246
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   }
254
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   }
262
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));
270
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;
276
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   }
285   
286   if (*matches_head != NULL)
287   {
288     ptr = *matches_head;
289     while (ptr->next != NULL)
290       ptr = ptr->next;
291
292     ptr->next = m;
293   }
294   else
295   {
296     *matches_head = m;
297   }
298
299   return (0);
300 } /* }}} int fc_config_add_match */
301
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;
308
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   }
316
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   }
324
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   }
332
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));
340
341   sstrncpy (t->name, ptr->name, sizeof (t->name));
342   memcpy (&t->proc, &ptr->proc, sizeof (t->proc));
343   assert (t->proc.create != NULL);
344   t->user_data = NULL;
345   t->next = NULL;
346
347   status = (*t->proc.create) (ci, &t->user_data);
348   if (status != 0)
349   {
350     WARNING ("Filter subsystem: Failed to create a %s match.",
351         t->name);
352     fc_free_targets (t);
353     return (-1);
354   }
355   
356   if (*targets_head != NULL)
357   {
358     ptr = *targets_head;
359     while (ptr->next != NULL)
360       ptr = ptr->next;
361
362     ptr->next = t;
363   }
364   else
365   {
366     *targets_head = t;
367   }
368
369   return (0);
370 } /* }}} int fc_config_add_target */
371
372 static int fc_config_add_rule (fc_chain_t *chain, /* {{{ */
373     oconfig_item_t *ci)
374 {
375   fc_rule_t *rule;
376   char rule_name[2*DATA_MAX_NAME_LEN] = "Unnamed rule";
377   int status = 0;
378   int i;
379
380   if (ci->values_num > 1)
381   {
382     WARNING ("Filter subsystem: `Rule' blocks have at most one argument.");
383     return (-1);
384   }
385   else if ((ci->values_num == 1)
386       && (ci->values[0].type != OCONFIG_TYPE_STRING))
387   {
388     WARNING ("Filter subsystem: `Rule' blocks expect one string argument "
389         "or no argument at all.");
390     return (-1);
391   }
392
393   rule = (fc_rule_t *) malloc (sizeof (*rule));
394   if (rule == NULL)
395   {
396     ERROR ("fc_config_add_rule: malloc failed.");
397     return (-1);
398   }
399   memset (rule, 0, sizeof (*rule));
400   rule->next = NULL;
401
402   if (ci->values_num == 1)
403   {
404     sstrncpy (rule->name, ci->values[0].value.string, sizeof (rule->name));
405     ssnprintf (rule_name, sizeof (rule_name), "Rule \"%s\"",
406         ci->values[0].value.string);
407   }
408
409   for (i = 0; i < ci->children_num; i++)
410   {
411     oconfig_item_t *option = ci->children + i;
412     status = 0;
413
414     if (strcasecmp ("Match", option->key) == 0)
415       status = fc_config_add_match (&rule->matches, option);
416     else if (strcasecmp ("Target", option->key) == 0)
417       status = fc_config_add_target (&rule->targets, option);
418     else
419     {
420       WARNING ("Filter subsystem: %s: Option `%s' not allowed "
421           "inside a <Rule> block.", rule_name, option->key);
422       status = -1;
423     }
424
425     if (status != 0)
426       break;
427   } /* for (ci->children) */
428
429   /* Additional sanity checking. */
430   while (status == 0)
431   {
432     if (rule->targets == NULL)
433     {
434       WARNING ("Filter subsystem: %s: No target has been specified.",
435           rule_name);
436       status = -1;
437       break;
438     }
439
440     break;
441   } /* while (status == 0) */
442
443   if (status != 0)
444   {
445     fc_free_rules (rule);
446     return (-1);
447   }
448
449   if (chain->rules != NULL)
450   {
451     fc_rule_t *ptr;
452
453     ptr = chain->rules;
454     while (ptr->next != NULL)
455       ptr = ptr->next;
456
457     ptr->next = rule;
458   }
459   else
460   {
461     chain->rules = rule;
462   }
463
464   return (0);
465 } /* }}} int fc_config_add_rule */
466
467 static int fc_config_add_chain (const oconfig_item_t *ci) /* {{{ */
468 {
469   fc_chain_t *chain;
470   int status = 0;
471   int i;
472
473   if ((ci->values_num != 1)
474       || (ci->values[0].type != OCONFIG_TYPE_STRING))
475   {
476     WARNING ("Filter subsystem: <Chain> blocks require exactly one "
477         "string argument.");
478     return (-1);
479   }
480
481   chain = (fc_chain_t *) malloc (sizeof (*chain));
482   if (chain == NULL)
483   {
484     ERROR ("fc_config_add_chain: malloc failed.");
485     return (-1);
486   }
487   memset (chain, 0, sizeof (*chain));
488   sstrncpy (chain->name, ci->values[0].value.string, sizeof (chain->name));
489   chain->rules = NULL;
490   chain->targets = NULL;
491   chain->next = NULL;
492
493   for (i = 0; i < ci->children_num; i++)
494   {
495     oconfig_item_t *option = ci->children + i;
496     status = 0;
497
498     if (strcasecmp ("Rule", option->key) == 0)
499       status = fc_config_add_rule (chain, option);
500     else if (strcasecmp ("Target", option->key) == 0)
501       status = fc_config_add_target (&chain->targets, option);
502     else
503     {
504       WARNING ("Filter subsystem: Chain %s: Option `%s' not allowed "
505           "inside a <Chain> block.", chain->name, option->key);
506       status = -1;
507     }
508
509     if (status != 0)
510       break;
511   } /* for (ci->children) */
512
513   /* Additional sanity checking. */
514   while (status == 0)
515   {
516     if (chain->targets == NULL)
517     {
518       WARNING ("Filter subsystem: Chain %s: No default target has been "
519           "specified. Please make sure that there is a <Target> block within "
520           "the <Chain> block!", chain->name);
521       status = -1;
522       break;
523     }
524
525     break;
526   } /* while (status == 0) */
527
528   if (status != 0)
529   {
530     fc_free_chains (chain);
531     return (-1);
532   }
533
534   if (chain_list_head != NULL)
535   {
536     fc_chain_t *ptr;
537
538     ptr = chain_list_head;
539     while (ptr->next != NULL)
540       ptr = ptr->next;
541
542     ptr->next = chain;
543   }
544   else
545   {
546     chain_list_head = chain;
547   }
548
549   return (0);
550 } /* }}} int fc_config_add_chain */
551
552 int fc_process_chain (const data_set_t *ds, value_list_t *vl, /* {{{ */
553     fc_chain_t *chain)
554 {
555   fc_rule_t *rule;
556   fc_target_t *target;
557   int status;
558
559   if (chain == NULL)
560     return (-1);
561
562   status = FC_ACTION_CONTINUE;
563
564   for (rule = chain->rules; rule != NULL; rule = rule->next)
565   {
566     fc_match_t *match;
567
568     /* N. B.: rule->matches may be NULL. */
569     for (match = rule->matches; match != NULL; match = match->next)
570     {
571       status = (*match->proc.match) (ds, vl, /* meta = */ NULL,
572           &match->user_data);
573       if (status < 0)
574       {
575         WARNING ("fc_process: A match failed.");
576         break;
577       }
578       else if (status != FC_MATCH_MATCHES)
579         break;
580     }
581
582     /* for-loop has been aborted: Either error or no match. */
583     if (match != NULL)
584       continue;
585
586     for (target = rule->targets; target != NULL; target = target->next)
587     {
588       /* If we get here, all matches have matched the value. Execute the target. */
589       status = (*target->proc.invoke) (ds, vl, /* meta = */ NULL,
590           &target->user_data);
591       if (status < 0)
592       {
593         WARNING ("fc_process: A target failed.");
594         continue;
595       }
596       else if (status == FC_ACTION_CONTINUE)
597         continue;
598       else if (status == FC_ACTION_STOP)
599         break;
600       else
601       {
602         WARNING ("fc_process: Unknown target return value: %i", status);
603       }
604     }
605
606     if (status == FC_ACTION_STOP)
607       break;
608   } /* for (rule) */
609
610   /* for-loop has been aborted: A target returned `FC_ACTION_STOP' */
611   if (rule != NULL)
612     return (0);
613
614   for (target = chain->targets; target != NULL; target = target->next)
615   {
616     /* If we get here, all matches have matched the value. Execute the target. */
617     status = (*target->proc.invoke) (ds, vl, /* meta = */ NULL,
618         &target->user_data);
619     if (status < 0)
620     {
621       WARNING ("fc_process: The default target failed.");
622     }
623   }
624
625   return (0);
626 } /* }}} int fc_process_chain */
627
628 /*
629  * Built-in target "jump"
630  *
631  * Prefix `bit' like `_b_uilt-_i_n _t_arget'
632  */
633 static int fc_bit_jump_create (const oconfig_item_t *ci, /* {{{ */
634     void **user_data)
635 {
636   oconfig_item_t *ci_chain;
637
638   if (ci->children_num != 1)
639   {
640     ERROR ("Filter subsystem: The built-in target `jump' needs exactly "
641         "one `Chain' argument!");
642     return (-1);
643   }
644
645   ci_chain = ci->children;
646   if (strcasecmp ("Chain", ci_chain->key) != 0)
647   {
648     ERROR ("Filter subsystem: The built-in target `jump' does not "
649         "support the configuration option `%s'.",
650         ci_chain->key);
651     return (-1);
652   }
653
654   if ((ci_chain->values_num != 1)
655       || (ci_chain->values[0].type != OCONFIG_TYPE_STRING))
656   {
657     ERROR ("Filter subsystem: Built-in target `jump': The `Chain' option "
658         "needs exactly one string argument.");
659     return (-1);
660   }
661
662   *user_data = fc_strdup (ci_chain->values[0].value.string);
663   if (*user_data == NULL)
664   {
665     ERROR ("fc_bit_jump_create: fc_strdup failed.");
666     return (-1);
667   }
668
669   return (0);
670 } /* }}} int fc_bit_jump_create */
671
672 static int fc_bit_jump_destroy (void **user_data) /* {{{ */
673 {
674   if (user_data != NULL)
675   {
676     free (*user_data);
677     *user_data = NULL;
678   }
679
680   return (0);
681 } /* }}} int fc_bit_jump_destroy */
682
683 static int fc_bit_jump_invoke (const data_set_t *ds, /* {{{ */
684     value_list_t *vl, notification_meta_t **meta, void **user_data)
685 {
686   char *chain_name;
687   fc_chain_t *chain;
688   int status;
689
690   chain_name = *user_data;
691
692   for (chain = chain_list_head; chain != NULL; chain = chain->next)
693     if (strcasecmp (chain_name, chain->name) == 0)
694       break;
695
696   if (chain == NULL)
697   {
698     ERROR ("Filter subsystem: Built-in target `jump': There is no chain "
699         "named `%s'.", chain_name);
700     return (-1);
701   }
702
703   status = fc_process_chain (ds, vl, chain);
704   if (status < 0)
705     return (status);
706
707   return (FC_ACTION_CONTINUE);
708 } /* }}} int fc_bit_jump_invoke */
709
710 static int fc_bit_stop_invoke (const data_set_t *ds, /* {{{ */
711     value_list_t *vl, notification_meta_t **meta, void **user_data)
712 {
713   return (FC_ACTION_STOP);
714 } /* }}} int fc_bit_stop_invoke */
715
716 static int fc_bit_write_create (const oconfig_item_t *ci, /* {{{ */
717     void **user_data)
718 {
719   int i;
720
721   char **plugin_list;
722   size_t plugin_list_len;
723
724   plugin_list = NULL;
725   plugin_list_len = 0;
726
727   for (i = 0; i < ci->children_num; i++)
728   {
729     oconfig_item_t *child = ci->children + i;
730     char **temp;
731     int j;
732
733     if (strcasecmp ("Plugin", child->key) != 0)
734     {
735       ERROR ("Filter subsystem: The built-in target `write' does not "
736           "support the configuration option `%s'.",
737           child->key);
738       continue;
739     }
740
741     for (j = 0; j < child->values_num; j++)
742     {
743       if (child->values[j].type != OCONFIG_TYPE_STRING)
744       {
745         ERROR ("Filter subsystem: Built-in target `write': "
746             "The `Plugin' option accepts only string arguments.");
747         continue;
748       }
749
750       temp = (char **) realloc (plugin_list, (plugin_list_len + 2)
751           * (sizeof (*plugin_list)));
752       if (temp == NULL)
753       {
754         ERROR ("fc_bit_write_create: realloc failed.");
755         continue;
756       }
757       plugin_list = temp;
758
759       plugin_list[plugin_list_len] = fc_strdup (child->values[j].value.string);
760       if (plugin_list[plugin_list_len] == NULL)
761       {
762         ERROR ("fc_bit_write_create: fc_strdup failed.");
763         continue;
764       }
765       plugin_list_len++;
766       plugin_list[plugin_list_len] = NULL;
767     } /* for (j = 0; j < child->values_num; j++) */
768   } /* for (i = 0; i < ci->children_num; i++) */
769
770   *user_data = plugin_list;
771
772   return (0);
773 } /* }}} int fc_bit_write_create */
774
775 static int fc_bit_write_destroy (void **user_data) /* {{{ */
776 {
777   char **plugin_list;
778   size_t i;
779
780   if ((user_data == NULL) || (*user_data == NULL))
781     return (0);
782
783   plugin_list = *user_data;
784
785   for (i = 0; plugin_list[i] != NULL; i++)
786     free (plugin_list[i]);
787   free (plugin_list);
788
789   return (0);
790 } /* }}} int fc_bit_write_destroy */
791
792 static int fc_bit_write_invoke (const data_set_t *ds, /* {{{ */
793     value_list_t *vl, notification_meta_t **meta, void **user_data)
794 {
795   char **plugin_list;
796   int status;
797
798   plugin_list = NULL;
799   if (user_data != NULL)
800     plugin_list = *user_data;
801
802   if ((plugin_list == NULL) || (plugin_list[0] == NULL))
803   {
804     status = plugin_write (/* plugin = */ NULL, ds, vl);
805     if (status != 0)
806     {
807       INFO ("Filter subsystem: Built-in target `write': Dispatching value to "
808           "all write plugins failed with status %i.", status);
809     }
810   }
811   else
812   {
813     size_t i;
814
815     for (i = 0; plugin_list[i] != NULL; i++)
816     {
817       status = plugin_write (plugin_list[i], ds, vl);
818       if (status != 0)
819       {
820         INFO ("Filter subsystem: Built-in target `write': Dispatching value to "
821             "the `%s' plugin failed with status %i.", plugin_list[i], status);
822       }
823     } /* for (i = 0; plugin_list[i] != NULL; i++) */
824   }
825
826   return (FC_ACTION_CONTINUE);
827 } /* }}} int fc_bit_write_invoke */
828
829 static int fc_init_once (void) /* {{{ */
830 {
831   static int done = 0;
832   target_proc_t tproc;
833
834   if (done != 0)
835     return (0);
836
837   memset (&tproc, 0, sizeof (tproc));
838   tproc.create  = fc_bit_jump_create;
839   tproc.destroy = fc_bit_jump_destroy;
840   tproc.invoke  = fc_bit_jump_invoke;
841   fc_register_target ("jump", tproc);
842
843   memset (&tproc, 0, sizeof (tproc));
844   tproc.create  = NULL;
845   tproc.destroy = NULL;
846   tproc.invoke  = fc_bit_stop_invoke;
847   fc_register_target ("stop", tproc);
848
849   memset (&tproc, 0, sizeof (tproc));
850   tproc.create  = fc_bit_write_create;
851   tproc.destroy = fc_bit_write_destroy;
852   tproc.invoke  = fc_bit_write_invoke;
853   fc_register_target ("write", tproc);
854
855   done++;
856   return (0);
857 } /* }}} int fc_init_once */
858
859 /*
860  * Public functions
861  */
862 /* Add a match to list of available matches. */
863 int fc_register_match (const char *name, match_proc_t proc) /* {{{ */
864 {
865   fc_match_t *m;
866
867   m = (fc_match_t *) malloc (sizeof (*m));
868   if (m == NULL)
869     return (-ENOMEM);
870   memset (m, 0, sizeof (*m));
871
872   sstrncpy (m->name, name, sizeof (m->name));
873   memcpy (&m->proc, &proc, sizeof (m->proc));
874   m->next = NULL;
875
876   if (match_list_head == NULL)
877   {
878     match_list_head = m;
879   }
880   else
881   {
882     fc_match_t *ptr;
883
884     ptr = match_list_head;
885     while (ptr->next != NULL)
886       ptr = ptr->next;
887
888     ptr->next = m;
889   }
890
891   return (0);
892 } /* }}} int fc_register_match */
893
894 /* Add a target to list of available targets. */
895 int fc_register_target (const char *name, target_proc_t proc) /* {{{ */
896 {
897   fc_target_t *t;
898
899   t = (fc_target_t *) malloc (sizeof (*t));
900   if (t == NULL)
901     return (-ENOMEM);
902   memset (t, 0, sizeof (*t));
903
904   sstrncpy (t->name, name, sizeof (t->name));
905   memcpy (&t->proc, &proc, sizeof (t->proc));
906   t->next = NULL;
907
908   if (target_list_head == NULL)
909   {
910     target_list_head = t;
911   }
912   else
913   {
914     fc_target_t *ptr;
915
916     ptr = target_list_head;
917     while (ptr->next != NULL)
918       ptr = ptr->next;
919
920     ptr->next = t;
921   }
922
923   return (0);
924 } /* }}} int fc_register_target */
925
926 /* Iterate over all rules in the chain and execute all targets for which all
927  * matches match. */
928 int fc_process (const data_set_t *ds, value_list_t *vl) /* {{{ */
929 {
930   fc_chain_t *chain;
931
932   for (chain = chain_list_head; chain != NULL; chain = chain->next)
933     if (strcasecmp ("Main", chain->name) == 0)
934       break;
935
936   if (chain != NULL)
937     return (fc_process_chain (ds, vl, chain));
938
939   ERROR ("fc_process: TODO: Implement default behavior!");
940
941   return (0);
942 } /* }}} int fc_process */
943
944 int fc_configure (const oconfig_item_t *ci) /* {{{ */
945 {
946   fc_init_once ();
947
948   if (ci == NULL)
949     return (-EINVAL);
950
951   if (strcasecmp ("Chain", ci->key) == 0)
952     return (fc_config_add_chain (ci));
953
954   WARNING ("Filter subsystem: Unknown top level config option `%s'.",
955       ci->key);
956
957   return (-1);
958 } /* }}} int fc_configure */
959
960 /* vim: set sw=2 sts=2 et fdm=marker : */