Codename Pineapple

Home page | Mailing list | Docs

Last updated: Sat Feb 3 05:01:37 2007

Asterisk developer's documentation :: Codename Pineapple


module.h File Reference


Detailed Description

Asterisk module definitions.

This file contains the definitons for functions Asterisk modules should provide and some other module related functions.

Definition in file module.h.

#include "asterisk/utils.h"

Include dependency graph for module.h:

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  ast_module_info

Defines

#define AST_MODULE_CONFIG   "modules.conf"
 Module configuration file.
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
#define AST_MODULE_INFO_STANDARD(keystr, desc)
#define ast_module_user_add(chan)   __ast_module_user_add(ast_module_info->self, chan)
#define ast_module_user_hangup_all()   __ast_module_user_hangup_all(ast_module_info->self)
#define ast_module_user_remove(user)   __ast_module_user_remove(ast_module_info->self, user)
#define ASTERISK_GPL_KEY   "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n"
 The text the key() function should return.

Enumerations

enum  ast_module_flags { AST_MODFLAG_DEFAULT = 0, AST_MODFLAG_GLOBAL_SYMBOLS = (1 << 0) }
enum  ast_module_load_result { AST_MODULE_LOAD_SUCCESS = 0, AST_MODULE_LOAD_DECLINE = 1, AST_MODULE_LOAD_SKIP = 2, AST_MODULE_LOAD_FAILURE = -1 }
enum  ast_module_unload_mode { AST_FORCE_SOFT = 0, AST_FORCE_FIRM = 1, AST_FORCE_HARD = 2 }

Functions

ast_module_user__ast_module_user_add (struct ast_module *, struct ast_channel *)
void __ast_module_user_hangup_all (struct ast_module *)
void __ast_module_user_remove (struct ast_module *, struct ast_module_user *)
static const __attribute__ ((unused)) struct ast_module_info *ast_module_info
enum ast_module_load_result ast_load_resource (const char *resource_name)
 Load a module.
int ast_loader_register (int(*updater)(void))
 Add a procedure to be run when modules have been updated.
int ast_loader_unregister (int(*updater)(void))
 Remove a procedure to be run when modules are updated.
int ast_module_check (char *name)
 Check if module with the name given is loaded.
char * ast_module_helper (const char *line, const char *word, int pos, int state, int rpos, int needsreload)
 Match modules names for the Asterisk cli.
ast_moduleast_module_ref (struct ast_module *)
void ast_module_register (const struct ast_module_info *)
void ast_module_unref (struct ast_module *)
void ast_module_unregister (const struct ast_module_info *)
int ast_unload_resource (const char *resource_name, enum ast_module_unload_mode)
 Unload a module.
int ast_update_module_list (int(*modentry)(const char *module, const char *description, int usecnt, const char *like), const char *like)
 Ask for a list of modules, descriptions, and use counts.
void ast_update_use_count (void)
 Notify when usecount has been changed.


Define Documentation

#define AST_MODULE_CONFIG   "modules.conf"
 

Module configuration file.

Definition at line 51 of file module.h.

Referenced by load_modules().

#define AST_MODULE_INFO keystr,
flags_to_set,
desc,
fields...   ) 
 

Definition at line 256 of file module.h.

#define AST_MODULE_INFO_STANDARD keystr,
desc   ) 
 

Value:

AST_MODULE_INFO(keystr, AST_MODFLAG_DEFAULT, desc, \
         .load = load_module,       \
         .unload = unload_module,      \
             )

Definition at line 274 of file module.h.

#define ast_module_user_add chan   )     __ast_module_user_add(ast_module_info->self, chan)
 

Definition at line 215 of file module.h.

Referenced by __login_exec(), agi_exec_full(), builtin_parkcall(), dundifunc_read(), local_new(), nbs_new(), park_call_exec(), and park_exec().

 
#define ast_module_user_hangup_all  )     __ast_module_user_hangup_all(ast_module_info->self)
 

Definition at line 217 of file module.h.

Referenced by unload_module().

#define ast_module_user_remove user   )     __ast_module_user_remove(ast_module_info->self, user)
 

Definition at line 216 of file module.h.

Referenced by agi_exec_full(), builtin_parkcall(), local_hangup(), nbs_destroy(), park_call_exec(), and park_exec().

#define ASTERISK_GPL_KEY   "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n"
 

The text the key() function should return.

Definition at line 38 of file module.h.

Referenced by key().


Enumeration Type Documentation

enum ast_module_flags
 

