1933707f3Ssthen /*
2933707f3Ssthen  * services/modstack.h - stack of modules
3933707f3Ssthen  *
4933707f3Ssthen  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5933707f3Ssthen  *
6933707f3Ssthen  * This software is open source.
7933707f3Ssthen  *
8933707f3Ssthen  * Redistribution and use in source and binary forms, with or without
9933707f3Ssthen  * modification, are permitted provided that the following conditions
10933707f3Ssthen  * are met:
11933707f3Ssthen  *
12933707f3Ssthen  * Redistributions of source code must retain the above copyright notice,
13933707f3Ssthen  * this list of conditions and the following disclaimer.
14933707f3Ssthen  *
15933707f3Ssthen  * Redistributions in binary form must reproduce the above copyright notice,
16933707f3Ssthen  * this list of conditions and the following disclaimer in the documentation
17933707f3Ssthen  * and/or other materials provided with the distribution.
18933707f3Ssthen  *
19933707f3Ssthen  * Neither the name of the NLNET LABS nor the names of its contributors may
20933707f3Ssthen  * be used to endorse or promote products derived from this software without
21933707f3Ssthen  * specific prior written permission.
22933707f3Ssthen  *
23933707f3Ssthen  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
245d76a658Ssthen  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
255d76a658Ssthen  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
265d76a658Ssthen  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
275d76a658Ssthen  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
285d76a658Ssthen  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
295d76a658Ssthen  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
305d76a658Ssthen  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
315d76a658Ssthen  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
325d76a658Ssthen  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
335d76a658Ssthen  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34933707f3Ssthen  */
35933707f3Ssthen 
36933707f3Ssthen /**
37933707f3Ssthen  * \file
38933707f3Ssthen  *
39933707f3Ssthen  * This file contains functions to help maintain a stack of modules.
40933707f3Ssthen  */
41933707f3Ssthen 
42933707f3Ssthen #ifndef SERVICES_MODSTACK_H
43933707f3Ssthen #define SERVICES_MODSTACK_H
44933707f3Ssthen struct module_func_block;
45933707f3Ssthen struct module_env;
46933707f3Ssthen 
47933707f3Ssthen /**
48933707f3Ssthen  * Stack of modules.
49933707f3Ssthen  */
50933707f3Ssthen struct module_stack {
51933707f3Ssthen 	/** the number of modules */
52933707f3Ssthen 	int num;
53933707f3Ssthen 	/** the module callbacks, array of num_modules length (ref only) */
54933707f3Ssthen 	struct module_func_block** mod;
55933707f3Ssthen };
56933707f3Ssthen 
57933707f3Ssthen /**
58933707f3Ssthen  * Init a stack of modules
59933707f3Ssthen  * @param stack: initialised as empty.
60933707f3Ssthen  */
61933707f3Ssthen void modstack_init(struct module_stack* stack);
62933707f3Ssthen 
63933707f3Ssthen /**
64*98bc733bSsthen  * Free the stack of modules
65*98bc733bSsthen  * @param stack: stack that frees up memory.
66*98bc733bSsthen  */
67*98bc733bSsthen void modstack_free(struct module_stack* stack);
68*98bc733bSsthen 
69*98bc733bSsthen /**
70*98bc733bSsthen  * Initialises modules and assignes ids. Calls module_startup().
71*98bc733bSsthen  * @param stack: Expected empty, filled according to module_conf
72*98bc733bSsthen  * @param module_conf: string what modules to initialize
73*98bc733bSsthen  * @param env: module environment which is inited by the modules.
74*98bc733bSsthen  *	environment should have a superalloc, cfg,
75*98bc733bSsthen  * @return on false a module init failed.
76*98bc733bSsthen  */
77*98bc733bSsthen int modstack_call_startup(struct module_stack* stack, const char* module_conf,
78*98bc733bSsthen 	struct module_env* env);
79*98bc733bSsthen 
80*98bc733bSsthen /**
81933707f3Ssthen  * Read config file module settings and set up the modfunc block
82933707f3Ssthen  * @param stack: the stack of modules (empty before call).
83933707f3Ssthen  * @param module_conf: string what modules to insert.
84933707f3Ssthen  * @return false on error
85933707f3Ssthen  */
86933707f3Ssthen int modstack_config(struct module_stack* stack, const char* module_conf);
87933707f3Ssthen 
88933707f3Ssthen /**
89933707f3Ssthen  * Get funcblock for module name
90933707f3Ssthen  * @param str: string with module name. Advanced to next value on success.
91933707f3Ssthen  *	The string is assumed whitespace separated list of module names.
92933707f3Ssthen  * @return funcblock or NULL on error.
93933707f3Ssthen  */
94933707f3Ssthen struct module_func_block* module_factory(const char** str);
95933707f3Ssthen 
96933707f3Ssthen /**
97933707f3Ssthen  * Get list of modules available.
98933707f3Ssthen  * @return list of modules available. Static strings, ends with NULL.
99933707f3Ssthen  */
100933707f3Ssthen const char** module_list_avail(void);
101933707f3Ssthen 
102933707f3Ssthen /**
103*98bc733bSsthen  * Init modules. Calls module_init().
104*98bc733bSsthen  * @param stack: It is modstack_setupped().
105*98bc733bSsthen  * @param module_conf: module ordering to check against the ordering in stack.
106*98bc733bSsthen  *	fails on changed ordering.
107933707f3Ssthen  * @param env: module environment which is inited by the modules.
108933707f3Ssthen  *	environment should have a superalloc, cfg,
109933707f3Ssthen  *	env.need_to_validate is set by the modules.
110933707f3Ssthen  * @return on false a module init failed.
111933707f3Ssthen  */
112*98bc733bSsthen int modstack_call_init(struct module_stack* stack, const char* module_conf,
113933707f3Ssthen 	struct module_env* env);
114933707f3Ssthen 
115933707f3Ssthen /**
116*98bc733bSsthen  * Deinit the modules.
117933707f3Ssthen  * @param stack: made empty.
118933707f3Ssthen  * @param env: module env for module deinit() calls.
119933707f3Ssthen  */
120*98bc733bSsthen void modstack_call_deinit(struct module_stack* stack, struct module_env* env);
121*98bc733bSsthen 
122*98bc733bSsthen /**
123*98bc733bSsthen  * Destartup the modules, close, delete.
124*98bc733bSsthen  * @param stack: made empty.
125*98bc733bSsthen  * @param env: module env for module destartup() calls.
126*98bc733bSsthen  */
127*98bc733bSsthen void modstack_call_destartup(struct module_stack* stack, struct module_env* env);
128933707f3Ssthen 
129933707f3Ssthen /**
130933707f3Ssthen  * Find index of module by name.
131933707f3Ssthen  * @param stack: to look in
132933707f3Ssthen  * @param name: the name to look for
133933707f3Ssthen  * @return -1 on failure, otherwise index number.
134933707f3Ssthen  */
135933707f3Ssthen int modstack_find(struct module_stack* stack, const char* name);
136933707f3Ssthen 
1372be9e038Ssthen /** fetch memory for a module by name, returns 0 if module not there */
1382be9e038Ssthen size_t mod_get_mem(struct module_env* env, const char* name);
1392be9e038Ssthen 
140933707f3Ssthen #endif /* SERVICES_MODSTACK_H */
141