1b7579f77SDag-Erling Smørgrav /* 2b7579f77SDag-Erling Smørgrav * services/modstack.h - stack of modules 3b7579f77SDag-Erling Smørgrav * 4b7579f77SDag-Erling Smørgrav * Copyright (c) 2007, NLnet Labs. All rights reserved. 5b7579f77SDag-Erling Smørgrav * 6b7579f77SDag-Erling Smørgrav * This software is open source. 7b7579f77SDag-Erling Smørgrav * 8b7579f77SDag-Erling Smørgrav * Redistribution and use in source and binary forms, with or without 9b7579f77SDag-Erling Smørgrav * modification, are permitted provided that the following conditions 10b7579f77SDag-Erling Smørgrav * are met: 11b7579f77SDag-Erling Smørgrav * 12b7579f77SDag-Erling Smørgrav * Redistributions of source code must retain the above copyright notice, 13b7579f77SDag-Erling Smørgrav * this list of conditions and the following disclaimer. 14b7579f77SDag-Erling Smørgrav * 15b7579f77SDag-Erling Smørgrav * Redistributions in binary form must reproduce the above copyright notice, 16b7579f77SDag-Erling Smørgrav * this list of conditions and the following disclaimer in the documentation 17b7579f77SDag-Erling Smørgrav * and/or other materials provided with the distribution. 18b7579f77SDag-Erling Smørgrav * 19b7579f77SDag-Erling Smørgrav * Neither the name of the NLNET LABS nor the names of its contributors may 20b7579f77SDag-Erling Smørgrav * be used to endorse or promote products derived from this software without 21b7579f77SDag-Erling Smørgrav * specific prior written permission. 22b7579f77SDag-Erling Smørgrav * 23b7579f77SDag-Erling Smørgrav * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 2417d15b25SDag-Erling Smørgrav * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 2517d15b25SDag-Erling Smørgrav * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 2617d15b25SDag-Erling Smørgrav * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 2717d15b25SDag-Erling Smørgrav * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2817d15b25SDag-Erling Smørgrav * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 2917d15b25SDag-Erling Smørgrav * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 3017d15b25SDag-Erling Smørgrav * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 3117d15b25SDag-Erling Smørgrav * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 3217d15b25SDag-Erling Smørgrav * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 3317d15b25SDag-Erling Smørgrav * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34b7579f77SDag-Erling Smørgrav */ 35b7579f77SDag-Erling Smørgrav 36b7579f77SDag-Erling Smørgrav /** 37b7579f77SDag-Erling Smørgrav * \file 38b7579f77SDag-Erling Smørgrav * 39b7579f77SDag-Erling Smørgrav * This file contains functions to help maintain a stack of modules. 40b7579f77SDag-Erling Smørgrav */ 41b7579f77SDag-Erling Smørgrav 42b7579f77SDag-Erling Smørgrav #ifndef SERVICES_MODSTACK_H 43b7579f77SDag-Erling Smørgrav #define SERVICES_MODSTACK_H 44b7579f77SDag-Erling Smørgrav struct module_func_block; 45b7579f77SDag-Erling Smørgrav struct module_env; 46b7579f77SDag-Erling Smørgrav 47b7579f77SDag-Erling Smørgrav /** 48b7579f77SDag-Erling Smørgrav * Stack of modules. 49b7579f77SDag-Erling Smørgrav */ 50b7579f77SDag-Erling Smørgrav struct module_stack { 51b7579f77SDag-Erling Smørgrav /** the number of modules */ 52b7579f77SDag-Erling Smørgrav int num; 53b7579f77SDag-Erling Smørgrav /** the module callbacks, array of num_modules length (ref only) */ 54b7579f77SDag-Erling Smørgrav struct module_func_block** mod; 55b7579f77SDag-Erling Smørgrav }; 56b7579f77SDag-Erling Smørgrav 57b7579f77SDag-Erling Smørgrav /** 58b7579f77SDag-Erling Smørgrav * Init a stack of modules 59b7579f77SDag-Erling Smørgrav * @param stack: initialised as empty. 60b7579f77SDag-Erling Smørgrav */ 61b7579f77SDag-Erling Smørgrav void modstack_init(struct module_stack* stack); 62b7579f77SDag-Erling Smørgrav 63b7579f77SDag-Erling Smørgrav /** 64*56850988SCy Schubert * Free the stack of modules 65*56850988SCy Schubert * @param stack: stack that frees up memory. 66*56850988SCy Schubert */ 67*56850988SCy Schubert void modstack_free(struct module_stack* stack); 68*56850988SCy Schubert 69*56850988SCy Schubert /** 70*56850988SCy Schubert * Initialises modules and assignes ids. Calls module_startup(). 71*56850988SCy Schubert * @param stack: Expected empty, filled according to module_conf 72*56850988SCy Schubert * @param module_conf: string what modules to initialize 73*56850988SCy Schubert * @param env: module environment which is inited by the modules. 74*56850988SCy Schubert * environment should have a superalloc, cfg, 75*56850988SCy Schubert * @return on false a module init failed. 76*56850988SCy Schubert */ 77*56850988SCy Schubert int modstack_call_startup(struct module_stack* stack, const char* module_conf, 78*56850988SCy Schubert struct module_env* env); 79*56850988SCy Schubert 80*56850988SCy Schubert /** 81b7579f77SDag-Erling Smørgrav * Read config file module settings and set up the modfunc block 82b7579f77SDag-Erling Smørgrav * @param stack: the stack of modules (empty before call). 83b7579f77SDag-Erling Smørgrav * @param module_conf: string what modules to insert. 84b7579f77SDag-Erling Smørgrav * @return false on error 85b7579f77SDag-Erling Smørgrav */ 86b7579f77SDag-Erling Smørgrav int modstack_config(struct module_stack* stack, const char* module_conf); 87b7579f77SDag-Erling Smørgrav 88b7579f77SDag-Erling Smørgrav /** 89b7579f77SDag-Erling Smørgrav * Get funcblock for module name 90b7579f77SDag-Erling Smørgrav * @param str: string with module name. Advanced to next value on success. 91b7579f77SDag-Erling Smørgrav * The string is assumed whitespace separated list of module names. 92b7579f77SDag-Erling Smørgrav * @return funcblock or NULL on error. 93b7579f77SDag-Erling Smørgrav */ 94b7579f77SDag-Erling Smørgrav struct module_func_block* module_factory(const char** str); 95b7579f77SDag-Erling Smørgrav 96b7579f77SDag-Erling Smørgrav /** 97b7579f77SDag-Erling Smørgrav * Get list of modules available. 98b7579f77SDag-Erling Smørgrav * @return list of modules available. Static strings, ends with NULL. 99b7579f77SDag-Erling Smørgrav */ 100b7579f77SDag-Erling Smørgrav const char** module_list_avail(void); 101b7579f77SDag-Erling Smørgrav 102b7579f77SDag-Erling Smørgrav /** 103*56850988SCy Schubert * Init modules. Calls module_init(). 104*56850988SCy Schubert * @param stack: It is modstack_setupped(). 105*56850988SCy Schubert * @param module_conf: module ordering to check against the ordering in stack. 106*56850988SCy Schubert * fails on changed ordering. 107b7579f77SDag-Erling Smørgrav * @param env: module environment which is inited by the modules. 108b7579f77SDag-Erling Smørgrav * environment should have a superalloc, cfg, 109b7579f77SDag-Erling Smørgrav * env.need_to_validate is set by the modules. 110b7579f77SDag-Erling Smørgrav * @return on false a module init failed. 111b7579f77SDag-Erling Smørgrav */ 112*56850988SCy Schubert int modstack_call_init(struct module_stack* stack, const char* module_conf, 113b7579f77SDag-Erling Smørgrav struct module_env* env); 114b7579f77SDag-Erling Smørgrav 115b7579f77SDag-Erling Smørgrav /** 116*56850988SCy Schubert * Deinit the modules. 117b7579f77SDag-Erling Smørgrav * @param stack: made empty. 118b7579f77SDag-Erling Smørgrav * @param env: module env for module deinit() calls. 119b7579f77SDag-Erling Smørgrav */ 120*56850988SCy Schubert void modstack_call_deinit(struct module_stack* stack, struct module_env* env); 121*56850988SCy Schubert 122*56850988SCy Schubert /** 123*56850988SCy Schubert * Destartup the modules, close, delete. 124*56850988SCy Schubert * @param stack: made empty. 125*56850988SCy Schubert * @param env: module env for module destartup() calls. 126*56850988SCy Schubert */ 127*56850988SCy Schubert void modstack_call_destartup(struct module_stack* stack, struct module_env* env); 128b7579f77SDag-Erling Smørgrav 129b7579f77SDag-Erling Smørgrav /** 130b7579f77SDag-Erling Smørgrav * Find index of module by name. 131b7579f77SDag-Erling Smørgrav * @param stack: to look in 132b7579f77SDag-Erling Smørgrav * @param name: the name to look for 133b7579f77SDag-Erling Smørgrav * @return -1 on failure, otherwise index number. 134b7579f77SDag-Erling Smørgrav */ 135b7579f77SDag-Erling Smørgrav int modstack_find(struct module_stack* stack, const char* name); 136b7579f77SDag-Erling Smørgrav 137c7f4d7adSDag-Erling Smørgrav /** fetch memory for a module by name, returns 0 if module not there */ 138c7f4d7adSDag-Erling Smørgrav size_t mod_get_mem(struct module_env* env, const char* name); 139c7f4d7adSDag-Erling Smørgrav 140b7579f77SDag-Erling Smørgrav #endif /* SERVICES_MODSTACK_H */ 141