2 * services/modstack.c - stack of modules
4 * Copyright (c) 2007, NLnet Labs. All rights reserved.
6 * This software is open source.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
19 * Neither the name of the NLNET LABS nor the names of its contributors may
20 * be used to endorse or promote products derived from this software without
21 * specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
39 * This file contains functions to help maintain a stack of modules.
43 #include "services/modstack.h"
44 #include "util/module.h"
45 #include "util/fptr_wlist.h"
46 #include "iterator/iterator.h"
47 #include "validator/validator.h"
49 #ifdef WITH_PYTHONMODULE
50 #include "pythonmod/pythonmod.h"
53 /** count number of modules (words) in the string */
55 count_modules(const char* s)
62 while(*s && isspace((int)*s))
64 if(*s && !isspace((int)*s)) {
67 while(*s && !isspace((int)*s))
75 modstack_init(struct module_stack* stack)
82 modstack_config(struct module_stack* stack, const char* module_conf)
85 verbose(VERB_QUERY, "module config: \"%s\"", module_conf);
86 stack->num = count_modules(module_conf);
88 log_err("error: no modules specified");
91 if(stack->num > MAX_MODULE) {
92 log_err("error: too many modules (%d max %d)",
93 stack->num, MAX_MODULE);
96 stack->mod = (struct module_func_block**)calloc((size_t)
97 stack->num, sizeof(struct module_func_block*));
99 log_err("out of memory");
102 for(i=0; i<stack->num; i++) {
103 stack->mod[i] = module_factory(&module_conf);
105 log_err("Unknown value for next module: '%s'",
113 /** The list of module names */
115 module_list_avail(void)
117 /* these are the modules available */
118 static const char* names[] = {
119 #ifdef WITH_PYTHONMODULE
128 /** func block get function type */
129 typedef struct module_func_block* (*fbgetfunctype)(void);
131 /** The list of module func blocks */
132 static fbgetfunctype*
133 module_funcs_avail(void)
135 static struct module_func_block* (*fb[])(void) = {
136 #ifdef WITH_PYTHONMODULE
137 &pythonmod_get_funcblock,
146 module_func_block* module_factory(const char** str)
149 const char* s = *str;
150 const char** names = module_list_avail();
151 fbgetfunctype* fb = module_funcs_avail();
152 while(*s && isspace((int)*s))
155 if(strncmp(names[i], s, strlen(names[i])) == 0) {
156 s += strlen(names[i]);
166 modstack_setup(struct module_stack* stack, const char* module_conf,
167 struct module_env* env)
171 modstack_desetup(stack, env);
172 /* fixed setup of the modules */
173 if(!modstack_config(stack, module_conf)) {
176 env->need_to_validate = 0; /* set by module init below */
177 for(i=0; i<stack->num; i++) {
178 verbose(VERB_OPS, "init module %d: %s",
179 i, stack->mod[i]->name);
180 fptr_ok(fptr_whitelist_mod_init(stack->mod[i]->init));
181 if(!(*stack->mod[i]->init)(env, i)) {
182 log_err("module init for module %s failed",
183 stack->mod[i]->name);
191 modstack_desetup(struct module_stack* stack, struct module_env* env)
194 for(i=0; i<stack->num; i++) {
195 fptr_ok(fptr_whitelist_mod_deinit(stack->mod[i]->deinit));
196 (*stack->mod[i]->deinit)(env, i);
204 modstack_find(struct module_stack* stack, const char* name)
207 for(i=0; i<stack->num; i++) {
208 if(strcmp(stack->mod[i]->name, name) == 0)