2 * Copyright (C) 2005-2009 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file filter_common.c Common helper functions for filter input/output. */
10 #include <sys/types.h>
19 #include "buffer_tree.h"
24 /** The array of supported filters. */
25 struct filter filters[NUM_SUPPORTED_FILTERS] = {FILTER_ARRAY};
28 * Call the init function of each supported filter.
31 void filter_init(void)
35 FOR_EACH_SUPPORTED_FILTER(i)
36 filters[i].init(filters + i);
40 * Close and destroy a filter callback.
42 * \param fcb The filter callback to close.
44 * This removes \a fcb from the list of filter callbacks and calls
45 * the close callback associated with \a fcb.
47 static void close_filter_callback(struct filter_callback *fcb)
49 PARA_NOTICE_LOG("closing filter_callback %p\n", fcb);
55 * Close all callbacks of a filter node.
57 * \param fn The filter node which contains the filter callbacks to be closed.
59 * Call close_filter_callback() for each entry in the filter callback list
62 static void close_callbacks(struct filter_node *fn)
64 struct filter_callback *fcb, *tmp;
66 list_for_each_entry_safe(fcb, tmp, &fn->callbacks, node) {
67 PARA_INFO_LOG("closing %s filter callback\n",
68 filters[fn->filter_num].name);
69 close_filter_callback(fcb);
74 * Close all filter nodes and their callbacks.
76 * \param fc The filter chain to close.
78 * For each filter node determined by \a fc, call the close function of each
79 * registered filter callback as well as the close function of the
80 * corresponding filter. Free all resources and destroy all callback lists and
81 * the filter node list.
83 * \sa filter::close, filter_callback::close
85 void close_filters(struct filter_chain *fc)
87 struct filter_node *fn;
92 PARA_NOTICE_LOG("closing filter chain %p\n", fc);
93 FOR_EACH_FILTER_NODE(fn, fc, i) {
94 struct filter *f = filters + fn->filter_num;
96 PARA_INFO_LOG("closing %s filter\n", f->name);
100 free(fc->filter_nodes);
104 * If the filter has a command line parser and options is not NULL, run it.
105 * Returns filter_num on success, negative on errors
107 static int parse_filter_args(int filter_num, char *options, void **conf)
109 struct filter *f = &filters[filter_num];
110 int ret, i, argc = 2;
113 // PARA_DEBUG_LOG("%s, options: %s, parser: %p\n", f->name,
114 // options? options : "(none)", f->parse_config);
115 if (!f->parse_config)
116 return strlen(options)? -E_BAD_FILTER_OPTIONS : filter_num;
117 // PARA_DEBUG_LOG("options: %s\n", options);
118 argc = create_argv(options, " \t", &argv);
120 return -E_BAD_FILTER_OPTIONS;
121 PARA_DEBUG_LOG("argc = %d, argv[0]: %s\n", argc, argv[0]);
122 for (i = argc - 1; i >= 0; i--)
123 argv[i + 1] = argv[i];
124 argv[0] = para_strdup(f->name);
126 ret = f->parse_config(argc, argv, conf);
127 free(argv[argc - 1]);
128 argv[argc - 1] = NULL;
130 return ret < 0? ret : filter_num;
134 * Check the filter command line options.
136 * \param fa The command line options.
137 * \param conf Points to the filter configuration upon successful return.
139 * Check if \a fa starts with a the name of a supported filter, followed by
140 * a colon. If yes, call the command line parser of that filter.
142 * \return On success, the number of the filter is returned and \a conf
143 * is initialized to point to the filter configuration determined by \a fa.
144 * On errors, a negative value is returned.
146 * Note: If \a fa specifies a filter that has no command line parser success is
147 * returned, and \a conf is initialized to \p NULL.
149 * \sa filter::parse_config
151 int check_filter_arg(char *fa, void **conf)
156 // PARA_DEBUG_LOG("arg: %s\n", fa);
157 FOR_EACH_SUPPORTED_FILTER(j) {
158 const char *name = filters[j].name;
159 size_t len = strlen(name);
161 if (strlen(fa) < len)
163 if (strncmp(name, fa, len))
168 if (c && !filters[j].parse_config)
169 return -E_BAD_FILTER_OPTIONS;
170 return parse_filter_args(j, c? fa + len + 1 :
171 fa + strlen(fa), conf);
173 return -E_UNSUPPORTED_FILTER;
176 void print_filter_helps(int detailed)
180 printf_or_die("\nAvailable filters: \n\t");
181 FOR_EACH_SUPPORTED_FILTER(i)
182 printf_or_die("%s%s", i? " " : "", filters[i].name);
183 printf_or_die("\n\n");
185 FOR_EACH_SUPPORTED_FILTER(i) {
186 struct filter *f = filters + i;
188 if (!f->help.short_help)
190 printf_or_die("Options for %s:\n", f->name);
191 ggo_print_help(&f->help, detailed);
196 void generic_filter_pre_select(struct sched *s, struct task *t)
198 struct filter_node *fn = container_of(t, struct filter_node, task);
201 if (btr_node_status(fn->btrn, fn->min_iqs, BTR_NT_INTERNAL) != 0) {
202 s->timeout.tv_sec = 0;
203 s->timeout.tv_usec = 1;