Enumerator:
AST_MODFLAG_DEFAULT 
AST_MODFLAG_GLOBAL_SYMBOLS 

Definition at line 179 of file module.h.

00179                       {
00180    AST_MODFLAG_DEFAULT = 0,
00181    AST_MODFLAG_GLOBAL_SYMBOLS = (1 << 0),
00182 };

enum ast_module_load_result
 

Enumerator:
AST_MODULE_LOAD_SUCCESS  Module loaded and configured
AST_MODULE_LOAD_DECLINE  Module is not configured
AST_MODULE_LOAD_SKIP  Module was skipped for some reason
AST_MODULE_LOAD_FAILURE  Module could not be loaded properly

Definition at line 60 of file module.h.

00060                             {
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 };

enum ast_module_unload_mode
 

Enumerator:
AST_FORCE_SOFT  Softly unload a module, only if not in use
AST_FORCE_FIRM  Firmly unload a module, even if in use
AST_FORCE_HARD  as FIRM, plus dlclose() on the module. Not recommended as it may cause crashes

Definition at line 53 of file module.h.

00053                             {
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 };


Function Documentation

struct ast_module_user* __ast_module_user_add struct ast_module ,
struct ast_channel
 

Definition at line 179 of file loader.c.

References ast_calloc, AST_LIST_INSERT_HEAD, AST_LIST_LOCK, AST_LIST_UNLOCK, ast_update_use_count(), ast_module_user::chan, ast_module::usecount, and ast_module::users.

00181 {
00182    struct ast_module_user *u = ast_calloc(1, sizeof(*u));
00183 
00184    if (!u)
00185       return NULL;
00186 
00187    u->chan = chan;
00188 
00189    AST_LIST_LOCK(&mod->users);
00190    AST_LIST_INSERT_HEAD(&mod->users, u, entry);
00191    AST_LIST_UNLOCK(&mod->users);
00192 
00193    ast_atomic_fetchadd_int(&mod->usecount, +1);
00194 
00195    ast_update_use_count();
00196 
00197    return u;
00198 }

void __ast_module_user_hangup_all struct ast_module  ) 
 

Definition at line 211 of file loader.c.

References AST_LIST_LOCK, AST_LIST_REMOVE_HEAD, AST_LIST_UNLOCK, ast_softhangup(), AST_SOFTHANGUP_APPUNLOAD, ast_update_use_count(), ast_module_user::chan, free, ast_module::usecount, and ast_module::users.

Referenced by ast_unload_resource().

00212 {
00213    struct ast_module_user *u;
00214 
00215    AST_LIST_LOCK(&mod->users);
00216    while ((u = AST_LIST_REMOVE_HEAD(&mod->users, entry))) {
00217       ast_softhangup(u->chan, AST_SOFTHANGUP_APPUNLOAD);
00218       ast_atomic_fetchadd_int(&mod->usecount, -1);
00219       free(u);
00220    }
00221    AST_LIST_UNLOCK(&mod->users);
00222 
00223         ast_update_use_count();
00224 }

void __ast_module_user_remove struct ast_module ,
struct ast_module_user
 

Definition at line 200 of file loader.c.

References AST_LIST_LOCK, AST_LIST_REMOVE, AST_LIST_UNLOCK, ast_update_use_count(), free, ast_module::usecount, and ast_module::users.

00201 {
00202    AST_LIST_LOCK(&mod->users);
00203    AST_LIST_REMOVE(&mod->users, u, entry);
00204    AST_LIST_UNLOCK(&mod->users);
00205    ast_atomic_fetchadd_int(&mod->usecount, -1);
00206    free(u);
00207 
00208    ast_update_use_count();
00209 }

static const __attribute__ (unused)   )  [static]
 

enum ast_module_load_result ast_load_resource const char *  resource_name  ) 
 

Load a module.

Parameters:
resource_name The name of the module to load.
This function is run by the PBX to load the modules. It performs all loading and initilization tasks. Basically, to load a module, just give it the name of the module and it will do the rest.

Returns:
See possible enum values for ast_module_load_result.

Definition at line 662 of file loader.c.

References AST_LIST_LOCK, AST_LIST_UNLOCK, and load_resource().

Referenced by file_ok_sel(), handle_load(), and reload_module().

00663 {
00664        AST_LIST_LOCK(&module_list);
00665        load_resource(resource_name, 0);
00666        AST_LIST_UNLOCK(&module_list);
00667 
00668        return 0;
00669 }

int ast_loader_register int(*)(void)  updater  ) 
 

