2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2008, 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 /*! \li \ref module.h uses the configuration file \ref modules.conf
29 * \addtogroup configuration_file
32 /*! \page modules.conf modules.conf
33 * \verbinclude modules.conf.sample
36 #ifndef _ASTERISK_MODULE_H
37 #define _ASTERISK_MODULE_H
39 #include "asterisk/utils.h"
41 #if defined(__cplusplus) || defined(c_plusplus)
45 /*! \brief The text the key() function should return. */
46 #define ASTERISK_GPL_KEY \
47 "This paragraph is copyright (c) 2006 by Digium, Inc. \
48 In order for your module to load, it must return this \
49 key via a function called \"key\". Any code which \
50 includes this paragraph must be licensed under the GNU \
51 General Public License version 2 or later (at your \
52 option). In addition to Digium's general reservations \
53 of rights, Digium expressly reserves the right to \
54 allow other parties to license this paragraph under \
55 different terms. Any use of Digium, Inc. trademarks or \
56 logos (including \"Asterisk\" or \"Digium\") without \
57 express written permission of Digium, Inc. is prohibited.\n"
59 #define AST_MODULE_CONFIG "modules.conf" /*!< \brief Module configuration file */
61 enum ast_module_unload_mode {
62 AST_FORCE_SOFT = 0, /*!< Softly unload a module, only if not in use */
63 AST_FORCE_FIRM = 1, /*!< Firmly unload a module, even if in use */
64 AST_FORCE_HARD = 2, /*!< as FIRM, plus dlclose() on the module. Not recommended
65 as it may cause crashes */
68 enum ast_module_load_result {
69 AST_MODULE_LOAD_SUCCESS = 0, /*!< Module loaded and configured */
70 AST_MODULE_LOAD_DECLINE = 1, /*!< Module is not configured */
71 AST_MODULE_LOAD_SKIP = 2, /*!< Module was skipped for some reason */
72 AST_MODULE_LOAD_PRIORITY = 3, /*!< Module is not loaded yet, but is added to prioity heap */
73 AST_MODULE_LOAD_FAILURE = -1, /*!< Module could not be loaded properly */
77 * \brief Load a module.
78 * \param resource_name The name of the module to load.
80 * This function is run by the PBX to load the modules. It performs
81 * all loading and initialization tasks. Basically, to load a module, just
82 * give it the name of the module and it will do the rest.
84 * \return See possible enum values for ast_module_load_result.
86 enum ast_module_load_result ast_load_resource(const char *resource_name);
89 * \brief Unload a module.
90 * \param resource_name The name of the module to unload.
91 * \param ast_module_unload_mode The force flag. This should be set using one of the AST_FORCE flags.
93 * This function unloads a module. It will only unload modules that are not in
94 * use (usecount not zero), unless #AST_FORCE_FIRM or #AST_FORCE_HARD is
95 * specified. Setting #AST_FORCE_FIRM or #AST_FORCE_HARD will unload the
96 * module regardless of consequences (NOT RECOMMENDED).
98 * \retval 0 on success.
99 * \retval -1 on error.
101 int ast_unload_resource(const char *resource_name, enum ast_module_unload_mode);
104 * \brief Notify when usecount has been changed.
106 * This function calulates use counts and notifies anyone trying to keep track
107 * of them. It should be called whenever your module's usecount changes.
109 * \note The ast_module_user_* functions take care of calling this function for you.
111 void ast_update_use_count(void);
114 * \brief Ask for a list of modules, descriptions, and use counts.
115 * \param modentry A callback to an updater function.
118 * For each of the modules loaded, modentry will be executed with the resource,
119 * description, and usecount values of each particular module.
121 * \return the number of modules loaded
123 int ast_update_module_list(int (*modentry)(const char *module, const char *description, int usecnt, const char *like),
127 * \brief Check if module with the name given is loaded
128 * \param name Module name, like "chan_sip.so"
132 int ast_module_check(const char *name);
135 * \brief Add a procedure to be run when modules have been updated.
136 * \param updater The function to run when modules have been updated.
138 * This function adds the given function to a linked list of functions to be
139 * run when the modules are updated.
141 * \retval 0 on success
142 * \retval -1 on failure.
144 int ast_loader_register(int (*updater)(void));
147 * \brief Remove a procedure to be run when modules are updated.
148 * \param updater The updater function to unregister.
150 * This removes the given function from the updater list.
152 * \retval 0 on success
153 * \retval -1 on failure.
155 int ast_loader_unregister(int (*updater)(void));
158 * \brief Run the unload() callback for all loaded modules
160 * This function should be called when Asterisk is shutting down gracefully.
162 void ast_module_shutdown(void);
165 * \brief Match modules names for the Asterisk cli.
166 * \param line Unused by this function, but this should be the line we are
168 * \param word The partial name to match.
169 * \param pos The position the word we are completing is in.
170 * \param state The possible match to return.
171 * \param rpos The position we should be matching. This should be the same as
173 * \param needsreload This should be 1 if we need to reload this module and 0
174 * otherwise. This function will only return modules that are reloadble
177 * \retval A possible completion of the partial match.
178 * \retval NULL if no matches were found.
180 char *ast_module_helper(const char *line, const char *word, int pos, int state, int rpos, int needsreload);
182 /* Opaque type for module handles generated by the loader */
187 * \brief Get the name of a module.
188 * \param mod A pointer to the module.
189 * \return the name of the module
190 * \retval NULL if mod or mod->info is NULL
192 const char *ast_module_name(const struct ast_module *mod);
194 /* User count routines keep track of which channels are using a given module
195 resource. They can help make removing modules safer, particularly if
196 they're in use at the time they have been requested to be removed */
198 struct ast_module_user;
199 struct ast_module_user_list;
201 /*! \page ModMngmnt The Asterisk Module management interface
203 * All modules must implement the module API (load, unload...)
206 enum ast_module_flags {
207 AST_MODFLAG_DEFAULT = 0,
208 AST_MODFLAG_GLOBAL_SYMBOLS = (1 << 0),
209 AST_MODFLAG_LOAD_ORDER = (1 << 1),
212 enum ast_module_load_priority {
213 AST_MODPRI_REALTIME_DEPEND = 10, /*!< Dependency for a realtime driver */
214 AST_MODPRI_REALTIME_DEPEND2 = 20, /*!< Second level dependency for a realtime driver (func_curl needs res_curl, but is needed by res_config_curl) */
215 AST_MODPRI_REALTIME_DRIVER = 30, /*!< A realtime driver, which provides configuration services for other modules */
216 AST_MODPRI_TIMING = 40, /*!< Dependency for a channel (MOH needs timing interfaces to be fully loaded) */
217 AST_MODPRI_CHANNEL_DEPEND = 50, /*!< Channel driver dependency (may depend upon realtime, e.g. MOH) */
218 AST_MODPRI_CHANNEL_DRIVER = 60, /*!< Channel drivers (provide devicestate) */
219 AST_MODPRI_APP_DEPEND = 70, /*!< Dependency for an application */
220 AST_MODPRI_DEVSTATE_PROVIDER = 80, /*!< Applications and other modules that _provide_ devicestate (e.g. meetme) */
221 AST_MODPRI_DEVSTATE_PLUGIN = 90, /*!< Plugin for a module that provides devstate (e.g. res_calendar_*) */
222 AST_MODPRI_CDR_DRIVER = 100, /*!< CDR or CEL backend */
223 AST_MODPRI_DEFAULT = 128, /*!< Modules not otherwise defined (such as most apps) will load here */
224 AST_MODPRI_DEVSTATE_CONSUMER = 150, /*!< Certain modules, which consume devstate, need to load after all others (e.g. app_queue) */
227 struct ast_module_info {
230 * The 'self' pointer for a module; it will be set by the loader before
231 * it calls the module's load_module() entrypoint, and used by various
232 * other macros that need to identify the module.
235 struct ast_module *self;
236 enum ast_module_load_result (*load)(void); /*!< register stuff etc. Optional. */
237 int (*reload)(void); /*!< config etc. Optional. */
238 int (*unload)(void); /*!< unload. called with the module locked */
239 int (*backup_globals)(void); /*!< for embedded modules, backup global data */
240 void (*restore_globals)(void); /*!< for embedded modules, restore global data */
241 const char *name; /*!< name of the module for loader reference and CLI commands */
242 const char *description; /*!< user friendly description of the module. */
245 * This holds the ASTERISK_GPL_KEY, signifiying that you agree to the terms of
246 * the Asterisk license as stated in the ASTERISK_GPL_KEY. Your module will not
247 * load if it does not return the EXACT key string.
253 /*! The value of AST_BUILDOPT_SUM when this module was compiled */
254 const char buildopt_sum[33];
256 /*! This value represents the order in which a module's load() function is initialized.
257 * The lower this value, the higher the priority. The value is only checked if the
258 * AST_MODFLAG_LOAD_ORDER flag is set. If the AST_MODFLAG_LOAD_ORDER flag is not set,
259 * this value will never be read and the module will be given the lowest possible priority
261 unsigned char load_pri;
263 /*! Modules which should be loaded first, in comma-separated string format.
264 * These are only required for loading, when the optional_api header file
265 * detects that the compiler does not support the optional API featureset. */
266 const char *nonoptreq;
269 void ast_module_register(const struct ast_module_info *);
270 void ast_module_unregister(const struct ast_module_info *);
272 struct ast_module_user *__ast_module_user_add(struct ast_module *, struct ast_channel *);
273 void __ast_module_user_remove(struct ast_module *, struct ast_module_user *);
274 void __ast_module_user_hangup_all(struct ast_module *);
276 #define ast_module_user_add(chan) __ast_module_user_add(ast_module_info->self, chan)
277 #define ast_module_user_remove(user) __ast_module_user_remove(ast_module_info->self, user)
278 #define ast_module_user_hangup_all() __ast_module_user_hangup_all(ast_module_info->self)
280 struct ast_module *ast_module_ref(struct ast_module *);
281 void ast_module_unref(struct ast_module *);
283 #if defined(__cplusplus) || defined(c_plusplus)
284 #define AST_MODULE_INFO(keystr, flags_to_set, desc, load_func, unload_func, reload_func, load_pri) \
285 static struct ast_module_info __mod_info = { \
299 static void __attribute__((constructor)) __reg_module(void) \
301 ast_module_register(&__mod_info); \
303 static void __attribute__((destructor)) __unreg_module(void) \
305 ast_module_unregister(&__mod_info); \
307 static const __attribute__((unused)) struct ast_module_info *ast_module_info = &__mod_info
309 #define AST_MODULE_INFO_STANDARD(keystr, desc) \
310 AST_MODULE_INFO(keystr, AST_MODFLAG_LOAD_ORDER, desc, \
318 /* forward declare this pointer in modules, so that macro/function
319 calls that need it can get it, since it will actually be declared
320 and populated at the end of the module's source file... */
321 static const __attribute__((unused)) struct ast_module_info *ast_module_info;
323 #if !defined(EMBEDDED_MODULE)
324 #define __MODULE_INFO_SECTION
325 #define __MODULE_INFO_GLOBALS
328 * For embedded modules we need additional information to backup and
329 * restore the global variables in the module itself, so we can unload
331 * EMBEDDED_MODULE is defined as the module name, so the calls to make_var()
332 * below will actually define different symbols for each module.
334 #define __MODULE_INFO_SECTION __attribute__((section(".embed_module")))
335 #define __MODULE_INFO_GLOBALS .backup_globals = __backup_globals, .restore_globals = __restore_globals,
337 #define make_var_sub(mod, type) __ ## mod ## _ ## type
338 #define make_var(mod, type) make_var_sub(mod, type)
340 extern void make_var(EMBEDDED_MODULE, bss_start);
341 extern void make_var(EMBEDDED_MODULE, bss_end);
342 extern void make_var(EMBEDDED_MODULE, data_start);
343 extern void make_var(EMBEDDED_MODULE, data_end);
345 static void * __attribute__((section(".embed_module"))) __global_backup;
347 static int __backup_globals(void)
349 size_t data_size = & make_var(EMBEDDED_MODULE, data_end) - & make_var(EMBEDDED_MODULE, data_start);
357 if (!(__global_backup = ast_malloc(data_size)))
360 memcpy(__global_backup, & make_var(EMBEDDED_MODULE, data_start), data_size);
365 static void __restore_globals(void)
367 size_t data_size = & make_var(EMBEDDED_MODULE, data_end) - & make_var(EMBEDDED_MODULE, data_start);
368 size_t bss_size = & make_var(EMBEDDED_MODULE, bss_end) - & make_var(EMBEDDED_MODULE, bss_start);
371 memset(& make_var(EMBEDDED_MODULE, bss_start), 0, bss_size);
373 if (!data_size || !__global_backup)
376 memcpy(& make_var(EMBEDDED_MODULE, data_start), __global_backup, data_size);
380 #endif /* EMBEDDED_MODULE */
382 #define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...) \
383 static struct ast_module_info \
384 __MODULE_INFO_SECTION \
386 __MODULE_INFO_GLOBALS \
387 .name = AST_MODULE, \
388 .flags = flags_to_set, \
389 .description = desc, \
391 .buildopt_sum = AST_BUILDOPT_SUM, \
394 static void __attribute__((constructor)) __reg_module(void) \
396 ast_module_register(&__mod_info); \
398 static void __attribute__((destructor)) __unreg_module(void) \
400 ast_module_unregister(&__mod_info); \
402 static const struct ast_module_info *ast_module_info = &__mod_info
404 #define AST_MODULE_INFO_STANDARD(keystr, desc) \
405 AST_MODULE_INFO(keystr, AST_MODFLAG_LOAD_ORDER, desc, \
406 .load = load_module, \
407 .unload = unload_module, \
408 .load_pri = AST_MODPRI_DEFAULT, \
413 * \brief Register an application.
415 * \param app Short name of the application
416 * \param execute a function callback to execute the application. It should return
417 * non-zero if the channel needs to be hung up.
418 * \param synopsis a short description (one line synopsis) of the application
419 * \param description long description with all of the details about the use of
422 * This registers an application with Asterisk's internal application list.
423 * \note The individual applications themselves are responsible for registering and unregistering
424 * and unregistering their own CLI commands.
427 * \retval -1 failure.
429 #define ast_register_application(app, execute, synopsis, description) ast_register_application2(app, execute, synopsis, description, ast_module_info->self)
432 * \brief Register an application using XML documentation.
434 * \param app Short name of the application
435 * \param execute a function callback to execute the application. It should return
436 * non-zero if the channel needs to be hung up.
438 * This registers an application with Asterisk's internal application list.
439 * \note The individual applications themselves are responsible for registering and unregistering
440 * and unregistering their own CLI commands.
443 * \retval -1 failure.
445 #define ast_register_application_xml(app, execute) ast_register_application(app, execute, NULL, NULL)
449 * \brief Register an application.
451 * \param app Short name of the application
452 * \param execute a function callback to execute the application. It should return
453 * non-zero if the channel needs to be hung up.
454 * \param synopsis a short description (one line synopsis) of the application
455 * \param description long description with all of the details about the use of
457 * \param mod module this application belongs to
459 * This registers an application with Asterisk's internal application list.
460 * \note The individual applications themselves are responsible for registering and unregistering
461 * and unregistering their own CLI commands.
464 * \retval -1 failure.
466 int ast_register_application2(const char *app, int (*execute)(struct ast_channel *, const char *),
467 const char *synopsis, const char *description, void *mod);
470 * \brief Unregister an application
472 * \param app name of the application (does not have to be the same string as the one that was registered)
474 * This unregisters an application from Asterisk's internal application list.
479 int ast_unregister_application(const char *app);
482 #if defined(__cplusplus) || defined(c_plusplus)
486 #endif /* _ASTERISK_MODULE_H */