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