Add a procedure to be run when modules have been updated.

Parameters:
updater The function to run when modules have been updated.
This function adds the given function to a linked list of functions to be run when the modules are updated.

Returns:
Zero on success and -1 on failure.

Definition at line 888 of file loader.c.

References AST_LIST_INSERT_HEAD, AST_LIST_LOCK, AST_LIST_UNLOCK, and ast_malloc.

Referenced by show_console().

00889 {
00890    struct loadupdate *tmp;
00891 
00892    if (!(tmp = ast_malloc(sizeof(*tmp))))
00893       return -1;
00894 
00895    tmp->updater = v;
00896    AST_LIST_LOCK(&module_list);
00897    AST_LIST_INSERT_HEAD(&updaters, tmp, entry);
00898    AST_LIST_UNLOCK(&module_list);
00899 
00900    return 0;
00901 }

int ast_loader_unregister int(*)(void)  updater  ) 
 

Remove a procedure to be run when modules are updated.

Parameters:
updater The updater function to unregister.
This removes the given function from the updater list.

Returns:
Zero on success, -1 on failure.

Definition at line 903 of file loader.c.

References AST_LIST_LOCK, AST_LIST_REMOVE_CURRENT, AST_LIST_TRAVERSE_SAFE_BEGIN, AST_LIST_TRAVERSE_SAFE_END, AST_LIST_UNLOCK, and loadupdate::updater.

Referenced by exit_now().

00904 {
00905    struct loadupdate *cur;
00906 
00907    AST_LIST_LOCK(&module_list);
00908    AST_LIST_TRAVERSE_SAFE_BEGIN(&updaters, cur, entry) {
00909       if (cur->updater == v)  {
00910          AST_LIST_REMOVE_CURRENT(&updaters, entry);
00911          break;
00912       }
00913    }
00914    AST_LIST_TRAVERSE_SAFE_END;
00915    AST_LIST_UNLOCK(&module_list);
00916 
00917    return cur ? 0 : -1;
00918 }

int ast_module_check char *  name  ) 
 

Check if module with the name given is loaded.

Parameters:
name Module name, like "chan_sip.so"
Returns:
0 if false, 1 if true

Definition at line 875 of file loader.c.

References ast_strlen_zero(), and find_resource().

00876 {
00877    struct ast_module *cur;
00878 
00879    if (ast_strlen_zero(name))
00880       return 0;       /* FALSE */
00881 
00882    cur = find_resource(name, 1);
00883 
00884    return (cur != NULL);
00885 }

char* ast_module_helper const char *  line,
const char *  word,
int  pos,
int  state,
int  rpos,
int  needsreload
 

Match modules names for the Asterisk cli.

Parameters:
line Unused by this function, but this should be the line we are matching.
word The partial name to match.
pos The position the word we are completing is in.
state The possible match to return.
rpos The position we should be matching. This should be the same as pos.
needsreload This should be 1 if we need to reload this module and 0 otherwise. This function will only return modules that are reloadble if this is 1.
Returns:
A possible completion of the partial match, or NULL if no matches were found.

Definition at line 489 of file loader.c.

References AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_module::info, name, ast_module_info::reload, and strdup.

Referenced by handle_modlist(), handle_reload(), and handle_unload().

00490 {
00491    struct ast_module *cur;
00492    int i, which=0, l = strlen(word);
00493    char *ret = NULL;
00494 
00495    if (pos != rpos)
00496       return NULL;
00497 
00498    AST_LIST_LOCK(&module_list);
00499    AST_LIST_TRAVERSE(&module_list, cur, entry) {
00500       if (!strncasecmp(word, cur->resource, l) &&
00501           (cur->info->reload || !needsreload) &&
00502           ++which > state) {
00503          ret = strdup(cur->resource);
00504          break;
00505       }
00506    }
00507    AST_LIST_UNLOCK(&module_list);
00508 
00509    if (!ret) {
00510       for (i=0; !ret && reload_classes[i].name; i++) {
00511          if (!strncasecmp(word, reload_classes[i].name, l) && ++which > state)
00512             ret = strdup(reload_classes[i].name);
00513       }
00514    }
00515 
00516    return ret;
00517 }

struct ast_module* ast_module_ref struct ast_module  ) 
 

Definition at line 920 of file loader.c.

References ast_update_use_count(), and ast_module::usecount.

Referenced by __oh323_new(), alsa_new(), ast_iax2_new(), cli_audio_convert(), complete_orig(), fn_wrapper(), gtalk_new(), handle_orig(), mgcp_new(), oss_new(), phone_check_exception(), phone_new(), sip_new(), and skinny_new().

