dccp sender: Keep polling until an entry becomes free (Gerrit Renker).
[paraslash.git] / filter_chain.c
1 /*
2 * Copyright (C) 2005-2007 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file filter_chain.c common helper functions for filter input/output */
8
9 #include "para.h"
10 #include "list.h"
11 #include "sched.h"
12 #include "fd.h"
13 #include "filter.h"
14 #include "error.h"
15 #include "string.h"
16
17 DEFINE_FILTER_ARRAY(filters);
18
19 /**
20 * call the init function of each supported filter
21 *
22 * \param all_filters the array of all supported filters
23 * \sa filter::init
24 */
25 void filter_init(struct filter *all_filters)
26 {
27 struct filter *f;
28
29 for (f = all_filters; f->name; f++)
30 f->init(f);
31 }
32
33 /**
34 * close and destroy a filter callback
35 *
36 * \param fcb the filter callback to close
37 *
38 * This removes \a fcb from the list of filter callbacks and calls
39 * the close callback associated with \a fcb.
40 */
41 static void close_filter_callback(struct filter_callback *fcb)
42 {
43 PARA_NOTICE_LOG("closing filter_callback %p, data: %p\n", fcb, fcb->data);
44 list_del(&fcb->node);
45 fcb->close(fcb);
46 }
47
48 /**
49 * close all callbacks of a filter node
50 *
51 * \param fn the filter node which contains the filter callbacks to be closed
52 *
53 * Call close_filter_callback() for each entry in the filter callback list
54 * of \a fn.
55 */
56 static void close_callbacks(struct filter_node *fn)
57 {
58 struct filter_callback *fcb, *tmp;
59
60 list_for_each_entry_safe(fcb, tmp, &fn->callbacks, node) {
61 PARA_INFO_LOG("closing %s filter callback\n",
62 fn->filter->name);
63 close_filter_callback(fcb);
64 }
65 }
66
67 static void call_callbacks(struct filter_node *fn, char *inbuf, size_t inlen,
68 char *outbuf, size_t outlen)
69 {
70 struct filter_callback *fcb, *tmp;
71 list_for_each_entry_safe(fcb, tmp, &fn->callbacks, node) {
72 int ret;
73 if (inlen && fcb->input_cb) {
74 ret = fcb->input_cb(inbuf, inlen, fcb);
75 if (ret < 0) {
76 close_filter_callback(fcb);
77 continue;
78 }
79 }
80 if (!outlen || !fcb->output_cb)
81 continue;
82 ret = fcb->output_cb(outbuf, outlen, fcb);
83 if (ret < 0)
84 close_filter_callback(fcb);
85 }
86 }
87
88 /**
89 * call the convert function of each filter
90 *
91 * \param s unused
92 * \param t the task containing the filter chain
93 *
94 * This is the core function of the filter subsystem. It loops over the list of
95 * filter nodes determined by \a t and calls the filter's convert function if
96 * there is input available for the filter node in question. If the convert
97 * function consumed some or all of its input data, all registered input
98 * callbacks are called. Similarly, if a convert function produced output, all
99 * registerd output callbacks get called.
100 *
101 * \return The sum of output bytes produced by the convert functions on
102 * success, negative return value on errors (the return value is stored in
103 * t->ret).
104 *
105 * \sa filter_node, filter#convert, filter_callback
106 */
107 void filter_pre_select(__a_unused struct sched *s, struct task *t)
108 {
109 struct filter_chain *fc = t->private_data;
110 struct filter_node *fn;
111 char *ib;
112 size_t *loaded;
113 int conv, conv_total = 0;
114
115 t->ret = -E_FC_EOF;
116 if (fc->output_eof && *fc->output_eof)
117 goto err_out;
118 again:
119 ib = fc->inbuf;
120 loaded = fc->in_loaded;
121 conv = 0;
122 list_for_each_entry(fn, &fc->filters, node) {
123 if (*loaded && fn->loaded < fn->bufsize) {
124 size_t size, old_fn_loaded = fn->loaded;
125 // PARA_DEBUG_LOG("fc %p loaded: %zd, calling %s convert\n",
126 // fc, *loaded, fn->filter->name);
127 t->ret = fn->filter->convert(ib, *loaded, fn);
128 if (t->ret < 0)
129 goto err_out;
130 size = t->ret;
131 call_callbacks(fn, ib, size, fn->buf + old_fn_loaded,
132 fn->loaded - old_fn_loaded);
133 *loaded -= size;
134 conv += size;
135 if (*loaded && size) {
136 // PARA_DEBUG_LOG("moving %zd bytes in input "
137 // "buffer for %s filter\n",
138 // *loaded, fn->filter->name);
139 memmove(ib, ib + size, *loaded);
140 }
141 }
142 ib = fn->buf;
143 loaded = &fn->loaded;
144 }
145 conv_total += conv;
146 // PARA_DEBUG_LOG("eof (in/out/fc): %d/%d/%d out_loaded: %zd, "
147 // "conv: %d, conv_total: %d\n", *fc->input_eof,
148 // fc->output_eof? *fc->output_eof : -42,
149 // fc->eof, *fc->out_loaded, conv, conv_total);
150 if (conv)
151 goto again;
152 t->ret = 1;
153 if (!*fc->input_eof)
154 return;
155 if (*fc->out_loaded)
156 return;
157 if (*fc->in_loaded && conv_total)
158 return;
159 t->ret = -E_FC_EOF;
160 err_out:
161 fc->eof = 1;
162 }
163
164 /**
165 * close all filter nodes and its callbacks
166 *
167 * \param fc the filter chain to close
168 *
169 * For each filter node determined by \a fc, call the close function of each
170 * registered filter callback as well as the close function of the
171 * corresponding filter. Free all resources and destroy all callback lists and
172 * the filter node list.
173 *
174 * \sa filter::close, filter_callback::close
175 */
176 void close_filters(struct filter_chain *fc)
177 {
178 struct filter_node *fn, *tmp;
179
180 if (!fc)
181 return;
182 PARA_NOTICE_LOG("closing filter chain %p\n", fc);
183 list_for_each_entry_safe(fn, tmp, &fc->filters, node) {
184 close_callbacks(fn);
185 PARA_INFO_LOG("closing %s filter\n", fn->filter->name);
186 fn->filter->close(fn);
187 list_del(&fn->node);
188 free(fn);
189 }
190 }
191
192 /*
193 * If the filter has a command line parser and options is not NULL, run it.
194 * Returns filter_num on success, negative on errors
195 */
196 static int parse_filter_args(int filter_num, char *options, void **conf)
197 {
198 struct filter *f = &filters[filter_num];
199 int i, argc = 2;
200 char **argv;
201
202 // PARA_DEBUG_LOG("%s, options: %s, parser: %p\n", f->name,
203 // options? options : "(none)", f->parse_config);
204 if (!f->parse_config)
205 return strlen(options)? -E_BAD_FILTER_OPTIONS : filter_num;
206 // PARA_DEBUG_LOG("options: %s\n", options);
207 argc = split_args(options, &argv, " \t");
208 // PARA_DEBUG_LOG("argc = %d, argv[0]: %s\n", argc, argv[0]);
209 for (i = argc - 1; i >= 0; i--)
210 argv[i + 1] = argv[i];
211 argv[0] = para_strdup(f->name);
212 argc += 1;
213 *conf = f->parse_config(argc, argv);
214 free(argv[0]);
215 free(argv);
216 return *conf? filter_num : -E_BAD_FILTER_OPTIONS;
217 }
218
219 /**
220 * check the filter command line options
221 *
222 * \param fa the command line options
223 * \param conf points to the filter configuration upon successful return
224 *
225 * Check if \a fa starts with a the name of a supported filter, followed by
226 * a colon. If yes, call the command line parser of that filter.
227 *
228 * \return On success, the number of the filter is returned and \a conf
229 * is initialized to point to the filter configuration determined by \a fa.
230 * On errors, a negative value is returned.
231 *
232 * Note: If \a fa specifies a filter that has no command line parser success is
233 * returned, and \a conf is initialized to \p NULL.
234 *
235 * \sa filter::parse_config
236 */
237 int check_filter_arg(char *fa, void **conf)
238 {
239 int j;
240
241 *conf = NULL;
242 // PARA_DEBUG_LOG("arg: %s\n", fa);
243 for (j = 0; filters[j].name; j++) {
244 const char *name = filters[j].name;
245 size_t len = strlen(name);
246 char c;
247 if (strlen(fa) < len)
248 continue;
249 if (strncmp(name, fa, len))
250 continue;
251 c = fa[len];
252 if (c && c != ' ')
253 continue;
254 if (c && !filters[j].parse_config)
255 return -E_BAD_FILTER_OPTIONS;
256 return parse_filter_args(j, c? fa + len + 1 :
257 fa + strlen(fa), conf);
258 }
259 return -E_UNSUPPORTED_FILTER;
260 }
261