src/filter_chain.c: Document the `meta' argument passed to matches and targets inline.
[collectd.git] / src / filter_chain.c
1 /**
2  * collectd - src/filter_chain.h
3  * Copyright (C) 2008,2009  Florian octo Forster
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; only version 2 of the License is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  * Authors:
19  *   Florian octo Forster <octo at verplant.org>
20  **/
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_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 }; /* }}} */
103
104 /* List of chains, used for `chain_list_head' */
105 struct fc_chain_s /* {{{ */
106 {
107   char name[DATA_MAX_NAME_LEN];
108   fc_rule_t   *rules;
109   fc_target_t *targets;
110   fc_chain_t  *next;
111 }; /* }}} */
112
113 /*
114  * Global variables
115  */
116 static fc_match_t  *match_list_head;
117 static fc_target_t *target_list_head;
118 static fc_chain_t  *chain_list_head;
119
120 /*
121  * Private functions
122  */
123 static void fc_free_matches (fc_match_t *m) /* {{{ */
124 {
125   if (m == NULL)
126     return;
127
128   if (m->proc.destroy != NULL)
129     (*m->proc.destroy) (&m->user_data);
130   else if (m->user_data != NULL)
131   {
132     ERROR ("Filter sybsystem: fc_free_matches: There is user data, but no "
133         "destroy functions has been specified. "
134         "Memory will probably be lost!");
135   }
136
137   if (m->next != NULL)
138     fc_free_matches (m->next);
139
140   free (m);
141 } /* }}} void fc_free_matches */
142
143 static void fc_free_targets (fc_target_t *t) /* {{{ */
144 {
145   if (t == NULL)
146     return;
147
148   if (t->proc.destroy != NULL)
149     (*t->proc.destroy) (&t->user_data);
150   else if (t->user_data != NULL)
151   {
152     ERROR ("Filter sybsystem: fc_free_targets: There is user data, but no "
153         "destroy functions has been specified. "
154         "Memory will probably be lost!");
155   }
156
157   if (t->next != NULL)
158     fc_free_targets (t->next);
159
160   free (t);
161 } /* }}} void fc_free_targets */
162
163 static void fc_free_rules (fc_rule_t *r) /* {{{ */
164 {
165   if (r == NULL)
166     return;
167
168   fc_free_matches (r->matches);
169   fc_free_targets (r->targets);
170
171   if (r->next != NULL)
172     fc_free_rules (r->next);
173
174   free (r);
175 } /* }}} void fc_free_rules */
176
177 static void fc_free_chains (fc_chain_t *c) /* {{{ */
178 {
179   if (c == NULL)
180     return;
181
182   fc_free_rules (c->rules);
183   fc_free_targets (c->targets);
184
185   if (c->next != NULL)
186     fc_free_chains (c->next);
187
188   free (c);
189 } /* }}} void fc_free_chains */
190
191 static char *fc_strdup (const char *orig) /* {{{ */
192 {
193   size_t sz;
194   char *dest;
195
196   if (orig == NULL)
197     return (NULL);
198
199   sz = strlen (orig) + 1;
200   dest = (char *) malloc (sz);
201   if (dest == NULL)
202     return (NULL);
203
204   memcpy (dest, orig, sz);
205
206   return (dest);
207 } /* }}} char *fc_strdup */
208
209 /*
210  * Configuration.
211  *
212  * The configuration looks somewhat like this:
213  *
214  *  <Chain "main">
215  *    <Rule>
216  *      <Match "regex">
217  *        Plugin "^mysql$"
218  *        Type "^mysql_command$"
219  *        TypeInstance "^show_"
220  *      </Match>
221  *      <Target "drop">
222  *      </Target>
223  *    </Rule>
224  *
225  *    <Target "write">
226  *      Plugin "rrdtool"
227  *    </Target>
228  *  </Chain>
229  */
230 static int fc_config_add_match (fc_match_t **matches_head, /* {{{ */
231     oconfig_item_t *ci)
232 {
233   fc_match_t *m;
234   fc_match_t *ptr;
235   int status;
236
237   if ((ci->values_num != 1)
238       || (ci->values[0].type != OCONFIG_TYPE_STRING))
239   {
240     WARNING ("Filter subsystem: `Match' blocks require "
241         "exactly one string argument.");
242     return (-1);
243   }
244
245   ptr = match_list_head;
246   while (ptr != NULL)
247   {
248     if (strcasecmp (ptr->name, ci->values[0].value.string) == 0)
249       break;
250     ptr = ptr->next;
251   }
252
253   if (ptr == NULL)
254   {
255     WARNING ("Filter subsystem: Cannot find a \"%s\" match. "
256         "Did you load the appropriate plugin?",
257         ci->values[0].value.string);
258     return (-1);
259   }
260
261   m = (fc_match_t *) malloc (sizeof (*m));
262   if (m == NULL)
263   {
264     ERROR ("fc_config_add_match: malloc failed.");
265     return (-1);
266   }
267   memset (m, 0, sizeof (*m));
268
269   sstrncpy (m->name, ptr->name, sizeof (m->name));
270   memcpy (&m->proc, &ptr->proc, sizeof (m->proc));
271   assert (m->proc.create != NULL);
272   m->user_data = NULL;
273   m->next = NULL;
274
275   status = (*m->proc.create) (ci, &m->user_data);
276   if (status != 0)
277   {
278     WARNING ("Filter subsystem: Failed to create a %s match.",
279         m->name);
280     fc_free_matches (m);
281     return (-1);
282   }
283   
284   if (*matches_head != NULL)
285   {
286     ptr = *matches_head;
287     while (ptr->next != NULL)
288       ptr = ptr->next;
289
290     ptr->next = m;
291   }
292   else
293   {
294     *matches_head = m;
295   }
296
297   return (0);
298 } /* }}} int fc_config_add_match */
299
300 static int fc_config_add_target (fc_target_t **targets_head, /* {{{ */
301     oconfig_item_t *ci)
302 {
303   fc_target_t *t;
304   fc_target_t *ptr;
305   int status;
306
307   if ((ci->values_num != 1)
308       || (ci->values[0].type != OCONFIG_TYPE_STRING))
309   {
310     WARNING ("Filter subsystem: `Target' blocks require "
311         "exactly one string argument.");
312     return (-1);
313   }
314
315   ptr = target_list_head;
316   while (ptr != NULL)
317   {
318     if (strcasecmp (ptr->name, ci->values[0].value.string) == 0)
319       break;
320     ptr = ptr->next;
321   }
322
323   if (ptr == NULL)
324   {
325     WARNING ("Filter subsystem: Cannot find a \"%s\" target. "
326         "Did you load the appropriate plugin?",
327         ci->values[0].value.string);
328     return (-1);
329   }
330
331   t = (fc_target_t *) malloc (sizeof (*t));
332   if (t == NULL)
333   {
334     ERROR ("fc_config_add_match: malloc failed.");
335     return (-1);
336   }
337   memset (t, 0, sizeof (*t));
338
339   sstrncpy (t->name, ptr->name, sizeof (t->name));
340   memcpy (&t->proc, &ptr->proc, sizeof (t->proc));
341   t->user_data = NULL;
342   t->next = NULL;
343
344   if (t->proc.create != NULL)
345   {
346     status = (*t->proc.create) (ci, &t->user_data);
347     if (status != 0)
348     {
349       WARNING ("Filter subsystem: Failed to create a %s match.",
350           t->name);
351       fc_free_targets (t);
352       return (-1);
353     }
354   }
355   else
356   {
357     t->user_data = NULL;
358   }
359   
360   if (*targets_head != NULL)
361   {
362     ptr = *targets_head;
363     while (ptr->next != NULL)
364       ptr = ptr->next;
365
366     ptr->next = t;
367   }
368   else
369   {
370     *targets_head = t;
371   }
372
373   return (0);
374 } /* }}} int fc_config_add_target */
375
376 static int fc_config_add_rule (fc_chain_t *chain, /* {{{ */
377     oconfig_item_t *ci)
378 {
379   fc_rule_t *rule;
380   char rule_name[2*DATA_MAX_NAME_LEN] = "Unnamed rule";
381   int status = 0;
382   int i;
383
384   if (ci->values_num > 1)
385   {
386     WARNING ("Filter subsystem: `Rule' blocks have at most one argument.");
387     return (-1);
388   }
389   else if ((ci->values_num == 1)
390       && (ci->values[0].type != OCONFIG_TYPE_STRING))
391   {
392     WARNING ("Filter subsystem: `Rule' blocks expect one string argument "
393         "or no argument at all.");
394     return (-1);
395   }
396
397   rule = (fc_rule_t *) malloc (sizeof (*rule));
398   if (rule == NULL)
399   {
400     ERROR ("fc_config_add_rule: malloc failed.");
401     return (-1);
402   }
403   memset (rule, 0, sizeof (*rule));
404   rule->next = NULL;
405
406   if (ci->values_num == 1)
407   {
408     sstrncpy (rule->name, ci->values[0].value.string, sizeof (rule->name));
409     ssnprintf (rule_name, sizeof (rule_name), "Rule \"%s\"",
410         ci->values[0].value.string);
411   }
412
413   for (i = 0; i < ci->children_num; i++)
414   {
415     oconfig_item_t *option = ci->children + i;
416     status = 0;
417
418     if (strcasecmp ("Match", option->key) == 0)
419       status = fc_config_add_match (&rule->matches, option);
420     else if (strcasecmp ("Target", option->key) == 0)
421       status = fc_config_add_target (&rule->targets, option);
422     else
423     {
424       WARNING ("Filter subsystem: %s: Option `%s' not allowed "
425           "inside a <Rule> block.", rule_name, option->key);
426       status = -1;
427     }
428
429     if (status != 0)
430       break;
431   } /* for (ci->children) */
432
433   /* Additional sanity checking. */
434   while (status == 0)
435   {
436     if (rule->targets == NULL)
437     {
438       WARNING ("Filter subsystem: %s: No target has been specified.",
439           rule_name);
440       status = -1;
441       break;
442     }
443
444     break;
445   } /* while (status == 0) */
446
447   if (status != 0)
448   {
449     fc_free_rules (rule);
450     return (-1);
451   }
452
453   if (chain->rules != NULL)
454   {
455     fc_rule_t *ptr;
456
457     ptr = chain->rules;
458     while (ptr->next != NULL)
459       ptr = ptr->next;
460
461     ptr->next = rule;
462   }
463   else
464   {
465     chain->rules = rule;
466   }
467
468   return (0);
469 } /* }}} int fc_config_add_rule */
470
471 static int fc_config_add_chain (const oconfig_item_t *ci) /* {{{ */
472 {
473   fc_chain_t *chain;
474   int status = 0;
475   int i;
476
477   if ((ci->values_num != 1)
478       || (ci->values[0].type != OCONFIG_TYPE_STRING))
479   {
480     WARNING ("Filter subsystem: <Chain> blocks require exactly one "
481         "string argument.");
482     return (-1);
483   }
484
485   chain = (fc_chain_t *) malloc (sizeof (*chain));
486   if (chain == NULL)
487   {
488     ERROR ("fc_config_add_chain: malloc failed.");
489     return (-1);
490   }
491   memset (chain, 0, sizeof (*chain));
492   sstrncpy (chain->name, ci->values[0].value.string, sizeof (chain->name));
493   chain->rules = NULL;
494   chain->targets = NULL;
495   chain->next = NULL;
496
497   for (i = 0; i < ci->children_num; i++)
498   {
499     oconfig_item_t *option = ci->children + i;
500     status = 0;
501
502     if (strcasecmp ("Rule", option->key) == 0)
503       status = fc_config_add_rule (chain, option);
504     else if (strcasecmp ("Target", option->key) == 0)
505       status = fc_config_add_target (&chain->targets, option);
506     else
507     {
508       WARNING ("Filter subsystem: Chain %s: Option `%s' not allowed "
509           "inside a <Chain> block.", chain->name, option->key);
510       status = -1;
511     }
512
513     if (status != 0)
514       break;
515   } /* for (ci->children) */
516
517   /* Additional sanity checking. */
518   while (status == 0)
519   {
520     if (chain->targets == NULL)
521     {
522       WARNING ("Filter subsystem: Chain %s: No default target has been "
523           "specified. Please make sure that there is a <Target> block within "
524           "the <Chain> block!", chain->name);
525       status = -1;
526       break;
527     }
528
529     break;
530   } /* while (status == 0) */
531
532   if (status != 0)
533   {
534     fc_free_chains (chain);
535     return (-1);
536   }
537
538   if (chain_list_head != NULL)
539   {
540     fc_chain_t *ptr;
541
542     ptr = chain_list_head;
543     while (ptr->next != NULL)
544       ptr = ptr->next;
545
546     ptr->next = chain;
547   }
548   else
549   {
550     chain_list_head = chain;
551   }
552
553   return (0);
554 } /* }}} int fc_config_add_chain */
555
556 /*
557  * Built-in target "jump"
558  *
559  * Prefix `bit' like `_b_uilt-_i_n _t_arget'
560  */
561 static int fc_bit_jump_create (const oconfig_item_t *ci, /* {{{ */
562     void **user_data)
563 {
564   oconfig_item_t *ci_chain;
565
566   if (ci->children_num != 1)
567   {
568     ERROR ("Filter subsystem: The built-in target `jump' needs exactly "
569         "one `Chain' argument!");
570     return (-1);
571   }
572
573   ci_chain = ci->children;
574   if (strcasecmp ("Chain", ci_chain->key) != 0)
575   {
576     ERROR ("Filter subsystem: The built-in target `jump' does not "
577         "support the configuration option `%s'.",
578         ci_chain->key);
579     return (-1);
580   }
581
582   if ((ci_chain->values_num != 1)
583       || (ci_chain->values[0].type != OCONFIG_TYPE_STRING))
584   {
585     ERROR ("Filter subsystem: Built-in target `jump': The `Chain' option "
586         "needs exactly one string argument.");
587     return (-1);
588   }
589
590   *user_data = fc_strdup (ci_chain->values[0].value.string);
591   if (*user_data == NULL)
592   {
593     ERROR ("fc_bit_jump_create: fc_strdup failed.");
594     return (-1);
595   }
596
597   return (0);
598 } /* }}} int fc_bit_jump_create */
599
600 static int fc_bit_jump_destroy (void **user_data) /* {{{ */
601 {
602   if (user_data != NULL)
603   {
604     free (*user_data);
605     *user_data = NULL;
606   }
607
608   return (0);
609 } /* }}} int fc_bit_jump_destroy */
610
611 static int fc_bit_jump_invoke (const data_set_t *ds, /* {{{ */
612     value_list_t *vl, notification_meta_t __attribute__((unused)) **meta,
613     void **user_data)
614 {
615   char *chain_name;
616   fc_chain_t *chain;
617   int status;
618
619   chain_name = *user_data;
620
621   for (chain = chain_list_head; chain != NULL; chain = chain->next)
622     if (strcasecmp (chain_name, chain->name) == 0)
623       break;
624
625   if (chain == NULL)
626   {
627     ERROR ("Filter subsystem: Built-in target `jump': There is no chain "
628         "named `%s'.", chain_name);
629     return (-1);
630   }
631
632   status = fc_process_chain (ds, vl, chain);
633   if (status < 0)
634     return (status);
635   else if (status == FC_TARGET_STOP)
636     return (FC_TARGET_STOP);
637   else
638     return (FC_TARGET_CONTINUE);
639 } /* }}} int fc_bit_jump_invoke */
640
641 static int fc_bit_stop_invoke (const data_set_t __attribute__((unused)) *ds, /* {{{ */
642     value_list_t __attribute__((unused)) *vl,
643     notification_meta_t __attribute__((unused)) **meta,
644     void __attribute__((unused)) **user_data)
645 {
646   return (FC_TARGET_STOP);
647 } /* }}} int fc_bit_stop_invoke */
648
649 static int fc_bit_return_invoke (const data_set_t __attribute__((unused)) *ds, /* {{{ */
650     value_list_t __attribute__((unused)) *vl,
651     notification_meta_t __attribute__((unused)) **meta,
652     void __attribute__((unused)) **user_data)
653 {
654   return (FC_TARGET_RETURN);
655 } /* }}} int fc_bit_return_invoke */
656
657 static int fc_bit_write_create (const oconfig_item_t *ci, /* {{{ */
658     void **user_data)
659 {
660   int i;
661
662   char **plugin_list;
663   size_t plugin_list_len;
664
665   plugin_list = NULL;
666   plugin_list_len = 0;
667
668   for (i = 0; i < ci->children_num; i++)
669   {
670     oconfig_item_t *child = ci->children + i;
671     char **temp;
672     int j;
673
674     if (strcasecmp ("Plugin", child->key) != 0)
675     {
676       ERROR ("Filter subsystem: The built-in target `write' does not "
677           "support the configuration option `%s'.",
678           child->key);
679       continue;
680     }
681
682     for (j = 0; j < child->values_num; j++)
683     {
684       if (child->values[j].type != OCONFIG_TYPE_STRING)
685       {
686         ERROR ("Filter subsystem: Built-in target `write': "
687             "The `Plugin' option accepts only string arguments.");
688         continue;
689       }
690
691       temp = (char **) realloc (plugin_list, (plugin_list_len + 2)
692           * (sizeof (*plugin_list)));
693       if (temp == NULL)
694       {
695         ERROR ("fc_bit_write_create: realloc failed.");
696         continue;
697       }
698       plugin_list = temp;
699
700       plugin_list[plugin_list_len] = fc_strdup (child->values[j].value.string);
701       if (plugin_list[plugin_list_len] == NULL)
702       {
703         ERROR ("fc_bit_write_create: fc_strdup failed.");
704         continue;
705       }
706       plugin_list_len++;
707       plugin_list[plugin_list_len] = NULL;
708     } /* for (j = 0; j < child->values_num; j++) */
709   } /* for (i = 0; i < ci->children_num; i++) */
710
711   *user_data = plugin_list;
712
713   return (0);
714 } /* }}} int fc_bit_write_create */
715
716 static int fc_bit_write_destroy (void **user_data) /* {{{ */
717 {
718   char **plugin_list;
719   size_t i;
720
721   if ((user_data == NULL) || (*user_data == NULL))
722     return (0);
723
724   plugin_list = *user_data;
725
726   for (i = 0; plugin_list[i] != NULL; i++)
727     free (plugin_list[i]);
728   free (plugin_list);
729
730   return (0);
731 } /* }}} int fc_bit_write_destroy */
732
733 static int fc_bit_write_invoke (const data_set_t *ds, /* {{{ */
734     value_list_t *vl, notification_meta_t __attribute__((unused)) **meta,
735     void **user_data)
736 {
737   char **plugin_list;
738   int status;
739
740   plugin_list = NULL;
741   if (user_data != NULL)
742     plugin_list = *user_data;
743
744   if ((plugin_list == NULL) || (plugin_list[0] == NULL))
745   {
746     status = plugin_write (/* plugin = */ NULL, ds, vl);
747     if (status != 0)
748     {
749       INFO ("Filter subsystem: Built-in target `write': Dispatching value to "
750           "all write plugins failed with status %i.", status);
751     }
752   }
753   else
754   {
755     size_t i;
756
757     for (i = 0; plugin_list[i] != NULL; i++)
758     {
759       status = plugin_write (plugin_list[i], ds, vl);
760       if (status != 0)
761       {
762         INFO ("Filter subsystem: Built-in target `write': Dispatching value to "
763             "the `%s' plugin failed with status %i.", plugin_list[i], status);
764       }
765     } /* for (i = 0; plugin_list[i] != NULL; i++) */
766   }
767
768   return (FC_TARGET_CONTINUE);
769 } /* }}} int fc_bit_write_invoke */
770
771 static int fc_init_once (void) /* {{{ */
772 {
773   static int done = 0;
774   target_proc_t tproc;
775
776   if (done != 0)
777     return (0);
778
779   memset (&tproc, 0, sizeof (tproc));
780   tproc.create  = fc_bit_jump_create;
781   tproc.destroy = fc_bit_jump_destroy;
782   tproc.invoke  = fc_bit_jump_invoke;
783   fc_register_target ("jump", tproc);
784
785   memset (&tproc, 0, sizeof (tproc));
786   tproc.create  = NULL;
787   tproc.destroy = NULL;
788   tproc.invoke  = fc_bit_stop_invoke;
789   fc_register_target ("stop", tproc);
790
791   memset (&tproc, 0, sizeof (tproc));
792   tproc.create  = NULL;
793   tproc.destroy = NULL;
794   tproc.invoke  = fc_bit_return_invoke;
795   fc_register_target ("return", tproc);
796
797   memset (&tproc, 0, sizeof (tproc));
798   tproc.create  = fc_bit_write_create;
799   tproc.destroy = fc_bit_write_destroy;
800   tproc.invoke  = fc_bit_write_invoke;
801   fc_register_target ("write", tproc);
802
803   done++;
804   return (0);
805 } /* }}} int fc_init_once */
806
807 /*
808  * Public functions
809  */
810 /* Add a match to list of available matches. */
811 int fc_register_match (const char *name, match_proc_t proc) /* {{{ */
812 {
813   fc_match_t *m;
814
815   DEBUG ("fc_register_match (%s);", name);
816
817   m = (fc_match_t *) malloc (sizeof (*m));
818   if (m == NULL)
819     return (-ENOMEM);
820   memset (m, 0, sizeof (*m));
821
822   sstrncpy (m->name, name, sizeof (m->name));
823   memcpy (&m->proc, &proc, sizeof (m->proc));
824   m->next = NULL;
825
826   if (match_list_head == NULL)
827   {
828     match_list_head = m;
829   }
830   else
831   {
832     fc_match_t *ptr;
833
834     ptr = match_list_head;
835     while (ptr->next != NULL)
836       ptr = ptr->next;
837
838     ptr->next = m;
839   }
840
841   return (0);
842 } /* }}} int fc_register_match */
843
844 /* Add a target to list of available targets. */
845 int fc_register_target (const char *name, target_proc_t proc) /* {{{ */
846 {
847   fc_target_t *t;
848
849   DEBUG ("fc_register_target (%s);", name);
850
851   t = (fc_target_t *) malloc (sizeof (*t));
852   if (t == NULL)
853     return (-ENOMEM);
854   memset (t, 0, sizeof (*t));
855
856   sstrncpy (t->name, name, sizeof (t->name));
857   memcpy (&t->proc, &proc, sizeof (t->proc));
858   t->next = NULL;
859
860   if (target_list_head == NULL)
861   {
862     target_list_head = t;
863   }
864   else
865   {
866     fc_target_t *ptr;
867
868     ptr = target_list_head;
869     while (ptr->next != NULL)
870       ptr = ptr->next;
871
872     ptr->next = t;
873   }
874
875   return (0);
876 } /* }}} int fc_register_target */
877
878 fc_chain_t *fc_chain_get_by_name (const char *chain_name) /* {{{ */
879 {
880   fc_chain_t *chain;
881
882   if (chain_name == NULL)
883     return (NULL);
884
885   for (chain = chain_list_head; chain != NULL; chain = chain->next)
886     if (strcasecmp (chain_name, chain->name) == 0)
887       return (chain);
888
889   return (NULL);
890 } /* }}} int fc_chain_get_by_name */
891
892 int fc_process_chain (const data_set_t *ds, value_list_t *vl, /* {{{ */
893     fc_chain_t *chain)
894 {
895   fc_rule_t *rule;
896   fc_target_t *target;
897   int status;
898
899   if (chain == NULL)
900     return (-1);
901
902   DEBUG ("fc_process_chain (chain = %s);", chain->name);
903
904   status = FC_TARGET_CONTINUE;
905   for (rule = chain->rules; rule != NULL; rule = rule->next)
906   {
907     fc_match_t *match;
908
909     if (rule->name[0] != 0)
910     {
911       DEBUG ("fc_process_chain (%s): Testing the `%s' rule.",
912           chain->name, rule->name);
913     }
914
915     /* N. B.: rule->matches may be NULL. */
916     for (match = rule->matches; match != NULL; match = match->next)
917     {
918       /* FIXME: Pass the meta-data to match targets here (when implemented). */
919       status = (*match->proc.match) (ds, vl, /* meta = */ NULL,
920           &match->user_data);
921       if (status < 0)
922       {
923         WARNING ("fc_process_chain (%s): A match failed.", chain->name);
924         break;
925       }
926       else if (status != FC_MATCH_MATCHES)
927         break;
928     }
929
930     /* for-loop has been aborted: Either error or no match. */
931     if (match != NULL)
932     {
933       status = FC_TARGET_CONTINUE;
934       continue;
935     }
936
937     if (rule->name[0] != 0)
938     {
939       DEBUG ("fc_process_chain (%s): Rule `%s' matches.",
940           chain->name, rule->name);
941     }
942
943     for (target = rule->targets; target != NULL; target = target->next)
944     {
945       /* If we get here, all matches have matched the value. Execute the
946        * target. */
947       /* FIXME: Pass the meta-data to match targets here (when implemented). */
948       status = (*target->proc.invoke) (ds, vl, /* meta = */ NULL,
949           &target->user_data);
950       if (status < 0)
951       {
952         WARNING ("fc_process_chain (%s): A target failed.", chain->name);
953         continue;
954       }
955       else if (status == FC_TARGET_CONTINUE)
956         continue;
957       else if (status == FC_TARGET_STOP)
958         break;
959       else if (status == FC_TARGET_RETURN)
960         break;
961       else
962       {
963         WARNING ("fc_process_chain (%s): Unknown return value "
964             "from target `%s': %i",
965             chain->name, target->name, status);
966       }
967     }
968
969     if ((status == FC_TARGET_STOP)
970         || (status == FC_TARGET_RETURN))
971     {
972       if (rule->name[0] != 0)
973       {
974         DEBUG ("fc_process_chain (%s): Rule `%s' signaled "
975             "the %s condition.",
976             chain->name, rule->name,
977             (status == FC_TARGET_STOP) ? "stop" : "return");
978       }
979       break;
980     }
981     else
982     {
983       status = FC_TARGET_CONTINUE;
984     }
985   } /* for (rule) */
986
987   if (status == FC_TARGET_STOP)
988     return (FC_TARGET_STOP);
989   else if (status == FC_TARGET_RETURN)
990     return (FC_TARGET_CONTINUE);
991
992   /* for-loop has been aborted: A target returned `FC_TARGET_STOP' */
993   if (rule != NULL)
994     return (FC_TARGET_CONTINUE);
995
996   DEBUG ("fc_process_chain (%s): Executing the default targets.",
997       chain->name);
998
999   status = FC_TARGET_CONTINUE;
1000   for (target = chain->targets; target != NULL; target = target->next)
1001   {
1002     /* If we get here, all matches have matched the value. Execute the
1003      * target. */
1004     /* FIXME: Pass the meta-data to match targets here (when implemented). */
1005     status = (*target->proc.invoke) (ds, vl, /* meta = */ NULL,
1006         &target->user_data);
1007     if (status < 0)
1008     {
1009       WARNING ("fc_process_chain (%s): The default target failed.",
1010           chain->name);
1011     }
1012     else if (status == FC_TARGET_CONTINUE)
1013       continue;
1014     else if (status == FC_TARGET_STOP)
1015       break;
1016     else if (status == FC_TARGET_RETURN)
1017       break;
1018     else
1019     {
1020       WARNING ("fc_process_chain (%s): Unknown return value "
1021           "from target `%s': %i",
1022           chain->name, target->name, status);
1023     }
1024   }
1025
1026   if ((status == FC_TARGET_STOP)
1027       || (status == FC_TARGET_RETURN))
1028   {
1029     assert (target != NULL);
1030     DEBUG ("fc_process_chain (%s): Default target `%s' signaled "
1031         "the %s condition.",
1032         chain->name, target->name,
1033         (status == FC_TARGET_STOP) ? "stop" : "return");
1034     if (status == FC_TARGET_STOP)
1035       return (FC_TARGET_STOP);
1036     else
1037       return (FC_TARGET_CONTINUE);
1038   }
1039
1040   DEBUG ("fc_process_chain (%s): Signaling `continue' at end of chain.",
1041       chain->name);
1042
1043   return (FC_TARGET_CONTINUE);
1044 } /* }}} int fc_process_chain */
1045
1046 /* Iterate over all rules in the chain and execute all targets for which all
1047  * matches match. */
1048 int fc_default_action (const data_set_t *ds, value_list_t *vl) /* {{{ */
1049 {
1050   /* FIXME: Pass the meta-data to match targets here (when implemented). */
1051   return (fc_bit_write_invoke (ds, vl,
1052         /* meta = */ NULL, /* user_data = */ NULL));
1053 } /* }}} int fc_default_action */
1054
1055 int fc_configure (const oconfig_item_t *ci) /* {{{ */
1056 {
1057   fc_init_once ();
1058
1059   if (ci == NULL)
1060     return (-EINVAL);
1061
1062   if (strcasecmp ("Chain", ci->key) == 0)
1063     return (fc_config_add_chain (ci));
1064
1065   WARNING ("Filter subsystem: Unknown top level config option `%s'.",
1066       ci->key);
1067
1068   return (-1);
1069 } /* }}} int fc_configure */
1070
1071 /* vim: set sw=2 sts=2 et fdm=marker : */