00921 {
00922    ast_atomic_fetchadd_int(&mod->usecount, +1);
00923    ast_update_use_count();
00924 
00925    return mod;
00926 }

void ast_module_register const struct ast_module_info  ) 
 

Definition at line 117 of file loader.c.

References ast_calloc, AST_LIST_HEAD_INIT, AST_LIST_INSERT_TAIL, AST_LIST_LOCK, AST_LIST_UNLOCK, embedding, ast_module::info, ast_module_info::name, resource_being_loaded, ast_module_info::self, and ast_module::users.

00118 {
00119    struct ast_module *mod;
00120 
00121    if (embedding) {
00122       if (!(mod = ast_calloc(1, sizeof(*mod) + strlen(info->name) + 1)))
00123          return;
00124       strcpy(mod->resource, info->name);
00125    } else {
00126       mod = resource_being_loaded;
00127    }
00128 
00129    mod->info = info;
00130    AST_LIST_HEAD_INIT(&mod->users);
00131 
00132    /* during startup, before the loader has been initialized,
00133       there are no threads, so there is no need to take the lock
00134       on this list to manipulate it. it is also possible that it
00135       might be unsafe to use the list lock at that point... so
00136       let's avoid it altogether
00137    */
00138    if (!embedding)
00139       AST_LIST_LOCK(&module_list);
00140 
00141    /* it is paramount that the new entry be placed at the tail of
00142       the list, otherwise the code that uses dlopen() to load
00143       dynamic modules won't be able to find out if the module it
00144       just opened was registered or failed to load
00145    */
00146    AST_LIST_INSERT_TAIL(&module_list, mod, entry);
00147 
00148    if (!embedding)
00149       AST_LIST_UNLOCK(&module_list);
00150 
00151    /* give the module a copy of its own handle, for later use in registrations and the like */
00152    *((struct ast_module **) &(info->self)) = mod;
00153 }

void ast_module_unref struct ast_module  ) 
 

Definition at line 928 of file loader.c.

References ast_update_use_count(), and ast_module::usecount.

Referenced by alsa_hangup(), ast_closestream(), ast_smdi_interface_destroy(), cli_audio_convert(), complete_orig(), destroy(), gtalk_hangup(), handle_orig(), iax2_predestroy(), mgcp_hangup(), oh323_hangup(), oss_hangup(), phone_check_exception(), phone_hangup(), sip_hangup(), and zt_hangup().

00929 {
00930    ast_atomic_fetchadd_int(&mod->usecount, -1);
00931    ast_update_use_count();
00932 }

void ast_module_unregister const struct ast_module_info  ) 
 

Definition at line 155 of file loader.c.

References AST_LIST_HEAD_DESTROY, AST_LIST_LOCK, AST_LIST_REMOVE_CURRENT, AST_LIST_TRAVERSE_SAFE_BEGIN, AST_LIST_TRAVERSE_SAFE_END, AST_LIST_UNLOCK, free, ast_module::info, and ast_module::users.

00156 {
00157    struct ast_module *mod = NULL;
00158 
00159    /* it is assumed that the users list in the module structure
00160       will already be empty, or we cannot have gotten to this
00161       point
00162    */
00163    AST_LIST_LOCK(&module_list);
00164    AST_LIST_TRAVERSE_SAFE_BEGIN(&module_list, mod, entry) {
00165       if (mod->info == info) {
00166          AST_LIST_REMOVE_CURRENT(&module_list, entry);
00167          break;
00168       }
00169    }
00170    AST_LIST_TRAVERSE_SAFE_END;
00171    AST_LIST_UNLOCK(&module_list);
00172 
00173    if (mod) {
00174       AST_LIST_HEAD_DESTROY(&mod->users);
00175       free(mod);
00176    }
00177 }

int ast_unload_resource const char *  resource_name,
enum  ast_module_unload_mode
 

Unload a module.

Parameters:
resource_name The name of the module to unload.
ast_module_unload_mode The force flag. This should be set using one of the AST_FORCE flags.
This function unloads a module. It will only unload modules that are not in use (usecount not zero), unless AST_FORCE_FIRM or AST_FORCE_HARD is specified. Setting AST_FORCE_FIRM or AST_FORCE_HARD will unload the module regardless of consequences (NOT RECOMMENDED).

Returns:
Zero on success, -1 on error.

Definition at line 433 of file loader.c.

