Thu Jul 9 13:40:37 2009

Asterisk developer's documentation


module.h

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2008, Digium, Inc.
00005  *
00006  * Mark Spencer <markster@digium.com>
00007  * Kevin P. Fleming <kpfleming@digium.com>
00008  * Luigi Rizzo <rizzo@icir.org>
00009  *
00010  * See http://www.asterisk.org for more information about
00011  * the Asterisk project. Please do not directly contact
00012  * any of the maintainers of this project for assistance;
00013  * the project provides a web site, mailing lists and IRC
00014  * channels for your use.
00015  *
00016  * This program is free software, distributed under the terms of
00017  * the GNU General Public License Version 2. See the LICENSE file
00018  * at the top of the source tree.
00019  */
00020 
00021 /*! \file
00022  * \brief Asterisk module definitions.
00023  *
00024  * This file contains the definitons for functions Asterisk modules should
00025  * provide and some other module related functions.
00026  */
00027 
00028 #ifndef _ASTERISK_MODULE_H
00029 #define _ASTERISK_MODULE_H
00030 
00031 #include "asterisk/utils.h"
00032 
00033 #if defined(__cplusplus) || defined(c_plusplus)
00034 extern "C" {
00035 #endif
00036 
00037 /*! \brief The text the key() function should return. */
00038 #define ASTERISK_GPL_KEY \
00039 "This paragraph is copyright (c) 2006 by Digium, Inc. \
00040 In order for your module to load, it must return this \
00041 key via a function called \"key\".  Any code which \
00042 includes this paragraph must be licensed under the GNU \
00043 General Public License version 2 or later (at your \
00044 option).  In addition to Digium's general reservations \
00045 of rights, Digium expressly reserves the right to \
00046 allow other parties to license this paragraph under \
00047 different terms. Any use of Digium, Inc. trademarks or \
00048 logos (including \"Asterisk\" or \"Digium\") without \
00049 express written permission of Digium, Inc. is prohibited.\n"
00050 
00051 #define AST_MODULE_CONFIG "modules.conf" /*!< \brief Module configuration file */
00052 
00053 enum ast_module_unload_mode {
00054    AST_FORCE_SOFT = 0, /*!< Softly unload a module, only if not in use */
00055    AST_FORCE_FIRM = 1, /*!< Firmly unload a module, even if in use */
00056    AST_FORCE_HARD = 2, /*!< as FIRM, plus dlclose() on the module. Not recommended
00057             as it may cause crashes */
00058 };
00059 
00060 enum ast_module_load_result {
00061    AST_MODULE_LOAD_SUCCESS = 0,  /*!< Module loaded and configured */
00062    AST_MODULE_LOAD_DECLINE = 1,  /*!< Module is not configured */
00063    AST_MODULE_LOAD_SKIP = 2,  /*!< Module was skipped for some reason */
00064    AST_MODULE_LOAD_FAILURE = -1, /*!< Module could not be loaded properly */
00065 };
00066 
00067 /*! 
00068  * \brief Load a module.
00069  * \param resource_name The name of the module to load.
00070  *
00071  * This function is run by the PBX to load the modules.  It performs
00072  * all loading and initilization tasks.   Basically, to load a module, just
00073  * give it the name of the module and it will do the rest.
00074  *
00075  * \return See possible enum values for ast_module_load_result.
00076  */
00077 enum ast_module_load_result ast_load_resource(const char *resource_name);
00078 
00079 /*! 
00080  * \brief Unload a module.
00081  * \param resource_name The name of the module to unload.
00082  * \param ast_module_unload_mode The force flag. This should be set using one of the AST_FORCE flags.
00083  *
00084  * This function unloads a module.  It will only unload modules that are not in
00085  * use (usecount not zero), unless #AST_FORCE_FIRM or #AST_FORCE_HARD is 
00086  * specified.  Setting #AST_FORCE_FIRM or #AST_FORCE_HARD will unload the
00087  * module regardless of consequences (NOT RECOMMENDED).
00088  *
00089  * \retval 0 on success.
00090  * \retval -1 on error.
00091  */
00092 int ast_unload_resource(const char *resource_name, enum ast_module_unload_mode);
00093 
00094 /*! 
00095  * \brief Notify when usecount has been changed.
00096  *
00097  * This function calulates use counts and notifies anyone trying to keep track
00098  * of them.  It should be called whenever your module's usecount changes.
00099  *
00100  * \note The ast_module_user_* functions take care of calling this function for you.
00101  */
00102 void ast_update_use_count(void);
00103 
00104 /*! 
00105  * \brief Ask for a list of modules, descriptions, and use counts.
00106  * \param modentry A callback to an updater function.
00107  * \param like
00108  *
00109  * For each of the modules loaded, modentry will be executed with the resource,
00110  * description, and usecount values of each particular module.
00111  * 
00112  * \return the number of modules loaded
00113  */
00114 int ast_update_module_list(int (*modentry)(const char *module, const char *description, int usecnt, const char *like),
00115             const char *like);
00116 
00117 /*!
00118  * \brief Check if module with the name given is loaded
00119  * \param name Module name, like "chan_sip.so"
00120  * \retval 1 if true 
00121  * \retval 0 if false
00122  */
00123 int ast_module_check(const char *name);
00124 
00125 /*! 
00126  * \brief Add a procedure to be run when modules have been updated.
00127  * \param updater The function to run when modules have been updated.
00128  *
00129  * This function adds the given function to a linked list of functions to be
00130  * run when the modules are updated. 
00131  *
00132  * \retval 0 on success 
00133  * \retval -1 on failure.
00134  */
00135 int ast_loader_register(int (*updater)(void));
00136 
00137 /*! 
00138  * \brief Remove a procedure to be run when modules are updated.
00139  * \param updater The updater function to unregister.
00140  *
00141  * This removes the given function from the updater list.
00142  * 
00143  * \retval 0 on success
00144  * \retval -1 on failure.
00145  */
00146 int ast_loader_unregister(int (*updater)(void));
00147 
00148 /*!
00149  * \brief Run the unload() callback for all loaded modules
00150  *
00151  * This function should be called when Asterisk is shutting down gracefully.
00152  */
00153 void ast_module_shutdown(void);
00154 
00155 /*! 
00156  * \brief Match modules names for the Asterisk cli.
00157  * \param line Unused by this function, but this should be the line we are
00158  *        matching.
00159  * \param word The partial name to match. 
00160  * \param pos The position the word we are completing is in.
00161  * \param state The possible match to return.
00162  * \param rpos The position we should be matching.  This should be the same as
00163  *        pos.
00164  * \param needsreload This should be 1 if we need to reload this module and 0
00165  *        otherwise.  This function will only return modules that are reloadble
00166  *        if this is 1.
00167  *
00168  * \retval A possible completion of the partial match.
00169  * \retval NULL if no matches were found.
00170  */
00171 char *ast_module_helper(const char *line, const char *word, int pos, int state, int rpos, int needsreload);
00172 
00173 /* Opaque type for module handles generated by the loader */
00174 
00175 struct ast_module;
00176 
00177 /* User count routines keep track of which channels are using a given module
00178    resource.  They can help make removing modules safer, particularly if
00179    they're in use at the time they have been requested to be removed */
00180 
00181 struct ast_module_user;
00182 struct ast_module_user_list;
00183 
00184 /*! \page ModMngmnt The Asterisk Module management interface
00185  *
00186  * All modules must implement the module API (load, unload...)
00187  */
00188 
00189 enum ast_module_flags {
00190    AST_MODFLAG_DEFAULT = 0,
00191    AST_MODFLAG_GLOBAL_SYMBOLS = (1 << 0),
00192 };
00193 
00194 struct ast_module_info {
00195 
00196    /*!
00197     * The 'self' pointer for a module; it will be set by the loader before
00198     * it calls the module's load_module() entrypoint, and used by various
00199     * other macros that need to identify the module.
00200     */
00201 
00202    struct ast_module *self;
00203    enum ast_module_load_result (*load)(void);   /*!< register stuff etc. Optional. */
00204    int (*reload)(void);       /*!< config etc. Optional. */
00205    int (*unload)(void);       /*!< unload. called with the module locked */
00206    int (*backup_globals)(void);     /*!< for embedded modules, backup global data */
00207    void (*restore_globals)(void);      /*!< for embedded modules, restore global data */
00208    const char *name;       /*!< name of the module for loader reference and CLI commands */
00209    const char *description;      /*!< user friendly description of the module. */
00210 
00211    /*! 
00212     * This holds the ASTERISK_GPL_KEY, signifiying that you agree to the terms of
00213     * the Asterisk license as stated in the ASTERISK_GPL_KEY.  Your module will not
00214     * load if it does not return the EXACT key string.
00215     */
00216 
00217    const char *key;
00218    unsigned int flags;
00219 
00220    /*! The value of AST_BUILDOPT_SUM when this module was compiled */
00221    const char buildopt_sum[33];
00222 };
00223 
00224 void ast_module_register(const struct ast_module_info *);
00225 void ast_module_unregister(const struct ast_module_info *);
00226 
00227 struct ast_module_user *__ast_module_user_add(struct ast_module *, struct ast_channel *);
00228 void __ast_module_user_remove(struct ast_module *, struct ast_module_user *);
00229 void __ast_module_user_hangup_all(struct ast_module *);
00230 
00231 #define ast_module_user_add(chan) __ast_module_user_add(ast_module_info->self, chan)
00232 #define ast_module_user_remove(user) __ast_module_user_remove(ast_module_info->self, user)
00233 #define ast_module_user_hangup_all() __ast_module_user_hangup_all(ast_module_info->self)
00234 
00235 struct ast_module *ast_module_ref(struct ast_module *);
00236 void ast_module_unref(struct ast_module *);
00237 
00238 #if defined(__cplusplus) || defined(c_plusplus)
00239 #define AST_MODULE_INFO(keystr, flags_to_set, desc, load_func, unload_func, reload_func)  \
00240    static struct ast_module_info __mod_info = { \
00241       NULL,             \
00242       load_func,           \
00243       reload_func,            \
00244       unload_func,            \
00245       NULL,             \
00246       NULL,             \
00247       AST_MODULE,          \
00248       desc,             \
00249       keystr,              \
00250       flags_to_set,           \
00251       AST_BUILDOPT_SUM,       \
00252    };                \
00253    static void  __attribute__((constructor)) __reg_module(void) \
00254    { \
00255       ast_module_register(&__mod_info); \
00256    } \
00257    static void  __attribute__((destructor)) __unreg_module(void) \
00258    { \
00259       ast_module_unregister(&__mod_info); \
00260    } \
00261    const static __attribute__((unused)) struct ast_module_info *ast_module_info = &__mod_info
00262 
00263 #define AST_MODULE_INFO_STANDARD(keystr, desc)     \
00264    AST_MODULE_INFO(keystr, AST_MODFLAG_DEFAULT, desc, \
00265          load_module,         \
00266          unload_module,    \
00267          NULL        \
00268              )
00269 #else /* plain C */
00270 
00271 /* forward declare this pointer in modules, so that macro/function
00272    calls that need it can get it, since it will actually be declared
00273    and populated at the end of the module's source file... */
00274 const static __attribute__((unused)) struct ast_module_info *ast_module_info;
00275 
00276 #if !defined(EMBEDDED_MODULE)
00277 #define __MODULE_INFO_SECTION
00278 #define __MODULE_INFO_GLOBALS
00279 #else
00280 /*
00281  * For embedded modules we need additional information to backup and
00282  * restore the global variables in the module itself, so we can unload
00283  * reload the module.
00284  * EMBEDDED_MODULE is defined as the module name, so the calls to make_var()
00285  * below will actually define different symbols for each module.
00286  */
00287 #define __MODULE_INFO_SECTION __attribute__((section(".embed_module")))
00288 #define __MODULE_INFO_GLOBALS .backup_globals = __backup_globals, .restore_globals = __restore_globals,
00289 
00290 #define make_var_sub(mod, type) __ ## mod ## _ ## type
00291 #define make_var(mod, type) make_var_sub(mod, type)
00292 
00293 extern void make_var(EMBEDDED_MODULE, bss_start);
00294 extern void make_var(EMBEDDED_MODULE, bss_end);
00295 extern void make_var(EMBEDDED_MODULE, data_start);
00296 extern void make_var(EMBEDDED_MODULE, data_end);
00297 
00298 static void * __attribute__((section(".embed_module"))) __global_backup;
00299 
00300 static int __backup_globals(void)
00301 {
00302    size_t data_size = & make_var(EMBEDDED_MODULE, data_end) - & make_var(EMBEDDED_MODULE, data_start);
00303 
00304    if (__global_backup)
00305       return 0;
00306 
00307    if (!data_size)
00308       return 0;
00309 
00310    if (!(__global_backup = ast_malloc(data_size)))
00311       return -1;
00312 
00313    memcpy(__global_backup, & make_var(EMBEDDED_MODULE, data_start), data_size);
00314 
00315    return 0;
00316 }
00317 
00318 static void __restore_globals(void)
00319 {
00320    size_t data_size = & make_var(EMBEDDED_MODULE, data_end) - & make_var(EMBEDDED_MODULE, data_start);
00321    size_t bss_size = & make_var(EMBEDDED_MODULE, bss_end) - & make_var(EMBEDDED_MODULE, bss_start);
00322 
00323    if (bss_size)
00324       memset(& make_var(EMBEDDED_MODULE, bss_start), 0, bss_size);
00325 
00326    if (!data_size || !__global_backup)
00327       return;
00328 
00329    memcpy(& make_var(EMBEDDED_MODULE, data_start), __global_backup, data_size);
00330 }
00331 #undef make_var
00332 #undef make_var_sub
00333 #endif /* EMBEDDED_MODULE */
00334 
00335 #define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...) \
00336    static struct ast_module_info             \
00337       __MODULE_INFO_SECTION            \
00338       __mod_info = {             \
00339       __MODULE_INFO_GLOBALS            \
00340       .name = AST_MODULE,           \
00341       .flags = flags_to_set,           \
00342       .description = desc,          \
00343       .key = keystr,             \
00344       .buildopt_sum = AST_BUILDOPT_SUM,      \
00345       fields                  \
00346    };                   \
00347    static void  __attribute__((constructor)) __reg_module(void) \
00348    { \
00349       ast_module_register(&__mod_info); \
00350    } \
00351    static void  __attribute__((destructor)) __unreg_module(void) \
00352    { \
00353       ast_module_unregister(&__mod_info); \
00354    } \
00355    const static struct ast_module_info *ast_module_info = &__mod_info
00356 
00357 #define AST_MODULE_INFO_STANDARD(keystr, desc)     \
00358    AST_MODULE_INFO(keystr, AST_MODFLAG_DEFAULT, desc, \
00359          .load = load_module,       \
00360          .unload = unload_module,      \
00361              )
00362 #endif   /* plain C */
00363 
00364 /*! 
00365  * \brief Register an application.
00366  *
00367  * \param app Short name of the application
00368  * \param execute a function callback to execute the application. It should return
00369  *                non-zero if the channel needs to be hung up.
00370  * \param synopsis a short description (one line synopsis) of the application
00371  * \param description long description with all of the details about the use of 
00372  *                    the application
00373  * 
00374  * This registers an application with Asterisk's internal application list. 
00375  * \note The individual applications themselves are responsible for registering and unregistering
00376  *       and unregistering their own CLI commands.
00377  * 
00378  * \retval 0 success 
00379  * \retval -1 failure.
00380  */
00381 #define ast_register_application(app, execute, synopsis, description) ast_register_application2(app, execute, synopsis, description, ast_module_info->self)
00382 
00383 /*!
00384  * \brief Register an application.
00385  *
00386  * \param app Short name of the application
00387  * \param execute a function callback to execute the application. It should return
00388  *                non-zero if the channel needs to be hung up.
00389  * \param synopsis a short description (one line synopsis) of the application
00390  * \param description long description with all of the details about the use of
00391  *                    the application
00392  * \param mod module this application belongs to
00393  *
00394  * This registers an application with Asterisk's internal application list.
00395  * \note The individual applications themselves are responsible for registering and unregistering
00396  *       and unregistering their own CLI commands.
00397  *
00398  * \retval 0 success
00399  * \retval -1 failure.
00400  */
00401 int ast_register_application2(const char *app, int (*execute)(struct ast_channel *, void *),
00402                  const char *synopsis, const char *description, void *mod);
00403 
00404 /*! 
00405  * \brief Unregister an application
00406  * 
00407  * \param app name of the application (does not have to be the same string as the one that was registered)
00408  * 
00409  * This unregisters an application from Asterisk's internal application list.
00410  * 
00411  * \retval 0 success 
00412  * \retval -1 failure
00413  */
00414 int ast_unregister_application(const char *app);
00415 
00416 
00417 #if defined(__cplusplus) || defined(c_plusplus)
00418 }
00419 #endif
00420 
00421 #endif /* _ASTERISK_MODULE_H */

Generated on Thu Jul 9 13:40:37 2009 for Asterisk - the Open Source PBX by  doxygen 1.4.7