2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2006, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
7 * Kevin P. Fleming <kpfleming@digium.com>
8 * Luigi Rizzo <rizzo@icir.org>
10 * See http://www.asterisk.org for more information about
11 * the Asterisk project. Please do not directly contact
12 * any of the maintainers of this project for assistance;
13 * the project provides a web site, mailing lists and IRC
14 * channels for your use.
16 * This program is free software, distributed under the terms of
17 * the GNU General Public License Version 2. See the LICENSE file
18 * at the top of the source tree.
22 * \brief Asterisk module definitions.
24 * This file contains the definitons for functions Asterisk modules should
25 * provide and some other module related functions.
28 #ifndef _ASTERISK_MODULE_H
29 #define _ASTERISK_MODULE_H
31 #include "asterisk/utils.h"
33 #if defined(__cplusplus) || defined(c_plusplus)
37 /*! \brief The text the key() function should return. */
38 #define ASTERISK_GPL_KEY \
39 "This paragraph is copyright (c) 2006 by Digium, Inc. \
40 In order for your module to load, it must return this \
41 key via a function called \"key\". Any code which \
42 includes this paragraph must be licensed under the GNU \
43 General Public License version 2 or later (at your \
44 option). In addition to Digium's general reservations \
45 of rights, Digium expressly reserves the right to \
46 allow other parties to license this paragraph under \
47 different terms. Any use of Digium, Inc. trademarks or \
48 logos (including \"Asterisk\" or \"Digium\") without \
49 express written permission of Digium, Inc. is prohibited.\n"
51 #define AST_MODULE_CONFIG "modules.conf" /*!< \brief Module configuration file */
53 enum ast_module_unload_mode {
54 AST_FORCE_SOFT = 0, /*!< Softly unload a module, only if not in use */
55 AST_FORCE_FIRM = 1, /*!< Firmly unload a module, even if in use */
56 AST_FORCE_HARD = 2, /*!< as FIRM, plus dlclose() on the module. Not recommended
57 as it may cause crashes */
60 enum ast_module_load_result {
61 AST_MODULE_LOAD_SUCCESS = 0, /*!< Module loaded and configured */
62 AST_MODULE_LOAD_DECLINE = 1, /*!< Module is not configured */
63 AST_MODULE_LOAD_SKIP = 2, /*!< Module was skipped for some reason */
64 AST_MODULE_LOAD_FAILURE = -1, /*!< Module could not be loaded properly */
68 * \brief Load a module.
69 * \param resource_name The name of the module to load.
71 * This function is run by the PBX to load the modules. It performs
72 * all loading and initilization tasks. Basically, to load a module, just
73 * give it the name of the module and it will do the rest.
75 * \return See possible enum values for ast_module_load_result.
77 enum ast_module_load_result ast_load_resource(const char *resource_name);
80 * \brief Unload a module.
81 * \param resource_name The name of the module to unload.
82 * \param ast_module_unload_mode The force flag. This should be set using one of the AST_FORCE flags.
84 * This function unloads a module. It will only unload modules that are not in
85 * use (usecount not zero), unless #AST_FORCE_FIRM or #AST_FORCE_HARD is
86 * specified. Setting #AST_FORCE_FIRM or #AST_FORCE_HARD will unload the
87 * module regardless of consequences (NOT RECOMMENDED).
89 * \retval 0 on success.
90 * \retval -1 on error.
92 int ast_unload_resource(const char *resource_name, enum ast_module_unload_mode);
95 * \brief Notify when usecount has been changed.
97 * This function calulates use counts and notifies anyone trying to keep track
98 * of them. It should be called whenever your module's usecount changes.
100 * \note The LOCAL_USER macros take care of calling this function for you.
102 void ast_update_use_count(void);
105 * \brief Ask for a list of modules, descriptions, and use counts.
106 * \param modentry A callback to an updater function.
109 * For each of the modules loaded, modentry will be executed with the resource,
110 * description, and usecount values of each particular module.
112 * \return the number of modules loaded
114 int ast_update_module_list(int (*modentry)(const char *module, const char *description, int usecnt, const char *like),
118 * \brief Check if module with the name given is loaded
119 * \param name Module name, like "chan_sip.so"
123 int ast_module_check(const char *name);
126 * \brief Add a procedure to be run when modules have been updated.
127 * \param updater The function to run when modules have been updated.
129 * This function adds the given function to a linked list of functions to be
130 * run when the modules are updated.
132 * \retval 0 on success
133 * \retval -1 on failure.
135 int ast_loader_register(int (*updater)(void));
138 * \brief Remove a procedure to be run when modules are updated.
139 * \param updater The updater function to unregister.
141 * This removes the given function from the updater list.
143 * \retval 0 on success
144 * \retval -1 on failure.
146 int ast_loader_unregister(int (*updater)(void));
149 * \brief Run the unload() callback for all loaded modules
151 * This function should be called when Asterisk is shutting down gracefully.
153 void ast_module_shutdown(void);
156 * \brief Match modules names for the Asterisk cli.
157 * \param line Unused by this function, but this should be the line we are
159 * \param word The partial name to match.
160 * \param pos The position the word we are completing is in.
161 * \param state The possible match to return.
162 * \param rpos The position we should be matching. This should be the same as
164 * \param needsreload This should be 1 if we need to reload this module and 0
165 * otherwise. This function will only return modules that are reloadble
168 * \retval A possible completion of the partial match.
169 * \retval NULL if no matches were found.
171 char *ast_module_helper(const char *line, const char *word, int pos, int state, int rpos, int needsreload);
173 /* Opaque type for module handles generated by the loader */
177 /* User count routines keep track of which channels are using a given module
178 resource. They can help make removing modules safer, particularly if
179 they're in use at the time they have been requested to be removed */
181 struct ast_module_user;
182 struct ast_module_user_list;
184 /*! \page ModMngmnt The Asterisk Module management interface
186 * All modules must implement the module API (load, unload...)
187 * whose functions are exported through fields of a "struct module_symbol";
190 enum ast_module_flags {
191 AST_MODFLAG_DEFAULT = 0,
192 AST_MODFLAG_GLOBAL_SYMBOLS = (1 << 0),
195 struct ast_module_info {
197 /*! The 'self' pointer for a module; it will be set by the loader before
198 it calls the module's load_module() entrypoint, and used by various
199 other macros that need to identify the module.
202 struct ast_module *self;
203 enum ast_module_load_result (*load)(void); /*!< register stuff etc. Optional. */
204 int (*reload)(void); /*!< config etc. Optional. */
205 int (*unload)(void); /*!< unload. called with the module locked */
206 int (*backup_globals)(void); /*!< for embedded modules, backup global data */
207 void (*restore_globals)(void); /*!< for embedded modules, restore global data */
208 const char *name; /*!< name of the module for loader reference and CLI commands */
209 const char *description; /*!< user friendly description of the module. */
212 * This holds the ASTERISK_GPL_KEY, signifiying that you agree to the terms of
213 * the Asterisk license as stated in the ASTERISK_GPL_KEY. Your module will not
214 * load if it does not return the EXACT key string.
219 unsigned int buildopt_sum[4]; /* The value of AST_BUILDOPT_SUM when this module was compiled */
222 void ast_module_register(const struct ast_module_info *);
223 void ast_module_unregister(const struct ast_module_info *);
225 struct ast_module_user *__ast_module_user_add(struct ast_module *, struct ast_channel *);
226 void __ast_module_user_remove(struct ast_module *, struct ast_module_user *);
227 void __ast_module_user_hangup_all(struct ast_module *);
229 #define ast_module_user_add(chan) __ast_module_user_add(ast_module_info->self, chan)
230 #define ast_module_user_remove(user) __ast_module_user_remove(ast_module_info->self, user)
231 #define ast_module_user_hangup_all() __ast_module_user_hangup_all(ast_module_info->self)
233 struct ast_module *ast_module_ref(struct ast_module *);
234 void ast_module_unref(struct ast_module *);
236 #if defined(__cplusplus) || defined(c_plusplus)
237 #define AST_MODULE_INFO(keystr, flags_to_set, desc, load_func, unload_func, reload_func) \
238 static struct ast_module_info __mod_info = { \
249 static void __attribute__ ((constructor)) __reg_module(void) \
251 ast_module_register(&__mod_info); \
253 static void __attribute__ ((destructor)) __unreg_module(void) \
255 ast_module_unregister(&__mod_info); \
257 const static __attribute__((unused)) struct ast_module_info *ast_module_info = &__mod_info
259 #define AST_MODULE_INFO_STANDARD(keystr, desc) \
260 AST_MODULE_INFO(keystr, AST_MODFLAG_DEFAULT, desc, \
266 /* forward declare this pointer in modules, so that macro/function
267 calls that need it can get it, since it will actually be declared
268 and populated at the end of the module's source file... */
269 const static __attribute__((unused)) struct ast_module_info *ast_module_info;
271 #if defined(EMBEDDED_MODULE)
273 #define make_var_sub(mod, type) __ ## mod ## _ ## type
274 #define make_var(mod, type) make_var_sub(mod, type)
276 extern void make_var(EMBEDDED_MODULE, bss_start);
277 extern void make_var(EMBEDDED_MODULE, bss_end);
278 extern void make_var(EMBEDDED_MODULE, data_start);
279 extern void make_var(EMBEDDED_MODULE, data_end);
281 static void * __attribute__((section(".embed_module"))) __global_backup;
283 static int __backup_globals(void)
285 size_t data_size = & make_var(EMBEDDED_MODULE, data_end) - & make_var(EMBEDDED_MODULE, data_start);
293 if (!(__global_backup = ast_malloc(data_size)))
296 memcpy(__global_backup, & make_var(EMBEDDED_MODULE, data_start), data_size);
301 static void __restore_globals(void)
303 size_t data_size = & make_var(EMBEDDED_MODULE, data_end) - & make_var(EMBEDDED_MODULE, data_start);
304 size_t bss_size = & make_var(EMBEDDED_MODULE, bss_end) - & make_var(EMBEDDED_MODULE, bss_start);
307 memset(& make_var(EMBEDDED_MODULE, bss_start), 0, bss_size);
309 if (!data_size || !__global_backup)
312 memcpy(& make_var(EMBEDDED_MODULE, data_start), __global_backup, data_size);
315 #define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...) \
316 static struct ast_module_info \
317 __attribute__((section(".embed_module"))) \
319 .backup_globals = __backup_globals, \
320 .restore_globals = __restore_globals, \
321 .name = AST_MODULE, \
322 .flags = flags_to_set, \
323 .description = desc, \
327 static void __attribute__ ((constructor)) __reg_module(void) \
329 ast_module_register(&__mod_info); \
331 static void __attribute__ ((destructor)) __unreg_module(void) \
333 ast_module_unregister(&__mod_info); \
335 const static struct ast_module_info *ast_module_info = &__mod_info
340 #else /* !defined(EMBEDDED_MODULE) */
342 #define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...) \
343 static struct ast_module_info __mod_info = { \
344 .name = AST_MODULE, \
345 .flags = flags_to_set, \
346 .description = desc, \
348 .buildopt_sum = AST_BUILDOPT_SUM, \
351 static void __attribute__ ((constructor)) __reg_module(void) \
353 ast_module_register(&__mod_info); \
355 static void __attribute__ ((destructor)) __unreg_module(void) \
357 ast_module_unregister(&__mod_info); \
359 const static struct ast_module_info *ast_module_info = &__mod_info
361 #endif /* !defined(EMBEDDED_MODULE) */
363 #define AST_MODULE_INFO_STANDARD(keystr, desc) \
364 AST_MODULE_INFO(keystr, AST_MODFLAG_DEFAULT, desc, \
365 .load = load_module, \
366 .unload = unload_module, \
370 #if defined(__cplusplus) || defined(c_plusplus)
374 #endif /* _ASTERISK_MODULE_H */