References __ast_module_user_hangup_all(), ast_clear_flag, AST_FORCE_FIRM, AST_LIST_LOCK, AST_LIST_UNLOCK, ast_log(), ast_test_flag, ast_update_use_count(), error(), find_resource(), FLAG_DECLINED, FLAG_RUNNING, and ast_module::usecount.

Referenced by exit_now(), handle_unload(), reload_module(), and remove_module().

00434 {
00435    struct ast_module *mod;
00436    int res = -1;
00437    int error = 0;
00438 
00439    AST_LIST_LOCK(&module_list);
00440 
00441    if (!(mod = find_resource(resource_name, 0))) {
00442       AST_LIST_UNLOCK(&module_list);
00443       return 0;
00444    }
00445 
00446    if (!ast_test_flag(mod, FLAG_RUNNING | FLAG_DECLINED))
00447       error = 1;
00448 
00449    if (!error && (mod->usecount > 0)) {
00450       if (force)
00451          ast_log(LOG_WARNING, "Warning:  Forcing removal of module '%s' with use count %d\n",
00452             resource_name, mod->usecount);
00453       else {
00454          ast_log(LOG_WARNING, "Soft unload failed, '%s' has use count %d\n", resource_name,
00455             mod->usecount);
00456          error = 1;
00457       }
00458    }
00459 
00460    if (!error) {
00461       __ast_module_user_hangup_all(mod);
00462       res = mod->info->unload();
00463 
00464       if (res) {
00465          ast_log(LOG_WARNING, "Firm unload failed for %s\n", resource_name);
00466          if (force <= AST_FORCE_FIRM)
00467             error = 1;
00468          else
00469             ast_log(LOG_WARNING, "** Dangerous **: Unloading resource anyway, at user request\n");
00470       }
00471    }
00472 
00473    if (!error)
00474       ast_clear_flag(mod, FLAG_RUNNING | FLAG_DECLINED);
00475 
00476    AST_LIST_UNLOCK(&module_list);
00477 
00478 #if LOADABLE_MODULES
00479    if (!error)
00480       unload_dynamic_module(mod);
00481 #endif
00482 
00483    if (!error)
00484       ast_update_use_count();
00485 
00486    return res;
00487 }

int ast_update_module_list int(*)(const char *module, const char *description, int usecnt, const char *like)  modentry,
const char *  like
 

Ask for a list of modules, descriptions, and use counts.

Parameters:
modentry A callback to an updater function.
like For each of the modules loaded, modentry will be executed with the resource, description, and usecount values of each particular module.
Returns:
the number of modules loaded

Definition at line 854 of file loader.c.

References AST_LIST_TRAVERSE, AST_LIST_TRYLOCK, AST_LIST_UNLOCK, ast_module_info::description, ast_module::info, and ast_module::usecount.

Referenced by handle_modlist(), and mod_update().

00856 {
00857    struct ast_module *cur;
00858    int unlock = -1;
00859    int total_mod_loaded = 0;
00860 
00861    if (AST_LIST_TRYLOCK(&module_list))
00862       unlock = 0;
00863 
00864    AST_LIST_TRAVERSE(&module_list, cur, entry) {
00865       total_mod_loaded += modentry(cur->resource, cur->info->description, cur->usecount, like);
00866    }
00867 
00868    if (unlock)
00869       AST_LIST_UNLOCK(&module_list);
00870 
00871    return total_mod_loaded;
00872 }

void ast_update_use_count void   ) 
 

Notify when usecount has been changed.

This function calulates use counts and notifies anyone trying to keep track of them. It should be called whenever your module's usecount changes.

Note:
The LOCAL_USER macros take care of calling this function for you.

Definition at line 842 of file loader.c.

References AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, and loadupdate::updater.

Referenced by __ast_module_user_add(), __ast_module_user_hangup_all(), __ast_module_user_remove(), ast_module_ref(), ast_module_unref(), ast_unload_resource(), exit_now(), load_module(), load_resource(), oh323_request(), process_sip_queue(), and sip_request_call().

00843 {
00844    /* Notify any module monitors that the use count for a
00845       resource has changed */
00846    struct loadupdate *m;
00847 
00848    AST_LIST_LOCK(&module_list);
00849    AST_LIST_TRAVERSE(&updaters, m, entry)
00850       m->updater();
00851    AST_LIST_UNLOCK(&module_list);
00852 }


Asterisk is a trademark for Digium, inc.. | Edvina.net | Asterisk.org | This documentation was generated with Doxygen